platform_sensor_ambient_light_mac.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #include "services/device/generic_sensor/platform_sensor_ambient_light_mac.h"
  5. #include <stdint.h>
  6. #include <IOKit/IOMessage.h>
  7. #include "base/bind.h"
  8. #include "base/time/time.h"
  9. #include "device/base/synchronization/shared_memory_seqlock_buffer.h"
  10. #include "services/device/generic_sensor/generic_sensor_consts.h"
  11. #include "services/device/generic_sensor/platform_sensor_provider_mac.h"
  12. #include "services/device/public/cpp/generic_sensor/sensor_traits.h"
  13. namespace {
  14. // Convert the value returned by the ambient light LMU sensor on Mac
  15. // hardware to a lux value.
  16. double LMUvalueToLux(uint64_t raw_value) {
  17. // Conversion formula from regression.
  18. // https://bugzilla.mozilla.org/show_bug.cgi?id=793728
  19. // Let x = raw_value, then
  20. // lux = -2.978303814*(10^-27)*x^4 + 2.635687683*(10^-19)*x^3 -
  21. // 3.459747434*(10^-12)*x^2 + 3.905829689*(10^-5)*x - 0.1932594532
  22. static const long double k4 = pow(10.L, -7);
  23. static const long double k3 = pow(10.L, -4);
  24. static const long double k2 = pow(10.L, -2);
  25. static const long double k1 = pow(10.L, 5);
  26. long double scaled_value = raw_value / k1;
  27. long double lux_value =
  28. (-3 * k4 * pow(scaled_value, 4)) + (2.6 * k3 * pow(scaled_value, 3)) +
  29. (-3.4 * k2 * pow(scaled_value, 2)) + (3.9 * scaled_value) - 0.19;
  30. double lux = ceil(static_cast<double>(lux_value));
  31. return lux > 0 ? lux : 0;
  32. }
  33. } // namespace
  34. namespace device {
  35. using mojom::SensorType;
  36. enum LmuFunctionIndex {
  37. kGetSensorReadingID = 0, // getSensorReading(int *, int *)
  38. };
  39. PlatformSensorAmbientLightMac::PlatformSensorAmbientLightMac(
  40. SensorReadingSharedBuffer* reading_buffer,
  41. PlatformSensorProvider* provider)
  42. : PlatformSensor(SensorType::AMBIENT_LIGHT, reading_buffer, provider),
  43. light_sensor_port_(nullptr),
  44. current_lux_(0.0) {}
  45. PlatformSensorAmbientLightMac::~PlatformSensorAmbientLightMac() = default;
  46. mojom::ReportingMode PlatformSensorAmbientLightMac::GetReportingMode() {
  47. return mojom::ReportingMode::ON_CHANGE;
  48. }
  49. bool PlatformSensorAmbientLightMac::CheckSensorConfiguration(
  50. const PlatformSensorConfiguration& configuration) {
  51. return configuration.frequency() > 0 &&
  52. configuration.frequency() <=
  53. SensorTraits<SensorType::AMBIENT_LIGHT>::kMaxAllowedFrequency;
  54. }
  55. PlatformSensorConfiguration
  56. PlatformSensorAmbientLightMac::GetDefaultConfiguration() {
  57. PlatformSensorConfiguration default_configuration;
  58. default_configuration.set_frequency(
  59. SensorTraits<SensorType::AMBIENT_LIGHT>::kDefaultFrequency);
  60. return default_configuration;
  61. }
  62. void PlatformSensorAmbientLightMac::IOServiceCallback(void* context,
  63. io_service_t service,
  64. natural_t message_type,
  65. void* message_argument) {
  66. PlatformSensorAmbientLightMac* sensor =
  67. static_cast<PlatformSensorAmbientLightMac*>(context);
  68. if (!sensor->ReadAndUpdate()) {
  69. sensor->NotifySensorError();
  70. sensor->StopSensor();
  71. }
  72. }
  73. bool PlatformSensorAmbientLightMac::StartSensor(
  74. const PlatformSensorConfiguration& configuration) {
  75. // Tested and verified by riju that the following call works on
  76. // MacBookPro9,1 : Macbook Pro 15" (Mid 2012 model)
  77. // MacBookPro10,1 : Macbook Pro 15" (Retina Display, Early 2013 model).
  78. // MacBookPro10,2 : Macbook Pro 13" (Retina Display, Early 2013 model).
  79. // MacBookAir5,2 : Macbook Air 13" (Mid 2012 model) (by François Beaufort).
  80. // MacBookAir6,2 : Macbook Air 13" (Mid 2013 model).
  81. // Testing plans : please download the code and follow the comments :-
  82. // https://gist.github.com/riju/74af8c81a665e412d122/
  83. // and add an entry here about the model and the status returned by the code.
  84. // Look up a registered IOService object whose class is AppleLMUController.
  85. light_sensor_service_.reset(IOServiceGetMatchingService(
  86. kIOMasterPortDefault, IOServiceMatching("AppleLMUController")));
  87. // Return early if the ambient light sensor is not present.
  88. if (!light_sensor_service_)
  89. return false;
  90. light_sensor_port_.reset(IONotificationPortCreate(kIOMasterPortDefault));
  91. if (!light_sensor_port_.is_valid())
  92. return false;
  93. IONotificationPortSetDispatchQueue(
  94. light_sensor_port_.get(),
  95. dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0));
  96. kern_return_t kr = IOServiceAddInterestNotification(
  97. light_sensor_port_.get(), light_sensor_service_, kIOGeneralInterest,
  98. IOServiceCallback, this, light_sensor_notification_.InitializeInto());
  99. if (kr != KERN_SUCCESS)
  100. return false;
  101. kr = IOServiceAddInterestNotification(
  102. light_sensor_port_.get(), light_sensor_service_, kIOBusyInterest,
  103. IOServiceCallback, this,
  104. light_sensor_busy_notification_.InitializeInto());
  105. if (kr != KERN_SUCCESS)
  106. return false;
  107. kr = IOServiceOpen(light_sensor_service_, mach_task_self(), 0,
  108. light_sensor_object_.InitializeInto());
  109. if (kr != KERN_SUCCESS)
  110. return false;
  111. bool success = ReadAndUpdate();
  112. if (!success)
  113. StopSensor();
  114. return success;
  115. }
  116. void PlatformSensorAmbientLightMac::StopSensor() {
  117. light_sensor_port_.reset();
  118. light_sensor_notification_.reset();
  119. light_sensor_busy_notification_.reset();
  120. light_sensor_object_.reset();
  121. light_sensor_service_.reset();
  122. current_lux_ = 0.0;
  123. }
  124. bool PlatformSensorAmbientLightMac::ReadAndUpdate() {
  125. uint32_t scalar_output_count = 2;
  126. uint64_t lux_values[2];
  127. kern_return_t kr = IOConnectCallMethod(
  128. light_sensor_object_, LmuFunctionIndex::kGetSensorReadingID, nullptr, 0,
  129. nullptr, 0, lux_values, &scalar_output_count, nullptr, 0);
  130. if (kr != KERN_SUCCESS)
  131. return false;
  132. uint64_t mean = (lux_values[0] + lux_values[1]) / 2;
  133. double lux = LMUvalueToLux(mean);
  134. if (lux == current_lux_)
  135. return true;
  136. current_lux_ = lux;
  137. SensorReading reading;
  138. reading.als.timestamp =
  139. (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
  140. reading.als.value = current_lux_;
  141. UpdateSharedBufferAndNotifyClients(reading);
  142. return true;
  143. }
  144. } // namespace device