takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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 #include "AT_CellularInformation.h"
00020 #include "AT_CellularNetwork.h"
00021 #include "AT_CellularPower.h"
00022 #include "AT_CellularSIM.h"
00023 #include "AT_CellularSMS.h"
00024 #include "ATHandler.h"
00025 
00026 using namespace events;
00027 using namespace mbed;
00028 
00029 #define DEFAULT_AT_TIMEOUT 1000 // at default timeout in milliseconds
00030 
00031 AT_CellularDevice::AT_CellularDevice(EventQueue &queue) :
00032     _atHandlers(0), _network(0), _sms(0), _sim(0), _power(0), _information(0), _queue(queue),
00033     _default_timeout(DEFAULT_AT_TIMEOUT), _modem_debug_on(false)
00034 {
00035 }
00036 
00037 AT_CellularDevice::~AT_CellularDevice()
00038 {
00039     close_network();
00040     close_sms();
00041     close_power();
00042     close_sim();
00043     close_information();
00044 
00045     ATHandler *atHandler = _atHandlers;
00046     while (atHandler) {
00047         ATHandler *old = atHandler;
00048         atHandler = atHandler->_nextATHandler;
00049         delete old;
00050     }
00051 }
00052 
00053 events::EventQueue *AT_CellularDevice::get_queue() const
00054 {
00055     return &_queue;
00056 }
00057 
00058 // each parser is associated with one filehandle (that is UART)
00059 ATHandler *AT_CellularDevice::get_at_handler(FileHandle *fileHandle)
00060 {
00061     if (!fileHandle) {
00062         return NULL;
00063     }
00064     ATHandler *atHandler = _atHandlers;
00065     while (atHandler) {
00066         if (atHandler->get_file_handle() == fileHandle) {
00067             atHandler->inc_ref_count();
00068             return atHandler;
00069         }
00070         atHandler = atHandler->_nextATHandler;
00071     }
00072 
00073     atHandler = new ATHandler(fileHandle, _queue, _default_timeout, "\r", get_send_delay());
00074     if (_modem_debug_on) {
00075         atHandler->set_debug(_modem_debug_on);
00076     }
00077     atHandler->_nextATHandler = _atHandlers;
00078     _atHandlers = atHandler;
00079 
00080     return atHandler;
00081 }
00082 
00083 void AT_CellularDevice::release_at_handler(ATHandler *at_handler)
00084 {
00085     if (!at_handler) {
00086         return;
00087     }
00088     at_handler->dec_ref_count();
00089     if (at_handler->get_ref_count() == 0) {
00090         // we can delete this at_handler
00091         ATHandler *atHandler = _atHandlers;
00092         ATHandler *prev = NULL;
00093         while (atHandler) {
00094             if (atHandler == at_handler) {
00095                 if (prev == NULL) {
00096                     _atHandlers = _atHandlers->_nextATHandler;
00097                 } else {
00098                     prev->_nextATHandler = atHandler->_nextATHandler;
00099                 }
00100                 delete atHandler;
00101                 break;
00102             } else {
00103                 prev = atHandler;
00104                 atHandler = atHandler->_nextATHandler;
00105             }
00106         }
00107     }
00108 }
00109 
00110 CellularNetwork *AT_CellularDevice::open_network(FileHandle *fh)
00111 {
00112     if (!_network) {
00113         ATHandler *atHandler = get_at_handler(fh);
00114         if (atHandler) {
00115             _network = open_network_impl(*atHandler);
00116         }
00117     }
00118     if (_network) {
00119         _network_ref_count++;
00120     }
00121     return _network;
00122 }
00123 
00124 CellularSMS *AT_CellularDevice::open_sms(FileHandle *fh)
00125 {
00126     if (!_sms) {
00127         ATHandler *atHandler = get_at_handler(fh);
00128         if (atHandler) {
00129             _sms = open_sms_impl(*atHandler);
00130         }
00131     }
00132     if (_sms) {
00133         _sms_ref_count++;
00134     }
00135     return _sms;
00136 }
00137 
00138 CellularSIM *AT_CellularDevice::open_sim(FileHandle *fh)
00139 {
00140     if (!_sim) {
00141         ATHandler *atHandler = get_at_handler(fh);
00142         if (atHandler) {
00143             _sim = open_sim_impl(*atHandler);
00144         }
00145     }
00146     if (_sim) {
00147         _sim_ref_count++;
00148     }
00149     return _sim;
00150 }
00151 
00152 CellularPower *AT_CellularDevice::open_power(FileHandle *fh)
00153 {
00154     if (!_power) {
00155         ATHandler *atHandler = get_at_handler(fh);
00156         if (atHandler) {
00157             _power = open_power_impl(*atHandler);
00158         }
00159     }
00160     if (_power) {
00161         _power_ref_count++;
00162     }
00163     return _power;
00164 }
00165 
00166 CellularInformation *AT_CellularDevice::open_information(FileHandle *fh)
00167 {
00168     if (!_information) {
00169         ATHandler *atHandler = get_at_handler(fh);
00170         if (atHandler) {
00171             _information = open_information_impl(*atHandler);
00172         }
00173     }
00174     if (_information) {
00175         _info_ref_count++;
00176     }
00177     return _information;
00178 }
00179 
00180 AT_CellularNetwork *AT_CellularDevice::open_network_impl(ATHandler &at)
00181 {
00182     return new AT_CellularNetwork(at);
00183 }
00184 
00185 AT_CellularSMS *AT_CellularDevice::open_sms_impl(ATHandler &at)
00186 {
00187     return new AT_CellularSMS(at);
00188 }
00189 
00190 AT_CellularPower *AT_CellularDevice::open_power_impl(ATHandler &at)
00191 {
00192     return new AT_CellularPower(at);
00193 }
00194 
00195 AT_CellularSIM *AT_CellularDevice::open_sim_impl(ATHandler &at)
00196 {
00197     return new AT_CellularSIM(at);
00198 }
00199 
00200 AT_CellularInformation *AT_CellularDevice::open_information_impl(ATHandler &at)
00201 {
00202     return new AT_CellularInformation(at);
00203 }
00204 
00205 void AT_CellularDevice::close_network()
00206 {
00207     if (_network) {
00208         _network_ref_count--;
00209         if (_network_ref_count == 0) {
00210             ATHandler *atHandler = &_network->get_at_handler();
00211             delete _network;
00212             _network = NULL;
00213             release_at_handler(atHandler);
00214         }
00215     }
00216 }
00217 
00218 void AT_CellularDevice::close_sms()
00219 {
00220     if (_sms) {
00221         _sms_ref_count--;
00222         if (_sms_ref_count == 0) {
00223             ATHandler *atHandler = &_sms->get_at_handler();
00224             delete _sms;
00225             _sms = NULL;
00226             release_at_handler(atHandler);
00227         }
00228     }
00229 }
00230 
00231 void AT_CellularDevice::close_power()
00232 {
00233     if (_power) {
00234         _power_ref_count--;
00235         if (_power_ref_count == 0) {
00236             ATHandler *atHandler = &_power->get_at_handler();
00237             delete _power;
00238             _power = NULL;
00239             release_at_handler(atHandler);
00240         }
00241     }
00242 }
00243 
00244 void AT_CellularDevice::close_sim()
00245 {
00246     if (_sim) {
00247         _sim_ref_count--;
00248         if (_sim_ref_count == 0) {
00249             ATHandler *atHandler = &_sim->get_at_handler();
00250             delete _sim;
00251             _sim = NULL;
00252             release_at_handler(atHandler);
00253         }
00254     }
00255 }
00256 
00257 void AT_CellularDevice::close_information()
00258 {
00259     if (_information) {
00260         _info_ref_count--;
00261         if (_info_ref_count == 0) {
00262             ATHandler *atHandler = &_information->get_at_handler();
00263             delete _information;
00264             _information = NULL;
00265             release_at_handler(atHandler);
00266         }
00267     }
00268 }
00269 
00270 void AT_CellularDevice::set_timeout(int timeout)
00271 {
00272     _default_timeout = timeout;
00273 
00274     ATHandler *atHandler = _atHandlers;
00275     while (atHandler) {
00276         atHandler->set_at_timeout(_default_timeout, true); // set as default timeout
00277         atHandler = atHandler->_nextATHandler;
00278     }
00279 }
00280 
00281 uint16_t AT_CellularDevice::get_send_delay()
00282 {
00283     return 0;
00284 }
00285 
00286 void AT_CellularDevice::modem_debug_on(bool on)
00287 {
00288     _modem_debug_on = on;
00289 
00290     ATHandler *atHandler = _atHandlers;
00291     while (atHandler) {
00292         atHandler->set_debug(_modem_debug_on);
00293         atHandler = atHandler->_nextATHandler;
00294     }
00295 }
00296 
00297 NetworkStack *AT_CellularDevice::get_stack()
00298 {
00299     if (!_network) {
00300         return NULL;
00301     }
00302     return _network->get_stack();
00303 }
00304 
00305 nsapi_error_t AT_CellularDevice::init_module(FileHandle *fh)
00306 {
00307     return NSAPI_ERROR_OK ;
00308 }