1
votes

I'm working on a project where I have a LPC1768 MBED device. I can connect this device by USB with the computer. The device itself has a working outgoing Ethernet connection which I can read using the MBED library.

On the embedded device, internet traffic enters with the USB-CDC ECM protocol. I want to give the packets to the working Ethernet interface. I have the following code:

#include "mbed.h"
#include "USBCDC_ECM.h"
#include "rtos.h"

extern "C"{
#include <stdint.h>
}

Ethernet eth;

// create a buffer
#define ETH_MTU 1514
static uint8_t buf[ETH_MTU];
static char ethbuf[ETH_MTU];

int main(void) {
    USBCDC_ECM usb = USBCDC_ECM(0x0525, 0xa4a1, 1);

    wait(1);

    int rx_i = 0;

    while(1)
    {
        int size = eth.receive();
        if(size > 0)
        {
            eth.read(ethbuf, size);
            printf("Read from Ethernet: %d\r\n", size);

            // send data to usb
            uint8_t *usb_data = (uint8_t *)malloc(sizeof(uint8_t) * size);
            memcpy(ethbuf, usb_data, size);
            int sent = 0;
            while(sent < size)
            {
                int to_send = 64;
                if ((size - sent) < to_send) { to_send = size - sent; }
                int res = usb.send(usb_data + sent, to_send);
                printf("result from sending to usb: %d\r\n", res);
                sent += to_send;
            }
            if ((size % 64) == 0) { usb.send(usb_data, 0); }
        }

        bool ret = usb.readEP_NB(buf + rx_i, (uint32_t *)&size);
        if(!ret) { continue; }

        rx_i += size;
        if(size < 64) {
            printf("received frame with size: %d\r\n", rx_i);

            // send this frame over Ethernet
            char *ethernet_data = (char *)malloc(sizeof(char) * rx_i);
            memcpy(buf, ethernet_data, rx_i);
            eth.write(ethernet_data, rx_i);
            int result = eth.send();
            printf("result: %d\r\n", result);

            rx_i = 0;
        }

        wait(0.05);
    }
}

So in each loop, I read from the Ethernet interface and pass these packages to USB-CDC ECM (64 bytes at a time). Next, I read the incoming traffic from the USB side and pass this traffic to the Ethernet interface.

This is not working. I've used Wireshark to analyze the traffic but it looks very strange and the packets seems to be corrupted. The payload is always the same. This occurs for traffic from the internet to the computer.

Does anyone know what goes wrong here? I just pass the packets to the other interface and don't modify them. Below, I've added my wireshark output.

enter image description here

1

1 Answers

1
votes

It appears to me like your memcpy() calls are all the wrong way around.

void *memcpy(void *dest, const void *src, size_t n);

Destination is the first, source the second argument.