MIP_SDK  v3.0.0
MicroStrain Communications Library for embedded systems
firmware_version.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdint.h>
4 #include <stddef.h>
5 
6 #include <string>
7 
8 #if __cpp_lib_string_view >= 201606L
9 #include <string_view>
10 #endif
11 
12 namespace mip
13 {
14 
26 {
27 public:
28  FirmwareVersion() = default;
29  FirmwareVersion(uint8_t major, uint8_t minor, uint8_t patch) { fromParts(major, minor, patch); }
30  explicit FirmwareVersion(uint16_t version) : m_version(version) {}
31 
32  FirmwareVersion(const FirmwareVersion&) = default;
33  FirmwareVersion& operator=(const FirmwareVersion&) = default;
34  FirmwareVersion& operator=(uint16_t version) { m_version = version; return *this; }
35 
36  bool isNull() const { return m_version == 0; }
37  bool isValid() const { return !isNull() && major() < 10 && minor() < 10 && patch() < 100; }
38  bool isDevVersion() const { return major() == 0; }
39  bool isReleaseVersion() const { return major() > 0; }
40  bool isSpecialVersion() const { return major() > 1; }
41 
42  uint16_t asU16() const { return m_version; }
43  uint16_t& asU16() { return m_version; }
44 
45  void fromParts(uint8_t major, uint8_t minor, uint8_t patch) { m_version = major * 1000 + minor * 100 + patch; }
46 
47  uint8_t major() const { return uint8_t(m_version / 1000); }
48  uint8_t minor() const { return (m_version / 100) % 10; }
49  uint8_t patch() const { return m_version % 100; }
50 
51  bool operator==(FirmwareVersion other) const { return m_version == other.m_version; }
52  bool operator!=(FirmwareVersion other) const { return m_version != other.m_version; }
53  bool operator<=(FirmwareVersion other) const { return m_version <= other.m_version; }
54  bool operator>=(FirmwareVersion other) const { return m_version >= other.m_version; }
55  bool operator< (FirmwareVersion other) const { return m_version < other.m_version; }
56  bool operator> (FirmwareVersion other) const { return m_version > other.m_version; }
57 
58  void toString(char* buffer, size_t buffer_size) const;
59  bool fromString(const char* str, size_t length=-1);
60 
61  std::string toString() const;
62 #if __cpp_lib_string_view >= 201606L
63  bool fromString(std::string_view str) { return fromString(str.data(), str.size()); }
64 #endif
65 
66 private:
67  uint16_t m_version = 0;
68 };
69 
70 } // namespace mip
mip::FirmwareVersion::isDevVersion
bool isDevVersion() const
Definition: firmware_version.hpp:38
mip
A collection of C++ classes and functions covering the full mip api.
Definition: commands_3dm.c:11
mip::FirmwareVersion::FirmwareVersion
FirmwareVersion()=default
mip::FirmwareVersion::FirmwareVersion
FirmwareVersion(uint16_t version)
Definition: firmware_version.hpp:30
mip::FirmwareVersion
Represents the device firmware version.
Definition: firmware_version.hpp:25
mip::FirmwareVersion::asU16
uint16_t asU16() const
Definition: firmware_version.hpp:42
mip::FirmwareVersion::toString
std::string toString() const
Convert a FirmwareVersion to a string separated by periods.
Definition: firmware_version.cpp:65
mip::FirmwareVersion::operator<=
bool operator<=(FirmwareVersion other) const
Definition: firmware_version.hpp:53
mip::FirmwareVersion::patch
uint8_t patch() const
Definition: firmware_version.hpp:49
mip::FirmwareVersion::fromString
bool fromString(const char *str, size_t length=-1)
Reads a standard-format string (X.Y.ZZ\0 or XYZZ\0).
Definition: firmware_version.cpp:31
mip::FirmwareVersion::operator==
bool operator==(FirmwareVersion other) const
Definition: firmware_version.hpp:51
mip::FirmwareVersion::major
uint8_t major() const
Definition: firmware_version.hpp:47
mip::FirmwareVersion::asU16
uint16_t & asU16()
Definition: firmware_version.hpp:43
mip::FirmwareVersion::fromParts
void fromParts(uint8_t major, uint8_t minor, uint8_t patch)
Definition: firmware_version.hpp:45
mip::FirmwareVersion::operator>=
bool operator>=(FirmwareVersion other) const
Definition: firmware_version.hpp:54
mip::FirmwareVersion::operator!=
bool operator!=(FirmwareVersion other) const
Definition: firmware_version.hpp:52
mip::FirmwareVersion::operator=
FirmwareVersion & operator=(uint16_t version)
Definition: firmware_version.hpp:34
mip::FirmwareVersion::isReleaseVersion
bool isReleaseVersion() const
Definition: firmware_version.hpp:39
mip::FirmwareVersion::operator>
bool operator>(FirmwareVersion other) const
Definition: firmware_version.hpp:56
mip::FirmwareVersion::FirmwareVersion
FirmwareVersion(uint8_t major, uint8_t minor, uint8_t patch)
Definition: firmware_version.hpp:29
mip::FirmwareVersion::isSpecialVersion
bool isSpecialVersion() const
Definition: firmware_version.hpp:40
mip::FirmwareVersion::minor
uint8_t minor() const
Definition: firmware_version.hpp:48
mip::FirmwareVersion::isNull
bool isNull() const
Definition: firmware_version.hpp:36
mip::FirmwareVersion::operator=
FirmwareVersion & operator=(const FirmwareVersion &)=default
mip::FirmwareVersion::isValid
bool isValid() const
Definition: firmware_version.hpp:37
mip::FirmwareVersion::operator<
bool operator<(FirmwareVersion other) const
Definition: firmware_version.hpp:55