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>
28 #include <json-c/json.h>
30 #include <afb/afb-binding.h>
31 #include <afb/afb-service-itf.h>
33 #define CAN_DEV "vcan0"
37 static const struct afb_binding_interface *interface;
39 // Initialize CAN hvac array that will be sent trough the socket
44 { "LeftTemperature", 21 },
45 { "RightTemperature", 21 },
46 { "Temperature", 21 },
52 struct sockaddr_can txAddress;
55 static struct can_handler can_handler = { .socket = -1 };
57 /*****************************************************************************************/
58 /*****************************************************************************************/
61 /** SECTION: HANDLE CAN DEVICE **/
64 /*****************************************************************************************/
65 /*****************************************************************************************/
67 static int open_can_dev()
69 #if defined(SIMULATE_HVAC)
70 DEBUG(interface, "Defining can handler socket to 0 and return");
71 can_handler.socket = 0;
76 can_handler.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
77 if (can_handler.socket < 0)
79 ERROR(interface, "socket could not be created");
83 // Attempts to open a socket to CAN bus
84 strcpy(ifr.ifr_name, CAN_DEV);
85 if(ioctl(can_handler.socket, SIOCGIFINDEX, &ifr) < 0)
87 ERROR(interface, "ioctl failed");
91 can_handler.txAddress.can_family = AF_CAN;
92 can_handler.txAddress.can_ifindex = ifr.ifr_ifindex;
94 // And bind it to txAddress
95 if (bind(can_handler.socket, (struct sockaddr *)&can_handler.txAddress, sizeof(can_handler.txAddress)) < 0)
97 ERROR(interface, "bind failed");
103 close(can_handler.socket);
104 can_handler.socket = -1;
110 // Get original get temperature function from cpp hvacplugin code
111 static uint8_t to_can_temp(uint8_t value)
113 int result = ((0xF0 - 0x10) / 15) * value - 16;
119 return (uint8_t)result;
122 static uint8_t read_temp_left_zone()
124 return hvac_values[0].value;
127 static uint8_t read_temp_right_zone()
129 return hvac_values[1].value;
132 static uint8_t read_temp()
134 return (uint8_t)(((int)read_temp_left_zone() + (int)read_temp_right_zone()) >> 1);
137 static uint8_t read_fanspeed()
139 return hvac_values[3].value;
142 static int write_can()
144 struct can_frame txCanFrame;
147 rc = can_handler.socket;
150 // Hardcoded can_id and dlc (data lenght code)
151 txCanFrame.can_id = 0x30;
152 txCanFrame.can_dlc = 8;
153 txCanFrame.data[0] = to_can_temp(read_temp_left_zone());
154 txCanFrame.data[1] = to_can_temp(read_temp_right_zone());
155 txCanFrame.data[2] = to_can_temp(read_temp());
156 txCanFrame.data[3] = 0xf0;
157 txCanFrame.data[4] = read_fanspeed();
158 txCanFrame.data[5] = 1;
159 txCanFrame.data[6] = 0;
160 txCanFrame.data[7] = 0;
162 #if defined(SIMULATE_HVAC)
163 DEBUG(interface, "WRITING CAN: %d %d [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
164 txCanFrame.can_id, txCanFrame.can_dlc,
165 txCanFrame.data[0], txCanFrame.data[1], txCanFrame.data[2], txCanFrame.data[3],
166 txCanFrame.data[4], txCanFrame.data[5], txCanFrame.data[6], txCanFrame.data[7]);
168 rc = sendto(can_handler.socket, &txCanFrame, sizeof(struct can_frame), 0,
169 (struct sockaddr*)&can_handler.txAddress, sizeof(can_handler.txAddress));
172 ERROR(interface, "Sending can frame failed");
178 ERROR(interface, "socket not initialized");
183 /*****************************************************************************************/
184 /*****************************************************************************************/
187 /** SECTION: BINDING VERBS IMPLEMENTATION **/
190 /*****************************************************************************************/
191 /*****************************************************************************************/
194 * @brief Get fan speed HVAC system
196 * @param struct afb_req : an afb request structure
199 static void get_fanspeed(struct afb_req request)
201 json_object *ret_json;
202 uint8_t fanspeed = read_fanspeed();
204 ret_json = json_object_new_object();
205 json_object_object_add(ret_json, "FanSpeed", json_object_new_int(fanspeed));
207 afb_req_success(request, ret_json, NULL);
211 * @brief Read Consign right zone temperature for HVAC system
213 * @param struct afb_req : an afb request structure
216 static void get_temp_right_zone(struct afb_req request)
218 json_object *ret_json;
219 uint8_t temp = read_temp_right_zone();
221 ret_json = json_object_new_object();
222 json_object_object_add(ret_json, "RightTemperature", json_object_new_int(temp));
224 afb_req_success(request, ret_json, NULL);
228 * @brief Read Consign left zone temperature for HVAC system
230 * @param struct afb_req : an afb request structure
233 static void get_temp_left_zone(struct afb_req request)
235 json_object *ret_json;
236 uint8_t temp = read_temp_left_zone();
238 ret_json = json_object_new_object();
239 json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(temp));
241 afb_req_success(request, ret_json, NULL);
245 * @brief Read all values
247 * @param struct afb_req : an afb request structure
250 static void get(struct afb_req request)
252 DEBUG(interface, "Getting all values");
253 json_object *ret_json;
255 ret_json = json_object_new_object();
256 json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(read_temp_left_zone()));
257 json_object_object_add(ret_json, "RightTemperature", json_object_new_int(read_temp_right_zone()));
258 json_object_object_add(ret_json, "FanSpeed", json_object_new_int(read_fanspeed()));
260 afb_req_success(request, ret_json, NULL);
264 * @brief Set a component value using a json object retrieved from request
266 * @param struct afb_req : an afb request structure
269 static void set(struct afb_req request)
271 int i, rc, x, changed;
272 struct json_object *query, *val;
273 uint8_t values[sizeof hvac_values / sizeof *hvac_values];
274 uint8_t saves[sizeof hvac_values / sizeof *hvac_values];
276 /* records initial values */
277 DEBUG(interface, "Records initial values");
278 i = (int)(sizeof hvac_values / sizeof *hvac_values);
281 values[i] = saves[i] = hvac_values[i].value;
284 /* Loop getting arguments */
285 query = afb_req_json(request);
287 i = (int)(sizeof hvac_values / sizeof *hvac_values);
288 DEBUG(interface, "Looping for args. i: %d", i);
292 DEBUG(interface, "Searching... query: %s, i: %d, comp: %s", json_object_to_json_string(query), i, hvac_values[i].name);
293 if (json_object_object_get_ex(query, hvac_values[i].name, &val))
295 DEBUG(interface, "We got it. Tests if it is an int or not.");
296 if (!json_object_is_type(val, json_type_int))
298 afb_req_fail_f(request, "bad-request",
299 "argument '%s' isn't integer", hvac_values[i].name);
302 DEBUG(interface, "We get an 'int'. Hail for the int: %d", x);
303 x = json_object_get_int(val);
304 if (x < 0 || x > 255)
306 afb_req_fail_f(request, "bad-request",
307 "argument '%s' is out of bounds", hvac_values[i].name);
310 if (values[i] != x) {
311 values[i] = (uint8_t)x;
315 DEBUG(interface, "Not found !");
318 /* attemps to set new values */
319 DEBUG(interface, "Diff: %d", changed);
322 i = (int)(sizeof hvac_values / sizeof *hvac_values);
325 hvac_values[i].value = values[i];
329 afb_req_success(request, NULL, NULL);
331 /* restore initial values */
332 i = (int)(sizeof hvac_values / sizeof *hvac_values);
335 hvac_values[i].value = saves[i];
337 afb_req_fail(request, "error", "CAN error");
341 afb_req_success(request, NULL, "No changes");
345 // TODO: Have to change session management flag to AFB_SESSION_CHECK to use token auth
346 static const struct afb_verb_desc_v1 verbs[]= {
347 {"get_temp_left_zone" , AFB_SESSION_NONE, get_temp_left_zone , "Get the left zone temperature"},
348 {"get_temp_right_zone" , AFB_SESSION_NONE, get_temp_right_zone , "Get the right zone temperature"},
349 {"get_fanspeed" , AFB_SESSION_NONE, get_fanspeed , "Read fan speed"},
350 {"get" , AFB_SESSION_NONE, get , "Read all values"},
351 {"set" , AFB_SESSION_NONE, set , "Set a HVAC component value"},
355 static const struct afb_binding binding_desc = {
356 .type = AFB_BINDING_VERSION_1,
358 .info = "hvac service",
364 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
368 return &binding_desc;
371 int afbBindingV1ServiceInit(struct afb_service service)
373 return open_can_dev();