[test/sensor] Add test case to publish the jpeg data
authorSangjung Woo <sangjung.woo@samsung.com>
Tue, 6 Jul 2021 05:03:51 +0000 (14:03 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Thu, 2 Sep 2021 08:37:17 +0000 (17:37 +0900)
This test case publishes the jpeg data as "TestTopic" topic name 10
times. If data is successfully received, then the image is shown on the
server side.

Signed-off-by: Sangjung Woo <sangjung.woo@samsung.com>
tests/CMakeLists.txt [new file with mode: 0644]
tests/res/0.jpg [new file with mode: 0644]
tests/res/1.jpg [new file with mode: 0644]
tests/res/2.jpg [new file with mode: 0644]
tests/test_edge_sensor.c [new file with mode: 0644]

diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..fbf3b37
--- /dev/null
@@ -0,0 +1,6 @@
+# Edge Sensor test
+ADD_EXECUTABLE(test_edge_sensor test_edge_sensor.c)
+TARGET_INCLUDE_DIRECTORIES(test_edge_sensor PRIVATE ${EDGE_REQUIRE_PKGS_INCLUDE_DIRS} ${INCLUDE_DIR})
+TARGET_LINK_LIBRARIES(test_edge_sensor -L${PROJECT_BINARY_DIR}/src edge-sensor)
+
+INSTALL (TARGETS test_edge_sensor DESTINATION ${BIN_INSTALL_DIR})
diff --git a/tests/res/0.jpg b/tests/res/0.jpg
new file mode 100644 (file)
index 0000000..1eb1060
Binary files /dev/null and b/tests/res/0.jpg differ
diff --git a/tests/res/1.jpg b/tests/res/1.jpg
new file mode 100644 (file)
index 0000000..84b0c92
Binary files /dev/null and b/tests/res/1.jpg differ
diff --git a/tests/res/2.jpg b/tests/res/2.jpg
new file mode 100644 (file)
index 0000000..1c0ccad
Binary files /dev/null and b/tests/res/2.jpg differ
diff --git a/tests/test_edge_sensor.c b/tests/test_edge_sensor.c
new file mode 100644 (file)
index 0000000..85d7afc
--- /dev/null
@@ -0,0 +1,148 @@
+
+/* SPDX-License-Identifier: Apache-2.0 */
+/**
+ * @file       test_edge_sensor.c
+ * @date       05 July 2021
+ * @brief Publish the jpeg data as "TestTopic" topic name 10 times
+ *
+ * @details Usage examples
+ *   This test case publishes the jpeg data as "TestTopic" topic name 10 times.
+ *   If data is successfully received, then the image is shown on the server-side.
+ *
+ *   Server Side:
+ *   gst-launch-1.0 \
+ *     mqttsrc debug=1 sub-topic=TestTopic host=localhost sub-timeout=9223372036854775807 ! \
+ *     jpegdec ! video/x-raw,framerate=0/1 ! \
+ *     videoscale ! videoconvert ! ximagesink qos=0
+ *
+ *   Client Side:
+ *     test_edge_sensor [Image path]
+ *     (e.g. ./tests/test_edge_sensor ../tests/res in build directory)
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "edge_sensor.h"
+
+/**
+ * @brief Get jpeg data and its size from the file path
+ * @param[in] path jpeg file path
+ * @param[out] buf image data of the input jpeg file
+ * @param[out] buf_size image size of the input jpeg file
+ */
+int
+read_jpeg_file (const char *path, uint8_t **buf, uint64_t *buf_size)
+{
+  uint8_t *out_buf;
+  uint64_t curr_size = 0;
+  uint64_t total_size = 0;
+
+  FILE *fr = fopen(path,"rb");
+
+  /* get file size */
+  fseek(fr, 0, SEEK_END);
+  *buf_size = (uint64_t)ftell(fr);
+
+  /* allocate the memory space */
+  out_buf = (uint8_t*)malloc(*buf_size);
+  if (!out_buf) {
+    printf ("Fail to malloc()\n");
+    return -1;
+  }
+  
+  /* get the data from file */
+  fseek(fr, 0, SEEK_SET);
+  while ((curr_size = fread (&out_buf[total_size], sizeof(uint8_t), *buf_size - total_size, fr)) > 0) {
+    total_size += curr_size;
+  }
+
+  if (total_size != *buf_size) {
+    printf ("Fail to fread(): different size\n");
+    return -1;
+  }
+
+  fclose (fr);
+  *buf = out_buf;
+  return 0;
+}
+
+/**
+ * @brief Callback function to be called the connection state is changed.
+ * @param[in,out] user_data User data that provided when calling edge_open_connection()
+ * @param[in] state The new state of MQTT connection
+ */
+void
+state_change_cb (void *user_data, edge_mqtt_state_t state)
+{
+  edge_mqtt_state_t *s = (edge_mqtt_state_t*)user_data;
+  switch (state) {
+    case MQTT_CONNECTED:
+      *s = MQTT_CONNECTED;
+      printf(" - User: State change: MQTT_CONNECTED\n");
+      break;
+
+    case MQTT_DISCONNECTED:
+      *s = MQTT_DISCONNECTED;
+      printf(" - User: State change: MQTT_DISCONNECTED\n");
+      break;
+    
+    default:
+      printf(" - User: State change: Default\n");
+      break;
+  }
+  return ;
+}
+
+int
+main(int argc, char* argv[])
+{
+  uint8_t *buf;
+  uint64_t buf_size;
+  edge_h handle;
+  edge_mqtt_state_t state = MQTT_CONNECTION_LOST;
+  char path[512] = { 0 };
+  int count  = 0;
+
+  if (argc != 2) {
+    printf ("Error!: Omit the image directory.\n");
+    return -1;
+  }
+  
+  int ret = edge_open_connection (&handle, 
+      NULL, NULL, "TestTopic",
+      0, 0, "", state_change_cb, (void*)&state);
+  if (ret != 0) {
+      printf ("Error: edge_open_connection() ret: %d\n", ret);
+      return -1;
+  }
+  
+  while (1) {
+    usleep(200000L);
+
+    if (state != MQTT_CONNECTED)
+      continue;
+
+    snprintf (path, 512, "%s/%d.jpg", argv[1], count++%3);
+    read_jpeg_file (path, &buf, &buf_size);
+
+    ret = edge_publish_single_msg (handle, buf, buf_size);
+    if (ret != 0) {
+      printf ("Error: edge_publish_single_msg() ret: %d\n", ret);
+      return -1;
+    }
+    free(buf);
+
+    usleep(200000L);
+    if (count > 10)
+      break;
+  }
+
+  ret = edge_close_connection (handle);
+  if (ret != 0) {
+      printf ("Error: edge_close_connection() ret: %d\n", ret);
+      return -1;
+  }
+
+  printf ("Success: All %d test topics are sent successfully!\n", count-1);
+  return 0;
+}