00001
00012 #include "DJI_API.h"
00013 #include <string.h>
00014 #include <stdio.h>
00015
00016 using namespace DJI;
00017 using namespace DJI::onboardSDK;
00018
00019 #ifdef USE_ENCRYPT
00020 uint8_t DJI::onboardSDK::encrypt = 1;
00021 #else
00022 uint8_t DJI::onboardSDK::encrypt = 0;
00023 #endif // USE_ENCRYPT
00024
00025 CoreAPI::CoreAPI(HardDriver *sDevice, Version SDKVersion, bool userCallbackThread,
00026 CallBack userRecvCallback, UserData userData)
00027 {
00028 CallBackHandler handler;
00029 handler.callback = userRecvCallback;
00030 handler.userData = userData;
00031 init(sDevice, handler, userCallbackThread, SDKVersion);
00032 }
00033
00034 void CoreAPI::init(HardDriver *sDevice, CallBackHandler userRecvCallback,
00035 bool userCallbackThread, Version SDKVersion)
00036 {
00037 serialDevice = sDevice;
00038
00039
00040 seq_num = 0;
00041 ackFrameStatus = 11;
00042 broadcastFrameStatus = false;
00043
00044 filter.recvIndex = 0;
00045 filter.reuseCount = 0;
00046 filter.reuseIndex = 0;
00047 filter.encode = 0;
00048
00049 broadcastCallback.callback = 0;
00050 broadcastCallback.userData = 0;
00051 fromMobileCallback.callback = 0;
00052 fromMobileCallback.userData = 0;
00053 hotPointCallback.callback = 0;
00054 wayPointCallback.callback = 0;
00055 hotPointCallback.userData = 0;
00056 wayPointEventCallback.callback = 0;
00057 wayPointEventCallback.userData = 0;
00058 wayPointCallback.userData = 0;
00059 followCallback.callback = 0;
00060 followCallback.userData = 0;
00061 missionCallback.callback = 0;
00062 missionCallback.userData = 0;
00063
00064 recvCallback.callback = userRecvCallback.callback;
00065 recvCallback.userData = userRecvCallback.userData;
00066
00067 callbackThread = false;
00068 hotPointData = false;
00069 followData = false;
00070 wayPointData = false;
00071 callbackThread = userCallbackThread;
00072
00073 nonBlockingCBThreadEnable = false;
00074 ack_data = 99;
00075 versionData.version = SDKVersion;
00076
00078 memset((unsigned char *)&broadcastData, 0, sizeof(broadcastData));
00079
00080 setup();
00081 }
00082
00083 CoreAPI::CoreAPI(HardDriver *sDevice, Version SDKVersion, CallBackHandler userRecvCallback,
00084 bool userCallbackThread)
00085 {
00086 init(sDevice, userRecvCallback, userCallbackThread, SDKVersion);
00087 getSDKVersion();
00088 }
00089
00090 void CoreAPI::send(unsigned char session, unsigned char is_enc, CMD_SET cmdSet,
00091 unsigned char cmdID, void *pdata, int len, CallBack ackCallback, int timeout,
00092 int retry)
00093 {
00094 Command param;
00095 unsigned char *ptemp = (unsigned char *)encodeSendData;
00096 *ptemp++ = cmdSet;
00097 *ptemp++ = cmdID;
00098
00099 memcpy(encodeSendData + SET_CMD_SIZE, pdata, len);
00100
00101 param.handler = ackCallback;
00102 param.sessionMode = session;
00103 param.length = len + SET_CMD_SIZE;
00104 param.buf = encodeSendData;
00105 param.retry = retry;
00106
00107 param.timeout = timeout;
00108 param.encrypt = is_enc;
00109
00110 param.userData = 0;
00111
00112 sendInterface(¶m);
00113 }
00114
00115 void CoreAPI::send(unsigned char session_mode, bool is_enc, CMD_SET cmd_set,
00116 unsigned char cmd_id, void *pdata, size_t len, int timeout, int retry_time,
00117 CallBack ack_handler, UserData userData)
00118 {
00119 Command param;
00120 unsigned char *ptemp = (unsigned char *)encodeSendData;
00121 *ptemp++ = cmd_set;
00122 *ptemp++ = cmd_id;
00123
00124 memcpy(encodeSendData + SET_CMD_SIZE, pdata, len);
00125
00126 param.handler = ack_handler;
00127 param.sessionMode = session_mode;
00128 param.length = len + SET_CMD_SIZE;
00129 param.buf = encodeSendData;
00130 param.retry = retry_time;
00131
00132 param.timeout = timeout;
00133 param.encrypt = is_enc ? 1 : 0;
00134
00135 param.userData = userData;
00136
00137 sendInterface(¶m);
00138 }
00139
00140 void CoreAPI::send(Command *parameter) { sendInterface(parameter); }
00141
00142 void CoreAPI::ack(req_id_t req_id, unsigned char *ackdata, int len)
00143 {
00144 Ack param;
00145
00146 memcpy(encodeACK, ackdata, len);
00147
00148 param.sessionID = req_id.session_id;
00149 param.seqNum = req_id.sequence_number;
00150 param.encrypt = req_id.need_encrypt;
00151 param.buf = encodeACK;
00152 param.length = len;
00153
00154 this->ackInterface(¶m);
00155 }
00156
00157 void CoreAPI::getDroneVersion(CallBack callback, UserData userData)
00158 {
00159 versionData.version_ack = ACK_COMMON_NO_RESPONSE;
00160 versionData.version_crc = 0x0;
00161 versionData.version_name[0] = 0;
00162
00163 unsigned cmd_timeout = 100;
00164 unsigned retry_time = 3;
00165 unsigned char cmd_data = 0;
00166
00167 send(2, 0, SET_ACTIVATION, CODE_GETVERSION, (unsigned char *)&cmd_data, 1, cmd_timeout,
00168 retry_time, callback ? callback : CoreAPI::getDroneVersionCallback, userData);
00169 }
00170
00171 VersionData CoreAPI::getDroneVersion(int timeout)
00172 {
00173 versionData.version_ack = ACK_COMMON_NO_RESPONSE;
00174 versionData.version_crc = 0x0;
00175 versionData.version_name[0] = 0;
00176
00177 unsigned cmd_timeout = 100;
00178 unsigned retry_time = 3;
00179 unsigned char cmd_data = 0;
00180
00181 send(2, 0, SET_ACTIVATION, CODE_GETVERSION, (unsigned char *)&cmd_data, 1, cmd_timeout,
00182 retry_time, 0, 0);
00183
00184
00185 serialDevice->lockACK();
00186 serialDevice->wait(timeout);
00187 serialDevice->freeACK();
00188
00189
00190 unsigned char *ptemp = &(missionACKUnion.droneVersion.ack[0]);
00191
00192 if(versionData.version != versionA3_32){
00193 versionData.version_ack = ptemp[0] + (ptemp[1] << 8);
00194 ptemp += 2;
00195 versionData.version_crc = ptemp[0] + (ptemp[1] << 8) + (ptemp[2] << 16) + (ptemp[3] << 24);
00196 ptemp += 4;
00197 if (versionData.version != versionM100_23)
00198 {
00199 memcpy(versionData.version_ID, ptemp, 11);
00200 ptemp += 11;
00201 }
00202 memcpy(versionData.version_name, ptemp, 32);
00203 }else{
00204 versionData.version_ack = missionACKUnion.missionACK;
00205 memcpy(versionData.version_name, "versionA3_32", strlen("versionA3_32")+1);
00206 }
00207
00208 return versionData;
00209 }
00210
00211 void CoreAPI::activate(ActivateData *data, CallBack callback, UserData userData)
00212 {
00213 data->version = versionData.version;
00214 accountData = *data;
00215 accountData.reserved = 2;
00216
00217 for (int i = 0; i < 32; ++i) accountData.iosID[i] = '0';
00218 API_LOG(serialDevice, DEBUG_LOG, "version 0x%X/n", versionData.version);
00219 API_LOG(serialDevice, DEBUG_LOG, "%.32s", accountData.iosID);
00220 send(2, 0, SET_ACTIVATION, CODE_ACTIVATE, (unsigned char *)&accountData,
00221 sizeof(accountData) - sizeof(char *), 1000, 3,
00222 callback ? callback : CoreAPI::activateCallback, userData);
00223
00224 ack_data = missionACKUnion.simpleACK;
00225 if(ack_data == ACK_ACTIVE_SUCCESS && accountData.encKey)
00226 setKey(accountData.encKey);
00227 }
00228
00229 unsigned short CoreAPI::activate(ActivateData *data, int timeout)
00230 {
00231 data->version = versionData.version;
00232 accountData = *data;
00233 accountData.reserved = 2;
00234
00235 for (int i = 0; i < 32; ++i) accountData.iosID[i] = '0';
00236 API_LOG(serialDevice, DEBUG_LOG, "version 0x%X/n", versionData.version);
00237 API_LOG(serialDevice, DEBUG_LOG, "%.32s", accountData.iosID);
00238 send(2, 0, SET_ACTIVATION, CODE_ACTIVATE, (unsigned char *)&accountData,
00239 sizeof(accountData) - sizeof(char *), 1000, 3, 0, 0);
00240
00241
00242 serialDevice->lockACK();
00243 serialDevice->wait(timeout);
00244 serialDevice->freeACK();
00245 ack_data = missionACKUnion.simpleACK;
00246 if(ack_data == ACK_ACTIVE_SUCCESS && accountData.encKey)
00247 setKey(accountData.encKey);
00248
00249 return ack_data;
00250 }
00251
00252 void CoreAPI::sendToMobile(uint8_t *data, uint8_t len, CallBack callback, UserData userData)
00253 {
00254 if (len > 100)
00255 {
00256 API_LOG(serialDevice, ERROR_LOG, "Too much data to send");
00257 return;
00258 }
00259 send(0, 0, SET_ACTIVATION, CODE_TOMOBILE, data, len, 500, 1,
00260 callback ? callback : CoreAPI::sendToMobileCallback, userData);
00261 }
00262
00263 void CoreAPI::setBroadcastFreq(uint8_t *dataLenIs16, CallBack callback, UserData userData)
00264 {
00266 for (int i = 0; i < 16; ++i)
00267 {
00268 if (versionData.version == versionM100_31)
00269 if (i < 12)
00270 {
00271 dataLenIs16[i] = (dataLenIs16[i] > 5 ? 5 : dataLenIs16[i]);
00272 }
00273 else
00274 dataLenIs16[i] = 0;
00275 else
00276 {
00277 if (i < 14)
00278 {
00279 dataLenIs16[i] = (dataLenIs16[i] > 5 ? 5 : dataLenIs16[i]);
00280 }
00281 else
00282 dataLenIs16[i] = 0;
00283 }
00284 }
00285 send(2, 0, SET_ACTIVATION, CODE_FREQUENCY, dataLenIs16, 16, 100, 1,
00286 callback ? callback : CoreAPI::setFrequencyCallback, userData);
00287 }
00288
00289 unsigned short CoreAPI::setBroadcastFreq(uint8_t *dataLenIs16, int timeout)
00290 {
00292 for (int i = 0; i < 16; ++i)
00293 {
00294 if (versionData.version == versionM100_31)
00295 if (i < 12)
00296 {
00297 dataLenIs16[i] = (dataLenIs16[i] > 5 ? 5 : dataLenIs16[i]);
00298 }
00299 else
00300 dataLenIs16[i] = 0;
00301 else
00302 {
00303 if (i < 14)
00304 {
00305 dataLenIs16[i] = (dataLenIs16[i] > 5 ? 5 : dataLenIs16[i]);
00306 }
00307 else
00308 dataLenIs16[i] = 0;
00309 }
00310 }
00311 send(2, 0, SET_ACTIVATION, CODE_FREQUENCY, dataLenIs16, 16, 100, 1, 0, 0);
00312
00313
00314 serialDevice->lockACK();
00315 serialDevice->wait(timeout);
00316 serialDevice->freeACK();
00317 return missionACKUnion.simpleACK;
00318 }
00319
00320 void CoreAPI::setBroadcastFreqDefaults()
00321 {
00322 uint8_t freq[16];
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 if (versionData.version == versionM100_31 || versionData.version == versionM100_23) {
00358 freq[0] = BROADCAST_FREQ_1HZ;
00359 freq[1] = BROADCAST_FREQ_10HZ;
00360 freq[2] = BROADCAST_FREQ_50HZ;
00361 freq[3] = BROADCAST_FREQ_100HZ;
00362 freq[4] = BROADCAST_FREQ_50HZ;
00363 freq[5] = BROADCAST_FREQ_10HZ;
00364 freq[6] = BROADCAST_FREQ_1HZ;
00365 freq[7] = BROADCAST_FREQ_10HZ;
00366 freq[8] = BROADCAST_FREQ_50HZ;
00367 freq[9] = BROADCAST_FREQ_100HZ;
00368 freq[10] = BROADCAST_FREQ_50HZ;
00369 freq[11] = BROADCAST_FREQ_10HZ;
00370 }
00371 else if (versionData.version == versionA3_31 || versionData.version == versionA3_32) {
00372 freq[0] = BROADCAST_FREQ_1HZ;
00373 freq[1] = BROADCAST_FREQ_10HZ;
00374 freq[2] = BROADCAST_FREQ_50HZ;
00375 freq[3] = BROADCAST_FREQ_100HZ;
00376 freq[4] = BROADCAST_FREQ_50HZ;
00377 freq[5] = BROADCAST_FREQ_10HZ;
00378 freq[6] = BROADCAST_FREQ_0HZ;
00379 freq[7] = BROADCAST_FREQ_0HZ;
00380 freq[8] = BROADCAST_FREQ_1HZ;
00381 freq[9] = BROADCAST_FREQ_10HZ;
00382 freq[10] = BROADCAST_FREQ_50HZ;
00383 freq[11] = BROADCAST_FREQ_100HZ;
00384 freq[12] = BROADCAST_FREQ_50HZ;
00385 freq[13] = BROADCAST_FREQ_10HZ;
00386 }
00387 setBroadcastFreq(freq);
00388 }
00389
00390 void CoreAPI::setBroadcastFreqToZero()
00391 {
00392 uint8_t freq[16];
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427 freq[0] = BROADCAST_FREQ_0HZ;
00428 freq[1] = BROADCAST_FREQ_0HZ;
00429 freq[2] = BROADCAST_FREQ_0HZ;
00430 freq[3] = BROADCAST_FREQ_0HZ;
00431 freq[4] = BROADCAST_FREQ_0HZ;
00432 freq[5] = BROADCAST_FREQ_0HZ;
00433 freq[6] = BROADCAST_FREQ_0HZ;
00434 freq[7] = BROADCAST_FREQ_0HZ;
00435 freq[8] = BROADCAST_FREQ_0HZ;
00436 freq[9] = BROADCAST_FREQ_0HZ;
00437 freq[10] = BROADCAST_FREQ_0HZ;
00438 freq[11] = BROADCAST_FREQ_0HZ;
00439 if(versionData.version == versionA3_31 || versionData.version == versionA3_32)
00440 {
00441 freq[12] = BROADCAST_FREQ_0HZ;
00442 freq[13] = BROADCAST_FREQ_0HZ;
00443 }
00444 setBroadcastFreq(freq);
00445 }
00446
00447
00448 unsigned short CoreAPI::setBroadcastFreqDefaults(int timeout)
00449 {
00450 uint8_t freq[16];
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485 if (versionData.version == versionM100_31 || versionData.version == versionM100_23) {
00486 freq[0] = BROADCAST_FREQ_1HZ;
00487 freq[1] = BROADCAST_FREQ_10HZ;
00488 freq[2] = BROADCAST_FREQ_50HZ;
00489 freq[3] = BROADCAST_FREQ_100HZ;
00490 freq[4] = BROADCAST_FREQ_50HZ;
00491 freq[5] = BROADCAST_FREQ_10HZ;
00492 freq[6] = BROADCAST_FREQ_1HZ;
00493 freq[7] = BROADCAST_FREQ_10HZ;
00494 freq[8] = BROADCAST_FREQ_50HZ;
00495 freq[9] = BROADCAST_FREQ_100HZ;
00496 freq[10] = BROADCAST_FREQ_50HZ;
00497 freq[11] = BROADCAST_FREQ_10HZ;
00498 }
00499 else if (versionData.version == versionA3_31 || versionData.version == versionA3_32) {
00500 freq[0] = BROADCAST_FREQ_1HZ;
00501 freq[1] = BROADCAST_FREQ_10HZ;
00502 freq[2] = BROADCAST_FREQ_50HZ;
00503 freq[3] = BROADCAST_FREQ_100HZ;
00504 freq[4] = BROADCAST_FREQ_50HZ;
00505 freq[5] = BROADCAST_FREQ_10HZ;
00506 freq[6] = BROADCAST_FREQ_0HZ;
00507 freq[7] = BROADCAST_FREQ_0HZ;
00508 freq[8] = BROADCAST_FREQ_1HZ;
00509 freq[9] = BROADCAST_FREQ_10HZ;
00510 freq[10] = BROADCAST_FREQ_50HZ;
00511 freq[11] = BROADCAST_FREQ_100HZ;
00512 freq[12] = BROADCAST_FREQ_50HZ;
00513 freq[13] = BROADCAST_FREQ_10HZ;
00514 }
00515
00516 return setBroadcastFreq(freq, timeout);
00517 }
00518
00519 TimeStampData CoreAPI::getTime() const { return broadcastData.timeStamp; }
00520
00521 FlightStatus CoreAPI::getFlightStatus() const { return broadcastData.status; }
00522
00523 void CoreAPI::setFromMobileCallback(CallBackHandler FromMobileEntrance)
00524 {
00525 fromMobileCallback = FromMobileEntrance;
00526 }
00527
00528
00529 ActivateData CoreAPI::getAccountData() const { return accountData; }
00530
00531 void CoreAPI::setAccountData(const ActivateData &value) { accountData = value; }
00532 void CoreAPI::setHotPointData(bool value) { hotPointData = value; }
00533 void CoreAPI::setWayPointData(bool value) { wayPointData = value; }
00534 void CoreAPI::setFollowData(bool value) { followData = value; }
00535 bool CoreAPI::getHotPointData() const { return hotPointData; }
00536 bool CoreAPI::getWayPointData() const { return wayPointData; }
00537 bool CoreAPI::getFollowData() const { return followData; }
00538
00539 void CoreAPI::setControl(bool enable, CallBack callback, UserData userData)
00540 {
00541 unsigned char data = enable ? 1 : 0;
00542 send(2, DJI::onboardSDK::encrypt, SET_CONTROL, CODE_SETCONTROL, &data, 1, 500, 2,
00543 callback ? callback : CoreAPI::setControlCallback, userData);
00544 }
00545
00546 unsigned short CoreAPI::setControl(bool enable, int timeout)
00547 {
00548 unsigned char data = enable ? 1 : 0;
00549 send(2, DJI::onboardSDK::encrypt, SET_CONTROL, CODE_SETCONTROL, &data, 1, 500, 2, 0, 0);
00550
00551
00552 serialDevice->lockACK();
00553 serialDevice->wait(timeout);
00554 serialDevice->freeACK();
00555
00556 if (missionACKUnion.simpleACK == ACK_SETCONTROL_ERROR_MODE)
00557 {
00558 if(versionData.version != versionA3_32)
00559 missionACKUnion.simpleACK = ACK_SETCONTROL_NEED_MODE_F;
00560 else
00561 missionACKUnion.simpleACK = ACK_SETCONTROL_NEED_MODE_P;
00562 }
00563
00564 return missionACKUnion.simpleACK;
00565 }
00566
00567 HardDriver *CoreAPI::getDriver() const { return serialDevice; }
00568
00569 SimpleACK CoreAPI::getSimpleACK () const { return missionACKUnion.simpleACK; }
00570
00571 void CoreAPI::setDriver(HardDriver *sDevice) { serialDevice = sDevice; }
00572
00573 void CoreAPI::getDroneVersionCallback(CoreAPI *api, Header *protocolHeader, UserData userData __UNUSED)
00574 {
00575 unsigned char *ptemp = ((unsigned char *)protocolHeader) + sizeof(Header);
00576 size_t ack_length = protocolHeader->length - EXC_DATA_SIZE;
00577
00578 if(ack_length > 1){
00579 api->versionData.version_ack = ptemp[0] + (ptemp[1] << 8);
00580 ptemp += 2;
00581 api->versionData.version_crc =
00582 ptemp[0] + (ptemp[1] << 8) + (ptemp[2] << 16) + (ptemp[3] << 24);
00583 ptemp += 4;
00584 if (api->versionData.version != versionM100_23)
00585 {
00586 memcpy(api->versionData.version_ID, ptemp, 11);
00587 ptemp += 11;
00588 }
00589 memcpy(api->versionData.version_name, ptemp, 32);
00590 }else{
00591 api->versionData.version_ack = ptemp[0];
00592 memcpy(api->versionData.version_name, "versionA3_32", strlen("versionA3_32")+1);
00593 }
00594
00595 API_LOG(api->serialDevice, STATUS_LOG, "version ack = %d\n", api->versionData.version_ack);
00596
00597 if(api->versionData.version != versionA3_32){
00598 API_LOG(api->serialDevice, STATUS_LOG, "version crc = 0x%X\n", api->versionData.version_crc);
00599 if (api->versionData.version != versionM100_23)
00600 API_LOG(api->serialDevice, STATUS_LOG, "version ID = %.11s\n", api->versionData.version_ID);
00601 }
00602
00603 API_LOG(api->serialDevice, STATUS_LOG, "version name = %.32s\n",
00604 api->versionData.version_name);
00605 }
00606
00607 void CoreAPI::activateCallback(CoreAPI *api, Header *protocolHeader, UserData userData __UNUSED)
00608 {
00609
00610 unsigned short ack_data;
00611 if (protocolHeader->length - EXC_DATA_SIZE <= 2)
00612 {
00613 memcpy((unsigned char *)&ack_data, ((unsigned char *)protocolHeader) + sizeof(Header),
00614 (protocolHeader->length - EXC_DATA_SIZE));
00615 switch (ack_data)
00616 {
00617 case ACK_ACTIVE_SUCCESS:
00618 API_LOG(api->serialDevice, STATUS_LOG, "Activated successfully\n");
00619
00620 if (api->accountData.encKey)
00621 api->setKey(api->accountData.encKey);
00622 return;
00623 case ACK_ACTIVE_NEW_DEVICE:
00624 API_LOG(api->serialDevice, STATUS_LOG, "New device, please link DJIGO to your "
00625 "remote controller and try again\n");
00626 break;
00627 case ACK_ACTIVE_PARAMETER_ERROR:
00628 API_LOG(api->serialDevice, ERROR_LOG, "Wrong parameter\n");
00629 break;
00630 case ACK_ACTIVE_ENCODE_ERROR:
00631 API_LOG(api->serialDevice, ERROR_LOG, "Encode error\n");
00632 break;
00633 case ACK_ACTIVE_APP_NOT_CONNECTED:
00634 API_LOG(api->serialDevice, ERROR_LOG, "DJIGO not connected\n");
00635 break;
00636 case ACK_ACTIVE_NO_INTERNET:
00637 API_LOG(api->serialDevice, ERROR_LOG, "DJIGO not "
00638 "connected to the internet\n");
00639 break;
00640 case ACK_ACTIVE_SERVER_REFUSED:
00641 API_LOG(api->serialDevice, ERROR_LOG, "DJI server rejected "
00642 "your request, please use your SDK ID\n");
00643 break;
00644 case ACK_ACTIVE_ACCESS_LEVEL_ERROR:
00645 API_LOG(api->serialDevice, ERROR_LOG, "Wrong SDK permission\n");
00646 break;
00647 case ACK_ACTIVE_VERSION_ERROR:
00648 API_LOG(api->serialDevice, ERROR_LOG, "SDK version did not match\n");
00649 api->getDroneVersion();
00650 break;
00651 default:
00652 if (!api->decodeACKStatus(ack_data))
00653 {
00654 API_LOG(api->serialDevice, ERROR_LOG, "While calling this function");
00655 }
00656 break;
00657 }
00658 }
00659 else
00660 {
00661 API_LOG(api->serialDevice, ERROR_LOG, "ACK is exception, session id %d,sequence %d\n",
00662 protocolHeader->sessionID, protocolHeader->sequenceNumber);
00663 }
00664 }
00665
00666 void CoreAPI::sendToMobileCallback(CoreAPI *api, Header *protocolHeader, UserData userData __UNUSED)
00667 {
00668 unsigned short ack_data = ACK_COMMON_NO_RESPONSE;
00669 if (protocolHeader->length - EXC_DATA_SIZE <= 2)
00670 {
00671 memcpy((unsigned char *)&ack_data, ((unsigned char *)protocolHeader) + sizeof(Header),
00672 (protocolHeader->length - EXC_DATA_SIZE));
00673 if (!api->decodeACKStatus(ack_data))
00674 {
00675 API_LOG(api->serialDevice, ERROR_LOG, "While calling this function");
00676 }
00677 }
00678 else
00679 {
00680 API_LOG(api->serialDevice, ERROR_LOG, "ACK is exception, session id %d,sequence %d\n",
00681 protocolHeader->sessionID, protocolHeader->sequenceNumber);
00682 }
00683 }
00684
00686 void CoreAPI::parseFromMobileCallback(CoreAPI *api, Header *protocolHeader, UserData userData __UNUSED)
00687 {
00688 uint16_t mobile_data_id;
00689
00690 if (protocolHeader->length - EXC_DATA_SIZE <= 4)
00691 {
00692 mobile_data_id = *((unsigned char*)protocolHeader + sizeof(Header) + 2);
00693
00694 switch (mobile_data_id)
00695 {
00696 case 2:
00697 if (obtainControlMobileCallback.callback)
00698 {
00699 obtainControlMobileCallback.callback(api, protocolHeader, obtainControlMobileCallback.userData);
00700 }
00701 else
00702 {
00703 obtainControlMobileCMD = true;
00704 }
00705 break;
00706
00707 case 3:
00708 if (releaseControlMobileCallback.callback)
00709 {
00710 releaseControlMobileCallback.callback(api, protocolHeader, releaseControlMobileCallback.userData);
00711 }
00712 else
00713 {
00714 releaseControlMobileCMD = true;
00715 }
00716 break;
00717
00718 case 4:
00719 if (activateMobileCallback.callback)
00720 {
00721 activateMobileCallback.callback(api, protocolHeader, activateMobileCallback.userData);
00722 }
00723 else
00724 {
00725 activateMobileCMD = true;
00726 }
00727 break;
00728
00729 case 5:
00730 if (armMobileCallback.callback)
00731 {
00732 armMobileCallback.callback(api, protocolHeader, armMobileCallback.userData);
00733 }
00734 else
00735 {
00736 armMobileCMD = true;
00737 }
00738 break;
00739
00740 case 6:
00741 if (disArmMobileCallback.callback)
00742 {
00743 disArmMobileCallback.callback(api, protocolHeader, disArmMobileCallback.userData);
00744 }
00745 else
00746 {
00747 disArmMobileCMD = true;
00748 }
00749 break;
00750
00751 case 7:
00752 if (takeOffMobileCallback.callback)
00753 {
00754 takeOffMobileCallback.callback(api, protocolHeader, takeOffMobileCallback.userData);
00755 }
00756 else
00757 {
00758 takeOffMobileCMD = true;
00759 }
00760 break;
00761
00762 case 8:
00763 if (landingMobileCallback.callback)
00764 {
00765 landingMobileCallback.callback(api, protocolHeader, landingMobileCallback.userData);
00766 }
00767 else
00768 {
00769 landingMobileCMD = true;
00770 }
00771 break;
00772
00773 case 9:
00774 if (goHomeMobileCallback.callback)
00775 {
00776 goHomeMobileCallback.callback(api, protocolHeader, goHomeMobileCallback.userData);
00777 }
00778 else
00779 {
00780 goHomeMobileCMD = true;
00781 }
00782 break;
00783
00784 case 10:
00785 if (takePhotoMobileCallback.callback)
00786 {
00787 takePhotoMobileCallback.callback(api, protocolHeader, takePhotoMobileCallback.userData);
00788 }
00789 else
00790 {
00791 takePhotoMobileCMD = true;
00792 }
00793 break;
00794
00795 case 11:
00796 if (startVideoMobileCallback.callback)
00797 {
00798 startVideoMobileCallback.callback(api, protocolHeader, startVideoMobileCallback.userData);
00799 }
00800 else
00801 {
00802 startVideoMobileCMD = true;
00803 }
00804 break;
00805
00806 case 13:
00807 if (stopVideoMobileCallback.callback)
00808 {
00809 stopVideoMobileCallback.callback(api, protocolHeader, stopVideoMobileCallback.userData);
00810 }
00811 else
00812 {
00813 stopVideoMobileCMD = true;
00814 }
00815 break;
00817 case 61:
00818 drawCirMobileCMD = true;
00819 break;
00820 case 62:
00821 drawSqrMobileCMD = true;
00822 break;
00823 case 63:
00824 attiCtrlMobileCMD = true;
00825 break;
00826 case 64:
00827 gimbalCtrlMobileCMD = true;
00828 break;
00829 case 65:
00830 wayPointTestMobileCMD = true;
00831 break;
00832 case 66:
00833 localNavTestMobileCMD = true;
00834 break;
00835 case 67:
00836 globalNavTestMobileCMD = true;
00837 break;
00838 case 68:
00839 VRCTestMobileCMD = true;
00840 break;
00841 case 69:
00842 localMissionPlanCMD = true;
00843 break;
00844 }
00845 }
00846 }
00847
00848 void CoreAPI::setFrequencyCallback(CoreAPI *api __UNUSED, Header *protocolHeader,
00849 UserData userData __UNUSED)
00850 {
00851 unsigned short ack_data = ACK_COMMON_NO_RESPONSE;
00852
00853 if (protocolHeader->length - EXC_DATA_SIZE <= 2)
00854 {
00855 memcpy((unsigned char *)&ack_data, ((unsigned char *)protocolHeader) + sizeof(Header),
00856 (protocolHeader->length - EXC_DATA_SIZE));
00857 }
00858 switch (ack_data)
00859 {
00860 case 0x0000:
00861 API_LOG(api->serialDevice, STATUS_LOG, "Frequency set successfully\n");
00862 break;
00863 case 0x0001:
00864 API_LOG(api->serialDevice, ERROR_LOG, "Frequency parameter error\n");
00865 break;
00866 default:
00867 if (!api->decodeACKStatus(ack_data))
00868 {
00869 API_LOG(api->serialDevice, ERROR_LOG, "While calling this function\n");
00870 }
00871 break;
00872 }
00873 }
00874
00875 Version CoreAPI::getSDKVersion() const { return versionData.version; }
00876
00877 SDKFilter CoreAPI::getFilter() const { return filter; }
00878
00879 void CoreAPI::setVersion(const Version &value) { versionData.version = value; }
00880
00881 void CoreAPI::setControlCallback(CoreAPI *api, Header *protocolHeader, UserData userData __UNUSED)
00882 {
00883 unsigned short ack_data = ACK_COMMON_NO_RESPONSE;
00884 unsigned char data = 0x1;
00885
00886 if (protocolHeader->length - EXC_DATA_SIZE <= sizeof(ack_data))
00887 {
00888 memcpy((unsigned char *)&ack_data, ((unsigned char *)protocolHeader) + sizeof(Header),
00889 (protocolHeader->length - EXC_DATA_SIZE));
00890 }
00891 else
00892 {
00893 API_LOG(api->serialDevice, ERROR_LOG, "ACK is exception, session id %d,sequence %d\n",
00894 protocolHeader->sessionID, protocolHeader->sequenceNumber);
00895 }
00896
00897 switch (ack_data)
00898 {
00899 case ACK_SETCONTROL_ERROR_MODE:
00900 if(api->versionData.version != versionA3_32)
00901 {
00902 API_LOG(api->serialDevice, STATUS_LOG, "Obtain control failed: switch to F mode\n");
00903 }
00904 else
00905 {
00906 API_LOG(api->serialDevice, STATUS_LOG, "Obtain control failed: switch to P mode\n");
00907 }
00908 break;
00909 case ACK_SETCONTROL_RELEASE_SUCCESS:
00910 API_LOG(api->serialDevice, STATUS_LOG, "Released control successfully\n");
00911 break;
00912 case ACK_SETCONTROL_OBTAIN_SUCCESS:
00913 API_LOG(api->serialDevice, STATUS_LOG, "Obtained control successfully\n");
00914 break;
00915 case ACK_SETCONTROL_OBTAIN_RUNNING:
00916 API_LOG(api->serialDevice, STATUS_LOG, "Obtain control running\n");
00917 api->send(2, DJI::onboardSDK::encrypt, SET_CONTROL, CODE_SETCONTROL, &data, 1, 500,
00918 2, CoreAPI::setControlCallback);
00919 break;
00920 case ACK_SETCONTROL_RELEASE_RUNNING:
00921 API_LOG(api->serialDevice, STATUS_LOG, "Release control running\n");
00922 data = 0;
00923 api->send(2, DJI::onboardSDK::encrypt, SET_CONTROL, CODE_SETCONTROL, &data, 1, 500,
00924 2, CoreAPI::setControlCallback);
00925 break;
00926 case ACK_SETCONTROL_IOC:
00927 API_LOG(api->serialDevice, STATUS_LOG, "IOC mode opening can not obtain control\n");
00928 break;
00929 default:
00930 if (!api->decodeACKStatus(ack_data))
00931 {
00932 API_LOG(api->serialDevice, ERROR_LOG, "While calling this function");
00933 }
00934 break;
00935 }
00936 }