Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TELIT_ME910_CellularContext.cpp Source File

TELIT_ME910_CellularContext.cpp

00001 /*
00002  * Copyright (c) 2018, 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 #include "TELIT_ME910_CellularContext.h"
00018 #include "CellularLog.h"
00019 
00020 #include "Semaphore.h"
00021 
00022 using namespace mbed_cellular_util;
00023 
00024 namespace mbed {
00025 
00026 TELIT_ME910_CellularContext::TELIT_ME910_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req) :
00027     AT_CellularContext(at, device, apn, cp_req, nonip_req)
00028 {
00029 }
00030 
00031 TELIT_ME910_CellularContext::~TELIT_ME910_CellularContext()
00032 {
00033 }
00034 
00035 bool TELIT_ME910_CellularContext::get_context()
00036 {
00037     bool modem_supports_ipv6 = get_device()->get_property(AT_CellularDevice::PROPERTY_IPV6_PDP_TYPE);
00038     bool modem_supports_ipv4 = get_device()->get_property(AT_CellularDevice::PROPERTY_IPV4_PDP_TYPE);
00039 
00040     _at.cmd_start_stop("+CGDCONT", "?");
00041     _at.resp_start("+CGDCONT:");
00042     _cid = -1;
00043     int cid_max = 0; // needed when creating new context
00044     char apn[MAX_ACCESSPOINT_NAME_LENGTH];
00045     int apn_len = 0;
00046 
00047     while (_at.info_resp()) {
00048         int cid = _at.read_int();
00049         if (cid > cid_max) {
00050             cid_max = cid;
00051         }
00052         char pdp_type_from_context[10];
00053         int pdp_type_len = _at.read_string(pdp_type_from_context, sizeof(pdp_type_from_context) - 1);
00054         if (pdp_type_len > 0) {
00055             apn_len = _at.read_string(apn, sizeof(apn) - 1);
00056             if (apn_len >= 0) {
00057                 if (_apn && apn_len > 0 && (strcmp(apn, _apn) != 0)) {
00058                     continue;
00059                 }
00060 
00061                 // APN matched -> Check PDP type
00062                 pdp_type_t pdp_type = string_to_pdp_type(pdp_type_from_context);
00063 
00064                 // Accept exact matching PDP context type or dual PDP context for IPv4/IPv6 only modems
00065                 if (get_device()->get_property(pdp_type_t_to_cellular_property(pdp_type)) ||
00066                         ((pdp_type == IPV4V6_PDP_TYPE && (modem_supports_ipv4 || modem_supports_ipv6)) && !_nonip_req)) {
00067                     _pdp_type = pdp_type;
00068                     _cid = cid;
00069                     break;
00070                 }
00071             }
00072         }
00073     }
00074 
00075     _at.resp_stop();
00076     if (_cid == -1) { // no suitable context was found so create a new one
00077         if (!set_new_context(1)) {
00078             return false;
00079         }
00080     }
00081 
00082     // save the apn
00083     if (apn_len > 0 && !_apn) {
00084         memcpy(_found_apn, apn, apn_len + 1);
00085     }
00086 
00087     tr_info("Found PDP context %d", _cid);
00088     return true;
00089 }
00090 
00091 } /* namespace mbed */