summaryrefslogtreecommitdiffstats
path: root/hal/gonk/GonkSensorsRegistryInterface.h
blob: a9d98d653fbb115b3457af02247cc9f0d7701261 (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
/* -*- 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 registry interface gives yo access to the Sensors daemon's Registry
 * service. The purpose of the service is to register and setup all other
 * services, and make them available.
 *
 * All public methods and callback methods run on the main thread.
 */

#ifndef hal_gonk_GonkSensorsRegistryInterface_h
#define hal_gonk_GonkSensorsRegistryInterface_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
 * Registry interface. Methods always run on the main thread.
 */
class GonkSensorsRegistryResultHandler : public DaemonSocketResultHandler
{
public:

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

  /**
   * The callback method for |GonkSensorsRegistryInterface::RegisterModule|.
   *
   * @param aProtocolVersion The daemon's protocol version. Make sure it's
   *                         compatible with Gecko's implementation.
   */
  virtual void RegisterModule(uint32_t aProtocolVersion);

  /**
   * The callback method for |SensorsRegsitryInterface::UnregisterModule|.
   */
  virtual void UnregisterModule();

protected:
  virtual ~GonkSensorsRegistryResultHandler();
};

/**
 * This is the module class for the Sensors registry 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 |GonkSensorsRegistryInterface| instead.
 */
class GonkSensorsRegistryModule
{
public:
  enum {
    SERVICE_ID = 0x00
  };

  enum {
    OPCODE_ERROR = 0x00,
    OPCODE_REGISTER_MODULE = 0x01,
    OPCODE_UNREGISTER_MODULE = 0x02
  };

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

  //
  // Commands
  //

  nsresult RegisterModuleCmd(uint8_t aId,
                             GonkSensorsRegistryResultHandler* aRes);

  nsresult UnregisterModuleCmd(uint8_t aId,
                               GonkSensorsRegistryResultHandler* aRes);

protected:
  virtual ~GonkSensorsRegistryModule();

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

  //
  // Responses
  //

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

  typedef mozilla::ipc::DaemonResultRunnable1<
    GonkSensorsRegistryResultHandler, void, uint32_t, uint32_t>
    Uint32ResultRunnable;

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

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

  void RegisterModuleRsp(const DaemonSocketPDUHeader& aHeader,
                         DaemonSocketPDU& aPDU,
                         GonkSensorsRegistryResultHandler* aRes);

  void UnregisterModuleRsp(const DaemonSocketPDUHeader& aHeader,
                           DaemonSocketPDU& aPDU,
                           GonkSensorsRegistryResultHandler* aRes);
};

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

  /**
   * Sends a RegisterModule command to the Sensors daemon. When the
   * result handler's |RegisterModule| method gets called, the service
   * has been registered successfully and can be used.
   *
   * @param aId The id of the service that is to be registered.
   * @param aRes The result handler.
   */
  void RegisterModule(uint8_t aId, GonkSensorsRegistryResultHandler* aRes);

  /**
   * Sends an UnregisterModule command to the Sensors daemon. The service
   * should not be used afterwards until it has been registered again.
   *
   * @param aId The id of the service that is to be unregistered.
   * @param aRes The result handler.
   */
  void UnregisterModule(uint8_t aId, GonkSensorsRegistryResultHandler* aRes);

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

  GonkSensorsRegistryModule* mModule;
};

} // namespace hal
} // namespace mozilla

#endif // hal_gonk_GonkSensorsRegistryInterface_h