• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Validates a ChppTestResponse. Since the error field within the
62  * ChppAppHeader struct is optional (and not used for common services), this
63  * function returns the error field to be checked if desired, depending on the
64  * service.
65  *
66  * @param buf Buffer containing response.
67  * @param ackSeq Ack sequence to be verified.
68  * @param handle Handle number to be verified
69  * @param transactionID Transaction ID to be verified.
70  *
71  * @return The error field within the ChppAppHeader struct that is used by some
72  * but not all services.
73  */
74 uint8_t validateChppTestResponse(void *buf, uint8_t ackSeq, uint8_t handle,
75                                  uint8_t transactionID);
76 
77 /**
78  * Aborts a packet and validates state.
79  *
80  * @param transportcontext Maintains status for each transport layer instance.
81  */
82 void endAndValidatePacket(struct ChppTransportState *transportContext);
83 
84 /**
85  * Adds a preamble to a certain location in a buffer, and increases the location
86  * accordingly, to account for the length of the added preamble.
87  *
88  * @param buf Buffer.
89  * @param location Location to add the preamble, which its value will be
90  * increased accordingly.
91  */
92 void addPreambleToBuf(uint8_t *buf, size_t *location);
93 
94 /**
95  * Adds a transport header (with default values) to a certain location in a
96  * buffer, and increases the location accordingly, to account for the length of
97  * the added transport header.
98  *
99  * @param buf Buffer.
100  * @param location Location to add the transport header, which its value will be
101  * increased accordingly.
102  *
103  * @return Pointer to the added transport header (e.g. to modify its fields).
104  */
105 ChppTransportHeader *addTransportHeaderToBuf(uint8_t *buf, size_t *location);
106 
107 /**
108  * Adds an app header (with default values) to a certain location in a buffer,
109  * and increases the location accordingly, to account for the length of the
110  * added app header.
111  *
112  * @param buf Buffer.
113  * @param location Location to add the app header, which its value will be
114  * increased accordingly.
115  *
116  * @return Pointer to the added app header (e.g. to modify its fields).
117  */
118 ChppAppHeader *addAppHeaderToBuf(uint8_t *buf, size_t *location);
119 
120 /**
121  * Adds a transport footer to a certain location in a buffer, and increases the
122  * location accordingly, to account for the length of the added preamble.
123  *
124  * @param buf Buffer.
125  * @param location Location to add the footer. The value of location will be
126  * increased accordingly.
127  *
128  */
129 void addTransportFooterToBuf(uint8_t *buf, size_t *location);
130 
131 /**
132  * Opens a service and checks to make sure it was opened correctly.
133  *
134  * @param transportContext Transport layer context.
135  * @param buf Buffer.
136  * @param ackSeq Ack sequence of the packet to be sent out
137  * @param seq Sequence number of the packet to be sent out.
138  * @param handle Handle of the service to be opened.
139  * @param transactionID Transaction ID for the open request.
140  * @param command Open command.
141  */
142 void openService(ChppTransportState *transportContext, uint8_t *buf,
143                  uint8_t ackSeq, uint8_t seq, uint8_t handle,
144                  uint8_t transactionID, uint16_t command,
145                  struct ChppLinuxLinkState &chppLinuxLinkContext);
146 
147 /**
148  * Sends a command to a service and checks for errors.
149  *
150  * @param transportContext Transport layer context.
151  * @param buf Buffer.
152  * @param ackSeq Ack sequence of the packet to be sent out
153  * @param seq Sequence number of the packet to be sent out.
154  * @param handle Handle of the service to be opened.
155  * @param transactionID Transaction ID for the open request.
156  * @param command Command to be sent.
157  */
158 void sendCommandToService(ChppTransportState *transportContext, uint8_t *buf,
159                           uint8_t ackSeq, uint8_t seq, uint8_t handle,
160                           uint8_t transactionID, uint16_t command,
161                           struct ChppLinuxLinkState &chppLinuxLinkContext);
162 
163 /**
164  * Find service handle by name.
165  *
166  * @param appContext App context.
167  * @param name Service name.
168  * @param handle Output service handle if found.
169  *
170  * @return True if service found, false otherwise.
171  */
172 bool findServiceHandle(ChppAppState *appContext, const char *name,
173                        uint8_t *handle);
174 
175 }  // namespace chpp::test
176 
177 #endif  // CHPP_TRANSPORT_UTIL_H_
178