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

7
lib/CMakeLists.txt Normal file
View File

@@ -0,0 +1,7 @@
# CMakeLists files in this project can
# refer to the root source directory of the project as ${HELLO_SOURCE_DIR} and
# to the root binary directory of the project as ${HELLO_BINARY_DIR}.
cmake_minimum_required (VERSION 2.8.11)
#add_subdirectory (alarmes)
add_subdirectory (ubuscpp/builders/cmake)

View File

@@ -0,0 +1,27 @@
cmake_minimum_required(VERSION 2.8.11)
include (libubus)
include (libubox)
include (libubuscpp)
project (libubuscpp)
file(
GLOB_RECURSE
source_files
../../src/UBusCall.cpp
../../src/UBusObject.cpp
)
add_library(
ubuscpp
SHARED
${source_files}
)
target_link_libraries (ubuscpp
LINK_PUBLIC
ubus
)
target_include_directories (ubuscpp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

View File

@@ -0,0 +1,52 @@
/*!
* UbusCall.h
*
* 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_CALL_H
#define _UBUS_CALL_H
/*-------------------------------- INCLUDES ---------------------------------*/
#include <string>
/*---------------------------------- CLASS ----------------------------------*/
class UBusCall
{
public:
UBusCall (void);
int Exec (const std::string &aPath, const std::string &aMethod,
const std::string &aParameter, std::string &aResult);
int SetTimeout (int aTimeout);
int SetResult (const std::string &aResult);
private:
int mTimeout;
std::string mData;
};
#endif /* _UBUS_CALL_H */

View File

@@ -0,0 +1,957 @@
/*!
* UbusCall.h
*
* 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_CPP_H
#define _UBUS_CPP_H
/*------------------------------- INCLUDES ----------------------------------*/
extern "C" {
#include <ubus/libubus.h>
}
namespace UBus {
namespace detail {
/**
* simple POD vehicle for arrays
*/
template<typename T>
struct Array {
T* ptr;
size_t count;
};
/**
* construct a detail::Array with one arg
* @note : T should be a POD and each argument will be copied as is using memcpy
*/
template<typename T>
static inline Array<T> makeArray(const T& e0) {
Array<T> result;
result.count = 1;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1) {
Array<T> result;
result.count = 2;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2) {
Array<T> result;
result.count = 3;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2,
const T& e3) {
Array<T> result;
result.count = 4;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
memcpy(&result.ptr[3], &e3, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2,
const T& e3, const T& e4) {
Array<T> result;
result.count = 5;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
memcpy(&result.ptr[3], &e3, sizeof(T));
memcpy(&result.ptr[4], &e4, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2,
const T& e3, const T& e4, const T& e5) {
Array<T> result;
result.count = 6;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
memcpy(&result.ptr[3], &e3, sizeof(T));
memcpy(&result.ptr[4], &e4, sizeof(T));
memcpy(&result.ptr[5], &e5, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2,
const T& e3, const T& e4, const T& e5,
const T& e6) {
Array<T> result;
result.count = 7;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
memcpy(&result.ptr[3], &e3, sizeof(T));
memcpy(&result.ptr[4], &e4, sizeof(T));
memcpy(&result.ptr[5], &e5, sizeof(T));
memcpy(&result.ptr[6], &e6, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2,
const T& e3, const T& e4, const T& e5,
const T& e6, const T& e7) {
Array<T> result;
result.count = 8;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
memcpy(&result.ptr[3], &e3, sizeof(T));
memcpy(&result.ptr[4], &e4, sizeof(T));
memcpy(&result.ptr[5], &e5, sizeof(T));
memcpy(&result.ptr[6], &e6, sizeof(T));
memcpy(&result.ptr[7], &e7, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2,
const T& e3, const T& e4, const T& e5,
const T& e6, const T& e7, const T& e8) {
Array<T> result;
result.count = 9;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
memcpy(&result.ptr[3], &e3, sizeof(T));
memcpy(&result.ptr[4], &e4, sizeof(T));
memcpy(&result.ptr[5], &e5, sizeof(T));
memcpy(&result.ptr[6], &e6, sizeof(T));
memcpy(&result.ptr[7], &e7, sizeof(T));
memcpy(&result.ptr[8], &e8, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2,
const T& e3, const T& e4, const T& e5,
const T& e6, const T& e7, const T& e8,
const T& e9) {
Array<T> result;
result.count = 10;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
memcpy(&result.ptr[3], &e3, sizeof(T));
memcpy(&result.ptr[4], &e4, sizeof(T));
memcpy(&result.ptr[5], &e5, sizeof(T));
memcpy(&result.ptr[6], &e6, sizeof(T));
memcpy(&result.ptr[7], &e7, sizeof(T));
memcpy(&result.ptr[8], &e8, sizeof(T));
memcpy(&result.ptr[9], &e9, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2,
const T& e3, const T& e4, const T& e5,
const T& e6, const T& e7, const T& e8,
const T& e9, const T& e10) {
Array<T> result;
result.count = 11;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
memcpy(&result.ptr[3], &e3, sizeof(T));
memcpy(&result.ptr[4], &e4, sizeof(T));
memcpy(&result.ptr[5], &e5, sizeof(T));
memcpy(&result.ptr[6], &e6, sizeof(T));
memcpy(&result.ptr[7], &e7, sizeof(T));
memcpy(&result.ptr[8], &e8, sizeof(T));
memcpy(&result.ptr[9], &e9, sizeof(T));
memcpy(&result.ptr[10], &e10, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2,
const T& e3, const T& e4, const T& e5,
const T& e6, const T& e7, const T& e8,
const T& e9, const T& e10, const T& e11) {
Array<T> result;
result.count = 12;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
memcpy(&result.ptr[3], &e3, sizeof(T));
memcpy(&result.ptr[4], &e4, sizeof(T));
memcpy(&result.ptr[5], &e5, sizeof(T));
memcpy(&result.ptr[6], &e6, sizeof(T));
memcpy(&result.ptr[7], &e7, sizeof(T));
memcpy(&result.ptr[8], &e8, sizeof(T));
memcpy(&result.ptr[9], &e9, sizeof(T));
memcpy(&result.ptr[10], &e10, sizeof(T));
memcpy(&result.ptr[11], &e11, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2,
const T& e3, const T& e4, const T& e5,
const T& e6, const T& e7, const T& e8,
const T& e9, const T& e10, const T& e11,
const T& e12) {
Array<T> result;
result.count = 13;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
memcpy(&result.ptr[3], &e3, sizeof(T));
memcpy(&result.ptr[4], &e4, sizeof(T));
memcpy(&result.ptr[5], &e5, sizeof(T));
memcpy(&result.ptr[6], &e6, sizeof(T));
memcpy(&result.ptr[7], &e7, sizeof(T));
memcpy(&result.ptr[8], &e8, sizeof(T));
memcpy(&result.ptr[9], &e9, sizeof(T));
memcpy(&result.ptr[10], &e10, sizeof(T));
memcpy(&result.ptr[11], &e11, sizeof(T));
memcpy(&result.ptr[12], &e12, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2,
const T& e3, const T& e4, const T& e5,
const T& e6, const T& e7, const T& e8,
const T& e9, const T& e10, const T& e11,
const T& e12, const T& e13) {
Array<T> result;
result.count = 14;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
memcpy(&result.ptr[3], &e3, sizeof(T));
memcpy(&result.ptr[4], &e4, sizeof(T));
memcpy(&result.ptr[5], &e5, sizeof(T));
memcpy(&result.ptr[6], &e6, sizeof(T));
memcpy(&result.ptr[7], &e7, sizeof(T));
memcpy(&result.ptr[8], &e8, sizeof(T));
memcpy(&result.ptr[9], &e9, sizeof(T));
memcpy(&result.ptr[10], &e10, sizeof(T));
memcpy(&result.ptr[11], &e11, sizeof(T));
memcpy(&result.ptr[12], &e12, sizeof(T));
memcpy(&result.ptr[13], &e13, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2,
const T& e3, const T& e4, const T& e5,
const T& e6, const T& e7, const T& e8,
const T& e9, const T& e10, const T& e11,
const T& e12, const T& e13, const T& e14) {
Array<T> result;
result.count = 15;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
memcpy(&result.ptr[3], &e3, sizeof(T));
memcpy(&result.ptr[4], &e4, sizeof(T));
memcpy(&result.ptr[5], &e5, sizeof(T));
memcpy(&result.ptr[6], &e6, sizeof(T));
memcpy(&result.ptr[7], &e7, sizeof(T));
memcpy(&result.ptr[8], &e8, sizeof(T));
memcpy(&result.ptr[9], &e9, sizeof(T));
memcpy(&result.ptr[10], &e10, sizeof(T));
memcpy(&result.ptr[11], &e11, sizeof(T));
memcpy(&result.ptr[12], &e12, sizeof(T));
memcpy(&result.ptr[13], &e13, sizeof(T));
memcpy(&result.ptr[14], &e14, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2,
const T& e3, const T& e4, const T& e5,
const T& e6, const T& e7, const T& e8,
const T& e9, const T& e10, const T& e11,
const T& e12, const T& e13, const T& e14,
const T& e15) {
Array<T> result;
result.count = 16;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
memcpy(&result.ptr[3], &e3, sizeof(T));
memcpy(&result.ptr[4], &e4, sizeof(T));
memcpy(&result.ptr[5], &e5, sizeof(T));
memcpy(&result.ptr[6], &e6, sizeof(T));
memcpy(&result.ptr[7], &e7, sizeof(T));
memcpy(&result.ptr[8], &e8, sizeof(T));
memcpy(&result.ptr[9], &e9, sizeof(T));
memcpy(&result.ptr[10], &e10, sizeof(T));
memcpy(&result.ptr[11], &e11, sizeof(T));
memcpy(&result.ptr[12], &e12, sizeof(T));
memcpy(&result.ptr[13], &e13, sizeof(T));
memcpy(&result.ptr[14], &e14, sizeof(T));
memcpy(&result.ptr[15], &e15, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2,
const T& e3, const T& e4, const T& e5,
const T& e6, const T& e7, const T& e8,
const T& e9, const T& e10, const T& e11,
const T& e12, const T& e13, const T& e14,
const T& e15, const T& e16) {
Array<T> result;
result.count = 17;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
memcpy(&result.ptr[3], &e3, sizeof(T));
memcpy(&result.ptr[4], &e4, sizeof(T));
memcpy(&result.ptr[5], &e5, sizeof(T));
memcpy(&result.ptr[6], &e6, sizeof(T));
memcpy(&result.ptr[7], &e7, sizeof(T));
memcpy(&result.ptr[8], &e8, sizeof(T));
memcpy(&result.ptr[9], &e9, sizeof(T));
memcpy(&result.ptr[10], &e10, sizeof(T));
memcpy(&result.ptr[11], &e11, sizeof(T));
memcpy(&result.ptr[12], &e12, sizeof(T));
memcpy(&result.ptr[13], &e13, sizeof(T));
memcpy(&result.ptr[14], &e14, sizeof(T));
memcpy(&result.ptr[15], &e15, sizeof(T));
memcpy(&result.ptr[16], &e16, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2,
const T& e3, const T& e4, const T& e5,
const T& e6, const T& e7, const T& e8,
const T& e9, const T& e10, const T& e11,
const T& e12, const T& e13, const T& e14,
const T& e15, const T& e16, const T& e17) {
Array<T> result;
result.count = 18;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
memcpy(&result.ptr[3], &e3, sizeof(T));
memcpy(&result.ptr[4], &e4, sizeof(T));
memcpy(&result.ptr[5], &e5, sizeof(T));
memcpy(&result.ptr[6], &e6, sizeof(T));
memcpy(&result.ptr[7], &e7, sizeof(T));
memcpy(&result.ptr[8], &e8, sizeof(T));
memcpy(&result.ptr[9], &e9, sizeof(T));
memcpy(&result.ptr[10], &e10, sizeof(T));
memcpy(&result.ptr[11], &e11, sizeof(T));
memcpy(&result.ptr[12], &e12, sizeof(T));
memcpy(&result.ptr[13], &e13, sizeof(T));
memcpy(&result.ptr[14], &e14, sizeof(T));
memcpy(&result.ptr[15], &e15, sizeof(T));
memcpy(&result.ptr[16], &e16, sizeof(T));
memcpy(&result.ptr[17], &e17, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2,
const T& e3, const T& e4, const T& e5,
const T& e6, const T& e7, const T& e8,
const T& e9, const T& e10, const T& e11,
const T& e12, const T& e13, const T& e14,
const T& e15, const T& e16, const T& e17,
const T& e18) {
Array<T> result;
result.count = 19;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
memcpy(&result.ptr[3], &e3, sizeof(T));
memcpy(&result.ptr[4], &e4, sizeof(T));
memcpy(&result.ptr[5], &e5, sizeof(T));
memcpy(&result.ptr[6], &e6, sizeof(T));
memcpy(&result.ptr[7], &e7, sizeof(T));
memcpy(&result.ptr[8], &e8, sizeof(T));
memcpy(&result.ptr[9], &e9, sizeof(T));
memcpy(&result.ptr[10], &e10, sizeof(T));
memcpy(&result.ptr[11], &e11, sizeof(T));
memcpy(&result.ptr[12], &e12, sizeof(T));
memcpy(&result.ptr[13], &e13, sizeof(T));
memcpy(&result.ptr[14], &e14, sizeof(T));
memcpy(&result.ptr[15], &e15, sizeof(T));
memcpy(&result.ptr[16], &e16, sizeof(T));
memcpy(&result.ptr[17], &e17, sizeof(T));
memcpy(&result.ptr[18], &e18, sizeof(T));
return result;
}
template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2,
const T& e3, const T& e4, const T& e5,
const T& e6, const T& e7, const T& e8,
const T& e9, const T& e10, const T& e11,
const T& e12, const T& e13, const T& e14,
const T& e15, const T& e16, const T& e17,
const T& e18, const T& e19) {
Array<T> result;
result.count = 20;
result.ptr = new T[result.count]();
memcpy(&result.ptr[0], &e0, sizeof(T));
memcpy(&result.ptr[1], &e1, sizeof(T));
memcpy(&result.ptr[2], &e2, sizeof(T));
memcpy(&result.ptr[3], &e3, sizeof(T));
memcpy(&result.ptr[4], &e4, sizeof(T));
memcpy(&result.ptr[5], &e5, sizeof(T));
memcpy(&result.ptr[6], &e6, sizeof(T));
memcpy(&result.ptr[7], &e7, sizeof(T));
memcpy(&result.ptr[8], &e8, sizeof(T));
memcpy(&result.ptr[9], &e9, sizeof(T));
memcpy(&result.ptr[10], &e10, sizeof(T));
memcpy(&result.ptr[11], &e11, sizeof(T));
memcpy(&result.ptr[12], &e12, sizeof(T));
memcpy(&result.ptr[13], &e13, sizeof(T));
memcpy(&result.ptr[14], &e14, sizeof(T));
memcpy(&result.ptr[15], &e15, sizeof(T));
memcpy(&result.ptr[16], &e16, sizeof(T));
memcpy(&result.ptr[17], &e17, sizeof(T));
memcpy(&result.ptr[18], &e18, sizeof(T));
memcpy(&result.ptr[19], &e19, sizeof(T));
return result;
}
/**
* simple copy of c string, free should be used to release memory
*/
static inline char* copy_str(const char* str) {
return str ? strdup(str) : 0;
}
/**
* generic function which copy an Array
*/
template<typename T>
static inline T* copy_array(const T* src, const size_t count, void(*copy_func)(T&, const T&)) {
if(count == 0 || src == 0) {
return 0;
}
T* result = new T[count];
for(size_t i = 0; i < count; ++i) {
copy_func(result[i], src[i]);
}
return result;
}
/**
* generic function which delete an array
*/
template<typename T>
static inline void delete_array(T* target, const size_t count,
void(*delete_func)(T&)) {
if(count == 0 || target == 0) {
return;
}
for(size_t i = 0; i < count; ++i) {
delete_func(target[i]);
}
delete[] target;
}
struct Arg : public blobmsg_policy { };
/**
* copy ubus method argument
* @important : does not delete the previous content
*/
static inline void copy_arg(blobmsg_policy& dest, const blobmsg_policy& src) {
dest.name = copy_str(src.name);
dest.type = src.type;
}
static inline blobmsg_policy* copy_args(const blobmsg_policy* src, const size_t count) {
return copy_array(src, count, copy_arg);
}
static inline void delete_arg(blobmsg_policy& target) {
free((char*)target.name);
}
static inline void delete_args(blobmsg_policy* target, size_t count) {
delete_array(target, count, delete_arg);
}
/**
* simple wrapper which avoid mixin of c and c++ declaration
*/
struct _Method : public ubus_method { };
/**
* copy a Method
* @important : does not delete the prpevious content !!!
*/
static inline void copy_method(ubus_method& dest, const ubus_method& src) {
dest.name = copy_str(src.name);
dest.handler = src.handler;
dest.mask = src.mask;
dest.policy = copy_args(src.policy, src.n_policy);
dest.n_policy = src.n_policy;
}
/**
* copy an array of Method
*/
static inline ubus_method* copy_methods(const ubus_method* src, const size_t count) {
return copy_array(src, count, copy_method);
}
/**
* delete an Method
*/
static inline void delete_method(ubus_method& target) {
free((char*)target.name);
delete_args((blobmsg_policy*)target.policy, target.n_policy);
}
/**
* delete an array of Method
*/
static inline void delete_methods(ubus_method* target, const size_t count) {
delete_array(target, count, delete_method);
}
}
class ObjectType : public ubus_object_type {
public:
ObjectType(const char* name_, const detail::_Method& m0) {
Init(name_, detail::makeArray(m0));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1) {
Init(name_, detail::makeArray(m0, m1));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2) {
Init(name_, detail::makeArray(m0, m1, m2));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2, const detail::_Method& m3) {
Init(name_, detail::makeArray(m0, m1, m2, m3));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2, const detail::_Method& m3, const detail::_Method m4) {
Init(name_, detail::makeArray(m0, m1, m2, m3, m4));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2, const detail::_Method& m3, const detail::_Method m4,
const detail::_Method& m5) {
Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2, const detail::_Method& m3, const detail::_Method m4,
const detail::_Method& m5, const detail::_Method& m6) {
Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2, const detail::_Method& m3, const detail::_Method m4,
const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7) {
Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2, const detail::_Method& m3, const detail::_Method m4,
const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7,
const detail::_Method& m8) {
Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4,
const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7,
const detail::_Method& m8, const detail::_Method& m9) {
Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4,
const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7,
const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10) {
Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4,
const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7,
const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10,
const detail::_Method& m11) {
Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4,
const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7,
const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10,
const detail::_Method& m11, const detail::_Method& m12) {
Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4,
const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7,
const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10,
const detail::_Method& m11, const detail::_Method& m12, const detail::_Method& m13) {
Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12,
m13));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4,
const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7,
const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10,
const detail::_Method& m11, const detail::_Method& m12, const detail::_Method& m13,
const detail::_Method& m14) {
Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12,
m13, m14));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4,
const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7,
const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10,
const detail::_Method& m11, const detail::_Method& m12, const detail::_Method& m13,
const detail::_Method& m14, const detail::_Method& m15) {
Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12,
m13, m14, m15));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4,
const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7,
const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10,
const detail::_Method& m11, const detail::_Method& m12, const detail::_Method& m13,
const detail::_Method& m14, const detail::_Method& m15, const detail::_Method& m16) {
Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12,
m13, m14, m15, m16));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4,
const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7,
const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10,
const detail::_Method& m11, const detail::_Method& m12, const detail::_Method& m13,
const detail::_Method& m14, const detail::_Method& m15, const detail::_Method& m16,
const detail::_Method& m17) {
Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12,
m13, m14, m15, m16, m17));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4,
const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7,
const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10,
const detail::_Method& m11, const detail::_Method& m12, const detail::_Method& m13,
const detail::_Method& m14, const detail::_Method& m15, const detail::_Method& m16,
const detail::_Method& m17, const detail::_Method& m18) {
Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12,
m13, m14, m15, m16, m17, m18));
}
ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1,
const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4,
const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7,
const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10,
const detail::_Method& m11, const detail::_Method& m12, const detail::_Method& m13,
const detail::_Method& m14, const detail::_Method& m15, const detail::_Method& m16,
const detail::_Method& m17, const detail::_Method& m18, const detail::_Method& m19) {
Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12,
m13, m14, m15, m16, m17, m18, m19));
}
ObjectType(const char* name_, detail::Array<detail::_Method> m) {
Init(name_, m);
}
ObjectType(const ObjectType& other) {
name = detail::copy_str(other.name);
id = other.id;
methods = detail::copy_methods(other.methods, other.n_methods);
n_methods = other.n_methods;
}
~ObjectType() {
free((char*)name);
detail::delete_methods((ubus_method*) methods, n_methods);
}
ObjectType& operator=(ObjectType other) {
Swap(other);
return *this;
}
void Swap(ObjectType& other) {
ubus_object_type tmp = other;
memcpy(&other, this, sizeof(*this));
memcpy(this, &tmp, sizeof(*this));
}
private:
// delegate constructor ...
void Init(const char* name_, detail::Array<detail::_Method> m) {
name = detail::copy_str(name_);
id = 0;
methods = m.ptr;
n_methods = m.count;
}
};
/**
* generate an ubus_method without arg
*/
static inline detail::_Method Method(const char* name, ubus_handler_t handler) {
detail::_Method result;
result.name = detail::copy_str(name);
result.handler = handler;
result.mask = 0;
result.policy = 0;
result.n_policy = 0;
return result;
}
/**
* generate an ubus_method with an array of args
*/
static inline detail::_Method Method(const char* name, ubus_handler_t handler,
detail::Array<detail::Arg> args) {
detail::_Method result;
result.name = detail::copy_str(name);
result.handler = handler;
result.mask = 0;
result.policy = args.ptr;
result.n_policy = args.count;
return result;
}
/**
* generate an ubus_method with one arg (shortcut for Array of one arg)
*/
static inline detail::_Method Method(const char* name, ubus_handler_t handler,
const detail::Arg& arg0) {
return Method(name, handler, detail::makeArray(arg0));
}
static inline detail::_Method Method(const char* name, ubus_handler_t handler,
const detail::Arg& arg0, const detail::Arg& arg1) {
return Method(name, handler, detail::makeArray(arg0, arg1));
}
static inline detail::_Method Method(const char* name, ubus_handler_t handler,
const detail::Arg& arg0, const detail::Arg& arg1,
const detail::Arg& arg2) {
return Method(name, handler, detail::makeArray(arg0, arg1, arg2));
}
static inline detail::_Method Method(const char* name, ubus_handler_t handler,
const detail::Arg& arg0, const detail::Arg& arg1,
const detail::Arg& arg2, const detail::Arg& arg3) {
return Method(name, handler, detail::makeArray(arg0, arg1, arg2, arg3));
}
static inline detail::_Method Method(const char* name, ubus_handler_t handler,
const detail::Arg& arg0, const detail::Arg& arg1,
const detail::Arg& arg2, const detail::Arg& arg3,
const detail::Arg& arg4) {
return Method(name, handler, detail::makeArray(arg0, arg1, arg2, arg3, arg4));
}
static inline detail::_Method Method(const char* name, ubus_handler_t handler,
const detail::Arg& arg0, const detail::Arg& arg1,
const detail::Arg& arg2, const detail::Arg& arg3,
const detail::Arg& arg4, const detail::Arg& arg5) {
return Method(name, handler, detail::makeArray(arg0, arg1, arg2, arg3, arg4, arg5));
}
static inline detail::_Method Method(const char* name, ubus_handler_t handler,
const detail::Arg& arg0, const detail::Arg& arg1,
const detail::Arg& arg2, const detail::Arg& arg3,
const detail::Arg& arg4, const detail::Arg& arg5,
const detail::Arg& arg6) {
return Method(name, handler, detail::makeArray(arg0, arg1, arg2, arg3, arg4, arg5, arg6));
}
static inline detail::_Method Method(const char* name, ubus_handler_t handler,
const detail::Arg& arg0, const detail::Arg& arg1,
const detail::Arg& arg2, const detail::Arg& arg3,
const detail::Arg& arg4, const detail::Arg& arg5,
const detail::Arg& arg6, const detail::Arg& arg7) {
return Method(name, handler, detail::makeArray(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7));
}
static inline detail::_Method Method(const char* name, ubus_handler_t handler,
const detail::Arg& arg0, const detail::Arg& arg1,
const detail::Arg& arg2, const detail::Arg& arg3,
const detail::Arg& arg4, const detail::Arg& arg5,
const detail::Arg& arg6, const detail::Arg& arg7,
const detail::Arg& arg8) {
return Method(name, handler, detail::makeArray(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8));
}
static inline detail::Arg Arg(const char* name, enum blobmsg_type type) {
detail::Arg res;
res.name = detail::copy_str(name);
res.type = type;
return res;
}
static inline detail::Arg UnspecArg(const char* name) {
return Arg(name, BLOBMSG_TYPE_UNSPEC);
}
static inline detail::Arg ArrayArg(const char* name) {
return Arg(name, BLOBMSG_TYPE_ARRAY);
}
static inline detail::Arg TableArg(const char* name) {
return Arg(name, BLOBMSG_TYPE_TABLE);
}
static inline detail::Arg StringArg(const char* name) {
return Arg(name, BLOBMSG_TYPE_STRING);
}
static inline detail::Arg Int64Arg(const char* name) {
return Arg(name, BLOBMSG_TYPE_INT64);
}
static inline detail::Arg Int32Arg(const char* name) {
return Arg(name, BLOBMSG_TYPE_INT32);
}
static inline detail::Arg Int16Arg(const char* name) {
return Arg(name, BLOBMSG_TYPE_INT16);
}
static inline detail::Arg Int8Arg(const char* name) {
return Arg(name, BLOBMSG_TYPE_INT8);
}
static inline detail::Arg BoolArg(const char* name) {
return Arg(name, BLOBMSG_TYPE_BOOL);
}
} // namespace UBus
/**
* Generate a function which dispatch a ubus method call to the appropriate
* method of a aw_ubus_object.
*
* This template generate a function compatible with ubus_handler
*
* @tparam ObjectType : the type of the object containing the method to call
* @tparam Method : the member function of ObjectType to call, this member function
* has the signature :
* int(struct ubus_context *ctx, struct ubus_request_data *req, struct blob_attr *msg)
*/
template<typename ObjectType,
int (ObjectType::*Method)(struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
>
static int ubus_hook (struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
return (static_cast<ObjectType*>(obj)->*Method)(ctx, req, msg);
}
/**
* Declare a ubus trampoline function which map call the right method of an ubus object
* @param CLASS : class name of the instance object targeted by the trampoline
* @param METHOD : method name targeted by the trampoline
* @return a ubus_handler_t
*/
#define UBUS_CPP(CLASS, METHOD) &ubus_hook<CLASS, &CLASS::METHOD>
#endif /* _UBUS_CPP_H */

View File

@@ -0,0 +1,51 @@
/*!
* UbusCall.h
*
* 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_OBJECT_H
#define _UBUS_OBJECT_H
/*------------------------------- INCLUDES ----------------------------------*/
#include "ubuscpp/UBusCpp.h"
/*----------------------------- Dependencies --------------------------------*/
/*--------------------------------- CLASS ----------------------------------*/
class UBusObject : public ubus_object {
// Disable copy construction and copy assignement
UBusObject (const UBusObject&);
UBusObject &operator=(const UBusObject&);
public:
UBusObject (ubus_object_type &anObjType, const char *AnObjName, int anObjID = 0,
const char *anObjPath = 0);
virtual ~UBusObject (void);
};
#endif /* _UBUS_OBJECT_H */

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 */

13
src/CMakeLists.txt Normal file
View File

@@ -0,0 +1,13 @@
# CMakeLists files in this project can
# refer to the root source directory of the project as ${HELLO_SOURCE_DIR} and
# to the root binary directory of the project as ${HELLO_BINARY_DIR}.
cmake_minimum_required (VERSION 2.8.11)
#add_subdirectory (alarmd/builders/cmake)
add_subdirectory (domod/builders/cmake)
#add_subdirectory (chacond/builders/cmake)
#add_subdirectory (sprinklersd/builders/cmake)
#add_subdirectory (rf_listenerd/builders/cmake)
#add_subdirectory (restd/builders/cmake)
#add_subdirectory (ui/builders/cmake)
# add_subdirectory (board/builders/cmake)

View File

@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 2.8.11)
project(domod)
include (libubus)
include (libubox)
include (libubuscpp)
include (libjsoncpp)
include_directories(../../src)
set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -Wextra -pedantic -Werror=strict-aliasing")
file(
GLOB_RECURSE
source_files
../../src/main.cpp
../../src/ubus/capabilities.cpp
../../src/ubus/capabilities_lights.cpp
../../src/ubus/capabilities_shutters.cpp
../../src/ubus/capabilities_sprinklers.cpp
../../src/ubus/speach_command.cpp
../../src/models/Devices.cpp
../../src/helpers/Tokenizer.cpp
../../src/helpers/Strings.cpp
)
add_executable (
domod
${source_files}
)
target_link_libraries (domod
LINK_PUBLIC
jsoncpp
ubuscpp
rt
)

