Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / third_party / sudden_motion_sensor / sudden_motion_sensor_mac.cc
blobbbb7c91a81662b9e29ab040c431424b4a783c61e
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 // Note:
138 // - MacBook8,1 (12" MacBook, early 2015)
139 // has no accelerometer sensors.
141 // Tested by crc on a 13" MacBook Air.
142 { "MacBookAir1,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
144 // Tested by sfiera, pjw on a 13" MacBook Air.
145 { "MacBookAir2,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
147 // Note:
148 // - MacBookAir3,1 (11" MacBook Air, late 2010)
149 // - MacBookAir3,2 (13" MacBook Air, late 2010)
150 // - MacBookAir4,1 (11" MacBook Air, mid 2011)
151 // - MacBookAir4,2 (13" MacBook Air, mid 2011)
152 // - MacBookAir5,1 (11" MacBook Air, mid 2012)
153 // - MacBookAir5,2 (13" MacBook Air, mid 2012)
154 // - MacBookAir6,1 (11" MacBook Air, mid 2013)
155 // - MacBookAir6,2 (13" MacBook Air, mid 2013)
156 // - MacBookAir7,1 (11" MacBook Air, early 2015)
157 // - MacBookAir7,2 (13" MacBook Air, early 2015)
158 // have no accelerometer sensors.
160 // Tested by crc on a 15" MacBook Pro.
161 { "MacBookPro1,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
163 // Tested by Raul Cuza (via avi) on a 17" MacBook Pro.
164 { "MacBookPro1,2", NULL, { { 0, true }, { 2, true }, { 4, false } } },
166 // Tested by L.V. (via avi) on a 17" MacBook Pro.
167 { "MacBookPro2,1", NULL, { { 0, true }, { 2, false }, { 4, true } } },
169 // Tested by leandrogracia on a 15" MacBook Pro.
170 { "MacBookPro2,2", NULL, { { 0, true }, { 2, true }, { 4, false } } },
172 // Tested by S.Som. (via avi) on a 17" MacBook Pro.
173 { "MacBookPro3,1", "Mac-F42388C8",
174 { { 0, true }, { 2, false }, { 4, true } } },
176 // Tested by leandrogracia on a 15" MacBook Pro.
177 { "MacBookPro3,1", NULL, { { 0, false }, { 2, true }, { 4, true } } },
179 // Tested by leandrogracia on a 15" MacBook Pro.
180 // Tested by Eric Shapiro (via avi) on a 17" MacBook Pro.
181 { "MacBookPro4,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
183 // MacBookPro5,1 handled by the generic case below.
184 // Tested by leandrogracia on a 15" MacBook Pro.
186 // MacBookPro5,2 handled by the generic case below.
187 // Tested by S.Selz. (via avi) on a 17" MacBook Pro.
189 // Tested by dmaclach on a 15" MacBook Pro.
190 { "MacBookPro5,3", NULL, { { 2, false }, { 0, false }, { 4, true } } },
192 // MacBookPro5,4 handled by the generic case below.
193 // Tested by leandrogracia on a 15" MacBook Pro.
195 // Tested by leandrogracia on a 13" MacBook Pro.
196 { "MacBookPro5,5", NULL, { { 0, true }, { 2, true }, { 4, false } } },
198 // MacBookPro6,1 handled by the generic case below.
199 // Tested by khom, leadpipe on a 17" MacBook Pro.
201 // Tested by leandrogracia on a 15" MacBook Pro.
202 { "MacBookPro6,2", NULL, { { 0, true }, { 2, false }, { 4, true } } },
204 // Tested by leandrogracia on a 13" MacBook Pro.
205 { "MacBookPro7,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
207 // MacBookPro8,1 handled by the generic case below.
208 // Tested by avi on a 13" MacBook Pro.
210 // MacBookPro8,2 handled by the generic case below.
211 // Tested by avi on a 15" MacBook Pro.
213 // MacBookPro8,3 handled by the generic case below.
214 // Tested by avi on a 17" MacBook Pro.
216 // MacBookPro9,1 handled by the generic case below.
217 // Tested by avi on a 15" MacBook Pro.
219 // MacBookPro9,2 handled by the generic case below.
220 // Tested by avi on a 13" MacBook Pro.
222 // Note:
223 // - MacBookPro10,1 (15" MacBook Pro with Retina display)
224 // has no accelerometer sensors.
226 // MacBookPro10,2 handled by the generic case below.
227 // Tested by avi on a 13" MacBook Pro with Retina display.
229 // Note:
230 // - MacBookPro11,1 (13" MacBook Pro with Retina display)
231 // - MacBookPro11,2 (15" MacBook Pro with Retina display)
232 // - MacBookPro11,3 (15" MacBook Pro with Retina display)
233 // - MacBookPro11,4 (15" MacBook Pro with Retina display)
234 // - MacBookPro12,1 (13" MacBook Pro with Retina display)
235 // have no accelerometer sensors.
237 // Generic MacBook accelerometer sensor data, to be used for future models
238 // as well as models for which it is verified to be correct. Note that this
239 // configuration may have problems with inverted axes when used generically
240 // for untested models.
241 { "", NULL, { { 0, false }, { 2, false }, { 4, false } } }
244 // Create a SuddenMotionSensor object and return NULL if no valid sensor found.
245 SuddenMotionSensor* SuddenMotionSensor::Create() {
246 scoped_ptr<SuddenMotionSensor> accelerometer(new SuddenMotionSensor);
247 return accelerometer->Init() ? accelerometer.release() : NULL;
250 SuddenMotionSensor::~SuddenMotionSensor() {
251 IOServiceClose(io_connection_);
254 SuddenMotionSensor::SuddenMotionSensor()
255 : sensor_(NULL),
256 io_connection_(0) {
259 // Retrieve per-axis accelerometer values.
260 bool SuddenMotionSensor::ReadSensorValues(float axes[3]) {
261 DCHECK(sensor_);
263 // Reset output record memory buffer.
264 std::fill(output_record_.begin(), output_record_.end(), 0x00);
266 // Read record data from memory.
267 const size_t kInputSize = kGenericSensor.record_size;
268 size_t output_size = kGenericSensor.record_size;
270 if (IOConnectCallStructMethod(io_connection_, kGenericSensor.function,
271 static_cast<const char *>(&input_record_[0]), kInputSize,
272 &output_record_[0], &output_size) != KERN_SUCCESS) {
273 return false;
276 // Calculate per-axis calibrated values.
277 float axis_value[3];
279 for (int i = 0; i < 3; ++i) {
280 int sensor_value = 0;
281 int size = kGenericSensor.axis_size;
282 int index = sensor_->axis[i].index;
284 // Important Note: Little endian is assumed as this code is Mac-only
285 // and PowerPC is currently not supported.
286 memcpy(&sensor_value, &output_record_[index], size);
288 sensor_value = ExtendSign(sensor_value, size);
290 // Correct value using the current calibration.
291 axis_value[i] = static_cast<float>(sensor_value - kGenericSensor.zero_g) /
292 kGenericSensor.one_g;
294 // Make sure we reject any NaN or infinite values.
295 if (!isfinite(axis_value[i]))
296 return false;
298 // Clamp value to the [-1, 1] range.
299 if (axis_value[i] < -1.0)
300 axis_value[i] = -1.0;
301 else if (axis_value[i] > 1.0)
302 axis_value[i] = 1.0;
304 // Apply axis inversion.
305 if (sensor_->axis[i].inverted)
306 axis_value[i] = -axis_value[i];
309 axes[0] = axis_value[0];
310 axes[1] = axis_value[1];
311 axes[2] = axis_value[2];
313 return true;
316 // Probe the local hardware looking for a supported sensor device
317 // and initialize an I/O connection to it.
318 bool SuddenMotionSensor::Init() {
319 // Request model name from the kernel.
320 char local_model[32]; // size from SMSLib
321 size_t local_model_size = sizeof(local_model);
322 int params[2] = { CTL_HW, HW_MODEL };
323 if (sysctl(params, 2, local_model, &local_model_size, NULL, 0) != 0)
324 return false;
326 const SensorDescriptor* sensor_candidate = NULL;
328 // Look for the current model in the supported sensor list.
329 base::ScopedCFTypeRef<CFDataRef> board_id_data;
330 const int kNumSensors = arraysize(kSupportedSensors);
332 for (int i = 0; i < kNumSensors; ++i) {
333 // Check if the supported sensor model name is a prefix
334 // of the local hardware model (empty names are accepted).
335 const char* p1 = kSupportedSensors[i].model_name;
336 for (const char* p2 = local_model; *p1 != '\0' && *p1 == *p2; ++p1, ++p2)
337 continue;
338 if (*p1 != '\0')
339 continue;
341 // Check the board id.
342 if (kSupportedSensors[i].board_id) {
343 if (!board_id_data.get()) {
344 CFMutableDictionaryRef dict =
345 IOServiceMatching("IOPlatformExpertDevice");
346 if (!dict)
347 continue;
349 io_service_t platform_expert =
350 IOServiceGetMatchingService(kIOMasterPortDefault, dict);
351 if (!platform_expert)
352 continue;
354 board_id_data.reset((CFDataRef)
355 IORegistryEntryCreateCFProperty(platform_expert,
356 CFSTR("board-id"),
357 kCFAllocatorDefault,
358 0));
359 IOObjectRelease(platform_expert);
360 if (!board_id_data.get())
361 continue;
364 if (strcmp(kSupportedSensors[i].board_id,
365 (const char*)CFDataGetBytePtr(board_id_data)) != 0) {
366 continue;
370 // Local hardware found in the supported sensor list.
371 sensor_candidate = &kSupportedSensors[i];
373 // Get a dictionary of the services matching to the one in the sensor.
374 CFMutableDictionaryRef dict =
375 IOServiceMatching(kGenericSensor.service_name);
376 if (!dict)
377 continue;
379 // Get the first matching service.
380 io_service_t device = IOServiceGetMatchingService(kIOMasterPortDefault,
381 dict);
382 if (!device)
383 continue;
385 // Try to open device.
386 kern_return_t result;
387 result = IOServiceOpen(device, mach_task_self(), 0, &io_connection_);
388 IOObjectRelease(device);
389 if (result != KERN_SUCCESS || io_connection_ == 0)
390 return false;
392 // Local sensor service confirmed by IOKit.
393 sensor_ = sensor_candidate;
394 break;
397 if (sensor_ == NULL)
398 return false;
400 // Allocate and initialize input/output records.
401 input_record_.resize(kGenericSensor.record_size, 0x01);
402 output_record_.resize(kGenericSensor.record_size, 0x00);
404 // Try to retrieve the current orientation.
405 float test_axes[3];
406 return ReadSensorValues(test_axes);
409 // Extend the sign of an integer of less than 32 bits to a 32-bit integer.
410 int SuddenMotionSensor::ExtendSign(int value, size_t size) {
411 switch (size) {
412 case 1:
413 if (value & 0x00000080)
414 return value | 0xffffff00;
415 break;
417 case 2:
418 if (value & 0x00008000)
419 return value | 0xffff0000;
420 break;
422 case 3:
423 if (value & 0x00800000)
424 return value | 0xff000000;
425 break;
427 default:
428 LOG(FATAL) << "Invalid integer size for sign extension: " << size;
431 return value;