LdmrsNtpTimeApp.cpp
Go to the documentation of this file.
00001 //
00002 // LdmrsNtpTimeApp.cpp
00003 //
00004 // Demonstrates setting and receiving the NTP timestamp of the
00005 // LDMRS. Sets the timestamp to Jan 1st, 2000.
00006 // Note that during startup, the LD-MRS device class already sets the current
00007 // local time.
00008 // This feature is available in all firmware versions.
00009 //
00010 // The App starts a thread that, after some time, sets the new time in the scanner.
00011 //
00012 // All changes to the sensor configuration are non-permanent.
00013 //
00014 
00015 #include "LdmrsNtpTimeApp.hpp"
00016 #include "../tools/errorhandler.hpp"    // for printInfoMessage()
00017 #include "../tools/toolbox.hpp"                 // for toString()
00018 #include "../tools/MathToolbox.hpp"
00019 #include "../datatypes/Scan.hpp"
00020 #include "../datatypes/Object.hpp"
00021 #include "../datatypes/Msg.hpp"
00022 #include "../datatypes/Measurement.hpp"
00023 #include "../datatypes/Fields.hpp"
00024 #include "../datatypes/EvalCases.hpp"
00025 #include "../datatypes/EvalCaseResults.hpp"
00026 #include "../devices/LD_MRS.hpp"
00027 #include <time.h>
00028 
00029 namespace application
00030 {
00031 
00032 //
00033 // Constructor
00034 //
00035 LdmrsNtpTimeApp::LdmrsNtpTimeApp(Manager* manager) :
00036                 m_manager(manager)
00037 {
00038         m_beVerbose = true;
00039         
00040         // Start the thread that sets the time.
00041         if (m_changeThread.isRunning() == false)
00042         {
00043                 m_changeThread.run(this);
00044         }
00045         
00046         printInfoMessage("LdmrsNtpTimeApp constructor done.", m_beVerbose);
00047 }
00048 
00049 
00050 // Destructor
00051 // Clean up all dynamic data structures
00052 LdmrsNtpTimeApp::~LdmrsNtpTimeApp()
00053 {
00054         printInfoMessage("LdmrsNtpTimeApp says Goodbye!", m_beVerbose);
00055 }
00056 
00057 
00058 //
00059 // Receiver for new data from the manager.
00060 //
00061 void LdmrsNtpTimeApp::setData(BasicData& data)
00062 {
00063         // we are just interested in scan data
00064         switch (data.getDatatype())
00065         {
00066                 case Datatype_Scan:
00067                 {
00068                         // Print the scan start timestamp (UTC time)
00069                         Scan* scan = dynamic_cast<Scan*>(&data);
00070                         const ScannerInfo* info = scan->getScannerInfoByDeviceId(1);
00071 
00072                         if (info != NULL)
00073                         {
00074                                 const Time& time = info->getStartTimestamp();
00075                                 printInfoMessage("LdmrsApp::setData(): Scan start time: " + time.toLongString(), m_beVerbose);
00076                         }
00077                         break;
00078                 }
00079                 default:
00080                         break;
00081         }
00082 }
00083 
00084 
00085 //
00086 // Convert UNIX system time (seconds since 1.1.1970) to NTP time (secons since 1.1.1900).
00087 //
00088 ntp_time_t LdmrsNtpTimeApp::convertUnixTimeToNtpTime(struct timeval& unixTime)
00089 {
00090         ntp_time_t ntpTime;
00091         ntpTime.second = unixTime.tv_sec + 0x83AA7E80;
00092         ntpTime.fraction = (uint32_t)( (double)(unixTime.tv_usec+1) * (double)(1LL<<32) * 1.0e-6 );
00093         return ntpTime;
00094 }
00095 
00096 //
00097 // Thread that does the actual changing of parameters.
00098 //
00099 void LdmrsNtpTimeApp::changeThreadFunction(bool& endThread, UINT16& waitTimeMs)
00100 {
00101         printInfoMessage("LdmrsNtpTimeApp::changeThreadFunction(): Started.", m_beVerbose);
00102         bool result;
00103 
00104         devices::LDMRS* ldmrs = dynamic_cast<devices::LDMRS*>(m_manager->getFirstDeviceByType(Sourcetype_LDMRS));
00105 
00106         // Ensure that we have a valid pointer to the device
00107         if (ldmrs != NULL)
00108         {
00109                 // Sleep some time to receive some scans first.
00110                 UINT32 sleepTimeMs = 2000;      // 2000 ms = 2 s
00111                 usleep(sleepTimeMs * 1000);
00112 
00113                 // Set the date and time to Jan 1st, 2000.
00114                 struct timeval now;
00115                 now.tv_sec = 946681200;                 // 1.1.2000
00116                 now.tv_usec = 0;                                // 0 microseconds
00117                 // Convert to NTP
00118                 ntp_time_t ntpTime = convertUnixTimeToNtpTime(now);
00119                 ntpTime.fraction = (uint32_t)0;
00120         
00121                 // Set the time
00122                 result = ldmrs->setNtpTime(ntpTime.second, ntpTime.fraction);
00123                 
00124                 if (result == true)
00125                 {
00126                         // Success
00127                         printInfoMessage("LdmrsNtpTimeApp::changeThreadFunction(): Successfully set the NTP time.", true);
00128                 }
00129                 else
00130                 {
00131                         // Failure
00132                         printError("LdmrsNtpTimeApp::changeThreadFunction(): Failed to set NTP time!");
00133                 }
00134         }
00135         else
00136         {
00137                 // We do not have a pointer to the sensor!
00138                 printError("LdmrsNtpTimeApp::changeThreadFunction():  Failed to get a valid pointer to the MRS, aborting!");
00139         }
00140                 
00141 
00142         endThread = true;
00143         waitTimeMs = 0;
00144         printInfoMessage("LdmrsNtpTimeApp::changeThreadFunction():  All done, leaving.", m_beVerbose);
00145 }
00146 
00147 }       // namespace application


libsick_ldmrs
Author(s): SICK AG , Martin Günther , Jochen Sprickerhof
autogenerated on Wed Jun 14 2017 04:04:50