Compare commits

...

5 Commits

11 changed files with 652 additions and 143 deletions

View File

@ -3,11 +3,11 @@ Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of Savanni's AVR library.
Lumeto 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 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.
Lumeto 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.
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 Lumeto. If not, see <https://www.gnu.org/licenses/>.
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 __BASE_H__

View File

@ -34,7 +34,12 @@
${gcc} ${cflags} ${include_dirs} -c $source
fi
done
cp ${src}/*.h . || true
for source in ${src}/src/*.c; do
if [ -e $source ]; then
${gcc} ${cflags} ${include_dirs} -c $source
fi
done
cp ${src}/*.h ${src}/src/*.h . || true
'';
installPhase = ''
@ -44,15 +49,16 @@
'';
};
mkProgram = { pkgs, gcc, cflags, pname, psrc, pbuildInputs ? [] }:
mkProgram = { pkgs, gcc, cflags, pname, psrc, pbuildInputs ? [], pnativeBuildInputs ? [] }:
pkgs.stdenv.mkDerivation rec {
name = pname;
src = psrc;
buildInputs = pbuildInputs;
nativeBuildInputs = pnativeBuildInputs;
include_dirs = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}/include") buildInputs);
object_files = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/lib/*.o") buildInputs);
include_dirs = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}/include") pbuildInputs);
object_files = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/lib/*.o") pbuildInputs);
dontUnpack = true;
dontConfigure = true;
@ -73,6 +79,35 @@
'';
};
mkTests = { pkgs, gcc, cflags, pname, psrc, pbuildInputs ? [], pnativeBuildInputs ? [] }:
pkgs.stdenv.mkDerivation rec {
name = pname;
src = psrc;
buildInputs = pbuildInputs;
nativeBuildInputs = pnativeBuildInputs;
include_dirs = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}/include") buildInputs);
object_files = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/lib/*.o") buildInputs);
dontUnpack = true;
dontConfigure = true;
buildPhase = ''
for source in ${src}/tests/*.c; do
if [ -e $source ]; then
${gcc} ${cflags} ${include_dirs} -c $source
fi
done
${gcc} -o ${name} ${cflags} ${object_files} *.o
'';
installPhase = ''
mkdir -p $out/bin
cp ${name} $out/bin
'';
};
in rec
{
@ -85,7 +120,8 @@
paths = [
(packages."x86_64-linux"."prime-tx" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; })
(packages."x86_64-linux"."radio-rx" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; })
(packages."x86_64-linux"."packet-radio" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; })
(packages."x86_64-linux"."packet-radio" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = "-Wall -Werror"; })
(packages."x86_64-linux"."packet-radio-tests" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = "-Wall -Werror"; })
];
};
@ -202,6 +238,11 @@
];
};
packages."x86_64-linux"."packet-radio_" =
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
in packages."x86_64-linux"."packet-radio" { gcc = "${pkgs.gcc}/bin/gcc"; cflags = "-Wall -Werror"; };
packages."x86_64-linux"."packet-radio" =
{ gcc, cflags }:
let
@ -210,11 +251,31 @@
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "display";
psrc = ./display;
pname = "packet-radio";
psrc = ./packet-radio;
pbuildInputs = [ ];
};
packages."x86_64-linux"."packet-radio-tests_" =
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
in packages."x86_64-linux"."packet-radio-tests" { gcc = "${pkgs.gcc}/bin/gcc"; cflags = "-Wall -Werror -lcriterion"; };
packages."x86_64-linux"."packet-radio-tests" =
{ gcc, cflags }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
in mkTests {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "packet-radio-tests";
psrc = ./packet-radio;
pbuildInputs = [
(packages."x86_64-linux"."dio" { inherit gcc cflags; })
(packages."x86_64-linux"."shift-register" { inherit gcc cflags; })
(packages."x86_64-linux"."packet-radio" { inherit gcc cflags; })
];
pnativeBuildInputs = [
pkgs.criterion
];
};
@ -266,6 +327,7 @@
avrdude
simavr
gtkwave
criterion
];
in
pkgs.mkShell {

View File

@ -1,113 +0,0 @@
#include "radio.h"
#include <string.h>
#include <stdlib.h>
typedef enum {
data,
ack,
retry,
} packet_type_e;
typedef struct {
bool packet_sent;
bool packet_ready;
} flags_t;
typedef struct {
packet_type_e type;
uint16_t count;
uint8_t sender;
uint8_t receiver;
size_t size;
char message[60];
} packet_t;
struct conn_s {
uint16_t packet_counter;
uint8_t address;
packet_t outgoing_buffer[5];
size_t outgoing_bottom;
size_t outgoing_top;
packet_t incoming_buffer[5];
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;
};
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);
}
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 (self->outgoing_top == self->outgoing_bottom) {
*status = outgoing_full;
return;
}
self->outgoing_buffer[self->outgoing_top].type = data;
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);
if (IS_OK(status)) self->outgoing_top++;
return;
}
received_msg_t * conn_receive(conn_t *self, radio_status_e *status) {
if (!IS_OK(status)) return NULL;
if (self->incoming_bottom == self->incoming_top) {
return NULL;
}
size_t length = self->incoming_buffer[self->incoming_bottom].size;
received_msg_t *msg = malloc(sizeof(received_msg_t) + length);
msg->sender = self->incoming_buffer[self->incoming_bottom].sender;
msg->message.length = self->incoming_buffer[self->incoming_bottom].size;
memcpy(msg->message.data, self->incoming_buffer[self->incoming_bottom].message, msg->message.length);
self->incoming_bottom++;
return msg;
}
void conn_handle_interrupt(conn_t *self) {
radio_status_e *status = ok;
flags_t flags = self->get_flags(self->radio, status);
if (!IS_OK(status)) return;
if (flags.packet_ready) {
uint8_t data[66];
self->receive(self->radio, data, status);
if (!IS_OK(status)) return;
}
}

36
packet-radio/src/packet.h Normal file
View File

@ -0,0 +1,36 @@
/*
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__
#define __PACKET_H__
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
typedef enum {
packet_type_data,
packet_type_ack,
packet_type_retry,
} packet_type_e;
typedef struct {
packet_type_e type;
uint16_t count;
uint8_t sender;
uint8_t receiver;
size_t length;
char message[60];
} packet_t;
#endif

View File

@ -0,0 +1,81 @@
/*
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_buffer.h"
// #include <stdlib.h>
// #include <string.h>
size_t circular_next(size_t current, size_t max);
packet_buffer_t *packet_buffer_new(void) {
packet_buffer_t *buffer = malloc(sizeof(packet_buffer_t));
packet_buffer_init(buffer);
return buffer;
}
void packet_buffer_init(packet_buffer_t *buffer) {
buffer->count = 0;
buffer->bottom = 0;
buffer->top = 0;
}
packet_t *packet_buffer_enqueue(packet_buffer_t *self, packet_buffer_status_e *status) {
if (!IS_OK(status)) return NULL;
if (packet_buffer_is_full(self)) {
*status = packet_buffer_status_full;
return NULL;
}
packet_t *ptr = &self->buffer[self->top];
self->top = circular_next(self->top, 5);
self->count++;
return ptr;
}
packet_t *packet_buffer_head(packet_buffer_t *self, packet_buffer_status_e *status) {
if (!IS_OK(status)) return NULL;
if (packet_buffer_is_empty(self)) {
*status = packet_buffer_status_empty;
return NULL;
}
return &self->buffer[self->bottom];
}
void packet_buffer_dequeue(packet_buffer_t *self, packet_buffer_status_e *status) {
if (!IS_OK(status)) return;
if (packet_buffer_is_empty(self)) {
*status = packet_buffer_status_empty;
return;
}
self->bottom = circular_next(self->bottom, 5);
self->count--;
}
bool packet_buffer_is_full(packet_buffer_t *self) {
return self->count == 5;
}
bool packet_buffer_is_empty(packet_buffer_t *self) {
return self->count == 0;
}
size_t circular_next(size_t current, size_t max) {
current++;
return (current < max) ? current : 0;
}

View File

@ -0,0 +1,41 @@
/*
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_BUFFER_H__
#define __PACKET_BUFFER_H__
#include "packet.h"
typedef enum {
packet_buffer_status_ok,
packet_buffer_status_empty,
packet_buffer_status_full,
} packet_buffer_status_e;
typedef struct {
size_t count;
size_t bottom;
size_t top;
packet_t buffer[5];
} packet_buffer_t;
packet_buffer_t *packet_buffer_new(void);
void packet_buffer_init(packet_buffer_t *);
packet_t * packet_buffer_enqueue(packet_buffer_t *, packet_buffer_status_e *);
packet_t * packet_buffer_head(packet_buffer_t *, packet_buffer_status_e *);
void packet_buffer_dequeue(packet_buffer_t *, packet_buffer_status_e *);
bool packet_buffer_is_full(packet_buffer_t *);
bool packet_buffer_is_empty(packet_buffer_t *);
#endif

175
packet-radio/src/radio.c Normal file
View File

@ -0,0 +1,175 @@
/*
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?
*/
}

