Kathan Shukla | b6261cb | 2019-11-25 22:52:12 -0800 | [diff] [blame] | 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 "OccupantAwareness.h" |
| 18 | |
| 19 | namespace android { |
| 20 | namespace hardware { |
| 21 | namespace automotive { |
| 22 | namespace occupant_awareness { |
| 23 | namespace V1_0 { |
| 24 | namespace implementation { |
| 25 | |
| 26 | using ndk::ScopedAStatus; |
| 27 | |
Kathan Shukla | 88d63cc | 2019-12-04 15:51:14 -0800 | [diff] [blame] | 28 | static const int32_t kAllCapabilities = OccupantAwareness::CAP_PRESENCE_DETECTION | |
Kathan Shukla | b6261cb | 2019-11-25 22:52:12 -0800 | [diff] [blame] | 29 | OccupantAwareness::CAP_GAZE_DETECTION | |
| 30 | OccupantAwareness::CAP_DRIVER_MONITORING_DETECTION; |
| 31 | |
| 32 | ScopedAStatus OccupantAwareness::startDetection(OccupantAwarenessStatus* status) { |
| 33 | std::lock_guard<std::mutex> lock(mMutex); |
| 34 | if (mStatus != OccupantAwarenessStatus::NOT_SUPPORTED) { |
| 35 | mStatus = OccupantAwarenessStatus::NOT_SUPPORTED; |
| 36 | if (mCallback) { |
| 37 | mCallback->onSystemStatusChanged(kAllCapabilities, |
| 38 | OccupantAwarenessStatus::NOT_SUPPORTED); |
| 39 | } |
| 40 | } |
| 41 | *status = mStatus; |
| 42 | return ScopedAStatus::ok(); |
| 43 | } |
| 44 | |
| 45 | ScopedAStatus OccupantAwareness::stopDetection(OccupantAwarenessStatus* status) { |
| 46 | std::lock_guard<std::mutex> lock(mMutex); |
| 47 | if (mStatus != OccupantAwarenessStatus::NOT_INITIALIZED) { |
| 48 | mStatus = OccupantAwarenessStatus::NOT_INITIALIZED; |
| 49 | if (mCallback) { |
| 50 | mCallback->onSystemStatusChanged(kAllCapabilities, |
| 51 | OccupantAwarenessStatus::NOT_INITIALIZED); |
| 52 | } |
| 53 | } |
| 54 | *status = mStatus; |
| 55 | return ScopedAStatus::ok(); |
| 56 | } |
| 57 | |
| 58 | ScopedAStatus OccupantAwareness::getCapabilityForRole(Role occupantRole, int32_t* capabilities) { |
| 59 | if (!isValidRole(occupantRole)) { |
| 60 | return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED); |
| 61 | } |
| 62 | |
| 63 | // No awareness capability for default HAL. |
| 64 | *capabilities = 0; |
| 65 | return ScopedAStatus::ok(); |
| 66 | } |
| 67 | |
| 68 | ScopedAStatus OccupantAwareness::getState(Role occupantRole, int detectionCapability, |
| 69 | OccupantAwarenessStatus* status) { |
| 70 | if (!isValidRole(occupantRole)) { |
| 71 | return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED); |
| 72 | } |
| 73 | |
| 74 | if (!isValidDetectionCapabilities(detectionCapability) || |
| 75 | !isSingularCapability(detectionCapability)) { |
| 76 | return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED); |
| 77 | } |
| 78 | |
| 79 | std::lock_guard<std::mutex> lock(mMutex); |
| 80 | *status = mStatus; |
| 81 | return ScopedAStatus::ok(); |
| 82 | } |
| 83 | |
| 84 | ScopedAStatus OccupantAwareness::setCallback( |
| 85 | const std::shared_ptr<IOccupantAwarenessClientCallback>& callback) { |
| 86 | if (callback == nullptr) { |
| 87 | return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED); |
| 88 | } |
| 89 | |
| 90 | std::lock_guard<std::mutex> lock(mMutex); |
| 91 | mCallback = callback; |
| 92 | return ScopedAStatus::ok(); |
| 93 | } |
| 94 | |
| 95 | ScopedAStatus OccupantAwareness::getLatestDetection(OccupantDetections* detections) { |
| 96 | // No detection generated for default hal. |
| 97 | (void)detections; |
| 98 | return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED); |
| 99 | } |
| 100 | |
| 101 | bool OccupantAwareness::isValidRole(Role occupantRole) { |
| 102 | int intVal = static_cast<int>(occupantRole); |
| 103 | int allOccupants = static_cast<int>(Role::ALL_OCCUPANTS); |
| 104 | return (occupantRole != Role::INVALID) && ((intVal & (~allOccupants)) == 0); |
| 105 | } |
| 106 | |
| 107 | bool OccupantAwareness::isValidDetectionCapabilities(int detectionCapabilities) { |
| 108 | return (detectionCapabilities != CAP_NONE) && |
| 109 | ((detectionCapabilities & (~kAllCapabilities)) == 0); |
| 110 | } |
| 111 | |
| 112 | bool OccupantAwareness::isSingularCapability(int detectionCapability) { |
| 113 | // Check whether the value is 0, or the value has only one bit set. |
| 114 | return (detectionCapability & (detectionCapability - 1)) == 0; |
| 115 | } |
| 116 | |
| 117 | } // namespace implementation |
| 118 | } // namespace V1_0 |
| 119 | } // namespace occupant_awareness |
| 120 | } // namespace automotive |
| 121 | } // namespace hardware |
| 122 | } // namespace android |