Mostly minor fixes up until version 0.8.10.
[irreco.git] / irtrans / irserver / src / lowlevel.c
blobc616ec98e1fedd529a97847033e6d610c2e605e9
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
31 #include "winsock2.h"
32 #include <windows.h>
33 #include <io.h>
34 #include <direct.h>
35 #include <sys/types.h>
36 #include <time.h>
37 #include <sys/timeb.h>
39 #define MSG_NOSIGNAL 0
40 #endif
42 #ifdef WINCE
43 #include "winsock2.h"
44 #include <windows.h>
45 #include <time.h>
46 #endif
48 #include <stdio.h>
50 #ifdef LINUX
51 #include <stdlib.h>
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <sys/un.h>
56 #include <arpa/inet.h>
57 #include <dirent.h>
58 #include <sys/stat.h>
59 #include <errno.h>
60 #include <fcntl.h>
61 #include <signal.h>
62 #include <stdint.h>
63 #include <sys/time.h>
65 typedef int SOCKET;
66 typedef int DWORD;
67 typedef int WSAEVENT;
68 #define closesocket close
69 #endif
72 #include "remote.h"
73 #include "errcode.h"
74 #include "network.h"
75 #include "dbstruct.h"
76 #include "lowlevel.h"
77 #include "fileio.h"
78 #include "global.h"
79 #include "flash.h"
81 #ifdef WIN32
82 #include "winio.h"
83 #include "winusbio.h"
84 BOOL WINAPI ShutdownHandler (DWORD type);
85 extern WSAEVENT IrtLanEvent;
86 #endif
88 #define RESEND_WAIT_SER 5
89 #define RESEND_WAIT_USB 10
92 byte byteorder;
93 extern STATUS_BUFFER remote_statusex[MAX_IR_DEVICES];
94 extern byte new_lcd_flag;
95 extern int display_bus;
96 extern SOCKET irtlan_socket;
97 int OpenIRTransLANSocket (void);
98 int OpenIRTransBroadcastSockets (void);
99 void DecodeFunctions (DEVICEINFO *dev);
100 extern SOCKET irtlan_outbound;
101 extern int protocol_version;
102 extern char pidfile[256];
104 byte DispConvTable[TABLE_CNT][256];
106 #define FN_IR 1
107 #define FN_SBUS 2
108 #define FN_SER 4
109 #define FN_USB 8
110 #define FN_POWERON 16
111 #define FN_B_O 32
112 #define FN_TEMP 64
113 #define FN_SOFTID 128
114 #define FN_EEPROM 256
115 #define FN_TRANSL 512
116 #define FN_HWCARR 1024
117 #define FN_DUALRCV 2048
119 int IRTransLanFlash (DEVICEINFO *dev,IRDATA_LAN_FLASH *ird,int len,uint32_t ip);
120 void SwapWordN (word *pnt);
121 void SwapIntN (int32_t *pnt);
125 int SendSerialBlock (int bus,byte data[],byte len)
127 IRDATA_LAN_FLASH irdf;
129 if (IRDevices[bus].io.if_type != IF_LAN || !(IRDevices[bus].fw_capabilities & FN_SER)) return (ERR_NO_RS232);
131 irdf.adr = 0;
132 irdf.len = len;
133 SwapWordN (&irdf.len);
135 memcpy (irdf.data,data,len);
136 irdf.netcommand = COMMAND_SEND_RS232;
137 #ifdef WIN32
138 IRTransLanFlash (IRDevices+bus,&irdf,len,IRDevices[bus].io.IPAddr[0].sin_addr.S_un.S_addr);
139 #else
140 IRTransLanFlash (IRDevices+bus,&irdf,len,IRDevices[bus].io.IPAddr[0].sin_addr.s_addr);
141 #endif
142 msSleep (30);
144 return (0);
148 void GetBusList (REMOTEBUFFER *buf,int offset)
150 int i;
151 memset (buf,0,sizeof (REMOTEBUFFER));
152 buf->statustype = STATUS_BUSLIST;
153 buf->statuslen = sizeof (REMOTEBUFFER);
154 buf->offset = (short)offset;
156 i = 0;
157 while (i < 40 && offset < device_cnt) {
158 memset (buf->remotes[i].name,0,80);
159 sprintf (buf->remotes[i].name,"%3d: %s [%s]",offset,IRDevices[offset].name,IRDevices[offset].device_node);
160 buf->remotes[i].source_mask = 0;
161 buf->remotes[i].target_mask = 0;
162 i++;
163 offset++;
166 buf->count_buffer = i;
167 buf->count_total = (word)device_cnt;
168 if (i == 40) buf->count_remaining = (short)(device_cnt - offset);
169 else buf->count_remaining = 0;
175 int SetTransceiverModusEx2 (int bus,byte addr,char *hotcode,int hotlen)
177 MODE_BUFFER md;
178 byte res;
179 md.sbus_command = HOST_SET_MODE2;
180 md.sbus_address = 0;
181 if (addr <= 15) md.sbus_address |= addr;
182 if (addr == 'L') md.sbus_address |= ADRESS_LOCAL;
183 if (addr == '*') md.sbus_address |= ADRESS_ALL;
185 md.hotcode_len = hotlen;
186 memcpy (md.hotcode,hotcode,hotlen);
188 md.sbus_len = sizeof (MODE_BUFFER) + md.hotcode_len - CODE_LENRAW;
190 res = WriteTransceiverEx (IRDevices + bus,(IRDATA *)&md);
191 if (res) return (res);
193 return (0);
196 int SetPowerLED (int bus,byte mode,byte val)
198 byte res;
199 IRDATA ird;
201 ird.command = SET_POWER_LED;
202 ird.len = 6;
203 ird.address = mode;
204 ird.target_mask = val;
206 res = WriteTransceiverEx (IRDevices + bus,&ird);
207 if (res) return (res);
209 return (0);
213 int SetTransceiverModusEx (int bus,byte mode,word send_mask,byte addr,char *hotcode,int hotlen,byte extended_mode,byte extended_mode2,byte extended_mode3,byte extended_mode4,byte *mac)
215 MODE_BUFFER md;
216 byte res;
217 md.sbus_command = HOST_SETMODE;
218 md.sbus_address = 0;
219 if (addr <= 15) md.sbus_address |= addr;
220 if (addr == 'L') md.sbus_address |= ADRESS_LOCAL;
221 if (addr == '*') md.sbus_address |= ADRESS_ALL;
223 md.hotcode_len = hotlen;
224 memcpy (md.hotcode,hotcode,hotlen);
226 md.hotcode[hotlen] = extended_mode;
227 md.hotcode[hotlen+1] = extended_mode2;
228 md.hotcode[hotlen+2] = extended_mode3;
229 md.hotcode[hotlen+3] = extended_mode4;
231 md.sbus_len = sizeof (MODE_BUFFER) + md.hotcode_len - (CODE_LENRAW - 4);
233 if (mac && (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5])) {
234 memcpy (md.hotcode+hotlen+4,mac,6);
235 md.sbus_len += 6;
238 md.mode = mode;
239 md.target_mask = send_mask;
241 res = WriteTransceiverEx (IRDevices + bus,(IRDATA *)&md);
242 if (res) return (res);
244 return (0);
247 int rcv_status_timeout (int timeout,uint32_t ip);
249 int TransferFlashdataEx (int bus,word data[],int adr,int len,byte active,int iradr)
251 IRDATA md;
252 int res,cnt;
253 int sl;
254 unsigned int cap;
256 if (bus > MAX_IR_DEVICES) bus = 0; // Fix für Kompatibilität mit Clients < V5.0
257 if (iradr == IRDevices[bus].my_addr) cap = IRDevices[bus].fw_capabilities;
258 else cap = remote_statusex[bus].stat[iradr].capabilities;
260 if ((cap & FN_EEPROM) == 0) {
261 fprintf (stderr,"No IRTrans with EEPROM connected\n\n");
262 return (-1);
265 if (len > 128) {
266 fprintf (stderr,"Maximum Transfer length is 128 Bytes [%d]\n\n",len);
267 return (-1);
270 md.command = SET_TRANSLATE_DATA;
271 md.address = (byte)(iradr & 0xff);
272 md.target_mask = adr;
273 md.ir_length = len;
274 md.mode = active;
275 md.len = (sizeof (IRDATA) - CODE_LEN) + md.ir_length;
277 memcpy (md.data,data,len);
279 if (time_len != TIME_LEN) ConvertToIRTRANS3 (&md);
281 if (IRDevices[bus].io.if_type == IF_LAN) {
282 res = cnt = 0;
283 while (res != COMMAND_FLASH_ACK && res != COMMAND_FLASH_ACK_1 && res != COMMAND_FLASH_ACK_2 && cnt < 10) {
284 res = WriteTransceiverEx (IRDevices + bus,&md);
285 if (res) return (res);
286 #ifdef WIN32
287 res = rcv_status_timeout (500,IRDevices[bus].io.IPAddr[0].sin_addr.S_un.S_addr);
288 #else
289 res = rcv_status_timeout (500,IRDevices[bus].io.IPAddr[0].sin_addr.s_addr);
290 #endif
291 cnt++;
293 if (cnt == 10) fprintf (stderr,"Flash Error !\n");
296 else {
297 res = WriteTransceiverEx (IRDevices + bus,&md);
298 if (res) return (res);
300 if ((iradr & 0xff) != IRDevices[bus].my_addr) {
301 if (remote_statusex[bus].stat[iradr & 0xff].extended_mode & SBUS_UART) {
302 sl = 250;
303 if ((remote_statusex[bus].stat[iradr & 0xff].extended_mode2 & SBUS_BAUD_MASK) == 0) sl = 400;
304 else if ((remote_statusex[bus].stat[iradr & 0xff].extended_mode2 & SBUS_BAUD_MASK) == 16) sl = 150;
305 else if ((remote_statusex[bus].stat[iradr & 0xff].extended_mode2 & SBUS_BAUD_MASK) == 24) sl = 80;
307 else sl = 400;
308 msSleep (sl);
314 return (0);
317 int ReadFlashdataEx (int bus,int adr)
319 IRDATA md;
320 byte res;
321 int offset = 0,len = 128;
322 byte databuffer[132];
325 if ((IRDevices[bus].fw_capabilities & FN_EEPROM) == 0) {
326 fprintf (stderr,"No IRTrans with EEPROM connected\n\n");
327 return (-1);
331 md.command = 202;
332 md.address = 0;
333 md.target_mask = adr / 2;
334 md.ir_length = len;
335 md.len = 20;
337 res = WriteTransceiverEx (IRDevices + bus,&md);
338 if (res) return (res);
340 msSleep (500);
341 offset = 2;
342 len = recv (irtlan_socket,databuffer,129,0);
343 // res = ReadIRStringEx (IRDevices+bus,databuffer,len,2000);
344 // if (res != len) return (-1);
346 printf ("%04x ",0);
347 for (res=0;res < len;res++) {
348 printf ("%02x ",databuffer[res+offset]);
349 if (!((res + 1) % 16)) {
350 printf ("\n");
351 if ((res + 1) < len) printf ("%04x ",res+1+adr);
354 printf ("\n\n");
356 return (0);
361 int ReadAnalogInputs (int bus,byte mask,ANALOG_INPUTS *inputs)
363 IRDATA md;
364 int res,i;
365 GET_ANALOG_INPUTS ai;
366 struct sockaddr_in send_adr;
368 #ifdef LINUX
369 fd_set events;
370 int maxfd,wait;
371 struct timeval tv;
372 #endif
374 md.command = READ_ANALOG_INPUTS;
375 md.address = mask;
377 md.len = 4;
380 res = WriteTransceiverEx (IRDevices + bus,&md);
381 if (res) return (res);
384 if (IRDevices[bus].io.if_type == IF_LAN) {
385 #ifdef WIN32
386 res = WaitForSingleObject (IrtLanEvent,2000);
387 if (res == WAIT_TIMEOUT) return (ERR_TIMEOUT);
388 #endif
390 #ifdef LINUX
391 FD_ZERO (&events);
393 FD_SET (irtlan_socket,&events);
394 maxfd = irtlan_socket + 1;
396 tv.tv_sec = 2;
397 tv.tv_usec = 0;
399 wait = select (maxfd,&events,NULL,NULL,&tv);
400 if (!wait) return (ERR_TIMEOUT);
401 #endif
402 i = sizeof (send_adr);
403 res = recvfrom(irtlan_socket,(byte *)&ai,sizeof (GET_ANALOG_INPUTS),MSG_NOSIGNAL,(struct sockaddr *)&send_adr,&i);
404 if (res <= 0 || res != ai.ai.len + 1 || ai.netcommand != RESULT_ANALOG_INPUT) return (ERR_TIMEOUT);
408 else {
409 res = ReadIRStringEx (IRDevices,(byte *)&(ai.ai),sizeof (ANALOG_INPUTS),500);
410 if (res != sizeof (ANALOG_INPUTS)) return (ERR_TIMEOUT);
413 memcpy (inputs,&(ai.ai),sizeof (ANALOG_INPUTS));
414 return (0);
419 int SetRelaisEx (int bus,byte val,byte rel)
421 IRDATA md;
422 byte res;
424 md.command = SET_ID;
425 md.address = 0;
426 md.ir_length = 0x83;
428 md.target_mask = rel;
429 md.transmit_freq = val;
431 md.len = 8;
434 res = WriteTransceiverEx (IRDevices + bus,&md);
435 if (res) return (res);
437 return (0);
442 int SetTransceiverIDEx (int bus,byte id)
444 IRDATA md;
445 byte res;
448 if ((IRDevices[bus].fw_capabilities & FN_SOFTID) == 0) {
449 fprintf (stderr,"No SoftID IRTrans connected. Cannot set ID\n\n");
450 return (-1);
453 md.command = SET_ID;
454 md.address = 0;
455 md.ir_length = 0x4b;
456 md.transmit_freq = id * 37;
458 md.len = 8;
460 md.target_mask = id;
462 res = WriteTransceiverEx (IRDevices + bus,&md);
463 if (res) return (res);
465 return (0);
469 int ResetTransceiverEx (int bus)
471 IRDATA md;
472 byte res;
475 md.command = SBUS_RESET;
476 md.len = 3;
478 res = WriteTransceiverEx (IRDevices + bus,&md);
479 if (res) return (res);
481 return (0);
485 int GetBusInfoEx (STATUS_BUFFER *sb,int bus)
487 int i,res;
488 IRDATA ir;
490 ir.command = HOST_NETWORK_STATUS;
491 ir.len = 3;
493 if (bus == 0xffff) {
494 for (i=0;i<device_cnt;i++) {
495 res = WriteTransceiverEx (IRDevices + i,&ir);
496 if (res) return (res);
497 if (IRDevices[i].io.if_type == IF_LAN) {
498 res = GetBusInfoDetail (sb + i,(byte)i);
499 if (res) return (res);
502 for (i=0;i<device_cnt;i++) {
503 if (IRDevices[i].io.if_type != IF_LAN) res = GetBusInfoDetail (sb + i,(byte)i);
504 if (res) return (res);
507 else {
508 res = WriteTransceiverEx (IRDevices + bus,&ir);
509 if (res) return (res);
510 res = GetBusInfoDetail (sb,bus);
511 if (res) return (res);
514 return (0);
518 int GetBusInfoDetail (STATUS_BUFFER *sb,int bus)
520 int i,res,j;
521 STATUS_LINE_LAN sl;
522 STATUS_BUFFER_1 sb1;
523 STATUS_BUFFER_2 sb2;
524 STATUS_BUFFER_3 sb3;
525 STATUS_BUFFER_4 sb4;
526 struct sockaddr_in send_adr;
528 #ifdef LINUX
529 fd_set events;
530 int maxfd,wait;
531 struct timeval tv;
532 #endif
534 if (IRDevices[bus].io.if_type == IF_LAN) {
535 sb->my_adress = 0;
536 #ifdef WIN32
537 if (IRDevices[bus].fw_capabilities & FN_SBUS) for (j=0;j < 16;j++) while (1) {
538 res = WaitForSingleObject (IrtLanEvent,2000);
540 if (res == WAIT_TIMEOUT) return (ERR_TIMEOUT);
542 ResetEvent (IrtLanEvent);
543 i = sizeof (send_adr);
544 memset (&sl,0,sizeof (sl));
545 res = recvfrom(irtlan_socket,(byte *)&sl,sizeof (sl),MSG_NOSIGNAL,(struct sockaddr *)&send_adr,&i);
546 if (res <= 0 || res != sl.stat.sbus_len + 1) return (ERR_TIMEOUT);
547 if (sl.netcommand == RESULT_DEVSTATUS && send_adr.sin_addr.S_un.S_addr == IRDevices[bus].io.IPAddr[0].sin_addr.S_un.S_addr) {
548 IRDevices[bus].my_addr = 0;
549 memcpy (&sb->stat[j],&sl.stat,sizeof (STATUS_LINE));
550 break;
553 else while (1) {
554 res = WaitForSingleObject (IrtLanEvent,2000);
555 if (res == WAIT_TIMEOUT) return (ERR_TIMEOUT);
557 ResetEvent (IrtLanEvent);
558 i = sizeof (send_adr);
559 memset (&sl,0,sizeof (sl));
560 res = recvfrom(irtlan_socket,(byte *)&sl,sizeof (sl),MSG_NOSIGNAL,(struct sockaddr *)&send_adr,&i);
561 if (res <= 0 || res != sl.stat.sbus_len + 1) return (ERR_TIMEOUT);
562 if (sl.netcommand == RESULT_DEVSTATUS && send_adr.sin_addr.S_un.S_addr == IRDevices[bus].io.IPAddr[0].sin_addr.S_un.S_addr) {
563 IRDevices[bus].my_addr = 0;
564 memcpy (&sb->stat[0],&sl.stat,sizeof (STATUS_LINE));
565 break;
568 #endif
569 #ifdef LINUX
570 if (IRDevices[bus].fw_capabilities & FN_SBUS) for (j=0;j < 16;j++) while (1) {
571 FD_ZERO (&events);
572 FD_SET (irtlan_socket,&events);
573 maxfd = irtlan_socket + 1;
574 tv.tv_sec = 2;
575 tv.tv_usec = 0;
577 wait = select (maxfd,&events,NULL,NULL,&tv);
578 if (!wait) return (ERR_TIMEOUT);
579 i = sizeof (send_adr);
580 memset (&sl,0,sizeof (sl));
581 res = recvfrom(irtlan_socket,(byte *)&sl,sizeof (sl),MSG_NOSIGNAL,(struct sockaddr *)&send_adr,&i);
582 if (res <= 0 || res != sl.stat.sbus_len + 1) return (ERR_TIMEOUT);
583 if (sl.netcommand == RESULT_DEVSTATUS && send_adr.sin_addr.s_addr == IRDevices[bus].io.IPAddr[0].sin_addr.s_addr) {
584 IRDevices[bus].my_addr = 0;
585 memcpy (&sb->stat[j],&sl.stat,sizeof (STATUS_LINE));
586 break;
589 else while (1) {
590 FD_ZERO (&events);
591 FD_SET (irtlan_socket,&events);
592 maxfd = irtlan_socket + 1;
593 tv.tv_sec = 2;
594 tv.tv_usec = 0;
596 wait = select (maxfd,&events,NULL,NULL,&tv);
597 if (!wait) return (ERR_TIMEOUT);
598 i = sizeof (send_adr);
599 memset (&sl,0,sizeof (sl));
600 res = recvfrom(irtlan_socket,(byte *)&sl,sizeof (sl),MSG_NOSIGNAL,(struct sockaddr *)&send_adr,&i);
601 if (res <= 0 || res != sl.stat.sbus_len + 1) return (ERR_TIMEOUT);
602 if (sl.netcommand == RESULT_DEVSTATUS && send_adr.sin_addr.s_addr == IRDevices[bus].io.IPAddr[0].sin_addr.s_addr) {
603 IRDevices[bus].my_addr = 0;
604 memcpy (&sb->stat[0],&sl.stat,sizeof (STATUS_LINE));
605 break;
608 #endif
611 else {
612 res = ReadIRStringEx (IRDevices+bus,(byte *)sb,sizeof (STATUS_BUFFER),5000);
613 if (res == sizeof (STATUS_BUFFER_1)) {
614 memcpy (&sb1,sb,sizeof (STATUS_BUFFER_1));
615 memset (sb,0,sizeof (STATUS_BUFFER));
616 sb->my_adress = sb1.my_adress;
618 for (i=0;i <= 15;i++) {
619 sb->stat[i].device_mode = sb1.stat[i].device_mode;
620 sb->stat[i].send_mask = sb1.stat[i].send_mask;
621 memcpy (sb->stat[i].version,sb1.stat[i].version,10);
622 if (i == sb->my_adress) sb->stat[i].capabilities = IRDevices[bus].fw_capabilities;
626 if (res == sizeof (STATUS_BUFFER_2)) {
627 memcpy (&sb2,sb,sizeof (STATUS_BUFFER_2));
628 memset (sb,0,sizeof (STATUS_BUFFER));
629 sb->my_adress = sb2.my_adress;
631 for (i=0;i <= 15;i++) {
632 sb->stat[i].device_mode = sb2.stat[i].device_mode;
633 sb->stat[i].extended_mode = sb2.stat[i].extended_mode;
634 sb->stat[i].send_mask = sb2.stat[i].send_mask;
635 memcpy (sb->stat[i].version,sb2.stat[i].version,10);
636 if (i == sb->my_adress) sb->stat[i].capabilities = IRDevices[bus].fw_capabilities;
640 if (res == sizeof (STATUS_BUFFER_3)) {
641 memcpy (&sb3,sb,sizeof (STATUS_BUFFER_3));
642 memset (sb,0,sizeof (STATUS_BUFFER));
643 sb->my_adress = sb3.my_adress;
645 for (i=0;i <= 15;i++) {
646 sb->stat[i].device_mode = sb3.stat[i].device_mode;
647 sb->stat[i].extended_mode = sb3.stat[i].extended_mode;
648 sb->stat[i].send_mask = sb3.stat[i].send_mask;
649 memcpy (sb->stat[i].version,sb3.stat[i].version,10);
650 sb->stat[i].capabilities = sb3.stat[i].capabilities;
654 if (res == sizeof (STATUS_BUFFER_4)) {
655 memcpy (&sb4,sb,sizeof (STATUS_BUFFER_4));
656 memset (sb,0,sizeof (STATUS_BUFFER));
657 sb->my_adress = sb4.my_adress;
659 for (i=0;i <= 15;i++) {
660 sb->stat[i].device_mode = sb4.stat[i].device_mode;
661 sb->stat[i].extended_mode = sb4.stat[i].extended_mode;
662 sb->stat[i].send_mask = sb4.stat[i].send_mask;
663 memcpy (sb->stat[i].version,sb4.stat[i].version,10);
664 sb->stat[i].capabilities = sb4.stat[i].capabilities;
665 sb->stat[i].extended_mode2 = sb4.stat[i].extended_mode2;
668 if (res != sizeof (STATUS_BUFFER) && res != sizeof (STATUS_BUFFER_1) && res != sizeof (STATUS_BUFFER_2) && res != sizeof (STATUS_BUFFER_3) && res != sizeof (STATUS_BUFFER_4)) return (ERR_TIMEOUT);
670 IRDevices[bus].my_addr = sb->my_adress;
673 SwapStatusbuffer (sb);
674 return (0);
678 int ResendIREx (int bus,IRDATA *ir_data)
680 int res,i;
682 ir_data -> command = HOST_RESEND;
683 ir_data -> len = 6;
685 if (bus == 0xffff) for (i=0;i<device_cnt;i++) res = WriteTransceiverEx (&IRDevices[i],ir_data);
686 else res = (WriteTransceiverEx (&IRDevices[bus],ir_data));
687 if (res == ERR_RESEND) {
688 ir_data -> command = HOST_SEND;
689 if (ir_data -> mode & RAW_DATA)
690 ir_data -> len = (sizeof (IRDATA) - (CODE_LEN + (RAW_EXTRA))) + ir_data -> ir_length;
691 else
692 ir_data -> len = (sizeof (IRDATA) - CODE_LEN) + ir_data -> ir_length;
693 if (bus == 0xffff) for (i=0;i<device_cnt;i++) res = WriteTransceiverEx (&IRDevices[i],ir_data);
694 else res = (WriteTransceiverEx (&IRDevices[bus],ir_data));
697 return (res);
700 int GetDeviceData (int cmd_num,DATABUFFER *dat)
702 int res;
703 IRDATA ird;
704 IRDATA send;
705 int mac_len,mac_pause,rpt_len;
707 res = DBGetIRCode (cmd_num,&ird,0,&mac_len,&mac_pause,&rpt_len,0);
708 if (res) return (res);
709 if (mac_len || rpt_len) return (ERR_ISMACRO);
711 ird.command = HOST_SEND;
712 if (ird.mode & RAW_DATA) ird.len = (sizeof (IRDATA) - (CODE_LEN + (RAW_EXTRA))) + ird.ir_length;
713 else {
714 ird.len = (sizeof (IRDATA) - CODE_LEN) + ird.ir_length;
715 if (ird.mode & RC6_DATA) ird.len++;
718 swap_irdata (&ird,&send);
720 if (time_len != TIME_LEN) {
721 if (ird.mode & RAW_DATA) {
722 if (ird.ir_length > OLD_LENRAW) return (ERR_LONGRAW);
724 else {
725 if (ird.time_cnt > 6) return (ERR_LONGDATA);
726 ConvertToIRTRANS3 (&send);
729 send.checksumme = get_checksumme (&send);
730 dat->statustype = STATUS_DEVICEDATA;
731 dat->statuslen = sizeof (DATABUFFER);
732 memcpy (dat->data,&send,sizeof (IRDATA));
734 return (0);
738 int SendIR (int cmd_num,int address)
740 byte cal = 0;
741 int i,res,bus = 0,rpt;
742 IRDATA ird,ird_rpt;
743 int mac_len,mac_pause,rpt_len;
745 if (protocol_version >= 210) bus = (address >> 19) & (MAX_IR_DEVICES - 1);
746 else bus = (address >> 20) & (MAX_IR_DEVICES - 1);
747 if (address & 0x40000000) {
748 bus = 0xffff;
749 cal = (IRDevices[0].fw_capabilities & FN_CALIBRATE) != 0;
751 else cal = (IRDevices[bus].fw_capabilities & FN_CALIBRATE) != 0;
753 res = DBGetIRCode (cmd_num,&ird,0,&mac_len,&mac_pause,&rpt_len,cal);
754 if (res) return (res);
756 rpt = DBGetRepeatCode (cmd_num,&ird_rpt,cal);
758 if (address & 0x10000) {
759 ird.target_mask = (word)address & 0xffff;
761 if (address & 0x60000) ird.address = (byte)((address >> 17) & 0x3);
763 if (address & 0x80000000) ird.address |= ((address >> 25) & 28) + 4;
766 if (!rpt)res = DoSendIR (&ird,&ird_rpt,rpt_len,mac_pause,bus);
767 else res = DoSendIR (&ird,NULL,rpt_len,mac_pause,bus);
769 if (mac_len) msSleep (mac_pause);
771 for (i=1;i < mac_len;i++) {
772 res = DBGetIRCode (cmd_num,&ird,i,&mac_len,&mac_pause,&rpt_len,cal);
773 if (res) return (res);
775 if (address & 0x10000) {
776 ird.target_mask = (word)address & 0xffff;
778 if (address & 0x60000) ird.address = (byte)((address >> 17) & 0x3);
780 if (address & 0x80000000) ird.address |= ((address >> 25) & 28) + 4;
782 if (!rpt)res = DoSendIR (&ird,&ird_rpt,rpt_len,mac_pause,bus);
783 else res = DoSendIR (&ird,NULL,rpt_len,mac_pause,bus);
785 if (mac_len) msSleep (mac_pause);
788 return (res);
793 int DoSendIR (IRDATA *ir_data,IRDATA *ir_rep,int rpt_len,int rpt_pause,int bus)
795 byte ocarrier;
796 int res = 0,i;
797 unsigned int end_time;
799 ir_data -> command = HOST_SEND;
800 if (ir_data -> mode & RAW_DATA) {
801 ir_data -> len = (sizeof (IRDATA) - (CODE_LEN + (RAW_EXTRA))) + ir_data -> ir_length;
802 if (mode_flag & CODEDUMP) PrintRawData ((IRRAW *)ir_data);
804 else {
805 ir_data -> len = (sizeof (IRDATA) - CODE_LEN) + ir_data -> ir_length;
806 if (ir_data->mode & RC6_DATA) ir_data->len++;
807 if (mode_flag & CODEDUMP) PrintPulseData (ir_data);
811 if (!rpt_len) {
812 if (bus == 0xffff) for (i=0;i<device_cnt;i++) {
813 ocarrier = ir_data->transmit_freq;
814 if (!IRDevices[i].io.ext_carrier) ir_data->transmit_freq = Convert2OldCarrier (ocarrier);
815 res = WriteTransceiverEx (&IRDevices[i],ir_data);
816 ir_data->transmit_freq = ocarrier;
818 else {
819 ocarrier = ir_data->transmit_freq;
820 if (!IRDevices[bus].io.ext_carrier) ir_data->transmit_freq = Convert2OldCarrier (ocarrier);
821 res = (WriteTransceiverEx (&IRDevices[bus],ir_data));
822 ir_data->transmit_freq = ocarrier;
824 return (res);
827 if (ir_rep) {
828 ir_rep -> command = HOST_SEND;
829 if (ir_rep -> mode & RAW_DATA) {
830 ir_rep -> len = (sizeof (IRDATA) - (CODE_LEN + (RAW_EXTRA))) + ir_rep -> ir_length;
831 if (mode_flag & CODEDUMP) PrintRawData ((IRRAW *)ir_rep);
833 else {
834 ir_rep -> len = (sizeof (IRDATA) - CODE_LEN) + ir_rep -> ir_length;
835 if (ir_rep->mode & RC6_DATA) ir_rep->len++;
836 if (mode_flag & CODEDUMP) PrintPulseData (ir_rep);
841 end_time = GetMsTime () + rpt_len;
842 if (bus == 0xffff) for (i=0;i<device_cnt;i++) {
843 ocarrier = ir_data->transmit_freq;
844 if (!IRDevices[i].io.ext_carrier) ir_data->transmit_freq = Convert2OldCarrier (ocarrier);
845 res = WriteTransceiverEx (&IRDevices[i],ir_data);
846 ir_data->transmit_freq = ocarrier;
848 else {
849 ocarrier = ir_data->transmit_freq;
850 if (!IRDevices[bus].io.ext_carrier) ir_data->transmit_freq = Convert2OldCarrier (ocarrier);
851 res = (WriteTransceiverEx (&IRDevices[bus],ir_data));
852 ir_data->transmit_freq = ocarrier;
855 msSleep (rpt_pause);
857 if (ir_rep && GetMsTime () < end_time) {
858 if (bus == 0xffff) for (i=0;i<device_cnt;i++) {
859 ocarrier = ir_rep->transmit_freq;
860 if (!IRDevices[i].io.ext_carrier) ir_rep->transmit_freq = Convert2OldCarrier (ocarrier);
861 res = WriteTransceiverEx (&IRDevices[i],ir_rep);
862 ir_rep->transmit_freq = ocarrier;
864 else {
865 ocarrier = ir_rep->transmit_freq;
866 if (!IRDevices[bus].io.ext_carrier) ir_rep->transmit_freq = Convert2OldCarrier (ocarrier);
867 res = (WriteTransceiverEx (&IRDevices[bus],ir_rep));
868 ir_rep->transmit_freq = ocarrier;
870 msSleep (rpt_pause);
873 while (GetMsTime () < end_time) {
874 ir_data -> command = HOST_RESEND;
875 ir_data -> len = 3;
877 if (bus == 0xffff) for (i=0;i<device_cnt;i++) res = WriteTransceiverEx (&IRDevices[i],ir_data);
878 else res = (WriteTransceiverEx (&IRDevices[bus],ir_data));
879 if (res == ERR_RESEND) {
880 if (ir_rep) memcpy (ir_data,ir_rep,sizeof (IRDATA));
881 ir_data -> command = HOST_SEND;
882 if (ir_data -> mode & RAW_DATA) ir_data -> len = (sizeof (IRDATA) - (CODE_LEN + (RAW_EXTRA))) + ir_data -> ir_length;
883 else {
884 ir_data -> len = (sizeof (IRDATA) - CODE_LEN) + ir_data -> ir_length;
885 if (ir_data->mode & RC6_DATA) ir_data->len++;
887 if (bus == 0xffff) for (i=0;i<device_cnt;i++) {
888 ocarrier = ir_data->transmit_freq;
889 if (!IRDevices[i].io.ext_carrier) ir_data->transmit_freq = Convert2OldCarrier (ocarrier);
890 res = WriteTransceiverEx (&IRDevices[i],ir_data);
891 ir_data->transmit_freq = ocarrier;
893 else {
894 ocarrier = ir_data->transmit_freq;
895 if (!IRDevices[bus].io.ext_carrier) ir_data->transmit_freq = Convert2OldCarrier (ocarrier);
896 res = (WriteTransceiverEx (&IRDevices[bus],ir_data));
897 ir_data->transmit_freq = ocarrier;
900 msSleep (rpt_pause);
902 return (res);
906 byte Convert2OldCarrier (byte carrier)
908 word oc;
909 if (carrier == 255 || carrier < 128) return (carrier);
910 oc = (carrier & 127) << 2;
911 if (oc > 255) oc = 255;
912 return ((byte)oc);
916 int SendIRDataEx (IRDATA *ir_data,int address)
918 byte ocarrier;
919 int bus = 0,res,i;
920 if (address & 0x10000) {
921 ir_data->target_mask = (word)address & 0xffff;
923 if (protocol_version >= 210) bus = (address >> 19) & (MAX_IR_DEVICES - 1);
924 else bus = (address >> 20) & (MAX_IR_DEVICES - 1);
925 if (address & 0x40000000) bus = 0xffff;
927 if (address & 0x60000) ir_data -> address = (byte)((address >> 17) & 0x3);
929 if (address & 0x80000000) ir_data -> address |= ((address >> 25) & 28) + 4;
931 ir_data -> command = HOST_SEND;
932 if (ir_data -> mode & RAW_DATA) {
933 ir_data -> len = (sizeof (IRDATA) - (CODE_LEN + (RAW_EXTRA))) + ir_data -> ir_length;
934 if (mode_flag & CODEDUMP) PrintRawData ((IRRAW *)ir_data);
936 else {
937 ir_data -> len = (sizeof (IRDATA) - CODE_LEN) + ir_data -> ir_length;
938 if (ir_data->mode & RC6_DATA) ir_data->len++;
939 if (mode_flag & CODEDUMP) PrintPulseData (ir_data);
941 if (bus == 0xffff) for (i=0;i<device_cnt;i++) {
942 ocarrier = ir_data->transmit_freq;
943 if (!IRDevices[i].io.ext_carrier) ir_data->transmit_freq = Convert2OldCarrier (ocarrier);
944 res = WriteTransceiverEx (&IRDevices[i],ir_data);
945 ir_data->transmit_freq = ocarrier;
947 else {
948 ocarrier = ir_data->transmit_freq;
949 if (!IRDevices[bus].io.ext_carrier) ir_data->transmit_freq = Convert2OldCarrier (ocarrier);
950 res = (WriteTransceiverEx (&IRDevices[bus],ir_data));
951 ir_data->transmit_freq = ocarrier;
953 return (res);
957 void LCDTimeCommand (byte mode)
959 #ifndef WINCE
960 int i;
961 IRRAW irw;
962 #ifdef WIN32
963 struct _timeb tb;
964 #endif
965 #ifdef LINUX
966 struct timeval tb;
967 struct timezone tz;
968 #endif
970 memset (&irw,0,sizeof (IRRAW));
972 irw.command = HOST_SEND;
973 irw.mode = LCD_DATA | mode;
974 irw.target_mask = 0xffff;
976 #ifdef WIN32
977 _ftime (&tb);
978 if (tb.dstflag) i = 3600;
979 else i = 0;
980 *((unsigned int *)irw.data) = (unsigned int)(tb.time - 60 * tb.timezone + i);
981 irw.data[4] = tb.millitm / 48;
982 #endif
984 #ifdef LINUX
985 i = gettimeofday (&tb,&tz);
986 if (i) {
987 gettimeofday (&tb,NULL);
988 *((unsigned int *)irw.data) = tb.tv_sec;
990 else *((unsigned int *)irw.data) = tb.tv_sec - 60 * tz.tz_minuteswest;
991 irw.data[4] = tb.tv_usec / 48000;
992 #endif
994 swap_int ((int *)irw.data);
995 irw.len = (sizeof (IRDATA) - (CODE_LEN + (RAW_EXTRA))) + 5;
996 for (i=0;i<device_cnt;i++) WriteTransceiverEx (&IRDevices[i],(IRDATA *)&irw);
997 #endif
1001 void LCDBrightness (int val)
1003 byte data[10];
1005 *data = val;
1006 AdvancedLCD (LCD_DATA | LCD_BRIGHTNESS,data,1);
1009 int AdvancedLCD (byte mode,byte data[],int len)
1011 int res,i;
1012 IRRAW irr;
1014 if (display_bus != 0xffff && !IRDevices[display_bus].io.advanced_lcd) return (0);
1016 memset (&irr,0,sizeof (IRRAW));
1018 irr.command = HOST_SEND;
1019 irr.ir_length = 0;
1020 irr.mode = mode;
1022 irr.target_mask = 0xffff;
1024 memcpy (irr.data,data,len);
1026 irr.len = (byte)(sizeof (IRRAW) - CODE_LENRAW + len);
1028 if (display_bus == 0xffff) for (i=0;i<device_cnt;i++) {
1029 res = WriteTransceiverEx (&IRDevices[i],(IRDATA *)&irr);
1031 else res = WriteTransceiverEx (&IRDevices[display_bus],(IRDATA *)&irr);
1033 return (res);
1038 int SendLCD (IRRAW *ir_data,int address)
1040 int i,res,bus = 0;
1042 ir_data -> command = HOST_SEND;
1043 ir_data -> ir_length = 0;
1044 if (address & 0x10000) {
1045 ir_data->target_mask = (word)address & 0xffff;
1047 bus = display_bus;
1049 ir_data -> len = sizeof (IRRAW) - CODE_LENRAW + (byte)strlen (ir_data -> data) + 1;
1050 if (bus == 0xffff) for (i=0;i<device_cnt;i++) {
1051 res = WriteTransceiverEx (&IRDevices[i],(IRDATA *)ir_data);
1053 else res = WriteTransceiverEx (&IRDevices[bus],(IRDATA *)ir_data);
1054 return (res);
1057 int ReadLearndataLAN (DEVICEINFO *dev,byte *ird,int timeout)
1059 int res,i;
1060 byte buf[sizeof (IRDATA) + 10];
1061 struct sockaddr_in send_adr;
1062 #ifdef LINUX
1063 fd_set events;
1064 int maxfd,wait;
1065 struct timeval tv;
1066 #endif
1068 memset (buf,0,10);
1069 do {
1070 #ifdef WIN32
1071 res = WaitForSingleObject (IrtLanEvent,timeout);
1072 if (res == WAIT_TIMEOUT) {
1073 buf[0] = 5;
1074 IRTransLanSend (dev,(IRDATA *)buf);
1075 return (ERR_TIMEOUT);
1077 #endif
1079 #ifdef LINUX
1080 FD_ZERO (&events);
1082 FD_SET (irtlan_socket,&events);
1083 maxfd = irtlan_socket + 1;
1085 tv.tv_sec = timeout / 1000;
1086 tv.tv_usec = (timeout % 1000) * 1000;
1088 wait = select (maxfd,&events,NULL,NULL,&tv);
1089 if (!wait) return (ERR_TIMEOUT);
1090 #endif
1092 i = sizeof (send_adr);
1093 res = recvfrom(irtlan_socket,buf,sizeof (IRDATA),MSG_NOSIGNAL,(struct sockaddr *)&send_adr,&i);
1094 if (res <= 0 || res != buf[1] + 1) return (ERR_TIMEOUT);
1096 } while (buf[0] != RESULT_LEARN || send_adr.sin_addr.s_addr != dev->io.IPAddr[0].sin_addr.s_addr);
1098 memcpy (ird,buf+1,buf[1]);
1100 return (0);
1105 int LearnNextIREx (IRDATA *ir_data,word addr,word timeout,word ir_timeout,byte carrier)
1107 int res;
1108 byte len;
1109 int bus;
1110 IRDATA buffer;
1111 do {
1112 ir_data -> address = 0;
1113 if ((addr & 0xff) <= 15) ir_data -> address |= addr;
1114 if ((addr & 0xff) == 'L') ir_data -> address |= ADRESS_LOCAL;
1115 if ((addr & 0xff) == '*') ir_data -> address |= ADRESS_ALL;
1117 bus = (addr >> 8) & (MAX_IR_DEVICES - 1);
1119 ir_data -> command = HOST_LEARNIRQUICK;
1120 ir_data -> len = sizeof (IRDATA) - CODE_LEN;
1122 res = ir_timeout & 15;
1123 if (strcmp (IRDevices[bus].version+1,"4.04.35") < 0 && res > 14) res = 14;
1124 ir_data -> target_mask = res;
1125 ir_data->target_mask |= (ir_timeout / 2) & (LONG_LEARN_MODE | RECEIVER_SELECT_MASK);
1127 res = WriteTransceiverEx (IRDevices + bus,ir_data);
1128 if (res) return (res);
1130 if (IRDevices[bus].io.if_type == IF_LAN) len = sizeof (IRDATA);
1132 else {
1133 res = ReadIRStringEx (IRDevices + bus,&len,1,timeout);
1135 if (!res) {
1136 CancelLearnEx (IRDevices + bus);
1137 return (ERR_TIMEOUT);
1141 } while (len <= sizeof (IRDATA) - CODE_LEN);
1143 if (IRDevices[bus].io.if_type == IF_LAN) {
1144 if (ReadLearndataLAN (IRDevices + bus,(byte *)&buffer,timeout)) return (ERR_TIMEOUT);
1146 else {
1147 if (ReadIRStringEx (IRDevices + bus,&buffer.checksumme,len-1,200) != len-1) return (ERR_TIMEOUT);
1151 if (TIME_LEN != time_len) ConvertToIRTRANS4 ((IRDATA3 *)&buffer);
1153 swap_irdata (&buffer,NULL);
1155 memcpy (ir_data -> data,buffer.data,CODE_LEN);
1156 ir_data -> ir_length = buffer.ir_length;
1157 ir_data -> address = buffer.address;
1158 ir_data -> data[ir_data -> ir_length] = 0;
1159 ir_data -> time_cnt = buffer.time_cnt;
1161 return (0);
1164 void ConvertToIRTRANS4 (IRDATA3 *ird)
1166 IRDATA irnew;
1168 memset (&irnew,0,sizeof (IRDATA));
1170 memcpy (&irnew,ird,21);
1171 memcpy (irnew.pulse_len,ird->pulse_len,12);
1172 memcpy (&irnew.time_cnt,&ird->time_cnt,CODE_LEN + 3);
1173 irnew.len = ird->len + 8;
1174 memcpy (ird,&irnew,sizeof (IRDATA));
1178 void CancelLearnEx (DEVICEINFO *dev)
1180 byte res = 1;
1181 char st[10];
1182 WriteIRStringEx (dev,(byte *)&res,1);
1183 res = ReadIRStringEx (dev,st,1,500);
1190 int LearnIREx (IRDATA *ir_data,word addr,word timeout,word ir_timeout,byte carrier)
1192 int res;
1193 byte len;
1194 int bus;
1196 do {
1197 ir_data -> address = 0;
1198 if ((addr & 0xff) <= 15) ir_data -> address |= addr;
1199 if ((addr & 0xff) == 'L') ir_data -> address |= ADRESS_LOCAL;
1200 if ((addr & 0xff) == '*') ir_data -> address |= ADRESS_ALL;
1202 bus = (addr >> 8) & (MAX_IR_DEVICES - 1);
1204 ir_data -> command = HOST_LEARNIR;
1206 res = ir_timeout & 15;
1207 if (strcmp (IRDevices[bus].version+1,"4.04.35") < 0 && res > 14) res = 14;
1208 ir_data -> target_mask = res;
1209 ir_data->target_mask |= (ir_timeout / 2) & (LONG_LEARN_MODE | RECEIVER_SELECT_MASK);
1211 ir_data -> len = 6;
1213 res = WriteTransceiverEx (IRDevices + bus,ir_data);
1214 if (res) return (res);
1216 if (IRDevices[bus].io.if_type == IF_LAN) len = sizeof (IRDATA);
1218 else {
1219 res = ReadIRStringEx (IRDevices + bus,&len,1,timeout);
1221 if (!res) {
1222 CancelLearnEx (IRDevices + bus);
1223 return (ERR_TIMEOUT);
1227 } while (len <= sizeof (IRDATA) - CODE_LEN);
1229 if (IRDevices[bus].io.if_type == IF_LAN) {
1230 if (ReadLearndataLAN (IRDevices + bus,(byte *)ir_data,timeout)) return (ERR_TIMEOUT);
1232 else {
1233 if (ReadIRStringEx (IRDevices + bus,&ir_data->checksumme,len-1,200) != len-1) return (ERR_TIMEOUT);
1236 if (TIME_LEN != time_len) ConvertToIRTRANS4 ((IRDATA3 *)ir_data);
1238 ir_data -> data[ir_data -> ir_length] = 0;
1239 swap_irdata (ir_data,NULL);
1241 if (carrier && ir_data->transmit_freq != 255) ir_data->transmit_freq = carrier;
1242 CorrectIRTimings (ir_data);
1244 PrintPulseData (ir_data);
1246 return (0);
1250 int LearnRepeatIREx (IRDATA *ir_data,word addr,word timeout,word ir_timeout,byte carrier)
1252 int res;
1253 byte len;
1254 int bus;
1256 do {
1257 ir_data -> address = 0;
1258 if ((addr & 0xff) <= 15) ir_data -> address |= addr;
1259 if ((addr & 0xff) == 'L') ir_data -> address |= ADRESS_LOCAL;
1260 if ((addr & 0xff) == '*') ir_data -> address |= ADRESS_ALL;
1262 bus = (addr >> 8) & (MAX_IR_DEVICES - 1);
1264 ir_data -> command = HOST_LEARNIRREPEAT;
1266 res = ir_timeout & 15;
1267 if (strcmp (IRDevices[bus].version+1,"4.04.35") < 0 && res > 14) res = 14;
1268 ir_data -> target_mask = res;
1269 ir_data->target_mask |= (ir_timeout / 2) & (LONG_LEARN_MODE | RECEIVER_SELECT_MASK);
1271 ir_data -> len = 6;
1273 res = WriteTransceiverEx (IRDevices + bus,(IRDATA *)ir_data);
1274 if (res) return (res);
1275 if (IRDevices[bus].io.if_type == IF_LAN) len = sizeof (IRDATA);
1277 else {
1278 res = ReadIRStringEx (IRDevices + bus,&len,1,timeout);
1280 if (!res) {
1281 CancelLearnEx (IRDevices + bus);
1282 return (ERR_TIMEOUT);
1286 } while (len <= sizeof (IRDATA) - CODE_LEN);
1288 if (IRDevices[bus].io.if_type == IF_LAN) {
1289 if (ReadLearndataLAN (IRDevices + bus,(byte *)ir_data,timeout)) return (ERR_TIMEOUT);
1291 else {
1292 if (ReadIRStringEx (IRDevices + bus,&ir_data->checksumme,len-1,200) != len-1) return (ERR_TIMEOUT);
1295 if (TIME_LEN != time_len) ConvertToIRTRANS4 ((IRDATA3 *)ir_data);
1297 ir_data -> data[ir_data -> ir_length] = 0;
1299 swap_irdata (ir_data,NULL);
1300 if (carrier && ir_data->transmit_freq != 255) ir_data->transmit_freq = carrier;
1301 CorrectIRTimings (ir_data);
1303 return (0);
1307 int LearnRawIREx (IRRAW *ir_data,word addr,word timeout,word ir_timeout,byte carrier)
1309 int res;
1310 byte len;
1311 int bus;
1313 do {
1314 ir_data -> address = 0;
1315 if ((addr & 0xff) <= 15) ir_data -> address |= addr;
1316 if ((addr & 0xff) == 'L') ir_data -> address |= ADRESS_LOCAL;
1317 if ((addr & 0xff) == '*') ir_data -> address |= ADRESS_ALL;
1319 bus = (addr >> 8) & (MAX_IR_DEVICES - 1);
1321 ir_data -> command = HOST_LEARNIRRAW;
1324 res = ir_timeout & 15;
1325 if (strcmp (IRDevices[bus].version+1,"4.04.35") < 0 && res > 14) res = 14;
1326 ir_data -> target_mask = res;
1327 ir_data->target_mask |= (ir_timeout / 2) & (RECEIVER_SELECT_MASK);
1329 ir_data -> len = 6;
1331 res = WriteTransceiverEx (IRDevices + bus,(IRDATA *)ir_data);
1333 if (res) return (res);
1334 if (IRDevices[bus].io.if_type == IF_LAN) len = sizeof (IRDATA);
1336 else {
1337 res = ReadIRStringEx (IRDevices + bus,&len,1,timeout);
1339 if (!res) {
1340 CancelLearnEx (IRDevices + bus);
1341 return (ERR_TIMEOUT);
1345 } while (len <= sizeof (IRRAW) - CODE_LENRAW);
1347 if (IRDevices[bus].io.if_type == IF_LAN) {
1348 if (ReadLearndataLAN (IRDevices + bus,(byte *)ir_data,timeout)) return (ERR_TIMEOUT);
1350 else {
1351 if (ReadIRStringEx (IRDevices + bus,&ir_data->checksumme,len-1,200) != len-1) return (ERR_TIMEOUT);
1353 ir_data -> data[ir_data -> ir_length] = 0;
1355 swap_irdata ((IRDATA *)ir_data,NULL);
1357 if (carrier && ir_data->transmit_freq != 255) ir_data->transmit_freq = carrier;
1358 CorrectIRTimingsRAW (ir_data);
1360 return (0);
1363 int LearnRawIRRepeatEx (IRRAW *ir_data,word addr,word timeout,word ir_timeout,byte carrier)
1365 int res;
1366 byte len;
1367 int bus;
1369 do {
1370 ir_data -> address = 0;
1371 if ((addr & 0xff) <= 15) ir_data -> address |= addr;
1372 if ((addr & 0xff) == 'L') ir_data -> address |= ADRESS_LOCAL;
1373 if ((addr & 0xff) == '*') ir_data -> address |= ADRESS_ALL;
1375 bus = (addr >> 8) & (MAX_IR_DEVICES - 1);
1377 ir_data -> command = HOST_LEARNIRRAWREPEAT;
1379 res = ir_timeout & 15;
1380 if (strcmp (IRDevices[bus].version+1,"4.04.35") < 0 && res > 14) res = 14;
1381 ir_data -> target_mask = res;
1382 ir_data->target_mask |= (ir_timeout / 2) & (RECEIVER_SELECT_MASK);
1384 ir_data -> len = 6;
1386 res = WriteTransceiverEx (IRDevices + bus,(IRDATA *)ir_data);
1387 if (res) return (res);
1388 if (IRDevices[bus].io.if_type == IF_LAN) len = sizeof (IRDATA);
1390 else {
1391 res = ReadIRStringEx (IRDevices + bus,&len,1,timeout);
1393 if (!res) {
1394 CancelLearnEx (IRDevices + bus);
1395 return (ERR_TIMEOUT);
1399 } while (len <= sizeof (IRRAW) - CODE_LENRAW);
1401 if (IRDevices[bus].io.if_type == IF_LAN) {
1402 if (ReadLearndataLAN (IRDevices + bus,(byte *)ir_data,timeout)) return (ERR_TIMEOUT);
1404 else {
1405 if (ReadIRStringEx (IRDevices + bus,&ir_data->checksumme,len-1,200) != len-1) return (ERR_TIMEOUT);
1407 ir_data -> data[ir_data -> ir_length] = 0;
1409 swap_irdata ((IRDATA *)ir_data,NULL);
1411 if (carrier && ir_data->transmit_freq != 255) ir_data->transmit_freq = carrier;
1412 CorrectIRTimingsRAW (ir_data);
1414 return (0);
1417 void CorrectIRTimings (IRDATA *ir_data)
1419 int i;
1421 if (ir_data->transmit_freq >= 48 && ir_data->transmit_freq <= 100) {
1422 for (i=0;i < ir_data->time_cnt;i++) {
1423 ir_data->pulse_len[i] += 4;
1424 ir_data->pause_len[i] -= 4;
1429 void CorrectIRTimingsRAW (IRRAW *ir_data)
1431 int i = 0;
1432 word pulse,pause;
1434 if (ir_data->transmit_freq >= 48 && ir_data->transmit_freq <= 100) {
1436 while (i < ir_data -> ir_length) {
1437 pulse = ir_data ->data [i++];
1438 if (pulse) ir_data ->data [i-1] += 4;
1439 else {
1440 pulse = ir_data ->data [i++] << 8;
1441 pulse |= ir_data->data [i++];
1442 pulse += 4;
1443 ir_data->data[i-2] = (pulse >> 8);
1444 ir_data->data[i-1] = pulse & 0xff;
1446 pause = ir_data ->data [i++];
1447 if (pause) ir_data ->data [i-1] -= 4;
1448 else {
1449 pause = ir_data ->data[i++] << 8;
1450 pause |= ir_data->data[i++];
1451 pause -= 4;
1452 ir_data->data[i-2] = (pause >> 8);
1453 ir_data->data[i-1] = pause & 0xff;
1459 void PrintPulseData (IRDATA *ir_data)
1461 int i,frq;
1462 char msg[256];
1464 frq = ir_data->transmit_freq;
1466 if (frq == 255) frq = 1000;
1467 else if (frq & 128) frq = (frq & 127) * 4;
1468 for (i=0;i < TIME_LEN;i++) {
1469 sprintf (msg,"TIME : %d %4d %4d\n",i,ir_data -> pulse_len[i] * 8,ir_data -> pause_len[i] * 8);
1470 log_print (msg,LOG_INFO);
1472 sprintf (msg,"T.REP : %4d\n",ir_data->repeat_pause);
1473 log_print (msg,LOG_INFO);
1474 sprintf (msg,"REP/MODE: %4d %d\n",ir_data->ir_repeat,ir_data -> mode);
1475 log_print (msg,LOG_INFO);
1476 sprintf (msg,"FREQ : %4d\n",frq);
1477 log_print (msg,LOG_INFO);
1478 sprintf (msg,"DATA : %s\n",ir_data->data);
1479 log_print (msg,LOG_INFO);
1480 sprintf (msg,"LEN : %4d\n",ir_data->ir_length);
1481 log_print (msg,LOG_INFO);
1482 sprintf (msg,"ADR : %d\n",ir_data -> address);
1483 log_print (msg,LOG_INFO);
1484 sprintf (msg,"MASK : %d\n",ir_data -> target_mask);
1485 log_print (msg,LOG_INFO);
1490 void PrintRawData (IRRAW *ir_data)
1492 int i,frq;
1493 char msg[256];
1494 word pulse,pause;
1496 frq = ir_data->transmit_freq;
1498 if (frq == 255) frq = 455;
1499 else if (frq & 128) frq = (frq & 127) * 4;
1500 sprintf (msg,"MODE : %d\n",ir_data -> mode & 0xf0);
1501 log_print (msg,LOG_INFO);
1502 sprintf (msg,"Repeat : %d\n",ir_data -> mode & 0xf);
1503 log_print (msg,LOG_INFO);
1504 sprintf (msg,"FREQ : %4d\n",frq);
1505 log_print (msg,LOG_INFO);
1506 sprintf (msg,"LEN : %4d\n",ir_data->ir_length);
1507 log_print (msg,LOG_INFO);
1508 sprintf (msg,"ADR : %d\n",ir_data -> address);
1509 log_print (msg,LOG_INFO);
1511 i = 0;
1513 while (i < ir_data -> ir_length) {
1514 pulse = ir_data ->data [i++];
1515 if (!pulse) {
1516 pulse = ir_data ->data [i++] << 8;
1517 pulse |= ir_data->data [i++];
1519 pause = ir_data ->data [i++];
1520 if (!pause) {
1521 pause = ir_data ->data[i++] << 8;
1522 pause |= ir_data->data[i++];
1524 sprintf (msg,"%5d %5d\n",pulse * 8,pause * 8);
1525 log_print (msg,LOG_INFO);
1531 void PrintCommand (IRDATA *ir_data)
1533 char msg[256];
1535 sprintf (msg,"DATA : %s\n",ir_data->data);
1536 log_print (msg,LOG_INFO);
1540 #ifdef LINUX
1542 typedef void (*sighandler_t)(int);
1544 void ShutdownHandler (void)
1546 mode_flag |= NO_RECONNECT;
1548 if (!(mode_flag & NO_CLOCK)) LCDTimeCommand (LCD_DISPLAYTIME);
1549 LCDBrightness (4);
1550 if (pidfile[0]) unlink (pidfile);
1552 exit (0);
1555 #endif
1558 DEVICEINFO IRDevices[MAX_IR_DEVICES];
1559 int device_cnt;
1561 int InitCommunicationEx (char devicesel[])
1563 int i,res;
1564 char msg[255];
1566 byteorder = GetByteorder ();
1568 res = get_devices (devicesel,0);
1570 if (res) return (res);
1572 raw_repeat = 1;
1573 if (device_cnt) time_len = IRDevices[0].io.time_len;
1574 else time_len = TIME_LEN;
1576 for (i=0;i<device_cnt;i++) {
1577 sprintf (msg,"Name : %s\n",IRDevices[i].name);
1578 log_print (msg,LOG_INFO);
1579 sprintf (msg,"Version: %s\n",IRDevices[i].version);
1580 log_print (msg,LOG_INFO);
1581 sprintf (msg,"FW SNo : %d\n",IRDevices[i].fw_serno);
1582 log_print (msg,LOG_INFO);
1583 sprintf (msg,"Capab : %s\n",IRDevices[i].cap_string);
1584 log_print (msg,LOG_INFO);
1585 sprintf (msg,"FW Cap : %d\n",IRDevices[i].fw_capabilities);
1586 log_print (msg,LOG_INFO);
1587 sprintf (msg,"USB SNo: %s\n",IRDevices[i].usb_serno);
1588 log_print (msg,LOG_INFO);
1589 sprintf (msg,"Node : %s\n\n",IRDevices[i].device_node);
1590 log_print (msg,LOG_INFO);
1593 for (i=0;i<device_cnt;i++) {
1594 raw_repeat &= IRDevices[i].io.raw_repeat;
1595 if (IRDevices[i].io.time_len != time_len) {
1596 sprintf (msg,"Devices < Version 4.xx.xx and Devices > 4.xx.xx can not be mixed.\nExiting now\n\n");
1597 log_print (msg,LOG_FATAL);
1598 return (-1);
1602 if (new_lcd_flag) display_bus = 0xffff;
1604 for (i=0;i < device_cnt;i++) if (IRDevices[i].version[0] == 'D' || (IRDevices[i].fw_capabilities & FN_DISPMASK)) {
1605 display_bus = i;
1606 break;
1609 #ifdef WIN32
1610 SetConsoleCtrlHandler (ShutdownHandler,TRUE);
1611 #endif
1613 #ifdef LINUX
1614 signal (SIGINT,(sighandler_t)ShutdownHandler);
1615 #endif
1617 return (0);
1621 #ifdef WIN32
1623 BOOL WINAPI ShutdownHandler (DWORD type)
1625 mode_flag |= NO_RECONNECT;
1627 if (!(mode_flag & NO_CLOCK)) LCDTimeCommand (LCD_DISPLAYTIME);
1628 LCDBrightness (4);
1630 if (pidfile[0]) _unlink (pidfile);
1632 exit (0);
1635 int GetAvailableDataEx (DEVICEINFO *dev)
1637 if (dev->io.if_type == IF_USB) return GetUSBAvailableEx (dev);
1638 if (dev->io.if_type == IF_RS232 || dev->io.if_type == IF_SERUSB) return GetSerialAvailableEx (dev);
1640 return (0);
1643 #endif
1645 int ReadInstantReceive (DEVICEINFO *dev,byte pnt[],int len)
1647 word to,cnt = 0;
1649 #ifdef WIN32
1650 if (dev->io.if_type == IF_USB) to = 5;
1651 else to = 25;
1652 #endif
1654 #ifdef LINUX
1655 to = 25;
1656 #endif
1658 cnt = ReadIRStringEx (dev,pnt,len,to);
1660 if (!cnt) return (0);
1662 if (dev->io.inst_receive_mode & 1) {
1663 if (pnt[cnt-1]) {
1664 cnt += ReadIRStringEx (dev,pnt+cnt,len-cnt,100);
1667 pnt[cnt] = 0;
1668 return (cnt);
1672 #ifdef LINUX
1674 extern SOCKET irt_bcast[32];
1675 extern int if_count;
1677 #endif
1680 int get_devices (char sel[],byte testflag) // Errflag = Continue bei USB Error
1682 DEVICEINFO *dev;
1683 unsigned int adr;
1684 int res,i,p,q,autoflag = 0;
1685 char st[1024];
1686 GETVERSION_LAN ver;
1687 IRDATA_LAN irdlan;
1688 struct sockaddr_in target,send_adr;
1690 #ifdef LINUX
1691 fd_set events;
1692 int maxfd,wait;
1693 struct timeval tv;
1694 #endif
1696 #ifdef WIN32
1697 int err;
1698 WORD wVersionRequired;
1699 WSADATA wsaData;
1700 enum FT_STATUS stat;
1701 char *buf_pnt[260];
1702 char buf_value[256][64];
1703 int numDevs;
1704 #else
1705 char dst[50];
1706 #endif
1709 strcpy (st,sel);
1710 st[strlen (st) + 1] = 0;
1711 q = p = 0;
1712 device_cnt = 0;
1713 if (testflag) {
1714 printf ("Probing IRTrans Devices ..");
1715 fflush (stdout);
1717 while (st[p]) {
1718 if (testflag) {
1719 printf (".");
1720 fflush (stdout);
1722 while (st[p] && sel[p] != ' ' && st[p] != ';') p++; st[p] = 0;
1723 #ifdef WIN32
1724 if ((!strncmp (st + q,"usb",3) || !strncmp (st + q,"USB",3)) && !autoflag) {
1725 res = LoadUSBLibrary ();
1726 if (res) {
1727 if (testflag) {
1728 q = ++p;
1729 continue;
1731 return (ERR_OPENUSB);
1734 autoflag = 1;
1735 for (i=0;i<256;i++) buf_pnt[i] = buf_value[i];
1736 buf_pnt[i] = NULL;
1738 stat = F_ListDevices(buf_pnt,&numDevs,FT_LIST_ALL|FT_OPEN_BY_SERIAL_NUMBER);
1740 if (FT_SUCCESS(stat)) {
1741 for (i=0;i<numDevs && device_cnt < MAX_IR_DEVICES;i++) get_detail_deviceinfo (buf_pnt[i],"USB",IF_USB);
1744 else {
1745 if (testflag) {
1746 q = ++p;
1747 continue;
1749 return (ERR_OPENUSB);
1751 if (st[q + 3] == ':') sort_ir_devices (st + q + 4);
1753 else if (!strncmp (st + q,"com",3) || !strncmp (st + q,"COM",3)) {
1754 res = get_detail_deviceinfo ("",st+q,IF_RS232);
1756 else if (!strcmp (st + q,"dummy")) res = 0;
1757 #endif
1758 #ifdef LINUX
1759 //LINUX Autofind USB devices
1760 if ((!strncmp (st + q,"usb",3) || !strncmp (st + q,"USB",3)) && !autoflag) {
1761 autoflag = 1;
1762 res = 0;
1763 for (i=0;i < 16;i++) {
1764 sprintf (dst,"/dev/usb/ttyUSB%d",i);
1765 if (get_detail_deviceinfo ("",dst,IF_USB)) {
1766 sprintf (dst,"/dev/tts/USB%d",i);
1767 if (get_detail_deviceinfo ("",dst,IF_USB)) {
1768 sprintf (dst,"/dev/usb/tts/%d",i);
1769 get_detail_deviceinfo ("",dst,IF_USB);
1775 else if ((!strncmp (st + q,"/dev/usb/tty",12) || !strncmp (st + q,"/dev/ttyUSB",11) || !strncmp (st + q,"/dev/usb/tts",12) ||
1776 !strncmp (st + q,"/dev/ttyusb",11) || !strncmp (st + q,"/dev/tts/USB",12)) && !autoflag) {
1777 if (st[q + strlen (st+q) - 1] == ']') {
1778 int j,beg,end;
1779 char base[256];
1781 i = q + strlen (st+q) - 1;
1782 while (i >= q && st[i] != '[') i--;
1783 if (st[i] != '[') return (0);
1784 i++;
1785 j = i;
1786 while (st[i] >= '0' && st[i] <= '9') i++;
1787 beg = atoi (st+j);
1788 i++;
1789 end = atoi (st+i);
1790 memset (base,0,sizeof (base));
1791 memcpy (base,st+q,j-q-1);
1793 res = 0;
1794 for (i=beg;i<=end;i++) {
1795 sprintf (dst,"%s%d",base,i);
1796 get_detail_deviceinfo ("",dst,IF_USB);
1800 else res = get_detail_deviceinfo ("",st+q,IF_USB);
1802 else if (!strncmp (st + q,"/dev/tty",8)) res = get_detail_deviceinfo ("",st+q,IF_RS232);
1803 else if (!strcmp (st + q,"dummy")) res = 0;
1804 #endif
1805 else if (st[q] >= '1' && st[q] <= '9') {
1807 dev = IRDevices + device_cnt;
1808 strcpy (dev->device_node,st+q);
1809 dev->io.if_type = IF_LAN;
1810 dev->usb_serno[0] = 0;
1811 #ifdef WIN32
1812 dev->io.comport = NULL;
1813 dev->io.event = NULL;
1814 #endif
1816 #ifdef LINUX
1817 dev->io.comport = (int)NULL;
1818 dev->io.event = (int)NULL;
1819 #endif
1821 memset (&dev->io.IPAddr,0,sizeof (dev->io.IPAddr));
1822 dev->io.IPAddr[0].sin_family = AF_INET;
1824 adr = inet_addr(dev->device_node);
1825 // Hier Erweiterung auf Devicenamen
1827 haddr = gethostbyaddr ((char *)&adr,4,AF_INET);
1828 if (haddr == NULL) {
1829 sprintf (msg,"\nCan not open device %s.\nAborting ...\n\n",st+q);
1830 log_print (msg,LOG_FATAL);
1831 exit (-1);
1834 dev->io.IPAddr[0].sin_addr.s_addr = adr;
1835 dev->io.IPAddr[0].sin_port = htons ((word)IRTRANS_PORT);
1837 res = GetTransceiverVersionEx (dev);
1838 if (res) return (ERR_OPEN);
1839 else device_cnt++;
1842 else if ((!strncmp (st + q,"lan",3) || !strncmp (st + q,"LAN",3))) {
1843 // Find LAN Devices ...
1845 if (testflag) {
1846 #ifdef WIN32
1847 wVersionRequired = MAKEWORD(2,2);
1848 err = WSAStartup(wVersionRequired, &wsaData);
1849 if (err != 0) exit(1);
1850 #endif
1851 OpenIRTransLANSocket ();
1852 #ifdef LINUX
1853 OpenIRTransBroadcastSockets ();
1854 #endif
1857 memset (&irdlan,0,sizeof (irdlan));
1858 irdlan.netcommand = COMMAND_LAN;
1859 irdlan.ir_data.command = HOST_VERSION;
1860 irdlan.ir_data.len = 3;
1861 irdlan.ir_data.checksumme = get_checksumme (&irdlan.ir_data);
1863 memset (&target,0,sizeof (struct sockaddr));
1864 target.sin_family = AF_INET;
1865 target.sin_addr.s_addr = INADDR_BROADCAST;
1866 target.sin_port = htons ((word)IRTRANS_PORT);
1868 #ifdef LINUX
1869 for (i=0;i < if_count;i++) {
1870 res = send (irt_bcast[i],(char *)&irdlan,irdlan.ir_data.len + sizeof (IRDATA_LAN) - sizeof (IRDATA),MSG_NOSIGNAL);
1872 #else
1873 if (res = connect (irtlan_outbound,(struct sockaddr *)&target,sizeof (struct sockaddr_in)) < 0) return (ERR_BINDSOCKET);
1874 res = send (irtlan_outbound,(char *)&irdlan,irdlan.ir_data.len + sizeof (IRDATA_LAN) - sizeof (IRDATA),MSG_NOSIGNAL);
1875 #endif
1877 #ifdef WIN32
1878 IrtLanEvent = WSACreateEvent ();
1879 WSAEventSelect (irtlan_socket, IrtLanEvent,FD_READ);
1880 #endif
1881 while (1) {
1882 #ifdef WIN32
1883 res = WaitForSingleObject (IrtLanEvent,1000);
1884 if (res == WAIT_TIMEOUT) break;
1885 ResetEvent (IrtLanEvent);
1887 #endif
1889 #ifdef LINUX
1890 FD_ZERO (&events);
1892 FD_SET (irtlan_socket,&events);
1893 maxfd = irtlan_socket + 1;
1895 tv.tv_sec = 1;
1896 tv.tv_usec = 0;
1898 wait = select (maxfd,&events,NULL,NULL,&tv);
1899 if (!wait) break;
1900 #endif
1902 i = sizeof (send_adr);
1903 res = recvfrom(irtlan_socket,(byte *)&ver,sizeof (GETVERSION_LAN),MSG_NOSIGNAL,(struct sockaddr *)&send_adr,&i);
1904 if (res == ver.len + 1 && ver.netcommand == RESULT_GETVERSION) {
1905 dev = IRDevices + device_cnt;
1907 dev->io.if_type = IF_LAN;
1908 strcpy (dev->name,"IRTrans Ethernet");
1909 memcpy (dev->version,ver.ir_version,8);
1910 memcpy (dev->lan_version,ver.lan_version,8);
1911 dev->fw_capabilities = ver.ir_capabilities;
1912 dev->fw_serno = ver.ir_serno;
1913 memcpy (dev->mac_adr,ver.mac_adr,6);
1914 dev->cap_string[0] = 0;
1916 #ifdef WIN32
1917 dev->io.comport = NULL;
1918 dev->io.event = NULL;
1919 #endif
1921 #ifdef LINUX
1922 dev->io.comport = (int)NULL;
1923 dev->io.event = (int)NULL;
1924 #endif
1926 memset (&dev->io.IPAddr,0,sizeof (dev->io.IPAddr));
1927 dev->io.IPAddr[0].sin_family = AF_INET;
1929 dev->io.IPAddr[0].sin_addr.s_addr = send_adr.sin_addr.s_addr;
1930 dev->io.IPAddr[0].sin_port = htons ((word)IRTRANS_PORT);
1932 DecodeFunctions (dev);
1934 device_cnt++;
1937 res = 0;
1939 else if (st[q] == ':') sort_ir_devices (st + q + 1);
1940 if (res && !testflag) {
1941 return (ERR_OPEN);
1943 q = ++p;
1946 if (testflag) {
1947 printf ("\n\n");
1948 if (!device_cnt) {
1949 sprintf (st,"No IRTrans Devices found.\nAborting ...\n\n");
1950 log_print (st,LOG_FATAL);
1951 return (ERR_OPEN);
1953 else {
1954 for (i=0;i<device_cnt;i++) {
1955 sprintf (st,"Name : %s\n",IRDevices[i].name);
1956 log_print (st,LOG_INFO);
1957 sprintf (st,"Version: %s\n",IRDevices[i].version);
1958 log_print (st,LOG_INFO);
1959 sprintf (st,"FW SNo : %d\n",IRDevices[i].fw_serno);
1960 log_print (st,LOG_INFO);
1961 sprintf (st,"Capab : %s\n",IRDevices[i].cap_string);
1962 log_print (st,LOG_INFO);
1963 sprintf (st,"FW Cap : %d\n",IRDevices[i].fw_capabilities);
1964 log_print (st,LOG_INFO);
1965 if (IRDevices[i].io.if_type == IF_USB) {
1966 sprintf (st,"USB SNo: %s\n",IRDevices[i].usb_serno);
1967 log_print (st,LOG_INFO);
1968 sprintf (st,"Node : %s\n\n",IRDevices[i].device_node);
1969 log_print (st,LOG_INFO);
1971 if (IRDevices[i].io.if_type == IF_LAN) {
1972 sprintf (st,"LAN Ver: %s\n",IRDevices[i].lan_version);
1973 log_print (st,LOG_INFO);
1974 sprintf (st,"Mac Adr: %s\n",IRDevices[i].usb_serno);
1975 log_print (st,LOG_INFO);
1976 sprintf (st,"IP Adr : %s\n\n",IRDevices[i].device_node);
1977 log_print (st,LOG_INFO);
1983 return (0);
1986 void sort_ir_devices (char selstring[])
1988 int i,cnt,start,p,q;
1989 DEVICEINFO di[MAX_IR_DEVICES];
1991 memcpy (di,IRDevices,sizeof (DEVICEINFO) * MAX_IR_DEVICES);
1993 i = 0;
1994 while ((di[i].io.if_type == IF_RS232 || di[i].io.if_type == IF_SERUSB) && i < device_cnt) i++;
1995 start = cnt = i;
1996 p = q = 0;
1997 while (selstring[p]) {
1998 while (selstring[p] && selstring[p] != ' ' && selstring[p] != ',') p++;
1999 selstring[p] = 0;
2000 for (i = start;i < device_cnt;i++) {
2001 if (!strcmp (selstring+q,di[i].usb_serno) || (atoi (selstring+q) && (word)atoi (selstring+q) == di[i].fw_serno)) {
2002 memcpy(&IRDevices[cnt++],&di[i],sizeof (DEVICEINFO));
2005 q = ++p;
2007 device_cnt = cnt;
2011 int get_detail_deviceinfo (char serno[],char devnode[],byte if_type)
2013 int res;
2014 DEVICEINFO *dev;
2015 DWORD deviceID;
2016 char SerialNumber[16];
2017 char Description[64];
2019 #ifdef WIN32
2020 FT_DEVICE ftDevice;
2021 FT_STATUS ftStatus;
2022 enum FT_STATS stat;
2024 dev = IRDevices + device_cnt;
2025 strcpy (dev->device_node,devnode);
2026 dev->io.if_type = if_type;
2027 if (if_type == IF_USB) {
2028 if (memcmp (serno,"MM",2) && memcmp (serno,"IR",2)) return (ERR_OPEN);
2031 strcpy (dev->usb_serno,serno);
2032 stat = F_OpenEx(dev->usb_serno,FT_OPEN_BY_SERIAL_NUMBER,&(dev->io.usbport));
2034 if (!FT_SUCCESS(stat)) return (ERR_OPEN);
2036 ftStatus = F_GetDeviceInfo(dev->io.usbport,&ftDevice,&deviceID,SerialNumber,Description,NULL);
2038 if (!strcmp (Description,"IRTrans USB A")) return (ERR_OPEN);
2041 else {
2042 if (if_type == IF_SERUSB) res = OpenSerialPortEx (devnode,&(dev->io.comport),250);
2043 else res = OpenSerialPortEx (devnode,&(dev->io.comport),1000);
2044 if (res) return (ERR_OPEN);
2046 dev->io.event = CreateEvent (NULL,TRUE,FALSE,NULL);
2047 #endif
2049 #ifdef LINUX
2050 dev = IRDevices + device_cnt;
2051 dev->usb_serno[0] = 0;
2052 strcpy (dev->device_node,devnode);
2053 dev->io.if_type = if_type;
2055 res = OpenSerialPortEx (devnode,&(dev->io.comport),0);
2056 if (res) return (ERR_OPEN);
2057 #endif
2059 res = GetTransceiverVersionEx (dev);
2060 if (res) {
2061 #ifdef WIN32
2062 if (dev->io.if_type == IF_USB) F_Close (dev->io.usbport);
2063 CloseHandle (dev->io.comport);
2064 #else
2065 close (dev->io.comport);
2066 #endif
2067 return (ERR_OPEN);
2070 device_cnt++;
2071 return (0);
2075 int GetTransceiverVersionEx (DEVICEINFO *dev)
2077 int i,res;
2078 char data[8];
2079 IRDATA ir;
2080 GETVERSION_LAN ver;
2081 struct sockaddr_in send_adr;
2083 #ifdef LINUX
2084 fd_set events;
2085 int maxfd,wait;
2086 struct timeval tv;
2087 #endif
2089 ir.command = HOST_VERSION;
2090 ir.len = 3;
2092 FlushIoEx (dev);
2094 dev->version[0] = 0;
2095 res = WriteTransceiverEx (dev,&ir);
2097 dev->version[8] = 0;
2099 if (dev->io.if_type == IF_LAN) {
2100 dev->usb_serno[0] = 0;
2102 #ifdef WIN32
2103 res = WaitForSingleObject (IrtLanEvent,2000);
2104 if (res == WAIT_TIMEOUT) {
2105 #endif
2107 #ifdef LINUX
2108 FD_ZERO (&events);
2110 FD_SET (irtlan_socket,&events);
2111 maxfd = irtlan_socket + 1;
2113 tv.tv_sec = 2;
2114 tv.tv_usec = 0;
2116 wait = select (maxfd,&events,NULL,NULL,&tv);
2117 if (!wait) {
2118 #endif
2119 if (!(mode_flag & NO_INIT_LAN) || dev == IRDevices) return (ERR_VERSION);
2120 memcpy (dev->version,IRDevices[0].version,8);
2121 memcpy (dev->lan_version,IRDevices[0].lan_version,8);
2122 dev->fw_capabilities = IRDevices[0].fw_capabilities;
2123 dev->fw_serno = 0;
2124 memset (dev->mac_adr,0,6);
2127 else {
2128 i = sizeof (send_adr);
2129 res = recvfrom(irtlan_socket,(byte *)&ver,sizeof (GETVERSION_LAN),MSG_NOSIGNAL,(struct sockaddr *)&send_adr,&i);
2130 if (res <= 0 || res != ver.len + 1 || ver.netcommand != RESULT_GETVERSION) {
2131 if (!(mode_flag & NO_INIT_LAN) || dev == IRDevices) return (ERR_VERSION);
2132 memcpy (dev->version,IRDevices[0].version,8);
2133 memcpy (dev->lan_version,IRDevices[0].lan_version,8);
2134 dev->fw_capabilities = IRDevices[0].fw_capabilities;
2135 dev->fw_serno = 0;
2136 memset (dev->mac_adr,0,6);
2138 else {
2139 memcpy (dev->version,ver.ir_version,8);
2140 memcpy (dev->lan_version,ver.lan_version,8);
2141 dev->fw_capabilities = ver.ir_capabilities;
2142 dev->fw_serno = ver.ir_serno;
2143 memcpy (dev->mac_adr,ver.mac_adr,6);
2148 else {
2149 res = ReadIRStringEx (dev,dev->version,8,500);
2150 if (res != 8) return (ERR_READVERSION);
2152 if (strcmp (dev->version+1,MINIMUM_SW_VERSION) < 0) return (ERR_VERSION);
2154 if (strcmp (dev->version+1,"2.24.01") >= 0) {
2155 res = ReadIRStringEx (dev,data,8,500);
2156 if (res != 8) return (ERR_READVERSION);
2158 if (strcmp (dev->version+1,"4.04.01") >= 0) {
2159 memcpy (&dev->fw_capabilities,data,4);
2160 swap_int (&dev->fw_capabilities);
2161 memcpy (&dev->fw_serno,data + 4,4);
2162 swap_int (&dev->fw_serno);
2164 else {
2165 memcpy (&dev->fw_capabilities,data + 1,2);
2166 swap_int (&dev->fw_capabilities);
2167 memcpy (&dev->fw_serno,data + 3,4);
2168 swap_int (&dev->fw_serno);
2170 if (dev->fw_serno == 2802 || dev->fw_serno == 0x11223344) dev->fw_serno = 0;
2175 DecodeFunctions (dev);
2177 return (0);
2180 void DecodeFunctions (DEVICEINFO *dev)
2182 dev->io.raw_repeat = 0;
2183 dev->io.ext_carrier = 0;
2184 dev->io.inst_receive_mode = 0;
2185 dev->io.advanced_lcd = 0;
2186 if (strcmp (dev->version+1,"4.00.00") >= 0) dev->io.time_len = 8; // Version 4.0 with TIME_LEN = 8
2187 else dev->io.time_len = 6;
2188 if (strcmp (dev->version+1,"4.02.00") >= 0) dev->io.raw_repeat = 1; // Version 4.2 with RAW_REPEAT feature
2189 if (strcmp (dev->version+1,"4.03.00") >= 0) dev->io.ext_carrier = 1; // Version 4.3 mit erweitertem Carrier
2190 if (strcmp (dev->version+1,"4.04.06") >= 0) dev->io.inst_receive_mode |= 1; // Version 4.4.6 mit geändertem Instant Rcv
2191 if (strcmp (dev->version+1,"4.04.07") >= 0 &&
2192 (dev->version[0] == 'D')) dev->io.inst_receive_mode |= 2; // Version 4.4.7 mit geändertem Instant Rcv
2193 if (strcmp (dev->version+1,"4.04.17") >= 0 &&
2194 (dev->version[0] == 'D')) dev->io.advanced_lcd |= 1; // Version 4.4.17 mit zusätzlichen LCD Funktionen
2195 if (strcmp (dev->version+1,"4.04.23") >= 0 &&
2196 (dev->version[0] == 'D')) dev->io.advanced_lcd |= 2; // Version 4.4.23 mit neuem LCD Protokoll
2198 if (dev->version[0] == 'F') dev->io.advanced_lcd = 7; // Uneed V2
2200 if (dev->fw_capabilities & FN_DEBOUNCE) dev->io.inst_receive_mode |= 2;
2202 if (strcmp (dev->version+1,"5.05.15") >= 0 && (dev->io.if_type == IF_USB || (
2203 dev->io.if_type == IF_SERUSB && (dev->version[0] == 'U' || dev->version[0] == 'X' || dev->version[0] == 'D')))) dev->io.io_seq_mode = 1; // IO Sequence Mode
2205 switch (dev->version[0]) {
2206 case 'X':
2207 strcpy (dev->name,"IRTrans Translator");
2208 break;
2209 case 'U':
2210 strcpy (dev->name,"IRTrans USB");
2211 break;
2212 case 'V':
2213 strcpy (dev->name,"IRTrans USB 455kHz");
2214 break;
2215 case 'S':
2216 strcpy (dev->name,"IRTrans RS232");
2217 break;
2218 case 'T':
2219 strcpy (dev->name,"IRTrans RS232 455kHz");
2220 break;
2221 case 'E':
2222 strcpy (dev->name,"IRTrans LAN");
2223 break;
2224 case 'G':
2225 strcpy (dev->name,"IRTrans LAN 455kHz");
2226 break;
2228 dev->cap_string[0] = 0;
2230 if (dev->fw_capabilities & FN_POWERON) strcat (dev->cap_string,"Power On; ");
2231 if (dev->fw_capabilities & FN_SOFTID) strcat (dev->cap_string,"Soft ID; ");
2232 if (dev->fw_capabilities & FN_DUALSND) strcat (dev->cap_string,"Dual Transmitter Drivers; ");
2233 if (dev->fw_capabilities & FN_IRDB) strcat (dev->cap_string,"IR Database; ");
2235 if (dev->io.if_type == IF_LAN) {
2236 sprintf (dev->usb_serno,"%02x-%02x-%02x-%02x-%02x-%02x",dev->mac_adr[0],dev->mac_adr[1],dev->mac_adr[2],dev->mac_adr[3],dev->mac_adr[4],dev->mac_adr[5]);
2237 sprintf (dev->device_node,"%d.%d.%d.%d",dev->io.IPAddr[0].sin_addr.s_addr & 0xff,(dev->io.IPAddr[0].sin_addr.s_addr >> 8) & 0xff,(dev->io.IPAddr[0].sin_addr.s_addr >> 16) & 0xff,(dev->io.IPAddr[0].sin_addr.s_addr >> 24) & 0xff);
2242 void FlushIoEx (DEVICEINFO *dev)
2244 #ifdef WIN32
2245 if (dev->io.if_type == IF_USB) FlushUSBEx (dev->io.usbport);
2246 else FlushComEx (dev->io.comport);
2247 #endif
2249 #ifdef LINUX
2250 FlushComEx (dev->io.comport);
2251 #endif
2255 void Hexdump_File (IRDATA *ird)
2257 int i;
2258 byte *pnt;
2260 pnt = (byte *)ird;
2261 for (i=0;i < ird->len;i++) {
2262 fprintf (hexfp,"0x%02x ",pnt[i]);
2263 if (((i+1)%16) == 0) fprintf (hexfp,"\n");
2265 fprintf (hexfp,"\n\n");
2266 fflush (hexfp);
2267 hexflag = 0;
2270 void Hexdump_IO (IRDATA *ird)
2272 int i;
2273 char st[2048],nm[100];
2274 byte *pnt;
2276 strcpy (st,"IODUMP: ");
2277 pnt = (byte *)ird;
2278 for (i=0;i < ird->len;i++) {
2279 sprintf (nm,"0x%x ",pnt[i]);
2280 strcat (st,nm);
2282 strcat (st,"\n");
2284 log_print (st,LOG_DEBUG);
2287 void Hexdump_Medialon (IRDATA *ird)
2289 int i;
2290 char st[2048],nm[100];
2291 byte *pnt;
2293 st[0] = 0;
2294 pnt = (byte *)ird;
2295 for (i=0;i < ird->len;i++) {
2296 sprintf (nm,"!%02x",pnt[i]);
2297 strcat (st,nm);
2299 strcat (st,"\n");
2301 log_print (st,LOG_DEBUG);
2306 int WriteTransceiverEx (DEVICEINFO *dev,IRDATA *src)
2308 byte res = 0,i,pos;
2309 int count = 0,max,timeo;
2310 IRDATA send;
2311 byte buffer[1024],sbuffer[1024];
2313 dev->io.io_sequence++;
2315 if (dev->version[0] == 0 && src->command != HOST_VERSION) return (ERR_WRONGBUS);
2317 swap_irdata (src,&send);
2319 if (time_len != TIME_LEN && ((src->command == HOST_SEND && !(src->mode & NON_IRMODE)) || src->command == HOST_LEARNIRQUICK)) {
2320 if (src->mode & RAW_DATA) {
2321 if (src->ir_length > OLD_LENRAW) return (ERR_LONGRAW);
2323 else {
2324 if (src->time_cnt > 6) return (ERR_LONGDATA);
2325 ConvertToIRTRANS3 (&send);
2329 if (dev->io.io_seq_mode || (src->command == HOST_VERSION && (dev->io.if_type == IF_USB || dev->io.if_type == IF_SERUSB))) {
2330 if (dev->version[0]) max = 10;
2331 else max = 4;
2332 timeo = 250;
2334 else {
2335 if (dev->version[0]) max = 5;
2336 else max = 2;
2337 timeo = 500;
2340 send.checksumme = get_checksumme (&send);
2341 do {
2342 if (mode_flag & HEXDUMP) Hexdump_IO (&send);
2343 if (mode_flag & MEDIALON) Hexdump_Medialon (&send);
2344 if (hexflag) Hexdump_File (&send);
2346 if (dev->io.if_type == IF_LAN) return (IRTransLanSend (dev,&send));
2348 if (dev->io.io_seq_mode || (src->command == HOST_VERSION && (dev->io.if_type == IF_USB || dev->io.if_type == IF_SERUSB))) {
2349 memcpy (sbuffer,&send,send.len);
2350 sbuffer[send.len] = dev->io.io_sequence;
2351 res = WriteIRStringEx (dev,sbuffer,send.len + 1);
2352 if (dev->version[0]) max = 10;
2353 else max = 4;
2355 else {
2356 if (dev->version[0]) max = 10;
2357 else max = 4;
2358 res = WriteIRStringEx (dev,(byte *)&send,send.len);
2361 if (res) return (ERR_TIMEOUT);
2363 *buffer = 0;
2364 res = ReadIRStringEx (dev,buffer,1,(word)timeo);
2365 count++;
2366 if (res != 1 || *buffer != 'O') {
2367 sprintf (sbuffer,"IRTRans Send status: %d - %d\n",res,*buffer);
2368 log_print (sbuffer,LOG_DEBUG);
2369 if (res == 1) {
2370 res = ReadIRStringEx (dev,buffer+1,250,20);
2371 if (res <= 1) {
2372 if (*buffer == 'R') return (ERR_RESEND);
2374 else {
2375 res++;
2376 pos = 0;
2377 while (pos < res) {
2378 if (dev->io.receive_buffer_cnt == 4) {
2379 i = 1;
2380 while (buffer[pos+i] && buffer[pos+i] != 'E' && buffer[pos+i] != 'O' && buffer[pos+i] != 'E' && (pos+i) < res) i++;
2382 else {
2383 dev->io.receive_buffer[dev->io.receive_buffer_cnt][0] = buffer[pos];
2384 i = 1;
2385 while (buffer[pos+i] && buffer[pos+i] != 'E' && buffer[pos+i] != 'O' && buffer[pos+i] != 'E' && (pos+i) < res) {
2386 dev->io.receive_buffer[dev->io.receive_buffer_cnt][i] = buffer[pos+i];
2387 i++;
2389 dev->io.receive_buffer[dev->io.receive_buffer_cnt][i] = 0;
2390 dev->io.receive_cnt[dev->io.receive_buffer_cnt] = i;
2392 pos += i;
2393 dev->io.receive_buffer_cnt++;
2394 if (dev->io.inst_receive_mode & 1 && buffer[pos] == 0) pos++;
2396 if (buffer[res-1] == 'O') return (0);
2397 res = ReadIRStringEx (dev,buffer,1,500);
2398 if (*buffer == 'O') return (0);
2402 FlushIoEx (dev);
2403 msSleep (150);
2404 if (count > 5) msSleep (timeo * 2);
2405 res = 0;
2407 } while (res == 0 && count < max && !(mode_flag & NO_RECONNECT));
2409 sprintf (sbuffer,"IRTRans Send Done: %d\n",count);
2410 log_print (sbuffer,LOG_DEBUG);
2412 if (count == 10) return (ERR_TIMEOUT);
2413 return (0);
2417 void ConvertToIRTRANS3 (IRDATA *ird)
2419 IRDATA3 irold;
2421 memcpy (&irold,ird,21);
2422 memcpy (irold.pulse_len,ird->pulse_len,12);
2423 memcpy (&irold.time_cnt,&ird->time_cnt,CODE_LEN + 3);
2424 irold.len = ird->len - 8;
2425 memcpy (ird,&irold,irold.len);
2429 void SwapStatusbuffer (STATUS_BUFFER *sb)
2431 int i;
2433 if (!byteorder) return;
2435 for (i=0;i<16;i++) {
2436 swap_word (&sb->stat[i].send_mask);
2437 swap_int ((int32_t *)&sb->stat[i].capabilities);
2442 void swap_irdata (IRDATA *src,IRDATA *tar)
2444 int i;
2445 IRDATA *ir;
2447 if (tar) {
2448 memcpy (tar,src,sizeof (IRDATA));
2449 ir = tar;
2451 else ir = src;
2453 if (!byteorder) return;
2455 swap_word (&ir->target_mask);
2457 if (!(ir -> mode & (RAW_DATA | LCD_DATA))) {
2459 for (i=0;i < TIME_LEN;i++) {
2460 swap_word (ir->pause_len + i);
2461 swap_word (ir->pulse_len + i);
2467 void swap_word (word *pnt)
2469 byte *a,v;
2471 if (!byteorder) return;
2473 a = (byte *)pnt;
2474 v = a[0];
2475 a[0] = a[1];
2476 a[1] = v;
2480 void swap_int (int32_t *pnt)
2482 byte *a,v;
2484 if (!byteorder) return;
2486 a = (byte *)pnt;
2487 v = a[0];
2488 a[0] = a[3];
2489 a[3] = v;
2491 v = a[1];
2492 a[1] = a[2];
2493 a[2] = v;
2497 byte get_checksumme (IRDATA *ir)
2499 int i = 2;
2500 byte cs = 0;
2501 while (i < ir->len) cs += ((byte *)ir)[i++];
2502 return (cs);
2506 int GetByteorder ()
2508 char arr[2];
2509 short *pnt;
2511 pnt = (short *)arr;
2512 *pnt = 1;
2514 return (arr[1]);
2518 int WriteIRStringEx (DEVICEINFO *dev,byte pnt[],int len)
2520 #ifdef WIN32
2521 if (dev->io.if_type == IF_USB) {
2522 WriteUSBStringEx (dev,pnt,len);
2523 return (0);
2526 return (WriteSerialStringEx (dev,pnt,len));
2527 #endif
2529 #ifdef LINUX
2530 return WriteSerialStringEx (dev,pnt,len);
2531 #endif
2534 int ReadIRStringEx (DEVICEINFO *dev,byte pnt[],int len,word timeout)
2537 #ifdef WIN32
2538 if (dev->io.if_type == IF_USB) return (ReadUSBStringEx (dev,pnt,len,timeout));
2539 else return (ReadSerialStringEx (dev,pnt,len,timeout));
2540 #endif
2542 #ifdef LINUX
2543 return (ReadSerialStringEx (dev,pnt,len,timeout));
2544 #endif
2547 void ConvertLCDCharset (byte *pnt)
2549 int i,t;
2550 if (new_lcd_flag) t = 1;
2551 else t = 0;
2552 for (i=0;i < 200;i++) pnt[i] = DispConvTable[1][pnt[i]];
2556 void InitConversionTables ()
2558 int i;
2559 int num;
2561 num = 0;
2562 memset (DispConvTable[num],' ',256); // Initialize Table
2564 for (i=0;i <= 127;i++) DispConvTable[num][i] = i; // Set lower 7 Bits
2566 DispConvTable[num][192] = 65;
2567 DispConvTable[num][193] = 65;
2568 DispConvTable[num][194] = 65;
2569 DispConvTable[num][195] = 65;
2570 DispConvTable[num][197] = 0x81;
2571 DispConvTable[num][198] = 0x90;
2572 DispConvTable[num][199] = 0x99;
2573 DispConvTable[num][200] = 69;
2574 DispConvTable[num][201] = 69;
2575 DispConvTable[num][202] = 69;
2576 DispConvTable[num][203] = 69;
2577 DispConvTable[num][204] = 73;
2578 DispConvTable[num][205] = 73;
2579 DispConvTable[num][206] = 73;
2580 DispConvTable[num][207] = 73;
2581 DispConvTable[num][208] = 68;
2582 DispConvTable[num][209] = 78;
2583 DispConvTable[num][210] = 79;
2584 DispConvTable[num][211] = 79;
2585 DispConvTable[num][212] = 79;
2586 DispConvTable[num][213] = 79;
2587 DispConvTable[num][216] = 0x88;
2588 DispConvTable[num][217] = 85;
2589 DispConvTable[num][218] = 85;
2590 DispConvTable[num][219] = 85;
2591 DispConvTable[num][221] = 89;
2592 DispConvTable[num][224] = 97;
2593 DispConvTable[num][225] = 0x83;
2594 DispConvTable[num][226] = 97;
2595 DispConvTable[num][227] = 97;
2596 DispConvTable[num][229] = 0x84;
2597 DispConvTable[num][230] = 0x91;
2598 DispConvTable[num][231] = 0x99;
2599 DispConvTable[num][232] = 101;
2600 DispConvTable[num][233] = 101;
2601 DispConvTable[num][234] = 101;
2602 DispConvTable[num][235] = 101;
2603 DispConvTable[num][236] = 105;
2604 DispConvTable[num][237] = 105;
2605 DispConvTable[num][238] = 105;
2606 DispConvTable[num][239] = 105;
2607 DispConvTable[num][241] = 0xee;
2608 DispConvTable[num][242] = 111;
2609 DispConvTable[num][243] = 111;
2610 DispConvTable[num][244] = 111;
2611 DispConvTable[num][245] = 111;
2612 DispConvTable[num][248] = 0x88;
2613 DispConvTable[num][249] = 117;
2614 DispConvTable[num][250] = 117;
2615 DispConvTable[num][251] = 117;
2616 DispConvTable[num][253] = 121;
2617 DispConvTable[num][255] = 121;
2621 DispConvTable[num][0xC4] = 0x80; // Set Conversion (Upper 8 Bits)
2622 DispConvTable[num][0xC5] = 0x82;
2623 DispConvTable[num][0xE1] = 0x83;
2624 DispConvTable[num][0xE5] = 0x84;
2625 DispConvTable[num][0xD6] = 0x86;
2626 DispConvTable[num][0xF6] = 0x87;
2627 DispConvTable[num][0xD8] = 0x88;
2628 DispConvTable[num][0xF8] = 0x89;
2629 DispConvTable[num][0xDC] = 0x8A;
2630 DispConvTable[num][0xFC] = 0x8B;
2631 DispConvTable[num][0x5C] = 0x8C;
2632 DispConvTable[num][0xA5] = 0x5C;
2633 DispConvTable[num][0xA7] = 0x8F;
2634 DispConvTable[num][0xC6] = 0x90;
2635 DispConvTable[num][0xE6] = 0x91;
2636 DispConvTable[num][0xA3] = 0x92;
2637 DispConvTable[num][0xA6] = 0x98;
2638 DispConvTable[num][0xC7] = 0x99;
2639 DispConvTable[num][0xB0] = 0xDF;
2640 DispConvTable[num][0xE4] = 0xE1;
2641 DispConvTable[num][0xDF] = 0xE2;
2642 DispConvTable[num][0xB5] = 0xE4;
2643 DispConvTable[num][0xA4] = 0xEB;
2644 DispConvTable[num][0xA2] = 0xEC;
2645 DispConvTable[num][0xF1] = 0xEE;
2646 DispConvTable[num][0xF7] = 0xFD;
2647 DispConvTable[num][0x94] = 0x94;
2648 DispConvTable[num][0x95] = 0x95;
2649 DispConvTable[num][0x96] = 0x96;
2650 DispConvTable[num][0x97] = 0x97;
2651 DispConvTable[num][0x9b] = 0x9b;
2652 DispConvTable[num][0x9c] = 0x9c;
2653 DispConvTable[num][0x9d] = 0x9d;
2654 DispConvTable[num][0x9e] = 0x9e;
2655 DispConvTable[num][0x9f] = 0x9f;
2657 num = 1;
2658 memset (DispConvTable[num],' ',256); // Initialize Table
2660 for (i=0;i <= 127;i++) DispConvTable[num][i] = i; // Set lower 7 Bits
2661 DispConvTable[num][192] = 65;
2662 DispConvTable[num][193] = 65;
2663 DispConvTable[num][194] = 65;
2664 DispConvTable[num][195] = 65;
2665 DispConvTable[num][197] = 0x81;
2666 DispConvTable[num][198] = 0x90;
2667 DispConvTable[num][199] = 0x99;
2668 DispConvTable[num][200] = 69;
2669 DispConvTable[num][201] = 69;
2670 DispConvTable[num][202] = 69;
2671 DispConvTable[num][203] = 69;
2672 DispConvTable[num][204] = 73;
2673 DispConvTable[num][205] = 73;
2674 DispConvTable[num][206] = 73;
2675 DispConvTable[num][207] = 73;
2676 DispConvTable[num][208] = 68;
2677 DispConvTable[num][209] = 78;
2678 DispConvTable[num][210] = 79;
2679 DispConvTable[num][211] = 79;
2680 DispConvTable[num][212] = 79;
2681 DispConvTable[num][213] = 79;
2682 DispConvTable[num][216] = 0x88;
2683 DispConvTable[num][217] = 85;
2684 DispConvTable[num][218] = 85;
2685 DispConvTable[num][219] = 85;
2686 DispConvTable[num][221] = 89;
2687 DispConvTable[num][224] = 97;
2688 DispConvTable[num][225] = 0x83;
2689 DispConvTable[num][226] = 97;
2690 DispConvTable[num][227] = 97;
2691 DispConvTable[num][229] = 0x84;
2692 DispConvTable[num][230] = 0x91;
2693 DispConvTable[num][231] = 0x99;
2694 DispConvTable[num][232] = 101;
2695 DispConvTable[num][233] = 101;
2696 DispConvTable[num][234] = 101;
2697 DispConvTable[num][235] = 101;
2698 DispConvTable[num][236] = 105;
2699 DispConvTable[num][237] = 105;
2700 DispConvTable[num][238] = 105;
2701 DispConvTable[num][239] = 105;
2702 DispConvTable[num][241] = 0xee;
2703 DispConvTable[num][242] = 111;
2704 DispConvTable[num][243] = 111;
2705 DispConvTable[num][244] = 111;
2706 DispConvTable[num][245] = 111;
2707 DispConvTable[num][248] = 0x88;
2708 DispConvTable[num][249] = 117;
2709 DispConvTable[num][250] = 117;
2710 DispConvTable[num][251] = 117;
2711 DispConvTable[num][253] = 121;
2712 DispConvTable[num][255] = 255;
2716 DispConvTable[num][0xC4] = 0x80; // Set Conversion (Upper 8 Bits)
2717 DispConvTable[num][0xC5] = 0x82;
2718 DispConvTable[num][0xE1] = 0x83;
2719 DispConvTable[num][0xE5] = 0x84;
2720 DispConvTable[num][0xD6] = 0x86;
2721 DispConvTable[num][0xF6] = 0x87;
2722 DispConvTable[num][0xD8] = 0x88;
2723 DispConvTable[num][0xF8] = 0x89;
2724 DispConvTable[num][0xDC] = 0x8A;
2725 DispConvTable[num][0xFC] = 0x8B;
2726 DispConvTable[num][0x5C] = 0x8C;
2727 DispConvTable[num][0xA5] = 0x5C;
2728 DispConvTable[num][0xA7] = 0x8F;
2729 DispConvTable[num][0xC6] = 0x90;
2730 DispConvTable[num][0xE6] = 0x91;
2731 DispConvTable[num][0xA3] = 0x92;
2732 DispConvTable[num][0xA6] = 0x98;
2733 DispConvTable[num][0xC7] = 0x99;
2734 DispConvTable[num][0xB0] = 0xDF;
2735 DispConvTable[num][0xE4] = 0xE1;
2736 DispConvTable[num][0xDF] = 0xE2;
2737 DispConvTable[num][0xB5] = 0xE4;
2738 DispConvTable[num][0xA4] = 0xEB;
2739 DispConvTable[num][0xA2] = 0xEC;
2740 DispConvTable[num][0xF1] = 0xEE;
2741 DispConvTable[num][0xF7] = 0xFD;
2742 DispConvTable[num][0x94] = 0x94;
2743 DispConvTable[num][0x95] = 0x95;
2744 DispConvTable[num][0x96] = 0x96;
2745 DispConvTable[num][0x97] = 0x97;
2746 DispConvTable[num][0x9b] = 0x9b;
2747 DispConvTable[num][0x9c] = 0x9c;
2748 DispConvTable[num][0x9d] = 0x9d;
2749 DispConvTable[num][0x9e] = 0x9e;
2750 DispConvTable[num][0x9f] = 0x9f;
2753 void SetLCDProcCharsV (byte dat[])
2755 int i;
2757 dat[0] = 4;
2759 i = 1;
2760 dat[i] = (i / 9) + 1;
2761 dat[i+1] = 16;
2762 dat[i+2] = 16;
2763 dat[i+3] = 16;
2764 dat[i+4] = 16;
2765 dat[i+5] = 16;
2766 dat[i+6] = 16;
2767 dat[i+7] = 16;
2768 dat[i+8] = 16;
2770 i = 10;
2771 dat[i] = (i / 9) + 1;
2772 dat[i+1] = 24;
2773 dat[i+2] = 24;
2774 dat[i+3] = 24;
2775 dat[i+4] = 24;
2776 dat[i+5] = 24;
2777 dat[i+6] = 24;
2778 dat[i+7] = 24;
2779 dat[i+8] = 24;
2781 i = 19;
2782 dat[i] = (i / 9) + 1;
2783 dat[i+1] = 28;
2784 dat[i+2] = 28;
2785 dat[i+3] = 28;
2786 dat[i+4] = 28;
2787 dat[i+5] = 28;
2788 dat[i+6] = 28;
2789 dat[i+7] = 28;
2790 dat[i+8] = 28;
2792 i = 28;
2793 dat[i] = (i / 9) + 1;
2794 dat[i+1] = 30;
2795 dat[i+2] = 30;
2796 dat[i+3] = 30;
2797 dat[i+4] = 30;
2798 dat[i+5] = 30;
2799 dat[i+6] = 30;
2800 dat[i+7] = 30;
2801 dat[i+8] = 30;
2805 void SetSpecialChars (byte dat[])
2807 int i;
2809 dat[0] = 6;
2811 i = 1;
2812 dat[i] = (i / 9) + 1; // Play
2813 dat[i+1] = 16;
2814 dat[i+2] = 24;
2815 dat[i+3] = 28;
2816 dat[i+4] = 30;
2817 dat[i+5] = 28;
2818 dat[i+6] = 24;
2819 dat[i+7] = 16;
2820 dat[i+8] = 0;
2822 i = 10;
2823 dat[i] = (i / 9) + 1; // Pause
2824 dat[i+1] = 27;
2825 dat[i+2] = 27;
2826 dat[i+3] = 27;
2827 dat[i+4] = 27;
2828 dat[i+5] = 27;
2829 dat[i+6] = 27;
2830 dat[i+7] = 27;
2831 dat[i+8] = 0;
2833 i = 19;
2834 dat[i] = (i / 9) + 1; // Stop
2835 dat[i+1] = 0;
2836 dat[i+2] = 31;
2837 dat[i+3] = 31;
2838 dat[i+4] = 31;
2839 dat[i+5] = 31;
2840 dat[i+6] = 31;
2841 dat[i+7] = 0;
2842 dat[i+8] = 0;
2844 i = 28;
2845 dat[i] = (i / 9) + 1; // Next
2846 dat[i+1] = 17;
2847 dat[i+2] = 25;
2848 dat[i+3] = 29;
2849 dat[i+4] = 31;
2850 dat[i+5] = 29;
2851 dat[i+6] = 25;
2852 dat[i+7] = 17;
2853 dat[i+8] = 0;
2855 i = 37;
2856 dat[i] = (i / 9) + 1; // Prev
2857 dat[i+1] = 17;
2858 dat[i+2] = 19;
2859 dat[i+3] = 23;
2860 dat[i+4] = 31;
2861 dat[i+5] = 23;
2862 dat[i+6] = 19;
2863 dat[i+7] = 17;
2864 dat[i+8] = 0;
2866 i = 46;
2867 dat[i] = (i / 9) + 1; // Rew
2868 dat[i+1] = 1;
2869 dat[i+2] = 3;
2870 dat[i+3] = 7;
2871 dat[i+4] = 15;
2872 dat[i+5] = 7;
2873 dat[i+6] = 3;
2874 dat[i+7] = 1;
2875 dat[i+8] = 0;
2879 int SetTransceiverModus (byte mode,word send_mask,byte addr,char *hotcode,int hotlen,byte extended_mode,byte extended_mode2,byte usb_mode)
2881 MODE_BUFFER md;
2882 byte res;
2883 md.sbus_command = HOST_SETMODE;
2884 md.sbus_address = 0;
2885 if (addr <= 15) md.sbus_address |= addr;
2886 if (addr == 'L') md.sbus_address |= ADRESS_LOCAL;
2887 if (addr == '*') md.sbus_address |= ADRESS_ALL;
2889 md.hotcode_len = hotlen;
2890 memcpy (md.hotcode,hotcode,hotlen);
2892 md.hotcode[hotlen] = extended_mode;
2893 md.hotcode[hotlen+1] = extended_mode2;
2895 md.sbus_len = sizeof (MODE_BUFFER) + md.hotcode_len - 98;
2898 md.mode = mode;
2899 md.target_mask = send_mask;
2901 res = WriteTransceiver ((IRDATA *)&md,usb_mode);
2902 if (res) return (res);
2904 return (0);
2908 int GetTransceiverVersion (char version [],unsigned int *cap,unsigned int *serno,char mac_adr[],byte usbmode)
2910 int res,i;
2911 IRDATA ir;
2912 char data[8];
2913 GETVERSION_LAN ver;
2914 struct sockaddr_in send_adr;
2916 #ifdef LINUX
2917 fd_set events;
2918 int maxfd,wait;
2919 struct timeval tv;
2920 #endif
2922 ir.command = HOST_VERSION;
2923 ir.checksumme = HOST_VERSION;
2924 ir.len = 3;
2926 // FlushIoEx (dev);
2928 *cap = 0;
2929 *serno = 0;
2930 version[0] = 0;
2931 memset (mac_adr,0,6);
2933 if (usbmode == 2) {
2935 IRTransLanSend (NULL,&ir);
2937 #ifdef WIN32
2938 res = WaitForSingleObject (IrtLanEvent,2000);
2939 if (res == WAIT_TIMEOUT) return (ERR_READVERSION);
2940 WSAResetEvent (IrtLanEvent);
2941 #endif
2943 #ifdef LINUX
2944 FD_ZERO (&events);
2946 FD_SET (irtlan_socket,&events);
2947 maxfd = irtlan_socket + 1;
2949 tv.tv_sec = 2;
2950 tv.tv_usec = 0;
2952 wait = select (maxfd,&events,NULL,NULL,&tv);
2953 if (!wait) return (ERR_READVERSION);
2954 #endif
2957 i = sizeof (send_adr);
2958 res = recvfrom(irtlan_socket,(byte *)&ver,sizeof (GETVERSION_LAN),MSG_NOSIGNAL,(struct sockaddr *)&send_adr,&i);
2959 if (res <= 0 || res != ver.len + 1 || ver.netcommand != RESULT_GETVERSION) return (ERR_READVERSION);
2961 version[8] = 0;
2962 memcpy (version,ver.ir_version,8);
2963 // memcpy (dev->lan_version,ver.lan_version,8);
2964 *cap = ver.ir_capabilities;
2965 *serno = ver.ir_serno;
2966 memcpy (mac_adr,ver.mac_adr,6);
2969 else {
2970 res = WriteTransceiver (&ir,usbmode);
2972 version[8] = 0;
2973 res = ReadIRString (version,8,500,usbmode);
2974 if (res != 8) return (ERR_READVERSION);
2976 if (strcmp (version+1,"2.24.01") >= 0) {
2977 res = ReadIRString (data,8,500,usbmode);
2978 if (res != 8) return (ERR_READVERSION);
2980 if (strcmp (version+1,"4.04.01") >= 0) {
2981 memcpy (cap,data,4);
2982 swap_int (cap);
2983 memcpy (serno,data + 4,4);
2984 swap_int ((int32_t *)serno);
2986 else {
2987 memcpy (cap,data + 1,2);
2988 swap_int (cap);
2989 memcpy (serno,data + 3,4);
2990 swap_int ((int32_t *)serno);
2992 if (*serno == 2802 || *serno == 0x11223344) *serno = 0;
2996 return (0);
3000 int WriteTransceiverL (IRDATA *src,byte usb_mode);
3002 int GetTransceiverVersionL (char version [],unsigned int *cap,unsigned int *serno,char mac_adr[],byte usbmode)
3004 int res,i;
3005 IRDATA ir;
3006 char data[8];
3007 GETVERSION_LAN ver;
3008 struct sockaddr_in send_adr;
3010 #ifdef LINUX
3011 fd_set events;
3012 int maxfd,wait;
3013 struct timeval tv;
3014 #endif
3016 ir.command = HOST_VERSION;
3017 ir.checksumme = HOST_VERSION;
3018 ir.len = 3;
3020 // FlushIoEx (dev);
3022 *cap = 0;
3023 *serno = 0;
3024 version[0] = 0;
3025 memset (mac_adr,0,6);
3027 if (usbmode == 2) {
3029 IRTransLanSend (NULL,&ir);
3031 #ifdef WIN32
3032 res = WaitForSingleObject (IrtLanEvent,2000);
3033 if (res == WAIT_TIMEOUT) return (ERR_READVERSION);
3034 WSAResetEvent (IrtLanEvent);
3035 #endif
3037 #ifdef LINUX
3038 FD_ZERO (&events);
3040 FD_SET (irtlan_socket,&events);
3041 maxfd = irtlan_socket + 1;
3043 tv.tv_sec = 2;
3044 tv.tv_usec = 0;
3046 wait = select (maxfd,&events,NULL,NULL,&tv);
3047 if (!wait) return (ERR_READVERSION);
3048 #endif
3051 i = sizeof (send_adr);
3052 res = recvfrom(irtlan_socket,(byte *)&ver,sizeof (GETVERSION_LAN),MSG_NOSIGNAL,(struct sockaddr *)&send_adr,&i);
3053 if (res <= 0 || res != ver.len + 1 || ver.netcommand != RESULT_GETVERSION) return (ERR_READVERSION);
3055 version[8] = 0;
3056 memcpy (version,ver.ir_version,8);
3057 // memcpy (dev->lan_version,ver.lan_version,8);
3058 *cap = ver.ir_capabilities;
3059 *serno = ver.ir_serno;
3060 memcpy (mac_adr,ver.mac_adr,6);
3063 else {
3064 res = WriteTransceiverL (&ir,usbmode);
3066 version[8] = 0;
3067 res = ReadIRString (version,8,500,usbmode);
3068 if (res != 8) return (ERR_READVERSION);
3070 if (strcmp (version+1,"2.24.01") >= 0) {
3071 res = ReadIRString (data,8,500,usbmode);
3072 if (res != 8) return (ERR_READVERSION);
3074 if (strcmp (version+1,"4.04.01") >= 0) {
3075 memcpy (cap,data,4);
3076 swap_int (cap);
3077 memcpy (serno,data + 4,4);
3078 swap_int ((int32_t *)serno);
3080 else {
3081 memcpy (cap,data + 1,2);
3082 swap_int (cap);
3083 memcpy (serno,data + 3,4);
3084 swap_int ((int32_t *)serno);
3086 if (*serno == 2802 || *serno == 0x11223344) *serno = 0;
3090 return (0);
3097 int WriteTransceiver (IRDATA *src,byte usb_mode)
3099 byte res = 0;
3100 char st[100];
3101 int count = 0;
3102 IRDATA send;
3104 swap_irdata (src,&send);
3106 send.checksumme = get_checksumme (&send);
3107 do {
3108 if (usb_mode == 1) FlushUSB ();
3109 else if (!usb_mode) FlushCom ();
3110 if (mode_flag & HEXDUMP) Hexdump_IO (&send);
3111 if (hexflag) Hexdump_File (&send);
3112 WriteIRString ((byte *)&send,send.len,usb_mode);
3113 res = ReadIRString (st,1,500,usb_mode);
3114 count++;
3115 if (res != 1 || *st != 'O') {
3116 if (res && *st == 'R') return (ERR_RESEND);
3117 if (usb_mode) FlushUSB ();
3118 else FlushCom ();
3119 msSleep (200);
3120 if (count > 2) msSleep (1000);
3121 res = 0;
3123 } while (res == 0 && count < 5);
3125 if (count == 5) return (ERR_TIMEOUT);
3127 return (0);
3131 int WriteTransceiverL (IRDATA *src,byte usb_mode)
3133 byte res = 0;
3134 char st[100];
3135 static byte seq;
3136 int count = 0;
3137 IRDATA send;
3139 swap_irdata (src,&send);
3141 send.checksumme = get_checksumme (&send);
3143 ((byte *)&send)[send.len] = seq++;
3145 do {
3146 if (usb_mode == 1) FlushUSB ();
3147 else if (!usb_mode) FlushCom ();
3148 if (mode_flag & HEXDUMP) Hexdump_IO (&send);
3149 if (hexflag) Hexdump_File (&send);
3150 if (usb_mode) WriteIRString ((byte *)&send,send.len+1,usb_mode);
3151 else WriteIRString ((byte *)&send,send.len,usb_mode);
3152 res = ReadIRString (st,1,500,usb_mode);
3153 count++;
3154 if (res != 1 || *st != 'O') {
3155 if (res && *st == 'R') return (ERR_RESEND);
3156 if (usb_mode) FlushUSB ();
3157 else FlushCom ();
3158 msSleep (200);
3159 if (count > 2) msSleep (1000);
3160 res = 0;
3162 } while (res == 0 && count < 5);
3164 if (count == 5) return (ERR_TIMEOUT);
3166 return (0);
3170 void WriteIRString (byte pnt[],int len,byte usb_mode)
3172 #ifdef WIN32
3173 if (usb_mode) WriteUSBString (pnt,len);
3174 else WriteSerialString (pnt,len);
3175 #endif
3177 #ifdef LINUX
3178 WriteSerialString (pnt,len);
3179 #endif
3182 int ReadIRString (byte pnt[],int len,word timeout,byte usb_mode)
3184 #ifdef WIN32
3185 if (usb_mode) return (ReadUSBString (pnt,len,timeout));
3186 else return (ReadSerialString (pnt,len,timeout));
3187 #endif
3189 #ifdef LINUX
3190 return (ReadSerialString (pnt,len,timeout));
3191 #endif
3195 #ifdef LINUX
3196 void FlushUSB (void)
3198 FlushCom ();
3200 #endif
3202 void FlushIoEx (DEVICEINFO *dev)
3204 #ifdef WIN32
3205 if (dev->io.if_type == IF_USB) FlushUSBEx (dev->io.usbport);
3206 else FlushComEx (dev->io.comport);
3207 #endif
3209 #ifdef LINUX
3210 FlushComEx (dev->io.comport);
3211 #endif
3215 void SwapWordN (word *pnt)
3217 byte *a,v;
3219 if (byteorder) return;
3221 a = (byte *)pnt;
3222 v = a[0];
3223 a[0] = a[1];
3224 a[1] = v;
3228 void SwapIntN (int32_t *pnt)
3230 byte *a,v;
3232 if (byteorder) return;
3234 a = (byte *)pnt;
3235 v = a[0];
3236 a[0] = a[3];
3237 a[3] = v;
3239 v = a[1];
3240 a[1] = a[2];
3241 a[2] = v;