![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Fork to see if I can get working
Dependencies: BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated
Fork of xDotBridge_update_test20180823 by
xDotBridge/inc/CommProtocolPeerBrute.h
- Committer:
- Matt Briggs
- Date:
- 2017-03-14
- Revision:
- 64:46c8819c07cc
- Parent:
- 63:e1efbe3402d9
- Child:
- 65:d546060aa03d
File content as of revision 64:46c8819c07cc:
/** * Library for LoRa Peer to Peer Brute Force Protocol */ #ifndef COMMPROTOCOLPEERBRUTE_H_ #define COMMPROTOCOLPEERBRUTE_H_ #include <inttypes.h> #include <vector> #include "../config.h" #include "mDot.h" // TODO make base class to allow different protocols to be used with easy // TODO wrap radio commands for error checking // TODO change to const static uint8_t pair_network_address[] = { 0x01, 0x00, 0x00, 0x00 }; // 0x00000000 reserved for multicast so use 1 static uint8_t pair_network_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 }; static uint8_t pair_data_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 }; const uint16_t PROTOCOL_NVM_START_ADDR = 0x1000; const uint16_t PROTOCOL_NVM_SIZE = 54; // Bytes const uint16_t PROTOCOL_FLAG = 0x5A00; const uint16_t PROTOCOL_REV = 0x0002; class NvmProtocolObj { public: NvmProtocolObj(); void setDefaults(); CmdResult fromBytes(uint8_t *data, uint8_t size); CmdResult toBytes(uint8_t *data, uint8_t &size); uint16_t getProtocolFlag(); bool validProtocolFlag(); uint16_t getProtocolRev(); bool validProtocolRev(); void getNetworkAddr(std::vector<uint8_t> &addr); void setNetworkAddr(const std::vector<uint8_t> &addr); void getNetworkSessionKey(std::vector<uint8_t> &key); void setNetworkSessionKey(const std::vector<uint8_t> &key); void getDataSessionKey(std::vector<uint8_t> &key); void setDataSessionKey(const std::vector<uint8_t> &key); uint16_t getLogicalAddr(); void setLogicalAddr(uint16_t in); uint32_t getLastMsgSeq(); void setLastMsgSeq(uint32_t in); // TODO some day make these private with setters and getters uint8_t mNetworkAddr[4]; uint8_t mNetworkSessionKey[16]; uint8_t mDataSessionKey[16]; private: uint16_t mProtocolFlag; uint16_t mProtocolRev; uint32_t mFreq; uint16_t mLogicalAddr; uint32_t mSeqNum; }; /** * @class PeerBruteCommProtocol * @brief This class implements a peer-to-peer (P2P) brute force protocol. * * @details The protocol consists of at a minimum one transmitter (TX) and one * receiver (RX) which communicate via the 900MHz LoRa modulation. The concept * is that the RX is cycling on and off thus only listening for a small period * of time. Since we are not relying on a common time-base or other * synchronization mechanism the TX simply transmits for a duration long enough * guarantee that the RX will have at least one receive window during that time * period. Hence the name brute since the TX is just transmitting plenty for * the RX to hear it. * * The following should be implemented outside of the class: * - Power settings? * - What to do with the msgs which are received * - When to send messages * - When to sleep * */ class CommProtocolPeerBrute { public: /** * @brief CommProtocolPeerBrute constructor * * @details Just initialized internal variables does not configure devices. * Should call init before other functions are called. * * On Entry: * * On Exit: * Internal variables are set to a known state but further initialization is required * * @return */ CommProtocolPeerBrute(); /** * @brief Initialize radio with stored network network settings * * @return Returns the result of all the commands */ CmdResult init(); CmdResult configForSavedNetwork(); /** * @brief Sets weather this object is configured as a TX or RX * * @param isTx if true then configured as transmitter if false then configured as receiver. */ void setTx(bool isTx); /** * @brief Returns weather this object is configured as a TX or RX * @return Returns true if TX */ bool isTx(); /** * @brief Returns weather this object is configured as a TX or RX * @return Returns true if RX */ bool isRx() {return !isTx();} /** * @brief This will clear the security pairs and channel info for pair * * @details This will clear both the value in RAM as well as the value stored in EEPROM for security and * clarity purposes. If RX this command will automatically trigger the generation of new security keys and * pair specific parameters. * * @return Returns status of all commands completed */ CmdResult clearPair(); // TX focused /** * @brief Transmit the msg attached * * @details TODO figure out what information is wrapped by the LoRaWAN stuff implemented by * Multitech Systems * * @param msg A STL vector of uint8_t * @return Returns status of all commands completed */ CmdResult send (const std::vector<uint8_t> &msg); /** * @brief This function sends a request to pair with a RX. If successful a pair accept message is received. * * @return Returns status of all commands completed */ CmdResult sendPairReq(); // RX focused /** * @brief This function enables the RX hardware of a radio. Note the listen time is defined internally by the protocol * * @param msgPending This boolean is return by reference. If true then during the listen window a msg was received. * @return Returns status of all commands completed */ CmdResult listen (bool &msgPending); CmdResult sampleDLC(); /** * @brief Returns the last message received via listening * * @param msg This STL vector of uint8_t is returned by reference with msg contents. * @return Returns status of all commands completed */ CmdResult recv (std::vector<uint8_t> &msg); /** * @brief This fucntion enables the radio to listen for pair requests. * @param waitTime The maximum time which radio waits for pair requests * * TODO determine if it is important to know who is paired * * @return If pair message received then cmdSucess is returned otherwise timeout is returned. */ CmdResult waitForPairing(float waitTime); /** * @brief Send a pair accept message. This message contains all the information for TX to properly send * msgs to RX in P2P mode. * @return Returns status of all commands completed */ CmdResult sendPairAccepted(); CmdResult waitForAccept(float waitTime); // xDot Peer to Peer Specific /** * Convenience function to get the internal downlink count from radio * @return Number of downlink msgs */ uint32_t getDLC(); /** * Convenience function to get the internal uplink count from radio * @return Number of uplink msgs */ uint32_t getULC(); /** * Resets both uplink and downlink counters (1 and 0 respectively). * @return */ CmdResult resetCounters(); // TODO maybe this should be private CmdResult configForPairingNetwork(); void printDotConfig(); private: NvmProtocolObj mMemObj; bool mIsTx; uint32_t mPrevDownLinkCnt; /** * @brief Reads baseboard information from non-volatile memory (NVM) * * @details This data is read from the xDot's internal EEPROM. This * method is called from init(). The following * is stored: * 1. Peer Brute Storage code word * 2. 8-bit protocol version * 3. 16-bit Baseboard serial number * 4. 4 Byte network address * 5. 16 Byte network session key * 6. 16 Byte data session key * 7. 4 Byte DLC * 8. 4 Byte ULC * * TODO add memory map information * * @return CmdResult */ CmdResult readInfoFromNVM(); /** * @brief Stores baseboard information to non-volatile memory (NVM) * * @details This method is called during special configuration events like first time boot or factory reset. * * TODO add memory map information * * @return CmdResult */ CmdResult writeInfoToNVM(); /** * Generates new encryption keys for radio * @return */ CmdResult genEncypKey(std::vector<uint8_t> &newKey, uint8_t keySize, bool allowZero); CmdResult genEncypKey(std::vector<uint8_t> &newKey, uint8_t keySize); }; #endif