Peer to peer xDot
Dependencies: ISL29011 libxDot-dev-mbed5-deprecated
src/dot_util.cpp@0:33f2abdc6bf9, 2018-03-05 (annotated)
- Committer:
- javierp
- Date:
- Mon Mar 05 23:51:58 2018 +0000
- Revision:
- 0:33f2abdc6bf9
Conexi?n peer to peer para que con una misma clave se entiendan dos xDot.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
javierp | 0:33f2abdc6bf9 | 1 | #include "dot_util.h" |
javierp | 0:33f2abdc6bf9 | 2 | #if defined(TARGET_XDOT_L151CC) |
javierp | 0:33f2abdc6bf9 | 3 | #include "xdot_low_power.h" |
javierp | 0:33f2abdc6bf9 | 4 | #endif |
javierp | 0:33f2abdc6bf9 | 5 | |
javierp | 0:33f2abdc6bf9 | 6 | void display_config() { |
javierp | 0:33f2abdc6bf9 | 7 | // display configuration and library version information |
javierp | 0:33f2abdc6bf9 | 8 | logInfo("====================="); |
javierp | 0:33f2abdc6bf9 | 9 | logInfo("general configuration"); |
javierp | 0:33f2abdc6bf9 | 10 | logInfo("====================="); |
javierp | 0:33f2abdc6bf9 | 11 | logInfo("version ------------------ %s", dot->getId().c_str()); |
javierp | 0:33f2abdc6bf9 | 12 | logInfo("device ID/EUI ------------ %s", mts::Text::bin2hexString(dot->getDeviceId()).c_str()); |
javierp | 0:33f2abdc6bf9 | 13 | logInfo("default channel plan ----- %s", mDot::FrequencyBandStr(dot->getDefaultFrequencyBand()).c_str()); |
javierp | 0:33f2abdc6bf9 | 14 | logInfo("current channel plan ----- %s", mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str()); |
javierp | 0:33f2abdc6bf9 | 15 | if (lora::ChannelPlan::IsPlanFixed(dot->getFrequencyBand())) { |
javierp | 0:33f2abdc6bf9 | 16 | logInfo("frequency sub band ------- %u", dot->getFrequencySubBand()); |
javierp | 0:33f2abdc6bf9 | 17 | } |
javierp | 0:33f2abdc6bf9 | 18 | logInfo("public network ----------- %s", dot->getPublicNetwork() ? "on" : "off"); |
javierp | 0:33f2abdc6bf9 | 19 | logInfo("========================="); |
javierp | 0:33f2abdc6bf9 | 20 | logInfo("credentials configuration"); |
javierp | 0:33f2abdc6bf9 | 21 | logInfo("========================="); |
javierp | 0:33f2abdc6bf9 | 22 | logInfo("device class ------------- %s", dot->getClass().c_str()); |
javierp | 0:33f2abdc6bf9 | 23 | logInfo("network join mode -------- %s", mDot::JoinModeStr(dot->getJoinMode()).c_str()); |
javierp | 0:33f2abdc6bf9 | 24 | if (dot->getJoinMode() == mDot::MANUAL || dot->getJoinMode() == mDot::PEER_TO_PEER) { |
javierp | 0:33f2abdc6bf9 | 25 | logInfo("network address ---------- %s", mts::Text::bin2hexString(dot->getNetworkAddress()).c_str()); |
javierp | 0:33f2abdc6bf9 | 26 | logInfo("network session key------- %s", mts::Text::bin2hexString(dot->getNetworkSessionKey()).c_str()); |
javierp | 0:33f2abdc6bf9 | 27 | logInfo("data session key---------- %s", mts::Text::bin2hexString(dot->getDataSessionKey()).c_str()); |
javierp | 0:33f2abdc6bf9 | 28 | } else { |
javierp | 0:33f2abdc6bf9 | 29 | logInfo("network name ------------- %s", dot->getNetworkName().c_str()); |
javierp | 0:33f2abdc6bf9 | 30 | logInfo("network phrase ----------- %s", dot->getNetworkPassphrase().c_str()); |
javierp | 0:33f2abdc6bf9 | 31 | logInfo("network EUI -------------- %s", mts::Text::bin2hexString(dot->getNetworkId()).c_str()); |
javierp | 0:33f2abdc6bf9 | 32 | logInfo("network KEY -------------- %s", mts::Text::bin2hexString(dot->getNetworkKey()).c_str()); |
javierp | 0:33f2abdc6bf9 | 33 | } |
javierp | 0:33f2abdc6bf9 | 34 | logInfo("========================"); |
javierp | 0:33f2abdc6bf9 | 35 | logInfo("communication parameters"); |
javierp | 0:33f2abdc6bf9 | 36 | logInfo("========================"); |
javierp | 0:33f2abdc6bf9 | 37 | if (dot->getJoinMode() == mDot::PEER_TO_PEER) { |
javierp | 0:33f2abdc6bf9 | 38 | logInfo("TX frequency ------------- %lu", dot->getTxFrequency()); |
javierp | 0:33f2abdc6bf9 | 39 | } else { |
javierp | 0:33f2abdc6bf9 | 40 | logInfo("acks --------------------- %s, %u attempts", dot->getAck() > 0 ? "on" : "off", dot->getAck()); |
javierp | 0:33f2abdc6bf9 | 41 | } |
javierp | 0:33f2abdc6bf9 | 42 | logInfo("TX datarate -------------- %s", mDot::DataRateStr(dot->getTxDataRate()).c_str()); |
javierp | 0:33f2abdc6bf9 | 43 | logInfo("TX power ----------------- %lu dBm", dot->getTxPower()); |
javierp | 0:33f2abdc6bf9 | 44 | logInfo("antenna gain ------------- %u dBm", dot->getAntennaGain()); |
javierp | 0:33f2abdc6bf9 | 45 | logInfo("LBT ---------------------- %s", dot->getLbtTimeUs() ? "on" : "off"); |
javierp | 0:33f2abdc6bf9 | 46 | if (dot->getLbtTimeUs()) { |
javierp | 0:33f2abdc6bf9 | 47 | logInfo("LBT time ----------------- %lu us", dot->getLbtTimeUs()); |
javierp | 0:33f2abdc6bf9 | 48 | logInfo("LBT threshold ------------ %d dBm", dot->getLbtThreshold()); |
javierp | 0:33f2abdc6bf9 | 49 | } |
javierp | 0:33f2abdc6bf9 | 50 | } |
javierp | 0:33f2abdc6bf9 | 51 | |
javierp | 0:33f2abdc6bf9 | 52 | void update_peer_to_peer_config(uint8_t *network_address, uint8_t *network_session_key, uint8_t *data_session_key, uint32_t tx_frequency, uint8_t tx_datarate, uint8_t tx_power) { |
javierp | 0:33f2abdc6bf9 | 53 | std::vector<uint8_t> current_network_address = dot->getNetworkAddress(); |
javierp | 0:33f2abdc6bf9 | 54 | std::vector<uint8_t> current_network_session_key = dot->getNetworkSessionKey(); |
javierp | 0:33f2abdc6bf9 | 55 | std::vector<uint8_t> current_data_session_key = dot->getDataSessionKey(); |
javierp | 0:33f2abdc6bf9 | 56 | uint32_t current_tx_frequency = dot->getTxFrequency(); |
javierp | 0:33f2abdc6bf9 | 57 | uint8_t current_tx_datarate = dot->getTxDataRate(); |
javierp | 0:33f2abdc6bf9 | 58 | uint8_t current_tx_power = dot->getTxPower(); |
javierp | 0:33f2abdc6bf9 | 59 | |
javierp | 0:33f2abdc6bf9 | 60 | std::vector<uint8_t> network_address_vector(network_address, network_address + 4); |
javierp | 0:33f2abdc6bf9 | 61 | std::vector<uint8_t> network_session_key_vector(network_session_key, network_session_key + 16); |
javierp | 0:33f2abdc6bf9 | 62 | std::vector<uint8_t> data_session_key_vector(data_session_key, data_session_key + 16); |
javierp | 0:33f2abdc6bf9 | 63 | |
javierp | 0:33f2abdc6bf9 | 64 | if (current_network_address != network_address_vector) { |
javierp | 0:33f2abdc6bf9 | 65 | logInfo("changing network address from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_network_address).c_str(), mts::Text::bin2hexString(network_address_vector).c_str()); |
javierp | 0:33f2abdc6bf9 | 66 | if (dot->setNetworkAddress(network_address_vector) != mDot::MDOT_OK) { |
javierp | 0:33f2abdc6bf9 | 67 | logError("failed to set network address to \"%s\"", mts::Text::bin2hexString(network_address_vector).c_str()); |
javierp | 0:33f2abdc6bf9 | 68 | } |
javierp | 0:33f2abdc6bf9 | 69 | } |
javierp | 0:33f2abdc6bf9 | 70 | |
javierp | 0:33f2abdc6bf9 | 71 | if (current_network_session_key != network_session_key_vector) { |
javierp | 0:33f2abdc6bf9 | 72 | logInfo("changing network session key from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_network_session_key).c_str(), mts::Text::bin2hexString(network_session_key_vector).c_str()); |
javierp | 0:33f2abdc6bf9 | 73 | if (dot->setNetworkSessionKey(network_session_key_vector) != mDot::MDOT_OK) { |
javierp | 0:33f2abdc6bf9 | 74 | logError("failed to set network session key to \"%s\"", mts::Text::bin2hexString(network_session_key_vector).c_str()); |
javierp | 0:33f2abdc6bf9 | 75 | } |
javierp | 0:33f2abdc6bf9 | 76 | } |
javierp | 0:33f2abdc6bf9 | 77 | |
javierp | 0:33f2abdc6bf9 | 78 | if (current_data_session_key != data_session_key_vector) { |
javierp | 0:33f2abdc6bf9 | 79 | logInfo("changing data session key from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_data_session_key).c_str(), mts::Text::bin2hexString(data_session_key_vector).c_str()); |
javierp | 0:33f2abdc6bf9 | 80 | if (dot->setDataSessionKey(data_session_key_vector) != mDot::MDOT_OK) { |
javierp | 0:33f2abdc6bf9 | 81 | logError("failed to set data session key to \"%s\"", mts::Text::bin2hexString(data_session_key_vector).c_str()); |
javierp | 0:33f2abdc6bf9 | 82 | } |
javierp | 0:33f2abdc6bf9 | 83 | } |
javierp | 0:33f2abdc6bf9 | 84 | |
javierp | 0:33f2abdc6bf9 | 85 | if (current_tx_frequency != tx_frequency) { |
javierp | 0:33f2abdc6bf9 | 86 | logInfo("changing TX frequency from %lu to %lu", current_tx_frequency, tx_frequency); |
javierp | 0:33f2abdc6bf9 | 87 | if (dot->setTxFrequency(tx_frequency) != mDot::MDOT_OK) { |
javierp | 0:33f2abdc6bf9 | 88 | logError("failed to set TX frequency to %lu", tx_frequency); |
javierp | 0:33f2abdc6bf9 | 89 | } |
javierp | 0:33f2abdc6bf9 | 90 | } |
javierp | 0:33f2abdc6bf9 | 91 | |
javierp | 0:33f2abdc6bf9 | 92 | if (current_tx_datarate != tx_datarate) { |
javierp | 0:33f2abdc6bf9 | 93 | logInfo("changing TX datarate from %u to %u", current_tx_datarate, tx_datarate); |
javierp | 0:33f2abdc6bf9 | 94 | if (dot->setTxDataRate(tx_datarate) != mDot::MDOT_OK) { |
javierp | 0:33f2abdc6bf9 | 95 | logError("failed to set TX datarate to %u", tx_datarate); |
javierp | 0:33f2abdc6bf9 | 96 | } |
javierp | 0:33f2abdc6bf9 | 97 | } |
javierp | 0:33f2abdc6bf9 | 98 | |
javierp | 0:33f2abdc6bf9 | 99 | if (current_tx_power != tx_power) { |
javierp | 0:33f2abdc6bf9 | 100 | logInfo("changing TX power from %u to %u", current_tx_power, tx_power); |
javierp | 0:33f2abdc6bf9 | 101 | if (dot->setTxPower(tx_power) != mDot::MDOT_OK) { |
javierp | 0:33f2abdc6bf9 | 102 | logError("failed to set TX power to %u", tx_power); |
javierp | 0:33f2abdc6bf9 | 103 | } |
javierp | 0:33f2abdc6bf9 | 104 | } |
javierp | 0:33f2abdc6bf9 | 105 | } |
javierp | 0:33f2abdc6bf9 | 106 | |
javierp | 0:33f2abdc6bf9 | 107 | void join_network() { |
javierp | 0:33f2abdc6bf9 | 108 | int32_t j_attempts = 0; |
javierp | 0:33f2abdc6bf9 | 109 | int32_t ret = mDot::MDOT_ERROR; |
javierp | 0:33f2abdc6bf9 | 110 | |
javierp | 0:33f2abdc6bf9 | 111 | // attempt to join the network |
javierp | 0:33f2abdc6bf9 | 112 | while (ret != mDot::MDOT_OK) { |
javierp | 0:33f2abdc6bf9 | 113 | logInfo("attempt %d to join network", ++j_attempts); |
javierp | 0:33f2abdc6bf9 | 114 | ret = dot->joinNetwork(); |
javierp | 0:33f2abdc6bf9 | 115 | if (ret != mDot::MDOT_OK) { |
javierp | 0:33f2abdc6bf9 | 116 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
javierp | 0:33f2abdc6bf9 | 117 | // in some frequency bands we need to wait until another channel is available before transmitting again |
javierp | 0:33f2abdc6bf9 | 118 | uint32_t delay_s = (dot->getNextTxMs() / 1000) + 1; |
javierp | 0:33f2abdc6bf9 | 119 | if (delay_s < 2) { |
javierp | 0:33f2abdc6bf9 | 120 | logInfo("waiting %lu s until next free channel", delay_s); |
javierp | 0:33f2abdc6bf9 | 121 | wait(delay_s); |
javierp | 0:33f2abdc6bf9 | 122 | } else { |
javierp | 0:33f2abdc6bf9 | 123 | logInfo("sleeping %lu s until next free channel", delay_s); |
javierp | 0:33f2abdc6bf9 | 124 | dot->sleep(delay_s, mDot::RTC_ALARM, false); |
javierp | 0:33f2abdc6bf9 | 125 | } |
javierp | 0:33f2abdc6bf9 | 126 | } |
javierp | 0:33f2abdc6bf9 | 127 | } |
javierp | 0:33f2abdc6bf9 | 128 | } |
javierp | 0:33f2abdc6bf9 | 129 | |
javierp | 0:33f2abdc6bf9 | 130 | void send_data(std::vector<uint8_t> data) { |
javierp | 0:33f2abdc6bf9 | 131 | int32_t ret; |
javierp | 0:33f2abdc6bf9 | 132 | |
javierp | 0:33f2abdc6bf9 | 133 | ret = dot->send(data); |
javierp | 0:33f2abdc6bf9 | 134 | if (ret != mDot::MDOT_OK) { |
javierp | 0:33f2abdc6bf9 | 135 | logError("failed to send data to %s [%d][%s]", dot->getJoinMode() == mDot::PEER_TO_PEER ? "peer" : "gateway", ret, mDot::getReturnCodeString(ret).c_str()); |
javierp | 0:33f2abdc6bf9 | 136 | } else { |
javierp | 0:33f2abdc6bf9 | 137 | logInfo("successfully sent data to %s", dot->getJoinMode() == mDot::PEER_TO_PEER ? "peer" : "gateway"); |
javierp | 0:33f2abdc6bf9 | 138 | } |
javierp | 0:33f2abdc6bf9 | 139 | } |
javierp | 0:33f2abdc6bf9 | 140 |