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 #pragma once
18 
19 #include <thread>
20 
21 #include <aidl/android/hardware/automotive/occupant_awareness/BnOccupantAwareness.h>
22 #include <aidl/android/hardware/automotive/occupant_awareness/BnOccupantAwarenessClientCallback.h>
23 #include <utils/StrongPointer.h>
24 
25 #include "DetectionGenerator.h"
26 
27 namespace android {
28 namespace hardware {
29 namespace automotive {
30 namespace occupant_awareness {
31 namespace V1_0 {
32 namespace implementation {
33 
34 using ::aidl::android::hardware::automotive::occupant_awareness::BnOccupantAwareness;
35 using ::aidl::android::hardware::automotive::occupant_awareness::IOccupantAwarenessClientCallback;
36 using ::aidl::android::hardware::automotive::occupant_awareness::OccupantAwarenessStatus;
37 using ::aidl::android::hardware::automotive::occupant_awareness::OccupantDetections;
38 using ::aidl::android::hardware::automotive::occupant_awareness::Role;
39 
40 /**
41  * The mock HAL can detect presence of Driver and front passenger, and driver awareness detection
42  * for driver.
43  **/
44 class OccupantAwareness : public BnOccupantAwareness {
45   public:
46     // Methods from ::android::hardware::automotive::occupant_awareness::IOccupantAwareness
47     // follow.
48     ndk::ScopedAStatus startDetection(OccupantAwarenessStatus* status) override;
49     ndk::ScopedAStatus stopDetection(OccupantAwarenessStatus* status) override;
50     ndk::ScopedAStatus getCapabilityForRole(Role occupantRole, int32_t* capabilities) override;
51     ndk::ScopedAStatus getState(Role occupantRole, int detectionCapability,
52                                 OccupantAwarenessStatus* status) override;
53     ndk::ScopedAStatus setCallback(
54             const std::shared_ptr<IOccupantAwarenessClientCallback>& callback) override;
55     ndk::ScopedAStatus getLatestDetection(OccupantDetections* detections) override;
56 
57   private:
58     bool isValidRole(Role occupantRole);
59     bool isValidDetectionCapabilities(int detectionCapabilities);
60     bool isSingularCapability(int detectionCapability);
61 
62     void workerThreadFunction();
63     static void startWorkerThread(OccupantAwareness* occupantAwareness);
64 
65     std::mutex mMutex;
66     std::shared_ptr<IOccupantAwarenessClientCallback> mCallback = nullptr;
67     OccupantAwarenessStatus mStatus = OccupantAwarenessStatus::NOT_INITIALIZED;
68 
69     OccupantDetections mLatestDetections;
70     std::thread mWorkerThread;
71 
72     DetectionGenerator mGenerator;
73 
74     // Generate a new detection every 1ms.
75     const int64_t mDetectionDurationMs = 1;
76 };
77 
78 }  // namespace implementation
79 }  // namespace V1_0
80 }  // namespace occupant_awareness
81 }  // namespace automotive
82 }  // namespace hardware
83 }  // namespace android
84