summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-09-09 13:59:27 +0200
committerJo-Philipp Wich <jo@mein.io>2022-09-09 14:23:07 +0200
commit01c412c4607911f20086264ddf4ac269f3b8eb5f (patch)
tree355dacbb89458c72e3a88d88fcb7ae7f0a5d084d
parent8e240fa8e58e68f6ea084efbdd59051ad6ce031f (diff)
ubus: add toplevel constants for ubus status codes
Add constants for ubus status codes to the toplevel module scope in order to avoid the need for magic values in the code. Suggested-by: Felix Fietkau <nbd@nbd.name> Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--CMakeLists.txt10
-rw-r--r--lib/ubus.c19
2 files changed, 29 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index be8a394..45de57a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -123,6 +123,16 @@ IF(UBUS_SUPPORT)
SET_TARGET_PROPERTIES(ubus_lib PROPERTIES OUTPUT_NAME ubus PREFIX "")
TARGET_LINK_OPTIONS(ubus_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS})
TARGET_LINK_LIBRARIES(ubus_lib ${ubus} ${blobmsg_json})
+ FILE(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/test.c" "
+ #include <libubus.h>
+ int main() { return UBUS_STATUS_NO_MEMORY; }
+ ")
+ TRY_COMPILE(HAVE_NEW_UBUS_STATUS_CODES
+ ${CMAKE_BINARY_DIR}
+ "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/test.c")
+ IF(HAVE_NEW_UBUS_STATUS_CODES)
+ ADD_DEFINITIONS(-DHAVE_NEW_UBUS_STATUS_CODES)
+ ENDIF()
ENDIF()
IF(UCI_SUPPORT)
diff --git a/lib/ubus.c b/lib/ubus.c
index e2491ec..a9bafcb 100644
--- a/lib/ubus.c
+++ b/lib/ubus.c
@@ -2053,6 +2053,25 @@ void uc_module_init(uc_vm_t *vm, uc_value_t *scope)
{
uc_function_list_register(scope, global_fns);
+#define ADD_CONST(x) ucv_object_add(scope, #x, ucv_int64_new(UBUS_##x))
+ ADD_CONST(STATUS_OK);
+ ADD_CONST(STATUS_INVALID_COMMAND);
+ ADD_CONST(STATUS_INVALID_ARGUMENT);
+ ADD_CONST(STATUS_METHOD_NOT_FOUND);
+ ADD_CONST(STATUS_NOT_FOUND);
+ ADD_CONST(STATUS_NO_DATA);
+ ADD_CONST(STATUS_PERMISSION_DENIED);
+ ADD_CONST(STATUS_TIMEOUT);
+ ADD_CONST(STATUS_NOT_SUPPORTED);
+ ADD_CONST(STATUS_UNKNOWN_ERROR);
+ ADD_CONST(STATUS_CONNECTION_FAILED);
+
+#ifdef HAVE_NEW_UBUS_STATUS_CODES
+ ADD_CONST(STATUS_NO_MEMORY);
+ ADD_CONST(STATUS_PARSE_ERROR);
+ ADD_CONST(STATUS_SYSTEM_ERROR);
+#endif
+
conn_type = uc_type_declare(vm, "ubus.connection", conn_fns, free_connection);
defer_type = uc_type_declare(vm, "ubus.deferred", defer_fns, free_deferred);
object_type = uc_type_declare(vm, "ubus.object", object_fns, free_object);