static tcp_cli_t g_ota_client;
extern char * devinfo_report_str( void );
+extern void iot_rpt_init( void );
int32_t eth_devinfo_report( tcp_cli_t *client )
{
}
rpt_data = devinfo_report_str();
+ if( !rpt_data ){
+ //数据不变,不上报
+ return 0;
+ }
if( strlen( rpt_data) > tcp_sndbuf(g_iot_client.pcb) ){
debug_printf( "tx len[%d] > sendbuf[%d]\n", strlen(rpt_data), tcp_sndbuf(g_iot_client.pcb) );
debug_printf( "tcp sent %d Bytes\n", len );
return ERR_OK;
}
+
+extern void iot_resp_req( uint8_t *resp, uint32_t sz );
+extern uint32_t resp_data_get( uint8_t **pdata, uint32_t *sz );
+extern void resp_data_clean( void );
+
static int32_t iot_act_resp = 0;
static err_t tcp_eth_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
int32_t act = iot_cmd_parse( 1, (char *)p->payload );
if( act == 1 || act == 2 ){
iot_act_resp = 1;
- }
+ }else if( act == 4){
+ //需要原包返回到平台
+ iot_resp_req( p->payload, p->tot_len );
+ iot_act_resp = 4;
+ }
}else if( client == &g_ota_client ){
//TODO:
extern int32_t ota_handle_http_resp_data( uint8_t *rx_data, uint32_t rx_sz );
}
g_dev.eth_connected = 2;
+ iot_rpt_init();
+
//5.定时上报
last_ticks = xTaskGetTickCount();
rpt_secs = g_dev.cfg.iot_rpt_period;
if( tcp_write( g_iot_client.pcb, s, strlen(s), 1 ) == ERR_OK ){
tcp_output( g_iot_client.pcb );
}
+ iot_act_resp = 0;
+ }else if( iot_act_resp == 4 ){
+ uint8_t *tx_buf;
+ uint32_t tx_sz;
+ if( resp_data_get( &tx_buf,&tx_sz) > 0 ){
+ if( tcp_write( g_iot_client.pcb, tx_buf, tx_sz, 1 ) == ERR_OK ){
+ tcp_output( g_iot_client.pcb );
+ }
+ resp_data_clean();
+ }
iot_act_resp = 0;
}
#include "user.h"
#include "cJSON.h"
+extern void ui_cfg_remote_update( void );
+
+__attribute__((section(".DTCM"),zero_init)) static uint8_t tcp_resp_buf[ 512 ];
+static uint16_t tcp_resp_n;
+
+
static char mac_str_buf[20]; //"00:00:00:00:00:00";
+static char g_sw_ver[20];
static char *g_mac_str;
+
+
+
#if 0
static const char *fmt = "{\n\
\"cmd\":\"rt\",\n\
#else
static const char *fmt = "{\n\
- \"cmd\":\"rt\",\n\
- \"cid\":%d,\n\
- \"mac\":\"%s\",\n\
- \"val\":{\n\
- \"c\":\"%0.1f\",\n\
- \"s\":\"%d\",\n\
- \"g\":\"%0.1f\"\n\
- },\n\
- \"weldTech\":\"%s\",\n\
- \"solarTermFactor\":\"%d\",\n\
- \"gasPressure\":\"%d\",\n\
- \"totalFlow\":\"%d\",\n\
- \"onlineStatus\":\"%d\",\n\
- \"softVersion\":\"V%d.%d\",\n\
- \"lock\":\"%d\",\n\
- \"deviceNo\":\"%s\",\n\
- \"gasPathType\":%d,\n\
- \"airRatio\":%d,\n\
- \"syncFrequency\":%d\n\
- }";
+\"cmd\":\"rt\",\n\
+\"cid\":%d,\n\
+\"mac\":\"%s\",\n\
+\"val\":{\n\
+ \"c\":\"%0.1f\",\n\
+ \"s\":\"%d\",\n\
+ \"g\":\"%0.1f\"\n\
+},\n\
+\"weldTech\":\"%s\",\n\
+\"solarTermFactor\":\"%d\",\n\
+\"gasPressure\":\"%d\",\n\
+\"totalFlow\":\"%d\",\n\
+\"onlineStatus\":\"%d\",\n\
+\"softVersion\":\"%s\",\n\
+\"lock\":\"%d\",\n\
+\"deviceNo\":\"%s\",\n\
+\"gasPathType\":%d,\n\
+\"airRatio\":%d,\n\
+\"syncFrequency\":%d,\n\
+\"gasType\":%d\n\
+}";
#endif
+typedef struct{
+ uint32_t cid;
+ char* mac;
+ uint32_t curr_x10;
+ uint32_t weld_st;
+ uint32_t flow_x10;
+ uint32_t weld_tech;
+ uint32_t factor;
+ uint32_t pressure;
+ uint32_t total_flow;
+ uint32_t online_st;
+ char* ver;
+ uint32_t lock;
+ char* deviceNo;
+ uint32_t path;
+ uint32_t prev;
+ uint32_t freq;
+ uint32_t gas; //1,2,3
+}rpt_info_t;
+
+static rpt_info_t g_rpt_sent;
+
+
+extern float welding_curr_disp_value( float curr );
+static void mac_str_init( void );
+
+void iot_rpt_init( void )
+{
+ g_rpt_sent.online_st = 0;//改成0, 后面一定会上报一帧数据
+}
+
+int32_t get_new_rpt_info( rpt_info_t *rpt )
+{
+ //return 0:正常上报, -1:不上报
+ if(!rpt)
+ return -1;
+
+ if( !g_mac_str ){
+ mac_str_init();
+ }
+
+ rpt->cid = g_rpt_sent.cid;
+ rpt->mac = g_mac_str;
+ rpt->curr_x10 = (uint32_t)(welding_curr_disp_value( g_dev.welding_current )*10);
+ rpt->weld_st = g_dev.welding_start;
+ rpt->flow_x10 = (uint32_t)(g_dev.flowmeter.flow_speed*10);
+ rpt->weld_tech = g_dev.cfg.welding_method;
+ rpt->factor = g_dev.cfg.save_gas_factor,
+ rpt->pressure = (uint32_t)g_dev.pressure;
+ rpt->total_flow = (uint32_t)g_dev.flowmeter.flow_total;
+ rpt->online_st = 1;
+ rpt->ver = g_sw_ver;
+ rpt->lock = g_dev.cfg.remote_locked==1?0:1;
+ rpt->deviceNo = g_dev.serial_number;
+ rpt->path = g_dev.cfg.bypass?0:1, //平台定义节气为1, 旁路为0
+ rpt->prev = g_dev.cfg.valve_open_default;
+ rpt->freq = g_dev.cfg.iot_rpt_period;
+
+ if( g_dev.cfg2.gas > GAS_DEFAULT && g_dev.cfg2.gas <= GAS_MAX ){
+ rpt->gas = g_dev.cfg2.gas;
+ }else{
+ if( g_dev.cfg.cal.flow_coeff_x1000 == 570 )
+ rpt->gas = 2;
+ else if( g_dev.cfg.cal.flow_coeff_x1000 == 910 )
+ rpt->gas = 3;
+ else
+ rpt->gas = 1;
+ }
+
+ if( memcmp( rpt, &g_rpt_sent, sizeof( g_rpt_sent ) ) ){
+ rpt->cid++;
+ return 0;
+ }
+ return -1;
+}
void offline_time_update( uint32_t add_secs )
{
device_get_mac(mac);
sprintf( mac_str_buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] );
g_mac_str = mac_str_buf;
+ sprintf( g_sw_ver, "V%d.%d", APP_VER_MAJOR, APP_VER_MINOR );
}
static const char * weld_method_str[3] = {"TIG", "MIG", "MAG" };
-extern float welding_curr_disp_value( float curr );
char * devinfo_report_str( void )
{
- static uint32_t cid = 0;
static char tx_buf[500];
- float c,g;
- uint32_t s;
+ static rpt_info_t to_rpt;
- if( !g_mac_str ){
- mac_str_init();
- }
+ if( get_new_rpt_info( &to_rpt ) < 0 ){
+ return NULL;
+ }
+ memcpy( &g_rpt_sent, &to_rpt, sizeof( g_rpt_sent ) );
- //c = g_dev.welding_current;
- g = g_dev.flowmeter.flow_speed;
- s = g_dev.welding_start;
-
- //+10A 到 -10A 上报0,与屏上显示一致
- c = welding_curr_disp_value( g_dev.welding_current );
-
- sprintf( (char*)tx_buf, fmt, cid++, g_mac_str, c, s, g,
- weld_method_str[g_dev.cfg.welding_method],
- g_dev.cfg.save_gas_factor,
- (int)g_dev.pressure,
- (int)g_dev.flowmeter.flow_total,
- 1,
- APP_VER_MAJOR, APP_VER_MINOR,
- g_dev.cfg.remote_locked==1?0:1,
- g_dev.serial_number,
- g_dev.cfg.bypass?0:1, //平台定义节气为1, 旁路为0
- g_dev.cfg.valve_open_default,
- g_dev.cfg.iot_rpt_period
+ sprintf( (char*)tx_buf, fmt,
+ g_rpt_sent.cid,
+ g_rpt_sent.mac,
+ g_rpt_sent.curr_x10/10.0f,
+ g_rpt_sent.weld_st,
+ g_rpt_sent.flow_x10/10.0f,
+ weld_method_str[g_rpt_sent.weld_tech],
+ g_rpt_sent.factor,
+ g_rpt_sent.pressure,
+ g_rpt_sent.total_flow,
+ g_rpt_sent.online_st,
+ g_rpt_sent.ver,
+ g_rpt_sent.lock,
+ g_rpt_sent.deviceNo,
+ g_rpt_sent.path,
+ g_rpt_sent.prev,
+ g_rpt_sent.freq,
+ g_rpt_sent.gas
);
return tx_buf;
return tx_buf;
}
+#if 0
+//节气因子,远程修改
+{
+ "action":"updateSolarTermFactor",
+ "timeStamp":1716445485381,
+ "data":{
+ "mac":"123",
+ "solarTermFactor":"10"
+ }
+}
+//气路选择,远程修改
+{
+ "action":"updateGasPathType",
+ "timeStamp":1716445485381,
+ "data":{
+ "mac":"123",
+ // 1 节气 0不节气
+ "gasPathType":1
+ }
+}
+//设备回应,原包返回
+#endif
+
int32_t iot_cmd_parse( uint32_t link, char *cmd )
{
+ cJSON * data;
+ cJSON * ver;
+ cJSON * url;
+ cJSON * mac;
+ cJSON * gasPathType;
+ cJSON * solarTermFactor;
+
+
int32_t act = 0;
cJSON*root = cJSON_Parse( (char*)cmd );
if( root ){
g_dev.cfg.remote_locked = 1;
dev_cfg_store( &g_dev.cfg );
act = 1;
+ }else if( strcmp( action->valuestring, "updateSolarTermFactor" )== 0 ){
+ data = cJSON_GetObjectItem( root, "data" );
+ debug_printf( "-----ota----- " );
+ if(data){
+ mac = cJSON_GetObjectItem( data, "mac" );
+ solarTermFactor = cJSON_GetObjectItem( data, "solarTermFactor" );
+ if( mac && solarTermFactor && strcasecmp( mac->valuestring, g_mac_str ) == 0 ){
+ int32_t factor = atoi( solarTermFactor->valuestring );
+ if( factor >=2 && factor <=30 ){
+ g_dev.cfg.save_gas_factor = factor;
+ dev_cfg_store( &g_dev.cfg );
+ ui_cfg_remote_update();
+ act = 4; //原包回发。
+ }
+ }
+ }
+ }else if( strcmp( action->valuestring, "updateGasPathType" )== 0 ){
+ data = cJSON_GetObjectItem( root, "data" );
+ debug_printf( "-----ota----- " );
+ if(data){
+ mac = cJSON_GetObjectItem( data, "mac" );
+ gasPathType = cJSON_GetObjectItem( data, "gasPathType" );
+ if( mac && gasPathType && strcasecmp( mac->valuestring, g_mac_str ) == 0 ){
+ if( gasPathType->valueint == 1 || gasPathType->valueint == 0 ){ // 1 节气 0不节气
+ g_dev.cfg.bypass = (g_dev.has_bypass && gasPathType->valueint == 0)?1:0;
+ dev_cfg_store( &g_dev.cfg );
+ ui_cfg_remote_update();
+ act = 4; //原包回发。
+ }
+ }
+ }
+
}else if( strcmp( action->valuestring, "unlockDevice" )== 0 ){
debug_printf( "unlockdevice %d->0\n", g_dev.cfg.remote_locked );
g_dev.cfg.remote_locked = 0;
act = 2;
}else if( strcmp( action->valuestring, "update" )== 0 ){
//ota;
- cJSON * data;
- cJSON * ver;
- cJSON * url;
+
data = cJSON_GetObjectItem( root, "data" );
debug_printf( "-----ota----- " );
if(data){
}
return act;
}
+
+void iot_resp_req( uint8_t *resp, uint32_t sz )
+{
+ if( sz > sizeof( tcp_resp_buf) )
+ sz = sizeof(tcp_resp_buf);
+ memcpy( tcp_resp_buf, resp, sz );
+ tcp_resp_n = sz;
+}
+
+uint32_t resp_data_get( uint8_t **pdata, uint32_t *sz )
+{
+ *pdata = tcp_resp_buf;
+ *sz = tcp_resp_n;
+ return tcp_resp_n;
+}
+void resp_data_clean( void )
+{
+ tcp_resp_n = 0;
+}
+
extern int32_t ota_handle_http_resp_data( uint8_t *rx_data, uint32_t rx_sz );
extern int32_t wifi_ota_start( char * url, char *ver );
extern char * devinfo_report_str( void );
+extern void iot_rpt_init( void );
extern UART_HandleTypeDef huart5;
__attribute__((section(".DTCM"),zero_init)) static uint8_t ota_rx_buf[2048];
static uint16_t ota_rx_n;
+
+
+
int32_t wifi_tcp_tx( uint8_t link, uint8_t *data, uint32_t sz );
void wifi_uart_rx_start( void )
char * rpt_data;
rpt_data = devinfo_report_str();
+ if( !rpt_data ){
+ //数据不变,不上报
+ return 0;
+ }
wifi_tcp_tx( CONN_ID_IOT, (uint8_t *)rpt_data, strlen((char *)rpt_data) );
return 0;
}
+extern void iot_resp_req( uint8_t *resp, uint32_t sz );
+extern uint32_t resp_data_get( uint8_t **pdata, uint32_t *sz );
+extern void resp_data_clean( void );
+
static int32_t iot_act_resp = 0;
static void tcp_rx_data_handle(void *arg, char *rspinfo, int rsplen)
{
//char * s = lock_resp_str();
//wifi_tcp_tx(0, (uint8_t*)s, strlen(s) );
iot_act_resp = 1;
+ }else if( act == 4){
+ //需要原包返回到平台
+
+ iot_resp_req( tcp_rx_buf, tcp_rx_n );
+ iot_act_resp = 4;
}
}
tcp_rx_n = 0;
return ret;
}
+static void wifi_tcp_closed_cb(void *arg, char *rspinfo, int rsplen)
+{
+ if( g_dev.wifi_connected == 2 )
+ g_dev.wifi_connected = 1 ;
+}
+
void user_wifi_task( void* unused )
{
char *ssid;
}
at_register_callback("+IPD", NULL, NULL, 0, tcp_rx_data_handle, NULL);
-// at_register_callback( "link is not valid", NULL, NULL, 0, link_down, NULL );
+ at_register_callback( "0,CLOSED", NULL, NULL, 0, wifi_tcp_closed_cb, NULL );
while( 1 ){
ssid = g_dev.cfg.net.wifi_ssid;
//tcp connect
if( tcp_server_connect( CONN_ID_IOT, g_dev.cfg.net.tcp_server, g_dev.cfg.net.tcp_port ) == 0 ){
g_dev.wifi_connected = 2;
+ if( g_dev.eth_connected != 2 ){
+ iot_rpt_init(); //tcp连接上,开始上报一帧数据, 以免设备信息不变一直不上报。
+ }
}else{
osDelay( 1000 );
continue;
char * s = lock_resp_str();
wifi_tcp_tx( CONN_ID_IOT, (uint8_t*)s, strlen(s) );
iot_act_resp = 0;
+ }else if( iot_act_resp == 4 ){
+ uint8_t *tx_buf;
+ uint32_t tx_sz;
+ if( resp_data_get( &tx_buf,&tx_sz) > 0 ){
+ wifi_tcp_tx( CONN_ID_IOT, tx_buf, tx_sz );
+ resp_data_clean();
+ }
+ iot_act_resp = 0;
}
if( g_dev.eth_connected == 2 ){
//如果有线已经连接,wifi不上传数据