From 5564ad3b6e3fd67fc51f42e7b00002d73bb382af Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Tue, 1 Mar 2016 22:53:43 +0100 Subject: [PATCH] Import ubuscpp domod --- lib/CMakeLists.txt | 7 + lib/ubuscpp/builders/cmake/CMakeLists.txt | 27 + lib/ubuscpp/include/ubuscpp/UBusCall.h | 52 + lib/ubuscpp/include/ubuscpp/UBusCpp.h | 957 ++++++++++++++++++ lib/ubuscpp/include/ubuscpp/UBusObject.h | 51 + lib/ubuscpp/src/UBusCall.cpp | 133 +++ lib/ubuscpp/src/UBusObject.cpp | 61 ++ lib/ubuscpp/src/common.h | 33 + src/CMakeLists.txt | 13 + src/domod/builders/cmake/CMakeLists.txt | 38 + src/domod/src/helpers/Strings.cpp | 57 ++ src/domod/src/helpers/Strings.h | 34 + src/domod/src/helpers/Tokenizer.cpp | 116 +++ src/domod/src/helpers/Tokenizer.h | 55 + src/domod/src/main.cpp | 131 +++ src/domod/src/models/Devices.cpp | 205 ++++ src/domod/src/models/Devices.h | 56 + src/domod/src/ubus/capabilities.cpp | 96 ++ src/domod/src/ubus/capabilities.h | 50 + src/domod/src/ubus/capabilities_lights.cpp | 191 ++++ src/domod/src/ubus/capabilities_lights.h | 60 ++ src/domod/src/ubus/capabilities_shutters.cpp | 202 ++++ src/domod/src/ubus/capabilities_shutters.h | 64 ++ .../src/ubus/capabilities_sprinklers.cpp | 132 +++ src/domod/src/ubus/capabilities_sprinklers.h | 56 + src/domod/src/ubus/speach_command.cpp | 181 ++++ src/domod/src/ubus/speach_command.h | 57 ++ 27 files changed, 3115 insertions(+) create mode 100644 lib/CMakeLists.txt create mode 100644 lib/ubuscpp/builders/cmake/CMakeLists.txt create mode 100644 lib/ubuscpp/include/ubuscpp/UBusCall.h create mode 100644 lib/ubuscpp/include/ubuscpp/UBusCpp.h create mode 100644 lib/ubuscpp/include/ubuscpp/UBusObject.h create mode 100644 lib/ubuscpp/src/UBusCall.cpp create mode 100644 lib/ubuscpp/src/UBusObject.cpp create mode 100644 lib/ubuscpp/src/common.h create mode 100644 src/CMakeLists.txt create mode 100644 src/domod/builders/cmake/CMakeLists.txt create mode 100644 src/domod/src/helpers/Strings.cpp create mode 100644 src/domod/src/helpers/Strings.h create mode 100644 src/domod/src/helpers/Tokenizer.cpp create mode 100644 src/domod/src/helpers/Tokenizer.h create mode 100644 src/domod/src/main.cpp create mode 100644 src/domod/src/models/Devices.cpp create mode 100644 src/domod/src/models/Devices.h create mode 100644 src/domod/src/ubus/capabilities.cpp create mode 100644 src/domod/src/ubus/capabilities.h create mode 100644 src/domod/src/ubus/capabilities_lights.cpp create mode 100644 src/domod/src/ubus/capabilities_lights.h create mode 100644 src/domod/src/ubus/capabilities_shutters.cpp create mode 100644 src/domod/src/ubus/capabilities_shutters.h create mode 100644 src/domod/src/ubus/capabilities_sprinklers.cpp create mode 100644 src/domod/src/ubus/capabilities_sprinklers.h create mode 100644 src/domod/src/ubus/speach_command.cpp create mode 100644 src/domod/src/ubus/speach_command.h diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt new file mode 100644 index 00000000..459058d0 --- /dev/null +++ b/lib/CMakeLists.txt @@ -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) diff --git a/lib/ubuscpp/builders/cmake/CMakeLists.txt b/lib/ubuscpp/builders/cmake/CMakeLists.txt new file mode 100644 index 00000000..3a5fb333 --- /dev/null +++ b/lib/ubuscpp/builders/cmake/CMakeLists.txt @@ -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}) diff --git a/lib/ubuscpp/include/ubuscpp/UBusCall.h b/lib/ubuscpp/include/ubuscpp/UBusCall.h new file mode 100644 index 00000000..ea9d56a6 --- /dev/null +++ b/lib/ubuscpp/include/ubuscpp/UBusCall.h @@ -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 + +/*---------------------------------- 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 */ diff --git a/lib/ubuscpp/include/ubuscpp/UBusCpp.h b/lib/ubuscpp/include/ubuscpp/UBusCpp.h new file mode 100644 index 00000000..3dc89b78 --- /dev/null +++ b/lib/ubuscpp/include/ubuscpp/UBusCpp.h @@ -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 +} + + +namespace UBus { + +namespace detail { + +/** + * simple POD vehicle for arrays + */ +template +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 +static inline Array makeArray(const T& e0) { + Array result; + result.count = 1; + result.ptr = new T[result.count](); + memcpy(&result.ptr[0], &e0, sizeof(T)); + return result; +} + +template +static inline Array makeArray(const T& e0, const T& e1) { + Array 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 +static inline Array makeArray(const T& e0, const T& e1, const T& e2) { + Array 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 +static inline Array makeArray(const T& e0, const T& e1, const T& e2, + const T& e3) { + Array 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 +static inline Array makeArray(const T& e0, const T& e1, const T& e2, + const T& e3, const T& e4) { + Array 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 +static inline Array makeArray(const T& e0, const T& e1, const T& e2, + const T& e3, const T& e4, const T& e5) { + Array 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 +static inline Array makeArray(const T& e0, const T& e1, const T& e2, + const T& e3, const T& e4, const T& e5, + const T& e6) { + Array 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 +static inline Array 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 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 +static inline Array 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 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 +static inline Array 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 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 +static inline Array 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 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 +static inline Array 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 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 +static inline Array 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 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 +static inline Array 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 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 +static inline Array 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 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 +static inline Array 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 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 +static inline Array 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 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 +static inline Array 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 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 +static inline Array 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 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 +static inline Array 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 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 +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 +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 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 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 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 +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(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 + +#endif /* _UBUS_CPP_H */ diff --git a/lib/ubuscpp/include/ubuscpp/UBusObject.h b/lib/ubuscpp/include/ubuscpp/UBusObject.h new file mode 100644 index 00000000..093129bf --- /dev/null +++ b/lib/ubuscpp/include/ubuscpp/UBusObject.h @@ -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 */ diff --git a/lib/ubuscpp/src/UBusCall.cpp b/lib/ubuscpp/src/UBusCall.cpp new file mode 100644 index 00000000..c1a1e667 --- /dev/null +++ b/lib/ubuscpp/src/UBusCall.cpp @@ -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 +#include +} + +#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(aReq->priv); + theStr = blobmsg_format_json (aMsg, true); + + anUBusObject->SetResult (std::string (theStr)); + + free (theStr); +} \ No newline at end of file diff --git a/lib/ubuscpp/src/UBusObject.cpp b/lib/ubuscpp/src/UBusObject.cpp new file mode 100644 index 00000000..4c7e1d90 --- /dev/null +++ b/lib/ubuscpp/src/UBusObject.cpp @@ -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); +} diff --git a/lib/ubuscpp/src/common.h b/lib/ubuscpp/src/common.h new file mode 100644 index 00000000..0b743857 --- /dev/null +++ b/lib/ubuscpp/src/common.h @@ -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 */ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 00000000..732a99bc --- /dev/null +++ b/src/CMakeLists.txt @@ -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) diff --git a/src/domod/builders/cmake/CMakeLists.txt b/src/domod/builders/cmake/CMakeLists.txt new file mode 100644 index 00000000..af93acf1 --- /dev/null +++ b/src/domod/builders/cmake/CMakeLists.txt @@ -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 +) diff --git a/src/domod/src/helpers/Strings.cpp b/src/domod/src/helpers/Strings.cpp new file mode 100644 index 00000000..81cf7bbf --- /dev/null +++ b/src/domod/src/helpers/Strings.cpp @@ -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 +#include + +#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; +} diff --git a/src/domod/src/helpers/Strings.h b/src/domod/src/helpers/Strings.h new file mode 100644 index 00000000..92f9c0fb --- /dev/null +++ b/src/domod/src/helpers/Strings.h @@ -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 + + +std::string toupper (const std::string &src); + + #endif /* _HELPERS_STRINGS_H */ diff --git a/src/domod/src/helpers/Tokenizer.cpp b/src/domod/src/helpers/Tokenizer.cpp new file mode 100644 index 00000000..dfa7463c --- /dev/null +++ b/src/domod/src/helpers/Tokenizer.cpp @@ -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; +} diff --git a/src/domod/src/helpers/Tokenizer.h b/src/domod/src/helpers/Tokenizer.h new file mode 100644 index 00000000..cd9e1e12 --- /dev/null +++ b/src/domod/src/helpers/Tokenizer.h @@ -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 + +/*--------------------------------- 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 */ diff --git a/src/domod/src/main.cpp b/src/domod/src/main.cpp new file mode 100644 index 00000000..27247521 --- /dev/null +++ b/src/domod/src/main.cpp @@ -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 + +extern "C" { +#include /* getenv */ +#include +} + +#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; +} diff --git a/src/domod/src/models/Devices.cpp b/src/domod/src/models/Devices.cpp new file mode 100644 index 00000000..eadba445 --- /dev/null +++ b/src/domod/src/models/Devices.cpp @@ -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 +#include +#include + +#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 +#include + +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 */ diff --git a/src/domod/src/ubus/capabilities.cpp b/src/domod/src/ubus/capabilities.cpp new file mode 100644 index 00000000..0449b3d6 --- /dev/null +++ b/src/domod/src/ubus/capabilities.cpp @@ -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 +#include +#include + +extern "C" { +#include +} + +#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; +} diff --git a/src/domod/src/ubus/capabilities.h b/src/domod/src/ubus/capabilities.h new file mode 100644 index 00000000..b4c42751 --- /dev/null +++ b/src/domod/src/ubus/capabilities.h @@ -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 + +#include + +/*--------------------------------- 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 */ diff --git a/src/domod/src/ubus/capabilities_lights.cpp b/src/domod/src/ubus/capabilities_lights.cpp new file mode 100644 index 00000000..0e7da773 --- /dev/null +++ b/src/domod/src/ubus/capabilities_lights.cpp @@ -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 +} + +#include + +#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); +} diff --git a/src/domod/src/ubus/capabilities_lights.h b/src/domod/src/ubus/capabilities_lights.h new file mode 100644 index 00000000..11c08d17 --- /dev/null +++ b/src/domod/src/ubus/capabilities_lights.h @@ -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 + +#include +#include + + + +/*--------------------------------- 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 */ diff --git a/src/domod/src/ubus/capabilities_shutters.cpp b/src/domod/src/ubus/capabilities_shutters.cpp new file mode 100644 index 00000000..4e8a5c3c --- /dev/null +++ b/src/domod/src/ubus/capabilities_shutters.cpp @@ -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 +} + +#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; +} diff --git a/src/domod/src/ubus/capabilities_shutters.h b/src/domod/src/ubus/capabilities_shutters.h new file mode 100644 index 00000000..d242ad4b --- /dev/null +++ b/src/domod/src/ubus/capabilities_shutters.h @@ -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 + +#include + +/*--------------------------------- 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 */ diff --git a/src/domod/src/ubus/capabilities_sprinklers.cpp b/src/domod/src/ubus/capabilities_sprinklers.cpp new file mode 100644 index 00000000..24a6d59d --- /dev/null +++ b/src/domod/src/ubus/capabilities_sprinklers.cpp @@ -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 +} + +#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; +} diff --git a/src/domod/src/ubus/capabilities_sprinklers.h b/src/domod/src/ubus/capabilities_sprinklers.h new file mode 100644 index 00000000..e399cb35 --- /dev/null +++ b/src/domod/src/ubus/capabilities_sprinklers.h @@ -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 + +#include + +/*--------------------------------- 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 */ diff --git a/src/domod/src/ubus/speach_command.cpp b/src/domod/src/ubus/speach_command.cpp new file mode 100644 index 00000000..ad85b56d --- /dev/null +++ b/src/domod/src/ubus/speach_command.cpp @@ -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 + +extern "C" { +#include +} + +#include + +#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 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; +} diff --git a/src/domod/src/ubus/speach_command.h b/src/domod/src/ubus/speach_command.h new file mode 100644 index 00000000..afa33f37 --- /dev/null +++ b/src/domod/src/ubus/speach_command.h @@ -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 + +#include +#include + + + +/*--------------------------------- 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 */