Fork to see if I can get working

Dependencies:   BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated

Fork of xDotBridge_update_test20180823 by Matt Briggs

Committer:
Matt Briggs
Date:
Wed Mar 15 09:49:58 2017 -0600
Revision:
65:d546060aa03d
Parent:
64:46c8819c07cc
Child:
70:6b3ca63792c2
Added better alert message code.  Quick commit before debug.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Matt Briggs 40:2ec4be320961 1 /**
Matt Briggs 40:2ec4be320961 2 * Library for LoRa Peer to Peer Brute Force Protocol
Matt Briggs 40:2ec4be320961 3 */
Matt Briggs 40:2ec4be320961 4
Matt Briggs 41:9ef4c4d77711 5 #ifndef COMMPROTOCOLPEERBRUTE_H_
Matt Briggs 41:9ef4c4d77711 6 #define COMMPROTOCOLPEERBRUTE_H_
Matt Briggs 40:2ec4be320961 7
Matt Briggs 41:9ef4c4d77711 8 #include <inttypes.h>
Matt Briggs 41:9ef4c4d77711 9 #include <vector>
Matt Briggs 40:2ec4be320961 10 #include "../config.h"
Matt Briggs 41:9ef4c4d77711 11 #include "mDot.h"
Matt Briggs 40:2ec4be320961 12
Matt Briggs 40:2ec4be320961 13 // TODO make base class to allow different protocols to be used with easy
Matt Briggs 41:9ef4c4d77711 14 // TODO wrap radio commands for error checking
Matt Briggs 41:9ef4c4d77711 15
Matt Briggs 41:9ef4c4d77711 16 // TODO change to const
Matt Briggs 62:9751a8504c82 17 static uint8_t pair_network_address[] = { 0x01, 0x00, 0x00, 0x00 }; // 0x00000000 reserved for multicast so use 1
Matt Briggs 40:2ec4be320961 18 static uint8_t pair_network_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };
Matt Briggs 40:2ec4be320961 19 static uint8_t pair_data_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };
Matt Briggs 40:2ec4be320961 20
Matt Briggs 55:79ab0bbc5008 21 const uint16_t PROTOCOL_NVM_START_ADDR = 0x1000;
Matt Briggs 62:9751a8504c82 22 const uint16_t PROTOCOL_NVM_SIZE = 54; // Bytes
Matt Briggs 55:79ab0bbc5008 23 const uint16_t PROTOCOL_FLAG = 0x5A00;
Matt Briggs 62:9751a8504c82 24 const uint16_t PROTOCOL_REV = 0x0002;
Matt Briggs 55:79ab0bbc5008 25 class NvmProtocolObj {
Matt Briggs 55:79ab0bbc5008 26 public:
Matt Briggs 55:79ab0bbc5008 27 NvmProtocolObj();
Matt Briggs 55:79ab0bbc5008 28 void setDefaults();
Matt Briggs 55:79ab0bbc5008 29 CmdResult fromBytes(uint8_t *data, uint8_t size);
Matt Briggs 55:79ab0bbc5008 30 CmdResult toBytes(uint8_t *data, uint8_t &size);
Matt Briggs 55:79ab0bbc5008 31
Matt Briggs 55:79ab0bbc5008 32 uint16_t getProtocolFlag();
Matt Briggs 55:79ab0bbc5008 33 bool validProtocolFlag();
Matt Briggs 55:79ab0bbc5008 34 uint16_t getProtocolRev();
Matt Briggs 55:79ab0bbc5008 35 bool validProtocolRev();
Matt Briggs 55:79ab0bbc5008 36
Matt Briggs 62:9751a8504c82 37 void getNetworkAddr(std::vector<uint8_t> &addr);
Matt Briggs 62:9751a8504c82 38 void setNetworkAddr(const std::vector<uint8_t> &addr);
Matt Briggs 62:9751a8504c82 39 void getNetworkSessionKey(std::vector<uint8_t> &key);
Matt Briggs 62:9751a8504c82 40 void setNetworkSessionKey(const std::vector<uint8_t> &key);
Matt Briggs 62:9751a8504c82 41 void getDataSessionKey(std::vector<uint8_t> &key);
Matt Briggs 62:9751a8504c82 42 void setDataSessionKey(const std::vector<uint8_t> &key);
Matt Briggs 62:9751a8504c82 43
Matt Briggs 55:79ab0bbc5008 44 uint16_t getLogicalAddr();
Matt Briggs 55:79ab0bbc5008 45 void setLogicalAddr(uint16_t in);
Matt Briggs 55:79ab0bbc5008 46 uint32_t getLastMsgSeq();
Matt Briggs 55:79ab0bbc5008 47 void setLastMsgSeq(uint32_t in);
Matt Briggs 65:d546060aa03d 48 void incLastMsgSeq();
Matt Briggs 62:9751a8504c82 49
Matt Briggs 62:9751a8504c82 50 // TODO some day make these private with setters and getters
Matt Briggs 62:9751a8504c82 51 uint8_t mNetworkAddr[4];
Matt Briggs 62:9751a8504c82 52 uint8_t mNetworkSessionKey[16];
Matt Briggs 62:9751a8504c82 53 uint8_t mDataSessionKey[16];
Matt Briggs 55:79ab0bbc5008 54 private:
Matt Briggs 55:79ab0bbc5008 55 uint16_t mProtocolFlag;
Matt Briggs 55:79ab0bbc5008 56 uint16_t mProtocolRev;
Matt Briggs 62:9751a8504c82 57 uint32_t mFreq;
Matt Briggs 55:79ab0bbc5008 58 uint16_t mLogicalAddr;
Matt Briggs 55:79ab0bbc5008 59 uint32_t mSeqNum;
Matt Briggs 55:79ab0bbc5008 60 };
Matt Briggs 55:79ab0bbc5008 61
Matt Briggs 40:2ec4be320961 62 /**
Matt Briggs 40:2ec4be320961 63 * @class PeerBruteCommProtocol
Matt Briggs 41:9ef4c4d77711 64 * @brief This class implements a peer-to-peer (P2P) brute force protocol.
Matt Briggs 41:9ef4c4d77711 65 *
Matt Briggs 41:9ef4c4d77711 66 * @details The protocol consists of at a minimum one transmitter (TX) and one
Matt Briggs 41:9ef4c4d77711 67 * receiver (RX) which communicate via the 900MHz LoRa modulation. The concept
Matt Briggs 41:9ef4c4d77711 68 * is that the RX is cycling on and off thus only listening for a small period
Matt Briggs 41:9ef4c4d77711 69 * of time. Since we are not relying on a common time-base or other
Matt Briggs 41:9ef4c4d77711 70 * synchronization mechanism the TX simply transmits for a duration long enough
Matt Briggs 41:9ef4c4d77711 71 * guarantee that the RX will have at least one receive window during that time
Matt Briggs 41:9ef4c4d77711 72 * period. Hence the name brute since the TX is just transmitting plenty for
Matt Briggs 41:9ef4c4d77711 73 * the RX to hear it.
Matt Briggs 41:9ef4c4d77711 74 *
Matt Briggs 41:9ef4c4d77711 75 * The following should be implemented outside of the class:
Matt Briggs 41:9ef4c4d77711 76 * - Power settings?
Matt Briggs 41:9ef4c4d77711 77 * - What to do with the msgs which are received
Matt Briggs 41:9ef4c4d77711 78 * - When to send messages
Matt Briggs 41:9ef4c4d77711 79 * - When to sleep
Matt Briggs 41:9ef4c4d77711 80 *
Matt Briggs 40:2ec4be320961 81 */
Matt Briggs 41:9ef4c4d77711 82
Matt Briggs 41:9ef4c4d77711 83 class CommProtocolPeerBrute
Matt Briggs 40:2ec4be320961 84 {
Matt Briggs 40:2ec4be320961 85 public:
Matt Briggs 41:9ef4c4d77711 86 /**
Matt Briggs 41:9ef4c4d77711 87 * @brief CommProtocolPeerBrute constructor
Matt Briggs 41:9ef4c4d77711 88 *
Matt Briggs 41:9ef4c4d77711 89 * @details Just initialized internal variables does not configure devices.
Matt Briggs 41:9ef4c4d77711 90 * Should call init before other functions are called.
Matt Briggs 41:9ef4c4d77711 91 *
Matt Briggs 41:9ef4c4d77711 92 * On Entry:
Matt Briggs 41:9ef4c4d77711 93 *
Matt Briggs 41:9ef4c4d77711 94 * On Exit:
Matt Briggs 44:ece6330e9b57 95 * Internal variables are set to a known state but further initialization is required
Matt Briggs 41:9ef4c4d77711 96 *
Matt Briggs 41:9ef4c4d77711 97 * @return
Matt Briggs 41:9ef4c4d77711 98 */
Matt Briggs 44:ece6330e9b57 99 CommProtocolPeerBrute();
Matt Briggs 40:2ec4be320961 100
Matt Briggs 41:9ef4c4d77711 101 /**
Matt Briggs 47:a68747642a7a 102 * @brief Initialize radio with stored network network settings
Matt Briggs 41:9ef4c4d77711 103 *
Matt Briggs 41:9ef4c4d77711 104 * @return Returns the result of all the commands
Matt Briggs 41:9ef4c4d77711 105 */
Matt Briggs 41:9ef4c4d77711 106 CmdResult init();
Matt Briggs 62:9751a8504c82 107 CmdResult configForSavedNetwork();
Matt Briggs 41:9ef4c4d77711 108
Matt Briggs 41:9ef4c4d77711 109 /**
Matt Briggs 44:ece6330e9b57 110 * @brief Sets weather this object is configured as a TX or RX
Matt Briggs 44:ece6330e9b57 111 *
Matt Briggs 44:ece6330e9b57 112 * @param isTx if true then configured as transmitter if false then configured as receiver.
Matt Briggs 44:ece6330e9b57 113 */
Matt Briggs 44:ece6330e9b57 114 void setTx(bool isTx);
Matt Briggs 44:ece6330e9b57 115
Matt Briggs 44:ece6330e9b57 116 /**
Matt Briggs 41:9ef4c4d77711 117 * @brief Returns weather this object is configured as a TX or RX
Matt Briggs 41:9ef4c4d77711 118 * @return Returns true if TX
Matt Briggs 41:9ef4c4d77711 119 */
Matt Briggs 40:2ec4be320961 120 bool isTx();
Matt Briggs 41:9ef4c4d77711 121
Matt Briggs 41:9ef4c4d77711 122 /**
Matt Briggs 41:9ef4c4d77711 123 * @brief Returns weather this object is configured as a TX or RX
Matt Briggs 41:9ef4c4d77711 124 * @return Returns true if RX
Matt Briggs 41:9ef4c4d77711 125 */
Matt Briggs 40:2ec4be320961 126 bool isRx() {return !isTx();}
Matt Briggs 40:2ec4be320961 127
Matt Briggs 41:9ef4c4d77711 128 /**
Matt Briggs 41:9ef4c4d77711 129 * @brief This will clear the security pairs and channel info for pair
Matt Briggs 41:9ef4c4d77711 130 *
Matt Briggs 41:9ef4c4d77711 131 * @details This will clear both the value in RAM as well as the value stored in EEPROM for security and
Matt Briggs 41:9ef4c4d77711 132 * clarity purposes. If RX this command will automatically trigger the generation of new security keys and
Matt Briggs 41:9ef4c4d77711 133 * pair specific parameters.
Matt Briggs 41:9ef4c4d77711 134 *
Matt Briggs 41:9ef4c4d77711 135 * @return Returns status of all commands completed
Matt Briggs 41:9ef4c4d77711 136 */
Matt Briggs 40:2ec4be320961 137 CmdResult clearPair();
Matt Briggs 40:2ec4be320961 138
Matt Briggs 40:2ec4be320961 139 // TX focused
Matt Briggs 65:d546060aa03d 140 uint32_t getSeqNum();
Matt Briggs 41:9ef4c4d77711 141 /**
Matt Briggs 41:9ef4c4d77711 142 * @brief Transmit the msg attached
Matt Briggs 41:9ef4c4d77711 143 *
Matt Briggs 41:9ef4c4d77711 144 * @details TODO figure out what information is wrapped by the LoRaWAN stuff implemented by
Matt Briggs 41:9ef4c4d77711 145 * Multitech Systems
Matt Briggs 41:9ef4c4d77711 146 *
Matt Briggs 41:9ef4c4d77711 147 * @param msg A STL vector of uint8_t
Matt Briggs 41:9ef4c4d77711 148 * @return Returns status of all commands completed
Matt Briggs 41:9ef4c4d77711 149 */
Matt Briggs 40:2ec4be320961 150 CmdResult send (const std::vector<uint8_t> &msg);
Matt Briggs 41:9ef4c4d77711 151
Matt Briggs 41:9ef4c4d77711 152 /**
Matt Briggs 65:d546060aa03d 153 * @brief Transmit an alert per documentation
Matt Briggs 65:d546060aa03d 154 *
Matt Briggs 65:d546060aa03d 155 * @details Sends EUI, uint16 data, alert seqNum
Matt Briggs 65:d546060aa03d 156 *
Matt Briggs 65:d546060aa03d 157 * @param data Input data to send
Matt Briggs 65:d546060aa03d 158 * @return if transmission is successful (note no ACK checked)
Matt Briggs 65:d546060aa03d 159 */
Matt Briggs 65:d546060aa03d 160 CmdResult sendAlert (uint16_t data);
Matt Briggs 65:d546060aa03d 161
Matt Briggs 65:d546060aa03d 162 /**
Matt Briggs 41:9ef4c4d77711 163 * @brief This function sends a request to pair with a RX. If successful a pair accept message is received.
Matt Briggs 41:9ef4c4d77711 164 *
Matt Briggs 41:9ef4c4d77711 165 * @return Returns status of all commands completed
Matt Briggs 41:9ef4c4d77711 166 */
Matt Briggs 40:2ec4be320961 167 CmdResult sendPairReq();
Matt Briggs 40:2ec4be320961 168
Matt Briggs 40:2ec4be320961 169 // RX focused
Matt Briggs 41:9ef4c4d77711 170 /**
Matt Briggs 41:9ef4c4d77711 171 * @brief This function enables the RX hardware of a radio. Note the listen time is defined internally by the protocol
Matt Briggs 41:9ef4c4d77711 172 *
Matt Briggs 41:9ef4c4d77711 173 * @param msgPending This boolean is return by reference. If true then during the listen window a msg was received.
Matt Briggs 41:9ef4c4d77711 174 * @return Returns status of all commands completed
Matt Briggs 41:9ef4c4d77711 175 */
Matt Briggs 40:2ec4be320961 176 CmdResult listen (bool &msgPending);
Matt Briggs 41:9ef4c4d77711 177
Matt Briggs 54:c04d7b6fa075 178 CmdResult sampleDLC();
Matt Briggs 54:c04d7b6fa075 179
Matt Briggs 41:9ef4c4d77711 180 /**
Matt Briggs 41:9ef4c4d77711 181 * @brief Returns the last message received via listening
Matt Briggs 41:9ef4c4d77711 182 *
Matt Briggs 41:9ef4c4d77711 183 * @param msg This STL vector of uint8_t is returned by reference with msg contents.
Matt Briggs 41:9ef4c4d77711 184 * @return Returns status of all commands completed
Matt Briggs 41:9ef4c4d77711 185 */
Matt Briggs 40:2ec4be320961 186 CmdResult recv (std::vector<uint8_t> &msg);
Matt Briggs 65:d546060aa03d 187 CmdResult recvAlert (std::vector<uint8_t> &eui, uint16_t &data, uint32_t &seqNum);
Matt Briggs 41:9ef4c4d77711 188
Matt Briggs 41:9ef4c4d77711 189 /**
Matt Briggs 41:9ef4c4d77711 190 * @brief This fucntion enables the radio to listen for pair requests.
Matt Briggs 41:9ef4c4d77711 191 * @param waitTime The maximum time which radio waits for pair requests
Matt Briggs 41:9ef4c4d77711 192 *
Matt Briggs 41:9ef4c4d77711 193 * TODO determine if it is important to know who is paired
Matt Briggs 41:9ef4c4d77711 194 *
Matt Briggs 41:9ef4c4d77711 195 * @return If pair message received then cmdSucess is returned otherwise timeout is returned.
Matt Briggs 41:9ef4c4d77711 196 */
Matt Briggs 40:2ec4be320961 197 CmdResult waitForPairing(float waitTime);
Matt Briggs 40:2ec4be320961 198
Matt Briggs 41:9ef4c4d77711 199 /**
Matt Briggs 41:9ef4c4d77711 200 * @brief Send a pair accept message. This message contains all the information for TX to properly send
Matt Briggs 41:9ef4c4d77711 201 * msgs to RX in P2P mode.
Matt Briggs 41:9ef4c4d77711 202 * @return Returns status of all commands completed
Matt Briggs 41:9ef4c4d77711 203 */
Matt Briggs 41:9ef4c4d77711 204 CmdResult sendPairAccepted();
Matt Briggs 41:9ef4c4d77711 205
Matt Briggs 61:8d9efd33cac9 206 CmdResult waitForAccept(float waitTime);
Matt Briggs 61:8d9efd33cac9 207
Matt Briggs 65:d546060aa03d 208
Matt Briggs 41:9ef4c4d77711 209 // xDot Peer to Peer Specific
Matt Briggs 41:9ef4c4d77711 210 /**
Matt Briggs 41:9ef4c4d77711 211 * Convenience function to get the internal downlink count from radio
Matt Briggs 41:9ef4c4d77711 212 * @return Number of downlink msgs
Matt Briggs 41:9ef4c4d77711 213 */
Matt Briggs 41:9ef4c4d77711 214 uint32_t getDLC();
Matt Briggs 41:9ef4c4d77711 215
Matt Briggs 41:9ef4c4d77711 216 /**
Matt Briggs 41:9ef4c4d77711 217 * Convenience function to get the internal uplink count from radio
Matt Briggs 41:9ef4c4d77711 218 * @return Number of uplink msgs
Matt Briggs 41:9ef4c4d77711 219 */
Matt Briggs 41:9ef4c4d77711 220 uint32_t getULC();
Matt Briggs 41:9ef4c4d77711 221
Matt Briggs 64:46c8819c07cc 222 /**
Matt Briggs 64:46c8819c07cc 223 * Resets both uplink and downlink counters (1 and 0 respectively).
Matt Briggs 64:46c8819c07cc 224 * @return
Matt Briggs 64:46c8819c07cc 225 */
Matt Briggs 64:46c8819c07cc 226 CmdResult resetCounters();
Matt Briggs 64:46c8819c07cc 227
Matt Briggs 62:9751a8504c82 228 // TODO maybe this should be private
Matt Briggs 62:9751a8504c82 229 CmdResult configForPairingNetwork();
Matt Briggs 63:e1efbe3402d9 230 void printDotConfig();
Matt Briggs 62:9751a8504c82 231
Matt Briggs 40:2ec4be320961 232 private:
Matt Briggs 55:79ab0bbc5008 233 NvmProtocolObj mMemObj;
Matt Briggs 40:2ec4be320961 234 bool mIsTx;
Matt Briggs 54:c04d7b6fa075 235 uint32_t mPrevDownLinkCnt;
Matt Briggs 40:2ec4be320961 236
Matt Briggs 40:2ec4be320961 237 /**
Matt Briggs 40:2ec4be320961 238 * @brief Reads baseboard information from non-volatile memory (NVM)
Matt Briggs 40:2ec4be320961 239 *
Matt Briggs 40:2ec4be320961 240 * @details This data is read from the xDot's internal EEPROM. This
Matt Briggs 40:2ec4be320961 241 * method is called from init(). The following
Matt Briggs 40:2ec4be320961 242 * is stored:
Matt Briggs 40:2ec4be320961 243 * 1. Peer Brute Storage code word
Matt Briggs 40:2ec4be320961 244 * 2. 8-bit protocol version
Matt Briggs 40:2ec4be320961 245 * 3. 16-bit Baseboard serial number
Matt Briggs 40:2ec4be320961 246 * 4. 4 Byte network address
Matt Briggs 40:2ec4be320961 247 * 5. 16 Byte network session key
Matt Briggs 40:2ec4be320961 248 * 6. 16 Byte data session key
Matt Briggs 40:2ec4be320961 249 * 7. 4 Byte DLC
Matt Briggs 40:2ec4be320961 250 * 8. 4 Byte ULC
Matt Briggs 40:2ec4be320961 251 *
Matt Briggs 40:2ec4be320961 252 * TODO add memory map information
Matt Briggs 40:2ec4be320961 253 *
Matt Briggs 40:2ec4be320961 254 * @return CmdResult
Matt Briggs 40:2ec4be320961 255 */
Matt Briggs 40:2ec4be320961 256 CmdResult readInfoFromNVM();
Matt Briggs 40:2ec4be320961 257
Matt Briggs 40:2ec4be320961 258 /**
Matt Briggs 40:2ec4be320961 259 * @brief Stores baseboard information to non-volatile memory (NVM)
Matt Briggs 40:2ec4be320961 260 *
Matt Briggs 40:2ec4be320961 261 * @details This method is called during special configuration events like first time boot or factory reset.
Matt Briggs 40:2ec4be320961 262 *
Matt Briggs 40:2ec4be320961 263 * TODO add memory map information
Matt Briggs 40:2ec4be320961 264 *
Matt Briggs 40:2ec4be320961 265 * @return CmdResult
Matt Briggs 40:2ec4be320961 266 */
Matt Briggs 40:2ec4be320961 267 CmdResult writeInfoToNVM();
Matt Briggs 40:2ec4be320961 268
Matt Briggs 41:9ef4c4d77711 269 /**
Matt Briggs 41:9ef4c4d77711 270 * Generates new encryption keys for radio
Matt Briggs 41:9ef4c4d77711 271 * @return
Matt Briggs 41:9ef4c4d77711 272 */
Matt Briggs 62:9751a8504c82 273 CmdResult genEncypKey(std::vector<uint8_t> &newKey, uint8_t keySize, bool allowZero);
Matt Briggs 61:8d9efd33cac9 274 CmdResult genEncypKey(std::vector<uint8_t> &newKey, uint8_t keySize);
Matt Briggs 61:8d9efd33cac9 275
Matt Briggs 40:2ec4be320961 276 };
Matt Briggs 40:2ec4be320961 277
Matt Briggs 55:79ab0bbc5008 278
Matt Briggs 40:2ec4be320961 279 #endif