Firmware
uORBKraitFastRpcChannel.hpp
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>
40 #include "uORB/uORBCommunicator.hpp"
41 #include "px4muorb_KraitRpcWrapper.hpp"
42 #include <map>
43 #include "drivers/drv_hrt.h"
44 
45 namespace uORB
46 {
47 class KraitFastRpcChannel;
48 }
49 
51 {
52 public:
57  {
58  if (_InstancePtr == nullptr) {
59  _InstancePtr = new uORB::KraitFastRpcChannel();
60  }
61 
62  return _InstancePtr;
63  }
64 
68  static bool isInstance()
69  {
70  return (_InstancePtr != nullptr);
71  }
72 
84  virtual int16_t topic_advertised(const char *messageName);
85 
98  virtual int16_t topic_unadvertised(const char *messageName);
99 
114  virtual int16_t add_subscription(const char *messageName, int32_t msgRateInHz);
115 
116 
128  virtual int16_t remove_subscription(const char *messageName);
129 
133  virtual int16_t register_handler(uORBCommunicator::IChannelRxHandler *handler);
134 
135 
136  //=========================================================================
137  // INTERFACES FOR Data messages
138  //=========================================================================
139 
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
161  static uORB::KraitFastRpcChannel *_InstancePtr;
163  pthread_t _RecvThread;
164  bool _ThreadStarted;
165  bool _ThreadShouldExit;
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 
173  struct BulkTransferHeader {
174  uint16_t _MsgType;
175  uint16_t _MsgNameLen;
176  uint16_t _DataLen;
177  };
178 
179  px4muorb::KraitRpcWrapper _KraitWrapper;
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.
189 
190  static void *thread_start(void *handler);
191 
192  void fastrpc_recv_thread();
193 
194 };
195 
196 #endif /* _uORBKraitFastRpcChannel_hpp_ */
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...
Definition: uORBKraitFastRpcChannel.cpp:80
virtual int16_t send_message(const char *messageName, int32_t length, uint8_t *data)
Sends the data message over the communication link.
Definition: uORBKraitFastRpcChannel.cpp:113
Definition: uORBKraitFastRpcChannel.hpp:50
virtual int16_t topic_advertised(const char *messageName)
Interface to notify the remote entity of a topic being advertised.
Definition: uORBKraitFastRpcChannel.cpp:71
Definition: uORBFastRpcChannel.hpp:44
High-resolution timer with callouts and timekeeping.
Definition: px4muorb_KraitRpcWrapper.hpp:42
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
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
static uORB::KraitFastRpcChannel * GetInstance()
static method to get the IChannel Implementor.
Definition: uORBKraitFastRpcChannel.hpp:56
virtual int16_t register_handler(uORBCommunicator::IChannelRxHandler *handler)
Register Message Handler.
Definition: uORBKraitFastRpcChannel.cpp:107
virtual int16_t remove_subscription(const char *messageName)
Interface to notify the remote entity of removal of a subscription.
Definition: uORBKraitFastRpcChannel.cpp:98
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.
Definition: uORBKraitFastRpcChannel.cpp:89
static bool isInstance()
Static method to check if there is an instance.
Definition: uORBKraitFastRpcChannel.hpp:68