Mbed OS Reference
Loading...
Searching...
No Matches
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
26class Nanostack::Interface : public OnboardNetworkStack::Interface, private mbed::NonCopyable<Nanostack::Interface> {
27public:
29 char *get_mac_address(char *buf, nsapi_size_t buflen) override;
30 nsapi_error_t set_mac_address(uint8_t *buf, nsapi_size_t buflen);
33 void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) final;
34 nsapi_connection_status_t get_connection_status() const final;
35 virtual void get_mac_address(uint8_t *buf) const;
36
37 /**
38 * \brief Callback from C-layer
39 * \param status state of the network
40 * */
41 void network_handler(mesh_connection_status_t status);
42
43 int8_t get_interface_id() const
44 {
45 return interface_id;
46 }
47 int8_t get_driver_id() const
48 {
49 return _device_id;
50 }
51
52private:
53 NanostackPhy &interface_phy;
54protected:
55 Interface(NanostackPhy &phy);
56 nsapi_error_t register_phy();
57 NanostackPhy &get_phy() const
58 {
59 return interface_phy;
60 }
61 int8_t interface_id = -1;
62 int8_t _device_id = -1;
63 rtos::Semaphore connect_semaphore;
64 rtos::Semaphore disconnect_semaphore;
65
66 mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
67 nsapi_connection_status_t _connect_status = NSAPI_STATUS_DISCONNECTED;
68 nsapi_connection_status_t _previous_connection_status = NSAPI_STATUS_DISCONNECTED;
69 bool _blocking = true;
70};
71
73public:
74 char *get_interface_name(char *buf);
75protected:
77 NanostackRfPhy &get_phy() const
78 {
79 return static_cast<NanostackRfPhy &>(Interface::get_phy());
80 }
81};
82
83
84class InterfaceNanostack : public virtual NetworkInterface {
85public:
86 /** Start the interface
87 *
88 * @return 0 on success, negative error code on failure
89 */
91
92 /** Stop the interface
93 *
94 * @return 0 on success, negative error code on failure
95 */
97
98 /** @copydoc NetworkInterface::get_ip_address */
100
101 /** Get the internally stored MAC address
102 /return MAC address of the interface
103 */
104 const char *get_mac_address() override;
105
106 /** @copydoc NetworkInterface::set_mac_address */
107 nsapi_error_t set_mac_address(uint8_t *mac_addr, nsapi_size_t addr_len) override;
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 void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) override;
118
119 /** Get the connection status
120 *
121 * @return The connection status according to ConnectionStatusType
122 */
123 nsapi_connection_status_t get_connection_status() const override;
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 nsapi_error_t set_blocking(bool blocking) override;
131
132 /** Set file system root path.
133 *
134 * Set file system root path that stack will use to write and read its data.
135 * Setting root_path to NULL will disable file system usage.
136 *
137 * @param root_path Address to NUL-terminated root-path string or NULL to disable file system usage.
138 * @return MESH_ERROR_NONE on success, MESH_ERROR_MEMORY in case of memory failure, MESH_ERROR_UNKNOWN in case of other error.
139 */
141
142 /** Get the interface ID
143 * @return Interface identifier
144 */
145 int8_t get_interface_id() const
146 {
147 return _interface->get_interface_id();
148 }
149
150protected:
151 InterfaceNanostack() = default;
152 Nanostack *get_stack(void) override;
153 Nanostack::Interface *get_interface() const
154 {
155 return _interface;
156 }
157 virtual nsapi_error_t do_initialize() = 0;
158
159 Nanostack::Interface *_interface = nullptr;
160
161 SocketAddress ip_addr;
162 char mac_addr_str[24] {};
163 mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
164 bool _blocking = true;
165 bool _configured = false;
166};
167
168class MeshInterfaceNanostack : public InterfaceNanostack, public MeshInterface, private mbed::NonCopyable<MeshInterfaceNanostack> {
169public:
170
171 /** Attach phy and initialize the mesh
172 *
173 * Initializes a mesh interface on the given phy. Not needed if
174 * the phy is passed to the mesh's constructor.
175 *
176 * @return 0 on success, negative on failure
177 */
179
180protected:
183 Nanostack::MeshInterface *get_interface() const
184 {
185 return static_cast<Nanostack::MeshInterface *>(_interface);
186 }
187 NanostackRfPhy *_phy = nullptr;
188};
189
190#endif /* MESHINTERFACENANOSTACK_H */
nsapi_error_t disconnect() override
Stop the interface.
nsapi_error_t set_mac_address(uint8_t *mac_addr, nsapi_size_t addr_len) override
Set the MAC address to the interface.
const char * get_mac_address() override
Get the internally stored MAC address /return MAC address of the interface.
nsapi_connection_status_t get_connection_status() const override
Get the connection status.
nsapi_error_t connect() override
Start the interface.
nsapi_error_t get_ip_address(SocketAddress *address) override
Get the local IP address.
int8_t get_interface_id() const
Get the interface ID.
void attach(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb) override
Register callback for status reporting.
nsapi_error_t set_blocking(bool blocking) override
Set blocking status of connect() which by default should be blocking.
nsapi_error_t set_file_system_root_path(const char *root_path)
Set file system root path.
Common interface that is shared between mesh hardware.
Definition: MeshInterface.h:30
nsapi_error_t initialize(NanostackRfPhy *phy)
Attach phy and initialize the mesh.
void network_handler(mesh_connection_status_t status)
Callback from C-layer.
nsapi_error_t get_gateway(SocketAddress *address) override
Writes the gateway address of the network interface into a user-supplied SocketAddress.
nsapi_error_t get_ip_address(SocketAddress *address) final
Get the local IP address.
char * get_mac_address(char *buf, nsapi_size_t buflen) override
Return MAC address of the network interface.
nsapi_connection_status_t get_connection_status() const final
Get the connection status.
void attach(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb) final
Register callback for status reporting.
nsapi_error_t get_netmask(SocketAddress *address) final
Writes the netmask of the network interface into a user-supplied SocketAddress.
char * get_interface_name(char *buf)
Returns interface name.
PHY driver class for Nanostack.
Definition: NanostackPhy.h:25
Radio PHY driver class for Nanostack.
Common interface that is shared between network devices.
Representation of a stack's view of an interface.
SocketAddress class.
Definition: SocketAddress.h:37
Callback class based on template specialization.
Definition: Callback.h:53
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:162
The Semaphore class is used to manage and protect access to a set of shared resources.
Definition: Semaphore.h:50
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:142
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:146
@ NSAPI_STATUS_DISCONNECTED
Definition: nsapi_types.h:118