C-API/Common: error reporting API implementation.
authorMyungJoo Ham <myungjoo.ham@samsung.com>
Mon, 25 Oct 2021 10:52:46 +0000 (19:52 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Tue, 26 Oct 2021 01:56:37 +0000 (10:56 +0900)
API implementation:
ml_error(), ml_strerror()

Internal interface for API implementation:
_ml_error_report()

TODO: apply _ prefix for all internal interfaces.

Signed-off-by: MyungJoo Ham <myungjoo.ham@samsung.com>
c/src/ml-api-common.c
c/src/ml-api-internal.h

index a500dfe..939c8db 100644 (file)
@@ -11,6 +11,7 @@
  */
 
 #include <string.h>
+#include <glib.h>
 
 #include "nnstreamer.h"
 #include "ml-api-internal.h"
@@ -930,21 +931,63 @@ ml_replace_string (gchar * source, const gchar * what, const gchar * to,
 }
 
 /**
+ * @brief error reporting infra
+ */
+#define _ML_ERRORMSG_LENGTH (4096)
+static char errormsg[_ML_ERRORMSG_LENGTH] = { 0 };      /* one page limit */
+
+static int reported = 0;
+G_LOCK_DEFINE_STATIC (errlock);
+
+/**
  * @brief public API function of error reporting.
  */
 const char *
 ml_error (void)
 {
-  /** @todo NYI **/
-  return NULL;
+  G_LOCK (errlock);
+  if (reported != 0) {
+    errormsg[0] = '\0';
+    reported = 0;
+  }
+  if (errormsg[0] == '\0') {
+    G_UNLOCK (errlock);
+    return NULL;
+  }
+
+  reported = 1;
+
+  G_UNLOCK (errlock);
+  return errormsg;
 }
 
 /**
+ * @brief Internal interface to write messages for ml_error()
+ */
+void
+_ml_error_report (const char *message)
+{
+  G_LOCK (errlock);
+  strncpy (errormsg, message, _ML_ERRORMSG_LENGTH);
+  errormsg[_ML_ERRORMSG_LENGTH - 1] = '\0';
+  reported = 0;
+  G_UNLOCK (errlock);
+}
+
+static const char *strerrors[] = {
+  [0] = "Not an error",
+  [EINVAL] =
+      "Invalid parameters are given to a function. Check parameter values. (EINVAL)",
+};
+
+/**
  * @brief public API function of error code descriptions
  */
 const char *
 ml_strerror (int errnum)
 {
-  /** @todo NYI **/
-  return NULL;
+  int size = sizeof (strerrors) / sizeof (strerrors[0]);
+  if (errnum < 0 || errnum >= size)
+    return NULL;
+  return strerrors[errnum];
 }
index 7b88c84..8be6aab 100644 (file)
@@ -293,6 +293,27 @@ int ml_tizen_get_feature_enabled (void);
 int ml_tizen_set_feature_state (int state);
 /****** TIZEN CHECK FEATURE ENDS *****/
 #endif /* __TIZEN__ */
+
+
+/***** Begin: Error reporting internal interfaces ****
+ * ml-api-* implementation needs to use these interfaces to provide
+ * proper error messages for ml_error();
+ */
+
+/**
+ * @brief Call when an error occurs during API execution.
+ * @param[in] message The error description
+ */
+void _ml_error_report (const char *message);
+
+#define _ml_error_report_return (errno, message)  do { \
+  _ml_error_report (message); \
+  return errno; \
+} while(0)
+
+/***** End: Error reporting internal interfaces *****/
+
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */