2 * Copyright (c) 2007, IRTrans GmbH
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of IRTrans GmbH nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY IRTrans GmbH ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL IRTrans GmbH BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/types.h>
39 #include <sys/timeb.h>
43 #define MSG_NOSIGNAL 0
54 #define MSG_NOSIGNAL 0
63 #include <sys/types.h>
64 #include <sys/socket.h>
65 #include <netinet/in.h>
68 #include <arpa/inet.h>
81 #define closesocket close
83 #define _getpid getpid
95 #include "webserver.h"
100 time_t xAP_last_hbeat
;
106 // target=IRTrans.irserver.<bus>:<ID>.<LED>
107 // Internal LEDs: target=IRTrans.irserver.<bus>:<ID>:int
108 // External LEDs: target=IRTrans.irserver.<bus>:<ID>:ext
111 void xAP_SendHeartbeat (void)
116 if (!(mode_flag
& XAP
)) return;
118 for (i
=0;i
<device_cnt
;i
++) {
119 sprintf (st
,"xap-hbeat\n{\nv=%d\nhop=1\nuid=%s00\nclass=xap-hbeat.alive\nsource=%s.%d\ninterval=%d\nport=%d\npid=%d\n}\n",
120 XAP_VERSION
,XAP_UID
,XAP_ADR
,i
,XAP_HBEAT
,xAP_rcv_port
,_getpid ());
123 xAP_last_hbeat
= time (0);
128 void xAP_SendIREvent (char remote
[],char command
[],int bus
,int adr
)
132 if (!(mode_flag
& XAP
)) return;
134 sprintf (st
,"xap-header\n{\nv=%d\nhop=1\nuid=%s%02x\nclass=ir.receive\nsource=%s.%d:%d\n}\nIR.Signal\n{\ndevice=%s\nsignal=%s\n}\n",
135 XAP_VERSION
,XAP_UID
,adr
+1,XAP_ADR
,bus
,adr
,remote
,command
);
141 void xAP_EventReceived (void)
146 res
= recv(xAP_rcv
,data
,1500,MSG_NOSIGNAL
);
147 if (res
<= 0) return;
151 xAP_ProcessHeader (data
);
154 void xAP_SendData (char data
[])
156 send (xAP_send
,data
,(int)strlen (data
),MSG_NOSIGNAL
);
161 void xAP_ProcessHeader (char data
[])
166 int p
,q
,pos
,res
,bus
,mask
,led
;
167 char line
[256],err
[256],txt
[256];
172 if (strncmp (data
,"xap-header",10)) return;
174 if (xAP_GetItem (data
,"class=",line
) <= 0 || strcmp (line
,"ir.transmit")) return;
181 if (xAP_GetItem (data
,"target=",line
) > 0) {
182 if (xAP_GetItem (data
,"target=irtrans.irserver.",line
) <= 0) return;
185 while (line
[pos
] && line
[pos
] != ':') pos
++;
190 if (line
[0] == '*') {
195 adr
= atoi (line
) << 20;
201 while (line
[pos
] && line
[pos
] != '.') pos
++;
206 if (line
[p
] == '*') {
211 adr
|= 0x10000 | (1 << atoi (line
+p
));
212 mask
= (1 << atoi (line
+p
));
218 if (line
[pos
] == '*') {
222 else if (line
[pos
] == 'i') {
226 else if (line
[pos
] == 'e') {
233 if ((pos
= xAP_GetItem (data
,"ir.signal",line
)) > 0) {
235 if (xAP_GetItem (data
+pos
,"device=",remote
) <= 0) return;
236 if (xAP_GetItem (data
+pos
,"signal=",command
) <= 0) return;
238 res
= DBFindRemoteCommand (remote
,command
,&cmd_num
,NULL
);
239 if (!res
) res
= SendIR (cmd_num
,adr
);
243 case ERR_REMOTENOTFOUND
:
244 sprintf (err
, txt
, remote
);
246 case ERR_COMMANDNOTFOUND
:
247 sprintf (err
, txt
, command
);
250 sprintf (err
,txt
,(adr
>> 20) & (MAX_IR_DEVICES
- 1));
256 log_print (err
, LOG_ERROR
);
259 sprintf (txt
,"xAP send: %s-%s [%x]\n", remote
,command
,adr
);
260 log_print (txt
,LOG_DEBUG
);
264 if ((pos
= xAP_GetItem (data
,"ir.pronto",line
)) > 0) {
266 if (xAP_GetItem (data
+pos
,"ir=",remote
) <= 0) return;
268 res
= DecodeCCF (remote
,&ird
,START
);
270 sprintf (err
,"Illegal xAP Pronto command\n");
271 log_print (err
, LOG_ERROR
);
276 ird
.target_mask
= mask
;
278 DoSendIR (&ird
,NULL
,0,0,bus
);
283 int xAP_GetItem (char data
[],char item
[],char value
[])
289 i
= xAP_GetLine (data
,line
,i
);
290 ConvertLcase (line
,(int)strlen (line
));
291 if (!memcmp (line
,item
,strlen (item
))) {
292 strcpy (value
,line
+ strlen (item
));
301 int xAP_GetLine (char data
[],char line
[],int pos
)
306 while (data
[pos
] == 10 || data
[pos
] == 13) pos
++;
308 if (!data
[pos
]) return (-1);
311 while (data
[pos
] && data
[pos
] != 10 && data
[pos
] != 13) pos
++;
314 memcpy (line
,data
+p
,pos
-p
);