View File

@@ -0,0 +1,57 @@
/*!
* strings_helpers.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: 28/06/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include <locale>
#include <algorithm>
#include "Strings.h"
typedef std::string::value_type char_t;
/*! ----------------------------------------------------------------------------
* @fn SpeachCommand
*
* @brief Constructor of the UBus Speach Command execution.
*/
char_t up_char (char_t ch)
{
return std::use_facet< std::ctype< char_t > >( std::locale() ).toupper (ch);
}
/*! ----------------------------------------------------------------------------
* @fn toupper
*
* @brief method to make a toupper missing commands on the std::string.
*/
std::string toupper (const std::string &src)
{
//std::string result;
//std::transform (src.begin(), src.end(), std::back_inserter (result), up_char);
//return result;
return src;
}

View File

@@ -0,0 +1,34 @@
/*!
* strings_helpers.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: 28/06/2015
*
*/
#ifndef _HELPERS_STRINGS_H
#define _HELPERS_STRINGS_H
#include <string>
std::string toupper (const std::string &src);
#endif /* _HELPERS_STRINGS_H */

View File

@@ -0,0 +1,116 @@
/*!
* Tokenizer.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: 04/05/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include "Tokenizer.h"
const std::string Tokenizer::DELIMITERS(" \t\n\r");
/*! ----------------------------------------------------------------------------
* @fn Tokenizer
*
* @brief Constructor of the Tokenizer.
*/
Tokenizer::Tokenizer (const std::string& aString) :
m_offset (0),
m_string (aString),
m_delimiters (DELIMITERS) {}
/*! ----------------------------------------------------------------------------
* @fn Tokenizer
*
* @brief Constructor of the Tokenizer.
*/
Tokenizer::Tokenizer (const std::string& aString, const std::string& aDelimiters) :
m_offset (0),
m_string (aString),
m_delimiters (aDelimiters) {}
/*! ----------------------------------------------------------------------------
* @fn NextToken
*
* @brief Return true if we found another token.
*/
bool Tokenizer::NextToken (void)
{
return NextToken (m_delimiters);
}
/*! ----------------------------------------------------------------------------
* @fn NextToken
*
* @brief Return true if we found another token.
*/
bool Tokenizer::NextToken (const std::string& delimiters)
{
size_t i = m_string.find_first_not_of (delimiters, m_offset);
if (std::string::npos == i) {
m_offset = m_string.length ();
return false;
}
size_t j = m_string.find_first_of (delimiters, i);
if (std::string::npos == j) {
m_token = m_string.substr (i);
m_offset = m_string.length ();
return true;
}
m_token = m_string.substr (i, j - i);
m_offset = j;
return true;
}
/*! ----------------------------------------------------------------------------
* @fn GetToken
*
* @brief Return the Current Token.
*/
const std::string Tokenizer::GetToken (void) const
{
return m_token;
}
/*! ----------------------------------------------------------------------------
* @fn Reset
*
* @brief Return the Current Token navigation.
*/
void Tokenizer::Reset (void)
{
m_offset = 0;
}

