update: libubus-cpp

This commit is contained in:
jbnadal
2018-04-24 17:35:57 +02:00
parent 3d1b4c7182
commit 60da11013f
14 changed files with 622 additions and 30 deletions

View File

@@ -16,8 +16,11 @@ include_directories (${workspaceRoot}/src/lib/libubus-cpp/include/)
file( file(
GLOB_RECURSE GLOB_RECURSE
source_files source_files
${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/ubus-call.cpp
${workspaceRoot}/src/lib/libubus-cpp/src/ubus-exec-receiver.cpp
${workspaceRoot}/src/lib/libubus-cpp/src/ubus-event-receiver.cpp
${workspaceRoot}/src/lib/libubus-cpp/src/ubus-event.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 ${workspaceRoot}/src/lib/libubus-cpp/src/ubus-sync.c
) )

View File

@@ -1,5 +1,4 @@
/*! /*!
* UbusCall.h
* *
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
* *
@@ -30,6 +29,16 @@
#include <string> #include <string>
/*---------------------------------- DEFINE ----------------------------------*/
#define kExecSuccess 0
#define kExecFailed -6
#define kExecInvalidArguments -5
/*---------------------------------- DEPS ----------------------------------*/
class UBusExecReceiver;
/*---------------------------------- CLASS ----------------------------------*/ /*---------------------------------- CLASS ----------------------------------*/
class UBusCall class UBusCall
@@ -40,6 +49,8 @@ public:
int exec (const std::string &a_path, const std::string &a_method, int 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 exec_async (struct ubus_context *a_ctx, uint32_t an_object, const std::string &a_method, const std::string &a_parameter, UBusExecReceiver *a_receiver);
int set_timeout (int a_timeout); int set_timeout (int a_timeout);
int set_result (const std::string &a_result); int set_result (const std::string &a_result);

View File

@@ -1,5 +1,4 @@
/*! /*!
* UbusCall.h
* *
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
* *

View File

@@ -0,0 +1,53 @@
/*!
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 18/08/2017
*/
#ifndef _UBUS_EVENT_RECEIVER_H
#define _UBUS_EVENT_RECEIVER_H
/*------------------------------- INCLUDES ----------------------------------*/
#include <list>
#include <string>
extern "C" {
#include <libubus.h>
}
/*----------------------------- Dependencies --------------------------------*/
struct ubus_context;
/*--------------------------------- CLASS ----------------------------------*/
class UBusEventReceiver : public ubus_event_handler {
public:
UBusEventReceiver (void);
virtual ~UBusEventReceiver (void);
int register_event (struct ubus_context *a_ctx, const std::string &an_event);
int unregister_event (struct ubus_context *a_ctx);
virtual void handle_event (const char *a_type, const char *a_json_msg) = 0;
};
#endif /* _UBUS_EVENT_RECEIVER_H */

View File

@@ -0,0 +1,56 @@
/*!
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 15/10/2014
*/
#ifndef _UBUS_EVENT_H
#define _UBUS_EVENT_H
/*------------------------------- INCLUDES ----------------------------------*/
#include <list>
#include <string>
extern "C" {
#include <libubus.h>
}
/*----------------------------- Dependencies --------------------------------*/
struct ubus_context;
struct ubus_event_handler;
/*--------------------------------- CLASS ----------------------------------*/
class UBusEvent {
public:
UBusEvent (void);
virtual ~UBusEvent (void);
int listen (struct ubus_context *a_ctx, const std::string &an_event);
virtual void handle_event (const char *a_type, const char *a_json_msg);
static int send (const std::string &an_event, const std::string &a_data);
private:
std::list <struct ubus_event_handler *> m_listeners;
};
#endif /* _UBUS_EVENT_H */

View File

@@ -0,0 +1,60 @@
/*!
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 04/05/2017
*/
#ifndef _UBUS_EXEC_RECIVER_H
#define _UBUS_EXEC_RECIVER_H
/*------------------------------- INCLUDES ----------------------------------*/
#include <string>
extern "C" {
#include <libubus.h>
#include <libubox/blobmsg_json.h>
}
/*--------------------------------- CLASS ----------------------------------*/
class UBusExecReceiver : public ubus_request {
public:
UBusExecReceiver (void);
virtual ~UBusExecReceiver (void);
int abort (struct ubus_context *a_ctx);
int complete_request_async (struct ubus_context *a_ctx);
void add_data (std::string a_data);
const std::string &get_data (void);
void add_result (int a_result);
int get_result (void);
virtual void complete (void) = 0;
private:
std::string m_data;
int m_result;
};
#endif /* _UBUS_EXEC_RECIVER_H */

View File

@@ -1,6 +1,4 @@
/*! /*!
* UbusObject.h
*
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or

View File

@@ -1,7 +1,5 @@
/*! /*!
* UbusTimer.h * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
*
* Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public

View File

@@ -1,5 +1,4 @@
/*! /*!
* ubus-call.cpp
* *
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
* *
@@ -36,12 +35,29 @@ extern "C" {
#include "common.h" #include "common.h"
#include "ubus-cpp/ubus-exec-receiver.h"
#include "ubus-cpp/ubus-call.h" #include "ubus-cpp/ubus-call.h"
#define kDefaultTimeoutInSecond 5 #define kDefaultTimeoutInSecond 5
// #define UBUS_PROFILE 1 // #define UBUS_PROFILE 1
static void receive_call_result_data (struct ubus_request *a_req, int a_type, struct blob_attr *a_msg); /*! ----------------------------------------------------------------------------
* @fn receive_call_result_data
*
* @brief callback of result.
*/
static void receive_call_result_data (struct ubus_request *a_req, int a_type, struct blob_attr *a_msg)
{
char *the_str;
UBusCall *a_caller = static_cast<UBusCall*>(a_req->priv);
the_str = blobmsg_format_json(a_msg, true);
a_caller->set_result(the_str);
free(the_str);
}
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn UBusCall * @fn UBusCall
@@ -80,6 +96,32 @@ int UBusCall::exec (const std::string &a_path, const std::string &a_method,
return the_ret; return the_ret;
} }
/*! ----------------------------------------------------------------------------
* @fn exec-async
*
* @brief Execute an UBus Method Asynchrously;
*/
int UBusCall::exec_async (struct ubus_context *a_ctx, uint32_t an_object, const std::string &a_method, const std::string &a_parameter, UBusExecReceiver *a_receiver)
{
int the_return = -1;
struct blob_buf the_parameter = { 0 };
a_receiver->abort (a_ctx);
blob_buf_init (&the_parameter, 0);
if (!a_parameter.empty())
blobmsg_add_json_from_string (&the_parameter, a_parameter.c_str());
if (!ubus_invoke_async (a_ctx, an_object, a_method.c_str(), the_parameter.head, a_receiver)) {
a_receiver->complete_request_async (a_ctx);
the_return = 0;
}
blob_buf_free (&the_parameter);
return the_return;
}
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn set_result * @fn set_result
@@ -91,21 +133,3 @@ int UBusCall::set_result (const std::string &a_result)
m_data = a_result; m_data = a_result;
return 0; return 0;
} }
/*! ----------------------------------------------------------------------------
* @fn receive_call_result_data
*
* @brief UBus received call back.
*/
static void receive_call_result_data (struct ubus_request *a_req, int a_type, struct blob_attr *a_msg)
{
UNUSED_PARAMETER (a_type);
char *the_str;
UBusCall *an_ubus_object = static_cast<UBusCall*>(a_req->priv);
the_str = blobmsg_format_json(a_msg, true);
an_ubus_object->set_result(std::string(the_str));
free(the_str);
}

