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:
Tue Mar 14 08:32:55 2017 -0600
Revision:
62:9751a8504c82
Parent:
61:8d9efd33cac9
Child:
63:e1efbe3402d9
First cut at pair code

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 62:9751a8504c82 48
Matt Briggs 62:9751a8504c82 49 // TODO some day make these private with setters and getters
Matt Briggs 62:9751a8504c82 50 uint8_t mNetworkAddr[4];
Matt Briggs 62:9751a8504c82 51 uint8_t mNetworkSessionKey[16];
Matt Briggs 62:9751a8504c82 52 uint8_t mDataSessionKey[16];
Matt Briggs 55:79ab0bbc5008 53 private:
Matt Briggs 55:79ab0bbc5008 54 uint16_t mProtocolFlag;
Matt Briggs 55:79ab0bbc5008 55 uint16_t mProtocolRev;
Matt Briggs 62:9751a8504c82 56 uint32_t mFreq;
Matt Briggs 55:79ab0bbc5008 57 uint16_t mLogicalAddr;
Matt Briggs 55:79ab0bbc5008 58 uint32_t mSeqNum;
Matt Briggs 55:79ab0bbc5008 59 };
Matt Briggs 55:79ab0bbc5008 60
Matt Briggs 40:2ec4be320961 61 /**
Matt Briggs 40:2ec4be320961 62 * @class PeerBruteCommProtocol
Matt Briggs 41:9ef4c4d77711 63 * @brief This class implements a peer-to-peer (P2P) brute force protocol.
Matt Briggs 41:9ef4c4d77711 64 *
Matt Briggs 41:9ef4c4d77711 65 * @details The protocol consists of at a minimum one transmitter (TX) and one
Matt Briggs 41:9ef4c4d77711 66 * receiver (RX) which communicate via the 900MHz LoRa modulation. The concept
Matt Briggs 41:9ef4c4d77711 67 * is that the RX is cycling on and off thus only listening for a small period
Matt Briggs 41:9ef4c4d77711 68 * of time. Since we are not relying on a common time-base or other
Matt Briggs 41:9ef4c4d77711 69 * synchronization mechanism the TX simply transmits for a duration long enough
Matt Briggs 41:9ef4c4d77711 70 * guarantee that the RX will have at least one receive window during that time
Matt Briggs 41:9ef4c4d77711 71 * period. Hence the name brute since the TX is just transmitting plenty for
Matt Briggs 41:9ef4c4d77711 72 * the RX to hear it.
Matt Briggs 41:9ef4c4d77711 73 *
Matt Briggs 41:9ef4c4d77711 74 * The following should be implemented outside of the class:
Matt Briggs 41:9ef4c4d77711 75 * - Power settings?
Matt Briggs 41:9ef4c4d77711 76 * - What to do with the msgs which are received
Matt Briggs 41:9ef4c4d77711 77 * - When to send messages
Matt Briggs 41:9ef4c4d77711 78 * - When to sleep
Matt Briggs 41:9ef4c4d77711 79 *
Matt Briggs 40:2ec4be320961 80 */
Matt Briggs 41:9ef4c4d77711 81
Matt Briggs 41:9ef4c4d77711 82 class CommProtocolPeerBrute
Matt Briggs 40:2ec4be320961 83 {
Matt Briggs 40:2ec4be320961 84 public:
Matt Briggs 41:9ef4c4d77711 85 /**
Matt Briggs 41:9ef4c4d77711 86 * @brief CommProtocolPeerBrute constructor
Matt Briggs 41:9ef4c4d77711 87 *
Matt Briggs 41:9ef4c4d77711 88 * @details Just initialized internal variables does not configure devices.
Matt Briggs 41:9ef4c4d77711 89 * Should call init before other functions are called.
Matt Briggs 41:9ef4c4d77711 90 *
Matt Briggs 41:9ef4c4d77711 91 * On Entry:
Matt Briggs 41:9ef4c4d77711 92 *
Matt Briggs 41:9ef4c4d77711 93 * On Exit:
Matt Briggs 44:ece6330e9b57 94 * Internal variables are set to a known state but further initialization is required
Matt Briggs 41:9ef4c4d77711 95 *
Matt Briggs 41:9ef4c4d77711 96 * @return
Matt Briggs 41:9ef4c4d77711 97 */
Matt Briggs 44:ece6330e9b57 98 CommProtocolPeerBrute();
Matt Briggs 40:2ec4be320961 99
Matt Briggs 41:9ef4c4d77711 100 /**
Matt Briggs 47:a68747642a7a 101 * @brief Initialize radio with stored network network settings
Matt Briggs 41:9ef4c4d77711 102 *
Matt Briggs 41:9ef4c4d77711 103 * @return Returns the result of all the commands
Matt Briggs 41:9ef4c4d77711 104 */
Matt Briggs 41:9ef4c4d77711 105 CmdResult init();
Matt Briggs 62:9751a8504c82 106 CmdResult configForSavedNetwork();
Matt Briggs 41:9ef4c4d77711 107
Matt Briggs 41:9ef4c4d77711 108 /**
Matt Briggs 44:ece6330e9b57 109 * @brief Sets weather this object is configured as a TX or RX
Matt Briggs 44:ece6330e9b57 110 *
Matt Briggs 44:ece6330e9b57 111 * @param isTx if true then configured as transmitter if false then configured as receiver.
Matt Briggs 44:ece6330e9b57 112 */
Matt Briggs 44:ece6330e9b57 113 void setTx(bool isTx);
Matt Briggs 44:ece6330e9b57 114
Matt Briggs 44:ece6330e9b57 115 /**
Matt Briggs 41:9ef4c4d77711 116 * @brief Returns weather this object is configured as a TX or RX
Matt Briggs 41:9ef4c4d77711 117 * @return Returns true if TX
Matt Briggs 41:9ef4c4d77711 118 */
Matt Briggs 40:2ec4be320961 119 bool isTx();
Matt Briggs 41:9ef4c4d77711 120
Matt Briggs 41:9ef4c4d77711 121 /**
Matt Briggs 41:9ef4c4d77711 122 * @brief Returns weather this object is configured as a TX or RX
Matt Briggs 41:9ef4c4d77711 123 * @return Returns true if RX
Matt Briggs 41:9ef4c4d77711 124 */
Matt Briggs 40:2ec4be320961 125 bool isRx() {return !isTx();}
Matt Briggs 40:2ec4be320961 126
Matt Briggs 41:9ef4c4d77711 127 /**
Matt Briggs 41:9ef4c4d77711 128 * @brief This will clear the security pairs and channel info for pair
Matt Briggs 41:9ef4c4d77711 129 *
Matt Briggs 41:9ef4c4d77711 130 * @details This will clear both the value in RAM as well as the value stored in EEPROM for security and
Matt Briggs 41:9ef4c4d77711 131 * clarity purposes. If RX this command will automatically trigger the generation of new security keys and
Matt Briggs 41:9ef4c4d77711 132 * pair specific parameters.
Matt Briggs 41:9ef4c4d77711 133 *
Matt Briggs 41:9ef4c4d77711 134 * @return Returns status of all commands completed
Matt Briggs 41:9ef4c4d77711 135 */
Matt Briggs 40:2ec4be320961 136 CmdResult clearPair();
Matt Briggs 40:2ec4be320961 137
Matt Briggs 40:2ec4be320961 138 // TX focused
Matt Briggs 41:9ef4c4d77711 139 /**
Matt Briggs 41:9ef4c4d77711 140 * @brief Transmit the msg attached
Matt Briggs 41:9ef4c4d77711 141 *
Matt Briggs 41:9ef4c4d77711 142 * @details TODO figure out what information is wrapped by the LoRaWAN stuff implemented by
Matt Briggs 41:9ef4c4d77711 143 * Multitech Systems
Matt Briggs 41:9ef4c4d77711 144 *
Matt Briggs 41:9ef4c4d77711 145 * @param msg A STL vector of uint8_t
Matt Briggs 41:9ef4c4d77711 146 * @return Returns status of all commands completed
Matt Briggs 41:9ef4c4d77711 147 */
Matt Briggs 40:2ec4be320961 148 CmdResult send (const std::vector<uint8_t> &msg);
Matt Briggs 41:9ef4c4d77711 149
Matt Briggs 41:9ef4c4d77711 150 /**
Matt Briggs 41:9ef4c4d77711 151 * @brief This function sends a request to pair with a RX. If successful a pair accept message is received.
Matt Briggs 41:9ef4c4d77711 152 *
Matt Briggs 41:9ef4c4d77711 153 * @return Returns status of all commands completed
Matt Briggs 41:9ef4c4d77711 154 */
Matt Briggs 40:2ec4be320961 155 CmdResult sendPairReq();
Matt Briggs 40:2ec4be320961 156
Matt Briggs 40:2ec4be320961 157 // RX focused
Matt Briggs 41:9ef4c4d77711 158 /**
Matt Briggs 41:9ef4c4d77711 159 * @brief This function enables the RX hardware of a radio. Note the listen time is defined internally by the protocol
Matt Briggs 41:9ef4c4d77711 160 *
Matt Briggs 41:9ef4c4d77711 161 * @param msgPending This boolean is return by reference. If true then during the listen window a msg was received.
Matt Briggs 41:9ef4c4d77711 162 * @return Returns status of all commands completed
Matt Briggs 41:9ef4c4d77711 163 */
Matt Briggs 40:2ec4be320961 164 CmdResult listen (bool &msgPending);
Matt Briggs 41:9ef4c4d77711 165
Matt Briggs 54:c04d7b6fa075 166 CmdResult sampleDLC();
Matt Briggs 54:c04d7b6fa075 167
Matt Briggs 41:9ef4c4d77711 168 /**
Matt Briggs 41:9ef4c4d77711 169 * @brief Returns the last message received via listening
Matt Briggs 41:9ef4c4d77711 170 *
Matt Briggs 41:9ef4c4d77711 171 * @param msg This STL vector of uint8_t is returned by reference with msg contents.
Matt Briggs 41:9ef4c4d77711 172 * @return Returns status of all commands completed
Matt Briggs 41:9ef4c4d77711 173 */
Matt Briggs 40:2ec4be320961 174 CmdResult recv (std::vector<uint8_t> &msg);
Matt Briggs 41:9ef4c4d77711 175
Matt Briggs 41:9ef4c4d77711 176 /**
Matt Briggs 41:9ef4c4d77711 177 * @brief This fucntion enables the radio to listen for pair requests.
Matt Briggs 41:9ef4c4d77711 178 * @param waitTime The maximum time which radio waits for pair requests
Matt Briggs 41:9ef4c4d77711 179 *
Matt Briggs 41:9ef4c4d77711 180 * TODO determine if it is important to know who is paired
Matt Briggs 41:9ef4c4d77711 181 *
Matt Briggs 41:9ef4c4d77711 182 * @return If pair message received then cmdSucess is returned otherwise timeout is returned.
Matt Briggs 41:9ef4c4d77711 183 */
Matt Briggs 40:2ec4be320961 184 CmdResult waitForPairing(float waitTime);
Matt Briggs 40:2ec4be320961 185
Matt Briggs 41:9ef4c4d77711 186 /**
Matt Briggs 41:9ef4c4d77711 187 * @brief Send a pair accept message. This message contains all the information for TX to properly send
Matt Briggs 41:9ef4c4d77711 188 * msgs to RX in P2P mode.
Matt Briggs 41:9ef4c4d77711 189 * @return Returns status of all commands completed
Matt Briggs 41:9ef4c4d77711 190 */
Matt Briggs 41:9ef4c4d77711 191 CmdResult sendPairAccepted();
Matt Briggs 41:9ef4c4d77711 192
Matt Briggs 61:8d9efd33cac9 193 CmdResult waitForAccept(float waitTime);
Matt Briggs 61:8d9efd33cac9 194
Matt Briggs 41:9ef4c4d77711 195 // xDot Peer to Peer Specific
Matt Briggs 41:9ef4c4d77711 196 /**
Matt Briggs 41:9ef4c4d77711 197 * Convenience function to get the internal downlink count from radio
Matt Briggs 41:9ef4c4d77711 198 * @return Number of downlink msgs
Matt Briggs 41:9ef4c4d77711 199 */
Matt Briggs 41:9ef4c4d77711 200 uint32_t getDLC();
Matt Briggs 41:9ef4c4d77711 201
Matt Briggs 41:9ef4c4d77711 202 /**
Matt Briggs 41:9ef4c4d77711 203 * Convenience function to get the internal uplink count from radio
Matt Briggs 41:9ef4c4d77711 204 * @return Number of uplink msgs
Matt Briggs 41:9ef4c4d77711 205 */
Matt Briggs 41:9ef4c4d77711 206 uint32_t getULC();
Matt Briggs 41:9ef4c4d77711 207
Matt Briggs 62:9751a8504c82 208 // TODO maybe this should be private
Matt Briggs 62:9751a8504c82 209 CmdResult configForPairingNetwork();
Matt Briggs 62:9751a8504c82 210
Matt Briggs 40:2ec4be320961 211 private:
Matt Briggs 55:79ab0bbc5008 212 NvmProtocolObj mMemObj;
Matt Briggs 40:2ec4be320961 213 bool mIsTx;
Matt Briggs 54:c04d7b6fa075 214 uint32_t mPrevDownLinkCnt;
Matt Briggs 40:2ec4be320961 215
Matt Briggs 40:2ec4be320961 216 /**
Matt Briggs 40:2ec4be320961 217 * @brief Reads baseboard information from non-volatile memory (NVM)
Matt Briggs 40:2ec4be320961 218 *
Matt Briggs 40:2ec4be320961 219 * @details This data is read from the xDot's internal EEPROM. This
Matt Briggs 40:2ec4be320961 220 * method is called from init(). The following
Matt Briggs 40:2ec4be320961 221 * is stored:
Matt Briggs 40:2ec4be320961 222 * 1. Peer Brute Storage code word
Matt Briggs 40:2ec4be320961 223 * 2. 8-bit protocol version
Matt Briggs 40:2ec4be320961 224 * 3. 16-bit Baseboard serial number
Matt Briggs 40:2ec4be320961 225 * 4. 4 Byte network address
Matt Briggs 40:2ec4be320961 226 * 5. 16 Byte network session key
Matt Briggs 40:2ec4be320961 227 * 6. 16 Byte data session key
Matt Briggs 40:2ec4be320961 228 * 7. 4 Byte DLC
Matt Briggs 40:2ec4be320961 229 * 8. 4 Byte ULC
Matt Briggs 40:2ec4be320961 230 *
Matt Briggs 40:2ec4be320961 231 * TODO add memory map information
Matt Briggs 40:2ec4be320961 232 *
Matt Briggs 40:2ec4be320961 233 * @return CmdResult
Matt Briggs 40:2ec4be320961 234 */
Matt Briggs 40:2ec4be320961 235 CmdResult readInfoFromNVM();
Matt Briggs 40:2ec4be320961 236
Matt Briggs 40:2ec4be320961 237 /**
Matt Briggs 40:2ec4be320961 238 * @brief Stores baseboard information to non-volatile memory (NVM)
Matt Briggs 40:2ec4be320961 239 *
Matt Briggs 40:2ec4be320961 240 * @details This method is called during special configuration events like first time boot or factory reset.
Matt Briggs 40:2ec4be320961 241 *
Matt Briggs 40:2ec4be320961 242 * TODO add memory map information
Matt Briggs 40:2ec4be320961 243 *
Matt Briggs 40:2ec4be320961 244 * @return CmdResult
Matt Briggs 40:2ec4be320961 245 */
Matt Briggs 40:2ec4be320961 246 CmdResult writeInfoToNVM();
Matt Briggs 40:2ec4be320961 247
Matt Briggs 41:9ef4c4d77711 248 /**
Matt Briggs 41:9ef4c4d77711 249 * Resets both uplink and downlink counters to zero. Useful for when pairing.
Matt Briggs 41:9ef4c4d77711 250 * @return
Matt Briggs 41:9ef4c4d77711 251 */
Matt Briggs 40:2ec4be320961 252 CmdResult resetCounters();
Matt Briggs 40:2ec4be320961 253
Matt Briggs 41:9ef4c4d77711 254 /**
Matt Briggs 41:9ef4c4d77711 255 * Generates new encryption keys for radio
Matt Briggs 41:9ef4c4d77711 256 * @return
Matt Briggs 41:9ef4c4d77711 257 */
Matt Briggs 62:9751a8504c82 258 CmdResult genEncypKey(std::vector<uint8_t> &newKey, uint8_t keySize, bool allowZero);
Matt Briggs 61:8d9efd33cac9 259 CmdResult genEncypKey(std::vector<uint8_t> &newKey, uint8_t keySize);
Matt Briggs 61:8d9efd33cac9 260
Matt Briggs 40:2ec4be320961 261 };
Matt Briggs 40:2ec4be320961 262
Matt Briggs 55:79ab0bbc5008 263
Matt Briggs 40:2ec4be320961 264 #endif