|
| 1 | +/* main.c |
| 2 | + * |
| 3 | + * Copyright (C) 2026 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfIP TCP/IP stack. |
| 6 | + * |
| 7 | + * wolfIP is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfIP is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 20 | + */ |
| 21 | +#include <stdint.h> |
| 22 | +#include <string.h> |
| 23 | +#include "config.h" |
| 24 | +#include "wolfip.h" |
| 25 | +#include "stm32h5_eth.h" |
| 26 | + |
| 27 | +#define ECHO_PORT 7 |
| 28 | +#define RX_BUF_SIZE 1024 |
| 29 | + |
| 30 | +#define RCC_BASE 0x44020C00u |
| 31 | +#define RCC_AHB1ENR (*(volatile uint32_t *)(RCC_BASE + 0x88u)) |
| 32 | + |
| 33 | +static struct wolfIP *IPStack; |
| 34 | +static int listen_fd = -1; |
| 35 | +static int client_fd = -1; |
| 36 | +static uint8_t rx_buf[RX_BUF_SIZE]; |
| 37 | + |
| 38 | +uint32_t wolfIP_getrandom(void) |
| 39 | +{ |
| 40 | + static uint32_t lfsr = 0x1A2B3C4DU; |
| 41 | + lfsr ^= lfsr << 13; |
| 42 | + lfsr ^= lfsr >> 17; |
| 43 | + lfsr ^= lfsr << 5; |
| 44 | + return lfsr; |
| 45 | +} |
| 46 | + |
| 47 | +static void echo_cb(int fd, uint16_t event, void *arg) |
| 48 | +{ |
| 49 | + struct wolfIP *s = (struct wolfIP *)arg; |
| 50 | + int ret; |
| 51 | + |
| 52 | + if ((fd == listen_fd) && (event & CB_EVENT_READABLE) && (client_fd == -1)) { |
| 53 | + client_fd = wolfIP_sock_accept(s, listen_fd, NULL, NULL); |
| 54 | + if (client_fd > 0) { |
| 55 | + wolfIP_register_callback(s, client_fd, echo_cb, s); |
| 56 | + } |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if ((fd == client_fd) && (event & CB_EVENT_READABLE)) { |
| 61 | + ret = wolfIP_sock_recvfrom(s, client_fd, rx_buf, sizeof(rx_buf), 0, NULL, NULL); |
| 62 | + if (ret > 0) { |
| 63 | + (void)wolfIP_sock_sendto(s, client_fd, rx_buf, (uint32_t)ret, 0, NULL, 0); |
| 64 | + } else if (ret == 0) { |
| 65 | + wolfIP_sock_close(s, client_fd); |
| 66 | + client_fd = -1; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if ((fd == client_fd) && (event & CB_EVENT_CLOSED)) { |
| 71 | + wolfIP_sock_close(s, client_fd); |
| 72 | + client_fd = -1; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +int main(void) |
| 77 | +{ |
| 78 | + struct wolfIP_ll_dev *ll; |
| 79 | + struct wolfIP_sockaddr_in addr; |
| 80 | + uint64_t tick = 0; |
| 81 | + |
| 82 | + wolfIP_init_static(&IPStack); |
| 83 | + RCC_AHB1ENR |= (1u << 19) | (1u << 20) | (1u << 21); |
| 84 | + ll = wolfIP_getdev(IPStack); |
| 85 | + (void)stm32h5_eth_init(ll, NULL); |
| 86 | + |
| 87 | + wolfIP_ipconfig_set(IPStack, |
| 88 | + atoip4(WOLFIP_IP), |
| 89 | + atoip4(WOLFIP_NETMASK), |
| 90 | + atoip4(WOLFIP_GW)); |
| 91 | + |
| 92 | + listen_fd = wolfIP_sock_socket(IPStack, AF_INET, IPSTACK_SOCK_STREAM, 0); |
| 93 | + wolfIP_register_callback(IPStack, listen_fd, echo_cb, IPStack); |
| 94 | + |
| 95 | + memset(&addr, 0, sizeof(addr)); |
| 96 | + addr.sin_family = AF_INET; |
| 97 | + addr.sin_port = ee16(ECHO_PORT); |
| 98 | + addr.sin_addr.s_addr = 0; |
| 99 | + (void)wolfIP_sock_bind(IPStack, listen_fd, (struct wolfIP_sockaddr *)&addr, sizeof(addr)); |
| 100 | + (void)wolfIP_sock_listen(IPStack, listen_fd, 1); |
| 101 | + |
| 102 | + for (;;) { |
| 103 | + (void)wolfIP_poll(IPStack, tick++); |
| 104 | + } |
| 105 | + return 0; |
| 106 | +} |
0 commit comments