1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
/*
* Copyright (C) 2015, 2016 "IoT.bzh"
* Author "Manuel Bachmann"
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _GNU_SOURCE
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/can.h>
#include <json-c/json.h>
#include <afb/afb-binding.h>
#define CAN_DEV "can0"
static const struct afb_binding_interface *interface;
// Initialize CAN hvac array that will be sent trough the socket
static char *can_hvac_components[8] = {
"LeftTemperature",
"RightTemperature",
"Temperature",
"Unknow3",
"FanSpeed",
"Unknow5",
"Unknow6",
"Unknow7"
};
// Initialize CAN hvac array that will be sent trough the socket
static __u8 can_hvac_values[8] = {
21,
21,
21,
240, // Don't know why 240 but it was 0xF0 in the original amb hvacplugin
0,
1, // Don't know why 1 but it was 0xF0 in the original amb hvacplugin
0, // Don't know why 0 but it was 0xF0 in the original amb hvacplugin
0 // Don't know why 0 but it was 0xF0 in the original amb hvacplugin
};
struct can_handler {
int socket;
struct sockaddr_can txAddress;
};
/*****************************************************************************************/
/*****************************************************************************************/
/** **/
/** **/
/** SECTION: HANDLE CAN DEVICE **/
/** **/
/** **/
/*****************************************************************************************/
/*****************************************************************************************/
static struct can_handler open_can_dev()
{
struct can_handler ch;
ch.socket = -1;
ch.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (ch.socket < 0)
{
ERROR(interface, "socket could not be created");
}
else
{
struct ifreq ifr;
// Attemts to open a socket to CAN bus
strcpy(ifr.ifr_name, CAN_DEV);
if(ioctl(ch.socket, SIOCGIFINDEX, &ifr) < 0)
{
close(ch.socket);
ch.socket = -1;
ERROR(interface, "ioctl failed");
}
else
{
ch.txAddress.can_family = AF_CAN;
ch.txAddress.can_ifindex = ifr.ifr_ifindex;
// And bind it to ch.txAddress struct
if (bind(ch.socket, (struct sockaddr *)&ch.txAddress, sizeof(ch.txAddress)) < 0)
{
close(ch.socket);
ch.socket = -1;
ERROR(interface, "bind failed");
}
return ch;
}
}
ERROR(interface, "unexpected behavior");
return ch;
}
static void close_can_dev(struct can_handler ch)
{
int socketId = ch.socket;
if(socketId >= 0)
{
close(socketId);
}
}
// Get original get temperature function from cpp hvacplugin code
static uint8_t get_temperature(uint8_t value)
{
uint8_t result = ((0xF0 - 0x10) / 15) * value - 16;
if (result < 0x10)
result = 0x10;
if (result > 0xF0)
result = 0xF0;
return result;
}
static void write_can(struct can_handler ch)
{
// Hardcoded can_id and dlc (data lenght code)
struct can_frame txCanFrame;
txCanFrame.can_id = 0x30;
txCanFrame.can_dlc = 8;
if (ch.socket >= 0)
{
txCanFrame.data[0] = get_temperature(can_hvac_values[0]);
txCanFrame.data[1] = get_temperature(can_hvac_values[1]);
txCanFrame.data[2] = get_temperature((can_hvac_values[0] + can_hvac_values[1]) & 0x02);
txCanFrame.data[3] = can_hvac_values[3];
txCanFrame.data[4] = can_hvac_values[4];
txCanFrame.data[5] = can_hvac_values[5];
txCanFrame.data[6] = can_hvac_values[6];
txCanFrame.data[7] = can_hvac_values[7];
sendto(ch.socket, &txCanFrame, sizeof(struct can_frame), 0,
(struct sockaddr*)&ch.txAddress, sizeof(ch.txAddress));
return;
}
else
{
ERROR(interface, "socket not initialized");
return;
}
ERROR(interface, "sending on CAN bus. Unexpected behavior");
}
static __u8 read_temp_left_zone()
{
return can_hvac_values[0];
}
static __u8 read_temp_right_zone()
{
return can_hvac_values[1];
}
static __u8 read_fanspeed()
{
return can_hvac_values[4];
}
static uint8_t make_atoi(const char *str)
{
return str ? atoi(str) : 0;
}
/*****************************************************************************************/
/*****************************************************************************************/
/** **/
/** **/
/** SECTION: BINDING VERBS IMPLEMENTATION **/
/** **/
/** **/
/*****************************************************************************************/
/*****************************************************************************************/
/*
* @brief Simple afb-daemon ping
*
* @param struct afb_req : an afb request structure
*
*/
static void ping (struct afb_req request)
{
static int pingcount = 0;
json_object *query = afb_req_json(request);
afb_req_success_f(request, NULL, "Ping Binder Daemon count=%d query=%s", ++pingcount, json_object_to_json_string(query));
}
/*
* @brief Get fan speed HVAC system
*
* @param struct afb_req : an afb request structure
*
*/
static void get_fanspeed(struct afb_req request)
{
json_object *ret_json;
__u8 fanspeed = read_fanspeed();
ret_json = json_object_new_object();
json_object_object_add(ret_json, "FanSpeed", json_object_new_int(fanspeed));
json_object *query = afb_req_json(request);
afb_req_success_f(request, ret_json, "Fan Speed is:%d query=%s", fanspeed, json_object_to_json_string(query));
}
/*
* @brief Read Consign right zone temperature for HVAC system
*
* @param struct afb_req : an afb request structure
*
*/
static void get_temp_right_zone(struct afb_req request)
{
json_object *ret_json;
__u8 temp = read_temp_right_zone();
ret_json = json_object_new_object();
json_object_object_add(ret_json, "RightTemperature", json_object_new_int(temp));
json_object *query = afb_req_json(request);
afb_req_success_f(request, ret_json, "Right zone Temperature is:%d query=%s", temp, json_object_to_json_string(query));
}
/*
* @brief Read Consign left zone temperature for HVAC system
*
* @param struct afb_req : an afb request structure
*
*/
static void get_temp_left_zone(struct afb_req request)
{
json_object *ret_json;
__u8 temp = read_temp_left_zone();
ret_json = json_object_new_object();
json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(temp));
json_object *query = afb_req_json(request);
afb_req_success_f(request, ret_json, "Left zone Temperature is:%d query=%s", temp, json_object_to_json_string(query));
}
/*
* @brief Read all values
*
* @param struct afb_req : an afb request structure
*
*/
static void get_all(struct afb_req request)
{
json_object *ret_json;
ret_json = json_object_new_object();
json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(read_temp_left_zone()));
json_object_object_add(ret_json, "RightTemperature", json_object_new_int(read_temp_right_zone()));
json_object_object_add(ret_json, "FanSpeed", json_object_new_int(read_fanspeed()));
afb_req_success(request, ret_json, NULL);
}
/*
* @brief Set a component value using a json object retrieved from request
*
* @param struct afb_req : an afb request structure
*
*/
static void set(struct afb_req request)
{
struct can_handler ch;
const char *val;
struct json_object *query;
struct json_object_iterator iter;
struct json_object_iterator iter_end;
query = afb_req_json(request);
iter = json_object_iter_begin(query);
iter_end = json_object_iter_end(query);
/*
* Loop over the json object that will set every component
* which it will find in it.
*/
int i;
while(!json_object_iter_equal(&iter, &iter_end))
{
const char *key = json_object_iter_peek_name(&iter);
for (i=0;i<8;i++)
{
if(strcmp(can_hvac_components[i], key) == 0)
{
val = afb_req_value(request, key);
uint8_t comp_val = make_atoi(val);
can_hvac_values[i] = comp_val;
break;
}
}
json_object_iter_next(&iter);
}
ch = open_can_dev();
write_can(ch);
close_can_dev(ch);
afb_req_success(request, query, "HVAC settings updated");
}
// TODO: Have to change session management flag to AFB_SESSION_CHECK to use token auth
static const struct afb_verb_desc_v1 verbs[]= {
{"ping" , AFB_SESSION_NONE, ping , "Ping the binder"},
{"get_temp_left_zone" , AFB_SESSION_NONE, get_temp_left_zone , "Get the left zone temperature"},
{"get_temp_right_zone" , AFB_SESSION_NONE, get_temp_right_zone , "Get the right zone temperature"},
{"get_fanspeed" , AFB_SESSION_NONE, get_fanspeed , "Read fan speed"},
{"get_all" , AFB_SESSION_NONE, get_all , "Read all values"},
{"set" , AFB_SESSION_NONE, set , "Set a HVAC component value"},
{NULL}
};
static const struct afb_binding binding_desc = {
.type = AFB_BINDING_VERSION_1,
.v1 = {
.info = "hvac hybrid service",
.prefix = "hvac",
.verbs = verbs
}
};
const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
{
interface = itf;
return &binding_desc;
}
|