-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmds-base.c
230 lines (181 loc) · 3.74 KB
/
cmds-base.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <libmicro/can.h>
#include <libmicro/debug.h>
#include <libmicro/lap.h>
#include "cmds-base.h"
void cmd_loopback(int argc, char *argv[])
{
int mode;
if (argc != 2) goto argerror;
if (sscanf(argv[1], "%i", &mode) != 1)
goto argerror;
if (mode)
{
printf("Activating loopack mode\n");
can_setmode(loopback);
} else {
printf("Deactivating loopack mode\n");
can_setmode(normal);
}
return;
argerror:
debug(0, "loopback [0|1]");
}
void cmd_ping(int argc, char *argv[])
{
int addr;
can_message *msg;
if (argc != 2) goto argerror;
if (sscanf(argv[1], "%i", &addr) != 1)
goto argerror;
lap_ping(addr);
for (;;)
{
msg = can_get();
if ((msg != NULL) && msg->addr_src == addr && msg->port_src == PORT_MGT && msg->data[0] == FKT_MGT_PONG)
{
printf("Pong from 0x%x\n", addr);
free(msg);
return;
}
}
return;
argerror:
debug(0, "ping <addr>");
}
void cmd_reset(int argc, char *argv[])
{
int addr;
if (argc != 2) goto argerror;
if (sscanf(argv[1], "%i", &addr) != 1)
goto argerror;
lap_reset(addr);
return;
argerror:
debug(0, "reset <addr>");
}
void hexdump(unsigned char *addr, int size)
{
unsigned char x = 0;
while (size--)
{
printf("%02x ", *addr++);
if (++x == 16)
{
printf("\n");
x = 0;
}
}
}
void dump_packet_v2(can_message_v2 *msg)
{
time_t muh = time(0);
struct tm *tme = localtime(&muh);
printf( "%02d:%02d.%02d: %03x,%x: %02x -> %02x ",
tme->tm_hour, tme->tm_min, tme->tm_sec,
msg->channel, msg->subchannel,
msg->addr_src, msg->addr_dst );
hexdump(msg->data, msg->dlc);
printf("\n");
}
void dump_packet(can_message *msg)
{
time_t muh = time(0);
struct tm *tme = localtime(&muh);
printf( "%02d:%02d.%02d: %02x:%02x -> %02x:%02x ",
tme->tm_hour, tme->tm_min, tme->tm_sec,
msg->addr_src, msg->port_src,
msg->addr_dst, msg->port_dst );
hexdump(msg->data, msg->dlc);
printf("\n");
}
void cmd_dump(int argc, char *argv[])
{
if (argc > 1)
{
can_message_v2 *msg;
while (1)
{
msg = can_get_v2_nb();
if (msg)
{
dump_packet_v2(msg);
can_free_v2(msg);
}
usleep(100);
}
} else
{
can_message *msg;
while (1)
{
msg = can_get();
if (msg)
{
dump_packet(msg);
can_free(msg);
}
}
}
}
void cmd_packet(int argc, char *argv[])
{
int src_addr, src_port;
int dst_addr, dst_port;
int i;
char *tok;
can_message *msg;
if (argc != 4)
goto argerror;
if (sscanf(argv[1], "%i:%i", &src_addr, &src_port ) != 2)
goto argerror;
if (sscanf(argv[2], "%i:%i", &dst_addr, &dst_port ) != 2)
goto argerror;
msg = can_buffer_get();
msg->addr_src = src_addr;
msg->addr_dst = dst_addr;
msg->port_src = src_port;
msg->port_dst = dst_port;
msg->dlc = 0;
tok = strtok( argv[3], ",");
while (tok)
{
if (msg->dlc >= 8 ) goto argerror;
if (sscanf(tok, "%i", &i) != 1) goto argerror;
msg->data[msg->dlc++] = i;
tok = strtok(NULL, ",");
}
printf("%02x:%02x -> %02x:%02x (length=%d)\n", src_addr, src_port, dst_addr, dst_port, msg->dlc);
can_transmit(msg);
return;
argerror:
debug(0, "packet <src-addr>:<src-port> <dst-addr>:<dst-port> <data>,<data>,....");
}
void cmd_lamp(int argc,char *argv[])
{
if (argc != 4)
goto argerror;
int dst;
int lamp;
int value;
sscanf(argv[1],"%x",&dst);
sscanf(argv[2],"%x",&lamp);
sscanf(argv[3],"%x",&value);
pdo_message * msg = (pdo_message *) can_buffer_get();
msg->port_dst = PORT_LAMPE;
msg->port_src = PORT_MGT;
msg->addr_dst = (can_addr)dst;
msg->addr_src = 0x00;
msg->dlc = 0x03;
msg->cmd = FKT_LAMPE_SET;
msg->data[0] = lamp;
msg->data[1] = value;
can_transmit((can_message *)msg);
return;
argerror:
debug(0, "lamp <addr> <lamp> <value>");
}