summaryrefslogtreecommitdiffstats
path: root/hal/gonk/GonkSensorsPollInterface.h
blob: 89381a9bdb2072e31c8f57b2b4f972c12ff64457 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * The poll interface gives yo access to the Sensors daemon's Poll service,
 * which handles sensors. The poll service will inform you when sensors are
 * detected or removed from the system. You can activate (or deactivate)
 * existing sensors and poll will deliver the sensors' events.
 *
 * All public methods and callback methods run on the main thread.
 */

#ifndef hal_gonk_GonkSensorsPollInterface_h
#define hal_gonk_GonkSensorsPollInterface_h

#include <mozilla/ipc/DaemonRunnables.h>
#include <mozilla/ipc/DaemonSocketMessageHandlers.h>
#include "SensorsTypes.h"

namespace mozilla {
namespace ipc {

class DaemonSocketPDU;
class DaemonSocketPDUHeader;

}
}

namespace mozilla {
namespace hal {

class SensorsInterface;

using mozilla::ipc::DaemonSocketPDU;
using mozilla::ipc::DaemonSocketPDUHeader;
using mozilla::ipc::DaemonSocketResultHandler;

/**
 * This class is the result-handler interface for the Sensors
 * Poll interface. Methods always run on the main thread.
 */
class GonkSensorsPollResultHandler : public DaemonSocketResultHandler
{
public:

  /**
   * Called if a poll command failed.
   *
   * @param aError The error code.
   */
  virtual void OnError(SensorsError aError);

  /**
   * The callback method for |GonkSensorsPollInterface::EnableSensor|.
   */
  virtual void EnableSensor();

  /**
   * The callback method for |GonkSensorsPollInterface::DisableSensor|.
   */
  virtual void DisableSensor();

  /**
   * The callback method for |GonkSensorsPollInterface::SetPeriod|.
   */
  virtual void SetPeriod();

protected:
  virtual ~GonkSensorsPollResultHandler();
};

/**
 * This is the notification-handler interface. Implement this classes
 * methods to handle event and notifications from the sensors daemon.
 */
class GonkSensorsPollNotificationHandler
{
public:

  /**
   * The notification handler for errors. You'll receive this call if
   * there's been a critical error in the daemon. Either try to handle
   * the error, or restart the daemon.
   *
   * @param aError The error code.
   */
  virtual void ErrorNotification(SensorsError aError);

  /**
   * This methods gets call when a new sensor has been detected.
   *
   * @param aId The sensor's id.
   * @param aType The sensor's type.
   * @param aRange The sensor's maximum value.
   * @param aResolution The minimum difference between two consecutive values.
   * @param aPower The sensor's power consumption (in mA).
   * @param aMinPeriod The minimum time between two events (in ns).
   * @param aMaxPeriod The maximum time between two events (in ns).
   * @param aTriggerMode The sensor's mode for triggering events.
   * @param aDeliveryMode The sensor's urgency for event delivery.
   */
  virtual void SensorDetectedNotification(int32_t aId, SensorsType aType,
                                          float aRange, float aResolution,
                                          float aPower, int32_t aMinPeriod,
                                          int32_t aMaxPeriod,
                                          SensorsTriggerMode aTriggerMode,
                                          SensorsDeliveryMode aDeliveryMode);

  /**
   * This methods gets call when an existing sensor has been removed.
   *
   * @param aId The sensor's id.
   */
  virtual void SensorLostNotification(int32_t aId);

  /**
   * This is the callback methods for sensor events. Only activated sensors
   * generate events. All sensors are disabled by default. The actual data
   * of the event depends on the sensor type.
   *
   * @param aId The sensor's id.
   * @param aEvent The event's data.
   */
  virtual void EventNotification(int32_t aId, const SensorsEvent& aEvent);

protected:
  virtual ~GonkSensorsPollNotificationHandler();
};

/**
 * This is the module class for the Sensors poll component. It handles PDU
 * packing and unpacking. Methods are either executed on the main thread or
 * the I/O thread.
 *
 * This is an internal class, use |GonkSensorsPollInterface| instead.
 */
class GonkSensorsPollModule
{
public:
  class NotificationHandlerWrapper;

  enum {
    SERVICE_ID = 0x01
  };

  enum {
    OPCODE_ERROR = 0x00,
    OPCODE_ENABLE_SENSOR = 0x01,
    OPCODE_DISABLE_SENSOR = 0x02,
    OPCODE_SET_PERIOD = 0x03
  };

  enum {
    MIN_PROTOCOL_VERSION = 1,
    MAX_PROTOCOL_VERSION = 1
  };

  virtual nsresult Send(DaemonSocketPDU* aPDU,
                        DaemonSocketResultHandler* aRes) = 0;

  nsresult SetProtocolVersion(unsigned long aProtocolVersion);

  //
  // Commands
  //

  nsresult EnableSensorCmd(int32_t aId,
                           GonkSensorsPollResultHandler* aRes);

  nsresult DisableSensorCmd(int32_t aId,
                            GonkSensorsPollResultHandler* aRes);

  nsresult SetPeriodCmd(int32_t aId, uint64_t aPeriod,
                        GonkSensorsPollResultHandler* aRes);

protected:
  GonkSensorsPollModule();
  virtual ~GonkSensorsPollModule();

  void HandleSvc(const DaemonSocketPDUHeader& aHeader,
                 DaemonSocketPDU& aPDU,
                 DaemonSocketResultHandler* aRes);

private:

