update sprinklerd to used with json-c

This commit is contained in:
2018-06-15 11:55:50 +02:00
parent 6cec7806e3
commit 6786d7a4f7
7 changed files with 144 additions and 130 deletions

View File

@@ -108,10 +108,12 @@ int DevicesController::create(struct ubus_context *, struct ubus_request_data *,
if (m_devices_manager->create(m_capability, the_root_node) < 0) if (m_devices_manager->create(m_capability, the_root_node) < 0)
{ {
fprintf(stderr, "Failed to create a new light.\n"); fprintf(stderr, "Failed to create a new device.\n");
return UBUS_STATUS_INVALID_ARGUMENT; return UBUS_STATUS_INVALID_ARGUMENT;
} }
json_object_put(the_root_node);
return 0; return 0;
} }

View File

@@ -9,7 +9,7 @@ include (br)
include_directories ($ENV{SRC_DIR}/src/prog/sprinklerd) include_directories ($ENV{SRC_DIR}/src/prog/sprinklerd)
set(CMAKE_CXX_FLAGS "-Wall -Wextra -pedantic -Werror=strict-aliasing") # set(CMAKE_CXX_FLAGS "-Wall -Wextra -pedantic -Werror=strict-aliasing")
file( file(
GLOB_RECURSE GLOB_RECURSE
@@ -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
jsonc json-c
ubox ubox
ubus ubus
blobmsg_json blobmsg_json

View File

@@ -1,5 +1,4 @@
/*! /*!
*
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@@ -45,9 +44,9 @@ extern "C"
*/ */
extern "C" extern "C"
{ {
static struct ubus_context *setupUbus(void) static struct ubus_context *setup_ubus(void)
{ {
ubus_context *theCtx; ubus_context *the_ctx;
if (uloop_init() != 0) if (uloop_init() != 0)
{ {
@@ -57,31 +56,31 @@ extern "C"
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
theCtx = ubus_connect(NULL); the_ctx = ubus_connect(NULL);
if (!theCtx) if (!the_ctx)
{ {
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(the_ctx);
return theCtx; return the_ctx;
} }
} // SetupUbus. }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn main * @fn main
* *
* @brief Main function of Domo Daemon. * @brief Main function of Sprinkler Daemon.
*/ */
int main(void) int main(void)
{ {
int theRet = 0; int the_ret = 0;
SprinkerModel theSprinklersModel; SprinkerModel the_sprinklers_model;
struct ubus_context *theCtx = NULL; struct ubus_context *the_ctx = NULL;
if (setuid(0)) if (setuid(0))
{ {
@@ -90,31 +89,29 @@ int main(void)
} }
/* Setup the Ubus context. */ /* Setup the Ubus context. */
theCtx = setupUbus(); the_ctx = setup_ubus();
if (theCtx == NULL) if (the_ctx == NULL)
{ {
return -1; return -1;
} }
/* Setup the sprinklers. */ /* Setup the sprinklers. */
theRet = sprinklers_setup(); the_ret = sprinklers_setup();
if (theRet != 0) if (the_ret != 0)
{ {
fprintf(stderr, "Impossible to Setup the Sprinklers.\n");
fprintf(stderr, "Impossible to Setup the Sprinkers.\n"); return the_ret;
return theRet;
} }
/* Add the UBus to the model exposed. */ /* Add the UBus to the model exposed. */
ubus_add_object(theCtx, &theSprinklersModel); ubus_add_object(the_ctx, &the_sprinklers_model);
/* Main Event Loop. */ /* Main Event Loop. */
uloop_run(); uloop_run();
ubus_free(theCtx); ubus_free(the_ctx);
uloop_done(); uloop_done();
return theRet; return the_ret;
} }

View File

@@ -1,5 +1,4 @@
/*! /*!
*
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@@ -28,9 +27,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <json-c/json.h>
#include <string> #include <string>
#include <json-c/json.h>
#include <ubus-cpp/ubus-call.h> #include <ubus-cpp/ubus-call.h>
#include "sprinklers.h" #include "sprinklers.h"
@@ -40,39 +40,45 @@
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn sprinklers_setup * @fn sprinklers_setup
* *
* @brief this function setup the OSPI environement. * @brief this function setup the GPIO environnement.
*/ */
int sprinklers_setup(void) int sprinklers_setup(void)
{ {
UBusCall theCmd; UBusCall the_cmd;
std::string theResult; std::string the_result;
int theIDVal; int the_id;
#if 0
fprintf(stderr, "Setup the Gpios used by the Sprinklers.\n"); fprintf(stderr, "Setup the Gpios used by the Sprinklers.\n");
if (theCmd.Exec("domo.capabilities.sprinklers", "get", "", theResult) == 0) if (the_cmd.exec("domo.sprinklers", "list", "", the_result) == 0)
{ {
// We get the the sprinklers. Get the IDs. struct json_object *the_root_node, *the_sprinklers_array_node, *the_sprinkler_node, *the_value_node;
Json::Reader theReader; the_root_node = json_tokener_parse(the_result.c_str());
Json::Value theRoot;
Json::Value theElement;
if (!theReader.parse(theResult, theRoot)) if (the_root_node == NULL)
{ {
fprintf(stderr, "Impossible to get the Sprinklers configuration.\n");
fprintf(stderr, "Failed parse the List of Sprinklers.\n");
return -1; return -1;
} }
for (const Json::Value &theElement : theRoot["Sprinklers"]) if (json_object_object_get_ex(the_root_node, "sprinklers", &the_sprinklers_array_node))
{ {
int i, the_array_len = json_object_array_length(the_sprinklers_array_node);
theIDVal = theElement["id"].asInt(); for (i = 0; i < the_array_len; i++)
sprinkler_setup_station(theIDVal); {
the_sprinkler_node = json_object_array_get_idx(the_sprinklers_array_node, i);
if (json_object_object_get_ex(the_sprinkler_node, "id", &the_value_node))
{
the_id = json_object_get_int(the_value_node);
sprinkler_setup_station(the_id);
}
}
} }
json_object_put(the_root_node);
} }
#endif
return 0; return 0;
} }
@@ -83,22 +89,22 @@ int sprinklers_setup(void)
*/ */
int sprinkler_setup_station(int a_station) int sprinkler_setup_station(int a_station)
{ {
char theCommand[255]; char the_command[255];
#if 0
printf(" Setup the Station: %d\n", aStation); printf(" Setup the Station: %d\n", a_station);
// Activate the Gpio // Activate the Gpio
sprintf(theCommand, "echo %d > /sys/class/gpio/export", aStation); sprintf(the_command, "echo %d > /sys/class/gpio/export", a_station);
system(theCommand); system(the_command);
// Select the Direction of the Gpio // Select the Direction of the Gpio
sprintf(theCommand, "echo out > /sys/class/gpio/gpio%d/direction", aStation); sprintf(the_command, "echo out > /sys/class/gpio/gpio%d/direction", a_station);
system(theCommand); system(the_command);
// Select the Direction of the Gpio // Select the Direction of the Gpio
sprintf(theCommand, "echo 1 > /sys/class/gpio/gpio%d/value", aStation); sprintf(the_command, "echo 1 > /sys/class/gpio/gpio%d/value", a_station);
system(theCommand); system(the_command);
#endif
return 0; return 0;
} }
@@ -109,30 +115,30 @@ int sprinkler_setup_station(int a_station)
*/ */
int sprinklers_set_station(uint32_t a_station, bool a_state) int sprinklers_set_station(uint32_t a_station, bool a_state)
{ {
FILE *theFp; FILE *the_fp;
char theGpioPath[255]; char the_gpio_path[255];
#if 0
sprintf(theGpioPath, "/sys/class/gpio/gpio%d/value", aStation);
theFp = fopen(theGpioPath, "w"); sprintf(the_gpio_path, "/sys/class/gpio/gpio%d/value", a_station);
if (theFp == NULL) the_fp = fopen(the_gpio_path, "w");
if (the_fp == NULL)
{ {
fprintf(stderr, "Impossible to open: <%s>\n", theGpioPath); fprintf(stderr, "Impossible to open: <%s>\n", the_gpio_path);
return -1; return -1;
} }
if (aState) if (a_state)
{ {
fwrite("0", 1, 1, theFp); fwrite("0", 1, 1, the_fp);
} }
else else
{ {
fwrite("1", 1, 1, theFp); fwrite("1", 1, 1, the_fp);
} }
fclose(theFp); fclose(the_fp);
#endif
return 0; return 0;
} }
@@ -143,38 +149,37 @@ int sprinklers_set_station(uint32_t a_station, bool a_state)
*/ */
int sprinklers_get_station(uint32_t a_station, bool &a_state) int sprinklers_get_station(uint32_t a_station, bool &a_state)
{ {
#if 0 FILE *the_fp;
FILE *theFp; char the_gpio_path[255];
char theGpioPath[255]; char the_data[5];
char theData[5];
fprintf(stdout, "get station state (%d)\n", aStation); fprintf(stdout, "get station state (%d)\n", a_station);
sprintf(theGpioPath, "/sys/class/gpio/gpio%d/value", aStation); sprintf(the_gpio_path, "/sys/class/gpio/gpio%d/value", a_station);
theFp = fopen(theGpioPath, "r"); the_fp = fopen(the_gpio_path, "r");
if (theFp == NULL) if (the_fp == NULL)
{ {
fprintf(stderr, "Impossible to open: <%s>\n", theGpioPath); fprintf(stderr, "Impossible to open: <%s>\n", the_gpio_path);
return -1; return -1;
} }
fread(theData, 1, 1, theFp); fread(the_data, 1, 1, the_fp);
//printf ("theData: %s\n", theData); //printf ("theData: %s\n", theData);
if (theData[0] == '0') if (the_data[0] == '0')
{ {
aState = true; a_state = true;
} }
else else
{ {
aState = false; a_state = false;
} }
fclose(theFp); fclose(the_fp);
#endif
return 0; return 0;
} }

View File

@@ -1,5 +1,4 @@
/*! /*!
*
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or

View File

@@ -1,5 +1,4 @@
/*! /*!
*
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@@ -28,7 +27,7 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
// #include <jsoncpp/json.h> #include <json-c/json.h>
extern "C" extern "C"
{ {
@@ -77,53 +76,60 @@ SprinkerModel::~SprinkerModel(void)
int SprinkerModel::get(struct ubus_context *a_ctx, struct ubus_request_data *a_req, struct blob_attr *a_msg) int SprinkerModel::get(struct ubus_context *a_ctx, struct ubus_request_data *a_req, struct blob_attr *a_msg)
{ {
int the_result = 0; int the_result = 0;
#if 0 struct blob_buf the_buf = { 0 };
struct blob_buf theBuf = {0}; struct json_object *the_root_node, *the_station_node;
char *theString = blobmsg_format_json(aMsg, true); uint32_t the_station_id = 0;
Json::Reader theReader; bool the_state = false;
Json::StyledWriter theWriter;
Json::Value theRoot;
Json::Value theOutput;
uint32_t theStation = 0;
bool theState = false;
if (!theReader.parse(theString, theRoot)) char *the_string = blobmsg_format_json(a_msg, true);
the_root_node = json_tokener_parse(the_string);
free(the_string);
if (the_root_node == NULL)
{ {
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(); if (json_object_object_get_ex(the_root_node, "station", &the_station_node))
if (theStation == 0)
{ {
the_station_id = json_object_get_int(the_station_node);
}
json_object_put(the_root_node);
if (the_station_id == 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); the_result = sprinklers_get_station(the_station_id, the_state);
if (theResult != 0) if (the_result != 0)
{ {
fprintf(stderr, "Wrong Station ID (%d).\n", the_station_id);
fprintf(stderr, "Wrong Station ID (%d).\n", theStation);
return UBUS_STATUS_INVALID_ARGUMENT; return UBUS_STATUS_INVALID_ARGUMENT;
} }
printf("aState: %d\n", theState); printf("station<%d> state: %d\n", the_station_id, the_state);
theOutput["station"] = theStation;
theOutput["state"] = theState;
blob_buf_init(&theBuf, 0); the_root_node = json_object_new_object ();
blobmsg_add_json_from_string(&theBuf, theWriter.write(theOutput).c_str()); json_object_object_add(the_root_node, "station", json_object_new_int(the_station_id));
json_object_object_add(the_root_node, "state", json_object_new_boolean(the_state));
theResult = ubus_send_reply(aCtx, aReq, theBuf.head); blob_buf_init(&the_buf, 0);
blobmsg_add_json_from_string(&the_buf, json_object_to_json_string(the_root_node));
the_result = ubus_send_reply(a_ctx, a_req, the_buf.head);
blob_buf_free(&the_buf);
json_object_put(the_root_node);
blob_buf_free(&theBuf);
#endif
return the_result; return the_result;
} }
@@ -132,41 +138,47 @@ int SprinkerModel::get(struct ubus_context *a_ctx, struct ubus_request_data *a_r
* *
* @brief Set a new State for a Sprinkler Station. * @brief Set a new State for a Sprinkler Station.
*/ */
int SprinkerModel::set(struct ubus_context *a_ctx, struct ubus_request_data *a_req, struct blob_attr *a_msg) int SprinkerModel::set(struct ubus_context *, struct ubus_request_data *, struct blob_attr *a_msg)
{ {
int the_result = 0; int the_result = 0;
#if 0 struct json_object *the_root_node, *the_value_node;
char *theString = blobmsg_format_json(aMsg, true); uint32_t the_station_id = 0;
Json::Reader theReader; bool the_state = false;
Json::Value theRoot;
uint32_t theStation = 0;
bool theState = false;
if (!theReader.parse(theString, theRoot)) char *the_string = blobmsg_format_json(a_msg, true);
the_root_node = json_tokener_parse(the_string);
free(the_string);
if (the_root_node == NULL)
{ {
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(); if (json_object_object_get_ex(the_root_node, "station", &the_value_node))
theState = theRoot["state"].asBool();
if (theStation == 0)
{ {
the_station_id = json_object_get_int(the_value_node);
}
if (json_object_object_get_ex(the_root_node, "state", &the_value_node))
{
the_state = json_object_get_boolean(the_value_node);
}
if (the_station_id == 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); the_result = sprinklers_set_station(the_station_id, the_state);
if (theResult != 0) if (the_result != 0)
{ {
fprintf(stderr, "Wrong Station ID (%d).\n", the_station_id);
fprintf(stderr, "Wrong Station ID (%d).\n", theStation);
return UBUS_STATUS_INVALID_ARGUMENT; return UBUS_STATUS_INVALID_ARGUMENT;
} }
#endif
return the_result; return the_result;
} }

View File

@@ -1,5 +1,4 @@
/*! /*!
*
* Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or