Files
xiuos/Ubiquitous/XiZi_AIoT/services/net/net_server/arch/ethernetif.c
T
2024-08-14 17:22:54 +08:00

279 lines
8.2 KiB
C

/*
* Copyright (c) 2020 AIIT XUOS Lab
* XiUOS is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include "lwip/opt.h"
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/pbuf.h"
#include "lwip/stats.h"
#include "lwip/snmp.h"
#include "lwip/ethip6.h"
#include "lwip/etharp.h"
#include "libserial.h"
#include "rndis_service.h"
/* Define those to better describe your network interface. */
#define IFNAME0 'e'
#define IFNAME1 'n'
/* the lwip network interface */
struct netif *g_netif = NULL;
/* IPC rndis client */
struct Session *g_session_rndis = NULL;
/**
* In this function, the hardware should be initialized.
* Called from ethernetif_init().
*
* @param netif the already initialized lwip network interface structure
* for this ethernetif
*/
static void low_level_init(struct netif *netif)
{
#if LWIP_ARP || LWIP_ETHERNET
/* set MAC hardware address length */
netif->hwaddr_len = ETH_HWADDR_LEN;
/* set MAC hardware address */
netif->hwaddr[0] = 0x02;
netif->hwaddr[1] = 0x12;
netif->hwaddr[2] = 0x34;
netif->hwaddr[3] = 0x56;
netif->hwaddr[4] = 0x78;
netif->hwaddr[5] = 0xab;
netif->hwaddr_len = 6;
/* maximum transfer unit */
netif->mtu = 1500;
#if LWIP_ARP
netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP ;
#else
netif->flags |= NETIF_FLAG_BROADCAST;
#endif /* LWIP_ARP */
#endif /* LWIP_ARP || LWIP_ETHERNET */
}
void print_packet_data(const char *packet_data, int length) {
printf("Packet Data:\n");
for (int i = 0; i < length; ++i) {
printf("%02X ", packet_data[i]); // 打印十六进制值,每个字节两位
if ((i + 1) % 16 == 0 || i == length - 1) { // 每16个字节一行
for (int j = 0; j < 15 - (i % 16); ++j) {
printf(" "); // 打印空格对齐
}
printf("| ");
for (int j = i - (i % 16); j <= i; ++j) {
if (packet_data[j] >= 32 && packet_data[j] <= 126) {
printf("%c", packet_data[j]); // 如果是可打印的ASCII字符,打印对应字符
} else {
printf("."); // 否则打印点表示不可打印字符
}
}
printf("\n");
}
}
}
/**
* This function should do the actual transmission of the packet.
*
* @param dataptr pointer to the actual data in the buffer
* @param tot_len the length of this buffer
* @return ERR_OK if the send is success
* any other err_t on error
*/
int usbh_rndis_eth_tx(void *dataptr, size_t tot_len)
{
struct Session* session = g_session_rndis;
return ipc_usbh_rndis_eth_tx(session, dataptr, tot_len);;
}
/**
* This function should do the actual transmission of the packet. The packet is
* contained in the pbuf that is passed to the function. This pbuf
* might be chained.
*
* @param netif the lwip network interface structure for this ethernetif
* @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
* @return ERR_OK if the packet could be sent
* an err_t value if the packet couldn't be sent
*
* @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
* strange results. You might consider waiting for space in the DMA queue
* to become available since the stack doesn't retry to send a packet
* dropped because of memory failure (except for the TCP timers).
*/
static err_t low_level_output(struct netif *netif, struct pbuf *p)
{
char buf[1518];
#if ETH_PAD_SIZE
pbuf_remove_header(p, ETH_PAD_SIZE); /* drop the padding word */
#endif
pbuf_copy_partial(p, buf, p->tot_len, 0);
print_packet_data(buf, p->tot_len);
usbh_rndis_eth_tx(buf, p->tot_len);
return ERR_OK;
}
/**
* Should allocate a pbuf and transfer the bytes of the incoming
* packet from the interface into the pbuf.
*
* @param netif the lwip network interface structure for this ethernetif
* @param dataptr the received data
* @param len the length of received data
* @return a pbuf filled with the received packet (including MAC header)
* NULL on memory error
*/
static struct pbuf* low_level_input(struct netif *netif, void *dataptr, size_t len)
{
struct pbuf *p=NULL, *q=NULL;
int payload_offset = 0;
/* allocate buffer */
p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); //cherryusb 0.10.2
if (p != NULL) {
for (q = p; q != NULL; q = q->next) {
/* Copy the received frame into buffer from memory pointed by the current ETHERNET DMA Rx descriptor */
memcpy(q->payload, dataptr + payload_offset, q->len);
payload_offset += q->len;
}
}
return p;
}
/**
* This function should be called when a packet is ready to be read
* from the interface. It uses the function low_level_input() that
* should handle the actual reception of bytes from the network
* interface. Then the type of the received packet is determined and
* the appropriate input function is called.
*
* @param netif the lwip network interface structure for this ethernetif
* @param dataptr the received data
* @param len the length of received data
*/
void ethernetif_input(struct netif *netif, void *dataptr, size_t len)
{
struct pbuf *p;
/* move received packet into a new pbuf */
p = low_level_input(netif, dataptr, len);
/* if no packet could be read, silently ignore this */
if (p != NULL) {
/* pass all packets to ethernet_input, which decides what packets it supports */
if (netif->input(p, netif) != ERR_OK) {
LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
pbuf_free(p);
p = NULL;
}
}
}
/**
* This function should be called when a packet is ready to be read
* from the interface. It uses the function ethernetif_input() that
* should handle the received data.
*
* @param dataptr received data
* @param len length of received data
* @return lwIP error code
*/
int lwip_rndis_data_recv(void *dataptr, size_t len)
{
struct netif *netif = g_netif;
ethernetif_input(netif, dataptr, len);
return ERR_OK;
}
/**
* In this function, get session info.
* Called from ethernetif_init().
*
* @return lwIP error code
*/
int ipc_rndis_client_init(void)
{
struct Session *session;
session = mem_malloc(sizeof(struct Session));
if(connect_session(session, "RndisServer", 4096) < 0) {
printf("connect session failed\n");
return ERR_IF;
}
g_session_rndis = session;
return ERR_OK;
}
/**
* Should be called at the beginning of the program to set up the
* network interface. It calls the function low_level_init() to do the
* actual setup of the hardware.
*
* This function should be passed as a parameter to netif_add().
*
* @param netif the lwip network interface structure for this ethernetif
* @return ERR_OK if the loopif is initialized
* ERR_MEM if private data couldn't be allocated
* any other err_t on error
*/
err_t ethernetif_init(struct netif *netif)
{
LWIP_ASSERT("netif != NULL", (netif != NULL));
#if LWIP_NETIF_HOSTNAME
/* Initialize interface hostname */
netif->hostname = "lwip";
#endif /* LWIP_NETIF_HOSTNAME */
/*
* Initialize the snmp variables and counters inside the struct netif.
* The last argument should be replaced with your link speed, in units
* of bits per second.
*/
MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, 100000000);
netif->name[0] = IFNAME0;
netif->name[1] = IFNAME1;
#if LWIP_IPV4
netif->output = etharp_output;
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
netif->output_ip6 = ethip6_output;
#endif /* LWIP_IPV6 */
netif->linkoutput = low_level_output;
/* initialize the hardware */
low_level_init(netif);
g_netif = netif;
ipc_rndis_client_init();
return ERR_OK;
}