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