summaryrefslogtreecommitdiffstats
path: root/can_hal/src/driver_can__CWORD31_.c
blob: bd9cb291def0efe1dc6b9229967a9e9acab7f7c1 (plain)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * @copyright Copyright (c) 2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <stdio.h>
#include <string.h>
#include "can_mng_api.h"

#include <poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

#include <linux/can/raw.h>
#include <net/if.h>


#define DUMP_DIR	"/nv/driver-can"
#define DUMP_RCVID	"/nv/driver-can/dump_rcvid"
#define CAN_DATA_SIZE (sizeof(CanData))

static int fd_can = -1;
static struct sockaddr_can tx_address;
static CanCtlRcvId RcvId;
const char* device_name = "vcan0";

static UINT32 getData(CanCtlRcvId*, CanCtlApiCmd*);
static UINT32 getCanData(unsigned char*);
static UINT32 copyEnableCandata(UINT16* , unsigned char*, unsigned char*);
static UINT32 isOpendDriver(void);

UINT32  CanCtlApiOpen(CanCtlApiObj* pClientObj)
{
  int err_no;
  int ret;

  CAN_MNG_API_LOGT("FUNC IN");

  /*----------------------------------------------------------------------*/
  /*  Clear Object                                                        */
  /*----------------------------------------------------------------------*/
  if (pClientObj != NULL) {
    memset(pClientObj, 0, sizeof(*pClientObj));
  }

  if (fd_can != -1) {
    CAN_MNG_API_LOGT("Already Opened");
    CAN_MNG_API_LOGT("FUNC OUT");
    return CAN_CTL_RET_SUCCESS;
  }

  // open socket
  CAN_MNG_API_LOGT("open socket start");
  struct ifreq ifr = {0};
  fd_can = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  err_no = errno;
  CAN_MNG_API_LOGT("open socket end");
  if (fd_can == -1) {
    CAN_MNG_API_LOGE("Socket Open Error.");
    CAN_MNG_API_LOGE("(errno[%d]).", err_no);
    return CAN_CTL_RET_ERR_ERR;
  }

  // ioctl(SIOCGIFINDEX)
  strcpy(ifr.ifr_name, device_name);
  ret = ioctl(fd_can, SIOCGIFINDEX, &ifr);
  err_no = errno;
  if (ret < 0) {
    CAN_MNG_API_LOGE("ioctl(SIOCGIFINDEX) Error.");
    CAN_MNG_API_LOGE("(errno[%d]).", err_no);
    (void)close(fd_can);
    fd_can = -1;
    return CAN_CTL_RET_ERR_ERR;
  }

  // bind
  tx_address.can_family = AF_CAN;
  tx_address.can_ifindex = ifr.ifr_ifindex;

  ret = bind(fd_can, (struct sockaddr *)&tx_address, sizeof(tx_address));
  err_no = errno;
  if (ret < 0) {
    CAN_MNG_API_LOGE("Socket Bind Error.");
    CAN_MNG_API_LOGE("(errno[%d]).", err_no);
    (void)close(fd_can);
    fd_can = -1;
    return -1;
  }

  // Initialize RcvId Map
  memset(&RcvId, 0, sizeof(RcvId));

  CAN_MNG_API_LOGT("FUNC OUT");

  return CAN_CTL_RET_SUCCESS;
}

UINT32  CanCtlApiClose(CanCtlApiObj* pClientObj)
{
  /*----------------------------------------------------------------------*/
  /*  Clear Object                                                        */
  /*----------------------------------------------------------------------*/
  if (pClientObj != NULL) {
    memset(pClientObj, 0, sizeof(*pClientObj));
  }

  if (fd_can != -1) {
    (void)close(fd_can);
  }

  // Initialize fd info
  fd_can = -1;

  CAN_MNG_API_LOGT("FUNC OUT");

  return CAN_CTL_RET_SUCCESS;
}

