2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Copyright (C) 2016 Konsulko Group
4 * Author "Romain Forlot"
6 * Author "Scott Murray <scott.murray@konsulko.com>"
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/ioctl.h>
30 #include <linux/can.h>
32 #include <json-c/json.h>
34 #define AFB_BINDING_VERSION 2
35 #include <afb/afb-binding.h>
37 #define RED "/sys/class/leds/blinkm-3-9-red/brightness"
38 #define GREEN "/sys/class/leds/blinkm-3-9-green/brightness"
39 #define BLUE "/sys/class/leds/blinkm-3-9-blue/brightness"
41 #define CAN_DEV "vcan0"
47 static const struct afb_binding_interface *interface;
48 static struct afb_event event;
50 /*****************************************************************************************/
51 /*****************************************************************************************/
54 /** SECTION: UTILITY FUNCTIONS **/
57 /*****************************************************************************************/
58 /*****************************************************************************************/
61 * @brief Retry a function 3 times
63 * @param int function(): function that return an int wihtout any parameter
65 * @ return : 0 if ok, -1 if failed
68 static int retry( int(*func)())
83 /*****************************************************************************************/
84 /*****************************************************************************************/
87 /** SECTION: HANDLE CAN DEVICE **/
90 /*****************************************************************************************/
91 /*****************************************************************************************/
93 // Initialize CAN hvac array that will be sent trough the socket
98 { "LeftTemperature", 21 },
99 { "RightTemperature", 21 },
100 { "Temperature", 21 },
107 // Holds RGB combinations for each temperature
109 const int temperature;
111 } degree_colours[] = {
119 {22, {104, 0, 116} },
120 {23, {116, 0, 102} },
134 struct sockaddr_can txAddress;
141 }led_paths_values = {
147 static struct can_handler can_handler = { .socket = -1, .simulation = false, .send_msg = "SENDING CAN FRAME"};
149 static int open_can_dev_helper()
153 AFB_DEBUG("CAN Handler socket : %d", can_handler.socket);
154 close(can_handler.socket);
156 can_handler.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
157 if (can_handler.socket < 0)
159 AFB_ERROR("socket could not be created");
163 // Attempts to open a socket to CAN bus
164 strcpy(ifr.ifr_name, CAN_DEV);
165 if(ioctl(can_handler.socket, SIOCGIFINDEX, &ifr) < 0)
167 AFB_ERROR("ioctl failed");
171 can_handler.txAddress.can_family = AF_CAN;
172 can_handler.txAddress.can_ifindex = ifr.ifr_ifindex;
174 // And bind it to txAddress
175 if (bind(can_handler.socket, (struct sockaddr *)&can_handler.txAddress, sizeof(can_handler.txAddress)) < 0)
177 AFB_ERROR("bind failed");
183 close(can_handler.socket);
184 can_handler.socket = -1;
189 static int open_can_dev()
191 int rc = retry(open_can_dev_helper);
194 AFB_ERROR("Open of interface %s failed. Falling back to simulation mode", CAN_DEV);
195 can_handler.socket = 0;
196 can_handler.simulation = true;
197 can_handler.send_msg = "FAKE CAN FRAME";
203 // Get original get temperature function from cpp hvacplugin code
204 static uint8_t to_can_temp(uint8_t value)
206 int result = ((0xF0 - 0x10) / 15) * (value - 15) + 0x10;
212 return (uint8_t)result;
215 static uint8_t read_temp_left_zone()
217 return hvac_values[0].value;
220 static uint8_t read_temp_right_zone()
222 return hvac_values[1].value;
225 static uint8_t read_temp_left_led()
227 return hvac_values[5].value;
230 static uint8_t read_temp_right_led()
232 return hvac_values[6].value;
235 static uint8_t read_temp()
237 return (uint8_t)(((int)read_temp_left_zone() + (int)read_temp_right_zone()) >> 1);
240 static uint8_t read_fanspeed()
242 return hvac_values[3].value;
248 * @brief: Parse JSON configuration file for blinkm path
250 static int parse_config()
252 struct json_object *ledtemp = NULL;
253 struct json_object *jobj = json_object_from_file("/etc/hvac.json");
255 // Check if .json file has been parsed as a json_object
257 AFB_ERROR("JSON file could not be opened!\n");
261 // Check if json_object with key "ledtemp" has been found in .json file
262 if (!json_object_object_get_ex(jobj, "ledtemp", &ledtemp)){
263 AFB_ERROR("Key not found!\n");
267 // Extract directory paths for each LED colour
268 json_object_object_foreach(ledtemp, key, value) {
269 if (strcmp(key, "red") == 0) {
270 led_paths_values.red = json_object_get_string(value);
271 } else if (strcmp(key, "green") == 0) {
272 led_paths_values.green = json_object_get_string(value);
273 } else if (strcmp(key, "blue") == 0) {
274 led_paths_values.blue = json_object_get_string(value);
278 // return 0 if all succeeded
284 * @brief Writing to LED for both temperature sliders
286 static int temp_write_led()
289 int rc = -1, red_value, green_value, blue_value;
293 left_temp = read_temp_left_led() - 15;
294 right_temp = read_temp_right_led() - 15;
296 // Calculates average colour value taken from the temperature toggles
297 red_value = (degree_colours[left_temp].rgb[0] + degree_colours[right_temp].rgb[0]) / 2;
298 green_value = (degree_colours[left_temp].rgb[1] + degree_colours[right_temp].rgb[1]) / 2;
299 blue_value = (degree_colours[left_temp].rgb[2] + degree_colours[right_temp].rgb[2]) / 2;
301 // default path: /sys/class/leds/blinkm-3-9-red/brightness
302 FILE* r = fopen(led_paths_values.red, "w");
304 fprintf(r, "%d", red_value);
307 AFB_ERROR("Unable to open red LED path!\n");
311 // default path: /sys/class/leds/blinkm-3-9-green/brightness
312 FILE* g = fopen(led_paths_values.green, "w");
314 fprintf(g, "%d", green_value);
317 AFB_ERROR("Unable to open green LED path!\n");
321 // default path: /sys/class/leds/blinkm-3-9-blue/brightness
322 FILE* b = fopen(led_paths_values.blue, "w");
324 fprintf(b, "%d", blue_value);
327 AFB_ERROR("Unable to open blue LED path!\n");
335 * @brief Get temperature of left toggle in HVAC system
337 * @param struct afb_req : an afb request structure
340 static void temp_left_zone_led(struct afb_req request)
342 int i = 5, rc, x, changed;
344 struct json_object *query, *val;
345 uint8_t values[sizeof hvac_values / sizeof *hvac_values];
346 uint8_t saves[sizeof hvac_values / sizeof *hvac_values];
348 AFB_WARNING("In temp_left_zone_led.");
350 query = afb_req_json(request);
352 /* records initial values */
353 AFB_WARNING("Records initial values");
354 values[i] = saves[i] = hvac_values[i].value;
357 if (json_object_object_get_ex(query, hvac_values[i].name, &val))
359 AFB_WARNING("Value of values[i] = %d", values[i]);
360 AFB_WARNING("We got it. Tests if it is an int or double.");
361 if (json_object_is_type(val, json_type_int)) {
362 x = json_object_get_int(val);
363 AFB_WARNING("We get an int: %d",x);
365 else if (json_object_is_type(val, json_type_double)) {
366 d = json_object_get_double(val);
368 AFB_WARNING("We get a double: %f => %d",d,x);
371 afb_req_fail_f(request, "bad-request",
372 "argument '%s' isn't integer or double", hvac_values[i].name);
375 if (x < 0 || x > 255)
377 afb_req_fail_f(request, "bad-request",
378 "argument '%s' is out of bounds", hvac_values[i].name);
381 if (values[i] != x) {
382 values[i] = (uint8_t)x;
384 AFB_WARNING("%s changed to %d", hvac_values[i].name,x);
388 AFB_WARNING("%s not found in query!",hvac_values[i].name);
393 hvac_values[i].value = values[i]; // update structure at line 102
394 AFB_WARNING("WRITE_LED: value: %d", hvac_values[i].value);
395 rc = temp_write_led();
397 afb_req_success(request, NULL, NULL);
398 else if (retry(temp_write_led)) {
399 /* restore initial values */
400 hvac_values[i].value = saves[i];
401 afb_req_fail(request, "error", "I2C error");
407 * @brief Get temperature of right toggle in HVAC system
409 * @param struct afb_req : an afb request structure
412 static void temp_right_zone_led(struct afb_req request)
414 int i = 6, rc, x, changed;
416 struct json_object *query, *val;
417 uint8_t values[sizeof hvac_values / sizeof *hvac_values];
418 uint8_t saves[sizeof hvac_values / sizeof *hvac_values];
420 AFB_WARNING("In temp_right_zone_led.");
422 query = afb_req_json(request);
424 /* records initial values */
425 AFB_WARNING("Records initial values");
426 values[i] = saves[i] = hvac_values[i].value;
429 if (json_object_object_get_ex(query, hvac_values[i].name, &val))
431 AFB_WARNING("Value of values[i] = %d", values[i]);
432 AFB_WARNING("We got it. Tests if it is an int or double.");
433 if (json_object_is_type(val, json_type_int)) {
434 x = json_object_get_int(val);
435 AFB_WARNING("We get an int: %d",x);
437 else if (json_object_is_type(val, json_type_double)) {
438 d = json_object_get_double(val);
440 AFB_WARNING("We get a double: %f => %d",d,x);
443 afb_req_fail_f(request, "bad-request",
444 "argument '%s' isn't integer or double", hvac_values[i].name);
447 if (x < 0 || x > 255)
449 afb_req_fail_f(request, "bad-request",
450 "argument '%s' is out of bounds", hvac_values[i].name);
453 if (values[i] != x) {
454 values[i] = (uint8_t)x;
456 AFB_WARNING("%s changed to %d", hvac_values[i].name,x);
460 AFB_WARNING("%s not found in query!", hvac_values[i].name);
465 hvac_values[i].value = values[i]; // update structure at line 102
466 AFB_WARNING("WRITE_LED: value: %d", hvac_values[i].value);
467 rc = temp_write_led();
469 afb_req_success(request, NULL, NULL);
470 else if (retry(temp_write_led)) {
471 /* restore initial values */
472 hvac_values[i].value = saves[i];
473 afb_req_fail(request, "error", "I2C error");
478 static int write_can()
480 struct can_frame txCanFrame;
483 rc = can_handler.socket;
486 // Hardcoded can_id and dlc (data lenght code)
487 txCanFrame.can_id = 0x30;
488 txCanFrame.can_dlc = 8;
489 txCanFrame.data[0] = to_can_temp(read_temp_left_zone());
490 txCanFrame.data[1] = to_can_temp(read_temp_right_zone());
491 txCanFrame.data[2] = to_can_temp(read_temp());
492 txCanFrame.data[3] = 0xf0;
493 txCanFrame.data[4] = read_fanspeed();
494 txCanFrame.data[5] = 1;
495 txCanFrame.data[6] = 0;
496 txCanFrame.data[7] = 0;
498 AFB_DEBUG("%s: %d %d [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
499 can_handler.send_msg,
500 txCanFrame.can_id, txCanFrame.can_dlc,
501 txCanFrame.data[0], txCanFrame.data[1], txCanFrame.data[2], txCanFrame.data[3],
502 txCanFrame.data[4], txCanFrame.data[5], txCanFrame.data[6], txCanFrame.data[7]);
504 if(!can_handler.simulation)
506 rc = (int)sendto(can_handler.socket, &txCanFrame, sizeof(struct can_frame), 0,
507 (struct sockaddr*)&can_handler.txAddress, sizeof(can_handler.txAddress));
510 AFB_ERROR("Sending CAN frame failed.");
516 AFB_ERROR("socket not initialized. Attempt to reopen can device socket.");
522 /*****************************************************************************************/
523 /*****************************************************************************************/
526 /** SECTION: BINDING VERBS IMPLEMENTATION **/
529 /*****************************************************************************************/
530 /*****************************************************************************************/
533 * @brief Get fan speed HVAC system
535 * @param struct afb_req : an afb request structure
538 static void get_fanspeed(struct afb_req request)
540 json_object *ret_json;
541 uint8_t fanspeed = read_fanspeed();
543 ret_json = json_object_new_object();
544 json_object_object_add(ret_json, "FanSpeed", json_object_new_int(fanspeed));
546 afb_req_success(request, ret_json, NULL);
550 * @brief Read Consign right zone temperature for HVAC system
552 * @param struct afb_req : an afb request structure
555 static void get_temp_right_zone(struct afb_req request)
557 json_object *ret_json;
558 uint8_t temp = read_temp_right_zone();
560 ret_json = json_object_new_object();
561 json_object_object_add(ret_json, "RightTemperature", json_object_new_int(temp));
563 afb_req_success(request, ret_json, NULL);
567 * @brief Read Consign left zone temperature for HVAC system
569 * @param struct afb_req : an afb request structure
572 static void get_temp_left_zone(struct afb_req request)
574 json_object *ret_json;
575 uint8_t temp = read_temp_left_zone();
577 ret_json = json_object_new_object();
578 json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(temp));
580 afb_req_success(request, ret_json, NULL);
584 * @brief Read all values
586 * @param struct afb_req : an afb request structure
589 static void get(struct afb_req request)
591 AFB_DEBUG("Getting all values");
592 json_object *ret_json;
594 ret_json = json_object_new_object();
595 json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(read_temp_left_zone()));
596 json_object_object_add(ret_json, "RightTemperature", json_object_new_int(read_temp_right_zone()));
597 json_object_object_add(ret_json, "FanSpeed", json_object_new_int(read_fanspeed()));
599 afb_req_success(request, ret_json, NULL);
603 * @brief Set a component value using a json object retrieved from request
605 * @param struct afb_req : an afb request structure
608 static void set(struct afb_req request)
610 int i, rc, x, changed;
612 struct json_object *query, *val;
613 uint8_t values[sizeof hvac_values / sizeof *hvac_values];
614 uint8_t saves[sizeof hvac_values / sizeof *hvac_values];
616 /* records initial values */
617 AFB_DEBUG("Records initial values");
618 i = (int)(sizeof hvac_values / sizeof *hvac_values);
621 values[i] = saves[i] = hvac_values[i].value;
624 /* Loop getting arguments */
625 query = afb_req_json(request);
627 i = (int)(sizeof hvac_values / sizeof *hvac_values);
628 AFB_DEBUG("Looping for args. i: %d", i);
632 AFB_DEBUG("Searching... query: %s, i: %d, comp: %s", json_object_to_json_string(query), i, hvac_values[i].name);
633 if (json_object_object_get_ex(query, hvac_values[i].name, &val))
635 AFB_DEBUG("We got it. Tests if it is an int or double.");
636 if (json_object_is_type(val, json_type_int)) {
637 x = json_object_get_int(val);
638 AFB_DEBUG("We get an int: %d",x);
640 else if (json_object_is_type(val, json_type_double)) {
641 d = json_object_get_double(val);
643 AFB_DEBUG("We get a double: %f => %d",d,x);
646 afb_req_fail_f(request, "bad-request",
647 "argument '%s' isn't integer or double", hvac_values[i].name);
650 if (x < 0 || x > 255)
652 afb_req_fail_f(request, "bad-request",
653 "argument '%s' is out of bounds", hvac_values[i].name);
656 if (values[i] != x) {
657 values[i] = (uint8_t)x;
659 AFB_DEBUG("%s changed to %d",hvac_values[i].name,x);
663 AFB_DEBUG("%s not found in query!",hvac_values[i].name);
667 /* attemps to set new values */
668 AFB_DEBUG("Diff: %d", changed);
671 i = (int)(sizeof hvac_values / sizeof *hvac_values);
674 hvac_values[i].value = values[i];
678 afb_req_success(request, NULL, NULL);
679 else if (retry(write_can)) {
680 /* restore initial values */
681 i = (int)(sizeof hvac_values / sizeof *hvac_values);
684 hvac_values[i].value = saves[i];
686 afb_req_fail(request, "error", "CAN error");
690 afb_req_success(request, NULL, "No changes");
694 int bindingServicePreInit(struct afb_service service)
696 return open_can_dev();
699 int bindingServiceInit(struct afb_service service)
701 event = afb_daemon_make_event("language");
702 if(parse_config() != 0)
703 AFB_WARNING("Default values are being used!\n");
704 if(afb_daemon_require_api("identity", 1))
706 return afb_service_call_sync("identity", "subscribe", NULL, NULL);
709 void onEvent(const char *event_name, struct json_object *object)
711 json_object *args, *language = json_object_new_object();
712 json_object *id_evt_name, *current_identity;
714 AFB_NOTICE("Event '%s' received: %s", event_name,
715 json_object_to_json_string_ext(object, JSON_C_TO_STRING_PRETTY));
717 if (json_object_object_get_ex(object, "eventName", &id_evt_name) &&
718 !strcmp(json_object_get_string(id_evt_name), "login") &&
719 !afb_service_call_sync("identity", "get", NULL, ¤t_identity)) {
720 json_object *response;
721 if (! json_object_object_get_ex(current_identity, "response", &response) || ! json_object_object_get_ex(response, "graphPreferredLanguage", &language)) {
722 language = json_object_new_string("en_US");
724 afb_event_broadcast(event, language);
728 // TODO: Have to change session management flag to AFB_SESSION_CHECK to use token auth
729 static const struct afb_verb_v2 _afb_verbs_v2_hvac[]= {
731 .verb = "get_temp_left_zone",
732 .callback = get_temp_left_zone,
734 .info = "Get the left zone temperature",
735 .session = AFB_SESSION_NONE_V2
738 .verb = "get_temp_right_zone",
739 .callback = get_temp_right_zone,
741 .info = "Get the right zone temperature",
742 .session = AFB_SESSION_NONE_V2
745 .verb = "get_fanspeed",
746 .callback = get_fanspeed,
748 .info = "Read fan speed",
749 .session = AFB_SESSION_NONE_V2
755 .info = "Read all speed",
756 .session = AFB_SESSION_NONE_V2
762 .info = "Set a HVAC component value",
763 .session = AFB_SESSION_NONE_V2
766 .verb = "temp_left_zone_led",
767 .callback = temp_left_zone_led,
769 .info = "Turn on LED on left temperature zone",
770 .session = AFB_SESSION_NONE_V2
773 .verb = "temp_right_zone_led",
774 .callback = temp_right_zone_led,
776 .info = "Turn on LED on left temperature zone",
777 .session = AFB_SESSION_NONE_V2
789 const struct afb_binding_v2 afbBindingV2 = {
791 .specification = NULL,
792 .info = "HVAC service",
793 .verbs = _afb_verbs_v2_hvac,
794 .preinit = bindingServicePreInit,
795 .init = bindingServiceInit,