1 /*
2  * Copyright (C) 2017 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 <future>
21 
22 #include <android/hardware/contexthub/1.0/IContexthub.h>
23 #include <hidl/MQDescriptor.h>
24 #include <hidl/Status.h>
25 
26 #include "chre_host/socket_client.h"
27 #include "chre_host/host_protocol_host.h"
28 
29 namespace android {
30 namespace hardware {
31 namespace contexthub {
32 namespace V1_0 {
33 namespace implementation {
34 
35 using ::android::hardware::contexthub::V1_0::ContextHub;
36 using ::android::hardware::contexthub::V1_0::ContextHubMsg;
37 using ::android::hardware::contexthub::V1_0::IContexthub;
38 using ::android::hardware::contexthub::V1_0::IContexthubCallback;
39 using ::android::hardware::contexthub::V1_0::NanoAppBinary;
40 using ::android::hardware::contexthub::V1_0::Result;
41 using ::android::hardware::hidl_string;
42 using ::android::hardware::hidl_vec;
43 using ::android::hardware::Return;
44 using ::android::sp;
45 
46 class GenericContextHub : public IContexthub {
47  public:
48   GenericContextHub();
49 
50   // Methods from ::android::hardware::contexthub::V1_0::IContexthub follow.
51   Return<void> getHubs(getHubs_cb _hidl_cb) override;
52   Return<Result> registerCallback(uint32_t hubId, const sp<IContexthubCallback>& cb) override;
53   Return<Result> sendMessageToHub(uint32_t hubId, const ContextHubMsg& msg) override;
54   Return<Result> loadNanoApp(uint32_t hubId, const NanoAppBinary& appBinary, uint32_t transactionId) override;
55   Return<Result> unloadNanoApp(uint32_t hubId, uint64_t appId, uint32_t transactionId) override;
56   Return<Result> enableNanoApp(uint32_t hubId, uint64_t appId, uint32_t transactionId) override;
57   Return<Result> disableNanoApp(uint32_t hubId, uint64_t appId, uint32_t transactionId) override;
58   Return<Result> queryApps(uint32_t hubId) override;
59 
60  private:
61   ::android::chre::SocketClient mClient;
62   sp<IContexthubCallback> mCallbacks;
63 
64   class SocketCallbacks : public ::android::chre::SocketClient::ICallbacks,
65                           public ::android::chre::IChreMessageHandlers {
66    public:
67     SocketCallbacks(GenericContextHub& parent);
68     void onMessageReceived(const void *data, size_t length) override;
69     void onConnected() override;
70     void onDisconnected() override;
71 
72     void handleNanoappMessage(
73         uint64_t appId, uint32_t messageType, uint16_t hostEndpoint,
74         const void *messageData, size_t messageDataLen) override;
75     void handleHubInfoResponse(
76         const char *name, const char *vendor,
77         const char *toolchain, uint32_t legacyPlatformVersion,
78         uint32_t legacyToolchainVersion, float peakMips, float stoppedPower,
79         float sleepPower, float peakPower, uint32_t maxMessageLen,
80         uint64_t platformId, uint32_t version) override;
81 
82     void handleNanoappListResponse(
83         const ::chre::fbs::NanoappListResponseT& response) override;
84 
85     void handleLoadNanoappResponse(
86       const ::chre::fbs::LoadNanoappResponseT& response) override;
87 
88    private:
89     GenericContextHub& mParent;
90     bool mHaveConnected = false;
91   };
92 
93   sp<SocketCallbacks> mSocketCallbacks;
94 
95   // Cached hub info used for getHubs(), and synchronization primitives to make
96   // that function call synchronous if we need to query it
97   ContextHub mHubInfo;
98   bool mHubInfoValid = false;
99   std::mutex mHubInfoMutex;
100   std::condition_variable mHubInfoCond;
101 };
102 
103 extern "C" IContexthub* HIDL_FETCH_IContexthub(const char* name);
104 
105 }  // namespace implementation
106 }  // namespace V1_0
107 }  // namespace contexthub
108 }  // namespace hardware
109 }  // namespace android
110 
111 #endif  // ANDROID_HARDWARE_CONTEXTHUB_V1_0_CONTEXTHUB_H
112