Fix sprinkler build.

This commit is contained in:
2018-05-25 22:50:32 +02:00
parent 5b303675f6
commit 6cec7806e3
7 changed files with 173 additions and 176 deletions

View File

@@ -7,12 +7,11 @@ BR2_PACKAGE_OVERRIDE_FILE="$(BOARD)/local.mk"
BR2_GLOBAL_PATCH_DIR="$(BR2_EXTERNAL)/patches" BR2_GLOBAL_PATCH_DIR="$(BR2_EXTERNAL)/patches"
BR2_TOOLCHAIN_EXTERNAL=y BR2_TOOLCHAIN_EXTERNAL=y
BR2_TOOLCHAIN_EXTERNAL_DOWNLOAD=y BR2_TOOLCHAIN_EXTERNAL_DOWNLOAD=y
BR2_TOOLCHAIN_EXTERNAL_URL="http://serveur.nadal-fr.com/buildroot/toolchain_rpi-5_4_0-glibc-2_23-2.tar.bz2" BR2_TOOLCHAIN_EXTERNAL_URL="http://serveur.nadal-fr.com/buildroot/toolchain_rpi-5_4_0-glibc.tar.bz2"
BR2_TOOLCHAIN_EXTERNAL_GCC_5=y BR2_TOOLCHAIN_EXTERNAL_GCC_5=y
BR2_TOOLCHAIN_EXTERNAL_HEADERS_4_4=y BR2_TOOLCHAIN_EXTERNAL_HEADERS_4_4=y
BR2_TOOLCHAIN_EXTERNAL_CUSTOM_GLIBC=y BR2_TOOLCHAIN_EXTERNAL_CUSTOM_GLIBC=y
BR2_TOOLCHAIN_EXTERNAL_CXX=y BR2_TOOLCHAIN_EXTERNAL_CXX=y
BR2_TOOLCHAIN_EXTERNAL_GDB_SERVER_COPY=y
BR2_TARGET_GENERIC_HOSTNAME="rpi_Domo" BR2_TARGET_GENERIC_HOSTNAME="rpi_Domo"
BR2_TARGET_GENERIC_ISSUE="Welcome to Domo" BR2_TARGET_GENERIC_ISSUE="Welcome to Domo"
BR2_INIT_NONE=y BR2_INIT_NONE=y
@@ -60,4 +59,3 @@ BR2_PACKAGE_UHTTPD_REST_API_PLUGIN=y
BR2_PACKAGE_CHACOND=y BR2_PACKAGE_CHACOND=y
BR2_PACKAGE_SPRINKLERD=y BR2_PACKAGE_SPRINKLERD=y
BR2_PACKAGE_DOMOD=y BR2_PACKAGE_DOMOD=y
BR2_PACKAGE_DASHBOARD=y

View File

