HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
weekly_scheduling.h
Go to the documentation of this file.
1 // =============================================================================
15 // =============================================================================
16 
17 #ifndef HF_CORE_WEEKLY_SCHEDULING_H
18 #define HF_CORE_WEEKLY_SCHEDULING_H
19 
20 #include "hanfun/protocol.h"
21 #include "hanfun/core.h"
22 
23 #include "hanfun/core/scheduling.h"
24 #include "hanfun/core/time.h"
25 
26 namespace HF
27 {
28  namespace Core
29  {
30  namespace Scheduling
31  {
32  // Forward declaration
33  namespace Weekly
34  {
35  struct IServer;
36  }
37  }
38 
54  uint8_t uid);
55 
56  namespace Scheduling
57  {
59  namespace Weekly
60  {
70  typedef enum _Days
72  {
73  MONDAY = 0x00,
74  TUESDAY,
75  WEDNESDAY,
76  THURSDAY,
77  FRIDAY,
78  SATURDAY,
79  SUNDAY
80  } Days;
81 
83  struct Day
84  {
85  uint8_t day;
86  uint8_t hour;
87  uint8_t minute;
88 
89  Day(uint8_t _day, uint8_t _hour, uint8_t _minute):
90  day(_day), hour(_hour), minute(_minute)
91  {}
92 
93  Day() = default;
94 
95  static constexpr int MINUTE = 60;
96  static constexpr int HOUR = 60 * MINUTE;
97  static constexpr int DAY = 24 * HOUR;
98  static constexpr uint32_t WEEK = 7 * DAY;
99 
100  // Jan, 1st 2000 was on a Saturday (UTC)
101  static constexpr uint8_t DAY_OF_WEEK_BASE = SATURDAY;
102 
103  static Day convert(const uint32_t &timestamp)
104  {
105  Day result;
106 
107  std::div_t temp = std::div(timestamp, DAY); // get the number of days
108  // since the start
109 
110  result.day = temp.quot % 7 // n of days MOD 7
111  + DAY_OF_WEEK_BASE;
112 
113  result.day = result.day % 7;
114 
115  temp = std::div(temp.rem, HOUR);
116  result.hour = temp.quot;
117  temp = std::div(temp.rem, MINUTE);
118  result.minute = temp.quot;
119 
120  return result;
121  }
122 
130  uint32_t first(uint32_t timestamp = 0) const
131  {
132  uint32_t result = timestamp;
133 
134  Day current(convert(timestamp));
135 
136  result += (this->hour - current.hour) * 60 * 60;
137  result += (this->minute - current.minute) * 60;
138 
139  result += (this->day - current.day) * 24 * 60 * 60;
140 
141  if (timestamp > result)
142  {
143  result += WEEK;
144  }
145 
146  return result;
147  }
148 
154  uint32_t step() const
155  {
156  return WEEK;
157  }
158 
164  bool active(uint32_t _time) const
165  {
166  UNUSED(_time);
167  return true;
168  }
169 
171  static constexpr uint16_t min_size = sizeof(uint8_t) // Day of Week.
172  + sizeof(uint8_t) // Hour of Day.
173  + sizeof(uint8_t); // Minute of Hour.
174 
176  uint16_t size() const
177  {
178  return min_size;
179  }
180 
182  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
183 
185  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
186  };
187 
190 
193 
199  {
200  MaximumNumberOfEntries(uint8_t value = 0, HF::Interface *owner = nullptr):
202  value, owner)
203  {}
204  };
205 
211  {
212  NumberOfEntries(uint8_t value = 0, HF::Interface *owner = nullptr):
214  value, owner)
215  {}
216  };
217 
222  struct Status: public Scheduling::Status
223  {
224  Status(bool value = false, HF::Interface *owner = nullptr):
226  value, owner)
227  {}
228  };
229 
241 
244 
247 
253  struct IClient: public IClientBase
254  {
257  {}
258 
260  virtual ~IClient() {}
261 
263 
270  virtual void activate_scheduler(const Protocol::Address &addr, bool enabled)
271  {
272  Scheduling::IClient::activate_scheduler(addr, ITF, enabled);
273  }
274 
285  virtual void define_event(const Protocol::Address &addr, uint8_t id, uint8_t status,
286  Day &time, uint8_t pid);
287 
288 #ifdef HF_CORE_WEEKLY_SCHEDULING_UPDATE_EVENT_STATUS_CMD
289 
296  virtual void update_event_status(const Protocol::Address &addr,
297  uint8_t id, bool enabled)
298  {
299  Scheduling::IClient::update_event_status(addr, ITF, id, enabled);
300  }
301 #endif
302 
303 #ifdef HF_CORE_WEEKLY_SCHEDULING_GET_EVENT_ENTRY_CMD
304 
310  virtual void get_event_entry(const Protocol::Address &addr, uint8_t id)
311  {
312  Scheduling::IClient::get_event_entry(addr, ITF, id);
313  }
314 #endif
315 
321  virtual void delete_event(const Protocol::Address &addr, uint8_t id)
322  {
323  Scheduling::IClient::delete_event(addr, ITF, id);
324  }
325 
326 #ifdef HF_CORE_WEEKLY_SCHEDULING_DELETE_ALL_EVENTS_CMD
327 
332  virtual void delete_all_events(const Protocol::Address &addr)
333  {
334  Scheduling::IClient::delete_all_events(addr, ITF);
335  }
336 #endif
337 
338  using IClientBase::send;
339  };
340 
346  struct Client: public IClient
347  {
352  {}
353 
354  virtual ~Client()
355  {}
356  };
357 
363  struct IServer: public IServerBase
364  {
365  uint16_t uid() const
366  {
367  return IServerBase::uid();
368  }
369 
370  HF::Attributes::UIDS attributes(uint8_t uid = HF::Attributes::Pack::MANDATORY) const
371  {
373  }
374 
375  uint8_t number_of_entries() const;
376 
389  virtual Common::Result define_event(const Protocol::Packet &packet,
391 
403  const UpdateStatus &msg);
404 
415  virtual Common::Result get_event_entry(const Protocol::Packet &packet,
416  const GetEntry &msg);
417 
428  virtual Common::Result delete_event(const Protocol::Packet &packet,
429  const DeleteEvent &msg);
430 
439  virtual Common::Result delete_all_events(const Protocol::Packet &packet);
440 
441  void periodic(uint32_t time);
442 
444  IServer(Unit0 &unit): IServerBase(unit)
445  {}
446 
448  virtual ~IServer() {}
449 
450  // =============================================================================
451  // Entries API
452  // =============================================================================
453 
460  virtual IEntries<Day> &entries() const = 0;
461 
470  Common::Pointer<Entry> entry(const uint8_t id) const
471  {
472  return entries().find(id);
473  }
474 
478  uint8_t next_id() const
479  {
480  return entries().next_id();
481  }
482 
483  using IServerBase::notify;
484 
485  // =============================================================================
486  // Protected types and methods
487  // =============================================================================
488 
489  protected:
490 
491  Common::Result handle_command(Protocol::Packet &packet, Common::ByteArray &payload,
492  uint16_t offset);
493 
494  };
495 
501  template<typename _Entries>
502  struct Server: public IServer
503  {
504  static_assert(std::is_base_of<HF::Core::Scheduling::IEntries<Day>, _Entries>::value,
505  "_Entries must be of type HF::Core::Scheduling::IEntries<Day>");
506 
507  protected:
508 
509  _Entries _entries;
510 
511  public:
512 
518  Server(Unit0 &unit): IServer(unit)
519  {}
520 
521  virtual ~Server()
522  {}
523 
525  _Entries &entries() const
526  {
527  return const_cast<_Entries &>(_entries);
528  }
529  };
530 
536 
539  } // namespace Weekly
540  } // namespace Scheduling
541  } // namespace Core
542 } // namespace HF
543 
544 #endif /* HF_CORE_WEEKLY_SCHEDULING_H */
uint16_t size() const
Number bytes needed to serialize the message.
Scheduling Service : Parent.
Definition: scheduling.h:738
UID
Interfaces Unique Identifiers (UID).
Definition: interface.h:57
virtual Common::Result update_event_status(const Protocol::Packet &packet, const UpdateStatus &msg)
Callback that is called when a Scheduling::UPDATE_STATUS_CMD, is received.
This represent the special unit with ID/UID = 0.
Definition: core.h:67
uint8_t next_id() const
Return next available id for event.
Scheduling::GetEntryResponse< Day > GetEntryResponse
Specific part for the Weekly Scheduler of the HF::Scheduling::GetEntryResonse.
This file contains the definitions for the Time service.
Server< Entries< Day > > DefaultServer
Weekly Scheduling Service : Server side with default persistence implementation.
List of attributes UIDs.
Definition: attributes.h:176
virtual Common::Result delete_all_events(const Protocol::Packet &packet)
Callback that is called when a Scheduling::DELETE_ALL_CMD, is received.
Weekly Scheduling Service : Client side implementation.
Weekly Scheduling interface UID.
Definition: interface.h:68
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
Return all mandatory attributes for the interface.
Definition: attributes.h:842
Scheduling Service : Client side implementation.
Definition: scheduling.h:876
This file contains the forward declarations of the core services and interfaces implementing classes...
Helper class template for parent class implementation of the interfaces.
Definition: interface.h:371
Helper class to handle the Status attribute for the Weekly Scheduling service.
Helper class to handle the Maximum Number Of Entries attribute for the Weekly Scheduling service...
This file contains the definitions for the HAN-FUN protocol messages.
virtual Common::Result get_event_entry(const Protocol::Packet &packet, const GetEntry &msg)
Callback that is called when a Scheduling::GET_ENTRY_CMD, is received.
Helper class to handle the Maximum Number Of Entries attribute for the scheduling services...
Definition: scheduling.h:119
Base class for scheduling services entries.
Definition: scheduling.h:175
This file contains the common definitions for the scheduling services.
uint8_t minute
Minute of Hour.
Message payload for a Scheduling::GET_ENTRY_CMD request.
Definition: scheduling.h:372
Scheduling Service : Client side implementation.
uint16_t uid() const
This method returns the interface UID.
Definition: core.h:191
Scheduling Service : Server side implementation.
HF::Interface const * owner() const
Definition: attributes.h:414
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.
Helper class to handle the Status attribute for the scheduling services.
Definition: scheduling.h:146
Scheduling - Persistent Storage API.
Definition: scheduling.h:483
This class represents a byte array.
virtual void activate_scheduler(const Protocol::Address &addr, bool enabled)
Send a HAN-FUN message containing a Scheduling::ACTIVATE_SCHEDULER_CMD, to the given network address...
virtual void delete_event(const Protocol::Address &addr, uint8_t id)
Send a HAN-FUN message containing a Scheduling::DELETE_CMD, to the given network address.
Network Address.
Definition: protocol.h:201
_Entries & entries() const
Get a reference to the current object implementing the persistence API, for the device information...
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
HAN-FUN Protocol Packet.
Definition: protocol.h:298
void activate_scheduler(const Protocol::Address &addr, const Interface::UID itf_uid, bool enabled=true)
Send a HAN-FUN message containing a Scheduling::ACTIVATE_SCHEDULER_CMD, to the given network address...
Helper class to handle the Number Of Entries attribute for the Weekly Scheduling service.
Helper class to handle the Number Of Entries attribute for the scheduling services.
Definition: scheduling.h:133
Interface/Service Attribute API.
Definition: attributes.h:44
virtual IEntries< Day > & entries() const =0
Get a reference to the current object implementing the persistence API, for the device information...
virtual Common::Result define_event(const Protocol::Packet &packet, Scheduling::Entry< Day > &msg)
Callback that is called when a Scheduling::DEFINE_EVENT_CMD, is received.
uint32_t step() const
Get the step between executions.
HF::Attributes::IAttribute * create_attribute(uint8_t uid)
Create an attribute object that can hold the attribute with the given uid. (HF::Core::Scheduling::Wee...
Scheduling Service : Server side implementation.
Definition: scheduling.h:753
bool active(uint32_t _time) const
Simple raw pointer wrapper.
#define UNUSED(x)
Helper macro to remove warning about unused function/method argument.
Message payload for a Scheduling::UPDATE_STATUS_CMD request.
Definition: scheduling.h:342
uint32_t first(uint32_t timestamp=0) const
Get the initial value for the next_run entry attribute.
Common interface for all Interfaces.
Definition: interface.h:43
virtual Common::Result delete_event(const Protocol::Packet &packet, const DeleteEvent &msg)
Callback that is called when a Scheduling::DELETE_CMD, is received.
Days
Day of the Week enumeration.
virtual void define_event(const Protocol::Address &addr, uint8_t id, uint8_t status, Day &time, uint8_t pid)
Send a HAN-FUN message containing a Scheduling::DEFINE_CMD, to the given network address.
Common::Pointer< Entry > entry(const uint8_t id) const
Get the Weekly Scheduling entry given by id.
HF::Attributes::UIDS attributes(uint8_t pack_id=HF::Attributes::Pack::MANDATORY) const
Return a vector containing the attribute UIDs, for the given pack ID.
void delete_event(const Protocol::Address &addr, const Interface::UID itf_uid, uint8_t id)
Send a HAN-FUN message containing a Scheduling::DELETE_CMD, to the given network address.
Scheduling::Entry< Day > Entry
Specific part for the Weekly Scheduler of the HF::Scheduling::Entry.
Result
Commands result codes.
Response message payload for a Scheduling::GET_ENTRY_CMD request.
Definition: scheduling.h:402
Scheduling Service : Server side implementation.
HF::Attributes::IAttribute * create_attribute(HF::Core::AttributeReporting::IServer *server, uint8_t uid)
Create an attribute object that can hold the attribute with the given uid.
Specific part for the Weekly Scheduler of the HF::Scheduling::Entry.
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22