增加授权和激活的流程
authorkyz <kongyuzhong@gmail.com>
Thu, 25 May 2023 15:35:05 +0000 (23:35 +0800)
committerkyz <kongyuzhong@gmail.com>
Fri, 26 May 2023 07:33:20 +0000 (15:33 +0800)
14 files changed:
CMakeLists.txt
app/CMakeLists.txt
app/inc/device.h
app/src/device_config.c [new file with mode: 0644]
app/src/device_ctrl.c
app/src/device_wifi.c [new file with mode: 0644]
app/src/iot.c
app/src/iot_dp.c
app/src/iot_key.c
app/src/main.c
button/src/button_scan.c
led/CMakeLists.txt
led/src/led.c
led/test/led_test.c [new file with mode: 0644]

index 84bc2125c35ec995824fae6edd5a6ee25987ace0..ec9c922d7da69cc4ed2743f905b5873d9f80380d 100644 (file)
@@ -1,3 +1,5 @@
+cmake_minimum_required(VERSION 3.8)
+
 # Add the posix targets
 add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/app )
 add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/button )
index a3ae76da307e1b8b60a05e03a7a44d3494da2e4e..7f18519fc17405cdaa34c2193ae466c105c83b50 100644 (file)
@@ -2,6 +2,10 @@ cmake_minimum_required(VERSION 3.8)
 
 project(mg_app)
 