@@ -25,7 +25,7 @@ add_executable (sprinklerd ${source_files})
target_link_libraries (sprinklerd target_link_libraries (sprinklerd
LINK_PUBLIC LINK_PUBLIC
ubus-cpp ubus-cpp
json-cpp jsonc
ubox ubox
ubus ubus
blobmsg_json blobmsg_json

View File

@@ -1,5 +1,4 @@
/*! /*!
* main.c
* *
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
* *
@@ -28,7 +27,8 @@
#include <cstdio> #include <cstdio>
#include <unistd.h> #include <unistd.h>
extern "C" { extern "C"
{
#include <libubus.h> #include <libubus.h>
} }
@@ -38,83 +38,83 @@ extern "C" {
/*--------------------------------- GLOBALS ---------------------------------*/ /*--------------------------------- GLOBALS ---------------------------------*/
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn setupUbus * @fn setupUbus
* *
* @brief Setup the UBus Mechanism. * @brief Setup the UBus Mechanism.
*/ */
extern "C" { extern "C"
static struct ubus_context *setupUbus (void)
{ {
static struct ubus_context *setupUbus(void)
{
ubus_context *theCtx; ubus_context *theCtx;
if (uloop_init () !=0) { if (uloop_init() != 0)
{
fprintf (stderr, "Failed to init Uloop.\n"); fprintf(stderr, "Failed to init Uloop.\n");
return NULL; return NULL;
} }
signal (SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
theCtx = ubus_connect (NULL); theCtx = ubus_connect(NULL);
if (!theCtx) { if (!theCtx)
{
fprintf (stderr, "Failed to connect to ubus\n"); fprintf(stderr, "Failed to connect to ubus\n");
return NULL; return NULL;
} }
ubus_add_uloop (theCtx); ubus_add_uloop(theCtx);
return theCtx; return theCtx;
} }
} // SetupUbus. } // SetupUbus.
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn main * @fn main
* *
* @brief Main function of Domo Daemon. * @brief Main function of Domo Daemon.
*/ */
int main (void) int main(void)
{ {
int theRet = 0; int theRet = 0;
SprinkerModel theSprinklersModel; SprinkerModel theSprinklersModel;
struct ubus_context *theCtx = NULL; struct ubus_context *theCtx = NULL;
if (setuid (0)) { if (setuid(0))
{
perror ("setuid"); perror("setuid");
return 1; return 1;
} }
/* Setup the Ubus context. */ /* Setup the Ubus context. */
theCtx = setupUbus (); theCtx = setupUbus();
if (theCtx == NULL) { if (theCtx == NULL)
{
return -1; return -1;
} }
/* Setup the sprinklers. */ /* Setup the sprinklers. */
theRet = sprinklers_setup (); theRet = sprinklers_setup();
if (theRet != 0) { if (theRet != 0)
{
fprintf (stderr, "Impossible to Setup the Sprinkers.\n"); fprintf(stderr, "Impossible to Setup the Sprinkers.\n");
return theRet; return theRet;
} }
/* Add the UBus to the model exposed. */ /* Add the UBus to the model exposed. */
ubus_add_object (theCtx, &theSprinklersModel); ubus_add_object(theCtx, &theSprinklersModel);
/* Main Event Loop. */ /* Main Event Loop. */
uloop_run (); uloop_run();
ubus_free (theCtx); ubus_free(theCtx);
uloop_done (); uloop_done();
return theRet; return theRet;
} }

View File

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

View File

@@ -1,5 +1,4 @@
/*! /*!
* sprinklers.h
* *
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
* *
@@ -32,11 +31,11 @@
/*-------------------------------- METHODS ---------------------------------*/ /*-------------------------------- METHODS ---------------------------------*/
int sprinklers_setup (void); int sprinklers_setup(void);
int sprinkler_setup_station (int aStation); int sprinkler_setup_station(int a_station);
int sprinklers_set_station (uint32_t aStation, bool aState); int sprinklers_set_station(uint32_t a_station, bool a_state);
int sprinklers_get_station (uint32_t aStation, bool &aState); int sprinklers_get_station(uint32_t a_station, bool &a_state);
#endif /* __SPRINKLERS_H */ #endif /* __SPRINKLERS_H */

View File

@@ -1,5 +1,4 @@
/*! /*!
* UBusSprinkerModel.cpp
* *
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
* *
@@ -23,16 +22,16 @@
* *
*/ */
/*-------------------------------- INCLUDES ---------------------------------*/ /*-------------------------------- INCLUDES ---------------------------------*/
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <jsoncpp/json.h> // #include <jsoncpp/json.h>
extern "C" { extern "C"
{
#include <libubox/blobmsg_json.h> #include <libubox/blobmsg_json.h>
} }
@@ -40,50 +39,47 @@ extern "C" {
#include "ubus-sprinkler-model.h" #include "ubus-sprinkler-model.h"
namespace { namespace
{
using namespace UBus; using namespace UBus;
static ObjectType gSprinklerModelUbus_types( static ObjectType gSprinklerModelUbus_types(
"sprinklers", "sprinklers",
Method("get", UBUS_CPP(SprinkerModel, Get)), Method("get", UBUS_CPP(SprinkerModel, get)),
Method("set", UBUS_CPP(SprinkerModel, Set)) Method("set", UBUS_CPP(SprinkerModel, set)));
);
} } // namespace
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn SprinkerModel * @fn SprinkerModel
* *
* @brief Constructor of the UBus Mixer Volume. * @brief Constructor of the UBus Mixer Volume.
*/ */
SprinkerModel::SprinkerModel (void) : SprinkerModel::SprinkerModel(void) : UBusObject(gSprinklerModelUbus_types, "sprinklers")
UBusObject (gSprinklerModelUbus_types, "sprinklers")
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~SprinkerModel * @fn ~SprinkerModel
* *
* @brief Destructor of the UBus Mixer Volume. * @brief Destructor of the UBus Mixer Volume.
*/ */
SprinkerModel::~SprinkerModel (void) SprinkerModel::~SprinkerModel(void)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn Get * @fn get
* *
* @brief Get the State of a Sprinkler Station. * @brief Get the State of a Sprinkler Station.
*/ */
int SprinkerModel::Get (struct ubus_context *aCtx, struct ubus_request_data *aReq, int SprinkerModel::get(struct ubus_context *a_ctx, struct ubus_request_data *a_req, struct blob_attr *a_msg)
struct blob_attr *aMsg)
{ {
int theResult = 0; int the_result = 0;
#if 0
struct blob_buf theBuf = {0}; struct blob_buf theBuf = {0};
char *theString = blobmsg_format_json (aMsg, true); char *theString = blobmsg_format_json(aMsg, true);
Json::Reader theReader; Json::Reader theReader;
Json::StyledWriter theWriter; Json::StyledWriter theWriter;
Json::Value theRoot; Json::Value theRoot;
@@ -91,81 +87,86 @@ int SprinkerModel::Get (struct ubus_context *aCtx, struct ubus_request_data *aRe
uint32_t theStation = 0; uint32_t theStation = 0;
bool theState = false; bool theState = false;
if (!theReader.parse (theString, theRoot)) { if (!theReader.parse(theString, theRoot))
{
fprintf (stderr, "Failed parse the parameters.\n"); fprintf(stderr, "Failed parse the parameters.\n");
return UBUS_STATUS_INVALID_ARGUMENT; return UBUS_STATUS_INVALID_ARGUMENT;
} }
theStation = theRoot["station"].asInt(); theStation = theRoot["station"].asInt();
if (theStation == 0) { if (theStation == 0)
{
fprintf (stderr, "Station ID is missing.\n"); fprintf(stderr, "Station ID is missing.\n");
return UBUS_STATUS_INVALID_ARGUMENT; return UBUS_STATUS_INVALID_ARGUMENT;
} }
theResult = sprinklers_get_station (theStation, theState); theResult = sprinklers_get_station(theStation, theState);
if (theResult != 0) { if (theResult != 0)
{
fprintf (stderr, "Wrong Station ID (%d).\n", theStation); fprintf(stderr, "Wrong Station ID (%d).\n", theStation);
return UBUS_STATUS_INVALID_ARGUMENT; return UBUS_STATUS_INVALID_ARGUMENT;
} }
printf ("aState: %d\n", theState); printf("aState: %d\n", theState);
theOutput ["station"] = theStation; theOutput["station"] = theStation;
theOutput ["state"] = theState; theOutput["state"] = theState;
blob_buf_init (&theBuf, 0); blob_buf_init(&theBuf, 0);
blobmsg_add_json_from_string (&theBuf, theWriter.write (theOutput).c_str()); blobmsg_add_json_from_string(&theBuf, theWriter.write(theOutput).c_str());
theResult = ubus_send_reply (aCtx, aReq, theBuf.head); theResult = ubus_send_reply(aCtx, aReq, theBuf.head);
blob_buf_free (&theBuf); blob_buf_free(&theBuf);
#endif
return theResult; return the_result;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn Set * @fn set
* *
* @brief Set a new State for a Sprinkler Station. * @brief Set a new State for a Sprinkler Station.
*/ */
int SprinkerModel::Set (struct ubus_context *aCtx, struct ubus_request_data *aReq, int SprinkerModel::set(struct ubus_context *a_ctx, struct ubus_request_data *a_req, struct blob_attr *a_msg)
struct blob_attr *aMsg)
{ {
int theResult; int the_result = 0;
char *theString = blobmsg_format_json (aMsg, true); #if 0
char *theString = blobmsg_format_json(aMsg, true);
Json::Reader theReader; Json::Reader theReader;
Json::Value theRoot; Json::Value theRoot;
uint32_t theStation = 0; uint32_t theStation = 0;
bool theState = false; bool theState = false;
if (!theReader.parse (theString, theRoot)) { if (!theReader.parse(theString, theRoot))
{
fprintf (stderr, "Failed parse the parameters.\n"); fprintf(stderr, "Failed parse the parameters.\n");
return UBUS_STATUS_INVALID_ARGUMENT; return UBUS_STATUS_INVALID_ARGUMENT;
} }
theStation = theRoot["station"].asInt(); theStation = theRoot["station"].asInt();
theState = theRoot["state"].asBool(); theState = theRoot["state"].asBool();
if (theStation == 0) { if (theStation == 0)
{
fprintf (stderr, "Station ID is missing.\n"); fprintf(stderr, "Station ID is missing.\n");
return UBUS_STATUS_INVALID_ARGUMENT; return UBUS_STATUS_INVALID_ARGUMENT;
} }
theResult = sprinklers_set_station (theStation, theState); theResult = sprinklers_set_station(theStation, theState);
if (theResult != 0) { if (theResult != 0)
{
fprintf (stderr, "Wrong Station ID (%d).\n", theStation); fprintf(stderr, "Wrong Station ID (%d).\n", theStation);
return UBUS_STATUS_INVALID_ARGUMENT; return UBUS_STATUS_INVALID_ARGUMENT;
} }
#endif
return theResult; return the_result;
} }

View File

@@ -1,5 +1,4 @@
/*! /*!
* UBusModel.h
* *
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
* *
@@ -30,20 +29,20 @@
#include <stdint.h> #include <stdint.h>
#include <ubus-cpp/UBusObject.h> #include <ubus-cpp/ubus-object.h>
/*--------------------------------- Define ----------------------------------*/ /*--------------------------------- Define ----------------------------------*/
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class SprinkerModel : public UBusObject { class SprinkerModel : public UBusObject
{
public:
SprinkerModel(void);
~SprinkerModel(void);
public: int get(struct ubus_context *, struct ubus_request_data *, struct blob_attr *);
SprinkerModel (void); int set(struct ubus_context *, struct ubus_request_data *, struct blob_attr *);
~SprinkerModel (void);
int Get (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Set (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
}; };
#endif /* _UBUS_SPRINKLER_MODEL_H */ #endif /* _UBUS_SPRINKLER_MODEL_H */