PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/services/sensorservice/SensorService.h

https://gitlab.com/cde/debian_android-tools_android-platform-frameworks-native
C Header | 253 lines | 124 code | 41 blank | 88 comment | 0 complexity | ce5cf86e8b68985988490fb8880042f7 MD5 | raw file
  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef ANDROID_SENSOR_SERVICE_H
  17. #define ANDROID_SENSOR_SERVICE_H
  18. #include "SensorList.h"
  19. #include "RecentEventLogger.h"
  20. #include <binder/BinderService.h>
  21. #include <cutils/compiler.h>
  22. #include <gui/ISensorServer.h>
  23. #include <gui/ISensorEventConnection.h>
  24. #include <gui/Sensor.h>
  25. #include <utils/AndroidThreads.h>
  26. #include <utils/KeyedVector.h>
  27. #include <utils/Looper.h>
  28. #include <utils/SortedVector.h>
  29. #include <utils/String8.h>
  30. #include <utils/Vector.h>
  31. #include <utils/threads.h>
  32. #include <stdint.h>
  33. #include <sys/types.h>
  34. #include <unordered_map>
  35. #include <unordered_set>
  36. #if __clang__
  37. // Clang warns about SensorEventConnection::dump hiding BBinder::dump. The cause isn't fixable
  38. // without changing the API, so let's tell clang this is indeed intentional.
  39. #pragma clang diagnostic ignored "-Woverloaded-virtual"
  40. #endif
  41. // ---------------------------------------------------------------------------
  42. #define IGNORE_HARDWARE_FUSION false
  43. #define DEBUG_CONNECTIONS false
  44. // Max size is 100 KB which is enough to accept a batch of about 1000 events.
  45. #define MAX_SOCKET_BUFFER_SIZE_BATCHED 100 * 1024
  46. // For older HALs which don't support batching, use a smaller socket buffer size.
  47. #define SOCKET_BUFFER_SIZE_NON_BATCHED 4 * 1024
  48. #define SENSOR_REGISTRATIONS_BUF_SIZE 20
  49. namespace android {
  50. // ---------------------------------------------------------------------------
  51. class SensorInterface;
  52. using namespace SensorServiceUtil;
  53. class SensorService :
  54. public BinderService<SensorService>,
  55. public BnSensorServer,
  56. protected Thread
  57. {
  58. // nested class/struct for internal use
  59. class SensorEventConnection;
  60. public:
  61. void cleanupConnection(SensorEventConnection* connection);
  62. status_t enable(const sp<SensorEventConnection>& connection, int handle,
  63. nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags,
  64. const String16& opPackageName);
  65. status_t disable(const sp<SensorEventConnection>& connection, int handle);
  66. status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns,
  67. const String16& opPackageName);
  68. status_t flushSensor(const sp<SensorEventConnection>& connection,
  69. const String16& opPackageName);
  70. private:
  71. friend class BinderService<SensorService>;
  72. // nested class/struct for internal use
  73. class SensorRecord;
  74. class SensorEventAckReceiver;
  75. struct SensorRegistrationInfo;
  76. enum Mode {
  77. // The regular operating mode where any application can register/unregister/call flush on
  78. // sensors.
  79. NORMAL = 0,
  80. // This mode is only used for testing purposes. Not all HALs support this mode. In this mode,
  81. // the HAL ignores the sensor data provided by physical sensors and accepts the data that is
  82. // injected from the SensorService as if it were the real sensor data. This mode is primarily
  83. // used for testing various algorithms like vendor provided SensorFusion, Step Counter and
  84. // Step Detector etc. Typically in this mode, there will be a client (a
  85. // SensorEventConnection) which will be injecting sensor data into the HAL. Normal apps can
  86. // unregister and register for any sensor that supports injection. Registering to sensors
  87. // that do not support injection will give an error. TODO(aakella) : Allow exactly one
  88. // client to inject sensor data at a time.
  89. DATA_INJECTION = 1,
  90. // This mode is used only for testing sensors. Each sensor can be tested in isolation with
  91. // the required sampling_rate and maxReportLatency parameters without having to think about
  92. // the data rates requested by other applications. End user devices are always expected to be
  93. // in NORMAL mode. When this mode is first activated, all active sensors from all connections
  94. // are disabled. Calling flush() will return an error. In this mode, only the requests from
  95. // selected apps whose package names are whitelisted are allowed (typically CTS apps). Only
  96. // these apps can register/unregister/call flush() on sensors. If SensorService switches to
  97. // NORMAL mode again, all sensors that were previously registered to are activated with the
  98. // corresponding paramaters if the application hasn't unregistered for sensors in the mean
  99. // time. NOTE: Non whitelisted app whose sensors were previously deactivated may still
  100. // receive events if a whitelisted app requests data from the same sensor.
  101. RESTRICTED = 2
  102. // State Transitions supported.
  103. // RESTRICTED <--- NORMAL ---> DATA_INJECTION
  104. // ---> <---
  105. // Shell commands to switch modes in SensorService.
  106. // 1) Put SensorService in RESTRICTED mode with packageName .cts. If it is already in
  107. // restricted mode it is treated as a NO_OP (and packageName is NOT changed).
  108. //
  109. // $ adb shell dumpsys sensorservice restrict .cts.
  110. //
  111. // 2) Put SensorService in DATA_INJECTION mode with packageName .xts. If it is already in
  112. // data_injection mode it is treated as a NO_OP (and packageName is NOT changed).
  113. //
  114. // $ adb shell dumpsys sensorservice data_injection .xts.
  115. //
  116. // 3) Reset sensorservice back to NORMAL mode.
  117. // $ adb shell dumpsys sensorservice enable
  118. };
  119. static const char* WAKE_LOCK_NAME;
  120. static char const* getServiceName() ANDROID_API { return "sensorservice"; }
  121. SensorService() ANDROID_API;
  122. virtual ~SensorService();
  123. virtual void onFirstRef();
  124. // Thread interface
  125. virtual bool threadLoop();
  126. // ISensorServer interface
  127. virtual Vector<Sensor> getSensorList(const String16& opPackageName);
  128. virtual Vector<Sensor> getDynamicSensorList(const String16& opPackageName);
  129. virtual sp<ISensorEventConnection> createSensorEventConnection(
  130. const String8& packageName,
  131. int requestedMode, const String16& opPackageName);
  132. virtual int isDataInjectionEnabled();
  133. virtual status_t dump(int fd, const Vector<String16>& args);
  134. String8 getSensorName(int handle) const;
  135. bool isVirtualSensor(int handle) const;
  136. sp<SensorInterface> getSensorInterfaceFromHandle(int handle) const;
  137. bool isWakeUpSensor(int type) const;
  138. void recordLastValueLocked(sensors_event_t const* buffer, size_t count);
  139. static void sortEventBuffer(sensors_event_t* buffer, size_t count);
  140. const Sensor& registerSensor(SensorInterface* sensor,
  141. bool isDebug = false, bool isVirtual = false);
  142. const Sensor& registerVirtualSensor(SensorInterface* sensor, bool isDebug = false);
  143. const Sensor& registerDynamicSensorLocked(SensorInterface* sensor, bool isDebug = false);
  144. bool unregisterDynamicSensorLocked(int handle);
  145. status_t cleanupWithoutDisable(const sp<SensorEventConnection>& connection, int handle);
  146. status_t cleanupWithoutDisableLocked(const sp<SensorEventConnection>& connection, int handle);
  147. void cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection,
  148. sensors_event_t const* buffer, const int count);
  149. static bool canAccessSensor(const Sensor& sensor, const char* operation,
  150. const String16& opPackageName);
  151. // SensorService acquires a partial wakelock for delivering events from wake up sensors. This
  152. // method checks whether all the events from these wake up sensors have been delivered to the
  153. // corresponding applications, if yes the wakelock is released.
  154. void checkWakeLockState();
  155. void checkWakeLockStateLocked();
  156. bool isWakeLockAcquired();
  157. bool isWakeUpSensorEvent(const sensors_event_t& event) const;
  158. sp<Looper> getLooper() const;
  159. // Reset mWakeLockRefCounts for all SensorEventConnections to zero. This may happen if
  160. // SensorService did not receive any acknowledgements from apps which have registered for
  161. // wake_up sensors.
  162. void resetAllWakeLockRefCounts();
  163. // Acquire or release wake_lock. If wake_lock is acquired, set the timeout in the looper to 5
  164. // seconds and wake the looper.
  165. void setWakeLockAcquiredLocked(bool acquire);
  166. // Send events from the event cache for this particular connection.
  167. void sendEventsFromCache(const sp<SensorEventConnection>& connection);
  168. // Promote all weak referecences in mActiveConnections vector to strong references and add them
  169. // to the output vector.
  170. void populateActiveConnections( SortedVector< sp<SensorEventConnection> >* activeConnections);
  171. // If SensorService is operating in RESTRICTED mode, only select whitelisted packages are
  172. // allowed to register for or call flush on sensors. Typically only cts test packages are
  173. // allowed.
  174. bool isWhiteListedPackage(const String8& packageName);
  175. // Reset the state of SensorService to NORMAL mode.
  176. status_t resetToNormalMode();
  177. status_t resetToNormalModeLocked();
  178. // Transforms the UUIDs for all the sensors into proper IDs.
  179. void makeUuidsIntoIdsForSensorList(Vector<Sensor> &sensorList) const;
  180. // Gets the appropriate ID from the given UUID.
  181. int32_t getIdFromUuid(const Sensor::uuid_t &uuid) const;
  182. // Either read from storage or create a new one.
  183. static bool initializeHmacKey();
  184. static uint8_t sHmacGlobalKey[128];
  185. static bool sHmacGlobalKeyIsValid;
  186. SensorList mSensors;
  187. status_t mInitCheck;
  188. // Socket buffersize used to initialize BitTube. This size depends on whether batching is
  189. // supported or not.
  190. uint32_t mSocketBufferSize;
  191. sp<Looper> mLooper;
  192. sp<SensorEventAckReceiver> mAckReceiver;
  193. // protected by mLock
  194. mutable Mutex mLock;
  195. DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
  196. std::unordered_set<int> mActiveVirtualSensors;
  197. SortedVector< wp<SensorEventConnection> > mActiveConnections;
  198. bool mWakeLockAcquired;
  199. sensors_event_t *mSensorEventBuffer, *mSensorEventScratch;
  200. SensorEventConnection const **mMapFlushEventsToConnections;
  201. std::unordered_map<int, RecentEventLogger*> mRecentEvent;
  202. Mode mCurrentOperatingMode;
  203. // This packagaName is set when SensorService is in RESTRICTED or DATA_INJECTION mode. Only
  204. // applications with this packageName are allowed to activate/deactivate or call flush on
  205. // sensors. To run CTS this is can be set to ".cts." and only CTS tests will get access to
  206. // sensors.
  207. String8 mWhiteListedPackage;
  208. int mNextSensorRegIndex;
  209. Vector<SensorRegistrationInfo> mLastNSensorRegistrations;
  210. };
  211. } // namespace android
  212. #endif // ANDROID_SENSOR_SERVICE_H