Mistake on this page?
Report an issue in GitHub or email us
MeshInterfaceNanostack.h
1 /*
2  * Copyright (c) 2016-2019 ARM Limited. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  * Licensed under the Apache License, Version 2.0 (the License); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MESHINTERFACENANOSTACK_H
18 #define MESHINTERFACENANOSTACK_H
19 
20 #include "Semaphore.h"
21 #include "MeshInterface.h"
22 #include "NanostackRfPhy.h"
23 #include "Nanostack.h"
24 #include "mesh_interface_types.h"
25 
26 class Nanostack::Interface : public OnboardNetworkStack::Interface, private mbed::NonCopyable<Nanostack::Interface> {
27 public:
29  char *get_mac_address(char *buf, nsapi_size_t buflen) final;
30  nsapi_error_t get_netmask(SocketAddress *address) final;
31  nsapi_error_t get_gateway(SocketAddress *address) override;
32  void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) final;
33  nsapi_connection_status_t get_connection_status() const final;
34 
35  void get_mac_address(uint8_t *buf) const
36  {
37  NanostackMACPhy *phy = interface_phy.nanostack_mac_phy();
38  if (phy) {
39  phy->get_mac_address(buf);
40  }
41  }
42 
43  /**
44  * \brief Callback from C-layer
45  * \param status state of the network
46  * */
47  void network_handler(mesh_connection_status_t status);
48 
49  int8_t get_interface_id() const
50  {
51  return interface_id;
52  }
53  int8_t get_driver_id() const
54  {
55  return _device_id;
56  }
57 
58 private:
59  NanostackPhy &interface_phy;
60 protected:
61  Interface(NanostackPhy &phy);
62  nsapi_error_t register_phy();
63  NanostackPhy &get_phy() const
64  {
65  return interface_phy;
66  }
67  int8_t interface_id = -1;
68  int8_t _device_id = -1;
69  rtos::Semaphore connect_semaphore;
70  rtos::Semaphore disconnect_semaphore;
71 
73  nsapi_connection_status_t _connect_status = NSAPI_STATUS_DISCONNECTED;
74  nsapi_connection_status_t _previous_connection_status = NSAPI_STATUS_DISCONNECTED;
75  bool _blocking = true;
76 };
77 
79 public:
80  char *get_interface_name(char *buf);
81 protected:
82  MeshInterface(NanostackRfPhy &phy) : Interface(phy) { }
83  NanostackRfPhy &get_phy() const
84  {
85  return static_cast<NanostackRfPhy &>(Interface::get_phy());
86  }
87 };
88 
89 
90 class InterfaceNanostack : public virtual NetworkInterface {
91 public:
92  /** Start the interface
93  *
94  * @return 0 on success, negative error code on failure
95  */
96  nsapi_error_t connect() override;
97 
98  /** Stop the interface
99  *
100  * @return 0 on success, negative error code on failure
101  */
102  nsapi_error_t disconnect() override;
103 
104  /** @copydoc NetworkInterface::get_ip_address */
105  nsapi_error_t get_ip_address(SocketAddress *address) override;
106 
107  /** Get the internally stored MAC address
108  /return MAC address of the interface
109  */
110  const char *get_mac_address() override;
111 
112  /** Register callback for status reporting
113  *
114  * The specified status callback function will be called on status changes
115  * on the network. The parameters on the callback are the event type and
116  * event-type dependent reason parameter.
117  *
118  * @param status_cb The callback for status changes
119  */
120  void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) override;
121 
122  /** Get the connection status
123  *
124  * @return The connection status according to ConnectionStatusType
125  */
126  nsapi_connection_status_t get_connection_status() const override;
127 
128  /** Set blocking status of connect() which by default should be blocking
129  *
130  * @param blocking true if connect is blocking
131  * @return 0 on success, negative error code on failure
132  */
133  nsapi_error_t set_blocking(bool blocking) override;
134 
135  /** Set file system root path.
136  *
137  * Set file system root path that stack will use to write and read its data.
138  * Setting root_path to NULL will disable file system usage.
139  *
140  * @param root_path Address to NUL-terminated root-path string or NULL to disable file system usage.
141  * @return MESH_ERROR_NONE on success, MESH_ERROR_MEMORY in case of memory failure, MESH_ERROR_UNKNOWN in case of other error.
142  */
143  nsapi_error_t set_file_system_root_path(const char *root_path);
144 
145  /** Get the interface ID
146  * @return Interface identifier
147  */
148  int8_t get_interface_id() const
149  {
150  return _interface->get_interface_id();
151  }
152 
153 protected:
154  InterfaceNanostack() = default;
155  Nanostack *get_stack(void) override;
156  Nanostack::Interface *get_interface() const
157  {
158  return _interface;
159  }
160  virtual nsapi_error_t do_initialize() = 0;
161 
162  Nanostack::Interface *_interface = nullptr;
163 
164  SocketAddress ip_addr;
165  char mac_addr_str[24] {};
166  mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
167  bool _blocking = true;
168  bool _configured = false;
169 };
170 
171 class MeshInterfaceNanostack : public InterfaceNanostack, public MeshInterface, private mbed::NonCopyable<MeshInterfaceNanostack> {
172 public:
173 
174  /** Attach phy and initialize the mesh
175  *
176  * Initializes a mesh interface on the given phy. Not needed if
177  * the phy is passed to the mesh's constructor.
178  *
179  * @return 0 on success, negative on failure
180  */
181  nsapi_error_t initialize(NanostackRfPhy *phy);
182 
183 protected:
184  MeshInterfaceNanostack() = default;
185  MeshInterfaceNanostack(NanostackRfPhy *phy) : _phy(phy) { }
186  Nanostack::MeshInterface *get_interface() const
187  {
188  return static_cast<Nanostack::MeshInterface *>(_interface);
189  }
190  NanostackRfPhy *_phy = nullptr;
191 };
192 
193 #endif /* MESHINTERFACENANOSTACK_H */
Radio PHY driver class for Nanostack.
virtual char * get_interface_name(char *buf)
Returns interface name.
The Semaphore class is used to manage and protect access to a set of shared resources.
Definition: Semaphore.h:50
Representation of a stack&#39;s view of an interface.
char * get_mac_address(char *buf, nsapi_size_t buflen) final
Return MAC address of the network interface.
virtual void get_mac_address(uint8_t *mac)=0
Read the mac address of this physical interface.
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:140
nsapi_connection_status_t get_connection_status() const final
Get the connection status.
Common interface that is shared between mesh hardware.
Definition: MeshInterface.h:30
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:162
SocketAddress class.
Definition: SocketAddress.h:37
Common interface that is shared between network devices.
PHY driver class for Nanostack.
Definition: NanostackPhy.h:25
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:144
MAC PHY driver class for Nanostack.
void attach(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb) final
Register callback for status reporting.
void network_handler(mesh_connection_status_t status)
Callback from C-layer.
virtual NanostackMACPhy * nanostack_mac_phy()
Return pointer to a NanostackMACPhy.
Definition: NanostackPhy.h:40
nsapi_error_t get_netmask(SocketAddress *address) final
nsapi_error_t get_gateway(SocketAddress *address) override
nsapi_error_t get_ip_address(SocketAddress *address) final
Get the local IP address.
Callback class based on template specialization.
Definition: Callback.h:53
int8_t get_interface_id() const
Get the interface ID.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.