backport ubuscpp, chacond, and sprinklersd

This commit is contained in:
2017-04-21 22:48:42 +02:00
parent ad868bda8e
commit 3201fb4735
27 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
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
../../src/UBusTimer.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,61 @@
/*!
* UbusTimer.h
*
* Copyright (c) 2016, 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: 17/05/2016
*
*/
#ifndef _UBUS_TIMER_H
#define _UBUS_TIMER_H
/*------------------------------- INCLUDES ----------------------------------*/
#include <stdint.h>
extern "C" {
#include <libubox/uloop.h>
}
/*----------------------------- Dependencies --------------------------------*/
/*--------------------------------- CLASS ----------------------------------*/
class UBusTimer : public uloop_timeout {
// Disable copy construction and copy assignement
UBusTimer (const UBusTimer&);
UBusTimer &operator=(const UBusTimer&);
public:
UBusTimer (void);
virtual ~UBusTimer (void);
virtual int Expire (void);
int Start (unsigned int aTimerValue, bool aRepeat = false);
int Tick (void);
private:
bool mRepeat;
uint16_t mTimerValue;
};
#endif /* _UBUS_TIMER_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);
}

View File

@@ -0,0 +1,113 @@
/*!
* UbusTimer.cpp
*
* Copyright (c) 2016, 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: 17/05/2016
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include <cstdio>
#include "ubuscpp/UBusTimer.h"
/*! ----------------------------------------------------------------------------
* @fn timer_callback
*
* @brief Callback used when a Timer has expired.
*/
static void timer_callback (struct uloop_timeout *aTimeout)
{
UBusTimer *aTmp = static_cast<UBusTimer *>(aTimeout);
aTmp->Tick();
}
/*! ----------------------------------------------------------------------------
* @fn UBusTimer
*
* @brief Constructor of the Timer UBUS Object.
*/
UBusTimer::UBusTimer (void)
{
// Uloop Timer Init.
cb = timer_callback;
pending = 0;
// UBus Timer.
mRepeat = false;
mTimerValue = 0;
}
/*! ----------------------------------------------------------------------------
* @fn ~UBusTimer
*
* @brief Destructor of the Timer UBUS Object.
*/
UBusTimer::~UBusTimer (void)
{
}
/*! ----------------------------------------------------------------------------
* @fn Start the Timer
*
* @brief Start the Timer object.
*/
int UBusTimer::Start (unsigned int aTimerValue, bool aRepeat)
{
mTimerValue = aTimerValue;
mRepeat = aRepeat;
return uloop_timeout_set (this, mTimerValue);
}
/*! ----------------------------------------------------------------------------
* @fn Expire
*
* @brief Method Should be overrided.
*/
int UBusTimer::Expire (void)
{
//fprintf (stderr, "UBusTimer::Expire\n");
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn Tick
*
* @brief Method called when the Timer has expired.
*/
int UBusTimer::Tick (void)
{
//fprintf (stderr, "UBusTimer::Tick\n");
Expire ();
if (mRepeat == true)
uloop_timeout_set (this, mTimerValue);
return 0;
}

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

View File

@@ -0,0 +1,30 @@
cmake_minimum_required(VERSION 2.8.11)
project(chacond)
include (libubus)
include (libubox)
include (wiringPi)
include_directories(../../src)
file(
GLOB_RECURSE
source_files
../../src/main.c
../../src/UBusModel.c
../../src/Chacon.c
)
add_executable (
chacond
${source_files}
)
target_link_libraries (chacond
LINK_PUBLIC
ubus
wiringPi
rt
pthread
)

Binary file not shown.

BIN
src/prog/chacond/radioEmission Executable file

Binary file not shown.

View File

@@ -0,0 +1,231 @@
#include <wiringPi.h>
#include <iostream>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <sched.h>
#include <sstream>
#include <unistd.h>
/*
Par Idleman (idleman@idleman.fr - http://blog.idleman.fr)
Licence : CC by sa
Toutes question sur le blog ou par mail, possibilité de m'envoyer des bières via le blog
*/
using namespace std;
int pin;
bool bit2[26]={}; // 26 bit Identifiant emetteur
bool bit2Interruptor[4]={};
int interruptor;
int sender;
string onoff;
void log(string a){
//Décommenter pour avoir les logs
cout << a << endl;
}
void scheduler_realtime() {
struct sched_param p;
p.__sched_priority = sched_get_priority_max(SCHED_RR);
if( sched_setscheduler( 0, SCHED_RR, &p ) == -1 ) {
perror("Failed to switch to realtime scheduler.");
}
}
void scheduler_standard() {
struct sched_param p;
p.__sched_priority = 0;
if( sched_setscheduler( 0, SCHED_OTHER, &p ) == -1 ) {
perror("Failed to switch to normal scheduler.");
}
}
//Envois d'une pulsation (passage de l'etat haut a l'etat bas)
//1 = 310µs haut puis 1340µs bas
//0 = 310µs haut puis 310µs bas
void sendBit(bool b) {
if (b) {
digitalWrite(pin, HIGH);
delayMicroseconds(310); //275 orinally, but tweaked.
digitalWrite(pin, LOW);
delayMicroseconds(1340); //1225 orinally, but tweaked.
}
else {
digitalWrite(pin, HIGH);
delayMicroseconds(310); //275 orinally, but tweaked.
digitalWrite(pin, LOW);
delayMicroseconds(310); //275 orinally, but tweaked.
}
}
//Calcul le nombre 2^chiffre indiqué, fonction utilisé par itob pour la conversion decimal/binaire
unsigned long power2(int power){
unsigned long integer=1;
for (int i=0; i<power; i++){
integer*=2;
}
return integer;
}
//Convertis un nombre en binaire, nécessite le nombre, et le nombre de bits souhaité en sortie (ici 26)
// Stocke le résultat dans le tableau global "bit2"
void itob(unsigned long integer, int length)
{
for (int i=0; i<length; i++){
if ((integer / power2(length-1-i))==1){
integer-=power2(length-1-i);
bit2[i]=1;
}
else bit2[i]=0;
}
}
void itobInterruptor(unsigned long integer, int length)
{
for (int i=0; i<length; i++){
if ((integer / power2(length-1-i))==1){
integer-=power2(length-1-i);
bit2Interruptor[i]=1;
}
else bit2Interruptor[i]=0;
}
}
//Envoie d'une paire de pulsation radio qui definissent 1 bit réel : 0 =01 et 1 =10
//c'est le codage de manchester qui necessite ce petit bouzin, ceci permet entre autres de dissocier les données des parasites
void sendPair(bool b) {
if(b)
{
sendBit(true);
sendBit(false);
}
else
{
sendBit(false);
sendBit(true);
}
}
//Fonction d'envois du signal
//recoit en parametre un booleen définissant l'arret ou la marche du matos (true = on, false = off)
void transmit(int blnOn)
{
int i;
// Sequence de verrou anoncant le départ du signal au recepeteur
digitalWrite(pin, HIGH);
delayMicroseconds(275); // un bit de bruit avant de commencer pour remettre les delais du recepteur a 0
digitalWrite(pin, LOW);
delayMicroseconds(9900); // premier verrou de 9900µs
digitalWrite(pin, HIGH); // high again
delayMicroseconds(275); // attente de 275µs entre les deux verrous
digitalWrite(pin, LOW); // second verrou de 2675µs
delayMicroseconds(2675);
digitalWrite(pin, HIGH); // On reviens en état haut pour bien couper les verrous des données
// Envoie du code emetteur (272946 = 1000010101000110010 en binaire)
for(i=0; i<26;i++)
{
sendPair(bit2[i]);
}
// Envoie du bit définissant si c'est une commande de groupe ou non (26em bit)
sendPair(false);
// Envoie du bit définissant si c'est allumé ou eteint 27em bit)
sendPair(blnOn);
// Envoie des 4 derniers bits, qui représentent le code interrupteur, ici 0 (encode sur 4 bit donc 0000)
// nb: sur les télécommandes officielle chacon, les interrupteurs sont logiquement nommés de 0 à x
// interrupteur 1 = 0 (donc 0000) , interrupteur 2 = 1 (1000) , interrupteur 3 = 2 (0100) etc...
for(i=0; i<4;i++)
{
if(bit2Interruptor[i]==0){
sendPair(false);
}else{
sendPair(true);
}
}
digitalWrite(pin, HIGH); // coupure données, verrou
delayMicroseconds(275); // attendre 275µs
digitalWrite(pin, LOW); // verrou 2 de 2675µs pour signaler la fermeture du signal
}
int main (int argc, char** argv)
{
if (setuid(0))
{
perror("setuid");
return 1;
}
scheduler_realtime();
log("Demarrage du programme");
pin = atoi(argv[1]);
sender = atoi(argv[2]);
interruptor = atoi(argv[3]);
onoff = argv[4];
//Si on ne trouve pas la librairie wiringPI, on arrête l'execution
if(wiringPiSetup() == -1)
{
log("Librairie Wiring PI introuvable, veuillez lier cette librairie...");
return -1;
}
pinMode(pin, OUTPUT);
log("Pin GPIO configure en sortie");
itob(sender,26); // convertion du code de l'emetteur (ici 8217034) en code binaire
itobInterruptor(interruptor,4);
if(onoff=="on"){
system("/etc/lcd/screen -p \"Radio signal ON...\"");
log("envois du signal ON");
for(int i=0;i<5;i++){
transmit(true); // envoyer ON
delay(10); // attendre 10 ms (sinon le socket nous ignore)
}
}else{
system("/etc/lcd/screen -p \"Radio signal OFF...\"");
log("envois du signal OFF");
for(int i=0;i<5;i++){
transmit(false); // envoyer OFF
delay(10); // attendre 10 ms (sinon le socket nous ignore)
}
}
log("fin du programme"); // execution terminée.
scheduler_standard();
}

View File

@@ -0,0 +1,187 @@
/*
* Code pour reception d'un signal de sonde de temperature "maison", récupère une temperature et l'envois sur un fichier php passé en paramêtre
* Fréquence : 433.92 mhz
* Protocole : Ridle (Radio Idle, protocole customisé basé sur home easy)
* Licence : CC -by -sa
* Matériel associé : récepteur RF AM 433.92 mhz
* Auteur : Valentin CARRUESCO aka idleman (idleman@idleman.fr - http://blog.idleman.fr)
*/
#include <wiringPi.h>
#include <iostream>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <sched.h>
#include <sstream>
using namespace std;
int pin = 7;
void log(string a){
//Décommenter pour avoir les logs
cout << a << endl;
}
void scheduler_realtime() {
struct sched_param p;
p.__sched_priority = sched_get_priority_max(SCHED_RR);
if( sched_setscheduler( 0, SCHED_RR, &p ) == -1 ) {
perror("Failed to switch to realtime scheduler.");
}
}
void scheduler_standard() {
struct sched_param p;
p.__sched_priority = 0;
if( sched_setscheduler( 0, SCHED_OTHER, &p ) == -1 ) {
perror("Failed to switch to normal scheduler.");
}
}
string longToString(long mylong){
string mystring;
stringstream mystream;
mystream << mylong;
return mystream.str();
}
int pulseIn(int pin, int level, int timeout)
{
struct timeval tn, t0, t1;
long micros;
gettimeofday(&t0, NULL);
micros = 0;
while (digitalRead(pin) != level)
{
gettimeofday(&tn, NULL);
if (tn.tv_sec > t0.tv_sec) micros = 1000000L; else micros = 0;
micros += (tn.tv_usec - t0.tv_usec);
if (micros > timeout) return 0;
}
gettimeofday(&t1, NULL);
while (digitalRead(pin) == level)
{
gettimeofday(&tn, NULL);
if (tn.tv_sec > t0.tv_sec) micros = 1000000L; else micros = 0;
micros = micros + (tn.tv_usec - t0.tv_usec);
if (micros > timeout) return 0;
}
if (tn.tv_sec > t1.tv_sec) micros = 1000000L; else micros = 0;
micros = micros + (tn.tv_usec - t1.tv_usec);
return micros;
}
int main (int argc, char** argv)
{
//scheduler_realtime();
string command;
string path = "php ";
path.append(argv[1]);
log("Demarrage du programme");
pin = atoi(argv[2]);
if(wiringPiSetup() == -1)
{
log("Librairie Wiring PI introuvable, veuillez lier cette librairie...");
return -1;
}
pinMode(pin, INPUT);
log("Pin GPIO configure en entree");
log("Attente d'un signal du transmetteur ...");
for(;;)
{
int i = 0;
unsigned long t = 0;
int prevBit = 0;
int bit = 0;
unsigned long temperature = 0;
unsigned long emiter = 0;
unsigned long positive = 0;
bool group=false;
bool on =false;
unsigned long recipient = 0;
command = path+" ";
t = pulseIn(pin, LOW, 1000000);
while((t < 2550 || t > 2900)){
t = pulseIn(pin, LOW,1000000);
}
while(i < 24)
{
t = pulseIn(pin, LOW, 1000000);
if(t > 500 && t < 1500)
{
bit = 0;
}
else if(t > 2000 && t < 3000)
{
bit = 1;
}
else
{
i = 0;
cout << "FAIL? = " << t << endl;
break;
}
if(i % 2 == 1)
{
if((prevBit ^ bit) == 0)
{
i = 0;
break;
}
if(i < 15)
{
temperature <<= 1;
temperature |= prevBit;
}else if(i == 15){
positive = prevBit;
}else{
emiter <<= 1;
emiter |= prevBit;
}
}
prevBit = bit;
++i;
}
if(i>0){
log("------------------------------");
log("Donnees detectees");
cout << "temperature = " << temperature << " C" << endl;
cout << "positif = " << positive << endl;
cout << "code sonde = " << emiter << endl;
command.append("UPDATE_ENGINE_STATE ");
command.append(" "+longToString(emiter));
command.append(" "+longToString(temperature));
command.append(" "+longToString(positive));
log("Execution de la commande PHP...");
log(command.c_str());
system(command.c_str());
}else{
log("Aucune donnee...");
}
delay(3000);
}
//scheduler_standard();
}

View File

@@ -0,0 +1,309 @@
/*!
* chacon.c
*
* 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: 20/03/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include <stdio.h>
#include <sched.h>
#include <wiringPi.h>
#include "chacon.h"
/*-------------------------------- GLOBALES ---------------------------------*/
static int gGpioPin = 0;
bool gBit2[26]={}; // 26 bit Identifiant emetteur
bool gBit2Interruptor[4]={};
void sendBit (bool aBit);
unsigned long power2 (int aPower);
void itob (unsigned long anInteger, int aLength);
void itobInterruptor (unsigned long anInteger, int aLength);
void sendPair (bool aBit);
void transmit (bool aState);
/*! ----------------------------------------------------------------------------
* @fn SetupChacon
*
* @brief this method setup the Chacon environment.
*/
int SetupChacon (int aGpioPin)
{
gGpioPin = aGpioPin;
if (wiringPiSetup() == -1) {
fprintf (stderr, "Impossible to setup the WiringPI...");
return -1;
}
pinMode (gGpioPin, OUTPUT);
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn Chacon_SetInterruptorState
*
* @brief This function Change the State of the Interruptor.
*/
int Chacon_SetInterruptorState (uint32_t aSender, uint32_t anInterruptor, bool aState)
{
int i;
fprintf (stdout, "Chacon_SetInterruptorState - aSender:%d, anInterruptor:%d, aState:%d\n",
aSender, anInterruptor, aState);
/* convert sender and interruptor ID */
itob (aSender, 26); // convertion du code de l'emetteur (ici 8217034) en code binaire
itobInterruptor (anInterruptor, 4);
/* Send the State Five Time. */
for (i = 0; i < 10; i++) {
transmit (aState);
delay (10); // attendre 10 ms (sinon le socket nous ignore)
}
return 0;
}
/*-------------------------------- LOCALES ---------------------------------*/
/*! ----------------------------------------------------------------------------
* @fn scheduler_realtime
*
* @brief Switch the process to the RealTime Scheduler
*/
void scheduler_realtime (void)
{
struct sched_param theSchedParam;
theSchedParam.__sched_priority = sched_get_priority_max (SCHED_RR);
if( sched_setscheduler (0, SCHED_RR, &theSchedParam) == -1 ) {
perror ("Failed to switch to realtime scheduler.");
}
}
/*! ----------------------------------------------------------------------------
* @fn scheduler_standard
*
* @brief Switch the process to the regular Scheduler
*/
void scheduler_standard (void)
{
struct sched_param theSchedParam;
theSchedParam.__sched_priority = 0;
if( sched_setscheduler (0, SCHED_OTHER, &theSchedParam) == -1 ) {
perror ("Failed to switch to normal scheduler.");
}
}
/*! ----------------------------------------------------------------------------
* @fn sendBit
*
* @brief Envois d'une pulsation (passage de l'etat haut a l'etat bas)
* 1 = 310µs haut puis 1340µs bas
* 0 = 310µs haut puis 310µs bas
*/
void sendBit (bool aBit)
{
if (aBit) {
digitalWrite (gGpioPin, HIGH);
delayMicroseconds (310); //275 orinally, but tweaked.
digitalWrite (gGpioPin, LOW);
delayMicroseconds (1340); //1225 orinally, but tweaked.
}
else {
digitalWrite (gGpioPin, HIGH);
delayMicroseconds (310); //275 orinally, but tweaked.
digitalWrite (gGpioPin, LOW);
delayMicroseconds (310); //275 orinally, but tweaked.
}
}
/*! ----------------------------------------------------------------------------
* @fn power2
*
* @brief Calcul le nombre 2^chiffre indiqué, fonction utilisé par itob pour
* la conversion decimal/binaire
*/
unsigned long power2 (int aPower)
{
int i;
unsigned long theInteger = 1;
for (i = 0; i < aPower; i++) {
theInteger *= 2;
}
return theInteger;
}
/*! ----------------------------------------------------------------------------
* @fn itob
*
* @brief Convertis un nombre en binaire, nécessite le nombre, et le nombre de
* bits souhaité en sortie (ici 26)
* Stocke le résultat dans le tableau global "bit2"
*/
void itob (unsigned long anInteger, int aLength)
{
int i;
for (i = 0; i < aLength; i++) {
if ((anInteger / power2 (aLength - 1 - i)) == 1) {
anInteger -= power2 (aLength - 1 - i);
gBit2[i] = 1;
}
else {
gBit2[i]=0;
}
}
}
/*! ----------------------------------------------------------------------------
* @fn itobInterruptor
*
* @brief TODO
*/
void itobInterruptor (unsigned long anInteger, int aLength)
{
int i;
for (i = 0; i < aLength; i++) {
if ((anInteger / power2 (aLength - 1 - i)) == 1) {
anInteger -= power2 (aLength - 1 - i);
gBit2Interruptor[i] = 1;
}
else {
gBit2Interruptor[i] = 0;
}
}
}
/*! ----------------------------------------------------------------------------
* @fn sendPair
*
* @brief Envoie d'une paire de pulsation radio qui definissent 1 bit réel :
* 0 =01 et 1 =10
* c'est le codage de manchester qui necessite ce petit bouzin, ceci permet
* entre autres de dissocier les données des parasites
*/
void sendPair (bool aBit)
{
if (aBit) {
sendBit (true);
sendBit (false);
}
else {
sendBit (false);
sendBit (true);
}
}
/*! ----------------------------------------------------------------------------
* @fn transmit
*
* @brief Fonction d'envois du signal
* recoit en parametre un booleen définissant l'arret ou la marche du matos
* (true = on, false = off)
*/
void transmit (bool aState)
{
int i;
// Sequence de verrou anoncant le départ du signal au recepeteur
digitalWrite (gGpioPin, HIGH);
delayMicroseconds (275); // un bit de bruit avant de commencer pour remettre les delais du recepteur a 0
digitalWrite (gGpioPin, LOW);
delayMicroseconds (9900); // premier verrou de 9900µs
digitalWrite (gGpioPin, HIGH); // high again
delayMicroseconds (275); // attente de 275µs entre les deux verrous
digitalWrite (gGpioPin, LOW); // second verrou de 2675µs
delayMicroseconds (2675);
digitalWrite (gGpioPin, HIGH); // On reviens en état haut pour bien couper les verrous des données
// Envoie du code emetteur (272946 = 1000010101000110010 en binaire)
for (i = 0; i < 26; i++) {
sendPair (gBit2[i]);
}
// Envoie du bit définissant si c'est une commande de groupe ou non (26em bit)
sendPair (false);
// Envoie du bit définissant si c'est allumé ou eteint 27em bit)
sendPair (aState);
// Envoie des 4 derniers bits, qui représentent le code interrupteur, ici 0 (encode sur 4 bit donc 0000)
// nb: sur les télécommandes officielle chacon, les interrupteurs sont logiquement nommés de 0 à x
// interrupteur 1 = 0 (donc 0000) , interrupteur 2 = 1 (1000) , interrupteur 3 = 2 (0100) etc...
for (i = 0; i < 4; i++) {
if (gBit2Interruptor[i] == 0) {
sendPair (false);
} else {
sendPair (true);
}
}
digitalWrite (gGpioPin, HIGH); // coupure données, verrou
delayMicroseconds(275); // attendre 275µs
digitalWrite (gGpioPin, LOW); // verrou 2 de 2675µs pour signaler la fermeture du signal
}

View File

@@ -0,0 +1,167 @@
/*!
* UBusModel.c
*
* 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: 26/02/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include <unistd.h>
#include <signal.h>
#include <libubox/blobmsg_json.h>
#include <libubus.h>
/*------------------------------- DECLARATION -------------------------------*/
static int gChacon_set (struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
/*--------------------------------- GLOBALS ---------------------------------*/
static struct ubus_context *gCtx;
static struct blob_buf gBuffer;
enum {
SET_SENDER,
SET_INTERRUPTOR,
SET_STATE,
__CHACON_MAX
};
static const struct blobmsg_policy set_chacon_policy[] = {
[SET_SENDER] = { .name = "sender", .type = BLOBMSG_TYPE_INT32 },
[SET_INTERRUPTOR] = { .name = "interruptor", .type = BLOBMSG_TYPE_INT32 },
[SET_STATE] = { .name = "state", .type = BLOBMSG_TYPE_BOOL },
};
static const struct ubus_method gChacon_methods[] = {
UBUS_METHOD("set", gChacon_set, set_chacon_policy),
};
static struct ubus_object_type gChacon_object_type =
UBUS_OBJECT_TYPE ("chacon", gChacon_methods);
static struct ubus_object gChacon_object = {
.name = "chacon",
.type = &gChacon_object_type,
.methods = gChacon_methods,
.n_methods = ARRAY_SIZE (gChacon_methods),
};
/*! ----------------------------------------------------------------------------
* @fn gChacon_set
*
* @brief Method to send a chacon DIO command to a specific device.
*/
static int gChacon_set (struct ubus_context *aCtx, struct ubus_object *anObj,
struct ubus_request_data *aReq, const char *aMethod,
struct blob_attr *aMsg)
{
struct blob_attr *theParams[__CHACON_MAX];
uint32_t theSender;
uint32_t theInterruptor;
bool theState;
/* Parse the Message */
blobmsg_parse (set_chacon_policy, __CHACON_MAX, theParams, blob_data (aMsg), blob_len(aMsg));
/* Sanity Checks. */
if (!theParams[SET_SENDER] || !theParams[SET_INTERRUPTOR]|| !theParams[SET_STATE]) {
printf ("Chacon Set Invalid arguments.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
/* Get Parameters. */
theSender = blobmsg_get_u32 (theParams[SET_SENDER]);
theInterruptor = blobmsg_get_u32 (theParams[SET_INTERRUPTOR]);
theState = blobmsg_get_bool (theParams[SET_STATE]);
return Chacon_SetInterruptorState (theSender, theInterruptor, theState);
}
/*! ----------------------------------------------------------------------------
* @fn SetupUbus
*
* @brief Setup the UBus Mechanism.
*/
int SetupUbus (void)
{
uloop_init ();
signal (SIGPIPE, SIG_IGN);
gCtx = ubus_connect (NULL);
if (!gCtx) {
fprintf (stderr, "Failed to connect to ubus\n");
return -1;
}
ubus_add_uloop (gCtx);
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn UBusMain
*
* @brief Main function of the UBus Loop.
*/
int UBusMain (void)
{
int theRet;
theRet = ubus_add_object (gCtx, &gChacon_object);
if (theRet) {
fprintf (stderr, "Failed to add object: %s\n", ubus_strerror (theRet));
}
uloop_run ();
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn TerminateUBus
*
* @brief This function terminate the Ubus mechanisme.
*/
int TerminateUBus (void)
{
ubus_free (gCtx);
uloop_done ();
return 0;
}

View File

@@ -0,0 +1,35 @@
/*!
* UBusModel.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: 26/02/2015
*
*/
#ifndef __UBUSMODEL_H
#define __UBUSMODEL_H
int SetupUbus (void);
int TerminateUBus (void);
int UBusMain (void);
#endif /* __UBUSMODEL_H */

View File

@@ -0,0 +1,43 @@
/*!
* chacon.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: 20/03/2015
*
*/
#ifndef __CHACON_H
#define __CHACON_H
/*-------------------------------- INCLUDES ---------------------------------*/
#include <stdint.h>
#include <stdbool.h>
/*-------------------------------- METHODS ---------------------------------*/
int SetupChacon (int aGpioPin);
int Chacon_SetInterruptorState (uint32_t aSender, uint32_t anInterruptor, bool aState);
void scheduler_realtime (void);
void scheduler_standard (void);
#endif /* __CHACON_H */

View File

@@ -0,0 +1,83 @@
/*!
* main.c
*
* 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: 09/12/2014
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include <stdio.h>
#include <unistd.h>
#include "UbusModel.h"
#include "chacon.h"
/*! ----------------------------------------------------------------------------
* @fn main
*
* @brief Main function of Chacon Daemon.
*/
int main (int argc, char **argv)
{
int thePin;
int theRet = 0;
if (argc != 2) {
fprintf (stderr, "You should specify a WiringPI Pin ID\n");
return -1;
}
thePin = atoi (argv[1]);
if (setuid (0)) {
perror ("setuid");
return 1;
}
scheduler_realtime();
/* Setup UBus. */
theRet = SetupUbus ();
if (theRet != 0) {
fprintf (stderr, "Impossible to Setup the UBUS Environment.\n");
return theRet;
}
/* Setup Chacon. */
theRet = SetupChacon (thePin);
if (theRet != 0) {
fprintf (stderr, "Impossible to Setup the Chacon Environment.\n");
return theRet;
}
/* Main loop */
UBusMain ();
scheduler_standard();
return TerminateUBus();
}

187
src/prog/chacond/wiringPi.h Normal file
View File

@@ -0,0 +1,187 @@
/*
* wiringPi:
* Arduino compatable (ish) Wiring library for the Raspberry Pi
* Copyright (c) 2012 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi 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 3 of the License, or
* (at your option) any later version.
*
* wiringPi 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 wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#ifndef __WIRING_PI_H__
#define __WIRING_PI_H__
// Handy defines
// Deprecated
#define NUM_PINS 17
#define WPI_MODE_PINS 0
#define WPI_MODE_GPIO 1
#define WPI_MODE_GPIO_SYS 2
#define WPI_MODE_PHYS 3
#define WPI_MODE_PIFACE 4
#define WPI_MODE_UNINITIALISED -1
// Pin modes
#define INPUT 0
#define OUTPUT 1
#define PWM_OUTPUT 2
#define GPIO_CLOCK 3
#define LOW 0
#define HIGH 1
// Pull up/down/none
#define PUD_OFF 0
#define PUD_DOWN 1
#define PUD_UP 2
// PWM
#define PWM_MODE_MS 0
#define PWM_MODE_BAL 1
// Interrupt levels
#define INT_EDGE_SETUP 0
#define INT_EDGE_FALLING 1
#define INT_EDGE_RISING 2
#define INT_EDGE_BOTH 3
// Threads
#define PI_THREAD(X) void *X (void *dummy)
// Failure modes
#define WPI_FATAL (1==1)
#define WPI_ALMOST (1==2)
// wiringPiNodeStruct:
// This describes additional device nodes in the extended wiringPi
// 2.0 scheme of things.
// It's a simple linked list for now, but will hopefully migrate to
// a binary tree for efficiency reasons - but then again, the chances
// of more than 1 or 2 devices being added are fairly slim, so who
// knows....
struct wiringPiNodeStruct
{
int pinBase ;
int pinMax ;
int fd ; // Node specific
unsigned int data0 ; // ditto
unsigned int data1 ; // ditto
unsigned int data2 ; // ditto
unsigned int data3 ; // ditto
void (*pinMode) (struct wiringPiNodeStruct *node, int pin, int mode) ;
void (*pullUpDnControl) (struct wiringPiNodeStruct *node, int pin, int mode) ;
int (*digitalRead) (struct wiringPiNodeStruct *node, int pin) ;
void (*digitalWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
void (*pwmWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
int (*analogRead) (struct wiringPiNodeStruct *node, int pin) ;
void (*analogWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
struct wiringPiNodeStruct *next ;
} ;
extern struct wiringPiNodeStruct *wiringPiNodes ;
// Function prototypes
// c++ wrappers thanks to a comment by Nick Lott
// (and others on the Raspberry Pi forums)
#ifdef __cplusplus
extern "C" {
#endif
// Internal
extern int wiringPiFailure (int fatal, const char *message, ...) ;
// Core wiringPi functions
extern struct wiringPiNodeStruct *wiringPiFindNode (int pin) ;
extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins) ;
extern int wiringPiSetup (void) ;
extern int wiringPiSetupSys (void) ;
extern int wiringPiSetupGpio (void) ;
extern int wiringPiSetupPhys (void) ;
extern void pinModeAlt (int pin, int mode) ;
extern void pinMode (int pin, int mode) ;
extern void pullUpDnControl (int pin, int pud) ;
extern int digitalRead (int pin) ;
extern void digitalWrite (int pin, int value) ;
extern void pwmWrite (int pin, int value) ;
extern int analogRead (int pin) ;
extern void analogWrite (int pin, int value) ;
// PiFace specifics
// (Deprecated)
extern int wiringPiSetupPiFace (void) ;
extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio program only
// On-Board Raspberry Pi hardware specific stuff
extern int piBoardRev (void) ;
extern int wpiPinToGpio (int wpiPin) ;
extern int physPinToGpio (int physPin) ;
extern void setPadDrive (int group, int value) ;
extern int getAlt (int pin) ;
extern void digitalWriteByte (int value) ;
extern void pwmSetMode (int mode) ;
extern void pwmSetRange (unsigned int range) ;
extern void pwmSetClock (int divisor) ;
extern void gpioClockSet (int pin, int freq) ;
// Interrupts
// (Also Pi hardware specific)
extern int waitForInterrupt (int pin, int mS) ;
extern int wiringPiISR (int pin, int mode, void (*function)(void)) ;
// Threads
extern int piThreadCreate (void *(*fn)(void *)) ;
extern void piLock (int key) ;
extern void piUnlock (int key) ;
// Schedulling priority
extern int piHiPri (const int pri) ;
// Extras from arduino land
extern void delay (unsigned int howLong) ;
extern void delayMicroseconds (unsigned int howLong) ;
extern unsigned int millis (void) ;
extern unsigned int micros (void) ;
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,20 @@
exemple de commande du opensprinker PI
https://github.com/rayshobby/opensprinkler
Bonne appli on peut s'en inspirer
https://opensprinkler.com/forums/topic/sprinklers_pi-an-alternative-sprinkler-control-program/
https://github.com/rszimm/sprinklers_pi/
## Command used to activate or not a Sprinkler.
>
>./bin/ucli call sprinkers set "{ \"station\": 7,\"state\": true}"
>
>./bin/ucli call sprinkers set "{ \"station\": 7,\"state\": false}"
>

View File

@@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 2.8.11)
project(sprinklersd)
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/UbusSprinklerModel.cpp
../../src/sprinklers.cpp
)
add_executable (
sprinklersd
${source_files}
)
target_link_libraries (sprinklersd
LINK_PUBLIC
jsoncpp
ubuscpp
rt
)

View File

@@ -0,0 +1,172 @@
/*!
* UBusSprinkerModel.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: 30/04/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include <fstream>
#include <string>
#include <sstream>
#include <jsoncpp/json.h>
extern "C" {
#include <libubox/blobmsg_json.h>
}
#include "sprinklers.h"
#include "UbusSprinklerModel.h"
namespace {
using namespace UBus;
static ObjectType gSprinklerModelUbus_types(
"sprinklers",
Method("get", UBUS_CPP(SprinkerModel, Get)),
Method("set", UBUS_CPP(SprinkerModel, Set))
);
}
/*! ----------------------------------------------------------------------------
* @fn SprinkerModel
*
* @brief Constructor of the UBus Mixer Volume.
*/
SprinkerModel::SprinkerModel (void) :
UBusObject (gSprinklerModelUbus_types, "sprinklers")
{
}
/*! ----------------------------------------------------------------------------
* @fn ~SprinkerModel
*
* @brief Destructor of the UBus Mixer Volume.
*/
SprinkerModel::~SprinkerModel (void)
{
}
/*! ----------------------------------------------------------------------------
* @fn Get
*
* @brief Get the State of a Sprinkler Station.
*/
int SprinkerModel::Get (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::StyledWriter theWriter;
Json::Value theRoot;
Json::Value theOutput;
uint32_t theStation = 0;
bool theState = false;
if (!theReader.parse (theString, theRoot)) {
fprintf (stderr, "Failed parse the parameters.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
theStation = theRoot["station"].asInt();
if (theStation == 0) {
fprintf (stderr, "Station ID is missing.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
theResult = sprinklers_get_station (theStation, theState);
if (theResult != 0) {
fprintf (stderr, "Wrong Station ID (%d).\n", theStation);
return UBUS_STATUS_INVALID_ARGUMENT;
}
printf ("aState: %d\n", theState);
theOutput ["station"] = theStation;
theOutput ["state"] = theState;
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 Set
*
* @brief Set a new State for a Sprinkler Station.
*/
int SprinkerModel::Set (struct ubus_context *aCtx, struct ubus_request_data *aReq,
struct blob_attr *aMsg)
{
int theResult;
char *theString = blobmsg_format_json (aMsg, true);
Json::Reader theReader;
Json::Value theRoot;
uint32_t theStation = 0;
bool theState = false;
if (!theReader.parse (theString, theRoot)) {
fprintf (stderr, "Failed parse the parameters.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
theStation = theRoot["station"].asInt();
theState = theRoot["state"].asBool();
if (theStation == 0) {
fprintf (stderr, "Station ID is missing.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
theResult = sprinklers_set_station (theStation, theState);
if (theResult != 0) {
fprintf (stderr, "Wrong Station ID (%d).\n", theStation);
return UBUS_STATUS_INVALID_ARGUMENT;
}
return theResult;
}

View File

@@ -0,0 +1,49 @@
/*!
* UBusModel.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: 30/04/2015
*
*/
#ifndef _UBUS_SPRINKLER_MODEL_H
#define _UBUS_SPRINKLER_MODEL_H
/*------------------------------- INCLUDES ----------------------------------*/
#include <stdint.h>
#include <ubuscpp/UBusObject.h>
/*--------------------------------- Define ----------------------------------*/
/*--------------------------------- CLASS ----------------------------------*/
class SprinkerModel : public UBusObject {
public:
SprinkerModel (void);
~SprinkerModel (void);
int Get (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Set (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
};
#endif /* _UBUS_SPRINKLER_MODEL_H */

View File

@@ -0,0 +1,120 @@
/*!
* main.c
*
* 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: 30/04/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include <cstdio>
#include <unistd.h>
extern "C" {
#include <libubus.h>
}
#include "UbusSprinklerModel.h"
#include "sprinklers.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;
SprinkerModel theSprinklersModel;
struct ubus_context *theCtx = NULL;
if (setuid (0)) {
perror ("setuid");
return 1;
}
/* Setup the Ubus context. */
theCtx = setupUbus ();
if (theCtx == NULL) {
return -1;
}
/* Setup the sprinklers. */
theRet = sprinklers_setup ();
if (theRet != 0) {
fprintf (stderr, "Impossible to Setup the Sprinkers.\n");
return theRet;
}
/* Add the UBus to the model exposed. */
ubus_add_object (theCtx, &theSprinklersModel);
/* Main Event Loop. */
uloop_run ();
ubus_free (theCtx);
uloop_done ();
return theRet;
}

View File

@@ -0,0 +1,180 @@
/*!
* sprinklers.c
*
* 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: 30/04/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <jsoncpp/json.h>
#include <string>
#include <ubuscpp/UBusCall.h>
#include "sprinklers.h"
/*-------------------------------- GLOBALES ---------------------------------*/
/*! ----------------------------------------------------------------------------
* @fn sprinklers_setup
*
* @brief this function setup the OSPI environement.
*/
int sprinklers_setup (void)
{
UBusCall theCmd;
std::string theResult;
int theIDVal;
fprintf (stderr, "Setup the Gpios used by the Sprinklers.\n");
if (theCmd.Exec ("domo.capabilities.sprinklers", "get", "", theResult) == 0){
// We get the the sprinklers. Get the IDs.
Json::Reader theReader;
Json::Value theRoot;
Json::Value theElement;
if (!theReader.parse (theResult, theRoot)) {
fprintf (stderr, "Failed parse the List of Sprinklers.\n");
return -1;
}
for (const Json::Value& theElement : theRoot["Sprinklers"]) {
theIDVal = theElement["id"].asInt();
sprinkler_setup_station (theIDVal);
}
}
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn sprinkler_setup_station
*
* @brief This function change the State of an Ospi Station.
*/
int sprinkler_setup_station (int aStation)
{
char theCommand[255];
printf (" Setup the Station: %d\n", aStation);
// Activate the Gpio
sprintf (theCommand, "echo %d > /sys/class/gpio/export", aStation);
system (theCommand);
// Select the Direction of the Gpio
sprintf (theCommand, "echo out > /sys/class/gpio/gpio%d/direction", aStation);
system (theCommand);
// Select the Direction of the Gpio
sprintf (theCommand, "echo 1 > /sys/class/gpio/gpio%d/value", aStation);
system (theCommand);
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn sprinkler_set_station
*
* @brief This function change the State of an Ospi Station.
*/
int sprinklers_set_station (uint32_t aStation, bool aState)
{
FILE *theFp;
char theGpioPath[255];
sprintf (theGpioPath, "/sys/class/gpio/gpio%d/value", aStation);
theFp = fopen (theGpioPath, "w");
if (theFp == NULL) {
fprintf (stderr, "Impossible to open: <%s>\n", theGpioPath);
return -1;
}
if (aState) {
fwrite ("0", 1, 1, theFp);
}
else {
fwrite ("1", 1, 1, theFp);
}
fclose (theFp);
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn sprinkler_get_station
*
* @brief This function change the State of an Ospi Station.
*/
int sprinklers_get_station (uint32_t aStation, bool &aState)
{
FILE *theFp;
char theGpioPath[255];
char theData[5];
fprintf (stdout, "get station state (%d)\n", aStation);
sprintf (theGpioPath, "/sys/class/gpio/gpio%d/value", aStation);
theFp = fopen (theGpioPath, "r");
if (theFp == NULL) {
fprintf (stderr, "Impossible to open: <%s>\n", theGpioPath);
return -1;
}
fread (theData, 1, 1, theFp);
//printf ("theData: %s\n", theData);
if (theData[0] == '0') {
aState = true;
}
else {
aState = false;
}
fclose (theFp);
return 0;
}

View File

@@ -0,0 +1,42 @@
/*!
* 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: 30/04/2015
*
*/
#ifndef __SPRINKLERS_H
#define __SPRINKLERS_H
/*-------------------------------- INCLUDES ---------------------------------*/
#include <stdint.h>
/*-------------------------------- METHODS ---------------------------------*/
int sprinklers_setup (void);
int sprinkler_setup_station (int aStation);
int sprinklers_set_station (uint32_t aStation, bool aState);
int sprinklers_get_station (uint32_t aStation, bool &aState);
#endif /* __SPRINKLERS_H */