Mistake on this page?
Report an issue in GitHub or email us
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 /** L3IPInterface class
27  * Implementation of the NetworkInterface for an IP-based driver
28  *
29  * This class provides the necessary glue logic to create a NetworkInterface
30  * based on an L3IP and an OnboardNetworkStack. Cellular Interface drivers derive from it.
31  *
32  * Drivers derived from L3IPInterface should be constructed so that their
33  * L3IP is functional without the need to call `connect()`.
34  */
35 class L3IPInterface : public virtual NetworkInterface {
36 public:
37  /** Create an L3IP-based network interface.
38  *
39  * The default arguments obtain the default L3IP, which will be target-
40  * dependent (and the target may have some JSON option to choose which
41  * is the default, if there are multiple). The default stack is configured
42  * by JSON option nsapi.default-stack.
43  *
44  * Due to inability to return errors from the constructor, no real
45  * work is done until the first call to connect().
46  *
47  * @param l3ip Reference to L3IP to use
48  * @param stack Reference to onboard-network stack to use
49  */
52  ~L3IPInterface() override;
53  /** Set a static IP address
54  *
55  * Configures this network interface to use a static IP address.
56  * Implicitly disables DHCP, which can be enabled in set_dhcp.
57  * Requires that the network is disconnected.
58  *
59  * @param ip_address SocketAddress representation of the local IP address
60  * @param netmask SocketAddress representation of the local network mask
61  * @param gateway SocketAddress representation of the local gateway
62  * @return 0 on success, negative error code on failure
63  */
64  nsapi_error_t set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway) override;
65 
66  /** Enable or disable DHCP on the network
67  *
68  * Requires that the network is disconnected
69  *
70  * @param dhcp False to disable dhcp (defaults to enabled)
71  * @return 0 on success, negative error code on failure
72  */
73  nsapi_error_t set_dhcp(bool dhcp) override;
74 
75  /** Start the interface
76  * @return 0 on success, negative on failure
77  */
78  nsapi_error_t connect() override;
79 
80  /** Stop the interface
81  * @return 0 on success, negative on failure
82  */
83  nsapi_error_t disconnect() override;
84 
85  /** @copydoc NetworkInterface::get_ip_address */
86  nsapi_error_t get_ip_address(SocketAddress *address) override;
87 
88  /** @copydoc NetworkInterface::get_netmask */
89  nsapi_error_t get_netmask(SocketAddress *address) override;
90 
91  /** @copydoc NetworkInterface::get_gateway */
92  nsapi_error_t get_gateway(SocketAddress *address) override;
93 
94  /** Get the network interface name
95  *
96  * @return Null-terminated representation of the network interface name
97  * or null if interface not exists
98  */
99  char *get_interface_name(char *interface_name) override;
100 
101  /** Set the network interface as default one
102  */
103  void set_as_default() override;
104 
105  /** Register callback for status reporting
106  *
107  * @param status_cb The callback for status changes
108  */
109  void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) override;
110 
111  /** Get the connection status
112  *
113  * @return The connection status according to nsapi_connection_status_t
114  */
115  nsapi_connection_status_t get_connection_status() const override;
116 
117  /** Set blocking status of connect() which by default should be blocking
118  *
119  * @param blocking true if connect is blocking
120  * @return 0 on success, negative error code on failure
121  */
122  nsapi_error_t set_blocking(bool blocking) override;
123 
124  /** Provide access to the L3IP
125  *
126  * This should be used with care - normally the network stack would
127  * control the L3IP, so manipulating the L3IP while the stack
128  * is also using it (ie after connect) will likely cause problems.
129  *
130  * @return Reference to the L3IP in use
131  */
132  L3IP &getl3ip() const
133  {
134  return _l3ip;
135  }
136 
137 #if 0
138  /* NetworkInterface does not currently have l3ipInterface, so this
139  * "dynamic cast" is non-functional.
140  */
141  L3IPInterface *l3ipInterface() final
142  {
143  return this;
144  }
145 #endif
146 
147 protected:
148  /** Provide access to the underlying stack
149  *
150  * @return The underlying network stack
151  */
152  NetworkStack *get_stack() override;
153 
154  L3IP &_l3ip;
155  OnboardNetworkStack &_stack;
156  OnboardNetworkStack::Interface *_interface = nullptr;
157  bool _dhcp = true;
158  bool _blocking = true;
159  SocketAddress _ip_address;
160  SocketAddress _netmask;
161  SocketAddress _gateway;
162  mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
163 };
164 
165 #endif
nsapi_error_t set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway) override
Set a static IP address.
SocketAddress class.
L3IPInterface(L3IP &l3ip=L3IP::get_default_instance(), OnboardNetworkStack &stack=OnboardNetworkStack::get_default_instance())
Create an L3IP-based network interface.
nsapi_error_t set_dhcp(bool dhcp) override
Enable or disable DHCP on the network.
NetworkStack class.
Definition: NetworkStack.h:42
Representation of a stack&#39;s view of an interface.
char * get_interface_name(char *interface_name) override
Get the network interface name.
nsapi_error_t get_netmask(SocketAddress *address) override
Get the local network mask.
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:142
void attach(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb) override
Register callback for status reporting.
nsapi_error_t connect() override
Start the interface.
nsapi_error_t set_blocking(bool blocking) override
Set blocking status of connect() which by default should be blocking.
nsapi_error_t get_gateway(SocketAddress *address) override
Get the local gateway.
mbed OS API for onboard IP stack abstraction
nsapi_error_t disconnect() override
Stop the interface.
This interface should be used to abstract low level access to networking hardware All operations rece...
Definition: L3IP.h:31
SocketAddress class.
Definition: SocketAddress.h:37
Common interface that is shared between network devices.
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:35
void set_as_default() override
Set the network interface as default one.
static OnboardNetworkStack & get_default_instance()
Return the default on-board network stack.
nsapi_error_t get_ip_address(SocketAddress *address) override
Get the local IP address.
Callback class based on template specialization.
Definition: Callback.h:53
nsapi_connection_status_t get_connection_status() const override
Get the connection status.
NetworkStack * get_stack() override
Provide access to the underlying stack.
L3IP & getl3ip() const
Provide access to the L3IP.
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.