mirror of
https://github.com/oopuuu/zTC1.git
synced 2025-12-12 21:18:13 +08:00
fix:字符串长度不足导致的数据异常及mac地址异常
fix:mac地址错误(为ap设备mac地址)的问题 add:mqtt配置/idx配置/device name配置
This commit is contained in:
33
TC1/main.c
33
TC1/main.c
@@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
char rtc_init = 0; //sntpУʱ<D0A3>ɹ<EFBFBD><C9B9><EFBFBD>־λ
|
char rtc_init = 0; //sntpУʱ<D0A3>ɹ<EFBFBD><C9B9><EFBFBD>־λ
|
||||||
|
|
||||||
char strMac[12] = { 0 };
|
char strMac[16] = { 0 };
|
||||||
|
|
||||||
system_config_t * sys_config;
|
system_config_t * sys_config;
|
||||||
user_config_t * user_config;
|
user_config_t * user_config;
|
||||||
@@ -78,6 +78,7 @@ int application_start( void )
|
|||||||
err = mico_system_init( sys_config );
|
err = mico_system_init( sys_config );
|
||||||
require_noerr( err, exit );
|
require_noerr( err, exit );
|
||||||
|
|
||||||
|
|
||||||
for ( i = 0; i < Relay_NUM; i++ )
|
for ( i = 0; i < Relay_NUM; i++ )
|
||||||
{
|
{
|
||||||
MicoGpioInitialize( Relay[i], OUTPUT_PUSH_PULL );
|
MicoGpioInitialize( Relay[i], OUTPUT_PUSH_PULL );
|
||||||
@@ -100,20 +101,26 @@ int application_start( void )
|
|||||||
err = mico_system_context_restore( sys_config );
|
err = mico_system_context_restore( sys_config );
|
||||||
require_noerr( err, exit );
|
require_noerr( err, exit );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
os_log( "mqtt_ip:%s",user_config->mqtt_ip );
|
||||||
|
os_log( "mqtt_port:%d",user_config->mqtt_port );
|
||||||
|
os_log( "mqtt_user:%s",user_config->mqtt_user );
|
||||||
|
os_log( "mqtt_password:%s",user_config->mqtt_password );
|
||||||
|
|
||||||
os_log( "version:%d",user_config->version );
|
os_log( "version:%d",user_config->version );
|
||||||
os_log( "idx:%d",user_config->idx );
|
os_log( "idx:%d",user_config->idx );
|
||||||
for ( i = 0; i < PLUG_NUM; i++ )
|
// for ( i = 0; i < PLUG_NUM; i++ )
|
||||||
{
|
// {
|
||||||
os_log("plug_%d:",i);
|
// os_log("plug_%d:",i);
|
||||||
os_log("\tname:%s:",user_config->plug[i].name);
|
// os_log("\tname:%s:",user_config->plug[i].name);
|
||||||
os_log("\tidx:%d:",user_config->plug[i].idx);
|
// os_log("\tidx:%d:",user_config->plug[i].idx);
|
||||||
for ( j = 0; j < PLUG_TIME_TASK_NUM; j++ )
|
// for ( j = 0; j < PLUG_TIME_TASK_NUM; j++ )
|
||||||
{
|
// {
|
||||||
os_log("\t\ton:%d\t %02d:%02d repeat:0x%X",user_config->plug[i].task[j].on,
|
// os_log("\t\ton:%d\t %02d:%02d repeat:0x%X",user_config->plug[i].task[j].on,
|
||||||
user_config->plug[i].task[j].hour,user_config->plug[i].task[j].minute,
|
// user_config->plug[i].task[j].hour,user_config->plug[i].task[j].minute,
|
||||||
user_config->plug[i].task[j].repeat);
|
// user_config->plug[i].task[j].repeat);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
wifi_init( );
|
wifi_init( );
|
||||||
key_init( );
|
key_init( );
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
#define ZTC1_NAME "zTC1_%02X%02X"
|
#define ZTC1_NAME "zTC1_%02X%02X"
|
||||||
|
|
||||||
#define USER_CONFIG_VERSION 1
|
#define USER_CONFIG_VERSION 2
|
||||||
#define SETTING_MQTT_STRING_LENGTH_MAX 32 //<2F><><EFBFBD><EFBFBD> 4 <20>ֽڶ<D6BD><DAB6>롣
|
#define SETTING_MQTT_STRING_LENGTH_MAX 32 //<2F><><EFBFBD><EFBFBD> 4 <20>ֽڶ<D6BD><DAB6>롣
|
||||||
|
|
||||||
#define PLUG_NAME_LENGTH 32
|
#define PLUG_NAME_LENGTH 32
|
||||||
@@ -64,7 +64,7 @@ typedef struct
|
|||||||
|
|
||||||
extern char rtc_init;
|
extern char rtc_init;
|
||||||
|
|
||||||
extern char strMac[12];
|
extern char strMac[16];
|
||||||
extern system_config_t * sys_config;
|
extern system_config_t * sys_config;
|
||||||
extern user_config_t * user_config;
|
extern user_config_t * user_config;
|
||||||
|
|
||||||
|
|||||||
@@ -17,13 +17,13 @@ void user_send( int udp_flag, uint8_t *s )
|
|||||||
if ( udp_flag )
|
if ( udp_flag )
|
||||||
user_udp_send( s ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
user_udp_send( s ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
else
|
else
|
||||||
user_mqtt_send( "domoticz/in", s );
|
user_mqtt_send( s );
|
||||||
}
|
}
|
||||||
|
|
||||||
void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
|
void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
|
||||||
{
|
{
|
||||||
OSStatus err = kNoErr;
|
OSStatus err = kNoErr;
|
||||||
char *out;
|
|
||||||
cJSON * pJsonRoot = cJSON_Parse( pusrdata );
|
cJSON * pJsonRoot = cJSON_Parse( pusrdata );
|
||||||
if ( !pJsonRoot )
|
if ( !pJsonRoot )
|
||||||
{
|
{
|
||||||
@@ -32,7 +32,6 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
|
|||||||
}
|
}
|
||||||
|
|
||||||
//<2F><><EFBFBD><EFBFBD>device report
|
//<2F><><EFBFBD><EFBFBD>device report
|
||||||
os_log( "start json:device report\r\n" );
|
|
||||||
cJSON *p_cmd = cJSON_GetObjectItem( pJsonRoot, "cmd" );
|
cJSON *p_cmd = cJSON_GetObjectItem( pJsonRoot, "cmd" );
|
||||||
if ( p_cmd && cJSON_IsString( p_cmd ) && strcmp( p_cmd->valuestring, "device report" ) == 0 )
|
if ( p_cmd && cJSON_IsString( p_cmd ) && strcmp( p_cmd->valuestring, "device report" ) == 0 )
|
||||||
{
|
{
|
||||||
@@ -51,23 +50,161 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
|
|||||||
// cJSON_Delete(p_cmd);
|
// cJSON_Delete(p_cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
//<2F><><EFBFBD><EFBFBD>idx<EFBFBD>ֶ<EFBFBD>int<EFBFBD><EFBFBD><EFBFBD><EFBFBD> description<6F>ֶ<EFBFBD>string<6E><67><EFBFBD><EFBFBD> name<6D>ֶ<EFBFBD>string<6E><67><EFBFBD><EFBFBD>
|
//<2F><><EFBFBD><EFBFBD>
|
||||||
cJSON *p_idx = cJSON_GetObjectItem( pJsonRoot, "idx" );
|
cJSON *p_idx = cJSON_GetObjectItem( pJsonRoot, "idx" );
|
||||||
cJSON *p_description = cJSON_GetObjectItem( pJsonRoot, "description" );
|
cJSON *p_description = cJSON_GetObjectItem( pJsonRoot, "description" );
|
||||||
cJSON *p_name = cJSON_GetObjectItem( pJsonRoot, "name" );
|
cJSON *p_name = cJSON_GetObjectItem( pJsonRoot, "name" );
|
||||||
|
cJSON *p_mac = cJSON_GetObjectItem( pJsonRoot, "mac" );
|
||||||
|
|
||||||
if ( p_idx && cJSON_IsNumber( p_idx ) && (p_idx->valueint >= 3 && p_idx->valueint <= 9) ) //idx
|
//
|
||||||
|
if ( (p_idx && cJSON_IsNumber( p_idx ) && p_idx->valueint == user_config->idx) //idx
|
||||||
|
|| (p_description && cJSON_IsString( p_description ) && strcmp( p_description->valuestring, sys_config->micoSystemConfig.name ) == 0) //p_description name
|
||||||
|
|| (p_name && cJSON_IsString( p_name ) && strcmp( p_name->valuestring, sys_config->micoSystemConfig.name ) == 0) //name
|
||||||
|
|| (p_mac && cJSON_IsString( p_mac ) && strcmp( p_mac->valuestring, strMac ) == 0) //mac
|
||||||
|
)
|
||||||
{
|
{
|
||||||
|
// os_log("device enter\r\n");
|
||||||
|
cJSON *json_send = cJSON_CreateObject( );
|
||||||
|
cJSON_AddStringToObject( json_send, "mac", strMac );
|
||||||
|
|
||||||
cJSON *p_nvalue = cJSON_GetObjectItem( pJsonRoot, "nvalue" );
|
cJSON *p_nvalue = cJSON_GetObjectItem( pJsonRoot, "nvalue" );
|
||||||
if ( p_nvalue )
|
// if ( p_nvalue && cJSON_IsNumber( p_nvalue ) )
|
||||||
|
// {
|
||||||
|
//// uint32 now_time = system_get_time( );
|
||||||
|
//// os_log( "system_get_time:%d,%d = %09d\r\n", last_time, now_time, now_time - last_time );
|
||||||
|
//// if ( now_time - last_time < 1500000 && p_idx && p_nvalue->valueint == user_rudder_get_direction( ) )
|
||||||
|
//// {
|
||||||
|
//// return_flag = false;
|
||||||
|
//// } else
|
||||||
|
//// {
|
||||||
|
//// user_rudder_press( p_nvalue->valueint );
|
||||||
|
//// }
|
||||||
|
//// user_json_set_last_time( );
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if ( p_nvalue )
|
||||||
|
// {
|
||||||
|
// cJSON_AddNumberToObject( json_send, "nvalue", user_rudder_get_direction( ) );
|
||||||
|
// } else
|
||||||
|
// last_time = 0;
|
||||||
|
|
||||||
|
cJSON *p_setting = cJSON_GetObjectItem( pJsonRoot, "setting" );
|
||||||
|
if ( p_setting )
|
||||||
{
|
{
|
||||||
user_led_set( p_nvalue->valueint );
|
cJSON *json_setting_send = cJSON_CreateObject( );
|
||||||
user_relay_set( p_idx->valueint - 3, p_nvalue->valueint );
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD>/deviceid
|
||||||
|
cJSON *p_setting_name = cJSON_GetObjectItem( p_setting, "name" );
|
||||||
|
if ( p_setting_name && cJSON_IsString( p_setting_name ) )
|
||||||
|
{
|
||||||
|
sprintf( sys_config->micoSystemConfig.name, p_setting_name->valuestring );
|
||||||
|
}
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>mqtt ip
|
||||||
|
cJSON *p_mqtt_ip = cJSON_GetObjectItem( p_setting, "mqtt_uri" );
|
||||||
|
if ( p_mqtt_ip && cJSON_IsString( p_mqtt_ip ) )
|
||||||
|
{
|
||||||
|
sprintf( user_config->mqtt_ip, p_mqtt_ip->valuestring );
|
||||||
|
}
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>mqtt port
|
||||||
|
cJSON *p_mqtt_port = cJSON_GetObjectItem( p_setting, "mqtt_port" );
|
||||||
|
if ( p_mqtt_port && cJSON_IsNumber( p_mqtt_port ) )
|
||||||
|
{
|
||||||
|
user_config->mqtt_port = p_mqtt_port->valueint;
|
||||||
|
}
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>mqtt user
|
||||||
|
cJSON *p_mqtt_user = cJSON_GetObjectItem( p_setting, "mqtt_user" );
|
||||||
|
if ( p_mqtt_user && cJSON_IsString( p_mqtt_user ) )
|
||||||
|
{
|
||||||
|
sprintf( user_config->mqtt_user, p_mqtt_user->valuestring );
|
||||||
|
}
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>mqtt password
|
||||||
|
cJSON *p_mqtt_password = cJSON_GetObjectItem( p_setting, "mqtt_password" );
|
||||||
|
if ( p_mqtt_password && cJSON_IsString( p_mqtt_password ) )
|
||||||
|
{
|
||||||
|
sprintf( user_config->mqtt_password, p_mqtt_password->valuestring );
|
||||||
|
}
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>domoticz idx
|
||||||
|
cJSON *p_setting_idx = cJSON_GetObjectItem( p_setting, "idx" );
|
||||||
|
if ( p_setting_idx && cJSON_IsNumber( p_setting_idx ) )
|
||||||
|
{
|
||||||
|
user_config->idx = p_setting_idx->valueint;
|
||||||
|
os_log( "idx:%d",user_config->idx );
|
||||||
|
mico_system_context_update( sys_config );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( p_setting_name || p_mqtt_ip || p_mqtt_port || p_mqtt_user || p_mqtt_password || p_setting_idx )
|
||||||
|
{
|
||||||
|
os_log( "mico_system_context_update" );
|
||||||
|
err = mico_system_context_update( sys_config );
|
||||||
|
require_noerr( err, exit );
|
||||||
|
}
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD>/deviceid
|
||||||
|
if ( p_setting_name )
|
||||||
|
{
|
||||||
|
cJSON_AddStringToObject( json_setting_send, "name", sys_config->micoSystemConfig.name );
|
||||||
|
}
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>mqtt ip
|
||||||
|
if ( p_mqtt_ip )
|
||||||
|
{
|
||||||
|
cJSON_AddStringToObject( json_setting_send, "mqtt_uri", user_config->mqtt_ip );
|
||||||
|
}
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>mqtt port
|
||||||
|
if ( p_mqtt_port )
|
||||||
|
{
|
||||||
|
cJSON_AddNumberToObject( json_setting_send, "mqtt_port", user_config->mqtt_port );
|
||||||
|
}
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>mqtt user
|
||||||
|
if ( p_mqtt_user )
|
||||||
|
{
|
||||||
|
cJSON_AddStringToObject( json_setting_send, "mqtt_user", user_config->mqtt_user );
|
||||||
|
}
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>mqtt password
|
||||||
|
if ( p_mqtt_password )
|
||||||
|
{
|
||||||
|
cJSON_AddStringToObject( json_setting_send, "mqtt_password", user_config->mqtt_password );
|
||||||
|
}
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>domoticz idx
|
||||||
|
if ( p_setting_idx )
|
||||||
|
{
|
||||||
|
cJSON_AddNumberToObject( json_setting_send, "idx", user_config->idx );
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON_AddItemToObject( json_send, "setting", json_setting_send );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cJSON_AddStringToObject( json_send, "name", sys_config->micoSystemConfig.name );
|
||||||
|
|
||||||
|
//if (p_idx)
|
||||||
|
if ( user_config->idx >= 0 )
|
||||||
|
cJSON_AddNumberToObject( json_send, "idx", user_config->idx );
|
||||||
|
|
||||||
|
//if ( return_flag == true )
|
||||||
|
{
|
||||||
|
char *json_str = cJSON_Print( json_send );
|
||||||
|
os_log( "pRoot: %s\r\n", json_str );
|
||||||
|
user_send( udp_flag, json_str ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
free( (void *) json_str );
|
||||||
|
}
|
||||||
|
cJSON_Delete( json_send );
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON_Delete( pJsonRoot );
|
cJSON_Delete( pJsonRoot );
|
||||||
free( out );
|
return;
|
||||||
|
exit:
|
||||||
|
os_log("user_function_cmd_received ERROR:0x%x",err);
|
||||||
|
|
||||||
|
cJSON_Delete( pJsonRoot );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ void wifi_start_easylink( )
|
|||||||
micoWlanStartEasyLink( 20000 );
|
micoWlanStartEasyLink( 20000 );
|
||||||
user_led_set( 1 );
|
user_led_set( 1 );
|
||||||
}
|
}
|
||||||
uint32_t ip=0xd248912c;
|
uint32_t ip = 0xd248912c;
|
||||||
//easylink <20><><EFBFBD>ɻص<C9BB>
|
//easylink <20><><EFBFBD>ɻص<C9BB>
|
||||||
void wifi_easylink_completed_handle( network_InitTypeDef_st *nwkpara, void * arg )
|
void wifi_easylink_completed_handle( network_InitTypeDef_st *nwkpara, void * arg )
|
||||||
{
|
{
|
||||||
@@ -67,9 +67,8 @@ void wifi_easylink_completed_handle( network_InitTypeDef_st *nwkpara, void * arg
|
|||||||
//wifi<66><69><EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD>ȡ<EFBFBD><C8A1>IP<49><50>ַ <20>ص<EFBFBD>
|
//wifi<66><69><EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD>ȡ<EFBFBD><C8A1>IP<49><50>ַ <20>ص<EFBFBD>
|
||||||
static void wifi_get_ip_callback( IPStatusTypedef *pnet, void * arg )
|
static void wifi_get_ip_callback( IPStatusTypedef *pnet, void * arg )
|
||||||
{
|
{
|
||||||
os_log("got IP:%s, MAC:%s", pnet->ip,pnet->mac);
|
os_log("got IP:%s", pnet->ip);
|
||||||
wifi_status = WIFI_STATE_CONNECTED;
|
wifi_status = WIFI_STATE_CONNECTED;
|
||||||
strcpy(strMac, pnet->mac);
|
|
||||||
}
|
}
|
||||||
//wifi<66><69><EFBFBD><EFBFBD>״̬<D7B4>ı<EFBFBD><C4B1>ص<EFBFBD>
|
//wifi<66><69><EFBFBD><EFBFBD>״̬<D7B4>ı<EFBFBD><C4B1>ص<EFBFBD>
|
||||||
static void wifi_status_callback( WiFiEvent status, void *arg )
|
static void wifi_status_callback( WiFiEvent status, void *arg )
|
||||||
@@ -101,10 +100,10 @@ static void wifi_led_timer_callback( void* arg )
|
|||||||
|
|
||||||
case WIFI_STATE_CONNECTING:
|
case WIFI_STATE_CONNECTING:
|
||||||
//if ( num > 1 )
|
//if ( num > 1 )
|
||||||
{
|
{
|
||||||
num = 0;
|
num = 0;
|
||||||
user_led_set( -1 );
|
user_led_set( -1 );
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case WIFI_STATE_NOEASYLINK:
|
case WIFI_STATE_NOEASYLINK:
|
||||||
wifi_start_easylink( );
|
wifi_start_easylink( );
|
||||||
@@ -144,5 +143,9 @@ void wifi_init( void )
|
|||||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>wifi<66><69><EFBFBD><EFBFBD>
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>wifi<66><69><EFBFBD><EFBFBD>
|
||||||
if ( !mico_rtos_is_timer_running( &wifi_led_timer ) ) mico_rtos_start_timer( &wifi_led_timer );
|
if ( !mico_rtos_is_timer_running( &wifi_led_timer ) ) mico_rtos_start_timer( &wifi_led_timer );
|
||||||
|
|
||||||
|
IPStatusTypedef para;
|
||||||
|
micoWlanGetIPStatus( ¶, Station );
|
||||||
|
strcpy( strMac, para.mac );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user