HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
example_03.cpp
Go to the documentation of this file.
1 // =============================================================================
15 // =============================================================================
16 
17 #include "hanfun.h"
18 #include "hanfun/debug.h"
19 
20 #include "localloop.h"
21 
22 // =============================================================================
23 // Implementation
24 // =============================================================================
25 
26 namespace
27 {
28  /*
29  * Unit implementing the Simple Detector profile.
30  */
31  struct SimpleDetector: public HF::Units::Unit<HF::Profiles::SimpleDetector>
32  {
33  SimpleDetector(uint8_t id, HF::IDevice &device):
34  HF::Units::Unit<HF::Profiles::SimpleDetector>(id, device)
35  {}
36  };
37 
38  /*
39  * Unit implementing the Alertable profile, to receive alert events.
40  */
41  struct AlertSink: public HF::Units::Unit<HF::Profiles::Alertable>
42  {
43  AlertSink(uint8_t id, HF::IDevice &device):
44  HF::Units::Unit<HF::Profiles::Alertable>(id, device)
45  {}
46 
47  void status(HF::Protocol::Address &source, HF::Interfaces::Alert::Message &message)
48  {
50 
51  LOG(INFO) << "Alert received " << std::hex << std::showbase
52  << message.state << " !" << NL;
53  }
54  };
55 
56  /*
57  * Example node.
58  */
59  struct Node: public HF::Devices::Node::Node
60  {};
61 
62  /*
63  * Example concentrator.
64  */
66  {};
67 
68 } // namespace
69 
70 // =============================================================================
71 // MAIN
72 // =============================================================================
73 
74 int main(int argc, char **argv)
75 {
76  UNUSED(argc);
77  UNUSED(argv);
78 
79  LOG(INFO) << "Use case : Alert interface usage" << NL;
80 
81  /*
82  * Each node variable is a remote device, i.e.,
83  * the Node variables are declared on the remote device
84  * code and are not used on the base code.
85  */
86  LOG(INFO) << "Create the node instances ..." << NL;
87 
88  Node node1;
89 
90  LOG(INFO) << "Add unit to node1 instance ..." << NL;
91  SimpleDetector simple_detector(1, node1);
92 
93  Node node2;
94 
95  LOG(INFO) << "Add unit to node2 instance ..." << NL;
96  AlertSink alert_sink(1, node2);
97 
98  /*
99  * This instance represents the base application.
100  */
101  LOG(INFO) << "Create the base instance ..." << NL;
102  Base base;
103 
104  LOG(INFO) << "Create transport instance" << NL;
105  Localloop loop;
106 
107  /*
108  * Setup the network.
109  *
110  * This simulates the devices connecting to the base using for
111  * example a TCP/IP connection or a DECT ULE PVC.
112  */
113  LOG(INFO) << "Network setup ..." << NL;
114 
115  loop.set_base(&base);
116  loop.add_node(&node1, "node_1");
117  loop.add_node(&node2, "node_2");
118 
119  // Register the two devices.
120 
121  // Node 1 is unregistered.
122  assert(node1.address() == HF::Protocol::BROADCAST_ADDR);
123 
124  LOG(INFO) << "Registering node1 ... " << NL;
125  node1.unit0()->device_management()->register_device();
126  LOG(INFO) << "Node1 address ... " << node1.address() << NL;
127 
128  // Node 1 is registered
129  assert(node1.address() == 1);
130 
131  // Node 2 is unregistered.
132  assert(node2.address() == HF::Protocol::BROADCAST_ADDR);
133 
134  LOG(INFO) << "Registering node2 ... " << NL;
135  node2.unit0()->device_management()->register_device();
136  LOG(INFO) << "Node2 address ... " << node2.address() << NL;
137 
138  // Node 2 is registered
139  assert(node2.address() == 2);
140 
141  LOG(INFO) << "There should be two registered devices ... "
142  << base.unit0()->device_management()->entries().size() << NL;
143 
144  assert(base.unit0()->device_management()->entries().size() == 2);
145 
146  // =============================================================================
147  // Send alerts a specific device/unit.
148  // =============================================================================
149 
150  LOG(INFO) << "Send alerts to a specific device/unit ... " << NL;
151 
152  HF::Protocol::Address addr(2, 1);
153 
154  LOG(INFO) << "Send alert on ..." << NL;
155  simple_detector.alert(addr, true);
156 
157  LOG(INFO) << "Send alert off ... " << NL;
158  simple_detector.alert(addr, false);
159 
160  // =============================================================================
161  // Send alerts to the broadcast device/unit.
162  // =============================================================================
163 
164  LOG(INFO) << "Send alerts to the broadcast device/unit ... " << NL;
165 
166  // Create a bind entry.
167  LOG(INFO) << "Create bind entry on the base ... " << NL;
168  HF::Protocol::Address source(1, 1);
169  HF::Protocol::Address destination(2, 1);
171 
172  base.unit0()->bind_management()->add(source, destination, itf);
173  LOG(INFO) << "There should be one bind entry ... "
174  << base.unit0()->bind_management()->entries().size() << NL;
175  assert(base.unit0()->bind_management()->entries().size() == 1);
176 
177  LOG(INFO) << "Send alert on to the broadcast device/unit ... " << NL;
178  simple_detector.alert(true);
179 
180  LOG(INFO) << "Send alert off to the broadcast device/unit ... " << NL;
181  simple_detector.alert(false);
182 
183  return 0;
184 }
This class represents a HAN-FUN Concentrator.
Definition: base.h:302
This is the top level include file for the HAN-FUN library.
This file contains the prototypes of the debug functionality in HAN-FUN.
constexpr uint16_t BROADCAST_ADDR
HAN-FUN Broadcast - device address.
Definition: protocol.h:45
Payload for the Status command.
Definition: alert.h:86
uint32_t state
Current state of the server.
Definition: alert.h:93
Alert interface UID.
Definition: interface.h:78
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
Template for HAN-FUN concentrator devices.
Definition: devices.h:906
#define NL
Helper define for new-line and stream clear.
Definition: debug.h:34
CoreServices * unit0() const
Get the unit 0 used by this concentrator device.
Definition: devices.h:910
Network Address.
Definition: protocol.h:201
This file contains an implementation of a HAN-FUN transport layer to be used in the example applicati...
#define UNUSED(x)
Helper macro to remove warning about unused function/method argument.
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81
Template for declaring HAN-FUN node devices.
Definition: devices.h:425
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22