+SET(CMAKE_BUILD_TYPE "Debug")
+SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
+SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
+
 include_directories(
        ${PROJECT_SOURCE_DIR}/./
        ${PROJECT_SOURCE_DIR}/src/
@@ -38,6 +42,7 @@ target_link_libraries(
        mg_log
        log4c
        rt
+       z
        pthread )
 
 install( TARGETS ${PROJECT_NAME} RUNTIME DESTINATION /app )
index 4e111c312800d158d2762ea28480fc6c9eed57f2..8ce35d8583a94256f6449ff861b70d90c956574a 100644 (file)
@@ -2,6 +2,99 @@
 #define __MG_DEVICE_H__
 
 #include <stdint.h>
+#include <time.h>
+#include "led.h"
+#include "button.h"
+
+#ifdef __x86_64__
+#warning  "PC Version Debug"
+#define DEV_CFG_PATH       "dev.cfg"
+#define TUYA_AUTH_PATH     "tuya_auth"
+#define MG_ACTIVE_PATH     "mg.active"
+#define READ_UUID_CMD      "cat cpuinfo | grep Serial"
+
+#else
+#define DEV_CFG_PATH       "/appconfigs/dev.cfg"
+#define TUYA_AUTH_PATH     "/customer/tuya_auth"
+#define MG_ACTIVE_PATH     "/customer/mg.active"
+#define READ_UUID_CMD      "cat /proc/cpuinfo | grep Serial"
+
+#endif
+
+#define KEY_LED_MAX_DEF            100 //(0-100%)
+#define ATMOS_LED_MAX_DEF          100 //(0-100%)
+//#define RECOGNITION_MODE_DEF       0   //enum 0:"light_mode", 1:"dark_mode"
+#define RECOGNITION_LEVEL_DEF      6   //1--10
+#define RECOGNITION_DURATION_DEF   300 //5--1000 seconds 
+#define LED_ATMOS_ON_DURATION_DEF  5   //1--20 seconds
+#define LED_TOF_THRESHOLD_DEF      150 //10--150 cm
+#define MG_TOF_INTERVAL_DEF        10  //10-20
+#define MG_DISALBE_ROW_DEF         0   //bool 
+#define MG_KEY_ENABLE_DEF          0xff  //bit0-7 -> btn1 - 8 
+#define BL_MODE_DEF                0   //enum 0:"auto", 1"manual"
+//#define MG_TOF_STATUS_DEF          0   //bool 
+//#define MG_TOF_SYNC_DEF            0   //bool
+#define LED_ATMOS_OFF_DURATION_DEF 5   //1-20
+
+
+typedef enum{
+       DS_INIT = 0,
+       DS_NO_AUTH, //未授权:控制所有按键背光连续慢闪,装饰灯带常灭;不加载涂鸦 SDK应用程序轮询授权是否已写入,并校验写入长度符合要求;
+       DS_AUTH_NO_WIFICONFIG, //已授权未配网:所有灯光常灭;
+       DS_AUTH_WIFICONFIGING, //配网过程中 LED7 & LED8 连续慢闪, 2分钟超时后退出
+       DS_AUTH_WIFICONFIG_NO_ACTIVE, //已授权已配网未激活:所有灯光常灭;
+       DS_ACTIVE, //已授权已配网已激活:进入主流程,灯光根据 DP 设置处理。
+}dev_st_t;
+
+typedef struct{
+//dp info
+       //key1 -- key8  //event
+       uint32_t key_led_max;
+       uint32_t atmos_led_max;
+       //recognition_running  bool ro
+       //gesture_event        event  ro
+       //uint32_t recognition_mode;  //手势识别补光 
+       uint32_t recognition_level; //手势识别灵敏度
+       uint32_t recognition_duration; //手势识别关闭延时
+       //privacy_mode bool ro
+       //485_cmd; //action
+       //active ro;
+       //uint32_t key;
+       uint32_t led_atmos_on_duration;//氛围灯打开时间
+       uint32_t mg_tof_threshold; //人感检测距离
+       uint32_t mg_tof_interval;  //人感检测周期
+       uint32_t mg_disalbe_row;   //关闭联排功能
+       uint32_t mg_key_enable;    //按键禁使能, 默认FF, 一个字节, bit0 是第一个按键, bit7是第八个按键
+       uint32_t bl_mode;          //背光模式 默认"自动",  自动模式: 氛围灯亮, 背光灭, 有人后反转.   手动模式: 氛围灯可调, 背光亮度可调, 两者无关联
+       //uint32_t mg_tof_status;    //人感状态
+       //uint32_t mg_tof_sync;      //人感状态同步
+       uint32_t led_atmos_off_duration;//氛围灯关闭时间, 数值范围: 1-20, 间距: 1, 倍数: 0, 单位: 秒  默认5秒
+//
+}dev_cfg_t;
+
+typedef struct{
+       dev_st_t st;
+       dev_cfg_t cfg;
+
+       uint32_t iot_connect;
+       uint32_t tof_st;   //人感状态
+       uint32_t tof_sync; //人感状态同步
+
+       uint32_t mg_active;
+
+       uint32_t auth; //auth
+       char *auth_key_id;
+       char *auth_key_value;
+       char *uuid;
+       //pthread_mutex_t lock;
+
+       uint32_t clean_device;
+       uint32_t clean_timeout;
+
+       uint32_t wifi_config;
+       uint32_t wifi_config_timeout;
+       uint32_t bl_manual;
+}mg_dev_t;
 
 
 typedef enum{
@@ -47,17 +140,21 @@ typedef enum{
 }dev_mode_t;
        
 
-typedef struct{
-       dev_mode_t mode;
-       uint32_t  clean_device;
-       time_t clean_timeout;
-       uint32_t  wifi_config;
-       time_t wifi_config_timeout;
-       uint32_t bl_manual; //1:manual 0:auto
+void light_mode_set( light_mode_t mode );
+void light_brightness_set( led_id_t id, uint32_t br );
 
-}mg_dev_t;
+void device_wifi_init( void );
+void device_wiwi_start( void );
 
 
-void light_mode_set( light_mode_t mode );
+int32_t load_device_cfg( dev_cfg_t *cfg );
+int32_t store_device_cfg( dev_cfg_t *cfg );
+int32_t load_device_cfg( dev_cfg_t *cfg );
+
+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 );
+
 #endif //__MG_DEVICE_H__
 
diff --git a/app/src/device_config.c b/app/src/device_config.c
new file mode 100644 (file)
index 0000000..35151d4
--- /dev/null
@@ -0,0 +1,235 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "mg_log.h"
+#include "device.h"
+
+#include "zlib.h"
+
+
+static const dev_cfg_t dev_cfg_default={
+       //dp info
+       .key_led_max            = KEY_LED_MAX_DEF,
+       .atmos_led_max          = ATMOS_LED_MAX_DEF,
+       //.recognition_mode       = RECOGNITION_MODE_DEF,  //手势识别补光
+       .recognition_level      = RECOGNITION_LEVEL_DEF, //手势识别灵敏度
+       .recognition_duration   = RECOGNITION_DURATION_DEF, //手势识别关闭延时
+       .led_atmos_on_duration  = LED_ATMOS_ON_DURATION_DEF, //氛围灯打开时间
+       .mg_tof_threshold       = LED_TOF_THRESHOLD_DEF, //人感检测距离
+       .mg_tof_interval        = MG_TOF_INTERVAL_DEF,  //人感检测周期
+       .mg_disalbe_row         = MG_DISALBE_ROW_DEF,   //关闭联排功能
+       .mg_key_enable          = MG_KEY_ENABLE_DEF,    //按键禁使能, 默认FF, 一个字节, bit0 是第一个按键, bit7是第八个按键
+       .bl_mode                = BL_MODE_DEF,          //背光模式 默认"自动",  自动模式: 氛围灯亮, 背光灭, 有人后反转.   手动模式: 氛围灯可调, 背光亮度可调, 两者无关联
+       //.mg_tof_status;    //人感状态
+       //.mg_tof_sync            = MG_TOF_SYNC_DEF,      //人感状态同步
+       .led_atmos_off_duration = LED_ATMOS_OFF_DURATION_DEF, //氛围灯关闭时间, 数值范围: 1-20, 间距: 1, 倍数: 0, 单位: 秒    默认5秒
+};
+
+int32_t nv_read( char *path, uint8_t *read_data, uint32_t read_sz )
+{
+       int32_t retval = -1;
+       int32_t read_crc;
+       FILE *fp;
+
+       fp = fopen( path, "r" );
+       if( fp ){
+               if( fread( read_data, read_sz, 1, fp )==1 && fread( &read_crc, 4, 1, fp )==1){
+                               if( read_crc == crc32( 0, read_data, read_sz ) ){
+                                       retval = 0;
+                               }else{
+                                       MG_LOGI( "cfg file[%s] verify fail!", path );
+                               }
+               }else{
+                       MG_LOGI( "read cfg file[%s] fail!", path );
+               }
+               fclose(fp );
+       }else{
+               MG_LOGI( "open cfg file[%s] fail!", path );
+       }
+
+
+       return retval;
+}
+
+int32_t nv_write( char *path, uint8_t *write_data, uint32_t write_sz )
+{
+       int32_t retval = -1;
+       int32_t wr_crc;
+       FILE *fp;
+       uint8_t * wr_buf;
+
+       
+       fp = fopen( path, "w" );
+       if( fp ){
+               wr_crc = crc32( 0, (char *)write_data, write_sz );
+               fwrite( write_data, write_sz, 1, fp );
+               fwrite( &wr_crc, sizeof( wr_crc ), 1, fp );
+               fclose( fp );
+               retval = 0;
+       }else{
+               MG_LOGE( "create cfg file[%s] fail!", path );
+       }
+
+       // write cfg 
+       // write crc(cfg)
+       return retval;
+}
+
+int32_t load_device_cfg( dev_cfg_t *cfg )
+{
+       //read file
+       if( nv_read( DEV_CFG_PATH, (uint8_t*)cfg, sizeof( cfg ) ) != 0 ){
+               MG_LOGI( "load cfg file fail, use default cfg!" );
+               memcpy( cfg, &dev_cfg_default, sizeof( *cfg ) );
+       }
+       return 0;
+}
+
+int32_t store_device_cfg( dev_cfg_t *cfg )
+{
+       //write file
+       return nv_write( DEV_CFG_PATH, (uint8_t*)cfg, sizeof(cfg) );
+}
+
+
+int32_t load_auth_key( mg_dev_t *dev )
+{
+       FILE * fp;
+       char key[50];
+       char value[50];
+       int32_t auth = 0;
+       int32_t retval = -1;
+
+       fp = fopen( TUYA_AUTH_PATH, "r" );
+       if( fp ){
+               if( fgets( key, 50-1, fp ) && fgets( value, 50-1, fp ) ){
+                       strtok( key, "\r\n");
+                       strtok( value, "\n\r");
+                       char *s = strchr( key, '\r' );
+                       dev->auth_key_id = strdup( key );
+                       dev->auth_key_value  = strdup( value );
+                       dev->auth = 1;
+                       MG_LOGI( "get auth key[ %s:%s ]", dev->auth_key_id, dev->auth_key_value );
+                       retval = 0;
+               }
+               fclose(fp);
+       }else{
+               MG_LOGE( "open auth file[%s] fail!", TUYA_AUTH_PATH );
+               retval = -1;
+       }
+       if( retval != 0 ){
+               if( dev->auth_key_id )
+                       free( dev->auth_key_id );
+               if( dev->auth_key_value )
+                       free( dev->auth_key_value );
+               dev->auth_key_id = NULL;
+               dev->auth_key_value = NULL;
+               dev->auth = 0;
+       }
+       return retval;
+}
+
+int32_t is_dev_auth( mg_dev_t *dev )
+{
+       return dev->auth?1:0;
+}
+
+static int  rx_key = 1;
+static char received_key_value[50];
+
+int32_t recv_key_value_cb( char * value )
+{
+       if( value ){
+               strncpy( received_key_value, value, sizeof(received_key_value)-1 );
+               rx_key= 1;
+       }
+}
+
+int32_t is_device_active( mg_dev_t *dev )
+{
+
+       int fd;
+       if( dev->mg_active )
+               return 1;
+
+       fd = open( MG_ACTIVE_PATH, O_RDONLY );
+       if( fd >= 0  ){
+               dev->mg_active = 1;
+               close(fd);
+       }else{
+               MG_LOGD( "open [%s] fail: %d", MG_ACTIVE_PATH, fd );
+       }
+
+       return dev->mg_active;
+}
+
+int32_t read_device_uuid( mg_dev_t *dev )
+{
+       FILE *fp;
+       char buf[200];
+       char uuid[100];
+       char *s;
+
+       int32_t retval = -1;
+
+       if( dev->uuid )
+               free( dev->uuid );
+
+       dev->uuid = NULL;
+
+       fp = popen( READ_UUID_CMD, "r" );
+       if( fp ){
+               if( fgets( buf, sizeof( buf )-1, fp ) ){
+                       printf( "get cpu serial :[%s]\n", buf );        
+                       s = strstr( buf, "Serial" );
+                       if( s && sscanf( s, "%*[^:]:%*[ ]%s", uuid ) == 1 ){
+                               dev->uuid = strdup( uuid );
+                               printf( "get uuid [%s] from [%s]\n", uuid, buf );
+                               retval = 0;
+                       }
+               }
+               fclose( fp );
+       }
+       return retval;
+}
+
+extern int32_t dp_property_report_key( char *dp_name, char *dp_value );
+int32_t        mg_device_active( mg_dev_t *dev )
+{
+       FILE *fp;
+       uint32_t now = time(NULL);
+       char key_id[200];
+       char uuid[100];
+       char *s;
+
+       //read uuid;
+       if( !dev->uuid ){
+               read_device_uuid( dev );
+       }
+
+       sprintf( key_id, "%s%s", dev->auth_key_id, dev->uuid );
+
+       while( !dev->mg_active ){
+               rx_key = 0;
+               dp_property_report_key( "key", key_id );
+               //wait recv value
+               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!" );
+                               }
+                               break;
+                       }
+                       sleep(1);
+               }
+               usleep(10);
+       }
+
+       return 0;
+}
index 67962770e172843bf4c323a3cea531afba58dd8e..7e90aae4b6c88ce9ea9973b7ce6543b74a335a2e 100644 (file)
@@ -14,7 +14,7 @@
 #include "device.h"
 #include "tof.h"
 
