HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
Examples

This module contains the classes for the provided example applications. More...

Namespaces

 HF::Application
 This namespace contains the declaration of the functions that implement the HAN-FUN example applications.
 

Data Structures

struct  Base
 This class represents a HAN-FUN Concentrator. More...
 
struct  ICommand
 Example application menu entry API. More...
 
class  Command
 Parent class for the commands API implementations. More...
 
struct  Node
 This class represents a node in the HAN-FUN network. More...
 
struct  SimpleLight
 Unit implementing a Simple Light. More...
 
struct  SimpleSwitch
 Unit implementing a Simple Switch. More...
 
struct  msg_t
 Message sent between the concentrator and the nodes. More...
 
struct  hello_msg_t
 Hello message payload. More...
 

Macros

#define HF_APP_CONFIG_FILE   "./hanfun.json"
 JSON database file. More...
 
#define COMMAND(_name, _key, _help)
 Helper macro to define new commands.
 
#define COMMAND_ADD(_name)
 Helper macro to add a new command to the registry.
 
#define STRTOL(X)   strtol(X.c_str(), NULL, 10);
 Helper macro to convert a std::string into a number (base 10).
 
#define STRTOL_HEX(X)   strtol(X.c_str(), NULL, 16);
 Helper macro to convert a std::string into a number (base 16).
 
#define NONE_MSG   0xFFFF
 No message primitive id. More...
 
#define HELLO_MSG   0x0101
 Hello message primitive id. More...
 
#define DATA_MSG   0x0201
 Data message primitive id. More...
 
#define CHECK_STATUS()
 Helper macro the check libuv command status.
 

Typedefs

typedef HF::Devices::Concentrator::Unit0< HF::Core::DeviceInformation::Server, ::DeviceManagement::Server, HF::Core::AttributeReporting::Server, ::BindManagement::ServerUnit0
 Custom Unit0 declaration.
 

Functions

void to_json (const HF::Common::Interface &interface, Json::Value &node)
 Serialize a HF::Common::Interface to the given Json::Value. More...
 
void to_json (const HF::Core::DeviceManagement::Unit &unit, Json::Value &node)
 Serialize a HF::Core::DeviceManagement::Unit to the given Json::Value. More...
 
void to_json (const HF::Core::DeviceManagement::Device &device, Json::Value &node)
 Serialize a HF::Core::DeviceManagement::Device to the given Json::Value. More...
 
void to_json (const HF::Protocol::Address &address, Json::Value &node)
 Serialize a HF::Protocol::Address to the given Json::Value. More...
 
void to_json (const HF::Core::BindManagement::Entry &entry, Json::Value &node)
 Serialize a HF::Core::BindManagement::Entry to the given Json::Value. More...
 
void from_json (Json::Value &node, HF::Common::Interface &interface)
 Fill a HF::Common::Interface from the given Json::Value object. More...
 
void from_json (Json::Value &node, HF::Core::DeviceManagement::Unit &unit)
 Fill a HF::Core::DeviceManagement::Unit from the given Json::Value object. More...
 
void from_json (Json::Value &node, HF::Core::DeviceManagement::Device &device)
 Fill a HF::Core::DeviceManagement::Device from the given Json::Value object. More...
 
void from_json (Json::Value &node, HF::Protocol::Address &address)
 Fill a HF::Protocol::Address from the given Json::Value object. More...
 
void from_json (Json::Value &node, HF::Core::BindManagement::Entry &entry)
 Fill a HF::Core::BindManagement::Entry from the given Json::Value object. More...
 
