瀏覽代碼

1、更新最新qt程序
2、添加usb网口在邮件和mqtt的绑定

guohui 1 年之前
父節點
當前提交
e37b67eea9
共有 8 個文件被更改,包括 138 次插入 和 1 次删除
  1. 二進制
      cust_data/qt/Qt_Test
  2. 二進制
      cust_data/qt/rgb/Qt_Test
  3. 16 0
      pro/src/Cellular.c
  4. 1 0
      pro/src/Cellular.h
  5. 6 1
      pro/src/mail.c
  6. 12 0
      pro/src/mqtt.c
  7. 91 0
      pro/src/sys.c
  8. 12 0
      pro/src/sys.h

二進制
cust_data/qt/Qt_Test


二進制
cust_data/qt/rgb/Qt_Test


+ 16 - 0
pro/src/Cellular.c

@@ -1138,6 +1138,10 @@ static int cell_sock_stat(sock_conn_t *conn)
 
     return 0;
 }
+static int cell_sock_connected(cell_handle_t *h)
+{
+    return 0;
+}
 static int cell_send_sms(cell_handle_t *h, char *number, char *txt)
 {
     int r=-1,rt;
@@ -1954,6 +1958,18 @@ int cellular_get_stat(cell_stat_t *stat)
 }
 
 
+int cellular_is_connected(void)
+{
+    int r=0;
+    cell_handle_t *h=&ceHandle;
+
+#ifdef USE_CELL
+    r = cell_sock_connected(h);
+#endif
+
+    return r;
+}
+
 
 
 int cellular_test(void)

+ 1 - 0
pro/src/Cellular.h

@@ -20,5 +20,6 @@ int cellular_reset(void);
 
 int cellular_get_info(cell_info_t *info);
 int cellular_get_stat(cell_stat_t *stat);
+int cellular_is_connected(void);
 
 #endif

+ 6 - 1
pro/src/mail.c

@@ -1,6 +1,8 @@
 #include <stdint.h>
 #include <pthread.h>
 #include "cfg.h"
+#include "sys.h"
+#include "cellular.h"
 #include "common.h"
 #include "thread.h"
 #include "xlist.h"
@@ -147,8 +149,11 @@ static int send_mail(mail_send_t *send, void *addr, char *sub, char *body, char
     //curl_easy_setopt(curl, CURLOPT_LOGIN_OPTIONS, tmp);
     
     r = curl_easy_setopt(curl, CURLOPT_USERNAME, send->account);        //setting user
-    r = curl_easy_setopt(curl, CURLOPT_PASSWORD, send->password);      //setting password
+    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(strcmp(send->port, "587")==0) { //tls
         r = curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);    //setting openssl/tls

+ 12 - 0
pro/src/mqtt.c

@@ -8,6 +8,7 @@
 #include "elog.h"
 #include "lock.h"
 #include "sys.h"
+#include "cellular.h"
 #include "xlist.h"
 #include "websocket_handle.h"
 #include "switch_ctrl.h"
@@ -344,6 +345,14 @@ static int stop_all(mqtt_handle_t *h)
     return 0;
 }
 
+static int mg_mqtt_bind(mg_conn_t *c)
+{
+    int r=0;
+    if(cellular_is_connected()) {
+        r = sys_bind_nic(NIC_USB, (int)c->fd);
+    }
+    return r;
+}
 static int conn_one(mqtt_conn_t *conn)
 {
     int r=-1;
@@ -374,8 +383,11 @@ static int conn_one(mqtt_conn_t *conn)
         };
 
         get_url(&conn->ser, url);
+
         conn->c = mg_mqtt_connect(&conn->mgr, url, &opts, mqtt_fn, conn);
         if(conn->c) {
+            mg_mqtt_bind(conn->c);
+
             sprintf(conn->ser.cid, "smartPDU_%d\n", h->prod_id);
             my_sub(conn->c, h->prod_id);
             r = 0;

+ 91 - 0
pro/src/sys.c

@@ -902,6 +902,96 @@ int sys_get_net(NetworkInfo_t *para, int ipVersion)
     return r;
 }
 
