|
|
@@ -31,7 +31,7 @@
|
|
|
#endif
|
|
|
|
|
|
|
|
|
-//#define USE_CELL
|
|
|
+#define USE_CELL
|
|
|
//#define GNSS_ENABLE //EC200N-CN, 不支持gnss
|
|
|
|
|
|
#define RETRY_TIMES 3
|
|
|
@@ -185,6 +185,8 @@ typedef struct {
|
|
|
int stat;
|
|
|
cell_all_t all;
|
|
|
int curcmd; //current cmd
|
|
|
+
|
|
|
+ mqtt_conn_t mq_conn[MQTT_MAX];
|
|
|
at_data_t data[AT_CMD_MAX];
|
|
|
GlobalDeviceManager *dm;
|
|
|
}cell_handle_t;
|
|
|
@@ -365,10 +367,10 @@ at_para_t init_para[]={
|
|
|
{AT_CMD_ATE, "0"},
|
|
|
{AT_CMD_CPIN, "?"},
|
|
|
{AT_CMD_CREG, "?"},
|
|
|
- //{AT_CMD_QIDEACT, "="CONTEXT_ID},
|
|
|
- {AT_CMD_QIACT, "="CONTEXT_ID},
|
|
|
- {AT_CMD_CGDCONT, ""},
|
|
|
+ //{AT_CMD_CGDCONT, ""},
|
|
|
{AT_CMD_QICSGP, ""},
|
|
|
+ {AT_CMD_QIDEACT, "="CONTEXT_ID},
|
|
|
+ {AT_CMD_QIACT, "="CONTEXT_ID},
|
|
|
{AT_CMD_QCFG, "=\"usbnet\",1"}, //set module to ecm mode
|
|
|
{AT_CMD_QNETDEVCTL, "=3,"CONTEXT_ID",0"},
|
|
|
{AT_CMD_CMGF, "=1"},
|
|
|
@@ -426,10 +428,30 @@ static void mem_print(char *buf, int len)
|
|
|
}
|
|
|
LOGD("\n");
|
|
|
}
|
|
|
+static int nic_set(int nic, int on)
|
|
|
+{
|
|
|
+ char tmp[200],path[100];
|
|
|
+ const char *ifname=sys_get_ifname(nic);
|
|
|
+
|
|
|
+ sprintf(path, "/var/run/udhcpc_%s.pid", ifname);
|
|
|
+
|
|
|
+ sprintf(tmp, "kill $(cat %s)", path);
|
|
|
+ system(tmp);
|
|
|
+
|
|
|
+ sprintf(tmp, "ip link set dev %s %s", ifname, on?"up":"down");
|
|
|
+ system(tmp);
|
|
|
+
|
|
|
+ if(on) {
|
|
|
+ sprintf(tmp, "udhcpc -R -b -i %s -p %s ", ifname, path);
|
|
|
+ system(tmp);
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
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);
|
|
|
+static void conn_init(cell_handle_t *h, mqtt_conn_t *conn);
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
@@ -930,6 +952,16 @@ static int cell_sock_connected(cell_handle_t *h)
|
|
|
{
|
|
|
return 0;
|
|
|
}
|
|
|
+static mqtt_conn_t* get_mqtt_conn_free(cell_handle_t *h)
|
|
|
+{
|
|
|
+ int i;
|
|
|
+ for(i=0; i<MQTT_MAX; i++) {
|
|
|
+ if(h->mq_conn[i].h==NULL) {
|
|
|
+ return &h->mq_conn[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
static int cell_mqtt_init(cell_handle_t *h)
|
|
|
{
|
|
|
int i;
|
|
|
@@ -945,7 +977,6 @@ static int cell_mqtt_conn(cell_handle_t *h, mqtt_para_t *para)
|
|
|
{
|
|
|
int r;
|
|
|
char tmp[200],buf[100];
|
|
|
- at_cmd_t *pcmd=NULL;
|
|
|
mqtt_conn_t *conn=NULL;
|
|
|
|
|
|
conn = get_mqtt_conn_free(h);
|
|
|
@@ -954,17 +985,15 @@ static int cell_mqtt_conn(cell_handle_t *h, mqtt_para_t *para)
|
|
|
}
|
|
|
conn_init(h, conn);
|
|
|
|
|
|
- pcmd = data_of(CMD_QMTOPEN);
|
|
|
sprintf(tmp, "=%d,\"%s\",%d", conn->cliId, para->host, para->port);
|
|
|
- r = send_cmd(h, pcmd, tmp, buf, sizeof(buf), 1);
|
|
|
- if(r!=CELL_OK) {
|
|
|
+ r = send_cmd(h, AT_CMD_QMTOPEN, tmp, buf, sizeof(buf));
|
|
|
+ if(r!=AT_OK) {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- pcmd = data_of(CMD_QMTCONN);
|
|
|
sprintf(tmp, "=%d,\"mqtt%d\",\"%s\",\"%s\"", conn->cliId, conn->cliId, para->username, para->password);
|
|
|
- r = send_cmd(h, pcmd, tmp, buf, sizeof(buf), 1);
|
|
|
- if(r!=CELL_OK) {
|
|
|
+ r = send_cmd(h, AT_CMD_QMTCONN, tmp, buf, sizeof(buf));
|
|
|
+ if(r!=AT_OK) {
|
|
|
return -1;
|
|
|
}
|
|
|
conn->h = h;
|
|
|
@@ -979,16 +1008,14 @@ static int cell_mqtt_disconn(mqtt_conn_t *conn)
|
|
|
char tmp[100],buf[100];
|
|
|
at_cmd_t *pcmd=NULL;
|
|
|
|
|
|
- pcmd = data_of(CMD_QMTDISC);
|
|
|
sprintf(tmp, "=%d", conn->cliId);
|
|
|
- r = send_cmd(conn->h, pcmd, tmp, buf, sizeof(buf), 1);
|
|
|
- if(r!=CELL_OK) {
|
|
|
+ r = send_cmd(conn->h, AT_CMD_QMTDISC, tmp, buf, sizeof(buf));
|
|
|
+ if(r!=AT_OK) {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- pcmd = data_of(CMD_QMTCLOSE);
|
|
|
- r = send_cmd(conn->h, pcmd, tmp, buf, sizeof(buf), 1);
|
|
|
- if(r!=CELL_OK) {
|
|
|
+ r = send_cmd(conn->h, AT_CMD_QMTCLOSE, tmp, buf, sizeof(buf));
|
|
|
+ if(r!=AT_OK) {
|
|
|
return -1;
|
|
|
}
|
|
|
conn->msgId = 0;
|
|
|
@@ -1002,10 +1029,9 @@ static int cell_mqtt_sub(mqtt_conn_t *conn, char *topic)
|
|
|
char buf[500];
|
|
|
at_cmd_t *pcmd=NULL;
|
|
|
|
|
|
- pcmd = data_of(CMD_QMTSUB);
|
|
|
sprintf(buf, "=%d, %d, \"%s\",%d", conn->cliId, conn->msgId, topic, conn->qos);
|
|
|
- r = send_cmd(conn->h, pcmd, buf, NULL, 0, 1);
|
|
|
- if(r!=CELL_OK) {
|
|
|
+ r = send_cmd(conn->h, AT_CMD_QMTSUB, buf, NULL, 0);
|
|
|
+ if(r!=AT_OK) {
|
|
|
return -1;
|
|
|
}
|
|
|
return 0;
|
|
|
@@ -1013,21 +1039,19 @@ static int cell_mqtt_sub(mqtt_conn_t *conn, char *topic)
|
|
|
static int cell_mqtt_pub(mqtt_conn_t *conn, char *topic, void *data, int len)
|
|
|
{
|
|
|
char buf[200],*pd=(char*)data;
|
|
|
- at_cmd_t *pcmd=data_of(CMD_QMTPUB);
|
|
|
int r=0,rt,offset=0,remain_len,send_len;
|
|
|
|
|
|
sprintf(buf, "=%d,%d,%d,%d,\"%s\",%d", conn->cliId, conn->msgId, conn->qos, conn->retain, topic, len);
|
|
|
- rt = send_cmd(conn->h, pcmd, NULL, buf, sizeof(buf), 1);
|
|
|
- if(rt!=CELL_OK) {
|
|
|
+ rt = send_cmd(conn->h, AT_CMD_QMTPUB, NULL, buf, sizeof(buf));
|
|
|
+ if(rt!=AT_OK) {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
while(offset<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);
|
|
|
- if(rt!=CELL_OK) {
|
|
|
+ if(rt!=AT_OK) {
|
|
|
r = -1; break;
|
|
|
}
|
|
|
offset += send_len;
|
|
|
@@ -1297,9 +1321,9 @@ int cellular_init(void)
|
|
|
h->pcell = &h->dm->_globalDevInfo.cell;
|
|
|
h->prod_id = h->dm->_globalDevInfo.product.id;
|
|
|
h->lck = lock_d_init();
|
|
|
+ nic_set(NIC_USB, 1);
|
|
|
|
|
|
thread_start(THREAD_ID_CELLULAR, cellular_thread, h);
|
|
|
- while(1) sleep(10);
|
|
|
#endif
|
|
|
|
|
|
return 0;
|