peer to peer rx terminal

Dependencies:   ISL29011 libxDot-dev-mbed5-deprecated

Fork of peer-to-peer_rxtx by Natalia Requejo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "mDot.h"
00003 #include "MTSLog.h"
00004 #include "MTSText.h"
00005 #include "RadioEvent.h"
00006 #include "ChannelPlans.h"
00007 #include <string>
00008 #include <vector>
00009 
00010 #define PEER_TO_PEER_SAMPLE 1
00011  //#define OTA
00012 using namespace mts;
00013 #if defined(PEER_TO_PEER_SAMPLE)
00014 static uint8_t network_address[] = { 0x01, 0x02, 0x03, 0x04 };
00015 static uint8_t network_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };
00016 static uint8_t data_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };
00017 #else
00018 
00019 static std::string config_network_name = "conduitUNSAM";
00020 static std::string config_network_pass = "conduitUNSAM";
00021 static uint8_t config_frequency_sub_band = 4;
00022 #endif
00023 
00024 mDot* dot;
00025 lora::ChannelPlan* plan = NULL;
00026 
00027 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) {
00028     std::vector<uint8_t> current_network_address = dot->getNetworkAddress();
00029     std::vector<uint8_t> current_network_session_key = dot->getNetworkSessionKey();
00030     std::vector<uint8_t> current_data_session_key = dot->getDataSessionKey();
00031     uint32_t current_tx_frequency = dot->getTxFrequency();
00032     uint8_t current_tx_datarate = dot->getTxDataRate();
00033     uint8_t current_tx_power = dot->getTxPower();
00034 
00035     std::vector<uint8_t> network_address_vector(network_address, network_address + 4);
00036     std::vector<uint8_t> network_session_key_vector(network_session_key, network_session_key + 16);
00037     std::vector<uint8_t> data_session_key_vector(data_session_key, data_session_key + 16);
00038 
00039     if (current_network_address != network_address_vector) {
00040         logInfo("changing network address from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_network_address).c_str(), mts::Text::bin2hexString(network_address_vector).c_str());
00041         if (dot->setNetworkAddress(network_address_vector) != mDot::MDOT_OK) {
00042             logError("failed to set network address to \"%s\"", mts::Text::bin2hexString(network_address_vector).c_str());
00043         }
00044     }
00045     
00046     if (current_network_session_key != network_session_key_vector) {
00047         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());
00048         if (dot->setNetworkSessionKey(network_session_key_vector) != mDot::MDOT_OK) {
00049             logError("failed to set network session key to \"%s\"", mts::Text::bin2hexString(network_session_key_vector).c_str());
00050         }
00051     }
00052     
00053     if (current_data_session_key != data_session_key_vector) {
00054         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());
00055         if (dot->setDataSessionKey(data_session_key_vector) != mDot::MDOT_OK) {
00056             logError("failed to set data session key to \"%s\"", mts::Text::bin2hexString(data_session_key_vector).c_str());
00057         }
00058     }
00059     
00060     if (current_tx_frequency != tx_frequency) {
00061     logInfo("changing TX frequency from %lu to %lu", current_tx_frequency, tx_frequency);
00062     if (dot->setTxFrequency(tx_frequency) != mDot::MDOT_OK) {
00063         logError("failed to set TX frequency to %lu", tx_frequency);
00064     }
00065     }
00066 
00067     if (current_tx_datarate != tx_datarate) {
00068     logInfo("changing TX datarate from %u to %u", current_tx_datarate, tx_datarate);
00069     if (dot->setTxDataRate(tx_datarate) != mDot::MDOT_OK) {
00070         logError("failed to set TX datarate to %u", tx_datarate);
00071     }
00072     }
00073 
00074     if (current_tx_power != tx_power) {
00075     logInfo("changing TX power from %u to %u", current_tx_power, tx_power);
00076     if (dot->setTxPower(tx_power) != mDot::MDOT_OK) {
00077         logError("failed to set TX power to %u", tx_power);
00078     }
00079     }
00080 }
00081 
00082 int main() {
00083     Serial debug(USBTX, USBRX);
00084     debug.baud(115200);
00085     
00086     RadioEvent events;
00087     
00088     int32_t ret;
00089     int32_t next_tx;
00090     int32_t wait_time = 2;
00091     
00092     std::vector<uint8_t> send_data;
00093     std::vector<uint8_t> recv_data;
00094     uint8_t recv = 0;
00095     uint8_t recv_mismatch = 0;
00096     uint8_t send_failure = 0;
00097     uint8_t iterations = 5;
00098     
00099     send_data.push_back(0x00);
00100     send_data.push_back('N');
00101     send_data.push_back('A');
00102     send_data.push_back('T');
00103     send_data.push_back('I');
00104     send_data.push_back(' ');
00105     send_data.push_back(' ');
00106     send_data.push_back(' ');
00107     
00108     plan = new lora::ChannelPlan_US915();
00109 
00110     dot = mDot::getInstance(plan);
00111     logInfo("defaulting Dot configuration");
00112     dot->resetConfig();
00113     //si se hace un deep sleep la aplicacion inicia desde el principio.
00114     //Y no desde el punto donde se ejecuto el deep_sleep
00115     //Por esto es necesario salvar la sesion y luego recuperarla para continuar trabajando
00116     //dot->resetNetworkSession();
00117     //dot->restoreNetworkSession();
00118     //dot->saveNetworkSession();
00119     dot->setLogLevel(MTSLog::INFO_LEVEL);
00120     
00121  #if defined(PEER_TO_PEER_SAMPLE)
00122     // attach the custom events handler
00123      dot->setEvents(&events);
00124     
00125     //Peer-to-peer    
00126     // Actualizamos la configuracion de Join unicamente si es diferente 
00127      //( 0= Manual, 1 = OTA, 2= AUTO_OTA, 3=Peer to Peer)
00128     if (dot->getJoinMode() != mDot::PEER_TO_PEER) {
00129         logInfo("changing network join mode to PEER_TO_PEER");
00130         if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) {
00131             logError("failed to set network join mode to PEER_TO_PEER");
00132         }
00133     }
00134     uint32_t tx_frequency= 915500000;
00135     uint8_t tx_datarate = lora::DR_13;
00136     uint8_t tx_power = 20;
00137     
00138     update_peer_to_peer_config(network_address, network_session_key, data_session_key, tx_frequency, tx_datarate, tx_power);
00139 
00140  #else
00141     // Actualizamos la configuracion de Join unicamente si es diferente 
00142     //( 0= Manual, 1 = OTA, 2= AUTO_OTA, 3=Peer to Peer)
00143     if (dot->getJoinMode() != mDot::AUTO_OTA) {
00144         logInfo("changing network join mode to AUTO_OTA");
00145         if (dot->setJoinMode(mDot::AUTO_OTA) != mDot::MDOT_OK) {
00146             logError("failed to set network join mode to AUTO_OTA");
00147         }
00148     }
00149     while ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
00150         logError("failed to set frequency sub band: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00151     }
00152     while ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
00153         logError("failed to set network name: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00154     }
00155     while ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
00156         logError("failed to set network password: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00157     }
00158     while ((ret = dot->setPublicNetwork(1)) != mDot::MDOT_OK) {
00159         logError("failed to set public network : [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00160     }
00161     
00162  #endif
00163         
00164     // save changes to configuration
00165     logInfo("saving configuration");
00166     if (!dot->saveConfig()) {
00167         logError("failed to save configuration");
00168     }
00169     
00170     //logInfo("enabling activity LED");
00171     dot->setActivityLedEnable(true);
00172  
00173     logInfo("joining network");
00174     while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
00175         logError("failed to join network: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00176         wait_ms(dot->getNextTxMs() + 1);
00177     }
00178     logInfo("joined");
00179  
00180     while(1) {
00181         std::string rxxdata = events.print();
00182         next_tx = dot->getNextTxMs() + 1;
00183         logInfo("Datos : %s", rxxdata);
00184         wait_ms(5000);
00185         logInfo("waiting data \n");
00186         wait_ms(100);
00187     }
00188     
00189     logInfo("Version: %s", dot->getId().c_str());
00190     logInfo("Recv: %d/%d", recv, iterations);
00191     logInfo("Recv Mismatch: %d/%d", recv_mismatch, iterations);
00192     logInfo("Send Failure: %d/%d", send_failure, iterations);
00193     logInfo("Dropped: %d/%d", iterations - (recv + recv_mismatch + send_failure), iterations);
00194  
00195     return 0;
00196 }