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;
23 LMUvalueToLux(uint64_t aValue)
25 //Conversion formula from regression. See Bug 793728.
26 // -3*(10^-27)*x^4 + 2.6*(10^-19)*x^3 + -3.4*(10^-12)*x^2 + 3.9*(10^-5)*x - 0.19
27 long double powerC4 = 1/pow((long double)10,27);
28 long double powerC3 = 1/pow((long double)10,19);
29 long double powerC2 = 1/pow((long double)10,12);
30 long double powerC1 = 1/pow((long double)10,5);
32 long double term4 = -3.0 * powerC4 * pow(aValue,4);
33 long double term3 = 2.6 * powerC3 * pow(aValue,3);
34 long double term2 = -3.4 * powerC2 * pow(aValue,2);
35 long double term1 = 3.9 * powerC1 * aValue;
37 float lux = ceil(static_cast<float>(term4 + term3 + term2 + term1 - 0.19));
38 return lux > 0 ? lux : 0;
41 UpdateHandler(nsITimer *aTimer, void *aClosure)
43 for (int i = 0; i < NUM_SENSOR_TYPE; i++) {
44 if (!sActiveSensors[i]) {
47 SensorType sensor = static_cast<SensorType>(i);
48 InfallibleTArray<float> values;
49 if (sensor == SENSOR_ACCELERATION) {
50 sms_acceleration accel;
53 values.AppendElement(accel.x * MEAN_GRAVITY);
54 values.AppendElement(accel.y * MEAN_GRAVITY);
55 values.AppendElement(accel.z * MEAN_GRAVITY);
56 } else if (sensor == SENSOR_LIGHT && sDataPort != IO_OBJECT_NULL) {
59 uint64_t lightLMU[outputs];
61 kr = IOConnectCallMethod(sDataPort, 0, nil, 0, nil, 0, lightLMU, &outputs, nil, 0);
62 if (kr == KERN_SUCCESS) {
63 uint64_t mean = (lightLMU[0] + lightLMU[1]) / 2;
64 if (mean == sLastMean) {
68 values.AppendElement(LMUvalueToLux(mean));
69 } else if (kr == kIOReturnBusy) {
74 hal::SensorData sdata(sensor,
77 hal::SENSOR_ACCURACY_UNKNOWN);
78 hal::NotifySensorChange(sdata);
82 EnableSensorNotifications(SensorType aSensor)
84 if (aSensor == SENSOR_ACCELERATION) {
85 int result = smsStartup(nil, nil);
87 if (result != SMS_SUCCESS) {
91 if (!smsLoadCalibration()) {
94 } else if (aSensor == SENSOR_LIGHT) {
95 io_service_t serviceObject;
96 serviceObject = IOServiceGetMatchingService(kIOMasterPortDefault,
97 IOServiceMatching("AppleLMUController"));
102 kr = IOServiceOpen(serviceObject, mach_task_self(), 0, &sDataPort);
103 IOObjectRelease(serviceObject);
104 if (kr != KERN_SUCCESS) {
108 NS_WARNING("EnableSensorNotifications called on an unknown sensor type");
111 sActiveSensors[aSensor] = true;
114 CallCreateInstance("@mozilla.org/timer;1", &sUpdateTimer);
116 sUpdateTimer->InitWithFuncCallback(UpdateHandler,
119 nsITimer::TYPE_REPEATING_SLACK);
124 DisableSensorNotifications(SensorType aSensor)
126 if (!sActiveSensors[aSensor] || (aSensor != SENSOR_ACCELERATION && aSensor != SENSOR_LIGHT)) {
130 sActiveSensors[aSensor] = false;
132 if (aSensor == SENSOR_ACCELERATION) {
134 } else if (aSensor == SENSOR_LIGHT) {
135 IOServiceClose(sDataPort);
137 // If all sensors are disabled, cancel the update timer.
139 for (int i = 0; i < NUM_SENSOR_TYPE; i++) {
140 if (sActiveSensors[i]) {
144 sUpdateTimer->Cancel();
145 NS_RELEASE(sUpdateTimer);