summaryrefslogtreecommitdiff
path: root/src/components/dbus/include/dbus/dbus_adapter.h
blob: 42a2ac1140e6cff1b08910d2ce63d5b06875ef4e (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 * Copyright (c) 2013-2014, Ford Motor Company
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the Ford Motor Company nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef SRC_COMPONENTS_DBUS_INCLUDE_DBUS_DBUS_ADAPTER_H_
#define SRC_COMPONENTS_DBUS_INCLUDE_DBUS_DBUS_ADAPTER_H_

#include <string>
#include "smart_objects/smart_object.h"
#include "dbus/schema.h"
#include "dbus/message_descriptions.h"

struct DBusConnection;
struct DBusMessage;
struct DBusMessageIter;

namespace dbus {

/**
 * \brief class for work with DBus
 */
class DBusAdapter {
 public:
  /**
   * \brief constructs DBus adapter
   * \param sdlServiceName core service name
   * \param sdlObjectPath core object path
   * \param hmiServiceName hmi service name
   */
  DBusAdapter(const std::string& sdlServiceName,
              const std::string& sdlObjectPath,
              const std::string& hmiServiceName,
              const std::string& hmiObjectPath);

  /**
   * \brief destructs DBus adapter
   */
  virtual ~DBusAdapter();

  /**
   * \brief inits service
   * \return true if success
   */
  bool Init();

  /**
   * \brief return schema messages for DBus
   * \return schema
   */
  const DBusSchema& get_schema() const;

 protected:
  /**
   * \brief calls method on HMI
   * \param id id message
   * \param func_id id function in Ford protocol
   * \param name pair interface and name of method for call
   * \param obj params for call
   */
  void MethodCall(uint id,
                  const MessageId func_id,
                  const MessageName& name,
                  const smart_objects::SmartObject& obj);

  /**
   * \brief sends signal
   * \param id id message
   * \param func_id id function in Ford protocol
   * \param name pair interface and name of signal for call
   * \param obj params for signal
   */
  void Signal(const MessageId func_id,
              const MessageName& name,
              const smart_objects::SmartObject& obj);

  /**
   * \brief returns result of call method to HMI
   * \param id id message
   * \param obj params for return
   */
  void MethodReturn(uint id,
                    const MessageId func_id,
                    const MessageName& name,
                    const smart_objects::SmartObject& obj);

  /**
   * \brief sends error on message from HMI
   * \param id id message
   * \param msg message from HMI
   * \param name name of error
   * \param description description of error
   */
  void Error(uint id, const std::string& name, const std::string& description);

  /**
   * \brief adds a match rule
   * \param rule string rule in the DBus specification
   */
  void AddMatch(const std::string& rule);

  /**
   * \brief processes incoming message from DBus if queue isn't empty
   * and fill obj
   * \param obj object for send to core
   * \return true if message processed
   */
  bool Process(smart_objects::SmartObject& obj);

  /**
   * \brief saves link D-Bus serial to Ford message id
   * \param serial D-Bus message serial
   * \param ids pair correlation id and Ford message id
   */
  inline void SaveRequestToHMI(uint32_t serial,
                               const std::pair<uint, MessageId>& ids);

  /**
   * \brief gets Ford message id by serial
   * \param serial D-Bus message serial
   * \return pair correlation id and Ford message id
   */
  inline std::pair<uint, MessageId> GetRequestToHMI(uint32_t serial);

  /**
   * \brief saves link D-Bus serial to Ford message id
   * \param serial D-Bus message serial
   * \param request D-Bus message from HMI
   */
  inline void SaveRequestFromHMI(uint32_t serial, DBusMessage* request);

  /**
   * \brief gets D-Bus message id by serial
   * \param serial DBus message serial
   * \return D-Bus message from HMI
   */
  inline DBusMessage* GetRequestFromHMI(uint32_t serial);

  std::string sdl_service_name_;
  std::string sdl_object_path_;
  std::string hmi_service_name_;
  std::string hmi_object_path_;
  DBusConnection* conn_;

 private:
  /**
   * \brief schema messages and arguments for DBus
   */
  const DBusSchema* schema_;

  /**
   * \brief mapping serial message DBus on message id Ford protocol
   */
  std::map<uint32_t, std::pair<uint, MessageId> > requests_to_hmi_;

  /**
   * \brief mapping message id Ford protocol on message DBus
   */
  std::map<uint32_t, DBusMessage*> requests_from_hmi_;

  /**
   * \brief processes incoming call of method and fill obj
   * \param msg message from DBus
   * \param obj object for send to core
   * \return true if success
   */
  bool ProcessMethodCall(DBusMessage* msg, smart_objects::SmartObject& obj);

  /**
   * \brief processes incoming return of method and fill obj
   * \param msg message from DBus
   * \param obj object for send to core
   * \return true if success
   */
  bool ProcessMethodReturn(DBusMessage* msg, smart_objects::SmartObject& obj);

  /**
   * \brief processes incoming error and fill obj
   * \param msg message from DBus
   * \param obj object for send to core
   * \return true if success
   */
  bool ProcessError(DBusMessage* msg, smart_objects::SmartObject& obj);

  /**
   * \brief processes incoming signal and fill obj
   * \param msg message from DBus
   * \param obj object for send to core
   * \return true if success
   */
  bool ProcessSignal(DBusMessage* msg, smart_objects::SmartObject& obj);

  /**
   * \brief sets arguments to message
   * \param msg DBus message
   * \param rules list of rules for arguments
   * \param args map of arguments
   * \return true if success
   */
  bool SetArguments(DBusMessage* msg,
                    const ListArgs& rules,
                    const smart_objects::SmartObject& args);

  /**
   * \brief Sets one argument to message
   * \param iter DBus message iterator
   * \param rules description for argument
   * \param param value of argument
   * \return true if success
   */
  bool SetOneArgument(
      DBusMessageIter* iter,
      const ford_message_descriptions::ParameterDescription* rules,
      const smart_objects::SmartObject& param);

  /**
   * \brief sets value for argument
   * \param iter DBus message iterator
   * \param rules description for argument
   * \param param value of argument
   * \return true if success
   */
  bool SetValue(DBusMessageIter* iter,
                const ford_message_descriptions::ParameterDescription* rules,
                const smart_objects::SmartObject& param);

  /**
   * \brief sets value for every element of argument
   * \param iter DBus message iterator
   * \param rules description for argument
   * \param param array values of argument
   * \return true if success
   */
  bool SetArrayValue(DBusMessageIter* iter,
                     const ford_message_descriptions::ArrayDescription* rules,
                     const smart_objects::SmartObject& param);

  /**
   * \brief sets struct value for argument
   * \param iter DBus message iter
   * \param rules description for argument
   * \param param structure
   * \return true if success
   */
  bool SetStructValue(DBusMessageIter* iter,
                      const ford_message_descriptions::StructDescription* rules,
                      const smart_objects::SmartObject& param);

  /**
   * \brief sets optional value for argument.
   * Optional param is struct bool, value
   * \param iter DBus message iterator
   * \param rules description for argument
   * \param param value of optional argument
   * \return true if success
   */
  bool SetOptionalValue(
      DBusMessageIter* iter,
      const ford_message_descriptions::ParameterDescription* rules,
      const smart_objects::SmartObject& param);

  /**
   * \brief gets arguments from message
   * \param iter DBus message iterator
   * \param code response code (output)
   * \param message response message (output)
   * \return true if success
   */
  bool GetHeader(DBusMessageIter* iter, int* code, std::string* message);

  /**
   * \brief gets arguments from message with header
   * \param iter DBus message iterator
   * \param rules list of rules for arguments
   * \param args map of arguments
   * \return true if success
   */
  bool GetArguments(DBusMessageIter* iter,
                    const ListArgs& rules,
                    smart_objects::SmartObject& args);

  /**
   * \brief gets one argument from message
   * \param iter DBus message iterator
   * \param rules description for argument
   * \param args map of arguments
   * \return true if success
   */
  bool GetOneArgument(
      DBusMessageIter* iter,
      const ford_message_descriptions::ParameterDescription* rules,
      smart_objects::SmartObject& args);

  /**
   * \brief gets value for argument
   * \param iter DBus message iterator
   * \param rules description for argument
   * \param param value of argument
   * \return true if success
   */
  bool GetValue(DBusMessageIter* iter,
                const ford_message_descriptions::ParameterDescription* rules,
                smart_objects::SmartObject& param);

  /**
   * \brief gets value for every element of argument
   * \param iter DBus message iterator
   * \param rules description for argument
   * \param param array values of argument
   * \return true if success
   */
  bool GetArrayValue(DBusMessageIter* iter,
                     const ford_message_descriptions::ArrayDescription* rules,
                     smart_objects::SmartObject& param);

  /**
   * \brief gets struct value for argument
   * \param iter DBus message iterator
   * \param rules description for argument
   * \param param structure
   * \return true if success
   */
  bool GetStructValue(DBusMessageIter* iter,
                      const ford_message_descriptions::StructDescription* rules,
                      smart_objects::SmartObject& param);

  /**
   * \brief gets optional value for argument.
   * Optional param is struct bool, value
   * \param iter DBus message iterator
   * \param rules description for argument
   * \param param value of optional argument
   * \return true if success
   */
  bool GetOptionalValue(
      DBusMessageIter* iter,
      const ford_message_descriptions::ParameterDescription* rules,
      smart_objects::SmartObject& param);

  /**
   * \brief processes request on introspect
   * \param msg DBus message
   */
  void Introspect(DBusMessage* msg);
};

}  // namespace dbus

#endif  // SRC_COMPONENTS_DBUS_INCLUDE_DBUS_DBUS_ADAPTER_H_