libuav original

Dependents:   UAVCAN UAVCAN_Subscriber

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers data_type.hpp Source File

data_type.hpp

00001 /*
00002  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
00003  */
00004 
00005 #ifndef UAVCAN_DATA_TYPE_HPP_INCLUDED
00006 #define UAVCAN_DATA_TYPE_HPP_INCLUDED
00007 
00008 #include <cassert>
00009 #include <cstring>
00010 #include <uavcan/std.hpp>
00011 #include <uavcan/build_config.hpp>
00012 #include <uavcan/transport/transfer.hpp>
00013 
00014 namespace uavcan
00015 {
00016 
00017 class UAVCAN_EXPORT TransferCRC;
00018 
00019 enum DataTypeKind
00020 {
00021     DataTypeKindService,
00022     DataTypeKindMessage
00023 };
00024 
00025 static const uint8_t NumDataTypeKinds = 2;
00026 
00027 
00028 static inline DataTypeKind getDataTypeKindForTransferType(const TransferType tt)
00029 {
00030     if (tt == TransferTypeServiceResponse ||
00031         tt == TransferTypeServiceRequest)
00032     {
00033         return DataTypeKindService;
00034     }
00035     else if (tt == TransferTypeMessageBroadcast)
00036     {
00037         return DataTypeKindMessage;
00038     }
00039     else
00040     {
00041         UAVCAN_ASSERT(0);
00042         return DataTypeKind(0);
00043     }
00044 }
00045 
00046 
00047 class UAVCAN_EXPORT DataTypeID
00048 {
00049     uint32_t value_;
00050 
00051 public:
00052     static const uint16_t MaxServiceDataTypeIDValue = 255;
00053     static const uint16_t MaxMessageDataTypeIDValue = 65535;
00054     static const uint16_t MaxPossibleDataTypeIDValue = MaxMessageDataTypeIDValue;
00055 
00056     DataTypeID() : value_(0xFFFFFFFFUL) { }
00057 
00058     DataTypeID(uint16_t id)  // Implicit
00059         : value_(id)
00060     { }
00061 
00062     static DataTypeID getMaxValueForDataTypeKind(const DataTypeKind dtkind);
00063 
00064     bool isValidForDataTypeKind(DataTypeKind dtkind) const
00065     {
00066         return value_ <= getMaxValueForDataTypeKind(dtkind).get();
00067     }
00068 
00069     uint16_t get() const { return static_cast<uint16_t>(value_); }
00070 
00071     bool operator==(DataTypeID rhs) const { return value_ == rhs.value_; }
00072     bool operator!=(DataTypeID rhs) const { return value_ != rhs.value_; }
00073 
00074     bool operator<(DataTypeID rhs) const { return value_ < rhs.value_; }
00075     bool operator>(DataTypeID rhs) const { return value_ > rhs.value_; }
00076     bool operator<=(DataTypeID rhs) const { return value_ <= rhs.value_; }
00077     bool operator>=(DataTypeID rhs) const { return value_ >= rhs.value_; }
00078 };
00079 
00080 
00081 /**
00082  * CRC-64-WE
00083  * Description: http://reveng.sourceforge.net/crc-catalogue/17plus.htm#crc.cat-bits.64
00084  * Initial value: 0xFFFFFFFFFFFFFFFF
00085  * Poly: 0x42F0E1EBA9EA3693
00086  * Reverse: no
00087  * Output xor: 0xFFFFFFFFFFFFFFFF
00088  * Check: 0x62EC59E3F1A4F00A
00089  */
00090 class UAVCAN_EXPORT DataTypeSignatureCRC
00091 {
00092     uint64_t crc_;
00093 
00094 public:
00095     static DataTypeSignatureCRC extend(uint64_t crc);
00096 
00097     DataTypeSignatureCRC() : crc_(0xFFFFFFFFFFFFFFFFULL) { }
00098 
00099     void add(uint8_t byte);
00100 
00101     void add(const uint8_t* bytes, unsigned len);
00102 
00103     uint64_t get() const { return crc_ ^ 0xFFFFFFFFFFFFFFFFULL; }
00104 };
00105 
00106 
00107 class UAVCAN_EXPORT DataTypeSignature
00108 {
00109     uint64_t value_;
00110 
00111     void mixin64(uint64_t x);
00112 
00113 public:
00114     DataTypeSignature() : value_(0) { }
00115     explicit DataTypeSignature(uint64_t value) : value_(value) { }
00116 
00117     void extend(DataTypeSignature dts);
00118 
00119     TransferCRC toTransferCRC() const;
00120 
00121     uint64_t get() const { return value_; }
00122 
00123     bool operator==(DataTypeSignature rhs) const { return value_ == rhs.value_; }
00124     bool operator!=(DataTypeSignature rhs) const { return !operator==(rhs); }
00125 };
00126 
00127 /**
00128  * This class contains complete description of a data type.
00129  */
00130 class UAVCAN_EXPORT DataTypeDescriptor
00131 {
00132     DataTypeSignature signature_;
00133     const char* full_name_;
00134     DataTypeKind kind_;
00135     DataTypeID id_;
00136 
00137 public:
00138     static const unsigned MaxFullNameLen = 80;
00139 
00140     DataTypeDescriptor() :
00141         full_name_(""),
00142         kind_(DataTypeKind(0))
00143     { }
00144 
00145     DataTypeDescriptor(DataTypeKind kind, DataTypeID id, const DataTypeSignature& signature, const char* name) :
00146         signature_(signature),
00147         full_name_(name),
00148         kind_(kind),
00149         id_(id)
00150     {
00151         UAVCAN_ASSERT((kind == DataTypeKindMessage) || (kind == DataTypeKindService));
00152         UAVCAN_ASSERT(name);
00153         UAVCAN_ASSERT(std::strlen(name) <= MaxFullNameLen);
00154     }
00155 
00156     bool isValid() const;
00157 
00158     DataTypeKind getKind() const { return kind_; }
00159     DataTypeID getID() const { return id_; }
00160     const DataTypeSignature& getSignature() const { return signature_; }
00161     const char* getFullName() const { return full_name_; }
00162 
00163     bool match(DataTypeKind kind, const char* name) const;
00164     bool match(DataTypeKind kind, DataTypeID id) const;
00165 
00166     bool operator!=(const DataTypeDescriptor& rhs) const { return !operator==(rhs); }
00167     bool operator==(const DataTypeDescriptor& rhs) const;
00168 
00169 #if UAVCAN_TOSTRING
00170     std::string toString() const;
00171 #endif
00172 };
00173 
00174 }
00175 
00176 #endif // UAVCAN_DATA_TYPE_HPP_INCLUDED