|
0
|
1 |
/*
|
|
|
2 |
* Copyright (c) 2006 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 |
|
|
|
19 |
#include <e32cons.h>
|
|
|
20 |
#include <f32file.h>
|
|
|
21 |
|
|
|
22 |
#include "TrkConnData.h"
|
|
|
23 |
#include "TrkDccCommPort.h"
|
|
|
24 |
#include "TrkFramingLayer.h"
|
|
|
25 |
|
|
|
26 |
_LIT(KSendErrMsg, "Failed to write to the DCC channel");
|
|
|
27 |
|
|
|
28 |
//
|
|
|
29 |
// Default comm values
|
|
|
30 |
//
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
//
|
|
|
34 |
// Static helper functions
|
|
|
35 |
//
|
|
|
36 |
|
|
|
37 |
//
|
|
|
38 |
//
|
|
|
39 |
// CTrkDccCommPort implementation
|
|
|
40 |
//
|
|
|
41 |
//
|
|
|
42 |
|
|
|
43 |
//
|
|
|
44 |
// CTrkDccCommPort constructor
|
|
|
45 |
//
|
|
|
46 |
CTrkDccCommPort::CTrkDccCommPort()
|
|
|
47 |
: CTrkCommPort(CActive::EPriorityStandard),
|
|
|
48 |
iConnected(EFalse),
|
|
|
49 |
iListening(EFalse),
|
|
|
50 |
iLineNumber(0),
|
|
|
51 |
iLddLoaded(EFalse)
|
|
|
52 |
{
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
//
|
|
|
56 |
// CTrkDccCommPort::ConstructL
|
|
|
57 |
//
|
|
|
58 |
void CTrkDccCommPort::ConstructL(TTrkConnData& aTrkConnData, TDes& aErrorMessage)
|
|
|
59 |
{
|
|
|
60 |
|
|
|
61 |
iLDD = aTrkConnData.iLDD;
|
|
|
62 |
iUnitNumber = aTrkConnData.iPortNumber;
|
|
|
63 |
|
|
|
64 |
if (!iLDD.Size())
|
|
|
65 |
{
|
|
|
66 |
aErrorMessage.Format(_L("LDD not specified in init file\r\n"));
|
|
|
67 |
User::Leave(KErrCorrupt);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
//
|
|
|
74 |
// CTrkDccCommPort destructor
|
|
|
75 |
//
|
|
|
76 |
CTrkDccCommPort::~CTrkDccCommPort()
|
|
|
77 |
{
|
|
|
78 |
if (iLddLoaded)
|
|
|
79 |
{
|
|
|
80 |
//cancel the read request
|
|
|
81 |
iDccDriver.ReadCancel();
|
|
|
82 |
iDccDriver.Close();
|
|
|
83 |
|
|
|
84 |
TInt err = User::FreeLogicalDevice(iLDD);
|
|
|
85 |
if (KErrNone != err)
|
|
|
86 |
User::Panic(_L("FreeLogicalDevice failed"), err);
|
|
|
87 |
|
|
|
88 |
iLddLoaded = EFalse;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
//
|
|
|
94 |
// CTrkDccCommPort::NewL
|
|
|
95 |
//
|
|
|
96 |
CTrkDccCommPort* CTrkDccCommPort::NewL(TTrkConnData& aTrkConnData, TDes& aErrorMessage)
|
|
|
97 |
{
|
|
|
98 |
CTrkDccCommPort* self = new(ELeave) CTrkDccCommPort;
|
|
|
99 |
CleanupStack::PushL(self);
|
|
|
100 |
self->ConstructL(aTrkConnData, aErrorMessage);
|
|
|
101 |
CleanupStack::Pop(self);
|
|
|
102 |
return self;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
//
|
|
|
106 |
// CTrkDccCommPort::OpenPortL
|
|
|
107 |
//
|
|
|
108 |
// Open the serial type communications port
|
|
|
109 |
//
|
|
|
110 |
void CTrkDccCommPort::OpenPortL()
|
|
|
111 |
{
|
|
|
112 |
TInt error = KErrNone;
|
|
|
113 |
|
|
|
114 |
// make sure the LDD is not already loaded
|
|
|
115 |
User::FreeLogicalDevice(iLDD);
|
|
|
116 |
|
|
|
117 |
error = User::LoadLogicalDevice(iLDD);
|
|
|
118 |
if ((KErrAlreadyExists != error) && (KErrNone != error))
|
|
|
119 |
ReportAndLeaveIfErrorL(error, _L("Failed to load dcc logical device."));
|
|
|
120 |
|
|
|
121 |
error = iDccDriver.Open();
|
|
|
122 |
if (KErrNone != error)
|
|
|
123 |
ReportAndLeaveIfErrorL(error, _L("Failed to open dcc logical device."));
|
|
|
124 |
|
|
|
125 |
iLddLoaded = ETrue;
|
|
|
126 |
|
|
|
127 |
|
|
|
128 |
_LIT(KDCCMSG, "LDD: TrkDccDriver\r\nPort number: 42\r\n");
|
|
|
129 |
|
|
|
130 |
iConnectionMessage = KDCCMSG;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
//
|
|
|
134 |
// CTrkDccCommPort::ClosePort
|
|
|
135 |
//
|
|
|
136 |
// Close the communications port
|
|
|
137 |
//
|
|
|
138 |
void CTrkDccCommPort::ClosePort()
|
|
|
139 |
{
|
|
|
140 |
Cancel();
|
|
|
141 |
|
|
|
142 |
if (iLddLoaded)
|
|
|
143 |
{
|
|
|
144 |
//cancel the read request
|
|
|
145 |
iDccDriver.ReadCancel();
|
|
|
146 |
iDccDriver.Close();
|
|
|
147 |
|
|
|
148 |
TInt err = User::FreeLogicalDevice(iLDD);
|
|
|
149 |
if (KErrNone != err)
|
|
|
150 |
User::Panic(_L("FreeLogicalDevice failed"), err);
|
|
|
151 |
|
|
|
152 |
iLddLoaded = EFalse;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
//
|
|
|
158 |
// CTrkDccCommPort::SendDataL
|
|
|
159 |
//
|
|
|
160 |
// Write data to the serial type port
|
|
|
161 |
//
|
|
|
162 |
void CTrkDccCommPort::SendDataL(const TDesC8& aBuffer)
|
|
|
163 |
{
|
|
|
164 |
TInt err = iDccDriver.Write(aBuffer);
|
|
|
165 |
|
|
|
166 |
ReportAndLeaveIfErrorL(err, KSendErrMsg);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
//
|
|
|
170 |
// CTrkDccCommPort::Listen
|
|
|
171 |
//
|
|
|
172 |
// Start listening for data coming into the serial type communications port
|
|
|
173 |
//
|
|
|
174 |
void CTrkDccCommPort::Listen(CTrkFramingLayer *aFramingLayer)
|
|
|
175 |
{
|
|
|
176 |
iFramingLayer = aFramingLayer;
|
|
|
177 |
CActiveScheduler::Add(this);
|
|
|
178 |
IssueReadRequest();
|
|
|
179 |
iListening = ETrue;
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
//
|
|
|
183 |
// CTrkDccCommPort::StopListening
|
|
|
184 |
//
|
|
|
185 |
// Stop listening for data coming into the serial type communications port
|
|
|
186 |
//
|
|
|
187 |
void CTrkDccCommPort::StopListening()
|
|
|
188 |
{
|
|
|
189 |
if (iListening)
|
|
|
190 |
{
|
|
|
191 |
Cancel();
|
|
|
192 |
Deque();
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
iListening = EFalse;
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
//
|
|
|
199 |
// CTrkDccCommPort::ReportAndLeaveIfErrorL
|
|
|
200 |
//
|
|
|
201 |
// If an error occurred, print the error information to the screen and bail out
|
|
|
202 |
//
|
|
|
203 |
void CTrkDccCommPort::ReportAndLeaveIfErrorL(TInt aError, const TDesC& aDesc)
|
|
|
204 |
{
|
|
|
205 |
if (KErrNone != aError)
|
|
|
206 |
{
|
|
|
207 |
iErrorMessage.Format(_L("%S\r\nError Code: %d\r\n"), &aDesc, aError);
|
|
|
208 |
User::Leave(aError);
|
|
|
209 |
}
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
|
|
|
213 |
//
|
|
|
214 |
// CTrkDccCommPort::IssueReadRequest
|
|
|
215 |
//
|
|
|
216 |
// Wait for data to come into the communications port
|
|
|
217 |
//
|
|
|
218 |
void CTrkDccCommPort::IssueReadRequest()
|
|
|
219 |
{
|
|
|
220 |
iNextReadChar = 0;
|
|
|
221 |
iDccDriver.ReadOneOrMore(iStatus, iReceivedChars);
|
|
|
222 |
SetActive();
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
//
|
|
|
226 |
// CTrkDccCommPort::DoCancel
|
|
|
227 |
//
|
|
|
228 |
// Cancel the request for data from the communications port
|
|
|
229 |
//
|
|
|
230 |
void CTrkDccCommPort::DoCancel()
|
|
|
231 |
{
|
|
|
232 |
iDccDriver.ReadCancel();
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
//
|
|
|
236 |
// CTrkDccCommPort::RunL
|
|
|
237 |
//
|
|
|
238 |
// Called when data comes into the communications port
|
|
|
239 |
//
|
|
|
240 |
void CTrkDccCommPort::RunL()
|
|
|
241 |
{
|
|
|
242 |
// pass the data onto the framing layer
|
|
|
243 |
if (iStatus.Int() == KErrNone)
|
|
|
244 |
{
|
|
|
245 |
while (iNextReadChar < iReceivedChars.Length())
|
|
|
246 |
iFramingLayer->HandleByte(iReceivedChars[iNextReadChar++]);
|
|
|
247 |
//iDccDriver.Write(iReceivedChars);
|
|
|
248 |
}
|
|
|
249 |
// continue waiting for data
|
|
|
250 |
IssueReadRequest();
|
|
|
251 |
}
|
|
|
252 |
|