1 /*
2  * Copyright (C) 2020 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_CLIENT_LOOPBACK_H_
18 #define CHPP_CLIENT_LOOPBACK_H_
19 
20 #include <stdbool.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 
24 #include "chpp/app.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /**
31  * Loopback Test Results.
32  */
33 struct ChppLoopbackTestResult {
34   enum ChppAppErrorCode error;  // Indicates success or error type
35   size_t requestLen;   // Length of the loopback request datagram, including app
36                        // header
37   size_t responseLen;  // Length of the loopback response datagram, including
38                        // app header
39   size_t firstError;   // Location of the first incorrect byte in the response
40                        // datagram
41   size_t byteErrors;   // Number of incorrect bytes in the response datagram
42   uint64_t rttNs;      // Round trip time
43 };
44 
45 /**
46  * Minimum header length for a loopback packet. Everything afterwards is
47  * considered a payload and is looped back.
48  */
49 #define CHPP_LOOPBACK_HEADER_LEN 2
50 
51 /************************************************
52  *  Public functions
53  ***********************************************/
54 
55 /**
56  * Initializes the client.
57  *
58  * @param context Maintains status for each app layer instance.
59  */
60 void chppLoopbackClientInit(struct ChppAppState *context);
61 
62 /**
63  * Deinitializes the client.
64  *
65  * @param context Maintains status for each app layer instance.
66  */
67 void chppLoopbackClientDeinit(struct ChppAppState *context);
68 
69 /**
70  * Dispatches an Rx Datagram from the transport layer that is determined to
71  * be for the CHPP Loopback Client.
72  *
73  * @param context Maintains status for each app layer instance.
74  * @param response Input (response) datagram. Cannot be null.
75  * @param len Length of input data in bytes.
76  */
77 bool chppDispatchLoopbackServiceResponse(struct ChppAppState *context,
78                                          const uint8_t *response, size_t len);
79 
80 /**
81  * Initiates a CHPP service loopback from the client side.
82  * Note that only one loopback test may be run at any time on each client.
83  *
84  * @param context Maintains status for each app layer instance.
85  */
86 struct ChppLoopbackTestResult chppRunLoopbackTest(struct ChppAppState *context,
87                                                   const uint8_t *buf,
88                                                   size_t len);
89 
90 #ifdef __cplusplus
91 }
92 #endif
93 
94 #endif  // CHPP_CLIENT_LOOPBACK_H_
95