aboutsummaryrefslogtreecommitdiff
path: root/core/include/chre/core/sensor_type_helpers.h
blob: ccbb65b77bc597101f045f9208bd332c3ab1e161 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (C) 2019 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 CHRE_CORE_SENSOR_TYPE_HELPERS_H_
#define CHRE_CORE_SENSOR_TYPE_HELPERS_H_

#include <cstring>

#include "chre/core/sensor_type.h"
#include "chre/platform/platform_sensor_type_helpers.h"

namespace chre {

/**
 * Exposes several static methods to assist in determining sensor information
 * from the sensor type.
 */
class SensorTypeHelpers : public PlatformSensorTypeHelpers {
 public:
  /**
   * @param sensorType The type of sensor to check
   * @return Whether this sensor is a one-shot sensor.
   */
  static bool isOneShot(uint8_t sensorType) {
    return getReportingMode(sensorType) == ReportingMode::OneShot;
  }

  /**
   * @param sensorType The type of sensor to check
   * @return Whether this sensor is an on-change sensor.
   */
  static bool isOnChange(uint8_t sensorType) {
    return getReportingMode(sensorType) == ReportingMode::OnChange;
  }

  /**
   * @param sensorType The type of sensor to check
   * @return Whether this sensor is a continuous sensor.
   */
  static bool isContinuous(uint8_t sensorType) {
    return getReportingMode(sensorType) == ReportingMode::Continuous;
  }

  /**
   * @param sensorType The type of sensor to check
   * @return true if this sensor is a vendor sensor.
   */
  static bool isVendorSensorType(uint8_t sensorType) {
    return sensorType >= CHRE_SENSOR_TYPE_VENDOR_START;
  }

  /**
   * @param sensorType The type of sensor to get the reporting mode for.
   * @return the reporting mode for this sensor.
   */
  static ReportingMode getReportingMode(uint8_t sensorType);

  /**
   * @param sensorType The type of sensor to check
   * @return Whether this sensor is calibrated.
   */
  static bool isCalibrated(uint8_t sensorType);

  /**
   * @param sensorType The type of sensor to get the bias event type for.
   * @param eventType A non-null pointer to the event type to populate
   * @return true if this sensor has a bias event type.
   */
  static bool getBiasEventType(uint8_t sensorType, uint16_t *eventType);

  /**
   * Determines the size needed to store the latest event from a sensor. Since
   * only on-change sensors have their latest events retained, only those
   * sensors will receive a non-zero value from this method.
   *
   * @param sensorType The sensorType of this sensor.
   * @return the memory size for an on-change sensor to store its last data
   *     event.
   */
  static size_t getLastEventSize(uint8_t sensorType);

  /**
   * @param sensorType The sensor type to obtain a string for.
   * @return A string representation of the sensor type.
   */
  static const char *getSensorTypeName(uint8_t sensorType);

  /**
   * Extracts the last sample from the suppled event and updates it to the
   * supplied last event memory as a single-sample event. No-op if not an
   * on-change sensor.
   *
   * @param sensorType The type of this sensor.
   * @param event A non-null data event of the specified sensorType, must
   *     contain one more more samples.
   * @param lastEvent The sensor's last event memory to store the last event
   *     of an on-change sensor.
   */
  static void getLastSample(uint8_t sensorType, const ChreSensorData *event,
                            ChreSensorData *lastEvent);

  /**
   * Copies the last data sample from newEvent to lastEvent memory and modifies
   * its header accordingly.
   *
   * @param newEvent Sensor data event of type SensorDataType.
   * @param lastEvent Single-sample data event of type SensorDataType.
   */
  template <typename SensorDataType>
  static void copyLastSample(const SensorDataType *newEvent,
                             SensorDataType *lastEvent);
};

template <typename SensorDataType>
void SensorTypeHelpers::copyLastSample(const SensorDataType *newEvent,
                                       SensorDataType *lastEvent) {
  // Copy header and one sample over to last event memory.
  memcpy(lastEvent, newEvent, sizeof(SensorDataType));

  // Modify last event if there are more than one samples in the supplied event.
  if (newEvent->header.readingCount > 1) {
    // Identify last sample's timestamp.
    uint64_t sampleTimestampNs = newEvent->header.baseTimestamp;
    for (size_t i = 0; i < newEvent->header.readingCount; ++i) {
      sampleTimestampNs += newEvent->readings[i].timestampDelta;
    }

    // Update last event to match the last data sample.
    lastEvent->header.baseTimestamp = sampleTimestampNs;
    lastEvent->header.readingCount = 1;
    lastEvent->readings[0] =
        newEvent->readings[newEvent->header.readingCount - 1];
    lastEvent->readings[0].timestampDelta = 0;
  }
}

}  // namespace chre

#endif  // CHRE_CORE_SENSOR_TYPE_HELPERS_H_