Skip to content

Commit 7145fd6

Browse files
More patches, support time
1 parent 4461602 commit 7145fd6

File tree

8 files changed

+255
-18
lines changed

8 files changed

+255
-18
lines changed

connectivity/cellular/include/cellular/framework/AT/AT_CellularContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ class AT_CellularContext : public CellularContext {
139139
nsapi_error_t check_operation(nsapi_error_t err, ContextOperation op);
140140
void ciot_opt_cb(mbed::CellularNetwork::CIoT_Supported_Opt ciot_opt);
141141
virtual void do_connect_with_retry();
142+
143+
protected:
142144
void set_cid(int cid);
143145

144146
private:

connectivity/cellular/source/framework/AT/AT_CellularContext.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ bool AT_CellularContext::get_context()
370370
int pdp_type_len = _at.read_string(pdp_type_from_context, sizeof(pdp_type_from_context));
371371
if (pdp_type_len > 0) {
372372
apn_len = _at.read_string(apn, sizeof(apn));
373-
if (apn_len > 0) {
373+
if (apn_len >= 0) {
374374
if (_apn && (strcmp(apn, _apn) != 0)) {
375375
tr_debug("CID %d APN \"%s\"", cid, apn);
376376
continue;
@@ -388,9 +388,6 @@ bool AT_CellularContext::get_context()
388388
set_cid(cid);
389389
}
390390
}
391-
else {
392-
cid_max = 0;
393-
}
394391
}
395392
}
396393

connectivity/drivers/cellular/GEMALTO/COMPONENT_GEMALTO_CINTERION/GEMALTO_CINTERION.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "GEMALTO_CINTERION_CellularContext.h"
1919
#include "GEMALTO_CINTERION_CellularInformation.h"
20+
#include "GEMALTO_CINTERION_CellularNetwork.h"
2021
#include "GEMALTO_CINTERION.h"
2122
#include "AT_CellularNetwork.h"
2223
#include "CellularLog.h"
@@ -30,6 +31,82 @@ GEMALTO_CINTERION::GEMALTO_CINTERION(FileHandle *fh) : AT_CellularDevice(fh)
3031
{
3132
}
3233

34+
time_t GEMALTO_CINTERION::get_time()
35+
{
36+
tr_info("GEMALTO_CINTERION::get_time\n");
37+
38+
//Enable automatic time zone update:
39+
set_automatic_time_zone_update();
40+
41+
_at.lock();
42+
43+
//"+CCLK: \"%y/%m/%d,%H:%M:%S+ZZ"
44+
_at.cmd_start_stop("+CCLK", "?");
45+
_at.resp_start("+CCLK:");
46+
47+
char time_str[50];
48+
time_t now = 0;
49+
while (_at.info_resp()) {
50+
int date_len = _at.read_string(time_str, sizeof(time_str));
51+
tr_debug("Read %d bytes for the date\n", date_len);
52+
if (date_len > 0) {
53+
now = parse_time(time_str);
54+
}
55+
}
56+
_at.resp_stop();
57+
58+
_at.unlock();
59+
60+
// adjust for timezone offset which is +/- in 15 minute increments
61+
time_t delta = ((time_str[18] - '0') * 10 + (time_str[19] - '0')) * (15 * 60);
62+
63+
if (time_str[17] == '-') {
64+
now = now + delta;
65+
} else if (time_str[17] == '+') {
66+
now = now - delta;
67+
}
68+
69+
return now;
70+
}
71+
72+
time_t GEMALTO_CINTERION::get_local_time()
73+
{
74+
tr_info("GEMALTO_CINTERION::get_local_time\n");
75+
76+
_at.lock();
77+
78+
//"+CCLK: \"%y/%m/%d,%H:%M:%S"
79+
_at.cmd_start_stop("+CCLK", "?");
80+
_at.resp_start("+CCLK:");
81+
82+
char time_str[50];
83+
time_t now;
84+
while (_at.info_resp()) {
85+
int date_len = _at.read_string(time_str, sizeof(time_str));
86+
tr_debug("Read %d bytes for the date\n", date_len);
87+
if (date_len > 0) {
88+
now = parse_time(time_str);
89+
}
90+
}
91+
_at.resp_stop();
92+
93+
_at.unlock();
94+
95+
return now;
96+
}
97+
98+
void GEMALTO_CINTERION::set_time(time_t const epoch, int const timezone)
99+
{
100+
char time_buf[21];
101+
strftime(time_buf, sizeof(time_buf), "%g/%m/%d,%H:%M:%S", gmtime(&epoch));
102+
snprintf(time_buf + 17, 4, "%+03d", timezone);
103+
104+
_at.lock();
105+
_at.at_cmd_discard("+CCLK", "=", "%s", time_buf);
106+
_at.unlock();
107+
}
108+
109+
33110
AT_CellularContext *GEMALTO_CINTERION::create_context_impl(ATHandler &at, const char *apn, bool cp_req, bool nonip_req)
34111
{
35112
return new GEMALTO_CINTERION_CellularContext(at, this, apn, cp_req, nonip_req);
@@ -43,6 +120,11 @@ AT_CellularInformation *GEMALTO_CINTERION::open_information_impl(ATHandler &at)
43120
return AT_CellularDevice::open_information_impl(at);
44121
}
45122

123+
AT_CellularNetwork *GEMALTO_CINTERION::open_network_impl(ATHandler &at)
124+
{
125+
return new GEMALTO_CINTERION_CellularNetwork(at, *this, _module);
126+
}
127+
46128
nsapi_error_t GEMALTO_CINTERION::init()
47129
{
48130
nsapi_error_t err = AT_CellularDevice::init();
@@ -233,6 +315,26 @@ void GEMALTO_CINTERION::init_module_tx62()
233315
_module = ModuleTX62;
234316
}
235317

318+
time_t GEMALTO_CINTERION::parse_time(char const *time_str) {
319+
struct tm now;
320+
321+
now.tm_year = std::strtol(time_str, NULL, 10) + 100; // mktime starts from 1900
322+
time_str += 3; // Skip '/'
323+
now.tm_mon = std::strtol(time_str, NULL, 10);
324+
time_str += 3; // Skip '/'
325+
now.tm_mday = std::strtol(time_str, NULL, 10);
326+
time_str += 3; // Skip ','
327+
now.tm_hour = std::strtol(time_str, NULL, 10);
328+
time_str += 3; // Skip ':'
329+
now.tm_min = std::strtol(time_str, NULL, 10);
330+
time_str += 3;
331+
now.tm_sec = std::strtol(time_str, NULL, 10);
332+
333+
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);
334+
335+
return mktime(&now);
336+
}
337+
236338
#if MBED_CONF_GEMALTO_CINTERION_PROVIDE_DEFAULT
237339
#include "drivers/BufferedSerial.h"
238340
CellularDevice *CellularDevice::get_default_instance()

