diff --git a/flight/OpenPilot/Makefile b/flight/OpenPilot/Makefile index ac773a4de..0bcb55c08 100644 --- a/flight/OpenPilot/Makefile +++ b/flight/OpenPilot/Makefile @@ -125,6 +125,7 @@ SRC += $(OPUAVOBJ)/eventdispatcher.c ## #SRC += $(OPTESTS)/test_I2c_PCF8570.c #SRC += $(OPTESTS)/test_BMP085.c +#SRC += $(OPTESTS)/test_uavobjects.c ## UAVOBJECTS diff --git a/flight/OpenPilot/Modules/Example/examplemodevent.c b/flight/OpenPilot/Modules/Example/examplemodevent.c index 8c1c937ac..f3d6ce3f5 100644 --- a/flight/OpenPilot/Modules/Example/examplemodevent.c +++ b/flight/OpenPilot/Modules/Example/examplemodevent.c @@ -56,7 +56,7 @@ // Private variables // Private functions -void ObjectUpdatedCb(UAVObjEvent* ev); +static void ObjectUpdatedCb(UAVObjEvent* ev); /** * Initialise the module, called on startup. @@ -80,7 +80,7 @@ int32_t ExampleModEventInitialize(void) * important since all callbacks are executed from the same thread, so other * queued events can not be executed until the currently active callback returns. */ -void ObjectUpdatedCb(UAVObjEvent* ev) +static void ObjectUpdatedCb(UAVObjEvent* ev) { ExampleSettingsData settings; ExampleObject1Data data1; diff --git a/flight/OpenPilot/Modules/Example/examplemodperiodic.c b/flight/OpenPilot/Modules/Example/examplemodperiodic.c index c2190c155..07210eecb 100644 --- a/flight/OpenPilot/Modules/Example/examplemodperiodic.c +++ b/flight/OpenPilot/Modules/Example/examplemodperiodic.c @@ -50,7 +50,7 @@ // Private constants #define STACK_SIZE 200 -#define TASK_PRIORITY (tskIDLE_PRIORITY) +#define TASK_PRIORITY (tskIDLE_PRIORITY+1) // Private types @@ -75,7 +75,7 @@ int32_t ExampleModPeriodicInitialize(void) /** * Module thread, should not return. */ -void exampleTask(void* parameters) +static void exampleTask(void* parameters) { ExampleSettingsData settings; ExampleObject1Data data; diff --git a/flight/OpenPilot/Modules/Example/examplemodthread.c b/flight/OpenPilot/Modules/Example/examplemodthread.c index b701b568a..650e6227e 100644 --- a/flight/OpenPilot/Modules/Example/examplemodthread.c +++ b/flight/OpenPilot/Modules/Example/examplemodthread.c @@ -51,7 +51,7 @@ // Private constants #define MAX_QUEUE_SIZE 20 #define STACK_SIZE 200 -#define TASK_PRIORITY (tskIDLE_PRIORITY) +#define TASK_PRIORITY (tskIDLE_PRIORITY+1) // Private types @@ -83,7 +83,7 @@ int32_t ExampleModThreadInitialize(void) /** * Module thread, should not return. */ -void exampleTask(void* parameters) +static void exampleTask(void* parameters) { UAVObjEvent ev; ExampleSettingsData settings; diff --git a/flight/OpenPilot/Modules/Telemetry/telemetry.c b/flight/OpenPilot/Modules/Telemetry/telemetry.c index 1ffac86d6..3036676fd 100644 --- a/flight/OpenPilot/Modules/Telemetry/telemetry.c +++ b/flight/OpenPilot/Modules/Telemetry/telemetry.c @@ -28,7 +28,7 @@ // Private constants #define MAX_QUEUE_SIZE 20 #define STACK_SIZE 100 -#define TASK_PRIORITY (tskIDLE_PRIORITY + 2) +#define TASK_PRIORITY (tskIDLE_PRIORITY + 1) #define REQ_TIMEOUT_MS 500 #define MAX_RETRIES 3 @@ -78,7 +78,7 @@ int32_t TelemetryInitialize(void) * telemetry settings. * \param[in] obj Object to connect */ -void registerObject(UAVObjHandle obj) +static void registerObject(UAVObjHandle obj) { // Setup object for periodic updates addObject(obj); @@ -91,7 +91,7 @@ void registerObject(UAVObjHandle obj) * Update object's queue connections and timer, depending on object's settings * \param[in] obj Object to updates */ -void updateObject(UAVObjHandle obj) +static void updateObject(UAVObjHandle obj) { UAVObjMetadata metadata; int32_t eventMask; diff --git a/flight/OpenPilot/System/openpilot.c b/flight/OpenPilot/System/openpilot.c index 43f488087..781a43c72 100644 --- a/flight/OpenPilot/System/openpilot.c +++ b/flight/OpenPilot/System/openpilot.c @@ -26,6 +26,7 @@ /* OpenPilot Includes */ #include "openpilot.h" +#include "uavobjectsinit.h" /* Task Priorities */ #define PRIORITY_TASK_HOOKS (tskIDLE_PRIORITY + 3) @@ -82,6 +83,11 @@ int main() } } + /* Initialize UAVObject libraries */ + EventDispatcherInitialize(); + UAVObjInitialize(); + UAVObjectsInitializeAll(); + /* Call LoadSettings which populates global variables so the rest of the hardware can be configured. */ PIOS_Settings_Load(); diff --git a/flight/OpenPilot/Tests/test_uavobjects.c b/flight/OpenPilot/Tests/test_uavobjects.c new file mode 100644 index 000000000..35f390396 --- /dev/null +++ b/flight/OpenPilot/Tests/test_uavobjects.c @@ -0,0 +1,173 @@ +/** + ****************************************************************************** + * + * @file test_uavobjects.c + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief Tests for the UAVObject libraries + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * 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" +#include "exampleobject1.h" +#include "uavobjectsinit.h" + +// Local functions +static void testTask(void *pvParameters); +static void eventCallback(UAVObjEvent* ev); +static void eventCallbackPeriodic(UAVObjEvent* ev); + +// Variables +int testDelay = 0; + +int main() +{ + /* Brings up System using CMSIS functions, enables the LEDs. */ + PIOS_SYS_Init(); + + /* Delay system */ + PIOS_DELAY_Init(); + + /* SPI Init */ + PIOS_SPI_Init(); + + /* Enables the SDCard */ + PIOS_SDCARD_Init(); + PIOS_SDCARD_MountFS(0); + + // Turn on both LEDs + PIOS_LED_On(LED1); + PIOS_LED_On(LED2); + + // Create test task + xTaskCreate(testTask, (signed portCHAR *)"ObjTest", 1000 , NULL, 1, NULL); + + // Start the FreeRTOS scheduler + vTaskStartScheduler(); + return 0; +} + +static void testTask(void *pvParameters) +{ + ExampleObject1Data data; + + // Initialize object and manager + EventDispatcherInitialize(); + UAVObjInitialize(); + UAVObjectsInitializeAll(); + + // Set data + ExampleObject1Get(&data); + data.Field1 = 1; + data.Field2 = 2; + ExampleObject1Set(&data); + + // Get data + memset(&data, 0, sizeof(data)); + ExampleObject1Get(&data); + + // Benchmark time is takes to get and set data (both operations) + int tickStart = xTaskGetTickCount(); + for (int n = 0; n < 10000; ++n) + { + ExampleObject1Set(&data); + ExampleObject1Get(&data); + } + int delay = xTaskGetTickCount() - tickStart; + + // Create a new instance and get/set its data + uint16_t instId = ExampleObject1CreateInstance(); + ExampleObject1InstSet(instId, &data); + memset(&data, 0, sizeof(data)); + ExampleObject1InstGet(instId, &data); + + // Test pack/unpack + uint8_t buffer[EXAMPLEOBJECT1_NUMBYTES]; + memset(buffer, 0, EXAMPLEOBJECT1_NUMBYTES); + UAVObjPack(ExampleObject1Handle(), 0, buffer); + memset(&data, 0, sizeof(data)); + ExampleObject1Set(&data); + UAVObjUnpack(ExampleObject1Handle(), 0, buffer); + ExampleObject1Get(&data); + + // Test object saving/loading to SD card + UAVObjSave(ExampleObject1Handle(), 0); + memset(&data, 0, sizeof(data)); + ExampleObject1Set(&data); + UAVObjLoad(ExampleObject1Handle(), 0); + ExampleObject1Get(&data); + + // Retrieve object handle by ID + UAVObjHandle handle = UAVObjGetByID(EXAMPLEOBJECT1_OBJID); + const char* name = UAVObjGetName(handle); + + // Get/Set the metadata + UAVObjMetadata mdata; + ExampleObject1GetMetadata(&mdata); + mdata.gcsTelemetryAcked = 0; + ExampleObject1SetMetadata(&mdata); + memset(&mdata, 0, sizeof(mdata)); + ExampleObject1GetMetadata(&mdata); + + // Test callbacks + ExampleObject1ConnectCallback(eventCallback); + ExampleObject1Set(&data); + + // Test periodic callback + UAVObjEvent ev; + ev.event = 0; + ev.instId = 0; + ev.obj = ExampleObject1Handle(); + EventPeriodicCreate(&ev, eventCallbackPeriodic, 500); + + // Test queue events + xQueueHandle queue; + queue = xQueueCreate(10, sizeof(UAVObjEvent)); + ExampleObject1ConnectQueue(queue); + + // Done testing + while (1) + { + if(xQueueReceive(queue, &ev, portMAX_DELAY) == pdTRUE) + { + name = UAVObjGetName(ev.obj); + PIOS_LED_Toggle(LED2); + } + } + +} + +static void eventCallback(UAVObjEvent* ev) +{ + const char* name = UAVObjGetName(ev->obj); +} + +static void eventCallbackPeriodic(UAVObjEvent* ev) +{ + static int lastUpdate; + testDelay = xTaskGetTickCount() - lastUpdate; + lastUpdate = xTaskGetTickCount(); + const char* name = UAVObjGetName(ev->obj); + PIOS_LED_Toggle(LED1); + ExampleObject1Updated(); +} + +void vApplicationIdleHook(void) +{ + /* Called when the scheduler has no tasks to run */ +} diff --git a/flight/OpenPilot/UAVObjects/eventdispatcher.c b/flight/OpenPilot/UAVObjects/eventdispatcher.c index 169f87c04..00df283a1 100644 --- a/flight/OpenPilot/UAVObjects/eventdispatcher.c +++ b/flight/OpenPilot/UAVObjects/eventdispatcher.c @@ -27,11 +27,10 @@ #include "openpilot.h" // Private constants -#define MAX_QUEUE_SIZE 10 -#define STACK_SIZE 100 -#define TASK_PRIORITY 100 +#define MAX_QUEUE_SIZE 20 +#define STACK_SIZE configMINIMAL_STACK_SIZE +#define TASK_PRIORITY (tskIDLE_PRIORITY + 2) #define MAX_UPDATE_PERIOD_MS 1000 -#define MIN_UPDATE_PERIOD_MS 1 // Private types @@ -56,14 +55,14 @@ struct PeriodicObjectListStruct { typedef struct PeriodicObjectListStruct PeriodicObjectList; // Private variables -PeriodicObjectList* objList; -xQueueHandle queue; -xTaskHandle eventTaskHandle; -xSemaphoreHandle mutex; +static PeriodicObjectList* objList; +static xQueueHandle queue; +static xTaskHandle eventTaskHandle; +static xSemaphoreHandle mutex; // Private functions -int32_t processPeriodicUpdates(); -void eventTask(); +static int32_t processPeriodicUpdates(); +static void eventTask(); /** * Initialize the dispatcher @@ -80,7 +79,7 @@ int32_t EventDispatcherInitialize() return -1; // Create event queue - queue = xQueueCreate(MAX_QUEUE_SIZE, sizeof(UAVObjEvent)); + queue = xQueueCreate(MAX_QUEUE_SIZE, sizeof(EventCallbackInfo)); // Create task xTaskCreate( eventTask, (signed char*)"Event", STACK_SIZE, NULL, TASK_PRIORITY, &eventTaskHandle ); @@ -132,6 +131,7 @@ int32_t EventPeriodicCreate(UAVObjEvent* ev, UAVObjEventCallback cb, int32_t per objEntry = (PeriodicObjectList*)pvPortMalloc(sizeof(PeriodicObjectList)); if (objEntry == NULL) return -1; memcpy(&objEntry->evInfo.ev, ev, sizeof(UAVObjEvent)); + objEntry->evInfo.cb = cb; objEntry->updatePeriodMs = periodMs; objEntry->timeToNextUpdateMs = 0; // Add to list @@ -202,7 +202,7 @@ int32_t EventPeriodicDelete(UAVObjEvent* ev, UAVObjEventCallback cb) /** * Event task, responsible of invoking callbacks. */ -void eventTask() +static void eventTask() { int32_t timeToNextUpdateMs; int32_t delayMs; @@ -240,50 +240,42 @@ void eventTask() * Handle periodic updates for all objects. * \return The system time until the next update (in ms) or -1 if failed */ -int32_t processPeriodicUpdates() +static int32_t processPeriodicUpdates() { - static int32_t timeOfLastUpdate = 0; PeriodicObjectList* objEntry; - int32_t delaySinceLastUpdateMs; - int32_t minDelay = MAX_UPDATE_PERIOD_MS; + int32_t timeNow; + int32_t timeToNextUpdate; // Get lock xSemaphoreTakeRecursive(mutex, portMAX_DELAY); // Iterate through each object and update its timer, if zero then transmit object. // Also calculate smallest delay to next update. - delaySinceLastUpdateMs = xTaskGetTickCount()*portTICK_RATE_MS - timeOfLastUpdate; + timeToNextUpdate = xTaskGetTickCount()*portTICK_RATE_MS + MAX_UPDATE_PERIOD_MS; LL_FOREACH(objList, objEntry) { // If object is configured for periodic updates if (objEntry->updatePeriodMs > 0) { - objEntry->timeToNextUpdateMs -= delaySinceLastUpdateMs; // Check if time for the next update - if (objEntry->timeToNextUpdateMs <= 0) + timeNow = xTaskGetTickCount()*portTICK_RATE_MS; + if (objEntry->timeToNextUpdateMs <= timeNow) { // Reset timer - objEntry->timeToNextUpdateMs = objEntry->updatePeriodMs; + objEntry->timeToNextUpdateMs = timeNow + objEntry->updatePeriodMs; // Invoke callback objEntry->evInfo.cb(&objEntry->evInfo.ev); // the function is expected to copy the event information } // Update minimum delay - if (objEntry->timeToNextUpdateMs < minDelay) + if (objEntry->timeToNextUpdateMs < timeToNextUpdate) { - minDelay = objEntry->timeToNextUpdateMs; + timeToNextUpdate = objEntry->timeToNextUpdateMs; } } } - // Check if delay for the next update is too short - if (minDelay < MIN_UPDATE_PERIOD_MS) - { - minDelay = MIN_UPDATE_PERIOD_MS; - } - // Done - timeOfLastUpdate = xTaskGetTickCount()*portTICK_RATE_MS; xSemaphoreGiveRecursive(mutex); - return timeOfLastUpdate + minDelay; + return timeToNextUpdate; } diff --git a/flight/OpenPilot/UAVObjects/exampleobject1.c b/flight/OpenPilot/UAVObjects/exampleobject1.c index ef864e6c2..ba7f8a283 100644 --- a/flight/OpenPilot/UAVObjects/exampleobject1.c +++ b/flight/OpenPilot/UAVObjects/exampleobject1.c @@ -32,7 +32,7 @@ #include "exampleobject1.h" // Private variables -UAVObjHandle handle; +static UAVObjHandle handle; /** * Initialize object. diff --git a/flight/OpenPilot/UAVObjects/exampleobject2.c b/flight/OpenPilot/UAVObjects/exampleobject2.c index 3f5654dee..42ac90e25 100644 --- a/flight/OpenPilot/UAVObjects/exampleobject2.c +++ b/flight/OpenPilot/UAVObjects/exampleobject2.c @@ -32,7 +32,7 @@ #include "exampleobject2.h" // Private variables -UAVObjHandle handle; +static UAVObjHandle handle; /** * Initialize object. diff --git a/flight/OpenPilot/UAVObjects/examplesettings.c b/flight/OpenPilot/UAVObjects/examplesettings.c index 96ee0d91d..216a2e073 100644 --- a/flight/OpenPilot/UAVObjects/examplesettings.c +++ b/flight/OpenPilot/UAVObjects/examplesettings.c @@ -32,7 +32,7 @@ #include "examplesettings.h" // Private variables -UAVObjHandle handle; +static UAVObjHandle handle; /** * Initialize object. diff --git a/flight/OpenPilot/UAVObjects/inc/exampleobject1.h b/flight/OpenPilot/UAVObjects/inc/exampleobject1.h index d332f8457..35b294a38 100644 --- a/flight/OpenPilot/UAVObjects/inc/exampleobject1.h +++ b/flight/OpenPilot/UAVObjects/inc/exampleobject1.h @@ -41,11 +41,20 @@ #define EXAMPLEOBJECT1_ISSETTINGS 0 #define EXAMPLEOBJECT1_NUMBYTES sizeof(ExampleObject1Data) -// Data access macros +// Object access macros #define ExampleObject1Get(dataOut) UAVObjGetData(ExampleObject1Handle(), dataOut) #define ExampleObject1Set(dataIn) UAVObjSetData(ExampleObject1Handle(), dataIn) +#define ExampleObject1InstGet(instId, dataOut) UAVObjGetInstanceData(ExampleObject1Handle(), instId, dataOut) +#define ExampleObject1InstSet(instId, dataIn) UAVObjSetInstanceData(ExampleObject1Handle(), instId, dataIn) #define ExampleObject1ConnectQueue(queue) UAVObjConnectQueue(ExampleObject1Handle(), queue, EV_MASK_ALL_UPDATES) #define ExampleObject1ConnectCallback(cb) UAVObjConnectCallback(ExampleObject1Handle(), cb, EV_MASK_ALL_UPDATES) +#define ExampleObject1CreateInstance() UAVObjCreateInstance(ExampleObject1Handle()) +#define ExampleObject1RequestUpdate() UAVObjRequestUpdate(ExampleObject1Handle()) +#define ExampleObject1RequestInstUpdate(instId) UAVObjRequestInstanceUpdate(ExampleObject1Handle(), instId) +#define ExampleObject1Updated() UAVObjUpdated(ExampleObject1Handle()) +#define ExampleObject1InstUpdated(instId) UAVObjUpdated(ExampleObject1Handle(), instId) +#define ExampleObject1GetMetadata(dataOut) UAVObjGetMetadata(ExampleObject1Handle(), dataOut) +#define ExampleObject1SetMetadata(dataIn) UAVObjSetMetadata(ExampleObject1Handle(), dataIn) // Object data typedef struct { diff --git a/flight/OpenPilot/UAVObjects/inc/exampleobject2.h b/flight/OpenPilot/UAVObjects/inc/exampleobject2.h index 18c7be7c0..902767849 100644 --- a/flight/OpenPilot/UAVObjects/inc/exampleobject2.h +++ b/flight/OpenPilot/UAVObjects/inc/exampleobject2.h @@ -41,11 +41,20 @@ #define EXAMPLEOBJECT2_ISSETTINGS 0 #define EXAMPLEOBJECT2_NUMBYTES sizeof(ExampleObject2Data) -// Data access macros +// Object access macros #define ExampleObject2Get(dataOut) UAVObjGetData(ExampleObject2Handle(), dataOut) #define ExampleObject2Set(dataIn) UAVObjSetData(ExampleObject2Handle(), dataIn) +#define ExampleObject2InstGet(instId, dataOut) UAVObjGetInstanceData(ExampleObject2Handle(), instId, dataOut) +#define ExampleObject2InstSet(instId, dataIn) UAVObjSetInstanceData(ExampleObject2Handle(), instId, dataIn) #define ExampleObject2ConnectQueue(queue) UAVObjConnectQueue(ExampleObject2Handle(), queue, EV_MASK_ALL_UPDATES) #define ExampleObject2ConnectCallback(cb) UAVObjConnectCallback(ExampleObject2Handle(), cb, EV_MASK_ALL_UPDATES) +#define ExampleObject2CreateInstance() UAVObjCreateInstance(ExampleObject2Handle()) +#define ExampleObject2RequestUpdate() UAVObjRequestUpdate(ExampleObject2Handle()) +#define ExampleObject2RequestInstUpdate(instId) UAVObjRequestInstanceUpdate(ExampleObject2Handle(), instId) +#define ExampleObject2Updated() UAVObjUpdated(ExampleObject2Handle()) +#define ExampleObject2InstUpdated(instId) UAVObjUpdated(ExampleObject2Handle(), instId) +#define ExampleObject2GetMetadata(dataOut) UAVObjGetMetadata(ExampleObject2Handle(), dataOut) +#define ExampleObject2SetMetadata(dataIn) UAVObjSetMetadata(ExampleObject2Handle(), dataIn) // Object data typedef struct { diff --git a/flight/OpenPilot/UAVObjects/inc/examplesettings.h b/flight/OpenPilot/UAVObjects/inc/examplesettings.h index ab261c9b0..f596f2b6c 100644 --- a/flight/OpenPilot/UAVObjects/inc/examplesettings.h +++ b/flight/OpenPilot/UAVObjects/inc/examplesettings.h @@ -41,11 +41,20 @@ #define EXAMPLESETTINGS_ISSETTINGS 1 #define EXAMPLESETTINGS_NUMBYTES sizeof(ExampleSettingsData) -// Data access macros +// Object access macros #define ExampleSettingsGet(dataOut) UAVObjGetData(ExampleSettingsHandle(), dataOut) #define ExampleSettingsSet(dataIn) UAVObjSetData(ExampleSettingsHandle(), dataIn) +#define ExampleSettingsInstGet(instId, dataOut) UAVObjGetInstanceData(ExampleSettingsHandle(), instId, dataOut) +#define ExampleSettingsInstSet(instId, dataIn) UAVObjSetInstanceData(ExampleSettingsHandle(), instId, dataIn) #define ExampleSettingsConnectQueue(queue) UAVObjConnectQueue(ExampleSettingsHandle(), queue, EV_MASK_ALL_UPDATES) #define ExampleSettingsConnectCallback(cb) UAVObjConnectCallback(ExampleSettingsHandle(), cb, EV_MASK_ALL_UPDATES) +#define ExampleSettingsCreateInstance() UAVObjCreateInstance(ExampleSettingsHandle()) +#define ExampleSettingsRequestUpdate() UAVObjRequestUpdate(ExampleSettingsHandle()) +#define ExampleSettingsRequestInstUpdate(instId) UAVObjRequestInstanceUpdate(ExampleSettingsHandle(), instId) +#define ExampleSettingsUpdated() UAVObjUpdated(ExampleSettingsHandle()) +#define ExampleSettingsInstUpdated(instId) UAVObjUpdated(ExampleSettingsHandle(), instId) +#define ExampleSettingsGetMetadata(dataOut) UAVObjGetMetadata(ExampleSettingsHandle(), dataOut) +#define ExampleSettingsSetMetadata(dataIn) UAVObjSetMetadata(ExampleSettingsHandle(), dataIn) // Object data typedef struct { diff --git a/flight/OpenPilot/UAVObjects/inc/uavobjectmanager.h b/flight/OpenPilot/UAVObjects/inc/uavobjectmanager.h index d4169e47d..65e670920 100644 --- a/flight/OpenPilot/UAVObjects/inc/uavobjectmanager.h +++ b/flight/OpenPilot/UAVObjects/inc/uavobjectmanager.h @@ -96,13 +96,12 @@ UAVObjHandle UAVObjGetByName(char* name); uint32_t UAVObjGetID(UAVObjHandle obj); const char* UAVObjGetName(UAVObjHandle obj); uint32_t UAVObjGetNumBytes(UAVObjHandle obj); -uint32_t UAVObjGetNumInstances(UAVObjHandle obj); +uint16_t UAVObjGetNumInstances(UAVObjHandle obj); UAVObjHandle UAVObjGetLinkedObj(UAVObjHandle obj); -int32_t UAVObjCreateInstance(UAVObjHandle obj); +uint16_t UAVObjCreateInstance(UAVObjHandle obj); int32_t UAVObjIsSingleInstance(UAVObjHandle obj); int32_t UAVObjIsMetaobject(UAVObjHandle obj); int32_t UAVObjIsSettings(UAVObjHandle obj); -int32_t UAVObjInitData(UAVObjHandle obj, const char* init); int32_t UAVObjUnpack(UAVObjHandle obj, uint16_t instId, const uint8_t* dataIn); int32_t UAVObjPack(UAVObjHandle obj, uint16_t instId, uint8_t* dataOut); int32_t UAVObjSave(UAVObjHandle obj, uint16_t instId); diff --git a/flight/OpenPilot/UAVObjects/inc/uavobjecttemplate.h b/flight/OpenPilot/UAVObjects/inc/uavobjecttemplate.h index 7be97c173..703ac6aee 100644 --- a/flight/OpenPilot/UAVObjects/inc/uavobjecttemplate.h +++ b/flight/OpenPilot/UAVObjects/inc/uavobjecttemplate.h @@ -41,11 +41,20 @@ #define $(NAMEUC)_ISSETTINGS $(ISSETTINGS) #define $(NAMEUC)_NUMBYTES sizeof($(NAME)Data) -// Data access macros +// Object access macros #define $(NAME)Get(dataOut) UAVObjGetData($(NAME)Handle(), dataOut) #define $(NAME)Set(dataIn) UAVObjSetData($(NAME)Handle(), dataIn) +#define $(NAME)InstGet(instId, dataOut) UAVObjGetInstanceData($(NAME)Handle(), instId, dataOut) +#define $(NAME)InstSet(instId, dataIn) UAVObjSetInstanceData($(NAME)Handle(), instId, dataIn) #define $(NAME)ConnectQueue(queue) UAVObjConnectQueue($(NAME)Handle(), queue, EV_MASK_ALL_UPDATES) #define $(NAME)ConnectCallback(cb) UAVObjConnectCallback($(NAME)Handle(), cb, EV_MASK_ALL_UPDATES) +#define $(NAME)CreateInstance() UAVObjCreateInstance($(NAME)Handle()) +#define $(NAME)RequestUpdate() UAVObjRequestUpdate($(NAME)Handle()) +#define $(NAME)RequestInstUpdate(instId) UAVObjRequestInstanceUpdate($(NAME)Handle(), instId) +#define $(NAME)Updated() UAVObjUpdated($(NAME)Handle()) +#define $(NAME)InstUpdated(instId) UAVObjUpdated($(NAME)Handle(), instId) +#define $(NAME)GetMetadata(dataOut) UAVObjGetMetadata($(NAME)Handle(), dataOut) +#define $(NAME)SetMetadata(dataIn) UAVObjSetMetadata($(NAME)Handle(), dataIn) // Object data typedef struct { diff --git a/flight/OpenPilot/UAVObjects/uavobjectmanager.c b/flight/OpenPilot/UAVObjects/uavobjectmanager.c index f3e842c61..2b8153770 100644 --- a/flight/OpenPilot/UAVObjects/uavobjectmanager.c +++ b/flight/OpenPilot/UAVObjects/uavobjectmanager.c @@ -70,16 +70,16 @@ struct ObjectListStruct { typedef struct ObjectListStruct ObjectList; // Private functions -int32_t sendEvent(ObjectList* obj, uint16_t instId, UAVObjEventType event); -ObjectInstList* createInstance(ObjectList* obj, uint16_t instId); -ObjectInstList* getInstance(ObjectList* obj, uint16_t instId); -int32_t connectObj(UAVObjHandle obj, xQueueHandle queue, UAVObjEventCallback cb, int32_t eventMask); -int32_t disconnectObj(UAVObjHandle obj, xQueueHandle queue, UAVObjEventCallback cb); +static int32_t sendEvent(ObjectList* obj, uint16_t instId, UAVObjEventType event); +static ObjectInstList* createInstance(ObjectList* obj, uint16_t instId); +static ObjectInstList* getInstance(ObjectList* obj, uint16_t instId); +static int32_t connectObj(UAVObjHandle obj, xQueueHandle queue, UAVObjEventCallback cb, int32_t eventMask); +static int32_t disconnectObj(UAVObjHandle obj, xQueueHandle queue, UAVObjEventCallback cb); // Private variables -ObjectList* objList; -xSemaphoreHandle mutex; -UAVObjMetadata defMetadata; +static ObjectList* objList; +static xSemaphoreHandle mutex; +static UAVObjMetadata defMetadata; /** * Initialize the object manager @@ -299,7 +299,7 @@ UAVObjHandle UAVObjGetLinkedObj(UAVObjHandle obj) * \param[in] obj The object handle * \return The number of instances */ -uint32_t UAVObjGetNumInstances(UAVObjHandle obj) +uint16_t UAVObjGetNumInstances(UAVObjHandle obj) { uint32_t numInstances; xSemaphoreTakeRecursive(mutex, portMAX_DELAY); @@ -311,9 +311,9 @@ uint32_t UAVObjGetNumInstances(UAVObjHandle obj) /** * Create a new instance in the object. * \param[in] obj The object handle - * \return The instance ID or -1 if an error + * \return The instance ID or 0 if an error */ -int32_t UAVObjCreateInstance(UAVObjHandle obj) +uint16_t UAVObjCreateInstance(UAVObjHandle obj) { ObjectList* objEntry; ObjectInstList* instEntry; @@ -365,18 +365,6 @@ int32_t UAVObjIsSettings(UAVObjHandle obj) return ((ObjectList*)obj)->isSettings; } -/** - * Initialize object data from a string (usually stored as a settings file) - * \param[in] obj The object handle - * \param[in] init Text with initialization information (settings file) - * \return 0 if success or -1 if failure - */ -int32_t UAVObjInitData(UAVObjHandle obj, const char* init) -{ - // TODO: Implement object data initialization from string (settings) - return -1; -} - /** * Unpack an object from a byte array * \param[in] obj The object handle @@ -784,15 +772,15 @@ int32_t UAVObjSetMetadata(UAVObjHandle obj, const UAVObjMetadata* dataIn) // Lock xSemaphoreTakeRecursive(mutex, portMAX_DELAY); - // Set metadata + // Set metadata (metadata of metaobjects can not be modified) objEntry = (ObjectList*)obj; - if (objEntry->isMetaobject) + if (!objEntry->isMetaobject) { - memcpy(&defMetadata, dataIn, sizeof(UAVObjMetadata)); + UAVObjSetData((UAVObjHandle)objEntry->linkedObj, dataIn); } else { - UAVObjSetData((UAVObjHandle)objEntry->linkedObj, dataIn); + return -1; } // Unlock @@ -963,7 +951,7 @@ void UAVObjIterate(void (*iterator)(UAVObjHandle obj)) /** * Send an event to all event queues registered on the object. */ -int32_t sendEvent(ObjectList* obj, uint16_t instId, UAVObjEventType event) +static int32_t sendEvent(ObjectList* obj, uint16_t instId, UAVObjEventType event) { ObjectQueueList* queueEntry; UAVObjEvent msg; @@ -998,7 +986,7 @@ int32_t sendEvent(ObjectList* obj, uint16_t instId, UAVObjEventType event) /** * Create a new object instance, return the instance info or NULL if failure. */ -ObjectInstList* createInstance(ObjectList* obj, uint16_t instId) +static ObjectInstList* createInstance(ObjectList* obj, uint16_t instId) { ObjectInstList* instEntry; int32_t n; @@ -1050,7 +1038,7 @@ ObjectInstList* createInstance(ObjectList* obj, uint16_t instId) /** * Get the instance information or NULL if the instance does not exist */ -ObjectInstList* getInstance(ObjectList* obj, uint16_t instId) +static ObjectInstList* getInstance(ObjectList* obj, uint16_t instId) { ObjectInstList* instEntry; @@ -1074,7 +1062,7 @@ ObjectInstList* getInstance(ObjectList* obj, uint16_t instId) * \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 */ -int32_t connectObj(UAVObjHandle obj, xQueueHandle queue, UAVObjEventCallback cb, int32_t eventMask) +static int32_t connectObj(UAVObjHandle obj, xQueueHandle queue, UAVObjEventCallback cb, int32_t eventMask) { ObjectQueueList* queueEntry; ObjectList* objEntry; @@ -1113,7 +1101,7 @@ int32_t connectObj(UAVObjHandle obj, xQueueHandle queue, UAVObjEventCallback cb, * \param[in] cb The event callback * \return 0 if success or -1 if failure */ -int32_t disconnectObj(UAVObjHandle obj, xQueueHandle queue, UAVObjEventCallback cb) +static int32_t disconnectObj(UAVObjHandle obj, xQueueHandle queue, UAVObjEventCallback cb) { ObjectQueueList* queueEntry; ObjectList* objEntry; diff --git a/flight/OpenPilot/UAVObjects/uavobjecttemplate.c b/flight/OpenPilot/UAVObjects/uavobjecttemplate.c index b242cd8ff..62d6c8082 100644 --- a/flight/OpenPilot/UAVObjects/uavobjecttemplate.c +++ b/flight/OpenPilot/UAVObjects/uavobjecttemplate.c @@ -32,7 +32,7 @@ #include "$(NAMELC).h" // Private variables -UAVObjHandle handle; +static UAVObjHandle handle; /** * Initialize object. diff --git a/flight/OpenPilot/UAVTalk/uavtalk.c b/flight/OpenPilot/UAVTalk/uavtalk.c index a49d0acf8..a08603044 100644 --- a/flight/OpenPilot/UAVTalk/uavtalk.c +++ b/flight/OpenPilot/UAVTalk/uavtalk.c @@ -44,21 +44,21 @@ typedef enum {STATE_SYNC, STATE_OBJID, STATE_INSTID, STATE_DATA, STATE_CS} RxState; // Private variables -UAVTalkOutputStream outStream; -xSemaphoreHandle lock; -xSemaphoreHandle respSema; -UAVObjHandle respObj; -uint16_t respInstId; -uint8_t rxBuffer[MAX_PACKET_LENGTH]; -uint8_t txBuffer[MAX_PACKET_LENGTH]; +static UAVTalkOutputStream outStream; +static xSemaphoreHandle lock; +static xSemaphoreHandle respSema; +static UAVObjHandle respObj; +static uint16_t respInstId; +static uint8_t rxBuffer[MAX_PACKET_LENGTH]; +static uint8_t txBuffer[MAX_PACKET_LENGTH]; // Private functions -uint16_t updateChecksum(uint16_t cs, uint8_t* data, int32_t length); -int32_t objectTransaction(uint32_t objectId, uint16_t instId, uint8_t type, int32_t timeout); -int32_t sendObject(UAVObjHandle obj, uint16_t instId, uint8_t type); -int32_t sendSingleObject(UAVObjHandle obj, uint16_t instId, uint8_t type); -int32_t receiveObject(uint8_t type, UAVObjHandle obj, uint16_t instId, uint8_t* data, int32_t length); -void updateAck(UAVObjHandle obj, uint16_t instId); +static uint16_t updateChecksum(uint16_t cs, uint8_t* data, int32_t length); +static int32_t objectTransaction(uint32_t objectId, uint16_t instId, uint8_t type, int32_t timeout); +static int32_t sendObject(UAVObjHandle obj, uint16_t instId, uint8_t type); +static int32_t sendSingleObject(UAVObjHandle obj, uint16_t instId, uint8_t type); +static int32_t receiveObject(uint8_t type, UAVObjHandle obj, uint16_t instId, uint8_t* data, int32_t length); +static void updateAck(UAVObjHandle obj, uint16_t instId); /** * Initialize the UAVTalk library @@ -121,7 +121,7 @@ int32_t UAVTalkSendObject(UAVObjHandle obj, uint16_t instId, uint8_t acked, int3 * \return 0 Success * \return -1 Failure */ -int32_t objectTransaction(UAVObjHandle obj, uint16_t instId, uint8_t type, int32_t timeoutMs) +static int32_t objectTransaction(UAVObjHandle obj, uint16_t instId, uint8_t type, int32_t timeoutMs) { int32_t respReceived; @@ -303,7 +303,7 @@ int32_t UAVTalkProcessInputStream(uint8_t rxbyte) * \return 0 Success * \return -1 Failure */ -int32_t receiveObject(uint8_t type, UAVObjHandle obj, uint16_t instId, uint8_t* data, int32_t length) +static int32_t receiveObject(uint8_t type, UAVObjHandle obj, uint16_t instId, uint8_t* data, int32_t length) { int32_t ret = 0; @@ -369,7 +369,7 @@ int32_t receiveObject(uint8_t type, UAVObjHandle obj, uint16_t instId, uint8_t* /** * Check if an ack is pending on an object and give response semaphore */ -void updateAck(UAVObjHandle obj, uint16_t instId) +static void updateAck(UAVObjHandle obj, uint16_t instId) { if (respObj == obj && (respInstId == instId || respInstId == UAVOBJ_ALL_INSTANCES)) { @@ -386,7 +386,7 @@ void updateAck(UAVObjHandle obj, uint16_t instId) * \return 0 Success * \return -1 Failure */ -int32_t sendObject(UAVObjHandle obj, uint16_t instId, uint8_t type) +static int32_t sendObject(UAVObjHandle obj, uint16_t instId, uint8_t type) { uint32_t numInst; uint32_t n; @@ -445,7 +445,7 @@ int32_t sendObject(UAVObjHandle obj, uint16_t instId, uint8_t type) * \return 0 Success * \return -1 Failure */ -int32_t sendSingleObject(UAVObjHandle obj, uint16_t instId, uint8_t type) +static int32_t sendSingleObject(UAVObjHandle obj, uint16_t instId, uint8_t type) { int32_t length; int32_t dataOffset; @@ -517,7 +517,7 @@ int32_t sendSingleObject(UAVObjHandle obj, uint16_t instId, uint8_t type) * \param[in] length Length of buffer * \return Updated checksum */ -uint16_t updateChecksum(uint16_t cs, uint8_t* data, int32_t length) +static uint16_t updateChecksum(uint16_t cs, uint8_t* data, int32_t length) { int32_t n; for (n = 0; n < length; ++n)