1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "chre_api/chre/sensor.h"
18
19 #include <cstdint>
20
21 #include "chre/core/event_loop_manager.h"
22 #include "chre/core/settings.h"
23 #include "chre/platform/linux/pal_sensor.h"
24 #include "chre/platform/log.h"
25 #include "chre/util/system/napp_permissions.h"
26 #include "chre_api/chre/event.h"
27
28 #include "gtest/gtest.h"
29 #include "inc/test_util.h"
30 #include "test_base.h"
31 #include "test_event.h"
32 #include "test_event_queue.h"
33 #include "test_util.h"
34
35 namespace chre {
36 namespace {
37
TEST_F(TestBase,SensorCanSubscribeAndUnsubscribeToDataEvents)38 TEST_F(TestBase, SensorCanSubscribeAndUnsubscribeToDataEvents) {
39 CREATE_CHRE_TEST_EVENT(CONFIGURE, 0);
40
41 struct Configuration {
42 uint32_t sensorHandle;
43 uint64_t interval;
44 enum chreSensorConfigureMode mode;
45 };
46
47 struct App : public TestNanoapp {
48 void (*handleEvent)(uint32_t, uint16_t, const void *) =
49 [](uint32_t, uint16_t eventType, const void *eventData) {
50 switch (eventType) {
51 case CHRE_EVENT_SENSOR_SAMPLING_CHANGE: {
52 auto *event =
53 static_cast<const struct chreSensorSamplingStatusEvent *>(
54 eventData);
55 TestEventQueueSingleton::get()->pushEvent(
56 CHRE_EVENT_SENSOR_SAMPLING_CHANGE, *event);
57 break;
58 }
59
60 case CHRE_EVENT_TEST_EVENT: {
61 auto event = static_cast<const TestEvent *>(eventData);
62 switch (event->type) {
63 case CONFIGURE: {
64 auto config = static_cast<const Configuration *>(event->data);
65 const bool success = chreSensorConfigure(
66 config->sensorHandle, config->mode, config->interval, 0);
67 TestEventQueueSingleton::get()->pushEvent(CONFIGURE, success);
68 break;
69 }
70 }
71 }
72 }
73 };
74 };
75
76 auto app = loadNanoapp<App>();
77 bool success;
78
79 EXPECT_FALSE(chrePalSensorIsSensor0Enabled());
80
81 Configuration config{.sensorHandle = 0,
82 .interval = 100,
83 .mode = CHRE_SENSOR_CONFIGURE_MODE_CONTINUOUS};
84 sendEventToNanoapp(app, CONFIGURE, config);
85 waitForEvent(CONFIGURE, &success);
86 EXPECT_TRUE(success);
87 struct chreSensorSamplingStatusEvent event;
88 waitForEvent(CHRE_EVENT_SENSOR_SAMPLING_CHANGE, &event);
89 EXPECT_EQ(event.sensorHandle, config.sensorHandle);
90 EXPECT_EQ(event.status.interval, config.interval);
91 EXPECT_TRUE(event.status.enabled);
92 EXPECT_TRUE(chrePalSensorIsSensor0Enabled());
93
94 config = {.sensorHandle = 0,
95 .interval = 50,
96 .mode = CHRE_SENSOR_CONFIGURE_MODE_DONE};
97 sendEventToNanoapp(app, CONFIGURE, config);
98 waitForEvent(CONFIGURE, &success);
99 EXPECT_TRUE(success);
100 EXPECT_FALSE(chrePalSensorIsSensor0Enabled());
101 }
102
TEST_F(TestBase,SensorUnsubscribeToDataEventsOnUnload)103 TEST_F(TestBase, SensorUnsubscribeToDataEventsOnUnload) {
104 CREATE_CHRE_TEST_EVENT(CONFIGURE, 0);
105
106 struct Configuration {
107 uint32_t sensorHandle;
108 uint64_t interval;
109 enum chreSensorConfigureMode mode;
110 };
111
112 struct App : public TestNanoapp {
113 void (*handleEvent)(uint32_t, uint16_t, const void *) =
114 [](uint32_t, uint16_t eventType, const void *eventData) {
115 switch (eventType) {
116 case CHRE_EVENT_SENSOR_SAMPLING_CHANGE: {
117 auto *event =
118 static_cast<const struct chreSensorSamplingStatusEvent *>(
119 eventData);
120 TestEventQueueSingleton::get()->pushEvent(
121 CHRE_EVENT_SENSOR_SAMPLING_CHANGE, *event);
122 break;
123 }
124
125 case CHRE_EVENT_TEST_EVENT: {
126 auto event = static_cast<const TestEvent *>(eventData);
127 switch (event->type) {
128 case CONFIGURE: {
129 auto config = static_cast<const Configuration *>(event->data);
130 const bool success = chreSensorConfigure(
131 config->sensorHandle, config->mode, config->interval, 0);
132 TestEventQueueSingleton::get()->pushEvent(CONFIGURE, success);
133 break;
134 }
135 }
136 }
137 }
138 };
139 };
140
141 auto app = loadNanoapp<App>();
142 EXPECT_FALSE(chrePalSensorIsSensor0Enabled());
143
144 Configuration config{.sensorHandle = 0,
145 .interval = 100,
146 .mode = CHRE_SENSOR_CONFIGURE_MODE_CONTINUOUS};
147 sendEventToNanoapp(app, CONFIGURE, config);
148 bool success;
149 waitForEvent(CONFIGURE, &success);
150 EXPECT_TRUE(success);
151 struct chreSensorSamplingStatusEvent event;
152 waitForEvent(CHRE_EVENT_SENSOR_SAMPLING_CHANGE, &event);
153 EXPECT_EQ(event.sensorHandle, config.sensorHandle);
154 EXPECT_EQ(event.status.interval, config.interval);
155 EXPECT_TRUE(event.status.enabled);
156 EXPECT_TRUE(chrePalSensorIsSensor0Enabled());
157
158 unloadNanoapp(app);
159 EXPECT_FALSE(chrePalSensorIsSensor0Enabled());
160 }
161
162 } // namespace
163 } // namespace chre