[CodeClean] separate util functions
authorJaeyun <jy1210.jung@samsung.com>
Wed, 24 Aug 2022 09:39:56 +0000 (18:39 +0900)
committerjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Wed, 31 Aug 2022 02:04:23 +0000 (11:04 +0900)
Separate utility functions - header for log and utility.

Signed-off-by: Jaeyun <jy1210.jung@samsung.com>
src/CMakeLists.txt
src/libnnstreamer-edge/nnstreamer-edge-common.c
src/libnnstreamer-edge/nnstreamer-edge-common.h
src/libnnstreamer-edge/nnstreamer-edge-internal.c
src/libnnstreamer-edge/nnstreamer-edge-log.h [new file with mode: 0644]
src/libnnstreamer-edge/nnstreamer-edge-mqtt.c
src/libnnstreamer-edge/nnstreamer-edge-util.c [new file with mode: 0644]
src/libnnstreamer-edge/nnstreamer-edge-util.h [new file with mode: 0644]
tests/unittest_nnstreamer-edge.cc

index 4f42aa56049e4481b7403abb36260afbd66ae43f..4626f2dfd5a5bb58dff791e4f903917cddcb8e6f 100644 (file)
@@ -3,6 +3,7 @@ SET(NNS_EDGE_LIB_NAME nnstreamer-edge)
 SET(NNS_EDGE_SRCS
     ${NNS_EDGE_SRC_DIR}/nnstreamer-edge-common.c
     ${NNS_EDGE_SRC_DIR}/nnstreamer-edge-internal.c
+    ${NNS_EDGE_SRC_DIR}/nnstreamer-edge-util.c
 )
 
 IF(ENABLE_PAHO_MQTT)
index 227ecc9147643f76281c84ec10ace529ad044f00..4bce06d63fe7743c11b7ed4e5dcdbbf90d6ad2a7 100644 (file)
  * @bug    No known bugs except for NYI items
  */
 
-#define _GNU_SOURCE
-#include <stdio.h>
-
 #include "nnstreamer-edge-common.h"
