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 2
35 #include <afb/afb-binding.h>
37 #define CAN_DEV "vcan0"
43 static const struct afb_binding_interface *interface;
44 static struct afb_event event;
46 /*****************************************************************************************/
47 /*****************************************************************************************/
50 /** SECTION: UTILITY FUNCTIONS **/
53 /*****************************************************************************************/
54 /*****************************************************************************************/
57 * @brief Retry a function 3 times
59 * @param int function(): function that return an int wihtout any parameter
61 * @ return : 0 if ok, -1 if failed
64 static int retry( int(*func)())
79 /*****************************************************************************************/
80 /*****************************************************************************************/
83 /** SECTION: HANDLE CAN DEVICE **/
86 /*****************************************************************************************/
87 /*****************************************************************************************/
89 // Initialize CAN hvac array that will be sent trough the socket
94 { "LeftTemperature", 21 },
95 { "RightTemperature", 21 },
96 { "Temperature", 21 },
104 struct sockaddr_can txAddress;
107 static struct can_handler can_handler = { .socket = -1, .simulation = false, .send_msg = "SENDING CAN FRAME"};
109 static int open_can_dev_helper()
113 AFB_DEBUG("CAN Handler socket : %d", can_handler.socket);
114 close(can_handler.socket);
116 can_handler.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
117 if (can_handler.socket < 0)
119 AFB_ERROR("socket could not be created");
123 // Attempts to open a socket to CAN bus
124 strcpy(ifr.ifr_name, CAN_DEV);
125 if(ioctl(can_handler.socket, SIOCGIFINDEX, &ifr) < 0)
127 AFB_ERROR("ioctl failed");
131 can_handler.txAddress.can_family = AF_CAN;
132 can_handler.txAddress.can_ifindex = ifr.ifr_ifindex;
134 // And bind it to txAddress
135 if (bind(can_handler.socket, (struct sockaddr *)&can_handler.txAddress, sizeof(can_handler.txAddress)) < 0)
137 AFB_ERROR("bind failed");
143 close(can_handler.socket);
144 can_handler.socket = -1;
149 static int open_can_dev()
151 int rc = retry(open_can_dev_helper);
154 AFB_ERROR("Open of interface %s failed. Falling back to simulation mode", CAN_DEV);
155 can_handler.socket = 0;
156 can_handler.simulation = true;
157 can_handler.send_msg = "FAKE CAN FRAME";
163 // Get original get temperature function from cpp hvacplugin code
164 static uint8_t to_can_temp(uint8_t value)
166 int result = ((0xF0 - 0x10) / 15) * (value - 15) + 0x10;
172 return (uint8_t)result;
175 static uint8_t read_temp_left_zone()
177 return hvac_values[0].value;
180 static uint8_t read_temp_right_zone()
182 return hvac_values[1].value;
185 static uint8_t read_temp()
187 return (uint8_t)(((int)read_temp_left_zone() + (int)read_temp_right_zone()) >> 1);
190 static uint8_t read_fanspeed()
192 return hvac_values[3].value;
195 static int write_can()
197 struct can_frame txCanFrame;
200 rc = can_handler.socket;
203 // Hardcoded can_id and dlc (data lenght code)
204 txCanFrame.can_id = 0x30;
205 txCanFrame.can_dlc = 8;
206 txCanFrame.data[0] = to_can_temp(read_temp_left_zone());
207 txCanFrame.data[1] = to_can_temp(read_temp_right_zone());
208 txCanFrame.data[2] = to_can_temp(read_temp());
209 txCanFrame.data[3] = 0xf0;
210 txCanFrame.data[4] = read_fanspeed();
211 txCanFrame.data[5] = 1;
212 txCanFrame.data[6] = 0;
213 txCanFrame.data[7] = 0;
215 AFB_DEBUG("%s: %d %d [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
216 can_handler.send_msg,
217 txCanFrame.can_id, txCanFrame.can_dlc,
218 txCanFrame.data[0], txCanFrame.data[1], txCanFrame.data[2], txCanFrame.data[3],
219 txCanFrame.data[4], txCanFrame.data[5], txCanFrame.data[6], txCanFrame.data[7]);
221 if(!can_handler.simulation)
223 rc = (int)sendto(can_handler.socket, &txCanFrame, sizeof(struct can_frame), 0,
224 (struct sockaddr*)&can_handler.txAddress, sizeof(can_handler.txAddress));
227 AFB_ERROR("Sending CAN frame failed.");
233 AFB_ERROR("socket not initialized. Attempt to reopen can device socket.");
239 /*****************************************************************************************/
240 /*****************************************************************************************/
243 /** SECTION: BINDING VERBS IMPLEMENTATION **/
246 /*****************************************************************************************/
247 /*****************************************************************************************/
250 * @brief Get fan speed HVAC system
252 * @param struct afb_req : an afb request structure
255 static void get_fanspeed(struct afb_req request)
257 json_object *ret_json;
258 uint8_t fanspeed = read_fanspeed();
260 ret_json = json_object_new_object();
261 json_object_object_add(ret_json, "FanSpeed", json_object_new_int(fanspeed));
263 afb_req_success(request, ret_json, NULL);
267 * @brief Read Consign right zone temperature for HVAC system
269 * @param struct afb_req : an afb request structure
272 static void get_temp_right_zone(struct afb_req request)
274 json_object *ret_json;
275 uint8_t temp = read_temp_right_zone();
277 ret_json = json_object_new_object();
278 json_object_object_add(ret_json, "RightTemperature", json_object_new_int(temp));
280 afb_req_success(request, ret_json, NULL);
284 * @brief Read Consign left zone temperature for HVAC system
286 * @param struct afb_req : an afb request structure
289 static void get_temp_left_zone(struct afb_req request)
291 json_object *ret_json;
292 uint8_t temp = read_temp_left_zone();
294 ret_json = json_object_new_object();
295 json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(temp));
297 afb_req_success(request, ret_json, NULL);
301 * @brief Read all values
303 * @param struct afb_req : an afb request structure
306 static void get(struct afb_req request)
308 AFB_DEBUG("Getting all values");
309 json_object *ret_json;
311 ret_json = json_object_new_object();
312 json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(read_temp_left_zone()));
313 json_object_object_add(ret_json, "RightTemperature", json_object_new_int(read_temp_right_zone()));
314 json_object_object_add(ret_json, "FanSpeed", json_object_new_int(read_fanspeed()));
316 afb_req_success(request, ret_json, NULL);
320 * @brief Set a component value using a json object retrieved from request
322 * @param struct afb_req : an afb request structure
325 static void set(struct afb_req request)
327 int i, rc, x, changed;
329 struct json_object *query, *val;
330 uint8_t values[sizeof hvac_values / sizeof *hvac_values];
331 uint8_t saves[sizeof hvac_values / sizeof *hvac_values];
333 /* records initial values */
334 AFB_DEBUG("Records initial values");
335 i = (int)(sizeof hvac_values / sizeof *hvac_values);
338 values[i] = saves[i] = hvac_values[i].value;
341 /* Loop getting arguments */
342 query = afb_req_json(request);
344 i = (int)(sizeof hvac_values / sizeof *hvac_values);
345 AFB_DEBUG("Looping for args. i: %d", i);
349 AFB_DEBUG("Searching... query: %s, i: %d, comp: %s", json_object_to_json_string(query), i, hvac_values[i].name);
350 if (json_object_object_get_ex(query, hvac_values[i].name, &val))
352 AFB_DEBUG("We got it. Tests if it is an int or double.");
353 if (json_object_is_type(val, json_type_int)) {
354 x = json_object_get_int(val);
355 AFB_DEBUG("We get an int: %d",x);
357 else if (json_object_is_type(val, json_type_double)) {
358 d = json_object_get_double(val);
360 AFB_DEBUG("We get a double: %f => %d",d,x);
363 afb_req_fail_f(request, "bad-request",
364 "argument '%s' isn't integer or double", hvac_values[i].name);
367 if (x < 0 || x > 255)
369 afb_req_fail_f(request, "bad-request",
370 "argument '%s' is out of bounds", hvac_values[i].name);
373 if (values[i] != x) {
374 values[i] = (uint8_t)x;
376 AFB_DEBUG("%s changed to %d",hvac_values[i].name,x);
380 AFB_DEBUG("%s not found in query!",hvac_values[i].name);
384 /* attemps to set new values */
385 AFB_DEBUG("Diff: %d", changed);
388 i = (int)(sizeof hvac_values / sizeof *hvac_values);
391 hvac_values[i].value = values[i];
395 afb_req_success(request, NULL, NULL);
396 else if (retry(write_can)) {
397 /* restore initial values */
398 i = (int)(sizeof hvac_values / sizeof *hvac_values);
401 hvac_values[i].value = saves[i];
403 afb_req_fail(request, "error", "CAN error");
407 afb_req_success(request, NULL, "No changes");
411 int bindingServicePreInit(struct afb_service service)
413 return open_can_dev();
416 int bindingServiceInit(struct afb_service service)
418 event = afb_daemon_make_event("language");
419 if(afb_daemon_require_api("identity", 1))
421 return afb_service_call_sync("identity", "subscribe", NULL, NULL);
424 void onEvent(const char *event_name, struct json_object *object)
426 json_object *args, *language = json_object_new_object();
427 json_object *id_evt_name, *current_identity;
429 AFB_NOTICE("Event '%s' received: %s", event_name,
430 json_object_to_json_string_ext(object, JSON_C_TO_STRING_PRETTY));
432 if (json_object_object_get_ex(object, "eventName", &id_evt_name) &&
433 !strcmp(json_object_get_string(id_evt_name), "login") &&
434 !afb_service_call_sync("identity", "get", NULL, ¤t_identity)) {
435 json_object *response;
436 if (! json_object_object_get_ex(current_identity, "response", &response) || ! json_object_object_get_ex(response, "graphPreferredLanguage", &language)) {
437 language = json_object_new_string("en_US");
439 afb_event_broadcast(event, language);
443 // TODO: Have to change session management flag to AFB_SESSION_CHECK to use token auth
444 static const struct afb_verb_v2 _afb_verbs_v2_hvac[]= {
446 .verb = "get_temp_left_zone",
447 .callback = get_temp_left_zone,
449 .info = "Get the left zone temperature",
450 .session = AFB_SESSION_NONE_V2
453 .verb = "get_temp_right_zone",
454 .callback = get_temp_right_zone,
456 .info = "Get the right zone temperature",
457 .session = AFB_SESSION_NONE_V2
460 .verb = "get_fanspeed",
461 .callback = get_fanspeed,
463 .info = "Read fan speed",
464 .session = AFB_SESSION_NONE_V2
470 .info = "Read all speed",
471 .session = AFB_SESSION_NONE_V2
477 .info = "Set a HVAC component value",
478 .session = AFB_SESSION_NONE_V2
489 const struct afb_binding_v2 afbBindingV2 = {
491 .specification = NULL,
492 .info = "HVAC service",
493 .verbs = _afb_verbs_v2_hvac,
494 .preinit = bindingServicePreInit,
495 .init = bindingServiceInit,