From 6813c7c8b697421a720d705b2ebfed6b724306f8 Mon Sep 17 00:00:00 2001 From: kyz Date: Thu, 15 Jun 2023 14:50:54 +0800 Subject: [PATCH] json file read/write --- app/CMakeLists.txt | 1 + app/src/device_config.c | 74 ++++++++++ app/src/device_wifi.c | 119 +++++++++++++++- app/src/iot.c | 7 +- app/src/iot_key.c | 39 +----- app/src/main.c | 3 + app/src/rk_ble_app.c | 291 ++++++++++++++++++++++++++++++++++++++++ app/src/rk_ble_app.h | 18 +++ 8 files changed, 516 insertions(+), 36 deletions(-) create mode 100644 app/src/rk_ble_app.c create mode 100644 app/src/rk_ble_app.h diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 5cc8c8b..65dee6f 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -61,6 +61,7 @@ target_link_libraries( easymedia stasteria rkaiq + DeviceIo pthread ) install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION /app) diff --git a/app/src/device_config.c b/app/src/device_config.c index 1f07c1c..ed9291a 100644 --- a/app/src/device_config.c +++ b/app/src/device_config.c @@ -613,3 +613,77 @@ err_out: return -1; } + +cJSON * load_json_data_from_file( char *path ) +{ + FILE *fd; + char *json_data; + cJSON *root = NULL; + size_t file_size; + + fd = fopen( path, "r" ); + if( fd ){ + fseek(fd,0,SEEK_END); + file_size = ftell(fd); + fseek(fd,0,SEEK_SET); + json_data = (char *)malloc( file_size+1 ); + if( json_data ){ + if(fread( json_data, file_size, 1, fd ) ){ + json_data[ file_size ] = 0; + root = cJSON_Parse((const char *)json_data); + if( !root ){ + printf( "parser json data fail!\r\n" ); + } + }else{ + MG_LOGE( "read file[%s] fail!\n", path ); + } + free( json_data ); + }else{ + MG_LOGE( "out of memory!\n"); + } + fclose( fd ); + }else{ + MG_LOGE( "open file [%s] fail\n", path ); + } + + return root; +} + +int32_t wifi_config_store( char *ssid, char *passwd ) +{ + FILE *fd; + int32_t ret = -1; + + if( ssid && passwd ){ + if( (fd = fopen( "/customer/wifi.cfg", "w" ) )){ + fprintf( fd, "{\n\t\"ssid\":\"%s\",\n\t\"password\":\"%s\"\n}", ssid, passwd ); + fclose( fd ); + ret = 0; + } + } + return ret; +} + +int32_t wifi_config_load( char *ssid, uint32_t ssid_max_len, char *passwd, uint32_t passwd_max_len ) +{ + cJSON *root; + cJSON *json_ssid; + cJSON *json_passwd; + int32_t ret = -1; + + root = load_json_data_from_file( "/customer/wifi.cfg" ); + 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); + ret = 0; + } + + cJSON_Delete( root ); + } + + return ret; +} + diff --git a/app/src/device_wifi.c b/app/src/device_wifi.c index ba0378a..0b83424 100644 --- a/app/src/device_wifi.c +++ b/app/src/device_wifi.c @@ -10,8 +10,59 @@ #include "cQueue.h" #include "device.h" +#include "rk_ble_app.h" + +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 wifi_config_start( void ) +{ + wifi_config_start_flag = 1; + return 0; +} + +int32_t wifi_config_stop( void ) +{ + wifi_config_start_flag = 0; + return 0; +} + +void wifi_config_success( char * ssid, char *password ) +{ + char sys_cmd[100]; + + wifi_config_st = 1; + +//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 ); +} + +void wifi_config_fail( void ) +{ + wifi_config_st = 2; +} + void device_wifi_init( void ) { MG_LOGI( "wifi init" ); @@ -19,12 +70,76 @@ void device_wifi_init( void ) void * device_wifi_task( void* unused ) { + int32_t ble_start = 0; + + time_t ble_start_time; + char ssid[50]; + char passwd[50]; + + 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){ - MG_LOGD( "wifi task ..." ); - sleep(10); + 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 ); + } + } } } +//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 收到退出指令后退出。 + 超时退出。 +*/ + void device_wifi_start( void ) { if( pthread_create( &th, NULL, device_wifi_task, (void *)NULL ) != 0 ){ diff --git a/app/src/iot.c b/app/src/iot.c index e917a47..9b9e132 100644 --- a/app/src/iot.c +++ b/app/src/iot.c @@ -102,8 +102,11 @@ void* iot_task( void * unused ) iot_dp_init(); //to create queue; tuya_mqtt_context_t* client = &client_instance; - - iot_key_load( &productId, &deviceId, &deviceSecret ); + + while( iot_key_load( &productId, &deviceId, &deviceSecret ) != 0){ + MG_LOGE( "iot_task: load iot key fail!" ); + sleep(5); + } ret = tuya_mqtt_init(client, &(const tuya_mqtt_config_t) { .host = "m1.tuyacn.com", diff --git a/app/src/iot_key.c b/app/src/iot_key.c index 297121e..4f41d8b 100644 --- a/app/src/iot_key.c +++ b/app/src/iot_key.c @@ -18,51 +18,38 @@ static char deviceID[50]; static char deviceSecret[50]; +cJSON * load_json_data_from_file( char *path ); int32_t iot_key_load( char ** out_productID, char ** out_deviceID, char ** out_deviceSecret ) { - FILE *fd; - char json_data[500]; - size_t n; cJSON *root; cJSON *di; cJSON *pi; cJSON *ds; + printf( "iot load key...\r\n" ); - //1. open file - fd = fopen( IOT_KEY_PATH, "r" ); - if( !fd ){ - printf( "open auty key file[%s] fail!\r\n", IOT_KEY_PATH ); - goto err_out; - } - //2. read json data - n = fread( json_data, 1, sizeof(json_data)-1, fd ); - if( n == 0 ){ - printf( "read auty key file fail!\r\n" ); - goto err_read; - } - json_data[n] = 0; - printf( "get json data[%ld]:%s\r\n", n, json_data ); - //3. parse json - root = cJSON_Parse((const char *)json_data); + root = load_json_data_from_file( IOT_KEY_PATH ); if( !root ){ - printf( "parser json data fail!\r\n" ); + printf( "load json data from [%s] fail!\r\n", IOT_KEY_PATH ); goto err_json; } di =cJSON_GetObjectItem(root, "deviceID"); pi =cJSON_GetObjectItem(root, "productID"); ds =cJSON_GetObjectItem(root, "deviceSecret"); + if( !di){ printf( "get devieID:fail\r\n" ); goto err_json; } printf( "get devieID:%s\r\n", di->valuestring ); + if( !pi){ printf( "get productID:fail\r\n" ); goto err_json; } printf( "get productID:%s\r\n", pi->valuestring ); + if( !ds){ printf( "get device secret:fail\r\n" ); goto err_json; @@ -87,18 +74,6 @@ err_json: if( root ){ cJSON_Delete( root ); } - -err_read: - if( fd ) - fclose(fd); -err_out: - if( out_productID ) - *out_productID = (char*)def_productID; - if( out_deviceID ) - *out_deviceID = (char*)def_deviceID; - if( out_deviceSecret ) - *out_deviceSecret = (char*)def_deviceSecret; return -1; - } diff --git a/app/src/main.c b/app/src/main.c index 2e68fe8..7e4cecd 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -17,6 +17,8 @@ #include "device.h" #include "version.h" +#include "rk_ble_app.h" + #define PID_FILE "/var/run/mg_app/mg_app.pid" extern void device_wifi_start( void ); @@ -219,6 +221,7 @@ int main( void ) MG_LOGE( "start gesture task fail!\n" ); } + rk_ble_wifi_init( NULL ); while(1){ sleep(5*1000); } diff --git a/app/src/rk_ble_app.c b/app/src/rk_ble_app.c new file mode 100644 index 0000000..a0ba535 --- /dev/null +++ b/app/src/rk_ble_app.c @@ -0,0 +1,291 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "rk_ble_app.h" + +/* Immediate wifi Service UUID */ +#define BLE_UUID_SERVICE "0000180A-0000-1000-8000-00805F9B34FB" +#define BLE_UUID_WIFI_CHAR "00009999-0000-1000-8000-00805F9B34FB" +#define BLE_UUID_PROXIMITY "7B931104-1810-4CBC-94DA-875C8067F845" + +#define UUID_MAX_LEN 36 + +typedef enum { + RK_BLE_WIFI_State_IDLE = 0, + RK_BLE_WIFI_State_CONNECTTING, + RK_BLE_WIFI_State_SUCCESS, + RK_BLE_WIFI_State_FAIL, + RK_BLE_WIFI_State_WRONGKEY_FAIL, + RK_BLE_WIFI_State_DISCONNECT +} RK_BLE_WIFI_State_e; + +const char *MSG_BLE_WIFI_LIST_FORMAT = "{\"cmd\":\"wifilists\", \"ret\":%s}"; + +static unsigned char rk_wifi_list_buf[20 * 1024]; +static int scanr_len = 0, scanr_len_use = 0; + +static pthread_t wificonfig_tid = 0; +static pthread_t wificonfig_scan_tid = 0; + +static char wifi_ssid[256]; +static char wifi_password[256]; + +static unsigned int ble_mtu = 0; + +struct wifi_config { + char ssid[512]; + int ssid_len; + char psk[512]; + int psk_len; + char key_mgmt[512]; + int key_len; + bool hide; + void (*wifi_status_callback)(int status, int reason); +}; + +static struct wifi_config wifi_cfg; +typedef int (*RK_blewifi_state_callback)(RK_BLE_WIFI_State_e state); + +#define RK_BLE_DATA_CMD_LEN 12 +#define RK_BLE_DATA_SSID_LEN 32 +#define RK_BLE_DATA_PSK_LEN 32 + +/* 消息开头必须为0x01,结尾必须为0x04 */ +typedef struct { + unsigned char start; // 段, 01Byte, 固定为 0x01 + char cmd[RK_BLE_DATA_CMD_LEN]; // 段, 12Byte, 值为:"wifisetup"、"wifilists" + char ssid[RK_BLE_DATA_SSID_LEN]; // 段, 32Byte, 需要连接的 WiFi ssid, 长度不足 32 的部分填充 0 + char psk[RK_BLE_DATA_PSK_LEN]; // 段, 32Byte, 需要连接的 wifi password, 长度不足 32 的部分填充 0 + //unsigned char end; // 段, 01Byte, 固定位 0x04 +} RockChipBleData; + +typedef struct { + uint8_t data[6]; +} mac_t; + +typedef struct { + uint8_t data[16]; +} uuid128_t; + +void _rk_ble_status_cb(const char *bd_addr, const char *name, RK_BLE_STATE state) +{ + switch (state) { + case RK_BLE_STATE_IDLE: + printf("[RK] ble status: RK_BLE_STATE_IDLE\n"); + break; + case RK_BLE_STATE_CONNECT: + printf("[RK] ble status: RK_BLE_STATE_CONNECT\n"); + break; + case RK_BLE_STATE_DISCONNECT: + printf("[RK] ble status: RK_BLE_STATE_DISCONNECT\n"); + ble_mtu = 0; + break; + } +} + +static void _rk_ble_mtu_callback(const char *bd_addr, unsigned int mtu) +{ + printf("=== %s: bd_addr: %s, mtu: %d ===\n", __func__, bd_addr, mtu); + ble_mtu = mtu; +} + +void wifi_config_fail( void ); +void wifi_config_success( char * ssid, char *password ); + +static int rk_blewifi_state_callback(RK_WIFI_RUNNING_State_e state, RK_WIFI_INFO_Connection_s *info) +{ + uint8_t ret = 0; + + printf("[RK] %s state: %d\n", __func__, state); + switch(state) { + case RK_WIFI_State_CONNECTED: + ret = 0x1; + +wifi_config_success( wifi_cfg.ssid, wifi_cfg.psk ); + + break; + case RK_WIFI_State_CONNECTFAILED: + case RK_WIFI_State_CONNECTFAILED_WRONG_KEY: + ret = 0x2; +wifi_config_fail(); + break; + } + + if(ret) + rk_ble_write(BLE_UUID_WIFI_CHAR, &ret, 0x1); + + return 0; +} + +static void *rk_config_wifi_thread(void) +{ + printf("[RK] rk_config_wifi_thread\n"); + + prctl(PR_SET_NAME,"rk_config_wifi_thread"); + + RK_wifi_register_callback(rk_blewifi_state_callback); + RK_wifi_enable(0); + RK_wifi_enable(1); + RK_wifi_connect(wifi_cfg.ssid, wifi_cfg.psk); + return NULL; +} + +static void rk_ble_send_data(void) +{ + int len, send_max_len = BT_ATT_DEFAULT_LE_MTU; + uint8_t *data; + + if (scanr_len == 0) { + scanr_len_use = 0; + printf("[RK] NO WIFI SCAN_R OR READ END!!!\n"); + return; + } + + prctl(PR_SET_NAME,"rk_ble_send_data"); + + if(ble_mtu > BT_ATT_HEADER_LEN) + send_max_len = ble_mtu; + + send_max_len -= BT_ATT_HEADER_LEN; + if(send_max_len > BT_ATT_MAX_VALUE_LEN) + send_max_len = BT_ATT_MAX_VALUE_LEN; + + while (scanr_len) { + printf("[RK] %s: wifi use: %d, remain len: %d\n", __func__, scanr_len_use, scanr_len); + len = (scanr_len > send_max_len) ? send_max_len : scanr_len; + data = rk_wifi_list_buf + scanr_len_use; + usleep(100000); + rk_ble_write(BLE_UUID_WIFI_CHAR, data, len); + scanr_len -= len; + scanr_len_use += len; + } +} + +void _rk_ble_recv_data_cb(const char *uuid, char *data, int len) +{ + if (!strcmp(uuid, BLE_UUID_WIFI_CHAR)) { + uint8_t str[512]; + RockChipBleData *ble_data; + char *wifilist; + unsigned char end_flag = 0; + + memset(str, 0, 512); + memcpy(str, data, len); + str[len] = '\0'; + end_flag = str[len - 1]; + + printf("[RK] chr_write_value: %p, %d, data: %s\n", data, len, data); + + for (int i = 0; i < len; i++) { + if (!( i % 8)) + printf("\n"); + printf("0x%02x ", str[i]); + } + printf("\n"); + + ble_data = (RockChipBleData *)str; + + /* Msg is valid? */ + printf("[RK] ble_data.cmd: %s, ble_data.start: %d, ble_data.end: %d\n", + ble_data->cmd, ble_data->start, end_flag); + if ((ble_data->start != 0x1) || (end_flag != 0x4)) { + printf("[RK] BLE RECV DATA ERROR !!!\n"); + return; + } + + if (strncmp(ble_data->cmd, "wifilists", 9) == 0) { +scan_retry: + printf("[RK] RK_wifi_scan ...\n"); + RK_wifi_scan(); + usleep(800000); + + printf("[RK] RK_wifi_scan_r_sec ...\n"); + wifilist = RK_wifi_scan_r_sec(0x14); + printf("[RK] RK_wifi_scan_r_sec end wifilist: %p\n", wifilist); + + if (wifilist == NULL) + goto scan_retry; + if (wifilist && (strlen(wifilist) < 3)) { + free(wifilist); + goto scan_retry; + } + + memset(rk_wifi_list_buf, 0, sizeof(rk_wifi_list_buf)); + snprintf(rk_wifi_list_buf, sizeof(rk_wifi_list_buf), MSG_BLE_WIFI_LIST_FORMAT, wifilist); + scanr_len = strlen(rk_wifi_list_buf); + scanr_len_use = 0; + printf("[RK] wifi scan_r: %s, len: %d\n", rk_wifi_list_buf, scanr_len); + + free(wifilist); + pthread_create(&wificonfig_scan_tid, NULL, rk_ble_send_data, NULL); + } 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); + + printf("[RK] wifi ssid is %s\n", wifi_ssid); + printf("[RK] wifi psk is %s\n", wifi_password); + + strcpy(wifi_cfg.ssid, wifi_ssid); + strcpy(wifi_cfg.psk, wifi_password); + //wifi_cfg.wifi_status_callback = wifi_status_callback; + pthread_create(&wificonfig_tid, NULL, rk_config_wifi_thread, NULL); + } + } +} + +void _rk_ble_request_data_cb(const char *uuid) +{ + if (!strcmp(uuid, BLE_UUID_WIFI_CHAR)) { + printf("Call ble wifi request callback!\n"); + } +} + +void rk_ble_wifi_init(void *data) +{ + RkBtContent bt_content; + + printf("===== %s =====\n", __func__); + + //MUST TO SET 0 + memset(&bt_content, 0, sizeof(RkBtContent)); + bt_content.bt_name = strdup("RockChip"); + bt_content.ble_content.ble_name = strdup("RockChipBle"); + bt_content.ble_content.server_uuid.uuid = BLE_UUID_SERVICE; + bt_content.ble_content.server_uuid.len = UUID_128; + bt_content.ble_content.chr_uuid[0].uuid = BLE_UUID_WIFI_CHAR; + bt_content.ble_content.chr_uuid[0].len = UUID_128; + bt_content.ble_content.chr_cnt = 1; + bt_content.ble_content.cb_ble_recv_fun = _rk_ble_recv_data_cb; + bt_content.ble_content.cb_ble_request_data = _rk_ble_request_data_cb; + bt_content.ble_content.advDataType = BLE_ADVDATA_TYPE_SYSTEM; + + //bsa ble must register data recv callback, can't delete + rk_ble_register_recv_callback(_rk_ble_recv_data_cb); + rk_ble_register_status_callback(_rk_ble_status_cb); + rk_bt_init(&bt_content); + + sleep(3); + printf(">>>>> Start ble ....\n"); + rk_ble_register_mtu_callback(_rk_ble_mtu_callback); + rk_ble_start(&bt_content.ble_content); +} + +void rk_ble_wifi_deinit(void *data) +{ + ble_mtu = 0; + rk_ble_stop(); + sleep(3); + rk_bt_deinit(); +} diff --git a/app/src/rk_ble_app.h b/app/src/rk_ble_app.h new file mode 100644 index 0000000..fdd77c9 --- /dev/null +++ b/app/src/rk_ble_app.h @@ -0,0 +1,18 @@ +#ifndef __RK_BLE_APP_H__ +#define __RK_BLE_APP_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void rk_ble_wifi_init(void *data); +void rk_ble_wifi_deinit(void *data); + +#ifdef __cplusplus +} +#endif + +#endif /* __RK_BLE_APP_H__ */ -- 2.34.1