Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Side by Side Diff: device/generic_sensor/platform_sensor_ambient_light_mac.cc

Issue 2417643006: [sensors][mac] Make sure to update the sensor value when initializing the sensor. (Closed)
Patch Set: Fix Tim comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/generic_sensor/platform_sensor_ambient_light_mac.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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();
timvolodine 2016/10/21 12:51:50 if the callback is executed often would that be a
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 } 59 }
66 60
67 bool PlatformSensorAmbientLightMac::StartSensor( 61 bool PlatformSensorAmbientLightMac::StartSensor(
68 const PlatformSensorConfiguration& configuration) { 62 const PlatformSensorConfiguration& configuration) {
69 // Tested and verified by riju that the following call works on 63 // Tested and verified by riju that the following call works on
70 // MacBookPro9,1 : Macbook Pro 15" (Mid 2012 model) 64 // MacBookPro9,1 : Macbook Pro 15" (Mid 2012 model)
71 // MacBookPro10,1 : Macbook Pro 15" (Retina Display, Early 2013 model). 65 // MacBookPro10,1 : Macbook Pro 15" (Retina Display, Early 2013 model).
72 // MacBookPro10,2 : Macbook Pro 13" (Retina Display, Early 2013 model). 66 // MacBookPro10,2 : Macbook Pro 13" (Retina Display, Early 2013 model).
73 // MacBookAir5,2 : Macbook Air 13" (Mid 2012 model) (by François Beaufort). 67 // MacBookAir5,2 : Macbook Air 13" (Mid 2012 model) (by François Beaufort).
74 // MacBookAir6,2 : Macbook Air 13" (Mid 2013 model). 68 // MacBookAir6,2 : Macbook Air 13" (Mid 2013 model).
(...skipping 18 matching lines...) Expand all
93 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)); 87 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0));
94 88
95 kern_return_t kr = IOServiceAddInterestNotification( 89 kern_return_t kr = IOServiceAddInterestNotification(
96 light_sensor_port_.get(), light_sensor_service_, kIOGeneralInterest, 90 light_sensor_port_.get(), light_sensor_service_, kIOGeneralInterest,
97 IOServiceCallback, this, light_sensor_notification_.InitializeInto()); 91 IOServiceCallback, this, light_sensor_notification_.InitializeInto());
98 if (kr != KERN_SUCCESS) 92 if (kr != KERN_SUCCESS)
99 return false; 93 return false;
100 94
101 kr = IOServiceOpen(light_sensor_service_, mach_task_self(), 0, 95 kr = IOServiceOpen(light_sensor_service_, mach_task_self(), 0,
102 light_sensor_object_.InitializeInto()); 96 light_sensor_object_.InitializeInto());
97 if (kr != KERN_SUCCESS)
98 return false;
103 99
104 return kr == KERN_SUCCESS; 100 bool success = ReadAndUpdate();
101 if (!success)
102 StopSensor();
103
104 return success;
105 } 105 }
106 106
107 void PlatformSensorAmbientLightMac::StopSensor() { 107 void PlatformSensorAmbientLightMac::StopSensor() {
108 light_sensor_port_.reset(); 108 light_sensor_port_.reset();
109 light_sensor_notification_.reset(); 109 light_sensor_notification_.reset();
110 light_sensor_object_.reset(); 110 light_sensor_object_.reset();
timvolodine 2016/10/21 12:51:50 reset light_sensor_service_?
111 current_lux_ = 0.0; 111 current_lux_ = 0.0;
112 } 112 }
113 113
114 void PlatformSensorAmbientLightMac::UpdateReading(uint64_t lux_values[2]) { 114 bool PlatformSensorAmbientLightMac::ReadAndUpdate() {
115 uint32_t scalar_output_count = 2;
116 uint64_t lux_values[2];
117 kern_return_t kr = IOConnectCallMethod(
118 light_sensor_object_, LmuFunctionIndex::kGetSensorReadingID, nullptr, 0,
119 nullptr, 0, lux_values, &scalar_output_count, nullptr, 0);
120
121 if (kr != KERN_SUCCESS)
122 return false;
123
115 uint64_t mean = (lux_values[0] + lux_values[1]) / 2; 124 uint64_t mean = (lux_values[0] + lux_values[1]) / 2;
116 double lux = LMUvalueToLux(mean); 125 double lux = LMUvalueToLux(mean);
117 if (lux == current_lux_) 126 if (lux == current_lux_)
118 return; 127 return true;
119 current_lux_ = lux; 128 current_lux_ = lux;
120 129
121 SensorReading reading; 130 SensorReading reading;
122 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); 131 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
123 reading.values[0] = current_lux_; 132 reading.values[0] = current_lux_;
124 UpdateSensorReading(reading, true); 133 UpdateSensorReading(reading, true);
134 return true;
125 } 135 }
126 136
127 } // namespace device 137 } // namespace device
OLDNEW
« no previous file with comments | « device/generic_sensor/platform_sensor_ambient_light_mac.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698