diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..ea62168 --- /dev/null +++ b/README.txt @@ -0,0 +1,23 @@ +Basic applications for panStamp. + +1. simpletest : basic "blink a LED" application. + +2. basicbeacon : Basic wireless beacon transmitter. + +3. basicradio : Basic bi-directional application that pairs with another + basicradio node and toggles an LED whenever a packet is received. + +4. modem : Serial wireless modem that interfaces between a network of panStamps + and a serial port (RS232, USB, ...). This application is used as the modem for + all computer applications developed for panStamp. + + Description of the serial (ASCII) protocol: + https://github.com/panStamp/panstamp/wiki/Modem%20and%20serial%20protocol + +5. aesencryption : use of the hadware AES-128 encryption engine available + on the CC430 MCU (panStamp NRG only). + +6. ctrencryption : CTR-cypher algorithm on top of the basic AES-128 encryption + (panStamp NRG only). + + diff --git a/aesencryption/aesencryption.ino b/aesencryption/aesencryption.ino new file mode 100644 index 0000000..9c11a4c --- /dev/null +++ b/aesencryption/aesencryption.ino @@ -0,0 +1,63 @@ +/** + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * panStamp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 10/17/2014 + */ + +#include "HardwareSerial.h" +#include "cc430aes.h" + +CC430AES aes; + +void setup() +{ + byte i; + + Serial.begin(57600); + + // 128-bit key + byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; + byte data[] = "Data to be encrypted/decrypted needs to be aligned to 16 bytes.."; + + Serial.print("Input string: "); + Serial.println((char*) data); + Serial.println("Setting AES-128 key"); + + // Set key + aes.setKey(key); + + Serial.println("Encripting..."); + // Encrypt + aes.aesEncrypt(data, sizeof(data)-1); + + Serial.println("Data encrypted"); + Serial.println("Now decripting..."); + + // Decrypt + aes.aesDecrypt(data, sizeof(data)-1); + + Serial.print("Data decrypted: "); + Serial.println((char*) data); +} + +void loop() +{ +} diff --git a/basicbeacon/basicbeacon.ino b/basicbeacon/basicbeacon.ino new file mode 100644 index 0000000..558902a --- /dev/null +++ b/basicbeacon/basicbeacon.ino @@ -0,0 +1,67 @@ +/** + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * panStamp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 07/23/2014 + */ + +/** + * Basic beacon example + * + * This application sleeps (ultra-low-current consumption) most of the time and + * wakes-up every five seconds to transmit a packet and then enters the sleep + * mode again + */ + +#define RFCHANNEL 0 // Let's use channel 0 +#define SYNCWORD1 0xB5 // Synchronization word, high byte +#define SYNCWORD0 0x47 // Synchronization word, low byte +#define SOURCE_ADDR 5 // Device address + +CCPACKET packet; + +void setup() +{ + byte i; + + // Setup LED output pin + pinMode(LED, OUTPUT); + digitalWrite(LED, LOW); + + panstamp.radio.setChannel(RFCHANNEL); + panstamp.radio.setSyncWord(SYNCWORD1, SYNCWORD0); + panstamp.radio.setDevAddress(SOURCE_ADDR); + panstamp.radio.setCCregs(); + panstamp.setHighTxPower(); + + packet.length = 10; + + for(i=0 ; i + * + * This file is part of the panStamp project. + * + * panStamp is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * panStamp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 07/23/2014 + */ + +/** + * Basic radio example + * + * Each node acts simultaneously as sender and receiver. Each node sends a packet + * every five senconds and toggles a LED each time a packet is received + */ + +#define RFCHANNEL 0 // Let's use channel 0 +#define SYNCWORD1 0xB5 // Synchronization word, high byte +#define SYNCWORD0 0x47 // Synchronization word, low byte +#define SOURCE_ADDR 4 // Sender address +#define DESTINATION_ADDR 5 // Receiver address + +CCPACKET txPacket; // packet object +byte count = 0; + +/** + * This function is called whenever a wireless packet is received + */ +void rfPacketReceived(CCPACKET *packet) +{ + // The LED will toggle when a new packet is received + digitalWrite(LED, !digitalRead(LED)); + + if (packet->length > 1) + { + // packet.data[0]; // Our source address + // packet.data[1]; // Self-incrementing value generated by the sender + } +} + +void setup() +{ + // Setup LED output pin + pinMode(LED, OUTPUT); + digitalWrite(LED, LOW); + + panstamp.radio.setChannel(RFCHANNEL); + panstamp.radio.setSyncWord(SYNCWORD1, SYNCWORD0); + panstamp.radio.setDevAddress(SOURCE_ADDR); + panstamp.radio.setCCregs(); + + // Let's disable address check for the current project so that our device + // will receive packets even not addressed to it. + panstamp.radio.disableAddressCheck(); + + // Declare RF callback function + panstamp.setPacketRxCallback(rfPacketReceived); +} + +void loop() +{ + txPacket.length = 2; // Let's send a single data byte plus the destination address + + txPacket.data[0] = DESTINATION_ADDR; // First data byte has to be the destination address + txPacket.data[1] = count++; // Self-incrementing value + panstamp.radio.sendData(txPacket); // Transmit packet + + delay(5000); // Transmit every 5 seconds + // For low-power applications replace "delay" by "panstamp.sleepWd(WDTO_8S)" for example +} + diff --git a/ctrencryption/ctrencryption.ino b/ctrencryption/ctrencryption.ino new file mode 100644 index 0000000..d66fb2d --- /dev/null +++ b/ctrencryption/ctrencryption.ino @@ -0,0 +1,64 @@ +/** + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * panStamp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 10/19/2014 + */ + +/** + * This sketch is only compatible with the NRG platform + */ + +#include "HardwareSerial.h" +#include "cc430aes.h" + +void setup() +{ + Serial.begin(57600); + + // 128-bit key + byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; + byte data[] = "Data to be encrypted/decrypted does not need to be aligned to 16 bytes"; + long nonce = 1654; + + Serial.print("Input string: "); + Serial.println((char*) data); + Serial.println("Setting AES-128 key"); + + // Set key + CC430AES::setKey(key); + + Serial.println("Encripting..."); + // Encrypt + CC430AES::ctrCrypto(data, sizeof(data)-1, nonce); + + Serial.println("Data encrypted"); + Serial.println("Now decripting..."); + + // Decrypt + CC430AES::ctrCrypto(data, sizeof(data)-1, nonce); + + Serial.print("Data decrypted: "); + Serial.println((char*) data); +} + +void loop() +{ +} diff --git a/modem/modem.h b/modem/modem.h new file mode 100644 index 0000000..bb5425b --- /dev/null +++ b/modem/modem.h @@ -0,0 +1,83 @@ +/* + * modem.h + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * panStamp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 10/12/10 + */ + +#ifndef _MODEM_H +#define _MODEM_H + +#include "Arduino.h" +#include "HardwareSerial.h" +#include "version.h" + + +#define SERIAL_BUF_LEN 128 // Maximum length for any serial string +#define TIMER1_TICK_PERIOD_US 500000 // Timer1 tick = 500 ms +#define MAX_SERIAL_SILENCE_MS 1000 // Maximum serial silence between serial command characters = 1000 ms +#define MAX_SERIAL_SILENCE_TK MAX_SERIAL_SILENCE_MS / (TIMER1_TICK_PERIOD_US / 1000) // Max serial silence in ticks + +/** + * AT command set + */ +#define AT_ATTENTION "AT" // Basic Attention command +#define AT_RESET "ATZ" // Reset modem +#define AT_GOTO_CMDMODE "+++" // Go to command mode +#define AT_GOTO_DATAMODE "ATO" // Go to data mode +#define AT_HVERSION "ATHV" // Hardware version +#define AT_FVERSION "ATFV" // Firmware version +#define AT_FREQCHANNEL "ATCH" // Frequency channel +#define AT_SYNCWORD "ATSW" // Synchronization word +#define AT_DEVADDRESS "ATDA" // Device address +#define AT_ADDRCHECK "ATAC" // Address check + +/** + * Serial modes + */ +enum SERMODE +{ + SERMODE_DATA, + SERMODE_COMMAND +}; + +/** + * Type of AT query + */ +enum ATQUERY +{ + ATQUERY_COMMAND = 0, + ATQUERY_REQUEST +}; + +/** + * Global variables + */ +CCPACKET *rxPacket; +char strSerial[SERIAL_BUF_LEN]; // Serial buffer +byte ch; +int len = 0; +SERMODE serMode = SERMODE_DATA; // Serial mode (data or command mode) +boolean packetAvailable = false; // Wireless packet received when true + +#endif // _MODEM_H + diff --git a/modem/modem.ino b/modem/modem.ino new file mode 100644 index 0000000..678f8f5 --- /dev/null +++ b/modem/modem.ino @@ -0,0 +1,385 @@ +/* + * modem.pde + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * panStamp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 15/02/2011 + * + * Device: + * Serial gateway or modem + * + * Description: + * This is not a proper SWAP gateway but a simple transparent UART-RF + * interface. This device can be used from a serial console as hyperterminal + * or minicom. Wireless packets are passed to/from the host computer in ASCII + * format whilst configuration is done via AT commands. + * + * Visit our wiki for details about the protocol in case you want to develop + * your own PC library for this device. + */ + +#include "modem.h" +#ifdef PANSTAMP_NRG +#include "timer1a0.h" +#define TIMER timer1a0 +#define RESET_TIMER() +#define INIT_TIMER() TIMER.attachInterrupt(isrT1event) +#define START_TIMER() TIMER.start(1000) +#define STOP_TIMER() TIMER.stop() +#elif PANSTAMP_AVR +#include +#include "TimerOne.h" +byte t1Ticks = 0; // Timer 1 ticks +#define TIMER Timer1 +#define RESET_TIMER() t1Ticks = 0 +#define INIT_TIMER() TIMER.initialize(TIMER1_TICK_PERIOD_US); TIMER.attachInterrupt(isrT1event) +#define START_TIMER() RESET_TIMER(); TIMER.attachInterrupt(isrT1event) +#define STOP_TIMER() TIMER.detachInterrupt() +#endif + +/** + * LED pin + */ +#define LEDPIN 4 + +byte charToHex(byte ch); + +/** + * This function is called whenever a wireless packet is received + */ +void rfPacketReceived(CCPACKET *packet) +{ + if (packet->length > 5) + { + rxPacket = packet; + packetAvailable = true; + } +} + +/** + * isrT1event + * + * Timer1 interrupt routine + */ +void isrT1event(void) +{ + #ifdef PANSTAMP_AVR + if (t1Ticks == MAX_SERIAL_SILENCE_TK) + { + #endif + // Detach Timer1 interrupt + STOP_TIMER(); + RESET_TIMER(); + // Pending "+++" command? + if (!strcmp(strSerial, AT_GOTO_CMDMODE)) + { + panstamp.rxOff(); // Disable wireless reception + Serial.println("OK-Command mode"); + serMode = SERMODE_COMMAND; + } + memset(strSerial, 0, sizeof(strSerial)); + len = 0; + #ifdef PANSTAMP_AVR + } + else + t1Ticks++; + #endif +} + +/** + * handleSerialCmd + * + * Handle serial command received + * + * 'command' AT command received + */ +void handleSerialCmd(char* command) +{ + byte i, len; + byte arrV[2]; + CCPACKET packet; + ATQUERY atQuery = ATQUERY_REQUEST; + + // Data mode? + if (serMode == SERMODE_DATA) + { + packet.length = strlen(command)/2; + + if (packet.length > 0) + { + // Convert ASCII string into array of bytes + for(i=0 ; i= 'A' && ch <= 'F') + val = ch - 55; + else if (ch >= 'a' && ch <= 'f') + val = ch - 87; + else if (ch >= '0' && ch <= '9') + val = ch - 48; + else + val = 0x00; + + return val; +} + diff --git a/modem/version.h b/modem/version.h new file mode 100644 index 0000000..1f7dc98 --- /dev/null +++ b/modem/version.h @@ -0,0 +1,41 @@ +/** + * version.h + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * panStamp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: #cdate# + */ + +#ifndef _VERSION_H +#define _VERSION_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000100 + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000102 + +#endif + diff --git a/simpletest/simpletest.ino b/simpletest/simpletest.ino new file mode 100644 index 0000000..fdf28ab --- /dev/null +++ b/simpletest/simpletest.ino @@ -0,0 +1,11 @@ +void setup() { + // put your setup code here, to run once: + pinMode(LED, OUTPUT); +} + +void loop() { + // put your main code here, to run repeatedly: + digitalWrite(LED, !digitalRead(LED)); + + delay(500); +}