Blame view

RIOT/sys/can/conn/raw.c 7.75 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
  /*
   * Copyright (C) 2016 OTA keys S.A.
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     sys_can_conn
   * @{
   * @file
   * @brief       Implementation of raw CAN connection
   *
   * @author      Vincent Dupont <vincent@otakeys.com>
   * @}
   */
  
  #include <errno.h>
  #include <string.h>
  
  #include "can/conn/raw.h"
  #include "can/can.h"
  #include "can/raw.h"
  #include "timex.h"
  
  #define ENABLE_DEBUG (0)
  #include "debug.h"
  
  #include "xtimer.h"
  
  #define _TIMEOUT_TX_MSG_TYPE    (0x8000)
  #define _TIMEOUT_RX_MSG_TYPE    (0x8001)
  #define _CLOSE_CONN_MSG_TYPE    (0x8002)
  #define _TIMEOUT_MSG_VALUE      (0xABCDEFAB)
  
  #ifndef CONN_CAN_RAW_TIMEOUT_TX_CONF
  #define CONN_CAN_RAW_TIMEOUT_TX_CONF (1 * US_PER_SEC)
  #endif
  
  int conn_can_raw_create(conn_can_raw_t *conn, struct can_filter *filter, size_t count,
                          int ifnum, int flags)
  {
      assert(conn != NULL);
      assert(ifnum < CAN_DLL_NUMOF);
  
      DEBUG("conn_can_raw_create: create conn=%p, ifnum=%d flags=%d\n", (void *)conn, ifnum, flags);
  
      mbox_init(&conn->mbox, conn->mbox_queue, CONN_CAN_RAW_MBOX_SIZE);
      conn->flags = flags;
      conn->count = 0;
      conn->ifnum = ifnum;
  
      if (flags & CONN_CAN_RECVONLY) {
          can_opt_t opt;
          opt.opt = CANOPT_STATE;
          canopt_state_t state = CANOPT_STATE_LISTEN_ONLY;
          opt.data = &state;
          opt.data_len = sizeof(state);
          int ret = raw_can_set_can_opt(ifnum, &opt);
          if (ret < 0) {
              return ret;
          }
      }
  
      return conn_can_raw_set_filter(conn, filter, count);
  }
  
  
  int conn_can_raw_set_filter(conn_can_raw_t *conn, struct can_filter *filter, size_t count)
  {
      assert(conn != NULL);
      assert(filter != NULL || count == 0);
  
      DEBUG("conn_can_raw_set_filter: conn=%p, filter=%p, count=%d\n",
            (void *)conn, (void *)filter, count);
      DEBUG("conn_can_raw_set_filter: conn->filter=%p, conn->count=%d\n",
            (void *)conn->filter, conn->count);
  
      /* unset previous filters */
      if (conn->count) {
          for (size_t i = 0; i < conn->count; i++) {
              DEBUG("conn_can_raw_set_filter: unsetting filter=0x%" PRIx32 ", mask=0x%" PRIx32 "\n",
                   conn->filter[i].can_id, conn->filter[i].can_mask);
              raw_can_unsubscribe_rx_mbox(conn->ifnum, &conn->filter[i], &conn->mbox, conn);
          }
      }
  
      for (size_t i = 0; i < count; i++) {
          DEBUG("conn_can_raw_set_filter: setting filter=0x%" PRIx32 ", mask=0x%" PRIx32 "\n",
                filter[i].can_id, filter[i].can_mask);
          int ret = raw_can_subscribe_rx_mbox(conn->ifnum, &filter[i], &conn->mbox, conn);
          if (ret < 0) {
              DEBUG("conn_can_raw_set_filter: error setting filters %d\n", ret);
              for (size_t j = 0; j < i; j++) {
                  DEBUG("conn_can_raw_set_filter: unsetting filter=0x%" PRIx32 ", mask=0x%" PRIx32 "\n",
                        filter[j].can_id, filter[j].can_mask);
                  raw_can_unsubscribe_rx_mbox(conn->ifnum, &filter[j], &conn->mbox, conn);
              }
              return ret;
          }
      }
  
      conn->filter = filter;
      conn->count = count;
  
      return 0;
  }
  
  static void _tx_conf_timeout(void *arg)
  {
      conn_can_raw_t *conn = arg;
      msg_t msg;
  
      msg.type = _TIMEOUT_TX_MSG_TYPE;
      msg.content.value = _TIMEOUT_MSG_VALUE;
  
      mbox_put(&conn->mbox, &msg);
  }
  
  int conn_can_raw_send(conn_can_raw_t *conn, const struct can_frame *frame, int flags)
  {
      assert(conn != NULL);
      assert(conn->ifnum < CAN_DLL_NUMOF);
      assert((conn->flags & CONN_CAN_RECVONLY) == 0);
      assert(frame != NULL);
  
      int ret = 0;
      int handle;
  
      DEBUG("conn_can_raw_send: conn=%p, frame=%p, flags=%d\n",
            (void *)conn, (void *)frame, flags);
  
      if (flags & CONN_CAN_DONTWAIT) {
          handle = ret = raw_can_send(conn->ifnum, frame, 0);
          if (ret >= 0) {
              ret = 0;
          }
      }
      else {
          xtimer_t timer;
          timer.callback = _tx_conf_timeout;
          timer.arg = conn;
          xtimer_set(&timer, CONN_CAN_RAW_TIMEOUT_TX_CONF);
  
          handle = raw_can_send_mbox(conn->ifnum, frame, &conn->mbox);
          if (handle < 0) {
              xtimer_remove(&timer);
              return handle;
          }
  
          msg_t msg;
          int timeout = 5;
          while (1) {
              mbox_get(&conn->mbox, &msg);
              xtimer_remove(&timer);
              switch (msg.type) {
              case CAN_MSG_TX_ERROR:
                  return -EIO;
              case CAN_MSG_TX_CONFIRMATION:
                  if ((int)msg.content.value == handle) {
                      DEBUG("conn_can_raw_send: frame sent correctly\n");
                      return 0;
                  }
                  else {
                      raw_can_abort(conn->ifnum, handle);
                      return -EINTR;
                  }
                  break;
              case _TIMEOUT_TX_MSG_TYPE:
                  DEBUG("conn_can_raw_send: timeout\n");
                  raw_can_abort(conn->ifnum, handle);
                  return -ETIMEDOUT;
                  break;
              default:
                  DEBUG("conn_can_raw_send: unexpected msg=%x, requeing\n", msg.type);
                  mbox_put(&conn->mbox, &msg);
                  if (!timeout--) {
                      return -EINTR;
                  }
                  xtimer_set(&timer, CONN_CAN_RAW_TIMEOUT_TX_CONF);
                  break;
              }
          }
      }
  
      return ret;
  }
  
  static void _rx_timeout(void *arg)
  {
      conn_can_raw_t *conn = arg;
      msg_t msg;
  
      msg.type = _TIMEOUT_RX_MSG_TYPE;
      msg.content.value = _TIMEOUT_MSG_VALUE;
  
      mbox_put(&conn->mbox, &msg);
  }
  
  int conn_can_raw_recv(conn_can_raw_t *conn, struct can_frame *frame, uint32_t timeout)
  {
      assert(conn != NULL);
      assert(conn->ifnum < CAN_DLL_NUMOF);
      assert(frame != NULL);
  
      xtimer_t timer;
  
      if (timeout != 0) {
          timer.callback = _rx_timeout;
          timer.arg = conn;
          xtimer_set(&timer, timeout);
      }
  
      int ret;
      msg_t msg;
      can_rx_data_t *rx;
  
      mbox_get(&conn->mbox, &msg);
      if (timeout != 0) {
          xtimer_remove(&timer);
      }
      switch (msg.type) {
      case CAN_MSG_RX_INDICATION:
          DEBUG("conn_can_raw_recv: CAN_MSG_RX_INDICATION\n");
          rx = msg.content.ptr;
          memcpy(frame, rx->data.iov_base, rx->data.iov_len);
          ret = rx->data.iov_len;
          raw_can_free_frame(rx);
          break;
      case _TIMEOUT_RX_MSG_TYPE:
          if (msg.content.value == _TIMEOUT_MSG_VALUE) {
              ret = -ETIMEDOUT;
          }
          else {
              ret = -EINTR;
          }
          break;
      case _CLOSE_CONN_MSG_TYPE:
          if (msg.content.ptr == conn) {
              ret = -ECONNABORTED;
          }
          else {
              ret = -EINTR;
          }
          break;
      default:
          mbox_put(&conn->mbox, &msg);
          ret = -EINTR;
          break;
      }
  
      return ret;
  }
  
  int conn_can_raw_close(conn_can_raw_t *conn)
  {
      assert(conn != NULL);
      assert(conn->ifnum < CAN_DLL_NUMOF);
  
      DEBUG("conn_can_raw_close: conn=%p\n", (void *)conn);
  
      if (conn->count) {
          for (size_t i = 0; i < conn->count; i++) {
              DEBUG("conn_can_raw_close: unsetting filter=0x%" PRIx32 ", mask=0x%" PRIx32 "\n",
                   conn->filter[i].can_id, conn->filter[i].can_mask);
              raw_can_unsubscribe_rx_mbox(conn->ifnum, &conn->filter[i], &conn->mbox, conn);
          }
          conn->count = 0;
          msg_t msg;
          while (mbox_try_get(&conn->mbox, &msg)) {
              if (msg.type == CAN_MSG_RX_INDICATION) {
                  DEBUG("conn_can_raw_close: incoming msg pending, freeing\n");
                  raw_can_free_frame(msg.content.ptr);
              }
          }
          msg.type = _CLOSE_CONN_MSG_TYPE;
          msg.content.ptr = conn;
          mbox_try_put(&conn->mbox, &msg);
      }
  
      return 0;
  }