|
1 /* |
|
2 * Copyright (c) 2009 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 #define _EXPORT_HTI_COMMON_ |
|
17 |
|
18 #include "..\libs\stdsoap2.h" |
|
19 #include "hticommon.h" |
|
20 #include "HtiSoapHandlerInterface.h" |
|
21 |
|
22 //********************************************************************************** |
|
23 // General util functions |
|
24 // |
|
25 //********************************************************************************** |
|
26 |
|
27 //********************************************************************************** |
|
28 // check_mandatory_string_parameter |
|
29 //********************************************************************************** |
|
30 EXPORT_FROM_DLL int check_mandatory_string_parameter(struct soap* soap, |
|
31 char* parameter, |
|
32 char* parameterName) |
|
33 { |
|
34 int result = SOAP_OK; |
|
35 |
|
36 if(!parameter) |
|
37 { |
|
38 char msg[] = "Mandatory parameter \"%s\" missing"; |
|
39 int msgLen = _scprintf(msg, parameterName)+1; //+1 for nul char |
|
40 char* buf = new char[msgLen]; |
|
41 sprintf(buf, msg, parameterName); |
|
42 soap->error = soap_receiver_fault(soap, "HtiGateway", buf); |
|
43 delete buf; |
|
44 result = SOAP_FAULT; |
|
45 } |
|
46 else if(strlen(parameter) == 0) |
|
47 { |
|
48 char msg[] = "Mandatory parameter \"%s\" empty"; |
|
49 int msgLen = _scprintf(msg, parameterName)+1; //+1 for nul char |
|
50 char* buf = new char[msgLen]; |
|
51 sprintf(buf, msg, parameterName); |
|
52 soap->error = soap_receiver_fault(soap, "HtiGateway", buf); |
|
53 delete buf; |
|
54 result = SOAP_FAULT; |
|
55 } |
|
56 |
|
57 return result; |
|
58 } |
|
59 |
|
60 //********************************************************************************** |
|
61 // check_mandatory_wcstring_parameter |
|
62 //********************************************************************************** |
|
63 EXPORT_FROM_DLL int check_mandatory_wcstring_parameter(struct soap* soap, |
|
64 wchar_t* parameter, |
|
65 char* parameterName) |
|
66 { |
|
67 int result = SOAP_OK; |
|
68 |
|
69 if ( !parameter ) |
|
70 { |
|
71 char msg[] = "Mandatory parameter \"%s\" missing"; |
|
72 int msgLen = _scprintf(msg, parameterName)+1; //+1 for nul char |
|
73 char* buf = new char[msgLen]; |
|
74 sprintf(buf, msg, parameterName); |
|
75 soap->error = soap_receiver_fault(soap, "HtiGateway", buf); |
|
76 delete buf; |
|
77 result = SOAP_FAULT; |
|
78 } |
|
79 else if ( wcslen(parameter) == 0 ) |
|
80 { |
|
81 char msg[] = "Mandatory parameter \"%s\" empty"; |
|
82 int msgLen = _scprintf(msg, parameterName)+1; //+1 for nul char |
|
83 char* buf = new char[msgLen]; |
|
84 sprintf(buf, msg, parameterName); |
|
85 soap->error = soap_receiver_fault(soap, "HtiGateway", buf); |
|
86 delete buf; |
|
87 result = SOAP_FAULT; |
|
88 } |
|
89 |
|
90 return result; |
|
91 } |
|
92 |
|
93 //********************************************************************************** |
|
94 // dump |
|
95 //********************************************************************************** |
|
96 EXPORT_FROM_DLL void dump(BYTE *data, int dataLen) |
|
97 { |
|
98 printf("\nDUMP len %d:\n", dataLen); |
|
99 |
|
100 int hex_index = 0; |
|
101 int char_index = 0; |
|
102 |
|
103 char hex_buf[48+1]; |
|
104 char char_buf[16+1]; |
|
105 hex_buf[0] = 0x0; |
|
106 char_buf[0] = 0x0; |
|
107 |
|
108 int j = 0; |
|
109 for(int i=0; i<dataLen; i++) |
|
110 { |
|
111 char tmp[3]; |
|
112 sprintf(tmp, "%02x", data[i]); |
|
113 strcat(hex_buf, tmp); |
|
114 |
|
115 if( (data[i] != 0x0a) && (data[i] != 0x0d) && (data[i] != 0x08)) |
|
116 char_buf[j] = data[i]; |
|
117 else |
|
118 char_buf[j] = '.'; |
|
119 |
|
120 if( i == dataLen-1 ) |
|
121 { |
|
122 char_buf[j+1] = 0x0; |
|
123 printf("%-45s\t\t%s\n", hex_buf, char_buf); |
|
124 break; |
|
125 } |
|
126 |
|
127 if( !(j%15) && (j!=0) ) |
|
128 { |
|
129 char_buf[j+1] = 0x0; |
|
130 printf("%-45s\t\t%s\n", hex_buf, char_buf); |
|
131 j = 0; |
|
132 hex_buf[0] = 0x0; |
|
133 continue; |
|
134 } |
|
135 else |
|
136 strcat(hex_buf, " "); |
|
137 |
|
138 j++; |
|
139 } |
|
140 } |
|
141 |
|
142 //********************************************************************************** |
|
143 // soap_receiver_fault_format |
|
144 //********************************************************************************** |
|
145 EXPORT_FROM_DLL int soap_receiver_fault_format(struct soap *soap, |
|
146 const char *faultstring, |
|
147 const char *faultdetail, ...) |
|
148 { |
|
149 va_list marker; |
|
150 va_start( marker, faultdetail ); |
|
151 |
|
152 char* tmp = new char[strlen(faultdetail)+1]; |
|
153 memcpy(tmp, faultdetail, strlen(faultdetail)+1); |
|
154 |
|
155 char* token = strtok(tmp, "%"); |
|
156 if(token == NULL) |
|
157 { |
|
158 // no parameters |
|
159 delete tmp; |
|
160 return soap_receiver_fault(soap, faultstring, faultdetail); |
|
161 } |
|
162 |
|
163 char* text = (char*) malloc( strlen(token) + 1); |
|
164 strcpy(text, token); |
|
165 |
|
166 token = strtok(NULL, "%"); |
|
167 while( token != NULL ) |
|
168 { |
|
169 char* parse = NULL; |
|
170 |
|
171 if(token[0] == 'd') |
|
172 { |
|
173 int var = va_arg( marker, int ); |
|
174 parse = new char[ _scprintf("%d", var) + 1 ]; |
|
175 sprintf( parse, "%d", var ); |
|
176 } |
|
177 else if(token[0] == 'x') |
|
178 { |
|
179 int var = va_arg( marker, int ); |
|
180 parse = new char[ _scprintf("%x", var) + 1 ]; |
|
181 sprintf( parse, "%x", var ); |
|
182 } |
|
183 else if(token[0] == 's' ) |
|
184 { |
|
185 char* var = va_arg( marker, char* ); |
|
186 parse = new char[ _scprintf("%s", var) + 1 ]; |
|
187 sprintf( parse, "%s", var ); |
|
188 } |
|
189 |
|
190 // add parsed part |
|
191 if(parse) |
|
192 { |
|
193 text = (char*) realloc( text, strlen(text) + strlen(parse) + 1 ); |
|
194 strcat(text, parse); |
|
195 delete parse; |
|
196 } |
|
197 |
|
198 // add remaining part |
|
199 text = (char*) realloc( text, strlen(text) + strlen(token+1) + 1 ); |
|
200 strcat(text, token+1); |
|
201 |
|
202 token = strtok(NULL, "%"); |
|
203 } |
|
204 va_end( marker ); |
|
205 delete tmp; |
|
206 |
|
207 int res = soap_receiver_fault(soap, faultstring, text); |
|
208 free (text); |
|
209 return res; |
|
210 } |
|
211 |
|
212 //********************************************************************************** |
|
213 // HtiMsgHelper class implementation |
|
214 // |
|
215 //********************************************************************************** |
|
216 //********************************************************************************** |
|
217 // HtiMsgHelper::HtiMsgHelper |
|
218 //********************************************************************************** |
|
219 EXPORT_FROM_DLL HtiMsgHelper::HtiMsgHelper(struct soap *soap) |
|
220 { |
|
221 m_msgBody = NULL; |
|
222 m_msgBodyLen = 0; |
|
223 m_soap = soap; |
|
224 m_serviceId = 0; |
|
225 } |
|
226 |
|
227 //********************************************************************************** |
|
228 // HtiMsgHelper::HtiMsgHelper |
|
229 //********************************************************************************** |
|
230 EXPORT_FROM_DLL HtiMsgHelper::HtiMsgHelper(struct soap *soap, |
|
231 DWORD serviceId) |
|
232 { |
|
233 m_msgBody = NULL; |
|
234 m_msgBodyLen = 0; |
|
235 m_soap = soap; |
|
236 m_serviceId = serviceId; |
|
237 } |
|
238 |
|
239 //********************************************************************************** |
|
240 // HtiMsgHelper::HtiMsgHelper |
|
241 //********************************************************************************** |
|
242 EXPORT_FROM_DLL HtiMsgHelper::HtiMsgHelper(struct soap *soap, |
|
243 DWORD serviceId, |
|
244 BYTE command) |
|
245 { |
|
246 m_msgBody = new BYTE[1]; |
|
247 m_msgBody[0] = command; |
|
248 m_msgBodyLen = 1; |
|
249 m_soap = soap; |
|
250 m_serviceId = serviceId; |
|
251 } |
|
252 |
|
253 //********************************************************************************** |
|
254 // HtiMsgHelper::~HtiMsgHelper |
|
255 //********************************************************************************** |
|
256 EXPORT_FROM_DLL HtiMsgHelper::~HtiMsgHelper() |
|
257 { |
|
258 // m_msgBody is not deleted as we dont have ownership if |
|
259 // it is a received message |
|
260 } |
|
261 |
|
262 //********************************************************************************** |
|
263 // HtiMsgHelper::IncBodySize |
|
264 //********************************************************************************** |
|
265 void HtiMsgHelper::IncBodySize(int size) |
|
266 { |
|
267 BYTE *newMsgBody = new BYTE[m_msgBodyLen+size]; |
|
268 memcpy(newMsgBody, m_msgBody, m_msgBodyLen); |
|
269 delete m_msgBody; |
|
270 m_msgBody = newMsgBody; |
|
271 m_msgBodyLen += size; |
|
272 } |
|
273 |
|
274 //********************************************************************************** |
|
275 // HtiMsgHelper::CheckMsgExactLen |
|
276 //********************************************************************************** |
|
277 EXPORT_FROM_DLL int HtiMsgHelper::CheckMsgExactLen( int len ) |
|
278 { |
|
279 if( GetMsgLen() != len ) |
|
280 { |
|
281 m_soap->error = soap_receiver_fault_format( m_soap, "HtiError", |
|
282 "Incorrect HTI msg length. Expecting %d received %d", len, GetMsgLen()); |
|
283 return SOAP_FAULT; |
|
284 } |
|
285 return SOAP_OK; |
|
286 } |
|
287 |
|
288 //********************************************************************************** |
|
289 // HtiMsgHelper::CheckMsgMinLen |
|
290 //********************************************************************************** |
|
291 EXPORT_FROM_DLL int HtiMsgHelper::CheckMsgMinLen( int len ) |
|
292 { |
|
293 if( GetMsgLen() < len ) |
|
294 { |
|
295 m_soap->error = soap_receiver_fault( |
|
296 m_soap, "HtiError", "HTI msg too small"); |
|
297 return SOAP_FAULT; |
|
298 } |
|
299 return SOAP_OK; |
|
300 } |
|
301 |
|
302 //********************************************************************************** |
|
303 // HtiAudioMsg::CheckCommandCode |
|
304 //********************************************************************************** |
|
305 EXPORT_FROM_DLL int HtiMsgHelper::CheckCommandCode(BYTE code) |
|
306 { |
|
307 if(GetByte(0) != code) |
|
308 { |
|
309 m_soap->error = soap_receiver_fault_format(m_soap, "HtiError", |
|
310 "incorrect response CommandCode (0x%x)", GetByte(0)); |
|
311 return SOAP_FAULT; |
|
312 } |
|
313 return SOAP_OK; |
|
314 } |
|
315 |
|
316 //********************************************************************************** |
|
317 // HtiMsgHelper::AddByte |
|
318 //********************************************************************************** |
|
319 EXPORT_FROM_DLL void HtiMsgHelper::AddByte( BYTE byte ) |
|
320 { |
|
321 int writePos = m_msgBodyLen; |
|
322 IncBodySize( 1 ); |
|
323 m_msgBody[writePos] = byte; |
|
324 } |
|
325 |
|
326 //********************************************************************************** |
|
327 // HtiMsgHelper::AddWord |
|
328 //********************************************************************************** |
|
329 EXPORT_FROM_DLL void HtiMsgHelper::AddWord( WORD data ) |
|
330 { |
|
331 int writePos = m_msgBodyLen; |
|
332 IncBodySize( 2 ); |
|
333 *(WORD*)(m_msgBody+writePos) = data; |
|
334 } |
|
335 |
|
336 //********************************************************************************** |
|
337 // HtiMsgHelper::AddDWord |
|
338 //********************************************************************************** |
|
339 EXPORT_FROM_DLL void HtiMsgHelper::AddDWord( DWORD data ) |
|
340 { |
|
341 int writePos = m_msgBodyLen; |
|
342 IncBodySize( 4 ); |
|
343 *(DWORD*)(m_msgBody+writePos) = data; |
|
344 } |
|
345 |
|
346 //********************************************************************************** |
|
347 // HtiMsgHelper::AddInt |
|
348 //********************************************************************************** |
|
349 EXPORT_FROM_DLL void HtiMsgHelper::AddInt( int data ) |
|
350 { |
|
351 int writePos = m_msgBodyLen; |
|
352 IncBodySize( 4 ); |
|
353 *(int*)(m_msgBody+writePos) = data; |
|
354 } |
|
355 |
|
356 //********************************************************************************** |
|
357 // HtiMsgHelper::AddUInt64 |
|
358 //********************************************************************************** |
|
359 EXPORT_FROM_DLL void HtiMsgHelper::AddUInt64( unsigned __int64 data ) |
|
360 { |
|
361 int writePos = m_msgBodyLen; |
|
362 IncBodySize( 8 ); |
|
363 *(unsigned __int64*)(m_msgBody+writePos) = data; |
|
364 } |
|
365 |
|
366 //********************************************************************************** |
|
367 // HtiMsgHelper::AddString |
|
368 //********************************************************************************** |
|
369 EXPORT_FROM_DLL void HtiMsgHelper::AddString( char* string ) |
|
370 { |
|
371 int stringLen = string ? (int) strlen( string ) : 0; |
|
372 if( stringLen == 0 ) |
|
373 return; |
|
374 |
|
375 int writePos = m_msgBodyLen; |
|
376 IncBodySize( stringLen ); |
|
377 memcpy(m_msgBody+writePos, string, stringLen); |
|
378 } |
|
379 |
|
380 //********************************************************************************** |
|
381 // HtiMsgHelper::AddStringWithLengthByte |
|
382 //********************************************************************************** |
|
383 EXPORT_FROM_DLL void HtiMsgHelper::AddStringWithLengthByte( char* string ) |
|
384 { |
|
385 int stringLen = string ? (int) strlen( string ) : 0; |
|
386 if( stringLen == 0 ) |
|
387 return; |
|
388 |
|
389 AddByte( stringLen ); |
|
390 AddString( string ); |
|
391 } |
|
392 |
|
393 //********************************************************************************** |
|
394 // HtiMsgHelper::AddStringWithLengthByteZero |
|
395 //********************************************************************************** |
|
396 EXPORT_FROM_DLL void HtiMsgHelper::AddStringWithLengthByteZero( char* string ) |
|
397 { |
|
398 int stringLen = string ? (int) strlen( string ) : 0; |
|
399 |
|
400 AddByte( stringLen ); |
|
401 |
|
402 if( stringLen == 0 ) |
|
403 return; |
|
404 |
|
405 AddString( string ); |
|
406 } |
|
407 |
|
408 //********************************************************************************** |
|
409 // HtiMsgHelper::AddWCString |
|
410 //********************************************************************************** |
|
411 EXPORT_FROM_DLL void HtiMsgHelper::AddWCString(wchar_t* string) |
|
412 { |
|
413 int stringLen = string ? (int) wcslen( string )*2 : 0; |
|
414 if( stringLen == 0 ) |
|
415 return; |
|
416 |
|
417 int writePos = m_msgBodyLen; |
|
418 IncBodySize( stringLen ); |
|
419 memcpy( m_msgBody+writePos, string, stringLen ); |
|
420 } |
|
421 |
|
422 //********************************************************************************** |
|
423 // HtiMsgHelper::AddWCStringWithLengthByte |
|
424 //********************************************************************************** |
|
425 EXPORT_FROM_DLL void HtiMsgHelper::AddWCStringWithLengthByte( wchar_t* string ) |
|
426 { |
|
427 int stringLen = string ? (int) wcslen( string ) : 0; |
|
428 if( stringLen == 0 ) |
|
429 return; |
|
430 |
|
431 AddByte( stringLen ); |
|
432 AddWCString( string ); |
|
433 } |
|
434 //********************************************************************************** |
|
435 // HtiMsgHelper::AddData |
|
436 //********************************************************************************** |
|
437 EXPORT_FROM_DLL void HtiMsgHelper::AddData( void* data, int dataLen ) |
|
438 { |
|
439 int writePos = m_msgBodyLen; |
|
440 IncBodySize( dataLen ); |
|
441 memcpy( m_msgBody+writePos, data, dataLen ); |
|
442 } |
|
443 |
|
444 //********************************************************************************** |
|
445 // HtiMsgHelper::SendMsg |
|
446 //********************************************************************************** |
|
447 EXPORT_FROM_DLL void HtiMsgHelper::SendMsg() |
|
448 { |
|
449 // Send the message to symbian side |
|
450 HtiSoapHandlerInterface* handler = |
|
451 static_cast<HtiSoapHandlerInterface*>(m_soap->user); |
|
452 handler->SendHtiMessage(m_serviceId, m_msgBody, m_msgBodyLen); |
|
453 |
|
454 // destroy the sent message |
|
455 delete m_msgBody; |
|
456 m_msgBody = NULL; |
|
457 m_msgBodyLen = 0; |
|
458 } |
|
459 |
|
460 //********************************************************************************** |
|
461 // HtiMsgHelper::ReceiveMsg |
|
462 //********************************************************************************** |
|
463 EXPORT_FROM_DLL int HtiMsgHelper::ReceiveMsg( int timeout ) |
|
464 { |
|
465 // Clean these up for received HTI message |
|
466 if( m_msgBody ) |
|
467 { |
|
468 delete m_msgBody; |
|
469 m_msgBody = NULL; |
|
470 m_msgBodyLen = 0; |
|
471 } |
|
472 |
|
473 HtiSoapHandlerInterface* handler = |
|
474 static_cast<HtiSoapHandlerInterface*>(m_soap->user); |
|
475 |
|
476 // Wait for OK or error msg |
|
477 if (handler->WaitForHtiMessage(timeout)) |
|
478 { |
|
479 if ( !handler->IsReceivedHtiError() ) |
|
480 { |
|
481 // Get received message |
|
482 // (handler has ownership of the message body) |
|
483 m_msgBody = (BYTE*) handler->ReceivedHtiMessageBody(); |
|
484 m_msgBodyLen = handler->ReceivedHtiMessageBodySize(); |
|
485 return SOAP_OK; |
|
486 } |
|
487 else |
|
488 { |
|
489 // Fill the error description |
|
490 handler->SendSoapFaultFromReceivedHtiError(); |
|
491 return SOAP_FAULT; |
|
492 } |
|
493 } |
|
494 // ...or timeout |
|
495 else |
|
496 { |
|
497 m_soap->error = soap_receiver_fault( |
|
498 m_soap, "HtiGateway", "No response from symbian side"); |
|
499 return SOAP_FAULT; |
|
500 } |
|
501 } |
|
502 |
|
503 //********************************************************************************** |
|
504 // HtiMsgHelper::SendReceiveMsg |
|
505 //********************************************************************************** |
|
506 EXPORT_FROM_DLL int HtiMsgHelper::SendReceiveMsg( int timeout ) |
|
507 { |
|
508 SendMsg(); |
|
509 return ReceiveMsg( timeout ); |
|
510 } |
|
511 |
|
512 //********************************************************************************** |
|
513 // HtiMsgHelper::GetSoapString |
|
514 //********************************************************************************** |
|
515 EXPORT_FROM_DLL char* HtiMsgHelper::GetSoapString( int offset, int len ) |
|
516 { |
|
517 char* string = (char*) soap_malloc( m_soap, len+1 );//+1 for the last null char |
|
518 if ( len > 0 ) |
|
519 memcpy( string, m_msgBody + offset, len ); |
|
520 string[len] = 0x00; //add null char |
|
521 return string; |
|
522 } |
|
523 |
|
524 //********************************************************************************** |
|
525 // HtiMsgHelper::GetSoapWCString |
|
526 //********************************************************************************** |
|
527 EXPORT_FROM_DLL wchar_t* HtiMsgHelper::GetSoapWCString( int offset, int len ) |
|
528 { |
|
529 wchar_t* string = (wchar_t*) soap_malloc( m_soap, len*2+2 );//+2 for the last null char |
|
530 if ( len > 0 ) |
|
531 memcpy( string, m_msgBody + offset, len*2 ); |
|
532 string[len] = 0x0000; //add null char |
|
533 return string; |
|
534 } |
|
535 |
|
536 //********************************************************************************** |
|
537 // HtiMsgHelper::AddStringWithLengthWordZero |
|
538 //********************************************************************************** |
|
539 EXPORT_FROM_DLL void HtiMsgHelper::AddStringWithLengthWordZero( char* string ) |
|
540 { |
|
541 int stringLen = string ? (int) strlen( string ) : 0; |
|
542 |
|
543 AddWord( stringLen ); |
|
544 |
|
545 if( stringLen == 0 ) |
|
546 return; |
|
547 |
|
548 AddString( string ); |
|
549 } |
|
550 |
|
551 //********************************************************************************** |
|
552 // HtiMsgHelper::AddWCStringWithLengthByteZero |
|
553 //********************************************************************************** |
|
554 EXPORT_FROM_DLL void HtiMsgHelper::AddWCStringWithLengthByteZero( wchar_t* string ) |
|
555 { |
|
556 int stringLen = string ? (int) wcslen( string ) : 0; |
|
557 |
|
558 AddByte( stringLen ); |
|
559 |
|
560 if( stringLen == 0 ) |
|
561 return; |
|
562 |
|
563 AddWCString( string ); |
|
564 } |