dji_subscription.hpp
Go to the documentation of this file.
1 
30 #ifndef DJI_DATASUBSCRIPTION_H
31 #define DJI_DATASUBSCRIPTION_H
32 
33 #include "dji_log.hpp"
34 #include "dji_telemetry.hpp"
35 #include "dji_vehicle_callback.hpp"
36 
37 #ifdef __linux__
38 #include <cstring>
39 #elif STM32
40 #include <stdlib.h>
42 #include <string.h>
43 #endif
44 
45 namespace DJI
46 {
47 namespace OSDK
48 {
49 
50 // Forward Declarations
51 class Vehicle;
52 
61 {
62 public:
63 #pragma pack(1)
64  typedef struct PackageInfo
65  {
66  uint8_t packageID;
67  uint16_t freq;
68  uint8_t config;
69  uint8_t numberOfTopics;
70  } PackageInfo;
71 #pragma pack()
72 
73 public:
76 
77  void setPackageID(uint8_t id);
78  void setConfig(uint8_t config);
79 
89  bool setTopicList(Telemetry::TopicName* topics, int numberOfTopics,
90  uint16_t freq);
91  void allocateDataBuffer();
92  void clearDataBuffer();
93 
94  void cleanUpPackage();
95 
102  int serializePackageInfo(uint8_t* buffer);
103 
104  void setUserUnpackCallback(VehicleCallBack userFunctionAfterPackageExtraction,
105  UserData userData);
106 
107  bool isOccupied();
108  void setOccupied(bool status);
109 
110  bool hasLeftOverData();
111  void setLeftOverDataFlag(bool flag);
112 
113  // Accessors to private variables:
114  PackageInfo getInfo();
115  uint32_t* getUidList(); // explicitly show it's a pointer
116  Telemetry::TopicName* getTopicList();
117  uint32_t* getOffsetList();
118  uint8_t* getDataBuffer();
119  uint32_t getBufferSize();
120  VehicleCallBackHandler getUnpackHandler();
121 
129 
137 
138 private: // Private variables
139  bool occupied;
140  bool leftOverDataFlag;
141  PackageInfo info;
142 
143  // We have only 30 topics and 5 packages.
144  // So let's not bother with dynamic memory for now.
145  // The UID
146  uint32_t uidList[Telemetry::TOTAL_TOPIC_NUMBER];
147  // The Name enum, used as index in the DataBase
148  Telemetry::TopicName topicList[Telemetry::TOTAL_TOPIC_NUMBER];
149  // The offset of each topic in the data flow
150  uint32_t offsetList[Telemetry::TOTAL_TOPIC_NUMBER];
151 
156  uint32_t packageDataSize;
157 
161  uint8_t* incomingDataBuffer;
162 
168  VehicleCallBackHandler userUnpackHandler;
169 }; // class SubscriptionPackage
170 
181 {
182 public: // public methods
183  DataSubscription(Vehicle* vehicle);
184  ~DataSubscription();
185 
186  Vehicle* getVehicle();
199  bool initPackageFromTopicList(int packageID, int numberOfTopics,
200  Telemetry::TopicName* topicList,
201  bool sendTimeStamp, uint16_t freq);
202 
208  void verify();
209 
217  ACK::ErrorCode verify(int timeout); // blocking call
218 
225  void startPackage(int packageID);
226 
235  ACK::ErrorCode startPackage(int packageID, int timeout); // blocking call
236 
244  void removePackage(int packageID);
245 
254  ACK::ErrorCode removePackage(int packageID, int timeout); // blocking call
255 
262  void removeLeftOverPackages();
263 
271 
278  void reset();
279 
286  ACK::ErrorCode reset(int timeout);
287 
296  int packageID, VehicleCallBack userFunctionAfterPackageExtraction,
297  UserData userData = NULL);
298 
299  // Not implemented yet
300  // bool pausePackage(int packageID);
301  // bool resumePackage(int packageID);
302  // bool changePackageFrequency(int packageID, uint16_t newFreq);
303 
311  static void verifyCallback(Vehicle* vehiclePtr, RecvContainer rcvContainer,
312  UserData userData);
313 
314  static void addPackageCallback(Vehicle* vehiclePtr,
315  RecvContainer rcvContainer,
316  UserData pkgHandle);
317 
318  static void removePackageCallback(Vehicle* vehiclePtr,
319  RecvContainer rcvContainer,
320  UserData pkgHandle);
321 
322  static void resetCallback(Vehicle* vehiclePtr,
323  RecvContainer rcvContainer,
324  UserData pkgHandle);
325 
333  static void decodeCallback(Vehicle* vehiclePtr, RecvContainer rcvContainer,
334  UserData subscriptionPtr);
335 
336  template <Telemetry::TopicName topic>
337  typename Telemetry::TypeMap<topic>::type getValue()
338  {
339  typename Telemetry::TypeMap<topic>::type ans;
340 
341  void* p = Telemetry::TopicDataBase[topic].latest;
342 
343  lockMSG();
344  if (p)
345  {
346  ans = *reinterpret_cast<typename Telemetry::TypeMap<topic>::type*>(p);
347  freeMSG();
348  return ans;
349  }
350  else
351  {
352  DERROR("Topic 0x%X value memory not initialized, return default", topic);
353  }
354  freeMSG();
355 
356  memset(&ans, 0xFF, sizeof(ans));
357  return ans;
358  }
359 
360 public: // public variables
361  const static uint8_t MAX_NUMBER_OF_PACKAGE = 7;
362  VehicleCallBackHandler subscriptionDataDecodeHandler;
363 
364 private: // private variables
365  Vehicle* vehicle;
366  SubscriptionPackage package[MAX_NUMBER_OF_PACKAGE];
367 
368 private: // private methods
369  void extractOnePackage(RecvContainer* pRcvContainer,
370  SubscriptionPackage* pkg);
371  T_OsdkMutexHandle m_msgLock;
372  void lockMSG();
373  void freeMSG();
374 };
375 }
376 }
377 
378 #endif // DJI_DATASUBSCRIPTION_H
void * UserData
This is used as the datatype for all data arguments in callbacks.
Definition: dji_type.hpp:75
void registerUserPackageUnpackCallback(int packageID, VehicleCallBack userFunctionAfterPackageExtraction, UserData userData=NULL)
Register a callback function after package[packageID] is received.
Definition: dji_subscription.cpp:181
DataSubscription(Vehicle *vehicle)
Definition: dji_subscription.cpp:99
This struct is returned from all blocking calls, except certain mission calls that have explicit type...
Definition: dji_ack.hpp:146
void removeAllExistingPackages()
Remove all occupied packages.
Definition: dji_subscription.cpp:495
Type definition for new Vehicle-style callbacks.
void removeLeftOverPackages()
Remove leftover incoming telemetry data due to unclean quit.
Definition: dji_subscription.cpp:472
void(* VehicleCallBack)(Vehicle *vehicle, RecvContainer recvFrame, UserData userData)
Function prototype for all callback functions used in the OSDK.
Definition: dji_vehicle_callback.hpp:76
void packageAddSuccessHandler()
Helper function to do post processing when adding package is successful.
Definition: dji_subscription.cpp:776
Package class to support Subscribe-style telemetry.
Definition: dji_subscription.hpp:60
int serializePackageInfo(uint8_t *buffer)
Serialize the info and uidList to a buffer to send to FC.
Definition: dji_subscription.cpp:713
TopicName
enum TopicName is the interface for user to create packages and access data It is also used as index ...
Definition: dji_telemetry.hpp:59
Telemetry API through asynchronous "Subscribe"-style messages.
Definition: dji_subscription.hpp:180
static void decodeCallback(Vehicle *vehiclePtr, RecvContainer rcvContainer, UserData subscriptionPtr)
This callback function is called by recvReqData, case CMD_ID_SUBSCRIBE.
Definition: dji_subscription.cpp:131
Received info.
Definition: dji_vehicle_callback.hpp:59
bool setTopicList(Telemetry::TopicName *topics, int numberOfTopics, uint16_t freq)
Fill in necessary information for ADD_PACKAGE call.
Definition: dji_subscription.cpp:627
void packageRemoveSuccessHandler()
Helper function to do post processing when removing package is successful.
Definition: dji_subscription.cpp:793
bool initPackageFromTopicList(int packageID, int numberOfTopics, Telemetry::TopicName *topicList, bool sendTimeStamp, uint16_t freq)
This is the interface for the end user to generate a package for subscription.
Definition: dji_subscription.cpp:166
The CallBackHandler struct allows users to encapsulate callbacks and data in one struct.
Definition: dji_vehicle_callback.hpp:83
Enumeration of all telemetry data types, structures and maps.
static void verifyCallback(Vehicle *vehiclePtr, RecvContainer rcvContainer, UserData userData)
Callback function for non-blocking verify()
Definition: dji_subscription.cpp:214
void verify()
Non-blocking call for version match.
Definition: dji_subscription.cpp:202
Definition: dji_ack.cpp:38
void removePackage(int packageID)
Non-blocking call for start package.
Definition: dji_subscription.cpp:406
void startPackage(int packageID)
Non-blocking call for starting a package.
Definition: dji_subscription.cpp:255
void reset()
Non-blocking call for resetting all packages.
Definition: dji_subscription.cpp:511