View File

@@ -0,0 +1,97 @@
/*!
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 18/08/2017
*/
/*------------------------------- INCLUDES ----------------------------------*/
#include <string.h>
extern "C" {
#include <libubox/blobmsg_json.h>
}
#include "ubus-cpp/ubus-event-receiver.h"
/*------------------------------- GLOBALS ----------------------------------*/
/*--------------------------------- DEFINES ---------------------------------*/
/*! ----------------------------------------------------------------------------
* @fn receive_event
*
* @brief callback called when the event arrive.
*/
static void receive_event (struct ubus_context *a_ctx, struct ubus_event_handler *an_ev,
const char *a_type, struct blob_attr *a_msg)
{
char *the_string;
UBusEventReceiver *the_receiver = static_cast<UBusEventReceiver *>(an_ev);
the_string = blobmsg_format_json (a_msg, true);
the_receiver->handle_event (a_type, the_string);
free (the_string);
}
/*! ----------------------------------------------------------------------------
* @fn UBusEventReceiver
*
* @brief Constructor of the UBus Event Receiver
*/
UBusEventReceiver::UBusEventReceiver (void)
{
obj = { 0 };
cb = receive_event;
}
/*! ----------------------------------------------------------------------------
* @fn ~UBusEventReceiver
*
* @brief Destructor of of the UBus Event Receiver
*/
UBusEventReceiver::~UBusEventReceiver (void)
{
}
/*! ----------------------------------------------------------------------------
* @fn register_event
*
* @brief register to an ubus event type.
*/
int UBusEventReceiver::register_event (struct ubus_context *a_ctx, const std::string &an_event)
{
return ubus_register_event_handler (a_ctx, this, an_event.c_str());;
}
/*! ----------------------------------------------------------------------------
* @fn unregister_event
*
* @brief un register to an ubus event type.
*/
int UBusEventReceiver::unregister_event (struct ubus_context *a_ctx)
{
return ubus_unregister_event_handler (a_ctx, this);
}