-
-/**
- * @brief Internal util function to get available port number.
- */
-int
-nns_edge_get_available_port (void)
-{
-  struct sockaddr_in sin;
-  int port = 0, sock;
-  socklen_t len = sizeof (struct sockaddr);
-
-  sin.sin_family = AF_INET;
-  sin.sin_addr.s_addr = INADDR_ANY;
-  sin.sin_port = 0;
-
-  sock = socket (AF_INET, SOCK_STREAM, 0);
-  if (sock < 0) {
-    nns_edge_loge ("Failed to get available port, socket creation failure.");
-    return 0;
-  }
-
-  if (bind (sock, (struct sockaddr *) &sin, sizeof (struct sockaddr)) == 0) {
-    if (getsockname (sock, (struct sockaddr *) &sin, &len) == 0) {
-      port = ntohs (sin.sin_port);
-      nns_edge_logi ("Available port number: %d", port);
-    } else {
-      nns_edge_logw ("Failed to read local socket info.");
-    }
-  }
-  close (sock);
-
-  return port;
-}
-
-/**
- * @brief Get host string (host:port). Caller should release returned string using nns_edge_free().
- */
-char *
-nns_edge_get_host_string (const char *host, const int port)
-{
-  return nns_edge_strdup_printf ("%s:%d", host, port);
-}
-
-/**
- * @brief Parse string and get host string (host:port).
- */
-void
-nns_edge_parse_host_string (const char *host_str, char **host, int *port)
-{
-  char *p = strchr (host_str, ':');
-
-  if (p) {
-    *host = nns_edge_strndup (host_str, (p - host_str));
-    *port = (int) strtoll (p + 1, NULL, 10);
-  }
-}
-
-/**
- * @brief Parse string and get port number. Return negative value when failed to get port number.
- */
-int
-nns_edge_parse_port_number (const char *port_str)
-{
-  int port;
-
-  if (!port_str)
-    return -1;
-
-  port = (int) strtoll (port_str, NULL, 10);
-
-  if (port <= 0 || port > 65535) {
-    nns_edge_loge ("Invalid port number %d.", port);
-    port = -1;
-  }
-
-  return port;
-}
-
-/**
- * @brief Free allocated memory.
- */
-void
-nns_edge_free (void *data)
-{
-  if (data)
-    free (data);
-}
-
-/**
- * @brief Allocate new memory and copy bytes.
- * @note Caller should release newly allocated memory using nns_edge_free().
- */
-void *
-nns_edge_memdup (const void *data, size_t size)
-{
-  void *mem = NULL;
-
-  if (data && size > 0) {
-    mem = malloc (size);
-
-    if (mem) {
-      memcpy (mem, data, size);
-    } else {
-      nns_edge_loge ("Failed to allocate memory (%zd).", size);
-    }
-  }
-
-  return mem;
-}
-
-/**
- * @brief Allocate new memory and copy string.
- * @note Caller should release newly allocated string using nns_edge_free().
- */
-char *
-nns_edge_strdup (const char *str)
-{
-  char *new_str = NULL;
-
-  if (str)
-    new_str = nns_edge_strndup (str, strlen (str));
-
-  return new_str;
-}
-
-/**
- * @brief Allocate new memory and copy bytes of string.
- * @note Caller should release newly allocated string using nns_edge_free().
- */
-char *
-nns_edge_strndup (const char *str, size_t len)
-{
-  char *new_str = NULL;
-
-  if (str) {
-    new_str = (char *) malloc (len + 1);
-
-    if (new_str) {
-      strncpy (new_str, str, len);
-      new_str[len] = '\0';
-    } else {
-      nns_edge_loge ("Failed to allocate memory (%zd).", len + 1);
-    }
-  }
-
-  return new_str;
-}
-
-/**
- * @brief Allocate new memory and print formatted string.
- * @note Caller should release newly allocated string using nns_edge_free().
- */
-char *
-nns_edge_strdup_printf (const char *format, ...)
-{
-  char *new_str = NULL;
-  va_list args;
-  int len;
-
-  va_start (args, format);
-  len = vasprintf (&new_str, format, args);
-  if (len < 0)
-    new_str = NULL;
-  va_end (args);
-
-  return new_str;
-}
+#include "nnstreamer-edge-log.h"
+#include "nnstreamer-edge-util.h"
 
 /**
  * @brief Internal function to find node in the list.
index 32c1c70d4afea727195d40a2e0de9c7cc5692b1f..37b37ea5957ef62a78ea90ba079a3a4b09040d06 100644 (file)
@@ -9,42 +9,19 @@
  * @author Gichan Jang <gichan2.jang@samsung.com>
  * @bug    No known bugs except for NYI items
  * @note   This file is internal header for nnstreamer edge utils. DO NOT export this file.
+ * @todo   Rename this to nnstreamer-edge-meta after separating util functions.
  */
 
 #ifndef __NNSTREAMER_EDGE_COMMON_H__
 #define __NNSTREAMER_EDGE_COMMON_H__
 
 #include <glib.h> /** @todo remove glib */
-#include <fcntl.h>
-#include <netinet/tcp.h>
-#include <netinet/in.h>
-#include <pthread.h>
-#include <unistd.h>
 #include "nnstreamer-edge.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif /* __cplusplus */
 
