Firmware
uORBManager.hpp
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2015 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 #ifndef _uORBManager_hpp_
35 #define _uORBManager_hpp_
36 
37 #include "uORBCommon.hpp"
38 #include "uORBDeviceMaster.hpp"
39 
40 #include <stdint.h>
41 
42 #ifdef __PX4_NUTTX
43 #include "ORBSet.hpp"
44 #else
45 #include <string>
46 #include <set>
47 #define ORBSet std::set<std::string>
48 #endif
49 
50 #ifdef ORB_COMMUNICATOR
51 #include "uORBCommunicator.hpp"
52 #endif /* ORB_COMMUNICATOR */
53 
54 namespace uORB
55 {
56 class Manager;
57 }
58 
65 #ifdef ORB_COMMUNICATOR
67 #endif /* ORB_COMMUNICATOR */
68 {
69 public:
70  // public interfaces for this class.
71 
76  static bool initialize();
77 
83  static uORB::Manager *get_instance() { return _Instance; }
84 
92 
93  // ==== uORB interface methods ====
119  orb_advert_t orb_advertise(const struct orb_metadata *meta, const void *data, unsigned int queue_size = 1)
120  {
121  return orb_advertise_multi(meta, data, nullptr, ORB_PRIO_DEFAULT, queue_size);
122  }
123 
158  orb_advert_t orb_advertise_multi(const struct orb_metadata *meta, const void *data, int *instance,
159  int priority, unsigned int queue_size = 1);
160 
167  int orb_unadvertise(orb_advert_t handle);
168 
182  int orb_publish(const struct orb_metadata *meta, orb_advert_t handle, const void *data);
183 
212  int orb_subscribe(const struct orb_metadata *meta);
213 
250  int orb_subscribe_multi(const struct orb_metadata *meta, unsigned instance);
251 
258  int orb_unsubscribe(int handle);
259 
276  int orb_copy(const struct orb_metadata *meta, int handle, void *buffer);
277 
296  int orb_check(int handle, bool *updated);
297 
307  int orb_stat(int handle, uint64_t *time);
308 
316  int orb_exists(const struct orb_metadata *meta, int instance);
317 
328  int orb_priority(int handle, int32_t *priority);
329 
348  int orb_set_interval(int handle, unsigned interval);
349 
350 
360  int orb_get_interval(int handle, unsigned *interval);
361 
362 #ifdef ORB_COMMUNICATOR
363 
371  void set_uorb_communicator(uORBCommunicator::IChannel *comm_channel);
372 
376  uORBCommunicator::IChannel *get_uorb_communicator();
377 
382  bool is_remote_subscriber_present(const char *messageName);
383 #endif /* ORB_COMMUNICATOR */
384 
385 private: // class methods
393  int node_advertise(const struct orb_metadata *meta, int *instance = nullptr, int priority = ORB_PRIO_DEFAULT);
394 
401  int node_open(const struct orb_metadata *meta, bool advertiser, int *instance = nullptr,
402  int priority = ORB_PRIO_DEFAULT);
403 
404 private: // data members
405  static Manager *_Instance;
406 
407 #ifdef ORB_COMMUNICATOR
408  // the communicator channel instance.
409  uORBCommunicator::IChannel *_comm_channel{nullptr};
410 
411  ORBSet _remote_subscriber_topics;
412  ORBSet _remote_topics;
413 #endif /* ORB_COMMUNICATOR */
414 
415  DeviceMaster *_device_master{nullptr};
416 
417 private: //class methods
418  Manager();
419  ~Manager();
420 
421 #ifdef ORB_COMMUNICATOR
422 
434  virtual int16_t process_remote_topic(const char *topic_name, bool isAdvertisement);
435 
448  virtual int16_t process_add_subscription(const char *messageName, int32_t msgRateInHz);
449 
460  virtual int16_t process_remove_subscription(const char *messageName);
461 
476  virtual int16_t process_received_message(const char *messageName, int32_t length, uint8_t *data);
477 #endif /* ORB_COMMUNICATOR */
478 
479 #ifdef ORB_USE_PUBLISHER_RULES
480 
481  struct PublisherRule {
482  const char **topics; //null-terminated list of topic names
483  const char *module_name; //only this module is allowed to publish one of the topics
484  bool ignore_other_topics;
485  };
486 
490  bool startsWith(const char *pre, const char *str);
491 
495  bool findTopic(const PublisherRule &rule, const char *topic_name);
496 
500  void strTrim(const char **str);
501 
511  int readPublisherRulesFromFile(const char *file_name, PublisherRule &rule);
512 
513  PublisherRule _publisher_rule;
514  bool _has_publisher_rules = false;
515 
516 #endif /* ORB_USE_PUBLISHER_RULES */
517 
518 };
519 
520 #endif /* _uORBManager_hpp_ */
This is implemented as a singleton.
Definition: uORBManager.hpp:64
uORB::DeviceMaster * get_device_master()
Get the DeviceMaster.
Definition: uORBManager.cpp:81
int orb_unadvertise(orb_advert_t handle)
Unadvertise a topic.
Definition: uORBManager.cpp:230
int orb_set_interval(int handle, unsigned interval)
Set the minimum interval between which updates are seen for a subscription.
Definition: uORBManager.cpp:307
int orb_unsubscribe(int handle)
Unsubscribe from a topic.
Definition: uORBManager.cpp:254
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
Definition: uORBFastRpcChannel.hpp:44
Definition: ORBSet.hpp:36
Master control device for ObjDev.
Definition: uORBDeviceMaster.hpp:59
int orb_priority(int handle, int32_t *priority)
Return the priority of the topic.
Definition: uORBManager.cpp:302
int orb_copy(const struct orb_metadata *meta, int handle, void *buffer)
Fetch data from a topic.
Definition: uORBManager.cpp:272
orb_advert_t orb_advertise(const struct orb_metadata *meta, const void *data, unsigned int queue_size=1)
Advertise as the publisher of a topic.
Definition: uORBManager.hpp:119
Interface to enable remote subscriptions.
Definition: uORBCommunicator.hpp:51
Class passed to the communication link implement to provide callback for received messages over a cha...
Definition: uORBCommunicator.hpp:153
int orb_publish(const struct orb_metadata *meta, orb_advert_t handle, const void *data)
Publish new data to a topic.
Definition: uORBManager.cpp:259
int orb_subscribe(const struct orb_metadata *meta)
Subscribe to a topic.
Definition: uORBManager.cpp:243
Object metadata.
Definition: uORB.h:50
int orb_stat(int handle, uint64_t *time)
Return the last time that the topic was updated.
Definition: uORBManager.cpp:297
orb_advert_t orb_advertise_multi(const struct orb_metadata *meta, const void *data, int *instance, int priority, unsigned int queue_size=1)
Advertise as the publisher of a topic.
Definition: uORBManager.cpp:159
Definition: video_device.h:50
int orb_subscribe_multi(const struct orb_metadata *meta, unsigned instance)
Subscribe to a multi-instance of a topic.
Definition: uORBManager.cpp:248
static uORB::Manager * get_instance()
Method to get the singleton instance for the uORB::Manager.
Definition: uORBManager.hpp:83
int orb_check(int handle, bool *updated)
Check whether a topic has been published to since the last orb_copy.
Definition: uORBManager.cpp:290
static bool initialize()
Initialize the singleton.
Definition: uORBManager.cpp:49
int orb_exists(const struct orb_metadata *meta, int instance)
Check if a topic has already been created and published (advertised)
Definition: uORBManager.cpp:95
int orb_get_interval(int handle, unsigned *interval)
Get the minimum interval between which updates are seen for a subscription.
Definition: uORBManager.cpp:312