1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "transport_util.h"
18
19 #include <gtest/gtest.h>
20
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <chrono>
26 #include <thread>
27
28 #include "chpp/app.h"
29 #include "chpp/common/discovery.h"
30 #include "chpp/common/gnss.h"
31 #include "chpp/common/gnss_types.h"
32 #include "chpp/common/standard_uuids.h"
33 #include "chpp/common/wifi.h"
34 #include "chpp/common/wifi_types.h"
35 #include "chpp/common/wwan.h"
36 #include "chpp/crc.h"
37 #include "chpp/macros.h"
38 #include "chpp/memory.h"
39 #include "chpp/platform/platform_link.h"
40 #include "chpp/platform/utils.h"
41 #include "chpp/services/discovery.h"
42 #include "chpp/services/loopback.h"
43 #include "chpp/transport.h"
44 #include "chre/pal/wwan.h"
45
46 namespace chpp::test {
47
48 /**
49 * Wait for chppTransportDoWork() to finish after it is notified by
50 * chppEnqueueTxPacket to run.
51 */
WaitForTransport(struct ChppTransportState * transportContext)52 void WaitForTransport(struct ChppTransportState *transportContext) {
53 // Start sending data out.
54 cycleSendThread();
55 // Wait for data to be received and processed.
56 std::this_thread::sleep_for(std::chrono::milliseconds(50));
57
58 // Should have reset loc and length for next packet / datagram
59 EXPECT_EQ(transportContext->rxStatus.locInDatagram, 0);
60 EXPECT_EQ(transportContext->rxDatagram.length, 0);
61 }
62
63 /**
64 * Validates a ChppTestResponse. Since the error field within the
65 * ChppAppHeader struct is optional (and not used for common services), this
66 * function returns the error field to be checked if desired, depending on the
67 * service.
68 *
69 * @param buf Buffer containing response.
70 * @param ackSeq Ack sequence to be verified.
71 * @param handle Handle number to be verified
72 * @param transactionID Transaction ID to be verified.
73 *
74 * @return The error field within the ChppAppHeader struct that is used by some
75 * but not all services.
76 */
validateChppTestResponse(void * buf,uint8_t ackSeq,uint8_t handle,uint8_t transactionID)77 uint8_t validateChppTestResponse(void *buf, uint8_t ackSeq, uint8_t handle,
78 uint8_t transactionID) {
79 struct ChppTestResponse *response = (ChppTestResponse *)buf;
80
81 // Check preamble
82 EXPECT_EQ(response->preamble0, kChppPreamble0);
83 EXPECT_EQ(response->preamble1, kChppPreamble1);
84
85 // Check response transport headers
86 EXPECT_EQ(response->transportHeader.packetCode, CHPP_TRANSPORT_ERROR_NONE);
87 EXPECT_EQ(response->transportHeader.ackSeq, ackSeq);
88
89 // Check response app headers
90 EXPECT_EQ(response->appHeader.handle, handle);
91 EXPECT_EQ(response->appHeader.type, CHPP_MESSAGE_TYPE_SERVICE_RESPONSE);
92 EXPECT_EQ(response->appHeader.transaction, transactionID);
93
94 // Return optional response error to be checked if desired
95 return response->appHeader.error;
96 }
97
98 /**
99 * Aborts a packet and validates state.
100 *
101 * @param transportcontext Maintains status for each transport layer instance.
102 */
endAndValidatePacket(struct ChppTransportState * transportContext)103 void endAndValidatePacket(struct ChppTransportState *transportContext) {
104 chppRxPacketCompleteCb(transportContext);
105 EXPECT_EQ(transportContext->rxStatus.state, CHPP_STATE_PREAMBLE);
106 EXPECT_EQ(transportContext->rxStatus.locInDatagram, 0);
107 EXPECT_EQ(transportContext->rxDatagram.length, 0);
108 }
109
110 /**
111 * Adds a preamble to a certain location in a buffer, and increases the location
112 * accordingly, to account for the length of the added preamble.
113 *
114 * @param buf Buffer.
115 * @param location Location to add the preamble, which its value will be
116 * increased accordingly.
117 */
addPreambleToBuf(uint8_t * buf,size_t * location)118 void addPreambleToBuf(uint8_t *buf, size_t *location) {
119 buf[(*location)++] = kChppPreamble0;
120 buf[(*location)++] = kChppPreamble1;
121 }
122
123 /**
124 * Adds a transport header (with default values) to a certain location in a
125 * buffer, and increases the location accordingly, to account for the length of
126 * the added transport header.
127 *
128 * @param buf Buffer.
129 * @param location Location to add the transport header, which its value will be
130 * increased accordingly.
131 *
132 * @return Pointer to the added transport header (e.g. to modify its fields).
133 */
addTransportHeaderToBuf(uint8_t * buf,size_t * location)134 ChppTransportHeader *addTransportHeaderToBuf(uint8_t *buf, size_t *location) {
135 size_t oldLoc = *location;
136
137 // Default values for initial, minimum size request packet
138 ChppTransportHeader transHeader = {};
139 transHeader.flags = CHPP_TRANSPORT_FLAG_FINISHED_DATAGRAM;
140 transHeader.packetCode = CHPP_TRANSPORT_ERROR_NONE;
141 transHeader.ackSeq = 1;
142 transHeader.seq = 0;
143 transHeader.length = sizeof(ChppAppHeader);
144 transHeader.reserved = 0;
145
146 memcpy(&buf[*location], &transHeader, sizeof(transHeader));
147 *location += sizeof(transHeader);
148
149 return (ChppTransportHeader *)&buf[oldLoc];
150 }
151
152 /**
153 * Adds an app header (with default values) to a certain location in a buffer,
154 * and increases the location accordingly, to account for the length of the
155 * added app header.
156 *
157 * @param buf Buffer.
158 * @param location Location to add the app header, which its value will be
159 * increased accordingly.
160 *
161 * @return Pointer to the added app header (e.g. to modify its fields).
162 */
addAppHeaderToBuf(uint8_t * buf,size_t * location)163 ChppAppHeader *addAppHeaderToBuf(uint8_t *buf, size_t *location) {
164 size_t oldLoc = *location;
165
166 // Default values - to be updated later as necessary
167 ChppAppHeader appHeader = {};
168 appHeader.handle = CHPP_HANDLE_NEGOTIATED_RANGE_START;
169 appHeader.type = CHPP_MESSAGE_TYPE_CLIENT_REQUEST;
170 appHeader.transaction = 0;
171 appHeader.error = CHPP_APP_ERROR_NONE;
172 appHeader.command = 0;
173
174 memcpy(&buf[*location], &appHeader, sizeof(appHeader));
175 *location += sizeof(appHeader);
176
177 return (ChppAppHeader *)&buf[oldLoc];
178 }
179
180 /**
181 * Adds a transport footer to a certain location in a buffer, and increases the
182 * location accordingly, to account for the length of the added preamble.
183 *
184 * @param buf Buffer.
185 * @param location Location to add the footer. The value of location will be
186 * increased accordingly.
187 *
188 */
addTransportFooterToBuf(uint8_t * buf,size_t * location)189 void addTransportFooterToBuf(uint8_t *buf, size_t *location) {
190 uint32_t *checksum = (uint32_t *)&buf[*location];
191
192 *checksum = chppCrc32(0, &buf[CHPP_PREAMBLE_LEN_BYTES],
193 *location - CHPP_PREAMBLE_LEN_BYTES);
194
195 *location += sizeof(ChppTransportFooter);
196 }
197
198 /**
199 * Opens a service and checks to make sure it was opened correctly.
200 *
201 * @param transportContext Transport layer context.
202 * @param buf Buffer.
203 * @param ackSeq Ack sequence of the packet to be sent out
204 * @param seq Sequence number of the packet to be sent out.
205 * @param handle Handle of the service to be opened.
206 * @param transactionID Transaction ID for the open request.
207 * @param command Open command.
208 */
openService(ChppTransportState * transportContext,uint8_t * buf,uint8_t ackSeq,uint8_t seq,uint8_t handle,uint8_t transactionID,uint16_t command,struct ChppLinuxLinkState & chppLinuxLinkContext)209 void openService(ChppTransportState *transportContext, uint8_t *buf,
210 uint8_t ackSeq, uint8_t seq, uint8_t handle,
211 uint8_t transactionID, uint16_t command,
212 struct ChppLinuxLinkState &chppLinuxLinkContext) {
213 size_t len = 0;
214
215 addPreambleToBuf(buf, &len);
216
217 ChppTransportHeader *transHeader = addTransportHeaderToBuf(buf, &len);
218 transHeader->ackSeq = ackSeq;
219 transHeader->seq = seq;
220
221 ChppAppHeader *appHeader = addAppHeaderToBuf(buf, &len);
222 appHeader->handle = handle;
223 appHeader->transaction = transactionID;
224 appHeader->command = command;
225
226 addTransportFooterToBuf(buf, &len);
227
228 // Send header + payload (if any) + footer
229 EXPECT_TRUE(chppRxDataCb(transportContext, buf, len));
230
231 // Check for correct state
232 uint8_t nextSeq = transHeader->seq + 1;
233 EXPECT_EQ(transportContext->rxStatus.expectedSeq, nextSeq);
234 EXPECT_EQ(transportContext->rxStatus.state, CHPP_STATE_PREAMBLE);
235
236 // Wait for response
237 WaitForTransport(transportContext);
238
239 // Validate common response fields
240 EXPECT_EQ(validateChppTestResponse(chppLinuxLinkContext.buf, nextSeq, handle,
241 transactionID),
242 CHPP_APP_ERROR_NONE);
243
244 // Check response length
245 EXPECT_EQ(sizeof(ChppTestResponse), CHPP_PREAMBLE_LEN_BYTES +
246 sizeof(ChppTransportHeader) +
247 sizeof(ChppAppHeader));
248 EXPECT_EQ(transportContext->linkBufferSize,
249 sizeof(ChppTestResponse) + sizeof(ChppTransportFooter));
250 }
251
252 /**
253 * Sends a command to a service and checks for errors.
254 *
255 * @param transportContext Transport layer context.
256 * @param buf Buffer.
257 * @param ackSeq Ack sequence of the packet to be sent out
258 * @param seq Sequence number of the packet to be sent out.
259 * @param handle Handle of the service to be opened.
260 * @param transactionID Transaction ID for the open request.
261 * @param command Command to be sent.
262 */
sendCommandToService(ChppTransportState * transportContext,uint8_t * buf,uint8_t ackSeq,uint8_t seq,uint8_t handle,uint8_t transactionID,uint16_t command,struct ChppLinuxLinkState & chppLinuxLinkContext)263 void sendCommandToService(ChppTransportState *transportContext, uint8_t *buf,
264 uint8_t ackSeq, uint8_t seq, uint8_t handle,
265 uint8_t transactionID, uint16_t command,
266 struct ChppLinuxLinkState &chppLinuxLinkContext) {
267 size_t len = 0;
268
269 addPreambleToBuf(buf, &len);
270
271 ChppTransportHeader *transHeader = addTransportHeaderToBuf(buf, &len);
272 transHeader->ackSeq = ackSeq;
273 transHeader->seq = seq;
274
275 ChppAppHeader *appHeader = addAppHeaderToBuf(buf, &len);
276 appHeader->handle = handle;
277 appHeader->transaction = transactionID;
278 appHeader->command = command;
279
280 addTransportFooterToBuf(buf, &len);
281
282 // Send header + payload (if any) + footer
283 EXPECT_TRUE(chppRxDataCb(transportContext, buf, len));
284
285 // Check for correct state
286 uint8_t nextSeq = transHeader->seq + 1;
287 EXPECT_EQ(transportContext->rxStatus.expectedSeq, nextSeq);
288 EXPECT_EQ(transportContext->rxStatus.state, CHPP_STATE_PREAMBLE);
289
290 // Wait for response
291 WaitForTransport(transportContext);
292
293 // Validate common response fields
294 EXPECT_EQ(validateChppTestResponse(chppLinuxLinkContext.buf, nextSeq, handle,
295 transactionID),
296 CHPP_APP_ERROR_NONE);
297 }
298
299 /**
300 * Find service handle by name.
301 *
302 * @param appContext App context.
303 * @param name Service name.
304 * @param handle Output service handle if found.
305 *
306 * @return True if service found, false otherwise.
307 */
findServiceHandle(ChppAppState * appContext,const char * name,uint8_t * handle)308 bool findServiceHandle(ChppAppState *appContext, const char *name,
309 uint8_t *handle) {
310 for (uint8_t i = 0; i < appContext->registeredServiceCount; i++) {
311 if (0 == strcmp(appContext->registeredServices[i]->descriptor.name, name)) {
312 *handle = appContext->registeredServiceStates[i]->handle;
313 return true;
314 }
315 }
316 return false;
317 }
318
319 } // namespace chpp::test
320