-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigtest.c
207 lines (171 loc) · 6.1 KB
/
bigtest.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
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <assert.h>
#include <can_ids.h>
#include <can_parser.h>
uint32_t timeout = 0;
struct test_mic_t
{
uint8_t signature; // Senders signature. Units:
struct
{ // Motor state
uint8_t motor_on : 1;
uint8_t dms_on : 1;
uint8_t reverse : 1;
uint8_t _unused : 5;
} motor;
uint8_t d; // Motor Duty Cycle. Units: %
uint8_t i; // Motor Soft Start. Units: %
};
struct test_mic_t test_mic;
static void parse_mic_state(can_msg_t *msg)
{
printf("mic state\n");
}
static void parse_mic_motor(can_msg_t *msg)
{
can_mic19_motor_msg_t *mic_motor = (can_mic19_motor_msg_t *)msg->raw;
test_mic.signature = mic_motor->signature;
test_mic.motor.motor_on = mic_motor->motor.motor_on;
test_mic.motor.dms_on = mic_motor->motor.dms_on;
test_mic.motor.reverse = mic_motor->motor.reverse;
test_mic.d = mic_motor->d;
test_mic.i = mic_motor->i;
}
static void parse_mswi_state(can_msg_t *msg)
{
printf("mswi state\n");
}
struct test_mswi_t
{
uint8_t signature; // Senders signature. Units:
struct
{ // Motor state
uint8_t motor_on : 1;
uint8_t dms_on : 1;
uint8_t _unused : 6;
} motor;
uint8_t d; // Motor Duty Cycle. Units: %
uint8_t i; // Motor Soft Start. Units: %
} test_mswi;
static void parse_mswi_motor(can_msg_t *msg)
{
can_mswi19_motor_msg_t *mswi_motor = (can_mswi19_motor_msg_t *)msg->raw;
test_mswi.signature = mswi_motor->signature;
test_mswi.d = mswi_motor->d;
test_mswi.i = mswi_motor->i;
test_mswi.motor.dms_on = mswi_motor->motor.dms_on;
test_mswi.motor.motor_on = mswi_motor->motor.motor_on;
}
CAN_REGISTER_TOPICS(mic,
{CAN_MSG_MIC19_STATE_ID, &parse_mic_state},
{CAN_MSG_MIC19_MOTOR_ID, &parse_mic_motor});
CAN_REGISTER_TOPICS(mswi,
{CAN_MSG_MSWI19_STATE_ID, &parse_mswi_state},
{CAN_MSG_MSWI19_MOTOR_ID, &parse_mswi_motor});
CAN_REGISTER_MODULES(mam_rx, 1, {CAN_SIGNATURE_MIC19, &CAN_TOPICS_NAME(mic), 100},
{CAN_SIGNATURE_MSWI19, &CAN_TOPICS_NAME(mswi), 100});
void can_handle_timeout(uint8_t signature)
{
if (signature == CAN_SIGNATURE_MIC19)
timeout = 1;
}
void test_msg_parsing(void)
{
can_msg_t msg;
for (int i = 0; i <= 255; i++)
{
/*
* Test case MIC -> MAM msg
*/
msg.raw[CAN_MSG_GENERIC_STATE_SIGNATURE_BYTE] = CAN_SIGNATURE_MIC19;
msg.raw[CAN_MSG_MIC19_MOTOR_D_BYTE] = i;
msg.raw[CAN_MSG_MIC19_MOTOR_I_BYTE] = 255 - i;
msg.raw[CAN_MSG_MIC19_MOTOR_MOTOR_BYTE] =
(((i % 2) == 0) << CAN_MSG_MIC19_MOTOR_MOTOR_MOTOR_ON_BIT);
msg.raw[CAN_MSG_MIC19_MOTOR_MOTOR_BYTE] |=
(((i % 2) == 0) << CAN_MSG_MIC19_MOTOR_MOTOR_DMS_ON_BIT);
msg.raw[CAN_MSG_MIC19_MOTOR_MOTOR_BYTE] |=
(((i % 2) != 0) << CAN_MSG_MIC19_MOTOR_MOTOR_REVERSE_BIT);
msg.id = CAN_MSG_MIC19_MOTOR_ID;
printf("parsing %d ...\n", msg.signature);
can_parser(&CAN_PARSER_NAME(mam_rx), &msg);
printf("test_mic.signature: %d\n", test_mic.signature);
printf("test_mic.d: %d\n", test_mic.d);
printf("test_mic.i: %d\n", test_mic.i);
printf("test_mic.motor.motor_on: %d\n", test_mic.motor.motor_on);
printf("test_mic.motor.dms_on: %d\n", test_mic.motor.dms_on);
printf("test_mic.motor.reverse: %d\n", test_mic.motor.reverse);
assert(CAN_SIGNATURE_MIC19 == test_mic.signature);
assert(i == test_mic.d);
assert(255 - i == test_mic.i);
assert(((i % 2) == 0) == test_mic.motor.motor_on);
assert(((i % 2) == 0) == test_mic.motor.dms_on);
assert(((i % 2) != 0) == test_mic.motor.reverse);
/*
* Test case MSWI -> MAM msg
*/
msg.id = CAN_MSG_MSWI19_MOTOR_ID;
msg.raw[CAN_MSG_GENERIC_STATE_SIGNATURE_BYTE] = CAN_SIGNATURE_MSWI19;
msg.raw[CAN_MSG_MSWI19_MOTOR_D_BYTE] = 255 - i;
msg.raw[CAN_MSG_MSWI19_MOTOR_I_BYTE] = i;
msg.raw[CAN_MSG_MSWI19_MOTOR_MOTOR_BYTE] = 0;
msg.raw[CAN_MSG_MSWI19_MOTOR_MOTOR_BYTE] |= (((i % 2) == 0) << CAN_MSG_MSWI19_MOTOR_MOTOR_MOTOR_ON_BIT);
msg.raw[CAN_MSG_MSWI19_MOTOR_MOTOR_BYTE] |= (((i % 2) != 0) << CAN_MSG_MSWI19_MOTOR_MOTOR_DMS_ON_BIT);
printf("parsing %d ...\n", msg.signature);
can_parser(&CAN_PARSER_NAME(mam_rx), &msg);
printf("test_mswi.signature: %d\n", test_mswi.signature);
printf("test_mswi.d: %d\n", test_mswi.d);
printf("test_mswi.i: %d\n", test_mswi.i);
printf("test_mswi.motor.motor_on: %d\n", test_mswi.motor.motor_on);
printf("test_mswi.motor.dms_on: %d\n", test_mswi.motor.dms_on);
assert(CAN_SIGNATURE_MSWI19 == test_mswi.signature);
assert(i == test_mswi.i);
assert(255 - i == test_mswi.d);
assert(((i % 2) == 0) == test_mswi.motor.motor_on);
assert(((i % 2) != 0) == test_mswi.motor.dms_on);
}
}
void test_timeout(void)
{
can_msg_t msg;
msg.id = CAN_MSG_GENERIC_STATE_ID;
printf("\n==> Testing if timeout is triggered\n");
timeout = 0;
for (int i = 1; i <= 100; i++)
{
msg.signature = CAN_SIGNATURE_MSWI19;
can_update_timeout(&CAN_PARSER_NAME(mam_rx));
can_parser(&CAN_PARSER_NAME(mam_rx), &msg);
// Testing if timeout_handler is triggered in 100
printf("i: %d\n", i);
if (i == 100)
{
assert(timeout == 1);
timeout = 0;
}
else
{
assert(timeout == 0);
}
}
printf("\n==>Testing if timeout have false triggers\n");
// Test if timeout have false triggers
timeout = 0;
for (int i = 1; i <= 1000; i++)
{
msg.signature = CAN_SIGNATURE_MSWI19;
can_update_timeout(&CAN_PARSER_NAME(mam_rx));
can_parser(&CAN_PARSER_NAME(mam_rx), &msg);
msg.signature = CAN_SIGNATURE_MIC19;
can_parser(&CAN_PARSER_NAME(mam_rx), &msg);
printf("i: %d\n", i);
assert(timeout == 0);
}
}
int main(void)
{
test_timeout();
test_msg_parsing();
}