View File

@@ -0,0 +1,55 @@
/*!
* Tokenizer.h
*
* 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: 04/05/2015
*
*/
#ifndef _HELPERS_TOKENIZER_H
#define _HELPERS_TOKENIZER_H
/*------------------------------- INCLUDES ----------------------------------*/
#include <string>
/*--------------------------------- Define ----------------------------------*/
/*--------------------------------- CLASS ----------------------------------*/
class Tokenizer
{
public:
static const std::string DELIMITERS;
Tokenizer (const std::string& str);
Tokenizer (const std::string& str, const std::string& delimiters);
bool NextToken ();
bool NextToken (const std::string& delimiters);
const std::string GetToken () const;
void Reset ();
protected:
size_t m_offset;
const std::string m_string;
std::string m_token;
std::string m_delimiters;
};
#endif /* _HELPERS_TOKENIZER_H */

131
src/domod/src/main.cpp Normal file
View File

@@ -0,0 +1,131 @@
/*!
* main.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: 04/03/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include <cstdio>
extern "C" {
#include <stdlib.h> /* getenv */
#include <libubus.h>
}
#include "ubus/capabilities.h"
#include "ubus/capabilities_lights.h"
#include "ubus/capabilities_shutters.h"
#include "ubus/capabilities_sprinklers.h"
#include "ubus/speach_command.h"
#include "models/Devices.h"
/*--------------------------------- GLOBALS ---------------------------------*/
/*! ----------------------------------------------------------------------------
* @fn setupUbus
*
* @brief Setup the UBus Mechanism.
*/
extern "C" {
static struct ubus_context *setupUbus (void)
{
ubus_context *theCtx;
if (uloop_init () !=0) {
fprintf (stderr, "Failed to init Uloop.\n");
return NULL;
}
signal (SIGPIPE, SIG_IGN);
theCtx = ubus_connect (NULL);
if (!theCtx) {
fprintf (stderr, "Failed to connect to ubus\n");
return NULL;
}
ubus_add_uloop (theCtx);
return theCtx;
}
} // SetupUbus.
/*! ----------------------------------------------------------------------------
* @fn main
*
* @brief Main function of Domo Daemon.
*/
int main (void)
{
int theRet = 0;
std::string thePath;
struct ubus_context *theCtx = NULL;
thePath = getenv ("DOMO_WRITE_PATH");
thePath += "/Devices.json";
//printf ("DOMO PATH: %s\n", thePath.c_str());
Devices theDevices (thePath);
if (theDevices.Setup() != 0) {
return -1;
}
Capabilities theCapabilities;
CapabilitiesLights theCapLights (&theDevices);
CapabilitiesShutters theCapShutters (&theDevices);
CapabilitiesSprinklers theCapSprinklers (&theDevices);
SpeachCommand theSpeachCommand (&theDevices);
/* Setup the Ubus context. */
theCtx = setupUbus ();
if (theCtx == NULL) {
return -1;
}
/* Add the UBus to the model exposed. */
ubus_add_object (theCtx, &theCapabilities);
ubus_add_object (theCtx, &theCapLights);
ubus_add_object (theCtx, &theCapShutters);
ubus_add_object (theCtx, &theCapSprinklers);
ubus_add_object (theCtx, &theSpeachCommand);
/* Main Event Loop. */
uloop_run ();
ubus_free (theCtx);
uloop_done ();
return theRet;
}