UINT32  CanCtlApiSndCmd(CanCtlApiObj* pClientObj, CanCtlApiCmd* pSndCmd)
{
  int i;
  printf("%s: call_id=0x%x len=%d\n", 
    __func__, pClientObj->call_id, pSndCmd->len);
  for (i = 0; i < pSndCmd->len; i++) {
    printf("[%d]0x%x ", i, pSndCmd->data[i]);
    if ((i != 0) && ((i % 7) == 0))
      printf("\n"); 
  }
  printf("\n");


  int ret;
  int err_no;

  CAN_MNG_API_LOGT("FUNC IN");
  /*----------------------------------------------------------------------*/
  /*  Check Input Value                                                   */
  /*----------------------------------------------------------------------*/
  if (NULL == pSndCmd)
  {
    CAN_MNG_API_LOGE("%s(%p) invalid parameter.", __func__, pSndCmd);
    return CAN_CTL_RET_ERR_PARAM;
  }

  ret = isOpendDriver();
  if (ret != CAN_CTL_RET_SUCCESS) {
    CAN_MNG_API_LOGE("Not Open Driver.");
    return CAN_CTL_RET_ERR_ERR;
  }

  struct can_frame frame = {0};

  frame.can_id = pSndCmd->data[1] & 0x7F;
  frame.can_id <<= 4;
  frame.can_id |= (pSndCmd->data[2] & 0xF0) >> 4;
  frame.can_dlc = pSndCmd->data[3];
  frame.data[0] = pSndCmd->data[4];
  frame.data[1] = pSndCmd->data[5];
  frame.data[2] = pSndCmd->data[6];
  frame.data[3] = pSndCmd->data[7];
  frame.data[4] = pSndCmd->data[8];
  frame.data[5] = pSndCmd->data[9];
  frame.data[6] = pSndCmd->data[10];
  frame.data[7] = pSndCmd->data[11];

  CAN_MNG_API_LOGT("write() CanDataExtSendNotif start size = %d", (int)sizeof(frame));
  ret = write(fd_can, &frame, sizeof(frame));
  err_no = errno;
  CAN_MNG_API_LOGE("write() ret = %d).", ret);

  CAN_MNG_API_LOGT("write() CanDataExtSendNotif end");
  if (ret == -1) {
    CAN_MNG_API_LOGE("write() error(errno[%d]).", err_no);
    return CAN_CTL_RET_ERR_ERR;
  }

  CAN_MNG_API_LOGT("FUNC OUT");

  return CAN_CTL_RET_SUCCESS;
}

UINT32  CanCtlApiSetRcvId(CanCtlApiObj* pClientObj, CanCtlRcvId* pRcvId)
{
  FILE* fp;
  size_t nmemb;

  CAN_MNG_API_LOGT("FUNC IN");
  /*----------------------------------------------------------------------*/
  /*  Check Input Value                                                   */
  /*----------------------------------------------------------------------*/
  if (NULL == pRcvId)
  {
    CAN_MNG_API_LOGE("%s(%p) invalid parameter.", __func__, pRcvId);
    return CAN_CTL_RET_ERR_PARAM;
  }

  struct stat st;
  int ret = 0;
  if (stat(DUMP_DIR, &st) != 0) {
    ret = mkdir(DUMP_DIR, 0755);
  }
  if (ret != 0) {
    CAN_MNG_API_LOGE("Can not Created RcvId Dump dir.");
    return CAN_CTL_RET_ERR_ERR;
  }
  CAN_MNG_API_LOGT("fopen(DUMP_RCVID) start");
  fp = fopen(DUMP_RCVID, "wb");
  CAN_MNG_API_LOGT("fopen(DUMP_RCVID) end");
  if (fp == NULL) {
    CAN_MNG_API_LOGE("Can not Opened RcvId Dump file.");
    return CAN_CTL_RET_ERR_ERR;
  }

  CAN_MNG_API_LOGT("fwrite(pRcvId) start");
  nmemb = fwrite(pRcvId, sizeof(CanCtlRcvId), 1, fp);
  CAN_MNG_API_LOGT("fwrite(pRcvId) end");
  if (nmemb != 1) {
    CAN_MNG_API_LOGE("RcvId Dump file write error.");
    (void)fclose(fp);
    return CAN_CTL_RET_ERR_ERR;
  }

  CAN_MNG_API_LOGT("fclose() start");
  (void)fclose(fp);
  CAN_MNG_API_LOGT("fclose() end");

  CAN_MNG_API_LOGT("FUNC OUT");

  return CAN_CTL_RET_SUCCESS;
}

