1 /* |
|
2 * Copyright (c) 2009 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: ECOM plugin for communication over serial port |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include <badesca.h> |
|
21 #include <f32file.h> |
|
22 |
|
23 #include "HtiBtCommEcomPlugin.h" |
|
24 #include "BtSerialClient.h" |
|
25 #include <HtiCfg.h> |
|
26 #include <HtiLogging.h> |
|
27 |
|
28 // CONSTANTS |
|
29 _LIT( KHtiCfgPath, "\\" ); // root of drive |
|
30 _LIT( KHtiBtCommCfg, "HTIBtComm.cfg" ); |
|
31 _LIT8( KBtDeviceAddress, "BtDeviceAddress" ); |
|
32 _LIT8( KBtDeviceName, "BtDeviceName" ); |
|
33 |
|
34 |
|
35 const TInt KBtAddressHexStringLength = 12; // 6 bytes |
|
36 const TUint KPortSeparatorChar = 30; // ASCII control char "record separator" |
|
37 |
|
38 // ---------------------------------------------------------------------------- |
|
39 CHtiBtCommEcomPlugin* CHtiBtCommEcomPlugin::NewL() |
|
40 { |
|
41 HTI_LOG_FUNC_IN( "CHtiBtCommEcomPlugin::NewL" ); |
|
42 CHtiBtCommEcomPlugin* plugin = new (ELeave) CHtiBtCommEcomPlugin(); |
|
43 CleanupStack::PushL( plugin ); |
|
44 plugin->ConstructL(); |
|
45 CleanupStack::Pop( plugin ); |
|
46 HTI_LOG_FUNC_OUT( "CHtiBtCommEcomPlugin::NewL" ); |
|
47 return plugin; |
|
48 } |
|
49 |
|
50 // ---------------------------------------------------------------------------- |
|
51 CHtiBtCommEcomPlugin::~CHtiBtCommEcomPlugin() |
|
52 { |
|
53 HTI_LOG_FUNC_IN( "CHtiBtCommEcomPlugin::~CHtiBtCommEcomPlugin" ); |
|
54 iBtCommInterface.CancelReceive(); |
|
55 iBtCommInterface.CancelSend(); |
|
56 iBtCommInterface.Close(); |
|
57 delete iBtDeviceNameOrAddress; |
|
58 delete iCfg; |
|
59 HTI_LOG_FUNC_OUT( "CHtiBtCommEcomPlugin::~CHtiBtCommEcomPlugin" ); |
|
60 } |
|
61 |
|
62 // ---------------------------------------------------------------------------- |
|
63 CHtiBtCommEcomPlugin::CHtiBtCommEcomPlugin():iPort( -1 ) |
|
64 { |
|
65 } |
|
66 |
|
67 // ---------------------------------------------------------------------------- |
|
68 void CHtiBtCommEcomPlugin::ConstructL() |
|
69 { |
|
70 TRAPD( err, LoadConfigL() ); |
|
71 if ( err == KErrNone ) |
|
72 { |
|
73 ReadConfig(); |
|
74 } |
|
75 else |
|
76 { |
|
77 TBufC8<1> emptyBuf( KNullDesC8 ); |
|
78 iBtDeviceNameOrAddress = emptyBuf.AllocL(); |
|
79 iPort = KErrNotFound; |
|
80 } |
|
81 // May take long! |
|
82 User::LeaveIfError( iBtCommInterface.Connect( |
|
83 *iBtDeviceNameOrAddress, iPort ) ); |
|
84 |
|
85 // Get the connected service port number (channel) and store it if needed |
|
86 TInt connectedPort = iBtCommInterface.GetPortNumber(); |
|
87 HTI_LOG_FORMAT( "Connected port number = %d", connectedPort ); |
|
88 if ( connectedPort != iPort && connectedPort >= 0 ) |
|
89 { |
|
90 TRAP( err, StorePortNumberL( connectedPort ) ); // ignore error |
|
91 } |
|
92 } |
|
93 |
|
94 // ---------------------------------------------------------------------------- |
|
95 void CHtiBtCommEcomPlugin::Receive( TDes8& aRawdataBuf, |
|
96 TRequestStatus& aStatus ) |
|
97 { |
|
98 iBtCommInterface.Receive( aRawdataBuf, aStatus ); |
|
99 } |
|
100 |
|
101 // ---------------------------------------------------------------------------- |
|
102 void CHtiBtCommEcomPlugin::Send( const TDesC8& aRawdataBuf, |
|
103 TRequestStatus& aStatus ) |
|
104 { |
|
105 iBtCommInterface.Send( aRawdataBuf, aStatus ); |
|
106 } |
|
107 |
|
108 // ---------------------------------------------------------------------------- |
|
109 void CHtiBtCommEcomPlugin::CancelReceive() |
|
110 { |
|
111 iBtCommInterface.CancelReceive(); |
|
112 } |
|
113 |
|
114 // ---------------------------------------------------------------------------- |
|
115 void CHtiBtCommEcomPlugin::CancelSend() |
|
116 { |
|
117 iBtCommInterface.CancelSend(); |
|
118 } |
|
119 |
|
120 // ---------------------------------------------------------------------------- |
|
121 TInt CHtiBtCommEcomPlugin::GetSendBufferSize() |
|
122 { |
|
123 return iBtCommInterface.GetSendBufferSize(); |
|
124 } |
|
125 |
|
126 // ---------------------------------------------------------------------------- |
|
127 TInt CHtiBtCommEcomPlugin::GetReceiveBufferSize() |
|
128 { |
|
129 return iBtCommInterface.GetReceiveBufferSize(); |
|
130 } |
|
131 |
|
132 // ---------------------------------------------------------------------------- |
|
133 void CHtiBtCommEcomPlugin::LoadConfigL() |
|
134 { |
|
135 HTI_LOG_FUNC_IN( "CHtiBtCommEcomPlugin::LoadConfigL" ); |
|
136 iCfg = CHtiCfg::NewL(); |
|
137 iCfg->LoadCfgL( KHtiCfgPath, KHtiBtCommCfg ); |
|
138 HTI_LOG_TEXT( "cfg file loaded" ); |
|
139 HTI_LOG_FUNC_OUT( "CHtiBtCommEcomPlugin::LoadConfigL" ); |
|
140 } |
|
141 |
|
142 // ---------------------------------------------------------------------------- |
|
143 void CHtiBtCommEcomPlugin::ReadConfig() |
|
144 { |
|
145 HTI_LOG_FUNC_IN( "CHtiBtCommEcomPlugin::ReadConfig" ); |
|
146 // First try to get address - if found and length is correct it is used |
|
147 TRAPD( paramErr, iBtDeviceNameOrAddress = iCfg->GetParameterL( |
|
148 KBtDeviceAddress ).AllocL() ); |
|
149 |
|
150 if ( paramErr == KErrNone ) |
|
151 { |
|
152 TInt port = ParsePortNumber(); |
|
153 if ( port >= 0 ) iPort = port; |
|
154 else iPort = KErrNotFound; |
|
155 } |
|
156 |
|
157 if ( paramErr || |
|
158 iBtDeviceNameOrAddress->Length() != KBtAddressHexStringLength ) |
|
159 { |
|
160 HTI_LOG_TEXT( "BT address not defined or not valid - getting name" ); |
|
161 delete iBtDeviceNameOrAddress; |
|
162 iBtDeviceNameOrAddress = NULL; |
|
163 // Address was not found - try to get name. |
|
164 TRAP( paramErr, iBtDeviceNameOrAddress = iCfg->GetParameterL( |
|
165 KBtDeviceName ).AllocL() ); |
|
166 if ( paramErr ) |
|
167 { |
|
168 HTI_LOG_TEXT( "BT name not defined either" ); |
|
169 TBufC8<1> emptyBuf( KNullDesC8 ); |
|
170 iBtDeviceNameOrAddress = emptyBuf.AllocL(); |
|
171 iPort = KErrNotFound; |
|
172 } |
|
173 else |
|
174 { |
|
175 TInt port = ParsePortNumber(); |
|
176 if ( port >= 0 ) iPort = port; |
|
177 else iPort = KErrNotFound; |
|
178 } |
|
179 } |
|
180 HTI_LOG_TEXT( "BT device name or address:" ); |
|
181 HTI_LOG_DES( *iBtDeviceNameOrAddress ); |
|
182 HTI_LOG_FORMAT( "Port number: %d", iPort ); |
|
183 HTI_LOG_FUNC_OUT( "CHtiBtCommEcomPlugin::ReadConfig" ); |
|
184 } |
|
185 |
|
186 // ---------------------------------------------------------------------------- |
|
187 TInt CHtiBtCommEcomPlugin::ParsePortNumber() |
|
188 { |
|
189 HTI_LOG_FUNC_IN( "CHtiBtCommEcomPlugin::ParsePortNumber" ); |
|
190 TInt port = KErrNotFound; |
|
191 TInt separatorIndex = iBtDeviceNameOrAddress->LocateReverse( |
|
192 TChar( KPortSeparatorChar ) ); |
|
193 if ( separatorIndex != KErrNotFound ) |
|
194 { |
|
195 TPtrC8 value = iBtDeviceNameOrAddress->Right( |
|
196 iBtDeviceNameOrAddress->Length() - ( separatorIndex + 1 ) ); |
|
197 TLex8 lex( value ); |
|
198 lex.Val( port ); |
|
199 // Strip away the port number |
|
200 iBtDeviceNameOrAddress->Des().SetLength( separatorIndex ); |
|
201 } |
|
202 HTI_LOG_FORMAT( "ParsePortNumber returning %d", port ); |
|
203 HTI_LOG_FUNC_OUT( "CHtiBtCommEcomPlugin::ParsePortNumber" ); |
|
204 return port; |
|
205 } |
|
206 |
|
207 // ---------------------------------------------------------------------------- |
|
208 void CHtiBtCommEcomPlugin::StorePortNumberL( TInt aPortNumber ) |
|
209 { |
|
210 HTI_LOG_FUNC_IN( "CHtiBtCommEcomPlugin::StorePortNumberL" ); |
|
211 if ( !iCfg ) |
|
212 { |
|
213 iCfg = CHtiCfg::NewL(); |
|
214 } |
|
215 iCfg->LoadCfgL( KHtiCfgPath, KHtiBtCommCfg ); |
|
216 |
|
217 TBool changesMade = EFalse; |
|
218 |
|
219 // Add or replace port number to address parameter if it exists |
|
220 HBufC8* value = NULL; |
|
221 TRAPD( err, value = iCfg->GetParameterL( KBtDeviceAddress ).AllocL() ); |
|
222 if ( !err ) |
|
223 { |
|
224 HTI_LOG_FORMAT( "Found address param (length = %d)", value->Length() ); |
|
225 CleanupStack::PushL( value ); |
|
226 TInt separatorIndex = value->Locate( TChar( KPortSeparatorChar ) ); |
|
227 if ( separatorIndex != KErrNotFound ) |
|
228 { |
|
229 HTI_LOG_FORMAT( "Separator char found from index %d", separatorIndex ); |
|
230 value->Des().SetLength( separatorIndex ); |
|
231 } |
|
232 value = value->ReAllocL( value->Length() + 5 ); |
|
233 // The old value has been deleted by ReAllocL and new allocated, |
|
234 // so update cleanup stack. |
|
235 CleanupStack::Pop(); |
|
236 CleanupStack::PushL( value ); |
|
237 TPtr8 ptr = value->Des(); |
|
238 ptr.Append( TChar( KPortSeparatorChar ) ); |
|
239 ptr.AppendNum( aPortNumber ); |
|
240 TRAP( err, iCfg->SetParameterL( KBtDeviceAddress, *value ) ); |
|
241 if ( !err ) changesMade = ETrue; |
|
242 CleanupStack::PopAndDestroy(); // value |
|
243 } |
|
244 |
|
245 // Add or replace port number to name parameter if it exists |
|
246 TRAP( err, value = iCfg->GetParameterL( KBtDeviceName ).AllocL() ); |
|
247 if ( !err ) |
|
248 { |
|
249 HTI_LOG_FORMAT( "Found name param (length = %d)", value->Length() ); |
|
250 CleanupStack::PushL( value ); |
|
251 TInt separatorIndex = value->Locate( TChar( KPortSeparatorChar ) ); |
|
252 if ( separatorIndex != KErrNotFound ) |
|
253 { |
|
254 HTI_LOG_FORMAT( "Separator char found from index %d", separatorIndex ); |
|
255 value->Des().SetLength( separatorIndex ); |
|
256 } |
|
257 value = value->ReAllocL( value->Length() + 5 ); |
|
258 // The old value has been deleted by ReAllocL and new allocated, |
|
259 // so update cleanup stack. |
|
260 CleanupStack::Pop(); |
|
261 CleanupStack::PushL( value ); |
|
262 TPtr8 ptr = value->Des(); |
|
263 ptr.Append( TChar( KPortSeparatorChar ) ); |
|
264 ptr.AppendNum( aPortNumber ); |
|
265 TRAP( err, iCfg->SetParameterL( KBtDeviceName, *value ) ); |
|
266 if ( !err ) changesMade = ETrue; |
|
267 CleanupStack::PopAndDestroy(); // value |
|
268 } |
|
269 |
|
270 if ( changesMade ) |
|
271 { |
|
272 iCfg->SaveCfgL( KHtiCfgPath, KHtiBtCommCfg ); |
|
273 } |
|
274 HTI_LOG_FUNC_OUT( "CHtiBtCommEcomPlugin::StorePortNumberL" ); |
|
275 } |
|
276 |
|
277 // End of file |
|