View File

@@ -0,0 +1,205 @@
/*!
* Devices.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: 27/03/2015
*
*/
/*------------------------------- INCLUDES ----------------------------------*/
#include <cstdio>
#include <iostream>
#include <fstream>
#include "helpers/Strings.h"
#include "Devices.h"
/*! ----------------------------------------------------------------------------
* @fn Devices
*
* @brief Constructor of the Devices Models.
*/
Devices::Devices (std::string aPath):
mDevicesPath (aPath)
{
}
/*! ----------------------------------------------------------------------------
* @fn ~Devices
*
* @brief Destructor of the Devices Models.
*/
Devices::~Devices (void)
{
}
/*! ----------------------------------------------------------------------------
* @fn Setup
*
* @brief Setup the Device object.
*/
int Devices::Setup (void)
{
Json::Reader theReader;
std::ifstream theDevicesFile (mDevicesPath.c_str());
if (!theReader.parse (theDevicesFile, mRoot)) {
fprintf (stderr, "Failed to parse the Devices File (%s).\n",
mDevicesPath.c_str());
return -1;
}
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn get
*
* @brief get the list of devices of a specific capabilities.
*/
std::string Devices::get (const std::string &aCapability)
{
Json::StyledWriter theWriter;
Json::Value theOutput;
printf ("Get aCapability: %s\n", aCapability.c_str());
theOutput[aCapability] = mRoot[aCapability]["data"];
return theWriter.write (theOutput).c_str();
}
/*! ----------------------------------------------------------------------------
* @fn get
*
* @brief get the device of a specific capabilities, with the ID.
*/
Json::Value Devices::get (const std::string &aCapability, int anId)
{
Json::Value theResult;
for (const Json::Value& theElement : mRoot[aCapability]["data"]) {
if (theElement["id"].asInt() == anId) {
theResult = theElement;
}
}
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn set
*
* @brief get the device of a specific capabilities, with the ID.
*/
int Devices::set (const std::string &aCapability, int anId, const Json::Value &aNewElement)
{
bool theStateChange = false;
for (Json::Value& theElement : mRoot[aCapability]["data"]) {
if (theElement["id"].asInt() == anId) {
Json::Value::Members memberNames = theElement.getMemberNames();
for(unsigned int i=0; i<memberNames.size(); ++i) {
std::string theMName = memberNames[i];
if (theElement[theMName] != aNewElement[theMName]) {
theElement[theMName] = aNewElement[theMName];
theStateChange = true;
}
}
}
}
if (theStateChange) {
printf ("Model Change, We save it....\n");
Json::StyledStreamWriter theWriter;
std::ofstream theOutput (mDevicesPath);
theWriter.write (theOutput,mRoot);
}
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn setDevice
*
* @brief get the device of a specific capabilities, with the ID.
*/
bool Devices::setDevice (const std::string &aCapability,
const std::string &aDeviceName, bool aState)
{
bool theStateChange = false;
fprintf (stdout, "setDevice cap:%s deviceName: %s\n", aCapability.c_str(),
aDeviceName.c_str());
for (Json::Value& theCap : mRoot) {
fprintf (stdout, "ici: %s\n", theCap["speach_name"].asString().c_str());
if (theCap["speach_name"].asString() == aCapability) {
for (Json::Value& theElement : theCap["data"]) {
if (theElement["speach_name"].asString() == aDeviceName) {
fprintf (stdout, "Device found.\n");
if (theElement["state"] != aState) {
printf ("Model Change, We save it....\n");
Json::StyledStreamWriter theWriter;
std::ofstream theOutput (mDevicesPath);
theWriter.write (theOutput,mRoot);
//TODO faire le chemin la bonne capability.
return true;
}
}
}
}
}
return false;
}
/*! ----------------------------------------------------------------------------
* @fn isCapability
*
* @brief return true if the capability is available.
*/
bool Devices::isCapability (const std::string &aCapability)
{
fprintf (stdout, "try to found: %s\n", aCapability.c_str());
for (Json::Value& theElement : mRoot) {
fprintf (stdout, "ici: %s\n", theElement["speach_name"].asString().c_str());
if (toupper(theElement["speach_name"].asString()) == aCapability)
return true;
}
return false;
}

View File

@@ -0,0 +1,56 @@
/*!
* Devices.h
*
* 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: 27/03/2015
*
*/
#ifndef _MODELS_DEVICES_H
#define _MODELS_DEVICES_H
/*------------------------------- INCLUDES ----------------------------------*/
#include <jsoncpp/json.h>
#include <string>
class Devices {
public:
Devices (std::string aPath);
~Devices (void);
int Setup (void);
std::string get (const std::string &aCapability);
Json::Value get (const std::string &aCapability, int anId);
int set (const std::string &aCapability, int anId, const Json::Value &anElement);
bool setDevice (const std::string &aCapability,
const std::string &aDeviceName, bool aState);
bool isCapability (const std::string &aCapability);
private:
std::string mDevicesPath;
Json::Value mRoot;
};
#endif /* _MODELS_DEVICES_H */

View File

@@ -0,0 +1,96 @@
/*!
* capabilities.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: 23/03/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include <fstream>
#include <string>
#include <sstream>
extern "C" {
#include <libubox/blobmsg_json.h>
}
#include "capabilities.h"
namespace {
using namespace UBus;
static ObjectType gCapabilitiesUbus_types(
"capabilities",
Method("get", UBUS_CPP(Capabilities, Get))
);
}
/*! ----------------------------------------------------------------------------
* @fn Capabilities
*
* @brief Constructor of the UBus Mixer Volume.
*/
Capabilities::Capabilities (void) :
UBusObject (gCapabilitiesUbus_types, "domo.capabilities")
{
}
/*! ----------------------------------------------------------------------------
* @fn ~Capabilities
*
* @brief Destructor of the UBus Mixer Volume.
*/
Capabilities::~Capabilities (void)
{
}
/*! ----------------------------------------------------------------------------
* @fn Get
*
* @brief Get the List of the Capabilities.
*/
int Capabilities::Get (struct ubus_context *aCtx, struct ubus_request_data *aReq,
struct blob_attr *aMsg)
{
int theResult;
struct blob_buf theBuf = {0};
std::ifstream theCapFile ("./rsc/capabilities.json");
std::stringstream theBuffer;
theBuffer << theCapFile.rdbuf();
blob_buf_init (&theBuf, 0);
blobmsg_add_json_from_string (&theBuf, theBuffer.str().c_str());
theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
blob_buf_free (&theBuf);
return theResult;
}

View File

@@ -0,0 +1,50 @@
/*!
* capabilities.h
*
* 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: 23/03/2015
*
*/
#ifndef _UBUS_CAPABILITIES_H
#define _UBUS_CAPABILITIES_H
/*------------------------------- INCLUDES ----------------------------------*/
#include <stdint.h>
#include <ubuscpp/UBusObject.h>
/*--------------------------------- Define ----------------------------------*/
/*--------------------------------- CLASS ----------------------------------*/
class Capabilities : public UBusObject {
public:
Capabilities (void);
~Capabilities (void);
int Get (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
private:
};
#endif /* _UBUS_CAPABILITIES_H */

View File

@@ -0,0 +1,191 @@
/*!
* capabilities_lights.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: 24/03/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
extern "C" {
#include <libubox/blobmsg_json.h>
}
#include <ubuscpp/UBusCall.h>
#include "models/Devices.h"
#include "capabilities_lights.h"
namespace {
using namespace UBus;
static ObjectType gCapabilitiesLightsUbus_types(
"lights",
Method("get", UBUS_CPP(CapabilitiesLights, Get)),
Method("delete", UBUS_CPP(CapabilitiesLights, Delete)),
Method("put", UBUS_CPP(CapabilitiesLights, Put)),
Method("post", UBUS_CPP(CapabilitiesLights, Post))
);
}
/*! ----------------------------------------------------------------------------
* @fn CapabilitiesLights
*
* @brief Constructor of the UBus Mixer Volume.
*/
CapabilitiesLights::CapabilitiesLights (Devices *aDevice) :
UBusObject (gCapabilitiesLightsUbus_types, "domo.capabilities.lights"),
mDevices (aDevice)
{
}
/*! ----------------------------------------------------------------------------
* @fn ~CapabilitiesLights
*
* @brief Destructor of the UBus Mixer Volume.
*/
CapabilitiesLights::~CapabilitiesLights (void)
{
}
/*! ----------------------------------------------------------------------------
* @fn Get
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesLights::Get (struct ubus_context *aCtx, struct ubus_request_data *aReq,
struct blob_attr *aMsg)
{
int theResult;
struct blob_buf theBuf = {0};
blob_buf_init (&theBuf, 0);
blobmsg_add_json_from_string (&theBuf, mDevices->get("Lights").c_str());
theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
blob_buf_free (&theBuf);
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn Delete
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesLights::Delete (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
{
int theResult = 0;
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn Put
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesLights::Put (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
{
int theResult = 0;
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn Post
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesLights::Post (struct ubus_context *aCtx, struct ubus_request_data *aReq,
struct blob_attr *aMsg)
{
int theResult;
struct blob_buf theBuf = {0};
char *theString = blobmsg_format_json (aMsg, true);
Json::Reader theReader;
Json::StyledWriter theWriter;
Json::Value theRoot;
Json::Value theOutput;
Json::Value theElement;
int theID;
bool theState;
if (!theReader.parse (theString, theRoot)) {
fprintf (stderr, "Failed parse the parameters.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
theID = theRoot["id"].asInt();
theState = theRoot["state"].asBool();
theElement = mDevices->get("Lights", theID);
theElement["state"] = theState;
if (ChangeState(theElement) == 0) {
mDevices->set("Lights", theID, theElement);
}
theOutput["Lights"] = theElement;
blob_buf_init (&theBuf, 0);
blobmsg_add_json_from_string (&theBuf, theWriter.write(theOutput).c_str());
theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
blob_buf_free (&theBuf);
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn ChangeState
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesLights::ChangeState (const Json::Value &aLight)
{
UBusCall theCmd;
Json::Value theParam;
Json::StyledWriter theWriter;
std::string theResult;
theParam["sender"] = aLight["sender"];
theParam["interruptor"] = aLight["interruptor"];
theParam["state"] = aLight["state"];
return theCmd.Exec ("chacon", "set", theWriter.write(theParam).c_str(),theResult);
}

View File

@@ -0,0 +1,60 @@
/*!
* capabilities_lights.h
*
* 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: 24/03/2015
*
*/
#ifndef _UBUS_CAPABILITIES_LIGHTS_H
#define _UBUS_CAPABILITIES_LIGHTS_H
/*------------------------------- INCLUDES ----------------------------------*/
#include <stdint.h>
#include <jsoncpp/json.h>
#include <ubuscpp/UBusObject.h>
/*--------------------------------- Define ----------------------------------*/
class Devices;
/*--------------------------------- CLASS ----------------------------------*/
class CapabilitiesLights : public UBusObject {
public:
CapabilitiesLights (Devices *aDevice);
~CapabilitiesLights (void);
int Get (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Delete (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Put (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Post (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
private:
int ChangeState (const Json::Value &aLight);
Devices *mDevices;
};
#endif /* _UBUS_CAPABILITIES_LIGHTS_H */

View File

@@ -0,0 +1,202 @@
/*!
* capabilities_shutters.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: 24/03/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
extern "C" {
#include <libubox/blobmsg_json.h>
}
#include "models/Devices.h"
#include "capabilities_shutters.h"
namespace {
using namespace UBus;
static ObjectType gCapabilitiesShuttersUbus_types(
"shutters",
Method("get", UBUS_CPP(CapabilitiesShutters, Get)),
Method("delete", UBUS_CPP(CapabilitiesShutters, Delete)),
Method("put", UBUS_CPP(CapabilitiesShutters, Put)),
Method("post", UBUS_CPP(CapabilitiesShutters, Post)),
Method("open", UBUS_CPP(CapabilitiesShutters, Open)),
Method("close", UBUS_CPP(CapabilitiesShutters, Close)),
Method("up", UBUS_CPP(CapabilitiesShutters, Up)),
Method("down", UBUS_CPP(CapabilitiesShutters, Down)),
Method("position", UBUS_CPP(CapabilitiesShutters, Position))
);
}
/*! ----------------------------------------------------------------------------
* @fn CapabilitiesShutters
*
* @brief Constructor of the UBus Mixer Volume.
*/
CapabilitiesShutters::CapabilitiesShutters (Devices *aDevice) :
UBusObject (gCapabilitiesShuttersUbus_types, "domo.capabilities.shutters"),
mDevices (aDevice)
{
}
/*! ----------------------------------------------------------------------------
* @fn ~CapabilitiesShutters
*
* @brief Destructor of the UBus Mixer Volume.
*/
CapabilitiesShutters::~CapabilitiesShutters (void)
{
}
/*! ----------------------------------------------------------------------------
* @fn Get
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesShutters::Get (struct ubus_context *aCtx, struct ubus_request_data *aReq,
struct blob_attr *aMsg)
{
int theResult;
struct blob_buf theBuf = {0};
blob_buf_init (&theBuf, 0);
blobmsg_add_json_from_string (&theBuf, mDevices->get("Shutters").c_str());
theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
blob_buf_free (&theBuf);
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn Delete
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesShutters::Delete (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
{
int theResult = 0;
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn Put
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesShutters::Put (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
{
int theResult = 0;
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn Post
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesShutters::Post (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
{
int theResult = 0;
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn Open
*
* @brief Open the Shutter
*/
int CapabilitiesShutters::Open (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
{
int theResult = 0;
printf ("Open the Shutter\n");
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn Close
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesShutters::Close (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
{
int theResult = 0;
printf ("Close the Shutter\n");
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn Up
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesShutters::Up (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
{
int theResult = 0;
printf ("Up the Shutter\n");
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn Down
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesShutters::Down (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
{
int theResult = 0;
printf ("Down the Shutter\n");
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn Position
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesShutters::Position (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
{
int theResult = 0;
printf ("Position the Shutter\n");
return theResult;
}

View File

@@ -0,0 +1,64 @@
/*!
* capabilities_shutters.h
*
* 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: 24/03/2015
*
*/
#ifndef _UBUS_CAPABILITIES_SHUTTERS_H
#define _UBUS_CAPABILITIES_SHUTTERS_H
/*------------------------------- INCLUDES ----------------------------------*/
#include <stdint.h>
#include <ubuscpp/UBusObject.h>
/*--------------------------------- Define ----------------------------------*/
class Devices;
/*--------------------------------- CLASS ----------------------------------*/
class CapabilitiesShutters : public UBusObject {
public:
CapabilitiesShutters (Devices *aDevice);
~CapabilitiesShutters (void);
int Get (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Delete (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Put (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Post (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Open (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Close (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Up (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Down (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Position (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
private:
Devices *mDevices;
};
#endif /* _UBUS_CAPABILITIES_SHUTTERS_H */

View File

@@ -0,0 +1,132 @@
/*!
* capabilities_sprinklers.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: 24/03/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
extern "C" {
#include <libubox/blobmsg_json.h>
}
#include "models/Devices.h"
#include "capabilities_sprinklers.h"
namespace {
using namespace UBus;
static ObjectType gCapabilitiesSprinklersUbus_types(
"sprinklers",
Method("get", UBUS_CPP(CapabilitiesSprinklers, Get)),
Method("delete", UBUS_CPP(CapabilitiesSprinklers, Delete)),
Method("put", UBUS_CPP(CapabilitiesSprinklers, Put)),
Method("post", UBUS_CPP(CapabilitiesSprinklers, Post))
);
}
/*! ----------------------------------------------------------------------------
* @fn CapabilitiesSprinkler
*
* @brief Constructor of the UBus Mixer Volume.
*/
CapabilitiesSprinklers::CapabilitiesSprinklers (Devices *aDevice) :
UBusObject (gCapabilitiesSprinklersUbus_types, "domo.capabilities.sprinklers"),
mDevices (aDevice)
{
}
/*! ----------------------------------------------------------------------------
* @fn ~CapabilitiesSprinklers
*
* @brief Destructor of the UBus Mixer Volume.
*/
CapabilitiesSprinklers::~CapabilitiesSprinklers (void)
{
}
/*! ----------------------------------------------------------------------------
* @fn Get
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesSprinklers::Get (struct ubus_context *aCtx, struct ubus_request_data *aReq,
struct blob_attr *aMsg)
{
int theResult;
struct blob_buf theBuf = {0};
blob_buf_init (&theBuf, 0);
blobmsg_add_json_from_string (&theBuf, mDevices->get("Sprinkers").c_str());
theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
blob_buf_free (&theBuf);
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn Delete
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesSprinklers::Delete (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
{
int theResult = 0;
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn Put
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesSprinklers::Put (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
{
int theResult = 0;
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn Post
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesSprinklers::Post (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
{
int theResult = 0;
return theResult;
}

View File

@@ -0,0 +1,56 @@
/*!
* capabilities_sprinklers.h
*
* 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: 24/03/2015
*
*/
#ifndef _UBUS_CAPABILITIES_SPRINKLERS_H
#define _UBUS_CAPABILITIES_SPRINKLERS_H
/*------------------------------- INCLUDES ----------------------------------*/
#include <stdint.h>
#include <ubuscpp/UBusObject.h>
/*--------------------------------- Define ----------------------------------*/
class Devices;
/*--------------------------------- CLASS ----------------------------------*/
class CapabilitiesSprinklers : public UBusObject {
public:
CapabilitiesSprinklers (Devices *aDevice);
~CapabilitiesSprinklers (void);
int Get (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Delete (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Put (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Post (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
private:
Devices *mDevices;
};
#endif /* _UBUS_CAPABILITIES_SPRINKLERS_H */

View File

@@ -0,0 +1,181 @@
/*!
* speach_command.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: 04/05/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include <vector>
extern "C" {
#include <libubox/blobmsg_json.h>
}
#include <ubuscpp/UBusCall.h>
#include "models/Devices.h"
#include "helpers/Tokenizer.h"
#include "helpers/Strings.h"
#include "speach_command.h"
namespace {
using namespace UBus;
static ObjectType gSpeachCommandUbus_types(
"speach",
Method("post", UBUS_CPP(SpeachCommand, Post))
);
}
/*! ----------------------------------------------------------------------------
* @fn SpeachCommand
*
* @brief Constructor of the UBus Speach Command execution.
*/
SpeachCommand::SpeachCommand (Devices *aDevice) :
UBusObject (gSpeachCommandUbus_types, "domo.speach.command"),
mDevices (aDevice)
{
}
/*! ----------------------------------------------------------------------------
* @fn ~SpeachCommand
*
* @brief Destructor of the UBus Speach Command execution.
*/
SpeachCommand::~SpeachCommand (void)
{
}
/*! ----------------------------------------------------------------------------
* @fn Post
*
* @brief Get the List of the Capabilities.
*/
int SpeachCommand::Post (struct ubus_context *aCtx, struct ubus_request_data *aReq,
struct blob_attr *aMsg)
{
int theResult = 0;
struct blob_buf theBuf = {0};
char *theString = blobmsg_format_json (aMsg, true);
Json::Reader theReader;
Json::Value theRoot;
std::string theCommand;
if (!theReader.parse (theString, theRoot)) {
fprintf (stderr, "Failed parse the parameters.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
theCommand = theRoot["command"].asString();
if (theCommand.empty()) {
fprintf (stderr, "the Command parameter is not present or empty...\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
theResult = AnalyseAndExecute (theCommand);
blob_buf_init (&theBuf, 0);
blobmsg_add_json_from_string (&theBuf,"{}");
ubus_send_reply (aCtx, aReq, theBuf.head);
blob_buf_free (&theBuf);
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn AnalyseAndExecute
*
* @brief analyse and Execute the following commmand.
*/
int SpeachCommand::AnalyseAndExecute (std::string aCommand)
{
std::vector <std::string> theTokens;
Tokenizer s(aCommand, " ");
fprintf (stdout, "Analyse: <%s>\n", aCommand.c_str());
int theSM = 0;
bool theState = false;
std::string theCapability;
std::string theDeviceName;
while (s.NextToken()) {
theTokens.push_back (s.GetToken());
fprintf (stdout, "word: <%s>\n", s.GetToken().c_str());
// Check the Command
if (theSM == 0) {
if (s.GetToken() == "allumer") {
theState = true;
}
else if (s.GetToken() == "Éteindre") {
theState = false;
}
else if (s.GetToken() == "ouvrir") {
theState = true;
}
else if (s.GetToken() == "fermer") {
theState = false;
}
else {
fprintf (stderr, "Uknown command: %s:\n", s.GetToken().c_str());
return -1;
}
}
else if (theSM == 1) {
theCapability = s.GetToken();
if (!mDevices->isCapability (theCapability)) {
fprintf (stderr, "Uknown capability: <%s>\n", s.GetToken().c_str());
return -2;
}
}
else if (theSM == 2) {
theDeviceName = s.GetToken();
if (!mDevices->setDevice (theCapability, theDeviceName, theState)) {
fprintf (stderr, "Uknown Device: <%s>\n", s.GetToken().c_str());
return -3;
}
}
theSM++;
}
return 0;
}

View File

@@ -0,0 +1,57 @@
/*!
* speach_command.h
*
* 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: 04/05/2015
*
*/
#ifndef _UBUS_SPEACH_COMMANDS_H
#define _UBUS_SPEACH_COMMANDS_H
/*------------------------------- INCLUDES ----------------------------------*/
#include <stdint.h>
#include <jsoncpp/json.h>
#include <ubuscpp/UBusObject.h>
/*--------------------------------- Define ----------------------------------*/
class Devices;
/*--------------------------------- CLASS ----------------------------------*/
class SpeachCommand : public UBusObject {
public:
SpeachCommand (Devices *aDevice);
~SpeachCommand (void);
int Post (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
private:
int AnalyseAndExecute (std::string aCommand);
Devices *mDevices;
};
#endif /* _UBUS_SPEACH_COMMANDS_H */