2011-05-14 22:15:33 +02:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup UAVObjects OpenPilot UAVObjects
|
|
|
|
* @{
|
|
|
|
* @addtogroup UAV Object Manager
|
|
|
|
* @brief The core UAV Objects functions, most of which are wrappered by
|
|
|
|
* autogenerated defines
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @file uavobjectmanager.h
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief Object manager library. This library holds a collection of all objects.
|
|
|
|
* It can be used by all modules/libraries to find an object reference.
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
2011-05-14 22:15:33 +02:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "openpilot.h"
|
2012-05-26 06:28:05 +02:00
|
|
|
#include "pios_struct_helper.h"
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-09 23:59:27 +02:00
|
|
|
extern uintptr_t pios_uavo_settings_fs_id;
|
|
|
|
|
2013-04-24 22:39:39 +02:00
|
|
|
#if (defined(__MACH__) && defined(__APPLE__))
|
|
|
|
#include <mach-o/getsect.h>
|
|
|
|
#endif
|
|
|
|
|
2011-05-14 22:15:33 +02:00
|
|
|
// Constants
|
|
|
|
|
|
|
|
// Private types
|
|
|
|
|
2012-02-21 02:45:18 +01:00
|
|
|
// Macros
|
2012-04-22 23:56:26 +02:00
|
|
|
#define SET_BITS(var, shift, value, mask) var = (var & ~(mask << shift)) | (value << shift);
|
2012-02-21 02:45:18 +01:00
|
|
|
|
2013-04-24 22:39:39 +02:00
|
|
|
// Mach-o: dummy segment to calculate ASLR offset in sim_osx
|
|
|
|
#if (defined(__MACH__) && defined(__APPLE__))
|
|
|
|
static long _aslr_offset __attribute__((section("__DATA,_aslr")));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Table of UAVO handles */
|
|
|
|
#if (defined(__MACH__) && defined(__APPLE__))
|
|
|
|
/* Mach-o format */
|
2013-05-19 16:37:30 +02:00
|
|
|
static struct UAVOData * *__start__uavo_handles;
|
|
|
|
static struct UAVOData * *__stop__uavo_handles;
|
2013-04-24 22:39:39 +02:00
|
|
|
#else
|
|
|
|
/* ELF format: automagically defined at compile time */
|
2013-05-19 16:37:30 +02:00
|
|
|
extern struct UAVOData *__start__uavo_handles[] __attribute__((weak));
|
|
|
|
extern struct UAVOData *__stop__uavo_handles[] __attribute__((weak));
|
2013-04-24 22:39:39 +02:00
|
|
|
#endif
|
2013-04-14 11:43:37 +02:00
|
|
|
|
|
|
|
#define UAVO_LIST_ITERATE(_item) \
|
2013-05-19 16:37:30 +02:00
|
|
|
for (struct UAVOData * *_uavo_slot = __start__uavo_handles; \
|
|
|
|
_uavo_slot && _uavo_slot < __stop__uavo_handles; \
|
|
|
|
_uavo_slot++) { \
|
|
|
|
struct UAVOData *_item = *_uavo_slot; \
|
|
|
|
if (_item == NULL) { continue; }
|
2013-04-14 11:43:37 +02:00
|
|
|
|
2011-05-14 22:15:33 +02:00
|
|
|
/**
|
|
|
|
* List of event queues and the eventmask associated with the queue.
|
|
|
|
*/
|
|
|
|
|
2012-05-26 06:28:05 +02:00
|
|
|
/** opaque type for instances **/
|
2013-05-19 16:37:30 +02:00
|
|
|
typedef void *InstanceHandle;
|
2012-05-18 13:10:27 +02:00
|
|
|
|
2012-05-26 06:28:05 +02:00
|
|
|
struct ObjectEventEntry {
|
2013-05-19 16:37:30 +02:00
|
|
|
struct ObjectEventEntry *next;
|
|
|
|
xQueueHandle queue;
|
|
|
|
UAVObjEventCallback cb;
|
|
|
|
uint8_t eventMask;
|
2012-05-26 04:39:57 +02:00
|
|
|
};
|
2012-05-17 19:47:18 +02:00
|
|
|
|
2012-05-26 06:28:05 +02:00
|
|
|
/*
|
2013-05-19 16:37:30 +02:00
|
|
|
MetaInstance == [UAVOBase [UAVObjMetadata]]
|
|
|
|
SingleInstance == [UAVOBase [UAVOData [InstanceData]]]
|
|
|
|
MultiInstance == [UAVOBase [UAVOData [NumInstances [InstanceData0 [next]]]]
|
2012-05-26 06:28:05 +02:00
|
|
|
____________________/
|
2013-05-19 16:37:30 +02:00
|
|
|
\-->[InstanceData1 [next]]
|
2012-05-26 06:28:05 +02:00
|
|
|
_________...________/
|
2013-05-19 16:37:30 +02:00
|
|
|
\-->[InstanceDataN [next]]
|
2011-05-14 22:15:33 +02:00
|
|
|
*/
|
2012-05-17 19:47:18 +02:00
|
|
|
|
2012-05-26 06:28:05 +02:00
|
|
|
/*
|
|
|
|
* UAVO Base Type
|
|
|
|
* - All Types of UAVObjects are of this base type
|
|
|
|
* - The flags determine what type(s) this object
|
|
|
|
*/
|
|
|
|
struct UAVOBase {
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Let these objects be added to an event queue */
|
|
|
|
struct ObjectEventEntry *next_event;
|
|
|
|
|
|
|
|
/* Describe the type of object that follows this header */
|
|
|
|
struct UAVOInfo {
|
|
|
|
bool isMeta : 1;
|
|
|
|
bool isSingle : 1;
|
|
|
|
bool isSettings : 1;
|
|
|
|
} flags;
|
2012-05-26 06:28:05 +02:00
|
|
|
} __attribute__((packed));
|
2012-05-17 19:47:18 +02:00
|
|
|
|
2012-05-26 06:28:05 +02:00
|
|
|
/* Augmented type for Meta UAVO */
|
|
|
|
struct UAVOMeta {
|
2013-05-19 16:37:30 +02:00
|
|
|
struct UAVOBase base;
|
|
|
|
UAVObjMetadata instance0;
|
2012-05-17 19:47:18 +02:00
|
|
|
} __attribute__((packed));
|
2012-05-26 04:39:57 +02:00
|
|
|
|
2012-05-26 06:28:05 +02:00
|
|
|
/* Shared data structure for all data-carrying UAVObjects (UAVOSingle and UAVOMulti) */
|
|
|
|
struct UAVOData {
|
2013-05-19 16:37:30 +02:00
|
|
|
struct UAVOBase base;
|
|
|
|
uint32_t id;
|
|
|
|
/*
|
|
|
|
* Embed the Meta object as another complete UAVO
|
|
|
|
* inside the payload for this UAVO.
|
|
|
|
*/
|
|
|
|
struct UAVOMeta metaObj;
|
|
|
|
uint16_t instance_size;
|
2013-04-14 04:22:56 +02:00
|
|
|
} __attribute__((packed, aligned(4)));
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2012-05-26 06:28:05 +02:00
|
|
|
/* Augmented type for Single Instance Data UAVO */
|
|
|
|
struct UAVOSingle {
|
2013-05-19 16:37:30 +02:00
|
|
|
struct UAVOData uavo;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
uint8_t instance0[];
|
|
|
|
/*
|
|
|
|
* Additional space will be malloc'd here to hold the
|
|
|
|
* the data for this instance.
|
|
|
|
*/
|
2012-05-18 13:10:27 +02:00
|
|
|
} __attribute__((packed));
|
2012-05-26 04:39:57 +02:00
|
|
|
|
2012-05-26 06:28:05 +02:00
|
|
|
/* Part of a linked list of instances chained off of a multi instance UAVO. */
|
|
|
|
struct UAVOMultiInst {
|
2013-05-19 16:37:30 +02:00
|
|
|
struct UAVOMultiInst *next;
|
|
|
|
uint8_t instance[];
|
|
|
|
/*
|
|
|
|
* Additional space will be malloc'd here to hold the
|
|
|
|
* the data for this instance.
|
|
|
|
*/
|
2012-05-18 13:10:27 +02:00
|
|
|
} __attribute__((packed));
|
|
|
|
|
2012-05-26 06:28:05 +02:00
|
|
|
/* Augmented type for Multi Instance Data UAVO */
|
|
|
|
struct UAVOMulti {
|
2013-05-19 16:37:30 +02:00
|
|
|
struct UAVOData uavo;
|
|
|
|
uint16_t num_instances;
|
|
|
|
struct UAVOMultiInst instance0 __attribute__((aligned(4)));
|
|
|
|
/*
|
|
|
|
* Additional space will be malloc'd here to hold the
|
|
|
|
* the data for instance 0.
|
|
|
|
*/
|
2012-05-18 13:10:27 +02:00
|
|
|
} __attribute__((packed));
|
|
|
|
|
2012-05-17 19:47:18 +02:00
|
|
|
/** all information about a metaobject are hardcoded constants **/
|
|
|
|
#define MetaNumBytes sizeof(UAVObjMetadata)
|
2013-05-19 16:37:30 +02:00
|
|
|
#define MetaBaseObjectPtr(obj) ((struct UAVOData *)((obj) - offsetof(struct UAVOData, metaObj)))
|
|
|
|
#define MetaObjectPtr(obj) ((struct UAVODataMeta *)&((obj)->metaObj))
|
|
|
|
#define MetaDataPtr(obj) ((UAVObjMetadata *)&((obj)->instance0))
|
|
|
|
#define LinkedMetaDataPtr(obj) ((UAVObjMetadata *)&((obj)->metaObj.instance0))
|
|
|
|
#define MetaObjectId(id) ((id) + 1)
|
2012-05-17 19:47:18 +02:00
|
|
|
|
2012-05-18 13:10:27 +02:00
|
|
|
/** all information about instances are dependant on object type **/
|
2013-05-19 16:37:30 +02:00
|
|
|
#define ObjSingleInstanceDataOffset(obj) ((void *)(&(((struct UAVOSingle *)obj)->instance0)))
|
|
|
|
#define InstanceDataOffset(inst) ((void *)&(((struct UAVOMultiInst *)inst)->instance))
|
|
|
|
#define InstanceData(instance) (void *)instance
|
2012-05-18 13:10:27 +02:00
|
|
|
|
2011-05-14 22:15:33 +02:00
|
|
|
// Private functions
|
2013-05-19 16:37:30 +02:00
|
|
|
static int32_t sendEvent(struct UAVOBase *obj, uint16_t instId,
|
|
|
|
UAVObjEventType event);
|
|
|
|
static InstanceHandle createInstance(struct UAVOData *obj, uint16_t instId);
|
|
|
|
static InstanceHandle getInstance(struct UAVOData *obj, uint16_t instId);
|
2012-05-26 06:28:05 +02:00
|
|
|
static int32_t connectObj(UAVObjHandle obj_handle, xQueueHandle queue,
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjEventCallback cb, uint8_t eventMask);
|
2012-05-26 06:28:05 +02:00
|
|
|
static int32_t disconnectObj(UAVObjHandle obj_handle, xQueueHandle queue,
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjEventCallback cb);
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-15 23:16:34 +02:00
|
|
|
#if defined(PIOS_USE_SETTINGS_ON_SDCARD) && defined(PIOS_INCLUDE_FLASH_LOGFS_SETTINGS)
|
|
|
|
#error Both PIOS_USE_SETTINGS_ON_SDCARD and PIOS_INCLUDE_FLASH_LOGFS_SETTINGS. Only one settings storage allowed.
|
2013-03-30 15:20:05 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PIOS_USE_SETTINGS_ON_SDCARD)
|
2013-05-19 16:37:30 +02:00
|
|
|
static void objectFilename(UAVObjHandle obj_handle, uint8_t *filename);
|
|
|
|
static void customSPrintf(uint8_t *buffer, uint8_t *format, ...);
|
2011-05-14 22:15:33 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Private variables
|
|
|
|
static xSemaphoreHandle mutex;
|
2012-05-26 06:28:05 +02:00
|
|
|
static const UAVObjMetadata defMetadata = {
|
2013-05-19 16:37:30 +02:00
|
|
|
.flags = (ACCESS_READWRITE << UAVOBJ_ACCESS_SHIFT |
|
|
|
|
ACCESS_READWRITE << UAVOBJ_GCS_ACCESS_SHIFT |
|
|
|
|
1 << UAVOBJ_TELEMETRY_ACKED_SHIFT |
|
|
|
|
1 << UAVOBJ_GCS_TELEMETRY_ACKED_SHIFT |
|
|
|
|
UPDATEMODE_ONCHANGE << UAVOBJ_TELEMETRY_UPDATE_MODE_SHIFT |
|
|
|
|
UPDATEMODE_ONCHANGE << UAVOBJ_GCS_TELEMETRY_UPDATE_MODE_SHIFT),
|
|
|
|
.telemetryUpdatePeriod = 0,
|
|
|
|
.gcsTelemetryUpdatePeriod = 0,
|
|
|
|
.loggingUpdatePeriod = 0,
|
2012-05-26 06:28:05 +02:00
|
|
|
};
|
|
|
|
|
2011-05-14 22:15:33 +02:00
|
|
|
static UAVObjStats stats;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the object manager
|
|
|
|
* \return 0 Success
|
|
|
|
* \return -1 Failure
|
|
|
|
*/
|
|
|
|
int32_t UAVObjInitialize()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// Initialize variables
|
|
|
|
memset(&stats, 0, sizeof(UAVObjStats));
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Initialize _uavo_handles start/stop pointers */
|
|
|
|
#if (defined(__MACH__) && defined(__APPLE__))
|
|
|
|
uint64_t aslr_offset = (uint64_t)&_aslr_offset - getsectbyname("__DATA", "_aslr")->addr;
|
|
|
|
__start__uavo_handles = (struct UAVOData * *)(getsectbyname("__DATA", "_uavo_handles")->addr + aslr_offset);
|
|
|
|
__stop__uavo_handles = (struct UAVOData * *)((uint64_t)__start__uavo_handles + getsectbyname("__DATA", "_uavo_handles")->size);
|
|
|
|
#endif
|
2013-04-24 22:39:39 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Initialize the uavo handle table
|
|
|
|
memset(__start__uavo_handles, 0,
|
|
|
|
(uintptr_t)__stop__uavo_handles - (uintptr_t)__start__uavo_handles);
|
2013-04-14 11:43:37 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Create mutex
|
|
|
|
mutex = xSemaphoreCreateRecursiveMutex();
|
|
|
|
if (mutex == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Done
|
|
|
|
return 0;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 06:28:05 +02:00
|
|
|
/*****************
|
|
|
|
* Statistics
|
|
|
|
****************/
|
|
|
|
|
2011-05-14 22:15:33 +02:00
|
|
|
/**
|
|
|
|
* Get the statistics counters
|
|
|
|
* @param[out] statsOut The statistics counters will be copied there
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
void UAVObjGetStats(UAVObjStats *statsOut)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
|
|
|
memcpy(statsOut, &stats, sizeof(UAVObjStats));
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the statistics counters
|
|
|
|
*/
|
|
|
|
void UAVObjClearStats()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
|
|
|
memset(&stats, 0, sizeof(UAVObjStats));
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 06:28:05 +02:00
|
|
|
/************************
|
|
|
|
* Object Initialization
|
|
|
|
***********************/
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static void UAVObjInitMetaData(struct UAVOMeta *obj_meta)
|
2012-05-26 06:28:05 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Fill in the common part of the UAVO */
|
|
|
|
struct UAVOBase *uavo_base = &(obj_meta->base);
|
|
|
|
|
|
|
|
memset(uavo_base, 0, sizeof(*uavo_base));
|
|
|
|
uavo_base->flags.isMeta = true;
|
|
|
|
uavo_base->flags.isSingle = true;
|
|
|
|
uavo_base->next_event = NULL;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Clear the instance data carried in the UAVO */
|
|
|
|
memset(&(obj_meta->instance0), 0, sizeof(obj_meta->instance0));
|
2012-05-26 06:28:05 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static struct UAVOData *UAVObjAllocSingle(uint32_t num_bytes)
|
2012-05-26 06:28:05 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Compute the complete size of the object, including the data for a single embedded instance */
|
|
|
|
uint32_t object_size = sizeof(struct UAVOSingle) + num_bytes;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Allocate the object from the heap */
|
|
|
|
struct UAVOSingle *uavo_single = (struct UAVOSingle *)pvPortMalloc(object_size);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!uavo_single) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Fill in the common part of the UAVO */
|
|
|
|
struct UAVOBase *uavo_base = &(uavo_single->uavo.base);
|
|
|
|
memset(uavo_base, 0, sizeof(*uavo_base));
|
|
|
|
uavo_base->flags.isSingle = true;
|
|
|
|
uavo_base->next_event = NULL;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Clear the instance data carried in the UAVO */
|
|
|
|
memset(&(uavo_single->instance0), 0, num_bytes);
|
|
|
|
|
|
|
|
/* Give back the generic UAVO part */
|
|
|
|
return &(uavo_single->uavo);
|
2012-05-26 06:28:05 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static struct UAVOData *UAVObjAllocMulti(uint32_t num_bytes)
|
2012-05-26 06:28:05 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Compute the complete size of the object, including the data for a single embedded instance */
|
|
|
|
uint32_t object_size = sizeof(struct UAVOMulti) + num_bytes;
|
|
|
|
|
|
|
|
/* Allocate the object from the heap */
|
|
|
|
struct UAVOMulti *uavo_multi = (struct UAVOMulti *)pvPortMalloc(object_size);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!uavo_multi) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Fill in the common part of the UAVO */
|
|
|
|
struct UAVOBase *uavo_base = &(uavo_multi->uavo.base);
|
|
|
|
memset(uavo_base, 0, sizeof(*uavo_base));
|
|
|
|
uavo_base->flags.isSingle = false;
|
|
|
|
uavo_base->next_event = NULL;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Set up the type-specific part of the UAVO */
|
|
|
|
uavo_multi->num_instances = 1;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Clear the multi instance data carried in the UAVO */
|
|
|
|
memset(&(uavo_multi->instance0), 0, sizeof(struct UAVOMultiInst) + num_bytes);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Give back the generic UAVO part */
|
|
|
|
return &(uavo_multi->uavo);
|
2012-05-26 06:28:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**************************
|
|
|
|
* UAVObject Database APIs
|
|
|
|
*************************/
|
|
|
|
|
2011-05-14 22:15:33 +02:00
|
|
|
/**
|
|
|
|
* Register and new object in the object manager.
|
|
|
|
* \param[in] id Unique object ID
|
|
|
|
* \param[in] isSingleInstance Is this a single instance or multi-instance object
|
|
|
|
* \param[in] isSettings Is this a settings object
|
|
|
|
* \param[in] numBytes Number of bytes of object data (for one instance)
|
|
|
|
* \param[in] initCb Default field and metadata initialization function
|
|
|
|
* \return Object handle, or NULL if failure.
|
|
|
|
* \return
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjHandle UAVObjRegister(uint32_t id,
|
|
|
|
int32_t isSingleInstance, int32_t isSettings,
|
|
|
|
uint32_t num_bytes,
|
|
|
|
UAVObjInitializeCallback initCb)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct UAVOData *uavo_data = NULL;
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Don't allow duplicate registrations */
|
|
|
|
if (UAVObjGetByID(id)) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Map the various flags to one of the UAVO types we understand */
|
|
|
|
if (isSingleInstance) {
|
|
|
|
uavo_data = UAVObjAllocSingle(num_bytes);
|
|
|
|
} else {
|
|
|
|
uavo_data = UAVObjAllocMulti(num_bytes);
|
|
|
|
}
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!uavo_data) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Fill in the details about this UAVO */
|
|
|
|
uavo_data->id = id;
|
|
|
|
uavo_data->instance_size = num_bytes;
|
|
|
|
if (isSettings) {
|
|
|
|
uavo_data->base.flags.isSettings = true;
|
|
|
|
}
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Initialize the embedded meta UAVO */
|
|
|
|
UAVObjInitMetaData(&uavo_data->metaObj);
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Initialize object fields and metadata to default values */
|
|
|
|
if (initCb) {
|
|
|
|
initCb((UAVObjHandle)uavo_data, 0);
|
|
|
|
}
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Always try to load the meta object from flash */
|
|
|
|
UAVObjLoad((UAVObjHandle) & (uavo_data->metaObj), 0);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Attempt to load settings object from flash */
|
|
|
|
if (uavo_data->base.flags.isSettings) {
|
|
|
|
UAVObjLoad((UAVObjHandle)uavo_data, 0);
|
|
|
|
}
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// fire events for outer object and its embedded meta object
|
|
|
|
UAVObjInstanceUpdated((UAVObjHandle)uavo_data, 0);
|
|
|
|
UAVObjInstanceUpdated((UAVObjHandle) & (uavo_data->metaObj), 0);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
|
|
|
unlock_exit:
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return (UAVObjHandle)uavo_data;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve an object from the list given its id
|
|
|
|
* \param[in] The object ID
|
|
|
|
* \return The object or NULL if not found.
|
|
|
|
*/
|
|
|
|
UAVObjHandle UAVObjGetByID(uint32_t id)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjHandle *found_obj = (UAVObjHandle *)NULL;
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Get lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Look for object
|
|
|
|
UAVO_LIST_ITERATE(tmp_obj)
|
|
|
|
if (tmp_obj->id == id) {
|
|
|
|
found_obj = (UAVObjHandle *)tmp_obj;
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
if (MetaObjectId(tmp_obj->id) == id) {
|
|
|
|
found_obj = (UAVObjHandle *)&(tmp_obj->metaObj);
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
}
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2012-05-26 06:28:05 +02:00
|
|
|
unlock_exit:
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return found_obj;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the object's ID
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \return The object ID
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
uint32_t UAVObjGetID(UAVObjHandle obj_handle)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Recover the common object header */
|
|
|
|
struct UAVOBase *uavo_base = (struct UAVOBase *)obj_handle;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (UAVObjIsMetaobject(obj_handle)) {
|
|
|
|
/* We have a meta object, find our containing UAVO */
|
|
|
|
struct UAVOData *uavo_data = container_of((struct UAVOMeta *)uavo_base, struct UAVOData, metaObj);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return MetaObjectId(uavo_data->id);
|
|
|
|
} else {
|
|
|
|
/* We have a data object, augment our pointer */
|
|
|
|
struct UAVOData *uavo_data = (struct UAVOData *)uavo_base;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return uavo_data->id;
|
|
|
|
}
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of bytes of the object's data (for one instance)
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \return The number of bytes
|
|
|
|
*/
|
|
|
|
uint32_t UAVObjGetNumBytes(UAVObjHandle obj)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
uint32_t instance_size;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Recover the common object header */
|
|
|
|
struct UAVOBase *uavo_base = (struct UAVOBase *)obj;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (uavo_base->flags.isMeta) {
|
|
|
|
instance_size = MetaNumBytes;
|
|
|
|
} else {
|
|
|
|
/* We have a data object, augment our pointer */
|
|
|
|
struct UAVOData *uavo = (struct UAVOData *)uavo_base;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
instance_size = uavo->instance_size;
|
|
|
|
}
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return instance_size;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the object this object is linked to. For regular objects, the linked object
|
|
|
|
* is the metaobject. For metaobjects the linked object is the parent object.
|
|
|
|
* This function is normally only needed by the telemetry module.
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \return The object linked object handle
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
UAVObjHandle UAVObjGetLinkedObj(UAVObjHandle obj_handle)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Recover the common object header */
|
|
|
|
struct UAVOBase *uavo_base = (struct UAVOBase *)obj_handle;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (UAVObjIsMetaobject(obj_handle)) {
|
|
|
|
/* We have a meta object, find our containing UAVO. */
|
|
|
|
struct UAVOData *uavo_data = container_of((struct UAVOMeta *)uavo_base, struct UAVOData, metaObj);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return (UAVObjHandle)uavo_data;
|
|
|
|
} else {
|
|
|
|
/* We have a data object, augment our pointer */
|
|
|
|
struct UAVOData *uavo_data = (struct UAVOData *)uavo_base;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return (UAVObjHandle) & (uavo_data->metaObj);
|
|
|
|
}
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of instances contained in the object.
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \return The number of instances
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
uint16_t UAVObjGetNumInstances(UAVObjHandle obj_handle)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (UAVObjIsSingleInstance(obj_handle)) {
|
|
|
|
/* Only one instance is allowed */
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
/* Multi-instance object. Inspect the object */
|
|
|
|
/* Augment our pointer to reflect the proper type */
|
|
|
|
struct UAVOMulti *uavo_multi = (struct UAVOMulti *)obj_handle;
|
|
|
|
return uavo_multi->num_instances;
|
|
|
|
}
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance in the object.
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \return The instance ID or 0 if an error
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
uint16_t UAVObjCreateInstance(UAVObjHandle obj_handle,
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjInitializeCallback initCb)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (UAVObjIsMetaobject(obj_handle)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
InstanceHandle instEntry;
|
|
|
|
uint16_t instId = 0;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Create new instance
|
|
|
|
instId = UAVObjGetNumInstances(obj_handle);
|
|
|
|
instEntry = createInstance((struct UAVOData *)obj_handle, instId);
|
|
|
|
if (instEntry == NULL) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Initialize instance data
|
|
|
|
if (initCb) {
|
|
|
|
initCb(obj_handle, instId);
|
|
|
|
}
|
2012-05-26 06:28:05 +02:00
|
|
|
|
|
|
|
unlock_exit:
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreGiveRecursive(mutex);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return instId;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does this object contains a single instance or multiple instances?
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \return True (1) if this is a single instance object
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
bool UAVObjIsSingleInstance(UAVObjHandle obj_handle)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Recover the common object header */
|
|
|
|
struct UAVOBase *uavo_base = (struct UAVOBase *)obj_handle;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return uavo_base->flags.isSingle;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this a metaobject?
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \return True (1) if this is metaobject
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
bool UAVObjIsMetaobject(UAVObjHandle obj_handle)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Recover the common object header */
|
|
|
|
struct UAVOBase *uavo_base = (struct UAVOBase *)obj_handle;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return uavo_base->flags.isMeta;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this a settings object?
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \return True (1) if this is a settings object
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
bool UAVObjIsSettings(UAVObjHandle obj_handle)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Recover the common object header */
|
|
|
|
struct UAVOBase *uavo_base = (struct UAVOBase *)obj_handle;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return uavo_base->flags.isSettings;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unpack an object from a byte array
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[in] instId The instance ID
|
|
|
|
* \param[in] dataIn The byte array
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
int32_t UAVObjUnpack(UAVObjHandle obj_handle, uint16_t instId,
|
2013-05-19 16:37:30 +02:00
|
|
|
const uint8_t *dataIn)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
|
|
|
|
|
|
|
// Lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
|
|
|
|
|
|
|
int32_t rc = -1;
|
|
|
|
|
|
|
|
if (UAVObjIsMetaobject(obj_handle)) {
|
|
|
|
if (instId != 0) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
memcpy(MetaDataPtr((struct UAVOMeta *)obj_handle), dataIn, MetaNumBytes);
|
|
|
|
} else {
|
|
|
|
struct UAVOData *obj;
|
|
|
|
InstanceHandle instEntry;
|
|
|
|
|
|
|
|
// Cast handle to object
|
|
|
|
obj = (struct UAVOData *)obj_handle;
|
|
|
|
|
|
|
|
// Get the instance
|
|
|
|
instEntry = getInstance(obj, instId);
|
|
|
|
|
|
|
|
// If the instance does not exist create it and any other instances before it
|
|
|
|
if (instEntry == NULL) {
|
|
|
|
instEntry = createInstance(obj, instId);
|
|
|
|
if (instEntry == NULL) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set the data
|
|
|
|
memcpy(InstanceData(instEntry), dataIn, obj->instance_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire event
|
|
|
|
sendEvent((struct UAVOBase *)obj_handle, instId, EV_UNPACKED);
|
|
|
|
rc = 0;
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2012-05-26 06:28:05 +02:00
|
|
|
unlock_exit:
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return rc;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pack an object to a byte array
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[in] instId The instance ID
|
|
|
|
* \param[out] dataOut The byte array
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t UAVObjPack(UAVObjHandle obj_handle, uint16_t instId, uint8_t *dataOut)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t rc = -1;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (UAVObjIsMetaobject(obj_handle)) {
|
|
|
|
if (instId != 0) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
memcpy(dataOut, MetaDataPtr((struct UAVOMeta *)obj_handle), MetaNumBytes);
|
|
|
|
} else {
|
|
|
|
struct UAVOData *obj;
|
|
|
|
InstanceHandle instEntry;
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Cast handle to object
|
|
|
|
obj = (struct UAVOData *)obj_handle;
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Get the instance
|
|
|
|
instEntry = getInstance(obj, instId);
|
|
|
|
if (instEntry == NULL) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
// Pack data
|
|
|
|
memcpy(dataOut, InstanceData(instEntry), obj->instance_size);
|
|
|
|
}
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
rc = 0;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
|
|
|
unlock_exit:
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return rc;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
2013-03-30 15:20:05 +01:00
|
|
|
#if defined(PIOS_USE_SETTINGS_ON_SDCARD)
|
2011-05-14 22:15:33 +02:00
|
|
|
/**
|
|
|
|
* Save the data of the specified object instance to the file system (SD card).
|
|
|
|
* The object will be appended and the file will not be closed.
|
|
|
|
* The object data can be restored using the UAVObjLoad function.
|
|
|
|
* @param[in] obj The object handle.
|
|
|
|
* @param[in] instId The instance ID
|
|
|
|
* @param[in] file File to append to
|
|
|
|
* @return 0 if success or -1 if failure
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
int32_t UAVObjSaveToFile(UAVObjHandle obj_handle, uint16_t instId,
|
2013-05-19 16:37:30 +02:00
|
|
|
FILEINFO *file)
|
|
|
|
{
|
|
|
|
PIOS_Assert(obj_handle);
|
|
|
|
|
|
|
|
uint32_t bytesWritten;
|
|
|
|
// Check for file system availability
|
|
|
|
if (PIOS_SDCARD_IsMounted() == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
|
|
|
|
|
|
|
if (UAVObjIsMetaobject(obj_handle)) {
|
|
|
|
// Get the instance information
|
|
|
|
if (instId != 0) {
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Write the object ID
|
|
|
|
uint32_t objId = UAVObjGetID(obj_handle);
|
|
|
|
PIOS_FWRITE(file, &objId, sizeof(objId),
|
|
|
|
&bytesWritten);
|
|
|
|
|
|
|
|
// Write the data and check that the write was successful
|
|
|
|
PIOS_FWRITE(file, MetaDataPtr((struct UAVOMeta *)obj_handle), MetaNumBytes,
|
|
|
|
&bytesWritten);
|
|
|
|
if (bytesWritten != MetaNumBytes) {
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct UAVOData *uavo;
|
|
|
|
InstanceHandle instEntry;
|
|
|
|
|
|
|
|
// Cast to object
|
|
|
|
uavo = (struct UAVOData *)obj_handle;
|
|
|
|
|
|
|
|
// Get the instance information
|
|
|
|
instEntry = getInstance(uavo, instId);
|
|
|
|
if (instEntry == NULL) {
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Write the object ID
|
|
|
|
PIOS_FWRITE(file, &uavo->id, sizeof(uavo->id),
|
|
|
|
&bytesWritten);
|
|
|
|
|
|
|
|
// Write the instance ID
|
|
|
|
if (!UAVObjIsSingleInstance(obj_handle)) {
|
|
|
|
PIOS_FWRITE(file, &instId,
|
|
|
|
sizeof(instId), &bytesWritten);
|
|
|
|
}
|
|
|
|
// Write the data and check that the write was successful
|
|
|
|
PIOS_FWRITE(file, InstanceData(instEntry), uavo->instance_size,
|
|
|
|
&bytesWritten);
|
|
|
|
if (bytesWritten != uavo->instance_size) {
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Done
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return 0;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
2013-03-30 15:20:05 +01:00
|
|
|
#endif /* PIOS_USE_SETTINGS_ON_SDCARD */
|
2011-05-14 22:15:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the data of the specified object to the file system (SD card).
|
|
|
|
* If the object contains multiple instances, all of them will be saved.
|
|
|
|
* A new file with the name of the object will be created.
|
|
|
|
* The object data can be restored using the UAVObjLoad function.
|
|
|
|
* @param[in] obj The object handle.
|
|
|
|
* @param[in] instId The instance ID
|
|
|
|
* @param[in] file File to append to
|
|
|
|
* @return 0 if success or -1 if failure
|
|
|
|
*/
|
2013-05-05 09:02:24 +02:00
|
|
|
int32_t UAVObjSave(UAVObjHandle obj_handle, __attribute__((unused)) uint16_t instId)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-15 23:16:34 +02:00
|
|
|
#if defined(PIOS_INCLUDE_FLASH_LOGFS_SETTINGS)
|
2013-05-19 16:37:30 +02:00
|
|
|
if (UAVObjIsMetaobject(obj_handle)) {
|
|
|
|
if (instId != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PIOS_FLASHFS_ObjSave(pios_uavo_settings_fs_id, UAVObjGetID(obj_handle), instId, (uint8_t *)MetaDataPtr((struct UAVOMeta *)obj_handle), UAVObjGetNumBytes(obj_handle)) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
InstanceHandle instEntry = getInstance((struct UAVOData *)obj_handle, instId);
|
|
|
|
|
|
|
|
if (instEntry == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (InstanceData(instEntry) == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PIOS_FLASHFS_ObjSave(pios_uavo_settings_fs_id, UAVObjGetID(obj_handle), instId, InstanceData(instEntry), UAVObjGetNumBytes(obj_handle)) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* if defined(PIOS_INCLUDE_FLASH_LOGFS_SETTINGS) */
|
2013-03-30 15:20:05 +01:00
|
|
|
#if defined(PIOS_USE_SETTINGS_ON_SDCARD)
|
2013-05-19 16:37:30 +02:00
|
|
|
FILEINFO file;
|
|
|
|
uint8_t filename[14];
|
|
|
|
|
|
|
|
// Check for file system availability
|
|
|
|
if (PIOS_SDCARD_IsMounted() == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
|
|
|
|
|
|
|
// Get filename
|
|
|
|
objectFilename(obj_handle, filename);
|
|
|
|
|
|
|
|
// Open file
|
|
|
|
if (PIOS_FOPEN_WRITE(filename, file)) {
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Append object
|
|
|
|
if (UAVObjSaveToFile(obj_handle, instId, &file) == -1) {
|
|
|
|
PIOS_FCLOSE(file);
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Done, close file and unlock
|
|
|
|
PIOS_FCLOSE(file);
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
2013-03-30 15:20:05 +01:00
|
|
|
#endif /* PIOS_USE_SETTINGS_ON_SDCARD */
|
2013-05-19 16:37:30 +02:00
|
|
|
return 0;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
2013-03-30 15:20:05 +01:00
|
|
|
#if defined(PIOS_USE_SETTINGS_ON_SDCARD)
|
2011-05-14 22:15:33 +02:00
|
|
|
/**
|
|
|
|
* Load an object from the file system (SD card).
|
|
|
|
* @param[in] file File to read from
|
|
|
|
* @return The handle of the object loaded or NULL if a failure
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjHandle UAVObjLoadFromFile(FILEINFO *file)
|
|
|
|
{
|
|
|
|
uint32_t bytesRead;
|
|
|
|
struct UAVOBase *objEntry;
|
|
|
|
InstanceHandle instEntry;
|
|
|
|
uint32_t objId;
|
|
|
|
uint16_t instId;
|
|
|
|
UAVObjHandle obj_handle;
|
|
|
|
|
|
|
|
// Check for file system availability
|
|
|
|
if (PIOS_SDCARD_IsMounted() == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// Lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
|
|
|
|
|
|
|
// Read the object ID
|
|
|
|
if (PIOS_FREAD(file, &objId, sizeof(objId), &bytesRead)) {
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// Get the object
|
|
|
|
obj_handle = UAVObjGetByID(objId);
|
|
|
|
if (obj_handle == 0) {
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
objEntry = (struct UAVOBase *)obj_handle;
|
|
|
|
|
|
|
|
// Get the instance ID
|
|
|
|
instId = 0;
|
|
|
|
if (!UAVObjIsSingleInstance(obj_handle)) {
|
|
|
|
if (PIOS_FREAD
|
|
|
|
(file, &instId, sizeof(instId), &bytesRead)) {
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UAVObjIsMetaobject(obj_handle)) {
|
|
|
|
// If the instance does not exist create it and any other instances before it
|
|
|
|
if (instId != 0) {
|
|
|
|
// Error, unlock and return
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// Read the instance data
|
|
|
|
if (PIOS_FREAD
|
|
|
|
(file, MetaDataPtr((struct UAVOMeta *)obj_handle), MetaNumBytes, &bytesRead)) {
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Get the instance information
|
|
|
|
instEntry = getInstance((struct UAVOData *)objEntry, instId);
|
|
|
|
|
|
|
|
// If the instance does not exist create it and any other instances before it
|
|
|
|
if (instEntry == NULL) {
|
|
|
|
instEntry = createInstance((struct UAVOData *)objEntry, instId);
|
|
|
|
if (instEntry == NULL) {
|
|
|
|
// Error, unlock and return
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Read the instance data
|
|
|
|
if (PIOS_FREAD
|
|
|
|
(file, InstanceData(instEntry), ((struct UAVOData *)objEntry)->instance_size, &bytesRead)) {
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire event
|
|
|
|
sendEvent(objEntry, instId, EV_UNPACKED);
|
|
|
|
|
|
|
|
// Unlock
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return obj_handle;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
2013-03-30 15:20:05 +01:00
|
|
|
#endif /* PIOS_USE_SETTINGS_ON_SDCARD */
|
2011-05-14 22:15:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load an object from the file system (SD card).
|
|
|
|
* A file with the name of the object will be opened.
|
|
|
|
* The object data can be saved using the UAVObjSave function.
|
|
|
|
* @param[in] obj The object handle.
|
|
|
|
* @param[in] instId The object instance
|
|
|
|
* @return 0 if success or -1 if failure
|
|
|
|
*/
|
2013-05-05 09:02:24 +02:00
|
|
|
int32_t UAVObjLoad(UAVObjHandle obj_handle, __attribute__((unused)) uint16_t instId)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-15 23:16:34 +02:00
|
|
|
#if defined(PIOS_INCLUDE_FLASH_LOGFS_SETTINGS)
|
2013-05-19 16:37:30 +02:00
|
|
|
if (UAVObjIsMetaobject(obj_handle)) {
|
|
|
|
if (instId != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire event on success
|
|
|
|
if (PIOS_FLASHFS_ObjLoad(pios_uavo_settings_fs_id, UAVObjGetID(obj_handle), instId, (uint8_t *)MetaDataPtr((struct UAVOMeta *)obj_handle), UAVObjGetNumBytes(obj_handle)) == 0) {
|
|
|
|
sendEvent((struct UAVOBase *)obj_handle, instId, EV_UNPACKED);
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
InstanceHandle instEntry = getInstance((struct UAVOData *)obj_handle, instId);
|
|
|
|
|
|
|
|
if (instEntry == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire event on success
|
|
|
|
if (PIOS_FLASHFS_ObjLoad(pios_uavo_settings_fs_id, UAVObjGetID(obj_handle), instId, InstanceData(instEntry), UAVObjGetNumBytes(obj_handle)) == 0) {
|
|
|
|
sendEvent((struct UAVOBase *)obj_handle, instId, EV_UNPACKED);
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* if defined(PIOS_INCLUDE_FLASH_LOGFS_SETTINGS) */
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-03-30 15:20:05 +01:00
|
|
|
#if defined(PIOS_USE_SETTINGS_ON_SDCARD)
|
2013-05-19 16:37:30 +02:00
|
|
|
FILEINFO file;
|
|
|
|
UAVObjHandle loadedObj;
|
|
|
|
uint8_t filename[14];
|
|
|
|
|
|
|
|
// Check for file system availability
|
|
|
|
if (PIOS_SDCARD_IsMounted() == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
|
|
|
|
|
|
|
// Get filename
|
|
|
|
objectFilename(obj_handle, filename);
|
|
|
|
|
|
|
|
// Open file
|
|
|
|
if (PIOS_FOPEN_READ(filename, file)) {
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Load object
|
|
|
|
loadedObj = UAVObjLoadFromFile(&file);
|
|
|
|
if (loadedObj == 0) {
|
|
|
|
PIOS_FCLOSE(file);
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Check that the IDs match
|
|
|
|
if (UAVObjGetID(loadedObj) != UAVObjGetID(obj_handle)) {
|
|
|
|
PIOS_FCLOSE(file);
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Done, close file and unlock
|
|
|
|
PIOS_FCLOSE(file);
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
2013-03-30 15:20:05 +01:00
|
|
|
#endif /* PIOS_USE_SETTINGS_ON_SDCARD */
|
2013-05-19 16:37:30 +02:00
|
|
|
return 0;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an object from the file system (SD card).
|
|
|
|
* @param[in] obj The object handle.
|
|
|
|
* @param[in] instId The object instance
|
|
|
|
* @return 0 if success or -1 if failure
|
|
|
|
*/
|
2013-05-05 09:02:24 +02:00
|
|
|
int32_t UAVObjDelete(UAVObjHandle obj_handle, __attribute__((unused)) uint16_t instId)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
2013-05-15 23:16:34 +02:00
|
|
|
#if defined(PIOS_INCLUDE_FLASH_LOGFS_SETTINGS)
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_FLASHFS_ObjDelete(pios_uavo_settings_fs_id, UAVObjGetID(obj_handle), instId);
|
2011-05-14 22:15:33 +02:00
|
|
|
#endif
|
2013-03-30 15:20:05 +01:00
|
|
|
#if defined(PIOS_USE_SETTINGS_ON_SDCARD)
|
2013-05-19 16:37:30 +02:00
|
|
|
uint8_t filename[14];
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Check for file system availability
|
|
|
|
if (PIOS_SDCARD_IsMounted() == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Get filename
|
|
|
|
objectFilename(obj_handle, filename);
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Delete file
|
|
|
|
PIOS_FUNLINK(filename);
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Done
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
2013-03-30 15:20:05 +01:00
|
|
|
#endif /* PIOS_USE_SETTINGS_ON_SDCARD */
|
2013-05-19 16:37:30 +02:00
|
|
|
return 0;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save all settings objects to the SD card.
|
|
|
|
* @return 0 if success or -1 if failure
|
|
|
|
*/
|
|
|
|
int32_t UAVObjSaveSettings()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// Get lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t rc = -1;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Save all settings objects
|
|
|
|
UAVO_LIST_ITERATE(obj)
|
|
|
|
// Check if this is a settings object
|
|
|
|
if (UAVObjIsSettings(obj)) {
|
|
|
|
// Save object
|
|
|
|
if (UAVObjSave((UAVObjHandle)obj, 0) ==
|
|
|
|
-1) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
rc = 0;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
|
|
|
unlock_exit:
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return rc;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load all settings objects from the SD card.
|
|
|
|
* @return 0 if success or -1 if failure
|
|
|
|
*/
|
|
|
|
int32_t UAVObjLoadSettings()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// Get lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t rc = -1;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Load all settings objects
|
|
|
|
UAVO_LIST_ITERATE(obj)
|
|
|
|
// Check if this is a settings object
|
|
|
|
if (UAVObjIsSettings(obj)) {
|
|
|
|
// Load object
|
|
|
|
if (UAVObjLoad((UAVObjHandle)obj, 0) ==
|
|
|
|
-1) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
rc = 0;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
|
|
|
unlock_exit:
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return rc;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all settings objects from the SD card.
|
|
|
|
* @return 0 if success or -1 if failure
|
|
|
|
*/
|
|
|
|
int32_t UAVObjDeleteSettings()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// Get lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t rc = -1;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Save all settings objects
|
|
|
|
UAVO_LIST_ITERATE(obj)
|
|
|
|
// Check if this is a settings object
|
|
|
|
if (UAVObjIsSettings(obj)) {
|
|
|
|
// Save object
|
|
|
|
if (UAVObjDelete((UAVObjHandle)obj, 0)
|
|
|
|
== -1) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
rc = 0;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
|
|
|
unlock_exit:
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return rc;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save all metaobjects to the SD card.
|
|
|
|
* @return 0 if success or -1 if failure
|
|
|
|
*/
|
|
|
|
int32_t UAVObjSaveMetaobjects()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// Get lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t rc = -1;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Save all settings objects
|
|
|
|
UAVO_LIST_ITERATE(obj)
|
|
|
|
// Save object
|
|
|
|
if (UAVObjSave((UAVObjHandle)MetaObjectPtr(obj), 0) ==
|
|
|
|
-1) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
}
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
rc = 0;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
|
|
|
unlock_exit:
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return rc;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load all metaobjects from the SD card.
|
|
|
|
* @return 0 if success or -1 if failure
|
|
|
|
*/
|
|
|
|
int32_t UAVObjLoadMetaobjects()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// Get lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t rc = -1;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Load all settings objects
|
|
|
|
UAVO_LIST_ITERATE(obj)
|
|
|
|
// Load object
|
|
|
|
if (UAVObjLoad((UAVObjHandle)MetaObjectPtr(obj), 0) ==
|
|
|
|
-1) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
}
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
rc = 0;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
|
|
|
unlock_exit:
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return rc;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all metaobjects from the SD card.
|
|
|
|
* @return 0 if success or -1 if failure
|
|
|
|
*/
|
|
|
|
int32_t UAVObjDeleteMetaobjects()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// Get lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t rc = -1;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Load all settings objects
|
|
|
|
UAVO_LIST_ITERATE(obj)
|
|
|
|
// Load object
|
|
|
|
if (UAVObjDelete((UAVObjHandle)MetaObjectPtr(obj), 0)
|
|
|
|
== -1) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
}
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
rc = 0;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
|
|
|
unlock_exit:
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return rc;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the object data
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[in] dataIn The object's data structure
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
int32_t UAVObjSetData(UAVObjHandle obj_handle, const void *dataIn)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return UAVObjSetInstanceData(obj_handle, 0, dataIn);
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
2011-06-03 06:18:34 +02:00
|
|
|
/**
|
|
|
|
* Set the object data
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[in] dataIn The object's data structure
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t UAVObjSetDataField(UAVObjHandle obj_handle, const void *dataIn, uint32_t offset, uint32_t size)
|
2011-06-03 06:18:34 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return UAVObjSetInstanceDataField(obj_handle, 0, dataIn, offset, size);
|
2011-06-03 06:18:34 +02:00
|
|
|
}
|
|
|
|
|
2011-05-14 22:15:33 +02:00
|
|
|
/**
|
|
|
|
* Get the object data
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[out] dataOut The object's data structure
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
int32_t UAVObjGetData(UAVObjHandle obj_handle, void *dataOut)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return UAVObjGetInstanceData(obj_handle, 0, dataOut);
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
2011-06-03 06:18:34 +02:00
|
|
|
/**
|
|
|
|
* Get the object data
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[out] dataOut The object's data structure
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t UAVObjGetDataField(UAVObjHandle obj_handle, void *dataOut, uint32_t offset, uint32_t size)
|
2011-06-03 06:18:34 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return UAVObjGetInstanceDataField(obj_handle, 0, dataOut, offset, size);
|
2011-06-03 06:18:34 +02:00
|
|
|
}
|
|
|
|
|
2011-05-14 22:15:33 +02:00
|
|
|
/**
|
|
|
|
* Set the data of a specific object instance
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[in] instId The object instance ID
|
|
|
|
* \param[in] dataIn The object's data structure
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
int32_t UAVObjSetInstanceData(UAVObjHandle obj_handle, uint16_t instId,
|
2013-05-19 16:37:30 +02:00
|
|
|
const void *dataIn)
|
|
|
|
{
|
|
|
|
PIOS_Assert(obj_handle);
|
|
|
|
|
|
|
|
// Lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
|
|
|
|
|
|
|
int32_t rc = -1;
|
|
|
|
|
|
|
|
if (UAVObjIsMetaobject(obj_handle)) {
|
|
|
|
if (instId != 0) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
memcpy(MetaDataPtr((struct UAVOMeta *)obj_handle), dataIn, MetaNumBytes);
|
|
|
|
} else {
|
|
|
|
struct UAVOData *obj;
|
|
|
|
InstanceHandle instEntry;
|
|
|
|
|
|
|
|
// Cast to object info
|
|
|
|
obj = (struct UAVOData *)obj_handle;
|
|
|
|
|
|
|
|
// Check access level
|
|
|
|
if (UAVObjReadOnly(obj_handle)) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
// Get instance information
|
|
|
|
instEntry = getInstance(obj, instId);
|
|
|
|
if (instEntry == NULL) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
// Set data
|
|
|
|
memcpy(InstanceData(instEntry), dataIn, obj->instance_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire event
|
|
|
|
sendEvent((struct UAVOBase *)obj_handle, instId, EV_UPDATED);
|
|
|
|
rc = 0;
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2012-05-26 06:28:05 +02:00
|
|
|
unlock_exit:
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return rc;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
2011-06-03 06:18:34 +02:00
|
|
|
/**
|
|
|
|
* Set the data of a specific object instance
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[in] instId The object instance ID
|
|
|
|
* \param[in] dataIn The object's data structure
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t UAVObjSetInstanceDataField(UAVObjHandle obj_handle, uint16_t instId, const void *dataIn, uint32_t offset, uint32_t size)
|
2011-06-03 06:18:34 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
2011-06-03 06:18:34 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t rc = -1;
|
2011-06-03 06:18:34 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (UAVObjIsMetaobject(obj_handle)) {
|
|
|
|
// Get instance information
|
|
|
|
if (instId != 0) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
2011-06-03 06:18:34 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Check for overrun
|
|
|
|
if ((size + offset) > MetaNumBytes) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
2012-05-17 19:47:18 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Set data
|
|
|
|
memcpy(MetaDataPtr((struct UAVOMeta *)obj_handle) + offset, dataIn, size);
|
|
|
|
} else {
|
|
|
|
struct UAVOData *obj;
|
|
|
|
InstanceHandle instEntry;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Cast to object info
|
|
|
|
obj = (struct UAVOData *)obj_handle;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Check access level
|
|
|
|
if (UAVObjReadOnly(obj_handle)) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Get instance information
|
|
|
|
instEntry = getInstance(obj, instId);
|
|
|
|
if (instEntry == NULL) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
2012-05-17 19:47:18 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Check for overrun
|
|
|
|
if ((size + offset) > obj->instance_size) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
2012-05-17 19:47:18 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Set data
|
|
|
|
memcpy(InstanceData(instEntry) + offset, dataIn, size);
|
|
|
|
}
|
2011-06-03 06:18:34 +02:00
|
|
|
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Fire event
|
|
|
|
sendEvent((struct UAVOBase *)obj_handle, instId, EV_UPDATED);
|
|
|
|
rc = 0;
|
2011-06-03 06:18:34 +02:00
|
|
|
|
2012-05-26 06:28:05 +02:00
|
|
|
unlock_exit:
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return rc;
|
2011-06-03 06:18:34 +02:00
|
|
|
}
|
|
|
|
|
2011-05-14 22:15:33 +02:00
|
|
|
/**
|
|
|
|
* Get the data of a specific object instance
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[in] instId The object instance ID
|
|
|
|
* \param[out] dataOut The object's data structure
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
int32_t UAVObjGetInstanceData(UAVObjHandle obj_handle, uint16_t instId,
|
2013-05-19 16:37:30 +02:00
|
|
|
void *dataOut)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
|
|
|
|
|
|
|
// Lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
|
|
|
|
|
|
|
int32_t rc = -1;
|
|
|
|
|
|
|
|
if (UAVObjIsMetaobject(obj_handle)) {
|
|
|
|
// Get instance information
|
|
|
|
if (instId != 0) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
// Set data
|
|
|
|
memcpy(dataOut, MetaDataPtr((struct UAVOMeta *)obj_handle), MetaNumBytes);
|
|
|
|
} else {
|
|
|
|
struct UAVOData *obj;
|
|
|
|
InstanceHandle instEntry;
|
|
|
|
|
|
|
|
// Cast to object info
|
|
|
|
obj = (struct UAVOData *)obj_handle;
|
|
|
|
|
|
|
|
// Get instance information
|
|
|
|
instEntry = getInstance(obj, instId);
|
|
|
|
if (instEntry == NULL) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
// Set data
|
|
|
|
memcpy(dataOut, InstanceData(instEntry), obj->instance_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
|
|
|
unlock_exit:
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return rc;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
2011-06-03 06:18:34 +02:00
|
|
|
/**
|
|
|
|
* Get the data of a specific object instance
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[in] instId The object instance ID
|
|
|
|
* \param[out] dataOut The object's data structure
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t UAVObjGetInstanceDataField(UAVObjHandle obj_handle, uint16_t instId, void *dataOut, uint32_t offset, uint32_t size)
|
2011-06-03 06:18:34 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
2011-06-03 06:18:34 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t rc = -1;
|
2012-05-17 19:47:18 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (UAVObjIsMetaobject(obj_handle)) {
|
|
|
|
// Get instance information
|
|
|
|
if (instId != 0) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
2012-05-17 19:47:18 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Check for overrun
|
|
|
|
if ((size + offset) > MetaNumBytes) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Set data
|
|
|
|
memcpy(dataOut, MetaDataPtr((struct UAVOMeta *)obj_handle) + offset, size);
|
|
|
|
} else {
|
|
|
|
struct UAVOData *obj;
|
|
|
|
InstanceHandle instEntry;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Cast to object info
|
|
|
|
obj = (struct UAVOData *)obj_handle;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Get instance information
|
|
|
|
instEntry = getInstance(obj, instId);
|
|
|
|
if (instEntry == NULL) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
2012-05-17 19:47:18 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Check for overrun
|
|
|
|
if ((size + offset) > obj->instance_size) {
|
|
|
|
goto unlock_exit;
|
|
|
|
}
|
2011-06-03 06:18:34 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Set data
|
|
|
|
memcpy(dataOut, InstanceData(instEntry) + offset, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
2012-05-26 06:28:05 +02:00
|
|
|
|
|
|
|
unlock_exit:
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return rc;
|
2011-06-03 06:18:34 +02:00
|
|
|
}
|
|
|
|
|
2011-05-14 22:15:33 +02:00
|
|
|
/**
|
|
|
|
* Set the object metadata
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[in] dataIn The object's metadata structure
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t UAVObjSetMetadata(UAVObjHandle obj_handle, const UAVObjMetadata *dataIn)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Set metadata (metadata of metaobjects can not be modified)
|
|
|
|
if (UAVObjIsMetaobject(obj_handle)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjSetData((UAVObjHandle)MetaObjectPtr((struct UAVOData *)obj_handle), dataIn);
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return 0;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the object metadata
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[out] dataOut The object's metadata structure
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t UAVObjGetMetadata(UAVObjHandle obj_handle, UAVObjMetadata *dataOut)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Get metadata
|
|
|
|
if (UAVObjIsMetaobject(obj_handle)) {
|
|
|
|
memcpy(dataOut, &defMetadata, sizeof(UAVObjMetadata));
|
|
|
|
} else {
|
|
|
|
UAVObjGetData((UAVObjHandle)MetaObjectPtr((struct UAVOData *)obj_handle),
|
|
|
|
dataOut);
|
|
|
|
}
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Unlock
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return 0;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 06:28:05 +02:00
|
|
|
/*******************************
|
|
|
|
* Object Metadata Manipulation
|
|
|
|
******************************/
|
2012-02-21 02:45:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the UAVObject metadata access member
|
|
|
|
* \param[in] metadata The metadata object
|
|
|
|
* \return the access type
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjAccessType UAVObjGetAccess(const UAVObjMetadata *metadata)
|
2012-02-21 02:45:18 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(metadata);
|
|
|
|
return (metadata->flags >> UAVOBJ_ACCESS_SHIFT) & 1;
|
2012-02-21 02:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the UAVObject metadata access member
|
|
|
|
* \param[in] metadata The metadata object
|
|
|
|
* \param[in] mode The access mode
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
void UAVObjSetAccess(UAVObjMetadata *metadata, UAVObjAccessType mode)
|
2012-02-21 02:45:18 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(metadata);
|
|
|
|
SET_BITS(metadata->flags, UAVOBJ_ACCESS_SHIFT, mode, 1);
|
2012-02-21 02:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the UAVObject metadata GCS access member
|
|
|
|
* \param[in] metadata The metadata object
|
|
|
|
* \return the GCS access type
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjAccessType UAVObjGetGcsAccess(const UAVObjMetadata *metadata)
|
2012-02-21 02:45:18 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(metadata);
|
|
|
|
return (metadata->flags >> UAVOBJ_GCS_ACCESS_SHIFT) & 1;
|
2012-02-21 02:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the UAVObject metadata GCS access member
|
|
|
|
* \param[in] metadata The metadata object
|
|
|
|
* \param[in] mode The access mode
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
void UAVObjSetGcsAccess(UAVObjMetadata *metadata, UAVObjAccessType mode)
|
|
|
|
{
|
|
|
|
PIOS_Assert(metadata);
|
|
|
|
SET_BITS(metadata->flags, UAVOBJ_GCS_ACCESS_SHIFT, mode, 1);
|
2012-02-21 02:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the UAVObject metadata telemetry acked member
|
|
|
|
* \param[in] metadata The metadata object
|
|
|
|
* \return the telemetry acked boolean
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
uint8_t UAVObjGetTelemetryAcked(const UAVObjMetadata *metadata)
|
|
|
|
{
|
|
|
|
PIOS_Assert(metadata);
|
|
|
|
return (metadata->flags >> UAVOBJ_TELEMETRY_ACKED_SHIFT) & 1;
|
2012-02-21 02:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the UAVObject metadata telemetry acked member
|
|
|
|
* \param[in] metadata The metadata object
|
|
|
|
* \param[in] val The telemetry acked boolean
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
void UAVObjSetTelemetryAcked(UAVObjMetadata *metadata, uint8_t val)
|
|
|
|
{
|
|
|
|
PIOS_Assert(metadata);
|
|
|
|
SET_BITS(metadata->flags, UAVOBJ_TELEMETRY_ACKED_SHIFT, val, 1);
|
2012-02-21 02:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the UAVObject metadata GCS telemetry acked member
|
|
|
|
* \param[in] metadata The metadata object
|
|
|
|
* \return the telemetry acked boolean
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
uint8_t UAVObjGetGcsTelemetryAcked(const UAVObjMetadata *metadata)
|
|
|
|
{
|
|
|
|
PIOS_Assert(metadata);
|
|
|
|
return (metadata->flags >> UAVOBJ_GCS_TELEMETRY_ACKED_SHIFT) & 1;
|
2012-02-21 02:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the UAVObject metadata GCS telemetry acked member
|
|
|
|
* \param[in] metadata The metadata object
|
|
|
|
* \param[in] val The GCS telemetry acked boolean
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
void UAVObjSetGcsTelemetryAcked(UAVObjMetadata *metadata, uint8_t val)
|
|
|
|
{
|
|
|
|
PIOS_Assert(metadata);
|
|
|
|
SET_BITS(metadata->flags, UAVOBJ_GCS_TELEMETRY_ACKED_SHIFT, val, 1);
|
2012-02-21 02:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the UAVObject metadata telemetry update mode
|
|
|
|
* \param[in] metadata The metadata object
|
|
|
|
* \return the telemetry update mode
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjUpdateMode UAVObjGetTelemetryUpdateMode(const UAVObjMetadata *metadata)
|
|
|
|
{
|
|
|
|
PIOS_Assert(metadata);
|
|
|
|
return (metadata->flags >> UAVOBJ_TELEMETRY_UPDATE_MODE_SHIFT) & UAVOBJ_UPDATE_MODE_MASK;
|
2012-02-21 02:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the UAVObject metadata telemetry update mode member
|
|
|
|
* \param[in] metadata The metadata object
|
|
|
|
* \param[in] val The telemetry update mode
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
void UAVObjSetTelemetryUpdateMode(UAVObjMetadata *metadata, UAVObjUpdateMode val)
|
|
|
|
{
|
|
|
|
PIOS_Assert(metadata);
|
|
|
|
SET_BITS(metadata->flags, UAVOBJ_TELEMETRY_UPDATE_MODE_SHIFT, val, UAVOBJ_UPDATE_MODE_MASK);
|
2012-02-21 02:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the UAVObject metadata GCS telemetry update mode
|
|
|
|
* \param[in] metadata The metadata object
|
|
|
|
* \return the GCS telemetry update mode
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjUpdateMode UAVObjGetGcsTelemetryUpdateMode(const UAVObjMetadata *metadata)
|
|
|
|
{
|
|
|
|
PIOS_Assert(metadata);
|
|
|
|
return (metadata->flags >> UAVOBJ_GCS_TELEMETRY_UPDATE_MODE_SHIFT) & UAVOBJ_UPDATE_MODE_MASK;
|
2012-02-21 02:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the UAVObject metadata GCS telemetry update mode member
|
|
|
|
* \param[in] metadata The metadata object
|
|
|
|
* \param[in] val The GCS telemetry update mode
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
void UAVObjSetGcsTelemetryUpdateMode(UAVObjMetadata *metadata, UAVObjUpdateMode val)
|
|
|
|
{
|
|
|
|
PIOS_Assert(metadata);
|
|
|
|
SET_BITS(metadata->flags, UAVOBJ_GCS_TELEMETRY_UPDATE_MODE_SHIFT, val, UAVOBJ_UPDATE_MODE_MASK);
|
2012-02-21 02:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-14 22:15:33 +02:00
|
|
|
/**
|
|
|
|
* Check if an object is read only
|
|
|
|
* \param[in] obj The object handle
|
2013-05-19 16:37:30 +02:00
|
|
|
* \return
|
|
|
|
* \arg 0 if not read only
|
2011-05-14 22:15:33 +02:00
|
|
|
* \arg 1 if read only
|
|
|
|
* \arg -1 if unable to get meta data
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
int8_t UAVObjReadOnly(UAVObjHandle obj_handle)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
|
|
|
if (!UAVObjIsMetaobject(obj_handle)) {
|
|
|
|
return UAVObjGetAccess(LinkedMetaDataPtr((struct UAVOData *)obj_handle)) == ACCESS_READONLY;
|
|
|
|
}
|
|
|
|
return -1;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect an event queue to the object, if the queue is already connected then the event mask is only updated.
|
|
|
|
* All events matching the event mask will be pushed to the event queue.
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[in] queue The event queue
|
|
|
|
* \param[in] eventMask The event mask, if EV_MASK_ALL then all events are enabled (e.g. EV_UPDATED | EV_UPDATED_MANUAL)
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
int32_t UAVObjConnectQueue(UAVObjHandle obj_handle, xQueueHandle queue,
|
2013-05-19 16:37:30 +02:00
|
|
|
uint8_t eventMask)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
|
|
|
PIOS_Assert(queue);
|
|
|
|
int32_t res;
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
|
|
|
res = connectObj(obj_handle, queue, 0, eventMask);
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return res;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect an event queue from the object.
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[in] queue The event queue
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
int32_t UAVObjDisconnectQueue(UAVObjHandle obj_handle, xQueueHandle queue)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
|
|
|
PIOS_Assert(queue);
|
|
|
|
int32_t res;
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
|
|
|
res = disconnectObj(obj_handle, queue, 0);
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return res;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect an event callback to the object, if the callback is already connected then the event mask is only updated.
|
|
|
|
* The supplied callback will be invoked on all events matching the event mask.
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[in] cb The event callback
|
|
|
|
* \param[in] eventMask The event mask, if EV_MASK_ALL then all events are enabled (e.g. EV_UPDATED | EV_UPDATED_MANUAL)
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
int32_t UAVObjConnectCallback(UAVObjHandle obj_handle, UAVObjEventCallback cb,
|
2013-05-19 16:37:30 +02:00
|
|
|
uint8_t eventMask)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
|
|
|
int32_t res;
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
|
|
|
res = connectObj(obj_handle, 0, cb, eventMask);
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return res;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect an event callback from the object.
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[in] cb The event callback
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
int32_t UAVObjDisconnectCallback(UAVObjHandle obj_handle, UAVObjEventCallback cb)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
|
|
|
int32_t res;
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
|
|
|
res = disconnectObj(obj_handle, 0, cb);
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
|
|
|
return res;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request an update of the object's data from the GCS. The call will not wait for the response, a EV_UPDATED event
|
|
|
|
* will be generated as soon as the object is updated.
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
void UAVObjRequestUpdate(UAVObjHandle obj_handle)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjRequestInstanceUpdate(obj_handle, UAVOBJ_ALL_INSTANCES);
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request an update of the object's data from the GCS. The call will not wait for the response, a EV_UPDATED event
|
|
|
|
* will be generated as soon as the object is updated.
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[in] instId Object instance ID to update
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
void UAVObjRequestInstanceUpdate(UAVObjHandle obj_handle, uint16_t instId)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
|
|
|
sendEvent((struct UAVOBase *)obj_handle, instId, EV_UPDATE_REQ);
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the object's data to the GCS (triggers a EV_UPDATED_MANUAL event on this object).
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
void UAVObjUpdated(UAVObjHandle obj_handle)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjInstanceUpdated(obj_handle, UAVOBJ_ALL_INSTANCES);
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the object's data to the GCS (triggers a EV_UPDATED_MANUAL event on this object).
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[in] instId The object instance ID
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
void UAVObjInstanceUpdated(UAVObjHandle obj_handle, uint16_t instId)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(obj_handle);
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
|
|
|
sendEvent((struct UAVOBase *)obj_handle, instId, EV_UPDATED_MANUAL);
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterate through all objects in the list.
|
|
|
|
* \param iterator This function will be called once for each object,
|
|
|
|
* the object will be passed as a parameter
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
void UAVObjIterate(void (*iterator)(UAVObjHandle obj))
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(iterator);
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Get lock
|
|
|
|
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Iterate through the list and invoke iterator for each object
|
|
|
|
UAVO_LIST_ITERATE (obj)
|
|
|
|
(*iterator)((UAVObjHandle)obj);
|
|
|
|
(*iterator)((UAVObjHandle) &obj->metaObj);
|
|
|
|
}
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Release lock
|
|
|
|
xSemaphoreGiveRecursive(mutex);
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-26 06:28:05 +02:00
|
|
|
* Send a triggered event to all event queues registered on the object.
|
2011-05-14 22:15:33 +02:00
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
static int32_t sendEvent(struct UAVOBase *obj, uint16_t instId,
|
|
|
|
UAVObjEventType triggered_event)
|
|
|
|
{
|
|
|
|
/* Set up the message that will be sent to all registered listeners */
|
|
|
|
UAVObjEvent msg = {
|
|
|
|
.obj = (UAVObjHandle)obj,
|
|
|
|
.event = triggered_event,
|
|
|
|
.instId = instId,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Go through each object and push the event message in the queue (if event is activated for the queue)
|
|
|
|
struct ObjectEventEntry *event;
|
|
|
|
|
|
|
|
LL_FOREACH(obj->next_event, event) {
|
|
|
|
if (event->eventMask == 0
|
|
|
|
|| (event->eventMask & triggered_event) != 0) {
|
|
|
|
// Send to queue if a valid queue is registered
|
|
|
|
if (event->queue) {
|
|
|
|
// will not block
|
|
|
|
if (xQueueSend(event->queue, &msg, 0) != pdTRUE) {
|
|
|
|
stats.lastQueueErrorID = UAVObjGetID(obj);
|
|
|
|
++stats.eventQueueErrors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke callback (from event task) if a valid one is registered
|
|
|
|
if (event->cb) {
|
|
|
|
// invoke callback from the event task, will not block
|
|
|
|
if (EventCallbackDispatch(&msg, event->cb) != pdTRUE) {
|
|
|
|
++stats.eventCallbackErrors;
|
|
|
|
stats.lastCallbackErrorID = UAVObjGetID(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new object instance, return the instance info or NULL if failure.
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
static InstanceHandle createInstance(struct UAVOData *obj, uint16_t instId)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct UAVOMultiInst *instEntry;
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Don't allow more than one instance for single instance objects */
|
|
|
|
if (UAVObjIsSingleInstance(&(obj->base))) {
|
|
|
|
PIOS_Assert(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Don't create more than the allowed number of instances */
|
|
|
|
if (instId >= UAVOBJ_MAX_INSTANCES) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Don't allow duplicate instances */
|
|
|
|
if (instId < UAVObjGetNumInstances(&(obj->base))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-05-26 06:28:05 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Create any missing instances (all instance IDs must be sequential)
|
|
|
|
for (uint16_t n = UAVObjGetNumInstances(&(obj->base)); n < instId; ++n) {
|
|
|
|
if (createInstance(obj, n) == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Create the actual instance */
|
|
|
|
uint32_t size = sizeof(struct UAVOMultiInst) + obj->instance_size;
|
|
|
|
instEntry = (struct UAVOMultiInst *)pvPortMalloc(size);
|
|
|
|
if (!instEntry) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memset(instEntry, 0, size);
|
|
|
|
LL_APPEND(((struct UAVOMulti *)obj)->instance0.next, instEntry);
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
((struct UAVOMulti *)obj)->num_instances++;
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Fire event
|
|
|
|
UAVObjInstanceUpdated((UAVObjHandle)obj, instId);
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Done
|
|
|
|
return InstanceDataOffset(instEntry);
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the instance information or NULL if the instance does not exist
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
static InstanceHandle getInstance(struct UAVOData *obj, uint16_t instId)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (UAVObjIsMetaobject(&obj->base)) {
|
|
|
|
/* Metadata Instance */
|
|
|
|
|
|
|
|
if (instId != 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Augment our pointer to reflect the proper type */
|
|
|
|
struct UAVOMeta *uavo_meta = (struct UAVOMeta *)obj;
|
|
|
|
return &(uavo_meta->instance0);
|
|
|
|
} else if (UAVObjIsSingleInstance(&(obj->base))) {
|
|
|
|
/* Single Instance */
|
|
|
|
|
|
|
|
if (instId != 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Augment our pointer to reflect the proper type */
|
|
|
|
struct UAVOSingle *uavo_single = (struct UAVOSingle *)obj;
|
|
|
|
return &(uavo_single->instance0);
|
|
|
|
} else {
|
|
|
|
/* Multi Instance */
|
|
|
|
/* Augment our pointer to reflect the proper type */
|
|
|
|
struct UAVOMulti *uavo_multi = (struct UAVOMulti *)obj;
|
|
|
|
if (instId >= uavo_multi->num_instances) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for specified instance ID
|
|
|
|
uint16_t instance = 0;
|
|
|
|
struct UAVOMultiInst *instEntry;
|
|
|
|
LL_FOREACH(&(uavo_multi->instance0), instEntry) {
|
|
|
|
if (instance++ == instId) {
|
|
|
|
/* Found it */
|
|
|
|
return &(instEntry->instance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Instance was not found */
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect an event queue to the object, if the queue is already connected then the event mask is only updated.
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[in] queue The event queue
|
|
|
|
* \param[in] cb The event callback
|
|
|
|
* \param[in] eventMask The event mask, if EV_MASK_ALL then all events are enabled (e.g. EV_UPDATED | EV_UPDATED_MANUAL)
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
static int32_t connectObj(UAVObjHandle obj_handle, xQueueHandle queue,
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjEventCallback cb, uint8_t eventMask)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct ObjectEventEntry *event;
|
|
|
|
struct UAVOBase *obj;
|
|
|
|
|
|
|
|
// Check that the queue is not already connected, if it is simply update event mask
|
|
|
|
obj = (struct UAVOBase *)obj_handle;
|
|
|
|
LL_FOREACH(obj->next_event, event) {
|
|
|
|
if (event->queue == queue && event->cb == cb) {
|
|
|
|
// Already connected, update event mask and return
|
|
|
|
event->eventMask = eventMask;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add queue to list
|
|
|
|
event = (struct ObjectEventEntry *)pvPortMalloc(sizeof(struct ObjectEventEntry));
|
|
|
|
if (event == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
event->queue = queue;
|
|
|
|
event->cb = cb;
|
|
|
|
event->eventMask = eventMask;
|
|
|
|
LL_APPEND(obj->next_event, event);
|
|
|
|
|
|
|
|
// Done
|
|
|
|
return 0;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect an event queue from the object
|
|
|
|
* \param[in] obj The object handle
|
|
|
|
* \param[in] queue The event queue
|
|
|
|
* \param[in] cb The event callback
|
|
|
|
* \return 0 if success or -1 if failure
|
|
|
|
*/
|
2012-05-26 06:28:05 +02:00
|
|
|
static int32_t disconnectObj(UAVObjHandle obj_handle, xQueueHandle queue,
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjEventCallback cb)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct ObjectEventEntry *event;
|
|
|
|
struct UAVOBase *obj;
|
2012-05-26 04:27:12 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Find queue and remove it
|
|
|
|
obj = (struct UAVOBase *)obj_handle;
|
|
|
|
LL_FOREACH(obj->next_event, event) {
|
|
|
|
if ((event->queue == queue
|
|
|
|
&& event->cb == cb)) {
|
|
|
|
LL_DELETE(obj->next_event, event);
|
|
|
|
vPortFree(event);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2011-05-14 22:15:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// If this point is reached the queue was not found
|
|
|
|
return -1;
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
2013-03-30 15:20:05 +01:00
|
|
|
#if defined(PIOS_USE_SETTINGS_ON_SDCARD)
|
2011-05-14 22:15:33 +02:00
|
|
|
/**
|
|
|
|
* Wrapper for the sprintf function
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
static void customSPrintf(uint8_t *buffer, uint8_t *format, ...)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
vsprintf((char *)buffer, (char *)format, args);
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an 8 character (plus extension) filename for the object.
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
static void objectFilename(UAVObjHandle obj_handle, uint8_t *filename)
|
2011-05-14 22:15:33 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
customSPrintf(filename, (uint8_t *)"%X.obj", UAVObjGetID(obj_handle));
|
2011-05-14 22:15:33 +02:00
|
|
|
}
|
2013-03-30 15:20:05 +01:00
|
|
|
#endif /* PIOS_USE_SETTINGS_ON_SDCARD */
|