-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcannode_sock.c
More file actions
157 lines (127 loc) · 4.06 KB
/
Copy pathcannode_sock.c
File metadata and controls
157 lines (127 loc) · 4.06 KB
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
/****************************************************************************
* apps/mini_cannode/cannode_sock.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <nuttx/can.h>
#include <nuttx/can/can.h>
#include "cannode.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: can_init
*
* Description:
* Initialzie CAN socket device interface.
*
****************************************************************************/
int can_init(void)
{
struct sockaddr_can addr;
struct ifreq req;
int s;
int ret;
/* Create sockete */
s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (s < 0)
{
PRINTF("failed to open sock %d\n", -errno);
return -errno;
}
/* Perform the ioctl to bring up network interface */
memset(&req, 0, sizeof(struct ifreq));
strlcpy(req.ifr_name, "can0", IFNAMSIZ);
req.ifr_flags |= IFF_UP;
ret = ioctl(s, SIOCSIFFLAGS, (unsigned long)&req);
/* Bind socket */
memset(&addr, 0, sizeof(addr));
addr.can_family = AF_CAN;
addr.can_ifindex = 1;
ret = bind(s, (struct sockaddr *)&addr, sizeof(addr));
if (ret < 0)
{
PRINTF("bind failed %d\n", -errno);
return -errno;
}
return s;
}
/****************************************************************************
* Name: can_read
*
* Description:
* Read CAN data (blocking).
*
****************************************************************************/
int can_read(int fd, FAR struct canmsg_s *msg)
{
struct can_frame frame;
int ret;
ret = read(fd, &frame, sizeof(struct can_frame));
if (ret < 0)
{
PRINTF("recvmsg failed %d\n", -errno);
return -errno;
}
msg->id = frame.can_id;
msg->len = can_dlc2bytes(frame.can_dlc);
memcpy(msg->data, frame.data, CAN_DATA_MAX);
return ret;
}
/****************************************************************************
* Name: can_send
*
* Description:
* Send CAN data.
*
****************************************************************************/
int can_send(int fd, FAR struct canmsg_s *msg)
{
struct can_frame frame;
int ret;
/* Convert to SocketCAN frame */
frame.can_id = msg->id;
#ifdef CONFIG_NET_CAN_EXTID
frame.can_id |= CAN_EFF_FLAG;
#endif
frame.can_dlc = can_bytes2dlc(msg->len);
memcpy(frame.data, msg->data, msg->len);
/* Send frame */
ret = write(fd, &frame, CAN_MTU);
if (ret < 0)
{
PRINTF("write failed %d\n", -errno);
return -errno;
}
else if (ret != CAN_MTU)
{
PRINTF("write failed to send\n");
return -EIO;
}
return ret;
}