xref: /aosp_15_r20/system/chre/core/sensor_request_manager.cc (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifdef CHRE_SENSORS_SUPPORT_ENABLED
18 
19 #include "chre/core/sensor_request_manager.h"
20 
21 #include "chre/core/event_loop_manager.h"
22 #include "chre/util/macros.h"
23 #include "chre/util/nested_data_ptr.h"
24 #include "chre/util/system/debug_dump.h"
25 #include "chre/util/system/event_callbacks.h"
26 #include "chre/util/time.h"
27 #include "chre_api/chre/version.h"
28 
29 #define LOG_INVALID_HANDLE(x) \
30   LOGE("Invalid sensor handle %" PRIu32 ": line %d", x, __LINE__)
31 
32 namespace chre {
33 namespace {
34 
isSensorRequestValid(const Sensor & sensor,const SensorRequest & sensorRequest)35 bool isSensorRequestValid(const Sensor &sensor,
36                           const SensorRequest &sensorRequest) {
37   bool isRequestOneShot = sensorModeIsOneShot(sensorRequest.getMode());
38   bool isRequestOff = sensorRequest.getMode() == SensorMode::Off;
39   uint64_t requestedInterval = sensorRequest.getInterval().toRawNanoseconds();
40   bool isRequestPassive = sensorModeIsPassive(sensorRequest.getMode());
41 
42   bool success = false;
43   if (!isRequestOff && requestedInterval < sensor.getMinInterval()) {
44     LOGE("Requested interval %" PRIu64 " < sensor's minInterval %" PRIu64,
45          requestedInterval, sensor.getMinInterval());
46   } else if (!isRequestOff && isRequestOneShot != sensor.isOneShot()) {
47     LOGE("Invalid request type for sensor reporting mode");
48   } else if (isRequestPassive && !sensor.supportsPassiveMode()) {
49     LOGE("Passive mode not supported");
50   } else {
51     success = true;
52   }
53   return success;
54 }
55 
56 /**
57  * A helper function that updates the last event of a sensor in the main thread.
58  *
59  * @param eventData A non-null pointer to the sensor's CHRE event data.
60  */
updateLastEvent(void * eventData)61 void updateLastEvent(void *eventData) {
62   CHRE_ASSERT(eventData);
63 
64   auto callback = [](uint16_t /*type*/, void *data, void * /*extraData*/) {
65     auto *sensorData = static_cast<ChreSensorData *>(data);
66     Sensor *sensor =
67         EventLoopManagerSingleton::get()->getSensorRequestManager().getSensor(
68             sensorData->header.sensorHandle);
69 
70     // Mark last event as valid only if the sensor is enabled. Event data may
71     // arrive after sensor is disabled.
72     if (sensor != nullptr &&
73         sensor->getMaximalRequest().getMode() != SensorMode::Off) {
74       sensor->setLastEvent(sensorData);
75     }
76   };
77 
78   // Schedule a deferred callback.
79   EventLoopManagerSingleton::get()->deferCallback(
80       SystemCallbackType::SensorLastEventUpdate, eventData, callback);
81 }
82 
sensorDataEventFree(uint16_t eventType,void * eventData)83 void sensorDataEventFree(uint16_t eventType, void *eventData) {
84   EventLoopManagerSingleton::get()
85       ->getSensorRequestManager()
86       .releaseSensorDataEvent(eventType, eventData);
87 }
88 
89 /**
90  * Posts a CHRE_EVENT_SENSOR_SAMPLING_CHANGE event to the specified Nanoapp.
91  *
92  * @param instanceId The instance ID of the nanoapp with an open request.
93  * @param sensorHandle The handle of the sensor.
94  * @param status A reference of the sampling status to be posted.
95  */
postSamplingStatusEvent(uint16_t instanceId,uint32_t sensorHandle,const struct chreSensorSamplingStatus & status)96 void postSamplingStatusEvent(uint16_t instanceId, uint32_t sensorHandle,
97                              const struct chreSensorSamplingStatus &status) {
98   auto *event = memoryAlloc<struct chreSensorSamplingStatusEvent>();
99   if (event == nullptr) {
100     LOG_OOM();
101   } else {
102     event->sensorHandle = sensorHandle;
103     event->status = status;
104 
105     EventLoopManagerSingleton::get()->getEventLoop().postEventOrDie(
106         CHRE_EVENT_SENSOR_SAMPLING_CHANGE, event, freeEventDataCallback,
107         instanceId);
108   }
109 }
110 
111 /**
112  * Notifies all listening nanoapps of the latest sampling status update.
113  *
114  * @param sensorHandle The handle of the sensor.
115  * @param status A reference of the sampling status to be posted.
116  */
postSamplingStatus(uint32_t sensorHandle,struct chreSensorSamplingStatus & status)117 void postSamplingStatus(uint32_t sensorHandle,
118                         struct chreSensorSamplingStatus &status) {
119   // Only post to Nanoapps with an open request.
120   const DynamicVector<SensorRequest> &requests =
121       EventLoopManagerSingleton::get()->getSensorRequestManager().getRequests(
122           sensorHandle);
123   for (const auto &req : requests) {
124     postSamplingStatusEvent(req.getInstanceId(), sensorHandle, status);
125   }
126 }
127 
128 }  // namespace
129 
~SensorRequestManager()130 SensorRequestManager::~SensorRequestManager() {
131   for (size_t i = 0; i < mSensors.size(); i++) {
132     // Disable sensors that have been enabled previously.
133     removeAllRequests(mSensors[i]);
134   }
135 }
136 
init()137 void SensorRequestManager::init() {
138   // The Platform sensor must be initialized prior to interacting with any
139   // sensors.
140   mPlatformSensorManager.init();
141 
142   mSensors = mPlatformSensorManager.getSensors();
143 }
144 
getSensorHandle(uint8_t sensorType,uint8_t sensorIndex,uint16_t targetGroupId,uint32_t * sensorHandle) const145 bool SensorRequestManager::getSensorHandle(uint8_t sensorType,
146                                            uint8_t sensorIndex,
147                                            uint16_t targetGroupId,
148                                            uint32_t *sensorHandle) const {
149   CHRE_ASSERT(sensorHandle);
150 
151   bool sensorHandleIsValid = false;
152   for (uint32_t i = 0; i < mSensors.size(); i++) {
153     if ((mSensors[i].getSensorType() == sensorType) &&
154         (mSensors[i].getSensorIndex() == sensorIndex) &&
155         (BITMASK_HAS_VALUE(mSensors[i].getTargetGroupMask(), targetGroupId))) {
156       sensorHandleIsValid = true;
157       *sensorHandle = i;
158       break;
159     }
160   }
161 
162   return sensorHandleIsValid;
163 }
164 
setSensorRequest(Nanoapp * nanoapp,uint32_t sensorHandle,const SensorRequest & sensorRequest)165 bool SensorRequestManager::setSensorRequest(
166     Nanoapp *nanoapp, uint32_t sensorHandle,
167     const SensorRequest &sensorRequest) {
168   CHRE_ASSERT(nanoapp);
169 
170   bool success = false;
171   bool requestChanged = false;
172 
173   if (sensorHandle >= mSensors.size()) {
174     LOG_INVALID_HANDLE(sensorHandle);
175   } else {
176     Sensor &sensor = mSensors[sensorHandle];
177     if (isSensorRequestValid(sensor, sensorRequest)) {
178       // Copy the request so it can be modified below.
179       SensorRequest request = sensorRequest;
180       if (sensor.isOneShot()) {
181         // Always use a latency value of ASAP for one-shot sensors since
182         // one-shot data is always expected to be delivered immediately.
183         request.setLatency(Nanoseconds(CHRE_SENSOR_LATENCY_ASAP));
184       }
185 
186       size_t requestIndex;
187       uint8_t sensorType = sensor.getSensorType();
188       uint16_t eventType = getSampleEventTypeForSensorType(sensorType);
189       bool nanoappHasRequest =
190           sensor.getRequestMultiplexer().findRequest(nanoapp->getInstanceId(),
191                                                      &requestIndex) != nullptr;
192 
193       if (request.getMode() == SensorMode::Off) {
194         if (nanoappHasRequest) {
195           // The request changes the mode to off and there was an existing
196           // request. The existing request is removed from the multiplexer. The
197           // nanoapp is unregistered from events of this type if this request
198           // was successful.
199           success = removeRequest(sensor, requestIndex, &requestChanged);
200           if (success) {
201             cancelFlushRequests(sensorHandle, nanoapp->getInstanceId());
202 
203             // Only unregister if nanoapp no longer has an outstanding request
204             // for a sensor + target group mask.
205             uint16_t activeMask =
206                 getActiveTargetGroupMask(nanoapp->getInstanceId(), sensorType);
207             uint16_t inactiveMask = sensor.getTargetGroupMask() & ~activeMask;
208             if (inactiveMask != 0) {
209               nanoapp->unregisterForBroadcastEvent(eventType, inactiveMask);
210 
211               uint16_t biasEventType;
212               if (sensor.getBiasEventType(&biasEventType)) {
213                 // Per API requirements, turn off bias reporting when
214                 // unsubscribing from the sensor.
215                 nanoapp->unregisterForBroadcastEvent(biasEventType,
216                                                      inactiveMask);
217               }
218             }
219           }
220         } else {
221           // The sensor is being configured to Off, but is already Off (there is
222           // no existing request). We assign to success to be true and no other
223           // operation is required.
224           success = true;
225         }
226       } else if (!nanoappHasRequest) {
227         // The request changes the mode to the enabled state and there was no
228         // existing request. The request is newly created and added to the
229         // multiplexer. The nanoapp is registered for events if this request was
230         // successful.
231         uint16_t biasEventType;
232         if (sensor.getBiasEventType(&biasEventType) && sensor.isCalibrated()) {
233           // Per API requirements, turn on bias reporting for calibrated sensors
234           // by default when subscribed.
235           request.setBiasUpdatesRequested(true);
236         }
237 
238         success = addRequest(sensor, request, &requestChanged);
239         if (success) {
240           nanoapp->registerForBroadcastEvent(eventType,
241                                              sensor.getTargetGroupMask());
242 
243           if (request.getBiasUpdatesRequested()) {
244             nanoapp->registerForBroadcastEvent(biasEventType,
245                                                sensor.getTargetGroupMask());
246           }
247 
248           // Deliver last valid event to new clients of on-change sensors
249           if (sensor.getLastEvent() != nullptr) {
250             EventLoopManagerSingleton::get()->getEventLoop().postEventOrDie(
251                 eventType, sensor.getLastEvent(), nullptr /* freeCallback */,
252                 nanoapp->getInstanceId());
253           }
254         }
255       } else {
256         // Ensure bias events stay requested if they were previously enabled.
257         const SensorRequest &previousRequest =
258             sensor.getRequestMultiplexer().getRequests()[requestIndex];
259         if (previousRequest.getBiasUpdatesRequested()) {
260           request.setBiasUpdatesRequested(true);
261         }
262         // The request changes the mode to the enabled state and there was an
263         // existing request. The existing request is updated.
264         success = updateRequest(sensor, requestIndex, request, &requestChanged);
265       }
266 
267       // TODO: Allow translating the sensor request
268 
269       if (requestChanged) {
270         // TODO: Send an event to nanoapps to indicate the rate change.
271       }
272 
273       if (success) {
274         addSensorRequestLog(nanoapp->getInstanceId(), sensorHandle, request);
275       }
276     }
277   }
278 
279   return success;
280 }
281 
getSensorInfo(uint32_t sensorHandle,const Nanoapp & nanoapp,struct chreSensorInfo * info) const282 bool SensorRequestManager::getSensorInfo(uint32_t sensorHandle,
283                                          const Nanoapp &nanoapp,
284                                          struct chreSensorInfo *info) const {
285   CHRE_ASSERT(info);
286 
287   bool success = false;
288   if (sensorHandle >= mSensors.size()) {
289     LOG_INVALID_HANDLE(sensorHandle);
290   } else {
291     mSensors[sensorHandle].populateSensorInfo(info,
292                                               nanoapp.getTargetApiVersion());
293     success = true;
294   }
295 
296   return success;
297 }
298 
removeAllRequests(uint32_t sensorHandle)299 bool SensorRequestManager::removeAllRequests(uint32_t sensorHandle) {
300   bool success = false;
301   if (sensorHandle >= mSensors.size()) {
302     LOG_INVALID_HANDLE(sensorHandle);
303   } else {
304     Sensor &sensor = mSensors[sensorHandle];
305     uint8_t sensorType = sensor.getSensorType();
306     uint16_t eventType = getSampleEventTypeForSensorType(sensorType);
307     for (const SensorRequest &request : sensor.getRequests()) {
308       Nanoapp *nanoapp = EventLoopManagerSingleton::get()
309                              ->getEventLoop()
310                              .findNanoappByInstanceId(request.getInstanceId());
311       if (nanoapp != nullptr) {
312         nanoapp->unregisterForBroadcastEvent(eventType,
313                                              sensor.getTargetGroupMask());
314       }
315     }
316 
317     cancelFlushRequests(sensorHandle);
318     success = removeAllRequests(sensor);
319   }
320 
321   return success;
322 }
323 
getSensor(uint32_t sensorHandle)324 Sensor *SensorRequestManager::getSensor(uint32_t sensorHandle) {
325   Sensor *sensorPtr = nullptr;
326   if (sensorHandle < mSensors.size()) {
327     sensorPtr = &mSensors[sensorHandle];
328   }
329   return sensorPtr;
330 }
331 
getSensorSamplingStatus(uint32_t sensorHandle,struct chreSensorSamplingStatus * status) const332 bool SensorRequestManager::getSensorSamplingStatus(
333     uint32_t sensorHandle, struct chreSensorSamplingStatus *status) const {
334   CHRE_ASSERT(status);
335 
336   bool success = false;
337   if (sensorHandle >= mSensors.size()) {
338     LOG_INVALID_HANDLE(sensorHandle);
339   } else {
340     success = mSensors[sensorHandle].getSamplingStatus(status);
341   }
342 
343   return success;
344 }
345 
getRequests(uint32_t sensorHandle) const346 const DynamicVector<SensorRequest> &SensorRequestManager::getRequests(
347     uint32_t sensorHandle) const {
348   if (sensorHandle >= mSensors.size()) {
349     LOG_INVALID_HANDLE(sensorHandle);
350     sensorHandle = 0;
351   }
352   return mSensors[sensorHandle].getRequests();
353 }
354 
configureBiasEvents(Nanoapp * nanoapp,uint32_t sensorHandle,bool enable)355 bool SensorRequestManager::configureBiasEvents(Nanoapp *nanoapp,
356                                                uint32_t sensorHandle,
357                                                bool enable) {
358   bool success = false;
359   uint16_t eventType;
360   if (sensorHandle >= mSensors.size()) {
361     LOG_INVALID_HANDLE(sensorHandle);
362   } else if (enable && !mSensors[sensorHandle].isSensorEnabled()) {
363     LOGE("Bias events can't be configured for a disabled sensor!");
364   } else if (mSensors[sensorHandle].getBiasEventType(&eventType)) {
365     Sensor &sensor = mSensors[sensorHandle];
366     size_t requestIndex;
367     bool nanoappHasRequest =
368         sensor.getRequestMultiplexer().findRequest(nanoapp->getInstanceId(),
369                                                    &requestIndex) != nullptr;
370     if (enable && !nanoappHasRequest) {
371       LOGE("0x%" PRIx64
372            " configuring bias events without an existing sensor request",
373            nanoapp->getAppId());
374     } else if (!enable && !nanoappHasRequest) {
375       // Treat configuration request as a success since the nanoapp's request
376       // already has been removed which would result in disabling bias event
377       // updates
378       success = true;
379     } else {
380       SensorRequest previousRequest =
381           sensor.getRequestMultiplexer().getRequests()[requestIndex];
382       previousRequest.setBiasUpdatesRequested(enable);
383       bool requestChanged;
384       success =
385           updateRequest(sensor, requestIndex, previousRequest, &requestChanged);
386       if (success) {
387         if (enable) {
388           nanoapp->registerForBroadcastEvent(eventType,
389                                              sensor.getTargetGroupMask());
390         } else {
391           nanoapp->unregisterForBroadcastEvent(eventType,
392                                                sensor.getTargetGroupMask());
393         }
394       }
395     }
396   }
397 
398   return success;
399 }
400 
getThreeAxisBias(uint32_t sensorHandle,struct chreSensorThreeAxisData * bias) const401 bool SensorRequestManager::getThreeAxisBias(
402     uint32_t sensorHandle, struct chreSensorThreeAxisData *bias) const {
403   CHRE_ASSERT(bias != nullptr);
404 
405   bool success = false;
406   if (bias != nullptr) {
407     if (sensorHandle >= mSensors.size()) {
408       LOG_INVALID_HANDLE(sensorHandle);
409     } else {
410       success =
411           mPlatformSensorManager.getThreeAxisBias(mSensors[sensorHandle], bias);
412     }
413   }
414 
415   return success;
416 }
417 
flushAsync(Nanoapp * nanoapp,uint32_t sensorHandle,const void * cookie)418 bool SensorRequestManager::flushAsync(Nanoapp *nanoapp, uint32_t sensorHandle,
419                                       const void *cookie) {
420   bool success = false;
421 
422   uint16_t nanoappInstanceId = nanoapp->getInstanceId();
423   if (sensorHandle >= mSensors.size()) {
424     LOG_INVALID_HANDLE(sensorHandle);
425   } else if (mSensors[sensorHandle].isOneShot()) {
426     LOGE("Cannot flush a one-shot sensor of type %" PRIu8,
427          mSensors[sensorHandle].getSensorType());
428   } else if (mFlushRequestQueue.full()) {
429     LOG_OOM();
430   } else {
431     mFlushRequestQueue.emplace_back(sensorHandle, nanoappInstanceId, cookie);
432     success = makeFlushRequest(mFlushRequestQueue.back()) == CHRE_ERROR_NONE;
433     if (!success) {
434       mFlushRequestQueue.pop_back();
435     }
436   }
437 
438   return success;
439 }
440 
releaseSensorDataEvent(uint16_t eventType,void * eventData)441 void SensorRequestManager::releaseSensorDataEvent(uint16_t eventType,
442                                                   void *eventData) {
443   // Remove all requests if it's a one-shot sensor and only after data has been
444   // delivered to all clients.
445   mPlatformSensorManager.releaseSensorDataEvent(eventData);
446   uint8_t sensorType = getSensorTypeForSampleEventType(eventType);
447   uint32_t sensorHandle;
448   if (getDefaultSensorHandle(sensorType, &sensorHandle) &&
449       mSensors[sensorHandle].isOneShot()) {
450     removeAllRequests(sensorHandle);
451   }
452 }
453 
handleFlushCompleteEvent(uint32_t sensorHandle,uint32_t flushRequestId,uint8_t errorCode)454 void SensorRequestManager::handleFlushCompleteEvent(uint32_t sensorHandle,
455                                                     uint32_t flushRequestId,
456                                                     uint8_t errorCode) {
457   UNUSED_VAR(flushRequestId);
458 
459   if (sensorHandle < mSensors.size() &&
460       mSensors[sensorHandle].isFlushRequestPending()) {
461     // Cancel flush request timer before posting to the event queue to ensure
462     // a timeout event isn't processed by CHRE now that the complete event
463     // has been received.
464     mSensors[sensorHandle].cancelPendingFlushRequestTimer();
465 
466     auto callback = [](uint16_t /*type*/, void *data, void *extraData) {
467       uint8_t cbErrorCode = NestedDataPtr<uint8_t>(data);
468       uint32_t cbSensorHandle = NestedDataPtr<uint32_t>(extraData);
469       EventLoopManagerSingleton::get()
470           ->getSensorRequestManager()
471           .handleFlushCompleteEventSync(cbErrorCode, cbSensorHandle);
472     };
473 
474     EventLoopManagerSingleton::get()->deferCallback(
475         SystemCallbackType::SensorFlushComplete,
476         NestedDataPtr<uint8_t>(errorCode), callback,
477         NestedDataPtr<uint32_t>(sensorHandle));
478   }
479 }
480 
handleSensorDataEvent(uint32_t sensorHandle,void * event)481 void SensorRequestManager::handleSensorDataEvent(uint32_t sensorHandle,
482                                                  void *event) {
483   if (sensorHandle >= mSensors.size()) {
484     LOG_INVALID_HANDLE(sensorHandle);
485     mPlatformSensorManager.releaseSensorDataEvent(event);
486   } else {
487     Sensor &sensor = mSensors[sensorHandle];
488     if (sensor.isOnChange()) {
489       updateLastEvent(event);
490     }
491 
492     uint16_t eventType =
493         getSampleEventTypeForSensorType(sensor.getSensorType());
494 
495     // Only allow dropping continuous sensor events since losing one-shot or
496     // on-change events could result in nanoapps stuck in a bad state.
497     if (sensor.isContinuous()) {
498       EventLoopManagerSingleton::get()
499           ->getEventLoop()
500           .postLowPriorityEventOrFree(eventType, event, sensorDataEventFree,
501                                       kSystemInstanceId, kBroadcastInstanceId,
502                                       sensor.getTargetGroupMask());
503     } else {
504       EventLoopManagerSingleton::get()->getEventLoop().postEventOrDie(
505           eventType, event, sensorDataEventFree, kBroadcastInstanceId,
506           sensor.getTargetGroupMask());
507     }
508   }
509 }
510 
handleSamplingStatusUpdate(uint32_t sensorHandle,struct chreSensorSamplingStatus * status)511 void SensorRequestManager::handleSamplingStatusUpdate(
512     uint32_t sensorHandle, struct chreSensorSamplingStatus *status) {
513   Sensor *sensor =
514       EventLoopManagerSingleton::get()->getSensorRequestManager().getSensor(
515           sensorHandle);
516   if (sensor == nullptr || sensor->isOneShot()) {
517     releaseSamplingStatusUpdate(status);
518   } else {
519     sensor->setSamplingStatus(*status);
520 
521     auto callback = [](uint16_t /*type*/, void *data, void *extraData) {
522       uint32_t cbSensorHandle = NestedDataPtr<uint32_t>(data);
523       auto *cbStatus =
524           static_cast<struct chreSensorSamplingStatus *>(extraData);
525       postSamplingStatus(cbSensorHandle, *cbStatus);
526       EventLoopManagerSingleton::get()
527           ->getSensorRequestManager()
528           .releaseSamplingStatusUpdate(cbStatus);
529     };
530 
531     // Schedule a deferred callback to handle sensor status change in the main
532     // thread.
533     EventLoopManagerSingleton::get()->deferCallback(
534         SystemCallbackType::SensorStatusUpdate,
535         NestedDataPtr<uint32_t>(sensorHandle), callback, status);
536   }
537 }
538 
handleBiasEvent(uint32_t sensorHandle,void * biasData)539 void SensorRequestManager::handleBiasEvent(uint32_t sensorHandle,
540                                            void *biasData) {
541   Sensor *sensor =
542       EventLoopManagerSingleton::get()->getSensorRequestManager().getSensor(
543           sensorHandle);
544   CHRE_ASSERT(sensor != nullptr);
545 
546   if (sensor == nullptr) {
547     releaseBiasData(biasData);
548   } else {
549     uint16_t eventType;
550     if (!sensor->reportsBiasEvents() || !sensor->getBiasEventType(&eventType)) {
551       LOGE("Received bias event for unsupported sensor type %s",
552            sensor->getSensorName());
553     } else {
554       auto freeCallback = [](uint16_t /* type */, void *data) {
555         EventLoopManagerSingleton::get()
556             ->getSensorRequestManager()
557             .releaseBiasData(data);
558       };
559 
560       EventLoopManagerSingleton::get()->getEventLoop().postEventOrDie(
561           eventType, biasData, freeCallback, kBroadcastInstanceId,
562           sensor->getTargetGroupMask());
563     }
564   }
565 }
566 
logStateToBuffer(DebugDumpWrapper & debugDump) const567 void SensorRequestManager::logStateToBuffer(DebugDumpWrapper &debugDump) const {
568   debugDump.print("\nSensors:\n");
569   for (uint8_t i = 0; i < mSensors.size(); i++) {
570     for (const auto &request : mSensors[i].getRequests()) {
571       // TODO: Rearrange these prints to be similar to sensor request logs
572       // below
573       debugDump.print(
574           " %s: mode=%d int=%" PRIu64 " lat=%" PRIu64 " nappId=%" PRIu16 "\n",
575           mSensors[i].getSensorTypeName(), static_cast<int>(request.getMode()),
576           request.getInterval().toRawNanoseconds(),
577           request.getLatency().toRawNanoseconds(), request.getInstanceId());
578     }
579   }
580   debugDump.print("\n Last %zu Sensor Requests:\n", mSensorRequestLogs.size());
581   static_assert(kMaxSensorRequestLogs <= INT8_MAX,
582                 "kMaxSensorRequestLogs must be <= INT8_MAX");
583   for (int8_t i = static_cast<int8_t>(mSensorRequestLogs.size()) - 1; i >= 0;
584        i--) {
585     const auto &log = mSensorRequestLogs[static_cast<size_t>(i)];
586     const Sensor &sensor = mSensors[log.sensorHandle];
587     debugDump.print("  ts=%" PRIu64 " nappId=%" PRIu16 " type=%s idx=%" PRIu8
588                     " mask=%" PRIx16 " mode=%s",
589                     log.timestamp.toRawNanoseconds(), log.instanceId,
590                     sensor.getSensorTypeName(), sensor.getSensorIndex(),
591                     sensor.getTargetGroupMask(), getSensorModeName(log.mode));
592 
593     if (sensorModeIsContinuous(log.mode)) {
594       debugDump.print(" int=%" PRIu64 " lat=%" PRIu64,
595                       log.interval.toRawNanoseconds(),
596                       log.latency.toRawNanoseconds());
597     }
598     debugDump.print("\n");
599   }
600 }
601 
disableAllSubscriptions(Nanoapp * nanoapp)602 uint32_t SensorRequestManager::disableAllSubscriptions(Nanoapp *nanoapp) {
603   uint32_t numDisabledSubscriptions = 0;
604 
605   const uint32_t numSensors = static_cast<uint32_t>(mSensors.size());
606   for (uint32_t handle = 0; handle < numSensors; handle++) {
607     Sensor &sensor = mSensors[handle];
608     bool nanoappHasRequest =
609         sensor.getRequestMultiplexer().findRequest(
610             nanoapp->getInstanceId(), nullptr /*index*/) != nullptr;
611     if (nanoappHasRequest) {
612       numDisabledSubscriptions++;
613       SensorRequest request(SensorMode::Off, Nanoseconds() /*interval*/,
614                             Nanoseconds() /*latency*/);
615       setSensorRequest(nanoapp, handle, request);
616     }
617   }
618 
619   return numDisabledSubscriptions;
620 }
621 
postFlushCompleteEvent(uint32_t sensorHandle,uint8_t errorCode,const FlushRequest & request)622 void SensorRequestManager::postFlushCompleteEvent(uint32_t sensorHandle,
623                                                   uint8_t errorCode,
624                                                   const FlushRequest &request) {
625   auto *event = memoryAlloc<chreSensorFlushCompleteEvent>();
626   if (event == nullptr) {
627     LOG_OOM();
628   } else {
629     event->sensorHandle = sensorHandle;
630     event->errorCode = errorCode;
631     event->cookie = request.cookie;
632     memset(event->reserved, 0, sizeof(event->reserved));
633 
634     EventLoopManagerSingleton::get()->getEventLoop().postEventOrDie(
635         CHRE_EVENT_SENSOR_FLUSH_COMPLETE, event, freeEventDataCallback,
636         request.nanoappInstanceId);
637   }
638 }
639 
completeFlushRequestAtIndex(size_t index,uint8_t errorCode)640 void SensorRequestManager::completeFlushRequestAtIndex(size_t index,
641                                                        uint8_t errorCode) {
642   if (index < mFlushRequestQueue.size()) {
643     const FlushRequest &request = mFlushRequestQueue[index];
644     uint32_t sensorHandle = request.sensorHandle;
645     if (request.isActive) {
646       mSensors[sensorHandle].clearPendingFlushRequest();
647     }
648 
649     postFlushCompleteEvent(sensorHandle, errorCode, request);
650     mFlushRequestQueue.erase(index);
651   }
652 }
653 
dispatchNextFlushRequest(uint32_t sensorHandle)654 void SensorRequestManager::dispatchNextFlushRequest(uint32_t sensorHandle) {
655   for (size_t i = 0; i < mFlushRequestQueue.size(); i++) {
656     FlushRequest &request = mFlushRequestQueue[i];
657     if (request.sensorHandle == sensorHandle) {
658       uint8_t newRequestErrorCode = makeFlushRequest(request);
659       if (newRequestErrorCode == CHRE_ERROR_NONE) {
660         break;
661       } else {
662         completeFlushRequestAtIndex(i, newRequestErrorCode);
663         i--;
664       }
665     }
666   }
667 }
668 
onFlushTimeout(uint32_t sensorHandle)669 void SensorRequestManager::onFlushTimeout(uint32_t sensorHandle) {
670   if (sensorHandle < mSensors.size()) {
671     Sensor &sensor = mSensors[sensorHandle];
672     sensor.setFlushRequestTimerHandle(CHRE_TIMER_INVALID);
673   }
674 }
675 
handleFlushCompleteEventSync(uint8_t errorCode,uint32_t sensorHandle)676 void SensorRequestManager::handleFlushCompleteEventSync(uint8_t errorCode,
677                                                         uint32_t sensorHandle) {
678   for (size_t i = 0; i < mFlushRequestQueue.size(); i++) {
679     if (mFlushRequestQueue[i].sensorHandle == sensorHandle) {
680       completeFlushRequestAtIndex(i, errorCode);
681       dispatchNextFlushRequest(sensorHandle);
682       break;
683     }
684   }
685 }
686 
cancelFlushRequests(uint32_t sensorHandle,uint32_t nanoappInstanceId)687 void SensorRequestManager::cancelFlushRequests(uint32_t sensorHandle,
688                                                uint32_t nanoappInstanceId) {
689   bool removeAll = (nanoappInstanceId == kSystemInstanceId);
690   for (size_t i = 0; i < mFlushRequestQueue.size(); i++) {
691     const FlushRequest &request = mFlushRequestQueue[i];
692     if (request.sensorHandle == sensorHandle &&
693         (request.nanoappInstanceId == nanoappInstanceId || removeAll)) {
694       completeFlushRequestAtIndex(i,
695                                   CHRE_ERROR_FUNCTION_DISABLED /* errorCode */);
696       i--;
697     }
698   }
699 
700   if (!mSensors[sensorHandle].isFlushRequestPending()) {
701     dispatchNextFlushRequest(sensorHandle);
702   }
703 }
704 
addSensorRequestLog(uint16_t nanoappInstanceId,uint32_t sensorHandle,const SensorRequest & sensorRequest)705 void SensorRequestManager::addSensorRequestLog(
706     uint16_t nanoappInstanceId, uint32_t sensorHandle,
707     const SensorRequest &sensorRequest) {
708   mSensorRequestLogs.kick_push(SensorRequestLog(
709       SystemTime::getMonotonicTime(), nanoappInstanceId, sensorHandle,
710       sensorRequest.getMode(), sensorRequest.getInterval(),
711       sensorRequest.getLatency()));
712 }
713 
addRequest(Sensor & sensor,const SensorRequest & request,bool * requestChanged)714 bool SensorRequestManager::addRequest(Sensor &sensor,
715                                       const SensorRequest &request,
716                                       bool *requestChanged) {
717   CHRE_ASSERT(requestChanged != nullptr);
718 
719   size_t addIndex;
720   bool success = true;
721   SensorRequestMultiplexer &multiplexer = sensor.getRequestMultiplexer();
722   SensorRequest prevRequest = sensor.getMaximalRequest();
723   if (!multiplexer.addRequest(request, &addIndex, requestChanged)) {
724     *requestChanged = false;
725     success = false;
726     LOG_OOM();
727   } else if (*requestChanged) {
728     success = configurePlatformSensor(sensor, prevRequest);
729     if (!success) {
730       // Remove the newly added request since the platform failed to handle
731       // it. The sensor is expected to maintain the existing request so there is
732       // no need to reset the platform to the last maximal request.
733       multiplexer.removeRequest(addIndex, requestChanged);
734 
735       // This is a roll-back operation so the maximal change in the
736       // multiplexer must not have changed. The request changed state is forced
737       // to false.
738       *requestChanged = false;
739     }
740   }
741 
742   return success;
743 }
744 
updateRequest(Sensor & sensor,size_t updateIndex,const SensorRequest & request,bool * requestChanged)745 bool SensorRequestManager::updateRequest(Sensor &sensor, size_t updateIndex,
746                                          const SensorRequest &request,
747                                          bool *requestChanged) {
748   CHRE_ASSERT(requestChanged != nullptr);
749 
750   bool success = true;
751   SensorRequestMultiplexer &multiplexer = sensor.getRequestMultiplexer();
752   SensorRequest previousRequest = multiplexer.getRequests()[updateIndex];
753   SensorRequest prevMaxRequest = sensor.getMaximalRequest();
754 
755   multiplexer.updateRequest(updateIndex, request, requestChanged);
756   if (*requestChanged) {
757     success = configurePlatformSensor(sensor, prevMaxRequest);
758     if (!success) {
759       // Roll back the request since sending it to the sensor failed. The
760       // request will roll back to the previous maximal. The sensor is
761       // expected to maintain the existing request if a request fails so there
762       // is no need to reset the platform to the last maximal request.
763       multiplexer.updateRequest(updateIndex, previousRequest, requestChanged);
764 
765       // This is a roll-back operation so the maximal change in the multiplexer
766       // must not have changed. The request changed state is forced to false.
767       *requestChanged = false;
768     }
769   }
770 
771   return success;
772 }
773 
removeRequest(Sensor & sensor,size_t removeIndex,bool * requestChanged)774 bool SensorRequestManager::removeRequest(Sensor &sensor, size_t removeIndex,
775                                          bool *requestChanged) {
776   CHRE_ASSERT(requestChanged != nullptr);
777 
778   bool success = true;
779   const SensorRequest prevRequest = sensor.getMaximalRequest();
780   sensor.getRequestMultiplexer().removeRequest(removeIndex, requestChanged);
781   if (*requestChanged) {
782     success = configurePlatformSensor(sensor, prevRequest);
783     if (!success) {
784       LOGE("SensorRequestManager failed to remove a request");
785 
786       // If the platform fails to handle this request in a debug build there is
787       // likely an error in the platform. This is not strictly a programming
788       // error but it does make sense to use assert semantics when a platform
789       // fails to handle a request that it had been sent previously.
790       CHRE_ASSERT(false);
791 
792       // The request to the platform to set a request when removing has failed
793       // so the request has not changed.
794       *requestChanged = false;
795     }
796   }
797   return success;
798 }
799 
removeAllRequests(Sensor & sensor)800 bool SensorRequestManager::removeAllRequests(Sensor &sensor) {
801   bool requestChanged;
802   SensorRequest prevRequest = sensor.getMaximalRequest();
803   sensor.getRequestMultiplexer().removeAllRequests(&requestChanged);
804 
805   bool success = true;
806   if (requestChanged) {
807     success = configurePlatformSensor(sensor, prevRequest);
808 
809     if (!success) {
810       LOGE("SensorRequestManager failed to remove all requests");
811 
812       // If the platform fails to handle this request in a debug build there
813       // is likely an error in the platform. This is not strictly a programming
814       // error but it does make sense to use assert semantics when a platform
815       // fails to handle a request that it had been sent previously.
816       CHRE_ASSERT(false);
817     }
818   }
819 
820   return success;
821 }
822 
makeFlushRequest(FlushRequest & request)823 uint8_t SensorRequestManager::makeFlushRequest(FlushRequest &request) {
824   uint8_t errorCode = CHRE_ERROR;
825   Sensor &sensor = mSensors[request.sensorHandle];
826   if (!sensor.isSensorEnabled()) {
827     LOGE("Cannot flush on disabled sensor");
828   } else if (!sensor.isFlushRequestPending()) {
829     Nanoseconds now = SystemTime::getMonotonicTime();
830     Nanoseconds deadline = request.deadlineTimestamp;
831     if (now >= deadline) {
832       LOGE("Flush sensor %s failed for nanoapp ID %" PRIu16
833            ": deadline exceeded",
834            sensor.getSensorName(), request.nanoappInstanceId);
835       errorCode = CHRE_ERROR_TIMEOUT;
836     } else if (doMakeFlushRequest(sensor)) {
837       errorCode = CHRE_ERROR_NONE;
838       Nanoseconds delay = deadline - now;
839       request.isActive = true;
840 
841       auto callback = [](uint16_t /*type*/, void *data, void * /*extraData*/) {
842         LOGE("Flush request timed out");
843         NestedDataPtr<uint32_t> sensorHandle(data);
844         EventLoopManagerSingleton::get()
845             ->getSensorRequestManager()
846             .onFlushTimeout(sensorHandle);
847 
848         // Send a complete event, thus closing out this flush request. If the
849         // request that has just timed out receives a response later, this may
850         // inadvertently close out a new request before it has actually
851         // completed.
852         // TODO: Attach an ID to all flush requests / responses so stale
853         // responses can be properly dropped.
854         EventLoopManagerSingleton::get()
855             ->getSensorRequestManager()
856             .handleFlushCompleteEventSync(CHRE_ERROR_TIMEOUT, sensorHandle);
857       };
858 
859       sensor.setFlushRequestTimerHandle(
860           EventLoopManagerSingleton::get()->setDelayedCallback(
861               SystemCallbackType::SensorFlushTimeout,
862               NestedDataPtr<uint32_t>(request.sensorHandle), callback, delay));
863     }
864   } else {
865     // Flush request will be made once the pending request is completed.
866     // Return true so that the nanoapp can wait for a result through the
867     // CHRE_EVENT_SENSOR_FLUSH_COMPLETE event.
868     errorCode = CHRE_ERROR_NONE;
869   }
870 
871   return errorCode;
872 }
873 
doMakeFlushRequest(Sensor & sensor)874 bool SensorRequestManager::doMakeFlushRequest(Sensor &sensor) {
875   // Set to true before making the request since the request may be a
876   // synchronous request and we may get the complete event before it returns.
877   sensor.setFlushRequestPending(true);
878   // TODO: Refactor code to take the request ID into account so multiple flush
879   // requests can be issued.
880   uint32_t flushRequestId;
881   bool success = mPlatformSensorManager.flush(sensor, &flushRequestId);
882   sensor.setFlushRequestPending(success);
883   return success;
884 }
885 
configurePlatformSensor(Sensor & sensor,const SensorRequest & prevSensorRequest)886 bool SensorRequestManager::configurePlatformSensor(
887     Sensor &sensor, const SensorRequest &prevSensorRequest) {
888   bool success = false;
889   const SensorRequest &request = sensor.getMaximalRequest();
890 
891   // Ensures that only configureBiasEvents is invoked if that's the only value
892   // that has changed since the previous request since CHRE shouldn't configure
893   // the platform for data events if the sensor data request hasn't changed.
894   bool biasChanged = (request.getBiasUpdatesRequested() !=
895                       prevSensorRequest.getBiasUpdatesRequested());
896   bool onlyBiasChanged = request.onlyBiasRequestUpdated(prevSensorRequest);
897   uint64_t currentLatency = 0;
898   bool enable = (request.getMode() != SensorMode::Off);
899   if (enable) {
900     currentLatency = request.getLatency().toRawNanoseconds();
901   }
902 
903   // Per platform API requirements, an active sensor subscription must exist
904   // before any bias configuration can be done.
905   if (!onlyBiasChanged &&
906       !mPlatformSensorManager.configureSensor(sensor, request)) {
907     LOGE("Failed to make platform sensor data request");
908   } else if (biasChanged &&
909              !mPlatformSensorManager.configureBiasEvents(
910                  sensor, request.getBiasUpdatesRequested(), currentLatency)) {
911     LOGE("Failed to make platform sensor bias request");
912     if (!onlyBiasChanged) {
913       mPlatformSensorManager.configureSensor(sensor, prevSensorRequest);
914     }
915   } else {
916     success = true;
917 
918     // Reset last event if an on-change sensor is turned off.
919     if (request.getMode() == SensorMode::Off) {
920       sensor.clearLastEvent();
921     }
922   }
923   return success;
924 }
925 
getActiveTargetGroupMask(uint16_t nanoappInstanceId,uint8_t sensorType)926 uint16_t SensorRequestManager::getActiveTargetGroupMask(
927     uint16_t nanoappInstanceId, uint8_t sensorType) {
928   uint16_t mask = 0;
929   for (Sensor &sensor : mSensors) {
930     if (sensor.getSensorType() == sensorType) {
931       size_t index;
932       if (sensor.getRequestMultiplexer().findRequest(nanoappInstanceId,
933                                                      &index) != nullptr) {
934         mask |= sensor.getTargetGroupMask();
935         break;
936       }
937     }
938   }
939 
940   return mask;
941 }
942 
943 }  // namespace chre
944 
945 #endif  // CHRE_SENSORS_SUPPORT_ENABLED
946