Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef _MAV2DJI_MAVMODULE_H_
00010 #define _MAV2DJI_MAVMODULE_H_
00011
00012
00013 #include <mavlink/common/mavlink.h>
00014 #include <new>
00015 #include <string>
00016 #include <pthread.h>
00017
00018 #include "moduleBuf.h"
00019 #include "dji_sdk_dji2mav/mavHandler.h"
00020 #include "dji_sdk_dji2mav/log.h"
00021
00022 namespace dji2mav{
00023
00024 class MavModule {
00025
00026 public:
00033 MavModule(MavHandler &handler, std::string name, uint16_t bufSize) {
00034 m_hdlr = &handler;
00035 m_masterGcsIdx = -1;
00036 m_name = name;
00037
00038 try {
00039 m_senderRecord = new int[m_hdlr->getMaxListSize()];
00040
00041 memset( m_senderRecord, 0xFF,
00042 m_hdlr->getMaxListSize() * sizeof(int) );
00043 m_moduleBuf = new ModuleBuf(bufSize);
00044 } catch(std::bad_alloc& m) {
00045 DJI2MAV_FATAL( "Failed to allocate memory for mavModule! "
00046 "Exception: %s!", m.what() );
00047 exit(EXIT_FAILURE);
00048 }
00049
00050 DJI2MAV_DEBUG("Construct base MavModule.");
00051 }
00052
00053
00054 virtual ~MavModule() {
00055 DJI2MAV_DEBUG("Going to destruct base MavModule...");
00056 if(NULL != m_senderRecord) {
00057 delete []m_senderRecord;
00058 m_senderRecord = NULL;
00059 }
00060 if(NULL != m_moduleBuf)
00061 delete m_moduleBuf;
00062 m_hdlr = NULL;
00063 DJI2MAV_DEBUG("...finish destructing base Mavmodule.");
00064 }
00065
00066
00071 inline std::string getName() {
00072 return m_name;
00073 }
00074
00075
00080 inline uint8_t getMySysid() {
00081 return m_hdlr->getMySysid();
00082 }
00083
00084
00089 inline int getMasterGcsIdx() {
00090 return m_masterGcsIdx;
00091 }
00092
00093
00098 int getMasterGcsSenderIdx() {
00099 if(-1 == m_masterGcsIdx)
00100 return -2;
00101 else
00102 return m_senderRecord[m_masterGcsIdx];
00103 }
00104
00105
00111 bool setMasterGcsIdx(uint16_t gcsIdx) {
00112 if( !activateSender(gcsIdx) ) {
00113 DJI2MAV_ERROR("Fail to set master GCS to #%u because it "
00114 "is not activated", gcsIdx);
00115 return false;
00116 }
00117 if(-1 != m_masterGcsIdx) {
00118 DJI2MAV_INFO("Switch master GCS from #%u to #%u.",
00119 m_masterGcsIdx, gcsIdx);
00120 }
00121
00122 m_masterGcsIdx = gcsIdx;
00123 return true;
00124 }
00125
00126
00131 inline const int* getSenderRecord() {
00132 return m_senderRecord;
00133 }
00134
00135
00141 int getGcsSenderIdx(uint16_t gcsIdx) {
00142 if( !m_hdlr->isValidMngIdx(gcsIdx) )
00143 return -2;
00144 return m_senderRecord[gcsIdx];
00145 }
00146
00147
00153 bool employGcsSender(uint16_t gcsIdx) {
00154 if( !activateSender(gcsIdx) ) {
00155 DJI2MAV_ERROR("Fail to employ a sender for GCS #%u!", gcsIdx);
00156 return false;
00157 }
00158
00159 m_masterGcsIdx = -1;
00160 DJI2MAV_DEBUG("Succeed in employing sender for GCS #%u.", gcsIdx);
00161 return true;
00162 }
00163
00164
00170 bool activateSender(uint16_t gcsIdx) {
00171 if( !m_hdlr->isValidMngIdx(gcsIdx) ) {
00172 DJI2MAV_ERROR("Fail to activate sender for invalid GCS "
00173 "index %u!", gcsIdx);
00174 return false;
00175 }
00176 int sender = m_senderRecord[gcsIdx];
00177 if( -1 == sender ) {
00178 sender = m_hdlr->registerSender(gcsIdx);
00179 if(0 > sender) {
00180 DJI2MAV_ERROR("Fail to register sender for GCS #%u!",
00181 gcsIdx);
00182 return false;
00183 }
00184 m_senderRecord[gcsIdx] = sender;
00185 }
00186 DJI2MAV_DEBUG("Succeed in activating sender with index %u for "
00187 "GCS #%u.", m_senderRecord[gcsIdx], gcsIdx);
00188 return true;
00189 }
00190
00191
00198 bool sendMsgToGcs(uint16_t gcsIdx, mavlink_message_t& msg) {
00199 if(0 > m_senderRecord[gcsIdx]) {
00200 DJI2MAV_ERROR("Fail to send message to GCS #%u because no "
00201 "sender is employed for it!", gcsIdx);
00202 return false;
00203 }
00204 bool ret = m_hdlr->sendEncodedMsgToMng(gcsIdx,
00205 (uint16_t)m_senderRecord[gcsIdx], msg);
00206 if(ret) {
00207 DJI2MAV_TRACE("Succeed in sending message to GCS #%u "
00208 "using sender with index %u.", gcsIdx,
00209 m_senderRecord[gcsIdx]);
00210 } else {
00211 DJI2MAV_ERROR("Fail to send message to GCS #%u!", gcsIdx);
00212 }
00213 return ret;
00214 }
00215
00216
00223 bool sendMsgToSys(uint8_t sysid, mavlink_message_t& msg) {
00224 int idx = m_hdlr->findMngIdx(sysid);
00225 if( idx <= 0 || idx > m_hdlr->getMaxListSize() ) {
00226 DJI2MAV_ERROR("Fail to send message to GCS because no "
00227 "valid index is matched sysid %u! Return value: "
00228 "%d!", sysid, idx);
00229 return false;
00230 }
00231
00232 return sendMsgToGcs( (uint8_t)idx, msg);
00233 }
00234
00235
00241 inline bool sendMsgToMaster(mavlink_message_t& msg) {
00242 return sendMsgToGcs(m_masterGcsIdx, msg);
00243 }
00244
00245
00251 bool sendMsgToAll(mavlink_message_t& msg) {
00252 bool ret = true;
00253 if(-1 != m_masterGcsIdx) {
00254 return sendMsgToMaster(msg);
00255 } else {
00256 bool ret = true;
00257 for(uint16_t i = 0; i < m_hdlr->getMaxListSize(); ++i) {
00258 if( -1 != m_senderRecord[i] && !sendMsgToGcs(i, msg) ) {
00259 DJI2MAV_ERROR("Fail to send message to GCS #%u!", i);
00260 ret = false;
00261 }
00262 }
00263 if(!ret)
00264 DJI2MAV_ERROR("Fail to send the message to some GCS!");
00265 return ret;
00266 }
00267 }
00268
00269
00275 inline bool pushMsg(uint16_t gcsIdx,
00276 const mavlink_message_t &srcMsg) {
00277
00278 return m_moduleBuf->writeBuf( (uint8_t*)&srcMsg,
00279 sizeof(mavlink_message_t) );
00280
00281 }
00282
00283
00289 inline bool pullMsg(mavlink_message_t &destMsg) {
00290
00291 return m_moduleBuf->readBuf( (uint8_t*)&destMsg,
00292 sizeof(mavlink_message_t) );
00293
00294 }
00295
00296
00300 inline void clearBuf() {
00301 m_moduleBuf->clear();
00302 }
00303
00304
00309 bool run() {
00310 int ret = pthread_create( &m_tid, NULL, thread_call,
00311 (void*)this );
00312 if(0 != ret) {
00313 printf("Fail to create thread for the module.\n");
00314 return false;
00315 }
00316 return true;
00317 }
00318
00319
00324 static void* thread_call(void* param) {
00325 mavlink_message_t msg;
00326 while(true) {
00327
00328 ( (MavModule*)param )->receiveHelper(msg);
00329
00330 ( (MavModule*)param )->activelySend();
00331 }
00332 }
00333
00334
00339 void receiveHelper(mavlink_message_t &msg) {
00340 if( pullMsg(msg) ) {
00341 if( -1 != m_masterGcsIdx && msg.sysid !=
00342 m_hdlr->findMngSysid(m_masterGcsIdx) ) {
00343 return;
00344 }
00345 passivelyReceive(msg);
00346 }
00347 }
00348
00349
00354 virtual void passivelyReceive(mavlink_message_t &msg) = 0;
00355
00356
00360 virtual void activelySend() = 0;
00361
00362
00363 private:
00364 int* m_senderRecord;
00365 ModuleBuf* m_moduleBuf;
00366 int m_masterGcsIdx;
00367 uint8_t m_masterSysid;
00368
00369 MavHandler* m_hdlr;
00370
00371 pthread_t m_tid;
00372 std::string m_name;
00373
00374 };
00375
00376 }
00377
00378
00379 #endif