-/**
- * @brief Utility to silence unused parameter warning for intentionally unused parameters (e.g., callback functions of a framework)
- */
-#ifndef UNUSED
-#define UNUSED(expr) do { (void)(expr); } while (0)
-#endif
-
-#define STR_IS_VALID(s) ((s) && (s)[0] != '\0')
-#define SAFE_FREE(p) do { if (p) { free (p); (p) = NULL; } } while (0)
-
-#define NNS_EDGE_MAGIC 0xfeedfeed
-#define NNS_EDGE_MAGIC_DEAD 0xdeaddead
-#define NNS_EDGE_MAGIC_IS_VALID(h) ((h) && (h)->magic == NNS_EDGE_MAGIC)
-
-#define nns_edge_lock_init(h) do { pthread_mutex_init (&(h)->lock, NULL); } while (0)
-#define nns_edge_lock_destroy(h) do { pthread_mutex_destroy (&(h)->lock); } while (0)
-#define nns_edge_lock(h) do { pthread_mutex_lock (&(h)->lock); } while (0)
-#define nns_edge_unlock(h) do { pthread_mutex_unlock (&(h)->lock); } while (0)
-
 typedef void *nns_edge_broker_h;
 
 /**
@@ -98,120 +75,6 @@ typedef struct {
   nns_edge_raw_data_s data;
 } nns_edge_event_s;
 
-#define TAG_NAME "nnstreamer-edge"
-
-#if defined(__TIZEN__)
-#include <dlog.h>
-
-#define nns_edge_logd(...) dlog_print (DLOG_DEBUG, TAG_NAME, __VA_ARGS__)
-#define nns_edge_logi(...) dlog_print (DLOG_INFO, TAG_NAME, __VA_ARGS__)
-#define nns_edge_logw(...) dlog_print (DLOG_WARN, TAG_NAME, __VA_ARGS__)
-#define nns_edge_loge(...) dlog_print (DLOG_ERROR, TAG_NAME, __VA_ARGS__)
-#define nns_edge_logf(...) dlog_print (DLOG_FATAL, TAG_NAME, __VA_ARGS__)
-#elif defined(__ANDROID__)
-#include <android/log.h>
-
-#define nns_edge_logd(...) __android_log_print (ANDROID_LOG_DEBUG, TAG_NAME, __VA_ARGS__)
-#define nns_edge_logi(...) __android_log_print (ANDROID_LOG_INFO, TAG_NAME, __VA_ARGS__)
-#define nns_edge_logw(...) __android_log_print (ANDROID_LOG_WARN, TAG_NAME, __VA_ARGS__)
-#define nns_edge_loge(...) __android_log_print (ANDROID_LOG_ERROR, TAG_NAME, __VA_ARGS__)
-#define nns_edge_logf(...) __android_log_print (ANDROID_LOG_FATAL, TAG_NAME, __VA_ARGS__)
-#else
-/**
- * @brief Internal enumeration for log message.
- */
-typedef enum {
-  NE_LOG_DEBUG = 0,
-  NE_LOG_INFO,
-  NE_LOG_WARNING,
-  NE_LOG_ERROR,
-  NE_LOG_FATAL,
-  NE_LOG_NONE
-} nns_edge_log_level_e;
-
-/**
- * @brief Internal util function to print log message.
- */
-static inline void
-nns_edge_print_log (nns_edge_log_level_e level, const char *fmt, ...)
-{
-  const char *level_str[] = {
-    [NE_LOG_DEBUG] = "DEBUG",
-    [NE_LOG_INFO] = "INFO",
-    [NE_LOG_WARNING] = "WARNING",
-    [NE_LOG_ERROR] = "ERROR",
-    [NE_LOG_FATAL] = "FATAL",
-    [NE_LOG_NONE] = "DEBUG",
-  };
-
-  va_list args;
-
-  /** @todo expand log util and handle log level (debug, release) */
-  va_start (args, fmt);
-
-  printf ("[%s][%s] ", level_str[level], TAG_NAME);
-  vprintf (fmt, args);
-  printf ("\n");
-
-  va_end (args);
-}
-
-#define nns_edge_logi(...) nns_edge_print_log (NE_LOG_INFO, __VA_ARGS__)
-#define nns_edge_logw(...) nns_edge_print_log (NE_LOG_WARNING, __VA_ARGS__)
-#define nns_edge_loge(...) nns_edge_print_log (NE_LOG_ERROR, __VA_ARGS__)
-#define nns_edge_logd(...) nns_edge_print_log (NE_LOG_DEBUG, __VA_ARGS__)
-#define nns_edge_logf(...) nns_edge_print_log (NE_LOG_FATAL, __VA_ARGS__)
-#endif
-
-/**
- * @brief Internal util function to get available port number.
- */
-int nns_edge_get_available_port (void);
-
-/**
- * @brief Get host string (host:port). Caller should release returned string using nns_edge_free().
- */
-char *nns_edge_get_host_string (const char *host, const int port);
-
-/**
- * @brief Parse string and get host string (host:port).
- */
-void nns_edge_parse_host_string (const char *host_str, char **host, int *port);
-
-/**
- * @brief Parse string and get port number. Return negative value when failed to get port number.
- */
-int nns_edge_parse_port_number (const char *port_str);
-
-/**
- * @brief Free allocated memory.
- */
-void nns_edge_free (void *data);
-
-/**
- * @brief Allocate new memory and copy bytes.
- * @note Caller should release newly allocated memory using nns_edge_free().
- */
-void *nns_edge_memdup (const void *data, size_t size);
-
-/**
- * @brief Allocate new memory and copy string.
- * @note Caller should release newly allocated string using nns_edge_free().
- */
-char *nns_edge_strdup (const char *str);
-
-/**
- * @brief Allocate new memory and copy bytes of string.
- * @note Caller should release newly allocated string using nns_edge_free().
- */
-char *nns_edge_strndup (const char *str, size_t len);
-
-/**
- * @brief Allocate new memory and print formatted string.
- * @note Caller should release newly allocated string using nns_edge_free().
- */
-char *nns_edge_strdup_printf (const char *format, ...);
-
 /**
  * @brief Internal function to initialize metadata structure.
  */
