From 36a556d10726a096dbad22ee14edfbc739382779 Mon Sep 17 00:00:00 2001 From: Lorenz Meier Date: Sun, 8 Nov 2015 10:29:07 +0100 Subject: [PATCH] Modify uORB API to allow cleaner in-app use --- src/modules/uORB/uORB.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/modules/uORB/uORB.h | 23 +++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/modules/uORB/uORB.cpp b/src/modules/uORB/uORB.cpp index 6d6f084d24..3d959b03dd 100644 --- a/src/modules/uORB/uORB.cpp +++ b/src/modules/uORB/uORB.cpp @@ -96,6 +96,44 @@ orb_advert_t orb_advertise_multi(const struct orb_metadata *meta, const void *da return uORB::Manager::get_instance()->orb_advertise_multi(meta, data, instance, priority); } +/** + * Advertise as the publisher of a topic. + * + * This performs the initial advertisement of a topic; it creates the topic + * node in /obj if required and publishes the initial data. + * + * Any number of advertisers may publish to a topic; publications are atomic + * but co-ordination between publishers is not provided by the ORB. + * + * @param meta The uORB metadata (usually from the ORB_ID() macro) + * for the topic. + * @param data A pointer to the initial data to be published. + * For topics updated by interrupt handlers, the advertisement + * must be performed from non-interrupt context. + * @param instance Pointer to an integer which will yield the instance ID (0-based) + * of the publication. + * @param priority The priority of the instance. If a subscriber subscribes multiple + * instances, the priority allows the subscriber to prioritize the best + * data source as long as its available. + * @return ERROR on error, zero on success. + * If the topic in question is not known (due to an + * ORB_DEFINE with no corresponding ORB_DECLARE) + * this function will return -1 and set errno to ENOENT. + */ +int orb_publish_auto(const struct orb_metadata *meta, orb_advert_t *handle, const void *data, int *instance, + int priority) +{ + if (*handle == nullptr) { + *handle = orb_advertise_multi(meta, data, instance, priority); + + if (handle != nullptr) { + return 0; + } + + } else { + return orb_publish(meta, handle, data); + } +} /** * Publish new data to a topic. diff --git a/src/modules/uORB/uORB.h b/src/modules/uORB/uORB.h index cfcf39b408..2f4f7af0c9 100644 --- a/src/modules/uORB/uORB.h +++ b/src/modules/uORB/uORB.h @@ -181,6 +181,29 @@ extern orb_advert_t orb_advertise(const struct orb_metadata *meta, const void *d extern orb_advert_t orb_advertise_multi(const struct orb_metadata *meta, const void *data, int *instance, int priority) __EXPORT; +/** + * Advertise and publish as the publisher of a topic. + * + * This performs the initial advertisement of a topic; it creates the topic + * node in /obj if required and publishes the initial data. + * + * Any number of advertisers may publish to a topic; publications are atomic + * but co-ordination between publishers is not provided by the ORB. + * + * @param meta The uORB metadata (usually from the ORB_ID() macro) + * for the topic. + * @param data A pointer to the initial data to be published. + * For topics updated by interrupt handlers, the advertisement + * must be performed from non-interrupt context. + * @param instance Pointer to an integer which will yield the instance ID (0-based, + * limited by ORB_MULTI_MAX_INSTANCES) of the publication. + * @param priority The priority of the instance. If a subscriber subscribes multiple + * instances, the priority allows the subscriber to prioritize the best + * data source as long as its available. + * @return zero on success, error number on failure + */ +extern int orb_publish_auto(const struct orb_metadata *meta, orb_advert_t *handle, const void *data, int *instance, + int priority) /** * Publish new data to a topic.