commands/usb/usb.cpp
changeset 0 7f656887cf89
child 77 60f47003f4b1
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // usb.cpp
       
     2 // 
       
     3 // Copyright (c) 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "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 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include <fshell/ioutils.h>
       
    14 #include <fshell/common.mmh>
       
    15 #include <usbman.h>
       
    16 
       
    17 using namespace IoUtils;
       
    18 
       
    19 class CCmdUsb : public CCommandBase
       
    20 	{
       
    21 public:
       
    22 	static CCommandBase* NewLC();
       
    23 	~CCmdUsb();
       
    24 private:
       
    25 	CCmdUsb();
       
    26 private: // From CCommandBase.
       
    27 	virtual const TDesC& Name() const;
       
    28 	virtual void DoRunL();
       
    29 	virtual void ArgumentsL(RCommandArgumentList& aArguments);
       
    30 	virtual void OptionsL(RCommandOptionList& aOptions);
       
    31 private:
       
    32 	RUsb iUsb;
       
    33 	TInt iPersonality;
       
    34 	};
       
    35 
       
    36 EXE_BOILER_PLATE(CCmdUsb)
       
    37 
       
    38 CCommandBase* CCmdUsb::NewLC()
       
    39 	{
       
    40 	CCmdUsb* self = new(ELeave) CCmdUsb();
       
    41 	CleanupStack::PushL(self);
       
    42 	self->BaseConstructL();
       
    43 	return self;
       
    44 	}
       
    45 
       
    46 CCmdUsb::~CCmdUsb()
       
    47 	{
       
    48 	iUsb.Close();
       
    49 	}
       
    50 
       
    51 CCmdUsb::CCmdUsb()
       
    52 	{
       
    53 	}
       
    54 
       
    55 const TDesC& CCmdUsb::Name() const
       
    56 	{
       
    57 	_LIT(KName, "usb");	
       
    58 	return KName;
       
    59 	}
       
    60 
       
    61 void CCmdUsb::ArgumentsL(RCommandArgumentList& aArguments)
       
    62 	{
       
    63 	aArguments.AppendIntL(iPersonality, _L("personality"));
       
    64 	}
       
    65 
       
    66 void CCmdUsb::OptionsL(RCommandOptionList& /*aOptions*/)
       
    67 	{
       
    68 	}
       
    69 
       
    70 #define CASE_LIT(x) case x: { _LIT(KName, #x); return &KName; }
       
    71 
       
    72 const TDesC* DeviceState(TUsbDeviceState aState)
       
    73 	{
       
    74 	switch (aState)
       
    75 		{
       
    76 		CASE_LIT(EUsbDeviceStateUndefined);
       
    77 		CASE_LIT(EUsbDeviceStateDefault);
       
    78 		CASE_LIT(EUsbDeviceStateAttached);
       
    79 		CASE_LIT(EUsbDeviceStatePowered);
       
    80 		CASE_LIT(EUsbDeviceStateConfigured);
       
    81 		CASE_LIT(EUsbDeviceStateAddress);
       
    82 		CASE_LIT(EUsbDeviceStateSuspended);
       
    83 		default:
       
    84 			{
       
    85 			_LIT(KUnknown, "?");
       
    86 			return &KUnknown;
       
    87 			}
       
    88 		}
       
    89 	}
       
    90 
       
    91 const TDesC* ServiceState(TUsbServiceState aState)
       
    92 	{
       
    93 	switch (aState)
       
    94 		{
       
    95 		CASE_LIT(EUsbServiceIdle);
       
    96 		CASE_LIT(EUsbServiceStarting);
       
    97 		CASE_LIT(EUsbServiceStarted);
       
    98 		CASE_LIT(EUsbServiceStopping);
       
    99 		CASE_LIT(EUsbServiceFatalError);
       
   100 		default:
       
   101 			{
       
   102 			_LIT(KUnknown, "?");
       
   103 			return &KUnknown;
       
   104 			}
       
   105 		}
       
   106 	}
       
   107 
       
   108 void CCmdUsb::DoRunL()
       
   109 	{
       
   110 	LeaveIfErr(iUsb.Connect(), _L("Couldn't connect to RUsb"));
       
   111 
       
   112 	TUsbDeviceState dev;
       
   113 	LeaveIfErr(iUsb.GetDeviceState(dev), _L("Couldn't get device state"));
       
   114 	Printf(_L("Usb Device state = %S (%d)\r\n"), DeviceState(dev), dev);
       
   115 
       
   116 	TUsbServiceState servicestate;
       
   117 	LeaveIfErr(iUsb.GetServiceState(servicestate), _L("Couldn't get service state"));
       
   118 	Printf(_L("Usb Service state = %S (%d)\r\n"), ServiceState(servicestate), servicestate);
       
   119 
       
   120 	RArray<TInt> ids;
       
   121 	LeaveIfErr(iUsb.GetPersonalityIds(ids), _L("Couldn't get personalities ids"));
       
   122 	for (TInt i = 0; i < ids.Count(); i++)
       
   123 		{
       
   124 		Printf(_L("Personality %d\r\n"), ids[i]);
       
   125 		}
       
   126 
       
   127 	if (iArguments.IsPresent(&iPersonality))
       
   128 		{
       
   129 		TRequestStatus stat;
       
   130 		iUsb.TryStart(iPersonality, stat);
       
   131 		User::WaitForRequest(stat);
       
   132 		LeaveIfErr(stat.Int(), _L("Couldn't start USB personality %d"), iPersonality);
       
   133 		Printf(_L("USB started ok.\r\n"));
       
   134 		}
       
   135 	}