hti/PC_Tools/HTIGateway/ServicePlugins/HtiAudio/HtiAudio.cpp
branchRCL_3
changeset 59 8ad140f3dd41
parent 0 a03f92240627
equal deleted inserted replaced
49:7fdc9a71d314 59:8ad140f3dd41
       
     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 #include "HtiAudioH.h"
       
    17 #include "HtiPlugin.h"
       
    18 #include "HtiSoapHandlerInterface.h"
       
    19 
       
    20 #include <iosfwd>
       
    21 #include <sstream>
       
    22 
       
    23 // Command codes
       
    24 const unsigned char CMD_LIST_AUDIOFILES	= 0x01;
       
    25 const unsigned char CMD_PLAY_FILE		= 0x02;
       
    26 const unsigned char CMD_PLAY_TONE		= 0x03;
       
    27 const unsigned char CMD_PLAY_DTMF		= 0x04;
       
    28 const unsigned char CMD_STOP			= 0x05;
       
    29 const unsigned char CMD_GET_DURATION	= 0x06;
       
    30 const unsigned char CMD_GET_MAXVOLUME	= 0x07;
       
    31 const unsigned char CMD_SET_VOLUME		= 0x08;
       
    32 
       
    33 // enums
       
    34 enum PlayState {
       
    35 	EStopped,
       
    36 	EPlaying,
       
    37 	EError
       
    38 };
       
    39 
       
    40 enum PlayCommand {
       
    41 	EPlayAudioFile = 0,
       
    42 	EPlayTone,
       
    43 	EPlayDtmf,
       
    44 	ENrOfPlayCommands
       
    45 };
       
    46 
       
    47 // global variable(s)
       
    48 int g_PlayCommandState[ENrOfPlayCommands] = { EStopped, EStopped, EStopped };
       
    49 
       
    50 
       
    51 // predeclarations
       
    52 bool handle_async_msg(HtiSoapHandlerInterface* handler);
       
    53 void SetSoapFaultFromReceivedHtiAudioError(struct soap *soap,
       
    54 										   BYTE *receivedMsgBody,
       
    55 										   int receivedMsgBodyLen);
       
    56 
       
    57 //**********************************************************************************
       
    58 // HELPER CLASSES
       
    59 //
       
    60 //**********************************************************************************
       
    61 
       
    62 //**********************************************************************************
       
    63 // CLASS HtiAudioMsg
       
    64 //**********************************************************************************
       
    65 class HtiAudioMsg : public HtiMsgHelper
       
    66 {
       
    67 public:
       
    68 	HtiAudioMsg( struct soap *soap );
       
    69 	HtiAudioMsg( struct soap *soap, DWORD serviceId, BYTE command );
       
    70 	int ReceiveMsgWithAsyncHandler( int timeout );
       
    71 	int SendReceiveMsgWithAsyncHandler( int timeout );
       
    72 
       
    73 public: // from HtiMsgHelper
       
    74 	// We need to use SetSoapFaultFromReceivedHtiAudioError handler
       
    75 	int ReceiveMsg( int timeout );
       
    76 };
       
    77 
       
    78 //**********************************************************************************
       
    79 // HtiAudioMsg::HtiAudioMsg
       
    80 //**********************************************************************************
       
    81 HtiAudioMsg::HtiAudioMsg( struct soap *soap ) : HtiMsgHelper( soap )
       
    82 {
       
    83 }
       
    84 
       
    85 //**********************************************************************************
       
    86 // HtiAudioMsg::HtiAudioMsg
       
    87 //**********************************************************************************
       
    88 HtiAudioMsg::HtiAudioMsg( struct soap *soap, DWORD serviceId, BYTE command )
       
    89                           : HtiMsgHelper( soap, serviceId, command )
       
    90 {
       
    91 }
       
    92 
       
    93 //**********************************************************************************
       
    94 // HtiAudioMsg::ReceiveMsg
       
    95 //**********************************************************************************
       
    96 int HtiAudioMsg::ReceiveMsg( int timeout )
       
    97 {
       
    98 	// Clean these up for received HTI message
       
    99 	if(m_msgBody)
       
   100 	{
       
   101 		delete m_msgBody;
       
   102 		m_msgBody = NULL;
       
   103 		m_msgBodyLen = 0;
       
   104 	}
       
   105 
       
   106 	HtiSoapHandlerInterface* handler =
       
   107 		static_cast<HtiSoapHandlerInterface*>(m_soap->user);
       
   108 
       
   109 	// Wait for OK or error msg
       
   110 	if (handler->WaitForHtiMessage(timeout))
       
   111 	{
       
   112 		// (handler has ownership of the message body)
       
   113 		m_msgBody = (BYTE*) handler->ReceivedHtiMessageBody();
       
   114 		m_msgBodyLen = handler->ReceivedHtiMessageBodySize();
       
   115 
       
   116 		if ( !handler->IsReceivedHtiError() )
       
   117 		{
       
   118 			// Get received message
       
   119 			return SOAP_OK;
       
   120 		}
       
   121 		else
       
   122 		{
       
   123 			/// Fill the error description
       
   124 			SetSoapFaultFromReceivedHtiAudioError(
       
   125 					m_soap, m_msgBody, m_msgBodyLen);
       
   126 				return SOAP_FAULT;
       
   127 		}
       
   128 	}
       
   129 	// ...or timeout
       
   130 	else
       
   131 	{
       
   132 		m_soap->error = soap_receiver_fault(m_soap,
       
   133 			"HtiGateway", "No response from symbian side");
       
   134 		return SOAP_FAULT;
       
   135 	}
       
   136 }
       
   137 
       
   138 //**********************************************************************************
       
   139 // HtiAudioMsg::SendReceiveMsg
       
   140 //**********************************************************************************
       
   141 int HtiAudioMsg::SendReceiveMsgWithAsyncHandler( int timeout )
       
   142 {
       
   143 	SendMsg();
       
   144 	return ReceiveMsgWithAsyncHandler( timeout );
       
   145 }
       
   146 
       
   147 //**********************************************************************************
       
   148 // HtiAudioMsg::ReceiveMsgWithAsyncHandler
       
   149 //**********************************************************************************
       
   150 int HtiAudioMsg::ReceiveMsgWithAsyncHandler( int timeout )
       
   151 {
       
   152 	// Clean these up for to be received HTI message
       
   153 	if( m_msgBody )
       
   154 	{
       
   155 		delete m_msgBody;
       
   156 		m_msgBody = NULL;
       
   157 		m_msgBodyLen = 0;
       
   158 	}
       
   159 
       
   160 	HtiSoapHandlerInterface* handler =
       
   161 		static_cast<HtiSoapHandlerInterface*>( m_soap->user );
       
   162 
       
   163 	while(1)
       
   164 	{
       
   165 		if ( handler->WaitForHtiMessage( timeout ) )
       
   166 		{
       
   167 			// NOTE: this will be destroyed by gateway
       
   168 			m_msgBody = (BYTE*) handler->ReceivedHtiMessageBody();
       
   169 			m_msgBodyLen = handler->ReceivedHtiMessageBodySize();
       
   170 
       
   171 			if ( handle_async_msg( handler ) )
       
   172 				continue; // async msg received wait for next msg
       
   173 			else
       
   174 			{
       
   175 				// error msg received ?
       
   176 				if( handler->IsReceivedHtiError() )
       
   177 				{
       
   178 					SetSoapFaultFromReceivedHtiAudioError(
       
   179 						m_soap, m_msgBody, m_msgBodyLen );
       
   180 					return SOAP_FAULT;
       
   181 				}
       
   182 				return SOAP_OK;
       
   183 			}
       
   184 		}
       
   185 		else
       
   186 		{
       
   187 			// timeout
       
   188 			m_soap->error = soap_receiver_fault(m_soap,
       
   189 				"HtiGateway", "No response from symbian side");
       
   190 			return SOAP_FAULT;
       
   191 		}
       
   192 	}
       
   193 }
       
   194 
       
   195 
       
   196 //**********************************************************************************
       
   197 // HELPER FUNCTIONS
       
   198 //
       
   199 //**********************************************************************************
       
   200 
       
   201 //**********************************************************************************
       
   202 // isAudioPlaying
       
   203 //**********************************************************************************
       
   204 bool isAudioPlaying(struct soap *soap)
       
   205 {
       
   206 	if( (g_PlayCommandState[EPlayAudioFile] == EPlaying) ||
       
   207 	    (g_PlayCommandState[EPlayTone] == EPlaying) ||
       
   208 	    (g_PlayCommandState[EPlayDtmf] == EPlaying) )
       
   209 	{
       
   210 		soap->error = soap_receiver_fault(soap,
       
   211 			"HtiGateway", "already playing audio");
       
   212 		return TRUE;
       
   213 	}
       
   214 	return FALSE;
       
   215 }
       
   216 
       
   217 //**********************************************************************************
       
   218 // SetSoapFaultFromReceivedHtiAudioError
       
   219 //**********************************************************************************
       
   220 void SetSoapFaultFromReceivedHtiAudioError(struct soap *soap,
       
   221 										   BYTE *receivedMsgBody,
       
   222 										   int receivedMsgBodyLen)
       
   223 {
       
   224 	
       
   225 	if( receivedMsgBodyLen == 10 )
       
   226 	{
       
   227 		// This is a standard error message
       
   228 		// (eg. not authenticated)
       
   229 		HtiSoapHandlerInterface* handler =
       
   230 			static_cast<HtiSoapHandlerInterface*>(soap->user);
       
   231 		handler->SendSoapFaultFromReceivedHtiError();
       
   232 		return;
       
   233 	}
       
   234 
       
   235 
       
   236 	// Get error codes
       
   237 	int frameworkErrorCode = *((BYTE*)(receivedMsgBody + 1));
       
   238 	int serviceErrorCode = *((DWORD*)(receivedMsgBody + 2));
       
   239 
       
   240 	// Get error description
       
   241 	// NOTE: first byte is skipped because it contains the command code
       
   242 	int serviceErrorDescLen = receivedMsgBodyLen - 11;
       
   243 	char* serviceErrorDesc = new char[receivedMsgBodyLen - 11 + 1];
       
   244 	memcpy(serviceErrorDesc, receivedMsgBody+11, serviceErrorDescLen);
       
   245 	serviceErrorDesc[serviceErrorDescLen] = 0x0;
       
   246 
       
   247 	// Fill the xml struct
       
   248 	std::stringstream s;
       
   249 	s<<"<htiError xmlns=\'urn:hti/fault\'><frameworkErrorCode>";
       
   250 	s<<frameworkErrorCode;
       
   251 	s<<"</frameworkErrorCode><serviceErrorCode>";
       
   252 	s<<serviceErrorCode;
       
   253 	s<<"</serviceErrorCode><serviceErrorDescription>";
       
   254 	s<<serviceErrorDesc;
       
   255     s<<"</serviceErrorDescription>";
       
   256 	s<<"</htiError>";
       
   257 
       
   258 	soap->error = soap_receiver_fault(soap, "HtiError", s.str().c_str() );
       
   259 
       
   260 	delete serviceErrorDesc;
       
   261 }
       
   262 
       
   263 
       
   264 //**********************************************************************************
       
   265 // handle_async_msg
       
   266 // Will return TRUE if message is handled
       
   267 //**********************************************************************************
       
   268 bool handle_async_msg( HtiSoapHandlerInterface* handler )
       
   269 {
       
   270 	// Get message
       
   271 	BYTE *msgBody = (BYTE*) handler->ReceivedHtiMessageBody();
       
   272 	int msgBodyLen = handler->ReceivedHtiMessageBodySize();
       
   273 
       
   274 	if(handler->IsReceivedHtiError())
       
   275 	{
       
   276 		// First byte of service error description is the command it concers
       
   277 		int serviceErrorDesPos = 1+1+4+4;
       
   278 		switch(msgBody[serviceErrorDesPos])
       
   279 		{
       
   280 		case CMD_PLAY_FILE:
       
   281 			g_PlayCommandState[EPlayAudioFile] = EError;
       
   282 			return TRUE;
       
   283 
       
   284 		case CMD_PLAY_TONE:
       
   285 			g_PlayCommandState[EPlayTone] = EError;
       
   286 			return TRUE;
       
   287 
       
   288 		case CMD_PLAY_DTMF:
       
   289 			g_PlayCommandState[EPlayDtmf] = EError;
       
   290 			return TRUE;
       
   291 
       
   292 		default:
       
   293 			return FALSE;
       
   294 		}
       
   295 	}
       
   296 
       
   297 	switch(msgBody[0])
       
   298 	{
       
   299 	// Received from symbian side to indicate playing is complete
       
   300 	case CMD_PLAY_FILE:
       
   301 		g_PlayCommandState[EPlayAudioFile] = EStopped;
       
   302 		return TRUE;
       
   303 
       
   304 	case CMD_PLAY_TONE:
       
   305 		g_PlayCommandState[EPlayTone] = EStopped;
       
   306 		return TRUE;
       
   307 
       
   308 	case CMD_PLAY_DTMF:
       
   309 		g_PlayCommandState[EPlayDtmf] = EStopped;
       
   310 		return TRUE;
       
   311 
       
   312 	default:
       
   313 		return FALSE;
       
   314 	}
       
   315 
       
   316 	// Should never come here
       
   317 	return FALSE;
       
   318 }
       
   319 
       
   320 //**********************************************************************************
       
   321 // EXPORTED FUNCTIONS
       
   322 //
       
   323 //**********************************************************************************
       
   324 //**********************************************************************************
       
   325 // hti_serve()
       
   326 //**********************************************************************************
       
   327 int hti_serve(HtiSoapHandlerInterface* handler)
       
   328 {
       
   329 	handle_async_msg( handler );
       
   330 	return 0;
       
   331 }
       
   332 
       
   333 //**********************************************************************************
       
   334 // SOAP FUNCTIONS
       
   335 //
       
   336 //**********************************************************************************
       
   337 //**********************************************************************************
       
   338 // ns1__listAudioFiles()
       
   339 //**********************************************************************************
       
   340 int ns1__listAudioFiles(struct soap* soap,
       
   341 						char *directory, // optional
       
   342                         struct ArrayOfAudioFiles *audiofiles)
       
   343 {
       
   344 	// construct & send & receive HTI message
       
   345 	HtiAudioMsg msg( soap, HTI_AUDIO_UID, CMD_LIST_AUDIOFILES );
       
   346 	msg.AddStringWithLengthByte( directory );
       
   347 	if ( msg.SendReceiveMsgWithAsyncHandler( HTIMSG_TIMEOUT_10_SECONDS ) )
       
   348 		return SOAP_FAULT;
       
   349 
       
   350 	// Check CommandCode
       
   351 	if ( msg.CheckCommandCode( CMD_LIST_AUDIOFILES ) )
       
   352 		return SOAP_FAULT;
       
   353 
       
   354 	// get size of array & alloc space for pointers
       
   355 	audiofiles->__size  = (int) msg.GetWord(1);
       
   356 	audiofiles->__ptr = (char**) soap_malloc(soap, sizeof(char**)*audiofiles->__size);
       
   357 
       
   358 	// read files
       
   359 	int offset = 3;
       
   360 	for ( int i = 0; i < audiofiles->__size; i++ )
       
   361 	{
       
   362 		int fileNameLen = msg.GetByte(offset);
       
   363 		offset += 1;
       
   364 		audiofiles->__ptr[i] = (char*) soap_malloc( soap, fileNameLen+1 ); // +1 for nul
       
   365 		memcpy( audiofiles->__ptr[i], msg.GetMsgBody()+offset, fileNameLen );
       
   366 		( audiofiles->__ptr[i] )[fileNameLen] = 0x0; // add nul
       
   367 		offset += fileNameLen;
       
   368 	}
       
   369 
       
   370 	return SOAP_OK;
       
   371 }
       
   372 
       
   373 //**********************************************************************************
       
   374 // ns1__startPlayAudioFile()
       
   375 //**********************************************************************************
       
   376 int ns1__startPlayAudioFile(struct soap* soap,
       
   377 							char *fileName,
       
   378 							unsigned char volume,
       
   379 							int startPosition,
       
   380 							int stopPosition,
       
   381 							unsigned char nrOfRepeats,
       
   382 							int silenceBetweenRepeats,
       
   383 							char *audioSettings,
       
   384                             struct ns1__startPlayAudioFileResponse *out)
       
   385 {
       
   386 	if( check_mandatory_string_parameter(soap, fileName, "fileName") ||
       
   387 		check_mandatory_string_parameter(soap, audioSettings, "audioSettings") )
       
   388 		return SOAP_FAULT;
       
   389 
       
   390 	if( isAudioPlaying( soap ) )
       
   391 		return SOAP_FAULT;
       
   392 
       
   393 	// construct & send HTI message
       
   394 	HtiAudioMsg msg( soap, HTI_AUDIO_UID, CMD_PLAY_FILE );
       
   395 	msg.AddStringWithLengthByte( fileName );
       
   396 	msg.AddByte( volume );
       
   397 	msg.AddInt( startPosition );
       
   398 	msg.AddInt( stopPosition );
       
   399 	msg.AddByte( nrOfRepeats );
       
   400 	msg.AddInt( silenceBetweenRepeats );
       
   401 	if(!strcmp("Default", audioSettings))
       
   402 		msg.AddByte( 0x00 );
       
   403 	else if(!strcmp("General Music", audioSettings))
       
   404 		msg.AddByte( 0x01 );
       
   405 	else if(!strcmp("Ring Tone Preview", audioSettings))
       
   406 		msg.AddByte( 0x02 );
       
   407 	else
       
   408 	{
       
   409 		soap->error = soap_receiver_fault(soap, "HtiGateway", "unknown audio setting");
       
   410 		return SOAP_FAULT;
       
   411 	}
       
   412 	msg.SendMsg();
       
   413 
       
   414 	g_PlayCommandState[EPlayAudioFile] = EPlaying;
       
   415 
       
   416 	return SOAP_OK;
       
   417 }
       
   418 
       
   419 //**********************************************************************************
       
   420 // ns1__playAudioFile()
       
   421 //**********************************************************************************
       
   422 int ns1__playAudioFile(struct soap* soap,
       
   423 					   char *fileName,
       
   424 					   unsigned char volume,
       
   425 					   int startPosition,
       
   426 					   int stopPosition,
       
   427 					   unsigned char nrOfRepeats,
       
   428 					   int silenceBetweenRepeats,
       
   429 					   char *audioSettings,
       
   430 					   int timeout,
       
   431                        struct ns1__playAudioFileResponse *out)
       
   432 {
       
   433 	// Start playing
       
   434 	if( ns1__startPlayAudioFile(soap,
       
   435 		                        fileName,
       
   436                                 volume,
       
   437     	                        startPosition,
       
   438 	     					    stopPosition,
       
   439 		     				    nrOfRepeats,
       
   440 		 	    			    silenceBetweenRepeats,
       
   441 		 		    		    audioSettings,
       
   442 		 					    NULL) )
       
   443 	{
       
   444 		return SOAP_FAULT;
       
   445 	}
       
   446 
       
   447 	// Set stopped immidietly as this is synchronous call
       
   448 	g_PlayCommandState[EPlayAudioFile] = EStopped;
       
   449 
       
   450 	// get response
       
   451 	HtiAudioMsg msg( soap );
       
   452 	if ( msg.ReceiveMsg( timeout ) )
       
   453 		return SOAP_FAULT;
       
   454 
       
   455 	return msg.CheckCommandCode( CMD_PLAY_FILE );
       
   456 }
       
   457 
       
   458 //**********************************************************************************
       
   459 // ns1__startPlayTone()
       
   460 //**********************************************************************************
       
   461 int ns1__startPlayTone(struct soap* soap,
       
   462 					   unsigned short frequency,
       
   463 					   int duration,
       
   464 					   unsigned char volume,
       
   465 					   unsigned char nrOfRepeats,
       
   466 					   int silenceBetweenRepeats,
       
   467                        struct ns1__startPlayToneResponse *out)
       
   468 {
       
   469 	if( isAudioPlaying( soap ) )
       
   470 		return SOAP_FAULT;
       
   471 
       
   472 	HtiAudioMsg msg( soap, HTI_AUDIO_UID, CMD_PLAY_TONE );
       
   473 	msg.AddWord( frequency );
       
   474 	msg.AddInt( duration );
       
   475 	msg.AddByte( volume );
       
   476 	msg.AddByte( nrOfRepeats );
       
   477 	msg.AddInt( silenceBetweenRepeats );
       
   478 	msg.SendMsg();
       
   479 
       
   480 	g_PlayCommandState[EPlayTone] = EPlaying;
       
   481 
       
   482 	return SOAP_OK;
       
   483 }
       
   484 
       
   485 //**********************************************************************************
       
   486 // ns1__playTone()
       
   487 //**********************************************************************************
       
   488 int ns1__playTone(struct soap* soap,
       
   489 				  unsigned short frequency,
       
   490 				  int duration,
       
   491 				  unsigned char volume,
       
   492 				  unsigned char nrOfRepeats,
       
   493 				  int silenceBetweenRepeats,
       
   494                   struct ns1__playToneResponse *out)
       
   495 {
       
   496 	// Start playing
       
   497 	if(ns1__startPlayTone(soap,
       
   498 		                  frequency,
       
   499 						  duration,
       
   500 						  volume,nrOfRepeats,
       
   501 						  silenceBetweenRepeats,
       
   502 						  NULL))
       
   503 		return SOAP_FAULT;
       
   504 
       
   505 	// Calculate timeout (in milliseconds)
       
   506 	int timeout = duration/1000 +
       
   507 		          duration/1000*nrOfRepeats +
       
   508 				  silenceBetweenRepeats*nrOfRepeats +
       
   509 				  HTIMSG_TIMEOUT_10_SECONDS;
       
   510 
       
   511 	// Set stopped immidietly as this is synchronous call
       
   512 	g_PlayCommandState[EPlayTone] = EStopped;
       
   513 
       
   514 	// get response
       
   515 	HtiAudioMsg msg( soap );
       
   516 	if ( msg.ReceiveMsg( timeout ) )
       
   517 		return SOAP_FAULT;
       
   518 
       
   519 	return msg.CheckCommandCode( CMD_PLAY_TONE );
       
   520 }
       
   521 
       
   522 //**********************************************************************************
       
   523 // ns1__startPlayDtmf()
       
   524 //**********************************************************************************
       
   525 int ns1__startPlayDtmf(struct soap* soap,
       
   526 					   char *dtmfString,
       
   527 					   int toneLength,
       
   528 					   int gapLength,
       
   529 					   unsigned char volume,
       
   530 					   unsigned char nrOfRepeats,
       
   531 					   int silenceBetweenRepeats,
       
   532                        struct ns1__startPlayDtmfResponse *out)
       
   533 {
       
   534 	if(check_mandatory_string_parameter(soap, dtmfString, "dtmfString"))
       
   535 		return SOAP_FAULT;
       
   536 
       
   537 	if(isAudioPlaying(soap))
       
   538 		return SOAP_FAULT;
       
   539 
       
   540 	HtiAudioMsg msg( soap, HTI_AUDIO_UID, CMD_PLAY_DTMF );
       
   541 	msg.AddStringWithLengthByte( dtmfString );
       
   542 	msg.AddInt( toneLength );
       
   543 	msg.AddInt( gapLength );
       
   544 	msg.AddByte( volume );
       
   545 	msg.AddByte( nrOfRepeats );
       
   546 	msg.AddInt( silenceBetweenRepeats );
       
   547 	msg.SendMsg();
       
   548 
       
   549 	g_PlayCommandState[EPlayDtmf] = EPlaying;
       
   550 
       
   551 	return SOAP_OK;
       
   552 }
       
   553 
       
   554 //**********************************************************************************
       
   555 // ns1__playDtmf()
       
   556 //**********************************************************************************
       
   557 int ns1__playDtmf(struct soap* soap,
       
   558 				  char *dtmfString,
       
   559 				  int toneLength,
       
   560 				  int gapLength,
       
   561 				  unsigned char volume,
       
   562 				  unsigned char nrOfRepeats,
       
   563 				  int silenceBetweenRepeats,
       
   564                   struct ns1__playDtmfResponse *out)
       
   565 {
       
   566 	// Start playing
       
   567 	if(ns1__startPlayDtmf(soap,
       
   568 		                  dtmfString,
       
   569 						  toneLength,
       
   570 						  gapLength,
       
   571 						  volume,
       
   572 						  nrOfRepeats,
       
   573 						  silenceBetweenRepeats,
       
   574 						  NULL))
       
   575 		return SOAP_FAULT;
       
   576 
       
   577 	// Calculate timeout
       
   578 	int nrOfTones = (int) strlen(dtmfString);
       
   579 	int timeout = toneLength/1000*nrOfTones +
       
   580 		          gapLength/1000*nrOfTones +
       
   581 				  toneLength/1000*nrOfTones*nrOfRepeats +
       
   582 		          gapLength/1000*nrOfTones*nrOfRepeats +
       
   583 				  silenceBetweenRepeats/1000*nrOfRepeats +
       
   584 				  HTIMSG_TIMEOUT_10_SECONDS;
       
   585 
       
   586 	// Set stopped immidietly as this is synchronous call
       
   587 	g_PlayCommandState[EPlayDtmf] = EStopped;
       
   588 
       
   589 	// get response
       
   590 	HtiAudioMsg msg( soap );
       
   591 	if ( msg.ReceiveMsg( timeout ) )
       
   592 		return SOAP_FAULT;
       
   593 
       
   594 	return msg.CheckCommandCode( CMD_PLAY_DTMF );
       
   595 }
       
   596 
       
   597 //**********************************************************************************
       
   598 // ns1__getPlayStatus()
       
   599 //**********************************************************************************
       
   600 int ns1__getPlayStatus(struct soap* soap,
       
   601 					   char* type,
       
   602 					   char* &status)
       
   603 {
       
   604 	PlayCommand command;
       
   605 
       
   606 	if( !strcmp(type, "audiofile") )
       
   607 		command = EPlayAudioFile;
       
   608 	else if( !strcmp(type, "tone") )
       
   609 		command = EPlayTone;
       
   610 	else if( !strcmp(type, "dtmf") )
       
   611 		command = EPlayDtmf;
       
   612 	else
       
   613 	{
       
   614 		soap->error = soap_receiver_fault(soap, "HtiGateway", "unknown type");
       
   615 		return SOAP_FAULT;
       
   616 	}
       
   617 
       
   618 	switch(g_PlayCommandState[command])
       
   619 	{
       
   620 	case EStopped:
       
   621 		status = (char*) soap_malloc(soap, strlen("stopped")+1); // +1 for nul char
       
   622 		strcpy(status, "stopped");
       
   623 		break;
       
   624 
       
   625 	case EPlaying:
       
   626 		status = (char*) soap_malloc(soap, strlen("playing")+1); // +1 for nul char
       
   627 		strcpy(status, "playing");
       
   628 		break;
       
   629 
       
   630 	case EError:
       
   631 		g_PlayCommandState[command] = EStopped;
       
   632 		status = (char*) soap_malloc(soap, strlen("error")+1); // +1 for nul char
       
   633 		strcpy(status, "error");
       
   634 		break;
       
   635 
       
   636 	default:
       
   637 		// should not happen ever
       
   638 		soap->error = soap_receiver_fault(soap, "HtiGateway", "unknown state");
       
   639 		return SOAP_FAULT;
       
   640 	}
       
   641 
       
   642 	return SOAP_OK;
       
   643 }
       
   644 
       
   645 //**********************************************************************************
       
   646 // ns1__stopPlayback()
       
   647 //**********************************************************************************
       
   648 int ns1__stopPlayback(struct soap* soap,
       
   649 					  void *_,
       
   650                       struct ns1__stopPlaybackResponse *out)
       
   651 {
       
   652 	for(int i=0; i < ENrOfPlayCommands; i++)
       
   653         g_PlayCommandState[i] = EStopped;
       
   654 
       
   655 	HtiAudioMsg msg( soap, HTI_AUDIO_UID, CMD_STOP );
       
   656 	if ( msg.SendReceiveMsgWithAsyncHandler( HTIMSG_TIMEOUT_10_SECONDS ) )
       
   657 		return SOAP_FAULT;
       
   658 
       
   659 	if ( msg.CheckCommandCode( CMD_STOP ) )
       
   660 		return SOAP_FAULT;
       
   661 
       
   662 	return SOAP_OK;
       
   663 }
       
   664 
       
   665 //**********************************************************************************
       
   666 // ns1__getDuration()
       
   667 //**********************************************************************************
       
   668 int ns1__getDuration(struct soap* soap,
       
   669 					 char *fileName, // optional
       
   670 					 int &duration)
       
   671 {
       
   672 	HtiAudioMsg msg( soap, HTI_AUDIO_UID, CMD_GET_DURATION );
       
   673 	msg.AddStringWithLengthByte( fileName );
       
   674 	if ( msg.SendReceiveMsgWithAsyncHandler( HTIMSG_TIMEOUT_10_SECONDS ) )
       
   675 		return SOAP_FAULT;
       
   676 
       
   677 	if ( msg.CheckCommandCode( CMD_GET_DURATION ) )
       
   678 		return SOAP_FAULT;
       
   679 
       
   680 	duration = msg.GetInt(1);
       
   681 
       
   682 	return SOAP_OK;
       
   683 }
       
   684 
       
   685 //**********************************************************************************
       
   686 // ns1__getMaxVolume()
       
   687 //**********************************************************************************
       
   688 int ns1__getMaxVolume(struct soap* soap,
       
   689 					  char *fileName, // optional
       
   690 					  unsigned char &volume)
       
   691 {
       
   692 	HtiAudioMsg msg( soap, HTI_AUDIO_UID, CMD_GET_MAXVOLUME );
       
   693 	msg.AddStringWithLengthByte( fileName );
       
   694 	if ( msg.SendReceiveMsgWithAsyncHandler( HTIMSG_TIMEOUT_10_SECONDS ) )
       
   695 		return SOAP_FAULT;
       
   696 
       
   697 	if ( msg.CheckCommandCode( CMD_GET_MAXVOLUME ) )
       
   698 		return SOAP_FAULT;
       
   699 
       
   700 	volume = msg.GetByte(1);
       
   701 
       
   702 	return SOAP_OK;
       
   703 }
       
   704 
       
   705 //**********************************************************************************
       
   706 // ns1__setVolume()
       
   707 //**********************************************************************************
       
   708 int ns1__setVolume(struct soap* soap,
       
   709 				   unsigned char volume,
       
   710 				   unsigned char &volumeSet)
       
   711 {
       
   712 	HtiAudioMsg msg( soap, HTI_AUDIO_UID, CMD_SET_VOLUME );
       
   713 	msg.AddByte( volume );
       
   714 	if ( msg.SendReceiveMsgWithAsyncHandler( HTIMSG_TIMEOUT_10_SECONDS ) )
       
   715 		return SOAP_FAULT;
       
   716 
       
   717 	if ( msg.CheckCommandCode( CMD_SET_VOLUME ) )
       
   718 		return SOAP_FAULT;
       
   719 
       
   720 	volumeSet = msg.GetByte(1);
       
   721 
       
   722 	return SOAP_OK;
       
   723 }
       
   724 
       
   725 
       
   726