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/wifi.h"
18*84e33947SAndroid Build Coastguard Worker
19*84e33947SAndroid Build Coastguard Worker #include <inttypes.h>
20*84e33947SAndroid Build Coastguard Worker #include <stddef.h>
21*84e33947SAndroid Build Coastguard Worker #include <stdint.h>
22*84e33947SAndroid Build Coastguard Worker
23*84e33947SAndroid Build Coastguard Worker #include "chpp/common/standard_uuids.h"
24*84e33947SAndroid Build Coastguard Worker #include "chpp/common/wifi.h"
25*84e33947SAndroid Build Coastguard Worker #include "chpp/common/wifi_types.h"
26*84e33947SAndroid Build Coastguard Worker #include "chpp/common/wifi_utils.h"
27*84e33947SAndroid Build Coastguard Worker #include "chpp/log.h"
28*84e33947SAndroid Build Coastguard Worker #include "chpp/macros.h"
29*84e33947SAndroid Build Coastguard Worker #include "chpp/services.h"
30*84e33947SAndroid Build Coastguard Worker #include "chre/pal/wifi.h"
31*84e33947SAndroid Build Coastguard Worker
32*84e33947SAndroid Build Coastguard Worker /************************************************
33*84e33947SAndroid Build Coastguard Worker * Prototypes
34*84e33947SAndroid Build Coastguard Worker ***********************************************/
35*84e33947SAndroid Build Coastguard Worker
36*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppDispatchWifiRequest(void *serviceContext,
37*84e33947SAndroid Build Coastguard Worker uint8_t *buf, size_t len);
38*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceNotifyReset(void *serviceContext);
39*84e33947SAndroid Build Coastguard Worker
40*84e33947SAndroid Build Coastguard Worker /************************************************
41*84e33947SAndroid Build Coastguard Worker * Private Definitions
42*84e33947SAndroid Build Coastguard Worker ***********************************************/
43*84e33947SAndroid Build Coastguard Worker
44*84e33947SAndroid Build Coastguard Worker /**
45*84e33947SAndroid Build Coastguard Worker * Configuration parameters for this service
46*84e33947SAndroid Build Coastguard Worker */
47*84e33947SAndroid Build Coastguard Worker static const struct ChppService kWifiServiceConfig = {
48*84e33947SAndroid Build Coastguard Worker .descriptor.uuid = CHPP_UUID_WIFI_STANDARD,
49*84e33947SAndroid Build Coastguard Worker
50*84e33947SAndroid Build Coastguard Worker // Human-readable name
51*84e33947SAndroid Build Coastguard Worker .descriptor.name = "WiFi",
52*84e33947SAndroid Build Coastguard Worker
53*84e33947SAndroid Build Coastguard Worker // Version
54*84e33947SAndroid Build Coastguard Worker .descriptor.version.major = 1,
55*84e33947SAndroid Build Coastguard Worker .descriptor.version.minor = 0,
56*84e33947SAndroid Build Coastguard Worker .descriptor.version.patch = 0,
57*84e33947SAndroid Build Coastguard Worker
58*84e33947SAndroid Build Coastguard Worker // Notifies service if CHPP is reset
59*84e33947SAndroid Build Coastguard Worker .resetNotifierFunctionPtr = &chppWifiServiceNotifyReset,
60*84e33947SAndroid Build Coastguard Worker
61*84e33947SAndroid Build Coastguard Worker // Client request dispatch function pointer
62*84e33947SAndroid Build Coastguard Worker .requestDispatchFunctionPtr = &chppDispatchWifiRequest,
63*84e33947SAndroid Build Coastguard Worker
64*84e33947SAndroid Build Coastguard Worker // Client notification dispatch function pointer
65*84e33947SAndroid Build Coastguard Worker .notificationDispatchFunctionPtr = NULL, // Not supported
66*84e33947SAndroid Build Coastguard Worker
67*84e33947SAndroid Build Coastguard Worker // Min length is the entire header
68*84e33947SAndroid Build Coastguard Worker .minLength = sizeof(struct ChppAppHeader),
69*84e33947SAndroid Build Coastguard Worker };
70*84e33947SAndroid Build Coastguard Worker
71*84e33947SAndroid Build Coastguard Worker /**
72*84e33947SAndroid Build Coastguard Worker * Structure to maintain state for the WiFi service and its Request/Response
73*84e33947SAndroid Build Coastguard Worker * (RR) functionality.
74*84e33947SAndroid Build Coastguard Worker */
75*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState {
76*84e33947SAndroid Build Coastguard Worker struct ChppEndpointState service; // CHPP service state
77*84e33947SAndroid Build Coastguard Worker const struct chrePalWifiApi *api; // WiFi PAL API
78*84e33947SAndroid Build Coastguard Worker
79*84e33947SAndroid Build Coastguard Worker // Based on chre/pal/wifi.h and chrePalWifiApi
80*84e33947SAndroid Build Coastguard Worker struct ChppIncomingRequestState open; // Service init state
81*84e33947SAndroid Build Coastguard Worker struct ChppIncomingRequestState close; // Service deinit state
82*84e33947SAndroid Build Coastguard Worker struct ChppIncomingRequestState getCapabilities; // Get Capabilities state
83*84e33947SAndroid Build Coastguard Worker struct ChppIncomingRequestState
84*84e33947SAndroid Build Coastguard Worker configureScanMonitorAsync; // Configure scan monitor state
85*84e33947SAndroid Build Coastguard Worker struct ChppIncomingRequestState requestScanAsync; // Request scan state
86*84e33947SAndroid Build Coastguard Worker struct ChppIncomingRequestState requestRangingAsync; // Request ranging state
87*84e33947SAndroid Build Coastguard Worker struct ChppIncomingRequestState
88*84e33947SAndroid Build Coastguard Worker requestNanSubscribe; // Request Nan Subscription state
89*84e33947SAndroid Build Coastguard Worker struct ChppIncomingRequestState
90*84e33947SAndroid Build Coastguard Worker requestNanSubscribeCancel; // Request Nan Subscription cancelation state
91*84e33947SAndroid Build Coastguard Worker struct ChppIncomingRequestState
92*84e33947SAndroid Build Coastguard Worker requestNanRangingAsync; // Request NAN ranging state
93*84e33947SAndroid Build Coastguard Worker };
94*84e33947SAndroid Build Coastguard Worker
95*84e33947SAndroid Build Coastguard Worker // Note: The CHRE PAL API only allows for one definition - see comment in WWAN
96*84e33947SAndroid Build Coastguard Worker // service for details.
97*84e33947SAndroid Build Coastguard Worker // Note: There is no notion of a cookie in the CHRE WiFi API so we need to use
98*84e33947SAndroid Build Coastguard Worker // the global service state (gWifiServiceContext) directly in all callbacks.
99*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState gWifiServiceContext;
100*84e33947SAndroid Build Coastguard Worker
101*84e33947SAndroid Build Coastguard Worker /************************************************
102*84e33947SAndroid Build Coastguard Worker * Prototypes
103*84e33947SAndroid Build Coastguard Worker ***********************************************/
104*84e33947SAndroid Build Coastguard Worker
105*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppWifiServiceOpen(
106*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
107*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader);
108*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppWifiServiceClose(
109*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
110*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader);
111*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppWifiServiceGetCapabilities(
112*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
113*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader);
114*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppWifiServiceConfigureScanMonitorAsync(
115*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
116*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len);
117*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppWifiServiceRequestScanAsync(
118*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
119*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len);
120*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppWifiServiceRequestRangingAsync(
121*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
122*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len);
123*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppWifiServiceRequestNanSubscribe(
124*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
125*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len);
126*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppWifiServiceRequestNanSubscribeCancel(
127*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
128*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len);
129*84e33947SAndroid Build Coastguard Worker static bool chppWifiServiceRequestNanRanging(
130*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
131*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len);
132*84e33947SAndroid Build Coastguard Worker
133*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceScanMonitorStatusChangeCallback(bool enabled,
134*84e33947SAndroid Build Coastguard Worker uint8_t errorCode);
135*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceScanResponseCallback(bool pending,
136*84e33947SAndroid Build Coastguard Worker uint8_t errorCode);
137*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceScanEventCallback(struct chreWifiScanEvent *event);
138*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceRangingEventCallback(
139*84e33947SAndroid Build Coastguard Worker uint8_t errorCode, struct chreWifiRangingEvent *event);
140*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceNanIdentifierCallback(uint8_t errorCode,
141*84e33947SAndroid Build Coastguard Worker uint32_t subscriptionId);
142*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceNanDiscoveryCallback(
143*84e33947SAndroid Build Coastguard Worker struct chreWifiNanDiscoveryEvent *event);
144*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceNanLostCallback(uint32_t subscriptionId,
145*84e33947SAndroid Build Coastguard Worker uint32_t publisherId);
146*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceNanTerminatedCallback(uint32_t reason,
147*84e33947SAndroid Build Coastguard Worker uint32_t subscriptionId);
148*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceNanSubscriptionCanceledCallback(
149*84e33947SAndroid Build Coastguard Worker uint8_t errorCode, uint32_t subscriptionId);
150*84e33947SAndroid Build Coastguard Worker
151*84e33947SAndroid Build Coastguard Worker /************************************************
152*84e33947SAndroid Build Coastguard Worker * Private Functions
153*84e33947SAndroid Build Coastguard Worker ***********************************************/
154*84e33947SAndroid Build Coastguard Worker
155*84e33947SAndroid Build Coastguard Worker /**
156*84e33947SAndroid Build Coastguard Worker * Dispatches a client request from the transport layer that is determined to be
157*84e33947SAndroid Build Coastguard Worker * for the WiFi service. If the result of the dispatch is an error, this
158*84e33947SAndroid Build Coastguard Worker * function responds to the client with the same error.
159*84e33947SAndroid Build Coastguard Worker *
160*84e33947SAndroid Build Coastguard Worker * This function is called from the app layer using its function pointer given
161*84e33947SAndroid Build Coastguard Worker * during service registration.
162*84e33947SAndroid Build Coastguard Worker *
163*84e33947SAndroid Build Coastguard Worker * @param serviceContext Maintains status for each service instance.
164*84e33947SAndroid Build Coastguard Worker * @param buf Input data. Cannot be null.
165*84e33947SAndroid Build Coastguard Worker * @param len Length of input data in bytes.
166*84e33947SAndroid Build Coastguard Worker *
167*84e33947SAndroid Build Coastguard Worker * @return Indicates the result of this function call.
168*84e33947SAndroid Build Coastguard Worker */
chppDispatchWifiRequest(void * serviceContext,uint8_t * buf,size_t len)169*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppDispatchWifiRequest(void *serviceContext,
170*84e33947SAndroid Build Coastguard Worker uint8_t *buf, size_t len) {
171*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;
172*84e33947SAndroid Build Coastguard Worker buf += sizeof(struct ChppAppHeader);
173*84e33947SAndroid Build Coastguard Worker len -= sizeof(struct ChppAppHeader);
174*84e33947SAndroid Build Coastguard Worker
175*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext =
176*84e33947SAndroid Build Coastguard Worker (struct ChppWifiServiceState *)serviceContext;
177*84e33947SAndroid Build Coastguard Worker struct ChppIncomingRequestState *inReqState = NULL;
178*84e33947SAndroid Build Coastguard Worker enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;
179*84e33947SAndroid Build Coastguard Worker bool dispatched = true;
180*84e33947SAndroid Build Coastguard Worker
181*84e33947SAndroid Build Coastguard Worker switch (rxHeader->command) {
182*84e33947SAndroid Build Coastguard Worker case CHPP_WIFI_OPEN: {
183*84e33947SAndroid Build Coastguard Worker inReqState = &wifiServiceContext->open;
184*84e33947SAndroid Build Coastguard Worker chppTimestampIncomingRequest(inReqState, rxHeader);
185*84e33947SAndroid Build Coastguard Worker error = chppWifiServiceOpen(wifiServiceContext, rxHeader);
186*84e33947SAndroid Build Coastguard Worker break;
187*84e33947SAndroid Build Coastguard Worker }
188*84e33947SAndroid Build Coastguard Worker
189*84e33947SAndroid Build Coastguard Worker case CHPP_WIFI_CLOSE: {
190*84e33947SAndroid Build Coastguard Worker inReqState = &wifiServiceContext->close;
191*84e33947SAndroid Build Coastguard Worker chppTimestampIncomingRequest(inReqState, rxHeader);
192*84e33947SAndroid Build Coastguard Worker error = chppWifiServiceClose(wifiServiceContext, rxHeader);
193*84e33947SAndroid Build Coastguard Worker break;
194*84e33947SAndroid Build Coastguard Worker }
195*84e33947SAndroid Build Coastguard Worker
196*84e33947SAndroid Build Coastguard Worker case CHPP_WIFI_GET_CAPABILITIES: {
197*84e33947SAndroid Build Coastguard Worker inReqState = &wifiServiceContext->getCapabilities;
198*84e33947SAndroid Build Coastguard Worker chppTimestampIncomingRequest(inReqState, rxHeader);
199*84e33947SAndroid Build Coastguard Worker error = chppWifiServiceGetCapabilities(wifiServiceContext, rxHeader);
200*84e33947SAndroid Build Coastguard Worker break;
201*84e33947SAndroid Build Coastguard Worker }
202*84e33947SAndroid Build Coastguard Worker
203*84e33947SAndroid Build Coastguard Worker case CHPP_WIFI_CONFIGURE_SCAN_MONITOR_ASYNC: {
204*84e33947SAndroid Build Coastguard Worker inReqState = &wifiServiceContext->configureScanMonitorAsync;
205*84e33947SAndroid Build Coastguard Worker chppTimestampIncomingRequest(inReqState, rxHeader);
206*84e33947SAndroid Build Coastguard Worker error = chppWifiServiceConfigureScanMonitorAsync(wifiServiceContext,
207*84e33947SAndroid Build Coastguard Worker rxHeader, buf, len);
208*84e33947SAndroid Build Coastguard Worker break;
209*84e33947SAndroid Build Coastguard Worker }
210*84e33947SAndroid Build Coastguard Worker
211*84e33947SAndroid Build Coastguard Worker case CHPP_WIFI_REQUEST_SCAN_ASYNC: {
212*84e33947SAndroid Build Coastguard Worker inReqState = &wifiServiceContext->requestScanAsync;
213*84e33947SAndroid Build Coastguard Worker chppTimestampIncomingRequest(inReqState, rxHeader);
214*84e33947SAndroid Build Coastguard Worker error = chppWifiServiceRequestScanAsync(wifiServiceContext, rxHeader, buf,
215*84e33947SAndroid Build Coastguard Worker len);
216*84e33947SAndroid Build Coastguard Worker break;
217*84e33947SAndroid Build Coastguard Worker }
218*84e33947SAndroid Build Coastguard Worker
219*84e33947SAndroid Build Coastguard Worker case CHPP_WIFI_REQUEST_RANGING_ASYNC: {
220*84e33947SAndroid Build Coastguard Worker inReqState = &wifiServiceContext->requestRangingAsync;
221*84e33947SAndroid Build Coastguard Worker chppTimestampIncomingRequest(inReqState, rxHeader);
222*84e33947SAndroid Build Coastguard Worker error = chppWifiServiceRequestRangingAsync(wifiServiceContext, rxHeader,
223*84e33947SAndroid Build Coastguard Worker buf, len);
224*84e33947SAndroid Build Coastguard Worker break;
225*84e33947SAndroid Build Coastguard Worker }
226*84e33947SAndroid Build Coastguard Worker
227*84e33947SAndroid Build Coastguard Worker case CHPP_WIFI_REQUEST_NAN_SUB: {
228*84e33947SAndroid Build Coastguard Worker inReqState = &wifiServiceContext->requestNanSubscribe;
229*84e33947SAndroid Build Coastguard Worker chppTimestampIncomingRequest(inReqState, rxHeader);
230*84e33947SAndroid Build Coastguard Worker error = chppWifiServiceRequestNanSubscribe(wifiServiceContext, rxHeader,
231*84e33947SAndroid Build Coastguard Worker buf, len);
232*84e33947SAndroid Build Coastguard Worker break;
233*84e33947SAndroid Build Coastguard Worker }
234*84e33947SAndroid Build Coastguard Worker
235*84e33947SAndroid Build Coastguard Worker case CHPP_WIFI_REQUEST_NAN_SUB_CANCEL: {
236*84e33947SAndroid Build Coastguard Worker inReqState = &wifiServiceContext->requestNanSubscribeCancel;
237*84e33947SAndroid Build Coastguard Worker chppTimestampIncomingRequest(inReqState, rxHeader);
238*84e33947SAndroid Build Coastguard Worker error = chppWifiServiceRequestNanSubscribeCancel(wifiServiceContext,
239*84e33947SAndroid Build Coastguard Worker rxHeader, buf, len);
240*84e33947SAndroid Build Coastguard Worker break;
241*84e33947SAndroid Build Coastguard Worker };
242*84e33947SAndroid Build Coastguard Worker
243*84e33947SAndroid Build Coastguard Worker case CHPP_WIFI_REQUEST_NAN_RANGING_ASYNC: {
244*84e33947SAndroid Build Coastguard Worker inReqState = &wifiServiceContext->requestNanRangingAsync;
245*84e33947SAndroid Build Coastguard Worker chppTimestampIncomingRequest(inReqState, rxHeader);
246*84e33947SAndroid Build Coastguard Worker error = chppWifiServiceRequestNanRanging(wifiServiceContext, rxHeader,
247*84e33947SAndroid Build Coastguard Worker buf, len);
248*84e33947SAndroid Build Coastguard Worker break;
249*84e33947SAndroid Build Coastguard Worker }
250*84e33947SAndroid Build Coastguard Worker
251*84e33947SAndroid Build Coastguard Worker default: {
252*84e33947SAndroid Build Coastguard Worker dispatched = false;
253*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_INVALID_COMMAND;
254*84e33947SAndroid Build Coastguard Worker break;
255*84e33947SAndroid Build Coastguard Worker }
256*84e33947SAndroid Build Coastguard Worker }
257*84e33947SAndroid Build Coastguard Worker
258*84e33947SAndroid Build Coastguard Worker if (dispatched == true && error != CHPP_APP_ERROR_NONE) {
259*84e33947SAndroid Build Coastguard Worker // Request was dispatched but an error was returned. Close out
260*84e33947SAndroid Build Coastguard Worker // chppTimestampIncomingRequest()
261*84e33947SAndroid Build Coastguard Worker chppTimestampOutgoingResponse(inReqState);
262*84e33947SAndroid Build Coastguard Worker }
263*84e33947SAndroid Build Coastguard Worker
264*84e33947SAndroid Build Coastguard Worker return error;
265*84e33947SAndroid Build Coastguard Worker }
266*84e33947SAndroid Build Coastguard Worker
267*84e33947SAndroid Build Coastguard Worker /**
268*84e33947SAndroid Build Coastguard Worker * Initializes the WiFi service upon an open request from the client and
269*84e33947SAndroid Build Coastguard Worker * responds to the client with the result.
270*84e33947SAndroid Build Coastguard Worker *
271*84e33947SAndroid Build Coastguard Worker * @param serviceContext Maintains status for each service instance.
272*84e33947SAndroid Build Coastguard Worker * @param requestHeader App layer header of the request.
273*84e33947SAndroid Build Coastguard Worker *
274*84e33947SAndroid Build Coastguard Worker * @return Indicates the result of this function call.
275*84e33947SAndroid Build Coastguard Worker */
chppWifiServiceOpen(struct ChppWifiServiceState * wifiServiceContext,struct ChppAppHeader * requestHeader)276*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppWifiServiceOpen(
277*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
278*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader) {
279*84e33947SAndroid Build Coastguard Worker static const struct chrePalWifiCallbacks palCallbacks = {
280*84e33947SAndroid Build Coastguard Worker .scanMonitorStatusChangeCallback =
281*84e33947SAndroid Build Coastguard Worker chppWifiServiceScanMonitorStatusChangeCallback,
282*84e33947SAndroid Build Coastguard Worker .scanResponseCallback = chppWifiServiceScanResponseCallback,
283*84e33947SAndroid Build Coastguard Worker .scanEventCallback = chppWifiServiceScanEventCallback,
284*84e33947SAndroid Build Coastguard Worker .rangingEventCallback = chppWifiServiceRangingEventCallback,
285*84e33947SAndroid Build Coastguard Worker .nanServiceIdentifierCallback = chppWifiServiceNanIdentifierCallback,
286*84e33947SAndroid Build Coastguard Worker .nanServiceDiscoveryCallback = chppWifiServiceNanDiscoveryCallback,
287*84e33947SAndroid Build Coastguard Worker .nanServiceLostCallback = chppWifiServiceNanLostCallback,
288*84e33947SAndroid Build Coastguard Worker .nanServiceTerminatedCallback = chppWifiServiceNanTerminatedCallback,
289*84e33947SAndroid Build Coastguard Worker .nanSubscriptionCanceledCallback =
290*84e33947SAndroid Build Coastguard Worker chppWifiServiceNanSubscriptionCanceledCallback,
291*84e33947SAndroid Build Coastguard Worker };
292*84e33947SAndroid Build Coastguard Worker
293*84e33947SAndroid Build Coastguard Worker enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;
294*84e33947SAndroid Build Coastguard Worker
295*84e33947SAndroid Build Coastguard Worker if (wifiServiceContext->service.openState == CHPP_OPEN_STATE_OPENED) {
296*84e33947SAndroid Build Coastguard Worker CHPP_DEBUG_ASSERT_LOG(false, "WiFi service already open");
297*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_INVALID_COMMAND;
298*84e33947SAndroid Build Coastguard Worker
299*84e33947SAndroid Build Coastguard Worker } else if (!wifiServiceContext->api->open(
300*84e33947SAndroid Build Coastguard Worker wifiServiceContext->service.appContext->systemApi,
301*84e33947SAndroid Build Coastguard Worker &palCallbacks)) {
302*84e33947SAndroid Build Coastguard Worker CHPP_DEBUG_ASSERT_LOG(false, "WiFi PAL open failed");
303*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_BEYOND_CHPP;
304*84e33947SAndroid Build Coastguard Worker
305*84e33947SAndroid Build Coastguard Worker } else {
306*84e33947SAndroid Build Coastguard Worker CHPP_LOGD("WiFi service opened");
307*84e33947SAndroid Build Coastguard Worker wifiServiceContext->service.openState = CHPP_OPEN_STATE_OPENED;
308*84e33947SAndroid Build Coastguard Worker
309*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *response =
310*84e33947SAndroid Build Coastguard Worker chppAllocResponseFixed(requestHeader, struct ChppAppHeader);
311*84e33947SAndroid Build Coastguard Worker size_t responseLen = sizeof(*response);
312*84e33947SAndroid Build Coastguard Worker
313*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
314*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
315*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_OOM;
316*84e33947SAndroid Build Coastguard Worker } else {
317*84e33947SAndroid Build Coastguard Worker chppSendTimestampedResponseOrFail(wifiServiceContext->service.appContext,
318*84e33947SAndroid Build Coastguard Worker &wifiServiceContext->open, response,
319*84e33947SAndroid Build Coastguard Worker responseLen);
320*84e33947SAndroid Build Coastguard Worker }
321*84e33947SAndroid Build Coastguard Worker }
322*84e33947SAndroid Build Coastguard Worker
323*84e33947SAndroid Build Coastguard Worker return error;
324*84e33947SAndroid Build Coastguard Worker }
325*84e33947SAndroid Build Coastguard Worker
326*84e33947SAndroid Build Coastguard Worker /**
327*84e33947SAndroid Build Coastguard Worker * Deinitializes the WiFi service.
328*84e33947SAndroid Build Coastguard Worker *
329*84e33947SAndroid Build Coastguard Worker * @param serviceContext Maintains status for each service instance.
330*84e33947SAndroid Build Coastguard Worker * @param requestHeader App layer header of the request.
331*84e33947SAndroid Build Coastguard Worker *
332*84e33947SAndroid Build Coastguard Worker * @return Indicates the result of this function call.
333*84e33947SAndroid Build Coastguard Worker */
chppWifiServiceClose(struct ChppWifiServiceState * wifiServiceContext,struct ChppAppHeader * requestHeader)334*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppWifiServiceClose(
335*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
336*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader) {
337*84e33947SAndroid Build Coastguard Worker enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;
338*84e33947SAndroid Build Coastguard Worker
339*84e33947SAndroid Build Coastguard Worker wifiServiceContext->api->close();
340*84e33947SAndroid Build Coastguard Worker wifiServiceContext->service.openState = CHPP_OPEN_STATE_CLOSED;
341*84e33947SAndroid Build Coastguard Worker
342*84e33947SAndroid Build Coastguard Worker CHPP_LOGD("WiFi service closed");
343*84e33947SAndroid Build Coastguard Worker
344*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *response =
345*84e33947SAndroid Build Coastguard Worker chppAllocResponseFixed(requestHeader, struct ChppAppHeader);
346*84e33947SAndroid Build Coastguard Worker size_t responseLen = sizeof(*response);
347*84e33947SAndroid Build Coastguard Worker
348*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
349*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
350*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_OOM;
351*84e33947SAndroid Build Coastguard Worker } else {
352*84e33947SAndroid Build Coastguard Worker chppSendTimestampedResponseOrFail(wifiServiceContext->service.appContext,
353*84e33947SAndroid Build Coastguard Worker &wifiServiceContext->close, response,
354*84e33947SAndroid Build Coastguard Worker responseLen);
355*84e33947SAndroid Build Coastguard Worker }
356*84e33947SAndroid Build Coastguard Worker return error;
357*84e33947SAndroid Build Coastguard Worker }
358*84e33947SAndroid Build Coastguard Worker
359*84e33947SAndroid Build Coastguard Worker /**
360*84e33947SAndroid Build Coastguard Worker * Notifies the service of an incoming reset.
361*84e33947SAndroid Build Coastguard Worker *
362*84e33947SAndroid Build Coastguard Worker * @param serviceContext Maintains status for each service instance.
363*84e33947SAndroid Build Coastguard Worker */
chppWifiServiceNotifyReset(void * serviceContext)364*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceNotifyReset(void *serviceContext) {
365*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext =
366*84e33947SAndroid Build Coastguard Worker (struct ChppWifiServiceState *)serviceContext;
367*84e33947SAndroid Build Coastguard Worker
368*84e33947SAndroid Build Coastguard Worker if (wifiServiceContext->service.openState != CHPP_OPEN_STATE_OPENED) {
369*84e33947SAndroid Build Coastguard Worker CHPP_LOGW("WiFi service reset but wasn't open");
370*84e33947SAndroid Build Coastguard Worker } else {
371*84e33947SAndroid Build Coastguard Worker CHPP_LOGD("WiFi service reset. Closing");
372*84e33947SAndroid Build Coastguard Worker wifiServiceContext->service.openState = CHPP_OPEN_STATE_CLOSED;
373*84e33947SAndroid Build Coastguard Worker wifiServiceContext->api->close();
374*84e33947SAndroid Build Coastguard Worker }
375*84e33947SAndroid Build Coastguard Worker }
376*84e33947SAndroid Build Coastguard Worker
377*84e33947SAndroid Build Coastguard Worker /**
378*84e33947SAndroid Build Coastguard Worker * Retrieves a set of flags indicating the WiFi features supported by the
379*84e33947SAndroid Build Coastguard Worker * current implementation.
380*84e33947SAndroid Build Coastguard Worker *
381*84e33947SAndroid Build Coastguard Worker * @param serviceContext Maintains status for each service instance.
382*84e33947SAndroid Build Coastguard Worker * @param requestHeader App layer header of the request.
383*84e33947SAndroid Build Coastguard Worker *
384*84e33947SAndroid Build Coastguard Worker * @return Indicates the result of this function call.
385*84e33947SAndroid Build Coastguard Worker */
chppWifiServiceGetCapabilities(struct ChppWifiServiceState * wifiServiceContext,struct ChppAppHeader * requestHeader)386*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppWifiServiceGetCapabilities(
387*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
388*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader) {
389*84e33947SAndroid Build Coastguard Worker enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;
390*84e33947SAndroid Build Coastguard Worker
391*84e33947SAndroid Build Coastguard Worker struct ChppWifiGetCapabilitiesResponse *response = chppAllocResponseFixed(
392*84e33947SAndroid Build Coastguard Worker requestHeader, struct ChppWifiGetCapabilitiesResponse);
393*84e33947SAndroid Build Coastguard Worker size_t responseLen = sizeof(*response);
394*84e33947SAndroid Build Coastguard Worker
395*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
396*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
397*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_OOM;
398*84e33947SAndroid Build Coastguard Worker } else {
399*84e33947SAndroid Build Coastguard Worker response->params.capabilities = wifiServiceContext->api->getCapabilities();
400*84e33947SAndroid Build Coastguard Worker
401*84e33947SAndroid Build Coastguard Worker CHPP_LOGD("chppWifiServiceGetCapabilities returning 0x%" PRIx32
402*84e33947SAndroid Build Coastguard Worker ", %" PRIuSIZE " bytes",
403*84e33947SAndroid Build Coastguard Worker response->params.capabilities, responseLen);
404*84e33947SAndroid Build Coastguard Worker chppSendTimestampedResponseOrFail(wifiServiceContext->service.appContext,
405*84e33947SAndroid Build Coastguard Worker &wifiServiceContext->getCapabilities,
406*84e33947SAndroid Build Coastguard Worker response, responseLen);
407*84e33947SAndroid Build Coastguard Worker }
408*84e33947SAndroid Build Coastguard Worker
409*84e33947SAndroid Build Coastguard Worker return error;
410*84e33947SAndroid Build Coastguard Worker }
411*84e33947SAndroid Build Coastguard Worker
412*84e33947SAndroid Build Coastguard Worker /**
413*84e33947SAndroid Build Coastguard Worker * Configures whether scanEventCallback receives unsolicited scan results, i.e.
414*84e33947SAndroid Build Coastguard Worker * the results of scans not performed at the request of CHRE.
415*84e33947SAndroid Build Coastguard Worker *
416*84e33947SAndroid Build Coastguard Worker * This function returns an error code synchronously.
417*84e33947SAndroid Build Coastguard Worker * A subsequent call to chppWifiServiceScanMonitorStatusChangeCallback() will be
418*84e33947SAndroid Build Coastguard Worker * used to communicate the result of this request (as a service response).
419*84e33947SAndroid Build Coastguard Worker *
420*84e33947SAndroid Build Coastguard Worker * @param serviceContext Maintains status for each service instance.
421*84e33947SAndroid Build Coastguard Worker * @param requestHeader App layer header of the request.
422*84e33947SAndroid Build Coastguard Worker * @param buf Input data. Cannot be null.
423*84e33947SAndroid Build Coastguard Worker * @param len Length of input data in bytes.
424*84e33947SAndroid Build Coastguard Worker *
425*84e33947SAndroid Build Coastguard Worker * @return Indicates the result of this function call.
426*84e33947SAndroid Build Coastguard Worker */
chppWifiServiceConfigureScanMonitorAsync(struct ChppWifiServiceState * wifiServiceContext,struct ChppAppHeader * requestHeader,uint8_t * buf,size_t len)427*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppWifiServiceConfigureScanMonitorAsync(
428*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
429*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len) {
430*84e33947SAndroid Build Coastguard Worker UNUSED_VAR(requestHeader);
431*84e33947SAndroid Build Coastguard Worker enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;
432*84e33947SAndroid Build Coastguard Worker
433*84e33947SAndroid Build Coastguard Worker if (len < sizeof(bool)) {
434*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_INVALID_ARG;
435*84e33947SAndroid Build Coastguard Worker } else {
436*84e33947SAndroid Build Coastguard Worker bool *enable = (bool *)buf;
437*84e33947SAndroid Build Coastguard Worker if (!wifiServiceContext->api->configureScanMonitor(*enable)) {
438*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_UNSPECIFIED;
439*84e33947SAndroid Build Coastguard Worker }
440*84e33947SAndroid Build Coastguard Worker }
441*84e33947SAndroid Build Coastguard Worker
442*84e33947SAndroid Build Coastguard Worker return error;
443*84e33947SAndroid Build Coastguard Worker }
444*84e33947SAndroid Build Coastguard Worker
445*84e33947SAndroid Build Coastguard Worker /**
446*84e33947SAndroid Build Coastguard Worker * Request that the WiFi chipset perform a scan, or deliver results from its
447*84e33947SAndroid Build Coastguard Worker * cache if the parameters allow for it.
448*84e33947SAndroid Build Coastguard Worker *
449*84e33947SAndroid Build Coastguard Worker * This function returns an error code synchronously.
450*84e33947SAndroid Build Coastguard Worker * A subsequent call to chppWifiServiceScanResponseCallback() will be used to
451*84e33947SAndroid Build Coastguard Worker * communicate the result of this request (as a service response).
452*84e33947SAndroid Build Coastguard Worker * A subsequent call to chppWifiServiceScanEventCallback() will be used to
453*84e33947SAndroid Build Coastguard Worker * communicate the scan results (as a service notification).
454*84e33947SAndroid Build Coastguard Worker *
455*84e33947SAndroid Build Coastguard Worker * @param serviceContext Maintains status for each service instance.
456*84e33947SAndroid Build Coastguard Worker * @param requestHeader App layer header of the request.
457*84e33947SAndroid Build Coastguard Worker * @param buf Input data. Cannot be null.
458*84e33947SAndroid Build Coastguard Worker * @param len Length of input data in bytes.
459*84e33947SAndroid Build Coastguard Worker *
460*84e33947SAndroid Build Coastguard Worker * @return Indicates the result of this function call.
461*84e33947SAndroid Build Coastguard Worker */
chppWifiServiceRequestScanAsync(struct ChppWifiServiceState * wifiServiceContext,struct ChppAppHeader * requestHeader,uint8_t * buf,size_t len)462*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppWifiServiceRequestScanAsync(
463*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
464*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len) {
465*84e33947SAndroid Build Coastguard Worker UNUSED_VAR(requestHeader);
466*84e33947SAndroid Build Coastguard Worker enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;
467*84e33947SAndroid Build Coastguard Worker
468*84e33947SAndroid Build Coastguard Worker struct chreWifiScanParams *chre =
469*84e33947SAndroid Build Coastguard Worker chppWifiScanParamsToChre((struct ChppWifiScanParams *)buf, len);
470*84e33947SAndroid Build Coastguard Worker
471*84e33947SAndroid Build Coastguard Worker if (chre == NULL) {
472*84e33947SAndroid Build Coastguard Worker CHPP_LOGE(
473*84e33947SAndroid Build Coastguard Worker "WifiServiceRequestScanAsync CHPP -> CHRE conversion failed. Input "
474*84e33947SAndroid Build Coastguard Worker "len=%" PRIuSIZE,
475*84e33947SAndroid Build Coastguard Worker len);
476*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_INVALID_ARG;
477*84e33947SAndroid Build Coastguard Worker
478*84e33947SAndroid Build Coastguard Worker } else {
479*84e33947SAndroid Build Coastguard Worker if (!wifiServiceContext->api->requestScan(chre)) {
480*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_UNSPECIFIED;
481*84e33947SAndroid Build Coastguard Worker }
482*84e33947SAndroid Build Coastguard Worker
483*84e33947SAndroid Build Coastguard Worker if (chre->frequencyListLen > 0) {
484*84e33947SAndroid Build Coastguard Worker void *frequencyList = CHPP_CONST_CAST_POINTER(chre->frequencyList);
485*84e33947SAndroid Build Coastguard Worker CHPP_FREE_AND_NULLIFY(frequencyList);
486*84e33947SAndroid Build Coastguard Worker }
487*84e33947SAndroid Build Coastguard Worker if (chre->ssidListLen > 0) {
488*84e33947SAndroid Build Coastguard Worker void *ssidList = CHPP_CONST_CAST_POINTER(chre->ssidList);
489*84e33947SAndroid Build Coastguard Worker CHPP_FREE_AND_NULLIFY(ssidList);
490*84e33947SAndroid Build Coastguard Worker }
491*84e33947SAndroid Build Coastguard Worker CHPP_FREE_AND_NULLIFY(chre);
492*84e33947SAndroid Build Coastguard Worker }
493*84e33947SAndroid Build Coastguard Worker
494*84e33947SAndroid Build Coastguard Worker return error;
495*84e33947SAndroid Build Coastguard Worker }
496*84e33947SAndroid Build Coastguard Worker
497*84e33947SAndroid Build Coastguard Worker /**
498*84e33947SAndroid Build Coastguard Worker * Request that the WiFi chipset perform RTT ranging against a set of access
499*84e33947SAndroid Build Coastguard Worker * points specified in params.
500*84e33947SAndroid Build Coastguard Worker *
501*84e33947SAndroid Build Coastguard Worker * This function returns an error code synchronously.
502*84e33947SAndroid Build Coastguard Worker * A subsequent call to chppWifiServiceRangingEventCallback() will be used to
503*84e33947SAndroid Build Coastguard Worker * communicate the ranging results (as a service notification).
504*84e33947SAndroid Build Coastguard Worker *
505*84e33947SAndroid Build Coastguard Worker * @param serviceContext Maintains status for each service instance.
506*84e33947SAndroid Build Coastguard Worker * @param requestHeader App layer header of the request.
507*84e33947SAndroid Build Coastguard Worker * @param buf Input data. Cannot be null.
508*84e33947SAndroid Build Coastguard Worker * @param len Length of input data in bytes.
509*84e33947SAndroid Build Coastguard Worker *
510*84e33947SAndroid Build Coastguard Worker * @return Indicates the result of this function call.
511*84e33947SAndroid Build Coastguard Worker */
chppWifiServiceRequestRangingAsync(struct ChppWifiServiceState * wifiServiceContext,struct ChppAppHeader * requestHeader,uint8_t * buf,size_t len)512*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppWifiServiceRequestRangingAsync(
513*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
514*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len) {
515*84e33947SAndroid Build Coastguard Worker UNUSED_VAR(requestHeader);
516*84e33947SAndroid Build Coastguard Worker enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;
517*84e33947SAndroid Build Coastguard Worker
518*84e33947SAndroid Build Coastguard Worker struct chreWifiRangingParams *chre =
519*84e33947SAndroid Build Coastguard Worker chppWifiRangingParamsToChre((struct ChppWifiRangingParams *)buf, len);
520*84e33947SAndroid Build Coastguard Worker
521*84e33947SAndroid Build Coastguard Worker if (chre == NULL) {
522*84e33947SAndroid Build Coastguard Worker CHPP_LOGE(
523*84e33947SAndroid Build Coastguard Worker "WifiServiceRequestRangingAsync CHPP -> CHRE conversion failed. Input "
524*84e33947SAndroid Build Coastguard Worker "len=%" PRIuSIZE,
525*84e33947SAndroid Build Coastguard Worker len);
526*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_INVALID_ARG;
527*84e33947SAndroid Build Coastguard Worker
528*84e33947SAndroid Build Coastguard Worker } else {
529*84e33947SAndroid Build Coastguard Worker if (!wifiServiceContext->api->requestRanging(chre)) {
530*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_UNSPECIFIED;
531*84e33947SAndroid Build Coastguard Worker
532*84e33947SAndroid Build Coastguard Worker } else {
533*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *response =
534*84e33947SAndroid Build Coastguard Worker chppAllocResponseFixed(requestHeader, struct ChppAppHeader);
535*84e33947SAndroid Build Coastguard Worker size_t responseLen = sizeof(*response);
536*84e33947SAndroid Build Coastguard Worker
537*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
538*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
539*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_OOM;
540*84e33947SAndroid Build Coastguard Worker } else {
541*84e33947SAndroid Build Coastguard Worker chppSendTimestampedResponseOrFail(
542*84e33947SAndroid Build Coastguard Worker wifiServiceContext->service.appContext,
543*84e33947SAndroid Build Coastguard Worker &wifiServiceContext->requestRangingAsync, response, responseLen);
544*84e33947SAndroid Build Coastguard Worker }
545*84e33947SAndroid Build Coastguard Worker }
546*84e33947SAndroid Build Coastguard Worker
547*84e33947SAndroid Build Coastguard Worker if (chre->targetListLen > 0) {
548*84e33947SAndroid Build Coastguard Worker void *targetList = CHPP_CONST_CAST_POINTER(chre->targetList);
549*84e33947SAndroid Build Coastguard Worker CHPP_FREE_AND_NULLIFY(targetList);
550*84e33947SAndroid Build Coastguard Worker }
551*84e33947SAndroid Build Coastguard Worker CHPP_FREE_AND_NULLIFY(chre);
552*84e33947SAndroid Build Coastguard Worker }
553*84e33947SAndroid Build Coastguard Worker
554*84e33947SAndroid Build Coastguard Worker return error;
555*84e33947SAndroid Build Coastguard Worker }
556*84e33947SAndroid Build Coastguard Worker
chppWifiServiceRequestNanSubscribe(struct ChppWifiServiceState * wifiServiceContext,struct ChppAppHeader * requestHeader,uint8_t * buf,size_t len)557*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppWifiServiceRequestNanSubscribe(
558*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
559*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len) {
560*84e33947SAndroid Build Coastguard Worker enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;
561*84e33947SAndroid Build Coastguard Worker
562*84e33947SAndroid Build Coastguard Worker struct chreWifiNanSubscribeConfig *chreConfig =
563*84e33947SAndroid Build Coastguard Worker chppWifiNanSubscribeConfigToChre((struct ChppWifiNanSubscribeConfig *)buf,
564*84e33947SAndroid Build Coastguard Worker len);
565*84e33947SAndroid Build Coastguard Worker if (chreConfig == NULL) {
566*84e33947SAndroid Build Coastguard Worker CHPP_LOGE(
567*84e33947SAndroid Build Coastguard Worker "WifiServiceNanSubscribeConfig CHPP -> CHRE conversion failed."
568*84e33947SAndroid Build Coastguard Worker "Input len: %" PRIuSIZE,
569*84e33947SAndroid Build Coastguard Worker len);
570*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_INVALID_ARG;
571*84e33947SAndroid Build Coastguard Worker } else {
572*84e33947SAndroid Build Coastguard Worker if (!wifiServiceContext->api->nanSubscribe(chreConfig)) {
573*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_UNSPECIFIED;
574*84e33947SAndroid Build Coastguard Worker
575*84e33947SAndroid Build Coastguard Worker } else {
576*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *response =
577*84e33947SAndroid Build Coastguard Worker chppAllocResponseFixed(requestHeader, struct ChppAppHeader);
578*84e33947SAndroid Build Coastguard Worker size_t responseLen = sizeof(*response);
579*84e33947SAndroid Build Coastguard Worker
580*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
581*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
582*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_OOM;
583*84e33947SAndroid Build Coastguard Worker } else {
584*84e33947SAndroid Build Coastguard Worker chppSendTimestampedResponseOrFail(
585*84e33947SAndroid Build Coastguard Worker wifiServiceContext->service.appContext,
586*84e33947SAndroid Build Coastguard Worker &wifiServiceContext->requestNanSubscribe, response, responseLen);
587*84e33947SAndroid Build Coastguard Worker }
588*84e33947SAndroid Build Coastguard Worker }
589*84e33947SAndroid Build Coastguard Worker }
590*84e33947SAndroid Build Coastguard Worker return error;
591*84e33947SAndroid Build Coastguard Worker }
592*84e33947SAndroid Build Coastguard Worker
chppWifiServiceRequestNanSubscribeCancel(struct ChppWifiServiceState * wifiServiceContext,struct ChppAppHeader * requestHeader,uint8_t * buf,size_t len)593*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppWifiServiceRequestNanSubscribeCancel(
594*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
595*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len) {
596*84e33947SAndroid Build Coastguard Worker enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;
597*84e33947SAndroid Build Coastguard Worker
598*84e33947SAndroid Build Coastguard Worker if (len < sizeof(struct ChppWifiNanSubscribeCancelRequest)) {
599*84e33947SAndroid Build Coastguard Worker CHPP_LOGE(
600*84e33947SAndroid Build Coastguard Worker "WifiServiceRequestNanSubscribecancel invalid input len = %" PRIuSIZE,
601*84e33947SAndroid Build Coastguard Worker len);
602*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_INVALID_ARG;
603*84e33947SAndroid Build Coastguard Worker } else {
604*84e33947SAndroid Build Coastguard Worker struct ChppWifiNanSubscribeCancelRequest *chppRequest =
605*84e33947SAndroid Build Coastguard Worker (struct ChppWifiNanSubscribeCancelRequest *)buf;
606*84e33947SAndroid Build Coastguard Worker uint32_t subscriptionId = chppRequest->subscriptionId;
607*84e33947SAndroid Build Coastguard Worker if (!wifiServiceContext->api->nanSubscribeCancel(subscriptionId)) {
608*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_UNSPECIFIED;
609*84e33947SAndroid Build Coastguard Worker
610*84e33947SAndroid Build Coastguard Worker } else {
611*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *response =
612*84e33947SAndroid Build Coastguard Worker chppAllocResponseFixed(requestHeader, struct ChppAppHeader);
613*84e33947SAndroid Build Coastguard Worker size_t responseLen = sizeof(*response);
614*84e33947SAndroid Build Coastguard Worker
615*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
616*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
617*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_OOM;
618*84e33947SAndroid Build Coastguard Worker } else {
619*84e33947SAndroid Build Coastguard Worker chppSendTimestampedResponseOrFail(
620*84e33947SAndroid Build Coastguard Worker wifiServiceContext->service.appContext,
621*84e33947SAndroid Build Coastguard Worker &wifiServiceContext->requestNanSubscribeCancel, response,
622*84e33947SAndroid Build Coastguard Worker responseLen);
623*84e33947SAndroid Build Coastguard Worker }
624*84e33947SAndroid Build Coastguard Worker }
625*84e33947SAndroid Build Coastguard Worker }
626*84e33947SAndroid Build Coastguard Worker return error;
627*84e33947SAndroid Build Coastguard Worker }
628*84e33947SAndroid Build Coastguard Worker
chppWifiServiceRequestNanRanging(struct ChppWifiServiceState * wifiServiceContext,struct ChppAppHeader * requestHeader,uint8_t * buf,size_t len)629*84e33947SAndroid Build Coastguard Worker static bool chppWifiServiceRequestNanRanging(
630*84e33947SAndroid Build Coastguard Worker struct ChppWifiServiceState *wifiServiceContext,
631*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len) {
632*84e33947SAndroid Build Coastguard Worker enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;
633*84e33947SAndroid Build Coastguard Worker
634*84e33947SAndroid Build Coastguard Worker struct chreWifiNanRangingParams *chreParams = chppWifiNanRangingParamsToChre(
635*84e33947SAndroid Build Coastguard Worker (struct ChppWifiNanRangingParams *)buf, len);
636*84e33947SAndroid Build Coastguard Worker if (chreParams == NULL) {
637*84e33947SAndroid Build Coastguard Worker CHPP_LOGE(
638*84e33947SAndroid Build Coastguard Worker "WifiServiceRequestNanRanging CHPP -> CHRE conversion failed. "
639*84e33947SAndroid Build Coastguard Worker "Input len: %" PRIuSIZE,
640*84e33947SAndroid Build Coastguard Worker len);
641*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_INVALID_ARG;
642*84e33947SAndroid Build Coastguard Worker
643*84e33947SAndroid Build Coastguard Worker } else {
644*84e33947SAndroid Build Coastguard Worker if (!wifiServiceContext->api->requestNanRanging(chreParams)) {
645*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_UNSPECIFIED;
646*84e33947SAndroid Build Coastguard Worker
647*84e33947SAndroid Build Coastguard Worker } else {
648*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *response =
649*84e33947SAndroid Build Coastguard Worker chppAllocResponseFixed(requestHeader, struct ChppAppHeader);
650*84e33947SAndroid Build Coastguard Worker size_t responseLen = sizeof(*response);
651*84e33947SAndroid Build Coastguard Worker
652*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
653*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
654*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_OOM;
655*84e33947SAndroid Build Coastguard Worker } else {
656*84e33947SAndroid Build Coastguard Worker chppSendTimestampedResponseOrFail(
657*84e33947SAndroid Build Coastguard Worker wifiServiceContext->service.appContext,
658*84e33947SAndroid Build Coastguard Worker &wifiServiceContext->requestNanRangingAsync, response, responseLen);
659*84e33947SAndroid Build Coastguard Worker }
660*84e33947SAndroid Build Coastguard Worker }
661*84e33947SAndroid Build Coastguard Worker }
662*84e33947SAndroid Build Coastguard Worker return error;
663*84e33947SAndroid Build Coastguard Worker }
664*84e33947SAndroid Build Coastguard Worker
665*84e33947SAndroid Build Coastguard Worker /**
666*84e33947SAndroid Build Coastguard Worker * PAL callback with the result of changes to the scan monitor registration
667*84e33947SAndroid Build Coastguard Worker * status requested via configureScanMonitor.
668*84e33947SAndroid Build Coastguard Worker *
669*84e33947SAndroid Build Coastguard Worker * @param enabled true if the scan monitor is currently active
670*84e33947SAndroid Build Coastguard Worker * @param errorCode An error code from enum chreError
671*84e33947SAndroid Build Coastguard Worker */
chppWifiServiceScanMonitorStatusChangeCallback(bool enabled,uint8_t errorCode)672*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceScanMonitorStatusChangeCallback(bool enabled,
673*84e33947SAndroid Build Coastguard Worker uint8_t errorCode) {
674*84e33947SAndroid Build Coastguard Worker // Recreate request header
675*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader requestHeader = {
676*84e33947SAndroid Build Coastguard Worker .handle = gWifiServiceContext.service.handle,
677*84e33947SAndroid Build Coastguard Worker .transaction = gWifiServiceContext.configureScanMonitorAsync.transaction,
678*84e33947SAndroid Build Coastguard Worker .command = CHPP_WIFI_CONFIGURE_SCAN_MONITOR_ASYNC,
679*84e33947SAndroid Build Coastguard Worker };
680*84e33947SAndroid Build Coastguard Worker
681*84e33947SAndroid Build Coastguard Worker struct ChppWifiConfigureScanMonitorAsyncResponse *response =
682*84e33947SAndroid Build Coastguard Worker chppAllocResponseFixed(&requestHeader,
683*84e33947SAndroid Build Coastguard Worker struct ChppWifiConfigureScanMonitorAsyncResponse);
684*84e33947SAndroid Build Coastguard Worker size_t responseLen = sizeof(*response);
685*84e33947SAndroid Build Coastguard Worker
686*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
687*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
688*84e33947SAndroid Build Coastguard Worker CHPP_ASSERT(false);
689*84e33947SAndroid Build Coastguard Worker
690*84e33947SAndroid Build Coastguard Worker } else {
691*84e33947SAndroid Build Coastguard Worker response->params.enabled = enabled;
692*84e33947SAndroid Build Coastguard Worker response->params.errorCode = errorCode;
693*84e33947SAndroid Build Coastguard Worker
694*84e33947SAndroid Build Coastguard Worker chppSendTimestampedResponseOrFail(
695*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.service.appContext,
696*84e33947SAndroid Build Coastguard Worker &gWifiServiceContext.configureScanMonitorAsync, response, responseLen);
697*84e33947SAndroid Build Coastguard Worker }
698*84e33947SAndroid Build Coastguard Worker }
699*84e33947SAndroid Build Coastguard Worker
700*84e33947SAndroid Build Coastguard Worker /**
701*84e33947SAndroid Build Coastguard Worker * PAL callback with the result of a requestScan.
702*84e33947SAndroid Build Coastguard Worker *
703*84e33947SAndroid Build Coastguard Worker * @param pending true if the request was successful.
704*84e33947SAndroid Build Coastguard Worker * @param errorCode An error code from enum chreError.
705*84e33947SAndroid Build Coastguard Worker */
chppWifiServiceScanResponseCallback(bool pending,uint8_t errorCode)706*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceScanResponseCallback(bool pending,
707*84e33947SAndroid Build Coastguard Worker uint8_t errorCode) {
708*84e33947SAndroid Build Coastguard Worker // Recreate request header
709*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader requestHeader = {
710*84e33947SAndroid Build Coastguard Worker .handle = gWifiServiceContext.service.handle,
711*84e33947SAndroid Build Coastguard Worker .transaction = gWifiServiceContext.requestScanAsync.transaction,
712*84e33947SAndroid Build Coastguard Worker .command = CHPP_WIFI_REQUEST_SCAN_ASYNC,
713*84e33947SAndroid Build Coastguard Worker };
714*84e33947SAndroid Build Coastguard Worker
715*84e33947SAndroid Build Coastguard Worker struct ChppWifiRequestScanResponse *response = chppAllocResponseFixed(
716*84e33947SAndroid Build Coastguard Worker &requestHeader, struct ChppWifiRequestScanResponse);
717*84e33947SAndroid Build Coastguard Worker size_t responseLen = sizeof(*response);
718*84e33947SAndroid Build Coastguard Worker
719*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
720*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
721*84e33947SAndroid Build Coastguard Worker CHPP_ASSERT(false);
722*84e33947SAndroid Build Coastguard Worker
723*84e33947SAndroid Build Coastguard Worker } else {
724*84e33947SAndroid Build Coastguard Worker response->params.pending = pending;
725*84e33947SAndroid Build Coastguard Worker response->params.errorCode = errorCode;
726*84e33947SAndroid Build Coastguard Worker
727*84e33947SAndroid Build Coastguard Worker chppSendTimestampedResponseOrFail(gWifiServiceContext.service.appContext,
728*84e33947SAndroid Build Coastguard Worker &gWifiServiceContext.requestScanAsync,
729*84e33947SAndroid Build Coastguard Worker response, responseLen);
730*84e33947SAndroid Build Coastguard Worker }
731*84e33947SAndroid Build Coastguard Worker }
732*84e33947SAndroid Build Coastguard Worker
733*84e33947SAndroid Build Coastguard Worker /**
734*84e33947SAndroid Build Coastguard Worker * PAL callback with WiFi scan results.
735*84e33947SAndroid Build Coastguard Worker *
736*84e33947SAndroid Build Coastguard Worker * @param event Scan result data.
737*84e33947SAndroid Build Coastguard Worker */
chppWifiServiceScanEventCallback(struct chreWifiScanEvent * event)738*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceScanEventCallback(struct chreWifiScanEvent *event) {
739*84e33947SAndroid Build Coastguard Worker // Craft response per parser script
740*84e33947SAndroid Build Coastguard Worker struct ChppWifiScanEventWithHeader *notification = NULL;
741*84e33947SAndroid Build Coastguard Worker size_t notificationLen = 0;
742*84e33947SAndroid Build Coastguard Worker
743*84e33947SAndroid Build Coastguard Worker CHPP_DEBUG_ASSERT(chppCheckWifiScanEventNotification(event));
744*84e33947SAndroid Build Coastguard Worker
745*84e33947SAndroid Build Coastguard Worker if (!chppWifiScanEventFromChre(event, ¬ification, ¬ificationLen)) {
746*84e33947SAndroid Build Coastguard Worker CHPP_LOGE("ScanEvent conversion failed (OOM?). ID=%" PRIu8,
747*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.requestScanAsync.transaction);
748*84e33947SAndroid Build Coastguard Worker
749*84e33947SAndroid Build Coastguard Worker notification = chppMalloc(sizeof(struct ChppAppHeader));
750*84e33947SAndroid Build Coastguard Worker if (notification == NULL) {
751*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
752*84e33947SAndroid Build Coastguard Worker } else {
753*84e33947SAndroid Build Coastguard Worker notificationLen = sizeof(struct ChppAppHeader);
754*84e33947SAndroid Build Coastguard Worker }
755*84e33947SAndroid Build Coastguard Worker }
756*84e33947SAndroid Build Coastguard Worker
757*84e33947SAndroid Build Coastguard Worker if (notification != NULL) {
758*84e33947SAndroid Build Coastguard Worker notification->header.handle = gWifiServiceContext.service.handle;
759*84e33947SAndroid Build Coastguard Worker notification->header.type = CHPP_MESSAGE_TYPE_SERVICE_NOTIFICATION;
760*84e33947SAndroid Build Coastguard Worker notification->header.transaction =
761*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.requestScanAsync.transaction;
762*84e33947SAndroid Build Coastguard Worker notification->header.error =
763*84e33947SAndroid Build Coastguard Worker (notificationLen > sizeof(struct ChppAppHeader))
764*84e33947SAndroid Build Coastguard Worker ? CHPP_APP_ERROR_NONE
765*84e33947SAndroid Build Coastguard Worker : CHPP_APP_ERROR_CONVERSION_FAILED;
766*84e33947SAndroid Build Coastguard Worker notification->header.command = CHPP_WIFI_REQUEST_SCAN_ASYNC;
767*84e33947SAndroid Build Coastguard Worker
768*84e33947SAndroid Build Coastguard Worker chppEnqueueTxDatagramOrFail(
769*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.service.appContext->transportContext, notification,
770*84e33947SAndroid Build Coastguard Worker notificationLen);
771*84e33947SAndroid Build Coastguard Worker }
772*84e33947SAndroid Build Coastguard Worker
773*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.api->releaseScanEvent(event);
774*84e33947SAndroid Build Coastguard Worker }
775*84e33947SAndroid Build Coastguard Worker
776*84e33947SAndroid Build Coastguard Worker /**
777*84e33947SAndroid Build Coastguard Worker * PAL callback with RTT ranging results from the WiFi module.
778*84e33947SAndroid Build Coastguard Worker *
779*84e33947SAndroid Build Coastguard Worker * @param errorCode An error code from enum chreError.
780*84e33947SAndroid Build Coastguard Worker * @param event Ranging data.
781*84e33947SAndroid Build Coastguard Worker */
chppWifiServiceRangingEventCallback(uint8_t errorCode,struct chreWifiRangingEvent * event)782*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceRangingEventCallback(
783*84e33947SAndroid Build Coastguard Worker uint8_t errorCode, struct chreWifiRangingEvent *event) {
784*84e33947SAndroid Build Coastguard Worker struct ChppWifiRangingEventWithHeader *notification = NULL;
785*84e33947SAndroid Build Coastguard Worker size_t notificationLen = 0;
786*84e33947SAndroid Build Coastguard Worker
787*84e33947SAndroid Build Coastguard Worker if (!chppWifiRangingEventFromChre(event, ¬ification, ¬ificationLen)) {
788*84e33947SAndroid Build Coastguard Worker CHPP_LOGE("RangingEvent conversion failed (OOM?) ID=%" PRIu8,
789*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.requestRangingAsync.transaction);
790*84e33947SAndroid Build Coastguard Worker
791*84e33947SAndroid Build Coastguard Worker notification = chppMalloc(sizeof(struct ChppAppHeader));
792*84e33947SAndroid Build Coastguard Worker if (notification == NULL) {
793*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
794*84e33947SAndroid Build Coastguard Worker } else {
795*84e33947SAndroid Build Coastguard Worker notificationLen = sizeof(struct ChppAppHeader);
796*84e33947SAndroid Build Coastguard Worker }
797*84e33947SAndroid Build Coastguard Worker }
798*84e33947SAndroid Build Coastguard Worker
799*84e33947SAndroid Build Coastguard Worker if (notification != NULL) {
800*84e33947SAndroid Build Coastguard Worker uint16_t command = CHPP_WIFI_REQUEST_RANGING_ASYNC;
801*84e33947SAndroid Build Coastguard Worker
802*84e33947SAndroid Build Coastguard Worker // Per CHRE's API contract, only one kind of ranging request can be pending
803*84e33947SAndroid Build Coastguard Worker // at a time - use the higher of the two for the notification.
804*84e33947SAndroid Build Coastguard Worker uint8_t transaction =
805*84e33947SAndroid Build Coastguard Worker MAX(gWifiServiceContext.requestRangingAsync.transaction,
806*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.requestNanRangingAsync.transaction);
807*84e33947SAndroid Build Coastguard Worker notification->header.handle = gWifiServiceContext.service.handle;
808*84e33947SAndroid Build Coastguard Worker notification->header.type = CHPP_MESSAGE_TYPE_SERVICE_NOTIFICATION;
809*84e33947SAndroid Build Coastguard Worker notification->header.transaction = transaction;
810*84e33947SAndroid Build Coastguard Worker notification->header.command = command;
811*84e33947SAndroid Build Coastguard Worker notification->header.error =
812*84e33947SAndroid Build Coastguard Worker (notificationLen > sizeof(struct ChppAppHeader))
813*84e33947SAndroid Build Coastguard Worker ? CHPP_APP_ERROR_NONE
814*84e33947SAndroid Build Coastguard Worker : CHPP_APP_ERROR_CONVERSION_FAILED;
815*84e33947SAndroid Build Coastguard Worker
816*84e33947SAndroid Build Coastguard Worker if (errorCode != CHRE_ERROR_NONE) {
817*84e33947SAndroid Build Coastguard Worker notification->header.error = CHPP_APP_ERROR_BEYOND_CHPP;
818*84e33947SAndroid Build Coastguard Worker notificationLen = MIN(notificationLen, sizeof(struct ChppAppHeader));
819*84e33947SAndroid Build Coastguard Worker }
820*84e33947SAndroid Build Coastguard Worker
821*84e33947SAndroid Build Coastguard Worker chppEnqueueTxDatagramOrFail(
822*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.service.appContext->transportContext, notification,
823*84e33947SAndroid Build Coastguard Worker notificationLen);
824*84e33947SAndroid Build Coastguard Worker }
825*84e33947SAndroid Build Coastguard Worker
826*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.api->releaseRangingEvent(event);
827*84e33947SAndroid Build Coastguard Worker }
828*84e33947SAndroid Build Coastguard Worker
829*84e33947SAndroid Build Coastguard Worker /**
830*84e33947SAndroid Build Coastguard Worker * PAL callback with NAN service subscription identifier information.
831*84e33947SAndroid Build Coastguard Worker *
832*84e33947SAndroid Build Coastguard Worker * @param errorCode Error code indicating if a NAN subscription failed. The
833*84e33947SAndroid Build Coastguard Worker * subscriptionId field is only valid if the error code is
834*84e33947SAndroid Build Coastguard Worker * CHRE_ERROR_NONE.
835*84e33947SAndroid Build Coastguard Worker * @param subscriptionId The ID assigned to the service subscription request.
836*84e33947SAndroid Build Coastguard Worker * This value is only valid if the error code is CHRE_ERROR_NONE.
837*84e33947SAndroid Build Coastguard Worker */
chppWifiServiceNanIdentifierCallback(uint8_t errorCode,uint32_t subscriptionId)838*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceNanIdentifierCallback(uint8_t errorCode,
839*84e33947SAndroid Build Coastguard Worker uint32_t subscriptionId) {
840*84e33947SAndroid Build Coastguard Worker size_t idLen = sizeof(struct ChppWifiNanServiceIdentifier);
841*84e33947SAndroid Build Coastguard Worker struct ChppWifiNanServiceIdentifier *id = chppMalloc(idLen);
842*84e33947SAndroid Build Coastguard Worker if (id == NULL) {
843*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
844*84e33947SAndroid Build Coastguard Worker } else {
845*84e33947SAndroid Build Coastguard Worker id->header.command = CHPP_WIFI_REQUEST_NAN_SUB;
846*84e33947SAndroid Build Coastguard Worker id->header.handle = gWifiServiceContext.service.handle;
847*84e33947SAndroid Build Coastguard Worker id->header.type = CHPP_MESSAGE_TYPE_SERVICE_NOTIFICATION;
848*84e33947SAndroid Build Coastguard Worker id->header.error = CHPP_APP_ERROR_NONE;
849*84e33947SAndroid Build Coastguard Worker id->header.transaction =
850*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.requestNanSubscribe.transaction;
851*84e33947SAndroid Build Coastguard Worker id->errorCode = errorCode;
852*84e33947SAndroid Build Coastguard Worker id->subscriptionId = subscriptionId;
853*84e33947SAndroid Build Coastguard Worker
854*84e33947SAndroid Build Coastguard Worker chppEnqueueTxDatagramOrFail(
855*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.service.appContext->transportContext, id, idLen);
856*84e33947SAndroid Build Coastguard Worker }
857*84e33947SAndroid Build Coastguard Worker }
858*84e33947SAndroid Build Coastguard Worker
859*84e33947SAndroid Build Coastguard Worker /**
860*84e33947SAndroid Build Coastguard Worker * PAL callback with NAN service discovery information.
861*84e33947SAndroid Build Coastguard Worker *
862*84e33947SAndroid Build Coastguard Worker * @param event Information about a discovered publishing service.
863*84e33947SAndroid Build Coastguard Worker */
chppWifiServiceNanDiscoveryCallback(struct chreWifiNanDiscoveryEvent * event)864*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceNanDiscoveryCallback(
865*84e33947SAndroid Build Coastguard Worker struct chreWifiNanDiscoveryEvent *event) {
866*84e33947SAndroid Build Coastguard Worker struct ChppWifiNanDiscoveryEventWithHeader *notif = NULL;
867*84e33947SAndroid Build Coastguard Worker size_t notifLen = 0;
868*84e33947SAndroid Build Coastguard Worker
869*84e33947SAndroid Build Coastguard Worker if (!chppWifiNanDiscoveryEventFromChre(event, ¬if, ¬ifLen)) {
870*84e33947SAndroid Build Coastguard Worker CHPP_LOGE("Discovery event conversion failed");
871*84e33947SAndroid Build Coastguard Worker notif = chppMalloc(sizeof(struct ChppAppHeader));
872*84e33947SAndroid Build Coastguard Worker if (notif == NULL) {
873*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
874*84e33947SAndroid Build Coastguard Worker } else {
875*84e33947SAndroid Build Coastguard Worker notifLen = sizeof(struct ChppAppHeader);
876*84e33947SAndroid Build Coastguard Worker }
877*84e33947SAndroid Build Coastguard Worker }
878*84e33947SAndroid Build Coastguard Worker
879*84e33947SAndroid Build Coastguard Worker if (notif != NULL) {
880*84e33947SAndroid Build Coastguard Worker notif->header.handle = gWifiServiceContext.service.handle;
881*84e33947SAndroid Build Coastguard Worker notif->header.type = CHPP_MESSAGE_TYPE_SERVICE_NOTIFICATION;
882*84e33947SAndroid Build Coastguard Worker notif->header.error = (notifLen > sizeof(struct ChppAppHeader))
883*84e33947SAndroid Build Coastguard Worker ? CHPP_APP_ERROR_NONE
884*84e33947SAndroid Build Coastguard Worker : CHPP_APP_ERROR_CONVERSION_FAILED;
885*84e33947SAndroid Build Coastguard Worker notif->header.command = CHPP_WIFI_NOTIFICATION_NAN_SERVICE_DISCOVERY;
886*84e33947SAndroid Build Coastguard Worker
887*84e33947SAndroid Build Coastguard Worker chppEnqueueTxDatagramOrFail(
888*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.service.appContext->transportContext, notif,
889*84e33947SAndroid Build Coastguard Worker notifLen);
890*84e33947SAndroid Build Coastguard Worker }
891*84e33947SAndroid Build Coastguard Worker
892*84e33947SAndroid Build Coastguard Worker if (event != NULL) {
893*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.api->releaseNanDiscoveryEvent(event);
894*84e33947SAndroid Build Coastguard Worker }
895*84e33947SAndroid Build Coastguard Worker }
896*84e33947SAndroid Build Coastguard Worker
897*84e33947SAndroid Build Coastguard Worker /**
898*84e33947SAndroid Build Coastguard Worker * PAL callback invoked when a publishing NAN service goes away.
899*84e33947SAndroid Build Coastguard Worker *
900*84e33947SAndroid Build Coastguard Worker * @param subscriptionId ID of the subscribing service.
901*84e33947SAndroid Build Coastguard Worker * @param publisherId ID of the publishing service that has gone away.
902*84e33947SAndroid Build Coastguard Worker */
chppWifiServiceNanLostCallback(uint32_t subscriptionId,uint32_t publisherId)903*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceNanLostCallback(uint32_t subscriptionId,
904*84e33947SAndroid Build Coastguard Worker uint32_t publisherId) {
905*84e33947SAndroid Build Coastguard Worker struct chreWifiNanSessionLostEvent chreEvent = {
906*84e33947SAndroid Build Coastguard Worker .id = subscriptionId,
907*84e33947SAndroid Build Coastguard Worker .peerId = publisherId,
908*84e33947SAndroid Build Coastguard Worker };
909*84e33947SAndroid Build Coastguard Worker struct ChppWifiNanSessionLostEventWithHeader *notif = NULL;
910*84e33947SAndroid Build Coastguard Worker size_t notifLen = 0;
911*84e33947SAndroid Build Coastguard Worker
912*84e33947SAndroid Build Coastguard Worker if (!chppWifiNanSessionLostEventFromChre(&chreEvent, ¬if, ¬ifLen)) {
913*84e33947SAndroid Build Coastguard Worker CHPP_LOGE("Session lost event conversion failed");
914*84e33947SAndroid Build Coastguard Worker notif = chppMalloc(sizeof(struct ChppAppHeader));
915*84e33947SAndroid Build Coastguard Worker if (notif == NULL) {
916*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
917*84e33947SAndroid Build Coastguard Worker } else {
918*84e33947SAndroid Build Coastguard Worker notifLen = sizeof(struct ChppAppHeader);
919*84e33947SAndroid Build Coastguard Worker }
920*84e33947SAndroid Build Coastguard Worker }
921*84e33947SAndroid Build Coastguard Worker
922*84e33947SAndroid Build Coastguard Worker if (notif != NULL) {
923*84e33947SAndroid Build Coastguard Worker notif->header.handle = gWifiServiceContext.service.handle;
924*84e33947SAndroid Build Coastguard Worker notif->header.type = CHPP_MESSAGE_TYPE_SERVICE_NOTIFICATION;
925*84e33947SAndroid Build Coastguard Worker notif->header.error = (notifLen > sizeof(struct ChppAppHeader))
926*84e33947SAndroid Build Coastguard Worker ? CHPP_APP_ERROR_NONE
927*84e33947SAndroid Build Coastguard Worker : CHPP_APP_ERROR_CONVERSION_FAILED;
928*84e33947SAndroid Build Coastguard Worker notif->header.command = CHPP_WIFI_NOTIFICATION_NAN_SERVICE_LOST;
929*84e33947SAndroid Build Coastguard Worker
930*84e33947SAndroid Build Coastguard Worker chppEnqueueTxDatagramOrFail(
931*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.service.appContext->transportContext, notif,
932*84e33947SAndroid Build Coastguard Worker notifLen);
933*84e33947SAndroid Build Coastguard Worker }
934*84e33947SAndroid Build Coastguard Worker }
935*84e33947SAndroid Build Coastguard Worker
936*84e33947SAndroid Build Coastguard Worker /**
937*84e33947SAndroid Build Coastguard Worker * PAL callback invoked when a NAN service subscription is terminated.
938*84e33947SAndroid Build Coastguard Worker *
939*84e33947SAndroid Build Coastguard Worker * @param reason Error code indicating the reason for the termination.
940*84e33947SAndroid Build Coastguard Worker * @param subscriptionId The subscription ID of the terminated NAN service.
941*84e33947SAndroid Build Coastguard Worker */
chppWifiServiceNanTerminatedCallback(uint32_t reason,uint32_t subscriptionId)942*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceNanTerminatedCallback(uint32_t reason,
943*84e33947SAndroid Build Coastguard Worker uint32_t subscriptionId) {
944*84e33947SAndroid Build Coastguard Worker uint8_t chreReason = (uint8_t)reason;
945*84e33947SAndroid Build Coastguard Worker struct chreWifiNanSessionTerminatedEvent chreEvent = {
946*84e33947SAndroid Build Coastguard Worker .id = subscriptionId,
947*84e33947SAndroid Build Coastguard Worker .reason = chreReason,
948*84e33947SAndroid Build Coastguard Worker };
949*84e33947SAndroid Build Coastguard Worker struct ChppWifiNanSessionTerminatedEventWithHeader *notif = NULL;
950*84e33947SAndroid Build Coastguard Worker size_t notifLen = 0;
951*84e33947SAndroid Build Coastguard Worker
952*84e33947SAndroid Build Coastguard Worker if (!chppWifiNanSessionTerminatedEventFromChre(&chreEvent, ¬if,
953*84e33947SAndroid Build Coastguard Worker ¬ifLen)) {
954*84e33947SAndroid Build Coastguard Worker CHPP_LOGE("Session terminated event conversion failed");
955*84e33947SAndroid Build Coastguard Worker notif = chppMalloc(sizeof(struct ChppAppHeader));
956*84e33947SAndroid Build Coastguard Worker if (notif == NULL) {
957*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
958*84e33947SAndroid Build Coastguard Worker } else {
959*84e33947SAndroid Build Coastguard Worker notifLen = sizeof(struct ChppAppHeader);
960*84e33947SAndroid Build Coastguard Worker }
961*84e33947SAndroid Build Coastguard Worker }
962*84e33947SAndroid Build Coastguard Worker
963*84e33947SAndroid Build Coastguard Worker if (notif != NULL) {
964*84e33947SAndroid Build Coastguard Worker notif->header.handle = gWifiServiceContext.service.handle;
965*84e33947SAndroid Build Coastguard Worker notif->header.type = CHPP_MESSAGE_TYPE_SERVICE_NOTIFICATION;
966*84e33947SAndroid Build Coastguard Worker notif->header.error = (notifLen > sizeof(struct ChppAppHeader))
967*84e33947SAndroid Build Coastguard Worker ? CHPP_APP_ERROR_NONE
968*84e33947SAndroid Build Coastguard Worker : CHPP_APP_ERROR_CONVERSION_FAILED;
969*84e33947SAndroid Build Coastguard Worker notif->header.command = CHPP_WIFI_NOTIFICATION_NAN_SERVICE_TERMINATED;
970*84e33947SAndroid Build Coastguard Worker
971*84e33947SAndroid Build Coastguard Worker chppEnqueueTxDatagramOrFail(
972*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.service.appContext->transportContext, notif,
973*84e33947SAndroid Build Coastguard Worker notifLen);
974*84e33947SAndroid Build Coastguard Worker }
975*84e33947SAndroid Build Coastguard Worker }
976*84e33947SAndroid Build Coastguard Worker
977*84e33947SAndroid Build Coastguard Worker /**
978*84e33947SAndroid Build Coastguard Worker * PAL callback invoked when a NAN service subscription is canceled.
979*84e33947SAndroid Build Coastguard Worker *
980*84e33947SAndroid Build Coastguard Worker * @param errorCode A value in @ref chreError indicating the result of the
981*84e33947SAndroid Build Coastguard Worker * cancelation, with CHRE_ERROR_NONE indicating success.
982*84e33947SAndroid Build Coastguard Worker * @param subscriptionId The subscription ID of the canceled NAN service.
983*84e33947SAndroid Build Coastguard Worker */
chppWifiServiceNanSubscriptionCanceledCallback(uint8_t errorCode,uint32_t subscriptionId)984*84e33947SAndroid Build Coastguard Worker static void chppWifiServiceNanSubscriptionCanceledCallback(
985*84e33947SAndroid Build Coastguard Worker uint8_t errorCode, uint32_t subscriptionId) {
986*84e33947SAndroid Build Coastguard Worker size_t responseLen = sizeof(struct ChppWifiNanSubscriptionCanceledResponse);
987*84e33947SAndroid Build Coastguard Worker struct ChppWifiNanSubscriptionCanceledResponse *response =
988*84e33947SAndroid Build Coastguard Worker chppMalloc(responseLen);
989*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
990*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
991*84e33947SAndroid Build Coastguard Worker } else {
992*84e33947SAndroid Build Coastguard Worker response->header.command = CHPP_WIFI_REQUEST_NAN_SUB_CANCEL;
993*84e33947SAndroid Build Coastguard Worker response->header.handle = gWifiServiceContext.service.handle;
994*84e33947SAndroid Build Coastguard Worker response->header.type = CHPP_MESSAGE_TYPE_SERVICE_NOTIFICATION;
995*84e33947SAndroid Build Coastguard Worker response->header.error = CHPP_APP_ERROR_NONE;
996*84e33947SAndroid Build Coastguard Worker response->header.transaction =
997*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.requestNanSubscribeCancel.transaction;
998*84e33947SAndroid Build Coastguard Worker response->errorCode = errorCode;
999*84e33947SAndroid Build Coastguard Worker response->subscriptionId = subscriptionId;
1000*84e33947SAndroid Build Coastguard Worker
1001*84e33947SAndroid Build Coastguard Worker chppEnqueueTxDatagramOrFail(
1002*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.service.appContext->transportContext, response,
1003*84e33947SAndroid Build Coastguard Worker responseLen);
1004*84e33947SAndroid Build Coastguard Worker }
1005*84e33947SAndroid Build Coastguard Worker }
1006*84e33947SAndroid Build Coastguard Worker
1007*84e33947SAndroid Build Coastguard Worker /************************************************
1008*84e33947SAndroid Build Coastguard Worker * Public Functions
1009*84e33947SAndroid Build Coastguard Worker ***********************************************/
1010*84e33947SAndroid Build Coastguard Worker
chppRegisterWifiService(struct ChppAppState * appContext)1011*84e33947SAndroid Build Coastguard Worker void chppRegisterWifiService(struct ChppAppState *appContext) {
1012*84e33947SAndroid Build Coastguard Worker gWifiServiceContext.api = chrePalWifiGetApi(CHRE_PAL_WIFI_API_V1_2);
1013*84e33947SAndroid Build Coastguard Worker
1014*84e33947SAndroid Build Coastguard Worker chppCheckWifiScanEventNotificationReset();
1015*84e33947SAndroid Build Coastguard Worker
1016*84e33947SAndroid Build Coastguard Worker if (gWifiServiceContext.api == NULL) {
1017*84e33947SAndroid Build Coastguard Worker CHPP_DEBUG_ASSERT_LOG(false,
1018*84e33947SAndroid Build Coastguard Worker "WiFi PAL API incompatible. Cannot register service");
1019*84e33947SAndroid Build Coastguard Worker
1020*84e33947SAndroid Build Coastguard Worker } else {
1021*84e33947SAndroid Build Coastguard Worker chppRegisterService(appContext, (void *)&gWifiServiceContext,
1022*84e33947SAndroid Build Coastguard Worker &gWifiServiceContext.service, NULL /*outReqStates*/,
1023*84e33947SAndroid Build Coastguard Worker &kWifiServiceConfig);
1024*84e33947SAndroid Build Coastguard Worker CHPP_DEBUG_ASSERT(gWifiServiceContext.service.handle);
1025*84e33947SAndroid Build Coastguard Worker }
1026*84e33947SAndroid Build Coastguard Worker }
1027*84e33947SAndroid Build Coastguard Worker
chppDeregisterWifiService(struct ChppAppState * appContext)1028*84e33947SAndroid Build Coastguard Worker void chppDeregisterWifiService(struct ChppAppState *appContext) {
1029*84e33947SAndroid Build Coastguard Worker // TODO
1030*84e33947SAndroid Build Coastguard Worker
1031*84e33947SAndroid Build Coastguard Worker UNUSED_VAR(appContext);
1032*84e33947SAndroid Build Coastguard Worker }
1033