+const char *sys_get_ifname(int nic)
+{
+    const char *ifname[NIC_MAX]={"eth0","eth1","usb0","wlan0"};
+    return ifname[nic];
+}
+int sys_nic_connected(int nic, int timeout)
+{
+    int fd,r=-1,ret;
+    char ipaddr[64];
+    sa_family_t family;
+    struct ifaddrs *ifap, *ifa;
+    const char *dstIP4="8.8.8.8";
+    const char *dstIP6="2001:4860:4860::8888";
+    const char *ifname=sys_get_ifname(nic);
+
+    if (getifaddrs(&ifap) == -1) {
+        LOGE("___getifaddrs error\n");
+        return -1;
+    }
+
+    fd = socket(AF_INET, SOCK_STREAM, 0);
+    if (fd<0) {
+        freeifaddrs(ifap);
+        return -1;
+    }
+
+    for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
+        if (ifa->ifa_addr != NULL && strcmp(ifa->ifa_name, ifname)==0) {
+
+            family = ifa->ifa_addr->sa_family;
+            if(family==AF_INET) {
+                struct sockaddr_in *sin=(struct sockaddr_in *)ifa->ifa_addr;
+                
+                inet_ntop(family, ifa->ifa_addr, ipaddr, INET6_ADDRSTRLEN);
+                if(bind(fd, (struct sockaddr *)sin, sizeof(*sin))==0) {
+                    struct timeval tv={timeout/1000,(timeout%1000)*1000};
+                    struct sockaddr_in remote={0};
+
+                    setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
+
+                    remote.sin_family = AF_INET;
+                    remote.sin_port = htons(53);
+                    inet_pton(AF_INET, dstIP4, &remote.sin_addr);
+                    ret = connect(fd, (struct sockaddr *)&remote, sizeof(remote));
+                    close(fd);
+                    if(ret==0) {
+                        r = 0; break;
+                    }
+                }
+            }
+            else {
+                struct sockaddr_in6 *sin=(struct sockaddr_in6 *)ifa->ifa_addr;
+                inet_ntop(family, ifa->ifa_addr, ipaddr, INET6_ADDRSTRLEN);
+                if(bind(fd, (struct sockaddr *)sin, sizeof(*sin))==0) {
+                    struct timeval tv={timeout/1000,(timeout%1000)*1000};
+                    struct sockaddr_in6 remote={0};
+
+                    setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
+
+                    remote.sin6_family = AF_INET6;
+                    remote.sin6_port = htons(53);
+                    inet_pton(AF_INET6, dstIP6, &remote.sin6_addr);
+                    ret = connect(fd, (struct sockaddr *)&remote, sizeof(remote));
+                    close(fd);
+                    if(ret==0) {
+                        r = 0; break;
+                    }
+                }  
+            }
+        }
+    }
+    freeifaddrs(ifap);
+
+    return r;
+}
+int sys_bind_nic(int nic, int fd)
+{
+    struct ifreq ifr;
+    const char *ifname=sys_get_ifname(nic);
+
+    memset(&ifr, 0, sizeof(ifr));
+    strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
+    
+    if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,(void *)&ifr, sizeof(ifr)) < 0) {
+        LOGE("___setsockopt(SO_BINDTODEVICE) failed\n");
+        return -1;
+    }
+    return 0;
+}
+
 
 int sys_is_running(char *prog)
 {
@@ -926,6 +1016,7 @@ int sys_is_running(char *prog)
 }
 
 
+
 int sys_get_disk_ava_MB(char *path)
 {
     int r;

+ 12 - 0
pro/src/sys.h

@@ -15,6 +15,14 @@ enum {
     IP_V4,
     IP_V6,
 };
+enum {
+    NIC_ETH0=0,
+    NIC_ETH1,
+    NIC_USB,
+    NIC_WLAN,
+    
+    NIC_MAX
+};
 
 
 int sys_init(void);
@@ -29,6 +37,10 @@ int sys_is_running(char *prog);
 int sys_get_path(char *path, char *name);
 int sys_get_net(NetworkInfo_t *para, int ipVersion);
 int sys_set_net(NetworkInfo_t *para, int ipVersion);
+int sys_bind_nic(int nic, int fd);
+int sys_nic_connected(int nic, int timeout);
+const char *sys_get_ifname(int nic);
+
 int sys_get_mac(char *mac, int maclen);
 int sys_get_chip_id(int *id);
 int sys_ip_check(char *ip);