Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
AT_CellularSIM.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_CellularSIM.h" 00019 #include "CellularLog.h" 00020 00021 using namespace mbed; 00022 00023 const int MAX_SIM_RESPONSE_LENGTH = 16; 00024 00025 AT_CellularSIM::AT_CellularSIM(ATHandler &at) : AT_CellularBase(at) 00026 { 00027 } 00028 00029 AT_CellularSIM::~AT_CellularSIM() 00030 { 00031 } 00032 00033 nsapi_error_t AT_CellularSIM::get_sim_state(SimState &state) 00034 { 00035 char simstr[MAX_SIM_RESPONSE_LENGTH]; 00036 _at.lock(); 00037 _at.flush(); 00038 _at.cmd_start("AT+CPIN?"); 00039 _at.cmd_stop(); 00040 _at.resp_start("+CPIN:"); 00041 ssize_t len = _at.read_string(simstr, sizeof(simstr)); 00042 if (len != -1) { 00043 if (len >= 5 && memcmp(simstr, "READY", 5) == 0) { 00044 state = SimStateReady; 00045 } else if (len >= 6 && memcmp(simstr, "SIM PIN", 6) == 0) { 00046 state = SimStatePinNeeded; 00047 } else if (len >= 6 && memcmp(simstr, "SIM PUK", 6) == 0) { 00048 state = SimStatePukNeeded; 00049 } else { 00050 simstr[len] = '\0'; 00051 tr_error("Unknown SIM state %s", simstr); 00052 state = SimStateUnknown; 00053 } 00054 } else { 00055 tr_warn("SIM not readable."); 00056 state = SimStateUnknown; // SIM may not be ready yet or +CPIN may be unsupported command 00057 } 00058 _at.resp_stop(); 00059 nsapi_error_t error = _at.get_last_error(); 00060 _at.unlock(); 00061 #if MBED_CONF_MBED_TRACE_ENABLE 00062 switch (state) { 00063 case SimStatePinNeeded: 00064 tr_info("SIM PIN required"); 00065 break; 00066 case SimStatePukNeeded: 00067 tr_error("SIM PUK required"); 00068 break; 00069 case SimStateUnknown: 00070 tr_warn("SIM state unknown"); 00071 break; 00072 default: 00073 tr_info("SIM is ready"); 00074 break; 00075 } 00076 #endif 00077 return error; 00078 } 00079 00080 00081 nsapi_error_t AT_CellularSIM::set_pin(const char *sim_pin) 00082 { 00083 // if SIM is already in ready state then settings the PIN 00084 // will return error so let's check the state before settings the pin. 00085 SimState state; 00086 if (get_sim_state(state) == NSAPI_ERROR_OK && state == SimStateReady) { 00087 return NSAPI_ERROR_OK ; 00088 } 00089 00090 _at.lock(); 00091 _at.cmd_start("AT+CPIN="); 00092 _at.write_string(sim_pin); 00093 _at.cmd_stop(); 00094 _at.resp_start(); 00095 _at.resp_stop(); 00096 return _at.unlock_return_error(); 00097 } 00098 00099 nsapi_error_t AT_CellularSIM::change_pin(const char *sim_pin, const char *new_pin) 00100 { 00101 _at.lock(); 00102 _at.cmd_start("AT+CPWD="); 00103 _at.write_string("SC"); 00104 _at.write_string(sim_pin); 00105 _at.write_string(new_pin); 00106 _at.cmd_stop(); 00107 _at.resp_start(); 00108 _at.resp_stop(); 00109 return _at.unlock_return_error(); 00110 } 00111 00112 nsapi_error_t AT_CellularSIM::set_pin_query(const char *sim_pin, bool query_pin) 00113 { 00114 _at.lock(); 00115 if (query_pin) { 00116 /* use the SIM locked */ 00117 _at.cmd_start("AT+CLCK="); 00118 _at.write_string("SC"); 00119 _at.write_int(1); 00120 _at.write_string(sim_pin); 00121 _at.cmd_stop(); 00122 _at.resp_start(); 00123 _at.resp_stop(); 00124 } else { 00125 /* use the SIM unlocked */ 00126 _at.cmd_start("AT+CLCK="); 00127 _at.write_string("SC"); 00128 _at.write_int(0); 00129 _at.write_string(sim_pin); 00130 _at.cmd_stop(); 00131 _at.resp_start(); 00132 _at.resp_stop(); 00133 } 00134 return _at.unlock_return_error(); 00135 } 00136 00137 nsapi_error_t AT_CellularSIM::get_imsi(char *imsi) 00138 { 00139 if (imsi == NULL) { 00140 return NSAPI_ERROR_PARAMETER ; 00141 } 00142 _at.lock(); 00143 _at.cmd_start("AT+CIMI"); 00144 _at.cmd_stop(); 00145 _at.resp_start(); 00146 int len = _at.read_string(imsi, MAX_IMSI_LENGTH); 00147 if (len > 0) { 00148 imsi[len] = '\0'; 00149 } 00150 _at.resp_stop(); 00151 return _at.unlock_return_error(); 00152 } 00153 00154 nsapi_error_t AT_CellularSIM::get_iccid(char *buf, size_t buf_size) 00155 { 00156 _at.lock(); 00157 _at.cmd_start("AT+CCID?"); 00158 _at.cmd_stop(); 00159 _at.resp_start("+CCID:"); 00160 _at.read_string(buf, buf_size); 00161 _at.resp_stop(); 00162 return _at.unlock_return_error(); 00163 }
Generated on Tue Jul 12 2022 12:43:34 by
