Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AT_ControlPlane_netif.cpp Source File

AT_ControlPlane_netif.cpp

00001 /*AT_ControlPlane_netif.cpp*/
00002 #include "AT_ControlPlane_netif.h"
00003 
00004 namespace mbed {
00005 
00006 AT_ControlPlane_netif::AT_ControlPlane_netif(ATHandler &at, int cid) :
00007     _cid(cid), _cb(NULL), _data(NULL), _recv_len(0), _at(at)
00008 {
00009     _at.set_urc_handler("+CRTDCP:", mbed::Callback<void()>(this, &AT_ControlPlane_netif::urc_cp_recv));
00010 }
00011 
00012 AT_ControlPlane_netif::~AT_ControlPlane_netif()
00013 {}
00014 
00015 void AT_ControlPlane_netif::urc_cp_recv()
00016 {
00017     //+CRTDCP: <cid>,<cpdata_length>,<cpdata>
00018     _at.lock();
00019     int cid = _at.read_int();
00020     int cpdata_length = _at.read_int();
00021     int read_len = _at.read_string(_recv_buffer, sizeof(_recv_buffer));
00022 
00023     _at.unlock();
00024 
00025     // cid not expected to be different because: one context - one file handle
00026     // so this file handle cannot get urc from different context
00027     if (read_len > 0 && read_len == cpdata_length && cid == _cid) {
00028         _recv_len = read_len;
00029         data_received();
00030     }
00031 }
00032 
00033 nsapi_size_or_error_t AT_ControlPlane_netif::send(const void *cpdata, nsapi_size_t cpdata_length)
00034 {
00035     //CSODCP
00036     _at.lock();
00037 
00038     nsapi_size_or_error_t err = _at.at_cmd_discard("+CSODCP", "=", "%d%d%b", _cid, cpdata_length, cpdata, cpdata_length);
00039 
00040     return (err == NSAPI_ERROR_OK ) ? cpdata_length : err;
00041 }
00042 
00043 nsapi_size_or_error_t AT_ControlPlane_netif::recv(void *cpdata, nsapi_size_t cpdata_length)
00044 {
00045     // If no data received through CRTDCP URC
00046     if (!_recv_len) {
00047         return NSAPI_ERROR_WOULD_BLOCK ;
00048     }
00049 
00050     // If too small buffer for data
00051     if (_recv_len > cpdata_length) {
00052         return NSAPI_ERROR_DEVICE_ERROR ;
00053     }
00054 
00055     memcpy(cpdata, _recv_buffer, _recv_len);
00056     size_t recv = _recv_len;
00057     _recv_len = 0;
00058     return recv;
00059 }
00060 
00061 void AT_ControlPlane_netif::attach(void (*callback)(void *), void *data)
00062 {
00063     _cb = callback;
00064     _data = data;
00065 }
00066 
00067 void AT_ControlPlane_netif::data_received()
00068 {
00069     // call socket event
00070     if (!_cb) {
00071         return;
00072     }
00073     _cb(_data);
00074 }
00075 
00076 } //mbed namespace