Knight KE / Mbed OS Game_Master
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 {
00041 public:
00042     /** Create an EMAC-based network interface.
00043      *
00044      * The default arguments obtain the default EMAC, which will be target-
00045      * dependent (and the target may have some JSON option to choose which
00046      * is the default, if there are multiple). The default stack is configured
00047      * by JSON option nsapi.default-stack.
00048      *
00049      * Due to inability to return errors from the constructor, no real
00050      * work is done until the first call to connect().
00051      *
00052      * @param emac  Reference to EMAC to use
00053      * @param stack Reference to onboard-network stack to use
00054      */
00055     EMACInterface(
00056             EMAC &emac = EMAC::get_default_instance(),
00057             OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance());
00058 
00059     /** Set a static IP address
00060      *
00061      *  Configures this network interface to use a static IP address.
00062      *  Implicitly disables DHCP, which can be enabled in set_dhcp.
00063      *  Requires that the network is disconnected.
00064      *
00065      *  @param ip_address  Null-terminated representation of the local IP address
00066      *  @param netmask     Null-terminated representation of the local network mask
00067      *  @param gateway     Null-terminated representation of the local gateway
00068      *  @return            0 on success, negative error code on failure
00069      */
00070     virtual nsapi_error_t set_network(
00071             const char *ip_address, const char *netmask, const char *gateway);
00072 
00073     /** Enable or disable DHCP on the network
00074      *
00075      *  Requires that the network is disconnected
00076      *
00077      *  @param dhcp     False to disable dhcp (defaults to enabled)
00078      *  @return         0 on success, negative error code on failure
00079      */
00080     virtual nsapi_error_t set_dhcp(bool dhcp);
00081 
00082     /** Start the interface
00083      *  @return             0 on success, negative on failure
00084      */
00085     virtual nsapi_error_t connect();
00086 
00087     /** Stop the interface
00088      *  @return             0 on success, negative on failure
00089      */
00090     virtual nsapi_error_t disconnect();
00091 
00092     /** Get the local MAC address
00093      *
00094      *  Provided MAC address is intended for info or debug purposes and
00095      *  may not be provided if the underlying network interface does not
00096      *  provide a MAC address
00097      *  
00098      *  @return         Null-terminated representation of the local MAC address
00099      *                  or null if no MAC address is available
00100      */
00101     virtual const char *get_mac_address();
00102 
00103     /** Get the local IP address
00104      *
00105      *  @return         Null-terminated representation of the local IP address
00106      *                  or null if no IP address has been recieved
00107      */
00108     virtual const char *get_ip_address();
00109 
00110     /** Get the local network mask
00111      *
00112      *  @return         Null-terminated representation of the local network mask 
00113      *                  or null if no network mask has been recieved
00114      */
00115     virtual const char *get_netmask();
00116 
00117     /** Get the local gateways
00118      *
00119      *  @return         Null-terminated representation of the local gateway
00120      *                  or null if no network mask has been recieved
00121      */
00122     virtual const char *get_gateway();
00123 
00124     /** Register callback for status reporting
00125      *
00126      *  @param status_cb The callback for status changes
00127      */
00128     virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
00129 
00130     /** Get the connection status
00131      *
00132      *  @return         The connection status according to nsapi_connection_status_t
00133      */
00134     virtual nsapi_connection_status_t get_connection_status() const;
00135 
00136     /** Set blocking status of connect() which by default should be blocking
00137      *
00138      *  @param blocking true if connect is blocking
00139      *  @return         0 on success, negative error code on failure
00140      */
00141     virtual nsapi_error_t set_blocking(bool blocking);
00142 
00143     /** Provide access to the EMAC
00144      *
00145      * This should be used with care - normally the network stack would
00146      * control the EMAC, so manipulating the EMAC while the stack
00147      * is also using it (ie after connect) will likely cause problems.
00148      *
00149      * @return          Reference to the EMAC in use
00150      */
00151     EMAC &get_emac() const { return _emac; }
00152 
00153     virtual EMACInterface *emacInterface() {
00154         return this;
00155     }
00156 
00157 protected:
00158     /** Provide access to the underlying stack
00159      *
00160      *  @return The underlying network stack 
00161      */
00162     virtual NetworkStack *get_stack();
00163 
00164     EMAC &_emac;
00165     OnboardNetworkStack &_stack;
00166     OnboardNetworkStack::Interface *_interface;
00167     bool _dhcp;
00168     bool _blocking;
00169     char _mac_address[NSAPI_MAC_SIZE];
00170     char _ip_address[NSAPI_IPv6_SIZE];
00171     char _netmask[NSAPI_IPv4_SIZE];
00172     char _gateway[NSAPI_IPv4_SIZE];
00173     Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
00174 };
00175 
00176 #endif