aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--hvac-hybrid-qml-binding.c136
1 files changed, 76 insertions, 60 deletions
diff --git a/hvac-hybrid-qml-binding.c b/hvac-hybrid-qml-binding.c
index 94b3269..42208ed 100644
--- a/hvac-hybrid-qml-binding.c
+++ b/hvac-hybrid-qml-binding.c
@@ -1,6 +1,7 @@
/*
* Copyright (C) 2015, 2016 "IoT.bzh"
- * Author "Manuel Bachmann"
+ * Author "Romain Forlot"
+ * Author "Jose Bolo"
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -38,15 +39,15 @@ static char *can_hvac_components[8] = {
"LeftTemperature",
"RightTemperature",
"Temperature",
- "Unknow3",
+ NULL,
"FanSpeed",
- "Unknow5",
- "Unknow6",
- "Unknow7"
+ NULL,
+ NULL,
+ NULL
};
// Initialize CAN hvac array that will be sent trough the socket
-static __u8 can_hvac_values[8] = {
+static uint8_t can_hvac_values[8] = {
21, // LeftTemperature
21, // RightTemperature
21, // AverageTemperature
@@ -62,7 +63,7 @@ struct can_handler {
struct sockaddr_can txAddress;
};
-static struct can_handler can_handler;
+static struct can_handler can_handler = { .socket = -1 };
/*****************************************************************************************/
/*****************************************************************************************/
@@ -114,25 +115,26 @@ static int open_can_dev()
// Get original get temperature function from cpp hvacplugin code
static uint8_t get_temperature(uint8_t value)
{
- uint8_t result = ((0xF0 - 0x10) / 15) * value - 16;
+ int result = ((0xF0 - 0x10) / 15) * value - 16;
if (result < 0x10)
result = 0x10;
if (result > 0xF0)
result = 0xF0;
- return result;
+ return (uint8_t)result;
}
-static void write_can()
+static int write_can()
{
- // Hardcoded can_id and dlc (data lenght code)
struct can_frame txCanFrame;
- txCanFrame.can_id = 0x30;
- txCanFrame.can_dlc = 8;
+ int rc = 0;
- if (can_handler.socket >= 0)
+ rc = can_handler.socket;
+ if (rc >= 0)
{
-
+ // Hardcoded can_id and dlc (data lenght code)
+ txCanFrame.can_id = 0x30;
+ txCanFrame.can_dlc = 8;
txCanFrame.data[0] = get_temperature(can_hvac_values[0]);
txCanFrame.data[1] = get_temperature(can_hvac_values[1]);
txCanFrame.data[2] = get_temperature((can_hvac_values[0] + can_hvac_values[1]) / 2);
@@ -142,33 +144,52 @@ static void write_can()
txCanFrame.data[6] = can_hvac_values[6];
txCanFrame.data[7] = can_hvac_values[7];
- sendto(can_handler.socket, &txCanFrame, sizeof(struct can_frame), 0,
+ 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");
+ }
}
else
{
ERROR(interface, "socket not initialized");
}
+ return rc;
}
-static __u8 read_temp_left_zone()
+static uint8_t read_temp_left_zone()
{
return can_hvac_values[0];
}
-static __u8 read_temp_right_zone()
+static uint8_t read_temp_right_zone()
{
return can_hvac_values[1];
}
-static __u8 read_fanspeed()
+static uint8_t read_fanspeed()
{
return can_hvac_values[4];
}
-static uint8_t make_atoi(const char *str)
+static int make_uint8(const char *str, uint8_t *value)
{
- return str ? atoi(str) : 0;
+ 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;
}
/*****************************************************************************************/
@@ -182,20 +203,6 @@ static uint8_t make_atoi(const char *str)
/*****************************************************************************************/
/*
- * @brief Simple afb-daemon ping
- *
- * @param struct afb_req : an afb request structure
- *
- */
-static void ping (struct afb_req request)
-{
- static int pingcount = 0;
-
- json_object *query = afb_req_json(request);
- afb_req_success_f(request, NULL, "Ping Binder Daemon count=%d query=%s", ++pingcount, json_object_to_json_string(query));
-}
-
-/*
* @brief Get fan speed HVAC system
*
* @param struct afb_req : an afb request structure
@@ -204,13 +211,12 @@ static void ping (struct afb_req request)
static void get_fanspeed(struct afb_req request)
{
json_object *ret_json;
- __u8 fanspeed = read_fanspeed();
+ uint8_t fanspeed = read_fanspeed();
ret_json = json_object_new_object();
json_object_object_add(ret_json, "FanSpeed", json_object_new_int(fanspeed));
- json_object *query = afb_req_json(request);
- afb_req_success_f(request, ret_json, "Fan Speed is:%d query=%s", fanspeed, json_object_to_json_string(query));
+ afb_req_success(request, ret_json, NULL);
}
/*
@@ -222,13 +228,12 @@ static void get_fanspeed(struct afb_req request)
static void get_temp_right_zone(struct afb_req request)
{
json_object *ret_json;
- __u8 temp = read_temp_right_zone();
+ uint8_t temp = read_temp_right_zone();
ret_json = json_object_new_object();
json_object_object_add(ret_json, "RightTemperature", json_object_new_int(temp));
- json_object *query = afb_req_json(request);
- afb_req_success_f(request, ret_json, "Right zone Temperature is:%d query=%s", temp, json_object_to_json_string(query));
+ afb_req_success(request, ret_json, NULL);
}
/*
@@ -240,13 +245,12 @@ static void get_temp_right_zone(struct afb_req request)
static void get_temp_left_zone(struct afb_req request)
{
json_object *ret_json;
- __u8 temp = read_temp_left_zone();
+ uint8_t temp = read_temp_left_zone();
ret_json = json_object_new_object();
json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(temp));
- json_object *query = afb_req_json(request);
- afb_req_success_f(request, ret_json, "Left zone Temperature is:%d query=%s", temp, json_object_to_json_string(query));
+ afb_req_success(request, ret_json, NULL);
}
/*
@@ -275,7 +279,8 @@ static void get_all(struct afb_req request)
*/
static void set(struct afb_req request)
{
- const char *val;
+ int i, rc, nerr, nchg;
+ const char *val, *key;
struct json_object *query;
struct json_object_iterator iter;
struct json_object_iterator iter_end;
@@ -288,31 +293,42 @@ static void set(struct afb_req request)
* Loop over the json object that will set every component
* which it will find in it.
*/
- int i;
+ nchg = nerr = 0;
while(!json_object_iter_equal(&iter, &iter_end))
{
- const char *key = json_object_iter_peek_name(&iter);
- for (i=0;i<8;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)
{
- if(strcmp(can_hvac_components[i], key) == 0)
- {
- val = afb_req_value(request, key);
- uint8_t comp_val = make_atoi(val);
- can_hvac_values[i] = comp_val;
- break;
- }
+ i++;
+ }
+ if (i < 8)
+ {
+ 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++;
}
+
json_object_iter_next(&iter);
}
- write_can();
-
- afb_req_success(request, query, "HVAC settings updated");
+ rc = nchg ? write_can() : 0;
+ if (rc < 0)
+ afb_req_fail(request, "error", "CAN error");
+ else
+ afb_req_success(request, NULL, nerr ? "error detected" : NULL);
}
// TODO: Have to change session management flag to AFB_SESSION_CHECK to use token auth
static const struct afb_verb_desc_v1 verbs[]= {
- {"ping" , AFB_SESSION_NONE, ping , "Ping the binder"},
{"get_temp_left_zone" , AFB_SESSION_NONE, get_temp_left_zone , "Get the left zone temperature"},
{"get_temp_right_zone" , AFB_SESSION_NONE, get_temp_right_zone , "Get the right zone temperature"},
{"get_fanspeed" , AFB_SESSION_NONE, get_fanspeed , "Read fan speed"},
@@ -324,7 +340,7 @@ static const struct afb_verb_desc_v1 verbs[]= {
static const struct afb_binding binding_desc = {
.type = AFB_BINDING_VERSION_1,
.v1 = {
- .info = "hvac hybrid service",
+ .info = "hvac service",
.prefix = "hvac",
.verbs = verbs
}