index 6fb47751ad0b4ad2dd9a7d07efe9d3296884fac9..e5d05a6e722cdc0fec1640c731c8c01899e56d82 100644 (file)
@@ -14,7 +14,8 @@
 #include <netdb.h>
 #include <sys/poll.h>
 
-#include "nnstreamer-edge-common.h"
+#include "nnstreamer-edge-log.h"
+#include "nnstreamer-edge-util.h"
 #include "nnstreamer-edge-internal.h"
 
 #define N_BACKLOG 10
diff --git a/src/libnnstreamer-edge/nnstreamer-edge-log.h b/src/libnnstreamer-edge/nnstreamer-edge-log.h
new file mode 100644 (file)
index 0000000..a1422b5
--- /dev/null
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+/**
+ * Copyright (C) 2022 Samsung Electronics Co., Ltd. All Rights Reserved.
+ *
+ * @file   nnstreamer-edge-log.h
+ * @date   24 August 2022
+ * @brief  Log util.
+ * @see    https://github.com/nnstreamer/nnstreamer-edge
+ * @author Jaeyun Jung <jy1210.jung@samsung.com>
+ * @note   This file is internal header for nnstreamer-edge. DO NOT export this file.
+ */
+
+#ifndef __NNSTREAMER_EDGE_LOG_H__
+#define __NNSTREAMER_EDGE_LOG_H__
+
+#include <stdio.h>
+#include <stdarg.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+#define TAG_NAME "nnstreamer-edge"
+
+#if defined(__TIZEN__)
+#include <dlog.h>
+
+#define nns_edge_logd(...) dlog_print (DLOG_DEBUG, TAG_NAME, __VA_ARGS__)
+#define nns_edge_logi(...) dlog_print (DLOG_INFO, TAG_NAME, __VA_ARGS__)
+#define nns_edge_logw(...) dlog_print (DLOG_WARN, TAG_NAME, __VA_ARGS__)
+#define nns_edge_loge(...) dlog_print (DLOG_ERROR, TAG_NAME, __VA_ARGS__)
+#define nns_edge_logf(...) dlog_print (DLOG_FATAL, TAG_NAME, __VA_ARGS__)
+#elif defined(__ANDROID__)
+#include <android/log.h>
+
+#define nns_edge_logd(...) __android_log_print (ANDROID_LOG_DEBUG, TAG_NAME, __VA_ARGS__)
+#define nns_edge_logi(...) __android_log_print (ANDROID_LOG_INFO, TAG_NAME, __VA_ARGS__)
+#define nns_edge_logw(...) __android_log_print (ANDROID_LOG_WARN, TAG_NAME, __VA_ARGS__)
+#define nns_edge_loge(...) __android_log_print (ANDROID_LOG_ERROR, TAG_NAME, __VA_ARGS__)
+#define nns_edge_logf(...) __android_log_print (ANDROID_LOG_FATAL, TAG_NAME, __VA_ARGS__)
+#else
+/**
+ * @brief Internal enumeration for log message.
+ */
+typedef enum {
+  NE_LOG_DEBUG = 0,
+  NE_LOG_INFO,
+  NE_LOG_WARNING,
+  NE_LOG_ERROR,
+  NE_LOG_FATAL,
+  NE_LOG_NONE
+} nns_edge_log_level_e;
+
+/**
+ * @brief Internal util function to print log message.
+ */
+static inline void
+nns_edge_print_log (nns_edge_log_level_e level, const char *fmt, ...)
+{
+  const char *level_str[] = {
+    [NE_LOG_DEBUG] = "DEBUG",
+    [NE_LOG_INFO] = "INFO",
+    [NE_LOG_WARNING] = "WARNING",
+    [NE_LOG_ERROR] = "ERROR",
+    [NE_LOG_FATAL] = "FATAL",
+    [NE_LOG_NONE] = "DEBUG",
+  };
+
+  va_list args;
+
+  /** @todo expand log util and handle log level (debug, release) */
+  va_start (args, fmt);
+
+  printf ("[%s][%s] ", level_str[level], TAG_NAME);
+  vprintf (fmt, args);
+  printf ("\n");
+
+  va_end (args);
+}
+
+#define nns_edge_logi(...) nns_edge_print_log (NE_LOG_INFO, __VA_ARGS__)
+#define nns_edge_logw(...) nns_edge_print_log (NE_LOG_WARNING, __VA_ARGS__)
+#define nns_edge_loge(...) nns_edge_print_log (NE_LOG_ERROR, __VA_ARGS__)
+#define nns_edge_logd(...) nns_edge_print_log (NE_LOG_DEBUG, __VA_ARGS__)
+#define nns_edge_logf(...) nns_edge_print_log (NE_LOG_FATAL, __VA_ARGS__)
+#endif
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+#endif /* __NNSTREAMER_EDGE_LOG_H__ */
index 9f6a009944120d83a333587c0e215f8929e364e0..09acaebe31ff7803b4728a0e2ce949842c4600c2 100644 (file)
 #error "This file can be built with Paho MQTT library."
 #endif
 
