mirror of
https://github.com/oopuuu/zTC1.git
synced 2025-12-13 13:38:14 +08:00
add:udp接收,准备在家udp发送队列
This commit is contained in:
@@ -28,6 +28,7 @@ $(NAME)_SOURCES := main.c\
|
||||
cJSON/cJSON.c\
|
||||
user_wifi.c\
|
||||
user_gpio.c\
|
||||
user_udp.c\
|
||||
user_sntp.c\
|
||||
user_rtc.c\
|
||||
user_mqtt_client.c\
|
||||
|
||||
@@ -3,11 +3,15 @@
|
||||
#include "user_gpio.h"
|
||||
#include "user_wifi.h"
|
||||
#include "user_rtc.h"
|
||||
#include "user_udp.h"
|
||||
#include "user_mqtt_client.h"
|
||||
|
||||
#define os_log(format, ...) custom_log("TC1", format, ##__VA_ARGS__)
|
||||
|
||||
char rtc_init=0;
|
||||
char rtc_init=0; //sntpУʱ<D0A3>ɹ<EFBFBD><C9B9><EFBFBD>־λ
|
||||
|
||||
char strMac[12] = { 0 };
|
||||
|
||||
system_config_t * sys_config;
|
||||
user_config_t * user_config;
|
||||
|
||||
@@ -101,6 +105,9 @@ int application_start( void )
|
||||
require_noerr( err, exit );
|
||||
err = user_rtc_init();
|
||||
require_noerr( err, exit );
|
||||
|
||||
|
||||
err = user_udp_init();
|
||||
while ( 1 )
|
||||
{
|
||||
// mico_thread_msleep(500);
|
||||
|
||||
@@ -56,6 +56,8 @@ typedef struct {
|
||||
|
||||
|
||||
extern char rtc_init;
|
||||
|
||||
extern char strMac[12];
|
||||
extern system_config_t * sys_config;
|
||||
extern user_config_t * user_config;
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
#define os_log(format, ...) custom_log("FUNCTION", format, ##__VA_ARGS__)
|
||||
|
||||
#include "main.h"
|
||||
@@ -13,32 +12,67 @@ typedef struct _user_json_context_t
|
||||
int8_t val;
|
||||
} user_json_context_t;
|
||||
|
||||
void user_function_cmd_received( uint8_t *pusrdata )
|
||||
void user_function_cmd_received(struct sockaddr *addr, uint8_t *pusrdata )
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
char *out;
|
||||
cJSON * pJsonRoot = cJSON_Parse( pusrdata );
|
||||
if ( !pJsonRoot )
|
||||
{
|
||||
os_log( "this is not a json data:\r\n%s\r\n", pusrdata );
|
||||
return;
|
||||
}
|
||||
|
||||
// //<2F><><EFBFBD><EFBFBD>device report
|
||||
// os_printf( "start json:device report\r\n" );
|
||||
// cJSON *p_cmd = cJSON_GetObjectItem( pJsonRoot, "cmd" );
|
||||
// if ( p_cmd && cJSON_IsString( p_cmd ) && os_strcmp( p_cmd->valuestring, "device report" ) == 0 )
|
||||
// {
|
||||
//
|
||||
// cJSON *pRoot = cJSON_CreateObject( );
|
||||
// cJSON_AddStringToObject( pRoot, "name", mqtt_device_id );
|
||||
// cJSON_AddStringToObject( pRoot, "mac", strMac );
|
||||
// cJSON_AddNumberToObject( pRoot, "type", TYPE );
|
||||
// cJSON_AddStringToObject( pRoot, "type_name", TYPE_NAME );
|
||||
// char *s = cJSON_Print( pRoot );
|
||||
// os_printf( "pRoot: %s\r\n", s );
|
||||
//
|
||||
// if ( addr )
|
||||
// {
|
||||
// if ( addr && pesp_conn->type == ESPCONN_UDP )
|
||||
// {
|
||||
// pesp_conn->type = ESPCONN_UDP;
|
||||
// pesp_conn->proto.udp->remote_port = 10181; //<2F><>ȡ<EFBFBD>˿<EFBFBD>
|
||||
// pesp_conn->proto.udp->remote_ip[0] = 255; //<2F><>ȡIP<49><50>ַ
|
||||
// pesp_conn->proto.udp->remote_ip[1] = 255;
|
||||
// pesp_conn->proto.udp->remote_ip[2] = 255;
|
||||
// pesp_conn->proto.udp->remote_ip[3] = 255;
|
||||
// }
|
||||
// espconn_send( pesp_conn, s, os_strlen( s ) ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// } else
|
||||
// {
|
||||
// user_mqtt_send( "domoticz/in", s );
|
||||
// }
|
||||
// cJSON_free( (void *) s );
|
||||
// cJSON_Delete( pRoot );
|
||||
// // cJSON_Delete(p_cmd);
|
||||
// }
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>idx<64>ֶ<EFBFBD>int<6E><74><EFBFBD><EFBFBD> description<6F>ֶ<EFBFBD>string<6E><67><EFBFBD><EFBFBD> name<6D>ֶ<EFBFBD>string<6E><67><EFBFBD><EFBFBD>
|
||||
cJSON *p_idx = cJSON_GetObjectItem( pJsonRoot, "idx" );
|
||||
cJSON *p_description = cJSON_GetObjectItem( pJsonRoot, "description" );
|
||||
cJSON *p_name = cJSON_GetObjectItem( pJsonRoot, "name" );
|
||||
|
||||
if ( p_idx && cJSON_IsNumber( p_idx ) && (p_idx->valueint>=3 &&p_idx->valueint<=9) ) //idx
|
||||
if ( p_idx && cJSON_IsNumber( p_idx ) && (p_idx->valueint >= 3 && p_idx->valueint <= 9) ) //idx
|
||||
{
|
||||
cJSON *p_nvalue = cJSON_GetObjectItem( pJsonRoot, "nvalue" );
|
||||
if ( p_nvalue )
|
||||
{
|
||||
user_led_set( p_nvalue->valueint );
|
||||
user_relay_set(p_idx->valueint-3,p_nvalue->valueint);
|
||||
user_relay_set( p_idx->valueint - 3, p_nvalue->valueint );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
if (
|
||||
(p_idx && cJSON_IsNumber( p_idx ) && p_idx->valueint == 2) //idx
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include "mico.h"
|
||||
#include "MiCOKit_EXT.h"
|
||||
|
||||
void user_function_cmd_received(uint8_t *pusrdata);
|
||||
void user_function_cmd_received(struct sockaddr *addr,uint8_t *pusrdata);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -394,7 +394,7 @@ OSStatus user_recv_handler( void *arg )
|
||||
require( p_recv_msg, exit );
|
||||
|
||||
app_log("user get data success! from_topic=[%s], msg=[%ld][%s].\r\n", p_recv_msg->topic, p_recv_msg->datalen, p_recv_msg->data);
|
||||
user_function_cmd_received( p_recv_msg->data );
|
||||
user_function_cmd_received(NULL, p_recv_msg->data );
|
||||
free( p_recv_msg );
|
||||
|
||||
exit:
|
||||
|
||||
@@ -92,7 +92,7 @@ OSStatus user_rtc_init( void )
|
||||
// MAX_MQTT_SEND_QUEUE_SIZE );
|
||||
// require_noerr_action( err, exit, app_log("ERROR: create mqtt msg send queue err=%d.", err) );
|
||||
|
||||
/* start mqtt client */
|
||||
/* start rtc client */
|
||||
err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY, "rtc",
|
||||
(mico_thread_function_t) rtc_thread,
|
||||
0x1000, 0 );
|
||||
@@ -145,7 +145,9 @@ void rtc_thread( mico_thread_arg_t arg )
|
||||
rtc_time.year = (currentTime->tm_year + 1900) % 100;
|
||||
|
||||
// MicoRtcSetTime( &rtc_time ); //MicoRtc<74><63><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>ʱ!
|
||||
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);
|
||||
|
||||
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++ )
|
||||
|
||||
@@ -32,7 +32,7 @@ static void sntp_time_call_back( void )
|
||||
|
||||
|
||||
MicoRtcGetTime(&rtc_time);
|
||||
os_log("time:20%d/%d/%d %d %d:%d:%d",rtc_time.year,rtc_time.month,rtc_time.date,rtc_time.weekday,rtc_time.hr,rtc_time.min,rtc_time.sec);
|
||||
os_log("SNTP time:20%d/%d/%d %d %d:%d:%d",rtc_time.year,rtc_time.month,rtc_time.date,rtc_time.weekday,rtc_time.hr,rtc_time.min,rtc_time.sec);
|
||||
|
||||
}
|
||||
|
||||
|
||||
96
TC1/user_udp.c
Normal file
96
TC1/user_udp.c
Normal file
@@ -0,0 +1,96 @@
|
||||
#define os_log(format, ...) custom_log("UDP", format, ##__VA_ARGS__)
|
||||
|
||||
#include "main.h"
|
||||
|
||||
#define LOCAL_UDP_PORT 10182
|
||||
|
||||
|
||||
#define MAX_UDP_DATA_SIZE (1024)
|
||||
#define MAX_UDP_SEND_QUEUE_SIZE (5)
|
||||
mico_queue_t udp_msg_send_queue = NULL;
|
||||
|
||||
void udp_thread( void *arg );
|
||||
|
||||
OSStatus user_udp_init( void )
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
/* start udp client */
|
||||
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." );
|
||||
|
||||
if ( kNoErr != err ) os_log("ERROR, app thread exit err: %d", err);
|
||||
|
||||
exit:
|
||||
return err;
|
||||
|
||||
}
|
||||
|
||||
/*create udp socket*/
|
||||
void udp_thread( void *arg )
|
||||
{
|
||||
UNUSED_PARAMETER( arg );
|
||||
|
||||
OSStatus err;
|
||||
struct sockaddr_in addr;
|
||||
fd_set readfds;
|
||||
socklen_t addrLen = sizeof(addr);
|
||||
int udp_fd = -1, len;
|
||||
char ip_address[16];
|
||||
uint8_t *buf = NULL;
|
||||
|
||||
// /* create udp msg send queue */
|
||||
// err = mico_rtos_init_queue( &udp_msg_send_queue, "uqp_msg_send_queue", MAX_UDP_DATA_SIZE/*sizeof(p_mqtt_send_msg_t)*/,
|
||||
// MAX_UDP_SEND_QUEUE_SIZE );
|
||||
// require_noerr_action( err, exit, os_log( "ERROR: create mqtt msg send queue err=%d.", err ) );
|
||||
|
||||
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 );
|
||||
|
||||
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 );
|
||||
|
||||
os_log("Open local UDP port %d", LOCAL_UDP_PORT);
|
||||
|
||||
while ( 1 )
|
||||
{
|
||||
FD_ZERO( &readfds );
|
||||
FD_SET( udp_fd, &readfds );
|
||||
|
||||
require_action( select( udp_fd + 1, &readfds, NULL, NULL, NULL ) >= 0, exit, err = kConnectionErr );
|
||||
|
||||
/*Read data from udp and send data back */
|
||||
if ( FD_ISSET( udp_fd, &readfds ) )
|
||||
{
|
||||
len = recvfrom( udp_fd, buf, 1024, 0, (struct sockaddr *) &addr, &addrLen );
|
||||
require_action( len >= 0, exit, err = kConnectionErr );
|
||||
|
||||
// os_log( "udp s_type:0x%x",((struct sockaddr )addr).s_type);
|
||||
// os_log( "udp s_port:0x%x",((struct sockaddr )addr).s_port);
|
||||
// os_log( "udp s_ip:0x%x",((struct sockaddr )addr).s_type);
|
||||
// os_log( "udp s_spares: %x %x %x %x %x %x",((struct sockaddr )addr).s_spares[0],((struct sockaddr )addr).s_spares[1],
|
||||
// ((struct sockaddr )addr).s_spares[2],((struct sockaddr )addr).s_spares[3],
|
||||
// ((struct sockaddr )addr).s_spares[4],((struct sockaddr )addr).s_spares[5],);
|
||||
|
||||
strcpy( ip_address, inet_ntoa( addr.sin_addr ) );
|
||||
// os_log( "udp recv from %s:%d, len:%d :%s", ip_address,addr.sin_port, len ,buf);
|
||||
sendto( udp_fd, buf, len, 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in) );
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
if ( err != kNoErr )
|
||||
os_log("UDP thread exit with err: %d", err);
|
||||
if ( buf != NULL ) free( buf );
|
||||
mico_rtos_delete_thread( NULL );
|
||||
}
|
||||
|
||||
|
||||
13
TC1/user_udp.h
Normal file
13
TC1/user_udp.h
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
#ifndef __USER_UDP_H_
|
||||
#define __USER_UDP_H_
|
||||
|
||||
|
||||
#include "mico.h"
|
||||
#include "MiCOKit_EXT.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
@@ -67,9 +67,9 @@ void wifi_easylink_completed_handle( network_InitTypeDef_st *nwkpara, void * arg
|
||||
//wifi<66><69><EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD>ȡ<EFBFBD><C8A1>IP<49><50>ַ <20>ص<EFBFBD>
|
||||
static void wifi_get_ip_callback( IPStatusTypedef *pnet, void * arg )
|
||||
{
|
||||
os_log("got IP:%s", pnet->ip);
|
||||
os_log("got IP:%s, MAC:%s", pnet->ip,pnet->mac);
|
||||
wifi_status = WIFI_STATE_CONNECTED;
|
||||
|
||||
strcpy(strMac, pnet->mac);
|
||||
}
|
||||
//wifi<66><69><EFBFBD><EFBFBD>״̬<D7B4>ı<EFBFBD><C4B1>ص<EFBFBD>
|
||||
static void wifi_status_callback( WiFiEvent status, void *arg )
|
||||
|
||||
Reference in New Issue
Block a user