Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: libmDot mbed-rtos mbed-src
Fork of mDot_test_rx by
main.cpp@7:4c9cead936d8, 2016-03-04 (annotated)
- Committer:
- jknapp_smtc
- Date:
- Fri Mar 04 00:59:45 2016 +0000
- Revision:
- 7:4c9cead936d8
- Parent:
- 6:b1afa2592e36
- Child:
- 8:4b47a8973f91
Changed to Personalization as Default mode
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| mfiore | 0:e17e5a07892d | 1 | #include "mbed.h" | 
| mfiore | 0:e17e5a07892d | 2 | #include "mDot.h" | 
| mfiore | 0:e17e5a07892d | 3 | #include "MTSLog.h" | 
| mfiore | 0:e17e5a07892d | 4 | #include "MTSText.h" | 
| mfiore | 0:e17e5a07892d | 5 | #include <string> | 
| mfiore | 0:e17e5a07892d | 6 | #include <vector> | 
| mfiore | 0:e17e5a07892d | 7 | |
| mfiore | 0:e17e5a07892d | 8 | using namespace mts; | 
| mfiore | 0:e17e5a07892d | 9 | |
| jknapp_smtc | 4:ee6bf074135c | 10 | /* ********************************************** */ | 
| jknapp_smtc | 4:ee6bf074135c | 11 | /* Create AppEUI identifier and Assigned AppKey */ | 
| jknapp_smtc | 4:ee6bf074135c | 12 | /* ********************************************** */ | 
| jknapp_smtc | 4:ee6bf074135c | 13 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 14 | static const uint8_t DEVKEY[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF }; // Normal | 
| jknapp_smtc | 4:ee6bf074135c | 15 | static const uint8_t APPEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // SMTC AppEUI - Most Significant Byte First | 
| jknapp_smtc | 7:4c9cead936d8 | 16 | |
| jknapp_smtc | 7:4c9cead936d8 | 17 | //DevAddr 01DC266E | 
| jknapp_smtc | 7:4c9cead936d8 | 18 | //NWKSKEY C7 58 CB 09 BD F2 C7 0E 5F 4C FA 5B 7A 96 68 D8 | 
| jknapp_smtc | 7:4c9cead936d8 | 19 | //APPSKEY AC B3 B3 A1 B3 26 25 F7 24 3F B4 39 65 06 38 A7 | 
| jknapp_smtc | 7:4c9cead936d8 | 20 | static const uint8_t DEV_ADDR[4] = { 0x01, 0xDC, 0x26, 0x6E }; | 
| jknapp_smtc | 7:4c9cead936d8 | 21 | static const uint8_t NETSKEY[16] = { 0xC7, 0x58, 0xCB, 0x09, 0xBD, 0xF2, 0xC7, 0x0E, 0x5F, 0x4C, 0xFA, 0x5B, 0x7A, 0x96, 0x68, 0xD8 }; // Normal | 
| jknapp_smtc | 7:4c9cead936d8 | 22 | static const uint8_t APPSKEY[16] = { 0xAC, 0xB3, 0xB3, 0xA1, 0xB3, 0x26, 0x25, 0xF7, 0x24, 0x3F, 0xB4, 0x39, 0x65, 0x06, 0x38, 0xA7 }; // Normal | 
| jknapp_smtc | 7:4c9cead936d8 | 23 | |
| jknapp_smtc | 7:4c9cead936d8 | 24 | static uint8_t config_frequency_sub_band = 1; // 0 = Enable all channels, 1 = 1st eight channels | 
| jknapp_smtc | 4:ee6bf074135c | 25 | |
| jknapp_smtc | 4:ee6bf074135c | 26 | /* ********************************************** */ | 
| jknapp_smtc | 3:7cc5dfd22b54 | 27 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 28 | static std::vector<uint8_t> AppKey (DEVKEY, DEVKEY + sizeof(DEVKEY)/sizeof(uint8_t) ); | 
| jknapp_smtc | 3:7cc5dfd22b54 | 29 | static std::vector<uint8_t> AppEUI (APPEUI, APPEUI + sizeof(APPEUI)/sizeof(uint8_t) ); | 
| jknapp_smtc | 3:7cc5dfd22b54 | 30 | |
| jknapp_smtc | 7:4c9cead936d8 | 31 | static std::vector<uint8_t> DevAddr (DEV_ADDR, DEV_ADDR + sizeof(DEV_ADDR)/sizeof(uint8_t) ); | 
| jknapp_smtc | 7:4c9cead936d8 | 32 | static std::vector<uint8_t> NetSKey (NETSKEY, NETSKEY + sizeof(NETSKEY)/sizeof(uint8_t) ); | 
| jknapp_smtc | 7:4c9cead936d8 | 33 | static std::vector<uint8_t> AppSKey (APPSKEY, APPSKEY + sizeof(APPSKEY)/sizeof(uint8_t) ); | 
| jknapp_smtc | 7:4c9cead936d8 | 34 | |
| jknapp_smtc | 7:4c9cead936d8 | 35 | |
| jknapp_smtc | 7:4c9cead936d8 | 36 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 37 | InterruptIn button(PB_1); | 
| jknapp_smtc | 3:7cc5dfd22b54 | 38 | DigitalOut drive_high(PB_0); | 
| jknapp_smtc | 3:7cc5dfd22b54 | 39 | DigitalIn door_open(PA_7); | 
| jknapp_smtc | 3:7cc5dfd22b54 | 40 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 41 | volatile bool msg_rdy; | 
| jknapp_smtc | 3:7cc5dfd22b54 | 42 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 43 | void queue_message(){ | 
| jknapp_smtc | 3:7cc5dfd22b54 | 44 | msg_rdy = true; | 
| jknapp_smtc | 3:7cc5dfd22b54 | 45 | } | 
| jknapp_smtc | 3:7cc5dfd22b54 | 46 | |
| mfiore | 0:e17e5a07892d | 47 | |
| mfiore | 0:e17e5a07892d | 48 | int main() { | 
| mfiore | 0:e17e5a07892d | 49 | Serial debug(USBTX, USBRX); | 
| mfiore | 0:e17e5a07892d | 50 | debug.baud(460800); | 
| jknapp_smtc | 3:7cc5dfd22b54 | 51 | drive_high = 1; | 
| mfiore | 0:e17e5a07892d | 52 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 53 | msg_rdy = false; | 
| mfiore | 0:e17e5a07892d | 54 | int32_t ret; | 
| mfiore | 0:e17e5a07892d | 55 | int32_t next_tx; | 
| mfiore | 2:aadbdfb6d517 | 56 | int32_t wait_time = 2; | 
| mfiore | 0:e17e5a07892d | 57 | mDot* dot; | 
| mfiore | 0:e17e5a07892d | 58 | std::vector<uint8_t> send_data; | 
| mfiore | 0:e17e5a07892d | 59 | std::vector<uint8_t> recv_data; | 
| mfiore | 0:e17e5a07892d | 60 | uint8_t recv = 0; | 
| mfiore | 0:e17e5a07892d | 61 | uint8_t recv_mismatch = 0; | 
| mfiore | 0:e17e5a07892d | 62 | uint8_t send_failure = 0; | 
| mfiore | 0:e17e5a07892d | 63 | uint8_t iterations = 50; | 
| jknapp_smtc | 3:7cc5dfd22b54 | 64 | button.rise(&queue_message); | 
| mfiore | 0:e17e5a07892d | 65 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 66 | send_data.push_back(0x01); | 
| jknapp_smtc | 3:7cc5dfd22b54 | 67 | send_data.push_back(0x02); | 
| mfiore | 0:e17e5a07892d | 68 | |
| mfiore | 0:e17e5a07892d | 69 | dot = mDot::getInstance(); | 
| mfiore | 0:e17e5a07892d | 70 | |
| mfiore | 0:e17e5a07892d | 71 | dot->resetConfig(); | 
| mfiore | 1:8295b8c0d802 | 72 | |
| mfiore | 1:8295b8c0d802 | 73 | dot->setLogLevel(MTSLog::TRACE_LEVEL); | 
| jknapp_smtc | 3:7cc5dfd22b54 | 74 | //dot->setJoinByteOrder(1); // MSB | 
| jknapp_smtc | 3:7cc5dfd22b54 | 75 | dot->setTxDataRate(mDot::SF_10); | 
| jknapp_smtc | 3:7cc5dfd22b54 | 76 | //dot->setTxPower(20); | 
| jknapp_smtc | 3:7cc5dfd22b54 | 77 | |
| mfiore | 0:e17e5a07892d | 78 | while ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { | 
| mfiore | 0:e17e5a07892d | 79 | logError("failed to set frequency sub band: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); | 
| mfiore | 0:e17e5a07892d | 80 | } | 
| jknapp_smtc | 3:7cc5dfd22b54 | 81 | while ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { | 
| jknapp_smtc | 3:7cc5dfd22b54 | 82 | logError("failed to set Public Network: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); | 
| mfiore | 0:e17e5a07892d | 83 | } | 
| jknapp_smtc | 3:7cc5dfd22b54 | 84 | /** Set network ID | 
| jknapp_smtc | 3:7cc5dfd22b54 | 85 | * for use with OTA & AUTO_OTA network join modes | 
| jknapp_smtc | 3:7cc5dfd22b54 | 86 | * setting network ID via this function sets network name to empty | 
| jknapp_smtc | 3:7cc5dfd22b54 | 87 | * @param id a vector of 8 bytes | 
| jknapp_smtc | 3:7cc5dfd22b54 | 88 | * @returns MDOT_OK if success | 
| jknapp_smtc | 3:7cc5dfd22b54 | 89 | */ | 
| jknapp_smtc | 7:4c9cead936d8 | 90 | // while ((ret = dot->setNetworkId(AppEUI)) != mDot::MDOT_OK) { | 
| jknapp_smtc | 7:4c9cead936d8 | 91 | // logError("failed to set AppEUI: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); | 
| jknapp_smtc | 7:4c9cead936d8 | 92 | // } | 
| jknapp_smtc | 7:4c9cead936d8 | 93 | // while ((ret = dot->setNetworkKey(AppKey)) != mDot::MDOT_OK) { | 
| jknapp_smtc | 7:4c9cead936d8 | 94 | // logError("failed to set AppEUI: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); | 
| jknapp_smtc | 7:4c9cead936d8 | 95 | // } | 
| jknapp_smtc | 7:4c9cead936d8 | 96 | while ((ret = dot->setNetworkAddress(DevAddr)) != mDot::MDOT_OK) { | 
| jknapp_smtc | 7:4c9cead936d8 | 97 | logError("failed to set DevAddr: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); | 
| jknapp_smtc | 3:7cc5dfd22b54 | 98 | } | 
| jknapp_smtc | 7:4c9cead936d8 | 99 | while ((ret = dot->setNetworkSessionKey(NetSKey)) != mDot::MDOT_OK) { | 
| jknapp_smtc | 7:4c9cead936d8 | 100 | logError("failed to set NetworkSessionKey: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); | 
| jknapp_smtc | 7:4c9cead936d8 | 101 | } | 
| jknapp_smtc | 7:4c9cead936d8 | 102 | while ((ret = dot->setDataSessionKey(AppSKey)) != mDot::MDOT_OK) { | 
| jknapp_smtc | 7:4c9cead936d8 | 103 | logError("failed to set App Session Key: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); | 
| mfiore | 0:e17e5a07892d | 104 | } | 
| mfiore | 0:e17e5a07892d | 105 | |
| mfiore | 2:aadbdfb6d517 | 106 | logInfo("enabling activity LED"); | 
| mfiore | 2:aadbdfb6d517 | 107 | dot->setActivityLedEnable(true); | 
| mfiore | 2:aadbdfb6d517 | 108 | |
| mfiore | 0:e17e5a07892d | 109 | logInfo("joining network"); | 
| jknapp_smtc | 7:4c9cead936d8 | 110 | dot->setJoinMode( dot->MANUAL ); // 0 = Manual and 1 = OtAA | 
| mfiore | 0:e17e5a07892d | 111 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { | 
| mfiore | 0:e17e5a07892d | 112 | logError("failed to join network: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); | 
| mfiore | 0:e17e5a07892d | 113 | wait_ms(dot->getNextTxMs() + 1); | 
| mfiore | 0:e17e5a07892d | 114 | } | 
| mfiore | 0:e17e5a07892d | 115 | logInfo("joined"); | 
| mfiore | 0:e17e5a07892d | 116 | |
| jknapp_smtc | 7:4c9cead936d8 | 117 | //dot->setAck(3); // Use Confirmed frames and try three times | 
| jknapp_smtc | 3:7cc5dfd22b54 | 118 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 119 | while (1) { | 
| jknapp_smtc | 3:7cc5dfd22b54 | 120 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 121 | send_data[0] = 192; //door_open; | 
| mfiore | 0:e17e5a07892d | 122 | if ((ret = dot->send(send_data)) != mDot::MDOT_OK) { | 
| mfiore | 0:e17e5a07892d | 123 | logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); | 
| mfiore | 0:e17e5a07892d | 124 | send_failure++; | 
| mfiore | 0:e17e5a07892d | 125 | } else { | 
| mfiore | 0:e17e5a07892d | 126 | logInfo("send data: %s", Text::bin2hexString(send_data).c_str()); | 
| mfiore | 0:e17e5a07892d | 127 | if ((ret = dot->recv(recv_data)) != mDot::MDOT_OK) { | 
| mfiore | 0:e17e5a07892d | 128 | logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); | 
| mfiore | 0:e17e5a07892d | 129 | } else { | 
| mfiore | 0:e17e5a07892d | 130 | logInfo("recv data: %s", Text::bin2hexString(recv_data).c_str()); | 
| jknapp_smtc | 3:7cc5dfd22b54 | 131 | if (recv_data[0] == 0x80) { | 
| mfiore | 0:e17e5a07892d | 132 | recv++; | 
| jknapp_smtc | 3:7cc5dfd22b54 | 133 | } else if (recv_data[0] == 0xFF) { | 
| jknapp_smtc | 3:7cc5dfd22b54 | 134 | goto END; | 
| mfiore | 0:e17e5a07892d | 135 | } else { | 
| mfiore | 0:e17e5a07892d | 136 | recv_mismatch++; | 
| mfiore | 0:e17e5a07892d | 137 | } | 
| mfiore | 0:e17e5a07892d | 138 | } | 
| mfiore | 0:e17e5a07892d | 139 | recv_data.clear(); | 
| mfiore | 0:e17e5a07892d | 140 | } | 
| jknapp_smtc | 6:b1afa2592e36 | 141 | msg_rdy = true; // Set to 'true' for free running. | 
| jknapp_smtc | 5:25114cb5e9a7 | 142 | wait(wait_time); | 
| jknapp_smtc | 3:7cc5dfd22b54 | 143 | while (!msg_rdy) wait(wait_time); | 
| mfiore | 0:e17e5a07892d | 144 | } | 
| jknapp_smtc | 3:7cc5dfd22b54 | 145 | END: | 
| mfiore | 0:e17e5a07892d | 146 | logInfo("Version: %s", dot->getId().c_str()); | 
| mfiore | 0:e17e5a07892d | 147 | logInfo("Recv: %d/%d", recv, iterations); | 
| mfiore | 0:e17e5a07892d | 148 | logInfo("Recv Mismatch: %d/%d", recv_mismatch, iterations); | 
| mfiore | 0:e17e5a07892d | 149 | logInfo("Send Failure: %d/%d", send_failure, iterations); | 
| mfiore | 0:e17e5a07892d | 150 | logInfo("Dropped: %d/%d", iterations - (recv + recv_mismatch + send_failure), iterations); | 
| mfiore | 0:e17e5a07892d | 151 | |
| mfiore | 0:e17e5a07892d | 152 | return 0; | 
| mfiore | 0:e17e5a07892d | 153 | } | 
