Import ubuscpp

domod
This commit is contained in:
2016-03-01 22:53:43 +01:00
parent 6288703488
commit 5564ad3b6e
27 changed files with 3115 additions and 0 deletions

View File

@@ -0,0 +1,133 @@
/*!
* UbusCall.cpp
*
* Copyright (c) 2015, 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: 21/03/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
extern "C" {
#include <libubus.h>
#include <libubox/blobmsg_json.h>
}
#include "common.h"
#include "ubuscpp/UBusCall.h"
#define kDefaultTimeoutInSecond 5
static void receive_call_result_data (struct ubus_request *aReq, int aType, struct blob_attr *aMsg);
/*! ----------------------------------------------------------------------------
* @fn UBusCall
*
* @brief Constructor of the UBus Handler.
*/
UBusCall::UBusCall (void):
mTimeout (kDefaultTimeoutInSecond)
{
}
/*! ----------------------------------------------------------------------------
* @fn Exec
*
* @brief execute a.
*/
int UBusCall::Exec (const std::string &aPath, const std::string &aMethod,
const std::string &aParameter, std::string &aResult)
{
int theRet = 0;
uint32_t theId;
struct blob_buf theBuf = {0};
struct ubus_context *theCtx = ubus_connect (NULL);
if (theCtx == NULL) {
fprintf (stderr, "%s - Failed to create an ubus context.\n", __PRETTY_FUNCTION__);
return -1;
}
blob_buf_init (&theBuf, 0);
if (!aParameter.empty()) {
if (!blobmsg_add_json_from_string (&theBuf, aParameter.c_str())) {
fprintf (stderr, "%s - Failed to parse message data\n", __PRETTY_FUNCTION__);
return -1;
}
}
theRet = ubus_lookup_id (theCtx, aPath.c_str(), &theId);
if (theRet) {
fprintf (stderr, "%s - ubus_lookup_id error = '%d'\n", __PRETTY_FUNCTION__, theRet);
ubus_free (theCtx);
blob_buf_free (&theBuf);
return theRet;
}
theRet = ubus_invoke (theCtx, theId, aMethod.c_str(), theBuf.head, receive_call_result_data, this, mTimeout * 1000);
ubus_free (theCtx);
blob_buf_free (&theBuf);
if (!mData.empty()) {
aResult = mData;
}
return theRet;
}
/*! ----------------------------------------------------------------------------
* @fn SetResult
*
* @brief Save the Result received by UBus.
*/
int UBusCall::SetResult (const std::string &aResult)
{
mData = aResult;
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn receive_call_result_data
*
* @brief UBus received call back.
*/
static void receive_call_result_data (struct ubus_request *aReq, int aType, struct blob_attr *aMsg)
{
UNUSED_PARAMETER (aType);
char *theStr;
UBusCall *anUBusObject = static_cast<UBusCall*>(aReq->priv);
theStr = blobmsg_format_json (aMsg, true);
anUBusObject->SetResult (std::string (theStr));
free (theStr);
}

View File

@@ -0,0 +1,61 @@
/*!
* (C) Copyright 2003-2015 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: 06/10/2014
*/
/*------------------------------- INCLUDES ----------------------------------*/
#include "ubuscpp/UBusObject.h"
/*------------------------------- GLOBALS ----------------------------------*/
/*--------------------------------- DEFINES ---------------------------------*/
/*! ----------------------------------------------------------------------------
* @fn awUBusObject
*
* @brief Construct a new awUBusObject, the object type and methods is
* provided statically using the type Derived
*
* @param anObjType object type, be carreful it should not be a temporary !!!
* @param AnObjName : the name of the object
* @param anObjID : TO BE DEFINED
* @param anObjPath : TO BE DEFINED
*
* @important : the parameter anObjType should not be a temporary !!!
*/
UBusObject::UBusObject (ubus_object_type &anObjType, const char *AnObjName, int anObjID,
const char *anObjPath)
{
name = AnObjName ? strdup (AnObjName) : 0;
id = anObjID;
path = anObjPath ? strdup (anObjPath) : 0;
type = &anObjType;
methods = anObjType.methods;
n_methods = anObjType.n_methods;
// nullify last fields
subscribe_cb = 0;
has_subscribers = false;
}
/*! ----------------------------------------------------------------------------
* @fn ~UBusObject
*
* @brief Destructor of the UBusObject
*/
UBusObject::~UBusObject (void)
{
free ((char*)name);
free ((char*)path);
}

33
lib/ubuscpp/src/common.h Normal file
View File

@@ -0,0 +1,33 @@
/*!
* UbusCall.cpp
*
* Copyright (c) 2015, 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: 21/03/2015
*
*/
#ifndef _UBUS_COMMON_H
#define _UBUS_COMMON_H
#ifndef UNUSED_PARAMETER
#define UNUSED_PARAMETER(x) (void)(x)
#endif
#endif /* _UBUS_COMMON_H */