|
1 /* |
|
2 * Copyright (c) 2008 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 * |
|
16 */ |
|
17 |
|
18 #include <memory> |
|
19 #include <errno.h> |
|
20 #include <f32file.h> |
|
21 #include <gsmumsg.h> |
|
22 #include <smsustrm.h> |
|
23 #include <charconv.h> |
|
24 |
|
25 #include "logger.h" |
|
26 #include "s60commonutils.h" |
|
27 #include "javacommonutils.h" |
|
28 #include "javasymbianoslayer.h" |
|
29 #include "csmsplatformservices60impl.h" |
|
30 #include "cservicecentrenumretrievers60impl.h" |
|
31 |
|
32 namespace java |
|
33 { |
|
34 namespace wma |
|
35 { |
|
36 //Constructor |
|
37 CSmsPlatformServiceS60Impl::CSmsPlatformServiceS60Impl(std::wstring aUri) |
|
38 :mUri(aUri) |
|
39 { |
|
40 JELOG2(EWMA); |
|
41 } |
|
42 |
|
43 //Destructor |
|
44 CSmsPlatformServiceS60Impl::~CSmsPlatformServiceS60Impl() |
|
45 { |
|
46 JELOG2(EWMA); |
|
47 if (mUri.find(L"cbs://:") == std::wstring::npos) |
|
48 { |
|
49 mSmsSendSocket.CancelAll(); |
|
50 mSmsSendSocket.Close(); |
|
51 delete mSendMessage; |
|
52 } |
|
53 delete mMessageParameters; |
|
54 } |
|
55 |
|
56 SmsPlatformService* SmsPlatformService::getNewInstance(std::wstring aUri) |
|
57 { |
|
58 JELOG2(EWMA); |
|
59 CSmsPlatformServiceS60Impl* msgConn = new CSmsPlatformServiceS60Impl(aUri); |
|
60 if (aUri.find(L"cbs://:") == std::wstring::npos) |
|
61 { |
|
62 msgConn->initialize(); |
|
63 } |
|
64 else |
|
65 { |
|
66 // Its a Cbs Connection nothing to be initialized. |
|
67 } |
|
68 return msgConn; |
|
69 } |
|
70 /** |
|
71 * Initializing function |
|
72 */ |
|
73 void CSmsPlatformServiceS60Impl::initialize() |
|
74 { |
|
75 JELOG2(EWMA); |
|
76 int err = KErrNone; |
|
77 err = mSocketServer.Connect(); |
|
78 if (KErrNone == err) |
|
79 { |
|
80 err = mSocketServer.ShareAuto(); |
|
81 } |
|
82 if (err == KErrNone) |
|
83 { |
|
84 err = mSmsSendSocket.Open(mSocketServer, KSMSAddrFamily, |
|
85 KSockDatagram, KSMSDatagramProtocol); |
|
86 if (err == KErrNone) |
|
87 { |
|
88 mSmsSendAddress.SetFamily(ESmsAddrApplication16BitPort); |
|
89 mSmsSendAddress.SetPort(mSendPort); |
|
90 err = mSmsSendSocket.Bind(mSmsSendAddress); |
|
91 if (err != KErrNone) |
|
92 { |
|
93 ELOG1(EWMA, "SMS : error while binding socket : %d", err); |
|
94 throw err; |
|
95 } |
|
96 else |
|
97 { |
|
98 // No error nothing to do |
|
99 } |
|
100 } |
|
101 else |
|
102 { |
|
103 ELOG1(EWMA, "SMS : error while opening socket : %d", err); |
|
104 throw err; |
|
105 } |
|
106 } |
|
107 else |
|
108 { |
|
109 ELOG1(EWMA, "SMS : error while connecting to socketserver : %d", err); |
|
110 throw err; |
|
111 } |
|
112 } |
|
113 /** |
|
114 * Converts the CString to Descriptor |
|
115 */ |
|
116 HBufC* CSmsPlatformServiceS60Impl::convertToDes(const char* aData, |
|
117 const int aMsgType, |
|
118 const int aLength) |
|
119 { |
|
120 HBufC* smsData = 0; |
|
121 smsData = HBufC::New(aLength+1); |
|
122 if (aData != NULL) |
|
123 { |
|
124 if (aMsgType == MESSAGE_TEXT) |
|
125 { |
|
126 TPtr ptr = smsData->Des(); |
|
127 TPtr16 ptr16((TUint16 *)aData,aLength); |
|
128 ptr16.SetLength(aLength); |
|
129 ptr.Copy(ptr16); |
|
130 ptr.ZeroTerminate(); |
|
131 } |
|
132 else |
|
133 { |
|
134 |
|
135 TPtr ptr = smsData->Des(); |
|
136 TPtr8 ptr8((TUint8 *)aData,aLength); |
|
137 ptr8.SetLength(aLength); |
|
138 ptr.Copy(ptr8); |
|
139 ptr.ZeroTerminate(); |
|
140 } |
|
141 } |
|
142 return smsData; |
|
143 } |
|
144 /** |
|
145 * Calculates and returns numberofDataSegments of the Message. |
|
146 */ |
|
147 int CSmsPlatformServiceS60Impl::getSmsDataSegments(const int aMsgType, |
|
148 const int aLength, |
|
149 const int aHostPort, |
|
150 const char* aHostAddress, |
|
151 const char* aData) |
|
152 { |
|
153 JELOG2(EWMA); |
|
154 int numberOfSegments =0; |
|
155 TBool PortAddressing = EFalse; |
|
156 TInt receiveIntoPort = 0; |
|
157 if (mUri.find(L"sms://:") != std::wstring::npos) |
|
158 { |
|
159 receiveIntoPort = java::util::JavaCommonUtils::wstringToInt( |
|
160 mUri.substr(KPortFieldStartIndex)); |
|
161 } |
|
162 else |
|
163 { |
|
164 // It is a client connection, Nothing to do |
|
165 } |
|
166 TRAPD(error, |
|
167 { |
|
168 HBufC* smsData = convertToDes(aData,aMsgType,aLength); |
|
169 std::auto_ptr<HBufC> smsAddress(stringToDes(aHostAddress)); |
|
170 // The sms message takes ownership of the buffer |
|
171 CSmsBuffer* buffer = CSmsBuffer::NewL(); |
|
172 delete mSendMessage; |
|
173 mSendMessage = 0; |
|
174 RFs fileServer; |
|
175 mSendMessage = CSmsMessage::NewL(fileServer,CSmsPDU::ESmsSubmit,buffer); |
|
176 if (smsAddress->Length()> 0) |
|
177 { |
|
178 // set the destination address |
|
179 mSendMessage->SetToFromAddressL((const TPtrC&)*smsAddress); |
|
180 // Set SMS port addresing |
|
181 if (aHostPort != 0) |
|
182 { |
|
183 PortAddressing = ETrue; |
|
184 } |
|
185 else |
|
186 { |
|
187 PortAddressing = EFalse; |
|
188 } |
|
189 (mSendMessage->SmsPDU()).SetApplicationPortAddressingL( |
|
190 PortAddressing,aHostPort,receiveIntoPort,KUse16BitPortAddressing); |
|
191 } |
|
192 else |
|
193 { |
|
194 // Nothing to do |
|
195 } |
|
196 |
|
197 // Get the encoding for the message that is to be sent |
|
198 TSmsDataCodingScheme::TSmsAlphabet smsEncoding = getEncodingSchemeL( |
|
199 *smsData,aMsgType,fileServer); |
|
200 // sets the encoding |
|
201 |
|
202 (mSendMessage->SmsPDU()).SetAlphabet(smsEncoding); |
|
203 (mSendMessage->SmsPDU()).SetTextCompressed(EFalse); |
|
204 (mSendMessage->SmsPDU()).SetTextConcatenatedL(ETrue,ETrue); |
|
205 |
|
206 (mSendMessage->Buffer()).Reset(); |
|
207 (mSendMessage->Buffer()).InsertL(0,*smsData); |
|
208 |
|
209 mSendMessage->OptimizeSettingsL(CSmsMessage:: |
|
210 ESmsFlagOptimizeConcatenation16BitReference); |
|
211 // calculate the number of message PDUs required to send this message |
|
212 numberOfSegments = (int)mSendMessage->NumMessagePDUsL(); |
|
213 LOG2(EWMA, EInfo,"SMS :number of segments %d data length %d", |
|
214 numberOfSegments, aLength); |
|
215 delete smsData; |
|
216 }); |
|
217 return (error == KErrNone)? numberOfSegments : error; |
|
218 } |
|
219 |
|
220 /** |
|
221 * Gets the best Message encoding for the message. |
|
222 */ |
|
223 TSmsDataCodingScheme::TSmsAlphabet CSmsPlatformServiceS60Impl :: |
|
224 getEncodingSchemeL(const TDesC& aSmsData,const TInt aMsgType,RFs& aFs) |
|
225 { |
|
226 JELOG2(EWMA); |
|
227 TSmsDataCodingScheme::TSmsAlphabet msgEncoding; |
|
228 if (aMsgType == MESSAGE_TEXT) |
|
229 { |
|
230 TBool isGSM7Convertible = EFalse; |
|
231 TPtrC tempData(aSmsData); |
|
232 TBuf8<KCharacterConversionBufferSize> tempBuf; |
|
233 TInt charsToBeConverted = aSmsData.Length(); |
|
234 TInt nonConvertibleCharacters; |
|
235 CCnvCharacterSetConverter* cConverter = |
|
236 CCnvCharacterSetConverter::NewL(); |
|
237 CCnvCharacterSetConverter::TAvailability cnvAvailable = |
|
238 cConverter->PrepareToConvertToOrFromL( |
|
239 KCharacterSetIdentifierSms7Bit, aFs); |
|
240 |
|
241 if (cnvAvailable == CCnvCharacterSetConverter::EAvailable) |
|
242 { |
|
243 isGSM7Convertible = ETrue; |
|
244 while (charsToBeConverted> 0) |
|
245 { |
|
246 charsToBeConverted = cConverter->ConvertFromUnicode( |
|
247 tempBuf,tempData, nonConvertibleCharacters); |
|
248 |
|
249 if (nonConvertibleCharacters> 0) |
|
250 { |
|
251 isGSM7Convertible = EFalse; |
|
252 break; |
|
253 } |
|
254 else |
|
255 { |
|
256 // Nothing to do |
|
257 } |
|
258 |
|
259 tempBuf.Zero(); |
|
260 tempData.Set(tempData.Right(charsToBeConverted)); |
|
261 } |
|
262 } |
|
263 else |
|
264 { |
|
265 // Nothing to do |
|
266 } |
|
267 (isGSM7Convertible)?msgEncoding = TSmsDataCodingScheme::ESmsAlphabet7Bit |
|
268 :msgEncoding = TSmsDataCodingScheme::ESmsAlphabetUCS2; |
|
269 delete cConverter; |
|
270 cConverter = 0; |
|
271 } |
|
272 else |
|
273 { |
|
274 msgEncoding = TSmsDataCodingScheme::ESmsAlphabet8Bit; |
|
275 } |
|
276 return msgEncoding; |
|
277 } |
|
278 /** |
|
279 * Method to send the message. |
|
280 */ |
|
281 int CSmsPlatformServiceS60Impl::send() |
|
282 { |
|
283 JELOG2(EWMA); |
|
284 int error =KErrNone; |
|
285 TRAP(error, |
|
286 { |
|
287 TBuf<RMobilePhone::KMaxMobileTelNumberSize>ServiceCentreNumber; |
|
288 //get the service centre number |
|
289 CServiceCentreNumRetrieverS60Impl::retrieveServiceCentreNumberL( |
|
290 ServiceCentreNumber); |
|
291 (mSendMessage->SmsPDU()).SetServiceCenterAddressL(ServiceCentreNumber); |
|
292 RSmsSocketWriteStream socketWrite(mSmsSendSocket); |
|
293 CleanupClosePushL(socketWrite); |
|
294 socketWrite << *mSendMessage; |
|
295 socketWrite.CommitL(); |
|
296 CleanupStack::PopAndDestroy(&socketWrite); |
|
297 TRequestStatus status; |
|
298 mSmsSendSocket.Ioctl(KIoctlSendSmsMessage,status,&mSmsSendDummyBuf, |
|
299 KSolSmsProv); |
|
300 User::WaitForRequest(status); |
|
301 User::LeaveIfError(status.Int()); |
|
302 }); |
|
303 return error; |
|
304 } |
|
305 |
|
306 /** |
|
307 * Gets the Message type of the received Message |
|
308 */ |
|
309 int CSmsPlatformServiceS60Impl :: receivedMessageType(ServerConnectionBase& |
|
310 aServerConn) |
|
311 { |
|
312 JELOG2(EWMA); |
|
313 int messageType = KErrNone; |
|
314 if (0 == mMessageParameters) |
|
315 { |
|
316 mMessageParameters = new TJavaMessageParametersBuf(); |
|
317 } |
|
318 else |
|
319 { |
|
320 // Nothing to do |
|
321 } |
|
322 int error = aServerConn.retrieveMessage(*mMessageParameters); |
|
323 if (error != KErrNone) |
|
324 { |
|
325 ELOG1(EWMA, "WMA :: receivedMessageType error: %d", error); |
|
326 return error; |
|
327 } |
|
328 else |
|
329 { |
|
330 switch ((*mMessageParameters)().mEncoding) |
|
331 { |
|
332 case TSmsDataCodingScheme::ESmsAlphabet7Bit: |
|
333 case TSmsDataCodingScheme::ESmsAlphabetUCS2: |
|
334 case TSmsDataCodingScheme::ESmsAlphabetReserved: |
|
335 { |
|
336 messageType = MESSAGE_TEXT; |
|
337 break; |
|
338 } |
|
339 case TSmsDataCodingScheme::ESmsAlphabet8Bit: |
|
340 { |
|
341 messageType = MESSAGE_BINARY; |
|
342 break; |
|
343 } |
|
344 default: |
|
345 { |
|
346 messageType = KErrNotSupported; |
|
347 ELOG(EWMA,"WMA : Invalid Message Type"); |
|
348 break; |
|
349 } |
|
350 } |
|
351 } |
|
352 return messageType; |
|
353 } |
|
354 /** |
|
355 * Gets the Remote Host Address. |
|
356 */ |
|
357 jstring CSmsPlatformServiceS60Impl::getHostAddress(JNIEnv& aJni) |
|
358 { |
|
359 JELOG2(EWMA); |
|
360 TBuf<KMaxUriSize> Uri; |
|
361 const TDesC& address = (*mMessageParameters)().mAddress; |
|
362 TUint16 port = (*mMessageParameters)().mPort; |
|
363 if (mUri.find(L"cbs://:") == std::wstring::npos) |
|
364 { |
|
365 Uri.Append(KSmsProtocol); |
|
366 Uri.Append(KUriColonAndSlashes); |
|
367 Uri.Append(address); |
|
368 } |
|
369 else |
|
370 { |
|
371 Uri.Append(KCbsProtocol); |
|
372 Uri.Append(KUriColonAndSlashes); |
|
373 Uri.Append(KCBSEmptyAddress); |
|
374 } |
|
375 Uri.Append(':'); |
|
376 Uri.AppendNum(port); |
|
377 return aJni.NewString((jchar*)Uri.Ptr(),Uri.Length()); |
|
378 } |
|
379 /** |
|
380 * gets the received message as jobject to java side. |
|
381 */ |
|
382 jobject CSmsPlatformServiceS60Impl::getMessageData( |
|
383 JNIEnv& aJni,const int aMessageType) |
|
384 { |
|
385 JELOG2(EWMA); |
|
386 jobject javaSmsData = 0; |
|
387 if (aMessageType ==MESSAGE_TEXT) |
|
388 { |
|
389 javaSmsData = aJni.NewString( |
|
390 (jchar*)((*mMessageParameters)().mData).Ptr(), |
|
391 ((*mMessageParameters)().mData).Length()); |
|
392 } |
|
393 else if (aMessageType == MESSAGE_BINARY) |
|
394 { |
|
395 TInt length = ((*mMessageParameters)().mData).Length(); |
|
396 TBuf8<KSmsMaxDataSize> dataBuf; |
|
397 dataBuf.SetLength(length); |
|
398 dataBuf.Copy(((const TDesC16&)((*mMessageParameters)().mData))); |
|
399 jbyteArray javaSmsBinaryData = aJni.NewByteArray(dataBuf.Length()); |
|
400 if (javaSmsBinaryData) |
|
401 { |
|
402 TInt nativeBufferLength = dataBuf.Length(); |
|
403 TUint8* nativeBufferPtr = const_cast<TUint8*>(dataBuf.Ptr()); |
|
404 jbyte* jNativeBufferPtr = reinterpret_cast<jbyte*>( |
|
405 nativeBufferPtr); |
|
406 aJni.SetByteArrayRegion(javaSmsBinaryData, 0, |
|
407 nativeBufferLength,jNativeBufferPtr); |
|
408 javaSmsData = javaSmsBinaryData; |
|
409 } |
|
410 else |
|
411 { |
|
412 // Noting to do |
|
413 } |
|
414 } |
|
415 else |
|
416 { |
|
417 // Not a supported message type, It is already handled |
|
418 } |
|
419 return javaSmsData; |
|
420 } |
|
421 /** |
|
422 * Gets the Time stamp of the received message. |
|
423 */ |
|
424 jlong CSmsPlatformServiceS60Impl::getTimeStamp() |
|
425 { |
|
426 JELOG2(EWMA); |
|
427 if (mUri.find(L"cbs://:") != std::wstring::npos) |
|
428 { |
|
429 return 0; |
|
430 } |
|
431 else |
|
432 { |
|
433 return java::util::S60CommonUtils:: |
|
434 TTimeToJavaTime((*mMessageParameters)().mTimestamp); |
|
435 } |
|
436 |
|
437 } |
|
438 } //namespace wma |
|
439 } //namespace java |
|
440 |
|
441 |