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

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

Issue 2332903002: [sensors] [mac] Implement ambient light sensor for macOS (Closed)
Patch Set: Remove polling thread and use IOServiceAddInterestNotification 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
OLDNEW
(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 "base/bind.h"
8 #include "device/base/device_util_mac.h"
9 #include "device/base/synchronization/shared_memory_seqlock_buffer.h"
10 #include "device/generic_sensor/platform_sensor_provider_mac.h"
11
12 #include <IOKit/IOMessage.h>
Robert Sesek 2016/09/29 22:51:27 This goes after the .h for the .cc, but before the
13
14 namespace {
15 enum LmuFunctionIndex {
16 kGetSensorReadingID = 0, // getSensorReading(int *, int *)
17 };
18 } // namespace
19
20 namespace device {
21
22 using DeviceLightHardwareBuffer = SharedMemorySeqLockBuffer<double>;
23
24 PlatformSensorAmbientLightMac::PlatformSensorAmbientLightMac(
25 mojom::SensorType type,
26 mojo::ScopedSharedBufferMapping mapping,
27 uint64_t buffer_size,
28 PlatformSensorProvider* provider)
29 : PlatformSensor(type, std::move(mapping), provider),
30 ui_task_runner_(base::MessageLoop::current()->task_runner()),
Robert Sesek 2016/09/29 22:51:27 I think this is deprecated MessageLoop method. Als
31 current_lux_(0) {
32 // Tested and verified by riju that the following call works on
33 // MacBookPro9,1 : Macbook Pro 15" (Mid 2012 model)
34 // MacBookPro10,1 : Macbook Pro 15" (Retina Display, Early 2013 model).
35 // MacBookPro10,2 : Macbook Pro 13" (Retina Display, Early 2013 model).
36 // MacBookAir5,2 : Macbook Air 13" (Mid 2012 model) (by François Beaufort).
37 // MacBookAir6,2 : Macbook Air 13" (Mid 2013 model).
38 // Testing plans : please download the code and follow the comments :-
39 // https://gist.github.com/riju/74af8c81a665e412d122/
40 // and add an entry here about the model and the status returned by the code.
41
42 // Look up a registered IOService object whose class is AppleLMUController.
43 light_sensor_service_ = IOServiceGetMatchingService(
44 kIOMasterPortDefault, IOServiceMatching("AppleLMUController"));
45 }
46
47 PlatformSensorAmbientLightMac::~PlatformSensorAmbientLightMac() {
48 if (light_sensor_port_)
49 IONotificationPortDestroy(light_sensor_port_);
50
51 if (light_sensor_service_)
Robert Sesek 2016/09/29 22:51:27 Use ScopedIOObject for these.
52 IOObjectRelease(light_sensor_service_);
53
54 if (light_sensor_object_)
55 IOObjectRelease(light_sensor_object_);
56 }
57
58 mojom::ReportingMode PlatformSensorAmbientLightMac::GetReportingMode() {
59 return mojom::ReportingMode::ON_CHANGE;
60 }
61
62 bool PlatformSensorAmbientLightMac::CheckSensorConfiguration(
63 const PlatformSensorConfiguration& configuration) {
64 return configuration.frequency() > 0 &&
65 configuration.frequency() <=
66 mojom::SensorConfiguration::kMaxAllowedFrequency;
67 }
68
69 PlatformSensorConfiguration
70 PlatformSensorAmbientLightMac::GetDefaultConfiguration() {
71 PlatformSensorConfiguration default_configuration;
72 // Default configuration inherited from content/browser/device_sensors.
73 default_configuration.set_frequency(5);
74 return default_configuration;
75 }
76
77 void PlatformSensorAmbientLightMac::IOServiceCallback(void* context,
78 io_service_t service,
79 natural_t message_type,
80 void*) {
81 PlatformSensorAmbientLightMac* sensor =
82 static_cast<PlatformSensorAmbientLightMac*>(context);
83 uint32_t scalar_output_count = 2;
84 uint64_t lux_values[2];
85 kern_return_t kr = IOConnectCallMethod(
86 sensor->light_sensor_object_, LmuFunctionIndex::kGetSensorReadingID,
87 nullptr, 0, nullptr, 0, lux_values, &scalar_output_count, nullptr, 0);
88
89 if (kr != KERN_SUCCESS)
90 return;
91 sensor->UpdateReading(lux_values);
92 }
93
94 bool PlatformSensorAmbientLightMac::StartSensor(
95 const PlatformSensorConfiguration& configuration) {
96 // Return early if the ambient light sensor is not present.
97 if (!light_sensor_service_)
98 return false;
99
100 light_sensor_port_ = IONotificationPortCreate(kIOMasterPortDefault);
101 if (!light_sensor_port_)
102 return false;
103
104 IONotificationPortSetDispatchQueue(light_sensor_port_,
105 dispatch_get_main_queue());
Robert Sesek 2016/09/29 22:51:27 This will be the UI thread, which is probably unde
106
107 io_object_t object;
108 kern_return_t kr = IOServiceAddInterestNotification(
109 light_sensor_port_, light_sensor_service_, kIOGeneralInterest,
110 IOServiceCallback, this, &object);
111 if (kr != KERN_SUCCESS)
112 return false;
113
114 kr = IOServiceOpen(light_sensor_service_, mach_task_self(), 0,
115 &light_sensor_object_);
116 current_lux_ = 0;
117 return kr == KERN_SUCCESS;
118 }
119
120 void PlatformSensorAmbientLightMac::StopSensor() {
121 if (light_sensor_port_)
122 IONotificationPortDestroy(light_sensor_port_);
123
124 if (light_sensor_object_)
125 IOObjectRelease(light_sensor_object_);
126
127 light_sensor_object_ = IO_OBJECT_NULL;
128 light_sensor_port_ = nullptr;
129 }
130
131 void PlatformSensorAmbientLightMac::UpdateReading(uint64_t lux_values[2]) {
132 uint64_t mean = (lux_values[0] + lux_values[1]) / 2;
133 double lux = LMUvalueToLux(mean);
134 if (lux == current_lux_)
135 return;
136 current_lux_ = lux;
137 DeviceLightHardwareBuffer* light_buffer =
138 static_cast<DeviceLightHardwareBuffer*>(shared_buffer_mapping_.get());
139 light_buffer->seqlock.WriteBegin();
140 light_buffer->data = lux;
141 light_buffer->seqlock.WriteEnd();
142 ui_task_runner_->PostTask(
143 FROM_HERE,
144 base::Bind(&PlatformSensorAmbientLightMac::NotifySensorReadingChanged,
145 this));
146 }
147
148 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698