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.
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/ioctl.h>
30 #include <linux/can.h>
33 #include <json-c/json.h>
35 #include <afb/afb-binding.h>
36 #include <afb/afb-service-itf.h>
38 #define CAN_DEV "vcan0"
39 #define LED_BRIGHTNESS "/sys/class/leds/blinkm-3-9-red/brightness"
41 static const struct afb_binding_interface *interface;
43 /*****************************************************************************************/
44 /*****************************************************************************************/
47 /** SECTION: UTILITY FUNCTIONS **/
50 /*****************************************************************************************/
51 /*****************************************************************************************/
54 * @brief Retry a function 3 times
56 * @param int function(): function that return an int wihtout any parameter
58 * @ return : 0 if ok, -1 if failed
61 static int retry( int(*func)())
76 /*****************************************************************************************/
77 /*****************************************************************************************/
80 /** SECTION: HANDLE CAN DEVICE **/
83 /*****************************************************************************************/
84 /*****************************************************************************************/
86 // Initialize CAN hvac array that will be sent trough the socket
91 { "LeftTemperature", 21 },
92 { "RightTemperature", 21 },
93 { "Temperature", 21 },
102 struct sockaddr_can txAddress;
105 static struct can_handler can_handler = { .socket = -1, .simulation = false, .send_msg = "SENDING CAN FRAME"};
107 static int open_can_dev_helper()
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;
147 static int open_can_dev()
149 int rc = retry(open_can_dev_helper);
152 ERROR(interface, "Open of interface %s failed. Falling back to simulation mode", CAN_DEV);
153 can_handler.socket = 0;
154 can_handler.simulation = true;
155 can_handler.send_msg = "FAKE CAN FRAME";
161 // Get original get temperature function from cpp hvacplugin code
162 static uint8_t to_can_temp(uint8_t value)
164 int result = ((0xF0 - 0x10) / 15) * (value - 15) + 0x10;
170 return (uint8_t)result;
173 static uint8_t read_temp_left_zone()
175 return hvac_values[0].value;
178 static uint8_t read_temp_right_zone()
180 return hvac_values[1].value;
183 static uint8_t read_temp()
185 return (uint8_t)(((int)read_temp_left_zone() + (int)read_temp_right_zone()) >> 1);
188 static uint8_t read_fanspeed()
190 return hvac_values[3].value;
193 static uint8_t read_acenabled()
195 WARNING(interface, "in read_acenabled: value= %d", hvac_values[4].value);
196 return hvac_values[4].value;
199 static int write_led()
203 // /sys/class/leds/blinkm-3-9-red/brightness
204 FILE* f = fopen(LED_BRIGHTNESS, "w");
206 ERROR(interface, "Unable to open path for writing");
210 fprintf(f, "%d", read_acenabled());
215 static int write_can()
217 struct can_frame txCanFrame;
220 rc = can_handler.socket;
223 // Hardcoded can_id and dlc (data lenght code)
224 txCanFrame.can_id = 0x30;
225 txCanFrame.can_dlc = 8;
226 txCanFrame.data[0] = to_can_temp(read_temp_left_zone());
227 txCanFrame.data[1] = to_can_temp(read_temp_right_zone());
228 txCanFrame.data[2] = to_can_temp(read_temp());
229 txCanFrame.data[3] = 0xf0;
230 txCanFrame.data[4] = read_fanspeed();
231 txCanFrame.data[5] = 1;
232 txCanFrame.data[6] = 0;
233 txCanFrame.data[7] = 0;
235 DEBUG(interface, "%s: %d %d [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
236 can_handler.send_msg,
237 txCanFrame.can_id, txCanFrame.can_dlc,
238 txCanFrame.data[0], txCanFrame.data[1], txCanFrame.data[2], txCanFrame.data[3],
239 txCanFrame.data[4], txCanFrame.data[5], txCanFrame.data[6], txCanFrame.data[7]);
241 if(!can_handler.simulation)
243 rc = sendto(can_handler.socket, &txCanFrame, sizeof(struct can_frame), 0,
244 (struct sockaddr*)&can_handler.txAddress, sizeof(can_handler.txAddress));
247 ERROR(interface, "Sending CAN frame failed.");
253 ERROR(interface, "socket not initialized. Attempt to reopen can device socket.");
259 /*****************************************************************************************/
260 /*****************************************************************************************/
263 /** SECTION: BINDING VERBS IMPLEMENTATION **/
266 /*****************************************************************************************/
267 /*****************************************************************************************/
270 * @brief Get fan speed HVAC system
272 * @param struct afb_req : an afb request structure
275 static void get_acenabled(struct afb_req request)
277 json_object *ret_json;
279 uint8_t acenabled = read_acenabled();
280 WARNING(interface, "in get_acenabled: acenabled= %d", acenabled);
282 ret_json = json_object_new_object();
283 json_object_object_add(ret_json, "ACEnabled", json_object_new_int(acenabled));
285 afb_req_success(request, ret_json, NULL);
290 * @brief Get fan speed HVAC system
292 * @param struct afb_req : an afb request structure
295 static void get_fanspeed(struct afb_req request)
297 json_object *ret_json;
298 uint8_t fanspeed = read_fanspeed();
300 ret_json = json_object_new_object();
301 json_object_object_add(ret_json, "FanSpeed", json_object_new_int(fanspeed));
303 afb_req_success(request, ret_json, NULL);
307 * @brief Read Consign right zone temperature for HVAC system
309 * @param struct afb_req : an afb request structure
312 static void get_temp_right_zone(struct afb_req request)
314 json_object *ret_json;
315 uint8_t temp = read_temp_right_zone();
317 ret_json = json_object_new_object();
318 json_object_object_add(ret_json, "RightTemperature", json_object_new_int(temp));
320 afb_req_success(request, ret_json, NULL);
324 * @brief Read Consign left zone temperature for HVAC system
326 * @param struct afb_req : an afb request structure
329 static void get_temp_left_zone(struct afb_req request)
331 json_object *ret_json;
332 uint8_t temp = read_temp_left_zone();
334 ret_json = json_object_new_object();
335 json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(temp));
337 afb_req_success(request, ret_json, NULL);
341 * @brief Read all values
343 * @param struct afb_req : an afb request structure
346 static void get(struct afb_req request)
348 DEBUG(interface, "Getting all values");
349 json_object *ret_json;
351 ret_json = json_object_new_object();
352 json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(read_temp_left_zone()));
353 json_object_object_add(ret_json, "RightTemperature", json_object_new_int(read_temp_right_zone()));
354 json_object_object_add(ret_json, "FanSpeed", json_object_new_int(read_fanspeed()));
355 json_object_object_add(ret_json, "ACEnabled", json_object_new_int(read_acenabled()));
357 afb_req_success(request, ret_json, NULL);
360 static void set_acenabled(struct afb_req request)
362 int i=4, rc, x, changed;
364 struct json_object *query, *val;
365 uint8_t values[sizeof hvac_values / sizeof *hvac_values];
366 uint8_t saves[sizeof hvac_values / sizeof *hvac_values];
368 WARNING(interface, "in set_acenabled.");
370 query = afb_req_json(request);
372 /* records initial values */
373 WARNING(interface, "Records initial values");
374 values[i] = saves[i] = hvac_values[i].value;
377 if (json_object_object_get_ex(query, hvac_values[i].name, &val))
379 WARNING(interface, "We got it. Tests if it is an int or double.");
380 if (json_object_is_type(val, json_type_int)) {
381 x = json_object_get_int(val);
382 WARNING(interface, "We get an int: %d",x);
384 else if (json_object_is_type(val, json_type_double)) {
385 d = json_object_get_double(val);
387 WARNING(interface, "We get a double: %f => %d",d,x);
390 afb_req_fail_f(request, "bad-request",
391 "argument '%s' isn't integer or double", hvac_values[i].name);
394 if (x < 0 || x > 255)
396 afb_req_fail_f(request, "bad-request",
397 "argument '%s' is out of bounds", hvac_values[i].name);
400 if (values[i] != x) {
401 values[i] = (uint8_t)x;
403 WARNING(interface,"%s changed to %d",hvac_values[i].name,x);
407 WARNING(interface, "%s not found in query!",hvac_values[i].name);
412 i = 4; //(int)(sizeof hvac_values / sizeof *hvac_values);
413 hvac_values[i].value = values[i];
414 WARNING(interface, "WRITE_LED: value: %d ",hvac_values[i].value);
417 afb_req_success(request, NULL, NULL);
418 else if (retry(write_led)) {
419 /* restore initial values */
420 i = 4; //(int)(sizeof hvac_values / sizeof *hvac_values);
421 hvac_values[i].value = saves[i];
422 afb_req_fail(request, "error", "I2C error");
426 afb_req_success(request, NULL, "No changes");
433 * @brief Set a component value using a json object retrieved from request
435 * @param struct afb_req : an afb request structure
438 static void set(struct afb_req request)
440 int i, rc, x, changed;
442 struct json_object *query, *val;
443 uint8_t values[sizeof hvac_values / sizeof *hvac_values];
444 uint8_t saves[sizeof hvac_values / sizeof *hvac_values];
446 /* records initial values */
447 DEBUG(interface, "Records initial values");
448 i = (int)(sizeof hvac_values / sizeof *hvac_values);
451 values[i] = saves[i] = hvac_values[i].value;
454 /* Loop getting arguments */
455 query = afb_req_json(request);
457 i = (int)(sizeof hvac_values / sizeof *hvac_values);
458 DEBUG(interface, "Looping for args. i: %d", i);
462 DEBUG(interface, "Searching... query: %s, i: %d, comp: %s", json_object_to_json_string(query), i, hvac_values[i].name);
463 if (json_object_object_get_ex(query, hvac_values[i].name, &val))
465 DEBUG(interface, "We got it. Tests if it is an int or double.");
466 if (json_object_is_type(val, json_type_int)) {
467 x = json_object_get_int(val);
468 DEBUG(interface, "We get an int: %d",x);
470 else if (json_object_is_type(val, json_type_double)) {
471 d = json_object_get_double(val);
473 DEBUG(interface, "We get a double: %f => %d",d,x);
476 afb_req_fail_f(request, "bad-request",
477 "argument '%s' isn't integer or double", hvac_values[i].name);
480 if (x < 0 || x > 255)
482 afb_req_fail_f(request, "bad-request",
483 "argument '%s' is out of bounds", hvac_values[i].name);
486 if (values[i] != x) {
487 values[i] = (uint8_t)x;
489 DEBUG(interface,"%s changed to %d",hvac_values[i].name,x);
493 DEBUG(interface, "%s not found in query!",hvac_values[i].name);
497 /* attemps to set new values */
498 DEBUG(interface, "Diff: %d", changed);
501 i = (int)(sizeof hvac_values / sizeof *hvac_values);
504 hvac_values[i].value = values[i];
508 afb_req_success(request, NULL, NULL);
509 else if (retry(write_can)) {
510 /* restore initial values */
511 i = (int)(sizeof hvac_values / sizeof *hvac_values);
514 hvac_values[i].value = saves[i];
516 afb_req_fail(request, "error", "CAN error");
520 afb_req_success(request, NULL, "No changes");
524 // TODO: Have to change session management flag to AFB_SESSION_CHECK to use token auth
525 static const struct afb_verb_desc_v1 verbs[]= {
526 {"get_temp_left_zone" , AFB_SESSION_NONE, get_temp_left_zone , "Get the left zone temperature"},
527 {"get_temp_right_zone" , AFB_SESSION_NONE, get_temp_right_zone , "Get the right zone temperature"},
528 {"get_fanspeed" , AFB_SESSION_NONE, get_fanspeed , "Read fan speed"},
529 {"get_acenabled" , AFB_SESSION_NONE, get_acenabled , "Read fan speed"},
530 {"get" , AFB_SESSION_NONE, get , "Read all values"},
531 {"set" , AFB_SESSION_NONE, set , "Set a HVAC component value"},
532 {"set_acenabled" , AFB_SESSION_NONE, set_acenabled , "Set a HVAC component value"},
536 static const struct afb_binding binding_desc = {
537 .type = AFB_BINDING_VERSION_1,
539 .info = "hvac service",
545 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
549 return &binding_desc;
552 int afbBindingV1ServiceInit(struct afb_service service)
554 return open_can_dev();