diff options
-rw-r--r-- | hvac-demo-bindings.c | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/hvac-demo-bindings.c b/hvac-demo-bindings.c index b43cfa1..ea09204 100644 --- a/hvac-demo-bindings.c +++ b/hvac-demo-bindings.c @@ -80,6 +80,7 @@ static struct can_handler can_handler = { .socket = -1 }; static int open_can_dev() { #if defined(SIMULATE_HVAC) + DEBUG(interface, "Defining can handler socket to 0 and return"); can_handler.socket = 0; return 0; #else @@ -92,7 +93,7 @@ static int open_can_dev() } else { - // Attemts to open a socket to CAN bus + // Attempts to open a socket to CAN bus strcpy(ifr.ifr_name, CAN_DEV); if(ioctl(can_handler.socket, SIOCGIFINDEX, &ifr) < 0) { @@ -256,6 +257,7 @@ static void get_temp_left_zone(struct afb_req request) */ static void get(struct afb_req request) { + DEBUG(interface, "Getting all values"); json_object *ret_json; ret_json = json_object_new_object(); @@ -280,22 +282,28 @@ static void set(struct afb_req request) uint8_t saves[sizeof can_hvac_components / sizeof *can_hvac_components]; /* records initial values */ + DEBUG(interface, "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); i = (int)(sizeof can_hvac_components / sizeof *can_hvac_components); + DEBUG(interface, "Looping for args. i: %d", i); while (i) { - if (json_object_object_get_ex(query, can_hvac_components[--i], &val)) + i--; + DEBUG(interface, "Searching... query: %s, i: %d, comp: %s", json_object_to_json_string(query), i, can_hvac_components[i]); + if (can_hvac_components[i] != NULL && json_object_object_get_ex(query, can_hvac_components[i], &val)) { - if (!json_object_is_type(json_type_int)) + DEBUG(interface, "We got it. Tests if it is an int or not."); + if (!json_object_is_type(val, json_type_int)) { afb_req_fail_f(request, "bad-request", "argument '%s' isn't integer", can_hvac_components[i]); return; } + DEBUG(interface, "We get an 'int'. Hail for the int: %d", x); x = json_object_get_int(val); if (x < 0 || x > 255) { @@ -305,17 +313,25 @@ static void set(struct afb_req request) } values[i] = (uint8_t)x; } + DEBUG(interface, "Not found !"); } /* attemps to set new values */ - memcpy(can_hvac_values, values, sizeof values); - rc = nchg ? write_can() : 0; - if (rc >= 0) - afb_req_success(request, NULL, nerr ? "error detected" : NULL); + DEBUG(interface, "Diff: %d", memcmp(can_hvac_values, values, sizeof values)); + if (memcmp(can_hvac_values, values, sizeof values) != 0) + { + memcpy(can_hvac_values, values, sizeof values); + rc = write_can(); + if (rc >= 0) + afb_req_success(request, NULL, NULL); + else { + /* restore initial values */ + memcpy(can_hvac_values, saves, sizeof saves); + afb_req_fail(request, "error", "CAN error"); + } + } else { - /* restore initial values */ - memcpy(can_hvac_values, saves, sizeof saves); - afb_req_fail(request, "error", "CAN error"); + afb_req_success(request, NULL, "No changes"); } } @@ -349,4 +365,3 @@ int afbBindingV1ServiceInit(struct afb_service service) { return open_can_dev(); } - |