Skip to content

Commit b23eb10

Browse files
giulcioffifacchinm
authored andcommitted
Implement GSM library
1 parent 74bfa8d commit b23eb10

File tree

9 files changed

+395
-3
lines changed

9 files changed

+395
-3
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <GSM.h>
2+
3+
REDIRECT_STDOUT_TO(Serial);
4+
5+
#include "arduino_secrets.h"
6+
char pin[] = SECRET_PIN;
7+
char apn[] = SECRET_APN;
8+
char username[] = SECRET_USERNAME;
9+
char pass[] = SECRET_PASSWORD;
10+
11+
const char server[] = "www.example.com";
12+
const char* ip_address;
13+
int port = 80;
14+
15+
GSMClient client;
16+
17+
void setup() {
18+
Serial.begin(115200);
19+
while(!Serial) {}
20+
21+
GSM.begin(pin, apn, username, pass, CATNB);
22+
23+
Serial.println("\nStarting connection to server...");
24+
// if you get a connection, report back via serial:
25+
if (client.connect(server, port)) {
26+
Serial.println("connected to server");
27+
// Make a HTTP request:
28+
client.println("GET / HTTP/1.1");
29+
client.print("Host: ");
30+
client.println(server);
31+
client.println("Connection: close");
32+
client.println();
33+
} else {
34+
Serial.println("unable to connect to server");
35+
}
36+
37+
}
38+
39+
void loop() {
40+
41+
// if there are incoming bytes available
42+
// from the server, read them and print them:
43+
while (client.available()) {
44+
char c = client.read();
45+
Serial.write(c);
46+
}
47+
48+
// if the server's disconnected, stop the client:
49+
if (!client.connected()) {
50+
Serial.println();
51+
Serial.println("disconnecting from server.");
52+
client.stop();
53+
54+
// do nothing forevermore:
55+
while (true);
56+
}
57+
58+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#define SECRET_PIN ""
2+
#define SECRET_APN ""
3+
#define SECRET_USERNAME ""
4+
#define SECRET_PASSWORD ""

libraries/GSM/library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=GSM
2+
version=1.0
3+
author=Arduino
4+
maintainer=Arduino <[email protected]>
5+
sentence=GSM wrapper
6+
paragraph=
7+
category=Other
8+
url=http://www.arduino.cc/en/Reference/GSM
9+
architectures=mbed,ArduinoCore-mbed,mbed_portenta

libraries/GSM/src/GSM.cpp

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include "GSM.h"
2+
3+
#include "mbed.h"
4+
#include "CellularLog.h"
5+
6+
#define MAXRETRY 3
7+
8+
mbed::CellularDevice *mbed::CellularDevice::get_default_instance()
9+
{
10+
static BufferedSerial serial(MBED_CONF_GEMALTO_CINTERION_TX, MBED_CONF_GEMALTO_CINTERION_RX, 115200);
11+
#if defined(MBED_CONF_GEMALTO_CINTERION_RTS) && defined(MBED_CONF_GEMALTO_CINTERION_CTS)
12+
serial.set_flow_control(mbed::SerialBase::RTSCTS, MBED_CONF_GEMALTO_CINTERION_RTS, MBED_CONF_GEMALTO_CINTERION_CTS);
13+
#endif
14+
static GEMALTO_CINTERION device(&serial);
15+
return &device;
16+
}
17+
18+
int arduino::GSMClass::begin(const char* pin, const char* apn, const char* username, const char* password, RadioAccessTechnologyType rat) {
19+
if (gsm_if == nullptr) {
20+
printf("Invalid gsm_if\n");
21+
return 0;
22+
}
23+
24+
static mbed::DigitalOut on(PJ_7, 1);
25+
static mbed::DigitalOut rts(MBED_CONF_GEMALTO_CINTERION_RTS, 0);
26+
27+
_context->set_sim_pin(pin);
28+
29+
_device->init();
30+
31+
_context->set_authentication_type((mbed::CellularContext::AuthenticationType)1);
32+
33+
_pin = pin;
34+
_apn = apn;
35+
_username = username;
36+
_password = password;
37+
_rat = rat;
38+
_context->set_credentials(apn, username, password);
39+
40+
_context->set_access_technology(rat);
41+
42+
uint8_t connect_status = NSAPI_ERROR_AUTH_FAILURE;
43+
uint8_t retryCount = 0;
44+
while(connect_status != NSAPI_ERROR_OK && retryCount < MAXRETRY) {
45+
46+
connect_status = _context->connect(pin, apn, username, password);
47+
retryCount++;
48+
49+
if (connect_status == NSAPI_ERROR_AUTH_FAILURE) {
50+
printf("Authentication Failure. Exiting application.\n");
51+
} else if (connect_status == NSAPI_ERROR_OK || connect_status == NSAPI_ERROR_IS_CONNECTED) {
52+
connect_status = NSAPI_ERROR_OK;
53+
printf("Connection Established.\n");
54+
} else if (retryCount > 2) {
55+
printf("Fatal connection failure: %d\n", connect_status);
56+
} else {
57+
printf("Couldn't connect, will retry...\n");
58+
continue;
59+
}
60+
61+
}
62+
63+
return connect_status;
64+
}
65+
66+
void arduino::GSMClass::end() {
67+
68+
}
69+
70+
int arduino::GSMClass::disconnect() {
71+
72+
}
73+
74+
unsigned long arduino::GSMClass::getTime()
75+
{
76+
return _device->get_time();
77+
}
78+
79+
unsigned long arduino::GSMClass::getLocalTime()
80+
{
81+
return _device->get_local_time();
82+
}
83+
84+
bool arduino::GSMClass::setTime(unsigned long const epoch, int const timezone)
85+
{
86+
return _device->set_time(epoch, timezone);
87+
}
88+
89+
static PlatformMutex trace_mutex;
90+
91+
static void trace_wait()
92+
{
93+
trace_mutex.lock();
94+
}
95+
96+
static void trace_release()
97+
{
98+
trace_mutex.unlock();
99+
}
100+
101+
static char* trace_time(size_t ss)
102+
{
103+
static char time_st[50];
104+
auto ms = std::chrono::time_point_cast<std::chrono::milliseconds>(rtos::Kernel::Clock::now()).time_since_epoch().count();
105+
//snprintf(time_st, 49, "[%08llums]", ms);
106+
snprintf(time_st, 1, "\n");
107+
return time_st;
108+
}
109+
110+
111+
void arduino::GSMClass::debug() {
112+
113+
#ifndef CELLULAR_DEMO_TRACING_H_
114+
#define CELLULAR_DEMO_TRACING_H_
115+
116+
#if MBED_CONF_MBED_TRACE_ENABLE
117+
118+
mbed_trace_init();
119+
mbed_trace_prefix_function_set( &trace_time );
120+
121+
mbed_trace_mutex_wait_function_set(trace_wait);
122+
mbed_trace_mutex_release_function_set(trace_release);
123+
124+
mbed_cellular_trace::mutex_wait_function_set(trace_wait);
125+
mbed_cellular_trace::mutex_release_function_set(trace_release);
126+
#endif
127+
#endif
128+
129+
}
130+
131+
NetworkInterface* arduino::GSMClass::getNetwork() {
132+
return gsm_if;
133+
}
134+
135+
arduino::GSMClass GSM(mbed::CellularContext::get_default_instance());

libraries/GSM/src/GSM.h

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
GSM.h - Library for GSM on mbed platforms.
3+
Copyright (c) 2011-2021 Arduino LLC. All right reserved.
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
This library is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
Lesser General Public License for more details.
12+
You should have received a copy of the GNU Lesser General Public
13+
License along with this library; if not, write to the Free Software
14+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15+
*/
16+
17+
#ifndef GSM_h
18+
#define GSM_h
19+
20+
#include <inttypes.h>
21+
22+
#include "SocketHelpers.h"
23+
#include "CellularContext.h"
24+
25+
#include "GEMALTO_CINTERION.h"
26+
#include "GENERIC_AT3GPP.h"
27+
28+
#include "drivers/BufferedSerial.h"
29+
30+
#define MBED_CONF_GEMALTO_CINTERION_TX PA_0
31+
#define MBED_CONF_GEMALTO_CINTERION_RX PI_9
32+
#define MBED_CONF_GEMALTO_CINTERION_RTS PI_10
33+
//#define MBED_CONF_GEMALTO_CINTERION_CTS PI_13
34+
#define MBED_CONF_APP_SOCK_TYPE 1
35+
36+
namespace arduino {
37+
38+
typedef void* (*voidPrtFuncPtr)(void);
39+
40+
class GSMClass : public MbedSocketClass {
41+
public:
42+
43+
GSMClass(NetworkInterface* _if)
44+
: _rat(CATNB),
45+
gsm_if(_if),
46+
_context((mbed::CellularContext*)gsm_if),
47+
_device(((mbed::CellularContext*)gsm_if)->get_device()) {}
48+
49+
/* Start GSM connection.
50+
* Configure the credentials into the device.
51+
*
52+
* param pin: Pointer to the pin string.
53+
* param apn: Pointer to the apn string.
54+
* param username: Pointer to the username string.
55+
* param password: Pointer to the password string.
56+
* param rat: Radio Access Technology.
57+
*
58+
* return: 0 in case of success, negative number in case of failure
59+
*/
60+
int begin(const char* pin, const char* apn, const char* username, const char* password, RadioAccessTechnologyType rat = CATNB);
61+
62+
/*
63+
* Disconnect from the network
64+
*
65+
* return: one value of wl_status_t enum
66+
*/
67+
int disconnect(void);
68+
69+
void end(void);
70+
71+
unsigned long getTime();
72+
73+
unsigned long getLocalTime();
74+
75+
bool setTime(unsigned long const epoch, int const timezone = 0);
76+
77+
void debug(void);
78+
79+
int ping(const char* hostname, uint8_t ttl = 128);
80+
int ping(const String& hostname, uint8_t ttl = 128);
81+
int ping(IPAddress host, uint8_t ttl = 128);
82+
83+
friend class GSMClient;
84+
friend class GSMUDP;
85+
86+
NetworkInterface* getNetwork();
87+
88+
private:
89+
const char* _pin = nullptr;
90+
const char* _apn = nullptr;
91+
const char* _username = nullptr;
92+
const char* _password = nullptr;
93+
RadioAccessTechnologyType _rat;
94+
NetworkInterface* gsm_if = nullptr;
95+
mbed::CellularContext* _context = nullptr;
96+
mbed::CellularDevice* _device = nullptr;
97+
};
98+
99+
}
100+
101+
extern GSMClass GSM;
102+
103+
#include "GSMClient.h"
104+
#include "GSMUdp.h"
105+
106+
#endif

libraries/GSM/src/GSMClient.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
GSMClient.h
3+
Copyright (c) 2021 Arduino SA. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef gsmclient_h
21+
#define gsmclient_h
22+
23+
#include "GSM.h"
24+
#include "MbedClient.h"
25+
26+
namespace arduino {
27+
28+
class GSMClient : public MbedClient {
29+
NetworkInterface *getNetwork() {
30+
return GSM.getNetwork();
31+
}
32+
};
33+
34+
}
35+
36+
#endif

libraries/GSM/src/GSMUdp.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
WiFiUdp.h
3+
Copyright (c) 2021 Arduino SA. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef gsmudp_h
21+
#define gsmudp_h
22+
23+
#include "GSM.h"
24+
#include "MbedUdp.h"
25+
26+
namespace arduino {
27+
28+
class GSMUDP : public MbedUDP {
29+
NetworkInterface *getNetwork() {
30+
return GSM.getNetwork();
31+
}
32+
};
33+
34+
}
35+
36+
#endif

0 commit comments

Comments
 (0)