蓝牙配网功能
authorkyz <kongyuzhong@gmail.com>
Mon, 19 Jun 2023 08:57:40 +0000 (16:57 +0800)
committerkyz <kongyuzhong@gmail.com>
Wed, 21 Jun 2023 05:43:00 +0000 (13:43 +0800)
13 files changed:
app/inc/device.h
app/src/device_button.c
app/src/device_config.c
app/src/device_ctrl.c
app/src/device_gesture.c
app/src/device_light.c
app/src/device_netconfig.c [new file with mode: 0644]
app/src/device_status.c
app/src/device_wifi.c
app/src/iot_dp.c
app/src/main.c
app/src/rk_ble_app.c
ble_wificonfig/Server/src/gatt-service.c

index e1176c3166899e337c0f7193e35efab896cd30b3..cd2cf6470e400defbf1c479f2bebe792209608f2 100644 (file)
@@ -7,6 +7,7 @@
 #include "tof.h"
 #include "iot.h"
 #include "mg_log.h"
+#include "mg_version.h"
 #include "led.h"
 #include "button.h"
 #include "cQueue.h" 
@@ -18,6 +19,8 @@
 #define IOT_KEY_PATH       "/customer/tuya_key"
 #define DEV_CFG_PATH       "dev.cfg"
 #define TUYA_AUTH_PATH     "tuya_auth"
+#define TUYA_BIND_PATH     "tuya.bind"
+#define WIFI_CONFIG_PATH   "wifi.cfg"
 #define MG_ACTIVE_PATH     "mg.active"
 #define READ_UUID_CMD      "cat cpuinfo | grep Serial"
 #define ALS_CFG_PATH       "als.cfg"
 #define IOT_KEY_PATH       "/customer/tuya_key"
 #define DEV_CFG_PATH       "/appconfigs/dev.cfg"
 #define ALS_CFG_PATH       "/appconfigs/als.cfg"
+
 #define TUYA_AUTH_PATH     "/customer/tuya_auth"
+#define TUYA_BINDCODE_PATH "/customer/tuya_bindcode"
+
+#define TUYA_BIND_PATH     "/appconfigs/tuya.bind"
+#define WIFI_CONFIG_PATH   "/appconfigs/wifi.cfg"
 #define MG_ACTIVE_PATH     "/customer/mg.active"
+
+#define HW_VER_PATH       "/customer/hw_version"
+
 #define READ_UUID_CMD      "cat /proc/cpuinfo | grep Serial"
 
 #endif
 
+#define NETCONFIG_TIMEOUT_SEC  (2*60) //配网超时2分钟
+#define CLEANMODE_TIMEOUT_SEC  (30)   //清洁模式超时30秒
+
 //环境光传感器默认阈值
 #define ALS_THRESHOLD_HIGH_DEFAULT  1000
 #define ALS_THRESHOLD_LOW_DEFAULT    400
