Adapt the AIL filter used to new ones
[profile/ivi/ico-uxf-homescreen.git] / lib / system-controller / CicoSCMessage.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   CicoSCMessage.cpp
13  *
14  *  @brief  This file implementation of CicoSCMessage class
15  */
16 //==========================================================================
17
18 #include <string>
19 #include <limits.h>
20
21 #include "CicoSCMessage.h"
22 #include "CicoLog.h"
23
24 //==========================================================================    
25 //  private static variable
26 //==========================================================================    
27 unsigned int CicoSCMessage::ms_counter = 0;
28
29 //--------------------------------------------------------------------------
30 /**
31  *  @brief  default constructor
32  */
33 //--------------------------------------------------------------------------
34 CicoSCMessage::CicoSCMessage()
35 {
36     if (ms_counter == UINT_MAX) {
37         ms_counter = 0;
38     }
39     m_id = ++ms_counter;
40     m_generator = json_generator_new();
41     m_root      = json_node_new(JSON_NODE_OBJECT);
42     m_rootObj   = json_object_new();
43     m_argObj    = json_object_new();
44     m_array     = json_array_new();
45     m_data.clear();
46 }
47
48 //--------------------------------------------------------------------------
49 /**
50  *  @brief  destructor
51  */
52 //--------------------------------------------------------------------------
53 CicoSCMessage::~CicoSCMessage()
54 {
55     //ICO_TRA("CicoSCMessage::~CicoSCMessage Enter");
56     json_array_unref(m_array);
57     json_object_unref(m_rootObj);
58     json_object_unref(m_argObj);
59     json_node_free(m_root);
60     g_object_unref(m_generator);
61     //ICO_TRA("CicoSCMessage::~CicoSCMessage Leave");
62 }
63
64 //--------------------------------------------------------------------------
65 /**
66  *  @brief  get message id
67  *
68  *  @return id of message
69  */
70 //--------------------------------------------------------------------------
71 unsigned int
72 CicoSCMessage::getId(void)
73 {
74     return m_id;
75 }
76
77 //--------------------------------------------------------------------------
78 /**
79  *  @brief  add root object for char pointer type
80  *
81  *  @param [in] key     key name of object
82  *  @param [in] value   value of object for char pointer type
83  */
84 //--------------------------------------------------------------------------
85 void
86 CicoSCMessage::addRootObject(const char * key, const char * value)
87 {
88 //    ICO_DBG("json_object_set_string_member(%s, %s) called.", key, value);
89     json_object_set_string_member(m_rootObj, key, value);
90 }
91
92 //--------------------------------------------------------------------------
93 /**
94  *  @brief  add root object for string type
95  *
96  *  @param [in] key     keyname of object
97  *  @param [in] value   value of object for string type
98  */
99 //--------------------------------------------------------------------------
100 void
101 CicoSCMessage::addRootObject(const char * key, const std::string & value)
102 {
103 //    ICO_DBG("json_object_set_string_member(%s, %s) called.", key, value.c_str());
104     json_object_set_string_member(m_rootObj, key, value.c_str());
105 }
106
107 //--------------------------------------------------------------------------
108 /**
109  *  @brief  add root object for integer type
110  *
111  *  @param [in] key     key name of object
112  *  @param [in] value   value of object for integer type
113  */
114 //--------------------------------------------------------------------------
115 void
116 CicoSCMessage::addRootObject(const char * key, int value)
117 {
118 //    ICO_DBG("json_object_set_int_member(%s, %d) called.", key, value);
119     json_object_set_int_member(m_rootObj, key, value);
120 }
121
122 //--------------------------------------------------------------------------
123 /**
124  *  @brief  add argument object for char pointer type
125  *
126  *  @param [in] key     key name of object
127  *  @param [in] value   value of object for char pointer type
128  */
129 //--------------------------------------------------------------------------
130 void
131 CicoSCMessage::addArgObject(const char * key, const char * value)
132 {
133 //    ICO_DBG("json_object_set_string_member(%s, %s) called.", key, value);
134     json_object_set_string_member(m_argObj, key, value);
135 }
136
137 //--------------------------------------------------------------------------
138 /**
139  *  @brief  add argument object for string type
140  *
141  *  @param [in] key     key name of object
142  *  @param [in] value   value of object for string type
143  */
144 //--------------------------------------------------------------------------
145 void
146 CicoSCMessage::addArgObject(const char * key, const std::string & value)
147 {
148 //    ICO_DBG("json_object_set_string_member(%s, %s) called.", key, value.c_str());
149     json_object_set_string_member(m_argObj, key, value.c_str());
150 }
151
152 //--------------------------------------------------------------------------
153 /**
154  *  @brief  add argument object for integer type
155  *
156  *  @param [in] key     key name of object
157  *  @param [in] value   value of object for integer type
158  */
159 //--------------------------------------------------------------------------
160 void
161 CicoSCMessage::addArgObject(const char * key, int value)
162 {
163 //    ICO_DBG("json_object_set_int_member(%s, %d) called.", key, value);
164     json_object_set_int_member(m_argObj, key, value);
165 }
166
167 //--------------------------------------------------------------------------
168 /**
169  *  @brief  add argument object for array type
170  *
171  *  @param [in] key     key name of array
172  */
173 //--------------------------------------------------------------------------
174 void
175 CicoSCMessage::addArgObject(const char * key)
176 {
177     // count up reference
178     json_array_ref(m_array);
179
180 //    ICO_DBG("json_object_set_array_member(%s) called.", key);
181     json_object_set_array_member(m_argObj, key, m_array);
182 }
183
184 //--------------------------------------------------------------------------
185 /**
186  *  @brief  add array element for char pointer type
187  *
188  *  @param [in] value   value of object for char pointer type
189  */
190 //--------------------------------------------------------------------------
191 void
192 CicoSCMessage::addElmArray(const char * value)
193 {
194 //    ICO_DBG("json_array_add_string_element(%s) called.", value);
195     json_array_add_string_element(m_array, value);
196 }
197
198 //--------------------------------------------------------------------------
199 /**
200  *  @brief  add array element for string type
201  *
202  *  @param [in] value   value of object for string type
203  */
204 //--------------------------------------------------------------------------
205 void
206 CicoSCMessage::addElmArray(const std::string & value)
207 {
208 //    ICO_DBG("json_array_add_string_element(%s) called.", value.c_str());
209     json_array_add_string_element(m_array, value.c_str());
210 }
211
212 //--------------------------------------------------------------------------
213 /**
214  *  @brief  get destination handle 
215  *
216  *  @return pointer of destination handle
217  */
218 //--------------------------------------------------------------------------
219 CicoSCUwsHandle*
220 CicoSCMessage::getHandle(void)
221 {
222     return m_uwsHandle;
223 }
224
225 //--------------------------------------------------------------------------
226 /**
227  *  @brief  set destination handle 
228  *
229  *  @param [in]  handle destination handle
230  */
231 //--------------------------------------------------------------------------
232 void
233 CicoSCMessage::setHandle(CicoSCUwsHandle* handle)
234 {
235     m_uwsHandle = handle;
236 }
237
238 //--------------------------------------------------------------------------
239 /**
240  *  @brief  get message data
241  *
242  *  @return pointer of message data
243  */
244 //--------------------------------------------------------------------------
245 const char*
246 CicoSCMessage::getData(void)
247 {
248     // count up reference
249     json_object_ref(m_rootObj);
250     json_object_ref(m_argObj);
251
252     json_object_set_object_member(m_rootObj, "arg", m_argObj);
253     json_node_take_object(m_root, m_rootObj);
254     json_generator_set_root(m_generator, m_root);
255     gsize len = 0;
256     gchar* data = json_generator_to_data(m_generator, &len);
257     m_data = data;
258     g_free(data);
259     return m_data.c_str();
260 }
261
262 //--------------------------------------------------------------------------
263 /**
264  *  @brief  get application id of destination
265  *
266  *  @return application id
267  */
268 //--------------------------------------------------------------------------
269 const std::string &
270 CicoSCMessage::getSendToAppid(void)
271 {
272     return m_toAppid;
273 }
274
275 //--------------------------------------------------------------------------
276 /**
277  *  @brief  set application id of destination
278  */
279 //--------------------------------------------------------------------------
280 void
281 CicoSCMessage::setSendToAppid(const std::string & appid)
282 {
283     m_toAppid = appid;
284 }
285 // vim:set expandtab ts=4 sw=4: