HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
example_05.cpp
Go to the documentation of this file.
1 // =============================================================================
15 // =============================================================================
16 
17 #include <cassert>
18 #include <cmath>
19 
20 #include "hanfun.h"
21 #include "hanfun/debug.h"
22 
23 #include "localloop.h"
24 
25 // =============================================================================
26 // Implementation
27 // =============================================================================
28 
29 namespace
30 {
31  /*
32  * Unit implementing the Simple %Level Control profile.
33  */
34  struct SimpleLevelControl: public HF::Units::Unit<HF::Profiles::SimpleLevelControl>
35  {
36  SimpleLevelControl(uint8_t id, HF::IDevice &device):
37  HF::Units::Unit<HF::Profiles::SimpleLevelControl>(id, device)
38  {}
39  };
40 
41  /*
42  * Unit implementing the Simple Level Controllable profile.
43  */
44  struct SimpleLevelControllable: public HF::Units::Unit<HF::Profiles::SimpleLevelControllable>
45  {
47 
48  SimpleLevelControllable(uint8_t id, HF::IDevice &device):
49  _Parent(id, device)
50  {}
51 
52  void level_change(HF::Protocol::Address &source, uint8_t old_level, uint8_t new_level)
53  {
54  _Parent::level_change(source, old_level, new_level);
55  LOG(INFO) << "Level Change : "
56  << (int) HF::Common::to_percent(old_level)
57  << "\% -> "
58  << (int) HF::Common::to_percent(new_level)
59  << "\%" << NL;
60  }
61  };
62 
63  /*
64  * Example node.
65  */
66  struct Node: public HF::Devices::Node::Node
67  {};
68 
69  /*
70  * Example concentrator.
71  */
73  {};
74 
75 } // namespace
76 
77 // =============================================================================
78 // MAIN
79 // =============================================================================
80 
81 int main(int argc, char **argv)
82 {
83  UNUSED(argc);
84  UNUSED(argv);
85 
86  LOG(INFO) << "Use case : Level Control interface usage" << NL;
87 
88  /*
89  * Each node variable is a remote device, i.e.,
90  * the Node variables are declared on the remote device
91  * code and are not used on the base code.
92  */
93  LOG(INFO) << "Create the node instances ..." << NL;
94 
95  Node node1;
96 
97  LOG(INFO) << "Add unit to node1 instance ..." << NL;
98  SimpleLevelControl level_control(1, node1);
99 
100  Node node2;
101 
102  LOG(INFO) << "Add unit to node2 instance ..." << NL;
103  SimpleLevelControllable level_controllable(1, node2);
104 
105  /*
106  * This instance represents the base application.
107  */
108  LOG(INFO) << "Create the base instance ..." << NL;
109  Base base;
110 
111  LOG(INFO) << "Create transport instance" << NL;
112  Localloop loop;
113 
114  /*
115  * Setup the network.
116  *
117  * This simulates the devices connecting to the base using for
118  * example a TCP/IP connection or a DECT ULE PVC.
119  */
120  LOG(INFO) << "Network setup ..." << NL;
121 
122  loop.set_base(&base);
123  loop.add_node(&node1, "node_1");
124  loop.add_node(&node2, "node_2");
125 
126  // Register the two devices.
127 
128  // Node 1 is unregistered.
129  assert(node1.address() == HF::Protocol::BROADCAST_ADDR);
130 
131  LOG(INFO) << "Registering node1 ... " << NL;
132  node1.unit0()->device_management()->register_device();
133  LOG(INFO) << "Node1 address ... " << node1.address() << NL;
134 
135  // Node 1 is registered
136  assert(node1.address() == 1);
137 
138  // Node 2 is unregistered.
139  assert(node2.address() == HF::Protocol::BROADCAST_ADDR);
140 
141  LOG(INFO) << "Registering node2 ... " << NL;
142  node2.unit0()->device_management()->register_device();
143  LOG(INFO) << "Node2 address ... " << node2.address() << NL;
144 
145  // Node 2 is registered
146  assert(node2.address() == 2);
147 
148  LOG(INFO) << "There should be two registered devices ... "
149  << base.unit0()->device_management()->entries().size() << NL;
150 
151  assert(base.unit0()->device_management()->entries().size() == 2);
152 
153  LOG(INFO) << "Set initial level value ... 10\%" << NL;
154  level_controllable.level(10.0f);
155 
156  // =============================================================================
157  // Send change level message to a specific device/unit
158  // =============================================================================
159 
160  LOG(INFO) << "Send level change to a specific device/unit ..." << NL;
161 
162  HF::Protocol::Address addr(2, 1);
163 
164  LOG(INFO) << "Change level to 20\% ..." << NL;
165  level_control.level(addr, 20.0f);
166 
167  LOG(INFO) << "Level Controllable should have new level value : "
168  << (int) HF::Common::to_percent(level_controllable.level()) << "\%" << NL;
169  assert(level_controllable.level() == HF::Common::from_percent<uint8_t>(20.0));
170 
171  // =============================================================================
172  // Send change level message to the broadcast device/unit
173  // =============================================================================
174 
175  LOG(INFO) << "Send level change to the broadcast device/unit ..." << NL;
176 
177  // Create a bind entry.
178  LOG(INFO) << "Create bind entry on the base ... " << NL;
179  HF::Protocol::Address source(1, 1);
180  HF::Protocol::Address destination(2, 1);
182 
183  base.unit0()->bind_management()->add(source, destination, itf);
184  LOG(INFO) << "There should be one bind entry ... "
185  << base.unit0()->bind_management()->entries().size() << NL;
186  assert(base.unit0()->bind_management()->entries().size() == 1);
187 
188  LOG(INFO) << "Change level to 50\% ..." << NL;
189  level_control.level(50.0f);
190 
191  LOG(INFO) << "Level Controllable should have new level value : "
192  << (int) HF::Common::to_percent(level_controllable.level()) << "\%" << NL;
193  assert(level_controllable.level() == HF::Common::from_percent<uint8_t>(50));
194 
195  return 0;
196 }
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
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
P to_percent(T value)
Convert a value in the [0,std::numeric_limits<T>::max()] range into a percentage. ...
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
Level Control interface UID.
Definition: interface.h:80
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