landmarks/locationlandmarks/tsrc/LandmarkTestModule/inc/lcfsbucommondefinitions.h
branchRCL_3
changeset 44 2b4ea9893b66
equal deleted inserted replaced
42:02ba3f1733c6 44:2b4ea9893b66
       
     1 /*
       
     2 * Copyright (c) 2009 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 #ifndef _LCF_SBU_COMMON_DEFINITIONS_H_
       
    19 #define _LCF_SBU_COMMON_DEFINITIONS_H_
       
    20 
       
    21 ///////////////////////////////////////////////////////
       
    22 //
       
    23 // SBu Common Definitions
       
    24 //
       
    25 ///////////////////////////////////////////////////////
       
    26 
       
    27 /** 
       
    28     $File: LcfSBuCommonDefitions.h
       
    29     $Author: burnevsk $
       
    30     $Date: 2004/10/18 08:14:23 $
       
    31     $Revision: 1.6 $
       
    32 */
       
    33 
       
    34 ///////////////////////////////////////////////////////
       
    35 //
       
    36 // Constants
       
    37 //
       
    38 ///////////////////////////////////////////////////////
       
    39 
       
    40 
       
    41 #include <e32def.h>
       
    42 #include <e32std.h>
       
    43 #include <e32err.h>
       
    44 
       
    45 #define DEF_LCFPSY1_MODULEID        0x03133002
       
    46 #define DEF_LCFPSY2_MODULEID        0x03133004
       
    47 #define DEF_LCFPSY3_MODULEID        0x03133006
       
    48 #define DEF_LCFPSYSLOW4_MODULEID    0x03133008
       
    49 #define DEF_LCFPSY6_MODULEID        0x0313300c
       
    50 
       
    51 const TInt KLcfPsy1UidValue = DEF_LCFPSY1_MODULEID;
       
    52 const TInt KLcfPsy3UidValue = DEF_LCFPSY3_MODULEID;
       
    53 const TInt KLcfPsy6UidValue = DEF_LCFPSY6_MODULEID;
       
    54 
       
    55 const TInt KSecondsToMicro = 1000*1000;
       
    56 
       
    57 // Show service formt
       
    58 _LIT(KShowServiceFormat, "Name: %S \nType: %d \n  Id: %S");
       
    59 
       
    60 // time value formatting
       
    61 _LIT(KTimeSpec, "%H:%T:%S:%C");
       
    62 
       
    63 // default requestors
       
    64 _LIT(KServiceName, "Lcf.Service");
       
    65 _LIT(KServiceId, "Lcf.ServiceId");
       
    66 
       
    67 _LIT(KContactName, "Lcf.Contact");
       
    68 _LIT(KPhone, "+79217654321");
       
    69 
       
    70 _LIT(KFieldNamePhone, "Phone");
       
    71 _LIT(KFieldNameEmail, "Email");
       
    72 _LIT(KFieldNameUrl, "Url");
       
    73 
       
    74 ///////////////////////////////////////////////////////
       
    75 //
       
    76 // Other text
       
    77 //
       
    78 ///////////////////////////////////////////////////////
       
    79 
       
    80 // Indicate not yet implemented
       
    81 _LIT(KNotImplemented, "Not yet implemented");   
       
    82 
       
    83 //_LIT(KBadRequestResult, "Wrong location request result (result=%d, expected=%d)");
       
    84 //_LIT(KBadDiagNum, "Wrong number of events in Notifier (count=%d, expected=%d)");
       
    85 //_LIT(KBadLcfResult, "Wrong notifier event result (result=%d, expected=%d)");
       
    86 
       
    87 _LIT(KBadDiagVerifEventNum, "Wrong number of Verification events in Notifier");
       
    88 _LIT(KBadDiagNotifEventNum, "Wrong number of Notification events in Notifier");
       
    89 
       
    90 ///////////////////////////////////////////////////////
       
    91 //
       
    92 // Here are macros for error logging 
       
    93 // Those imply that are called from within of 
       
    94 // CUtfwTestBase-derived class's function (they use iLog member)
       
    95 //
       
    96 ///////////////////////////////////////////////////////
       
    97 
       
    98 // LEAVES with _error_ if _condition_ is true
       
    99 #define LEAVE(condition, error)             \
       
   100     if (condition) {                        \
       
   101         errStr.Format(_L("%s (line %d)"), _S(error), __LINE__);    \
       
   102         iLog->Log(errStr);			\
       
   103         User::Leave(KErrGeneral);           	\
       
   104     }
       
   105 
       
   106 // if _error_ is descritor
       
   107 #define LEAVE_DES(condition, error_des)     \
       
   108     if (condition) {                        \
       
   109         errStr.Format(_L("%S (line %d)"), &error_des, __LINE__);    \
       
   110         iLog->Log(errStr);					\
       
   111         User::Leave(KErrGeneral);            \
       
   112     }
       
   113 
       
   114 // Must be included in the begging of function definition
       
   115 // in order to use following ("ERROR") macros
       
   116 // (The string buffer needs to be defined once, otherwise,
       
   117 // in case when a lot of errors are checked, stack may be
       
   118 // overflown, and copilation will fail)
       
   119 #define DECLARE_ERROR_STR                   TBuf<160> errStr;
       
   120 #define DECLARE_MESSAGE_STR                 TBuf<160> message;
       
   121 
       
   122 #define DECLARE_ERROR_LOGGING               DECLARE_ERROR_STR   DECLARE_MESSAGE_STR
       
   123 
       
   124 #define START_ERROR_LOGGING                 iErrorCounter = 0;
       
   125 
       
   126 // Logs a message, which is defined as "..." constant
       
   127 #define LOG(msg)                            \
       
   128     {                                       \
       
   129         errStr.Format(_L("%s (line %d)"), _S(msg), __LINE__);    \
       
   130         iLog->Log(errStr);                  \
       
   131     }
       
   132 #define LOG_DES(msg_des)                    \
       
   133     {                                       \
       
   134         errStr.Format(_L("%S (line %d)"), &msg_des, __LINE__);    \
       
   135         iLog->Log(errStr);                  \
       
   136     }
       
   137     
       
   138 #define LOG_FORMAT_1(_format, _param)       \
       
   139     message.Format(_format, _param);        \
       
   140     LOG_DES(message);
       
   141     
       
   142 #define LOG_FORMAT_2(_format, _param1, _param2)       \
       
   143     message.Format(_format, _param, _param2);         \
       
   144     LOG_DES(message);
       
   145 
       
   146 #define LOG_IIF(condition, msg1, msg2)      \
       
   147     if (condition) LOG(msg1) else LOG(msg2)
       
   148 
       
   149 // Logs an error, which is defined as "..." constant
       
   150 #define ERR(error)                          \
       
   151     {                                       \
       
   152         errStr.Format(_L("%s (line %d)"), _S(error), __LINE__);    \
       
   153         iLog->Log(errStr);             \
       
   154         iErrorCounter++;                    \
       
   155     }
       
   156 #define ERR_DES(error)                      \
       
   157     {                                       \
       
   158         errStr.Format(_L("%S (line %d)"), &error, __LINE__);    \
       
   159         iLog->Log(errStr);             \
       
   160         iErrorCounter++;                    \
       
   161     }
       
   162 
       
   163 // Logs an _error_ if _condition_ is true
       
   164 #define ERROR(condition, error)             \
       
   165     if (condition) ERR(error)
       
   166 
       
   167 // Logs an error, which defined as descriptor
       
   168 #define ERROR_DES(condition, error_des)     \
       
   169     if (condition) ERR_DES(error_des)
       
   170 
       
   171 #define ERROR_1(condition, msg_fmt_des, param1)  \
       
   172     message.Format(msg_fmt_des, param1);        \
       
   173     ERROR_DES(condition, message);
       
   174 
       
   175 #define ERROR_2(condition, msg_fmt_des, param1, param2)  \
       
   176     message.Format(msg_fmt_des, param1, param2);        \
       
   177     ERROR_DES(condition, message);
       
   178 
       
   179 #define ERROR_3(condition, msg_fmt_des, param1, param2, param3) \
       
   180     message.Format(msg_fmt_des, param1, param2, param3);        \
       
   181     ERROR_DES(condition, message);
       
   182 
       
   183 #define IF_ERROR(error_code, msg)                               \
       
   184     { TInt32 _err = error_code;                                 \
       
   185     ERROR_2(_err != KErrNone, _L("%s (error=%d)"), _S(msg), _err); }
       
   186 
       
   187 #define IF_ERROR_DES(error_code, msg_des)                       \
       
   188     { TInt32 _err = error_code;                                 \
       
   189     ERROR_2(_err != KErrNone, _L("%S (error=%d)"), &msg_des, _err); }
       
   190 
       
   191 #define CHECK_EQUAL(_actual, _expected, _msg)                     \
       
   192     ERROR_3(_actual != _expected, _L("%s (actual=%d, expected=%d)"), _S(_msg), _actual, _expected);
       
   193 
       
   194 #define CHECK_EQUAL_DES(_actual, _expected, _msg_des)           \
       
   195     ERROR_3(_actual != _expected, _L("%S (actual=%d, expected=%d)"), &_msg_des, _actual, _expected);
       
   196 
       
   197 // Leaves if there were errors generated by ERROR macros
       
   198 // (put it at the end of you test function)
       
   199 #define LEAVE_IF_ERRORS()               \
       
   200     if (iErrorCounter > 0) {             \
       
   201         errStr.Format(_L("There were %d errors in the test, see previous messages!"), iErrorCounter);    \
       
   202         iLog->Log(errStr); \
       
   203           User::Leave(KErrGeneral);  \
       
   204     }
       
   205 
       
   206 #define PAUSE_MS(_microseconds)                                 \
       
   207     User::After(TTimeIntervalMicroSeconds32(_microseconds));
       
   208 #define PAUSE(_seconds)                                         \
       
   209     PAUSE_MS(_seconds * KSecondsToMicro)
       
   210 
       
   211 class TWatch 
       
   212 {
       
   213     public:
       
   214         TWatch() { RestartWatch(); };
       
   215         void RestartWatch() { iTime.UniversalTime(); iLastTick = iTime; };
       
   216         void Tick() { iLastTick.UniversalTime(); };
       
   217         TTimeIntervalMicroSeconds ElapsedTotal()
       
   218         {
       
   219             TTime now;
       
   220             now.UniversalTime();
       
   221             return now.MicroSecondsFrom(iTime);
       
   222         };
       
   223         TTimeIntervalMicroSeconds ElapsedFromTick()
       
   224         {
       
   225             TTime now;
       
   226             now.UniversalTime();
       
   227             return now.MicroSecondsFrom(iLastTick);
       
   228         };
       
   229         TTime Time() {return iTime; };
       
   230     private:
       
   231         TTime iTime;
       
   232         TTime iLastTick;
       
   233 };
       
   234 
       
   235 class TThreadUtility
       
   236 {
       
   237     public:
       
   238         typedef void (*TThreadFunctionL)(TAny* aPtr);
       
   239 
       
   240         struct TOptions
       
   241         {
       
   242             TThreadFunctionL    ThreadFunctionL;
       
   243             TAny*               parameters; 
       
   244         };
       
   245 
       
   246         static TInt ThreadFunction(TAny* aPtr)
       
   247         {
       
   248             CTrapCleanup* cs = CTrapCleanup::New();
       
   249             if (cs == NULL)
       
   250             {
       
   251                 return KErrNoMemory;
       
   252             }
       
   253 
       
   254             // new ActiveScheduler
       
   255             CActiveScheduler* scheduler = new(ELeave) CActiveScheduler;
       
   256             CActiveScheduler::Install(scheduler);
       
   257 
       
   258             TOptions* options = reinterpret_cast<TOptions*>(aPtr);
       
   259             TRAPD(err, (*options->ThreadFunctionL)(options->parameters));
       
   260 
       
   261             delete scheduler;
       
   262             delete cs;
       
   263             User::Exit(err);
       
   264             return err;
       
   265         };
       
   266 
       
   267         void CreateL(const TDesC& aThreadName, TOptions* aOptions)
       
   268         {
       
   269             iName.Set(aThreadName);
       
   270             User::LeaveIfError( iThread.Create(aThreadName,
       
   271                                 TThreadUtility::ThreadFunction,
       
   272                                 KDefaultStackSize,
       
   273                                 KMinHeapSize,
       
   274                                 10 * KMinHeapSize,
       
   275                                 aOptions,
       
   276                                 EOwnerProcess));
       
   277         };
       
   278         void CreateReuseHeapL(const TDesC& aThreadName, TOptions* aOptions)
       
   279         {
       
   280             iName.Set(aThreadName);
       
   281             User::LeaveIfError( iThread.Create(aThreadName,
       
   282                                 TThreadUtility::ThreadFunction,
       
   283                                 KDefaultStackSize,
       
   284                                 &User::Heap(),
       
   285                                 aOptions,
       
   286                                 EOwnerProcess));
       
   287         };
       
   288         void Start()
       
   289         {
       
   290             iThread.Resume();
       
   291         };
       
   292         void Stop()
       
   293         {
       
   294             iThread.Suspend();
       
   295         };
       
   296 
       
   297         TInt Wait()
       
   298         {
       
   299             TRequestStatus threadStatus;
       
   300             if (iThread.ExitType() == EExitPending) // still executing
       
   301             {
       
   302                 iThread.Logon(threadStatus);
       
   303                 User::WaitForRequest(threadStatus);
       
   304             }
       
   305             return iThread.ExitReason();
       
   306         };
       
   307 
       
   308         const RThread Handle()
       
   309         {
       
   310             return iThread;
       
   311         };
       
   312 
       
   313         void Format(TDes& aMessage)
       
   314         {
       
   315             TExitCategoryName category = iThread.ExitCategory();
       
   316             _LIT(KThreadFormat, "Thread %S, Reason %d, ExitType %d, ExitName %S");
       
   317             aMessage.Format(KThreadFormat, 
       
   318                 &iName, iThread.ExitReason(), iThread.ExitType(), &category);
       
   319         };
       
   320 
       
   321         // used if class destroyed
       
   322         void Close()
       
   323         {
       
   324             if (iThread.Handle())
       
   325             {
       
   326                 if (iThread.ExitType() == EExitPending) // still executing
       
   327                 {
       
   328                     iThread.Kill(KErrNone);
       
   329                 }
       
   330                 iThread.Close();
       
   331             }
       
   332         };
       
   333 
       
   334     protected:
       
   335         RThread iThread;
       
   336         TPtrC   iName;
       
   337 };
       
   338 
       
   339 #endif // _LCF_SBU_COMMON_DEFINITIONS_H_