arduino-rx.ino 7.05 KB
Newer Older
hark's avatar
hark committed
1

yids's avatar
yids committed
2
#include <stdarg.h>
yids's avatar
yids committed
3
#include <uECC.h>
yids's avatar
yids committed
4 5 6
#include <AESLib.h>
#include <RadioHead.h>
#include <RH_ASK.h>
7
#include <RHDatagram.h>
yids's avatar
yids committed
8
#include <SPI.h> // Not actualy used but needed to compile
yids's avatar
yids committed
9
#include <string.h>
10
#include <Wire.h>
hark's avatar
hark committed
11
//#include <LCDI2C_LK162-12.h>
yids's avatar
yids committed
12
#include <MemoryFree.h>
hark's avatar
hark committed
13
#include <pagerlib.h>
hark's avatar
hark committed
14 15
#include <LiquidCrystal.h>
#include <avr/pgmspace.h>
yids's avatar
yids committed
16

hark's avatar
hark committed
17
/*
yids's avatar
yids committed
18
#define NUM_ECC_DIGITS 24 //size of privkey, curvesize in bytes 
yids's avatar
yids committed
19
#define CURVE uECC_secp192r1()
hark's avatar
hark committed
20 21
 */
//LCDI2C lcd = LCDI2C(2,16,0x50,0);
22

hark's avatar
hark committed
23 24 25
/* 
 * pagerlib things
 */
yids's avatar
yids committed
26
aes_context ctx; // context for the cbc crypto stuff
yids's avatar
yids committed
27

hark's avatar
hark committed
28 29 30 31 32 33 34 35 36 37 38
//const uint8_t tx_priv_key[ECC_COMPRESSED_SIZE-1] PROGMEM = {0xB4, 0x15, 0x70, 0x3E, 0xA7, 0x5D, 0x06, 0xF2, 0x33, 0x75, 0x8E, 0xE0, 0x86, 0x1B, 0x73, 0xBE, 0xEC, 0x87, 0x36, 0x0F, 0xF5, 0xE1, 0x79, 0x76, 0x2C, 0x3C, 0x74, 0x69, 0x83, 0x71, 0x07, 0xB5};
//const uint8_t tx_comp_p[ECC_COMPRESSED_SIZE] PROGMEM = {0x02, 0xA3, 0x91, 0x16, 0x71, 0x1E, 0x7B, 0xB2, 0x51, 0x7F, 0xD4, 0xF4, 0xC7, 0x81, 0x49, 0x75, 0x8B, 0x48, 0x75, 0x59, 0x27, 0xC9, 0x94, 0x3F, 0x59, 0xDD, 0xFF, 0x2E, 0x89, 0xE9, 0xD8, 0x1D, 0x2B };

struct pl_keypair *sender, *receiver, *kp;
char clear_message[] = "dit is een test berichtje :) ";
//const char clear_message[] PROGMEM = "Blaat coblaat, dit is een test berichtje :), en nog meer en meer en meer 123456744555 blablablablablablabal jajajajaj hee blaat ";
//const char b64_key[] PROGMEM = "Zs8rZ5EfIpMs2i5Mskq2Nnx0pHhKShirwswQbIAjXxUDkPCKKSD1o+oIF84qwlUk85B1W0lN4AyEtJZNk24HKL8=";
//const char b64_key2[] PROGMEM = "6+owJHKEhmoz1RI3dx+x7JODIjueBOZUmzjxSrmrpZ8COt4Tld6bsa2UlFnt4qDMf/Rz5M60d/wjgXzafEkCg54=";
const char b64_key[] = "Zs8rZ5EfIpMs2i5Mskq2Nnx0pHhKShirwswQbIAjXxUDkPCKKSD1o+oIF84qwlUk85B1W0lN4AyEtJZNk24HKL8=";
const char b64_key2[]= "6+owJHKEhmoz1RI3dx+x7JODIjueBOZUmzjxSrmrpZ8COt4Tld6bsa2UlFnt4qDMf/Rz5M60d/wjgXzafEkCg54=";

39

40

41

yids's avatar
yids committed
42
struct pl_ctx * context;
43

hark's avatar
hark committed
44
char output[MSG_SIZE] = "outputcharbla" ;
45

hark's avatar
hark committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#define LCD
#define RADIO

/*
 * hardware things
 */ 

#define ADDRESS 2
/*
#define MESSAGE_SIZE 32
#define PAGER_MESSAGE_SIZE 57
 */

