Skip to content

Commit 797c95f

Browse files
committed
Add UDP client feature
1 parent d1d6337 commit 797c95f

File tree

7 files changed

+471
-13
lines changed

7 files changed

+471
-13
lines changed

examples/soracom/soracom-uptime-off/soracom-uptime-off.ino

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ static const char APN[] = "soracom.io";
2020
static const char HOST[] = "uni.soracom.io";
2121
static constexpr int PORT = 23080;
2222

23+
template<typename MODULE> using CellularClient = WioCellularTcpClient2<MODULE>; // TCP
24+
// template<typename MODULE> using CellularClient = WioCellularUdpClient2<MODULE>; // UDP
25+
2326
static constexpr int INTERVAL = 1000 * 60 * 15; // [ms]
2427
static constexpr int POWER_ON_TIMEOUT = 1000 * 20; // [ms]
2528
static constexpr int NETWORK_TIMEOUT = 1000 * 60 * 3; // [ms]
29+
static constexpr int CONNECT_TIMEOUT = 1000 * 10; // [ms]
2630
static constexpr int RECEIVE_TIMEOUT = 1000 * 10; // [ms]
2731
static constexpr int POWER_OFF_DELAY_TIME = 1000 * 3; // [ms]
2832

@@ -111,13 +115,13 @@ static bool send(const JsonDocument &doc) {
111115
Serial.println(PORT);
112116

113117
{
114-
WioCellularTcpClient2<WioCellularModule> client{ WioCellular };
118+
CellularClient<WioCellularModule> client{ WioCellular };
115119
if (!client.open(WioNetwork.config.pdpContextId, HOST, PORT)) {
116120
Serial.printf("ERROR: Failed to open %s\n", WioCellularResultToString(client.getLastResult()));
117121
return false;
118122
}
119123

120-
if (!client.waitforConnect()) {
124+
if (!client.waitforConnect(CONNECT_TIMEOUT)) {
121125
Serial.printf("ERROR: Failed to connect %s\n", WioCellularResultToString(client.getLastResult()));
122126
return false;
123127
}

examples/soracom/soracom-uptime-psm/soracom-uptime-psm.ino

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ static const char APN[] = "soracom.io";
2020
static const char HOST[] = "uni.soracom.io";
2121
static constexpr int PORT = 23080;
2222

23+
template<typename MODULE> using CellularClient = WioCellularTcpClient2<MODULE>; // TCP
24+
// template<typename MODULE> using CellularClient = WioCellularUdpClient2<MODULE>; // UDP
25+
2326
static constexpr int INTERVAL = 1000 * 60 * 5; // [ms]
2427
static constexpr int POWER_ON_TIMEOUT = 1000 * 20; // [ms]
2528
static constexpr int NETWORK_TIMEOUT = 1000 * 60 * 3; // [ms]
29+
static constexpr int CONNECT_TIMEOUT = 1000 * 10; // [ms]
2630
static constexpr int RECEIVE_TIMEOUT = 1000 * 10; // [ms]
2731
static constexpr int PSM_PERIOD = 60 * 6; // [s]
2832
static constexpr int PSM_ACTIVE = 2; // [s]
@@ -133,13 +137,13 @@ static bool send(const JsonDocument &doc) {
133137
Serial.println(PORT);
134138

135139
{
136-
WioCellularTcpClient2<WioCellularModule> client{ WioCellular };
140+
CellularClient<WioCellularModule> client{ WioCellular };
137141
if (!client.open(WioNetwork.config.pdpContextId, HOST, PORT)) {
138142
Serial.printf("ERROR: Failed to open %s\n", WioCellularResultToString(client.getLastResult()));
139143
return false;
140144
}
141145

142-
if (!client.waitforConnect()) {
146+
if (!client.waitforConnect(CONNECT_TIMEOUT)) {
143147
Serial.printf("ERROR: Failed to connect %s\n", WioCellularResultToString(client.getLastResult()));
144148
return false;
145149
}

examples/soracom/soracom-uptime/soracom-uptime.ino

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ static const char APN[] = "soracom.io";
2020
static const char HOST[] = "uni.soracom.io";
2121
static constexpr int PORT = 23080;
2222

23+
template<typename MODULE> using CellularClient = WioCellularTcpClient2<MODULE>; // TCP
24+
// template<typename MODULE> using CellularClient = WioCellularUdpClient2<MODULE>; // UDP
25+
2326
static constexpr int INTERVAL = 1000 * 60 * 5; // [ms]
2427
static constexpr int POWER_ON_TIMEOUT = 1000 * 20; // [ms]
2528
static constexpr int NETWORK_TIMEOUT = 1000 * 60 * 3; // [ms]
29+
static constexpr int CONNECT_TIMEOUT = 1000 * 10; // [ms]
2630
static constexpr int RECEIVE_TIMEOUT = 1000 * 10; // [ms]
2731

2832
static void abortHandler(int sig) {
@@ -105,13 +109,13 @@ static bool send(const JsonDocument &doc) {
105109
Serial.println(PORT);
106110

107111
{
108-
WioCellularTcpClient2<WioCellularModule> client{ WioCellular };
112+
CellularClient<WioCellularModule> client{ WioCellular };
109113
if (!client.open(WioNetwork.config.pdpContextId, HOST, PORT)) {
110114
Serial.printf("ERROR: Failed to open %s\n", WioCellularResultToString(client.getLastResult()));
111115
return false;
112116
}
113117

114-
if (!client.waitforConnect()) {
118+
if (!client.waitforConnect(CONNECT_TIMEOUT)) {
115119
Serial.printf("ERROR: Failed to connect %s\n", WioCellularResultToString(client.getLastResult()));
116120
return false;
117121
}

src/WioCellular.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ extern WioCellularNetwork WioNetwork;
5353

5454
#include "client/WioCellularArduinoTcpClient.hpp"
5555
#include "client/WioCellularTcpClient2.hpp"
56+
#include "client/WioCellularUdpClient2.hpp"
5657

5758
template <typename MODULE>
5859
using WioCellularTcpClient2 = wiocellular::client::WioCellularTcpClient2<MODULE>;
60+
template <typename MODULE>
61+
using WioCellularUdpClient2 = wiocellular::client::WioCellularUdpClient2<MODULE>;
5962

6063
#endif // WIOCELLULAR_HPP

src/client/WioCellularTcpClient2.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
#ifndef WIOCELLULARTCPCLIENT2_HPP
88
#define WIOCELLULARTCPCLIENT2_HPP
99

10-
#include <bitset>
1110
#include <memory>
1211
#include "internal/CountdownTimer.hpp"
1312
#include "internal/Misc.hpp"
13+
#include "internal/UsedConnectIds.hpp"
1414
#include "WioCellularResult.hpp"
1515

1616
namespace wiocellular::client
@@ -66,7 +66,6 @@ namespace wiocellular::client
6666
State State_;
6767
WioCellularResult LastResult_;
6868
std::unique_ptr<typename MODULE::UrcHandler> UrcHandler_;
69-
inline static std::bitset<12> UsedConnectIds_;
7069
int ConnectId_;
7170
bool ReceivedNofity_;
7271
uint32_t openedTime_;
@@ -195,9 +194,9 @@ namespace wiocellular::client
195194

196195
{
197196
int connectId = -1;
198-
for (size_t i = 0; i < UsedConnectIds_.size(); ++i)
197+
for (size_t i = 0; i < wiocellular::internal::UsedConnectIds.size(); ++i)
199198
{
200-
if (!UsedConnectIds_.test(i))
199+
if (!wiocellular::internal::UsedConnectIds.test(i))
201200
{
202201
connectId = i;
203202
break;
@@ -208,7 +207,7 @@ namespace wiocellular::client
208207
LastResult_ = WioCellularResult::InsufficientResources;
209208
return false;
210209
}
211-
UsedConnectIds_.set(connectId);
210+
wiocellular::internal::UsedConnectIds.set(connectId);
212211
ConnectId_ = connectId;
213212
}
214213

@@ -218,7 +217,7 @@ namespace wiocellular::client
218217
if (const auto result = WioCellular.openSocket2(cid, ConnectId_, "TCP", ipAddress, remotePort, 0); result != WioCellularResult::Ok)
219218
{
220219
UrcHandler_ = nullptr;
221-
UsedConnectIds_.reset(ConnectId_);
220+
wiocellular::internal::UsedConnectIds.reset(ConnectId_);
222221
LastResult_ = result;
223222
return false;
224223
}
@@ -297,7 +296,7 @@ namespace wiocellular::client
297296
}
298297

299298
UrcHandler_ = nullptr;
300-
UsedConnectIds_.reset(ConnectId_);
299+
wiocellular::internal::UsedConnectIds.reset(ConnectId_);
301300
setState(State::Closed);
302301
}
303302

0 commit comments

Comments
 (0)