Fix current tests
This commit is contained in:
parent
c28d050037
commit
ce78c28367
|
@ -1,13 +1,21 @@
|
||||||
|
/*
|
||||||
|
Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
||||||
|
|
||||||
|
This file is part of Savanni's AVR library.
|
||||||
|
|
||||||
|
This AVR library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This AVR library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License along with this AVR library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
#ifndef __PACKET_H__
|
#ifndef __PACKET_H__
|
||||||
#define __PACKET_H__
|
#define __PACKET_H__
|
||||||
|
|
||||||
#ifdef __AVR__
|
|
||||||
|
|
||||||
#else
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
packet_type_data,
|
packet_type_data,
|
||||||
|
|
|
@ -11,14 +11,13 @@ You should have received a copy of the GNU General Public License along with thi
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "radio.h"
|
#include "radio.h"
|
||||||
#include "packet.h"
|
|
||||||
#include "packet_buffer.h"
|
#include "packet_buffer.h"
|
||||||
#include <stdlib.h>
|
// #include <stdlib.h>
|
||||||
#include <string.h>
|
// #include <string.h>
|
||||||
|
|
||||||
size_t circular_next(size_t current, size_t max);
|
size_t circular_next(size_t current, size_t max);
|
||||||
|
|
||||||
packet_buffer_t *packet_buffer_new() {
|
packet_buffer_t *packet_buffer_new(void) {
|
||||||
packet_buffer_t *buffer = malloc(sizeof(packet_buffer_t));
|
packet_buffer_t *buffer = malloc(sizeof(packet_buffer_t));
|
||||||
packet_buffer_init(buffer);
|
packet_buffer_init(buffer);
|
||||||
return buffer;
|
return buffer;
|
||||||
|
|
|
@ -28,7 +28,7 @@ typedef struct {
|
||||||
packet_t buffer[5];
|
packet_t buffer[5];
|
||||||
} packet_buffer_t;
|
} packet_buffer_t;
|
||||||
|
|
||||||
packet_buffer_t *packet_buffer_new();
|
packet_buffer_t *packet_buffer_new(void);
|
||||||
void packet_buffer_init(packet_buffer_t *);
|
void packet_buffer_init(packet_buffer_t *);
|
||||||
|
|
||||||
packet_t * packet_buffer_enqueue(packet_buffer_t *, packet_buffer_status_e *);
|
packet_t * packet_buffer_enqueue(packet_buffer_t *, packet_buffer_status_e *);
|
||||||
|
|
|
@ -1,54 +1,68 @@
|
||||||
|
/*
|
||||||
|
Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
||||||
|
|
||||||
|
This file is part of Savanni's AVR library.
|
||||||
|
|
||||||
|
This AVR library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This AVR library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License along with this AVR library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
#include "radio.h"
|
#include "radio.h"
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
|
#include "packet_buffer.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
bool packet_sent;
|
|
||||||
bool packet_ready;
|
|
||||||
} flags_t;
|
|
||||||
|
|
||||||
struct conn_s {
|
struct conn_s {
|
||||||
uint16_t packet_counter;
|
uint16_t packet_counter;
|
||||||
uint8_t address;
|
uint8_t address;
|
||||||
|
|
||||||
// packet_buffer_t outgoing_packets;
|
packet_buffer_t outgoing;
|
||||||
// packet_buffer_t incoming_packets;
|
packet_buffer_t incoming;
|
||||||
|
|
||||||
// packet_t outgoing_buffer[5];
|
void (*set_mode)(radio_t *, radio_mode_e, radio_status_e *);
|
||||||
// size_t outgoing_bottom;
|
flags_t (*get_flags)(radio_t *, radio_status_e *);
|
||||||
// size_t outgoing_top;
|
void (*transmit)(radio_t *, packet_t *, radio_status_e *);
|
||||||
|
void (*receive)(radio_t *, uint8_t data[66], radio_status_e *);
|
||||||
|
|
||||||
// packet_t incoming_buffer[5];
|
radio_t *radio;
|
||||||
// size_t incoming_bottom;
|
|
||||||
// size_t incoming_top;
|
|
||||||
|
|
||||||
void (*set_mode)(void *, radio_mode_e, radio_status_e *);
|
|
||||||
flags_t (*get_flags)(void *, radio_status_e *);
|
|
||||||
void (*transmit)(void *, packet_t *, radio_status_e *);
|
|
||||||
void (*receive)(void *, uint8_t data[66], radio_status_e *);
|
|
||||||
|
|
||||||
void *radio;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
conn_t * conn_connect(radio_t *radio) {
|
||||||
|
conn_t *connection = malloc(sizeof(struct conn_s));
|
||||||
|
connection->packet_counter = 0;
|
||||||
|
connection->address = 0;
|
||||||
|
|
||||||
|
packet_buffer_init(&connection->outgoing);
|
||||||
|
packet_buffer_init(&connection->incoming);
|
||||||
|
|
||||||
|
connection->set_mode = radio->set_mode;
|
||||||
|
connection->get_flags = radio->get_flags;
|
||||||
|
connection->transmit = radio->transmit;
|
||||||
|
connection->receive = radio->receive;
|
||||||
|
connection->radio = radio;
|
||||||
|
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
void conn_set_address(conn_t *self, uint8_t addr, radio_status_e *status) {
|
void conn_set_address(conn_t *self, uint8_t addr, radio_status_e *status) {
|
||||||
/*
|
|
||||||
if (!IS_OK(status)) return;
|
if (!IS_OK(status)) return;
|
||||||
self->address = addr;
|
self->address = addr;
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void conn_set_mode(conn_t *self, radio_mode_e mode, radio_status_e *status) {
|
void conn_set_mode(conn_t *self, radio_mode_e mode, radio_status_e *status) {
|
||||||
/*
|
|
||||||
if (!IS_OK(status)) return;
|
if (!IS_OK(status)) return;
|
||||||
self->set_mode(self->radio, mode, status);
|
self->set_mode(self->radio, mode, status);
|
||||||
*/
|
}
|
||||||
|
|
||||||
|
flags_t conn_get_flags(conn_t *self, radio_status_e *status) {
|
||||||
|
if (!IS_OK(status)) return (flags_t){ .packet_sent = false, .packet_ready = false };
|
||||||
|
return self->get_flags(self->radio, status);
|
||||||
}
|
}
|
||||||
|
|
||||||
void conn_send(conn_t *self, uint8_t dest, msg_t *message, radio_status_e *status) {
|
void conn_send(conn_t *self, uint8_t dest, msg_t *message, radio_status_e *status) {
|
||||||
/*
|
|
||||||
if (!IS_OK(status)) return;
|
if (!IS_OK(status)) return;
|
||||||
|
|
||||||
if (message->length > MAX_MESSAGE_LENGTH) {
|
if (message->length > MAX_MESSAGE_LENGTH) {
|
||||||
|
@ -56,44 +70,63 @@ void conn_send(conn_t *self, uint8_t dest, msg_t *message, radio_status_e *statu
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self->outgoing_top == self->outgoing_bottom) {
|
if (packet_buffer_is_full(&self->outgoing)) {
|
||||||
*status = outgoing_full;
|
*status = outgoing_full;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self->outgoing_buffer[self->outgoing_top].type = data;
|
packet_buffer_status_e buffer_status = packet_buffer_status_ok;
|
||||||
self->outgoing_buffer[self->outgoing_top].count = self->packet_counter;
|
|
||||||
self->outgoing_buffer[self->outgoing_top].sender = self->address;
|
|
||||||
self->outgoing_buffer[self->outgoing_top].receiver = dest;
|
|
||||||
self->outgoing_buffer[self->outgoing_top].size = message->length;
|
|
||||||
memcpy(self->outgoing_buffer[self->outgoing_top].message, message->data, message->length);
|
|
||||||
|
|
||||||
self->transmit(self->radio, &self->outgoing_buffer[self->outgoing_top], status);
|
packet_t *packet = packet_buffer_enqueue(&self->outgoing, &buffer_status);
|
||||||
if (IS_OK(status)) self->outgoing_top++;
|
if (!IS_OK(&buffer_status)) {
|
||||||
|
switch (buffer_status) {
|
||||||
|
case packet_buffer_status_full:
|
||||||
|
*status = outgoing_full;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
*status = unhandled_error;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
packet->type = packet_type_data;
|
||||||
|
packet->count = self->packet_counter;
|
||||||
|
packet->sender = self->address;
|
||||||
|
packet->receiver = dest;
|
||||||
|
packet->length = message->length;
|
||||||
|
memcpy(&packet->message, message->data, message->length);
|
||||||
|
|
||||||
|
self->transmit(self->radio, packet, status);
|
||||||
|
if (!IS_OK(status)) return;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
received_msg_t * conn_receive(conn_t *self, radio_status_e *status) {
|
received_msg_t * conn_receive(conn_t *self, radio_status_e *status) {
|
||||||
/*
|
|
||||||
if (!IS_OK(status)) return NULL;
|
if (!IS_OK(status)) return NULL;
|
||||||
|
|
||||||
if (self->incoming_bottom == self->incoming_top) {
|
if (packet_buffer_is_empty(&self->incoming)) {
|
||||||
|
*status = packet_buffer_status_empty;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t length = self->incoming_buffer[self->incoming_bottom].size;
|
packet_buffer_status_e buffer_status = packet_buffer_status_ok;
|
||||||
received_msg_t *msg = malloc(sizeof(received_msg_t) + length);
|
packet_t *packet = packet_buffer_head(&self->incoming, &buffer_status);
|
||||||
msg->sender = self->incoming_buffer[self->incoming_bottom].sender;
|
if (!IS_OK(&buffer_status)) {
|
||||||
msg->message.length = self->incoming_buffer[self->incoming_bottom].size;
|
// *status =
|
||||||
memcpy(msg->message.data, self->incoming_buffer[self->incoming_bottom].message, msg->message.length);
|
}
|
||||||
|
|
||||||
self->incoming_bottom++;
|
received_msg_t *msg = malloc(sizeof(received_msg_t) + packet->length);
|
||||||
|
msg->sender = packet->sender;
|
||||||
|
msg->message.length = packet->length;
|
||||||
|
memcpy(msg->message.data, packet->message, packet->length);
|
||||||
|
|
||||||
|
packet_buffer_dequeue(&self->incoming, &buffer_status);
|
||||||
|
if (!IS_OK(&buffer_status)) {
|
||||||
|
}
|
||||||
|
|
||||||
return msg;
|
return msg;
|
||||||
*/
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void conn_handle_interrupt(conn_t *self) {
|
void conn_handle_interrupt(conn_t *self) {
|
||||||
|
@ -117,12 +150,13 @@ void conn_handle_interrupt(conn_t *self) {
|
||||||
|
|
||||||
switch (packet->type) {
|
switch (packet->type) {
|
||||||
case packet_type_data:
|
case packet_type_data:
|
||||||
memcpy(self->incoming_buffer[self->incoming_top], packet, packet->size);
|
packet_buffer_status_e status;
|
||||||
self->incoming_top++;
|
packet_t *dest = packet_buffer_enqueue(self->incoming, &status);
|
||||||
|
memcpy(dest, packet, sizeof(packet_t) + packet->length);
|
||||||
break;
|
break;
|
||||||
case packet_type_ack:
|
case packet_type_ack:
|
||||||
uint16_t confirmed = packet->data[0] << 8 + packet->data[1];
|
// uint16_t confirmed = packet->data[0] << 8 + packet->data[1];
|
||||||
while (self->outgoing_buffer[self->outgoing_bottom]->count < confirmed && !is_empty(self->outgoing_buffer)
|
// while (self->outgoing_buffer[self->outgoing_bottom]->count < confirmed && !is_empty(self->outgoing_buffer)
|
||||||
break;
|
break;
|
||||||
case packet_type_retry:
|
case packet_type_retry:
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -14,13 +14,7 @@ You should have received a copy of the GNU General Public License along with thi
|
||||||
#define __RADIO_H__
|
#define __RADIO_H__
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include "packet.h"
|
||||||
#ifdef __AVR__
|
|
||||||
|
|
||||||
#else
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define MAX_MESSAGE_LENGTH 60
|
#define MAX_MESSAGE_LENGTH 60
|
||||||
#define IS_OK(val) (val == NULL || !*val)
|
#define IS_OK(val) (val == NULL || !*val)
|
||||||
|
@ -40,6 +34,7 @@ typedef enum {
|
||||||
not_found,
|
not_found,
|
||||||
message_too_long,
|
message_too_long,
|
||||||
outgoing_full,
|
outgoing_full,
|
||||||
|
unhandled_error,
|
||||||
} radio_status_e;
|
} radio_status_e;
|
||||||
|
|
||||||
typedef struct conn_s conn_t;
|
typedef struct conn_s conn_t;
|
||||||
|
@ -52,8 +47,25 @@ typedef enum {
|
||||||
listen
|
listen
|
||||||
} radio_mode_e;
|
} radio_mode_e;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
bool packet_sent;
|
||||||
|
bool packet_ready;
|
||||||
|
} flags_t;
|
||||||
|
|
||||||
|
typedef struct radio_s radio_t;
|
||||||
|
|
||||||
|
struct radio_s {
|
||||||
|
void (*set_mode)(radio_t *, radio_mode_e, radio_status_e *);
|
||||||
|
flags_t (*get_flags)(radio_t *, radio_status_e *);
|
||||||
|
void (*transmit)(radio_t *, packet_t *, radio_status_e *);
|
||||||
|
void (*receive)(radio_t *, uint8_t data[66], radio_status_e *);
|
||||||
|
};
|
||||||
|
|
||||||
|
conn_t * conn_connect(radio_t *radio);
|
||||||
|
|
||||||
void conn_set_address(conn_t *, uint8_t, radio_status_e *);
|
void conn_set_address(conn_t *, uint8_t, radio_status_e *);
|
||||||
void conn_set_mode(conn_t *, radio_mode_e, radio_status_e *);
|
void conn_set_mode(conn_t *, radio_mode_e, radio_status_e *);
|
||||||
|
flags_t conn_get_flags(conn_t *, radio_status_e *);
|
||||||
|
|
||||||
void conn_send(conn_t *, uint8_t, msg_t *, radio_status_e *);
|
void conn_send(conn_t *, uint8_t, msg_t *, radio_status_e *);
|
||||||
received_msg_t * conn_receive(conn_t *, radio_status_e *);
|
received_msg_t * conn_receive(conn_t *, radio_status_e *);
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
/*
|
||||||
|
Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
||||||
|
|
||||||
|
This file is part of Savanni's AVR library.
|
||||||
|
|
||||||
|
This AVR library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This AVR library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License along with this AVR library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <criterion/criterion.h>
|
||||||
|
#include "radio.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
typedef struct {
|
||||||
|
void (*set_mode)(radio_mock_t *, radio_mode_e, radio_status_e *);
|
||||||
|
flags_t (*get_flags)(radio_mock_t *, radio_status_e *);
|
||||||
|
radio_mock_t (*transmit)(radio_mock_t *, packet_t *, radio_status_e *);
|
||||||
|
void (*receive)(radio_mock_t *, uint8_t data[66], radio_status_e *);
|
||||||
|
} radio_mock_t;
|
||||||
|
*/
|
||||||
|
|
||||||
|
void set_mode(radio_t *radio, radio_mode_e mode, radio_status_e *status) { }
|
||||||
|
|
||||||
|
flags_t get_flags(radio_t *radio, radio_status_e *status) {
|
||||||
|
flags_t flags = { .packet_sent = false, .packet_ready = false };
|
||||||
|
return flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
void transmit(radio_t *radio, packet_t *packet, radio_status_e *status) { }
|
||||||
|
|
||||||
|
void receive(radio_t *radio, uint8_t data[66], radio_status_e *status) { }
|
||||||
|
|
||||||
|
Test(radio, connect) {
|
||||||
|
radio_t radio = (radio_t){ .set_mode = set_mode, .get_flags = get_flags, .transmit = transmit, .receive = receive };
|
||||||
|
conn_t *conn = conn_connect((radio_t *)&radio);
|
||||||
|
|
||||||
|
radio_status_e status = ok;
|
||||||
|
flags_t flags = conn_get_flags(conn, &status);
|
||||||
|
cr_assert(IS_OK(&status));
|
||||||
|
cr_assert(flags.packet_sent == false);
|
||||||
|
cr_assert(flags.packet_ready == false);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue