1 /* 2 * Copyright (C) 2021 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_AIDL_CONTEXTHUB_H 18 #define ANDROID_HARDWARE_CONTEXTHUB_AIDL_CONTEXTHUB_H 19 20 #include <aidl/android/hardware/contexthub/BnContextHub.h> 21 #include <android-base/file.h> 22 #include <log/log.h> 23 #include <map> 24 #include <mutex> 25 #include <optional> 26 #include <unordered_set> 27 28 #include "event_logger.h" 29 #include "hal_chre_socket_connection.h" 30 31 namespace aidl { 32 namespace android { 33 namespace hardware { 34 namespace contexthub { 35 36 class ContextHub : public BnContextHub, 37 public ::android::hardware::contexthub::common:: 38 implementation::IChreSocketCallback { 39 public: ContextHub()40 ContextHub() 41 : mDeathRecipient( 42 AIBinder_DeathRecipient_new(ContextHub::onServiceDied)) {} 43 44 ::ndk::ScopedAStatus getContextHubs( 45 std::vector<ContextHubInfo> *out_contextHubInfos) override; 46 ::ndk::ScopedAStatus loadNanoapp(int32_t contextHubId, 47 const NanoappBinary &appBinary, 48 int32_t transactionId) override; 49 ::ndk::ScopedAStatus unloadNanoapp(int32_t contextHubId, int64_t appId, 50 int32_t transactionId) override; 51 ::ndk::ScopedAStatus disableNanoapp(int32_t contextHubId, int64_t appId, 52 int32_t transactionId) override; 53 ::ndk::ScopedAStatus enableNanoapp(int32_t contextHubId, int64_t appId, 54 int32_t transactionId) override; 55 ::ndk::ScopedAStatus onSettingChanged(Setting setting, bool enabled) override; 56 ::ndk::ScopedAStatus queryNanoapps(int32_t contextHubId) override; 57 ::ndk::ScopedAStatus registerCallback( 58 int32_t contextHubId, 59 const std::shared_ptr<IContextHubCallback> &cb) override; 60 ::ndk::ScopedAStatus sendMessageToHub( 61 int32_t contextHubId, const ContextHubMessage &message) override; 62 ::ndk::ScopedAStatus onHostEndpointConnected( 63 const HostEndpointInfo &in_info) override; 64 ::ndk::ScopedAStatus onHostEndpointDisconnected( 65 char16_t in_hostEndpointId) override; 66 67 void onNanoappMessage(const ::chre::fbs::NanoappMessageT &message) override; 68 69 void onNanoappListResponse( 70 const ::chre::fbs::NanoappListResponseT &response) override; 71 72 void onTransactionResult(uint32_t transactionId, bool success) override; 73 74 void onContextHubRestarted() override; 75 76 void onDebugDumpData(const ::chre::fbs::DebugDumpDataT &data) override; 77 78 void onDebugDumpComplete( 79 const ::chre::fbs::DebugDumpResponseT &response) override; 80 81 void handleServiceDeath(); 82 static void onServiceDied(void *cookie); 83 84 binder_status_t dump(int fd, const char **args, uint32_t numArgs) override; 85 86 private: 87 ::android::hardware::contexthub::common::implementation:: 88 HalChreSocketConnection mConnection{this}; 89 90 // A mutex to protect concurrent modifications to the callback pointer and 91 // access (invocations). 92 std::mutex mCallbackMutex; 93 std::shared_ptr<IContextHubCallback> mCallback; 94 95 ndk::ScopedAIBinder_DeathRecipient mDeathRecipient; 96 97 std::map<Setting, bool> mSettingEnabled; 98 std::optional<bool> mIsWifiAvailable; 99 std::optional<bool> mIsBleAvailable; 100 101 std::mutex mConnectedHostEndpointsMutex; 102 std::unordered_set<char16_t> mConnectedHostEndpoints; 103 104 // Variables related to debug dump. 105 static constexpr int kInvalidFd = -1; 106 int mDebugFd = kInvalidFd; 107 bool mDebugDumpPending = false; 108 std::mutex mDebugDumpMutex; 109 std::condition_variable mDebugDumpCond; 110 111 // Logs events to be reported in debug dumps. 112 EventLogger mEventLogger; 113 isSettingEnabled(Setting setting)114 bool isSettingEnabled(Setting setting) { 115 return mSettingEnabled.count(setting) > 0 ? mSettingEnabled[setting] 116 : false; 117 } 118 toFbsSettingState(bool enabled)119 chre::fbs::SettingState toFbsSettingState(bool enabled) const { 120 return enabled ? chre::fbs::SettingState::ENABLED 121 : chre::fbs::SettingState::DISABLED; 122 } 123 124 // Write a string to mDebugFd writeToDebugFile(const std::string & str)125 void writeToDebugFile(const std::string &str) { 126 if (!::android::base::WriteStringToFd(str, mDebugFd)) { 127 ALOGW("Failed to write %zu bytes to debug dump fd", str.size()); 128 } 129 } 130 writeToDebugFile(const char * str)131 void writeToDebugFile(const char *str) { 132 writeToDebugFile(str, strlen(str)); 133 } 134 writeToDebugFile(const char * str,size_t len)135 void writeToDebugFile(const char *str, size_t len) { 136 std::string s(str, len); 137 writeToDebugFile(s); 138 } 139 }; 140 141 } // namespace contexthub 142 } // namespace hardware 143 } // namespace android 144 } // namespace aidl 145 146 #endif // ANDROID_HARDWARE_CONTEXTHUB_AIDL_CONTEXTHUB_H 147