24 #include <forward_list> 35 #include "json/json.h" 46 #ifndef HF_APP_CONFIG_FILE 47 #define HF_APP_CONFIG_FILE "./hanfun.json" 64 COMMAND(ListRegs,
"lr",
"lr:list registrations.")
68 auto &devices = base.
unit0()->device_management()->entries();
70 LOG(APP) << std::setfill(
' ');
71 LOG(APP) <<
"HAN-FUN" <<
" Registered Devices (" << (int) devices.size() <<
"):" <<
NL;
76 LOG (APP) << (base.
link (device.address) !=
nullptr ?
"+ " :
"- ");
77 LOG (APP) << std::right << std::setw (5) << device.address <<
" | ";
78 LOG (APP) << device.uid <<
NL;
86 COMMAND(ListBinds,
"lb",
"lb:list binds.")
92 LOG(APP) <<
"HAN-FUN Binds (" << entries.
size() <<
"):" <<
NL;
93 LOG(APP) <<
"\t" << std::left << std::setfill(
' ')
94 << std::setw(16) <<
"Source" 95 << std::setw(18) <<
"| Destination" 96 <<
"| Interface" << std::endl
97 <<
"\t" << std::setfill(
'-')
98 << std::setw(15) <<
"-" 99 << std::setw(18) <<
" | " 100 << std::setw(12) <<
" | " << std::endl
101 << std::setfill(
' ');
104 std::for_each (entries.begin (), entries.end (),
107 LOG (APP) <<
"\t" << entry.source <<
" | " << entry.destination <<
" | " 116 COMMAND(Register,
"r",
"r 1 x:register device x.\nr 0:exit registration mode.")
118 if (args.size() > 0 && args[0] ==
"0")
120 #ifdef HF_APP_EXT_REG 122 if (HF::Application::Registration(
false))
124 LOG(INFO) <<
"Disable Registration mode: SUCCESS" <<
NL;
128 LOG(WARN) <<
"Disable Registration mode: FAIL" <<
NL;
133 else if (args.size() > 1 && args[0] ==
"1")
135 uint16_t address =
STRTOL(args[1]);
137 if (base.
unit0()->device_management()->available(address) && address != 0 &&
140 base.
unit0()->device_management()->next_address(address);
141 #ifdef HF_APP_EXT_REG 143 if (HF::Application::Registration(
true))
145 LOG(INFO) <<
"Enable Registration mode: SUCCESS" <<
NL;
149 LOG(WARN) <<
"[HANFUN] Enable Registration mode: FAIL" <<
NL;
153 LOG(INFO) <<
"Next Registration will have address : " << (int) address <<
NL;
157 LOG(ERROR) <<
"Registration impossible: address " << (int) address
158 <<
" not available." <<
NL;
164 LOG(APP) << usage(
true) <<
NL;
170 COMMAND(Deregister,
"d",
"d x:de-register device x.")
174 LOG(APP) << usage(
true) <<
NL;
178 uint16_t address =
STRTOL(args[0]);
180 #ifdef HF_APP_EXT_REG 182 HF::Application::Deregister(address);
186 bool res = base.
unit0()->device_management()->deregister(address);
187 LOG(INFO) <<
"Device " << (int) address <<
" de-registration: " 188 << (res ?
"SUCCESS" :
"FAIL") <<
" !" <<
NL;
194 COMMAND(Bind,
"b",
"b x y:associate device x with device y (bind).")
198 LOG(APP) << usage(
true) <<
NL;
202 uint16_t arg1 =
STRTOL(args[0]);
205 uint8_t err = base.
bind(arg1,
arg2);
211 LOG(INFO) <<
"Bind created !" <<
NL;
216 LOG(WARN) <<
"Bind already created !" <<
NL;
222 LOG(ERROR) <<
"Bind impossible: " <<
NL;
224 if ((err & 0x01) != 0)
226 LOG(ERROR) <<
" - Second device does not exist !" <<
NL;
230 LOG(ERROR) <<
" - Fist device does not exist !" <<
NL;
236 LOG(ERROR) <<
"No match for bind !" <<
NL;
244 COMMAND(Unbind,
"u",
"u x y:unbind device x with y.")
250 LOG(APP) << usage(
true) <<
NL;
254 uint16_t arg1 =
STRTOL(args[0]);
259 LOG(INFO) <<
"Bind: " << arg1 <<
" - " <<
arg2 <<
" removed !" <<
NL;
263 LOG(ERROR) <<
"Bind: does not exist !" <<
NL;
270 COMMAND(GlobalBind,
"gb",
"gb 1:create bind to receive all interface events.\n" 271 "gb 0:remove bind to receive all interface events.")
275 LOG(APP) << usage(
true) <<
NL;
279 uint16_t arg1 =
STRTOL(args[0]);
288 auto res = base.
unit0()->bind_management()->add(source, dest, itf);
295 auto res = base.
unit0()->bind_management()->remove(source, dest, itf);
301 LOG(APP) << usage(
true) <<
NL;
308 COMMAND(On,
"on",
"on d u:Send an ON command to device/unit pair.")
312 LOG(APP) << usage(
true) <<
NL;
316 uint16_t arg1 =
STRTOL(args[0]);
320 base.commands.on_off().on(device);
326 COMMAND(Off,
"off",
"off d u:Send an OFF command to device/unit pair.")
330 LOG(APP) << usage(
true) <<
NL;
334 uint16_t arg1 =
STRTOL(args[0]);
338 base.commands.on_off().off(device);
344 COMMAND(Toggle,
"toggle",
"toggle d u:Send a TOGGLE command to device/unit pair.")
348 LOG(APP) << usage(
true) <<
NL;
352 uint16_t arg1 =
STRTOL(args[0]);
356 base.commands.on_off().toggle(device);
359 COMMAND(Raw,
"raw",
"raw <raw data>:simulate receiving a packet from the network.")
363 data.reserve(args.size());
366 std::for_each (args.begin (), args.end (), [&data](std::string byte)
369 data.push_back (temp);
374 uint16_t offset = packet.
unpack(data);
376 packet.link = base.
link(packet.source.device);
377 base.
receive(packet, data, offset);
383 COMMAND(DevInfo,
"di",
"di m d:Get device information mandatory attributes.\n" 384 "di a d:Get device information all attributes.")
388 LOG(APP) << usage(
true) <<
NL;
392 uint16_t arg1 =
STRTOL(args[1]);
402 else if (args[0] ==
"a")
409 base.commands.send(device, *msg,
nullptr);
414 LOG(APP) << usage(
true) <<
NL;
418 COMMAND(Links,
"ll",
"ll :list links.")
422 auto &links = base.links();
427 LOG (APP) << std::setw (5) << link->address() <<
" | ";
428 LOG (APP) << link->uid() <<
NL;
443 LOG(TRACE) << __PRETTY_FUNCTION__ <<
NL;
447 transport.
add(&base);
476 Json::StyledWriter writer;
479 base.
unit0()->device_management()->save(root[
"core"][
"device_management"]);
480 base.
unit0()->bind_management()->save(root[
"core"][
"bind_management"]);
505 if (reader.parse(ifs, root,
false) ==
false)
507 LOG(WARN) <<
"Reading configuration file !!" << reader.getFormattedErrorMessages() <<
NL;
511 base.
unit0()->device_management()->restore(root[
"core"][
"device_management"]);
512 base.
unit0()->bind_management()->restore(root[
"core"][
"bind_management"]);
This class represents a HAN-FUN Concentrator.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
This is the top level include file for the HAN-FUN library.
This file contains the definitions for the HAN-FUN example applications.
void Saved()
Callback indicating that the application configuration has been saved.
uint16_t arg2
Create bind command.
Default implementation of the bind entries container.
This class defines the API for the transport layer.
This interface represents a link on the transport layer, used to send data to the remote end-point...
constexpr uint16_t BROADCAST_ADDR
HAN-FUN Broadcast - device address.
Protocol::Message * all()
Create a message that can be used to retrieve all the attributes on a remote device.
#define STRTOL_HEX(X)
Helper macro to convert a std::string into a number (base 16).
void Restored()
Callback indicating that the application configuration has been restored.
#define COMMAND_ADD(_name)
Helper macro to add a new command to the registry.
virtual void initialize()=0
Initialize the associated transport layer.
virtual void add(Endpoint *ep)=0
Register the given Endpoint to receive events.
#define STRTOL(X)
Helper macro to convert a std::string into a number (base 10).
This class represents a byte array.
#define NL
Helper define for new-line and stream clear.
#define HF_APP_CONFIG_FILE
JSON database file.
CoreServices * unit0() const
Get the unit 0 used by this concentrator device.
bool unbind(uint16_t dev_addr_1, uint16_t dev_addr_2)
Remove the binding entry for the given devices.
This file contains the definition of the Base class that represents the HAN-FUN Concentrator on the a...
HF::Transport::Link * link(uint16_t addr) const
Return the link that can be used to send a packet to the device with the given address.
This represents a bind entry data structure.
void receive(HF::Protocol::Packet &packet, HF::Common::ByteArray &payload, uint16_t offset)
Callback to deliver a packet received from the transport layer.
#define UNUSED(x)
Helper macro to remove warning about unused function/method argument.
void Restore()
Restore application configuration.
COMMAND(ListRegs, "lr", "lr:list registrations.")
ListRegs List registrations command.
This file contains the definitions for the common functionality in the HAN-FUN example applications...
#define LOG(X)
Log messages with the level given by X.
uint8_t bind(uint16_t dev_addr_1, uint16_t dev_addr_2)
Create a new bind entry.
Protocol::Message * mandatory()
Create a message that can be used to retrieve the mandatory attributes on a remote device...
void Save()
Save application configuration.
uint16_t size() const
Return the number of entries in the container.
void Initialize(HF::Transport::Layer &transport)
Initialize the application.