diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2016-11-25 17:46:18 +0000 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2016-11-25 17:52:09 +0000 |
commit | 0ead25e3bc380af870ec34381b346771122c9d4e (patch) | |
tree | 1107d31fee9bed6ba874ef1c1f718c45d41b5294 | |
parent | 1d381ff91dc6c9448341259ff14510cb907e1490 (diff) |
Fix: using atoi() to manage char to int
Delete old singles set methods
Change-Id: I2ab290bb3f9346cfa90a9c6b51053e33ba7b40b0
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r-- | hvac-hybrid-qml-binding.c | 114 |
1 files changed, 36 insertions, 78 deletions
diff --git a/hvac-hybrid-qml-binding.c b/hvac-hybrid-qml-binding.c index bceed82..369fb1e 100644 --- a/hvac-hybrid-qml-binding.c +++ b/hvac-hybrid-qml-binding.c @@ -76,7 +76,6 @@ static struct can_handler open_can_dev() ch.socket = -1; ch.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW); - printf("Socket: %i\n", ch.socket); if (ch.socket < 0) { printf("ERROR: socket could not be created\n"); @@ -118,13 +117,23 @@ static void close_can_dev(struct can_handler ch) if(socketId >= 0) { close(socketId); - printf("Socket closed\n"); } } +// 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; + if (result < 0x10) + result = 0x10; + if (result > 0xF0) + result = 0xF0; + + return result; +} + static void write_can(struct can_handler ch) { - printf("Write to CAN bus\n"); // Hardcoded can_id and dlc (data lenght code) struct can_frame txCanFrame; txCanFrame.can_id = 0x30; @@ -132,11 +141,10 @@ static void write_can(struct can_handler ch) if (ch.socket >= 0) { - printf("Send CAN message\n"); - txCanFrame.data[0] = can_hvac_values[0]; - txCanFrame.data[1] = can_hvac_values[1]; - txCanFrame.data[2] = (can_hvac_values[0] + can_hvac_values[1]) & 0x02; + 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]) & 0x02); txCanFrame.data[3] = can_hvac_values[3]; txCanFrame.data[4] = can_hvac_values[4]; txCanFrame.data[5] = can_hvac_values[5]; @@ -170,6 +178,11 @@ static __u8 read_fanspeed() return can_hvac_values[4]; } +static uint8_t make_atoi(const char *str) +{ + return str ? atoi(str) : 0; +} + /***************************************************************************************/ /***************************************************************************************/ /** **/ @@ -209,29 +222,7 @@ static void get_fanspeed(struct afb_req request) 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:%d query=%s", fanspeed, json_object_to_json_string(query)); -} - -/* - * @brief Set fan speed to HVAC system - * - * @param struct afb_req : an afb request structure - * - */ -static void set_fanspeed(struct afb_req request) -{ - struct can_handler ch; - __u8 fanspeed = *afb_req_value(request, "FanSpeed"); - - // Set can_hvac array with the new fan speed value - can_hvac_values[4] = fanspeed; - - ch = open_can_dev(); - write_can(ch); - close_can_dev(ch); - - json_object *query = afb_req_json(request); - afb_req_success_f(request, NULL, "Fanspeed=%d query=%s", can_hvac_values[4], json_object_to_json_string(query)); + afb_req_success_f(request, ret_json, "Fan Speed is:%d query=%s", fanspeed, json_object_to_json_string(query)); } /* @@ -249,7 +240,7 @@ static void get_temp_right_zone(struct afb_req request) 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, "Temperature right zone:%d query=%s", temp, json_object_to_json_string(query)); + afb_req_success_f(request, ret_json, "Right zone Temperature is:%d query=%s", temp, json_object_to_json_string(query)); } /* @@ -267,65 +258,35 @@ static void get_temp_left_zone(struct afb_req request) 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, "Temperature left zone:%d query=%s", temp, json_object_to_json_string(query)); -} - - -/* - * @brief Set right zone temperature to HVAC system - * - * @param struct afb_req : an afb request structure - * - */ -static void set_temp_right_zone(struct afb_req request) -{ - struct can_handler ch; - __u8 tempright = *afb_req_value(request, "RightTemperature"); - - // Set can_hvac array with the new fan speed value - can_hvac_values[1] = tempright; - - ch = open_can_dev(); - write_can(ch); - close_can_dev(ch); - - json_object *query = afb_req_json(request); - afb_req_success_f(request, NULL, "Ping Binder Daemon fanspeed=%d query=%s", can_hvac_values[4], json_object_to_json_string(query)); + afb_req_success_f(request, ret_json, "Left zone Temperature is:%d query=%s", temp, json_object_to_json_string(query)); } /* - * @brief Set left zone temperature to HVAC system + * @brief Set a component value using a json object retrieved from request * * @param struct afb_req : an afb request structure * */ -static void set_temp_left_zone(struct afb_req request) +static void set(struct afb_req request) { struct can_handler ch; - __u8 templeft = *afb_req_value(request, "LeftTemperature"); + const char *comp, *val; - // Set can_hvac array with the new fan speed value - can_hvac_values[0] = templeft; + comp = afb_req_value(request, "component"); + val = afb_req_value(request, "value"); - ch = open_can_dev(); - write_can(ch); - close_can_dev(ch); - - json_object *query = afb_req_json(request); - afb_req_success_f(request, NULL, "Ping Binder Daemon fanspeed=%d query=%s", can_hvac_values[4], json_object_to_json_string(query)); -} - -static void set(struct afb_req request) -{ - struct can_handler ch; - const char *comp = afb_req_value(request, "component"); - __u8 comp_val = *afb_req_value(request, "value"); + if (comp == NULL || val == NULL) + { + afb_req_fail_f(request, "failure", "ERROR: You have to send a json object with attribute 'component' and 'value'.\nSend again please choosing between following components: LeftTemperature, RightTemperature, FanSpeed\ncomponent : %s\n val: %s", comp, val); + return; + } int i; for (i=0;i<8;i++) { if(strcmp(can_hvac_components[i], comp) == 0) { + uint8_t comp_val = make_atoi(val); can_hvac_values[i] = comp_val; break; } @@ -362,16 +323,13 @@ 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"}, - {"set_temp_left_zone" , AFB_SESSION_NONE, set_temp_left_zone , "Set the left zone temperature"}, - {"set_temp_right_zone" , AFB_SESSION_NONE, set_temp_right_zone , "Set the right zone temperature"}, - {"set_fanspeed" , AFB_SESSION_NONE, set_fanspeed , "Set fanspeed"}, {"get_fanspeed" , AFB_SESSION_NONE, get_fanspeed , "Read fan speed"}, {"get_all" , AFB_SESSION_NONE, get_all , "Read all values"}, {"set" , AFB_SESSION_NONE, set , "Set a HVAC component value"}, {NULL} }; -static const struct afb_binding plugin_desc = { +static const struct afb_binding binding_desc = { .type = AFB_BINDING_VERSION_1, .v1 = { .info = "hvac hybrid service", @@ -384,5 +342,5 @@ const struct afb_binding *afbBindingV1Register (const struct afb_binding_interfa { interface = itf; - return &plugin_desc; + return &binding_desc; } |