16 #ifndef HF_ATTRIBUTES_H 17 #define HF_ATTRIBUTES_H 51 virtual uint8_t
uid()
const = 0;
87 virtual uint16_t
size(
bool with_uid)
const = 0;
119 virtual bool operator==(
const IAttribute &other)
const = 0;
121 virtual bool operator<(
const IAttribute &other)
const = 0;
123 virtual bool operator>(
const IAttribute &other)
const = 0;
176 struct UIDS:
public std::vector<uint8_t>
178 UIDS(): std::vector<uint8_t>()
186 UIDS(std::initializer_list<uint8_t> uids): vector<uint8_t>(uids)
198 vector<uint8_t>::size_type
length()
const 200 return vector<uint8_t>::size();
204 static constexpr uint8_t
min_size =
sizeof(uint8_t);
206 uint16_t size()
const 208 return min_size +
sizeof(uint8_t) * vector<uint8_t>::size();
215 uint16_t start = offset;
218 offset += array.
write(offset, count);
221 std::for_each (vector<uint8_t>::begin(), vector<uint8_t>::end(),
222 [&offset,&array](uint8_t uid)
224 offset += array.
write (offset, uid);
228 return offset - start;
231 uint16_t unpack(
const Common::ByteArray &array, uint16_t offset = 0)
235 return unpack(array, offset, count);
248 uint16_t start = offset;
250 offset += array.
read(offset, count);
254 vector<uint8_t>::reserve(count);
256 for (uint8_t i = 0; i < count; i++)
259 offset += array.
read(offset, uid);
260 vector<uint8_t>::push_back(uid);
263 return offset - start;
299 bool operator==(
const IAttribute &other)
const 301 return this->
compare(other) == 0;
304 bool operator==(IAttribute &other)
const 306 return this->
compare(other) == 0;
309 bool operator<(
const IAttribute &other)
const 311 return this->
compare(other) < 0;
314 bool operator<(IAttribute &other)
const 316 return this->
compare(other) < 0;
319 bool operator>(
const IAttribute &other)
const 321 return this->
compare(other) > 0;
324 bool operator>(IAttribute &other)
const 326 return this->
compare(other) > 0;
338 return this->
uid() - other.
uid();
348 template<
typename T,
typename _Owner =
void,
typename =
void>
361 T data,
bool writable =
false):
388 typedef typename std::remove_reference<T>::type value_type;
394 void set(value_type __value)
399 value_type
get()
const 404 value_type value()
const 409 void value(value_type __value)
411 helper.
data = __value;
419 uint16_t
size(
bool with_uid)
const 421 return size() + (with_uid ?
sizeof(uint8_t) : 0);
426 return helper.
size();
433 uint16_t start = offset;
437 offset += array.
write(offset,
uid());
440 offset += helper.
pack(array, offset);
442 return offset - start;
447 return helper.
pack(array, offset);
454 uint16_t start = offset;
459 offset += array.
read(offset, temp);
467 offset += helper.
unpack(array, offset);
469 return offset - start;
474 return helper.
unpack(array, offset);
490 res = helper.compare(temp->helper);
503 return helper.changed(temp->helper);
527 template<
typename T,
typename _Owner>
531 typedef typename std::remove_reference<T>::type value_type;
533 typedef typename std::function<value_type(_Owner &)> getter_t;
534 typedef typename std::function<void (_Owner &, T)> setter_t;
545 Attribute(_Owner &__owner,
const uint8_t
uid, getter_t _getter, setter_t _setter,
546 bool writable =
false):
559 Attribute(_Owner &__owner,
const uint8_t
uid, getter_t _getter,
bool writable =
false):
572 __attribute__((__nonnull__(2)))
573 Attribute(_Owner *__owner, const uint8_t
uid, getter_t _getter, setter_t _setter,
574 bool writable = false):
587 __attribute__((__nonnull__(2)))
588 Attribute(_Owner *__owner, const uint8_t
uid, getter_t _getter,
bool writable = false):
596 void set(value_type __value)
601 value_type
get()
const 606 value_type value()
const 608 return getter(*_owner);
611 void value(value_type __value)
615 setter(*_owner, __value);
624 uint16_t
size(
bool with_uid)
const 626 return size() + (with_uid ?
sizeof(uint8_t) : 0);
632 return helper.
size();
639 uint16_t start = offset;
643 offset += array.
write(offset,
uid());
648 offset += helper.
pack(array, offset);
650 return offset - start;
657 return helper.
pack(array, offset);
665 uint16_t start = offset;
670 offset += array.
read(offset, temp);
678 offset += helper.
unpack(array, offset);
680 setter(*_owner, helper.
data);
683 return offset - start;
689 uint16_t result = helper.
unpack(array, offset);
691 setter(*_owner, helper.
data);
698 this->getter, this->setter,
711 res = lhs.compare(rhs);
726 return lhs.changed(rhs);
752 struct List:
public std::list<IAttribute *>
756 for (const_iterator it = begin(); it != end(); ++it)
758 if ((*it)->uid() == uid)
783 attribute(_attribute)
791 uint16_t size()
const 796 uint16_t pack(Common::ByteArray &array, uint16_t offset = 0)
const 800 uint16_t start = offset;
804 offset += (attribute !=
nullptr ? attribute->
pack(array, offset) : 0);
806 return offset - start;
809 uint16_t unpack(
const Common::ByteArray &array, uint16_t offset = 0)
813 uint16_t start = offset;
816 offset += (attribute !=
nullptr ? attribute->
unpack(array, offset) : 0);
818 return offset - start;
858 List
get(
const HF::Interface &itf, uint8_t pack_id,
const UIDS &uids);
861 Attribute<T> *adapt(IAttribute *attr)
863 return static_cast<Attribute<T> *
>(attr);
867 const Attribute<T> *adapt(
const IAttribute *attr)
869 return static_cast<const Attribute<T> *
>(attr);
886 namespace GetAttributePack
978 std::for_each (attributes.begin (), attributes.end (),
991 uint16_t
size()
const;
1010 namespace SetAttributePack
1034 uint16_t
size()
const;
1075 static constexpr uint16_t
min_size =
sizeof(uint8_t) +
sizeof(uint8_t);
1088 offset += array.
write(offset, (uint8_t)
uid);
1100 offset += array.
read(offset,
uid);
1103 array.
read(offset, temp);
1110 typedef std::vector<Result> results_t;
uint8_t count
Unpack attribute count.
virtual float changed(const IAttribute &other) const =0
This method is used to get the percentage of change that the attribute has in relation to the value p...
This class represents the payload of a HF::Message::GET_ATTR_PACK_RES message.
uint8_t count
Unpack the results count.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
uint16_t size() const
Number bytes needed to serialize the message.
virtual bool isWritable() const =0
Indicate if the attribute is writable.
results_t results
Response results.
const bool _writable
Attribute access mode.
IAttribute * clone() const
Create a clone object of the object where this method is being called.
This class represents the payload of a HF::Message::SET_ATTR_PACK_RES message.
uint16_t size() const
Number bytes needed to serialize the message.
virtual uint16_t unpack(const ByteArray &array, uint16_t offset=0)=0
Read a message from a ByteArray.
uint16_t write(uint16_t offset, uint8_t data)
Write a byte into the array at the given offset.
This class represents the interface that cloneable objects need to implement.
This class represents the payload of a HF::Message::GET_ATTR_PACK_REQ request, when the payload is Ty...
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
Return all mandatory and optional attributes for the interface.
static constexpr uint8_t min_size
Minimum pack/unpack required data size.
uint8_t uid
Attribute UID.
T data
Data type instance wrapped.
float changed(const IAttribute &other) const
This method is used to get the percentage of change that the attribute has in relation to the value p...
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
Return all mandatory attributes for the interface.
This file contains the common defines for the HAN-FUN library.
bool isWritable() const
Indicate if the attribute is writable.
const uint8_t _uid
Attribute unique identifier.
This represents the common interface for message serialization.
uint8_t count
Unpack attribute count.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
Factory get_factory(Common::Interface itf)
Return the attribute factory associated with the given interface identifier.
Result(uint8_t uid, Common::Result code)
Constructor.
uint16_t unpack(const ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
virtual uint16_t size() const =0
Number bytes needed to serialize the message.
This class has the same behavior has a list, however the list element access methods where overwritte...
uint16_t size() const
Number bytes needed to serialize the message.
This file contains the definitions for the HAN-FUN protocol messages.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset, uint8_t &count)
Read a message from a ByteArray.
Attribute(const uint16_t interface, const uint8_t uid, T data, bool writable=false)
Attribute template constructor.
Attribute(_Owner &__owner, const uint8_t uid, getter_t _getter, setter_t _setter, bool writable=false)
Attribute template constructor.
uint8_t count
Unpack attribute count.
Response(HF::Attributes::Factory factory)
Constructor.
Response(HF::Attributes::List &attributes)
Constructor.
Return the attributes with the given attributes.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
virtual HF::Interface const * owner() const =0
Pointer to the interface that owns this attribute.
#define HF_SERIALIZABLE_CHECK(__array, __offset, __size)
Helper macro to check if the given __array has enough size so __size bytes can be written/read from t...
virtual uint16_t size(bool with_uid) const =0
Number bytes needed to serialize the message.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
float changed(const IAttribute &other) const
This method is used to get the percentage of change that the attribute has in relation to the value p...
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
vector< uint8_t >::size_type length() const
Number of elements in the list.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Attribute(const uint16_t interface, const uint8_t uid, bool writable=false)
Attribute template constructor.
HF::Interface const * owner() const
Pointer to the interface that owns this attribute.
HF::Interface const * owner() const
Pointer to the interface that owns this attribute.
This class represents the response sent when a Protocol::Message::GET_ATTR_REQ request.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
UIDS(std::initializer_list< uint8_t > uids)
Constructor.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
Attribute(_Owner &__owner, const uint8_t uid, getter_t _getter, bool writable=false)
Attribute template constructor.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset, bool with_uid)
Read a message from a ByteArray.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
This class represents a byte array.
Pack
Attribute Pack Special IDs.
HF::Attributes::Factory attribute_factory
Pointer to the function to request the attribute instances to unpack the response.
Set attribute operation result.
uint16_t size() const
Number bytes needed to serialize the message.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset, bool with_uid)
Read a message from a ByteArray.
int compare(const IAttribute &other) const
Compare this attribute with the given attribute in other.
Parent class for Attribute API implementation.
uint16_t size(bool with_uid) const
Number bytes needed to serialize the message.
Response(IAttribute *_attribute=nullptr)
Constructor.
IAttribute *(* Factory)(uint8_t)
Attribute factory function type.
uint16_t pack(ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
uint16_t interface() const
Return the UID of the interface the attribute belongs to.
uint16_t size(bool with_uid) const
Number bytes needed to serialize the message.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
uint16_t pack(Common::ByteArray &array, uint16_t offset, bool with_uid) const
Write the object on to a ByteArray so it can be sent over the network.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
uint16_t size() const
Number bytes needed to serialize the message.
virtual uint16_t pack(Common::ByteArray &array, uint16_t offset, bool with_uid) const =0
Write the object on to a ByteArray so it can be sent over the network.
Interface/Service Attribute API.
HF::Attributes::UIDS attributes
Vector containing the attributes UID's to get.
uint16_t read(uint16_t offset, uint8_t &data) const
Read the byte at offset into data.
uint16_t size() const
Number bytes needed to serialize the message.
const uint16_t _itf_uid
Interface this attribute belongs to.
Attributes::Factory(* FactoryGetter)(Common::Interface)
Function pointer to a function that returns the attribute factory associated with a given interface...
uint8_t uid() const
Attribute's UID.
Parent class for the response messages.
virtual uint16_t pack(ByteArray &array, uint16_t offset=0) const =0
Write the object on to a ByteArray so it can be sent over the network.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
uint16_t size() const
Number bytes needed to serialize the message.
Helper template class to declare an attribute with the given T type.
HF::Attributes::List attributes
List containing the attributes to send.
Attribute(const uint16_t interface, const uint8_t uid, const HF::Interface *__owner, T data, bool writable=false)
Attribute template constructor.
static constexpr uint16_t min_size
Minimum number of bytes required by this message.
IAttribute * clone() const
Create a clone object of the object where this method is being called.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Common interface for all Interfaces.
virtual int compare(const IAttribute &other) const =0
Compare this attribute with the given attribute in other.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
Common::Result code
Command result.
int compare(const IAttribute &other) const
Compare this attribute with the given attribute in other.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
virtual uint8_t uid() const =0
Attribute's UID.
uint16_t size() const
Number bytes needed to serialize the message.
Result
Commands result codes.
uint16_t pack(Common::ByteArray &array, uint16_t offset, bool with_uid) const
Write the object on to a ByteArray so it can be sent over the network.
int compare(const IAttribute &other) const
Compare this attribute with the given attribute in other.
virtual uint16_t unpack(const Common::ByteArray &array, uint16_t offset, bool with_uid)=0
Read a message from a ByteArray.
virtual uint16_t interface() const =0
Return the UID of the interface the attribute belongs to.
This class represents the message payload of a HF::Message::SET_ATTR_PACK_REQ or HF::Message::SET_ATT...
uint16_t size() const
Number bytes needed to serialize the message.
Top-level namespace for the HAN-FUN library.