HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
example_06.cpp
Go to the documentation of this file.
1 // =============================================================================
16 // =============================================================================
17 
18 #include "hanfun.h"
19 #include "hanfun/debug.h"
20 
21 #include "localloop.h"
22 
23 // =============================================================================
24 // Implementation
25 // =============================================================================
26 
27 namespace
28 {
29  // =============================================================================
30  // Node
31  // =============================================================================
32 
33  /*
34  * Custom device information service.
35  */
36  struct DeviceInformation: public HF::Core::DeviceInformation::Server
37  {
38  std::string serial;
39 
40  DeviceInformation(HF::Core::Unit0 &unit):
41  Server(unit), serial("0123456789")
42  {}
43 
44  HF::Attributes::IAttribute *attribute(uint8_t uid)
45  {
47  {
48  LOG(INFO) << "On device " << unit().device().address()
49  << " serial number attribute read." << NL;
50 
52  this->uid(),
54  this, serial, true);
55  }
56  else
57  {
58  return HF::Core::DeviceInformation::Server::attribute(uid);
59  }
60  }
61  };
62 
63  /*
64  * Custom Unit 0 for node devices.
65  */
66  typedef HF::Devices::Node::Unit0<DeviceInformation,
69 #if HF_TIME_SUPPORT
71 #endif
72 #if HF_BATCH_PROGRAM_SUPPORT
74 #endif
75 #if HF_EVENT_SCHEDULING_SUPPORT
77 #endif
78 #if HF_WEEKLY_SCHEDULING_SUPPORT
80 #endif
81 #if HF_GROUP_SUPPORT
82  , HF::Core::GroupTable::DefaultServer
83 #endif
84  > NodeUnit0;
85 
89  struct Node: public HF::Devices::Node::Abstract<NodeUnit0>
90  {};
91 
92  // =============================================================================
93  // Base
94  // =============================================================================
95 
96  struct BaseUnit0: public HF::Devices::Concentrator::DefaultUnit0
97  {
98  BaseUnit0(HF::IDevice &device):
99  HF::Devices::Concentrator::DefaultUnit0(device)
100  {}
101 
103  uint16_t offset)
104  {
105  auto res = HF::Devices::Concentrator::DefaultUnit0::handle(packet, payload, offset);
106 
108  {
110  {
112  {
116 
117  HF::Attributes::Response resp(attr);
118  resp.unpack(payload, offset);
119 
120  LOG(INFO) << "Got serial number " << attr->get() << " from "
121  << packet.source.device << NL;
122  }
123  }
124  }
125 
126  return res;
127  }
128  };
129 
133  struct Base: public HF::Devices::Concentrator::Abstract<BaseUnit0>
134  {};
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 : Device Information service." << 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  // =============================================================================
208  // Device Information
209  // =============================================================================
210 
212 
213  LOG(INFO) << "Read serial number attribute from node1 ... " << NL;
214  node1.unit0()->device_info()->serial = "111";
215  HF::Protocol::Address addr(1, 0);
216  base.unit0()->send(addr, *message, nullptr);
217 
218  LOG(INFO) << "Read serial number attribute from node2 ... " << NL;
219 
220  node2.unit0()->device_info()->serial = "222";
221  addr.device = 2;
222  base.unit0()->send(addr, *message, nullptr);
223 
224  delete message;
225 
226  return 0;
227 }
This class represents a HAN-FUN Concentrator.
Definition: base.h:302
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.
Message message
Packet message payload;.
Definition: protocol.h:306
Units::IUnit * unit(uint8_t id) const
Return pointer to the unit with the given id.
Server< Entries< Interval > > DefaultServer
Event Scheduling Service : Server side with default persistence implementation.
constexpr uint16_t BROADCAST_ADDR
HAN-FUN Broadcast - device address.
Definition: protocol.h:45
uint16_t id
Identifier of the interface.
Device Management interface : Client side.
Attribute Reporting - Server Role.
Template to create Unit 0 for HAN-FUN node devices.
Definition: devices.h:230
Type type
Message type.
Definition: protocol.h:127
This class represents the interface common to all HAN-FUN devices.
Definition: device.h:99
Unit0< Core::DeviceInformation::Server, Core::DeviceManagement::Client, Core::AttributeReporting::Server > DefaultUnit0
Unit0 using default classes to provide the core services for node devices.
Definition: devices.h:419
Common::Result handle(HF::Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
Definition: core.h:532
virtual uint16_t address() const =0
Return the device address on the HAN-FUN network, when the device is registered, or HF_BROADCAST_ADDR...
This class represents the response sent when a Protocol::Message::GET_ATTR_REQ request.
Definition: attributes.h:772
Template for HAN-FUN concentrator devices.
Definition: devices.h:906
This class represents a byte array.
#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
virtual IDevice & device() const =0
Reference to the device this unit belongs to.
Protocol::Message * get(HF::Attributes::UIDS &uids)
Create a message that can be used to retrieve the attributes with the given uids of the device inform...
Network Address.
Definition: protocol.h:201
uint8_t member
Interface destination member.
Definition: protocol.h:99
Address source
Source Address.
Definition: protocol.h:300
Get attributes response.
Definition: protocol.h:72
HAN-FUN Protocol Packet.
Definition: protocol.h:298
Interface itf
Interface Address.
Definition: protocol.h:129
Time Service : Server side implementation.
Definition: time.h:220
Interface/Service Attribute API.
Definition: attributes.h:44
This file contains an implementation of a HAN-FUN transport layer to be used in the example applicati...
uint16_t device
Device Address.
Definition: protocol.h:204
#define UNUSED(x)
Helper macro to remove warning about unused function/method argument.
Helper template class to declare an attribute with the given T type.
Definition: attributes.h:349
Device Information interface : Server side.
Device information interface UID.
Definition: interface.h:64
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81
Result
Commands result codes.
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