Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers emac_lwip.c Source File

emac_lwip.c

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2016 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 #if DEVICE_EMAC
00018 
00019 #include "emac_api.h"
00020 #include "emac_stack_mem.h"
00021 #include "lwip/tcpip.h"
00022 #include "lwip/tcp.h"
00023 #include "lwip/ip.h"
00024 #include "netif/etharp.h"
00025 #include "lwip/ethip6.h"
00026 
00027 static err_t emac_lwip_low_level_output(struct netif *netif, struct pbuf *p)
00028 {
00029     emac_interface_t *mac = (emac_interface_t *)netif->state;
00030     bool ret = mac->ops.link_out(mac, (emac_stack_mem_t *)p);
00031 
00032     return ret ? ERR_OK : ERR_IF;
00033 }
00034 
00035 static void emac_lwip_input(void *data, emac_stack_t *buf)
00036 {
00037     struct pbuf *p = (struct pbuf *)buf;
00038     struct netif *netif = (struct netif *)data;
00039 
00040     /* pass all packets to ethernet_input, which decides what packets it supports */
00041     if (netif->input(p, netif) != ERR_OK) {
00042         LWIP_DEBUGF(NETIF_DEBUG, ("Emac LWIP: IP input error\n"));
00043 
00044         pbuf_free(p);
00045     }
00046 }
00047 
00048 static void emac_lwip_state_change(void *data, bool up)
00049 {
00050     struct netif *netif = (struct netif *)data;
00051 
00052     if (up) {
00053         tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_up, netif, 1);
00054     } else {
00055         tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_down, netif, 1);
00056     }
00057 }
00058 
00059 err_t emac_lwip_if_init(struct netif *netif)
00060 {
00061     int err = ERR_OK;
00062     emac_interface_t *mac = (emac_interface_t *)netif->state;
00063 
00064     mac->ops.set_link_input_cb(mac, emac_lwip_input, netif);
00065     mac->ops.set_link_state_cb(mac, emac_lwip_state_change, netif);
00066 
00067     if (!mac->ops.power_up(mac)) {
00068         err = ERR_IF;
00069     }
00070 
00071     netif->mtu = mac->ops.get_mtu_size(mac);
00072     netif->hwaddr_len = mac->ops.get_hwaddr_size(mac);
00073     mac->ops.get_hwaddr(mac, netif->hwaddr);
00074 
00075     /* Interface capabilities */
00076     netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP;
00077 
00078     mac->ops.get_ifname(mac, netif->name, 2);
00079 
00080 #if LWIP_IPV4
00081     netif->output = etharp_output;
00082 #endif /* LWIP_IPV4 */
00083 #if LWIP_IPV6
00084     netif->output_ip6 = ethip6_output;
00085 #endif /* LWIP_IPV6 */
00086 
00087     netif->linkoutput = emac_lwip_low_level_output;
00088 
00089     return err;
00090 }
00091 
00092 #endif /* DEVICE_EMAC */