[Android] strip memdump and ps_ext tools.
[chromium-blink-merge.git] / third_party / sudden_motion_sensor / sudden_motion_sensor_mac.cc
blob1a628c4441ed5ab05a028f2c1d2bd0da59695897
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // This file is based on the SMSLib library.
6 //
7 // SMSLib Sudden Motion Sensor Access Library
8 // Copyright (c) 2010 Suitable Systems
9 // All rights reserved.
11 // Developed by: Daniel Griscom
12 // Suitable Systems
13 // http://www.suitable.com
15 // Permission is hereby granted, free of charge, to any person obtaining a
16 // copy of this software and associated documentation files (the
17 // "Software"), to deal with the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // - Redistributions of source code must retain the above copyright notice,
24 // this list of conditions and the following disclaimers.
26 // - Redistributions in binary form must reproduce the above copyright
27 // notice, this list of conditions and the following disclaimers in the
28 // documentation and/or other materials provided with the distribution.
30 // - Neither the names of Suitable Systems nor the names of its
31 // contributors may be used to endorse or promote products derived from
32 // this Software without specific prior written permission.
34 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
35 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
37 // IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR
38 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
39 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
40 // SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
42 // For more information about SMSLib, see
43 // <http://www.suitable.com/tools/smslib.html>
44 // or contact
45 // Daniel Griscom
46 // Suitable Systems
47 // 1 Centre Street, Suite 204
48 // Wakefield, MA 01880
49 // (781) 665-0053
51 #include "sudden_motion_sensor_mac.h"
53 #include <math.h>
54 #include <sys/sysctl.h>
56 #include "base/logging.h"
57 #include "base/mac/scoped_cftyperef.h"
58 #include "base/memory/scoped_ptr.h"
60 struct SuddenMotionSensor::GenericMacbookSensor {
61 // Name of device to be read.
62 const char* service_name;
64 // Number of bytes of the axis data.
65 int axis_size;
67 // Default calibration value for zero g.
68 float zero_g;
70 // Default calibration value for one g (negative when axis is inverted).
71 float one_g;
73 // Kernel function index.
74 unsigned int function;
76 // Size of the sensor record to be sent/received.
77 unsigned int record_size;
80 struct SuddenMotionSensor::AxisData {
81 // Location of the first byte representing the axis in the sensor data.
82 int index;
84 // Axis inversion flag. The value changes often between models.
85 bool inverted;
88 // Sudden Motion Sensor descriptor.
89 struct SuddenMotionSensor::SensorDescriptor {
90 // Prefix of model to be tested.
91 const char* model_name;
93 // Board id of model, or NULL if it doesn't matter.
94 const char* board_id;
96 // Axis-specific data (x,y,z order).
97 AxisData axis[3];
100 // Typical sensor parameters in MacBook models.
101 const SuddenMotionSensor::GenericMacbookSensor
102 SuddenMotionSensor::kGenericSensor = {
103 "SMCMotionSensor", 2,
104 0, 251,
105 5, 40
108 // Supported sensor descriptors. Add entries here to enhance compatibility.
109 // Tested in order; place more specific entries before more general ones. (All
110 // non-tested entries from SMSLib have been removed.)
111 const SuddenMotionSensor::SensorDescriptor
112 SuddenMotionSensor::kSupportedSensors[] = {
113 // Tested by tommyw on a 13" MacBook.
114 { "MacBook1,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
116 // Tested by S.Selz. (via avi) on a 13" MacBook.
117 { "MacBook2,1", NULL, { { 0, true }, { 2, false }, { 4, true } } },
119 // Tested by verhees on a 13" MacBook.
120 { "MacBook3,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
122 // Tested by adlr on a 13" MacBook.
123 { "MacBook4,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
125 // Tested by thakis on a 13" MacBook.
126 { "MacBook5,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
128 // Tested by Adam Gerson (via avi) on a 13" MacBook.
129 { "MacBook5,2", NULL, { { 0, false }, { 2, true }, { 4, true } } },
131 // Tested by tommyw on a 13" MacBook.
132 { "MacBook6,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
134 // Tested by avi on a 13" MacBook.
135 { "MacBook7,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
137 // Tested by crc on a 13" MacBook Air.
138 { "MacBookAir1,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
140 // Tested by sfiera, pjw on a 13" MacBook Air.
141 { "MacBookAir2,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
143 // Note:
144 // - MacBookAir3,1 (11" MacBook Air, late 2010)
145 // - MacBookAir3,2 (13" MacBook Air, late 2010)
146 // - MacBookAir4,1 (11" MacBook Air, mid 2011)
147 // - MacBookAir4,2 (13" MacBook Air, mid 2011)
148 // - MacBookAir5,1 (11" MacBook Air, mid 2012)
149 // - MacBookAir5,2 (13" MacBook Air, mid 2012)
150 // - MacBookAir6,1 (11" MacBook Air, mid 2013)
151 // - MacBookAir6,2 (13" MacBook Air, mid 2013)
152 // have no accelerometer sensors.
154 // Tested by crc on a 15" MacBook Pro.
155 { "MacBookPro1,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
157 // Tested by Raul Cuza (via avi) on a 17" MacBook Pro.
158 { "MacBookPro1,2", NULL, { { 0, true }, { 2, true }, { 4, false } } },
160 // Tested by L.V. (via avi) on a 17" MacBook Pro.
161 { "MacBookPro2,1", NULL, { { 0, true }, { 2, false }, { 4, true } } },
163 // Tested by leandrogracia on a 15" MacBook Pro.
164 { "MacBookPro2,2", NULL, { { 0, true }, { 2, true }, { 4, false } } },
166 // Tested by S.Som. (via avi) on a 17" MacBook Pro.
167 { "MacBookPro3,1", "Mac-F42388C8",
168 { { 0, true }, { 2, false }, { 4, true } } },
170 // Tested by leandrogracia on a 15" MacBook Pro.
171 { "MacBookPro3,1", NULL, { { 0, false }, { 2, true }, { 4, true } } },
173 // Tested by leandrogracia on a 15" MacBook Pro.
174 // Tested by Eric Shapiro (via avi) on a 17" MacBook Pro.
175 { "MacBookPro4,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
177 // MacBookPro5,1 handled by the generic case below.
178 // Tested by leandrogracia on a 15" MacBook Pro.
180 // MacBookPro5,2 handled by the generic case below.
181 // Tested by S.Selz. (via avi) on a 17" MacBook Pro.
183 // Tested by dmaclach on a 15" MacBook Pro.
184 { "MacBookPro5,3", NULL, { { 2, false }, { 0, false }, { 4, true } } },
186 // MacBookPro5,4 handled by the generic case below.
187 // Tested by leandrogracia on a 15" MacBook Pro.
189 // Tested by leandrogracia on a 13" MacBook Pro.
190 { "MacBookPro5,5", NULL, { { 0, true }, { 2, true }, { 4, false } } },
192 // MacBookPro6,1 handled by the generic case below.
193 // Tested by khom, leadpipe on a 17" MacBook Pro.
195 // Tested by leandrogracia on a 15" MacBook Pro.
196 { "MacBookPro6,2", NULL, { { 0, true }, { 2, false }, { 4, true } } },
198 // Tested by leandrogracia on a 13" MacBook Pro.
199 { "MacBookPro7,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
201 // MacBookPro8,1 handled by the generic case below.
202 // Tested by avi on a 13" MacBook Pro.
204 // MacBookPro8,2 handled by the generic case below.
205 // Tested by avi on a 15" MacBook Pro.
207 // MacBookPro8,3 handled by the generic case below.
208 // Tested by avi on a 17" MacBook Pro.
210 // MacBookPro9,1 handled by the generic case below.
211 // Tested by avi on a 15" MacBook Pro.
213 // MacBookPro9,2 handled by the generic case below.
214 // Tested by avi on a 13" MacBook Pro.
216 // Note:
217 // - MacBookPro10,1 (15" MacBook Pro with Retina display)
218 // has no accelerometer sensors.
220 // MacBookPro10,2 handled by the generic case below.
221 // Tested by avi on a 13" MacBook Pro with Retina display.
223 // Note:
224 // - MacBookPro11,1 (13" MacBook Pro with Retina display)
225 // - MacBookPro11,2 (15" MacBook Pro with Retina display)
226 // - MacBookPro11,3 (15" MacBook Pro with Retina display)
227 // have no accelerometer sensors.
229 // Generic MacBook accelerometer sensor data, to be used for future models
230 // as well as models for which it is verified to be correct. Note that this
231 // configuration may have problems with inverted axes when used generically
232 // for untested models.
233 { "", NULL, { { 0, false }, { 2, false }, { 4, false } } }
236 // Create a SuddenMotionSensor object and return NULL if no valid sensor found.
237 SuddenMotionSensor* SuddenMotionSensor::Create() {
238 scoped_ptr<SuddenMotionSensor> accelerometer(new SuddenMotionSensor);
239 return accelerometer->Init() ? accelerometer.release() : NULL;
242 SuddenMotionSensor::~SuddenMotionSensor() {
243 IOServiceClose(io_connection_);
246 SuddenMotionSensor::SuddenMotionSensor()
247 : sensor_(NULL),
248 io_connection_(0) {
251 // Retrieve per-axis accelerometer values.
252 bool SuddenMotionSensor::ReadSensorValues(float axes[3]) {
253 DCHECK(sensor_);
255 // Reset output record memory buffer.
256 std::fill(output_record_.begin(), output_record_.end(), 0x00);
258 // Read record data from memory.
259 const size_t kInputSize = kGenericSensor.record_size;
260 size_t output_size = kGenericSensor.record_size;
262 if (IOConnectCallStructMethod(io_connection_, kGenericSensor.function,
263 static_cast<const char *>(&input_record_[0]), kInputSize,
264 &output_record_[0], &output_size) != KERN_SUCCESS) {
265 return false;
268 // Calculate per-axis calibrated values.
269 float axis_value[3];
271 for (int i = 0; i < 3; ++i) {
272 int sensor_value = 0;
273 int size = kGenericSensor.axis_size;
274 int index = sensor_->axis[i].index;
276 // Important Note: Little endian is assumed as this code is Mac-only
277 // and PowerPC is currently not supported.
278 memcpy(&sensor_value, &output_record_[index], size);
280 sensor_value = ExtendSign(sensor_value, size);
282 // Correct value using the current calibration.
283 axis_value[i] = static_cast<float>(sensor_value - kGenericSensor.zero_g) /
284 kGenericSensor.one_g;
286 // Make sure we reject any NaN or infinite values.
287 if (!isfinite(axis_value[i]))
288 return false;
290 // Clamp value to the [-1, 1] range.
291 if (axis_value[i] < -1.0)
292 axis_value[i] = -1.0;
293 else if (axis_value[i] > 1.0)
294 axis_value[i] = 1.0;
296 // Apply axis inversion.
297 if (sensor_->axis[i].inverted)
298 axis_value[i] = -axis_value[i];
301 axes[0] = axis_value[0];
302 axes[1] = axis_value[1];
303 axes[2] = axis_value[2];
305 return true;
308 // Probe the local hardware looking for a supported sensor device
309 // and initialize an I/O connection to it.
310 bool SuddenMotionSensor::Init() {
311 // Request model name from the kernel.
312 char local_model[32]; // size from SMSLib
313 size_t local_model_size = sizeof(local_model);
314 int params[2] = { CTL_HW, HW_MODEL };
315 if (sysctl(params, 2, local_model, &local_model_size, NULL, 0) != 0)
316 return false;
318 const SensorDescriptor* sensor_candidate = NULL;
320 // Look for the current model in the supported sensor list.
321 base::ScopedCFTypeRef<CFDataRef> board_id_data;
322 const int kNumSensors = arraysize(kSupportedSensors);
324 for (int i = 0; i < kNumSensors; ++i) {
325 // Check if the supported sensor model name is a prefix
326 // of the local hardware model (empty names are accepted).
327 const char* p1 = kSupportedSensors[i].model_name;
328 for (const char* p2 = local_model; *p1 != '\0' && *p1 == *p2; ++p1, ++p2)
329 continue;
330 if (*p1 != '\0')
331 continue;
333 // Check the board id.
334 if (kSupportedSensors[i].board_id) {
335 if (!board_id_data.get()) {
336 CFMutableDictionaryRef dict =
337 IOServiceMatching("IOPlatformExpertDevice");
338 if (!dict)
339 continue;
341 io_service_t platform_expert =
342 IOServiceGetMatchingService(kIOMasterPortDefault, dict);
343 if (!platform_expert)
344 continue;
346 board_id_data.reset((CFDataRef)
347 IORegistryEntryCreateCFProperty(platform_expert,
348 CFSTR("board-id"),
349 kCFAllocatorDefault,
350 0));
351 IOObjectRelease(platform_expert);
352 if (!board_id_data.get())
353 continue;
356 if (strcmp(kSupportedSensors[i].board_id,
357 (const char*)CFDataGetBytePtr(board_id_data)) != 0) {
358 continue;
362 // Local hardware found in the supported sensor list.
363 sensor_candidate = &kSupportedSensors[i];
365 // Get a dictionary of the services matching to the one in the sensor.
366 CFMutableDictionaryRef dict =
367 IOServiceMatching(kGenericSensor.service_name);
368 if (!dict)
369 continue;
371 // Get the first matching service.
372 io_service_t device = IOServiceGetMatchingService(kIOMasterPortDefault,
373 dict);
374 if (!device)
375 continue;
377 // Try to open device.
378 kern_return_t result;
379 result = IOServiceOpen(device, mach_task_self(), 0, &io_connection_);
380 IOObjectRelease(device);
381 if (result != KERN_SUCCESS || io_connection_ == 0)
382 return false;
384 // Local sensor service confirmed by IOKit.
385 sensor_ = sensor_candidate;
386 break;
389 if (sensor_ == NULL)
390 return false;
392 // Allocate and initialize input/output records.
393 input_record_.resize(kGenericSensor.record_size, 0x01);
394 output_record_.resize(kGenericSensor.record_size, 0x00);
396 // Try to retrieve the current orientation.
397 float test_axes[3];
398 return ReadSensorValues(test_axes);
401 // Extend the sign of an integer of less than 32 bits to a 32-bit integer.
402 int SuddenMotionSensor::ExtendSign(int value, size_t size) {
403 switch (size) {
404 case 1:
405 if (value & 0x00000080)
406 return value | 0xffffff00;
407 break;
409 case 2:
410 if (value & 0x00008000)
411 return value | 0xffff0000;
412 break;
414 case 3:
415 if (value & 0x00800000)
416 return value | 0xff000000;
417 break;
419 default:
420 LOG(FATAL) << "Invalid integer size for sign extension: " << size;
423 return value;