-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy path0098-Add-time-functions-to-Gemalto-stack.patch
239 lines (226 loc) · 7.99 KB
/
0098-Add-time-functions-to-Gemalto-stack.patch
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
From b2ee3ce3781ead5b053f9e4c67b5836eda8e3762 Mon Sep 17 00:00:00 2001
From: giulcioffi <g.cioffi@arduino.cc>
Date: Tue, 20 Jul 2021 11:48:06 +0200
Subject: [PATCH 098/106] Add time functions to Gemalto stack
---
.../cellular/framework/API/CellularDevice.h | 6 +
.../cellular/framework/AT/AT_CellularDevice.h | 7 +
.../source/framework/AT/AT_CellularDevice.cpp | 151 ++++++++++++++++++
.../GEMALTO/CINTERION/GEMALTO_CINTERION.cpp | 2 +-
4 files changed, 165 insertions(+), 1 deletion(-)
diff --git a/connectivity/cellular/include/cellular/framework/API/CellularDevice.h b/connectivity/cellular/include/cellular/framework/API/CellularDevice.h
index 236f2d329a..a5fa3b68fd 100644
--- a/connectivity/cellular/include/cellular/framework/API/CellularDevice.h
+++ b/connectivity/cellular/include/cellular/framework/API/CellularDevice.h
@@ -342,6 +342,12 @@ public: //Pure virtual functions
*/
virtual void set_timeout(int timeout) = 0;
+ virtual unsigned long get_time() = 0;
+
+ virtual unsigned long get_local_time() = 0;
+
+ virtual bool set_time(unsigned long const epoch, int const timezone = 0) = 0;
+
public: //Common functions
diff --git a/connectivity/cellular/include/cellular/framework/AT/AT_CellularDevice.h b/connectivity/cellular/include/cellular/framework/AT/AT_CellularDevice.h
index 20fe6c139e..5ee2d5cf2d 100755
--- a/connectivity/cellular/include/cellular/framework/AT/AT_CellularDevice.h
+++ b/connectivity/cellular/include/cellular/framework/AT/AT_CellularDevice.h
@@ -98,6 +98,12 @@ public:
virtual void set_timeout(int timeout);
+ virtual unsigned long get_time();
+
+ virtual unsigned long get_local_time();
+
+ virtual bool set_time(unsigned long const epoch, int const timezone = 0);
+
virtual void modem_debug_on(bool on);
virtual nsapi_error_t init();
@@ -184,6 +190,7 @@ protected:
private:
void urc_nw_deact();
void urc_pdn_deact();
+ nsapi_error_t set_authomatic_time_zone_update();
protected:
ATHandler _at;
diff --git a/connectivity/cellular/source/framework/AT/AT_CellularDevice.cpp b/connectivity/cellular/source/framework/AT/AT_CellularDevice.cpp
index f71921e29a..0b2d37e714 100644
--- a/connectivity/cellular/source/framework/AT/AT_CellularDevice.cpp
+++ b/connectivity/cellular/source/framework/AT/AT_CellularDevice.cpp
@@ -144,6 +144,11 @@ void AT_CellularDevice::urc_pdn_deact()
send_disconnect_to_context(cid);
}
+nsapi_error_t AT_CellularDevice::set_authomatic_time_zone_update()
+{
+ return _at.at_cmd_discard("+CTZU", "=", "%d", 1);
+}
+
void AT_CellularDevice::send_disconnect_to_context(int cid)
{
tr_debug("send_disconnect_to_context, cid: %d", cid);
@@ -398,6 +403,152 @@ void AT_CellularDevice::set_timeout(int timeout)
}
}
+unsigned long AT_CellularDevice::get_time()
+{
+ tr_info("AT_CellularDevice::get_time\n");
+
+ struct tm now;
+
+ //Enable authomatic time zone update:
+ set_authomatic_time_zone_update();
+
+ _at.lock();
+
+ //"+CCLK: \"%y/%m/%d,%H:%M:%S+ZZ"
+ _at.cmd_start_stop("+CCLK", "?");
+ _at.resp_start("+CCLK:");
+
+ char time_str[50];
+ int time_len = 0;
+ while (_at.info_resp()) {
+ int date_len = _at.read_string(time_str, sizeof(time_str));
+ tr_debug("Read %d bytes for the date\n", date_len);
+ if (date_len > 0) {
+ char *ptr = time_str;
+
+ now.tm_year = std::strtol(ptr, NULL, 10) + 100; // mktime starts from 1900
+ ptr = ptr + 3; // Skip '/'
+ now.tm_mon = std::strtol(ptr, NULL, 10);
+ ptr = ptr + 3; // Skip '/'
+ now.tm_mday = std::strtol(ptr, NULL, 10);
+ ptr = ptr + 3; // Skip ','
+ now.tm_hour = std::strtol(ptr, NULL, 10);
+ ptr = ptr + 3; // Skip ':'
+ now.tm_min = std::strtol(ptr, NULL, 10);
+ ptr = ptr + 3;
+ now.tm_sec = std::strtol(ptr, NULL, 10);
+ }
+ }
+ _at.resp_stop();
+
+ _at.unlock();
+
+ tr_debug("Year: %d, month: %d, day:%d, hour:%d, minute:%d, second:%d\n", now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec);
+
+ // adjust for timezone offset which is +/- in 15 minute increments
+ long long result = mktime(&now);
+ time_t delta = ((time_str[18] - '0') * 10 + (time_str[19] - '0')) * (15 * 60);
+
+ if (time_str[17] == '-') {
+ result = result + delta;
+ } else if (time_str[17] == '+') {
+ result = result - delta;
+ }
+
+ return result;
+}
+
+unsigned long AT_CellularDevice::get_local_time()
+{
+ tr_info("AT_CellularDevice::get_local_time\n");
+
+ struct tm now;
+
+ _at.lock();
+
+ //"+CCLK: \"%y/%m/%d,%H:%M:%S"
+ _at.cmd_start_stop("+CCLK", "?");
+ _at.resp_start("+CCLK:");
+
+ char time_str[50];
+ int time_len = 0;
+ while (_at.info_resp()) {
+ int date_len = _at.read_string(time_str, sizeof(time_str));
+ tr_debug("Read %d bytes for the date\n", date_len);
+ if (date_len > 0) {
+ char *ptr = time_str;
+
+ now.tm_year = std::strtol(ptr, NULL, 10) + 100; // mktime starts from 1900
+ ptr = ptr + 3; // Skip '/'
+ now.tm_mon = std::strtol(ptr, NULL, 10);
+ ptr = ptr + 3; // Skip '/'
+ now.tm_mday = std::strtol(ptr, NULL, 10);
+ ptr = ptr + 3; // Skip ','
+ now.tm_hour = std::strtol(ptr, NULL, 10);
+ ptr = ptr + 3; // Skip ':'
+ now.tm_min = std::strtol(ptr, NULL, 10);
+ ptr = ptr + 3;
+ now.tm_sec = std::strtol(ptr, NULL, 10);
+ }
+ }
+ _at.resp_stop();
+
+ _at.unlock();
+
+ tr_debug("Year: %d, month: %d, day:%d, hour:%d, minute:%d, second:%d\n", now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec);
+
+ long long result = mktime(&now);
+
+ return result;
+
+}
+
+bool AT_CellularDevice::set_time(unsigned long const epoch, int const timezone)
+ {
+ char time_buf[20];
+ const uint8_t daysInMonth [] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 };
+ unsigned long unix_time = epoch - 946684800UL; /* Subtract seconds from 1970 to 2000 */
+
+ /* Convert unix_time from seconds to days */
+ int days = unix_time / (24 * 3600);
+ int leap;
+ int year = 0;
+ while (1) {
+ leap = year % 4 == 0;
+ if (days < 365 + leap) {
+ break;
+ }
+ days -= 365 + leap;
+ year++;
+ }
+
+ int month;
+ for (month = 1; month < 12; month++) {
+ uint8_t daysPerMonth = daysInMonth[month - 1];
+ if (leap && month == 2)
+ daysPerMonth++;
+ if (days < daysPerMonth) {
+ break;
+ }
+ days -= daysPerMonth;
+ }
+
+ int hours = (unix_time % 86400L) / 3600;
+ int minutes = (unix_time % 3600) / 60;
+ int seconds = unix_time % 60;
+
+ tr_debug("Year: %d, month: %d, day:%d, hour:%d, minute:%d, second:%d\n", year, month, days, hours, minutes, seconds);
+
+ sprintf(time_buf, "%02d/%02d/%02d,%02d:%02d:%02d+%02d", year, month, days, hours, minutes, seconds, timezone);
+
+ _at.lock();
+ _at.at_cmd_discard("+CCLK", "=", "%s", time_buf);
+ _at.unlock();
+
+ return true;
+
+}
+
void AT_CellularDevice::modem_debug_on(bool on)
{
_modem_debug_on = on;
diff --git a/connectivity/drivers/cellular/GEMALTO/CINTERION/GEMALTO_CINTERION.cpp b/connectivity/drivers/cellular/GEMALTO/CINTERION/GEMALTO_CINTERION.cpp
index f8f892fccd..459bba8eb1 100644
--- a/connectivity/drivers/cellular/GEMALTO/CINTERION/GEMALTO_CINTERION.cpp
+++ b/connectivity/drivers/cellular/GEMALTO/CINTERION/GEMALTO_CINTERION.cpp
@@ -241,7 +241,7 @@ void GEMALTO_CINTERION::init_module_ehs5e()
#if MBED_CONF_GEMALTO_CINTERION_PROVIDE_DEFAULT
#include "drivers/BufferedSerial.h"
-CellularDevice *CellularDevice::get_default_instance()
+CellularDevice *GEMALTO_CINTERION::get_default_instance()
{
static BufferedSerial serial(MBED_CONF_GEMALTO_CINTERION_TX, MBED_CONF_GEMALTO_CINTERION_RX, MBED_CONF_GEMALTO_CINTERION_BAUDRATE);
#if defined(MBED_CONF_GEMALTO_CINTERION_RTS) && defined(MBED_CONF_GEMALTO_CINTERION_CTS)
--
2.33.1