From: kyz Date: Fri, 23 Jun 2023 06:29:00 +0000 (+0800) Subject: 蓝牙配网服务增加ip变化通知 X-Git-Url: http://kyz.pub/gitweb/?a=commitdiff_plain;h=f4bcf595474280d260bf4019dd5a0f5cb20d63c5;p=yn%2Falarmmonitor.git 蓝牙配网服务增加ip变化通知 --- diff --git a/app/src/device_netconfig.c b/app/src/device_netconfig.c index 0b809a9..11ec594 100644 --- a/app/src/device_netconfig.c +++ b/app/src/device_netconfig.c @@ -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; diff --git a/ble_wificonfig/Server/src/gatt-service.c b/ble_wificonfig/Server/src/gatt-service.c index f45a38e..efc1f8a 100644 --- a/ble_wificonfig/Server/src/gatt-service.c +++ b/ble_wificonfig/Server/src/gatt-service.c @@ -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"); }