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