Update contrib.
1 // Copyright (c) 2008-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 the License "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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\debug\crashMonitor\src\scmconfig.cpp
25 #include <e32const_private.h>
27 #include <scmconfig.h>
28 #include <scmconfigitem.h>
29 #include <scmdatatypes.h>
34 * SCMConfiguration constructor
35 * Initialises the configuration object to default values
37 SCMConfiguration::SCMConfiguration()
43 * SCMConfiguration destructor
45 SCMConfiguration::~SCMConfiguration()
51 * Goes to the flash and reads the configuration block and populates the object state
53 * @return one of the system wide error codes
55 TInt SCMConfiguration::Deserialize(TByteStreamReader& aReader)
59 // we need to set up a default configuration to load the data into
60 TInt err = SetDefaultConfig();
63 CLTRACE1("SCMConfiguration::Deserialize SetDefaultConfig failed err = %d", err);
68 TInt startPos = aReader.CurrentPosition();
70 TBuf8<10> magicNumbers;
71 // try and read the magic numbers - if they dont exist then
72 // there is not an scm config there
73 const TInt KLen = KScmConfigHeaderString().Length();
74 for(TInt i=0;i<KLen;i++)
76 TUint8 b = aReader.ReadByte();
77 magicNumbers.Append(TChar(b));
80 if(magicNumbers.Compare(KScmConfigHeaderString()) != 0)
82 CLTRACE("No scm, config to read !");
86 TConfigItem* item = iConfigList;
89 item->Deserialize(aReader);
93 TInt endPos = aReader.CurrentPosition();
94 if( endPos - startPos != GetSize())
96 // error between actual size & real size in data
97 CLTRACE("SCMConfiguration::Deserialize size error");
104 * This writes the current configuration object state to flash. This configuration will be used on the next crash
105 * @return one of the system wide error codes
107 TInt SCMConfiguration::Serialize(TByteStreamWriter& aWriter)
111 CLTRACE("SCMConfiguration::Serialize ERROR - NO LIST!!");
115 TInt startPos = aWriter.CurrentPosition();
117 // write the number of crashes and magic numbers
119 // try and read the magic numbers - if they dont exist then
120 // there is not an scm config there
121 const TInt KLen = KScmConfigHeaderString().Length();
122 const TDesC8& des = KScmConfigHeaderString();
123 for(TInt i=0;i<KLen;i++)
125 aWriter.WriteByte(des[i]);
128 TConfigItem* item = iConfigList;
131 item->Serialize(aWriter);
135 TInt endPos = aWriter.CurrentPosition();
136 if( endPos - startPos != GetSize())
138 // error between actual size & real size in data
139 CLTRACE("SCMConfiguration::Serialize size error");
146 * Returns entire size of the SCMConfiguration block
149 TInt SCMConfiguration::GetSize() const
151 // returns the size of all the config items when serialized to disk / flash
152 return (TConfigItem::ELast * iConfigList->GetSize()) + KScmConfigHeaderString().Length();
156 * This will return, one at a time, the highest priority.
157 * @see ResetToHighestPriority()
158 * @param aSizeToDump this will contain the size in bytes of data to dump for this type - 0 means dump all
159 * @return Data type to dump
161 TConfigItem* SCMConfiguration::GetNextItem()
168 //get the values we need
169 TConfigItem* item = iNextItem;
171 //Now move iNextItem to be next in the list
172 iNextItem = iNextItem->iNext;
177 * Deletes the linked list
178 * @return system wide OS code
180 void SCMConfiguration::ClearList()
187 //all we need to do in here is delete the members of our linked list
188 TConfigItem* item = iConfigList;
191 TConfigItem* tmp = item->iNext;
201 * Rather than reading the configuration from the flash, this method sets up the configuration object
202 * to a default configuration type
203 * @return one of the system wide error codes
205 TInt SCMConfiguration::SetDefaultConfig()
207 //flush the object first
210 //This is a predefined default config - in the future we may have multiple defaults based on use case
211 // currently however we use a fixed size list of config items of size TConfigItem::ELast
212 // also the TSCMDataType of each item must be unique
214 for(TInt cnt = TConfigItem::ELast - 1; cnt >= 0; cnt --)
216 TInt priority = cnt + 1;
218 //Lets not do these by default
219 if((TConfigItem::TSCMDataType)cnt == TConfigItem::EThreadsUsrStack || (TConfigItem::TSCMDataType)cnt == TConfigItem::EThreadsSvrStack)
224 //set it with the priority of its enum (ie. assume that the enum is listed in its priority - it is)
225 //by default dump everything until we run out of space
226 TInt err = CreateConfigItem((TConfigItem::TSCMDataType)cnt, priority, 0);
237 * This configures the required data for a given configuration item
238 * Note that aSizeToDump is only used in the case of memory dumps such as stacks
239 * @param aDataType - Type of data to dump
240 * @param aPriority - its priority 0-256. 0 Means do not dump and 256 is highest priority
241 * @param aSizeToDump - amount in bytes to dump. Only relevant for memory dumps and ignored when aPriority is 0
242 * @return one of the OS wide return codes
244 TInt SCMConfiguration::CreateConfigItem(const TConfigItem::TSCMDataType aDataType, const TUint8 aPriority, const TInt32 aSizeToDump)
246 //create the config item
247 TConfigItem* item = new TConfigItem(aDataType, aPriority, aSizeToDump);
249 //insert to priority list
250 return InsertToList(item);
255 * ModifyConfigItemPriority - modifies prioity for a given configuration item
256 * @param aDataType - The unique type of the config item
257 * @param aPriority - its priority 0-256. 0 Means do not dump and 256 is highest priority
258 * @return one of the OS wide return codes
260 TInt SCMConfiguration::ModifyConfigItemPriority(const TConfigItem::TSCMDataType aDataType, const TUint8 aPriority)
263 // find the item with the matching data type
264 TConfigItem* item = iConfigList;
267 if(item->iDataType == aDataType)
279 item->iPriority = aPriority;
281 // now reorder the list according to new priority
282 TInt err = RemoveFromList(item);
288 err = InsertToList(item);
299 * Removes item from the linked list
300 * @param aItem - item to remove
303 TInt SCMConfiguration::RemoveFromList(TConfigItem* aItem)
312 return KErrCorrupt; // oops no list to remove
316 if(aItem == iConfigList)
318 // special case remove from beginning of list
319 iConfigList = iConfigList->iNext;
323 TConfigItem* item = iConfigList;
326 // is the next item the match ?
327 if(item->iNext == aItem)
329 item->iNext = aItem->iNext;
339 * Inserts a priority item into the linked list in its correct location
340 * @param aItem - item to insert
343 TInt SCMConfiguration::InsertToList(TConfigItem* aItem)
346 //if the list is empty, then this is the only item
353 //should it go at the start? special case not covered by while loop
356 if(aItem->iPriority >= iConfigList->iPriority)
364 TConfigItem* item = iConfigList;
367 //if we get to the end of the list and the item still hasnt been assigned then it must be lowest priority
368 if(item->iNext == NULL)
374 //check if its priority is between these
375 if(aItem->iPriority < item->iPriority && aItem->iPriority >= item->iNext->iPriority)
377 //insert between these nodes
388 //should never get here
393 * This resets the next item counter back to the highest priority item in the list
395 void SCMConfiguration::ResetToHighestPriority()
397 //set the next item counter back to the head of the list
398 iNextItem = iConfigList;
402 * Overloaded == operator
403 * @param aOther Item to compare
406 TBool SCMConfiguration::operator == (const SCMConfiguration& aOther) const
409 if(!iConfigList && !aOther.iConfigList)
414 if((!iConfigList && aOther.iConfigList) || (iConfigList && !aOther.iConfigList))
420 TConfigItem* item1 = iConfigList;
421 TConfigItem* item2 = aOther.iConfigList;
423 while(item1 && item2)
425 if(!(*item1 == *item2))
430 item1 = item1->iNext;
431 item2 = item2->iNext;
434 if( item1 != item2) // both should now be null - if not then lists were different lengths
444 * Getter for the head of the SCMConfig list
445 * @return Head of List
447 TConfigItem* SCMConfiguration::ConfigList() const
449 // returns the head of the list
452 } //End of debug namespace