michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #include "Hal.h" michael@0: #include "nsITimer.h" michael@0: #include "smslib.h" michael@0: #include "nsComponentManagerUtils.h" michael@0: michael@0: #include michael@0: #include michael@0: #import michael@0: michael@0: #define MEAN_GRAVITY 9.80665 michael@0: #define DEFAULT_SENSOR_POLL 100 michael@0: using namespace mozilla::hal; michael@0: namespace mozilla { michael@0: namespace hal_impl { michael@0: static nsITimer* sUpdateTimer = nullptr; michael@0: static bool sActiveSensors[NUM_SENSOR_TYPE]; michael@0: static io_connect_t sDataPort = IO_OBJECT_NULL; michael@0: static uint64_t sLastMean = -1; michael@0: static float michael@0: LMUvalueToLux(uint64_t aValue) michael@0: { michael@0: //Conversion formula from regression. See Bug 793728. michael@0: // -3*(10^-27)*x^4 + 2.6*(10^-19)*x^3 + -3.4*(10^-12)*x^2 + 3.9*(10^-5)*x - 0.19 michael@0: long double powerC4 = 1/pow((long double)10,27); michael@0: long double powerC3 = 1/pow((long double)10,19); michael@0: long double powerC2 = 1/pow((long double)10,12); michael@0: long double powerC1 = 1/pow((long double)10,5); michael@0: michael@0: long double term4 = -3.0 * powerC4 * pow(aValue,4); michael@0: long double term3 = 2.6 * powerC3 * pow(aValue,3); michael@0: long double term2 = -3.4 * powerC2 * pow(aValue,2); michael@0: long double term1 = 3.9 * powerC1 * aValue; michael@0: michael@0: float lux = ceil(static_cast(term4 + term3 + term2 + term1 - 0.19)); michael@0: return lux > 0 ? lux : 0; michael@0: } michael@0: void michael@0: UpdateHandler(nsITimer *aTimer, void *aClosure) michael@0: { michael@0: for (int i = 0; i < NUM_SENSOR_TYPE; i++) { michael@0: if (!sActiveSensors[i]) { michael@0: continue; michael@0: } michael@0: SensorType sensor = static_cast(i); michael@0: InfallibleTArray values; michael@0: if (sensor == SENSOR_ACCELERATION) { michael@0: sms_acceleration accel; michael@0: smsGetData(&accel); michael@0: michael@0: values.AppendElement(accel.x * MEAN_GRAVITY); michael@0: values.AppendElement(accel.y * MEAN_GRAVITY); michael@0: values.AppendElement(accel.z * MEAN_GRAVITY); michael@0: } else if (sensor == SENSOR_LIGHT && sDataPort != IO_OBJECT_NULL) { michael@0: kern_return_t kr; michael@0: uint32_t outputs = 2; michael@0: uint64_t lightLMU[outputs]; michael@0: michael@0: kr = IOConnectCallMethod(sDataPort, 0, nil, 0, nil, 0, lightLMU, &outputs, nil, 0); michael@0: if (kr == KERN_SUCCESS) { michael@0: uint64_t mean = (lightLMU[0] + lightLMU[1]) / 2; michael@0: if (mean == sLastMean) { michael@0: continue; michael@0: } michael@0: sLastMean = mean; michael@0: values.AppendElement(LMUvalueToLux(mean)); michael@0: } else if (kr == kIOReturnBusy) { michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: hal::SensorData sdata(sensor, michael@0: PR_Now(), michael@0: values, michael@0: hal::SENSOR_ACCURACY_UNKNOWN); michael@0: hal::NotifySensorChange(sdata); michael@0: } michael@0: } michael@0: void michael@0: EnableSensorNotifications(SensorType aSensor) michael@0: { michael@0: if (aSensor == SENSOR_ACCELERATION) { michael@0: int result = smsStartup(nil, nil); michael@0: michael@0: if (result != SMS_SUCCESS) { michael@0: return; michael@0: } michael@0: michael@0: if (!smsLoadCalibration()) { michael@0: return; michael@0: } michael@0: } else if (aSensor == SENSOR_LIGHT) { michael@0: io_service_t serviceObject; michael@0: serviceObject = IOServiceGetMatchingService(kIOMasterPortDefault, michael@0: IOServiceMatching("AppleLMUController")); michael@0: if (!serviceObject) { michael@0: return; michael@0: } michael@0: kern_return_t kr; michael@0: kr = IOServiceOpen(serviceObject, mach_task_self(), 0, &sDataPort); michael@0: IOObjectRelease(serviceObject); michael@0: if (kr != KERN_SUCCESS) { michael@0: return; michael@0: } michael@0: } else { michael@0: NS_WARNING("EnableSensorNotifications called on an unknown sensor type"); michael@0: return; michael@0: } michael@0: sActiveSensors[aSensor] = true; michael@0: michael@0: if (!sUpdateTimer) { michael@0: CallCreateInstance("@mozilla.org/timer;1", &sUpdateTimer); michael@0: if (sUpdateTimer) { michael@0: sUpdateTimer->InitWithFuncCallback(UpdateHandler, michael@0: nullptr, michael@0: DEFAULT_SENSOR_POLL, michael@0: nsITimer::TYPE_REPEATING_SLACK); michael@0: } michael@0: } michael@0: } michael@0: void michael@0: DisableSensorNotifications(SensorType aSensor) michael@0: { michael@0: if (!sActiveSensors[aSensor] || (aSensor != SENSOR_ACCELERATION && aSensor != SENSOR_LIGHT)) { michael@0: return; michael@0: } michael@0: michael@0: sActiveSensors[aSensor] = false; michael@0: michael@0: if (aSensor == SENSOR_ACCELERATION) { michael@0: smsShutdown(); michael@0: } else if (aSensor == SENSOR_LIGHT) { michael@0: IOServiceClose(sDataPort); michael@0: } michael@0: // If all sensors are disabled, cancel the update timer. michael@0: if (sUpdateTimer) { michael@0: for (int i = 0; i < NUM_SENSOR_TYPE; i++) { michael@0: if (sActiveSensors[i]) { michael@0: return; michael@0: } michael@0: } michael@0: sUpdateTimer->Cancel(); michael@0: NS_RELEASE(sUpdateTimer); michael@0: } michael@0: } michael@0: } // hal_impl michael@0: } // mozilla