Skip to content

LoRa support #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ paragraph=Originally part of ArduinoIoTCloud
category=Communication
url=https://github.com/arduino-libraries/Arduino_ConnectionHandler
architectures=samd,esp32,esp8266
depends=Arduino_DebugUtils, WiFi101, WiFiNINA, MKRGSM, MKRNB
depends=Arduino_DebugUtils, WiFi101, WiFiNINA, MKRGSM, MKRNB, MKRWAN
55 changes: 55 additions & 0 deletions src/Arduino_ConnectionHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
This file is part of ArduinoIoTCloud.

Copyright 2019 ARDUINO SA (http://www.arduino.cc/)

This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html

You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to [email protected].
*/

/******************************************************************************
INCLUDE
******************************************************************************/

#include "Arduino_ConnectionHandler.h"

/******************************************************************************
PUBLIC MEMBER FUNCTIONS
******************************************************************************/

void ConnectionHandler::addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback) {
switch (event) {
case NetworkConnectionEvent::CONNECTED: _on_connect_event_callback = callback; break;
case NetworkConnectionEvent::DISCONNECTED: _on_disconnect_event_callback = callback; break;
case NetworkConnectionEvent::ERROR: _on_error_event_callback = callback; break;
case NetworkConnectionEvent::INIT: ; break;
case NetworkConnectionEvent::CONNECTING: ; break;
case NetworkConnectionEvent::DISCONNECTING: ; break;
case NetworkConnectionEvent::CLOSED: ; break;
}
}

void ConnectionHandler::addConnectCallback(OnNetworkEventCallback callback) {
_on_connect_event_callback = callback;
}
void ConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) {
_on_disconnect_event_callback = callback;
}
void ConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) {
_on_error_event_callback = callback;
}

void ConnectionHandler::execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg) {
if (callback) {
(*callback)(callback_arg);
}
}

42 changes: 21 additions & 21 deletions src/Arduino_ConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
INCLUDES
******************************************************************************/

#include <Client.h>
#include <Udp.h>

#include <Arduino_DebugUtils.h>

/******************************************************************************
Expand Down Expand Up @@ -57,30 +54,27 @@ class ConnectionHandler {
virtual void init() = 0;
virtual void check() = 0;
virtual void update() = 0;
virtual unsigned long getTime() = 0;
virtual Client &getClient();
virtual UDP &getUDP();

virtual NetworkConnectionState getStatus() {
return netConnectionState;
}
virtual void connect();
virtual void disconnect();
virtual void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback);
virtual void addConnectCallback(OnNetworkEventCallback callback);
virtual void addDisconnectCallback(OnNetworkEventCallback callback);
virtual void addErrorCallback(OnNetworkEventCallback callback);

private:
OnNetworkEventCallback _on_connect_event_callback,
_on_disconnect_event_callback,
_on_error_event_callback;
void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback);
void addConnectCallback(OnNetworkEventCallback callback);
void addDisconnectCallback(OnNetworkEventCallback callback);
void addErrorCallback(OnNetworkEventCallback callback);

protected:
OnNetworkEventCallback _on_connect_event_callback = NULL,
_on_disconnect_event_callback = NULL,
_on_error_event_callback = NULL;

unsigned long lastValidTimestamp = 0; /* UNUSED */
NetworkConnectionState netConnectionState = NetworkConnectionState::DISCONNECTED;

static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg);

};

