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

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: Address Robert comment 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/synchronization/shared_memory_seqlock_buffer.h"
11 #include "device/generic_sensor/platform_sensor_provider_mac.h"
12 #include "device/sensors/public/cpp/device_sensors_consts.h"
13 #include "device/sensors/public/cpp/device_util_mac.h"
14
15 namespace device {
16
17 enum LmuFunctionIndex {
18 kGetSensorReadingID = 0, // getSensorReading(int *, int *)
19 };
20
21 PlatformSensorAmbientLightMac::PlatformSensorAmbientLightMac(
22 mojom::SensorType type,
23 mojo::ScopedSharedBufferMapping mapping,
24 PlatformSensorProvider* provider,
25 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
26 : PlatformSensor(type, std::move(mapping), provider),
27 light_sensor_port_(nullptr),
28 task_runner_(std::move(task_runner)),
29 current_lux_(0) {
30 CHECK(type == mojom::SensorType::AMBIENT_LIGHT);
Robert Sesek 2016/10/06 18:58:53 DCHECK_EQ is probably sufficient since this should
31 }
32
33 PlatformSensorAmbientLightMac::~PlatformSensorAmbientLightMac() = default;
34
35 mojom::ReportingMode PlatformSensorAmbientLightMac::GetReportingMode() {
36 return mojom::ReportingMode::ON_CHANGE;
37 }
38
39 bool PlatformSensorAmbientLightMac::CheckSensorConfiguration(
40 const PlatformSensorConfiguration& configuration) {
41 return configuration.frequency() > 0 &&
42 configuration.frequency() <=
43 mojom::SensorConfiguration::kMaxAllowedFrequency;
44 }
45
46 PlatformSensorConfiguration
47 PlatformSensorAmbientLightMac::GetDefaultConfiguration() {
48 PlatformSensorConfiguration default_configuration;
49 default_configuration.set_frequency(kDefaultAmbientLightFrequencyHz);
50 return default_configuration;
51 }
52
53 void PlatformSensorAmbientLightMac::IOServiceCallback(void* context,
54 io_service_t service,
55 natural_t message_type,
56 void* message_argument) {
57 PlatformSensorAmbientLightMac* sensor =
58 static_cast<PlatformSensorAmbientLightMac*>(context);
59 uint32_t scalar_output_count = 2;
60 uint64_t lux_values[2];
61 kern_return_t kr = IOConnectCallMethod(
62 sensor->light_sensor_object_, LmuFunctionIndex::kGetSensorReadingID,
63 nullptr, 0, nullptr, 0, lux_values, &scalar_output_count, nullptr, 0);
64
65 if (kr == KERN_SUCCESS)
66 sensor->UpdateReading(lux_values);
67 }
68
69 bool PlatformSensorAmbientLightMac::StartSensor(
70 const PlatformSensorConfiguration& configuration) {
71 // Tested and verified by riju that the following call works on
72 // MacBookPro9,1 : Macbook Pro 15" (Mid 2012 model)
73 // MacBookPro10,1 : Macbook Pro 15" (Retina Display, Early 2013 model).
74 // MacBookPro10,2 : Macbook Pro 13" (Retina Display, Early 2013 model).
75 // MacBookAir5,2 : Macbook Air 13" (Mid 2012 model) (by François Beaufort).
76 // MacBookAir6,2 : Macbook Air 13" (Mid 2013 model).
77 // Testing plans : please download the code and follow the comments :-
78 // https://gist.github.com/riju/74af8c81a665e412d122/
79 // and add an entry here about the model and the status returned by the code.
80
81 // Look up a registered IOService object whose class is AppleLMUController.
82 light_sensor_service_.reset(IOServiceGetMatchingService(
83 kIOMasterPortDefault, IOServiceMatching("AppleLMUController")));
84
85 // Return early if the ambient light sensor is not present.
86 if (!light_sensor_service_)
87 return false;
88
89 light_sensor_port_.reset(IONotificationPortCreate(kIOMasterPortDefault));
90 if (!light_sensor_port_.is_valid())
91 return false;
92
93 IONotificationPortSetDispatchQueue(
94 light_sensor_port_.get(),
95 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0));
96
97 kern_return_t kr = IOServiceAddInterestNotification(
98 light_sensor_port_.get(), light_sensor_service_, kIOGeneralInterest,
99 IOServiceCallback, this, light_sensor_notification_.InitializeInto());
100 if (kr != KERN_SUCCESS)
101 return false;
102
103 kr = IOServiceOpen(light_sensor_service_, mach_task_self(), 0,
104 light_sensor_object_.InitializeInto());
105
106 return kr == KERN_SUCCESS;
107 }
108
109 void PlatformSensorAmbientLightMac::StopSensor() {
110 light_sensor_port_.reset();
111 light_sensor_notification_.reset();
112 light_sensor_object_.reset();
113 current_lux_ = 0;
114 }
115
116 void PlatformSensorAmbientLightMac::UpdateReading(uint64_t lux_values[2]) {
117 uint64_t mean = (lux_values[0] + lux_values[1]) / 2;
118 double lux = LMUvalueToLux(mean);
119 if (lux == current_lux_)
120 return;
121 current_lux_ = lux;
122
123 mojom::SensorReading reading;
124 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
125 reading.value1 = current_lux_;
126 UpdateSensorReading(reading);
127
128 task_runner_->PostTask(
129 FROM_HERE,
130 base::Bind(&PlatformSensorAmbientLightMac::NotifySensorReadingChanged,
131 this));
132 }
133
134 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698