Refactor mesh stuff

This commit is contained in:
Stefan `Sec` Zehl 2011-07-30 16:10:30 +02:00
parent 1077b5fd1e
commit 1a23641c8d
4 changed files with 250 additions and 184 deletions

View File

@ -10,39 +10,12 @@
#include "funk/nrf24l01p.h" #include "funk/nrf24l01p.h"
#include "funk/mesh.h"
#include <string.h> #include <string.h>
#define MESH_CHANNEL 85
#define MESH_MAC "MESHB"
/**************************************************************************/ /**************************************************************************/
uint32_t const meshkey[4] = {
0x00000000, 0x00000000, 0x00000000, 0x00000000
};
#define MESHBUFSIZE 10
#define MESHPKTSIZE 32
typedef struct {
uint8_t pkt[32];
char flags;
} MPKT;
MPKT meshbuffer[MESHBUFSIZE];
#define MF_FREE (0)
#define MF_USED (1<<0)
time_t _timet=0;
// Timezones suck. Currently we only do it all in localtime.
// I know it's broken. Sorry
time_t getSeconds(void){
return _timet+(getTimer()*SYSTICKSPEED/1000);
};
// **********************************************************************
void m_init(void){ void m_init(void){
nrf_init(); nrf_init();
@ -56,169 +29,16 @@ void m_init(void){
nrf_config_set(&config); nrf_config_set(&config);
for(int i=0;i<MESHBUFSIZE;i++){ initMesh();
meshbuffer[i].flags=MF_FREE;
};
memset(meshbuffer[0].pkt,0,MESHPKTSIZE);
meshbuffer[0].pkt[0]='T';
uint32touint8p(getSeconds(),meshbuffer[0].pkt+2);
meshbuffer[0].flags=MF_USED;
}; };
void m_tset(void){ void m_tset(void){
_timet=1311961112; _timet=1311961112;
}; };
void m_cleanup(void){
time_t now=getSeconds();
for(int i=1;i<MESHBUFSIZE;i++){
if(meshbuffer[i].flags&MF_USED){
if (uint8ptouint32(meshbuffer[i].pkt+2)<now){
meshbuffer[i].flags=MF_FREE;
};
};
};
};
#define YEAR0 1900 /* the first year */
#define EPOCH_YR 1970 /* EPOCH = Jan 1 1970 00:00:00 */
#define SECS_DAY (24L * 60L * 60L)
#define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
#define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
int _ytab[2][12] = {
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
struct tm * mygmtime(register const time_t time) {
static struct tm br_time;
register struct tm *timep = &br_time;
register unsigned long dayclock, dayno;
int year = EPOCH_YR;
dayclock = (unsigned long)time % SECS_DAY;
dayno = (unsigned long)time / SECS_DAY;
timep->tm_sec = dayclock % 60;
timep->tm_min = (dayclock % 3600) / 60;
timep->tm_hour = dayclock / 3600;
timep->tm_wday = (dayno + 4) % 7; /* day 0 was a thursday */
while (dayno >= YEARSIZE(year)) {
dayno -= YEARSIZE(year);
year++;
}
timep->tm_year = year - YEAR0;
timep->tm_yday = dayno;
timep->tm_mon = 0;
while (dayno >= _ytab[LEAPYEAR(year)][timep->tm_mon]) {
dayno -= _ytab[LEAPYEAR(year)][timep->tm_mon];
timep->tm_mon++;
}
timep->tm_mday = dayno + 1;
timep->tm_isdst = 0;
return timep;
}
#define M_SENDINT 500
#define M_RECVINT 1000
#define M_RECVTIM 100
void m_recv(void){
__attribute__ ((aligned (4))) uint8_t buf[32];
int len;
int recvend=M_RECVTIM/SYSTICKSPEED+getTimer();
static int toggle=0;
gpioSetValue (RB_LED2, toggle);
toggle=1-toggle;
m_cleanup();
nrf_rcv_pkt_start();
do{
len=nrf_rcv_pkt_poll_dec(sizeof(buf),buf,meshkey);
// Receive
if(len<=0){
delayms_power(10);
continue;
};
int i;
for(i=0;i<MESHBUFSIZE;i++){
if((meshbuffer[i].flags&MF_USED)==0)
break;
};
if(i==MESHBUFSIZE){ // Buffer full. Ah well. Kill a random packet
i=1; // XXX: GetRandom()?
};
memcpy(meshbuffer[i].pkt,buf,MESHPKTSIZE);
meshbuffer[i].flags=MF_USED;
if(buf[0]=='T'){
gpioSetValue (RB_LED1, 0);
time_t toff=uint8ptouint32(buf+2)-(getTimer()*SYSTICKSPEED/1000);
if(toff>_timet) // Do not live in the past.
_timet = toff;
}else if (buf[0]>='A' && buf[0] <'T'){ // Truncate ascii packets.
meshbuffer[i].pkt[MESHPKTSIZE-3]=0;
};
}while(getTimer()<recvend);
nrf_rcv_pkt_end();
}
void m_send(void){
int ctr=0;
__attribute__ ((aligned (4))) uint8_t buf[32];
int status;
// Update [T]ime packet
uint32touint8p(getSeconds(),meshbuffer[0].pkt+2);
for (int i=0;i<MESHBUFSIZE;i++){
if(!meshbuffer[i].flags&MF_USED)
continue;
ctr++;
memcpy(buf,meshbuffer[i].pkt,MESHPKTSIZE);
status=nrf_snd_pkt_crc_encr(MESHPKTSIZE,buf,meshkey);
//Check status? But what would we do...
};
};
void m_info(void){
char ctr=0;
getInputWaitRelease();
lcdClear();
for (int i=0;i<MESHBUFSIZE;i++){
if(!meshbuffer[i].flags&MF_USED)
continue;
ctr++;
};
lcdPrint("MeshQ:");
lcdPrintInt(ctr);
lcdNl();
lcdDisplay();
};
void tick_mesh(void){
static int ctr=0;
ctr++;
if((ctr % (M_RECVINT/SYSTICKSPEED))==0){
push_queue(&m_recv);
};
if((ctr % (M_SENDINT/SYSTICKSPEED))==0){
push_queue(&m_send);
};
};
void m_time(void){ void m_time(void){
struct tm* tm; struct tm* tm;
char c[2]={0,0};
getInputWaitRelease(); getInputWaitRelease();
delayms(100); delayms(100);
do{ do{
@ -236,7 +56,51 @@ void m_time(void){
lcdPrint("."); lcdPrint(".");
lcdPrintInt(tm->tm_year+YEAR0); lcdPrintInt(tm->tm_year+YEAR0);
lcdNl(); lcdNl();
lcdNl();
lcdPrint("<");
for(int i=0;i<MESHBUFSIZE;i++){
if(!meshbuffer[i].flags&MF_USED){
c[0]='_';
}else{
c[0]=meshbuffer[i].pkt[0];
};
lcdPrint(c);
};
lcdPrintln(">");
lcdPrint("Gen:");
lcdPrintInt(meshgen);
lcdNl();
lcdRefresh(); lcdRefresh();
delayms_queue(50); delayms_queue(50);
}while ((getInputRaw())==BTN_NONE); }while ((getInputRaw())==BTN_NONE);
}; };
inline void blink(char a, char b){
gpioSetValue (a,b, 1-gpioGetValue(a,b));
};
/***********************************************************************/
void m_cleanup(void){
mesh_cleanup();
};
void m_recv(void){
mesh_recvloop();
}
void m_send(void){
mesh_sendloop();
};
void tick_mesh(void){
mesh_systick();
};

View File

@ -8,6 +8,7 @@ OBJS += nrf24l01p.o
OBJS += rftransfer.o OBJS += rftransfer.o
OBJS += filetransfer.o OBJS += filetransfer.o
OBJS += openbeacon.o OBJS += openbeacon.o
OBJS += mesh.o
LIBNAME=funk LIBNAME=funk

163
firmware/funk/mesh.c Normal file
View File

@ -0,0 +1,163 @@
#include <sysinit.h>
#include <string.h>
#include "basic/basic.h"
#include "funk/mesh.h"
#include "funk/nrf24l01p.h"
#include "basic/byteorder.h"
#include "basic/random.h"
char meshgen=0; // Generation
MPKT meshbuffer[MESHBUFSIZE];
uint32_t const meshkey[4] = {
0x00000000, 0x00000000, 0x00000000, 0x00000000
};
struct NRF_CFG oldconfig;
void initMesh(void){
for(int i=0;i<MESHBUFSIZE;i++){
meshbuffer[i].flags=MF_FREE;
};
memset(meshbuffer[0].pkt,0,MESHPKTSIZE);
meshbuffer[0].pkt[0]='T';
MO_TIME_set(meshbuffer[0].pkt,getSeconds());
meshbuffer[0].flags=MF_USED;
};
inline void blink(char a, char b){
gpioSetValue (a,b, 1-gpioGetValue(a,b));
};
void mesh_cleanup(void){
time_t now=getSeconds();
for(int i=1;i<MESHBUFSIZE;i++){
if(meshbuffer[i].flags&MF_USED){
if (MO_GEN(meshbuffer[i].pkt)<meshgen)
meshbuffer[i].flags=MF_FREE;
if (MO_TIME(meshbuffer[i].pkt)<now)
meshbuffer[i].flags=MF_FREE;
if (MO_TIME(meshbuffer[i].pkt)-now>SECS_DAY)
meshbuffer[i].flags=MF_FREE;
};
};
};
void mesh_recvloop(void){
__attribute__ ((aligned (4))) uint8_t buf[32];
int len;
int recvend=M_RECVTIM/SYSTICKSPEED+getTimer();
int pktctr=0;
nrf_config_get(&oldconfig);
nrf_set_channel(MESH_CHANNEL);
nrf_set_rx_mac(0,MESHPKTSIZE,strlen(MESH_MAC),(uint8_t*)MESH_MAC);
mesh_cleanup();
nrf_rcv_pkt_start();
do{
len=nrf_rcv_pkt_poll_dec(sizeof(buf),buf,meshkey);
// Receive
if(len<=0){
delayms_power(10);
continue;
};
pktctr++;
if(MO_GEN(buf)>meshgen){
meshgen++;
_timet=0;
continue;
};
if(MO_TYPE(buf)=='T'){
time_t toff=MO_TIME(buf)-(getTimer()*SYSTICKSPEED/1000);
if (toff>_timet) // Do not live in the past.
_timet = toff;
continue;
};
// Safety: Truncate ascii packets by 0-ing the CRC
if (MO_TYPE(buf) >='A' && MO_TYPE(buf) <='Z'){
buf[MESHPKTSIZE-2]=0;
};
// Store packet in a free slot
int free=-1;
for(int i=0;i<MESHBUFSIZE;i++){
if ( ((meshbuffer[i].flags&MF_USED)==0) && free<0 )
free=i;
if ( (meshbuffer[i].flags&MF_USED) &&
(MO_TYPE(meshbuffer[i].pkt) == MO_TYPE(buf))){
if ( MO_TIME(buf) >
MO_TIME(meshbuffer[i].pkt)){
free=i;
break;
}else{
free=-2;
break;
};
};
};
if(free==-1){ // Buffer full. Ah well. Kill a random packet
free=1; // XXX: GetRandom()?
};
if(free<0)
continue;
memcpy(meshbuffer[free].pkt,buf,MESHPKTSIZE);
meshbuffer[free].flags=MF_USED;
}while(getTimer()<recvend || pktctr>MESHBUFSIZE);
nrf_rcv_pkt_end();
nrf_config_set(&oldconfig);
}
void mesh_sendloop(void){
int ctr=0;
__attribute__ ((aligned (4))) uint8_t buf[32];
int status;
nrf_config_get(&oldconfig);
nrf_set_channel(MESH_CHANNEL);
nrf_set_tx_mac(strlen(MESH_MAC),(uint8_t*)MESH_MAC);
// Update [T]ime packet
MO_TIME_set(meshbuffer[0].pkt,getSeconds());
MO_GEN_set(meshbuffer[0].pkt,meshgen);
for (int i=0;i<MESHBUFSIZE;i++){
if(!meshbuffer[i].flags&MF_USED)
continue;
ctr++;
memcpy(buf,meshbuffer[i].pkt,MESHPKTSIZE);
status=nrf_snd_pkt_crc_encr(MESHPKTSIZE,buf,meshkey);
//Check status? But what would we do...
};
nrf_config_set(&oldconfig);
};
void mesh_systick(void){
static int rcvctr=0;
static int sendctr=0;
if(rcvctr--<0){
push_queue(&mesh_recvloop);
rcvctr=M_RECVINT/SYSTICKSPEED/2;
rcvctr+=getRandom()%(rcvctr*2);
};
if(sendctr--<0){
push_queue(&mesh_sendloop);
sendctr=M_SENDINT/SYSTICKSPEED/2;
sendctr+=getRandom()%(sendctr*2);
};
};

38
firmware/funk/mesh.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef __MESH_H_
#define __MESH_H_
#define MESHBUFSIZE 10
#define MESHPKTSIZE 32
#define M_SENDINT 500
#define M_RECVINT 1000
#define M_RECVTIM 100
#define MESH_CHANNEL 85
#define MESH_MAC "MESHB"
#define MO_TYPE(x) (x[0])
#define MO_TYPE_set(x,y) (x[0]=y)
#define MO_GEN(x) (x[1])
#define MO_GEN_set(x,y) (x[1]=y)
#define MO_TIME(x) (uint8ptouint32(x+2))
#define MO_TIME_set(x,y) (uint32touint8p(y,x+2))
typedef struct {
uint8_t pkt[32];
char flags;
} MPKT;
#define MF_FREE (0)
#define MF_USED (1<<0)
extern char meshgen; // Generation
extern MPKT meshbuffer[MESHBUFSIZE];
void initMesh(void);
void mesh_cleanup(void);
void mesh_recvloop(void);
void mesh_sendloop(void);
void mesh_systick(void);
#endif