00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "threadserver.h"
00032 #include "threadserverstart.h"
00033 #include "threadserversession.h"
00034
00035 const TInt KThreadServerShutDownDelay = 50000000;
00036
00037
00038
00042 void CThreadServer::NewLC()
00043 {
00044 CThreadServer* s = new(ELeave) CThreadServer();
00045 CleanupStack::PushL(s);
00046 s->ConstructL();
00047 }
00051 CThreadServer::CThreadServer()
00052 :CServer2(EPriorityStandard, ESharableSessions),iDriverState(EStateUnknown)
00053 {
00054 }
00055 void CThreadServer::ConstructL()
00056 {
00057
00058
00059 StartL(KThreadServerName);
00060 iDelayThreadServerShutDown = CDelayServerShutDown::NewL();
00061 }
00065 CThreadServer::~CThreadServer()
00066 {
00067 if (iDelayThreadServerShutDown)
00068 {
00069 iDelayThreadServerShutDown->Cancel();
00070 delete iDelayThreadServerShutDown;
00071 }
00072
00073 UnloadDevice();
00074 }
00081 CSession2* CThreadServer::NewSessionL(const TVersion& aVersion,const RMessage2& ) const
00082 {
00083
00084 TVersion v(KThreadServerVersion,KThreadServerMinorVersionNumber,KThreadServerBuildVersionNumber);
00085 if(!User::QueryVersionSupported(v, aVersion))
00086 User::Leave(KErrNotSupported);
00087
00088
00089 CThreadServer& ncThis = const_cast<CThreadServer&>(*this);
00090 CThreadServerSession* serverSession = CThreadServerSession::NewL(ncThis);
00091 return serverSession;
00092 }
00096 void CThreadServer::IncrementRefCount()
00097 {
00098 iRefCount++;
00099 iDelayThreadServerShutDown->Cancel();
00100 }
00104 void CThreadServer::DecrementRefCount()
00105 {
00106 iRefCount--;
00107 if ( iRefCount == 0 )
00108 {
00109 iDelayThreadServerShutDown->SetDelay(TTimeIntervalMicroSeconds32(KThreadServerShutDownDelay));
00110 }
00111 }
00115 void CThreadServer::RenameServer()
00116 {
00117
00118 RThread serverThread;
00119 TThreadId threadId;
00120 TName name;
00121 name.Append(KThreadServerName);
00122 threadId = serverThread.Id();
00123 name.AppendNum(threadId.Id(),EHex);
00124
00125
00126 User::RenameThread(name);
00127 }
00132 TInt CThreadServer::LoadDevice()
00133 {
00134 if (iDriverState>=EDriverLoaded && iDriverState<EDriverUnloaded)
00135 return KErrNone;
00136
00137 TInt r=User::LoadPhysicalDevice(KThreadServerDriverPddFileName);
00138 if (r!=KErrNone && r!=KErrAlreadyExists)
00139 return r;
00140
00141 r = User::LoadLogicalDevice(KThreadServerDriverLddFileName);
00142 if (r!=KErrNone && r!=KErrAlreadyExists)
00143 return r;
00144
00145
00146 UpdateDriverState(EDriverLoaded);
00147 return KErrNone;
00148 }
00153 TInt CThreadServer::UnloadDevice()
00154 {
00155 if (iDriverState==EDriverUnloaded || iDriverState == EStateUnknown)
00156 return KErrNone;
00157
00158
00159 if (iDriver.Handle())
00160 iDriver.Close();
00161
00162 TInt r = User::FreeLogicalDevice(RDriver1::Name());
00163 if (r!=KErrNone)
00164 return r;
00165
00166 TName pddName(RDriver1::Name());
00167 _LIT(KVariantExtension,".template");
00168 pddName.Append(KVariantExtension);
00169 r=User::FreePhysicalDevice(pddName);
00170
00171 if (KErrNone==r)
00172 UpdateDriverState(EDriverUnloaded);
00173 return r;
00174 }
00179 TInt CThreadServer::OpenLogicalChannel()
00180 {
00181 if (iDriverState>=ELogicalChannelOpened && iDriverState<ELogicalChannelClosed)
00182 return KErrNone;
00183
00184 TInt r = iDriver.Open();
00185 if (KErrNone==r)
00186 UpdateDriverState(ELogicalChannelOpened);
00187
00188 return r;
00189 }
00193 void CThreadServer::CloseLogicalChannel()
00194 {
00195 if (iDriver.Handle())
00196 {
00197 iDriver.Close();
00198 UpdateDriverState(ELogicalChannelClosed);
00199 }
00200 }
00207 TInt CThreadServer::SendDataToDevice(TRequestStatus& aStatus, const TDesC8& aData)
00208 {
00209 TInt r = KErrNone;
00210 if (iDriverState>=ELogicalChannelOpened && iDriverState<ELogicalChannelClosed)
00211 {
00212 iDriver.SendData(aStatus, aData);
00213 UpdateDriverState(ESendingData);
00214 }
00215 else
00216 {
00217 r = KErrArgument;
00218 }
00219 return r;
00220 }
00224 void CThreadServer::CancelSendData()
00225 {
00226 iDriver.SendDataCancel();
00227 UpdateDriverState(ELogicalChannelOpened);
00228 }
00233 void CThreadServer::UpdateDriverState(TDriverState aState)
00234 {
00235 iDriverState = aState;
00236 }
00237