@@ -61,6 +75,7 @@ typedef enum{
        DS_AUTH_WIFICONFIGING, //配网过程中 LED7 & LED8 连续慢闪, 2分钟超时后退出
        DS_AUTH_WIFICONFIG_NO_ACTIVE, //已授权已配网未激活:所有灯光常灭;
        DS_ACTIVE, //已授权已配网已激活:进入主流程,灯光根据 DP 设置处理。
+       DS_INVALID 
 }dev_st_t;
 
 typedef struct{
@@ -97,7 +112,9 @@ typedef struct{
        uint32_t tof_st;   //人感状态
        uint32_t tof_sync; //人感状态同步
 
-       uint32_t mg_active;
+       uint32_t mg_active;     //moorgen 激活标志
+       uint32_t mg_wificonfig; //wificonfig 标志
+       uint32_t mg_tuyabind;   //涂鸦绑定标志
 
        uint32_t auth; //auth
        char *auth_key_id;
@@ -106,30 +123,44 @@ typedef struct{
        //pthread_mutex_t lock;
 
        uint32_t clean_device;
-       uint32_t clean_timeout;
+       uint32_t clean_start_time; //clean 开始时间
 
-       uint32_t wifi_config;
-       uint32_t wifi_config_timeout;
+       uint32_t netconfig; //wifi 配网 + tuya bind
+       time_t netconfig_start_time; //配网开始时间
 
        uint32_t bl_manual;
 
        uint32_t mg_tof_status;
        pthread_mutex_t lock;
+
+       //
+       btn_ctrl_t btn;
+       light_ctrl_t light;
+       mg_tof_t  *tof;
+
 }mg_dev_t;
 
 
+//灯光模式
 typedef enum{
+       //开机初始状态,所有灯常灭
        LIGHT_MODE_INIT = 0,
-//未授权:控制所有按键背光连续慢闪,装饰灯带常灭;不加载涂鸦 SDK应用程序轮询授权是否已写入,并校验写入长度符合要求;
+
+       //未授权:控制所有按键背光连续慢闪,装饰灯带常灭;不加载涂鸦 SDK应用程序轮询授权是否已写入,并校验写入长度符合要求;
        LIGHT_MODE_NO_AUTH,
-//已授权未配网:所有灯光常灭;
+
+       //已授权未配网:所有灯光常灭;
        LIGHT_MODE_AUTH_NO_WIFICONFIG,
-//配网过程中 LED7 & LED8 连续慢闪, 2分钟超时后退出
+
+       //配网过程中 LED7 & LED8 连续慢闪, 2分钟超时后退出
        LIGHT_MODE_AUTH_WIFICONFIGING,
-//已授权已配网未激活:所有灯光常灭;
+
+       //已授权已配网未激活:所有灯光常灭;
        LIGHT_MODE_AUTH_WIFICONFIG_NO_ACTIVE,
-//已授权已配网已激活:进入主流程,灯光根据 DP 设置处理。
+
+       //已授权已配网已激活:进入主流程,灯光根据 DP 设置处理。
        LIGHT_MODE_ACTIVE,
+
 //单个按键触发,按键对应背光快闪 1s,并上报按键触发类型⾄相应 DP;
 //按 5s KEY7 + KEY8,进入配网模式,LED7 & LED8 连续慢闪;
 //按 2s KEY1 + KEY8,进入清洁模式,所有 LED 连续慢闪;
@@ -137,9 +168,10 @@ typedef enum{
        LIGHT_MODE_CLEAN,
        LIGHT_MODE_TOF_ACTIVE,
        LIGHT_MODE_TOF_DEACTIVE,
+
 }light_mode_t;
 
-void light_mode_set( light_mode_t mode );
+void light_mode_set( mg_dev_t *dev, light_mode_t mode );
 //void light_brightness_set( led_id_t id, uint32_t br );
 
 void device_wifi_init( void );
@@ -155,7 +187,7 @@ int32_t load_auth_key( mg_dev_t *dev );
 int32_t is_device_active( mg_dev_t *dev );
 int32_t        mg_device_active( mg_dev_t *dev );
 
-int32_t mg_device_run_st_init( void );
+int32_t mg_device_run_st_init( mg_dev_t *dev );
 
 //----------------------------------------------------------------------------
 //cfg info
@@ -219,6 +251,7 @@ typedef enum{
 }gesture_ev_t;
 
 
+//-------------------------------------------------------------------------------------
 //iot 
 void iot_start( void );
 int32_t is_iot_connected( void );
@@ -240,11 +273,71 @@ int32_t dp_recognition_mode_report( uint32_t mode ); //0-"light_mode", 1-"dark_m
 int32_t dp_active_report( uint32_t active );
 int32_t dp_key_report( char *key_value );
 
-//
+//-------------------------------------------------------------------------------------
+//mg_dev
 void mg_dev_lock( mg_dev_t *dev );
 void mg_dev_unlock( mg_dev_t *dev );
+mg_dev_t * mg_dev_get( void );
 
+//-------------------------------------------------------------------------------------
 //
 int32_t load_als_threshold( uint32_t *ts_high, uint32_t *ts_low);
+
+void btn_light_ctrl_init( mg_dev_t *dev );
+void btn_light_ctrl_start(  mg_dev_t *dev );
+
+//-------------------------------------------------------------------------------------
+//button
+typedef struct{
+       uint32_t code;
+       uint32_t ev;
+       uint32_t value;
+}btn_msg_t;
+
+void device_button_init( mg_dev_t *dev );
+uint32_t get_single_click_code( btn_msg_t *msg );
+uint32_t get_double_click_code( btn_msg_t *msg );
+uint32_t get_long_press_code( btn_msg_t *msg );
+uint32_t get_long_press_up_code( btn_msg_t *msg );
+int32_t is_btn_device_clean_exit( btn_msg_t *msg  );
+int32_t is_btn_device_clean( btn_msg_t *msg  );
+int32_t is_btn_netconfig( btn_msg_t *msg  );
+int32_t btn_msg_rx( btn_msg_t *msg  );
+
+//-------------------------------------------------------------------------------------
+//nv
+int32_t wifi_config_store( char *ssid, char *passwd );
+int32_t wifi_config_load( char *ssid, uint32_t ssid_max_len, char *passwd, uint32_t passwd_max_len );
+
+//-------------------------------------------------------------------------------------
+//light
+void device_light_init( mg_dev_t *dev );
+void led_flash_for_btn_single_click( mg_dev_t *dev, button_code_t btn_code );
+void light_set_key_br( mg_dev_t *dev, uint32_t id, uint32_t br );
+void light_set_atmos_br( mg_dev_t *dev, uint32_t id, uint32_t br );
+
+//-------------------------------------------------------------------------------------
+//net config
+int32_t        netconfig_start( mg_dev_t *dev );
+int32_t        netconfig_stop( mg_dev_t *dev );
+void device_netconfig_task_start( mg_dev_t *dev );
+
+//-------------------------------------------------------------------------------------
+//gesture
+int32_t gesture_recognition_start( mg_dev_t *dev );
+
+//-------------------------------------------------------------------------------------
+//涂鸦用户绑定设备标志
+int32_t is_tuya_bind( mg_dev_t *dev );
+//wifi配网标志
+int32_t is_wifi_config( mg_dev_t *dev );
+//检查是否已经配网,需要wifi配网而且设备绑定
+int32_t is_tuya_config( mg_dev_t *dev );
+
+//-------------------------------------------------------------------------------------
+int32_t tuya_bindcode_read( char * code, int32_t max_sz );
+int32_t fw_version_read( char * ver, int32_t max_sz );
+int32_t hw_version_read( char * ver, int32_t max_sz );
+int32_t tuya_productid_read( char * ver, int32_t max_sz );
 #endif //__MG_DEVICE_H__
 
index f43c0f32193952ae1b47d8191de11546b27e8f3f..fde9a77bb4ccef69ed5c4cbeae86d731c18bdcd1 100644 (file)
@@ -6,25 +6,14 @@
 #include <pthread.h>
 #include "button.h"
 
-//#include "iot.h"
-//#include "mg_log.h"
-//#include "cQueue.h" 
 #include "device.h"
 
-static btn_ctrl_t *g_ctrl;
 static pthread_mutex_t btn_lock;
-
 static cQueue_t *btn_msg_q;
 
 static void button_single_cb( button_code_t btn_code, button_event_t btn_event, void* value );
 static void button_comb_cb( button_code_t btn_code, button_event_t btn_event, void* value );
 
-typedef struct{
-       uint32_t code;
-       uint32_t ev;
-       uint32_t value;
-}btn_msg_t;
-
 int32_t btn_msg_tx( btn_msg_t *msg  )
 {
 
@@ -32,7 +21,9 @@ int32_t btn_msg_tx( btn_msg_t *msg  )
        btn_msg_t *p;
        p = ( btn_msg_t *)malloc( sizeof(btn_msg_t) );
        memcpy( p, msg, sizeof( btn_msg_t ) );
+       pthread_mutex_lock( &btn_lock );
        cQPost( btn_msg_q, ( void *)p );
+       pthread_mutex_unlock( &btn_lock );
 }
 
 int32_t btn_msg_rx( btn_msg_t *msg  )
@@ -42,7 +33,9 @@ int32_t btn_msg_rx( btn_msg_t *msg  )
        if( !msg || !btn_msg_q )
                return -1;
 
+       pthread_mutex_lock( &btn_lock );
        p = cQRcv( btn_msg_q, &err );
+       pthread_mutex_unlock( &btn_lock );
        if( p ){
                memcpy( msg, p, sizeof( btn_msg_t ) );
                MG_LOGD( "btn msg rx: %x, %d, %c\n", msg->code, msg->ev, msg->value );
@@ -52,27 +45,28 @@ int32_t btn_msg_rx( btn_msg_t *msg  )
        return -1;
 }
 
-void device_button_init( void )
+void device_button_init( mg_dev_t *dev )
 {
+       btn_ctrl_t ctrl;
+
        MG_LOGE( "btn init" );
        pthread_mutex_init( &btn_lock, NULL );  
        btn_msg_q = cQcreate( 100 );    
-       g_ctrl = mg_button_open("/dev/input/by-path/platform-gpio_keys-event" );
-       if( g_ctrl ){
-               mg_button_event_register( g_ctrl, 0xFF, EVENT_SINGLE_CLICK|EVENT_DOUBLE_CLICK|EVENT_LONG_PRESS|EVENT_LONG_PRESS_UP, button_single_cb );
-               mg_button_event_combonation_register( g_ctrl, BUTTON_7|BUTTON_8, EVENT_COMB_PRESS_5S, 0, button_comb_cb );
-               mg_button_event_combonation_register( g_ctrl, BUTTON_1|BUTTON_8, EVENT_COMB_PRESS_2S, 0, button_comb_cb );
-               mg_button_event_combonation_register( g_ctrl, BUTTON_2|BUTTON_7, EVENT_COMB_PRESS_2S, 0, button_comb_cb );
+       ctrl = mg_button_open("/dev/input/by-path/platform-gpio_keys-event" );
+       if( ctrl ){
+               mg_button_event_register( ctrl, 0xFF, EVENT_SINGLE_CLICK|EVENT_DOUBLE_CLICK|EVENT_LONG_PRESS|EVENT_LONG_PRESS_UP, button_single_cb );
+               mg_button_event_combonation_register( ctrl, BUTTON_7|BUTTON_8, EVENT_COMB_PRESS_5S, 0, button_comb_cb );
+               mg_button_event_combonation_register( ctrl, BUTTON_1|BUTTON_8, EVENT_COMB_PRESS_2S, 0, button_comb_cb );
+               mg_button_event_combonation_register( ctrl, BUTTON_2|BUTTON_7, EVENT_COMB_PRESS_2S, 0, button_comb_cb );
        }else{
                MG_LOGE( "mg_button_open fail\n" );
        }
 }
 
-extern mg_dev_t g_mg_dev;
 
-int32_t is_key_disabled( button_code_t btn_code )
+static int32_t is_key_disabled( button_code_t btn_code )
 {
-       uint32_t mask = cfg_mg_key_enable_read( &g_mg_dev );
+       uint32_t mask = cfg_mg_key_enable_read( mg_dev_get() );
        if( (btn_code & mask ) != btn_code ){
                MG_LOGD( "drop key, code %02X, mask:%02X\n", mask, btn_code );  
                return 1;
@@ -107,7 +101,7 @@ static void button_comb_cb( button_code_t btn_code, button_event_t btn_event, vo
        printf( "COMB BTN cb--code:%x, event:%x, value:%p\n", btn_code, btn_event, value );
 } 
 
-int32_t is_btn_wifi_config( btn_msg_t *msg  )
+int32_t is_btn_netconfig( btn_msg_t *msg  )
 {
        if( msg->ev == EVENT_COMB_PRESS_5S && msg->code == BUTTON_7|BUTTON_8 )
                return 1;
index ed9291a2201219d246f7c455f3467f60115f2784..52e52d816d0f28e72ec18b80fab397ba3b1cef20 100644 (file)
@@ -168,6 +168,26 @@ int32_t is_device_active( mg_dev_t *dev )
        return dev->mg_active;
 }
 
+static int32_t touch_file( char * path )
+{
+       char cmd[200];
+       sprintf( cmd, "touch %s", path );
+       if( system( cmd ) ){
+               //MG_LOGE( "" );
+       }
+       return 0;
+}
+
+int32_t mg_device_active_flag_write( void )
+{
+       touch_file( MG_ACTIVE_PATH ); 
+}
+
+int32_t mg_device_bind_flag_write( void )
+{
+       touch_file( TUYA_BIND_PATH ); 
+}
+
 int32_t read_device_uuid( mg_dev_t *dev )
 {
        FILE *fp;
@@ -220,8 +240,7 @@ int32_t     mg_device_active( mg_dev_t *dev )
                for( int i = 0; i<10; i++ ){
                        if( rx_key && !strcmp( dev->auth_key_value, received_key_value ) ){
                                dev->mg_active = 1;
-                               if( system( "touch /customer/mg.active" ) ){
-                                       MG_LOGE( "create active flag file  /customer/mg.active fail!" );
+                               if( mg_device_active_flag_write() ){
                                }
                                break;
                        }
@@ -655,7 +674,7 @@ int32_t wifi_config_store( char *ssid, char *passwd )
        int32_t ret = -1;
 
        if( ssid && passwd ){
-               if( (fd = fopen( "/customer/wifi.cfg", "w" ) )){
+               if( (fd = fopen( WIFI_CONFIG_PATH, "w" ) )){
                        fprintf( fd, "{\n\t\"ssid\":\"%s\",\n\t\"password\":\"%s\"\n}", ssid, passwd );
                        fclose( fd );
                        ret = 0;
@@ -671,19 +690,117 @@ int32_t wifi_config_load( char *ssid, uint32_t ssid_max_len, char *passwd, uint3
        cJSON *json_passwd;
        int32_t ret = -1;
 
-       root = load_json_data_from_file( "/customer/wifi.cfg" );
+       root = load_json_data_from_file( WIFI_CONFIG_PATH );
        if( root ){
                json_ssid =cJSON_GetObjectItem(root, "ssid");
                json_passwd =cJSON_GetObjectItem(root, "password");
                if( json_ssid && json_passwd ){
-                       strncpy( ssid, json_ssid->valuestring, ssid_max_len);   
-                       strncpy( passwd, json_passwd->valuestring, passwd_max_len);     
+                       if( ssid )
+                               strncpy( ssid, json_ssid->valuestring, ssid_max_len);   
+                       if( passwd )
+                               strncpy( passwd, json_passwd->valuestring, passwd_max_len);     
+                       ret = 0;
+               }
+               
+               cJSON_Delete( root );
+       }
+
+       return ret;
+}
+
+//涂鸦用户绑定设备标志
+int32_t is_tuya_bind( mg_dev_t *dev )
+{
+
+       int fd;
+       if( dev->mg_tuyabind)
+               return 1;
+
+       fd = open( TUYA_BIND_PATH, O_RDONLY );
+       if( fd >= 0  ){
+               dev->mg_tuyabind= 1;
+               close(fd);
+       }else{
+               MG_LOGD( "open [%s] fail: %d", TUYA_BIND_PATH, fd );
+       }
+
+       return dev->mg_tuyabind;
+}
+
+//wifi配网标志
+int32_t is_wifi_config( mg_dev_t *dev )
+{
+
+       if( dev->mg_wificonfig)
+               return 1;
+       if( wifi_config_load( NULL, 0, NULL, 0 ) == 0 ){
+               dev->mg_wificonfig = 1; 
+       }
+
+       return dev->mg_wificonfig;
+}
+
+//检查是否已经配网,需要wifi配网而且设备绑定
+int32_t is_tuya_config( mg_dev_t *dev )
+{
+       return is_wifi_config( dev ) && is_tuya_bind( dev );
+}
+
+int32_t tuya_bindcode_read( char * code, int32_t max_sz )
+{
+       cJSON *root;
+       cJSON *json_bindcode;
+       int32_t ret = -1;
+               
+       root = load_json_data_from_file( TUYA_BINDCODE_PATH );
+       if( root ){
+               json_bindcode =cJSON_GetObjectItem(root, "bindcode");
+               if( json_bindcode ){
+                       if( code && max_sz )
+                               strncpy( code, json_bindcode->valuestring, max_sz );    
                        ret = 0;
                }
+               cJSON_Delete( root );
+       }
+
+       return ret;
+}
+
+int32_t fw_version_read( char * ver, int32_t max_sz )
+{
+       strncpy( ver, SW_VERSION, max_sz ); 
+       return 0;
+}
+
+int32_t hw_version_read( char * ver, int32_t max_sz )
+{
+       cJSON *root;
+       cJSON *json_ver;
+       int32_t ret = -1;
                
+       root = load_json_data_from_file( HW_VER_PATH );
+       if( root ){
+               json_ver = cJSON_GetObjectItem(root, "hw_version");
+               if( json_ver ){
+                       if( ver && max_sz )
+                               strncpy( ver, json_ver->valuestring, max_sz );  
+                       ret = 0;
+               }
                cJSON_Delete( root );
        }
 
        return ret;
 }
 
+int32_t tuya_productid_read( char * pid, int32_t max_sz )
+{
+       int32_t ret = -1;
+       char *productId;
+
+       if( iot_key_load( &productId, NULL, NULL ) == 0 ){
+               strncpy( pid, productId, max_sz );
+               ret = 0;
+       }
+       return ret;
+}
+
index 4918d8ad54dc6e08ea11f1c245afd4b3da6d0d50..7467a655846fc8c8edaf7e773806ad54a9c540bf 100644 (file)
 #include "device.h"
 #include "tof.h"
 
-extern mg_dev_t g_mg_dev;
-mg_tof_t *g_tof;
-
-#if 0
-
-int32_t mg_dp_get( key_ev_t *key_ev, timeout timeout ){ return 0; }
-int32_t mg_dp_put( key_ev_t *key_ev, timeout timeout ){ return 0; }
-//----------------------------------------------------------------
-#endif
-
-typedef struct{
-       uint32_t code;
-       uint32_t ev;
-       uint32_t value;
-}btn_msg_t;
-
-extern void led_flash_for_btn_single_click( button_code_t btn_code );
-extern uint32_t get_single_click_code( btn_msg_t *msg );
-extern uint32_t get_double_click_code( btn_msg_t *msg );
-extern uint32_t get_long_press_code( btn_msg_t *msg );
-extern uint32_t get_long_press_up_code( btn_msg_t *msg );
-extern int32_t is_btn_device_clean_exit( btn_msg_t *msg  );
-extern int32_t is_btn_device_clean( btn_msg_t *msg  );
-extern int32_t is_btn_wifi_config( btn_msg_t *msg  );
-
-extern void device_light_init( void );
-extern void light_set_key_br( uint32_t id, uint32_t br );
-extern void light_set_atmos_br( uint32_t id, uint32_t br );
-
-extern void device_button_init( void );
-extern int32_t btn_msg_rx( btn_msg_t *msg  );
-extern int32_t is_btn_wifi_config( btn_msg_t *msg  );
-extern int32_t is_btn_device_clean( btn_msg_t *msg  );
-extern int32_t is_btn_device_clean_exit( btn_msg_t *msg  );
-
-
-uint32_t device_clean_mode = 0;
-uint32_t device_wifi_config = 0;
-
-uint32_t device_bl_mode_manual = 0;
 
 //按键与背光子系统流程
-void * btn_light_ctrl_task( void* unused )
+void * btn_light_ctrl_task( void* para )
 {
+       mg_dev_t *dev;
        btn_msg_t btn_msg;
-       uint32_t to_wifi_config = 0;
+       uint32_t to_net_config = 0;
        uint32_t to_clean_mode = 0;
        uint32_t to_clean_mode_exit = 0;
        uint32_t btn_code = 0;
@@ -75,6 +36,8 @@ void * btn_light_ctrl_task( void* unused )
 
        MG_LOGI( "btn light ctrl task run..." );
 
+       dev = ( mg_dev_t * )para;
+
        tof_st = -1;
        bl_manual = -1;
        tof_check_time =0;
@@ -87,32 +50,36 @@ void * btn_light_ctrl_task( void* unused )
                        MG_LOGI( "get btn msg: %04X, %d, %d", btn_msg.code, btn_msg.ev, btn_msg.value );        
 
                        //如果是配网按键
-                       to_wifi_config = is_btn_wifi_config( &btn_msg );
+                       to_net_config = is_btn_netconfig( &btn_msg );
                        to_clean_mode = is_btn_device_clean( &btn_msg );
                        to_clean_mode_exit = is_btn_device_clean_exit( &btn_msg );
 
-                       if( !g_mg_dev.clean_device && to_clean_mode ){
+                       if( !dev->clean_device && to_clean_mode ){
 
                                MG_LOGI( "enter clean mode ..." );
-                               g_mg_dev.clean_device = 1;
-                               g_mg_dev.clean_timeout = time(NULL)+30;
+                               dev->clean_device = 1;
+                               dev->clean_start_time = time(NULL);
 
-                       }else if( g_mg_dev.clean_device && to_clean_mode_exit ){
+                       }else if( dev->clean_device && to_clean_mode_exit ){
 
                                MG_LOGI( "exit clean mode ..." );
-                               g_mg_dev.clean_device = 0;
-                               g_mg_dev.clean_timeout = 0;
-                       }else if( !g_mg_dev.clean_device && !g_mg_dev.wifi_config &&  to_wifi_config ){
-                               MG_LOGI( "enter wifi config mode ..." );
-                               g_mg_dev.wifi_config = 1;
-                               g_mg_dev.wifi_config_timeout = time(NULL)+2*60; //2minutes timeout
+                               dev->clean_device = 0;
+                               dev->clean_start_time = 0;
+                       }else if( !dev->clean_device && !dev->netconfig &&  to_net_config ){
+                               MG_LOGI( "enter net config mode ..." );
+                               //dev->netconfig = 1;
+                               //dev->netconfig_start_time = time(NULL);
+                               printf( "pressed key -- start net config, t:%ld\n", dev->netconfig_start_time );
+                               netconfig_start( dev );
                        }
 
-                       if( !g_mg_dev.clean_device && !g_mg_dev.wifi_config ){
+                       if( !dev->clean_device && !dev->netconfig ){
                                //获取单击按键的键值
-                               if((btn_code = get_single_click_code( &btn_msg ))){
+                               if( dev->st != DS_ACTIVE ){
+                                       //未激活,不响应按键
+                               }else if((btn_code = get_single_click_code( &btn_msg ))){
                                        MG_LOGD( "key led flash fast, btn_code:%04X\n", btn_code );
-                                       led_flash_for_btn_single_click( btn_code ); //单击按钮,LED快闪
+                                       led_flash_for_btn_single_click( dev, btn_code ); //单击按钮,LED快闪
                                        dp_button_event_report( btn_code, btn_msg.ev );
                                }else if((btn_code = get_double_click_code( &btn_msg ))){
                                        dp_button_event_report( btn_code, btn_msg.ev );
@@ -120,112 +87,114 @@ void * btn_light_ctrl_task( void* unused )
                                        dp_button_event_report( btn_code, btn_msg.ev );
                                }else if((btn_code = get_long_press_up_code( &btn_msg ))){
                                        dp_button_event_report( btn_code, btn_msg.ev );
-
-                                       //test gesture repot
-                                       //dp_gesture_event_report( GS_ILOVEYOU );
                                }
                        }
                }
 
-               if( g_mg_dev.clean_device  ){
+               if( dev->clean_device  ){
                        //检查清洁模式是否超时
-                       if( time(NULL) > g_mg_dev.clean_timeout ){
-                               g_mg_dev.clean_device = 0;
-                               g_mg_dev.clean_timeout = 0;
-                               g_mg_dev.wifi_config= 0;
-                               g_mg_dev.wifi_config_timeout = 0;
+                       if( dev->clean_start_time && time(NULL) - dev->clean_start_time > CLEANMODE_TIMEOUT_SEC ){
+                               mg_dev_lock( dev );
+                               dev->clean_device = 0;
+                               dev->clean_start_time = 0;
+                               mg_dev_unlock( dev );
+                               netconfig_stop( dev );
                        }
-               }else if( g_mg_dev.wifi_config){
+               }else if( dev->netconfig){
                        //检查配网超时
-                       if( time(NULL) > g_mg_dev.wifi_config_timeout ){
-                               g_mg_dev.wifi_config= 0;
-                               g_mg_dev.wifi_config_timeout = 0;
+                       if( dev->netconfig_start_time && time(NULL) - dev->netconfig_start_time > NETCONFIG_TIMEOUT_SEC ){
+                               printf( "net config, timeout, now:%ld, s:%ld\n", time(NULL), dev->netconfig_start_time );
+                               //mg_dev_lock( dev );
+                               //dev->netconfig= 0;
+                               //dev->netconfig_start_time = 0;
+                               //mg_dev_unlock( dev );
+                               netconfig_stop( dev );
                        }
                }
 
                //背光模式检查
-               if( bl_manual != cfg_bl_mode_read( &g_mg_dev )){
-                       bl_manual = cfg_bl_mode_read( &g_mg_dev );
+               if( bl_manual != cfg_bl_mode_read( dev )){
+                       bl_manual = cfg_bl_mode_read( dev );
                        MG_LOGI( "bl mode -> %s", bl_manual?"manual":"auto"  );
                        //背光模式改变,启动或停止tof
                        if( bl_manual ){
                                //
                                MG_LOGI( "bl manual mode, disable tof ..." );
-                               if( g_tof )
-                                       mg_tof_disable(g_tof);
+                               if( dev->tof )
+                                       mg_tof_disable(dev->tof);
                        }else{
                                //TOF_Enable...
                                MG_LOGI( "bl auto mode, enable tof ..." );
-                               if( g_tof )
-                                       mg_tof_enable(g_tof );
+                               if( dev->tof )
+                                       mg_tof_enable( dev->tof );
                        }
                }
 
-               dev_st_t st = g_mg_dev.st;
-               if( g_mg_dev.clean_device ){
-                       light_mode_set( LIGHT_MODE_CLEAN );
-               }else if( g_mg_dev.wifi_config ){
-                       light_mode_set( LIGHT_MODE_AUTH_WIFICONFIGING );
+               dev_st_t st = dev->st;
+               if( dev->clean_device ){
+                       light_mode_set( dev, LIGHT_MODE_CLEAN );
+               }else if( dev->netconfig ){
+                       light_mode_set( dev, LIGHT_MODE_AUTH_WIFICONFIGING );
                }else if( st == DS_INIT ){
-                       light_mode_set( LIGHT_MODE_INIT );
+                       light_mode_set( dev, LIGHT_MODE_INIT );
                }else if( st == DS_NO_AUTH ){
-                       light_mode_set( LIGHT_MODE_NO_AUTH );
+                       light_mode_set( dev, LIGHT_MODE_NO_AUTH );
                }else if( st == DS_AUTH_NO_WIFICONFIG ){
-                       light_mode_set( LIGHT_MODE_AUTH_NO_WIFICONFIG  );
+                       light_mode_set( dev, LIGHT_MODE_AUTH_NO_WIFICONFIG  );
                }else if( st == DS_AUTH_WIFICONFIGING ){
-                       light_mode_set( LIGHT_MODE_AUTH_WIFICONFIGING );
+                       light_mode_set( dev, LIGHT_MODE_AUTH_WIFICONFIGING );
                }else if( st == DS_AUTH_WIFICONFIG_NO_ACTIVE ){
-                       light_mode_set( LIGHT_MODE_AUTH_WIFICONFIG_NO_ACTIVE );
+                       light_mode_set( dev, LIGHT_MODE_AUTH_WIFICONFIG_NO_ACTIVE );
                }else if( st == DS_ACTIVE ){
 
                        if( bl_manual ){
                                uint32_t key_led_br;
                                uint32_t atmos_led_br;
 
-                               key_led_br = cfg_key_led_max_read( &g_mg_dev );
-                               atmos_led_br = cfg_atmos_led_max_read( &g_mg_dev );
+                               key_led_br = cfg_key_led_max_read( dev );
+                               atmos_led_br = cfg_atmos_led_max_read( dev );
 
                                //手动模式, 按dp调整亮度
-                               light_mode_set( LIGHT_MODE_ACTIVE );
-                               light_set_key_br( LED_ID_KEY_ALL, key_led_br );
-                               light_set_atmos_br( LED_ID_ATMOS_ALL, atmos_led_br );
+                               light_mode_set( dev, LIGHT_MODE_ACTIVE );
+                               light_set_key_br( dev, LED_ID_KEY_ALL, key_led_br );
+                               light_set_atmos_br( dev, LED_ID_ATMOS_ALL, atmos_led_br );
 
                        }else{
-                               tof_sync = cfg_mg_tof_sync_read( &g_mg_dev );
-                               row_disabled = cfg_mg_disable_row_read( &g_mg_dev );
+                               tof_sync = cfg_mg_tof_sync_read( dev );
+                               row_disabled = cfg_mg_disable_row_read( dev );
 
                                //自动模式 
-                               if( tof_threshold_cm!= cfg_mg_tof_threshold_read( &g_mg_dev ) ){
-                                       MG_LOGI( "tof dist change %d cm -> %d cm", tof_threshold_cm, cfg_mg_tof_threshold_read( &g_mg_dev ) );
-                                       tof_threshold_cm = cfg_mg_tof_threshold_read( &g_mg_dev );
+                               if( tof_threshold_cm!= cfg_mg_tof_threshold_read( dev ) ){
+                                       MG_LOGI( "tof dist change %d cm -> %d cm", tof_threshold_cm, cfg_mg_tof_threshold_read( dev ) );
+                                       tof_threshold_cm = cfg_mg_tof_threshold_read( dev );
                                        //阈值调整,重新设置阈值
-                                       Distance_parameter_correction(g_tof, tof_threshold_cm * 10 );
+                                       Distance_parameter_correction(dev->tof, tof_threshold_cm * 10 );
                                }
 
                                if( !row_disabled && tof_sync ){
                                        //联排功能,平台设置人感状态
-                                       light_mode_set( LIGHT_MODE_TOF_ACTIVE );
+                                       light_mode_set( dev, LIGHT_MODE_TOF_ACTIVE );
                                }
 
-                               if( g_tof && time(NULL ) - tof_check_time > cfg_mg_tof_interval_read( &g_mg_dev ) ){
+                               if( dev->tof && time(NULL ) - tof_check_time > cfg_mg_tof_interval_read( dev ) ){
 
                                        tof_check_time = time(NULL);
                                        memset( &info, 0, sizeof( info ) );
 
                                        //检查周期到达,读取tof状态
-                                       if( mg_tof_read( g_tof, &info )==0 ){
+                                       if( mg_tof_read( dev->tof, &info )==0 ){
                                                MG_LOGD( "read tof info, enable:%d, status:%d, dist:%d", info.enable, info.status, info.distance );
                                        }else{
-                                               MG_LOGW( "read tof info fail, g_tof:%p", g_tof );
+                                               MG_LOGW( "read tof info fail, g_tof:%p", dev->tof );
                                        }
 
                                        if( !row_disabled && tof_sync ){
                                                //联排功能,平台设置人感状态
-                                               light_mode_set( LIGHT_MODE_TOF_ACTIVE );
+                                               light_mode_set( dev, LIGHT_MODE_TOF_ACTIVE );
                                        }else if( info.enable && info.status == TOF_ANYBODY ){
-                                               light_mode_set( LIGHT_MODE_TOF_ACTIVE );
+                                               light_mode_set( dev, LIGHT_MODE_TOF_ACTIVE );
                                        }else{
-                                               light_mode_set( LIGHT_MODE_TOF_DEACTIVE );
+                                               light_mode_set( dev, LIGHT_MODE_TOF_DEACTIVE );
                                        }
                                        if( tof_st!= info.status ){
                                                //状态变化,上报DP
@@ -247,27 +216,26 @@ static void tof_cb( tof_info_t *info)
        MG_LOGI( "tof ec cb - tofinfo[ enable:%d, status:%d, dist:%d]", info->enable, info->status, info->distance );
 }
 
-void btn_light_ctrl_init( void )
+void btn_light_ctrl_init( mg_dev_t *dev )
 {
        uint32_t tof_threshold_cm;
 
-       device_button_init();
-       device_light_init();
+       device_button_init( dev );
+       device_light_init( dev );
 
-       g_tof = mg_tof_open( 1000 ); //1000mm
-       if( g_tof ){
-               mg_tof_event_register( g_tof, tof_cb );
-               tof_threshold_cm = cfg_mg_tof_threshold_read( &g_mg_dev );
-               Distance_parameter_correction(g_tof, tof_threshold_cm * 10 );
+       dev->tof = mg_tof_open( 1000 ); //1000mm
+       if( dev->tof ){
+               mg_tof_event_register( dev->tof, tof_cb );
+               tof_threshold_cm = cfg_mg_tof_threshold_read( dev );
+               Distance_parameter_correction(dev->tof, tof_threshold_cm * 10 );
        }else{
                MG_LOGE( "open tof fail!" );
        }
-
 }
 
-void btn_light_ctrl_start( void )
+void btn_light_ctrl_start(  mg_dev_t *dev )
 {
-       if( pthread_create( &th, NULL, btn_light_ctrl_task, (void *)NULL ) != 0 ){
+       if( pthread_create( &th, NULL, btn_light_ctrl_task, (void *)dev ) != 0 ){
                MG_LOGE( "create button and light ctrl task fail!\n" );
        }
 }
index 480564532020d4493d88e39f05c6119ca382ab0e..9ebe07798609f1551cc7d6971921eb6ac43b1245 100644 (file)
@@ -641,10 +641,7 @@ int32_t main( void )
 
 static gr_ctrl_t gr_ctrl;
 
-extern light_ctrl_t g_led_ctrl;
-extern mg_dev_t g_mg_dev;
-
-int32_t gesture_recognition_start(void)
+int32_t gesture_recognition_start( mg_dev_t *dev )
 {
     int32_t ret = 0;
     uint32_t als_th_high;
@@ -652,8 +649,8 @@ int32_t gesture_recognition_start(void)
 
     memset(&gr_ctrl, 0, sizeof(gr_ctrl));
 
-    gr_ctrl.light_ctrl = g_led_ctrl;
-    gr_ctrl.dev = &g_mg_dev;
+    gr_ctrl.light_ctrl = dev->light;
+    gr_ctrl.dev = dev;
     load_als_threshold(&als_th_high, &als_th_low);
 
     usleep(100 * 1000);
index 859cd4547b177fd8938d0a0a98cb6c4b09f7dc14..f23c0b093298a452c3f7e4bd8da8ecb4f80ed2a1 100644 (file)
@@ -3,16 +3,6 @@
 //#include "mg_log.h"
 //#include "button.h"
 #include "device.h"
-//--------------------------------------------------//
-/*      led ctrl api            */
-
-// mg_led_mode_set( g_led_ctrl, LED_ID_KEY_ALL|LED_ID_FUN_ALL, &cfg ); 
-// g_led_ctrl = mg_led_open();
-// mg_led_brightness_set( g_mg_led_ctrl, LED_ID_KEY_ALL, br ); 
-//--------------------------------------------------//
-
-light_ctrl_t  g_led_ctrl;
-extern mg_dev_t g_mg_dev;
 
 typedef struct{
        uint32_t key_led_max;
@@ -28,18 +18,18 @@ typedef struct{
 
 static lightinfo_t g_lightinfo; //保存灯状态
 
-void device_light_init( void )
+void device_light_init( mg_dev_t *dev )
 {
        MG_LOGI( "device light init" );
        g_lightinfo.mode = LIGHT_MODE_INIT;
        g_lightinfo.key_led_max = 100;
        g_lightinfo.fun_led_max = 100;
 
-       g_led_ctrl = mg_led_open();
+       dev->light= mg_led_open();
 
        //MG_LOGI( "device light init, g_led_ctrl:%p", g_led_ctrl );
-       if( g_led_ctrl ){
-               light_mode_set( LIGHT_MODE_INIT );
+       if( dev->light ){
+               light_mode_set( dev, LIGHT_MODE_INIT );
        }else{
                MG_LOGE( "mg_led_open fail!" );
        }
@@ -58,10 +48,10 @@ char *light_mode_name[]={
        "led_nochange",
 };
 
-void light_mode_set( light_mode_t mode )
+void light_mode_set( mg_dev_t *dev, light_mode_t mode )
 {
        uint32_t dur_sec;
-       uint32_t key_mask = cfg_mg_key_enable_read( &g_mg_dev );
+       uint32_t key_mask = cfg_mg_key_enable_read( dev );
 
        if( g_lightinfo.mode == mode )
                return;
@@ -71,70 +61,70 @@ void light_mode_set( light_mode_t mode )
        
        switch( g_lightinfo.mode ){
                case LIGHT_MODE_INIT:
-                       key_led_off( g_led_ctrl, LED_ID_KEY_ALL, 0, 0, 0);
-                       atmos_led_off( g_led_ctrl, LED_ID_ATMOS_ALL, 0, 0, 0);
-                       ring_led_off(  g_led_ctrl, 0, 0, 0);
+                       key_led_off( dev->light, LED_ID_KEY_ALL, 0, 0, 0);
+                       atmos_led_off( dev->light, LED_ID_ATMOS_ALL, 0, 0, 0);
+                       ring_led_off(  dev->light, 0, 0, 0);
                        break;
                case LIGHT_MODE_NO_AUTH:
-                       key_led_flash( g_led_ctrl, LED_ID_KEY_ALL&key_mask, 1000, 0);
-                       atmos_led_off( g_led_ctrl, LED_ID_ATMOS_ALL, 0, 0, 0);
-                       ring_led_off(  g_led_ctrl, 0, 0, 0);
+                       key_led_flash( dev->light, LED_ID_KEY_ALL&key_mask, 1000, 0);
+                       atmos_led_off( dev->light, LED_ID_ATMOS_ALL, 0, 0, 0);
+                       ring_led_off(  dev->light, 0, 0, 0);
                        break;
                case LIGHT_MODE_CLEAN: //clean mode en
                        //进入清洁模式,所有 LED 连续慢闪;
-                       key_led_flash( g_led_ctrl, LED_ID_KEY_ALL&key_mask, 1000, 0);
-                       atmos_led_flash( g_led_ctrl, LED_ID_ATMOS_ALL, 1000, 0);
+                       key_led_flash( dev->light, LED_ID_KEY_ALL&key_mask, 1000, 0);
+                       atmos_led_flash( dev->light, LED_ID_ATMOS_ALL, 1000, 0);
                        break;
 
                case LIGHT_MODE_AUTH_WIFICONFIGING : //wifi config 
-                       key_led_off( g_led_ctrl, LED_ID_KEY_ALL&~(LED_ID_KEY_7|LED_ID_KEY_8), 0, 0, 0);
-                       key_led_flash( g_led_ctrl, LED_ID_KEY_7|LED_ID_KEY_8,     1000, 0);
-                       atmos_led_off( g_led_ctrl, LED_ID_ATMOS_ALL, 0, 0, 0);
+                       key_led_off( dev->light, LED_ID_KEY_ALL&~(LED_ID_KEY_7|LED_ID_KEY_8), 0, 0, 0);
+                       key_led_flash( dev->light, LED_ID_KEY_7|LED_ID_KEY_8,     1000, 0);
+                       atmos_led_off( dev->light, LED_ID_ATMOS_ALL, 0, 0, 0);
                        //进入配网模式,所有 LED7 LED8 连续慢闪;
                        break;
 
                case LIGHT_MODE_AUTH_WIFICONFIG_NO_ACTIVE: //
-                       key_led_off( g_led_ctrl, LED_ID_KEY_ALL, 0, 0, 0);
-                       atmos_led_off( g_led_ctrl, LED_ID_ATMOS_ALL, 0, 0, 0);
-                       ring_led_off(  g_led_ctrl, 0, 0, 0);
+                       key_led_off( dev->light, LED_ID_KEY_ALL, 0, 0, 0);
+                       atmos_led_off( dev->light, LED_ID_ATMOS_ALL, 0, 0, 0);
+                       ring_led_off(  dev->light, 0, 0, 0);
                        break;
 
                case LIGHT_MODE_ACTIVE: //
-                       if( cfg_bl_mode_read( &g_mg_dev) ){
+                       if( cfg_bl_mode_read( dev) ){
                                //manual mode
-                               key_led_on( g_led_ctrl, LED_ID_KEY_ALL&key_mask, 0, cfg_key_led_max_read( &g_mg_dev ), 0);
-                               atmos_led_on( g_led_ctrl, LED_ID_ATMOS_ALL, 0, cfg_atmos_led_max_read( &g_mg_dev ), 0);
+                               key_led_on( dev->light, LED_ID_KEY_ALL&key_mask, 0, cfg_key_led_max_read( dev ), 0);
+                               atmos_led_on( dev->light, LED_ID_ATMOS_ALL, 0, cfg_atmos_led_max_read( dev ), 0);
                        }
                        break;
 
                case LIGHT_MODE_TOF_ACTIVE: //
-                       dur_sec = cfg_led_atmos_off_duration_read( &g_mg_dev );
-                       key_led_on( g_led_ctrl, LED_ID_KEY_ALL&key_mask, 0, 100, dur_sec*1000 );
-                       atmos_led_off( g_led_ctrl, LED_ID_ATMOS_ALL, 100, 0, dur_sec*1000 );
-                       //ring_led_breath( g_led_ctrl, 4000, 10*1000 );
+                       dur_sec = cfg_led_atmos_off_duration_read( dev );
+                       key_led_on( dev->light, LED_ID_KEY_ALL&key_mask, 0, 100, dur_sec*1000 );
+                       atmos_led_off( dev->light, LED_ID_ATMOS_ALL, 100, 0, dur_sec*1000 );
+                       //ring_led_breath( dev->light, 4000, 10*1000 );
                        break;
 
                case LIGHT_MODE_TOF_DEACTIVE: //
-                       dur_sec = cfg_led_atmos_on_duration_read( &g_mg_dev );
-                       key_led_off( g_led_ctrl, LED_ID_KEY_ALL, 100, 0, dur_sec*1000 );
-                       //atmos_led_on( g_led_ctrl, LED_ID_ATMOS_ALL, 0, 100, 3*1000 );
-                       atmos_led_on( g_led_ctrl, LED_ID_ATMOS_ALL, 0, 100, dur_sec*1000 );
-                       //ring_led_breath( g_led_ctrl, 2000, 0 );
+                       dur_sec = cfg_led_atmos_on_duration_read( dev );
+                       key_led_off( dev->light, LED_ID_KEY_ALL, 100, 0, dur_sec*1000 );
+                       //atmos_led_on( dev->light, LED_ID_ATMOS_ALL, 0, 100, 3*1000 );
+                       atmos_led_on( dev->light, LED_ID_ATMOS_ALL, 0, 100, dur_sec*1000 );
+                       //ring_led_breath( dev->light, 2000, 0 );
                        break;
 
        }
 }
 
-void light_set_atmos_br( uint32_t id, uint32_t br )
+void light_set_atmos_br( mg_dev_t *dev, uint32_t id, uint32_t br )
 {
-       atmos_led_br_update( g_led_ctrl, id, br );
+       atmos_led_br_update( dev->light, id, br );
 }
-void light_set_key_br( uint32_t id, uint32_t br )
+void light_set_key_br( mg_dev_t *dev, uint32_t id, uint32_t br )
 {
-       key_led_br_update( g_led_ctrl, id, br );
+       key_led_br_update( dev->light, id, br );
 }
 
-void led_flash_for_btn_single_click( button_code_t btn_code )
+void led_flash_for_btn_single_click( mg_dev_t *dev, button_code_t btn_code )
 {
        uint32_t led_id = 0;
 
@@ -148,7 +138,7 @@ void led_flash_for_btn_single_click( button_code_t btn_code )
        else if( btn_code & BUTTON_8 ) led_id = LED_ID_KEY_8;
 
        if( led_id ){
-               key_led_flash( g_led_ctrl, led_id, 125, 1*1000 );
+               key_led_flash( dev->light, led_id, 125, 1*1000 );
        }
 }
 
diff --git a/app/src/device_netconfig.c b/app/src/device_netconfig.c
new file mode 100644 (file)
index 0000000..36b4930
--- /dev/null
@@ -0,0 +1,240 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <pthread.h>
+//#include "button.h"
+//#include "iot.h"
+//#include "mg_log.h"
+#include "cQueue.h" 
+#include "device.h"
+
+#include "rk_ble_app.h"
+
+int32_t        netconfig_start( mg_dev_t *dev )
+{
+       printf( "netconfig_start,now:%ld, , cfg:%d, s:%ld\n", time(NULL),  dev->netconfig, dev->netconfig_start_time );
+       mg_dev_lock( dev );
+       dev->netconfig = 1;
+       dev->netconfig_start_time = time(NULL);
+       mg_dev_unlock( dev );
+       return 0;
+}
+
+int32_t        netconfig_stop( mg_dev_t *dev )
+{
+       printf( "netconfig_stop,now:%ld, , cfg:%d, s:%ld\n", time(NULL),  dev->netconfig, dev->netconfig_start_time );
+       mg_dev_lock( dev );
+       dev->netconfig = 0;
+       dev->netconfig_start_time = 0;
+       mg_dev_unlock( dev );
+       return 0;
+}
+
+int32_t netconfig_success_notify_cb( void )
+{
+       mg_device_bind_flag_write(); //create flag 
+       netconfig_stop( mg_dev_get() );
+}
+
+#if 0
+
+static int32_t wifi_config_st = 0; //1:ok, 2:fail
+static int32_t wifi_config_start_flag = 0;
+static pthread_t th;
+
+
+int32_t wifi_config_store( char *ssid, char *passwd );
+int32_t wifi_config_load( char *ssid, uint32_t ssid_max_len, char *passwd, uint32_t paaswd_max_len );
+
+int32_t wificonfig_start( void )
+{
+       wificonfig_start_flag = 1;
+       return 0;
+}
+
+int32_t wificonfig_stop( void )
+{
+       wificonfig_start_flag = 0;
+       return 0;
+}
+
+#endif
+
+void wifi_setup_fail( void )
+{
+
+}
+
+void wifi_setup_success( char * ssid, char *password )
+{
+       char sys_cmd[100];
+
+       //sed -i 's/ssid.*/ssid = "<\1>"/g'
+       //sed -i 's/psk.*/psk = "<\1>"/g'
+       
+       if( system( "killall wpa_supplicant" ) ){
+       }
+
+       sprintf( sys_cmd, "sed -i 's/ssid.*/ssid=\"%s\"/g' /appconfigs/service/wpa_supplicant.conf ", ssid );
+       if( system( sys_cmd ) ){
+       }
+
+       sprintf( sys_cmd, "sed -i 's/psk.*/psk=\"%s\"/g' /appconfigs/service/wpa_supplicant.conf ", password );
+       if( system( sys_cmd ) ){
+       }
+
+       if( system( "/etc/init.d/S99_tw8013_wifi start" ) ){
+       }
+
+       wifi_config_store( ssid, password );
+}
+
+extern int open_udp_listen( void );
+extern int close_udp_listen( void );
+
+void ble_wificonfig_start( void );
+void ble_wificonfig_stop( void );
+void * device_netconfig_task( void * para )
+{
+       mg_dev_t *dev = ( mg_dev_t *)para;
+       int32_t ble_start = 0;
+
+       while(1){
+
+               if( !ble_start &&  dev->netconfig ){
+                       printf( "ble start....\n" );
+                       //ble_start();
+                       //int ble_test_main( void );
+                       //ble_test_main();
+                       ble_start = dev->netconfig;
+                       ble_wificonfig_start();
+                       //open_udp_listen();
+               }else if( ble_start &&  !dev->netconfig ){
+                       printf( "ble stop....\n" );
+                       //ble_stop();
+                       ble_start = dev->netconfig;
+                       ble_wificonfig_stop();
+                       //close_udp_listen();
+               }
+               if( ble_start ){
+                       //wait timeout
+                       usleep( 1000*1000 );
+               }else{
+                       usleep( 1000*1000 );
+               }
+       }
+
+       return NULL;
+#if 0
+       MG_LOGD( "wifi task ..." );
+
+       if( wifi_config_load( ssid, sizeof(ssid), passwd, sizeof( passwd ) )!=0 ){
+               //设备没有配网信息,启动配网
+               printf( "load wifi cfg: fail!\n" );
+               wifi_config_start();
+       }else{
+               //已经配网
+               printf( "load wifi cfg: %s - %s\n", ssid, passwd );
+               wifi_config_start_flag = 0;
+       }
+
+       int n=0;
+       while(1){
+               sleep(2);
+
+               if( ble_start == 0 && wifi_config_start_flag ){
+                       ble_start = 1;
+                       ble_start_time = time(NULL);
+                       //启动配网, 在回调函数中完成配网动作
+                       rk_ble_wifi_init(NULL);
+               }
+
+               if( ble_start ){
+                       if( !wifi_config_start_flag || time(NULL)-ble_start_time > 5*60 ){ //TODO 暂按5分钟
+                               //关闭
+                               ble_start = 0;
+                               rk_ble_wifi_deinit( NULL );
+
+                       }else if( time(NULL)-ble_start_time > 5*60 ){ //TODO 暂按5分钟
+                               //超时
+                               ble_start = 0;
+                               rk_ble_wifi_deinit( NULL );
+
+                               //设备之前没有配网,重新打开配网
+                               if( wifi_config_load( ssid, sizeof(ssid), passwd, sizeof( passwd ) )!=0 )
+                                       wifi_config_start_flag = 1;
+
+                       }else if( !wifi_config_st ){
+                               //配网成功
+                               ble_start = 0;
+                               rk_ble_wifi_deinit( NULL );
+                       }
+               }
+       }
+#endif
+}
+
+//sed -i 's/ssid.*/ssid = "<\1>"/g'
+//sed -i 's/psk.*/psk = "<\1>"/g'
+
+//1. wifi 配网状态。 //customer/wifi.cfg  json:{ ssid:xxxx, psk:xxxx },
+//2. tuya bind状态。 //customer/tuyao.bindcode json:{ bindcode:xxxxxxxx },
+/*
+   1. 未配网,未绑定,启动ble advertise
+   2. 组合键, 启动广播
+   3. 每次上电, 启动广播
+
+   连接后收取到指令, 如果配网,更新wifi conf文件。
+   如果绑定, 发送bind code,
+   配网,成功连接后退出, 
+   bind 收到退出指令后退出。
+   超时退出。
+ */
+
+static pthread_t netconfig_tid;
+void device_netconfig_task_start( mg_dev_t *dev )
+{
+       if( pthread_create( &netconfig_tid, NULL, device_netconfig_task, (void *)dev) != 0 ){
+               MG_LOGE( "create button and light ctrl task fail!\n" );
+       }
+}
+
+#if 0
+int32_t        tuya_config_start( mg_dev_t *dev );
+int32_t        tuya_config_stop( mg_dev_t *dev );
+
+
+int32_t        wificonfig_start( mg_dev_t *dev )
+{
+       mg_dev_lock( dev );
+       dev->wifi_config = 1;
+       dev->wifi_config_timeout = time(NULL)+2*60;//2minutes timeout
+       mg_dev_unlock( dev );
+       //TODO: start ble adver ...
+       g_ble_adv = 1;
+       return 0;
+}
+
+int32_t        wificonfig_stop( mg_dev_t *dev )
+{
+       mg_dev_lock( dev );
+       dev->wifi_config = 0;
+       dev->wifi_config_timeout = 0;
+       mg_dev_unlock( dev );
+       //TODO: stop ble adver ...
+       g_ble_adv = 0;
+       return 0;
+}
+
+int32_t        wificonfig_success( mg_dev_t *dev )
+{
+       return 0;
+}
+
+int32_t        wificonfig_fail( mg_dev_t *dev )
+{
+       return 0;
+}
+#endif
index c9c43402c58002879025d71eded4f6b63c7a00a6..e494533c0dc2602e23e7b58b174ab58cb97d1412 100644 (file)
@@ -12,10 +12,11 @@ static int32_t version_init( void )
        return 0;
 }
 
-int32_t mg_device_run_st_init( void )
+int32_t mg_device_run_st_init( mg_dev_t *dev )
 {
        version_init();
 
        return 0;
 }
 
+
index f8240290ba51e6ea44e5693efd77d92fe701378d..4f48483f04c1db3ef40fd08e9ab04a92c9938faf 100644 (file)
@@ -12,6 +12,8 @@
 
 #include "rk_ble_app.h"
 
+#if 0
+
 static int32_t wifi_config_st = 0; //1:ok, 2:fail
 static int32_t wifi_config_start_flag = 0;
 static pthread_t th;
@@ -63,6 +65,7 @@ void wifi_config_fail( void )
        wifi_config_st = 2;
 }
 
+
 void device_wifi_init( void )
 {
        MG_LOGI( "wifi init" );
@@ -134,7 +137,6 @@ void * device_wifi_task( void* unused )
 
 //1. wifi 配网状态。 //customer/wifi.cfg  json:{ ssid:xxxx, psk:xxxx },
 //2. tuya bind状态。 //customer/tuyao.bindcode json:{ bindcode:xxxxxxxx },
-/*
        1. 未配网,未绑定,启动ble advertise
        2. 组合键, 启动广播
        3. 每次上电, 启动广播
@@ -144,7 +146,6 @@ void * device_wifi_task( void* unused )
        配网,成功连接后退出, 
        bind 收到退出指令后退出。
        超时退出。
-*/
 
 void device_wifi_start( void )
 {
@@ -153,3 +154,5 @@ void device_wifi_start( void )
        }
 }
 
+#endif
+
index 8d9be96190c6e0bdc72237aba11bf88d0f9de15a..e2f0c63f1bbbf56795098b7dc7404d1f4154d303 100644 (file)
@@ -16,7 +16,6 @@
 extern int32_t recv_dp_key_value_cb( char * value );
 static int32_t dp_event_report( char *code, char *event );
 
-extern mg_dev_t g_mg_dev;
 
 cQueue_t *g_dp_event_q;
 
@@ -194,23 +193,27 @@ void handle_iot_property_msg(tuya_mqtt_context_t* context, char *data_string )
        cJSON *root;
        cJSON *item;
        char buf[300];
+       mg_dev_t *dev;
 
        if( !context || !data_string ){
                return;
        }
 
+
        root = cJSON_Parse((const char *)data_string);
        if( root ){
+               dev = mg_dev_get();
+
                //button_1 .. buttont_8: - event
                if(( item =cJSON_GetObjectItem(root, "key_led_max"))){
                        //按键背光最大亮度
-                       cfg_key_led_max_write( &g_mg_dev, item->valueint );
+                       cfg_key_led_max_write( dev, item->valueint );
                        dp_property_report_int( "key_led_max",  item->valueint );
 
                }
                if(( item =cJSON_GetObjectItem(root, "atmos_led_max"))){
                        //氛围灯最大亮度
-                       cfg_atmos_led_max_write( &g_mg_dev, item->valueint );
+                       cfg_atmos_led_max_write( dev, item->valueint );
                        dp_property_report_int( "atmos_led_max",  item->valueint );
                }
                //"recognition_running" - ro
@@ -220,17 +223,17 @@ void handle_iot_property_msg(tuya_mqtt_context_t* context, char *data_string )
                        //不支持下发,忽略
                }
                if(( item =cJSON_GetObjectItem(root, "recognition_level"))){ //手势识别灵敏度
-                       cfg_recognition_level_write( &g_mg_dev, item->valueint);
+                       cfg_recognition_level_write( dev, item->valueint);
                        dp_property_report_int( "recognition_level",  item->valueint );
                }
                if(( item =cJSON_GetObjectItem(root, "recognition_duration"))){ //手势识别关闭延时
-                       cfg_recognition_duration_write( &g_mg_dev, item->valueint);
+                       cfg_recognition_duration_write( dev, item->valueint);
                        dp_property_report_int( "recognition_duration",  item->valueint );
                }
 
                if(( item =cJSON_GetObjectItem(root, "privacy_mode"))){ //"privacy_mode" 隐私模式 rw
                        //printf( "privacy_mode -->%d\r\n", item->valueint );
-                       cfg_privacy_mode_write( &g_mg_dev, item->valueint );
+                       cfg_privacy_mode_write( dev, item->valueint );
                        dp_property_report_bool( "privacy_mode",  item->valueint );
                }
 
@@ -245,22 +248,22 @@ void handle_iot_property_msg(tuya_mqtt_context_t* context, char *data_string )
                }
                if(( item =cJSON_GetObjectItem(root, "led_atmos_on_duration"))){
                        //氛围灯打开时间
-                       cfg_led_atmos_on_duration_write( &g_mg_dev, item->valueint);
+                       cfg_led_atmos_on_duration_write( dev, item->valueint);
                        dp_property_report_int( "led_atmos_on_duration",  item->valueint );
                }
                if(( item =cJSON_GetObjectItem(root, "mg_tof_threshold"))){
                        //人感检测距离
-                       cfg_mg_tof_threshold_write( &g_mg_dev, item->valueint);
+                       cfg_mg_tof_threshold_write( dev, item->valueint);
                        dp_property_report_int( "mg_tof_threshold",  item->valueint );
                }
                if(( item =cJSON_GetObjectItem(root, "mg_tof_interval"))){
                        //人感检测周期
-                       cfg_mg_tof_interval_write( &g_mg_dev, item->valueint);
+                       cfg_mg_tof_interval_write( dev, item->valueint);
                        dp_property_report_int( "mg_tof_interval",  item->valueint );
                }
                if(( item =cJSON_GetObjectItem(root, "mg_disalbe_row"))){
                        //关闭联排功能
-                       cfg_mg_disable_row_write( &g_mg_dev, item->valueint);
+                       cfg_mg_disable_row_write( dev, item->valueint);
                        dp_property_report_bool( "mg_disalbe_row",  item->valueint );
                }
                if(( item =cJSON_GetObjectItem(root, "mg_key_enable"))){
@@ -269,7 +272,7 @@ void handle_iot_property_msg(tuya_mqtt_context_t* context, char *data_string )
                        uint32_t olen = 0;
                        //需要通过base64解码
                        if( mbedtls_base64_decode( (uint8_t*)&mask, 3, &olen, item->valuestring, strlen( item->valuestring ) ) == 0 && olen >= 1 ){
-                               cfg_mg_key_enable_write( &g_mg_dev, mask&0xff);
+                               cfg_mg_key_enable_write( dev, mask&0xff);
                                MG_LOGD( "decode base64_str[%s] to hex[%08X] olen:%d\n", item->valuestring, mask, olen );
                                dp_property_report_str( "mg_key_enable",  item->valuestring);
                        }else{
@@ -278,7 +281,7 @@ void handle_iot_property_msg(tuya_mqtt_context_t* context, char *data_string )
                }
                if(( item =cJSON_GetObjectItem(root, "bl_mode"))){
                        //背光模式 默认"自动",  自动模式: 氛围灯亮, 背光灭, 有人后反转.   手动模式: 氛围灯可调, 背光亮度可调, 两者无关联
-                       cfg_bl_mode_write( &g_mg_dev, strcmp(item->valuestring, "manual" )==0?1:0);
+                       cfg_bl_mode_write( dev, strcmp(item->valuestring, "manual" )==0?1:0);
                        dp_property_report_str( "bl_mode",  item->valuestring  );
                }
                if(( item =cJSON_GetObjectItem(root, "mg_tof_staus"))){
@@ -289,12 +292,12 @@ void handle_iot_property_msg(tuya_mqtt_context_t* context, char *data_string )
                }
                if(( item =cJSON_GetObjectItem(root, "mg_tof_sync"))){
                        //人感状态同步
-                       cfg_mg_tof_sync_write( &g_mg_dev, item->valueint );
+                       cfg_mg_tof_sync_write( dev, item->valueint );
                        //dp_property_report_bool( "mg_tof_sync", item->valueint );
                }
                if(( item =cJSON_GetObjectItem(root, "led_atmos_off_duration"))){
                        //氛围灯关闭时间, 数值范围: 1-20, 间距: 1, 倍数: 0, 单位: 秒  默认5秒
-                       cfg_led_atmos_off_duration_write( &g_mg_dev, item->valueint);
+                       cfg_led_atmos_off_duration_write( dev, item->valueint);
                        dp_property_report_int( "led_atmos_off_duration",  item->valueint );
                }
                cJSON_Delete( root );
index 308174239aa99b59657f57348dafcb965ffe661f..65e1c2a0f35b5ab2c6ea884b2bfe0423f0bb836b 100644 (file)
 
 #define PID_FILE "/var/run/mg_app/mg_app.pid"
 
-extern void device_wifi_start( void );
-extern void mg_device_st_init( void );
+static mg_dev_t g_mg_dev;
 
-extern void btn_light_ctrl_init( void );
-extern void btn_light_ctrl_start( void );
-
-extern int32_t gesture_recognition_start( void );
-
-mg_dev_t g_mg_dev;
+mg_dev_t * mg_dev_get( void )
+{
+       return &g_mg_dev;
+}
 
 char * mg_st_name( dev_st_t st )
 {
@@ -39,57 +36,56 @@ char * mg_st_name( dev_st_t st )
        else if( st == DS_AUTH_WIFICONFIGING ) return "DS_AUTH_WIFICONFIGING";
        else if( st == DS_AUTH_WIFICONFIG_NO_ACTIVE ) return "DS_AUTH_WIFICONFIG_NO_ACTIVE";
        else if( st == DS_ACTIVE ) return "DS_ACTIVE";
+       else if( st == DS_INVALID ) return "DS_INVALID";
        return "unkown";
 }
 
-dev_st_t mg_device_st_get(void)
+dev_st_t mg_device_st_get( mg_dev_t *dev )
 {
-       dev_st_t st;
-       //lock
-       st = g_mg_dev.st;
-       //unlock
+       dev_st_t st = DS_INVALID;
+       if( dev ){
+               mg_dev_lock( dev );
+               st = g_mg_dev.st;
+               mg_dev_unlock( dev );
+       }
        return st;
 }
 
-dev_st_t mg_device_st_set(dev_st_t st )
+dev_st_t mg_device_st_set( mg_dev_t *dev, dev_st_t st )
 {
        dev_st_t old_st;
-       //lock
-       old_st  = g_mg_dev.st;
-       g_mg_dev.st = st;
-       //unlock
+
+       if( !dev || st >= DS_INVALID)
+               return DS_INVALID;
+
+       mg_dev_lock( dev );
+       old_st  = dev->st;
+       dev->st = st;
+       mg_dev_unlock( dev );
+
        if( old_st != st ){
                MG_LOGD( "mg device st %s -> %s\n", mg_st_name( old_st ), mg_st_name( st ) );
                if( st ==  DS_INIT ){
-                       light_mode_set( LIGHT_MODE_INIT );
+                       light_mode_set( dev, LIGHT_MODE_INIT );
                }else if( st == DS_NO_AUTH ){
-                       light_mode_set( LIGHT_MODE_NO_AUTH );
+                       light_mode_set( dev, LIGHT_MODE_NO_AUTH );
                }else if( st == DS_AUTH_NO_WIFICONFIG){
-                       light_mode_set( LIGHT_MODE_AUTH_NO_WIFICONFIG );
+                       light_mode_set( dev, LIGHT_MODE_AUTH_NO_WIFICONFIG );
                }else if( st == DS_AUTH_WIFICONFIG_NO_ACTIVE ){
-                       light_mode_set( LIGHT_MODE_AUTH_WIFICONFIG_NO_ACTIVE );
+                       light_mode_set( dev, LIGHT_MODE_AUTH_WIFICONFIG_NO_ACTIVE );
                }else if( st == DS_ACTIVE){
-                       light_mode_set( LIGHT_MODE_ACTIVE );
+                       light_mode_set( dev, LIGHT_MODE_ACTIVE );
                }
        }
-       return old_st;
-}
 
-int lock_file(int fd)
-{
-       struct flock fl;
-       fl.l_type = F_WRLCK;
-       fl.l_start = 0;
-       fl.l_whence = SEEK_SET;
-       fl.l_len = 0;
-
-       return (fcntl(fd, F_SETLK, &fl));
+       return old_st;
 }
 
-int alone_runnind(void)
+static int alone_runnind(void)
 {
        int fd;
        char buf[16];
+       struct flock fl;
 
        fd = open(PID_FILE, O_RDWR | O_CREAT, 0666);
        if (fd < 0)
@@ -98,7 +94,12 @@ int alone_runnind(void)
                exit(1);
        }
 
-       if (lock_file(fd) < 0)
+       fl.l_type = F_WRLCK;
+       fl.l_start = 0;
+       fl.l_whence = SEEK_SET;
+       fl.l_len = 0;
+
+       if(fcntl(fd, F_SETLK, &fl)<0)
        {
                if (errno == EACCES || errno == EAGAIN)
                {
@@ -113,18 +114,34 @@ int alone_runnind(void)
 
        if( ftruncate(fd, 0) ){  //设置文件的大小为0
        }
+
        sprintf(buf, "%ld\n", (long)getpid());
        if( write(fd, buf, strlen(buf) + 1)){
        }
+
        //这里不关闭文件, 保持锁定状态
 
        return 0;
 }
 
-int32_t mg_device_init( void )
+int32_t mg_device_init( mg_dev_t *dev )
 {
-       memset( &g_mg_dev, 0, sizeof( g_mg_dev ) );
-       pthread_mutex_init( &g_mg_dev.lock, NULL );     
+
+       if( !dev )
+               return -1;
+
+       memset( dev, 0, sizeof( mg_dev_t ) );
+       pthread_mutex_init( &dev->lock, NULL ); 
+
+       //从配置文件读取参数
+       load_device_cfg( &dev->cfg );
+       //printf( "load cfg, key_led_max:%d, led_atmos_off_duration:%d\n", g_mg_dev.cfg.key_led_max, g_mg_dev.cfg.led_atmos_off_duration );     
+
+       dev->st = DS_INIT;
+
+       //var/run/mg_app目录下输出信息
+       mg_device_run_st_init( dev );
+
        return 0;
 }
 
@@ -136,60 +153,77 @@ int main( void )
                exit( -1 );
        }
 
-       //var/run/mg_app目录下输出信息
-       mg_device_run_st_init();
-
        mg_log_init();
 
        MG_LOGD( "mg app start..." );
 
        //设备状态初始化
-       mg_device_init();
-
-       //从配置文件读取参数
-       load_device_cfg( &g_mg_dev.cfg );
-       //printf( "load cfg, key_led_max:%d, led_atmos_off_duration:%d\n", g_mg_dev.cfg.key_led_max, g_mg_dev.cfg.led_atmos_off_duration );     
+       mg_device_init( &g_mg_dev );
 
        //中间件初始化
-       btn_light_ctrl_init();
-       device_wifi_init();
+       btn_light_ctrl_init( &g_mg_dev );
 
-       mg_device_st_set( DS_INIT );
+       //device_wifi_init();
 
        // 启动wifi task;
-       device_wifi_start();
+       //device_wifi_start();
+       device_netconfig_task_start( &g_mg_dev ); //start tuya bind and wificonfig
+
+       //启动背光按键控制
+       btn_light_ctrl_start( &g_mg_dev );
 
        //检查授权文件
        while( load_auth_key( &g_mg_dev ) != 0 ){
                //未授权,等待授权
-               mg_device_st_set( DS_NO_AUTH );
+               mg_device_st_set( &g_mg_dev, DS_NO_AUTH );
                MG_LOGD( "wait mg auth" );
                sleep(5);
        }
 
-#if 0
-       //TODO:检查配网
        //检查配网状态
-       while( !is_wifi_config( &g_mg_dev ) ){
-               //等待配网
-               mg_device_st_set( DS_AUTH_NO_WIFICONFIG );
-               sleep(5);
-               if timeout goto ...
+       if( !is_tuya_config( &g_mg_dev ) ){
+               netconfig_start( &g_mg_dev );
+               while( !is_tuya_config( &g_mg_dev ) ){
+                       sleep(1);
+               }
+       }
+#if 0
+       while( !is_tuya_config( &g_mg_dev ) ){
+               time_t start_time = time(NULL);
+               //启动配网
+               net_config_start();
+               tuya_config_start( &g_mg_dev );
+               //等待配网 完成
+               while( is_tuya_config( &g_mg_dev ) ){
+                       mg_device_st_set( &g_mg_dev, DS_AUTH_NO_WIFICONFIG);
+                       MG_LOGD( "wait mg auth" );
+                       sleep(5);
+                       if( time(NULL)-start_time > 2*60 ){
+                               tuya_config_stop( &g_mg_dev );
+                               mg_device_st_set( &g_mg_dev, DS_AUTH_NO_WIFICONFIG );
+
+                               //配网超时  等待组合按键 再次启动配网
+                               while( !is_tuya_config( &g_mg_dev ) && g_mg_dev.wifi_config == 0){
+                                       usleep(1000*1000 );
+                               }
+                               break;
+                       }
+                       //如果两分钟超时,等待组合按键再进入配网流程
+               }
        }
 #endif
-
        if( is_device_active( &g_mg_dev ) ){
-               //已经激活,启动按键背光控制
-               mg_device_st_set( DS_ACTIVE );
-               btn_light_ctrl_start();
+               //已经激活
+               mg_device_st_set( &g_mg_dev, DS_ACTIVE );
        }else{
-               mg_device_st_set( DS_AUTH_WIFICONFIG_NO_ACTIVE );
+               //未激活
+               mg_device_st_set( &g_mg_dev, DS_AUTH_WIFICONFIG_NO_ACTIVE );
        }
        //连接涂鸦IOT平台
        MG_LOGD( "iot start..");
        iot_start();
 
-#if 1
+       //等待涂鸦连接
        while( !is_iot_connected() ){
                //wait iot connect ...
                MG_LOGD( "wait iot connect...");
@@ -202,27 +236,27 @@ int main( void )
        //未激活则连接tuya等待激活完成
        if( !is_device_active( &g_mg_dev ) ){
 
-               mg_device_st_set( DS_AUTH_WIFICONFIG_NO_ACTIVE );
+               mg_device_st_set( &g_mg_dev, DS_AUTH_WIFICONFIG_NO_ACTIVE );
 
-               //激活流程,堵塞,等待激活成功后返回
+               //启动激活流程,堵塞,等待激活成功后返回
                mg_device_active( &g_mg_dev );
 
-               //已经激活,启动按键背光控制
-               btn_light_ctrl_start();
        }
 
-       mg_device_st_set( DS_ACTIVE );
+       //激活,正常运行
+       mg_device_st_set( &g_mg_dev, DS_ACTIVE );
 
        //上报所有dp 信息。
        dp_report_all( &g_mg_dev );
-#endif
+
        //启动手势识别子流程
-       if( gesture_recognition_start() < 0 ){
+       if( gesture_recognition_start( &g_mg_dev ) < 0 ){
                MG_LOGE( "start gesture task fail!\n" );
        }
 
        ///rk_ble_wifi_init( NULL );
        while(1){
+               //TODO:
                sleep(5*1000);
        }
        return 0;
index a0ba535386978a673459ac035a525d59fd31e236..cba7a4231c9d25575cdd1664071f06c2fb116e03 100644 (file)
@@ -22,6 +22,7 @@
 
 #define UUID_MAX_LEN 36
 
+#if 0
 typedef enum {
        RK_BLE_WIFI_State_IDLE = 0,
        RK_BLE_WIFI_State_CONNECTTING,
@@ -289,3 +290,5 @@ void rk_ble_wifi_deinit(void *data)
        sleep(3);
        rk_bt_deinit();
 }
+#endif
+
index 2c2bea87dd1bf40928e7f6b8f74b184b14f264cd..f45a38e90bf6d61331c5f8decfe973ff33a9c2cd 100644 (file)
 #include <arpa/inet.h>
 #include <string.h>
 
+#define g_memdup  g_memdup2 //remove warning
+void execute(const char cmdline[],char recv_buff[]);
+
+
 #define GATT_MGR_IFACE                 "org.bluez.GattManager1"
 #define GATT_SERVICE_IFACE             "org.bluez.GattService1"
 #define GATT_CHR_IFACE                 "org.bluez.GattCharacteristic1"
@@ -84,7 +88,7 @@
 #define FWVERSION_UUID         "0x2A26"
 #define HWVERSION_UUID         "0x2A27"
 #define QRCODE_UUID            "fb3d0105-ea97-4769-9377-337c2f467236"
-#define PRODUCTLD_UUID         "fb3d0106-ea97-4769-9377-337c2f467236"
+#define PRODUCTID_UUID         "fb3d0106-ea97-4769-9377-337c2f467236"
 #define IP_UUID                "fb3d0107-ea97-4769-9377-337c2f467236"
 #define STATUSRESAULT_UUID             "fb3d0108-ea97-4769-9377-337c2f467236"
 
@@ -167,10 +171,10 @@ static const char *desc_props[] = { "read", "write", NULL };
 
 static void chr_write(struct characteristic *chr, const uint8_t *value, int len);
 static void chr_iface_destroy(gpointer user_data);
+#if 0
 /*hst################################################*/
 char  on_board_PID [30];
 #define PID_FILE_PATH  "/customer/tuya_key"
-#define ETH_NAME "wlan0"//使用的网卡名
 
 static void extract_product_id(const char* input_string)
 {
@@ -211,9 +215,11 @@ static int read_the_system_PID()
     return 1;
 }
 
-char* local_ip= NULL;
+#endif
+
+#define ETH_NAME "wlan0"//使用的网卡名
 
-char* get_local_ip()
+char* get_local_ip( char*ip, int32_t len )
 {
     int sock;
     struct sockaddr_in sin;
@@ -231,8 +237,10 @@ char* get_local_ip()
     }
 
     memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
-       close(sock);
-    return inet_ntoa(sin.sin_addr);
+    close(sock);
+
+    strncpy( ip, inet_ntoa(sin.sin_addr), len );
+    return ip;
 }
 
 
@@ -839,7 +847,8 @@ void execute(const char cmdline[],char recv_buff[]){
     FILE *stream = NULL;
     char buff[1024];
 
-    memset(recv_buff, 0, sizeof(recv_buff));
+    //memset(recv_buff, 0, sizeof(recv_buff));
+    recv_buff[0] = 0;
 
     if((stream = popen(cmdline,"r"))!=NULL){
         while(fgets(buff,1024,stream)){
@@ -1275,37 +1284,43 @@ static DBusMessage *chr_read_value(DBusConnection *conn, DBusMessage *msg,
         }
 
                if (!strcmp(FWVERSION_UUID, chr->uuid)){
-                char *fwversion = "fwversion";  
-                               chr_write(chr, fwversion, strlen(fwversion));
+                //char *fwversion = "fwversion";  
+                               str[0] = 0;     
+                               fw_version_read( str, sizeof(str) );
+                               chr_write(chr, str, strlen(str));
                 // printf("FWVERSION_UUID \n");                            
         }
 
                if (!strcmp(HWVERSION_UUID, chr->uuid)){
-                char *hwversion = "hwversion";  
-                               chr_write(chr, hwversion, strlen(hwversion));
+                //char *hwversion = "hwversion";  
+                               str[0] = 0;     
+                               hw_version_read( str, sizeof(str) );
+                               chr_write(chr, str, strlen(str));
                 // printf("HWVERSION_UUID \n");                            
         }
 
                if (!strcmp(QRCODE_UUID, chr->uuid)){
-                char *qrcode = "qrcode";  
-                               chr_write(chr, qrcode , strlen(qrcode));
+                //char *qrcode = "qrcode";  
+                               str[0] = 0;     
+                               tuya_bindcode_read( str, sizeof(str) );
+                               chr_write(chr, str, strlen(str));
                 // printf("QRCODE_UUID \n"); 
         }
 
-               if (!strcmp(PRODUCTLD_UUID, chr->uuid)){
-                char *productld = on_board_PID;
-                               chr_write(chr, productld, strlen(productld));
+               if (!strcmp(PRODUCTID_UUID, chr->uuid)){
+                //char *productld = on_board_PID;
+                               str[0] = 0;     
+                               tuya_productid_read( str, sizeof(str) );
+                               chr_write(chr, str, strlen(str));
                 // printf("PRODUCTLD_UUID \n"); 
         }
                
                if (!strcmp(IP_UUID, chr->uuid)){
-                       if (local_ip == NULL) {
-                               chr_write(chr, "IP IS NULL", strlen("IP IS NULL"));
-                // printf(" ip is null!\n");
-                       }
-                       else {
-                               chr_write(chr, local_ip, strlen(local_ip));
-                // printf("IP_UUID \n");
+                       char ip[50];
+                       if( get_local_ip( ip, sizeof(ip) )){
+                               chr_write(chr, ip, strlen( ip ) );
+                       }else{
+                               chr_write(chr, "0", 1 );
                        } 
         }
        chr_read(chr, &iter);
@@ -1336,6 +1351,10 @@ static pthread_t wifilist_tid= 0;
 
 static unsigned char rk_wifi_list_buf[20 * 1024];
 #if 1
+static int32_t wificonfig_wait_connect =0;
+extern void wifi_setup_success( char * ssid, char *password );
+extern void wifi_setup_fail( void );
+
 static struct characteristic *wifi_notify_chr = NULL;
 static int rk_blewifi_state_callback(RK_WIFI_RUNNING_State_e state, RK_WIFI_INFO_Connection_s *info)
 {
@@ -1345,17 +1364,22 @@ static int rk_blewifi_state_callback(RK_WIFI_RUNNING_State_e state, RK_WIFI_INFO
        switch(state) {
                case RK_WIFI_State_CONNECTED:
                        ret = 0x1;
-printf( "-----------------------------wificonfig  connect[%s,%s] success-------------\n", wifi_ssid, wifi_password );
-       wifi_config_success( wifi_ssid, wifi_password);
-
-       chr_write( wifi_notify_chr, &ret, 1 );
+                       if( wificonfig_wait_connect){
+                               wifi_setup_success( wifi_ssid, wifi_password);
+                               chr_write( wifi_notify_chr, &ret, 1 );
+                       }
+                       printf( "---------wificonfig  connect[%s,%s] success[%d]!--------\n", wifi_ssid, wifi_password, wificonfig_wait_connect );
+                       wificonfig_wait_connect = 0;
                        break;
                case RK_WIFI_State_CONNECTFAILED:
                case RK_WIFI_State_CONNECTFAILED_WRONG_KEY:
                        ret = 0x2;
-wifi_config_fail();
-printf( "-----------------------------wificonfig  connect[%s,%s] fail!-------------\n", wifi_ssid, wifi_password );
-       chr_write( wifi_notify_chr, &ret, 1 );
+                       if( wificonfig_wait_connect){
+                               wifi_setup_fail();
+                               chr_write( wifi_notify_chr, &ret, 1 );
+                       }
+                       printf( "---------wificonfig  connect[%s,%s] fail[%d]!!----------\n", wifi_ssid, wifi_password, wificonfig_wait_connect );
+                       wificonfig_wait_connect = 0;
                        break;
        }
 
@@ -1371,6 +1395,7 @@ static void* wifi_config_task( void * para)
 {
        uint8_t ret = 0;
        wifi_notify_chr =(struct characteristic *)para;
+       wificonfig_wait_connect =1;
 #if 0
        printf("[RK] save wifi ssid is %s\n", wifi_ssid);
        printf("[RK] save wifi psk is %s\n", wifi_password);
@@ -1380,18 +1405,20 @@ static void* wifi_config_task( void * para)
 #else
        printf("[RK] rk_config_wifi_thread\n");
 
-       prctl(PR_SET_NAME,"rk_config_wifi_thread");
+       //prctl(PR_SET_NAME,"rk_config_wifi_thread");
 
        RK_wifi_register_callback(rk_blewifi_state_callback);
        RK_wifi_enable(0);
        RK_wifi_enable(1);
+
+       wificonfig_wait_connect = 1;
        RK_wifi_connect(wifi_ssid, wifi_password );
 
 #endif
        return NULL;
 }
 
-static void wifi_scan_task( void * para)
+static void* wifi_scan_task( void * para)
 {
        struct characteristic *notify_chr =(struct characteristic *)para;
 
@@ -1404,7 +1431,7 @@ static void wifi_scan_task( void * para)
 
        if (data_len == 0) {
                printf("[RK] NO WIFI SCAN_R OR READ END!!!\n");
-               return;
+               return NULL;
        }
 
        //prctl(PR_SET_NAME,"rk_ble_send_data");
@@ -1423,6 +1450,7 @@ static void wifi_scan_task( void * para)
                printf("[RK] : notify:end\n" );
                sent += to_send;
        }
+       return NULL;
 }
 
 static void _recv_data_cb(struct characteristic *chr,  char *data, int len)
@@ -1457,19 +1485,23 @@ static void _recv_data_cb(struct characteristic *chr,  char *data, int len)
                }
 
                if (strncmp(ble_data->cmd, "wifilists", 9) == 0) {
+                       int32_t scan_count = 0;
 scan_retry:
+                       if( scan_count > 10 ){
+                               return;
+                       }
                        printf("[RK] RK_wifi_scan ...\n");
                        RK_wifi_scan();
                        usleep(800000);
+                       scan_count++;
 
-                       printf("[RK] RK_wifi_scan_r_sec ...\n");
-                       //wifilist = strdup("\"yino310\"");//RK_wifi_scan_r_sec(0x14);
                        wifilist = RK_wifi_scan_r_sec(0x14);
-                       printf("[RK] RK_wifi_scan_r_sec end wifilist: %s\n", wifilist?:"null");
-
-                       if (wifilist == NULL)
+                       printf("[RK] RK_wifi_scan_r_sec end wifilist: %s\n", wifilist?wifilist:"null");
+                               
+                       if (wifilist == NULL ){
                                goto scan_retry;
-                       if (wifilist && (strlen(wifilist) < 3)) {
+                       }
+                       if (wifilist && (strlen(wifilist) < 3) ){
                                free(wifilist);
                                goto scan_retry;
                        }
@@ -1479,6 +1511,7 @@ scan_retry:
                        //printf("[RK] wifi scan_r: %s, len: %d\n", rk_wifi_list_buf, scanr_len);
                        free(wifilist);
                        pthread_create(&wifiscan_tid, NULL, wifi_scan_task, chr);
+                       pthread_detach( wifiscan_tid );
                } else if (strncmp(ble_data->cmd, "wifisetup", 9) == 0) {
                        strcpy(wifi_ssid, ble_data->ssid); // str + 20);
                        strcpy(wifi_password, ble_data->psk); // str + 52);
@@ -1490,6 +1523,7 @@ scan_retry:
                        //strcpy(wifi_cfg.psk, wifi_password);
                        //wifi_cfg.wifi_status_callback = wifi_status_callback;
                        pthread_create(&wificonfig_tid, NULL, wifi_config_task, chr);
+                       pthread_detach( wificonfig_tid );
                }
 }
 
@@ -1579,8 +1613,13 @@ static DBusMessage *chr_write_value(DBusConnection *conn, DBusMessage *msg,
                #endif
        }
        if (!strcmp(STATUSRESAULT_UUID, chr->uuid)){
-                strcpy(wifi_security, str);
-                printf("STATUSRESAULT_UUID  %s\n", wifi_security);                            
+               
+               if( len == 1 && chr->value[0] == 1 ){
+                       printf( "ble wificonfig finish!\n" );   
+                       netconfig_success_notify_cb();
+               }else{
+                       printf( "ble wificonfig ...%d, data:%02x\n", len, len>0?chr->value[0]:0 );
+               }
         }
 #endif
 
@@ -1740,12 +1779,14 @@ static gboolean register_characteristic(const char *chr_uuid,
        return TRUE;
 }
 
+static int register_service_id = 1;
 static char *register_service(const char *uuid)
 {
        static int id = 1;
        char *path;
 
-       path = g_strdup_printf("/service%d", id++);
+       //path = g_strdup_printf("/service%d", id++);
+       path = g_strdup_printf("/service%d", register_service_id++);
        if (!g_dbus_register_interface(connection, path, GATT_SERVICE_IFACE,
                                NULL, NULL, service_properties,
                                g_strdup(uuid), g_free)) {
@@ -1816,14 +1857,13 @@ printf( "----------create wifi service -----------------\n" );
                                                NULL,
                                                desc_props,
                                                service_path);
-       read_the_system_PID();                                  
-       gboolean productld = register_characteristic(PRODUCTLD_UUID, //Read
+       //read_the_system_PID();                                        
+       gboolean productld = register_characteristic(PRODUCTID_UUID, //Read
                                                &level, sizeof(level),
                                                alert_level_props,
                                                NULL,
                                                desc_props,
                                                service_path);
-       local_ip = get_local_ip();                                      
        gboolean ip = register_characteristic(IP_UUID,  //Read, Notify
                                                &level, sizeof(level),
                                                level_props,
@@ -2047,7 +2087,6 @@ static struct adapter *find_ctrl(GList *source, const char *path)
 
         for (list = g_list_first(source); list; list = g_list_next(list)) {
                 struct adapter *adapter = list->data;
-
                 if (!strcasecmp(g_dbus_proxy_get_path(adapter->proxy), path))
                         return adapter;
         }
@@ -2369,123 +2408,81 @@ static void property_changed(GDBusProxy *proxy, const char *name,
        }
 }
 
-static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
-                                                       gpointer user_data)
-{
-       static bool __terminated = false;
-       struct signalfd_siginfo si;
-       ssize_t result;
-       int fd;
-
-       if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
-               return FALSE;
-
-       fd = g_io_channel_unix_get_fd(channel);
-
-       result = read(fd, &si, sizeof(si));
-       if (result != sizeof(si))
-               return FALSE;
-
-       switch (si.ssi_signo) {
-       case SIGINT:
-       case SIGTERM:
-               if (!__terminated) {
-                       printf("Terminating\n");
-                       g_main_loop_quit(main_loop);
-               }
-
-               __terminated = true;
-               break;
-       }
-
-       return TRUE;
-}
-
-static guint setup_signalfd(void)
-{
-       GIOChannel *channel;
-       guint source;
-       sigset_t mask;
-       int fd;
-
-       sigemptyset(&mask);
-       sigaddset(&mask, SIGINT);
-       sigaddset(&mask, SIGTERM);
-
-       if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
-               perror("Failed to set signal mask");
-               return 0;
-       }
-
-       fd = signalfd(-1, &mask, 0);
-       if (fd < 0) {
-               perror("Failed to create signal descriptor");
-               return 0;
-       }
-
-       channel = g_io_channel_unix_new(fd);
-
-       g_io_channel_set_close_on_unref(channel, TRUE);
-       g_io_channel_set_encoding(channel, NULL, NULL);
-       g_io_channel_set_buffered(channel, FALSE);
-
-       source = g_io_add_watch(channel,
-                               G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
-                               signal_handler, NULL);
-
-       g_io_channel_unref(channel);
-
-       return source;
-}
-
-int ble_test_main( void )
+void * ble_wificonfig( void* p )
 {
        GDBusClient *client;
-       guint signal;
 
-       signal = setup_signalfd();
-       if (signal == 0)
-               return -errno;
-
-
-system( "insmod /system/lib/modules/rtk_btusb.ko" );
+       if( system( "insmod /system/lib/modules/rtk_btusb.ko" ) ){
+       }
        sleep( 1 );
-system( "/usr/libexec/bluetooth/bluetoothd -C -E -d -n & " );
+       if( system( "/usr/libexec/bluetooth/bluetoothd -C -E -d -n & " ) ){
+       }
        sleep( 1 );
 
-
        connection = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL);
 
        main_loop = g_main_loop_new(NULL, FALSE);
 
        g_dbus_attach_object_manager(connection);
 
-       printf("gatt-service unique name: %s\n",
-                               dbus_bus_get_unique_name(connection));
+       printf("gatt-service unique name: %s\n", dbus_bus_get_unique_name(connection));
 
-#ifdef DUEROS
-    dueros_socket_thread_create();
-#endif
 
        create_wifi_services();
 
        client = g_dbus_client_new(connection, "org.bluez", "/");
-printf( "---g_dbus_client_set_proxy_handlers----\n" );
        g_dbus_client_set_proxy_handlers(client, proxy_added_cb, NULL, property_changed, NULL);
-printf( "+++g_dbus_client_set_proxy_handlers----\n" );
 
        g_main_loop_run(main_loop);
-
-#ifdef DUEROS
-    dueros_socket_thread_delete();
-#endif
-
        g_dbus_client_unref(client);
 
-       g_source_remove(signal);
-
        g_slist_free_full(services, g_free);
        dbus_connection_unref(connection);
 
+       main_loop = NULL;
+       services = NULL;
+       connection = NULL;
+       temp_chr = NULL;
+
+       default_ctrl = NULL;
+       ctrl_list = NULL;
+       default_dev = NULL;
+       default_attr = NULL;
+       register_service_id = 1;
+
+       printf( "------ble_wificonfig to exit ...\n " );
        return 0;
 }
+
+//#include "device.h"
+static pthread_t ble_tid;
+
+void ble_wificonfig_start( void )
+{
+       //MG_LOGE( "ble_start" );
+       printf( "ble_start\n" );
+       if( pthread_create( &ble_tid, NULL, ble_wificonfig, NULL ) ){
+               //MG_LOGE( "create ble thread fail!" );
+               printf( "create ble thread fail!\n" );
+               return;
+       }
+}
+
+void ble_wificonfig_stop( void )
+{
+       //MG_LOGE( "ble_stop" );
+       printf( "ble_stop\n" );
+       if( main_loop ){
+               printf( " main loop exit...\n" );
+               g_main_loop_quit(main_loop); 
+               sleep(1);
+               printf( " wait ble thread [%d] exit...\n", ble_tid );
+               pthread_join( ble_tid, NULL );
+               printf( "ble stopped!\n" );
+               if( system( "killall bluetoothd" ) ){
+               }
+       }else{
+               //MG_LOGE( "ble_stop error, main_loop is null" );
+               printf( "ble_stop error, main_loop is null\n" );
+       }
+}