connectivity/drivers/cellular/GEMALTO/COMPONENT_GEMALTO_CINTERION/GEMALTO_CINTERION.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,31 @@ class GEMALTO_CINTERION : public AT_CellularDevice {
4848
};
4949
static Module get_module();
5050

51+
/**
52+
* @brief Get the RTC time from the Cinterion, as a UNIX seconds timestamp in UTC
53+
*/
54+
time_t get_time();
55+
56+
/**
57+
* @brief Get the RTC time from the Cinterion, as a UNIX seconds timestamp in the local time zone
58+
*/
59+
time_t get_local_time();
60+
61+
/**
62+
* @brief Set the RTC time on the Cinterion.
63+
*
64+
* Note that any time set will be overwritten once the modem can get time from a cellular network
65+
*
66+
* @param timestamp UNIX timestamp to set, in the local time zone
67+
* @param timezone Local time zone, as a signed number of 15-minute increments from UTC time. Example:
68+
* passing -8 would indicate UTC-2.
69+
*/
70+
virtual void set_time(time_t timestamp, int const timezone = 0);
71+
5172
protected: // AT_CellularDevice
5273
virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn, bool cp_req = false, bool nonip_req = false);
5374
virtual AT_CellularInformation *open_information_impl(ATHandler &at);
75+
AT_CellularNetwork *open_network_impl(ATHandler &at) override;
5476

5577
protected:
5678
virtual nsapi_error_t init();
@@ -62,6 +84,9 @@ class GEMALTO_CINTERION : public AT_CellularDevice {
6284
void init_module_ems31();
6385
void init_module_ehs5e();
6486
void init_module_tx62();
87+
88+
/// Convert time & date string returned by the modem into time_t
89+
time_t parse_time(char const * time_str);
6590
};
6691

6792
} // namespace mbed

connectivity/drivers/cellular/GEMALTO/COMPONENT_GEMALTO_CINTERION/GEMALTO_CINTERION_CellularContext.cpp

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ NetworkStack *GEMALTO_CINTERION_CellularContext::get_stack()
5252
nsapi_error_t GEMALTO_CINTERION_CellularContext::do_user_authentication()
5353
{
5454
// if user has defined user name and password we need to call CGAUTH before activating or modifying context
55-
if (_pwd && _uname) {
55+
if (_pwd && _uname && (strcmp(_pwd, "") != 0) && (strcmp(_uname, "") != 0)) {
5656
if (!get_device()->get_property(AT_CellularDevice::PROPERTY_AT_CGAUTH)) {
5757
return NSAPI_ERROR_UNSUPPORTED;
5858
}
@@ -63,6 +63,9 @@ nsapi_error_t GEMALTO_CINTERION_CellularContext::do_user_authentication()
6363
return NSAPI_ERROR_AUTH_FAILURE;
6464
}
6565
}
66+
else {
67+
tr_info("Empty pwd and username fields: no need for authentication\n");
68+
}
6669

6770
return NSAPI_ERROR_OK;
6871
}
@@ -96,4 +99,58 @@ void GEMALTO_CINTERION_CellularContext::enable_access_technology()
9699
}
97100
}
98101

