1 /*
2  * Copyright (C) 2019 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 #include <utils/SystemClock.h>
18 
19 #include "OccupantAwareness.h"
20 
21 namespace android {
22 namespace hardware {
23 namespace automotive {
24 namespace occupant_awareness {
25 namespace V1_0 {
26 namespace implementation {
27 
28 using ndk::ScopedAStatus;
29 
30 static const int32_t kAllCapabilities = OccupantAwareness::CAP_PRESENCE_DETECTION |
31                                         OccupantAwareness::CAP_GAZE_DETECTION |
32                                         OccupantAwareness::CAP_DRIVER_MONITORING_DETECTION;
33 
34 constexpr int64_t kNanoSecondsPerMilliSecond = 1000 * 1000;
35 
startDetection(OccupantAwarenessStatus * status)36 ScopedAStatus OccupantAwareness::startDetection(OccupantAwarenessStatus* status) {
37     std::lock_guard<std::mutex> lock(mMutex);
38     if (mStatus != OccupantAwarenessStatus::NOT_INITIALIZED) {
39         return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
40     }
41 
42     mStatus = OccupantAwarenessStatus::READY;
43     mWorkerThread = std::thread(startWorkerThread, this);
44     if (mCallback) {
45         mCallback->onSystemStatusChanged(kAllCapabilities, mStatus);
46     }
47 
48     *status = mStatus;
49     return ScopedAStatus::ok();
50 }
51 
stopDetection(OccupantAwarenessStatus * status)52 ScopedAStatus OccupantAwareness::stopDetection(OccupantAwarenessStatus* status) {
53     std::lock_guard<std::mutex> lock(mMutex);
54     if (mStatus != OccupantAwarenessStatus::READY) {
55         return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
56     }
57 
58     mStatus = OccupantAwarenessStatus::NOT_INITIALIZED;
59     mWorkerThread.join();
60     if (mCallback) {
61         mCallback->onSystemStatusChanged(kAllCapabilities, mStatus);
62     }
63 
64     *status = mStatus;
65     return ScopedAStatus::ok();
66 }
67 
getCapabilityForRole(Role occupantRole,int32_t * capabilities)68 ScopedAStatus OccupantAwareness::getCapabilityForRole(Role occupantRole, int32_t* capabilities) {
69     if (!isValidRole(occupantRole)) {
70         return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
71     }
72 
73     int intVal = static_cast<int>(occupantRole);
74     if ((intVal & DetectionGenerator::getSupportedRoles()) == intVal) {
75         int capabilities_ = DetectionGenerator::getSupportedCapabilities();
76         if (occupantRole != Role::DRIVER) {
77             capabilities_ &= ~CAP_DRIVER_MONITORING_DETECTION;
78         }
79         *capabilities = capabilities_;
80     } else {
81         *capabilities = 0;
82     }
83 
84     return ScopedAStatus::ok();
85 }
86 
getState(Role occupantRole,int detectionCapability,OccupantAwarenessStatus * status)87 ScopedAStatus OccupantAwareness::getState(Role occupantRole, int detectionCapability,
88                                           OccupantAwarenessStatus* status) {
89     if (!isValidRole(occupantRole)) {
90         return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
91     }
92 
93     if (!isValidDetectionCapabilities(detectionCapability) ||
94         !isSingularCapability(detectionCapability)) {
95         return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
96     }
97 
98     int roleVal = static_cast<int>(occupantRole);
99 
100     if (((roleVal & DetectionGenerator::getSupportedRoles()) != roleVal) ||
101         ((detectionCapability & DetectionGenerator::getSupportedCapabilities()) !=
102          detectionCapability)) {
103         *status = OccupantAwarenessStatus::NOT_SUPPORTED;
104         return ScopedAStatus::ok();
105     }
106 
107     std::lock_guard<std::mutex> lock(mMutex);
108     *status = mStatus;
109     return ScopedAStatus::ok();
110 }
111 
setCallback(const std::shared_ptr<IOccupantAwarenessClientCallback> & callback)112 ScopedAStatus OccupantAwareness::setCallback(
113         const std::shared_ptr<IOccupantAwarenessClientCallback>& callback) {
114     if (callback == nullptr) {
115         return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
116     }
117 
118     std::lock_guard<std::mutex> lock(mMutex);
119     mCallback = callback;
120     return ScopedAStatus::ok();
121 }
122 
getLatestDetection(OccupantDetections * detections)123 ScopedAStatus OccupantAwareness::getLatestDetection(OccupantDetections* detections) {
124     std::lock_guard<std::mutex> lock(mMutex);
125 
126     if (mStatus != OccupantAwarenessStatus::READY) {
127         return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
128     }
129 
130     *detections = mLatestDetections;
131     return ScopedAStatus::ok();
132 }
133 
isValidRole(Role occupantRole)134 bool OccupantAwareness::isValidRole(Role occupantRole) {
135     int intVal = static_cast<int>(occupantRole);
136     int allOccupants = static_cast<int>(Role::ALL_OCCUPANTS);
137     return (occupantRole != Role::INVALID) && ((intVal & (~allOccupants)) == 0);
138 }
139 
isValidDetectionCapabilities(int detectionCapabilities)140 bool OccupantAwareness::isValidDetectionCapabilities(int detectionCapabilities) {
141     return (detectionCapabilities != OccupantAwareness::CAP_NONE) &&
142            ((detectionCapabilities & (~kAllCapabilities)) == 0);
143 }
144 
isSingularCapability(int detectionCapability)145 bool OccupantAwareness::isSingularCapability(int detectionCapability) {
146     // Check whether the value is 0, or the value has only one bit set.
147     return (detectionCapability & (detectionCapability - 1)) == 0;
148 }
149 
startWorkerThread(OccupantAwareness * occupantAwareness)150 void OccupantAwareness::startWorkerThread(OccupantAwareness* occupantAwareness) {
151     occupantAwareness->workerThreadFunction();
152 }
153 
workerThreadFunction()154 void OccupantAwareness::workerThreadFunction() {
155     bool isFirstDetection = true;
156     int64_t prevDetectionTimeMs;
157     while (mStatus == OccupantAwarenessStatus::READY) {
158         int64_t currentTimeMs = android::elapsedRealtimeNano() / kNanoSecondsPerMilliSecond;
159         if ((isFirstDetection) || (currentTimeMs - prevDetectionTimeMs > mDetectionDurationMs)) {
160             std::lock_guard<std::mutex> lock(mMutex);
161             mLatestDetections = mGenerator.GetNextDetections();
162             if (mCallback != nullptr) {
163                 mCallback->onDetectionEvent(mLatestDetections);
164             }
165             isFirstDetection = false;
166             prevDetectionTimeMs = currentTimeMs;
167         }
168     }
169 }
170 
171 }  // namespace implementation
172 }  // namespace V1_0
173 }  // namespace occupant_awareness
174 }  // namespace automotive
175 }  // namespace hardware
176 }  // namespace android
177