00001 00012 #include <string.h> 00013 #include <stdio.h> 00014 #include "DJI_App.h" 00015 #include "DJI_API.h" 00016 00017 using namespace DJI; 00018 using namespace DJI::onboardSDK; 00019 00020 inline void passData(uint16_t flag, uint16_t &enable, void *data, unsigned char *buf, 00021 size_t datalen, size_t &offset) 00022 { 00024 if ((flag & enable)) 00025 { 00026 memcpy((unsigned char *)data, (unsigned char *)buf + offset, datalen); 00027 offset += datalen; 00028 } 00029 enable <<= 1; 00030 } 00031 00032 unsigned char getCmdSet(Header *protocolHeader) 00033 { 00034 unsigned char *ptemp = ((unsigned char *)protocolHeader) + sizeof(Header); 00035 return *ptemp; 00036 } 00037 00038 unsigned char getCmdCode(Header *protocolHeader) 00039 { 00040 unsigned char *ptemp = ((unsigned char *)protocolHeader) + sizeof(Header); 00041 ptemp++; 00042 return *ptemp; 00043 } 00044 00045 BroadcastData DJI::onboardSDK::CoreAPI::getBroadcastData() const { return broadcastData; } 00046 00047 BatteryData DJI::onboardSDK::CoreAPI::getBatteryCapacity() const 00048 { 00049 return broadcastData.battery; 00050 } 00051 00052 CtrlInfoData DJI::onboardSDK::CoreAPI::getCtrlInfo() const { return broadcastData.ctrlInfo; } 00053 00054 void DJI::onboardSDK::CoreAPI::setBroadcastFrameStatus(bool isFrame) 00055 { 00056 broadcastFrameStatus = isFrame; 00057 } 00058 00059 bool DJI::onboardSDK::CoreAPI::getBroadcastFrameStatus() 00060 { 00061 return broadcastFrameStatus; 00062 } 00063 00064 #ifdef SDK_DEV 00065 #include "devApp.cpp" 00066 #else 00067 void DJI::onboardSDK::CoreAPI::broadcast(Header *protocolHeader) 00068 { 00069 unsigned char *pdata = ((unsigned char *)protocolHeader) + sizeof(Header); 00070 unsigned short *enableFlag; 00071 serialDevice->lockMSG(); 00072 pdata += 2; 00073 enableFlag = (unsigned short *)pdata; 00074 broadcastData.dataFlag = *enableFlag; 00075 size_t len = MSG_ENABLE_FLAG_LEN; 00076 00078 uint16_t DATA_FLAG = 0x0001; 00080 00081 if (versionData.version != versionM100_23) 00082 passData(*enableFlag, DATA_FLAG, &broadcastData.timeStamp, pdata, sizeof(TimeStampData), 00083 len); 00084 else 00085 passData(*enableFlag, DATA_FLAG, &broadcastData.timeStamp.time, pdata, sizeof(uint32_t), 00086 len); 00087 00088 passData(*enableFlag, DATA_FLAG, &broadcastData.q, pdata, sizeof(QuaternionData), len); 00089 passData(*enableFlag, DATA_FLAG, &broadcastData.a, pdata, sizeof(CommonData), len); 00090 passData(*enableFlag, DATA_FLAG, &broadcastData.v, pdata, sizeof(VelocityData), len); 00091 passData(*enableFlag, DATA_FLAG, &broadcastData.w, pdata, sizeof(CommonData), len); 00092 passData(*enableFlag, DATA_FLAG, &broadcastData.pos, pdata, sizeof(PositionData), len); 00093 00094 if (versionData.version == versionA3_31 || versionData.version == versionA3_32) 00095 { 00096 passData(*enableFlag, DATA_FLAG, &broadcastData.gps, pdata, sizeof(GPSData), len); 00097 passData(*enableFlag, DATA_FLAG, &broadcastData.rtk, pdata, sizeof(RTKData), len); 00098 if (((*enableFlag) & 0x0040)) 00099 API_LOG(serialDevice, RTK_LOG, "receive GPS data %llu\n", (unsigned long long)serialDevice->getTimeStamp()); 00100 if (((*enableFlag) & 0x0080)) 00101 API_LOG(serialDevice, RTK_LOG, "receive RTK data %llu\n", (unsigned long long)serialDevice->getTimeStamp()); 00102 } 00103 passData(*enableFlag, DATA_FLAG, &broadcastData.mag, pdata, sizeof(MagnetData), len); 00104 passData(*enableFlag, DATA_FLAG, &broadcastData.rc, pdata, sizeof(RadioData), len); 00105 passData(*enableFlag, DATA_FLAG, &broadcastData.gimbal, pdata, 00106 sizeof(GimbalData) - ((versionData.version == versionM100_23) ? 1 : 0), len); 00107 passData(*enableFlag, DATA_FLAG, &broadcastData.status, pdata, sizeof(FlightStatus), len); 00108 passData(*enableFlag, DATA_FLAG, &broadcastData.battery, pdata, sizeof(BatteryData), len); 00109 passData(*enableFlag, DATA_FLAG, &broadcastData.ctrlInfo, pdata, 00110 sizeof(CtrlInfoData) - ((versionData.version == versionM100_23) ? 1 : 0), len); 00111 serialDevice->freeMSG(); 00112 00117 setBroadcastFrameStatus(true); 00118 00119 if (broadcastCallback.callback) 00120 broadcastCallback.callback(this, protocolHeader, broadcastCallback.userData); 00121 } 00122 #endif 00123 void DJI::onboardSDK::CoreAPI::recvReqData(Header *protocolHeader) 00124 { 00125 unsigned char buf[100] = { 0, 0 }; 00126 00127 uint8_t ack = *((unsigned char *)protocolHeader + sizeof(Header) + 2); 00128 if (getCmdSet(protocolHeader) == SET_BROADCAST) 00129 { 00130 switch (getCmdCode(protocolHeader)) 00131 { 00132 case CODE_BROADCAST: 00133 broadcast(protocolHeader); 00134 break; 00135 case CODE_FROMMOBILE: 00136 API_LOG(serialDevice, STATUS_LOG, "Receive data from mobile\n"); 00137 if (fromMobileCallback.callback) 00138 { 00139 fromMobileCallback.callback(this, protocolHeader, fromMobileCallback.userData); 00140 } 00141 else 00142 { 00143 parseFromMobileCallback(this, protocolHeader); 00144 } 00145 break; 00146 case CODE_LOSTCTRL: 00147 API_LOG(serialDevice, STATUS_LOG, "onboardSDK lost control\n"); 00148 Ack param; 00149 if (protocolHeader->sessionID > 0) 00150 { 00151 buf[0] = buf[1] = 0; 00152 param.sessionID = protocolHeader->sessionID; 00153 param.seqNum = protocolHeader->sequenceNumber; 00154 param.encrypt = protocolHeader->enc; 00155 param.buf = buf; 00156 param.length = 2; 00157 ackInterface(¶m); 00158 } 00159 break; 00160 case CODE_MISSION: 00162 if (missionCallback.callback) 00163 missionCallback.callback(this, protocolHeader, missionCallback.userData); 00164 else 00165 { 00166 switch (ack) 00167 { 00168 case MISSION_MODE_A: 00169 break; 00170 case MISSION_WAYPOINT: 00171 if (wayPointData) 00172 { 00173 if (wayPointCallback.callback) 00174 wayPointCallback.callback(this, protocolHeader, 00175 wayPointCallback.userData); 00176 else 00177 API_LOG(serialDevice, STATUS_LOG, "Mode waypoint \n"); 00178 } 00179 break; 00180 case MISSION_HOTPOINT: 00181 if (hotPointData) 00182 { 00183 if (hotPointCallback.callback) 00184 hotPointCallback.callback(this, protocolHeader, 00185 hotPointCallback.userData); 00186 else 00187 API_LOG(serialDevice, STATUS_LOG, "Mode HP \n"); 00188 } 00189 break; 00190 case MISSION_FOLLOW: 00191 if (followData) 00192 { 00193 if (followCallback.callback) 00194 followCallback.callback(this, protocolHeader, 00195 followCallback.userData); 00196 else 00197 API_LOG(serialDevice, STATUS_LOG, "Mode Follow \n"); 00198 } 00199 break; 00200 case MISSION_IOC: 00202 API_LOG(serialDevice, STATUS_LOG, "Mode IOC \n"); 00203 break; 00204 default: 00205 API_LOG(serialDevice, ERROR_LOG, "Unknown mission code 0x%X \n", ack); 00206 break; 00207 } 00208 } 00209 break; 00210 case CODE_WAYPOINT: 00212 if (wayPointEventCallback.callback) 00213 wayPointEventCallback.callback(this, protocolHeader, 00214 wayPointEventCallback.userData); 00215 else 00216 API_LOG(serialDevice, STATUS_LOG, "WAYPOINT DATA"); 00217 break; 00218 default: 00219 API_LOG(serialDevice, STATUS_LOG, "Unknown BROADCAST command code\n"); 00220 break; 00221 } 00222 } 00223 else 00224 API_LOG(serialDevice, DEBUG_LOG, "Received unknown command\n"); 00225 if (recvCallback.callback) 00226 recvCallback.callback(this, protocolHeader, recvCallback.userData); 00227 } 00228 00229 void CoreAPI::setBroadcastCallback(CallBack userCallback, UserData userData) 00230 { 00231 broadcastCallback.callback = userCallback; 00232 broadcastCallback.userData = userData; 00233 } 00234 00235 void CoreAPI::setFromMobileCallback(CallBack userCallback, UserData userData) 00236 { 00237 fromMobileCallback.callback = userCallback; 00238 fromMobileCallback.userData = userData; 00239 }