cf8e7fbc407da16ee6d3b4e76e7ff2748948132e
[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-24-9-red/brightness"
37 #define BLUE "/sys/class/leds/blinkm-24-9-blue/brightness"
38 #define GREEN "/sys/class/leds/blinkm-24-9-green/brightness"
39 #include <afb/afb-binding.h>
40
41 #define CAN_DEV "sllin0"
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-24-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-24-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-24-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             usleep(100000);
467             rc = (int)sendto(can_handler.socket, &txCanFrame, sizeof(struct can_frame), 0,
468                     (struct sockaddr*)&can_handler.txAddress, sizeof(can_handler.txAddress));
469             if (rc < 0)
470             {
471                 AFB_ERROR("N frame failed.");
472             }
473         }
474     }
475     else
476     {
477         AFB_ERROR("initialized. Attempt to reopen can device socket.");
478         open_can_dev();
479     }
480     return rc;
481 }
482
483 /*****************************************************************************************/
484 /*****************************************************************************************/
485 /**                                         **/
486 /**                                         **/
487 /**    SECTION: BINDING VERBS IMPLEMENTATION                    **/
488 /**                                         **/
489 /**                                         **/
490 /*****************************************************************************************/
491 /*****************************************************************************************/
492
493 /*
494  * @brief Get fan speed HVAC system
495  *
496  * @param struct afb_req : an afb request structure
497  *
498  */
499 static void get_fanspeed(struct afb_req request)
500 {
501     json_object *ret_json;
502     uint8_t fanspeed = read_fanspeed();
503
504     ret_json = json_object_new_object();
505     json_object_object_add(ret_json, "FanSpeed", json_object_new_int(fanspeed));
506
507     afb_req_success(request, ret_json, NULL);
508 }
509
510 /*
511  * @brief Read Consign right zone temperature for HVAC system
512  *
513  * @param struct afb_req : an afb request structure
514  *
515  */
516 static void get_temp_right_zone(struct afb_req request)
517 {
518     json_object *ret_json;
519     uint8_t temp = read_temp_right_zone();
520
521     ret_json = json_object_new_object();
522     json_object_object_add(ret_json, "RightTemperature", json_object_new_int(temp));
523
524     afb_req_success(request, ret_json, NULL);
525 }
526
527 /*
528  * @brief Read Consign left zone temperature for HVAC system
529  *
530  * @param struct afb_req : an afb request structure
531  *
532  */
533 static void get_temp_left_zone(struct afb_req request)
534 {
535     json_object *ret_json;
536     uint8_t temp = read_temp_left_zone();
537
538     ret_json = json_object_new_object();
539     json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(temp));
540
541     afb_req_success(request, ret_json, NULL);
542 }
543
544 /*
545  * @brief Read all values
546  *
547  * @param struct afb_req : an afb request structure
548  *
549  */
550 static void get(struct afb_req request)
551 {
552     AFB_DEBUG("Getting all values");
553     json_object *ret_json;
554
555     ret_json = json_object_new_object();
556     json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(read_temp_left_zone()));
557     json_object_object_add(ret_json, "RightTemperature", json_object_new_int(read_temp_right_zone()));
558     json_object_object_add(ret_json, "FanSpeed", json_object_new_int(read_fanspeed()));
559
560     afb_req_success(request, ret_json, NULL);
561 }
562
563 /*
564  * @brief Set a component value using a json object retrieved from request
565  *
566  * @param struct afb_req : an afb request structure
567  *
568  */
569 static void set(struct afb_req request)
570 {
571     int i, rc, x, changed;
572     double d;
573     struct json_object *query, *val;
574     uint8_t values[sizeof hvac_values / sizeof *hvac_values];
575     uint8_t saves[sizeof hvac_values / sizeof *hvac_values];
576
577     /* records initial values */
578     AFB_DEBUG("Records initial values");
579     i = (int)(sizeof hvac_values / sizeof *hvac_values);
580     while (i) {
581         i--;
582         values[i] = saves[i] = hvac_values[i].value;
583     }
584
585     /* Loop getting arguments */
586     query = afb_req_json(request);
587     changed = 0;
588     i = (int)(sizeof hvac_values / sizeof *hvac_values);
589     AFB_DEBUG("Looping for args. i: %d", i);
590     while (i)
591     {
592         i--;
593         AFB_DEBUG("Searching... query: %s, i: %d, comp: %s", json_object_to_json_string(query), i, hvac_values[i].name);
594         if (json_object_object_get_ex(query, hvac_values[i].name, &val))
595         {
596             AFB_DEBUG("We got it. Tests if it is an int or double.");
597             if (json_object_is_type(val, json_type_int)) {
598                 x = json_object_get_int(val);
599                 AFB_DEBUG("We get an int: %d",x);
600             }
601             else if (json_object_is_type(val, json_type_double)) {
602                 d = json_object_get_double(val);
603                 x = (int)round(d);
604                 AFB_DEBUG("We get a double: %f => %d",d,x);
605             }
606             else {
607                 afb_req_fail_f(request, "bad-request",
608                     "argument '%s' isn't integer or double", hvac_values[i].name);
609                 return;
610             }
611             if (x < 0 || x > 255)
612             {
613                 afb_req_fail_f(request, "bad-request",
614                     "argument '%s' is out of bounds", hvac_values[i].name);
615                 return;
616             }
617             if (values[i] != x) {
618                 values[i] = (uint8_t)x;
619                 changed = 1;
620                 AFB_DEBUG("%s changed to %d",hvac_values[i].name,x);
621             }
622         }
623         else {
624             AFB_DEBUG("%s not found in query!",hvac_values[i].name);
625         }
626     }
627
628     /* attemps to set new values */
629     AFB_DEBUG("Diff: %d", changed);
630     if (changed)
631     {
632         i = (int)(sizeof hvac_values / sizeof *hvac_values);
633         while (i) {
634             i--;
635             hvac_values[i].value = values[i];
636         }
637         rc = write_can();
638         if (rc >= 0)
639             afb_req_success(request, NULL, NULL);
640         else if (retry(write_can)) {
641             /* restore initial values */
642             i = (int)(sizeof hvac_values / sizeof *hvac_values);
643             while (i) {
644                 i--;
645                 hvac_values[i].value = saves[i];
646             }
647             afb_req_fail(request, "error", "CAN error");
648         }
649     }
650     else {
651         afb_req_success(request, NULL, "No changes");
652     }
653 }
654
655 int bindingServicePreInit(struct afb_service service)
656 {
657     return open_can_dev();
658 }
659
660 int bindingServiceInit(struct afb_service service)
661 {
662     event = afb_daemon_make_event("language");
663     if(afb_daemon_require_api("identity", 1))
664         return -1;
665     return afb_service_call_sync("identity", "subscribe", NULL, NULL);
666 }
667
668 void onEvent(const char *event_name, struct json_object *object)
669 {
670     json_object *language = json_object_new_object();
671     json_object *id_evt_name, *current_identity;
672
673     AFB_NOTICE("Event '%s' received: %s", event_name,
674         json_object_to_json_string_ext(object, JSON_C_TO_STRING_PRETTY));
675
676     if (json_object_object_get_ex(object, "eventName", &id_evt_name) &&
677       !strcmp(json_object_get_string(id_evt_name), "login") &&
678       !afb_service_call_sync("identity", "get", NULL, &current_identity)) {
679         json_object *response;
680         if (! json_object_object_get_ex(current_identity, "response", &response) || ! json_object_object_get_ex(response, "graphPreferredLanguage", &language)) {
681             language = json_object_new_string("en_US");
682         }
683         afb_event_broadcast(event, language);
684     }
685 }
686
687 // TODO: Have to change session management flag to AFB_SESSION_CHECK to use token auth
688 static const struct afb_verb_v2 _afb_verbs_v2_hvac[]= {
689     {
690         .verb = "get_temp_left_zone",
691         .callback = get_temp_left_zone,
692         .auth = NULL,
693         .info = "Get the left zone temperature",
694         .session = AFB_SESSION_NONE_V2
695     },
696     {
697         .verb = "get_temp_right_zone",
698         .callback = get_temp_right_zone,
699         .auth = NULL,
700         .info = "Get the right zone temperature",
701         .session = AFB_SESSION_NONE_V2
702     },
703     {
704         .verb = "get_fanspeed",
705         .callback = get_fanspeed,
706         .auth = NULL,
707         .info = "Read fan speed",
708         .session = AFB_SESSION_NONE_V2
709     },
710     {
711         .verb = "get",
712         .callback = get,
713         .auth = NULL,
714         .info = "Read all speed",
715         .session = AFB_SESSION_NONE_V2
716     },
717     {
718         .verb = "set",
719         .callback = set,
720         .auth = NULL,
721         .info = "Set a HVAC component value",
722         .session = AFB_SESSION_NONE_V2
723     },
724     {
725         .verb = "temp_left_zone_led",
726         .callback = temp_left_zone_led,
727         .auth = NULL,
728         .info = "Turn on LED on left temperature zone",
729         .session = AFB_SESSION_NONE_V2
730
731     },
732         {
733         .verb = "temp_right_zone_led",
734         .callback = temp_right_zone_led,
735         .auth = NULL,
736         .info = "Turn on LED on left temperature zone",
737         .session = AFB_SESSION_NONE_V2
738
739     },
740     {
741         .verb = NULL,
742         .callback = NULL,
743         .auth = NULL,
744         .info = NULL,
745         .session = 0
746     }
747 };
748
749 const struct afb_binding_v2 afbBindingV2 = {
750     .api = "hvac",
751     .specification = NULL,
752     .info = "HVAC service",
753     .verbs = _afb_verbs_v2_hvac,
754     .preinit = bindingServicePreInit,
755     .init = bindingServiceInit,
756     .onevent = onEvent,
757     .noconcurrency = 0
758 };