Scott Hoppe / Mbed OS 4_Ecolab_RSSI_Checker

Dependencies:   DOGS102 GpsParser ISL29011 MMA845x MPL3115A2 MTS-Serial NCP5623B libmDot-dev-mbed5-deprecated

Fork of MTDOT-BOX-EVB-Factory-Firmware by MultiTech

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LoRaHandler.cpp Source File

LoRaHandler.cpp

00001 /* Copyright (c) <2016> <MultiTech Systems>, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or 
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 #include "LoRaHandler.h"
00020 
00021 #define signal (int32_t)0xA0
00022 
00023 typedef enum {
00024     l_none = 0,
00025     l_link_check,
00026     l_send,
00027     l_join
00028 } InternalLoRa;
00029 
00030 uint8_t cmd = l_none;
00031 std::vector<uint8_t> send_data;
00032 bool successLED = false;
00033 bool failLED = false;
00034 
00035 void l_worker(void const* argument) {
00036     LoRaHandler* l = (LoRaHandler*)argument;
00037     osEvent e;
00038 
00039     l->_dot = mDot::getInstance();
00040     int32_t ret;
00041     mDot::link_check lc;
00042     mDot::rssi_stats rs;
00043     mDot::snr_stats ss;
00044 
00045     while (true) {
00046         e = Thread::signal_wait(signal);
00047         if (e.status == osEventSignal) {
00048             l->_status = LoRaHandler::busy;
00049             l->_tick.attach(l, &LoRaHandler::blinker, 0.05);
00050             switch (cmd) {
00051                 case l_link_check:
00052                     l->_mutex.lock();
00053                     lc = l->_dot->networkLinkCheck();
00054                     l->_mutex.unlock();
00055                     if (lc.status) {
00056                         l->_link.up = lc;
00057                         l->_mutex.lock();
00058                         rs = l->_dot->getRssiStats();
00059                         ss = l->_dot->getSnrStats();
00060                         l->_mutex.unlock();
00061                         l->_link.down.rssi = rs.last;
00062                         l->_link.down.snr = ss.last;
00063                         successLED = true;
00064                         l->_status = LoRaHandler::link_check_success;
00065                         
00066                     } else {
00067                         failLED = true;
00068                         l->_status = LoRaHandler::link_check_failure;
00069                     }
00070                     osSignalSet(l->_main, loraSignal);
00071                     l->_tick.detach();
00072                     
00073                     if(successLED == true){ 
00074                         successLED = false;
00075                         l->_activity_led = LoRaHandler::green;
00076                     }
00077                     if(failLED == true){
00078                         failLED = false;
00079                         l->_activity_led = LoRaHandler::red;
00080                         }
00081 
00082                     break;
00083                 
00084                 case l_send:
00085                     l->_mutex.lock();
00086                     ret = l->_dot->send(send_data);
00087                     l->_mutex.unlock();
00088                     if (ret == mDot::MDOT_OK)
00089                         l->_status = LoRaHandler::send_success;
00090                     else
00091                         l->_status = LoRaHandler::send_failure;
00092                     osSignalSet(l->_main, loraSignal);
00093                     l->_tick.detach();
00094                     l->_activity_led = LoRaHandler::green;
00095                     break;
00096 
00097                 case l_join:
00098                     l->_mutex.lock();
00099                     ret = l->_dot->joinNetworkOnce();
00100                     l->_join_attempts++;
00101                     l->_mutex.unlock();
00102                     if (ret == mDot::MDOT_OK) {
00103                         l->_status = LoRaHandler::join_success;
00104                         osSignalSet(l->_main, loraSignal);
00105                         l->_tick.detach();
00106                         l->_activity_led = LoRaHandler::green;
00107                     } else {
00108                         l->_status = LoRaHandler::join_failure;
00109                         osSignalSet(l->_main, loraSignal);
00110                         l->_tick.detach();
00111                         l->_activity_led = LoRaHandler::red;
00112                     }
00113                 
00114                     break;
00115 
00116                 default:
00117                     l->_status = LoRaHandler::none;
00118                     break;
00119             }
00120         }
00121     }
00122 }
00123 
00124 LoRaHandler::LoRaHandler(osThreadId main)
00125   : _main(main),
00126     _thread(l_worker, (void*)this),
00127     _status(none),
00128     _join_attempts(1),
00129     _activity_led(XBEE_DIO1, PIN_OUTPUT, PullNone, red)
00130 {
00131     _link.status = false;
00132     _activity_led = red;
00133 }
00134 
00135 bool LoRaHandler::linkCheck() {
00136     return action(l_link_check);
00137 }
00138 
00139 bool LoRaHandler::send(std::vector<uint8_t> data) {
00140     send_data = data;
00141     return action(l_send);
00142 }
00143 
00144 bool LoRaHandler::join() {
00145         return action(l_join);
00146 }
00147 
00148 bool LoRaHandler::action(uint8_t c) {
00149     if (_status != busy) {
00150         cmd = c;
00151         _thread.signal_set(signal);
00152         _thread.signal_clr(signal);
00153         return true;
00154     }
00155 
00156     return false;
00157 }
00158 
00159 LoRaHandler::LoRaStatus LoRaHandler::getStatus() {
00160     LoRaStatus status;
00161     _mutex.lock();
00162     status = _status;
00163     _mutex.unlock();
00164 
00165     return status;
00166 }
00167 
00168 LoRaHandler::LoRaLink LoRaHandler::getLinkCheckResults() {
00169     LoRaLink link;
00170     _mutex.lock();
00171     link = _link;
00172     _mutex.unlock();
00173 
00174     return link;
00175 }
00176 
00177 uint32_t LoRaHandler::getJoinAttempts() {
00178     uint32_t val;
00179 
00180     _mutex.lock();
00181     val = _join_attempts;
00182     _mutex.unlock();
00183 
00184     return val;
00185 }
00186 
00187 void LoRaHandler::resetJoinAttempts() {
00188     _mutex.lock();
00189     _join_attempts = 1;
00190     _mutex.unlock();
00191 }
00192 
00193 void LoRaHandler::blinker() {
00194     _activity_led = !_activity_led;
00195 }
00196 
00197 void LoRaHandler::resetActivityLed() {
00198     _activity_led = red;
00199 }
00200 
00201 
00202 
00203