diff options
author | Rick Chen <rickctchen@google.com> | 2018-10-26 11:24:04 +0800 |
---|---|---|
committer | Rick Chen <rickctchen@google.com> | 2018-10-31 02:19:10 +0000 |
commit | 100ec9e99e04c3a95dc389194e088ef1495d481f (patch) | |
tree | 9bf97a3300eaf67fe0a736982716cdbff7def8bc /pixelstats | |
parent | ad78cedd942cdc893fedc2f7b05726f522d1a157 (diff) | |
download | bonito-100ec9e99e04c3a95dc389194e088ef1495d481f.tar.gz |
bonito: pixelstats: add DropDetect
Porting DropDetect part from below commits in crosshatch
74385bf6 crosshatch: pixelstats: hold a ref to DropDetect
80c6eea0 pixelstats: add vendor pixelstats
Bug: 117744507
Test: Enable drop nanoapp, drop device and see below logs:
12:43:49.383 769 787 I pixelstats-vendor: Drop Detect enabled
12:54:22.897 762 1074 I CHRE : @ 654.248: [dd] sendDdEvent, c=0.114009, p=55.700496, d=120000000
12:54:24.408 762 1074 I CHRE : @ 655.776: [dd] sendDdEvent, c=1.000000, p=123.142555, d=200000000
12:54:26.172 762 1074 I CHRE : @ 657.526: [dd] sendDdEvent, c=0.508084, p=75.404235, d=220000000
12:54:22.896 738 738 I sysui_multi_action: [757,1430,758,4,1431,11,1432,55700,1304,120]
12:54:24.405 738 738 I sysui_multi_action: [757,1430,758,4,1431,100,1432,123142,1304,200]
12:54:26.169 738 738 I sysui_multi_action: [757,1430,758,4,1431,50,1432,75404,1304,220]
Change-Id: Ic9721898350fa1d7f58a3ce36d247e9f3965626a
Signed-off-by: Rick Chen <rickctchen@google.com>
Diffstat (limited to 'pixelstats')
-rw-r--r-- | pixelstats/Android.bp | 3 | ||||
-rw-r--r-- | pixelstats/DropDetect.cpp | 148 | ||||
-rw-r--r-- | pixelstats/DropDetect.h | 45 | ||||
-rw-r--r-- | pixelstats/service.cpp | 9 |
4 files changed, 205 insertions, 0 deletions
diff --git a/pixelstats/Android.bp b/pixelstats/Android.bp index b70b5d64..ea283164 100644 --- a/pixelstats/Android.bp +++ b/pixelstats/Android.bp @@ -18,6 +18,7 @@ cc_binary { init_rc: ["pixelstats-vendor.bonito.rc"], srcs: [ "service.cpp", + "DropDetect.cpp", "SysfsCollector.cpp", "UeventListener.cpp", ], @@ -32,5 +33,7 @@ cc_binary { "libutils", ], proprietary: true, + static_libs: ["chre_client"], + header_libs: ["chre_api"], } diff --git a/pixelstats/DropDetect.cpp b/pixelstats/DropDetect.cpp new file mode 100644 index 00000000..b038eb3f --- /dev/null +++ b/pixelstats/DropDetect.cpp @@ -0,0 +1,148 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "DropDetect.h" + +#include "chre/util/nanoapp/app_id.h" +#include "chre_host/host_protocol_host.h" +#include "chre_host/socket_client.h" + +#include <hardware/google/pixelstats/1.0/IPixelStats.h> +#define LOG_TAG "pixelstats-vendor" +#include <log/log.h> + +#include <inttypes.h> + +using namespace android::chre; +using android::sp; +using android::chre::IChreMessageHandlers; + +// following convention of CHRE code. +namespace fbs = ::chre::fbs; + +namespace device { +namespace google { +namespace bonito { + +namespace { // anonymous namespace for file-local definitions +constexpr int64_t kDropDetectAppId = 0x476f6f676c001010ULL; +constexpr char kChreSocketName[] = "chre"; + +// This struct is defined in nanoapps/drop/messaging.h +// by the DropDetect nanoapp. +struct __attribute__((__packed__)) DropEventPayload { + float confidence; + float accel_magnitude_peak; + int32_t free_fall_duration_ns; +}; + +// This enum is defined in nanoapps/drop/messaging.h +// by the DropDetect nanoapp. +enum DropConstants { + kDropEnableRequest = 1, + kDropEnableNotification = 2, + kDropDisableRequest = 3, + kDropDisableNotification = 4, + kDropEventDetection = 5, +}; + +void requestNanoappList(SocketClient &client) { + flatbuffers::FlatBufferBuilder builder(64); + HostProtocolHost::encodeNanoappListRequest(builder); + + if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) { + ALOGE("Failed to send NanoappList request"); + } +} + +} // namespace + +void DropDetect::onConnected() { + requestNanoappList(*this); +} + +/** + * Decode unix socket msgs to CHRE messages, and call the appropriate + * callback depending on the CHRE message. + */ +void DropDetect::onMessageReceived(const void *data, size_t length) { + if (!HostProtocolHost::decodeMessageFromChre(data, length, *this)) { + ALOGE("Failed to decode message"); + } +} + +/** + * Handle the response of a NanoappList request. + * Ensure that the Drop Detect nanoapp is running. + */ +void DropDetect::handleNanoappListResponse(const fbs::NanoappListResponseT &response) { + for (const std::unique_ptr<fbs::NanoappListEntryT> &nanoapp : response.nanoapps) { + if (nanoapp->app_id == kDropDetectAppId) { + if (!nanoapp->enabled) + ALOGE("Drop Detect app not enabled"); + else + ALOGI("Drop Detect enabled"); + return; + } + } + ALOGE("Drop Detect app not found"); +} + +/** + * listen for messages from the DropDetect nanoapp and report them to + * PixelStats. + */ +void DropDetect::handleNanoappMessage(const fbs::NanoappMessageT &message) { + struct DropEventPayload *message_struct; + + if (message.app_id != kDropDetectAppId || message.message_type != kDropEventDetection || + message.message.size() < sizeof(struct DropEventPayload)) + return; + message_struct = (struct DropEventPayload *)&message.message[0]; + /* + * ALOGI("Received drop detect message! Confidence %f Peak %f Duration %g", + * message_struct->confidence, message_struct->accel_magnitude_peak, + * message_struct->free_fall_duration_ns / 1e9); + */ + int32_t confidence = message_struct->confidence * 100; + confidence = std::min(confidence, 100); + confidence = std::max(0, confidence); + + int32_t accel_magnitude_peak_1000ths_g = message_struct->accel_magnitude_peak * 1000.0; + int32_t free_fall_duration_ms = message_struct->free_fall_duration_ns / 1000000; + + using ::hardware::google::pixelstats::V1_0::IPixelStats; + sp<IPixelStats> client = IPixelStats::tryGetService(); + if (!client) { + ALOGE("Unable to connect to PixelStats service"); + return; + } + client->reportPhysicalDropDetected(confidence, accel_magnitude_peak_1000ths_g, + free_fall_duration_ms); +} + +sp<DropDetect> DropDetect::start() { + sp<DropDetect> dropDetect = new DropDetect(); + + if (!dropDetect->connectInBackground(kChreSocketName, dropDetect)) { + ALOGE("Couldn't connect to CHRE socket"); + return nullptr; + } + return dropDetect; +} + +} // namespace bonito +} // namespace google +} // namespace device diff --git a/pixelstats/DropDetect.h b/pixelstats/DropDetect.h new file mode 100644 index 00000000..1dd239c6 --- /dev/null +++ b/pixelstats/DropDetect.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DEVICE_GOOGLE_BONITO_PIXELSTATS_DROPDETECT_H +#define DEVICE_GOOGLE_BONITO_PIXELSTATS_DROPDETECT_H + +#include <utils/StrongPointer.h> +#include "chre_host/host_protocol_host.h" +#include "chre_host/socket_client.h" + +namespace device { +namespace google { +namespace bonito { + +class DropDetect : public android::chre::SocketClient::ICallbacks, + public android::chre::IChreMessageHandlers, + public android::chre::SocketClient { + public: + static android::sp<DropDetect> start(); + + void onConnected() override; + void onMessageReceived(const void *data, size_t length) override; + + void handleNanoappMessage(const ::chre::fbs::NanoappMessageT &message) override; + void handleNanoappListResponse(const ::chre::fbs::NanoappListResponseT &response) override; +}; + +} // namespace bonito +} // namespace google +} // namespace device + +#endif // DEVICE_GOOGLE_BONITO_PIXELSTATS_DROPDETECT_H diff --git a/pixelstats/service.cpp b/pixelstats/service.cpp index 0b50e8f2..d8fa4fdf 100644 --- a/pixelstats/service.cpp +++ b/pixelstats/service.cpp @@ -19,15 +19,24 @@ #include <android-base/logging.h> #include <utils/StrongPointer.h> +#include "DropDetect.h" #include "SysfsCollector.h" #include "UeventListener.h" +using android::sp; +using device::google::bonito::DropDetect; using device::google::bonito::SysfsCollector; using device::google::bonito::UeventListener; int main() { LOG(INFO) << "starting PixelStats"; + sp<DropDetect> dropDetector = DropDetect::start(); + if (!dropDetector) { + LOG(ERROR) << "Unable to launch drop detection"; + return 1; + } + UeventListener::ListenForeverInNewThread(); SysfsCollector collector; |