Chromium Code Reviews| Index: device/generic_sensor/platform_sensor_ambient_light_mac.cc |
| diff --git a/device/generic_sensor/platform_sensor_ambient_light_mac.cc b/device/generic_sensor/platform_sensor_ambient_light_mac.cc |
| index 7256cfb4bb72491b2f84823a0a38f1399fdfc6bf..646c4a34de8a2640f5d7d8780f5d80e83660b26d 100644 |
| --- a/device/generic_sensor/platform_sensor_ambient_light_mac.cc |
| +++ b/device/generic_sensor/platform_sensor_ambient_light_mac.cc |
| @@ -54,14 +54,8 @@ void PlatformSensorAmbientLightMac::IOServiceCallback(void* context, |
| void* message_argument) { |
| PlatformSensorAmbientLightMac* sensor = |
| static_cast<PlatformSensorAmbientLightMac*>(context); |
| - uint32_t scalar_output_count = 2; |
| - uint64_t lux_values[2]; |
| - kern_return_t kr = IOConnectCallMethod( |
| - sensor->light_sensor_object_, LmuFunctionIndex::kGetSensorReadingID, |
| - nullptr, 0, nullptr, 0, lux_values, &scalar_output_count, nullptr, 0); |
| - |
| - if (kr == KERN_SUCCESS) |
| - sensor->UpdateReading(lux_values); |
| + if (!sensor->ReadAndUpdate()) |
| + sensor->NotifySensorError(); |
|
timvolodine
2016/10/21 12:51:50
if the callback is executed often would that be a
|
| } |
| bool PlatformSensorAmbientLightMac::StartSensor( |
| @@ -100,8 +94,14 @@ bool PlatformSensorAmbientLightMac::StartSensor( |
| kr = IOServiceOpen(light_sensor_service_, mach_task_self(), 0, |
| light_sensor_object_.InitializeInto()); |
| + if (kr != KERN_SUCCESS) |
| + return false; |
| + |
| + bool success = ReadAndUpdate(); |
| + if (!success) |
| + StopSensor(); |
| - return kr == KERN_SUCCESS; |
| + return success; |
| } |
| void PlatformSensorAmbientLightMac::StopSensor() { |
| @@ -111,17 +111,27 @@ void PlatformSensorAmbientLightMac::StopSensor() { |
| current_lux_ = 0.0; |
| } |
| -void PlatformSensorAmbientLightMac::UpdateReading(uint64_t lux_values[2]) { |
| +bool PlatformSensorAmbientLightMac::ReadAndUpdate() { |
| + uint32_t scalar_output_count = 2; |
| + uint64_t lux_values[2]; |
| + kern_return_t kr = IOConnectCallMethod( |
| + light_sensor_object_, LmuFunctionIndex::kGetSensorReadingID, nullptr, 0, |
| + nullptr, 0, lux_values, &scalar_output_count, nullptr, 0); |
| + |
| + if (kr != KERN_SUCCESS) |
| + return false; |
| + |
| uint64_t mean = (lux_values[0] + lux_values[1]) / 2; |
| double lux = LMUvalueToLux(mean); |
| if (lux == current_lux_) |
| - return; |
| + return true; |
| current_lux_ = lux; |
| SensorReading reading; |
| reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); |
| reading.values[0] = current_lux_; |
| UpdateSensorReading(reading, true); |
| + return true; |
| } |
| } // namespace device |