This repository has been archived by the owner on Dec 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
toyothack.c
249 lines (219 loc) · 4.8 KB
/
toyothack.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright 2013 Fabio Baltieri <[email protected]>
*
* 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 3 of the License, or
* (at your option) any later version.
*/
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <curses.h>
#include <endian.h>
#define __packed __attribute__((packed))
static int sk;
enum frame_ids {
TOY_WHEEL_SPEED_A = 0x0b0,
TOY_WHEEL_SPEED_B = 0x0b2,
TOY_UNK_B4 = 0x0b4,
TOY_BRAKE = 0x224,
TOY_THROTTLE = 0x2c1,
TOY_ENGINE = 0x2c4,
TOY_FUEL_USAGE = 0x398,
};
#define UNKNOWN_COUNT 1024
static int unknown[UNKNOWN_COUNT];
union toyoframe {
struct __packed {
uint16_t a;
uint16_t b;
uint8_t flags;
uint8_t seq;
} wheel_speed;
struct __packed {
uint32_t _pad;
uint8_t distance_a;
uint16_t speed;
uint8_t distance_b;
} unkb4;
struct __packed {
uint8_t flags;
uint8_t _pad0;
uint8_t _pad1;
uint8_t _pad2;
uint8_t _pad3;
uint8_t _pad4;
uint8_t _pad5;
uint8_t _pad6;
} brake;
struct __packed {
uint8_t flags0;
int16_t unk0;
int16_t unk1;
uint8_t unk2;
int16_t throttle;
} throttle;
struct __packed {
uint16_t rpm;
uint8_t _pad0;
uint8_t unk0;
uint8_t _pad1;
uint8_t _pad2;
uint8_t unk1;
int8_t unk2;
} engine;
struct __packed {
int16_t fuel_usage;
} fuel_usage;
};
static void unknown_frame(int id)
{
int i;
for (i = 0; i < UNKNOWN_COUNT; i++)
if (unknown[i] == 0 || unknown[i] == id)
break;
if (i == UNKNOWN_COUNT)
return;
unknown[i] = id;
move(LINES - 3, 1);
clrtoeol();
mvprintw(LINES - 3, 1, "unk:");
for (i = 0; i < UNKNOWN_COUNT; i++) {
if (unknown[i] == 0)
break;
printw(" %02x", unknown[i]);
}
printw(" (%d)", i);
}
static void process_one(struct can_frame *frm)
{
int i;
union toyoframe *toy;
toy = (union toyoframe *)frm->data;
switch (frm->can_id) {
case TOY_WHEEL_SPEED_A:
case TOY_WHEEL_SPEED_B:
i = (frm->can_id == TOY_WHEEL_SPEED_A) ? 1 : 2;
move(i, 1);
clrtoeol();
mvprintw(i, 1, "wheel: a=%5d b=%5d (delta=%5d) flags=%02x seq=%02x",
be16toh(toy->wheel_speed.a),
be16toh(toy->wheel_speed.b),
be16toh(toy->wheel_speed.a) - be16toh(toy->wheel_speed.b),
toy->wheel_speed.flags,
toy->wheel_speed.seq);
break;
case TOY_UNK_B4:
move(3, 1);
clrtoeol();
mvprintw(3, 1, "unk_b4: distance_a=%3d speed=%5d distance_b=%3d",
toy->unkb4.distance_a,
be16toh(toy->unkb4.speed),
toy->unkb4.distance_b
);
break;
break;
case TOY_BRAKE:
move(4, 1);
clrtoeol();
mvprintw(4, 1, "brake: flags=%02x [%s]",
toy->brake.flags,
(toy->brake.flags) ? "ON" : " ");
break;
case TOY_THROTTLE:
move(5, 1);
clrtoeol();
mvprintw(5, 1, "throttle: flags0=%02x unk0=%5hd unk1=%5hd, unk2=%03hhd throttle=%4hu",
toy->throttle.flags0, /* bit 3: engine break? */
be16toh(toy->throttle.unk0),
be16toh(toy->throttle.unk1),
toy->throttle.unk2,
be16toh(toy->throttle.throttle)
);
break;
case TOY_ENGINE:
move(6, 1);
clrtoeol();
mvprintw(6, 1, "engine: rpm=%5hd unk0=%3d unk1=%3d, unk2=%3hhd",
be16toh(toy->engine.rpm),
toy->engine.unk0,
toy->engine.unk1,
toy->engine.unk2
);
break;
case TOY_FUEL_USAGE:
move(7, 1);
clrtoeol();
mvprintw(7, 1, "fuel_usage: %5hd",
be16toh(toy->fuel_usage.fuel_usage));
break;
default:
unknown_frame(frm->can_id);
}
refresh();
}
static int net_init(char *ifname)
{
int recv_own_msgs;
struct sockaddr_can addr;
struct ifreq ifr;
sk = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (sk < 0) {
perror("socket");
exit(1);
}
memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if (ioctl(sk, SIOCGIFINDEX, &ifr) < 0) {
perror("SIOCGIFINDEX");
exit(1);
}
memset(&addr, 0, sizeof(addr));
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
if (bind(sk, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("bind");
return 1;
}
recv_own_msgs = 0; /* 0 = disabled (default), 1 = enabled */
setsockopt(sk, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
&recv_own_msgs, sizeof(recv_own_msgs));
return 0;
}
static void receive_one(void)
{
struct can_frame frm;
struct sockaddr_can addr;
int ret;
socklen_t len;
ret = recvfrom(sk, &frm, sizeof(struct can_frame), 0,
(struct sockaddr *)&addr, &len);
if (ret < 0) {
perror("recvfrom");
exit(1);
}
process_one(&frm);
}
int main(int argc, char **argv)
{
if (argc != 2) {
printf("syntax: %s IFNAME\n", argv[0]);
exit(1);
}
memset(unknown, 0, sizeof(unknown));
initscr();
net_init(argv[1]);
for (;;)
receive_one();
endwin();
return 0;
}