View File

@@ -0,0 +1,122 @@
/*!
* (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: 08/10/2014
*/
/*------------------------------- INCLUDES ----------------------------------*/
#include <stdint.h>
#include <stdio.h>
extern "C" {
#include <libubox/blobmsg_json.h>
#include "ubus-sync.h"
}
#include "ubus-cpp/ubus-event.h"
/*------------------------------- GLOBALS ----------------------------------*/
/*--------------------------------- DEFINES ---------------------------------*/
// #define UBUS_PROFILE 1
static void receive_event (struct ubus_context *a_ctx, struct ubus_event_handler *an_ev,
const char *a_type, struct blob_attr *a_msg)
{
char *the_str;
UBusEvent *a_caller = static_cast<UBusEvent*>(an_ev->priv);
the_str = blobmsg_format_json(a_msg, true);
a_caller->handle_event(a_type, the_str);
free(the_str);
}
/*! ----------------------------------------------------------------------------
* @fn UBusEvent
*
* @brief Constructor of the UBusEvent
*/
UBusEvent::UBusEvent (void)
{
}
/*! ----------------------------------------------------------------------------
* @fn ~UBusEvent
*
* @brief Destructor of the UBusEvent
*/
UBusEvent::~UBusEvent (void)
{
while (!m_listeners.empty()) {
delete *(m_listeners.begin());
m_listeners.erase(m_listeners.begin());
}
}
/*! ----------------------------------------------------------------------------
* @fn listen
*
* @brief Handle an UBUS Event.
*/
int UBusEvent::listen (struct ubus_context *a_ctx, const std::string &an_event)
{
//static struct ubus_event_handler theListener;
//fprintf (stderr, "%s\n", __PRETTY_FUNCTION__);
struct ubus_event_handler *the_evt_listener = new ubus_event_handler;
memset (the_evt_listener, 0, sizeof (struct ubus_event_handler));
the_evt_listener->cb = receive_event;
the_evt_listener->priv = this;
m_listeners.push_back(the_evt_listener);
return ubus_register_event_handler(a_ctx, the_evt_listener, an_event.c_str());
}
/*! ----------------------------------------------------------------------------
* @fn handle_event
*
* @brief Handle an UBUS Event.
*/
void UBusEvent::handle_event (const char *a_type, const char *a_json_msg)
{
}
/*! ----------------------------------------------------------------------------
* @fn send
*
* @brief Send an UBus Method
*/
int UBusEvent::send (const std::string &an_event, const std::string &a_data)
{
int the_result;
#ifdef UBUS_PROFILE
struct timeval the_start_request, the_current_timeval;
gettimeofday(&the_start_request, NULL);
#endif
the_result = ubus_sync_send_event(an_event.c_str(), a_data.c_str());
#ifdef UBUS_PROFILE
gettimeofday(&the_current_timeval, NULL);
printf ("ubus_event [%s] duree: %.0lf ms\n", an_event.c_str(), time_diff(the_start_request, the_current_timeval) / 1000);
#endif
return the_result;
}

