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

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

Powered by Google App Engine
This is Rietveld 408576698