蓝牙配网服务增加ip变化通知
authorkyz <kongyuzhong@gmail.com>
Fri, 23 Jun 2023 06:29:00 +0000 (14:29 +0800)
committerkyz <kongyuzhong@gmail.com>
Sun, 25 Jun 2023 01:00:58 +0000 (09:00 +0800)
app/src/device_netconfig.c
ble_wificonfig/Server/src/gatt-service.c

index 0b809a99baa8e0ea3772b21862420eaed21c73d7..11ec59481460c6684a39e7bdf1e1c14fd779633c 100644 (file)
@@ -93,6 +93,8 @@ void wifi_setup_success( char * ssid, char *password )
 
 extern int open_udp_listen( void );
 extern int close_udp_listen( void );
+extern char *ble_get_local_ip( char* buf, int32_t buf_sz );
+extern void ip_change_notify( char * ip );
 
 void ble_wificonfig_start( void );
 void ble_wificonfig_stop( void );
@@ -100,7 +102,11 @@ void * device_netconfig_task( void * para )
 {
        mg_dev_t *dev = ( mg_dev_t *)para;
        int32_t ble_start = 0;
+       char ip_str[20];
+       char ip_str_new[20];
 
+       ble_get_local_ip( ip_str, sizeof( ip_str ) );
+       
        while(1){
 
                if( !ble_start &&  dev->netconfig ){
@@ -118,12 +124,18 @@ void * device_netconfig_task( void * para )
                        ble_wificonfig_stop();
                        close_udp_listen();
                }
-               if( ble_start ){
-                       //wait timeout
-                       usleep( 1000*1000 );
-               }else{
-                       usleep( 1000*1000 );
+
+
+               //scan ip
+               ble_get_local_ip( ip_str_new, sizeof( ip_str_new ) );
+               
+               if( strcmp( ip_str, ip_str_new ) ){ 
+                       MG_LOGD( "ip change [%s] -> [%s]\n", ip_str, ip_str_new );
+                       //ip changed, to notify
+                       strcpy( ip_str, ip_str_new );
+                       ip_change_notify( ip_str );
                }
+               usleep( 500*1000 );
        }
 
        return NULL;
index f45a38e90bf6d61331c5f8decfe973ff33a9c2cd..efc1f8a206ff0e13e0a3d2e1fc1b48c20db2946d 100644 (file)
@@ -219,11 +219,12 @@ static int read_the_system_PID()
 
 #define ETH_NAME "wlan0"//使用的网卡名
 
-char* get_local_ip( char*ip, int32_t len )
+static char *get_local_ip( void )
 {
     int sock;
     struct sockaddr_in sin;
     struct ifreq ifr;
+
     sock = socket(AF_INET, SOCK_DGRAM, 0);
     if (sock == -1) {
         perror("socket");
@@ -233,16 +234,26 @@ char* get_local_ip( char*ip, int32_t len )
     ifr.ifr_name[IFNAMSIZ - 1] = 0;
     if (ioctl(sock, SIOCGIFADDR, &ifr) < 0) {
         perror("ioctl");
+       close(sock);
         return NULL;
     }
 
     memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
     close(sock);
 
-    strncpy( ip, inet_ntoa(sin.sin_addr), len );
-    return ip;
+    return inet_ntoa(sin.sin_addr);
 }
 
+char *ble_get_local_ip( char* buf, int32_t buf_sz )
+{
+       char *ip_str = get_local_ip();
+       if( ip_str ){
+               strncpy( buf, ip_str, buf_sz ); 
+       }else{
+               strcpy( buf, "0" );
+       }
+       return buf;
+}
 
 
 #ifdef DUEROS
@@ -1314,14 +1325,11 @@ static DBusMessage *chr_read_value(DBusConnection *conn, DBusMessage *msg,
                                chr_write(chr, str, strlen(str));
                 // printf("PRODUCTLD_UUID \n"); 
         }
-               
+
                if (!strcmp(IP_UUID, chr->uuid)){
-                       char ip[50];
-                       if( get_local_ip( ip, sizeof(ip) )){
-                               chr_write(chr, ip, strlen( ip ) );
-                       }else{
-                               chr_write(chr, "0", 1 );
-                       } 
+                       char ip[20];
+                       ble_get_local_ip( ip, sizeof(ip) );
+                       chr_write(chr, ip, strlen( ip ) );
         }
        chr_read(chr, &iter);
        strncpy(str, chr->value, chr->vlen);
@@ -1625,10 +1633,25 @@ static DBusMessage *chr_write_value(DBusConnection *conn, DBusMessage *msg,
 
        return dbus_message_new_method_return(msg);
 }
+struct characteristic *ip_change_notify_chr = NULL;
+
+void ip_change_notify( char * ip )
+{
+       if( connection && ip_change_notify_chr ){
+                chr_write( ip_change_notify_chr, ip, strlen(ip) ); 
+       }
+}
 
 static DBusMessage *chr_start_notify(DBusConnection *conn, DBusMessage *msg,
                                                        void *user_data)
 {
+       struct characteristic *chr = user_data;
+
+       if (!strcmp(IP_UUID, chr->uuid)){
+               ip_change_notify_chr = chr;
+       }
+
+printf( "------------------------------------%s -chr uuid:%s\n", __func__, chr->uuid );
        return g_dbus_create_error(msg, DBUS_ERROR_NOT_SUPPORTED,
                                                        "Not Supported");
 }
@@ -1636,6 +1659,11 @@ static DBusMessage *chr_start_notify(DBusConnection *conn, DBusMessage *msg,
 static DBusMessage *chr_stop_notify(DBusConnection *conn, DBusMessage *msg,
                                                        void *user_data)
 {
+       struct characteristic *chr = user_data;
+       if (!strcmp(IP_UUID, chr->uuid)){
+               ip_change_notify_chr = NULL;
+       }
+printf( "------------------------------------%s -chr uuid:%s\n", __func__, chr->uuid );
        return g_dbus_create_error(msg, DBUS_ERROR_NOT_SUPPORTED,
                                                        "Not Supported");
 }