|
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 |
|
19 #include "btdataelementlistpopulator.h" |
|
20 #include "bluetoothconsts.h" |
|
21 |
|
22 namespace java |
|
23 { |
|
24 namespace bluetooth |
|
25 { |
|
26 |
|
27 BTDataElementListPopulator* BTDataElementListPopulator::New(JNIEnv* aJni, |
|
28 MSdpElementBuilder* aParentBuilder, jobject aParentDataElement, |
|
29 bool aCanUnregister) |
|
30 { |
|
31 JELOG2(EJavaBluetooth); |
|
32 BTDataElementListPopulator* populator = new BTDataElementListPopulator(); |
|
33 populator->Construct(aJni, aParentBuilder, aParentDataElement, |
|
34 aCanUnregister); |
|
35 return populator; |
|
36 } |
|
37 |
|
38 BTDataElementListPopulator::BTDataElementListPopulator() |
|
39 { |
|
40 } |
|
41 |
|
42 void BTDataElementListPopulator::Construct(JNIEnv* aJni, |
|
43 MSdpElementBuilder* aParentBuilder, jobject aParentDataElement, |
|
44 bool aCanUnregister) |
|
45 { |
|
46 JELOG2(EJavaBluetooth); |
|
47 mJni = aJni; |
|
48 mParentDataElement = aParentDataElement; |
|
49 mParentBuilder = aParentBuilder; |
|
50 mDataElementListPopulator = NULL; |
|
51 mCanUnregister = aCanUnregister; |
|
52 mDataElementClass = mJni->GetObjectClass(aParentDataElement); |
|
53 mAddElementMethod = mJni->GetMethodID(mDataElementClass, "addElement", |
|
54 "(Ljavax/bluetooth/DataElement;)V"); |
|
55 } |
|
56 |
|
57 BTDataElementListPopulator::~BTDataElementListPopulator() |
|
58 { |
|
59 JELOG2(EJavaBluetooth); |
|
60 if (mDataElementListPopulator) |
|
61 { |
|
62 delete mDataElementListPopulator; |
|
63 mDataElementListPopulator = NULL; |
|
64 } |
|
65 mParentDataElement = NULL; |
|
66 mParentBuilder = NULL; |
|
67 mCanUnregister = false; |
|
68 mDataElementClass = NULL; |
|
69 mAddElementMethod = NULL; |
|
70 } |
|
71 |
|
72 MSdpElementBuilder* BTDataElementListPopulator::BuildDEAL() |
|
73 { |
|
74 JELOG2(EJavaBluetooth); |
|
75 jmethodID constructorID = mJni->GetMethodID(mDataElementClass, "<init>", |
|
76 "(I)V"); |
|
77 jobject dataElement = mJni->NewObject(mDataElementClass, constructorID, |
|
78 DataElement_DATALT); |
|
79 |
|
80 (*mJni).CallVoidMethod(mParentDataElement, mAddElementMethod, dataElement); |
|
81 mDataElementListPopulator = BTDataElementListPopulator::New(mJni, this, |
|
82 dataElement, true); |
|
83 |
|
84 // Further 'Build' commands should add elements to the new list |
|
85 return mDataElementListPopulator; |
|
86 } |
|
87 |
|
88 MSdpElementBuilder* BTDataElementListPopulator::BuildDESL() |
|
89 { |
|
90 JELOG2(EJavaBluetooth); |
|
91 jmethodID constructorID = mJni->GetMethodID(mDataElementClass, "<init>", |
|
92 "(I)V"); |
|
93 jobject dataElement = mJni->NewObject(mDataElementClass, constructorID, |
|
94 DataElement_DATSEQ); |
|
95 |
|
96 (*mJni).CallVoidMethod(mParentDataElement, mAddElementMethod, dataElement); |
|
97 mDataElementListPopulator = BTDataElementListPopulator::New(mJni, this, |
|
98 dataElement, true); |
|
99 |
|
100 // Further 'Build' commands should add elements to the new list |
|
101 return mDataElementListPopulator; |
|
102 } |
|
103 |
|
104 MSdpElementBuilder* BTDataElementListPopulator::StartListL() |
|
105 { |
|
106 JELOG2(EJavaBluetooth); |
|
107 return this; |
|
108 } |
|
109 |
|
110 MSdpElementBuilder* BTDataElementListPopulator::EndListL() |
|
111 { |
|
112 JELOG2(EJavaBluetooth); |
|
113 MSdpElementBuilder* parentBuilder = mParentBuilder; |
|
114 if (mCanUnregister == true) |
|
115 { |
|
116 // End of the current object. Execution continues with the parent builder |
|
117 return ((BTDataElementListPopulator*) |
|
118 mParentBuilder)->UnregisterDataElementListPopulator(); |
|
119 } |
|
120 return parentBuilder; |
|
121 } |
|
122 |
|
123 MSdpElementBuilder* BTDataElementListPopulator::BuildUintL(const TDesC8& aUint) |
|
124 { |
|
125 JELOG2(EJavaBluetooth); |
|
126 CSdpAttrValue* value = CSdpAttrValueUint::NewUintL(aUint); |
|
127 TUint dataSize = value->DataSize(); |
|
128 jobject dataElement; |
|
129 |
|
130 if (dataSize <= 4) |
|
131 { |
|
132 jmethodID constructorID = mJni->GetMethodID(mDataElementClass, |
|
133 "<init>", "(IJ)V"); |
|
134 jlong dataValue = (jlong) value->Uint(); |
|
135 |
|
136 if (dataSize == 1) |
|
137 { |
|
138 dataElement = mJni->NewObject(mDataElementClass, constructorID, |
|
139 DataElement_U_INT_1, dataValue); |
|
140 } |
|
141 else if (dataSize == 2) |
|
142 { |
|
143 dataElement = mJni->NewObject(mDataElementClass, constructorID, |
|
144 DataElement_U_INT_2, dataValue); |
|
145 } |
|
146 else // if (dataSize == 4) |
|
147 { |
|
148 dataElement = mJni->NewObject(mDataElementClass, constructorID, |
|
149 DataElement_U_INT_4, dataValue); |
|
150 } |
|
151 } |
|
152 else |
|
153 { |
|
154 jmethodID constructorID = mJni->GetMethodID(mDataElementClass, |
|
155 "<init>", "(ILjava/lang/Object;)V"); |
|
156 |
|
157 jbyte* bytes = (jbyte*) aUint.Ptr(); |
|
158 jbyteArray byteArray = mJni->NewByteArray(dataSize); |
|
159 mJni->SetByteArrayRegion(byteArray, 0, dataSize, bytes); |
|
160 |
|
161 if (dataSize == 8) |
|
162 { |
|
163 dataElement = mJni->NewObject(mDataElementClass, constructorID, |
|
164 DataElement_U_INT_8, byteArray); |
|
165 } |
|
166 else |
|
167 { |
|
168 dataElement = mJni->NewObject(mDataElementClass, constructorID, |
|
169 DataElement_U_INT_16, byteArray); |
|
170 } |
|
171 } |
|
172 |
|
173 delete value; |
|
174 value = NULL; |
|
175 |
|
176 (*mJni).CallVoidMethod(mParentDataElement, mAddElementMethod, dataElement); |
|
177 LOG(EJavaBluetooth, EInfo, "- BTDataElementListPopulator::BuildUintL"); |
|
178 return this; |
|
179 } |
|
180 |
|
181 MSdpElementBuilder* BTDataElementListPopulator::BuildIntL(const TDesC8& aInt) |
|
182 { |
|
183 JELOG2(EJavaBluetooth); |
|
184 CSdpAttrValue* value = CSdpAttrValueInt::NewIntL(aInt); |
|
185 TUint dataSize = value->DataSize(); |
|
186 jobject dataElement; |
|
187 |
|
188 if (dataSize < 8) |
|
189 { |
|
190 jmethodID constructorID = mJni->GetMethodID(mDataElementClass, |
|
191 "<init>", "(IJ)V"); |
|
192 jlong dataValue = (jlong) value->Int(); |
|
193 |
|
194 if (dataSize == 1) |
|
195 { |
|
196 dataElement = mJni->NewObject(mDataElementClass, constructorID, |
|
197 DataElement_INT_1, dataValue); |
|
198 } |
|
199 else if (dataSize == 2) |
|
200 { |
|
201 dataElement = mJni->NewObject(mDataElementClass, constructorID, |
|
202 DataElement_INT_2, dataValue); |
|
203 } |
|
204 else //if (dataSize == 4) |
|
205 { |
|
206 dataElement = mJni->NewObject(mDataElementClass, constructorID, |
|
207 DataElement_INT_4, dataValue); |
|
208 } |
|
209 } |
|
210 else if (dataSize == 8) |
|
211 { |
|
212 jmethodID constructorID = mJni->GetMethodID(mDataElementClass, |
|
213 "<init>", "(IJ)V"); |
|
214 const TUint8* ptr = value->Des().Ptr(); |
|
215 TUint highBytes = BigEndian::Get32(ptr); |
|
216 TUint lowBytes = BigEndian::Get32(ptr + sizeof(TUint)); |
|
217 TInt64 dataValue = MAKE_TINT64(highBytes, lowBytes); |
|
218 dataElement = mJni->NewObject(mDataElementClass, constructorID, |
|
219 DataElement_INT_8, dataValue); |
|
220 } |
|
221 else |
|
222 { |
|
223 jmethodID constructorID = mJni->GetMethodID(mDataElementClass, |
|
224 "<init>", "(ILjava/lang/Object;)V"); |
|
225 |
|
226 jbyte* bytes = (jbyte*) aInt.Ptr(); |
|
227 jbyteArray byteArray = mJni->NewByteArray(dataSize); |
|
228 mJni->SetByteArrayRegion(byteArray, 0, dataSize, bytes); |
|
229 |
|
230 dataElement = mJni->NewObject(mDataElementClass, constructorID, |
|
231 DataElement_INT_16, byteArray); |
|
232 } |
|
233 |
|
234 delete value; |
|
235 value = NULL; |
|
236 |
|
237 (*mJni).CallVoidMethod(mParentDataElement, mAddElementMethod, dataElement); |
|
238 LOG(EJavaBluetooth, EInfo, "- BTDataElementListPopulator::BuildIntL"); |
|
239 return this; |
|
240 } |
|
241 |
|
242 MSdpElementBuilder* BTDataElementListPopulator::BuildNilL() |
|
243 { |
|
244 JELOG2(EJavaBluetooth); |
|
245 jmethodID constructorID = mJni->GetMethodID(mDataElementClass, "<init>", |
|
246 "(I)V"); |
|
247 jobject dataElement = mJni->NewObject(mDataElementClass, constructorID, |
|
248 DataElement_NULL); |
|
249 |
|
250 (*mJni).CallVoidMethod(mParentDataElement, mAddElementMethod, dataElement); |
|
251 return this; |
|
252 } |
|
253 |
|
254 MSdpElementBuilder* BTDataElementListPopulator::BuildBooleanL(TBool aBool) |
|
255 { |
|
256 JELOG2(EJavaBluetooth); |
|
257 jmethodID constructorID = mJni->GetMethodID(mDataElementClass, "<init>", |
|
258 "(Z)V"); |
|
259 jobject dataElement = mJni->NewObject(mDataElementClass, constructorID, |
|
260 aBool); |
|
261 |
|
262 (*mJni).CallVoidMethod(mParentDataElement, mAddElementMethod, dataElement); |
|
263 LOG1(EJavaBluetooth, EInfo, |
|
264 "- BTDataElementListPopulator::BuildBooleanL %d", (int) aBool); |
|
265 return this; |
|
266 } |
|
267 |
|
268 MSdpElementBuilder* BTDataElementListPopulator::BuildStringL( |
|
269 const TDesC8& aString) |
|
270 { |
|
271 JELOG2(EJavaBluetooth); |
|
272 HBufC8* bufString = HBufC8::New(aString.Length()+1); |
|
273 TPtr8 ptr = bufString->Des(); |
|
274 |
|
275 ptr.Copy(aString); |
|
276 jstring dataValue = mJni->NewStringUTF((char *) ptr.PtrZ()); |
|
277 delete bufString; |
|
278 |
|
279 jmethodID constructorID = mJni->GetMethodID(mDataElementClass, "<init>", |
|
280 "(ILjava/lang/Object;)V"); |
|
281 jobject dataElement = mJni->NewObject(mDataElementClass, constructorID, |
|
282 DataElement_STRING, dataValue); |
|
283 |
|
284 (*mJni).CallVoidMethod(mParentDataElement, mAddElementMethod, dataElement); |
|
285 LOG1(EJavaBluetooth, EInfo, |
|
286 "- BTDataElementListPopulator::BuildStringL %S", |
|
287 (char *) aString.Ptr()); |
|
288 return this; |
|
289 } |
|
290 |
|
291 MSdpElementBuilder* BTDataElementListPopulator::BuildURLL(const TDesC8& aURL) |
|
292 { |
|
293 JELOG2(EJavaBluetooth); |
|
294 HBufC8* bufString = HBufC8::New(aURL.Length()+1); |
|
295 TPtr8 ptr = bufString->Des(); |
|
296 |
|
297 ptr.Copy(aURL); |
|
298 jstring dataValue = mJni->NewStringUTF((char *) ptr.PtrZ()); |
|
299 delete bufString; |
|
300 jmethodID constructorID = mJni->GetMethodID(mDataElementClass, "<init>", |
|
301 "(ILjava/lang/Object;)V"); |
|
302 jobject dataElement = mJni->NewObject(mDataElementClass, constructorID, |
|
303 DataElement_URL, dataValue); |
|
304 |
|
305 (*mJni).CallVoidMethod(mParentDataElement, mAddElementMethod, dataElement); |
|
306 LOG1(EJavaBluetooth, EInfo, "- BTDataElementListPopulator::BuildURLL %S", |
|
307 (char *) aURL.Ptr()); |
|
308 return this; |
|
309 } |
|
310 |
|
311 MSdpElementBuilder* BTDataElementListPopulator::BuildUUIDL(const TUUID& aUUID) |
|
312 { |
|
313 JELOG2(EJavaBluetooth); |
|
314 jobject dataValue = TUUIDtoJUUID(aUUID); |
|
315 jmethodID constructorID = mJni->GetMethodID(mDataElementClass, "<init>", |
|
316 "(ILjava/lang/Object;)V"); |
|
317 jobject dataElement = mJni->NewObject(mDataElementClass, constructorID, |
|
318 DataElement_UUID, dataValue); |
|
319 |
|
320 (*mJni).CallVoidMethod(mParentDataElement, mAddElementMethod, dataElement); |
|
321 return this; |
|
322 } |
|
323 |
|
324 MSdpElementBuilder* BTDataElementListPopulator::BuildUnknownL(TUint8 /*aType*/, |
|
325 TUint8 /*aSizeDesc*/, const TDesC8& /*aData*/) |
|
326 { |
|
327 JELOG2(EJavaBluetooth); |
|
328 // Ignore if any unknown type of data exist !! |
|
329 return this; |
|
330 } |
|
331 |
|
332 MSdpElementBuilder* |
|
333 BTDataElementListPopulator::UnregisterDataElementListPopulator() |
|
334 { |
|
335 JELOG2(EJavaBluetooth); |
|
336 if (mDataElementListPopulator) |
|
337 { |
|
338 delete mDataElementListPopulator; |
|
339 mDataElementListPopulator = NULL; |
|
340 } |
|
341 return this; |
|
342 } |
|
343 |
|
344 char BTDataElementListPopulator::NibbleToHexChar(TUint8 aByte) |
|
345 { |
|
346 JELOG2(EJavaBluetooth); |
|
347 char hexChars[] = "0123456789abcdef"; |
|
348 return hexChars[aByte]; |
|
349 } |
|
350 |
|
351 jobject BTDataElementListPopulator::TUUIDtoJUUID(const TUUID& aUUID) |
|
352 { |
|
353 JELOG2(EJavaBluetooth); |
|
354 TPtrC8 ptrUUID = aUUID.Des(); |
|
355 int uuidLen = (int) ptrUUID.Length(); |
|
356 char *strUUIDBuff = new char[(uuidLen * 2) + 1]; |
|
357 int strIndex = 0; |
|
358 int ptrIndex = 0; |
|
359 |
|
360 for (ptrIndex = 0; ptrIndex < uuidLen; ptrIndex++) |
|
361 { |
|
362 strUUIDBuff[strIndex++] = NibbleToHexChar((ptrUUID[ptrIndex] >> 4) |
|
363 & 0xf); |
|
364 strUUIDBuff[strIndex++] = NibbleToHexChar(ptrUUID[ptrIndex] & 0xf); |
|
365 } |
|
366 strUUIDBuff[strIndex] = 0; |
|
367 |
|
368 jstring uuidString = mJni->NewStringUTF(strUUIDBuff); |
|
369 jclass uuidClass = mJni->FindClass("javax/bluetooth/UUID"); |
|
370 jmethodID constructorID = mJni->GetMethodID(uuidClass, "<init>", |
|
371 "(Ljava/lang/String;Z)V"); |
|
372 |
|
373 bool isShortUUID = (uuidLen > 4) ? false : true; |
|
374 |
|
375 LOG1(EJavaBluetooth, EInfo, |
|
376 "- BTDataElementListPopulator::TUUIDtoJUUID %s", strUUIDBuff); |
|
377 delete[] strUUIDBuff; |
|
378 strUUIDBuff = NULL; |
|
379 return mJni->NewObject(uuidClass, constructorID, uuidString, isShortUUID); |
|
380 } |
|
381 |
|
382 } //end namespace bluetooth |
|
383 } //end namespace java |