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 );
{
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 ){
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;
#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");
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
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);
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");
}
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");
}