summaryrefslogtreecommitdiffstats
path: root/src/canconfig.c
blob: 9c9d4ae0106aec245e089a0ba239d26450c40e60 (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
/*
 * canutils/canconfig.c
 *
 * Copyright (C) 2005 Marc Kleine-Budde <mkl@pengutronix.de>, Pengutronix
 * Copyright (C) 2007 Juergen Beisert <jbe@pengutronix.de>, Pengutronix
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the version 2 of the GNU General Public License 
 * as published by the Free Software Foundation
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <string.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 <linux/can.h>
#include <linux/can/ioctl.h>

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

#define kHz 1000

#define CONFIG_FILE_NAME "/etc/canconfig.conf"
#define CONFIG_FILE_NAME_SP "/etc/canconfig-%s.conf"

int             s;
struct ifreq	ifr;

static void help(void)
{
	fprintf(stderr, "usage:\n\t"
		"canconfig <dev> baudrate { BR }\n\t\t"
		"BR := <baudrate>\n\t\t"
		"canconfig <dev> mode MODE\n\t\t"
		"MODE := { start }\n\t"
		"canconfig <dev> tweak [ VALs ]\n\t\t"
		"VALs := <TBR TQ ERR PPS PHS1 PHS2 SJW SAM>\n\t\t"
		" TBR bit rate to be tweaked in Hz\n\t\t"
		" TQ time quantum in ps\n\t\t"
		" ERR max. allowed error in pps\n\t\t"
		" PPS Prop_Seg in tq\n\t\t"
		" PHS1 Phase_Seg1 in tq\n\t\t"
		" PHS2 Phase_Seg2 in tq\n\t\t"
		" SJW in tq\n\t\t"
		" SAM 1 for 3 times sampling, 0 else\n\t\t"
		" without VALSs " CONFIG_FILE_NAME " will be read\n"
		);

	exit(EXIT_FAILURE);
}

static void do_show_baudrate(int argc, char* argv[])
{
	uint32_t *baudrate = (uint32_t *)&ifr.ifr_ifru;
	int i;
	
	i = ioctl(s, SIOCGCANBAUDRATE, &ifr);

	if (i < 0) {
		perror("ioctl");
		exit(EXIT_FAILURE);
	}

	if (*baudrate != -1)
		fprintf(stdout,
			"%s: baudrate %d\n", ifr.ifr_name, *baudrate / kHz);
	else 
		fprintf(stdout,
			"%s: baudrate unknown\n", ifr.ifr_name);

}


static void do_set_baudrate(int argc, char* argv[])
{
	uint32_t *baudrate = (uint32_t *)&ifr.ifr_ifru;
	int i;

	*baudrate = (uint32_t)strtoul(argv[3], NULL, 0) * kHz;
	if (*baudrate == 0) {
		fprintf(stderr, "invalid baudrate\n");
		exit(EXIT_FAILURE);
	}

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


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

	exit(EXIT_SUCCESS);
}

static void cmd_mode(int argc, char *argv[])
{
	can_mode_t *mode = (can_mode_t *)&ifr.ifr_ifru;
	int i;

	if (argc < 4 )
		help();

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

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

	exit(EXIT_SUCCESS);
}

static void tweak_a_bitrate(uint32_t bit_rate, uint32_t tq, uint32_t err,
		uint32_t prop_seg, uint32_t phase_seg1, uint32_t phase_seg2,
		uint32_t sjw, int sam)
{
	struct can_bit_time_custom *bit_time = (struct can_bit_time_custom *)&ifr.ifr_ifru;
	int i;

	if (sizeof(struct can_bit_time_custom) > sizeof(ifr.ifr_ifru)) {
		printf("Error can_bit_time_custom to large! (%d, %d)",
			sizeof(struct can_bit_time_custom), sizeof(ifr.ifr_ifru));
		exit(EXIT_FAILURE);
	}

	bit_time->bit_rate = bit_rate;
	bit_time->tq = tq;
	bit_time->bit_error = err;
	bit_time->prop_seg = prop_seg;
	bit_time->phase_seg1 = phase_seg1;
	bit_time->phase_seg2 = phase_seg2;
	bit_time->sjw = sjw;
	bit_time->sam = sam;

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

static void tweak_from_command_line(int argc, char *argv[])
{
	uint32_t bit_rate;
	uint32_t tq;
	uint32_t err;
	uint32_t prop_seg;
	uint32_t phase_seg1;
	uint32_t phase_seg2;
	uint32_t sjw;
	int sam;

	if (argc < 9)
		help();

	bit_rate = (uint32_t)strtoul(argv[3], NULL, 0);
	tq = (uint32_t)strtoul(argv[4], NULL, 0);
	err = (uint32_t)strtoul(argv[5], NULL, 0);
	prop_seg = (uint32_t)strtoul(argv[6], NULL, 0);
	phase_seg1 = (uint32_t)strtoul(argv[7], NULL, 0);
	phase_seg2 = (uint32_t)strtoul(argv[8], NULL, 0);
	sjw = (uint32_t)strtoul(argv[9], NULL, 0);
	sam = (int)strtoul(argv[10], NULL, 0);

	tweak_a_bitrate(bit_rate, tq, err, prop_seg, phase_seg1, phase_seg2, sjw, sam);

	exit(EXIT_SUCCESS);
}

static void tweak_from_config_file(int argc, char *argv[])
{
	FILE *config_file;
	char file_name[PATH_MAX];
	int first_char,cnt,line = 1;
	uint32_t bit_rate;
	uint32_t tq;
	uint32_t err;
	uint32_t prop_seg;
	uint32_t phase_seg1;
	uint32_t phase_seg2;
	uint32_t sjw;
	int sam;

	sprintf(file_name, CONFIG_FILE_NAME_SP, argv[1]);
	config_file = fopen(file_name, "r");
	if (config_file == NULL) {
		sprintf(file_name, CONFIG_FILE_NAME);
		config_file = fopen(file_name, "r");
		if (config_file == NULL) {
			perror("Config file (" CONFIG_FILE_NAME ")");
			exit(EXIT_FAILURE);
		}
	}

	while(!feof(config_file)) {
		first_char = fgetc(config_file);
		printf("First char in line %d is: %02x (%c)\n", line, first_char, first_char);
		if (first_char == EOF)
			break;
		if (first_char == '#') {
			printf("eating line %d ", line);
			cnt=fscanf(config_file,"%*[^\n]");	/* eat the whole line */
			printf("-> %d\n", cnt);
			fgetc(config_file); /* FIXME: eat the \n */
			line ++;
			continue;
		}
		else
			ungetc(first_char, config_file);

		cnt = fscanf(config_file,"%u %u %u %u %u %u %u %d\n", &bit_rate,
			&tq, &err, &prop_seg, &phase_seg1, &phase_seg2,
			&sjw, &sam);
		if (cnt != 6) {
			fprintf(stderr,"Wrong data format in line %d in %s\n",
				line, file_name);
			exit(EXIT_FAILURE);
		}
		line ++;
		tweak_a_bitrate(bit_rate, tq, err, prop_seg, phase_seg1,
				phase_seg2, sjw, sam);
	}

	fclose(config_file);

	exit(EXIT_SUCCESS);
}

static void cmd_tweak(int argc, char *argv[])
{
	if (argc < 4)
		tweak_from_config_file(argc, argv);
	else {
		if (argc < 9)
			help();
		tweak_from_command_line(argc, argv);
	}
}

#if 0
static 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);
}


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

	exit(EXIT_SUCCESS);
}
#endif


static 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, CAN_RAW)) < 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], "tweak"))
		cmd_tweak(argc, argv);
	
/* 	if (!strcmp(argv[2], "state")) */
/* 		cmd_state(argc, argv); */

	help();

	exit(EXIT_SUCCESS);
}