OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "device/generic_sensor/platform_sensor_ambient_light_mac.h" |
| 6 |
| 7 #include <IOKit/IOMessage.h> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "device/base/synchronization/shared_memory_seqlock_buffer.h" |
| 11 #include "device/generic_sensor/platform_sensor_provider_mac.h" |
| 12 #include "device/sensors/public/cpp/device_sensors_consts.h" |
| 13 #include "device/sensors/public/cpp/device_util_mac.h" |
| 14 |
| 15 namespace device { |
| 16 |
| 17 enum LmuFunctionIndex { |
| 18 kGetSensorReadingID = 0, // getSensorReading(int *, int *) |
| 19 }; |
| 20 |
| 21 PlatformSensorAmbientLightMac::PlatformSensorAmbientLightMac( |
| 22 mojom::SensorType type, |
| 23 mojo::ScopedSharedBufferMapping mapping, |
| 24 PlatformSensorProvider* provider) |
| 25 : PlatformSensor(type, std::move(mapping), provider), |
| 26 light_sensor_port_(nullptr), |
| 27 current_lux_(0.0) { |
| 28 DCHECK_EQ(type, mojom::SensorType::AMBIENT_LIGHT); |
| 29 } |
| 30 |
| 31 PlatformSensorAmbientLightMac::~PlatformSensorAmbientLightMac() = default; |
| 32 |
| 33 mojom::ReportingMode PlatformSensorAmbientLightMac::GetReportingMode() { |
| 34 return mojom::ReportingMode::ON_CHANGE; |
| 35 } |
| 36 |
| 37 bool PlatformSensorAmbientLightMac::CheckSensorConfiguration( |
| 38 const PlatformSensorConfiguration& configuration) { |
| 39 return configuration.frequency() > 0 && |
| 40 configuration.frequency() <= |
| 41 mojom::SensorConfiguration::kMaxAllowedFrequency; |
| 42 } |
| 43 |
| 44 PlatformSensorConfiguration |
| 45 PlatformSensorAmbientLightMac::GetDefaultConfiguration() { |
| 46 PlatformSensorConfiguration default_configuration; |
| 47 default_configuration.set_frequency(kDefaultAmbientLightFrequencyHz); |
| 48 return default_configuration; |
| 49 } |
| 50 |
| 51 void PlatformSensorAmbientLightMac::IOServiceCallback(void* context, |
| 52 io_service_t service, |
| 53 natural_t message_type, |
| 54 void* message_argument) { |
| 55 PlatformSensorAmbientLightMac* sensor = |
| 56 static_cast<PlatformSensorAmbientLightMac*>(context); |
| 57 uint32_t scalar_output_count = 2; |
| 58 uint64_t lux_values[2]; |
| 59 kern_return_t kr = IOConnectCallMethod( |
| 60 sensor->light_sensor_object_, LmuFunctionIndex::kGetSensorReadingID, |
| 61 nullptr, 0, nullptr, 0, lux_values, &scalar_output_count, nullptr, 0); |
| 62 |
| 63 if (kr == KERN_SUCCESS) |
| 64 sensor->UpdateReading(lux_values); |
| 65 } |
| 66 |
| 67 bool PlatformSensorAmbientLightMac::StartSensor( |
| 68 const PlatformSensorConfiguration& configuration) { |
| 69 // Tested and verified by riju that the following call works on |
| 70 // MacBookPro9,1 : Macbook Pro 15" (Mid 2012 model) |
| 71 // MacBookPro10,1 : Macbook Pro 15" (Retina Display, Early 2013 model). |
| 72 // MacBookPro10,2 : Macbook Pro 13" (Retina Display, Early 2013 model). |
| 73 // MacBookAir5,2 : Macbook Air 13" (Mid 2012 model) (by François Beaufort). |
| 74 // MacBookAir6,2 : Macbook Air 13" (Mid 2013 model). |
| 75 // Testing plans : please download the code and follow the comments :- |
| 76 // https://gist.github.com/riju/74af8c81a665e412d122/ |
| 77 // and add an entry here about the model and the status returned by the code. |
| 78 |
| 79 // Look up a registered IOService object whose class is AppleLMUController. |
| 80 light_sensor_service_.reset(IOServiceGetMatchingService( |
| 81 kIOMasterPortDefault, IOServiceMatching("AppleLMUController"))); |
| 82 |
| 83 // Return early if the ambient light sensor is not present. |
| 84 if (!light_sensor_service_) |
| 85 return false; |
| 86 |
| 87 light_sensor_port_.reset(IONotificationPortCreate(kIOMasterPortDefault)); |
| 88 if (!light_sensor_port_.is_valid()) |
| 89 return false; |
| 90 |
| 91 IONotificationPortSetDispatchQueue( |
| 92 light_sensor_port_.get(), |
| 93 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)); |
| 94 |
| 95 kern_return_t kr = IOServiceAddInterestNotification( |
| 96 light_sensor_port_.get(), light_sensor_service_, kIOGeneralInterest, |
| 97 IOServiceCallback, this, light_sensor_notification_.InitializeInto()); |
| 98 if (kr != KERN_SUCCESS) |
| 99 return false; |
| 100 |
| 101 kr = IOServiceOpen(light_sensor_service_, mach_task_self(), 0, |
| 102 light_sensor_object_.InitializeInto()); |
| 103 |
| 104 return kr == KERN_SUCCESS; |
| 105 } |
| 106 |
| 107 void PlatformSensorAmbientLightMac::StopSensor() { |
| 108 light_sensor_port_.reset(); |
| 109 light_sensor_notification_.reset(); |
| 110 light_sensor_object_.reset(); |
| 111 current_lux_ = 0.0; |
| 112 } |
| 113 |
| 114 void PlatformSensorAmbientLightMac::UpdateReading(uint64_t lux_values[2]) { |
| 115 uint64_t mean = (lux_values[0] + lux_values[1]) / 2; |
| 116 double lux = LMUvalueToLux(mean); |
| 117 if (lux == current_lux_) |
| 118 return; |
| 119 current_lux_ = lux; |
| 120 |
| 121 SensorReading reading; |
| 122 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); |
| 123 reading.values[0] = current_lux_; |
| 124 UpdateSensorReading(reading, true); |
| 125 } |
| 126 |
| 127 } // namespace device |
OLD | NEW |