#ifdef ARDUINO_SAMD_MKR1000
Expand Down Expand Up @@ -121,6 +115,11 @@ class ConnectionHandler {
#define NETWORK_CONNECTED NB_NetworkStatus_t::GPRS_READY
#endif

#if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310)
#include <MKRWAN.h>
#define BOARD_HAS_LORA
#endif

#if defined(ARDUINO_ESP8266_ESP12) \
|| defined(ESP8266) \
|| defined(ESP8266_ESP01) \
Expand Down Expand Up @@ -163,12 +162,13 @@ class ConnectionHandler {
#define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED
#endif

#ifdef BOARD_HAS_WIFI
#include "Arduino_WiFiConnectionHandler.h"
#elif defined(BOARD_HAS_GSM)
#include "Arduino_GSMConnectionHandler.h"
#elif defined(BOARD_HAS_NB)
#include "Arduino_NBConnectionHandler.h"

#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB)
#include "Arduino_TcpIpConnectionHandler.h"
#endif

#if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310)
#include "Arduino_LPWANConnectionHandler.h"
#endif

#endif /* CONNECTION_HANDLER_H_ */
33 changes: 1 addition & 32 deletions src/Arduino_GSMConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ GSMConnectionHandler::GSMConnectionHandler(const char *pin, const char *apn, con
pass(pass),
lastConnectionTickTime(millis()),
connectionTickTimeInterval(CHECK_INTERVAL_IDLE),
keepAlive(_keepAlive),
_on_connect_event_callback(NULL),
_on_disconnect_event_callback(NULL),
_on_error_event_callback(NULL) {
keepAlive(_keepAlive) {
}

/******************************************************************************
Expand All @@ -71,34 +68,6 @@ void GSMConnectionHandler::init() {
}
}

void GSMConnectionHandler::addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback) {
switch (event) {
case NetworkConnectionEvent::CONNECTED: _on_connect_event_callback = callback; break;
case NetworkConnectionEvent::DISCONNECTED: _on_disconnect_event_callback = callback; break;
case NetworkConnectionEvent::ERROR: _on_error_event_callback = callback; break;
case NetworkConnectionEvent::INIT: ; break;
case NetworkConnectionEvent::CONNECTING: ; break;
case NetworkConnectionEvent::DISCONNECTING: ; break;
case NetworkConnectionEvent::CLOSED: ; break;
}
}

void GSMConnectionHandler::addConnectCallback(OnNetworkEventCallback callback) {
_on_connect_event_callback = callback;
}
void GSMConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) {
_on_disconnect_event_callback = callback;
}
void GSMConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) {
_on_error_event_callback = callback;
}

void GSMConnectionHandler::execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg) {
if (callback) {
(*callback)(callback_arg);
}
}

unsigned long GSMConnectionHandler::getTime() {
return gsmAccess.getTime();
}
Expand Down
15 changes: 3 additions & 12 deletions src/Arduino_GSMConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
INCLUDE
******************************************************************************/

#include "Arduino_ConnectionHandler.h"
#include "Arduino_TcpIpConnectionHandler.h"


#ifdef BOARD_HAS_GSM /* Only compile if this is a board with GSM */

/******************************************************************************
CLASS DECLARATION
******************************************************************************/

class GSMConnectionHandler : public ConnectionHandler {
class GSMConnectionHandler : public TcpIpConnectionHandler {
public:
GSMConnectionHandler(const char *pin, const char *apn, const char *login, const char *pass, const bool keepAlive = true);

Expand All @@ -55,11 +56,6 @@ class GSMConnectionHandler : public ConnectionHandler {
virtual void disconnect();
virtual void connect();

virtual void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback);
virtual void addConnectCallback(OnNetworkEventCallback callback);
virtual void addDisconnectCallback(OnNetworkEventCallback callback);
virtual void addErrorCallback(OnNetworkEventCallback callback);

private:

void changeConnectionState(NetworkConnectionState _newState);
Expand All @@ -79,11 +75,6 @@ class GSMConnectionHandler : public ConnectionHandler {

bool keepAlive;

OnNetworkEventCallback _on_connect_event_callback,
_on_disconnect_event_callback,
_on_error_event_callback;

static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg);
};

#endif /* #ifdef BOARD_HAS_GSM */
Expand Down
53 changes: 53 additions & 0 deletions src/Arduino_LPWANConnectionHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
This file is part of ArduinoIoTCloud.

Copyright 2019 ARDUINO SA (http://www.arduino.cc/)

This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html

You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to [email protected].
*/

#ifndef ARDUINO_LPWAN_CONNECTION_HANDLER_H_
#define ARDUINO_LPWAN_CONNECTION_HANDLER_H_

/******************************************************************************
INCLUDES
******************************************************************************/

#include <Arduino_DebugUtils.h>
#include <Arduino_ConnectionHandler.h>

/******************************************************************************
CLASS DECLARATION
******************************************************************************/

class LPWANConnectionHandler : public ConnectionHandler {
public:
virtual void init() = 0;
virtual void check() = 0;
virtual void update() = 0;
virtual unsigned long getTime() = 0;

virtual int write(const uint8_t *buf, size_t size) = 0;
virtual int read() = 0;
virtual bool available() = 0;

virtual void connect() = 0;
virtual void disconnect() = 0;

};

#if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310)
#include "Arduino_LoRaConnectionHandler.h"
#endif


#endif /* LPWAN_CONNECTION_HANDLER_H_ */
Loading