fp
Dependencies: libmDot-mbed5 ISL29011
examples/src/dot_util.cpp@0:a151a6350d7f, 2016-10-05 (annotated)
- Committer:
- mfiore
- Date:
- Wed Oct 05 21:07:50 2016 +0000
- Revision:
- 0:a151a6350d7f
- Child:
- 1:c4915e00d2ce
initial commit of OTA and AUTO_OTA examples
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 0:a151a6350d7f | 1 | #include "dot_util.h" |
mfiore | 0:a151a6350d7f | 2 | |
mfiore | 0:a151a6350d7f | 3 | void display_config() { |
mfiore | 0:a151a6350d7f | 4 | // display configuration and library version information |
mfiore | 0:a151a6350d7f | 5 | logInfo("version: %s", dot->getId().c_str()); |
mfiore | 0:a151a6350d7f | 6 | logInfo("general configuration"); |
mfiore | 0:a151a6350d7f | 7 | logInfo("---------------------"); |
mfiore | 0:a151a6350d7f | 8 | logInfo("\tdevice ID/EUI: %s", mts::Text::bin2hexString(dot->getDeviceId()).c_str()); |
mfiore | 0:a151a6350d7f | 9 | logInfo("\tfrequency band: %s", mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str()); |
mfiore | 0:a151a6350d7f | 10 | logInfo("\tfrequency sub band: %u", dot->getFrequencySubBand()); |
mfiore | 0:a151a6350d7f | 11 | logInfo("\tpublic network: %s", dot->getPublicNetwork() == true ? "on" : "off"); |
mfiore | 0:a151a6350d7f | 12 | logInfo("credentials configuration"); |
mfiore | 0:a151a6350d7f | 13 | logInfo("-------------------------"); |
mfiore | 0:a151a6350d7f | 14 | logInfo("\tnetwork name: %s", dot->getNetworkName().c_str()); |
mfiore | 0:a151a6350d7f | 15 | logInfo("\tnetwork phrase: %s", dot->getNetworkPassphrase().c_str()); |
mfiore | 0:a151a6350d7f | 16 | logInfo("\tnetwork join mode: %s", mDot::JoinModeStr(dot->getJoinMode()).c_str()); |
mfiore | 0:a151a6350d7f | 17 | logInfo("communication parameters"); |
mfiore | 0:a151a6350d7f | 18 | logInfo("------------------------"); |
mfiore | 0:a151a6350d7f | 19 | logInfo("\tacks: %s, %u attempts", dot->getAck() > 0 ? "on" : "off", dot->getAck()); |
mfiore | 0:a151a6350d7f | 20 | logInfo("\tTX datarate: %s", mDot::DataRateStr(dot->getTxDataRate()).c_str()); |
mfiore | 0:a151a6350d7f | 21 | logInfo("\tTX power: %lu dBm", dot->getTxPower()); |
mfiore | 0:a151a6350d7f | 22 | logInfo("\tatnenna gain: %u dBm", dot->getAntennaGain()); |
mfiore | 0:a151a6350d7f | 23 | } |
mfiore | 0:a151a6350d7f | 24 | |
mfiore | 0:a151a6350d7f | 25 | void update_ota_config(std::string network_name, std::string network_passphrase, uint8_t frequency_sub_band, bool public_network, uint8_t ack) { |
mfiore | 0:a151a6350d7f | 26 | std::string current_network_name = dot->getNetworkName(); |
mfiore | 0:a151a6350d7f | 27 | std::string current_network_passphrase = dot->getNetworkPassphrase(); |
mfiore | 0:a151a6350d7f | 28 | uint8_t current_frequency_sub_band = dot->getFrequencySubBand(); |
mfiore | 0:a151a6350d7f | 29 | bool current_public_network = dot->getPublicNetwork(); |
mfiore | 0:a151a6350d7f | 30 | uint8_t current_ack = dot->getAck(); |
mfiore | 0:a151a6350d7f | 31 | |
mfiore | 0:a151a6350d7f | 32 | if (current_network_name != network_name) { |
mfiore | 0:a151a6350d7f | 33 | logInfo("changing network name from \"%s\" to \"%s\"", current_network_name.c_str(), network_name.c_str()); |
mfiore | 0:a151a6350d7f | 34 | if (dot->setNetworkName(network_name) != mDot::MDOT_OK) { |
mfiore | 0:a151a6350d7f | 35 | logError("failed to set network name to \"%s\"", network_name.c_str()); |
mfiore | 0:a151a6350d7f | 36 | } |
mfiore | 0:a151a6350d7f | 37 | } |
mfiore | 0:a151a6350d7f | 38 | |
mfiore | 0:a151a6350d7f | 39 | if (current_network_passphrase != network_passphrase) { |
mfiore | 0:a151a6350d7f | 40 | logInfo("changing network passphrase from \"%s\" to \"%s\"", current_network_passphrase.c_str(), network_passphrase.c_str()); |
mfiore | 0:a151a6350d7f | 41 | if (dot->setNetworkPassphrase(network_passphrase) != mDot::MDOT_OK) { |
mfiore | 0:a151a6350d7f | 42 | logError("failed to set network passphrase to \"%s\"", network_passphrase.c_str()); |
mfiore | 0:a151a6350d7f | 43 | } |
mfiore | 0:a151a6350d7f | 44 | } |
mfiore | 0:a151a6350d7f | 45 | |
mfiore | 0:a151a6350d7f | 46 | if (current_frequency_sub_band != frequency_sub_band) { |
mfiore | 0:a151a6350d7f | 47 | logInfo("changing frequency sub band from %u to %u", current_frequency_sub_band, frequency_sub_band); |
mfiore | 0:a151a6350d7f | 48 | if (dot->setFrequencySubBand(frequency_sub_band) != mDot::MDOT_OK) { |
mfiore | 0:a151a6350d7f | 49 | logError("failed to set frequency sub band to %u", frequency_sub_band); |
mfiore | 0:a151a6350d7f | 50 | } |
mfiore | 0:a151a6350d7f | 51 | } |
mfiore | 0:a151a6350d7f | 52 | |
mfiore | 0:a151a6350d7f | 53 | if (current_public_network != public_network) { |
mfiore | 0:a151a6350d7f | 54 | logInfo("changing public network from %s to %s", current_public_network ? "true" : "false", public_network ? "true" : "false"); |
mfiore | 0:a151a6350d7f | 55 | if (dot->setPublicNetwork(public_network) != mDot::MDOT_OK) { |
mfiore | 0:a151a6350d7f | 56 | logError("failed to set public network to %s", public_network ? "true" : "false"); |
mfiore | 0:a151a6350d7f | 57 | } |
mfiore | 0:a151a6350d7f | 58 | } |
mfiore | 0:a151a6350d7f | 59 | |
mfiore | 0:a151a6350d7f | 60 | if (current_ack != ack) { |
mfiore | 0:a151a6350d7f | 61 | logInfo("changing acks from %u to %u", current_ack, ack); |
mfiore | 0:a151a6350d7f | 62 | if (dot->setAck(ack) != mDot::MDOT_OK) { |
mfiore | 0:a151a6350d7f | 63 | logError("failed to set acks to %u", ack); |
mfiore | 0:a151a6350d7f | 64 | } |
mfiore | 0:a151a6350d7f | 65 | } |
mfiore | 0:a151a6350d7f | 66 | } |
mfiore | 0:a151a6350d7f | 67 | |
mfiore | 0:a151a6350d7f | 68 | void join_network() { |
mfiore | 0:a151a6350d7f | 69 | int32_t j_attempts = 0; |
mfiore | 0:a151a6350d7f | 70 | int32_t ret = mDot::MDOT_ERROR; |
mfiore | 0:a151a6350d7f | 71 | |
mfiore | 0:a151a6350d7f | 72 | // attempt to join the network |
mfiore | 0:a151a6350d7f | 73 | while (ret != mDot::MDOT_OK) { |
mfiore | 0:a151a6350d7f | 74 | logInfo("attempt %d to join network", ++j_attempts); |
mfiore | 0:a151a6350d7f | 75 | ret = dot->joinNetwork(); |
mfiore | 0:a151a6350d7f | 76 | if (ret != mDot::MDOT_OK) { |
mfiore | 0:a151a6350d7f | 77 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:a151a6350d7f | 78 | // in some frequency bands we need to wait until another channel is available before transmitting again |
mfiore | 0:a151a6350d7f | 79 | uint32_t delay_s = (dot->getNextTxMs() / 1000) + 1; |
mfiore | 0:a151a6350d7f | 80 | if (delay_s < 2) { |
mfiore | 0:a151a6350d7f | 81 | logInfo("waiting %lu s until next free channel", delay_s); |
mfiore | 0:a151a6350d7f | 82 | wait(delay_s); |
mfiore | 0:a151a6350d7f | 83 | } else { |
mfiore | 0:a151a6350d7f | 84 | logInfo("sleeping %lu s until next free channel", delay_s); |
mfiore | 0:a151a6350d7f | 85 | dot->sleep(delay_s, mDot::RTC_ALARM, false); |
mfiore | 0:a151a6350d7f | 86 | } |
mfiore | 0:a151a6350d7f | 87 | } |
mfiore | 0:a151a6350d7f | 88 | } |
mfiore | 0:a151a6350d7f | 89 | } |
mfiore | 0:a151a6350d7f | 90 | |
mfiore | 0:a151a6350d7f | 91 | void sleep_wake_rtc_only(bool deepsleep) { |
mfiore | 0:a151a6350d7f | 92 | // in some frequency bands we need to wait until another channel is available before transmitting again |
mfiore | 0:a151a6350d7f | 93 | // wait at least 10s between transmissions |
mfiore | 0:a151a6350d7f | 94 | uint32_t delay_s = dot->getNextTxMs() / 1000; |
mfiore | 0:a151a6350d7f | 95 | if (delay_s < 10) { |
mfiore | 0:a151a6350d7f | 96 | delay_s = 10; |
mfiore | 0:a151a6350d7f | 97 | } |
mfiore | 0:a151a6350d7f | 98 | |
mfiore | 0:a151a6350d7f | 99 | logInfo("%ssleeping %lus", deepsleep ? "deep" : "", delay_s); |
mfiore | 0:a151a6350d7f | 100 | logInfo("application will %s after waking up", deepsleep ? "execute from beginning" : "resume"); |
mfiore | 0:a151a6350d7f | 101 | |
mfiore | 0:a151a6350d7f | 102 | // go to sleep/deepsleep for delay_s seconds and wake using the RTC alarm |
mfiore | 0:a151a6350d7f | 103 | dot->sleep(delay_s, mDot::RTC_ALARM, deepsleep); |
mfiore | 0:a151a6350d7f | 104 | } |
mfiore | 0:a151a6350d7f | 105 | |
mfiore | 0:a151a6350d7f | 106 | void sleep_wake_interrupt_only(bool deepsleep) { |
mfiore | 0:a151a6350d7f | 107 | #if defined (TARGET_XDOT_L151CC) |
mfiore | 0:a151a6350d7f | 108 | if (deepsleep) { |
mfiore | 0:a151a6350d7f | 109 | // for xDot, WAKE pin (connected to S2 on xDot-DK) is the only pin that can wake the processor from deepsleep |
mfiore | 0:a151a6350d7f | 110 | // it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call |
mfiore | 0:a151a6350d7f | 111 | } else { |
mfiore | 0:a151a6350d7f | 112 | // configure WAKE pin (connected to S2 on xDot-DK) as the pin that will wake the xDot from low power modes |
mfiore | 0:a151a6350d7f | 113 | // other pins can be confgured instead: GPIO0-3 or UART_RX |
mfiore | 0:a151a6350d7f | 114 | dot->setWakePin(WAKE); |
mfiore | 0:a151a6350d7f | 115 | } |
mfiore | 0:a151a6350d7f | 116 | |
mfiore | 0:a151a6350d7f | 117 | logInfo("%ssleeping until rising edge on %s pin", deepsleep ? "deep" : "", deepsleep ? "WAKE" : mDot::pinName2Str(dot->getWakePin()).c_str()); |
mfiore | 0:a151a6350d7f | 118 | #else |
mfiore | 0:a151a6350d7f | 119 | if (deepsleep) { |
mfiore | 0:a151a6350d7f | 120 | // for mDot, XBEE_DIO7 pin is the only pin that can wake the processor from deepsleep |
mfiore | 0:a151a6350d7f | 121 | // it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call |
mfiore | 0:a151a6350d7f | 122 | } else { |
mfiore | 0:a151a6350d7f | 123 | // configure XBEE_DIO7 pin as the pin that will wake the mDot from low power modes |
mfiore | 0:a151a6350d7f | 124 | // other pins can be confgured instead: XBEE_DIO2-6, XBEE_DI8, XBEE_DIN |
mfiore | 0:a151a6350d7f | 125 | dot->setWakePin(XBEE_DIO7); |
mfiore | 0:a151a6350d7f | 126 | } |
mfiore | 0:a151a6350d7f | 127 | |
mfiore | 0:a151a6350d7f | 128 | logInfo("%ssleeping until rising edge on %s pin", deepsleep ? "deep" : "", deepsleep ? "DIO7" : mDot::pinName2Str(dot->getWakePin()).c_str()); |
mfiore | 0:a151a6350d7f | 129 | #endif |
mfiore | 0:a151a6350d7f | 130 | |
mfiore | 0:a151a6350d7f | 131 | logInfo("application will %s after waking up", deepsleep ? "execute from beginning" : "resume"); |
mfiore | 0:a151a6350d7f | 132 | |
mfiore | 0:a151a6350d7f | 133 | // go to sleep/deepsleep and wake on rising edge of configured wake pin (only the WAKE pin in deepsleep) |
mfiore | 0:a151a6350d7f | 134 | // since we're not waking on the RTC alarm, the interval is ignored |
mfiore | 0:a151a6350d7f | 135 | dot->sleep(0, mDot::INTERRUPT, deepsleep); |
mfiore | 0:a151a6350d7f | 136 | } |
mfiore | 0:a151a6350d7f | 137 | |
mfiore | 0:a151a6350d7f | 138 | void sleep_wake_rtc_or_interrupt(bool deepsleep) { |
mfiore | 0:a151a6350d7f | 139 | // in some frequency bands we need to wait until another channel is available before transmitting again |
mfiore | 0:a151a6350d7f | 140 | // wait at least 10s between transmissions |
mfiore | 0:a151a6350d7f | 141 | uint32_t delay_s = dot->getNextTxMs() / 1000; |
mfiore | 0:a151a6350d7f | 142 | if (delay_s < 10) { |
mfiore | 0:a151a6350d7f | 143 | delay_s = 10; |
mfiore | 0:a151a6350d7f | 144 | } |
mfiore | 0:a151a6350d7f | 145 | |
mfiore | 0:a151a6350d7f | 146 | #if defined (TARGET_XDOT_L151CC) |
mfiore | 0:a151a6350d7f | 147 | if (deepsleep) { |
mfiore | 0:a151a6350d7f | 148 | // for xDot, WAKE pin (connected to S2 on xDot-DK) is the only pin that can wake the processor from deepsleep |
mfiore | 0:a151a6350d7f | 149 | // it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call |
mfiore | 0:a151a6350d7f | 150 | } else { |
mfiore | 0:a151a6350d7f | 151 | // configure WAKE pin (connected to S2 on xDot-DK) as the pin that will wake the xDot from low power modes |
mfiore | 0:a151a6350d7f | 152 | // other pins can be confgured instead: GPIO0-3 or UART_RX |
mfiore | 0:a151a6350d7f | 153 | dot->setWakePin(WAKE); |
mfiore | 0:a151a6350d7f | 154 | } |
mfiore | 0:a151a6350d7f | 155 | |
mfiore | 0:a151a6350d7f | 156 | logInfo("%ssleeping %lus or until rising edge on %s pin", deepsleep ? "deep" : "", delay_s, deepsleep ? "WAKE" : mDot::pinName2Str(dot->getWakePin()).c_str()); |
mfiore | 0:a151a6350d7f | 157 | #else |
mfiore | 0:a151a6350d7f | 158 | if (deepsleep) { |
mfiore | 0:a151a6350d7f | 159 | // for mDot, XBEE_DIO7 pin is the only pin that can wake the processor from deepsleep |
mfiore | 0:a151a6350d7f | 160 | // it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call |
mfiore | 0:a151a6350d7f | 161 | } else { |
mfiore | 0:a151a6350d7f | 162 | // configure XBEE_DIO7 pin as the pin that will wake the mDot from low power modes |
mfiore | 0:a151a6350d7f | 163 | // other pins can be confgured instead: XBEE_DIO2-6, XBEE_DI8, XBEE_DIN |
mfiore | 0:a151a6350d7f | 164 | dot->setWakePin(XBEE_DIO7); |
mfiore | 0:a151a6350d7f | 165 | } |
mfiore | 0:a151a6350d7f | 166 | |
mfiore | 0:a151a6350d7f | 167 | logInfo("%ssleeping %lus or until rising edge on %s pin", deepsleep ? "deep" : "", delay_s, deepsleep ? "DIO7" : mDot::pinName2Str(dot->getWakePin()).c_str()); |
mfiore | 0:a151a6350d7f | 168 | #endif |
mfiore | 0:a151a6350d7f | 169 | |
mfiore | 0:a151a6350d7f | 170 | logInfo("application will %s after waking up", deepsleep ? "execute from beginning" : "resume"); |
mfiore | 0:a151a6350d7f | 171 | |
mfiore | 0:a151a6350d7f | 172 | // go to sleep/deepsleep and wake using the RTC alarm after delay_s seconds or rising edge of configured wake pin (only the WAKE pin in deepsleep) |
mfiore | 0:a151a6350d7f | 173 | // whichever comes first will wake the xDot |
mfiore | 0:a151a6350d7f | 174 | dot->sleep(delay_s, mDot::RTC_ALARM_OR_INTERRUPT, deepsleep); |
mfiore | 0:a151a6350d7f | 175 | } |
mfiore | 0:a151a6350d7f | 176 | |
mfiore | 0:a151a6350d7f | 177 | void send_data(std::vector<uint8_t> data) { |
mfiore | 0:a151a6350d7f | 178 | uint32_t ret; |
mfiore | 0:a151a6350d7f | 179 | |
mfiore | 0:a151a6350d7f | 180 | ret = dot->send(data); |
mfiore | 0:a151a6350d7f | 181 | if (ret != mDot::MDOT_OK) { |
mfiore | 0:a151a6350d7f | 182 | logError("failed to send light data to gateway [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:a151a6350d7f | 183 | } else { |
mfiore | 0:a151a6350d7f | 184 | logInfo("successfully sent light data to gateway"); |
mfiore | 0:a151a6350d7f | 185 | } |
mfiore | 0:a151a6350d7f | 186 | } |