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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3af068c36cdbb2b19cb6579c499f7b94216a1f55 |
| --- /dev/null |
| +++ b/device/generic_sensor/platform_sensor_ambient_light_mac.cc |
| @@ -0,0 +1,146 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "device/generic_sensor/platform_sensor_ambient_light_mac.h" |
| + |
| +#include <IOKit/IOMessage.h> |
| + |
| +#include "base/bind.h" |
| +#include "device/base/device_sensors_consts.h" |
| +#include "device/base/device_util_mac.h" |
| +#include "device/base/synchronization/shared_memory_seqlock_buffer.h" |
| +#include "device/generic_sensor/platform_sensor_provider_mac.h" |
| + |
| +namespace { |
| +enum LmuFunctionIndex { |
| + kGetSensorReadingID = 0, // getSensorReading(int *, int *) |
| +}; |
| +} // namespace |
| + |
| +namespace device { |
| + |
| +using DeviceLightHardwareBuffer = SharedMemorySeqLockBuffer<double>; |
| + |
| +PlatformSensorAmbientLightMac::PlatformSensorAmbientLightMac( |
| + mojom::SensorType type, |
| + mojo::ScopedSharedBufferMapping mapping, |
| + uint64_t buffer_size, |
| + PlatformSensorProvider* provider, |
| + scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) |
| + : PlatformSensor(type, std::move(mapping), provider), |
| + ui_task_runner_(std::move(ui_task_runner)), |
| + current_lux_(0) { |
| + // Tested and verified by riju that the following call works on |
| + // MacBookPro9,1 : Macbook Pro 15" (Mid 2012 model) |
| + // MacBookPro10,1 : Macbook Pro 15" (Retina Display, Early 2013 model). |
| + // MacBookPro10,2 : Macbook Pro 13" (Retina Display, Early 2013 model). |
| + // MacBookAir5,2 : Macbook Air 13" (Mid 2012 model) (by François Beaufort). |
| + // MacBookAir6,2 : Macbook Air 13" (Mid 2013 model). |
| + // Testing plans : please download the code and follow the comments :- |
| + // https://gist.github.com/riju/74af8c81a665e412d122/ |
| + // and add an entry here about the model and the status returned by the code. |
| + |
| + // Look up a registered IOService object whose class is AppleLMUController. |
| + light_sensor_service_.reset(IOServiceGetMatchingService( |
| + kIOMasterPortDefault, IOServiceMatching("AppleLMUController"))); |
| +} |
| + |
| +PlatformSensorAmbientLightMac::~PlatformSensorAmbientLightMac() { |
| + if (light_sensor_port_) |
| + IONotificationPortDestroy(light_sensor_port_); |
|
Reilly Grant (use Gerrit)
2016/10/04 07:11:27
Consider adding a specialization of ScopedGeneric
|
| + |
| + if (light_sensor_object_) |
| + IOObjectRelease(light_sensor_object_); |
|
Reilly Grant (use Gerrit)
2016/10/04 07:11:27
Use ScopedIOObject for this field. If you need to
|
| +} |
| + |
| +mojom::ReportingMode PlatformSensorAmbientLightMac::GetReportingMode() { |
| + return mojom::ReportingMode::ON_CHANGE; |
| +} |
| + |
| +bool PlatformSensorAmbientLightMac::CheckSensorConfiguration( |
| + const PlatformSensorConfiguration& configuration) { |
| + return configuration.frequency() > 0 && |
| + configuration.frequency() <= |
| + mojom::SensorConfiguration::kMaxAllowedFrequency; |
| +} |
| + |
| +PlatformSensorConfiguration |
| +PlatformSensorAmbientLightMac::GetDefaultConfiguration() { |
| + PlatformSensorConfiguration default_configuration; |
| + default_configuration.set_frequency(kDefaultAmbientLightFrequencyHz); |
| + return default_configuration; |
| +} |
| + |
| +void PlatformSensorAmbientLightMac::IOServiceCallback(void* context, |
| + io_service_t service, |
| + natural_t message_type, |
| + void*) { |
|
Robert Sesek
2016/10/04 15:28:18
All parameters should be named.
|
| + 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) |
| + return; |
| + sensor->UpdateReading(lux_values); |
|
Reilly Grant (use Gerrit)
2016/10/04 07:11:27
I would just write this as:
if (kr == KERN_SUCCES
|
| +} |
| + |
| +bool PlatformSensorAmbientLightMac::StartSensor( |
| + const PlatformSensorConfiguration& configuration) { |
| + // Return early if the ambient light sensor is not present. |
| + if (!light_sensor_service_) |
| + return false; |
| + |
| + light_sensor_port_ = IONotificationPortCreate(kIOMasterPortDefault); |
| + if (!light_sensor_port_) |
| + return false; |
| + |
| + IONotificationPortSetDispatchQueue( |
| + light_sensor_port_, |
| + dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)); |
| + |
| + io_object_t object; |
|
Robert Sesek
2016/10/04 15:28:18
What is |object| and is it leaked here?
|
| + kern_return_t kr = IOServiceAddInterestNotification( |
| + light_sensor_port_, light_sensor_service_, kIOGeneralInterest, |
| + IOServiceCallback, this, &object); |
| + if (kr != KERN_SUCCESS) |
| + return false; |
| + |
| + kr = IOServiceOpen(light_sensor_service_, mach_task_self(), 0, |
| + &light_sensor_object_); |
| + return kr == KERN_SUCCESS; |
| +} |
| + |
| +void PlatformSensorAmbientLightMac::StopSensor() { |
| + if (light_sensor_port_) |
| + IONotificationPortDestroy(light_sensor_port_); |
| + |
| + if (light_sensor_object_) |
| + IOObjectRelease(light_sensor_object_); |
| + |
| + light_sensor_port_ = nullptr; |
| + light_sensor_object_ = IO_OBJECT_NULL; |
| +} |
| + |
| +void PlatformSensorAmbientLightMac::UpdateReading(uint64_t lux_values[2]) { |
| + uint64_t mean = (lux_values[0] + lux_values[1]) / 2; |
| + double lux = LMUvalueToLux(mean); |
| + if (lux == current_lux_) |
| + return; |
| + current_lux_ = lux; |
| + DeviceLightHardwareBuffer* light_buffer = |
| + static_cast<DeviceLightHardwareBuffer*>(shared_buffer_mapping_.get()); |
| + light_buffer->seqlock.WriteBegin(); |
| + light_buffer->data = lux; |
| + light_buffer->seqlock.WriteEnd(); |
| + ui_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&PlatformSensorAmbientLightMac::NotifySensorReadingChanged, |
| + this)); |
| +} |
| + |
| +} // namespace device |