(*) -> ()

This commit is contained in:
zogodo
2019-09-24 21:44:51 +08:00
parent e7009528aa
commit 2a328b9225
19 changed files with 824 additions and 824 deletions

View File

@@ -73,10 +73,10 @@ static int http_get_socket_status(httpd_request_t *req)
char* status = get_socket_status(); char* status = get_socket_status();
err = httpd_send_all_header(req, HTTP_RES_200, strlen(status), HTTP_CONTENT_HTML_STR); err = httpd_send_all_header(req, HTTP_RES_200, strlen(status), HTTP_CONTENT_HTML_STR);
require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http socket_status headers.") ); require_noerr_action(err, exit, app_httpd_log("ERROR: Unable to send http socket_status headers."));
err = httpd_send_body(req->sock, socket_status, strlen(status)); err = httpd_send_body(req->sock, socket_status, strlen(status));
require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http socket_status body.") ); require_noerr_action(err, exit, app_httpd_log("ERROR: Unable to send http socket_status body."));
exit: exit:
return err; return err;
@@ -188,7 +188,7 @@ static int _app_httpd_start()
/*Initialize HTTPD*/ /*Initialize HTTPD*/
if(is_http_init == false) { if(is_http_init == false) {
err = httpd_init(); err = httpd_init();
require_noerr_action( err, exit, app_httpd_log("failed to initialize httpd") ); require_noerr_action(err, exit, app_httpd_log("failed to initialize httpd"));
is_http_init = true; is_http_init = true;
} }
@@ -202,12 +202,12 @@ exit:
return err; return err;
} }
int app_httpd_start( void ) int app_httpd_start(void)
{ {
OSStatus err = kNoErr; OSStatus err = kNoErr;
err = _app_httpd_start(); err = _app_httpd_start();
require_noerr( err, exit ); require_noerr(err, exit);
if (is_handlers_registered == false) { if (is_handlers_registered == false) {
app_http_register_handlers(); app_http_register_handlers();
@@ -225,7 +225,7 @@ int app_httpd_stop()
/* HTTPD and services */ /* HTTPD and services */
app_httpd_log("stopping down httpd"); app_httpd_log("stopping down httpd");
err = httpd_stop(); err = httpd_stop();
require_noerr_action( err, exit, app_httpd_log("failed to halt httpd") ); require_noerr_action(err, exit, app_httpd_log("failed to halt httpd"));
exit: exit:
return err; return err;

View File

@@ -31,7 +31,7 @@
****************************************************************************** ******************************************************************************
*/ */
int app_httpd_start( void ); int app_httpd_start(void);
int app_httpd_stop(); int app_httpd_stop();

View File

@@ -25,14 +25,14 @@ char socket_status[32] = { 0 };
mico_gpio_t Relay[Relay_NUM] = { Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5 }; mico_gpio_t Relay[Relay_NUM] = { Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5 };
/* MICO system callback: Restore default configuration provided by application */ /* MICO system callback: Restore default configuration provided by application */
void appRestoreDefault_callback( void * const user_config_data, uint32_t size ) void appRestoreDefault_callback(void * const user_config_data, uint32_t size)
{ {
int i, j; int i, j;
UNUSED_PARAMETER( size ); UNUSED_PARAMETER(size);
mico_system_context_get( )->micoSystemConfig.name[0] = 1; //<2F><><EFBFBD>´<EFBFBD><C2B4><EFBFBD><EFBFBD><EFBFBD>ʱʹ<CAB1><CAB9>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> mico_system_context_get()->micoSystemConfig.name[0] = 1; //<2F><><EFBFBD>´<EFBFBD><C2B4><EFBFBD><EFBFBD><EFBFBD>ʱʹ<CAB1><CAB9>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
mico_system_context_get( )->micoSystemConfig.name[1] = 0; mico_system_context_get()->micoSystemConfig.name[1] = 0;
user_config_t* userConfigDefault = user_config_data; user_config_t* userConfigDefault = user_config_data;
@@ -43,7 +43,7 @@ void appRestoreDefault_callback( void * const user_config_data, uint32_t size )
userConfigDefault->mqtt_password[0] = 0; userConfigDefault->mqtt_password[0] = 0;
userConfigDefault->version = USER_CONFIG_VERSION; userConfigDefault->version = USER_CONFIG_VERSION;
for ( i = 0; i < PLUG_NUM; i++ ) for (i = 0; i < PLUG_NUM; i++)
{ {
userConfigDefault->plug[i].on = 1; userConfigDefault->plug[i].on = 1;
@@ -57,8 +57,8 @@ void appRestoreDefault_callback( void * const user_config_data, uint32_t size )
userConfigDefault->plug[i].name[6] = i + '1'; userConfigDefault->plug[i].name[6] = i + '1';
userConfigDefault->plug[i].name[7] = 0; userConfigDefault->plug[i].name[7] = 0;
// sprintf( userConfigDefault->plug[i].name, "<22><><EFBFBD><EFBFBD>%d", i );//<2F><><EFBFBD><EFBFBD><EFBFBD> // sprintf(userConfigDefault->plug[i].name, "<22><><EFBFBD><EFBFBD>%d", i);//<2F><><EFBFBD><EFBFBD><EFBFBD>
for ( j = 0; j < PLUG_TIME_TASK_NUM; j++ ) for (j = 0; j < PLUG_TIME_TASK_NUM; j++)
{ {
userConfigDefault->plug[i].task[j].hour = 0; userConfigDefault->plug[i].task[j].hour = 0;
userConfigDefault->plug[i].task[j].minute = 0; userConfigDefault->plug[i].task[j].minute = 0;
@@ -67,85 +67,85 @@ void appRestoreDefault_callback( void * const user_config_data, uint32_t size )
userConfigDefault->plug[i].task[j].action = 1; userConfigDefault->plug[i].task[j].action = 1;
} }
} }
// mico_system_context_update( sys_config ); // mico_system_context_update(sys_config);
} }
int application_start( void ) int application_start(void)
{ {
int i; int i;
os_log( "Start %s",VERSION ); os_log("Start %s",VERSION);
uint8_t main_num=0; uint8_t main_num=0;
uint32_t power_last = 0xffffffff; uint32_t power_last = 0xffffffff;
OSStatus err = kNoErr; OSStatus err = kNoErr;
// for ( i = 0; i < Relay_NUM; i++ ) // for (i = 0; i < Relay_NUM; i++)
// { // {
// MicoGpioOutputLow( Relay[(i)] ); // MicoGpioOutputLow(Relay[(i)]);
// MicoGpioInitialize( Relay[i], OUTPUT_PUSH_PULL ); // MicoGpioInitialize(Relay[i], OUTPUT_PUSH_PULL);
// MicoGpioOutputLow( Relay[(i)] ); // MicoGpioOutputLow(Relay[(i)]);
// MicoGpioOutputHigh(Relay[i]); // MicoGpioOutputHigh(Relay[i]);
// } // }
/* Create mico system context and read application's config data from flash */ /* Create mico system context and read application's config data from flash */
sys_config = mico_system_context_init( sizeof(user_config_t) ); sys_config = mico_system_context_init(sizeof(user_config_t));
user_config = ((system_context_t *) sys_config)->user_config_data; user_config = ((system_context_t *) sys_config)->user_config_data;
require_action( user_config, exit, err = kNoMemoryErr ); require_action(user_config, exit, err = kNoMemoryErr);
err = mico_system_init( sys_config ); err = mico_system_init(sys_config);
require_noerr( err, exit ); require_noerr(err, exit);
MicoGpioInitialize( (mico_gpio_t) Button, INPUT_PULL_UP ); MicoGpioInitialize((mico_gpio_t) Button, INPUT_PULL_UP);
if ( !MicoGpioInputGet( Button ) ) if (!MicoGpioInputGet(Button))
{ //<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ť״̬ { //<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ť״̬
os_log( "wifi_start_easylink" ); os_log("wifi_start_easylink");
wifi_status = WIFI_STATE_NOEASYLINK; //wifi_init<69><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>easylink wifi_status = WIFI_STATE_NOEASYLINK; //wifi_init<69><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>easylink
} }
MicoGpioInitialize( (mico_gpio_t) Led, OUTPUT_PUSH_PULL ); MicoGpioInitialize((mico_gpio_t) Led, OUTPUT_PUSH_PULL);
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);
user_relay_set( i, user_config->plug[i].on ); user_relay_set(i, user_config->plug[i].on);
} }
MicoSysLed( 0 ); MicoSysLed(0);
if ( user_config->version != USER_CONFIG_VERSION || user_config->plug[0].task[0].hour < 0 || user_config->plug[0].task[0].hour > 23 ) if (user_config->version != USER_CONFIG_VERSION || user_config->plug[0].task[0].hour < 0 || user_config->plug[0].task[0].hour > 23)
{ {
os_log( "WARNGIN: user params restored!" ); os_log("WARNGIN: user params restored!");
err = mico_system_context_restore( sys_config ); err = mico_system_context_restore(sys_config);
require_noerr( err, exit ); require_noerr(err, exit);
} }
if ( sys_config->micoSystemConfig.name[0] == 1 ) if (sys_config->micoSystemConfig.name[0] == 1)
{ {
IPStatusTypedef para; IPStatusTypedef para;
os_log( "micoWlanGetIPStatus:%d", micoWlanGetIPStatus( &para, Station )); //mac<61><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>0??!!! os_log("micoWlanGetIPStatus:%d", micoWlanGetIPStatus(&para, Station)); //mac<61><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>0??!!!
strcpy( strMac, para.mac ); strcpy(strMac, para.mac);
os_log( "result:%s",strMac ); os_log("result:%s",strMac);
os_log( "result:%s",para.mac ); os_log("result:%s",para.mac);
unsigned char mac1, mac2; unsigned char mac1, mac2;
mac1 = strtohex( strMac[8], strMac[9] ); mac1 = strtohex(strMac[8], strMac[9]);
mac2 = strtohex( strMac[10], strMac[11] ); mac2 = strtohex(strMac[10], strMac[11]);
os_log( "strtohex:0x%02x%02x",mac1,mac2 ); os_log("strtohex:0x%02x%02x",mac1,mac2);
sprintf( sys_config->micoSystemConfig.name, ZTC1_NAME, mac1, mac2 ); sprintf(sys_config->micoSystemConfig.name, ZTC1_NAME, mac1, mac2);
} }
os_log( "user:%s",user_config->user ); os_log("user:%s",user_config->user);
os_log( "device name:%s",sys_config->micoSystemConfig.name ); os_log("device name:%s",sys_config->micoSystemConfig.name);
os_log( "mqtt_ip:%s",user_config->mqtt_ip ); os_log("mqtt_ip:%s",user_config->mqtt_ip);
os_log( "mqtt_port:%d",user_config->mqtt_port ); os_log("mqtt_port:%d",user_config->mqtt_port);
os_log( "mqtt_user:%s",user_config->mqtt_user ); os_log("mqtt_user:%s",user_config->mqtt_user);
os_log( "mqtt_password:%s",user_config->mqtt_password ); os_log("mqtt_password:%s",user_config->mqtt_password);
os_log( "version:%d",user_config->version ); os_log("version:%d",user_config->version);
// 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);
// 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,
@@ -155,34 +155,34 @@ int application_start( void )
//sys_config = mico_system_context_init(sizeof(user_config_t)); //<2F><>ȡFlash<73>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD> //sys_config = mico_system_context_init(sizeof(user_config_t)); //<2F><>ȡFlash<73>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
ap_init(); ap_init();
wifi_init( ); wifi_init();
user_udp_init( ); user_udp_init();
key_init( ); key_init();
err = user_mqtt_init( ); err = user_mqtt_init();
require_noerr( err, exit ); require_noerr(err, exit);
err = user_rtc_init( ); err = user_rtc_init();
require_noerr( err, exit ); require_noerr(err, exit);
user_power_init(); user_power_init();
/* start http server thread */ /* start http server thread */
app_httpd_start(); app_httpd_start();
while ( 1 ) while (1)
{ {
main_num++; main_num++;
//<2F><><EFBFBD>͹<EFBFBD><CDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD>͹<EFBFBD><CDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if ( power_last != power || main_num>4 ) if (power_last != power || main_num>4)
{ {
power_last = power; power_last = power;
main_num =0; main_num =0;
uint8_t *power_buf = NULL; uint8_t *power_buf = NULL;
power_buf = malloc( 128 ); power_buf = malloc(128);
if ( power_buf != NULL ) if (power_buf != NULL)
{ {
sprintf( power_buf, "{\"mac\":\"%s\",\"power\":\"%d.%d\",\"total_time\":%d}", strMac, power / 10, power % 10, total_time ); sprintf(power_buf, "{\"mac\":\"%s\",\"power\":\"%d.%d\",\"total_time\":%d}", strMac, power / 10, power % 10, total_time);
user_send( 0, power_buf ); user_send(0, power_buf);
free( power_buf ); free(power_buf);
} }
user_mqtt_hass_power( ); user_mqtt_hass_power();
} }
mico_thread_msleep(1000); mico_thread_msleep(1000);

View File

@@ -48,11 +48,11 @@ static CRC16_Context crc_context;
static md5_context md5; static md5_context md5;
static uint32_t offset = 0; static uint32_t offset = 0;
static OSStatus onReceivedData( struct _HTTPHeader_t * httpHeader, static OSStatus onReceivedData(struct _HTTPHeader_t * httpHeader,
uint32_t pos, uint32_t pos,
uint8_t *data, uint8_t *data,
size_t len, size_t len,
void * userContext ); void * userContext);
static void hex2str(uint8_t *hex, int hex_len, char *str) static void hex2str(uint8_t *hex, int hex_len, char *str)
{ {
@@ -67,40 +67,40 @@ static void upper2lower(char *str, int len)
int i = 0; int i = 0;
for(i=0; i<len; i++) for(i=0; i<len; i++)
{ {
if( (*(str+i) >= 'A') && (*(str+i) <= 'Z') ){ if((*(str+i) >= 'A') && (*(str+i) <= 'Z')){
*(str+i) += 32; *(str+i) += 32;
} }
} }
} }
static int ota_server_send( uint8_t *data, int datalen ) static int ota_server_send(uint8_t *data, int datalen)
{ {
int res = 0; int res = 0;
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP ){ if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP){
res = send( ota_server_context->download_url.ota_fd, data, datalen, 0 ); res = send(ota_server_context->download_url.ota_fd, data, datalen, 0);
} }
#if OTA_USE_HTTPS #if OTA_USE_HTTPS
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS ){ if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS){
res = ssl_send( ota_server_context->download_url.ota_ssl, data, datalen); res = ssl_send(ota_server_context->download_url.ota_ssl, data, datalen);
} }
#endif #endif
return res; return res;
} }
static OSStatus ota_server_connect( struct sockaddr_in *addr, socklen_t addrlen ) static OSStatus ota_server_connect(struct sockaddr_in *addr, socklen_t addrlen)
{ {
OSStatus err = kNoErr; OSStatus err = kNoErr;
#if OTA_USE_HTTPS #if OTA_USE_HTTPS
int ssl_errno = 0; int ssl_errno = 0;
#endif #endif
err = connect( ota_server_context->download_url.ota_fd, (struct sockaddr *)addr, addrlen ); err = connect(ota_server_context->download_url.ota_fd, (struct sockaddr *)addr, addrlen);
require_noerr_string( err, exit, "ERROR: connect ota server failed" ); require_noerr_string(err, exit, "ERROR: connect ota server failed");
#if OTA_USE_HTTPS #if OTA_USE_HTTPS
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS ){ if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS){
ota_server_context->download_url.ota_ssl = ssl_connect( ota_server_context->download_url.ota_fd, 0, NULL, &ssl_errno ); ota_server_context->download_url.ota_ssl = ssl_connect(ota_server_context->download_url.ota_fd, 0, NULL, &ssl_errno);
require_action_string( ota_server_context->download_url.ota_ssl != NULL, exit, err = kConnectionErr,"ERROR: ssl disconnect" ); require_action_string(ota_server_context->download_url.ota_ssl != NULL, exit, err = kConnectionErr,"ERROR: ssl disconnect");
} }
#endif #endif
@@ -108,95 +108,95 @@ exit:
return err; return err;
} }
static int ota_server_read_header( HTTPHeader_t *httpHeader ) static int ota_server_read_header(HTTPHeader_t *httpHeader)
{ {
int res = 0; int res = 0;
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP ){ if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP){
res = SocketReadHTTPHeader( ota_server_context->download_url.ota_fd, httpHeader ); res = SocketReadHTTPHeader(ota_server_context->download_url.ota_fd, httpHeader);
} }
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS ){ if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS){
#if OTA_USE_HTTPS #if OTA_USE_HTTPS
res = SocketReadHTTPSHeader( ota_server_context->download_url.ota_ssl, httpHeader ); res = SocketReadHTTPSHeader(ota_server_context->download_url.ota_ssl, httpHeader);
#endif #endif
} }
return res; return res;
} }
static int ota_server_read_body( HTTPHeader_t *httpHeader ) static int ota_server_read_body(HTTPHeader_t *httpHeader)
{ {
int res = 0; int res = 0;
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP ){ if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP){
res = SocketReadHTTPBody( ota_server_context->download_url.ota_fd, httpHeader ); res = SocketReadHTTPBody(ota_server_context->download_url.ota_fd, httpHeader);
} }
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS ){ if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS){
#if OTA_USE_HTTPS #if OTA_USE_HTTPS
res = SocketReadHTTPSBody( ota_server_context->download_url.ota_ssl, httpHeader ); res = SocketReadHTTPSBody(ota_server_context->download_url.ota_ssl, httpHeader);
#endif #endif
} }
return res; return res;
} }
static int ota_server_send_header( void ) static int ota_server_send_header(void)
{ {
char *header = NULL; char *header = NULL;
int j = 0; int j = 0;
int ret = 0; int ret = 0;
header = malloc( OTA_SEND_HEAD_SIZE ); header = malloc(OTA_SEND_HEAD_SIZE);
memset( header, 0x00, OTA_SEND_HEAD_SIZE ); memset(header, 0x00, OTA_SEND_HEAD_SIZE);
j = sprintf( header, "GET " ); j = sprintf(header, "GET ");
j += sprintf( header + j, "/%s HTTP/1.1\r\n", ota_server_context->download_url.url ); j += sprintf(header + j, "/%s HTTP/1.1\r\n", ota_server_context->download_url.url);
if ( ota_server_context->download_url.port == 0 ) if (ota_server_context->download_url.port == 0)
{ {
j += sprintf( header + j, "Host: %s\r\n", ota_server_context->download_url.host ); j += sprintf(header + j, "Host: %s\r\n", ota_server_context->download_url.host);
} else } else
{ {
j += sprintf( header + j, "Host: %s:%d\r\n", ota_server_context->download_url.host, ota_server_context->download_url.port ); j += sprintf(header + j, "Host: %s:%d\r\n", ota_server_context->download_url.host, ota_server_context->download_url.port);
} }
j += sprintf( header + j, "Connection: close\r\n" ); //Keep-Alive close j += sprintf(header + j, "Connection: close\r\n"); //Keep-Alive close
//Range: bytes=start-end //Range: bytes=start-end
if ( ota_server_context->download_state.download_begin_pos > 0 ) if (ota_server_context->download_state.download_begin_pos > 0)
{ {
if ( ota_server_context->download_state.download_end_pos > 0 ) if (ota_server_context->download_state.download_end_pos > 0)
{ {
j += sprintf( header + j, "Range: bytes=%d-%d\r\n", ota_server_context->download_state.download_begin_pos, j += sprintf(header + j, "Range: bytes=%d-%d\r\n", ota_server_context->download_state.download_begin_pos,
ota_server_context->download_state.download_end_pos ); ota_server_context->download_state.download_end_pos);
} else } else
{ {
j += sprintf( header + j, "Range: bytes=%d-\r\n", ota_server_context->download_state.download_begin_pos ); j += sprintf(header + j, "Range: bytes=%d-\r\n", ota_server_context->download_state.download_begin_pos);
} }
} }
j += sprintf( header + j, "\r\n" ); j += sprintf(header + j, "\r\n");
ret = ota_server_send( (uint8_t *) header, strlen( header ) ); ret = ota_server_send((uint8_t *) header, strlen(header));
// ota_server_log("send: %d\r\n%s", strlen(header), header); // ota_server_log("send: %d\r\n%s", strlen(header), header);
if ( header != NULL ) free( header ); if (header != NULL) free(header);
return ret; return ret;
} }
static void ota_server_socket_close( void ) static void ota_server_socket_close(void)
{ {
#if OTA_USE_HTTPS #if OTA_USE_HTTPS
if ( ota_server_context->download_url.ota_ssl ) ssl_close( ota_server_context->download_url.ota_ssl ); if (ota_server_context->download_url.ota_ssl) ssl_close(ota_server_context->download_url.ota_ssl);
#endif #endif
SocketClose( &(ota_server_context->download_url.ota_fd) ); SocketClose(&(ota_server_context->download_url.ota_fd));
ota_server_context->download_url.ota_fd = -1; ota_server_context->download_url.ota_fd = -1;
} }
static int ota_server_connect_server( struct in_addr in_addr ) static int ota_server_connect_server(struct in_addr in_addr)
{ {
int err = 0; int err = 0;
struct sockaddr_in server_address; struct sockaddr_in server_address;
if ( ota_server_context->download_url.port == 0 ) if (ota_server_context->download_url.port == 0)
{ {
if ( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP ) if (ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP)
{ {
server_address.sin_port = htons(80); server_address.sin_port = htons(80);
} else } else
@@ -211,10 +211,10 @@ static int ota_server_connect_server( struct in_addr in_addr )
server_address.sin_family = AF_INET; server_address.sin_family = AF_INET;
server_address.sin_addr = in_addr; server_address.sin_addr = in_addr;
err = ota_server_connect( &server_address, sizeof(server_address) ); err = ota_server_connect(&server_address, sizeof(server_address));
if ( err != 0 ) if (err != 0)
{ {
mico_thread_sleep( 1 ); mico_thread_sleep(1);
return -1; return -1;
} }
@@ -222,17 +222,17 @@ static int ota_server_connect_server( struct in_addr in_addr )
return 0; return 0;
} }
static void ota_server_progress_set( OTA_STATE_E state ) static void ota_server_progress_set(OTA_STATE_E state)
{ {
float progress = 0.00; float progress = 0.00;
progress =(float) ota_server_context->download_state.download_begin_pos / ota_server_context->download_state.download_len; progress =(float) ota_server_context->download_state.download_begin_pos / ota_server_context->download_state.download_len;
progress = progress*100; progress = progress*100;
if( ota_server_context->ota_server_cb != NULL ) if( ota_server_context->ota_server_cb != NULL)
ota_server_context->ota_server_cb(state, progress); ota_server_context->ota_server_cb(state, progress);
} }
static void ota_server_thread( mico_thread_arg_t arg ) static void ota_server_thread(mico_thread_arg_t arg)
{ {
OSStatus err; OSStatus err;
uint16_t crc16 = 0; uint16_t crc16 = 0;
@@ -243,65 +243,65 @@ static void ota_server_thread( mico_thread_arg_t arg )
char **pptr = NULL; char **pptr = NULL;
struct in_addr in_addr; struct in_addr in_addr;
mico_logic_partition_t* ota_partition = MicoFlashGetInfo( MICO_PARTITION_OTA_TEMP ); mico_logic_partition_t* ota_partition = MicoFlashGetInfo(MICO_PARTITION_OTA_TEMP);
ota_server_context->ota_control = OTA_CONTROL_START; ota_server_context->ota_control = OTA_CONTROL_START;
hostent_content = gethostbyname( ota_server_context->download_url.host ); hostent_content = gethostbyname(ota_server_context->download_url.host);
require_action_quiet( hostent_content != NULL, DELETE, ota_server_progress_set(OTA_FAIL)); require_action_quiet(hostent_content != NULL, DELETE, ota_server_progress_set(OTA_FAIL));
pptr=hostent_content->h_addr_list; pptr=hostent_content->h_addr_list;
in_addr.s_addr = *(uint32_t *)(*pptr); in_addr.s_addr = *(uint32_t *)(*pptr);
strcpy( ota_server_context->download_url.ip, inet_ntoa(in_addr)); strcpy(ota_server_context->download_url.ip, inet_ntoa(in_addr));
ota_server_log("OTA server address: %s, host ip: %s", ota_server_context->download_url.host, ota_server_context->download_url.ip); ota_server_log("OTA server address: %s, host ip: %s", ota_server_context->download_url.host, ota_server_context->download_url.ip);
offset = 0; offset = 0;
MicoFlashErase( MICO_PARTITION_OTA_TEMP, 0x0, ota_partition->partition_length ); MicoFlashErase(MICO_PARTITION_OTA_TEMP, 0x0, ota_partition->partition_length);
CRC16_Init( &crc_context ); CRC16_Init(&crc_context);
if( ota_server_context->ota_check.is_md5 == true ){ if(ota_server_context->ota_check.is_md5 == true){
InitMd5( &md5 ); InitMd5(&md5);
} }
httpHeader = HTTPHeaderCreateWithCallback( 1024, onReceivedData, NULL, NULL ); httpHeader = HTTPHeaderCreateWithCallback(1024, onReceivedData, NULL, NULL);
require_action( httpHeader, DELETE, ota_server_progress_set(OTA_FAIL) ); require_action(httpHeader, DELETE, ota_server_progress_set(OTA_FAIL));
while ( 1 ) while (1)
{ {
if ( ota_server_context->ota_control == OTA_CONTROL_PAUSE ){ if (ota_server_context->ota_control == OTA_CONTROL_PAUSE){
mico_thread_sleep( 1 ); mico_thread_sleep(1);
continue; continue;
}else if( ota_server_context->ota_control == OTA_CONTROL_STOP ){ }else if(ota_server_context->ota_control == OTA_CONTROL_STOP){
goto DELETE; goto DELETE;
} }
ota_server_context->download_url.ota_fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); ota_server_context->download_url.ota_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
err = ota_server_connect_server( in_addr ); err = ota_server_connect_server(in_addr);
require_noerr_action( err, RECONNECTED, ota_server_progress_set(OTA_FAIL)); require_noerr_action(err, RECONNECTED, ota_server_progress_set(OTA_FAIL));
/* Send HTTP Request */ /* Send HTTP Request */
ota_server_send_header( ); ota_server_send_header();
FD_ZERO( &readfds ); FD_ZERO(&readfds);
FD_SET( ota_server_context->download_url.ota_fd, &readfds ); FD_SET(ota_server_context->download_url.ota_fd, &readfds);
select( ota_server_context->download_url.ota_fd + 1, &readfds, NULL, NULL, NULL ); select(ota_server_context->download_url.ota_fd + 1, &readfds, NULL, NULL, NULL);
if ( FD_ISSET( ota_server_context->download_url.ota_fd, &readfds ) ) if (FD_ISSET(ota_server_context->download_url.ota_fd, &readfds))
{ {
/*parse header*/ /*parse header*/
err = ota_server_read_header( httpHeader ); err = ota_server_read_header(httpHeader);
if ( ota_server_context->ota_control == OTA_CONTROL_START ) if (ota_server_context->ota_control == OTA_CONTROL_START)
{ {
ota_server_context->download_state.download_len = httpHeader->contentLength; ota_server_context->download_state.download_len = httpHeader->contentLength;
ota_server_context->ota_control = OTA_CONTROL_CONTINUE; ota_server_context->ota_control = OTA_CONTROL_CONTINUE;
} }
switch ( err ) switch (err)
{ {
case kNoErr: case kNoErr:
#if OTA_DEBUG #if OTA_DEBUG
PrintHTTPHeader( httpHeader ); PrintHTTPHeader(httpHeader);
#endif #endif
err = ota_server_read_body( httpHeader );/*get body data*/ err = ota_server_read_body(httpHeader);/*get body data*/
require_noerr( err, RECONNECTED ); require_noerr(err, RECONNECTED);
/*get data and print*/ /*get data and print*/
break; break;
case EWOULDBLOCK: case EWOULDBLOCK:
@@ -313,21 +313,21 @@ static void ota_server_thread( mico_thread_arg_t arg )
} }
} }
if ( ota_server_context->download_state.download_len == ota_server_context->download_state.download_begin_pos ) if (ota_server_context->download_state.download_len == ota_server_context->download_state.download_begin_pos)
{ {
if( httpHeader->statusCode != 200 ){ if(httpHeader->statusCode != 200){
ota_server_progress_set(OTA_FAIL); ota_server_progress_set(OTA_FAIL);
goto DELETE; goto DELETE;
} }
CRC16_Final( &crc_context, &crc16 ); CRC16_Final(&crc_context, &crc16);
if( ota_server_context->ota_check.is_md5 == true ){ if(ota_server_context->ota_check.is_md5 == true){
Md5Final( &md5, (unsigned char *) md5_value ); Md5Final(&md5, (unsigned char *) md5_value);
hex2str((uint8_t *)md5_value, 16, md5_value_string); hex2str((uint8_t *)md5_value, 16, md5_value_string);
} }
if ( memcmp( md5_value_string, ota_server_context->ota_check.md5, OTA_MD5_LENTH ) == 0 ){ if (memcmp(md5_value_string, ota_server_context->ota_check.md5, OTA_MD5_LENTH) == 0){
ota_server_progress_set(OTA_SUCCE); ota_server_progress_set(OTA_SUCCE);
mico_ota_switch_to_new_fw( ota_server_context->download_state.download_len, crc16 ); mico_ota_switch_to_new_fw(ota_server_context->download_state.download_len, crc16);
mico_system_power_perform( mico_system_context_get( ), eState_Software_Reset ); mico_system_power_perform(mico_system_context_get(), eState_Software_Reset);
}else{ }else{
ota_server_log("OTA md5 check err, Calculation:%s, Get:%s", md5_value_string, ota_server_context->ota_check.md5); ota_server_log("OTA md5 check err, Calculation:%s, Get:%s", md5_value_string, ota_server_context->ota_check.md5);
ota_server_progress_set(OTA_FAIL); ota_server_progress_set(OTA_FAIL);
@@ -336,16 +336,16 @@ static void ota_server_thread( mico_thread_arg_t arg )
} }
RECONNECTED: RECONNECTED:
ota_server_socket_close( ); ota_server_socket_close();
mico_thread_sleep(2); mico_thread_sleep(2);
continue; continue;
} }
DELETE: DELETE:
HTTPHeaderDestory( &httpHeader ); HTTPHeaderDestory(&httpHeader);
ota_server_socket_close( ); ota_server_socket_close();
if( ota_server_context != NULL ){ if(ota_server_context != NULL){
if( ota_server_context->download_url.url != NULL ){ if(ota_server_context->download_url.url != NULL){
free(ota_server_context->download_url.url); free(ota_server_context->download_url.url);
ota_server_context->download_url.url = NULL; ota_server_context->download_url.url = NULL;
} }
@@ -358,52 +358,52 @@ DELETE:
} }
/*one request may receive multi reply*/ /*one request may receive multi reply*/
static OSStatus onReceivedData( struct _HTTPHeader_t * inHeader, uint32_t inPos, uint8_t * inData, static OSStatus onReceivedData(struct _HTTPHeader_t * inHeader, uint32_t inPos, uint8_t * inData,
size_t inLen, void * inUserContext ) size_t inLen, void * inUserContext)
{ {
OSStatus err = kNoErr; OSStatus err = kNoErr;
if ( inLen == 0 ) if (inLen == 0)
return err; return err;
ota_server_context->download_state.download_begin_pos += inLen; ota_server_context->download_state.download_begin_pos += inLen;
CRC16_Update( &crc_context, inData, inLen ); CRC16_Update(&crc_context, inData, inLen);
if( ota_server_context->ota_check.is_md5 == true ){ if(ota_server_context->ota_check.is_md5 == true){
Md5Update( &md5, inData, inLen ); Md5Update(&md5, inData, inLen);
} }
MicoFlashWrite( MICO_PARTITION_OTA_TEMP, &offset, (uint8_t *) inData, inLen ); MicoFlashWrite(MICO_PARTITION_OTA_TEMP, &offset, (uint8_t *) inData, inLen);
ota_server_progress_set(OTA_LOADING); ota_server_progress_set(OTA_LOADING);
if( ota_server_context->ota_control == OTA_CONTROL_PAUSE ){ if(ota_server_context->ota_control == OTA_CONTROL_PAUSE){
while( 1 ){ while(1){
if( ota_server_context->ota_control != OTA_CONTROL_PAUSE ) if(ota_server_context->ota_control != OTA_CONTROL_PAUSE)
break; break;
mico_thread_msleep(100); mico_thread_msleep(100);
} }
} }
if( ota_server_context->ota_control == OTA_CONTROL_STOP ){ if(ota_server_context->ota_control == OTA_CONTROL_STOP){
err = kUnsupportedErr; err = kUnsupportedErr;
} }
return err; return err;
} }
static OSStatus ota_server_set_url( char *url ) static OSStatus ota_server_set_url(char *url)
{ {
OSStatus err = kNoErr; OSStatus err = kNoErr;
url_field_t *url_t; url_field_t *url_t;
char *pos = NULL; char *pos = NULL;
url_t = url_parse( url ); url_t = url_parse(url);
require_action(url, exit, err = kParamErr); require_action(url, exit, err = kParamErr);
#if OTA_DEBUG #if OTA_DEBUG
url_field_print( url_t ); url_field_print(url_t);
#endif #endif
if ( !strcmp( url_t->schema, "https" ) ) if (!strcmp(url_t->schema, "https"))
{ {
ota_server_context->download_url.HTTP_SECURITY = HTTP_SECURITY_HTTPS; ota_server_context->download_url.HTTP_SECURITY = HTTP_SECURITY_HTTPS;
} else } else
@@ -411,30 +411,30 @@ static OSStatus ota_server_set_url( char *url )
ota_server_context->download_url.HTTP_SECURITY = HTTP_SECURITY_HTTP; ota_server_context->download_url.HTTP_SECURITY = HTTP_SECURITY_HTTP;
} }
strcpy( ota_server_context->download_url.host, url_t->host ); strcpy(ota_server_context->download_url.host, url_t->host);
ota_server_context->download_url.port = atoi( url_t->port ); ota_server_context->download_url.port = atoi(url_t->port);
pos = strstr( url, url_t->path ); pos = strstr(url, url_t->path);
if ( pos == NULL ) if (pos == NULL)
{ {
strcpy( ota_server_context->download_url.url, "" ); strcpy(ota_server_context->download_url.url, "");
} else } else
{ {
strcpy( ota_server_context->download_url.url, pos ); strcpy(ota_server_context->download_url.url, pos);
} }
exit: exit:
url_free( url_t ); url_free(url_t);
return err; return err;
} }
OSStatus ota_server_start( char *url, char *md5, ota_server_cb_fn call_back ) OSStatus ota_server_start(char *url, char *md5, ota_server_cb_fn call_back)
{ {
OSStatus err = kNoErr; OSStatus err = kNoErr;
require_action(url, exit, err = kParamErr); require_action(url, exit, err = kParamErr);
if( ota_server_context != NULL ){ if(ota_server_context != NULL){
if( ota_server_context->download_url.url != NULL ){ if(ota_server_context->download_url.url != NULL){
free(ota_server_context->download_url.url); free(ota_server_context->download_url.url);
ota_server_context->download_url.url = NULL; ota_server_context->download_url.url = NULL;
} }
@@ -453,7 +453,7 @@ OSStatus ota_server_start( char *url, char *md5, ota_server_cb_fn call_back )
err = ota_server_set_url(url); err = ota_server_set_url(url);
require_noerr(err, exit); require_noerr(err, exit);
if( md5 != NULL ){ if(md5 != NULL){
ota_server_context->ota_check.is_md5 = true; ota_server_context->ota_check.is_md5 = true;
memcpy(ota_server_context->ota_check.md5, md5, OTA_MD5_LENTH); memcpy(ota_server_context->ota_check.md5, md5, OTA_MD5_LENTH);
upper2lower(ota_server_context->ota_check.md5, OTA_MD5_LENTH); upper2lower(ota_server_context->ota_check.md5, OTA_MD5_LENTH);
@@ -461,27 +461,27 @@ OSStatus ota_server_start( char *url, char *md5, ota_server_cb_fn call_back )
ota_server_context->ota_server_cb = call_back; ota_server_context->ota_server_cb = call_back;
err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY, "OTA", ota_server_thread, OTA_SERVER_THREAD_STACK_SIZE, 0 ); err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "OTA", ota_server_thread, OTA_SERVER_THREAD_STACK_SIZE, 0);
exit: exit:
return err; return err;
} }
void ota_server_pause( void ) void ota_server_pause(void)
{ {
ota_server_context->ota_control = OTA_CONTROL_PAUSE; ota_server_context->ota_control = OTA_CONTROL_PAUSE;
} }
void ota_server_continue( void ) void ota_server_continue(void)
{ {
ota_server_context->ota_control = OTA_CONTROL_CONTINUE; ota_server_context->ota_control = OTA_CONTROL_CONTINUE;
} }
void ota_server_stop( void ) void ota_server_stop(void)
{ {
ota_server_context->ota_control = OTA_CONTROL_STOP; ota_server_context->ota_control = OTA_CONTROL_STOP;
} }
OTA_CONTROL_E ota_server_get( void ) OTA_CONTROL_E ota_server_get(void)
{ {
return ota_server_context->ota_control; return ota_server_context->ota_control;
} }

View File

@@ -109,34 +109,34 @@ typedef struct _ota_server_context_t{
* @return kNoErr : on success. * @return kNoErr : on success.
* @return kGeneralErr : if an error occurred * @return kGeneralErr : if an error occurred
*/ */
OSStatus ota_server_start( char *url, char *md5, ota_server_cb_fn call_back ); OSStatus ota_server_start(char *url, char *md5, ota_server_cb_fn call_back);
/** @brief Get OTA server state /** @brief Get OTA server state
* *
* @return OTA_CONTROL_E : state * @return OTA_CONTROL_E : state
*/ */
OTA_CONTROL_E ota_server_state_get( void ); OTA_CONTROL_E ota_server_state_get(void);
/** @brief Pause OTA server daemons /** @brief Pause OTA server daemons
* *
* @return No * @return No
*/ */
void ota_server_pause( void ); void ota_server_pause(void);
/** @brief Continue OTA server daemons /** @brief Continue OTA server daemons
* *
* @return No * @return No
*/ */
void ota_server_continue( void ); void ota_server_continue(void);
/** @brief Stop OTA server daemons /** @brief Stop OTA server daemons
* *
* @return No * @return No
*/ */
void ota_server_stop( void ); void ota_server_stop(void);
#endif #endif

View File

@@ -11,201 +11,201 @@
uint32_t last_time = 0; uint32_t last_time = 0;
void user_function_set_last_time( ) void user_function_set_last_time()
{ {
last_time = UpTicks( ); last_time = UpTicks();
} }
bool json_plug_analysis( int udp_flag, unsigned char x, cJSON * pJsonRoot, cJSON * pJsonSend ); bool json_plug_analysis(int udp_flag, unsigned char x, cJSON * pJsonRoot, cJSON * pJsonSend);
bool json_plug_task_analysis( unsigned char x, unsigned char y, cJSON * pJsonRoot, cJSON * pJsonSend ); bool json_plug_task_analysis(unsigned char x, unsigned char y, cJSON * pJsonRoot, cJSON * pJsonSend);
void user_send( int udp_flag, char *s ) void user_send(int udp_flag, char *s)
{ {
if ( udp_flag || !user_mqtt_isconnect( ) ) if (udp_flag || !user_mqtt_isconnect())
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( 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)
{ {
unsigned char i; unsigned char i;
bool update_user_config_flag = false; //<2F><>־λ,<2C><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>´<EFBFBD><C2B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> bool update_user_config_flag = false; //<2F><>־λ,<2C><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>´<EFBFBD><C2B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool return_flag = true; //Ϊtrueʱ<65><CAB1><EFBFBD><EFBFBD>json<6F><6E><EFBFBD><EFBFBD>,<2C><><EFBFBD>򲻷<EFBFBD><F2B2BBB7><EFBFBD> bool return_flag = true; //Ϊtrueʱ<65><CAB1><EFBFBD><EFBFBD>json<6F><6E><EFBFBD><EFBFBD>,<2C><><EFBFBD>򲻷<EFBFBD><F2B2BBB7><EFBFBD>
cJSON * pJsonRoot = cJSON_Parse( pusrdata ); cJSON * pJsonRoot = cJSON_Parse(pusrdata);
if ( !pJsonRoot ) if (!pJsonRoot)
{ {
os_log( "this is not a json data:\r\n%s\r\n", pusrdata ); os_log("this is not a json data:\r\n%s\r\n", pusrdata);
return; return;
} }
//<2F><><EFBFBD><EFBFBD>UDP<44><50><EFBFBD><EFBFBD>device report(MQTTͬ<54><CDAC><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>) //<2F><><EFBFBD><EFBFBD>UDP<44><50><EFBFBD><EFBFBD>device report(MQTTͬ<54><CDAC><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>)
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)
{ {
cJSON *pRoot = cJSON_CreateObject( ); cJSON *pRoot = cJSON_CreateObject();
cJSON_AddStringToObject( pRoot, "name", sys_config->micoSystemConfig.name ); cJSON_AddStringToObject(pRoot, "name", sys_config->micoSystemConfig.name);
cJSON_AddStringToObject( pRoot, "mac", strMac ); cJSON_AddStringToObject(pRoot, "mac", strMac);
cJSON_AddNumberToObject( pRoot, "type", TYPE ); cJSON_AddNumberToObject(pRoot, "type", TYPE);
cJSON_AddStringToObject( pRoot, "type_name", TYPE_NAME ); cJSON_AddStringToObject(pRoot, "type_name", TYPE_NAME);
IPStatusTypedef para; IPStatusTypedef para;
micoWlanGetIPStatus( &para, Station ); micoWlanGetIPStatus(&para, Station);
cJSON_AddStringToObject( pRoot, "ip", para.ip ); cJSON_AddStringToObject(pRoot, "ip", para.ip);
char *s = cJSON_Print( pRoot ); char *s = cJSON_Print(pRoot);
// os_log( "pRoot: %s\r\n", s ); // os_log("pRoot: %s\r\n", s);
user_send( udp_flag, s ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> user_send(udp_flag, s); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
free( (void *) s ); free((void *) s);
cJSON_Delete( pRoot ); cJSON_Delete(pRoot);
// cJSON_Delete(p_cmd); // cJSON_Delete(p_cmd);
} }
//<2F><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEB2BF> //<2F><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEB2BF>
cJSON *p_name = cJSON_GetObjectItem( pJsonRoot, "name" ); cJSON *p_name = cJSON_GetObjectItem(pJsonRoot, "name");
cJSON *p_mac = cJSON_GetObjectItem( pJsonRoot, "mac" ); cJSON *p_mac = cJSON_GetObjectItem(pJsonRoot, "mac");
//<2F><>ʼ<EFBFBD><CABC>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><>ʼ<EFBFBD><CABC>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if ( (p_name && cJSON_IsString( p_name ) && strcmp( p_name->valuestring, sys_config->micoSystemConfig.name ) == 0) //name if ((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 || (p_mac && cJSON_IsString(p_mac) && strcmp(p_mac->valuestring, strMac) == 0) //mac
) )
{ {
cJSON *json_send = cJSON_CreateObject( ); cJSON *json_send = cJSON_CreateObject();
cJSON_AddStringToObject( json_send, "mac", strMac ); cJSON_AddStringToObject(json_send, "mac", strMac);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// cJSON *p_cmd = cJSON_GetObjectItem( pJsonRoot, "name" ); // cJSON *p_cmd = cJSON_GetObjectItem(pJsonRoot, "name");
if(p_cmd && cJSON_IsString( p_cmd ) && strcmp( p_cmd->valuestring, "restart" ) == 0) if(p_cmd && cJSON_IsString(p_cmd) && strcmp(p_cmd->valuestring, "restart") == 0)
{ {
os_log("cmd:restart"); os_log("cmd:restart");
mico_system_power_perform( mico_system_context_get( ), eState_Software_Reset ); mico_system_power_perform(mico_system_context_get(), eState_Software_Reset);
} }
//<2F><><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD><EFBFBD>
cJSON *p_version = cJSON_GetObjectItem( pJsonRoot, "version" ); cJSON *p_version = cJSON_GetObjectItem(pJsonRoot, "version");
if ( p_version ) if (p_version)
{ {
os_log("version:%s",VERSION); os_log("version:%s",VERSION);
cJSON_AddStringToObject( json_send, "version", VERSION ); cJSON_AddStringToObject(json_send, "version", VERSION);
} }
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
cJSON *p_total_time = cJSON_GetObjectItem( pJsonRoot, "total_time" ); cJSON *p_total_time = cJSON_GetObjectItem(pJsonRoot, "total_time");
if ( p_total_time ) if (p_total_time)
{ {
cJSON_AddNumberToObject( json_send, "total_time", total_time ); cJSON_AddNumberToObject(json_send, "total_time", total_time);
} }
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
cJSON *p_power = cJSON_GetObjectItem( pJsonRoot, "power" ); cJSON *p_power = cJSON_GetObjectItem(pJsonRoot, "power");
if ( p_power ) if (p_power)
{ {
uint8_t *temp_buf = malloc( 16 ); uint8_t *temp_buf = malloc(16);
if ( temp_buf != NULL ) if (temp_buf != NULL)
{ {
sprintf( temp_buf, "%d.%d", power / 10, power % 10 ); sprintf(temp_buf, "%d.%d", power / 10, power % 10);
cJSON_AddStringToObject( json_send, "power", temp_buf ); cJSON_AddStringToObject(json_send, "power", temp_buf);
free( temp_buf ); free(temp_buf);
} }
os_log("power:%d",power); os_log("power:%d",power);
} }
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>setting----------------------------------------------------------------- //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>setting-----------------------------------------------------------------
cJSON *p_setting = cJSON_GetObjectItem( pJsonRoot, "setting" ); cJSON *p_setting = cJSON_GetObjectItem(pJsonRoot, "setting");
if ( p_setting ) if (p_setting)
{ {
//<2F><><EFBFBD><EFBFBD>ota //<2F><><EFBFBD><EFBFBD>ota
cJSON *p_ota = cJSON_GetObjectItem( p_setting, "ota" ); cJSON *p_ota = cJSON_GetObjectItem(p_setting, "ota");
if ( p_ota ) if (p_ota)
{ {
if ( cJSON_IsString( p_ota ) ) if (cJSON_IsString(p_ota))
user_ota_start( p_ota->valuestring, NULL ); user_ota_start(p_ota->valuestring, NULL);
} }
cJSON *json_setting_send = cJSON_CreateObject( ); cJSON *json_setting_send = cJSON_CreateObject();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>/deviceid //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>/deviceid
cJSON *p_setting_name = cJSON_GetObjectItem( p_setting, "name" ); cJSON *p_setting_name = cJSON_GetObjectItem(p_setting, "name");
if ( p_setting_name && cJSON_IsString( p_setting_name ) ) if (p_setting_name && cJSON_IsString(p_setting_name))
{ {
update_user_config_flag = true; update_user_config_flag = true;
sprintf( sys_config->micoSystemConfig.name, p_setting_name->valuestring ); sprintf(sys_config->micoSystemConfig.name, p_setting_name->valuestring);
} }
//<2F><><EFBFBD><EFBFBD>mqtt ip //<2F><><EFBFBD><EFBFBD>mqtt ip
cJSON *p_mqtt_ip = cJSON_GetObjectItem( p_setting, "mqtt_uri" ); cJSON *p_mqtt_ip = cJSON_GetObjectItem(p_setting, "mqtt_uri");
if ( p_mqtt_ip && cJSON_IsString( p_mqtt_ip ) ) if (p_mqtt_ip && cJSON_IsString(p_mqtt_ip))
{ {
update_user_config_flag = true; update_user_config_flag = true;
sprintf( user_config->mqtt_ip, p_mqtt_ip->valuestring ); sprintf(user_config->mqtt_ip, p_mqtt_ip->valuestring);
} }
//<2F><><EFBFBD><EFBFBD>mqtt port //<2F><><EFBFBD><EFBFBD>mqtt port
cJSON *p_mqtt_port = cJSON_GetObjectItem( p_setting, "mqtt_port" ); cJSON *p_mqtt_port = cJSON_GetObjectItem(p_setting, "mqtt_port");
if ( p_mqtt_port && cJSON_IsNumber( p_mqtt_port ) ) if (p_mqtt_port && cJSON_IsNumber(p_mqtt_port))
{ {
update_user_config_flag = true; update_user_config_flag = true;
user_config->mqtt_port = p_mqtt_port->valueint; user_config->mqtt_port = p_mqtt_port->valueint;
} }
//<2F><><EFBFBD><EFBFBD>mqtt user //<2F><><EFBFBD><EFBFBD>mqtt user
cJSON *p_mqtt_user = cJSON_GetObjectItem( p_setting, "mqtt_user" ); cJSON *p_mqtt_user = cJSON_GetObjectItem(p_setting, "mqtt_user");
if ( p_mqtt_user && cJSON_IsString( p_mqtt_user ) ) if (p_mqtt_user && cJSON_IsString(p_mqtt_user))
{ {
update_user_config_flag = true; update_user_config_flag = true;
sprintf( user_config->mqtt_user, p_mqtt_user->valuestring ); sprintf(user_config->mqtt_user, p_mqtt_user->valuestring);
} }
//<2F><><EFBFBD><EFBFBD>mqtt password //<2F><><EFBFBD><EFBFBD>mqtt password
cJSON *p_mqtt_password = cJSON_GetObjectItem( p_setting, "mqtt_password" ); cJSON *p_mqtt_password = cJSON_GetObjectItem(p_setting, "mqtt_password");
if ( p_mqtt_password && cJSON_IsString( p_mqtt_password ) ) if (p_mqtt_password && cJSON_IsString(p_mqtt_password))
{ {
update_user_config_flag = true; update_user_config_flag = true;
sprintf( user_config->mqtt_password, p_mqtt_password->valuestring ); sprintf(user_config->mqtt_password, p_mqtt_password->valuestring);
} }
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD>豸ota //<2F><><EFBFBD><EFBFBD><EFBFBD>豸ota
if ( p_ota ) cJSON_AddStringToObject( json_setting_send, "ota", p_ota->valuestring ); if (p_ota) cJSON_AddStringToObject(json_setting_send, "ota", p_ota->valuestring);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>/deviceid //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>/deviceid
if ( p_setting_name ) cJSON_AddStringToObject( json_setting_send, "name", sys_config->micoSystemConfig.name ); if (p_setting_name) cJSON_AddStringToObject(json_setting_send, "name", sys_config->micoSystemConfig.name);
//<2F><><EFBFBD><EFBFBD>mqtt ip //<2F><><EFBFBD><EFBFBD>mqtt ip
if ( p_mqtt_ip ) cJSON_AddStringToObject( json_setting_send, "mqtt_uri", user_config->mqtt_ip ); if (p_mqtt_ip) cJSON_AddStringToObject(json_setting_send, "mqtt_uri", user_config->mqtt_ip);
//<2F><><EFBFBD><EFBFBD>mqtt port //<2F><><EFBFBD><EFBFBD>mqtt port
if ( p_mqtt_port ) cJSON_AddNumberToObject( json_setting_send, "mqtt_port", user_config->mqtt_port ); if (p_mqtt_port) cJSON_AddNumberToObject(json_setting_send, "mqtt_port", user_config->mqtt_port);
//<2F><><EFBFBD><EFBFBD>mqtt user //<2F><><EFBFBD><EFBFBD>mqtt user
if ( p_mqtt_user ) cJSON_AddStringToObject( json_setting_send, "mqtt_user", user_config->mqtt_user ); if (p_mqtt_user) cJSON_AddStringToObject(json_setting_send, "mqtt_user", user_config->mqtt_user);
//<2F><><EFBFBD><EFBFBD>mqtt password //<2F><><EFBFBD><EFBFBD>mqtt password
if ( p_mqtt_password ) cJSON_AddStringToObject( json_setting_send, "mqtt_password", user_config->mqtt_password ); if (p_mqtt_password) cJSON_AddStringToObject(json_setting_send, "mqtt_password", user_config->mqtt_password);
cJSON_AddItemToObject( json_send, "setting", json_setting_send ); cJSON_AddItemToObject(json_send, "setting", json_setting_send);
} }
//<2F><><EFBFBD><EFBFBD>plug----------------------------------------------------------------- //<2F><><EFBFBD><EFBFBD>plug-----------------------------------------------------------------
for ( i = 0; i < PLUG_NUM; i++ ) for (i = 0; i < PLUG_NUM; i++)
{ {
if ( json_plug_analysis( udp_flag, i, pJsonRoot, json_send ) ) if (json_plug_analysis(udp_flag, i, pJsonRoot, json_send))
update_user_config_flag = true; update_user_config_flag = true;
} }
cJSON_AddStringToObject( json_send, "name", sys_config->micoSystemConfig.name ); cJSON_AddStringToObject(json_send, "name", sys_config->micoSystemConfig.name);
if ( return_flag == true ) if (return_flag == true)
{ {
char *json_str = cJSON_Print( json_send ); char *json_str = cJSON_Print(json_send);
// os_log( "pRoot: %s\r\n", json_str ); // os_log("pRoot: %s\r\n", json_str);
user_send( udp_flag, json_str ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> user_send(udp_flag, json_str); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
free( (void *) json_str ); free((void *) json_str);
} }
cJSON_Delete( json_send ); cJSON_Delete(json_send);
} }
if ( update_user_config_flag ) if (update_user_config_flag)
{ {
mico_system_context_update( sys_config ); mico_system_context_update(sys_config);
update_user_config_flag = false; update_user_config_flag = false;
} }
cJSON_Delete( pJsonRoot ); cJSON_Delete(pJsonRoot);
} }
@@ -214,67 +214,67 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
*udp_flag:<3A><><EFBFBD><EFBFBD>udp/mqtt<74><74>־λ,<2C>˴<EFBFBD><CBB4>޸IJ<DEB8><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬ʱ,<2C><>Ҫʵʱ<CAB5><CAB1><EFBFBD>¸<EFBFBD>domoticz *udp_flag:<3A><><EFBFBD><EFBFBD>udp/mqtt<74><74>־λ,<2C>˴<EFBFBD><CBB4>޸IJ<DEB8><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬ʱ,<2C><>Ҫʵʱ<CAB5><CAB1><EFBFBD>¸<EFBFBD>domoticz
*x:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> *x:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/ */
bool json_plug_analysis( int udp_flag, unsigned char x, cJSON * pJsonRoot, cJSON * pJsonSend ) bool json_plug_analysis(int udp_flag, unsigned char x, cJSON * pJsonRoot, cJSON * pJsonSend)
{ {
if ( !pJsonRoot ) return false; if (!pJsonRoot) return false;
if ( !pJsonSend ) return false; if (!pJsonSend) return false;
char i; char i;
bool return_flag = false; bool return_flag = false;
char plug_str[] = "plug_X"; char plug_str[] = "plug_X";
plug_str[5] = x + '0'; plug_str[5] = x + '0';
cJSON *p_plug = cJSON_GetObjectItem( pJsonRoot, plug_str ); cJSON *p_plug = cJSON_GetObjectItem(pJsonRoot, plug_str);
if ( !p_plug ) return_flag = false; if (!p_plug) return_flag = false;
cJSON *json_plug_send = cJSON_CreateObject( ); cJSON *json_plug_send = cJSON_CreateObject();
//<2F><><EFBFBD><EFBFBD>plug on------------------------------------------------------ //<2F><><EFBFBD><EFBFBD>plug on------------------------------------------------------
if ( p_plug ) if (p_plug)
{ {
cJSON *p_plug_on = cJSON_GetObjectItem( p_plug, "on" ); cJSON *p_plug_on = cJSON_GetObjectItem(p_plug, "on");
if ( p_plug_on ) if (p_plug_on)
{ {
if ( cJSON_IsNumber( p_plug_on ) ) if (cJSON_IsNumber(p_plug_on))
{ {
user_relay_set( x, p_plug_on->valueint ); user_relay_set(x, p_plug_on->valueint);
return_flag = true; return_flag = true;
} }
user_mqtt_send_plug_state( x ); user_mqtt_send_plug_state(x);
} }
//<2F><><EFBFBD><EFBFBD>plug<75><67>setting<6E><67>Ŀ---------------------------------------------- //<2F><><EFBFBD><EFBFBD>plug<75><67>setting<6E><67>Ŀ----------------------------------------------
cJSON *p_plug_setting = cJSON_GetObjectItem( p_plug, "setting" ); cJSON *p_plug_setting = cJSON_GetObjectItem(p_plug, "setting");
if ( p_plug_setting ) if (p_plug_setting)
{ {
cJSON *json_plug_setting_send = cJSON_CreateObject( ); cJSON *json_plug_setting_send = cJSON_CreateObject();
//<2F><><EFBFBD><EFBFBD>plug<75><67>setting<6E><67>name---------------------------------------- //<2F><><EFBFBD><EFBFBD>plug<75><67>setting<6E><67>name----------------------------------------
cJSON *p_plug_setting_name = cJSON_GetObjectItem( p_plug_setting, "name" ); cJSON *p_plug_setting_name = cJSON_GetObjectItem(p_plug_setting, "name");
if ( p_plug_setting_name ) if (p_plug_setting_name)
{ {
if ( cJSON_IsString( p_plug_setting_name ) ) if (cJSON_IsString(p_plug_setting_name))
{ {
return_flag = true; return_flag = true;
sprintf( user_config->plug[x].name, p_plug_setting_name->valuestring ); sprintf(user_config->plug[x].name, p_plug_setting_name->valuestring);
user_mqtt_hass_auto_name( x ); user_mqtt_hass_auto_name(x);
} }
cJSON_AddStringToObject( json_plug_setting_send, "name", user_config->plug[x].name ); cJSON_AddStringToObject(json_plug_setting_send, "name", user_config->plug[x].name);
} }
//<2F><><EFBFBD><EFBFBD>plug<75><67>setting<6E><67>task---------------------------------------- //<2F><><EFBFBD><EFBFBD>plug<75><67>setting<6E><67>task----------------------------------------
for ( i = 0; i < PLUG_TIME_TASK_NUM; i++ ) for (i = 0; i < PLUG_TIME_TASK_NUM; i++)
{ {
if ( json_plug_task_analysis( x, i, p_plug_setting, json_plug_setting_send ) ) if (json_plug_task_analysis(x, i, p_plug_setting, json_plug_setting_send))
return_flag = true; return_flag = true;
} }
cJSON_AddItemToObject( json_plug_send, "setting", json_plug_setting_send ); cJSON_AddItemToObject(json_plug_send, "setting", json_plug_setting_send);
} }
} }
// cJSON *p_nvalue = cJSON_GetObjectItem( pJsonRoot, "nvalue" ); // cJSON *p_nvalue = cJSON_GetObjectItem(pJsonRoot, "nvalue");
// if ( p_plug || p_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 ); cJSON_AddItemToObject(pJsonSend, plug_str, json_plug_send);
return return_flag; return return_flag;
} }
@@ -282,35 +282,35 @@ bool json_plug_analysis( int udp_flag, unsigned char x, cJSON * pJsonRoot, cJSON
*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>json *<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>json
*x:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> y:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> *x:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> y:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/ */
bool json_plug_task_analysis( unsigned char x, unsigned char y, cJSON * pJsonRoot, cJSON * pJsonSend ) bool json_plug_task_analysis(unsigned char x, unsigned char y, cJSON * pJsonRoot, cJSON * pJsonSend)
{ {
if ( !pJsonRoot ) return false; if (!pJsonRoot) return false;
bool return_flag = false; bool return_flag = false;
char plug_task_str[] = "task_X"; char plug_task_str[] = "task_X";
plug_task_str[5] = y + '0'; plug_task_str[5] = y + '0';
cJSON *p_plug_task = cJSON_GetObjectItem( pJsonRoot, plug_task_str ); cJSON *p_plug_task = cJSON_GetObjectItem(pJsonRoot, plug_task_str);
if ( !p_plug_task ) return false; if (!p_plug_task) return false;
cJSON *json_plug_task_send = cJSON_CreateObject( ); cJSON *json_plug_task_send = cJSON_CreateObject();
cJSON *p_plug_task_hour = cJSON_GetObjectItem( p_plug_task, "hour" ); cJSON *p_plug_task_hour = cJSON_GetObjectItem(p_plug_task, "hour");
cJSON *p_plug_task_minute = cJSON_GetObjectItem( p_plug_task, "minute" ); cJSON *p_plug_task_minute = cJSON_GetObjectItem(p_plug_task, "minute");
cJSON *p_plug_task_repeat = cJSON_GetObjectItem( p_plug_task, "repeat" ); cJSON *p_plug_task_repeat = cJSON_GetObjectItem(p_plug_task, "repeat");
cJSON *p_plug_task_action = cJSON_GetObjectItem( p_plug_task, "action" ); cJSON *p_plug_task_action = cJSON_GetObjectItem(p_plug_task, "action");
cJSON *p_plug_task_on = cJSON_GetObjectItem( p_plug_task, "on" ); cJSON *p_plug_task_on = cJSON_GetObjectItem(p_plug_task, "on");
if ( p_plug_task_hour && p_plug_task_minute && p_plug_task_repeat && if (p_plug_task_hour && p_plug_task_minute && p_plug_task_repeat &&
p_plug_task_action p_plug_task_action
&& p_plug_task_on ) && p_plug_task_on)
{ {
if ( cJSON_IsNumber( p_plug_task_hour ) if (cJSON_IsNumber(p_plug_task_hour)
&& cJSON_IsNumber( p_plug_task_minute ) && cJSON_IsNumber(p_plug_task_minute)
&& cJSON_IsNumber( p_plug_task_repeat ) && cJSON_IsNumber(p_plug_task_repeat)
&& cJSON_IsNumber( p_plug_task_action ) && cJSON_IsNumber(p_plug_task_action)
&& cJSON_IsNumber( p_plug_task_on ) && cJSON_IsNumber(p_plug_task_on)
) )
{ {
return_flag = true; return_flag = true;
@@ -322,34 +322,34 @@ bool json_plug_task_analysis( unsigned char x, unsigned char y, cJSON * pJsonRoo
} }
} }
cJSON_AddNumberToObject( json_plug_task_send, "hour", user_config->plug[x].task[y].hour ); cJSON_AddNumberToObject(json_plug_task_send, "hour", user_config->plug[x].task[y].hour);
cJSON_AddNumberToObject( json_plug_task_send, "minute", user_config->plug[x].task[y].minute ); cJSON_AddNumberToObject(json_plug_task_send, "minute", user_config->plug[x].task[y].minute);
cJSON_AddNumberToObject( json_plug_task_send, "repeat", user_config->plug[x].task[y].repeat ); cJSON_AddNumberToObject(json_plug_task_send, "repeat", user_config->plug[x].task[y].repeat);
cJSON_AddNumberToObject( json_plug_task_send, "action", user_config->plug[x].task[y].action ); cJSON_AddNumberToObject(json_plug_task_send, "action", user_config->plug[x].task[y].action);
cJSON_AddNumberToObject( json_plug_task_send, "on", user_config->plug[x].task[y].on ); cJSON_AddNumberToObject(json_plug_task_send, "on", user_config->plug[x].task[y].on);
cJSON_AddItemToObject( pJsonSend, plug_task_str, json_plug_task_send ); cJSON_AddItemToObject(pJsonSend, plug_task_str, json_plug_task_send);
return return_flag; return return_flag;
} }
unsigned char strtohex( char a, char b ) unsigned char strtohex(char a, char b)
{ {
if ( a >= 0x30 && a <= 0x39 ) if (a >= 0x30 && a <= 0x39)
a -= 0x30; a -= 0x30;
else if ( a >= 0x41 && a <= 0x46 ) else if (a >= 0x41 && a <= 0x46)
{ {
a = a + 10 - 0x41; a = a + 10 - 0x41;
} else if ( a >= 0x61 && a <= 0x66 ) } else if (a >= 0x61 && a <= 0x66)
{ {
a = a + 10 - 0x61; a = a + 10 - 0x61;
} }
if ( b >= 0x30 && b <= 0x39 ) if (b >= 0x30 && b <= 0x39)
b -= 0x30; b -= 0x30;
else if ( b >= 0x41 && b <= 0x46 ) else if (b >= 0x41 && b <= 0x46)
{ {
b = b + 10 - 0x41; b = b + 10 - 0x41;
} else if ( b >= 0x61 && b <= 0x66 ) } else if (b >= 0x61 && b <= 0x66)
{ {
b = b + 10 - 0x61; b = b + 10 - 0x61;
} }

View File

@@ -6,7 +6,7 @@
#include "mico.h" #include "mico.h"
#include "micokit_ext.h" #include "micokit_ext.h"
void user_send( int udp_flag, char *s ); void user_send(int udp_flag, char *s);
void user_function_cmd_received(int udp_flag,uint8_t *pusrdata); void user_function_cmd_received(int udp_flag,uint8_t *pusrdata);
unsigned char strtohex(char a, char b); unsigned char strtohex(char a, char b);

View File

@@ -8,22 +8,22 @@
mico_gpio_t relay[Relay_NUM] = { Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5 }; mico_gpio_t relay[Relay_NUM] = { Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5 };
void user_led_set( char x ) void user_led_set(char x)
{ {
if ( x == -1 ) if (x == -1)
MicoGpioOutputTrigger( Led ); MicoGpioOutputTrigger(Led);
else if ( x ) else if (x)
MicoGpioOutputHigh( Led ); MicoGpioOutputHigh(Led);
else else
MicoGpioOutputLow( Led ); MicoGpioOutputLow(Led);
} }
bool relay_out( void ) bool relay_out(void)
{ {
unsigned char i; unsigned char i;
for ( i = 0; i < PLUG_NUM; i++ ) for (i = 0; i < PLUG_NUM; i++)
{ {
if ( user_config->plug[i].on != 0 ) if (user_config->plug[i].on != 0)
{ {
return true; return true;
} }
@@ -90,51 +90,51 @@ void user_relay_set(unsigned char i, unsigned char on)
* y: 0:ȫ<><C8AB><EFBFBD><EFBFBD> 1:ȫ<><C8AB><EFBFBD><EFBFBD> * y: 0:ȫ<><C8AB><EFBFBD><EFBFBD> 1:ȫ<><C8AB><EFBFBD><EFBFBD>
* *
*/ */
void user_relay_set_all( char y ) void user_relay_set_all(char y)
{ {
int i; int i;
for (i = 0; i < PLUG_NUM; i++) for (i = 0; i < PLUG_NUM; i++)
user_relay_set(i, y); user_relay_set(i, y);
} }
static void key_long_press( void ) static void key_long_press(void)
{ {
// os_log("key_long_press"); // os_log("key_long_press");
// user_led_set( 1 ); // user_led_set(1);
// user_mqtt_send( "mqtt test" ); // user_mqtt_send("mqtt test");
} }
static void key_long_10s_press( void ) static void key_long_10s_press(void)
{ {
OSStatus err; OSStatus err;
char i = 0; char i = 0;
os_log( "WARNGIN: user params restored!" ); os_log("WARNGIN: user params restored!");
// for ( i = 0; i < 3; i++ ) // for (i = 0; i < 3; i++)
// { // {
// user_led_set( 1 ); // user_led_set(1);
// mico_rtos_thread_msleep( 100 ); // mico_rtos_thread_msleep(100);
// user_led_set( 0 ); // user_led_set(0);
// } // }
// //
appRestoreDefault_callback( user_config, sizeof(user_config_t) ); appRestoreDefault_callback(user_config, sizeof(user_config_t));
sys_config->micoSystemConfig.ssid[0] = 0; sys_config->micoSystemConfig.ssid[0] = 0;
mico_system_context_update( mico_system_context_get( ) ); mico_system_context_update(mico_system_context_get());
} }
static void key_short_press( void ) static void key_short_press(void)
{ {
char i; char i;
OSStatus err; OSStatus err;
if ( relay_out() ) if (relay_out())
{ {
user_relay_set_all( 0 ); user_relay_set_all(0);
} }
else else
{ {
user_relay_set_all( 1 ); user_relay_set_all(1);
} }
for ( i = 0; i < PLUG_NUM; i++ ) for (i = 0; i < PLUG_NUM; i++)
{ {
user_mqtt_send_plug_state(i); user_mqtt_send_plug_state(i);
} }
@@ -146,42 +146,42 @@ mico_timer_t user_key_timer;
uint16_t key_time = 0; uint16_t key_time = 0;
#define BUTTON_LONG_PRESS_TIME 10 //100ms*10=1s #define BUTTON_LONG_PRESS_TIME 10 //100ms*10=1s
static void key_timeout_handler( void* arg ) static void key_timeout_handler(void* arg)
{ {
static uint8_t key_trigger, key_continue; static uint8_t key_trigger, key_continue;
static uint8_t key_last; static uint8_t key_last;
//<2F><><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
uint8_t tmp = ~(0xfe | MicoGpioInputGet( Button )); uint8_t tmp = ~(0xfe | MicoGpioInputGet(Button));
key_trigger = tmp & (tmp ^ key_continue); key_trigger = tmp & (tmp ^ key_continue);
key_continue = tmp; key_continue = tmp;
// os_log("button scan:%02x %02x",key_trigger,key_continue); // os_log("button scan:%02x %02x",key_trigger,key_continue);
if ( key_trigger != 0 ) key_time = 0; //<2F>°<EFBFBD><C2B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ,<2C><><EFBFBD>¿<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ if (key_trigger != 0) key_time = 0; //<2F>°<EFBFBD><C2B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ,<2C><><EFBFBD>¿<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
if ( key_continue != 0 ) if (key_continue != 0)
{ {
//any button pressed //any button pressed
key_time++; key_time++;
if ( key_time < BUTTON_LONG_PRESS_TIME ) if (key_time < BUTTON_LONG_PRESS_TIME)
key_last = key_continue; key_last = key_continue;
else else
{ {
os_log("button long pressed:%d",key_time); os_log("button long pressed:%d",key_time);
if ( key_time == 30 ) if (key_time == 30)
{ {
key_long_press( ); key_long_press();
} }
else if ( key_time == 100 ) else if (key_time == 100)
{ {
key_long_10s_press( ); key_long_10s_press();
} }
else if ( key_time == 102 ) else if (key_time == 102)
{ {
user_led_set( 1 ); user_led_set(1);
} }
else if ( key_time == 103 ) else if (key_time == 103)
{ {
user_led_set( 0 ); user_led_set(0);
key_time = 101; key_time = 101;
} }
} }
@@ -189,30 +189,30 @@ static void key_timeout_handler( void* arg )
} else } else
{ {
//button released //button released
if ( key_time < BUTTON_LONG_PRESS_TIME ) if (key_time < BUTTON_LONG_PRESS_TIME)
{ //100ms*10=1s <20><><EFBFBD><EFBFBD>1sΪ<73><CEAA><EFBFBD><EFBFBD> { //100ms*10=1s <20><><EFBFBD><EFBFBD>1sΪ<73><CEAA><EFBFBD><EFBFBD>
key_time = 0; key_time = 0;
os_log("button short pressed:%d",key_time); os_log("button short pressed:%d",key_time);
key_short_press( ); key_short_press();
} else if ( key_time > 100 ) } else if (key_time > 100)
{ {
MicoSystemReboot( ); MicoSystemReboot();
} }
key_last = 0; key_last = 0;
mico_rtos_stop_timer( &user_key_timer ); mico_rtos_stop_timer(&user_key_timer);
} }
} }
static void key_falling_irq_handler( void* arg ) static void key_falling_irq_handler(void* arg)
{ {
mico_rtos_start_timer( &user_key_timer ); mico_rtos_start_timer(&user_key_timer);
} }
void key_init( void ) void key_init(void)
{ {
MicoGpioInitialize( Button, INPUT_PULL_UP ); MicoGpioInitialize(Button, INPUT_PULL_UP);
mico_rtos_init_timer( &user_key_timer, 100, key_timeout_handler, NULL ); mico_rtos_init_timer(&user_key_timer, 100, key_timeout_handler, NULL);
MicoGpioEnableIRQ( Button, IRQ_TRIGGER_FALLING_EDGE, key_falling_irq_handler, NULL ); MicoGpioEnableIRQ(Button, IRQ_TRIGGER_FALLING_EDGE, key_falling_irq_handler, NULL);
} }

View File

@@ -8,9 +8,9 @@
void user_led_set(char x); void user_led_set(char x);
void key_init(void); void key_init(void);
void user_relay_set(unsigned char x,unsigned char y ); void user_relay_set(unsigned char x,unsigned char y);
void user_relay_set_all( char y ); void user_relay_set_all(char y);
bool relay_out( void ); bool relay_out(void);
char* get_socket_status(); char* get_socket_status();
#endif #endif

View File

@@ -76,15 +76,15 @@ typedef struct
uint32_t datalen; uint32_t datalen;
} mqtt_recv_msg_t, *p_mqtt_recv_msg_t, mqtt_send_msg_t, *p_mqtt_send_msg_t; } mqtt_recv_msg_t, *p_mqtt_recv_msg_t, mqtt_send_msg_t, *p_mqtt_send_msg_t;
static void mqtt_client_thread( mico_thread_arg_t arg ); static void mqtt_client_thread(mico_thread_arg_t arg);
static void messageArrived( MessageData* md ); static void messageArrived(MessageData* md);
static OSStatus mqtt_msg_publish( Client *c, const char* topic, char qos, char retained, const unsigned char* msg, uint32_t msg_len ); static OSStatus mqtt_msg_publish(Client *c, const char* topic, char qos, char retained, const unsigned char* msg, uint32_t msg_len);
OSStatus user_recv_handler( void *arg ); OSStatus user_recv_handler(void *arg);
OSStatus user_mqtt_send_plug_state( char plug_id ); OSStatus user_mqtt_send_plug_state(char plug_id);
void user_mqtt_hass_auto( char plug_id ); void user_mqtt_hass_auto(char plug_id);
void user_mqtt_hass_auto_power( void ); void user_mqtt_hass_auto_power(void);
bool isconnect = false; bool isconnect = false;
mico_queue_t mqtt_msg_send_queue = NULL; mico_queue_t mqtt_msg_send_queue = NULL;
@@ -100,92 +100,92 @@ char topic_set[MAX_MQTT_TOPIC_SIZE];
mico_timer_t timer_handle; mico_timer_t timer_handle;
static uint8_t timer_status = 0; static uint8_t timer_status = 0;
void user_mqtt_timer_func( void *arg ) void user_mqtt_timer_func(void *arg)
{ {
uint8_t *buf1 = NULL; uint8_t *buf1 = NULL;
LinkStatusTypeDef LinkStatus; LinkStatusTypeDef LinkStatus;
micoWlanGetLinkStatus( &LinkStatus ); micoWlanGetLinkStatus(&LinkStatus);
if ( LinkStatus.is_connected != 1 ) if (LinkStatus.is_connected != 1)
{ {
mico_stop_timer( &timer_handle ); mico_stop_timer(&timer_handle);
return; return;
} }
if ( mico_rtos_is_queue_empty( &mqtt_msg_send_queue ) == true ) if (mico_rtos_is_queue_empty(&mqtt_msg_send_queue) == true)
{ {
timer_status++; timer_status++;
switch ( timer_status ) switch (timer_status)
{ {
case 1: case 1:
user_mqtt_hass_auto_power( ); user_mqtt_hass_auto_power();
break; break;
case 2: case 2:
user_mqtt_hass_auto( 0 ); user_mqtt_hass_auto(0);
break; break;
case 3: case 3:
user_mqtt_hass_auto( 1 ); user_mqtt_hass_auto(1);
break; break;
case 4: case 4:
user_mqtt_hass_auto( 2 ); user_mqtt_hass_auto(2);
break; break;
case 5: case 5:
user_mqtt_hass_auto( 3 ); user_mqtt_hass_auto(3);
break; break;
case 6: case 6:
user_mqtt_hass_auto( 4 ); user_mqtt_hass_auto(4);
break; break;
case 7: case 7:
user_mqtt_hass_auto( 5 ); user_mqtt_hass_auto(5);
break; break;
case 8: case 8:
user_mqtt_hass_auto_name( 0 ); user_mqtt_hass_auto_name(0);
break; break;
case 9: case 9:
user_mqtt_hass_auto_name( 1 ); user_mqtt_hass_auto_name(1);
break; break;
case 10: case 10:
user_mqtt_hass_auto_name( 2 ); user_mqtt_hass_auto_name(2);
break; break;
case 11: case 11:
user_mqtt_hass_auto_name( 3 ); user_mqtt_hass_auto_name(3);
break; break;
case 12: case 12:
user_mqtt_hass_auto_name( 4 ); user_mqtt_hass_auto_name(4);
break; break;
case 13: case 13:
user_mqtt_hass_auto_name( 5 ); user_mqtt_hass_auto_name(5);
break; break;
case 14: case 14:
user_mqtt_hass_auto_power_name( ); user_mqtt_hass_auto_power_name();
break; break;
case 15: case 15:
buf1 = malloc( 1024 ); //idx为1位时长度为24 buf1 = malloc(1024); //idx为1位时长度为24
if ( buf1 != NULL ) if (buf1 != NULL)
{ {
sprintf( sprintf(
buf1, buf1,
"{\"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}}}", "{\"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 ); strMac);
user_function_cmd_received( 0, buf1 ); user_function_cmd_received(0, buf1);
free( buf1 ); free(buf1);
} }
break; break;
default: default:
mico_stop_timer( &timer_handle ); mico_stop_timer(&timer_handle);
// mico_deinit_timer( &timer_handle ); // mico_deinit_timer(&timer_handle);
break; break;
} }
} }
} }
/* Application entrance */ /* Application entrance */
OSStatus user_mqtt_init( void ) OSStatus user_mqtt_init(void)
{ {
OSStatus err = kNoErr; OSStatus err = kNoErr;
sprintf( topic_set, MQTT_CLIENT_SUB_TOPIC1 ); sprintf(topic_set, MQTT_CLIENT_SUB_TOPIC1);
sprintf( topic_state, MQTT_CLIENT_PUB_TOPIC, strMac ); sprintf(topic_state, MQTT_CLIENT_PUB_TOPIC, strMac);
#ifdef MQTT_CLIENT_SSL_ENABLE #ifdef MQTT_CLIENT_SSL_ENABLE
int mqtt_thread_stack_size = 0x3000; int mqtt_thread_stack_size = 0x3000;
@@ -193,39 +193,39 @@ OSStatus user_mqtt_init( void )
//TODO size:0x800 //TODO size:0x800
int mqtt_thread_stack_size = 0x2000; int mqtt_thread_stack_size = 0x2000;
#endif #endif
uint32_t mqtt_lib_version = MQTTClientLibVersion( ); uint32_t mqtt_lib_version = MQTTClientLibVersion();
app_log( "MQTT client version: [%ld.%ld.%ld]", app_log("MQTT client version: [%ld.%ld.%ld]",
0xFF & (mqtt_lib_version >> 16), 0xFF & (mqtt_lib_version >> 8), 0xFF & mqtt_lib_version); 0xFF & (mqtt_lib_version >> 16), 0xFF & (mqtt_lib_version >> 8), 0xFF & mqtt_lib_version);
/* create mqtt msg send queue */ /* create mqtt msg send queue */
err = mico_rtos_init_queue( &mqtt_msg_send_queue, "mqtt_msg_send_queue", sizeof(p_mqtt_send_msg_t), err = mico_rtos_init_queue(&mqtt_msg_send_queue, "mqtt_msg_send_queue", sizeof(p_mqtt_send_msg_t),
MAX_MQTT_SEND_QUEUE_SIZE ); MAX_MQTT_SEND_QUEUE_SIZE);
require_noerr_action( err, exit, app_log("ERROR: create mqtt msg send queue err=%d.", err) ); require_noerr_action(err, exit, app_log("ERROR: create mqtt msg send queue err=%d.", err));
/* start mqtt client */ /* start mqtt client */
err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY, "mqtt_client", err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mqtt_client",
(mico_thread_function_t) mqtt_client_thread, (mico_thread_function_t) mqtt_client_thread,
mqtt_thread_stack_size, 0 ); mqtt_thread_stack_size, 0);
require_noerr_string( err, exit, "ERROR: Unable to start the mqtt client thread." ); require_noerr_string(err, exit, "ERROR: Unable to start the mqtt client thread.");
/* Create a worker thread for user handling MQTT data event */ /* Create a worker thread for user handling MQTT data event */
err = mico_rtos_create_worker_thread( &mqtt_client_worker_thread, MICO_APPLICATION_PRIORITY, 0x800, 5 ); err = mico_rtos_create_worker_thread(&mqtt_client_worker_thread, MICO_APPLICATION_PRIORITY, 0x800, 5);
require_noerr_string( err, exit, "ERROR: Unable to start the mqtt client worker thread." ); require_noerr_string(err, exit, "ERROR: Unable to start the mqtt client worker thread.");
exit: exit:
if ( kNoErr != err ) app_log("ERROR, app thread exit err: %d", err); if (kNoErr != err) app_log("ERROR, app thread exit err: %d", err);
return err; return err;
} }
static OSStatus mqtt_client_release( Client *c, Network *n ) static OSStatus mqtt_client_release(Client *c, Network *n)
{ {
OSStatus err = kNoErr; OSStatus err = kNoErr;
if ( c->isconnected ) MQTTDisconnect( c ); if (c->isconnected) MQTTDisconnect(c);
n->disconnect( n ); // close connection n->disconnect(n); // close connection
if ( MQTT_SUCCESS != MQTTClientDeinit( c ) ) if (MQTT_SUCCESS != MQTTClientDeinit(c))
{ {
app_log("MQTTClientDeinit failed!"); app_log("MQTTClientDeinit failed!");
err = kDeletedErr; err = kDeletedErr;
@@ -234,15 +234,15 @@ static OSStatus mqtt_client_release( Client *c, Network *n )
} }
// publish msg to mqtt server // publish msg to mqtt server
static OSStatus mqtt_msg_publish( Client *c, const char* topic, char qos, char retained, static OSStatus mqtt_msg_publish(Client *c, const char* topic, char qos, char retained,
const unsigned char* msg, const unsigned char* msg,
uint32_t msg_len ) uint32_t msg_len)
{ {
OSStatus err = kUnknownErr; OSStatus err = kUnknownErr;
int ret = 0; int ret = 0;
MQTTMessage publishData = MQTTMessage_publishData_initializer; MQTTMessage publishData = MQTTMessage_publishData_initializer;
require( topic && msg_len && msg, exit ); require(topic && msg_len && msg, exit);
// upload data qos0 // upload data qos0
publishData.qos = (enum QoS) qos; publishData.qos = (enum QoS) qos;
@@ -250,12 +250,12 @@ static OSStatus mqtt_msg_publish( Client *c, const char* topic, char qos, char r
publishData.payload = (void*) msg; publishData.payload = (void*) msg;
publishData.payloadlen = msg_len; publishData.payloadlen = msg_len;
ret = MQTTPublish( c, topic, &publishData ); ret = MQTTPublish(c, topic, &publishData);
if ( MQTT_SUCCESS == ret ) if (MQTT_SUCCESS == ret)
{ {
err = kNoErr; err = kNoErr;
} else if ( MQTT_SOCKET_ERR == ret ) } else if (MQTT_SOCKET_ERR == ret)
{ {
err = kConnectionErr; err = kConnectionErr;
} else } else
@@ -267,7 +267,7 @@ static OSStatus mqtt_msg_publish( Client *c, const char* topic, char qos, char r
return err; return err;
} }
void mqtt_client_thread( mico_thread_arg_t arg ) void mqtt_client_thread(mico_thread_arg_t arg)
{ {
OSStatus err = kUnknownErr; OSStatus err = kUnknownErr;
@@ -284,12 +284,12 @@ void mqtt_client_thread( mico_thread_arg_t arg )
mqtt_log("MQTT client thread started..."); mqtt_log("MQTT client thread started...");
memset( &c, 0, sizeof(c) ); memset(&c, 0, sizeof(c));
memset( &n, 0, sizeof(n) ); memset(&n, 0, sizeof(n));
/* create msg send queue event fd */ /* create msg send queue event fd */
msg_send_event_fd = mico_create_event_fd( mqtt_msg_send_queue ); msg_send_event_fd = mico_create_event_fd(mqtt_msg_send_queue);
require_action( msg_send_event_fd >= 0, exit, mqtt_log("ERROR: create msg send queue event fd failed!!!") ); require_action(msg_send_event_fd >= 0, exit, mqtt_log("ERROR: create msg send queue event fd failed!!!"));
MQTT_start: MQTT_start:
@@ -305,22 +305,22 @@ void mqtt_client_thread( mico_thread_arg_t arg )
ssl_settings.ssl_enable = false; ssl_settings.ssl_enable = false;
#endif #endif
LinkStatusTypeDef LinkStatus; LinkStatusTypeDef LinkStatus;
while ( 1 ) while (1)
{ {
isconnect = false; isconnect = false;
mico_rtos_thread_sleep( 3 ); mico_rtos_thread_sleep(3);
if ( MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1 ) continue; //未配置mqtt服务器时不连接 if (MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1) continue; //未配置mqtt服务器时不连接
micoWlanGetLinkStatus( &LinkStatus ); micoWlanGetLinkStatus(&LinkStatus);
if ( LinkStatus.is_connected != 1 ) if (LinkStatus.is_connected != 1)
{ {
mqtt_log("ERROR:WIFI not connection , waiting 3s for connecting and then connecting MQTT ", rc); mqtt_log("ERROR:WIFI not connection , waiting 3s for connecting and then connecting MQTT ", rc);
mico_rtos_thread_sleep( 3 ); mico_rtos_thread_sleep(3);
continue; continue;
} }
rc = NewNetwork( &n, MQTT_SERVER, MQTT_SERVER_PORT, ssl_settings ); rc = NewNetwork(&n, MQTT_SERVER, MQTT_SERVER_PORT, ssl_settings);
if ( rc == MQTT_SUCCESS ) break; if (rc == MQTT_SUCCESS) break;
mqtt_log("ERROR: MQTT network connection err=%d, reconnect after 3s...", rc); mqtt_log("ERROR: MQTT network connection err=%d, reconnect after 3s...", rc);
} }
@@ -328,8 +328,8 @@ void mqtt_client_thread( mico_thread_arg_t arg )
/* 2. init mqtt client */ /* 2. init mqtt client */
//c.heartbeat_retry_max = 2; //c.heartbeat_retry_max = 2;
rc = MQTTClientInit( &c, &n, MQTT_CMD_TIMEOUT ); rc = MQTTClientInit(&c, &n, MQTT_CMD_TIMEOUT);
require_noerr_string( rc, MQTT_reconnect, "ERROR: MQTT client init err." ); require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client init err.");
mqtt_log("MQTT client init success!"); mqtt_log("MQTT client init success!");
@@ -342,134 +342,134 @@ void mqtt_client_thread( mico_thread_arg_t arg )
connectData.keepAliveInterval = MQTT_CLIENT_KEEPALIVE; connectData.keepAliveInterval = MQTT_CLIENT_KEEPALIVE;
connectData.cleansession = 1; connectData.cleansession = 1;
rc = MQTTConnect( &c, &connectData ); rc = MQTTConnect(&c, &connectData);
require_noerr_string( rc, MQTT_reconnect, "ERROR: MQTT client connect err." ); require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client connect err.");
mqtt_log("MQTT client connect success!"); mqtt_log("MQTT client connect success!");
/* 4. mqtt client subscribe */ /* 4. mqtt client subscribe */
rc = MQTTSubscribe( &c, topic_set, QOS0, messageArrived ); rc = MQTTSubscribe(&c, topic_set, QOS0, messageArrived);
require_noerr_string( rc, MQTT_reconnect, "ERROR: MQTT client subscribe err." ); require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client subscribe err.");
mqtt_log("MQTT client subscribe success! recv_topic=[%s].", topic_set); mqtt_log("MQTT client subscribe success! recv_topic=[%s].", topic_set);
/*4.1 连接成功后先更新发送一次数据*/ /*4.1 连接成功后先更新发送一次数据*/
isconnect = true; isconnect = true;
mico_init_timer( &timer_handle, 150, user_mqtt_timer_func, &arg ); mico_init_timer(&timer_handle, 150, user_mqtt_timer_func, &arg);
mico_start_timer( &timer_handle ); mico_start_timer(&timer_handle);
/* 5. client loop for recv msg && keepalive */ /* 5. client loop for recv msg && keepalive */
while ( 1 ) while (1)
{ {
isconnect = true; isconnect = true;
no_mqtt_msg_exchange = true; no_mqtt_msg_exchange = true;
FD_ZERO( &readfds ); FD_ZERO(&readfds);
FD_SET( c.ipstack->my_socket, &readfds ); FD_SET(c.ipstack->my_socket, &readfds);
FD_SET( msg_send_event_fd, &readfds ); FD_SET(msg_send_event_fd, &readfds);
select( msg_send_event_fd + 1, &readfds, NULL, NULL, &t ); select(msg_send_event_fd + 1, &readfds, NULL, NULL, &t);
/* recv msg from server */ /* recv msg from server */
if ( FD_ISSET( c.ipstack->my_socket, &readfds ) ) if (FD_ISSET(c.ipstack->my_socket, &readfds))
{ {
rc = MQTTYield( &c, (int) MQTT_YIELD_TMIE ); rc = MQTTYield(&c, (int) MQTT_YIELD_TMIE);
require_noerr( rc, MQTT_reconnect ); require_noerr(rc, MQTT_reconnect);
no_mqtt_msg_exchange = false; no_mqtt_msg_exchange = false;
} }
/* recv msg from user worker thread to be sent to server */ /* recv msg from user worker thread to be sent to server */
if ( FD_ISSET( msg_send_event_fd, &readfds ) ) if (FD_ISSET(msg_send_event_fd, &readfds))
{ {
while ( mico_rtos_is_queue_empty( &mqtt_msg_send_queue ) == false ) while (mico_rtos_is_queue_empty(&mqtt_msg_send_queue) == false)
{ {
// get msg from send queue // get msg from send queue
mico_rtos_pop_from_queue( &mqtt_msg_send_queue, &p_send_msg, 0 ); mico_rtos_pop_from_queue(&mqtt_msg_send_queue, &p_send_msg, 0);
require_string( p_send_msg, exit, "Wrong data point" ); require_string(p_send_msg, exit, "Wrong data point");
// send message to server // send message to server
err = mqtt_msg_publish( &c, p_send_msg->topic, p_send_msg->qos, p_send_msg->retained, err = mqtt_msg_publish(&c, p_send_msg->topic, p_send_msg->qos, p_send_msg->retained,
p_send_msg->data, p_send_msg->data,
p_send_msg->datalen ); p_send_msg->datalen);
require_noerr_string( err, MQTT_reconnect, "ERROR: MQTT publish data err" ); require_noerr_string(err, MQTT_reconnect, "ERROR: MQTT publish data err");
mqtt_log("MQTT publish data success! send_topic=[%s], msg=[%ld].\r\n", p_send_msg->topic, p_send_msg->datalen); mqtt_log("MQTT publish data success! send_topic=[%s], msg=[%ld].\r\n", p_send_msg->topic, p_send_msg->datalen);
no_mqtt_msg_exchange = false; no_mqtt_msg_exchange = false;
free( p_send_msg ); free(p_send_msg);
p_send_msg = NULL; p_send_msg = NULL;
} }
} }
/* if no msg exchange, we need to check ping msg to keep alive. */ /* if no msg exchange, we need to check ping msg to keep alive. */
if ( no_mqtt_msg_exchange ) if (no_mqtt_msg_exchange)
{ {
rc = keepalive( &c ); rc = keepalive(&c);
require_noerr_string( rc, MQTT_reconnect, "ERROR: keepalive err" ); require_noerr_string(rc, MQTT_reconnect, "ERROR: keepalive err");
} }
} }
MQTT_reconnect: MQTT_reconnect:
mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d, err = %d", rc, err ); mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d, err = %d", rc, err);
timer_status=100; timer_status=100;
mqtt_client_release( &c, &n ); mqtt_client_release(&c, &n);
isconnect = false; isconnect = false;
user_led_set( -1 ); user_led_set(-1);
mico_rtos_thread_msleep( 100 ); mico_rtos_thread_msleep(100);
user_led_set( -1 ); user_led_set(-1);
mico_rtos_thread_sleep( 5 ); mico_rtos_thread_sleep(5);
goto MQTT_start; goto MQTT_start;
exit: exit:
isconnect = false; isconnect = false;
mqtt_log("EXIT: MQTT client exit with err = %d.", err); mqtt_log("EXIT: MQTT client exit with err = %d.", err);
mqtt_client_release( &c, &n ); mqtt_client_release(&c, &n);
mico_rtos_delete_thread( NULL ); mico_rtos_delete_thread(NULL);
} }
// callback, msg received from mqtt server // callback, msg received from mqtt server
static void messageArrived( MessageData* md ) static void messageArrived(MessageData* md)
{ {
OSStatus err = kUnknownErr; OSStatus err = kUnknownErr;
p_mqtt_recv_msg_t p_recv_msg = NULL; p_mqtt_recv_msg_t p_recv_msg = NULL;
MQTTMessage* message = md->message; MQTTMessage* message = md->message;
p_recv_msg = (p_mqtt_recv_msg_t) calloc( 1, sizeof(mqtt_recv_msg_t) ); p_recv_msg = (p_mqtt_recv_msg_t) calloc(1, sizeof(mqtt_recv_msg_t));
require_action( p_recv_msg, exit, err = kNoMemoryErr ); require_action(p_recv_msg, exit, err = kNoMemoryErr);
p_recv_msg->datalen = message->payloadlen; p_recv_msg->datalen = message->payloadlen;
p_recv_msg->qos = (char) (message->qos); p_recv_msg->qos = (char) (message->qos);
p_recv_msg->retained = message->retained; p_recv_msg->retained = message->retained;
strncpy( p_recv_msg->topic, md->topicName->lenstring.data, md->topicName->lenstring.len ); strncpy(p_recv_msg->topic, md->topicName->lenstring.data, md->topicName->lenstring.len);
memcpy( p_recv_msg->data, message->payload, message->payloadlen ); memcpy(p_recv_msg->data, message->payload, message->payloadlen);
err = mico_rtos_send_asynchronous_event( &mqtt_client_worker_thread, user_recv_handler, p_recv_msg ); err = mico_rtos_send_asynchronous_event(&mqtt_client_worker_thread, user_recv_handler, p_recv_msg);
require_noerr( err, exit ); require_noerr(err, exit);
exit: exit:
if ( err != kNoErr ) if (err != kNoErr)
{ {
app_log("ERROR: Recv data err = %d", err); app_log("ERROR: Recv data err = %d", err);
if ( p_recv_msg ) free( p_recv_msg ); if (p_recv_msg) free(p_recv_msg);
} }
return; return;
} }
/* Application process MQTT received data */ /* Application process MQTT received data */
OSStatus user_recv_handler( void *arg ) OSStatus user_recv_handler(void *arg)
{ {
OSStatus err = kUnknownErr; OSStatus err = kUnknownErr;
p_mqtt_recv_msg_t p_recv_msg = arg; p_mqtt_recv_msg_t p_recv_msg = arg;
require( p_recv_msg, exit ); require(p_recv_msg, exit);
app_log("user get data success! from_topic=[%s], msg=[%ld].\r\n", p_recv_msg->topic, p_recv_msg->datalen); app_log("user get data success! from_topic=[%s], msg=[%ld].\r\n", p_recv_msg->topic, p_recv_msg->datalen);
user_function_cmd_received( 0, p_recv_msg->data ); user_function_cmd_received(0, p_recv_msg->data);
free( p_recv_msg ); free(p_recv_msg);
exit: exit:
return err; return err;
} }
OSStatus user_mqtt_send_topic( char *topic, char *arg, char retained ) OSStatus user_mqtt_send_topic(char *topic, char *arg, char retained)
{ {
OSStatus err = kUnknownErr; OSStatus err = kUnknownErr;
p_mqtt_send_msg_t p_send_msg = NULL; p_mqtt_send_msg_t p_send_msg = NULL;
@@ -477,180 +477,180 @@ OSStatus user_mqtt_send_topic( char *topic, char *arg, char retained )
// app_log("======App prepare to send ![%d]======", MicoGetMemoryInfo()->free_memory); // app_log("======App prepare to send ![%d]======", MicoGetMemoryInfo()->free_memory);
/* Send queue is full, pop the oldest */ /* Send queue is full, pop the oldest */
if ( mico_rtos_is_queue_full( &mqtt_msg_send_queue ) == true ) if (mico_rtos_is_queue_full(&mqtt_msg_send_queue) == true)
{ {
mico_rtos_pop_from_queue( &mqtt_msg_send_queue, &p_send_msg, 0 ); mico_rtos_pop_from_queue(&mqtt_msg_send_queue, &p_send_msg, 0);
free( p_send_msg ); free(p_send_msg);
p_send_msg = NULL; p_send_msg = NULL;
} }
/* Push the latest data into send queue*/ /* Push the latest data into send queue*/
p_send_msg = (p_mqtt_send_msg_t) calloc( 1, sizeof(mqtt_send_msg_t) ); p_send_msg = (p_mqtt_send_msg_t) calloc(1, sizeof(mqtt_send_msg_t));
require_action( p_send_msg, exit, err = kNoMemoryErr ); require_action(p_send_msg, exit, err = kNoMemoryErr);
p_send_msg->qos = 0; p_send_msg->qos = 0;
p_send_msg->retained = retained; p_send_msg->retained = retained;
p_send_msg->datalen = strlen( arg ); p_send_msg->datalen = strlen(arg);
memcpy( p_send_msg->data, arg, p_send_msg->datalen ); memcpy(p_send_msg->data, arg, p_send_msg->datalen);
strncpy( p_send_msg->topic, topic, MAX_MQTT_TOPIC_SIZE ); strncpy(p_send_msg->topic, topic, MAX_MQTT_TOPIC_SIZE);
err = mico_rtos_push_to_queue( &mqtt_msg_send_queue, &p_send_msg, 0 ); err = mico_rtos_push_to_queue(&mqtt_msg_send_queue, &p_send_msg, 0);
require_noerr( err, exit ); require_noerr(err, exit);
//app_log("Push user msg into send queue success!"); //app_log("Push user msg into send queue success!");
exit: exit:
if ( err != kNoErr && p_send_msg ) free( p_send_msg ); if (err != kNoErr && p_send_msg) free(p_send_msg);
return err; return err;
} }
/* Application collect data and seng them to MQTT send queue */ /* Application collect data and seng them to MQTT send queue */
OSStatus user_mqtt_send( char *arg ) OSStatus user_mqtt_send(char *arg)
{ {
return user_mqtt_send_topic( topic_state, arg, 0 ); return user_mqtt_send_topic(topic_state, arg, 0);
} }
//更新ha开关状态 //更新ha开关状态
OSStatus user_mqtt_send_plug_state( char plug_id ) OSStatus user_mqtt_send_plug_state(char plug_id)
{ {
uint8_t *send_buf = NULL; uint8_t *send_buf = NULL;
uint8_t *topic_buf = NULL; uint8_t *topic_buf = NULL;
send_buf = malloc( 64 ); // send_buf = malloc(64); //
topic_buf = malloc( 64 ); // topic_buf = malloc(64); //
if ( send_buf != NULL && topic_buf != NULL ) if (send_buf != NULL && topic_buf != NULL)
{ {
sprintf( topic_buf, "homeassistant/switch/%s/plug_%d/state", strMac, plug_id ); sprintf(topic_buf, "homeassistant/switch/%s/plug_%d/state", strMac, plug_id);
sprintf( send_buf, "{\"mac\":\"%s\",\"plug_%d\":{\"on\":%d}}", strMac, plug_id, user_config->plug[plug_id].on ); sprintf(send_buf, "{\"mac\":\"%s\",\"plug_%d\":{\"on\":%d}}", strMac, plug_id, user_config->plug[plug_id].on);
user_mqtt_send_topic( topic_buf, send_buf, 1 ); user_mqtt_send_topic(topic_buf, send_buf, 1);
} }
if ( send_buf ) free( send_buf ); if (send_buf) free(send_buf);
if ( topic_buf ) free( topic_buf ); if (topic_buf) free(topic_buf);
} }
//hass mqtt自动发现数据开关发送 //hass mqtt自动发现数据开关发送
void user_mqtt_hass_auto( char plug_id ) void user_mqtt_hass_auto(char plug_id)
{ {
uint8_t i; uint8_t i;
uint8_t *send_buf = NULL; uint8_t *send_buf = NULL;
uint8_t *topic_buf = NULL; uint8_t *topic_buf = NULL;
send_buf = malloc( 512 ); // send_buf = malloc(512); //
topic_buf = malloc( 128 ); // topic_buf = malloc(128); //
if ( send_buf != NULL && topic_buf != NULL ) if (send_buf != NULL && topic_buf != NULL)
{ {
sprintf( topic_buf, "homeassistant/switch/%s/plug_%d/config", strMac, plug_id ); sprintf(topic_buf, "homeassistant/switch/%s/plug_%d/config", strMac, plug_id);
sprintf( send_buf, "{" sprintf(send_buf, "{"
"\"name\":\"zTC1_plug%d_%s\"," "\"name\":\"zTC1_plug%d_%s\","
"\"stat_t\":\"homeassistant/switch/%s/plug_%d/state\"," "\"stat_t\":\"homeassistant/switch/%s/plug_%d/state\","
"\"cmd_t\":\"device/ztc1/set\"," "\"cmd_t\":\"device/ztc1/set\","
"\"pl_on\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":1}}\"," "\"pl_on\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":1}}\","
"\"pl_off\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":0}}\"" "\"pl_off\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":0}}\""
"}\0", "}\0",
plug_id, strMac + 8, strMac, plug_id, strMac, plug_id, strMac, plug_id ); plug_id, strMac + 8, strMac, plug_id, strMac, plug_id, strMac, plug_id);
user_mqtt_send_topic( topic_buf, send_buf, 1 ); user_mqtt_send_topic(topic_buf, send_buf, 1);
} }
if ( send_buf ) free( send_buf ); if (send_buf) free(send_buf);
if ( topic_buf ) free( topic_buf ); if (topic_buf) free(topic_buf);
} }
void user_mqtt_hass_auto_name( char plug_id ) void user_mqtt_hass_auto_name(char plug_id)
{ {
uint8_t *send_buf = NULL; uint8_t *send_buf = NULL;
uint8_t *topic_buf = NULL; uint8_t *topic_buf = NULL;
send_buf = (uint8_t *) malloc( 300 ); send_buf = (uint8_t *) malloc(300);
topic_buf = (uint8_t *) malloc( 64 ); topic_buf = (uint8_t *) malloc(64);
if ( send_buf != NULL && topic_buf != NULL ) if (send_buf != NULL && topic_buf != NULL)
{ {
sprintf( topic_buf, "homeassistant/switch/%s/plug_%d/config", strMac, plug_id ); sprintf(topic_buf, "homeassistant/switch/%s/plug_%d/config", strMac, plug_id);
sprintf( send_buf, "{" sprintf(send_buf, "{"
"\"name\":\"%s\"," "\"name\":\"%s\","
"\"stat_t\":\"homeassistant/switch/%s/plug_%d/state\"," "\"stat_t\":\"homeassistant/switch/%s/plug_%d/state\","
"\"cmd_t\":\"device/ztc1/set\"," "\"cmd_t\":\"device/ztc1/set\","
"\"pl_on\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":1}}\"," "\"pl_on\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":1}}\","
"\"pl_off\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":0}}\"" "\"pl_off\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":0}}\""
"}\0", "}\0",
user_config->plug[plug_id].name, strMac, plug_id, strMac, plug_id, strMac, plug_id ); user_config->plug[plug_id].name, strMac, plug_id, strMac, plug_id, strMac, plug_id);
user_mqtt_send_topic( topic_buf, send_buf, 0 ); user_mqtt_send_topic(topic_buf, send_buf, 0);
} }
if ( send_buf ) if (send_buf)
free( send_buf ); free(send_buf);
if ( topic_buf ) if (topic_buf)
free( topic_buf ); free(topic_buf);
} }
//hass mqtt自动发现数据功率发送 //hass mqtt自动发现数据功率发送
void user_mqtt_hass_auto_power( void ) void user_mqtt_hass_auto_power(void)
{ {
uint8_t i; uint8_t i;
uint8_t *send_buf = NULL; uint8_t *send_buf = NULL;
uint8_t *topic_buf = NULL; uint8_t *topic_buf = NULL;
send_buf = malloc( 512 ); // send_buf = malloc(512); //
topic_buf = malloc( 128 ); // topic_buf = malloc(128); //
if ( send_buf != NULL && topic_buf != NULL ) if (send_buf != NULL && topic_buf != NULL)
{ {
sprintf( topic_buf, "homeassistant/sensor/%s/power/config", strMac ); sprintf(topic_buf, "homeassistant/sensor/%s/power/config", strMac);
sprintf( send_buf, "{" sprintf(send_buf, "{"
"\"name\":\"zTC1_power_%s\"," "\"name\":\"zTC1_power_%s\","
"\"state_topic\":\"homeassistant/sensor/%s/power/state\"," "\"state_topic\":\"homeassistant/sensor/%s/power/state\","
"\"unit_of_measurement\":\"W\"," "\"unit_of_measurement\":\"W\","
"\"icon\":\"mdi:gauge\"," "\"icon\":\"mdi:gauge\","
"\"value_template\":\"{{ value_json.power }}\"" "\"value_template\":\"{{ value_json.power }}\""
"}", "}",
strMac + 8, strMac ); strMac + 8, strMac);
user_mqtt_send_topic( topic_buf, send_buf, 1 ); user_mqtt_send_topic(topic_buf, send_buf, 1);
} }
if ( send_buf ) free( send_buf ); if (send_buf) free(send_buf);
if ( topic_buf ) free( topic_buf ); if (topic_buf) free(topic_buf);
} }
void user_mqtt_hass_auto_power_name( void ) void user_mqtt_hass_auto_power_name(void)
{ {
uint8_t *send_buf = NULL; uint8_t *send_buf = NULL;
uint8_t *topic_buf = NULL; uint8_t *topic_buf = NULL;
send_buf = (uint8_t *) malloc( 300 ); // send_buf = (uint8_t *) malloc(300); //
topic_buf = (uint8_t *) malloc( 64 ); // topic_buf = (uint8_t *) malloc(64); //
if ( send_buf != NULL && topic_buf != NULL ) if (send_buf != NULL && topic_buf != NULL)
{ {
sprintf( topic_buf, "homeassistant/sensor/%s/power/config", strMac ); sprintf(topic_buf, "homeassistant/sensor/%s/power/config", strMac);
sprintf( send_buf, "{" sprintf(send_buf, "{"
"\"name\":\"zTC1xxxxxx\"," "\"name\":\"zTC1xxxxxx\","
"\"state_topic\":\"homeassistant/sensor/%s/power/state\"," "\"state_topic\":\"homeassistant/sensor/%s/power/state\","
"\"unit_of_measurement\":\"W\"," "\"unit_of_measurement\":\"W\","
"\"icon\":\"mdi:gauge\"," "\"icon\":\"mdi:gauge\","
"\"value_template\":\"{{ value_json.power }}\"" "\"value_template\":\"{{ value_json.power }}\""
"}", "}",
strMac ); strMac);
send_buf[13] = 0xe5; send_buf[13] = 0xe5;
send_buf[14] = 0x8a; send_buf[14] = 0x8a;
send_buf[15] = 0x9f; send_buf[15] = 0x9f;
send_buf[16] = 0xe7; send_buf[16] = 0xe7;
send_buf[17] = 0x8e; send_buf[17] = 0x8e;
send_buf[18] = 0x87; send_buf[18] = 0x87;
user_mqtt_send_topic( topic_buf, send_buf, 0 ); user_mqtt_send_topic(topic_buf, send_buf, 0);
} }
if ( send_buf ) if (send_buf)
free( send_buf ); free(send_buf);
if ( topic_buf ) if (topic_buf)
free( topic_buf ); free(topic_buf);
} }
void user_mqtt_hass_power( void ) void user_mqtt_hass_power(void)
{ {
uint8_t i; uint8_t i;
uint8_t *send_buf = NULL; uint8_t *send_buf = NULL;
uint8_t *topic_buf = NULL; uint8_t *topic_buf = NULL;
send_buf = malloc( 512 ); // send_buf = malloc(512); //
topic_buf = malloc( 128 ); // topic_buf = malloc(128); //
if ( send_buf != NULL && topic_buf != NULL ) if (send_buf != NULL && topic_buf != NULL)
{ {
sprintf( topic_buf, "homeassistant/sensor/%s/power/state", strMac ); sprintf(topic_buf, "homeassistant/sensor/%s/power/state", strMac);
sprintf( send_buf, "{\"power\":\"%d.%d\"}", power / 10, power % 10 ); sprintf(send_buf, "{\"power\":\"%d.%d\"}", power / 10, power % 10);
user_mqtt_send_topic( topic_buf, send_buf, 0 ); user_mqtt_send_topic(topic_buf, send_buf, 0);
} }
if ( send_buf ) free( send_buf ); if (send_buf) free(send_buf);
if ( topic_buf ) free( topic_buf ); if (topic_buf) free(topic_buf);
} }
bool user_mqtt_isconnect( ) bool user_mqtt_isconnect()
{ {
return isconnect; return isconnect;
} }

View File

@@ -12,12 +12,12 @@
extern OSStatus user_mqtt_init(void); extern OSStatus user_mqtt_init(void);
extern OSStatus user_mqtt_send( char *arg ); extern OSStatus user_mqtt_send(char *arg);
extern bool user_mqtt_isconnect(void); extern bool user_mqtt_isconnect(void);
extern OSStatus user_mqtt_send_plug_state( char plug_id ); extern OSStatus user_mqtt_send_plug_state(char plug_id);
extern void user_mqtt_hass_auto( char plug_id ); extern void user_mqtt_hass_auto(char plug_id);
extern void user_mqtt_hass_auto_name(char plug_id); extern void user_mqtt_hass_auto_name(char plug_id);
extern void user_mqtt_hass_power( void ); extern void user_mqtt_hass_power(void);
extern void user_mqtt_hass_auto_power( void ); extern void user_mqtt_hass_auto_power(void);
extern void user_mqtt_hass_auto_power_name(void); extern void user_mqtt_hass_auto_power_name(void);
#endif #endif

View File

@@ -7,36 +7,36 @@
#include "user_mqtt_client.h" #include "user_mqtt_client.h"
#include "user_function.h" #include "user_function.h"
static void ota_server_status_handler( OTA_STATE_E state, float progress ) static void ota_server_status_handler(OTA_STATE_E state, float progress)
{ {
char str[64] = { 0 }; char str[64] = { 0 };
switch ( state ) switch (state)
{ {
case OTA_LOADING: case OTA_LOADING:
os_log("ota server is loading, progress %.2f%%", progress); os_log("ota server is loading, progress %.2f%%", progress);
if ( ((int) progress)%10 == 1 ) if (((int) progress)%10 == 1)
sprintf( str, "{\"mac\":\"%s\",\"ota_progress\":%d}", strMac,((int) progress) ); sprintf(str, "{\"mac\":\"%s\",\"ota_progress\":%d}", strMac,((int) progress));
break; break;
case OTA_SUCCE: case OTA_SUCCE:
os_log("ota server daemons success"); os_log("ota server daemons success");
sprintf( str, "{\"mac\":\"%s\",\"ota_progress\":100}", strMac ); sprintf(str, "{\"mac\":\"%s\",\"ota_progress\":100}", strMac);
break; break;
case OTA_FAIL: case OTA_FAIL:
os_log("ota server daemons failed"); os_log("ota server daemons failed");
sprintf( str, "{\"mac\":\"%s\",\"ota_progress\":-1}", strMac ); sprintf(str, "{\"mac\":\"%s\",\"ota_progress\":-1}", strMac);
break; break;
default: default:
break; break;
} }
if ( str[0] > 0 ) if (str[0] > 0)
{ {
user_send( true, str ); user_send(true, str);
} }
} }
void user_ota_start( char *url, char *md5 ) void user_ota_start(char *url, char *md5)
{ {
os_log("ready to ota:%s",url); os_log("ready to ota:%s",url);
ota_server_start( url, md5, ota_server_status_handler ); ota_server_start(url, md5, ota_server_status_handler);
} }

View File

@@ -14,22 +14,22 @@ static uint32_t clock_count = 0;
static uint32_t timer_count = 0; static uint32_t timer_count = 0;
static uint32_t timer_irq_count = 0; static uint32_t timer_irq_count = 0;
static void power_timer_handler( void* arg ) static void power_timer_handler(void* arg)
{ {
// uint8_t pin_input = MicoGpioInputGet( POWER ); // uint8_t pin_input = MicoGpioInputGet(POWER);
uint32_t timer = 0; uint32_t timer = 0;
if ( timer_irq_count > 1 ) if (timer_irq_count > 1)
{ {
timer = (clock_count - clock_count_last); timer = (clock_count - clock_count_last);
// os_log("power_irq_handler:%09u %u %u",timer,timer_irq_count,timer_count); // os_log("power_irq_handler:%09u %u %u",timer,timer_irq_count,timer_count);
if ( timer_count > 3 ) if (timer_count > 3)
{ {
timer /= 1000; timer /= 1000;
timer += 4294967; //0xffffffff/1000; timer += 4294967; //0xffffffff/1000;
} else if ( clock_count < clock_count_last ) } else if (clock_count < clock_count_last)
{ {
timer += 0xffffffff; timer += 0xffffffff;
timer /= 1000; timer /= 1000;
@@ -42,11 +42,11 @@ static void power_timer_handler( void* arg )
timer_count++; timer_count++;
} }
// if ( clock_count_last != timer_count ) // if (clock_count_last != timer_count)
// { // {
//// os_log("power_irq_handler:%u-%u=%u",timer_count,clock_count_last,timer); //// os_log("power_irq_handler:%u-%u=%u",timer_count,clock_count_last,timer);
// timer = (timer_count - clock_count_last); // timer = (timer_count - clock_count_last);
// if ( timer_count < clock_count_last ) timer += 0xffffffff; // if (timer_count < clock_count_last) timer += 0xffffffff;
// //
// timer = timer / 1000; // timer = timer / 1000;
// power = 15200000 / timer; // power = 15200000 / timer;
@@ -58,21 +58,21 @@ static void power_timer_handler( void* arg )
// clock_count_last=timer_count; // clock_count_last=timer_count;
} }
static void power_irq_handler( void* arg ) static void power_irq_handler(void* arg)
{ {
clock_count = mico_nanosecond_clock_value( ); clock_count = mico_nanosecond_clock_value();
if ( timer_irq_count == 0 ) clock_count_last = clock_count; if (timer_irq_count == 0) clock_count_last = clock_count;
timer_irq_count++; timer_irq_count++;
} }
void user_power_init( void ) void user_power_init(void)
{ {
os_log("user_power_init"); os_log("user_power_init");
MicoGpioInitialize( POWER, INPUT_PULL_UP ); MicoGpioInitialize(POWER, INPUT_PULL_UP);
mico_rtos_init_timer( &power_timer, 1000, power_timer_handler, NULL ); mico_rtos_init_timer(&power_timer, 1000, power_timer_handler, NULL);
mico_rtos_start_timer( &power_timer ); mico_rtos_start_timer(&power_timer);
MicoGpioEnableIRQ( POWER, IRQ_TRIGGER_FALLING_EDGE, power_irq_handler, NULL ); MicoGpioEnableIRQ(POWER, IRQ_TRIGGER_FALLING_EDGE, power_irq_handler, NULL);
} }

View File

@@ -1,6 +1,6 @@
#ifndef __USER_POWER_H_ #ifndef __USER_POWER_H_
#define __USER_POWER_H_ #define __USER_POWER_H_
void user_power_init( void ); void user_power_init(void);
#endif #endif

View File

@@ -8,9 +8,9 @@
#include "user_mqtt_client.h" #include "user_mqtt_client.h"
#include "user_function.h" #include "user_function.h"
void rtc_thread( mico_thread_arg_t arg ); void rtc_thread(mico_thread_arg_t arg);
OSStatus user_sntp_get_time( ) OSStatus user_sntp_get_time()
{ {
OSStatus err = kNoErr; OSStatus err = kNoErr;
ntp_timestamp_t current_time; ntp_timestamp_t current_time;
@@ -21,56 +21,56 @@ OSStatus user_sntp_get_time( )
// mico_rtc_time_t rtc_time; // mico_rtc_time_t rtc_time;
hostent_content = gethostbyname( "pool.ntp.org" ); hostent_content = gethostbyname("pool.ntp.org");
pptr = hostent_content->h_addr_list; pptr = hostent_content->h_addr_list;
ipp.s_addr = 0xd248912c; ipp.s_addr = 0xd248912c;
err = sntp_get_time( &ipp, &current_time ); err = sntp_get_time(&ipp, &current_time);
if ( err != kNoErr ) if (err != kNoErr)
{ {
os_log("sntp_get_time err = %d.", err); os_log("sntp_get_time err = %d.", err);
ipp.s_addr = *(uint32_t *) (*pptr); ipp.s_addr = *(uint32_t *) (*pptr);
err = sntp_get_time( &ipp, &current_time ); err = sntp_get_time(&ipp, &current_time);
} }
if ( err != kNoErr ) if (err != kNoErr)
{ {
os_log("sntp_get_time0 err = %d.", err); os_log("sntp_get_time0 err = %d.", err);
hostent_content = gethostbyname( "cn.ntp.org.cn" ); hostent_content = gethostbyname("cn.ntp.org.cn");
pptr = hostent_content->h_addr_list; pptr = hostent_content->h_addr_list;
ipp.s_addr = *(uint32_t *) (*pptr); ipp.s_addr = *(uint32_t *) (*pptr);
err = sntp_get_time( &ipp, &current_time ); err = sntp_get_time(&ipp, &current_time);
} }
if ( err != kNoErr ) if (err != kNoErr)
{ {
os_log("sntp_get_time1 err = %d.", err); os_log("sntp_get_time1 err = %d.", err);
hostent_content = gethostbyname( "cn.pool.ntp.org" ); hostent_content = gethostbyname("cn.pool.ntp.org");
pptr = hostent_content->h_addr_list; pptr = hostent_content->h_addr_list;
ipp.s_addr = *(uint32_t *) (*pptr); ipp.s_addr = *(uint32_t *) (*pptr);
err = sntp_get_time( &ipp, &current_time ); err = sntp_get_time(&ipp, &current_time);
} }
if ( err != kNoErr ) if (err != kNoErr)
{ {
os_log("sntp_get_time2 err = %d.", err); os_log("sntp_get_time2 err = %d.", err);
hostent_content = gethostbyname( "s1a.time.edu.cn" ); hostent_content = gethostbyname("s1a.time.edu.cn");
pptr = hostent_content->h_addr_list; pptr = hostent_content->h_addr_list;
ipp.s_addr = *(uint32_t *) (*pptr); ipp.s_addr = *(uint32_t *) (*pptr);
err = sntp_get_time( &ipp, &current_time ); err = sntp_get_time(&ipp, &current_time);
} }
if ( err != kNoErr ) if (err != kNoErr)
{ {
os_log("sntp_get_time3 err = %d.", err); os_log("sntp_get_time3 err = %d.", err);
hostent_content = gethostbyname( "ntp.sjtu.edu.cn" ); hostent_content = gethostbyname("ntp.sjtu.edu.cn");
pptr = hostent_content->h_addr_list; pptr = hostent_content->h_addr_list;
ipp.s_addr = *(uint32_t *) (*pptr); ipp.s_addr = *(uint32_t *) (*pptr);
err = sntp_get_time( &ipp, &current_time ); err = sntp_get_time(&ipp, &current_time);
} }
if ( err == kNoErr ) if (err == kNoErr)
{ {
mico_utc_time_ms_t utc_time_ms = (uint64_t) current_time.seconds * (uint64_t) 1000 mico_utc_time_ms_t utc_time_ms = (uint64_t) current_time.seconds * (uint64_t) 1000
+ (current_time.microseconds / 1000); + (current_time.microseconds / 1000);
mico_time_set_utc_time_ms( &utc_time_ms ); mico_time_set_utc_time_ms(&utc_time_ms);
// mico_utc_time_t utc_time = utc_time_ms / 1000 + 28800; //+8:00 // mico_utc_time_t utc_time = utc_time_ms / 1000 + 28800; //+8:00
// struct tm * currentTime = localtime( (const time_t *) &utc_time ); // struct tm * currentTime = localtime((const time_t *) &utc_time);
// rtc_time.sec = currentTime->tm_sec; // rtc_time.sec = currentTime->tm_sec;
// rtc_time.min = currentTime->tm_min; // rtc_time.min = currentTime->tm_min;
// rtc_time.hr = currentTime->tm_hour; // rtc_time.hr = currentTime->tm_hour;
@@ -78,7 +78,7 @@ OSStatus user_sntp_get_time( )
// rtc_time.weekday = currentTime->tm_wday; // rtc_time.weekday = currentTime->tm_wday;
// rtc_time.month = currentTime->tm_mon + 1; // rtc_time.month = currentTime->tm_mon + 1;
// rtc_time.year = (currentTime->tm_year + 1900) % 100; // rtc_time.year = (currentTime->tm_year + 1900) % 100;
// MicoRtcSetTime( &rtc_time ); // MicoRtcSetTime(&rtc_time);
} }
else else
{ {
@@ -88,7 +88,7 @@ OSStatus user_sntp_get_time( )
return kNoErr; return kNoErr;
} }
OSStatus user_rtc_init( void ) OSStatus user_rtc_init(void)
{ {
OSStatus err = kNoErr; OSStatus err = kNoErr;
@@ -102,26 +102,26 @@ OSStatus user_rtc_init( void )
// rtc_time.month = 1; // rtc_time.month = 1;
// rtc_time.year = 1; // rtc_time.year = 1;
// //
// MicoRtcSetTime( &rtc_time ); // MicoRtcSetTime(&rtc_time);
// /* create mqtt msg send queue */ // /* create mqtt msg send queue */
// err = mico_rtos_init_queue( &mqtt_msg_send_queue, "mqtt_msg_send_queue", sizeof(p_mqtt_send_msg_t), // err = mico_rtos_init_queue(&mqtt_msg_send_queue, "mqtt_msg_send_queue", sizeof(p_mqtt_send_msg_t),
// MAX_MQTT_SEND_QUEUE_SIZE ); // MAX_MQTT_SEND_QUEUE_SIZE);
// require_noerr_action( err, exit, app_log("ERROR: create mqtt msg send queue err=%d.", err) ); // require_noerr_action(err, exit, app_log("ERROR: create mqtt msg send queue err=%d.", err));
/* start rtc client */ /* start rtc client */
err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY, "rtc", err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "rtc",
(mico_thread_function_t) rtc_thread, (mico_thread_function_t) rtc_thread,
0x1000, 0 ); 0x1000, 0);
require_noerr_string( err, exit, "ERROR: Unable to start the rtc thread." ); require_noerr_string(err, exit, "ERROR: Unable to start the rtc thread.");
if ( kNoErr != err ) os_log("ERROR, app thread exit err: %d", err); if (kNoErr != err) os_log("ERROR, app thread exit err: %d", err);
exit: exit:
return err; return err;
} }
void rtc_thread( mico_thread_arg_t arg ) void rtc_thread(mico_thread_arg_t arg)
{ {
int i, j; int i, j;
char task_flag[PLUG_NUM] = { -1, -1, -1, -1, -1, -1 }; //<2F><>¼ÿ<C2BC><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> char task_flag[PLUG_NUM] = { -1, -1, -1, -1, -1, -1 }; //<2F><>¼ÿ<C2BC><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@@ -132,13 +132,13 @@ void rtc_thread( mico_thread_arg_t arg )
mico_utc_time_t utc_time; mico_utc_time_t utc_time;
mico_utc_time_t utc_time_last; mico_utc_time_t utc_time_last;
while ( 1 ) while (1)
{ //<2F>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>wifi<66>ſ<EFBFBD>ʼ<EFBFBD><CABC>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD> { //<2F>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>wifi<66>ſ<EFBFBD>ʼ<EFBFBD><CABC>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>
micoWlanGetLinkStatus( &LinkStatus ); micoWlanGetLinkStatus(&LinkStatus);
if ( LinkStatus.is_connected == 1 ) if (LinkStatus.is_connected == 1)
{ {
err = user_sntp_get_time( ); err = user_sntp_get_time();
if ( err == kNoErr ) if (err == kNoErr)
{ {
os_log("sntp success!"); os_log("sntp success!");
rtc_init = 1; rtc_init = 1;
@@ -146,21 +146,21 @@ void rtc_thread( mico_thread_arg_t arg )
} }
} }
mico_rtos_thread_sleep( 3 ); mico_rtos_thread_sleep(3);
} }
while ( 1 ) while (1)
{ {
mico_time_get_utc_time( &utc_time ); mico_time_get_utc_time(&utc_time);
utc_time += 28800; utc_time += 28800;
if ( utc_time_last != utc_time ) if (utc_time_last != utc_time)
{ {
utc_time_last == utc_time; utc_time_last == utc_time;
total_time++; total_time++;
} }
struct tm * currentTime = localtime( (const time_t *) &utc_time ); struct tm * currentTime = localtime((const time_t *) &utc_time);
rtc_time.sec = currentTime->tm_sec; rtc_time.sec = currentTime->tm_sec;
rtc_time.min = currentTime->tm_min; rtc_time.min = currentTime->tm_min;
rtc_time.hr = currentTime->tm_hour; rtc_time.hr = currentTime->tm_hour;
@@ -170,17 +170,17 @@ void rtc_thread( mico_thread_arg_t arg )
rtc_time.month = currentTime->tm_mon + 1; rtc_time.month = currentTime->tm_mon + 1;
rtc_time.year = (currentTime->tm_year + 1900) % 100; rtc_time.year = (currentTime->tm_year + 1900) % 100;
// MicoRtcSetTime( &rtc_time ); //MicoRtc<74><63><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>ʱ! // MicoRtcSetTime(&rtc_time); //MicoRtc<74><63><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>ʱ!
if ( rtc_time.sec == 0 ) if (rtc_time.sec == 0)
os_log("time:20%02d/%02d/%02d %d %02d:%02d:%02d",rtc_time.year,rtc_time.month,rtc_time.date,rtc_time.weekday,rtc_time.hr,rtc_time.min,rtc_time.sec); os_log("time:20%02d/%02d/%02d %d %02d:%02d:%02d",rtc_time.year,rtc_time.month,rtc_time.date,rtc_time.weekday,rtc_time.hr,rtc_time.min,rtc_time.sec);
char update_user_config_flag = 0; char update_user_config_flag = 0;
for ( i = 0; i < PLUG_NUM; i++ ) for (i = 0; i < PLUG_NUM; i++)
{ {
for ( j = 0; j < PLUG_TIME_TASK_NUM; j++ ) for (j = 0; j < PLUG_TIME_TASK_NUM; j++)
{ {
if ( user_config->plug[i].task[j].on != 0 ) if (user_config->plug[i].task[j].on != 0)
{ {
uint8_t repeat = user_config->plug[i].task[j].repeat; uint8_t repeat = user_config->plug[i].task[j].repeat;
@@ -190,13 +190,13 @@ void rtc_thread( mico_thread_arg_t arg )
&& ((repeat == 0x00) || repeat & (1 << (rtc_time.weekday - 1))) && ((repeat == 0x00) || repeat & (1 << (rtc_time.weekday - 1)))
) )
{ {
if ( user_config->plug[i].on != user_config->plug[i].task[j].action ) if (user_config->plug[i].on != user_config->plug[i].task[j].action)
{ {
user_relay_set( i, user_config->plug[i].task[j].action ); user_relay_set(i, user_config->plug[i].task[j].action);
update_user_config_flag = 1; update_user_config_flag = 1;
user_mqtt_send_plug_state( i ); user_mqtt_send_plug_state(i);
} }
if ( repeat == 0x00 ) if (repeat == 0x00)
{ {
task_flag[i] = j; task_flag[i] = j;
user_config->plug[i].task[j].on = 0; user_config->plug[i].task[j].on = 0;
@@ -208,60 +208,60 @@ void rtc_thread( mico_thread_arg_t arg )
} }
//<2F><><EFBFBD>´<EFBFBD><C2B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD>´<EFBFBD><C2B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if ( update_user_config_flag == 1 ) if (update_user_config_flag == 1)
{ {
os_log("update_user_config_flag"); os_log("update_user_config_flag");
mico_system_context_update( sys_config ); mico_system_context_update(sys_config);
update_user_config_flag = 0; update_user_config_flag = 0;
cJSON *json_send = cJSON_CreateObject( ); cJSON *json_send = cJSON_CreateObject();
cJSON_AddStringToObject( json_send, "mac", strMac ); cJSON_AddStringToObject(json_send, "mac", strMac);
for ( i = 0; i < PLUG_NUM; i++ ) for (i = 0; i < PLUG_NUM; i++)
{ {
char strTemp1[] = "plug_X"; char strTemp1[] = "plug_X";
strTemp1[5] = i + '0'; strTemp1[5] = i + '0';
cJSON *json_send_plug = cJSON_CreateObject( ); cJSON *json_send_plug = cJSON_CreateObject();
cJSON_AddNumberToObject( json_send_plug, "on", user_config->plug[i].on ); cJSON_AddNumberToObject(json_send_plug, "on", user_config->plug[i].on);
if ( task_flag[i] >= 0 ) if (task_flag[i] >= 0)
{ {
cJSON *json_send_plug_setting = cJSON_CreateObject( ); cJSON *json_send_plug_setting = cJSON_CreateObject();
j = task_flag[i]; j = task_flag[i];
char strTemp2[] = "task_X"; char strTemp2[] = "task_X";
strTemp2[5] = j + '0'; strTemp2[5] = j + '0';
cJSON *json_send_plug_task = cJSON_CreateObject( ); cJSON *json_send_plug_task = cJSON_CreateObject();
cJSON_AddNumberToObject( json_send_plug_task, "hour", user_config->plug[i].task[j].hour ); cJSON_AddNumberToObject(json_send_plug_task, "hour", user_config->plug[i].task[j].hour);
cJSON_AddNumberToObject( json_send_plug_task, "minute", user_config->plug[i].task[j].minute ); cJSON_AddNumberToObject(json_send_plug_task, "minute", user_config->plug[i].task[j].minute);
cJSON_AddNumberToObject( json_send_plug_task, "repeat", user_config->plug[i].task[j].repeat ); cJSON_AddNumberToObject(json_send_plug_task, "repeat", user_config->plug[i].task[j].repeat);
cJSON_AddNumberToObject( json_send_plug_task, "action", user_config->plug[i].task[j].action ); cJSON_AddNumberToObject(json_send_plug_task, "action", user_config->plug[i].task[j].action);
cJSON_AddNumberToObject( json_send_plug_task, "on", user_config->plug[i].task[j].on ); cJSON_AddNumberToObject(json_send_plug_task, "on", user_config->plug[i].task[j].on);
cJSON_AddItemToObject( json_send_plug_setting, strTemp2, json_send_plug_task ); cJSON_AddItemToObject(json_send_plug_setting, strTemp2, json_send_plug_task);
cJSON_AddItemToObject( json_send_plug, "setting", json_send_plug_setting ); cJSON_AddItemToObject(json_send_plug, "setting", json_send_plug_setting);
task_flag[i] = -1; task_flag[i] = -1;
} }
cJSON_AddItemToObject( json_send, strTemp1, json_send_plug ); cJSON_AddItemToObject(json_send, strTemp1, json_send_plug);
} }
char *json_str = cJSON_Print( json_send ); char *json_str = cJSON_Print(json_send);
user_send( false, json_str ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> user_send(false, json_str); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
free( json_str ); free(json_str);
cJSON_Delete( json_send ); cJSON_Delete(json_send);
// os_log("cJSON_Delete"); // os_log("cJSON_Delete");
} }
//SNTP<54><50><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿСʱУ׼һ<D7BC><D2BB> //SNTP<54><50><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿСʱУ׼һ<D7BC><D2BB>
if ( rtc_init != 1 || (rtc_time.sec == 0 && rtc_time.min == 0) ) if (rtc_init != 1 || (rtc_time.sec == 0 && rtc_time.min == 0))
{ {
micoWlanGetLinkStatus( &LinkStatus ); micoWlanGetLinkStatus(&LinkStatus);
if ( LinkStatus.is_connected == 1 ) if (LinkStatus.is_connected == 1)
{ {
err = user_sntp_get_time( ); err = user_sntp_get_time();
if ( err == kNoErr ) if (err == kNoErr)
rtc_init = 1; rtc_init = 1;
else else
rtc_init = 2; rtc_init = 2;
@@ -270,11 +270,11 @@ void rtc_thread( mico_thread_arg_t arg )
mico_rtos_thread_msleep( 900 ); mico_rtos_thread_msleep(900);
} }
// exit: // exit:
os_log("EXIT: rtc exit with err = %d.", err); os_log("EXIT: rtc exit with err = %d.", err);
mico_rtos_delete_thread( NULL ); mico_rtos_delete_thread(NULL);
} }

View File

@@ -6,19 +6,19 @@
#include "user_sntp.h" #include "user_sntp.h"
/* Callback function when MiCO UTC time in sync to NTP server */ /* Callback function when MiCO UTC time in sync to NTP server */
static void sntp_time_call_back( void ) static void sntp_time_call_back(void)
{ {
struct tm * currentTime; struct tm * currentTime;
// iso8601_time_t iso8601_time; // iso8601_time_t iso8601_time;
mico_utc_time_t utc_time; mico_utc_time_t utc_time;
mico_rtc_time_t rtc_time; mico_rtc_time_t rtc_time;
// mico_time_get_iso8601_time( &iso8601_time ); // mico_time_get_iso8601_time(&iso8601_time);
// os_log("sntp_time_synced: %.26s", (char*)&iso8601_time); // os_log("sntp_time_synced: %.26s", (char*)&iso8601_time);
mico_time_get_utc_time( &utc_time ); mico_time_get_utc_time(&utc_time);
utc_time+=28800; //+8:00 utc_time+=28800; //+8:00
currentTime = localtime( (const time_t *)&utc_time ); currentTime = localtime((const time_t *)&utc_time);
rtc_time.sec = currentTime->tm_sec; rtc_time.sec = currentTime->tm_sec;
rtc_time.min = currentTime->tm_min; rtc_time.min = currentTime->tm_min;
rtc_time.hr = currentTime->tm_hour; rtc_time.hr = currentTime->tm_hour;
@@ -28,7 +28,7 @@ static void sntp_time_call_back( void )
rtc_time.month = currentTime->tm_mon + 1; rtc_time.month = currentTime->tm_mon + 1;
rtc_time.year = (currentTime->tm_year + 1900) % 100; rtc_time.year = (currentTime->tm_year + 1900) % 100;
MicoRtcSetTime( &rtc_time ); MicoRtcSetTime(&rtc_time);
MicoRtcGetTime(&rtc_time); MicoRtcGetTime(&rtc_time);

View File

@@ -16,19 +16,19 @@ typedef struct
uint8_t data[MAX_UDP_DATA_SIZE]; uint8_t data[MAX_UDP_DATA_SIZE];
} udp_send_msg_t, *p_udp_send_msg_t; } udp_send_msg_t, *p_udp_send_msg_t;
static OSStatus udp_msg_send( int socket, const unsigned char* msg, uint32_t msg_len ); static OSStatus udp_msg_send(int socket, const unsigned char* msg, uint32_t msg_len);
void udp_thread( void *arg ); void udp_thread(void *arg);
OSStatus user_udp_init( void ) OSStatus user_udp_init(void)
{ {
OSStatus err = kNoErr; OSStatus err = kNoErr;
/* start udp client */ /* start udp client */
err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY, "udp", err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "udp",
(mico_thread_function_t) udp_thread, (mico_thread_function_t) udp_thread,
0x1000, 0 ); 0x1000, 0);
require_noerr_string( err, exit, "ERROR: Unable to start the rtc thread." ); require_noerr_string(err, exit, "ERROR: Unable to start the rtc thread.");
if ( kNoErr != err ) os_log("ERROR, app thread exit err: %d", err); if (kNoErr != err) os_log("ERROR, app thread exit err: %d", err);
exit: exit:
return err; return err;
@@ -36,9 +36,9 @@ OSStatus user_udp_init( void )
} }
/*create udp socket*/ /*create udp socket*/
void udp_thread( void *arg ) void udp_thread(void *arg)
{ {
UNUSED_PARAMETER( arg ); UNUSED_PARAMETER(arg);
OSStatus err; OSStatus err;
struct sockaddr_in addr; struct sockaddr_in addr;
@@ -52,99 +52,99 @@ void udp_thread( void *arg )
uint8_t *buf = NULL; uint8_t *buf = NULL;
/* create udp msg send queue */ /* create udp msg send queue */
err = mico_rtos_init_queue( &udp_msg_send_queue, "uqp_msg_send_queue", sizeof(p_udp_send_msg_t), err = mico_rtos_init_queue(&udp_msg_send_queue, "uqp_msg_send_queue", sizeof(p_udp_send_msg_t),
MAX_UDP_SEND_QUEUE_SIZE ); MAX_UDP_SEND_QUEUE_SIZE);
require_noerr_action( err, exit, os_log( "ERROR: create udp msg send queue err=%d.", err ) ); require_noerr_action(err, exit, os_log("ERROR: create udp msg send queue err=%d.", err));
/* create msg send queue event fd */ /* create msg send queue event fd */
msg_send_event_fd = mico_create_event_fd( udp_msg_send_queue ); msg_send_event_fd = mico_create_event_fd(udp_msg_send_queue);
require_action( msg_send_event_fd >= 0, exit, os_log( "ERROR: create msg send queue event fd failed!!!" ) ); require_action(msg_send_event_fd >= 0, exit, os_log("ERROR: create msg send queue event fd failed!!!"));
buf = malloc( 1024 ); buf = malloc(1024);
require_action( buf, exit, err = kNoMemoryErr ); require_action(buf, exit, err = kNoMemoryErr);
/*Establish a UDP port to receive any data sent to this port*/ /*Establish a UDP port to receive any data sent to this port*/
udp_fd = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ); udp_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
require_action( IsValidSocket( udp_fd ), exit, err = kNoResourcesErr ); require_action(IsValidSocket(udp_fd), exit, err = kNoResourcesErr);
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY; addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons( LOCAL_UDP_PORT ); addr.sin_port = htons(LOCAL_UDP_PORT);
err = bind( udp_fd, (struct sockaddr *) &addr, sizeof(addr) ); err = bind(udp_fd, (struct sockaddr *) &addr, sizeof(addr));
require_noerr( err, exit ); require_noerr(err, exit);
os_log("Open local UDP port %d", LOCAL_UDP_PORT); os_log("Open local UDP port %d", LOCAL_UDP_PORT);
while ( 1 ) while (1)
{ {
FD_ZERO( &readfds ); FD_ZERO(&readfds);
FD_SET( msg_send_event_fd, &readfds ); FD_SET(msg_send_event_fd, &readfds);
FD_SET( udp_fd, &readfds ); FD_SET(udp_fd, &readfds);
select( udp_fd + 1, &readfds, NULL, NULL, NULL ); select(udp_fd + 1, &readfds, NULL, NULL, NULL);
/*Read data from udp and send data back */ /*Read data from udp and send data back */
if ( FD_ISSET( udp_fd, &readfds ) ) if (FD_ISSET(udp_fd, &readfds))
{ {
len = recvfrom( udp_fd, buf, 1024, 0, (struct sockaddr *) &addr, &addrLen ); len = recvfrom(udp_fd, buf, 1024, 0, (struct sockaddr *) &addr, &addrLen);
require_action( len >= 0, exit, err = kConnectionErr ); require_action(len >= 0, exit, err = kConnectionErr);
strcpy( ip_address, inet_ntoa( addr.sin_addr ) ); strcpy(ip_address, inet_ntoa(addr.sin_addr));
if(len<1024) buf[len]=0; if(len<1024) buf[len]=0;
os_log( "udp recv from %s:%d, len:%d ", ip_address,addr.sin_port, len ); os_log("udp recv from %s:%d, len:%d ", ip_address,addr.sin_port, len);
user_function_cmd_received(1,buf); user_function_cmd_received(1,buf);
// sendto( udp_fd, buf, len, 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in) ); // sendto(udp_fd, buf, len, 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
} }
/* recv msg from user worker thread to be sent to server */ /* recv msg from user worker thread to be sent to server */
if ( FD_ISSET( msg_send_event_fd, &readfds ) ) if (FD_ISSET(msg_send_event_fd, &readfds))
{ {
while ( mico_rtos_is_queue_empty( &udp_msg_send_queue ) == false ) while (mico_rtos_is_queue_empty(&udp_msg_send_queue) == false)
{ {
// get msg from send queue // get msg from send queue
mico_rtos_pop_from_queue( &udp_msg_send_queue, &p_send_msg, 0 ); mico_rtos_pop_from_queue(&udp_msg_send_queue, &p_send_msg, 0);
require_string( p_send_msg, exit, "Wrong data point" ); require_string(p_send_msg, exit, "Wrong data point");
// send message to server // send message to server
err = udp_msg_send( udp_fd, p_send_msg->data, p_send_msg->datalen ); err = udp_msg_send(udp_fd, p_send_msg->data, p_send_msg->datalen);
// require_noerr_string( err, MQTT_reconnect, "ERROR: udp publish data err" ); // require_noerr_string(err, MQTT_reconnect, "ERROR: udp publish data err");
os_log( "udp send data success! msg=[%ld].\r\n", p_send_msg->datalen); os_log("udp send data success! msg=[%ld].\r\n", p_send_msg->datalen);
free( p_send_msg ); free(p_send_msg);
p_send_msg = NULL; p_send_msg = NULL;
} }
} }
} }
exit: exit:
if ( err != kNoErr ) if (err != kNoErr)
os_log("UDP thread exit with err: %d", err); os_log("UDP thread exit with err: %d", err);
if ( buf != NULL ) free( buf ); if (buf != NULL) free(buf);
mico_rtos_delete_thread( NULL ); mico_rtos_delete_thread(NULL);
} }
// send msg to udp // send msg to udp
static OSStatus udp_msg_send( int socket, const unsigned char* msg, uint32_t msg_len ) static OSStatus udp_msg_send(int socket, const unsigned char* msg, uint32_t msg_len)
{ {
OSStatus err = kUnknownErr; OSStatus err = kUnknownErr;
int ret = 0; int ret = 0;
require( msg_len && msg, exit ); require(msg_len && msg, exit);
struct sockaddr_in addr; struct sockaddr_in addr;
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY; addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons( LOCAL_UDP_PORT ); addr.sin_port = htons(LOCAL_UDP_PORT);
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_BROADCAST; addr.sin_addr.s_addr = INADDR_BROADCAST;
addr.sin_port = htons( REMOTE_UDP_PORT ); addr.sin_port = htons(REMOTE_UDP_PORT);
/*the receiver should bind at port=20000*/ /*the receiver should bind at port=20000*/
sendto( socket, msg, msg_len, 0, (struct sockaddr *) &addr, sizeof(addr) ); sendto(socket, msg, msg_len, 0, (struct sockaddr *) &addr, sizeof(addr));
exit: exit:
return err; return err;
} }
/* Application collect data and seng them to udp send queue */ /* Application collect data and seng them to udp send queue */
OSStatus user_udp_send( char *arg ) OSStatus user_udp_send(char *arg)
{ {
OSStatus err = kUnknownErr; OSStatus err = kUnknownErr;
p_udp_send_msg_t p_send_msg = NULL; p_udp_send_msg_t p_send_msg = NULL;
@@ -152,27 +152,27 @@ OSStatus user_udp_send( char *arg )
// app_log("======App prepare to send ![%d]======", MicoGetMemoryInfo()->free_memory); // app_log("======App prepare to send ![%d]======", MicoGetMemoryInfo()->free_memory);
/* Send queue is full, pop the oldest */ /* Send queue is full, pop the oldest */
if ( mico_rtos_is_queue_full( &udp_msg_send_queue ) == true ) if (mico_rtos_is_queue_full(&udp_msg_send_queue) == true)
{ {
mico_rtos_pop_from_queue( &udp_msg_send_queue, &p_send_msg, 0 ); mico_rtos_pop_from_queue(&udp_msg_send_queue, &p_send_msg, 0);
free( p_send_msg ); free(p_send_msg);
p_send_msg = NULL; p_send_msg = NULL;
} }
/* Push the latest data into send queue*/ /* Push the latest data into send queue*/
p_send_msg = calloc( 1, sizeof(udp_send_msg_t) ); p_send_msg = calloc(1, sizeof(udp_send_msg_t));
require_action( p_send_msg, exit, err = kNoMemoryErr ); require_action(p_send_msg, exit, err = kNoMemoryErr);
p_send_msg->datalen = strlen( arg ); p_send_msg->datalen = strlen(arg);
memcpy( p_send_msg->data, arg, p_send_msg->datalen ); memcpy(p_send_msg->data, arg, p_send_msg->datalen);
err = mico_rtos_push_to_queue( &udp_msg_send_queue, &p_send_msg, 0 ); err = mico_rtos_push_to_queue(&udp_msg_send_queue, &p_send_msg, 0);
require_noerr( err, exit ); require_noerr(err, exit);
//app_log("Push user msg into send queue success!"); //app_log("Push user msg into send queue success!");
exit: exit:
if ( err != kNoErr && p_send_msg ) free( p_send_msg ); if (err != kNoErr && p_send_msg) free(p_send_msg);
return err; return err;
} }

View File

@@ -6,8 +6,8 @@
#include "mico.h" #include "mico.h"
#include "micokit_ext.h" #include "micokit_ext.h"
OSStatus user_udp_init( void ); OSStatus user_udp_init(void);
OSStatus user_udp_send( char *arg ); OSStatus user_udp_send(char *arg);

View File

@@ -11,110 +11,110 @@ char wifi_status = WIFI_STATE_NOCONNECT;
mico_timer_t wifi_led_timer; mico_timer_t wifi_led_timer;
static void wifi_connect_sys_config( void ) static void wifi_connect_sys_config(void)
{ {
if ( strlen( sys_config->micoSystemConfig.ssid ) > 0 ) if (strlen(sys_config->micoSystemConfig.ssid) > 0)
{ {
os_log("connect ssid:%s key:%s",sys_config->micoSystemConfig.ssid,sys_config->micoSystemConfig.user_key); os_log("connect ssid:%s key:%s",sys_config->micoSystemConfig.ssid,sys_config->micoSystemConfig.user_key);
network_InitTypeDef_st wNetConfig; network_InitTypeDef_st wNetConfig;
memset( &wNetConfig, 0, sizeof(network_InitTypeDef_st) ); memset(&wNetConfig, 0, sizeof(network_InitTypeDef_st));
strcpy( wNetConfig.wifi_ssid, sys_config->micoSystemConfig.ssid ); strcpy(wNetConfig.wifi_ssid, sys_config->micoSystemConfig.ssid);
strcpy( wNetConfig.wifi_key, sys_config->micoSystemConfig.user_key ); strcpy(wNetConfig.wifi_key, sys_config->micoSystemConfig.user_key);
wNetConfig.wifi_mode = Station; wNetConfig.wifi_mode = Station;
wNetConfig.dhcpMode = DHCP_Client; wNetConfig.dhcpMode = DHCP_Client;
wNetConfig.wifi_retry_interval = 6000; wNetConfig.wifi_retry_interval = 6000;
micoWlanStart( &wNetConfig ); micoWlanStart(&wNetConfig);
wifi_status = WIFI_STATE_CONNECTING; wifi_status = WIFI_STATE_CONNECTING;
} else } else
wifi_status = WIFI_STATE_FAIL; wifi_status = WIFI_STATE_FAIL;
} }
void wifi_start_easylink( ) void wifi_start_easylink()
{ {
wifi_status = WIFI_STATE_EASYLINK; wifi_status = WIFI_STATE_EASYLINK;
micoWlanStartEasyLink( 20000 ); micoWlanStartEasyLink(20000);
user_led_set( 1 ); user_led_set(1);
} }
//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)
{ {
os_log("wifi_easylink_wps_completed_handle:"); os_log("wifi_easylink_wps_completed_handle:");
if ( nwkpara == NULL ) if (nwkpara == NULL)
{ {
os_log("EasyLink fail"); os_log("EasyLink fail");
micoWlanStopEasyLink( ); micoWlanStopEasyLink();
return; return;
} }
os_log("ssid:\"%s\",\"%s\"",nwkpara->wifi_ssid,nwkpara->wifi_key); os_log("ssid:\"%s\",\"%s\"",nwkpara->wifi_ssid,nwkpara->wifi_key);
//<2F><><EFBFBD><EFBFBD>wifi<66><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD>wifi<66><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
strcpy( sys_config->micoSystemConfig.ssid, nwkpara->wifi_ssid ); strcpy(sys_config->micoSystemConfig.ssid, nwkpara->wifi_ssid);
strcpy( sys_config->micoSystemConfig.user_key, nwkpara->wifi_key ); strcpy(sys_config->micoSystemConfig.user_key, nwkpara->wifi_key);
sys_config->micoSystemConfig.user_keyLength = strlen( nwkpara->wifi_key ); sys_config->micoSystemConfig.user_keyLength = strlen(nwkpara->wifi_key);
mico_system_context_update( sys_config ); mico_system_context_update(sys_config);
wifi_status = WIFI_STATE_NOCONNECT; wifi_status = WIFI_STATE_NOCONNECT;
os_log("EasyLink stop"); os_log("EasyLink stop");
micoWlanStopEasyLink( ); micoWlanStopEasyLink();
} }
//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", pnet->ip); os_log("got IP:%s", pnet->ip);
wifi_status = WIFI_STATE_CONNECTED; wifi_status = WIFI_STATE_CONNECTED;
user_function_cmd_received(1,"{\"cmd\":\"device report\"}"); user_function_cmd_received(1,"{\"cmd\":\"device report\"}");
} }
//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)
{ {
if ( status == NOTIFY_STATION_UP ) //wifi<66><69><EFBFBD>ӳɹ<D3B3> if (status == NOTIFY_STATION_UP) //wifi<66><69><EFBFBD>ӳɹ<D3B3>
{ {
//wifi_status = WIFI_STATE_CONNECTED; //wifi_status = WIFI_STATE_CONNECTED;
} else if ( status == NOTIFY_STATION_DOWN ) //wifi<66>Ͽ<EFBFBD> } else if (status == NOTIFY_STATION_DOWN) //wifi<66>Ͽ<EFBFBD>
{ {
wifi_status = WIFI_STATE_NOCONNECT; wifi_status = WIFI_STATE_NOCONNECT;
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);
} }
} }
//100ms<6D><73>ʱ<EFBFBD><CAB1><EFBFBD>ص<EFBFBD> //100ms<6D><73>ʱ<EFBFBD><CAB1><EFBFBD>ص<EFBFBD>
static void wifi_led_timer_callback( void* arg ) static void wifi_led_timer_callback(void* arg)
{ {
static unsigned int num = 0; static unsigned int num = 0;
num++; num++;
switch ( wifi_status ) switch (wifi_status)
{ {
case WIFI_STATE_FAIL: case WIFI_STATE_FAIL:
os_log("wifi connect fail"); os_log("wifi connect fail");
user_led_set( 0 ); user_led_set(0);
mico_rtos_stop_timer( &wifi_led_timer ); mico_rtos_stop_timer(&wifi_led_timer);
break; break;
case WIFI_STATE_NOCONNECT: case WIFI_STATE_NOCONNECT:
//wifi_connect_sys_config( ); //wifi_connect_sys_config();
break; break;
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();
break; break;
case WIFI_STATE_EASYLINK: case WIFI_STATE_EASYLINK:
user_led_set( 1 ); user_led_set(1);
break; break;
case WIFI_STATE_CONNECTED: case WIFI_STATE_CONNECTED:
user_led_set( 0 ); user_led_set(0);
mico_rtos_stop_timer( &wifi_led_timer ); mico_rtos_stop_timer(&wifi_led_timer);
if ( relay_out( ) ) if (relay_out())
user_led_set( 1 ); user_led_set(1);
else else
user_led_set( 0 ); user_led_set(0);
break; break;
} }
} }
@@ -133,30 +133,30 @@ void wifi_connect(char* wifi_ssid, char* wifi_key)
micoWlanStart(&wNetConfig); micoWlanStart(&wNetConfig);
//<2F><><EFBFBD><EFBFBD>wifi<66><69><EFBFBD><EFBFBD><EFBFBD>뵽Flash //<2F><><EFBFBD><EFBFBD>wifi<66><69><EFBFBD><EFBFBD><EFBFBD>뵽Flash
strcpy(sys_config->micoSystemConfig.ssid, nwkpara->wifi_ssid); strcpy(sys_config->micoSystemConfig.ssid, wifi_ssid);
strcpy(sys_config->micoSystemConfig.user_key, nwkpara->wifi_key); strcpy(sys_config->micoSystemConfig.user_key, wifi_key);
sys_config->micoSystemConfig.user_keyLength = strlen(nwkpara->wifi_key); sys_config->micoSystemConfig.user_keyLength = strlen(wifi_key);
mico_system_context_update(sys_config); mico_system_context_update(sys_config);
wifi_status = WIFI_STATE_NOCONNECT; wifi_status = WIFI_STATE_NOCONNECT;
} }
void wifi_init( void ) void wifi_init(void)
{ {
//wifi״̬<D7B4><CCAC>led<65><64>˸<EFBFBD><CBB8>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC> //wifi״̬<D7B4><CCAC>led<65><64>˸<EFBFBD><CBB8>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
mico_rtos_init_timer( &wifi_led_timer, 100, (void *) wifi_led_timer_callback, NULL ); mico_rtos_init_timer(&wifi_led_timer, 100, (void *) wifi_led_timer_callback, NULL);
//easylink <20><><EFBFBD>ɻص<C9BB> //easylink <20><><EFBFBD>ɻص<C9BB>
mico_system_notify_register( mico_notify_EASYLINK_WPS_COMPLETED, (void *) wifi_easylink_completed_handle, NULL ); mico_system_notify_register(mico_notify_EASYLINK_WPS_COMPLETED, (void *) wifi_easylink_completed_handle, NULL);
//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>
mico_system_notify_register( mico_notify_DHCP_COMPLETED, (void *) wifi_get_ip_callback, NULL ); mico_system_notify_register(mico_notify_DHCP_COMPLETED, (void *) wifi_get_ip_callback, NULL);
//wifi<66><69><EFBFBD><EFBFBD>״̬<D7B4>ı<EFBFBD><C4B1>ص<EFBFBD> //wifi<66><69><EFBFBD><EFBFBD>״̬<D7B4>ı<EFBFBD><C4B1>ص<EFBFBD>
mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void*) wifi_status_callback, NULL ); mico_system_notify_register(mico_notify_WIFI_STATUS_CHANGED, (void*) wifi_status_callback, NULL);
//sntp_init(); //sntp_init();
//<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; IPStatusTypedef para;
micoWlanGetIPStatus( &para, Station ); micoWlanGetIPStatus(&para, Station);
strcpy( strMac, para.mac ); strcpy(strMac, para.mac);
} }