#ifdef RADIO
RH_ASK driver(2000, 2, 12, 13);
RHDatagram manager(driver, ADDRESS);
#endif
#ifdef LCD
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5
#endif
yids's avatar
yids committed
76 77
void setup()
{
hark's avatar
hark committed
78 79 80 81 82 83 84 85 86 87 88 89
#ifdef LCD
  // select the pins used on the LCD panel
  lcd.begin(16, 2);              // start the library
  lcd.setCursor(0,0);
  // lcd.print("Push the buttons"); // print a simple message
  //pinMode(10, OUTPUT);        
  //digitalWrite(10, HIGH);

  //  lcd.init();
  lcd.clear();
  lcd.print("dit is een pager");
#endif 
yids's avatar
yids committed
90
  Serial.begin(9600);	// Debugging only
hark's avatar
hark committed
91 92

#ifdef RADIO
yids's avatar
yids committed
93 94
  Serial.println("RX init");
  if (!manager.init())
hark's avatar
hark committed
95 96 97
    Serial.println("init failed");
#endif

yids's avatar
yids committed
98 99 100
  Serial.print("freeMemory()=");
  Serial.println(freeMemory());
  context = pl_init();
hark's avatar
hark committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  //  receiver = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));

  // init pager
  //  memcpy(receiver->private_key, tx_priv_key, ECC_COMPRESSED_SIZE-1);
  //  memcpy(receiver->compressed_point, tx_comp_p, ECC_COMPRESSED_SIZE);

#ifdef LCD
  lcd.setCursor(0,1);
  // create receiver keypair and load in list
  lcd.print("*");
#endif

  /*
   * load the keys from the base64 thingies
   */
yids's avatar
yids committed
116

hark's avatar
hark committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  receiver = (struct pl_keypair *)malloc(sizeof(struct pl_keypair));
  base64_decode((char *)receiver , (char *)&b64_key, (4*sizeof(struct pl_keypair) / 3)) ;
  pl_load_key_in_list(context, receiver);

  //    receiver = pl_create_keypair(context);
  //
  // create sender keypair
  //  kp = pl_create_keypair(context);
  sender = (struct pl_keypair *)malloc(sizeof(struct pl_keypair));
  context->kp = (struct pl_keypair *)malloc(sizeof(struct pl_keypair));
  base64_decode((char *)sender , (char *)&b64_key2, (4*sizeof(struct pl_keypair) / 3)) ;

  //  pl_load_key_in_list(context, kp);
  // set default
  context->kp = sender;
#ifdef LCD
  lcd.clear();
  lcd.print("finished making keys");
#endif
yids's avatar
yids committed
136
}
hark's avatar
hark committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150
#ifdef LCD
// read the buttons
int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor 
  // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
  // we add approx 50 to those values and check to see if we are close
  if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  // For V1.1 us this threshold
  if (adc_key_in < 50)   return btnRIGHT;  
  if (adc_key_in < 250)  return btnUP; 
  if (adc_key_in < 450)  return btnDOWN; 
  if (adc_key_in < 650)  return btnLEFT; 
  if (adc_key_in < 850)  return btnSELECT;  
yids's avatar
yids committed
151

hark's avatar
hark committed
152 153 154 155 156 157 158 159 160 161
  // For V1.0 comment the other threshold and use the one below:
  /*
     if (adc_key_in < 50)   return btnRIGHT;  
     if (adc_key_in < 195)  return btnUP; 
     if (adc_key_in < 380)  return btnDOWN; 
     if (adc_key_in < 555)  return btnLEFT; 
     if (adc_key_in < 790)  return btnSELECT;   
   */
}
#endif
162 163
void display(char *msg)
{
hark's avatar
hark committed
164 165 166 167 168 169 170 171 172 173 174
  /*   char line1[16];
       char line2[16];
       for(int i=0; i < MESSAGE_SIZE ; i++){
       if(i < 16)
       line1[i] = msg[i];
       else
       line2[i-16] = msg[i];
       };
   */
  //lcd.clear();
  //lcd.println(msg);
175
}
hark's avatar
hark committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
/*
   void loop()
   {
   lcd.setCursor(9,1);            // move cursor to second line "1" and 9 spaces over
   lcd.print(millis()/1000);      // display seconds elapsed since power-up


   lcd.setCursor(0,1);            // move to the begining of the second line
   lcd_key = read_LCD_buttons();  // read the buttons

   switch (lcd_key)               // depending on which button was pushed, we perform an action
   {
   case btnRIGHT:
   {
   lcd.print("RIGHT ");
   break;
   }
   case btnLEFT:
   {
   lcd.print("LEFT   ");
   break;
   }
   case btnUP:
   {
   lcd.print("UP    ");
   break;
   }
   case btnDOWN:
   {
   lcd.print("DOWN  ");
   break;
   }
   case btnSELECT:
   {
   lcd.print("SELECT");
   break;
   }
   case btnNONE:
   {
   lcd.print("NONE  ");
   break;
   }
   }

   }
 */
222

yids's avatar
yids committed
223 224
void loop()
{
hark's avatar
hark committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238

  Serial.println("bla\n");
#ifdef LCD
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis()/100);
#endif
  memcpy(context->msg->msg + 8, "bla", MSG_SIZE - 8);


  memcpy(&context->msg->msg, clear_message, MSG_SIZE);
  //    context->kp = kp;
  //to who to send the message to (get from kp)
  memcpy(&context->receiver_compressed_point, &kp->compressed_point, sizeof(context->receiver_compressed_point));
239

yids's avatar
yids committed
240
  //get msg with radio
hark's avatar
hark committed
241 242
  //memcpy(context->msg->msg + 8, "bla", MSG_SIZE - 8);
  pl_send_message(context);
243

hark's avatar
hark committed
244
  Serial.println("crypted message:");
yids's avatar
yids committed
245
  Serial.println(context->msg->msg);
hark's avatar
hark committed
246 247 248 249 250 251 252 253 254 255
  //  base64_encode((char *)&output, context->msg->msg, MSG_SIZE);
#ifdef LCD
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("crypted message:");
  delay(1000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(context->msg->msg);
#endif
256

hark's avatar
hark committed
257 258 259 260 261 262 263 264 265 266 267
  //  pl_receive_message(context);
#ifdef LCD
  delay(1000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("decrypted msg:");
  lcd.setCursor(0,0);
  lcd.println(context->msg->msg);
  delay(1000);
  lcd.clear();
#endif
yids's avatar
yids committed
268
}
hark's avatar
hark committed
269