client: Add support for json-structured parameters
authorSangchul Lee <sc11.lee@samsung.com>
Fri, 21 Apr 2023 07:14:34 +0000 (16:14 +0900)
committer이상철/Tizen Platform Lab(SR)/삼성전자 <sc11.lee@samsung.com>
Tue, 25 Apr 2023 23:05:25 +0000 (08:05 +0900)
Signed-off-by: Sangchul Lee <sc11.lee@samsung.com>
meson.build
packaging/espp-service.spec
src/client/espp_service_client_msg.c [new file with mode: 0644]
src/client/espp_service_client_priv.h
src/client/espp_service_client_socket.c
src/client/meson.build
src/common/espp_service_common.c
src/common/espp_service_common.h

index e896ffede66b19536e9c76885b77ced425e5a8e8..5a0a919096640f53d161da3da8354690a407c186 100644 (file)
@@ -39,6 +39,9 @@ message('================================================')
 glib_dep = dependency('glib-2.0', required: true)
 common_deps += [glib_dep]
 
+json_dep = dependency('json-glib-1.0', required: true)
+common_deps += [json_dep]
+
 subdir('src')
 
 configure_file(output: 'config.h', configuration: conf_data)
index bb2f79e1c55ec89589a875aee402f654a177581a..7458bb76cfbb4925d352423bc0456fa063e89999 100644 (file)
@@ -13,6 +13,7 @@ BuildRequires: meson
 BuildRequires: pkgconfig(dlog)
 BuildRequires: pkgconfig(glib-2.0)
 BuildRequires: pkgconfig(gio-2.0)
+BuildRequires: pkgconfig(json-glib-1.0)
 BuildRequires: pkgconfig(esplusplayer)
 
 %description
diff --git a/src/client/espp_service_client_msg.c b/src/client/espp_service_client_msg.c
new file mode 100644 (file)
index 0000000..a1a1aec
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "espp_service_client_priv.h"
+#include <stdarg.h>
+#include <json-glib/json-glib.h>
+
+/* Use g_free() to release the return value. */
+static gchar *get_string_from_json_object(JsonObject *object)
+{
+       g_autoptr(JsonNode) root = NULL;
+       g_autoptr(JsonGenerator) generator = NULL;
+
+       ASSERT(object);
+
+       root = json_node_init_object(json_node_alloc(), object);
+       generator = json_generator_new();
+       json_generator_set_root(generator, root);
+       return json_generator_to_data(generator, NULL);
+}
+
+static JsonObject *make_json_object_params(va_list ap, const char *formats, JsonObject *object)
+{
+       char c;
+       JsonObject *params = json_object_new();
+
+       ASSERT(object);
+
+       while (*formats) {
+               switch ((c = *formats++)) {
+               case 'b':
+                       json_object_set_boolean_member(params,
+                               data_type_strs[ESPP_SERVICE_DATA_TYPE_BOOL], (gboolean)va_arg(ap, int));
+                       break;
+               case 'i':
+                       json_object_set_int_member(params,
+                               data_type_strs[ESPP_SERVICE_DATA_TYPE_INT], (gint64)va_arg(ap, int));
+                       break;
+               case 'l':
+                       json_object_set_int_member(params,
+                               data_type_strs[ESPP_SERVICE_DATA_TYPE_INT64], (gint64)va_arg(ap, int64_t));
+                       break;
+               case 'u':
+                       json_object_set_int_member(params,
+                               data_type_strs[ESPP_SERVICE_DATA_TYPE_UINT], (gint64)va_arg(ap, unsigned int));
+                       break;
+               case 'k':
+                       json_object_set_int_member(params,
+                               data_type_strs[ESPP_SERVICE_DATA_TYPE_UINT64], (gint64)va_arg(ap, uint64_t));
+                       break;
+               case 'd':
+                       json_object_set_double_member(params,
+                               data_type_strs[ESPP_SERVICE_DATA_TYPE_DOUBLE], (gdouble)va_arg(ap, double));
+                       break;
+               case 's':
+                       json_object_set_string_member(params,
+                               data_type_strs[ESPP_SERVICE_DATA_TYPE_STRING], (const gchar*)va_arg(ap, char *));
+                       break;
+               default:
+                       LOG_ERROR("not supported format[%c]", c);
+                       json_object_unref(params);
+                       json_object_unref(object);
+                       return NULL;
+               }
+       }
+
+       json_object_set_object_member(object, "params", params);
+
+       return object;
+}
+
+gchar *espp_service_client_msg_params_new(const char *formats, ...)
+{
+       gchar *result = NULL;
+       JsonObject *obj;
+       va_list ap;
+
+       ASSERT(formats);
+
+       obj = json_object_new();
+
+       va_start(ap, formats);
+       obj = make_json_object_params(ap, formats, obj);
+       va_end(ap);
+
+       if (!obj)
+               return NULL;
+
+       result = get_string_from_json_object(obj);
+       json_object_unref(obj);
+
+       LOG_DEBUG("params: %s", result);
+
+       return result;
+}
\ No newline at end of file
index ff8216946869bd3a3613d4a34f3a9734bcf742b3..6a205b94576388a437e7384bf88d2b0b363fa2ac 100644 (file)
@@ -32,6 +32,10 @@ typedef struct _espp_s {
        int fd;
 } espp_s;
 
