sim7600.c 31 KB

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