mirror of
https://github.com/oopuuu/zTC1.git
synced 2025-12-12 04:58:17 +08:00
(*) -> ()
This commit is contained in:
@@ -73,10 +73,10 @@ static int http_get_socket_status(httpd_request_t *req)
|
||||
char* status = get_socket_status();
|
||||
|
||||
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));
|
||||
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:
|
||||
return err;
|
||||
@@ -188,7 +188,7 @@ static int _app_httpd_start()
|
||||
/*Initialize HTTPD*/
|
||||
if(is_http_init == false) {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -202,12 +202,12 @@ exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
int app_httpd_start( void )
|
||||
int app_httpd_start(void)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
err = _app_httpd_start();
|
||||
require_noerr( err, exit );
|
||||
require_noerr(err, exit);
|
||||
|
||||
if (is_handlers_registered == false) {
|
||||
app_http_register_handlers();
|
||||
@@ -225,7 +225,7 @@ int app_httpd_stop()
|
||||
/* HTTPD and services */
|
||||
app_httpd_log("stopping down httpd");
|
||||
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:
|
||||
return err;
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
int app_httpd_start( void );
|
||||
int app_httpd_start(void);
|
||||
|
||||
int app_httpd_stop();
|
||||
|
||||
|
||||
126
TC1/main.c
126
TC1/main.c
@@ -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 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;
|
||||
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[1] = 0;
|
||||
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;
|
||||
|
||||
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->version = USER_CONFIG_VERSION;
|
||||
for ( i = 0; i < PLUG_NUM; i++ )
|
||||
for (i = 0; i < PLUG_NUM; i++)
|
||||
{
|
||||
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[7] = 0;
|
||||
|
||||
// sprintf( userConfigDefault->plug[i].name, "<22><><EFBFBD><EFBFBD>%d", i );//<2F><><EFBFBD><EFBFBD><EFBFBD>쳣
|
||||
for ( j = 0; j < PLUG_TIME_TASK_NUM; j++ )
|
||||
// sprintf(userConfigDefault->plug[i].name, "<22><><EFBFBD><EFBFBD>%d", i);//<2F><><EFBFBD><EFBFBD><EFBFBD>쳣
|
||||
for (j = 0; j < PLUG_TIME_TASK_NUM; j++)
|
||||
{
|
||||
userConfigDefault->plug[i].task[j].hour = 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;
|
||||
}
|
||||
}
|
||||
// mico_system_context_update( sys_config );
|
||||
// mico_system_context_update(sys_config);
|
||||
|
||||
}
|
||||
|
||||
int application_start( void )
|
||||
int application_start(void)
|
||||
{
|
||||
int i;
|
||||
os_log( "Start %s",VERSION );
|
||||
os_log("Start %s",VERSION);
|
||||
|
||||
uint8_t main_num=0;
|
||||
uint32_t power_last = 0xffffffff;
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
// for ( i = 0; i < Relay_NUM; i++ )
|
||||
// for (i = 0; i < Relay_NUM; i++)
|
||||
// {
|
||||
// MicoGpioOutputLow( Relay[(i)] );
|
||||
// MicoGpioInitialize( Relay[i], OUTPUT_PUSH_PULL );
|
||||
// MicoGpioOutputLow( Relay[(i)] );
|
||||
// MicoGpioOutputLow(Relay[(i)]);
|
||||
// MicoGpioInitialize(Relay[i], OUTPUT_PUSH_PULL);
|
||||
// MicoGpioOutputLow(Relay[(i)]);
|
||||
// MicoGpioOutputHigh(Relay[i]);
|
||||
// }
|
||||
/* 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;
|
||||
require_action( user_config, exit, err = kNoMemoryErr );
|
||||
require_action(user_config, exit, err = kNoMemoryErr);
|
||||
|
||||
err = mico_system_init( sys_config );
|
||||
require_noerr( err, exit );
|
||||
err = mico_system_init(sys_config);
|
||||
require_noerr(err, exit);
|
||||
|
||||
MicoGpioInitialize( (mico_gpio_t) Button, INPUT_PULL_UP );
|
||||
if ( !MicoGpioInputGet( Button ) )
|
||||
MicoGpioInitialize((mico_gpio_t) Button, INPUT_PULL_UP);
|
||||
if (!MicoGpioInputGet(Button))
|
||||
{ //<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
|
||||
}
|
||||
|
||||
MicoGpioInitialize( (mico_gpio_t) Led, OUTPUT_PUSH_PULL );
|
||||
for ( i = 0; i < Relay_NUM; i++ )
|
||||
MicoGpioInitialize((mico_gpio_t) Led, OUTPUT_PUSH_PULL);
|
||||
for (i = 0; i < Relay_NUM; i++)
|
||||
{
|
||||
MicoGpioInitialize( Relay[i], OUTPUT_PUSH_PULL );
|
||||
user_relay_set( i, user_config->plug[i].on );
|
||||
MicoGpioInitialize(Relay[i], OUTPUT_PUSH_PULL);
|
||||
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!" );
|
||||
err = mico_system_context_restore( sys_config );
|
||||
require_noerr( err, exit );
|
||||
os_log("WARNGIN: user params restored!");
|
||||
err = mico_system_context_restore(sys_config);
|
||||
require_noerr(err, exit);
|
||||
}
|
||||
|
||||
if ( sys_config->micoSystemConfig.name[0] == 1 )
|
||||
if (sys_config->micoSystemConfig.name[0] == 1)
|
||||
{
|
||||
IPStatusTypedef para;
|
||||
os_log( "micoWlanGetIPStatus:%d", micoWlanGetIPStatus( ¶, Station )); //mac<61><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>0??!!!
|
||||
strcpy( strMac, para.mac );
|
||||
os_log( "result:%s",strMac );
|
||||
os_log( "result:%s",para.mac );
|
||||
os_log("micoWlanGetIPStatus:%d", micoWlanGetIPStatus(¶, Station)); //mac<61><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>0??!!!
|
||||
strcpy(strMac, para.mac);
|
||||
os_log("result:%s",strMac);
|
||||
os_log("result:%s",para.mac);
|
||||
|
||||
|
||||
unsigned char mac1, mac2;
|
||||
mac1 = strtohex( strMac[8], strMac[9] );
|
||||
mac2 = strtohex( strMac[10], strMac[11] );
|
||||
os_log( "strtohex:0x%02x%02x",mac1,mac2 );
|
||||
sprintf( sys_config->micoSystemConfig.name, ZTC1_NAME, mac1, mac2 );
|
||||
mac1 = strtohex(strMac[8], strMac[9]);
|
||||
mac2 = strtohex(strMac[10], strMac[11]);
|
||||
os_log("strtohex:0x%02x%02x",mac1,mac2);
|
||||
sprintf(sys_config->micoSystemConfig.name, ZTC1_NAME, mac1, mac2);
|
||||
}
|
||||
|
||||
os_log( "user:%s",user_config->user );
|
||||
os_log( "device name:%s",sys_config->micoSystemConfig.name );
|
||||
os_log( "mqtt_ip:%s",user_config->mqtt_ip );
|
||||
os_log( "mqtt_port:%d",user_config->mqtt_port );
|
||||
os_log( "mqtt_user:%s",user_config->mqtt_user );
|
||||
os_log( "mqtt_password:%s",user_config->mqtt_password );
|
||||
os_log("user:%s",user_config->user);
|
||||
os_log("device name:%s",sys_config->micoSystemConfig.name);
|
||||
os_log("mqtt_ip:%s",user_config->mqtt_ip);
|
||||
os_log("mqtt_port:%d",user_config->mqtt_port);
|
||||
os_log("mqtt_user:%s",user_config->mqtt_user);
|
||||
os_log("mqtt_password:%s",user_config->mqtt_password);
|
||||
|
||||
os_log( "version:%d",user_config->version );
|
||||
// for ( i = 0; i < PLUG_NUM; i++ )
|
||||
os_log("version:%d",user_config->version);
|
||||
// for (i = 0; i < PLUG_NUM; i++)
|
||||
// {
|
||||
// os_log("plug_%d:",i);
|
||||
// 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,
|
||||
// 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>
|
||||
ap_init();
|
||||
wifi_init( );
|
||||
user_udp_init( );
|
||||
key_init( );
|
||||
err = user_mqtt_init( );
|
||||
require_noerr( err, exit );
|
||||
err = user_rtc_init( );
|
||||
require_noerr( err, exit );
|
||||
wifi_init();
|
||||
user_udp_init();
|
||||
key_init();
|
||||
err = user_mqtt_init();
|
||||
require_noerr(err, exit);
|
||||
err = user_rtc_init();
|
||||
require_noerr(err, exit);
|
||||
user_power_init();
|
||||
|
||||
/* start http server thread */
|
||||
app_httpd_start();
|
||||
while ( 1 )
|
||||
while (1)
|
||||
{
|
||||
main_num++;
|
||||
//<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;
|
||||
main_num =0;
|
||||
uint8_t *power_buf = NULL;
|
||||
power_buf = malloc( 128 );
|
||||
if ( power_buf != NULL )
|
||||
power_buf = malloc(128);
|
||||
if (power_buf != NULL)
|
||||
{
|
||||
sprintf( power_buf, "{\"mac\":\"%s\",\"power\":\"%d.%d\",\"total_time\":%d}", strMac, power / 10, power % 10, total_time );
|
||||
user_send( 0, power_buf );
|
||||
free( power_buf );
|
||||
sprintf(power_buf, "{\"mac\":\"%s\",\"power\":\"%d.%d\",\"total_time\":%d}", strMac, power / 10, power % 10, total_time);
|
||||
user_send(0, power_buf);
|
||||
free(power_buf);
|
||||
}
|
||||
user_mqtt_hass_power( );
|
||||
user_mqtt_hass_power();
|
||||
}
|
||||
mico_thread_msleep(1000);
|
||||
|
||||
|
||||
@@ -48,11 +48,11 @@ static CRC16_Context crc_context;
|
||||
static md5_context md5;
|
||||
static uint32_t offset = 0;
|
||||
|
||||
static OSStatus onReceivedData( struct _HTTPHeader_t * httpHeader,
|
||||
static OSStatus onReceivedData(struct _HTTPHeader_t * httpHeader,
|
||||
uint32_t pos,
|
||||
uint8_t *data,
|
||||
size_t len,
|
||||
void * userContext );
|
||||
void * userContext);
|
||||
|
||||
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;
|
||||
for(i=0; i<len; i++)
|
||||
{
|
||||
if( (*(str+i) >= 'A') && (*(str+i) <= 'Z') ){
|
||||
if((*(str+i) >= 'A') && (*(str+i) <= 'Z')){
|
||||
*(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;
|
||||
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP ){
|
||||
res = send( ota_server_context->download_url.ota_fd, data, datalen, 0 );
|
||||
if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP){
|
||||
res = send(ota_server_context->download_url.ota_fd, data, datalen, 0);
|
||||
}
|
||||
#if OTA_USE_HTTPS
|
||||
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS ){
|
||||
res = ssl_send( ota_server_context->download_url.ota_ssl, data, datalen);
|
||||
if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS){
|
||||
res = ssl_send(ota_server_context->download_url.ota_ssl, data, datalen);
|
||||
}
|
||||
#endif
|
||||
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;
|
||||
#if OTA_USE_HTTPS
|
||||
int ssl_errno = 0;
|
||||
#endif
|
||||
|
||||
err = connect( ota_server_context->download_url.ota_fd, (struct sockaddr *)addr, addrlen );
|
||||
require_noerr_string( err, exit, "ERROR: connect ota server failed" );
|
||||
err = connect(ota_server_context->download_url.ota_fd, (struct sockaddr *)addr, addrlen);
|
||||
require_noerr_string(err, exit, "ERROR: connect ota server failed");
|
||||
|
||||
#if OTA_USE_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 );
|
||||
require_action_string( ota_server_context->download_url.ota_ssl != NULL, exit, err = kConnectionErr,"ERROR: ssl disconnect" );
|
||||
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);
|
||||
require_action_string(ota_server_context->download_url.ota_ssl != NULL, exit, err = kConnectionErr,"ERROR: ssl disconnect");
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -108,95 +108,95 @@ exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
static int ota_server_read_header( HTTPHeader_t *httpHeader )
|
||||
static int ota_server_read_header(HTTPHeader_t *httpHeader)
|
||||
{
|
||||
int res = 0;
|
||||
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP ){
|
||||
res = SocketReadHTTPHeader( ota_server_context->download_url.ota_fd, httpHeader );
|
||||
if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP){
|
||||
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
|
||||
res = SocketReadHTTPSHeader( ota_server_context->download_url.ota_ssl, httpHeader );
|
||||
res = SocketReadHTTPSHeader(ota_server_context->download_url.ota_ssl, httpHeader);
|
||||
#endif
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static int ota_server_read_body( HTTPHeader_t *httpHeader )
|
||||
static int ota_server_read_body(HTTPHeader_t *httpHeader)
|
||||
{
|
||||
int res = 0;
|
||||
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP ){
|
||||
res = SocketReadHTTPBody( ota_server_context->download_url.ota_fd, httpHeader );
|
||||
if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP){
|
||||
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
|
||||
res = SocketReadHTTPSBody( ota_server_context->download_url.ota_ssl, httpHeader );
|
||||
res = SocketReadHTTPSBody(ota_server_context->download_url.ota_ssl, httpHeader);
|
||||
#endif
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static int ota_server_send_header( void )
|
||||
static int ota_server_send_header(void)
|
||||
{
|
||||
char *header = NULL;
|
||||
int j = 0;
|
||||
int ret = 0;
|
||||
header = malloc( OTA_SEND_HEAD_SIZE );
|
||||
memset( header, 0x00, OTA_SEND_HEAD_SIZE );
|
||||
header = malloc(OTA_SEND_HEAD_SIZE);
|
||||
memset(header, 0x00, OTA_SEND_HEAD_SIZE);
|
||||
|
||||
j = sprintf( header, "GET " );
|
||||
j += sprintf( header + j, "/%s HTTP/1.1\r\n", ota_server_context->download_url.url );
|
||||
j = sprintf(header, "GET ");
|
||||
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
|
||||
{
|
||||
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
|
||||
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,
|
||||
ota_server_context->download_state.download_end_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);
|
||||
} 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);
|
||||
if ( header != NULL ) free( header );
|
||||
if (header != NULL) free(header);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void ota_server_socket_close( void )
|
||||
static void ota_server_socket_close(void)
|
||||
{
|
||||
#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
|
||||
SocketClose( &(ota_server_context->download_url.ota_fd) );
|
||||
SocketClose(&(ota_server_context->download_url.ota_fd));
|
||||
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;
|
||||
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);
|
||||
} 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_addr = in_addr;
|
||||
|
||||
err = ota_server_connect( &server_address, sizeof(server_address) );
|
||||
if ( err != 0 )
|
||||
err = ota_server_connect(&server_address, sizeof(server_address));
|
||||
if (err != 0)
|
||||
{
|
||||
mico_thread_sleep( 1 );
|
||||
mico_thread_sleep(1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -222,17 +222,17 @@ static int ota_server_connect_server( struct in_addr in_addr )
|
||||
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;
|
||||
|
||||
progress =(float) ota_server_context->download_state.download_begin_pos / ota_server_context->download_state.download_len;
|
||||
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);
|
||||
}
|
||||
|
||||
static void ota_server_thread( mico_thread_arg_t arg )
|
||||
static void ota_server_thread(mico_thread_arg_t arg)
|
||||
{
|
||||
OSStatus err;
|
||||
uint16_t crc16 = 0;
|
||||
@@ -243,65 +243,65 @@ static void ota_server_thread( mico_thread_arg_t arg )
|
||||
char **pptr = NULL;
|
||||
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;
|
||||
|
||||
hostent_content = gethostbyname( ota_server_context->download_url.host );
|
||||
require_action_quiet( hostent_content != NULL, DELETE, ota_server_progress_set(OTA_FAIL));
|
||||
hostent_content = gethostbyname(ota_server_context->download_url.host);
|
||||
require_action_quiet(hostent_content != NULL, DELETE, ota_server_progress_set(OTA_FAIL));
|
||||
pptr=hostent_content->h_addr_list;
|
||||
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);
|
||||
|
||||
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 );
|
||||
if( ota_server_context->ota_check.is_md5 == true ){
|
||||
InitMd5( &md5 );
|
||||
CRC16_Init(&crc_context);
|
||||
if(ota_server_context->ota_check.is_md5 == true){
|
||||
InitMd5(&md5);
|
||||
}
|
||||
|
||||
httpHeader = HTTPHeaderCreateWithCallback( 1024, onReceivedData, NULL, NULL );
|
||||
require_action( httpHeader, DELETE, ota_server_progress_set(OTA_FAIL) );
|
||||
httpHeader = HTTPHeaderCreateWithCallback(1024, onReceivedData, NULL, NULL);
|
||||
require_action(httpHeader, DELETE, ota_server_progress_set(OTA_FAIL));
|
||||
|
||||
while ( 1 )
|
||||
while (1)
|
||||
{
|
||||
if ( ota_server_context->ota_control == OTA_CONTROL_PAUSE ){
|
||||
mico_thread_sleep( 1 );
|
||||
if (ota_server_context->ota_control == OTA_CONTROL_PAUSE){
|
||||
mico_thread_sleep(1);
|
||||
continue;
|
||||
}else if( ota_server_context->ota_control == OTA_CONTROL_STOP ){
|
||||
}else if(ota_server_context->ota_control == OTA_CONTROL_STOP){
|
||||
goto DELETE;
|
||||
}
|
||||
|
||||
ota_server_context->download_url.ota_fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
|
||||
err = ota_server_connect_server( in_addr );
|
||||
require_noerr_action( err, RECONNECTED, ota_server_progress_set(OTA_FAIL));
|
||||
ota_server_context->download_url.ota_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
err = ota_server_connect_server(in_addr);
|
||||
require_noerr_action(err, RECONNECTED, ota_server_progress_set(OTA_FAIL));
|
||||
|
||||
/* Send HTTP Request */
|
||||
ota_server_send_header( );
|
||||
ota_server_send_header();
|
||||
|
||||
FD_ZERO( &readfds );
|
||||
FD_SET( ota_server_context->download_url.ota_fd, &readfds );
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(ota_server_context->download_url.ota_fd, &readfds);
|
||||
|
||||
select( ota_server_context->download_url.ota_fd + 1, &readfds, NULL, NULL, NULL );
|
||||
if ( FD_ISSET( ota_server_context->download_url.ota_fd, &readfds ) )
|
||||
select(ota_server_context->download_url.ota_fd + 1, &readfds, NULL, NULL, NULL);
|
||||
if (FD_ISSET(ota_server_context->download_url.ota_fd, &readfds))
|
||||
{
|
||||
/*parse header*/
|
||||
err = ota_server_read_header( httpHeader );
|
||||
if ( ota_server_context->ota_control == OTA_CONTROL_START )
|
||||
err = ota_server_read_header(httpHeader);
|
||||
if (ota_server_context->ota_control == OTA_CONTROL_START)
|
||||
{
|
||||
ota_server_context->download_state.download_len = httpHeader->contentLength;
|
||||
ota_server_context->ota_control = OTA_CONTROL_CONTINUE;
|
||||
}
|
||||
switch ( err )
|
||||
switch (err)
|
||||
{
|
||||
case kNoErr:
|
||||
#if OTA_DEBUG
|
||||
PrintHTTPHeader( httpHeader );
|
||||
PrintHTTPHeader(httpHeader);
|
||||
#endif
|
||||
err = ota_server_read_body( httpHeader );/*get body data*/
|
||||
require_noerr( err, RECONNECTED );
|
||||
err = ota_server_read_body(httpHeader);/*get body data*/
|
||||
require_noerr(err, RECONNECTED);
|
||||
/*get data and print*/
|
||||
break;
|
||||
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);
|
||||
goto DELETE;
|
||||
}
|
||||
CRC16_Final( &crc_context, &crc16 );
|
||||
if( ota_server_context->ota_check.is_md5 == true ){
|
||||
Md5Final( &md5, (unsigned char *) md5_value );
|
||||
CRC16_Final(&crc_context, &crc16);
|
||||
if(ota_server_context->ota_check.is_md5 == true){
|
||||
Md5Final(&md5, (unsigned char *) md5_value);
|
||||
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);
|
||||
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_ota_switch_to_new_fw(ota_server_context->download_state.download_len, crc16);
|
||||
mico_system_power_perform(mico_system_context_get(), eState_Software_Reset);
|
||||
}else{
|
||||
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);
|
||||
@@ -336,16 +336,16 @@ static void ota_server_thread( mico_thread_arg_t arg )
|
||||
}
|
||||
|
||||
RECONNECTED:
|
||||
ota_server_socket_close( );
|
||||
ota_server_socket_close();
|
||||
mico_thread_sleep(2);
|
||||
continue;
|
||||
|
||||
}
|
||||
DELETE:
|
||||
HTTPHeaderDestory( &httpHeader );
|
||||
ota_server_socket_close( );
|
||||
if( ota_server_context != NULL ){
|
||||
if( ota_server_context->download_url.url != NULL ){
|
||||
HTTPHeaderDestory(&httpHeader);
|
||||
ota_server_socket_close();
|
||||
if(ota_server_context != NULL){
|
||||
if(ota_server_context->download_url.url != NULL){
|
||||
free(ota_server_context->download_url.url);
|
||||
ota_server_context->download_url.url = NULL;
|
||||
}
|
||||
@@ -358,52 +358,52 @@ DELETE:
|
||||
}
|
||||
|
||||
/*one request may receive multi reply*/
|
||||
static OSStatus onReceivedData( struct _HTTPHeader_t * inHeader, uint32_t inPos, uint8_t * inData,
|
||||
size_t inLen, void * inUserContext )
|
||||
static OSStatus onReceivedData(struct _HTTPHeader_t * inHeader, uint32_t inPos, uint8_t * inData,
|
||||
size_t inLen, void * inUserContext)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
if ( inLen == 0 )
|
||||
if (inLen == 0)
|
||||
return err;
|
||||
|
||||
ota_server_context->download_state.download_begin_pos += inLen;
|
||||
|
||||
CRC16_Update( &crc_context, inData, inLen );
|
||||
if( ota_server_context->ota_check.is_md5 == true ){
|
||||
Md5Update( &md5, inData, inLen );
|
||||
CRC16_Update(&crc_context, inData, inLen);
|
||||
if(ota_server_context->ota_check.is_md5 == true){
|
||||
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);
|
||||
|
||||
if( ota_server_context->ota_control == OTA_CONTROL_PAUSE ){
|
||||
while( 1 ){
|
||||
if( ota_server_context->ota_control != OTA_CONTROL_PAUSE )
|
||||
if(ota_server_context->ota_control == OTA_CONTROL_PAUSE){
|
||||
while(1){
|
||||
if(ota_server_context->ota_control != OTA_CONTROL_PAUSE)
|
||||
break;
|
||||
mico_thread_msleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
if( ota_server_context->ota_control == OTA_CONTROL_STOP ){
|
||||
if(ota_server_context->ota_control == OTA_CONTROL_STOP){
|
||||
err = kUnsupportedErr;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static OSStatus ota_server_set_url( char *url )
|
||||
static OSStatus ota_server_set_url(char *url)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
url_field_t *url_t;
|
||||
char *pos = NULL;
|
||||
|
||||
url_t = url_parse( url );
|
||||
url_t = url_parse(url);
|
||||
require_action(url, exit, err = kParamErr);
|
||||
#if OTA_DEBUG
|
||||
url_field_print( url_t );
|
||||
url_field_print(url_t);
|
||||
#endif
|
||||
if ( !strcmp( url_t->schema, "https" ) )
|
||||
if (!strcmp(url_t->schema, "https"))
|
||||
{
|
||||
ota_server_context->download_url.HTTP_SECURITY = HTTP_SECURITY_HTTPS;
|
||||
} else
|
||||
@@ -411,30 +411,30 @@ static OSStatus ota_server_set_url( char *url )
|
||||
ota_server_context->download_url.HTTP_SECURITY = HTTP_SECURITY_HTTP;
|
||||
}
|
||||
|
||||
strcpy( ota_server_context->download_url.host, url_t->host );
|
||||
ota_server_context->download_url.port = atoi( url_t->port );
|
||||
pos = strstr( url, url_t->path );
|
||||
if ( pos == NULL )
|
||||
strcpy(ota_server_context->download_url.host, url_t->host);
|
||||
ota_server_context->download_url.port = atoi(url_t->port);
|
||||
pos = strstr(url, url_t->path);
|
||||
if (pos == NULL)
|
||||
{
|
||||
strcpy( ota_server_context->download_url.url, "" );
|
||||
strcpy(ota_server_context->download_url.url, "");
|
||||
} else
|
||||
{
|
||||
strcpy( ota_server_context->download_url.url, pos );
|
||||
strcpy(ota_server_context->download_url.url, pos);
|
||||
}
|
||||
|
||||
exit:
|
||||
url_free( url_t );
|
||||
url_free(url_t);
|
||||
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;
|
||||
|
||||
require_action(url, exit, err = kParamErr);
|
||||
|
||||
if( ota_server_context != NULL ){
|
||||
if( ota_server_context->download_url.url != NULL ){
|
||||
if(ota_server_context != NULL){
|
||||
if(ota_server_context->download_url.url != NULL){
|
||||
free(ota_server_context->download_url.url);
|
||||
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);
|
||||
require_noerr(err, exit);
|
||||
|
||||
if( md5 != NULL ){
|
||||
if(md5 != NULL){
|
||||
ota_server_context->ota_check.is_md5 = true;
|
||||
memcpy(ota_server_context->ota_check.md5, 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;
|
||||
|
||||
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:
|
||||
return err;
|
||||
}
|
||||
|
||||
void ota_server_pause( void )
|
||||
void ota_server_pause(void)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
void ota_server_stop( void )
|
||||
void ota_server_stop(void)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -109,34 +109,34 @@ typedef struct _ota_server_context_t{
|
||||
* @return kNoErr : on success.
|
||||
* @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
|
||||
*
|
||||
* @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
|
||||
*
|
||||
* @return No
|
||||
*/
|
||||
void ota_server_pause( void );
|
||||
void ota_server_pause(void);
|
||||
|
||||
|
||||
/** @brief Continue OTA server daemons
|
||||
*
|
||||
* @return No
|
||||
*/
|
||||
void ota_server_continue( void );
|
||||
void ota_server_continue(void);
|
||||
|
||||
|
||||
/** @brief Stop OTA server daemons
|
||||
*
|
||||
* @return No
|
||||
*/
|
||||
void ota_server_stop( void );
|
||||
void ota_server_stop(void);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -11,201 +11,201 @@
|
||||
|
||||
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_task_analysis( unsigned char x, unsigned char y, 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);
|
||||
|
||||
void user_send( int udp_flag, char *s )
|
||||
void user_send(int udp_flag, char *s)
|
||||
{
|
||||
if ( udp_flag || !user_mqtt_isconnect( ) )
|
||||
user_udp_send( s ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
if (udp_flag || !user_mqtt_isconnect())
|
||||
user_udp_send(s); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
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;
|
||||
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>
|
||||
|
||||
cJSON * pJsonRoot = cJSON_Parse( pusrdata );
|
||||
if ( !pJsonRoot )
|
||||
cJSON * pJsonRoot = cJSON_Parse(pusrdata);
|
||||
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;
|
||||
}
|
||||
|
||||
//<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" );
|
||||
if ( p_cmd && cJSON_IsString( p_cmd ) && strcmp( p_cmd->valuestring, "device report" ) == 0 )
|
||||
cJSON *p_cmd = cJSON_GetObjectItem(pJsonRoot, "cmd");
|
||||
if (p_cmd && cJSON_IsString(p_cmd) && strcmp(p_cmd->valuestring, "device report") == 0)
|
||||
{
|
||||
cJSON *pRoot = cJSON_CreateObject( );
|
||||
cJSON_AddStringToObject( pRoot, "name", sys_config->micoSystemConfig.name );
|
||||
cJSON_AddStringToObject( pRoot, "mac", strMac );
|
||||
cJSON_AddNumberToObject( pRoot, "type", TYPE );
|
||||
cJSON_AddStringToObject( pRoot, "type_name", TYPE_NAME );
|
||||
cJSON *pRoot = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(pRoot, "name", sys_config->micoSystemConfig.name);
|
||||
cJSON_AddStringToObject(pRoot, "mac", strMac);
|
||||
cJSON_AddNumberToObject(pRoot, "type", TYPE);
|
||||
cJSON_AddStringToObject(pRoot, "type_name", TYPE_NAME);
|
||||
|
||||
IPStatusTypedef para;
|
||||
micoWlanGetIPStatus( ¶, Station );
|
||||
cJSON_AddStringToObject( pRoot, "ip", para.ip );
|
||||
micoWlanGetIPStatus(¶, Station);
|
||||
cJSON_AddStringToObject(pRoot, "ip", para.ip);
|
||||
|
||||
char *s = cJSON_Print( pRoot );
|
||||
// os_log( "pRoot: %s\r\n", s );
|
||||
user_send( udp_flag, s ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
free( (void *) s );
|
||||
cJSON_Delete( pRoot );
|
||||
char *s = cJSON_Print(pRoot);
|
||||
// os_log("pRoot: %s\r\n", s);
|
||||
user_send(udp_flag, s); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
free((void *) s);
|
||||
cJSON_Delete(pRoot);
|
||||
// cJSON_Delete(p_cmd);
|
||||
}
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEB2BF>
|
||||
cJSON *p_name = cJSON_GetObjectItem( pJsonRoot, "name" );
|
||||
cJSON *p_mac = cJSON_GetObjectItem( pJsonRoot, "mac" );
|
||||
cJSON *p_name = cJSON_GetObjectItem(pJsonRoot, "name");
|
||||
cJSON *p_mac = cJSON_GetObjectItem(pJsonRoot, "mac");
|
||||
|
||||
//<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
|
||||
|| (p_mac && cJSON_IsString( p_mac ) && strcmp( p_mac->valuestring, strMac ) == 0) //mac
|
||||
)
|
||||
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
|
||||
)
|
||||
{
|
||||
cJSON *json_send = cJSON_CreateObject( );
|
||||
cJSON_AddStringToObject( json_send, "mac", strMac );
|
||||
cJSON *json_send = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(json_send, "mac", strMac);
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// cJSON *p_cmd = cJSON_GetObjectItem( pJsonRoot, "name" );
|
||||
if(p_cmd && cJSON_IsString( p_cmd ) && strcmp( p_cmd->valuestring, "restart" ) == 0)
|
||||
// cJSON *p_cmd = cJSON_GetObjectItem(pJsonRoot, "name");
|
||||
if(p_cmd && cJSON_IsString(p_cmd) && strcmp(p_cmd->valuestring, "restart") == 0)
|
||||
{
|
||||
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>汾
|
||||
cJSON *p_version = cJSON_GetObjectItem( pJsonRoot, "version" );
|
||||
if ( p_version )
|
||||
cJSON *p_version = cJSON_GetObjectItem(pJsonRoot, "version");
|
||||
if (p_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>
|
||||
cJSON *p_total_time = cJSON_GetObjectItem( pJsonRoot, "total_time" );
|
||||
if ( p_total_time )
|
||||
cJSON *p_total_time = cJSON_GetObjectItem(pJsonRoot, "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>
|
||||
cJSON *p_power = cJSON_GetObjectItem( pJsonRoot, "power" );
|
||||
if ( p_power )
|
||||
cJSON *p_power = cJSON_GetObjectItem(pJsonRoot, "power");
|
||||
if (p_power)
|
||||
{
|
||||
uint8_t *temp_buf = malloc( 16 );
|
||||
if ( temp_buf != NULL )
|
||||
uint8_t *temp_buf = malloc(16);
|
||||
if (temp_buf != NULL)
|
||||
{
|
||||
sprintf( temp_buf, "%d.%d", power / 10, power % 10 );
|
||||
cJSON_AddStringToObject( json_send, "power", temp_buf );
|
||||
free( temp_buf );
|
||||
sprintf(temp_buf, "%d.%d", power / 10, power % 10);
|
||||
cJSON_AddStringToObject(json_send, "power", temp_buf);
|
||||
free(temp_buf);
|
||||
}
|
||||
os_log("power:%d",power);
|
||||
}
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>setting-----------------------------------------------------------------
|
||||
cJSON *p_setting = cJSON_GetObjectItem( pJsonRoot, "setting" );
|
||||
if ( p_setting )
|
||||
cJSON *p_setting = cJSON_GetObjectItem(pJsonRoot, "setting");
|
||||
if (p_setting)
|
||||
{
|
||||
//<2F><><EFBFBD><EFBFBD>ota
|
||||
cJSON *p_ota = cJSON_GetObjectItem( p_setting, "ota" );
|
||||
if ( p_ota )
|
||||
cJSON *p_ota = cJSON_GetObjectItem(p_setting, "ota");
|
||||
if (p_ota)
|
||||
{
|
||||
if ( cJSON_IsString( p_ota ) )
|
||||
user_ota_start( p_ota->valuestring, NULL );
|
||||
if (cJSON_IsString(p_ota))
|
||||
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
|
||||
cJSON *p_setting_name = cJSON_GetObjectItem( p_setting, "name" );
|
||||
if ( p_setting_name && cJSON_IsString( p_setting_name ) )
|
||||
cJSON *p_setting_name = cJSON_GetObjectItem(p_setting, "name");
|
||||
if (p_setting_name && cJSON_IsString(p_setting_name))
|
||||
{
|
||||
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
|
||||
cJSON *p_mqtt_ip = cJSON_GetObjectItem( p_setting, "mqtt_uri" );
|
||||
if ( p_mqtt_ip && cJSON_IsString( p_mqtt_ip ) )
|
||||
cJSON *p_mqtt_ip = cJSON_GetObjectItem(p_setting, "mqtt_uri");
|
||||
if (p_mqtt_ip && cJSON_IsString(p_mqtt_ip))
|
||||
{
|
||||
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
|
||||
cJSON *p_mqtt_port = cJSON_GetObjectItem( p_setting, "mqtt_port" );
|
||||
if ( p_mqtt_port && cJSON_IsNumber( p_mqtt_port ) )
|
||||
cJSON *p_mqtt_port = cJSON_GetObjectItem(p_setting, "mqtt_port");
|
||||
if (p_mqtt_port && cJSON_IsNumber(p_mqtt_port))
|
||||
{
|
||||
update_user_config_flag = true;
|
||||
user_config->mqtt_port = p_mqtt_port->valueint;
|
||||
}
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>mqtt user
|
||||
cJSON *p_mqtt_user = cJSON_GetObjectItem( p_setting, "mqtt_user" );
|
||||
if ( p_mqtt_user && cJSON_IsString( p_mqtt_user ) )
|
||||
cJSON *p_mqtt_user = cJSON_GetObjectItem(p_setting, "mqtt_user");
|
||||
if (p_mqtt_user && cJSON_IsString(p_mqtt_user))
|
||||
{
|
||||
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
|
||||
cJSON *p_mqtt_password = cJSON_GetObjectItem( p_setting, "mqtt_password" );
|
||||
if ( p_mqtt_password && cJSON_IsString( p_mqtt_password ) )
|
||||
cJSON *p_mqtt_password = cJSON_GetObjectItem(p_setting, "mqtt_password");
|
||||
if (p_mqtt_password && cJSON_IsString(p_mqtt_password))
|
||||
{
|
||||
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>豸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
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
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-----------------------------------------------------------------
|
||||
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;
|
||||
}
|
||||
|
||||
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 );
|
||||
// os_log( "pRoot: %s\r\n", json_str );
|
||||
user_send( udp_flag, json_str ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
free( (void *) json_str );
|
||||
char *json_str = cJSON_Print(json_send);
|
||||
// os_log("pRoot: %s\r\n", json_str);
|
||||
user_send(udp_flag, json_str); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
free((void *) json_str);
|
||||
}
|
||||
cJSON_Delete( json_send );
|
||||
cJSON_Delete(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;
|
||||
}
|
||||
|
||||
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
|
||||
*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 ( !pJsonSend ) return false;
|
||||
if (!pJsonRoot) return false;
|
||||
if (!pJsonSend) return false;
|
||||
char i;
|
||||
bool return_flag = false;
|
||||
char plug_str[] = "plug_X";
|
||||
plug_str[5] = x + '0';
|
||||
|
||||
cJSON *p_plug = cJSON_GetObjectItem( pJsonRoot, plug_str );
|
||||
if ( !p_plug ) return_flag = false;
|
||||
cJSON *p_plug = cJSON_GetObjectItem(pJsonRoot, plug_str);
|
||||
if (!p_plug) return_flag = false;
|
||||
|
||||
cJSON *json_plug_send = cJSON_CreateObject( );
|
||||
cJSON *json_plug_send = cJSON_CreateObject();
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>plug on------------------------------------------------------
|
||||
if ( p_plug )
|
||||
if (p_plug)
|
||||
{
|
||||
cJSON *p_plug_on = cJSON_GetObjectItem( p_plug, "on" );
|
||||
if ( p_plug_on )
|
||||
cJSON *p_plug_on = cJSON_GetObjectItem(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;
|
||||
}
|
||||
user_mqtt_send_plug_state( x );
|
||||
user_mqtt_send_plug_state(x);
|
||||
}
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>plug<75><67>setting<6E><67>Ŀ----------------------------------------------
|
||||
cJSON *p_plug_setting = cJSON_GetObjectItem( p_plug, "setting" );
|
||||
if ( p_plug_setting )
|
||||
cJSON *p_plug_setting = cJSON_GetObjectItem(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----------------------------------------
|
||||
cJSON *p_plug_setting_name = cJSON_GetObjectItem( p_plug_setting, "name" );
|
||||
if ( p_plug_setting_name )
|
||||
cJSON *p_plug_setting_name = cJSON_GetObjectItem(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;
|
||||
sprintf( user_config->plug[x].name, p_plug_setting_name->valuestring );
|
||||
user_mqtt_hass_auto_name( x );
|
||||
sprintf(user_config->plug[x].name, p_plug_setting_name->valuestring);
|
||||
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----------------------------------------
|
||||
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;
|
||||
}
|
||||
|
||||
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" );
|
||||
// if ( p_plug || p_nvalue )
|
||||
cJSON_AddNumberToObject( json_plug_send, "on", user_config->plug[x].on );
|
||||
// cJSON *p_nvalue = cJSON_GetObjectItem(pJsonRoot, "nvalue");
|
||||
// if (p_plug || p_nvalue)
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -282,36 +282,36 @@ 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
|
||||
*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;
|
||||
|
||||
char plug_task_str[] = "task_X";
|
||||
plug_task_str[5] = y + '0';
|
||||
|
||||
cJSON *p_plug_task = cJSON_GetObjectItem( pJsonRoot, plug_task_str );
|
||||
if ( !p_plug_task ) return false;
|
||||
cJSON *p_plug_task = cJSON_GetObjectItem(pJsonRoot, plug_task_str);
|
||||
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_minute = cJSON_GetObjectItem( p_plug_task, "minute" );
|
||||
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_on = cJSON_GetObjectItem( p_plug_task, "on" );
|
||||
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_repeat = cJSON_GetObjectItem(p_plug_task, "repeat");
|
||||
cJSON *p_plug_task_action = cJSON_GetObjectItem(p_plug_task, "action");
|
||||
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_on )
|
||||
&& p_plug_task_on)
|
||||
{
|
||||
|
||||
if ( cJSON_IsNumber( p_plug_task_hour )
|
||||
&& cJSON_IsNumber( p_plug_task_minute )
|
||||
&& cJSON_IsNumber( p_plug_task_repeat )
|
||||
&& cJSON_IsNumber( p_plug_task_action )
|
||||
&& cJSON_IsNumber( p_plug_task_on )
|
||||
)
|
||||
if (cJSON_IsNumber(p_plug_task_hour)
|
||||
&& cJSON_IsNumber(p_plug_task_minute)
|
||||
&& cJSON_IsNumber(p_plug_task_repeat)
|
||||
&& cJSON_IsNumber(p_plug_task_action)
|
||||
&& cJSON_IsNumber(p_plug_task_on)
|
||||
)
|
||||
{
|
||||
return_flag = true;
|
||||
user_config->plug[x].task[y].hour = p_plug_task_hour->valueint;
|
||||
@@ -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, "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, "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, "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, "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, "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;
|
||||
}
|
||||
|
||||
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;
|
||||
else if ( a >= 0x41 && a <= 0x46 )
|
||||
else if (a >= 0x41 && a <= 0x46)
|
||||
{
|
||||
a = a + 10 - 0x41;
|
||||
} else if ( a >= 0x61 && a <= 0x66 )
|
||||
} else if (a >= 0x61 && a <= 0x66)
|
||||
{
|
||||
a = a + 10 - 0x61;
|
||||
}
|
||||
|
||||
if ( b >= 0x30 && b <= 0x39 )
|
||||
if (b >= 0x30 && b <= 0x39)
|
||||
b -= 0x30;
|
||||
else if ( b >= 0x41 && b <= 0x46 )
|
||||
else if (b >= 0x41 && b <= 0x46)
|
||||
{
|
||||
b = b + 10 - 0x41;
|
||||
} else if ( b >= 0x61 && b <= 0x66 )
|
||||
} else if (b >= 0x61 && b <= 0x66)
|
||||
{
|
||||
b = b + 10 - 0x61;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include "mico.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);
|
||||
unsigned char strtohex(char a, char b);
|
||||
|
||||
|
||||
100
TC1/user_gpio.c
100
TC1/user_gpio.c
@@ -8,22 +8,22 @@
|
||||
|
||||
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 )
|
||||
MicoGpioOutputTrigger( Led );
|
||||
else if ( x )
|
||||
MicoGpioOutputHigh( Led );
|
||||
if (x == -1)
|
||||
MicoGpioOutputTrigger(Led);
|
||||
else if (x)
|
||||
MicoGpioOutputHigh(Led);
|
||||
else
|
||||
MicoGpioOutputLow( Led );
|
||||
MicoGpioOutputLow(Led);
|
||||
}
|
||||
|
||||
bool relay_out( void )
|
||||
bool relay_out(void)
|
||||
{
|
||||
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;
|
||||
}
|
||||
@@ -90,51 +90,51 @@ void user_relay_set(unsigned char i, unsigned char on)
|
||||
* 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;
|
||||
for (i = 0; i < PLUG_NUM; i++)
|
||||
user_relay_set(i, y);
|
||||
}
|
||||
|
||||
static void key_long_press( void )
|
||||
static void key_long_press(void)
|
||||
{
|
||||
// os_log("key_long_press");
|
||||
// user_led_set( 1 );
|
||||
// user_mqtt_send( "mqtt test" );
|
||||
// user_led_set(1);
|
||||
// user_mqtt_send("mqtt test");
|
||||
}
|
||||
|
||||
static void key_long_10s_press( void )
|
||||
static void key_long_10s_press(void)
|
||||
{
|
||||
OSStatus err;
|
||||
char i = 0;
|
||||
os_log( "WARNGIN: user params restored!" );
|
||||
// for ( i = 0; i < 3; i++ )
|
||||
os_log("WARNGIN: user params restored!");
|
||||
// for (i = 0; i < 3; i++)
|
||||
// {
|
||||
// user_led_set( 1 );
|
||||
// mico_rtos_thread_msleep( 100 );
|
||||
// user_led_set( 0 );
|
||||
// user_led_set(1);
|
||||
// mico_rtos_thread_msleep(100);
|
||||
// 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;
|
||||
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;
|
||||
OSStatus err;
|
||||
|
||||
if ( relay_out() )
|
||||
if (relay_out())
|
||||
{
|
||||
user_relay_set_all( 0 );
|
||||
user_relay_set_all(0);
|
||||
}
|
||||
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);
|
||||
}
|
||||
@@ -146,42 +146,42 @@ mico_timer_t user_key_timer;
|
||||
uint16_t key_time = 0;
|
||||
#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_last;
|
||||
//<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_continue = tmp;
|
||||
// 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_continue != 0 )
|
||||
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)
|
||||
{
|
||||
//any button pressed
|
||||
key_time++;
|
||||
if ( key_time < BUTTON_LONG_PRESS_TIME )
|
||||
if (key_time < BUTTON_LONG_PRESS_TIME)
|
||||
key_last = key_continue;
|
||||
else
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -189,30 +189,30 @@ static void key_timeout_handler( void* arg )
|
||||
} else
|
||||
{
|
||||
//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>
|
||||
key_time = 0;
|
||||
os_log("button short pressed:%d",key_time);
|
||||
key_short_press( );
|
||||
} else if ( key_time > 100 )
|
||||
key_short_press();
|
||||
} else if (key_time > 100)
|
||||
{
|
||||
MicoSystemReboot( );
|
||||
MicoSystemReboot();
|
||||
}
|
||||
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 );
|
||||
mico_rtos_init_timer( &user_key_timer, 100, key_timeout_handler, NULL );
|
||||
MicoGpioInitialize(Button, INPUT_PULL_UP);
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -8,9 +8,9 @@
|
||||
|
||||
void user_led_set(char x);
|
||||
void key_init(void);
|
||||
void user_relay_set(unsigned char x,unsigned char y );
|
||||
void user_relay_set_all( char y );
|
||||
bool relay_out( void );
|
||||
void user_relay_set(unsigned char x,unsigned char y);
|
||||
void user_relay_set_all(char y);
|
||||
bool relay_out(void);
|
||||
char* get_socket_status();
|
||||
|
||||
#endif
|
||||
|
||||
@@ -76,15 +76,15 @@ typedef struct
|
||||
uint32_t datalen;
|
||||
} 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 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 void mqtt_client_thread(mico_thread_arg_t arg);
|
||||
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);
|
||||
|
||||
OSStatus user_recv_handler( void *arg );
|
||||
OSStatus user_recv_handler(void *arg);
|
||||
|
||||
OSStatus user_mqtt_send_plug_state( char plug_id );
|
||||
void user_mqtt_hass_auto( char plug_id );
|
||||
void user_mqtt_hass_auto_power( void );
|
||||
OSStatus user_mqtt_send_plug_state(char plug_id);
|
||||
void user_mqtt_hass_auto(char plug_id);
|
||||
void user_mqtt_hass_auto_power(void);
|
||||
|
||||
bool isconnect = false;
|
||||
mico_queue_t mqtt_msg_send_queue = NULL;
|
||||
@@ -100,92 +100,92 @@ char topic_set[MAX_MQTT_TOPIC_SIZE];
|
||||
|
||||
mico_timer_t timer_handle;
|
||||
static uint8_t timer_status = 0;
|
||||
void user_mqtt_timer_func( void *arg )
|
||||
void user_mqtt_timer_func(void *arg)
|
||||
{
|
||||
uint8_t *buf1 = NULL;
|
||||
|
||||
LinkStatusTypeDef LinkStatus;
|
||||
micoWlanGetLinkStatus( &LinkStatus );
|
||||
if ( LinkStatus.is_connected != 1 )
|
||||
micoWlanGetLinkStatus(&LinkStatus);
|
||||
if (LinkStatus.is_connected != 1)
|
||||
{
|
||||
mico_stop_timer( &timer_handle );
|
||||
mico_stop_timer(&timer_handle);
|
||||
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++;
|
||||
switch ( timer_status )
|
||||
switch (timer_status)
|
||||
{
|
||||
case 1:
|
||||
user_mqtt_hass_auto_power( );
|
||||
user_mqtt_hass_auto_power();
|
||||
break;
|
||||
case 2:
|
||||
user_mqtt_hass_auto( 0 );
|
||||
user_mqtt_hass_auto(0);
|
||||
break;
|
||||
case 3:
|
||||
user_mqtt_hass_auto( 1 );
|
||||
user_mqtt_hass_auto(1);
|
||||
break;
|
||||
case 4:
|
||||
user_mqtt_hass_auto( 2 );
|
||||
user_mqtt_hass_auto(2);
|
||||
break;
|
||||
case 5:
|
||||
user_mqtt_hass_auto( 3 );
|
||||
user_mqtt_hass_auto(3);
|
||||
break;
|
||||
case 6:
|
||||
user_mqtt_hass_auto( 4 );
|
||||
user_mqtt_hass_auto(4);
|
||||
break;
|
||||
case 7:
|
||||
user_mqtt_hass_auto( 5 );
|
||||
user_mqtt_hass_auto(5);
|
||||
break;
|
||||
case 8:
|
||||
user_mqtt_hass_auto_name( 0 );
|
||||
user_mqtt_hass_auto_name(0);
|
||||
break;
|
||||
case 9:
|
||||
user_mqtt_hass_auto_name( 1 );
|
||||
user_mqtt_hass_auto_name(1);
|
||||
break;
|
||||
case 10:
|
||||
user_mqtt_hass_auto_name( 2 );
|
||||
user_mqtt_hass_auto_name(2);
|
||||
break;
|
||||
case 11:
|
||||
user_mqtt_hass_auto_name( 3 );
|
||||
user_mqtt_hass_auto_name(3);
|
||||
break;
|
||||
case 12:
|
||||
user_mqtt_hass_auto_name( 4 );
|
||||
user_mqtt_hass_auto_name(4);
|
||||
break;
|
||||
case 13:
|
||||
user_mqtt_hass_auto_name( 5 );
|
||||
user_mqtt_hass_auto_name(5);
|
||||
break;
|
||||
case 14:
|
||||
user_mqtt_hass_auto_power_name( );
|
||||
user_mqtt_hass_auto_power_name();
|
||||
break;
|
||||
case 15:
|
||||
|
||||
buf1 = malloc( 1024 ); //idx为1位时长度为24
|
||||
if ( buf1 != NULL )
|
||||
buf1 = malloc(1024); //idx为1位时长度为24
|
||||
if (buf1 != NULL)
|
||||
{
|
||||
sprintf(
|
||||
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}}}",
|
||||
strMac );
|
||||
user_function_cmd_received( 0, buf1 );
|
||||
free( buf1 );
|
||||
strMac);
|
||||
user_function_cmd_received(0, buf1);
|
||||
free(buf1);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
mico_stop_timer( &timer_handle );
|
||||
// mico_deinit_timer( &timer_handle );
|
||||
mico_stop_timer(&timer_handle);
|
||||
// mico_deinit_timer(&timer_handle);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Application entrance */
|
||||
OSStatus user_mqtt_init( void )
|
||||
OSStatus user_mqtt_init(void)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
sprintf( topic_set, MQTT_CLIENT_SUB_TOPIC1 );
|
||||
sprintf( topic_state, MQTT_CLIENT_PUB_TOPIC, strMac );
|
||||
sprintf(topic_set, MQTT_CLIENT_SUB_TOPIC1);
|
||||
sprintf(topic_state, MQTT_CLIENT_PUB_TOPIC, strMac);
|
||||
|
||||
#ifdef MQTT_CLIENT_SSL_ENABLE
|
||||
int mqtt_thread_stack_size = 0x3000;
|
||||
@@ -193,39 +193,39 @@ OSStatus user_mqtt_init( void )
|
||||
//TODO size:0x800
|
||||
int mqtt_thread_stack_size = 0x2000;
|
||||
#endif
|
||||
uint32_t mqtt_lib_version = MQTTClientLibVersion( );
|
||||
app_log( "MQTT client version: [%ld.%ld.%ld]",
|
||||
uint32_t mqtt_lib_version = MQTTClientLibVersion();
|
||||
app_log("MQTT client version: [%ld.%ld.%ld]",
|
||||
0xFF & (mqtt_lib_version >> 16), 0xFF & (mqtt_lib_version >> 8), 0xFF & mqtt_lib_version);
|
||||
|
||||
/* create mqtt msg send queue */
|
||||
err = mico_rtos_init_queue( &mqtt_msg_send_queue, "mqtt_msg_send_queue", sizeof(p_mqtt_send_msg_t),
|
||||
MAX_MQTT_SEND_QUEUE_SIZE );
|
||||
require_noerr_action( err, exit, app_log("ERROR: create mqtt msg send queue err=%d.", err) );
|
||||
err = mico_rtos_init_queue(&mqtt_msg_send_queue, "mqtt_msg_send_queue", sizeof(p_mqtt_send_msg_t),
|
||||
MAX_MQTT_SEND_QUEUE_SIZE);
|
||||
require_noerr_action(err, exit, app_log("ERROR: create mqtt msg send queue err=%d.", err));
|
||||
|
||||
/* 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,
|
||||
mqtt_thread_stack_size, 0 );
|
||||
require_noerr_string( err, exit, "ERROR: Unable to start the mqtt client thread." );
|
||||
mqtt_thread_stack_size, 0);
|
||||
require_noerr_string(err, exit, "ERROR: Unable to start the mqtt client thread.");
|
||||
|
||||
/* 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 );
|
||||
require_noerr_string( err, exit, "ERROR: Unable to start the mqtt client worker thread." );
|
||||
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.");
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static OSStatus mqtt_client_release( Client *c, Network *n )
|
||||
static OSStatus mqtt_client_release(Client *c, Network *n)
|
||||
{
|
||||
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!");
|
||||
err = kDeletedErr;
|
||||
@@ -234,15 +234,15 @@ static OSStatus mqtt_client_release( Client *c, Network *n )
|
||||
}
|
||||
|
||||
// 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,
|
||||
uint32_t msg_len )
|
||||
uint32_t msg_len)
|
||||
{
|
||||
OSStatus err = kUnknownErr;
|
||||
int ret = 0;
|
||||
MQTTMessage publishData = MQTTMessage_publishData_initializer;
|
||||
|
||||
require( topic && msg_len && msg, exit );
|
||||
require(topic && msg_len && msg, exit);
|
||||
|
||||
// upload data qos0
|
||||
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.payloadlen = msg_len;
|
||||
|
||||
ret = MQTTPublish( c, topic, &publishData );
|
||||
ret = MQTTPublish(c, topic, &publishData);
|
||||
|
||||
if ( MQTT_SUCCESS == ret )
|
||||
if (MQTT_SUCCESS == ret)
|
||||
{
|
||||
err = kNoErr;
|
||||
} else if ( MQTT_SOCKET_ERR == ret )
|
||||
} else if (MQTT_SOCKET_ERR == ret)
|
||||
{
|
||||
err = kConnectionErr;
|
||||
} else
|
||||
@@ -267,7 +267,7 @@ static OSStatus mqtt_msg_publish( Client *c, const char* topic, char qos, char r
|
||||
return err;
|
||||
}
|
||||
|
||||
void mqtt_client_thread( mico_thread_arg_t arg )
|
||||
void mqtt_client_thread(mico_thread_arg_t arg)
|
||||
{
|
||||
OSStatus err = kUnknownErr;
|
||||
|
||||
@@ -284,12 +284,12 @@ void mqtt_client_thread( mico_thread_arg_t arg )
|
||||
|
||||
mqtt_log("MQTT client thread started...");
|
||||
|
||||
memset( &c, 0, sizeof(c) );
|
||||
memset( &n, 0, sizeof(n) );
|
||||
memset(&c, 0, sizeof(c));
|
||||
memset(&n, 0, sizeof(n));
|
||||
|
||||
/* create msg send queue event fd */
|
||||
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!!!") );
|
||||
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!!!"));
|
||||
|
||||
MQTT_start:
|
||||
|
||||
@@ -305,22 +305,22 @@ void mqtt_client_thread( mico_thread_arg_t arg )
|
||||
ssl_settings.ssl_enable = false;
|
||||
#endif
|
||||
LinkStatusTypeDef LinkStatus;
|
||||
while ( 1 )
|
||||
while (1)
|
||||
{
|
||||
isconnect = false;
|
||||
mico_rtos_thread_sleep( 3 );
|
||||
if ( MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1 ) continue; //未配置mqtt服务器时不连接
|
||||
mico_rtos_thread_sleep(3);
|
||||
if (MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1) continue; //未配置mqtt服务器时不连接
|
||||
|
||||
micoWlanGetLinkStatus( &LinkStatus );
|
||||
if ( LinkStatus.is_connected != 1 )
|
||||
micoWlanGetLinkStatus(&LinkStatus);
|
||||
if (LinkStatus.is_connected != 1)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
rc = NewNetwork( &n, MQTT_SERVER, MQTT_SERVER_PORT, ssl_settings );
|
||||
if ( rc == MQTT_SUCCESS ) break;
|
||||
rc = NewNetwork(&n, MQTT_SERVER, MQTT_SERVER_PORT, ssl_settings);
|
||||
if (rc == MQTT_SUCCESS) break;
|
||||
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 */
|
||||
//c.heartbeat_retry_max = 2;
|
||||
rc = MQTTClientInit( &c, &n, MQTT_CMD_TIMEOUT );
|
||||
require_noerr_string( rc, MQTT_reconnect, "ERROR: MQTT client init err." );
|
||||
rc = MQTTClientInit(&c, &n, MQTT_CMD_TIMEOUT);
|
||||
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client init err.");
|
||||
|
||||
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.cleansession = 1;
|
||||
|
||||
rc = MQTTConnect( &c, &connectData );
|
||||
require_noerr_string( rc, MQTT_reconnect, "ERROR: MQTT client connect err." );
|
||||
rc = MQTTConnect(&c, &connectData);
|
||||
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client connect err.");
|
||||
|
||||
mqtt_log("MQTT client connect success!");
|
||||
|
||||
/* 4. mqtt client subscribe */
|
||||
rc = MQTTSubscribe( &c, topic_set, QOS0, messageArrived );
|
||||
require_noerr_string( rc, MQTT_reconnect, "ERROR: MQTT client subscribe err." );
|
||||
rc = MQTTSubscribe(&c, topic_set, QOS0, messageArrived);
|
||||
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client subscribe err.");
|
||||
mqtt_log("MQTT client subscribe success! recv_topic=[%s].", topic_set);
|
||||
/*4.1 连接成功后先更新发送一次数据*/
|
||||
isconnect = true;
|
||||
|
||||
mico_init_timer( &timer_handle, 150, user_mqtt_timer_func, &arg );
|
||||
mico_start_timer( &timer_handle );
|
||||
mico_init_timer(&timer_handle, 150, user_mqtt_timer_func, &arg);
|
||||
mico_start_timer(&timer_handle);
|
||||
/* 5. client loop for recv msg && keepalive */
|
||||
while ( 1 )
|
||||
while (1)
|
||||
{
|
||||
isconnect = true;
|
||||
no_mqtt_msg_exchange = true;
|
||||
FD_ZERO( &readfds );
|
||||
FD_SET( c.ipstack->my_socket, &readfds );
|
||||
FD_SET( msg_send_event_fd, &readfds );
|
||||
select( msg_send_event_fd + 1, &readfds, NULL, NULL, &t );
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(c.ipstack->my_socket, &readfds);
|
||||
FD_SET(msg_send_event_fd, &readfds);
|
||||
select(msg_send_event_fd + 1, &readfds, NULL, NULL, &t);
|
||||
|
||||
/* 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 );
|
||||
require_noerr( rc, MQTT_reconnect );
|
||||
rc = MQTTYield(&c, (int) MQTT_YIELD_TMIE);
|
||||
require_noerr(rc, MQTT_reconnect);
|
||||
no_mqtt_msg_exchange = false;
|
||||
}
|
||||
|
||||
/* 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
|
||||
mico_rtos_pop_from_queue( &mqtt_msg_send_queue, &p_send_msg, 0 );
|
||||
require_string( p_send_msg, exit, "Wrong data point" );
|
||||
mico_rtos_pop_from_queue(&mqtt_msg_send_queue, &p_send_msg, 0);
|
||||
require_string(p_send_msg, exit, "Wrong data point");
|
||||
|
||||
// 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->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);
|
||||
no_mqtt_msg_exchange = false;
|
||||
free( p_send_msg );
|
||||
free(p_send_msg);
|
||||
p_send_msg = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* 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 );
|
||||
require_noerr_string( rc, MQTT_reconnect, "ERROR: keepalive err" );
|
||||
rc = keepalive(&c);
|
||||
require_noerr_string(rc, MQTT_reconnect, "ERROR: keepalive err");
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
mqtt_client_release( &c, &n );
|
||||
mqtt_client_release(&c, &n);
|
||||
isconnect = false;
|
||||
user_led_set( -1 );
|
||||
mico_rtos_thread_msleep( 100 );
|
||||
user_led_set( -1 );
|
||||
mico_rtos_thread_sleep( 5 );
|
||||
user_led_set(-1);
|
||||
mico_rtos_thread_msleep(100);
|
||||
user_led_set(-1);
|
||||
mico_rtos_thread_sleep(5);
|
||||
goto MQTT_start;
|
||||
|
||||
exit:
|
||||
isconnect = false;
|
||||
mqtt_log("EXIT: MQTT client exit with err = %d.", err);
|
||||
mqtt_client_release( &c, &n );
|
||||
mico_rtos_delete_thread( NULL );
|
||||
mqtt_client_release(&c, &n);
|
||||
mico_rtos_delete_thread(NULL);
|
||||
}
|
||||
|
||||
// callback, msg received from mqtt server
|
||||
static void messageArrived( MessageData* md )
|
||||
static void messageArrived(MessageData* md)
|
||||
{
|
||||
OSStatus err = kUnknownErr;
|
||||
p_mqtt_recv_msg_t p_recv_msg = NULL;
|
||||
MQTTMessage* message = md->message;
|
||||
|
||||
p_recv_msg = (p_mqtt_recv_msg_t) calloc( 1, sizeof(mqtt_recv_msg_t) );
|
||||
require_action( p_recv_msg, exit, err = kNoMemoryErr );
|
||||
p_recv_msg = (p_mqtt_recv_msg_t) calloc(1, sizeof(mqtt_recv_msg_t));
|
||||
require_action(p_recv_msg, exit, err = kNoMemoryErr);
|
||||
|
||||
p_recv_msg->datalen = message->payloadlen;
|
||||
p_recv_msg->qos = (char) (message->qos);
|
||||
p_recv_msg->retained = message->retained;
|
||||
strncpy( p_recv_msg->topic, md->topicName->lenstring.data, md->topicName->lenstring.len );
|
||||
memcpy( p_recv_msg->data, message->payload, message->payloadlen );
|
||||
strncpy(p_recv_msg->topic, md->topicName->lenstring.data, md->topicName->lenstring.len);
|
||||
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 );
|
||||
require_noerr( err, exit );
|
||||
err = mico_rtos_send_asynchronous_event(&mqtt_client_worker_thread, user_recv_handler, p_recv_msg);
|
||||
require_noerr(err, exit);
|
||||
|
||||
exit:
|
||||
if ( err != kNoErr )
|
||||
if (err != kNoErr)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
/* Application process MQTT received data */
|
||||
OSStatus user_recv_handler( void *arg )
|
||||
OSStatus user_recv_handler(void *arg)
|
||||
{
|
||||
OSStatus err = kUnknownErr;
|
||||
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);
|
||||
user_function_cmd_received( 0, p_recv_msg->data );
|
||||
free( p_recv_msg );
|
||||
user_function_cmd_received(0, p_recv_msg->data);
|
||||
free(p_recv_msg);
|
||||
|
||||
exit:
|
||||
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;
|
||||
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);
|
||||
|
||||
/* 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 );
|
||||
free( p_send_msg );
|
||||
mico_rtos_pop_from_queue(&mqtt_msg_send_queue, &p_send_msg, 0);
|
||||
free(p_send_msg);
|
||||
p_send_msg = NULL;
|
||||
}
|
||||
|
||||
/* Push the latest data into send queue*/
|
||||
p_send_msg = (p_mqtt_send_msg_t) calloc( 1, sizeof(mqtt_send_msg_t) );
|
||||
require_action( p_send_msg, exit, err = kNoMemoryErr );
|
||||
p_send_msg = (p_mqtt_send_msg_t) calloc(1, sizeof(mqtt_send_msg_t));
|
||||
require_action(p_send_msg, exit, err = kNoMemoryErr);
|
||||
|
||||
p_send_msg->qos = 0;
|
||||
p_send_msg->retained = retained;
|
||||
p_send_msg->datalen = strlen( arg );
|
||||
memcpy( p_send_msg->data, arg, p_send_msg->datalen );
|
||||
strncpy( p_send_msg->topic, topic, MAX_MQTT_TOPIC_SIZE );
|
||||
p_send_msg->datalen = strlen(arg);
|
||||
memcpy(p_send_msg->data, arg, p_send_msg->datalen);
|
||||
strncpy(p_send_msg->topic, topic, MAX_MQTT_TOPIC_SIZE);
|
||||
|
||||
err = mico_rtos_push_to_queue( &mqtt_msg_send_queue, &p_send_msg, 0 );
|
||||
require_noerr( err, exit );
|
||||
err = mico_rtos_push_to_queue(&mqtt_msg_send_queue, &p_send_msg, 0);
|
||||
require_noerr(err, exit);
|
||||
|
||||
//app_log("Push user msg into send queue success!");
|
||||
|
||||
exit:
|
||||
if ( err != kNoErr && p_send_msg ) free( p_send_msg );
|
||||
if (err != kNoErr && p_send_msg) free(p_send_msg);
|
||||
return err;
|
||||
}
|
||||
|
||||
/* 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开关状态
|
||||
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 *topic_buf = NULL;
|
||||
send_buf = malloc( 64 ); //
|
||||
topic_buf = malloc( 64 ); //
|
||||
if ( send_buf != NULL && topic_buf != NULL )
|
||||
send_buf = malloc(64); //
|
||||
topic_buf = malloc(64); //
|
||||
if (send_buf != NULL && topic_buf != NULL)
|
||||
{
|
||||
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 );
|
||||
user_mqtt_send_topic( topic_buf, send_buf, 1 );
|
||||
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);
|
||||
user_mqtt_send_topic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if ( send_buf ) free( send_buf );
|
||||
if ( topic_buf ) free( topic_buf );
|
||||
if (send_buf) free(send_buf);
|
||||
if (topic_buf) free(topic_buf);
|
||||
}
|
||||
|
||||
//hass mqtt自动发现数据开关发送
|
||||
void user_mqtt_hass_auto( char plug_id )
|
||||
void user_mqtt_hass_auto(char plug_id)
|
||||
{
|
||||
uint8_t i;
|
||||
uint8_t *send_buf = NULL;
|
||||
uint8_t *topic_buf = NULL;
|
||||
send_buf = malloc( 512 ); //
|
||||
topic_buf = malloc( 128 ); //
|
||||
if ( send_buf != NULL && topic_buf != NULL )
|
||||
send_buf = malloc(512); //
|
||||
topic_buf = malloc(128); //
|
||||
if (send_buf != NULL && topic_buf != NULL)
|
||||
{
|
||||
sprintf( topic_buf, "homeassistant/switch/%s/plug_%d/config", strMac, plug_id );
|
||||
sprintf( send_buf, "{"
|
||||
sprintf(topic_buf, "homeassistant/switch/%s/plug_%d/config", strMac, plug_id);
|
||||
sprintf(send_buf, "{"
|
||||
"\"name\":\"zTC1_plug%d_%s\","
|
||||
"\"stat_t\":\"homeassistant/switch/%s/plug_%d/state\","
|
||||
"\"cmd_t\":\"device/ztc1/set\","
|
||||
"\"pl_on\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":1}}\","
|
||||
"\"pl_off\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":0}}\""
|
||||
"}\0",
|
||||
plug_id, strMac + 8, strMac, plug_id, strMac, plug_id, strMac, plug_id );
|
||||
user_mqtt_send_topic( topic_buf, send_buf, 1 );
|
||||
plug_id, strMac + 8, strMac, plug_id, strMac, plug_id, strMac, plug_id);
|
||||
user_mqtt_send_topic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if ( send_buf ) free( send_buf );
|
||||
if ( topic_buf ) free( topic_buf );
|
||||
if (send_buf) free(send_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 *topic_buf = NULL;
|
||||
send_buf = (uint8_t *) malloc( 300 );
|
||||
topic_buf = (uint8_t *) malloc( 64 );
|
||||
if ( send_buf != NULL && topic_buf != NULL )
|
||||
send_buf = (uint8_t *) malloc(300);
|
||||
topic_buf = (uint8_t *) malloc(64);
|
||||
if (send_buf != NULL && topic_buf != NULL)
|
||||
{
|
||||
sprintf( topic_buf, "homeassistant/switch/%s/plug_%d/config", strMac, plug_id );
|
||||
sprintf( send_buf, "{"
|
||||
sprintf(topic_buf, "homeassistant/switch/%s/plug_%d/config", strMac, plug_id);
|
||||
sprintf(send_buf, "{"
|
||||
"\"name\":\"%s\","
|
||||
"\"stat_t\":\"homeassistant/switch/%s/plug_%d/state\","
|
||||
"\"cmd_t\":\"device/ztc1/set\","
|
||||
"\"pl_on\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":1}}\","
|
||||
"\"pl_off\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":0}}\""
|
||||
"}\0",
|
||||
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_config->plug[plug_id].name, strMac, plug_id, strMac, plug_id, strMac, plug_id);
|
||||
user_mqtt_send_topic(topic_buf, send_buf, 0);
|
||||
}
|
||||
if ( send_buf )
|
||||
free( send_buf );
|
||||
if ( topic_buf )
|
||||
free( topic_buf );
|
||||
if (send_buf)
|
||||
free(send_buf);
|
||||
if (topic_buf)
|
||||
free(topic_buf);
|
||||
}
|
||||
//hass mqtt自动发现数据功率发送
|
||||
void user_mqtt_hass_auto_power( void )
|
||||
void user_mqtt_hass_auto_power(void)
|
||||
{
|
||||
uint8_t i;
|
||||
uint8_t *send_buf = NULL;
|
||||
uint8_t *topic_buf = NULL;
|
||||
send_buf = malloc( 512 ); //
|
||||
topic_buf = malloc( 128 ); //
|
||||
if ( send_buf != NULL && topic_buf != NULL )
|
||||
send_buf = malloc(512); //
|
||||
topic_buf = malloc(128); //
|
||||
if (send_buf != NULL && topic_buf != NULL)
|
||||
{
|
||||
sprintf( topic_buf, "homeassistant/sensor/%s/power/config", strMac );
|
||||
sprintf( send_buf, "{"
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/power/config", strMac);
|
||||
sprintf(send_buf, "{"
|
||||
"\"name\":\"zTC1_power_%s\","
|
||||
"\"state_topic\":\"homeassistant/sensor/%s/power/state\","
|
||||
"\"unit_of_measurement\":\"W\","
|
||||
"\"icon\":\"mdi:gauge\","
|
||||
"\"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 ( topic_buf ) free( topic_buf );
|
||||
if (send_buf) free(send_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 *topic_buf = NULL;
|
||||
send_buf = (uint8_t *) malloc( 300 ); //
|
||||
topic_buf = (uint8_t *) malloc( 64 ); //
|
||||
if ( send_buf != NULL && topic_buf != NULL )
|
||||
send_buf = (uint8_t *) malloc(300); //
|
||||
topic_buf = (uint8_t *) malloc(64); //
|
||||
if (send_buf != NULL && topic_buf != NULL)
|
||||
{
|
||||
sprintf( topic_buf, "homeassistant/sensor/%s/power/config", strMac );
|
||||
sprintf( send_buf, "{"
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/power/config", strMac);
|
||||
sprintf(send_buf, "{"
|
||||
"\"name\":\"zTC1xxxxxx\","
|
||||
"\"state_topic\":\"homeassistant/sensor/%s/power/state\","
|
||||
"\"unit_of_measurement\":\"W\","
|
||||
"\"icon\":\"mdi:gauge\","
|
||||
"\"value_template\":\"{{ value_json.power }}\""
|
||||
"}",
|
||||
strMac );
|
||||
strMac);
|
||||
send_buf[13] = 0xe5;
|
||||
send_buf[14] = 0x8a;
|
||||
send_buf[15] = 0x9f;
|
||||
send_buf[16] = 0xe7;
|
||||
send_buf[17] = 0x8e;
|
||||
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 )
|
||||
free( send_buf );
|
||||
if ( topic_buf )
|
||||
free( topic_buf );
|
||||
if (send_buf)
|
||||
free(send_buf);
|
||||
if (topic_buf)
|
||||
free(topic_buf);
|
||||
}
|
||||
|
||||
void user_mqtt_hass_power( void )
|
||||
void user_mqtt_hass_power(void)
|
||||
{
|
||||
uint8_t i;
|
||||
uint8_t *send_buf = NULL;
|
||||
uint8_t *topic_buf = NULL;
|
||||
send_buf = malloc( 512 ); //
|
||||
topic_buf = malloc( 128 ); //
|
||||
if ( send_buf != NULL && topic_buf != NULL )
|
||||
send_buf = malloc(512); //
|
||||
topic_buf = malloc(128); //
|
||||
if (send_buf != NULL && topic_buf != NULL)
|
||||
{
|
||||
sprintf( topic_buf, "homeassistant/sensor/%s/power/state", strMac );
|
||||
sprintf( send_buf, "{\"power\":\"%d.%d\"}", power / 10, power % 10 );
|
||||
user_mqtt_send_topic( topic_buf, send_buf, 0 );
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/power/state", strMac);
|
||||
sprintf(send_buf, "{\"power\":\"%d.%d\"}", power / 10, power % 10);
|
||||
user_mqtt_send_topic(topic_buf, send_buf, 0);
|
||||
}
|
||||
if ( send_buf ) free( send_buf );
|
||||
if ( topic_buf ) free( topic_buf );
|
||||
if (send_buf) free(send_buf);
|
||||
if (topic_buf) free(topic_buf);
|
||||
}
|
||||
|
||||
bool user_mqtt_isconnect( )
|
||||
bool user_mqtt_isconnect()
|
||||
{
|
||||
return isconnect;
|
||||
}
|
||||
|
||||
@@ -12,12 +12,12 @@
|
||||
|
||||
|
||||
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 OSStatus user_mqtt_send_plug_state( char plug_id );
|
||||
extern void user_mqtt_hass_auto( 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_name(char plug_id);
|
||||
extern void user_mqtt_hass_power( void );
|
||||
extern void user_mqtt_hass_auto_power( void );
|
||||
extern void user_mqtt_hass_power(void);
|
||||
extern void user_mqtt_hass_auto_power(void);
|
||||
extern void user_mqtt_hass_auto_power_name(void);
|
||||
#endif
|
||||
|
||||
@@ -7,36 +7,36 @@
|
||||
#include "user_mqtt_client.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 };
|
||||
switch ( state )
|
||||
switch (state)
|
||||
{
|
||||
case OTA_LOADING:
|
||||
os_log("ota server is loading, progress %.2f%%", progress);
|
||||
if ( ((int) progress)%10 == 1 )
|
||||
sprintf( str, "{\"mac\":\"%s\",\"ota_progress\":%d}", strMac,((int) progress) );
|
||||
if (((int) progress)%10 == 1)
|
||||
sprintf(str, "{\"mac\":\"%s\",\"ota_progress\":%d}", strMac,((int) progress));
|
||||
break;
|
||||
case OTA_SUCCE:
|
||||
os_log("ota server daemons success");
|
||||
sprintf( str, "{\"mac\":\"%s\",\"ota_progress\":100}", strMac );
|
||||
sprintf(str, "{\"mac\":\"%s\",\"ota_progress\":100}", strMac);
|
||||
break;
|
||||
case OTA_FAIL:
|
||||
os_log("ota server daemons failed");
|
||||
sprintf( str, "{\"mac\":\"%s\",\"ota_progress\":-1}", strMac );
|
||||
sprintf(str, "{\"mac\":\"%s\",\"ota_progress\":-1}", strMac);
|
||||
break;
|
||||
default:
|
||||
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);
|
||||
ota_server_start( url, md5, ota_server_status_handler );
|
||||
ota_server_start(url, md5, ota_server_status_handler);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,22 +14,22 @@ static uint32_t clock_count = 0;
|
||||
static uint32_t timer_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;
|
||||
|
||||
if ( timer_irq_count > 1 )
|
||||
if (timer_irq_count > 1)
|
||||
{
|
||||
timer = (clock_count - clock_count_last);
|
||||
|
||||
// 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 += 4294967; //0xffffffff/1000;
|
||||
} else if ( clock_count < clock_count_last )
|
||||
} else if (clock_count < clock_count_last)
|
||||
{
|
||||
timer += 0xffffffff;
|
||||
timer /= 1000;
|
||||
@@ -42,11 +42,11 @@ static void power_timer_handler( void* arg )
|
||||
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);
|
||||
// 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;
|
||||
// power = 15200000 / timer;
|
||||
@@ -58,21 +58,21 @@ static void power_timer_handler( void* arg )
|
||||
// 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( );
|
||||
if ( timer_irq_count == 0 ) clock_count_last = clock_count;
|
||||
clock_count = mico_nanosecond_clock_value();
|
||||
if (timer_irq_count == 0) clock_count_last = clock_count;
|
||||
timer_irq_count++;
|
||||
}
|
||||
|
||||
void user_power_init( void )
|
||||
void user_power_init(void)
|
||||
{
|
||||
os_log("user_power_init");
|
||||
|
||||
MicoGpioInitialize( POWER, INPUT_PULL_UP );
|
||||
mico_rtos_init_timer( &power_timer, 1000, power_timer_handler, NULL );
|
||||
mico_rtos_start_timer( &power_timer );
|
||||
MicoGpioInitialize(POWER, INPUT_PULL_UP);
|
||||
mico_rtos_init_timer(&power_timer, 1000, power_timer_handler, NULL);
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#ifndef __USER_POWER_H_
|
||||
#define __USER_POWER_H_
|
||||
|
||||
void user_power_init( void );
|
||||
void user_power_init(void);
|
||||
|
||||
#endif
|
||||
|
||||
164
TC1/user_rtc.c
164
TC1/user_rtc.c
@@ -8,9 +8,9 @@
|
||||
#include "user_mqtt_client.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;
|
||||
ntp_timestamp_t current_time;
|
||||
@@ -21,56 +21,56 @@ OSStatus user_sntp_get_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;
|
||||
ipp.s_addr = 0xd248912c;
|
||||
err = sntp_get_time( &ipp, ¤t_time );
|
||||
if ( err != kNoErr )
|
||||
err = sntp_get_time(&ipp, ¤t_time);
|
||||
if (err != kNoErr)
|
||||
{
|
||||
os_log("sntp_get_time err = %d.", err);
|
||||
ipp.s_addr = *(uint32_t *) (*pptr);
|
||||
err = sntp_get_time( &ipp, ¤t_time );
|
||||
err = sntp_get_time(&ipp, ¤t_time);
|
||||
}
|
||||
if ( err != kNoErr )
|
||||
if (err != kNoErr)
|
||||
{
|
||||
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;
|
||||
ipp.s_addr = *(uint32_t *) (*pptr);
|
||||
err = sntp_get_time( &ipp, ¤t_time );
|
||||
err = sntp_get_time(&ipp, ¤t_time);
|
||||
}
|
||||
if ( err != kNoErr )
|
||||
if (err != kNoErr)
|
||||
{
|
||||
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;
|
||||
ipp.s_addr = *(uint32_t *) (*pptr);
|
||||
err = sntp_get_time( &ipp, ¤t_time );
|
||||
err = sntp_get_time(&ipp, ¤t_time);
|
||||
}
|
||||
if ( err != kNoErr )
|
||||
if (err != kNoErr)
|
||||
{
|
||||
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;
|
||||
ipp.s_addr = *(uint32_t *) (*pptr);
|
||||
err = sntp_get_time( &ipp, ¤t_time );
|
||||
err = sntp_get_time(&ipp, ¤t_time);
|
||||
}
|
||||
if ( err != kNoErr )
|
||||
if (err != kNoErr)
|
||||
{
|
||||
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;
|
||||
ipp.s_addr = *(uint32_t *) (*pptr);
|
||||
err = sntp_get_time( &ipp, ¤t_time );
|
||||
err = sntp_get_time(&ipp, ¤t_time);
|
||||
}
|
||||
|
||||
if ( err == kNoErr )
|
||||
if (err == kNoErr)
|
||||
{
|
||||
mico_utc_time_ms_t utc_time_ms = (uint64_t) current_time.seconds * (uint64_t) 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
|
||||
// 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.min = currentTime->tm_min;
|
||||
// rtc_time.hr = currentTime->tm_hour;
|
||||
@@ -78,7 +78,7 @@ OSStatus user_sntp_get_time( )
|
||||
// rtc_time.weekday = currentTime->tm_wday;
|
||||
// rtc_time.month = currentTime->tm_mon + 1;
|
||||
// rtc_time.year = (currentTime->tm_year + 1900) % 100;
|
||||
// MicoRtcSetTime( &rtc_time );
|
||||
// MicoRtcSetTime(&rtc_time);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -88,7 +88,7 @@ OSStatus user_sntp_get_time( )
|
||||
return kNoErr;
|
||||
}
|
||||
|
||||
OSStatus user_rtc_init( void )
|
||||
OSStatus user_rtc_init(void)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
@@ -102,26 +102,26 @@ OSStatus user_rtc_init( void )
|
||||
// rtc_time.month = 1;
|
||||
// rtc_time.year = 1;
|
||||
//
|
||||
// MicoRtcSetTime( &rtc_time );
|
||||
// MicoRtcSetTime(&rtc_time);
|
||||
|
||||
// /* create mqtt msg send queue */
|
||||
// err = mico_rtos_init_queue( &mqtt_msg_send_queue, "mqtt_msg_send_queue", sizeof(p_mqtt_send_msg_t),
|
||||
// MAX_MQTT_SEND_QUEUE_SIZE );
|
||||
// require_noerr_action( err, exit, app_log("ERROR: create mqtt msg send queue err=%d.", err) );
|
||||
// err = mico_rtos_init_queue(&mqtt_msg_send_queue, "mqtt_msg_send_queue", sizeof(p_mqtt_send_msg_t),
|
||||
// MAX_MQTT_SEND_QUEUE_SIZE);
|
||||
// require_noerr_action(err, exit, app_log("ERROR: create mqtt msg send queue err=%d.", err));
|
||||
|
||||
/* 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,
|
||||
0x1000, 0 );
|
||||
require_noerr_string( err, exit, "ERROR: Unable to start the rtc thread." );
|
||||
0x1000, 0);
|
||||
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:
|
||||
return err;
|
||||
}
|
||||
|
||||
void rtc_thread( mico_thread_arg_t arg )
|
||||
void rtc_thread(mico_thread_arg_t arg)
|
||||
{
|
||||
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>
|
||||
@@ -132,13 +132,13 @@ void rtc_thread( mico_thread_arg_t arg )
|
||||
|
||||
mico_utc_time_t utc_time;
|
||||
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>
|
||||
micoWlanGetLinkStatus( &LinkStatus );
|
||||
if ( LinkStatus.is_connected == 1 )
|
||||
micoWlanGetLinkStatus(&LinkStatus);
|
||||
if (LinkStatus.is_connected == 1)
|
||||
{
|
||||
err = user_sntp_get_time( );
|
||||
if ( err == kNoErr )
|
||||
err = user_sntp_get_time();
|
||||
if (err == kNoErr)
|
||||
{
|
||||
os_log("sntp success!");
|
||||
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;
|
||||
|
||||
if ( utc_time_last != utc_time )
|
||||
if (utc_time_last != utc_time)
|
||||
{
|
||||
utc_time_last == utc_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.min = currentTime->tm_min;
|
||||
rtc_time.hr = currentTime->tm_hour;
|
||||
@@ -170,33 +170,33 @@ void rtc_thread( mico_thread_arg_t arg )
|
||||
rtc_time.month = currentTime->tm_mon + 1;
|
||||
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);
|
||||
|
||||
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;
|
||||
if ( //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>̵<EFBFBD><CCB5><EFBFBD>״̬: <20><>Ϊ0 ʱ<>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>趨ֵ, <20>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>趨ֵ
|
||||
if ( //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>̵<EFBFBD><CCB5><EFBFBD>״̬: <20><>Ϊ0 ʱ<>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>趨ֵ, <20>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>趨ֵ
|
||||
rtc_time.sec == 0 && rtc_time.min == user_config->plug[i].task[j].minute
|
||||
&& rtc_time.hr == user_config->plug[i].task[j].hour
|
||||
&& ((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;
|
||||
user_mqtt_send_plug_state( i );
|
||||
user_mqtt_send_plug_state(i);
|
||||
}
|
||||
if ( repeat == 0x00 )
|
||||
if (repeat == 0x00)
|
||||
{
|
||||
task_flag[i] = j;
|
||||
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>
|
||||
if ( update_user_config_flag == 1 )
|
||||
if (update_user_config_flag == 1)
|
||||
{
|
||||
os_log("update_user_config_flag");
|
||||
mico_system_context_update( sys_config );
|
||||
mico_system_context_update(sys_config);
|
||||
update_user_config_flag = 0;
|
||||
|
||||
cJSON *json_send = cJSON_CreateObject( );
|
||||
cJSON_AddStringToObject( json_send, "mac", strMac );
|
||||
cJSON *json_send = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(json_send, "mac", strMac);
|
||||
|
||||
for ( i = 0; i < PLUG_NUM; i++ )
|
||||
for (i = 0; i < PLUG_NUM; i++)
|
||||
{
|
||||
char strTemp1[] = "plug_X";
|
||||
strTemp1[5] = i + '0';
|
||||
cJSON *json_send_plug = cJSON_CreateObject( );
|
||||
cJSON_AddNumberToObject( json_send_plug, "on", user_config->plug[i].on );
|
||||
cJSON *json_send_plug = cJSON_CreateObject();
|
||||
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];
|
||||
char strTemp2[] = "task_X";
|
||||
strTemp2[5] = j + '0';
|
||||
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, "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, "action", user_config->plug[i].task[j].action );
|
||||
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 *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, "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, "action", user_config->plug[i].task[j].action);
|
||||
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", json_send_plug_setting );
|
||||
cJSON_AddItemToObject(json_send_plug, "setting", json_send_plug_setting);
|
||||
|
||||
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 );
|
||||
user_send( false, json_str ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
char *json_str = cJSON_Print(json_send);
|
||||
user_send(false, json_str); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
free( json_str );
|
||||
cJSON_Delete( json_send );
|
||||
free(json_str);
|
||||
cJSON_Delete(json_send);
|
||||
// os_log("cJSON_Delete");
|
||||
}
|
||||
|
||||
//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 );
|
||||
if ( LinkStatus.is_connected == 1 )
|
||||
micoWlanGetLinkStatus(&LinkStatus);
|
||||
if (LinkStatus.is_connected == 1)
|
||||
{
|
||||
err = user_sntp_get_time( );
|
||||
if ( err == kNoErr )
|
||||
err = user_sntp_get_time();
|
||||
if (err == kNoErr)
|
||||
rtc_init = 1;
|
||||
else
|
||||
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:
|
||||
os_log("EXIT: rtc exit with err = %d.", err);
|
||||
mico_rtos_delete_thread( NULL );
|
||||
mico_rtos_delete_thread(NULL);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,19 +6,19 @@
|
||||
#include "user_sntp.h"
|
||||
|
||||
/* 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;
|
||||
// iso8601_time_t iso8601_time;
|
||||
mico_utc_time_t utc_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);
|
||||
|
||||
mico_time_get_utc_time( &utc_time );
|
||||
mico_time_get_utc_time(&utc_time);
|
||||
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.min = currentTime->tm_min;
|
||||
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.year = (currentTime->tm_year + 1900) % 100;
|
||||
|
||||
MicoRtcSetTime( &rtc_time );
|
||||
MicoRtcSetTime(&rtc_time);
|
||||
|
||||
|
||||
MicoRtcGetTime(&rtc_time);
|
||||
|
||||
118
TC1/user_udp.c
118
TC1/user_udp.c
@@ -16,19 +16,19 @@ typedef struct
|
||||
uint8_t data[MAX_UDP_DATA_SIZE];
|
||||
} udp_send_msg_t, *p_udp_send_msg_t;
|
||||
|
||||
static OSStatus udp_msg_send( int socket, const unsigned char* msg, uint32_t msg_len );
|
||||
void udp_thread( void *arg );
|
||||
static OSStatus udp_msg_send(int socket, const unsigned char* msg, uint32_t msg_len);
|
||||
void udp_thread(void *arg);
|
||||
|
||||
OSStatus user_udp_init( void )
|
||||
OSStatus user_udp_init(void)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
/* 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,
|
||||
0x1000, 0 );
|
||||
require_noerr_string( err, exit, "ERROR: Unable to start the rtc thread." );
|
||||
0x1000, 0);
|
||||
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:
|
||||
return err;
|
||||
@@ -36,9 +36,9 @@ OSStatus user_udp_init( void )
|
||||
}
|
||||
|
||||
/*create udp socket*/
|
||||
void udp_thread( void *arg )
|
||||
void udp_thread(void *arg)
|
||||
{
|
||||
UNUSED_PARAMETER( arg );
|
||||
UNUSED_PARAMETER(arg);
|
||||
|
||||
OSStatus err;
|
||||
struct sockaddr_in addr;
|
||||
@@ -52,99 +52,99 @@ void udp_thread( void *arg )
|
||||
uint8_t *buf = NULL;
|
||||
|
||||
/* create udp msg send queue */
|
||||
err = mico_rtos_init_queue( &udp_msg_send_queue, "uqp_msg_send_queue", sizeof(p_udp_send_msg_t),
|
||||
MAX_UDP_SEND_QUEUE_SIZE );
|
||||
require_noerr_action( err, exit, os_log( "ERROR: create udp msg send queue err=%d.", err ) );
|
||||
err = mico_rtos_init_queue(&udp_msg_send_queue, "uqp_msg_send_queue", sizeof(p_udp_send_msg_t),
|
||||
MAX_UDP_SEND_QUEUE_SIZE);
|
||||
require_noerr_action(err, exit, os_log("ERROR: create udp msg send queue err=%d.", err));
|
||||
/* create msg send queue event fd */
|
||||
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!!!" ) );
|
||||
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!!!"));
|
||||
|
||||
buf = malloc( 1024 );
|
||||
require_action( buf, exit, err = kNoMemoryErr );
|
||||
buf = malloc(1024);
|
||||
require_action(buf, exit, err = kNoMemoryErr);
|
||||
|
||||
/*Establish a UDP port to receive any data sent to this port*/
|
||||
udp_fd = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
|
||||
require_action( IsValidSocket( udp_fd ), exit, err = kNoResourcesErr );
|
||||
udp_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
require_action(IsValidSocket(udp_fd), exit, err = kNoResourcesErr);
|
||||
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
addr.sin_port = htons( LOCAL_UDP_PORT );
|
||||
err = bind( udp_fd, (struct sockaddr *) &addr, sizeof(addr) );
|
||||
require_noerr( err, exit );
|
||||
addr.sin_port = htons(LOCAL_UDP_PORT);
|
||||
err = bind(udp_fd, (struct sockaddr *) &addr, sizeof(addr));
|
||||
require_noerr(err, exit);
|
||||
|
||||
os_log("Open local UDP port %d", LOCAL_UDP_PORT);
|
||||
|
||||
while ( 1 )
|
||||
while (1)
|
||||
{
|
||||
FD_ZERO( &readfds );
|
||||
FD_SET( msg_send_event_fd, &readfds );
|
||||
FD_SET( udp_fd, &readfds );
|
||||
select( udp_fd + 1, &readfds, NULL, NULL, NULL );
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(msg_send_event_fd, &readfds);
|
||||
FD_SET(udp_fd, &readfds);
|
||||
select(udp_fd + 1, &readfds, NULL, NULL, NULL);
|
||||
|
||||
/*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 );
|
||||
require_action( len >= 0, exit, err = kConnectionErr );
|
||||
len = recvfrom(udp_fd, buf, 1024, 0, (struct sockaddr *) &addr, &addrLen);
|
||||
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;
|
||||
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);
|
||||
// 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 */
|
||||
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
|
||||
mico_rtos_pop_from_queue( &udp_msg_send_queue, &p_send_msg, 0 );
|
||||
require_string( p_send_msg, exit, "Wrong data point" );
|
||||
mico_rtos_pop_from_queue(&udp_msg_send_queue, &p_send_msg, 0);
|
||||
require_string(p_send_msg, exit, "Wrong data point");
|
||||
|
||||
// send message to server
|
||||
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" );
|
||||
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");
|
||||
|
||||
os_log( "udp send data success! msg=[%ld].\r\n", p_send_msg->datalen);
|
||||
free( p_send_msg );
|
||||
os_log("udp send data success! msg=[%ld].\r\n", p_send_msg->datalen);
|
||||
free(p_send_msg);
|
||||
p_send_msg = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
if ( err != kNoErr )
|
||||
if (err != kNoErr)
|
||||
os_log("UDP thread exit with err: %d", err);
|
||||
if ( buf != NULL ) free( buf );
|
||||
mico_rtos_delete_thread( NULL );
|
||||
if (buf != NULL) free(buf);
|
||||
mico_rtos_delete_thread(NULL);
|
||||
}
|
||||
|
||||
// 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;
|
||||
int ret = 0;
|
||||
|
||||
require( msg_len && msg, exit );
|
||||
require(msg_len && msg, exit);
|
||||
|
||||
struct sockaddr_in addr;
|
||||
addr.sin_family = AF_INET;
|
||||
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_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*/
|
||||
sendto( socket, msg, msg_len, 0, (struct sockaddr *) &addr, sizeof(addr) );
|
||||
sendto(socket, msg, msg_len, 0, (struct sockaddr *) &addr, sizeof(addr));
|
||||
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
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);
|
||||
|
||||
/* 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 );
|
||||
free( p_send_msg );
|
||||
mico_rtos_pop_from_queue(&udp_msg_send_queue, &p_send_msg, 0);
|
||||
free(p_send_msg);
|
||||
p_send_msg = NULL;
|
||||
}
|
||||
|
||||
/* Push the latest data into send queue*/
|
||||
p_send_msg = calloc( 1, sizeof(udp_send_msg_t) );
|
||||
require_action( p_send_msg, exit, err = kNoMemoryErr );
|
||||
p_send_msg = calloc(1, sizeof(udp_send_msg_t));
|
||||
require_action(p_send_msg, exit, err = kNoMemoryErr);
|
||||
|
||||
p_send_msg->datalen = strlen( arg );
|
||||
memcpy( p_send_msg->data, arg, p_send_msg->datalen );
|
||||
p_send_msg->datalen = strlen(arg);
|
||||
memcpy(p_send_msg->data, arg, p_send_msg->datalen);
|
||||
|
||||
err = mico_rtos_push_to_queue( &udp_msg_send_queue, &p_send_msg, 0 );
|
||||
require_noerr( err, exit );
|
||||
err = mico_rtos_push_to_queue(&udp_msg_send_queue, &p_send_msg, 0);
|
||||
require_noerr(err, exit);
|
||||
|
||||
//app_log("Push user msg into send queue success!");
|
||||
|
||||
exit:
|
||||
if ( err != kNoErr && p_send_msg ) free( p_send_msg );
|
||||
if (err != kNoErr && p_send_msg) free(p_send_msg);
|
||||
return err;
|
||||
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
#include "mico.h"
|
||||
#include "micokit_ext.h"
|
||||
|
||||
OSStatus user_udp_init( void );
|
||||
OSStatus user_udp_send( char *arg );
|
||||
OSStatus user_udp_init(void);
|
||||
OSStatus user_udp_send(char *arg);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -11,110 +11,110 @@ char wifi_status = WIFI_STATE_NOCONNECT;
|
||||
|
||||
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);
|
||||
network_InitTypeDef_st wNetConfig;
|
||||
memset( &wNetConfig, 0, sizeof(network_InitTypeDef_st) );
|
||||
strcpy( wNetConfig.wifi_ssid, sys_config->micoSystemConfig.ssid );
|
||||
strcpy( wNetConfig.wifi_key, sys_config->micoSystemConfig.user_key );
|
||||
memset(&wNetConfig, 0, sizeof(network_InitTypeDef_st));
|
||||
strcpy(wNetConfig.wifi_ssid, sys_config->micoSystemConfig.ssid);
|
||||
strcpy(wNetConfig.wifi_key, sys_config->micoSystemConfig.user_key);
|
||||
wNetConfig.wifi_mode = Station;
|
||||
wNetConfig.dhcpMode = DHCP_Client;
|
||||
wNetConfig.wifi_retry_interval = 6000;
|
||||
micoWlanStart( &wNetConfig );
|
||||
micoWlanStart(&wNetConfig);
|
||||
wifi_status = WIFI_STATE_CONNECTING;
|
||||
} else
|
||||
wifi_status = WIFI_STATE_FAIL;
|
||||
}
|
||||
void wifi_start_easylink( )
|
||||
void wifi_start_easylink()
|
||||
{
|
||||
wifi_status = WIFI_STATE_EASYLINK;
|
||||
micoWlanStartEasyLink( 20000 );
|
||||
user_led_set( 1 );
|
||||
micoWlanStartEasyLink(20000);
|
||||
user_led_set(1);
|
||||
}
|
||||
|
||||
//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:");
|
||||
if ( nwkpara == NULL )
|
||||
if (nwkpara == NULL)
|
||||
{
|
||||
os_log("EasyLink fail");
|
||||
micoWlanStopEasyLink( );
|
||||
micoWlanStopEasyLink();
|
||||
return;
|
||||
}
|
||||
|
||||
os_log("ssid:\"%s\",\"%s\"",nwkpara->wifi_ssid,nwkpara->wifi_key);
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>wifi<66><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
strcpy( sys_config->micoSystemConfig.ssid, nwkpara->wifi_ssid );
|
||||
strcpy( sys_config->micoSystemConfig.user_key, nwkpara->wifi_key );
|
||||
sys_config->micoSystemConfig.user_keyLength = strlen( nwkpara->wifi_key );
|
||||
mico_system_context_update( sys_config );
|
||||
strcpy(sys_config->micoSystemConfig.ssid, nwkpara->wifi_ssid);
|
||||
strcpy(sys_config->micoSystemConfig.user_key, nwkpara->wifi_key);
|
||||
sys_config->micoSystemConfig.user_keyLength = strlen(nwkpara->wifi_key);
|
||||
mico_system_context_update(sys_config);
|
||||
|
||||
wifi_status = WIFI_STATE_NOCONNECT;
|
||||
os_log("EasyLink stop");
|
||||
micoWlanStopEasyLink( );
|
||||
micoWlanStopEasyLink();
|
||||
}
|
||||
|
||||
//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);
|
||||
wifi_status = WIFI_STATE_CONNECTED;
|
||||
user_function_cmd_received(1,"{\"cmd\":\"device report\"}");
|
||||
}
|
||||
//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;
|
||||
} else if ( status == NOTIFY_STATION_DOWN ) //wifi<66>Ͽ<EFBFBD>
|
||||
} else if (status == NOTIFY_STATION_DOWN) //wifi<66>Ͽ<EFBFBD>
|
||||
{
|
||||
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>
|
||||
static void wifi_led_timer_callback( void* arg )
|
||||
static void wifi_led_timer_callback(void* arg)
|
||||
{
|
||||
static unsigned int num = 0;
|
||||
num++;
|
||||
|
||||
switch ( wifi_status )
|
||||
switch (wifi_status)
|
||||
{
|
||||
case WIFI_STATE_FAIL:
|
||||
os_log("wifi connect fail");
|
||||
user_led_set( 0 );
|
||||
mico_rtos_stop_timer( &wifi_led_timer );
|
||||
user_led_set(0);
|
||||
mico_rtos_stop_timer(&wifi_led_timer);
|
||||
break;
|
||||
case WIFI_STATE_NOCONNECT:
|
||||
//wifi_connect_sys_config( );
|
||||
//wifi_connect_sys_config();
|
||||
break;
|
||||
|
||||
case WIFI_STATE_CONNECTING:
|
||||
//if ( num > 1 )
|
||||
//if (num > 1)
|
||||
{
|
||||
num = 0;
|
||||
user_led_set( -1 );
|
||||
user_led_set(-1);
|
||||
}
|
||||
break;
|
||||
case WIFI_STATE_NOEASYLINK:
|
||||
wifi_start_easylink( );
|
||||
wifi_start_easylink();
|
||||
break;
|
||||
case WIFI_STATE_EASYLINK:
|
||||
user_led_set( 1 );
|
||||
user_led_set(1);
|
||||
break;
|
||||
case WIFI_STATE_CONNECTED:
|
||||
user_led_set( 0 );
|
||||
mico_rtos_stop_timer( &wifi_led_timer );
|
||||
if ( relay_out( ) )
|
||||
user_led_set( 1 );
|
||||
user_led_set(0);
|
||||
mico_rtos_stop_timer(&wifi_led_timer);
|
||||
if (relay_out())
|
||||
user_led_set(1);
|
||||
else
|
||||
user_led_set( 0 );
|
||||
user_led_set(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -133,30 +133,30 @@ void wifi_connect(char* wifi_ssid, char* wifi_key)
|
||||
micoWlanStart(&wNetConfig);
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>wifi<66><69><EFBFBD><EFBFBD><EFBFBD>뵽Flash
|
||||
strcpy(sys_config->micoSystemConfig.ssid, nwkpara->wifi_ssid);
|
||||
strcpy(sys_config->micoSystemConfig.user_key, nwkpara->wifi_key);
|
||||
sys_config->micoSystemConfig.user_keyLength = strlen(nwkpara->wifi_key);
|
||||
strcpy(sys_config->micoSystemConfig.ssid, wifi_ssid);
|
||||
strcpy(sys_config->micoSystemConfig.user_key, wifi_key);
|
||||
sys_config->micoSystemConfig.user_keyLength = strlen(wifi_key);
|
||||
mico_system_context_update(sys_config);
|
||||
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>
|
||||
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>
|
||||
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>
|
||||
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>
|
||||
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();
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>wifi<66><69><EFBFBD><EFBFBD>
|
||||
if ( !mico_rtos_is_timer_running( &wifi_led_timer ) ) mico_rtos_start_timer( &wifi_led_timer );
|
||||
if (!mico_rtos_is_timer_running(&wifi_led_timer)) mico_rtos_start_timer(&wifi_led_timer);
|
||||
|
||||
IPStatusTypedef para;
|
||||
micoWlanGetIPStatus( ¶, Station );
|
||||
strcpy( strMac, para.mac );
|
||||
micoWlanGetIPStatus(¶, Station);
|
||||
strcpy(strMac, para.mac);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user