HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
example_04.cpp
Go to the documentation of this file.
1 // =============================================================================
15 // =============================================================================
16 
17 #include <assert.h>
18 
19 #include "hanfun.h"
20 #include "hanfun/debug.h"
21 
22 #include "localloop.h"
23 
24 // =============================================================================
25 // Implementation
26 // =============================================================================
27 
28 namespace
29 {
30  /*
31  * Unit implementing the Simple On-Off Switch profile.
32  */
33  struct SimpleSwitch: public HF::Units::Unit<HF::Profiles::SimpleOnOffSwitch>
34  {
35  SimpleSwitch(uint8_t id, HF::IDevice &device):
36  HF::Units::Unit<HF::Profiles::SimpleOnOffSwitch>(id, device)
37  {}
38  };
39 
40  /*
41  * Unit implementing the Simple OnOff Switchable profile.
42  */
43  struct SimpleSwitchable: public HF::Units::Unit<HF::Profiles::SimpleOnOffSwitchable>
44  {
46 
47  SimpleSwitchable(uint8_t id, HF::IDevice &device): _Parent(id, device)
48  {}
49 
50  void on(HF::Protocol::Address &source)
51  {
52  _Parent::on(source);
53  LOG(INFO) << "Command ON received ..." << NL;
54  }
55 
56  void off(HF::Protocol::Address &source)
57  {
58  _Parent::off(source);
59  LOG(INFO) << "Command OFF received ..." << NL;
60  }
61 
62  void toggle(HF::Protocol::Address &source)
63  {
64  _Parent::toggle(source);
65  LOG(INFO) << "Command TOGGLE received ..." << NL;
66  }
67  };
68 
69  /*
70  * Example node.
71  */
72  struct Node: public HF::Devices::Node::Node
73  {};
74 
75  /*
76  * Example concentrator.
77  */
79  {};
80 
81 } // namespace
82 
83 // =============================================================================
84 // MAIN
85 // =============================================================================
86 
87 int main(int argc, char **argv)
88 {
89  UNUSED(argc);
90  UNUSED(argv);
91 
92  LOG(INFO) << "Use case : On-Off interface usage" << NL;
93 
94  /*
95  * Each node variable is a remote device, i.e.,
96  * the Node variables are declared on the remote device
97  * code and are not used on the base code.
98  */
99  LOG(INFO) << "Create the node instances ..." << NL;
100 
101  Node node1;
102 
103  LOG(INFO) << "Add unit to node1 instance ..." << NL;
104  SimpleSwitch simple_switch(1, node1);
105 
106  Node node2;
107 
108  LOG(INFO) << "Add unit to node2 instance ..." << NL;
109  SimpleSwitchable simple_switchable(1, node2);
110 
111  /*
112  * This instance represents the base application.
113  */
114  LOG(INFO) << "Create the base instance ..." << NL;
115  Base base;
116 
117  LOG(INFO) << "Create transport instance" << NL;
118  Localloop loop;
119 
120  /*
121  * Setup the network.
122  *
123  * This simulates the devices connecting to the base using for
124  * example a TCP/IP connection or a DECT ULE PVC.
125  */
126  LOG(INFO) << "Network setup ..." << NL;
127 
128  loop.set_base(&base);
129  loop.add_node(&node1, "node_1");
130  loop.add_node(&node2, "node_2");
131 
132  // Register the two devices.
133 
134  // Node 1 is unregistered.
135  assert(node1.address() == HF::Protocol::BROADCAST_ADDR);
136 
137  LOG(INFO) << "Registering node1 ... " << NL;
138  node1.unit0()->device_management()->register_device();
139  LOG(INFO) << "Node1 address ... " << node1.address() << NL;
140 
141  // Node 1 is registered
142  assert(node1.address() == 1);
143 
144  // Node 2 is unregistered.
145  assert(node2.address() == HF::Protocol::BROADCAST_ADDR);
146 
147  LOG(INFO) << "Registering node2 ... " << NL;
148  node2.unit0()->device_management()->register_device();
149  LOG(INFO) << "Node2 address ... " << node2.address() << NL;
150 
151  // Node 2 is registered
152  assert(node2.address() == 2);
153 
154  LOG(INFO) << "There should be two registered devices ... "
155  << base.unit0()->device_management()->entries().size() << NL;
156 
157  assert(base.unit0()->device_management()->entries().size() == 2);
158 
159  // =============================================================================
160  // Send ON/OFF/TOGGLE to a specific device/unit
161  // =============================================================================
162 
163  LOG(INFO) << "Send commands to a specific device/unit ..." << NL;
164 
165  HF::Protocol::Address addr(2, 1);
166 
167  // On Command.
168  LOG(INFO) << "Send a ON command ..." << NL;
169  simple_switch.on(addr);
170  LOG(INFO) << "Switchable state should be ON : " << simple_switchable.state() << NL;
171  assert(simple_switchable.state() == true);
172 
173  // Off Command.
174  LOG(INFO) << "Send a OFF command ..." << NL;
175  simple_switch.off(addr);
176  LOG(INFO) << "Switchable state should be OFF : " << simple_switchable.state() << NL;
177  assert(simple_switchable.state() == false);
178 
179  // Toggle Command
180  LOG(INFO) << "Send a TOGGLE command ... " << NL;
181  simple_switch.toggle(addr);
182  LOG(INFO) << "Switchable state should be ON : " << simple_switchable.state() << NL;
183  assert(simple_switchable.state() == true);
184 
185  LOG(INFO) << "Send a second TOGGLE command ... " << NL;
186  simple_switch.toggle(addr);
187  LOG(INFO) << "Switchable state should be OFF : " << simple_switchable.state() << NL;
188  assert(simple_switchable.state() == false);
189 
190  // =============================================================================
191  // Send ON/OFF/TOGGLE to the broadcast device/unit
192  // =============================================================================
193 
194  LOG(INFO) << "Send commands to the broadcast device/unit ..." << NL;
195 
196  // Create a bind entry.
197  LOG(INFO) << "Create bind entry on the base ... " << NL;
198  HF::Protocol::Address source(1, 1);
199  HF::Protocol::Address destination(2, 1);
201 
202  base.unit0()->bind_management()->add(source, destination, itf);
203  LOG(INFO) << "There should be one bind entry ... "
204  << base.unit0()->bind_management()->entries().size() << NL;
205  assert(base.unit0()->bind_management()->entries().size() == 1);
206 
207  // On Command.
208  LOG(INFO) << "Send a ON command ..." << NL;
209  simple_switch.on();
210  LOG(INFO) << "Switchable state should be ON : " << simple_switchable.state() << NL;
211  assert(simple_switchable.state() == true);
212 
213  // Off Command.
214  LOG(INFO) << "Send a OFF command ..." << NL;
215  simple_switch.off();
216  LOG(INFO) << "Switchable state should be OFF : " << simple_switchable.state() << NL;
217  assert(simple_switchable.state() == false);
218 
219  // Toggle Command
220  LOG(INFO) << "Send a TOGGLE command ... " << NL;
221  simple_switch.toggle();
222  LOG(INFO) << "Switchable state should be ON : " << simple_switchable.state() << NL;
223  assert(simple_switchable.state() == true);
224 
225  LOG(INFO) << "Send a second TOGGLE command ... " << NL;
226  simple_switch.toggle();
227  LOG(INFO) << "Switchable state should be OFF : " << simple_switchable.state() << NL;
228  assert(simple_switchable.state() == false);
229 
230  return 0;
231 }
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
SimpleSwitch(uint8_t index, HF::IDevice &device)
Constructor.
Definition: node.h:74
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
Unit(uint8_t id, IDevice &device)
Constructor.
Definition: units.h:212
This file contains an implementation of a HAN-FUN transport layer to be used in the example applicati...
ON-OFF interface UID.
Definition: interface.h:79
#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