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 /* raw.c
27  */
28 
29 #include <irda.h>
30 
31 /**********************************************************************
32  * Constants
33  **********************************************************************/
34 
35 static const char id_raw[]="raw";
36 
37 /**********************************************************************
38  * Data structures
39  **********************************************************************/
40 
41 typedef struct Raw {
42   SerialDevice sd;
43   SerialPort* sp;
44 } Raw;
45 
46 /**********************************************************************
47  * Methods
48  **********************************************************************/
49 
rawSetSpeed(SerialDevice * sd,int speed)50 static int rawSetSpeed(SerialDevice* sd, int speed)
51 {
52   Raw* raw=(Raw*)sd;
53   SerialPort* sp=raw->sp;
54 
55   sp->setSpeed(sp,speed);
56 
57   return 0;
58 }
59 
rawGetSpeedMask(SerialDevice * sd)60 static int rawGetSpeedMask(SerialDevice* sd)
61 {
62   Raw* raw=(Raw*)sd;
63 
64   return raw->sp->getSpeedMask(raw->sp)&
65     (SPEED_2400|SPEED_9600|SPEED_19200|SPEED_38400|SPEED_57600|SPEED_115200);
66 }
67 
rawGetMinTurnaroundMask(SerialDevice * sd)68 static int rawGetMinTurnaroundMask(SerialDevice* sd)
69 {
70   /* NB: Can we decrease this value? */
71 
72   return MIN_TA_10ms;
73 }
74 
rawGetChar(SerialDevice * sd)75 static int rawGetChar(SerialDevice* sd)
76 {
77   Raw* raw=(Raw*)sd;
78 
79   return raw->sp->getChar(raw->sp);
80 }
81 
rawPutChar(SerialDevice * sd,int c)82 static void rawPutChar(SerialDevice* sd, int c)
83 {
84   Raw* raw=(Raw*)sd;
85   raw->sp->putChar(raw->sp,c);
86 }
87 
rawClose(SerialDevice * sd)88 static void rawClose(SerialDevice* sd)
89 {
90   Raw* raw=(Raw*)sd;
91 
92   raw->sp->handle=0;
93   raw->sp->status=0;
94   freeMem(raw);
95 }
96 
rawStatus(SerialPort * sp,int event)97 static void rawStatus(SerialPort* sp, int event)
98 {
99   Raw* raw=(Raw*)sp->handle;
100 
101   if(raw->sd.status) raw->sd.status(&raw->sd,event);
102 }
103 
104 /**********************************************************************
105  * External interface
106  **********************************************************************/
107 
createRawDevice(SerialPort * sp)108 SerialDevice* createRawDevice(SerialPort* sp)
109 {
110   Raw* raw=allocMem(id_raw,sizeof(Raw));
111 
112   raw->sd.close=rawClose;
113   raw->sd.setSpeed=rawSetSpeed;
114   raw->sd.getSpeedMask=rawGetSpeedMask;
115   raw->sd.getMinTurnaroundMask=rawGetMinTurnaroundMask;
116   raw->sd.getChar=rawGetChar;
117   raw->sd.putChar=rawPutChar;
118   raw->sd.handle=0;
119   raw->sd.status=0;
120   raw->sp=sp;
121 
122   sp->handle=raw;
123   sp->status=rawStatus;
124 
125   return &raw->sd;
126 }
127