Просмотр исходного кода

使用手机卡,邮件和mqtt已通

guohui 1 год назад
Родитель
Сommit
fe2d133c70
5 измененных файлов с 70 добавлено и 35 удалено
  1. 50 26
      pro/src/Cellular.c
  2. 4 4
      pro/src/mail.c
  3. 1 1
      pro/src/mqtt.c
  4. 9 2
      pro/src/sys.c
  5. 6 2
      pro/src/wifi.c

+ 50 - 26
pro/src/Cellular.c

@@ -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;

+ 4 - 4
pro/src/mail.c

@@ -151,9 +151,9 @@ static int send_mail(mail_send_t *send, void *addr, char *sub, char *body, char
     r = curl_easy_setopt(curl, CURLOPT_USERNAME, send->account);        //setting user
     r = curl_easy_setopt(curl, CURLOPT_PASSWORD, send->password);       //setting password
 
-    if(cellular_is_connected()) {
-        r = curl_easy_setopt(curl, CURLOPT_INTERFACE, sys_get_ifname(NIC_USB));
-    }
+    //if(cellular_is_connected()) {
+        //r = curl_easy_setopt(curl, CURLOPT_INTERFACE, sys_get_ifname(NIC_USB));
+    //}
     
     if(strcmp(send->port, "587")==0) { //tls
         r = curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);    //setting openssl/tls
@@ -217,7 +217,7 @@ static int send_mail(mail_send_t *send, void *addr, char *sub, char *body, char
 
     r = curl_easy_perform(curl);
     if(r != CURLE_OK) {
-        LOGE("send mail failed, %s\n", curl_easy_strerror(r));
+        LOGE("send mail failed, %d, %s\n", r, curl_easy_strerror(r));
     }
     else {
         LOGD("send mail ok!\n");

+ 1 - 1
pro/src/mqtt.c

@@ -386,7 +386,7 @@ static int conn_one(mqtt_conn_t *conn)
 
         conn->c = mg_mqtt_connect(&conn->mgr, url, &opts, mqtt_fn, conn);
         if(conn->c) {
-            mg_mqtt_bind(conn->c);
+            //mg_mqtt_bind(conn->c);
 
             sprintf(conn->ser.cid, "smartPDU_%d\n", h->prod_id);
             my_sub(conn->c, h->prod_id);

+ 9 - 2
pro/src/sys.c

@@ -993,6 +993,14 @@ int sys_bind_nic(int nic, int fd)
 }
 
 
+int sys_route(int nic, char *gateway)
+{
+    char tmp[200];
+    sprintf(tmp, "ip route add default via %s dev %s", gateway, sys_get_ifname(nic));
+    return system(tmp);
+}
+
+
 int sys_is_running(char *prog)
 {
     FILE *fp;
@@ -1824,10 +1832,9 @@ int sys_init(void)
 
     sys_chmod();
     init_hash_table();
-    
+
     net_init();
     ui_init();
-
     thread_start(THREAD_ID_POLLING, polling_thread, NULL);
 
     return 0;

+ 6 - 2
pro/src/wifi.c

@@ -103,6 +103,7 @@ static void res_free(result_t *res)
 }
 static int udhcpc_start(int on)
 {
+    int r;
     char tmp[200],path[100];
 
     sprintf(path, "/var/run/udhcpc_%s.pid", WLAN_IFNAME);
@@ -112,7 +113,9 @@ static int udhcpc_start(int on)
     else {
         sprintf(tmp, "kill $(cat %s)", path);
     }
-    return system(tmp);
+
+    r = system(tmp);
+    return r;
 }
 static int sta_start(ap_info_t *info, int on)
 {
@@ -760,6 +763,7 @@ int wifi_set(WIFIManager *wm)
         strcpy(info.ssid, wm->WIFI_name);
         strcpy(info.password, wm->WIFI_code);
 
+
         sta_start(&info, 0);
         r = sta_start(&info, 1);
     }  
@@ -801,7 +805,7 @@ int wifi_sta_stat(ap_stat_t *stat)
     if(r==0) {
         stat->link = 1; //不知道何原因导致获取信号强度的回调函数触发不了,暂时绕过
     }
-    printf("______ link: %d, signal: %d, ip: %s\n", stat->link, stat->signal, stat->ipaddr);   
+    //printf("______ link: %d, signal: %d, ip: %s\n", stat->link, stat->signal, stat->ipaddr);   
 #endif
     
     return r;