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 #ifndef ANDROID_HARDWARE_CONTEXTHUB_V1_0_CONTEXTHUB_H_
18 #define ANDROID_HARDWARE_CONTEXTHUB_V1_0_CONTEXTHUB_H_
19 
20 #include <unordered_map>
21 
22 #include <android-base/macros.h>
23 #include <android/hardware/contexthub/1.0/IContexthub.h>
24 #include <hardware/context_hub.h>
25 
26 namespace android {
27 namespace hardware {
28 namespace contexthub {
29 namespace V1_0 {
30 namespace implementation {
31 
32 struct Contexthub : public ::android::hardware::contexthub::V1_0::IContexthub {
33     Contexthub();
34 
35     Return<void> getHubs(getHubs_cb _hidl_cb) override;
36 
37     Return<Result> registerCallback(uint32_t hubId,
38                                     const sp<IContexthubCallback> &cb) override;
39 
40     Return<Result> sendMessageToHub(uint32_t hubId,
41                                     const ContextHubMsg &msg) override;
42 
43     Return<Result> loadNanoApp(uint32_t hubId,
44                                const NanoAppBinary& appBinary,
45                                uint32_t transactionId) override;
46 
47     Return<Result> unloadNanoApp(uint32_t hubId,
48                                  uint64_t appId,
49                                  uint32_t transactionId) override;
50 
51     Return<Result> enableNanoApp(uint32_t hubId,
52                                  uint64_t appId,
53                                  uint32_t transactionId) override;
54 
55     Return<Result> disableNanoApp(uint32_t hubId,
56                                   uint64_t appId,
57                                   uint32_t transactionId) override;
58 
59     Return<Result> queryApps(uint32_t hubId) override;
60 
61     Return<Result> reboot(uint32_t hubId);
62 
63     bool isInitialized();
64 
65 private:
66 
67     struct CachedHubInformation{
68         struct hub_app_name_t osAppName;
69         sp<IContexthubCallback> callback;
70     };
71 
72     class DeathRecipient : public hidl_death_recipient {
73     public:
74         DeathRecipient(const sp<Contexthub> contexthub);
75 
76         void serviceDied(
77                 uint64_t cookie,
78                 const wp<::android::hidl::base::V1_0::IBase>& who) override;
79 
80     private:
81         sp<Contexthub> mContexthub;
82     };
83 
84     status_t mInitCheck;
85     const struct context_hub_module_t *mContextHubModule;
86     std::unordered_map<uint32_t, CachedHubInformation> mCachedHubInfo;
87 
88     sp<DeathRecipient> mDeathRecipient;
89     bool mIsTransactionPending;
90     uint32_t mTransactionId;
91 
92     bool isValidHubId(uint32_t hubId);
93 
94     sp<IContexthubCallback> getCallBackForHubId(uint32_t hubId);
95 
96     int handleOsMessage(sp<IContexthubCallback> cb,
97                         uint32_t msgType,
98                         const uint8_t *msg,
99                         int msgLen);
100 
101     // Handle the case where the callback registered for the given hub ID dies
102     void handleServiceDeath(uint32_t hubId);
103 
104     static int contextHubCb(uint32_t hubId,
105                             const struct hub_message_t *rxMsg,
106                             void *cookie);
107 
108     bool setOsAppAsDestination(hub_message_t *msg, int hubId);
109 
110     DISALLOW_COPY_AND_ASSIGN(Contexthub);
111 };
112 
113 extern "C" IContexthub *HIDL_FETCH_IContexthub(const char *name);
114 
115 }  // namespace implementation
116 }  // namespace V1_0
117 }  // namespace contexthub
118 }  // namespace hardware
119 }  // namespace android
120 
121 #endif  // ANDROID_HARDWARE_CONTEXTHUB_V1_0_CONTEXTHUB_H_
122