A/C blinks i2c LED
[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 #include <afb/afb-binding.h>
36 #include <afb/afb-service-itf.h>
37
38 #define CAN_DEV "vcan0"
39 #define LED_BRIGHTNESS "/sys/class/leds/blinkm-3-9-red/brightness"
40
41 static const struct afb_binding_interface *interface;
42
43 /*****************************************************************************************/
44 /*****************************************************************************************/
45 /**                                                                                     **/
46 /**                                                                                     **/
47 /**        SECTION: UTILITY FUNCTIONS                                                   **/
48 /**                                                                                     **/
49 /**                                                                                     **/
50 /*****************************************************************************************/
51 /*****************************************************************************************/
52
53 /*
54  * @brief Retry a function 3 times
55  *
56  * @param int function(): function that return an int wihtout any parameter
57  *
58  * @ return : 0 if ok, -1 if failed
59  *
60  */
61 static int retry( int(*func)())
62 {
63         int i;
64
65         for (i=0;i<4;i++)
66         {
67                 if ( (*func)() >= 0)
68                 {
69                         return 0;
70                 }
71                 usleep(100000);
72         }
73         return -1;
74 }
75
76 /*****************************************************************************************/
77 /*****************************************************************************************/
78 /**                                                                                     **/
79 /**                                                                                     **/
80 /**        SECTION: HANDLE CAN DEVICE                                                   **/
81 /**                                                                                     **/
82 /**                                                                                     **/
83 /*****************************************************************************************/
84 /*****************************************************************************************/
85
86 // Initialize CAN hvac array that will be sent trough the socket
87 static struct {
88         const char *name;
89         uint8_t value;
90 } hvac_values[] = {
91         { "LeftTemperature", 21 },
92         { "RightTemperature", 21 },
93         { "Temperature", 21 },
94         { "FanSpeed", 0 },
95         { "ACEnabled", 0 }
96 };
97
98 struct can_handler {
99         int socket;
100         bool simulation;
101         char *send_msg;
102         struct sockaddr_can txAddress;
103 };
104
105 static struct can_handler can_handler = { .socket = -1, .simulation = false, .send_msg = "SENDING CAN FRAME"};
106
107 static int open_can_dev_helper()
108 {
109         struct ifreq ifr;
110
111         DEBUG(interface, "CAN Handler socket : %d", can_handler.socket);
112         close(can_handler.socket);
113
114         can_handler.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
115         if (can_handler.socket < 0)
116         {
117                 ERROR(interface, "socket could not be created");
118         }
119         else
120         {
121                 // Attempts to open a socket to CAN bus
122                 strcpy(ifr.ifr_name, CAN_DEV);
123                 if(ioctl(can_handler.socket, SIOCGIFINDEX, &ifr) < 0)
124                 {
125                         ERROR(interface, "ioctl failed");
126                 }
127                 else
128                 {
129                         can_handler.txAddress.can_family = AF_CAN;
130                         can_handler.txAddress.can_ifindex = ifr.ifr_ifindex;
131
132                         // And bind it to txAddress
133                         if (bind(can_handler.socket, (struct sockaddr *)&can_handler.txAddress, sizeof(can_handler.txAddress)) < 0)
134                         {
135                                 ERROR(interface, "bind failed");
136                         }
137                         else {
138                                 return 0;
139                         }
140                 }
141                 close(can_handler.socket);
142                 can_handler.socket = -1;
143         }
144         return -1;
145 }
146
147 static int open_can_dev()
148 {
149         int rc = retry(open_can_dev_helper);
150         if(rc < 0)
151         {
152                 ERROR(interface, "Open of interface %s failed. Falling back to simulation mode", CAN_DEV);
153                 can_handler.socket = 0;
154                 can_handler.simulation = true;
155                 can_handler.send_msg = "FAKE CAN FRAME";
156                 rc = 0;
157         }
158         return rc;
159 }
160
161 // Get original get temperature function from cpp hvacplugin code
162 static uint8_t to_can_temp(uint8_t value)
163 {
164         int result = ((0xF0 - 0x10) / 15) * (value - 15) + 0x10;
165         if (result < 0x10)
166                 result = 0x10;
167         if (result > 0xF0)
168                 result = 0xF0;
169
170         return (uint8_t)result;
171 }
172
173 static uint8_t read_temp_left_zone()
174 {
175         return hvac_values[0].value;
176 }
177
178 static uint8_t read_temp_right_zone()
179 {
180         return hvac_values[1].value;
181 }
182
183 static uint8_t read_temp()
184 {
185         return (uint8_t)(((int)read_temp_left_zone() + (int)read_temp_right_zone()) >> 1);
186 }
187
188 static uint8_t read_fanspeed()
189 {
190         return hvac_values[3].value;
191 }
192
193 static uint8_t read_acenabled()
194 {
195         WARNING(interface, "in read_acenabled: value= %d", hvac_values[4].value);
196         return hvac_values[4].value;
197 }
198
199 static int write_led()
200 {
201     int rc = 0;
202
203     // /sys/class/leds/blinkm-3-9-red/brightness
204     FILE* f = fopen(LED_BRIGHTNESS, "w");
205     if (f == NULL) {
206         ERROR(interface, "Unable to open path for writing");
207         rc=1;
208     }
209
210     fprintf(f, "%d", read_acenabled());
211     fclose(f);
212     return rc;
213 }
214
215 static int write_can()
216 {
217         struct can_frame txCanFrame;
218         int rc = 0;
219
220         rc = can_handler.socket;
221         if (rc >= 0)
222         {
223                 // Hardcoded can_id and dlc (data lenght code)
224                 txCanFrame.can_id = 0x30;
225                 txCanFrame.can_dlc = 8;
226                 txCanFrame.data[0] = to_can_temp(read_temp_left_zone());
227                 txCanFrame.data[1] = to_can_temp(read_temp_right_zone());
228                 txCanFrame.data[2] = to_can_temp(read_temp());
229                 txCanFrame.data[3] = 0xf0;
230                 txCanFrame.data[4] = read_fanspeed();
231                 txCanFrame.data[5] = 1;
232                 txCanFrame.data[6] = 0;
233                 txCanFrame.data[7] = 0;
234
235                 DEBUG(interface, "%s: %d %d [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
236                         can_handler.send_msg,
237                         txCanFrame.can_id, txCanFrame.can_dlc,
238                         txCanFrame.data[0], txCanFrame.data[1], txCanFrame.data[2], txCanFrame.data[3],
239                         txCanFrame.data[4], txCanFrame.data[5], txCanFrame.data[6], txCanFrame.data[7]);
240
241                 if(!can_handler.simulation)
242                 {
243                         rc = sendto(can_handler.socket, &txCanFrame, sizeof(struct can_frame), 0,
244                                     (struct sockaddr*)&can_handler.txAddress, sizeof(can_handler.txAddress));
245                         if (rc < 0)
246                         {
247                                 ERROR(interface, "Sending CAN frame failed.");
248                         }
249                 }
250         }
251         else
252         {
253                 ERROR(interface, "socket not initialized. Attempt to reopen can device socket.");
254                 open_can_dev();
255         }
256         return rc;
257 }
258
259 /*****************************************************************************************/
260 /*****************************************************************************************/
261 /**                                                                                     **/
262 /**                                                                                     **/
263 /**        SECTION: BINDING VERBS IMPLEMENTATION                                        **/
264 /**                                                                                     **/
265 /**                                                                                     **/
266 /*****************************************************************************************/
267 /*****************************************************************************************/
268
269 /*
270  * @brief Get fan speed HVAC system
271  *
272  * @param struct afb_req : an afb request structure
273  *
274  */
275 static void get_acenabled(struct afb_req request)
276 {
277         json_object *ret_json;
278
279         uint8_t acenabled = read_acenabled();
280         WARNING(interface, "in get_acenabled: acenabled= %d", acenabled);
281
282         ret_json = json_object_new_object();
283         json_object_object_add(ret_json, "ACEnabled", json_object_new_int(acenabled));
284
285         afb_req_success(request, ret_json, NULL);
286 }
287
288
289 /*
290  * @brief Get fan speed HVAC system
291  *
292  * @param struct afb_req : an afb request structure
293  *
294  */
295 static void get_fanspeed(struct afb_req request)
296 {
297         json_object *ret_json;
298         uint8_t fanspeed = read_fanspeed();
299
300         ret_json = json_object_new_object();
301         json_object_object_add(ret_json, "FanSpeed", json_object_new_int(fanspeed));
302
303         afb_req_success(request, ret_json, NULL);
304 }
305
306 /*
307  * @brief Read Consign right zone temperature for HVAC system
308  *
309  * @param struct afb_req : an afb request structure
310  *
311  */
312 static void get_temp_right_zone(struct afb_req request)
313 {
314         json_object *ret_json;
315         uint8_t temp = read_temp_right_zone();
316
317         ret_json = json_object_new_object();
318         json_object_object_add(ret_json, "RightTemperature", json_object_new_int(temp));
319
320         afb_req_success(request, ret_json, NULL);
321 }
322
323 /*
324  * @brief Read Consign left zone temperature for HVAC system
325  *
326  * @param struct afb_req : an afb request structure
327  *
328  */
329 static void get_temp_left_zone(struct afb_req request)
330 {
331         json_object *ret_json;
332         uint8_t temp = read_temp_left_zone();
333
334         ret_json = json_object_new_object();
335         json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(temp));
336
337         afb_req_success(request, ret_json, NULL);
338 }
339
340 /*
341  * @brief Read all values
342  *
343  * @param struct afb_req : an afb request structure
344  *
345  */
346 static void get(struct afb_req request)
347 {
348         DEBUG(interface, "Getting all values");
349         json_object *ret_json;
350
351         ret_json = json_object_new_object();
352         json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(read_temp_left_zone()));
353         json_object_object_add(ret_json, "RightTemperature", json_object_new_int(read_temp_right_zone()));
354         json_object_object_add(ret_json, "FanSpeed", json_object_new_int(read_fanspeed()));
355         json_object_object_add(ret_json, "ACEnabled", json_object_new_int(read_acenabled()));
356
357         afb_req_success(request, ret_json, NULL);
358 }
359
360 static void set_acenabled(struct afb_req request)
361 {
362         int i=4, rc, x, changed;
363         double d;
364         struct json_object *query, *val;
365         uint8_t values[sizeof hvac_values / sizeof *hvac_values];
366         uint8_t saves[sizeof hvac_values / sizeof *hvac_values];
367
368         WARNING(interface, "in set_acenabled.");
369
370         query = afb_req_json(request);
371
372         /* records initial values */
373         WARNING(interface, "Records initial values");
374         values[i] = saves[i] = hvac_values[i].value;
375
376
377         if (json_object_object_get_ex(query, hvac_values[i].name, &val))
378         {
379                 WARNING(interface, "We got it. Tests if it is an int or double.");
380                 if (json_object_is_type(val, json_type_int)) {
381                         x = json_object_get_int(val);
382                         WARNING(interface, "We get an int: %d",x);
383                 }
384                 else if (json_object_is_type(val, json_type_double)) {
385                         d = json_object_get_double(val);
386                         x = (int)round(d);
387                         WARNING(interface, "We get a double: %f => %d",d,x);
388                 }
389                 else {
390                         afb_req_fail_f(request, "bad-request",
391                                 "argument '%s' isn't integer or double", hvac_values[i].name);
392                         return;
393                 }
394                 if (x < 0 || x > 255)
395                 {
396                         afb_req_fail_f(request, "bad-request",
397                                 "argument '%s' is out of bounds", hvac_values[i].name);
398                         return;
399                 }
400                 if (values[i] != x) {
401                         values[i] = (uint8_t)x;
402                         changed = 1;
403                         WARNING(interface,"%s changed to %d",hvac_values[i].name,x);
404                 }
405         }
406         else {
407                 WARNING(interface, "%s not found in query!",hvac_values[i].name);
408         }
409
410         if (changed)
411         {
412                 i = 4; //(int)(sizeof hvac_values / sizeof *hvac_values);
413                 hvac_values[i].value = values[i];
414                 WARNING(interface, "WRITE_LED: value: %d ",hvac_values[i].value);
415                 rc = write_led();
416                 if (rc >= 0)
417                         afb_req_success(request, NULL, NULL);
418                 else if (retry(write_led)) {
419                         /* restore initial values */
420                         i = 4; //(int)(sizeof hvac_values / sizeof *hvac_values);
421                         hvac_values[i].value = saves[i];
422                         afb_req_fail(request, "error", "I2C error");
423                 }
424         }
425         else {
426                 afb_req_success(request, NULL, "No changes");
427         }
428
429
430 }
431
432 /*
433  * @brief Set a component value using a json object retrieved from request
434  *
435  * @param struct afb_req : an afb request structure
436  *
437  */
438 static void set(struct afb_req request)
439 {
440         int i, rc, x, changed;
441         double d;
442         struct json_object *query, *val;
443         uint8_t values[sizeof hvac_values / sizeof *hvac_values];
444         uint8_t saves[sizeof hvac_values / sizeof *hvac_values];
445
446         /* records initial values */
447         DEBUG(interface, "Records initial values");
448         i = (int)(sizeof hvac_values / sizeof *hvac_values);
449         while (i) {
450                 i--;
451                 values[i] = saves[i] = hvac_values[i].value;
452         }
453
454         /* Loop getting arguments */
455         query = afb_req_json(request);
456         changed = 0;
457         i = (int)(sizeof hvac_values / sizeof *hvac_values);
458         DEBUG(interface, "Looping for args. i: %d", i);
459         while (i)
460         {
461                 i--;
462                 DEBUG(interface, "Searching... query: %s, i: %d, comp: %s", json_object_to_json_string(query), i, hvac_values[i].name);
463                 if (json_object_object_get_ex(query, hvac_values[i].name, &val))
464                 {
465                         DEBUG(interface, "We got it. Tests if it is an int or double.");
466                         if (json_object_is_type(val, json_type_int)) {
467                                 x = json_object_get_int(val);
468                                 DEBUG(interface, "We get an int: %d",x);
469                         }
470                         else if (json_object_is_type(val, json_type_double)) {
471                                 d = json_object_get_double(val);
472                                 x = (int)round(d);
473                                 DEBUG(interface, "We get a double: %f => %d",d,x);
474                         }
475                         else {
476                                 afb_req_fail_f(request, "bad-request",
477                                         "argument '%s' isn't integer or double", hvac_values[i].name);
478                                 return;
479                         }
480                         if (x < 0 || x > 255)
481                         {
482                                 afb_req_fail_f(request, "bad-request",
483                                         "argument '%s' is out of bounds", hvac_values[i].name);
484                                 return;
485                         }
486                         if (values[i] != x) {
487                                 values[i] = (uint8_t)x;
488                                 changed = 1;
489                                 DEBUG(interface,"%s changed to %d",hvac_values[i].name,x);
490                         }
491                 }
492                 else {
493                         DEBUG(interface, "%s not found in query!",hvac_values[i].name);
494                 }
495         }
496
497         /* attemps to set new values */
498         DEBUG(interface, "Diff: %d", changed);
499         if (changed)
500         {
501                 i = (int)(sizeof hvac_values / sizeof *hvac_values);
502                 while (i) {
503                         i--;
504                         hvac_values[i].value = values[i];
505                 }
506                 rc = write_can();
507                 if (rc >= 0)
508                         afb_req_success(request, NULL, NULL);
509                 else if (retry(write_can)) {
510                         /* restore initial values */
511                         i = (int)(sizeof hvac_values / sizeof *hvac_values);
512                         while (i) {
513                                 i--;
514                                 hvac_values[i].value = saves[i];
515                         }
516                         afb_req_fail(request, "error", "CAN error");
517                 }
518         }
519         else {
520                 afb_req_success(request, NULL, "No changes");
521         }
522 }
523
524 // TODO: Have to change session management flag to AFB_SESSION_CHECK to use token auth
525 static const struct afb_verb_desc_v1 verbs[]= {
526         {"get_temp_left_zone"    , AFB_SESSION_NONE, get_temp_left_zone , "Get the left zone temperature"},
527         {"get_temp_right_zone"   , AFB_SESSION_NONE, get_temp_right_zone        , "Get the right zone temperature"},
528         {"get_fanspeed"  , AFB_SESSION_NONE, get_fanspeed       , "Read fan speed"},
529         {"get_acenabled"         , AFB_SESSION_NONE, get_acenabled      , "Read fan speed"},
530         {"get"   , AFB_SESSION_NONE, get        , "Read all values"},
531         {"set"   , AFB_SESSION_NONE, set        , "Set a HVAC component value"},
532         {"set_acenabled"         , AFB_SESSION_NONE, set_acenabled      , "Set a HVAC component value"},
533         {NULL}
534 };
535
536 static const struct afb_binding binding_desc = {
537         .type = AFB_BINDING_VERSION_1,
538         .v1 = {
539                 .info = "hvac service",
540                 .prefix = "hvac",
541                 .verbs = verbs
542         }
543 };
544
545 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
546 {
547         interface = itf;
548
549         return &binding_desc;
550 }
551
552 int afbBindingV1ServiceInit(struct afb_service service)
553 {
554         return open_can_dev();
555 }