1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsComponentManagerUtils.h"
11 #import <IOKit/IOKitLib.h>
13 #define MEAN_GRAVITY 9.80665
14 #define DEFAULT_SENSOR_POLL 100
15 using namespace mozilla::hal;
18 static nsITimer* sUpdateTimer = nullptr;
19 static bool sActiveSensors[NUM_SENSOR_TYPE];
20 static io_connect_t sDataPort = IO_OBJECT_NULL;
21 static uint64_t sLastMean = -1;
22 static float LMUvalueToLux(uint64_t aValue) {
23 // Conversion formula from regression. See Bug 793728.
24 // -3*(10^-27)*x^4 + 2.6*(10^-19)*x^3 + -3.4*(10^-12)*x^2 + 3.9*(10^-5)*x - 0.19
25 long double powerC4 = 1 / pow((long double)10, 27);
26 long double powerC3 = 1 / pow((long double)10, 19);
27 long double powerC2 = 1 / pow((long double)10, 12);
28 long double powerC1 = 1 / pow((long double)10, 5);
30 long double term4 = -3.0 * powerC4 * pow(aValue, 4);
31 long double term3 = 2.6 * powerC3 * pow(aValue, 3);
32 long double term2 = -3.4 * powerC2 * pow(aValue, 2);
33 long double term1 = 3.9 * powerC1 * aValue;
35 float lux = ceil(static_cast<float>(term4 + term3 + term2 + term1 - 0.19));
36 return lux > 0 ? lux : 0;
38 void UpdateHandler(nsITimer* aTimer, void* aClosure) {
39 for (int i = 0; i < NUM_SENSOR_TYPE; i++) {
40 if (!sActiveSensors[i]) {
43 SensorType sensor = static_cast<SensorType>(i);
44 nsTArray<float> values;
45 if (sensor == SENSOR_ACCELERATION) {
46 sms_acceleration accel;
49 values.AppendElement(accel.x * MEAN_GRAVITY);
50 values.AppendElement(accel.y * MEAN_GRAVITY);
51 values.AppendElement(accel.z * MEAN_GRAVITY);
52 } else if (sensor == SENSOR_LIGHT && sDataPort != IO_OBJECT_NULL) {
55 uint64_t lightLMU[outputs];
57 kr = IOConnectCallMethod(sDataPort, 0, nil, 0, nil, 0, lightLMU, &outputs, nil, 0);
58 if (kr == KERN_SUCCESS) {
59 uint64_t mean = (lightLMU[0] + lightLMU[1]) / 2;
60 if (mean == sLastMean) {
64 values.AppendElement(LMUvalueToLux(mean));
65 } else if (kr == kIOReturnBusy) {
70 hal::SensorData sdata(sensor, PR_Now(), values);
71 hal::NotifySensorChange(sdata);
74 void EnableSensorNotifications(SensorType aSensor) {
75 if (aSensor == SENSOR_ACCELERATION) {
76 int result = smsStartup(nil, nil);
78 if (result != SMS_SUCCESS) {
82 if (!smsLoadCalibration()) {
85 } else if (aSensor == SENSOR_LIGHT) {
86 io_service_t serviceObject;
88 IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleLMUController"));
93 kr = IOServiceOpen(serviceObject, mach_task_self(), 0, &sDataPort);
94 IOObjectRelease(serviceObject);
95 if (kr != KERN_SUCCESS) {
99 NS_WARNING("EnableSensorNotifications called on an unknown sensor type");
102 sActiveSensors[aSensor] = true;
105 CallCreateInstance("@mozilla.org/timer;1", &sUpdateTimer);
107 sUpdateTimer->InitWithNamedFuncCallback(UpdateHandler, nullptr, DEFAULT_SENSOR_POLL,
108 nsITimer::TYPE_REPEATING_SLACK,
109 "hal_impl::UpdateHandler");
113 void DisableSensorNotifications(SensorType aSensor) {
114 if (!sActiveSensors[aSensor] || (aSensor != SENSOR_ACCELERATION && aSensor != SENSOR_LIGHT)) {
118 sActiveSensors[aSensor] = false;
120 if (aSensor == SENSOR_ACCELERATION) {
122 } else if (aSensor == SENSOR_LIGHT) {
123 IOServiceClose(sDataPort);
125 // If all sensors are disabled, cancel the update timer.
127 for (int i = 0; i < NUM_SENSOR_TYPE; i++) {
128 if (sActiveSensors[i]) {
132 sUpdateTimer->Cancel();
133 NS_RELEASE(sUpdateTimer);
136 } // namespace hal_impl
137 } // namespace mozilla