123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395 |
- #include "sim7600.h"
- #include <stdio.h>
- #include <string.h>
- #include "fifo.h"
- #include "usart.h"
- #include "stdlib.h"
- #include "timer4.h"
- #define ATC_RSP_FINISH 1
- #define ATC_RSP_WAIT 0
- #define AT_MAX_RESPONSE_TIME 300
- #define HTTP_HEADER_CONTENT_TYPE "application/json"
- #define MAX_HTTP_SIZE 1000
- u8 g_iemi_buf[16];
- struct module_buf {
- u8 *buf;
- u16 length;
- };
- static struct module_buf *module_send_data_buffer = NULL;
- static module_tcp_connected_callback tcp_connect_cb = NULL;
- static module_tcp_sent_callback tcp_sent_cb = NULL;
- static module_tcp_recv_callback tcp_recv_cb = NULL;
- static module_tcp_disconnected_callback tcp_disconnected_cb = NULL;
- static module_http_callback s_http_cb = NULL;
- static u16 send_data_len = 0;
- static char s_http_buffer[MAX_HTTP_SIZE];
- typedef s8 (*AT_cmdHandle)(BOOL *urc, char* buf);
- // 信号强度
- static u8 s_csq_value = 0;
- typedef struct {
- char *name;
- AT_cmdHandle at_cmd_handler;
- } AtcHandlerType;
- static s8 at_handler(BOOL *urc, char* buf);
- static s8 ate_handler(BOOL *urc, char* buf);
- static s8 csq_handler(BOOL *urc, char* buf);
- static s8 cpin_handler(BOOL *urc, char *buf);
- //static s8 reg_handler(BOOL *urc, char *buf);
- //static s8 creg_handler(BOOL *urc, char *buf);
- static s8 getip_handler(BOOL *urc, char *buf);
- // net open handler
- static s8 net_open_handler(BOOL *urc, char* buf);
- static s8 net_close_handler(BOOL *urc, char* buf);
- static s8 gsn_handler(BOOL *urc, char* buf);
- // http handler
- static s8 http_send_handler(BOOL *urc, char*buf);
- static s8 http_read_handler(BOOL *urc, char*buf);
- static s8 cch_open_handle(BOOL *urc, char*buf);
- static s8 cch_send_handle(BOOL *urc, char*buf);
- // tcp handle.
- static s8 tcp_connect_handle(BOOL *urc, char*buf);
- static s8 ipsend_handle(BOOL *urc, char*buf);
- static s8 urc_handle(BOOL *urc, char *buf);
- static s8 tcp_closed_handle(BOOL *urc, char *buf);
- static s8 tcp_disconnect_handle(BOOL *urc, char *buf);
- static s8 urc_process(struct module_buf* data);
- // AT Command table
- AtcHandlerType atCmdTable[ ] = {
- // 打开或关闭AT指令回显
- {"ATE", ate_handler},
- // 设置本地流量控制
- {"AT+IFC", at_handler},
- // 输入PIN码
- {"AT+CPIN", cpin_handler},
- // 查询信号强度
- {"AT+CSQ", csq_handler},
- // 获取序列号ID
- {"AT+CGSN", gsn_handler},
- // 设置 TCP/IP 应用模式
- // 0: 非透传
- // 1: 命令模式
- {"AT+CIPMODE", at_handler},
- // 打开SOCKET
- {"AT+NETOPEN", net_open_handler},
- {"AT+NETCLOSE", net_close_handler},
- // 查询注册网络状态
- {"AT+CREG", at_handler},
- // 注册 信息
- {"AT+CPSI?", at_handler},
- {"AT+IPADDR", getip_handler},
- // http
- {"AT+CHTTPACT", http_send_handler},
- // tcp
- // 建立tcp连接
- {"AT+CIPOPEN", tcp_connect_handle},
- // 向远端tcp或udp连接发送数据
- {"AT+CIPSEND", ipsend_handle},
- // 关闭tcp或ucp连接
- {"AT+CIPCLOSE", at_handler},
- // 设置收到消息时是否显示远程IP和端口
- {"AT+CIPSRIP", at_handler},
- // 在CIPCLOSE指令后调用,关闭所有网络链接
- {"AT+CIPCLOSE", at_handler},
-
- {"AT+NETCLOSE", at_handler},
- {"AT+CCHSET", at_handler},
- {"AT+CCHSTART", at_handler},
- {"AT+CCHOPEN", cch_open_handle},
- {"AT+CCHSEND", cch_send_handle},
- {"AT+CCHCLOSE", at_handler}, // TODO: consider response after OK.
- {"AT+CCHSTOP", at_handler},
- };
- AtcHandlerType urc_table[] = {
- {"+IPCLOSE:", tcp_disconnect_handle},
- {"+CPIN: READY", urc_handle},
- {"+STIN:", urc_handle},
- {"RDY", urc_handle},
- {"OPL UPDATING", urc_handle},
- {"PNN UPDATING", urc_handle},
- {"SMS DONE", urc_handle},
- {"PB DONE", urc_handle},
- };
- struct at_cmd_entity {
- char at_name[20];
- char at_cmd[128];
- };
- static s8 s_module_status = MODULE_OFF_LINE;
- static struct FIFO s_at_fifo;
- static u8 s_at_status = ATC_RSP_FINISH;
- static struct at_cmd_entity *s_current_at_command = NULL;
- // 发送at指令
- static void send_at_command(const char* cmd) {
- Usart2_Send_Data((u8*)cmd, strlen(cmd));
- }
- // urc 处理函数
- static s8 urc_handle(BOOL *urc, char* buf) {
- PRINTF("urc handler: %s\n", buf);
- return 0;
- }
- // at指令返回处理函数
- static s8 at_handler(BOOL *urc, char *buf) {
- char *rep_str[] = {"OK", "ERROR", "+CCHSTART: 0"};
- s8 res = -1;
- char *p;
- u8 i = 0;
- p = (char *)buf;
- while ( 'r' == *p || '\n' == *p) {
- p++;
- }
- for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
- {
- if (strstr( p,rep_str[i]))
- {
- res = i;
- break;
- }
- }
- switch (res)
- {
- case 0:
- s_at_status = ATC_RSP_FINISH;
- break;
- case 1:
- s_at_status = ATC_RSP_FINISH;
- break;
- case 2:
- s_at_status = ATC_RSP_FINISH;
- break;
- default:
- break;
- }
- return s_at_status;
- }
- void showpackage(uint8_t *buffer, uint16_t length)
- {
- int i = 0;
- PRINTF("Package length: %d\ncontent is: \n", length);
- for (i = 0; i < length; i++)
- {
- PRINTF("%02x ",(uint8_t)buffer[i]);
- }
- PRINTF("\n");
- }
- // CPIN指令返回处理函数
- static s8 cpin_handler(BOOL *urc, char *buf) {
- char *rep_str[] = {"OK", "ERROR", "+CPIN: "};
- s8 res = -1;
- char *p;
- u8 i = 0;
- p = (char *)buf;
- while ( 'r' == *p || '\n' == *p) {
- p++;
- }
- for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
- {
- if (strstr( p,rep_str[i]))
- {
- res = i;
- break;
- }
- }
- switch (res)
- {
- case 0:
- s_at_status = ATC_RSP_FINISH;
- break;
- case 1:
- //s_at_status = ATC_RSP_FINISH;
- break;
- case 2:
- // 返回ready说明正常
- if(strstr(p, "READY")!=NULL)
- {
- s_at_status = ATC_RSP_FINISH;
- }
- break;
- default:
- break;
- }
- return s_at_status;
- }
- static s8 csq_handler(BOOL *urc, char *buf) {
- char *rep_str [] = {"OK", "ERROR", "+CSQ: "};
- s8 res = -1;
- char *p;
- u8 i = 0;
- p= (char *)buf;
- while ( '\r' == *p || '\n' == *p)
- {
- p++;
- }
- for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
- {
- if (strstr( p,rep_str[i]))
- {
- res = i;
- break;
- }
- }
- switch (res)
- {
- case 0:
- p = strstr(p, "+CSQ: ");
- s_csq_value = atoi(p + strlen("+CSQ: "));
- s_at_status = ATC_RSP_FINISH;
- break;
- case 1:
- s_at_status = ATC_RSP_FINISH;
- break;
- case 2:
- PRINTF("csq:%s\r\n", p);
- s_csq_value = atoi(p + sizeof("+CSQ: "));
- s_at_status = ATC_RSP_FINISH;
- break;
- default:
- break;
- }
- return s_at_status;
- }
- static s8 getip_handler(BOOL *urc, char *buf) {
- char *rep_str[ ] = {"OK", "ERROR", "+IPADDR: ", "+IP ERROR: "};
- s8 res = -1;
- char *p;
- u8 i = 0;
- p= (char *)buf;
- while ( '\r' == *p || '\n' == *p)
- {
- p++;
- }
- for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
- {
- if (strstr( p,rep_str[i]))
- {
- res = i;
- break;
- }
- }
- switch (res)
- {
- case 0: // OK
- {
- s_at_status = ATC_RSP_FINISH;
- s_module_status = MODULE_GET_IP;
- }
- break;
- case 1: // ERROR
- {
- }
- break;
- case 2:
- {
- s_at_status = ATC_RSP_FINISH;
- s_module_status = MODULE_GET_IP;
- }
- break;
- case 3: // +IP ERROR:
- {
- PRINTF("Net\r\n");
- }
- break;
- default:
- break;
- }
- return s_at_status;
- }
- static s8 ate_handler(BOOL *urc, char* buf)
- {
- char *rep_str[ ] = {"OK","ERROR", "ATE"};
- s8 res = -1;
- char *p;
- u8 i = 0;
- p= (char *)buf;
- while ( '\r' == *p || '\n' == *p)
- {
- p++;
- }
- for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
- {
- if (strstr( p,rep_str[i]))
- {
- res = i;
- break;
- }
- }
- switch (res)
- {
- case 0: // OK
- {
- s_at_status = ATC_RSP_FINISH;
- }
- break;
- case 1: // ERROR
- {
- s_at_status = ATC_RSP_FINISH;
- }
- break;
- case 2: // ATE
- {
- //printf("open");
- }
- break;
- default:
- break;
- }
- return s_at_status;
- }
- static s8 gsn_handler(BOOL *urc, char*buf)
- {
- char *rep_str[ ] = {"OK","ERROR"};
- s8 res = -1;
- char *p;
- u8 i = 0;
- static u8 flag = 0;
- p= (char *)buf;
- while ( '\r' == *p || '\n' == *p)
- {
- p++;
- }
- if(flag == 0)
- {
- memcpy(g_iemi_buf, p, 15);
- g_iemi_buf[15] = 0;
- PRINTF("g_iemi_buf:%s", g_iemi_buf);
- flag = 1;
- }
- for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
- {
- if (strstr( p,rep_str[i]))
- {
- res = i;
- break;
- }
- }
- switch (res)
- {
- case 0: // OK
- {
- s_at_status = ATC_RSP_FINISH;
- }
- break;
- case 1: // ERROR
- {
- s_at_status = ATC_RSP_FINISH;
- }
- break;
- default:
- s_at_status = ATC_RSP_FINISH;
- break;
- }
- return s_at_status;
- }
- static s8 net_open_handler( BOOL *urc, char* buf)
- {
- char *rep_str[ ] = {"OK","ERROR", "+NETOPEN: "};
- s8 res = -1;
- char *p;
- u8 i = 0;
- p= (char *)buf;
- while ( '\r' == *p || '\n' == *p)
- {
- p++;
- }
- for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
- {
- if (strstr( p,rep_str[i]))
- {
- res = i;
- break;
- }
- }
- switch (res)
- {
- case 0: // OK
- {
- s_at_status = ATC_RSP_FINISH;
- s_module_status = MODULE_GET_IP;
- }
- break;
- case 1: // ERROR
- {
- s_at_status = ATC_RSP_FINISH;
- s_module_status = MODULE_GET_IP;
- }
- break;
- case 2:
- {
- s_at_status = ATC_RSP_FINISH;
- s_module_status = MODULE_GET_IP;
- }
- break;
- default:
- break;
- }
- return s_at_status;
- }
- static s8 net_close_handler( BOOL *urc, char* buf)
- {
- char *rep_str[ ] = {"OK","ERROR", "+NETCLOSE: ", "+IP ERROR: "};
- s8 res = -1;
- char *p;
- u8 i = 0;
- p= (char *)buf;
- while ( '\r' == *p || '\n' == *p)
- {
- p++;
- }
- for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
- {
- if (strstr( p,rep_str[i]))
- {
- res = i;
- break;
- }
- }
- switch (res)
- {
- case 0: // OK
- {
- s_at_status = ATC_RSP_FINISH;
- }
- break;
- case 1: // ERROR
- {
- s_at_status = ATC_RSP_FINISH;
- }
- break;
- case 2:
- {
- s_at_status = ATC_RSP_FINISH;
- }
- break;
- case 3: // +IP ERROR
- {
- s_at_status = ATC_RSP_FINISH;
- }
- break;
- default:
- break;
- }
- return s_at_status;
- }
- static s8 tcp_connect_handle(BOOL *urc, char*buf)
- {
- char *rep_str[ ] = {"+CIPOPEN: 1,", "ERROR", "OK"};
- s8 res = -1;
- char *p;
- u8 i = 0;
- p= (char *)buf;
- while ( '\r' == *p || '\n' == *p)
- {
- p++;
- }
- for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
- {
- if (strstr( p,rep_str[i]))
- {
- res = i;
- break;
- }
- }
- switch (res)
- {
- case 0: // CONNECT OK.
- {
- p = strstr(p, "+CIPOPEN: 1,");
- p = p + strlen("+CIPOPEN: 1,");
- int code = atoi(p);
- if(code==0)
- {
- if(tcp_connect_cb != NULL)
- {
- tcp_connect_cb(0, 0);
- }
-
- }
- s_at_status = ATC_RSP_FINISH;
- }
- break;
- case 1: // ERROR
- {
- tcp_connect_cb(0, -1);
- s_at_status = ATC_RSP_FINISH;
- }
- break;
- case 2:
- {
- }
- break;
- default:
- break;
- }
- return s_at_status;
- }
- // 这里遇到发CIPSEND后只返回oK,并不返回+CIPSEND:1,len
- s8 ipsend_handle(BOOL *urc, char*buf)
- {
- char *rep_str[ ] = {"+CIPSEND: ", "ERROR", ">", "OK", "+CIPERROR:"};
- s8 res = -1;
- char *p;
- u8 i = 0;
- p= (char *)buf;
- while ( '\r' == *p || '\n' == *p)
- {
- p++;
- }
- for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
- {
- if (strstr( p,rep_str[i]))
- {
- res = i;
- break;
- }
- }
- switch (res)
- {
- case 0: //+CCHSEND:
- {
- if(module_send_data_buffer != NULL)
- {
- if(module_send_data_buffer->buf != NULL)
- {
- free(module_send_data_buffer->buf);
- module_send_data_buffer->buf = NULL;
- }
- }
- free(module_send_data_buffer);
- module_send_data_buffer = NULL;
- s_at_status = ATC_RSP_FINISH;
- if(tcp_sent_cb != NULL)
- {
- tcp_sent_cb(0,0);
- }
- }
- break;
- case 1: //ERROR
- {
- if(tcp_sent_cb != NULL)
- {
- tcp_sent_cb(0,-1);
- }
- s_at_status = ATC_RSP_FINISH;
- }
- break;
- case 2: // >
- {
- struct fifo_data* tcp_data_buffer = fifo_get_data(&s_at_fifo);
- PRINTF("sending.....,%p,\n", tcp_data_buffer);
- if(tcp_data_buffer != NULL)
- {
- Usart2_Send_Data(tcp_data_buffer->buf, tcp_data_buffer->length);
- if(tcp_data_buffer->buf != NULL)
- {
- free(tcp_data_buffer->buf);
- tcp_data_buffer->buf = NULL;
- }
- free(tcp_data_buffer);
- tcp_data_buffer = NULL;
- }
- }
- break;
- case 3: //OK
- {
- s_at_status = ATC_RSP_FINISH;
- if(tcp_sent_cb != NULL)
- {
- tcp_sent_cb(0,0);
- }
- }
- break;
- case 4: //+CCH_PEER_CLOSED
- {
- if(tcp_sent_cb != NULL)
- {
- tcp_sent_cb(0,-1);
- }
- s_at_status = ATC_RSP_FINISH;
- }
- break;
- default:
- break;
- }
- return s_at_status;
- }
- static int8_t cch_open_handle(bool *urc, char*buf)
- {
- // TODO: deal with urc
- char *rep_str[ ] = {"+CCHOPEN", "OK", "ERROR"};
- int8_t res = -1;
- char *p;
- uint8_t i = 0;
- p= (char *)buf;
- while ( '\r' == *p || '\n' == *p)
- {
- p++;
- }
- for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
- {
- if (strstr( p,rep_str[i]))
- {
- res = i;
- break;
- }
- }
- switch (res)
- {
- case 0: // +CCHOPEN
- {
- s_at_status = ATC_RSP_FINISH;
- }
- break;
- case 1: // OK
- {
- }
- break;
- case 2:
- {
- s_at_status = ATC_RSP_FINISH;
- }
- break;
- default:
- break;
- }
- return s_at_status;
- }
- static int8_t cch_send_handle(bool *urc, char*buf)
- {
- char *rep_str[ ] = {"+CCHRECV", ">", "OK", "ERROR", "+CCHSEND:", "+CCH_PEER_CLOSED: "};
- int8_t res = -1;
- char *p;
- uint8_t i = 0;
- static uint16_t s_http_data_len = 0;
- p= (char *)buf;
- while ( '\r' == *p || '\n' == *p)
- {
- p++;
- }
- for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
- {
- if (strstr( p,rep_str[i]))
- {
- res = i;
- break;
- }
- }
- switch (res)
- {
- case 0: // +HTTPSRECV:
- {
- // check response length.
- p = strstr(p, "+CCHRECV: DATA,1,");
- p = p + strlen("+CCHRECV: DATA,1,");
- int response_len = atoi(p);
- if(response_len>1000)
- {
- PRINTF("response size is illegal:%d!\n", response_len);
- if(s_http_cb != NULL)
- {
- s_http_cb(NULL);
- }
- s_at_status = ATC_RSP_FINISH;
- return -1;
- }
- p = strstr(p, "\r\n");
- p = p + strlen("\r\n");
- memcpy(s_http_buffer + s_http_data_len, p, response_len);
- s_http_data_len += response_len;
- }
- break;
- case 1: // >
- {
- struct fifo_data* http_post_data;
- http_post_data = fifo_get_data(&s_at_fifo);
- if(http_post_data != NULL)
- {
- PRINTF("sending.....\n");
- showpackage(http_post_data->buf, http_post_data->length);
- Usart2_Send_Data(http_post_data->buf, http_post_data->length);
- if(http_post_data->buf != NULL)
- {
- free(http_post_data->buf);
- http_post_data->buf = NULL;
- }
- free(http_post_data);
- http_post_data = NULL;
- }
- }
- break;
- case 2:
- {
- }
- break;
- case 3:
- {
- //TODO: consider error situation.
- s_at_status = ATC_RSP_FINISH;
- }
- break;
- case 4:
- {
- }
- break;
- case 5:
- {
- // check http protocol.
- char* http_response_buffer = NULL;
- const char * version = "HTTP/1.1";
- PRINTF("http receive:%s\n", s_http_buffer);
- p = strstr(s_http_buffer, version);
- if (p == NULL)
- {
- PRINTF("Invalid version in %s\r\n", p);
- if(s_http_cb != NULL)
- {
- s_http_cb(NULL);
- }
- s_at_status = ATC_RSP_FINISH;
- return -1;
- }
- // check http status.
- int http_status = atoi(p + strlen(version));
- if(http_status != 200)
- {
- PRINTF("Invalid status:%d\r\n", http_status);
- if(s_http_cb != NULL)
- {
- s_http_cb(NULL);
- }
- s_at_status = ATC_RSP_FINISH;
- return -1;
- }
- http_response_buffer = (char *)strstr(p, "\r\n\r\n") + 4;
- s_http_data_len = 0;
- s_at_status = ATC_RSP_FINISH;
- if(s_http_cb != NULL)
- {
- s_http_cb(http_response_buffer);
- }
- }
- default:
- break;
- }
- free(buf);
- buf=NULL;
- return s_at_status;
- }
- static s8 http_send_handler(BOOL *urc, char *buf) {
- char *rep_str[] = {"+CHTTPACT: REQUEST", "+CHTTPACT: DATA,", "ERROR", "+CHTTPACT: 0"};
- int8_t res = -1;
- char *p;
- uint8_t i = 0;
- static uint16_t s_http_data_len = 0;
- p= (char *)buf;
- while ( '\r' == *p || '\n' == *p)
- {
- p++;
- }
- for (i = 0; i < sizeof(rep_str) / sizeof(rep_str[0]); i++)
- {
- if (strstr( p,rep_str[i]))
- {
- res = i;
- break;
- }
- }
- switch (res)
- {
- case 0:
- {
- struct fifo_data* http_post_data;
- http_post_data = fifo_get_data(&s_at_fifo);
- if(http_post_data != NULL)
- {
- PRINTF("sending Data:%s\r\n", http_post_data->buf);
- Usart2_Send_Data(http_post_data->buf, http_post_data->length);
- if(http_post_data->buf != NULL)
- {
- free(http_post_data->buf);
- http_post_data->buf = NULL;
- }
- free(http_post_data);
- http_post_data = NULL;
- }
- }
- break;
- case 1: // get response
- {
- p = strstr(p, "+CHTTPACT: DATA,");
- p = p + strlen("+CHTTPACT: DATA,");
- int response_len = atoi(p);
- PRINTF("response size is :%d!\n", response_len);
- if(response_len>10000)
- {
- //printf("response size is illegal:%d!\n", response_len);
- if(s_http_cb != NULL)
- {
- s_http_cb(NULL);
- }
- s_at_status = ATC_RSP_WAIT;
- return s_at_status;
- }
- p = strstr(p, "\r\n");
- p = p + strlen("\r\n");
- memcpy(s_http_buffer + s_http_data_len, p,response_len);
- s_http_data_len += response_len;
- }
- break;
- case 2: //error
- s_at_status = ATC_RSP_FINISH;
- break;
- case 3:
- {
- char *http_response_buffer = NULL;
- const char *version = "HTTP/1.1 ";
- PRINTF("http recive:%s\n", s_http_buffer);
- p = strstr(s_http_buffer, version);
- if (p == NULL) {
- PRINTF("Invalid version in %s\n", p);
- if(s_http_cb != NULL) {
- s_http_cb(NULL);
- }
- s_at_status = ATC_RSP_WAIT;
- return s_at_status;
- }
- // check http response code
- int http_status = atoi(p + strlen(version));
- if(http_status != 200) {
- PRINTF("response code error is %d, not 200", http_status);
- if(s_http_cb != NULL) {
- s_http_cb(NULL);
- }
- s_at_status = ATC_RSP_WAIT;
- return s_at_status;
- }
- http_response_buffer = (char *)strstr(p, "\r\n\r\n") + 4;
- PRINTF("get http response:%s", http_response_buffer);
- s_http_data_len = 0;
- s_at_status = ATC_RSP_FINISH;
- if(s_http_cb != NULL)
- {
- s_http_cb(http_response_buffer);
- }
- }
- break;
- default:
- break;
- }
- return s_at_status;
- }
- u8 inquire_signal_quality(void)
- {
- add_send_at_command("AT+CSQ", "AT+CSQ\r\n");
- return s_csq_value;
- }
- static s8 tcp_closed_handle(BOOL *urc, char *buf)
- {
- if(tcp_disconnected_cb != NULL)
- {
- tcp_disconnected_cb(0, -1);
- }
- return -1;
- }
- static s8 tcp_disconnect_handle(BOOL *urc, char *buf)
- {
- if(tcp_disconnected_cb != NULL)
- {
- tcp_disconnected_cb(0, -1);
- }
- return -1;
- }
- void register_module_tcp_connect_callback(u16 fd, module_tcp_connected_callback connect_cb)
- {
- tcp_connect_cb = connect_cb;
- }
- void register_module_tcp_sent_callback(u16 fd, module_tcp_sent_callback sent_callback)
- {
- tcp_sent_cb = sent_callback;
- }
- void register_module_tcp_recv_callback(u16 fd, module_tcp_recv_callback recv_callback)
- {
- tcp_recv_cb = recv_callback;
- }
- void register_module_tcp_disconnected_callback(u16 fd, module_tcp_disconnected_callback tcp_disconnected_callback)
- {
- tcp_disconnected_cb = tcp_disconnected_callback;
- }
- // fifo 定时检测
- static s8 at_fifo_check(void *arg)
- {
- static s8 wait_response_tick = 0;
- if(s_module_status == MODULE_START)
- {
- PRINTF("start send AT\r\n")
- send_at_command("AT\r\n");
- }
- else if(MODULE_INIT ==s_module_status|| MODULE_INIT_DONE == s_module_status)
- {
- if ((NULL == s_current_at_command))
- {
- if ((!FIFO_isEmpty(&s_at_fifo)) &&(s_at_status == ATC_RSP_FINISH))
- {
- s_current_at_command = (struct at_cmd_entity*)malloc(sizeof(struct at_cmd_entity));
- if (FIFO_Get(&s_at_fifo, s_current_at_command->at_name, s_current_at_command->at_cmd))
- {
- s_at_status = ATC_RSP_WAIT;
- send_at_command(s_current_at_command->at_cmd);
- PRINTF("sending:%s", s_current_at_command->at_cmd);
- }
- }
- else
- {
- //PRINTF("fifo is empty or the previous at command not complete!\r\n");
- }
- }
- else
- {
- PRINTF("sending:%s", s_current_at_command->at_cmd);
- //send
- //send_at_command(s_current_at_command->at_cmd);
- if(wait_response_tick > (s8)AT_MAX_RESPONSE_TIME)
- {
- }
- wait_response_tick++;
- }
- }
- return 0;
- }
- // 模块启动
- u8 module_system_start(void)
- {
- s_module_status = MODULE_START;
- timer3_init(1000, 1, at_fifo_check);
- timer3_start();
- return s_module_status;
- }
- // 模块初始化
- u8 module_system_init()
- {
- Usart_Send_Str_Data("module_init!!!\r\n");
- FIFO_Init (&s_at_fifo);
- add_send_at_command("ATE", "ATE0\r\n");
- //AT+IFC=0, set no flow control.
- //add_send_at_command("AT+IFC", "AT+IFC=0\r\n");
- //AT+CPIN, query .....
- add_send_at_command("AT+CPIN", "AT+CPIN?\r\n");
- //AT+CSQ
- add_send_at_command("AT+CSQ", "AT+CSQ\r\n");
- //AT+GSN request for the IMEI of the module.;
- add_send_at_command("AT+CGSN", "AT+CGSN\r\n");
- //AT+CREG
- //add_send_at_command("AT+CREG", "AT+CREG?\r\n");
- // AT+CGREG?
- //add_send_at_command("AT+CGREG", "AT+CGREG?\r\n");
- //AT+CIPMODE
- add_send_at_command("AT+CIPMODE", "AT+CIPMODE=0\r\n");
- //AT+NETCLOSE关闭上次开启的网络
- add_send_at_command("AT+NETCLOSE", "AT+NETCLOSE\r\n");
- //AT+NETOPEN开启网络
- add_send_at_command("AT+NETOPEN", "AT+NETOPEN\r\n");
- //AT+IPADDR获取模块IP地址
- //add_send_at_command("AT+IPADDR", "AT+IPADDR\r\n");
- s_module_status = MODULE_INIT;
- return s_module_status;
- }
- // 获取模块状态
- s8 get_module_status()
- {
- return s_module_status;
- }
- // 设置模块 状态
- void set_module_status(s8 status)
- {
- s_module_status = status;
- }
- void add_send_at_command(char *name_buffer, char *cmd_buffer)
- {
- char at_cmd_Buff[64] = {0};
- char at_name_Buff[20] = {0};
- strcpy(at_name_Buff, name_buffer);
- strcpy(at_cmd_Buff, cmd_buffer);
- if(FIFO_Put(&s_at_fifo, at_name_Buff, at_cmd_Buff) ==-1)
- {
- PRINTF("write fifo error!\r\n");
- }
- }
- // 模块发送http post 请求
- void module_http_post(const char *url, const char *data, module_http_callback http_cb)
- {
- s_http_cb = http_cb;
- char host_name[64] = "";
- char http_path[64] = "";
- int port = 80;
- BOOL is_http = strncmp(url, "http://", strlen("http://")) == 0;
- BOOL is_https = strncmp(url, "https://", strlen("https://")) == 0;
- if (is_http)
- url += strlen("http://"); // Get rid of the protocol.
- else if (is_https)
- {
- url += strlen("https://"); // Get rid of the protocol.
- port=443;
- }
- else
- {
- PRINTF("url is not http:// or https://")
- return;
- }
- char * path = strchr(url, '/');
- if (path == NULL)
- {
- path = strchr(url, '\0'); // Pointer to end of string.
- }
- char *colon = strchr(url, ':');
- if(colon > path) {
- colon = NULL;
- }
- if (colon == NULL) {
- memcpy(host_name, url, path - url);
- } else {
- port = atoi(colon + 1);
- if (port == 0) {
- //printf("Port error %s\n", url);
- return;
- }
- memcpy(host_name, url, colon - url);
- host_name[colon - url] = '\0';
- }
- host_name[path - url] = '\0';
- memcpy(http_path, path, strlen(url)- strlen(host_name));
- PRINTF("host_name:%s\n", host_name);
- PRINTF("http_path:%s\n", http_path);
- PRINTF("http_port:%d\n", port);
- // AT+CCHSET.
- add_send_at_command("AT+CCHSET", "AT+CCHSET=1\r\n");
- // AT+CCHSTART.
- add_send_at_command("AT+CCHSTART", "AT+CCHSTART\r\n");
- char http_url[64];
- memset(http_url, 0x0, sizeof(http_url));
- sprintf(http_url, "AT+CCHOPEN=%d,\"%s\",%d,1\r\n", 1, host_name, port);
- PRINTF("ready to http post:%s", http_url);
- add_send_at_command("AT+CCHOPEN", http_url);
- char post_headers[128] = "";
- char at_send_buffer[20] = "";
- sprintf(post_headers,
- "Content-Type:"
- HTTP_HEADER_CONTENT_TYPE
- "\r\n"
- "Content-Length: %d\r\n", strlen(data));
- struct fifo_data* http_post_data =(struct fifo_data*)malloc(sizeof(struct fifo_data));
- http_post_data->buf =(uint8_t*)malloc(512);
- int len = sprintf((char*)http_post_data->buf,
- "POST %s HTTP/1.1\r\n"
- "Host: %s:%d\r\n"
- "Accept:*/*\r\n"
- "Connection: close\r\n"
- "User-Agent: SIM7600\r\n"
- "%s"
- "\r\n%s",
- http_path, host_name, port, post_headers, data);
- http_post_data->length = len;
- PRINTF("http post:%s, len:%d", http_post_data->buf, len);
- sprintf(at_send_buffer, "AT+CCHSEND=%d,%d\r\n", 1, len);
- add_send_at_command("AT+CCHSEND", at_send_buffer);
- fifo_put_data(&s_at_fifo, http_post_data);
- // AT+CCHCLOSE
- add_send_at_command("AT+CCHCLOSE", "AT+CCHCLOSE=1\r\n");
- // AT+CCHSTOP
- add_send_at_command("AT+CCHSTOP", "AT+CCHSTOP\r\n");
- }
- // 监听tcp数据返回
- static s8 data_process(struct module_buf *buf)
- {
- struct module_buf *data_buf = buf;
- char *p = buf->buf;
- if(strstr(buf->buf, "RECV FROM:")!=NULL)
- {
- p = strstr(buf->buf, "+IPD");
- p += strlen("+IPD");
- send_data_len = atol(p);
- p = strstr(p, "\r\n");
- p = p + strlen("\r\n");
- memcpy(module_send_data_buffer->buf, p , send_data_len);
- return 1;
- }
- else
- {
- return 0;
- }
- }
- static s8 urc_process(struct module_buf* data)
- {
- // is urc.
- u8 i= 0;
- u8 urc = 0;
- for (i = 0; i <sizeof(urc_table)/sizeof(urc_table[0]); i++)
- {
- if(!strcmp((const char*)data->buf, urc_table[i].name))
- {
- urc_table[i].at_cmd_handler(&urc, (char*)data->buf);
- return 0;
- }
- }
- return -1;
- }
- // 模块返回数据处理
- s8 module_data_handler(void* data)
- {
- u8 urc = 0;
- struct module_buf* module_data = (struct module_buf*)data;
- if(module_data->length < 3)
- {
- PRINTF("module response not enough length!\r\n");
- if(module_data != NULL)
- {
- if(module_data->buf != NULL)
- {
- free(module_data->buf);
- module_data->buf = NULL;
- }
- free(module_data);
- module_data = NULL;
- }
- return -1;
- }
- if(MODULE_START == s_module_status)
- {
- if((strstr((char*)module_data->buf, "OK")))
- {
- PRINTF("the \"at\" cmd response \"OK\"\r\n");
- s_module_status = MODULE_SYNC;
- }
- }
- if (MODULE_INIT ==s_module_status|| MODULE_INIT_DONE == s_module_status)
- {
- int i = 0;
- if (NULL != s_current_at_command)
- {
- for (i = 0; i <sizeof(atCmdTable)/sizeof(atCmdTable[0]); i++)
- {
- if(!strcmp(s_current_at_command->at_name, atCmdTable[i].name))
- {
- s_at_status =atCmdTable[i].at_cmd_handler(&urc, (char*)module_data->buf);
- if (ATC_RSP_FINISH ==s_at_status)
- {
- free(s_current_at_command);
- s_current_at_command = NULL;
- }
- break;
- }
- }
- }
- else
- {
- // tcp response
- if(data_process(module_data)==1)
- {
- if(tcp_recv_cb != NULL)
- {
- tcp_recv_cb(0, module_send_data_buffer->buf, 4);
- send_data_len=0;
- }
- }
- else
- {
- urc_process(module_data);
- }
- }
- }
- if(module_data != NULL)
- {
- if(module_data->buf != NULL)
- {
- free(module_data->buf);
- module_data->buf = NULL;
- }
- free(module_data);
- module_data = NULL;
- }
- return 0;
- }
- void module_send_data(u16 fd, u8 *buf, u16 len)
- {
- struct fifo_data* tcp_data_buffer = (struct fifo_data *)malloc(sizeof(struct fifo_data));
- tcp_data_buffer->length = len;
- tcp_data_buffer->buf = (uint8_t*)malloc(len);
- memcpy(tcp_data_buffer->buf, buf, len);
- char command_buffer[48];
- sprintf(command_buffer, "AT+CIPSEND=%d,%d\r\n", 1, len);
- add_send_at_command("AT+CIPSEND", command_buffer);
- fifo_put_data(&s_at_fifo, tcp_data_buffer);
- }
- void module_tcp_connect(u16 fd, u32 ip, u16 port)
- {
- // 先关闭原来的连接
- add_send_at_command("AT+CIPCLOSE", "AT+CIPCLOSE=1\r\n");
- char connect_buf[80];
- u8 ip1, ip2, ip3, ip4;
- ip1 = ((u8*)(&ip))[0];
- ip2 = ((u8*)(&ip))[1];
- ip3 = ((u8*)(&ip))[2];
- ip4 = ((u8*)(&ip))[3];
- // AT+CIPOPEN.
- sprintf(connect_buf, "AT+CIPOPEN=%d,\"TCP\",\"%d.%d.%d.%d\",%d\r\n", 1, ip1, ip2, ip3, ip4, port);
- add_send_at_command("AT+CIPOPEN", connect_buf);
- }
- u8* get_module_serial(void)
- {
- return g_iemi_buf;
- }
|