Skip to content
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

Gemalto Cinterion disable URCs when reading #33

Closed
wants to merge 7 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class AT_CellularContext : public CellularContext {
* @return NIDD context text, e.g. Non-IP or NONIP
*/
virtual const char *get_nonip_context_type_str();
virtual void enable_access_technology();
virtual nsapi_error_t enable_access_technology();
virtual void set_cid(int cid);

private:
Expand All @@ -135,15 +135,15 @@ class AT_CellularContext : public CellularContext {

PinName _dcd_pin;
bool _active_high;
RadioAccessTechnologyType _rat;
FrequencyBand _band;

protected:
char _found_apn[MAX_APN_LENGTH];
// flag indicating if CP was requested to be setup
bool _cp_req;
bool _is_connected;
ATHandler &_at;
RadioAccessTechnologyType _rat;
FrequencyBand _band;
};

} // namespace mbed
Expand Down
32 changes: 2 additions & 30 deletions connectivity/cellular/source/framework/AT/AT_CellularContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,37 +443,9 @@ bool AT_CellularContext::set_new_context(int cid)
return success;
}

void AT_CellularContext::enable_access_technology()
nsapi_error_t AT_CellularContext::enable_access_technology()
{
char *buffer = new char [8];
memset(buffer, 0, 8);
sprintf(buffer,"%08X", _band);
switch (_rat)
{
case CATM1:
_at.at_cmd_discard("^SXRAT", "=","%d", _rat);
_at.cmd_start_stop("^SCFG", "=","%s%s", "Radio/Band/CatM",buffer);
_at.resp_start("^SCFG");
_at.cmd_start_stop("^SCFG", "=","%s%d%d", "Radio/Band/CatNB",0,0);
_at.resp_start("^SCFG");
break;

case CATNB:
_at.at_cmd_discard("^SXRAT", "=","%d", _rat);
_at.cmd_start_stop("^SCFG", "=","%s%s", "Radio/Band/CatNB",buffer);
_at.resp_start("^SCFG");
_at.cmd_start_stop("^SCFG", "=","%s%d%d", "Radio/Band/CatM",0,0);
_at.resp_start("^SCFG");
break;

default:
break;
}

_at.cmd_start_stop("^SCFG", "=", "%s%s", "Tcp/withURCs", "on");
_at.resp_start("^SCFG");
free(buffer);

return enable_access_technology();
}

nsapi_error_t AT_CellularContext::do_activate_context()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,41 @@ GEMALTO_CINTERION_CellularContext::~GEMALTO_CINTERION_CellularContext()
nsapi_error_t GEMALTO_CINTERION_CellularContext::connect(const char *sim_pin, const char *apn, const char *uname,
const char *pwd)
{
nsapi_error_t error = NSAPI_ERROR_OK;

set_sim_pin(sim_pin);
set_credentials(apn, uname, pwd);

set_device_ready();
error = set_device_ready();
if ((error != NSAPI_ERROR_OK) && (error != NSAPI_ERROR_ALREADY)) {
tr_error("Failure connecting to GEMALTO CINTERION modem");
return error;
}

_at.lock();
bool valid_context = get_context();
_at.unlock();

if(!valid_context) {
set_new_context(_cid);
if (!valid_context) {
valid_context = set_new_context(_cid);
}

do_user_authentication();
if (!valid_context) {
tr_error("Invalid AT cellular context %d", _cid);
return NSAPI_ERROR_DEVICE_ERROR;
}

enable_access_technology();
error = do_user_authentication();
if (error != NSAPI_ERROR_OK) {
tr_error("Failure during user authentication");
return error;
}

error = enable_access_technology();
if (error != NSAPI_ERROR_OK) {
tr_error("Failure enabling access technology");
return error;
}

return AT_CellularContext::connect();
}
Expand Down Expand Up @@ -148,4 +167,35 @@ NetworkStack *GEMALTO_CINTERION_CellularContext::get_stack()
}
#endif // NSAPI_PPP_AVAILABLE

nsapi_error_t GEMALTO_CINTERION_CellularContext::enable_access_technology()
{
nsapi_error_t error = NSAPI_ERROR_OK;
char buffer[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};

snprintf(buffer, 9, "%08X", _band);
switch (_rat)
{
case CATM1:
error = _at.at_cmd_discard("^SCFG", "=","%s%s", "Radio/Band/CatM", buffer);
break;

case CATNB:
error = _at.at_cmd_discard("^SCFG", "=","%s%s", "Radio/Band/CatNB", buffer);
break;

default:
break;
}
if (error != NSAPI_ERROR_OK) {
return error;
}

error = _at.at_cmd_discard("^SXRAT", "=","%d", _rat);
if (error != NSAPI_ERROR_OK) {
return error;
}

return _at.at_cmd_discard("^SCFG", "=", "%s%s", "Tcp/withURCs", "on");
}

} /* namespace mbed */
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class GEMALTO_CINTERION_CellularContext: public AT_CellularContext {
virtual NetworkStack *get_stack();
#endif // NSAPI_PPP_AVAILABLE
virtual nsapi_error_t do_user_authentication();
virtual nsapi_error_t enable_access_technology();
};

} /* namespace mbed */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,20 +524,25 @@ nsapi_size_or_error_t GEMALTO_CINTERION_CellularStack::socket_sendto_impl(Cellul
return (_at.get_last_error() == NSAPI_ERROR_OK) ? accept_len : NSAPI_ERROR_DEVICE_ERROR;
}

