-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacceptance.c
118 lines (102 loc) · 2.25 KB
/
acceptance.c
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
/*
* simple driver test: change the acceptance code and mask with ioctl()
*
*
* first argument can be the device name -- else it uses can0
*
* if a second arg is given, it is used as new acceptance code
* if a third arg is given it is used as new acceptance mask
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "can4linux.h"
#define STDDEV "can0"
/***********************************************************************
*
* set_accmask - sets the CAN acceptance and mask register
*
*
* Changing these registers only possible in Reset mode.
*
* RETURN:
*
*/
int set_accmask(
int fd, /* device descriptor */
int newcode,
int newmask
)
{
Config_par_t cfg;
volatile Command_par_t cmd;
cmd.cmd = CMD_STOP;
ioctl(fd, CAN_IOCTL_COMMAND, &cmd);
cfg.target = CONF_ACC;
cfg.val1 = newmask;
cfg.val2 = newcode;
ioctl(fd, CAN_IOCTL_CONFIG, &cfg);
cmd.cmd = CMD_START;
ioctl(fd, CAN_IOCTL_COMMAND, &cmd);
return 0;
}
int set_acccode(
int fd, /* device descriptor */
int newcode
)
{
Config_par_t cfg;
volatile Command_par_t cmd;
cmd.cmd = CMD_STOP;
ioctl(fd, CAN_IOCTL_COMMAND, &cmd);
cfg.target = CONF_ACCC;
cfg.val1 = newcode;
ioctl(fd, CAN_IOCTL_CONFIG, &cfg);
cmd.cmd = CMD_START;
ioctl(fd, CAN_IOCTL_COMMAND, &cmd);
return 0;
}
int main(int argc,char **argv)
{
int fd;
char device[40];
int newmask = 0;
int newcode = 0;
printf("usage: %s [dev] [acc_code] [acc_mask]\n", argv[0]);
if(argc > 1)
{
sprintf(device, "/dev/%s", argv[1]);
}
else
{
sprintf(device, "/dev/%s", STDDEV);
}
printf("using CAN device %s\n", device);
if(( fd = open(device, O_RDWR )) < 0 )
{
fprintf(stderr,"Error opening CAN device %s\n", device);
exit(1);
}
if(argc == 3)
{
newcode = strtoul(argv[2], NULL, 0);
printf("change acc_code to 0x%x\n", newcode);
set_acccode(fd, newcode);
}
if(argc == 4)
{
newcode = strtoul(argv[2], NULL, 0);
newmask = strtoul(argv[3], NULL, 0);
printf("set acc_code to 0x%x and acc_mask to 0x%x\n",
newcode, newmask);
set_accmask(fd, newcode, newmask);
}
sleep(1);
close(fd);
return 0;
}