Fix math for temp
[apps/hvac.git] / binding / hvac-demo-binding.c
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot"
4  * Author "Jose Bolo"
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #define _GNU_SOURCE
19
20 #include <string.h>
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/ioctl.h>
25 #include <net/if.h>
26 #include <linux/can.h>
27 #include <math.h>
28
29 #include <json-c/json.h>
30
31 #include <afb/afb-binding.h>
32 #include <afb/afb-service-itf.h>
33
34 // Uncomment this line to pass into can simulation mode
35 //#define SIMULATE_HVAC
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         struct sockaddr_can txAddress;
98 };
99
100 static struct can_handler can_handler = { .socket = -1 };
101
102 static int open_can_dev()
103 {
104 #if defined(SIMULATE_HVAC)
105         DEBUG(interface, "Defining can handler socket to 0 and return");
106         can_handler.socket = 0;
107         return 0;
108 #else
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 #endif
146 }
147
148 // Get original get temperature function from cpp hvacplugin code
149 static uint8_t to_can_temp(uint8_t value)
150 {
151         int result = ((0xF0 - 0x10) / 15) * (value - 15) + 0x10;
152         if (result < 0x10)
153                 result = 0x10;
154         if (result > 0xF0)
155                 result = 0xF0;
156
157         return (uint8_t)result;
158 }
159
160 static uint8_t read_temp_left_zone()
161 {
162         return hvac_values[0].value;
163 }
164
165 static uint8_t read_temp_right_zone()
166 {
167         return hvac_values[1].value;
168 }
169
170 static uint8_t read_temp()
171 {
172         return (uint8_t)(((int)read_temp_left_zone() + (int)read_temp_right_zone()) >> 1);
173 }
174
175 static uint8_t read_fanspeed()
176 {
177         return hvac_values[3].value ^ 0xFF;
178 }
179
180 static int write_can()
181 {
182         struct can_frame txCanFrame;
183         int rc = 0;
184
185         rc = can_handler.socket;
186         if (rc >= 0)
187         {
188                 // Hardcoded can_id and dlc (data lenght code)
189                 txCanFrame.can_id = 0x30;
190                 txCanFrame.can_dlc = 8;
191                 txCanFrame.data[0] = to_can_temp(read_temp_left_zone());
192                 txCanFrame.data[1] = to_can_temp(read_temp_right_zone());
193                 txCanFrame.data[2] = to_can_temp(read_temp());
194                 txCanFrame.data[3] = 0xf0;
195                 txCanFrame.data[4] = read_fanspeed();
196                 txCanFrame.data[5] = 1;
197                 txCanFrame.data[6] = 0;
198                 txCanFrame.data[7] = 0;
199
200                 DEBUG(interface, "%s: %d %d [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
201 #if defined(SIMULATE_HVAC)
202                         "FAKE CAN FRAME",
203 #else
204                         "SENDING CAN FRAME",
205 #endif
206                         txCanFrame.can_id, txCanFrame.can_dlc,
207                         txCanFrame.data[0], txCanFrame.data[1], txCanFrame.data[2], txCanFrame.data[3],
208                         txCanFrame.data[4], txCanFrame.data[5], txCanFrame.data[6], txCanFrame.data[7]);
209
210 #if !defined(SIMULATE_HVAC)
211                 rc = sendto(can_handler.socket, &txCanFrame, sizeof(struct can_frame), 0,
212                         (struct sockaddr*)&can_handler.txAddress, sizeof(can_handler.txAddress));
213                 if (rc < 0)
214                 {
215                         ERROR(interface, "Sending can frame failed. Attempt to reopen can device socket.");
216                         retry(open_can_dev);
217                 }
218 #endif
219         }
220         else
221         {
222                 ERROR(interface, "socket not initialized. Attempt to reopen can device socket.");
223                 retry(open_can_dev);
224         }
225         return rc;
226 }
227
228 /*****************************************************************************************/
229 /*****************************************************************************************/
230 /**                                                                                     **/
231 /**                                                                                     **/
232 /**        SECTION: BINDING VERBS IMPLEMENTATION                                        **/
233 /**                                                                                     **/
234 /**                                                                                     **/
235 /*****************************************************************************************/
236 /*****************************************************************************************/
237
238 /*
239  * @brief Get fan speed HVAC system
240  *
241  * @param struct afb_req : an afb request structure
242  *
243  */
244 static void get_fanspeed(struct afb_req request)
245 {
246         json_object *ret_json;
247         uint8_t fanspeed = read_fanspeed();
248
249         ret_json = json_object_new_object();
250         json_object_object_add(ret_json, "FanSpeed", json_object_new_int(fanspeed));
251
252         afb_req_success(request, ret_json, NULL);
253 }
254
255 /*
256  * @brief Read Consign right zone temperature for HVAC system
257  *
258  * @param struct afb_req : an afb request structure
259  *
260  */
261 static void get_temp_right_zone(struct afb_req request)
262 {
263         json_object *ret_json;
264         uint8_t temp = read_temp_right_zone();
265
266         ret_json = json_object_new_object();
267         json_object_object_add(ret_json, "RightTemperature", json_object_new_int(temp));
268
269         afb_req_success(request, ret_json, NULL);
270 }
271
272 /*
273  * @brief Read Consign left zone temperature for HVAC system
274  *
275  * @param struct afb_req : an afb request structure
276  *
277  */
278 static void get_temp_left_zone(struct afb_req request)
279 {
280         json_object *ret_json;
281         uint8_t temp = read_temp_left_zone();
282
283         ret_json = json_object_new_object();
284         json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(temp));
285
286         afb_req_success(request, ret_json, NULL);
287 }
288
289 /*
290  * @brief Read all values
291  *
292  * @param struct afb_req : an afb request structure
293  *
294  */
295 static void get(struct afb_req request)
296 {
297         DEBUG(interface, "Getting all values");
298         json_object *ret_json;
299
300         ret_json = json_object_new_object();
301         json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(read_temp_left_zone()));
302         json_object_object_add(ret_json, "RightTemperature", json_object_new_int(read_temp_right_zone()));
303         json_object_object_add(ret_json, "FanSpeed", json_object_new_int(read_fanspeed()));
304
305         afb_req_success(request, ret_json, NULL);
306 }
307
308 /*
309  * @brief Set a component value using a json object retrieved from request
310  *
311  * @param struct afb_req : an afb request structure
312  *
313  */
314 static void set(struct afb_req request)
315 {
316         int i, rc, x, changed;
317         double d;
318         struct json_object *query, *val;
319         uint8_t values[sizeof hvac_values / sizeof *hvac_values];
320         uint8_t saves[sizeof hvac_values / sizeof *hvac_values];
321
322         /* records initial values */
323         DEBUG(interface, "Records initial values");
324         i = (int)(sizeof hvac_values / sizeof *hvac_values);
325         while (i) {
326                 i--;
327                 values[i] = saves[i] = hvac_values[i].value;
328         }
329
330         /* Loop getting arguments */
331         query = afb_req_json(request);
332         changed = 0;
333         i = (int)(sizeof hvac_values / sizeof *hvac_values);
334         DEBUG(interface, "Looping for args. i: %d", i);
335         while (i)
336         {
337                 i--;
338                 DEBUG(interface, "Searching... query: %s, i: %d, comp: %s", json_object_to_json_string(query), i, hvac_values[i].name);
339                 if (json_object_object_get_ex(query, hvac_values[i].name, &val))
340                 {
341                         DEBUG(interface, "We got it. Tests if it is an int or double.");
342                         if (json_object_is_type(val, json_type_int)) {
343                                 x = json_object_get_int(val);
344                                 DEBUG(interface, "We get an int: %d",x);
345                         }
346                         else if (json_object_is_type(val, json_type_double)) {
347                                 d = json_object_get_double(val);
348                                 x = (int)round(d);
349                                 DEBUG(interface, "We get a double: %f => %d",d,x);
350                         }
351                         else {
352                                 afb_req_fail_f(request, "bad-request",
353                                         "argument '%s' isn't integer or double", hvac_values[i].name);
354                                 return;
355                         }
356                         if (x < 0 || x > 255)
357                         {
358                                 afb_req_fail_f(request, "bad-request",
359                                         "argument '%s' is out of bounds", hvac_values[i].name);
360                                 return;
361                         }
362                         if (values[i] != x) {
363                                 values[i] = (uint8_t)x;
364                                 changed = 1;
365                                 DEBUG(interface,"%s changed to %d",hvac_values[i].name,x);
366                         }
367                 }
368                 else {
369                         DEBUG(interface, "%s not found in query!",hvac_values[i].name);
370                 }
371         }
372
373         /* attemps to set new values */
374         DEBUG(interface, "Diff: %d", changed);
375         if (changed)
376         {
377                 i = (int)(sizeof hvac_values / sizeof *hvac_values);
378                 while (i) {
379                         i--;
380                         hvac_values[i].value = values[i];
381                 }
382                 rc = write_can();
383                 if (rc >= 0)
384                         afb_req_success(request, NULL, NULL);
385                 else if (retry(write_can)) {
386                         /* restore initial values */
387                         i = (int)(sizeof hvac_values / sizeof *hvac_values);
388                         while (i) {
389                                 i--;
390                                 hvac_values[i].value = saves[i];
391                         }
392                         afb_req_fail(request, "error", "CAN error");
393                 }
394         }
395         else {
396                 afb_req_success(request, NULL, "No changes");
397         }
398 }
399
400 // TODO: Have to change session management flag to AFB_SESSION_CHECK to use token auth
401 static const struct afb_verb_desc_v1 verbs[]= {
402         {"get_temp_left_zone"    , AFB_SESSION_NONE, get_temp_left_zone , "Get the left zone temperature"},
403         {"get_temp_right_zone"   , AFB_SESSION_NONE, get_temp_right_zone        , "Get the right zone temperature"},
404         {"get_fanspeed"  , AFB_SESSION_NONE, get_fanspeed       , "Read fan speed"},
405         {"get"   , AFB_SESSION_NONE, get        , "Read all values"},
406         {"set"   , AFB_SESSION_NONE, set        , "Set a HVAC component value"},
407         {NULL}
408 };
409
410 static const struct afb_binding binding_desc = {
411         .type = AFB_BINDING_VERSION_1,
412         .v1 = {
413                 .info = "hvac service",
414                 .prefix = "hvac",
415                 .verbs = verbs
416         }
417 };
418
419 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
420 {
421         interface = itf;
422
423         return &binding_desc;
424 }
425
426 int afbBindingV1ServiceInit(struct afb_service service)
427 {
428         return retry(open_can_dev);
429 }