|
|
@@ -35,11 +35,11 @@
|
|
|
|
|
|
|
|
|
//EC200N-A cellular module
|
|
|
-
|
|
|
+#define RETRY_TIMES 3
|
|
|
#define BUF_LEN 2000
|
|
|
-#define AT_PORT "/dev/ttyUSB1"
|
|
|
+#define AT_PORT "/dev/ttyUSB1"
|
|
|
#define BAUD_RATE B115200
|
|
|
-#define QUERY_INTERVAL 1000 // 查询间隔,毫秒
|
|
|
+#define QUERY_INTERVAL 1000 // 查询间隔,毫秒
|
|
|
#define RSSI_DBM_MIN1 -113 // LTE、HSDPA网络最小信号值,对应RSSI值范围0-57
|
|
|
#define RSSI_DBM_MIN2 -116 // TD-SCDMA网络最小信号值,对应RSSI值范围100-199
|
|
|
|
|
|
@@ -63,6 +63,10 @@ enum {
|
|
|
AT_ERR,
|
|
|
AT_TOUT,
|
|
|
};
|
|
|
+enum {
|
|
|
+ AT_STAT_UNINIT=0,
|
|
|
+ AT_STAT_INITED,
|
|
|
+};
|
|
|
enum {
|
|
|
SOCK_TCP=0,
|
|
|
SOCK_UDP,
|
|
|
@@ -174,10 +178,12 @@ typedef struct {
|
|
|
}mqtt_conn_t;
|
|
|
typedef struct {
|
|
|
int fd;
|
|
|
- CellularManager *pcell;
|
|
|
+ CellInfo_t *pcell;
|
|
|
lock_t lck;
|
|
|
int prod_id;
|
|
|
|
|
|
+ int stat;
|
|
|
+ cell_all_t all;
|
|
|
int curcmd; //current cmd
|
|
|
at_data_t data[AT_CMD_MAX];
|
|
|
GlobalDeviceManager *dm;
|
|
|
@@ -248,7 +254,7 @@ static at_cmd_t all_cmd[AT_CMD_MAX]={
|
|
|
{"AT+COPN", "OK", "ERROR", 300}, //查询运营商名称列表
|
|
|
{"AT+CTZU", "OK", "ERROR", 300}, //自动更新时区
|
|
|
{"AT+CTZR", "OK", "ERROR", 300}, //上报时区变化
|
|
|
- {"AT+QLTS", "OK", "ERROR", 300}, //获取通过网络同步的最新时间
|
|
|
+ {"AT+QLTS", "OK", "ERROR", 500}, //获取通过网络同步的最新时间
|
|
|
{"AT+QNWINFO", "OK", "ERROR", 300}, //查询网络信息
|
|
|
|
|
|
//call cmd
|
|
|
@@ -278,7 +284,7 @@ static at_cmd_t all_cmd[AT_CMD_MAX]={
|
|
|
{"AT+CMGD", "OK", "ERROR", 300}, //删除短消息
|
|
|
{"AT+CMGL", "OK", "ERROR", 300}, //按状态读取短消息
|
|
|
{"AT+CMGR", "OK", "ERROR", 300}, //按照索引读取短消息
|
|
|
- {"AT+CMGS", "OK", "ERROR", 120000}, //发送短消息
|
|
|
+ {"AT+CMGS", ">", "ERROR", 120000}, //发送短消息
|
|
|
{"AT+CMMS", "OK", "ERROR", 300}, //发送多条短消息
|
|
|
{"AT+CMGW", "OK", "ERROR", 300}, //存储短消息
|
|
|
{"AT+CMSS", "OK", "ERROR", 300}, //从存储器发送短消息
|
|
|
@@ -356,12 +362,53 @@ static at_cmd_t all_cmd[AT_CMD_MAX]={
|
|
|
};
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
-
|
|
|
static at_cmd_t* data_of(int cmd)
|
|
|
{
|
|
|
return &all_cmd[cmd];
|
|
|
}
|
|
|
+static int rssi_to_dbm(int rssi)
|
|
|
+{
|
|
|
+ int dbm=0;
|
|
|
|
|
|
+ if(rssi>=0 && rssi<=31) {
|
|
|
+ dbm = RSSI_DBM_MIN1+rssi*2;
|
|
|
+ }
|
|
|
+ else if(rssi>=100 && rssi<=191) {
|
|
|
+ dbm = RSSI_DBM_MIN2+rssi;
|
|
|
+ }
|
|
|
+ return dbm;
|
|
|
+}
|
|
|
+static char* str_get(char *buf, char c)
|
|
|
+{
|
|
|
+ int i,len=strlen(buf);
|
|
|
+ for(i=0; i<len; i++) {
|
|
|
+ if(buf[i]==c) return buf+i;
|
|
|
+ }
|
|
|
+ return buf;
|
|
|
+}
|
|
|
+static int mem_find(char *buf, int len, char *str)
|
|
|
+{
|
|
|
+ int i,slen=strlen(str);
|
|
|
+ for(i=0; i<=len-slen; i++) {
|
|
|
+ if(memcmp(buf+i, str, slen)==0) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+static void mem_print(char *buf, int len)
|
|
|
+{
|
|
|
+ int i;
|
|
|
+ for(i=0; i<len; i++) {
|
|
|
+ LOGD("%d, ", buf[i]);
|
|
|
+ if((i+1)%16==0) LOGD("\n");
|
|
|
+ }
|
|
|
+ LOGD("\n");
|
|
|
+}
|
|
|
+static int cell_reset(cell_handle_t *h);
|
|
|
+static int cell_sock_init(cell_handle_t *h);
|
|
|
+static int cell_get_info(cell_handle_t *h, cell_info_t *info);
|
|
|
+static int cell_get_stat(cell_handle_t *h, cell_stat_t *stat);
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
@@ -401,7 +448,7 @@ static int port_init(cell_handle_t *h)
|
|
|
return -1;
|
|
|
}
|
|
|
port_setup(h, BAUD_RATE);
|
|
|
- reset_var(h->pcell);
|
|
|
+ //reset_var(h->pcell);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
@@ -468,10 +515,10 @@ static int port_wait(cell_handle_t *h, int timeout)
|
|
|
r = select(h->fd + 1, &fset, NULL, NULL, &tv);
|
|
|
if (r<=0) {
|
|
|
if(r<0) {
|
|
|
- LOGE("select error %s\n", strerror(errno));
|
|
|
+ //LOGE("port_wait error %s\n", strerror(errno));
|
|
|
}
|
|
|
else {
|
|
|
- LOGE("select timeout\n");
|
|
|
+ //LOGE("port_wait timeout\n");
|
|
|
}
|
|
|
}
|
|
|
return r;
|
|
|
@@ -491,84 +538,92 @@ static int port_recv(cell_handle_t *h, char *buf, int buflen, int timeout)
|
|
|
}
|
|
|
return r;
|
|
|
}
|
|
|
-static int post_cmd(cell_handle_t *h, at_cmd_t *pcmd, char *para)
|
|
|
+static int send_cmd(cell_handle_t *h, int cmd, char *para, char *rcv, int rcvlen)
|
|
|
{
|
|
|
- int r;
|
|
|
- char buf[BUF_LEN];
|
|
|
-
|
|
|
- sprintf(buf, "%s%s\r", pcmd?pcmd->cmd:"", para?para:"");
|
|
|
- r = port_write(h, buf, strlen(buf));
|
|
|
- if(r<=0) {
|
|
|
- r = AT_ERR;
|
|
|
- }
|
|
|
- else {
|
|
|
- r = AT_OK;
|
|
|
- }
|
|
|
- return r;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-static int send_cmd(cell_handle_t *h, at_cmd_t *pcmd, char *para, char *rcv, int rcvlen, int times)
|
|
|
-{
|
|
|
- int i,r,buflen,rsp=AT_TOUT;
|
|
|
+ int i,r,l,tout,finish=0;
|
|
|
+ int tlen=0,buflen,wait_step=10;
|
|
|
char buf[BUF_LEN],*pbuf;
|
|
|
+ at_cmd_t *pcmd=data_of(cmd);
|
|
|
|
|
|
- sprintf(buf, "%s%s\r", pcmd?pcmd->cmd:"", para?para:"");
|
|
|
+ sprintf(buf, "%s%s\r", pcmd->cmd, para?para:"");
|
|
|
|
|
|
- LOGD("___send: %s\n", buf);
|
|
|
- for(i=0; i<times; i++) {
|
|
|
+ LOGD("[tx]: %s\n", buf);
|
|
|
+ for(i=0; i<RETRY_TIMES; i++) {
|
|
|
+ tout = 0;
|
|
|
port_write(h, buf, strlen(buf));
|
|
|
- r = port_wait(h, pcmd->timeout);
|
|
|
- if(r>0) {
|
|
|
- if(rcv && rcvlen>0) {
|
|
|
- pbuf = rcv;
|
|
|
- buflen = rcvlen;
|
|
|
- }
|
|
|
- else {
|
|
|
- pbuf = buf;
|
|
|
- buflen = sizeof(buf);
|
|
|
- }
|
|
|
-
|
|
|
- r = port_read(h, pbuf, buflen);
|
|
|
- if(r>0) {
|
|
|
- pbuf[r] = 0;
|
|
|
- LOGD("___recv: %s\n", pbuf);
|
|
|
- if(pcmd->ok && strstr(pbuf, pcmd->ok)) {
|
|
|
- rsp = AT_OK; break;
|
|
|
+ while(tout<pcmd->timeout) {
|
|
|
+ l = port_wait(h, wait_step);
|
|
|
+ if(l>0) {
|
|
|
+ if(rcv && rcvlen>0) {
|
|
|
+ pbuf = rcv;
|
|
|
+ buflen = rcvlen;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ pbuf = buf;
|
|
|
+ buflen = sizeof(buf);
|
|
|
}
|
|
|
-
|
|
|
- if(pcmd->err && strstr(pbuf, pcmd->err)) {
|
|
|
- rsp = AT_ERR; break;
|
|
|
+
|
|
|
+ l = port_read(h, pbuf+tlen, buflen);
|
|
|
+ if(l>0) {
|
|
|
+ tlen += l; pbuf[tlen] = 0;
|
|
|
+
|
|
|
+ LOGD("[rx]: %s\n", pbuf);
|
|
|
+ if(pcmd->ok && strlen(pcmd->ok) && strstr(pbuf, pcmd->ok)) {
|
|
|
+ return AT_OK;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(pcmd->err && strlen(pcmd->err) && strstr(pbuf, pcmd->err)) {
|
|
|
+ return AT_ERR;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ tout += wait_step;
|
|
|
}
|
|
|
}
|
|
|
- return rsp;
|
|
|
+ cell_reset(h);
|
|
|
+
|
|
|
+ return AT_TOUT;
|
|
|
}
|
|
|
static int send_data(cell_handle_t *h, void *data, int len, int timeout)
|
|
|
{
|
|
|
- int r=-1,rt;
|
|
|
char buf[100];
|
|
|
-
|
|
|
- rt = port_write(h, data, len);
|
|
|
- if(rt==len) {
|
|
|
- rt = port_wait(h, timeout);
|
|
|
- if(rt>0) {
|
|
|
- rt = port_read(h, buf, sizeof(buf));
|
|
|
- if(rt>0) {
|
|
|
- if(strstr(buf, "OK")) {
|
|
|
- r = 0;
|
|
|
+ int l,tlen=0,tout=0,wait_step=10;
|
|
|
+
|
|
|
+ l = port_write(h, data, len);
|
|
|
+ if(l==len) {
|
|
|
+ while(tout<timeout) {
|
|
|
+ l = port_wait(h, wait_step);
|
|
|
+ if(l>0) {
|
|
|
+ l = port_read(h, buf, sizeof(buf));
|
|
|
+ if(l>0) {
|
|
|
+ tlen += l; buf[tlen] = 0;
|
|
|
+ if(strstr(buf, "OK")) {
|
|
|
+ return AT_OK;
|
|
|
+ }
|
|
|
+ else if(strstr(buf, "ERROR")) {
|
|
|
+ return AT_ERR;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ tout += wait_step;
|
|
|
}
|
|
|
}
|
|
|
- return r;
|
|
|
+ return AT_TOUT;
|
|
|
}
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
+static int cell_reset(cell_handle_t *h)
|
|
|
+{
|
|
|
+ int r;
|
|
|
+
|
|
|
+ r = send_cmd(h, AT_CMD_CFUN, "=1,1", NULL, 0);
|
|
|
+ if(r==AT_OK) {
|
|
|
+ h->stat = AT_STAT_UNINIT;
|
|
|
+ }
|
|
|
+ return (r==AT_OK)?0:-1;
|
|
|
+}
|
|
|
static int cell_init(cell_handle_t *h)
|
|
|
{
|
|
|
int i,r;
|
|
|
- at_cmd_t *pcmd=NULL;
|
|
|
at_para_t paras[]={
|
|
|
{AT_CMD_AT, ""},
|
|
|
{AT_CMD_ATE, "0"},
|
|
|
@@ -577,76 +632,82 @@ static int cell_init(cell_handle_t *h)
|
|
|
{AT_CMD_CGACT, "?"},
|
|
|
{AT_CMD_CGDCONT, "=1,\"IPV4V6\",\"cmnet\""},
|
|
|
{AT_CMD_QCFG, "=\"usbnet\",1"}, //set module to ecm mode
|
|
|
+ {AT_CMD_CMGF, "=1"},
|
|
|
+ {AT_CMD_CSCS, "=GSM"},
|
|
|
//{AT_CMD_QGPS, "=1"},
|
|
|
//{AT_CMD_QGPSCFG, "=\"gnssconfig\",5"},
|
|
|
//{AT_CMD_QGPSCFG, "=\"gpsnmeatype\",1"},
|
|
|
{-1, ""},
|
|
|
};
|
|
|
|
|
|
+ if(h->stat==AT_STAT_INITED) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
for(i=0;; i++) {
|
|
|
if(paras[i].cmd==-1) {
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- pcmd = data_of(paras[i].cmd);
|
|
|
- r = send_cmd(h, pcmd, paras[i].para, NULL, 0, 3);
|
|
|
+ r = send_cmd(h, paras[i].cmd, paras[i].para, NULL, 0);
|
|
|
if(r!=AT_OK) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ if(r==0) {
|
|
|
+ h->stat = AT_STAT_INITED;
|
|
|
+
|
|
|
+ cell_sock_init(h);
|
|
|
+ cell_get_info(h, &h->all.info);
|
|
|
+ }
|
|
|
+
|
|
|
return r?-1:0;
|
|
|
}
|
|
|
static int cell_deinit(cell_handle_t *h)
|
|
|
{
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-static int rssi_to_dbm(int rssi)
|
|
|
-{
|
|
|
- int dbm=0;
|
|
|
-
|
|
|
- if(rssi>=0 && rssi<=31) {
|
|
|
- dbm = RSSI_DBM_MIN1+rssi*2;
|
|
|
- }
|
|
|
- else if(rssi>=100 && rssi<=191) {
|
|
|
- dbm = RSSI_DBM_MIN2+rssi;
|
|
|
+ if(h->stat==AT_STAT_UNINIT) {
|
|
|
+ return -1;
|
|
|
}
|
|
|
- return dbm;
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
static int cell_get_info(cell_handle_t *h, cell_info_t *info)
|
|
|
{
|
|
|
int r;
|
|
|
- char buf[100];
|
|
|
- at_cmd_t *pcmd=NULL;
|
|
|
+ char buf[400];
|
|
|
|
|
|
+ if(h->stat==AT_STAT_UNINIT) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
memset(info, 0, sizeof(cell_info_t));
|
|
|
+ r = send_cmd(h, AT_CMD_CNUM, NULL, buf, sizeof(buf));
|
|
|
+ if(r==AT_OK) {
|
|
|
+ r = sscanf(str_get(buf,'+'), "+CNUM: %*s,%s,%*d", info->cnum);
|
|
|
+ }
|
|
|
|
|
|
- pcmd = data_of(AT_CMD_CNUM);
|
|
|
- r = send_cmd(h, pcmd, NULL, buf, sizeof(buf), 1);
|
|
|
+ r = send_cmd(h, AT_CMD_GSN, NULL, buf, sizeof(buf));
|
|
|
if(r==AT_OK) {
|
|
|
- r = sscanf(buf, "+CNUM: %*s,%s,%*d", info->cnum);
|
|
|
+ r = sscanf(str_get(buf,'+'), "%s", info->imei);
|
|
|
}
|
|
|
|
|
|
- pcmd = data_of(AT_CMD_GSN);
|
|
|
- r = send_cmd(h, pcmd, NULL, buf, sizeof(buf), 1);
|
|
|
+ r = send_cmd(h, AT_CMD_CIMI, NULL, buf, sizeof(buf));
|
|
|
if(r==AT_OK) {
|
|
|
- r = sscanf(buf, "%s", info->imei);
|
|
|
+ r = sscanf(str_get(buf,'+'), "%s", info->imsi);
|
|
|
}
|
|
|
|
|
|
- pcmd = data_of(AT_CMD_CIMI);
|
|
|
- r = send_cmd(h, pcmd, NULL, buf, sizeof(buf), 1);
|
|
|
+ r = send_cmd(h, AT_CMD_CGDCONT, "?", buf, sizeof(buf));
|
|
|
if(r==AT_OK) {
|
|
|
- r = sscanf(buf, "%s", info->imsi);
|
|
|
+ r = sscanf(str_get(buf,'+'), "+CGDCONT: %*d,\"%*[^\"]\",\"%[^\"]", info->apn);
|
|
|
}
|
|
|
|
|
|
- pcmd = data_of(AT_CMD_COPS);
|
|
|
- r = send_cmd(h, pcmd, "?", buf, sizeof(buf), 1);
|
|
|
+ r = send_cmd(h, AT_CMD_COPS, "?", buf, sizeof(buf));
|
|
|
if(r==AT_OK) {
|
|
|
- r = sscanf(buf, "%*d,%*d,%s", info->isp);
|
|
|
+ r = sscanf(str_get(buf,'+'), "+COPS: %*d,%*d,\"%[^\"]", info->isp);
|
|
|
}
|
|
|
|
|
|
- //LOGD("___info->apn: %s\n", info->apn);
|
|
|
+ LOGD("___info->apn: %s\n", info->apn);
|
|
|
LOGD("___info->isp: %s\n", info->isp);
|
|
|
LOGD("___info->imei: %s\n", info->imei);
|
|
|
LOGD("___info->imsi: %s\n", info->imsi);
|
|
|
@@ -656,17 +717,19 @@ static int cell_get_info(cell_handle_t *h, cell_info_t *info)
|
|
|
}
|
|
|
static int cell_get_sig(cell_handle_t *h, cell_stat_t *stat)
|
|
|
{
|
|
|
- int r=-1;
|
|
|
- char buf[100];
|
|
|
- int rssi,ber;
|
|
|
- at_cmd_t *pcmd=NULL;
|
|
|
+ char buf[200];
|
|
|
+ int rssi,ber,r=-1;
|
|
|
+
|
|
|
+ if(h->stat==AT_STAT_UNINIT) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
|
|
|
- pcmd = data_of(AT_CMD_CSQ);
|
|
|
- r = send_cmd(h, pcmd, NULL, buf, sizeof(buf), 1);
|
|
|
+ r = send_cmd(h, AT_CMD_CSQ, NULL, buf, sizeof(buf));
|
|
|
if(r!=AT_OK) {
|
|
|
return -1;
|
|
|
}
|
|
|
- r = sscanf(buf, "+CSQ: %d,%d", &rssi, &ber);
|
|
|
+
|
|
|
+ r = sscanf(str_get(buf,'+'), "%*s %d,%d", &rssi, &ber);
|
|
|
if(r<2) {
|
|
|
return -1;
|
|
|
}
|
|
|
@@ -679,14 +742,17 @@ static int cell_get_gnss(cell_handle_t *h, cell_stat_t *stat)
|
|
|
{
|
|
|
int r;
|
|
|
char buf[200],utc[32],date[32];
|
|
|
- at_cmd_t *pcmd=NULL;
|
|
|
|
|
|
- pcmd = data_of(AT_CMD_QGPSLOC);
|
|
|
- r = send_cmd(h, pcmd, "=0", buf, sizeof(buf), 1);
|
|
|
+ if(h->stat==AT_STAT_UNINIT) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ r = send_cmd(h, AT_CMD_QGPSLOC, "=0", buf, sizeof(buf));
|
|
|
if(r!=AT_OK) {
|
|
|
return -1;
|
|
|
}
|
|
|
- r = sscanf(buf, "+QGPSLOC: %s,%s,%s,%*d,%d,%*d,%*d,%*d,%*d,%s,%d", utc, stat->latitude, stat->longitude, &stat->altitude, date, &stat->nsta);
|
|
|
+
|
|
|
+ r = sscanf(str_get(buf,'+'), "+QGPSLOC: %s,%s,%s,%*d,%d,%*d,%*d,%*d,%*d,%s,%d", utc, stat->latitude, stat->longitude, &stat->altitude, date, &stat->nsta);
|
|
|
if(r<6) {
|
|
|
return -1;
|
|
|
}
|
|
|
@@ -694,61 +760,90 @@ static int cell_get_gnss(cell_handle_t *h, cell_stat_t *stat)
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
-static int cell_get_stat(cell_handle_t *h, cell_stat_t *stat)
|
|
|
+static int cell_get_ping(cell_handle_t *h, cell_stat_t *stat)
|
|
|
{
|
|
|
- cell_get_sig(h, stat);
|
|
|
- cell_get_gnss(h, stat);
|
|
|
+ int r,ret;
|
|
|
+ char buf[200],utc[32],date[32];
|
|
|
+
|
|
|
+ if(h->stat==AT_STAT_UNINIT) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ r = send_cmd(h, AT_CMD_QPING, "=1,\"163.com\",3,2,", buf, sizeof(buf));
|
|
|
+ if(r!=AT_OK) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ r = sscanf(str_get(buf,'+'), "+QPING: %d", &ret);
|
|
|
+ if(r<1) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ if(ret==0) {
|
|
|
+ stat->linked = 1;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ stat->linked = 0;
|
|
|
+ }
|
|
|
return 0;
|
|
|
}
|
|
|
-static int cell_get_time(cell_handle_t *h)
|
|
|
+static int cell_get_time(cell_handle_t *h, cell_stat_t *stat)
|
|
|
{
|
|
|
- int r,dst;
|
|
|
- char buf[100],date[24],time[24];
|
|
|
- at_cmd_t *pcmd=NULL;
|
|
|
+ int r,offset;
|
|
|
+ char opr,buf[200],date[24],time[24];
|
|
|
+ struct tm utc_tm;
|
|
|
|
|
|
- pcmd = data_of(AT_CMD_QLTS);
|
|
|
- r = send_cmd(h, pcmd, "=1", buf, sizeof(buf), 1);
|
|
|
+ if(h->stat==AT_STAT_UNINIT) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ r = send_cmd(h, AT_CMD_QLTS, "=1", buf, sizeof(buf));
|
|
|
if(r!=AT_OK) {
|
|
|
return -1;
|
|
|
}
|
|
|
- r = sscanf(buf, "+QLTS: \"%s,%s,%d", date, time, &dst);
|
|
|
- if(r<3) {
|
|
|
+
|
|
|
+ r = sscanf(str_get(buf,'+'), "+QLTS: \"%d/%d/%d,%d:%d:%d",&utc_tm.tm_year,&utc_tm.tm_mon,&utc_tm.tm_mday,&utc_tm.tm_hour,&utc_tm.tm_min,&utc_tm.tm_sec);
|
|
|
+ if(r<6) {
|
|
|
return -1;
|
|
|
}
|
|
|
- LOGD("___date: %s, time: %s, dst: %d\n", date, time, dst);
|
|
|
+ stat->utctime = mktime2(&utc_tm, 0);
|
|
|
+ //print_time("999", &utc_tm);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
-static int cell_reset(cell_handle_t *h)
|
|
|
+static int cell_get_stat(cell_handle_t *h, cell_stat_t *stat)
|
|
|
{
|
|
|
- int r;
|
|
|
- at_cmd_t *pcmd=NULL;
|
|
|
+ if(h->stat==AT_STAT_UNINIT) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
|
|
|
- pcmd = data_of(AT_CMD_CFUN);
|
|
|
- r = post_cmd(h, pcmd, "=1,1");
|
|
|
- return (r==AT_OK)?0:-1;
|
|
|
-}
|
|
|
+ cell_get_sig(h, stat);
|
|
|
+ cell_get_gnss(h, stat);
|
|
|
+ cell_get_ping(h, stat);
|
|
|
+ cell_get_time(h, stat);
|
|
|
|
|
|
+ return 0;
|
|
|
+}
|
|
|
static int cell_sock_init(cell_handle_t *h)
|
|
|
{
|
|
|
int i,r;
|
|
|
- at_cmd_t *pcmd=NULL;
|
|
|
at_para_t paras[]={
|
|
|
- {AT_CMD_QICSGP, ""},
|
|
|
+ {AT_CMD_QICSGP, "="CONTEXT_ID},
|
|
|
{AT_CMD_QIDEACT, "="CONTEXT_ID},
|
|
|
{AT_CMD_QIACT, "="CONTEXT_ID},
|
|
|
{-1, ""},
|
|
|
};
|
|
|
|
|
|
+ if(h->stat==AT_STAT_UNINIT) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
for(i=0;; i++) {
|
|
|
if(paras[i].cmd==-1) {
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- pcmd = data_of(paras[i].cmd);
|
|
|
- r = send_cmd(h, pcmd, paras[i].para, NULL, 0, 3);
|
|
|
+ r = send_cmd(h, paras[i].cmd, paras[i].para, NULL, 0);
|
|
|
if(r!=AT_OK) {
|
|
|
- LOGE("___ %s failed\n", pcmd->cmd);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
@@ -757,17 +852,23 @@ static int cell_sock_init(cell_handle_t *h)
|
|
|
}
|
|
|
static int cell_sock_deinit(cell_handle_t *h)
|
|
|
{
|
|
|
+ if(h->stat==AT_STAT_UNINIT) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
return 0;
|
|
|
}
|
|
|
static int cell_sock_conn(cell_handle_t *h, int sock, char *host, uint16_t port, sock_conn_t *conn)
|
|
|
{
|
|
|
int r;
|
|
|
char buf[200];
|
|
|
- at_cmd_t *pcmd=NULL;
|
|
|
+
|
|
|
+ if(h->stat==AT_STAT_UNINIT) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
|
|
|
sprintf(buf, "=%s,%s,%s,“%s”,%d,0,1", CONTEXT_ID, CONNECT_ID, (sock==SOCK_TCP)?"TCP":"UDP", host, port);
|
|
|
- pcmd = data_of(AT_CMD_QIOPEN);
|
|
|
- r = send_cmd(h, pcmd, buf, NULL, 0, 3);
|
|
|
+ r = send_cmd(h, AT_CMD_QIOPEN, buf, NULL, 0);
|
|
|
if(r!=AT_OK) {
|
|
|
return -1;
|
|
|
}
|
|
|
@@ -782,11 +883,14 @@ static int cell_sock_disconn(sock_conn_t *conn)
|
|
|
{
|
|
|
int r;
|
|
|
char buf[200];
|
|
|
- at_cmd_t *pcmd=NULL;
|
|
|
+ cell_handle_t *h=(cell_handle_t*)conn->h;
|
|
|
+
|
|
|
+ if(h->stat==AT_STAT_UNINIT) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
|
|
|
sprintf(buf, "=%s,%s", CONNECT_ID, CONNECT_TIMEOUT);
|
|
|
- pcmd = data_of(AT_CMD_QICLOSE);
|
|
|
- r = send_cmd(conn->h, pcmd, buf, NULL, 0, 3);
|
|
|
+ r = send_cmd(h, AT_CMD_QICLOSE, buf, NULL, 0);
|
|
|
if(r!=AT_OK) {
|
|
|
return -1;
|
|
|
}
|
|
|
@@ -801,10 +905,14 @@ static int cell_sock_send(sock_conn_t *conn, void *data, int len)
|
|
|
{
|
|
|
int r=0,rt,offset=0,remain_len,send_len;
|
|
|
char buf[100],*pd=(char*)data;
|
|
|
- at_cmd_t *pcmd=data_of(AT_CMD_QISEND);
|
|
|
+ cell_handle_t *h=(cell_handle_t*)conn->h;
|
|
|
+
|
|
|
+ if(h->stat==AT_STAT_UNINIT) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
|
|
|
sprintf(buf, "=%d,%d", conn->conn_id, len);
|
|
|
- rt = send_cmd(conn->h, pcmd, buf, NULL, 0, 1);
|
|
|
+ rt = send_cmd(h, AT_CMD_QISEND, buf, NULL, 0);
|
|
|
if(rt!=AT_OK) {
|
|
|
return -1;
|
|
|
}
|
|
|
@@ -813,7 +921,7 @@ static int cell_sock_send(sock_conn_t *conn, void *data, int len)
|
|
|
remain_len = len - offset;
|
|
|
send_len = (remain_len>CHUNCK_SIZE)?CHUNCK_SIZE:remain_len;
|
|
|
|
|
|
- rt = send_data(conn->h, data+offset, send_len, 500);
|
|
|
+ rt = send_data(h, data+offset, send_len, 500);
|
|
|
if(rt!=AT_OK) {
|
|
|
r = -1; break;
|
|
|
}
|
|
|
@@ -826,10 +934,13 @@ static int cell_sock_stat(sock_conn_t *conn)
|
|
|
{
|
|
|
int r;
|
|
|
char buf[100];
|
|
|
- at_cmd_t *pcmd=NULL;
|
|
|
+ cell_handle_t *h=(cell_handle_t*)conn->h;
|
|
|
|
|
|
- pcmd = data_of(AT_CMD_QISTATE);
|
|
|
- r = send_cmd(conn->h, pcmd, NULL, buf, sizeof(buf), 1);
|
|
|
+ if(h->stat==AT_STAT_UNINIT) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ r = send_cmd(h, AT_CMD_QISTATE, NULL, buf, sizeof(buf));
|
|
|
if(r!=AT_OK) {
|
|
|
return -1;
|
|
|
}
|
|
|
@@ -844,35 +955,33 @@ static int cell_send_sms(cell_handle_t *h, char *number, char *txt)
|
|
|
{
|
|
|
int r=-1,rt;
|
|
|
char tmp[100],buf[200];
|
|
|
- at_cmd_t *pcmd=NULL;
|
|
|
|
|
|
- pcmd = data_of(AT_CMD_CMGF);
|
|
|
- r = send_cmd(h, pcmd, "=1", buf, sizeof(buf), 1);
|
|
|
- if(r!=AT_OK) {
|
|
|
+ if(h->stat==AT_STAT_UNINIT) {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- pcmd = data_of(AT_CMD_CMGS);
|
|
|
sprintf(tmp, "=\"%s\"", number);
|
|
|
- r = send_cmd(h, pcmd, tmp, buf, sizeof(buf), 1);
|
|
|
+ r = send_cmd(h, AT_CMD_CMGS, tmp, buf, sizeof(buf));
|
|
|
if(r!=AT_OK) {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- sprintf(buf, "%s\x1a", txt);
|
|
|
- r = port_write(h, buf, strlen(buf));
|
|
|
+ snprintf(buf, sizeof(buf), "%s\x1a", txt);
|
|
|
+ r = send_data(h, buf, strlen(buf), 5000);
|
|
|
|
|
|
- return 0;
|
|
|
+ return r;
|
|
|
}
|
|
|
static int cell_send_call(cell_handle_t *h, char *number, void *data, int len)
|
|
|
{
|
|
|
int r;
|
|
|
char buf[100];
|
|
|
- at_cmd_t *pcmd=NULL;
|
|
|
|
|
|
- pcmd = data_of(AT_CMD_ATD);
|
|
|
+ if(h->stat==AT_STAT_UNINIT) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
sprintf(buf, "%s", number);
|
|
|
- r = send_cmd(h, pcmd, NULL, buf, sizeof(buf), 1);
|
|
|
+ r = send_cmd(h, AT_CMD_ATD, NULL, buf, sizeof(buf));
|
|
|
if(r!=AT_OK) {
|
|
|
return -1;
|
|
|
}
|
|
|
@@ -885,10 +994,14 @@ static int conn_cfg(mqtt_conn_t *conn, char *item, char *value)
|
|
|
{
|
|
|
int r;
|
|
|
char tmp[100],buf[200];
|
|
|
- at_cmd_t *pcmd=data_of(AT_CMD_QMTCFG);
|
|
|
+ cell_handle_t *h=(cell_handle_t*)conn->h;
|
|
|
+
|
|
|
+ if(h->stat==AT_STAT_UNINIT) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
|
|
|
sprintf(tmp, "=\"%s\",%s", item, value);
|
|
|
- r = send_cmd(conn->h, pcmd, NULL, buf, sizeof(buf), 1);
|
|
|
+ r = send_cmd(h, AT_CMD_QMTCFG, NULL, buf, sizeof(buf));
|
|
|
if(r!=AT_OK) {
|
|
|
return -1;
|
|
|
}
|
|
|
@@ -1006,25 +1119,13 @@ static void conn_init(cell_handle_t *h, mqtt_conn_t *conn)
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
-static int sock_test(void)
|
|
|
+static int cell_test(cell_handle_t *h)
|
|
|
{
|
|
|
int r;
|
|
|
cell_info_t info;
|
|
|
cell_stat_t stat;
|
|
|
- cell_handle_t *h=&ceHandle;
|
|
|
-
|
|
|
- r = cell_get_info(h, &info);
|
|
|
- if(r==0) {
|
|
|
- LOGD("___apn: %s\n", info.apn);
|
|
|
- LOGD("___isp: %s\n", info.isp);
|
|
|
- LOGD("___dbm: %s\n", info.imei);
|
|
|
- LOGD("___dbm: %s\n", info.imsi);
|
|
|
- LOGD("___dbm: %s\n", info.cnum);
|
|
|
- }
|
|
|
- else {
|
|
|
- LOGE("___ get_info failed\n");
|
|
|
- }
|
|
|
|
|
|
+ cell_get_info(h, &info);
|
|
|
while(1) {
|
|
|
r = cell_get_stat(h, &stat);
|
|
|
if(r==0) {
|
|
|
@@ -1059,24 +1160,30 @@ static int urc_handle(char *data, int len)
|
|
|
static void* cellular_thread(void *arg)
|
|
|
{
|
|
|
int r;
|
|
|
+ uint32_t cnt=0;
|
|
|
char rxbuf[BUF_LEN];
|
|
|
thread_handle_t *th=(thread_handle_t*)arg;
|
|
|
cell_handle_t *h=(cell_handle_t*)th->arg;
|
|
|
|
|
|
+ port_init(h);
|
|
|
while (th->quit==0) {
|
|
|
+ cnt++;
|
|
|
if(h->pcell->mode) {
|
|
|
|
|
|
- r = port_recv(h, rxbuf, sizeof(rxbuf), QUERY_INTERVAL);
|
|
|
- if(r>0) {
|
|
|
- urc_handle(rxbuf, r);
|
|
|
+ if(cnt%5==0) {
|
|
|
+ cell_init(h);
|
|
|
}
|
|
|
+
|
|
|
+ if(cnt%5==0) {
|
|
|
+ cell_get_stat(h, &h->all.stat);
|
|
|
+ }
|
|
|
+
|
|
|
+ //sprintf(rxbuf, "hello %d", cnt);
|
|
|
+ //cell_send_sms(h, "15228908007", rxbuf);
|
|
|
}
|
|
|
- else {
|
|
|
- msleep(1000);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
+ sleep(1);
|
|
|
}
|
|
|
+ cell_deinit(h);
|
|
|
port_deinit(h);
|
|
|
|
|
|
pthread_exit(NULL);
|
|
|
@@ -1106,21 +1213,11 @@ int cellular_init(void)
|
|
|
memset(h, 0, sizeof(cell_handle_t));
|
|
|
|
|
|
h->dm = &__globalDeviceManage;
|
|
|
- h->pcell = &_globalCellularManager;
|
|
|
- h->pcell->mode = h->dm->_globalDevInfo.cell;
|
|
|
+ h->pcell = &h->dm->_globalDevInfo.cell;
|
|
|
h->prod_id = h->dm->_globalDevInfo.product.id;
|
|
|
h->lck = lock_d_init();
|
|
|
-
|
|
|
- port_init(h);
|
|
|
- r = cell_init(h);
|
|
|
- if(r) {
|
|
|
- LOGE("__cell_init failed\n");
|
|
|
- return -1;
|
|
|
- }
|
|
|
|
|
|
- if (0==thread_is_running(THREAD_ID_CELLULAR)) {
|
|
|
- thread_start(THREAD_ID_CELLULAR, cellular_thread, h);
|
|
|
- }
|
|
|
+ thread_start(THREAD_ID_CELLULAR, cellular_thread, h);
|
|
|
#endif
|
|
|
|
|
|
return 0;
|
|
|
@@ -1132,32 +1229,23 @@ int cellular_deinit(void)
|
|
|
|
|
|
#ifdef USE_CELL
|
|
|
lock_d_deinit(h->lck);
|
|
|
- cell_deinit(h);
|
|
|
thread_stop(THREAD_ID_CELLULAR);
|
|
|
#endif
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-int cellular_get_info(cell_info_t *info)
|
|
|
-{
|
|
|
- int r=-1;
|
|
|
- cell_handle_t *h=&ceHandle;
|
|
|
-
|
|
|
-#ifdef USE_CELL
|
|
|
- r = cell_get_info(h, info);
|
|
|
-#endif
|
|
|
|
|
|
- return r;
|
|
|
-}
|
|
|
-
|
|
|
-int cellular_get_stat(cell_stat_t *stat)
|
|
|
+int cellular_get_all(cell_all_t *all)
|
|
|
{
|
|
|
int r=-1;
|
|
|
cell_handle_t *h=&ceHandle;
|
|
|
|
|
|
#ifdef USE_CELL
|
|
|
- r = cell_get_stat(h, stat);
|
|
|
+ if(all) {
|
|
|
+ *all = h->all;
|
|
|
+ r = 0;
|
|
|
+ }
|
|
|
#endif
|
|
|
|
|
|
return r;
|
|
|
@@ -1180,8 +1268,10 @@ int cellular_is_connected(void)
|
|
|
|
|
|
int cellular_test(void)
|
|
|
{
|
|
|
+ cell_handle_t *h=&ceHandle;
|
|
|
+
|
|
|
#ifdef USE_CELL
|
|
|
- sock_test();
|
|
|
+ cell_test(h);
|
|
|
#endif
|
|
|
|
|
|
return 0;
|