liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include <cinttypes>
#include <cstddef>
#include <cstring>
 
#include "chre/core/event_loop_manager.h"
#include "chre/core/wifi_request_manager.h"
#include "chre/platform/fatal_error.h"
#include "chre/platform/log.h"
#include "chre/platform/system_time.h"
#include "chre/util/system/debug_dump.h"
#include "chre_api/chre/version.h"
 
namespace chre {
 
WifiRequestManager::WifiRequestManager() {
  // Reserve space for at least one scan monitoring nanoapp. This ensures that
  // the first asynchronous push_back will succeed. Future push_backs will be
  // synchronous and failures will be returned to the client.
  if (!mScanMonitorNanoapps.reserve(1)) {
    FATAL_ERROR_OOM();
  }
}
 
void WifiRequestManager::init() {
  mPlatformWifi.init();
}
 
uint32_t WifiRequestManager::getCapabilities() {
  return mPlatformWifi.getCapabilities();
}
 
bool WifiRequestManager::configureScanMonitor(Nanoapp *nanoapp, bool enable,
                                              const void *cookie) {
  CHRE_ASSERT(nanoapp);
 
  bool success = false;
  uint32_t instanceId = nanoapp->getInstanceId();
  bool hasScanMonitorRequest = nanoappHasScanMonitorRequest(instanceId);
  if (!mPendingScanMonitorRequests.empty()) {
    success = addScanMonitorRequestToQueue(nanoapp, enable, cookie);
  } else if (scanMonitorIsInRequestedState(enable, hasScanMonitorRequest)) {
    // The scan monitor is already in the requested state. A success event can
    // be posted immediately.
    success = postScanMonitorAsyncResultEvent(instanceId, true /* success */,
                                              enable, CHRE_ERROR_NONE, cookie);
  } else if (scanMonitorStateTransitionIsRequired(enable,
                                                  hasScanMonitorRequest)) {
    success = addScanMonitorRequestToQueue(nanoapp, enable, cookie);
    if (success) {
      success = mPlatformWifi.configureScanMonitor(enable);
      if (!success) {
        mPendingScanMonitorRequests.pop_back();
        LOGE("Failed to enable the scan monitor for nanoapp instance %" PRIu32,
             instanceId);
      }
    }
  } else {
    CHRE_ASSERT_LOG(false, "Invalid scan monitor configuration");
  }
 
  return success;
}
 
bool WifiRequestManager::requestRanging(
    Nanoapp *nanoapp, const struct chreWifiRangingParams *params,
    const void *cookie) {
  CHRE_ASSERT(nanoapp);
 
  bool success = false;
  if (!mPendingRangingRequests.emplace()) {
    LOGE("Can't issue new RTT request; pending queue full");
  } else {
    PendingRangingRequest& req = mPendingRangingRequests.back();
    req.nanoappInstanceId = nanoapp->getInstanceId();
    req.cookie = cookie;
 
    if (mPendingRangingRequests.size() == 1) {
      // First in line; dispatch request immediately
      success = mPlatformWifi.requestRanging(params);
      if (!success) {
        LOGE("WiFi RTT request failed");
        mPendingRangingRequests.pop_back();
      } else {
        mRangingResponseTimeout = SystemTime::getMonotonicTime()
            + Nanoseconds(CHRE_WIFI_RANGING_RESULT_TIMEOUT_NS);
      }
    } else {
      // Dispatch request later, after prior requests finish
      // TODO(b/65331248): use a timer to ensure the platform is meeting its
      // contract
      CHRE_ASSERT_LOG(SystemTime::getMonotonicTime() <= mRangingResponseTimeout,
                      "WiFi platform didn't give callback in time");
      success = req.targetList.copy_array(params->targetList,
                                          params->targetListLen);
      if (!success) {
        LOG_OOM();
        mPendingRangingRequests.pop_back();
      }
    }
  }
 
  return success;
}
 
bool WifiRequestManager::requestScan(Nanoapp *nanoapp,
                                     const struct chreWifiScanParams *params,
                                     const void *cookie) {
  CHRE_ASSERT(nanoapp);
 
  // TODO(b/65331248): replace with a timer to actively check response timeout
  bool timedOut = (mScanRequestingNanoappInstanceId.has_value()
                   && mLastScanRequestTime
                       + Nanoseconds(CHRE_WIFI_SCAN_RESULT_TIMEOUT_NS)
                     < SystemTime::getMonotonicTime());
  if (timedOut) {
    LOGE("Scan request async response timed out");
    mScanRequestingNanoappInstanceId.reset();
  }
 
  // Handle compatibility with nanoapps compiled against API v1.1, which doesn't
  // include the radioChainPref parameter in chreWifiScanParams
  struct chreWifiScanParams paramsCompat;
  if (nanoapp->getTargetApiVersion() < CHRE_API_VERSION_1_2) {
    memcpy(&paramsCompat, params, offsetof(chreWifiScanParams, radioChainPref));
    paramsCompat.radioChainPref = CHRE_WIFI_RADIO_CHAIN_PREF_DEFAULT;
    params = &paramsCompat;
  }
 
  bool success = false;
  if (mScanRequestingNanoappInstanceId.has_value()) {
     LOGE("Active wifi scan request made while a request is in flight");
  } else {
    success = mPlatformWifi.requestScan(params);
    if (!success) {
      LOGE("Wifi scan request failed");
    } else {
      mScanRequestingNanoappInstanceId = nanoapp->getInstanceId();
      mScanRequestingNanoappCookie = cookie;
      mLastScanRequestTime = SystemTime::getMonotonicTime();
    }
  }
 
  return success;
}
 
void WifiRequestManager::handleScanMonitorStateChange(bool enabled,
                                                      uint8_t errorCode) {
  struct CallbackState {
    bool enabled;
    uint8_t errorCode;
  };
 
  auto *cbState = memoryAlloc<CallbackState>();
  if (cbState == nullptr) {
    LOG_OOM();
  } else {
    cbState->enabled = enabled;
    cbState->errorCode = errorCode;
 
    auto callback = [](uint16_t /* eventType */, void *eventData) {
      auto *state = static_cast<CallbackState *>(eventData);
      EventLoopManagerSingleton::get()->getWifiRequestManager()
          .handleScanMonitorStateChangeSync(state->enabled, state->errorCode);
      memoryFree(state);
    };
 
    EventLoopManagerSingleton::get()->deferCallback(
        SystemCallbackType::WifiScanMonitorStateChange, cbState, callback);
  }
}
 
void WifiRequestManager::handleScanResponse(bool pending,
                                            uint8_t errorCode) {
  struct CallbackState {
    bool pending;
    uint8_t errorCode;
  };
 
  auto *cbState = memoryAlloc<CallbackState>();
  if (cbState == nullptr) {
    LOG_OOM();
  } else {
    cbState->pending = pending;
    cbState->errorCode = errorCode;
 
    auto callback = [](uint16_t /* eventType */, void *eventData) {
      auto *state = static_cast<CallbackState *>(eventData);
      EventLoopManagerSingleton::get()->getWifiRequestManager()
          .handleScanResponseSync(state->pending, state->errorCode);
      memoryFree(state);
    };
 
    EventLoopManagerSingleton::get()->deferCallback(
        SystemCallbackType::WifiRequestScanResponse, cbState, callback);
  }
}
 
void WifiRequestManager::handleRangingEvent(
    uint8_t errorCode, struct chreWifiRangingEvent *event) {
  // Use two different callbacks to avoid needing a temporary allocation to
  // carry the error code into the event loop context
  if (errorCode != CHRE_ERROR_NONE) {
    // Enables passing the error code through the event data pointer to avoid
    // allocating memory
    union NestedErrorCode {
      void *eventData;
      uint8_t errorCode;
    };
 
    auto errorCb = [](uint16_t /* eventType */, void *eventData) {
      NestedErrorCode cbErrorCode;
      cbErrorCode.eventData = eventData;
      EventLoopManagerSingleton::get()->getWifiRequestManager()
          .handleRangingEventSync(cbErrorCode.errorCode, nullptr);
    };
 
    NestedErrorCode error = {};
    error.errorCode = errorCode;
    EventLoopManagerSingleton::get()->deferCallback(
        SystemCallbackType::WifiHandleFailedRanging, error.eventData, errorCb);
  } else {
    auto successCb = [](uint16_t /* eventType */, void *eventData) {
      auto *rttEvent = static_cast<struct chreWifiRangingEvent *>(eventData);
      EventLoopManagerSingleton::get()->getWifiRequestManager()
          .handleRangingEventSync(CHRE_ERROR_NONE, rttEvent);
    };
 
    EventLoopManagerSingleton::get()->deferCallback(
        SystemCallbackType::WifiHandleRangingEvent, event, successCb);
  }
}
 
void WifiRequestManager::handleScanEvent(chreWifiScanEvent *event) {
  auto callback = [](uint16_t eventType, void *eventData) {
    chreWifiScanEvent *scanEvent = static_cast<chreWifiScanEvent *>(eventData);
    EventLoopManagerSingleton::get()->getWifiRequestManager()
        .postScanEventFatal(scanEvent);
  };
 
  EventLoopManagerSingleton::get()->deferCallback(
      SystemCallbackType::WifiHandleScanEvent, event, callback);
}
 
void WifiRequestManager::logStateToBuffer(char *buffer, size_t *bufferPos,
                                          size_t bufferSize) const {
  debugDumpPrint(buffer, bufferPos, bufferSize,
                 "\nWifi: scan monitor %s\n",
                 scanMonitorIsEnabled() ? "enabled" : "disabled");
  debugDumpPrint(buffer, bufferPos, bufferSize,
                 " Wifi scan monitor enabled nanoapps:\n");
  for (const auto& instanceId : mScanMonitorNanoapps) {
    debugDumpPrint(buffer, bufferPos, bufferSize,
                   "  nanoappId=%" PRIu32 "\n", instanceId);
  }
 
  if (mScanRequestingNanoappInstanceId.has_value()) {
    debugDumpPrint(buffer, bufferPos, bufferSize,
                   " Wifi request pending nanoappId=%" PRIu32 "\n",
                   mScanRequestingNanoappInstanceId.value());
  }
 
  debugDumpPrint(buffer, bufferPos, bufferSize,
                 " Wifi transition queue:\n");
  for (const auto& transition : mPendingScanMonitorRequests) {
    debugDumpPrint(buffer, bufferPos, bufferSize,
                   "  enable=%s nanoappId=%" PRIu32 "\n",
                   transition.enable ? "true" : "false",
                   transition.nanoappInstanceId);
  }
}
 
bool WifiRequestManager::scanMonitorIsEnabled() const {
  return !mScanMonitorNanoapps.empty();
}
 
bool WifiRequestManager::nanoappHasScanMonitorRequest(
    uint32_t instanceId, size_t *nanoappIndex) const {
  size_t index = mScanMonitorNanoapps.find(instanceId);
  bool hasScanMonitorRequest = (index != mScanMonitorNanoapps.size());
  if (hasScanMonitorRequest && nanoappIndex != nullptr) {
    *nanoappIndex = index;
  }
 
  return hasScanMonitorRequest;
}
 
bool WifiRequestManager::scanMonitorIsInRequestedState(
    bool requestedState, bool nanoappHasRequest) const {
  return (requestedState == scanMonitorIsEnabled() || (!requestedState
      && (!nanoappHasRequest || mScanMonitorNanoapps.size() > 1)));
}
 
bool WifiRequestManager::scanMonitorStateTransitionIsRequired(
    bool requestedState, bool nanoappHasRequest) const {
  return ((requestedState && mScanMonitorNanoapps.empty())
      || (!requestedState && nanoappHasRequest
              && mScanMonitorNanoapps.size() == 1));
}
 
bool WifiRequestManager::addScanMonitorRequestToQueue(Nanoapp *nanoapp,
                                                      bool enable,
                                                      const void *cookie) {
  PendingScanMonitorRequest scanMonitorStateTransition;
  scanMonitorStateTransition.nanoappInstanceId = nanoapp->getInstanceId();
  scanMonitorStateTransition.cookie = cookie;
  scanMonitorStateTransition.enable = enable;
 
  bool success = mPendingScanMonitorRequests.push(scanMonitorStateTransition);
  if (!success) {
    LOGW("Too many scan monitor state transitions");
  }
 
  return success;
}
 
bool WifiRequestManager::updateNanoappScanMonitoringList(bool enable,
                                                         uint32_t instanceId) {
  bool success = true;
  Nanoapp *nanoapp = EventLoopManagerSingleton::get()->getEventLoop()
      .findNanoappByInstanceId(instanceId);
  if (nanoapp == nullptr) {
    LOGW("Failed to update scan monitoring list for non-existent nanoapp");
  } else {
    size_t nanoappIndex;
    bool hasExistingRequest = nanoappHasScanMonitorRequest(instanceId,
                                                           &nanoappIndex);
    if (enable) {
      if (!hasExistingRequest) {
        // The scan monitor was successfully enabled for this nanoapp and
        // there is no existing request. Add it to the list of scan monitoring
        // nanoapps.
        success = mScanMonitorNanoapps.push_back(instanceId);
        if (!success) {
          LOG_OOM();
        } else {
          nanoapp->registerForBroadcastEvent(CHRE_EVENT_WIFI_SCAN_RESULT);
        }
      }
    } else if (hasExistingRequest) {
      // The scan monitor was successfully disabled for a previously enabled
      // nanoapp. Remove it from the list of scan monitoring nanoapps.
      mScanMonitorNanoapps.erase(nanoappIndex);
      nanoapp->unregisterForBroadcastEvent(CHRE_EVENT_WIFI_SCAN_RESULT);
    } // else disabling an inactive request, treat as success per the CHRE API.
  }
 
  return success;
}
 
bool WifiRequestManager::postScanMonitorAsyncResultEvent(
    uint32_t nanoappInstanceId, bool success, bool enable, uint8_t errorCode,
    const void *cookie) {
  // Allocate and post an event to the nanoapp requesting wifi.
  bool eventPosted = false;
  if (!success || updateNanoappScanMonitoringList(enable, nanoappInstanceId)) {
    chreAsyncResult *event = memoryAlloc<chreAsyncResult>();
    if (event == nullptr) {
      LOG_OOM();
    } else {
      event->requestType = CHRE_WIFI_REQUEST_TYPE_CONFIGURE_SCAN_MONITOR;
      event->success = success;
      event->errorCode = errorCode;
      event->reserved = 0;
      event->cookie = cookie;
 
      // Post the event.
      eventPosted = EventLoopManagerSingleton::get()->getEventLoop()
          .postEvent(CHRE_EVENT_WIFI_ASYNC_RESULT, event, freeEventDataCallback,
                     kSystemInstanceId, nanoappInstanceId);
      if (!eventPosted) {
        memoryFree(event);
      }
    }
  }
 
  return eventPosted;
}
 
void WifiRequestManager::postScanMonitorAsyncResultEventFatal(
    uint32_t nanoappInstanceId, bool success, bool enable, uint8_t errorCode,
    const void *cookie) {
  if (!postScanMonitorAsyncResultEvent(nanoappInstanceId, success, enable,
                                       errorCode, cookie)) {
    FATAL_ERROR("Failed to send WiFi scan monitor async result event");
  }
}
 
bool WifiRequestManager::postScanRequestAsyncResultEvent(
    uint32_t nanoappInstanceId, bool success, uint8_t errorCode,
    const void *cookie) {
  bool eventPosted = false;
  chreAsyncResult *event = memoryAlloc<chreAsyncResult>();
  if (event == nullptr) {
    LOG_OOM();
  } else {
    event->requestType = CHRE_WIFI_REQUEST_TYPE_REQUEST_SCAN;
    event->success = success;
    event->errorCode = errorCode;
    event->reserved = 0;
    event->cookie = cookie;
 
    // Post the event.
    eventPosted = EventLoopManagerSingleton::get()->getEventLoop()
        .postEvent(CHRE_EVENT_WIFI_ASYNC_RESULT, event, freeEventDataCallback,
                   kSystemInstanceId, nanoappInstanceId);
  }
 
  return eventPosted;
}
 
void WifiRequestManager::postScanRequestAsyncResultEventFatal(
    uint32_t nanoappInstanceId, bool success, uint8_t errorCode,
    const void *cookie) {
  if (!postScanRequestAsyncResultEvent(nanoappInstanceId, success, errorCode,
                                       cookie)) {
    FATAL_ERROR("Failed to send WiFi scan request async result event");
  }
}
 
void WifiRequestManager::postScanEventFatal(chreWifiScanEvent *event) {
  EventLoopManagerSingleton::get()->getEventLoop()
      .postEvent(CHRE_EVENT_WIFI_SCAN_RESULT, event, freeWifiScanEventCallback);
}
 
void WifiRequestManager::handleScanMonitorStateChangeSync(bool enabled,
                                                          uint8_t errorCode) {
  // Success is defined as having no errors ... in life ༼ つ ◕_◕ ༽つ
  bool success = (errorCode == CHRE_ERROR_NONE);
 
  // TODO(b/62904616): re-enable this assertion
  //CHRE_ASSERT_LOG(!mScanMonitorStateTransitions.empty(),
  //                "handleScanMonitorStateChangeSync called with no transitions");
  if (mPendingScanMonitorRequests.empty()) {
    LOGE("WiFi PAL error: handleScanMonitorStateChangeSync called with no "
         "transitions (enabled %d errorCode %" PRIu8 ")", enabled, errorCode);
  }
 
  // Always check the front of the queue.
  if (!mPendingScanMonitorRequests.empty()) {
    const auto& stateTransition = mPendingScanMonitorRequests.front();
    success &= (stateTransition.enable == enabled);
    postScanMonitorAsyncResultEventFatal(stateTransition.nanoappInstanceId,
                                         success, stateTransition.enable,
                                         errorCode, stateTransition.cookie);
    mPendingScanMonitorRequests.pop();
  }
 
  while (!mPendingScanMonitorRequests.empty()) {
    const auto& stateTransition = mPendingScanMonitorRequests.front();
    bool hasScanMonitorRequest = nanoappHasScanMonitorRequest(
        stateTransition.nanoappInstanceId);
    if (scanMonitorIsInRequestedState(
        stateTransition.enable, hasScanMonitorRequest)) {
      // We are already in the target state so just post an event indicating
      // success
      postScanMonitorAsyncResultEventFatal(stateTransition.nanoappInstanceId,
                                           true /* success */,
                                           stateTransition.enable,
                                           CHRE_ERROR_NONE,
                                           stateTransition.cookie);
    } else if (scanMonitorStateTransitionIsRequired(
        stateTransition.enable, hasScanMonitorRequest)) {
      if (mPlatformWifi.configureScanMonitor(stateTransition.enable)) {
        break;
      } else {
        postScanMonitorAsyncResultEventFatal(stateTransition.nanoappInstanceId,
                                             false /* success */,
                                             stateTransition.enable, CHRE_ERROR,
                                             stateTransition.cookie);
      }
    } else {
      CHRE_ASSERT_LOG(false, "Invalid scan monitor state");
      break;
    }
 
    mPendingScanMonitorRequests.pop();
  }
}
 
void WifiRequestManager::handleScanResponseSync(bool pending,
                                                uint8_t errorCode) {
  // TODO(b/65206783): re-enable this assertion
  //CHRE_ASSERT_LOG(mScanRequestingNanoappInstanceId.has_value(),
  //                "handleScanResponseSync called with no outstanding request");
  if (!mScanRequestingNanoappInstanceId.has_value()) {
    LOGE("handleScanResponseSync called with no outstanding request");
  }
 
  // TODO: raise this to CHRE_ASSERT_LOG
  if (!pending && errorCode == CHRE_ERROR_NONE) {
    LOGE("Invalid wifi scan response");
    errorCode = CHRE_ERROR;
  }
 
  if (mScanRequestingNanoappInstanceId.has_value()) {
    bool success = (pending && errorCode == CHRE_ERROR_NONE);
    if (!success) {
      LOGW("Wifi scan request failed: pending %d, errorCode %" PRIu8,
           pending, errorCode);
    }
    postScanRequestAsyncResultEventFatal(*mScanRequestingNanoappInstanceId,
                                         success, errorCode,
                                         mScanRequestingNanoappCookie);
 
    // Set a flag to indicate that results may be pending.
    mScanRequestResultsArePending = pending;
 
    if (pending) {
      Nanoapp *nanoapp = EventLoopManagerSingleton::get()->getEventLoop()
          .findNanoappByInstanceId(*mScanRequestingNanoappInstanceId);
      if (nanoapp == nullptr) {
        LOGW("Received WiFi scan response for unknown nanoapp");
      } else {
        nanoapp->registerForBroadcastEvent(CHRE_EVENT_WIFI_SCAN_RESULT);
      }
    } else {
      // If the scan results are not pending, clear the nanoapp instance ID.
      // Otherwise, wait for the results to be delivered and then clear the
      // instance ID.
      mScanRequestingNanoappInstanceId.reset();
    }
  }
}
 
bool WifiRequestManager::postRangingAsyncResult(uint8_t errorCode) {
  bool eventPosted = false;
 
  if (mPendingRangingRequests.empty()) {
    LOGE("Unexpected ranging event callback");
  } else {
    auto *event = memoryAlloc<struct chreAsyncResult>();
    if (event == nullptr) {
      LOG_OOM();
    } else {
      const PendingRangingRequest& req = mPendingRangingRequests.front();
 
      event->requestType = CHRE_WIFI_REQUEST_TYPE_RANGING;
      event->success = (errorCode == CHRE_ERROR_NONE);
      event->errorCode = errorCode;
      event->reserved = 0;
      event->cookie = req.cookie;
 
      eventPosted = EventLoopManagerSingleton::get()->getEventLoop().postEvent(
          CHRE_EVENT_WIFI_ASYNC_RESULT, event, freeEventDataCallback,
          kSystemInstanceId, req.nanoappInstanceId);
      if (!eventPosted) {
        memoryFree(event);
      }
    }
  }
 
  return eventPosted;
}
 
bool WifiRequestManager::dispatchQueuedRangingRequest() {
  const PendingRangingRequest& req = mPendingRangingRequests.front();
  struct chreWifiRangingParams params = {};
  params.targetListLen = static_cast<uint8_t>(req.targetList.size());
  params.targetList = req.targetList.data();
 
  bool success = mPlatformWifi.requestRanging(&params);
  if (!success) {
    LOGE("Failed to issue queued ranging result");
    postRangingAsyncResult(CHRE_ERROR);
    mPendingRangingRequests.pop();
  } else {
    mRangingResponseTimeout = SystemTime::getMonotonicTime()
        + Nanoseconds(CHRE_WIFI_RANGING_RESULT_TIMEOUT_NS);
  }
 
  return success;
}
 
void WifiRequestManager::handleRangingEventSync(
    uint8_t errorCode, struct chreWifiRangingEvent *event) {
  if (postRangingAsyncResult(errorCode)) {
    if (errorCode != CHRE_ERROR_NONE) {
      LOGW("RTT ranging failed with error %d", errorCode);
    } else {
      EventLoopManagerSingleton::get()->getEventLoop().postEvent(
          CHRE_EVENT_WIFI_RANGING_RESULT, event, freeWifiRangingEventCallback,
          kSystemInstanceId, mPendingRangingRequests.front().nanoappInstanceId);
    }
    mPendingRangingRequests.pop();
  }
 
  // If we have any pending requests, try issuing them to the platform until the
  // first one succeeds
  while (!mPendingRangingRequests.empty() && !dispatchQueuedRangingRequest());
}
 
void WifiRequestManager::handleFreeWifiScanEvent(chreWifiScanEvent *scanEvent) {
  if (mScanRequestResultsArePending) {
    // Reset the event distribution logic once an entire scan event has been
    // received and processed by the nanoapp requesting the scan event.
    mScanEventResultCountAccumulator += scanEvent->resultCount;
    if (mScanEventResultCountAccumulator >= scanEvent->resultTotal) {
      mScanEventResultCountAccumulator = 0;
      mScanRequestResultsArePending = false;
    }
 
    if (!mScanRequestResultsArePending
        && mScanRequestingNanoappInstanceId.has_value()) {
      Nanoapp *nanoapp = EventLoopManagerSingleton::get()->getEventLoop()
          .findNanoappByInstanceId(*mScanRequestingNanoappInstanceId);
      if (nanoapp == nullptr) {
        LOGW("Attempted to unsubscribe unknown nanoapp from WiFi scan events");
      } else if (!nanoappHasScanMonitorRequest(
          *mScanRequestingNanoappInstanceId)) {
        nanoapp->unregisterForBroadcastEvent(CHRE_EVENT_WIFI_SCAN_RESULT);
      }
 
      mScanRequestingNanoappInstanceId.reset();
    }
  }
 
  mPlatformWifi.releaseScanEvent(scanEvent);
}
 
void WifiRequestManager::freeWifiScanEventCallback(uint16_t eventType,
                                                   void *eventData) {
  chreWifiScanEvent *scanEvent = static_cast<chreWifiScanEvent *>(eventData);
  EventLoopManagerSingleton::get()->getWifiRequestManager()
      .handleFreeWifiScanEvent(scanEvent);
}
 
void WifiRequestManager::freeWifiRangingEventCallback(uint16_t eventType,
                                                      void *eventData) {
  auto *event = static_cast<struct chreWifiRangingEvent *>(eventData);
  EventLoopManagerSingleton::get()->getWifiRequestManager()
      .mPlatformWifi.releaseRangingEvent(event);
}
 
}  // namespace chre