CapoDetector.h 4.2 KB

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