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 return _at.unlock_return_error(); 00060 } 00061 00062 nsapi_error_t AT_CellularSIM::set_pin(const char *sim_pin) 00063 { 00064 // if SIM is already in ready state then settings the PIN 00065 // will return error so let's check the state before settings the pin. 00066 SimState state; 00067 if (get_sim_state(state) == NSAPI_ERROR_OK && state == SimStateReady) { 00068 return NSAPI_ERROR_OK ; 00069 } 00070 00071 _at.lock(); 00072 _at.cmd_start("AT+CPIN="); 00073 _at.write_string(sim_pin); 00074 _at.cmd_stop(); 00075 _at.resp_start(); 00076 _at.resp_stop(); 00077 return _at.unlock_return_error(); 00078 } 00079 00080 nsapi_error_t AT_CellularSIM::change_pin(const char *sim_pin, const char *new_pin) 00081 { 00082 _at.lock(); 00083 _at.cmd_start("AT+CPWD="); 00084 _at.write_string("SC"); 00085 _at.write_string(sim_pin); 00086 _at.write_string(new_pin); 00087 _at.cmd_stop(); 00088 _at.resp_start(); 00089 _at.resp_stop(); 00090 return _at.unlock_return_error(); 00091 } 00092 00093 nsapi_error_t AT_CellularSIM::set_pin_query(const char *sim_pin, bool query_pin) 00094 { 00095 _at.lock(); 00096 if (query_pin) { 00097 /* use the SIM locked */ 00098 _at.cmd_start("AT+CLCK="); 00099 _at.write_string("SC"); 00100 _at.write_int(1); 00101 _at.write_string(sim_pin); 00102 _at.cmd_stop(); 00103 _at.resp_start(); 00104 _at.resp_stop(); 00105 } else { 00106 /* use the SIM unlocked */ 00107 _at.cmd_start("AT+CLCK="); 00108 _at.write_string("SC"); 00109 _at.write_int(0); 00110 _at.write_string(sim_pin); 00111 _at.cmd_stop(); 00112 _at.resp_start(); 00113 _at.resp_stop(); 00114 } 00115 return _at.unlock_return_error(); 00116 } 00117 00118 nsapi_error_t AT_CellularSIM::get_imsi(char* imsi) 00119 { 00120 _at.lock(); 00121 _at.cmd_start("AT+CIMI"); 00122 _at.cmd_stop(); 00123 _at.resp_start(); 00124 int len = _at.read_string(imsi, MAX_IMSI_LENGTH); 00125 if (len > 0) { 00126 imsi[len] = '\0'; 00127 } 00128 _at.resp_stop(); 00129 return _at.unlock_return_error(); 00130 }
Generated on Tue Jul 12 2022 14:23:21 by
