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 #define CAN_DEV "vcan0"
38 static const struct afb_binding_interface *interface;
40 // Initialize CAN hvac array that will be sent trough the socket
45 { "LeftTemperature", 21 },
46 { "RightTemperature", 21 },
47 { "Temperature", 21 },
53 struct sockaddr_can txAddress;
56 static struct can_handler can_handler = { .socket = -1 };
58 /*****************************************************************************************/
59 /*****************************************************************************************/
62 /** SECTION: HANDLE CAN DEVICE **/
65 /*****************************************************************************************/
66 /*****************************************************************************************/
68 static int open_can_dev()
70 #if defined(SIMULATE_HVAC)
71 DEBUG(interface, "Defining can handler socket to 0 and return");
72 can_handler.socket = 0;
77 can_handler.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
78 if (can_handler.socket < 0)
80 ERROR(interface, "socket could not be created");
84 // Attempts to open a socket to CAN bus
85 strcpy(ifr.ifr_name, CAN_DEV);
86 if(ioctl(can_handler.socket, SIOCGIFINDEX, &ifr) < 0)
88 ERROR(interface, "ioctl failed");
92 can_handler.txAddress.can_family = AF_CAN;
93 can_handler.txAddress.can_ifindex = ifr.ifr_ifindex;
95 // And bind it to txAddress
96 if (bind(can_handler.socket, (struct sockaddr *)&can_handler.txAddress, sizeof(can_handler.txAddress)) < 0)
98 ERROR(interface, "bind failed");
104 close(can_handler.socket);
105 can_handler.socket = -1;
111 // Get original get temperature function from cpp hvacplugin code
112 static uint8_t to_can_temp(uint8_t value)
114 int result = ((0xF0 - 0x10) / 15) * value - 16;
120 return (uint8_t)result;
123 static uint8_t read_temp_left_zone()
125 return hvac_values[0].value;
128 static uint8_t read_temp_right_zone()
130 return hvac_values[1].value;
133 static uint8_t read_temp()
135 return (uint8_t)(((int)read_temp_left_zone() + (int)read_temp_right_zone()) >> 1);
138 static uint8_t read_fanspeed()
140 return hvac_values[3].value;
143 static int write_can()
145 struct can_frame txCanFrame;
148 rc = can_handler.socket;
151 // Hardcoded can_id and dlc (data lenght code)
152 txCanFrame.can_id = 0x30;
153 txCanFrame.can_dlc = 8;
154 txCanFrame.data[0] = to_can_temp(read_temp_left_zone());
155 txCanFrame.data[1] = to_can_temp(read_temp_right_zone());
156 txCanFrame.data[2] = to_can_temp(read_temp());
157 txCanFrame.data[3] = 0xf0;
158 txCanFrame.data[4] = read_fanspeed();
159 txCanFrame.data[5] = 1;
160 txCanFrame.data[6] = 0;
161 txCanFrame.data[7] = 0;
163 DEBUG(interface, "%s: %d %d [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
164 #if defined(SIMULATE_HVAC)
169 txCanFrame.can_id, txCanFrame.can_dlc,
170 txCanFrame.data[0], txCanFrame.data[1], txCanFrame.data[2], txCanFrame.data[3],
171 txCanFrame.data[4], txCanFrame.data[5], txCanFrame.data[6], txCanFrame.data[7]);
173 #if !defined(SIMULATE_HVAC)
174 rc = sendto(can_handler.socket, &txCanFrame, sizeof(struct can_frame), 0,
175 (struct sockaddr*)&can_handler.txAddress, sizeof(can_handler.txAddress));
178 ERROR(interface, "Sending can frame failed");
184 ERROR(interface, "socket not initialized");
189 /*****************************************************************************************/
190 /*****************************************************************************************/
193 /** SECTION: BINDING VERBS IMPLEMENTATION **/
196 /*****************************************************************************************/
197 /*****************************************************************************************/
200 * @brief Get fan speed HVAC system
202 * @param struct afb_req : an afb request structure
205 static void get_fanspeed(struct afb_req request)
207 json_object *ret_json;
208 uint8_t fanspeed = read_fanspeed();
210 ret_json = json_object_new_object();
211 json_object_object_add(ret_json, "FanSpeed", json_object_new_int(fanspeed));
213 afb_req_success(request, ret_json, NULL);
217 * @brief Read Consign right zone temperature for HVAC system
219 * @param struct afb_req : an afb request structure
222 static void get_temp_right_zone(struct afb_req request)
224 json_object *ret_json;
225 uint8_t temp = read_temp_right_zone();
227 ret_json = json_object_new_object();
228 json_object_object_add(ret_json, "RightTemperature", json_object_new_int(temp));
230 afb_req_success(request, ret_json, NULL);
234 * @brief Read Consign left zone temperature for HVAC system
236 * @param struct afb_req : an afb request structure
239 static void get_temp_left_zone(struct afb_req request)
241 json_object *ret_json;
242 uint8_t temp = read_temp_left_zone();
244 ret_json = json_object_new_object();
245 json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(temp));
247 afb_req_success(request, ret_json, NULL);
251 * @brief Read all values
253 * @param struct afb_req : an afb request structure
256 static void get(struct afb_req request)
258 DEBUG(interface, "Getting all values");
259 json_object *ret_json;
261 ret_json = json_object_new_object();
262 json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(read_temp_left_zone()));
263 json_object_object_add(ret_json, "RightTemperature", json_object_new_int(read_temp_right_zone()));
264 json_object_object_add(ret_json, "FanSpeed", json_object_new_int(read_fanspeed()));
266 afb_req_success(request, ret_json, NULL);
270 * @brief Set a component value using a json object retrieved from request
272 * @param struct afb_req : an afb request structure
275 static void set(struct afb_req request)
277 int i, rc, x, changed;
279 struct json_object *query, *val;
280 uint8_t values[sizeof hvac_values / sizeof *hvac_values];
281 uint8_t saves[sizeof hvac_values / sizeof *hvac_values];
283 /* records initial values */
284 DEBUG(interface, "Records initial values");
285 i = (int)(sizeof hvac_values / sizeof *hvac_values);
288 values[i] = saves[i] = hvac_values[i].value;
291 /* Loop getting arguments */
292 query = afb_req_json(request);
294 i = (int)(sizeof hvac_values / sizeof *hvac_values);
295 DEBUG(interface, "Looping for args. i: %d", i);
299 DEBUG(interface, "Searching... query: %s, i: %d, comp: %s", json_object_to_json_string(query), i, hvac_values[i].name);
300 if (json_object_object_get_ex(query, hvac_values[i].name, &val))
302 DEBUG(interface, "We got it. Tests if it is an int or double.");
303 if (json_object_is_type(val, json_type_int)) {
304 x = json_object_get_int(val);
305 DEBUG(interface, "We get an int: %d",x);
307 else if (json_object_is_type(val, json_type_double)) {
308 d = json_object_get_double(val);
310 DEBUG(interface, "We get a double: %f => %d",d,x);
313 afb_req_fail_f(request, "bad-request",
314 "argument '%s' isn't integer or double", hvac_values[i].name);
317 if (x < 0 || x > 255)
319 afb_req_fail_f(request, "bad-request",
320 "argument '%s' is out of bounds", hvac_values[i].name);
323 if (values[i] != x) {
324 values[i] = (uint8_t)x;
326 DEBUG(interface,"%s changed to %d",hvac_values[i].name,x);
330 DEBUG(interface, "%s not found in query!",hvac_values[i].name);
334 /* attemps to set new values */
335 DEBUG(interface, "Diff: %d", changed);
338 i = (int)(sizeof hvac_values / sizeof *hvac_values);
341 hvac_values[i].value = values[i];
345 afb_req_success(request, NULL, NULL);
347 /* restore initial values */
348 i = (int)(sizeof hvac_values / sizeof *hvac_values);
351 hvac_values[i].value = saves[i];
353 afb_req_fail(request, "error", "CAN error");
357 afb_req_success(request, NULL, "No changes");
361 // TODO: Have to change session management flag to AFB_SESSION_CHECK to use token auth
362 static const struct afb_verb_desc_v1 verbs[]= {
363 {"get_temp_left_zone" , AFB_SESSION_NONE, get_temp_left_zone , "Get the left zone temperature"},
364 {"get_temp_right_zone" , AFB_SESSION_NONE, get_temp_right_zone , "Get the right zone temperature"},
365 {"get_fanspeed" , AFB_SESSION_NONE, get_fanspeed , "Read fan speed"},
366 {"get" , AFB_SESSION_NONE, get , "Read all values"},
367 {"set" , AFB_SESSION_NONE, set , "Set a HVAC component value"},
371 static const struct afb_binding binding_desc = {
372 .type = AFB_BINDING_VERSION_1,
374 .info = "hvac service",
380 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
384 return &binding_desc;
387 int afbBindingV1ServiceInit(struct afb_service service)
389 return open_can_dev();