tizen 2.4 release
[framework/convergence/service/service-plugin-client.git] / src / plugin_message.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdarg.h>
20 #include <glib.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <glib-unix.h>
27 #include <glib-object.h>
28
29 #include "libservice_plugin_log.h"
30 #include <plugin_message.h>
31 #include <json-glib/json-glib.h>
32
33 /*      For debug */
34 /*#define CONSOLE_MODE */
35
36 #ifdef CONSOLE_MODE
37 #define PRINT_LOG printf
38 #else
39 #define PRINT_LOG LSP_LOG_debug
40 #endif
41
42 #define FUNC_START() do {PRINT_LOG("\033[1m\033[32m""Start >>%s>>\n""\033[0m", __FUNCTION__); } while (0)
43 #define FUNC_END() do {PRINT_LOG("\033[1m\033[36m""End <<%s<<\n""\033[0m", __FUNCTION__); } while (0)
44
45
46 #define PLUGIN_MESSAGE_ELEMENT_KEY_CONTEXT_ID           "ctx_id"
47 #define PLUGIN_MESSAGE_ELEMENT_KEY_FUNCTION_NAME        "func_id"
48 #define PLUGIN_MESSAGE_ELEMENT_KEY_PARAMETER_MANDATORY  "man_param"
49 #define PLUGIN_MESSAGE_ELEMENT_KEY_PARAMETER_OPTIONAL   "opt_param"
50 #define PLUGIN_MESSAGE_ELEMENT_KEY_REQUEST_ID           "req_id"
51 #define PLUGIN_MESSAGE_ELEMENT_KEY_MESSAGE_TYPE         "msg_id"
52 #define PLUGIN_MESSAGE_ELEMENT_KEY_RESULT_CODE          "rcode"
53 #define PLUGIN_MESSAGE_ELEMENT_KEY_RESULT_MESSAGE       "rmsg"
54
55
56 #define SUCCESS 0
57 #define FAIL -1
58
59 #define PLUGIN_PARAMETER_BUF_SIZE_MAX 10
60
61 #define JSON_EMPTY_NODE_FILL_NULL(node)         do {if (0 == json_node_get_value_type((node))) { \
62                                                         json_node_free((node)); \
63                                                         (node) = NULL; \
64                                                         (node) = json_node_new(JSON_NODE_NULL); } } while (0)
65
66
67 struct _plugin_message_s {
68         JsonNode *context;
69         JsonNode *function;
70         JsonNode *parameter_mandatory;
71         JsonNode *parameter_optional;
72         JsonNode *request_id;
73         JsonNode *message_type;
74         JsonNode *rcode;
75         JsonNode *rmsg;
76 };
77
78 struct _plugin_message_array_s {
79         JsonArray *element;
80         plugin_data_type *types;
81 };
82
83 int plugin_message_create(plugin_message_h *message)
84 {
85         FUNC_START();
86         if (NULL != message) {
87                 plugin_message_h _msg = (plugin_message_h) calloc(1, sizeof(struct _plugin_message_s));
88                 if (NULL != _msg) {
89                         _msg->context                   = json_node_new(JSON_NODE_VALUE);
90                         _msg->function                  = json_node_new(JSON_NODE_VALUE);
91                         _msg->parameter_mandatory       = json_node_new(JSON_NODE_OBJECT);
92                         _msg->parameter_optional        = json_node_new(JSON_NODE_OBJECT);
93                         _msg->request_id                = json_node_new(JSON_NODE_VALUE);
94                         _msg->message_type              = json_node_new(JSON_NODE_VALUE);
95                         _msg->rcode                     = json_node_new(JSON_NODE_VALUE);
96                         _msg->rmsg                      = json_node_new(JSON_NODE_VALUE);
97
98                         json_node_set_object(_msg->parameter_mandatory, json_object_new());
99                         json_node_set_object(_msg->parameter_optional, json_object_new());
100
101                         *message = _msg;
102
103                         FUNC_END();
104                         return SUCCESS;
105                 }
106                 FUNC_END();
107         }
108         return FAIL;
109 }
110
111 void plugin_message_destroy(plugin_message_h message)
112 {
113         FUNC_START();
114         if (NULL != message) {
115                 json_node_free(message->context);
116                 json_node_free(message->function);
117                 json_node_free(message->parameter_mandatory);
118                 json_node_free(message->parameter_optional);
119                 json_node_free(message->request_id);
120                 json_node_free(message->message_type);
121                 json_node_free(message->rcode);
122                 json_node_free(message->rmsg);
123
124                 free(message);
125         }
126         FUNC_END();
127 }
128
129 /*
130 int plugin_message_set_value_int(plugin_message_h message, plugin_message_element_e field, int value)
131 {
132         if (NULL == message)
133         {
134                 return FAIL;
135         }
136
137         switch (field)
138         {
139         case PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID:
140                 json_node_set_int(message->context, (gint64)value);
141                 break;
142         case PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME:
143                 json_node_set_int(message->function, (gint64)value);
144                 break;
145         case PLUGIN_MESSAGE_ELEMENT_REQUEST_ID:
146                 json_node_set_int(message->request_id, (gint64)value);
147                 break;
148         case PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE:
149                 json_node_set_int(message->message_type, (gint64)value);
150                 break;
151         case PLUGIN_MESSAGE_ELEMENT_RESULT_CODE:
152                 json_node_set_int(message->rcode, (gint64)value);
153                 break;
154         case PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE:
155                 json_node_set_int(message->rmsg, (gint64)value);
156                 break;
157         default:
158                 return FAIL;
159         }
160
161         return SUCCESS;
162 }
163
164 int plugin_message_get_value_int(plugin_message_h message, plugin_message_element_e field, int *value)
165 {
166         if ((NULL == message) || (NULL == value))
167         {
168                 return FAIL;
169         }
170
171         switch (field)
172         {
173         case PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID:
174                 *value = (int)json_node_get_int(message->context);
175                 break;
176         case PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME:
177                 *value = (int)json_node_get_int(message->function);
178                 break;
179         case PLUGIN_MESSAGE_ELEMENT_REQUEST_ID:
180                 *value = (int)json_node_get_int(message->request_id);
181                 break;
182         case PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE:
183                 *value = (int)json_node_get_int(message->message_type);
184                 break;
185         case PLUGIN_MESSAGE_ELEMENT_RESULT_CODE:
186                 *value = (int)json_node_get_int(message->rcode);
187                 break;
188         case PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE:
189                 *value = (int)json_node_get_int(message->rmsg);
190                 break;
191         default:
192                 return FAIL;
193         }
194
195         return SUCCESS;
196 }
197 */
198
199 int plugin_message_set_value_number(plugin_message_h message, plugin_message_element_e field, pmnumber value)
200 {
201         FUNC_START();
202         if (NULL == message) {
203                 return FAIL;
204         }
205
206         switch (field) {
207         case PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID:
208                 json_node_set_int(message->context, (gint64)value);
209                 break;
210         case PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME:
211                 json_node_set_int(message->function, (gint64)value);
212                 break;
213         case PLUGIN_MESSAGE_ELEMENT_REQUEST_ID:
214                 json_node_set_int(message->request_id, (gint64)value);
215                 break;
216         case PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE:
217                 json_node_set_int(message->message_type, (gint64)value);
218                 break;
219         case PLUGIN_MESSAGE_ELEMENT_RESULT_CODE:
220                 json_node_set_int(message->rcode, (gint64)value);
221                 break;
222         case PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE:
223                 json_node_set_int(message->rmsg, (gint64)value);
224                 break;
225         default:
226                 return FAIL;
227         }
228
229         FUNC_END();
230         return SUCCESS;
231 }
232
233 int plugin_message_get_value_number(plugin_message_h message, plugin_message_element_e field, pmnumber *value)
234 {
235         FUNC_START();
236         if ((NULL == message) || (NULL == value)) {
237                 return FAIL;
238         }
239
240         switch (field) {
241         case PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID:
242                 *value = (long long int)json_node_get_int(message->context);
243                 break;
244         case PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME:
245                 *value = (long long int)json_node_get_int(message->function);
246                 break;
247         case PLUGIN_MESSAGE_ELEMENT_REQUEST_ID:
248                 *value = (long long int)json_node_get_int(message->request_id);
249                 break;
250         case PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE:
251                 *value = (long long int)json_node_get_int(message->message_type);
252                 break;
253         case PLUGIN_MESSAGE_ELEMENT_RESULT_CODE:
254                 *value = (long long int)json_node_get_int(message->rcode);
255                 break;
256         case PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE:
257                 *value = (long long int)json_node_get_int(message->rmsg);
258                 break;
259         default:
260                 return FAIL;
261         }
262
263         FUNC_END();
264         return SUCCESS;
265 }
266
267 int plugin_message_set_value_string(plugin_message_h message, plugin_message_element_e field, const char *value)
268 {
269         FUNC_START();
270         if (NULL == message) {
271                 return FAIL;
272         }
273
274         switch (field) {
275         case PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID:
276                 json_node_set_string(message->context, value);
277                 break;
278         case PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME:
279                 json_node_set_string(message->function, value);
280                 break;
281         case PLUGIN_MESSAGE_ELEMENT_REQUEST_ID:
282                 json_node_set_string(message->request_id, value);
283                 break;
284         case PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE:
285                 json_node_set_string(message->message_type, value);
286                 break;
287         case PLUGIN_MESSAGE_ELEMENT_RESULT_CODE:
288                 json_node_set_string(message->rcode, value);
289                 break;
290         case PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE:
291                 json_node_set_string(message->rmsg, value);
292                 break;
293         default:
294                 return FAIL;
295         }
296
297         FUNC_END();
298         return SUCCESS;
299 }
300
301 int plugin_message_get_value_string(plugin_message_h message, plugin_message_element_e field, char **value)
302 {
303         FUNC_START();
304         if ((NULL == message) || (NULL == value)) {
305                 return FAIL;
306         }
307
308         switch (field) {
309         case PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID:
310                 *value = (char *)json_node_dup_string(message->context);
311                 break;
312         case PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME:
313                 *value = (char *)json_node_dup_string(message->function);
314                 break;
315         case PLUGIN_MESSAGE_ELEMENT_REQUEST_ID:
316                 *value = (char *)json_node_dup_string(message->request_id);
317                 break;
318         case PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE:
319                 *value = (char *)json_node_dup_string(message->message_type);
320                 break;
321         case PLUGIN_MESSAGE_ELEMENT_RESULT_CODE:
322                 *value = (char *)json_node_dup_string(message->rcode);
323                 break;
324         case PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE:
325                 *value = (char *)json_node_dup_string(message->rmsg);
326                 break;
327         default:
328                 return FAIL;
329         }
330
331         FUNC_END();
332         return SUCCESS;
333 }
334
335 int plugin_message_set_value_bool(plugin_message_h message, plugin_message_element_e field, bool value)
336 {
337         FUNC_START();
338         if (NULL == message) {
339                 return FAIL;
340         }
341
342         switch (field) {
343         case PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID:
344                 json_node_set_boolean(message->context, value);
345                 break;
346         case PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME:
347                 json_node_set_boolean(message->function, value);
348                 break;
349         case PLUGIN_MESSAGE_ELEMENT_REQUEST_ID:
350                 json_node_set_boolean(message->request_id, value);
351                 break;
352         case PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE:
353                 json_node_set_boolean(message->message_type, value);
354                 break;
355         case PLUGIN_MESSAGE_ELEMENT_RESULT_CODE:
356                 json_node_set_boolean(message->rcode, value);
357                 break;
358         case PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE:
359                 json_node_set_boolean(message->rmsg, value);
360                 break;
361         default:
362                 return FAIL;
363         }
364
365         FUNC_END();
366         return SUCCESS;
367 }
368
369 int plugin_message_get_value_bool(plugin_message_h message, plugin_message_element_e field, bool *value)
370 {
371         FUNC_START();
372         if ((NULL == message) || (NULL == value)) {
373                 return FAIL;
374         }
375
376         switch (field) {
377         case PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID:
378                 *value = (bool)json_node_get_boolean(message->context);
379                 break;
380         case PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME:
381                 *value = (bool)json_node_get_boolean(message->function);
382                 break;
383         case PLUGIN_MESSAGE_ELEMENT_REQUEST_ID:
384                 *value = (bool)json_node_get_boolean(message->request_id);
385                 break;
386         case PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE:
387                 *value = (bool)json_node_get_boolean(message->message_type);
388                 break;
389         case PLUGIN_MESSAGE_ELEMENT_RESULT_CODE:
390                 *value = (bool)json_node_get_boolean(message->rcode);
391                 break;
392         case PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE:
393                 *value = (bool)json_node_get_boolean(message->rmsg);
394                 break;
395         default:
396                 return FAIL;
397         }
398
399         FUNC_END();
400         return SUCCESS;
401 }
402
403 /*
404 int _plugin_message_set_parameter_value_int(JsonNode *node, int param_index, int value)
405 {
406         char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
407         if (0 > param_index)
408         {
409                 return FAIL;
410         }
411         snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
412
413         JsonObject *params;
414         params = json_node_get_object(node);
415
416         // release memory of legacy node
417
418 //      JsonNode *legacy_node = json_object_get_member(params, index);
419 //      if (NULL != legacy_node)
420 //      {
421 //              json_node_free(legacy_node);
422 //      }
423
424         JsonNode *new_node = json_node_new(JSON_NODE_VALUE);
425         json_node_set_int(new_node, (gint64)value);
426         json_object_set_member(params, index, new_node);
427
428         return SUCCESS;
429 }
430
431 int _plugin_message_get_parameter_value_int(JsonNode *node, int param_index, int *value)
432 {
433         char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
434         if (0 > param_index)
435         {
436                 return FAIL;
437         }
438         snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
439
440         JsonObject *params;
441         params = json_node_get_object(node);
442
443         JsonNode *legacy_node = json_object_get_member(params, index);
444         if (NULL != legacy_node)
445         {
446                 *value = (int)json_node_get_int(legacy_node);
447         }
448         else
449         {
450                 return FAIL;
451         }
452
453         return SUCCESS;
454 }
455 */
456
457 int _plugin_message_set_parameter_value_number(JsonNode *node, int param_index, pmnumber value)
458 {
459         FUNC_START();
460         char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
461         if (0 > param_index) {
462                 return FAIL;
463         }
464         snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
465
466         JsonObject *params;
467         params = json_node_get_object(node);
468
469         /* release memory of legacy node */
470 /*
471         JsonNode *legacy_node = json_object_get_member(params, index);
472         if (NULL != legacy_node)
473         {
474                 json_node_free(legacy_node);
475         }
476 */
477         JsonNode *new_node = json_node_new(JSON_NODE_VALUE);
478         json_node_set_int(new_node, (gint64)value);
479         json_object_set_member(params, index, new_node);
480
481         FUNC_END();
482         return SUCCESS;
483 }
484
485 int _plugin_message_get_parameter_value_number(JsonNode *node, int param_index, pmnumber *value)
486 {
487         FUNC_START();
488         char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
489         if (0 > param_index) {
490                 return FAIL;
491         }
492         snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
493
494         JsonObject *params;
495         params = json_node_get_object(node);
496
497         JsonNode *legacy_node = json_object_get_member(params, index);
498         if (NULL != legacy_node) {
499                 *value = (long long int)json_node_get_int(legacy_node);
500         } else {
501                 return FAIL;
502         }
503
504         FUNC_END();
505         return SUCCESS;
506 }
507
508 int _plugin_message_set_parameter_value_string(JsonNode *node, int param_index, const char *value)
509 {
510         FUNC_START();
511         char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
512         if (0 > param_index) {
513                 return FAIL;
514         }
515         snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
516
517         JsonObject *params;
518         params = json_node_get_object(node);
519
520         /* release memory of legacy node */
521 /*
522         JsonNode *legacy_node = json_object_get_member(params, index);
523         if (NULL != legacy_node)
524         {
525                 json_node_free(legacy_node);
526         }
527 */
528         JsonNode *new_node = json_node_new(JSON_NODE_VALUE);
529         json_node_set_string(new_node, value);
530         json_object_set_member(params, index, new_node);
531
532         FUNC_END();
533         return SUCCESS;
534 }
535
536 int _plugin_message_get_parameter_value_string(JsonNode *node, int param_index, char **value)
537 {
538         FUNC_START();
539         char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
540         if (0 > param_index) {
541                 return FAIL;
542         }
543         snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
544
545         JsonObject *params;
546         params = json_node_get_object(node);
547
548         JsonNode *legacy_node = json_object_get_member(params, index);
549         if (NULL != legacy_node) {
550                 *value = (char *)json_node_dup_string(legacy_node);
551         } else {
552                 return FAIL;
553         }
554
555         FUNC_END();
556         return SUCCESS;
557 }
558 int _plugin_message_set_parameter_value_bool(JsonNode *node, int param_index, bool value)
559 {
560         FUNC_START();
561         char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
562         if (0 > param_index) {
563                 return FAIL;
564         }
565         snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
566
567         JsonObject *params;
568         params = json_node_get_object(node);
569
570         /* release memory of legacy node */
571 /*
572         JsonNode *legacy_node = json_object_get_member(params, index);
573         if (NULL != legacy_node)
574         {
575                 json_node_free(legacy_node);
576         }
577 */
578         JsonNode *new_node = json_node_new(JSON_NODE_VALUE);
579         json_node_set_boolean(new_node, value);
580         json_object_set_member(params, index, new_node);
581
582         FUNC_END();
583         return SUCCESS;
584 }
585
586 int _plugin_message_get_parameter_value_bool(JsonNode *node, int param_index, bool *value)
587 {
588         FUNC_START();
589         char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
590         if (0 > param_index) {
591                 return FAIL;
592         }
593         snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
594
595         JsonObject *params;
596         params = json_node_get_object(node);
597
598         JsonNode *legacy_node = json_object_get_member(params, index);
599         if (NULL != legacy_node) {
600                 *value = (bool)json_node_get_boolean(legacy_node);
601         } else {
602                 return FAIL;
603         }
604
605         FUNC_END();
606         return SUCCESS;
607 }
608
609 int _plugin_message_set_parameter_value_array(JsonNode *node, int param_index, plugin_message_array_h value)
610 {
611         FUNC_START();
612         char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
613         if (0 > param_index) {
614                 return FAIL;
615         }
616         snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
617
618         JsonObject *params;
619         params = json_node_get_object(node);
620
621         /* release memory of legacy node */
622 /*
623         JsonNode *legacy_node = json_object_get_member(params, index);
624         if (NULL != legacy_node)
625         {
626                 json_node_free(legacy_node);
627         }
628 */
629         JsonNode *new_node = json_node_new(JSON_NODE_ARRAY);
630         json_node_set_array(new_node, value->element);
631         json_object_set_member(params, index, new_node);
632
633         FUNC_END();
634         return SUCCESS;
635 }
636
637 int _plugin_message_get_parameter_value_array(JsonNode *node, int param_index, plugin_message_array_h *value)
638 {
639         FUNC_START();
640         char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
641         if (0 > param_index) {
642                 return FAIL;
643         }
644         snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
645
646         JsonObject *params;
647         params = json_node_get_object(node);
648
649
650         JsonNode *legacy_node = json_object_get_member(params, index);
651         if ((NULL != legacy_node) && (JSON_NODE_ARRAY == json_node_get_node_type(legacy_node))) {
652                 JsonArray *j_array = NULL;
653                 PRINT_LOG("duplacate json array\n");
654                 j_array = json_node_dup_array(legacy_node);
655
656                 if (NULL != j_array) {
657                         plugin_message_array_h _array = (plugin_message_array_h) calloc(1, sizeof(struct _plugin_message_array_s));
658                         if (NULL == _array) {
659                                 PRINT_LOG("Memory allocation failed\n");
660                                 return FAIL;
661                         }
662
663                         _array->element = j_array;
664
665                         PRINT_LOG("get object from json array\n");
666                         JsonObject *job = json_array_get_object_element(j_array, 0);
667
668                         int len = (int) json_object_get_size(job);
669
670                         PRINT_LOG("json object length : %d\n", len);
671                         _array->types = (plugin_data_type *) calloc((len + 1), sizeof(plugin_data_type));
672                         if (NULL == _array->types) {
673                                 PRINT_LOG("Memory allocation failed\n");
674                                 free(_array);
675                                 return FAIL;
676                         }
677
678                         int i;
679                         JsonNode *iter_node;
680                         char idx[PLUGIN_PARAMETER_BUF_SIZE_MAX];
681
682                         for (i = 0; i < len; i++) {
683                                 snprintf(idx, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", i+1);
684
685                                 iter_node = json_object_get_member(job, idx);
686                                 if (JSON_NODE_VALUE == json_node_get_node_type(iter_node)) {
687                                         GType value_type = json_node_get_value_type(iter_node);
688                                         if (G_TYPE_INT64 == value_type) {
689                                                 PRINT_LOG("node type (long == int)\n");
690                                                 _array->types[i] = PLUGIN_DATA_TYPE_NUM;
691                                         } else if (G_TYPE_BOOLEAN == value_type) {
692                                                 PRINT_LOG("node type (bool)\n");
693                                                 _array->types[i] = PLUGIN_DATA_TYPE_BOOL;
694                                         } else if (G_TYPE_STRING == value_type) {
695                                                 PRINT_LOG("node type (string)\n");
696                                                 _array->types[i] = PLUGIN_DATA_TYPE_STRING;
697                                         } else {
698                                                 PRINT_LOG("node type (unknown)\n");
699                                                 _array->types[i] = PLUGIN_DATA_TYPE_UNKNOWN;
700                                         }
701                                 } else {
702                                         PRINT_LOG("node type (no value node)\n");
703                                         _array->types[i] = PLUGIN_DATA_TYPE_UNKNOWN;
704                                 }
705                         }
706
707                         *value = _array;
708
709                         FUNC_END();
710                         return SUCCESS;
711                 }
712         }
713
714         FUNC_END();
715         return FAIL;
716 }
717 /*
718 int plugin_message_set_param_int(plugin_message_h message, int param_index, int value)
719 {
720         if (NULL == message)
721         {
722                 return FAIL;
723         }
724
725         return _plugin_message_set_parameter_value_int(message->parameter_mandatory, param_index, value);
726 }
727
728 int plugin_message_get_param_int(plugin_message_h message, int param_index, int *value)
729 {
730         if ((NULL == message) || (NULL == value))
731         {
732                 return FAIL;
733         }
734
735         return _plugin_message_get_parameter_value_int(message->parameter_mandatory, param_index, value);
736 }
737 */
738
739 int plugin_message_set_param_number(plugin_message_h message, int param_index, pmnumber value)
740 {
741         FUNC_START();
742         if (NULL == message) {
743                 return FAIL;
744         }
745
746         FUNC_END();
747         return _plugin_message_set_parameter_value_number(message->parameter_mandatory, param_index, value);
748 }
749
750 int plugin_message_get_param_number(plugin_message_h message, int param_index, pmnumber *value)
751 {
752         FUNC_START();
753         if ((NULL == message) || (NULL == value)) {
754                 return FAIL;
755         }
756
757         FUNC_END();
758         return _plugin_message_get_parameter_value_number(message->parameter_mandatory, param_index, value);
759 }
760
761 int plugin_message_set_param_string(plugin_message_h message, int param_index, const char *value)
762 {
763         FUNC_START();
764         if ((NULL == message) || (NULL == value)) {
765                 return FAIL;
766         }
767
768         FUNC_END();
769         return _plugin_message_set_parameter_value_string(message->parameter_mandatory, param_index, value);
770 }
771
772 int plugin_message_get_param_string(plugin_message_h message, int param_index, char **value)
773 {
774         FUNC_START();
775         if ((NULL == message) || (NULL == value)) {
776                 return FAIL;
777         }
778
779         FUNC_END();
780         return _plugin_message_get_parameter_value_string(message->parameter_mandatory, param_index, value);
781 }
782
783 int plugin_message_set_param_bool(plugin_message_h message, int param_index, bool value)
784 {
785         FUNC_START();
786         if (NULL == message) {
787                 return FAIL;
788         }
789
790         FUNC_END();
791         return _plugin_message_set_parameter_value_bool(message->parameter_mandatory, param_index, value);
792 }
793
794 int plugin_message_get_param_bool(plugin_message_h message, int param_index, bool *value)
795 {
796         FUNC_START();
797         if ((NULL == message) || (NULL == value)) {
798                 return FAIL;
799         }
800
801         FUNC_END();
802         return _plugin_message_get_parameter_value_bool(message->parameter_mandatory, param_index, value);
803 }
804
805 int plugin_message_set_param_array(plugin_message_h message, int param_index, plugin_message_array_h value)
806 {
807         FUNC_START();
808         if ((NULL == message) || (NULL == value)) {
809                 return FAIL;
810         }
811
812         FUNC_END();
813         return _plugin_message_set_parameter_value_array(message->parameter_mandatory, param_index, value);
814 }
815
816 int plugin_message_get_param_array(plugin_message_h message, int param_index, plugin_message_array_h *value)
817 {
818         FUNC_START();
819         if ((NULL == message) || (NULL == value)) {
820                 return FAIL;
821         }
822
823         FUNC_END();
824         return _plugin_message_get_parameter_value_array(message->parameter_mandatory, param_index, value);
825 }
826
827 /*
828 int plugin_message_set_opt_param_int(plugin_message_h message, int param_index, int value)
829 {
830         FUNC_START();
831         if (NULL == message)
832         {
833                 return FAIL;
834         }
835
836         FUNC_END();
837         return _plugin_message_set_parameter_value_int(message->parameter_optional, param_index, value);
838 }
839
840 int plugin_message_get_opt_param_int(plugin_message_h message, int param_index, int *value)
841 {
842         FUNC_START();
843         if ((NULL == message) || (NULL == value))
844         {
845                 return FAIL;
846         }
847
848         FUNC_END();
849         return _plugin_message_get_parameter_value_int(message->parameter_optional, param_index, value);
850 }
851 */
852
853 int plugin_message_set_opt_param_number(plugin_message_h message, int param_index, pmnumber value)
854 {
855         FUNC_START();
856         if (NULL == message) {
857                 return FAIL;
858         }
859
860         FUNC_END();
861         return _plugin_message_set_parameter_value_number(message->parameter_optional, param_index, value);
862 }
863
864 int plugin_message_get_opt_param_number(plugin_message_h message, int param_index, pmnumber *value)
865 {
866         FUNC_START();
867         if ((NULL == message) || (NULL == value)) {
868                 return FAIL;
869         }
870
871         FUNC_END();
872         return _plugin_message_get_parameter_value_number(message->parameter_optional, param_index, value);
873 }
874
875 int plugin_message_set_opt_param_string(plugin_message_h message, int param_index, const char *value)
876 {
877         FUNC_START();
878         if ((NULL == message) || (NULL == value)) {
879                 return FAIL;
880         }
881
882         FUNC_END();
883         return _plugin_message_set_parameter_value_string(message->parameter_optional, param_index, value);
884 }
885
886 int plugin_message_get_opt_param_string(plugin_message_h message, int param_index, char **value)
887 {
888         FUNC_START();
889         if ((NULL == message) || (NULL == value)) {
890                 return FAIL;
891         }
892
893         FUNC_END();
894         return _plugin_message_get_parameter_value_string(message->parameter_optional, param_index, value);
895 }
896
897 int plugin_message_set_opt_param_bool(plugin_message_h message, int param_index, bool value)
898 {
899         FUNC_START();
900         if (NULL == message) {
901                 return FAIL;
902         }
903
904         FUNC_END();
905         return _plugin_message_set_parameter_value_bool(message->parameter_optional, param_index, value);
906 }
907
908 int plugin_message_get_opt_param_bool(plugin_message_h message, int param_index, bool *value)
909 {
910         FUNC_START();
911         if ((NULL == message) || (NULL == value)) {
912                 return FAIL;
913         }
914
915         FUNC_END();
916         return _plugin_message_get_parameter_value_bool(message->parameter_optional, param_index, value);
917 }
918
919 int plugin_message_set_opt_param_array(plugin_message_h message, int param_index, plugin_message_array_h value)
920 {
921         FUNC_START();
922         if ((NULL == message) || (NULL == value)) {
923                 return FAIL;
924         }
925
926         FUNC_END();
927         return _plugin_message_set_parameter_value_array(message->parameter_optional, param_index, value);
928 }
929
930 int plugin_message_get_opt_param_array(plugin_message_h message, int param_index, plugin_message_array_h *value)
931 {
932         FUNC_START();
933         if ((NULL == message) || (NULL == value)) {
934                 return FAIL;
935         }
936
937         FUNC_END();
938         return _plugin_message_get_parameter_value_array(message->parameter_optional, param_index, value);
939 }
940
941 char *_json_serialize_by_jnode(JsonNode *total_node)
942 {
943         FUNC_START();
944         JsonGenerator *gen = json_generator_new();
945         json_generator_set_root(gen, total_node);
946
947         char *t_data = NULL;
948         gsize len = 0;
949         t_data = json_generator_to_data(gen, &len);
950
951         g_object_unref(gen);
952
953         FUNC_END();
954         return t_data;
955 /*      return t; */
956 }
957
958 JsonNode *_json_deserialize_by_data(const char *data)
959 {
960         FUNC_START();
961         JsonParser *parser = json_parser_new();
962         json_parser_load_from_data(parser, data, strlen(data), NULL);
963
964         PRINT_LOG("next\n");
965
966         JsonNode *node = json_parser_get_root(parser);
967
968         JsonNodeType nt = json_node_get_node_type(node);
969         if (nt == JSON_NODE_OBJECT)
970                 PRINT_LOG("object type\n");
971         if (nt == JSON_NODE_ARRAY)
972                 PRINT_LOG("array type\n");
973         if (nt == JSON_NODE_VALUE)
974                 PRINT_LOG("value type\n");
975         if (nt == JSON_NODE_NULL)
976                 PRINT_LOG("null type\n");
977
978         FUNC_END();
979         return node;
980 }
981
982 int plugin_message_serialize(plugin_message_h message, char **data)
983 {
984         FUNC_START();
985         if ((NULL == message) || (NULL == data)) {
986                 return FAIL;
987         }
988
989         JsonObject *obj = json_object_new();
990
991         JsonNode *context_id = json_node_copy(message->context);
992         JsonNode *function_name = json_node_copy(message->function);
993         JsonNode *parameter_mandatory = json_node_copy(message->parameter_mandatory);
994         JsonNode *parameter_optional = json_node_copy(message->parameter_optional);
995         JsonNode *request_id = json_node_copy(message->request_id);
996         JsonNode *message_type = json_node_copy(message->message_type);
997         JsonNode *rcode = json_node_copy(message->rcode);
998         JsonNode *rmsg = json_node_copy(message->rmsg);
999
1000         JSON_EMPTY_NODE_FILL_NULL(context_id);
1001         JSON_EMPTY_NODE_FILL_NULL(function_name);
1002         JSON_EMPTY_NODE_FILL_NULL(parameter_mandatory);
1003         JSON_EMPTY_NODE_FILL_NULL(parameter_optional);
1004         JSON_EMPTY_NODE_FILL_NULL(request_id);
1005         JSON_EMPTY_NODE_FILL_NULL(message_type);
1006         JSON_EMPTY_NODE_FILL_NULL(rcode);
1007         JSON_EMPTY_NODE_FILL_NULL(rmsg);
1008
1009         json_object_set_member(obj, PLUGIN_MESSAGE_ELEMENT_KEY_CONTEXT_ID, context_id);
1010         json_object_set_member(obj, PLUGIN_MESSAGE_ELEMENT_KEY_FUNCTION_NAME, function_name);
1011         json_object_set_member(obj, PLUGIN_MESSAGE_ELEMENT_KEY_PARAMETER_MANDATORY, parameter_mandatory);
1012         json_object_set_member(obj, PLUGIN_MESSAGE_ELEMENT_KEY_PARAMETER_OPTIONAL, parameter_optional);
1013         json_object_set_member(obj, PLUGIN_MESSAGE_ELEMENT_KEY_REQUEST_ID, request_id);
1014         json_object_set_member(obj, PLUGIN_MESSAGE_ELEMENT_KEY_MESSAGE_TYPE, message_type);
1015         json_object_set_member(obj, PLUGIN_MESSAGE_ELEMENT_KEY_RESULT_CODE, rcode);
1016         json_object_set_member(obj, PLUGIN_MESSAGE_ELEMENT_KEY_RESULT_MESSAGE, rmsg);
1017
1018         JsonNode *node = json_node_new(JSON_NODE_OBJECT);
1019         json_node_take_object(node, obj);
1020
1021         char *_data = NULL;
1022         _data = _json_serialize_by_jnode(node);
1023 /*
1024         json_node_free(node);
1025         json_object_unref(obj);
1026
1027         json_node_free(context_id);
1028         json_node_free(function_name);
1029         json_node_free(parameter_mandatory);
1030         json_node_free(parameter_optional);
1031         json_node_free(request_id);
1032         json_node_free(message_type);
1033         json_node_free(rcode);
1034         json_node_free(rmsg);
1035 */
1036         *data = _data;
1037         FUNC_END();
1038         return SUCCESS;
1039 }
1040
1041 int plugin_message_deserialize(const char *data, plugin_message_h *message)
1042 {
1043         FUNC_START();
1044         if ((NULL == message) || (NULL == data)) {
1045                 return FAIL;
1046         }
1047
1048         plugin_message_h _msg = (plugin_message_h) calloc(1, sizeof(struct _plugin_message_s));
1049         if (NULL == _msg) {
1050                 return FAIL;
1051         }
1052
1053         JsonNode *root_node = _json_deserialize_by_data(data);
1054
1055         JsonObject *root_object = json_node_get_object(root_node);
1056
1057         _msg->context                   = json_object_dup_member(root_object, PLUGIN_MESSAGE_ELEMENT_KEY_CONTEXT_ID);
1058         _msg->function                  = json_object_dup_member(root_object, PLUGIN_MESSAGE_ELEMENT_KEY_FUNCTION_NAME);
1059         _msg->parameter_mandatory       = json_object_dup_member(root_object, PLUGIN_MESSAGE_ELEMENT_KEY_PARAMETER_MANDATORY);
1060         _msg->parameter_optional        = json_object_dup_member(root_object, PLUGIN_MESSAGE_ELEMENT_KEY_PARAMETER_OPTIONAL);
1061         _msg->request_id                = json_object_dup_member(root_object, PLUGIN_MESSAGE_ELEMENT_KEY_REQUEST_ID);
1062         _msg->message_type              = json_object_dup_member(root_object, PLUGIN_MESSAGE_ELEMENT_KEY_MESSAGE_TYPE);
1063         _msg->rcode                     = json_object_dup_member(root_object, PLUGIN_MESSAGE_ELEMENT_KEY_RESULT_CODE);
1064         _msg->rmsg                      = json_object_dup_member(root_object, PLUGIN_MESSAGE_ELEMENT_KEY_RESULT_MESSAGE);
1065
1066         *message = _msg;
1067
1068         json_node_free(root_node);
1069
1070         return SUCCESS;
1071 }
1072
1073 int plugin_message_array_create(const plugin_data_type *type_string, plugin_message_array_h *array)
1074 {
1075         FUNC_START();
1076         if ((NULL == type_string) || (NULL == array)) {
1077                 return FAIL;
1078         }
1079         int i = 0, len = strlen(type_string);
1080         for (i = 0; i < len; i++) {
1081                 switch (type_string[i]) {
1082                 case PLUGIN_DATA_TYPE_NUM:
1083                 case PLUGIN_DATA_TYPE_BOOL:
1084                 case PLUGIN_DATA_TYPE_STRING:
1085                         break;
1086                 default:
1087                         return FAIL;
1088                 }
1089         }
1090
1091         plugin_message_array_h _array = (plugin_message_array_h) calloc(1, sizeof(struct _plugin_message_array_s));
1092         plugin_data_type *_types = (plugin_data_type *) calloc(strlen(type_string)+1, sizeof(plugin_data_type));
1093
1094         if ((NULL == _array) || (NULL == _types)) {
1095                 free(_array);
1096                 free(_types);
1097                 return FAIL;
1098         }
1099
1100         _array->element = json_array_new();
1101
1102         _array->types = _types;
1103         strncpy(_array->types, type_string, strlen(type_string));
1104
1105         *array = _array;
1106
1107         FUNC_END();
1108         return SUCCESS;
1109 }
1110
1111 void plugin_message_array_destroy(plugin_message_array_h array)
1112 {
1113         FUNC_START();
1114         if (NULL != array) {
1115                 json_array_unref(array->element);
1116                 free(array->types);
1117                 free(array);
1118         }
1119         FUNC_END();
1120 }
1121
1122 int plugin_message_array_add_element(plugin_message_array_h array, ...)
1123 {
1124         FUNC_START();
1125         if (NULL == array) {
1126                 return FAIL;
1127         }
1128
1129         plugin_data_type *types = array->types;
1130
1131         int count = strlen(types);
1132         int i = 0;
1133         char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
1134
1135         JsonObject *obj = json_object_new();
1136         JsonNode *new_node;
1137
1138         PRINT_LOG("[%d]count : %d\n", __LINE__, count);
1139
1140         va_list vl;
1141         va_start(vl, array);
1142
1143         for (i = 0; i < count; i++) {
1144                 memset(index, 0, PLUGIN_PARAMETER_BUF_SIZE_MAX);
1145                 snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", i+1);
1146                 PRINT_LOG("[%d]index : %s, type : %c\n", __LINE__, index, types[i]);
1147
1148                 switch (types[i]) {
1149 /*
1150                 case PLUGIN_DATA_TYPE_INT:
1151                         new_node = json_node_new(JSON_NODE_VALUE);
1152                         json_node_set_int(new_node, (gint64)va_arg(vl, int));
1153                         json_object_set_member(obj, index, new_node);
1154                         PRINT_LOG("[%d]index : %s\n", __LINE__, index);
1155                         break;
1156 */
1157                 case PLUGIN_DATA_TYPE_NUM:
1158                         new_node = json_node_new(JSON_NODE_VALUE);
1159                         json_node_set_int(new_node, (gint64)va_arg(vl, long long int));
1160                         json_object_set_member(obj, index, new_node);
1161                         PRINT_LOG("[%d]index : %s\n", __LINE__, index);
1162                         break;
1163                 case PLUGIN_DATA_TYPE_BOOL:
1164                         new_node = json_node_new(JSON_NODE_VALUE);
1165                         json_node_set_boolean(new_node, va_arg(vl, int) ? true : false);
1166                         json_object_set_member(obj, index, new_node);
1167                         PRINT_LOG("[%d]index : %s\n", __LINE__, index);
1168                         break;
1169                 case PLUGIN_DATA_TYPE_STRING:
1170                         new_node = json_node_new(JSON_NODE_VALUE);
1171                         json_node_set_string(new_node, va_arg(vl, char *));
1172                         json_object_set_member(obj, index, new_node);
1173                         PRINT_LOG("[%d]index : %s\n", __LINE__, index);
1174                         break;
1175                 default:
1176                         new_node = json_node_new(JSON_NODE_NULL);
1177                         json_object_set_member(obj, index, new_node);
1178                         PRINT_LOG("[%d]index : %s <unknown dada, function fail>\n", __LINE__, index);
1179                         va_end(vl);
1180                         return FAIL;
1181                 }
1182         }
1183         va_end(vl);
1184
1185         PRINT_LOG("array length : %u\n", json_object_get_size(obj));
1186         json_array_add_object_element(array->element, obj);
1187
1188         FUNC_END();
1189         return SUCCESS;
1190 }
1191
1192 int plugin_message_array_get_element(plugin_message_array_h array, int idx, ...)
1193 {
1194         FUNC_START();
1195         if ((NULL == array) || (1 > idx)) {
1196                 return FAIL;
1197         }
1198
1199         if (json_array_get_length(array->element) < idx) {
1200                 return FAIL;
1201         }
1202
1203         JsonObject *obj = json_array_get_object_element(array->element, idx-1);
1204
1205         if (NULL == obj) {
1206                 return FAIL;
1207         }
1208
1209         plugin_data_type *types = array->types;
1210
1211         int count = strlen(types);
1212         int i = 0;
1213         char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
1214
1215         JsonNode *new_node;
1216
1217         PRINT_LOG("[%d]count : %d\n", __LINE__, count);
1218
1219         va_list vl;
1220         va_start(vl, idx);
1221
1222         for (i = 0; i < count; i++) {
1223                 memset(index, 0, PLUGIN_PARAMETER_BUF_SIZE_MAX);
1224                 snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", i+1);
1225                 PRINT_LOG("[%d]index : %s, type : %c\n", __LINE__, index, types[i]);
1226                 new_node = json_object_get_member(obj, index);
1227
1228                 switch (types[i]) {
1229 /*
1230                 case PLUGIN_DATA_TYPE_INT:
1231                         *(va_arg(vl, int *)) = (int) json_node_get_int(new_node);
1232                         PRINT_LOG("[%d]index : %s\n", __LINE__, index);
1233                         break;
1234 */
1235                 case PLUGIN_DATA_TYPE_NUM:
1236                         *(va_arg(vl, long long int *)) = (long long int) json_node_get_int(new_node);
1237                         PRINT_LOG("[%d]index : %s\n", __LINE__, index);
1238                         break;
1239                 case PLUGIN_DATA_TYPE_BOOL:
1240                         *(va_arg(vl, bool *)) = (bool) json_node_get_boolean(new_node);
1241                         PRINT_LOG("[%d]index : %s\n", __LINE__, index);
1242                         break;
1243                 case PLUGIN_DATA_TYPE_STRING:
1244                         *(va_arg(vl, char **)) = (char *) json_node_dup_string(new_node);
1245                         PRINT_LOG("[%d]index : %s\n", __LINE__, index);
1246                         break;
1247                 default:
1248                         PRINT_LOG("[%d]index : %s\n", __LINE__, index);
1249                         break;
1250                 }
1251         }
1252         va_end(vl);
1253
1254         FUNC_END();
1255         return SUCCESS;
1256 }
1257