File: can.c

package info (click to toggle)
microcom 2019.01.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 580 kB
  • sloc: sh: 4,121; ansic: 2,101; makefile: 13
file content (197 lines) | stat: -rw-r--r-- 3,986 bytes parent folder | download
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
/*
 * Description: the can part for microcom project
 *
 * Copyright (C) 2010 by Marc Kleine-Budde <mkl@pengutronix.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details at www.gnu.org
 *
 */

#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <unistd.h>

#include <net/if.h>

#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>

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

#include "microcom.h"

struct can_data {
	int can_id;
};

static struct can_data data;

static ssize_t can_write(struct ios_ops *ios, const void *buf, size_t count)
{
	size_t loopcount;
	ssize_t ret = 0, err;

	struct can_frame to_can = {
		.can_id = data.can_id,
	};

	while (count > 0) {
		loopcount = min(count, sizeof(to_can.data));
		memcpy(to_can.data, buf, loopcount);
		to_can.can_dlc = loopcount;
retry:
		err = write(ios->fd, &to_can, sizeof(to_can));
		if (err < 0 && errno == EINTR)
			goto retry;

		if (err < 0)
			return err;

		assert(err == sizeof(to_can));
		buf += loopcount;
		count -= loopcount;
		ret += loopcount;
	}

	return ret;
}

static ssize_t can_read(struct ios_ops *ios, void *buf, size_t count)
{
	struct can_frame from_can;
	ssize_t ret;

retry:
	ret = read(ios->fd, &from_can, sizeof(from_can));
	if (ret < 0 && errno != EINTR)
		goto retry;

	if (ret < 0)
		return ret;

	assert(count >= from_can.can_dlc);
	memcpy(buf, from_can.data, from_can.can_dlc);

	return from_can.can_dlc;
}

static int can_set_speed(struct ios_ops *ios, unsigned long speed)
{
	return 0;
}

static int can_set_flow(struct ios_ops *ios, int flow)
{
	return 0;
}

static int can_send_break(struct ios_ops *ios)
{
	return 0;
}

static void can_exit(struct ios_ops *ios)
{
	close(ios->fd);
	free(ios);
}

struct ios_ops *can_init(char *interface_id)
{
	struct ios_ops *ios;
	struct ifreq ifr;
	struct can_filter filter[] = {
		{
			.can_mask = CAN_SFF_MASK,
		},
	};
	struct sockaddr_can addr = {
		.can_family = PF_CAN,
	};
	char *interface = interface_id;
	char *id_str = NULL;

	ios = malloc(sizeof(*ios));
	if (!ios)
		return NULL;

	ios->write = can_write;
	ios->read = can_read;
	ios->set_speed = can_set_speed;
	ios->set_flow = can_set_flow;
	ios->send_break = can_send_break;
	ios->exit = can_exit;
	ios->istelnet = false;

	/*
	 * the string is supposed to be formated this way:
	 * interface:rx:tx
	 */
	if (interface_id)
		id_str = strchr(interface, ':');

	if (id_str) {
		*id_str = 0x0;
		id_str++;
		filter->can_id = strtol(id_str, NULL, 16) & CAN_SFF_MASK;

		id_str = strchr(id_str, ':');
	} else {
		filter->can_id = DEFAULT_CAN_ID;
	}

	if (id_str) {
		*id_str = 0x0;
		id_str++;
		data.can_id = strtol(id_str, NULL, 16) & CAN_SFF_MASK;
	} else {
		data.can_id = filter->can_id;
	}

	if (!interface || *interface == 0x0)
		interface = DEFAULT_CAN_INTERFACE;

	/* no cleanups on failure, we exit anyway */

	ios->fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
	if (ios->fd < 0) {
		perror("socket");
		return NULL;
	}

	if (setsockopt(ios->fd, SOL_CAN_RAW, CAN_RAW_FILTER,
		       filter, sizeof(filter))) {
		perror("setsockopt");
		return NULL;
	}

	strcpy(ifr.ifr_name, interface);
	if (ioctl(ios->fd, SIOCGIFINDEX, &ifr)) {
		printf("%s: %s\n", interface, strerror(errno));
		return NULL;
	}
	addr.can_ifindex = ifr.ifr_ifindex;

	if (bind(ios->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
		perror("bind");
		return NULL;
	}

	printf("connected to %s (rx_id=%x, tx_id=%x)\n",
	       interface, filter->can_id, data.can_id);

	return ios;
}