16 #ifndef HF_SESSION_MANAGEMENT_H 17 #define HF_SESSION_MANAGEMENT_H 29 namespace SessionManagement
143 template<
typename _Entry>
146 std::vector<_Entry> entries;
152 uint16_t size()
const 154 uint16_t result = min_size;
157 std::for_each (entries.begin (), entries.end (), [&result](
const _Entry &entry)
159 result += entry.size ();
170 uint16_t start = offset;
174 uint8_t count = entries.size();
175 offset += array.
write(offset, count);
178 std::for_each(entries.begin(), entries.end(),
179 [&offset, &array](
const _Entry &entry)
181 offset += entry.pack (array, offset);
185 return offset - start;
192 uint16_t start = offset;
197 offset += array.
read(offset, count);
203 for (uint8_t i = 0; i < count; i++)
205 offset += entry.unpack(array, offset);
206 entries.push_back(entry);
209 return offset - start;
214 struct GetEntriesResponse<void>:
public Protocol::Response
218 GetEntriesResponse(): count(0)
225 uint16_t size()
const 237 array.
write(offset, count);
249 array.
read(offset, count);
255 typedef GetEntriesResponse<void> GetEntriesEmptyResponse;
291 virtual bool exists(uint16_t address)
const = 0;
303 virtual bool is_valid(uint16_t address)
const = 0;
335 typedef std::vector<Session> Container;
336 typedef Container::iterator iterator;
337 typedef Container::const_iterator const_iterator;
357 auto it =
find(address);
371 auto it =
find(address);
386 auto it =
find(address);
404 session.valid =
false;
438 iterator
find(uint16_t address)
442 [address](
const Session &session)
444 return address == session.address;
456 const_iterator
find(uint16_t address)
const 460 [address](
const Session &session)
462 return address == session.address;
543 template<
typename Parent>
550 typedef typename Parent::value_type value_type;
568 Parent(other), manager(_manager)
577 auto res = Parent::save(entry);
597 template<
typename _Entries>
609 return const_cast<Container &
>(_entries);
637 return static_cast<uint16_t
>(_entries.size());
640 typedef typename _Entries::value_type value_type;
644 GetEntriesResponse<value_type>
response;
650 auto start = _entries.begin();
651 std::advance(start, offset);
654 std::advance(end, count);
655 std::copy(start, end, std::back_inserter(
response.entries));
689 virtual void start_session()
const = 0;
694 virtual void end_session()
const = 0;
702 virtual void get_entries(uint16_t offset, uint8_t count = 0)
const = 0;
775 template<u
int8_t _role, u
int16_t _u
id, u
int8_t _member>
784 message.
itf.
id = _uid;
802 template<u
int8_t _role, u
int16_t _u
id, u
int8_t _member>
809 message.
itf.
id = _uid;
823 template<
typename _Entry>
852 GetEntriesResponse<_Entry>
response;
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=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.
bool response(Message::Type type)
Check if message is a response.
Helper template to inject session management functionality into services requiring it - Server side...
virtual void entries(const GetEntriesResponse< _Entry > &response)
This event is called when a response to a get entries is received.
virtual uint16_t entries_size() const =0
Get the number of entries present in the container.
Wrapper over persistent storage APIs to invalidate sessions on save and destroy.
Helper template to inject session management functionality into services requiring it - Client side...
Server(const Server &other)
Copy-Constructor.
virtual bool is_valid(uint16_t address) const =0
Check if the session for the device with the given address is valid, i.e., the underling entries have...
void get_entries(uint16_t offset, uint8_t count=0) const
Helper method to send a message to read, count entries starting at offset from the server...
This file contains the common defines for the HAN-FUN library.
uint16_t count
Number of device entries.
uint16_t size() const
Number bytes needed to serialize the message.
Common::Result entries(uint16_t offset, uint8_t count, Common::ByteArray &payload)
Create a GetEntriesResponse message message with, count entries starting from the given offset and se...
bool valid
Indicate if the session is still valid.
EntriesWrapper(AbstractServer &_manager)
Constructor.
Get Entries Command Message.
Parent class for session management functionality - Server side.
constexpr uint16_t BROADCAST_ADDR
HAN-FUN Broadcast - device address.
uint16_t id
Identifier of the interface.
Container & entries() const
Return the container for the service entries.
virtual void send(const Protocol::Address &addr, Protocol::Message &message)=0
Send message msg to the network address given by addr.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
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.
Container sessions
Session's database.
virtual void start_session(uint16_t address)=0
Start a session for the device with the given address.
This file contains the definitions for the HAN-FUN protocol messages.
const_iterator find(uint16_t address) const
Find the session associated with the given device address.
Common::Result destroy(const value_type &entry)
Destroy the given entry in the persistent storage.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Common::ByteArray payload
Message payload.
Parent class for bind management - server role.
virtual void send(const Protocol::Address &addr, Protocol::Message &message)=0
Send message msg to the network address given by addr.
uint16_t offset
Start index for the first entry to be provided.
Protocol::Message * destroy(Type type, uint8_t report_id)
Remove the rule with the given type and the given report_id.
#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...
End the session for device.
Parent class for session management functionality - Client side.
iterator find(uint16_t address)
Find the session associated with the given device address.
bool is_valid(uint16_t address) const
Check if the session for the device with the given address is valid, i.e., the underling entries have...
AbstractServer()
Constructor.
uint8_t count
Number of entries to be sent in the response.
uint16_t role
Interface role : Server or Client.
Common::Result check_offset(uint16_t offset, uint8_t &count, uint16_t size) const
Check if the given offset is valid and adjust the count value if necessary.
bool exists(uint16_t address) const
Check if a session for the device with the given address exists.
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.
uint16_t address
Device address for session.
virtual bool exists(uint16_t address) const =0
Check if a session for the device with the given address exists.
void invalidate()
Invalidate all sessions.
Start Read Session Command Message.
void end_session(uint16_t address)
Terminate the session associated with the device with the given address.
uint8_t member
Interface destination member.
uint16_t payload_size(CMD cmd) const
Get the minimum number of bytes necessary to pack/unpack a message of the given command.
CMD
Available commands for session management.
Session Management API : Server side.
virtual Common::Result entries(uint16_t offset, uint8_t count, Common::ByteArray &payload)=0
Create a GetEntriesResponse message message with, count entries starting from the given offset and se...
Session(uint16_t _address=HF::Protocol::BROADCAST_ADDR, bool _valid=false)
Constructor.
void start_session(uint16_t address)
Start a session for the device with the given address.
EntriesWrapper(const EntriesWrapper &other, AbstractServer &_manager)
Constructor.
Interface itf
Interface Address.
uint16_t read(uint16_t offset, uint8_t &data) const
Read the byte at offset into data.
StartResponse(uint16_t count=0)
Constructor.
Parent class for the response messages.
Common::Result handle_command(CMD cmd, Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset=0)
Handle command request/response messages.
#define UNUSED(x)
Helper macro to remove warning about unused function/method argument.
Common::Result handle_command(CMD cmd, Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset=0)
Handle command request/response messages.
Common::Result check_session(uint16_t address, Common::ByteArray &payload) const
Check if a session for the given device address exists and if it is valid.
static constexpr uint16_t min_size
Minimum number of bytes required by this message.
void request() const
Helper method to create a session management request.
Common::Result save(const value_type &entry)
Store the given entry to persistent storage.
virtual void end_session(uint16_t address)=0
Terminate the session associated with the device with the given address.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
uint16_t payload_size(CMD cmd) const
Get the minimum number of bytes necessary to pack/unpack a message of the given command.
Result
Commands result codes.
uint16_t entries_size() const
Get the number of entries present in the container.
Start a new session for a device.
Top-level namespace for the HAN-FUN library.