Skip to content

Commit efcbd55

Browse files
readapting ConnectionHandler implementation to use setting struct
1 parent 87452e2 commit efcbd55

14 files changed

+124
-132
lines changed

src/CatM1ConnectionHandler.cpp

+20-9
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,21 @@
2828
CTOR/DTOR
2929
******************************************************************************/
3030

31-
CatM1ConnectionHandler::CatM1ConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, RadioAccessTechnologyType rat, uint32_t band, bool const keep_alive)
31+
CatM1ConnectionHandler::CatM1ConnectionHandler()
32+
: ConnectionHandler(true, NetworkAdapter::CATM1) { }
33+
34+
CatM1ConnectionHandler::CatM1ConnectionHandler(
35+
const char * pin, const char * apn, const char * login, const char * pass,
36+
RadioAccessTechnologyType rat, uint32_t band, bool const keep_alive)
3237
: ConnectionHandler{keep_alive, NetworkAdapter::CATM1}
33-
, _pin(pin)
34-
, _apn(apn)
35-
, _login(login)
36-
, _pass(pass)
37-
, _rat(rat)
38-
, _band(band)
3938
{
40-
39+
_settings.type = NetworkAdapter::CATM1;
40+
strncpy(_settings.catm1.pin, pin, sizeof(_settings.catm1.pin)-1);
41+
strncpy(_settings.catm1.apn, apn, sizeof(_settings.catm1.apn)-1);
42+
strncpy(_settings.catm1.login, login, sizeof(_settings.catm1.login)-1);
43+
strncpy(_settings.catm1.pass, pass, sizeof(_settings.catm1.pass)-1);
44+
_settings.catm1.rat = static_cast<uint8_t>(rat);
45+
_settings.catm1.band = band;
4146
}
4247

4348
/******************************************************************************
@@ -64,7 +69,13 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleInit()
6469

6570
NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
6671
{
67-
if(!GSM.begin(_pin, _apn, _login, _pass, _rat, _band))
72+
if(!GSM.begin(
73+
_settings.catm1.pin,
74+
_settings.catm1.apn,
75+
_settings.catm1.login,
76+
_settings.catm1.pass,
77+
static_cast<RadioAccessTechnologyType>(_settings.catm1.rat) ,
78+
_settings.catm1.band))
6879
{
6980
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
7081
return NetworkConnectionState::ERROR;

src/CatM1ConnectionHandler.h

+1-8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class CatM1ConnectionHandler : public ConnectionHandler
4040
{
4141
public:
4242

43+
CatM1ConnectionHandler();
4344
CatM1ConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, RadioAccessTechnologyType rat = CATM1, uint32_t band = BAND_3 | BAND_20 | BAND_19, bool const keep_alive = true);
4445

4546

@@ -59,14 +60,6 @@ class CatM1ConnectionHandler : public ConnectionHandler
5960

6061
private:
6162

62-
const char * _pin;
63-
const char * _apn;
64-
const char * _login;
65-
const char * _pass;
66-
67-
RadioAccessTechnologyType _rat;
68-
uint32_t _band;
69-
7063
GSMUDP _gsm_udp;
7164
GSMClient _gsm_client;
7265
};

src/CellularConnectionHandler.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@
2121
/******************************************************************************
2222
CTOR/DTOR
2323
******************************************************************************/
24+
CellularConnectionHandler::CellularConnectionHandler()
25+
: ConnectionHandler(true, NetworkAdapter::CELL) {}
2426

2527
CellularConnectionHandler::CellularConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive)
2628
: ConnectionHandler{keep_alive, NetworkAdapter::CELL}
27-
, _pin(pin)
28-
, _apn(apn)
29-
, _login(login)
30-
, _pass(pass)
3129
{
30+
_settings.type = NetworkAdapter::CELL;
31+
strncpy(_settings.cell.pin, pin, sizeof(_settings.cell.pin)-1);
32+
strncpy(_settings.cell.apn, apn, sizeof(_settings.cell.apn)-1);
33+
strncpy(_settings.cell.login, login, sizeof(_settings.cell.login)-1);
34+
strncpy(_settings.cell.pass, pass, sizeof(_settings.cell.pass)-1);
3235

3336
}
3437

