obsw/mission/testIp.cpp

94 lines
2.4 KiB
C++

#include "FreeRTOS.h"
#include "lwip/sio.h"
#include "task.h"
#include <arpa/inet.h>
#include <cstring>
#include <lwip/ip_addr.h>
#include <lwip/netifapi.h>
#include <lwip/tcpip.h>
#include <netif/slipif.h>
#include <sys/socket.h>
#include <xuartps.h>
extern "C" {
void slipif_rxbyte_input(struct netif *netif, u8_t c);
void myInitDone(void *arg) { puts("init done"); }
struct netif netif;
void pollUart(void *) {
while (1) {
if (XUartPs_IsReceiveData(STDIN_BASEADDRESS)) {
u32 RecievedByte;
/* Wait until there is data */
while (XUartPs_IsReceiveData(STDIN_BASEADDRESS)) {
RecievedByte = XUartPs_ReadReg(STDIN_BASEADDRESS, XUARTPS_FIFO_OFFSET);
slipif_rxbyte_input(&netif, (u8)RecievedByte);
}
}
vTaskDelay(pdMS_TO_TICKS(5));
}
}
}
uint32_t sio_data;
sio_fd_t sio_open(u8_t devnum) { return &sio_data; }
void sio_send(u8_t c, sio_fd_t fd) { XUartPs_SendByte(STDOUT_BASEADDRESS, c); }
void testIp() {
tcpip_init(myInitDone, nullptr);
ip_addr_t slip_addr = IPADDR4_INIT_BYTES(10, 0, 0, 32),
slip_mask = IPADDR4_INIT_BYTES(255, 255, 255, 0),
slip_gw = IPADDR4_INIT_BYTES(10, 25, 0, 1);
netifapi_netif_add(&netif, &slip_addr, &slip_mask, &slip_gw, NULL,
slipif_init, netif_input);
netifapi_netif_set_default(&netif);
// should be done by driver, which does not do it, so we do it here
netifapi_netif_set_link_up(&netif);
netifapi_netif_set_up(&netif);
vTaskDelay(pdMS_TO_TICKS(2000));
xTaskCreate(
pollUart, /* The function that implements the task. */
"uart", /* The text name assigned to the task - for debug only as it is not used by the
kernel. */
2048, /* The size of the stack to allocate to the task. */
nullptr, /* The parameter passed to the task - not used in this simple case. */
1, /* The priority assigned to the task. */
nullptr);
puts("socket");
int serverSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (serverSocket == -1) {
puts("socket failed");
return;
}
sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(0xcafe);
serverAddr.sin_addr.s_addr = inet_addr("10.0.0.13");
uint8_t data[] = {1, 2, 3, 4, 5, 6, 7};
puts("send");
sendto(serverSocket, data, sizeof(data), 0, (sockaddr *)&serverAddr,
sizeof(serverAddr));
puts("send done");
}