#include <unistd.h>
#include "user.h"
-#ifdef __x86_64__
-
-#define PID_FILE "/tmp/pcs_dtu.pid"
-
-#else
-
-#define PID_FILE "/var/run/pcs_dtu/pcs_dtu.pid"
-
-#endif
-
-
int bms_start(void);
int pcs_start(void);
int webserver_start(void);
char *bms_data_to_str(bms_data_t *bms_data);
extern bool user_bms_send_heartbeat(uint16_t heartbeat_value);
-board_info_t g_board_info;
-
static int alone_runnind(void)
{
int fd;
int main(void)
{
- pcs_data_t pcs_data;
- bms_data_t bms_data;
uint16_t heartbeat_value = 0x1234;
uint32_t n = 0;
}
#endif
webserver_start();
- user_pcs_data_get( &pcs_data );
- memcpy( &g_board_info.pcs_data, &pcs_data, sizeof( pcs_data ) );
+
while (1) {
sleep(1);
- user_pcs_data_get( &pcs_data );
- if( memcmp( &g_board_info.pcs_data, &pcs_data, sizeof( pcs_data ) ) != 0 ){
- memcpy( &g_board_info.pcs_data, &pcs_data, sizeof( pcs_data ) );
- printf( "pcs data chnged --: \n" );
- //todo
- }
- user_bms_data_get( &bms_data );
- if( memcmp( &g_board_info.bms_data, &bms_data, 32 ) != 0 ){
- printf( "bms data chnged \n" );
- printf( "\n\n bms data: %s \n\n", bms_data_to_str( &bms_data ) );
- memcpy( &g_board_info.bms_data, &bms_data, sizeof( bms_data ) );
- }
if( n++ > 5 ){
n = 0;
user_bms_send_heartbeat( heartbeat_value++ );
}
-
-
}
return 0;
#include <time.h>
#include "user_log.h"
+#include "user_can.h"
+#include "user_mb.h"
#include "user_bms.h"
#include "user_pcs.h"
#include "user_web.h"
-typedef struct{
- bms_info_t bms_data;
- pcs_info_t pcs_data;
-}board_info_t;
+#ifdef __x86_64__
+
+#define PID_FILE "/tmp/pcs_dtu.pid"
+
+//for debug
+#define BMS_CAN_BUS CAN_BUS_4 //"vcan0"
+#define PCS_MB_BUS MB_RTU_BUS_5 //ttyUSB0
+
+#else
+
+#define PID_FILE "/var/run/pcs_dtu/pcs_dtu.pid"
+
+#define BMS_CAN_BUS CAN_BUS_1 //"can0"
+#define PCS_MB_BUS MB_RTU_BUS_3 //ttyS3
+
+#endif
+
#endif // USER_H
#include <pthread.h>
#include <errno.h>
+#include "user.h"
+#include "user_can.h"
#include "user_bms.h"
-#define BMS_CAN "can0"
-#define BMS_CAN_DOWN "ip link set can0 down"
-#define BMS_CAN_UP "ip link set can0 up"
-
-static struct sockaddr_can addr;
-
bms_info_t g_bms;
return (int16_t)(data[0] | (data[1] << 8));
}
-void bms_can_reset( void )
-{
- printf( "can0 down...\n" );
- system( BMS_CAN_DOWN );
- usleep(100);
- printf( "can0 up...\n" );
- system( BMS_CAN_UP );
-
-}
-
/* 获取BMS设备信息 */
int user_bms_data_get(bms_data_t *bms_data)
{
return -1;
}
pthread_mutex_lock(&g_bms.mutex);
- bms_data->connected = g_bms.data.connected;
memcpy( bms_data, &g_bms.data, sizeof( bms_data_t ));
pthread_mutex_unlock(&g_bms.mutex);
return 0;
}
-bool user_bms_init(const char *can_interface)
-{
- struct ifreq ifr;
- int ret;
-
- //check can0 interface
- if( strcmp( can_interface, "can0" ) != 0 ){
- printf( "user_bms_init: can interface must be can0\n" );
- return false;
- }
- ret = system( "ip link set can0 down" );
- ret = system( "ip link set can0 type can bitrate 250000 dbitrate 250000 fd on" );
- ret = system( "ip link set can0 up" );
- printf( "system set can ret:%d\n", ret );
-
- // 初始化BMS数据
- memset(&g_bms, 0, sizeof(g_bms));
- g_bms.can_socket = -1;
- pthread_mutex_init(&g_bms.mutex, NULL);
-
- // 创建CAN socket
- if ((g_bms.can_socket = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
- perror("socket");
- return false;
- }
-
- // 设置CAN接口
- strcpy(ifr.ifr_name, can_interface);
- if (ioctl(g_bms.can_socket, SIOCGIFINDEX, &ifr) < 0) {
- perror("ioctl");
- close(g_bms.can_socket);
- g_bms.can_socket = -1;
- return false;
- }
-
- // 绑定CAN接口
- memset(&addr, 0, sizeof(addr));
- addr.can_family = AF_CAN;
- addr.can_ifindex = ifr.ifr_ifindex;
-
- if (bind(g_bms.can_socket, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
- perror("bind");
- close(g_bms.can_socket);
- g_bms.can_socket = -1;
- return false;
- }
-
- printf("BMS CAN interface initialized on %s\n", can_interface);
- return true;
-}
bool user_bms_send_heartbeat(uint16_t heartbeat_value)
{
- if (g_bms.can_socket < 0) return false;
+ if( !g_bms.can ) return false;
- struct can_frame frame;
- memset(&frame, 0, sizeof(struct can_frame));
+ can_frame_t frame;
+ memset(&frame, 0, sizeof(can_frame_t));
// 设置CAN ID和数据长度
- frame.can_id = PCS_HEARTBEAT_ID;
- frame.can_dlc = 8;
+ frame.id = PCS_HEARTBEAT_ID;
+ frame.dlc = 8;
// 设置心跳值 (小端格式)
frame.data[0] = heartbeat_value & 0xFF;
// 发送CAN帧
printf( "bms write ... \n" );
- if (write(g_bms.can_socket, &frame, sizeof(struct can_frame)) != sizeof(struct can_frame)) {
- perror("bsm write error");
- if (errno == ENOBUFS) {
- printf("write faile [ENOBUFS]: reset can interface!\b");
- bms_can_reset();
- }
- return false;
- }
return true;
}
-void user_bms_parse_frame1(const uint8_t *data) {
+void user_bms_parse_frame1(const uint8_t *data)
+{
pthread_mutex_lock( &g_bms.mutex );
g_bms.data.total_current_p1a );
}
-void user_bms_parse_frame2(const uint8_t *data) {
+void user_bms_parse_frame2(const uint8_t *data)
+{
pthread_mutex_lock( &g_bms.mutex );
g_bms.data.reserve_2 );
}
-void user_bms_parse_frame3(const uint8_t *data) {
+void user_bms_parse_frame3(const uint8_t *data)
+{
pthread_mutex_lock( &g_bms.mutex );
g_bms.data.max_temp_num );
}
-// 在文件开头添加poll相关头文件
-#include <poll.h>
-
-int user_bms_receive_data(void)
-{
- int connected = 0;
- static int no_data_count = 0; // 用于计数无数据的次数
-
- if (g_bms.can_socket < 0) return -1;
-
- struct can_frame frame;
- ssize_t nbytes = 0;
-
- // 设置poll文件描述符
- struct pollfd fds;
- fds.fd = g_bms.can_socket;
- fds.events = POLLIN; // 检查可读性
-
- // 使用poll进行非阻塞读取,设置1秒超时
- int ret = poll(&fds, 1, 1000); // 1000ms = 1秒
-
- if (ret > 0) {
- // 有数据可读
- if (fds.revents & POLLIN) {
- nbytes = read(g_bms.can_socket, &frame, sizeof(struct can_frame));
-
- if (nbytes < 0) {
- perror("read");
- sleep( 5 );
- return -1;
- }
-
- printf("can read msg, size:%ld\n", nbytes);
- if (nbytes != sizeof(struct can_frame)) return -1;
-
- printf("read frame: id:%08X, dlc:%d, data:", frame.can_id, frame.can_dlc);
- for( int i=0; i<frame.can_dlc; i++ ){
- printf("%02x ", frame.data[i]);
- }
- printf("\n");
-
- if( nbytes == sizeof(struct can_frame)){
-
- frame.can_id &= 0x1FFFFFFF;
- // 根据CAN ID解析不同的数据帧
- switch (frame.can_id) {
- case BMS_FRAME1_ID:
- if (frame.can_dlc == 8) {
- user_bms_parse_frame1(frame.data);
- connected = 1;
- }
- break;
-
- case BMS_FRAME2_ID:
- if (frame.can_dlc == 8) {
- user_bms_parse_frame2(frame.data);
- connected = 1;
- }
- break;
-
- case BMS_FRAME3_ID:
- if (frame.can_dlc == 8) {
- user_bms_parse_frame3(frame.data);
- connected = 1;
- }
- break;
-
- case BMS_FRAME4_ID:
- if (frame.can_dlc == 8) {
- user_bms_parse_frame4(frame.data);
- connected = 1;
- }
- break;
-
- case BMS_FRAME5_ID:
- if (frame.can_dlc == 8) {
- user_bms_parse_frame5(frame.data);
- connected = 1;
- }
- break;
-
- default:
- // 忽略其他ID的帧
- break;
- }
- }
-
- // 收到有效数据,重置无数据计数器
- if (connected) {
- no_data_count = 0;
-
- if (g_bms.data.connected == false) {
- printf("\n\n----------------BMS ----> connected\n");
- // 更新最后接收时间
- g_bms.last_update_time = time(NULL);
- g_bms.data.connected = true;
- }
- }
- }
- } else if (ret == 0) {
- // poll超时,没有数据可读
- no_data_count++;
-
- // 如果连续10秒没有数据(假设该函数每秒调用一次)
- if (no_data_count >= 10 && g_bms.data.connected) {
- printf("\n\n----------------BMS ----> disconnected (10s no data)\n");
- g_bms.data.connected = false;
- }
- } else {
- // poll出错
- perror("poll");
- }
- return 0;
-}
-
-
char *bms_data_to_str(bms_data_t *bms_data)
{
static char str[1024];
return str;
}
-const bms_data_t *user_bms_get_data( bms_data_t *data )
+
+void user_bms_deinit(void)
{
-// 检查BMS连接状态 (5秒超时)
-// time_t now = time(NULL);
-// if (now - bms_data.last_update_time > 5) {
-// bms_data.is_connected = false;
-// }
- if( data ){
- //todo locked
- memcpy( data, &g_bms.data, sizeof( bms_data_t) );
- //todo unlocked
- }else{
- data = &g_bms.data;
- }
- return data;
+
}
-void user_bms_deinit(void) {
- if (g_bms.can_socket >= 0) {
- close(g_bms.can_socket);
- g_bms.can_socket = -1;
- printf("BMS CAN interface closed\n");
- }
+static int bms_connect_cb(can_device_t *dev, int connected, void *user_data)
+{
+ printf( "bms connect cb ---->%d\n\n", connected );
+ g_bms.data.connected = connected;
+ return 0;
}
-// 线程函数声明
-static void* bms_task_thread(void* arg)
+// CAN数据接收回调函数
+static int bms_receive_cb(can_device_t *dev, can_frame_t *frame, void *user_data)
{
- // 初始化BMS模块 (使用vcan0接口)
- if (!user_bms_init("can0")) {
- printf("Failed to initialize BMS module\n");
- return NULL;
+ int ret = -1;
+ printf("收到CAN数据帧:\n");
+ printf(" ID: 0x%08X\n", frame->id);
+ printf(" 类型: %s\n", frame->is_extended ? "扩展帧" : "标准帧");
+ printf(" 长度: %d 字节\n", frame->dlc);
+ printf(" 数据: ");
+ for (int i = 0; i < frame->dlc; i++) {
+ printf("0x%02X ", frame->data[i]);
}
-
- uint16_t heartbeat = 0;
-
- // 主循环
- while (1) {
- // 接收BMS数据
- user_bms_receive_data();
+ printf("\n\n");
+ if (can_device_send( dev, frame) == 0) {
+ printf("send echo data...\n");
}
-
- // 关闭BMS模块
- user_bms_deinit();
- return NULL;
+ // 根据CAN ID解析不同的数据帧
+ if(frame->id == BMS_FRAME1_ID && frame->dlc == 8) {
+ user_bms_parse_frame1(frame->data);
+ ret = 0;
+ }else if(frame->id == BMS_FRAME2_ID && frame->dlc == 8) {
+ user_bms_parse_frame2(frame->data);
+ ret = 0;
+ }else if(frame->id == BMS_FRAME3_ID && frame->dlc == 8) {
+ user_bms_parse_frame3(frame->data);
+ ret = 0;
+ }else if(frame->id == BMS_FRAME4_ID && frame->dlc == 8) {
+ user_bms_parse_frame4(frame->data);
+ ret = 0;
+ }else if(frame->id == BMS_FRAME5_ID && frame->dlc == 8) {
+ user_bms_parse_frame5(frame->data);
+ ret = 0;
+ }
+ return ret;
}
/**
* @brief 启动BMS控制线程
* @return true-启动成功 false-启动失败
*/
-int bms_start(void) {
- pthread_t thread_id;
- int ret = pthread_create(&thread_id, NULL, bms_task_thread, NULL);
- if (ret != 0) {
- perror("pcs_start: pthread_create failed");
+int bms_start(void)
+{
+ // 初始化BMS数据
+ memset(&g_bms, 0, sizeof(g_bms));
+ pthread_mutex_init(&g_bms.mutex, NULL);
+
+ // 初始化CAN总线
+ printf("初始化CAN总线...\n");
+ if (can_bus_init(BMS_CAN_BUS) < 0) {
+ fprintf(stderr, "CAN总线初始化失败,程序退出\n");
+ return -1;
+ }
+
+ // 创建CAN设备
+ printf("创建CAN设备...\n");
+ g_bms.can = can_device_new(
+ "BMS",
+ BMS_CAN_BUS, // 总线ID
+ bms_receive_cb, // 接收回调函数
+ bms_connect_cb, // 接收回调函数
+ &g_bms, // 用户数据
+ 10 // 发送队列大小
+ );
+
+ if (!g_bms.can) {
+ fprintf(stderr, "CAN设备创建失败,程序退出\n");
return -1;
}
- // 分离线程,避免资源泄漏
- pthread_detach(thread_id);
+ // 启动CAN设备
+ printf("启动CAN设备...\n");
+ if (can_device_start( g_bms.can ) < 0) {
+ fprintf(stderr, "CAN设备启动失败,程序退出\n");
+ can_device_delete(g_bms.can );
+ g_bms.can = NULL;
+ return -1;
+ }
+
+
return 0;
}
#include <stdint.h>
#include <stdbool.h>
#include <pthread.h>
+#include "user_can.h"
// BMS设备地址定义
#define BMS_ADDR_1 0x01
typedef struct{
bms_data_t data;
- int lock; //for w/r data;
pthread_mutex_t mutex;
// 内部状态
uint32_t last_update_time; // 最后更新时间戳
- int can_socket;
+ can_device_t *can;
} bms_info_t;
-// 初始化BMS通信模块
-bool user_bms_init(const char *can_interface);
// 发送PCS心跳包
bool user_bms_send_heartbeat(uint16_t heartbeat_value);
-// 接收并解析BMS数据
-int user_bms_receive_data(void);
-
-// 获取BMS数据
-const bms_data_t *user_bms_get_data( bms_data_t *data );
-
// 关闭BMS通信模块
void user_bms_deinit(void);
/* 获取BMS设备信息 */
-int user_bms_data_get(bms_data_t *pcs_data);
+int user_bms_data_get(bms_data_t *bms_data);
#endif // USER_BMS_H
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <linux/can.h>
+#include <linux/can/raw.h>
+#include <poll.h>
+#include <errno.h>
+#include "user.h"
+#include "user_can.h"
+
+// 总线配置信息
+typedef struct {
+ const char *interface_name;
+ uint32_t bitrate;
+} can_bus_config_t;
+
+// 总线配置表
+static can_bus_config_t can_bus_configs[MAX_CAN_BUSES] = {
+ [CAN_BUS_1] = {"can0", 250000}, //can0 接口,250K速率
+ [CAN_BUS_2] = {"can1", 250000}, //can1 接口,250K速率
+ [CAN_BUS_3] = {"can2", 250000}, //can2 接口,250K速率
+ [CAN_BUS_4] = {"vcan0", 250000}, //vcan0 接口,250K速率 for debug
+};
+
+// 检查总线ID是否有效
+static int check_bus_id(int bus_id) {
+ if (bus_id < 0 || bus_id >= MAX_CAN_BUSES) {
+ fprintf(stderr, "错误: 无效的总线ID %d\n", bus_id);
+ return -1;
+ }
+ return 0;
+}
+
+// 执行系统命令
+static int execute_system_command(const char *command) {
+ int ret = system(command);
+ if (ret < 0) {
+ fprintf(stderr, "执行命令失败: %s, 错误: %s\n", command, strerror(errno));
+ return -1;
+ }
+ return 0;
+}
+
+// CAN总线初始化函数
+int can_bus_init(int bus_id) {
+ if (check_bus_id(bus_id) < 0) {
+ return -1;
+ }
+
+ const char *ifname = can_bus_configs[bus_id].interface_name;
+ uint32_t bitrate = can_bus_configs[bus_id].bitrate;
+
+ // 设置CAN接口参数
+ char command[256];
+ snprintf(command, sizeof(command), "ip link set %s type can bitrate %u", ifname, bitrate);
+ if (execute_system_command(command) < 0) {
+ return -1;
+ }
+
+ // 启用CAN接口
+ snprintf(command, sizeof(command), "ip link set up %s", ifname);
+ if (execute_system_command(command) < 0) {
+ return -1;
+ }
+
+ printf("CAN总线 %d (%s) 初始化成功,速率: %u bps\n",
+ bus_id, ifname, bitrate);
+
+ return 0;
+}
+
+// CAN总线重置函数
+int can_bus_reset(int bus_id) {
+ if (check_bus_id(bus_id) < 0) {
+ return -1;
+ }
+
+ const char *ifname = can_bus_configs[bus_id].interface_name;
+
+ // 关闭CAN接口
+ char command[256];
+ snprintf(command, sizeof(command), "ip link set down %s", ifname);
+ if (execute_system_command(command) < 0) {
+ return -1;
+ }
+
+ // 重新启用CAN接口
+ snprintf(command, sizeof(command), "ip link set up %s", ifname);
+ if (execute_system_command(command) < 0) {
+ return -1;
+ }
+
+ printf("CAN总线 %d (%s) 重置成功\n", bus_id, ifname);
+
+ return 0;
+}
+
+// 队列操作函数
+static int queue_is_full(can_device_t *device) {
+ return ((device->tx_queue_tail + 1) % device->tx_queue_size) == device->tx_queue_head;
+}
+
+static int queue_is_empty(can_device_t *device) {
+ return device->tx_queue_head == device->tx_queue_tail;
+}
+
+static int queue_push(can_device_t *device, can_frame_t *frame) {
+ pthread_mutex_lock(&device->tx_queue_mutex);
+
+ if (queue_is_full(device)) {
+ pthread_mutex_unlock(&device->tx_queue_mutex);
+ return -1; // 队列已满
+ }
+
+ memcpy(&device->tx_queue[device->tx_queue_tail], frame, sizeof(can_frame_t));
+ device->tx_queue_tail = (device->tx_queue_tail + 1) % device->tx_queue_size;
+
+ pthread_mutex_unlock(&device->tx_queue_mutex);
+ return 0;
+}
+
+static int queue_pop(can_device_t *device, can_frame_t *frame) {
+ pthread_mutex_lock(&device->tx_queue_mutex);
+
+ if (queue_is_empty(device)) {
+ pthread_mutex_unlock(&device->tx_queue_mutex);
+ return -1; // 队列为空
+ }
+
+ memcpy(frame, &device->tx_queue[device->tx_queue_head], sizeof(can_frame_t));
+ device->tx_queue_head = (device->tx_queue_head + 1) % device->tx_queue_size;
+
+ pthread_mutex_unlock(&device->tx_queue_mutex);
+ return 0;
+}
+
+// 创建CAN套接字
+static int create_can_socket(const char *ifname) {
+ int s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
+ if (s < 0) {
+ fprintf(stderr, "创建CAN套接字失败: %s\n", strerror(errno));
+ return -1;
+ }
+
+ struct ifreq ifr;
+ strcpy(ifr.ifr_name, ifname);
+ if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
+ fprintf(stderr, "获取接口索引失败: %s\n", strerror(errno));
+ close(s);
+ return -1;
+ }
+
+ struct sockaddr_can addr;
+ addr.can_family = AF_CAN;
+ addr.can_ifindex = ifr.ifr_ifindex;
+
+ if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+ fprintf(stderr, "绑定CAN接口失败: %s\n", strerror(errno));
+ close(s);
+ return -1;
+ }
+
+ return s;
+}
+
+// 设备线程函数
+static void *can_device_thread(void *arg) {
+ can_device_t *device = (can_device_t *)arg;
+ struct pollfd fds[1];
+ can_frame_t frame;
+ struct can_frame canfd_frame;
+ int connected = 0;
+ uint32_t timeout_ms = 0;
+
+ fds[0].fd = device->socket_fd;
+ // 初始时只检测POLLIN事件,不检测POLLOUT事件
+ fds[0].events = POLLIN;
+
+
+ while (device->is_running) {
+ int ret = poll(fds, 1, 100); // 100ms超时
+
+ if (ret < 0) {
+ if (errno != EINTR) {
+ fprintf(stderr, "Poll错误: %s\n", strerror(errno));
+ break;
+ }
+ continue;
+ }else if( ret == 0 ){
+ //timeout
+ timeout_ms += 100;
+ if( timeout_ms > 10*1000 && connected ){
+printf( "----connected -> 0 \n" );
+ connected = 0;
+ if( device->connect_cb )
+ device->connect_cb( device, connected, NULL );
+ }
+ }
+
+ // 处理接收事件
+ if (fds[0].revents & POLLIN) {
+ memset(&canfd_frame, 0, sizeof(canfd_frame));
+ ssize_t nbytes = read(device->socket_fd, &canfd_frame, sizeof(canfd_frame));
+
+ if (nbytes < 0) {
+ fprintf(stderr, "读取CAN数据失败: %s\n", strerror(errno));
+ continue;
+ }
+
+ if (nbytes < sizeof(struct can_frame)) {
+ fprintf(stderr, "接收到不完整的CAN帧\n");
+ continue;
+ }
+
+ // 转换为我们定义的帧格式
+ memset(&frame, 0, sizeof(frame));
+ frame.id = canfd_frame.can_id & CAN_EFF_MASK;
+ frame.is_extended = (canfd_frame.can_id & CAN_EFF_FLAG) ? 1 : 0;
+ frame.dlc = canfd_frame.can_dlc;
+ memcpy(frame.data, canfd_frame.data, frame.dlc);
+
+ // 调用回调函数处理数据
+ if( !device->rx_cb || device->rx_cb(device, &frame, device->user_data) == 0 ){
+ timeout_ms = 0;
+ if( !connected ){
+ connected = 1;
+ if( device->connect_cb )
+ device->connect_cb( device, connected, NULL );
+ }
+ }
+ }
+
+ // 处理发送事件
+ if (fds[0].revents & POLLOUT) {
+ if (!queue_is_empty(device)) {
+ memset(&frame, 0, sizeof(frame));
+ if (queue_pop(device, &frame) == 0) {
+ // 转换为Linux CAN帧格式
+ memset(&canfd_frame, 0, sizeof(canfd_frame));
+ canfd_frame.can_id = frame.id;
+ if (frame.is_extended) {
+ canfd_frame.can_id |= CAN_EFF_FLAG;
+ }
+ canfd_frame.can_dlc = frame.dlc;
+ memcpy(canfd_frame.data, frame.data, frame.dlc);
+
+ // 发送数据
+ ssize_t nbytes = write(device->socket_fd, &canfd_frame, sizeof(canfd_frame));
+ if (nbytes < 0) {
+ fprintf(stderr, "发送CAN数据失败: %s\n", strerror(errno));
+ // 可以考虑重新入队
+ //queue_push(device, &frame);
+ }
+ }
+ }
+
+ // 发送完成后,检查队列是否为空,如果为空则移除POLLOUT事件检测
+ if (queue_is_empty(device)) {
+ fds[0].events &= ~POLLOUT;
+ }
+ }
+
+ // 检查发送队列,如果不为空但当前未检测POLLOUT事件,则添加POLLOUT事件检测
+ if (!queue_is_empty(device) && !(fds[0].events & POLLOUT)) {
+ fds[0].events |= POLLOUT;
+ }
+ }
+
+ return NULL;
+}
+
+// 发送CAN数据帧
+int can_device_send(can_device_t *device, can_frame_t *frame) {
+ if (!device || !frame || !device->is_running) {
+ return -1;
+ }
+
+ // 检查数据长度
+ if (frame->dlc > 8) {
+ fprintf(stderr, "错误: CAN数据长度不能超过8字节\n");
+ return -1;
+ }
+
+ // 将数据帧加入发送队列
+ if (queue_push(device, frame) < 0) {
+ fprintf(stderr, "发送队列已满\n");
+ return -1;
+ }
+
+ // 数据入队后,不需要在这里设置POLLOUT事件,会在线程循环中自动检测
+
+ return 0;
+}
+
+// 创建CAN设备
+can_device_t *can_device_new( char * name, int bus_id, can_rx_cb_t rx_cb, can_connect_cb_t connect_cb, void *user_data, int tx_queue_size) {
+ if (check_bus_id(bus_id) < 0) {
+ return NULL;
+ }
+
+ if (tx_queue_size <= 0) {
+ fprintf(stderr, "错误: 发送队列大小必须大于0\n");
+ return NULL;
+ }
+
+ // 分配设备结构内存
+ can_device_t *device = (can_device_t *)malloc(sizeof(can_device_t));
+ if (!device) {
+ fprintf(stderr, "分配设备内存失败\n");
+ return NULL;
+ }
+
+ // 分配发送队列内存
+ device->tx_queue = (can_frame_t *)malloc(sizeof(can_frame_t) * tx_queue_size);
+ if (!device->tx_queue) {
+ fprintf(stderr, "分配发送队列内存失败\n");
+ free(device);
+ return NULL;
+ }
+
+ // 初始化设备结构
+ device->name = strdup( name );
+ device->bus_id = bus_id;
+ device->rx_cb = rx_cb;
+ device->connect_cb = connect_cb;
+ device->user_data = user_data;
+ device->tx_queue_size = tx_queue_size;
+ device->tx_queue_head = 0;
+ device->tx_queue_tail = 0;
+ device->is_running = false;
+
+ // 初始化互斥锁
+ if (pthread_mutex_init(&device->tx_queue_mutex, NULL) != 0) {
+ fprintf(stderr, "初始化互斥锁失败\n");
+ free(device->tx_queue);
+ free(device);
+ return NULL;
+ }
+
+ // 创建CAN套接字
+ const char *ifname = can_bus_configs[bus_id].interface_name;
+ device->socket_fd = create_can_socket(ifname);
+ if (device->socket_fd < 0) {
+ fprintf(stderr, "创建CAN套接字失败\n");
+ pthread_mutex_destroy(&device->tx_queue_mutex);
+ free(device->tx_queue);
+ free(device);
+ return NULL;
+ }
+
+ printf("CAN设备创建成功,总线ID: %d\n", bus_id);
+
+ return device;
+}
+
+// 销毁CAN设备
+void can_device_delete(can_device_t *device) {
+ if (!device) {
+ return;
+ }
+
+ // 确保设备已停止
+ can_device_stop(device);
+
+ // 关闭套接字
+ if (device->socket_fd >= 0) {
+ close(device->socket_fd);
+ device->socket_fd = -1;
+ }
+
+ // 清理资源
+ pthread_mutex_destroy(&device->tx_queue_mutex);
+ free(device->tx_queue);
+ free(device);
+
+ printf("CAN设备已销毁\n");
+}
+
+// 启动CAN设备
+int can_device_start(can_device_t *device) {
+ if (!device || device->is_running) {
+ return -1;
+ }
+
+ device->is_running = true;
+
+ // 创建设备线程
+ if (pthread_create(&device->thread_id, NULL, can_device_thread, device) != 0) {
+ fprintf(stderr, "创建设备线程失败\n");
+ device->is_running = false;
+ return -1;
+ }
+
+ printf("CAN设备已启动\n");
+
+ return 0;
+}
+
+// 停止CAN设备
+void can_device_stop(can_device_t *device) {
+ if (!device || !device->is_running) {
+ return;
+ }
+
+ device->is_running = false;
+
+ // 等待线程结束
+ pthread_join(device->thread_id, NULL);
+
+ printf("CAN设备已停止\n");
+}
--- /dev/null
+#ifndef __USER_CAN_H__
+#define __USER_CAN_H__
+
+#include <stdint.h>
+#include <pthread.h>
+#include <stdbool.h>
+
+// 总线编号定义
+#define CAN_BUS_1 0
+#define CAN_BUS_2 1
+#define CAN_BUS_3 2
+#define CAN_BUS_4 3
+#define MAX_CAN_BUSES 4
+
+// CAN帧数据结构
+typedef struct {
+ uint32_t id; // 帧ID
+ uint8_t data[8]; // 数据内容
+ uint8_t dlc; // 数据长度
+ uint8_t is_extended; // 是否为扩展帧
+} can_frame_t;
+
+typedef struct can_device can_device_t;
+// 数据接收回调函数类型
+typedef int (*can_rx_cb_t)( can_device_t *dev, can_frame_t *frame, void *user_data);
+typedef int (*can_connect_cb_t)( can_device_t *dev, int connected, void *user_data);
+
+// CAN设备结构
+struct can_device{
+ char *name;
+ int bus_id; // 所属总线ID
+ int socket_fd; // CAN套接字
+ pthread_t thread_id; // 设备线程ID
+ bool is_running; // 运行状态
+ can_rx_cb_t rx_cb; // 数据接收回调函数
+ can_connect_cb_t connect_cb; //
+ void *user_data; // 用户数据
+ int connected;
+
+ // 发送队列相关
+ can_frame_t *tx_queue; // 发送队列
+ int tx_queue_size; // 发送队列大小
+ int tx_queue_head; // 队列头指针
+ int tx_queue_tail; // 队列尾指针
+ pthread_mutex_t tx_queue_mutex; // 队列互斥锁
+};
+
+// 总线初始化函数
+extern int can_bus_init(int bus_id);
+
+// 总线重置函数
+extern int can_bus_reset(int bus_id);
+
+// 设备创建函数
+extern can_device_t *can_device_new( char *name, int bus_id, can_rx_cb_t rx_cb, can_connect_cb_t connect_cb, void *user_data, int tx_queue_size);
+
+// 设备销毁函数
+extern void can_device_delete(can_device_t *device);
+
+// 设备启动函数
+extern int can_device_start(can_device_t *device);
+
+// 设备停止函数
+extern void can_device_stop(can_device_t *device);
+
+// 发送数据帧函数
+extern int can_device_send(can_device_t *device, can_frame_t *frame);
+
+#endif // __USER_CAN_H__
+
#include <unistd.h>
#include <pthread.h>
#include "user_pcs.h"
+#include "user.h"
+
pcs_info_t g_pcs;
return true;
}
-
pcs_info_t g_pcs;
/**
user_log_info("-----------pcs_controller demo start!\n");
// 创建并启动PCS模块
- user_pcs_handler_t *pcs_handler = user_pcs_create("Main-PCS-Controller", MB_RTU_BUS_3, 1);
+ user_pcs_handler_t *pcs_handler = user_pcs_create("Main-PCS-Controller", PCS_MB_BUS, 1);
if (pcs_handler != NULL) {
g_pcs.handler = pcs_handler;
/* 初始化互斥锁 */
}
/* 全局变量声明 */
-extern board_info_t g_board_info;
extern pcs_info_t g_pcs;
extern bms_info_t g_bms;
// 设置响应
cJSON_AddNumberToObject(response_json, "code", 200);
cJSON_AddStringToObject(response_json, "msg", "pcs_parameter");
- }
+ parameter_set = parameter_set;
+ }
// 未知的请求类型
} else {
cJSON_AddNumberToObject(response_json, "code", 1);
cJSON_Delete(response_json);
return response_string;
-}
\ No newline at end of file
+}
static struct lws_context *g_context;
/* 使用cJSON解析JSON数据的函数 */
-static int parse_json_with_cjson(const char *json_str, char **text1, char **send);
+//static int parse_json_with_cjson(const char *json_str, char **text1, char **send);
/* 处理WebSocket JSON消息的函数 */
int process_websocket_message(const char *message, size_t len, char **p_resp );
lws_protocol_vh_priv_get(lws_get_vhost(wsi),
lws_get_protocol(wsi));
const struct msg *pmsg;
- int m, n, flags;
+ int m, flags;
char *recv_str;
char *resp_str;
/* 构建 JSON 响应 */
if (pss->post_body) {
char *resp = NULL;
- char *text1 = NULL;
- char *send = NULL;
+ //char *send = NULL;
printf("接收到的JSON请求体: %s\n", pss->post_body);
// 使用cJSON解析JSON格式的请求体
{
struct lws_context_creation_info info;
struct lws_context *context;
- const char *p;
int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
/* 要在内置 lws 中包含高于 NOTICE 的 LLL_ 详细度,
* lws 必须配置并构建为
/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
/* | LLL_DEBUG */;
- int thread_create_result;
lws_set_log_level(logs, NULL);
lwsl_user("LWS minimal HTTP & WebSocket combined server | visit http://localhost:12345\n");