Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

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         old = NULL;
00045     }
00046 }
00047 
00048 // each parser is associated with one filehandle (that is UART)
00049 ATHandler* AT_CellularDevice::get_at_handler(FileHandle *fileHandle)
00050 {
00051     if (!fileHandle) {
00052         return NULL;
00053     }
00054     ATHandler *atHandler = _atHandlers;
00055     while (atHandler) {
00056         if (atHandler->get_file_handle() == fileHandle) {
00057             atHandler->inc_ref_count();
00058             return atHandler;
00059         }
00060         atHandler = atHandler->_nextATHandler;
00061     }
00062 
00063     atHandler = new ATHandler(fileHandle, _queue, _default_timeout, "\r");
00064     if (atHandler) {
00065         if (_modem_debug_on) {
00066             atHandler->enable_debug(_modem_debug_on);
00067         }
00068         atHandler->_nextATHandler = _atHandlers;
00069         _atHandlers = atHandler;
00070     }
00071 
00072     return atHandler;
00073 }
00074 
00075 void AT_CellularDevice::release_at_handler(ATHandler* at_handler)
00076 {
00077     if (!at_handler) {
00078         return;
00079     }
00080     at_handler->dec_ref_count();
00081     if (at_handler->get_ref_count() == 0) {
00082         // we can delete this at_handler
00083         ATHandler *atHandler = _atHandlers;
00084         ATHandler *prev = NULL;
00085         while (atHandler) {
00086             if (atHandler == at_handler) {
00087                 if (prev == NULL) {
00088                     _atHandlers = _atHandlers->_nextATHandler;
00089                 } else {
00090                     prev->_nextATHandler = atHandler->_nextATHandler;
00091                 }
00092                 delete atHandler;
00093                 atHandler = NULL;
00094                 break;
00095             } else {
00096                 prev = atHandler;
00097                 atHandler =atHandler->_nextATHandler;
00098             }
00099         }
00100     }
00101 }
00102 
00103 CellularNetwork *AT_CellularDevice::open_network(FileHandle *fh)
00104 {
00105     if (!_network) {
00106         ATHandler *atHandler = get_at_handler(fh);
00107         if (atHandler) {
00108             _network = new AT_CellularNetwork(*atHandler);
00109             if (!_network) {
00110                 release_at_handler(atHandler);
00111             }
00112         }
00113     }
00114     return _network;
00115 }
00116 
00117 CellularSMS *AT_CellularDevice::open_sms(FileHandle *fh)
00118 {
00119     if (!_sms) {
00120         ATHandler *atHandler = get_at_handler(fh);
00121         if (atHandler) {
00122             _sms = new AT_CellularSMS(*atHandler);
00123             if (!_sms) {
00124                 release_at_handler(atHandler);
00125             }
00126         }
00127     }
00128     return _sms;
00129 }
00130 
00131 CellularSIM *AT_CellularDevice::open_sim(FileHandle *fh)
00132 {
00133     if (!_sim) {
00134         ATHandler *atHandler = get_at_handler(fh);
00135         if (atHandler) {
00136             _sim = new AT_CellularSIM(*atHandler);
00137             if (!_sim) {
00138                 release_at_handler(atHandler);
00139             }
00140         }
00141     }
00142     return _sim;
00143 }
00144 
00145 CellularPower *AT_CellularDevice::open_power(FileHandle *fh)
00146 {
00147     if (!_power) {
00148         ATHandler *atHandler = get_at_handler(fh);
00149         if (atHandler) {
00150             _power = new AT_CellularPower(*atHandler);
00151             if (!_power) {
00152                 release_at_handler(atHandler);
00153             }
00154         }
00155     }
00156     return _power;
00157 }
00158 
00159 CellularInformation *AT_CellularDevice::open_information(FileHandle *fh)
00160 {
00161     if (!_information) {
00162         ATHandler *atHandler = get_at_handler(fh);
00163         if (atHandler) {
00164             _information = new AT_CellularInformation(*atHandler);
00165             if (!_information) {
00166                 release_at_handler(atHandler);
00167             }
00168         }
00169     }
00170     return _information;
00171 }
00172 
00173 void AT_CellularDevice::close_network()
00174 {
00175     if (_network) {
00176         release_at_handler(&_network->get_at_handler());
00177         delete _network;
00178         _network = NULL;
00179     }
00180 }
00181 
00182 void AT_CellularDevice::close_sms()
00183 {
00184     if (_sms) {
00185         release_at_handler(&_sms->get_at_handler());
00186         delete _sms;
00187         _sms = NULL;
00188     }
00189 }
00190 void AT_CellularDevice::close_power()
00191 {
00192     if (_power) {
00193         release_at_handler(&_power->get_at_handler());
00194         delete _power;
00195         _power = NULL;
00196     }
00197 }
00198 
00199 void AT_CellularDevice::close_sim()
00200 {
00201     if (_sim) {
00202         release_at_handler(&_sim->get_at_handler());
00203         delete _sim;
00204         _sim = NULL;
00205     }
00206 }
00207 
00208 void AT_CellularDevice::close_information()
00209 {
00210     if (_information) {
00211         release_at_handler(&_information->get_at_handler());
00212         delete _information;
00213         _information = NULL;
00214     }
00215 }
00216 
00217 void AT_CellularDevice::set_timeout(int timeout)
00218 {
00219     _default_timeout = timeout;
00220 
00221     ATHandler *atHandler = _atHandlers;
00222     while (atHandler) {
00223         atHandler->set_at_timeout(_default_timeout, true); // set as default timeout
00224         atHandler = atHandler->_nextATHandler;
00225     }
00226 }
00227 
00228 void AT_CellularDevice::modem_debug_on(bool on)
00229 {
00230     _modem_debug_on = on;
00231 
00232     ATHandler *atHandler = _atHandlers;
00233     while (atHandler) {
00234         atHandler->enable_debug(_modem_debug_on);
00235         atHandler = atHandler->_nextATHandler;
00236     }
00237 }
00238 
00239 NetworkStack *AT_CellularDevice::get_stack()
00240 {
00241     if (!_network) {
00242         return NULL;
00243     }
00244     return _network->get_stack();
00245 }