crashtest-r0ket/firmware/funk/openbeacon.c

86 lines
1.6 KiB
C
Raw Normal View History

2011-07-17 15:42:19 +00:00
#include <stdint.h>
#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-17 12:25:15 +00:00
2011-07-17 15:42:19 +00:00
const uint32_t key[4] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
2011-07-17 12:25:15 +00:00
const uint8_t enctoggle = 0;
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
2011-07-17 15:42:19 +00:00
uint32_t oid = 0;
uint32_t ctr = 0;
2011-07-17 12:25:15 +00:00
uint8_t strength = 0;
2011-07-17 15:42:19 +00:00
void openbeaconSave()
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;
if( f_open(&file, "beacon", FA_OPEN_ALWAYS|FA_WRITE) )
return;
uint32touint8p(ctr, buf);
if( f_write(&file, buf, 4, &readbytes) )
return;
f_close(&file);
2011-07-17 15:42:19 +00:00
}
void openbeaconRead()
{
2011-07-17 19:01:09 +00:00
FIL file;
2011-07-17 18:32:17 +00:00
BYTE buf[4];
UINT readbytes;
if( f_open(&file, "beacon", FA_OPEN_EXISTING|FA_READ) )
return;
if( f_read(&file, buf, 4, &readbytes) )
return;
ctr = uint8ptouint32(buf);
2011-07-17 15:42:19 +00:00
}
void openbeaconSetup(uint32_t id)
{
oid = id;
2011-07-17 12:25:15 +00:00
strength = 0;
2011-07-17 15:42:19 +00:00
openbeaconRead();
2011-07-17 12:25:15 +00:00
}
void openbeaconSendPacket(uint32_t id, uint32_t ctr, uint8_t flags, uint8_t strength)
{
uint8_t buf[32];
int status;
buf[0]=0x10; // Length: 16 bytes
buf[1]=0x17; // Proto - fixed at 0x17?
buf[2]=flags;
buf[3]=strength*85; // Send intensity
2011-07-17 15:42:19 +00:00
uint32touint8p(ctr, buf+4);
uint32touint8p(id, buf+8);
2011-07-17 12:25:15 +00:00
buf[12]=0xff; // salt (0xffff always?)
buf[13]=0xff;
2011-07-17 15:42:19 +00:00
status=nrf_snd_pkt_crc_encr(16,buf,enctoggle?key:NULL);
2011-07-17 12:25:15 +00:00
}
void openbeaconSend(void)
{
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-07-17 15:42:19 +00:00
openbeaconSendPacket(oid, ctr++, 0xFF, strength++);
2011-07-17 12:25:15 +00:00
if( strength == 4 )
strength = 0;
2011-07-17 15:42:19 +00:00
if( ctr % OPENBEACON_SAVECOUNTER == 0 )
openbeaconSave();
2011-07-17 12:25:15 +00:00
}
2011-07-17 19:01:09 +00:00