takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EMACInterface.h Source File

EMACInterface.h

00001 /* LWIP implementation of NetworkInterfaceAPI
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef EMAC_INTERFACE_H
00018 #define EMAC_INTERFACE_H
00019 
00020 #include "nsapi.h"
00021 #include "rtos.h"
00022 #include "EMAC.h"
00023 #include "OnboardNetworkStack.h"
00024 
00025 
00026 /** EMACInterface class
00027  *  Implementation of the NetworkInterface for an EMAC-based driver
00028  *
00029  * This class provides the necessary glue logic to create a NetworkInterface
00030  * based on an EMAC and an OnboardNetworkStack. EthernetInterface and
00031  * EMAC-based Wi-Fi drivers derive from it.
00032  *
00033  * Drivers derived from EMACInterface should be constructed so that their
00034  * EMAC is functional without the need to call `connect()`. For example
00035  * a Wi-Fi driver should permit `WiFi::get_emac().power_up()` as soon as
00036  * the credentials have been set. This is necessary to support specialised
00037  * applications such as 6LoWPAN mesh border routers.
00038  */
00039 class EMACInterface : public virtual NetworkInterface {
00040 public:
00041     /** Create an EMAC-based network interface.
00042      *
00043      * The default arguments obtain the default EMAC, which will be target-
00044      * dependent (and the target may have some JSON option to choose which
00045      * is the default, if there are multiple). The default stack is configured
00046      * by JSON option nsapi.default-stack.
00047      *
00048      * Due to inability to return errors from the constructor, no real
00049      * work is done until the first call to connect().
00050      *
00051      * @param emac  Reference to EMAC to use
00052      * @param stack Reference to onboard-network stack to use
00053      */
00054     EMACInterface(EMAC &emac = EMAC::get_default_instance(),
00055                   OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance());
00056 
00057     /** Set a static IP address
00058      *
00059      *  Configures this network interface to use a static IP address.
00060      *  Implicitly disables DHCP, which can be enabled in set_dhcp.
00061      *  Requires that the network is disconnected.
00062      *
00063      *  @param ip_address  Null-terminated representation of the local IP address
00064      *  @param netmask     Null-terminated representation of the local network mask
00065      *  @param gateway     Null-terminated representation of the local gateway
00066      *  @return            0 on success, negative error code on failure
00067      */
00068     virtual nsapi_error_t set_network(const char *ip_address, const char *netmask, const char *gateway);
00069 
00070     /** Enable or disable DHCP on the network
00071      *
00072      *  Requires that the network is disconnected
00073      *
00074      *  @param dhcp     False to disable dhcp (defaults to enabled)
00075      *  @return         0 on success, negative error code on failure
00076      */
00077     virtual nsapi_error_t set_dhcp(bool dhcp);
00078 
00079     /** Start the interface
00080      *  @return             0 on success, negative on failure
00081      */
00082     virtual nsapi_error_t connect();
00083 
00084     /** Stop the interface
00085      *  @return             0 on success, negative on failure
00086      */
00087     virtual nsapi_error_t disconnect();
00088 
00089     /** Get the local MAC address
00090      *
00091      *  Provided MAC address is intended for info or debug purposes and
00092      *  may not be provided if the underlying network interface does not
00093      *  provide a MAC address
00094      *
00095      *  @return         Null-terminated representation of the local MAC address
00096      *                  or null if no MAC address is available
00097      */
00098     virtual const char *get_mac_address();
00099 
00100     /** Get the local IP address
00101      *
00102      *  @return         Null-terminated representation of the local IP address
00103      *                  or null if no IP address has been recieved
00104      */
00105     virtual const char *get_ip_address();
00106 
00107     /** Get the local network mask
00108      *
00109      *  @return         Null-terminated representation of the local network mask
00110      *                  or null if no network mask has been recieved
00111      */
00112     virtual const char *get_netmask();
00113 
00114     /** Get the local gateways
00115      *
00116      *  @return         Null-terminated representation of the local gateway
00117      *                  or null if no network mask has been recieved
00118      */
00119     virtual const char *get_gateway();
00120 
00121     /** Register callback for status reporting
00122      *
00123      *  @param status_cb The callback for status changes
00124      */
00125     virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
00126 
00127     /** Get the connection status
00128      *
00129      *  @return         The connection status according to nsapi_connection_status_t
00130      */
00131     virtual nsapi_connection_status_t get_connection_status() const;
00132 
00133     /** Set blocking status of connect() which by default should be blocking
00134      *
00135      *  @param blocking true if connect is blocking
00136      *  @return         0 on success, negative error code on failure
00137      */
00138     virtual nsapi_error_t set_blocking(bool blocking);
00139 
00140     /** Provide access to the EMAC
00141      *
00142      * This should be used with care - normally the network stack would
00143      * control the EMAC, so manipulating the EMAC while the stack
00144      * is also using it (ie after connect) will likely cause problems.
00145      *
00146      * @return          Reference to the EMAC in use
00147      */
00148     EMAC &get_emac() const
00149     {
00150         return _emac;
00151     }
00152 
00153     virtual EMACInterface *emacInterface()
00154     {
00155         return this;
00156     }
00157 
00158 protected:
00159     /** Provide access to the underlying stack
00160      *
00161      *  @return The underlying network stack
00162      */
00163     virtual NetworkStack *get_stack();
00164 
00165     EMAC &_emac;
00166     OnboardNetworkStack &_stack;
00167     OnboardNetworkStack::Interface *_interface;
00168     bool _dhcp;
00169     bool _blocking;
00170     char _mac_address[NSAPI_MAC_SIZE];
00171     char _ip_address[NSAPI_IPv6_SIZE];
00172     char _netmask[NSAPI_IPv4_SIZE];
00173     char _gateway[NSAPI_IPv4_SIZE];
00174     Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
00175 };
00176 
00177 #endif