This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathu_cell_mux_private_test.c
296 lines (254 loc) · 11.8 KB
/
u_cell_mux_private_test.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
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
/*
* Copyright 2019-2024 u-blox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Only #includes of u_* and the C standard library are allowed here,
* no platform stuff and no OS stuff. Anything required from
* the platform/OS must be brought in through u_port* to maintain
* portability.
*/
/** @file
* @brief Tests for the internal cellulare mux API. No cellular module
* is required to run this set of tests, all testing is back to back.
* IMPORTANT: see notes in u_cfg_test_platform_specific.h for the
* naming rules that must be followed when using the U_PORT_TEST_FUNCTION()
* macro.
*/
#ifdef U_CFG_OVERRIDE
# include "u_cfg_override.h" // For a customer's configuration override
#endif
#include "stddef.h" // NULL, size_t etc.
#include "stdint.h" // int32_t etc.
#include "stdbool.h"
#include "string.h" // memcmp()/memset()
#include "u_cfg_sw.h"
#include "u_cfg_os_platform_specific.h"
#include "u_cfg_app_platform_specific.h"
#include "u_cfg_test_platform_specific.h"
#include "u_error_common.h"
#include "u_test_util_resource_check.h"
#include "u_timeout.h"
#include "u_at_client.h"
#include "u_ringbuffer.h"
#include "u_device_serial.h"
#include "u_port.h"
#include "u_port_os.h"
#include "u_port_heap.h"
#include "u_port_debug.h"
#include "u_cell_module_type.h"
#include "u_cell.h"
#include "u_cell_file.h"
#include "u_cell_net.h" // Required by u_cell_private.h
#include "u_cell_private.h"
#include "u_cell_mux.h"
#include "u_cell_mux_private.h"
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
/** The base string to put at the start of all prints from this test.
*/
#define U_TEST_PREFIX_BASE "U_CELL_MUX_PRIVATE_TEST"
/** The string to put at the start of all prints from this test
* that do not require an iteration on the end.
*/
#define U_TEST_PREFIX U_TEST_PREFIX_BASE ": "
/** Print a whole line, with terminator, prefixed for this test
* file, no iteration version.
*/
#define U_TEST_PRINT_LINE(format, ...) uPortLog(U_TEST_PREFIX format "\n", ##__VA_ARGS__)
#ifndef U_CELL_MUX_PRIVATE_TEST_MAX_FRAME_SIZE_BYTES
/** The maximum CMUX frame size to encode, sized to match
* the largest GNSS-tunnelled UBX-format message we might
* ever get.
*/
# define U_CELL_MUX_PRIVATE_TEST_MAX_FRAME_SIZE_BYTES (1024 * 2)
#endif
#ifndef U_CELL_MUX_PRIVATE_TEST_MAX_INFORMATION_SIZE_BYTES
/** The maximum length of information field to encode.
*/
# define U_CELL_MUX_PRIVATE_TEST_MAX_INFORMATION_SIZE_BYTES (U_CELL_MUX_PRIVATE_TEST_MAX_FRAME_SIZE_BYTES - U_CELL_MUX_PRIVATE_FRAME_OVERHEAD_MAX_BYTES)
#endif
#ifndef U_CELL_MUX_PRIVATE_TEST_FILL_CHAR
/** Character to use as fill in the mux buffer so that we
* can check it has been written for the correct length by
* the decoder.
*/
# define U_CELL_MUX_PRIVATE_TEST_FILL_CHAR 0xFF
#endif
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
/* ----------------------------------------------------------------
* VARIABLES
* -------------------------------------------------------------- */
/** The command types to test; must have the same number of elements
* as gCommandResponse.
*/
static uCellMuxPrivateFrameType_t gType[] = {U_CELL_MUX_PRIVATE_FRAME_TYPE_SABM_COMMAND,
U_CELL_MUX_PRIVATE_FRAME_TYPE_UA_RESPONSE,
U_CELL_MUX_PRIVATE_FRAME_TYPE_DM_RESPONSE,
U_CELL_MUX_PRIVATE_FRAME_TYPE_DISC_COMMAND,
U_CELL_MUX_PRIVATE_FRAME_TYPE_UIH,
U_CELL_MUX_PRIVATE_FRAME_TYPE_UI
};
/** What the commmand/response value should be for each of gType.
*/
static bool gCommandResponse[] = {true, // U_CELL_MUX_PRIVATE_FRAME_TYPE_SABM_COMMAND
false, // U_CELL_MUX_PRIVATE_FRAME_TYPE_UA_RESPONSE,
false, // U_CELL_MUX_PRIVATE_FRAME_TYPE_DM_RESPONSE,
true, // U_CELL_MUX_PRIVATE_FRAME_TYPE_DISC_COMMAND,
true, // U_CELL_MUX_PRIVATE_FRAME_TYPE_UIH,
true // U_CELL_MUX_PRIVATE_FRAME_TYPE_UI
};
/* ----------------------------------------------------------------
* STATIC FUNCTIONS
* -------------------------------------------------------------- */
// Return "true" or "false" for a Boolean type.
const char *pBool(bool isTrue)
{
return isTrue ? "true" : "false";
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS
* -------------------------------------------------------------- */
/** Test the mux encode/decode functions back-to-back.
*
* IMPORTANT: see notes in u_cfg_test_platform_specific.h for the
* naming rules that must be followed when using the
* U_PORT_TEST_FUNCTION() macro.
*/
U_PORT_TEST_FUNCTION("[cellMuxPrivate]", "cellMuxPrivateBackToBack")
{
int32_t resourceCount;
uint8_t address;
bool pollFinal = false;
char *pInformation;
int32_t expectedFrameLength;
uCellMuxPrivateParserContext_t parserContext = {0};
int32_t z;
// Obtain the initial resource count
resourceCount = uTestUtilGetDynamicResourceCount();
U_PORT_TEST_ASSERT(uPortInit() == 0);
// Grab some memory for the encoded CMUX frame and the information field we want to encode
parserContext.bufferSize = U_CELL_MUX_PRIVATE_TEST_MAX_FRAME_SIZE_BYTES;
parserContext.pBuffer = (char *) pUPortMalloc(parserContext.bufferSize);
U_PORT_TEST_ASSERT(parserContext.pBuffer != NULL);
pInformation = (char *) pUPortMalloc(U_CELL_MUX_PRIVATE_TEST_MAX_FRAME_SIZE_BYTES);
U_PORT_TEST_ASSERT(pInformation != NULL);
// Encode a variety of lengths, addresses and types
for (size_t informationLength = 0;
informationLength < U_CELL_MUX_PRIVATE_TEST_MAX_INFORMATION_SIZE_BYTES;
informationLength += 10) {
// Fill the information field with a known pattern
for (size_t y = 0; y < informationLength; y++) {
*(pInformation + y) = (char) y;
}
for (size_t x = 0; x < sizeof(gType) / sizeof(gType[0]); x++) {
// Fill the frame with a known character
memset(parserContext.pBuffer, U_CELL_MUX_PRIVATE_TEST_FILL_CHAR,
U_CELL_MUX_PRIVATE_TEST_MAX_FRAME_SIZE_BYTES);
// The expected length is the information length plus the
// maximum overhead size, one less if the information
// field fits into a single byte
expectedFrameLength = informationLength + U_CELL_MUX_PRIVATE_FRAME_OVERHEAD_MAX_BYTES;
if (informationLength <= 0x7F) {
expectedFrameLength--;
}
// Set up for encoding
address = 0;
pollFinal = !pollFinal;
// Encode into the buffer
parserContext.bufferSize = uCellMuxPrivateEncode(address,
gType[x], pollFinal,
pInformation, informationLength,
parserContext.pBuffer);
if ((int32_t)parserContext.bufferSize != expectedFrameLength) {
U_TEST_PRINT_LINE("encoded frame length %d when %d was expected.",
parserContext.bufferSize, expectedFrameLength);
U_PORT_TEST_ASSERT(false);
}
// Set up for decoding
parserContext.address = U_CELL_MUX_PRIVATE_ADDRESS_ANY;
parserContext.commandResponse = !gCommandResponse[x];
parserContext.type = U_CELL_MUX_PRIVATE_FRAME_TYPE_NONE;
parserContext.pollFinal = !pollFinal;
// Decode the information field back into the same buffer
parserContext.pInformation = parserContext.pBuffer;
parserContext.informationLengthBytes = U_CELL_MUX_PRIVATE_TEST_MAX_FRAME_SIZE_BYTES;
parserContext.bufferIndex = 0;
// Decode from the buffer, information field back into the buffer
z = (int32_t) U_ERROR_COMMON_NOT_FOUND;
while ((parserContext.bufferIndex < parserContext.bufferSize) &&
(z < 0) && (z != (int32_t) U_ERROR_COMMON_TIMEOUT)) {
z = uCellMuxPrivateParseCmux(NULL, &parserContext);
}
if (parserContext.informationLengthBytes != informationLength) {
U_TEST_PRINT_LINE("decoded information field length %d when %d was expected.",
parserContext.informationLengthBytes, informationLength);
U_PORT_TEST_ASSERT(false);
}
if (memcmp(parserContext.pBuffer, pInformation, informationLength) != 0) {
U_TEST_PRINT_LINE("decoded information field not as expected.");
U_PORT_TEST_ASSERT(false);
}
if (parserContext.address != address) {
U_TEST_PRINT_LINE("decoded address 0x%02x when 0x%02x was expected.",
parserContext.address, address);
U_PORT_TEST_ASSERT(false);
}
if (parserContext.type != gType[x]) {
U_TEST_PRINT_LINE("decoded type 0x%02x when 0x%02x was expected.",
parserContext.type, gType[x]);
U_PORT_TEST_ASSERT(false);
}
if (parserContext.commandResponse != gCommandResponse[x]) {
U_TEST_PRINT_LINE("decoded command/response %s when %s was expected.",
pBool(parserContext.commandResponse), pBool(gCommandResponse[x]));
U_PORT_TEST_ASSERT(false);
}
if (parserContext.pollFinal != pollFinal) {
U_TEST_PRINT_LINE("decoded poll/final %s when %s was expected.",
pBool(parserContext.pollFinal), pBool(pollFinal));
U_PORT_TEST_ASSERT(false);
}
if (parserContext.bufferIndex != parserContext.bufferSize) {
U_TEST_PRINT_LINE("bufferIndex %d when %d was expected.",
parserContext.bufferIndex, parserContext.bufferSize);
U_PORT_TEST_ASSERT(false);
}
// Invert poll/final for next time
pollFinal = !pollFinal;
}
// Switch addresses
if (address == 0) {
address = U_CELL_MUX_PRIVATE_ADDRESS_MAX;
} else {
address = 0;
}
// Some platforms run a task watchdog which might be starved with such
// a large processing loop: give it a bone
uPortTaskBlock(U_CFG_OS_YIELD_MS);
}
// Free memory
uPortFree(parserContext.pBuffer);
uPortFree(pInformation);
uPortDeinit();
// Check for resource leaks
resourceCount = uTestUtilGetDynamicResourceCount() - resourceCount;
U_TEST_PRINT_LINE("we have leaked %d resources(s).", resourceCount);
U_PORT_TEST_ASSERT(resourceCount <= 0);
// Printed for information: asserting happens in the postamble
uTestUtilResourceCheck(U_TEST_PREFIX, NULL, true);
}
// End of file