update ubus exec implementation

This commit is contained in:
jbnadal
2018-04-12 19:45:52 +02:00
parent 1cd7e4db36
commit 4baa7f38fb
4 changed files with 1311 additions and 45 deletions

View File

@@ -5,6 +5,10 @@ project (libubus-cpp)
set (CMAKE_MODULE_PATH "${MODULE_PATH}") set (CMAKE_MODULE_PATH "${MODULE_PATH}")
set (CMAKE_CXX_STANDARD 11) set (CMAKE_CXX_STANDARD 11)
set(UBUS_MAX_MSGLEN 1048576)
add_definitions(-DUBUS_MAX_MSGLEN=${UBUS_MAX_MSGLEN})
add_definitions(-DUBUS_UNIX_SOCKET="/tmp/ubus.sock")
include (br) include (br)
include_directories (${workspaceRoot}/src/lib/libubus-cpp/include/) include_directories (${workspaceRoot}/src/lib/libubus-cpp/include/)
@@ -15,6 +19,7 @@ file(
${workspaceRoot}/src/lib/libubus-cpp/src/ubus-call.cpp ${workspaceRoot}/src/lib/libubus-cpp/src/ubus-call.cpp
${workspaceRoot}/src/lib/libubus-cpp/src/ubus-object.cpp ${workspaceRoot}/src/lib/libubus-cpp/src/ubus-object.cpp
${workspaceRoot}/src/lib/libubus-cpp/src/uloop-timer.cpp ${workspaceRoot}/src/lib/libubus-cpp/src/uloop-timer.cpp
${workspaceRoot}/src/lib/libubus-cpp/src/ubus-sync.c
) )
add_library( add_library(

View File

@@ -1,5 +1,5 @@
/*! /*!
* UbusCall.cpp * ubus-call.cpp
* *
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
* *
@@ -30,13 +30,18 @@ extern "C" {
#include <libubox/blobmsg_json.h> #include <libubox/blobmsg_json.h>
} }
extern "C" {
#include "ubus-sync.h"
}
#include "common.h" #include "common.h"
#include "ubus-cpp/ubus-call.h" #include "ubus-cpp/ubus-call.h"
#define kDefaultTimeoutInSecond 5 #define kDefaultTimeoutInSecond 5
// #define UBUS_PROFILE 1
static void receive_call_result_data (struct ubus_request *aReq, int aType, struct blob_attr *aMsg); static void receive_call_result_data (struct ubus_request *a_req, int a_type, struct blob_attr *a_msg);
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn UBusCall * @fn UBusCall
@@ -57,48 +62,21 @@ m_timeout(kDefaultTimeoutInSecond)
int UBusCall::exec (const std::string &a_path, const std::string &a_method, int UBusCall::exec (const std::string &a_path, const std::string &a_method,
const std::string &a_parameter, std::string &a_result) const std::string &a_parameter, std::string &a_result)
{ {
int the_ret = 0; int the_ret = 0;
uint32_t the_id; #ifdef UBUS_PROFILE
struct timeval the_start_request, the_current_timeval;
struct blob_buf the_buf = { 0 }; gettimeofday(&the_start_request, NULL);
struct ubus_context *the_ctx = ubus_connect(NULL); #endif
the_ret = ubus_sync_exec(a_path.c_str(), a_method.c_str(), a_parameter.c_str(), receive_call_result_data, this);
if (the_ctx == NULL) {
fprintf(stderr, "%s - Failed to create an ubus context.\n", __PRETTY_FUNCTION__);
return -1;
}
blob_buf_init(&the_buf, 0);
if (!a_parameter.empty()) {
if (!blobmsg_add_json_from_string(&the_buf, a_parameter.c_str())) {
fprintf(stderr, "%s - Failed to parse message data\n", __PRETTY_FUNCTION__);
return -1;
}
}
the_ret = ubus_lookup_id(the_ctx, a_path.c_str(), &the_id);
if (the_ret) {
fprintf(stderr, "%s - ubus_lookup_id error = '%d'\n", __PRETTY_FUNCTION__, the_ret);
ubus_free(the_ctx);
blob_buf_free(&the_buf);
return the_ret;
}
the_ret = ubus_invoke(the_ctx, the_id, a_method.c_str(), the_buf.head, receive_call_result_data, this, m_timeout * 1000);
ubus_free(the_ctx);
blob_buf_free(&the_buf);
if (!m_data.empty()) { if (!m_data.empty()) {
a_result = m_data.c_str();
a_result = m_data;
} }
#ifdef UBUS_PROFILE
gettimeofday(&the_current_timeval, NULL);
printf("ubus_exec [%s]-[%s] duree: %.0lf ms\n", a_path.c_str(), a_method.c_str(), time_diff(the_start_request, the_current_timeval) / 1000);
#endif
return the_ret; return the_ret;
} }
@@ -120,14 +98,14 @@ int UBusCall::set_result (const std::string &a_result)
* *
* @brief UBus received call back. * @brief UBus received call back.
*/ */
static void receive_call_result_data (struct ubus_request *aReq, int aType, struct blob_attr *aMsg) static void receive_call_result_data (struct ubus_request *a_req, int a_type, struct blob_attr *a_msg)
{ {
UNUSED_PARAMETER (aType); UNUSED_PARAMETER (a_type);
char *the_str; char *the_str;
UBusCall *anUBusObject = static_cast<UBusCall*>(aReq->priv); UBusCall *an_ubus_object = static_cast<UBusCall*>(a_req->priv);
the_str = blobmsg_format_json(aMsg, true); the_str = blobmsg_format_json(a_msg, true);
anUBusObject->set_result(std::string(the_str)); an_ubus_object->set_result(std::string(the_str));
free(the_str); free(the_str);
} }

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,25 @@
/*!
* (C) Copyright 2003-2018 Awox SA. All rights reserved.
* This work contains confidential trade secrets of Awox.
* Use, examination, copying, transfer and disclosure to others
* are prohibited, except with the express written agreement of Awox.
*
* @Author: Awox
* @Date: 09/02/2018
*/
/*------------------------------- INCLUDES ----------------------------------*/
#ifndef _AW_UBUS_SYNC_H
#define _AW_UBUS_SYNC_H
#include <stdbool.h>
#include <libubus.h> // temporary for ubus_data_handler_t
double time_diff (struct timeval x , struct timeval y);
int ubus_sync_exec (const char *a_path, const char* a_method, const char *a_parameter, ubus_data_handler_t cb, void *priv);
int ubus_sync_send_event (const char *an_event, const char *a_data);
#endif /* _AW_UBUS_SYNC_H */