Aaron Headley / mDot_test_Loriot

Dependencies:   libmDot mbed-rtos mbed-src

Fork of mDot_test by Semtech

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 <string>
00006 #include <vector>
00007 
00008 using namespace mts;
00009 
00010 /* ********************************************** */
00011 /* Create AppEUI identifier and Assigned AppKey   */
00012 /* ********************************************** */
00013 
00014 #define USE_OTAA 0 // 1 for OtAA and 0 for Personalized
00015 
00016 static const uint8_t DEVKEY[16] = { 0x3D, 0x4B, 0x87, 0x8B, 0x56, 0xEA, 0xA8, 0x1D, 0xA9, 0x7F, 0xB1, 0x9E, 0x69, 0xF8, 0x23, 0x6B }; // Normal
00017 static const uint8_t APPEUI[8]  = { 0xBE, 0x7A, 0x00, 0x00, 0x00, 0x00, 0x02, 0x83  }; // SMTC AppEUI - Most Significant Byte First
00018 
00019 //DevAddr  01DC266E 
00020 //NWKSKEY  C7 58 CB 09 BD F2 C7 0E 5F 4C FA 5B 7A 96 68 D8
00021 //APPSKEY  AC B3 B3 A1 B3 26 25 F7 24 3F B4 39 65 06 38 A7
00022 static const uint8_t DEV_ADDR[4] = { 0x01, 0xDC, 0x26, 0x6E };
00023 static const uint8_t NETSKEY[16] = { 0xC7, 0x58, 0xCB, 0x09, 0xBD, 0xF2, 0xC7, 0x0E, 0x5F, 0x4C, 0xFA, 0x5B, 0x7A, 0x96, 0x68, 0xD8 }; // Normal
00024 static const uint8_t APPSKEY[16] = { 0xAC, 0xB3, 0xB3, 0xA1, 0xB3, 0x26, 0x25, 0xF7, 0x24, 0x3F, 0xB4, 0x39, 0x65, 0x06, 0x38, 0xA7 }; // Normal
00025 
00026 //DevAddr  01DC266E 
00027 //NWKSKEY  2b 7e 15 16 28 ae d2 a6 ab f7 15 88 09 cf 4f 3c 
00028 //APPSKEY  2b 7e 15 16 28 ae d2 a6 ab f7 15 88 09 cf 4f 3c 
00029 //static const uint8_t DEV_ADDR[4] = { 0x01, 0xDC, 0x26, 0x6E };
00030 //static const uint8_t NETSKEY[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }; // Normal
00031 //static const uint8_t APPSKEY[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }; // Normal
00032 
00033 static uint8_t       config_frequency_sub_band = 1; // 0 = Enable all channels, 1 = 1st eight channels
00034 
00035 /* ********************************************** */
00036 
00037 static std::vector<uint8_t> AppKey (DEVKEY, DEVKEY + sizeof(DEVKEY)/sizeof(uint8_t) );
00038 static std::vector<uint8_t> AppEUI (APPEUI, APPEUI + sizeof(APPEUI)/sizeof(uint8_t) ); 
00039 
00040 static std::vector<uint8_t> DevAddr (DEV_ADDR, DEV_ADDR + sizeof(DEV_ADDR)/sizeof(uint8_t) );
00041 static std::vector<uint8_t> NetSKey (NETSKEY, NETSKEY + sizeof(NETSKEY)/sizeof(uint8_t) ); 
00042 static std::vector<uint8_t> AppSKey (APPSKEY, APPSKEY + sizeof(APPSKEY)/sizeof(uint8_t) );
00043 
00044 
00045 
00046 InterruptIn button(PB_1);
00047 DigitalOut  drive_high(PB_0);
00048 DigitalIn   door_open(PA_7);
00049 
00050 volatile bool msg_rdy;
00051 
00052 void queue_message(){
00053     msg_rdy = true;
00054 }
00055     
00056 
00057 int main() {
00058     Serial debug(USBTX, USBRX);
00059     debug.baud(115200);
00060     drive_high = 1;
00061     
00062     msg_rdy = false;
00063     int32_t ret;
00064     int32_t next_tx;
00065     int32_t wait_time = 2;
00066     mDot* dot;
00067     std::vector<uint8_t> send_data;
00068     std::vector<uint8_t> recv_data;
00069     uint8_t recv = 0;
00070     uint8_t recv_mismatch = 0;
00071     uint8_t send_failure = 0;
00072     uint8_t iterations = 50;
00073     button.rise(&queue_message);
00074     
00075     send_data.push_back(0x01);
00076     send_data.push_back(0x02);
00077 
00078     dot = mDot::getInstance();
00079 
00080     dot->resetConfig();
00081     
00082     dot->setLogLevel(MTSLog::TRACE_LEVEL);
00083     //dot->setJoinByteOrder(1); // MSB
00084     dot->setTxDataRate(mDot::SF_10);
00085     //dot->setTxPower(20);
00086     
00087     while ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
00088         logError("failed to set frequency sub band: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00089     }
00090     while ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
00091         logError("failed to set Public Network: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00092     }
00093     /** Set network ID
00094      * for use with OTA & AUTO_OTA network join modes
00095      * setting network ID via this function sets network name to empty
00096      * @param id a vector of 8 bytes
00097      * @returns MDOT_OK if success
00098      */
00099 #if USE_OTAA == 1
00100     while ((ret = dot->setNetworkId(AppEUI)) != mDot::MDOT_OK) {
00101         logError("failed to set AppEUI: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00102     }
00103     while ((ret = dot->setNetworkKey(AppKey)) != mDot::MDOT_OK) {
00104         logError("failed to set AppKey: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00105     }
00106     dot->setJoinMode( dot->OTA ); // 0 = Manual and 1 = OTA
00107 #else
00108     while ((ret = dot->setNetworkAddress(DevAddr)) != mDot::MDOT_OK) {
00109         logError("failed to set DevAddr: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00110     }
00111       while ((ret = dot->setNetworkKey(AppKey)) != mDot::MDOT_OK) {
00112         logError("failed to set AppKey: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00113     }
00114     while ((ret = dot->setNetworkSessionKey(NetSKey)) != mDot::MDOT_OK) {
00115         logError("failed to set NetworkSessionKey: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00116     }
00117     while ((ret = dot->setDataSessionKey(AppSKey)) != mDot::MDOT_OK) {
00118         logError("failed to set App Session Key: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00119     }
00120     dot->setJoinMode( dot->MANUAL ); // 0 = Manual and 1 = OtAA
00121 #endif
00122 
00123     logInfo("enabling activity LED");
00124     dot->setActivityLedEnable(true);
00125 
00126     logInfo("joining network");
00127     while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
00128         logError("failed to join network: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00129         wait_ms(dot->getNextTxMs() + 1);
00130     }
00131     logInfo("joined");
00132 
00133     //dot->setAck(3); // Use Confirmed frames and try three times
00134 
00135     while (1) {
00136 
00137         send_data[0] = 192; //door_open;
00138         if ((ret = dot->send(send_data)) != mDot::MDOT_OK) {
00139             logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00140             send_failure++;
00141         } else {
00142             logInfo("send data: %s", Text::bin2hexString(send_data).c_str());
00143             if ((ret = dot->recv(recv_data)) != mDot::MDOT_OK) {
00144                 logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00145             } else {
00146                 logInfo("recv data: %s", Text::bin2hexString(recv_data).c_str());
00147                 if (recv_data[0] == 0x80) {
00148                     recv++;
00149                 } else if (recv_data[0] == 0xFF) {
00150                     goto END;
00151                 } else {
00152                     recv_mismatch++;
00153                 }
00154             }
00155             recv_data.clear();
00156         }
00157         msg_rdy = true; // Set to 'true' for free running.
00158         wait(wait_time);
00159         while (!msg_rdy) wait(wait_time);
00160     }
00161 END:    
00162     logInfo("Version: %s", dot->getId().c_str());
00163     logInfo("Recv: %d/%d", recv, iterations);
00164     logInfo("Recv Mismatch: %d/%d", recv_mismatch, iterations);
00165     logInfo("Send Failure: %d/%d", send_failure, iterations);
00166     logInfo("Dropped: %d/%d", iterations - (recv + recv_mismatch + send_failure), iterations);
00167 
00168     return 0;
00169 }