PX4 Firmware
PX4 Autopilot Software http://px4.io
uORBKraitFastRpcChannel.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2015 Mark Charlebois. 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 _uORBKraitFastRpcChannel_hpp_
35 #define _uORBKraitFastRpcChannel_hpp_
36 
37 #include <stdint.h>
38 #include <string>
39 #include <pthread.h>
42 #include <map>
43 #include "drivers/drv_hrt.h"
44 
45 namespace uORB
46 {
47 class KraitFastRpcChannel;
48 }
49 
51 {
52 public:
53  /**
54  * static method to get the IChannel Implementor.
55  */
57  {
58  if (_InstancePtr == nullptr) {
60  }
61 
62  return _InstancePtr;
63  }
64 
65  /**
66  * Static method to check if there is an instance.
67  */
68  static bool isInstance()
69  {
70  return (_InstancePtr != nullptr);
71  }
72 
73  /**
74  * @brief Interface to notify the remote entity of a topic being advertised.
75  *
76  * @param messageName
77  * This represents the uORB message name(aka topic); This message name should be
78  * globally unique.
79  * @return
80  * 0 = success; This means the messages is successfully sent to the receiver
81  * Note: This does not mean that the receiver as received it.
82  * otherwise = failure.
83  */
84  virtual int16_t topic_advertised(const char *messageName);
85 
86  /**
87  * @brief Interface to notify the remote entity of a topic being unadvertised
88  * and is no longer publishing messages.
89  *
90  * @param messageName
91  * This represents the uORB message name(aka topic); This message name should be
92  * globally unique.
93  * @return
94  * 0 = success; This means the messages is successfully sent to the receiver
95  * Note: This does not mean that the receiver as received it.
96  * otherwise = failure.
97  */
98  virtual int16_t topic_unadvertised(const char *messageName);
99 
100  /**
101  * @brief Interface to notify the remote entity of interest of a
102  * subscription for a message.
103  *
104  * @param messageName
105  * This represents the uORB message name; This message name should be
106  * globally unique.
107  * @param msgRate
108  * The max rate at which the subscriber can accept the messages.
109  * @return
110  * 0 = success; This means the messages is successfully sent to the receiver
111  * Note: This does not mean that the receiver as received it.
112  * otherwise = failure.
113  */
114  virtual int16_t add_subscription(const char *messageName, int32_t msgRateInHz);
115 
116 
117  /**
118  * @brief Interface to notify the remote entity of removal of a subscription
119  *
120  * @param messageName
121  * This represents the uORB message name; This message name should be
122  * globally unique.
123  * @return
124  * 0 = success; This means the messages is successfully sent to the receiver
125  * Note: This does not necessarily mean that the receiver as received it.
126  * otherwise = failure.
127  */
128  virtual int16_t remove_subscription(const char *messageName);
129 
130  /**
131  * Register Message Handler. This is internal for the IChannel implementer*
132  */
133  virtual int16_t register_handler(uORBCommunicator::IChannelRxHandler *handler);
134 
135 
136  //=========================================================================
137  // INTERFACES FOR Data messages
138  //=========================================================================
139 
140  /**
141  * @brief Sends the data message over the communication link.
142  * @param messageName
143  * This represents the uORB message name; This message name should be
144  * globally unique.
145  * @param length
146  * The length of the data buffer to be sent.
147  * @param data
148  * The actual data to be sent.
149  * @return
150  * 0 = success; This means the messages is successfully sent to the receiver
151  * Note: This does not mean that the receiver as received it.
152  * otherwise = failure.
153  */
154  virtual int16_t send_message(const char *messageName, int32_t length, uint8_t *data);
155 
156 
157  void Start();
158  void Stop();
159 
160 private: // data members
163  pthread_t _RecvThread;
166 
167  static const int32_t _CONTROL_MSG_TYPE_ADD_SUBSCRIBER = 1;
168  static const int32_t _CONTROL_MSG_TYPE_REMOVE_SUBSCRIBER = 2;
169  static const int32_t _DATA_MSG_TYPE = 3;
170  static const int32_t _CONTROL_MSG_TYPE_ADVERTISE = 4;
171  static const int32_t _CONTROL_MSG_TYPE_UNADVERTISE = 5;
172 
174  uint16_t _MsgType;
175  uint16_t _MsgNameLen;
176  uint16_t _DataLen;
177  };
178 
180 
181  std::map<std::string, int32_t> _AdspSubscriberCache;
182  std::map<std::string, hrt_abstime> _AdspSubscriberSampleTimestamp;
183  //hrt_abstime _SubCacheSampleTimestamp;
184  static const hrt_abstime _SubCacheRefreshRate = 1000000; // 1 second;
185 
186 private://class members.
187  /// constructor.
189 
190  static void *thread_start(void *handler);
191 
192  void fastrpc_recv_thread();
193 
194 };
195 
196 #endif /* _uORBKraitFastRpcChannel_hpp_ */
std::map< std::string, int32_t > _AdspSubscriberCache
virtual int16_t send_message(const char *messageName, int32_t length, uint8_t *data)
Sends the data message over the communication link.
uORBCommunicator::IChannelRxHandler * _RxHandler
static const int32_t _CONTROL_MSG_TYPE_REMOVE_SUBSCRIBER
static bool isInstance()
Static method to check if there is an instance.
High-resolution timer with callouts and timekeeping.
virtual int16_t register_handler(uORBCommunicator::IChannelRxHandler *handler)
Register Message Handler.
static const int32_t _CONTROL_MSG_TYPE_UNADVERTISE
static uORB::KraitFastRpcChannel * GetInstance()
static method to get the IChannel Implementor.
uint8_t * data
Definition: dataman.cpp:149
static void * thread_start(void *handler)
virtual int16_t topic_advertised(const char *messageName)
Interface to notify the remote entity of a topic being advertised.
static uORB::KraitFastRpcChannel * _InstancePtr
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
Class passed to the communication link implement to provide callback for received messages over a cha...
static const int32_t _CONTROL_MSG_TYPE_ADVERTISE
px4muorb::KraitRpcWrapper _KraitWrapper
virtual int16_t add_subscription(const char *messageName, int32_t msgRateInHz)
Interface to notify the remote entity of interest of a subscription for a message.
virtual int16_t topic_unadvertised(const char *messageName)
Interface to notify the remote entity of a topic being unadvertised and is no longer publishing messa...
Interface to enable remote subscriptions.
std::map< std::string, hrt_abstime > _AdspSubscriberSampleTimestamp
static const hrt_abstime _SubCacheRefreshRate
virtual int16_t remove_subscription(const char *messageName)
Interface to notify the remote entity of removal of a subscription.
static const int32_t _CONTROL_MSG_TYPE_ADD_SUBSCRIBER