-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibcgate.c
More file actions
461 lines (408 loc) · 13.9 KB
/
Copy pathlibcgate.c
File metadata and controls
461 lines (408 loc) · 13.9 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* libcgate - library to send and receve messages from Clipsal's C-Gate server.
* Copyright (C) 2017, Andrew Tarabaras.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*/
#include <sys/ioctl.h>
#include <sys/time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include "libcgate.h"
#define DEBUG
#if defined DEBUG
#define DEBUG_PRINT(fmt, ...) do { printf("%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__); } while (0)
#else
#define DEBUG_PRINT(fmt, ...) /* Don't do anything in release builds */
#endif
static void (*cgate_lighting_event_handler)(uint8_t,
uint8_t,
uint8_t,
uint8_t,
uint8_t);
static void (*cgate_measurement_event_handler)(uint8_t,
uint8_t,
uint8_t,
uint8_t,
int32_t,
int8_t,
cbus_measurement_unit_type);
void cgate_lighting_register_event_handler(void (*f)(uint8_t,
uint8_t,
uint8_t,
uint8_t,
uint8_t))
{
cgate_lighting_event_handler = f;
}
void cgate_measurement_register_handler(void (*f)(uint8_t,
uint8_t,
uint8_t,
uint8_t,
int32_t,
int8_t,
cbus_measurement_unit_type))
{
cgate_measurement_event_handler = f;
}
int8_t *get_cgate_value(int8_t *buf)
{
int32_t i;
static int8_t ret[32];
for(i=0; i<=255;i++){
if(buf[i]=='='){
buf +=i;
break;
}
}
if(*buf != '=')
return NULL;
for(i=0; i< 32; i++){
if(buf[i] == '\r'){
ret[i] = '\0';
return ret;
}
ret[i] = buf[i+1];
}
/* Should not get here */
return NULL;
}
int32_t cgate_get_ok(int sockfd)
{
int n;
int8_t buf[255];
while(1){
n = read(sockfd,buf,255);
buf[7] = '\0';
n = strncmp((const char*)buf,"200 OK", 6);
if(n==0)
return 0;
}
DEBUG_PRINT("ERROR sending to bus: %s n=%d\n", buf,n);
return 1;
}
int32_t cgate_text_label(int sockfd, uint8_t net, uint8_t app, uint8_t group, int8_t* value)
{
char buf[255];
/* hard code language 1 english */
sprintf(buf, "lighting label %d/%d 1 %d - text %s\r", net, app, group, value);
printf("BUFFER %s\n", buf);
if(write(sockfd,buf,strlen(buf)) < 0)
return -1;
if(!cgate_get_ok(sockfd))
return 0;
DEBUG_PRINT("Failed to set label\n");
return -1;
}
int32_t cgate_set_group(int sockfd, uint8_t net, uint8_t app, uint8_t group, uint8_t value)
{
char buf[255];
if(value == 0)
sprintf(buf,"OFF %d/%d/%d\r",net,app,group);
else
sprintf(buf,"ON %d/%d/%d\r",net,app,group);
if(write(sockfd,buf,strlen(buf)) < 0)
return -1;
if(!cgate_get_ok(sockfd))
return 0;
DEBUG_PRINT("Failed to set group\n");
return -1;
}
int32_t cgate_set_ramp(int sockfd, uint8_t net, uint8_t app, uint8_t group, uint8_t value, uint8_t ramprate)
{
char buf[255];
sprintf(buf, "ramp %d/%d/%d %d %d\r", net, app, group, value, ramprate);
if(write(sockfd,buf,strlen(buf)) < 0)
return -1;
if(!cgate_get_ok(sockfd))
return 0;
DEBUG_PRINT("Failed to set group\n");
return -1;
}
int32_t cgate_send_measurement(int sockfd, uint8_t net, uint8_t app, uint8_t device, uint8_t channel, int32_t value, int8_t exponent, cbus_measurement_unit_type units)
{
char buf[255];
sprintf(buf, "measurement data %d/%d/%d/%d %d %d %d\r", net, app, device, channel, value, exponent, units);
if(write(sockfd,buf,strlen(buf)) < 0)
return -1;
if(!cgate_get_ok(sockfd))
return 0;
DEBUG_PRINT("Failed to set group\n");
return -1;
}
void cgate_process_lighting_event(uint8_t *buf)
{
char *ret, *ptr;
long val;
u_int8_t value, net, group, application, ramp;
char project[32];
int i;
if(strstr((const char*)buf,"lighting on") != NULL){
DEBUG_PRINT("On \n");
value = 255;
ret = strstr((const char*)buf,"//");
i = strstr((const char*)ret+2,"/") - (ret+2);
memcpy(project,ret+2,i);
project[i] = '\0';
DEBUG_PRINT("PROJECT = %s\n",project);
ret += (i+3);
val = strtol(ret,&ptr,10);
net = (u_int8_t)val;
DEBUG_PRINT("NETWORK = %d\n",(int)val);
ret = ptr+1;
val = strtol(ret,&ptr,10);
application = (u_int8_t)val;
DEBUG_PRINT("APP = %d\n",(int)val);
ret = ptr+1;
val = strtol((const char*)ret,&ptr,10);
group = (u_int8_t)val;
DEBUG_PRINT("GROUP = %d\n",(int)val);
ramp = 0;
if((void (*)(void))cgate_lighting_event_handler)
cgate_lighting_event_handler(net,application,group,value,ramp);
}
if(strstr((const char*)buf,"lighting off") != NULL){
DEBUG_PRINT("Off \n");
value = 0;
ret = strstr((const char*)buf,"//");
i = strstr((const char*)ret+2,"/") - (ret+2);
memcpy(project,ret+2,i);
project[i] = '\0';
DEBUG_PRINT("PROJECT = %s\n",project);
ret += (i+3);
val = strtol(ret,&ptr,10);
net = (u_int8_t)val;
DEBUG_PRINT("NETWORK = %d\n",(int)val);
ret = ptr+1;
val = strtol(ret,&ptr,10);
application = (u_int8_t)val;
DEBUG_PRINT("APP = %d\n",(int)val);
ret = ptr+1;
val = strtol((const char*)ret,&ptr,10);
group = (u_int8_t)val;
DEBUG_PRINT("GROUP = %d\n",(int)val);
ramp = 0;
if((void (*)(void))cgate_lighting_event_handler)
cgate_lighting_event_handler(net,application,group,value,ramp);
}
if(strstr((const char*)buf,"lighting ramp") != NULL){
DEBUG_PRINT("Ramp \n");
ret = strstr((const char*)buf,"//");
i = strstr((const char*)ret+2,"/") - (ret+2);
memcpy(project,ret+2,i);
project[i] = '\0';
DEBUG_PRINT("PROJECT = %s\n",project);
ret += (i+3);
val = strtol(ret,&ptr,10);
net = (u_int8_t)val;
DEBUG_PRINT("NETWORK = %d\n",(int)val);
ret = ptr+1;
val = strtol(ret,&ptr,10);
application = (u_int8_t)val;
DEBUG_PRINT("APP = %d\n",(int)val);
ret = ptr+1;
val = strtol((const char*)ret,&ptr,10);
group = (u_int8_t)val;
DEBUG_PRINT("GROUP = %d\n",(int)val);
ret = strstr((const char*)ret," ");
ret++;
val = strtol((const char*)ret,&ptr,10);
value = (u_int8_t)val;
DEBUG_PRINT("RAMP = %d\n",(int)val);
ret = strstr((const char*)ret," ");
ret++;
val = strtol((const char*)ret,&ptr,10);
ramp = (u_int8_t)val;
DEBUG_PRINT("RAMP RATE = %d\n",(int)val);
if((void (*)(void))cgate_lighting_event_handler)
cgate_lighting_event_handler(net,application,group,value,ramp);
}
}
void cgate_process_measurement_event(uint8_t *buf)
{
char *ret, *ptr;
long val;
int8_t exponent;
u_int8_t net, device, channel, application;
char project[32];
int32_t i, value;
cbus_measurement_unit_type type;
if(strstr((const char*)buf,"measurement data") != NULL){
DEBUG_PRINT("Measurement Data \n");
ret = strstr((const char*)buf,"//");
i = strstr((const char*)ret+2,"/") - (ret+2);
memcpy(project,ret+2,i);
project[i] = '\0';
DEBUG_PRINT("PROJECT = %s\n",project);
ret += (i+3);
val = strtol(ret,&ptr,10);
net = (u_int8_t)val;
DEBUG_PRINT("NETWORK = %d\n",(int)val);
ret = ptr+1;
val = strtol(ret,&ptr,10);
application = (u_int8_t)val;
DEBUG_PRINT("APP = %d\n",(int)val);
ret = ptr+1;
val = strtol((const char*)ret,&ptr,10);
device = (u_int8_t)val;
DEBUG_PRINT("DEVICE = %d\n",(int)val);
ret = ptr+1;
val = strtol((const char*)ret,&ptr,10);
channel = (u_int8_t)val;
DEBUG_PRINT("CHANNEL = %d\n",(int)val);
ret = strstr((const char*)ret," ");
ret++;
val = strtol((const char*)ret,&ptr,10);
value = (int32_t)val;
DEBUG_PRINT("VALUE = %d\n",(int)val);
ret = strstr((const char*)ret," ");
ret++;
val = strtol((const char*)ret,&ptr,10);
exponent = (int8_t)val;
DEBUG_PRINT("EXPONENT = %d\n",(int)val);
ret = strstr((const char*)ret," ");
ret++;
val = strtol((const char*)ret,&ptr,10);
type = (u_int8_t)val;
DEBUG_PRINT("TYPE = %d\n",(int)val);
if((void (*)(void))cgate_measurement_event_handler)
cgate_measurement_event_handler(net,application,device,channel,value,exponent,type);
}
}
void cgate_process_event(uint8_t *buf)
{
if(strstr((const char*)buf,"lighting") != NULL)
cgate_process_lighting_event(buf);
if(strstr((const char*)buf,"measurement") != NULL)
cgate_process_measurement_event(buf);
}
void cgate_receive_character(uint8_t rx)
{
static uint8_t buf[255];
static uint8_t index=0;
if(rx == '\n')
return;
if(rx == '\r')
{
buf[index] = '\0';
DEBUG_PRINT("Event Buffer: %s\n",buf);
cgate_process_event(buf);
index=0;
memset(buf, 0, sizeof(buf));
}
else
{
buf[index] = rx;
index++;
}
}
void *cgate_event(void *eventfd)
{
int i, bytes_read;
uint8_t buf[255];
while(1){
bytes_read = read((*(int*)eventfd), buf, 255);
for(i = 0; i < bytes_read; i++)
cgate_receive_character(buf[i]);
}
}
int cgate_connect(char* ip, int portno, int8_t *project, uint8_t net)
{
static int sockfd, eventfd;
char buf[255];
struct sockaddr_in serverAddr, serverAddr2;
pthread_t sig_thr_id;
/* Close sockets if doing a reconnect */
if(sockfd)
close(sockfd);
if(eventfd)
close(eventfd);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
DEBUG_PRINT("ERROR opening socket");
eventfd = socket(AF_INET, SOCK_STREAM, 0);
if (eventfd < 0)
DEBUG_PRINT("ERROR opening socket");
/*---- Configure settings of the server address struct ----*/
/* Address family = Internet */
serverAddr.sin_family = AF_INET;
serverAddr2.sin_family = AF_INET;
/* Set port number, using htons function to use proper byte order */
serverAddr.sin_port = htons(portno);
serverAddr2.sin_port = htons(portno+2);
/* Set IP address to localhost */
serverAddr.sin_addr.s_addr = inet_addr(ip); /* IP address */
serverAddr2.sin_addr.s_addr = inet_addr(ip);
/* Set all bits of the padding field to 0 */
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
memset(serverAddr2.sin_zero, '\0', sizeof serverAddr2.sin_zero);
if (connect(sockfd,(struct sockaddr *)&serverAddr,sizeof(serverAddr)) < 0)
return -1;
if (connect(eventfd,(struct sockaddr *)&serverAddr2,sizeof(serverAddr2)) < 0)
return -1;
if(read(sockfd,buf,255) < 0){
DEBUG_PRINT("Socket read error\n");
return -1;
}
if(!strncmp((const char*)buf,"201 Service ready",17))
DEBUG_PRINT("Connected to C-Gate\n");
else{
DEBUG_PRINT("Could not connected to C-Gate\n");
return -1;
}
/* Load the project */
sprintf(buf, "project load %s\r", project);
if(write(sockfd,buf,strlen(buf)) < 0)
DEBUG_PRINT("Write to socket error\n");
if(!cgate_get_ok(sockfd))
DEBUG_PRINT("Project load=%s\n", project);
else
DEBUG_PRINT("Can not load project\n");
/* Use The project */
sprintf(buf, "project use %s\r", project);
if(write(sockfd,buf,strlen(buf)) < 0)
DEBUG_PRINT("Write to socket error\n");
if(!cgate_get_ok(sockfd))
DEBUG_PRINT("Project Use = %s\n", project);
else
DEBUG_PRINT("Can not use project\n");
/* Open the network */
sprintf(buf, "net open %d\r",net);
if(write(sockfd,buf,strlen(buf)) < 0)
DEBUG_PRINT("Write to socket error\n");
if(!cgate_get_ok(sockfd))
DEBUG_PRINT("Network load=%d\n", net);
else
DEBUG_PRINT("Can not load project\n");
/* Start the project */
sprintf(buf, "project start\r");
if(write(sockfd,buf,strlen(buf)) < 0)
DEBUG_PRINT("Write to socket error\n");
if(!cgate_get_ok(sockfd))
DEBUG_PRINT("Project started=%s\n", project);
else
DEBUG_PRINT("Can not start project\n");
if(pthread_create (&sig_thr_id, NULL, cgate_event, &eventfd))
return -1;
return sockfd;
}