bthci/bthci2/btpowercontrol/src/btpowercontrol.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 #include <bluetooth/btpowercontrol.h>
       
    22 #include <bluetooth/hciserverclient.h>
       
    23 #include <bluetooth/hci/hciipc.h>
       
    24 #include <bluetooth/logger.h>
       
    25 #include <bluetooth/btpowercontrolmessages.h>
       
    26 
       
    27 #ifdef __FLOG_ACTIVE
       
    28 _LIT8(KLogComponent, LOG_COMPONENT_BTPOWERCONTROL);
       
    29 #endif
       
    30 
       
    31 
       
    32 EXPORT_C RBTPowerControl::RBTPowerControl()
       
    33 	: iHCIServerSession(NULL)
       
    34 	{
       
    35 	}
       
    36 
       
    37 /**
       
    38 Opens the handle to the power control interface for the Bluetooth hardware.
       
    39 
       
    40 @panic KBTPowerControlPanic EHandleIsAlreadyOpen If the RBTPowerControl instance has already been opened (and not subsequently closed).
       
    41 */
       
    42 EXPORT_C TInt RBTPowerControl::Open()
       
    43 	{
       
    44 	__ASSERT_ALWAYS(!iHCIServerSession, PANIC(KBTPowerControlPanic, EHandleIsAlreadyOpen));
       
    45 	TInt err = KErrNoMemory;
       
    46 	iHCIServerSession = new RHCIServerSession;
       
    47 	if(iHCIServerSession)
       
    48 		{
       
    49 		err = iHCIServerSession->Open(KHCIPowerControlManagerUid);
       
    50 		if(err != KErrNone)
       
    51 			{
       
    52 			delete iHCIServerSession;
       
    53 			iHCIServerSession = NULL;
       
    54 			}
       
    55 		}
       
    56 	return err;
       
    57 	}
       
    58 
       
    59 /**
       
    60 Closes the handle to the Bluetooth power control interface.
       
    61 
       
    62 Note that it is safe to call this multiple times.
       
    63 */
       
    64 EXPORT_C void RBTPowerControl::Close()
       
    65 	{
       
    66 	if(iHCIServerSession)
       
    67 		{
       
    68 		iHCIServerSession->Close();
       
    69 		delete iHCIServerSession;
       
    70 		iHCIServerSession = NULL;
       
    71 		}
       
    72 	}
       
    73 
       
    74 /**
       
    75 Makes an asynchronous request to set the power state of the Bluetooth hardware.
       
    76 
       
    77 @param aState The desired state.
       
    78 @param aInfo 
       
    79 @param aStatus The TRequestStatus that will signal the result of the request when completed.
       
    80 
       
    81 @panic KBTPowerControlPanic EApiUsedWhenHandleIsNotOpen If called when the RBTPowerControl instance has not been opened.
       
    82 */
       
    83 EXPORT_C void RBTPowerControl::SetPower(TBTPowerState aState, TDes8* aInfo, TRequestStatus &aStatus)
       
    84 	{
       
    85 	__ASSERT_ALWAYS(iHCIServerSession, PANIC(KBTPowerControlPanic, EApiUsedWhenHandleIsNotOpen));
       
    86 	TInt state = static_cast<TInt>(aState);
       
    87 	iHCIServerSession->SendAsync(EHCISetPower, aInfo, NULL, state, aStatus);
       
    88 	}
       
    89 
       
    90 /**
       
    91 Makes a synchronous request to get the current power state of the Bluetooth hardware.
       
    92 
       
    93 @param aState Set (on return) to the current power state if request was successful.
       
    94 @param aInfo
       
    95 @return KErrNone if the request was successful, otherwise one of the system-wide error codes.
       
    96 
       
    97 @panic KBTPowerControlPanic EApiUsedWhenHandleIsNotOpen If called when the RBTPowerControl instance has not been opened.
       
    98 */
       
    99 EXPORT_C TInt RBTPowerControl::GetPower(TBTPowerState& aState, TDes8* aInfo)
       
   100 	{
       
   101 	__ASSERT_ALWAYS(iHCIServerSession, PANIC(KBTPowerControlPanic, EApiUsedWhenHandleIsNotOpen));
       
   102 	TPckgBuf<TBTPowerState> pckg;
       
   103 	TInt err = iHCIServerSession->SendSync(EHCIGetPower, aInfo, &pckg, 0);
       
   104 	if(err==KErrNone)
       
   105 		{
       
   106 		aState = pckg();
       
   107 		}
       
   108 	return err;
       
   109 	}
       
   110 
       
   111 /**
       
   112 Cancels asynchronous requests made through this handle to the power control interface.
       
   113 
       
   114 @panic KBTPowerControlPanic EApiUsedWhenHandleIsNotOpen If called when the RBTPowerControl instance has not been opened.
       
   115 */
       
   116 EXPORT_C void RBTPowerControl::Cancel()
       
   117 	{
       
   118 	__ASSERT_ALWAYS(iHCIServerSession, PANIC(KBTPowerControlPanic, EApiUsedWhenHandleIsNotOpen));
       
   119 	// Cast away the return value - we can only do our best here, if there is an error (which shouldn't originate
       
   120 	// from the HCI Server at least) we just ignore it.
       
   121 	static_cast<void>(iHCIServerSession->SendSync(KHCICancelPower, NULL, NULL, 0));
       
   122 	}