sim7600.c 31 KB

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