From 041bf2e69e1ba2b336179a7a511add08674338fd Mon Sep 17 00:00:00 2001 From: kyz Date: Tue, 25 Mar 2025 16:44:23 +0800 Subject: [PATCH] add sensor board scan --- app/inc/alarm_monitor.h | 13 +- app/inc/am_tcp.h | 31 ++++ app/src/{ma_monitor.c => am_main.c} | 10 +- app/src/button.c | 2 +- app/src/crc16.c | 133 ++++++++++++++ app/src/crc16.h | 42 +++++ app/src/iot_report.c | 19 ++ app/src/lcd.c | 0 app/src/sensor_board.c | 265 ++++++++++++++++++++++++++++ app/src/tcp.c | 40 +++-- 10 files changed, 537 insertions(+), 18 deletions(-) create mode 100644 app/inc/am_tcp.h rename app/src/{ma_monitor.c => am_main.c} (97%) create mode 100755 app/src/crc16.c create mode 100755 app/src/crc16.h create mode 100644 app/src/iot_report.c mode change 100755 => 100644 app/src/lcd.c create mode 100644 app/src/sensor_board.c diff --git a/app/inc/alarm_monitor.h b/app/inc/alarm_monitor.h index cee3406..e610d27 100644 --- a/app/inc/alarm_monitor.h +++ b/app/inc/alarm_monitor.h @@ -12,7 +12,7 @@ #include #include -#include "cQueue.h" +#include "cQueue.h" #include "cJSON.h" #include "button.h" @@ -96,7 +96,12 @@ typedef struct{ }alarm_t; typedef struct{ - uint32_t dummy; + uint32_t pressure; + uint32_t alarm; +}sound_t; + +typedef struct{ + sound_t sound; }sensor_data_t; typedef struct{ @@ -111,7 +116,7 @@ typedef struct{ //配置参数 factory_info_t factory_info; dev_cfg_t cfg; - + pthread_mutex_t lock; //设备状态信息 @@ -129,7 +134,7 @@ typedef struct{ int32_t ctrl_mode; //0:IDLE, 1:ALARM, 2:SETTING, 3:UNLOCKING int32_t monitor_mode; //0:IDLE - uv_loop_t* loop; //uv_loop + uv_loop_t* loop; //uv_loop }am_dev_t; int32_t store_device_cfg( dev_cfg_t *cfg ); diff --git a/app/inc/am_tcp.h b/app/inc/am_tcp.h new file mode 100644 index 0000000..7088308 --- /dev/null +++ b/app/inc/am_tcp.h @@ -0,0 +1,31 @@ +#ifndef __AM_TCP_H +#define __AM_TCP_H +#include +#include +#include +#include +#include +#include + +typedef void (*tcp_rx_cb_t)( uv_stream_t * client, uint8_t *data, uint16_t data_sz ); +typedef struct{ + const char * name; + uv_timer_t timer; + uv_loop_t *loop; + char * host; + char port[8]; +// int state; //0:init, 1:resolved, 2:connected, + int connected; + uv_getaddrinfo_t resolver; + uv_tcp_t sock; + uv_connect_t conn_req; + uv_write_t write_req; + tcp_rx_cb_t rx_cb; + void * data; +}tcp_cli_t; + +void tcp_cli_init( tcp_cli_t * cli, char * host, uint16_t port, char *name, tcp_rx_cb_t cb ); +void tcp_cli_start( tcp_cli_t *cli, int delay_ms ); + +#endif + diff --git a/app/src/ma_monitor.c b/app/src/am_main.c similarity index 97% rename from app/src/ma_monitor.c rename to app/src/am_main.c index 06ce16b..00d9c62 100644 --- a/app/src/ma_monitor.c +++ b/app/src/am_main.c @@ -80,7 +80,7 @@ int32_t device_lcd_refresh( am_dev_t *dev ) return 0; } -int32_t button_event_cb( button_code_t btn_code, button_event_t btn_event, void* value ) +void button_event_cb( button_code_t btn_code, button_event_t btn_event, void* value ) { int32_t alarm = 0; @@ -123,7 +123,7 @@ int32_t button_event_cb( button_code_t btn_code, button_event_t btn_event, void* } - return 0; + return; } int32_t device_lcd_init( am_dev_t *dev ) @@ -169,6 +169,8 @@ void monitor_timer_cb( uv_timer_t *handle ) } +extern void sensor_board_init( am_dev_t *dev ); + int main( void ) { am_dev_t *dev = &g_dev; @@ -190,13 +192,15 @@ int main( void ) device_button_init( dev ); device_lcd_init( dev ); //device_sensor_init( dev ); - if(1){ + if(0){ extern void tcp_test( void ); tcp_test(); } + sensor_board_init( dev ); event_recorder_init(); //init sqlite + uv_timer_init( dev->loop, &monitor_timer ); uv_timer_start( &monitor_timer, monitor_timer_cb, 10, 10 ); diff --git a/app/src/button.c b/app/src/button.c index 543a29d..5e89f66 100644 --- a/app/src/button.c +++ b/app/src/button.c @@ -270,7 +270,7 @@ static void button_single_cb( button_code_t btn_code, button_event_t btn_event, //static const char *btn_dev_path = "/dev/input/by-path/platform-gpio_keys-event"; static const char *btn_dev_path = "/dev/input/event2"; -extern int32_t button_event_cb( button_code_t btn_code, button_event_t btn_event, void* value ); +extern void button_event_cb( button_code_t btn_code, button_event_t btn_event, void* value ); void device_button_init( am_dev_t *dev ) { diff --git a/app/src/crc16.c b/app/src/crc16.c new file mode 100755 index 0000000..8f134e6 --- /dev/null +++ b/app/src/crc16.c @@ -0,0 +1,133 @@ +/** + * + * _ _| \/ |___ __| | |__ _ _ __| \/ |__ _ __| |_ ___ _ _ + * | || | |\/| / _ \/ _` | '_ \ || (_-< |\/| / _` (_-< _/ -_) '_| + * \_,_|_| |_\___/\__,_|_.__/\_,_/__/_| |_\__,_/__/\__\___|_| + * + * File : mbmCRC16.c + * Copyright (C) <2018> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +/* ----------------------- ANSI-C include files -----------------------------*/ +#include +#include +/* ----------------------- Modbus core include files -----------------------------*/ +/** + * @addtogroup CRC16 const define + * @note Private + */ + +/*@{*/ + +static const uint8_t CRC16_HI[] = { + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, + 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40 +}; + +static const uint8_t CRC16_LO[] = { + 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, + 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, + 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, + 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, + 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, + 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, + 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, + 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, + 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, + 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, + 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, + 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, + 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, + 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, + 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, + 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, + 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, + 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, + 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, + 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, + 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, + 0x41, 0x81, 0x80, 0x40 +}; + +/*@}*/ + +/** + * @addtogroup CRC16 functions + * @note Private + */ + +/*@{*/ + +/** + * 计算一定长度数据的CRC16值 + * @note [Rev.1] + * + * @param *data, 数据 + * @param length, 长度 + * + * @return uint16_t, 计算结果 + */ +uint16_t mbm_CRC16Calc(uint8_t *data, uint32_t length) +{ + uint8_t crcHI = 0xFF; + uint8_t crcLO = 0xFF; + int32_t index; + + while (length--) { + index = crcLO ^ *(data++); + crcLO = (uint8_t) (crcHI ^ CRC16_HI[index]); + crcHI = CRC16_LO[index]; + } + + return (uint16_t) (crcHI << 8 | crcLO); +} + +uint16_t mbm_crc16( uint16_t init, uint8_t *data, uint32_t length) +{ + uint8_t crcHI = (init>>8)&0xFF; + uint8_t crcLO = init&0xFF; + int32_t index; + + while (length--) { + index = crcLO ^ *(data++); + crcLO = (uint8_t) (crcHI ^ CRC16_HI[index]); + crcHI = CRC16_LO[index]; + } + + return (uint16_t) (crcHI << 8 | crcLO); +} + +/*@}*/ diff --git a/app/src/crc16.h b/app/src/crc16.h new file mode 100755 index 0000000..b149a05 --- /dev/null +++ b/app/src/crc16.h @@ -0,0 +1,42 @@ +/** + * __ __ _ _ __ __ _ + * _ _| \/ |___ __| | |__ _ _ __| \/ |__ _ __| |_ ___ _ _ + * | || | |\/| / _ \/ _` | '_ \ || (_-< |\/| / _` (_-< _/ -_) '_| + * \_,_|_| |_\___/\__,_|_.__/\_,_/__/_| |_\__,_/__/\__\___|_| + * + * File : mbmCRC16.h + * Copyright (C) <2018> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef MBMCRC16_H_ +#define MBMCRC16_H_ + +/* ----------------------- ANSI-C include files -----------------------------*/ +#include +#include + +/** + * @addtogroup CRC16 functions + * @note Private + */ + +/*@{*/ + +extern uint16_t mbm_CRC16Calc(uint8_t *data, uint16_t length); + +/*@}*/ + +#endif diff --git a/app/src/iot_report.c b/app/src/iot_report.c new file mode 100644 index 0000000..7ed8c57 --- /dev/null +++ b/app/src/iot_report.c @@ -0,0 +1,19 @@ +#include "alarm_monitor.h" +/* +typedef struct { +}rpt_item_t; + +char * wrap_dykz( int jbqtdzt, int jbkzqtdzt ); +char * wrap_dycs( int dl, int dy, int gl); +char * wrap_jbqzt( int gzzt, int jkdy, int dtdy, int dydy ); +char * wrap_hbdyzt( int cdzt, int dl, int dlzt, int dy, int gdzt, int gzzt, int kqzt, int whzt ); +char * wrap_jzsb( +char * wrap_hw( +char * wrap_hjgz( +char * wrap_spjk( +char * wrap_mj( int zt, int crmb, int crsj, int ); +char * wrap_time( + + +char wrap_rpt_pkt( int incresect, char * rpt_plain ) + */ diff --git a/app/src/lcd.c b/app/src/lcd.c old mode 100755 new mode 100644 diff --git a/app/src/sensor_board.c b/app/src/sensor_board.c new file mode 100644 index 0000000..5fa98c8 --- /dev/null +++ b/app/src/sensor_board.c @@ -0,0 +1,265 @@ +#include "alarm_monitor.h" +#include "am_tcp.h" +#include "crc16.h" + +#define get_u16( p ) ( ((p[0])<<8)|(p[1]<<0) ) +#define get_u32( p ) ( ((p[0])<<24)|(p[1]<<16)|((p[2])<<8)|(p[3]<<0) ) + +typedef struct{ + int32_t sound_pressure; // + int32_t sound_type; // +}sound_info_t; + + +typedef struct{ + sound_info_t sound; + uv_timer_t sound_timer; + am_dev_t *dev; + + tcp_cli_t tcp_client; + + uint16_t tx_pkt_sn; + uint16_t rx_pkt_sn; + void*data; + char *name; + +}sensor_board_t; + +static sensor_board_t s_board; + + + +int query_sound_info( void ) +{ + //tcp tx query cmd + return 0; +} + +static uint16_t pkt_sn = 0; + + +int handle_rx_pkt( uint8_t *rx_data, uint16_t rx_sz ) +{ +#if 0 + uint8_t *p = buff; + uint16_t crc; + uint16_t flag, sn, cmd, data_sz; + char *p_data; + p=rx_data; flag = get_u16(p); + p+=2; sn = get_u16(p); + p+=2; cmd = get_u16(p); + p+=2; data_sz = get_u16(p); + p+=2; p_data = p; + p+= data_sz; crc = get_u16(p); + + if( crc == 0 ){ + //timeout_flag = 0; + uv_timer_start( &sound_timeout_timer, sound_timer_cb, 30*1000, 0 ); //reset timeout + if( cmd == 0x0002 || cmd == 0x0003 ){ + //Sensor Rx data + g_sound.pressure = p_data[0]; + g_sound.alarm_type = p_data[3]; + }else if( cmd == 0x1002 ){ + //Lora Rx + }else if( cmd == 0x2002 ){ + //Laki Rx + }else if( cmd == 0x3002 ){ + //BD Rx + } + } +#endif + return 0; +} + +extern void tcp_write_data( tcp_cli_t *cli, char *tx_data, int tx_size ); +extern void tcp_write_buf( tcp_cli_t *cli, uv_buf_t *buf ); + +uv_buf_t wrap_sensor_scan_pkt( sensor_board_t *board ); + +void sound_timer_cb( uv_timer_t * handle ) +{ + sensor_board_t *board; + uv_buf_t tx_buf; + + board = (sensor_board_t *)handle->data; + + if( board->tcp_client.connected ){ + tx_buf = wrap_sensor_scan_pkt( board ); + if( tx_buf.base ){ + tcp_write_buf( &board->tcp_client, &tx_buf ); + free( tx_buf.base ); + } + } +} +int read_sound_info( int* sound_pressure, int * sound_type ); + +void sensor_board_rx_cb( uv_stream_t *client, uint8_t * rx_data, uint16_t rx_sz ) +{ + tcp_cli_t *p_cli = (tcp_cli_t*)client->data; + sensor_board_t *board = p_cli->data; + am_dev_t *dev = board->data; + + printf( "[%s] rx cb : data:%p-[%02X %02X %02X ..., len:%d\n", board->name, rx_data, rx_data[0], rx_data[1], rx_data[2], rx_sz ); + uint16_t flag, sn, cmd, data_len, crc; + uint8_t *p = rx_data; + flag = get_u16(p); p+=2; + if( rx_sz >= 10 && flag == 0xaa55 ){ + uint8_t *p_data; + sn = get_u16(p); p+=2; + cmd = get_u16(p); p+=2; + data_len = get_u16(p); p+=2; + p_data = p; + p+= data_len; + crc = get_u16(p); p+=2; + if( crc == mbm_CRC16Calc( rx_data, data_len+8 ) ){ + //get a pkt + if( cmd == 0x0002 ){ + uint32_t pressure, alarm; + pressure = get_u32( p ); p+=4; + alarm = get_u32(p); + dev->sensor_data.sound.pressure = pressure; + dev->sensor_data.sound.alarm= alarm; + } + } + + } + +} + +void sensor_board_init( am_dev_t *dev ) +{ + printf( "sensor board init ...\n" ); + memset( &s_board, 0, sizeof(s_board) ); + s_board.name = "BoardX"; + s_board.dev = dev; + + uv_timer_init( dev->loop, &s_board.sound_timer ); + s_board.sound_timer.data = &s_board; + uv_timer_start( &s_board.sound_timer, sound_timer_cb, 100, 5000 ); + + s_board.tcp_client.data = &s_board; + tcp_cli_init( &s_board.tcp_client, "kyz.pub", 9999, "sensorboard", sensor_board_rx_cb ); + tcp_cli_start( &s_board.tcp_client, 0 ); + printf( "SensorBoard: tcp client connect to kyz.pub:9999\n" ); +} + +//start timer 5s. +//timercb call query_sound_info, +//tcp_recv_cb fill info + + +/* +DTU: ETH/TCP + Lora/BD/Laki tx/rx + VoiceDetect + + +get sensor_board_info: + noise_P1_DB: x dB + alarm_X: x + + + + + TX Pkt + FLAG SN CMD LEN DATA CRC + 55AA 0002 0001 0008 XX... XXXX + + RX PKT + FLAG SN CMD LEN DATA CRC + 55AA 0002 0001 0008 XX... XXXX +CMD: + 0001: query LEN 0 query period:5s + 0002: resp LEN 8 , DATA: noise + alarm + 1001: Lora Tx + 1002: Lora Rx + 2001: BD Tx + 2002: BD Rx +*/ + +#if 0 +typedef struct{ + int channel; //0:Lora 1:BD 2:Laki + char * pdata; + int size; +}dtu_pkt_t; + +typedef struct{ + tcp_cli_t tcp; +}dtu_t; + + +static dtu_t g_dtu; +void dtu_init( void ) +{ + // create tcp socket client + tcp.data = &g_dtu; + tcp_cli_init( &g_dtu.tcp, "kyz.pub", 9999, "CLI_A" ); + tcp_cli_start( &g_dtu.tcp, 0 ); +} + + +void dtu_send( void * int ch, char * data, int sz ) +{ +} + +void dtu_lora_send( char * data, int sz ) +{ + dtu_send( 0, data, sz ); +} + +void dtu_bd_send( char * data, int sz ) +{ + dtu_send( 1, data, sz ); +} + +void dtu_laki_send( char * data, int sz ) +{ + dtu_send( 2, data, sz ); +} + +void dtu_recv_cb( ch, data, sz ) +{ + if( ch==0) dtu_lora_recv_cb( data, sz ); + elif( ch==1 ) dtu_bd_recv_cb( data, sz ); + elif( ch==2 ) dtu_laki_recv_cb( data, sz ); +} +#endif + +uv_buf_t wrap_tx_pkt( sensor_board_t *board, uint16_t cmd, uint8_t *data, uint16_t data_sz ) +{ + uint8_t *p; + uint16_t pkt_sn; + uint16_t crc; + + uv_buf_t buf; + + uint8_t *p_data = ( uint8_t*)malloc( data_sz+10 ); + if( p_data ){ + p = p_data; + board->tx_pkt_sn++; + pkt_sn = board->tx_pkt_sn; + + *p++ = (0x55AA>>8)&0xff; + *p++ = (0x55AA>>0)&0xff; + *p++ = (pkt_sn>>8)&0xff; + *p++ = (pkt_sn>>0)&0xff; + *p++ = (cmd>>8)&0xff; + *p++ = (cmd>>0)&0xff; + *p++ = (data_sz>>8)&0xff; + *p++ = (data_sz>>0)&0xff; + if( data_sz ) + memcpy( p, data, data_sz ); + p+=data_sz; + crc = mbm_CRC16Calc( p_data, data_sz+8); + *p++ = (crc>>8)&0xff; + *p++ = (crc>>0)&0xff; + } + return uv_buf_init(p_data, data_sz+10 ); +} + +uv_buf_t wrap_sensor_scan_pkt( sensor_board_t *board ) +{ + return wrap_tx_pkt( board, 0x0001, NULL, 0 ); +} + diff --git a/app/src/tcp.c b/app/src/tcp.c index bc83278..519b3c1 100644 --- a/app/src/tcp.c +++ b/app/src/tcp.c @@ -1,18 +1,22 @@ #include "alarm_monitor.h" - +#include "am_tcp.h" +/* typedef struct{ const char * name; uv_timer_t timer; - uv_loop_t *loop; + uv_loop_t *loop; char * host; char port[8]; // int state; //0:init, 1:resolved, 2:connected, - + int connected; uv_getaddrinfo_t resolver; uv_tcp_t sock; uv_connect_t conn_req; uv_write_t write_req; + + void * data; }tcp_cli_t; +*/ void tcp_cli_start( tcp_cli_t *cli, int delay_ms ); void on_tcp_write(uv_write_t* req, int status); @@ -28,6 +32,7 @@ void on_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) tcp_cli_t *p_cli = (tcp_cli_t*)client->data; printf( "%s : on_read -- nread:%ld\n", p_cli->name, nread ); if (nread < 0) { + p_cli->connected = 0; //if (nread != UV_EOF) fprintf(stderr, "Read error %s\n", uv_err_name(nread)); uv_close((uv_handle_t*) client, NULL); @@ -36,7 +41,10 @@ void on_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) tcp_cli_start( client->data, 10*1000); //tcp close, reconnect .... return; } - + + printf( "\n\n------------On Read -- nread:%ld, buf_len:%ld\n\n", nread, buf->len ); + if( p_cli->rx_cb ) + p_cli->rx_cb( client, buf->base, nread ); char *data = (char*) malloc(sizeof(char) * (nread+1)); data[nread] = '\0'; strncpy(data, buf->base, nread); @@ -73,6 +81,7 @@ void on_connect(uv_connect_t* req, int status) p_cli->write_req.data = p_cli; uv_read_start((uv_stream_t*)&p_cli->sock, alloc_buffer, on_read ); + p_cli->connected = 1; char* str = "hello server, this is client!"; @@ -124,11 +133,11 @@ void tcp_cli_resolve( uv_timer_t *handle ) if (r) { fprintf(stderr, "getaddrinfo call error %s\n", uv_strerror( r ) ); - tcp_cli_start( handle->data, 5*1000 ); + tcp_cli_start( handle->data, 5*1000 ); } } -void tcp_cli_init( tcp_cli_t * cli, char * host, uint16_t port, char *name ) +void tcp_cli_init( tcp_cli_t * cli, char * host, uint16_t port, char *name, tcp_rx_cb_t cb ) { cli->loop = uv_default_loop(); cli->host = strdup( host ); @@ -140,6 +149,8 @@ void tcp_cli_init( tcp_cli_t * cli, char * host, uint16_t port, char *name ) cli->sock.data = cli; cli->name = strdup( name ); + + cli->rx_cb = cb; } void tcp_cli_start( tcp_cli_t *cli, int delay_ms ) @@ -154,6 +165,15 @@ void tcp_cli_start( tcp_cli_t *cli, int delay_ms ) void tcp_write_data( tcp_cli_t *cli, char *tx_data, int tx_size ) { + uv_buf_t uvBuf = uv_buf_init( tx_data, tx_size ); + uv_write( &cli->write_req, (uv_stream_t*)&cli->sock, &uvBuf, 1, on_tcp_write); + printf( "tcp write data -- %s\n", tx_data ); +} + +void tcp_write_buf( tcp_cli_t *cli, uv_buf_t *uvBuf ) +{ + uv_write( &cli->write_req, (uv_stream_t*)&cli->sock, uvBuf, 1, on_tcp_write); + printf( "tcp write buf -- %p -- %ld\n", uvBuf->base, uvBuf->len ); } void tcp_test( void ) @@ -161,11 +181,11 @@ void tcp_test( void ) static tcp_cli_t tcp_cli_a; static tcp_cli_t tcp_cli_b; - tcp_cli_init( & tcp_cli_a, "kyz.pub", 9999, "CLI_A" ); - tcp_cli_start( &tcp_cli_a, 0 ); +// tcp_cli_init( & tcp_cli_a, "kyz.pub", 9999, "CLI_A" ); +// tcp_cli_start( &tcp_cli_a, 0 ); - tcp_cli_init( & tcp_cli_b, "kyz.pub", 9998, "CLI_B" ); - tcp_cli_start( &tcp_cli_b, 0 ); +// tcp_cli_init( & tcp_cli_b, "kyz.pub", 9998, "CLI_B" ); +// tcp_cli_start( &tcp_cli_b, 0 ); return ; } -- 2.34.1