xref: /aosp_15_r20/system/chre/chpp/test/transport_util.h (revision 84e339476a462649f82315436d70fd732297a399)
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 #ifndef CHPP_TRANSPORT_UTIL_H_
18 #define CHPP_TRANSPORT_UTIL_H_
19 
20 #include <stdbool.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 
24 #include "chpp/app.h"
25 #include "chpp/macros.h"
26 #include "chpp/platform/platform_link.h"
27 #include "chpp/transport.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 CHPP_PACKED_START
34 struct ChppTestResponse {
35   char preamble0;
36   char preamble1;
37   struct ChppTransportHeader transportHeader;
38   struct ChppAppHeader appHeader;
39 } CHPP_PACKED_ATTR;
40 CHPP_PACKED_END
41 
42 #ifdef __cplusplus
43 }
44 #endif
45 
46 namespace chpp::test {
47 
48 namespace {
49 
50 // Preamble as separate bytes for testing
51 constexpr uint8_t kChppPreamble0 = 0x68;
52 constexpr uint8_t kChppPreamble1 = 0x43;
53 
54 }  // namespace
55 
56 /************************************************
57  *  Helper functions available for other tests
58  ***********************************************/
59 
60 /**
61  * Wait for chppTransportDoWork() to finish after it is notified by
62  * chppEnqueueTxPacket to run.
63  */
64 void WaitForTransport(struct ChppTransportState *transportContext);
65 
66 /**
67  * Validates a ChppTestResponse. Since the error field within the
68  * ChppAppHeader struct is optional (and not used for common services), this
69  * function returns the error field to be checked if desired, depending on the
70  * service.
71  *
72  * @param buf Buffer containing response.
73  * @param ackSeq Ack sequence to be verified.
74  * @param handle Handle number to be verified
75  * @param transactionID Transaction ID to be verified.
76  *
77  * @return The error field within the ChppAppHeader struct that is used by some
78  * but not all services.
79  */
80 uint8_t validateChppTestResponse(void *buf, uint8_t ackSeq, uint8_t handle,
81                                  uint8_t transactionID);
82 
83 /**
84  * Aborts a packet and validates state.
85  *
86  * @param transportcontext Maintains status for each transport layer instance.
87  */
88 void endAndValidatePacket(struct ChppTransportState *transportContext);
89 
90 /**
91  * Adds a preamble to a certain location in a buffer, and increases the location
92  * accordingly, to account for the length of the added preamble.
93  *
94  * @param buf Buffer.
95  * @param location Location to add the preamble, which its value will be
96  * increased accordingly.
97  */
98 void addPreambleToBuf(uint8_t *buf, size_t *location);
99 
100 /**
101  * Adds a transport header (with default values) to a certain location in a
102  * buffer, and increases the location accordingly, to account for the length of
103  * the added transport header.
104  *
105  * @param buf Buffer.
106  * @param location Location to add the transport header, which its value will be
107  * increased accordingly.
108  *
109  * @return Pointer to the added transport header (e.g. to modify its fields).
110  */
111 ChppTransportHeader *addTransportHeaderToBuf(uint8_t *buf, size_t *location);
112 
113 /**
114  * Adds an app header (with default values) to a certain location in a buffer,
115  * and increases the location accordingly, to account for the length of the
116  * added app header.
117  *
118  * @param buf Buffer.
119  * @param location Location to add the app header, which its value will be
120  * increased accordingly.
121  *
122  * @return Pointer to the added app header (e.g. to modify its fields).
123  */
124 ChppAppHeader *addAppHeaderToBuf(uint8_t *buf, size_t *location);
125 
126 /**
127  * Adds a transport footer to a certain location in a buffer, and increases the
128  * location accordingly, to account for the length of the added preamble.
129  *
130  * @param buf Buffer.
131  * @param location Location to add the footer. The value of location will be
132  * increased accordingly.
133  *
134  */
135 void addTransportFooterToBuf(uint8_t *buf, size_t *location);
136 
137 /**
138  * Opens a service and checks to make sure it was opened correctly.
139  *
140  * @param transportContext Transport layer context.
141  * @param buf Buffer.
142  * @param ackSeq Ack sequence of the packet to be sent out
143  * @param seq Sequence number of the packet to be sent out.
144  * @param handle Handle of the service to be opened.
145  * @param transactionID Transaction ID for the open request.
146  * @param command Open command.
147  */
148 void openService(ChppTransportState *transportContext, uint8_t *buf,
149                  uint8_t ackSeq, uint8_t seq, uint8_t handle,
150                  uint8_t transactionID, uint16_t command,
151                  struct ChppLinuxLinkState &chppLinuxLinkContext);
152 
153 /**
154  * Sends a command to a service and checks for errors.
155  *
156  * @param transportContext Transport layer context.
157  * @param buf Buffer.
158  * @param ackSeq Ack sequence of the packet to be sent out
159  * @param seq Sequence number of the packet to be sent out.
160  * @param handle Handle of the service to be opened.
161  * @param transactionID Transaction ID for the open request.
162  * @param command Command to be sent.
163  */
164 void sendCommandToService(ChppTransportState *transportContext, uint8_t *buf,
165                           uint8_t ackSeq, uint8_t seq, uint8_t handle,
166                           uint8_t transactionID, uint16_t command,
167                           struct ChppLinuxLinkState &chppLinuxLinkContext);
168 
169 /**
170  * Find service handle by name.
171  *
172  * @param appContext App context.
173  * @param name Service name.
174  * @param handle Output service handle if found.
175  *
176  * @return True if service found, false otherwise.
177  */
178 bool findServiceHandle(ChppAppState *appContext, const char *name,
179                        uint8_t *handle);
180 
181 }  // namespace chpp::test
182 
183 #endif  // CHPP_TRANSPORT_UTIL_H_
184