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

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

View File

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

View File

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

View File

@@ -1,5 +1,4 @@
/*!
* sprinklers.h
*
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
*
@@ -29,14 +28,14 @@
/*-------------------------------- INCLUDES ---------------------------------*/
#include <stdint.h>
/*-------------------------------- 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_get_station (uint32_t aStation, bool &aState);
int sprinklers_set_station(uint32_t a_station, bool a_state);
int sprinklers_get_station(uint32_t a_station, bool &a_state);
#endif /* __SPRINKLERS_H */

View File

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

View File

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