+/* message */
+gchar *espp_service_client_msg_params_new(const char *formats, ...);
+
+/* socket */
 int espp_service_client_socket_request_create(espp_s *espp);
 int espp_service_client_socket_request_destroy(espp_s *espp);
 int espp_service_client_socket_request_start(espp_s *espp);
index 6295640be1d1d786a4107026e57b6553c36dac4c..00cb4dbe0abacc5e3893eb0ce2b8c58d0b37c0d6 100644 (file)
@@ -31,13 +31,15 @@ do { \
        x_dst[x_size - 1] = '\0';\
 } while (0) \
 
-#define FILL_SOCKET_MSG_PARAMS(x_msg, x_params) \
+#define FILL_SOCKET_MSG_PARAMS(x_msg, x_request, ...) \
 do { \
+       gchar *params = espp_service_client_msg_params_new(requests[x_request].param_formats, ##__VA_ARGS__); \
        memset(&x_msg.params, 0x00, MAX_PARAMS_LEN); \
-       STRNCPY(x_msg.params, x_params, MAX_PARAMS_LEN); \
+       if (!params) break; \
+       STRNCPY(x_msg.params, params, MAX_PARAMS_LEN); \
+       g_free(params); \
 } while (0) \
 
-
 #define FILL_SOCKET_MSG_REQUEST(x_msg, x_request) \
 do { \
        x_msg.request = x_request; \
index 61c2ce0c6d5dad14ab36686a8d78e8a4a63fbe64..e3c1b51b29d619c09d2aed5584be5ef6f7828c10 100644 (file)
@@ -10,6 +10,7 @@ espp_service_client_sources = files([
   '../common/espp_service_common.c',
   'espp_service_client.c',
   'espp_service_client_socket.c',
+  'espp_service_client_msg.c',
 ])
 
 espp_dep = dependency('esplusplayer', required: true)
index 6b3a63ef5e45317edfff3d4fa8486341a92840ac..42b7b3253a03383cd59ec7e3710f1dd8c180827c 100644 (file)
 
 #include "espp_service_common.h"
 
-espp_service_request_s requests[] = { /* str, num_of_params */
-       [ESPP_SERVICE_REQUEST_CREATE] = { "Create", 0 },
-       [ESPP_SERVICE_REQUEST_DESTROY] = {"Destroy", 0 },
-       [ESPP_SERVICE_REQUEST_START] = { "Start", 0 },
+espp_service_request_s requests[] = { /* str, param_formats - 'b':bool, 'i':int, 'l':int64, 'u':uint, 'k':uint64, 'd':double, 's':string) */
+       [ESPP_SERVICE_REQUEST_CREATE] = { "Create", NULL },
+       [ESPP_SERVICE_REQUEST_DESTROY] = {"Destroy", NULL },
+       [ESPP_SERVICE_REQUEST_START] = { "Start", NULL },
+};
+
+const char *data_type_strs[] = {
+       [ESPP_SERVICE_DATA_TYPE_BOOL] = "bool",
+       [ESPP_SERVICE_DATA_TYPE_INT] = "int",
+       [ESPP_SERVICE_DATA_TYPE_INT64] = "int64",
+       [ESPP_SERVICE_DATA_TYPE_UINT] = "uint",
+       [ESPP_SERVICE_DATA_TYPE_UINT64] = "uint64",
+       [ESPP_SERVICE_DATA_TYPE_DOUBLE] = "double",
+       [ESPP_SERVICE_DATA_TYPE_STRING] = "string",
 };
\ No newline at end of file
index a7e3122f534604e798db28d4eef20ab7ed0e9bd9..179645646cbb4f6d9d4fea24b4bd91a738854916 100644 (file)
@@ -30,6 +30,7 @@
 #include <stdlib.h>
 #include <glib.h>
 #include <stdbool.h>
+#include <stdint.h>
 #ifdef USE_DLOG
 #include <dlog.h>
 #endif
@@ -117,7 +118,7 @@ do {\
 } while (0)
 
 #define ESPP_SERVICE_SOCK ESPP_SVC_SOCK_PATH
-#define MAX_PARAMS_LEN 128
+#define MAX_PARAMS_LEN 256
 #define MAX_ERROR_LEN 64
 
 typedef enum {
@@ -126,6 +127,16 @@ typedef enum {
        ESPP_SERVICE_REQUEST_START,
 } espp_service_request_e;
 
+enum {
+       ESPP_SERVICE_DATA_TYPE_BOOL,
+       ESPP_SERVICE_DATA_TYPE_INT,
+       ESPP_SERVICE_DATA_TYPE_INT64,
+       ESPP_SERVICE_DATA_TYPE_UINT,
+       ESPP_SERVICE_DATA_TYPE_UINT64,
+       ESPP_SERVICE_DATA_TYPE_DOUBLE,
+       ESPP_SERVICE_DATA_TYPE_STRING,
+};
+
 typedef struct {
        espp_service_request_e request;
        char params[MAX_PARAMS_LEN];
@@ -137,10 +148,11 @@ typedef struct {
 
 typedef struct {
        const char *str;
-       int num_of_params;
+       const char *param_formats;
 } espp_service_request_s;
 
 extern espp_service_request_s requests[];
+extern const char *data_type_strs[];
 
 #ifdef __cplusplus
 }