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

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: Fixes for 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
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_sensors_consts.h"
11 #include "device/base/device_util_mac.h"
12 #include "device/base/synchronization/shared_memory_seqlock_buffer.h"
13 #include "device/generic_sensor/platform_sensor_provider_mac.h"
14
15 namespace {
16 enum LmuFunctionIndex {
17 kGetSensorReadingID = 0, // getSensorReading(int *, int *)
18 };
19 } // namespace
20
21 namespace device {
22
23 using DeviceLightHardwareBuffer = SharedMemorySeqLockBuffer<double>;
24
25 PlatformSensorAmbientLightMac::PlatformSensorAmbientLightMac(
26 mojom::SensorType type,
27 mojo::ScopedSharedBufferMapping mapping,
28 uint64_t buffer_size,
29 PlatformSensorProvider* provider,
30 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner)
31 : PlatformSensor(type, std::move(mapping), provider),
32 ui_task_runner_(std::move(ui_task_runner)),
33 current_lux_(0) {
34 // Tested and verified by riju that the following call works on
35 // MacBookPro9,1 : Macbook Pro 15" (Mid 2012 model)
36 // MacBookPro10,1 : Macbook Pro 15" (Retina Display, Early 2013 model).
37 // MacBookPro10,2 : Macbook Pro 13" (Retina Display, Early 2013 model).
38 // MacBookAir5,2 : Macbook Air 13" (Mid 2012 model) (by François Beaufort).
39 // MacBookAir6,2 : Macbook Air 13" (Mid 2013 model).
40 // Testing plans : please download the code and follow the comments :-
41 // https://gist.github.com/riju/74af8c81a665e412d122/
42 // and add an entry here about the model and the status returned by the code.
43
44 // Look up a registered IOService object whose class is AppleLMUController.
45 light_sensor_service_.reset(IOServiceGetMatchingService(
46 kIOMasterPortDefault, IOServiceMatching("AppleLMUController")));
47 }
48
49 PlatformSensorAmbientLightMac::~PlatformSensorAmbientLightMac() {
50 if (light_sensor_port_)
51 IONotificationPortDestroy(light_sensor_port_);
Reilly Grant (use Gerrit) 2016/10/04 07:11:27 Consider adding a specialization of ScopedGeneric
52
53 if (light_sensor_object_)
54 IOObjectRelease(light_sensor_object_);
Reilly Grant (use Gerrit) 2016/10/04 07:11:27 Use ScopedIOObject for this field. If you need to
55 }
56
57 mojom::ReportingMode PlatformSensorAmbientLightMac::GetReportingMode() {
58 return mojom::ReportingMode::ON_CHANGE;
59 }
60
61 bool PlatformSensorAmbientLightMac::CheckSensorConfiguration(
62 const PlatformSensorConfiguration& configuration) {
63 return configuration.frequency() > 0 &&
64 configuration.frequency() <=
65 mojom::SensorConfiguration::kMaxAllowedFrequency;
66 }
67
68 PlatformSensorConfiguration
69 PlatformSensorAmbientLightMac::GetDefaultConfiguration() {
70 PlatformSensorConfiguration default_configuration;
71 default_configuration.set_frequency(kDefaultAmbientLightFrequencyHz);
72 return default_configuration;
73 }
74
75 void PlatformSensorAmbientLightMac::IOServiceCallback(void* context,
76 io_service_t service,
77 natural_t message_type,
78 void*) {
Robert Sesek 2016/10/04 15:28:18 All parameters should be named.
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);
Reilly Grant (use Gerrit) 2016/10/04 07:11:27 I would just write this as: if (kr == KERN_SUCCES
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;
Robert Sesek 2016/10/04 15:28:18 What is |object| and is it leaked here?
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