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