Mbed OS Reference
Loading...
Searching...
No Matches
L3IPInterface.h
1/*
2 * Copyright (c) 2018 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef L3IP_INTERFACE_H
19#define L3IP_INTERFACE_H
20
21#include "nsapi.h"
22#include "L3IP.h"
23#include "OnboardNetworkStack.h"
24#include "SocketAddress.h"
25
26/** \addtogroup netinterface */
27/** @{*/
28
29/** L3IPInterface class
30 * Implementation of the NetworkInterface for an IP-based driver
31 *
32 * This class provides the necessary glue logic to create a NetworkInterface
33 * based on an L3IP and an OnboardNetworkStack. Cellular Interface drivers derive from it.
34 *
35 * Drivers derived from L3IPInterface should be constructed so that their
36 * L3IP is functional without the need to call `connect()`.
37 */
38class L3IPInterface : public virtual NetworkInterface {
39public:
40 /** Create an L3IP-based network interface.
41 *
42 * The default arguments obtain the default L3IP, which will be target-
43 * dependent (and the target may have some JSON option to choose which
44 * is the default, if there are multiple). The default stack is configured
45 * by JSON option nsapi.default-stack.
46 *
47 * Due to inability to return errors from the constructor, no real
48 * work is done until the first call to connect().
49 *
50 * @param l3ip Reference to L3IP to use
51 * @param stack Reference to onboard-network stack to use
52 */
55 ~L3IPInterface() override;
56 /** Set a static IP address
57 *
58 * Configures this network interface to use a static IP address.
59 * Implicitly disables DHCP, which can be enabled in set_dhcp.
60 * Requires that the network is disconnected.
61 *
62 * @param ip_address SocketAddress representation of the local IP address
63 * @param netmask SocketAddress representation of the local network mask
64 * @param gateway SocketAddress representation of the local gateway
65 * @return 0 on success, negative error code on failure
66 */
67 nsapi_error_t set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway) override;
68
69 /** Enable or disable DHCP on the network
70 *
71 * Requires that the network is disconnected
72 *
73 * @param dhcp False to disable dhcp (defaults to enabled)
74 * @return 0 on success, negative error code on failure
75 */
76 nsapi_error_t set_dhcp(bool dhcp) override;
77
78 /** Start the interface
79 * @return 0 on success, negative on failure
80 */
82
83 /** Stop the interface
84 * @return 0 on success, negative on failure
85 */
87
88 /** @copydoc NetworkInterface::get_ip_address */
90
91 /** @copydoc NetworkInterface::get_netmask */
93
94 /** @copydoc NetworkInterface::get_gateway */
96
97 /** Get the network interface name
98 *
99 * @return Null-terminated representation of the network interface name
100 * or null if interface not exists
101 */
102 char *get_interface_name(char *interface_name) override;
103
104 /** Set the network interface as default one
105 */
106 void set_as_default() override;
107
108 /** Register callback for status reporting
109 *
110 * @param status_cb The callback for status changes
111 */
112 void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) override;
113
114 /** Get the connection status
115 *
116 * @return The connection status according to nsapi_connection_status_t
117 */
118 nsapi_connection_status_t get_connection_status() const override;
119
120 /** Set blocking status of connect() which by default should be blocking
121 *
122 * @param blocking true if connect is blocking
123 * @return 0 on success, negative error code on failure
124 */
125 nsapi_error_t set_blocking(bool blocking) override;
126
127 /** Provide access to the L3IP
128 *
129 * This should be used with care - normally the network stack would
130 * control the L3IP, so manipulating the L3IP while the stack
131 * is also using it (ie after connect) will likely cause problems.
132 *
133 * @return Reference to the L3IP in use
134 */
135 L3IP &getl3ip() const
136 {
137 return _l3ip;
138 }
139
140#if 0
141 /* NetworkInterface does not currently have l3ipInterface, so this
142 * "dynamic cast" is non-functional.
143 */
144 L3IPInterface *l3ipInterface() final
145 {
146 return this;
147 }
148#endif
149
150protected:
151 /** Provide access to the underlying stack
152 *
153 * @return The underlying network stack
154 */
156
157 L3IP &_l3ip;
158 OnboardNetworkStack &_stack;
159 OnboardNetworkStack::Interface *_interface = nullptr;
160 bool _dhcp = true;
161 bool _blocking = true;
162 SocketAddress _ip_address;
163 SocketAddress _netmask;
164 SocketAddress _gateway;
165 mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
166};
167
168/** @}*/
169
170#endif
SocketAddress class.
This interface should be used to abstract low level access to networking hardware All operations rece...
Definition: L3IP.h:31
static L3IP & get_default_instance()
Return the default on-board L3IP.
L3IPInterface class Implementation of the NetworkInterface for an IP-based driver.
Definition: L3IPInterface.h:38
nsapi_error_t disconnect() override
Stop the interface.
char * get_interface_name(char *interface_name) override
Get the network interface name.
nsapi_connection_status_t get_connection_status() const override
Get the connection status.
nsapi_error_t connect() override
Start the interface.
void set_as_default() override
Set the network interface as default one.
nsapi_error_t get_gateway(SocketAddress *address) override
Get the local gateway.
nsapi_error_t get_ip_address(SocketAddress *address) override
Get the local IP address.
L3IP & getl3ip() const
Provide access to the L3IP.
nsapi_error_t set_dhcp(bool dhcp) override
Enable or disable DHCP on the network.
void attach(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb) override
Register callback for status reporting.
nsapi_error_t set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway) override
Set a static IP address.
L3IPInterface(L3IP &l3ip=L3IP::get_default_instance(), OnboardNetworkStack &stack=OnboardNetworkStack::get_default_instance())
Create an L3IP-based network interface.
nsapi_error_t set_blocking(bool blocking) override
Set blocking status of connect() which by default should be blocking.
NetworkStack * get_stack() override
Provide access to the underlying stack.
nsapi_error_t get_netmask(SocketAddress *address) override
Get the local network mask.
Common interface that is shared between network devices.
NetworkStack class.
Definition: NetworkStack.h:42
Representation of a stack's view of an interface.
mbed OS API for onboard IP stack abstraction
static OnboardNetworkStack & get_default_instance()
Return the default on-board network stack.
SocketAddress class.
Definition: SocketAddress.h:37
Callback class based on template specialization.
Definition: Callback.h:53
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:142