summaryrefslogtreecommitdiffstats
path: root/sensorhal/activity.h
blob: 1d5fa5e3c3eb515110a48f023c5ed4078de24316 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * 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 <hardware/activity_recognition.h>
#include <media/stagefright/foundation/ABase.h>
#include <utils/KeyedVector.h>
#include <utils/Vector.h>

#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<android::HubConnection> 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<ActivityEvent> 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_