|
| 1 | +/* LWIP implementation of NetworkInterfaceAPI |
| 2 | + * Copyright (c) 2015 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may 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, |
| 12 | + * WITHOUT 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 EMAC_INTERFACE_H |
| 18 | +#define EMAC_INTERFACE_H |
| 19 | + |
| 20 | +#include "nsapi.h" |
| 21 | +#include "rtos.h" |
| 22 | +#include "EMAC.h" |
| 23 | +#include "OnboardNetworkStack.h" |
| 24 | + |
| 25 | + |
| 26 | +/** EMACInterface class |
| 27 | + * Implementation of the NetworkInterface for an EMAC-based driver |
| 28 | + * |
| 29 | + * This class provides the necessary glue logic to create a NetworkInterface |
| 30 | + * based on an EMAC and an OnboardNetworkStack. EthernetInterface and |
| 31 | + * EMAC-based Wi-Fi drivers derive from it. |
| 32 | + * |
| 33 | + * Drivers derived from EMACInterface should be constructed so that their |
| 34 | + * EMAC is functional without the need to call `connect()`. For example |
| 35 | + * a Wi-Fi driver should permit `WiFi::get_emac().power_up()` as soon as |
| 36 | + * the credentials have been set. This is necessary to support specialised |
| 37 | + * applications such as 6LoWPAN mesh border routers. |
| 38 | + */ |
| 39 | +class EMACInterface : public virtual NetworkInterface |
| 40 | +{ |
| 41 | +public: |
| 42 | + /** Create an EMAC-based network interface. |
| 43 | + * |
| 44 | + * The default arguments obtain the default EMAC, which will be target- |
| 45 | + * dependent (and the target may have some JSON option to choose which |
| 46 | + * is the default, if there are multiple). The default stack is configured |
| 47 | + * by JSON option nsapi.default-stack. |
| 48 | + * |
| 49 | + * Due to inability to return errors from the constructor, no real |
| 50 | + * work is done until the first call to connect(). |
| 51 | + * |
| 52 | + * @param emac Reference to EMAC to use |
| 53 | + * @param stack Reference to onboard-network stack to use |
| 54 | + */ |
| 55 | + EMACInterface( |
| 56 | + EMAC &emac = EMAC::get_default_instance(), |
| 57 | + OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance()); |
| 58 | + |
| 59 | + /** Set a static IP address |
| 60 | + * |
| 61 | + * Configures this network interface to use a static IP address. |
| 62 | + * Implicitly disables DHCP, which can be enabled in set_dhcp. |
| 63 | + * Requires that the network is disconnected. |
| 64 | + * |
| 65 | + * @param ip_address Null-terminated representation of the local IP address |
| 66 | + * @param netmask Null-terminated representation of the local network mask |
| 67 | + * @param gateway Null-terminated representation of the local gateway |
| 68 | + * @return 0 on success, negative error code on failure |
| 69 | + */ |
| 70 | + virtual nsapi_error_t set_network( |
| 71 | + const char *ip_address, const char *netmask, const char *gateway); |
| 72 | + |
| 73 | + /** Enable or disable DHCP on the network |
| 74 | + * |
| 75 | + * Requires that the network is disconnected |
| 76 | + * |
| 77 | + * @param dhcp False to disable dhcp (defaults to enabled) |
| 78 | + * @return 0 on success, negative error code on failure |
| 79 | + */ |
| 80 | + virtual nsapi_error_t set_dhcp(bool dhcp); |
| 81 | + |
| 82 | + /** Start the interface |
| 83 | + * @return 0 on success, negative on failure |
| 84 | + */ |
| 85 | + virtual nsapi_error_t connect(); |
| 86 | + |
| 87 | + /** Stop the interface |
| 88 | + * @return 0 on success, negative on failure |
| 89 | + */ |
| 90 | + virtual nsapi_error_t disconnect(); |
| 91 | + |
| 92 | + /** Get the local MAC address |
| 93 | + * |
| 94 | + * Provided MAC address is intended for info or debug purposes and |
| 95 | + * may not be provided if the underlying network interface does not |
| 96 | + * provide a MAC address |
| 97 | + * |
| 98 | + * @return Null-terminated representation of the local MAC address |
| 99 | + * or null if no MAC address is available |
| 100 | + */ |
| 101 | + virtual const char *get_mac_address(); |
| 102 | + |
| 103 | + /** Get the local IP address |
| 104 | + * |
| 105 | + * @return Null-terminated representation of the local IP address |
| 106 | + * or null if no IP address has been recieved |
| 107 | + */ |
| 108 | + virtual const char *get_ip_address(); |
| 109 | + |
| 110 | + /** Get the local network mask |
| 111 | + * |
| 112 | + * @return Null-terminated representation of the local network mask |
| 113 | + * or null if no network mask has been recieved |
| 114 | + */ |
| 115 | + virtual const char *get_netmask(); |
| 116 | + |
| 117 | + /** Get the local gateways |
| 118 | + * |
| 119 | + * @return Null-terminated representation of the local gateway |
| 120 | + * or null if no network mask has been recieved |
| 121 | + */ |
| 122 | + virtual const char *get_gateway(); |
| 123 | + |
| 124 | + /** Register callback for status reporting |
| 125 | + * |
| 126 | + * @param status_cb The callback for status changes |
| 127 | + */ |
| 128 | + virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb); |
| 129 | + |
| 130 | + /** Get the connection status |
| 131 | + * |
| 132 | + * @return The connection status according to nsapi_connection_status_t |
| 133 | + */ |
| 134 | + virtual nsapi_connection_status_t get_connection_status() const; |
| 135 | + |
| 136 | + /** Set blocking status of connect() which by default should be blocking |
| 137 | + * |
| 138 | + * @param blocking true if connect is blocking |
| 139 | + * @return 0 on success, negative error code on failure |
| 140 | + */ |
| 141 | + virtual nsapi_error_t set_blocking(bool blocking); |
| 142 | + |
| 143 | + /** Provide access to the EMAC |
| 144 | + * |
| 145 | + * This should be used with care - normally the network stack would |
| 146 | + * control the EMAC, so manipulating the EMAC while the stack |
| 147 | + * is also using it (ie after connect) will likely cause problems. |
| 148 | + * |
| 149 | + * @return Reference to the EMAC in use |
| 150 | + */ |
| 151 | + EMAC &get_emac() const { return _emac; } |
| 152 | + |
| 153 | +protected: |
| 154 | + /** Provide access to the underlying stack |
| 155 | + * |
| 156 | + * @return The underlying network stack |
| 157 | + */ |
| 158 | + virtual NetworkStack *get_stack(); |
| 159 | + |
| 160 | + EMAC &_emac; |
| 161 | + OnboardNetworkStack &_stack; |
| 162 | + OnboardNetworkStack::Interface *_interface; |
| 163 | + bool _dhcp; |
| 164 | + char _mac_address[NSAPI_MAC_SIZE]; |
| 165 | + char _ip_address[NSAPI_IPv6_SIZE]; |
| 166 | + char _netmask[NSAPI_IPv4_SIZE]; |
| 167 | + char _gateway[NSAPI_IPv4_SIZE]; |
| 168 | +}; |
| 169 | + |
| 170 | +#endif |
0 commit comments