UINT32  CanCtlApiRcvCmd(CanCtlApiObj* pClientObj, CanCtlApiCmd* pRcvCmd)
{
  int ret;
  int enable_ret;

  CanCtlRcvId InitialRcvId;
  CanCtlRcvId RcvId;
  FILE* fp;
  size_t nmemb;

  CAN_MNG_API_LOGT("FUNC IN");
  /*----------------------------------------------------------------------*/
  /*  Check Input Value                                                   */
  /*----------------------------------------------------------------------*/
  if (NULL == pRcvCmd)
  {
    CAN_MNG_API_LOGE("%s(%p) invalid parameter.", __func__, pRcvCmd);
    return CAN_CTL_RET_ERR_PARAM;
  }

  ret = isOpendDriver();
  if (ret != CAN_CTL_RET_SUCCESS) {
    CAN_MNG_API_LOGE("Not Open Driver.");
    return CAN_CTL_RET_ERR_ERR;
  }

  // Restore RcvId
  CAN_MNG_API_LOGT("fopen(DUMP_RCVID) start");
  fp = fopen(DUMP_RCVID, "rb");
  CAN_MNG_API_LOGT("fopen(DUMP_RCVID) end");
  if (fp == NULL) {
    CAN_MNG_API_LOGE("Can not Opened RcvId Dump file.");
    return CAN_CTL_RET_ERR_ERR;
  }

  CAN_MNG_API_LOGT("fread(RcvId) start");
  nmemb = fread(&RcvId, sizeof(CanCtlRcvId), 1, fp);
  CAN_MNG_API_LOGT("fread(RcvId) end");
  if (nmemb != 1) {
    CAN_MNG_API_LOGE("RcvId Dump file read error.");
    (void)fclose(fp);
    return CAN_CTL_RET_ERR_ERR;
  }

  CAN_MNG_API_LOGT("fclose() start");
  (void)fclose(fp);
  CAN_MNG_API_LOGT("fclose() end");

  memset(&InitialRcvId.id, 0, sizeof(InitialRcvId.id));

  if (memcmp(InitialRcvId.id, RcvId.id, sizeof(RcvId.id)) == 0) {
    CAN_MNG_API_LOGE("No RecvId Maps.");
    return CAN_CTL_RET_ERR_ERR;
  }

  enable_ret = getData(&RcvId, pRcvCmd);

  if (enable_ret != CAN_CTL_RET_SUCCESS) {
    CAN_MNG_API_LOGE("No data founds.");
    return enable_ret;
  }

  CAN_MNG_API_LOGT("FUNC OUT");

  return CAN_CTL_RET_SUCCESS;
}

static UINT32 getData(CanCtlRcvId* pRcvId, CanCtlApiCmd* pRcvCmd) {
  int ret;
  int total_cnt = 0;
  int copy_cnt = 0;
  int i;
  unsigned char can_data[CAN_CTL_CMD_LEN_MAX+1];
  unsigned char enable_can_data[CAN_CTL_CMD_LEN_MAX];
  unsigned char store_can_data[CAN_CTL_CMD_LEN_MAX];
  unsigned char* scd;


  UINT16 id_map[CAN_CTL_CMD_ID_HI_NUM];

  CAN_MNG_API_LOGT("FUNC IN");

  memcpy(id_map, pRcvId->id, sizeof(id_map));

  memset(store_can_data, 0, sizeof(store_can_data));
  scd = store_can_data;

  while(1) {
    memset(can_data, 0, sizeof(can_data));
    memset(enable_can_data, 0, sizeof(enable_can_data));
    ret = getCanData(can_data);
    if (ret != CAN_CTL_RET_SUCCESS) {
      // In case of error, read next data
      CAN_MNG_API_LOGE("getCanData error.");
      continue;
    }

    // Extract data of "CAN data extended reception notification 4"
    copy_cnt = copyEnableCandata(id_map, can_data, enable_can_data);
    if( copy_cnt == 0 ){
      continue;
    }

    // Store in work buffer
    for (i = 0; i < copy_cnt; i++) {
      if (total_cnt >= CAN_CTL_MAX_RCV_CAN_SIZE) {
        CAN_MNG_API_LOGE("buffer over");
        break;
      }
      memcpy(scd, &enable_can_data[CAN_DATA_SIZE * i], CAN_DATA_SIZE);
      scd = scd + CAN_DATA_SIZE;
      total_cnt++;
    }

    // Check work buffer overflow
    if (total_cnt >= CAN_CTL_MAX_RCV_CAN_SIZE) {
      CAN_MNG_API_LOGE("buffer over");
      break;
    }
  }

  // If data exists, set and return
  if (total_cnt != 0) {
    pRcvCmd->len = 1 + (total_cnt * CAN_DATA_SIZE);
    pRcvCmd->data[0] = total_cnt;
    memcpy( &(pRcvCmd->data[1]), store_can_data, (total_cnt * CAN_DATA_SIZE));
  } else {
  // If there is no data, return with length 0
    pRcvCmd->len = 0;
    memset( &(pRcvCmd->data[0]), 0, sizeof(UINT8) * CAN_DAT_LEN_MAX );
    CAN_MNG_API_LOGE("data not found");
  }

  CAN_MNG_API_LOGT("FUNC OUT");

  return CAN_CTL_RET_SUCCESS;
}

