4ac8d6eba8ad9786f698623149c00c4c352e7203
[apps/hvac.git] / binding / hvac-demo-binding.c
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Copyright (C) 2016 Konsulko Group
4  * Author "Romain Forlot"
5  * Author "Jose Bolo"
6  * Author "Scott Murray <scott.murray@konsulko.com>"
7  *
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
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20 #define _GNU_SOURCE
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdbool.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/ioctl.h>
29 #include <net/if.h>
30 #include <linux/can.h>
31 #include <math.h>
32
33 #include <json-c/json.h>
34
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>
40
41 #define CAN_DEV "vcan0"
42
43 #ifndef NULL
44 #define NULL 0
45 #endif
46
47 static const struct afb_binding_interface *interface;
48 static struct afb_event event;
49
50 /*****************************************************************************************/
51 /*****************************************************************************************/
52 /**                                         **/
53 /**                                         **/
54 /**    SECTION: UTILITY FUNCTIONS                           **/
55 /**                                         **/
56 /**                                         **/
57 /*****************************************************************************************/
58 /*****************************************************************************************/
59
60 /*
61  * @brief Retry a function 3 times
62  *
63  * @param int function(): function that return an int wihtout any parameter
64  *
65  * @ return : 0 if ok, -1 if failed
66  *
67  */
68 static int retry( int(*func)())
69 {
70     int i;
71
72     for (i=0;i<4;i++)
73     {
74         if ( (*func)() >= 0)
75         {
76             return 0;
77         }
78         usleep(100000);
79     }
80     return -1;
81 }
82
83 /*****************************************************************************************/
84 /*****************************************************************************************/
85 /**                                         **/
86 /**                                         **/
87 /**    SECTION: HANDLE CAN DEVICE                           **/
88 /**                                         **/
89 /**                                         **/
90 /*****************************************************************************************/
91 /*****************************************************************************************/
92
93 // Initialize CAN hvac array that will be sent trough the socket
94 static struct {
95     const char *name;
96     uint8_t value;
97 } hvac_values[] = {
98     { "LeftTemperature", 21 },
99     { "RightTemperature", 21 },
100     { "Temperature", 21 },
101     { "FanSpeed", 0 },
102     { "ACEnabled", 0 },
103     { "LeftLed", 15 },
104     { "RightLed", 15 }
105 };
106
107 // Holds RGB combinations for each temperature
108 static struct {
109     const int temperature;
110     const int rgb[3];
111 } degree_colours[] = {
112     {15, {0, 0, 229} },
113     {16, {22, 0, 204} },
114     {17, {34, 0, 189} },
115     {18, {46, 0, 175} },
116     {19, {58, 0, 186} },
117     {20, {70, 0, 146} },
118     {21, {82, 0, 131} },
119     {22, {104, 0, 116} },
120     {23, {116, 0, 102} },
121     {24, {128, 0, 87} },
122     {25, {140, 0, 73} },
123     {26, {152, 0, 58} },
124     {27, {164, 0, 43} },
125     {28, {176, 0, 29} },
126     {29, {188, 0, 14} },
127     {30, {201, 0, 5} }
128 };
129
130 struct can_handler {
131     int socket;
132     bool simulation;
133     char *send_msg;
134     struct sockaddr_can txAddress;
135 };
136
137 static struct can_handler can_handler = { .socket = -1, .simulation = false, .send_msg = "SENDING CAN FRAME"};
138
139 static int open_can_dev_helper()
140 {
141     struct ifreq ifr;
142     
143     AFB_DEBUG("CAN Handler socket : %d", can_handler.socket);
144     close(can_handler.socket);
145
146     can_handler.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
147     if (can_handler.socket < 0)
148     {
149         AFB_ERROR("ld not created");
150     }
151     else
152     {
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)
156         {
157             AFB_ERROR("ed");
158         }
159         else
160         {
161             can_handler.txAddress.can_family = AF_CAN;
162             can_handler.txAddress.can_ifindex = ifr.ifr_ifindex;
163
164             // And bind it to txAddress
165             if (bind(can_handler.socket, (struct sockaddr *)&can_handler.txAddress, sizeof(can_handler.txAddress)) < 0)
166             {
167                 AFB_ERROR("d");
168             }
169             else {
170                 return 0;
171             }
172         }
173         close(can_handler.socket);
174         can_handler.socket = -1;
175     }
176     return -1;
177 }
178
179 static int open_can_dev()
180 {
181     int rc = retry(open_can_dev_helper);
182     if(rc < 0)
183     {
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";
188         rc = 0;
189     }
190     return rc;
191 }
192
193 // Get original get temperature function from cpp hvacplugin code
194 static uint8_t to_can_temp(uint8_t value)
195 {
196     int result = ((0xF0 - 0x10) / 15) * (value - 15) + 0x10;
197     if (result < 0x10)
198         result = 0x10;
199     if (result > 0xF0)
200         result = 0xF0;
201
202     return (uint8_t)result;
203 }
204
205 static uint8_t read_temp_left_zone()
206 {
207     return hvac_values[0].value;
208 }
209
210 static uint8_t read_temp_right_zone()
211 {
212     return hvac_values[1].value;
213 }
214
215 static uint8_t read_temp_left_led()
216 {
217     return hvac_values[5].value;
218 }
219
220 static uint8_t read_temp_right_led()
221 {
222     return hvac_values[6].value;
223 }
224
225 static uint8_t read_temp()
226 {
227     return (uint8_t)(((int)read_temp_left_zone() + (int)read_temp_right_zone()) >> 1);
228 }
229
230 static uint8_t read_fanspeed()
231 {
232     return hvac_values[3].value;
233 }
234
235 /* 
236  * @brief Writing to LED for both temperature sliders
237  */
238
239 static int temp_write_led()
240 {
241     int rc = 0, red_flag, blue_flag, green_flag, red_value, green_value, blue_value;
242     int right_temp;
243     int left_temp;
244
245     // /sys/class/leds/blinkm-3-9-red/brightness
246     FILE* r = fopen(RED, "w");
247     if (r == NULL) {
248         AFB_ERROR("Unable to open RED path for writing");
249         red_flag = 1;
250     }
251
252     // /sys/class/leds/blinkm-3-9-green/brightness
253     FILE* g = fopen(GREEN, "w");
254     if (g == NULL) {
255         AFB_ERROR("Unable to open GREEN path for writing");
256         green_flag = 1;
257     }
258
259     // /sys/class/leds/blinkm-3-9-blue/brightness
260     FILE* b = fopen(BLUE, "w");
261     if (b == NULL) {
262         AFB_ERROR("Unable to open BLUE path for writing");
263         blue_flag = 1;
264     }
265
266     if (red_flag || green_flag || blue_flag)
267     {
268         rc = 1;
269     }
270
271     left_temp = read_temp_left_led() - 15;
272     right_temp = read_temp_right_led() - 15;
273
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;
278
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);
283     fclose(r);
284     fclose(g);
285     fclose(b);
286     return rc;
287
288 }
289
290 /*
291  * @brief Get temperature of left toggle in HVAC system
292  *
293  * @param struct afb_req : an afb request structure
294  *
295  */
296 static void temp_left_zone_led(struct afb_req request)
297 {
298     int i = 5, rc, x, changed;
299     double d;
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];
303
304     AFB_WARNING("In temp_left_zone_led.");
305
306     query = afb_req_json(request);
307
308     /* records initial values */
309     AFB_WARNING("Records initial values");
310     values[i] = saves[i] = hvac_values[i].value;
311
312
313     if (json_object_object_get_ex(query, hvac_values[i].name, &val))
314     {
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);
320         }
321         else if (json_object_is_type(val, json_type_double)) {
322             d = json_object_get_double(val);
323             x = (int)round(d);
324             AFB_WARNING("We get a double: %f => %d",d,x);
325         }
326         else {
327             afb_req_fail_f(request, "bad-request",
328                 "argument '%s' isn't integer or double", hvac_values[i].name);
329             return;
330         }
331         if (x < 0 || x > 255)
332         {
333             afb_req_fail_f(request, "bad-request",
334                 "argument '%s' is out of bounds", hvac_values[i].name);
335             return;
336         }
337         if (values[i] != x) {
338             values[i] = (uint8_t)x;
339             changed = 1;
340             AFB_WARNING("%s changed to %d", hvac_values[i].name,x);
341         }
342     }
343     else {
344         AFB_WARNING("%s not found in query!",hvac_values[i].name);
345     }
346
347
348     if (changed) {
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();
353         if (rc >= 0)
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");
360         }
361     }
362 }
363
364 /*
365  * @brief Get temperature of right toggle in HVAC system
366  *
367  * @param struct afb_req : an afb request structure
368  *
369  */
370 static void temp_right_zone_led(struct afb_req request)
371 {
372     int i = 6, rc, x, changed;
373     double d;
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];
377
378     AFB_WARNING("In temp_right_zone_led.");
379
380     query = afb_req_json(request);
381
382     /* records initial values */
383     AFB_WARNING("Records initial values");
384     values[i] = saves[i] = hvac_values[i].value;
385
386
387     if (json_object_object_get_ex(query, hvac_values[i].name, &val))
388     {
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);
394         }
395         else if (json_object_is_type(val, json_type_double)) {
396             d = json_object_get_double(val);
397             x = (int)round(d);
398             AFB_WARNING("We get a double: %f => %d",d,x);
399         }
400         else {
401             afb_req_fail_f(request, "bad-request",
402                 "argument '%s' isn't integer or double", hvac_values[i].name);
403             return;
404         }
405         if (x < 0 || x > 255)
406         {
407             afb_req_fail_f(request, "bad-request",
408                 "argument '%s' is out of bounds", hvac_values[i].name);
409             return;
410         }
411         if (values[i] != x) {
412             values[i] = (uint8_t)x;
413             changed = 1;
414             AFB_WARNING("%s changed to %d", hvac_values[i].name,x);
415         }
416     }
417     else {
418         AFB_WARNING("%s not found in query!", hvac_values[i].name);
419     }
420
421
422     if (changed) {
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();
427         if (rc >= 0)
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");
434         }
435     }
436 }
437
438 static int write_can()
439 {
440     struct can_frame txCanFrame;
441     int rc = 0;
442
443     rc = can_handler.socket;
444     if (rc >= 0)
445     {
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;
457
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]);
463
464         if(!can_handler.simulation)
465         {
466             rc = (int)sendto(can_handler.socket, &txCanFrame, sizeof(struct can_frame), 0,
467                     (struct sockaddr*)&can_handler.txAddress, sizeof(can_handler.txAddress));
468             if (rc < 0)
469             {
470                 AFB_ERROR("N frame failed.");
471             }
472         }
473     }
474     else
475     {
476         AFB_ERROR("initialized. Attempt to reopen can device socket.");
477         open_can_dev();
478     }
479     return rc;
480 }
481
482 /*****************************************************************************************/
483 /*****************************************************************************************/
484 /**                                         **/
485 /**                                         **/
486 /**    SECTION: BINDING VERBS IMPLEMENTATION                    **/
487 /**                                         **/
488 /**                                         **/
489 /*****************************************************************************************/
490 /*****************************************************************************************/
491
492 /*
493  * @brief Get fan speed HVAC system
494  *
495  * @param struct afb_req : an afb request structure
496  *
497  */
498 static void get_fanspeed(struct afb_req request)
499 {
500     json_object *ret_json;
501     uint8_t fanspeed = read_fanspeed();
502
503     ret_json = json_object_new_object();
504     json_object_object_add(ret_json, "FanSpeed", json_object_new_int(fanspeed));
505
506     afb_req_success(request, ret_json, NULL);
507 }
508
509 /*
510  * @brief Read Consign right zone temperature for HVAC system
511  *
512  * @param struct afb_req : an afb request structure
513  *
514  */
515 static void get_temp_right_zone(struct afb_req request)
516 {
517     json_object *ret_json;
518     uint8_t temp = read_temp_right_zone();
519
520     ret_json = json_object_new_object();
521     json_object_object_add(ret_json, "RightTemperature", json_object_new_int(temp));
522
523     afb_req_success(request, ret_json, NULL);
524 }
525
526 /*
527  * @brief Read Consign left zone temperature for HVAC system
528  *
529  * @param struct afb_req : an afb request structure
530  *
531  */
532 static void get_temp_left_zone(struct afb_req request)
533 {
534     json_object *ret_json;
535     uint8_t temp = read_temp_left_zone();
536
537     ret_json = json_object_new_object();
538     json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(temp));
539
540     afb_req_success(request, ret_json, NULL);
541 }
542
543 /*
544  * @brief Read all values
545  *
546  * @param struct afb_req : an afb request structure
547  *
548  */
549 static void get(struct afb_req request)
550 {
551     AFB_DEBUG("Getting all values");
552     json_object *ret_json;
553
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()));
558
559     afb_req_success(request, ret_json, NULL);
560 }
561
562 /*
563  * @brief Set a component value using a json object retrieved from request
564  *
565  * @param struct afb_req : an afb request structure
566  *
567  */
568 static void set(struct afb_req request)
569 {
570     int i, rc, x, changed;
571     double d;
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];
575
576     /* records initial values */
577     AFB_DEBUG("Records initial values");
578     i = (int)(sizeof hvac_values / sizeof *hvac_values);
579     while (i) {
580         i--;
581         values[i] = saves[i] = hvac_values[i].value;
582     }
583
584     /* Loop getting arguments */
585     query = afb_req_json(request);
586     changed = 0;
587     i = (int)(sizeof hvac_values / sizeof *hvac_values);
588     AFB_DEBUG("Looping for args. i: %d", i);
589     while (i)
590     {
591         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))
594         {
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);
599             }
600             else if (json_object_is_type(val, json_type_double)) {
601                 d = json_object_get_double(val);
602                 x = (int)round(d);
603                 AFB_DEBUG("We get a double: %f => %d",d,x);
604             }
605             else {
606                 afb_req_fail_f(request, "bad-request",
607                     "argument '%s' isn't integer or double", hvac_values[i].name);
608                 return;
609             }
610             if (x < 0 || x > 255)
611             {
612                 afb_req_fail_f(request, "bad-request",
613                     "argument '%s' is out of bounds", hvac_values[i].name);
614                 return;
615             }
616             if (values[i] != x) {
617                 values[i] = (uint8_t)x;
618                 changed = 1;
619                 AFB_DEBUG("%s changed to %d",hvac_values[i].name,x);
620             }
621         }
622         else {
623             AFB_DEBUG("%s not found in query!",hvac_values[i].name);
624         }
625     }
626
627     /* attemps to set new values */
628     AFB_DEBUG("Diff: %d", changed);
629     if (changed)
630     {
631         i = (int)(sizeof hvac_values / sizeof *hvac_values);
632         while (i) {
633             i--;
634             hvac_values[i].value = values[i];
635         }
636         rc = write_can();
637         if (rc >= 0)
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);
642             while (i) {
643                 i--;
644                 hvac_values[i].value = saves[i];
645             }
646             afb_req_fail(request, "error", "CAN error");
647         }
648     }
649     else {
650         afb_req_success(request, NULL, "No changes");
651     }
652 }
653
654 int bindingServicePreInit(struct afb_service service)
655 {
656     return open_can_dev();
657 }
658
659 int bindingServiceInit(struct afb_service service)
660 {
661     event = afb_daemon_make_event("language");
662     if(afb_daemon_require_api("identity", 1))
663         return -1;
664     return afb_service_call_sync("identity", "subscribe", NULL, NULL);
665 }
666
667 void onEvent(const char *event_name, struct json_object *object)
668 {
669     json_object *language = json_object_new_object();
670     json_object *id_evt_name, *current_identity;
671
672     AFB_NOTICE("Event '%s' received: %s", event_name,
673         json_object_to_json_string_ext(object, JSON_C_TO_STRING_PRETTY));
674
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, &current_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");
681         }
682         afb_event_broadcast(event, language);
683     }
684 }
685
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[]= {
688     {
689         .verb = "get_temp_left_zone",
690         .callback = get_temp_left_zone,
691         .auth = NULL,
692         .info = "Get the left zone temperature",
693         .session = AFB_SESSION_NONE_V2
694     },
695     {
696         .verb = "get_temp_right_zone",
697         .callback = get_temp_right_zone,
698         .auth = NULL,
699         .info = "Get the right zone temperature",
700         .session = AFB_SESSION_NONE_V2
701     },
702     {
703         .verb = "get_fanspeed",
704         .callback = get_fanspeed,
705         .auth = NULL,
706         .info = "Read fan speed",
707         .session = AFB_SESSION_NONE_V2
708     },
709     {
710         .verb = "get",
711         .callback = get,
712         .auth = NULL,
713         .info = "Read all speed",
714         .session = AFB_SESSION_NONE_V2
715     },
716     {
717         .verb = "set",
718         .callback = set,
719         .auth = NULL,
720         .info = "Set a HVAC component value",
721         .session = AFB_SESSION_NONE_V2
722     },
723     {
724         .verb = "temp_left_zone_led",
725         .callback = temp_left_zone_led,
726         .auth = NULL,
727         .info = "Turn on LED on left temperature zone",
728         .session = AFB_SESSION_NONE_V2
729
730     },
731         {
732         .verb = "temp_right_zone_led",
733         .callback = temp_right_zone_led,
734         .auth = NULL,
735         .info = "Turn on LED on left temperature zone",
736         .session = AFB_SESSION_NONE_V2
737
738     },
739     {
740         .verb = NULL,
741         .callback = NULL,
742         .auth = NULL,
743         .info = NULL,
744         .session = 0
745     }
746 };
747
748 const struct afb_binding_v2 afbBindingV2 = {
749     .api = "hvac",
750     .specification = NULL,
751     .info = "HVAC service",
752     .verbs = _afb_verbs_v2_hvac,
753     .preinit = bindingServicePreInit,
754     .init = bindingServiceInit,
755     .onevent = onEvent,
756     .noconcurrency = 0
757 };