102+
bool GEMALTO_CINTERION_CellularContext::get_context()
103+
{
104+
_at.cmd_start_stop("+CGDCONT", "?");
105+
_at.resp_start("+CGDCONT:");
106+
set_cid(-1);
107+
int cid_max = 0; // needed when creating new context
108+
char apn[MAX_ACCESSPOINT_NAME_LENGTH];
109+
int apn_len = 0;
110+
111+
while (_at.info_resp()) {
112+
int cid = _at.read_int();
113+
if (cid > cid_max) {
114+
cid_max = cid;
115+
}
116+
char pdp_type_from_context[10];
117+
int pdp_type_len = _at.read_string(pdp_type_from_context, sizeof(pdp_type_from_context));
118+
if (pdp_type_len > 0) {
119+
apn_len = _at.read_string(apn, sizeof(apn));
120+
if (apn_len > 0 && (strcmp(apn, _apn) == 0)) {
121+
// APN matched -> Check PDP type
122+
pdp_type_t pdp_type = string_to_pdp_type(pdp_type_from_context);
123+
tr_debug("CID %d APN \"%s\" pdp_type %u", cid, apn, pdp_type);
124+
125+
// Accept exact matching PDP context type or dual PDP context for modems that support both IPv4 and IPv6 stacks
126+
if (get_device()->get_property(pdp_type_t_to_cellular_property(pdp_type)) ||
127+
((pdp_type == IPV4V6_PDP_TYPE && (get_device()->get_property(AT_CellularDevice::PROPERTY_IPV4_PDP_TYPE) &&
128+
get_device()->get_property(AT_CellularDevice::PROPERTY_IPV6_PDP_TYPE))) && !_nonip_req)) {
129+
_pdp_type = pdp_type;
130+
set_cid(cid);
131+
}
132+
}
133+
else {
134+
cid_max = 0;
135+
}
136+
}
137+
}
138+
139+
_at.resp_stop();
140+
if (_cid == -1) { // no suitable context was found so create a new one
141+
if (!set_new_context(cid_max + 1)) {
142+
return false;
143+
}
144+
}
145+
146+
// save the apn
147+
if (apn_len > 0 && !_apn) {
148+
memcpy(_found_apn, apn, apn_len + 1);
149+
}
150+
151+
tr_info("Found PDP context %d", _cid);
152+
153+
return true;
154+
}
155+
99156
} /* namespace mbed */

connectivity/drivers/cellular/GEMALTO/COMPONENT_GEMALTO_CINTERION/GEMALTO_CINTERION_CellularContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class GEMALTO_CINTERION_CellularContext: public AT_CellularContext {
3232
#endif // NSAPI_PPP_AVAILABLE
3333
nsapi_error_t do_user_authentication() override;
3434
void enable_access_technology() override;
35+
bool get_context() override;
3536
};
3637

3738
} /* namespace mbed */
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2018, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#ifndef GEMALTO_CINTERION_CELLULARNETWORK_H_
18+
#define GEMALTO_CINTERION_CELLULARNETWORK_H_
19+
20+
#include "AT_CellularNetwork.h"
21+
#include "GEMALTO_CINTERION.h"
22+
23+
namespace mbed {
24+
25+
class GEMALTO_CINTERION_CellularNetwork: public AT_CellularNetwork {
26+
const GEMALTO_CINTERION::Module _module;
27+
public:
28+
GEMALTO_CINTERION_CellularNetwork(ATHandler &at, AT_CellularDevice &device, GEMALTO_CINTERION::Module module):
29+
AT_CellularNetwork(at, device),
30+
_module(module)
31+
{}
32+
33+
virtual nsapi_error_t set_attach()
34+
{
35+
if (_module == GEMALTO_CINTERION::ModuleTX62) {
36+
return NSAPI_ERROR_OK;
37+
}
38+
return AT_CellularNetwork::set_attach();
39+
}
40+
41+
protected:
42+
};
43+
44+
} /* namespace mbed */
45+
46+
#endif // GEMALTO_CINTERION_CELLULARNETWORK_H_

0 commit comments

Comments
 (0)