Adapt the AIL filter used to new ones
[profile/ivi/ico-uxf-homescreen.git] / lib / system-controller / CicoSCCommand.cpp
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 //==========================================================================
11 /**
12  *  @file   CicoSCCommand.cpp
13  *
14  *  @brief  This file is implementation of CicoSCCommand class
15  */
16 //==========================================================================
17
18 #include <iostream>
19 #include <sstream>
20 #include <exception>
21 #include <boost/property_tree/ptree.hpp>
22 #include <boost/property_tree/json_parser.hpp>
23 #include <boost/foreach.hpp>
24 #include <boost/optional.hpp>
25 using namespace std;
26 using namespace boost;
27 using namespace boost::property_tree;
28
29 #include "CicoSCCommand.h"
30 #include "CicoLog.h"
31 #include "ico_syc_error.h"
32 #include "ico_syc_msg_cmd_def.h"
33 #include "ico_syc_mrp_resource_private.h"
34
35 //--------------------------------------------------------------------------
36 /**
37  *  @brief  default constructor
38  */
39 //--------------------------------------------------------------------------
40 CicoSCCommand::CicoSCCommand()
41     : cmdid(0), appid(""), pid(-1), opt(NULL)
42 {
43 }
44
45 //--------------------------------------------------------------------------
46 /**
47  *  @brief  destructor
48  */
49 //--------------------------------------------------------------------------
50 CicoSCCommand::~CicoSCCommand()
51 {
52     delete opt;
53 }
54
55 //--------------------------------------------------------------------------
56 /**
57  *  @brief  parse from message, create command
58  *
59  *  @param [in] message     message of interprocess communication
60  *
61  *  @note
62  *  message format(json format)
63  *  key name of "appid", "command", "pid" is common command propety.
64  *  <pre>
65  *  {
66  *    "appid":   (string) application id,
67  *    "command": (int)    command id,
68  *    "pid":     (int)    process id
69  *    ...
70  *    }
71  *  }
72  *  </pre>
73  */
74 //--------------------------------------------------------------------------
75 int
76 CicoSCCommand::parseMessage(const char *message)
77 {
78 //    ICO_TRA("CicoSCCommand::parseMessage Enter(%s)", message);
79     try {
80         stringstream jsonString(message);
81         ptree root;
82         read_json(jsonString, root);
83
84         this->appid = getStrValue(root, MSG_PRMKEY_APPID);
85         this->cmdid = getIntValue(root, MSG_PRMKEY_CMD);
86         this->pid   = getIntValue(root, MSG_PRMKEY_PID);
87
88         switch ((this->cmdid & MSG_CMD_TYPE_MASK)) {
89         case MSG_CMD_TYPE_WINCTRL:
90             parseWinCtrlOpt(root);
91             break;
92         case MSG_CMD_TYPE_INPUTCTRL:
93             parseInputDevCtrlOpt(root);
94             break;
95         case MSG_CMD_TYPE_USERMGR:
96             parseUserMgrOpt(root);
97             break;
98         case MSG_CMD_TYPE_RESOURCEMGR:
99             parseResCtrlOpt(root);
100             break;
101         case MSG_CMD_TYPE_INPUTDEVSETTING:
102             parseInputDevSettingOpt(root);
103             break;
104         default:
105             break;
106         }
107
108         // dump log command variables
109         dump();
110     }
111     catch (std::exception const& e)
112     {
113         ICO_WRN("catch exception %s", e.what());
114 //        ICO_TRA("CicoSCCommand::parseMessage Leave(EINVAL)");
115         return ICO_SYC_EINVAL;
116     }
117
118 //    ICO_TRA("CicoSCCommand::parseMessage Leave(EOK)");
119     return ICO_SYC_EOK;
120 }
121
122 //--------------------------------------------------------------------------
123 /**
124  *  @brief  dump log this class member variables
125  */
126 //--------------------------------------------------------------------------
127 void
128 CicoSCCommand::dump(void)
129 {
130     ICO_INF("Command: cmdid=0x%08x appid=%s pid=%d", cmdid, appid.c_str(), pid);
131     if (NULL != this->opt) {
132         this->opt->dump();
133     }
134 }
135
136 //--------------------------------------------------------------------------
137 /**
138  *  @brief  get integer value by key
139  *
140  *  @param [in] root    root object of json object
141  *  @param [in] key     Key to get the value
142  *
143  *  @return integer value on success, INT_MAX on error
144  */
145 //--------------------------------------------------------------------------
146 int
147 CicoSCCommand::getIntValue(const ptree & root, const char* key)
148 {
149     int intValue = -1;
150     try {
151         intValue = root.get<int>(key);
152     }
153     catch (std::exception const& e)
154     {
155         //ICO_WRN("catch exception %s", e.what());
156         return intValue;
157     }
158     return intValue;
159 }
160
161 //--------------------------------------------------------------------------
162 /**
163  *  @brief  get string value by key
164  *
165  *  @param [in] root    root object of json object
166  *  @param [in] key     Key to get the value
167  *
168  *  @return string value on success, empty string on error
169  */
170 //--------------------------------------------------------------------------
171 string
172 CicoSCCommand::getStrValue(const ptree & root, const char* key)
173 {
174     string strValue = "";
175     try {
176         strValue = root.get<string>(key);
177     }
178     catch (std::exception const& e)
179     {
180         //ICO_WRN("catch exception %s", e.what());
181         return strValue;
182     }
183
184     if (strValue == "null") {
185         strValue = "";
186     }
187
188     return strValue;
189 }
190
191 //--------------------------------------------------------------------------
192 /**
193  *  @brief  query whether the object exists
194  *
195  *  @param [in] root    root object of json object
196  *  @param [in] key     Key to get the value
197  *
198  *  @return true on exist, false on not exist
199  */
200 //--------------------------------------------------------------------------
201 bool
202 CicoSCCommand::isExistObject(const ptree & root, const char *key)
203 {
204     try {
205         (void)root.get_child(key);
206     }
207     catch (std::exception const& e)
208     {
209         //ICO_WRN("catch exception %s", e.what());
210         return false;
211     }
212
213     return true;
214 }
215
216 //--------------------------------------------------------------------------
217 /**
218  *  @brief  parse from message, create window control options
219  *
220  *  @param [in] root    root object of json object
221  *
222  *  @note
223  *  message format of window control(json format)
224  *  <pre>
225  *  {
226  *    "arg": {
227  *      "surface":   (int)    surface id
228  *      "layer":     (int)    layer id
229  *      "node":      (int)    node id
230  *      "zone":      (string) display zone name
231  *      "anim_name": (string) animation name
232  *      "anim_time": (int)    animation time
233  *      "pos_x":     (int)    window x position
234  *      "pos_y":     (int)    window y position
235  *      "width":     (int)    window width
236  *      "height":    (int)    window height
237  *      "raise":     (int)    raise flag
238  *      "visible":   (int)    visible flag
239  *      "active":    (int)    active flag
240  *      "stride":    (int)    stride
241  *      "format":    (int)    format
242  *      "framerate": (int)    frame rate
243  *    }
244  *  }
245  *  </pre>
246  */
247 //--------------------------------------------------------------------------
248 void
249 CicoSCCommand::parseWinCtrlOpt(const ptree & root)
250 {
251     CicoSCCmdWinCtrlOpt* options = new CicoSCCmdWinCtrlOpt();
252     this->opt = options;
253
254     options->surfaceid     = getIntValue(root, "arg.surface");
255     options->layerid       = getIntValue(root, "arg.layer");
256     options->nodeid        = getIntValue(root, "arg.node");
257     options->zone          = getStrValue(root, "arg.zone");
258     options->animation     = getStrValue(root, "arg.anim_name");
259     options->animationType = getIntValue(root, "arg.anim_type");
260     if (-1 == options->animationType) {
261         options->animationType = 0xff;
262     }
263     options->animationTime = getIntValue(root, "arg.anim_time");
264     if (-1 == options->animationTime) {
265         options->animationTime = 0;
266     }
267     options->x             = getIntValue(root, "arg.pos_x");
268     options->y             = getIntValue(root, "arg.pos_y");
269     options->width         = getIntValue(root, "arg.width");
270     options->height        = getIntValue(root, "arg.height");
271     options->raise         = getIntValue(root, "arg.raise");
272     options->visible       = getIntValue(root, "arg.visible");
273     options->active        = getIntValue(root, "arg.active");
274     options->stride        = getIntValue(root, "arg.stride");
275     options->format        = getIntValue(root, "arg.format");
276     options->framerate     = getIntValue(root, "arg.framerate");
277 }
278
279 //--------------------------------------------------------------------------
280 /**
281  *  @brief  parse from message, create input device control options
282  *
283  *  @param [in] root    root object of json object
284  *
285  *  @note
286  *  message format of input device control(json format)
287  *  <pre>
288  *  {
289  *    "arg": {
290  *      "device":     (string) device name
291  *      "input_num":  (int) input number
292  *      "surface":    (int) surface id
293  *      "deviceno":   (int) device number
294  *      "alloc_type": (int) allocation type
295  *      "keycode":    (int) key code
296  *      "ev_type":    (int) event type
297  *      "ev_time":    (int) event time
298  *      "ev_code":    (int) event cord
299  *      "ev_value":   (int) event value
300  *    }
301  *  }
302  *  </pre>
303  */
304 //--------------------------------------------------------------------------
305 void
306 CicoSCCommand::parseInputDevCtrlOpt(const ptree & root)
307 {
308     CicoSCCmdInputDevCtrlOpt* options = new CicoSCCmdInputDevCtrlOpt();
309     this->opt = options;
310
311     options->device    = getStrValue(root, "arg.device");
312     options->input     = getIntValue(root, "arg.input_num");
313     options->surfaceid = getIntValue(root, "arg.surface");
314     options->deviceno  = getIntValue(root, "arg.deviceno");
315     options->fix       = getIntValue(root, "arg.alloc_type");
316     options->keycode   = getIntValue(root, "arg.keycode");
317     options->evtype    = getIntValue(root, "arg.ev_type");
318     options->evtime    = getIntValue(root, "arg.ev_time");
319     options->evcode    = getIntValue(root, "arg.ev_code");
320     options->evvalue   = getIntValue(root, "arg.ev_value");
321 }
322
323 //--------------------------------------------------------------------------
324 /**
325  *  @brief  parse from message, create user manager options
326  *
327  *  @param [in] root    root object of json object
328  *
329  *  @note
330  *  message format of user manager options(json format)
331  *  <pre>
332  *  {
333  *    "arg": {
334  *      "user":     (string) user name
335  *      "password": (string) user password
336  *      "lastinof": (string) information of application last state
337  *    }
338  *  }
339  *  </pre>
340  */
341 //--------------------------------------------------------------------------
342 void
343 CicoSCCommand::parseUserMgrOpt(const ptree & root)
344 {
345     CicoSCCmdUserMgrOpt* options = new CicoSCCmdUserMgrOpt();
346     this->opt = options;
347
348     options->user     = getStrValue(root, "arg.user");
349     options->pass     = getStrValue(root, "arg.password");
350     options->lastinfo = getStrValue(root, "arg.lastinfo");
351 }
352
353 //--------------------------------------------------------------------------
354 /**
355  *  @brief  parse from message, create resource manage options
356  *
357  *  @param [in] root    root object of json object
358  *
359  *  @note
360  *  message format of resource manage options(json format)
361  *  <pre>
362  *  {
363  *    "res": {
364  *      "window": {
365  *        "zone":    (string) window zone,
366  *        "name":    (string) window's surface name,
367  *        "id":      (string) window id
368  *        "ECU":
369  *        "display":
370  *        "layer":
371  *        "layout":
372  *        "area":
373  *        "dispatchApp":
374  *        "role":
375  *        "resourceId":
376  *      },
377  *      "sound": {
378  *        "zone":    (string) sound zone,
379  *        "name":    (string) sound's stream name,
380  *        "id":      (string) sound id,
381  *        "adjust":  (int)    sound adjust type
382  *      },
383  *      "input": {
384  *        "name":    (string) input device name,
385  *        "event":   (int)    input event id
386  *      }
387  *      "type":      (int)    basic or interruption
388  *    }
389  *  }
390  *  </pre>
391  */
392 //--------------------------------------------------------------------------
393 void
394 CicoSCCommand::parseResCtrlOpt(const ptree & root)
395 {
396     CicoSCCmdResCtrlOpt* options = new CicoSCCmdResCtrlOpt();
397     this->opt = options;
398
399     if (true == isExistObject(root, "res.window")) {
400         options->dispres   = true;
401         options->dispzone  = getStrValue(root, "res.window.zone");
402         options->winname   = getStrValue(root, "res.window.name");
403         options->surfaceid = getIntValue(root, "res.window.id");
404         bool bEx = true;
405         options->ECU       = getStrValue(root, "res.window.ECU");
406         if (true == options->ECU.empty()) {
407             bEx = false;
408         }
409         options->display   = getStrValue(root, "res.window.display");
410         if (true == options->display.empty()) {
411             bEx = false;
412         }
413         options->layer     = getStrValue(root, "res.window.layer");
414         if (true == options->layer.empty()) {
415             bEx = false;
416         }
417         options->layout    = getStrValue(root, "res.window.layout");
418         if (true == options->layout.empty()) {
419             bEx = false;
420         }
421         options->area      = getStrValue(root, "res.window.area");
422         if (true == options->area.empty()) {
423             bEx = false;
424         }
425         options->dispatchApp= getStrValue(root, "res.window.dispatchApp");
426         options->role      = getStrValue(root, "res.window.role");
427         options->resourceID= getIntValue(root, "res.window.resourceId");
428         options->dispresEx = bEx;
429     }
430
431     if (true == isExistObject(root, "res.sound")) {
432         options->soundres  = true;
433         options->soundzone = getStrValue(root, "res.sound.zone");
434         options->soundname = getStrValue(root, "res.sound.name");
435         options->soundid   = getIntValue(root, "res.sound.id");
436         options->adjust    = getIntValue(root, "res.sound.adjust");
437     }
438
439     if (true == isExistObject(root, "res.input")) {
440         options->inputres = true;
441         options->device   = getStrValue(root, "res.input.name");
442         options->input    = getIntValue(root, "res.input.event");
443     }
444
445     options->type = getIntValue(root, "res.type");
446 }
447
448 //--------------------------------------------------------------------------
449 /**
450  *  @brief  parse from message, create input device setting options
451  *
452  *  @param [in] root    root object of json object
453  *
454  *  @note
455  *  message format of input device setting(json format)
456  *  <pre>
457  *  {
458  *    "region": {
459  *      "winname":      (string) window name,
460  *      "pos_x":        (int) position x,
461  *      "pos_y":        (int) position y,
462  *      "width":        (int) width,
463  *      "height":       (int) height,
464  *      "hotspot_x":    (int) cursor hotspot x,
465  *      "hotspot_y":    (int) cursor hotspot y,
466  *      "cursor_x":     (int) cursor position x,
467  *      "cursor_y":     (int) cursor position y,
468  *      "cursor_width": (int) cursor width,
469  *      "cursor_height":(int) cursor height,
470  *      "attr":         (int) attribute
471  *    }
472  *  }
473  *  </pre>
474  */
475 //--------------------------------------------------------------------------
476 void
477 CicoSCCommand::parseInputDevSettingOpt(const ptree & root)
478 {
479     CicoSCCmdInputDevSettingOpt* options = new CicoSCCmdInputDevSettingOpt();
480     this->opt = options;
481
482     if (true == isExistObject(root, "region")) {
483         options->winname    = getStrValue(root, "region.winname");
484         options->x          = getIntValue(root, "region.pos_x");
485         options->y          = getIntValue(root, "region.pos_y");
486         options->width      = getIntValue(root, "region.width");
487         options->height     = getIntValue(root, "region.height");
488         options->hotspot_x  = getIntValue(root, "region.hotspot_x");
489         options->hotspot_y  = getIntValue(root, "region.hotspot_y");
490         options->cursor_x   = getIntValue(root, "region.cursor_x");
491         options->cursor_y   = getIntValue(root, "region.cursor_y");
492         options->cursor_width  = getIntValue(root, "region.cursor_width");
493         options->cursor_height = getIntValue(root, "region.cursor_height");
494         options->attr       = getIntValue(root, "region.attr");
495     }
496 }
497 // vim:set expandtab ts=4 sw=4: