summaryrefslogtreecommitdiffstats
path: root/hal/gonk/GonkSensorsInterface.h
blob: 6e356dc364e6e05204dde8687fe06071bb7be7ff (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 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 sensors interface gives you access to the low-level sensors code
 * in a platform-independent manner. The interfaces in this file allow
 * for starting an stopping the sensors driver. Specific functionality
 * is implemented in sub-interfaces.
 */

#ifndef hal_gonk_GonkSensorsInterface_h
#define hal_gonk_GonkSensorsInterface_h

#include <mozilla/ipc/DaemonSocketConsumer.h>
#include <mozilla/ipc/DaemonSocketMessageHandlers.h>
#include <mozilla/ipc/ListenSocketConsumer.h>
#include <mozilla/UniquePtr.h>
#include "SensorsTypes.h"

namespace mozilla {
namespace ipc {

class DaemonSocket;
class ListenSocket;

}
}

namespace mozilla {
namespace hal {

class GonkSensorsPollInterface;
class GonkSensorsProtocol;
class GonkSensorsRegistryInterface;

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

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

  /**
   * The callback method for |GonkSensorsInterface::Connect|.
   */
  virtual void Connect();

  /**
   * The callback method for |GonkSensorsInterface::Connect|.
   */
  virtual void Disconnect();

protected:
  virtual ~GonkSensorsResultHandler();
};

/**
 * This is the notification-handler interface. Implement this classes
 * methods to handle event and notifications from the sensors daemon.
 * All methods run on the main thread.
 */
class GonkSensorsNotificationHandler
{
public:

  /**
   * This notification is called when the backend code fails
   * unexpectedly. Save state in the high-level code and restart
   * the driver.
   *
   * @param aCrash True is the sensors driver crashed.
   */
  virtual void BackendErrorNotification(bool aCrashed);

protected:
  virtual ~GonkSensorsNotificationHandler();
};

/**
 * This class implements the public interface to the Sensors functionality
 * and driver. Use |GonkSensorsInterface::GetInstance| to retrieve an instance.
 * All methods run on the main thread.
 */
class GonkSensorsInterface final
  : public mozilla::ipc::DaemonSocketConsumer
  , public mozilla::ipc::ListenSocketConsumer
{
public:
  /**
   * Returns an instance of the Sensors backend. This code can return
   * |nullptr| if no Sensors backend is available.
   *
   * @return An instance of |GonkSensorsInterface|.
   */
  static GonkSensorsInterface* GetInstance();

  /**
   * This method sets the notification handler for sensor notifications. Call
   * this method immediately after retreiving an instance of the class, or you
   * won't be able able to receive notifications. You may not free the handler
   * class while the Sensors backend is connected.
   *
   * @param aNotificationHandler An instance of a notification handler.
   */
  void SetNotificationHandler(
    GonkSensorsNotificationHandler* aNotificationHandler);

  /**
   * This method starts the Sensors backend and establishes ad connection
   * with Gecko. This is a multi-step process and errors are signalled by
   * |GonkSensorsNotificationHandler::BackendErrorNotification|. If you see
   * this notification before the connection has been established, it's
   * certainly best to assume the Sensors backend to be not evailable.
   *
   * @param aRes The result handler.
   */
  void Connect(GonkSensorsNotificationHandler* aNotificationHandler,
               GonkSensorsResultHandler* aRes);

  /**
   * This method disconnects Gecko from the Sensors backend and frees
   * the backend's resources. This will invalidate all interfaces and
   * state. Don't use any sensors functionality without reconnecting
   * first.
   *
   * @param aRes The result handler.
   */
  void Disconnect(GonkSensorsResultHandler* aRes);

  /**
   * Returns the Registry interface for the connected Sensors backend.
   *
   * @return An instance of the Sensors Registry interface.
   */
  GonkSensorsRegistryInterface* GetSensorsRegistryInterface();

  /**
   * Returns the Poll interface for the connected Sensors backend.
   *
   * @return An instance of the Sensors Poll interface.
   */
  GonkSensorsPollInterface* GetSensorsPollInterface();

private:
  enum Channel {
    LISTEN_SOCKET,
    DATA_SOCKET
  };

  GonkSensorsInterface();
  ~GonkSensorsInterface();

  void DispatchError(GonkSensorsResultHandler* aRes, SensorsError aError);
  void DispatchError(GonkSensorsResultHandler* aRes, nsresult aRv);

  // Methods for |DaemonSocketConsumer| and |ListenSocketConsumer|
  //

  void OnConnectSuccess(int aIndex) override;
  void OnConnectError(int aIndex) override;
  void OnDisconnect(int aIndex) override;

  nsCString mListenSocketName;
  RefPtr<mozilla::ipc::ListenSocket> mListenSocket;
  RefPtr<mozilla::ipc::DaemonSocket> mDataSocket;
  UniquePtr<GonkSensorsProtocol> mProtocol;

  nsTArray<RefPtr<GonkSensorsResultHandler> > mResultHandlerQ;

  GonkSensorsNotificationHandler* mNotificationHandler;

  UniquePtr<GonkSensorsRegistryInterface> mRegistryInterface;
  UniquePtr<GonkSensorsPollInterface> mPollInterface;
};

} // namespace hal
} // namespace mozilla

#endif // hal_gonk_GonkSensorsInterface_h