summaryrefslogtreecommitdiff
path: root/Mode/ModeJoin.cpp
blob: 62ac25d706d4b0a20e7818e7bcc2b057e4991803 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Copyright (c) <2016> <MultiTech Systems>, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "ModeJoin.h"
#include "MTSLog.h"
#include "MTSText.h"

ModeJoin::ModeJoin(DOGS102* lcd, ButtonHandler* buttons, mDot* dot, LoRaHandler* lora, GPSPARSER* gps, SensorHandler* sensors)
  : Mode(lcd, buttons, dot, lora, gps, sensors),
    _join(lcd, _band),
    _joined(false)
{}

ModeJoin::~ModeJoin() {}

bool ModeJoin::start() {
    bool joining = false;

    // clear any stale signals
    osSignalClear(_main_id, buttonSignal | loraSignal);

    if (_dot->getJoinMode() == mDot::MANUAL) {
        // already joined
        return true;
    }

    _initial_data_rate = _dot->getTxDataRate();
    _initial_power = _dot->getTxPower();

    _data_rate = mDot::DR0;
    _power = 20;
    _joined = false;

    display();

    _dot->setTxDataRate(_data_rate);
    _dot->setTxPower(_power);
    _lora->resetJoinAttempts();

    while (! _joined) {
        _next_tx = _dot->getNextTxMs();
        if (! joining && _next_tx > 0) {
            logInfo("next tx %lu ms", _next_tx);
            _join.updateStatus("Waiting...");
            _join.updateCountdown(_next_tx / 1000);
        } else if (! joining) {
            logInfo("attempting to join");
            joining = true;
            display();
            _lora->join();
        }

        osEvent e = Thread::signal_wait(0, 250);
        if (e.status == osEventSignal) {
            if (e.value.signals & buttonSignal) {
                _be = _buttons->getButtonEvent();
                switch (_be) {
                    case ButtonHandler::sw1_press:
                        _dot->setTxDataRate(_initial_data_rate);
                        _dot->setTxPower(_initial_power);
                        return false;
                    case ButtonHandler::sw2_press:
                        break;
                    case ButtonHandler::sw1_hold:
                        _dot->setTxDataRate(_initial_data_rate);
                        _dot->setTxPower(_initial_power);
                        return false;
                }
            }
            if (e.value.signals & loraSignal) {
                _ls = _lora->getStatus();
                switch (_ls) {
                    case LoRaHandler::join_success:
                        _join.updateStatus("Join Success!");
                        _join.displayCancel(false);
                        logInfo("joined");
                        _joined = true;
                        osDelay(2000);
                        _dot->setTxDataRate(_initial_data_rate);
                        _dot->setTxPower(_initial_power);
                        return true;

                    case LoRaHandler::join_failure:
                        logInfo("failed to join");
                        joining = false;
                        break;
                }
            }
        }
    }

    _dot->setTxDataRate(_initial_data_rate);
    _dot->setTxPower(_initial_power);
    return false;
}

void ModeJoin::display() {
    _join.display();
    _join.updateStatus("Joining...");
    if (_dot->getJoinMode() == mDot::MANUAL) {
        _join.updateId(mts::Text::bin2hexString(_dot->getNetworkId()));
        _join.updateKey(mts::Text::bin2hexString(_dot->getNetworkKey()));
    } else {
        _join.updateId(_dot->getNetworkName());
        _join.updateKey(_dot->getNetworkPassphrase());
    }
    if (_band == mDot::FB_US915 || _band == mDot::FB_AU915) {
        _sub_band = _dot->getFrequencySubBand();
        _join.updateFsb(_sub_band);
    }
    _join.updateRate(_dot->getTxDataRate());
    _join.updatePower(_power);
    _join.updateAttempt(_lora->getJoinAttempts());
}