60
|
1 |
/*
|
|
2 |
* Component Name: Headset Simulator
|
|
3 |
* Author: Comarch S.A.
|
|
4 |
* Version: 1.0
|
|
5 |
* Copyright (c) 2010 Comarch S.A.
|
|
6 |
*
|
|
7 |
* This Software is submitted by Comarch S.A. to Symbian Foundation Limited on
|
|
8 |
* the basis of the Member Contribution Agreement entered between Comarch S.A.
|
|
9 |
* and Symbian Foundation Limited on 5th June 2009 (“Agreement”) and may be
|
|
10 |
* used only in accordance with the terms and conditions of the Agreement.
|
|
11 |
* Any other usage, duplication or redistribution of this Software is not
|
|
12 |
* allowed without written permission of Comarch S.A.
|
|
13 |
*
|
|
14 |
*/
|
|
15 |
|
|
16 |
#include "hsserver.h"
|
|
17 |
#include "hsclientobservers.h"
|
|
18 |
#include "debug.h"
|
|
19 |
|
|
20 |
CHsServer* CHsServer::NewL( RSocketServ &aSocketServ )
|
|
21 |
{
|
|
22 |
CHsServer* self = CHsServer::NewLC( aSocketServ );
|
|
23 |
CleanupStack::Pop( self );
|
|
24 |
return self;
|
|
25 |
}
|
|
26 |
|
|
27 |
CHsServer* CHsServer::NewLC( RSocketServ &aSocketServ )
|
|
28 |
{
|
|
29 |
CHsServer* self = new ( ELeave ) CHsServer( aSocketServ );
|
|
30 |
CleanupStack::PushL( self );
|
|
31 |
self->ConstructL();
|
|
32 |
return self;
|
|
33 |
}
|
|
34 |
|
|
35 |
CHsServer::~CHsServer()
|
|
36 |
{
|
|
37 |
TRACE_FUNC_ENTRY
|
|
38 |
|
|
39 |
if ( iSocketServer )
|
|
40 |
{
|
|
41 |
delete iSocketServer;
|
|
42 |
}
|
|
43 |
|
|
44 |
if ( iBlankSocket )
|
|
45 |
{
|
|
46 |
delete iBlankSocket;
|
|
47 |
}
|
|
48 |
|
|
49 |
TRACE_FUNC_EXIT
|
|
50 |
}
|
|
51 |
|
|
52 |
TInt CHsServer::Listen()
|
|
53 |
{
|
|
54 |
TRACE_FUNC_ENTRY
|
|
55 |
|
|
56 |
TInt error = KErrNone;
|
|
57 |
TProtocolDesc protDesc;
|
|
58 |
TBTSockAddr addr;
|
|
59 |
TProtocolName protName( KRFCOMMDesC );
|
|
60 |
|
|
61 |
if ( iListen )
|
|
62 |
{
|
|
63 |
TRACE_FUNC_EXIT
|
|
64 |
return KErrAlreadyExists;
|
|
65 |
}
|
|
66 |
|
|
67 |
error = iSServ.FindProtocol( protName, protDesc );
|
|
68 |
if ( error != KErrNone )
|
|
69 |
{
|
|
70 |
TRACE_FUNC_EXIT
|
|
71 |
return error;
|
|
72 |
}
|
|
73 |
|
|
74 |
TRAP(error, iSocketServer = CBluetoothSocket::NewL(*this, iSServ,
|
|
75 |
protDesc.iSockType, protDesc.iProtocol));
|
|
76 |
|
|
77 |
if ( error != KErrNone )
|
|
78 |
{
|
|
79 |
TRACE_FUNC_EXIT
|
|
80 |
return error;
|
|
81 |
}
|
|
82 |
|
|
83 |
error = iSocketServer->GetOpt( KRFCOMMGetAvailableServerChannel,
|
|
84 |
KSolBtRFCOMM, iPort );
|
|
85 |
if ( error != KErrNone )
|
|
86 |
{
|
|
87 |
TRACE_FUNC_EXIT
|
|
88 |
return error;
|
|
89 |
}
|
|
90 |
|
|
91 |
addr.SetPort( iPort );
|
|
92 |
addr.SetSecurity( iServerSecurity );
|
|
93 |
|
|
94 |
error = iSocketServer->Bind( addr );
|
|
95 |
if ( error != KErrNone )
|
|
96 |
{
|
|
97 |
TRACE_FUNC_EXIT
|
|
98 |
return error;
|
|
99 |
}
|
|
100 |
|
|
101 |
error = iSocketServer->Listen( KListenQueueSize );
|
|
102 |
if ( error != KErrNone )
|
|
103 |
{
|
|
104 |
TRACE_FUNC_EXIT
|
|
105 |
return error;
|
|
106 |
}
|
|
107 |
|
|
108 |
if ( !iBlankSocket )
|
|
109 |
{
|
|
110 |
TRAP(error, iBlankSocket=CBluetoothSocket::NewL(*this, iSServ));
|
|
111 |
}
|
|
112 |
|
|
113 |
if ( error != KErrNone )
|
|
114 |
{
|
|
115 |
TRACE_FUNC_EXIT
|
|
116 |
return error;
|
|
117 |
}
|
|
118 |
|
|
119 |
error = iSocketServer->Accept( *iBlankSocket );
|
|
120 |
|
|
121 |
iListen = ETrue;
|
|
122 |
|
|
123 |
TRACE_INFO( ( _L("Returned value = %d"), error) );
|
|
124 |
TRACE_FUNC_EXIT
|
|
125 |
|
|
126 |
return error;
|
|
127 |
}
|
|
128 |
|
|
129 |
TBool CHsServer::IsReady()
|
|
130 |
{
|
|
131 |
return iListen;
|
|
132 |
}
|
|
133 |
|
|
134 |
void CHsServer::Shutdown()
|
|
135 |
{
|
|
136 |
TRACE_FUNC_ENTRY
|
|
137 |
|
|
138 |
if ( iSocketServer )
|
|
139 |
{
|
|
140 |
if ( iListen )
|
|
141 |
{
|
|
142 |
iSocketServer->CancelAccept();
|
|
143 |
iListen = EFalse;
|
|
144 |
}
|
|
145 |
|
|
146 |
// Return value may be ignored
|
|
147 |
iSocketServer->Shutdown( RSocket::EImmediate );
|
|
148 |
delete iSocketServer;
|
|
149 |
iSocketServer = NULL;
|
|
150 |
}
|
|
151 |
else
|
|
152 |
{
|
|
153 |
TRAP_IGNORE( HandleShutdownCompleteL(KErrNone) );
|
|
154 |
}
|
|
155 |
|
|
156 |
TRACE_FUNC_EXIT
|
|
157 |
}
|
|
158 |
|
|
159 |
void CHsServer::SetNewClientObserver(
|
|
160 |
MHsAcceptNewClientObserver &aNewClientObserv )
|
|
161 |
{
|
|
162 |
iNewClientObserver = &aNewClientObserv;
|
|
163 |
}
|
|
164 |
|
|
165 |
TInt CHsServer::GetPort()
|
|
166 |
{
|
|
167 |
return iPort;
|
|
168 |
}
|
|
169 |
|
|
170 |
void CHsServer::GetSocketAndTryToConnectL( const TBTSockAddr &aAddr )
|
|
171 |
{
|
|
172 |
TRACE_FUNC_ENTRY
|
|
173 |
TBTSockAddr sockaddr( aAddr );
|
|
174 |
TProtocolName protocolName( KRFCOMMDesC );
|
|
175 |
TProtocolDesc protocolDesc;
|
|
176 |
|
|
177 |
if ( iBlankSocket2 != NULL )
|
|
178 |
{
|
|
179 |
delete iBlankSocket2;
|
|
180 |
iBlankSocket2 = NULL;
|
|
181 |
}
|
|
182 |
|
|
183 |
User::LeaveIfError( iSServ.FindProtocol( protocolName, protocolDesc ) );
|
|
184 |
iBlankSocket2 = CBluetoothSocket::NewL( *this, iSServ,
|
|
185 |
protocolDesc.iSockType, protocolDesc.iProtocol );
|
|
186 |
User::LeaveIfError( iBlankSocket2->Connect( sockaddr ) );
|
|
187 |
TRACE_FUNC_EXIT
|
|
188 |
}
|
|
189 |
|
|
190 |
CHsServer::CHsServer( RSocketServ &aSocketServ ) :
|
|
191 |
iSServ( aSocketServ )
|
|
192 |
{
|
|
193 |
iServerSecurity.SetUid( TUid::Uid( 0x10ff ) );
|
|
194 |
iServerSecurity.SetAuthentication( EFalse );
|
|
195 |
iServerSecurity.SetAuthorisation( EFalse );
|
|
196 |
iServerSecurity.SetEncryption( EFalse );
|
|
197 |
}
|
|
198 |
|
|
199 |
void CHsServer::ConstructL()
|
|
200 |
{
|
|
201 |
|
|
202 |
}
|
|
203 |
|
|
204 |
void CHsServer::HandleConnectCompleteL( TInt aErr )
|
|
205 |
{
|
|
206 |
TRACE_FUNC_ENTRY
|
|
207 |
TRACE_INFO( ( _L("aErr = %d"), aErr) );
|
|
208 |
|
|
209 |
iNewClientObserver->HandleNewClientL( aErr == KErrNone ? iBlankSocket2
|
|
210 |
: NULL, aErr );
|
|
211 |
if ( aErr != KErrNone )
|
|
212 |
{
|
|
213 |
delete iBlankSocket2;
|
|
214 |
}
|
|
215 |
iBlankSocket2 = NULL;
|
|
216 |
|
|
217 |
TRACE_FUNC_EXIT
|
|
218 |
}
|
|
219 |
|
|
220 |
void CHsServer::HandleAcceptCompleteL( TInt aErr )
|
|
221 |
{
|
|
222 |
TRACE_FUNC_ENTRY
|
|
223 |
TRACE_INFO( ( _L("aErr = %d"), aErr) );
|
|
224 |
|
|
225 |
if ( iNewClientObserver )
|
|
226 |
{
|
|
227 |
iNewClientObserver->HandleNewClientL( iBlankSocket, aErr );
|
|
228 |
}
|
|
229 |
else
|
|
230 |
{
|
|
231 |
iNewClientObserver->HandleNewClientL( NULL, KErrBadHandle );
|
|
232 |
delete iBlankSocket;
|
|
233 |
}
|
|
234 |
iBlankSocket = NULL;
|
|
235 |
iBlankSocket = CBluetoothSocket::NewL( *this, iSServ );
|
|
236 |
User::LeaveIfError( iSocketServer->Accept( *iBlankSocket ) );
|
|
237 |
|
|
238 |
TRACE_FUNC_EXIT
|
|
239 |
}
|
|
240 |
|
|
241 |
void CHsServer::HandleShutdownCompleteL( TInt /** aErr */)
|
|
242 |
{
|
|
243 |
TRACE_FUNC_ENTRY
|
|
244 |
|
|
245 |
if ( iBlankSocket )
|
|
246 |
{
|
|
247 |
delete iBlankSocket;
|
|
248 |
iBlankSocket = NULL;
|
|
249 |
}
|
|
250 |
if ( iSocketServer )
|
|
251 |
{
|
|
252 |
delete iSocketServer;
|
|
253 |
iSocketServer = NULL;
|
|
254 |
}
|
|
255 |
iListen = EFalse;
|
|
256 |
|
|
257 |
TRACE_FUNC_EXIT
|
|
258 |
}
|
|
259 |
|
|
260 |
void CHsServer::HandleSendCompleteL( TInt /** aErr */)
|
|
261 |
{
|
|
262 |
|
|
263 |
}
|
|
264 |
|
|
265 |
void CHsServer::HandleReceiveCompleteL( TInt /** aErr */)
|
|
266 |
{
|
|
267 |
|
|
268 |
}
|
|
269 |
|
|
270 |
void CHsServer::HandleIoctlCompleteL( TInt /** aErr */)
|
|
271 |
{
|
|
272 |
}
|
|
273 |
|
|
274 |
void CHsServer::HandleActivateBasebandEventNotifierCompleteL( TInt /** aErr */,
|
|
275 |
TBTBasebandEventNotification& /** aEventNotification */)
|
|
276 |
{
|
|
277 |
|
|
278 |
}
|