Add C Bindings for rust link 19/318519/1
authorYoungsun Suh <youngsun.suh@samsung.com>
Mon, 20 Jan 2025 02:26:10 +0000 (11:26 +0900)
committerYoungsun Suh <youngsun.suh@samsung.com>
Mon, 20 Jan 2025 02:26:41 +0000 (11:26 +0900)
Change-Id: I96dae3ed444c58901a6fde73953c07b8be266a0c

libaurum/inc/AurumCBindings.h [new file with mode: 0644]
libaurum/meson.build
libaurum/src/c_bindings/AurumCBindings.cc [new file with mode: 0644]
libaurum/src/c_bindings/meson.build [new file with mode: 0644]
libaurum/src/meson.build
meson_options.txt
tests/Test_CBindings.cc [new file with mode: 0644]
tests/meson.build

diff --git a/libaurum/inc/AurumCBindings.h b/libaurum/inc/AurumCBindings.h
new file mode 100644 (file)
index 0000000..d940599
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2025 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.
+ *
+ */
+
+#ifndef AURUM_C_BINDINGS_H
+#define AURUM_C_BINDINGS_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+    // Initialize the Aurum library.
+    void aurum_init();
+    // Dump current screen as JSON string.
+    const char* dump_screen();
+    // Free the allocated string memory.
+    void free_string_result(const char* ptr);
+#ifdef __cplusplus
+}
+#endif
+
+#endif // AURUM_C_BINDINGS_H
\ No newline at end of file
index daa56488c88705dbd4607fe40e2dbb82e03e0e13..7f199a13830c9fb08a05078a3d47e45776dfbd21 100644 (file)
@@ -86,3 +86,9 @@ libaurum =  declare_dependency(link_with: libaurum_lib,
 
 install_headers(libaurum_install_inc,
 )
+
+if get_option('build_c_bindings') == true
+  c_bindings_dir = dir_include + '/c_bindings'
+  message('install c bindings header to ' + c_bindings_dir)
+  install_headers('./inc/AurumCBindings.h', install_dir: c_bindings_dir,)
+endif
diff --git a/libaurum/src/c_bindings/AurumCBindings.cc b/libaurum/src/c_bindings/AurumCBindings.cc
new file mode 100644 (file)
index 0000000..9aa581f
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2025 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 "AurumCBindings.h"
+#include "Aurum.h"
+#include <cstring>
+
+using namespace Aurum;
+
+namespace
+{
+    constexpr const char WINDOWS_KEY[] = "\"windows\"";
+}
+
+void aurum_init()
+{
+    LOGI("aurum_init");
+    AccessibleWatcher::getInstance();
+}
+
+const char* dump_screen()
+{
+    LOGI("dump_screen");
+    auto device = UiDevice::getInstance();
+    auto windowRoots = device->getWindowRoot();
+    std::string dumpResult{WINDOWS_KEY};
+    dumpResult += " : [";
+
+    for (const auto& root: windowRoots) {
+        // TODO: implement dump logic
+        // root->dump();
+        dumpResult += "{},";
+    }
+
+    // remove last comma if exists.
+    if(dumpResult.back() == ',') {
+        dumpResult.pop_back();
+    }
+
+    dumpResult += "]";
+    char* cResult = new char[dumpResult.size() + 1]; // Consumers must free this memory later
+    std::strcpy(cResult, dumpResult.c_str());
+    return cResult;
+
+}
+
+void free_string_result(const char* ptr)
+{
+    LOGI("free_string_result");
+    delete[] ptr;
+}
\ No newline at end of file
diff --git a/libaurum/src/c_bindings/meson.build b/libaurum/src/c_bindings/meson.build
new file mode 100644 (file)
index 0000000..dce0bab
--- /dev/null
@@ -0,0 +1,3 @@
+libaurum_src += [
+    files('AurumCBindings.cc'),
+]
index 17595be6f0203025c90ed684ee97bae0c75405b1..261bc7461caca2f951e30f6dfc4e3dcb73c130c0 100644 (file)
@@ -16,3 +16,7 @@ libaurum_src += [
 subdir('Accessibility')
 subdir('Impl')
 subdir('Runnable')
+
+if get_option('build_c_bindings') == true
+  subdir('c_bindings')
+endif
index 3ca0306f182f59ce75a5c9ace0bfe78785baa143..386c0c13accf407e340e297c870848bb5131ba87 100644 (file)
@@ -32,3 +32,9 @@ option('libaurum_only',
   value: false,
   description: 'skip building grpc and protobuf protocol'
 )
+
+option('build_c_bindings',
+  type: 'boolean',
+  value: false,
+  description: 'build c bindings to be called from rust code'
+)
\ No newline at end of file
diff --git a/tests/Test_CBindings.cc b/tests/Test_CBindings.cc
new file mode 100644 (file)
index 0000000..564c7b7
--- /dev/null
@@ -0,0 +1,16 @@
+#include <gtest/gtest.h>
+
+#include <AurumCBindings.h>
+
+TEST(CBindingsTest, DumpScreen)
+{
+    aurum_init();
+    const char *c_str = dump_screen();
+    EXPECT_NE(c_str, nullptr);
+    free_string_result(c_str);
+}
+
+TEST(CBindingsTest, FreeStringResultWorksWithNull)
+{
+    free_string_result(nullptr);
+}
\ No newline at end of file
index 7d668feaeb9e6ff5acad1ef353a1c1ba79305321..1e52a62d2f8c5327e64a487e94b38d614482b6f2 100644 (file)
@@ -6,6 +6,10 @@ test_src = [
     ['test_uiselector', files('Test_UiSelector.cc')],
 ]
 
+if get_option('build_c_bindings') == true
+    test_src += [['Test_CBindings', files('Test_CBindings.cc')]]
+endif
+
 test_inc = [
     include_directories('./')
 ]