Updated to libmDot 1.0.8-1
Dependencies: DOGS102 GpsParser ISL29011 MMA845x MPL3115A2 MTS-Serial NCP5623B libmDot mbed-rtos mbed
Fork of MTDOT-BOX-EVB-Factory-Firmware by
LoRaHandler/LoRaHandler.cpp@6:ab581c4260e7, 2016-07-13 (annotated)
- Committer:
- jreiss
- Date:
- Wed Jul 13 19:14:04 2016 +0000
- Revision:
- 6:ab581c4260e7
- Parent:
- 1:71125aa00e33
Use DR enum values instead of SF values
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mike Fiore |
1:71125aa00e33 | 1 | /* Copyright (c) <2016> <MultiTech Systems>, MIT License |
Mike Fiore |
1:71125aa00e33 | 2 | * |
Mike Fiore |
1:71125aa00e33 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Mike Fiore |
1:71125aa00e33 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
Mike Fiore |
1:71125aa00e33 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
Mike Fiore |
1:71125aa00e33 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
Mike Fiore |
1:71125aa00e33 | 7 | * furnished to do so, subject to the following conditions: |
Mike Fiore |
1:71125aa00e33 | 8 | * |
Mike Fiore |
1:71125aa00e33 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Mike Fiore |
1:71125aa00e33 | 10 | * substantial portions of the Software. |
Mike Fiore |
1:71125aa00e33 | 11 | * |
Mike Fiore |
1:71125aa00e33 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Mike Fiore |
1:71125aa00e33 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Mike Fiore |
1:71125aa00e33 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Mike Fiore |
1:71125aa00e33 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Mike Fiore |
1:71125aa00e33 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Mike Fiore |
1:71125aa00e33 | 17 | */ |
Mike Fiore |
1:71125aa00e33 | 18 | |
Mike Fiore |
1:71125aa00e33 | 19 | #include "LoRaHandler.h" |
Mike Fiore |
1:71125aa00e33 | 20 | |
Mike Fiore |
1:71125aa00e33 | 21 | #define signal (int32_t)0xA0 |
Mike Fiore |
1:71125aa00e33 | 22 | |
Mike Fiore |
1:71125aa00e33 | 23 | typedef enum { |
Mike Fiore |
1:71125aa00e33 | 24 | l_none = 0, |
Mike Fiore |
1:71125aa00e33 | 25 | l_link_check, |
Mike Fiore |
1:71125aa00e33 | 26 | l_send, |
Mike Fiore |
1:71125aa00e33 | 27 | l_join |
Mike Fiore |
1:71125aa00e33 | 28 | } InternalLoRa; |
Mike Fiore |
1:71125aa00e33 | 29 | |
Mike Fiore |
1:71125aa00e33 | 30 | uint8_t cmd = l_none; |
Mike Fiore |
1:71125aa00e33 | 31 | std::vector<uint8_t> send_data; |
Mike Fiore |
1:71125aa00e33 | 32 | |
Mike Fiore |
1:71125aa00e33 | 33 | void l_worker(void const* argument) { |
Mike Fiore |
1:71125aa00e33 | 34 | LoRaHandler* l = (LoRaHandler*)argument; |
Mike Fiore |
1:71125aa00e33 | 35 | osEvent e; |
Mike Fiore |
1:71125aa00e33 | 36 | |
Mike Fiore |
1:71125aa00e33 | 37 | l->_dot = mDot::getInstance(); |
Mike Fiore |
1:71125aa00e33 | 38 | int32_t ret; |
Mike Fiore |
1:71125aa00e33 | 39 | mDot::link_check lc; |
Mike Fiore |
1:71125aa00e33 | 40 | mDot::rssi_stats rs; |
Mike Fiore |
1:71125aa00e33 | 41 | mDot::snr_stats ss; |
Mike Fiore |
1:71125aa00e33 | 42 | |
Mike Fiore |
1:71125aa00e33 | 43 | while (true) { |
Mike Fiore |
1:71125aa00e33 | 44 | e = Thread::signal_wait(signal); |
Mike Fiore |
1:71125aa00e33 | 45 | if (e.status == osEventSignal) { |
Mike Fiore |
1:71125aa00e33 | 46 | l->_status = LoRaHandler::busy; |
Mike Fiore |
1:71125aa00e33 | 47 | l->_tick.attach(l, &LoRaHandler::blinker, 0.05); |
Mike Fiore |
1:71125aa00e33 | 48 | switch (cmd) { |
Mike Fiore |
1:71125aa00e33 | 49 | case l_link_check: |
Mike Fiore |
1:71125aa00e33 | 50 | l->_mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 51 | lc = l->_dot->networkLinkCheck(); |
Mike Fiore |
1:71125aa00e33 | 52 | l->_mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 53 | if (lc.status) { |
Mike Fiore |
1:71125aa00e33 | 54 | l->_link.up = lc; |
Mike Fiore |
1:71125aa00e33 | 55 | l->_mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 56 | rs = l->_dot->getRssiStats(); |
Mike Fiore |
1:71125aa00e33 | 57 | ss = l->_dot->getSnrStats(); |
Mike Fiore |
1:71125aa00e33 | 58 | l->_mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 59 | l->_link.down.rssi = rs.last; |
Mike Fiore |
1:71125aa00e33 | 60 | l->_link.down.snr = ss.last; |
Mike Fiore |
1:71125aa00e33 | 61 | l->_status = LoRaHandler::link_check_success; |
Mike Fiore |
1:71125aa00e33 | 62 | } else { |
Mike Fiore |
1:71125aa00e33 | 63 | l->_status = LoRaHandler::link_check_failure; |
Mike Fiore |
1:71125aa00e33 | 64 | } |
Mike Fiore |
1:71125aa00e33 | 65 | osSignalSet(l->_main, loraSignal); |
Mike Fiore |
1:71125aa00e33 | 66 | l->_tick.detach(); |
Mike Fiore |
1:71125aa00e33 | 67 | l->_activity_led = LoRaHandler::green; |
Mike Fiore |
1:71125aa00e33 | 68 | break; |
Mike Fiore |
1:71125aa00e33 | 69 | |
Mike Fiore |
1:71125aa00e33 | 70 | case l_send: |
Mike Fiore |
1:71125aa00e33 | 71 | l->_mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 72 | ret = l->_dot->send(send_data); |
Mike Fiore |
1:71125aa00e33 | 73 | l->_mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 74 | if (ret == mDot::MDOT_OK) |
Mike Fiore |
1:71125aa00e33 | 75 | l->_status = LoRaHandler::send_success; |
Mike Fiore |
1:71125aa00e33 | 76 | else |
Mike Fiore |
1:71125aa00e33 | 77 | l->_status = LoRaHandler::send_failure; |
Mike Fiore |
1:71125aa00e33 | 78 | osSignalSet(l->_main, loraSignal); |
Mike Fiore |
1:71125aa00e33 | 79 | l->_tick.detach(); |
Mike Fiore |
1:71125aa00e33 | 80 | l->_activity_led = LoRaHandler::green; |
Mike Fiore |
1:71125aa00e33 | 81 | break; |
Mike Fiore |
1:71125aa00e33 | 82 | |
Mike Fiore |
1:71125aa00e33 | 83 | case l_join: |
Mike Fiore |
1:71125aa00e33 | 84 | l->_mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 85 | ret = l->_dot->joinNetworkOnce(); |
Mike Fiore |
1:71125aa00e33 | 86 | l->_join_attempts++; |
Mike Fiore |
1:71125aa00e33 | 87 | l->_mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 88 | if (ret == mDot::MDOT_OK) { |
Mike Fiore |
1:71125aa00e33 | 89 | l->_status = LoRaHandler::join_success; |
Mike Fiore |
1:71125aa00e33 | 90 | } else { |
Mike Fiore |
1:71125aa00e33 | 91 | l->_status = LoRaHandler::join_failure; |
Mike Fiore |
1:71125aa00e33 | 92 | } |
Mike Fiore |
1:71125aa00e33 | 93 | osSignalSet(l->_main, loraSignal); |
Mike Fiore |
1:71125aa00e33 | 94 | l->_tick.detach(); |
Mike Fiore |
1:71125aa00e33 | 95 | l->_activity_led = LoRaHandler::green; |
Mike Fiore |
1:71125aa00e33 | 96 | break; |
Mike Fiore |
1:71125aa00e33 | 97 | |
Mike Fiore |
1:71125aa00e33 | 98 | default: |
Mike Fiore |
1:71125aa00e33 | 99 | l->_status = LoRaHandler::none; |
Mike Fiore |
1:71125aa00e33 | 100 | break; |
Mike Fiore |
1:71125aa00e33 | 101 | } |
Mike Fiore |
1:71125aa00e33 | 102 | } |
Mike Fiore |
1:71125aa00e33 | 103 | } |
Mike Fiore |
1:71125aa00e33 | 104 | } |
Mike Fiore |
1:71125aa00e33 | 105 | |
Mike Fiore |
1:71125aa00e33 | 106 | LoRaHandler::LoRaHandler(osThreadId main) |
Mike Fiore |
1:71125aa00e33 | 107 | : _main(main), |
Mike Fiore |
1:71125aa00e33 | 108 | _thread(l_worker, (void*)this), |
Mike Fiore |
1:71125aa00e33 | 109 | _status(none), |
Mike Fiore |
1:71125aa00e33 | 110 | _join_attempts(1), |
Mike Fiore |
1:71125aa00e33 | 111 | _activity_led(XBEE_DIO1, PIN_OUTPUT, PullNone, red) |
Mike Fiore |
1:71125aa00e33 | 112 | { |
Mike Fiore |
1:71125aa00e33 | 113 | _link.status = false; |
Mike Fiore |
1:71125aa00e33 | 114 | _activity_led = red; |
Mike Fiore |
1:71125aa00e33 | 115 | } |
Mike Fiore |
1:71125aa00e33 | 116 | |
Mike Fiore |
1:71125aa00e33 | 117 | bool LoRaHandler::linkCheck() { |
Mike Fiore |
1:71125aa00e33 | 118 | return action(l_link_check); |
Mike Fiore |
1:71125aa00e33 | 119 | } |
Mike Fiore |
1:71125aa00e33 | 120 | |
Mike Fiore |
1:71125aa00e33 | 121 | bool LoRaHandler::send(std::vector<uint8_t> data) { |
Mike Fiore |
1:71125aa00e33 | 122 | send_data = data; |
Mike Fiore |
1:71125aa00e33 | 123 | return action(l_send); |
Mike Fiore |
1:71125aa00e33 | 124 | } |
Mike Fiore |
1:71125aa00e33 | 125 | |
Mike Fiore |
1:71125aa00e33 | 126 | bool LoRaHandler::join() { |
Mike Fiore |
1:71125aa00e33 | 127 | return action(l_join); |
Mike Fiore |
1:71125aa00e33 | 128 | } |
Mike Fiore |
1:71125aa00e33 | 129 | |
Mike Fiore |
1:71125aa00e33 | 130 | bool LoRaHandler::action(uint8_t c) { |
Mike Fiore |
1:71125aa00e33 | 131 | if (_status != busy) { |
Mike Fiore |
1:71125aa00e33 | 132 | cmd = c; |
Mike Fiore |
1:71125aa00e33 | 133 | _thread.signal_set(signal); |
Mike Fiore |
1:71125aa00e33 | 134 | _thread.signal_clr(signal); |
Mike Fiore |
1:71125aa00e33 | 135 | return true; |
Mike Fiore |
1:71125aa00e33 | 136 | } |
Mike Fiore |
1:71125aa00e33 | 137 | |
Mike Fiore |
1:71125aa00e33 | 138 | return false; |
Mike Fiore |
1:71125aa00e33 | 139 | } |
Mike Fiore |
1:71125aa00e33 | 140 | |
Mike Fiore |
1:71125aa00e33 | 141 | LoRaHandler::LoRaStatus LoRaHandler::getStatus() { |
Mike Fiore |
1:71125aa00e33 | 142 | LoRaStatus status; |
Mike Fiore |
1:71125aa00e33 | 143 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 144 | status = _status; |
Mike Fiore |
1:71125aa00e33 | 145 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 146 | |
Mike Fiore |
1:71125aa00e33 | 147 | return status; |
Mike Fiore |
1:71125aa00e33 | 148 | } |
Mike Fiore |
1:71125aa00e33 | 149 | |
Mike Fiore |
1:71125aa00e33 | 150 | LoRaHandler::LoRaLink LoRaHandler::getLinkCheckResults() { |
Mike Fiore |
1:71125aa00e33 | 151 | LoRaLink link; |
Mike Fiore |
1:71125aa00e33 | 152 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 153 | link = _link; |
Mike Fiore |
1:71125aa00e33 | 154 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 155 | |
Mike Fiore |
1:71125aa00e33 | 156 | return link; |
Mike Fiore |
1:71125aa00e33 | 157 | } |
Mike Fiore |
1:71125aa00e33 | 158 | |
Mike Fiore |
1:71125aa00e33 | 159 | uint32_t LoRaHandler::getJoinAttempts() { |
Mike Fiore |
1:71125aa00e33 | 160 | uint32_t val; |
Mike Fiore |
1:71125aa00e33 | 161 | |
Mike Fiore |
1:71125aa00e33 | 162 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 163 | val = _join_attempts; |
Mike Fiore |
1:71125aa00e33 | 164 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 165 | |
Mike Fiore |
1:71125aa00e33 | 166 | return val; |
Mike Fiore |
1:71125aa00e33 | 167 | } |
Mike Fiore |
1:71125aa00e33 | 168 | |
Mike Fiore |
1:71125aa00e33 | 169 | void LoRaHandler::resetJoinAttempts() { |
Mike Fiore |
1:71125aa00e33 | 170 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 171 | _join_attempts = 1; |
Mike Fiore |
1:71125aa00e33 | 172 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 173 | } |
Mike Fiore |
1:71125aa00e33 | 174 | |
Mike Fiore |
1:71125aa00e33 | 175 | void LoRaHandler::blinker() { |
Mike Fiore |
1:71125aa00e33 | 176 | _activity_led = !_activity_led; |
Mike Fiore |
1:71125aa00e33 | 177 | } |
Mike Fiore |
1:71125aa00e33 | 178 | |
Mike Fiore |
1:71125aa00e33 | 179 | void LoRaHandler::resetActivityLed() { |
Mike Fiore |
1:71125aa00e33 | 180 | _activity_led = red; |
Mike Fiore |
1:71125aa00e33 | 181 | } |
Mike Fiore |
1:71125aa00e33 | 182 |