• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 <inttypes.h>
18 #include <android/hardware_buffer.h>
19 #include <android/sensor.h>
20 #include <sensor/Sensor.h>
21 #include <sensor/SensorManager.h>
22 #include <sensor/SensorEventQueue.h>
23 #include <utils/Looper.h>
24 #include <vndk/hardware_buffer.h>
25 
26 using namespace android;
27 
28 static nsecs_t sStartTime = 0;
29 
30 
receiver(__unused int fd,__unused int events,void * data)31 int receiver(__unused int fd, __unused int events, void* data) {
32     sp<SensorEventQueue> q((SensorEventQueue*)data);
33     ssize_t n;
34     ASensorEvent buffer[8];
35 
36     static nsecs_t oldTimeStamp = 0;
37 
38     while ((n = q->read(buffer, 8)) > 0) {
39         for (int i=0 ; i<n ; i++) {
40             float t;
41             if (oldTimeStamp) {
42                 t = float(buffer[i].timestamp - oldTimeStamp) / s2ns(1);
43             } else {
44                 t = float(buffer[i].timestamp - sStartTime) / s2ns(1);
45             }
46             oldTimeStamp = buffer[i].timestamp;
47 
48             if (buffer[i].type == Sensor::TYPE_ACCELEROMETER) {
49                 printf("%" PRId64 "\t%8f\t%8f\t%8f\t%f\n",
50                         buffer[i].timestamp,
51                         buffer[i].data[0], buffer[i].data[1], buffer[i].data[2],
52                         1.0/t);
53             }
54 
55         }
56     }
57     if (n<0 && n != -EAGAIN) {
58         printf("error reading events (%s)\n", strerror(-n));
59     }
60     return 1;
61 }
62 
testInvalidSharedMem_NoCrash(SensorManager & mgr)63 void testInvalidSharedMem_NoCrash(SensorManager &mgr) {
64     AHardwareBuffer *hardwareBuffer;
65     char* buffer;
66 
67     constexpr size_t kEventSize = sizeof(ASensorEvent);
68     constexpr size_t kNEvent = 4096; // enough to contain 1.5 * 800 * 2.2 events
69     constexpr size_t kMemSize = kEventSize * kNEvent;
70     AHardwareBuffer_Desc desc = {
71             .width = static_cast<uint32_t>(kMemSize),
72             .height = 1,
73             .layers = 1,
74             .format = AHARDWAREBUFFER_FORMAT_BLOB,
75             .usage = AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA
76                         | AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
77     };
78 
79     AHardwareBuffer_allocate(&desc, &hardwareBuffer);
80     AHardwareBuffer_lock(hardwareBuffer, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY,
81                          -1, nullptr, reinterpret_cast<void **>(&buffer));
82 
83     const native_handle_t *resourceHandle = AHardwareBuffer_getNativeHandle(hardwareBuffer);
84 
85     // Pass in AHardwareBuffer, but with the wrong DIRECT_CHANNEL_TYPE to see
86     // if anything in the Sensor framework crashes
87     int ret = mgr.createDirectChannel(
88             kMemSize, ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY, resourceHandle);
89 
90     // Should print -22 (BAD_VALUE) and the device runtime shouldn't restart
91     printf("createInvalidDirectChannel=%d\n", ret);
92 }
93 
main()94 int main() {
95     SensorManager& mgr = SensorManager::getInstanceForPackage(String16("Sensor Service Test"));
96 
97     testInvalidSharedMem_NoCrash(mgr);
98 
99     Sensor const* const* list;
100     ssize_t count = mgr.getSensorList(&list);
101     printf("numSensors=%d\n", int(count));
102 
103     sp<SensorEventQueue> q = mgr.createEventQueue();
104     printf("queue=%p\n", q.get());
105 
106     Sensor const* accelerometer = mgr.getDefaultSensor(Sensor::TYPE_ACCELEROMETER);
107     printf("accelerometer=%p (%s)\n",
108             accelerometer, accelerometer->getName().string());
109 
110     sStartTime = systemTime();
111 
112     q->enableSensor(accelerometer);
113 
114     q->setEventRate(accelerometer, ms2ns(10));
115 
116     sp<Looper> loop = new Looper(false);
117     loop->addFd(q->getFd(), 0, ALOOPER_EVENT_INPUT, receiver, q.get());
118 
119     do {
120         //printf("about to poll...\n");
121         int32_t ret = loop->pollOnce(-1);
122         switch (ret) {
123             case ALOOPER_POLL_WAKE:
124                 //("ALOOPER_POLL_WAKE\n");
125                 break;
126             case ALOOPER_POLL_CALLBACK:
127                 //("ALOOPER_POLL_CALLBACK\n");
128                 break;
129             case ALOOPER_POLL_TIMEOUT:
130                 printf("ALOOPER_POLL_TIMEOUT\n");
131                 break;
132             case ALOOPER_POLL_ERROR:
133                 printf("ALOOPER_POLL_TIMEOUT\n");
134                 break;
135             default:
136                 printf("ugh? poll returned %d\n", ret);
137                 break;
138         }
139     } while (1);
140 
141 
142     return 0;
143 }
144