void to_json (HF::UID::UID *uid, Json::Value &node)
 Serialize a HF::UID::UID pointer to the given Json::Value. More...
 
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. More...
 
 COMMAND (ListRegs, "lr", "lr:list registrations.")
 ListRegs List registrations command.
 
 COMMAND (ListBinds, "lb", "lb:list binds.")
 List binds command.
 
 COMMAND (Register, "r", "r 1 x:register device x.\ 0:exit registration mode.")
 Setup registration command. More...
 
 COMMAND (Deregister, "d", "d x:de-register device x.")
 De-register device command.
 
 COMMAND (Unbind, "u", "u x y:unbind device x with y.")
 Destroy bind command.
 
 COMMAND (GlobalBind, "gb", "gb 1:create bind to receive all interface events.\ "gb 0:remove bind to receive all interface events.")
 Global binds command.
 
 COMMAND (On, "on", "on d u:Send an ON command to device/unit pair.")
 Send ON command.
 
 COMMAND (Off, "off", "off d u:Send an OFF command to device/unit pair.")
 Send OFF command.
 
 COMMAND (Toggle, "toggle", "toggle d u:Send a TOGGLE command to device/unit pair.")
 Send TOGGLE command.
 
 COMMAND (DevInfo, "di", "di m d:Get device information mandatory attributes.\ "di a d:Get device information all attributes.")
 Get Device information.
 
uv_buf_t alloc_buffer (uv_handle_t *handle, size_t suggested_size)
 This function is used to allocate the buffers used with libuv. More...
 
 COMMAND (Register, "r", "r:register device")
 Register node command.
 
 COMMAND (Address, "a", "a:device address")
 Print node address command.
 
 COMMAND (SimpleLight, "sl", "sl:set device as a simple light")
 Change device to a simple light.
 
 COMMAND (SimpleSwitch, "ss", "ss:set device as a simple switch")
 Change device to a simple switch.
 
 COMMAND (On, "on", "on:On Command")
 Send an on command from the simple switch.
 
 COMMAND (Off, "off", "off:Off Command")
 Send an off command from the simple switch.
 
 COMMAND (Toggle, "toggle", "toggle:Toggle Command")
 Send a toggle command from the simple switch.
 
void print_error (uv_err_t status)
 Print a user friendly string for given status code. More...
 
void DeviceManagement::Entries::insert (const Device &device)
 Insert a device management entry into the database. More...
 
bool DeviceManagement::Server::available (uint16_t address)
 Check if the given address is available for registration. More...
 
uint16_t DeviceManagement::Server::next_address ()
 Return next available address for registering a device. More...
 
bool DeviceManagement::Server::deregister (uint16_t address)
 De-register the device with the given address. More...
 
void DeviceManagement::Server::save (Json::Value &root)
 Save the device entries into the JSON database. More...
 
void DeviceManagement::Server::restore (Json::Value root)
 Restore the device entries from the JSON database. More...
 
void BindManagement::Entries::insert (Entry &entry)
 Insert a bind management entry into the database. More...
 
void BindManagement::Server::save (Json::Value &root)
 Save the bind entries into the JSON database. More...
 
void BindManagement::Server::restore (Json::Value root)
 Restore the bind entries from the JSON database. More...
 
void Base::receive (HF::Protocol::Packet &packet, HF::Common::ByteArray &payload, uint16_t offset)
 Callback to deliver a packet received from the transport layer. More...
 
bool Base::has_bind (uint16_t dev_addr_1, uint16_t dev_addr_2)
 Check if bind exists. More...
 
uint8_t Base::bind (uint16_t dev_addr_1, uint16_t dev_addr_2)
 Create a new bind entry. More...
 
bool Base::unbind (uint16_t dev_addr_1, uint16_t dev_addr_2)
 Remove the binding entry for the given devices. More...
 
void HF::Application::Initialize (HF::Transport::Layer &transport)
 Initialize the application. More...
 
void HF::Application::Save ()
 Save application configuration.
 
void HF::Application::Restore ()
 Restore application configuration.
 
void HF::Application::Saved ()
 Callback indicating that the application configuration has been saved.
 
void HF::Application::Restored ()
 Callback indicating that the application configuration has been restored.
 
void Node::receive (HF::Protocol::Packet &packet, HF::Common::ByteArray &payload, uint16_t offset)
 Callback to deliver a packet received from the transport layer. More...
 
void SimpleLight::on (HF::Protocol::Address &source)
 Callback that is called when a ON_CMD message is received. More...
 
void SimpleLight::off (HF::Protocol::Address &source)
 Callback that is called when a OFF_CMD message is received. More...
 
void SimpleLight::toggle (HF::Protocol::Address &source)
 Callback that is called when a TOGGLE_CMD message is received. More...
 
void HF::Application::Transport::initialize ()
 Initialize the associated transport layer. More...
 
void HF::Application::Transport::destroy ()
 Free the system resources associated with this transport layer.
 
void HF::Application::Link::send (HF::Common::ByteArray &array)
 Send the data in the given ByteArray using the link to the remote end-point. More...
 

Variables

uint16_t arg2 = STRTOL(args[1])
 Create bind command.
 

Detailed Description

This module contains the classes for the provided example applications.

Macro Definition Documentation

◆ DATA_MSG

#define DATA_MSG   0x0201

Data message primitive id.

Definition at line 45 of file transport.cpp.

◆ HELLO_MSG

#define HELLO_MSG   0x0101

Hello message primitive id.

Definition at line 44 of file transport.cpp.

◆ HF_APP_CONFIG_FILE

#define HF_APP_CONFIG_FILE   "./hanfun.json"

JSON database file.

Definition at line 47 of file base_app.cpp.

Referenced by HF::Application::Restore(), and HF::Application::Save().

◆ NONE_MSG

#define NONE_MSG   0xFFFF

No message primitive id.

Definition at line 43 of file transport.cpp.

Function Documentation

◆ alloc_buffer()

uv_buf_t alloc_buffer ( uv_handle_t *  handle,
size_t  suggested_size 
)

This function is used to allocate the buffers used with libuv.

Parameters
[in]handlea pointer to the handle to allocate the buffer for.
[in]suggested_sizenumber bytes to allocate.
Returns
buffer allocated.

Definition at line 212 of file transport.cpp.

References UNUSED.

213 {
214  UNUSED(handle);
215  return uv_buf_init((char *) calloc(1, suggested_size), suggested_size);
216 }
#define UNUSED(x)
Helper macro to remove warning about unused function/method argument.

◆ available()

bool DeviceManagement::Server::available ( uint16_t  address)

Check if the given address is available for registration.

Parameters
[in]addressdevice address to check.
Return values
<tt>true</tt>if no device is registered with the given address;
<tt>false</tt>otherwise.

Definition at line 90 of file base.cpp.

References HF::Core::DeviceManagement::Device::address, and HF::Protocol::BROADCAST_ADDR.

91 {
93  {
94  return false;
95  }
96 
97  /* *INDENT-OFF* */
98  auto it = std::find_if(entries().begin(), entries().end(),
100  {
101  return address == dev.address;
102  });
103  /* *INDENT-ON* */
104 
105  return it == entries().end();
106 }
constexpr uint16_t BROADCAST_ADDR
HAN-FUN Broadcast - device address.
Definition: protocol.h:45
uint16_t address
Device Address.
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

◆ bind()

uint8_t Base::bind ( uint16_t  dev_addr_1,
uint16_t  dev_addr_2 
)

Create a new bind entry.

Parameters
[in]dev_addr_1HAN-FUN address of the first device.
[in]dev_addr_2HAN-FUN address of the second device.
Return values
0Bind created.
1Bind already created.
2First device does not exist.
3Second device does not exist.

Definition at line 629 of file base.cpp.

References HF::Common::OK, HF::Interface::ON_OFF, and HF::Interface::SERVER_ROLE.

630 {
631  if (this->has_bind(dev_addr_1, dev_addr_2))
632  {
633  return 1;
634  }
635 
636  HF::Protocol::Address source(dev_addr_1, 1);
638  HF::Protocol::Address destination(dev_addr_2, 1);
639 
640  auto res = _unit0.bind_management()->add(source, destination, itf);
641 
642  if (res == HF::Common::OK)
643  {
644  return 0;
645  }
646 
647  if (_unit0.device_management()->entry(dev_addr_1) == nullptr)
648  {
649  return 2;
650  }
651 
652  if (_unit0.device_management()->entry(dev_addr_2) == nullptr)
653  {
654  return 3;
655  }
656 
657  return 4;
658 }
Network Address.
Definition: protocol.h:201
ON-OFF interface UID.
Definition: interface.h:79
bool has_bind(uint16_t dev_addr_1, uint16_t dev_addr_2)
Check if bind exists.
Definition: base.cpp:613

◆ COMMAND()

COMMAND ( Register  ,
"r"  ,
"r 1 x:register device x.\ 0:exit registration mode."   
)

Setup registration command.

< Disable Registration

< Enable Registration

Definition at line 116 of file base_app.cpp.

References LOG.

116  :register device x.\nr 0:exit registration mode.")
117 {
118  if (args.size() > 0 && args[0] == "0") //!< Disable Registration
119  {
120 #ifdef HF_APP_EXT_REG
121 
122  if (HF::Application::Registration(false))
123  {
124  LOG(INFO) << "Disable Registration mode: SUCCESS" << NL;
125  }
126  else
127  {
128  LOG(WARN) << "Disable Registration mode: FAIL" << NL;
129  }
130 
131 #endif
132  }
133  else if (args.size() > 1 && args[0] == "1") //!< Enable Registration
134  {
135  uint16_t address = STRTOL(args[1]);
136 
137  if (base.unit0()->device_management()->available(address) && address != 0 &&
138  address < HF::Protocol::BROADCAST_ADDR)
139  {
140  base.unit0()->device_management()->next_address(address);
141 #ifdef HF_APP_EXT_REG
142 
143  if (HF::Application::Registration(true))
144  {
145  LOG(INFO) << "Enable Registration mode: SUCCESS" << NL;
146  }
147  else
148  {
149  LOG(WARN) << "[HANFUN] Enable Registration mode: FAIL" << NL;
150  }
151 
152 #endif
153  LOG(INFO) << "Next Registration will have address : " << (int) address << NL;
154  }
155  else
156  {
157  LOG(ERROR) << "Registration impossible: address " << (int) address
158  << " not available." << NL;
159  }
160 
161  return;
162  }
163 
164  LOG(APP) << usage(true) << NL;
165 }

◆ deregister()

bool DeviceManagement::Server::deregister ( uint16_t  address)

De-register the device with the given address.

Parameters
[in]addressaddress for the device to de-register.
Return values
<tt>true</tt>if the device was de-registered;
<tt>false</tt>otherwise.

Definition at line 136 of file base.cpp.

References HF::Protocol::BROADCAST_ADDR, and HF::Core::DeviceManagement::AbstractServer::deregister().

137 {
139  {
140  return false;
141  }
142 
143  auto _entry = entry(address);
144 
145  if (_entry != nullptr)
146  {
148  return true;
149  }
150 
151  return false;
152 }
constexpr uint16_t BROADCAST_ADDR
HAN-FUN Broadcast - device address.
Definition: protocol.h:45
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
virtual Common::Result deregister(DevicePtr &device)
De-register the device that corresponds to the given Device entry.
DevicePtr entry(uint16_t address) const
Return the Device entry for the given address.
+ Here is the call graph for this function:

◆ from_json() [1/6]

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.

Parameters
[in]nodereference to the Json::Value containing the fields for the HF::UID::UID.
[out]uidreference to the pointer that will hold the created HF::UID::UID object of the correct type (NONE/RFPI/IPUI/MAC/URI).

◆ from_json() [2/6]

void from_json ( Json::Value &  node,
HF::Common::Interface interface 
)

Fill a HF::Common::Interface from the given Json::Value object.

Parameters
[in]nodereference to the Json::Value containing the fields for the HF::Common::Interface.
[out]interfacereference to the HF::Common::Interface to update.

Definition at line 845 of file base.cpp.

846 {
847  std::string role = node.get("role", "client").asString();
848 
849  if (role[0] == 's' || role[0] == 'S')
850  {
851  interface.role = HF::Interface::SERVER_ROLE;
852  }
853  else
854  {
855  interface.role = HF::Interface::CLIENT_ROLE;
856  }
857 
858  interface.id = json_uid(node.get("id", "0x7FFF").asString());
859 }
uint16_t id
Identifier of the interface.
uint16_t role
Interface role : Server or Client.

◆ from_json() [3/6]

void from_json ( Json::Value &  node,
HF::Core::DeviceManagement::Unit unit 
)

Fill a HF::Core::DeviceManagement::Unit from the given Json::Value object.

Parameters
[in]nodereference to the Json::Value containing the fields for the HF::Core::DeviceManagement::Unit.
[out]unitreference to the HF::Core::DeviceManagement::Unit to update.

Definition at line 914 of file base.cpp.

915 {
916  unit.id = (uint8_t) node.get("id", HF::Protocol::BROADCAST_UNIT).asUInt();
917 
918  unit.profile = json_uid(node.get("profile", "0x7FFF").asString());
919 
920  for (unsigned i = 0; i < node["opts"].size(); i++)
921  {
922  unit.interfaces.push_back(0);
923  from_json(node["opts"][i], unit.interfaces[i]);
924  }
925 }
void from_json(Json::Value &node, HF::Common::Interface &interface)
Fill a HF::Common::Interface from the given Json::Value object.
Definition: base.cpp:845
std::vector< Common::Interface > interfaces
Optional interfaces.
constexpr uint8_t BROADCAST_UNIT
HAN-FUN Broadcast - unit address.
Definition: protocol.h:48

◆ from_json() [4/6]

void from_json ( Json::Value &  node,
HF::Core::DeviceManagement::Device device 
)

Fill a HF::Core::DeviceManagement::Device from the given Json::Value object.

Parameters
[in]nodereference to the Json::Value containing the fields for the HF::Core::DeviceManagement::Device.
[out]devicereference to the HF::Core::DeviceManagement::Device to update.

Definition at line 934 of file base.cpp.

935 {
936  dev.address = (uint16_t) node.get("address", HF::Protocol::BROADCAST_ADDR).asUInt();
937 
938  for (unsigned i = 0; i < node["units"].size(); i++)
939  {
940  dev.units.push_back(0);
941  from_json(node["units"][i], dev.units[i]);
942  }
943 
944  dev.emc = json_uid(node.get("emc", "0x0000").asString());
945 
946  from_json(node["uid"], dev.uid);
947 }
constexpr uint16_t BROADCAST_ADDR
HAN-FUN Broadcast - device address.
Definition: protocol.h:45
void from_json(Json::Value &node, HF::Common::Interface &interface)
Fill a HF::Common::Interface from the given Json::Value object.
Definition: base.cpp:845

◆ from_json() [5/6]

void from_json ( Json::Value &  node,
HF::Protocol::Address address 
)

Fill a HF::Protocol::Address from the given Json::Value object.

Parameters
[in]nodereference to the Json::Value containing the fields for the HF::Protocol::Address.
[out]addressreference to the HF::Protocol::Address to update.

Definition at line 956 of file base.cpp.

957 {
958  addr.mod = (uint16_t) node.get("mod", 0).asUInt();
959  addr.device = (uint16_t) node.get("device", 0).asUInt();
960  addr.unit = (uint8_t) node.get("unit", 0).asUInt();
961 }

◆ from_json() [6/6]

void from_json ( Json::Value &  node,
HF::Core::BindManagement::Entry entry 
)

Fill a HF::Core::BindManagement::Entry from the given Json::Value object.

Parameters
[in]nodereference to the Json::Value containing the fields for the HF::Core::BindManagement::Entry.
[out]entryreference to the HF::Core::BindManagement::Entry to update.

Definition at line 970 of file base.cpp.

971 {
972  from_json(node["src"], entry.source);
973  from_json(node["dst"], entry.destination);
974  from_json(node["itf"], entry.itf);
975 }
Protocol::Address source
Source Address.
Common::Interface itf
Destination Interface.
void from_json(Json::Value &node, HF::Common::Interface &interface)
Fill a HF::Common::Interface from the given Json::Value object.
Definition: base.cpp:845
Protocol::Address destination
Destination Address.

◆ has_bind()

bool Base::has_bind ( uint16_t  dev_addr_1,
uint16_t  dev_addr_2 
)

Check if bind exists.

Parameters
[in]dev_addr_1HAN-FUN address of the first device.
[in]dev_addr_2HAN-FUN address of the second device.
Return values
trueif the binding exist;
falseotherwise.

Definition at line 613 of file base.cpp.

References HF::Interface::ON_OFF, and HF::Interface::SERVER_ROLE.

614 {
615  HF::Protocol::Address source(dev_addr_1, 1);
617  HF::Protocol::Address destination(dev_addr_2, 1);
618 
619  return _unit0.bind_management()->entries().find(source, itf, destination) != nullptr;
620 }
Network Address.
Definition: protocol.h:201
ON-OFF interface UID.
Definition: interface.h:79

◆ Initialize()

void HF::Application::Initialize ( HF::Transport::Layer transport)

Initialize the application.

Parameters
[in]transportreference to the transport layer to use.

Definition at line 441 of file base_app.cpp.

442 {
443  LOG(TRACE) << __PRETTY_FUNCTION__ << NL;
444 
445  transport.initialize();
446 
447  transport.add(&base);
448 
449  COMMAND_ADD(ListRegs);
450  COMMAND_ADD(ListBinds);
451  COMMAND_ADD(Register);
452  COMMAND_ADD(Deregister);
453  COMMAND_ADD(Bind);
454  COMMAND_ADD(Unbind);
455  COMMAND_ADD(GlobalBind);
456  COMMAND_ADD(On);
457  COMMAND_ADD(Off);
458  COMMAND_ADD(Toggle);
459  COMMAND_ADD(Raw);
460  COMMAND_ADD(DevInfo);
461  COMMAND_ADD(Links);
462 
463  Restore();
464 }
#define COMMAND_ADD(_name)
Helper macro to add a new command to the registry.
Definition: apps/common.h:166
virtual void initialize()=0
Initialize the associated transport layer.
virtual void add(Endpoint *ep)=0
Register the given Endpoint to receive events.
#define NL
Helper define for new-line and stream clear.
Definition: debug.h:34
void Restore()
Restore application configuration.
Definition: base_app.cpp:498
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81

◆ initialize()

void HF::Application::Transport::initialize ( )
virtual

Initialize the associated transport layer.

This method should initialize the transport layer using it's default values.

Reimplemented from HF::Transport::AbstractLayer.

Definition at line 474 of file transport.cpp.

References LOG, and HF::Application::Transport::socket.

475 {
476  uv_loop_t *loop = uv_default_loop();
477 
478  uv_tcp_init(loop, &socket);
479 
480  socket.data = this;
481 
482  LOG(TRACE) << __PRETTY_FUNCTION__ << NL;
483 
484  struct sockaddr_in bind_addr = uv_ip4_addr("127.0.0.1", 8000);
485 
486  if (uv_tcp_bind(&socket, bind_addr) != 0)
487  {
488  print_error(loop->last_err);
489  exit(-1);
490  }
491 
492  if (uv_listen((uv_stream_t *) &socket, 128, on_connect) != 0)
493  {
494  print_error(loop->last_err);
495  exit(-2);
496  }
497 }
uv_tcp_t socket
libuv tcp/ip socket.
void print_error(uv_err_t status)
Print a user friendly string for given status code.
Definition: transport.cpp:227
#define NL
Helper define for new-line and stream clear.
Definition: debug.h:34
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81

◆ insert() [1/2]

void Entries::insert ( const Device device)

Insert a device management entry into the database.

This is used to add the entries when reading the configuration from the JSON database.

Parameters
[in]devicedevice entry to add.

Definition at line 63 of file base.cpp.

64 {
65  HF::Core::DeviceManagement::Entries::save(device);
66 }

◆ insert() [2/2]

void Entries::insert ( Entry entry)

Insert a bind management entry into the database.

This is used to add the entries when reading the configuration from the JSON database.

Parameters
[in]entrydevice entry to add.

Definition at line 223 of file base.cpp.

224 {
225  HF::Core::BindManagement::Entries::save(entry);
226 }

◆ next_address()

uint16_t DeviceManagement::Server::next_address ( )
protectedvirtual

Return next available address for registering a device.

Returns
the address to use in the next registration.

Reimplemented from HF::Core::DeviceManagement::IServer.

Definition at line 115 of file base.cpp.

References HF::Protocol::BROADCAST_ADDR, and HF::Core::DeviceManagement::IServer::next_address().

116 {
117  uint16_t result = _next_address;
118 
120 
121  if (result == HF::Protocol::BROADCAST_ADDR)
122  {
124  }
125 
126  return result;
127 }
virtual uint16_t next_address()
Return next available address for registering a device.
constexpr uint16_t BROADCAST_ADDR
HAN-FUN Broadcast - device address.
Definition: protocol.h:45
uint16_t _next_address
The address the next registering device will be assigned.
Definition: base.h:129
+ Here is the call graph for this function:

◆ off()

void SimpleLight::off ( HF::Protocol::Address source)
virtual

Callback that is called when a OFF_CMD message is received.

Parameters
[in]sourcedevice address that sent the command.

Reimplemented from HF::Interfaces::OnOff::Server.

Definition at line 66 of file node.cpp.

References LOG, and SimpleLight::off().

Referenced by SimpleLight::off().

67 {
69  LOG(INFO) << ">>>>>>>>>>>>> SimpleLight : OFF <<<<<<<<<<<<<" << NL;
70 }
Helper template class to implement units.
Definition: units.h:196
#define NL
Helper define for new-line and stream clear.
Definition: debug.h:34
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ on()

void SimpleLight::on ( HF::Protocol::Address source)
virtual

Callback that is called when a ON_CMD message is received.

Parameters
[in]sourcedevice address that sent the command.

Reimplemented from HF::Interfaces::OnOff::Server.

Definition at line 60 of file node.cpp.

References LOG, and SimpleLight::on().

Referenced by SimpleLight::on().

61 {
63  LOG(INFO) << ">>>>>>>>>>>>> SimpleLight : ON <<<<<<<<<<<<<" << NL;
64 }
Helper template class to implement units.
Definition: units.h:196
#define NL
Helper define for new-line and stream clear.
Definition: debug.h:34
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ print_error()

void print_error ( uv_err_t  status)

Print a user friendly string for given status code.

Parameters
[in]statusstatus code to print.

Definition at line 227 of file transport.cpp.

References LOG.

228 {
229  LOG(ERROR) << uv_err_name((uv_err_t &) status) << " - "
230  << uv_strerror((uv_err_t &) status) << NL;
231 }
#define NL
Helper define for new-line and stream clear.
Definition: debug.h:34
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81

◆ receive() [1/2]

void Node::receive ( HF::Protocol::Packet packet,
HF::Common::ByteArray payload,
uint16_t  offset 
)
virtual

Callback to deliver a packet received from the transport layer.

Parameters
[in]packetreference to the received packet.
[in]payloadreference a ByteArray containing the received data.
[in]offsetoffset from where the received data starts on the payload byte array buffer.

Reimplemented from HF::Devices::Node::Abstract< CoreServices >.

Definition at line 45 of file node.cpp.

References LOG, and Node::receive().

Referenced by Node::receive().

46 {
47  LOG(DEBUG) << ">>>>>>>>>>>>> Message Received <<<<<<<<<<<<<" << NL;
48 
49  LOG(TRACE) << "Payload : " << payload << NL;
50 
51  LOG(DEBUG) << packet << NL;
52 
54 }
#define NL
Helper define for new-line and stream clear.
Definition: debug.h:34
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81
void receive(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset) _override
Callback to deliver a packet received from the transport layer.
Definition: devices.h:446
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ receive() [2/2]

void Base::receive ( HF::Protocol::Packet packet,
HF::Common::ByteArray payload,
uint16_t  offset 
)
virtual

Callback to deliver a packet received from the transport layer.

Parameters
[in]packetreference to the received packet.
[in]payloadreference a ByteArray containing the received data.
[in]offsetoffset from where the received data starts on the payload byte array buffer.

Reimplemented from HF::Devices::Concentrator::AbstractBase.

Definition at line 595 of file base.cpp.

References LOG.

596 {
597  LOG(DEBUG) << ">>>>>>>>>>>>> Message Received <<<<<<<<<<<<<" << NL;
598 
599  LOG(TRACE) << "Payload : " << payload << NL;
600 
601  LOG(DEBUG) << std::endl << packet << NL;
602 
604 }
void receive(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Callback to deliver a packet received from the transport layer.
#define NL
Helper define for new-line and stream clear.
Definition: debug.h:34
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81

◆ restore() [1/2]

void DeviceManagement::Server::restore ( Json::Value  root)

Restore the device entries from the JSON database.

Parameters
[in]rootdatabase root to start reading the entries.

Definition at line 184 of file base.cpp.

References LOG.

185 {
186  LOG(INFO) << "Restoring registration entries ..." << NL;
187 
189 
190  for (unsigned i = 0; i < root.size(); i++)
191  {
192  from_json(root[i], device);
194  }
195 }
void from_json(Json::Value &node, HF::Common::Interface &interface)
Fill a HF::Common::Interface from the given Json::Value object.
Definition: base.cpp:845
#define NL
Helper define for new-line and stream clear.
Definition: debug.h:34
Container & entries() const
Get a reference to the current object implementing the persistence API, for the device information...
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81

◆ restore() [2/2]

void BindManagement::Server::restore ( Json::Value  root)

Restore the bind entries from the JSON database.

Parameters
[in]rootdatabase root to start reading the entries.

Definition at line 273 of file base.cpp.

References LOG.

274 {
275  LOG(INFO) << "Restoring binding entries ..." << NL;
276 
277  for (unsigned i = 0; i < root.size(); i++)
278  {
280  from_json(root[i], entry);
281  entries().insert(entry);
282  LOG(TRACE) << "Bind Add : " << entry.source.device
283  << " -> " << entry.destination.device << NL;
284  }
285 }
Protocol::Address source
Source Address.
void from_json(Json::Value &node, HF::Common::Interface &interface)
Fill a HF::Common::Interface from the given Json::Value object.
Definition: base.cpp:845
#define NL
Helper define for new-line and stream clear.
Definition: debug.h:34
This represents a bind entry data structure.
uint16_t device
Device Address.
Definition: protocol.h:204
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81
Protocol::Address destination
Destination Address.

◆ save() [1/2]

void DeviceManagement::Server::save ( Json::Value &  root)

Save the device entries into the JSON database.

Parameters
[in]rootdatabase root to start saving the entries.

Definition at line 161 of file base.cpp.

References LOG.

162 {
163  LOG(INFO) << "Saving registration entries ..." << NL;
164 
165  unsigned i = 0;
166 
167  /* *INDENT-OFF* */
168  for_each( entries ().begin (), entries ().end (),
169  [&root, &i](HF::Core::DeviceManagement::Device &device)
170  {
171  to_json (device, root[i]);
172  i++;
173  });
174  /* *INDENT-ON* */
175 }
#define NL
Helper define for new-line and stream clear.
Definition: debug.h:34
void to_json(const HF::Common::Interface &interface, Json::Value &node)
Serialize a HF::Common::Interface to the given Json::Value.
Definition: base.cpp:702
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81

◆ save() [2/2]

void BindManagement::Server::save ( Json::Value &  root)

Save the bind entries into the JSON database.

Parameters
[in]rootdatabase root to start saving the entries.

Definition at line 250 of file base.cpp.

References LOG.

251 {
252  LOG(INFO) << "Saving binding entries ..." << NL;
253 
254  unsigned i = 0;
255 
256  /* *INDENT-OFF* */
257  for_each(entries().begin(), entries().end(),
258  [&root, &i](const HF::Core::BindManagement::Entry &entry)
259  {
260  to_json (entry, root[i]);
261  i++;
262  });
263  /* *INDENT-ON* */
264 }
#define NL
Helper define for new-line and stream clear.
Definition: debug.h:34
This represents a bind entry data structure.
void to_json(const HF::Common::Interface &interface, Json::Value &node)
Serialize a HF::Common::Interface to the given Json::Value.
Definition: base.cpp:702
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81

◆ send()

void HF::Application::Link::send ( HF::Common::ByteArray array)
virtual

Send the data in the given ByteArray using the link to the remote end-point.

Parameters
[in]arrayreference to the ByteArray containing the data to send.

Implements HF::Transport::Link.

Definition at line 588 of file transport.cpp.

References LOG.

589 {
590  LOG(TRACE) << __PRETTY_FUNCTION__ << NL;
591 
592  msg_t msg(DATA_MSG);
593 
594  msg.data = array;
595 
596  send_message((uv_stream_t *) stream, msg);
597 }
Message sent between the concentrator and the nodes.
Definition: transport.cpp:60
#define NL
Helper define for new-line and stream clear.
Definition: debug.h:34
#define DATA_MSG
Data message primitive id.
Definition: transport.cpp:45
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81

◆ to_json() [1/6]

void to_json ( HF::UID::UID uid,
Json::Value &  node 
)

Serialize a HF::UID::UID pointer to the given Json::Value.

Parameters
[in]uidpointer to the HF::UID::UID to serialize to JSON.
[out]nodereference to the Json::Value to place the serialization result in.

◆ to_json() [2/6]

void to_json ( const HF::Common::Interface interface,
Json::Value &  node 
)

Serialize a HF::Common::Interface to the given Json::Value.

Parameters
[in]interfacereference to the HF::Common::Interface to serialize to JSON.
[out]nodereference to the Json::Value to place the serialization result in.

Definition at line 702 of file base.cpp.

703 {
704  if (interface.role == HF::Interface::SERVER_ROLE)
705  {
706  node["role"] = "server";
707  }
708  else
709  {
710  node["role"] = "client";
711  }
712 
713  node["id"] = json_uid(interface.id);
714 }
uint16_t id
Identifier of the interface.
uint16_t role
Interface role : Server or Client.

◆ to_json() [3/6]

void to_json ( const HF::Core::DeviceManagement::Unit unit,
Json::Value &  node 
)

Serialize a HF::Core::DeviceManagement::Unit to the given Json::Value.

Parameters
[in]unitreference to the HF::Core::DeviceManagement::Unit to serialize to JSON.
[out]nodereference to the Json::Value to place the serialization result in.

Definition at line 777 of file base.cpp.

778 {
779  node["id"] = unit.id;
780 
781  node["profile"] = json_uid(unit.profile);
782 
783  for (unsigned i = 0; i < unit.interfaces.size(); i++)
784  {
785  to_json(unit.interfaces[i], node["opts"][i]);
786  }
787 }
void to_json(const HF::Common::Interface &interface, Json::Value &node)
Serialize a HF::Common::Interface to the given Json::Value.
Definition: base.cpp:702
std::vector< Common::Interface > interfaces
Optional interfaces.

◆ to_json() [4/6]

void to_json ( const HF::Core::DeviceManagement::Device device,
Json::Value &  node 
)

Serialize a HF::Core::DeviceManagement::Device to the given Json::Value.

Parameters
[in]devicereference to the HF::Core::DeviceManagement::Device to serialize to JSON.
[out]nodereference to the Json::Value to place the serialization result in.

Definition at line 796 of file base.cpp.

797 {
798  node["address"] = device.address;
799 
800  for (unsigned i = 0; i < device.units.size(); i++)
801  {
802  to_json(device.units[i], node["units"][i]);
803  }
804 
805  node["emc"] = json_uid(device.emc);
806 
807  to_json(device.uid, node["uid"]);
808 }
uint16_t address
Device Address.
uint16_t emc
Device EMC if applicable, 0 otherwise.
void to_json(const HF::Common::Interface &interface, Json::Value &node)
Serialize a HF::Common::Interface to the given Json::Value.
Definition: base.cpp:702
std::vector< Unit > units
Unit list of the interface.

◆ to_json() [5/6]

void to_json ( const HF::Protocol::Address address,
Json::Value &  node 
)

Serialize a HF::Protocol::Address to the given Json::Value.

Parameters
[in]addressreference to the HF::Protocol::Address to serialize to JSON.
[out]nodereference to the Json::Value to place the serialization result in.

Definition at line 817 of file base.cpp.

818 {
819  node["mod"] = (int) address.mod;
820  node["device"] = (int) address.device;
821  node["unit"] = (int) address.unit;
822 }
uint8_t unit
Source Unit.
Definition: protocol.h:206
uint16_t mod
Address modifier.
Definition: protocol.h:203
uint16_t device
Device Address.
Definition: protocol.h:204

◆ to_json() [6/6]

void to_json ( const HF::Core::BindManagement::Entry entry,
Json::Value &  node 
)

Serialize a HF::Core::BindManagement::Entry to the given Json::Value.

Parameters
[in]entryreference to the HF::Core::BindManagement::Entry to serialize to JSON.
[out]nodereference to the Json::Value to place the serialization result in.

Definition at line 831 of file base.cpp.

832 {
833  to_json(entry.source, node["src"]);
834  to_json(entry.destination, node["dst"]);
835  to_json(entry.itf, node["itf"]);
836 }
Protocol::Address source
Source Address.
Common::Interface itf
Destination Interface.
void to_json(const HF::Common::Interface &interface, Json::Value &node)
Serialize a HF::Common::Interface to the given Json::Value.
Definition: base.cpp:702
Protocol::Address destination
Destination Address.

◆ toggle()

void SimpleLight::toggle ( HF::Protocol::Address source)
virtual

Callback that is called when a TOGGLE_CMD message is received.

Parameters
[in]sourcedevice address that sent the command.

Reimplemented from HF::Interfaces::OnOff::Server.

Definition at line 72 of file node.cpp.

References LOG, and SimpleLight::toggle().

Referenced by SimpleLight::toggle().

73 {
75  LOG(INFO) << ">>>>>>>>>>>>> SimpleLight : TOGGLE <<<<<<<<<<<<<" << NL;
76 }
Helper template class to implement units.
Definition: units.h:196
#define NL
Helper define for new-line and stream clear.
Definition: debug.h:34
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ unbind()

bool Base::unbind ( uint16_t  dev_addr_1,
uint16_t  dev_addr_2 
)

Remove the binding entry for the given devices.

If the bind does not exist, nothing changes.

Parameters
[in]dev_addr_1HAN-FUN address of the first device.
[in]dev_addr_2HAN-FUN address of the second device.
Return values
trueif the bind entry was found and removed,
falseotherwise.

Definition at line 667 of file base.cpp.

References HF::Common::OK, HF::Interface::ON_OFF, and HF::Interface::SERVER_ROLE.

668 {
669  HF::Protocol::Address source(dev_addr_1, 1);
671  HF::Protocol::Address destination(dev_addr_2, 1);
672 
673  return _unit0.bind_management()->remove(source, destination, itf) == HF::Common::Result::OK;
674 }
Network Address.
Definition: protocol.h:201
ON-OFF interface UID.
Definition: interface.h:79