View File

@@ -0,0 +1,173 @@
/*!
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 04/05/2017
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include "ubus-cpp/ubus-exec-receiver.h"
/*! ----------------------------------------------------------------------------
* @fn handle_data_request
*
* @brief callback called when data arrive.
*/
static void handle_data_request (struct ubus_request *a_req, int, struct blob_attr *a_msg)
{
char *the_string;
UBusExecReceiver *the_receiver = static_cast<UBusExecReceiver *>(a_req);
//printf ("handle_data_request\n");
the_string = blobmsg_format_json (a_msg, true);
the_receiver->add_data (the_string);
free (the_string);
}
/*! ----------------------------------------------------------------------------
* @fn handle_complete_request
*
* @brief callback called when the request is complete.
*/
static void handle_complete_request (struct ubus_request *a_req, int a_ret)
{
UBusExecReceiver *the_receiver = static_cast<UBusExecReceiver *>(a_req);
//printf ("handle_complete_request (%d)\n", a_ret);
the_receiver->add_result (a_ret);
the_receiver->complete ();
}
/*! ----------------------------------------------------------------------------
* @fn UBusExecReceiver
*
* @brief Constructor of the Ubus Exec Receiver
*/
UBusExecReceiver::UBusExecReceiver (void)
{
list = LIST_HEAD_INIT (list);
pending = { 0 };
status_code = 0;
status_msg = 0;
blocked = 0;
cancelled = 0;
notify = 0;
peer = 0;
seq = 0;
raw_data_cb = NULL;
data_cb = handle_data_request;
fd_cb = NULL;
complete_cb = handle_complete_request;
ctx = NULL;
priv = NULL;
m_result = -1;
}
/*! ----------------------------------------------------------------------------
* @fn ~UBusExecReceiver
*
* @brief Destructor of the Ubus Exec Receiver
*/
UBusExecReceiver::~UBusExecReceiver (void)
{
}
/*! ----------------------------------------------------------------------------
* @fn abort
*
* @brief abort the current exec command.
*/
int UBusExecReceiver::abort (struct ubus_context *a_ctx)
{
ubus_abort_request (a_ctx, this);
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn complete_request_async
*
* @brief launch the registered async request.
*/
int UBusExecReceiver::complete_request_async (struct ubus_context *a_ctx)
{
data_cb = handle_data_request;
complete_cb = handle_complete_request;
ubus_complete_request_async (a_ctx, this);
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn add_data
*
* @brief add data from the receiver.
*/
void UBusExecReceiver::add_data (std::string a_data)
{
m_data += a_data;
}
/*! ----------------------------------------------------------------------------
* @fn get_data
*
* @brief return the data received from the command.
*/
const std::string &UBusExecReceiver::get_data (void)
{
return m_data;
}
/*! ----------------------------------------------------------------------------
* @fn get_data
*
* @brief save the result of the request.
*/
void UBusExecReceiver::add_result (int a_result)
{
m_result = a_result;
}
/*! ----------------------------------------------------------------------------
* @fn get_result
*
* @brief return the result of the request.
*/
int UBusExecReceiver::get_result (void)
{
return m_result;
}

View File

@@ -1,5 +1,4 @@
/*! /*!
* UbusCall.cpp
* *
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
* *

View File

@@ -1,5 +1,4 @@
/*! /*!
* UbusTimer.cpp
* *
* Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved.
* *