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:
Thu Mar 09 16:47:42 2017 -0700
Revision:
61:8d9efd33cac9
Parent:
55:79ab0bbc5008
Child:
62:9751a8504c82
Slight modification of pair code and documentation.  Also some test code to all xDot dev boards to act like bridges.

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