Mistake on this page?
Report an issue in GitHub or email us
MeshInterfaceNanostack.h
1 /*
2  * Copyright (c) 2016 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:
28  virtual char *get_ip_address(char *buf, nsapi_size_t buflen);
29  virtual char *get_mac_address(char *buf, nsapi_size_t buflen);
30  virtual char *get_netmask(char *buf, nsapi_size_t buflen);
31  virtual char *get_gateway(char *buf, nsapi_size_t buflen);
32  virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
33  virtual nsapi_connection_status_t get_connection_status() const;
34 
35  void get_mac_address(uint8_t *buf) const
36  {
37  interface_phy.get_mac_address(buf);
38  }
39 
40  /**
41  * \brief Callback from C-layer
42  * \param status state of the network
43  * */
44  void network_handler(mesh_connection_status_t status);
45 
46  int8_t get_interface_id() const
47  {
48  return interface_id;
49  }
50  int8_t get_driver_id() const
51  {
52  return _device_id;
53  }
54 
55 private:
56  NanostackPhy &interface_phy;
57 protected:
58  Interface(NanostackPhy &phy);
59  virtual nsapi_error_t register_phy();
60  NanostackPhy &get_phy() const
61  {
62  return interface_phy;
63  }
64  int8_t interface_id;
65  int8_t _device_id;
66  rtos::Semaphore connect_semaphore;
67  rtos::Semaphore disconnect_semaphore;
68 
70  nsapi_connection_status_t _connect_status;
71  nsapi_connection_status_t _previous_connection_status;
72  bool _blocking;
73 };
74 
76 protected:
77  MeshInterface(NanostackRfPhy &phy) : Interface(phy) { }
78  NanostackRfPhy &get_phy() const
79  {
80  return static_cast<NanostackRfPhy &>(Interface::get_phy());
81  }
82 };
83 
84 
85 class InterfaceNanostack : public virtual NetworkInterface {
86 public:
87  /** Start the interface
88  *
89  * @return 0 on success, negative error code on failure
90  */
91  virtual nsapi_error_t connect();
92 
93  /** Stop the interface
94  *
95  * @return 0 on success, negative error code on failure
96  */
97  virtual nsapi_error_t disconnect();
98 
99  /** Get the internally stored IP address
100  /return IP address of the interface or null if not yet connected
101  */
102  virtual const char *get_ip_address();
103 
104  /** Get the internally stored MAC address
105  /return MAC address of the interface
106  */
107  virtual const char *get_mac_address();
108 
109  /** Register callback for status reporting
110  *
111  * The specified status callback function will be called on status changes
112  * on the network. The parameters on the callback are the event type and
113  * event-type dependent reason parameter.
114  *
115  * @param status_cb The callback for status changes
116  */
117  virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
118 
119  /** Get the connection status
120  *
121  * @return The connection status according to ConnectionStatusType
122  */
123  virtual nsapi_connection_status_t get_connection_status() const;
124 
125  /** Set blocking status of connect() which by default should be blocking
126  *
127  * @param blocking true if connect is blocking
128  * @return 0 on success, negative error code on failure
129  */
130  virtual nsapi_error_t set_blocking(bool blocking);
131 
132  /** Get the interface ID
133  /return Interface identifier
134  */
135  int8_t get_interface_id() const
136  {
137  return _interface->get_interface_id();
138  }
139 
140 protected:
142  virtual Nanostack *get_stack(void);
143  Nanostack::Interface *get_interface() const
144  {
145  return _interface;
146  }
147  virtual nsapi_error_t do_initialize() = 0;
148 
149  Nanostack::Interface *_interface;
150 
151  char ip_addr_str[40];
152  char mac_addr_str[24];
153  mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
154  bool _blocking;
155 };
156 
157 class MeshInterfaceNanostack : public InterfaceNanostack, public MeshInterface, private mbed::NonCopyable<MeshInterfaceNanostack> {
158 public:
159 
160  /** Attach phy and initialize the mesh
161  *
162  * Initializes a mesh interface on the given phy. Not needed if
163  * the phy is passed to the mesh's constructor.
164  *
165  * @return 0 on success, negative on failure
166  */
167  nsapi_error_t initialize(NanostackRfPhy *phy);
168 
169 protected:
170  MeshInterfaceNanostack() : _phy(NULL) { }
171  MeshInterfaceNanostack(NanostackRfPhy *phy) : _phy(phy) { }
172  Nanostack::MeshInterface *get_interface() const
173  {
174  return static_cast<Nanostack::MeshInterface *>(_interface);
175  }
176  NanostackRfPhy *_phy;
177 };
178 
179 #endif /* MESHINTERFACENANOSTACK_H */
Radio PHY driver class for Nanostack.
The Semaphore class is used to manage and protect access to a set of shared resources.
Definition: Semaphore.h:46
Representation of a stack&#39;s view of an interface.
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:95
virtual void attach(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb)
Register callback for status reporting.
Common interface that is shared between mesh hardware.
Definition: MeshInterface.h:29
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:168
virtual char * get_ip_address(char *buf, nsapi_size_t buflen)
Copies IP address of the network interface to user supplied buffer.
virtual char * get_mac_address(char *buf, nsapi_size_t buflen)
Return MAC address of the network interface.
Common interface that is shared between network devices.
virtual char * get_netmask(char *buf, nsapi_size_t buflen)
Copies netmask of the network interface to user supplied buffer.
PHY driver class for Nanostack.
Definition: NanostackPhy.h:22
virtual char * get_gateway(char *buf, nsapi_size_t buflen)
Copies gateway address of the network interface to user supplied buffer.
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:99
void network_handler(mesh_connection_status_t status)
Callback from C-layer.
Callback class based on template specialization.
Definition: Callback.h:39
virtual nsapi_connection_status_t get_connection_status() const
Get the connection status.
virtual void get_mac_address(uint8_t *mac)=0
Read the mac address of this physical interface.
int8_t get_interface_id() const
Get the interface ID /return Interface identifier.
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.