From 227013f7cc6c03e9353a99ad3bdd19a49a628d4c Mon Sep 17 00:00:00 2001 From: Mirela Chirica Date: Tue, 11 Jun 2019 14:18:19 +0300 Subject: [PATCH] Cellular: Added handling for BG96 network PDP context deactivation --- .../cellular/framework/AT/AT_CellularDevice.h | 2 +- .../targets/QUECTEL/BG96/QUECTEL_BG96.cpp | 15 ++++++++ .../targets/QUECTEL/BG96/QUECTEL_BG96.h | 1 + .../BG96/QUECTEL_BG96_CellularStack.cpp | 38 +++++++++---------- .../QUECTEL/BG96/QUECTEL_BG96_CellularStack.h | 13 ++++++- 5 files changed, 46 insertions(+), 23 deletions(-) mode change 100644 => 100755 features/cellular/framework/AT/AT_CellularDevice.h diff --git a/features/cellular/framework/AT/AT_CellularDevice.h b/features/cellular/framework/AT/AT_CellularDevice.h old mode 100644 new mode 100755 index 9167726802..49de5a0108 --- a/features/cellular/framework/AT/AT_CellularDevice.h +++ b/features/cellular/framework/AT/AT_CellularDevice.h @@ -143,11 +143,11 @@ public: protected: virtual void cellular_callback(nsapi_event_t ev, intptr_t ptr, CellularContext *ctx = NULL); + void send_disconnect_to_context(int cid); private: void urc_nw_deact(); void urc_pdn_deact(); - void send_disconnect_to_context(int cid); }; } // namespace mbed diff --git a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.cpp b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.cpp index c2148bf0a5..b7c3743b7d 100644 --- a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.cpp +++ b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.cpp @@ -69,6 +69,8 @@ QUECTEL_BG96::QUECTEL_BG96(FileHandle *fh, PinName pwr, bool active_high, PinNam _pwr(pwr, !_active_high), _rst(rst, !_active_high) { + _at->set_urc_handler("+QIURC: \"pdpde", mbed::Callback(this, &QUECTEL_BG96::urc_pdpdeact)); + AT_CellularBase::set_cellular_properties(cellular_properties); } @@ -199,3 +201,16 @@ CellularDevice *CellularDevice::get_default_instance() return &device; } #endif + +void QUECTEL_BG96::urc_pdpdeact() +{ + _at->lock(); + _at->skip_param(); + int cid = _at->read_int(); + const nsapi_error_t err = _at->unlock_return_error(); + + if (err != NSAPI_ERROR_OK) { + return; + } + send_disconnect_to_context(cid); +} diff --git a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.h b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.h index f7000fbfe4..ea7274d10a 100644 --- a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.h +++ b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.h @@ -54,6 +54,7 @@ private: bool _active_high; DigitalOut _pwr; DigitalOut _rst; + void urc_pdpdeact(); }; } // namespace mbed #endif // QUECTEL_BG96_H_ diff --git a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.cpp b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.cpp index 7ad3892e22..60e06dbfce 100755 --- a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.cpp +++ b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.cpp @@ -20,15 +20,10 @@ using namespace mbed; -const char *QIURC_RECV = "recv"; -const uint8_t QIURC_RECV_LENGTH = 4; -const char *QIURC_CLOSED = "closed"; -const uint8_t QIURC_CLOSED_LENGTH = 6; -const uint8_t MAX_QIURC_LENGTH = QIURC_CLOSED_LENGTH; - QUECTEL_BG96_CellularStack::QUECTEL_BG96_CellularStack(ATHandler &atHandler, int cid, nsapi_ip_stack_t stack_type) : AT_CellularStack(atHandler, cid, stack_type) { - _at.set_urc_handler("+QIURC:", mbed::Callback(this, &QUECTEL_BG96_CellularStack::urc_qiurc)); + _at.set_urc_handler("+QIURC: \"recv", mbed::Callback(this, &QUECTEL_BG96_CellularStack::urc_qiurc_recv)); + _at.set_urc_handler("+QIURC: \"close", mbed::Callback(this, &QUECTEL_BG96_CellularStack::urc_qiurc_closed)); } QUECTEL_BG96_CellularStack::~QUECTEL_BG96_CellularStack() @@ -119,11 +114,20 @@ nsapi_error_t QUECTEL_BG96_CellularStack::socket_connect(nsapi_socket_t handle, return NSAPI_ERROR_NO_CONNECTION; } -void QUECTEL_BG96_CellularStack::urc_qiurc() +void QUECTEL_BG96_CellularStack::urc_qiurc_recv() +{ + urc_qiurc(URC_RECV); +} + +void QUECTEL_BG96_CellularStack::urc_qiurc_closed() +{ + urc_qiurc(URC_CLOSED); +} + +void QUECTEL_BG96_CellularStack::urc_qiurc(urc_type_t urc_type) { - char urc_string[MAX_QIURC_LENGTH + 1]; _at.lock(); - (void)_at.read_string(urc_string, sizeof(urc_string)); + _at.skip_param(); const int sock_id = _at.read_int(); const nsapi_error_t err = _at.unlock_return_error(); @@ -131,20 +135,14 @@ void QUECTEL_BG96_CellularStack::urc_qiurc() return; } - bool recv = strcmp(urc_string, "recv") == 0; - bool closed = strcmp(urc_string, "closed") == 0; - CellularSocket *sock = find_socket(sock_id); if (sock) { - if (closed) { - tr_error("Socket closed %d", sock_id); + if (urc_type == URC_CLOSED) { + tr_info("Socket closed %d", sock_id); sock->closed = true; } - - if (recv || closed) { - if (sock->_cb) { - sock->_cb(sock->_data); - } + if (sock->_cb) { + sock->_cb(sock->_data); } } } diff --git a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.h b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.h index e8eb670c1c..ae993ccbee 100644 --- a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.h +++ b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.h @@ -29,6 +29,11 @@ namespace mbed { #define BG96_MAX_SEND_SIZE 1460 #define BG96_SOCKET_BIND_FAIL 556 +typedef enum { + URC_RECV, + URC_CLOSED, +} urc_type_t; + class QUECTEL_BG96_CellularStack : public AT_CellularStack { public: QUECTEL_BG96_CellularStack(ATHandler &atHandler, int cid, nsapi_ip_stack_t stack_type); @@ -60,8 +65,12 @@ protected: // AT_CellularStack void *buffer, nsapi_size_t size); private: - // URC handlers - void urc_qiurc(); + // URC handler + void urc_qiurc(urc_type_t urc_type); + // URC handler for socket data being received + void urc_qiurc_recv(); + // URC handler for socket being closed + void urc_qiurc_closed(); void handle_open_socket_response(int &modem_connect_id, int &err); };