diff --git a/app/include/cmd.h b/app/include/cmd.h index 7474121..44bb6ca 100755 --- a/app/include/cmd.h +++ b/app/include/cmd.h @@ -230,10 +230,10 @@ typedef struct{ char hostname[FILE_NAME_LEN]; // 设备名 128byte uint32_t factory_date; // 出厂日期. uint32_t deployment_date; // 部署日期. - uint8_t app_version[32]; // 软件版本 - uint8_t app_compile_time[32]; // 软件编译时间 - uint8_t hardware_version[32]; // 硬件版本 - uint8_t FPGA_version[32]; // fpga版本 + uint8_t app_version[DEV_VERSION_STR_LEN]; // 软件版本 + uint8_t app_compile_time[DEV_VERSION_STR_LEN]; // 软件编译时间 + uint8_t hardware_version[DEV_VERSION_STR_LEN]; // 硬件版本 + uint8_t FPGA_version[DEV_VERSION_STR_LEN]; // fpga版本 uint32_t ip; // 本机 IP. uint32_t mask; // 本机 MASK. uint32_t gw; // 本机网关 @@ -249,8 +249,6 @@ typedef struct int lines; /* System wide terminal lines. */ char *version; /* 版本号. */ char *compile; /* 编译时间. */ - char *hardversion; /* 硬件版本号. */ - char *FPGAversion; /* PFGA版本号. */ char configfile[FILE_NAME_LEN]; /* config file name of this host */ char bannerfile[FILE_NAME_LEN]; /* Banner configuration file name. */ } host_t; diff --git a/app/include/common.h b/app/include/common.h index 6ce1c9d..c5570d1 100755 --- a/app/include/common.h +++ b/app/include/common.h @@ -85,6 +85,7 @@ #define DEV_DATA_LEN 1000 #define DEV_DATA_BITMAP_LEN 8 #define DEV_NAME_STR_LEN 32 +#define DEV_VERSION_STR_LEN 32 #define THREAD_NAME_LEN 32 /* Does the I/O error indicate that the operation should be retried later? */ diff --git a/app/include/pd_csg.h b/app/include/pd_csg.h index c49904b..4d281c9 100755 --- a/app/include/pd_csg.h +++ b/app/include/pd_csg.h @@ -333,10 +333,10 @@ typedef struct char hostname[FILE_NAME_LEN]; // 设备名 128byte uint32_t factory_date; // 出厂日期. uint32_t deployment_date; // 部署日期. - uint8_t app_version[32]; // 软件版本 - uint8_t app_compile_time[32]; // 软件编译时间 - uint8_t hardware_version[32]; // 硬件版本 - uint8_t FPGA_version[32]; // fpga版本 + uint8_t app_version[DEV_VERSION_STR_LEN]; // 软件版本 + uint8_t app_compile_time[DEV_VERSION_STR_LEN]; // 软件编译时间 + uint8_t hardware_version[DEV_VERSION_STR_LEN]; // 硬件版本 + uint8_t FPGA_version[DEV_VERSION_STR_LEN]; // fpga版本 uint32_t ip; // 本机 IP. uint32_t mask; // 本机 MASK. uint32_t gw; // 本机网关 diff --git a/app/include/pd_dau.h b/app/include/pd_dau.h index c170c25..81b9060 100755 --- a/app/include/pd_dau.h +++ b/app/include/pd_dau.h @@ -46,16 +46,17 @@ #include "common.h" /* Define --------------------------------------------------------------------*/ -#define BUFFER_SIZE 2048 #define DAU_ETH_SLOTS_SUM 4 -#define RS485_SLOTS 2 +#define RS485_SLOTS 2 +#define DAU_BUF_SIZE 1536 + #define UHF "UDP_UHF" #define HF "UDP_HF" #define ULTRASONIC "UDP_ULTRASONIC" #define IRONCORE "RS485_IRONCORE" -#define UDP_PORT 5000 + @@ -179,13 +180,25 @@ typedef struct dau_info_t info; } dau_manager_t; - -typedef struct { - int slot; - DauType type; - DauState state; + +/* DAU 状态结构体 */ +typedef struct +{ + uint8_t is_insert:1; + uint8_t is_connect:1; +} dau_state_t; + +/* DAU 全局结构体 */ +typedef struct +{ + uint8_t slot; + DauType type; + int fd; + char buf_recv[DAU_BUF_SIZE]; + DauState stat; void *private_data; - dau_info_t info; + dau_info_t info; + dau_state_t state; } dau_t; /* Exported macro ------------------------------------------------------------*/ diff --git a/app/lib/a_process/pd_csg.c b/app/lib/a_process/pd_csg.c index a6f40a2..e5df2c7 100755 --- a/app/lib/a_process/pd_csg.c +++ b/app/lib/a_process/pd_csg.c @@ -216,10 +216,10 @@ int32_t _csg_connect_send(void) strncpy(pinfo->hostname, host.name, sizeof(pinfo->hostname)-1); pinfo->factory_date = device_info.factory_date; pinfo->deployment_date = device_info.deployment_date; - strncpy((char *)pinfo->app_version, host.version, sizeof(pinfo->app_version)-1); - strncpy((char *)pinfo->app_compile_time, host.compile, sizeof(pinfo->app_compile_time)-1); - strncpy((char *)pinfo->hardware_version, host.hardversion, 31); - strncpy((char *)pinfo->FPGA_version, host.FPGAversion, 31); + snprintf((char*)pinfo->app_version, DEV_VERSION_STR_LEN, "%s", device_info.app_version); + snprintf((char*)pinfo->app_compile_time, DEV_VERSION_STR_LEN, "%s", device_info.app_compile_time); + snprintf((char*)pinfo->hardware_version, DEV_VERSION_STR_LEN, "%s", device_info.hardware_version); + snprintf((char*)pinfo->FPGA_version, DEV_VERSION_STR_LEN, "%s", device_info.FPGA_version); pinfo->ip = device_info.ip; pinfo->mask = device_info.mask; pinfo->gw = device_info.gw; @@ -364,7 +364,7 @@ int32_t _csg_dev_info_set_recv(char *pkt) if (strncmp((char *)(pinfo->hostname), device_info.hostname, sizeof(device_info.hostname))) { - snprintf((char*)device_info.hostname, PD_DEV_NUM_LEN, "%s", pinfo->hostname); + snprintf((char*)device_info.hostname, FILE_NAME_LEN, "%s", pinfo->hostname); boottype = REBOOT_REMOTE_HOST_NAME_CHANGE; } @@ -421,10 +421,10 @@ int32_t _csg_dev_info_get_recv(char *pkt) strcpy(pinfo->hostname, device_info.hostname); pinfo->factory_date = device_info.factory_date; pinfo->deployment_date = device_info.deployment_date; - strncpy((char *)pinfo->app_compile_time, host.compile, 31); - strncpy((char *)pinfo->app_version, host.version, 31); - strncpy((char *)pinfo->hardware_version, host.hardversion, 31); - strncpy((char *)pinfo->FPGA_version, host.FPGAversion, 31); + snprintf((char*)pinfo->app_version, DEV_VERSION_STR_LEN, "%s", device_info.app_version); + snprintf((char*)pinfo->app_compile_time, DEV_VERSION_STR_LEN, "%s", device_info.app_compile_time); + snprintf((char*)pinfo->hardware_version, DEV_VERSION_STR_LEN, "%s", device_info.hardware_version); + snprintf((char*)pinfo->FPGA_version, DEV_VERSION_STR_LEN, "%s", device_info.FPGA_version); pinfo->ip = device_info.ip; pinfo->mask = device_info.mask; pinfo->gw = device_info.gw; @@ -486,10 +486,10 @@ int32_t _csg_config_get_recv(char *pkt) /* 通道提交端口参数设置. */ int32_t _csg_port_config_set_recv(char *pkt) { - uint8_t vport = *(uint8_t*)(pkt + CSG_HEAD_LEN); + //uint8_t vport = *(uint8_t*)(pkt + CSG_HEAD_LEN); uint8_t unit = 0; uint8_t port = 0; - csg_pkt_head_t *head = (csg_pkt_head_t*)pkt; + //csg_pkt_head_t *head = (csg_pkt_head_t*)pkt; csg_config_port_t *pnet = (csg_config_port_t *)(pkt + CSG_HEAD_LEN); pd_config.config_port[unit][port].vport = pnet->vport; @@ -535,7 +535,7 @@ int32_t _csg_port_config_get_recv(char *pkt) uint8_t unit = 0; uint8_t port = 0; csg_pkt_head_t *head = (csg_pkt_head_t*)pkt; - csg_config_port_t *pnet = (csg_config_port_t *)(pkt + CSG_HEAD_LEN); + //csg_config_port_t *pnet = (csg_config_port_t *)(pkt + CSG_HEAD_LEN); //if (dau_vport_to_port(pnet->vport, &unit, &port) != E_NONE) { diff --git a/app/lib/a_process/pd_dau.c b/app/lib/a_process/pd_dau.c index 49f38ee..2a186a7 100755 --- a/app/lib/a_process/pd_dau.c +++ b/app/lib/a_process/pd_dau.c @@ -27,16 +27,58 @@ #include "pd_csg.h" /* Define --------------------------------------------------------------------*/ +#define DUA_UDP_PORT 23760 + +/* Private typedef -----------------------------------------------------------*/ +/* 上传平台回调函数类型 */ +typedef void (*UploadCallback)(int slot, const void *data, size_t len); + +/* 报文头 */ +typedef struct +{ + uint16_t len; + uint8_t dev_type_m; + uint8_t dev_type_s; + uint32_t dev_id; + uint8_t cmd_type; + uint8_t cmd; + uint16_t pkt_id; + uint8_t version; + uint8_t reserve[19]; +} dau_pkt_head_t; + +/* 开机联络 */ +typedef struct +{ + uint8_t type_m; // 主设备号 + uint8_t type_s; // 次设备号 + uint8_t reserved1[2]; // 保留 + uint32_t dev_id; // 设备ID + char hostname[FILE_NAME_LEN]; // 设备名 128byte + uint32_t factory_date; // 出厂日期. + uint32_t deployment_date; // 部署日期. + uint8_t app_version[32]; // 软件版本 + uint8_t app_compile_time[32]; // 软件编译时间 + uint8_t hardware_version[32]; // 硬件版本 + uint8_t FPGA_version[32]; // fpga版本 + uint32_t ip; // 本机 IP. + uint32_t mask; // 本机 MASK. + uint32_t gw; // 本机网关 + uint8_t mac[6]; // MAC地址. + uint16_t server_port; // 服务器端口号. + uint32_t server_ipv4; // 服务器 IP. + uint8_t port[8]; + uint8_t port_type[8]; +} dau_contact_t; /* Private variables ---------------------------------------------------------*/ - -//dau_t dau; pthread_mutex_t board_mutex = PTHREAD_MUTEX_INITIALIZER; int udp_socket; dau_t daus[PD_SLOTS_MAX]; + + -// 上传平台回调函数类型 -typedef void (*UploadCallback)(int slot, const void *data, size_t len); + /* Private function prototypes -----------------------------------------------*/ int _dau_insert(int slot, DauType type); @@ -287,7 +329,7 @@ static int32_t _dau_find_proper_function(char *pkt) if (daus[i].slot != head->slot) continue; - if (daus[i].state == DAU_STATE_REGISTERED) + if (daus[i].stat == DAU_STATE_REGISTERED) { flag = 1; } @@ -507,7 +549,7 @@ int _dau_insert(int slot, DauType type) pthread_mutex_lock(&board_mutex); - if (daus[slot].state != DAU_STATE_DISCONNECTED) + if (daus[slot].stat != DAU_STATE_DISCONNECTED) { pthread_mutex_unlock(&board_mutex); return E_ERROR; @@ -523,7 +565,7 @@ int _dau_insert(int slot, DauType type) // 更新板卡信息 daus[slot].type = type; - daus[slot].state = DAU_STATE_CONNECTED; + daus[slot].stat = DAU_STATE_CONNECTED; daus[slot].private_data = priv_data; daus[slot].slot = slot; @@ -538,7 +580,7 @@ void _dau_remove(int slot) { pthread_mutex_lock(&board_mutex); - if (daus[slot].state == DAU_STATE_DISCONNECTED) + if (daus[slot].stat == DAU_STATE_DISCONNECTED) { pthread_mutex_unlock(&board_mutex); return; @@ -549,7 +591,7 @@ void _dau_remove(int slot) // 重置板卡信息 daus[slot].type = DAU_TYPE_NONE; - daus[slot].state = DAU_STATE_DISCONNECTED; + daus[slot].stat = DAU_STATE_DISCONNECTED; daus[slot].private_data = NULL; pthread_mutex_unlock(&board_mutex); @@ -612,45 +654,10 @@ int main() { } #endif -// 初始化UDP服务器 -int _dau_init_udp_server() -{ - int sock = socket(AF_INET, SOCK_DGRAM, 0); - if (sock < 0) - { - perror("UDP socket creation failed"); - return -1; - } - - struct sockaddr_in server_addr = - { - .sin_family = AF_INET, - .sin_port = htons(UDP_PORT), - .sin_addr.s_addr = INADDR_ANY - }; - - if (bind(sock, (struct sockaddr*)&server_addr, sizeof(server_addr))) - { - perror("UDP bind failed"); - close(sock); - return -1; - } - return sock; -} + -void *_dau_manager_handle(void *arg) -{ - prctl(PR_SET_NAME, "CSG_RCVE", 0, 0, 0); - - while(1) - { - sleep(1); - - } - return NULL; -} void *_dau_udp_receive_handle(void *arg) { @@ -674,7 +681,7 @@ void *_dau_udp_receive_handle(void *arg) for (int i = 0; i < DAU_ETH_SLOTS_SUM; i++) { //printf("state=%d\n", daus[i].state); - if (daus[i].state == DAU_STATE_DISCONNECTED) + if (daus[i].stat == DAU_STATE_DISCONNECTED) continue; udp_client_data *client = (udp_client_data *)daus[i].private_data; @@ -684,11 +691,11 @@ void *_dau_udp_receive_handle(void *arg) } // 如果是新连接 - if (daus[i].state == DAU_STATE_CONNECTED && + if (daus[i].stat == DAU_STATE_CONNECTED && client->addr.sin_port == 0) { memcpy(&client->addr, &client_addr, sizeof(client_addr)); - daus[i].state = DAU_STATE_REGISTERED; + daus[i].stat = DAU_STATE_REGISTERED; break; } } @@ -705,11 +712,117 @@ void *_dau_udp_receive_handle(void *arg) return NULL; } - + +/* 初始化UDP服务器 */ +int32_t _dau_udp_server_init(dau_t *dau) +{ + struct sockaddr_in server = {0}; + int fd = -1; + + fd = socket(AF_INET, SOCK_DGRAM, 0); + if (fd < 0) + { + log_err(LOG_DAU, "ERROR at socket creating return %s!", safe_strerror(errno)); + return E_SYS_CALL; + } + + /* 绑定端口 */ + bzero(&server, sizeof(server)); + server.sin_family = AF_INET; + server.sin_addr.s_addr = htonl(INADDR_ANY); + server.sin_port = htons(DUA_UDP_PORT); + if (bind(fd, (struct sockaddr*)&server, sizeof(server)) < 0) + { + log_err(LOG_DAU, "ERROR at socket bind return %s!", safe_strerror(errno)); + close(fd); + return E_SYS_CALL; + } + + dau->fd = fd; + return E_NONE; +} + +/* 校验收到包的包头, 长度, 校验码. */ +int32_t _dau_pkt_check(char *pkt) +{ + csg_pkt_head_t *head = (csg_pkt_head_t*)pkt; + + /* 对主次设备号进行识别, 次设备号可以是广播. */ + if (head->dev_type_m != device_info.type_m) + { + DBG(DBG_M_PD_CSG_ERR, "@1 type_m=%d %d\r\n", head->dev_type_m, device_info.type_m); + return E_ERROR; + } + + if (head->len > DAU_BUF_SIZE) + { + DBG(DBG_M_PD_CSG_ERR, "@2 receive packet len(%d) is out of range\r\n", head->len); + return E_ERROR; + } + + return E_NONE; +} + +/* dau 数据包处理函数 */ +void _dau_recv_data_process(dau_t *dau, uint16_t recv_len) +{ + +} + +/* dau 开机联络处理函数 */ +void _dau_recv_connect_process(dau_t *dau, uint16_t recv_len) +{ + +} + +/* dau 收包线程 */ +void *_dau_recv_handle(void *arg) +{ + dau_t *dau = (dau_t*)arg; + struct sockaddr_in server; + socklen_t server_len; + uint16_t data_len = 0; + + /* 初始化 socket */ + if (_dau_udp_server_init(dau) != E_NONE) + { + return NULL; + } + + while(1) + { + /* 读取数据. */ + data_len = recvfrom(dau->fd, dau->buf_recv, DAU_BUF_SIZE, 0, (struct sockaddr*)&server, &server_len); + if (data_len <= 0) + { + DBG(DBG_M_PD_DAU_ERR, "Recvfrom return ERROR %s!\r\n", safe_strerror(errno)); + continue; + } + + if (dau->state.is_connect) + { + _dau_recv_data_process(dau, data_len); + } + else + { + _dau_recv_connect_process(dau, data_len); + } + } + + return NULL; +} + /* dau 预初始化 */ int32_t dau_handle_init(void) { + uint8_t i = 0; + memset(&daus, 0, sizeof(dau_t) * PD_SLOTS_MAX); + + for (i = 0; i < PD_SLOTS_MAX; i++) + { + daus[i].slot = i; + } cmd_install_element(COMMON_NODE, &dau_add_cmd); cmd_install_element(COMMON_NODE, &no_dau_add_cmd); @@ -723,19 +836,14 @@ int32_t dau_handle_init_after(void) thread_param_t param = {0}; uint8_t i = 0; - if (0 == udp_socket) - { - udp_socket = _dau_init_udp_server(); - } - printf("udp_socket=%d\n", udp_socket); - /* 初始化模块. */ - - + /* 初始收包线程 */ for (i = 0; i < DAU_ETH_SLOTS_SUM; i++) { + param.arg = (void*)(&daus[i]); param.priority = 80; + param.log_module = LOG_DAU; snprintf(param.thread_name, THREAD_NAME_LEN, "DAU_RECV_%d", i); - create_thread(_dau_manager_handle, ¶m); + create_thread(_dau_recv_handle, ¶m); } return E_NONE; diff --git a/app/lib/m_management/cmd.c b/app/lib/m_management/cmd.c index 7386ce9..0319e8a 100755 --- a/app/lib/m_management/cmd.c +++ b/app/lib/m_management/cmd.c @@ -2332,8 +2332,6 @@ void cmd_init(void) strncpy(host.configfile, "startup-config", FILE_NAME_LEN); host.version = softversion_get(); host.compile = softversion_date_get(); - host.hardversion = hardware_version_get(); - host.FPGAversion = fpga_version_date_get(); host.lines = -1; /* 安装命令行模式节点. */ @@ -2845,8 +2843,9 @@ void vtysh_device_init(void) } else { + memset(&device_info, 0, sizeof(device_info_t)); device_info.type_m = 3; - device_info.type_s = 1; + device_info.type_s = 128; inet_aton(DEV_INFO_DEFAULT_IP, &server.sin_addr); device_info.ip = server.sin_addr.s_addr; inet_aton(DEV_INFO_DEFAULT_MASK, &server.sin_addr); @@ -2855,10 +2854,8 @@ void vtysh_device_init(void) device_info.gw = server.sin_addr.s_addr; device_info.factory_date = 1745317827; device_info.deployment_date = 1745317827; - strncpy((char *)device_info.app_version, host.version, 31); - strncpy((char *)device_info.app_compile_time, host.compile, 31); - strncpy((char *)device_info.hardware_version, host.hardversion, 31); - strncpy((char *)device_info.FPGA_version, host.FPGAversion, 31); + snprintf((char *)device_info.app_version, DEV_VERSION_STR_LEN, "%s", host.version); + snprintf((char *)device_info.app_compile_time, DEV_VERSION_STR_LEN, "%s", host.compile); device_info.dev_id = 0x6010FFFF; strncpy((char *)device_info.hostname, "LandPower", FILE_NAME_LEN); diff --git a/build/version.c b/build/version.c index cd2f977..ef798d3 100644 --- a/build/version.c +++ b/build/version.c @@ -10,14 +10,3 @@ char* softversion_date_get() return SOFT_COMPILE_DATE; } -char* hardware_version_get() -{ - return HARDV_ERSION; -} - -char* fpga_version_date_get() -{ - return FPGA_VERSION; -} - -