Mostly minor fixes up until version 0.8.10.
[irreco.git] / irtrans / irserver / src / client.c
blob5b57b404fde73653bcf40cb621e30eb1cdd0f5a0
1 #ifdef WIN32
3 #include <winsock2.h>
4 #include <windows.h>
6 #else
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <sys/un.h>
12 #include <arpa/inet.h>
13 #include <sys/stat.h>
14 #include <errno.h>
15 #include <netdb.h>
16 #include <signal.h>
17 typedef int SOCKET;
18 typedef void* WSAEVENT;
19 #define closesocket close
20 #endif
22 #include <stdio.h>
23 #include "remote.h"
24 #include "network.h"
25 #include "errcode.h"
28 int InitClientSocket (char host[],SOCKET *sock,unsigned long id);
29 void PrintStatError (NETWORKSTATUS *stat);
31 void ReceiveIR (void);
32 char *trim (char st[],int i);
33 int MainMenu (void);
34 void BuildMainMenu ();
35 void BuildSendMenu ();
36 void BuildLearnMenu ();
37 int SendRemoteCommand (char rem[],char com[],char mask[],char bus[],char led[]);
38 int SendCommand (NETWORKCOMMAND *com,NETWORKSTATUS *stat);
39 int NetworkCommand (int netcommand,char remote[],char command[],word timeout,int adr,NETWORKSTATUS *stat);
40 int ShutdownServer (void);
41 int ReloadServer (void);
42 int SetSwitch (char sw[],char value[]);
43 int SendSerial (char data[]);
44 int SetLED (char mode[],char value[]);
45 int SendUDP (char ip[],char cmdstr[]);
46 int RcvUDP (void);
48 #define TIMEOUT 30000
50 SOCKET serv;
52 char send_remote[100];
53 char learn_remote[100];
55 main (int argc,char *argv[])
57 int res;
58 // unsigned char st[256];
59 // NETWORKSTATUS stat;
61 if (argc != 2 && argc != 3 && argc != 4 && argc != 5 && argc != 6 && argc != 7) {
62 fprintf(stderr, "usage : %s <ip>\n", argv[0]);
63 fprintf(stderr, "send : %s <ip> <remote> <command>\n",argv[0]);
64 fprintf(stderr, "send : %s <ip> <remote> <command> <sendmask> <bus>\n",argv[0]);
65 fprintf(stderr, "send : %s <ip> <remote> <command> <sendmask> <bus> <led>\n",argv[0]);
66 fprintf(stderr, "shutdown : %s <ip> -shutdown\n", argv[0]);
67 fprintf(stderr, "reload IRDB: %s <ip> -reload\n", argv[0]);
68 fprintf(stderr, "receive : %s <ip> -receive\n", argv[0]);
69 fprintf(stderr, "Send RS232 : %s <ip> -rs232send <data>\n", argv[0]);
70 fprintf(stderr, "switch : %s <ip> -switch <switch> <value>\n", argv[0]);
71 fprintf(stderr, "LED : %s <ip> -led <mode> <value>\n", argv[0]);
72 exit (-1);
75 if (argc == 4 && !strcmp (argv[2],"-udp")) return (SendUDP (argv[1],argv[3]));
77 if (argc == 2 && !strcmp (argv[1],"-receiveudp")) return (RcvUDP());
79 res = InitClientSocket (argv[1],&serv,0);
80 if (res) {
81 fprintf (stderr,"Error connecting to host %s\n",argv[1]);
82 exit (res);
85 if (argc == 3 && !strcmp (argv[2],"-shutdown")) return (ShutdownServer ());
86 if (argc == 3 && !strcmp (argv[2],"-reload")) return (ReloadServer ());
87 if (argc == 3 && !strcmp (argv[2],"-receive")) ReceiveIR ();
88 if (argc == 4 && !strcmp (argv[2],"-rs232send")) return (SendSerial (argv[3]));
89 if (argc == 5 && !strcmp (argv[2],"-switch")) return (SetSwitch (argv[3],argv[4]));
90 if (argc == 5 && !strcmp (argv[2],"-led")) return (SetLED (argv[3],argv[4]));
91 if (argc == 4) return (SendRemoteCommand (argv[2],argv[3],NULL,NULL,NULL));
92 if (argc == 5) return (SendRemoteCommand (argv[2],argv[3],argv[4],NULL,NULL));
93 if (argc == 6) return (SendRemoteCommand (argv[2],argv[3],argv[4],argv[5],NULL));
94 if (argc == 7) return (SendRemoteCommand (argv[2],argv[3],argv[4],argv[5],argv[6]));
98 // NetworkCommand (COMMAND_MCE_CHARS,"",0,0,'L',&stat);
100 strcpy (st," -123456");
101 // strcpy (st,"-1234567890abcdefghijklmnop");
102 *st = 1;
103 st[1] = 2;
104 st[2] = 3;
105 st[3] = 4;
106 st[4] = 5;
107 st[5] = 0x94;
108 st[6] = 1;
109 st[7] = 1;
110 st[8] = 6;
111 st[9] = 6;
112 st[10] = 0x9f;
113 st[11] = 0x9e;
114 // st[12] = 0;
115 sprintf (st,"%-40s%-40s%-40s%-40s","12345678901234567","XP Mediacenter."," "," ");
116 // strcpy (st,"123456789012 ");
117 NetworkCommand (COMMAND_LCD,st,0,LCD_TEXT | LCD_BACKLIGHT,'L',&stat);
118 return;
120 NetworkCommand (COMMAND_STARTCLOCK,"",0,0,'L',&stat);
121 res = 0;
122 while (1) {
123 sprintf (st,"Test Nr %d",res++);
124 NetworkCommand (COMMAND_LCD,st,0,LCD_TEXT | LCD_BACKLIGHT,'L',&stat);
125 Sleep (500);
129 while (MainMenu ());
131 return (0);
135 int RcvUDP ()
137 char mem[100];
138 int res;
139 int irtlan_socket;
140 struct sockaddr_in serv_addr;
142 #ifdef WIN32
143 int err;
144 WORD wVersionRequired;
145 WSADATA wsaData;
146 wVersionRequired = MAKEWORD(2,2);
147 err = WSAStartup(wVersionRequired, &wsaData);
148 if (err != 0) exit(1);
149 #endif
153 irtlan_socket = socket (PF_INET,SOCK_DGRAM,0);
154 if (irtlan_socket < 0) return (ERR_OPENSOCKET);
156 memset (&serv_addr,0,sizeof (serv_addr));
157 serv_addr.sin_family = AF_INET;
159 serv_addr.sin_addr.s_addr = htonl (INADDR_ANY);
160 serv_addr.sin_port = htons (437);
162 if (bind (irtlan_socket,(struct sockaddr *)&serv_addr,sizeof (serv_addr)) < 0) {
163 printf ("Error Bind\n");
164 return (ERR_BINDSOCKET);
167 while (1) {
168 memset (mem,0,100);
169 res = recv (irtlan_socket,mem,100,0);
171 printf ("RECV: %s\n",mem);
175 return (0);
179 int SendUDP (char ip[],char cmdstr[])
181 char stat[50];
182 int res,rcv;
183 int irtlan_outbound,irtlan_in;
184 struct sockaddr_in iadr;
185 struct sockaddr_in serv_addr;
187 #ifdef WIN32
188 int err;
189 WORD wVersionRequired;
190 WSADATA wsaData;
191 wVersionRequired = MAKEWORD(2,2);
192 err = WSAStartup(wVersionRequired, &wsaData);
193 if (err != 0) exit(1);
194 #endif
196 irtlan_outbound = socket (PF_INET,SOCK_DGRAM,0);
197 if (irtlan_outbound < 0) return (ERR_OPENSOCKET);
199 irtlan_in = socket (PF_INET,SOCK_DGRAM,0);
200 memset (&serv_addr,0,sizeof (serv_addr));
201 serv_addr.sin_family = AF_INET;
203 serv_addr.sin_addr.s_addr = htonl (INADDR_ANY);
204 serv_addr.sin_port = htons (IRTRANS_PORT);
205 // serv_addr.sin_port = htons (50000);
207 rcv = bind (irtlan_in,(struct sockaddr *)&serv_addr,sizeof (serv_addr));
209 memset (&iadr,0,sizeof (struct sockaddr));
210 iadr.sin_family = AF_INET;
211 iadr.sin_addr.s_addr = inet_addr(ip);
212 iadr.sin_port = htons ((word)IRTRANS_PORT);
213 // iadr.sin_port = htons ((word)8010);
215 if (connect (irtlan_outbound,(struct sockaddr *)&iadr,sizeof (struct sockaddr_in)) < 0) {
216 printf ("Error Connect\n");
217 return (ERR_BINDSOCKET);
220 res = send (irtlan_outbound,cmdstr,strlen (cmdstr),0);
222 if (res != (int)strlen (cmdstr)) {
223 printf ("Error Sending\n");
224 return (ERR_BINDSOCKET);
228 if (rcv < 0) printf ("ACK Rcv Socket busy (same machine ?)\n");
229 else {
230 memset (stat,0,50);
231 recv (irtlan_in,stat,50,0);
232 printf ("RCV STAT: %s\n",stat);
235 return (0);
238 int SendSerial (char data[])
240 NETWORKSTATUS stat;
242 NetworkCommand (COMMAND_RS232_SEND,data,0,0,'L',&stat);
244 return (0);
248 int SetSwitch (char sw[],char value[])
250 char st[10];
251 NETWORKSTATUS stat;
253 st[0] = atoi (sw);
254 st[1] = atoi (value);
255 NetworkCommand (COMMAND_SETSWITCH,st,st+1,0,'L',&stat);
257 return (0);
260 int SetLED (char mode[],char value[])
262 char st[10];
263 NETWORKSTATUS stat;
265 st[0] = atoi (mode);
266 st[1] = atoi (value);
267 NetworkCommand (COMMAND_LED,st,st+1,0,'L',&stat);
269 return (0);
272 void ReceiveIR ()
274 int res;
275 NETWORKSTATUS stat;
276 NETWORKRECV *rcv;
278 printf ("Waiting for IR commands ...\n");
279 memset (&stat,0,sizeof (NETWORKSTATUS));
280 rcv = (NETWORKRECV *)&stat;
281 for (;;) {
282 res = recv (serv,(char *)&stat,8,0);
283 if (stat.statuslen > 8) {
284 res = recv (serv,((char *)&stat) + 8,stat.statuslen-8,0);
285 if (stat.statustype == STATUS_RECEIVE) {
286 printf ("RCV: %s.%s - %d [%d: %s]\n",trim (rcv->remote,80),trim (rcv->command,20),rcv->adress,rcv->clientid,trim (rcv->data,200));
293 char *trim (char st[],int i)
295 i--;
296 while (i && st[i] == ' ') i--;
297 st[i+1] = 0;
298 return (st);
302 int SendRemoteCommand (char rem[],char com[],char mask[],char bus[],char led[])
304 int adr = 0,res,wait = 0,ibus = 0,iled = 0;
305 NETWORKSTATUS stat;
306 #ifdef WIN32
308 if (!strcmp (rem,"wait") || !strcmp (rem,"Wait") || !strcmp (rem,"WAIT")) {
309 adr = atoi (com);
310 Sleep (adr);
311 return (0);
314 #endif
315 if (mask) {
316 if (!strcmp (mask,"wait") || !strcmp (mask,"Wait") || !strcmp (mask,"WAIT")) wait = atoi (mask+4);
317 else if (!strcmp (mask,"int") || !strcmp (mask,"Int") || !strcmp (mask,"INT")) adr |= 1 << 17;
318 else if (!strcmp (mask,"ext") || !strcmp (mask,"Ext") || !strcmp (mask,"EXT")) adr |= 2 << 17;
319 else if (!strcmp (mask,"all") || !strcmp (mask,"All") || !strcmp (mask,"ALL")) adr |= 3 << 17;
320 else {
321 if (mask[0] == '0' && mask[1] == 'x') sscanf (mask,"%x",&adr);
322 else sscanf (mask,"%d",&adr);
323 if (adr) adr = (adr & 0xffff) | 0x10000;
326 if (bus) {
327 ibus = atoi (bus);
328 if (ibus == 255) adr |= 0x40000000;
329 else {
330 ibus = abs (ibus) & (MAX_IR_DEVICES - 1);
331 adr |= ibus << 20;
334 if (led) {
335 iled = atoi (led) & 3;
336 adr |= iled << 17;
338 res = NetworkCommand (COMMAND_SEND,rem,com,0,adr,&stat);
339 #ifdef WIN32
340 Sleep (wait);
341 #endif
342 return (res);
346 int ShutdownServer (void)
348 NETWORKCOMMAND com;
349 NETWORKSTATUS stat;
351 com.netcommand = COMMAND_SHUTDOWN;
352 strcpy (com.remote,"XXXshutdownXXX");
354 SendCommand (&com,&stat);
356 #ifdef WIN32
357 Sleep (8000);
358 #endif
360 return (0);
363 int ReloadServer (void)
365 NETWORKCOMMAND com;
366 NETWORKSTATUS stat;
368 com.netcommand = COMMAND_RELOAD;
370 SendCommand (&com,&stat);
372 return (0);
375 int SendMenu (void)
377 char st[255],l2[255];
378 int choice = 0;
379 int sw,md;
380 NETWORKSTATUS stat;
382 BuildSendMenu ();
383 scanf ("%s",st);
384 getchar ();
385 choice = atoi (st);
387 switch (choice) {
388 case 1:
389 printf ("\nEnter Remote Name: ");
390 fflush (stdout);
391 scanf ("%s",send_remote);
392 getchar ();
393 break;
394 case 2:
395 printf ("\nCommand Name: ");
396 fflush (stdout);
397 scanf ("%s",st);
398 getchar ();
399 NetworkCommand (COMMAND_SEND,send_remote,st,0,0,&stat);
400 break;
401 case 3:
402 printf ("\nLCD Text: ");
403 fflush (stdout);
404 fgets (st,sizeof (st),stdin);
405 NetworkCommand (COMMAND_LCD,st,0,LCD_TEXT | LCD_BACKLIGHT,'L',&stat);
406 break;
407 case 4:
408 printf ("\nLCD Init Text Line 1: ");
409 fflush (stdout);
410 fgets (st,sizeof (st),stdin);
411 printf ("\nLCD Init Text Line 2: ");
412 fflush (stdout);
413 fgets (l2,sizeof (l2),stdin);
414 strcat (st,l2);
415 NetworkCommand (COMMAND_LCDINIT,st,0,LCD_TEXT,'L',&stat);
416 break;
417 case 5:
418 printf ("\nSwitch Number: ");
419 fflush (stdout);
420 scanf ("%d",&sw);
421 getchar ();
422 printf ("\nSwitch Value: ");
423 fflush (stdout);
424 scanf ("%d",&md);
425 getchar ();
426 st[0] = sw;
427 st[1] = md;
428 NetworkCommand (COMMAND_SETSWITCH,st,st+1,0,'L',&stat);
429 break;
430 case 99:
431 return (0);
432 break;
435 return (1);
438 int LearnMenu (void)
440 char st[255];
441 int choice = 0;
442 NETWORKSTATUS stat;
444 BuildLearnMenu ();
445 scanf ("%s",st);
446 getchar ();
447 choice = atoi (st);
449 switch (choice) {
450 case 1:
451 if (*learn_remote) NetworkCommand (COMMAND_CLOSE,learn_remote,st,0,'L',&stat);
452 printf ("\nEnter Remote Name: ");
453 fflush (stdout);
454 scanf ("%s",learn_remote);
455 getchar ();
456 NetworkCommand (COMMAND_LRNREM,learn_remote,st,0,'L',&stat);
457 break;
458 case 2:
459 printf ("\nPress Remote Button to record timing ....");
460 fflush (stdout);
461 NetworkCommand (COMMAND_LRNTIM,learn_remote,st,TIMEOUT,'*',&stat);
462 printf ("OK\n");
463 fflush (stdout);
464 break;
465 case 3:
466 printf ("\nCommand Name: ");
467 fflush (stdout);
468 scanf ("%s",st);
469 getchar ();
470 printf ("\nPress Remote Button to learn ....");
471 fflush (stdout);
472 NetworkCommand (COMMAND_LRNCOM,learn_remote,st,TIMEOUT,'*',&stat);
473 printf ("OK\n");
474 fflush (stdout);
475 break;
476 case 4:
477 printf ("\nCommand Name: ");
478 fflush (stdout);
479 scanf ("%s",st);
480 getchar ();
481 printf ("\nPress Remote Button to learn ....");
482 fflush (stdout);
483 NetworkCommand (COMMAND_LRNLONG,learn_remote,st,TIMEOUT,'*',&stat);
484 printf ("OK\n");
485 fflush (stdout);
486 break;
487 case 5:
488 printf ("\nCommand Name: ");
489 fflush (stdout);
490 scanf ("%s",st);
491 getchar ();
492 printf ("\nPress Remote Button to learn ....");
493 fflush (stdout);
494 NetworkCommand (COMMAND_LRNRAW,learn_remote,st,TIMEOUT,'*',&stat);
495 printf ("OK\n");
496 fflush (stdout);
497 break;
498 case 99:
499 if (*learn_remote) NetworkCommand (COMMAND_CLOSE,learn_remote,st,0,'L',&stat);
500 return (0);
501 break;
504 return (1);
508 void ShowSetStatus (int adr,int bus,NETWORKMODEEXN *status)
510 int i;
511 status->stat[bus][adr].version[8] = 0;
513 printf ("Device [%d]: %s\n\n",adr,status->stat[bus][adr].version);
515 printf ("Repeat Mask: ");
516 for (i=0;i<16;i++) {
517 printf ("%c ",(status->stat[bus][adr].send_mask & (1 << i) ? 'x':'o'));
519 printf ("\n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15\n\n");
522 printf (" Device Mode Send [20]: %c\n",(status->stat[bus][adr].device_mode & DEVMODE_SEND ? 'x':'o'));
523 printf (" IR [21]: %c\n",(status->stat[bus][adr].device_mode & DEVMODE_IR ? 'x':'o'));
524 printf (" SBUS [22]: %c\n",(status->stat[bus][adr].device_mode & DEVMODE_SBUS ? 'x':'o'));
525 // printf (" IR RemoteCtrl [23]: %c\n",(status->stat[bus][adr].device_mode & DEVMODE_IRCODE ? 'x':'o'));
526 printf (" SBUS RemoteCtrl [24]: %c\n",(status->stat[bus][adr].device_mode & DEVMODE_SBUSCODE ? 'x':'o'));
527 printf (" RAW [25]: %c\n",(status->stat[bus][adr].device_mode & DEVMODE_RAW ? 'x':'o'));
528 if (strcmp (status->stat[bus][adr].version+1,"4.05.01") < 0) printf (" Fast Mode [26]: %c\n",(status->stat[bus][adr].device_mode & DEVMODE_RAWFAST ? 'x':'o'));
529 printf (" SBUS send Repeat [27]: %c\n",(status->stat[bus][adr].device_mode & DEVMODE_REPEAT ? 'x':'o'));
531 if (status->stat[bus][adr].features & FN_DUALSND) {
532 printf ("\n Internal LEDs [30]: %c\n",(status->stat[bus][adr].extended_mode & INTERNAL_LEDS ? 'x':'o'));
533 printf (" External LEDs [31]: %c\n",(status->stat[bus][adr].extended_mode & EXTERNAL_LEDS ? 'x':'o'));
535 if (status->stat[bus][adr].features & FN_DUALRCV) {
536 printf ("\n Standard Receivr.[33]: %c\n",(status->stat[bus][adr].extended_mode & STANDARD_RCV ? 'x':'o'));
537 printf (" 455 kHz Receiver [34]: %c\n",(status->stat[bus][adr].extended_mode & BO_RCV ? 'x':'o'));
540 if (status->stat[bus][adr].version[0] == 'V' || status->stat[bus][adr].version[0] == 'T' || status->stat[bus][adr].version[0] == 'C' || status->stat[bus][adr].version[0] == 'X' ) { // 455kHz
541 printf ("\n Receive B&O [35]: %c\n",(status->stat[bus][adr].extended_mode & BO_MODE ? 'x':'o'));
544 if (strcmp (status->stat[bus][adr].version+1,"3.02.10") >= 0) {
545 printf ("\n Self Repeat [36]: %c\n",(status->stat[bus][adr].extended_mode & SELF_REPEAT ? 'x':'o'));
546 if (status->stat[bus][adr].features & FN_DUALSND) {
547 printf (" via Internal LEDs [32]: %c\n",(status->stat[bus][adr].extended_mode & INTERNAL_REPEAT ? 'x':'o'));
550 if (strcmp (status->stat[bus][adr].version+1,"4.04.01") >= 0) {
551 printf ("\n Learn Timeout 5ms [37]: %c\n",(((status->stat[bus][adr].extended_mode2 & LRN_TIMEOUT_MASK) == 0) ? 'x':'o'));
552 printf (" Learn Timeout 15ms [38]: %c\n",(((status->stat[bus][adr].extended_mode2 & LRN_TIMEOUT_MASK) == 1) ? 'x':'o'));
553 printf (" Learn Timeout 30ms [39]: %c\n",(((status->stat[bus][adr].extended_mode2 & LRN_TIMEOUT_MASK) == 2) ? 'x':'o'));
554 printf (" Learn Timeout 60ms [40]: %c\n",(((status->stat[bus][adr].extended_mode2 & LRN_TIMEOUT_MASK) == 3) ? 'x':'o'));
555 printf (" Learn Timeout 90ms [41]: %c\n",(((status->stat[bus][adr].extended_mode2 & LRN_TIMEOUT_MASK) == 4) ? 'x':'o'));
556 printf (" Learn Timeout 120ms [42]: %c\n",(((status->stat[bus][adr].extended_mode2 & LRN_TIMEOUT_MASK) == 5) ? 'x':'o'));
557 printf (" Learn Timeout 150ms [43]: %c\n",(((status->stat[bus][adr].extended_mode2 & LRN_TIMEOUT_MASK) == 6) ? 'x':'o'));
558 if (strcmp (status->stat[bus][adr].version+1,"4.04.35") >= 0)
559 printf (" Learn Timeout 250ms [44]: %c\n",(((status->stat[bus][adr].extended_mode2 & LRN_TIMEOUT_MASK) == 7) ? 'x':'o'));
562 if (strcmp (status->stat[bus][adr].version+1,"4.05.01") >= 0) {
563 printf ("\n Repeat Timeout 5ms [45]: %c\n",(status->stat[bus][adr].device_mode & DEVMODE_RAWFAST ? 'o':'x'));
564 printf (" Repeat Timeout 25ms [46]: %c\n",(status->stat[bus][adr].device_mode & DEVMODE_RAWFAST ? 'x':'o'));
567 if (status->stat[bus][adr].features & FN_SBUS_UART) {
568 printf ("\n SBUS Classic [47]: %c\n",(status->stat[bus][adr].extended_mode & SBUS_UART ? 'o':'x'));
569 printf (" SBUS 4800 Baud [48]: %c\n",((status->stat[bus][adr].extended_mode & SBUS_UART && (status->stat[bus][adr].extended_mode2 & SBUS_BAUD_MASK) == 0) ? 'x':'o'));
570 printf (" SBUS 9600 Baud [49]: %c\n",((status->stat[bus][adr].extended_mode & SBUS_UART && (status->stat[bus][adr].extended_mode2 & SBUS_BAUD_MASK) == 8) ? 'x':'o'));
571 printf (" SBUS 19200 Baud [50]: %c\n",((status->stat[bus][adr].extended_mode & SBUS_UART && (status->stat[bus][adr].extended_mode2 & SBUS_BAUD_MASK) == 16) ? 'x':'o'));
572 printf (" SBUS 38400 Baud [51]: %c\n",((status->stat[bus][adr].extended_mode & SBUS_UART && (status->stat[bus][adr].extended_mode2 & SBUS_BAUD_MASK) == 24) ? 'x':'o'));
575 if (!(status->stat[bus][adr].features & FN_SOFTID) || (status->stat[bus][adr].features & FN_POWERON) || (status->stat[bus][adr].features & FN_USBWAKEUP)) {
576 printf ("\nPowerOn Remote [52]: %s\n",status->stat[bus][adr].remote);
577 printf ("PowerOn Command [53]: %s\n",status->stat[bus][adr].command);
580 if (status->stat[bus][adr].features & FN_DUALPOWERON) {
581 printf ("\nPowerOff Remote [54]: %s\n",status->stat[bus][adr].remote2);
582 printf ("PowerOff Command [55]: %s\n",status->stat[bus][adr].command2);
585 printf ("\nCancel [88]\n");
586 printf ("Save & Exit [99]\n");
588 printf ("\nSelect command ");
589 fflush (stdout);
592 void SetDeviceStatus (int adr,NETWORKMODEEXN *status)
594 int res;
595 char st[255];
596 int choice = 0;
597 int bus = 0;
598 NETWORKCOMMAND command;
599 NETWORKSTATUS stat;
601 while (choice < 56) {
602 ShowSetStatus (adr,bus,status);
604 scanf ("%s",st);
605 getchar ();
606 choice = atoi (st);
608 if (choice < 20) status->stat[bus][adr].send_mask ^= 1 << choice;
610 else if (choice < 30) status->stat[bus][adr].device_mode ^= 1 << (choice - 20);
611 else if (choice <= 44) {
612 if (choice >= 37 && choice <= 44) status->stat[bus][adr].extended_mode2 = choice - 37;
613 else {
614 if (choice == 32) choice = 36;
615 else if (choice > 32) choice--;
616 status->stat[bus][adr].extended_mode ^= 1 << (choice - 30);
619 if (choice == 45) status->stat[bus][adr].device_mode &= ~DEVMODE_RAWFAST;
620 if (choice == 46) status->stat[bus][adr].device_mode |= DEVMODE_RAWFAST;
621 if (choice >= 47 && choice <= 51) {
622 if (choice == 47) status->stat[bus][adr].extended_mode &= ~SBUS_UART;
623 else {
624 status->stat[bus][adr].extended_mode2 &= ~SBUS_BAUD_MASK;
625 status->stat[bus][adr].extended_mode2 |= (choice - 48) * 8;
626 status->stat[bus][adr].extended_mode |= SBUS_UART;
630 if (choice == 52) {
631 printf ("\nEnter PowerOn Remote: ");
632 fflush (stdout);
633 scanf ("%s",status->stat[bus][adr].remote);
636 if (choice == 53) {
637 printf ("\nEnter PowerOn Command: ");
638 fflush (stdout);
639 scanf ("%s",status->stat[bus][adr].command);
642 if (choice == 54) {
643 printf ("\nEnter PowerOff Remote: ");
644 fflush (stdout);
645 scanf ("%s",status->stat[bus][adr].remote2);
648 if (choice == 55) {
649 printf ("\nEnter PowerOff Command: ");
650 fflush (stdout);
651 scanf ("%s",status->stat[bus][adr].command2);
654 if (choice == 99) {
655 command.netcommand = COMMAND_SETSTAT;
656 strcpy (command.command,status->stat[bus][adr].command);
657 strcpy (command.remote,status->stat[bus][adr].remote);
658 command.timeout = adr;
659 command.mode = status->stat[bus][adr].device_mode;
660 command.adress = status->stat[bus][adr].send_mask | (status->stat[bus][adr].extended_mode << 16);
661 command.trasmit_freq = status->stat[bus][adr].extended_mode2;
662 memset (&stat,0,sizeof (stat));
663 res = SendCommand (&command,&stat);
665 if (res) return;
667 if (stat.statustype == STATUS_MESSAGE) {
668 PrintStatError (&stat);
669 return;
671 if (status->stat[bus][adr].features & FN_DUALPOWERON) {
672 command.netcommand = COMMAND_SETSTAT2;
673 strcpy (command.command,status->stat[bus][adr].command2);
674 strcpy (command.remote,status->stat[bus][adr].remote2);
675 memset (&stat,0,sizeof (stat));
676 res = SendCommand (&command,&stat);
678 if (res) return;
680 if (stat.statustype == STATUS_MESSAGE) {
681 PrintStatError (&stat);
682 return;
692 int StatusMenu (void)
694 char st[255];
695 int choice = 0;
696 int res,i,cnt;
697 NETWORKMODEEXN status;
698 NETWORKCOMMAND com;
701 printf ("Getting Device Status ...");
702 fflush (stdout);
704 com.netcommand = COMMAND_STATUSEXN;
706 memset (&status,0,sizeof (status));
707 res = SendCommand (&com,(NETWORKSTATUS *)&status);
708 printf ("SEND RES: %d - %d\n",res,status.statustype);
709 if (res) return (0);
711 if (status.statustype != STATUS_DEVICEMODEEXN) {
712 PrintStatError ((NETWORKSTATUS *)&status);
713 return (0);
716 cnt = 0;
717 for (i=0;i < 16;i++) {
718 if (status.stat[0][i].version[0]) cnt++;
719 status.stat[0][i].version[8] = 0;
722 printf ("%c \n%d Device(s) Found:\n\n",13,cnt);
723 for (i=0;i < 16;i++) {
724 if (status.stat[0][i].version[0]) printf ("%2d: - %s\n",i,status.stat[0][i].version);
727 printf ("\n99 - Exit\n");
728 printf ("\nSelect command ");
729 fflush (stdout);
730 scanf ("%s",st);
731 getchar ();
732 choice = atoi (st);
734 if (choice == 99) return (0);
736 SetDeviceStatus (choice,&status);
738 return (0);
741 int MainMenu (void)
743 char st[255];
744 int choice = 0;
746 BuildMainMenu ();
747 scanf ("%s",st);
748 getchar ();
749 choice = atoi (st);
751 switch (choice) {
752 case 1:
753 while (SendMenu ());
754 break;
755 case 2:
756 while (LearnMenu ());
757 break;
758 case 3:
759 while (StatusMenu ());
760 break;
761 case 99:
762 return (0);
763 break;
766 return (1);
770 void BuildMainMenu ()
772 printf ("\n\n 1 - Send\n");
773 printf (" 2 - Learn\n");
774 printf (" 3 - Status\n");
775 printf ("\n 99 - Exit\n");
776 printf ("\nSelect command ");
777 fflush (stdout);
781 void BuildSendMenu ()
783 printf ("\n\n**Selected Remote: [%s]\n\n",send_remote);
784 printf (" 1 - Select remote\n");
785 printf (" 2 - Send command\n");
786 printf (" 3 - Send LCD\n");
787 printf (" 4 - Set LCD Init String\n");
788 printf (" 5 - Set Switch\n");
789 printf ("\n 99 - Exit\n");
790 printf ("\nSelect command ");
791 fflush (stdout);
794 void BuildLearnMenu ()
796 printf ("\n\n**Selected Remote: [%s]\n\n",learn_remote);
797 printf (" 1 - Select remote\n");
798 printf (" 2 - Learn Timing\n");
799 printf (" 3 - Learn Command [Based on timing]\n");
800 printf (" 4 - Learn Command [Command with timing]\n");
801 printf (" 5 - Learn Command [RAW Data]\n");
802 printf ("\n 99 - Exit\n");
803 printf ("\nSelect command ");
804 fflush (stdout);
807 void PrintStatError (NETWORKSTATUS *stat)
809 printf ("%s\n",stat->message);
813 int NetworkCommand (int netcommand,char remote[],char command[],word timeout,int adr,NETWORKSTATUS *stat)
815 int res;
816 LCDCOMMAND *lcd;
817 SERCOMMAND ser;
818 NETWORKCOMMAND *com;
820 memset (&ser,0,sizeof (ser));
822 com = (NETWORKCOMMAND *)&ser;
823 lcd = (LCDCOMMAND *)&ser;
824 com->adress = adr;
825 com->netcommand = netcommand;
826 if (netcommand == COMMAND_LCD || netcommand == COMMAND_LCDINIT) {
827 strcpy (lcd->framebuffer,remote);
828 lcd->hgt = 4;
829 lcd->wid = 40;
830 lcd->lcdcommand = (byte)timeout;
832 else if (netcommand == COMMAND_SETSWITCH) {
833 com->remote[0] = remote[0];
834 com->command[0] = command[0];
836 else if (netcommand == COMMAND_RS232_SEND) {
837 ser.len = strlen (remote);
838 strcpy (ser.data,remote);
840 else {
841 strcpy (com->remote,remote);
842 if (command) strcpy (com->command,command);
843 com->timeout = timeout;
845 res = SendCommand (com,stat);
847 if (stat->netstatus) {
848 PrintStatError (stat);
849 if (stat -> statuslevel == FATAL) exit (res);
851 return (stat->netstatus);
855 int SendCommand (NETWORKCOMMAND *com,NETWORKSTATUS *stat)
857 STATUSBUFFER buf;
858 int res,size;
859 fd_set events;
860 struct timeval tv;
862 com->protocol_version = 200;
863 size = sizeof (NETWORKCOMMAND);
864 if (com->netcommand == COMMAND_LCD) size = sizeof (LCDCOMMAND);
865 if (com->netcommand == COMMAND_RS232_SEND) size = sizeof (SERCOMMAND);
867 res = send (serv,(char *)com,size,0);
869 if (res != size) {
870 closesocket (serv);
871 return (ERR_SEND);
874 memset (stat,0,sizeof (NETWORKSTATUS));
876 tv.tv_sec = 3;
877 tv.tv_usec = 0;
878 FD_ZERO (&events);
879 FD_SET (serv,&events);
880 res = select (serv+1,&events,NULL,NULL,&tv);
882 if (res <= 0) return (ERR_SEND);
884 do {
885 res = recv (serv,(char *)&buf,32768,0);
887 if (res && (buf.statustype == STATUS_MESSAGE || buf.statustype == STATUS_DEVICEMODEEXN)) memcpy (stat,&buf,res);
888 if (res == 8) return (0);
890 } while (buf.statustype == STATUS_RECEIVE);
892 return (0);
898 int InitClientSocket (char host[],SOCKET *sock,unsigned long id)
900 struct sockaddr_in serv_addr;
901 unsigned long adr;
902 struct hostent *he;
903 struct in_addr addr;
905 #ifdef WIN32
907 int err;
908 WORD wVersionRequired;
909 WSADATA wsaData;
910 wVersionRequired = MAKEWORD(2,2);
911 err = WSAStartup(wVersionRequired, &wsaData);
912 if (err != 0) exit(1);
914 #endif
916 adr = inet_addr (host);
917 if (adr == INADDR_NONE) {
918 he = (struct hostent *)gethostbyname (host);
919 if (he == NULL) return (ERR_FINDHOST);
920 memcpy(&addr, he->h_addr_list[0], sizeof(struct in_addr));
921 adr = addr.s_addr;
926 *sock = socket (PF_INET,SOCK_STREAM,0);
927 if (*sock < 0) return (ERR_OPENSOCKET);
929 memset (&serv_addr,0,sizeof (serv_addr));
930 serv_addr.sin_family = AF_INET;
931 serv_addr.sin_addr.s_addr = adr;
932 serv_addr.sin_port = htons (TCP_PORT);
934 if (connect (*sock,(struct sockaddr *)&serv_addr,sizeof (serv_addr)) < 0) return (ERR_CONNECT);
936 send (*sock,(char *)&id,4,0);
938 return (0);