feat: C接口化改造 — P0~P2 完成 (icp67/cmd/comm 三个模块)
P0 libicp67: 添加 extern "C" 包裹,bool test → uint8_t test
P1 libcmd: CMD_REGISTER 拆分为 CMD_REGISTER/CMD_REGISTER_C,
移除默认参数,添加 extern "C"
P2 libcomm: enum class → C enum,std::string → char[],
添加 COMM_IP_LEN/COMM_DEV_LEN 宏,添加 extern "C"
同步更新 5 处 CMD_REGISTER 调用点及 comm 内部实现
This commit is contained in:
parent
392fadcdb0
commit
2cc8f1d643
|
|
@ -0,0 +1,112 @@
|
|||
# public/protocol 模块 C 接口化改造方案
|
||||
|
||||
## 背景
|
||||
|
||||
开发团队要求 `src/public/` 和 `src/protocol/` 下所有模块提供纯 C 接口(头文件可被 C 编译器解析)。
|
||||
当前除 `lib60870` 外,逐一排查后发现有 5 个模块不符合要求。
|
||||
|
||||
## 检查结果汇总
|
||||
|
||||
| 模块 | 目录 | C接口? | 主要问题 |
|
||||
|------|------|--------|----------|
|
||||
| libcJSON | public | ✅ | — |
|
||||
| libfunc | public | ✅ | — |
|
||||
| libmd5 | public | ✅ | — |
|
||||
| libmy_xxhash | public | ✅ | — |
|
||||
| libtask | public | ✅ | — |
|
||||
| libmy_mosquitto | public | ✅ | — |
|
||||
| libmodbus | protocol | ✅ | — |
|
||||
| libmongoose | protocol | ✅ | — |
|
||||
| libmms_m | protocol | ✅ | 接口已是C(有extern "C") |
|
||||
| libmms_s | protocol | ✅ | 接口已是C(有extern "C") |
|
||||
| **libicp67** | protocol | ❌ | 缺extern "C",含`bool test` |
|
||||
| **libcmd** | public | ❌ | C++默认参数,无extern "C" |
|
||||
| **libcomm** | public | ❌ | enum class + std::string |
|
||||
| **libdatacenter** | public | ❌ | 大面积std::string/vector/map |
|
||||
| **libxml** | public | ❌ | 第三方C++类库 |
|
||||
|
||||
## 实施优先级
|
||||
|
||||
P0 → P1 → P2 → P3 → P4,逐步推进,每一步完成后编译验证。
|
||||
|
||||
---
|
||||
|
||||
## P0: libicp67 — 添加 extern "C" + 替换 bool 类型
|
||||
|
||||
### 改动点
|
||||
|
||||
1. `release/inc/myIcp67.h`: 添加 `extern "C"` 包裹,`bool test;` → `uint8_t test;`
|
||||
2. `src/protocol/libicp67/src/icp67.cpp`: `bool test` → `uint8_t test`(同步修改所有引用)
|
||||
|
||||
### 难度: 低
|
||||
|
||||
---
|
||||
|
||||
## P1: libcmd — 移除 C++ 默认参数 + 添加 extern "C"
|
||||
|
||||
### 改动点
|
||||
|
||||
1. `release/inc/myCmd.h`:
|
||||
- 移除 `= NULL` 默认参数
|
||||
- 新增便捷宏供纯 C 调用方使用
|
||||
- 添加 `extern "C"` 包裹
|
||||
2. `src/public/libcmd/src/my_cmd.cpp`: 无改动(C++ 实现内部仍可用 STL)
|
||||
|
||||
### 难度: 低
|
||||
|
||||
---
|
||||
|
||||
## P2: libcomm — enum class → C enum + std::string → char[]
|
||||
|
||||
### 改动点
|
||||
|
||||
1. `release/inc/myComm.h`:
|
||||
- `enum class CommType` → `typedef enum { COMM_TYPE_... } CommType;`
|
||||
- `enum class CommDebugShow` → `typedef enum { COMM_DEBUG_... } CommDebugShow;`
|
||||
- `enum class CommState` → `typedef enum { COMM_STATE_... } CommState;`
|
||||
- `enum class CommFdType` → `typedef enum { COMM_FD_... } CommFdType;`
|
||||
- `std::string` 成员 → `char[]` 定长数组
|
||||
- 添加 `extern "C"` 包裹
|
||||
2. `src/public/libcomm/src/comm*.cpp`: 同步修改 STL string → char[] 引用
|
||||
|
||||
### 难度: 中
|
||||
|
||||
---
|
||||
|
||||
## P3: libdatacenter — 全量 STL → C 类型
|
||||
|
||||
### 改动点
|
||||
|
||||
1. `release/inc/myDatacenter.h`:
|
||||
- `std::string` 参数 → `const char*`
|
||||
- `std::string` 返回值 → `const char*` 或 `int` + `char*` 输出参数
|
||||
- `std::string &desc` → `char *desc, int desc_len`
|
||||
- `std::vector<void*>*` → `void **, int *count`
|
||||
- `std::vector<std::string>` → `char **, int *count` 或定长数组
|
||||
- 结构体 `std::string` 成员 → `char xxx[N]` 定长数组
|
||||
- 回调签名中 `std::string` → `const char*`
|
||||
- 移除 `<vector> <string> <map>` include
|
||||
- 添加 `extern "C"` 包裹
|
||||
2. `src/public/libdatacenter/src/*.cpp`: 同步修改所有调用处
|
||||
|
||||
### 难度: 高
|
||||
|
||||
---
|
||||
|
||||
## P4: libxml (tinyxml2) — 薄 C 封装层
|
||||
|
||||
### 方案: 新建 C 封装层
|
||||
|
||||
新建文件:
|
||||
- `src/public/libxml/src/xml_c_wrapper.h` — C 接口声明
|
||||
- `src/public/libxml/src/xml_c_wrapper.cpp` — C 封装实现(内部调用 tinyxml2)
|
||||
|
||||
对外使用不透明指针:
|
||||
```c
|
||||
typedef struct xml_doc xml_doc;
|
||||
xml_doc* xml_open(const char *filepath);
|
||||
void xml_close(xml_doc *doc);
|
||||
const char* xml_get_child_text(xml_doc *doc, const char *path);
|
||||
```
|
||||
|
||||
### 难度: 中
|
||||
|
|
@ -2,6 +2,10 @@
|
|||
|
||||
#include "myBase.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const char *name;
|
||||
|
|
@ -11,12 +15,17 @@ typedef struct
|
|||
}stru_cmd;
|
||||
|
||||
|
||||
#define CMD_REGISTER(cmd_name, func_ptr, cmd_desc, ...) \
|
||||
#define CMD_REGISTER(cmd_name, func_ptr, cmd_desc) \
|
||||
__attribute__((constructor)) static void register_##func_ptr(void) { \
|
||||
cmd_manager_add_command(cmd_name, func_ptr, cmd_desc, ##__VA_ARGS__); \
|
||||
cmd_manager_add_command(cmd_name, func_ptr, cmd_desc, NULL); \
|
||||
}
|
||||
|
||||
void cmd_manager_add_command(const char *name, void (*func)(int argc, char *argv[]), const char *desc, void (*complete)(const char *, char ***, int *) = NULL);
|
||||
#define CMD_REGISTER_C(cmd_name, func_ptr, cmd_desc, complete) \
|
||||
__attribute__((constructor)) static void register_##func_ptr(void) { \
|
||||
cmd_manager_add_command(cmd_name, func_ptr, cmd_desc, complete); \
|
||||
}
|
||||
|
||||
void cmd_manager_add_command(const char *name, void (*func)(int argc, char *argv[]), const char *desc, void (*complete)(const char *, char ***, int *));
|
||||
|
||||
|
||||
stru_cmd *cmd_manager_get_commands(unsigned int *out_count);
|
||||
|
|
@ -32,3 +41,7 @@ void linenoiseFree(void *ptr);
|
|||
int linenoiseHistoryAdd(const char *line);
|
||||
void linenoiseHistoryFree(void);
|
||||
void lineniseSetCompletionCallback(void (*cb)(const char *, char ***, int *));
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -3,61 +3,65 @@
|
|||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define COMM_IP_LEN 16
|
||||
#define COMM_DEV_LEN 256
|
||||
|
||||
enum class CommType
|
||||
typedef enum
|
||||
{
|
||||
unknown,
|
||||
tcp_server,
|
||||
tcp_client,
|
||||
udp_server,
|
||||
udp_client,
|
||||
uart,
|
||||
};
|
||||
COMM_TYPE_UNKNOWN,
|
||||
COMM_TYPE_TCP_SERVER,
|
||||
COMM_TYPE_TCP_CLIENT,
|
||||
COMM_TYPE_UDP_SERVER,
|
||||
COMM_TYPE_UDP_CLIENT,
|
||||
COMM_TYPE_UART,
|
||||
}CommType;
|
||||
|
||||
enum class CommDebugShow
|
||||
typedef enum
|
||||
{
|
||||
off,
|
||||
on,
|
||||
};
|
||||
COMM_DEBUG_OFF,
|
||||
COMM_DEBUG_ON,
|
||||
}CommDebugShow;
|
||||
|
||||
enum class CommState
|
||||
typedef enum
|
||||
{
|
||||
disconnected,
|
||||
connected,
|
||||
};
|
||||
COMM_STATE_DISCONNECTED,
|
||||
COMM_STATE_CONNECTED,
|
||||
}CommState;
|
||||
|
||||
enum class CommFdType
|
||||
typedef enum
|
||||
{
|
||||
unknown = 0,
|
||||
file,
|
||||
socket,
|
||||
serial,
|
||||
pipe
|
||||
};
|
||||
COMM_FD_UNKNOWN = 0,
|
||||
COMM_FD_FILE,
|
||||
COMM_FD_SOCKET,
|
||||
COMM_FD_SERIAL,
|
||||
COMM_FD_PIPE,
|
||||
}CommFdType;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
std::string local_ip;
|
||||
char local_ip[COMM_IP_LEN];
|
||||
uint16_t local_port;
|
||||
std::string remote_ip;
|
||||
char remote_ip[COMM_IP_LEN];
|
||||
uint16_t remote_port;
|
||||
}stru_tcp_para;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
std::string local_ip;
|
||||
char local_ip[COMM_IP_LEN];
|
||||
uint16_t local_port;
|
||||
std::string remote_ip;
|
||||
char remote_ip[COMM_IP_LEN];
|
||||
uint16_t remote_port;
|
||||
}stru_udp_para;
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
std::string device;
|
||||
char device[COMM_DEV_LEN];
|
||||
uint32_t baudrate;
|
||||
uint8_t data_bits;
|
||||
uint8_t stop_bits;
|
||||
|
|
@ -111,4 +115,8 @@ int comm_state_register(int id, comm_state_cb cb);
|
|||
int comm_destroy(int id);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
@ -2,6 +2,10 @@
|
|||
|
||||
#include "myBase.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ICP67_DEV_ADDR 1
|
||||
#define ICP67_TX_BUF_SIZE 2048 // tx/resend_tx 缓冲区大小
|
||||
#define ICP67_RESEND_MAX 5 // 重发最大次数
|
||||
|
|
@ -279,7 +283,7 @@ typedef struct _icp67
|
|||
uint32_t tm_out;
|
||||
uint32_t tm_cnt;
|
||||
|
||||
bool test;
|
||||
uint8_t test;
|
||||
}icp67;
|
||||
|
||||
|
||||
|
|
@ -287,3 +291,6 @@ void icp67_init(stru_icp67 *p_icp67, icp67_send_cb send_cb, void *arg);
|
|||
int icp67_set_ao_cfg_md5(stru_icp67 *p_icp67, uint8_t *p_md5);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "myComm.h"
|
||||
#include "comm.h"
|
||||
|
|
@ -11,11 +12,11 @@
|
|||
std::map<int, stru_comm> g_comm_map;
|
||||
|
||||
std::map<CommType, std::string> g_comm_type_str_map = {
|
||||
{CommType::tcp_client, "tcp_client"},
|
||||
{CommType::tcp_server, "tcp_server"},
|
||||
{CommType::udp_client, "udp_client"},
|
||||
{CommType::udp_server, "udp_server"},
|
||||
{CommType::uart, "uart"}
|
||||
{COMM_TYPE_TCP_CLIENT, "tcp_client"},
|
||||
{COMM_TYPE_TCP_SERVER, "tcp_server"},
|
||||
{COMM_TYPE_UDP_CLIENT, "udp_client"},
|
||||
{COMM_TYPE_UDP_SERVER, "udp_server"},
|
||||
{COMM_TYPE_UART, "uart"}
|
||||
};
|
||||
|
||||
static int g_comm_id = 0;
|
||||
|
|
@ -81,8 +82,8 @@ int comm_create(CommType type, CommDebugShow debug_show, void *para)
|
|||
|
||||
switch(type)
|
||||
{
|
||||
case CommType::tcp_client:
|
||||
case CommType::tcp_server:
|
||||
case COMM_TYPE_TCP_CLIENT:
|
||||
case COMM_TYPE_TCP_SERVER:
|
||||
{
|
||||
comm.p_comm = (stru_comm_tcp *)malloc(sizeof(stru_comm_tcp));
|
||||
stru_comm_tcp *p_tcp = (stru_comm_tcp *)comm.p_comm;
|
||||
|
|
@ -91,8 +92,8 @@ int comm_create(CommType type, CommDebugShow debug_show, void *para)
|
|||
ret = comm_tcp_create(p_tcp);
|
||||
break;
|
||||
}
|
||||
case CommType::udp_client:
|
||||
case CommType::udp_server:
|
||||
case COMM_TYPE_UDP_CLIENT:
|
||||
case COMM_TYPE_UDP_SERVER:
|
||||
{
|
||||
comm.p_comm = (stru_comm_udp *)malloc(sizeof(stru_comm_udp));
|
||||
stru_comm_udp *p_udp = (stru_comm_udp *)comm.p_comm;
|
||||
|
|
@ -101,7 +102,7 @@ int comm_create(CommType type, CommDebugShow debug_show, void *para)
|
|||
ret = comm_udp_create(p_udp);
|
||||
break;
|
||||
}
|
||||
case CommType::uart:
|
||||
case COMM_TYPE_UART:
|
||||
{
|
||||
comm.p_comm = (stru_comm_uart *)malloc(sizeof(stru_comm_uart));
|
||||
stru_comm_uart *p_uart = (stru_comm_uart *)comm.p_comm;
|
||||
|
|
@ -113,7 +114,7 @@ int comm_create(CommType type, CommDebugShow debug_show, void *para)
|
|||
default:
|
||||
{
|
||||
LOG_E("unknown comm type:%d", type);
|
||||
type = CommType::unknown;
|
||||
type = COMM_TYPE_UNKNOWN;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -129,21 +130,21 @@ int comm_create(CommType type, CommDebugShow debug_show, void *para)
|
|||
|
||||
switch (type)
|
||||
{
|
||||
case CommType::tcp_client:
|
||||
case CommType::tcp_server:
|
||||
case COMM_TYPE_TCP_CLIENT:
|
||||
case COMM_TYPE_TCP_SERVER:
|
||||
{
|
||||
stru_comm_tcp *p_tcp = (stru_comm_tcp *)comm.p_comm;
|
||||
ret = p_tcp->tcp_debug_show_register((void *)p_tcp, comm_debug_show);
|
||||
break;
|
||||
}
|
||||
case CommType::udp_client:
|
||||
case CommType::udp_server:
|
||||
case COMM_TYPE_UDP_CLIENT:
|
||||
case COMM_TYPE_UDP_SERVER:
|
||||
{
|
||||
stru_comm_udp *p_udp = (stru_comm_udp *)comm.p_comm;
|
||||
ret = p_udp->udp_debug_show_register((void *)p_udp, comm_debug_show);
|
||||
break;
|
||||
}
|
||||
case CommType::uart:
|
||||
case COMM_TYPE_UART:
|
||||
{
|
||||
stru_comm_uart *p_uart = (stru_comm_uart *)comm.p_comm;
|
||||
ret = p_uart->uart_debug_show_register((void *)p_uart, comm_debug_show);
|
||||
|
|
@ -186,27 +187,27 @@ int comm_connect(int id)
|
|||
|
||||
switch (comm.type)
|
||||
{
|
||||
case CommType::tcp_client:
|
||||
case COMM_TYPE_TCP_CLIENT:
|
||||
{
|
||||
stru_comm_tcp *p_tcp = (stru_comm_tcp *)comm.p_comm;
|
||||
return p_tcp->tcp_client_connect(id, (void *)p_tcp);
|
||||
}
|
||||
case CommType::tcp_server:
|
||||
case COMM_TYPE_TCP_SERVER:
|
||||
{
|
||||
stru_comm_tcp *p_tcp = (stru_comm_tcp *)comm.p_comm;
|
||||
return p_tcp->tcp_server_connect(id, (void *)p_tcp);
|
||||
}
|
||||
case CommType::udp_client:
|
||||
case COMM_TYPE_UDP_CLIENT:
|
||||
{
|
||||
stru_comm_udp *p_udp = (stru_comm_udp *)comm.p_comm;
|
||||
return p_udp->udp_client_connect(id, (void *)p_udp);
|
||||
}
|
||||
case CommType::udp_server:
|
||||
case COMM_TYPE_UDP_SERVER:
|
||||
{
|
||||
stru_comm_udp *p_udp = (stru_comm_udp *)comm.p_comm;
|
||||
return p_udp->udp_server_connect(id, (void *)p_udp);
|
||||
}
|
||||
case CommType::uart:
|
||||
case COMM_TYPE_UART:
|
||||
{
|
||||
stru_comm_uart *p_uart = (stru_comm_uart *)comm.p_comm;
|
||||
return p_uart->uart_connect(id, (void *)p_uart);
|
||||
|
|
@ -238,19 +239,19 @@ int comm_disconnect(int id)
|
|||
|
||||
switch (comm.type)
|
||||
{
|
||||
case CommType::tcp_client:
|
||||
case CommType::tcp_server:
|
||||
case COMM_TYPE_TCP_CLIENT:
|
||||
case COMM_TYPE_TCP_SERVER:
|
||||
{
|
||||
stru_comm_tcp *p_tcp = (stru_comm_tcp *)comm.p_comm;
|
||||
return p_tcp->tcp_close(id, (void *)p_tcp);
|
||||
}
|
||||
case CommType::udp_client:
|
||||
case CommType::udp_server:
|
||||
case COMM_TYPE_UDP_CLIENT:
|
||||
case COMM_TYPE_UDP_SERVER:
|
||||
{
|
||||
stru_comm_udp *p_udp = (stru_comm_udp *)comm.p_comm;
|
||||
return p_udp->udp_close(id, (void *)p_udp);
|
||||
}
|
||||
case CommType::uart:
|
||||
case COMM_TYPE_UART:
|
||||
{
|
||||
stru_comm_uart *p_uart = (stru_comm_uart *)comm.p_comm;
|
||||
return p_uart->uart_close((void *)p_uart);
|
||||
|
|
@ -282,19 +283,19 @@ int comm_recv_register(int id, comm_recv_cb cb)
|
|||
|
||||
switch (comm.type)
|
||||
{
|
||||
case CommType::tcp_client:
|
||||
case CommType::tcp_server:
|
||||
case COMM_TYPE_TCP_CLIENT:
|
||||
case COMM_TYPE_TCP_SERVER:
|
||||
{
|
||||
stru_comm_tcp *p_tcp = (stru_comm_tcp *)comm.p_comm;
|
||||
return p_tcp->tcp_recv_register((void *)p_tcp, cb);
|
||||
}
|
||||
case CommType::udp_client:
|
||||
case CommType::udp_server:
|
||||
case COMM_TYPE_UDP_CLIENT:
|
||||
case COMM_TYPE_UDP_SERVER:
|
||||
{
|
||||
stru_comm_udp *p_udp = (stru_comm_udp *)comm.p_comm;
|
||||
return p_udp->udp_recv_register((void *)p_udp, cb);
|
||||
}
|
||||
case CommType::uart:
|
||||
case COMM_TYPE_UART:
|
||||
{
|
||||
stru_comm_uart *p_uart = (stru_comm_uart *)comm.p_comm;
|
||||
return p_uart->uart_recv_register((void *)p_uart, cb);
|
||||
|
|
@ -326,27 +327,27 @@ int comm_send(int id, int fd, const uint8_t *data, uint16_t len)
|
|||
|
||||
switch (comm.type)
|
||||
{
|
||||
case CommType::tcp_client:
|
||||
case COMM_TYPE_TCP_CLIENT:
|
||||
{
|
||||
stru_comm_tcp *p_tcp = (stru_comm_tcp *)comm.p_comm;
|
||||
return p_tcp->tcp_send(id, fd, "client", (void *)p_tcp, data, len);
|
||||
}
|
||||
case CommType::tcp_server:
|
||||
case COMM_TYPE_TCP_SERVER:
|
||||
{
|
||||
stru_comm_tcp *p_tcp = (stru_comm_tcp *)comm.p_comm;
|
||||
return p_tcp->tcp_send(id, fd, "server", (void *)p_tcp, data, len);
|
||||
}
|
||||
case CommType::udp_client:
|
||||
case COMM_TYPE_UDP_CLIENT:
|
||||
{
|
||||
stru_comm_udp *p_udp = (stru_comm_udp *)comm.p_comm;
|
||||
return p_udp->udp_send(id, fd, "client", (void *)p_udp, data, len);
|
||||
}
|
||||
case CommType::udp_server:
|
||||
case COMM_TYPE_UDP_SERVER:
|
||||
{
|
||||
stru_comm_udp *p_udp = (stru_comm_udp *)comm.p_comm;
|
||||
return p_udp->udp_send(id, fd, "server", (void *)p_udp, data, len);
|
||||
}
|
||||
case CommType::uart:
|
||||
case COMM_TYPE_UART:
|
||||
{
|
||||
stru_comm_uart *p_uart = (stru_comm_uart *)comm.p_comm;
|
||||
return p_uart->uart_send((void *)p_uart, data, len);
|
||||
|
|
@ -378,19 +379,19 @@ int comm_state_register(int id, comm_state_cb cb)
|
|||
|
||||
switch (comm.type)
|
||||
{
|
||||
case CommType::tcp_client:
|
||||
case CommType::tcp_server:
|
||||
case COMM_TYPE_TCP_CLIENT:
|
||||
case COMM_TYPE_TCP_SERVER:
|
||||
{
|
||||
stru_comm_tcp *p_tcp = (stru_comm_tcp *)comm.p_comm;
|
||||
return p_tcp->tcp_state_register((void *)p_tcp, cb);
|
||||
}
|
||||
case CommType::udp_client:
|
||||
case CommType::udp_server:
|
||||
case COMM_TYPE_UDP_CLIENT:
|
||||
case COMM_TYPE_UDP_SERVER:
|
||||
{
|
||||
stru_comm_udp *p_udp = (stru_comm_udp *)comm.p_comm;
|
||||
return p_udp->udp_state_register((void *)p_udp, cb);
|
||||
}
|
||||
case CommType::uart:
|
||||
case COMM_TYPE_UART:
|
||||
{
|
||||
stru_comm_uart *p_uart = (stru_comm_uart *)comm.p_comm;
|
||||
return p_uart->uart_state_register((void *)p_uart, cb);
|
||||
|
|
@ -446,13 +447,13 @@ CommFdType get_fd_type(int fd)
|
|||
|
||||
if (fstat(fd, &st) < 0)
|
||||
{
|
||||
return CommFdType::unknown;
|
||||
return COMM_FD_UNKNOWN;
|
||||
}
|
||||
|
||||
// 判断文件类型
|
||||
if (S_ISSOCK(st.st_mode))
|
||||
{
|
||||
return CommFdType::socket;
|
||||
return COMM_FD_SOCKET;
|
||||
} else if (S_ISCHR(st.st_mode))
|
||||
{
|
||||
// 可能是串口或终端
|
||||
|
|
@ -461,16 +462,16 @@ CommFdType get_fd_type(int fd)
|
|||
struct serial_struct serinfo;
|
||||
if (ioctl(fd, TIOCGSERIAL, &serinfo) == 0)
|
||||
{
|
||||
return CommFdType::serial;
|
||||
return COMM_FD_SERIAL;
|
||||
}
|
||||
#endif
|
||||
return CommFdType::file;
|
||||
return COMM_FD_FILE;
|
||||
} else if (S_ISFIFO(st.st_mode))
|
||||
{
|
||||
return CommFdType::pipe;
|
||||
return COMM_FD_PIPE;
|
||||
} else
|
||||
{
|
||||
return CommFdType::file;
|
||||
return COMM_FD_FILE;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -485,7 +486,7 @@ int get_fd_info(int fd, stru_comm_fd_info *info)
|
|||
|
||||
switch (info->type)
|
||||
{
|
||||
case CommFdType::socket:
|
||||
case COMM_FD_SOCKET:
|
||||
{
|
||||
// 获取socket信息
|
||||
struct sockaddr_in addr;
|
||||
|
|
@ -514,7 +515,7 @@ int get_fd_info(int fd, stru_comm_fd_info *info)
|
|||
break;
|
||||
}
|
||||
|
||||
case CommFdType::serial:
|
||||
case COMM_FD_SERIAL:
|
||||
{
|
||||
// 获取串口信息
|
||||
#ifdef __linux__
|
||||
|
|
@ -560,7 +561,7 @@ int get_fd_info(int fd, stru_comm_fd_info *info)
|
|||
break;
|
||||
}
|
||||
|
||||
case CommFdType::file:
|
||||
case COMM_FD_FILE:
|
||||
{
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) == 0)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <string>
|
||||
|
||||
|
||||
// #include <netdb.h>
|
||||
|
|
@ -132,7 +133,7 @@ static int tcp_connect(stru_comm_tcp *p_tcp)
|
|||
memset(&serv_addr, 0, sizeof(serv_addr));
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_port = htons(p_tcp->p_para->remote_port);
|
||||
serv_addr.sin_addr.s_addr = inet_addr(p_tcp->p_para->remote_ip.c_str());
|
||||
serv_addr.sin_addr.s_addr = inet_addr(p_tcp->p_para->remote_ip);
|
||||
if(serv_addr.sin_addr.s_addr == INADDR_NONE)
|
||||
{
|
||||
LOG_E("inet_addr failed");
|
||||
|
|
@ -146,7 +147,7 @@ static int tcp_connect(stru_comm_tcp *p_tcp)
|
|||
int ret = connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
|
||||
if(ret < 0 && errno != EINPROGRESS)
|
||||
{
|
||||
LOG_E("connect %s:%d failed, errno:%d", p_tcp->p_para->remote_ip.c_str(), p_tcp->p_para->remote_port, errno);
|
||||
LOG_E("connect %s:%d failed, errno:%d", p_tcp->p_para->remote_ip, p_tcp->p_para->remote_port, errno);
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -162,7 +163,7 @@ static int tcp_connect(stru_comm_tcp *p_tcp)
|
|||
ret = select(sockfd + 1, &rset, &wset, NULL, &tmval);
|
||||
if(ret < 0)
|
||||
{
|
||||
LOG_E("connect time out %s,%d!", p_tcp->p_para->remote_ip.c_str(), p_tcp->p_para->remote_port);
|
||||
LOG_E("connect time out %s,%d!", p_tcp->p_para->remote_ip, p_tcp->p_para->remote_port);
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -172,7 +173,7 @@ static int tcp_connect(stru_comm_tcp *p_tcp)
|
|||
getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &err, &len);
|
||||
if(err != 0)
|
||||
{
|
||||
LOG_E("connect %s:%d failed, err:%d", p_tcp->p_para->remote_ip.c_str(), p_tcp->p_para->remote_port, err);
|
||||
LOG_E("connect %s:%d failed, err:%d", p_tcp->p_para->remote_ip, p_tcp->p_para->remote_port, err);
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -181,12 +182,12 @@ static int tcp_connect(stru_comm_tcp *p_tcp)
|
|||
|
||||
if(tcp_open(sockfd) < 0)
|
||||
{
|
||||
LOG_E("tcp_open %s:%d failed", p_tcp->p_para->remote_ip.c_str(), p_tcp->p_para->remote_port);
|
||||
LOG_E("tcp_open %s:%d failed", p_tcp->p_para->remote_ip, p_tcp->p_para->remote_port);
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
LOG_I("connect %s:%d success", p_tcp->p_para->remote_ip.c_str(), p_tcp->p_para->remote_port);
|
||||
LOG_I("connect %s:%d success", p_tcp->p_para->remote_ip, p_tcp->p_para->remote_port);
|
||||
|
||||
return sockfd;
|
||||
}
|
||||
|
|
@ -206,7 +207,7 @@ static int tcp_client_connect(int id, void *p_this)
|
|||
{
|
||||
if(p_tcp->sockfd < 0)
|
||||
{
|
||||
if(p_tcp->p_para->remote_ip.empty())
|
||||
if(p_tcp->p_para->remote_ip[0] == '\0')
|
||||
{
|
||||
LOG_E("remote_ip is empty");
|
||||
tcp_sleep(5000);
|
||||
|
|
@ -227,7 +228,7 @@ static int tcp_client_connect(int id, void *p_this)
|
|||
|
||||
if(p_tcp->state_cb)
|
||||
{
|
||||
p_tcp->state_cb(id, p_tcp->sockfd, CommState::disconnected);
|
||||
p_tcp->state_cb(id, p_tcp->sockfd, COMM_STATE_DISCONNECTED);
|
||||
}
|
||||
p_tcp->sockfd = -1;
|
||||
}
|
||||
|
|
@ -236,10 +237,10 @@ static int tcp_client_connect(int id, void *p_this)
|
|||
|
||||
if(p_tcp->state_cb)
|
||||
{
|
||||
p_tcp->state_cb(id, sockfd, CommState::connected);
|
||||
p_tcp->state_cb(id, sockfd, COMM_STATE_CONNECTED);
|
||||
}
|
||||
|
||||
LOG_I("tcp_client_connect %s:%d success, sockfd:%d", p_tcp->p_para->remote_ip.c_str(), p_tcp->p_para->remote_port, sockfd);
|
||||
LOG_I("tcp_client_connect %s:%d success, sockfd:%d", p_tcp->p_para->remote_ip, p_tcp->p_para->remote_port, sockfd);
|
||||
}
|
||||
|
||||
fd_set recvfdset;
|
||||
|
|
@ -263,7 +264,7 @@ static int tcp_client_connect(int id, void *p_this)
|
|||
LOG_E("recv_cb is null");
|
||||
}
|
||||
|
||||
if(p_tcp->debug_show_cb && p_tcp->debug_show == CommDebugShow::on)
|
||||
if(p_tcp->debug_show_cb && p_tcp->debug_show == COMM_DEBUG_ON)
|
||||
{
|
||||
std::string str = "tcp_client_id: " + std::to_string(id) + ", " + "fd: " + std::to_string(p_tcp->sockfd) + ",";
|
||||
p_tcp->debug_show_cb(str.c_str(), CommDir::dir_recv, buffer, len);
|
||||
|
|
@ -277,7 +278,7 @@ static int tcp_client_connect(int id, void *p_this)
|
|||
|
||||
if(p_tcp->state_cb)
|
||||
{
|
||||
p_tcp->state_cb(id, p_tcp->sockfd, CommState::disconnected);
|
||||
p_tcp->state_cb(id, p_tcp->sockfd, COMM_STATE_DISCONNECTED);
|
||||
}
|
||||
|
||||
p_tcp->sockfd = -1;
|
||||
|
|
@ -314,16 +315,16 @@ static int tcp_listen(stru_comm_tcp *p_tcp)
|
|||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_port = htons(p_tcp->p_para->local_port);
|
||||
|
||||
if(p_tcp->p_para->local_ip.empty() || p_tcp->p_para->local_ip.compare("0.0.0.0") == 0)
|
||||
if(p_tcp->p_para->local_ip[0] == '\0' || strcmp(p_tcp->p_para->local_ip, "0.0.0.0") == 0)
|
||||
{
|
||||
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
}
|
||||
else
|
||||
{
|
||||
serv_addr.sin_addr.s_addr = inet_addr(p_tcp->p_para->local_ip.c_str());
|
||||
serv_addr.sin_addr.s_addr = inet_addr(p_tcp->p_para->local_ip);
|
||||
if(serv_addr.sin_addr.s_addr == INADDR_NONE)
|
||||
{
|
||||
LOG_E("inet_addr %s failed", p_tcp->p_para->local_ip.c_str());
|
||||
LOG_E("inet_addr %s failed", p_tcp->p_para->local_ip);
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -331,21 +332,21 @@ static int tcp_listen(stru_comm_tcp *p_tcp)
|
|||
|
||||
if(bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
|
||||
{
|
||||
LOG_E("bind %s:%d failed, errno:%d", p_tcp->p_para->local_ip.c_str(), p_tcp->p_para->local_port, errno);
|
||||
LOG_E("bind %s:%d failed, errno:%d", p_tcp->p_para->local_ip, p_tcp->p_para->local_port, errno);
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(tcp_open(sockfd) < 0)
|
||||
{
|
||||
LOG_E("tcp_open %s:%d failed", p_tcp->p_para->local_ip.c_str(), p_tcp->p_para->local_port);
|
||||
LOG_E("tcp_open %s:%d failed", p_tcp->p_para->local_ip, p_tcp->p_para->local_port);
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(listen(sockfd, TCP_MAX_CONN_NUM) < 0)
|
||||
{
|
||||
LOG_E("listen %s:%d failed, errno:%d", p_tcp->p_para->local_ip.c_str(), p_tcp->p_para->local_port, errno);
|
||||
LOG_E("listen %s:%d failed, errno:%d", p_tcp->p_para->local_ip, p_tcp->p_para->local_port, errno);
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -369,7 +370,7 @@ static int tcp_server_connect(int id, void *p_this)
|
|||
int listen_fd = tcp_listen(p_tcp);
|
||||
if(listen_fd < 0)
|
||||
{
|
||||
LOG_E("tcp_listen %s:%d failed", p_tcp->p_para->local_ip.c_str(), p_tcp->p_para->local_port);
|
||||
LOG_E("tcp_listen %s:%d failed", p_tcp->p_para->local_ip, p_tcp->p_para->local_port);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -449,7 +450,7 @@ static int tcp_server_connect(int id, void *p_this)
|
|||
|
||||
if(p_tcp->state_cb)
|
||||
{
|
||||
p_tcp->state_cb(id, new_sd, CommState::connected);
|
||||
p_tcp->state_cb(id, new_sd, COMM_STATE_CONNECTED);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -471,7 +472,7 @@ static int tcp_server_connect(int id, void *p_this)
|
|||
LOG_E("recv_cb is null");
|
||||
}
|
||||
|
||||
if(p_tcp->debug_show_cb && p_tcp->debug_show == CommDebugShow::on)
|
||||
if(p_tcp->debug_show_cb && p_tcp->debug_show == COMM_DEBUG_ON)
|
||||
{
|
||||
std::string str = "tcp_server_id: " + std::to_string(id) + ", " + "fd: " + std::to_string(sd) + ",";
|
||||
p_tcp->debug_show_cb(str.c_str(), CommDir::dir_recv, buffer, len);
|
||||
|
|
@ -492,7 +493,7 @@ static int tcp_server_connect(int id, void *p_this)
|
|||
|
||||
if(p_tcp->state_cb)
|
||||
{
|
||||
p_tcp->state_cb(id, sd, CommState::disconnected);
|
||||
p_tcp->state_cb(id, sd, COMM_STATE_DISCONNECTED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -525,7 +526,7 @@ static int tcp_close(int id, void *p_this)
|
|||
|
||||
if(p_tcp->state_cb)
|
||||
{
|
||||
p_tcp->state_cb(id, p_tcp->sockfd, CommState::disconnected);
|
||||
p_tcp->state_cb(id, p_tcp->sockfd, COMM_STATE_DISCONNECTED);
|
||||
}
|
||||
p_tcp->sockfd = -1;
|
||||
|
||||
|
|
@ -538,7 +539,7 @@ static int tcp_close(int id, void *p_this)
|
|||
close(p_tcp->client_fd[i]);
|
||||
if(p_tcp->state_cb)
|
||||
{
|
||||
p_tcp->state_cb(id, p_tcp->client_fd[i], CommState::disconnected);
|
||||
p_tcp->state_cb(id, p_tcp->client_fd[i], COMM_STATE_DISCONNECTED);
|
||||
}
|
||||
p_tcp->client_fd[i] = -1;
|
||||
}
|
||||
|
|
@ -582,7 +583,7 @@ static int tcp_send(int id, int fd, const char *tcp_type, void *p_this, const ui
|
|||
|
||||
if(p_tcp->state_cb)
|
||||
{
|
||||
p_tcp->state_cb(id, fd, CommState::disconnected);
|
||||
p_tcp->state_cb(id, fd, COMM_STATE_DISCONNECTED);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
@ -595,14 +596,14 @@ static int tcp_send(int id, int fd, const char *tcp_type, void *p_this, const ui
|
|||
|
||||
if(p_tcp->state_cb)
|
||||
{
|
||||
p_tcp->state_cb(id, fd, CommState::disconnected);
|
||||
p_tcp->state_cb(id, fd, COMM_STATE_DISCONNECTED);
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(p_tcp->debug_show_cb && p_tcp->debug_show == CommDebugShow::on)
|
||||
if(p_tcp->debug_show_cb && p_tcp->debug_show == COMM_DEBUG_ON)
|
||||
{
|
||||
std::string str = "tcp" + std::string(tcp_type) + "id: " + std::to_string(id) + ", " + "fd: " + std::to_string(fd) + ",";
|
||||
p_tcp->debug_show_cb(str.c_str(), CommDir::dir_send, data, send_len);
|
||||
|
|
|
|||
|
|
@ -75,10 +75,10 @@ static int uart_create(stru_comm_uart *p_uart)
|
|||
return -1;
|
||||
}
|
||||
|
||||
p_uart->uart_fd = open(p_uart->p_para->device.c_str(), O_RDWR | O_NOCTTY);
|
||||
p_uart->uart_fd = open(p_uart->p_para->device, O_RDWR | O_NOCTTY);
|
||||
if(0 > p_uart->uart_fd)
|
||||
{
|
||||
LOG_E("open %s failed", p_uart->p_para->device.c_str());
|
||||
LOG_E("open %s failed", p_uart->p_para->device);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -216,7 +216,7 @@ static int uart_create(stru_comm_uart *p_uart)
|
|||
}
|
||||
|
||||
LOG_I("uart init success: %s, %d, %d, %d, %c",
|
||||
p_uart->p_para->device.c_str(), p_uart->p_para->baudrate,
|
||||
p_uart->p_para->device, p_uart->p_para->baudrate,
|
||||
p_uart->p_para->data_bits, p_uart->p_para->stop_bits, p_uart->p_para->parity);
|
||||
|
||||
return 0;
|
||||
|
|
@ -241,7 +241,7 @@ static int uart_connect(int id, void *p_this)
|
|||
|
||||
if(p_uart->state_cb)
|
||||
{
|
||||
p_uart->state_cb(id, p_uart->uart_fd, CommState::connected);
|
||||
p_uart->state_cb(id, p_uart->uart_fd, COMM_STATE_CONNECTED);
|
||||
}
|
||||
|
||||
uint8_t rx_buffer[1024];
|
||||
|
|
@ -256,9 +256,9 @@ static int uart_connect(int id, void *p_this)
|
|||
p_uart->recv_cb(id, p_uart->uart_fd, rx_buffer, len);
|
||||
}
|
||||
|
||||
if(p_uart->debug_show_cb && p_uart->debug_show == CommDebugShow::on)
|
||||
if(p_uart->debug_show_cb && p_uart->debug_show == COMM_DEBUG_ON)
|
||||
{
|
||||
p_uart->debug_show_cb(p_uart->p_para->device.c_str(), CommDir::dir_recv, rx_buffer, len);
|
||||
p_uart->debug_show_cb(p_uart->p_para->device, CommDir::dir_recv, rx_buffer, len);
|
||||
}
|
||||
}
|
||||
else if(0 == len)
|
||||
|
|
@ -299,9 +299,9 @@ static int uart_send(void *p_this, const uint8_t *tx, uint16_t tx_len)
|
|||
|
||||
tcdrain(p_uart->uart_fd);
|
||||
|
||||
if(p_uart->debug_show_cb && p_uart->debug_show == CommDebugShow::on)
|
||||
if(p_uart->debug_show_cb && p_uart->debug_show == COMM_DEBUG_ON)
|
||||
{
|
||||
p_uart->debug_show_cb(p_uart->p_para->device.c_str(), CommDir::dir_send, tx, tx_len);
|
||||
p_uart->debug_show_cb(p_uart->p_para->device, CommDir::dir_send, tx, tx_len);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
@ -325,7 +325,7 @@ static int uart_close(void *p_this)
|
|||
|
||||
if(p_uart->state_cb)
|
||||
{
|
||||
p_uart->state_cb(0, p_uart->uart_fd, CommState::disconnected);
|
||||
p_uart->state_cb(0, p_uart->uart_fd, COMM_STATE_DISCONNECTED);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <string>
|
||||
|
||||
#include "comm_udp.h"
|
||||
#include "myLog.h"
|
||||
|
|
@ -83,7 +84,7 @@ static void udp_run(int id, const char *udp_type, int sockfd, stru_comm_udp *p_u
|
|||
p_udp->recv_cb(id, sockfd, buffer, len);
|
||||
}
|
||||
|
||||
if(p_udp->debug_show_cb && p_udp->debug_show == CommDebugShow::on)
|
||||
if(p_udp->debug_show_cb && p_udp->debug_show == COMM_DEBUG_ON)
|
||||
{
|
||||
std::string str = "udp" + std::string(udp_type) + "id: " + std::to_string(id) + ", " + "fd: " + std::to_string(sockfd) + ",";
|
||||
p_udp->debug_show_cb(str.c_str(), CommDir::dir_recv, buffer, len);
|
||||
|
|
@ -126,26 +127,26 @@ static int udp_client_connect(int id, void *p_this)
|
|||
|
||||
addr.sin_port = htons(p_udp->p_para->remote_port);
|
||||
|
||||
if(p_udp->p_para->remote_ip.empty())
|
||||
if(p_udp->p_para->remote_ip[0] == '\0')
|
||||
{
|
||||
LOG_E("remote_ip is empty");
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
if(inet_pton(AF_INET, p_udp->p_para->remote_ip.c_str(), &addr.sin_addr) < 0)
|
||||
if(inet_pton(AF_INET, p_udp->p_para->remote_ip, &addr.sin_addr) < 0)
|
||||
{
|
||||
LOG_E("inet_pton failed for ip:%s", p_udp->p_para->remote_ip.c_str());
|
||||
LOG_E("inet_pton failed for ip:%s", p_udp->p_para->remote_ip);
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
LOG_I("udp client connect %s:%d success", p_udp->p_para->remote_ip.c_str(), p_udp->p_para->remote_port);
|
||||
LOG_I("udp client connect %s:%d success", p_udp->p_para->remote_ip, p_udp->p_para->remote_port);
|
||||
|
||||
p_udp->sockfd = sockfd;
|
||||
|
||||
if(p_udp->state_cb)
|
||||
{
|
||||
p_udp->state_cb(id, sockfd, CommState::connected);
|
||||
p_udp->state_cb(id, sockfd, COMM_STATE_CONNECTED);
|
||||
}
|
||||
|
||||
udp_run(id, "client", sockfd, p_udp);
|
||||
|
|
@ -182,15 +183,15 @@ static int udp_server_connect(int id, void *p_this)
|
|||
|
||||
addr.sin_port = htons(p_udp->p_para->local_port);
|
||||
|
||||
if(p_udp->p_para->local_ip.empty() || p_udp->p_para->local_ip.compare("0.0.0.0") == 0)
|
||||
if(p_udp->p_para->local_ip[0] == '\0' || strcmp(p_udp->p_para->local_ip, "0.0.0.0") == 0)
|
||||
{
|
||||
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(inet_pton(AF_INET, p_udp->p_para->local_ip.c_str(), &addr.sin_addr) < 0)
|
||||
if(inet_pton(AF_INET, p_udp->p_para->local_ip, &addr.sin_addr) < 0)
|
||||
{
|
||||
LOG_E("inet_pton failed for ip:%s", p_udp->p_para->local_ip.c_str());
|
||||
LOG_E("inet_pton failed for ip:%s", p_udp->p_para->local_ip);
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -203,13 +204,13 @@ static int udp_server_connect(int id, void *p_this)
|
|||
return -1;
|
||||
}
|
||||
|
||||
LOG_I("udp server bind %s:%d success", p_udp->p_para->local_ip.c_str(), p_udp->p_para->local_port);
|
||||
LOG_I("udp server bind %s:%d success", p_udp->p_para->local_ip, p_udp->p_para->local_port);
|
||||
|
||||
p_udp->sockfd = sockfd;
|
||||
|
||||
if(p_udp->state_cb)
|
||||
{
|
||||
p_udp->state_cb(id, sockfd, CommState::connected);
|
||||
p_udp->state_cb(id, sockfd, COMM_STATE_CONNECTED);
|
||||
}
|
||||
|
||||
udp_run(id, "server", sockfd, p_udp);
|
||||
|
|
@ -237,9 +238,9 @@ static int udp_send(int id, int fd, const char *udp_type, void *p_this, const ui
|
|||
memset(&dest_addr, 0, sizeof(dest_addr));
|
||||
dest_addr.sin_family = AF_INET;
|
||||
dest_addr.sin_port = htons(p_udp->p_para->remote_port);
|
||||
if(inet_pton(AF_INET, p_udp->p_para->remote_ip.c_str(), &dest_addr.sin_addr) < 0)
|
||||
if(inet_pton(AF_INET, p_udp->p_para->remote_ip, &dest_addr.sin_addr) < 0)
|
||||
{
|
||||
LOG_E("inet_pton failed for ip:%s", p_udp->p_para->remote_ip.c_str());
|
||||
LOG_E("inet_pton failed for ip:%s", p_udp->p_para->remote_ip);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -250,7 +251,7 @@ static int udp_send(int id, int fd, const char *udp_type, void *p_this, const ui
|
|||
return -1;
|
||||
}
|
||||
|
||||
if(p_udp->debug_show_cb && p_udp->debug_show == CommDebugShow::on)
|
||||
if(p_udp->debug_show_cb && p_udp->debug_show == COMM_DEBUG_ON)
|
||||
{
|
||||
std::string str = "udp" + std::string(udp_type) + "id: " + std::to_string(id) + ", " + "fd: " + std::to_string(fd) + ",";
|
||||
p_udp->debug_show_cb(str.c_str(), CommDir::dir_send, data, len);
|
||||
|
|
@ -281,7 +282,7 @@ static int udp_close(int id, void *p_this)
|
|||
|
||||
if(p_udp->state_cb)
|
||||
{
|
||||
p_udp->state_cb(id, fd, CommState::disconnected);
|
||||
p_udp->state_cb(id, fd, COMM_STATE_DISCONNECTED);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -2392,6 +2392,6 @@ LOCAL void cmd_dc_complete(const char *buf, char ***completions, int *ncomp)
|
|||
cmd_sub_complete(buf, completions, ncomp, subs, sizeof(subs)/sizeof(subs[0]));
|
||||
}
|
||||
|
||||
CMD_REGISTER("datacenter", cmd_dc, "数据中心的控制命令", cmd_dc_complete);
|
||||
CMD_REGISTER_C("datacenter", cmd_dc, "数据中心的控制命令", cmd_dc_complete);
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -58,29 +58,29 @@ typedef struct
|
|||
LOCAL stru_channel_para g_channel_para[ENUM_COMM_MAX] =
|
||||
{
|
||||
[ENUM_COMM_TCP_S_0] = {
|
||||
.type = CommType::tcp_server,
|
||||
.debug_show = CommDebugShow::off,
|
||||
.type = COMM_TYPE_TCP_SERVER,
|
||||
.debug_show = COMM_DEBUG_OFF,
|
||||
.p_para = &g_tcp_para[ENUM_TCP_SERVER_0],
|
||||
.id = -1,
|
||||
.socket_fd = -1,
|
||||
},
|
||||
[ENUM_COMM_TCP_C_0] = {
|
||||
.type = CommType::tcp_client,
|
||||
.debug_show = CommDebugShow::off,
|
||||
.type = COMM_TYPE_TCP_CLIENT,
|
||||
.debug_show = COMM_DEBUG_OFF,
|
||||
.p_para = &g_tcp_para[ENUM_TCP_CLIENT_0],
|
||||
.id = -1,
|
||||
.socket_fd = -1,
|
||||
},
|
||||
[ENUM_COMM_UART_0] = {
|
||||
.type = CommType::uart,
|
||||
.debug_show = CommDebugShow::off,
|
||||
.type = COMM_TYPE_UART,
|
||||
.debug_show = COMM_DEBUG_OFF,
|
||||
.p_para = &g_uart_para[ENUM_UART_0],
|
||||
.id = -1,
|
||||
.socket_fd = -1,
|
||||
},
|
||||
[ENUM_COMM_TCP_S_1] = {
|
||||
.type = CommType::tcp_server,
|
||||
.debug_show = CommDebugShow::off,
|
||||
.type = COMM_TYPE_TCP_SERVER,
|
||||
.debug_show = COMM_DEBUG_OFF,
|
||||
.p_para = &g_tcp_para[ENUM_TCP_SERVER_1],
|
||||
.id = -1,
|
||||
.socket_fd = -1,
|
||||
|
|
@ -113,11 +113,11 @@ LOCAL void com_channel_state_cb(int id, int socket_fd, CommState state)
|
|||
{
|
||||
if(g_channel_para[i].id == id)
|
||||
{
|
||||
if(state == CommState::connected)
|
||||
if(state == COMM_STATE_CONNECTED)
|
||||
{
|
||||
g_channel_para[i].socket_fd = socket_fd;
|
||||
}
|
||||
else if(state == CommState::disconnected)
|
||||
else if(state == COMM_STATE_DISCONNECTED)
|
||||
{
|
||||
g_channel_para[i].socket_fd = -1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -550,4 +550,4 @@ LOCAL void cmd_iec_complete(const char *buf, char ***completions, int *ncomp)
|
|||
}
|
||||
}
|
||||
|
||||
CMD_REGISTER("iec", cmd_iec, "iec线程的控制命令", cmd_iec_complete);
|
||||
CMD_REGISTER_C("iec", cmd_iec, "iec线程的控制命令", cmd_iec_complete);
|
||||
|
|
@ -876,4 +876,4 @@ LOCAL void cmd_iec61850m_complete(const char *buf, char ***completions, int *nco
|
|||
cmd_sub_complete(buf, completions, ncomp, subs, sizeof(subs)/sizeof(subs[0]));
|
||||
}
|
||||
|
||||
CMD_REGISTER(MODULE_IEC61850M, cmd_iec61850m, "iec61850客户端线程的控制命令", cmd_iec61850m_complete);
|
||||
CMD_REGISTER_C(MODULE_IEC61850M, cmd_iec61850m, "iec61850客户端线程的控制命令", cmd_iec61850m_complete);
|
||||
|
|
@ -918,7 +918,7 @@ LOCAL void cmd_modbus_m_complete(const char *buf, char ***completions, int *ncom
|
|||
cmd_sub_complete(buf, completions, ncomp, subs, sizeof(subs)/sizeof(subs[0]));
|
||||
}
|
||||
|
||||
CMD_REGISTER(MODULE_MODBUS_M, cmd_modbus_m, "Modbus主站线程的控制命令", cmd_modbus_m_complete);
|
||||
CMD_REGISTER_C(MODULE_MODBUS_M, cmd_modbus_m, "Modbus主站线程的控制命令", cmd_modbus_m_complete);
|
||||
|
||||
|
||||
// ==================== 应用线程入口 ====================
|
||||
|
|
|
|||
|
|
@ -1249,7 +1249,7 @@ LOCAL void cmd_self_ptl_complete(const char *buf, char ***completions, int *ncom
|
|||
cmd_sub_complete(buf, completions, ncomp, subs, sizeof(subs)/sizeof(subs[0]));
|
||||
}
|
||||
|
||||
CMD_REGISTER("self_ptl", cmd_self_ptl, "私有规约的控制命令", cmd_self_ptl_complete);
|
||||
CMD_REGISTER_C("self_ptl", cmd_self_ptl, "私有规约的控制命令", cmd_self_ptl_complete);
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue