summaryrefslogtreecommitdiffstats
path: root/canconfig.c
blob: c10ef135df0f4fff388cdd815933e09cb681e0b9 (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
/***
 *
 *  canconfig.c
 *
 *  Copyright (C) 2005 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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ether.h>

#include "can.h"


#define MIN(a, b) ((a) < (b) ? (a) : (b))

int              s;
struct ifreq	ifr;

struct speed_map {
	unsigned short speed;
	unsigned short value;
};

static const struct speed_map speeds[] = {
	{CAN_BAUD_10K,   10},
	{CAN_BAUD_20K,   20},
	{CAN_BAUD_50K,   50},
	{CAN_BAUD_100K, 100},
	{CAN_BAUD_125K, 125},
	{CAN_BAUD_250K, 250},
	{CAN_BAUD_500K, 500},
	{CAN_BAUD_800K, 800},
	{CAN_BAUD_1M,  1000},
};

static const int NUM_SPEEDS = (sizeof(speeds) / sizeof(struct speed_map));

unsigned long can_baud_to_value(speed_t speed)
{
	int i = 0;

	do {
		if (speed == speeds[i].speed) {
			return speeds[i].value;
		}
	} while (++i < NUM_SPEEDS);

	return 0;
}

speed_t can_value_to_baud(unsigned long value)
{
	int i = 0;

	do {
		if (value == can_baud_to_value(speeds[i].speed)) {
			return speeds[i].speed;
		}
	} while (++i < NUM_SPEEDS);

	return (speed_t)-1;
}


void help(void)
{
    fprintf(stderr, "usage:\n\t"
	    "canconfig <dev> baudrate { BR | BTR }\n\t\t"
	    "BR := { 10 | 20 | 50 | 100 | 125 | 250 | 500 | 800 | 1000 }\n\t\t"
	    "BTR := btr <btr0> [ <btr1> [ <btr2> ] ]\n\t"
	    "canconfig <dev> mode MODE\n\t\t"
	    "MODE := { start }\n\t"
	    "canconfig <dev> state\n"
	    );

    exit(EXIT_FAILURE);
}


void do_show_baudrate(int argc, char* argv[])
{
	struct can_baudrate	*br = (struct can_baudrate *)&ifr.ifr_ifru;
	int			i, value;
	
	i = ioctl(s, SIOCGCANBAUDRATE, &ifr);
	if (i < 0) {
		perror("ioctl");
		exit(EXIT_FAILURE);
	}

	value = can_baud_to_value(br->baudrate);

	if (value != 0)
		fprintf(stdout,
			"%s: baudrate %d btr 0x%02x 0x%02x 0x%02x\n",
			&ifr.ifr_name, value, br->btr[0], br->btr[1], br->btr[2]);
	else
		fprintf(stdout,
			"%s: baudrate <unknown> btr 0x%02x 0x%02x 0x%02x\n",
			&ifr.ifr_name, br->btr[0], br->btr[1], br->btr[2]);
}


void do_set_baudrate(int argc, char* argv[])
{
	struct can_baudrate	*br = (struct can_baudrate *)&ifr.ifr_ifru;
	speed_t			baudrate;
	int			value, i;

	if (!strcmp(argv[3], "btr")) {
		if (argc < 5)
			help();

		br->baudrate = CAN_BAUD_BTR;
		for (i = 4; i < MIN(argc, 4 + 3); i++) {
			value = strtol(argv[i], NULL, 0);
			br->btr[i-4] = value;
		}
	} else {
		value = atoi(argv[3]);
		baudrate = can_value_to_baud(value);
		if ( baudrate == -1) {
			fprintf(stderr, "invalid baudrate\n");
			exit(EXIT_FAILURE);
		}
		br->baudrate = baudrate;
	}

	i = ioctl(s, SIOCSCANBAUDRATE, &ifr);
	if (i < 0) {
		perror("ioctl");
		exit(EXIT_FAILURE);
	}
}


void cmd_baudrate(int argc, char *argv[])
{
	if (argc >= 4) {
		do_set_baudrate(argc, argv);
	}
	do_show_baudrate(argc, argv);

	exit(EXIT_SUCCESS);
}


void cmd_mode(int argc, char *argv[])
{
	union can_settings *settings = (union can_settings *)&ifr.ifr_ifru;
	int i;

	if (argc < 4 )
		help();

	if (!strcmp(argv[3], "start")) {
		settings->mode = CAN_MODE_START;
	} else {
		help();
	}

	i = ioctl(s, SIOCSCANMODE, &ifr);
	if (i < 0) {
		perror("ioctl");
		exit(EXIT_FAILURE);
	}
		
	exit(EXIT_SUCCESS);
}


void do_show_state(int argc, char *argv[])
{
	union can_settings *settings = (union can_settings *)&ifr.ifr_ifru;
	enum CAN_STATE state;
	char *str;
	int i;

	i = ioctl(s, SIOCGCANSTATE, &ifr);
	if (i < 0) {
		perror("ioctl");
		exit(EXIT_FAILURE);
	}

	state = settings->state;
	fprintf(stdout, "%s: state ", &ifr.ifr_name);
	switch (state) {
	case CAN_STATE_BUS_PASSIVE:
		str = "bus passive";
		break;
	case CAN_STATE_ACTIVE:
		str = "active";
		break;
	case CAN_STATE_BUS_OFF:
		str = "bus off";
		break;
	default:
		str = "<unknown>";
	}
	fprintf(stdout, "%s \n", str);

	exit(EXIT_SUCCESS);
}


void cmd_state(int argc, char *argv[])
{
	do_show_state(argc, argv);

	exit(EXIT_SUCCESS);
}


void cmd_show_interface(int argc, char *argv[])
{
	do_show_baudrate(argc, argv);
	do_show_state(argc, argv);

	exit(EXIT_SUCCESS);
}


int main(int argc, char *argv[])
{
	if ((argc < 2) || !strcmp(argv[1], "--help"))
		help();
	
	if ((s = socket(AF_CAN, SOCK_RAW, 0)) < 0) {
		perror("socket");
		exit(EXIT_FAILURE);
	}

	strncpy(ifr.ifr_name, argv[1], IFNAMSIZ);

	if (argc < 3)
		cmd_show_interface(argc, argv);

	if (!strcmp(argv[2], "baudrate"))
		cmd_baudrate(argc, argv);
	if (!strcmp(argv[2], "mode"))
		cmd_mode(argc, argv);
	if (!strcmp(argv[2], "state"))
		cmd_state(argc, argv);

	help();

	return 0;
}