sim7600.c.orig 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. #include "sim7600.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "fifo.h"
  5. #include "usart.h"
  6. #include "stdlib.h"
  7. #include "timer4.h"
  8. #define ATC_RSP_FINISH 1
  9. #define ATC_RSP_WAIT 0
  10. #define AT_MAX_RESPONSE_TIME 300
  11. #define HTTP_HEADER_CONTENT_TYPE "application/json"
  12. #define MAX_HTTP_SIZE 1000
  13. u8 g_iemi_buf[16];
  14. struct module_buffer {
  15. u8 *buffer;
  16. u16 length;
  17. };
  18. struct module_buf {
  19. u8 *buf;
  20. u16 length;
  21. };
  22. static struct module_buffer *module_send_data_buffer = NULL;
  23. static module_tcp_connected_callback tcp_connect_cb = NULL;
  24. static module_tcp_sent_callback tcp_sent_cb = NULL;
  25. static module_tcp_recv_callback tcp_recv_cb = NULL;
  26. static module_tcp_disconnected_callback tcp_disconnected_cb = NULL;
  27. static module_http_callback s_http_cb = NULL;
  28. static char s_http_buffer[MAX_HTTP_SIZE];
  29. typedef s8 (*AT_cmdHandle)(BOOL *urc, char* buf);
  30. // 信号强度
  31. static u8 s_csq_value = 0;
  32. typedef struct {
  33. char *name;
  34. AT_cmdHandle at_cmd_handler;
  35. } AtcHandlerType;
  36. static s8 at_handler(BOOL *urc, char* buf);
  37. static s8 ate_handler(BOOL *urc, char* buf);
  38. static s8 csq_handler(BOOL *urc, char* buf);
  39. static s8 cpin_handler(BOOL *urc, char *buf);
  40. //static s8 reg_handler(BOOL *urc, char *buf);
  41. //static s8 creg_handler(BOOL *urc, char *buf);
  42. static s8 getip_handler(BOOL *urc, char *buf);
  43. // net open handler
  44. static s8 net_open_handler(BOOL *urc, char* buf);
  45. static s8 net_close_handler(BOOL *urc, char* buf);
  46. static s8 gsn_handler(BOOL *urc, char* buf);
  47. // http handler
  48. static s8 http_send_handler(BOOL *urc, char*buf);
  49. static s8 http_read_handler(BOOL *urc, char*buf);
  50. static s8 cch_open_handle(BOOL *urc, char*buf);
  51. static s8 cch_send_handle(BOOL *urc, char*buf);
  52. // tcp handle.
  53. static s8 tcp_connect_handle(BOOL *urc, char*buf);
  54. static s8 ipsend_handle(BOOL *urc, char*buf);
  55. static s8 urc_handle(BOOL *urc, char *buf);
  56. static s8 tcp_closed_handle(BOOL *urc, char *buf);
  57. static s8 tcp_disconnect_handle(BOOL *urc, char *buf);
  58. // AT Command table
  59. AtcHandlerType atCmdTable[ ] = {
  60. // 打开或关闭AT指令回显
  61. {"ATE", ate_handler},
  62. // 设置本地流量控制
  63. {"AT+IFC", at_handler},
  64. // 输入PIN码
  65. {"AT+CPIN", cpin_handler},
  66. // 查询信号强度
  67. {"AT+CSQ", csq_handler},
  68. // 获取序列号ID
  69. {"AT+CGSN", gsn_handler},
  70. // 设置 TCP/IP 应用模式
  71. // 0: 非透传
  72. // 1: 命令模式
  73. {"AT+CIPMODE", at_handler},
  74. // 打开SOCKET
  75. {"AT+NETOPEN", net_open_handler},
  76. {"AT+NETCLOSE", net_close_handler},
  77. // 查询注册网络状态
  78. {"AT+CREG", at_handler},
  79. // 注册 信息
  80. {"AT+CPSI?", at_handler},
  81. {"AT+IPADDR", getip_handler},
  82. // http
  83. {"AT+CHTTPACT", http_send_handler},
  84. // tcp
  85. // 建立tcp连接
  86. {"AT+CIPOPEN", tcp_connect_handle},
  87. // 向远端tcp或udp连接发送数据
  88. {"AT+CIPSEND", ipsend_handle},
  89. // 关闭tcp或ucp连接
  90. {"AT+CIPCLOSE", tcp_closed_handle},
  91. // 设置收到消息时是否显示远程IP和端口
  92. {"AT+CIPSRIP", at_handler},
  93. // 在CIPCLOSE指令后调用,关闭所有网络链接
  94. {"AT+NETCLOSE", at_handler},
  95. {"AT+CCHSET", at_handler},
  96. {"AT+CCHSTART", at_handler},
  97. {"AT+CCHOPEN", cch_open_handle},
  98. {"AT+CCHSEND", cch_send_handle},
  99. {"AT+CCHCLOSE", at_handler}, // TODO: consider response after OK.
  100. {"AT+CCHSTOP", at_handler},
  101. };
  102. AtcHandlerType urc_table[] = {
  103. {"+IPCLOSE:", tcp_disconnect_handle}
  104. };
  105. struct at_cmd_entity {
  106. char at_name[20];
  107. char at_cmd[128];
  108. };
  109. static s8 s_module_status = MODULE_OFF_LINE;
  110. static struct FIFO s_at_fifo;
  111. static u8 s_at_status = ATC_RSP_FINISH;
  112. static struct at_cmd_entity *s_current_at_command = NULL;
  113. // 发送at指令
  114. static void send_at_command(const char* cmd) {
  115. Usart2_Send_Data((u8*)cmd, strlen(cmd));
  116. }
  117. // urc 处理函数
  118. static s8 urc_handle(BOOL *urc, char* buf) {
  119. // //printf("urc handler: %s\n", buf);
  120. return 0;
  121. }
  122. // at指令返回处理函数
  123. static s8 at_handler(BOOL *urc, char *buf) {
  124. char *rep_str[] = {"OK", "ERROR"};
  125. s8 res = -1;
  126. char *p;
  127. u8 i = 0;
  128. p = (char *)buf;
  129. while ( 'r' == *p || '\n' == *p) {
  130. p++;
  131. }
  132. for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
  133. {
  134. if (strstr( p,rep_str[i]))
  135. {
  136. res = i;
  137. break;
  138. }
  139. }
  140. switch (res)
  141. {
  142. case 0:
  143. s_at_status = ATC_RSP_FINISH;
  144. break;
  145. case 1:
  146. s_at_status = ATC_RSP_FINISH;
  147. break;
  148. default:
  149. break;
  150. }
  151. return s_at_status;
  152. }
  153. // CPIN指令返回处理函数
  154. static s8 cpin_handler(BOOL *urc, char *buf) {
  155. char *rep_str[] = {"OK", "ERROR", "+CPIN: "};
  156. s8 res = -1;
  157. char *p;
  158. u8 i = 0;
  159. p = (char *)buf;
  160. while ( 'r' == *p || '\n' == *p) {
  161. p++;
  162. }
  163. for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
  164. {
  165. if (strstr( p,rep_str[i]))
  166. {
  167. res = i;
  168. break;
  169. }
  170. }
  171. switch (res)
  172. {
  173. case 0:
  174. //s_at_status = ATC_RSP_FINISH;
  175. break;
  176. case 1:
  177. //s_at_status = ATC_RSP_FINISH;
  178. break;
  179. case 2:
  180. // 返回ready说明正常
  181. if(strstr(p, "READY")!=NULL)
  182. {
  183. s_at_status = ATC_RSP_FINISH;
  184. }
  185. break;
  186. default:
  187. break;
  188. }
  189. return s_at_status;
  190. }
  191. static s8 csq_handler(BOOL *urc, char *buf) {
  192. char *rep_str [] = {"OK", "ERROR", "+CSQ: "};
  193. s8 res = -1;
  194. char *p;
  195. u8 i = 0;
  196. p= (char *)buf;
  197. while ( '\r' == *p || '\n' == *p)
  198. {
  199. p++;
  200. }
  201. for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
  202. {
  203. if (strstr( p,rep_str[i]))
  204. {
  205. res = i;
  206. break;
  207. }
  208. }
  209. switch (res)
  210. {
  211. case 0:
  212. p = strstr(p, "+CSQ: ");
  213. s_csq_value = atoi(p + strlen("+CSQ: "));
  214. s_at_status = ATC_RSP_FINISH;
  215. break;
  216. case 1:
  217. s_at_status = ATC_RSP_FINISH;
  218. break;
  219. case 2:
  220. PRINTF("csq:%s\r\n", p);
  221. s_csq_value = atoi(p + sizeof("+CSQ: "));
  222. s_at_status = ATC_RSP_FINISH;
  223. break;
  224. default:
  225. break;
  226. }
  227. return s_at_status;
  228. }
  229. static s8 getip_handler(BOOL *urc, char *buf) {
  230. char *rep_str[ ] = {"OK", "ERROR", "+IPADDR: ", "+IP ERROR: "};
  231. s8 res = -1;
  232. char *p;
  233. u8 i = 0;
  234. p= (char *)buf;
  235. while ( '\r' == *p || '\n' == *p)
  236. {
  237. p++;
  238. }
  239. for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
  240. {
  241. if (strstr( p,rep_str[i]))
  242. {
  243. res = i;
  244. break;
  245. }
  246. }
  247. switch (res)
  248. {
  249. case 0: // OK
  250. {
  251. s_at_status = ATC_RSP_FINISH;
  252. s_module_status = MODULE_GET_IP;
  253. }
  254. break;
  255. case 1: // ERROR
  256. {
  257. }
  258. break;
  259. case 2:
  260. {
  261. s_at_status = ATC_RSP_FINISH;
  262. s_module_status = MODULE_GET_IP;
  263. }
  264. break;
  265. case 3: // +IP ERROR:
  266. {
  267. PRINTF("Net\r\n");
  268. }
  269. break;
  270. default:
  271. break;
  272. }
  273. return s_at_status;
  274. }
  275. static s8 ate_handler(BOOL *urc, char* buf)
  276. {
  277. char *rep_str[ ] = {"OK","ERROR", "ATE"};
  278. s8 res = -1;
  279. char *p;
  280. u8 i = 0;
  281. p= (char *)buf;
  282. while ( '\r' == *p || '\n' == *p)
  283. {
  284. p++;
  285. }
  286. for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
  287. {
  288. if (strstr( p,rep_str[i]))
  289. {
  290. res = i;
  291. break;
  292. }
  293. }
  294. switch (res)
  295. {
  296. case 0: // OK
  297. {
  298. s_at_status = ATC_RSP_FINISH;
  299. }
  300. break;
  301. case 1: // ERROR
  302. {
  303. s_at_status = ATC_RSP_FINISH;
  304. }
  305. break;
  306. case 2: // ATE
  307. {
  308. //printf("open");
  309. }
  310. break;
  311. default:
  312. break;
  313. }
  314. return s_at_status;
  315. }
  316. static s8 gsn_handler(BOOL *urc, char*buf)
  317. {
  318. char *rep_str[ ] = {"OK","ERROR"};
  319. s8 res = -1;
  320. char *p;
  321. u8 i = 0;
  322. static u8 flag = 0;
  323. p= (char *)buf;
  324. while ( '\r' == *p || '\n' == *p)
  325. {
  326. p++;
  327. }
  328. if(flag == 0)
  329. {
  330. memcpy(g_iemi_buf, p, 15);
  331. g_iemi_buf[15] = 0;
  332. PRINTF("g_iemi_buf:%s", g_iemi_buf);
  333. flag = 1;
  334. }
  335. for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
  336. {
  337. if (strstr( p,rep_str[i]))
  338. {
  339. res = i;
  340. break;
  341. }
  342. }
  343. switch (res)
  344. {
  345. case 0: // OK
  346. {
  347. s_at_status = ATC_RSP_FINISH;
  348. }
  349. break;
  350. case 1: // ERROR
  351. {
  352. s_at_status = ATC_RSP_FINISH;
  353. }
  354. break;
  355. default:
  356. s_at_status = ATC_RSP_FINISH;
  357. break;
  358. }
  359. return s_at_status;
  360. }
  361. static s8 net_open_handler( BOOL *urc, char* buf)
  362. {
  363. char *rep_str[ ] = {"OK","ERROR", "+NETOPEN: "};
  364. s8 res = -1;
  365. char *p;
  366. u8 i = 0;
  367. p= (char *)buf;
  368. while ( '\r' == *p || '\n' == *p)
  369. {
  370. p++;
  371. }
  372. for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
  373. {
  374. if (strstr( p,rep_str[i]))
  375. {
  376. res = i;
  377. break;
  378. }
  379. }
  380. switch (res)
  381. {
  382. case 0: // OK
  383. {
  384. }
  385. break;
  386. case 1: // ERROR
  387. {
  388. s_at_status = ATC_RSP_FINISH;
  389. s_module_status = MODULE_GET_IP;
  390. }
  391. break;
  392. case 2:
  393. {
  394. s_at_status = ATC_RSP_FINISH;
  395. s_module_status = MODULE_GET_IP;
  396. }
  397. break;
  398. default:
  399. break;
  400. }
  401. return s_at_status;
  402. }
  403. static s8 net_close_handler( BOOL *urc, char* buf)
  404. {
  405. char *rep_str[ ] = {"OK","ERROR", "+NETCLOSE: ", "+IP ERROR: "};
  406. s8 res = -1;
  407. char *p;
  408. u8 i = 0;
  409. p= (char *)buf;
  410. while ( '\r' == *p || '\n' == *p)
  411. {
  412. p++;
  413. }
  414. for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
  415. {
  416. if (strstr( p,rep_str[i]))
  417. {
  418. res = i;
  419. break;
  420. }
  421. }
  422. switch (res)
  423. {
  424. case 0: // OK
  425. {
  426. //s_at_status = ATC_RSP_FINISH;
  427. }
  428. break;
  429. case 1: // ERROR
  430. {
  431. // s_at_status = ATC_RSP_FINISH;
  432. }
  433. break;
  434. case 2:
  435. {
  436. s_at_status = ATC_RSP_FINISH;
  437. }
  438. break;
  439. case 3: // +IP ERROR
  440. {
  441. s_at_status = ATC_RSP_FINISH;
  442. }
  443. break;
  444. default:
  445. break;
  446. }
  447. return s_at_status;
  448. }
  449. static s8 tcp_connect_handle(BOOL *urc, char*buf)
  450. {
  451. char *rep_str[ ] = {"+CCHOPEN", "ERROR", "OK"};
  452. s8 res = -1;
  453. char *p;
  454. u8 i = 0;
  455. p= (char *)buf;
  456. while ( '\r' == *p || '\n' == *p)
  457. {
  458. p++;
  459. }
  460. for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
  461. {
  462. if (strstr( p,rep_str[i]))
  463. {
  464. res = i;
  465. break;
  466. }
  467. }
  468. switch (res)
  469. {
  470. case 0: // CONNECT OK.
  471. {
  472. if(tcp_connect_cb != NULL)
  473. {
  474. tcp_connect_cb(0, 0);
  475. }
  476. s_at_status = ATC_RSP_FINISH;
  477. }
  478. break;
  479. case 1: // ERROR
  480. {
  481. //tcp_connect_cb(conn, 1);
  482. s_at_status = ATC_RSP_FINISH;
  483. }
  484. break;
  485. case 2:
  486. {
  487. }
  488. break;
  489. default:
  490. break;
  491. }
  492. return s_at_status;
  493. }
  494. s8 ipsend_handle(BOOL *urc, char*buf)
  495. {
  496. char *rep_str[ ] = {"+CCHSEND:", "ERROR", ">", "OK", "+CCH_PEER_CLOSED:"};
  497. s8 res = -1;
  498. char *p;
  499. u8 i = 0;
  500. p= (char *)buf;
  501. while ( '\r' == *p || '\n' == *p)
  502. {
  503. p++;
  504. }
  505. for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
  506. {
  507. if (strstr( p,rep_str[i]))
  508. {
  509. res = i;
  510. break;
  511. }
  512. }
  513. switch (res)
  514. {
  515. case 0: //+CCHSEND:
  516. {
  517. if(module_send_data_buffer != NULL)
  518. {
  519. if(module_send_data_buffer->buffer != NULL)
  520. {
  521. free(module_send_data_buffer->buffer);
  522. module_send_data_buffer->buffer = NULL;
  523. }
  524. }
  525. free(module_send_data_buffer);
  526. module_send_data_buffer = NULL;
  527. s_at_status = ATC_RSP_FINISH;
  528. if(tcp_sent_cb != NULL)
  529. {
  530. tcp_sent_cb(0,0);
  531. }
  532. }
  533. break;
  534. case 1: //ERROR
  535. {
  536. if(tcp_sent_cb != NULL)
  537. {
  538. tcp_sent_cb(0,-1);
  539. }
  540. s_at_status = ATC_RSP_FINISH;
  541. }
  542. break;
  543. case 2: // >
  544. {
  545. struct fifo_data* tcp_data_buffer = fifo_get_data(&s_at_fifo);
  546. //printf("sending.....,%p,\n", module_send_data_buffer);
  547. if(tcp_data_buffer != NULL)
  548. {
  549. Usart2_Send_Data(tcp_data_buffer->buf, tcp_data_buffer->length);
  550. if(tcp_data_buffer->buf != NULL)
  551. {
  552. free(tcp_data_buffer->buf);
  553. tcp_data_buffer->buf = NULL;
  554. }
  555. free(tcp_data_buffer);
  556. tcp_data_buffer = NULL;
  557. }
  558. }
  559. break;
  560. case 3: //OK
  561. {
  562. }
  563. break;
  564. case 4: //+CCH_PEER_CLOSED
  565. {
  566. if(tcp_sent_cb != NULL)
  567. {
  568. tcp_sent_cb(0,-1);
  569. }
  570. s_at_status = ATC_RSP_FINISH;
  571. }
  572. break;
  573. default:
  574. break;
  575. }
  576. return s_at_status;
  577. }
  578. static void show_package(u8 *buf, u16 len) {
  579. int i = 0;
  580. PRINTF("Package length: %d\ncontent is: \n", len);
  581. for (i = 0; i < len; i++)
  582. {
  583. PRINTF("%02x ",(uint8_t)buf[i]);
  584. }
  585. PRINTF("\n");
  586. }
  587. static int8_t cch_open_handle(bool *urc, char*buf)
  588. {
  589. // TODO: deal with urc
  590. char *rep_str[ ] = {"+CCHOPEN", "OK", "ERROR"};
  591. int8_t res = -1;
  592. char *p;
  593. uint8_t i = 0;
  594. p= (char *)buf;
  595. while ( '\r' == *p || '\n' == *p)
  596. {
  597. p++;
  598. }
  599. for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
  600. {
  601. if (strstr( p,rep_str[i]))
  602. {
  603. res = i;
  604. break;
  605. }
  606. }
  607. switch (res)
  608. {
  609. case 0: // +CCHOPEN
  610. {
  611. s_at_status = ATC_RSP_FINISH;
  612. }
  613. break;
  614. case 1: // OK
  615. {
  616. }
  617. break;
  618. case 2:
  619. {
  620. s_at_status = ATC_RSP_FINISH;
  621. }
  622. break;
  623. default:
  624. break;
  625. }
  626. return s_at_status;
  627. }
  628. static int8_t cch_send_handle(bool *urc, char*buf)
  629. {
  630. char *rep_str[ ] = {"+CCHRECV", ">", "OK", "ERROR", "+CCHSEND:", "+CCH_PEER_CLOSED:"};
  631. int8_t res = -1;
  632. char *p;
  633. uint8_t i = 0;
  634. static uint16_t s_http_data_len = 0;
  635. p= (char *)buf;
  636. while ( '\r' == *p || '\n' == *p)
  637. {
  638. p++;
  639. }
  640. for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
  641. {
  642. if (strstr( p,rep_str[i]))
  643. {
  644. res = i;
  645. break;
  646. }
  647. }
  648. switch (res)
  649. {
  650. case 0: // +HTTPSRECV:
  651. {
  652. // check response length.
  653. p = strstr(p, "+CCHRECV: DATA,1,");
  654. p = p + strlen("+CCHRECV: DATA,1,");
  655. int response_len = atoi(p);
  656. if(response_len>1000)
  657. {
  658. printf("response size is illegal:%d!\n", response_len);
  659. if(s_http_cb != NULL)
  660. {
  661. s_http_cb(NULL);
  662. }
  663. s_at_status = ATC_RSP_FINISH;
  664. return -1;
  665. }
  666. p = strstr(p, "\r\n");
  667. p = p + strlen("\r\n");
  668. memcpy(s_http_buffer + s_http_data_len, p, response_len);
  669. s_http_data_len += response_len;
  670. }
  671. break;
  672. case 1: // >
  673. {
  674. struct fifo_data* http_post_data;
  675. http_post_data = fifo_get_data(&s_at_fifo);
  676. if(http_post_data != NULL)
  677. {
  678. printf("sending.....\n");
  679. usart2_send(http_post_data->buf, http_post_data->length);
  680. if(http_post_data->buf != NULL)
  681. {
  682. myfree(http_post_data->buf);
  683. http_post_data->buf = NULL;
  684. }
  685. myfree(http_post_data);
  686. http_post_data = NULL;
  687. }
  688. }
  689. break;
  690. case 2:
  691. {
  692. }
  693. break;
  694. case 3:
  695. {
  696. //TODO: consider error situation.
  697. s_at_status = ATC_RSP_FINISH;
  698. }
  699. break;
  700. case 4:
  701. {
  702. }
  703. break;
  704. case 5:
  705. {
  706. // check http protocol.
  707. char* http_response_buffer = NULL;
  708. const char * version = "HTTP/1.1 ";
  709. printf("receive:%s\n", s_http_buffer);
  710. p = strstr(s_http_buffer, version);
  711. if (p == NULL)
  712. {
  713. printf("Invalid version in %s\n", p);
  714. if(s_http_cb != NULL)
  715. {
  716. s_http_cb(NULL);
  717. }
  718. s_at_status = ATC_RSP_FINISH;
  719. return -1;
  720. }
  721. // check http status.
  722. int http_status = atoi(p + strlen(version));
  723. if(http_status != 200)
  724. {
  725. printf("Invalid version in %s\n", p);
  726. if(s_http_cb != NULL)
  727. {
  728. s_http_cb(NULL);
  729. }
  730. s_at_status = ATC_RSP_FINISH;
  731. return -1;
  732. }
  733. http_response_buffer = (char *)strstr(p, "\r\n\r\n") + 4;
  734. s_http_data_len = 0;
  735. s_at_status = ATC_RSP_FINISH;
  736. if(s_http_cb != NULL)
  737. {
  738. s_http_cb(http_response_buffer);
  739. }
  740. }
  741. default:
  742. break;
  743. }
  744. return s_at_status;
  745. }
  746. static s8 http_send_handler(BOOL *urc, char *buf) {
  747. char *rep_str[] = {"+CHTTPACT: REQUEST", "+CHTTPACT: DATA,", "ERROR", "+CHTTPACT: 0"};
  748. int8_t res = -1;
  749. char *p;
  750. uint8_t i = 0;
  751. static uint16_t s_http_data_len = 0;
  752. p= (char *)buf;
  753. while ( '\r' == *p || '\n' == *p)
  754. {
  755. p++;
  756. }
  757. for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
  758. {
  759. if (strstr( p,rep_str[i]))
  760. {
  761. res = i;
  762. break;
  763. }
  764. }
  765. switch (res)
  766. {
  767. case 0:
  768. {
  769. struct fifo_data* http_post_data;
  770. http_post_data = fifo_get_data(&s_at_fifo);
  771. if(http_post_data != NULL)
  772. {
  773. PRINTF("sending Data:%s\r\n", http_post_data->buf);
  774. Usart2_Send_Data(http_post_data->buf, http_post_data->length);
  775. if(http_post_data->buf != NULL)
  776. {
  777. free(http_post_data->buf);
  778. http_post_data->buf = NULL;
  779. }
  780. free(http_post_data);
  781. http_post_data = NULL;
  782. }
  783. }
  784. break;
  785. case 1: // get response
  786. {
  787. p = strstr(p, "+CHTTPACT: DATA,");
  788. p = p + strlen("+CHTTPACT: DATA,");
  789. int response_len = atoi(p);
  790. PRINTF("response size is :%d!\n", response_len);
  791. if(response_len>10000)
  792. {
  793. //printf("response size is illegal:%d!\n", response_len);
  794. if(s_http_cb != NULL)
  795. {
  796. s_http_cb(NULL);
  797. }
  798. s_at_status = ATC_RSP_WAIT;
  799. return s_at_status;
  800. }
  801. p = strstr(p, "\r\n");
  802. p = p + strlen("\r\n");
  803. memcpy(s_http_buffer + s_http_data_len, p,response_len);
  804. s_http_data_len += response_len;
  805. }
  806. break;
  807. case 2: //error
  808. s_at_status = ATC_RSP_FINISH;
  809. break;
  810. case 3:
  811. {
  812. char *http_response_buffer = NULL;
  813. const char *version = "http/1.1 ";
  814. PRINTF("http recive:%s\n", s_http_buffer);
  815. p = strstr(s_http_buffer, version);
  816. if (p == NULL) {
  817. PRINTF("Invalid version in %s\n", p);
  818. if(s_http_cb != NULL) {
  819. s_http_cb(NULL);
  820. }
  821. s_at_status = ATC_RSP_WAIT;
  822. return s_at_status;
  823. }
  824. // check http response code
  825. int http_status = atoi(p + strlen(version));
  826. if(http_status != 200) {
  827. PRINTF("response code error is %d, not 200", http_status);
  828. if(s_http_cb != NULL) {
  829. s_http_cb(NULL);
  830. }
  831. s_at_status = ATC_RSP_WAIT;
  832. return s_at_status;
  833. }
  834. http_response_buffer = (char *)strstr(p, "\r\n\r\n") + 4;
  835. PRINTF("get http response:%s", http_response_buffer);
  836. s_http_data_len = 0;
  837. s_at_status = ATC_RSP_FINISH;
  838. if(s_http_cb != NULL)
  839. {
  840. s_http_cb(http_response_buffer);
  841. }
  842. }
  843. break;
  844. default:
  845. break;
  846. }
  847. return s_at_status;
  848. }
  849. u8 inquire_signal_quality(void)
  850. {
  851. add_send_at_command("AT+CSQ", "AT+CSQ\r\n");
  852. return s_csq_value;
  853. }
  854. // void module_tcp_connect(u16 fd, uint32_t ip, u16 port)
  855. // {
  856. // char connect_buf[80];
  857. // u8 ip1, ip2, ip3, ip4;
  858. // ip1 = ((u8*)(&ip))[0];
  859. // ip2 = ((u8*)(&ip))[1];
  860. // ip3 = ((u8*)(&ip))[2];
  861. // ip4 = ((u8*)(&ip))[3];
  862. // // AT+CCHSET.
  863. // add_send_at_command("AT+CCHSET", "AT+CCHSET=1\r\n");
  864. // // AT+CCHSTART.
  865. // add_send_at_command("AT+CCHSTART", "AT+CCHSTART\r\n");
  866. // // AT+CCHOPEN.
  867. // sprintf(connect_buf, "AT+CCHOPEN=%d,\"%d.%d.%d.%d\",%d, 2\r\n", 1, ip1, ip2, ip3, ip4, port);
  868. // //printf("tcp connecting:%s\n", connect_buf);
  869. // add_send_at_command("AT+CCHOPEN_TLS", connect_buf);
  870. // }
  871. static s8 tcp_closed_handle(BOOL *urc, char *buf)
  872. {
  873. if(tcp_disconnected_cb != NULL)
  874. {
  875. tcp_disconnected_cb(0, -1);
  876. }
  877. return -1;
  878. }
  879. static s8 tcp_disconnect_handle(BOOL *urc, char *buf)
  880. {
  881. if(tcp_disconnected_cb != NULL)
  882. {
  883. tcp_disconnected_cb(0, -1);
  884. }
  885. return -1;
  886. }
  887. void register_module_tcp_connect_callback(u16 fd, module_tcp_connected_callback connect_cb)
  888. {
  889. tcp_connect_cb = connect_cb;
  890. }
  891. void register_module_tcp_sent_callback(u16 fd, module_tcp_sent_callback sent_callback)
  892. {
  893. tcp_sent_cb = sent_callback;
  894. }
  895. void register_module_tcp_recv_callback(u16 fd, module_tcp_recv_callback recv_callback)
  896. {
  897. tcp_recv_cb = recv_callback;
  898. }
  899. void register_module_tcp_disconnected_callback(u16 fd, module_tcp_disconnected_callback tcp_disconnected_callback)
  900. {
  901. tcp_disconnected_cb = tcp_disconnected_callback;
  902. }
  903. // fifo 定时检测
  904. static s8 at_fifo_check(void *arg)
  905. {
  906. static s8 wait_response_tick = 0;
  907. if(s_module_status == MODULE_START)
  908. {
  909. PRINTF("start send AT\r\n")
  910. send_at_command("AT\r\n");
  911. }
  912. else if(MODULE_INIT ==s_module_status|| MODULE_INIT_DONE == s_module_status)
  913. {
  914. if ((NULL == s_current_at_command))
  915. {
  916. if ((!FIFO_isEmpty(&s_at_fifo)) &&(s_at_status == ATC_RSP_FINISH))
  917. {
  918. s_current_at_command = (struct at_cmd_entity*)malloc(sizeof(struct at_cmd_entity));
  919. if (FIFO_Get(&s_at_fifo, s_current_at_command->at_name, s_current_at_command->at_cmd))
  920. {
  921. s_at_status = ATC_RSP_WAIT;
  922. send_at_command(s_current_at_command->at_cmd);
  923. }
  924. }
  925. else
  926. {
  927. PRINTF("fifo is empty or the previous at command not complete!\r\n");
  928. }
  929. }
  930. else
  931. {
  932. // PRINTF("sending:%s", s_current_at_command->at_cmd);
  933. //send
  934. //send_at_command(s_current_at_command->at_cmd);
  935. if(wait_response_tick > (s8)AT_MAX_RESPONSE_TIME)
  936. {
  937. }
  938. wait_response_tick++;
  939. }
  940. }
  941. return 0;
  942. }
  943. // 模块启动
  944. u8 module_system_start(void)
  945. {
  946. s_module_status = MODULE_START;
  947. timer3_init(1000, 1, at_fifo_check);
  948. timer3_start();
  949. return s_module_status;
  950. }
  951. // 模块初始化
  952. u8 module_system_init()
  953. {
  954. Usart_Send_Str_Data("module_init!!!\r\n");
  955. FIFO_Init (&s_at_fifo);
  956. add_send_at_command("ATE", "ATE0\r\n");
  957. //AT+IFC=0, set no flow control.
  958. //add_send_at_command("AT+IFC", "AT+IFC=0\r\n");
  959. //AT+CPIN, query .....
  960. add_send_at_command("AT+CPIN", "AT+CPIN?\r\n");
  961. //AT+CSQ
  962. add_send_at_command("AT+CSQ", "AT+CSQ\r\n");
  963. //AT+GSN request for the IMEI of the module.;
  964. add_send_at_command("AT+CGSN", "AT+CGSN\r\n");
  965. //AT+CREG
  966. //add_send_at_command("AT+CREG", "AT+CREG?\r\n");
  967. // AT+CGREG?
  968. //add_send_at_command("AT+CGREG", "AT+CGREG?\r\n");
  969. //AT+CIPMODE
  970. add_send_at_command("AT+CIPMODE", "AT+CIPMODE=0\r\n");
  971. //AT+NETCLOSE关闭上次开启的网络
  972. add_send_at_command("AT+NETCLOSE", "AT+NETCLOSE\r\n");
  973. //AT+NETOPEN开启网络
  974. add_send_at_command("AT+NETOPEN", "AT+NETOPEN\r\n");
  975. //AT+IPADDR获取模块IP地址
  976. //add_send_at_command("AT+IPADDR", "AT+IPADDR\r\n");
  977. s_module_status = MODULE_INIT;
  978. return s_module_status;
  979. }
  980. // 获取模块状态
  981. s8 get_module_status()
  982. {
  983. return s_module_status;
  984. }
  985. // 设置模块 状态
  986. void set_module_status(s8 status)
  987. {
  988. s_module_status = status;
  989. }
  990. void add_send_at_command(char *name_buffer, char *cmd_buffer)
  991. {
  992. char at_cmd_Buff[64] = {0};
  993. char at_name_Buff[20] = {0};
  994. strcpy(at_name_Buff, name_buffer);
  995. strcpy(at_cmd_Buff, cmd_buffer);
  996. if(FIFO_Put(&s_at_fifo, at_name_Buff, at_cmd_Buff) ==-1)
  997. {
  998. PRINTF("write fifo error!\r\n");
  999. }
  1000. }
  1001. // 模块发送http post 请求
  1002. void module_http_post(const char *url, const char *data, module_http_callback http_cb)
  1003. {
  1004. s_http_cb = http_cb;
  1005. char host_name[64] = "";
  1006. char http_path[64] = "";
  1007. int port = 80;
  1008. BOOL is_http = strncmp(url, "http://", strlen("http://")) == 0;
  1009. BOOL is_https = strncmp(url, "https://", strlen("https://")) == 0;
  1010. if (is_http)
  1011. url += strlen("http://"); // Get rid of the protocol.
  1012. else if (is_https)
  1013. {
  1014. url += strlen("https://"); // Get rid of the protocol.
  1015. }
  1016. else
  1017. {
  1018. PRINTF("url is not http:// or https://")
  1019. return;
  1020. }
  1021. char * path = strchr(url, '/');
  1022. if (path == NULL)
  1023. {
  1024. path = strchr(url, '\0'); // Pointer to end of string.
  1025. }
  1026. char *colon = strchr(url, ':');
  1027. if(colon > path) {
  1028. colon = NULL;
  1029. }
  1030. if (colon == NULL) {
  1031. memcpy(host_name, url, path - url);
  1032. } else {
  1033. port = atoi(colon + 1);
  1034. if (port == 0) {
  1035. //printf("Port error %s\n", url);
  1036. return;
  1037. }
  1038. memcpy(host_name, url, colon - url);
  1039. host_name[colon - url] = '\0';
  1040. }
  1041. host_name[path - url] = '\0';
  1042. memcpy(http_path, path, strlen(url)- strlen(host_name));
  1043. PRINTF("host_name:%s\n", host_name);
  1044. PRINTF("http_path:%s\n", http_path);
  1045. PRINTF("http_port:%d\n", port);
  1046. // AT+CCHSET.
  1047. add_send_at_command("AT+CCHSET", "AT+CCHSET=1\r\n");
  1048. // AT+CCHSTART.
  1049. add_send_at_command("AT+CCHSTART", "AT+CCHSTART\r\n");
  1050. char http_url[64];
  1051. memset(http_url, 0x0, sizeof(http_url));
  1052. sprintf(http_url, "AT+CCHOPEN=%d,\"%s\",%d,1\r\n", 1, host_name, port);
  1053. PRINTF("ready to http post:%s", http_url);
  1054. add_send_at_command("AT+CCHOPEN", http_url);
  1055. char post_headers[128] = "";
  1056. char at_send_buffer[20] = "";
  1057. sprintf(post_headers,
  1058. "Content-Type:"
  1059. HTTP_HEADER_CONTENT_TYPE
  1060. "\r\n"
  1061. "Content-Length: %d\r\n", strlen(data));
  1062. struct fifo_data* http_post_data =(struct fifo_data*)malloc(sizeof(struct fifo_data));
  1063. http_post_data->buf =(uint8_t*)malloc(512);
  1064. int len = sprintf((char*)http_post_data->buf,
  1065. "POST %s HTTP/1.1\r\n"
  1066. "Host: %s:%d\r\n"
  1067. "Connection: close\r\n"
  1068. "User-Agent: SIM7600\r\n"
  1069. "%s"
  1070. "\r\n%s",
  1071. http_path, host_name, port, post_headers, data);
  1072. http_post_data->length = len;
  1073. PRINTF("http post:%s", http_post_data->buf);
  1074. sprintf(at_send_buffer, "AT+CCHSEND=%d,%d\r\n", 1, len);
  1075. add_send_at_command("AT+CCHSEND", at_send_buffer);
  1076. fifo_put_data(&s_at_fifo, http_post_data);
  1077. // AT+CCHCLOSE
  1078. add_send_at_command("AT+CCHCLOSE", "AT+CCHCLOSE=1\r\n");
  1079. // AT+CCHSTOP
  1080. add_send_at_command("AT+CCHSTOP", "AT+CCHSTOP\r\n");
  1081. }
  1082. static s8 data_process(struct module_buf *buf)
  1083. {
  1084. struct module_buf *data_buf = buf;
  1085. u8 *p = data_buf->buf;
  1086. if(strncmp((char*)data_buf->buf, "http/1.1", strlen("http/1.1")) == 0)
  1087. {
  1088. memcpy(s_http_buffer, p, data_buf->length);
  1089. return 1;
  1090. }
  1091. else
  1092. {
  1093. return 0;
  1094. }
  1095. }
  1096. // 模块返回数据处理
  1097. s8 module_data_handler(void* data)
  1098. {
  1099. u8 urc = 0;
  1100. struct module_buf* module_data = (struct module_buf*)data;
  1101. if(module_data->length < 3)
  1102. {
  1103. PRINTF("module response not enough length!\r\n");
  1104. if(module_data != NULL)
  1105. {
  1106. if(module_data->buf != NULL)
  1107. {
  1108. free(module_data->buf);
  1109. module_data->buf = NULL;
  1110. }
  1111. free(module_data);
  1112. module_data = NULL;
  1113. }
  1114. return -1;
  1115. }
  1116. if(MODULE_START == s_module_status)
  1117. {
  1118. if((strstr((char*)module_data->buf, "OK")))
  1119. {
  1120. Usart_Send_Str_Data("the \"at\" cmd response \"OK\"\r\n");
  1121. s_module_status = MODULE_SYNC;
  1122. }
  1123. }
  1124. if (MODULE_INIT ==s_module_status|| MODULE_INIT_DONE == s_module_status)
  1125. {
  1126. int i = 0;
  1127. if (NULL != s_current_at_command)
  1128. {
  1129. for (i = 0; i <sizeof(atCmdTable)/sizeof(atCmdTable[0]); i++)
  1130. {
  1131. if(!strcmp(s_current_at_command->at_name, atCmdTable[i].name))
  1132. {
  1133. s_at_status =atCmdTable[i].at_cmd_handler(&urc, (char*)module_data->buf);
  1134. if (ATC_RSP_FINISH ==s_at_status)
  1135. {
  1136. free(s_current_at_command);
  1137. s_current_at_command = NULL;
  1138. }
  1139. break;
  1140. }
  1141. }
  1142. }
  1143. else
  1144. {
  1145. // http response
  1146. if(data_process(module_data)==1)
  1147. {
  1148. }
  1149. }
  1150. }
  1151. if(module_data != NULL)
  1152. {
  1153. Usart_Send_Str_Data("free\r\n");
  1154. if(module_data->buf != NULL)
  1155. {
  1156. free(module_data->buf);
  1157. module_data->buf = NULL;
  1158. }
  1159. free(module_data);
  1160. module_data = NULL;
  1161. }
  1162. return 0;
  1163. }