Dawn Framework 1.0
Universal data acquisition framework for embedded systems
can.cxx
1// dawn/src/porting/nuttx/can.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/porting/can.hxx"
7
8#include <cstring>
9#include <errno.h>
10#include <fcntl.h>
11#include <sys/ioctl.h>
12#include <unistd.h>
13
14#include "dawn/debug.hxx"
15#include "dawn/porting/config.hxx"
16
17//***************************************************************************
18// Public Functions
19//***************************************************************************
20
21/****************************************************************************
22 * Public Functions
23 ****************************************************************************/
24
25/****************************************************************************
26 * Name: can_open
27 *
28 * Description:
29 * Open CAN character device interface.
30 *
31 ****************************************************************************/
32
33int can_open(const char *path)
34{
35 return open(path, O_RDWR);
36}
37
38/****************************************************************************
39 * Name: can_close
40 *
41 * Description:
42 * Close CAN character device interface.
43 *
44 ****************************************************************************/
45
46void can_close(int fd)
47{
48 close(fd);
49}
50
51/****************************************************************************
52 * Name: can_init
53 *
54 * Description:
55 * Initialzie CAN character device interface.
56 *
57 ****************************************************************************/
58
59int can_init(int fd)
60{
61 unsigned int msgalign = 0;
62 int ret;
63
64 /* The upper half packs as many queued messages as fit into the read
65 * buffer. Alignment 0 makes read() return exactly one message, which is
66 * what can_read() expects.
67 */
68
69 ret = ioctl(fd, CANIOC_SET_MSGALIGN, &msgalign);
70 if (ret < 0)
71 {
72 DAWNERR("CANIOC_SET_MSGALIGN failed %d\n", -errno);
73 return -errno;
74 }
75
76 return OK;
77}
78
79/****************************************************************************
80 * Name: can_read
81 *
82 * Description:
83 * Read CAN data (blocking).
84 *
85 ****************************************************************************/
86
87int can_read(int fd, FAR dawn::porting::canmsg_s *msg)
88{
89 struct can_msg_s frame;
90 ssize_t ret;
91
92 /* Read frame */
93
94 ret = read(fd, &frame, sizeof(struct can_msg_s));
95 if (ret < 0)
96 {
97 if (errno != EAGAIN)
98 {
99 DAWNERR("read failed %d\n", -errno);
100 }
101
102 return -errno;
103 }
104
105 /* Convert frame to common format */
106
107 msg->id = frame.cm_hdr.ch_id;
108 msg->len = can_dlc2bytes(frame.cm_hdr.ch_dlc);
109 msg->rtr = frame.cm_hdr.ch_rtr;
110 msg->error = 0;
111 msg->_res = 0;
112#ifdef CONFIG_CAN_EXTID
113 msg->extid = frame.cm_hdr.ch_extid;
114#else
115 msg->extid = 0;
116#endif
117#ifdef CONFIG_CAN_ERRORS
118 msg->error = frame.cm_hdr.ch_error;
119#endif
120
121 std::memcpy(msg->data, frame.cm_data, CAN_DATA_MAX);
122
123 return static_cast<int>(ret);
124}
125
126/****************************************************************************
127 * Name: can_send
128 *
129 * Description:
130 * Send CAN data.
131 *
132 ****************************************************************************/
133
134int can_send(int fd, FAR dawn::porting::canmsg_s *msg)
135{
136 struct can_msg_s frame;
137 ssize_t ret;
138
139 /* Convert from common format */
140
141 frame.cm_hdr.ch_id = msg->id;
142 frame.cm_hdr.ch_rtr = msg->rtr;
143 frame.cm_hdr.ch_dlc = can_bytes2dlc(msg->len);
144#ifdef CONFIG_CAN_ERRORS
145 frame.cm_hdr.ch_error = 0;
146#endif
147#ifdef CONFIG_CAN_EXTID
148 frame.cm_hdr.ch_extid = msg->extid;
149#endif
150 frame.cm_hdr.ch_tcf = 0;
151 std::memcpy(frame.cm_data, msg->data, CAN_DATA_MAX);
152
153 /* Send frame */
154
155 ret = write(fd, &frame, CAN_MSGLEN(msg->len));
156 if (ret < 0)
157 {
158 DAWNERR("write failed %d\n", -errno);
159 return -errno;
160 }
161
162 return static_cast<int>(ret);
163}
Common CAN message format for chardev and socketCAN.
Definition can.hxx:20