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>
33 #include <json-c/json.h>
35 #define AFB_BINDING_VERSION 2
36 #define RED "/sys/class/leds/blinkm-3-9-red/brightness"
37 #define BLUE "/sys/class/leds/blinkm-3-9-blue/brightness"
38 #define GREEN "/sys/class/leds/blinkm-3-9-green/brightness"
39 #include <afb/afb-binding.h>
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;
137 static struct can_handler can_handler = { .socket = -1, .simulation = false, .send_msg = "SENDING CAN FRAME"};
139 static int open_can_dev_helper()
143 AFB_DEBUG("CAN Handler socket : %d", can_handler.socket);
144 close(can_handler.socket);
146 can_handler.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
147 if (can_handler.socket < 0)
149 AFB_ERROR("ld not created");
153 // Attempts to open a socket to CAN bus
154 strcpy(ifr.ifr_name, CAN_DEV);
155 if(ioctl(can_handler.socket, SIOCGIFINDEX, &ifr) < 0)
161 can_handler.txAddress.can_family = AF_CAN;
162 can_handler.txAddress.can_ifindex = ifr.ifr_ifindex;
164 // And bind it to txAddress
165 if (bind(can_handler.socket, (struct sockaddr *)&can_handler.txAddress, sizeof(can_handler.txAddress)) < 0)
173 close(can_handler.socket);
174 can_handler.socket = -1;
179 static int open_can_dev()
181 int rc = retry(open_can_dev_helper);
184 AFB_ERROR("Interface %s failed. Falling back to simulation mode", CAN_DEV);
185 can_handler.socket = 0;
186 can_handler.simulation = true;
187 can_handler.send_msg = "FAKE CAN FRAME";
193 // Get original get temperature function from cpp hvacplugin code
194 static uint8_t to_can_temp(uint8_t value)
196 int result = ((0xF0 - 0x10) / 15) * (value - 15) + 0x10;
202 return (uint8_t)result;
205 static uint8_t read_temp_left_zone()
207 return hvac_values[0].value;
210 static uint8_t read_temp_right_zone()
212 return hvac_values[1].value;
215 static uint8_t read_temp_left_led()
217 return hvac_values[5].value;
220 static uint8_t read_temp_right_led()
222 return hvac_values[6].value;
225 static uint8_t read_temp()
227 return (uint8_t)(((int)read_temp_left_zone() + (int)read_temp_right_zone()) >> 1);
230 static uint8_t read_fanspeed()
232 return hvac_values[3].value;
236 * @brief Writing to LED for both temperature sliders
239 static int temp_write_led()
241 int rc = 0, red_flag, blue_flag, green_flag, red_value, green_value, blue_value;
245 // /sys/class/leds/blinkm-3-9-red/brightness
246 FILE* r = fopen(RED, "w");
248 AFB_ERROR("Unable to open RED path for writing");
252 // /sys/class/leds/blinkm-3-9-green/brightness
253 FILE* g = fopen(GREEN, "w");
255 AFB_ERROR("Unable to open GREEN path for writing");
259 // /sys/class/leds/blinkm-3-9-blue/brightness
260 FILE* b = fopen(BLUE, "w");
262 AFB_ERROR("Unable to open BLUE path for writing");
266 if (red_flag || green_flag || blue_flag)
271 left_temp = read_temp_left_led() - 15;
272 right_temp = read_temp_right_led() - 15;
274 // Calculates average colour value taken from the temperature toggles
275 red_value = (degree_colours[left_temp].rgb[0] + degree_colours[right_temp].rgb[0]) / 2;
276 green_value = (degree_colours[left_temp].rgb[1] + degree_colours[right_temp].rgb[1]) / 2;
277 blue_value = (degree_colours[left_temp].rgb[2] + degree_colours[right_temp].rgb[2]) / 2;
279 // Writes to LED file the specific colour
280 fprintf(r, "%d", red_value);
281 fprintf(g, "%d", green_value);
282 fprintf(b, "%d", blue_value);
291 * @brief Get temperature of left toggle in HVAC system
293 * @param struct afb_req : an afb request structure
296 static void temp_left_zone_led(struct afb_req request)
298 int i = 5, rc, x, changed;
300 struct json_object *query, *val;
301 uint8_t values[sizeof hvac_values / sizeof *hvac_values];
302 uint8_t saves[sizeof hvac_values / sizeof *hvac_values];
304 AFB_WARNING("In temp_left_zone_led.");
306 query = afb_req_json(request);
308 /* records initial values */
309 AFB_WARNING("Records initial values");
310 values[i] = saves[i] = hvac_values[i].value;
313 if (json_object_object_get_ex(query, hvac_values[i].name, &val))
315 AFB_WARNING("Value of values[i] = %d", values[i]);
316 AFB_WARNING("We got it. Tests if it is an int or double.");
317 if (json_object_is_type(val, json_type_int)) {
318 x = json_object_get_int(val);
319 AFB_WARNING("We get an int: %d",x);
321 else if (json_object_is_type(val, json_type_double)) {
322 d = json_object_get_double(val);
324 AFB_WARNING("We get a double: %f => %d",d,x);
327 afb_req_fail_f(request, "bad-request",
328 "argument '%s' isn't integer or double", hvac_values[i].name);
331 if (x < 0 || x > 255)
333 afb_req_fail_f(request, "bad-request",
334 "argument '%s' is out of bounds", hvac_values[i].name);
337 if (values[i] != x) {
338 values[i] = (uint8_t)x;
340 AFB_WARNING("%s changed to %d", hvac_values[i].name,x);
344 AFB_WARNING("%s not found in query!",hvac_values[i].name);
349 i = 5; //(int)(sizeof hvac_values / sizeof *hvac_values);
350 hvac_values[i].value = values[i]; // update structure at line 102
351 AFB_WARNING("WRITE_LED: value: %d", hvac_values[i].value);
352 rc = temp_write_led();
354 afb_req_success(request, NULL, NULL);
355 else if (retry(temp_write_led)) {
356 /* restore initial values */
357 i = 5; //(int)(sizeof hvac_values / sizeof *hvac_values);
358 hvac_values[i].value = saves[i];
359 afb_req_fail(request, "error", "I2C error");
365 * @brief Get temperature of right toggle in HVAC system
367 * @param struct afb_req : an afb request structure
370 static void temp_right_zone_led(struct afb_req request)
372 int i = 6, rc, x, changed;
374 struct json_object *query, *val;
375 uint8_t values[sizeof hvac_values / sizeof *hvac_values];
376 uint8_t saves[sizeof hvac_values / sizeof *hvac_values];
378 AFB_WARNING("In temp_right_zone_led.");
380 query = afb_req_json(request);
382 /* records initial values */
383 AFB_WARNING("Records initial values");
384 values[i] = saves[i] = hvac_values[i].value;
387 if (json_object_object_get_ex(query, hvac_values[i].name, &val))
389 AFB_WARNING("Value of values[i] = %d", values[i]);
390 AFB_WARNING("We got it. Tests if it is an int or double.");
391 if (json_object_is_type(val, json_type_int)) {
392 x = json_object_get_int(val);
393 AFB_WARNING("We get an int: %d",x);
395 else if (json_object_is_type(val, json_type_double)) {
396 d = json_object_get_double(val);
398 AFB_WARNING("We get a double: %f => %d",d,x);
401 afb_req_fail_f(request, "bad-request",
402 "argument '%s' isn't integer or double", hvac_values[i].name);
405 if (x < 0 || x > 255)
407 afb_req_fail_f(request, "bad-request",
408 "argument '%s' is out of bounds", hvac_values[i].name);
411 if (values[i] != x) {
412 values[i] = (uint8_t)x;
414 AFB_WARNING("%s changed to %d", hvac_values[i].name,x);
418 AFB_WARNING("%s not found in query!", hvac_values[i].name);
423 i = 6; //(int)(sizeof hvac_values / sizeof *hvac_values);
424 hvac_values[i].value = values[i]; // update structure at line 102
425 AFB_WARNING("WRITE_LED: value: %d", hvac_values[i].value);
426 rc = temp_write_led();
428 afb_req_success(request, NULL, NULL);
429 else if (retry(temp_write_led)) {
430 /* restore initial values */
431 i = 6; //(int)(sizeof hvac_values / sizeof *hvac_values);
432 hvac_values[i].value = saves[i];
433 afb_req_fail(request, "error", "I2C error");
438 static int write_can()
440 struct can_frame txCanFrame;
443 rc = can_handler.socket;
446 // Hardcoded can_id and dlc (data lenght code)
447 txCanFrame.can_id = 0x30;
448 txCanFrame.can_dlc = 8;
449 txCanFrame.data[0] = to_can_temp(read_temp_left_zone());
450 txCanFrame.data[1] = to_can_temp(read_temp_right_zone());
451 txCanFrame.data[2] = to_can_temp(read_temp());
452 txCanFrame.data[3] = 0xf0;
453 txCanFrame.data[4] = read_fanspeed();
454 txCanFrame.data[5] = 1;
455 txCanFrame.data[6] = 0;
456 txCanFrame.data[7] = 0;
458 AFB_DEBUG("%s: %d %d [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
459 can_handler.send_msg,
460 txCanFrame.can_id, txCanFrame.can_dlc,
461 txCanFrame.data[0], txCanFrame.data[1], txCanFrame.data[2], txCanFrame.data[3],
462 txCanFrame.data[4], txCanFrame.data[5], txCanFrame.data[6], txCanFrame.data[7]);
464 if(!can_handler.simulation)
466 rc = (int)sendto(can_handler.socket, &txCanFrame, sizeof(struct can_frame), 0,
467 (struct sockaddr*)&can_handler.txAddress, sizeof(can_handler.txAddress));
470 AFB_ERROR("N frame failed.");
476 AFB_ERROR("initialized. Attempt to reopen can device socket.");
482 /*****************************************************************************************/
483 /*****************************************************************************************/
486 /** SECTION: BINDING VERBS IMPLEMENTATION **/
489 /*****************************************************************************************/
490 /*****************************************************************************************/
493 * @brief Get fan speed HVAC system
495 * @param struct afb_req : an afb request structure
498 static void get_fanspeed(struct afb_req request)
500 json_object *ret_json;
501 uint8_t fanspeed = read_fanspeed();
503 ret_json = json_object_new_object();
504 json_object_object_add(ret_json, "FanSpeed", json_object_new_int(fanspeed));
506 afb_req_success(request, ret_json, NULL);
510 * @brief Read Consign right zone temperature for HVAC system
512 * @param struct afb_req : an afb request structure
515 static void get_temp_right_zone(struct afb_req request)
517 json_object *ret_json;
518 uint8_t temp = read_temp_right_zone();
520 ret_json = json_object_new_object();
521 json_object_object_add(ret_json, "RightTemperature", json_object_new_int(temp));
523 afb_req_success(request, ret_json, NULL);
527 * @brief Read Consign left zone temperature for HVAC system
529 * @param struct afb_req : an afb request structure
532 static void get_temp_left_zone(struct afb_req request)
534 json_object *ret_json;
535 uint8_t temp = read_temp_left_zone();
537 ret_json = json_object_new_object();
538 json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(temp));
540 afb_req_success(request, ret_json, NULL);
544 * @brief Read all values
546 * @param struct afb_req : an afb request structure
549 static void get(struct afb_req request)
551 AFB_DEBUG("Getting all values");
552 json_object *ret_json;
554 ret_json = json_object_new_object();
555 json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(read_temp_left_zone()));
556 json_object_object_add(ret_json, "RightTemperature", json_object_new_int(read_temp_right_zone()));
557 json_object_object_add(ret_json, "FanSpeed", json_object_new_int(read_fanspeed()));
559 afb_req_success(request, ret_json, NULL);
563 * @brief Set a component value using a json object retrieved from request
565 * @param struct afb_req : an afb request structure
568 static void set(struct afb_req request)
570 int i, rc, x, changed;
572 struct json_object *query, *val;
573 uint8_t values[sizeof hvac_values / sizeof *hvac_values];
574 uint8_t saves[sizeof hvac_values / sizeof *hvac_values];
576 /* records initial values */
577 AFB_DEBUG("Records initial values");
578 i = (int)(sizeof hvac_values / sizeof *hvac_values);
581 values[i] = saves[i] = hvac_values[i].value;
584 /* Loop getting arguments */
585 query = afb_req_json(request);
587 i = (int)(sizeof hvac_values / sizeof *hvac_values);
588 AFB_DEBUG("Looping for args. i: %d", i);
592 AFB_DEBUG("Searching... query: %s, i: %d, comp: %s", json_object_to_json_string(query), i, hvac_values[i].name);
593 if (json_object_object_get_ex(query, hvac_values[i].name, &val))
595 AFB_DEBUG("We got it. Tests if it is an int or double.");
596 if (json_object_is_type(val, json_type_int)) {
597 x = json_object_get_int(val);
598 AFB_DEBUG("We get an int: %d",x);
600 else if (json_object_is_type(val, json_type_double)) {
601 d = json_object_get_double(val);
603 AFB_DEBUG("We get a double: %f => %d",d,x);
606 afb_req_fail_f(request, "bad-request",
607 "argument '%s' isn't integer or double", hvac_values[i].name);
610 if (x < 0 || x > 255)
612 afb_req_fail_f(request, "bad-request",
613 "argument '%s' is out of bounds", hvac_values[i].name);
616 if (values[i] != x) {
617 values[i] = (uint8_t)x;
619 AFB_DEBUG("%s changed to %d",hvac_values[i].name,x);
623 AFB_DEBUG("%s not found in query!",hvac_values[i].name);
627 /* attemps to set new values */
628 AFB_DEBUG("Diff: %d", changed);
631 i = (int)(sizeof hvac_values / sizeof *hvac_values);
634 hvac_values[i].value = values[i];
638 afb_req_success(request, NULL, NULL);
639 else if (retry(write_can)) {
640 /* restore initial values */
641 i = (int)(sizeof hvac_values / sizeof *hvac_values);
644 hvac_values[i].value = saves[i];
646 afb_req_fail(request, "error", "CAN error");
650 afb_req_success(request, NULL, "No changes");
654 int bindingServicePreInit(struct afb_service service)
656 return open_can_dev();
659 int bindingServiceInit(struct afb_service service)
661 event = afb_daemon_make_event("language");
662 if(afb_daemon_require_api("identity", 1))
664 return afb_service_call_sync("identity", "subscribe", NULL, NULL);
667 void onEvent(const char *event_name, struct json_object *object)
669 json_object *language = json_object_new_object();
670 json_object *id_evt_name, *current_identity;
672 AFB_NOTICE("Event '%s' received: %s", event_name,
673 json_object_to_json_string_ext(object, JSON_C_TO_STRING_PRETTY));
675 if (json_object_object_get_ex(object, "eventName", &id_evt_name) &&
676 !strcmp(json_object_get_string(id_evt_name), "login") &&
677 !afb_service_call_sync("identity", "get", NULL, ¤t_identity)) {
678 json_object *response;
679 if (! json_object_object_get_ex(current_identity, "response", &response) || ! json_object_object_get_ex(response, "graphPreferredLanguage", &language)) {
680 language = json_object_new_string("en_US");
682 afb_event_broadcast(event, language);
686 // TODO: Have to change session management flag to AFB_SESSION_CHECK to use token auth
687 static const struct afb_verb_v2 _afb_verbs_v2_hvac[]= {
689 .verb = "get_temp_left_zone",
690 .callback = get_temp_left_zone,
692 .info = "Get the left zone temperature",
693 .session = AFB_SESSION_NONE_V2
696 .verb = "get_temp_right_zone",
697 .callback = get_temp_right_zone,
699 .info = "Get the right zone temperature",
700 .session = AFB_SESSION_NONE_V2
703 .verb = "get_fanspeed",
704 .callback = get_fanspeed,
706 .info = "Read fan speed",
707 .session = AFB_SESSION_NONE_V2
713 .info = "Read all speed",
714 .session = AFB_SESSION_NONE_V2
720 .info = "Set a HVAC component value",
721 .session = AFB_SESSION_NONE_V2
724 .verb = "temp_left_zone_led",
725 .callback = temp_left_zone_led,
727 .info = "Turn on LED on left temperature zone",
728 .session = AFB_SESSION_NONE_V2
732 .verb = "temp_right_zone_led",
733 .callback = temp_right_zone_led,
735 .info = "Turn on LED on left temperature zone",
736 .session = AFB_SESSION_NONE_V2
748 const struct afb_binding_v2 afbBindingV2 = {
750 .specification = NULL,
751 .info = "HVAC service",
752 .verbs = _afb_verbs_v2_hvac,
753 .preinit = bindingServicePreInit,
754 .init = bindingServiceInit,