Compare commits
6 Commits
main
...
radio-abst
Author | SHA1 | Date |
---|---|---|
Savanni D'Gerinel | c1b3891a29 | |
Savanni D'Gerinel | ce78c28367 | |
Savanni D'Gerinel | c28d050037 | |
Savanni D'Gerinel | bc09a7e411 | |
Savanni D'Gerinel | 4aa9598e00 | |
Savanni D'Gerinel | 38109b55e3 |
|
@ -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__
|
||||
|
|
100
flake.nix
100
flake.nix
|
@ -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,22 +120,11 @@
|
|||
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 = "-Wall -Werror"; })
|
||||
(packages."x86_64-linux"."packet-radio-tests" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = "-Wall -Werror"; })
|
||||
];
|
||||
};
|
||||
|
||||
/* packages."x86_64-linux"."dio-attiny85" = (packages."x86_64-linux"."dio" attiny85); */
|
||||
|
||||
/*
|
||||
packages."x86_64-linux"."dio" =
|
||||
let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
in mkLibrary {
|
||||
mkDerivation = pkgs.stdenv.mkDerivation;
|
||||
gcc =
|
||||
cflags =
|
||||
name =
|
||||
*/
|
||||
|
||||
packages."x86_64-linux"."dio" =
|
||||
{ gcc, cflags }:
|
||||
let
|
||||
|
@ -214,6 +238,47 @@
|
|||
];
|
||||
};
|
||||
|
||||
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
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
in mkLibrary {
|
||||
pkgs = pkgs;
|
||||
gcc = gcc;
|
||||
cflags = cflags;
|
||||
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"."packet-radio" { inherit gcc cflags; })
|
||||
];
|
||||
pnativeBuildInputs = [
|
||||
pkgs.criterion
|
||||
];
|
||||
};
|
||||
|
||||
packages."x86_64-linux"."prime-tx" =
|
||||
{ gcc, cflags }:
|
||||
let
|
||||
|
@ -262,6 +327,7 @@
|
|||
avrdude
|
||||
simavr
|
||||
gtkwave
|
||||
criterion
|
||||
];
|
||||
in
|
||||
pkgs.mkShell {
|
||||
|
|
|
@ -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
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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
|
|
@ -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?
|
||||
*/
|
||||
}
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
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 __RADIO_H__
|
||||
#define __RADIO_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include "packet.h"
|
||||
|
||||
#define MAX_MESSAGE_LENGTH 60
|
||||
#define IS_OK(val) (val == NULL || !*val)
|
||||
|
||||
typedef struct {
|
||||
size_t length;
|
||||
char data[0];
|
||||
} msg_t;
|
||||
|
||||
typedef struct {
|
||||
uint16_t sender;
|
||||
msg_t message;
|
||||
} received_msg_t;
|
||||
|
||||
typedef enum {
|
||||
ok,
|
||||
not_found,
|
||||
message_too_long,
|
||||
outgoing_full,
|
||||
unhandled_error,
|
||||
} radio_status_e;
|
||||
|
||||
typedef struct conn_s conn_t;
|
||||
|
||||
typedef enum {
|
||||
sleep,
|
||||
standby,
|
||||
tx,
|
||||
rx,
|
||||
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 *);
|
||||
|
||||
// void radio_broadcast(conn_t *, uint16_t, msg_t *, radio_status_e *);
|
||||
|
||||
#endif
|
|
@ -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));
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue