LP-572 Add button_search icon
[librepilot.git] / flight / uavobjects / uavobjectpersistence.c
blob0f0395ce7b073cbe59dab53bbef85e1bde9303ef
1 /**
2 ******************************************************************************
4 * @file uavobjectpersistence.c
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2014.
6 * @brief handles uavo persistence
7 * --
8 * @see The GNU Public License (GPL) Version 3
10 *****************************************************************************/
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "openpilot.h"
28 #include "pios_struct_helper.h"
29 #include "inc/uavobjectprivate.h"
31 extern uintptr_t pios_uavo_settings_fs_id;
33 /**
34 * Save the data of the specified object to the file system (SD card).
35 * If the object contains multiple instances, all of them will be saved.
36 * A new file with the name of the object will be created.
37 * The object data can be restored using the UAVObjLoad function.
38 * @param[in] obj The object handle.
39 * @param[in] instId The instance ID
40 * @return 0 if success or -1 if failure
42 int32_t UAVObjSave(UAVObjHandle obj_handle, uint16_t instId)
44 PIOS_Assert(obj_handle);
46 if (UAVObjIsMetaobject(obj_handle)) {
47 if (instId != 0) {
48 return -1;
51 if (PIOS_FLASHFS_ObjSave(pios_uavo_settings_fs_id, UAVObjGetID(obj_handle), instId, (uint8_t *)MetaDataPtr((struct UAVOMeta *)obj_handle), UAVObjGetNumBytes(obj_handle)) != 0) {
52 return -1;
54 } else {
55 InstanceHandle instEntry = getInstance((struct UAVOData *)obj_handle, instId);
57 if (instEntry == NULL) {
58 return -1;
61 if (InstanceData(instEntry) == NULL) {
62 return -1;
65 if (PIOS_FLASHFS_ObjSave(pios_uavo_settings_fs_id, UAVObjGetID(obj_handle), instId, InstanceData(instEntry), UAVObjGetNumBytes(obj_handle)) != 0) {
66 return -1;
69 return 0;
73 /**
74 * Load an object from the file system (SD card).
75 * A file with the name of the object will be opened.
76 * The object data can be saved using the UAVObjSave function.
77 * @param[in] obj The object handle.
78 * @param[in] instId The object instance
79 * @return 0 if success or -1 if failure
81 int32_t UAVObjLoad(UAVObjHandle obj_handle, uint16_t instId)
83 PIOS_Assert(obj_handle);
85 if (UAVObjIsMetaobject(obj_handle)) {
86 if (instId != 0) {
87 return -1;
90 // Fire event on success
91 if (PIOS_FLASHFS_ObjLoad(pios_uavo_settings_fs_id, UAVObjGetID(obj_handle), instId, (uint8_t *)MetaDataPtr((struct UAVOMeta *)obj_handle), UAVObjGetNumBytes(obj_handle)) == 0) {
92 sendEvent((struct UAVOBase *)obj_handle, instId, EV_UNPACKED);
93 } else {
94 return -1;
96 } else {
97 InstanceHandle instEntry = getInstance((struct UAVOData *)obj_handle, instId);
99 if (instEntry == NULL) {
100 return -1;
103 // Fire event on success
104 if (PIOS_FLASHFS_ObjLoad(pios_uavo_settings_fs_id, UAVObjGetID(obj_handle), instId, InstanceData(instEntry), UAVObjGetNumBytes(obj_handle)) == 0) {
105 sendEvent((struct UAVOBase *)obj_handle, instId, EV_UNPACKED);
106 } else {
107 return -1;
112 return 0;
116 * Delete an object from the file system (SD card).
117 * @param[in] obj The object handle.
118 * @param[in] instId The object instance
119 * @return 0 if success or -1 if failure
121 int32_t UAVObjDelete(UAVObjHandle obj_handle, uint16_t instId)
123 PIOS_Assert(obj_handle);
124 if (PIOS_FLASHFS_ObjDelete(pios_uavo_settings_fs_id, UAVObjGetID(obj_handle), instId) == 0) {
125 UAVObjSetInstanceDefaults(obj_handle, instId);
127 return 0;