From 7e159a4f35fd95437c2b0d34861d40dc22d9dd3e Mon Sep 17 00:00:00 2001 From: Ben Fennema Date: Tue, 6 Mar 2018 11:11:07 -0800 Subject: Remove Activity Recognition from nanohub sensorhal The connection to the AR HAL was severed in Android Oreo. This change simply removes all unused references. Test: Build compiles. Ensure that clients that provide AR through other channels continue to work. Signed-off-by: Ben Fennema Change-Id: Id0b1eaf3e67aa3ec81d8fb81cdddd2109250a990 --- sensorhal/Android.mk | 31 --- sensorhal/activity.cpp | 437 --------------------------------------- sensorhal/activity.h | 108 ---------- sensorhal/activityeventhandler.h | 48 ----- sensorhal/hubconnection.cpp | 155 ++------------ sensorhal/hubconnection.h | 5 - sensorhal/hubdefs.h | 53 +---- 7 files changed, 30 insertions(+), 807 deletions(-) delete mode 100644 sensorhal/activity.cpp delete mode 100644 sensorhal/activity.h delete mode 100644 sensorhal/activityeventhandler.h diff --git a/sensorhal/Android.mk b/sensorhal/Android.mk index 04bc80eb..d789d167 100644 --- a/sensorhal/Android.mk +++ b/sensorhal/Android.mk @@ -102,37 +102,6 @@ include $(BUILD_SHARED_LIBRARY) include $(CLEAR_VARS) -LOCAL_MODULE := activity_recognition.$(TARGET_DEVICE) -LOCAL_MODULE_RELATIVE_PATH := hw -LOCAL_MODULE_TAGS := optional -LOCAL_MODULE_OWNER := google -LOCAL_PROPRIETARY_MODULE := true - -LOCAL_CFLAGS += $(COMMON_CFLAGS) - -LOCAL_C_INCLUDES += \ - device/google/contexthub/firmware/os/inc \ - device/google/contexthub/util/common - -LOCAL_SRC_FILES := \ - activity.cpp - -LOCAL_HEADER_LIBRARIES := \ - libhardware_headers - -LOCAL_SHARED_LIBRARIES := \ - libcutils \ - libhubconnection \ - liblog \ - libstagefright_foundation \ - libutils - -include $(BUILD_SHARED_LIBRARY) - -################################################################################ - -include $(CLEAR_VARS) - LOCAL_MODULE := libhubconnection LOCAL_MODULE_TAGS := optional LOCAL_MODULE_OWNER := google diff --git a/sensorhal/activity.cpp b/sensorhal/activity.cpp deleted file mode 100644 index 491266b1..00000000 --- a/sensorhal/activity.cpp +++ /dev/null @@ -1,437 +0,0 @@ -/* - * Copyright (C) 2015 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 -#include - -#define LOG_TAG "ActivityRecognitionHAL" -#include - -#include - -#include "activity.h" - -using namespace android; - -static const int kVersionMajor = 1; -static const int kVersionMinor = 0; - -// The maximum delta between events at which point their timestamps are to be -// considered equal. -static const int64_t kEventTimestampThresholdNanos = 100000000; // 100ms. -static const int64_t kMaxEventAgeNanos = 10000000000; // 10000ms. -static const useconds_t kFlushDelayMicros = 10000; // 10ms. - -static const char *const kActivityList[] = { - ACTIVITY_TYPE_IN_VEHICLE, - ACTIVITY_TYPE_ON_BICYCLE, - ACTIVITY_TYPE_WALKING, - ACTIVITY_TYPE_RUNNING, - ACTIVITY_TYPE_STILL, - ACTIVITY_TYPE_TILTING -}; - -static const int kActivitySensorMap[ARRAY_SIZE(kActivityList)][2] = { - { COMMS_SENSOR_ACTIVITY_IN_VEHICLE_START, - COMMS_SENSOR_ACTIVITY_IN_VEHICLE_STOP, }, - { COMMS_SENSOR_ACTIVITY_ON_BICYCLE_START, - COMMS_SENSOR_ACTIVITY_ON_BICYCLE_STOP, }, - { COMMS_SENSOR_ACTIVITY_WALKING_START, - COMMS_SENSOR_ACTIVITY_WALKING_STOP, }, - { COMMS_SENSOR_ACTIVITY_RUNNING_START, - COMMS_SENSOR_ACTIVITY_RUNNING_STOP, }, - { COMMS_SENSOR_ACTIVITY_STILL_START, - COMMS_SENSOR_ACTIVITY_STILL_STOP, }, - { COMMS_SENSOR_ACTIVITY_TILTING, - COMMS_SENSOR_ACTIVITY_TILTING, }, -}; - -// The global ActivityContext singleton. -static ActivityContext *gActivityContext = NULL; - -static int ActivityClose(struct hw_device_t *) { - ALOGI("close_activity"); - delete gActivityContext; - gActivityContext = NULL; - return 0; -} - -static void RegisterActivityCallbackWrapper( - const struct activity_recognition_device *, - const activity_recognition_callback_procs_t *callback) { - gActivityContext->registerActivityCallback(callback); -} - -static int EnableActivityEventWrapper( - const struct activity_recognition_device *, - uint32_t activity_handle, - uint32_t event_type, - int64_t max_batch_report_latency_ns) { - return gActivityContext->enableActivityEvent(activity_handle, event_type, - max_batch_report_latency_ns); -} - -static int DisableActivityEventWrapper( - const struct activity_recognition_device *, - uint32_t activity_handle, - uint32_t event_type) { - return gActivityContext->disableActivityEvent(activity_handle, event_type); -} - -static int FlushWrapper(const struct activity_recognition_device *) { - return gActivityContext->flush(); -} - -ActivityContext::ActivityContext(const struct hw_module_t *module) - : mHubConnection(HubConnection::getInstance()), - mCallback(NULL), - mNewestPublishedEventIndexIsKnown(false), - mNewestPublishedEventIndex(0), - mNewestPublishedTimestamp(0), - mOutstandingFlushEvents(0) { - memset(&device, 0, sizeof(device)); - - device.common.tag = HARDWARE_DEVICE_TAG; - device.common.version = ACTIVITY_RECOGNITION_API_VERSION_0_1; - device.common.module = const_cast(module); - device.common.close = ActivityClose; - device.register_activity_callback = RegisterActivityCallbackWrapper; - device.enable_activity_event = EnableActivityEventWrapper; - device.disable_activity_event = DisableActivityEventWrapper; - device.flush = FlushWrapper; - - if (getHubAlive()) { - mHubConnection->setActivityCallback(this); - - // Reset the system to a known good state by disabling all transitions. - for (int i = COMMS_SENSOR_ACTIVITY_FIRST; - i <= COMMS_SENSOR_ACTIVITY_LAST; i++) { - mHubConnection->queueActivate(i, false /* enable */); - } - } -} - -ActivityContext::~ActivityContext() { - mHubConnection->setActivityCallback(NULL); -} - -/* - * Obtain the activity handle for a given activity sensor index. - */ -static int GetActivityHandleFromSensorIndex(int sensorIndex) { - int normalizedSensorIndex = sensorIndex - COMMS_SENSOR_ACTIVITY_FIRST; - return normalizedSensorIndex / 2; -} - -/* - * Obtain the activity type for a given activity sensor index. - */ -static int GetActivityTypeFromSensorIndex(int sensorIndex) { - int normalizedSensorIndex = sensorIndex - COMMS_SENSOR_ACTIVITY_FIRST; - return (normalizedSensorIndex % 2) + 1; -} - -void ActivityContext::PublishUnpublishedEvents() { - if (mUnpublishedEvents.empty()) { - return; - } - - while (mUnpublishedEvents.size() > 0) { - bool eventWasPublished = false; - - for (size_t i = 0; i < mUnpublishedEvents.size(); i++) { - const ActivityEvent *event = &mUnpublishedEvents[i]; - if (event->eventIndex == (uint8_t)(mNewestPublishedEventIndex + 1)) { - PublishEvent(*event); - eventWasPublished = true; - mUnpublishedEvents.removeAt(i); - break; - } - } - - if (!eventWasPublished) { - ALOGD("Waiting on unpublished events"); - break; - } - } -} - -void ActivityContext::PublishEvent(const ActivityEvent& event) { - activity_event_t halEvent; - memset(&halEvent, 0, sizeof(halEvent)); - - int64_t timestampDelta = event.whenNs - mNewestPublishedTimestamp; - if (std::abs(timestampDelta) > kEventTimestampThresholdNanos) { - mNewestPublishedTimestamp = event.whenNs; - } - - halEvent.activity = GetActivityHandleFromSensorIndex(event.sensorIndex); - halEvent.timestamp = mNewestPublishedTimestamp; - - if (event.sensorIndex == COMMS_SENSOR_ACTIVITY_TILTING) { - ALOGD("Publishing tilt event (enter/exit)"); - - // Publish two events (enter/exit) for TILTING events. - halEvent.event_type = ACTIVITY_EVENT_ENTER; - (*mCallback->activity_callback)(mCallback, &halEvent, 1); - - halEvent.event_type = ACTIVITY_EVENT_EXIT; - } else { - ALOGD("Publishing event - activity_handle: %d, event_type: %d" - ", timestamp: %" PRIu64, - halEvent.activity, halEvent.event_type, halEvent.timestamp); - - // Just a single event is required for all other activity types. - halEvent.event_type = GetActivityTypeFromSensorIndex(event.sensorIndex); - } - - (*mCallback->activity_callback)(mCallback, &halEvent, 1); - mNewestPublishedEventIndex = event.eventIndex; - mNewestPublishedEventIndexIsKnown = true; -} - -void ActivityContext::DiscardExpiredUnpublishedEvents(uint64_t whenNs) { - // Determine the current oldest buffered event. - uint64_t oldestEventTimestamp = UINT64_MAX; - for (size_t i = 0; i < mUnpublishedEvents.size(); i++) { - const ActivityEvent *event = &mUnpublishedEvents[i]; - if (event->whenNs < oldestEventTimestamp) { - oldestEventTimestamp = event->whenNs; - } - } - - // If the age of the oldest buffered event is too large an AR sample - // has been lost. When this happens all AR transitions are set to - // ACTIVITY_EVENT_EXIT and the event ordering logic is reset. - if (oldestEventTimestamp != UINT64_MAX - && (whenNs - oldestEventTimestamp) > kMaxEventAgeNanos) { - ALOGD("Lost event detected, discarding buffered events"); - - // Publish stop events for all activity types except for TILTING. - for (uint32_t activity = 0; - activity < (ARRAY_SIZE(kActivityList) - 1); activity++) { - activity_event_t halEvent; - memset(&halEvent, 0, sizeof(halEvent)); - - halEvent.activity = activity; - halEvent.timestamp = oldestEventTimestamp; - halEvent.event_type = ACTIVITY_EVENT_EXIT; - (*mCallback->activity_callback)(mCallback, &halEvent, 1); - } - - // Reset the event reordering logic. - OnSensorHubReset(); - } -} - -void ActivityContext::OnActivityEvent(int sensorIndex, uint8_t eventIndex, - uint64_t whenNs) { - ALOGD("OnActivityEvent sensorIndex = %d, eventIndex = %" PRIu8 - ", whenNs = %" PRIu64, sensorIndex, eventIndex, whenNs); - - Mutex::Autolock autoLock(mCallbackLock); - if (!mCallback) { - return; - } - - DiscardExpiredUnpublishedEvents(whenNs); - - ActivityEvent event = { - .eventIndex = eventIndex, - .sensorIndex = sensorIndex, - .whenNs = whenNs, - }; - - if (!mNewestPublishedEventIndexIsKnown - || eventIndex == (uint8_t)(mNewestPublishedEventIndex + 1)) { - PublishEvent(event); - PublishUnpublishedEvents(); - } else { - ALOGD("OnActivityEvent out of order, pushing back"); - mUnpublishedEvents.push(event); - } -} - -void ActivityContext::OnFlush() { - // Once the number of outstanding flush events has reached zero, publish an - // event via the AR HAL. - Mutex::Autolock autoLock(mCallbackLock); - if (!mCallback) { - return; - } - - // For each flush event from the sensor hub, decrement the counter of - // outstanding flushes. - mOutstandingFlushEvents--; - if (mOutstandingFlushEvents > 0) { - ALOGV("OnFlush with %d outstanding flush events", mOutstandingFlushEvents); - return; - } else if (mOutstandingFlushEvents < 0) { - // This can happen on app start. - ALOGD("more flush events received than requested"); - mOutstandingFlushEvents = 0; - } - - activity_event_t ev = { - .event_type = ACTIVITY_EVENT_FLUSH_COMPLETE, - .activity = 0, - .timestamp = 0ll, - }; - - (*mCallback->activity_callback)(mCallback, &ev, 1); - ALOGD("OnFlush published"); -} - -void ActivityContext::OnSensorHubReset() { - // Reset the unpublished event queue and clear the last known published - // event index. - mUnpublishedEvents.clear(); - mNewestPublishedEventIndexIsKnown = false; - mOutstandingFlushEvents = 0; - mNewestPublishedTimestamp = 0; -} - -void ActivityContext::registerActivityCallback( - const activity_recognition_callback_procs_t *callback) { - ALOGI("registerActivityCallback"); - - Mutex::Autolock autoLock(mCallbackLock); - mCallback = callback; -} - -/* - * Returns a sensor index for a given activity handle and transition type. - */ -int GetActivitySensorForHandleAndType(uint32_t activity_handle, - uint32_t event_type) { - // Ensure that the requested activity index is valid. - if (activity_handle >= ARRAY_SIZE(kActivityList)) { - return 0; - } - - // Ensure that the event type is either an ENTER or EXIT. - if (event_type < ACTIVITY_EVENT_ENTER || event_type > ACTIVITY_EVENT_EXIT) { - return 0; - } - - return kActivitySensorMap[activity_handle][event_type - 1]; -} - -int ActivityContext::enableActivityEvent(uint32_t activity_handle, - uint32_t event_type, int64_t max_report_latency_ns) { - ALOGI("enableActivityEvent - activity_handle: %" PRIu32 - ", event_type: %" PRIu32 ", latency: %" PRId64, - activity_handle, event_type, max_report_latency_ns); - - int sensor_index = GetActivitySensorForHandleAndType(activity_handle, - event_type); - if (sensor_index <= 0) { - ALOGE("Enabling invalid activity_handle: %" PRIu32 - ", event_type: %" PRIu32, activity_handle, event_type); - return 1; - } - - mHubConnection->queueBatch(sensor_index, 1000000, max_report_latency_ns); - mHubConnection->queueActivate(sensor_index, true /* enable */); - return 0; -} - -int ActivityContext::disableActivityEvent(uint32_t activity_handle, - uint32_t event_type) { - ALOGI("disableActivityEvent"); - - // Obtain the sensor index for the requested activity and transition types. - int sensor_index = kActivitySensorMap[activity_handle][event_type - 1]; - if (sensor_index > 0) { - mHubConnection->queueActivate(sensor_index, false /* enable */); - } else { - ALOGE("Disabling invalid activity_handle: %" PRIu32 - ", event_type: %" PRIu32, activity_handle, event_type); - } - - return 0; -} - -int ActivityContext::flush() { - { - // Aquire a lock for the mOutstandingFlushEvents shared state. OnFlush - // modifies this value as flush results are returned. Nested scope is - // used here to control the lifecycle of the lock as OnFlush may be - // invoked before this method returns. - Mutex::Autolock autoLock(mCallbackLock); - mOutstandingFlushEvents += - (COMMS_SENSOR_ACTIVITY_LAST - COMMS_SENSOR_ACTIVITY_FIRST) + 1; - } - - // Flush all activity sensors. - for (int i = COMMS_SENSOR_ACTIVITY_FIRST; - i <= COMMS_SENSOR_ACTIVITY_LAST; i++) { - mHubConnection->queueFlush(i); - usleep(kFlushDelayMicros); - } - - return 0; -} - -bool ActivityContext::getHubAlive() { - return mHubConnection->initCheck() == OK - && mHubConnection->getAliveCheck() == OK; -} - -//////////////////////////////////////////////////////////////////////////////// - -static int open_activity( - const struct hw_module_t *module, - const char *, - struct hw_device_t **dev) { - ALOGI("open_activity"); - - gActivityContext = new ActivityContext(module); - *dev = &gActivityContext->device.common; - return 0; -} - -static struct hw_module_methods_t activity_module_methods = { - .open = open_activity -}; - -static int get_activity_list(struct activity_recognition_module *, - char const* const **activity_list) { - ALOGI("get_activity_list"); - - if (gActivityContext != NULL && gActivityContext->getHubAlive()) { - *activity_list = kActivityList; - return sizeof(kActivityList) / sizeof(kActivityList[0]); - } else { - *activity_list = {}; - return 0; - } -} - -struct activity_recognition_module HAL_MODULE_INFO_SYM = { - .common = { - .tag = HARDWARE_MODULE_TAG, - .version_major = kVersionMajor, - .version_minor = kVersionMinor, - .id = ACTIVITY_RECOGNITION_HARDWARE_MODULE_ID, - .name = "Google Activity Recognition module", - .author = "Google", - .methods = &activity_module_methods, - .dso = NULL, - .reserved = {0}, - }, - .get_supported_activities_list = get_activity_list, -}; diff --git a/sensorhal/activity.h b/sensorhal/activity.h deleted file mode 100644 index 1d5fa5e3..00000000 --- a/sensorhal/activity.h +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (C) 2015 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 ACTIVITY_H_ - -#define ACTIVITY_H_ - -#include -#include -#include -#include - -#include "activityeventhandler.h" -#include "hubconnection.h" - -namespace android { - -class ActivityContext : public ActivityEventHandler { - public: - activity_recognition_device_t device; - - explicit ActivityContext(const struct hw_module_t *module); - ~ActivityContext(); - - bool getHubAlive(); - - void registerActivityCallback( - const activity_recognition_callback_procs_t *callback); - - int enableActivityEvent(uint32_t activity_handle, - uint32_t event_type, int64_t max_report_latency_ns); - - int disableActivityEvent(uint32_t activity_handle, uint32_t event_type); - - int flush(); - - // ActivityEventHandler interface. - virtual void OnActivityEvent(int sensorIndex, uint8_t eventIndex, - uint64_t whenNs) override; - virtual void OnFlush() override; - virtual void OnSensorHubReset() override; - - private: - android::sp mHubConnection; - - android::Mutex mCallbackLock; - const activity_recognition_callback_procs_t *mCallback; - - struct ActivityEvent { - uint8_t eventIndex; - int sensorIndex; - uint64_t whenNs; - }; - - // Whether or not the newest published event index is known. When the AR HAL - // is initially started this is set to false to allow any event index from - // the sensor hub. It is also set to false when a hub reset occurs. - bool mNewestPublishedEventIndexIsKnown; - - // The index of the newest published event. The next event from the sensor - // hub must follow this event or else it will be pushed into a list of - // events to be published once the gap in events has been received. - uint8_t mNewestPublishedEventIndex; - - // The timestamp of the most recently published event. If the absolute value - // of the delta of the next timestamp to the current timestamp is below some - // threshold, this timestamp will be reused. This is used to ensure that - // activity transitions share the same timestamp and works around agressive - // AP->ContextHub time synchronization mechansims. - uint64_t mNewestPublishedTimestamp; - - // The list of unpublished events. These are published once the next - // event arrives and is greater than mNewestPublishedEventIndex by 1 - // (wrapping across 255). - Vector mUnpublishedEvents; - - // Track the number of flush events sent to the sensor hub. - int mOutstandingFlushEvents; - - // Publishes remaining unpublished events. - void PublishUnpublishedEvents(); - - // Publishes an AR event to the AR HAL client. - void PublishEvent(const ActivityEvent& event); - - // Searches for very old AR events, discards them and publishes EVENT_EXIT - // transitions for all activities. - void DiscardExpiredUnpublishedEvents(uint64_t whenNs); - - DISALLOW_EVIL_CONSTRUCTORS(ActivityContext); -}; - -} // namespace android - -#endif // ACTIVITY_H_ diff --git a/sensorhal/activityeventhandler.h b/sensorhal/activityeventhandler.h deleted file mode 100644 index 60768808..00000000 --- a/sensorhal/activityeventhandler.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) 2016 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 ACTIVITYEVENTHANDLER_H_ -#define ACTIVITYEVENTHANDLER_H_ - -namespace android { - -/* - * An interface that specifies an event handler for activity sensor events. - */ -class ActivityEventHandler { - public: - virtual ~ActivityEventHandler() {} - - // Invoked when an activity recognition event has occured. - // - // activityIndex - The index of the activity sensor. - // eventIndex - The event index (enter/exit). - // whenNs - The timestamp of when the event occured. - virtual void OnActivityEvent(int sensorIndex, uint8_t eventIndex, - uint64_t whenNs) = 0; - - // Invoked when an activity recognition flush is requested. - virtual void OnFlush() = 0; - - // Invoked when a sensor hub reset has occured. This is used to reset any - // internal state. - virtual void OnSensorHubReset() = 0; -}; - -} // namespace android - -#endif // ACTIVITYEVENTHANDLER_H_ diff --git a/sensorhal/hubconnection.cpp b/sensorhal/hubconnection.cpp index 3af5a604..d5696625 100644 --- a/sensorhal/hubconnection.cpp +++ b/sensorhal/hubconnection.cpp @@ -98,11 +98,6 @@ HubConnection *HubConnection::getInstance() return sInstance; } -static bool isActivitySensor(int sensorIndex) { - return sensorIndex >= COMMS_SENSOR_ACTIVITY_FIRST - && sensorIndex <= COMMS_SENSOR_ACTIVITY_LAST; -} - static bool isWakeEvent(int32_t sensor) { switch (sensor) { @@ -121,7 +116,6 @@ static bool isWakeEvent(int32_t sensor) HubConnection::HubConnection() : Thread(false /* canCallJava */), mRing(10 *1024), - mActivityEventHandler(NULL), mScaleAccel(1.0f), mScaleMag(1.0f), mStepCounterOffset(0ull), @@ -240,28 +234,6 @@ HubConnection::HubConnection() mSensorState[COMMS_SENSOR_WRIST_TILT].sensorType = SENS_TYPE_WRIST_TILT; mSensorState[COMMS_SENSOR_DOUBLE_TOUCH].sensorType = SENS_TYPE_DOUBLE_TOUCH; mSensorState[COMMS_SENSOR_DOUBLE_TOUCH].rate = SENSOR_RATE_ONESHOT; - mSensorState[COMMS_SENSOR_ACTIVITY_IN_VEHICLE_START].sensorType = SENS_TYPE_ACTIVITY_IN_VEHICLE_START; - mSensorState[COMMS_SENSOR_ACTIVITY_IN_VEHICLE_START].rate = SENSOR_RATE_ONCHANGE; - mSensorState[COMMS_SENSOR_ACTIVITY_IN_VEHICLE_STOP].sensorType = SENS_TYPE_ACTIVITY_IN_VEHICLE_STOP; - mSensorState[COMMS_SENSOR_ACTIVITY_IN_VEHICLE_STOP].rate = SENSOR_RATE_ONCHANGE; - mSensorState[COMMS_SENSOR_ACTIVITY_ON_BICYCLE_START].sensorType = SENS_TYPE_ACTIVITY_ON_BICYCLE_START; - mSensorState[COMMS_SENSOR_ACTIVITY_ON_BICYCLE_START].rate = SENSOR_RATE_ONCHANGE; - mSensorState[COMMS_SENSOR_ACTIVITY_ON_BICYCLE_STOP].sensorType = SENS_TYPE_ACTIVITY_ON_BICYCLE_STOP; - mSensorState[COMMS_SENSOR_ACTIVITY_ON_BICYCLE_STOP].rate = SENSOR_RATE_ONCHANGE; - mSensorState[COMMS_SENSOR_ACTIVITY_WALKING_START].sensorType = SENS_TYPE_ACTIVITY_WALKING_START; - mSensorState[COMMS_SENSOR_ACTIVITY_WALKING_START].rate = SENSOR_RATE_ONCHANGE; - mSensorState[COMMS_SENSOR_ACTIVITY_WALKING_STOP].sensorType = SENS_TYPE_ACTIVITY_WALKING_STOP; - mSensorState[COMMS_SENSOR_ACTIVITY_WALKING_STOP].rate = SENSOR_RATE_ONCHANGE; - mSensorState[COMMS_SENSOR_ACTIVITY_RUNNING_START].sensorType = SENS_TYPE_ACTIVITY_RUNNING_START; - mSensorState[COMMS_SENSOR_ACTIVITY_RUNNING_START].rate = SENSOR_RATE_ONCHANGE; - mSensorState[COMMS_SENSOR_ACTIVITY_RUNNING_STOP].sensorType = SENS_TYPE_ACTIVITY_RUNNING_STOP; - mSensorState[COMMS_SENSOR_ACTIVITY_RUNNING_STOP].rate = SENSOR_RATE_ONCHANGE; - mSensorState[COMMS_SENSOR_ACTIVITY_STILL_START].sensorType = SENS_TYPE_ACTIVITY_STILL_START; - mSensorState[COMMS_SENSOR_ACTIVITY_STILL_START].rate = SENSOR_RATE_ONCHANGE; - mSensorState[COMMS_SENSOR_ACTIVITY_STILL_STOP].sensorType = SENS_TYPE_ACTIVITY_STILL_STOP; - mSensorState[COMMS_SENSOR_ACTIVITY_STILL_STOP].rate = SENSOR_RATE_ONCHANGE; - mSensorState[COMMS_SENSOR_ACTIVITY_TILTING].sensorType = SENS_TYPE_ACTIVITY_TILTING; - mSensorState[COMMS_SENSOR_ACTIVITY_TILTING].rate = SENSOR_RATE_ONCHANGE; mSensorState[COMMS_SENSOR_GAZE].sensorType = SENS_TYPE_GAZE; mSensorState[COMMS_SENSOR_GAZE].rate = SENSOR_RATE_ONESHOT; mSensorState[COMMS_SENSOR_UNGAZE].sensorType = SENS_TYPE_UNGAZE; @@ -628,22 +600,6 @@ void HubConnection::processSample(uint64_t timestamp, uint32_t type, uint32_t se int cnt = 0; switch (sensor) { - case COMMS_SENSOR_ACTIVITY_IN_VEHICLE_START: - case COMMS_SENSOR_ACTIVITY_IN_VEHICLE_STOP: - case COMMS_SENSOR_ACTIVITY_ON_BICYCLE_START: - case COMMS_SENSOR_ACTIVITY_ON_BICYCLE_STOP: - case COMMS_SENSOR_ACTIVITY_WALKING_START: - case COMMS_SENSOR_ACTIVITY_WALKING_STOP: - case COMMS_SENSOR_ACTIVITY_RUNNING_START: - case COMMS_SENSOR_ACTIVITY_RUNNING_STOP: - case COMMS_SENSOR_ACTIVITY_STILL_START: - case COMMS_SENSOR_ACTIVITY_STILL_STOP: - case COMMS_SENSOR_ACTIVITY_TILTING: - if (mActivityEventHandler != NULL) { - mActivityEventHandler->OnActivityEvent(sensor, sample->idata & 0xff, - timestamp); - } - break; case COMMS_SENSOR_PRESSURE: initEv(&nev[cnt++], timestamp, type, sensor)->pressure = sample->fdata; break; @@ -1046,10 +1002,6 @@ void HubConnection::restoreSensorState() } mStepCounterOffset = mLastStepCount; - - if (mActivityEventHandler != NULL) { - mActivityEventHandler->OnSensorHubReset(); - } } void HubConnection::postOsLog(uint8_t *buf, ssize_t len) @@ -1278,61 +1230,6 @@ ssize_t HubConnection::processBuf(uint8_t *buf, size_t len) sensor = COMMS_SENSOR_DOUBLE_TOUCH; one = true; break; - case SENS_TYPE_TO_EVENT(SENS_TYPE_ACTIVITY_IN_VEHICLE_START): - type = 0; - sensor = COMMS_SENSOR_ACTIVITY_IN_VEHICLE_START; - one = true; - break; - case SENS_TYPE_TO_EVENT(SENS_TYPE_ACTIVITY_IN_VEHICLE_STOP): - type = 0; - sensor = COMMS_SENSOR_ACTIVITY_IN_VEHICLE_STOP; - one = true; - break; - case SENS_TYPE_TO_EVENT(SENS_TYPE_ACTIVITY_ON_BICYCLE_START): - type = 0; - sensor = COMMS_SENSOR_ACTIVITY_ON_BICYCLE_START; - one = true; - break; - case SENS_TYPE_TO_EVENT(SENS_TYPE_ACTIVITY_ON_BICYCLE_STOP): - type = 0; - sensor = COMMS_SENSOR_ACTIVITY_ON_BICYCLE_STOP; - one = true; - break; - case SENS_TYPE_TO_EVENT(SENS_TYPE_ACTIVITY_WALKING_START): - type = 0; - sensor = COMMS_SENSOR_ACTIVITY_WALKING_START; - one = true; - break; - case SENS_TYPE_TO_EVENT(SENS_TYPE_ACTIVITY_WALKING_STOP): - type = 0; - sensor = COMMS_SENSOR_ACTIVITY_WALKING_STOP; - one = true; - break; - case SENS_TYPE_TO_EVENT(SENS_TYPE_ACTIVITY_RUNNING_START): - type = 0; - sensor = COMMS_SENSOR_ACTIVITY_RUNNING_START; - one = true; - break; - case SENS_TYPE_TO_EVENT(SENS_TYPE_ACTIVITY_RUNNING_STOP): - type = 0; - sensor = COMMS_SENSOR_ACTIVITY_RUNNING_STOP; - one = true; - break; - case SENS_TYPE_TO_EVENT(SENS_TYPE_ACTIVITY_STILL_START): - type = 0; - sensor = COMMS_SENSOR_ACTIVITY_STILL_START; - one = true; - break; - case SENS_TYPE_TO_EVENT(SENS_TYPE_ACTIVITY_STILL_STOP): - type = 0; - sensor = COMMS_SENSOR_ACTIVITY_STILL_STOP; - one = true; - break; - case SENS_TYPE_TO_EVENT(SENS_TYPE_ACTIVITY_TILTING): - type = 0; - sensor = COMMS_SENSOR_ACTIVITY_TILTING; - one = true; - break; case SENS_TYPE_TO_EVENT(SENS_TYPE_GAZE): type = SENSOR_TYPE_GAZE; sensor = COMMS_SENSOR_GAZE; @@ -1410,31 +1307,27 @@ ssize_t HubConnection::processBuf(uint8_t *buf, size_t len) primary = (primary ? primary : sensor); for (i=0; ifirstSample.numFlushes; i++) { - if (isActivitySensor(sensor) && mActivityEventHandler != NULL) { - mActivityEventHandler->OnFlush(); - } else { - struct Flush& flush = mFlushesPending[primary].front(); - memset(&ev, 0x00, sizeof(sensors_event_t)); - ev.version = META_DATA_VERSION; - ev.timestamp = 0; - ev.type = SENSOR_TYPE_META_DATA; - ev.sensor = 0; - ev.meta_data.what = META_DATA_FLUSH_COMPLETE; - ev.meta_data.sensor = flush.handle; - - if (flush.internal) { - if (flush.handle == COMMS_SENSOR_ACCEL_WRIST_AWARE) - mLefty.accel = !mLefty.accel; - else if (flush.handle == COMMS_SENSOR_GYRO_WRIST_AWARE) - mLefty.gyro = !mLefty.gyro; - } else - write(&ev, 1); - - if (--flush.count == 0) - mFlushesPending[primary].pop_front(); - - ALOGV("flushing %d", ev.meta_data.sensor); - } + struct Flush& flush = mFlushesPending[primary].front(); + memset(&ev, 0x00, sizeof(sensors_event_t)); + ev.version = META_DATA_VERSION; + ev.timestamp = 0; + ev.type = SENSOR_TYPE_META_DATA; + ev.sensor = 0; + ev.meta_data.what = META_DATA_FLUSH_COMPLETE; + ev.meta_data.sensor = flush.handle; + + if (flush.internal) { + if (flush.handle == COMMS_SENSOR_ACCEL_WRIST_AWARE) + mLefty.accel = !mLefty.accel; + else if (flush.handle == COMMS_SENSOR_GYRO_WRIST_AWARE) + mLefty.gyro = !mLefty.gyro; + } else + write(&ev, 1); + + if (--flush.count == 0) + mFlushesPending[primary].pop_front(); + + ALOGV("flushing %d", ev.meta_data.sensor); } } else { ALOGW("too little data for sensor %d: len=%zu\n", sensor, len); @@ -1621,12 +1514,6 @@ bool HubConnection::threadLoop() { return false; } -void HubConnection::setActivityCallback(ActivityEventHandler *eventHandler) -{ - Mutex::Autolock autoLock(mLock); - mActivityEventHandler = eventHandler; -} - void HubConnection::initConfigCmd(struct ConfigCmd *cmd, int handle) { memset(cmd, 0x00, sizeof(*cmd)); diff --git a/sensorhal/hubconnection.h b/sensorhal/hubconnection.h index 558ece34..e8b0e1db 100644 --- a/sensorhal/hubconnection.h +++ b/sensorhal/hubconnection.h @@ -29,7 +29,6 @@ #include -#include "activityeventhandler.h" #include "directchannel.h" #include "eventnums.h" #include "halIntf.h" @@ -82,8 +81,6 @@ struct HubConnection : public Thread { ssize_t read(sensors_event_t *ev, size_t size); ssize_t write(const sensors_event_t *ev, size_t n); - void setActivityCallback(ActivityEventHandler *eventHandler); - void saveSensorSettings() const; void setRawScale(float scaleAccel, float scaleMag) { @@ -241,8 +238,6 @@ private: RingBuffer mRing; int32_t mWriteFailures; - ActivityEventHandler *mActivityEventHandler; - float mMagBias[3]; uint8_t mMagAccuracy; uint8_t mMagAccuracyRestore; diff --git a/sensorhal/hubdefs.h b/sensorhal/hubdefs.h index f63cc42a..f39bd7cc 100644 --- a/sensorhal/hubdefs.h +++ b/sensorhal/hubdefs.h @@ -19,8 +19,6 @@ #include -#define MAX_SPI_PAYLOAD_SIZE 256 - namespace android { #define CONTEXTHUB_SETTINGS_PATH "/persist/sensorcal.json" @@ -63,54 +61,21 @@ enum comms_sensor_t { COMMS_SENSOR_DOUBLE_TAP = 29, COMMS_SENSOR_WINDOW_ORIENTATION = 30, COMMS_SENSOR_WRIST_TILT = 31, - - // The order of the activity sensors must be sequential. It is used in - // various places to iterate over the list of sensors that are associated - // with an activity. - COMMS_SENSOR_ACTIVITY_IN_VEHICLE_START = 32, - COMMS_SENSOR_ACTIVITY_IN_VEHICLE_STOP = 33, - COMMS_SENSOR_ACTIVITY_ON_BICYCLE_START = 34, - COMMS_SENSOR_ACTIVITY_ON_BICYCLE_STOP = 35, - COMMS_SENSOR_ACTIVITY_WALKING_START = 36, - COMMS_SENSOR_ACTIVITY_WALKING_STOP = 37, - COMMS_SENSOR_ACTIVITY_RUNNING_START = 38, - COMMS_SENSOR_ACTIVITY_RUNNING_STOP = 39, - COMMS_SENSOR_ACTIVITY_STILL_START = 40, - COMMS_SENSOR_ACTIVITY_STILL_STOP = 41, - COMMS_SENSOR_ACTIVITY_TILTING = 42, - COMMS_SENSOR_ACCEL_BIAS = 43, - COMMS_SENSOR_DOUBLE_TOUCH = 44, - COMMS_SENSOR_GAZE = 45, - COMMS_SENSOR_UNGAZE = 46, - COMMS_SENSOR_ACCEL_UNCALIBRATED = 47, - COMMS_SENSOR_HUMIDITY = 48, - COMMS_SENSOR_ACCEL_WRIST_AWARE = 49, - COMMS_SENSOR_GYRO_WRIST_AWARE = 50, - COMMS_SENSOR_AMBIENT_TEMPERATURE = 51, + COMMS_SENSOR_ACCEL_BIAS = 32, + COMMS_SENSOR_DOUBLE_TOUCH = 33, + COMMS_SENSOR_GAZE = 34, + COMMS_SENSOR_UNGAZE = 35, + COMMS_SENSOR_ACCEL_UNCALIBRATED = 36, + COMMS_SENSOR_HUMIDITY = 37, + COMMS_SENSOR_ACCEL_WRIST_AWARE = 38, + COMMS_SENSOR_GYRO_WRIST_AWARE = 39, + COMMS_SENSOR_AMBIENT_TEMPERATURE = 40, NUM_COMMS_SENSORS_PLUS_1, COMMS_SENSOR_DEBUG = 0x99, }; -#define COMMS_SENSOR_ACTIVITY_FIRST COMMS_SENSOR_ACTIVITY_IN_VEHICLE_START -#define COMMS_SENSOR_ACTIVITY_LAST COMMS_SENSOR_ACTIVITY_TILTING - -enum { - SPI_COMMS_CMD_SYNC = 0, - SPI_COMMS_CMD_SWITCH_SENSOR = 1, - SPI_COMMS_CMD_ABSOLUTE_TIME = 2, - SPI_COMMS_SENSOR_DATA_SCALAR = 3, - SPI_COMMS_SENSOR_DATA_VEC3 = 4, - SPI_COMMS_SENSOR_DATA_VEC4 = 5, - SPI_COMMS_SENSOR_DATA_FLUSH = 6, - SPI_COMMS_CMD_UPDATE_MAG_BIAS = 7, - SPI_COMMS_CMD_UPDATE_MAG_ACCURACY = 8, - SPI_COMMS_CMD_UPDATE_GYRO_BIAS = 9, - SPI_COMMS_CMD_ACK_SUSPEND_STATE = 10, - SPI_COMMS_DEBUG_OUTPUT = 0xff, -}; - // Please keep existing values unchanged when adding or removing SENSOR_TYPE enum { SENSOR_TYPE_INTERNAL_TEMPERATURE = SENSOR_TYPE_DEVICE_PRIVATE_BASE + 0, -- cgit v1.2.3 From 12118875ef4a11f81c94d762dcbc36688ddfa9dd Mon Sep 17 00:00:00 2001 From: Matthew Bouyack Date: Mon, 21 May 2018 16:18:10 -0700 Subject: Protect accesses to mFlushesPending. Previously we weren't taking 'mLock' before accessing mFlushesPending introducing a possible race condition. Bug: 79994899 Change-Id: I9753d4ffc55c81716a80aefc2e8368f9b727484c --- sensorhal/hubconnection.cpp | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/sensorhal/hubconnection.cpp b/sensorhal/hubconnection.cpp index ef95ae68..6f387581 100644 --- a/sensorhal/hubconnection.cpp +++ b/sensorhal/hubconnection.cpp @@ -1333,25 +1333,33 @@ ssize_t HubConnection::processBuf(uint8_t *buf, size_t len) primary = (primary ? primary : sensor); for (i=0; ifirstSample.numFlushes; i++) { - struct Flush& flush = mFlushesPending[primary].front(); - memset(&ev, 0x00, sizeof(sensors_event_t)); - ev.version = META_DATA_VERSION; - ev.timestamp = 0; - ev.type = SENSOR_TYPE_META_DATA; - ev.sensor = 0; - ev.meta_data.what = META_DATA_FLUSH_COMPLETE; - ev.meta_data.sensor = flush.handle; - - if (flush.internal) { - if (flush.handle == COMMS_SENSOR_ACCEL_WRIST_AWARE) - mLefty.accel = !mLefty.accel; - else if (flush.handle == COMMS_SENSOR_GYRO_WRIST_AWARE) - mLefty.gyro = !mLefty.gyro; - } else - write(&ev, 1); + bool internal = false; + + { + Mutex::Autolock autoLock(mLock); + struct Flush& flush = mFlushesPending[primary].front(); + memset(&ev, 0x00, sizeof(sensors_event_t)); + ev.version = META_DATA_VERSION; + ev.timestamp = 0; + ev.type = SENSOR_TYPE_META_DATA; + ev.sensor = 0; + ev.meta_data.what = META_DATA_FLUSH_COMPLETE; + ev.meta_data.sensor = flush.handle; + + if (flush.internal) { + internal = true; + if (flush.handle == COMMS_SENSOR_ACCEL_WRIST_AWARE) + mLefty.accel = !mLefty.accel; + else if (flush.handle == COMMS_SENSOR_GYRO_WRIST_AWARE) + mLefty.gyro = !mLefty.gyro; + } - if (--flush.count == 0) - mFlushesPending[primary].pop_front(); + if (--flush.count == 0) + mFlushesPending[primary].pop_front(); + } + + if (!internal) + write(&ev, 1); ALOGV("flushing %d", ev.meta_data.sensor); } -- cgit v1.2.3 From 6fd11f9b38db09fe860d5743ef06c03b208f4867 Mon Sep 17 00:00:00 2001 From: mkramerm Date: Fri, 25 Jan 2019 22:59:17 +0000 Subject: [SensorData] -- Reorder struct to improve byte packing and save unnecessary padding especially for large arrays (e.g., synchronizer). Can save ~7kB for the ImuCal nanoapp. Base Memory text data bss dec 26012 3108 29432 58552 New Memory text data bss dec 25788 3004 23032 51824 Change-Id: I5f1d35cdec90c834854fa3fb3be45ede38ab4426 Head: 230936402 Test: Unit Test --- .../online_calibration/common_data/sensor_data.h | 42 ++++++++++++++-------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/firmware/os/algos/calibration/online_calibration/common_data/sensor_data.h b/firmware/os/algos/calibration/online_calibration/common_data/sensor_data.h index 1331153e..f4c16133 100644 --- a/firmware/os/algos/calibration/online_calibration/common_data/sensor_data.h +++ b/firmware/os/algos/calibration/online_calibration/common_data/sensor_data.h @@ -22,10 +22,11 @@ #ifndef LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_ONLINE_CALIBRATION_COMMON_DATA_SENSOR_DATA_H_ #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_ONLINE_CALIBRATION_COMMON_DATA_SENSOR_DATA_H_ -#include -#include #include +#include +#include + #include "common/math/macros.h" namespace online_calibration { @@ -48,6 +49,14 @@ enum class SensorType : int8_t { kWifiM = 6 // 3-axis sensor (units = meter). }; +// Helper function for determining if a sensor type is 3-axis, otherwise it's +// single-axis. +inline bool is_three_axis(SensorType type) { + return type == SensorType::kAccelerometerMps2 || + type == SensorType::kGyroscopeRps || + type == SensorType::kMagnetometerUt; +} + /* * SensorData is a generalized data structure used to represent sensor samples * produced by either a single- or three-axis device. Usage is implied through @@ -59,7 +68,10 @@ enum class SensorType : int8_t { * * NOTE: The unified dimensional representation makes it convenient to pass * either type of data into the interface functions defined in the - * OnlineCalibration. + * OnlineCalibration. Additionally, this approach, although admittedly wasteful + * for single-axis sensors, allows one to avoid dynamic memory allocation for + * hardware systems where having a fixed memory footprint is either required or + * extremely desireable. */ // Axis index definitions for SensorData::data. @@ -71,28 +83,30 @@ enum SensorIndex : int8_t { }; struct SensorData { - // Indicates the type of sensor this data originated from. - SensorType type; - // Sensor sample timestamp. uint64_t timestamp_nanos; // Generalized sensor sample (represents either single- or three-axis data). float data[3]; - SensorData() : type(SensorType::kUndefined), timestamp_nanos(0) { + // Indicates the type of sensor this data originated from. + SensorType type; + + SensorData() : timestamp_nanos(0), type(SensorType::kUndefined) { memset(data, 0, sizeof(data)); } - SensorData(SensorType type, uint64_t ts, float x, float y, float z) - : type(type), timestamp_nanos(ts) { - data[SensorIndex::kXAxis] = x; - data[SensorIndex::kYAxis] = y; - data[SensorIndex::kZAxis] = z; + SensorData(SensorType type, uint64_t timestamp_nanos, float x_axis, + float y_axis, float z_axis) + : timestamp_nanos(timestamp_nanos), type(type) { + data[SensorIndex::kXAxis] = x_axis; + data[SensorIndex::kYAxis] = y_axis; + data[SensorIndex::kZAxis] = z_axis; } - SensorData(SensorType type, uint64_t ts, float single_axis_sample) - : type(type), timestamp_nanos(ts) { + SensorData(SensorType type, uint64_t timestamp_nanos, + float single_axis_sample) + : timestamp_nanos(timestamp_nanos), type(type) { data[SensorIndex::kSingleAxis] = single_axis_sample; } }; -- cgit v1.2.3