Add big-endian tests for our BSD layer

This commit is contained in:
Sergio R. Caprile committed 2026-07-10 18:28:40 -03:00
1 parent 72a0787161
commit 0aa25dbd18
6 files changed
+277 -106

No files matched your search

+14 -50
View File
@@ -1,36 +1,15 @@
#include <errno.h>
#include <stdio.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "mongoose.h"
#include "shm_queue.h"
#ifndef O_RDWR
#define O_RDWR 02
#endif
#define FRAME_SIZE 1540
#define FRAME_QUEUE_DEPTH 8
#define SHM_NAME "/mongoose-shm"
struct shm_frame {
size_t len;
uint8_t buf[FRAME_SIZE];
};
struct shm_queue {
sem_t full;
sem_t empty;
size_t head;
size_t tail;
struct shm_frame frames[FRAME_QUEUE_DEPTH];
};
struct shm_link {
struct shm_queue to_tap;
struct shm_queue from_tap;
};
struct shm_driver_data {
const char *name;
FILE *fp;
@@ -48,14 +27,21 @@ static bool shm_init(struct mg_tcpip_if *ifp) {
int fd;
fd = shm_open(d->name, O_RDWR, 0600);
if (fd < 0) return false;
if (fd < 0) {
printf("shm_open failed: %d\n", errno);
return false;
}
d->fp = fdopen(fd, "r+");
if (d->fp == NULL) return false;
if (d->fp == NULL) {
printf("fdopen failed: %d\n", errno);
return false;
}
d->shm = (struct shm_link *) mmap(NULL, sizeof(*d->shm),
PROT_READ | PROT_WRITE, MAP_SHARED,
fileno(d->fp), 0);
if (d->shm == MAP_FAILED) {
printf("mmap failed: %d\n", errno);
fclose(d->fp);
d->fp = NULL;
d->shm = NULL;
@@ -67,39 +53,17 @@ static bool shm_init(struct mg_tcpip_if *ifp) {
static size_t shm_tx(const void *buf, size_t len, struct mg_tcpip_if *ifp) {
struct shm_driver_data *d = (struct shm_driver_data *) ifp->driver_data;
struct shm_queue *q = &d->shm->to_tap;
struct shm_frame *f;
if (len > sizeof(q->frames[0].buf)) return 0;
if (sem_trywait(&q->empty) != 0) {
if (!shm_queue_push(&d->shm->to_tap, buf, len)) {
printf("\nFRAME LOST\n");
return 0;
}
f = &q->frames[q->tail];
memcpy(f->buf, buf, len);
f->len = len;
q->tail = (q->tail + 1) % FRAME_QUEUE_DEPTH;
sem_post(&q->full);
return len;
}
static size_t shm_rx(void *buf, size_t len, struct mg_tcpip_if *ifp) {
struct shm_driver_data *d = (struct shm_driver_data *) ifp->driver_data;
struct shm_queue *q = &d->shm->from_tap;
struct shm_frame *f;
size_t n;
if (sem_trywait(&q->full) != 0) return 0;
f = &q->frames[q->head];
n = f->len;
q->head = (q->head + 1) % FRAME_QUEUE_DEPTH;
if (n > len) {
sem_post(&q->empty);
return 0;
}
memcpy(buf, f->buf, n);
sem_post(&q->empty);
return n;
return shm_queue_pop(&d->shm->from_tap, buf, len);
}
static bool shm_poll(struct mg_tcpip_if *ifp, bool s1) {
@@ -140,7 +104,7 @@ int main(void) {
// Setup interface
const char *mac = "02:00:01:02:03:78"; // MAC address
printf("Opened SHM interface\n");
printf("Opening SHM interface\n");
usleep(200000); // 200 ms
result = bsd_x_test(&shm_driver, &shm_data, mac, debug_level);