|
1 /* |
|
2 * Copyright (c) 2004 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: |
|
15 * This class handles data exchange between BTSap server and client |
|
16 * |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 // INCLUDE FILES |
|
22 #include "BTSapRequestHandler.h" |
|
23 #include "BTSapServerState.h" |
|
24 #include "BTSapSocketHandler.h" |
|
25 #include "debug.h" |
|
26 |
|
27 const TInt KSegmentTimeout = 1000000; // 1s |
|
28 |
|
29 CBTSapRequestHandler::CBTSapRequestHandler(CBTSapServerState& aServerState) |
|
30 : CActive(EPriorityNormal), |
|
31 iServerState(aServerState), |
|
32 iRequestMessage(aServerState.BTSapRequestMessage()) |
|
33 { |
|
34 CActiveScheduler::Add(this); |
|
35 } |
|
36 |
|
37 CBTSapRequestHandler::~CBTSapRequestHandler() |
|
38 { |
|
39 BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap] ~CBTSapRequestHandler"))); |
|
40 |
|
41 Cancel(); |
|
42 delete iSegmentTimer; |
|
43 } |
|
44 |
|
45 // --------------------------------------------------------- |
|
46 // NewL() |
|
47 // --------------------------------------------------------- |
|
48 // |
|
49 CBTSapRequestHandler* CBTSapRequestHandler::NewL(CBTSapServerState& aServerState) |
|
50 { |
|
51 CBTSapRequestHandler* self = new (ELeave) CBTSapRequestHandler(aServerState); |
|
52 CleanupStack::PushL(self); |
|
53 self->ConstructL(); |
|
54 CleanupStack::Pop(); |
|
55 return self; |
|
56 } |
|
57 |
|
58 // --------------------------------------------------------- |
|
59 // ConstructL |
|
60 // --------------------------------------------------------- |
|
61 // |
|
62 void CBTSapRequestHandler::ConstructL() |
|
63 { |
|
64 BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap] CBTSapRequestHandler: ConstructL"))); |
|
65 |
|
66 iSegmentTimer = CSegmentTimer::NewL(this); |
|
67 |
|
68 iStatus = KRequestPending; |
|
69 SetActive(); |
|
70 } |
|
71 |
|
72 // --------------------------------------------------------- |
|
73 // DoCancel |
|
74 // --------------------------------------------------------- |
|
75 // |
|
76 void CBTSapRequestHandler::DoCancel() |
|
77 { |
|
78 BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap] CBTSapRequestHandler: DoCancel"))); |
|
79 |
|
80 TRequestStatus* status = &iStatus; |
|
81 User::RequestComplete(status, KErrCancel); |
|
82 } |
|
83 |
|
84 // --------------------------------------------------------- |
|
85 // RunL |
|
86 // --------------------------------------------------------- |
|
87 // |
|
88 void CBTSapRequestHandler::RunL() |
|
89 { |
|
90 BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap] CBTSapRequestHandler: RunL"))); |
|
91 TInt status = iStatus.Int(); |
|
92 |
|
93 if ( status != KErrNone ) |
|
94 { |
|
95 HandleSapMessage(EInvalidSegmented); |
|
96 } |
|
97 else |
|
98 { |
|
99 while (!iRawMessage.IsEmpty()) |
|
100 { |
|
101 TValidationResult result = iRawMessage.Validate(); |
|
102 |
|
103 if (result == EInvalidSegmented) |
|
104 { |
|
105 // start timer if not started |
|
106 iSegmentTimer->Cancel(); |
|
107 iSegmentTimer->After(KSegmentTimeout); |
|
108 break; |
|
109 } |
|
110 else |
|
111 { |
|
112 // complete message |
|
113 HandleSapMessage(result); |
|
114 } |
|
115 } |
|
116 } |
|
117 iStatus = KRequestPending; |
|
118 SetActive(); |
|
119 } |
|
120 |
|
121 void CBTSapRequestHandler::HandleSapMessage(const TValidationResult aResult) |
|
122 { |
|
123 // stop timer |
|
124 iSegmentTimer->Cancel(); |
|
125 |
|
126 iRequestMessage.SetData(iRawMessage.Data()); |
|
127 //DEB(Hex(_L8("[BTSap] Request: %S"), &iRequestMessage.Data())); |
|
128 |
|
129 iRawMessage.Reset(); |
|
130 |
|
131 TBTSapServerState nextState = EStateIdle; |
|
132 |
|
133 if (aResult == EValidFormat) |
|
134 { |
|
135 nextState = KStateByRequest[(TInt)iRequestMessage.MsgID()]; |
|
136 } |
|
137 |
|
138 if (iServerState.HandleStateChangeRequest(nextState) != KErrNone) |
|
139 { |
|
140 iServerState.SendErrorResponse(); |
|
141 |
|
142 if (nextState != iServerState.CurrentState()) |
|
143 { |
|
144 iServerState.ChangeState(nextState); |
|
145 } |
|
146 } |
|
147 else |
|
148 { |
|
149 // always change state if request is acceptable |
|
150 iServerState.ChangeState(nextState); |
|
151 } |
|
152 } |
|
153 |
|
154 void CBTSapRequestHandler::HandleSapData(const TDes8& aData) |
|
155 { |
|
156 BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap] CBTSapRequestHandler: HandleSapData"))); |
|
157 |
|
158 TInt result = iRawMessage.AppendData(aData); |
|
159 |
|
160 if (IsActive() && iStatus == KRequestPending) |
|
161 { |
|
162 TRequestStatus* status = &iStatus; |
|
163 User::RequestComplete(status, result); |
|
164 } |
|
165 |
|
166 } |
|
167 |
|
168 void CBTSapRequestHandler::HandleSegmentTimeout() |
|
169 { |
|
170 BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap] CBTSapRequestHandler: HandleIncompleteMessage"))); |
|
171 |
|
172 TValidationResult result = iRawMessage.Validate(); |
|
173 HandleSapMessage(result); |
|
174 } |
|
175 |
|
176 CBTSapRequestHandler::CSegmentTimer::CSegmentTimer(CBTSapRequestHandler* aRequestHandler) |
|
177 : CTimer(EPriorityStandard), iRequestHandler(aRequestHandler) |
|
178 { |
|
179 CActiveScheduler::Add(this); |
|
180 } |
|
181 |
|
182 CBTSapRequestHandler::CSegmentTimer* CBTSapRequestHandler::CSegmentTimer::NewL( |
|
183 CBTSapRequestHandler* aRequestHandler) |
|
184 { |
|
185 CSegmentTimer* self = new (ELeave) CSegmentTimer(aRequestHandler); |
|
186 CleanupStack::PushL(self); |
|
187 self->ConstructL(); |
|
188 CleanupStack::Pop(self); |
|
189 return self; |
|
190 } |
|
191 |
|
192 void CBTSapRequestHandler::CSegmentTimer::RunL() |
|
193 { |
|
194 iRequestHandler->HandleSegmentTimeout(); |
|
195 } |
|
196 |
|
197 // End of file |