|
1 /* |
|
2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "CTcSerialConnection.h" |
|
19 #include "debuglog.h" |
|
20 #include "ErrorHandling.h" |
|
21 |
|
22 CTcSerialConnection::CTcSerialConnection( |
|
23 RCommServ& aCommServ, |
|
24 const TDesC& aPortName, |
|
25 TUint aPortSpeed ) : |
|
26 iCommServ( aCommServ ), |
|
27 iPortName( aPortName ), |
|
28 iPortSpeed( aPortSpeed ) |
|
29 { |
|
30 } |
|
31 |
|
32 CTcSerialConnection::~CTcSerialConnection() |
|
33 { |
|
34 Cancel(); |
|
35 iCommPort.Close(); |
|
36 } |
|
37 |
|
38 void CTcSerialConnection::DoCancel() |
|
39 { |
|
40 iCommPort.Cancel(); |
|
41 } |
|
42 |
|
43 void CTcSerialConnection::ConnectL() |
|
44 { |
|
45 LOG( _L("CTcSerialConnection::ConnectL() start") ); |
|
46 if( iState != EIdle ) |
|
47 { |
|
48 User::Leave( KErrNotReady ); |
|
49 } |
|
50 |
|
51 iState = EConnecting; |
|
52 |
|
53 // Open serial port |
|
54 SetupPortL(); |
|
55 |
|
56 // Serial port can be used immediately after port opening, do some nasty |
|
57 // RunL call to get in connected state |
|
58 iStatus = KErrNone; |
|
59 RunL(); |
|
60 |
|
61 LOG( _L("CTcSerialConnection::ConnectL() end") ); |
|
62 } |
|
63 |
|
64 void CTcSerialConnection::Close() |
|
65 { |
|
66 Cancel(); |
|
67 iCommPort.Close(); |
|
68 } |
|
69 |
|
70 void CTcSerialConnection::Send( const TDesC8& aDes ) |
|
71 { |
|
72 LOG( _L("CTcSerialConnection::Send() start") ); |
|
73 // Make sure we're in correct state |
|
74 __ASSERT_ALWAYS( iState == EConnected, Panic( KErrNotReady ) ); |
|
75 __ASSERT_ALWAYS( !IsActive(), Panic( KErrInUse ) ); |
|
76 |
|
77 // Start writing |
|
78 iState = ESending; |
|
79 iCommPort.Write( iStatus, aDes ); |
|
80 SetActive(); |
|
81 |
|
82 LOG( _L("CTcSerialConnection::Send() end") ); |
|
83 } |
|
84 |
|
85 void CTcSerialConnection::Receive( TDes8& aDes ) |
|
86 { |
|
87 LOG( _L("CTcSerialConnection::Receive() start") ); |
|
88 // Make sure we're in correct state |
|
89 __ASSERT_ALWAYS( iState == EConnected, Panic( KErrNotReady ) ); |
|
90 __ASSERT_ALWAYS( !IsActive(), Panic( KErrInUse ) ); |
|
91 |
|
92 // Start reading |
|
93 iState = EReceiving; |
|
94 iCommPort.Read( iStatus, aDes ); |
|
95 SetActive(); |
|
96 |
|
97 LOG( _L("CTcSerialConnection::Receive() end") ); |
|
98 } |
|
99 |
|
100 void CTcSerialConnection::ReceiveOneOrMore( TDes8& aDes ) |
|
101 { |
|
102 LOG( _L("CTcSerialConnection::ReceiveOneOrMore() start") ); |
|
103 // Make sure we're in correct state |
|
104 __ASSERT_ALWAYS( iState == EConnected, Panic( KErrNotReady ) ); |
|
105 __ASSERT_ALWAYS( !IsActive(), Panic( KErrInUse ) ); |
|
106 |
|
107 // Start reading |
|
108 iState = EReceiving; |
|
109 iCommPort.ReadOneOrMore( iStatus, aDes ); |
|
110 SetActive(); |
|
111 |
|
112 LOG( _L("CTcSerialConnection::ReceiveOneOrMore() end") ); |
|
113 } |
|
114 |
|
115 void CTcSerialConnection::SetupPortL() |
|
116 { |
|
117 LOG( _L("CTcSerialConnection::SetupPortL()") ); |
|
118 |
|
119 SetupPortBaudrateValuesL(); |
|
120 |
|
121 User::LeaveIfError( iCommPort.Open( iCommServ, iPortName, ECommShared ) ); |
|
122 |
|
123 // Configure port, chack first whether ports supports settings we want |
|
124 TCommCaps ourCapabilities; |
|
125 iCommPort.Caps( ourCapabilities ); |
|
126 |
|
127 if ( ( ( ourCapabilities().iRate & iBaudrateCap ) == 0 ) || |
|
128 ( ( ourCapabilities().iDataBits & KCapsData8 ) == 0 ) || |
|
129 ( ( ourCapabilities().iStopBits & KCapsStop1 ) == 0 ) || |
|
130 ( ( ourCapabilities().iParity & KCapsParityNone ) == 0 ) ) |
|
131 { |
|
132 User::Leave( KErrNotSupported ); |
|
133 } |
|
134 |
|
135 TCommConfig portSettings; |
|
136 iCommPort.Config( portSettings ); |
|
137 portSettings().iRate = iBaudrate; |
|
138 portSettings().iParity = EParityNone; |
|
139 portSettings().iDataBits = EData8; |
|
140 portSettings().iStopBits = EStop1; |
|
141 |
|
142 // as well as the physical characteristics, we need to set various logical ones |
|
143 // to do with handshaking, behaviour of reads and writes and so so |
|
144 portSettings().iFifo = EFifoEnable; |
|
145 portSettings().iHandshake = ( KConfigObeyCTS | KConfigFreeRTS ); // for cts/rts |
|
146 |
|
147 portSettings ().iTerminator[ 0 ] = 10; |
|
148 portSettings ().iTerminatorCount = 1; // so that we terminate a read on each line feed arrives |
|
149 |
|
150 User::LeaveIfError( iCommPort.SetConfig( portSettings ) ); |
|
151 |
|
152 // now turn on CTS and RTS |
|
153 iCommPort.SetSignals( KSignalCTS, 0 ); |
|
154 iCommPort.SetSignals( KSignalRTS, 0 ); |
|
155 |
|
156 // (perhaps set our receive buffer size also) |
|
157 //TInt curlenth = commPort.ReceiveBufferLength(); |
|
158 //commPort.SetReceiveBufferLength( KTcRequestMaxLength ); |
|
159 |
|
160 LOG( _L("CTcSerialConnection::ConnectL() done") ); |
|
161 } |
|
162 |
|
163 void CTcSerialConnection::SetupPortBaudrateValuesL() |
|
164 { |
|
165 LOG( _L("CTcSerialConnection::SetupPortBaudrateValuesL()") ); |
|
166 |
|
167 switch ( iPortSpeed ) |
|
168 { |
|
169 case 1200: |
|
170 { |
|
171 iBaudrate = EBps1200; |
|
172 iBaudrateCap = KCapsBps1200; |
|
173 break; |
|
174 } |
|
175 case 1800: |
|
176 { |
|
177 iBaudrate = EBps1800; |
|
178 iBaudrateCap = KCapsBps1800; |
|
179 break; |
|
180 } |
|
181 case 2000: |
|
182 { |
|
183 iBaudrate = EBps2000; |
|
184 iBaudrateCap = KCapsBps2000; |
|
185 break; |
|
186 } |
|
187 case 2400: |
|
188 { |
|
189 iBaudrate = EBps2400; |
|
190 iBaudrateCap = KCapsBps2400; |
|
191 break; |
|
192 } |
|
193 case 3600: |
|
194 { |
|
195 iBaudrate = EBps3600; |
|
196 iBaudrateCap = KCapsBps3600; |
|
197 break; |
|
198 } |
|
199 case 4800: |
|
200 { |
|
201 iBaudrate = EBps4800; |
|
202 iBaudrateCap = KCapsBps4800; |
|
203 break; |
|
204 } |
|
205 case 7200: |
|
206 { |
|
207 iBaudrate = EBps7200; |
|
208 iBaudrateCap = KCapsBps7200; |
|
209 break; |
|
210 } |
|
211 case 9600: |
|
212 { |
|
213 iBaudrate = EBps9600; |
|
214 iBaudrateCap = KCapsBps9600; |
|
215 break; |
|
216 } |
|
217 case 19200: |
|
218 { |
|
219 iBaudrate = EBps19200; |
|
220 iBaudrateCap = KCapsBps19200; |
|
221 break; |
|
222 } |
|
223 case 38400: |
|
224 { |
|
225 iBaudrate = EBps38400; |
|
226 iBaudrateCap = KCapsBps38400; |
|
227 break; |
|
228 } |
|
229 case 57600: |
|
230 { |
|
231 iBaudrate = EBps57600; |
|
232 iBaudrateCap = KCapsBps57600; |
|
233 break; |
|
234 } |
|
235 case 115200: |
|
236 { |
|
237 iBaudrate = EBps115200; |
|
238 iBaudrateCap = KCapsBps115200; |
|
239 break; |
|
240 } |
|
241 default: |
|
242 { |
|
243 User::Leave( KErrArgument ); |
|
244 break; |
|
245 } |
|
246 } |
|
247 } |
|
248 |