View File

@ -14,13 +14,7 @@ You should have received a copy of the GNU General Public License along with thi
#define __RADIO_H__
#include <stddef.h>
#ifdef __AVR__
#else
#include <stdint.h>
#include <stdbool.h>
#endif
#include "packet.h"
#define MAX_MESSAGE_LENGTH 60
#define IS_OK(val) (val == NULL || !*val)
@ -40,6 +34,7 @@ typedef enum {
not_found,
message_too_long,
outgoing_full,
unhandled_error,
} radio_status_e;
typedef struct conn_s conn_t;
@ -52,8 +47,25 @@ typedef enum {
listen
} 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_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 *);
received_msg_t * conn_receive(conn_t *, radio_status_e *);

View File

@ -0,0 +1,180 @@
/*
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 <stdio.h>
#include "packet_buffer.h"
Test(packet_buffer, empty_buffer) {
packet_buffer_t *buffer = packet_buffer_new();
cr_assert(packet_buffer_is_empty(buffer));
cr_assert(!packet_buffer_is_full(buffer));
}
Test(packet_buffer, full_buffer) {
packet_buffer_status_e status = packet_buffer_status_ok;
packet_buffer_t *buffer = packet_buffer_new();
packet_t *packet;
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(packet != NULL);
cr_assert(status == packet_buffer_status_ok);
cr_assert(!packet_buffer_is_empty(buffer));
cr_assert(!packet_buffer_is_full(buffer));
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(packet != NULL);
cr_assert(status == packet_buffer_status_ok);
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(packet != NULL);
cr_assert(status == packet_buffer_status_ok);
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(packet != NULL);
cr_assert(status == packet_buffer_status_ok);
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(packet != NULL);
cr_assert(status == packet_buffer_status_ok);
cr_assert(packet_buffer_is_full(buffer));
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(packet == NULL);
cr_assert(status == packet_buffer_status_full);
cr_assert(packet_buffer_is_full(buffer));
}
Test(packet_buffer, basic_enqueue_and_dequeue) {
packet_buffer_status_e status = packet_buffer_status_ok;
packet_buffer_t *buffer = packet_buffer_new();
packet_t *packet;
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
packet->type = packet_type_data;
packet->count = 0;
packet->sender = 15;
packet->receiver = 16;
packet->length = 2;
memcpy(packet->message, (char [2]){ 25, 24 }, 2);
packet = packet_buffer_head(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
cr_assert(packet->type == packet_type_data);
cr_assert(packet->count == 0);
cr_assert(packet->sender == 15);
cr_assert(packet->receiver == 16);
cr_assert(packet->length == 2);
cr_assert(packet->message[0] == 25);
cr_assert(packet->message[1] == 24);
packet_buffer_dequeue(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
}
Test(packet_buffer, enqueue_to_full) {
packet_buffer_status_e status = packet_buffer_status_ok;
packet_buffer_t *buffer = packet_buffer_new();
packet_t *packet;
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
packet->type = packet_type_data;
packet->count = 0;
packet->sender = 15;
packet->receiver = 16;
packet->length = 2;
memcpy(packet->message, (char [2]){ 25, 24 }, 2);
packet = packet_buffer_enqueue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
packet = packet_buffer_head(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
cr_assert(packet->type == packet_type_data);
cr_assert(packet->count == 0);
cr_assert(packet->sender == 15);
cr_assert(packet->receiver == 16);
cr_assert(packet->length == 2);
cr_assert(packet->message[0] == 25);
cr_assert(packet->message[1] == 24);
packet_buffer_dequeue(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
}
Test(packet_buffer, circular_buffer) {
packet_buffer_status_e status = packet_buffer_status_ok;
packet_buffer_t *buffer = packet_buffer_new();
packet_t *packet;
packet = packet_buffer_enqueue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
packet->type = packet_type_data;
packet->count = 0;
packet->sender = 15;
packet->receiver = 16;
packet->length = 2;
memcpy(packet->message, (char [2]){ 25, 24 }, 2);
packet_buffer_dequeue(buffer, &status);
packet_buffer_dequeue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
packet->type = packet_type_data;
packet->count = 1;
packet->sender = 15;
packet->receiver = 16;
packet->length = 2;
memcpy(packet->message, (char [2]){ 25, 24 }, 2);
packet = packet_buffer_head(buffer, &status);
printf("packet-type: %d\n", packet->type);
cr_assert(status == packet_buffer_status_ok);
cr_assert(packet->type == packet_type_data);
cr_assert(packet->count == 0);
cr_assert(packet->sender == 15);
cr_assert(packet->receiver == 16);
cr_assert(packet->length == 2);
cr_assert(packet->message[0] == 25);
cr_assert(packet->message[1] == 24);
packet_buffer_dequeue(buffer, &status);
packet_buffer_dequeue(buffer, &status);
packet_buffer_dequeue(buffer, &status);
packet_buffer_dequeue(buffer, &status);
packet = packet_buffer_head(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
cr_assert(packet->type == packet_type_data);
cr_assert(packet->count == 1);
cr_assert(packet->sender == 15);
cr_assert(packet->receiver == 16);
cr_assert(packet->length == 2);
cr_assert(packet->message[0] == 25);
cr_assert(packet->message[1] == 24);
packet_buffer_dequeue(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
cr_assert(packet_buffer_is_empty(buffer));
}

View File

@ -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);
}

View File

@ -327,19 +327,8 @@ interrupt_flags_t rfm_interrupts(rfm_t *rfm) {
}
uint8_t rfm_rssi(rfm_t *rfm) {
// uint8_t rssi_reg;
uint8_t rssi_value;
/*
_rfm_write(rfm, REG_RSSI_CONFIG, (uint8_t [1]){ _BV(0) }, 1);
while(!(rssi_reg & _BV(1))) {
_rfm_read(rfm, REG_RSSI_CONFIG, &rssi_reg, 1);
}
*/
_rfm_read(rfm, REG_RSSI_VALUE, &rssi_value, 1);
return rssi_value;
}