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