Merge branch 'master' of github.com:r0ket/r0ket

This commit is contained in:
schneider 2011-07-17 01:10:34 +02:00
commit 4f8c1c7ade
4 changed files with 142 additions and 30 deletions

View File

@ -12,11 +12,28 @@
#include "funk/rftransfer.h"
/**************************************************************************/
#define BEACON_CHANNEL 81
#define BEACON_MAC "\x1\x2\x3\x2\1"
uint32_t const testkey[4] = {
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
};
int enctoggle=0;
void f_init(void){
nrf_init();
int dx=0;
int dy=8;
dx=DoString(0,dy,"Done."); ;dy+=8;
struct NRF_CFG config = {
.channel= BEACON_CHANNEL,
.txmac= BEACON_MAC,
.nrmacs=1,
.mac0= BEACON_MAC,
.maclen ="\x10",
};
nrf_config_set(&config);
lcdPrintln("Done.");
};
void f_status(void){
@ -50,8 +67,7 @@ void f_recv(void){
__attribute__ ((aligned (4))) uint8_t buf[32];
int len;
len=nrf_rcv_pkt_time(1000,sizeof(buf),buf);
len=nrf_rcv_pkt_time_encr(1000,sizeof(buf),buf,enctoggle?testkey:NULL);
if(len==0){
lcdPrintln("No pkt (Timeout)");
@ -60,17 +76,82 @@ void f_recv(void){
lcdPrint("1:");lcdPrintIntHex( *(int*)(buf+ 0) ); lcdNl();
lcdPrint("2:");lcdPrintIntHex( *(int*)(buf+ 4) ); lcdNl();
lcdPrint("3:");lcdPrintIntHex( *(int*)(buf+ 8) ); lcdNl();
lcdPrint("4:");lcdPrintShortHex( *(int*)(buf+12) ); lcdNl();
lcdPrint("4:");lcdPrintIntHex( *(int*)(buf+12) ); lcdNl();
len=crc16(buf,14);
lcdPrint("crc:");lcdPrintShortHex(len); lcdNl();
};
void f_cfg(void){
struct NRF_CFG config;
nrfconfig cfg=&config;
nrf_config_get(cfg);
lcdPrint("ch:");lcdPrintInt( cfg->channel ); lcdNl();
lcdPrint("nr:");lcdPrintInt( cfg->nrmacs ); lcdNl();
lcdPrint("0:");
lcdPrintCharHex(cfg->mac0[0]);
lcdPrintCharHex(cfg->mac0[1]);
lcdPrintCharHex(cfg->mac0[2]);
lcdPrintCharHex(cfg->mac0[3]);
lcdPrintCharHex(cfg->mac0[4]);
lcdNl();
lcdPrint("1:");
lcdPrintCharHex(cfg->mac1[0]);
lcdPrintCharHex(cfg->mac1[1]);
lcdPrintCharHex(cfg->mac1[2]);
lcdPrintCharHex(cfg->mac1[3]);
lcdPrintCharHex(cfg->mac1[4]);
lcdNl();
lcdPrint("2345:");
lcdPrintCharHex(cfg->mac2345[0]);
lcdPrintCharHex(cfg->mac2345[1]);
lcdPrintCharHex(cfg->mac2345[2]);
lcdPrintCharHex(cfg->mac2345[3]);
lcdNl();
lcdPrint("tx:");
lcdPrintCharHex(cfg->txmac[0]);
lcdPrintCharHex(cfg->txmac[1]);
lcdPrintCharHex(cfg->txmac[2]);
lcdPrintCharHex(cfg->txmac[3]);
lcdPrintCharHex(cfg->txmac[4]);
lcdNl();
lcdPrint("len:");
lcdPrintCharHex(cfg->maclen[0]);
lcdPrintCharHex(cfg->maclen[1]);
lcdPrintCharHex(cfg->maclen[2]);
lcdPrintCharHex(cfg->maclen[3]);
lcdPrintCharHex(cfg->maclen[4]);
lcdNl();
};
void f_cfg_set(void){
struct NRF_CFG config = {
.channel= 13,
.txmac= "R0KET",
.nrmacs=1,
.mac0= "R0KET",
.maclen ="\x10",
};
nrf_config_set(&config);
};
void f_enctog(void){
enctoggle=1-enctoggle;
if(enctoggle)
lcdPrintln("Encrypt ON!");
else
lcdPrintln("encrypt off!");
};
void f_send(void){
static char ctr=1;
int dx=0;
int dy=8;
uint8_t buf[32];
int status;
@ -79,10 +160,14 @@ void f_send(void){
buf[2]=0xff; // Flags (0xff)
buf[3]=0xff; // Send intensity
/*
buf[4]=0x00; // ctr
buf[5]=0x00; // ctr
buf[6]=0x00; // ctr
buf[7]=ctr++; // ctr
*/
*(int*)(buf+4)=ctr++;
buf[8]=0x0; // Object id
buf[9]=0x0;
@ -92,10 +177,11 @@ void f_send(void){
buf[12]=0xff; // salt (0xffff always?)
buf[13]=0xff;
status=nrf_snd_pkt_crc(14,buf);
dx=DoString(0,dy,"St:"); DoIntX(dx,dy,status); dy+=8;
status=nrf_snd_pkt_crc_encr(16,buf,enctoggle?testkey:NULL);
lcdPrint("Status:");
lcdPrintCharHex(status);
lcdNl();
};
void gotoISP(void) {
@ -135,6 +221,9 @@ const struct MENU_DEF menu_init = {"F Init", &f_init};
const struct MENU_DEF menu_status = {"F Status", &f_status};
const struct MENU_DEF menu_rcv = {"F Recv", &f_recv};
const struct MENU_DEF menu_snd = {"F Send", &f_send};
const struct MENU_DEF menu_cfg = {"F CfgGet", &f_cfg};
const struct MENU_DEF menu_cfg2 = {"F CfgSet", &f_cfg_set};
const struct MENU_DEF menu_enc = {"Toggle Encr", &f_enctog};
const struct MENU_DEF menu_sndblock={"F Send block", &f_sendBlock};
const struct MENU_DEF menu_mirror = {"Mirror", &lcd_mirror};
const struct MENU_DEF menu_volt = {"Akku", &adc_check};
@ -145,7 +234,9 @@ static menuentry menu[] = {
&menu_status,
&menu_rcv,
&menu_snd,
&menu_sndblock,
&menu_enc,
&menu_cfg2,
&menu_cfg,
&menu_nop,
&menu_mirror,
&menu_volt,

View File

@ -14,6 +14,10 @@
#define CHANNEL_BEACON 81
#define MAC_BEACON "\x1\x2\x3\x2\1"
uint32_t const testkey[4] = {
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
};
void f_init(void){
nrf_init();
@ -35,7 +39,7 @@ void f_recv(void){
int len;
while(1){
len=nrf_rcv_pkt_time(1000,sizeof(buf),buf);
len=nrf_rcv_pkt_time_encr(1000,sizeof(buf),buf,testkey);
if(len==0){
lcdPrintln("No pkt (Timeout)");
@ -56,7 +60,7 @@ void f_recv(void){
lcdPrint("ct:");lcdPrintIntHex( *(int*)(buf+ 4) ); lcdNl();
lcdPrint("id:");lcdPrintIntHex( *(int*)(buf+ 8) ); lcdNl();
lcdPrint("xx:");lcdPrintShortHex( *(int*)(buf+12) ); lcdNl();
lcdPrint("xx:");lcdPrintIntHex( *(int*)(buf+12) ); lcdNl();
lcdDisplay(0);
};
};
@ -64,7 +68,7 @@ void f_recv(void){
void f_send(void){
int ctr=1;
uint8_t buf[32];
__attribute__ ((aligned (4))) uint8_t buf[32];
int status;
while(1){
@ -92,7 +96,7 @@ void f_send(void){
buf[12]=0xff; // salt (0xffff always?)
buf[13]=0xff;
status=nrf_snd_pkt_crc(14,buf);
status=nrf_snd_pkt_crc_encr(16,buf,testkey);
lcdClear();
lcdPrint("Key:"); lcdPrintInt(buf[2]); lcdNl();
lcdPrint("F-St:"); lcdPrintInt(status);

View File

@ -66,6 +66,13 @@ void nrf_read_long(const uint8_t cmd, int len, uint8_t* data){
CS_HIGH();
};
void nrf_read_pkt(int len, uint8_t* data){
CS_LOW();
xmit_spi(C_R_RX_PAYLOAD);
sspReceive(0,data,len);
CS_HIGH();
};
void nrf_read_pkt_crc(int len, uint8_t* data, uint8_t* crc){
CS_LOW();
xmit_spi(C_R_RX_PAYLOAD);
@ -103,11 +110,9 @@ int nrf_rcv_pkt_time_xxtea(int maxtime, int maxsize,
return n;
}
int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt){
int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t const key[4]){
uint8_t len;
uint8_t status=0;
uint8_t crc[2];
uint16_t cmpcrc;
nrf_write_reg(R_CONFIG,
@ -140,16 +145,18 @@ int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt){
continue;
return -2; // no packet error
};
len-=2; // crc is not part of the length
if(len>maxsize){
continue;
return -1; // packet too large
};
nrf_read_pkt_crc(len,pkt,crc);
cmpcrc=crc16(pkt,len);
nrf_read_pkt(len,pkt);
if(key != NULL)
xxtea_decode_words((uint32_t*)pkt,len/4,key);
if(cmpcrc != (crc[0] <<8 | crc[1])) {
cmpcrc=crc16(pkt,len-2);
if(cmpcrc != (pkt[len-2] <<8 | pkt[len-1])) {
continue;
return -3; // CRC failed
};
@ -167,7 +174,7 @@ int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt){
return len;
};
char nrf_snd_pkt_crc(int size, uint8_t * pkt){
char nrf_snd_pkt_crc_encr(int size, uint8_t * pkt, uint32_t const key[4]){
if(size > MAX_PKT)
size=MAX_PKT;
@ -178,12 +185,15 @@ char nrf_snd_pkt_crc(int size, uint8_t * pkt){
);
// nrf_write_long(C_W_TX_PAYLOAD,size,pkt);
uint16_t crc=crc16(pkt,size);
uint16_t crc=crc16(pkt,size-2);
pkt[size-2]=(crc >>8) & 0xff;
pkt[size-1]=crc & 0xff;
if(key !=NULL)
xxtea_encode_words((uint32_t*)pkt,size/4,key);
CS_LOW();
xmit_spi(C_W_TX_PAYLOAD);
sspSend(0,pkt,size);
xmit_spi((crc >>8) & 0xff);
xmit_spi(crc & 0xff);
CS_HIGH();
CE_HIGH();
@ -321,6 +331,5 @@ void nrf_init() {
// Set speed / strength
nrf_write_reg(R_RF_SETUP,DEFAULT_SPEED|R_RF_SETUP_RF_PWR_3);
};

View File

@ -128,10 +128,18 @@ typedef struct NRF_CFG * nrfconfig;
/* exported functions */
int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt);
#define nrf_rcv_pkt_time(maxtime, maxsize, pkt) \
nrf_rcv_pkt_time_encr(maxtime, maxsize, pkt, NULL)
int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t const k[4]);
#define nrf_snd_pkt_crc(size, pkt) \
nrf_snd_pkt_crc_encr(size, pkt, NULL)
char nrf_snd_pkt_crc_encr(int size, uint8_t * pkt, uint32_t const k[4]);
// Schneider-stuff?
int nrf_rcv_pkt_time_xxtea(int maxtime, int maxsize,
uint8_t * pkt, uint32_t const k[4]);
char nrf_snd_pkt_crc(int size, uint8_t * pkt);
char nrf_snd_pkt_xxtea(int size, uint8_t * pkt, uint32_t const k[4]);
void nrf_init() ;