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