HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
example_02.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  // Node
30  // =============================================================================
32 
33  /*
34  * Custom Device Management service for the node.
35  */
36  struct DeviceManagementClient: public HF::Core::DeviceManagement::Client
37  {
38  DeviceManagementClient(HF::Core::Unit0 &unit):
39  HF::Core::DeviceManagement::Client(unit)
40  {}
41 
43  {
45 
46  LOG(INFO) << ">>> Registration response <<<" << NL;
47  LOG(INFO) << " Code : " << (int) response.code << NL;
48  LOG(INFO) << "Address : " << response.address << NL;
49  LOG(INFO) << " EMC : " << response.emc << NL;
50  }
51  };
52 
53  /*
54  * Custom Unit 0 for node with the custom Device Management service.
55  */
57  DeviceManagementClient,
59 #if HF_TIME_SUPPORT
61 #endif
62 #if HF_BATCH_PROGRAM_SUPPORT
64 #endif
65 #if HF_EVENT_SCHEDULING_SUPPORT
67 #endif
68 #if HF_WEEKLY_SCHEDULING_SUPPORT
70 #endif
71 #if HF_GROUP_SUPPORT
72  , HF::Core::GroupTable::DefaultServer
73 #endif
74  > NodeUnit0;
75 
76  /*
77  * Example node.
78  */
79  struct Node: public HF::Devices::Node::Abstract<NodeUnit0>
80  {};
81 
83 
84  // =============================================================================
85  // Concentrator
86  // =============================================================================
88 
89  /*
90  * Custom Device Management service for the concentrator.
91  */
92  struct DeviceManagementServer: public HF::Core::DeviceManagement::DefaultServer
93  {
94  DeviceManagementServer(HF::Core::Unit0 &unit):
95  HF::Core::DeviceManagement::DefaultServer(unit)
96  {}
97 
99  {
100  LOG(INFO) << "Device " << device->uid << " registered !" << NL;
101  }
102  };
103 
104  /*
105  * Custom Unit 0 for concentrator with the custom Device Management service.
106  */
108  DeviceManagementServer,
110 #if HF_TIME_SUPPORT
112 #endif
113 #if HF_BATCH_PROGRAM_SUPPORT
115 #endif
116 #if HF_EVENT_SCHEDULING_SUPPORT
118 #endif
119 #if HF_WEEKLY_SCHEDULING_SUPPORT
121 #endif
122 #if HF_GROUP_SUPPORT
123  HF::Core::GroupTable::DefaultServer,
125 #endif
127 
128  /*
129  * Example concentrator.
130  */
131  struct Base: public HF::Devices::Concentrator::Abstract<BaseUnit0>
132  {};
133 
135 
136 } // namespace
137 
138 // =============================================================================
139 // MAIN
140 // =============================================================================
141 
142 int main(int argc, char **argv)
143 {
144  UNUSED(argc);
145  UNUSED(argv);
146 
147  LOG(INFO) << "Use case : Handling registration events" << NL;
148 
149  /*
150  * Each node variable is a remote device, i.e.,
151  * the Node variables are declared on the remote device
152  * code and are not used on the base code.
153  */
154  LOG(INFO) << "Create the node instances ..." << NL;
155 
156  Node node1;
157  Node node2;
158 
159  /*
160  * This instance represents the base application.
161  */
162  LOG(INFO) << "Create the base instance ..." << NL;
163  Base base;
164 
165  LOG(INFO) << "Create transport instance" << NL;
166  Localloop loop;
167 
168  /*
169  * Setup the network.
170  *
171  * This simulates the devices connecting to the base using for
172  * example a TCP/IP connection or a DECT ULE PVC.
173  */
174  LOG(INFO) << "Network setup ..." << NL;
175 
176  loop.set_base(&base);
177  loop.add_node(&node1, "node_1");
178  loop.add_node(&node2, "node_2");
179 
180  // Register two devices.
181 
182  // Node 1 is unregistered.
183  assert(node1.address() == HF::Protocol::BROADCAST_ADDR);
184 
185  LOG(INFO) << "Registering node1 ... " << NL;
186  node1.unit0()->device_management()->register_device();
187  LOG(INFO) << "Node1 address ... " << node1.address() << NL;
188 
189  // Node 1 is registered
190  assert(node1.address() == 1);
191 
192  // Node 2 is unregistered.
193  assert(node2.address() == HF::Protocol::BROADCAST_ADDR);
194 
195  LOG(INFO) << "Registering node2 ... " << NL;
196  node2.unit0()->device_management()->register_device();
197  LOG(INFO) << "Node2 address ... " << node2.address() << NL;
198 
199  // Node 2 is registered
200  assert(node2.address() == 2);
201 
202  LOG(INFO) << "There should be two registered devices ... "
203  << base.unit0()->device_management()->entries().size() << NL;
204 
205  assert(base.unit0()->device_management()->entries().size() == 2);
206 
207  return 0;
208 }
This class represents a HAN-FUN Concentrator.
Definition: base.h:302
bool response(Message::Type type)
Check if message is a response.
This represent the special unit with ID/UID = 0.
Definition: core.h:67
This is the top level include file for the HAN-FUN library.
This file contains the prototypes of the debug functionality in HAN-FUN.
Server< Entries< Day > > DefaultServer
Weekly Scheduling Service : Server side with default persistence implementation.
Server< Entries< Interval > > DefaultServer
Event Scheduling Service : Server side with default persistence implementation.
virtual void registered(DevicePtr &device)
Indicate that a device has been registered.
constexpr uint16_t BROADCAST_ADDR
HAN-FUN Broadcast - device address.
Definition: protocol.h:45
Device Management interface : Client side.
Attribute Reporting - Server Role.
Template to create Unit 0 for HAN-FUN node devices.
Definition: devices.h:230
Helper class used to implement custom functionality to the device management server side...
virtual void registered(RegisterResponse &response)
This method is called when a response to a registration message is received.
Helper class used to implement custom functionality to the bind management server side...
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
Template to create Unit0 for HAN-FUN concentrator devices.
Definition: devices.h:668
Time Service : Server side implementation.
Definition: time.h:220
This file contains an implementation of a HAN-FUN transport layer to be used in the example applicati...
Simple raw pointer wrapper.
#define UNUSED(x)
Helper macro to remove warning about unused function/method argument.
Device Information interface : Server side.
Helper class used to implement custom functionality to the group management server side...
#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
Scheduling Service : Server side implementation.
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22