From 83922079ef52c0b29a271c36deaa8c150a665ee5 Mon Sep 17 00:00:00 2001 From: kyz Date: Tue, 30 May 2023 16:37:07 +0800 Subject: [PATCH] =?utf8?q?=E5=A2=9E=E5=8A=A0dp=E4=BF=A1=E6=81=AF=E5=A4=84?= =?utf8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- app/inc/device.h | 114 +++++++++---- app/inc/mg_version.h | 7 + app/src/device_button.c | 37 +++-- app/src/device_config.c | 281 ++++++++++++++++++++++++++++++- app/src/device_ctrl.c | 130 ++++++++------- app/src/device_light.c | 24 ++- app/src/device_status.c | 21 +++ app/src/iot_dp.c | 360 ++++++++++++++++------------------------ app/src/main.c | 142 +++++++++++++--- app/src/mg_init.c | 39 ----- button/src/button.c | 2 - 11 files changed, 766 insertions(+), 391 deletions(-) create mode 100644 app/inc/mg_version.h create mode 100644 app/src/device_status.c delete mode 100644 app/src/mg_init.c diff --git a/app/inc/device.h b/app/inc/device.h index 02d5770..3c9c1ef 100644 --- a/app/inc/device.h +++ b/app/inc/device.h @@ -12,7 +12,7 @@ #include "cQueue.h" #ifdef __x86_64__ -#warning "PC Version Debug" +//#warning "PC Version Debug" #define DEV_CFG_PATH "dev.cfg" #define TUYA_AUTH_PATH "tuya_auth" #define MG_ACTIVE_PATH "mg.active" @@ -38,7 +38,7 @@ #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 MG_TOF_SYNC_DEF 0 //bool #define LED_ATMOS_OFF_DURATION_DEF 5 //1-20 @@ -61,18 +61,18 @@ typedef struct{ //uint32_t recognition_mode; //手势识别补光 uint32_t recognition_level; //手势识别灵敏度 uint32_t recognition_duration; //手势识别关闭延时 - //privacy_mode bool ro + uint32_t privacy_mode; //true, 关闭手势识别功能 //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_disable_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 mg_tof_sync; //人感状态同步, mg_disable_row 为false时,平台下发sync值, 人感状态由sync和本地传感器信号任意一个触发。 + uint32_t led_atmos_on_duration;//氛围灯打开时间 uint32_t led_atmos_off_duration;//氛围灯关闭时间, 数值范围: 1-20, 间距: 1, 倍数: 0, 单位: 秒 默认5秒 // }dev_cfg_t; @@ -98,7 +98,11 @@ typedef struct{ uint32_t wifi_config; uint32_t wifi_config_timeout; + uint32_t bl_manual; + + uint32_t mg_tof_status; + pthread_mutex_t lock; }mg_dev_t; @@ -123,28 +127,6 @@ typedef enum{ LIGHT_MODE_TOF_DEACTIVE, }light_mode_t; -typedef enum{ - DEV_MODE_INIT = 0, -//未授权:控制所有按键背光连续慢闪,装饰灯带常灭;不加载涂鸦 SDK应用程序轮询授权是否已写入,并校验写入长度符合要求; - DEV_MODE_NO_AUTH, -//已授权未配网:所有灯光常灭; - DEV_MODE_AUTH_NO_WIFICONFIG, -//配网过程中 LED7 & LED8 连续慢闪, 2分钟超时后退出 - DEV_MODE_AUTH_WIFICONFIGING, -//已授权已配网未激活:所有灯光常灭; - DEV_MODE_AUTH_WIFICONFIG_NO_ACTIVE, -//已授权已配网已激活:进入主流程,灯光根据 DP 设置处理。 - DEV_MODE_ACTIVE, - DEV_MODE_TOF_ACTIVE, - DEV_MODE_TOF_DEACTIVE, -//单个按键触发,按键对应背光快闪 1s,并上报按键触发类型⾄相应 DP; -//按 5s KEY7 + KEY8,进入配网模式,LED7 & LED8 连续慢闪; -//按 2s KEY1 + KEY8,进入清洁模式,所有 LED 连续慢闪; -//按 2s KEY7 + KEY2,退出清洁模式,所有 LED 恢复正常状态 - //DEV_MODE_CLEAN, -}dev_mode_t; - - void light_mode_set( light_mode_t mode ); //void light_brightness_set( led_id_t id, uint32_t br ); @@ -161,5 +143,81 @@ 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 ); + +//---------------------------------------------------------------------------- +//cfg info + +int32_t cfg_key_led_max_read( mg_dev_t *dev ); +int32_t cfg_key_led_max_write( mg_dev_t *dev, int32_t br ); + +int32_t cfg_atmos_led_max_read( mg_dev_t *dev ); +int32_t cfg_atmos_led_max_write( mg_dev_t *dev, int32_t br ); + +int32_t cfg_recognition_level_read( mg_dev_t *dev ); +int32_t cfg_recognition_level_write( mg_dev_t *dev, int32_t level ); + +int32_t cfg_recognition_duration_read( mg_dev_t *dev ); +int32_t cfg_recognition_duration_write( mg_dev_t *dev, int32_t dur_sec ); + +int32_t cfg_privacy_mode_read( mg_dev_t *dev); +int32_t cfg_privacy_mode_write( mg_dev_t *dev, int32_t mode ); + +int32_t cfg_mg_tof_threshold_read( mg_dev_t *dev ); +int32_t cfg_mg_tof_threshold_write( mg_dev_t *dev, int32_t threshold_cm ); + +int32_t cfg_mg_tof_interval_read( mg_dev_t *dev ); +int32_t cfg_mg_tof_interval_write( mg_dev_t *dev, int32_t interval_sec); + +int32_t cfg_mg_disable_row_read( mg_dev_t *dev ); +int32_t cfg_mg_disable_row_write( mg_dev_t *dev, int32_t disable ); + +int32_t cfg_mg_key_enable_read( mg_dev_t *dev ); +int32_t cfg_mg_key_enable_write( mg_dev_t *dev, int32_t en_mask ); + +int32_t cfg_bl_mode_read( mg_dev_t *dev ); +int32_t cfg_bl_mode_write( mg_dev_t *dev, int32_t mode ); //0:auto, 1-manual + +int32_t cfg_led_atmos_on_duration_read( mg_dev_t *dev ); +int32_t cfg_led_atmos_on_duration_write( mg_dev_t *dev, int32_t dur_sec ); + +int32_t cfg_led_atmos_off_duration_read( mg_dev_t *dev ); +int32_t cfg_led_atmos_off_duration_write( mg_dev_t *dev, int32_t dur_sec ); + +int32_t cfg_mg_tof_sync_read( mg_dev_t *dev); +int32_t cfg_mg_tof_sync_write( mg_dev_t *dev, int32_t sync ); + +//dp +typedef enum{ + GS_GUN, + GS_LITTLE_FINGER, + GS_HORNED_HAND, + GS_ILOVEYOU, + GS_FOUR_FINGERS, + GS_THREE_FINGERS, + GS_WELL_PLAYED, + GS_FINGERTIP, + GS_FINGER_HEART, + GS_ROCK, + GS_PAPER, + GS_THUMBS_UP, + GS_SCISSORS, + GS_OK, + GS_NR +}gesture_ev_t; + +void iot_dp_init( void ); + +//dp report +int32_t dp_tof_status_report( uint32_t st ); +void dp_gesture_event_report( gesture_ev_t ev ); +void dp_button_event_report( button_code_t btn_code, button_event_t btn_evt ); +int32_t dp_recognition_running_report( uint32_t run ); //bool +int32_t dp_recognition_mode_report( uint32_t mode ); //0-"light_mode", 1-"dark_mode" + +// +void mg_dev_lock( mg_dev_t *dev ); +void mg_dev_unlock( mg_dev_t *dev ); + #endif //__MG_DEVICE_H__ diff --git a/app/inc/mg_version.h b/app/inc/mg_version.h new file mode 100644 index 0000000..a02a20a --- /dev/null +++ b/app/inc/mg_version.h @@ -0,0 +1,7 @@ +#ifndef __MG_VERSION_H__ +#define __MG_VERSION_H__ + +#define SW_VERSION "1.0" + +#endif //__MG_VERION_H__ + diff --git a/app/src/device_button.c b/app/src/device_button.c index c2d850c..d212729 100644 --- a/app/src/device_button.c +++ b/app/src/device_button.c @@ -16,7 +16,7 @@ static pthread_mutex_t btn_lock; static cQueue_t *btn_msg_q; -static void button_single_clicked_cb( button_code_t btn_code, button_event_t btn_event, void* value ); +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{ @@ -59,32 +59,34 @@ void device_button_init( void ) btn_msg_q = cQcreate( 100 ); btn_ctrl = mg_button_open(NULL); //"/dev/input/by-path/platform-gpio_keys-event" ); if( btn_ctrl ){ - mg_button_event_register( btn_ctrl, 0xFF, EVENT_SINGLE_CLICK|EVENT_DOUBLE_CLICK|EVENT_LONG_PRESS|EVENT_LONG_PRESS_UP, button_single_clicked_cb ); + mg_button_event_register( btn_ctrl, 0xFF, EVENT_SINGLE_CLICK|EVENT_DOUBLE_CLICK|EVENT_LONG_PRESS|EVENT_LONG_PRESS_UP, button_single_cb ); mg_button_event_combonation_register( btn_ctrl, BUTTON_7|BUTTON_8, EVENT_COMB_PRESS_5S, 0, button_comb_cb ); mg_button_event_combonation_register( btn_ctrl, BUTTON_1|BUTTON_8, EVENT_COMB_PRESS_2S, 0, button_comb_cb ); mg_button_event_combonation_register( btn_ctrl, BUTTON_2|BUTTON_7, EVENT_COMB_PRESS_2S, 0, button_comb_cb ); -#if 0 -//for debug - mg_button_event_register( btn_ctrl, 0xff, 0x0f, button_single_clicked_cb); - mg_button_event_combonation_register( btn_ctrl, 0x03, EVENT_COMB_PRESS, 0, button_comb_cb ); - - printf( "find 0x80 0f cb:%p\n", find_single_cb( btn_ctrl, 0x80, 0xf ) ); - printf( "find combo 0x03 10 cb:%p\n", find_comb_cb( btn_ctrl, 0x03, 0 ) ); - printf( "find combo 0xc0 10 cb:%p\n", find_comb_cb( btn_ctrl, 0xc0, 0 ) ); - printf( "find combo 0x30 10 cb:%p\n", find_comb_cb( btn_ctrl, 0x30, 0 ) ); -#endif - -// }else{ MG_LOGE( "mg_button_open fail\n" ); } } -extern int32_t dp_button_event_push( button_code_t btn_code, button_event_t btn_event ); +extern mg_dev_t g_mg_dev; +int32_t is_key_disabled( button_code_t btn_code ) +{ + uint32_t mask = cfg_mg_key_enable_read( &g_mg_dev ); + if( (btn_code & mask ) != btn_code ){ + MG_LOGD( "drop key, code %02X, mask:%02X\n", mask, btn_code ); + return 1; + }else{ + return 0; + } +} -static void button_single_clicked_cb( button_code_t btn_code, button_event_t btn_event, void* value ) +static void button_single_cb( button_code_t btn_code, button_event_t btn_event, void* value ) { + //key_enable + if( is_key_disabled( btn_code ) ){ + return; + } btn_msg_t msg; msg.code = btn_code; msg.ev = btn_event; @@ -94,6 +96,9 @@ static void button_single_clicked_cb( button_code_t btn_code, button_event_t btn static void button_comb_cb( button_code_t btn_code, button_event_t btn_event, void* value ) { + if( is_key_disabled( btn_code ) ){ + return; + } btn_msg_t msg; msg.code = btn_code; msg.ev = btn_event; diff --git a/app/src/device_config.c b/app/src/device_config.c index d895dba..64f03dc 100644 --- a/app/src/device_config.c +++ b/app/src/device_config.c @@ -6,11 +6,11 @@ #include #include #include +#include //#include "mg_log.h" #include "device.h" - static const dev_cfg_t dev_cfg_default={ //dp info .key_led_max = KEY_LED_MAX_DEF, @@ -21,11 +21,11 @@ static const dev_cfg_t dev_cfg_default={ .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_disable_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, //人感状态同步 + .mg_tof_sync = MG_TOF_SYNC_DEF, //人感状态同步 .led_atmos_off_duration = LED_ATMOS_OFF_DURATION_DEF, //氛围灯关闭时间, 数值范围: 1-20, 间距: 1, 倍数: 0, 单位: 秒 默认5秒 }; @@ -232,3 +232,278 @@ int32_t mg_device_active( mg_dev_t *dev ) return 0; } + +//---------------------------------------------------------------------------- +void mg_dev_lock( mg_dev_t *dev ) +{ + pthread_mutex_lock( &dev->lock ); +} + +void mg_dev_unlock( mg_dev_t *dev ) +{ + pthread_mutex_unlock( &dev->lock ); +} + +int32_t cfg_key_led_max_read( mg_dev_t *dev) +{ + int32_t br = 0; + mg_dev_lock( dev ); + br = dev->cfg.key_led_max; + mg_dev_unlock( dev ); + return br; +} + +int32_t cfg_key_led_max_write( mg_dev_t *dev, int br ) +{ + if( br != dev->cfg.key_led_max ){ + MG_LOGI( "key_led_max: %d -> %d\n", dev->cfg.key_led_max, br ); + } + mg_dev_lock( dev ); + dev->cfg.key_led_max = br; + mg_dev_unlock( dev ); + return 0; +} + +int32_t cfg_atmos_led_max_read( mg_dev_t *dev) +{ + int32_t br = 0; + mg_dev_lock( dev ); + br = dev->cfg.atmos_led_max; + mg_dev_unlock( dev ); + return br; +} + +int32_t cfg_atmos_led_max_write( mg_dev_t *dev, int br ) +{ + if( br != dev->cfg.atmos_led_max ){ + MG_LOGI( "atmos_led_max: %d -> %d\n", dev->cfg.atmos_led_max, br ); + } + mg_dev_lock( dev ); + dev->cfg.atmos_led_max = br; + mg_dev_unlock( dev ); + return 0; +} + +int32_t cfg_recognition_level_read( mg_dev_t *dev) +{ + int32_t level; + mg_dev_lock( dev ); + level = dev->cfg.recognition_level; + mg_dev_unlock( dev ); + return level; +} + +int32_t cfg_recognition_level_write( mg_dev_t *dev, int level ) +{ + if( level != dev->cfg.recognition_level ){ + MG_LOGI( ": %d -> %d\n", dev->cfg.recognition_level, level ); + } + mg_dev_lock( dev ); + dev->cfg.recognition_level= level; + mg_dev_unlock( dev ); + return 0; +} + +int32_t cfg_recognition_duration_read( mg_dev_t *dev) +{ + int32_t dur_sec; + mg_dev_lock( dev ); + dur_sec = dev->cfg.recognition_duration; + mg_dev_unlock( dev ); + return dur_sec; +} + +int32_t cfg_recognition_duration_write( mg_dev_t *dev, int32_t dur_sec ) +{ + if( dur_sec != dev->cfg.recognition_duration){ + MG_LOGI( "dur_sec: %d -> %d\n", dev->cfg.recognition_duration, dur_sec ); + } + mg_dev_lock( dev ); + dev->cfg.recognition_duration = dur_sec; + mg_dev_unlock( dev ); + return 0; +} + +int32_t cfg_privacy_mode_read( mg_dev_t *dev) +{ + int32_t mode; + mg_dev_lock( dev ); + mode = dev->cfg.privacy_mode; + mg_dev_unlock( dev ); + return mode; +} + +int32_t cfg_privacy_mode_write( mg_dev_t *dev, int32_t mode ) +{ + if( mode != dev->cfg.privacy_mode ){ + MG_LOGI( "privacy_mode: %d -> %d\n", dev->cfg.privacy_mode, mode ); + } + mg_dev_lock( dev ); + dev->cfg.privacy_mode = mode; + mg_dev_unlock( dev ); + return 0; +} + +int32_t cfg_mg_tof_threshold_read( mg_dev_t *dev) +{ + int32_t threshold; + mg_dev_lock( dev ); + threshold = dev->cfg.mg_tof_threshold; + mg_dev_unlock( dev ); + return threshold; +} + +int32_t cfg_mg_tof_threshold_write( mg_dev_t *dev, int32_t threshold_cm ) +{ + if( threshold_cm != dev->cfg.mg_tof_threshold ){ + MG_LOGI( ": %d -> %d\n", dev->cfg.mg_tof_threshold, threshold_cm ); + } + mg_dev_lock( dev ); + dev->cfg.mg_tof_threshold = threshold_cm; + mg_dev_unlock( dev ); + return 0; +} + +int32_t cfg_mg_tof_interval_read( mg_dev_t *dev) +{ + int32_t interval; + mg_dev_lock( dev ); + interval = dev->cfg.mg_tof_interval; + mg_dev_unlock( dev ); + return interval; +} + +int32_t cfg_mg_tof_interval_write( mg_dev_t *dev, int32_t interval_sec) +{ + if( interval_sec != dev->cfg.mg_tof_interval ){ + MG_LOGI( "mg_tof_interval: %d -> %d\n", dev->cfg.mg_tof_interval, interval_sec ); + } + mg_dev_lock( dev ); + dev->cfg.mg_tof_interval = interval_sec; + mg_dev_unlock( dev ); + return 0; +} + +int32_t cfg_mg_disable_row_read( mg_dev_t *dev) +{ + int32_t row_disable; + + mg_dev_lock( dev ); + row_disable = dev->cfg.mg_disable_row; + mg_dev_unlock( dev ); + return row_disable; +} + +int32_t cfg_mg_disable_row_write( mg_dev_t *dev, int32_t disable ) +{ + if( disable != dev->cfg.mg_disable_row ){ + MG_LOGI( "mg_disable_row: %d -> %d\n", dev->cfg.mg_disable_row, disable ); + } + mg_dev_lock( dev ); + dev->cfg.mg_disable_row = disable; + mg_dev_unlock( dev ); + return 0; +} + + +int32_t cfg_mg_key_enable_read( mg_dev_t *dev) +{ + int32_t en_mask; + mg_dev_lock( dev ); + en_mask = dev->cfg.mg_key_enable; + mg_dev_unlock( dev ); + return en_mask; +} + +int32_t cfg_mg_key_enable_write( mg_dev_t *dev, int32_t en_mask ) +{ + if( en_mask != dev->cfg.mg_key_enable){ + MG_LOGI( "key enable: %02X -> %02X\n", dev->cfg.mg_key_enable, en_mask ); + } + mg_dev_lock( dev ); + dev->cfg.mg_key_enable = en_mask; + mg_dev_unlock( dev ); + return 0; +} + +int32_t cfg_bl_mode_read( mg_dev_t *dev) //0:auto, 1-manual +{ + int32_t mode; + mg_dev_lock( dev ); + mode = dev->cfg.bl_mode; + mg_dev_unlock( dev ); + return mode; +} + +int32_t cfg_bl_mode_write( mg_dev_t *dev, int32_t mode ) //0:auto, 1-manual +{ + if( mode != dev->cfg.bl_mode){ + MG_LOGI( "bl_mode: %d -> %d\n", dev->cfg.bl_mode, mode ); + } + mg_dev_lock( dev ); + dev->cfg.bl_mode = mode; + mg_dev_unlock( dev ); + return 0; +} + +int32_t cfg_led_atmos_on_duration_read( mg_dev_t *dev) +{ + int32_t dur_sec; + mg_dev_lock( dev ); + dur_sec = dev->cfg.led_atmos_on_duration; + mg_dev_unlock( dev ); + return dur_sec; +} + +int32_t cfg_led_atmos_on_duration_write( mg_dev_t *dev, int32_t dur_sec ) +{ + if( dur_sec != dev->cfg.led_atmos_on_duration ){ + MG_LOGI( "led_atmos_on_duration %d -> %d\n", dev->cfg.led_atmos_on_duration, dur_sec ); + } + mg_dev_lock( dev ); + dev->cfg.led_atmos_on_duration = dur_sec; + mg_dev_unlock( dev ); + return 0; +} + + +int32_t cfg_led_atmos_off_duration_read( mg_dev_t *dev) +{ + int32_t dur_sec; + mg_dev_lock( dev ); + dur_sec = dev->cfg.led_atmos_off_duration; + mg_dev_unlock( dev ); + return dur_sec; +} + +int32_t cfg_led_atmos_off_duration_write( mg_dev_t *dev, int32_t dur_sec ) +{ + if( dur_sec != dev->cfg.led_atmos_off_duration ){ + MG_LOGI( "led_atmos_off_duration %d -> %d\n", dev->cfg.led_atmos_off_duration, dur_sec ); + } + mg_dev_lock( dev ); + dev->cfg.led_atmos_off_duration = dur_sec; + mg_dev_unlock( dev ); + return 0; +} + +int32_t cfg_mg_tof_sync_read( mg_dev_t *dev) +{ + int32_t sync; + mg_dev_lock( dev ); + sync = dev->cfg.mg_tof_sync; + mg_dev_unlock( dev ); + return sync; +} + +int32_t cfg_mg_tof_sync_write( mg_dev_t *dev, int32_t sync ) +{ + if( sync!= dev->cfg.mg_tof_sync ){ + MG_LOGI( "mg_tof_sync: %d -> %d\n", dev->cfg.mg_tof_sync, sync); + } + mg_dev_lock( dev ); + dev->cfg.mg_tof_sync = sync; + mg_dev_unlock( dev ); + return 0; +} + diff --git a/app/src/device_ctrl.c b/app/src/device_ctrl.c index 78e9ce3..be067ca 100644 --- a/app/src/device_ctrl.c +++ b/app/src/device_ctrl.c @@ -31,7 +31,6 @@ typedef struct{ }btn_msg_t; extern void led_flash_for_btn_single_click( button_code_t btn_code ); -extern void dp_report_button_event( button_code_t btn_code, button_event_t btn_evt ); 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 ); @@ -66,12 +65,23 @@ void * btn_light_ctrl_task( void* unused ) uint32_t to_clean_mode = 0; uint32_t to_clean_mode_exit = 0; uint32_t btn_code = 0; - uint32_t bl_manual = 0; + int32_t bl_manual = 0; - //timeout = 100ms; + //tof + int32_t tof_threshold_cm; + int32_t tof_st; + int32_t tof_sync; + int32_t row_disabled; + time_t tof_check_time; + tof_info_t info; MG_LOGI( "btn light ctrl task run..." ); + tof_st = -1; + bl_manual = -1; + tof_check_time =0; + tof_threshold_cm = 0; + while(1){ usleep(100*1000); //读取按键信息 @@ -82,7 +92,7 @@ void * btn_light_ctrl_task( void* unused ) to_wifi_config = is_btn_wifi_config( &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 ){ MG_LOGI( "enter clean mode ..." ); @@ -94,7 +104,6 @@ void * btn_light_ctrl_task( void* unused ) MG_LOGI( "exit clean mode ..." ); g_mg_dev.clean_device = 0; g_mg_dev.clean_timeout = 0; - //ligth_mode_set( LIGHT_MODE_ACTIVE ); //TODO }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; @@ -105,23 +114,23 @@ void * btn_light_ctrl_task( void* unused ) //获取单击按键的键值 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 ); - dp_report_button_event( btn_code, btn_msg.ev ); + led_flash_for_btn_single_click( btn_code ); //单击按钮,LED快闪 + dp_button_event_report( btn_code, btn_msg.ev ); }else if((btn_code = get_double_click_code( &btn_msg ))){ - dp_report_button_event( btn_code, btn_msg.ev ); + dp_button_event_report( btn_code, btn_msg.ev ); }else if((btn_code = get_long_press_code( &btn_msg ))){ - dp_report_button_event( btn_code, btn_msg.ev ); + dp_button_event_report( btn_code, btn_msg.ev ); }else if((btn_code = get_long_press_up_code( &btn_msg ))){ - dp_report_button_event( btn_code, btn_msg.ev ); - if( btn_code & BUTTON_6 ){ - dbg_tof_active = !dbg_tof_active; - MG_LOGD( "btn_6 -> tof statu: %d", dbg_tof_active ); - } + 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( time(NULL) > g_mg_dev.clean_timeout ){ g_mg_dev.clean_device = 0; g_mg_dev.clean_timeout = 0; @@ -129,6 +138,7 @@ void * btn_light_ctrl_task( void* unused ) g_mg_dev.wifi_config_timeout = 0; } }else if( g_mg_dev.wifi_config){ + //检查配网超时 if( time(NULL) > g_mg_dev.wifi_config_timeout ){ g_mg_dev.wifi_config= 0; g_mg_dev.wifi_config_timeout = 0; @@ -136,34 +146,21 @@ void * btn_light_ctrl_task( void* unused ) } //背光模式检查 - bl_manual = dp_bl_mode_read()?1:0; - if( g_mg_dev.bl_manual != bl_manual ){ + if( bl_manual != cfg_bl_mode_read( &g_mg_dev )){ + bl_manual = cfg_bl_mode_read( &g_mg_dev ); MG_LOGI( "bl mode -> %s", bl_manual?"manual":"auto" ); - g_mg_dev.bl_manual = bl_manual; - if( device_bl_mode_manual ){ - //TOF_Disable... - MG_LOGI( "disable tof ..." ); + //背光模式改变,启动或停止tof + if( bl_manual ){ + // + MG_LOGI( "bl manual mode, disable tof ..." ); mg_tof_disable(g_tof); }else{ //TOF_Enable... - MG_LOGI( "enable tof ..." ); + MG_LOGI( "bl auto mode, enable tof ..." ); mg_tof_enable(g_tof ); } } - if( g_mg_dev.bl_manual ){ - //手动模式 - //get key led brightness - //get atmos led brightness - //if( key_led_brightness change ) - // key_led_update brightness - //if( atoms_led_brightness change ) - // atoms_led_update brightness - // led_key_update brightness - }else{ - //自动模式 - } - dev_st_t st = g_mg_dev.st; if( g_mg_dev.clean_device ){ light_mode_set( LIGHT_MODE_CLEAN ); @@ -181,44 +178,61 @@ void * btn_light_ctrl_task( void* unused ) light_mode_set( LIGHT_MODE_AUTH_WIFICONFIG_NO_ACTIVE ); }else if( st == DS_ACTIVE ){ - if( g_mg_dev.bl_manual ){ + if( bl_manual ){ uint32_t key_led_br; - uint32_t fun_led_br; + uint32_t atmos_led_br; - key_led_br = dp_key_led_br_read(); - fun_led_br = dp_fun_led_br_read(); + key_led_br = cfg_key_led_max_read( &g_mg_dev ); + atmos_led_br = cfg_atmos_led_max_read( &g_mg_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, fun_led_br ); - + light_set_atmos_br( LED_ID_ATMOS_ALL, atmos_led_br ); + }else{ - static uint32_t tof_dist = 1000; - if( tof_dist != dp_tof_threshold_read() ){ - MG_LOGI( "tof dist change %d -> %d", tof_dist, dp_tof_threshold_read() ); - tof_dist = dp_tof_threshold_read(); - Distance_parameter_correction(g_tof, tof_dist); + tof_sync = cfg_mg_tof_sync_read( &g_mg_dev ); + row_disabled = cfg_mg_disable_row_read( &g_mg_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 ); + //阈值调整,重新设置阈值 + Distance_parameter_correction(g_tof, tof_threshold_cm * 10 ); } - //自动模式 - static time_t tof_check_time =0; - if( tof_check_time < time(NULL ) ){ - tof_check_time = time(NULL) + 5; //暂时按5秒检查一次 - tof_info_t info; + + if( !row_disabled && tof_sync ){ + //联排功能,平台设置人感状态 + light_mode_set( LIGHT_MODE_TOF_ACTIVE ); + } + + if( g_tof && time(NULL ) - tof_check_time > cfg_mg_tof_interval_read( &g_mg_dev ) ){ + + tof_check_time = time(NULL); memset( &info, 0, sizeof( info ) ); - - if( g_tof && mg_tof_read( g_tof, &info )==0 ){ + + //检查周期到达,读取tof状态 + if( mg_tof_read( g_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 ); } - if( info.enable && info.status == TOF_ANYBODY ){ + if( !row_disabled && tof_sync ){ + //联排功能,平台设置人感状态 + light_mode_set( LIGHT_MODE_TOF_ACTIVE ); + }else if( info.enable && info.status == TOF_ANYBODY ){ light_mode_set( LIGHT_MODE_TOF_ACTIVE ); - //TODO:dp_report tof mode; }else{ light_mode_set( LIGHT_MODE_TOF_DEACTIVE ); } + if( tof_st!= info.status ){ + //状态变化,上报DP + tof_st= info.status; + dp_tof_status_report( tof_st== TOF_ANYBODY?1:0 ); + MG_LOGI( "device tof status - > %d \n", tof_st ); + } } } } @@ -235,9 +249,7 @@ static void tof_cb( tof_info_t *info) void btn_light_ctrl_init( void ) { - g_mg_dev.st = DEV_MODE_ACTIVE; - g_mg_dev.clean_device = 0; - g_mg_dev.clean_timeout = 0; + uint32_t tof_threshold_cm; device_button_init(); device_light_init(); @@ -245,6 +257,8 @@ void btn_light_ctrl_init( void ) 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 ); }else{ MG_LOGE( "open tof fail!" ); } diff --git a/app/src/device_light.c b/app/src/device_light.c index 9a3abd9..7660d14 100644 --- a/app/src/device_light.c +++ b/app/src/device_light.c @@ -12,6 +12,7 @@ //--------------------------------------------------// static light_ctrl_t g_led_ctrl; +extern mg_dev_t g_mg_dev; typedef struct{ uint32_t key_led_max; @@ -59,22 +60,29 @@ char *light_mode_name[]={ void light_mode_set( light_mode_t mode ) { + uint32_t dur_sec; + uint32_t key_mask = cfg_mg_key_enable_read( &g_mg_dev ); if( g_lightinfo.mode == mode ) return; MG_LOGI( "light mode %s -> %s", light_mode_name[g_lightinfo.mode], light_mode_name[mode] ); g_lightinfo.mode = 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); 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); + break; case LIGHT_MODE_CLEAN: //clean mode en //进入清洁模式,所有 LED 连续慢闪; - key_led_flash( g_led_ctrl, LED_ID_KEY_ALL, 1000, 0); + 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); break; @@ -92,20 +100,22 @@ void light_mode_set( light_mode_t mode ) break; case LIGHT_MODE_ACTIVE: // - key_led_on( g_led_ctrl, LED_ID_KEY_ALL, 0, 100, 0); + key_led_on( g_led_ctrl, LED_ID_KEY_ALL&key_mask, 0, 100, 0); atmos_led_on( g_led_ctrl, LED_ID_ATMOS_ALL, 0, 100, 0); break; case LIGHT_MODE_TOF_ACTIVE: // - key_led_on( g_led_ctrl, LED_ID_KEY_ALL, 0, 100, 3*1000 ); - atmos_led_off( g_led_ctrl, LED_ID_ATMOS_ALL, 100, 0, 3*1000 ); + 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, 2000, 10*1000 ); break; case LIGHT_MODE_TOF_DEACTIVE: // - key_led_off( g_led_ctrl, LED_ID_KEY_ALL, 100, 0, 10*1000 ); + 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, 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 ); break; diff --git a/app/src/device_status.c b/app/src/device_status.c new file mode 100644 index 0000000..c9c4340 --- /dev/null +++ b/app/src/device_status.c @@ -0,0 +1,21 @@ +#include "device.h" +#include "mg_version.h" + +#define MG_ST_PATH "/var/run/mg_app/" +//#define MG_VER_PATH "/var/run/mg_app/version" + +static int32_t version_init( void ) +{ + if( system( "echo " SW_VERSION " > " MG_ST_PATH "version" ) ){ + // + } + return 0; +} + +int32_t mg_device_run_st_init( void ) +{ + version_init(); + + return 0; +} + diff --git a/app/src/iot_dp.c b/app/src/iot_dp.c index d8301b2..a5e65ad 100644 --- a/app/src/iot_dp.c +++ b/app/src/iot_dp.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -10,129 +11,34 @@ #include "iot.h" #include "device.h" -typedef enum{ - DP_SINGLE_CLICK = 0, - DP_DOUBLE_CLICK = 1, - DP_LONG_PRESS = 2, - DP_LONG_PRESS_UP = 3, - DP_BUTTON_EV_MAX, -}dp_button_event_t; - +#include "base64.h" +#if 0 typedef struct{ - uint32_t key_led_max; - uint32_t atmos_led_max; + //uint32_t key_led_max; + //uint32_t atmos_led_max; uint32_t recognition_mode; //手势识别补光 - uint32_t recognition_level; //手势识别灵敏度 - uint32_t recognition_duration; //手势识别关闭延时 + //uint32_t recognition_level; //手势识别灵敏度 + //uint32_t recognition_duration; //手势识别关闭延时 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 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秒 + //uint32_t led_atmos_off_duration;//氛围灯关闭时间, 数值范围: 1-20, 间距: 1, 倍数: 0, 单位: 秒 默认5秒 } iot_dp_t; - -static iot_dp_t g_iot_dp = { - .key_led_max = 20, - .atmos_led_max = 20, - .mg_key_enable = 0xff, - .bl_mode = 0, //0-auto 1-manual - .led_atmos_off_duration = 5, -}; - - -extern void mg_led_brigtness_change_notify( void ); -int32_t iot_dp_int_read( uint32_t dp_id, uint32_t *out_value ); -int32_t iot_dp_int_write( uint32_t dp_id, uint32_t value ); - -int32_t iot_dp_int_write( uint32_t dp_id, uint32_t value ) -{ - printf( "dp write int , id:%d, value:%d\r\n", dp_id, value ); - switch( dp_id ){ - case DP_KEY_LED_MAX: - g_iot_dp.key_led_max = value; - //notify led - //mg_led_brigtness_change_notify(); - break; - case DP_ATMOS_LED_MAX: - g_iot_dp.atmos_led_max = value; - //notify led - //mg_led_brigtness_change_notify(); - break; - case DP_BL_MODE: - g_iot_dp.bl_mode = value; - //mg_bl_change_notify(); - break; - case DP_MG_TOF_THRESHOLD: - g_iot_dp.mg_tof_threshold = value; - //mg_bl_change_notify(); - break; - } - return -1; -} - -int32_t iot_dp_int_read( uint32_t dp_id, uint32_t *out_value ) -{ - switch( dp_id ){ - case DP_KEY_LED_MAX: - *out_value = g_iot_dp.key_led_max; - break; - case DP_ATMOS_LED_MAX: - *out_value = g_iot_dp.atmos_led_max; - break; - case DP_BL_MODE: - *out_value = g_iot_dp.bl_mode; - break; - case DP_MG_TOF_THRESHOLD: - *out_value = g_iot_dp.mg_tof_threshold; - break; - } - return 0; -} - -void mg_led_brigtness_change_notify( void ) -{ - //send msg - uint32_t key_led_max; - uint32_t atmos_led_max; - - if( iot_dp_int_read( DP_KEY_LED_MAX, &key_led_max ) == 0 ){ - //mg_key_led_max_set( key_led_max ); - } - if( iot_dp_int_read( DP_ATMOS_LED_MAX, &atmos_led_max ) == 0 ){ - //mg_atmos_led_max_set( atmos_led_max ); - } - return; -} - - - - - -extern int32_t iot_key_load( char ** out_productID, char ** out_deviceID, char ** out_deviceSecret ); - -//const char productId[] = "3xeyrwyvbuyajz4a"; -//const char deviceId[] = "26dfc49b45e25a93dcwgzg"; -//const char deviceSecret[] = "IAS0hHa3mmgrKn9L"; -char *productId; -char *deviceId; -char *deviceSecret; - -tuya_mqtt_context_t client_instance; +#endif void handle_iot_msg(tuya_mqtt_context_t* context, char *data_string ); +static int32_t dp_event_report( char *code, char *event ); -cQueue_t *g_dp_event_q; - -typedef struct{ - uint32_t value; -} dp_event_t; +extern mg_dev_t g_mg_dev; +cQueue_t *g_dp_event_q; char * button_code_name[]={ "button_1", @@ -152,40 +58,30 @@ char * button_event_name[]={ "long_press_up" }; -//code : 1 -- 8 ->button_1 .. button8 -//event: 1 -- single_click 2--double_click 3--long_press 4--long_press_up -#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]; - if( btn_code >= DP_BUTTON_1 && btn_code <= DP_BUTTON_8 && btn_event < DP_BUTTON_EV_MAX ){ - sprintf( msg_buf, btn_event_fmt, button_code_name[ btn_code-DP_BUTTON_1 ], button_code_name[ btn_code-DP_BUTTON_1 ], button_event_name[btn_event] ); - - void *msg_str = strdup( msg_buf ); - //lock(); - if( g_dp_event_q ){ - cQPost( g_dp_event_q, ( void *)msg_str ); - } - //unlock(); - //send wake event - } - return 0; -} +char * gesture_event_name[] = { + "gun", + "little_finger", + "horned_hand", + "iloveyou", + "four_fingers", + "three_fingers", + "well_played", + "fingertip", + "finger_heart", + "rock", + "paper", + "thumbs_up", + "scissors", + "ok" +}; -//激活流程,通过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\"}" +#define event_fmt_str "E{\"eventCode\":\"%s\",\"outputParams\":{\"%s\":\"%s\" } }" -#endif -int32_t dp_property_report( char *payload ) +int32_t dp_report( char *payload ) { void *msg_str = strdup( payload ); if( g_dp_event_q ){ @@ -195,13 +91,22 @@ int32_t dp_property_report( char *payload ) } +int32_t dp_event_report( char *code, char *event ) +{ + char msg_buf[100]; + + sprintf( msg_buf, event_fmt_str, code, code, event); + + return dp_report( msg_buf ); +} + 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 ); + return dp_report( msg_buf ); } int32_t dp_property_report_int( char *dp_name, int32_t dp_value ) @@ -210,7 +115,7 @@ int32_t dp_property_report_int( char *dp_name, int32_t dp_value ) sprintf( msg_buf, property_fmt_int, dp_name, dp_value ); - return dp_property_report( msg_buf ); + return dp_report( msg_buf ); } int32_t dp_property_report_bool( char *dp_name, int32_t dp_value ) @@ -219,21 +124,26 @@ int32_t dp_property_report_bool( char *dp_name, int32_t dp_value ) sprintf( msg_buf, property_fmt_bool, dp_name, dp_value?"true":"false" ); - return dp_property_report( msg_buf ); + return dp_report( msg_buf ); } +//激活流程,通过dp_key上报KEY ID, 也通过dp_key接收 KEY Value 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 ); + return dp_report( msg_buf ); } int32_t dp_report_all( mg_dev_t *dev ) { - dev_cfg_t *cfg = &dev->cfg; + dev_cfg_t *cfg; + + mg_dev_lock( dev ); + cfg = &dev->cfg; + mg_dev_unlock( dev ); //dp_property_report_bool( "active", dev->mg_active ); @@ -245,20 +155,25 @@ int32_t dp_report_all( mg_dev_t *dev ) usleep(50*1000); dp_property_report_int( "recognition_duration", cfg->recognition_duration); usleep(50*1000); + dp_property_report_int( "privacy_mode", cfg->privacy_mode ); + 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 ); + dp_property_report_int( "mg_disalbe_row", cfg->mg_disable_row); usleep(50*1000); + //dp_property_report_bool( "mg_key_enable", base64_encode(cfg->mg_key_enable) ); //add base64 encode + //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); + usleep(50*1000); + dp_property_report_bool( "mg_tof_status", 0 ); + return 0; } @@ -289,36 +204,39 @@ void handle_iot_msg(tuya_mqtt_context_t* context, char *data_string ) //button_1 .. buttont_8: - event if(( item =cJSON_GetObjectItem(root, "key_led_max"))){ //按键背光最大亮度 - printf( "key_led_max-->%d\r\n", item->valueint ); - sprintf( buf, "{\"key_led_max\":{\"value\":%d}}", item->valueint ); - tuyalink_thing_property_report(context, NULL, buf ); - iot_dp_int_write( DP_KEY_LED_MAX, item->valueint ); + cfg_key_led_max_write( &g_mg_dev, item->valueint ); + dp_property_report_int( "key_led_max", item->valueint ); } if(( item =cJSON_GetObjectItem(root, "atmos_led_max"))){ //氛围灯最大亮度 - printf( "atmos_led_max-->%d\r\n", item->valueint ); - sprintf( buf, "{\"atmos_led_max\":{\"value\":%d}}", item->valueint ); - tuyalink_thing_property_report(context, NULL, buf ); - iot_dp_int_write( DP_ATMOS_LED_MAX, item->valueint ); + cfg_atmos_led_max_write( &g_mg_dev, item->valueint ); + dp_property_report_int( "atmos_led_max", item->valueint ); } //"recognition_running" - ro //gesture event: - event if(( item =cJSON_GetObjectItem(root, "recognition_mode"))){ //手势识别补光 - // - printf( "recognition_mode-->%s\r\n", item->valuestring ); + printf( "recognition_mode-->%s, drop msg\r\n", item->valuestring ); + //不支持下发,忽略 } if(( item =cJSON_GetObjectItem(root, "recognition_level"))){ //手势识别灵敏度 - // - printf( "recognition_level-->%d\r\n", item->valueint); + cfg_recognition_level_write( &g_mg_dev, item->valueint); + dp_property_report_int( "recognition_level", item->valueint ); } if(( item =cJSON_GetObjectItem(root, "recognition_duration"))){ //手势识别关闭延时 - // - printf( "recognition_duration-->%d\r\n", item->valueint); + cfg_recognition_duration_write( &g_mg_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 ); + dp_property_report_bool( "privacy_mode", item->valueint ); } - //"privacy_mode" 隐私模式 ro + //"485_cmd" 动作 //"active" ro + if(( item =cJSON_GetObjectItem(root, "key"))){ // extern int32_t recv_key_value_cb( char * value ); @@ -328,48 +246,57 @@ void handle_iot_msg(tuya_mqtt_context_t* context, char *data_string ) } if(( item =cJSON_GetObjectItem(root, "led_atmos_on_duration"))){ //氛围灯打开时间 - printf( "led_atmos_on_duration-->%d\r\n", item->valueint); + cfg_led_atmos_on_duration_write( &g_mg_dev, item->valueint); + dp_property_report_int( "led_atmos_on_duration", item->valueint ); } if(( item =cJSON_GetObjectItem(root, "mg_tof_threshold"))){ //人感检测距离 - iot_dp_int_write( DP_MG_TOF_THRESHOLD, item->valueint*10 ); - printf( "mg_tof_threshold-->%d cm\r\n", item->valueint); + cfg_mg_tof_threshold_write( &g_mg_dev, item->valueint); + dp_property_report_int( "mg_tof_threshold", item->valueint ); } if(( item =cJSON_GetObjectItem(root, "mg_tof_interval"))){ //人感检测周期 - printf( "mg_tof_interval-->%d\r\n", item->valueint); + cfg_mg_tof_interval_write( &g_mg_dev, item->valueint); + dp_property_report_int( "mg_tof_interval", item->valueint ); } if(( item =cJSON_GetObjectItem(root, "mg_disalbe_row"))){ //关闭联排功能 - printf( "mg_disalbe_row-->%d\r\n", item->valueint); //bool + cfg_mg_disable_row_write( &g_mg_dev, item->valueint); + dp_property_report_bool( "mg_disalbe_row", item->valueint ); } if(( item =cJSON_GetObjectItem(root, "mg_key_enable"))){ //按键禁使能, 默认FF, 一个字节, bit0 是第一个按键, bit7是第八个按键 - printf( "mg_key_enable-->%d\r\n", item->valueint); + uint32_t mask = 0; + 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); + 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{ + MG_LOGI( "decode base64_str[%s] fail!\n", item->valuestring ); + } } if(( item =cJSON_GetObjectItem(root, "bl_mode"))){ //背光模式 默认"自动", 自动模式: 氛围灯亮, 背光灭, 有人后反转. 手动模式: 氛围灯可调, 背光亮度可调, 两者无关联 - printf( "bl_mode-->%s\r\n", item->valuestring); - if( strcmp(item->valuestring, "auto" )==0){ - iot_dp_int_write( DP_BL_MODE, 0 ); - sprintf( buf, "{\"bl_mode\":\"auto\"}"); - }else if( strcmp(item->valuestring, "manual" )==0){ - iot_dp_int_write( DP_BL_MODE, 1 ); - sprintf( buf, "{\"bl_mode\":\"manual\"}"); - } - tuyalink_thing_property_report(context, NULL, buf ); + cfg_bl_mode_write( &g_mg_dev, strcmp(item->valuestring, "manual" )==0?1:0); + dp_property_report_str( "bl_mode", item->valuestring ); } if(( item =cJSON_GetObjectItem(root, "mg_tof_staus"))){ //人感状态 - printf( "mg_tof_status-->%d\r\n", item->valueint); + MG_LOGD( "mg_tof_status-->%d, ignore msg\r\n", item->valueint); + //只上报,忽略消息 + } if(( item =cJSON_GetObjectItem(root, "mg_tof_sync"))){ //人感状态同步 - printf( "mg_tof_sync-->%d\r\n", item->valueint); + cfg_mg_tof_sync_write( &g_mg_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秒 - printf( "led_atmos_off_duration-->%d\r\n", item->valueint); + cfg_led_atmos_off_duration_write( &g_mg_dev, item->valueint); + dp_property_report_int( "led_atmos_off_duration", item->valueint ); } cJSON_Delete( root ); }else{ @@ -377,61 +304,64 @@ void handle_iot_msg(tuya_mqtt_context_t* context, char *data_string ) } } -dp_button_event_t button_event_to_dp( button_event_t evt ) +static char * get_button_event_name( button_event_t evt ) { - if( evt == EVENT_SINGLE_CLICK ) return DP_SINGLE_CLICK; - else if( evt == EVENT_DOUBLE_CLICK ) return DP_DOUBLE_CLICK; - else if( evt == EVENT_LONG_PRESS ) return DP_LONG_PRESS; - else if( evt == EVENT_LONG_PRESS_UP ) return DP_LONG_PRESS_UP; - else return DP_BUTTON_EV_MAX; + if( evt == EVENT_SINGLE_CLICK ) return button_event_name[0]; + else if( evt == EVENT_DOUBLE_CLICK ) return button_event_name[1]; + else if( evt == EVENT_LONG_PRESS ) return button_event_name[2]; + else if( evt == EVENT_LONG_PRESS_UP ) return button_event_name[3]; + else return "null"; } -dp_button_code_t button_code_to_dp( button_code_t btn_code ) +static char *get_button_code_name( button_code_t btn_code ) { - if( btn_code & BUTTON_1 ) return DP_BUTTON_1; - else if( btn_code & BUTTON_2 ) return DP_BUTTON_2; - else if( btn_code & BUTTON_3 ) return DP_BUTTON_3; - else if( btn_code & BUTTON_4 ) return DP_BUTTON_4; - else if( btn_code & BUTTON_5 ) return DP_BUTTON_5; - else if( btn_code & BUTTON_6 ) return DP_BUTTON_6; - else if( btn_code & BUTTON_7 ) return DP_BUTTON_7; - else if( btn_code & BUTTON_8 ) return DP_BUTTON_8; - else return 0; + if( btn_code & BUTTON_1 ) return button_code_name[0]; + else if( btn_code & BUTTON_2 ) return button_code_name[1]; + else if( btn_code & BUTTON_3 ) return button_code_name[2]; + else if( btn_code & BUTTON_4 ) return button_code_name[3]; + else if( btn_code & BUTTON_5 ) return button_code_name[4]; + else if( btn_code & BUTTON_6 ) return button_code_name[5]; + else if( btn_code & BUTTON_7 ) return button_code_name[6]; + else if( btn_code & BUTTON_8 ) return button_code_name[7]; + else return "null"; } -void dp_report_button_event( button_code_t btn_code, button_event_t btn_evt ) +void iot_dp_init( void ) { - dp_button_code_t dp_code; - dp_button_event_t dp_evt; - - dp_code = button_code_to_dp( btn_code ); - dp_evt = button_event_to_dp( btn_evt ); - - dp_button_event_push( dp_code, dp_evt ); + g_dp_event_q = cQcreate( 100 ); } -void iot_dp_init( void ) + +int32_t dp_tof_status_report( uint32_t st ) { - g_dp_event_q = cQcreate( 100 ); + return dp_property_report_bool( "mg_tof_status", st ); } -uint32_t dp_bl_mode_read( void ) +void dp_gesture_event_report( gesture_ev_t ev ) { - return g_iot_dp.bl_mode; + if( ev < GS_NR ){ + dp_event_report( "gesture_event", gesture_event_name[ev] ); + } } -uint32_t dp_key_led_br_read( void ) +void dp_button_event_report( button_code_t btn_code, button_event_t btn_evt ) { - return g_iot_dp.key_led_max; + char* code; + char* event; + + code = get_button_code_name( btn_code ); + event = get_button_event_name( btn_evt ); + + dp_event_report( code, event ); } -uint32_t dp_fun_led_br_read( void ) +int32_t dp_recognition_running_report( uint32_t run ) //bool { - return g_iot_dp.atmos_led_max; + return dp_property_report_bool( "recogninition_running", run ); } -uint32_t dp_tof_threshold_read( void ) +int32_t dp_recognition_mode_report( uint32_t mode ) //0-"light_mode", 1-"dark_mode" { - return g_iot_dp.mg_tof_threshold; + return dp_property_report_str( "recogninition_mode", mode?"dark_mode":"light_mode" ); } diff --git a/app/src/main.c b/app/src/main.c index dae6778..9b14c67 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -5,13 +5,23 @@ #include #include +#include +#include +#include +#include + //#include "button.h" //#include "led.h" //#include "iot.h" //#include "mg_log.h" #include "device.h" +#include "version.h" +#define PID_FILE "/var/run/mg_app/mg_app.pid" +extern int32_t is_iot_connected( void ); +extern void device_wifi_start( void ); +extern void mg_device_st_init( void ); void iot_start( void ); extern void btn_light_ctrl_init( void ); @@ -22,6 +32,17 @@ extern int32_t dp_report_all( mg_dev_t *dev ); mg_dev_t g_mg_dev; +char * mg_st_name( dev_st_t st ) +{ + if( st == DS_INIT ) return "DS_INIT"; + else if( st == DS_NO_AUTH ) return "DS_NO_AUTH"; + else if( st == DS_AUTH_NO_WIFICONFIG ) return "DS_AUTH_NO_WIFICONFIG"; + 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"; + return "unkown"; +} + dev_st_t mg_device_st_get(void) { dev_st_t st; @@ -34,23 +55,97 @@ dev_st_t mg_device_st_get(void) dev_st_t mg_device_st_set(dev_st_t st ) { dev_st_t old_st; -//lock + //lock old_st = g_mg_dev.st; g_mg_dev.st = st; -//unlock + //unlock + 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 ); + }else if( st == DS_NO_AUTH ){ + light_mode_set( LIGHT_MODE_NO_AUTH ); + }else if( st == DS_AUTH_NO_WIFICONFIG){ + light_mode_set( LIGHT_MODE_AUTH_NO_WIFICONFIG ); + }else if( st == DS_AUTH_WIFICONFIG_NO_ACTIVE ){ + light_mode_set( LIGHT_MODE_AUTH_WIFICONFIG_NO_ACTIVE ); + }else if( st == DS_ACTIVE){ + light_mode_set( LIGHT_MODE_ACTIVE ); + } + } return old_st; } -extern int32_t is_iot_connected( void ); -extern void device_wifi_start( void ); +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)); +} + +int alone_runnind(void) +{ + int fd; + char buf[16]; + + fd = open(PID_FILE, O_RDWR | O_CREAT, 0666); + if (fd < 0) + { + perror("open"); + exit(1); + } + + if (lock_file(fd) < 0) + { + if (errno == EACCES || errno == EAGAIN) + { + close(fd); + //printf("alone runnind\n"); + return -1; + } + + printf("can't lock %s: %s\n", PID_FILE, strerror(errno)); + + } + + 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 ) +{ + memset( &g_mg_dev, 0, sizeof( g_mg_dev ) ); + pthread_mutex_init( &g_mg_dev.lock, NULL ); + return 0; +} int main( void ) { + //检查程序是否已经运行 + if( alone_runnind() ){ + printf( "APP is alread runnig!\n" ); + exit( -1 ); + } + + //var/run/mg_app目录下输出信息 + mg_device_run_st_init(); + mg_log_init(); + MG_LOGD( "mg app start..." ); //设备状态初始化 - memset( &g_mg_dev, 0, sizeof( g_mg_dev ) ); + mg_device_init(); //从配置文件读取参数 load_device_cfg( &g_mg_dev.cfg ); @@ -60,9 +155,10 @@ int main( void ) btn_light_ctrl_init(); device_wifi_init(); - //mg_device_st_set( DEV_MODE_INIT ); + mg_device_st_set( DS_INIT ); - device_wifi_start(); // 启动wifi task; + // 启动wifi task; + device_wifi_start(); //检查授权文件 while( load_auth_key( &g_mg_dev ) != 0 ){ @@ -72,7 +168,7 @@ int main( void ) sleep(5); } - #if 0 +#if 0 //TODO:检查配网 //检查配网状态 while( !is_wifi_config( &g_mg_dev ) ){ @@ -81,15 +177,17 @@ int main( void ) sleep(5); if timeout goto ... } - #endif +#endif if( is_device_active( &g_mg_dev ) ){ - btn_light_ctrl_start(); //启动按键背光控制子流程 - }else{ //已经激活,启动按键背光控制 + mg_device_st_set( DS_ACTIVE ); + btn_light_ctrl_start(); + }else{ mg_device_st_set( DS_AUTH_WIFICONFIG_NO_ACTIVE ); } + //连接涂鸦IOT平台 MG_LOGD( "iot start.."); iot_start(); @@ -99,35 +197,33 @@ int main( void ) sleep(2); } + //上报激活状态 dp_property_report_bool( "active", g_mg_dev.mg_active ); - //激活 + //未激活则连接tuya等待激活完成 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(); //启动按键背光控制子流程 + + //已经激活,启动按键背光控制 + 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); + sleep(5*1000); } return 0; } + diff --git a/app/src/mg_init.c b/app/src/mg_init.c deleted file mode 100644 index ff27af7..0000000 --- a/app/src/mg_init.c +++ /dev/null @@ -1,39 +0,0 @@ -#include -#include - -//产品初始化流程 -#if 0 -int32_t is_local_auth( void ) -{ - return 1; -} - -int32_t mg_device_init( void ) -{ - MG_LOGI( "检查本地授权...\r\n" ); - - //检查本地授权 - whie( !is_local_auth() ){ - sleep(2); - } - - MG_LOGI( "本地授权...OK!\r\n" ); - - //wait wifi config - - //等待配网 - while( is_wifi_config() ){ - sleep(2); - } - - //加载涂鸦SDK - tuya_start(); - - //等待涂鸦连接成功 - while( !is_tuya_connected() ){ - sleep(1); - } - - return 0; -} -#endif diff --git a/button/src/button.c b/button/src/button.c index ec1fc01..f1e2af2 100644 --- a/button/src/button.c +++ b/button/src/button.c @@ -32,7 +32,6 @@ typedef struct{ struct mg_button{ char dev_path[100]; int32_t fd; - //todo int32_t lock; pthread_t btn_thread; btn_cb_list_t cb_list; @@ -231,7 +230,6 @@ int32_t mg_button_event_register( mg_button_t *btn_ctrl, button_code_t code, but } pthread_mutex_unlock( &p_list->lock ); - //TODO:增加事件处理函数管理 return 0; } -- 2.34.1