Kev Mann / mbed-dev-OS5_10_4
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AT_CellularDevice.cpp Source File

AT_CellularDevice.cpp

00001 /*
00002  * Copyright (c) 2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "AT_CellularDevice.h"
00019 
00020 using namespace events;
00021 using namespace mbed;
00022 
00023 #define DEFAULT_AT_TIMEOUT 1000 // at default timeout in milliseconds
00024 
00025 AT_CellularDevice::AT_CellularDevice(EventQueue &queue) :
00026     _atHandlers(0), _network(0), _sms(0), _sim(0), _power(0), _information(0), _queue(queue),
00027     _default_timeout(DEFAULT_AT_TIMEOUT), _modem_debug_on(false)
00028 {
00029 }
00030 
00031 AT_CellularDevice::~AT_CellularDevice()
00032 {
00033     close_network();
00034     close_sms();
00035     close_power();
00036     close_sim();
00037     close_information();
00038 
00039     ATHandler *atHandler = _atHandlers;
00040     while (atHandler) {
00041         ATHandler *old = atHandler;
00042         atHandler = atHandler->_nextATHandler;
00043         delete old;
00044     }
00045 }
00046 
00047 // each parser is associated with one filehandle (that is UART)
00048 ATHandler *AT_CellularDevice::get_at_handler(FileHandle *fileHandle)
00049 {
00050     if (!fileHandle) {
00051         return NULL;
00052     }
00053     ATHandler *atHandler = _atHandlers;
00054     while (atHandler) {
00055         if (atHandler->get_file_handle() == fileHandle) {
00056             atHandler->inc_ref_count();
00057             return atHandler;
00058         }
00059         atHandler = atHandler->_nextATHandler;
00060     }
00061 
00062     atHandler = new ATHandler(fileHandle, _queue, _default_timeout, "\r", get_send_delay());
00063     if (atHandler) {
00064         if (_modem_debug_on) {
00065             atHandler->set_debug(_modem_debug_on);
00066         }
00067         atHandler->_nextATHandler = _atHandlers;
00068         _atHandlers = atHandler;
00069     }
00070 
00071     return atHandler;
00072 }
00073 
00074 void AT_CellularDevice::release_at_handler(ATHandler *at_handler)
00075 {
00076     if (!at_handler) {
00077         return;
00078     }
00079     at_handler->dec_ref_count();
00080     if (at_handler->get_ref_count() == 0) {
00081         // we can delete this at_handler
00082         ATHandler *atHandler = _atHandlers;
00083         ATHandler *prev = NULL;
00084         while (atHandler) {
00085             if (atHandler == at_handler) {
00086                 if (prev == NULL) {
00087                     _atHandlers = _atHandlers->_nextATHandler;
00088                 } else {
00089                     prev->_nextATHandler = atHandler->_nextATHandler;
00090                 }
00091                 delete atHandler;
00092                 break;
00093             } else {
00094                 prev = atHandler;
00095                 atHandler = atHandler->_nextATHandler;
00096             }
00097         }
00098     }
00099 }
00100 
00101 CellularNetwork *AT_CellularDevice::open_network(FileHandle *fh)
00102 {
00103     if (!_network) {
00104         ATHandler *atHandler = get_at_handler(fh);
00105         if (atHandler) {
00106             _network = new AT_CellularNetwork(*atHandler);
00107             if (!_network) {
00108                 release_at_handler(atHandler);
00109             }
00110         }
00111     }
00112     return _network;
00113 }
00114 
00115 CellularSMS *AT_CellularDevice::open_sms(FileHandle *fh)
00116 {
00117     if (!_sms) {
00118         ATHandler *atHandler = get_at_handler(fh);
00119         if (atHandler) {
00120             _sms = new AT_CellularSMS(*atHandler);
00121             if (!_sms) {
00122                 release_at_handler(atHandler);
00123             }
00124         }
00125     }
00126     return _sms;
00127 }
00128 
00129 CellularSIM *AT_CellularDevice::open_sim(FileHandle *fh)
00130 {
00131     if (!_sim) {
00132         ATHandler *atHandler = get_at_handler(fh);
00133         if (atHandler) {
00134             _sim = new AT_CellularSIM(*atHandler);
00135             if (!_sim) {
00136                 release_at_handler(atHandler);
00137             }
00138         }
00139     }
00140     return _sim;
00141 }
00142 
00143 CellularPower *AT_CellularDevice::open_power(FileHandle *fh)
00144 {
00145     if (!_power) {
00146         ATHandler *atHandler = get_at_handler(fh);
00147         if (atHandler) {
00148             _power = new AT_CellularPower(*atHandler);
00149             if (!_power) {
00150                 release_at_handler(atHandler);
00151             }
00152         }
00153     }
00154     return _power;
00155 }
00156 
00157 CellularInformation *AT_CellularDevice::open_information(FileHandle *fh)
00158 {
00159     if (!_information) {
00160         ATHandler *atHandler = get_at_handler(fh);
00161         if (atHandler) {
00162             _information = new AT_CellularInformation(*atHandler);
00163             if (!_information) {
00164                 release_at_handler(atHandler);
00165             }
00166         }
00167     }
00168     return _information;
00169 }
00170 
00171 void AT_CellularDevice::close_network()
00172 {
00173     if (_network) {
00174         ATHandler *atHandler = &_network->get_at_handler();
00175         delete _network;
00176         _network = NULL;
00177         release_at_handler(atHandler);
00178     }
00179 }
00180 
00181 void AT_CellularDevice::close_sms()
00182 {
00183     if (_sms) {
00184         release_at_handler(&_sms->get_at_handler());
00185         delete _sms;
00186         _sms = NULL;
00187     }
00188 }
00189 void AT_CellularDevice::close_power()
00190 {
00191     if (_power) {
00192         release_at_handler(&_power->get_at_handler());
00193         delete _power;
00194         _power = NULL;
00195     }
00196 }
00197 
00198 void AT_CellularDevice::close_sim()
00199 {
00200     if (_sim) {
00201         release_at_handler(&_sim->get_at_handler());
00202         delete _sim;
00203         _sim = NULL;
00204     }
00205 }
00206 
00207 void AT_CellularDevice::close_information()
00208 {
00209     if (_information) {
00210         release_at_handler(&_information->get_at_handler());
00211         delete _information;
00212         _information = NULL;
00213     }
00214 }
00215 
00216 void AT_CellularDevice::set_timeout(int timeout)
00217 {
00218     _default_timeout = timeout;
00219 
00220     ATHandler *atHandler = _atHandlers;
00221     while (atHandler) {
00222         atHandler->set_at_timeout(_default_timeout, true); // set as default timeout
00223         atHandler = atHandler->_nextATHandler;
00224     }
00225 }
00226 
00227 uint16_t AT_CellularDevice::get_send_delay()
00228 {
00229     return 0;
00230 }
00231 
00232 void AT_CellularDevice::modem_debug_on(bool on)
00233 {
00234     _modem_debug_on = on;
00235 
00236     ATHandler *atHandler = _atHandlers;
00237     while (atHandler) {
00238         atHandler->set_debug(_modem_debug_on);
00239         atHandler = atHandler->_nextATHandler;
00240     }
00241 }
00242 
00243 NetworkStack *AT_CellularDevice::get_stack()
00244 {
00245     if (!_network) {
00246         return NULL;
00247     }
00248     return _network->get_stack();
00249 }
00250 
00251 nsapi_error_t AT_CellularDevice::init_module(FileHandle *fh)
00252 {
00253     return NSAPI_ERROR_OK ;
00254 }