@@ -55,7 +58,7 @@ NetworkConnectionState CellularConnectionHandler::update_handleInit()
5558
{
5659
_cellular.begin();
5760
_cellular.setDebugStream(Serial);
58-
if (String(_pin).length() > 0 && !_cellular.unlockSIM(_pin)) {
61+
if (strlen(_settings.cell.pin) > 0 && !_cellular.unlockSIM(_settings.cell.pin)) {
5962
Debug.print(DBG_ERROR, F("SIM not present or wrong PIN"));
6063
return NetworkConnectionState::ERROR;
6164
}
@@ -64,7 +67,7 @@ NetworkConnectionState CellularConnectionHandler::update_handleInit()
6467

6568
NetworkConnectionState CellularConnectionHandler::update_handleConnecting()
6669
{
67-
if (!_cellular.connect(_apn, _login, _pass)) {
70+
if (!_cellular.connect(String(_settings.cell.apn), String(_settings.cell.login), String(_settings.cell.pass))) {
6871
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
6972
return NetworkConnectionState::ERROR;
7073
}

src/CellularConnectionHandler.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
class CellularConnectionHandler : public ConnectionHandler
3434
{
3535
public:
36-
36+
CellularConnectionHandler();
3737
CellularConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive = true);
3838

3939

@@ -53,11 +53,6 @@ class CellularConnectionHandler : public ConnectionHandler
5353

5454
private:
5555

56-
const char * _pin;
57-
const char * _apn;
58-
const char * _login;
59-
const char * _pass;
60-
6156
ArduinoCellular _cellular;
6257
TinyGsmClient _gsm_client = _cellular.getNetworkClient();
6358
};

src/EthernetConnectionHandler.cpp

+45-42
Original file line numberDiff line numberDiff line change
@@ -25,51 +25,41 @@
2525
CTOR/DTOR
2626
******************************************************************************/
2727

28-
EthernetConnectionHandler::EthernetConnectionHandler(unsigned long const timeout, unsigned long const responseTimeout, bool const keep_alive)
29-
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
30-
,_ip{INADDR_NONE}
31-
,_dns{INADDR_NONE}
32-
,_gateway{INADDR_NONE}
33-
,_netmask{INADDR_NONE}
34-
,_timeout{timeout}
35-
,_response_timeout{responseTimeout}
36-
{
37-
28+
static inline void fromIPAddress(const IPAddress src, models::ip_addr& dst) {
29+
if(src.type() == IPv4) {
30+
dst.dword[IPADDRESS_V4_DWORD_INDEX] = (uint32_t)src;
31+
} else if(src.type() == IPv6) {
32+
for(uint8_t i=0; i<sizeof(dst.bytes); i++) {
33+
dst.bytes[i] = src[i];
34+
}
35+
}
3836
}
3937

40-
EthernetConnectionHandler::EthernetConnectionHandler(const IPAddress ip, const IPAddress dns, const IPAddress gateway, const IPAddress netmask, unsigned long const timeout, unsigned long const responseTimeout, bool const keep_alive)
38+
EthernetConnectionHandler::EthernetConnectionHandler(
39+
unsigned long const timeout,
40+
unsigned long const responseTimeout,
41+
bool const keep_alive)
4142
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
42-
,_ip{ip}
43-
,_dns{dns}
44-
,_gateway{gateway}
45-
,_netmask{netmask}
46-
,_timeout{timeout}
47-
,_response_timeout{responseTimeout}
4843
{
49-
44+
memset(_settings.eth.ip.dword, 0, sizeof(_settings.eth.ip.dword));
45+
memset(_settings.eth.dns.dword, 0, sizeof(_settings.eth.dns.dword));
46+
memset(_settings.eth.gateway.dword, 0, sizeof(_settings.eth.gateway.dword));
47+
memset(_settings.eth.netmask.dword, 0, sizeof(_settings.eth.netmask.dword));
48+
_settings.eth.timeout = timeout;
49+
_settings.eth.response_timeout = responseTimeout;
5050
}
5151

52-
EthernetConnectionHandler::EthernetConnectionHandler(const char * ip, const char * dns, const char * gateway, const char * netmask, unsigned long const timeout, unsigned long const responseTimeout, bool const keep_alive)
52+
EthernetConnectionHandler::EthernetConnectionHandler(
53+
const IPAddress ip, const IPAddress dns, const IPAddress gateway, const IPAddress netmask,
54+
unsigned long const timeout, unsigned long const responseTimeout, bool const keep_alive)
5355
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
54-
,_ip{INADDR_NONE}
55-
,_dns{INADDR_NONE}
56-
,_gateway{INADDR_NONE}
57-
,_netmask{INADDR_NONE}
58-
,_timeout{timeout}
59-
,_response_timeout{responseTimeout}
6056
{
61-
if(!_ip.fromString(ip)) {
62-
_ip = INADDR_NONE;
63-
}
64-
if(!_dns.fromString(dns)) {
65-
_dns = INADDR_NONE;
66-
}
67-
if(!_gateway.fromString(gateway)) {
68-
_gateway = INADDR_NONE;
69-
}
70-
if(!_netmask.fromString(netmask)) {
71-
_netmask = INADDR_NONE;
72-
}
57+
fromIPAddress(ip, _settings.eth.ip);
58+
fromIPAddress(dns, _settings.eth.dns);
59+
fromIPAddress(gateway, _settings.eth.gateway);
60+
fromIPAddress(netmask, _settings.eth.netmask);
61+
_settings.eth.timeout = timeout;
62+
_settings.eth.response_timeout = responseTimeout;
7363
}
7464

7565
/******************************************************************************
@@ -87,16 +77,29 @@ NetworkConnectionState EthernetConnectionHandler::update_handleInit()
8777

8878
NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
8979
{
90-
if (_ip != INADDR_NONE) {
91-
if (Ethernet.begin(nullptr, _ip, _dns, _gateway, _netmask, _timeout, _response_timeout) == 0) {
80+
IPAddress ip(_settings.eth.ip.type, _settings.eth.ip.bytes);
81+
82+
// An ip address is provided -> static ip configuration
83+
if (ip != INADDR_NONE) {
84+
if (Ethernet.begin(nullptr, ip,
85+
IPAddress(_settings.eth.dns.type, _settings.eth.dns.bytes),
86+
IPAddress(_settings.eth.gateway.type, _settings.eth.gateway.bytes),
87+
IPAddress(_settings.eth.netmask.type, _settings.eth.netmask.bytes),
88+
_settings.eth.timeout,
89+
_settings.eth.response_timeout) == 0) {
90+
9291
Debug.print(DBG_ERROR, F("Failed to configure Ethernet, check cable connection"));
93-
Debug.print(DBG_VERBOSE, "timeout: %d, response timeout: %d", _timeout, _response_timeout);
92+
Debug.print(DBG_VERBOSE, "timeout: %d, response timeout: %d",
93+
_settings.eth.timeout, _settings.eth.response_timeout);
9494
return NetworkConnectionState::CONNECTING;
9595
}
96+
// An ip address is not provided -> dhcp configuration
9697
} else {
97-
if (Ethernet.begin(nullptr, _timeout, _response_timeout) == 0) {
98+
if (Ethernet.begin(nullptr, _settings.eth.timeout, _settings.eth.response_timeout) == 0) {
9899
Debug.print(DBG_ERROR, F("Waiting Ethernet configuration from DHCP server, check cable connection"));
99-
Debug.print(DBG_VERBOSE, "timeout: %d, response timeout: %d", _timeout, _response_timeout);
100+
Debug.print(DBG_VERBOSE, "timeout: %d, response timeout: %d",
101+
_settings.eth.timeout, _settings.eth.response_timeout);
102+
100103
return NetworkConnectionState::CONNECTING;
101104
}
102105
}

src/EthernetConnectionHandler.h

-8
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,6 @@ class EthernetConnectionHandler : public ConnectionHandler
6464

6565
private:
6666

67-
IPAddress _ip;
68-
IPAddress _dns;
69-
IPAddress _gateway;
70-
IPAddress _netmask;
71-
72-
unsigned long _timeout;
73-
unsigned long _response_timeout;
74-
7567
EthernetUDP _eth_udp;
7668
EthernetClient _eth_client;
7769

src/GSMConnectionHandler.cpp

+10-7
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,17 @@ __attribute__((weak)) void mkr_gsm_feed_watchdog()
4646
/******************************************************************************
4747
CTOR/DTOR
4848
******************************************************************************/
49+
GSMConnectionHandler::GSMConnectionHandler()
50+
: ConnectionHandler(true, NetworkAdapter::GSM) {}
4951

5052
GSMConnectionHandler::GSMConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive)
5153
: ConnectionHandler{keep_alive, NetworkAdapter::GSM}
52-
, _pin(pin)
53-
, _apn(apn)
54-
, _login(login)
55-
, _pass(pass)
5654
{
57-
55+
_settings.type = NetworkAdapter::GSM;
56+
strncpy(_settings.gsm.pin, pin, sizeof(_settings.gsm.pin)-1);
57+
strncpy(_settings.gsm.apn, apn, sizeof(_settings.gsm.apn)-1);
58+
strncpy(_settings.gsm.login, login, sizeof(_settings.gsm.login)-1);
59+
strncpy(_settings.gsm.pass, pass, sizeof(_settings.gsm.pass)-1);
5860
}
5961

6062
/******************************************************************************
@@ -74,7 +76,7 @@ NetworkConnectionState GSMConnectionHandler::update_handleInit()
7476
{
7577
mkr_gsm_feed_watchdog();
7678

77-
if (_gsm.begin(_pin) != GSM_READY)
79+
if (_gsm.begin(_settings.gsm.pin) != GSM_READY)
7880
{
7981
Debug.print(DBG_ERROR, F("SIM not present or wrong PIN"));
8082
return NetworkConnectionState::ERROR;
@@ -88,7 +90,8 @@ NetworkConnectionState GSMConnectionHandler::update_handleInit()
8890

8991
mkr_gsm_feed_watchdog();
9092

91-
GSM3_NetworkStatus_t const network_status = _gprs.attachGPRS(_apn, _login, _pass, true);
93+
GSM3_NetworkStatus_t const network_status = _gprs.attachGPRS(
94+
_settings.gsm.apn, _settings.gsm.login, _settings.gsm.pass, true);
9295
Debug.print(DBG_DEBUG, F("GPRS.attachGPRS(): %d"), network_status);
9396
if (network_status == GSM3_NetworkStatus_t::ERROR)
9497
{

src/GSMConnectionHandler.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
class GSMConnectionHandler : public ConnectionHandler
4040
{
4141
public:
42-
42+
GSMConnectionHandler();
4343
GSMConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive = true);
4444

4545

@@ -59,11 +59,6 @@ class GSMConnectionHandler : public ConnectionHandler
5959

6060
private:
6161

62-
const char * _pin;
63-
const char * _apn;
64-
const char * _login;
65-
const char * _pass;
66-
6762
GSM _gsm;
6863
GPRS _gprs;
6964
GSMUDP _gsm_udp;

src/LoRaConnectionHandler.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ typedef enum
4646
******************************************************************************/
4747
LoRaConnectionHandler::LoRaConnectionHandler(char const * appeui, char const * appkey, _lora_band const band, char const * channelMask, _lora_class const device_class)
4848
: ConnectionHandler{false, NetworkAdapter::LORA}
49-
, _appeui(appeui)
50-
, _appkey(appkey)
51-
, _band(band)
52-
, _channelMask(channelMask)
53-
, _device_class(device_class)
5449
{
55-
50+
_settings.type = NetworkAdapter::LORA;
51+
strncpy(_settings.lora.appeui, appeui, sizeof(_settings.lora.appeui)-1);
52+
strncpy(_settings.lora.appkey, appkey, sizeof(_settings.lora.appkey)-1);
53+
_settings.lora.band = band;
54+
strncpy(_settings.lora.channelMask, channelMask, sizeof(_settings.lora.channelMask)-1);
55+
_settings.lora.deviceClass = device_class;
5656
}
5757

5858
/******************************************************************************
@@ -103,26 +103,26 @@ bool LoRaConnectionHandler::available()
103103

104104
NetworkConnectionState LoRaConnectionHandler::update_handleInit()
105105
{
106-
if (!_modem.begin(_band))
106+
if (!_modem.begin((_lora_band)_settings.lora.band))
107107
{
108108
Debug.print(DBG_ERROR, F("Something went wrong; are you indoor? Move near a window, then reset and retry."));
109109
return NetworkConnectionState::ERROR;
110110
}
111111
// Set channelmask based on configuration
112-
if (_channelMask) {
113-
_modem.sendMask(_channelMask);
112+
if (_settings.lora.channelMask) {
113+
_modem.sendMask(_settings.lora.channelMask);
114114
}
115115
//A delay is required between _modem.begin(band) and _modem.joinOTAA(appeui, appkey) in order to let the chip to be correctly initialized before the connection attempt
116116
delay(100);
117-
_modem.configureClass(_device_class);
117+
_modem.configureClass((_lora_class)_settings.lora.deviceClass);
118118
delay(100);
119119
Debug.print(DBG_INFO, F("Connecting to the network"));
120120
return NetworkConnectionState::CONNECTING;
121121
}
122122

123123
NetworkConnectionState LoRaConnectionHandler::update_handleConnecting()
124124
{
125-
bool const network_status = _modem.joinOTAA(_appeui, _appkey);
125+
bool const network_status = _modem.joinOTAA(_settings.lora.appeui, _settings.lora.appkey);
126126
if (network_status != true)
127127
{
128128
Debug.print(DBG_ERROR, F("Connection to the network failed"));

src/LoRaConnectionHandler.h

-5
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,6 @@ class LoRaConnectionHandler : public ConnectionHandler
7373

7474
private:
7575

76-
char const * _appeui;
77-
char const * _appkey;
78-
_lora_band _band;
79-
char const * _channelMask;
80-
_lora_class _device_class;
8176
LoRaModem _modem;
8277
};
8378

0 commit comments

Comments
 (0)