Advertisement
Artem78

LbsPositionRequestor.cpp

Sep 24th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.62 KB | None | 0 0
  1. // INCLUDE FILES
  2. #include <Lbs.h>
  3. #include "LBSPositionRequestor.h"
  4. #include "LBSPositionlistener.h"
  5.  
  6. // CONSTANTS
  7.  
  8. //Second
  9. const TInt KSecond = 1000000;
  10.  
  11. //Update interval
  12. const TInt KUpdateInterval = KSecond;
  13.  
  14. //Update time out
  15. const TInt KUpdateTimeOut = 15*KSecond;
  16.  
  17. //MaxAge
  18. const TInt KMaxAge = 500000;
  19.  
  20. // Unknown string used when module name is not known
  21. _LIT(KUnknown,"Unknown");
  22.  
  23. //The name of the requestor
  24. _LIT(KRequestor,"Location Reference Application");
  25.  
  26. // Event messages
  27. _LIT(KLastPosUnknown,"Last known position unknown.");
  28.  
  29. //Error messages
  30. _LIT(KLbsErrLocRequest,     "Request error: %d.");
  31. _LIT(KLbsErrAccess,         "Access denied: %d.");
  32. _LIT(KLbsErrPosServConn,    "Connecting to server: %d.");
  33. _LIT(KLbsErrOpenPos,        "Opening a positioner: %d.");
  34. _LIT(KLbsErrSetRequestor,   "Setting requestor: %d.");
  35. _LIT(KLbsErrSetUpOpt,       "Setting update options: %d.");
  36. _LIT(KLbsErrCanceled,       "Location request canceled.");
  37. _LIT(KLbsErrQualityLoss,    "KPositionQualityLoss.");
  38. _LIT(KLbsErrTimedout,       "Timed out.");
  39.  
  40. // ============================ MEMBER FUNCTIONS ===============================
  41.  
  42. // -----------------------------------------------------------------------------
  43. // CLbsPositionRequestor::CLbsPositionRequestor
  44. // C++ default constructor can NOT contain any code, that
  45. // might leave.
  46. // -----------------------------------------------------------------------------
  47. //
  48. CLbsPositionRequestor::CLbsPositionRequestor( MLbsPositionListener& aPositionListener )
  49.     : CActive(CActive::EPriorityStandard),
  50.     iPositionListener( aPositionListener ),
  51.     iPosInfoBase( &iPositionInfo ),
  52.     iGettingLastknownPosition( ETrue )
  53.     {
  54.     // Set update interval to one second to receive one position data per second
  55.     iUpdateops.SetUpdateInterval(TTimeIntervalMicroSeconds(KUpdateInterval));
  56.  
  57.     // If position server could not get position
  58.     // In two minutes it will terminate the position request
  59.     iUpdateops.SetUpdateTimeOut(TTimeIntervalMicroSeconds(KUpdateTimeOut));
  60.  
  61.     // Positions which have time stamp below KMaxAge can be reused
  62.     iUpdateops.SetMaxUpdateAge(TTimeIntervalMicroSeconds(KMaxAge));
  63.  
  64.     // Enables location framework to send partial position data
  65.     iUpdateops.SetAcceptPartialUpdates(ETrue);
  66.  
  67.     // Add this position requestor to the active scheduler
  68.     CActiveScheduler::Add( this );
  69.     }
  70.  
  71. // -----------------------------------------------------------------------------
  72. // CLbsPositionRequestor::ConstructL
  73. // Symbian 2nd phase constructor can leave.
  74. // -----------------------------------------------------------------------------
  75. //
  76. void CLbsPositionRequestor::ConstructL()
  77.     {
  78.     // Initialise the position request sequence
  79.     DoInitialiseL();
  80.     }
  81.  
  82. // -----------------------------------------------------------------------------
  83. // CLbsPositionRequestor::NewL
  84. // Two-phased constructor.
  85. // -----------------------------------------------------------------------------
  86. //
  87. CLbsPositionRequestor* CLbsPositionRequestor::NewL(
  88.                                         MLbsPositionListener& aPositionListener )
  89.     {
  90.     //Create the object
  91.     CLbsPositionRequestor* self = new( ELeave ) CLbsPositionRequestor(
  92.         aPositionListener);
  93.  
  94.     //Push to the cleanup stack
  95.     CleanupStack::PushL( self );
  96.  
  97.     //Construct the object
  98.     self->ConstructL();
  99.  
  100.     //Remove from cleanup stack
  101.     CleanupStack::Pop( self );
  102.  
  103.     //Return pointer to the created object
  104.     return self;
  105.     }
  106.  
  107. // -----------------------------------------------------------------------------
  108. // CLbsPositionRequestor::~CLbsPositionRequestor
  109. // Destructor
  110. // -----------------------------------------------------------------------------
  111. //
  112. CLbsPositionRequestor::~CLbsPositionRequestor()
  113.     {
  114.     // Cancel active object
  115.     Cancel();
  116.  
  117.     // Close the positioner
  118.     iPositioner.Close();
  119.  
  120.     // Close the session to the position server
  121.     iPosServer.Close();
  122.     }
  123.  
  124. // -----------------------------------------------------------------------------
  125. // DoCancel() implements CActive DoCancel()
  126. // Implements the cancellation of an outstanding request.
  127. // -----------------------------------------------------------------------------
  128. //
  129. void CLbsPositionRequestor::DoCancel()
  130.     {
  131.     // When a client application wishes to close one of its connections to Location
  132.     // Server, there can be no outstanding requests on that particular connection
  133.     // If a client application attempts to close a connection before outstanding
  134.     // requests have been cancelled or completed, it is panicked
  135.  
  136.     //If we are getting the last known position
  137.     if ( iGettingLastknownPosition )
  138.         {
  139.         //Cancel the last known position request
  140.         iPositioner.CancelRequest(EPositionerGetLastKnownPosition);
  141.         }
  142.     else
  143.         {
  144.         iPositioner.CancelRequest(EPositionerNotifyPositionUpdate);
  145.         }
  146.     }
  147.  
  148. // -----------------------------------------------------------------------------
  149. // RunL() implements CActive RunL()
  150. // Handles an active object’s request completion event.
  151. // -----------------------------------------------------------------------------
  152. //
  153. void CLbsPositionRequestor::RunL()
  154.     {
  155.     TBuf<KPositionMaxModuleName> buffer;
  156.    
  157.     switch ( iStatus.Int() )
  158.         {
  159.         // The fix is valid
  160.         case KErrNone:
  161.         // The fix has only partially valid information.
  162.         // It is guaranteed to only have a valid timestamp
  163.         case KPositionPartialUpdate:
  164.             {
  165.  
  166.             // Pre process the position information
  167.             PositionUpdatedL();
  168.  
  169.             break;
  170.             }
  171.         // The data class used was not supported
  172.         case KErrArgument:
  173.             {
  174.             // Set info base to position info
  175.             iPosInfoBase = &iPositionInfo;
  176.  
  177.              // Request next position
  178.             iPositioner.NotifyPositionUpdate( *iPosInfoBase, iStatus );
  179.  
  180.             // Set this object active
  181.             SetActive();
  182.             break;
  183.             }
  184.         // The position data could not be delivered
  185.         case KPositionQualityLoss:
  186.             {
  187.             // Send event to position listener
  188.             iPositionListener.ShowErrorL( KLbsErrQualityLoss );
  189.  
  190.             if ( iGettingLastknownPosition )
  191.                 {
  192.                 //Change the data class type
  193.                 iPosInfoBase = &iSatelliteInfo;
  194.                 }
  195.  
  196.             // Request position
  197.             iPositioner.NotifyPositionUpdate( *iPosInfoBase, iStatus );
  198.  
  199.             // Set this object active
  200.             SetActive();
  201.             break;
  202.             }
  203.         // Access is denied
  204.         case KErrAccessDenied:
  205.             {
  206.             // Send error to position listener
  207.             buffer.Format(KLbsErrAccess, iStatus.Int());
  208.             iPositionListener.ShowErrorL(buffer);
  209.             break;
  210.             }
  211.         // Request timed out
  212.         case KErrTimedOut:
  213.             {
  214.             // Send error to position listener
  215.             iPositionListener.ShowErrorL(KLbsErrTimedout);
  216.  
  217.             if ( iGettingLastknownPosition )
  218.                 {
  219.                 //Change the data class type
  220.                 iPosInfoBase = &iSatelliteInfo;
  221.                 }
  222.  
  223.             // Request position
  224.             iPositioner.NotifyPositionUpdate( *iPosInfoBase, iStatus );
  225.  
  226.             // Set this object active
  227.             SetActive();
  228.             break;
  229.             }
  230.         // The request was canceled
  231.         case KErrCancel:
  232.             {
  233.             // Send error to position listener
  234.             iPositionListener.ShowErrorL( KLbsErrCanceled );
  235.             break;
  236.             }
  237.         // There is no last known position
  238.         case KErrUnknown:
  239.             {
  240.             // Send event to position listener
  241.             iPositionListener.ShowMessageL(KLastPosUnknown);
  242.  
  243.             if ( iGettingLastknownPosition )
  244.                 {
  245.                 //Change the data class type
  246.                 iPosInfoBase = &iSatelliteInfo;
  247.  
  248.                 //Mark that we are not requesting NotifyPositionUpdate
  249.                 iGettingLastknownPosition = EFalse;
  250.                 }
  251.  
  252.             // Request next position
  253.             iPositioner.NotifyPositionUpdate( *iPosInfoBase, iStatus );
  254.  
  255.             // Set this object active
  256.             SetActive();
  257.             break;
  258.             }
  259.         // Unrecoverable errors.
  260.         default:
  261.             {
  262.             // Send error to position listener
  263.             buffer.Format(KLbsErrLocRequest, iStatus.Int());
  264.             iPositionListener.ShowErrorL( buffer );
  265.             break;
  266.             }
  267.         }
  268.  
  269.          //We are not going to query the last known position anymore.
  270.         if ( iGettingLastknownPosition )
  271.             {
  272.             //Mark that we are not requesting NotifyPositionUpdate
  273.             iGettingLastknownPosition = EFalse;
  274.             }
  275.     }
  276.  
  277.  
  278. // -----------------------------------------------------------------------------
  279. // CLbsPositionRequestor::DoInitialiseL
  280. // Initialises position server and positioner and
  281. // begins the position request sequence.
  282. // -----------------------------------------------------------------------------
  283. //
  284. void CLbsPositionRequestor::DoInitialiseL()
  285.     {
  286.     // Connect to the position server
  287.     TInt error = iPosServer.Connect( );
  288.     TBuf<100> buffer;
  289.    
  290.     // The connection failed
  291.     if ( KErrNone != error )
  292.         {
  293.         // Show error to the user and leave
  294.         buffer.Format(KLbsErrPosServConn, error);
  295.         iPositionListener.ShowErrorL( buffer );
  296.         return;
  297.         }
  298.  
  299.     // Open subsession to the position server
  300.     error = iPositioner.Open(iPosServer);
  301.  
  302.     // The opening of a subsession failed
  303.     if ( KErrNone != error )
  304.         {
  305.         // Show error to the user and leave
  306.         buffer.Format(KLbsErrOpenPos, error);
  307.         iPositionListener.ShowErrorL( buffer );
  308.         iPosServer.Close();
  309.         return;
  310.         }
  311.  
  312.     // Set position requestor
  313.     error = iPositioner.SetRequestor( CRequestor::ERequestorService ,
  314.          CRequestor::EFormatApplication , KRequestor );
  315.  
  316.     // The requestor could not be set
  317.     if ( KErrNone != error )
  318.         {
  319.         // Show error to the user and leave
  320.  
  321.         buffer.Format(KLbsErrSetRequestor, error);
  322.         iPositionListener.ShowErrorL( buffer );
  323.         iPositioner.Close();
  324.         iPosServer.Close();
  325.         return;
  326.         }
  327.  
  328.     // Set update options
  329.     error =  iPositioner.SetUpdateOptions( iUpdateops );
  330.  
  331.     // The options could not be updated
  332.     if ( KErrNone != error  )
  333.         {
  334.         // Show error to the user and leave
  335.         buffer.Format(KLbsErrSetUpOpt, error);
  336.         iPositionListener.ShowErrorL( buffer );
  337.         iPositioner.Close();
  338.         iPosServer.Close();
  339.         return;
  340.         }
  341.  
  342.     // Get last known position. The processing of the result
  343.     // is done in RunL method
  344.     iPositioner.GetLastKnownPosition(*iPosInfoBase,iStatus);
  345.  
  346.     // Set this active object active
  347.     SetActive();
  348.     }
  349.  
  350. // -----------------------------------------------------------------------------
  351. // CLbsPositionRequestor::GetModuleName
  352. // Gets the name of the positioning module by id.
  353. // -----------------------------------------------------------------------------
  354. //
  355. void CLbsPositionRequestor::GetModuleName(const TPositionModuleId& aModuleId)
  356.     {
  357.     // Module information of the position module
  358.     TPositionModuleInfo modInfo;
  359.  
  360.     // Get module info by module id
  361.     TInt error = iPosServer.GetModuleInfoById( *&aModuleId, modInfo );
  362.  
  363.     // If error occured
  364.     if ( KErrNone != error )
  365.         {
  366.         // Set the name of the module to 'Unknown'
  367.         iModuleName = KUnknown;
  368.         }
  369.     else
  370.         {
  371.         // Get the name of the position module
  372.         modInfo.GetModuleName(iModuleName);
  373.         }
  374.     }
  375.  
  376.  
  377. // -----------------------------------------------------------------------------
  378. // CLbsPositionRequestor::PositionUpdatedL
  379. // Pre process the position information
  380. // -----------------------------------------------------------------------------
  381. //
  382. void CLbsPositionRequestor::PositionUpdatedL()
  383.     {
  384.     //If we are getting the last known position
  385.     if ( iGettingLastknownPosition )
  386.         {
  387.         // Set the module name to 'Unknown' because last
  388.         // known position has no module name
  389.         iModuleName = KUnknown;
  390.  
  391.         // Send position information to registered listener
  392.         iPositionListener.PositionInfoUpdatedL(*iPosInfoBase,iModuleName);
  393.  
  394.         //Change the data class type
  395.         iPosInfoBase = &iSatelliteInfo;
  396.  
  397.         // Request next position
  398.         iPositioner.NotifyPositionUpdate( *iPosInfoBase, iStatus );
  399.  
  400.         // Set this object active
  401.         SetActive();
  402.         }
  403.     else
  404.         {
  405.         // Check if the id of the used PSY is 0
  406.         if ( 0 == iUsedPsy.iUid)
  407.             {
  408.             // Set the id of the currently used PSY
  409.             iUsedPsy = iPosInfoBase->ModuleId();
  410.             }
  411.         // Check if the position module has changed
  412.         else if ( iPosInfoBase->ModuleId() != iUsedPsy )
  413.             {
  414.             // Set the id of the currently used PSY
  415.             iUsedPsy = iPosInfoBase->ModuleId();
  416.  
  417.             //Position module info of new module
  418.             TPositionModuleInfo moduleInfo;
  419.  
  420.             // Get module info
  421.             iPosServer.GetModuleInfoById(iUsedPsy,moduleInfo);
  422.  
  423.             // Get classes supported
  424.             TInt32 moduleInfoFamily = moduleInfo.ClassesSupported(EPositionInfoFamily);
  425.  
  426.             iPosInfoBase = &iSatelliteInfo;
  427.  
  428.             // Check if the new module supports
  429.             // TPositionSatelliteInfo class
  430.             if ( EPositionSatelliteInfoClass & moduleInfoFamily )
  431.                 {
  432.                 // Set info base to satellite info
  433.                 iPosInfoBase = &iSatelliteInfo;
  434.                 }
  435.             // The position module must support atleast
  436.             // TPositionInfo class
  437.             else
  438.                 {
  439.                 // Set info base to position info
  440.                 iPosInfoBase = &iPositionInfo;
  441.                 }
  442.             }
  443.  
  444.         // Process the position information
  445.         // and request next position
  446.  
  447.         // Get module name
  448.         GetModuleName(iUsedPsy);
  449.  
  450.         // Send position information to registered listener
  451.         iPositionListener.PositionInfoUpdatedL(*iPosInfoBase,iModuleName);
  452.  
  453.         // Request next position
  454.         iPositioner.NotifyPositionUpdate( *iPosInfoBase, iStatus );
  455.  
  456.         // Set this object active
  457.         SetActive();
  458.         }
  459.     }
  460.  
  461. //  End of File
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement