HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
base.h
Go to the documentation of this file.
1 // =============================================================================
16 // =============================================================================
17 #ifndef HF_APP_BASE_H
18 #define HF_APP_BASE_H
19 
20 #include "hanfun.h"
21 
22 #include "common.h"
23 
24 #include "json/json.h"
25 
26 // =============================================================================
27 // Base
28 // =============================================================================
29 
35 namespace DeviceManagement
36 {
44  {
47 
48  HF::Common::Result save(const Device &device);
49 
50  HF::Common::Result destroy(const Device &device);
51 
59  void insert(const Device &device);
60  };
61 
69  {
73  {}
74 
75  virtual ~Server() {}
76 
84  uint16_t next_address(uint16_t addr)
85  {
86  _next_address = addr;
87  return _next_address;
88  }
89 
98  bool available(uint16_t address);
99 
108  bool deregister(uint16_t address);
109 
115  void save(Json::Value &root);
116 
122  void restore(Json::Value root);
123 
124  protected:
125 
127 
129  uint16_t _next_address;
130 
131  uint16_t next_address();
132  };
133 
134 } // namespace DeviceManagement
135 
136 namespace BindManagement
137 {
145  {
148 
149  HF::Common::Result save(const Entry &entry);
150 
151  HF::Common::Result destroy(const Entry &entry);
152 
160  void insert(Entry &entry);
161  };
162 
168  struct Server: public HF::Core::BindManagement::Server<Entries>
169  {
172  {}
173 
179  void save(Json::Value &root);
180 
186  void restore(Json::Value root);
187  };
188 
189 } // namespace BindManagement
190 
191 // =============================================================================
192 // Helper Units.
193 // =============================================================================
194 
195 namespace Events
196 {
197  struct Alert: public HF::Interfaces::Alert::Client
198  {
199  void status(HF::Protocol::Address &source, HF::Interfaces::Alert::Message &message);
200  };
201 
202  struct LevelControl: public HF::Interfaces::LevelControl::Server
203  {
204  void level_change(HF::Protocol::Address &source, uint8_t old_level, uint8_t new_level);
205  };
206 
207  struct OnOff: public HF::Interfaces::OnOff::Server
208  {
209  void on(HF::Protocol::Address &source);
210 
211  void off(HF::Protocol::Address &source);
212 
213  void toggle(HF::Protocol::Address &source);
214  };
215 
216  struct SimplePowerMeter: public HF::Interfaces::SimplePowerMeter::Client
217  {
219  };
220 
221  struct Unit: public HF::Units::Unit<HF::Profiles::GenericApplicationLogic, Alert,
222  LevelControl, OnOff, SimplePowerMeter>
223  {
224  Unit(uint8_t id, HF::IDevice &device):
225  HF::Units::Unit<HF::Profiles::GenericApplicationLogic, Alert, LevelControl,
226  OnOff, SimplePowerMeter>(id, device)
227  {}
228  };
229 
230 } // namespace Listeners
231 
232 namespace Commands
233 {
234  typedef HF::Interfaces::Alert::Server Alert;
235  typedef HF::Interfaces::LevelControl::Client LevelControl;
236  typedef HF::Interfaces::OnOff::Client OnOff;
237  typedef HF::Interfaces::SimplePowerMeter::Server SimplePowerMeter;
238 
239  struct Unit: public HF::Units::Unit<HF::Profiles::GenericApplicationLogic, Alert,
240  LevelControl, OnOff, SimplePowerMeter>
241  {
242  Unit(uint8_t id, HF::IDevice &device):
243  HF::Units::Unit<HF::Profiles::GenericApplicationLogic, Alert, LevelControl,
244  OnOff, SimplePowerMeter>(id, device)
245  {}
246 
247  Alert &alert()
248  {
249  return *const_cast<Alert *>(get<0>());
250  }
251 
252  LevelControl &level_control()
253  {
254  return *const_cast<LevelControl *>(get<1>());
255  }
256 
257  OnOff &on_off()
258  {
259  return *const_cast<OnOff *>(get<2>());
260  }
261 
262  SimplePowerMeter &simple_power_meter()
263  {
264  return *const_cast<SimplePowerMeter *>(get<3>());
265  }
266 
268  uint16_t offset);
269  };
270 
271 } // namespace
272 
273 // =============================================================================
274 // Concentrator.
275 // =============================================================================
276 
281 #if HF_TIME_SUPPORT
283 #endif
284 #if HF_BATCH_PROGRAM_SUPPORT
286 #endif
287 #if HF_EVENT_SCHEDULING_SUPPORT
289 #endif
290 #if HF_WEEKLY_SCHEDULING_SUPPORT
292 #endif
293 #if HF_GROUP_SUPPORT
294  HF::Core::GroupTable::DefaultServer,
296 #endif
298 
303 {
304  Events::Unit events;
305  Commands::Unit commands;
306 
308  events(1, *this), commands(2, *this)
309  {}
310 
311  virtual ~Base()
312  {}
313 
314  void receive(HF::Protocol::Packet &packet, HF::Common::ByteArray &payload, uint16_t offset);
315 
317 
318  std::forward_list<HF::Transport::Link *> &links()
319  {
320  return _links;
321  }
322 
332  bool has_bind(uint16_t dev_addr_1, uint16_t dev_addr_2);
333 
345  uint8_t bind(uint16_t dev_addr_1, uint16_t dev_addr_2);
346 
358  bool unbind(uint16_t dev_addr_1, uint16_t dev_addr_2);
359 };
360 
361 // =============================================================================
362 // Helper Functions
363 // =============================================================================
364 
365 // =============================================================================
366 // to_json
367 // =============================================================================
375 // =============================================================================
376 void to_json(const HF::Common::Interface &interface, Json::Value &node);
377 
378 // =============================================================================
379 // to_json
380 // =============================================================================
388 // =============================================================================
389 void to_json(HF::UID::UID *uid, Json::Value &node);
390 
391 // =============================================================================
392 // to_json
393 // =============================================================================
402 // =============================================================================
403 void to_json(const HF::Core::DeviceManagement::Unit &unit, Json::Value &node);
404 
405 // =============================================================================
406 // to_json
407 // =============================================================================
416 // =============================================================================
417 void to_json(const HF::Core::DeviceManagement::Device &device, Json::Value &node);
418 
419 // =============================================================================
420 // to_json
421 // =============================================================================
429 // =============================================================================
430 void to_json(const HF::Protocol::Address &address, Json::Value &node);
431 
432 // =============================================================================
433 // to_json
434 // =============================================================================
443 // =============================================================================
444 void to_json(const HF::Core::BindManagement::Entry &entry, Json::Value &node);
445 
446 // =============================================================================
447 // from_json
448 // =============================================================================
457 // =============================================================================
458 void from_json(Json::Value &node, HF::Common::Interface &interface);
459 
460 // =============================================================================
461 // from_json
462 // =============================================================================
473 // =============================================================================
474 void from_json(Json::Value &node, HF::UID::UID * &uid);
475 
476 // =============================================================================
477 // from_json
478 // =============================================================================
487 // =============================================================================
488 void from_json(Json::Value &node, HF::Core::DeviceManagement::Unit &unit);
489 
490 // =============================================================================
491 // from_json
492 // =============================================================================
502 // =============================================================================
503 void from_json(Json::Value &node, HF::Core::DeviceManagement::Device &device);
504 
505 // =============================================================================
506 // from_json
507 // =============================================================================
516 // =============================================================================
517 void from_json(Json::Value &node, HF::Protocol::Address &address);
518 
519 // =============================================================================
520 // from_json
521 // =============================================================================
531 // =============================================================================
532 void from_json(Json::Value &node, HF::Core::BindManagement::Entry &entry);
533 
536 #endif /* HF_APP_BASE_H */
void insert(Entry &entry)
Insert a bind management entry into the database.
Definition: base.cpp:223
bool available(uint16_t address)
Check if the given address is available for registration.
Definition: base.cpp:90
This class represents a HAN-FUN Concentrator.
Definition: base.h:302
HF::Devices::Concentrator::Unit0< HF::Core::DeviceInformation::Server, ::DeviceManagement::Server, HF::Core::AttributeReporting::Server, ::BindManagement::Server > Unit0
Custom Unit0 declaration.
Definition: base.h:297
On-Off Interface : Client side implementation.
Definition: on_off.h:218
This represent the special unit with ID/UID = 0.
Definition: core.h:67
This is the top level include file for the HAN-FUN library.
Simple Power Meter Interface : Server side implementation.
Default implementation of the persistence API.
Server< Entries< Day > > DefaultServer
Weekly Scheduling Service : Server side with default persistence implementation.
void insert(const Device &device)
Insert a device management entry into the database.
Definition: base.cpp:63
Default implementation of the bind entries container.
bool deregister(uint16_t address)
De-register the device with the given address.
Definition: base.cpp:136
Units::IUnit * unit(uint8_t id) const
Return pointer to the unit with the given id.
Server< Entries< Interval > > DefaultServer
Event Scheduling Service : Server side with default persistence implementation.
constexpr uint16_t BROADCAST_ADDR
HAN-FUN Broadcast - device address.
Definition: protocol.h:45
Level Control Interface : Client side implementation.
void save(Json::Value &root)
Save the bind entries into the JSON database.
Definition: base.cpp:250
Definition: base.h:195
Payload for the Status command.
Definition: alert.h:86
Attribute Reporting - Server Role.
Alert Interface : Client side implementation.
Definition: alert.h:367
Custom Device Management class.
Definition: base.h:68
Helper class used to implement custom functionality to the device management server side...
Helper template class to implement units.
Definition: units.h:196
This class represents the interface common to all HAN-FUN devices.
Definition: device.h:99
Custom Device Management Entries class.
Definition: base.h:43
Definition: base.h:232
On-Off Interface : Server side implementation.
Definition: on_off.h:119
Common::SimpleList< Transport::Link * > _links
List of links present in this concentrator.
Definition: devices.h:878
Unit 0 interface API for HAN-FUN Concentrators.
Definition: devices.h:607
uint16_t address() const
Return the device address on the HAN-FUN network, when the device is registered, or HF_BROADCAST_ADDR...
Definition: devices.h:858
Helper class used to implement custom functionality to the bind management server side...
Template for HAN-FUN concentrator devices.
Definition: devices.h:906
Wrapper around UID_T pointer&#39;s.
Definition: uids.h:531
This class represents a byte array.
Simple Power Meter Interface : Client side implementation.
void to_json(HF::UID::UID *uid, Json::Value &node)
Serialize a HF::UID::UID pointer to the given Json::Value.
Template to create Unit0 for HAN-FUN concentrator devices.
Definition: devices.h:668
Network Address.
Definition: protocol.h:201
uint16_t next_address(uint16_t addr)
Set the device address for the next registration message.
Definition: base.h:84
Custom Bind Management Entries class.
Definition: base.h:144
Level Control Interface : Server side implementation.
bool unbind(uint16_t dev_addr_1, uint16_t dev_addr_2)
Remove the binding entry for the given devices.
Definition: base.cpp:667
HAN-FUN Protocol Packet.
Definition: protocol.h:298
Alert Interface : Server side implementation.
Definition: alert.h:176
Time Service : Server side implementation.
Definition: time.h:220
This represents a bind entry data structure.
Simple raw pointer wrapper.
void receive(HF::Protocol::Packet &packet, HF::Common::ByteArray &payload, uint16_t offset)
Callback to deliver a packet received from the transport layer.
Definition: base.cpp:595
void restore(Json::Value root)
Restore the device entries from the JSON database.
Definition: base.cpp:184
void from_json(Json::Value &node, HF::UID::UID *&uid)
Create a new HF::UID::UID based on the fields present in the given Json::Value object.
void save(Json::Value &root)
Save the device entries into the JSON database.
Definition: base.cpp:161
Device Information interface : Server side.
This file contains the definitions for the common functionality in the HAN-FUN example applications...
Helper class used to implement custom functionality to the group management server side...
uint16_t next_address()
Return next available address for registering a device.
Definition: base.cpp:115
void restore(Json::Value root)
Restore the bind entries from the JSON database.
Definition: base.cpp:273
bool has_bind(uint16_t dev_addr_1, uint16_t dev_addr_2)
Check if bind exists.
Definition: base.cpp:613
uint16_t _next_address
The address the next registering device will be assigned.
Definition: base.h:129
Result
Commands result codes.
uint8_t bind(uint16_t dev_addr_1, uint16_t dev_addr_2)
Create a new bind entry.
Definition: base.cpp:629
Custom Bind Management class.
Definition: base.h:168
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22