otam_protocol.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. #include "bcomdef.h"
  29. #include "OSAL.h"
  30. #include "OSAL_PwrMgr.h"
  31. #include "OSAL_bufmgr.h"
  32. #include "gatt.h"
  33. #include "ll.h"
  34. #include "ll_common.h"
  35. #include "hci.h"
  36. #include "gapgattserver.h"
  37. #include "gattservapp.h"
  38. #include "central.h"
  39. #include "gapbondmgr.h"
  40. #include "simpleGATTprofile_ota.h"
  41. #include "ota_mesh_master.h"
  42. #include "timer.h"
  43. #include "log.h"
  44. #include "ll_def.h"
  45. #include "global_config.h"
  46. #include "flash.h"
  47. #include "rflib.h"
  48. #include "otam_cmd.h"
  49. #include "stdio.h"
  50. #include "stdlib.h"
  51. #include "string.h"
  52. #include "otam_protocol.h"
  53. #include "ota_mesh.h"
  54. #include "ota_app_service.h"
  55. #include "ota_protocol.h"
  56. #include "ota_flash.h"
  57. #include "ota_flash_mesh.h"
  58. //#include "otam_1clk_ota.h"
  59. #include "crc16.h"
  60. #include "error.h"
  61. #define OTAF_BASE_ADDR 0x11000000
  62. #define OTAF_END_ADDR 0x1107ffff
  63. enum{
  64. OTAM_ST_DISCONNECT = 0,
  65. OTAM_ST_CONNECTED, //connected, idle
  66. OTAM_ST_WAIT_STARTED,
  67. OTAM_ST_PARAM,
  68. OTAM_ST_WAIT_PARTITION_INFO,
  69. OTAM_ST_DATA,
  70. OTAM_ST_COMPLETE,
  71. OTAM_ST_ERROR,
  72. OTAM_ST_CANCELING, //wait stop
  73. };
  74. typedef struct{
  75. ota_fw_t fw;
  76. //data cache for a block transmit
  77. uint32_t cache_size;
  78. uint32_t cache_offset;
  79. uint8_t cache_retry;
  80. uint8_t* cache;
  81. }otam_fw_t;
  82. typedef struct{
  83. uint8_t state;
  84. uint8_t run_mode;
  85. bool reset_mode;
  86. uint16_t mtu_a;
  87. uint16_t burst_size;
  88. uint8_t opcode;
  89. otam_fw_t fw;
  90. otam_proto_meth_t method;
  91. }otam_proto_ctx_t;
  92. static otam_proto_ctx_t s_otap_ctx_t;
  93. static void print_hex (const uint8 *data, uint16 len)
  94. {
  95. //return;
  96. #if(DEBUG_INFO > 1)
  97. uint16 i;
  98. char strdata[5];
  99. for (i = 0; i < len - 1; i++)
  100. {
  101. //if(i %16 == 0 && i >0)
  102. // LOG("\n");
  103. sprintf(strdata, "%.2x", data[i]);
  104. AT_LOG("%s ",strdata);
  105. }
  106. sprintf(strdata, "%.2x", data[i]);
  107. AT_LOG("%s\n",strdata);
  108. #endif
  109. }
  110. static int otam_proto_ctx_reset(uint8_t st)
  111. {
  112. otam_proto_ctx_t *pctx = &s_otap_ctx_t;
  113. otam_proto_meth_t *method = &(pctx->method);
  114. pctx->state = st;
  115. pctx->opcode = 0;
  116. memset(&(pctx->fw), 0, sizeof(otam_fw_t));
  117. method->clear();
  118. return PPlus_SUCCESS;
  119. }
  120. static void otam_proto_disconnect(void* param)
  121. {
  122. otam_proto_ctx_reset(OTAM_ST_DISCONNECT);
  123. }
  124. static void otam_proto_connect(void* param)
  125. {
  126. otam_proto_ctx_t *pctx = &s_otap_ctx_t;
  127. otam_proto_conn_param_t* pconn = (otam_proto_conn_param_t*)param;
  128. pctx->state = OTAM_ST_CONNECTED;
  129. pctx->mtu_a = pconn->mtu -3;
  130. pctx->opcode = 0xff;
  131. if(pconn->run_mode == OTAC_RUNMODE_APP){
  132. pctx->run_mode = OTAC_RUNMODE_APP;
  133. pctx->reset_mode = 0;
  134. }
  135. else if(pconn->run_mode == OTAC_RUNMODE_OTA){
  136. pctx->run_mode = OTAC_RUNMODE_OTA;
  137. if(pctx->reset_mode == OTAC_RUNMODE_OTARES)
  138. pctx->run_mode = OTAC_RUNMODE_OTARES;
  139. pctx->reset_mode = 0;
  140. }
  141. else
  142. {
  143. pctx->run_mode = 0;
  144. pctx->reset_mode = 0;
  145. }
  146. }
  147. static int send_patition_info(void)
  148. {
  149. otam_proto_ctx_t *pctx = &s_otap_ctx_t;
  150. otam_fw_t *pfw = &(pctx->fw);
  151. otam_proto_meth_t *method = &(pctx->method);
  152. uint8_t data[20];
  153. if(pctx->state != OTAM_ST_WAIT_STARTED && pctx->state != OTAM_ST_DATA){
  154. return PPlus_ERR_INVALID_STATE;
  155. }
  156. //partition cmd: 02 ID FA FA FA FA RA RA RA RA SZ SZ SZ SZ CS CS
  157. // ID: index
  158. // FA: flash address
  159. // RA: run address
  160. // SZ: partition size
  161. // CS: checksum
  162. uint32_t val;
  163. uint16_t offset = 0;
  164. uint32_t flash_addr = 0;
  165. uint32_t run_addr = pfw->fw.part[pfw->fw.part_current].run_addr;
  166. if(run_addr > OTAF_BASE_ADDR && run_addr < OTAF_END_ADDR )
  167. {
  168. flash_addr = run_addr;
  169. }
  170. else
  171. {
  172. //calculate store address in flash
  173. for(int i = 0; i<pfw->fw.part_current; i++ ){
  174. if(pfw->fw.part[i].run_addr > OTAF_BASE_ADDR && pfw->fw.part[i].run_addr < OTAF_END_ADDR)
  175. continue;
  176. val = pfw->fw.part[i].size +3;
  177. val = val - (val%4);
  178. flash_addr += val;
  179. }
  180. }
  181. data[offset ++] = OTA_CMD_PARTITION_INFO;
  182. data[offset ++] = pfw->fw.part_current;
  183. val = flash_addr;
  184. data[offset ++] = (uint8_t)(val&0xff);
  185. data[offset ++] = (uint8_t)((val>>8)&0xff);
  186. data[offset ++] = (uint8_t)((val>>16)&0xff);
  187. data[offset ++] = (uint8_t)((val>>24)&0xff);
  188. val = run_addr;
  189. data[offset ++] = (uint8_t)(val&0xff);
  190. data[offset ++] = (uint8_t)((val>>8)&0xff);
  191. data[offset ++] = (uint8_t)((val>>16)&0xff);
  192. data[offset ++] = (uint8_t)((val>>24)&0xff);
  193. val = pfw->fw.part[pfw->fw.part_current].size;
  194. data[offset ++] = (uint8_t)(val&0xff);
  195. data[offset ++] = (uint8_t)((val>>8)&0xff);
  196. data[offset ++] = (uint8_t)((val>>16)&0xff);
  197. data[offset ++] = (uint8_t)((val>>24)&0xff);
  198. val = (uint32_t)(pfw->fw.part[pfw->fw.part_current].checksum);
  199. data[offset ++] = (uint8_t)(val&0xff);
  200. data[offset ++] = (uint8_t)((val>>8)&0xff);
  201. pctx->state = OTAM_ST_WAIT_PARTITION_INFO;
  202. pfw->cache_offset = 0;
  203. pfw->cache_size = 0;
  204. pfw->cache_retry = 0;
  205. pfw->fw.offset = 0;
  206. return method->write_cmd(data, offset, 1000);
  207. }
  208. static int load_data_cache(void)
  209. {
  210. otam_proto_ctx_t *pctx = &s_otap_ctx_t;
  211. otam_fw_t *pfw = &(pctx->fw);
  212. ota_fw_part_t* ppart = &(pfw->fw.part[pfw->fw.part_current]);
  213. uint16_t mtu_a = pctx->mtu_a;
  214. uint32_t size = mtu_a * pctx->burst_size;
  215. if(pfw->fw.offset + size > ppart->size){
  216. size = ppart->size - pfw->fw.offset;
  217. }
  218. //memset(pfw->cache, 0, (ATT_MTU_SIZE-3));
  219. pfw->cache = (uint8_t*)(ppart->flash_addr + pfw->fw.offset+OTAFM_FW_OTA_DATA_ADDR);
  220. pfw->cache_size = size;
  221. pfw->cache_offset = 0;
  222. pfw->cache_retry = 0;
  223. pfw->fw.offset += size;
  224. return PPlus_SUCCESS;
  225. }
  226. static int send_data(void)
  227. {
  228. otam_proto_ctx_t *pctx = &s_otap_ctx_t;
  229. otam_fw_t *pfw = &(pctx->fw);
  230. otam_proto_meth_t *method = &(pctx->method);
  231. if(!(method->write_data))
  232. return PPlus_ERR_NOT_REGISTED;
  233. if(pctx->state != OTAM_ST_DATA)
  234. return PPlus_ERR_INVALID_STATE;
  235. int ret = PPlus_SUCCESS;
  236. uint16_t size = 0;
  237. uint16_t mtu_a = pctx->mtu_a;
  238. while(pfw->cache_size - pfw->cache_offset){
  239. size = mtu_a;
  240. if((pfw->cache_size - pfw->cache_offset) < mtu_a)
  241. size = pfw->cache_size - pfw->cache_offset;
  242. ret = method->write_data(pfw->cache + pfw->cache_offset, size);
  243. if(ret != PPlus_SUCCESS){
  244. method->write_data_delay(2);
  245. return PPlus_SUCCESS;
  246. }
  247. pfw->cache_offset += size;
  248. }
  249. return PPlus_SUCCESS;
  250. }
  251. void print_version(uint8_t* vinfo)
  252. {
  253. }
  254. static void handle_app_notify_event(void* param, uint8_t len)
  255. {
  256. otam_proto_ctx_t *pctx = &s_otap_ctx_t;
  257. switch(pctx->opcode)
  258. {
  259. case OTAAPP_CMD_START_OTA:
  260. break;
  261. case OTAAPP_CMD_VER:
  262. print_version(param);
  263. break;
  264. default:
  265. break;
  266. }
  267. }
  268. static void handle_ota_notify_event(void* param, uint8_t len)
  269. {
  270. otam_proto_ctx_t *pctx = &s_otap_ctx_t;
  271. otam_fw_t* pfw = &(pctx->fw);
  272. uint8_t* pnotify = (uint8_t*)param;
  273. otam_proto_meth_t* method = &(pctx->method);
  274. int retval = pnotify[0];
  275. if(len == 1){ //fatal error
  276. otam_proto_ctx_reset(OTAM_ST_CONNECTED);
  277. return;
  278. }
  279. LOG("OTA Notif %x, %x\n",pnotify[0],pnotify[1]);
  280. print_hex(pnotify, len);
  281. switch(pnotify[1]) //response type
  282. {
  283. case OTA_RSP_START_OTA:
  284. {
  285. if(retval == PPlus_SUCCESS && pctx->state == OTAM_ST_WAIT_STARTED){
  286. send_patition_info();
  287. }
  288. else
  289. {
  290. pctx->state = OTAM_ST_ERROR;
  291. }
  292. break;
  293. }
  294. case OTA_RSP_PARAM:
  295. case OTA_RSP_BLOCK_INFO:
  296. case OTA_RSP_BLOCK_COMPLETE:
  297. {
  298. pctx->state = OTAM_ST_ERROR;
  299. break;
  300. }
  301. case OTA_RSP_OTA_COMPLETE:
  302. {
  303. uint8_t data[20];
  304. if(method->write_cmd){
  305. data[0] = OTA_CMD_REBOOT;
  306. data[1] = 1;
  307. pctx->state = OTAM_ST_COMPLETE;
  308. LOG("OTA completed!!\n");
  309. method->write_cmd(data, 2, 1000);
  310. }
  311. break;
  312. }
  313. case OTA_RSP_PARTITION_INFO:
  314. {
  315. pctx->state = OTAM_ST_DATA;
  316. if(pfw->fw.part_current)
  317. {
  318. LOG(" ");
  319. }
  320. load_data_cache();
  321. send_data();
  322. break;
  323. }
  324. case OTA_RSP_PARTITION_COMPLETE:
  325. {
  326. pfw->fw.total_offset += pfw->cache_size;
  327. pfw->fw.part_current ++;
  328. send_patition_info();
  329. break;
  330. }
  331. case OTA_RSP_BLOCK_BURST:
  332. {
  333. if(pctx->state != OTAM_ST_DATA)
  334. {
  335. pctx->state = OTAM_ST_ERROR;
  336. break;
  337. }
  338. if(retval == PPlus_SUCCESS)
  339. {
  340. load_data_cache();
  341. send_data();
  342. }
  343. else if(retval == PPlus_ERR_OTA_BAD_DATA){
  344. //case block data is not completed, retry block data
  345. pfw->cache_retry++;
  346. pfw->cache_offset = 0;
  347. if(pfw->cache_retry > 3){
  348. pctx->state = OTAM_ST_ERROR;
  349. break;
  350. }
  351. send_data();
  352. }
  353. else
  354. {
  355. pctx->state = OTAM_ST_ERROR;
  356. }
  357. break;
  358. }
  359. case OTA_RSP_REBOOT:
  360. {
  361. LOG("[OTA_RSP_REBOOT]GAPCentralRole_TerminateLink\n");
  362. GAPCentralRole_TerminateLink(0);
  363. }
  364. case OTA_RSP_ERASE:
  365. case OTA_RSP_ERROR:
  366. default:
  367. {
  368. pctx->state = OTAM_ST_ERROR;
  369. break;
  370. }
  371. }
  372. }
  373. void otamProtocol_event(otap_evt_t* pev)
  374. {
  375. otam_proto_ctx_t *pctx = &s_otap_ctx_t;
  376. switch(pev->ev){
  377. case OTAP_EVT_DISCONNECTED:
  378. otam_proto_disconnect(pev->data);
  379. break;
  380. case OTAP_EVT_CONNECTED:
  381. otam_proto_connect(pev->data);
  382. break;
  383. case OTAP_EVT_NOTIFY:
  384. {
  385. if(pctx->run_mode == OTAC_RUNMODE_APP)
  386. handle_app_notify_event(pev->data, pev->len);
  387. else
  388. handle_ota_notify_event(pev->data, pev->len);
  389. break;
  390. }
  391. case OTAP_EVT_DATA_WR_DELAY:
  392. {
  393. send_data();
  394. break;
  395. }
  396. case OTAP_EVT_BLE_TIMEOUT:
  397. default:
  398. {
  399. //pctx->state = OTAM_ST_ERROR;
  400. break;
  401. }
  402. }
  403. }
  404. #define OTAM_FW_DATA_ADDR 0x11040000
  405. #define OTAM_FW_DATA_ADDR1 0x11060000
  406. /*
  407. word | desc:
  408. 0 | flag: "OTAF"
  409. 1 | partition number
  410. i*2 + 2 | run address
  411. i*2 + 3 | size
  412. N*2 +2 | data area
  413. */
  414. int load_fw(uint8_t fw_id)
  415. {
  416. otam_proto_ctx_t *pctx = &s_otap_ctx_t;
  417. otam_fw_t *pfw = &(pctx->fw);
  418. uint32_t faddr = fw_id == 0 ? OTAM_FW_DATA_ADDR : OTAM_FW_DATA_ADDR1;
  419. uint8_t* pdata = (uint8_t*) (faddr);
  420. uint32_t* pdata32 = (uint32_t*) pdata;
  421. uint32_t offset = 0;
  422. LOG("load fw %x\n", faddr);
  423. memset((void*)pfw, 0 ,sizeof(otam_fw_t));
  424. if(!((char)(pdata[0]) == 'O' && (char)(pdata[1]) == 'T' &&(char)(pdata[2]) == 'A'&&(char)(pdata[3]) == 'F'))
  425. {
  426. return PPlus_ERR_INVALID_DATA;
  427. }
  428. pfw->fw.part_num = (uint8_t)(pdata32[1]);
  429. offset = 2 * 4 + 2 * 4 * pfw->fw.part_num;
  430. pfw->fw.total_size = 0;
  431. for (uint8_t i = 0; i < pfw->fw.part_num; i++){
  432. pfw->fw.part[i].run_addr = pdata32[i*2+2];
  433. pfw->fw.part[i].size = pdata32[i*2+3];
  434. pfw->fw.part[i].flash_addr = faddr + offset;
  435. pfw->fw.part[i].checksum = crc16(0, (const volatile void * )(pfw->fw.part[i].flash_addr), pfw->fw.part[i].size);
  436. offset += pfw->fw.part[i].size;
  437. pfw->fw.total_size += pfw->fw.part[i].size;
  438. }
  439. pfw->fw.total_size += 0;
  440. return PPlus_SUCCESS;
  441. }
  442. int load_res(void)
  443. {
  444. return PPlus_ERR_NOT_IMPLEMENTED;
  445. }
  446. int otamProtocol_start_ota(ota_fw_t* pffw)
  447. {
  448. otam_proto_ctx_t *pctx = &s_otap_ctx_t;
  449. otam_fw_t *pfw = &(pctx->fw);
  450. otam_proto_meth_t *method = &(pctx->method);
  451. uint8_t data[20];
  452. memcpy(&(pfw->fw), pffw, sizeof(ota_fw_t));
  453. pfw->fw.offset = 0;
  454. pfw->fw.part_current = 0;
  455. pfw->fw.total_offset = 0;
  456. if(pctx->state < OTAM_ST_CONNECTED)
  457. return PPlus_ERR_BLE_NOT_READY;
  458. if(pctx->run_mode != OTAC_RUNMODE_OTA){
  459. return PPlus_ERR_INVALID_STATE;
  460. }
  461. if(method->write_cmd){
  462. pctx->state = OTAM_ST_WAIT_STARTED;
  463. pfw->fw.part_current = 0;
  464. if(pctx->mtu_a == 20){
  465. pctx->burst_size = OTA_BURST_SIZE_DEFAULT;
  466. data[0] = OTA_CMD_START_OTA;
  467. data[1] = pfw->fw.part_num;
  468. data[2] = 0;
  469. }
  470. else
  471. {
  472. pctx->burst_size = 0xffff;
  473. data[0] = OTA_CMD_START_OTA;
  474. data[1] = pfw->fw.part_num;
  475. data[2] = OTA_BURST_SIZE_HISPEED;
  476. }
  477. return method->write_cmd(data, 3, 1000);
  478. }
  479. return PPlus_ERR_NOT_REGISTED;
  480. }
  481. int otamProtocol_stop_ota(void)
  482. {
  483. otam_proto_ctx_t *pctx = &s_otap_ctx_t;
  484. otam_proto_meth_t *method = &(pctx->method);
  485. uint8_t data[20];
  486. if(method->write_cmd){
  487. data[0] = OTA_CMD_START_OTA;
  488. data[1] = 0xff;
  489. data[2] = 0;
  490. pctx->state = OTAM_ST_CANCELING;
  491. return method->write_cmd(data, 3, 1000);
  492. }
  493. return PPlus_ERR_NOT_REGISTED;
  494. }
  495. int otamProtocol_app_start_ota(uint8_t mode)
  496. {
  497. otam_proto_ctx_t *pctx = &s_otap_ctx_t;
  498. otam_proto_meth_t *method = &(pctx->method);
  499. uint8_t data[20];
  500. if(pctx->run_mode != OTAC_RUNMODE_APP)
  501. return PPlus_ERR_INVALID_STATE;
  502. if(mode > OTA_MODE_RESOURCE)
  503. return PPlus_ERR_INVALID_PARAM;
  504. if(method->write_cmd){
  505. data[0] = OTAAPP_CMD_START_OTA;
  506. data[1] = mode;
  507. data[2] = 1;
  508. return method->write_cmd(data, 3, 0);
  509. }
  510. return PPlus_ERR_NOT_REGISTED;
  511. }
  512. int otamProtocol_init(otam_proto_meth_t* method)
  513. {
  514. memset(&s_otap_ctx_t, 0, sizeof(s_otap_ctx_t));
  515. s_otap_ctx_t.method = *method;
  516. return PPlus_SUCCESS;
  517. }