|
1 // Copyright (c) 2004-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 #include "hciloggerbtsnoop.h" |
|
17 |
|
18 static const TInt KSnoopLogFileHeaderSize = 16; // bytes |
|
19 static const TInt KSnoopLogRecordHeaderSize = 24; // bytes |
|
20 static const TUint KSnoopLogVersion = 1; |
|
21 |
|
22 static const TInt KHCILoggerFileCloseTime = 5000000; // 5 seconds |
|
23 |
|
24 _LIT8(KSnoopLogBlankDescriptor, ""); |
|
25 _LIT8(KSnoopLogFileHeaderFormat, "btsnoop\0%M%M"); |
|
26 _LIT8(KSnoopLogRecordHeaderFormat, "%M%M%M%M%M%M"); |
|
27 |
|
28 #ifdef __WINS__ |
|
29 _LIT(KSnoopLogFileH1, "\\shared\\hcilogger\\hcilog_H1.log"); |
|
30 _LIT(KSnoopLogFileH4, "\\shared\\hcilogger\\hcilog_H4.log"); |
|
31 _LIT(KSnoopLogFileBCSP, "\\shared\\hcilogger\\hcilog_Bcsp.log"); |
|
32 _LIT(KSnoopLogFileH5, "\\shared\\hcilogger\\hcilog_H5.log"); |
|
33 _LIT(KSnoopLogFileDefault, "\\shared\\hcilogger\\hcilog_unknown.log"); |
|
34 #else |
|
35 _LIT(KSnoopLogFileH1, "e:\\shared\\hcilogger\\hcilog_H1.log"); |
|
36 _LIT(KSnoopLogFileH4, "e:\\shared\\hcilogger\\hcilog_H4.log"); |
|
37 _LIT(KSnoopLogFileBCSP, "e:\\shared\\hcilogger\\hcilog_Bcsp.log"); |
|
38 _LIT(KSnoopLogFileH5, "e:\\shared\\hcilogger\\hcilog_H5.log"); |
|
39 _LIT(KSnoopLogFileDefault, "e:\\shared\\hcilogger\\hcilog_unknown.log"); |
|
40 #endif //__WINS__ |
|
41 |
|
42 #ifndef EKA2 |
|
43 GLDEF_C TInt E32Dll(TDllReason) |
|
44 #else |
|
45 GLDEF_C TInt E32Dll() |
|
46 #endif |
|
47 { |
|
48 return KErrNone; |
|
49 } |
|
50 |
|
51 CHCILoggerBtSnoop* CHCILoggerBtSnoop::NewLC() |
|
52 { |
|
53 CHCILoggerBtSnoop* self = new (ELeave) CHCILoggerBtSnoop; |
|
54 CleanupStack::PushL(self); |
|
55 self->ConstructL(); |
|
56 return self; |
|
57 } |
|
58 |
|
59 CHCILoggerBtSnoop::CHCILoggerBtSnoop() |
|
60 { |
|
61 } |
|
62 |
|
63 void CHCILoggerBtSnoop::ConstructL() |
|
64 { |
|
65 iFileCloseTimer = CHCILoggerFileCloseTimer::NewL(iLogFile); |
|
66 for (TInt i = 0; i <= TInt(KHCILoggerControllerToHost | KHCILoggerCommandOrEvent); i++) |
|
67 { |
|
68 CHCILoggerBufferedFrame* temp = new(ELeave) CHCILoggerBufferedFrame; |
|
69 iBuffers.Append(temp); // array now "owns" temp |
|
70 } |
|
71 } |
|
72 |
|
73 void CHCILoggerBtSnoop::DoInitialise(TInt aType) |
|
74 { |
|
75 iDatalinkType = aType; |
|
76 iFs.Connect(); |
|
77 OpenFile(); // if it fails, just ignore it - try later when writing |
|
78 } |
|
79 |
|
80 CHCILoggerBtSnoop::~CHCILoggerBtSnoop() |
|
81 { |
|
82 iBuffers.ResetAndDestroy(); |
|
83 iLogFile.Close(); |
|
84 iFs.Close(); |
|
85 delete iFileCloseTimer; |
|
86 } |
|
87 |
|
88 void CHCILoggerBtSnoop::LogFrame(TUint aFrameFlags, const TDesC8& aDesc) |
|
89 { |
|
90 TInt packetFlags = aFrameFlags & (KHCILoggerControllerToHost | KHCILoggerCommandOrEvent); |
|
91 CHCILoggerBufferedFrame* buffer = iBuffers[packetFlags]; |
|
92 if (aFrameFlags & KHCILoggerFrameFragmented) |
|
93 { |
|
94 buffer->BufferData(aDesc); |
|
95 return; |
|
96 } |
|
97 |
|
98 TBtSnoopPacketRecord record; |
|
99 record.iOriginalLength = aDesc.Length(); |
|
100 record.iIncludedLength = record.iOriginalLength; |
|
101 record.iPacketFlags = aFrameFlags; |
|
102 record.iCumulativeDrops = iCumulativeDrops; |
|
103 iCurrentTime.HomeTime(); |
|
104 record.iTimestampMicroseconds = iCurrentTime.Int64(); |
|
105 |
|
106 // if the file is not open, try to open it |
|
107 if (!iLogFile.SubSessionHandle()) |
|
108 { |
|
109 if (OpenFile() != KErrNone) |
|
110 { |
|
111 return; |
|
112 } |
|
113 } |
|
114 |
|
115 record.iOriginalLength += buffer->OriginalLength(); |
|
116 TDesC8& des = buffer->Buffer(); |
|
117 if (buffer->OriginalLength() > des.Length()) |
|
118 { // some data lost |
|
119 record.iIncludedLength = des.Length(); |
|
120 FileWriteRecord(record, des); |
|
121 } |
|
122 else |
|
123 { // no data lost |
|
124 record.iIncludedLength = record.iOriginalLength; |
|
125 FileWriteRecord(record, des, aDesc); |
|
126 } |
|
127 buffer->Reset(); |
|
128 } |
|
129 |
|
130 TInt CHCILoggerBtSnoop::OpenFile() |
|
131 { |
|
132 TPtrC fileName; |
|
133 switch (iDatalinkType) |
|
134 { |
|
135 case KHCILoggerDatalinkTypeH1: |
|
136 fileName.Set(KSnoopLogFileH1); |
|
137 break; |
|
138 case KHCILoggerDatalinkTypeH4: |
|
139 fileName.Set(KSnoopLogFileH4); |
|
140 break; |
|
141 case KHCILoggerDatalinkTypeBCSP: |
|
142 fileName.Set(KSnoopLogFileBCSP); |
|
143 break; |
|
144 case KHCILoggerDatalinkTypeH5: |
|
145 fileName.Set(KSnoopLogFileH5); |
|
146 break; |
|
147 default: |
|
148 fileName.Set(KSnoopLogFileDefault); |
|
149 break; |
|
150 }; |
|
151 |
|
152 TInt err = iLogFile.Open(iFs, fileName, EFileWrite | EFileShareAny); |
|
153 if (err == KErrNotFound) |
|
154 { // if it doesn't already exist, create it |
|
155 //Note that the potential KErrPathNotFound error is deliberately not handled, |
|
156 //since the shared\hcilogger directory not being present is used to indicate |
|
157 //that logging is not desired. |
|
158 err = iLogFile.Create(iFs, fileName, EFileWrite | EFileShareAny); |
|
159 } |
|
160 TInt size = 0; |
|
161 if (err == KErrNone) |
|
162 { |
|
163 err = iLogFile.Size(size); |
|
164 } |
|
165 if (err == KErrNone) |
|
166 { |
|
167 if (size < KSnoopLogFileHeaderSize) |
|
168 { |
|
169 err = FileWriteHeader(); // write will automatically start at pos 0, |
|
170 } // hence overwriting the file |
|
171 else |
|
172 { // want to append the data to the current file |
|
173 TInt writePos = KSnoopLogFileHeaderSize; // KSnoopLogFileHeaderSize is const, so can't pass in Seek() |
|
174 iLogFile.Seek(ESeekEnd, writePos); |
|
175 } |
|
176 } |
|
177 if (err) |
|
178 { |
|
179 iLogFile.Close(); // just in case |
|
180 } |
|
181 return err; |
|
182 } |
|
183 |
|
184 TInt CHCILoggerBtSnoop::FileWriteHeader() |
|
185 { |
|
186 TBuf8<KSnoopLogFileHeaderSize> buf; |
|
187 buf.Format(KSnoopLogFileHeaderFormat, KSnoopLogVersion, iDatalinkType); |
|
188 TInt err = iLogFile.Write(buf); |
|
189 iFileCloseTimer->ResetTimer(); |
|
190 return err; |
|
191 } |
|
192 |
|
193 void CHCILoggerBtSnoop::FileWriteRecord(const TBtSnoopPacketRecord& aRecord, const TDesC8& aBufferedData, const TDesC8& aNewData) |
|
194 { |
|
195 TBuf8<KSnoopLogRecordHeaderSize> buf; |
|
196 buf.Format(KSnoopLogRecordHeaderFormat, aRecord.iOriginalLength, |
|
197 aRecord.iIncludedLength, |
|
198 aRecord.iPacketFlags, |
|
199 aRecord.iCumulativeDrops, |
|
200 I64HIGH(aRecord.iTimestampMicroseconds), |
|
201 I64LOW(aRecord.iTimestampMicroseconds)); |
|
202 |
|
203 // if these fail, just ignore them |
|
204 iLogFile.Write(buf); // header |
|
205 iLogFile.Write(aBufferedData); // data |
|
206 iLogFile.Write(aNewData); // data |
|
207 iFileCloseTimer->ResetTimer(); |
|
208 } |
|
209 |
|
210 void CHCILoggerBtSnoop::FileWriteRecord(const TBtSnoopPacketRecord& aRecord, const TDesC8& aNewData) |
|
211 { |
|
212 FileWriteRecord(aRecord, KSnoopLogBlankDescriptor, aNewData); |
|
213 } |
|
214 |
|
215 // CHCILoggerFileCloseTimer functions // |
|
216 |
|
217 CHCILoggerFileCloseTimer* CHCILoggerFileCloseTimer::NewL(RFile& aLogFile) |
|
218 { |
|
219 CHCILoggerFileCloseTimer* self = new(ELeave) CHCILoggerFileCloseTimer(aLogFile); |
|
220 CleanupStack::PushL(self); |
|
221 self->ConstructL(); |
|
222 CleanupStack::Pop(self); |
|
223 return self; |
|
224 } |
|
225 |
|
226 CHCILoggerFileCloseTimer::CHCILoggerFileCloseTimer(RFile& aLogFile) |
|
227 : CTimer(EPriorityStandard), iLogFile(aLogFile) |
|
228 { |
|
229 } |
|
230 |
|
231 void CHCILoggerFileCloseTimer::ConstructL() |
|
232 { |
|
233 CTimer::ConstructL(); |
|
234 CActiveScheduler::Add(this); |
|
235 } |
|
236 |
|
237 void CHCILoggerFileCloseTimer::ResetTimer() |
|
238 { |
|
239 if (IsActive()) |
|
240 { |
|
241 Cancel(); |
|
242 } |
|
243 After(TTimeIntervalMicroSeconds32(KHCILoggerFileCloseTime)); |
|
244 } |
|
245 |
|
246 void CHCILoggerFileCloseTimer::RunL() |
|
247 { |
|
248 iLogFile.Close(); |
|
249 } |
|
250 |
|
251 void CHCILoggerFileCloseTimer::DoCancel() |
|
252 { |
|
253 CTimer::DoCancel(); |
|
254 } |
|
255 |
|
256 CHCILoggerFileCloseTimer::~CHCILoggerFileCloseTimer() |
|
257 { |
|
258 Cancel(); |
|
259 } |
|
260 |
|
261 CHCILoggerBufferedFrame::CHCILoggerBufferedFrame() |
|
262 { |
|
263 iBuffer = HBufC8::New(0); |
|
264 } |
|
265 |
|
266 CHCILoggerBufferedFrame::~CHCILoggerBufferedFrame() |
|
267 { |
|
268 delete iBuffer; |
|
269 } |
|
270 |
|
271 void CHCILoggerBufferedFrame::BufferData(const TDesC8& aDesc) |
|
272 { |
|
273 if (iOriginalLength == iBuffer->Length()) |
|
274 { // then no allocations have failed so far |
|
275 TInt newLength = iOriginalLength + aDesc.Length(); |
|
276 // add aDesc to buffer |
|
277 HBufC8* buf = iBuffer; |
|
278 if(buf != NULL) |
|
279 { |
|
280 if(newLength > buf->Des().MaxLength()) |
|
281 { |
|
282 buf = buf->ReAlloc(newLength); |
|
283 } |
|
284 if(buf != NULL) |
|
285 { // successful allocation |
|
286 buf->Des().Append(aDesc); |
|
287 iBuffer = buf; |
|
288 } |
|
289 // else if it failed, iBuffer stays the same |
|
290 } |
|
291 } |
|
292 iOriginalLength += aDesc.Length(); // add this on anyway |
|
293 } |
|
294 |
|
295 void CHCILoggerBufferedFrame::Reset() |
|
296 { |
|
297 iBuffer->Des().Zero(); |
|
298 iOriginalLength = 0; |
|
299 } |
|
300 |
|
301 TInt CHCILoggerBufferedFrame::OriginalLength() const |
|
302 { |
|
303 return iOriginalLength; |
|
304 } |
|
305 |
|
306 TDesC8& CHCILoggerBufferedFrame::Buffer() const |
|
307 { |
|
308 return *iBuffer; |
|
309 } |
|
310 |