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