crashtest-r0ket/firmware/funk/openbeacon.c

157 lines
3.2 KiB
C
Raw Normal View History

2011-07-17 15:42:19 +00:00
#include <stdint.h>
#include <string.h>
2011-07-17 15:42:19 +00:00
#include "funk/openbeacon.h"
#include "funk/nrf24l01p.h"
2011-07-17 12:25:15 +00:00
#include "basic/byteorder.h"
2011-07-17 15:42:19 +00:00
#include "sysdefs.h"
2011-07-17 18:32:17 +00:00
#include "filesystem/ff.h"
2011-07-23 17:17:14 +00:00
#include "basic/uuid.h"
#include "basic/config.h"
#include "basic/random.h"
2011-07-17 12:25:15 +00:00
#include "SECRETS"
2011-12-18 00:34:06 +00:00
2011-12-20 01:43:11 +00:00
#define SAVE_OPENBEACON 1
2011-07-17 19:01:09 +00:00
const uint8_t mac[5] = {1,2,3,2,1};
2011-07-17 18:32:17 +00:00
volatile uint32_t oid = 0;
volatile uint32_t seq = 0;
volatile uint8_t strength = 0;
2011-12-20 01:43:11 +00:00
#if SAVE_OPENBEACON
2011-08-05 20:55:46 +00:00
static void openbeaconSave(uint32_t s);
2011-12-18 00:34:06 +00:00
#endif
2011-07-17 12:25:15 +00:00
2011-08-06 00:41:40 +00:00
static struct NRF_CFG oldconfig;
2011-12-20 01:43:11 +00:00
#if SAVE_OPENBEACON
2011-08-05 20:55:46 +00:00
static void openbeaconShutdown(void)
{
openbeaconSave(seq);
}
2011-08-05 20:55:46 +00:00
static void openbeaconSaveBlock(void)
{
openbeaconSave(seq + OPENBEACON_SAVE + 1);
}
2011-08-05 20:55:46 +00:00
static void openbeaconSave(uint32_t s)
2011-07-17 12:25:15 +00:00
{
2011-07-17 19:01:09 +00:00
FIL file;
2011-07-17 18:32:17 +00:00
BYTE buf[4];
UINT readbytes;
2011-08-05 21:35:25 +00:00
if( f_open(&file, "beacon.cfg", FA_OPEN_ALWAYS|FA_WRITE) )
2011-07-17 18:32:17 +00:00
return;
uint32touint8p(s, buf);
2011-07-17 18:32:17 +00:00
if( f_write(&file, buf, 4, &readbytes) )
return;
f_close(&file);
2011-07-17 15:42:19 +00:00
}
2011-08-05 20:55:46 +00:00
static void openbeaconRead()
2011-07-17 15:42:19 +00:00
{
2011-07-17 19:01:09 +00:00
FIL file;
2011-07-17 18:32:17 +00:00
BYTE buf[4];
UINT readbytes;
2011-08-05 21:35:25 +00:00
if( f_open(&file, "beacon.cfg", FA_OPEN_EXISTING|FA_READ) )
2011-07-17 18:32:17 +00:00
return;
if( f_read(&file, buf, 4, &readbytes) )
return;
2011-07-17 23:16:44 +00:00
seq = uint8ptouint32(buf);
f_close(&file);
2011-07-17 15:42:19 +00:00
}
2011-12-18 00:34:06 +00:00
#endif
2011-07-17 15:42:19 +00:00
2011-07-23 17:17:14 +00:00
void openbeaconSetup(void)
2011-07-17 15:42:19 +00:00
{
2011-07-23 17:17:14 +00:00
oid = GetUUID32();
2011-07-17 12:25:15 +00:00
strength = 0;
2011-12-20 01:43:11 +00:00
#if SAVE_OPENBEACON
2011-07-17 15:42:19 +00:00
openbeaconRead();
openbeaconSaveBlock();
2011-12-18 00:34:06 +00:00
#endif
2011-07-17 12:25:15 +00:00
}
static void openbeaconSendPacket(uint32_t id, uint32_t seq,
2011-07-17 23:16:44 +00:00
uint8_t flags, uint8_t strength)
2011-07-17 12:25:15 +00:00
{
uint8_t buf[16];
uint8_t proto = 0x17; //Tracking
2011-07-17 12:25:15 +00:00
2011-12-21 18:54:31 +00:00
volatile uint16_t i;
i = (getRandom()&0xfff)+1;
2011-12-21 18:52:30 +00:00
while(i--);
2011-12-22 14:05:22 +00:00
static uint32_t n = 123;
if( --n == 0 ){
n = 123;
proto = 0x23; //Nick name
}
2011-07-17 12:25:15 +00:00
buf[0]=0x10; // Length: 16 bytes
buf[1]=proto;
if( proto == 0x17 ){
buf[2]=flags;
buf[3]=strength*85; // Send intensity
2011-07-17 12:25:15 +00:00
uint32touint8p(seq, buf+4);
uint32touint8p(id, buf+8);
2011-07-17 12:25:15 +00:00
buf[12]=0xff; // salt (0xffff always?)
buf[13]=0xff;
#if ENCRYPT_OPENBEACON
nrf_snd_pkt_crc_encr(16,buf,openbeaconkey);
#else
nrf_snd_pkt_crc_encr(16,buf,NULL);
#endif
}else{
2011-12-22 14:05:22 +00:00
if( strlen(GLOBAL(nickname)) > 8 )
buf[1] = 0x24;
nrf_set_strength(3);
uint32touint8p(id, buf+2);
memcpy(buf+6, GLOBAL(nickname), 8);
#if ENCRYPT_OPENBEACON
nrf_snd_pkt_crc_encr(16,buf,openbeaconkey);
#else
nrf_snd_pkt_crc_encr(16,buf,NULL);
#endif
if( strlen(GLOBAL(nickname)) < 9 )
return;
2011-12-22 14:05:22 +00:00
buf[1]=0x25;
memcpy(buf+6, GLOBAL(nickname)+8, 8);
2011-12-18 00:34:06 +00:00
#if ENCRYPT_OPENBEACON
nrf_snd_pkt_crc_encr(16,buf,openbeaconkey);
2011-12-18 00:34:06 +00:00
#else
nrf_snd_pkt_crc_encr(16,buf,NULL);
2011-12-18 00:34:06 +00:00
#endif
}
2011-07-17 12:25:15 +00:00
}
2011-12-21 18:52:30 +00:00
void openbeaconSend(void)
2011-07-17 12:25:15 +00:00
{
2011-08-06 00:41:40 +00:00
nrf_config_get(&oldconfig);
2011-08-02 22:24:31 +00:00
nrf_set_channel(OPENBEACON_CHANNEL);
2011-07-17 18:32:17 +00:00
nrf_set_strength(strength);
2011-07-17 19:01:09 +00:00
nrf_set_tx_mac(sizeof(mac), mac);
2011-12-21 18:52:30 +00:00
openbeaconSendPacket(oid, seq++, 0xFF, strength++);
2011-07-17 12:25:15 +00:00
if( strength == 4 )
strength = 0;
2011-12-20 01:43:11 +00:00
#if SAVE_OPENBEACON
if( (seq & OPENBEACON_SAVE) == OPENBEACON_SAVE )
2011-12-21 18:52:30 +00:00
openbeaconSaveBlock();
2011-12-18 00:34:06 +00:00
#endif
2011-08-06 00:41:40 +00:00
nrf_config_set(&oldconfig);
nrf_set_strength(3);
2011-07-17 12:25:15 +00:00
}
2011-07-17 19:01:09 +00:00