OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/generic_sensor/platform_sensor_ambient_light_mac.h" | 5 #include "device/generic_sensor/platform_sensor_ambient_light_mac.h" |
6 | 6 |
7 #include <IOKit/IOMessage.h> | 7 #include <IOKit/IOMessage.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "device/base/synchronization/shared_memory_seqlock_buffer.h" | 10 #include "device/base/synchronization/shared_memory_seqlock_buffer.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 default_configuration.set_frequency(kDefaultAmbientLightFrequencyHz); | 47 default_configuration.set_frequency(kDefaultAmbientLightFrequencyHz); |
48 return default_configuration; | 48 return default_configuration; |
49 } | 49 } |
50 | 50 |
51 void PlatformSensorAmbientLightMac::IOServiceCallback(void* context, | 51 void PlatformSensorAmbientLightMac::IOServiceCallback(void* context, |
52 io_service_t service, | 52 io_service_t service, |
53 natural_t message_type, | 53 natural_t message_type, |
54 void* message_argument) { | 54 void* message_argument) { |
55 PlatformSensorAmbientLightMac* sensor = | 55 PlatformSensorAmbientLightMac* sensor = |
56 static_cast<PlatformSensorAmbientLightMac*>(context); | 56 static_cast<PlatformSensorAmbientLightMac*>(context); |
57 uint32_t scalar_output_count = 2; | 57 if (!sensor->ReadAndUpdate()) { |
58 uint64_t lux_values[2]; | 58 sensor->NotifySensorError(); |
59 kern_return_t kr = IOConnectCallMethod( | 59 sensor->StopSensor(); |
60 sensor->light_sensor_object_, LmuFunctionIndex::kGetSensorReadingID, | 60 } |
61 nullptr, 0, nullptr, 0, lux_values, &scalar_output_count, nullptr, 0); | |
62 | |
63 if (kr == KERN_SUCCESS) | |
64 sensor->UpdateReading(lux_values); | |
65 } | 61 } |
66 | 62 |
67 bool PlatformSensorAmbientLightMac::StartSensor( | 63 bool PlatformSensorAmbientLightMac::StartSensor( |
68 const PlatformSensorConfiguration& configuration) { | 64 const PlatformSensorConfiguration& configuration) { |
69 // Tested and verified by riju that the following call works on | 65 // Tested and verified by riju that the following call works on |
70 // MacBookPro9,1 : Macbook Pro 15" (Mid 2012 model) | 66 // MacBookPro9,1 : Macbook Pro 15" (Mid 2012 model) |
71 // MacBookPro10,1 : Macbook Pro 15" (Retina Display, Early 2013 model). | 67 // MacBookPro10,1 : Macbook Pro 15" (Retina Display, Early 2013 model). |
72 // MacBookPro10,2 : Macbook Pro 13" (Retina Display, Early 2013 model). | 68 // MacBookPro10,2 : Macbook Pro 13" (Retina Display, Early 2013 model). |
73 // MacBookAir5,2 : Macbook Air 13" (Mid 2012 model) (by François Beaufort). | 69 // MacBookAir5,2 : Macbook Air 13" (Mid 2012 model) (by François Beaufort). |
74 // MacBookAir6,2 : Macbook Air 13" (Mid 2013 model). | 70 // MacBookAir6,2 : Macbook Air 13" (Mid 2013 model). |
(...skipping 18 matching lines...) Expand all Loading... |
93 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)); | 89 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)); |
94 | 90 |
95 kern_return_t kr = IOServiceAddInterestNotification( | 91 kern_return_t kr = IOServiceAddInterestNotification( |
96 light_sensor_port_.get(), light_sensor_service_, kIOGeneralInterest, | 92 light_sensor_port_.get(), light_sensor_service_, kIOGeneralInterest, |
97 IOServiceCallback, this, light_sensor_notification_.InitializeInto()); | 93 IOServiceCallback, this, light_sensor_notification_.InitializeInto()); |
98 if (kr != KERN_SUCCESS) | 94 if (kr != KERN_SUCCESS) |
99 return false; | 95 return false; |
100 | 96 |
101 kr = IOServiceOpen(light_sensor_service_, mach_task_self(), 0, | 97 kr = IOServiceOpen(light_sensor_service_, mach_task_self(), 0, |
102 light_sensor_object_.InitializeInto()); | 98 light_sensor_object_.InitializeInto()); |
| 99 if (kr != KERN_SUCCESS) |
| 100 return false; |
103 | 101 |
104 return kr == KERN_SUCCESS; | 102 bool success = ReadAndUpdate(); |
| 103 if (!success) |
| 104 StopSensor(); |
| 105 |
| 106 return success; |
105 } | 107 } |
106 | 108 |
107 void PlatformSensorAmbientLightMac::StopSensor() { | 109 void PlatformSensorAmbientLightMac::StopSensor() { |
108 light_sensor_port_.reset(); | 110 light_sensor_port_.reset(); |
109 light_sensor_notification_.reset(); | 111 light_sensor_notification_.reset(); |
110 light_sensor_object_.reset(); | 112 light_sensor_object_.reset(); |
| 113 light_sensor_service_.reset(); |
111 current_lux_ = 0.0; | 114 current_lux_ = 0.0; |
112 } | 115 } |
113 | 116 |
114 void PlatformSensorAmbientLightMac::UpdateReading(uint64_t lux_values[2]) { | 117 bool PlatformSensorAmbientLightMac::ReadAndUpdate() { |
| 118 uint32_t scalar_output_count = 2; |
| 119 uint64_t lux_values[2]; |
| 120 kern_return_t kr = IOConnectCallMethod( |
| 121 light_sensor_object_, LmuFunctionIndex::kGetSensorReadingID, nullptr, 0, |
| 122 nullptr, 0, lux_values, &scalar_output_count, nullptr, 0); |
| 123 |
| 124 if (kr != KERN_SUCCESS) |
| 125 return false; |
| 126 |
115 uint64_t mean = (lux_values[0] + lux_values[1]) / 2; | 127 uint64_t mean = (lux_values[0] + lux_values[1]) / 2; |
116 double lux = LMUvalueToLux(mean); | 128 double lux = LMUvalueToLux(mean); |
117 if (lux == current_lux_) | 129 if (lux == current_lux_) |
118 return; | 130 return true; |
119 current_lux_ = lux; | 131 current_lux_ = lux; |
120 | 132 |
121 SensorReading reading; | 133 SensorReading reading; |
122 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); | 134 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); |
123 reading.values[0] = current_lux_; | 135 reading.values[0] = current_lux_; |
124 UpdateSensorReading(reading, true); | 136 UpdateSensorReading(reading, true); |
| 137 return true; |
125 } | 138 } |
126 | 139 |
127 } // namespace device | 140 } // namespace device |
OLD | NEW |