xref: /aosp_15_r20/system/chre/chpp/services.c (revision 84e339476a462649f82315436d70fd732297a399)
1*84e33947SAndroid Build Coastguard Worker /*
2*84e33947SAndroid Build Coastguard Worker  * Copyright (C) 2020 The Android Open Source Project
3*84e33947SAndroid Build Coastguard Worker  *
4*84e33947SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*84e33947SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*84e33947SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*84e33947SAndroid Build Coastguard Worker  *
8*84e33947SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*84e33947SAndroid Build Coastguard Worker  *
10*84e33947SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*84e33947SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*84e33947SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*84e33947SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*84e33947SAndroid Build Coastguard Worker  * limitations under the License.
15*84e33947SAndroid Build Coastguard Worker  */
16*84e33947SAndroid Build Coastguard Worker 
17*84e33947SAndroid Build Coastguard Worker #include "chpp/services.h"
18*84e33947SAndroid Build Coastguard Worker 
19*84e33947SAndroid Build Coastguard Worker #include <inttypes.h>
20*84e33947SAndroid Build Coastguard Worker #include <stdbool.h>
21*84e33947SAndroid Build Coastguard Worker #include <stddef.h>
22*84e33947SAndroid Build Coastguard Worker #include <stdint.h>
23*84e33947SAndroid Build Coastguard Worker 
24*84e33947SAndroid Build Coastguard Worker #include "chpp/app.h"
25*84e33947SAndroid Build Coastguard Worker #include "chpp/log.h"
26*84e33947SAndroid Build Coastguard Worker #include "chpp/macros.h"
27*84e33947SAndroid Build Coastguard Worker #include "chpp/memory.h"
28*84e33947SAndroid Build Coastguard Worker #include "chpp/mutex.h"
29*84e33947SAndroid Build Coastguard Worker #ifdef CHPP_SERVICE_ENABLED_GNSS
30*84e33947SAndroid Build Coastguard Worker #include "chpp/services/gnss.h"
31*84e33947SAndroid Build Coastguard Worker #endif
32*84e33947SAndroid Build Coastguard Worker #ifdef CHPP_SERVICE_ENABLED_WIFI
33*84e33947SAndroid Build Coastguard Worker #include "chpp/services/wifi.h"
34*84e33947SAndroid Build Coastguard Worker #endif
35*84e33947SAndroid Build Coastguard Worker #ifdef CHPP_SERVICE_ENABLED_WWAN
36*84e33947SAndroid Build Coastguard Worker #include "chpp/services/wwan.h"
37*84e33947SAndroid Build Coastguard Worker #endif
38*84e33947SAndroid Build Coastguard Worker #include "chpp/transport.h"
39*84e33947SAndroid Build Coastguard Worker 
40*84e33947SAndroid Build Coastguard Worker /************************************************
41*84e33947SAndroid Build Coastguard Worker  *  Public Functions
42*84e33947SAndroid Build Coastguard Worker  ***********************************************/
43*84e33947SAndroid Build Coastguard Worker 
chppRegisterCommonServices(struct ChppAppState * context)44*84e33947SAndroid Build Coastguard Worker void chppRegisterCommonServices(struct ChppAppState *context) {
45*84e33947SAndroid Build Coastguard Worker   CHPP_DEBUG_NOT_NULL(context);
46*84e33947SAndroid Build Coastguard Worker   UNUSED_VAR(context);
47*84e33947SAndroid Build Coastguard Worker 
48*84e33947SAndroid Build Coastguard Worker #ifdef CHPP_SERVICE_ENABLED_WWAN
49*84e33947SAndroid Build Coastguard Worker   if (context->clientServiceSet.wwanService) {
50*84e33947SAndroid Build Coastguard Worker     chppRegisterWwanService(context);
51*84e33947SAndroid Build Coastguard Worker   }
52*84e33947SAndroid Build Coastguard Worker #endif
53*84e33947SAndroid Build Coastguard Worker 
54*84e33947SAndroid Build Coastguard Worker #ifdef CHPP_SERVICE_ENABLED_WIFI
55*84e33947SAndroid Build Coastguard Worker   if (context->clientServiceSet.wifiService) {
56*84e33947SAndroid Build Coastguard Worker     chppRegisterWifiService(context);
57*84e33947SAndroid Build Coastguard Worker   }
58*84e33947SAndroid Build Coastguard Worker #endif
59*84e33947SAndroid Build Coastguard Worker 
60*84e33947SAndroid Build Coastguard Worker #ifdef CHPP_SERVICE_ENABLED_GNSS
61*84e33947SAndroid Build Coastguard Worker   if (context->clientServiceSet.gnssService) {
62*84e33947SAndroid Build Coastguard Worker     chppRegisterGnssService(context);
63*84e33947SAndroid Build Coastguard Worker   }
64*84e33947SAndroid Build Coastguard Worker #endif
65*84e33947SAndroid Build Coastguard Worker }
66*84e33947SAndroid Build Coastguard Worker 
chppDeregisterCommonServices(struct ChppAppState * context)67*84e33947SAndroid Build Coastguard Worker void chppDeregisterCommonServices(struct ChppAppState *context) {
68*84e33947SAndroid Build Coastguard Worker   CHPP_DEBUG_NOT_NULL(context);
69*84e33947SAndroid Build Coastguard Worker   UNUSED_VAR(context);
70*84e33947SAndroid Build Coastguard Worker 
71*84e33947SAndroid Build Coastguard Worker #ifdef CHPP_SERVICE_ENABLED_WWAN
72*84e33947SAndroid Build Coastguard Worker   if (context->clientServiceSet.wwanService) {
73*84e33947SAndroid Build Coastguard Worker     chppDeregisterWwanService(context);
74*84e33947SAndroid Build Coastguard Worker   }
75*84e33947SAndroid Build Coastguard Worker #endif
76*84e33947SAndroid Build Coastguard Worker 
77*84e33947SAndroid Build Coastguard Worker #ifdef CHPP_SERVICE_ENABLED_WIFI
78*84e33947SAndroid Build Coastguard Worker   if (context->clientServiceSet.wifiService) {
79*84e33947SAndroid Build Coastguard Worker     chppDeregisterWifiService(context);
80*84e33947SAndroid Build Coastguard Worker   }
81*84e33947SAndroid Build Coastguard Worker #endif
82*84e33947SAndroid Build Coastguard Worker 
83*84e33947SAndroid Build Coastguard Worker #ifdef CHPP_SERVICE_ENABLED_GNSS
84*84e33947SAndroid Build Coastguard Worker   if (context->clientServiceSet.gnssService) {
85*84e33947SAndroid Build Coastguard Worker     chppDeregisterGnssService(context);
86*84e33947SAndroid Build Coastguard Worker   }
87*84e33947SAndroid Build Coastguard Worker #endif
88*84e33947SAndroid Build Coastguard Worker }
89*84e33947SAndroid Build Coastguard Worker 
chppRegisterService(struct ChppAppState * appContext,void * serviceContext,struct ChppEndpointState * serviceState,struct ChppOutgoingRequestState * outReqStates,const struct ChppService * newService)90*84e33947SAndroid Build Coastguard Worker void chppRegisterService(struct ChppAppState *appContext, void *serviceContext,
91*84e33947SAndroid Build Coastguard Worker                          struct ChppEndpointState *serviceState,
92*84e33947SAndroid Build Coastguard Worker                          struct ChppOutgoingRequestState *outReqStates,
93*84e33947SAndroid Build Coastguard Worker                          const struct ChppService *newService) {
94*84e33947SAndroid Build Coastguard Worker   CHPP_DEBUG_NOT_NULL(appContext);
95*84e33947SAndroid Build Coastguard Worker   CHPP_DEBUG_NOT_NULL(serviceContext);
96*84e33947SAndroid Build Coastguard Worker   CHPP_DEBUG_NOT_NULL(serviceState);
97*84e33947SAndroid Build Coastguard Worker   CHPP_DEBUG_NOT_NULL(newService);
98*84e33947SAndroid Build Coastguard Worker 
99*84e33947SAndroid Build Coastguard Worker   const uint8_t numServices = appContext->registeredServiceCount;
100*84e33947SAndroid Build Coastguard Worker 
101*84e33947SAndroid Build Coastguard Worker   serviceState->openState = CHPP_OPEN_STATE_CLOSED;
102*84e33947SAndroid Build Coastguard Worker   serviceState->appContext = appContext;
103*84e33947SAndroid Build Coastguard Worker   serviceState->outReqStates = outReqStates;
104*84e33947SAndroid Build Coastguard Worker   serviceState->context = serviceContext;
105*84e33947SAndroid Build Coastguard Worker 
106*84e33947SAndroid Build Coastguard Worker   if (numServices >= CHPP_MAX_REGISTERED_SERVICES) {
107*84e33947SAndroid Build Coastguard Worker     CHPP_LOGE("Max services registered: # %" PRIu8, numServices);
108*84e33947SAndroid Build Coastguard Worker     serviceState->handle = CHPP_HANDLE_NONE;
109*84e33947SAndroid Build Coastguard Worker     return;
110*84e33947SAndroid Build Coastguard Worker   }
111*84e33947SAndroid Build Coastguard Worker 
112*84e33947SAndroid Build Coastguard Worker   serviceState->index = numServices;
113*84e33947SAndroid Build Coastguard Worker   serviceState->handle = CHPP_SERVICE_HANDLE_OF_INDEX(numServices);
114*84e33947SAndroid Build Coastguard Worker 
115*84e33947SAndroid Build Coastguard Worker   appContext->registeredServices[numServices] = newService;
116*84e33947SAndroid Build Coastguard Worker   appContext->registeredServiceStates[numServices] = serviceState;
117*84e33947SAndroid Build Coastguard Worker   appContext->registeredServiceCount++;
118*84e33947SAndroid Build Coastguard Worker 
119*84e33947SAndroid Build Coastguard Worker   chppMutexInit(&serviceState->syncResponse.mutex);
120*84e33947SAndroid Build Coastguard Worker   chppConditionVariableInit(&serviceState->syncResponse.condVar);
121*84e33947SAndroid Build Coastguard Worker 
122*84e33947SAndroid Build Coastguard Worker   char uuidText[CHPP_SERVICE_UUID_STRING_LEN];
123*84e33947SAndroid Build Coastguard Worker   chppUuidToStr(newService->descriptor.uuid, uuidText);
124*84e33947SAndroid Build Coastguard Worker   CHPP_LOGD("Registered service # %" PRIu8
125*84e33947SAndroid Build Coastguard Worker             " on handle %d"
126*84e33947SAndroid Build Coastguard Worker             " with name=%s, UUID=%s, version=%" PRIu8 ".%" PRIu8 ".%" PRIu16
127*84e33947SAndroid Build Coastguard Worker             ", min_len=%" PRIuSIZE " ",
128*84e33947SAndroid Build Coastguard Worker             numServices, serviceState->handle, newService->descriptor.name,
129*84e33947SAndroid Build Coastguard Worker             uuidText, newService->descriptor.version.major,
130*84e33947SAndroid Build Coastguard Worker             newService->descriptor.version.minor,
131*84e33947SAndroid Build Coastguard Worker             newService->descriptor.version.patch, newService->minLength);
132*84e33947SAndroid Build Coastguard Worker }
133*84e33947SAndroid Build Coastguard Worker 
chppAllocServiceNotification(size_t len)134*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *chppAllocServiceNotification(size_t len) {
135*84e33947SAndroid Build Coastguard Worker   return chppAllocNotification(CHPP_MESSAGE_TYPE_SERVICE_NOTIFICATION, len);
136*84e33947SAndroid Build Coastguard Worker }
137*84e33947SAndroid Build Coastguard Worker 
chppAllocServiceRequest(struct ChppEndpointState * serviceState,size_t len)138*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *chppAllocServiceRequest(
139*84e33947SAndroid Build Coastguard Worker     struct ChppEndpointState *serviceState, size_t len) {
140*84e33947SAndroid Build Coastguard Worker   CHPP_DEBUG_NOT_NULL(serviceState);
141*84e33947SAndroid Build Coastguard Worker   return chppAllocRequest(CHPP_MESSAGE_TYPE_SERVICE_REQUEST, serviceState, len);
142*84e33947SAndroid Build Coastguard Worker }
143*84e33947SAndroid Build Coastguard Worker 
chppAllocServiceRequestCommand(struct ChppEndpointState * serviceState,uint16_t command)144*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *chppAllocServiceRequestCommand(
145*84e33947SAndroid Build Coastguard Worker     struct ChppEndpointState *serviceState, uint16_t command) {
146*84e33947SAndroid Build Coastguard Worker   struct ChppAppHeader *request =
147*84e33947SAndroid Build Coastguard Worker       chppAllocServiceRequest(serviceState, sizeof(struct ChppAppHeader));
148*84e33947SAndroid Build Coastguard Worker 
149*84e33947SAndroid Build Coastguard Worker   if (request != NULL) {
150*84e33947SAndroid Build Coastguard Worker     request->command = command;
151*84e33947SAndroid Build Coastguard Worker   }
152*84e33947SAndroid Build Coastguard Worker   return request;
153*84e33947SAndroid Build Coastguard Worker }
154*84e33947SAndroid Build Coastguard Worker 
chppServiceSendTimestampedRequestOrFail(struct ChppEndpointState * serviceState,struct ChppOutgoingRequestState * outReqState,void * buf,size_t len,uint64_t timeoutNs)155*84e33947SAndroid Build Coastguard Worker bool chppServiceSendTimestampedRequestOrFail(
156*84e33947SAndroid Build Coastguard Worker     struct ChppEndpointState *serviceState,
157*84e33947SAndroid Build Coastguard Worker     struct ChppOutgoingRequestState *outReqState, void *buf, size_t len,
158*84e33947SAndroid Build Coastguard Worker     uint64_t timeoutNs) {
159*84e33947SAndroid Build Coastguard Worker   return chppSendTimestampedRequestOrFail(serviceState, outReqState, buf, len,
160*84e33947SAndroid Build Coastguard Worker                                           timeoutNs);
161*84e33947SAndroid Build Coastguard Worker }
162*84e33947SAndroid Build Coastguard Worker 
chppServiceSendTimestampedRequestAndWait(struct ChppEndpointState * serviceState,struct ChppOutgoingRequestState * outReqState,void * buf,size_t len)163*84e33947SAndroid Build Coastguard Worker bool chppServiceSendTimestampedRequestAndWait(
164*84e33947SAndroid Build Coastguard Worker     struct ChppEndpointState *serviceState,
165*84e33947SAndroid Build Coastguard Worker     struct ChppOutgoingRequestState *outReqState, void *buf, size_t len) {
166*84e33947SAndroid Build Coastguard Worker   return chppServiceSendTimestampedRequestAndWaitTimeout(
167*84e33947SAndroid Build Coastguard Worker       serviceState, outReqState, buf, len, CHPP_REQUEST_TIMEOUT_DEFAULT);
168*84e33947SAndroid Build Coastguard Worker }
169*84e33947SAndroid Build Coastguard Worker 
chppServiceSendTimestampedRequestAndWaitTimeout(struct ChppEndpointState * serviceState,struct ChppOutgoingRequestState * outReqState,void * buf,size_t len,uint64_t timeoutNs)170*84e33947SAndroid Build Coastguard Worker bool chppServiceSendTimestampedRequestAndWaitTimeout(
171*84e33947SAndroid Build Coastguard Worker     struct ChppEndpointState *serviceState,
172*84e33947SAndroid Build Coastguard Worker     struct ChppOutgoingRequestState *outReqState, void *buf, size_t len,
173*84e33947SAndroid Build Coastguard Worker     uint64_t timeoutNs) {
174*84e33947SAndroid Build Coastguard Worker   CHPP_DEBUG_NOT_NULL(serviceState);
175*84e33947SAndroid Build Coastguard Worker 
176*84e33947SAndroid Build Coastguard Worker   bool result = chppServiceSendTimestampedRequestOrFail(
177*84e33947SAndroid Build Coastguard Worker       serviceState, outReqState, buf, len, CHPP_REQUEST_TIMEOUT_INFINITE);
178*84e33947SAndroid Build Coastguard Worker 
179*84e33947SAndroid Build Coastguard Worker   if (!result) {
180*84e33947SAndroid Build Coastguard Worker     return false;
181*84e33947SAndroid Build Coastguard Worker   }
182*84e33947SAndroid Build Coastguard Worker 
183*84e33947SAndroid Build Coastguard Worker   return chppWaitForResponseWithTimeout(&serviceState->syncResponse,
184*84e33947SAndroid Build Coastguard Worker                                         outReqState, timeoutNs);
185*84e33947SAndroid Build Coastguard Worker }
186*84e33947SAndroid Build Coastguard Worker 
chppServiceCloseOpenRequests(struct ChppEndpointState * serviceState,const struct ChppService * service,bool clearOnly)187*84e33947SAndroid Build Coastguard Worker void chppServiceCloseOpenRequests(struct ChppEndpointState *serviceState,
188*84e33947SAndroid Build Coastguard Worker                                   const struct ChppService *service,
189*84e33947SAndroid Build Coastguard Worker                                   bool clearOnly) {
190*84e33947SAndroid Build Coastguard Worker   UNUSED_VAR(service);
191*84e33947SAndroid Build Coastguard Worker   chppCloseOpenRequests(serviceState, CHPP_ENDPOINT_SERVICE, clearOnly);
192*84e33947SAndroid Build Coastguard Worker }