static UINT32 getCanData(unsigned char *can_data) {
  int err_no;
  int i;

  CAN_MNG_API_LOGT("FUNC IN");

  CAN_MNG_API_LOGE("recvfrom start");

  int nbytes = 0;
  struct can_frame frame = {0};
  socklen_t addrlen = sizeof(struct sockaddr_can);
  nbytes = recvfrom(fd_can, &frame, sizeof(frame), 0, (struct sockaddr*)&tx_address, &addrlen);
  err_no = errno;
  CAN_MNG_API_LOGE("recvfrom end ret = %d",nbytes);
  if (nbytes == -1) {
    CAN_MNG_API_LOGE("Not Read CAN Driver(errno[%d]).", err_no);
    return CAN_CTL_RET_ERR_ERR;
  } else if (nbytes != CAN_MTU) {
    CAN_MNG_API_LOGE("Receive Error size: %d.", nbytes);
    return CAN_CTL_RET_ERR_ERR;
  }

  CAN_MNG_API_LOGT("Recvfrom CAN Data(start)");
  can_data[0] = (frame.can_id & 0x7F0) >> 4;
  can_data[1] = (frame.can_id & 0x0F) << 4;
  can_data[2] = frame.can_dlc;
  for (i = 0; i < frame.can_dlc; i++) {
    can_data[3+i] = frame.data[i];
    CAN_MNG_API_LOGE(" 0x%02x", frame.data[i]);
  }
  CAN_MNG_API_LOGT("");
  CAN_MNG_API_LOGT("Recvfrom Data(end)");

  CAN_MNG_API_LOGT("FUNC OUT");
  return CAN_CTL_RET_SUCCESS;
}

static UINT32 copyEnableCandata(UINT16* id_map, unsigned char* can_data, unsigned char* enable_can_data) {
  int id;
  UINT16 val;
  unsigned char* ecd = enable_can_data;
  UINT32 count = 0;

  CAN_MNG_API_LOGT("FUNC IN");
  // Search data of "CAN data extended reception notification 4"
  id  = can_data[0];
  val = can_data[1] >> 4;  // Shift right 4 bits and determine search position

  CAN_MNG_API_LOGE("data id =%x", id);
  CAN_MNG_API_LOGE("data val=%x", val);
  CAN_MNG_API_LOGE("file val=%x", id_map[id]);

  // If the target data is found (If the bit is on ?)
  if ((id_map[id] & (0x01 << val)) != 0) {
    CAN_MNG_API_LOGT("matched.");
    CAN_MNG_API_LOGE("id_map[%u]", id_map[id]);

    // Store data in work buffer
    memcpy(ecd, &can_data[0], CAN_DATA_SIZE);
    count++;
  }

  CAN_MNG_API_LOGT("FUNC OUT");
  return count;
}

UINT32  CanCtlApiRcvSpd(CanCtlApiObj* pClientObj, CanCtlApiCmd* pRcvCmd)
{
  // Note.
  // If vendor needs the special implementation about receiving the vehicle speed,
  // it should be implemented by vendor.
  return CAN_CTL_RET_SUCCESS;
}

static UINT32 isOpendDriver() {
  CAN_MNG_API_LOGT("FUNC IN");

  if (fd_can == -1) {
      CAN_MNG_API_LOGE("Not Open CAN Driver.");
      return CAN_CTL_RET_ERR_ERR;
  }

  CAN_MNG_API_LOGT("FUNC OUT");
  return CAN_CTL_RET_SUCCESS;
}