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