2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Copyright (C) 2016 Konsulko Group
4 * Author "Romain Forlot"
6 * Author "Scott Murray <scott.murray@konsulko.com>"
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
29 #include <linux/can.h>
32 #include <json-c/json.h>
34 #define AFB_BINDING_VERSION 1
35 #include <afb/afb-binding.h>
37 #define CAN_DEV "vcan0"
39 static const struct afb_binding_interface *interface;
41 /*****************************************************************************************/
42 /*****************************************************************************************/
45 /** SECTION: UTILITY FUNCTIONS **/
48 /*****************************************************************************************/
49 /*****************************************************************************************/
52 * @brief Retry a function 3 times
54 * @param int function(): function that return an int wihtout any parameter
56 * @ return : 0 if ok, -1 if failed
59 static int retry( int(*func)())
74 /*****************************************************************************************/
75 /*****************************************************************************************/
78 /** SECTION: HANDLE CAN DEVICE **/
81 /*****************************************************************************************/
82 /*****************************************************************************************/
84 // Initialize CAN hvac array that will be sent trough the socket
89 { "LeftTemperature", 21 },
90 { "RightTemperature", 21 },
91 { "Temperature", 21 },
99 struct sockaddr_can txAddress;
102 static struct can_handler can_handler = { .socket = -1, .simulation = false, .send_msg = "SENDING CAN FRAME"};
104 static int open_can_dev_helper()
108 AFB_DEBUG(interface, "CAN Handler socket : %d", can_handler.socket);
109 close(can_handler.socket);
111 can_handler.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
112 if (can_handler.socket < 0)
114 AFB_ERROR(interface, "socket could not be created");
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)
122 AFB_ERROR(interface, "ioctl failed");
126 can_handler.txAddress.can_family = AF_CAN;
127 can_handler.txAddress.can_ifindex = ifr.ifr_ifindex;
129 // And bind it to txAddress
130 if (bind(can_handler.socket, (struct sockaddr *)&can_handler.txAddress, sizeof(can_handler.txAddress)) < 0)
132 AFB_ERROR(interface, "bind failed");
138 close(can_handler.socket);
139 can_handler.socket = -1;
144 static int open_can_dev()
146 int rc = retry(open_can_dev_helper);
149 AFB_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";
158 // Get original get temperature function from cpp hvacplugin code
159 static uint8_t to_can_temp(uint8_t value)
161 int result = ((0xF0 - 0x10) / 15) * (value - 15) + 0x10;
167 return (uint8_t)result;
170 static uint8_t read_temp_left_zone()
172 return hvac_values[0].value;
175 static uint8_t read_temp_right_zone()
177 return hvac_values[1].value;
180 static uint8_t read_temp()
182 return (uint8_t)(((int)read_temp_left_zone() + (int)read_temp_right_zone()) >> 1);
185 static uint8_t read_fanspeed()
187 return hvac_values[3].value;
190 static int write_can()
192 struct can_frame txCanFrame;
195 rc = can_handler.socket;
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;
210 AFB_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]);
216 if(!can_handler.simulation)
218 rc = sendto(can_handler.socket, &txCanFrame, sizeof(struct can_frame), 0,
219 (struct sockaddr*)&can_handler.txAddress, sizeof(can_handler.txAddress));
222 AFB_ERROR(interface, "Sending CAN frame failed.");
228 AFB_ERROR(interface, "socket not initialized. Attempt to reopen can device socket.");
234 /*****************************************************************************************/
235 /*****************************************************************************************/
238 /** SECTION: BINDING VERBS IMPLEMENTATION **/
241 /*****************************************************************************************/
242 /*****************************************************************************************/
245 * @brief Get fan speed HVAC system
247 * @param struct afb_req : an afb request structure
250 static void get_fanspeed(struct afb_req request)
252 json_object *ret_json;
253 uint8_t fanspeed = read_fanspeed();
255 ret_json = json_object_new_object();
256 json_object_object_add(ret_json, "FanSpeed", json_object_new_int(fanspeed));
258 afb_req_success(request, ret_json, NULL);
262 * @brief Read Consign right zone temperature for HVAC system
264 * @param struct afb_req : an afb request structure
267 static void get_temp_right_zone(struct afb_req request)
269 json_object *ret_json;
270 uint8_t temp = read_temp_right_zone();
272 ret_json = json_object_new_object();
273 json_object_object_add(ret_json, "RightTemperature", json_object_new_int(temp));
275 afb_req_success(request, ret_json, NULL);
279 * @brief Read Consign left zone temperature for HVAC system
281 * @param struct afb_req : an afb request structure
284 static void get_temp_left_zone(struct afb_req request)
286 json_object *ret_json;
287 uint8_t temp = read_temp_left_zone();
289 ret_json = json_object_new_object();
290 json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(temp));
292 afb_req_success(request, ret_json, NULL);
296 * @brief Read all values
298 * @param struct afb_req : an afb request structure
301 static void get(struct afb_req request)
303 AFB_DEBUG(interface, "Getting all values");
304 json_object *ret_json;
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()));
311 afb_req_success(request, ret_json, NULL);
315 * @brief Set a component value using a json object retrieved from request
317 * @param struct afb_req : an afb request structure
320 static void set(struct afb_req request)
322 int i, rc, x, changed;
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];
328 /* records initial values */
329 AFB_DEBUG(interface, "Records initial values");
330 i = (int)(sizeof hvac_values / sizeof *hvac_values);
333 values[i] = saves[i] = hvac_values[i].value;
336 /* Loop getting arguments */
337 query = afb_req_json(request);
339 i = (int)(sizeof hvac_values / sizeof *hvac_values);
340 AFB_DEBUG(interface, "Looping for args. i: %d", i);
344 AFB_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))
347 AFB_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 AFB_DEBUG(interface, "We get an int: %d",x);
352 else if (json_object_is_type(val, json_type_double)) {
353 d = json_object_get_double(val);
355 AFB_DEBUG(interface, "We get a double: %f => %d",d,x);
358 afb_req_fail_f(request, "bad-request",
359 "argument '%s' isn't integer or double", hvac_values[i].name);
362 if (x < 0 || x > 255)
364 afb_req_fail_f(request, "bad-request",
365 "argument '%s' is out of bounds", hvac_values[i].name);
368 if (values[i] != x) {
369 values[i] = (uint8_t)x;
371 AFB_DEBUG(interface,"%s changed to %d",hvac_values[i].name,x);
375 AFB_DEBUG(interface, "%s not found in query!",hvac_values[i].name);
379 /* attemps to set new values */
380 AFB_DEBUG(interface, "Diff: %d", changed);
383 i = (int)(sizeof hvac_values / sizeof *hvac_values);
386 hvac_values[i].value = values[i];
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);
396 hvac_values[i].value = saves[i];
398 afb_req_fail(request, "error", "CAN error");
402 afb_req_success(request, NULL, "No changes");
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"},
416 static const struct afb_binding binding_desc = {
417 .type = AFB_BINDING_VERSION_1,
419 .info = "hvac service",
425 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
429 return &binding_desc;
432 int afbBindingV1ServiceInit(struct afb_service service)
434 return open_can_dev();