aboutsummaryrefslogtreecommitdiff
path: root/chpp/include/chpp/services.h
blob: ad75e9bfceab42e4cdff4e7a8222f7160d7274c2 (plain)
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
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * 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.
 */

#ifndef CHPP_SERVICES_H_
#define CHPP_SERVICES_H_

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#include "chpp/app.h"
#include "chpp/macros.h"

#ifdef __cplusplus
extern "C" {
#endif

/************************************************
 *  Public Definitions
 ***********************************************/

#if defined(CHPP_SERVICE_ENABLED_WWAN) || \
    defined(CHPP_SERVICE_ENABLED_WIFI) || defined(CHPP_SERVICE_ENABLED_GNSS)
#define CHPP_SERVICE_ENABLED
#endif

/**
 * Uses chppAllocServiceNotification() to allocate a variable-length response
 * message of a specific type.
 *
 * @param type Type of notification which includes an arrayed member.
 * @param count number of items in the array of arrayField.
 * @param arrayField The arrayed member field.
 *
 * @return Pointer to allocated memory
 */
#define chppAllocServiceNotificationTypedArray(type, count, arrayField) \
  (type *)chppAllocServiceNotification(                                 \
      sizeof(type) + (count)*sizeof_member(type, arrayField[0]))

/**
 * Uses chppAllocServiceNotification() to allocate a response message of a
 * specific type and its corresponding length.
 *
 * @param type Type of notification.
 *
 * @return Pointer to allocated memory
 */
#define chppAllocServiceNotificationFixed(type) \
  (type *)chppAllocServiceNotification(sizeof(type))

/**
 * Uses chppAllocServiceResponse() to allocate a variable-length response
 * message of a specific type.
 *
 * @param requestHeader client request header, as per
 * chppAllocServiceResponse().
 * @param type Type of response which includes an arrayed member.
 * @param count number of items in the array of arrayField.
 * @param arrayField The arrayed member field.
 *
 * @return Pointer to allocated memory
 */
#define chppAllocServiceResponseTypedArray(requestHeader, type, count, \
                                           arrayField)                 \
  (type *)chppAllocServiceResponse(                                    \
      requestHeader,                                                   \
      sizeof(type) + (count)*sizeof_member(type, arrayField[0]))

/**
 * Uses chppAllocServiceResponse() to allocate a response message of a specific
 * type and its corresponding length.
 *
 * @param requestHeader client request header, as per
 * chppAllocServiceResponse().
 * @param type Type of response.
 *
 * @return Pointer to allocated memory
 */
#define chppAllocServiceResponseFixed(requestHeader, type) \
  (type *)chppAllocServiceResponse(requestHeader, sizeof(type))

/**
 * Maintains the basic state of a service.
 * This is expected to be included once in the (context) status variable of each
 * service.
 */
struct ChppServiceState {
  struct ChppAppState *appContext;  // Pointer to app layer context
  uint8_t handle;                   // Handle number for this service

  uint8_t openState;  // As defined in enum ChppOpenState
};

/************************************************
 *  Public functions
 ***********************************************/

/**
 * Registers common services with the CHPP app layer. These services are enabled
 * by CHPP_SERVICE_ENABLED_xxxx definitions. This function is automatically
 * called by chppAppInit().
 *
 * @param context Maintains status for each app layer instance.
 */
void chppRegisterCommonServices(struct ChppAppState *context);

/**
 * Deregisters common services with the CHPP app layer. These services are
 * enabled by CHPP_SERVICE_ENABLED_xxxx definitions. This function is
 * automatically called by chppAppInit().
 *
 * @param context Maintains status for each app layer instance.
 */
void chppDeregisterCommonServices(struct ChppAppState *context);

/**
 * Registers a new service on CHPP. This function is to be called by the
 * platform initialization code for every non-common service available on a
 * server (if any), i.e. except those that are registered through
 * chppRegisterCommonServices().
 *
 * Note that the maximum number of services that can be registered on a platform
 * can specified as CHPP_MAX_REGISTERED_SERVICES by the initialization code.
 * Otherwise, a default value will be used.
 *
 * @param appContext Maintains status for each app layer instance.
 * @param serviceContext Maintains status for each service instance.
 * @param newService The service to be registered on this platform.
 *
 * @return Handle number of the registered service.
 */
uint8_t chppRegisterService(struct ChppAppState *appContext,
                            void *serviceContext,
                            const struct ChppService *newService);

/**
 * Allocates a service notification of a specified length.
 *
 * It is expected that for most use cases, the
 * chppAllocServiceNotificationFixed() or
 * chppAllocServiceNotificationTypedArray() macros shall be used rather than
 * calling this function directly.
 *
 * @param len Length of the notification (including header) in bytes. Note
 * that the specified length must be at least equal to the lendth of the app
 * layer header.
 *
 * @return Pointer to allocated memory
 */
struct ChppAppHeader *chppAllocServiceNotification(size_t len);

/**
 * Allocates a service response message of a specified length, populating the
 * (app layer) service response header accorging to the provided client request
 * (app layer) header.
 *
 * It is expected that for most use cases, the chppAllocServiceResponseFixed()
 * or chppAllocServiceResponseTypedArray() macros shall be used rather than
 * calling this function directly.
 *
 * @param requestHeader Client request header.
 * @param len Length of the response message (including header) in bytes. Note
 * that the specified length must be at least equal to the lendth of the app
 * layer header.
 *
 * @return Pointer to allocated memory
 */
struct ChppAppHeader *chppAllocServiceResponse(
    const struct ChppAppHeader *requestHeader, size_t len);

/**
 * This function shall be called for all incoming client requests in order to
 * A) Timestamp them, and
 * B) Save their Transaction ID
 * as part of the request/response's ChppRequestResponseState struct.
 *
 * This function prints an error message if a duplicate request is received
 * while outstanding request is still pending without a response.
 *
 * @param rRState Maintains the basic state for each request/response
 * functionality of a service.
 * @param requestHeader Client request header.
 */
void chppServiceTimestampRequest(struct ChppRequestResponseState *rRState,
                                 struct ChppAppHeader *requestHeader);

/**
 * This function shall be called for the final service response to a client
 * request in order to
 * A) Timestamp them, and
 * B) Mark them as fulfilled
 * part of the request/response's ChppRequestResponseState struct.
 *
 * This function prints an error message if a response is attempted without an
 * outstanding request.
 *
 * For most responses, it is expected that chppSendTimestampedResponseOrFail()
 * shall be used to both timestamp and send the response in one shot.
 *
 * @param rRState Maintains the basic state for each request/response
 * functionality of a service.
 */
void chppServiceTimestampResponse(struct ChppRequestResponseState *rRState);

/**
 * Timestamps a service response using chppServiceTimestampResponse() and
 * enqueues it using chppEnqueueTxDatagramOrFail().
 *
 * Refer to their respective documentation for details.
 *
 * @param serviceState State of the service sending the response service.
 * @param rRState Maintains the basic state for each request/response
 * functionality of a service.
 * @param buf Datagram payload allocated through chppMalloc. Cannot be null.
 * @param len Datagram length in bytes.
 *
 * @return True informs the sender that the datagram was successfully enqueued.
 * False informs the sender that the queue was full and the payload discarded.
 */
bool chppSendTimestampedResponseOrFail(struct ChppServiceState *serviceState,
                                       struct ChppRequestResponseState *rRState,
                                       void *buf, size_t len);

#ifdef __cplusplus
}
#endif

#endif  // CHPP_SERVICES_H_