2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Romain Forlot"
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/ioctl.h>
26 #include <linux/can.h>
29 #include <json-c/json.h>
31 #include <afb/afb-binding.h>
32 #include <afb/afb-service-itf.h>
34 // Uncomment this line to pass into can simulation mode
35 //#define SIMULATE_HVAC
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 },
97 struct sockaddr_can txAddress;
100 static struct can_handler can_handler = { .socket = -1 };
102 static int open_can_dev()
104 #if defined(SIMULATE_HVAC)
105 DEBUG(interface, "Defining can handler socket to 0 and return");
106 can_handler.socket = 0;
111 DEBUG(interface, "CAN Handler socket : %d", can_handler.socket);
112 close(can_handler.socket);
114 can_handler.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
115 if (can_handler.socket < 0)
117 ERROR(interface, "socket could not be created");
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)
125 ERROR(interface, "ioctl failed");
129 can_handler.txAddress.can_family = AF_CAN;
130 can_handler.txAddress.can_ifindex = ifr.ifr_ifindex;
132 // And bind it to txAddress
133 if (bind(can_handler.socket, (struct sockaddr *)&can_handler.txAddress, sizeof(can_handler.txAddress)) < 0)
135 ERROR(interface, "bind failed");
141 close(can_handler.socket);
142 can_handler.socket = -1;
148 // Get original get temperature function from cpp hvacplugin code
149 static uint8_t to_can_temp(uint8_t value)
151 int result = ((0xF0 - 0x10) / 15) * (value - 15) + 0x10;
157 return (uint8_t)result;
160 static uint8_t read_temp_left_zone()
162 return hvac_values[0].value;
165 static uint8_t read_temp_right_zone()
167 return hvac_values[1].value;
170 static uint8_t read_temp()
172 return (uint8_t)(((int)read_temp_left_zone() + (int)read_temp_right_zone()) >> 1);
175 static uint8_t read_fanspeed()
177 return hvac_values[3].value ^ 0xFF;
180 static int write_can()
182 struct can_frame txCanFrame;
185 rc = can_handler.socket;
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;
200 DEBUG(interface, "%s: %d %d [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
201 #if defined(SIMULATE_HVAC)
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]);
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));
215 ERROR(interface, "Sending can frame failed. Attempt to reopen can device socket.");
222 ERROR(interface, "socket not initialized. Attempt to reopen can device socket.");
228 /*****************************************************************************************/
229 /*****************************************************************************************/
232 /** SECTION: BINDING VERBS IMPLEMENTATION **/
235 /*****************************************************************************************/
236 /*****************************************************************************************/
239 * @brief Get fan speed HVAC system
241 * @param struct afb_req : an afb request structure
244 static void get_fanspeed(struct afb_req request)
246 json_object *ret_json;
247 uint8_t fanspeed = read_fanspeed();
249 ret_json = json_object_new_object();
250 json_object_object_add(ret_json, "FanSpeed", json_object_new_int(fanspeed));
252 afb_req_success(request, ret_json, NULL);
256 * @brief Read Consign right zone temperature for HVAC system
258 * @param struct afb_req : an afb request structure
261 static void get_temp_right_zone(struct afb_req request)
263 json_object *ret_json;
264 uint8_t temp = read_temp_right_zone();
266 ret_json = json_object_new_object();
267 json_object_object_add(ret_json, "RightTemperature", json_object_new_int(temp));
269 afb_req_success(request, ret_json, NULL);
273 * @brief Read Consign left zone temperature for HVAC system
275 * @param struct afb_req : an afb request structure
278 static void get_temp_left_zone(struct afb_req request)
280 json_object *ret_json;
281 uint8_t temp = read_temp_left_zone();
283 ret_json = json_object_new_object();
284 json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(temp));
286 afb_req_success(request, ret_json, NULL);
290 * @brief Read all values
292 * @param struct afb_req : an afb request structure
295 static void get(struct afb_req request)
297 DEBUG(interface, "Getting all values");
298 json_object *ret_json;
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()));
305 afb_req_success(request, ret_json, NULL);
309 * @brief Set a component value using a json object retrieved from request
311 * @param struct afb_req : an afb request structure
314 static void set(struct afb_req request)
316 int i, rc, x, changed;
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];
322 /* records initial values */
323 DEBUG(interface, "Records initial values");
324 i = (int)(sizeof hvac_values / sizeof *hvac_values);
327 values[i] = saves[i] = hvac_values[i].value;
330 /* Loop getting arguments */
331 query = afb_req_json(request);
333 i = (int)(sizeof hvac_values / sizeof *hvac_values);
334 DEBUG(interface, "Looping for args. i: %d", 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))
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);
346 else if (json_object_is_type(val, json_type_double)) {
347 d = json_object_get_double(val);
349 DEBUG(interface, "We get a double: %f => %d",d,x);
352 afb_req_fail_f(request, "bad-request",
353 "argument '%s' isn't integer or double", hvac_values[i].name);
356 if (x < 0 || x > 255)
358 afb_req_fail_f(request, "bad-request",
359 "argument '%s' is out of bounds", hvac_values[i].name);
362 if (values[i] != x) {
363 values[i] = (uint8_t)x;
365 DEBUG(interface,"%s changed to %d",hvac_values[i].name,x);
369 DEBUG(interface, "%s not found in query!",hvac_values[i].name);
373 /* attemps to set new values */
374 DEBUG(interface, "Diff: %d", changed);
377 i = (int)(sizeof hvac_values / sizeof *hvac_values);
380 hvac_values[i].value = values[i];
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);
390 hvac_values[i].value = saves[i];
392 afb_req_fail(request, "error", "CAN error");
396 afb_req_success(request, NULL, "No changes");
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"},
410 static const struct afb_binding binding_desc = {
411 .type = AFB_BINDING_VERSION_1,
413 .info = "hvac service",
419 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
423 return &binding_desc;
426 int afbBindingV1ServiceInit(struct afb_service service)
428 return retry(open_can_dev);