-#include <unistd.h>
 #include <MQTTAsync.h>
-#include "nnstreamer-edge-common.h"
 #include "nnstreamer-edge-internal.h"
+#include "nnstreamer-edge-log.h"
+#include "nnstreamer-edge-util.h"
 
 /**
  * @brief Data structure for mqtt broker handle.
@@ -43,7 +43,6 @@ mqtt_cb_message_arrived (void *context, char *topic, int topic_len,
 
   UNUSED (topic);
   UNUSED (topic_len);
-  UNUSED (message);
   eh = (nns_edge_handle_s *) context;
 
   if (!NNS_EDGE_MAGIC_IS_VALID (eh) || !eh->broker_h) {
diff --git a/src/libnnstreamer-edge/nnstreamer-edge-util.c b/src/libnnstreamer-edge/nnstreamer-edge-util.c
new file mode 100644 (file)
index 0000000..9cfa4a8
--- /dev/null
@@ -0,0 +1,183 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+/**
+ * Copyright (C) 2022 Samsung Electronics Co., Ltd. All Rights Reserved.
+ *
+ * @file   nnstreamer-edge-util.c
+ * @date   24 August 2022
+ * @brief  Utility functions.
+ * @see    https://github.com/nnstreamer/nnstreamer-edge
+ * @author Jaeyun Jung <jy1210.jung@samsung.com>
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+
+#include "nnstreamer-edge-log.h"
+#include "nnstreamer-edge-util.h"
+
+/**
+ * @brief Internal util function to get available port number.
+ */
+int
+nns_edge_get_available_port (void)
+{
+  struct sockaddr_in sin;
+  int port = 0, sock;
+  socklen_t len = sizeof (struct sockaddr);
+
+  sin.sin_family = AF_INET;
+  sin.sin_addr.s_addr = INADDR_ANY;
+  sin.sin_port = 0;
+
+  sock = socket (AF_INET, SOCK_STREAM, 0);
+  if (sock < 0) {
+    nns_edge_loge ("Failed to get available port, socket creation failure.");
+    return 0;
+  }
+
+  if (bind (sock, (struct sockaddr *) &sin, sizeof (struct sockaddr)) == 0) {
+    if (getsockname (sock, (struct sockaddr *) &sin, &len) == 0) {
+      port = ntohs (sin.sin_port);
+      nns_edge_logi ("Available port number: %d", port);
+    } else {
+      nns_edge_logw ("Failed to read local socket info.");
+    }
+  }
+  close (sock);
+
+  return port;
+}
+
+/**
+ * @brief Get host string (host:port). Caller should release returned string using nns_edge_free().
+ */
+char *
+nns_edge_get_host_string (const char *host, const int port)
+{
+  return nns_edge_strdup_printf ("%s:%d", host, port);
+}
+
+/**
+ * @brief Parse string and get host string (host:port).
+ */
+void
+nns_edge_parse_host_string (const char *host_str, char **host, int *port)
+{
+  char *p = strchr (host_str, ':');
+
+  if (p) {
+    *host = nns_edge_strndup (host_str, (p - host_str));
+    *port = (int) strtoll (p + 1, NULL, 10);
+  }
+}
+
+/**
+ * @brief Parse string and get port number. Return negative value when failed to get port number.
+ */
+int
+nns_edge_parse_port_number (const char *port_str)
+{
+  int port;
+
+  if (!port_str)
+    return -1;
+
+  port = (int) strtoll (port_str, NULL, 10);
+
+  if (port <= 0 || port > 65535) {
+    nns_edge_loge ("Invalid port number %d.", port);
+    port = -1;
+  }
+
+  return port;
+}
+
+/**
+ * @brief Free allocated memory.
+ */
+void
+nns_edge_free (void *data)
+{
+  if (data)
+    free (data);
+}
+
+/**
+ * @brief Allocate new memory and copy bytes.
+ * @note Caller should release newly allocated memory using nns_edge_free().
+ */
+void *
+nns_edge_memdup (const void *data, size_t size)
+{
+  void *mem = NULL;
+
+  if (data && size > 0) {
+    mem = malloc (size);
+
+    if (mem) {
+      memcpy (mem, data, size);
+    } else {
+      nns_edge_loge ("Failed to allocate memory (%zd).", size);
+    }
+  }
+
+  return mem;
+}
+
+/**
+ * @brief Allocate new memory and copy string.
+ * @note Caller should release newly allocated string using nns_edge_free().
+ */
+char *
+nns_edge_strdup (const char *str)
+{
+  char *new_str = NULL;
+
+  if (str)
+    new_str = nns_edge_strndup (str, strlen (str));
+
+  return new_str;
+}
+
+/**
+ * @brief Allocate new memory and copy bytes of string.
+ * @note Caller should release newly allocated string using nns_edge_free().
+ */
+char *
+nns_edge_strndup (const char *str, size_t len)
+{
+  char *new_str = NULL;
+
+  if (str) {
+    new_str = (char *) malloc (len + 1);
+
+    if (new_str) {
+      strncpy (new_str, str, len);
+      new_str[len] = '\0';
+    } else {
+      nns_edge_loge ("Failed to allocate memory (%zd).", len + 1);
+    }
+  }
+
+  return new_str;
+}
+
+/**
+ * @brief Allocate new memory and print formatted string.
+ * @note Caller should release newly allocated string using nns_edge_free().
+ */
+char *
+nns_edge_strdup_printf (const char *format, ...)
+{
+  char *new_str = NULL;
+  va_list args;
+  int len;
+
+  va_start (args, format);
+  len = vasprintf (&new_str, format, args);
+  if (len < 0)
+    new_str = NULL;
+  va_end (args);
+
+  return new_str;
+}
diff --git a/src/libnnstreamer-edge/nnstreamer-edge-util.h b/src/libnnstreamer-edge/nnstreamer-edge-util.h
new file mode 100644 (file)
index 0000000..97aa77d
--- /dev/null
@@ -0,0 +1,98 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+/**
+ * Copyright (C) 2022 Samsung Electronics Co., Ltd. All Rights Reserved.
+ *
+ * @file   nnstreamer-edge-util.h
+ * @date   24 August 2022
+ * @brief  Utility functions.
+ * @see    https://github.com/nnstreamer/nnstreamer-edge
+ * @author Jaeyun Jung <jy1210.jung@samsung.com>
+ * @note   This file is internal header for nnstreamer-edge. DO NOT export this file.
+ */
+
+#ifndef __NNSTREAMER_EDGE_UTIL_H__
+#define __NNSTREAMER_EDGE_UTIL_H__
+
+#include <netinet/tcp.h>
+#include <netinet/in.h>
+#include <pthread.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/**
+ * @brief Utility to silence unused parameter warning for intentionally unused parameters (e.g., callback functions of a framework)
+ */
+#ifndef UNUSED
+#define UNUSED(expr) do { (void)(expr); } while (0)
+#endif
+
+#define STR_IS_VALID(s) ((s) && (s)[0] != '\0')
+#define SAFE_FREE(p) do { if (p) { free (p); (p) = NULL; } } while (0)
+
+#define NNS_EDGE_MAGIC 0xfeedfeed
+#define NNS_EDGE_MAGIC_DEAD 0xdeaddead
+#define NNS_EDGE_MAGIC_IS_VALID(h) ((h) && (h)->magic == NNS_EDGE_MAGIC)
+
+#define nns_edge_lock_init(h) do { pthread_mutex_init (&(h)->lock, NULL); } while (0)
+#define nns_edge_lock_destroy(h) do { pthread_mutex_destroy (&(h)->lock); } while (0)
+#define nns_edge_lock(h) do { pthread_mutex_lock (&(h)->lock); } while (0)
+#define nns_edge_unlock(h) do { pthread_mutex_unlock (&(h)->lock); } while (0)
+
+/**
+ * @brief Get available port number.
+ */
+int nns_edge_get_available_port (void);
+
+/**
+ * @brief Get host string (host:port). Caller should release returned string using nns_edge_free().
+ */
+char *nns_edge_get_host_string (const char *host, const int port);
+
+/**
+ * @brief Parse string and get host string (host:port).
+ */
+void nns_edge_parse_host_string (const char *host_str, char **host, int *port);
+
+/**
+ * @brief Parse string and get port number. Return negative value when failed to get port number.
+ */
+int nns_edge_parse_port_number (const char *port_str);
+
+/**
+ * @brief Free allocated memory.
+ */
+void nns_edge_free (void *data);
+
+/**
+ * @brief Allocate new memory and copy bytes.
+ * @note Caller should release newly allocated memory using nns_edge_free().
+ */
+void *nns_edge_memdup (const void *data, size_t size);
+
+/**
+ * @brief Allocate new memory and copy string.
+ * @note Caller should release newly allocated string using nns_edge_free().
+ */
+char *nns_edge_strdup (const char *str);
+
+/**
+ * @brief Allocate new memory and copy bytes of string.
+ * @note Caller should release newly allocated string using nns_edge_free().
+ */
+char *nns_edge_strndup (const char *str, size_t len);
+
+/**
+ * @brief Allocate new memory and print formatted string.
+ * @note Caller should release newly allocated string using nns_edge_free().
+ */
+char *nns_edge_strdup_printf (const char *format, ...);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+#endif /* __NNSTREAMER_EDGE_UTIL_H__ */
index 6d58fcba7f7af7d2f01db47f8f81011908c8af57..e686dc0ee382a63802432d3ef621587d1280950a 100644 (file)
@@ -11,6 +11,8 @@
 #include "nnstreamer-edge.h"\r
 #include "nnstreamer-edge-common.h"\r
 #include "nnstreamer-edge-internal.h"\r
+#include "nnstreamer-edge-log.h"\r
+#include "nnstreamer-edge-util.h"\r
 \r
 /**\r
  * @brief Data struct for unittest.\r