-static mg_dev_t g_mg_dev;
+extern mg_dev_t g_mg_dev;
 mg_tof_t *g_tof;
 
 #if 0
@@ -161,22 +161,22 @@ void * btn_light_ctrl_task( void* unused )
                        //自动模式
                }
 
-
+               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 );
-               }else if( g_mg_dev.mode == DEV_MODE_INIT ){
+               }else if( st == DS_INIT ){
                        light_mode_set( LIGHT_MODE_INIT );
-               }else if( g_mg_dev.mode == DEV_MODE_NO_AUTH ){
+               }else if( st == DS_NO_AUTH ){
                        light_mode_set( LIGHT_MODE_NO_AUTH );
-               }else if( g_mg_dev.mode == DEV_MODE_AUTH_NO_WIFICONFIG ){
+               }else if( st == DS_AUTH_NO_WIFICONFIG ){
                        light_mode_set( LIGHT_MODE_AUTH_NO_WIFICONFIG  );
-               }else if( g_mg_dev.mode == DEV_MODE_AUTH_WIFICONFIGING ){
+               }else if( st == DS_AUTH_WIFICONFIGING ){
                        light_mode_set( LIGHT_MODE_AUTH_WIFICONFIGING );
-               }else if( g_mg_dev.mode == DEV_MODE_AUTH_WIFICONFIG_NO_ACTIVE ){
+               }else if( st == DS_AUTH_WIFICONFIG_NO_ACTIVE ){
                        light_mode_set( LIGHT_MODE_AUTH_WIFICONFIG_NO_ACTIVE );
-               }else if( g_mg_dev.mode == DEV_MODE_ACTIVE ){
+               }else if( st == DS_ACTIVE ){
 
                        if( g_mg_dev.bl_manual ){
                                uint32_t key_led_br;
@@ -234,18 +234,20 @@ static void tof_cb( tof_info_t *info)
 
 void btn_light_ctrl_init( void )
 {
-       g_mg_dev.mode = DEV_MODE_ACTIVE;
+       g_mg_dev.st = DEV_MODE_ACTIVE;
        g_mg_dev.clean_device  = 0;
        g_mg_dev.clean_timeout = 0;
 
        device_button_init();
        device_light_init();
+
        g_tof = mg_tof_open( 1000 ); //1000mm
        if( g_tof ){
                mg_tof_event_register( g_tof, tof_cb );
        }else{
                MG_LOGE( "open tof fail!" );
        }
+
 }
 
 void btn_light_ctrl_start( void )
diff --git a/app/src/device_wifi.c b/app/src/device_wifi.c
new file mode 100644 (file)
index 0000000..0cb7e8e
--- /dev/null
@@ -0,0 +1,35 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <pthread.h>
+#include "button.h"
+#include "led.h"
+#include "iot.h"
+#include "mg_log.h"
+#include "cQueue.h" 
+#include "device.h"
+
+static pthread_t th;
+
+void device_wifi_init( void )
+{
+       MG_LOGI( "wifi init" );
+}
+
+void * device_wifi_task( void* unused )
+{
+       while(1){
+               MG_LOGD( "wifi task ..." );
+               sleep(10);
+       }
+}
+
+void device_wifi_start( void )
+{
+       if( pthread_create( &th, NULL, device_wifi_task, (void *)NULL ) != 0 ){
+               MG_LOGE( "create button and light ctrl task fail!\n" );
+       }
+}
+
index 8ff9c47c35302b33c6d0f0c56eedca096a44e597..a88be7896e7ffd01af8ff27f65934a4d3130d44c 100644 (file)
@@ -13,6 +13,7 @@
 
 extern int32_t iot_key_load( char ** out_productID, char ** out_deviceID, char ** out_deviceSecret );
 
+static uint32_t s_iot_connected = 0;
 //const char productId[] = "3xeyrwyvbuyajz4a";
 //const char deviceId[] = "26dfc49b45e25a93dcwgzg";
 //const char deviceSecret[] = "IAS0hHa3mmgrKn9L";
@@ -29,6 +30,7 @@ void on_connected(tuya_mqtt_context_t* context, void* user_data)
     TY_LOGI("on connected");
 
     /* data model test code */
+#if 0
     tuyalink_thing_data_model_get(context, NULL);
     tuyalink_thing_desired_get(context, NULL, "[\"power\"]");
     tuyalink_thing_property_report(context, NULL, "{\"power\":{\"value\":1234,\"time\":1631708204231}}");
@@ -37,13 +39,18 @@ void on_connected(tuya_mqtt_context_t* context, void* user_data)
     tuyalink_thing_batch_report(context, "{\"msgId\":\"45lkj3551234001\",\"time\":1626197189638,\"sys\":{\"ack\":1},\"data\":{\"properties\":{\"power\":{\"value\":11,\"time\":1626197189638}},\"events\":{\"boom\":{\"outputParams\":{\"param1\":\"10\"},\"eventTime\":1626197189001}}}}");
 
 
-    tuyalink_thing_property_report(context, NULL, "{\"key_led_max\":{\"value\":88}}");
-    tuyalink_thing_property_report(context, NULL, "{\"atmos_led_max\":{\"value\":73}}");
+//tuyalink_thing_property_report(context, NULL, "{\"key_led_max\":{\"value\":88}}");
+//tuyalink_thing_property_report(context, NULL, "{\"atmos_led_max\":{\"value\":73}}");
+#endif
+    s_iot_connected = 1;
+    MG_LOGI( "IOT  Connected...\n" );
 }
 
 void on_disconnect(tuya_mqtt_context_t* context, void* user_data)
 {
     TY_LOGI("on disconnect");
+    s_iot_connected = 0;
+    MG_LOGI( "IOT  Disconnect...\n" );
 }
 
 void on_messages(tuya_mqtt_context_t* context, void* user_data, const tuyalink_message_t* msg)
@@ -120,8 +127,13 @@ void* iot_task( void * unused )
        for (;;) {
                msg = dp_event_pop();
                if( msg ){
-
-                       tuyalink_thing_event_trigger(client, NULL, msg );               
+                       if( msg[0] == 'E' ){ //Event
+printf( "report event[%s]\n", &msg[1] );
+                               tuyalink_thing_event_trigger(client, NULL, &msg[1] );           
+                       }else if( msg[0] == 'P' ){ //Property
+printf( "report property[%s]\n", &msg[1] );
+                               tuyalink_thing_property_report( client, NULL, &msg[1] );
+                       }
                        free( msg );
                }
                /* Loop to receive packets, and handles client keepalive */
@@ -134,6 +146,11 @@ void* iot_task( void * unused )
 static pthread_t iot_thread;
 static pthread_attr_t  attr;
 
+int32_t is_iot_connected( void )
+{
+       return s_iot_connected;
+}
+
 void iot_start( void )
 {
        if( pthread_create( &iot_thread, NULL, iot_task, NULL ) != 0 ){
index 1b158d6d4dcb8cbd20a7cd693c1718a928cf7eb4..d8301b2bebf80e5f7ece98a4db50a37434969e58 100644 (file)
@@ -2,11 +2,13 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <pthread.h>
+#include <unistd.h>
 
 #include "cQueue.h" 
 #include "button.h" 
 
 #include "iot.h"
+#include "device.h"
 
 typedef enum{
        DP_SINGLE_CLICK   = 0,
@@ -152,7 +154,7 @@ char * button_event_name[]={
 
 //code : 1 -- 8  ->button_1 .. button8
 //event: 1 -- single_click  2--double_click  3--long_press 4--long_press_up
-#define btn_event_fmt "{\"eventCode\":\"%s\",\"outputParams\":{\"%s\":\"%s\" } }" 
+#define btn_event_fmt "E{\"eventCode\":\"%s\",\"outputParams\":{\"%s\":\"%s\" } }" 
 int32_t dp_button_event_push( dp_button_code_t btn_code, dp_button_event_t btn_event  )
 {
        char msg_buf[100];
@@ -170,6 +172,96 @@ int32_t dp_button_event_push( dp_button_code_t btn_code, dp_button_event_t btn_e
        return 0;
 }
 
+//激活流程,通过dp_key上报KEY ID, 也通过dp_key接收 KEY Value
+#if 1
+#define property_fmt_str   "P{\"%s\":\"%s\"}" 
+#define property_fmt_int   "P{\"%s\":%d}" 
+#define property_fmt_bool  "P{\"%s\":%s}" 
+#define property_fmt_key   "P{\"%s\":\"%s\"}" 
+#else
+#define property_fmt_str   "P{\"%s\":{\"value\":\"%s\"}}" 
+#define property_fmt_int   "P{\"%s\":{\"value\":%d}}" 
+#define property_fmt_bool  "P{\"%s\":{\"value\":%s}}" 
+#define property_fmt_key   "P{\"%s\":\"%s\"}" 
+
+#endif
+int32_t dp_property_report( char *payload )
+{
+       void *msg_str = strdup( payload );
+       if( g_dp_event_q ){
+               cQPost( g_dp_event_q, ( void *)msg_str );
+       }
+       return 0;
+
+}
+
+int32_t dp_property_report_str( char *dp_name, char *dp_value )
+{
+       char msg_buf[100];
+
+       sprintf( msg_buf, property_fmt_str, dp_name, dp_value );
+
+       return dp_property_report( msg_buf );
+}
+
+int32_t dp_property_report_int( char *dp_name, int32_t dp_value )
+{
+       char msg_buf[100];
+
+       sprintf( msg_buf, property_fmt_int, dp_name, dp_value );
+
+       return dp_property_report( msg_buf );
+}
+
+int32_t dp_property_report_bool( char *dp_name, int32_t dp_value )
+{
+       char msg_buf[100];
+
+       sprintf( msg_buf, property_fmt_bool, dp_name, dp_value?"true":"false" );
+
+       return dp_property_report( msg_buf );
+}
+
+int32_t dp_property_report_key( char *dp_name, char *dp_value )
+{
+       char msg_buf[100];
+
+       sprintf( msg_buf, property_fmt_key, dp_name, dp_value );
+
+       return dp_property_report( msg_buf );
+}
+
+int32_t        dp_report_all( mg_dev_t *dev )
+{
+       dev_cfg_t  *cfg = &dev->cfg;
+
+       //dp_property_report_bool( "active", dev->mg_active );
+
+       dp_property_report_int( "key_led_max", cfg->key_led_max );
+       usleep(50*1000);
+       dp_property_report_int( "atmos_led_max", cfg->atmos_led_max );
+       usleep(50*1000);
+       dp_property_report_int( "recognition_level", cfg->recognition_level);
+       usleep(50*1000);
+       dp_property_report_int( "recognition_duration", cfg->recognition_duration);
+       usleep(50*1000);
+       dp_property_report_int( "led_atmos_on_duration", cfg->led_atmos_on_duration);
+       usleep(50*1000);
+       dp_property_report_int( "mg_tof_threshold", cfg->mg_tof_threshold);
+       usleep(50*1000);
+       dp_property_report_int( "mg_tof_interval", cfg->mg_tof_interval);
+       usleep(50*1000);
+       dp_property_report_int( "mg_disalbe_row", cfg->mg_disalbe_row);
+       usleep(50*1000);
+       dp_property_report_bool( "mg_key_enable", cfg->mg_key_enable );
+       usleep(50*1000);
+       dp_property_report_str( "bl_mode", cfg->bl_mode?"auto":"manual");
+       usleep(50*1000);
+       dp_property_report_int( "led_atmos_off_duration", cfg->led_atmos_off_duration);
+
+       return 0;
+}
+
 char* dp_event_pop( void )
 {
        char *msg;
@@ -229,7 +321,10 @@ void handle_iot_msg(tuya_mqtt_context_t* context, char *data_string )
                //"active"    ro
                if(( item =cJSON_GetObjectItem(root, "key"))){
                        //
+                       extern int32_t recv_key_value_cb( char * value );
                        printf( "key-->%s\r\n", item->valuestring);
+                       recv_key_value_cb( item->valuestring );
+                       
                }
                if(( item =cJSON_GetObjectItem(root, "led_atmos_on_duration"))){
                        //氛围灯打开时间
index 50e5506d522539f2649b75881bdc0452c4309d9d..b5dba993a7667c8bef87a8005463afeec5309701 100644 (file)
@@ -7,8 +7,8 @@
 #include "cQueue.h" 
 #include "iot.h" 
 
-#define IOT_KEY_PATH "/customer/tuya_key"
-#define AUTH_KEY_PATH "./customer/tuya_auth"
+//#define IOT_KEY_PATH "/customer/tuya_key"
+#define IOT_KEY_PATH "./customer/tuya_key"
 
 static const char def_productID[] = "3xeyrwyvbuyajz4a";
 static const char def_deviceID[] = "26dfc49b45e25a93dcwgzg";
@@ -32,7 +32,7 @@ int32_t iot_key_load( char ** out_productID, char ** out_deviceID, char ** out_d
        //1. open file 
        fd = fopen( IOT_KEY_PATH, "r" );
        if( !fd ){
-               printf( "open auty key file[%s] fail!\r\n", AUTH_KEY_PATH );
+               printf( "open auty key file[%s] fail!\r\n", IOT_KEY_PATH );
                goto err_out;
        }
        //2. read json data
index 10c3ec810b46f8a7314e0410c9a552bad2329949..803e33726d9fd743ab2e62fc585ac68920b1dcfc 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <zlib.h>
+
 #include "button.h"
 #include "led.h"
 #include "iot.h"
 #include "mg_log.h"
 
-extern int dp_demo( void );
+#include "device.h"
 
-void iot_start( void );
 
+void iot_start( void );
 extern void btn_light_ctrl_init( void );
 extern void btn_light_ctrl_start( void );
 
+extern int32_t dp_report_all( mg_dev_t *dev );
+
+mg_dev_t g_mg_dev;
+
+dev_st_t mg_device_st_get(void)
+{
+       dev_st_t st;
+       //lock
+       st = g_mg_dev.st;
+       //unlock
+       return st;
+}
+
+dev_st_t mg_device_st_set(dev_st_t st )
+{
+       dev_st_t old_st;
+//lock
+       old_st  = g_mg_dev.st;
+       g_mg_dev.st = st;
+//unlock
+       return old_st;
+}
+
+int32_t is_iot_connected( void );
+
 int main( void )
 {
        mg_log_init();
        MG_LOGD( "mg app start..." );
 
+       //设备状态初始化
+       memset( &g_mg_dev, 0, sizeof( g_mg_dev ) );
+
+       //从配置文件读取参数
+       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 );     
+
+       //中间件初始化
        btn_light_ctrl_init();
+       device_wifi_init();
 
-       //wifi_init();
-       MG_LOGD( "btn light ctrl start..");
-       btn_light_ctrl_start();
+       //mg_device_st_set( DEV_MODE_INIT );
+
+       device_wifi_start(); // 启动wifi task;
+
+       //检查授权文件
+       while( load_auth_key( &g_mg_dev ) != 0 ){
+               //未授权,等待授权
+               mg_device_st_set( 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 ...
+       }
+       #endif
+
+       if( is_device_active( &g_mg_dev ) ){
+               btn_light_ctrl_start(); //启动按键背光控制子流程
+       }else{
+               //已经激活,启动按键背光控制
+               mg_device_st_set( DS_AUTH_WIFICONFIG_NO_ACTIVE );
+       }
 
        MG_LOGD( "iot start..");
        iot_start();
 
+       while( !is_iot_connected() ){
+               //wait iot connect ...
+               MG_LOGD( "wait iot connect...");
+               sleep(2);       
+       }
+
+       dp_property_report_bool( "active", g_mg_dev.mg_active );
+
+       //激活
+       if( !is_device_active( &g_mg_dev ) ){
+               mg_device_st_set( DS_AUTH_WIFICONFIG_NO_ACTIVE );
+
+               //激活流程,堵塞,直到激活成功后返回
+               mg_device_active( &g_mg_dev );
+               btn_light_ctrl_start(); //启动按键背光控制子流程
+       }
+
+       mg_device_st_set( DS_ACTIVE );
+       //上报所有dp 信息。
+       dp_report_all( &g_mg_dev );
+
+
+#if 0 
+       MG_LOGD( "btn light ctrl start..");
+       //TODO :启动手势识别子流程
+
+       while( !is_device_active() ){
+               //todo  开始激活流程
+               device_active();
+               sleep(1);
+       }
+       mg_device_st_set( AUTH_ST_ACTIVE );
+#endif 
        while(1){
                sleep(1000);
        }
        return 0;
 }
-
index 633b03f5aae459fa38e29660ed09ebd26cb0fd72..4a8eac7140c8f2e803a602cfb2806d76c81920c1 100644 (file)
@@ -92,15 +92,15 @@ void comb_btn_scan( mg_button_t *btn_ctrl, comb_btn_st_t *comb, uint32_t pressed
                                comb->st = 2; //wait all btn released;
                        }else{
                                if( !comb->long_press_1s && curr_time_ms - comb->pressed_tick > 1000 ){
-                                       printf( "---------------comb long press 1s ...%x, time:[%d - %d]\n", comb->code, comb->pressed_tick );
+                                       //printf( "---------------comb long press 1s ...%x, time:[%d - %d]\n", comb->code, curr_ms, comb->pressed_tick );
                                        comb->long_press_1s = 1;
                                        comb_btn_report( btn_ctrl, comb->code, EVENT_COMB_PRESS );
                                }else if( !comb->long_press_2s && curr_time_ms - comb->pressed_tick > 2000 ){
-                                       printf( "---------------comb long press 2s ...%x\n", comb->code );
+                                       //printf( "---------------comb long press 2s ...%x\n", comb->code );
                                        comb->long_press_2s = 1;
                                        comb_btn_report( btn_ctrl, comb->code, EVENT_COMB_PRESS_2S );
                                }else if( !comb->long_press_5s && curr_time_ms - comb->pressed_tick > 5000 ){
-                                       printf( "---------------comb long press 5s ...%x\n", comb->code );
+                                       //printf( "---------------comb long press 5s ...%x\n", comb->code );
                                        comb->long_press_5s = 1;
                                        comb->st = 2;
                                        comb_btn_report( btn_ctrl, comb->code, EVENT_COMB_PRESS_5S );
index 742db5b56908f0bb7b1c551722857b1161425f6c..903bd56dc422b0184bade87504096c31a37884a5 100644 (file)
@@ -17,7 +17,16 @@ include_directories(
 aux_source_directory(./src/ SRCS)
 
 add_library( ${PROJECT_NAME} STATIC  ${SRCS} )
+
 #add_library( ${PROJECT_NAME}  ${SRCS} )
 #install( TARGETS ${PROJECT_NAME} RUNTIME DESTINATION /app/lib )
 #install( TARGETS ${PROJECT_NAME} LIBRARY DESTINATION /app/lib )
 #install( TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION /app/lib )
+
+link_directories( ${PROJECT_SOURCE_DIR}/. )
+
+aux_source_directory(./test/ TEST_SRCS)
+add_executable( led_test ${TEST_SRCS} )
+target_link_libraries( led_test PUBLIC rt pthread mg_led mg_log log4c rt pthread )
+
+
index a452175a45732e4021dc08df4fdce0243ddc9ccf..be07b62b3f362487954425bc344a852c952add34 100644 (file)
@@ -285,7 +285,7 @@ void* led_thread_fun( void *led )
                                timeradd( &curr_tm, &add_tm, &to_next_mode_tm );
                        }
 
-                       printf( " to next: curr:%d, +%d = %d \n", clock(), curr_cfg.next_time_ms, to_next_mode_tick );
+                       //printf( " to next: curr:%d, +%d = %d \n", clock(), curr_cfg.next_time_ms, to_next_mode_tick );
 
                        while( 1){
                                        usleep( 100*1000 );
diff --git a/led/test/led_test.c b/led/test/led_test.c
new file mode 100644 (file)
index 0000000..d2e87bf
--- /dev/null
@@ -0,0 +1,23 @@
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <signal.h>
+#include <time.h>
+#include "led.h"
+#include "mg_log.h"
+#include "pthread.h"
+#include "semaphore.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+int main( void )
+{
+       mg_led_ctrl_t * ctrl = mg_led_open();
+       printf( "led test..., open led, ret:%p\n", ctrl );
+       return 0;
+}
+