00001 /* 00002 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions are met: 00006 * 00007 * * Redistributions of source code must retain the above copyright notice, this 00008 * list of conditions and the following disclaimer. 00009 * * Redistributions in binary form must reproduce the above copyright notice, 00010 * this list of conditions and the following disclaimer in the documentation 00011 * and/or other materials provided with the distribution. 00012 * * Neither the name of Nokia Corporation nor the names of its contributors 00013 * may be used to endorse or promote products derived from this software 00014 * without specific prior written permission. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00017 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00018 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00020 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00021 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00022 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00023 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00024 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00025 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 * 00027 * Description: 00028 */ 00029 00030 00031 // INCLUDES 00032 #include "SharedIntermediator.h" 00033 #include "BluetoothInfo.h" 00034 #include "Devicelistcontainer.h" 00035 #include "BluetoothRefreshTimer.h" 00036 #include "ThreadAOAppUi.h" 00037 #include "btdiscoverer.h" 00038 00039 //CONSTANTS 00040 const TInt KRefreshTime = 20; 00041 00042 // ---------------------------------------------------------------------------- 00043 // CSharedIntermediator::CSharedIntermediator(CDeviceListContainer* 00044 // aDeviceListContainer) 00045 // 00046 // Constructor. 00047 // ---------------------------------------------------------------------------- 00048 CSharedIntermediator::CSharedIntermediator( 00049 CDeviceListContainer* aDeviceListContainer) 00050 : iDeviceListContainer(aDeviceListContainer), 00051 iStopSearching(false), 00052 iBTRefreshTimer(NULL), 00053 iInititialRefreshTime( KRefreshTime ) 00054 { 00055 } 00056 00057 // ---------------------------------------------------------------------------- 00058 // CSharedIntermediator::~CSharedIntermediator() 00059 // 00060 // Destructor. 00061 // ---------------------------------------------------------------------------- 00062 CSharedIntermediator::~CSharedIntermediator() 00063 { 00064 iMutex.Close(); 00065 if( iBluetoothInfoArray ) 00066 { 00067 iBluetoothInfoArray->Reset(); 00068 } 00069 delete iBluetoothInfoArray; 00070 } 00071 00072 CSharedIntermediator* CSharedIntermediator::NewL(CDeviceListContainer* aView) 00073 { 00074 CSharedIntermediator* self = CSharedIntermediator::NewLC(aView); 00075 CleanupStack::Pop(); 00076 return self; 00077 } 00078 00079 CSharedIntermediator* CSharedIntermediator::NewLC(CDeviceListContainer* aView) 00080 { 00081 CSharedIntermediator* self = new (ELeave) CSharedIntermediator(aView); 00082 CleanupStack::PushL(self); 00083 self->ConstructL(); 00084 return self; 00085 } 00086 00087 // Standard Symbian OS 2nd phase constructor 00088 void CSharedIntermediator::ConstructL() 00089 { 00090 iBluetoothInfoArray = new (ELeave) CArrayFixFlat <TBluetoothInfo>(1); 00091 iMutex.CreateLocal(); 00092 } 00093 00094 // ---------------------------------------------------------------------------- 00095 // CSharedIntermediator::AddBluetoothInfoL(TBluetoothInfo& btInfoElement) 00096 // 00097 // Add bluetooth info into bluetooth info array and add's new bluetooth 00098 // device's name into UI's list box. 00099 // ---------------------------------------------------------------------------- 00100 void CSharedIntermediator::AddBluetoothInfoL(TBluetoothInfo& aBtInfoElement) 00101 { 00102 iMutex.Wait(); 00103 00104 // Add new device data into array 00105 iBluetoothInfoArray->AppendL(aBtInfoElement); 00106 00107 TBuf <KBTDeviceLength> deviceName; 00108 aBtInfoElement.GetDeviceName(deviceName); 00109 // Show new device name in the list box. 00110 iDeviceListContainer->AddItemL(deviceName); 00111 00112 iMutex.Signal(); 00113 } 00114 00115 // ---------------------------------------------------------------------------- 00116 // CSharedIntermediator::ResetArray() 00117 // 00118 // Clears bluetooth info array. 00119 // ---------------------------------------------------------------------------- 00120 void CSharedIntermediator::ResetArray() 00121 { 00122 iMutex.Wait(); 00123 iBluetoothInfoArray->Reset(); 00124 iMutex.Signal(); 00125 } 00126 00127 // ---------------------------------------------------------------------------- 00128 // CSharedIntermediator::SetRefreshTimerInitlVal(TInt aRefreshTime) 00129 // 00130 // Set thread one's CBluetoothrefreshtimer initial value. 00131 // ---------------------------------------------------------------------------- 00132 void CSharedIntermediator::SetRefreshTimerInitlVal(TInt aRefreshTime) 00133 { 00134 if ( aRefreshTime < KRefreshTimeMin ) 00135 { 00136 aRefreshTime = KRefreshTimeDefault; 00137 } 00138 00139 iInititialRefreshTime = aRefreshTime; 00140 } 00141 00142 // ---------------------------------------------------------------------------- 00143 // CSharedIntermediator::RefreshTimerInitlVal() 00144 // 00145 // Get thread one's CBluetoothrefreshtimer initial value. 00146 // ---------------------------------------------------------------------------- 00147 TInt CSharedIntermediator::RefreshTimerInitlVal() 00148 { 00149 return iInititialRefreshTime; 00150 } 00151 00152 // ---------------------------------------------------------------------------- 00153 // CSharedIntermediator::GetAddress(TDes& aAddress, TInt aIndex) 00154 // 00155 // Get bluetooth device address from the bluetooth info array. 00156 // ---------------------------------------------------------------------------- 00157 void CSharedIntermediator::GetAddress(TDes& aAddress, TInt aIndex) 00158 { 00159 iMutex.Wait(); 00160 if ( aIndex < iBluetoothInfoArray->Count() ) 00161 { 00162 TBluetoothInfo& tempBTInfo = iBluetoothInfoArray->At(aIndex); 00163 tempBTInfo.GetAddressHex(aAddress); 00164 } 00165 iMutex.Signal(); 00166 } 00167 00168 // ---------------------------------------------------------------------------- 00169 // CSharedIntermediator::SetStopSearching(bool aStopSearhing) 00170 // 00171 // Set variable (iStopSearching), which tells to thread 00172 // that Activescheduler's wait loop should stop. After that thread cleans up 00173 // itself, the program is terminated "naturally". 00174 // ---------------------------------------------------------------------------- 00175 void CSharedIntermediator::SetStopSearching(bool aStopSearhing) 00176 { 00177 iStopSearching = aStopSearhing; 00178 } 00179 00180 00181 // ---------------------------------------------------------------------------- 00182 // TBool CSharedIntermediator::StopSearching() 00183 // 00184 // ---------------------------------------------------------------------------- 00185 TBool CSharedIntermediator::StopSearching() 00186 { 00187 return iStopSearching; 00188 } 00189 00190 // ---------------------------------------------------------------------------- 00191 // CSharedIntermediator::BTRefreshTimerPtr() 00192 // 00193 // Get pointer to thread's bluetooth refresh timer. New refresh time is set 00194 // in main program. Refresh time is time that has passed after bluetooth 00195 // discovery is started again. 00196 // ---------------------------------------------------------------------------- 00197 CBluetoothRefreshTimer* CSharedIntermediator::BTRefreshTimerPtr() 00198 { 00199 return iBTRefreshTimer; 00200 } 00201 00202 // ---------------------------------------------------------------------------- 00203 // CSharedIntermediator::SetBTRefreshTimerPtr(CBluetoothRefreshTimer* timer) 00204 // 00205 // Set pointer to thread's bluetooth refresh timer. New refresh time is set 00206 // in the main program by the user. 00207 // ---------------------------------------------------------------------------- 00208 void CSharedIntermediator::SetBTRefreshTimerPtr(CBluetoothRefreshTimer* aTimer) 00209 { 00210 iBTRefreshTimer = aTimer; 00211 } 00212 00213 // ---------------------------------------------------------------------------- 00214 // CSharedIntermediator::DeviceListContainer() 00215 // 00216 // Get pointer to listbox container. Function is used 00217 // when CBTRefreshTimer clears the listbox. 00218 // ---------------------------------------------------------------------------- 00219 CDeviceListContainer* CSharedIntermediator::DeviceListContainer() 00220 { 00221 return iDeviceListContainer; 00222 } 00223