abup_hal_application.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*****************************************************************************
  2. * Copyright (c) 2018 ABUP.Co.Ltd. All rights reserved.
  3. * File name: abup_hal.c
  4. * Description: 逻辑、AT处理
  5. * Author: Ray Shen
  6. * Version: v1.0
  7. * Date: 20181101
  8. *****************************************************************************/
  9. #include "abup_hal.h"
  10. #if defined(ABUP_RTTHREAD)
  11. #include "fal.h"
  12. #else
  13. #if !defined(ABUP_BOOTLOADER)
  14. #include "abup_custom.h"
  15. #endif
  16. #include "abup_hal_flash.h"
  17. #include "abup_hal_uart.h"
  18. #endif
  19. #if !defined(ABUP_BOOTLOADER)
  20. abup_uint32 abup_action_result = ABUP_RESULT_FAIL;
  21. abup_msg_cb abup_app_msg_cb = NULL;
  22. abup_msg_cb abup_atp_msg_cb = NULL;
  23. abup_state_result abup_state_result_cb = NULL;
  24. download_uri_struct download_atp_uri_data = {0};
  25. download_uri_struct *download_atp_uri = &download_atp_uri_data;
  26. ABUP_ALIGN(1) abup_uint8 abup_hal_data[ABUP_HAL_DATA_MAX_LEN] = {0};
  27. abup_uint32 download_index = 0;
  28. abup_uint32 download_index_max = 0;
  29. abup_uint abup_md5_calc_result = 0;
  30. abup_uint16 abup_current_msgid = 0;
  31. abup_uint8 abup_current_token[ABUP_TOKEN_MAXLEN] = {0};
  32. abup_uint8 abup_current_token_len = 0;
  33. abup_uint8 abup_str_ip[16] = {0};
  34. abup_int8 abup_conn_try_count = 0;
  35. abup_uint32 abup_port = 0;
  36. #endif
  37. #ifdef ABUP_QUEUE_MSG
  38. void abup_hal_app_msg(void* ptr)
  39. {
  40. if(abup_app_msg_cb != NULL)
  41. {
  42. if(abup_exist_msgid(ABUP_MSG_ATP_SEND_ABUP_MSG_2_APP) == NULL)
  43. {
  44. abup_obtain_msg(ABUP_MSG_ATP_SEND_ABUP_MSG_2_APP, abup_app_msg_cb, ptr);
  45. }
  46. }
  47. }
  48. void abup_hal_start(abup_msg_cb app_msg_cb,abup_msg_cb atp_msg_cb,abup_state_result state_result)
  49. {
  50. abup_action_result = ABUP_RESULT_FAIL;
  51. abup_app_msg_cb = app_msg_cb;
  52. abup_atp_msg_cb = atp_msg_cb;
  53. abup_state_result_cb = state_result;
  54. abup_atp_init();
  55. abup_display_update(abup_false);
  56. }
  57. abup_bool abup_hal_started(void)
  58. {
  59. if((abup_app_msg_cb != NULL)
  60. ||(abup_atp_msg_cb != NULL)
  61. ||(abup_state_result_cb != NULL))
  62. {
  63. abup_info_printf("[Abup] try later!\r\n");
  64. return abup_true;
  65. }
  66. return abup_false;
  67. }
  68. void abup_hal_stop(void)
  69. {
  70. abup_app_msg_cb = NULL;
  71. abup_atp_msg_cb = NULL;
  72. abup_state_result_cb = NULL;
  73. abup_atp_at_stop_timer();
  74. abup_display_update(abup_true);
  75. abup_msg_from_app(abup_action_result);
  76. }
  77. abup_bool abup_hal_get_state(void)
  78. {
  79. if(abup_app_msg_cb == NULL)
  80. return abup_false;
  81. else
  82. return abup_true;
  83. }
  84. #endif
  85. #if defined(ABUP_BOOTLOADER)
  86. #else
  87. abup_uint8 *abup_get_hal_data(void)
  88. {
  89. return (abup_uint8 *)abup_hal_data;
  90. }
  91. abup_uint abup_get_hal_data_len(void)
  92. {
  93. return ABUP_HAL_DATA_MAX_LEN;
  94. }
  95. void abup_reset_hal_data(void)
  96. {
  97. abup_memset(abup_hal_data,0,ABUP_HAL_DATA_MAX_LEN);
  98. }
  99. download_uri_struct* abup_get_download_atp_uri(void)
  100. {
  101. return (download_uri_struct *)download_atp_uri;
  102. }
  103. abup_uint8* abup_hal_get_download_host_ip(void)
  104. {
  105. return (abup_uint8 *)download_atp_uri->download_host_ip;
  106. }
  107. abup_uint32 abup_hal_get_download_delta_id(void)
  108. {
  109. return (abup_uint32)download_atp_uri->download_delta_id;
  110. }
  111. abup_uint32 abup_hal_get_download_delta_size(void)
  112. {
  113. return (abup_uint32)download_atp_uri->download_delta_size;
  114. }
  115. abup_char* abup_hal_get_download_url(void)
  116. {
  117. return (abup_char*)download_atp_uri->download_url;
  118. }
  119. abup_char* abup_hal_get_download_host(void)
  120. {
  121. return (abup_char*)download_atp_uri->download_host;
  122. }
  123. abup_uint32 abup_get_download_index(void)
  124. {
  125. return download_index;
  126. }
  127. abup_uint32 abup_get_download_index_max(void)
  128. {
  129. return download_index_max;
  130. }
  131. abup_int8 abup_get_conn_try_count(void)
  132. {
  133. return abup_conn_try_count;
  134. }
  135. void abup_set_conn_try_count(abup_int8 count)
  136. {
  137. abup_conn_try_count = count;
  138. }
  139. abup_bool abup_legal_ID(abup_int32 mid,abup_int32 msgid)
  140. {
  141. if((mid > (msgid - abup_get_conn_try_count()))&&(mid < (msgid + 1)))
  142. {
  143. return abup_true;
  144. }
  145. abup_info_printf("mid = %d msgid=%d\r\n",mid,msgid);
  146. return abup_false;
  147. }
  148. abup_int abup_isipv4str(const abup_char *str)
  149. {
  150. abup_int i, a[4];
  151. if(abup_sscanf(str,"%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) != 4 )
  152. {
  153. return 0;
  154. }
  155. for(i=0; i<4; i++)
  156. {
  157. if (a[i] < 0 || a[i] > 255)
  158. {
  159. return 0;
  160. }
  161. else
  162. {
  163. continue;
  164. }
  165. }
  166. if((a[0] == 0)&&(a[1] == 0)&&(a[2] == 0)&&(a[3] == 0))
  167. {
  168. return 0;
  169. }
  170. return 1;
  171. }
  172. abup_uint8* abup_get_str_ip(abup_uint8* ip)
  173. {
  174. abup_memset(abup_str_ip,0,16);
  175. if(ip != NULL)
  176. {
  177. abup_snprintf((abup_char *)abup_str_ip,sizeof(abup_str_ip),"%d.%d.%d.%d",ip[0],ip[1],ip[2],ip[3]);
  178. }
  179. return abup_str_ip;
  180. }
  181. abup_bool abup_get_domain(abup_char *server,abup_char* domain,abup_int16 domain_len,abup_int* port)
  182. {
  183. if(server)
  184. {
  185. abup_char* c = abup_strstr(server,":");
  186. if(c&&domain)
  187. {
  188. if(port != NULL)
  189. {
  190. abup_memset(domain,0,domain_len);
  191. abup_strcpy(domain,c + 1);
  192. *port = abup_atoi(domain);
  193. }
  194. abup_memset(domain,0,domain_len);
  195. abup_memcpy(domain,server,c - server);
  196. return abup_true;
  197. }
  198. }
  199. return abup_false;
  200. }
  201. abup_char* abup_get_server_host(void)
  202. {
  203. #if ((ABUP_DEFAULT_NETWORK_PROTOCOL == ABUP_PROTOCOL_COAP)||(ABUP_DEFAULT_NETWORK_PROTOCOL == ABUP_PROTOCOL_COAP_HTTP))
  204. if(abup_get_default_protocol() == ABUP_PROTOCOL_COAP)
  205. return abup_get_coap_server_host();
  206. #endif
  207. #if ((ABUP_DEFAULT_NETWORK_PROTOCOL == ABUP_PROTOCOL_HTTP)||(ABUP_DEFAULT_NETWORK_PROTOCOL == ABUP_PROTOCOL_COAP_HTTP))
  208. if(abup_get_default_protocol() == ABUP_PROTOCOL_HTTP)
  209. return abup_get_http_server_host();
  210. #endif
  211. return NULL;
  212. }
  213. abup_bool abup_hal_para_url(abup_char *url,abup_uint16 url_len)
  214. {
  215. abup_uint8 *abup_p = NULL;
  216. abup_uint8 flag = 0;
  217. abup_uint8 *p1 = NULL;
  218. abup_uint8 *p2 = NULL;
  219. abup_uint8 length = 0;
  220. abup_uint8 id = 0;
  221. abup_char *tmp = (abup_char *)abup_get_buf();
  222. abup_uint8 len = 0;
  223. download_uri_struct* download_uri = abup_get_download_atp_uri();
  224. abup_p = (abup_uint8*)url;
  225. flag = 0;
  226. abup_memset(download_uri,0,sizeof(download_uri_struct));
  227. while((abup_p != NULL)&&(abup_p < (abup_uint8 *)(url + url_len)))
  228. {
  229. abup_bool max = abup_false;
  230. if(*abup_p&0xC0)
  231. {
  232. abup_reset_buf();
  233. if(*abup_p&0x08)
  234. {
  235. length = *(abup_p + 2);
  236. max = abup_true;
  237. }
  238. else
  239. {
  240. length = *abup_p - 0xC0;
  241. }
  242. id = *(abup_p + 1);
  243. if(id == 0x01)
  244. {
  245. p1 = (abup_uint8*)abup_strstr((const abup_char*)url,"coap://");
  246. if(p1 == NULL)
  247. break;
  248. len = abup_strlen("coap://");
  249. p2 = (abup_uint8*)abup_strstr((const abup_char*)p1 + len,":");
  250. if(p2 == NULL)
  251. break;
  252. p1 += len;
  253. if(p1 == NULL)
  254. break;
  255. if((p2 - p1) < sizeof(download_uri->download_host))
  256. abup_memcpy(download_uri->download_host,p1,p2 - p1);
  257. else
  258. break;
  259. // ABUP_hal_parse_dns(download_uri->download_host,download_uri->download_host_ip);
  260. p1 = p2 + 1;
  261. if(p1 == NULL)
  262. break;
  263. p2 = (abup_uint8*)abup_strstr((const abup_char*)p1,"/");
  264. if(p2 == NULL)
  265. break;
  266. abup_memcpy(tmp,p1,p2 - p1);
  267. abup_sscanf(tmp,"%d",&download_uri->download_port);
  268. abup_memset(tmp,0,8);
  269. if((abup_p + 2 + length + (max?1:0) - p2) < sizeof(download_uri->download_url))
  270. abup_memcpy(download_uri->download_url,p2,abup_p + 2 + length + (max?1:0) - p2);
  271. else
  272. break;
  273. flag |= ABUP_HAL_FLAG_1;
  274. }
  275. else if(id == 0x64)
  276. {
  277. p1 = abup_p + 2 + (max?1:0);
  278. if(p1 == NULL)
  279. break;
  280. abup_memcpy(tmp,p1,length);
  281. abup_sscanf(tmp,"%d",&download_uri->download_delta_size);
  282. abup_memset(tmp,0,8);
  283. flag |= ABUP_HAL_FLAG_2;
  284. }
  285. else if(id == 0x65)
  286. {
  287. p1 = abup_p + 2 + (max?1:0);
  288. if(p1 == NULL)
  289. break;
  290. abup_memcpy(tmp,p1,length);
  291. abup_sscanf(tmp,"%d",&download_uri->download_delta_id);
  292. abup_memset(tmp,0,8);
  293. flag |= ABUP_HAL_FLAG_4;
  294. }
  295. else if(id == 0x66)
  296. {
  297. p1 = abup_p + 2 + (max?1:0);
  298. if(p1 == NULL)
  299. break;
  300. abup_hexstr2byte((abup_char*)download_uri->download_md5,(const abup_uint8 *)p1,(sizeof(download_uri->download_md5)*2) < length?(sizeof(download_uri->download_md5)*2):length);
  301. flag |= ABUP_HAL_FLAG_8;
  302. }
  303. if(flag == ABUP_HAL_FLAG_ALL)
  304. break;
  305. abup_p = abup_p + 2 + length + (max?1:0);
  306. }
  307. else
  308. {
  309. abup_p++;
  310. }
  311. }
  312. if(flag == ABUP_HAL_FLAG_ALL)
  313. {
  314. return abup_true;
  315. }
  316. else
  317. return abup_false;
  318. }
  319. abup_uint32* abup_get_host_port(void)
  320. {
  321. return &abup_port;
  322. }
  323. abup_bool abup_hal_para_http(abup_uint8 *data,abup_http_parameter* tmp,abup_uint8 tmplen)
  324. {
  325. abup_uint8 i = 0;
  326. abup_bool result = abup_false;
  327. abup_memset(tmp,0,tmplen);
  328. abup_memcpy(tmp[i++].type,"\"deltaUrl\":",abup_strlen("\"deltaUrl\":"));
  329. abup_memcpy(tmp[i++].type,"\"deltaID\":",abup_strlen("\"deltaID\":"));
  330. abup_memcpy(tmp[i++].type,"\"fileSize\":",abup_strlen("\"fileSize\":"));
  331. abup_memcpy(tmp[i++].type,"\"md5sum\":",abup_strlen("\"md5sum\":"));
  332. #ifdef ABUP_SLIM_RES
  333. #else
  334. abup_memcpy(tmp[i++].type,"\"bakUrl\":",abup_strlen("\"bakUrl\":"));
  335. #endif
  336. if(abup_parse_http_data((abup_char*)data,tmp,i))
  337. {
  338. download_uri_struct* uri = abup_get_download_atp_uri();
  339. abup_char * id = abup_get_product_id();
  340. abup_memset(uri->download_md5,0,sizeof(uri->download_md5));
  341. if((tmp[1].content_len - 2) < sizeof(uri->download_md5))
  342. {
  343. abup_memcpy(uri->download_md5,tmp[1].content + 1,tmp[1].content_len - 2);
  344. uri->download_delta_id = abup_atoi(uri->download_md5);
  345. }
  346. uri->download_port = 80;
  347. abup_memset(uri->download_md5,0,sizeof(uri->download_md5));
  348. if(tmp[2].content_len < sizeof(uri->download_md5))
  349. {
  350. abup_memcpy(uri->download_md5,tmp[2].content,tmp[2].content_len);
  351. }
  352. uri->download_delta_size = abup_atoi(uri->download_md5);
  353. abup_memset(uri->download_md5,0,sizeof(uri->download_md5));
  354. abup_snprintf((abup_char*)uri->download_md5,ABUP_DOWNLOAD_MD5_LEN,"/%s",id);
  355. #ifdef ABUP_SLIM_RES
  356. abup_char* http = NULL;
  357. abup_char* bk = NULL;
  358. #else
  359. abup_char* bk = abup_strstr((const abup_char*)tmp[4].content,(const abup_char*)uri->download_md5);
  360. abup_char* http = abup_strstr((const abup_char*)tmp[4].content,(const abup_char*)"http://");
  361. if(http)
  362. {
  363. http += abup_strlen("http://");
  364. if(bk)
  365. {
  366. if(((abup_char*)bk - (abup_char*)http) < sizeof(uri->bkup_host))
  367. {
  368. abup_memcpy(uri->bkup_host,http,((abup_char*)bk - (abup_char*)http));
  369. }
  370. }
  371. }
  372. #endif
  373. bk = abup_strstr((const abup_char*)tmp[0].content,(const abup_char*)"com/");
  374. http = abup_strstr((const abup_char*)tmp[0].content,(const abup_char*)"http://");
  375. if(http)
  376. {
  377. abup_uint8 http_len = abup_strlen("http://");
  378. if(bk)
  379. {
  380. bk += 3;
  381. abup_memcpy(uri->download_host,http + http_len,((abup_char*)bk - (abup_char*)http - http_len));
  382. if((http + tmp[0].content_len - 2 - bk) < sizeof(uri->download_url))
  383. {
  384. abup_memcpy(uri->download_url,bk,http + tmp[0].content_len - 2 - bk);
  385. }
  386. result = abup_true;
  387. }
  388. }
  389. abup_memset(uri->download_md5,0,sizeof(uri->download_md5));
  390. abup_hexstr2byte((abup_char*)uri->download_md5,(const abup_uint8 *)tmp[3].content + 1,(tmp[3].content_len - 2)>(sizeof(uri->download_md5)*2)?(sizeof(uri->download_md5)*2):(tmp[3].content_len - 2));
  391. }
  392. return result;
  393. }
  394. abup_uint abup_MD5Calc_result(void)
  395. {
  396. download_uri_struct* abup_app_uri = abup_get_download_atp_uri();
  397. abup_char* download_md5 = abup_get_buf();
  398. abup_uint result = 8;
  399. abup_memset(download_md5,0,ABUP_DOWNLOAD_MD5_LEN + 1);
  400. if(abup_MD5Calc(abup_app_uri->download_delta_size,download_md5) == 0)
  401. {
  402. if(abup_memcmp(download_md5,abup_app_uri->download_md5,ABUP_DOWNLOAD_MD5_LEN) == 0)
  403. {
  404. result = 1;
  405. }
  406. }
  407. if(result == 1)
  408. {
  409. abup_info_printf("[Abup] MD5 calc OK\r\n");
  410. }
  411. else
  412. {
  413. abup_info_printf("[Abup] MD5 calc error\r\n");
  414. }
  415. return result;
  416. }
  417. abup_int abup_MD5Calc(abup_uint buflen, abup_char *md5out)
  418. {
  419. abup_uint idx=0, segnum=0, remain=0;
  420. abup_uint16 segmaxsize = abup_get_data_max_len();
  421. ABUP_MD5_CTX md5;
  422. abup_uint8* tmp = NULL;
  423. if((buflen==0) || !md5out)
  424. {
  425. return -1;
  426. }
  427. tmp = (abup_uint8*)abup_get_ATBuf();
  428. AbupMD5Init(&md5);
  429. segnum = buflen / segmaxsize;
  430. remain = buflen - (segnum*segmaxsize);
  431. if((segnum==0) && (remain==0))
  432. return -1;
  433. for(idx=0; idx<segnum; idx++)
  434. {
  435. abup_memset(tmp,0,segmaxsize);
  436. abup_hal_flash_read(ABUP_FILETYPE_PATCH,idx*segmaxsize, (abup_uint8 *)tmp, segmaxsize);
  437. AbupMD5Update(&md5, tmp, segmaxsize);
  438. }
  439. if(remain > 0)
  440. {
  441. abup_memset(tmp,0,segmaxsize);
  442. abup_hal_flash_read(ABUP_FILETYPE_PATCH,buflen - remain, (abup_uint8 *)tmp, segmaxsize);
  443. AbupMD5Update(&md5, tmp, remain);
  444. }
  445. AbupMD5Final(&md5,(abup_uint8 *)md5out);
  446. return 0;
  447. }
  448. abup_int8 abup_hal_get_current_state(void)
  449. {
  450. abup_int8 abup_state = -1;
  451. #if defined(ABUP_RTTHREAD)
  452. extern abup_uint8 AbupState;
  453. abup_state = AbupState;
  454. #else
  455. switch(*abup_get_atp_state())
  456. {
  457. case ABUP_SETUP_REGISTER:
  458. {
  459. abup_state = STATE_RG;
  460. }
  461. break;
  462. case ABUP_SETUP_CHECK_VERSION:
  463. {
  464. abup_state = STATE_CV;
  465. }
  466. break;
  467. case ABUP_SETUP_GET_KEY:
  468. {
  469. abup_state = STATE_KY;
  470. }
  471. break;
  472. case ABUP_SETUP_RTP_DOWNLOAD:
  473. {
  474. abup_state = STATE_RD;
  475. }
  476. break;
  477. case ABUP_SETUP_RTP_UPDATE:
  478. {
  479. abup_state = STATE_RU;
  480. }
  481. break;
  482. case ABUP_SETUP_DOWNLOAD:
  483. {
  484. abup_state = STATE_DL;
  485. }
  486. break;
  487. default:
  488. break;
  489. }
  490. #endif
  491. return abup_state;
  492. }
  493. void abup_hal_update_device(abup_uint8* mid,abup_int16 mid_len,abup_uint8* deviceId,abup_int16 deviceIdLen,abup_uint8* deviceSecret,abup_int16 deviceSecretLen)
  494. {
  495. abup_update_struct* abup_update = abup_hal_get_update();
  496. abup_bool NeedSave = abup_false;
  497. if((mid != NULL)&&(mid_len > 0))
  498. {
  499. if(abup_strncmp((const abup_char*)mid,(const abup_char*)abup_hal_get_mid(),ABUP_MID_MAXLEN) != 0)
  500. {
  501. abup_memset(abup_hal_get_mid(),0,ABUP_MID_MAXLEN);
  502. abup_memset(abup_update->deviceId,0,sizeof(abup_update->deviceId));
  503. abup_memset(abup_update->deviceSecret,0,sizeof(abup_update->deviceSecret));
  504. abup_memcpy(abup_hal_get_mid(),mid,mid_len);
  505. NeedSave = abup_true;
  506. }
  507. }
  508. else if((deviceSecret != NULL)&&(deviceSecretLen > 0)&&(deviceId != NULL)&&(deviceIdLen > 0))
  509. {
  510. deviceIdLen = deviceIdLen>sizeof(abup_update->deviceId)?sizeof(abup_update->deviceId):deviceIdLen;
  511. deviceSecretLen = deviceSecretLen>sizeof(abup_update->deviceSecret)?sizeof(abup_update->deviceSecret):deviceSecretLen;
  512. abup_memset(abup_update->deviceId,0,sizeof(abup_update->deviceId));
  513. abup_memset(abup_update->deviceSecret,0,sizeof(abup_update->deviceSecret));
  514. abup_memcpy(abup_update->deviceSecret,deviceSecret,deviceSecretLen);
  515. abup_memcpy(abup_update->deviceId,deviceId,deviceIdLen);
  516. NeedSave = abup_true;
  517. }
  518. if(NeedSave)
  519. {
  520. abup_hal_set_update(abup_update);
  521. }
  522. }
  523. #endif