aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2016-11-30 16:46:37 +0000
committerRomain Forlot <romain.forlot@iot.bzh>2016-12-01 11:19:54 +0000
commitad8be41f63198aea13155189e8215d4bf8c4c204 (patch)
treeba229e0e15efe0982700ee0bec8f1149be9ab3d8
parent530020b8e51c9a212d7630e90db77f7c08756644 (diff)
Renamed to hvac-demo
Change-Id: I8254485a830db6e0dee06e7df62c63c7ce9fb5d5 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r--CMakeLists.txt6
-rw-r--r--config.xml.in2
-rw-r--r--hvac-demo-bindings.c (renamed from hvac-hybrid-qml-binding.c)104
-rw-r--r--icon_hvac-demo.png (renamed from icon_hvac_hybrid_qml.png)bin4236 -> 4236 bytes
-rw-r--r--qml/hvac-demo-app.qml (renamed from qml/hvac-hybrid-qml-app.qml)0
5 files changed, 52 insertions, 60 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f0b4776..c449422 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,14 +17,14 @@
# limitations under the License.
###########################################################################
-project(hvac-hybrid-qml)
+project(hvac-demo)
cmake_minimum_required(VERSION 3.3)
include(GNUInstallDirs)
set(PROJECT_VERSION "0.1")
-set(PROJECT_ICON "icon_hvac_hybrid_qml.png")
+set(PROJECT_ICON "icon_hvac-demo.png")
set(PROJECT_LIBDIR "lib")
set(CMAKE_BUILD_TYPE Debug)
@@ -61,7 +61,7 @@ link_libraries(${EXTRAS_LIBRARIES})
message(STATUS "Creation of ${PROJECT_NAME} for AFB-DAEMON")
###############################################################
-add_library(${PROJECT_NAME} MODULE ${PROJECT_NAME}-binding.c
+add_library(${PROJECT_NAME} MODULE ${PROJECT_NAME}-bindings.c
./qml)
# ./qml/${PROJECT_NAME}-app.qml)
diff --git a/config.xml.in b/config.xml.in
index 1d9c86d..912b3c3 100644
--- a/config.xml.in
+++ b/config.xml.in
@@ -3,7 +3,7 @@
<name>@PROJECT_NAME@</name>
<icon src="icon.png"/>
<content src="qml/@PROJECT_NAME@-app.qml" type="application/vnd.agl.qml.hybrid"/>
- <description>This application is used to control and dialog with HVAC system</description>
+ <description>This is a demo application used to control and dialog with HVAC system</description>
<author>Romain Forlot &lt;romain.forlot@iot.bzh&gt;</author>
<license>APL 2.0</license>
</widget>
diff --git a/hvac-hybrid-qml-binding.c b/hvac-demo-bindings.c
index 4b5434c..b43cfa1 100644
--- a/hvac-hybrid-qml-binding.c
+++ b/hvac-demo-bindings.c
@@ -32,6 +32,8 @@
#define CAN_DEV "can0"
+#define SIMULATE_HVAC
+
static const struct afb_binding_interface *interface;
// Initialize CAN hvac array that will be sent trough the socket
@@ -77,6 +79,10 @@ static struct can_handler can_handler = { .socket = -1 };
static int open_can_dev()
{
+#if defined(SIMULATE_HVAC)
+ can_handler.socket = 0;
+ return 0;
+#else
struct ifreq ifr;
can_handler.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
@@ -110,6 +116,7 @@ static int open_can_dev()
can_handler.socket = -1;
}
return -1;
+#endif
}
// Get original get temperature function from cpp hvacplugin code
@@ -144,12 +151,19 @@ static int write_can()
txCanFrame.data[6] = can_hvac_values[6];
txCanFrame.data[7] = can_hvac_values[7];
+#if defined(SIMULATE_HVAC)
+ DEBUG(interface, "WRITING CAN: %d %d [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
+ txCanFrame.can_id, txCanFrame.can_dlc,
+ txCanFrame.data[0], txCanFrame.data[1], txCanFrame.data[2], txCanFrame.data[3],
+ txCanFrame.data[4], txCanFrame.data[5], txCanFrame.data[6], txCanFrame.data[7]);
+#else
rc = sendto(can_handler.socket, &txCanFrame, sizeof(struct can_frame), 0,
(struct sockaddr*)&can_handler.txAddress, sizeof(can_handler.txAddress));
if (rc < 0)
{
ERROR(interface, "Sending can frame failed");
}
+#endif
}
else
{
@@ -173,25 +187,6 @@ static uint8_t read_fanspeed()
return can_hvac_values[4];
}
-static int make_uint8(const char *str, uint8_t *value)
-{
- long int x;
- char **end;
-
- if (str == NULL)
- return -1;
-
- x = strtol(str, &end, 10);
- if (end == str || *end)
- return -1;
-
- if (x < 0 || x > 255)
- return -1;
-
- *value = (uint8_t)x;
- return 0;
-}
-
/*****************************************************************************************/
/*****************************************************************************************/
/** **/
@@ -279,52 +274,49 @@ static void get(struct afb_req request)
*/
static void set(struct afb_req request)
{
- int i, rc, nerr, nchg;
- const char *val, *key;
- struct json_object *query;
- struct json_object_iterator iter;
- struct json_object_iterator iter_end;
+ int i, rc, x;
+ struct json_object *query, *val;
+ uint8_t values[sizeof can_hvac_components / sizeof *can_hvac_components];
+ uint8_t saves[sizeof can_hvac_components / sizeof *can_hvac_components];
+ /* records initial values */
+ memcpy(values, can_hvac_values, sizeof values);
+ memcpy(saves, can_hvac_values, sizeof saves);
+
+ /* Loop getting arguments */
query = afb_req_json(request);
- iter = json_object_iter_begin(query);
- iter_end = json_object_iter_end(query);
-
- /*
- * Loop over the json object that will set every component
- * which it will find in it.
- */
- nchg = nerr = 0;
- while(!json_object_iter_equal(&iter, &iter_end))
+ i = (int)(sizeof can_hvac_components / sizeof *can_hvac_components);
+ while (i)
{
- key = json_object_iter_peek_name(&iter);
- i = 0;
- while (i < 8 && can_hvac_components[i] != NULL && strcmp(can_hvac_components[i], key) != 0)
- {
- i++;
- }
- if (i < 8)
+ if (json_object_object_get_ex(query, can_hvac_components[--i], &val))
{
- val = afb_req_value(request, key);
- rc = make_uint8(val, &can_hvac_values[i]);
- if (rc < 0)
- nerr++;
- else
- nchg++;
- }
- else
- {
- // not found! ignore the error silently
- nerr++;
+ if (!json_object_is_type(json_type_int))
+ {
+ afb_req_fail_f(request, "bad-request",
+ "argument '%s' isn't integer", can_hvac_components[i]);
+ return;
+ }
+ x = json_object_get_int(val);
+ if (x < 0 || x > 255)
+ {
+ afb_req_fail_f(request, "bad-request",
+ "argument '%s' is out of bounds", can_hvac_components[i]);
+ return;
+ }
+ values[i] = (uint8_t)x;
}
-
- json_object_iter_next(&iter);
}
+ /* attemps to set new values */
+ memcpy(can_hvac_values, values, sizeof values);
rc = nchg ? write_can() : 0;
- if (rc < 0)
- afb_req_fail(request, "error", "CAN error");
- else
+ if (rc >= 0)
afb_req_success(request, NULL, nerr ? "error detected" : NULL);
+ else {
+ /* restore initial values */
+ memcpy(can_hvac_values, saves, sizeof saves);
+ afb_req_fail(request, "error", "CAN error");
+ }
}
// TODO: Have to change session management flag to AFB_SESSION_CHECK to use token auth
diff --git a/icon_hvac_hybrid_qml.png b/icon_hvac-demo.png
index 27df1a0..27df1a0 100644
--- a/icon_hvac_hybrid_qml.png
+++ b/icon_hvac-demo.png
Binary files differ
diff --git a/qml/hvac-hybrid-qml-app.qml b/qml/hvac-demo-app.qml
index 4472803..4472803 100644
--- a/qml/hvac-hybrid-qml-app.qml
+++ b/qml/hvac-demo-app.qml