brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include "aos/common/queue.h" |
| 2 | |
Brian | 4a424a2 | 2014-04-02 11:52:45 -0700 | [diff] [blame] | 3 | #include <inttypes.h> |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame^] | 4 | #include <chrono> |
| 5 | |
| 6 | #include "aos/common/byteorder.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 7 | |
| 8 | namespace aos { |
| 9 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame^] | 10 | namespace chrono = ::std::chrono; |
| 11 | |
| 12 | void Message::Zero() { sent_time = monotonic_clock::min_time; } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 13 | |
| 14 | size_t Message::Deserialize(const char *buffer) { |
| 15 | int32_t sec; |
| 16 | int32_t nsec; |
| 17 | to_host(&buffer[0], &sec); |
| 18 | to_host(&buffer[4], &nsec); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame^] | 19 | sent_time = monotonic_clock::time_point(chrono::seconds(sec) + |
| 20 | chrono::nanoseconds(nsec)); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 21 | return Size(); |
| 22 | } |
| 23 | // Serializes the common fields into the buffer. |
| 24 | size_t Message::Serialize(char *buffer) const { |
| 25 | // TODO(aschuh): to_network shouldn't need a pointer. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame^] | 26 | int32_t sec = |
| 27 | chrono::duration_cast<chrono::seconds>(sent_time.time_since_epoch()) |
| 28 | .count(); |
| 29 | int32_t nsec = chrono::duration_cast<chrono::nanoseconds>( |
| 30 | sent_time.time_since_epoch() - chrono::seconds(sec)) |
| 31 | .count(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 32 | to_network(&sec, &buffer[0]); |
| 33 | to_network(&nsec, &buffer[4]); |
| 34 | return Size(); |
| 35 | } |
| 36 | |
| 37 | size_t Message::Print(char *buffer, int length) const { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame^] | 38 | int32_t sec = |
| 39 | chrono::duration_cast<chrono::seconds>(sent_time.time_since_epoch()) |
| 40 | .count(); |
| 41 | int32_t nsec = chrono::duration_cast<chrono::nanoseconds>( |
| 42 | sent_time.time_since_epoch() - chrono::seconds(sec)) |
| 43 | .count(); |
| 44 | return snprintf(buffer, length, "%" PRId32 ".%09" PRId32 "s", sec, nsec); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | } // namespace aos |