#define DISABLE_URCs _at.at_cmd_discard("^SCFG", "=", "%s%s","Tcp/WithURCs","off")
#define RESTORE_URCs_AND_RETURN(ret) do { _at.at_cmd_discard("^SCFG", "=", "%s%s","Tcp/WithURCs","on"); return ret; } while(0)

nsapi_size_or_error_t GEMALTO_CINTERION_CellularStack::socket_recvfrom_impl(CellularSocket *socket, SocketAddress *address,
void *buffer, nsapi_size_t size)
{
// AT_CellularStack::recvfrom(...) will make sure that we do have a socket
// open on the modem, assert here to catch a programming error
MBED_ASSERT(socket->id != -1);

DISABLE_URCs;

// we must use this flag, otherwise ^SISR URC can come while we are reading response and there is
// no way to detect if that is really an URC or response
if (!socket->pending_bytes) {
_at.process_oob(); // check for ^SISR URC
if (!socket->pending_bytes) {
tr_debug("Socket %d recv would block", socket->id);
return NSAPI_ERROR_WOULD_BLOCK;
RESTORE_URCs_AND_RETURN(NSAPI_ERROR_WOULD_BLOCK);
}
}

Expand All @@ -552,7 +557,7 @@ nsapi_size_or_error_t GEMALTO_CINTERION_CellularStack::socket_recvfrom_impl(Cell
_at.resp_start("^SISR:");
if (!_at.info_resp()) {
tr_error("Socket %d not responding", socket->id);
return NSAPI_ERROR_DEVICE_ERROR;
RESTORE_URCs_AND_RETURN(NSAPI_ERROR_DEVICE_ERROR);
}

int socket_id = _at.read_int();
Expand All @@ -564,24 +569,24 @@ nsapi_size_or_error_t GEMALTO_CINTERION_CellularStack::socket_recvfrom_impl(Cell
goto sisr_retry;
}
tr_error("Socket recvfrom id %d != %d", socket_id, socket->id);
return NSAPI_ERROR_DEVICE_ERROR;
RESTORE_URCs_AND_RETURN(NSAPI_ERROR_DEVICE_ERROR);
}

nsapi_size_or_error_t len = _at.read_int();
if (len == 0) {
tr_warn("Socket %d no data", socket->id);
_at.resp_stop();
return NSAPI_ERROR_WOULD_BLOCK;
RESTORE_URCs_AND_RETURN(NSAPI_ERROR_WOULD_BLOCK);
}
if (len == -1) {
if (GEMALTO_CINTERION::get_module() == GEMALTO_CINTERION::ModuleTX62 && _at.get_last_read_error() == -2) {
_at.process_oob();
tr_error("Socket %d recvfrom finished!", socket->id);
socket->pending_bytes = 0;
return NSAPI_ERROR_OK;
RESTORE_URCs_AND_RETURN(NSAPI_ERROR_OK);
}
tr_error("Socket %d recvfrom failed!", socket->id);
return NSAPI_ERROR_DEVICE_ERROR;
RESTORE_URCs_AND_RETURN(NSAPI_ERROR_DEVICE_ERROR);
}
if (len >= (nsapi_size_or_error_t)size) {
len = (nsapi_size_or_error_t)size;
Expand All @@ -606,7 +611,7 @@ nsapi_size_or_error_t GEMALTO_CINTERION_CellularStack::socket_recvfrom_impl(Cell
int len = _at.read_bytes(at_buf + ip_len, 1);
if (len <= 0) {
tr_error("Socket %d recvfrom addr (len %d)", socket->id, ip_len);
return NSAPI_ERROR_DEVICE_ERROR;
RESTORE_URCs_AND_RETURN(NSAPI_ERROR_DEVICE_ERROR);
}
ip_len += len;
} while (ip_len < ip_address_len && at_buf[ip_len - 2] != '\r' && at_buf[ip_len - 1] != '\n');
Expand All @@ -629,7 +634,7 @@ nsapi_size_or_error_t GEMALTO_CINTERION_CellularStack::socket_recvfrom_impl(Cell
int ip_len = _at.read_string(ip_address, sizeof(ip_address));
if (ip_len <= 0) {
tr_error("Socket %d recvfrom addr (len %d)", socket->id, ip_len);
return NSAPI_ERROR_DEVICE_ERROR;
RESTORE_URCs_AND_RETURN(NSAPI_ERROR_DEVICE_ERROR);
}
}

Expand Down Expand Up @@ -671,7 +676,7 @@ nsapi_size_or_error_t GEMALTO_CINTERION_CellularStack::socket_recvfrom_impl(Cell

_at.resp_stop();

return (_at.get_last_error() == NSAPI_ERROR_OK) ? (recv_len ? recv_len : NSAPI_ERROR_WOULD_BLOCK) : NSAPI_ERROR_DEVICE_ERROR;
RESTORE_URCs_AND_RETURN((_at.get_last_error() == NSAPI_ERROR_OK) ? (recv_len ? recv_len : NSAPI_ERROR_WOULD_BLOCK) : NSAPI_ERROR_DEVICE_ERROR);
}

// setup internet connection profile for sockets
Expand Down
5 changes: 3 additions & 2 deletions connectivity/netsocket/source/TLSSocketWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,11 @@ nsapi_error_t TLSSocketWrapper::append_root_ca_cert(const void *root_ca, size_t

crt = get_ca_chain();
if (!crt) {
return NSAPI_ERROR_NO_MEMORY;
/* In no chain is configured create a new one */
return set_root_ca_cert(root_ca, len);
}

/* Parse CA certification */
/* Append root_ca to the crt chain */
int ret;
if ((ret = mbedtls_x509_crt_parse(crt, static_cast<const unsigned char *>(root_ca),
len)) != 0) {
Expand Down
Loading