  //
  // Responses
  //

  typedef mozilla::ipc::DaemonResultRunnable0<
    GonkSensorsPollResultHandler, void>
    ResultRunnable;

  typedef mozilla::ipc::DaemonResultRunnable1<
    GonkSensorsPollResultHandler, void, SensorsError, SensorsError>
    ErrorRunnable;

  void ErrorRsp(const DaemonSocketPDUHeader& aHeader,
                DaemonSocketPDU& aPDU,
                GonkSensorsPollResultHandler* aRes);

  void EnableSensorRsp(const DaemonSocketPDUHeader& aHeader,
                       DaemonSocketPDU& aPDU,
                       GonkSensorsPollResultHandler* aRes);

  void DisableSensorRsp(const DaemonSocketPDUHeader& aHeader,
                        DaemonSocketPDU& aPDU,
                        GonkSensorsPollResultHandler* aRes);

  void SetPeriodRsp(const DaemonSocketPDUHeader& aHeader,
                    DaemonSocketPDU& aPDU,
                    GonkSensorsPollResultHandler* aRes);

  void HandleRsp(const DaemonSocketPDUHeader& aHeader,
                 DaemonSocketPDU& aPDU,
                 DaemonSocketResultHandler* aRes);

  //
  // Notifications
  //

  typedef mozilla::ipc::DaemonNotificationRunnable1<
    NotificationHandlerWrapper, void, SensorsError>
    ErrorNotification;

  typedef mozilla::ipc::DaemonNotificationRunnable9<
    NotificationHandlerWrapper, void, int32_t, SensorsType,
    float, float, float, int32_t, int32_t, SensorsTriggerMode,
    SensorsDeliveryMode>
    SensorDetectedNotification;

  typedef mozilla::ipc::DaemonNotificationRunnable1<
    NotificationHandlerWrapper, void, int32_t>
    SensorLostNotification;

  typedef mozilla::ipc::DaemonNotificationRunnable2<
    NotificationHandlerWrapper, void, int32_t, SensorsEvent, int32_t,
    const SensorsEvent&>
    EventNotification;

  class SensorDetectedInitOp;
  class SensorLostInitOp;
  class EventInitOp;

  void ErrorNtf(const DaemonSocketPDUHeader& aHeader,
                DaemonSocketPDU& aPDU);

  void SensorDetectedNtf(const DaemonSocketPDUHeader& aHeader,
                         DaemonSocketPDU& aPDU);

  void SensorLostNtf(const DaemonSocketPDUHeader& aHeader,
                     DaemonSocketPDU& aPDU);

  void EventNtf(const DaemonSocketPDUHeader& aHeader,
                DaemonSocketPDU& aPDU);

  void HandleNtf(const DaemonSocketPDUHeader& aHeader,
                 DaemonSocketPDU& aPDU,
                 DaemonSocketResultHandler* aRes);

private:
  unsigned long mProtocolVersion;
};

/**
 * This class implements the public interface to the Sensors poll
 * component. Use |SensorsInterface::GetPollInterface| to retrieve
 * an instance. All methods run on the main thread.
 */
class GonkSensorsPollInterface final
{
public:
  GonkSensorsPollInterface(GonkSensorsPollModule* aModule);
  ~GonkSensorsPollInterface();

  /**
   * This method sets the notification handler for poll notifications. Call
   * this method immediately after registering the module. Otherwise you won't
   * be able able to receive poll notifications. You may not free the handler
   * class while the poll component is regsitered.
   *
   * @param aNotificationHandler An instance of a poll notification handler.
   */
  void SetNotificationHandler(
    GonkSensorsPollNotificationHandler* aNotificationHandler);

  /**
   * This method sets the protocol version. You should set it to the
   * value that has been returned from the backend when registering the
   * Poll service. You cannot send or receive messages before setting
   * the protocol version.
   *
   * @param aProtocolVersion
   * @return NS_OK for supported versions, or an XPCOM error code otherwise.
   */
  nsresult SetProtocolVersion(unsigned long aProtocolVersion);

  /**
   * Enables an existing sensor. The sensor id will have been delivered in
   * a SensorDetectedNotification.
   *
   * @param aId The sensor's id.
   * @param aRes The result handler.
   */
  void EnableSensor(int32_t aId, GonkSensorsPollResultHandler* aRes);

  /**
   * Disables an existing sensor. The sensor id will have been delivered in
   * a SensorDetectedNotification.
   *
   * @param aId The sensor's id.
   * @param aRes The result handler.
   */
  void DisableSensor(int32_t aId, GonkSensorsPollResultHandler* aRes);

  /**
   * Sets the period for a sensor. The sensor id will have been delivered in
   * a SensorDetectedNotification. The value for the period should be between
   * the sensor's minimum and maximum period.
   *
   * @param aId The sensor's id.
   * @param aPeriod The sensor's new period.
   * @param aRes The result handler.
   */
  void SetPeriod(int32_t aId, uint64_t aPeriod, GonkSensorsPollResultHandler* aRes);

private:
  void DispatchError(GonkSensorsPollResultHandler* aRes, SensorsError aError);
  void DispatchError(GonkSensorsPollResultHandler* aRes, nsresult aRv);

  GonkSensorsPollModule* mModule;
};

} // hal
} // namespace mozilla

#endif // hal_gonk_GonkSensorsPollInterface_h