1 /*
2  * Copyright (c) 2001 Tommy Bohlin <tommy@gatespace.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 /* ircomm.c
27  */
28 
29 #include <irda.h>
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 
35 static int verbosity;
36 static int nlcr;
37 
38 /**********************************************************************
39  * COMM control
40  **********************************************************************/
41 
input(void * handle)42 static void input(void* handle)
43 {
44   COMMClient* comm=(COMMClient*)handle;
45   u_char buf[1024];
46   int i,len=read(fileno(stdin),buf,sizeof buf);
47 
48   if(len>0) {
49     if(nlcr) for(i=0;i<len;i++) if(buf[i]=='\n') buf[i]='\r';
50     commCltWrite(comm,buf,len);
51   }
52 }
53 
commStatus(COMMClient * cc,int event)54 static void commStatus(COMMClient* cc, int event)
55 {
56   switch(event) {
57   case COMM_CONNECTED:
58     if(verbosity>0) birda_log("comm connected\n");
59 
60     commCltSetParams(cc,57600,COMM_8N1);
61     commCltSetDTE(cc,COMM_DTR|COMM_RTS);
62 
63     evtAddSource(fileno(stdin),input,cc);
64 
65     break;
66   case COMM_DISCONNECTED:
67     if(verbosity>0) birda_log("comm disconnected\n");
68     break;
69   }
70 }
71 
commData(COMMClient * c,void * buf,int len)72 static void commData(COMMClient* c, void* buf, int len)
73 {
74   fwrite(buf,1,len,stdout);
75 }
76 
77 /**********************************************************************
78  * LAP control
79  **********************************************************************/
80 
lapConnected(LAP * lap)81 static void lapConnected(LAP* lap)
82 {
83   COMMClient* comm;
84 
85   if(verbosity>0) birda_log("LAP connected\n");
86   comm=createCOMMClient(lap,COMM_ST_3WIRE|COMM_ST_9WIRE,COMM_PT_SERIAL,FALSE);
87   if(!comm) {
88     birda_log("COMM connect failed\n");
89     lapDisconnect(lap);
90   }
91   comm->status=commStatus;
92   comm->data=commData;
93 }
94 
lapDisconnected(LAP * lap)95 static void lapDisconnected(LAP* lap)
96 {
97   exit(0);
98 }
99 
100 /**********************************************************************
101  * Main function
102  **********************************************************************/
103 
104 static const char* description="Connect stdin or pty to IrDA COMM port";
105 
106 static Option options[] = {
107   { OPTION_BOOL, 'r', 0, "map \\n to \\r in input" },
108 };
109 
main(int argc,char ** argv)110 int main(int argc, char** argv)
111 {
112   doOptions(argc,argv,1,options,&verbosity,description);
113 
114   optLap->flags|=HINT_IRCOMM;
115   optLapConnected=lapConnected;
116   optLapDisconnected=lapDisconnected;
117 
118   if(options[0].val.b) nlcr=1;
119   doConnect();
120   evtLoop();
121 
122   return 0;
123 }
124