Factory firmware for the MultiTech Dotbox (MTDOT-BOX) and EVB (MTDOT-EVB) products.

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ModeJoin.cpp Source File

ModeJoin.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 "ModeJoin.h"
00020 #include "MTSLog.h"
00021 #include "MTSText.h"
00022 
00023 ModeJoin::ModeJoin(DOGS102* lcd, ButtonHandler* buttons, mDot* dot, LoRaHandler* lora, GPSPARSER* gps, SensorHandler* sensors)
00024   : Mode(lcd, buttons, dot, lora, gps, sensors),
00025     _join(lcd, _band),
00026     _joined(false)
00027 {}
00028 
00029 ModeJoin::~ModeJoin() {}
00030 
00031 bool ModeJoin::start() {
00032     bool joining = false;
00033 
00034     // clear any stale signals
00035     osSignalClear(_main_id, buttonSignal | loraSignal);
00036 
00037     if (_dot->getJoinMode() == mDot::MANUAL) {
00038         // already joined
00039         return true;
00040     }
00041 
00042     _initial_data_rate = _dot->getTxDataRate();
00043     _initial_power = _dot->getTxPower();
00044 
00045     _data_rate = mDot::DR0;
00046     _power = 20;
00047     _joined = false;
00048 
00049     display();
00050 
00051     _dot->setTxDataRate(_data_rate);
00052     _dot->setTxPower(_power);
00053     _lora->resetJoinAttempts();
00054 
00055     while (! _joined) {
00056         _next_tx = _dot->getNextTxMs();
00057         if (! joining && _next_tx > 0) {
00058             logInfo("next tx %lu ms", _next_tx);
00059             _join.updateStatus("Waiting...");
00060             _join.updateCountdown(_next_tx / 1000);
00061         } else if (! joining) {
00062             logInfo("attempting to join");
00063             joining = true;
00064             display();
00065             _lora->join();
00066         }
00067 
00068         osEvent e = Thread::signal_wait(0, 250);
00069         if (e.status == osEventSignal) {
00070             if (e.value.signals & buttonSignal) {
00071                 _be = _buttons->getButtonEvent();
00072                 switch (_be) {
00073                     case ButtonHandler::sw1_press:
00074                         _dot->setTxDataRate(_initial_data_rate);
00075                         _dot->setTxPower(_initial_power);
00076                         return false;
00077                     case ButtonHandler::sw2_press:
00078                         break;
00079                     case ButtonHandler::sw1_hold:
00080                         _dot->setTxDataRate(_initial_data_rate);
00081                         _dot->setTxPower(_initial_power);
00082                         return false;
00083                 }
00084             }
00085             if (e.value.signals & loraSignal) {
00086                 _ls = _lora->getStatus();
00087                 switch (_ls) {
00088                     case LoRaHandler::join_success:
00089                         _join.updateStatus("Join Success!");
00090                         _join.displayCancel(false);
00091                         logInfo("joined");
00092                         _joined = true;
00093                         osDelay(2000);
00094                         _dot->setTxDataRate(_initial_data_rate);
00095                         _dot->setTxPower(_initial_power);
00096                         return true;
00097 
00098                     case LoRaHandler::join_failure:
00099                         logInfo("failed to join");
00100                         joining = false;
00101                         break;
00102                 }
00103             }
00104         }
00105     }
00106 
00107     _dot->setTxDataRate(_initial_data_rate);
00108     _dot->setTxPower(_initial_power);
00109     return false;
00110 }
00111 
00112 void ModeJoin::display() {
00113     _join.display();
00114     _join.updateStatus("Joining...");
00115     if (_dot->getJoinMode() == mDot::MANUAL) {
00116         _join.updateId(mts::Text::bin2hexString(_dot->getNetworkId()));
00117         _join.updateKey(mts::Text::bin2hexString(_dot->getNetworkKey()));
00118     } else {
00119         _join.updateId(_dot->getNetworkName());
00120         _join.updateKey(_dot->getNetworkPassphrase());
00121     }
00122     if (lora::ChannelPlan::IsPlanFixed(_band)) {
00123         _sub_band = _dot->getFrequencySubBand();
00124         _join.updateFsb(_sub_band);
00125     }
00126     _join.updateRate(_dot->getTxDataRate());
00127     _join.updatePower(_power);
00128     _join.updateAttempt(_lora->getJoinAttempts());
00129 }