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 
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  virtual ~L3IPInterface();
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 Null-terminated representation of the local IP address
60  * @param netmask Null-terminated representation of the local network mask
61  * @param gateway Null-terminated representation of the local gateway
62  * @return 0 on success, negative error code on failure
63  */
64  virtual nsapi_error_t set_network(const char *ip_address, const char *netmask, const char *gateway);
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  virtual nsapi_error_t set_dhcp(bool dhcp);
74 
75  /** Start the interface
76  * @return 0 on success, negative on failure
77  */
78  virtual nsapi_error_t connect();
79 
80  /** Stop the interface
81  * @return 0 on success, negative on failure
82  */
83  virtual nsapi_error_t disconnect();
84 
85  /** Get the local IP address
86  *
87  * @return Null-terminated representation of the local IP address
88  * or null if no IP address has been received
89  */
90  virtual const char *get_ip_address();
91 
92  /** Get the local network mask
93  *
94  * @return Null-terminated representation of the local network mask
95  * or null if no network mask has been received
96  */
97  virtual const char *get_netmask();
98 
99  /** Get the local gateways
100  *
101  * @return Null-terminated representation of the local gateway
102  * or null if no network mask has been received
103  */
104  virtual const char *get_gateway();
105 
106  /** Get the network interface name
107  *
108  * @return Null-terminated representation of the network interface name
109  * or null if interface not exists
110  */
111  virtual char *get_interface_name(char *interface_name);
112 
113  /** Set the network interface as default one
114  */
115  virtual void set_as_default();
116 
117  /** Register callback for status reporting
118  *
119  * @param status_cb The callback for status changes
120  */
121  virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
122 
123  /** Get the connection status
124  *
125  * @return The connection status according to nsapi_connection_status_t
126  */
127  virtual nsapi_connection_status_t get_connection_status() const;
128 
129  /** Set blocking status of connect() which by default should be blocking
130  *
131  * @param blocking true if connect is blocking
132  * @return 0 on success, negative error code on failure
133  */
134  virtual nsapi_error_t set_blocking(bool blocking);
135 
136  /** Provide access to the L3IP
137  *
138  * This should be used with care - normally the network stack would
139  * control the L3IP, so manipulating the L3IP while the stack
140  * is also using it (ie after connect) will likely cause problems.
141  *
142  * @return Reference to the L3IP in use
143  */
144  L3IP &getl3ip() const
145  {
146  return _l3ip;
147  }
148 
149  virtual L3IPInterface *l3ipInterface()
150  {
151  return this;
152  }
153 
154 protected:
155  /** Provide access to the underlying stack
156  *
157  * @return The underlying network stack
158  */
159  virtual NetworkStack *get_stack();
160 
161  L3IP &_l3ip;
162  OnboardNetworkStack &_stack;
163  OnboardNetworkStack::Interface *_interface;
164  bool _dhcp;
165  bool _blocking;
166  char _ip_address[NSAPI_IPv6_SIZE];
167  char _netmask[NSAPI_IPv4_SIZE];
168  char _gateway[NSAPI_IPv4_SIZE];
169  mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
170 };
171 
172 #endif
virtual NetworkStack * get_stack()
Provide access to the underlying stack.
L3IPInterface(L3IP &l3ip=L3IP::get_default_instance(), OnboardNetworkStack &stack=OnboardNetworkStack::get_default_instance())
Create an L3IP-based network interface.
NetworkStack class.
Definition: NetworkStack.h:40
Representation of a stack&#39;s view of an interface.
virtual const char * get_netmask()
Get the local network mask.
virtual const char * get_ip_address()
Get the local IP address.
virtual void attach(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb)
Register callback for status reporting.
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:95
virtual nsapi_error_t disconnect()
Stop the interface.
virtual nsapi_error_t set_blocking(bool blocking)
Set blocking status of connect() which by default should be blocking.
mbed OS API for onboard IP stack abstraction
#define NSAPI_IPv4_SIZE
Size of IPv4 representation.
Definition: nsapi_types.h:159
This interface should be used to abstract low level access to networking hardware All operations rece...
Definition: L3IP.h:31
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
virtual nsapi_error_t set_dhcp(bool dhcp)
Enable or disable DHCP on the network.
#define NSAPI_IPv6_SIZE
Size of IPv6 representation.
Definition: nsapi_types.h:167
virtual char * get_interface_name(char *interface_name)
Get the network interface name.
virtual void set_as_default()
Set the network interface as default one.
virtual const char * get_gateway()
Get the local gateways.
static OnboardNetworkStack & get_default_instance()
Return the default on-board network stack.
Callback class based on template specialization.
Definition: Callback.h:39
virtual nsapi_error_t set_network(const char *ip_address, const char *netmask, const char *gateway)
Set a static IP address.
virtual nsapi_error_t connect()
Start the interface.
virtual nsapi_connection_status_t get_connection_status() const
Get the connection status.
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.