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 01 15:20:45 2017 -0700
Revision:
44:ece6330e9b57
Parent:
41:9ef4c4d77711
Child:
47:a68747642a7a
First cut at BaseboardIO class implementation.
Minior cleanup in main and CommProtocolPeerBrute

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