avr/packet-radio/src/radio.c

176 lines
5.7 KiB
C

/*
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 "packet.h"
#include "packet_buffer.h"
#include <string.h>
struct conn_s {
uint16_t packet_counter;
uint8_t address;
packet_buffer_t outgoing;
packet_buffer_t incoming;
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 *);
radio_t *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) {
if (!IS_OK(status)) return;
self->address = addr;
}
void conn_set_mode(conn_t *self, radio_mode_e mode, radio_status_e *status) {
if (!IS_OK(status)) return;
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) {
if (!IS_OK(status)) return;
if (message->length > MAX_MESSAGE_LENGTH) {
*status = message_too_long;
return;
}
if (packet_buffer_is_full(&self->outgoing)) {
*status = outgoing_full;
return;
}
packet_buffer_status_e buffer_status = packet_buffer_status_ok;
packet_t *packet = packet_buffer_enqueue(&self->outgoing, &buffer_status);
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;
}
received_msg_t * conn_receive(conn_t *self, radio_status_e *status) {
if (!IS_OK(status)) return NULL;
if (packet_buffer_is_empty(&self->incoming)) {
*status = packet_buffer_status_empty;
return NULL;
}
packet_buffer_status_e buffer_status = packet_buffer_status_ok;
packet_t *packet = packet_buffer_head(&self->incoming, &buffer_status);
if (!IS_OK(&buffer_status)) {
// *status =
}
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;
}
void conn_handle_interrupt(conn_t *self) {
/*
radio_status_e *status = ok;
flags_t flags = self->get_flags(self->radio, status);
// if this is failing, we should raise a flag that indicates that the radio itself is in trouble
if (!IS_OK(status)) return;
if (flags.packet_ready) {
uint8_t data[66];
self->receive(self->radio, data, status);
// if this fails, increment an error counter
if (!IS_OK(status)) return;
packet_t *packet = (packet_t *)data;
if (packet->receiver != self->address) {
return;
}
switch (packet->type) {
case packet_type_data:
packet_buffer_status_e status;
packet_t *dest = packet_buffer_enqueue(self->incoming, &status);
memcpy(dest, packet, sizeof(packet_t) + packet->length);
break;
case packet_type_ack:
// uint16_t confirmed = packet->data[0] << 8 + packet->data[1];
// while (self->outgoing_buffer[self->outgoing_bottom]->count < confirmed && !is_empty(self->outgoing_buffer)
break;
case packet_type_retry:
break;
}
// process the packet type
// break;
}
// timeout processing
// if (time > receiving_timeout) { }
// if we were receiving data, a receiving_timeout must have been set. At this point, we're not receiving any more data, so it's time to send a confirmation.
// if (time > confirmation_timeout) { }
// if we were sending data, a confirmation timeout must have been set. But I don't recall what this is good for? Do we want to retransmit everything?
*/
}