Knight KE / Mbed OS Game_Master
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         release_at_handler(&_network->get_at_handler());
00175         delete _network;
00176         _network = NULL;
00177     }
00178 }
00179 
00180 void AT_CellularDevice::close_sms()
00181 {
00182     if (_sms) {
00183         release_at_handler(&_sms->get_at_handler());
00184         delete _sms;
00185         _sms = NULL;
00186     }
00187 }
00188 void AT_CellularDevice::close_power()
00189 {
00190     if (_power) {
00191         release_at_handler(&_power->get_at_handler());
00192         delete _power;
00193         _power = NULL;
00194     }
00195 }
00196 
00197 void AT_CellularDevice::close_sim()
00198 {
00199     if (_sim) {
00200         release_at_handler(&_sim->get_at_handler());
00201         delete _sim;
00202         _sim = NULL;
00203     }
00204 }
00205 
00206 void AT_CellularDevice::close_information()
00207 {
00208     if (_information) {
00209         release_at_handler(&_information->get_at_handler());
00210         delete _information;
00211         _information = NULL;
00212     }
00213 }
00214 
00215 void AT_CellularDevice::set_timeout(int timeout)
00216 {
00217     _default_timeout = timeout;
00218 
00219     ATHandler *atHandler = _atHandlers;
00220     while (atHandler) {
00221         atHandler->set_at_timeout(_default_timeout, true); // set as default timeout
00222         atHandler = atHandler->_nextATHandler;
00223     }
00224 }
00225 
00226 uint16_t AT_CellularDevice::get_send_delay()
00227 {
00228     return 0;
00229 }
00230 
00231 void AT_CellularDevice::modem_debug_on(bool on)
00232 {
00233     _modem_debug_on = on;
00234 
00235     ATHandler *atHandler = _atHandlers;
00236     while (atHandler) {
00237         atHandler->set_debug(_modem_debug_on);
00238         atHandler = atHandler->_nextATHandler;
00239     }
00240 }
00241 
00242 NetworkStack *AT_CellularDevice::get_stack()
00243 {
00244     if (!_network) {
00245         return NULL;
00246     }
00247     return _network->get_stack();
00248 }