diff --git a/TC1/TC1.mk b/TC1/TC1.mk index cbe71a2..42d7982 100644 --- a/TC1/TC1.mk +++ b/TC1/TC1.mk @@ -32,7 +32,10 @@ $(NAME)_SOURCES := main.c\ user_sntp.c\ user_rtc.c\ user_mqtt_client.c\ + user_tftp_ota.c\ user_function.c $(NAME)_COMPONENTS := protocols/SNTP\ - protocols/mqtt \ No newline at end of file + protocols/mqtt\ + daemons/ota_server + \ No newline at end of file diff --git a/TC1/main.c b/TC1/main.c index 221d10b..c47dcfb 100644 --- a/TC1/main.c +++ b/TC1/main.c @@ -79,7 +79,7 @@ void appRestoreDefault_callback( void * const user_config_data, uint32_t size ) int application_start( void ) { int i; - os_log( "Start v0.1" ); + os_log( "Start %s",VERSION ); OSStatus err = kNoErr; diff --git a/TC1/main.h b/TC1/main.h index 5ac63b8..a3b12d4 100644 --- a/TC1/main.h +++ b/TC1/main.h @@ -4,6 +4,8 @@ #include "mico.h" #include "MiCOKit_EXT.h" +#define VERSION "v0.2" + #define TYPE 1 #define TYPE_NAME "zTC1" diff --git a/TC1/user_function.c b/TC1/user_function.c index cc01d98..1918cd0 100644 --- a/TC1/user_function.c +++ b/TC1/user_function.c @@ -134,11 +134,25 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata ) } cJSON_AddNumberToObject( json_send, "nvalue", p_nvalue->valueint ); } - + //解析版本 + cJSON *p_version = cJSON_GetObjectItem( pJsonRoot, "version" ); + if ( p_version ) + { + os_log("version:%s",VERSION); + cJSON_AddStringToObject( json_send, "version", VERSION ); + } //解析主机setting----------------------------------------------------------------- cJSON *p_setting = cJSON_GetObjectItem( pJsonRoot, "setting" ); if ( p_setting ) { + //解析ota + cJSON *p_ota = cJSON_GetObjectItem( p_setting, "ota" ); + if ( p_ota ) + { + if ( cJSON_IsString( p_ota ) ) + user_ota_start( p_ota->valuestring, NULL ); + } + cJSON *json_setting_send = cJSON_CreateObject( ); //设置设备名称/deviceid cJSON *p_setting_name = cJSON_GetObjectItem( p_setting, "name" ); @@ -330,7 +344,7 @@ bool json_plug_analysis( int udp_flag, char x, cJSON * pJsonRoot, cJSON * pJsonS } cJSON *p_nvalue = cJSON_GetObjectItem( pJsonRoot, "nvalue" ); // if ( p_plug || p_nvalue ) - cJSON_AddNumberToObject( json_plug_send, "on", user_config->plug[x].on ); + cJSON_AddNumberToObject( json_plug_send, "on", user_config->plug[x].on ); cJSON_AddItemToObject( pJsonSend, plug_str, json_plug_send ); return return_flag; diff --git a/TC1/user_function.h b/TC1/user_function.h index 04693f1..12c287b 100644 --- a/TC1/user_function.h +++ b/TC1/user_function.h @@ -6,6 +6,7 @@ #include "mico.h" #include "MiCOKit_EXT.h" +void user_send( int udp_flag, char *s ); void user_function_cmd_received(int udp_flag,uint8_t *pusrdata); unsigned char strtohex(char a, char b); diff --git a/TC1/user_gpio.c b/TC1/user_gpio.c index 259ba74..f023f4c 100644 --- a/TC1/user_gpio.c +++ b/TC1/user_gpio.c @@ -5,6 +5,7 @@ #include "user_mqtt_client.h" #include "user_udp.h" #include "cJSON/cJSON.h" +#include "user_tftp_ota.h" mico_gpio_t relay[Relay_NUM] = { Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5 }; diff --git a/TC1/user_mqtt_client.c b/TC1/user_mqtt_client.c index 0044dd9..f97bb27 100644 --- a/TC1/user_mqtt_client.c +++ b/TC1/user_mqtt_client.c @@ -303,7 +303,7 @@ void mqtt_client_thread( mico_thread_arg_t arg ) { sprintf( buf1, - "{\"mac\":\"%s\",\"plug_0\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_1\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_2\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_3\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_4\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_5\":{\"on\":null,\"setting\":{\"name\":null}}}", + "{\"mac\":\"%s\",\"version\":null,\"plug_0\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_1\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_2\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_3\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_4\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_5\":{\"on\":null,\"setting\":{\"name\":null}}}", strMac ); user_function_cmd_received( 0, buf1 ); diff --git a/TC1/user_tftp_ota.c b/TC1/user_tftp_ota.c new file mode 100644 index 0000000..bdbd49e --- /dev/null +++ b/TC1/user_tftp_ota.c @@ -0,0 +1,40 @@ +#define os_log(format, ...) custom_log("OTA", format, ##__VA_ARGS__) + +#include "mico.h" +#include "ota_server.h" +#include "main.h" +#include "user_udp.h" +#include "user_mqtt_client.h" +#include "user_function.h" + +static void ota_server_status_handler( OTA_STATE_E state, float progress ) +{ + char str[64]={0}; + switch ( state ) + { + case OTA_LOADING: + os_log("ota server is loading, progress %.2f%%", progress); + break; + case OTA_SUCCE: + os_log("ota server daemons success"); + sprintf( str, "{\"mac\":\"%s\",\"ota_progress\":100}", strMac ); + break; + case OTA_FAIL: + os_log("ota server daemons failed"); + sprintf( str, "{\"mac\":\"%s\",\"ota_progress\":-1}", strMac ); + break; + default: + break; + } + if(str[0]>0) + { + user_send(true,str); + } +} + +void user_ota_start( char *url, char *md5 ) +{ + os_log("ready to ota:%s",url); + ota_server_start( url, md5, ota_server_status_handler ); +} + diff --git a/TC1/user_tftp_ota.h b/TC1/user_tftp_ota.h new file mode 100644 index 0000000..5d7f8b7 --- /dev/null +++ b/TC1/user_tftp_ota.h @@ -0,0 +1,7 @@ +#ifndef __USER_TFTP_OTA_H_ +#define __USER_TFTP_OTA_H_ + + +void user_ota_start(char *url, char *md5); + +#endif diff --git a/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.all.bin b/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.all.bin new file mode 100644 index 0000000..00fb63b Binary files /dev/null and b/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.all.bin differ diff --git a/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.ota.bin b/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.ota.bin new file mode 100644 index 0000000..ba3fe0e Binary files /dev/null and b/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.ota.bin differ