1 /* 2 * Copyright (C) 2023 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 #include <chre_host/host_protocol_host.h> 18 #include <chre_host/socket_client.h> 19 20 #include <chrono> 21 22 #include "proto/capo.pb.h" 23 24 using android::sp; 25 using android::chre::HostProtocolHost; 26 using android::chre::IChreMessageHandlers; 27 using android::chre::SocketClient; 28 29 // following convention of CHRE code. 30 namespace fbs = ::chre::fbs; 31 32 namespace android { 33 namespace chre { 34 35 #define NS_FROM_MS(x) ((x)*1000000) 36 37 struct CapoMDParams { 38 uint64_t still_time_threshold_ns; 39 uint32_t window_width_ns; 40 float motion_confidence_threshold; 41 float still_confidence_threshold; 42 float var_threshold; 43 float var_threshold_delta; 44 }; 45 46 class CapoDetector : public android::chre::SocketClient::ICallbacks, 47 public android::chre::IChreMessageHandlers, 48 public android::chre::SocketClient { 49 public: 50 // Typedef declaration for callback function. 51 typedef std::function<void(uint8_t)> cb_fn_t; 52 53 // Called when initializing connection with CHRE socket. 54 static android::sp<CapoDetector> start(); 55 // Common getTime function to share getCurrentTimeInMs()56 static uint32_t getCurrentTimeInMs() { 57 return std::chrono::duration_cast<std::chrono::milliseconds>( 58 std::chrono::system_clock::now().time_since_epoch()) 59 .count(); 60 } 61 // Called when the socket is successfully (re-)connected. 62 // Reset the position and try to send NanoappList request. 63 void onConnected() override; 64 // Called when we have failed to (re-)connect the socket after many attempts 65 // and are giving up. 66 void onConnectionAborted() override; 67 // Invoked when the socket is disconnected, and this connection loss 68 // was not the result of an explicit call to disconnect(). 69 // Reset the position while disconnecting. 70 void onDisconnected() override; 71 // Decode unix socket msgs to CHRE messages, and call the appropriate 72 // callback depending on the CHRE message. 73 void onMessageReceived(const void *data, size_t length) override; 74 // Listen for messages from capo nanoapp and handle the message. 75 void handleNanoappMessage(const ::chre::fbs::NanoappMessageT &message) override; 76 // Handle the response of a NanoappList request. 77 // Ensure that capo nanoapp is running. 78 void handleNanoappListResponse(const ::chre::fbs::NanoappListResponseT &response) override; 79 // Send enabling message to the nanoapp. 80 void enable(); 81 // Get last carried position type and time simultaneously. 82 void getCarriedPositionInfo(uint8_t *position, uint32_t *time); 83 // Get last carried position type. getCarriedPosition()84 uint8_t getCarriedPosition() { return last_position_type_; } 85 // Get the host endpoint. getHostEndPoint()86 uint16_t getHostEndPoint() { return kHostEndpoint; } 87 // Get the capo nanoapp ID. getNanoppAppId()88 uint64_t getNanoppAppId() { return kCapoNanoappId; } 89 // Set up callback_func_ if needed. setCallback(cb_fn_t cb)90 void setCallback(cb_fn_t cb) { callback_func_ = cb; } 91 92 private: 93 // Nanoapp ID of capo, ref: go/nanoapp-id-tracker. 94 static constexpr uint64_t kCapoNanoappId = 0x476f6f676c001020ULL; 95 // String of socket name for connecting chre. 96 static constexpr char kChreSocketName[] = "chre"; 97 // The host endpoint we use when sending message. 98 // Set with 0x9020 based on 0x8000 AND capo_app_id(1020). 99 // Ref: go/host-endpoint-id-tracker. 100 static constexpr uint16_t kHostEndpoint = 0x9020; 101 // Using for hal layer callback function. 102 cb_fn_t callback_func_ = nullptr; 103 // Last carried position received from the nano app 104 capo::PositionType last_position_type_ = capo::PositionType::UNKNOWN; 105 // Last face up event 106 uint32_t mLastFaceUpEvent = 0; 107 // Mutex for time + position tuple 108 std::mutex mCapoMutex; 109 // Motion detector parameters for host-driven capo config 110 const struct CapoMDParams mCapoDetectorMDParameters { 111 .still_time_threshold_ns = NS_FROM_MS(500), .window_width_ns = NS_FROM_MS(100), 112 .motion_confidence_threshold = 0.98f, .still_confidence_threshold = 0.99f, 113 .var_threshold = 0.0125f, .var_threshold_delta = 0.0125f, 114 }; 115 }; 116 117 } // namespace chre 118 } // namespace android 119