usbmgmt/usbmgr/host/fdf/production/server/src/eventqueue.cpp
changeset 0 c9bc50fca66e
child 7 fcd52893bbd3
equal deleted inserted replaced
-1:000000000000 0:c9bc50fca66e
       
     1 /*
       
     2 * Copyright (c) 2007-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 */
       
    17 
       
    18 #include "eventqueue.h"
       
    19 #include <usb/usblogger.h>
       
    20 #include "fdf.h"
       
    21 #include "fdfsession.h"
       
    22 #include "utils.h"
       
    23 
       
    24 #ifdef __FLOG_ACTIVE
       
    25 _LIT8(KLogComponent, "fdf      ");
       
    26 #endif
       
    27 
       
    28 #ifdef __FLOG_ACTIVE
       
    29 #define LOG Log()
       
    30 #else
       
    31 #define LOG
       
    32 #endif
       
    33 
       
    34 #ifdef _DEBUG
       
    35 PANICCATEGORY("eventq");
       
    36 #endif
       
    37 
       
    38 class CFdfSession;
       
    39 
       
    40 CEventQueue* CEventQueue::NewL(CFdf& aFdf)
       
    41 	{
       
    42 	LOG_STATIC_FUNC_ENTRY
       
    43 
       
    44 	CEventQueue* self = new(ELeave) CEventQueue(aFdf);
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 CEventQueue::CEventQueue(CFdf& aFdf)
       
    49 :	iFdf(aFdf),
       
    50 	iDeviceEvents(_FOFF(TDeviceEvent, iLink))
       
    51 	{
       
    52 	LOG_FUNC
       
    53 	}
       
    54 
       
    55 CEventQueue::~CEventQueue()
       
    56 	{
       
    57 	LOG_FUNC
       
    58 
       
    59 	// There will be things left on the queue at this time if USBMAN shuts us
       
    60 	// down without having picked up everything that was on the queue.
       
    61 	// This is valid behaviour and these events need destroying now.
       
    62 	TSglQueIter<TDeviceEvent> iter(iDeviceEvents);
       
    63 	iter.SetToFirst();
       
    64 	TDeviceEvent* event;
       
    65 	while ( ( event = iter++ ) != NULL )
       
    66 		{
       
    67 		delete event;
       
    68 		}
       
    69 	}
       
    70 
       
    71 // Increments the count of failed attachments.
       
    72 void CEventQueue::AttachmentFailure(TInt aError)
       
    73 	{
       
    74 	LOG_FUNC
       
    75 	LOGTEXT2(_L8("\taError = %d"), aError);
       
    76 	LOG;
       
    77 
       
    78 	TUint index = 0;
       
    79 	switch ( aError )
       
    80 		{
       
    81 	case KErrUsbSetAddrFailed:
       
    82 		index = KSetAddrFailed;
       
    83 		break;
       
    84 	case KErrUsbNoPower:
       
    85 		index = KNoPower;
       
    86 		break;
       
    87 	case KErrBadPower:
       
    88 		index = KBadPower;
       
    89 		break;
       
    90 	case KErrUsbIOError:
       
    91 		index = KIOError;
       
    92 		break;
       
    93 	case KErrUsbTimeout:
       
    94 		index = KTimeout;
       
    95 		break;
       
    96 	case KErrUsbStalled:
       
    97 		index = KStalled;
       
    98 		break;
       
    99 	case KErrNoMemory:
       
   100 		index = KNoMemory;
       
   101 		break;
       
   102 	case KErrUsbConfigurationHasNoInterfaces:
       
   103 		index = KConfigurationHasNoInterfaces;
       
   104 		break;
       
   105 	case KErrUsbInterfaceCountMismatch:
       
   106 		index = KInterfaceCountMismatch;
       
   107 		break;
       
   108 	case KErrUsbDuplicateInterfaceNumbers:
       
   109 		index = KDuplicateInterfaceNumbers;
       
   110 		break;
       
   111 	case KErrBadHandle:
       
   112 		index = KBadHandle;
       
   113 		break;
       
   114 
       
   115 	default:
       
   116 		// we must deal with every error we are ever given
       
   117 		LOGTEXT2(_L8("\tFDF did not expect this error %d as a fail attachment"), aError);
       
   118 		index = KAttachmentFailureGeneralError;
       
   119 		break;
       
   120 		}
       
   121 	++(iAttachmentFailureCount[index]);
       
   122 
       
   123 	PokeSession();
       
   124 	LOG;
       
   125 	}
       
   126 
       
   127 // Called to add an event to the tail of the queue.
       
   128 // Takes ownership of aEvent.
       
   129 void CEventQueue::AddDeviceEvent(TDeviceEvent& aEvent)
       
   130 	{
       
   131 	LOG_FUNC
       
   132 	LOGTEXT2(_L8("\t&aEvent = 0x%08x"), &aEvent);
       
   133 	LOG;
       
   134 
       
   135 	iDeviceEvents.AddLast(aEvent);
       
   136 
       
   137 	PokeSession();
       
   138 	LOG;
       
   139 	}
       
   140 
       
   141 // Poke the session object (if it exists) to complete any outstanding event
       
   142 // notifications it has.
       
   143 // It only makes sense to call this function if there's some event to give up.
       
   144 void CEventQueue::PokeSession()
       
   145 	{
       
   146 	LOG_FUNC
       
   147 
       
   148 	// If the session exists, and has a notification outstanding, give them
       
   149 	// the head event.
       
   150 	CFdfSession* sess = iFdf.Session();
       
   151 	if ( sess )
       
   152 		{
       
   153 		if ( sess->NotifyDevmonEventOutstanding() )
       
   154 			{
       
   155 			TInt event;
       
   156 			if ( GetDevmonEvent(event) )
       
   157 				{
       
   158 				sess->DevmonEvent(event);
       
   159 				}
       
   160 			}
       
   161 		if ( sess->NotifyDeviceEventOutstanding() )
       
   162 			{
       
   163 			TDeviceEvent event;
       
   164 			if ( GetDeviceEvent(event) )
       
   165 				{
       
   166 				sess->DeviceEvent(event);
       
   167 				}
       
   168 			}
       
   169 		}
       
   170 	}
       
   171 
       
   172 // This is called to get a device event. Attachment failures are given up
       
   173 // before actual queued events.
       
   174 // If ETrue is returned, the queued event is destroyed and a copy is delivered
       
   175 // in aEvent.
       
   176 TBool CEventQueue::GetDeviceEvent(TDeviceEvent& aEvent)
       
   177 	{
       
   178 	LOG_FUNC
       
   179 	LOG;
       
   180 
       
   181 	TBool ret = EFalse;
       
   182 
       
   183 	// We need to see if any attachment failures have been collected by us. If
       
   184 	// there have, report exactly one of them to the client.
       
   185 	for ( TUint ii = 0 ; ii < KNumberOfAttachmentFailureTypes ; ++ii )
       
   186 		{
       
   187 		TUint& errorCount = iAttachmentFailureCount[ii];
       
   188 		if ( errorCount )
       
   189 			{
       
   190 			--errorCount;
       
   191 			switch ( ii )
       
   192 				{
       
   193 			case KSetAddrFailed:
       
   194 				aEvent.iInfo.iError = KErrUsbSetAddrFailed;
       
   195 				break;
       
   196 			case KNoPower:
       
   197 				aEvent.iInfo.iError = KErrUsbNoPower;
       
   198 				break;
       
   199 			case KBadPower:
       
   200 				aEvent.iInfo.iError = KErrBadPower;
       
   201 				break;
       
   202 			case KIOError:
       
   203 				aEvent.iInfo.iError = KErrUsbIOError;
       
   204 				break;
       
   205 			case KTimeout:
       
   206 				aEvent.iInfo.iError = KErrUsbTimeout;
       
   207 				break;
       
   208 			case KStalled:
       
   209 				aEvent.iInfo.iError = KErrUsbStalled;
       
   210 				break;
       
   211 			case KNoMemory:
       
   212 				aEvent.iInfo.iError = KErrNoMemory;
       
   213 				break;
       
   214 			case KConfigurationHasNoInterfaces:
       
   215 				aEvent.iInfo.iError = KErrUsbConfigurationHasNoInterfaces;
       
   216 				break;
       
   217 			case KInterfaceCountMismatch:
       
   218 				aEvent.iInfo.iError = KErrUsbInterfaceCountMismatch;
       
   219 				break;
       
   220 			case KDuplicateInterfaceNumbers:
       
   221 				aEvent.iInfo.iError = KErrUsbDuplicateInterfaceNumbers;
       
   222 				break;
       
   223 			case KBadHandle:
       
   224 				aEvent.iInfo.iError = KErrBadHandle;
       
   225 				break;
       
   226 			case KAttachmentFailureGeneralError:
       
   227 				aEvent.iInfo.iError = KErrUsbAttachmentFailureGeneralError;
       
   228 				break;
       
   229 
       
   230 			case KNumberOfAttachmentFailureTypes:
       
   231 			default:
       
   232 				// this switch should deal with every error type we store
       
   233 				ASSERT_DEBUG(0);
       
   234 
       
   235 				}
       
   236 
       
   237 			ret = ETrue;
       
   238 			aEvent.iInfo.iEventType = EDeviceAttachment;
       
   239 			LOGTEXT2(_L8("\treturning attachment failure event (code %d)"), aEvent.iInfo.iError);
       
   240 			// Only give the client one error at a time.
       
   241 			break;
       
   242 			}
       
   243 		}
       
   244 
       
   245 	if ( !ret && !iDeviceEvents.IsEmpty() )
       
   246 		{
       
   247 		TDeviceEvent* const event = iDeviceEvents.First();
       
   248 		LOGTEXT2(_L8("\tevent = 0x%08x"), event);
       
   249 		iDeviceEvents.Remove(*event);
       
   250 		(void)Mem::Copy(&aEvent, event, sizeof(TDeviceEvent));
       
   251 		delete event;
       
   252 		ret = ETrue;
       
   253 		}
       
   254 
       
   255 	LOG;
       
   256 	LOGTEXT2(_L8("\treturning %d"), ret);
       
   257 	return ret;
       
   258 	}
       
   259 
       
   260 TBool CEventQueue::GetDevmonEvent(TInt& aEvent)
       
   261 	{
       
   262 	LOG_FUNC
       
   263 	LOG;
       
   264 
       
   265 	TBool ret = EFalse;
       
   266 
       
   267 	for ( TUint ii = 0 ; ii < KNumberOfDevmonEventTypes ; ++ii )
       
   268 		{
       
   269 		TUint& eventCount = iDevmonEventCount[ii];
       
   270 		if ( eventCount )
       
   271 			{
       
   272 			--eventCount;
       
   273 			switch ( ii )
       
   274 				{
       
   275 
       
   276 			case KUsbDeviceRejected:
       
   277 				aEvent = KErrUsbDeviceRejected;
       
   278 				break;
       
   279 			case KUsbDeviceFailed:
       
   280 				aEvent = KErrUsbDeviceFailed;
       
   281 				break;
       
   282 			case KUsbBadDevice:
       
   283 				aEvent = KErrUsbBadDevice;
       
   284 				break;
       
   285 			case KUsbBadHubPosition:
       
   286 				aEvent = KErrUsbBadHubPosition;
       
   287 				break;
       
   288 			case KUsbBadHub:
       
   289 				aEvent = KErrUsbBadHub;
       
   290 				break;
       
   291 			case KUsbEventOverflow:
       
   292 				aEvent = KErrUsbEventOverflow;
       
   293 				break;
       
   294 			case KUsbBadDeviceAttached:
       
   295 			    aEvent = KErrUsbBadDeviceAttached;
       
   296 			    break;
       
   297 			case KUsbBadDeviceDetached:
       
   298 			    aEvent = KEventUsbBadDeviceDetached;
       
   299 			    break;
       
   300 			case KNumberOfDevmonEventTypes:
       
   301 			default:
       
   302 				LOGTEXT2(_L8("\tUnexpected devmon error, not handled properly %d"), ii);
       
   303 				ASSERT_DEBUG(0);
       
   304 				aEvent = KErrUsbDeviceRejected;
       
   305 				// this switch should deal with every error type we store
       
   306 				}
       
   307 
       
   308 			ret = ETrue;
       
   309 			// Only give the client one error at a time.
       
   310 			break;
       
   311 			}
       
   312 		}
       
   313 
       
   314 	LOG;
       
   315 	LOGTEXT2(_L8("\treturning %d"), ret);
       
   316 	return ret;
       
   317 	}
       
   318 
       
   319 void CEventQueue::AddDevmonEvent(TInt aEvent)
       
   320 	{
       
   321 	LOG_FUNC
       
   322 	LOGTEXT2(_L8("\taEvent = %d"), aEvent);
       
   323 
       
   324 	// Increment the relevant count.
       
   325 	TInt index = 0;
       
   326 	switch ( aEvent )
       
   327 		{
       
   328 
       
   329 		case KErrUsbDeviceRejected:
       
   330 			index = KUsbDeviceRejected;
       
   331 			break;
       
   332 		case KErrUsbDeviceFailed:
       
   333 			index = KUsbDeviceFailed;
       
   334 			break;
       
   335 		case KErrUsbBadDevice:
       
   336 			index = KUsbBadDevice;
       
   337 			break;
       
   338 		case KErrUsbBadHubPosition:
       
   339 			index = KUsbBadHubPosition;
       
   340 			break;
       
   341 		case KErrUsbBadHub:
       
   342 			index = KUsbBadHub;
       
   343 			break;
       
   344 		case KErrUsbEventOverflow:
       
   345 			index = KUsbEventOverflow;
       
   346 			break;
       
   347         case KErrUsbBadDeviceAttached:
       
   348             index = KUsbBadDeviceAttached;
       
   349             break;
       
   350         case KEventUsbBadDeviceDetached:
       
   351             index = KUsbBadDeviceDetached;
       
   352             break;			
       
   353 
       
   354 		default:
       
   355 			LOGTEXT2(_L8("\tUnexpected devmon error, not handled properly %d"), aEvent);
       
   356 			ASSERT_DEBUG(0);
       
   357 			// this switch should deal with every type of event we ever receive from devmon
       
   358 			}
       
   359 
       
   360 	TUint& eventCount = iDevmonEventCount[index];
       
   361 	ASSERT_DEBUG(eventCount < KMaxTUint);
       
   362 	++eventCount;
       
   363 	PokeSession();
       
   364 	}
       
   365 
       
   366 #ifdef __FLOG_ACTIVE
       
   367 
       
   368 void CEventQueue::Log()
       
   369 	{
       
   370 	LOG_FUNC
       
   371 
       
   372 	for ( TUint ii = 0 ; ii < KNumberOfAttachmentFailureTypes ; ++ii )
       
   373 		{
       
   374 		const TInt& errorCount = iAttachmentFailureCount[ii];
       
   375 		if ( errorCount )
       
   376 			{
       
   377 			LOGTEXT3(_L8("\tNumber of attachment failures of type %d is %d"), ii, errorCount);
       
   378 			}
       
   379 		}
       
   380 
       
   381 	for ( TUint ii = 0 ; ii < KNumberOfDevmonEventTypes ; ++ii )
       
   382 		{
       
   383 		const TInt& eventCount = iDevmonEventCount[ii];
       
   384 		if ( eventCount )
       
   385 			{
       
   386 			LOGTEXT3(_L8("\tNumber of devmon events of type %d is %d"), ii, eventCount);
       
   387 			}
       
   388 		}
       
   389 
       
   390 	TUint pos = 0;
       
   391 	TSglQueIter<TDeviceEvent> iter(iDeviceEvents);
       
   392 	iter.SetToFirst();
       
   393 	TDeviceEvent* event;
       
   394 	while ( ( event = iter++ ) != NULL )
       
   395 		{
       
   396 		LOGTEXT2(_L8("\tLogging event at position %d"), pos);
       
   397 		event->Log();
       
   398 		++pos;
       
   399 		}
       
   400 	}
       
   401 
       
   402 #endif // __FLOG_ACTIVE
       
   403