|
1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 |
|
17 #include "WapSocketWatcher.h" |
|
18 #include <watcher.h> |
|
19 |
|
20 // |
|
21 // CWapPortWatcher |
|
22 // |
|
23 |
|
24 // |
|
25 // Global Defines |
|
26 // |
|
27 const TInt KSmsWRetryTimer = 1000000 * 15; // 15 Secs |
|
28 |
|
29 // |
|
30 // Construction |
|
31 // private |
|
32 // |
|
33 CWapPortWatcher::CWapPortWatcher(CWatcherLog& aWatcherLog, TInt aPriority, TUid aBioID, RFs& aFs, TUint16 aWapPort) |
|
34 : CBaseSmsActiveSocketWatcher(aWatcherLog, aPriority, aBioID, aFs), iWapPort(aWapPort) |
|
35 { |
|
36 } |
|
37 |
|
38 CWapPortWatcher* CWapPortWatcher::NewL(TMsvId aBioServiceId, |
|
39 TMsvId aSmsServiceId, |
|
40 CBIODatabase& aBioDb, |
|
41 CWatcherLog& aWatcherLog, |
|
42 TInt aPriority, |
|
43 TUid aBioID, |
|
44 RFs& aFs, |
|
45 const TBioMsgId& aBioMsg) |
|
46 { |
|
47 CWapPortWatcher* self = NULL; |
|
48 |
|
49 //Create either a WDP or WSP watcher, depending on the BioMsgId type |
|
50 switch (aBioMsg.iType) |
|
51 { |
|
52 case EBioMsgIdNbs: |
|
53 case EBioMsgIdWap: |
|
54 case EBioMsgIdWapSecure: |
|
55 { |
|
56 self = new (ELeave) CWdpPortWatcher(aWatcherLog, aPriority, aBioID, aFs, STATIC_CAST(Wap::TPort,aBioMsg.iPort)); |
|
57 break; |
|
58 } |
|
59 case EBioMsgIdWsp: |
|
60 case EBioMsgIdWspSecure: |
|
61 { |
|
62 self = new (ELeave) CWspPortWatcher(aWatcherLog, aPriority, aBioID, aFs, STATIC_CAST(Wap::TPort,aBioMsg.iPort)); |
|
63 break; |
|
64 } |
|
65 default: |
|
66 User::Leave(KErrNotSupported); |
|
67 } |
|
68 |
|
69 CleanupStack::PushL(self); |
|
70 self->ConstructL(aBioDb, aBioServiceId, aSmsServiceId); |
|
71 CleanupStack::Pop(self); |
|
72 return self; |
|
73 } |
|
74 |
|
75 CWapPortWatcher::~CWapPortWatcher() |
|
76 { |
|
77 delete iRecvBuf; |
|
78 iRecvBuf = NULL; |
|
79 } |
|
80 |
|
81 void CWapPortWatcher::DoComplete(TInt& rStatus) |
|
82 { |
|
83 BIOWATCHERLOG(iWatcherLog.Printf(_L("BioWap: DoComplete: %S, port %d, status %d"), &iBioMsgText, iWapPort, rStatus)); |
|
84 |
|
85 CloseConn(); |
|
86 } |
|
87 |
|
88 void CWapPortWatcher::DoReceiveL(const TDesC8& aRecvFrom) |
|
89 { |
|
90 BIOWATCHERLOG(iWatcherLog.Printf(_L("BioWap: DoReceiveL: %S, port %d"),&iBioMsgText, iWapPort)); |
|
91 |
|
92 // Code for collecting message goes here... |
|
93 BIOWATCHERLOG(iWatcherLog.Printf(_L8("BioWap: Recv datagram length: %d on socket OK"), iRecvBuf->Length())); |
|
94 |
|
95 // Create a CSmsMessage from the recieved buffer |
|
96 CSmsBuffer* buffer=CSmsBuffer::NewL(); |
|
97 |
|
98 |
|
99 CSmsMessage* smsmessage = CSmsMessage::NewL(iFs, CSmsPDU::ESmsDeliver, buffer); |
|
100 CleanupStack::PushL(smsmessage); |
|
101 |
|
102 smsmessage->SmsPDU().SetAlphabet(TSmsDataCodingScheme::ESmsAlphabet8Bit); |
|
103 |
|
104 if (aRecvFrom.Length()) |
|
105 { |
|
106 //Convert aRecvFrom to 16-bit |
|
107 HBufC* tempRemoteAddr = HBufC::NewLC(aRecvFrom.Length()); |
|
108 tempRemoteAddr->Des().Copy(aRecvFrom); |
|
109 |
|
110 //Set the from address in the CSmsMessage |
|
111 smsmessage->SmsPDU().SetToFromAddressL(*tempRemoteAddr); |
|
112 CleanupStack::PopAndDestroy(tempRemoteAddr); |
|
113 } |
|
114 |
|
115 //Convert iRecvTotalMessage to 16-bit |
|
116 HBufC* tempRecvBuf = HBufC::NewLC(iRecvBuf->Length()); |
|
117 tempRecvBuf->Des().Copy(*iRecvBuf); |
|
118 buffer->InsertL(0, *tempRecvBuf); |
|
119 CleanupStack::PopAndDestroy(tempRecvBuf); |
|
120 |
|
121 //Pop buffer and smsmessage off the cleanup stack, |
|
122 //because StoreMsgL() will destroy the CSmsMessage (which contains the buffer). |
|
123 CleanupStack::Pop();// smsmessag |
|
124 |
|
125 StoreMsgL(smsmessage); |
|
126 |
|
127 // Not Async so signal here... |
|
128 TRequestStatus* pS=&iStatus; |
|
129 User::RequestComplete(pS, KErrNone); |
|
130 } |
|
131 |
|
132 void CWapPortWatcher::AppendToHBufC8L(HBufC8*& aBuf, const TDesC8& aAppend) |
|
133 { |
|
134 //Appends aAppend to aBuf. If aBuf is NULL, then copies aAppend into aBuf. |
|
135 |
|
136 if (!aBuf || aBuf->Length() == 0) |
|
137 { |
|
138 delete aBuf; |
|
139 aBuf = NULL; |
|
140 aBuf = aAppend.AllocL(); |
|
141 } |
|
142 else |
|
143 { |
|
144 //Append the new message in iWspRecvBuf to the iTempRecvBuf |
|
145 aBuf = aBuf->ReAllocL(aBuf->Length() + aAppend.Length()); |
|
146 aBuf->Des().Append(aAppend); |
|
147 } |
|
148 } |
|
149 |
|
150 void CWapPortWatcher::CloseConn() |
|
151 { |
|
152 delete iRecvBuf; |
|
153 iRecvBuf = NULL; |
|
154 } |
|
155 |
|
156 void CWapPortWatcher::DoRunL() |
|
157 { |
|
158 switch(iState) |
|
159 { |
|
160 case ESmsWWaitForMsg: |
|
161 iState = ESmsWReadMsg; |
|
162 WaitForMessageL(); |
|
163 break; |
|
164 |
|
165 case ESmsWReadMsg: |
|
166 iState = ESmsWWaitForMsg; |
|
167 ReceiveL(); |
|
168 break; |
|
169 |
|
170 case ESmsWRetryError: |
|
171 if (!iTimer.Handle()) |
|
172 { |
|
173 User::LeaveIfError(iTimer.CreateLocal()); |
|
174 } |
|
175 iState=ESmsWWaitForMsg; |
|
176 iTimer.After(iStatus, KSmsWRetryTimer); |
|
177 break; |
|
178 |
|
179 case ESmsIsClass0Msg: |
|
180 iState = ESmsWWaitForMsg; |
|
181 CompleteSelf(); |
|
182 break; |
|
183 } |
|
184 |
|
185 SetActive(); |
|
186 |
|
187 } |
|
188 |
|
189 // WAP messages that use Class 0 SMS messages should be treated as non Class 0 messages, |
|
190 // so need to go back to ESmsWWaitForMsg state. |
|
191 void CWapPortWatcher::CompleteSelf() |
|
192 { |
|
193 // Complete self. |
|
194 TRequestStatus* status = &iStatus; |
|
195 iStatus = KRequestPending; |
|
196 User::RequestComplete(status, KErrNone); |
|
197 } |