LdmrsNtpTimeApp.cpp
Go to the documentation of this file.
1 //
2 // LdmrsNtpTimeApp.cpp
3 //
4 // Demonstrates setting and receiving the NTP timestamp of the
5 // LDMRS. Sets the timestamp to Jan 1st, 2000.
6 // Note that during startup, the LD-MRS device class already sets the current
7 // local time.
8 // This feature is available in all firmware versions.
9 //
10 // The App starts a thread that, after some time, sets the new time in the scanner.
11 //
12 // All changes to the sensor configuration are non-permanent.
13 //
14 
15 #include "LdmrsNtpTimeApp.hpp"
16 #include "../tools/errorhandler.hpp" // for printInfoMessage()
17 #include "../tools/toolbox.hpp" // for toString()
18 #include "../tools/MathToolbox.hpp"
19 #include "../datatypes/Scan.hpp"
20 #include "../datatypes/Object.hpp"
21 #include "../datatypes/Msg.hpp"
22 #include "../datatypes/Measurement.hpp"
23 #include "../datatypes/Fields.hpp"
24 #include "../datatypes/EvalCases.hpp"
25 #include "../datatypes/EvalCaseResults.hpp"
26 #include "../devices/LD_MRS.hpp"
27 #include <time.h>
28 
29 namespace application
30 {
31 
32 //
33 // Constructor
34 //
36  m_manager(manager)
37 {
38  m_beVerbose = true;
39 
40  // Start the thread that sets the time.
41  if (m_changeThread.isRunning() == false)
42  {
43  m_changeThread.run(this);
44  }
45 
46  printInfoMessage("LdmrsNtpTimeApp constructor done.", m_beVerbose);
47 }
48 
49 
50 // Destructor
51 // Clean up all dynamic data structures
53 {
54  printInfoMessage("LdmrsNtpTimeApp says Goodbye!", m_beVerbose);
55 }
56 
57 
58 //
59 // Receiver for new data from the manager.
60 //
62 {
63  // we are just interested in scan data
64  switch (data.getDatatype())
65  {
66  case Datatype_Scan:
67  {
68  // Print the scan start timestamp (UTC time)
69  Scan* scan = dynamic_cast<Scan*>(&data);
70  const ScannerInfo* info = scan->getScannerInfoByDeviceId(1);
71 
72  if (info != NULL)
73  {
74  const Time& time = info->getStartTimestamp();
75  printInfoMessage("LdmrsApp::setData(): Scan start time: " + time.toLongString(), m_beVerbose);
76  }
77  break;
78  }
79  default:
80  break;
81  }
82 }
83 
84 
85 //
86 // Convert UNIX system time (seconds since 1.1.1970) to NTP time (secons since 1.1.1900).
87 //
89 {
90  ntp_time_t ntpTime;
91  ntpTime.second = unixTime.tv_sec + 0x83AA7E80;
92  ntpTime.fraction = (uint32_t)( (double)(unixTime.tv_usec+1) * (double)(1LL<<32) * 1.0e-6 );
93  return ntpTime;
94 }
95 
96 //
97 // Thread that does the actual changing of parameters.
98 //
99 void LdmrsNtpTimeApp::changeThreadFunction(bool& endThread, UINT16& waitTimeMs)
100 {
101  printInfoMessage("LdmrsNtpTimeApp::changeThreadFunction(): Started.", m_beVerbose);
102  bool result;
103 
105 
106  // Ensure that we have a valid pointer to the device
107  if (ldmrs != NULL)
108  {
109  // Sleep some time to receive some scans first.
110  UINT32 sleepTimeMs = 2000; // 2000 ms = 2 s
111  usleep(sleepTimeMs * 1000);
112 
113  // Set the date and time to Jan 1st, 2000.
114  struct timeval now;
115  now.tv_sec = 946681200; // 1.1.2000
116  now.tv_usec = 0; // 0 microseconds
117  // Convert to NTP
118  ntp_time_t ntpTime = convertUnixTimeToNtpTime(now);
119  ntpTime.fraction = (uint32_t)0;
120 
121  // Set the time
122  result = ldmrs->setNtpTime(ntpTime.second, ntpTime.fraction);
123 
124  if (result == true)
125  {
126  // Success
127  printInfoMessage("LdmrsNtpTimeApp::changeThreadFunction(): Successfully set the NTP time.", true);
128  }
129  else
130  {
131  // Failure
132  printError("LdmrsNtpTimeApp::changeThreadFunction(): Failed to set NTP time!");
133  }
134  }
135  else
136  {
137  // We do not have a pointer to the sensor!
138  printError("LdmrsNtpTimeApp::changeThreadFunction(): Failed to get a valid pointer to the MRS, aborting!");
139  }
140 
141 
142  endThread = true;
143  waitTimeMs = 0;
144  printInfoMessage("LdmrsNtpTimeApp::changeThreadFunction(): All done, leaving.", m_beVerbose);
145 }
146 
147 } // namespace application
void printError(std::string message)
uint16_t UINT16
ntp_time_t convertUnixTimeToNtpTime(struct timeval &unixTime)
Definition: Time.hpp:44
devices::BasicDevice * getFirstDeviceByType(Sourcetype type)
Definition: manager.cpp:412
#define printInfoMessage(a, b)
uint32_t UINT32
LdmrsNtpTimeApp(Manager *manager)
const Time & getStartTimestamp() const
Definition: ScannerInfo.hpp:73
void changeThreadFunction(bool &endThread, UINT16 &waitTimeMs)
const ScannerInfo * getScannerInfoByDeviceId(UINT8 id) const
Definition: Scan.cpp:360
bool setNtpTime(UINT32 seconds, UINT32 fractionalSec)
Definition: LD_MRS.cpp:306
std::string toLongString() const
Definition: Time.cpp:51
void setData(BasicData &data)
SickThread< LdmrsNtpTimeApp,&LdmrsNtpTimeApp::changeThreadFunction > m_changeThread


libsick_ldmrs
Author(s): SICK AG , Martin Günther , Jochen Sprickerhof
autogenerated on Sat Jun 8 2019 17:57:33