Mostly minor fixes up until version 0.8.10.
[irreco.git] / irtrans / irserver / src / xap.c
blobfe0b03d72e9574bc81bc5fabf963433c8237c151
1 /*
2 * Copyright (c) 2007, IRTrans GmbH
3 * All rights reserved.
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.
30 #ifdef WIN32
32 #include <winsock2.h>
33 #include <windows.h>
34 #include <winuser.h>
35 #include <io.h>
36 #include <direct.h>
37 #include <sys/types.h>
38 #include <time.h>
39 #include <sys/timeb.h>
40 #include <process.h>
41 #include <string.h>
43 #define MSG_NOSIGNAL 0
45 #endif
47 #ifdef WINCE
49 #include <winsock2.h>
50 #include <windows.h>
51 #include <winuser.h>
52 #include <time.h>
54 #define MSG_NOSIGNAL 0
56 #endif
58 #include <stdio.h>
59 #include <stdlib.h>
61 #ifdef LINUX
62 #include <time.h>
63 #include <sys/types.h>
64 #include <sys/socket.h>
65 #include <netinet/in.h>
66 #include <sys/un.h>
67 #include <sys/time.h>
68 #include <arpa/inet.h>
69 #include <dirent.h>
70 #include <sys/stat.h>
71 #include <errno.h>
72 #include <fcntl.h>
73 #include <signal.h>
74 #include <netdb.h>
75 #include <stdint.h>
77 typedef int SOCKET;
78 typedef int DWORD;
79 typedef int WSAEVENT;
81 #define closesocket close
83 #define _getpid getpid
85 #endif
87 #include "remote.h"
88 #include "dbstruct.h"
89 #include "network.h"
90 #include "errcode.h"
91 #include "fileio.h"
92 #include "lowlevel.h"
93 #include "server.h"
94 #include "global.h"
95 #include "webserver.h"
96 #include "flash.h"
97 #include "xap.h"
98 #include "ccf.h"
100 time_t xAP_last_hbeat;
101 int xAP_rcv_port;
102 SOCKET xAP_rcv;
103 SOCKET xAP_send;
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)
113 int i;
114 char st[1500];
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 ());
121 xAP_SendData (st);
123 xAP_last_hbeat = time (0);
128 void xAP_SendIREvent (char remote[],char command[],int bus,int adr)
130 char st[1500];
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);
137 xAP_SendData (st);
141 void xAP_EventReceived (void)
143 int res;
144 char data[1500];
146 res = recv(xAP_rcv,data,1500,MSG_NOSIGNAL);
147 if (res <= 0) return;
149 data[res] = 0;
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[])
163 IRDATA ird;
164 int cmd_num;
165 unsigned int adr;
166 int p,q,pos,res,bus,mask,led;
167 char line[256],err[256],txt[256];
169 char remote[1500];
170 char command[512];
172 if (strncmp (data,"xap-header",10)) return;
174 if (xAP_GetItem (data,"class=",line) <= 0 || strcmp (line,"ir.transmit")) return;
176 adr = 0;
177 bus = 0;
178 mask = 0xffff;
179 led = 0;
181 if (xAP_GetItem (data,"target=",line) > 0) {
182 if (xAP_GetItem (data,"target=irtrans.irserver.",line) <= 0) return;
184 pos = 0;
185 while (line[pos] && line[pos] != ':') pos++;
187 p = line[pos];
188 line[pos] = 0;
190 if (line[0] == '*') {
191 adr = 0x40000000;
192 bus = 255;
194 else {
195 adr = atoi (line) << 20;
196 bus = atoi (line);
199 if (p) {
200 p = ++pos;
201 while (line[pos] && line[pos] != '.') pos++;
203 q = line[pos];
204 line[pos] = 0;
206 if (line[p] == '*') {
207 adr |= 0x1ffff;
208 mask = 0xffff;
210 else {
211 adr |= 0x10000 | (1 << atoi (line+p));
212 mask = (1 << atoi (line+p));
216 if (q) {
217 pos++;
218 if (line[pos] == '*') {
219 adr |= 0x60000;
220 led = 3;
222 else if (line[pos] == 'i') {
223 adr |= 0x20000;
224 led = 1;
226 else if (line[pos] == 'e') {
227 adr |= 0x40000;
228 led = 2;
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);
240 if (res) {
241 GetError (res, txt);
242 switch(res) {
243 case ERR_REMOTENOTFOUND:
244 sprintf (err, txt, remote);
245 break;
246 case ERR_COMMANDNOTFOUND:
247 sprintf (err, txt, command);
248 break;
249 case ERR_WRONGBUS:
250 sprintf (err,txt,(adr >> 20) & (MAX_IR_DEVICES - 1));
251 break;
252 default:
253 sprintf (err, txt);
254 break;
256 log_print (err, LOG_ERROR);
257 return;
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);
269 if (res <= 0) {
270 sprintf (err,"Illegal xAP Pronto command\n");
271 log_print (err, LOG_ERROR);
272 return;
275 ird.address = led;
276 ird.target_mask = mask;
278 DoSendIR (&ird,NULL,0,0,bus);
283 int xAP_GetItem (char data[],char item[],char value[])
285 int i = 0;
286 char line[256];
288 do {
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));
293 return (i);
295 } while (i != -1);
297 return (0);
301 int xAP_GetLine (char data[],char line[],int pos)
303 int p;
305 line[0] = 0;
306 while (data[pos] == 10 || data[pos] == 13) pos++;
308 if (!data[pos]) return (-1);
310 p = pos;
311 while (data[pos] && data[pos] != 10 && data[pos] != 13) pos++;
314 memcpy (line,data+p,pos-p);
315 line[pos-p] = 0;
317 return (pos);