2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sys/types.h>
26 #include <glib-unix.h>
27 #include <glib-object.h>
29 #include "libservice_plugin_log.h"
30 #include <plugin_message.h>
31 #include <json-glib/json-glib.h>
34 /*#define CONSOLE_MODE */
37 #define PRINT_LOG printf
39 #define PRINT_LOG LSP_LOG_debug
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)
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"
59 #define PLUGIN_PARAMETER_BUF_SIZE_MAX 10
61 #define JSON_EMPTY_NODE_FILL_NULL(node) do {if (0 == json_node_get_value_type((node))) { \
62 json_node_free((node)); \
64 (node) = json_node_new(JSON_NODE_NULL); } } while (0)
67 struct _plugin_message_s {
70 JsonNode *parameter_mandatory;
71 JsonNode *parameter_optional;
73 JsonNode *message_type;
78 struct _plugin_message_array_s {
80 plugin_data_type *types;
83 int plugin_message_create(plugin_message_h *message)
86 if (NULL != message) {
87 plugin_message_h _msg = (plugin_message_h) calloc(1, sizeof(struct _plugin_message_s));
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);
98 json_node_set_object(_msg->parameter_mandatory, json_object_new());
99 json_node_set_object(_msg->parameter_optional, json_object_new());
111 void plugin_message_destroy(plugin_message_h message)
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);
130 int plugin_message_set_value_int(plugin_message_h message, plugin_message_element_e field, int value)
139 case PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID:
140 json_node_set_int(message->context, (gint64)value);
142 case PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME:
143 json_node_set_int(message->function, (gint64)value);
145 case PLUGIN_MESSAGE_ELEMENT_REQUEST_ID:
146 json_node_set_int(message->request_id, (gint64)value);
148 case PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE:
149 json_node_set_int(message->message_type, (gint64)value);
151 case PLUGIN_MESSAGE_ELEMENT_RESULT_CODE:
152 json_node_set_int(message->rcode, (gint64)value);
154 case PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE:
155 json_node_set_int(message->rmsg, (gint64)value);
164 int plugin_message_get_value_int(plugin_message_h message, plugin_message_element_e field, int *value)
166 if ((NULL == message) || (NULL == value))
173 case PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID:
174 *value = (int)json_node_get_int(message->context);
176 case PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME:
177 *value = (int)json_node_get_int(message->function);
179 case PLUGIN_MESSAGE_ELEMENT_REQUEST_ID:
180 *value = (int)json_node_get_int(message->request_id);
182 case PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE:
183 *value = (int)json_node_get_int(message->message_type);
185 case PLUGIN_MESSAGE_ELEMENT_RESULT_CODE:
186 *value = (int)json_node_get_int(message->rcode);
188 case PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE:
189 *value = (int)json_node_get_int(message->rmsg);
199 int plugin_message_set_value_number(plugin_message_h message, plugin_message_element_e field, pmnumber value)
202 if (NULL == message) {
207 case PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID:
208 json_node_set_int(message->context, (gint64)value);
210 case PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME:
211 json_node_set_int(message->function, (gint64)value);
213 case PLUGIN_MESSAGE_ELEMENT_REQUEST_ID:
214 json_node_set_int(message->request_id, (gint64)value);
216 case PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE:
217 json_node_set_int(message->message_type, (gint64)value);
219 case PLUGIN_MESSAGE_ELEMENT_RESULT_CODE:
220 json_node_set_int(message->rcode, (gint64)value);
222 case PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE:
223 json_node_set_int(message->rmsg, (gint64)value);
233 int plugin_message_get_value_number(plugin_message_h message, plugin_message_element_e field, pmnumber *value)
236 if ((NULL == message) || (NULL == value)) {
241 case PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID:
242 *value = (long long int)json_node_get_int(message->context);
244 case PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME:
245 *value = (long long int)json_node_get_int(message->function);
247 case PLUGIN_MESSAGE_ELEMENT_REQUEST_ID:
248 *value = (long long int)json_node_get_int(message->request_id);
250 case PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE:
251 *value = (long long int)json_node_get_int(message->message_type);
253 case PLUGIN_MESSAGE_ELEMENT_RESULT_CODE:
254 *value = (long long int)json_node_get_int(message->rcode);
256 case PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE:
257 *value = (long long int)json_node_get_int(message->rmsg);
267 int plugin_message_set_value_string(plugin_message_h message, plugin_message_element_e field, const char *value)
270 if (NULL == message) {
275 case PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID:
276 json_node_set_string(message->context, value);
278 case PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME:
279 json_node_set_string(message->function, value);
281 case PLUGIN_MESSAGE_ELEMENT_REQUEST_ID:
282 json_node_set_string(message->request_id, value);
284 case PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE:
285 json_node_set_string(message->message_type, value);
287 case PLUGIN_MESSAGE_ELEMENT_RESULT_CODE:
288 json_node_set_string(message->rcode, value);
290 case PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE:
291 json_node_set_string(message->rmsg, value);
301 int plugin_message_get_value_string(plugin_message_h message, plugin_message_element_e field, char **value)
304 if ((NULL == message) || (NULL == value)) {
309 case PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID:
310 *value = (char *)json_node_dup_string(message->context);
312 case PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME:
313 *value = (char *)json_node_dup_string(message->function);
315 case PLUGIN_MESSAGE_ELEMENT_REQUEST_ID:
316 *value = (char *)json_node_dup_string(message->request_id);
318 case PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE:
319 *value = (char *)json_node_dup_string(message->message_type);
321 case PLUGIN_MESSAGE_ELEMENT_RESULT_CODE:
322 *value = (char *)json_node_dup_string(message->rcode);
324 case PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE:
325 *value = (char *)json_node_dup_string(message->rmsg);
335 int plugin_message_set_value_bool(plugin_message_h message, plugin_message_element_e field, bool value)
338 if (NULL == message) {
343 case PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID:
344 json_node_set_boolean(message->context, value);
346 case PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME:
347 json_node_set_boolean(message->function, value);
349 case PLUGIN_MESSAGE_ELEMENT_REQUEST_ID:
350 json_node_set_boolean(message->request_id, value);
352 case PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE:
353 json_node_set_boolean(message->message_type, value);
355 case PLUGIN_MESSAGE_ELEMENT_RESULT_CODE:
356 json_node_set_boolean(message->rcode, value);
358 case PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE:
359 json_node_set_boolean(message->rmsg, value);
369 int plugin_message_get_value_bool(plugin_message_h message, plugin_message_element_e field, bool *value)
372 if ((NULL == message) || (NULL == value)) {
377 case PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID:
378 *value = (bool)json_node_get_boolean(message->context);
380 case PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME:
381 *value = (bool)json_node_get_boolean(message->function);
383 case PLUGIN_MESSAGE_ELEMENT_REQUEST_ID:
384 *value = (bool)json_node_get_boolean(message->request_id);
386 case PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE:
387 *value = (bool)json_node_get_boolean(message->message_type);
389 case PLUGIN_MESSAGE_ELEMENT_RESULT_CODE:
390 *value = (bool)json_node_get_boolean(message->rcode);
392 case PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE:
393 *value = (bool)json_node_get_boolean(message->rmsg);
404 int _plugin_message_set_parameter_value_int(JsonNode *node, int param_index, int value)
406 char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
411 snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
414 params = json_node_get_object(node);
416 // release memory of legacy node
418 // JsonNode *legacy_node = json_object_get_member(params, index);
419 // if (NULL != legacy_node)
421 // json_node_free(legacy_node);
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);
431 int _plugin_message_get_parameter_value_int(JsonNode *node, int param_index, int *value)
433 char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
438 snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
441 params = json_node_get_object(node);
443 JsonNode *legacy_node = json_object_get_member(params, index);
444 if (NULL != legacy_node)
446 *value = (int)json_node_get_int(legacy_node);
457 int _plugin_message_set_parameter_value_number(JsonNode *node, int param_index, pmnumber value)
460 char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
461 if (0 > param_index) {
464 snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
467 params = json_node_get_object(node);
469 /* release memory of legacy node */
471 JsonNode *legacy_node = json_object_get_member(params, index);
472 if (NULL != legacy_node)
474 json_node_free(legacy_node);
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);
485 int _plugin_message_get_parameter_value_number(JsonNode *node, int param_index, pmnumber *value)
488 char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
489 if (0 > param_index) {
492 snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
495 params = json_node_get_object(node);
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);
508 int _plugin_message_set_parameter_value_string(JsonNode *node, int param_index, const char *value)
511 char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
512 if (0 > param_index) {
515 snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
518 params = json_node_get_object(node);
520 /* release memory of legacy node */
522 JsonNode *legacy_node = json_object_get_member(params, index);
523 if (NULL != legacy_node)
525 json_node_free(legacy_node);
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);
536 int _plugin_message_get_parameter_value_string(JsonNode *node, int param_index, char **value)
539 char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
540 if (0 > param_index) {
543 snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
546 params = json_node_get_object(node);
548 JsonNode *legacy_node = json_object_get_member(params, index);
549 if (NULL != legacy_node) {
550 *value = (char *)json_node_dup_string(legacy_node);
558 int _plugin_message_set_parameter_value_bool(JsonNode *node, int param_index, bool value)
561 char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
562 if (0 > param_index) {
565 snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
568 params = json_node_get_object(node);
570 /* release memory of legacy node */
572 JsonNode *legacy_node = json_object_get_member(params, index);
573 if (NULL != legacy_node)
575 json_node_free(legacy_node);
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);
586 int _plugin_message_get_parameter_value_bool(JsonNode *node, int param_index, bool *value)
589 char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
590 if (0 > param_index) {
593 snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
596 params = json_node_get_object(node);
598 JsonNode *legacy_node = json_object_get_member(params, index);
599 if (NULL != legacy_node) {
600 *value = (bool)json_node_get_boolean(legacy_node);
609 int _plugin_message_set_parameter_value_array(JsonNode *node, int param_index, plugin_message_array_h value)
612 char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
613 if (0 > param_index) {
616 snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
619 params = json_node_get_object(node);
621 /* release memory of legacy node */
623 JsonNode *legacy_node = json_object_get_member(params, index);
624 if (NULL != legacy_node)
626 json_node_free(legacy_node);
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);
637 int _plugin_message_get_parameter_value_array(JsonNode *node, int param_index, plugin_message_array_h *value)
640 char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
641 if (0 > param_index) {
644 snprintf(index, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", param_index);
647 params = json_node_get_object(node);
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);
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");
663 _array->element = j_array;
665 PRINT_LOG("get object from json array\n");
666 JsonObject *job = json_array_get_object_element(j_array, 0);
668 int len = (int) json_object_get_size(job);
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");
680 char idx[PLUGIN_PARAMETER_BUF_SIZE_MAX];
682 for (i = 0; i < len; i++) {
683 snprintf(idx, PLUGIN_PARAMETER_BUF_SIZE_MAX, "%d", i+1);
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;
698 PRINT_LOG("node type (unknown)\n");
699 _array->types[i] = PLUGIN_DATA_TYPE_UNKNOWN;
702 PRINT_LOG("node type (no value node)\n");
703 _array->types[i] = PLUGIN_DATA_TYPE_UNKNOWN;
718 int plugin_message_set_param_int(plugin_message_h message, int param_index, int value)
725 return _plugin_message_set_parameter_value_int(message->parameter_mandatory, param_index, value);
728 int plugin_message_get_param_int(plugin_message_h message, int param_index, int *value)
730 if ((NULL == message) || (NULL == value))
735 return _plugin_message_get_parameter_value_int(message->parameter_mandatory, param_index, value);
739 int plugin_message_set_param_number(plugin_message_h message, int param_index, pmnumber value)
742 if (NULL == message) {
747 return _plugin_message_set_parameter_value_number(message->parameter_mandatory, param_index, value);
750 int plugin_message_get_param_number(plugin_message_h message, int param_index, pmnumber *value)
753 if ((NULL == message) || (NULL == value)) {
758 return _plugin_message_get_parameter_value_number(message->parameter_mandatory, param_index, value);
761 int plugin_message_set_param_string(plugin_message_h message, int param_index, const char *value)
764 if ((NULL == message) || (NULL == value)) {
769 return _plugin_message_set_parameter_value_string(message->parameter_mandatory, param_index, value);
772 int plugin_message_get_param_string(plugin_message_h message, int param_index, char **value)
775 if ((NULL == message) || (NULL == value)) {
780 return _plugin_message_get_parameter_value_string(message->parameter_mandatory, param_index, value);
783 int plugin_message_set_param_bool(plugin_message_h message, int param_index, bool value)
786 if (NULL == message) {
791 return _plugin_message_set_parameter_value_bool(message->parameter_mandatory, param_index, value);
794 int plugin_message_get_param_bool(plugin_message_h message, int param_index, bool *value)
797 if ((NULL == message) || (NULL == value)) {
802 return _plugin_message_get_parameter_value_bool(message->parameter_mandatory, param_index, value);
805 int plugin_message_set_param_array(plugin_message_h message, int param_index, plugin_message_array_h value)
808 if ((NULL == message) || (NULL == value)) {
813 return _plugin_message_set_parameter_value_array(message->parameter_mandatory, param_index, value);
816 int plugin_message_get_param_array(plugin_message_h message, int param_index, plugin_message_array_h *value)
819 if ((NULL == message) || (NULL == value)) {
824 return _plugin_message_get_parameter_value_array(message->parameter_mandatory, param_index, value);
828 int plugin_message_set_opt_param_int(plugin_message_h message, int param_index, int value)
837 return _plugin_message_set_parameter_value_int(message->parameter_optional, param_index, value);
840 int plugin_message_get_opt_param_int(plugin_message_h message, int param_index, int *value)
843 if ((NULL == message) || (NULL == value))
849 return _plugin_message_get_parameter_value_int(message->parameter_optional, param_index, value);
853 int plugin_message_set_opt_param_number(plugin_message_h message, int param_index, pmnumber value)
856 if (NULL == message) {
861 return _plugin_message_set_parameter_value_number(message->parameter_optional, param_index, value);
864 int plugin_message_get_opt_param_number(plugin_message_h message, int param_index, pmnumber *value)
867 if ((NULL == message) || (NULL == value)) {
872 return _plugin_message_get_parameter_value_number(message->parameter_optional, param_index, value);
875 int plugin_message_set_opt_param_string(plugin_message_h message, int param_index, const char *value)
878 if ((NULL == message) || (NULL == value)) {
883 return _plugin_message_set_parameter_value_string(message->parameter_optional, param_index, value);
886 int plugin_message_get_opt_param_string(plugin_message_h message, int param_index, char **value)
889 if ((NULL == message) || (NULL == value)) {
894 return _plugin_message_get_parameter_value_string(message->parameter_optional, param_index, value);
897 int plugin_message_set_opt_param_bool(plugin_message_h message, int param_index, bool value)
900 if (NULL == message) {
905 return _plugin_message_set_parameter_value_bool(message->parameter_optional, param_index, value);
908 int plugin_message_get_opt_param_bool(plugin_message_h message, int param_index, bool *value)
911 if ((NULL == message) || (NULL == value)) {
916 return _plugin_message_get_parameter_value_bool(message->parameter_optional, param_index, value);
919 int plugin_message_set_opt_param_array(plugin_message_h message, int param_index, plugin_message_array_h value)
922 if ((NULL == message) || (NULL == value)) {
927 return _plugin_message_set_parameter_value_array(message->parameter_optional, param_index, value);
930 int plugin_message_get_opt_param_array(plugin_message_h message, int param_index, plugin_message_array_h *value)
933 if ((NULL == message) || (NULL == value)) {
938 return _plugin_message_get_parameter_value_array(message->parameter_optional, param_index, value);
941 char *_json_serialize_by_jnode(JsonNode *total_node)
944 JsonGenerator *gen = json_generator_new();
945 json_generator_set_root(gen, total_node);
949 t_data = json_generator_to_data(gen, &len);
958 JsonNode *_json_deserialize_by_data(const char *data)
961 JsonParser *parser = json_parser_new();
962 json_parser_load_from_data(parser, data, strlen(data), NULL);
966 JsonNode *node = json_parser_get_root(parser);
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");
982 int plugin_message_serialize(plugin_message_h message, char **data)
985 if ((NULL == message) || (NULL == data)) {
989 JsonObject *obj = json_object_new();
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);
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);
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);
1018 JsonNode *node = json_node_new(JSON_NODE_OBJECT);
1019 json_node_take_object(node, obj);
1022 _data = _json_serialize_by_jnode(node);
1024 json_node_free(node);
1025 json_object_unref(obj);
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);
1041 int plugin_message_deserialize(const char *data, plugin_message_h *message)
1044 if ((NULL == message) || (NULL == data)) {
1048 plugin_message_h _msg = (plugin_message_h) calloc(1, sizeof(struct _plugin_message_s));
1053 JsonNode *root_node = _json_deserialize_by_data(data);
1055 JsonObject *root_object = json_node_get_object(root_node);
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);
1068 json_node_free(root_node);
1073 int plugin_message_array_create(const plugin_data_type *type_string, plugin_message_array_h *array)
1076 if ((NULL == type_string) || (NULL == array)) {
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:
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));
1094 if ((NULL == _array) || (NULL == _types)) {
1100 _array->element = json_array_new();
1102 _array->types = _types;
1103 strncpy(_array->types, type_string, strlen(type_string));
1111 void plugin_message_array_destroy(plugin_message_array_h array)
1114 if (NULL != array) {
1115 json_array_unref(array->element);
1122 int plugin_message_array_add_element(plugin_message_array_h array, ...)
1125 if (NULL == array) {
1129 plugin_data_type *types = array->types;
1131 int count = strlen(types);
1133 char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
1135 JsonObject *obj = json_object_new();
1138 PRINT_LOG("[%d]count : %d\n", __LINE__, count);
1141 va_start(vl, array);
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]);
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);
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);
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);
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);
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);
1185 PRINT_LOG("array length : %u\n", json_object_get_size(obj));
1186 json_array_add_object_element(array->element, obj);
1192 int plugin_message_array_get_element(plugin_message_array_h array, int idx, ...)
1195 if ((NULL == array) || (1 > idx)) {
1199 if (json_array_get_length(array->element) < idx) {
1203 JsonObject *obj = json_array_get_object_element(array->element, idx-1);
1209 plugin_data_type *types = array->types;
1211 int count = strlen(types);
1213 char index[PLUGIN_PARAMETER_BUF_SIZE_MAX+1] = {0, };
1217 PRINT_LOG("[%d]count : %d\n", __LINE__, count);
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);
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);
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);
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);
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);
1248 PRINT_LOG("[%d]index : %s\n", __LINE__, index);