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 Feb 22 10:45:56 2017 -0700
Revision:
53:a1563574a980
Parent:
47:a68747642a7a
Child:
54:c04d7b6fa075
Mostly working TX in main app

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