diff options
author | Max Shi <meixuanshi@google.com> | 2020-03-05 11:23:16 -0800 |
---|---|---|
committer | Max Shi <meixuanshi@google.com> | 2020-03-25 10:16:19 -0700 |
commit | dc569338d41334e908aa136d7b417e049d694c60 (patch) | |
tree | 948bbb38187ddadbee6d7536e69bf1bc5e529c86 | |
parent | 3189ee9aab687cebf898c81b34b07a24a5e9373a (diff) | |
download | pixel-dc569338d41334e908aa136d7b417e049d694c60.tar.gz |
Update drop detect data interface.
Bug: 141603839
Test: Verified pixelstats receives and correctly decodes the new drop
Test: event as well as the old drop event.
Change-Id: I319828984db30cc2c4034f6cc5de5aa00259ac62
-rw-r--r-- | pixelstats/DropDetect.cpp | 95 |
1 files changed, 73 insertions, 22 deletions
diff --git a/pixelstats/DropDetect.cpp b/pixelstats/DropDetect.cpp index 5d51b760..1b1bb1cc 100644 --- a/pixelstats/DropDetect.cpp +++ b/pixelstats/DropDetect.cpp @@ -24,6 +24,7 @@ #include <log/log.h> #include <inttypes.h> +#include <math.h> using android::sp; using android::chre::HostProtocolHost; @@ -42,7 +43,7 @@ namespace pixel { namespace { // anonymous namespace for file-local definitions -// This struct is defined in nanoapps/drop/messaging.h +// The following two structs are defined in nanoapps/drop/messaging.h // by the DropDetect nanoapp. struct __attribute__((__packed__)) DropEventPayload { float confidence; @@ -50,6 +51,13 @@ struct __attribute__((__packed__)) DropEventPayload { int32_t free_fall_duration_ns; }; +struct __attribute__((__packed__)) DropEventPayloadV2 { + uint64_t free_fall_duration_ns; + float impact_accel_x; + float impact_accel_y; + float impact_accel_z; +}; + // This enum is defined in nanoapps/drop/messaging.h // by the DropDetect nanoapp. enum DropConstants { @@ -58,6 +66,7 @@ enum DropConstants { kDropDisableRequest = 3, kDropDisableNotification = 4, kDropEventDetection = 5, + kDropEventDetectionV2 = 6, }; void requestNanoappList(SocketClient &client) { @@ -113,36 +122,78 @@ void DropDetect::handleNanoappListResponse(const fbs::NanoappListResponseT &resp 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]; +static PhysicalDropDetected dropEventFromNanoappPayload(const struct DropEventPayload *p) { 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); - uint8_t confidence = message_struct->confidence * 100; + p->confidence, p->accel_magnitude_peak, p->free_fall_duration_ns / 1e9); + + uint8_t confidence = p->confidence * 100; confidence = std::min<int>(confidence, 100); confidence = std::max<int>(0, confidence); + int32_t accel_magnitude_peak_1000ths_g = p->accel_magnitude_peak * 1000.0; + int32_t free_fall_duration_ms = p->free_fall_duration_ns / 1000000; - 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; + return PhysicalDropDetected{ confidence, + accel_magnitude_peak_1000ths_g, + free_fall_duration_ms }; +} + +static PhysicalDropDetected dropEventFromNanoappPayload(const struct DropEventPayloadV2 *p) { + ALOGI("Received drop detect message: " + "duration %g ms, impact acceleration: x = %f, y = %f, z = %f", + p->free_fall_duration_ns / 1e6, + p->impact_accel_x, + p->impact_accel_y, + p->impact_accel_z); + + float impact_magnitude = sqrt(p->impact_accel_x * p->impact_accel_x + + p->impact_accel_y * p->impact_accel_y + + p->impact_accel_z * p->impact_accel_z); + /* Scale impact magnitude as percentage between [50, 100] m/s2. */ + constexpr float min_confidence_magnitude = 50; + constexpr float max_confidence_magnitude = 100; + uint8_t confidence_percentage = + impact_magnitude < min_confidence_magnitude ? 0 : + impact_magnitude > max_confidence_magnitude ? 100 : + (impact_magnitude - min_confidence_magnitude) / + (max_confidence_magnitude - min_confidence_magnitude) * 100; + + int32_t free_fall_duration_ms = static_cast<int32_t>(p->free_fall_duration_ns / 1000000); + + return PhysicalDropDetected{ + .confidencePctg = confidence_percentage, + .accelPeak = static_cast<int32_t>(impact_magnitude * 1000), + .freefallDuration = free_fall_duration_ms, + }; +} +static void reportDropEventToStatsd(const PhysicalDropDetected& drop) { sp<IStats> stats_client = IStats::tryGetService(); - if (stats_client) { - PhysicalDropDetected drop = {confidence, accel_magnitude_peak_1000ths_g, - free_fall_duration_ms}; + if (!stats_client) { + ALOGE("Unable to connect to Stats service"); + } else { Return<void> ret = stats_client->reportPhysicalDropDetected(drop); if (!ret.isOk()) ALOGE("Unable to report physical drop to Stats service"); - } else - ALOGE("Unable to connect to Stats service"); + } +} + +/** + * listen for messages from the DropDetect nanoapp and report them to + * PixelStats. + */ +void DropDetect::handleNanoappMessage(const fbs::NanoappMessageT &message) { + if (message.app_id != kDropDetectAppId) + return; + + if (message.message_type == kDropEventDetection && + message.message.size() >= sizeof(struct DropEventPayload)) { + reportDropEventToStatsd(dropEventFromNanoappPayload( + reinterpret_cast<const struct DropEventPayload *>(&message.message[0]))); + } else if (message.message_type == kDropEventDetectionV2 && + message.message.size() >= sizeof(struct DropEventPayloadV2)) { + reportDropEventToStatsd(dropEventFromNanoappPayload( + reinterpret_cast<const struct DropEventPayloadV2 *>(&message.message[0]))); + } } } // namespace pixel |