Fork of ublox-cellular-base to add LARA-R2 support

Fork of ublox-cellular-base by u-blox

Committer:
fahim alavi
Date:
Tue Jan 02 12:38:56 2018 +0500
Revision:
11:57f64bc518c0
Parent:
10:c4281fa79b8f
Child:
12:aa3cbc3f88c5
IMEI provided to application

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RobMeades 0:5cffef3371f6 1 /* Copyright (c) 2017 ublox Limited
RobMeades 0:5cffef3371f6 2 *
RobMeades 0:5cffef3371f6 3 * Licensed under the Apache License, Version 2.0 (the "License");
RobMeades 0:5cffef3371f6 4 * you may not use this file except in compliance with the License.
RobMeades 0:5cffef3371f6 5 * You may obtain a copy of the License at
RobMeades 0:5cffef3371f6 6 *
RobMeades 0:5cffef3371f6 7 * http://www.apache.org/licenses/LICENSE-2.0
RobMeades 0:5cffef3371f6 8 *
RobMeades 0:5cffef3371f6 9 * Unless required by applicable law or agreed to in writing, software
RobMeades 0:5cffef3371f6 10 * distributed under the License is distributed on an "AS IS" BASIS,
RobMeades 0:5cffef3371f6 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
RobMeades 0:5cffef3371f6 12 * See the License for the specific language governing permissions and
RobMeades 0:5cffef3371f6 13 * limitations under the License.
RobMeades 0:5cffef3371f6 14 */
RobMeades 0:5cffef3371f6 15
RobMeades 0:5cffef3371f6 16 #include "UARTSerial.h"
RobMeades 0:5cffef3371f6 17 #include "APN_db.h"
RobMeades 0:5cffef3371f6 18 #include "UbloxCellularBase.h"
RobMeades 0:5cffef3371f6 19 #include "onboard_modem_api.h"
RobMeades 0:5cffef3371f6 20 #ifdef FEATURE_COMMON_PAL
RobMeades 0:5cffef3371f6 21 #include "mbed_trace.h"
RobMeades 0:5cffef3371f6 22 #define TRACE_GROUP "UCB"
RobMeades 0:5cffef3371f6 23 #else
rob.meades@u-blox.com 2:73fcc33c9400 24 #define tr_debug(format, ...) debug_if(_debug_trace_on, format "\n", ## __VA_ARGS__)
rob.meades@u-blox.com 2:73fcc33c9400 25 #define tr_info(format, ...) debug_if(_debug_trace_on, format "\n", ## __VA_ARGS__)
rob.meades@u-blox.com 2:73fcc33c9400 26 #define tr_warn(format, ...) debug_if(_debug_trace_on, format "\n", ## __VA_ARGS__)
rob.meades@u-blox.com 2:73fcc33c9400 27 #define tr_error(format, ...) debug_if(_debug_trace_on, format "\n", ## __VA_ARGS__)
RobMeades 0:5cffef3371f6 28 #endif
RobMeades 0:5cffef3371f6 29
RobMeades 0:5cffef3371f6 30 /**********************************************************************
RobMeades 0:5cffef3371f6 31 * PRIVATE METHODS
RobMeades 0:5cffef3371f6 32 **********************************************************************/
RobMeades 0:5cffef3371f6 33
RobMeades 0:5cffef3371f6 34 void UbloxCellularBase::set_nwk_reg_status_csd(int status)
RobMeades 0:5cffef3371f6 35 {
RobMeades 0:5cffef3371f6 36 switch (status) {
RobMeades 0:5cffef3371f6 37 case CSD_NOT_REGISTERED_NOT_SEARCHING:
RobMeades 0:5cffef3371f6 38 case CSD_NOT_REGISTERED_SEARCHING:
RobMeades 0:5cffef3371f6 39 tr_info("Not (yet) registered for circuit switched service");
RobMeades 0:5cffef3371f6 40 break;
RobMeades 0:5cffef3371f6 41 case CSD_REGISTERED:
RobMeades 0:5cffef3371f6 42 case CSD_REGISTERED_ROAMING:
RobMeades 0:5cffef3371f6 43 tr_info("Registered for circuit switched service");
RobMeades 0:5cffef3371f6 44 break;
RobMeades 0:5cffef3371f6 45 case CSD_REGISTRATION_DENIED:
RobMeades 0:5cffef3371f6 46 tr_info("Circuit switched service denied");
RobMeades 0:5cffef3371f6 47 break;
RobMeades 0:5cffef3371f6 48 case CSD_UNKNOWN_COVERAGE:
RobMeades 0:5cffef3371f6 49 tr_info("Out of circuit switched service coverage");
RobMeades 0:5cffef3371f6 50 break;
RobMeades 0:5cffef3371f6 51 case CSD_SMS_ONLY:
RobMeades 0:5cffef3371f6 52 tr_info("SMS service only");
RobMeades 0:5cffef3371f6 53 break;
RobMeades 0:5cffef3371f6 54 case CSD_SMS_ONLY_ROAMING:
RobMeades 0:5cffef3371f6 55 tr_info("SMS service only");
RobMeades 0:5cffef3371f6 56 break;
RobMeades 0:5cffef3371f6 57 case CSD_CSFB_NOT_PREFERRED:
RobMeades 0:5cffef3371f6 58 tr_info("Registered for circuit switched service with CSFB not preferred");
RobMeades 0:5cffef3371f6 59 break;
RobMeades 0:5cffef3371f6 60 default:
RobMeades 0:5cffef3371f6 61 tr_info("Unknown circuit switched service registration status. %d", status);
RobMeades 0:5cffef3371f6 62 break;
RobMeades 0:5cffef3371f6 63 }
RobMeades 0:5cffef3371f6 64
RobMeades 0:5cffef3371f6 65 _dev_info.reg_status_csd = static_cast<NetworkRegistrationStatusCsd>(status);
RobMeades 0:5cffef3371f6 66 }
RobMeades 0:5cffef3371f6 67
RobMeades 0:5cffef3371f6 68 void UbloxCellularBase::set_nwk_reg_status_psd(int status)
RobMeades 0:5cffef3371f6 69 {
RobMeades 0:5cffef3371f6 70 switch (status) {
RobMeades 0:5cffef3371f6 71 case PSD_NOT_REGISTERED_NOT_SEARCHING:
RobMeades 0:5cffef3371f6 72 case PSD_NOT_REGISTERED_SEARCHING:
RobMeades 0:5cffef3371f6 73 tr_info("Not (yet) registered for packet switched service");
RobMeades 0:5cffef3371f6 74 break;
RobMeades 0:5cffef3371f6 75 case PSD_REGISTERED:
RobMeades 0:5cffef3371f6 76 case PSD_REGISTERED_ROAMING:
RobMeades 0:5cffef3371f6 77 tr_info("Registered for packet switched service");
RobMeades 0:5cffef3371f6 78 break;
RobMeades 0:5cffef3371f6 79 case PSD_REGISTRATION_DENIED:
RobMeades 0:5cffef3371f6 80 tr_info("Packet switched service denied");
RobMeades 0:5cffef3371f6 81 break;
RobMeades 0:5cffef3371f6 82 case PSD_UNKNOWN_COVERAGE:
RobMeades 0:5cffef3371f6 83 tr_info("Out of packet switched service coverage");
RobMeades 0:5cffef3371f6 84 break;
RobMeades 0:5cffef3371f6 85 case PSD_EMERGENCY_SERVICES_ONLY:
RobMeades 0:5cffef3371f6 86 tr_info("Limited access for packet switched service. Emergency use only.");
RobMeades 0:5cffef3371f6 87 break;
RobMeades 0:5cffef3371f6 88 default:
RobMeades 0:5cffef3371f6 89 tr_info("Unknown packet switched service registration status. %d", status);
RobMeades 0:5cffef3371f6 90 break;
RobMeades 0:5cffef3371f6 91 }
RobMeades 0:5cffef3371f6 92
RobMeades 0:5cffef3371f6 93 _dev_info.reg_status_psd = static_cast<NetworkRegistrationStatusPsd>(status);
RobMeades 0:5cffef3371f6 94 }
RobMeades 0:5cffef3371f6 95
RobMeades 0:5cffef3371f6 96 void UbloxCellularBase::set_nwk_reg_status_eps(int status)
RobMeades 0:5cffef3371f6 97 {
RobMeades 0:5cffef3371f6 98 switch (status) {
RobMeades 0:5cffef3371f6 99 case EPS_NOT_REGISTERED_NOT_SEARCHING:
RobMeades 0:5cffef3371f6 100 case EPS_NOT_REGISTERED_SEARCHING:
RobMeades 0:5cffef3371f6 101 tr_info("Not (yet) registered for EPS service");
RobMeades 0:5cffef3371f6 102 break;
RobMeades 0:5cffef3371f6 103 case EPS_REGISTERED:
RobMeades 0:5cffef3371f6 104 case EPS_REGISTERED_ROAMING:
RobMeades 0:5cffef3371f6 105 tr_info("Registered for EPS service");
RobMeades 0:5cffef3371f6 106 break;
RobMeades 0:5cffef3371f6 107 case EPS_REGISTRATION_DENIED:
RobMeades 0:5cffef3371f6 108 tr_info("EPS service denied");
RobMeades 0:5cffef3371f6 109 break;
RobMeades 0:5cffef3371f6 110 case EPS_UNKNOWN_COVERAGE:
RobMeades 0:5cffef3371f6 111 tr_info("Out of EPS service coverage");
RobMeades 0:5cffef3371f6 112 break;
RobMeades 0:5cffef3371f6 113 case EPS_EMERGENCY_SERVICES_ONLY:
RobMeades 0:5cffef3371f6 114 tr_info("Limited access for EPS service. Emergency use only.");
RobMeades 0:5cffef3371f6 115 break;
RobMeades 0:5cffef3371f6 116 default:
RobMeades 0:5cffef3371f6 117 tr_info("Unknown EPS service registration status. %d", status);
RobMeades 0:5cffef3371f6 118 break;
RobMeades 0:5cffef3371f6 119 }
RobMeades 0:5cffef3371f6 120
RobMeades 0:5cffef3371f6 121 _dev_info.reg_status_eps = static_cast<NetworkRegistrationStatusEps>(status);
RobMeades 0:5cffef3371f6 122 }
RobMeades 0:5cffef3371f6 123
RobMeades 0:5cffef3371f6 124 void UbloxCellularBase::set_rat(int AcTStatus)
RobMeades 0:5cffef3371f6 125 {
RobMeades 0:5cffef3371f6 126 switch (AcTStatus) {
RobMeades 0:5cffef3371f6 127 case GSM:
RobMeades 0:5cffef3371f6 128 case COMPACT_GSM:
RobMeades 0:5cffef3371f6 129 tr_info("Connected in GSM");
RobMeades 0:5cffef3371f6 130 break;
RobMeades 0:5cffef3371f6 131 case UTRAN:
RobMeades 0:5cffef3371f6 132 tr_info("Connected to UTRAN");
RobMeades 0:5cffef3371f6 133 break;
RobMeades 0:5cffef3371f6 134 case EDGE:
RobMeades 0:5cffef3371f6 135 tr_info("Connected to EDGE");
RobMeades 0:5cffef3371f6 136 break;
RobMeades 0:5cffef3371f6 137 case HSDPA:
RobMeades 0:5cffef3371f6 138 tr_info("Connected to HSDPA");
RobMeades 0:5cffef3371f6 139 break;
RobMeades 0:5cffef3371f6 140 case HSUPA:
RobMeades 0:5cffef3371f6 141 tr_info("Connected to HSPA");
RobMeades 0:5cffef3371f6 142 break;
RobMeades 0:5cffef3371f6 143 case HSDPA_HSUPA:
RobMeades 0:5cffef3371f6 144 tr_info("Connected to HDPA/HSPA");
RobMeades 0:5cffef3371f6 145 break;
RobMeades 0:5cffef3371f6 146 case LTE:
RobMeades 0:5cffef3371f6 147 tr_info("Connected to LTE");
RobMeades 0:5cffef3371f6 148 break;
fahimalavi 10:c4281fa79b8f 149 case EC_GSM_IoT:
fahimalavi 10:c4281fa79b8f 150 tr_info("Connected to EC_GSM_IoT");
fahimalavi 10:c4281fa79b8f 151 break;
fahimalavi 10:c4281fa79b8f 152 case E_UTRAN_NB_S1:
fahimalavi 10:c4281fa79b8f 153 tr_info("Connected to E_UTRAN NB1");
fahimalavi 10:c4281fa79b8f 154 break;
RobMeades 0:5cffef3371f6 155 default:
RobMeades 0:5cffef3371f6 156 tr_info("Unknown RAT %d", AcTStatus);
RobMeades 0:5cffef3371f6 157 break;
RobMeades 0:5cffef3371f6 158 }
RobMeades 0:5cffef3371f6 159
RobMeades 0:5cffef3371f6 160 _dev_info.rat = static_cast<RadioAccessNetworkType>(AcTStatus);
RobMeades 0:5cffef3371f6 161 }
RobMeades 0:5cffef3371f6 162
RobMeades 0:5cffef3371f6 163 bool UbloxCellularBase::get_iccid()
RobMeades 0:5cffef3371f6 164 {
RobMeades 0:5cffef3371f6 165 bool success;
RobMeades 0:5cffef3371f6 166 LOCK();
RobMeades 0:5cffef3371f6 167
RobMeades 0:5cffef3371f6 168 MBED_ASSERT(_at != NULL);
RobMeades 0:5cffef3371f6 169
RobMeades 0:5cffef3371f6 170 // Returns the ICCID (Integrated Circuit Card ID) of the SIM-card.
RobMeades 0:5cffef3371f6 171 // ICCID is a serial number identifying the SIM.
RobMeades 0:5cffef3371f6 172 // AT Command Manual UBX-13002752, section 4.12
RobMeades 0:5cffef3371f6 173 success = _at->send("AT+CCID") && _at->recv("+CCID: %20[^\n]\nOK\n", _dev_info.iccid);
RobMeades 0:5cffef3371f6 174 tr_info("DevInfo: ICCID=%s", _dev_info.iccid);
RobMeades 0:5cffef3371f6 175
RobMeades 0:5cffef3371f6 176 UNLOCK();
RobMeades 0:5cffef3371f6 177 return success;
RobMeades 0:5cffef3371f6 178 }
RobMeades 0:5cffef3371f6 179
RobMeades 0:5cffef3371f6 180 bool UbloxCellularBase::get_imsi()
RobMeades 0:5cffef3371f6 181 {
RobMeades 0:5cffef3371f6 182 bool success;
RobMeades 0:5cffef3371f6 183 LOCK();
RobMeades 0:5cffef3371f6 184
RobMeades 0:5cffef3371f6 185 MBED_ASSERT(_at != NULL);
RobMeades 0:5cffef3371f6 186
RobMeades 0:5cffef3371f6 187 // International mobile subscriber identification
RobMeades 0:5cffef3371f6 188 // AT Command Manual UBX-13002752, section 4.11
RobMeades 0:5cffef3371f6 189 success = _at->send("AT+CIMI") && _at->recv("%15[^\n]\nOK\n", _dev_info.imsi);
RobMeades 0:5cffef3371f6 190 tr_info("DevInfo: IMSI=%s", _dev_info.imsi);
RobMeades 0:5cffef3371f6 191
RobMeades 0:5cffef3371f6 192 UNLOCK();
RobMeades 0:5cffef3371f6 193 return success;
RobMeades 0:5cffef3371f6 194 }
RobMeades 0:5cffef3371f6 195
RobMeades 0:5cffef3371f6 196 bool UbloxCellularBase::get_imei()
RobMeades 0:5cffef3371f6 197 {
RobMeades 0:5cffef3371f6 198 bool success;
RobMeades 0:5cffef3371f6 199 LOCK();
RobMeades 0:5cffef3371f6 200
RobMeades 0:5cffef3371f6 201 MBED_ASSERT(_at != NULL);
RobMeades 0:5cffef3371f6 202
RobMeades 0:5cffef3371f6 203 // International mobile equipment identifier
RobMeades 0:5cffef3371f6 204 // AT Command Manual UBX-13002752, section 4.7
RobMeades 0:5cffef3371f6 205 success = _at->send("AT+CGSN") && _at->recv("%15[^\n]\nOK\n", _dev_info.imei);
RobMeades 0:5cffef3371f6 206 tr_info("DevInfo: IMEI=%s", _dev_info.imei);
RobMeades 0:5cffef3371f6 207
RobMeades 0:5cffef3371f6 208 UNLOCK();
RobMeades 0:5cffef3371f6 209 return success;
RobMeades 0:5cffef3371f6 210 }
RobMeades 0:5cffef3371f6 211
fahim alavi 11:57f64bc518c0 212 /** Get the IMEI.
fahim alavi 11:57f64bc518c0 213 *
fahim alavi 11:57f64bc518c0 214 * @return true if successful, otherwise false.
fahim alavi 11:57f64bc518c0 215 */
fahim alavi 11:57f64bc518c0 216 bool UbloxCellularBase::get_imei(char *imei_to_send,int size)
fahim alavi 11:57f64bc518c0 217 {
fahim alavi 11:57f64bc518c0 218 bool success;
fahim alavi 11:57f64bc518c0 219 LOCK();
fahim alavi 11:57f64bc518c0 220
fahim alavi 11:57f64bc518c0 221 MBED_ASSERT(_at != NULL);
fahim alavi 11:57f64bc518c0 222
fahim alavi 11:57f64bc518c0 223 // International mobile equipment identifier
fahim alavi 11:57f64bc518c0 224 // AT Command Manual UBX-13002752, section 4.7
fahim alavi 11:57f64bc518c0 225 success = _at->send("AT+CGSN") && _at->recv("%15[^\n]\nOK\n", _dev_info.imei);
fahim alavi 11:57f64bc518c0 226 tr_info("DevInfo: IMEI=%s", _dev_info.imei);
fahim alavi 11:57f64bc518c0 227
fahim alavi 11:57f64bc518c0 228 if(success)
fahim alavi 11:57f64bc518c0 229 {
fahim alavi 11:57f64bc518c0 230 memcpy(imei_to_send,_dev_info.imei,size);
fahim alavi 11:57f64bc518c0 231 imei_to_send[size-1] = '\0';
fahim alavi 11:57f64bc518c0 232 }
fahim alavi 11:57f64bc518c0 233
fahim alavi 11:57f64bc518c0 234 UNLOCK();
fahim alavi 11:57f64bc518c0 235 return success;
fahim alavi 11:57f64bc518c0 236 }
fahim alavi 11:57f64bc518c0 237
RobMeades 0:5cffef3371f6 238 bool UbloxCellularBase::get_meid()
RobMeades 0:5cffef3371f6 239 {
RobMeades 0:5cffef3371f6 240 bool success;
RobMeades 0:5cffef3371f6 241 LOCK();
RobMeades 0:5cffef3371f6 242
RobMeades 0:5cffef3371f6 243 MBED_ASSERT(_at != NULL);
RobMeades 0:5cffef3371f6 244
RobMeades 0:5cffef3371f6 245 // Mobile equipment identifier
RobMeades 0:5cffef3371f6 246 // AT Command Manual UBX-13002752, section 4.8
RobMeades 0:5cffef3371f6 247 success = _at->send("AT+GSN") && _at->recv("%18[^\n]\nOK\n", _dev_info.meid);
RobMeades 0:5cffef3371f6 248 tr_info("DevInfo: MEID=%s", _dev_info.meid);
RobMeades 0:5cffef3371f6 249
RobMeades 0:5cffef3371f6 250 UNLOCK();
RobMeades 0:5cffef3371f6 251 return success;
RobMeades 0:5cffef3371f6 252 }
RobMeades 0:5cffef3371f6 253
RobMeades 0:5cffef3371f6 254 bool UbloxCellularBase::set_sms()
RobMeades 0:5cffef3371f6 255 {
RobMeades 0:5cffef3371f6 256 bool success = false;
RobMeades 0:5cffef3371f6 257 char buf[32];
RobMeades 0:5cffef3371f6 258 LOCK();
RobMeades 0:5cffef3371f6 259
RobMeades 0:5cffef3371f6 260 MBED_ASSERT(_at != NULL);
RobMeades 0:5cffef3371f6 261
RobMeades 0:5cffef3371f6 262 // Set up SMS format and enable URC
RobMeades 0:5cffef3371f6 263 // AT Command Manual UBX-13002752, section 11
RobMeades 0:5cffef3371f6 264 if (_at->send("AT+CMGF=1") && _at->recv("OK")) {
RobMeades 0:5cffef3371f6 265 tr_debug("SMS in text mode");
RobMeades 0:5cffef3371f6 266 if (_at->send("AT+CNMI=2,1") && _at->recv("OK")) {
RobMeades 0:5cffef3371f6 267 tr_debug("SMS URC enabled");
RobMeades 0:5cffef3371f6 268 // Set to CS preferred since PS preferred doesn't work
RobMeades 0:5cffef3371f6 269 // on some networks
RobMeades 0:5cffef3371f6 270 if (_at->send("AT+CGSMS=1") && _at->recv("OK")) {
RobMeades 0:5cffef3371f6 271 tr_debug("SMS set to CS preferred");
RobMeades 0:5cffef3371f6 272 success = true;
RobMeades 0:5cffef3371f6 273 memset (buf, 0, sizeof (buf));
RobMeades 0:5cffef3371f6 274 if (_at->send("AT+CSCA?") &&
RobMeades 0:5cffef3371f6 275 _at->recv("+CSCA: \"%31[^\"]\"", buf) &&
RobMeades 0:5cffef3371f6 276 _at->recv("OK")) {
RobMeades 0:5cffef3371f6 277 tr_info("SMS Service Centre address is \"%s\"", buf);
RobMeades 0:5cffef3371f6 278 }
RobMeades 0:5cffef3371f6 279 }
RobMeades 0:5cffef3371f6 280 }
RobMeades 0:5cffef3371f6 281 }
RobMeades 0:5cffef3371f6 282
RobMeades 0:5cffef3371f6 283 UNLOCK();
RobMeades 0:5cffef3371f6 284 return success;
RobMeades 0:5cffef3371f6 285 }
RobMeades 0:5cffef3371f6 286
RobMeades 0:5cffef3371f6 287 void UbloxCellularBase::parser_abort_cb()
RobMeades 0:5cffef3371f6 288 {
RobMeades 0:5cffef3371f6 289 _at->abort();
RobMeades 0:5cffef3371f6 290 }
RobMeades 0:5cffef3371f6 291
RobMeades 0:5cffef3371f6 292 // Callback for CME ERROR and CMS ERROR.
RobMeades 0:5cffef3371f6 293 void UbloxCellularBase::CMX_ERROR_URC()
RobMeades 0:5cffef3371f6 294 {
RobMeades 0:5cffef3371f6 295 char buf[48];
RobMeades 0:5cffef3371f6 296
RobMeades 0:5cffef3371f6 297 if (read_at_to_char(buf, sizeof (buf), '\n') > 0) {
RobMeades 0:5cffef3371f6 298 tr_debug("AT error %s", buf);
RobMeades 0:5cffef3371f6 299 }
RobMeades 0:5cffef3371f6 300 parser_abort_cb();
RobMeades 0:5cffef3371f6 301 }
RobMeades 0:5cffef3371f6 302
RobMeades 0:5cffef3371f6 303 // Callback for circuit switched registration URC.
RobMeades 0:5cffef3371f6 304 void UbloxCellularBase::CREG_URC()
RobMeades 0:5cffef3371f6 305 {
RobMeades 0:5cffef3371f6 306 char buf[10];
RobMeades 0:5cffef3371f6 307 int status;
RobMeades 0:5cffef3371f6 308
RobMeades 0:5cffef3371f6 309 // If this is the URC it will be a single
RobMeades 0:5cffef3371f6 310 // digit followed by \n. If it is the
RobMeades 0:5cffef3371f6 311 // answer to a CREG query, it will be
RobMeades 0:5cffef3371f6 312 // a ": %d,%d\n" where the second digit
RobMeades 0:5cffef3371f6 313 // indicates the status
RobMeades 0:5cffef3371f6 314 // Note: not calling _at->recv() from here as we're
RobMeades 0:5cffef3371f6 315 // already in an _at->recv()
RobMeades 0:5cffef3371f6 316 if (read_at_to_char(buf, sizeof (buf), '\n') > 0) {
RobMeades 0:5cffef3371f6 317 if (sscanf(buf, ": %*d,%d", &status) == 1) {
RobMeades 0:5cffef3371f6 318 set_nwk_reg_status_csd(status);
RobMeades 0:5cffef3371f6 319 } else if (sscanf(buf, ": %d", &status) == 1) {
RobMeades 0:5cffef3371f6 320 set_nwk_reg_status_csd(status);
RobMeades 0:5cffef3371f6 321 }
RobMeades 0:5cffef3371f6 322 }
RobMeades 0:5cffef3371f6 323 }
RobMeades 0:5cffef3371f6 324
RobMeades 0:5cffef3371f6 325 // Callback for packet switched registration URC.
RobMeades 0:5cffef3371f6 326 void UbloxCellularBase::CGREG_URC()
RobMeades 0:5cffef3371f6 327 {
RobMeades 0:5cffef3371f6 328 char buf[10];
RobMeades 0:5cffef3371f6 329 int status;
RobMeades 0:5cffef3371f6 330
RobMeades 0:5cffef3371f6 331 // If this is the URC it will be a single
RobMeades 0:5cffef3371f6 332 // digit followed by \n. If it is the
RobMeades 0:5cffef3371f6 333 // answer to a CGREG query, it will be
RobMeades 0:5cffef3371f6 334 // a ": %d,%d\n" where the second digit
RobMeades 0:5cffef3371f6 335 // indicates the status
RobMeades 0:5cffef3371f6 336 // Note: not calling _at->recv() from here as we're
RobMeades 0:5cffef3371f6 337 // already in an _at->recv()
RobMeades 0:5cffef3371f6 338 if (read_at_to_char(buf, sizeof (buf), '\n') > 0) {
RobMeades 0:5cffef3371f6 339 if (sscanf(buf, ": %*d,%d", &status) == 1) {
RobMeades 0:5cffef3371f6 340 set_nwk_reg_status_psd(status);
RobMeades 0:5cffef3371f6 341 } else if (sscanf(buf, ": %d", &status) == 1) {
RobMeades 0:5cffef3371f6 342 set_nwk_reg_status_psd(status);
RobMeades 0:5cffef3371f6 343 }
RobMeades 0:5cffef3371f6 344 }
RobMeades 0:5cffef3371f6 345 }
RobMeades 0:5cffef3371f6 346
RobMeades 0:5cffef3371f6 347 // Callback for EPS registration URC.
RobMeades 0:5cffef3371f6 348 void UbloxCellularBase::CEREG_URC()
RobMeades 0:5cffef3371f6 349 {
RobMeades 0:5cffef3371f6 350 char buf[10];
RobMeades 0:5cffef3371f6 351 int status;
RobMeades 0:5cffef3371f6 352
RobMeades 0:5cffef3371f6 353 // If this is the URC it will be a single
RobMeades 0:5cffef3371f6 354 // digit followed by \n. If it is the
RobMeades 0:5cffef3371f6 355 // answer to a CEREG query, it will be
RobMeades 0:5cffef3371f6 356 // a ": %d,%d\n" where the second digit
RobMeades 0:5cffef3371f6 357 // indicates the status
RobMeades 0:5cffef3371f6 358 // Note: not calling _at->recv() from here as we're
RobMeades 0:5cffef3371f6 359 // already in an _at->recv()
RobMeades 0:5cffef3371f6 360 if (read_at_to_char(buf, sizeof (buf), '\n') > 0) {
RobMeades 0:5cffef3371f6 361 if (sscanf(buf, ": %*d,%d", &status) == 1) {
RobMeades 0:5cffef3371f6 362 set_nwk_reg_status_eps(status);
RobMeades 0:5cffef3371f6 363 } else if (sscanf(buf, ": %d", &status) == 1) {
RobMeades 0:5cffef3371f6 364 set_nwk_reg_status_eps(status);
RobMeades 0:5cffef3371f6 365 }
RobMeades 0:5cffef3371f6 366 }
RobMeades 0:5cffef3371f6 367 }
RobMeades 0:5cffef3371f6 368
RobMeades 0:5cffef3371f6 369 // Callback UMWI, just filtering it out.
RobMeades 0:5cffef3371f6 370 void UbloxCellularBase::UMWI_URC()
RobMeades 0:5cffef3371f6 371 {
RobMeades 0:5cffef3371f6 372 char buf[10];
RobMeades 0:5cffef3371f6 373
RobMeades 0:5cffef3371f6 374 // Note: not calling _at->recv() from here as we're
RobMeades 0:5cffef3371f6 375 // already in an _at->recv()
RobMeades 0:5cffef3371f6 376 read_at_to_char(buf, sizeof (buf), '\n');
RobMeades 0:5cffef3371f6 377 }
RobMeades 0:5cffef3371f6 378
RobMeades 0:5cffef3371f6 379 /**********************************************************************
RobMeades 0:5cffef3371f6 380 * PROTECTED METHODS
RobMeades 0:5cffef3371f6 381 **********************************************************************/
RobMeades 0:5cffef3371f6 382
RobMeades 0:5cffef3371f6 383 #if MODEM_ON_BOARD
RobMeades 0:5cffef3371f6 384 void UbloxCellularBase::modem_init()
RobMeades 0:5cffef3371f6 385 {
RobMeades 0:5cffef3371f6 386 ::onboard_modem_init();
RobMeades 0:5cffef3371f6 387 }
RobMeades 0:5cffef3371f6 388
RobMeades 0:5cffef3371f6 389 void UbloxCellularBase::modem_deinit()
RobMeades 0:5cffef3371f6 390 {
RobMeades 0:5cffef3371f6 391 ::onboard_modem_deinit();
RobMeades 0:5cffef3371f6 392 }
RobMeades 0:5cffef3371f6 393
RobMeades 0:5cffef3371f6 394 void UbloxCellularBase::modem_power_up()
RobMeades 0:5cffef3371f6 395 {
RobMeades 0:5cffef3371f6 396 ::onboard_modem_power_up();
RobMeades 0:5cffef3371f6 397 }
RobMeades 0:5cffef3371f6 398
RobMeades 0:5cffef3371f6 399 void UbloxCellularBase::modem_power_down()
RobMeades 0:5cffef3371f6 400 {
RobMeades 0:5cffef3371f6 401 ::onboard_modem_power_down();
RobMeades 0:5cffef3371f6 402 }
RobMeades 0:5cffef3371f6 403 #else
RobMeades 0:5cffef3371f6 404 void UbloxCellularBase::modem_init()
RobMeades 0:5cffef3371f6 405 {
RobMeades 0:5cffef3371f6 406 // Meant to be overridden
RobMeades 0:5cffef3371f6 407 }
RobMeades 0:5cffef3371f6 408
RobMeades 0:5cffef3371f6 409 void UbloxCellularBase::modem_deinit()
RobMeades 0:5cffef3371f6 410 {
RobMeades 0:5cffef3371f6 411 // Meant to be overridden
RobMeades 0:5cffef3371f6 412 }
RobMeades 0:5cffef3371f6 413
RobMeades 0:5cffef3371f6 414 void UbloxCellularBase::modem_power_up()
RobMeades 0:5cffef3371f6 415 {
RobMeades 0:5cffef3371f6 416 // Meant to be overridden
RobMeades 0:5cffef3371f6 417 }
RobMeades 0:5cffef3371f6 418
RobMeades 0:5cffef3371f6 419 void UbloxCellularBase::modem_power_down()
RobMeades 0:5cffef3371f6 420 {
RobMeades 0:5cffef3371f6 421 // Mmeant to be overridden
RobMeades 0:5cffef3371f6 422 }
RobMeades 0:5cffef3371f6 423 #endif
RobMeades 0:5cffef3371f6 424
RobMeades 0:5cffef3371f6 425 // Constructor.
RobMeades 0:5cffef3371f6 426 // Note: to allow this base class to be inherited as a virtual base class
RobMeades 0:5cffef3371f6 427 // by everyone, it takes no parameters. See also comment above classInit()
RobMeades 0:5cffef3371f6 428 // in the header file.
RobMeades 0:5cffef3371f6 429 UbloxCellularBase::UbloxCellularBase()
RobMeades 0:5cffef3371f6 430 {
RobMeades 0:5cffef3371f6 431 _pin = NULL;
RobMeades 0:5cffef3371f6 432 _at = NULL;
RobMeades 0:5cffef3371f6 433 _at_timeout = AT_PARSER_TIMEOUT;
RobMeades 0:5cffef3371f6 434 _fh = NULL;
RobMeades 0:5cffef3371f6 435 _modem_initialised = false;
RobMeades 0:5cffef3371f6 436 _sim_pin_check_enabled = false;
RobMeades 0:5cffef3371f6 437 _debug_trace_on = false;
RobMeades 0:5cffef3371f6 438
RobMeades 0:5cffef3371f6 439 _dev_info.dev = DEV_TYPE_NONE;
RobMeades 0:5cffef3371f6 440 _dev_info.reg_status_csd = CSD_NOT_REGISTERED_NOT_SEARCHING;
RobMeades 0:5cffef3371f6 441 _dev_info.reg_status_psd = PSD_NOT_REGISTERED_NOT_SEARCHING;
RobMeades 0:5cffef3371f6 442 _dev_info.reg_status_eps = EPS_NOT_REGISTERED_NOT_SEARCHING;
RobMeades 0:5cffef3371f6 443 }
RobMeades 0:5cffef3371f6 444
RobMeades 0:5cffef3371f6 445 // Destructor.
RobMeades 0:5cffef3371f6 446 UbloxCellularBase::~UbloxCellularBase()
RobMeades 0:5cffef3371f6 447 {
RobMeades 0:5cffef3371f6 448 deinit();
RobMeades 0:5cffef3371f6 449 delete _at;
RobMeades 0:5cffef3371f6 450 delete _fh;
RobMeades 0:5cffef3371f6 451 }
RobMeades 0:5cffef3371f6 452
RobMeades 0:5cffef3371f6 453 // Initialise the portions of this class that are parameterised.
RobMeades 0:5cffef3371f6 454 void UbloxCellularBase::baseClassInit(PinName tx, PinName rx,
RobMeades 0:5cffef3371f6 455 int baud, bool debug_on)
RobMeades 0:5cffef3371f6 456 {
RobMeades 0:5cffef3371f6 457 // Only initialise ourselves if it's not already been done
RobMeades 0:5cffef3371f6 458 if (_at == NULL) {
RobMeades 0:5cffef3371f6 459 if (_debug_trace_on == false) {
RobMeades 0:5cffef3371f6 460 _debug_trace_on = debug_on;
RobMeades 0:5cffef3371f6 461 }
rob.meades@u-blox.com 4:2e640a101db1 462 _baud = baud;
RobMeades 0:5cffef3371f6 463
RobMeades 0:5cffef3371f6 464 // Set up File Handle for buffered serial comms with cellular module
RobMeades 0:5cffef3371f6 465 // (which will be used by the AT parser)
rob.meades@u-blox.com 4:2e640a101db1 466 // Note: the UART is initialised to run no faster than 115200 because
rob.meades@u-blox.com 4:2e640a101db1 467 // the modems cannot reliably auto-baud at faster rates. The faster
rob.meades@u-blox.com 4:2e640a101db1 468 // rate is adopted later with a specific AT command and the
rob.meades@u-blox.com 4:2e640a101db1 469 // UARTSerial rate is adjusted at that time
rob.meades@u-blox.com 4:2e640a101db1 470 if (baud > 115200) {
rob.meades@u-blox.com 4:2e640a101db1 471 baud = 115200;
rob.meades@u-blox.com 4:2e640a101db1 472 }
RobMeades 0:5cffef3371f6 473 _fh = new UARTSerial(tx, rx, baud);
RobMeades 0:5cffef3371f6 474
RobMeades 0:5cffef3371f6 475 // Set up the AT parser
RobMeades 0:5cffef3371f6 476 _at = new ATCmdParser(_fh, OUTPUT_ENTER_KEY, AT_PARSER_BUFFER_SIZE,
rob.meades@u-blox.com 3:f9b2cd6f72b1 477 _at_timeout, _debug_trace_on);
RobMeades 0:5cffef3371f6 478
RobMeades 0:5cffef3371f6 479 // Error cases, out of band handling
RobMeades 0:5cffef3371f6 480 _at->oob("ERROR", callback(this, &UbloxCellularBase::parser_abort_cb));
RobMeades 0:5cffef3371f6 481 _at->oob("+CME ERROR", callback(this, &UbloxCellularBase::CMX_ERROR_URC));
RobMeades 0:5cffef3371f6 482 _at->oob("+CMS ERROR", callback(this, &UbloxCellularBase::CMX_ERROR_URC));
RobMeades 0:5cffef3371f6 483
RobMeades 0:5cffef3371f6 484 // Registration status, out of band handling
RobMeades 0:5cffef3371f6 485 _at->oob("+CREG", callback(this, &UbloxCellularBase::CREG_URC));
RobMeades 0:5cffef3371f6 486 _at->oob("+CGREG", callback(this, &UbloxCellularBase::CGREG_URC));
RobMeades 0:5cffef3371f6 487 _at->oob("+CEREG", callback(this, &UbloxCellularBase::CEREG_URC));
RobMeades 0:5cffef3371f6 488
RobMeades 0:5cffef3371f6 489 // Capture the UMWI, just to stop it getting in the way
RobMeades 0:5cffef3371f6 490 _at->oob("+UMWI", callback(this, &UbloxCellularBase::UMWI_URC));
RobMeades 0:5cffef3371f6 491 }
RobMeades 0:5cffef3371f6 492 }
RobMeades 0:5cffef3371f6 493
RobMeades 0:5cffef3371f6 494 // Set the AT parser timeout.
RobMeades 0:5cffef3371f6 495 // Note: the AT interface should be locked before this is called.
RobMeades 0:5cffef3371f6 496 void UbloxCellularBase::at_set_timeout(int timeout) {
RobMeades 0:5cffef3371f6 497
RobMeades 0:5cffef3371f6 498 MBED_ASSERT(_at != NULL);
RobMeades 0:5cffef3371f6 499
RobMeades 0:5cffef3371f6 500 _at_timeout = timeout;
RobMeades 0:5cffef3371f6 501 _at->set_timeout(timeout);
RobMeades 0:5cffef3371f6 502 }
RobMeades 0:5cffef3371f6 503
RobMeades 0:5cffef3371f6 504 // Read up to size bytes from the AT interface up to a "end".
RobMeades 0:5cffef3371f6 505 // Note: the AT interface should be locked before this is called.
RobMeades 0:5cffef3371f6 506 int UbloxCellularBase::read_at_to_char(char * buf, int size, char end)
RobMeades 0:5cffef3371f6 507 {
RobMeades 0:5cffef3371f6 508 int count = 0;
RobMeades 0:5cffef3371f6 509 int x = 0;
RobMeades 0:5cffef3371f6 510
RobMeades 0:5cffef3371f6 511 if (size > 0) {
RobMeades 0:5cffef3371f6 512 for (count = 0; (count < size) && (x >= 0) && (x != end); count++) {
RobMeades 0:5cffef3371f6 513 x = _at->getc();
RobMeades 0:5cffef3371f6 514 *(buf + count) = (char) x;
RobMeades 0:5cffef3371f6 515 }
RobMeades 0:5cffef3371f6 516
RobMeades 0:5cffef3371f6 517 count--;
RobMeades 0:5cffef3371f6 518 *(buf + count) = 0;
RobMeades 0:5cffef3371f6 519
RobMeades 0:5cffef3371f6 520 // Convert line endings:
RobMeades 0:5cffef3371f6 521 // If end was '\n' (0x0a) and the preceding character was 0x0d, then
RobMeades 0:5cffef3371f6 522 // overwrite that with null as well.
RobMeades 0:5cffef3371f6 523 if ((count > 0) && (end == '\n') && (*(buf + count - 1) == '\x0d')) {
RobMeades 0:5cffef3371f6 524 count--;
RobMeades 0:5cffef3371f6 525 *(buf + count) = 0;
RobMeades 0:5cffef3371f6 526 }
RobMeades 0:5cffef3371f6 527 }
RobMeades 0:5cffef3371f6 528
RobMeades 0:5cffef3371f6 529 return count;
RobMeades 0:5cffef3371f6 530 }
RobMeades 0:5cffef3371f6 531
RobMeades 0:5cffef3371f6 532 // Power up the modem.
RobMeades 0:5cffef3371f6 533 // Enables the GPIO lines to the modem and then wriggles the power line in short pulses.
RobMeades 0:5cffef3371f6 534 bool UbloxCellularBase::power_up()
RobMeades 0:5cffef3371f6 535 {
RobMeades 0:5cffef3371f6 536 bool success = false;
RobMeades 0:5cffef3371f6 537 int at_timeout;
RobMeades 0:5cffef3371f6 538 LOCK();
RobMeades 0:5cffef3371f6 539
RobMeades 0:5cffef3371f6 540 at_timeout = _at_timeout; // Has to be inside LOCK()s
RobMeades 0:5cffef3371f6 541
RobMeades 0:5cffef3371f6 542 MBED_ASSERT(_at != NULL);
RobMeades 0:5cffef3371f6 543
RobMeades 0:5cffef3371f6 544 /* Initialize GPIO lines */
RobMeades 0:5cffef3371f6 545 tr_info("Powering up modem...");
pilotak 7:c2cf27a981b3 546 modem_init();
RobMeades 0:5cffef3371f6 547 /* Give modem a little time to settle down */
RobMeades 0:5cffef3371f6 548 wait_ms(250);
RobMeades 0:5cffef3371f6 549
RobMeades 0:5cffef3371f6 550 for (int retry_count = 0; !success && (retry_count < 20); retry_count++) {
pilotak 7:c2cf27a981b3 551 modem_power_up();
RobMeades 0:5cffef3371f6 552 wait_ms(500);
RobMeades 0:5cffef3371f6 553 // Modem tends to spit out noise during power up - don't confuse the parser
RobMeades 0:5cffef3371f6 554 _at->flush();
RobMeades 0:5cffef3371f6 555 at_set_timeout(1000);
RobMeades 0:5cffef3371f6 556 if (_at->send("AT")) {
RobMeades 0:5cffef3371f6 557 // C027 needs a delay here
RobMeades 0:5cffef3371f6 558 wait_ms(100);
RobMeades 0:5cffef3371f6 559 if (_at->recv("OK")) {
RobMeades 0:5cffef3371f6 560 success = true;
RobMeades 0:5cffef3371f6 561 }
RobMeades 0:5cffef3371f6 562 }
RobMeades 0:5cffef3371f6 563 at_set_timeout(at_timeout);
RobMeades 0:5cffef3371f6 564 }
RobMeades 0:5cffef3371f6 565
rob.meades@u-blox.com 4:2e640a101db1 566 if (success) {
rob.meades@u-blox.com 4:2e640a101db1 567 // Set the final baud rate
rob.meades@u-blox.com 4:2e640a101db1 568 if (_at->send("AT+IPR=%d", _baud) && _at->recv("OK")) {
rob.meades@u-blox.com 4:2e640a101db1 569 // Need to wait for things to be sorted out on the modem side
rob.meades@u-blox.com 4:2e640a101db1 570 wait_ms(100);
rob.meades@u-blox.com 4:2e640a101db1 571 ((UARTSerial *)_fh)->set_baud(_baud);
rob.meades@u-blox.com 4:2e640a101db1 572 }
rob.meades@u-blox.com 4:2e640a101db1 573
rob.meades@u-blox.com 4:2e640a101db1 574 // Turn off modem echoing and turn on verbose responses
rob.meades@u-blox.com 4:2e640a101db1 575 success = _at->send("ATE0;+CMEE=2") && _at->recv("OK") &&
rob.meades@u-blox.com 4:2e640a101db1 576 // The following commands are best sent separately
rob.meades@u-blox.com 4:2e640a101db1 577 _at->send("AT&K0") && _at->recv("OK") && // Turn off RTC/CTS handshaking
rob.meades@u-blox.com 4:2e640a101db1 578 _at->send("AT&C1") && _at->recv("OK") && // Set DCD circuit(109), changes in accordance with the carrier detect status
rob.meades@u-blox.com 4:2e640a101db1 579 _at->send("AT&D0") && _at->recv("OK"); // Set DTR circuit, we ignore the state change of DTR
rob.meades@u-blox.com 4:2e640a101db1 580 }
RobMeades 0:5cffef3371f6 581
RobMeades 0:5cffef3371f6 582 if (!success) {
RobMeades 0:5cffef3371f6 583 tr_error("Preliminary modem setup failed.");
RobMeades 0:5cffef3371f6 584 }
RobMeades 0:5cffef3371f6 585
RobMeades 0:5cffef3371f6 586 UNLOCK();
RobMeades 0:5cffef3371f6 587 return success;
RobMeades 0:5cffef3371f6 588 }
RobMeades 0:5cffef3371f6 589
RobMeades 0:5cffef3371f6 590 // Power down modem via AT interface.
RobMeades 0:5cffef3371f6 591 void UbloxCellularBase::power_down()
RobMeades 0:5cffef3371f6 592 {
RobMeades 0:5cffef3371f6 593 LOCK();
RobMeades 0:5cffef3371f6 594
RobMeades 0:5cffef3371f6 595 MBED_ASSERT(_at != NULL);
RobMeades 0:5cffef3371f6 596
RobMeades 0:5cffef3371f6 597 // If we have been running, do a soft power-off first
RobMeades 0:5cffef3371f6 598 if (_modem_initialised && (_at != NULL)) {
RobMeades 0:5cffef3371f6 599 _at->send("AT+CPWROFF") && _at->recv("OK");
RobMeades 0:5cffef3371f6 600 }
RobMeades 0:5cffef3371f6 601
RobMeades 0:5cffef3371f6 602 // Now do a hard power-off
pilotak 7:c2cf27a981b3 603 modem_power_down();
pilotak 7:c2cf27a981b3 604 modem_deinit();
RobMeades 0:5cffef3371f6 605
RobMeades 0:5cffef3371f6 606 _dev_info.reg_status_csd = CSD_NOT_REGISTERED_NOT_SEARCHING;
RobMeades 0:5cffef3371f6 607 _dev_info.reg_status_psd = PSD_NOT_REGISTERED_NOT_SEARCHING;
RobMeades 0:5cffef3371f6 608 _dev_info.reg_status_eps = EPS_NOT_REGISTERED_NOT_SEARCHING;
RobMeades 0:5cffef3371f6 609
RobMeades 0:5cffef3371f6 610 UNLOCK();
RobMeades 0:5cffef3371f6 611 }
RobMeades 0:5cffef3371f6 612
RobMeades 0:5cffef3371f6 613 // Get the device ID.
RobMeades 0:5cffef3371f6 614 bool UbloxCellularBase::set_device_identity(DeviceType *dev)
RobMeades 0:5cffef3371f6 615 {
RobMeades 0:5cffef3371f6 616 char buf[20];
RobMeades 0:5cffef3371f6 617 bool success;
RobMeades 0:5cffef3371f6 618 LOCK();
RobMeades 0:5cffef3371f6 619
RobMeades 0:5cffef3371f6 620 MBED_ASSERT(_at != NULL);
RobMeades 0:5cffef3371f6 621
RobMeades 0:5cffef3371f6 622 success = _at->send("ATI") && _at->recv("%19[^\n]\nOK\n", buf);
RobMeades 0:5cffef3371f6 623
RobMeades 0:5cffef3371f6 624 if (success) {
RobMeades 0:5cffef3371f6 625 if (strstr(buf, "SARA-G35"))
RobMeades 0:5cffef3371f6 626 *dev = DEV_SARA_G35;
RobMeades 0:5cffef3371f6 627 else if (strstr(buf, "LISA-U200-03S"))
RobMeades 0:5cffef3371f6 628 *dev = DEV_LISA_U2_03S;
RobMeades 0:5cffef3371f6 629 else if (strstr(buf, "LISA-U2"))
RobMeades 0:5cffef3371f6 630 *dev = DEV_LISA_U2;
RobMeades 0:5cffef3371f6 631 else if (strstr(buf, "SARA-U2"))
RobMeades 0:5cffef3371f6 632 *dev = DEV_SARA_U2;
fahimalavi 10:c4281fa79b8f 633 else if (strstr(buf, "SARA-R4"))
fahimalavi 10:c4281fa79b8f 634 *dev = DEV_SARA_R4;
RobMeades 0:5cffef3371f6 635 else if (strstr(buf, "LEON-G2"))
RobMeades 0:5cffef3371f6 636 *dev = DEV_LEON_G2;
RobMeades 0:5cffef3371f6 637 else if (strstr(buf, "TOBY-L2"))
RobMeades 0:5cffef3371f6 638 *dev = DEV_TOBY_L2;
RobMeades 0:5cffef3371f6 639 else if (strstr(buf, "MPCI-L2"))
RobMeades 0:5cffef3371f6 640 *dev = DEV_MPCI_L2;
RobMeades 0:5cffef3371f6 641 }
RobMeades 0:5cffef3371f6 642
RobMeades 0:5cffef3371f6 643 UNLOCK();
RobMeades 0:5cffef3371f6 644 return success;
RobMeades 0:5cffef3371f6 645 }
RobMeades 0:5cffef3371f6 646
RobMeades 0:5cffef3371f6 647 // Send initialisation AT commands that are specific to the device.
RobMeades 0:5cffef3371f6 648 bool UbloxCellularBase::device_init(DeviceType dev)
RobMeades 0:5cffef3371f6 649 {
RobMeades 0:5cffef3371f6 650 bool success = false;
RobMeades 0:5cffef3371f6 651 LOCK();
RobMeades 0:5cffef3371f6 652
RobMeades 0:5cffef3371f6 653 MBED_ASSERT(_at != NULL);
RobMeades 0:5cffef3371f6 654
RobMeades 0:5cffef3371f6 655 if ((dev == DEV_LISA_U2) || (dev == DEV_LEON_G2) || (dev == DEV_TOBY_L2)) {
RobMeades 0:5cffef3371f6 656 success = _at->send("AT+UGPIOC=20,2") && _at->recv("OK");
RobMeades 0:5cffef3371f6 657 } else if ((dev == DEV_SARA_U2) || (dev == DEV_SARA_G35)) {
RobMeades 0:5cffef3371f6 658 success = _at->send("AT+UGPIOC=16,2") && _at->recv("OK");
RobMeades 0:5cffef3371f6 659 } else {
RobMeades 0:5cffef3371f6 660 success = true;
RobMeades 0:5cffef3371f6 661 }
RobMeades 0:5cffef3371f6 662
RobMeades 0:5cffef3371f6 663 UNLOCK();
RobMeades 0:5cffef3371f6 664 return success;
RobMeades 0:5cffef3371f6 665 }
RobMeades 0:5cffef3371f6 666
RobMeades 0:5cffef3371f6 667 // Get the SIM card going.
RobMeades 0:5cffef3371f6 668 bool UbloxCellularBase::initialise_sim_card()
RobMeades 0:5cffef3371f6 669 {
RobMeades 0:5cffef3371f6 670 bool success = false;
RobMeades 0:5cffef3371f6 671 int retry_count = 0;
RobMeades 0:5cffef3371f6 672 bool done = false;
RobMeades 0:5cffef3371f6 673 LOCK();
RobMeades 0:5cffef3371f6 674
RobMeades 0:5cffef3371f6 675 MBED_ASSERT(_at != NULL);
RobMeades 0:5cffef3371f6 676
RobMeades 0:5cffef3371f6 677 /* SIM initialisation may take a significant amount, so an error is
RobMeades 0:5cffef3371f6 678 * kind of expected. We should retry 10 times until we succeed or timeout. */
RobMeades 0:5cffef3371f6 679 for (retry_count = 0; !done && (retry_count < 10); retry_count++) {
RobMeades 0:5cffef3371f6 680 char pinstr[16];
RobMeades 0:5cffef3371f6 681
RobMeades 0:5cffef3371f6 682 if (_at->send("AT+CPIN?") && _at->recv("+CPIN: %15[^\n]\n", pinstr) &&
RobMeades 0:5cffef3371f6 683 _at->recv("OK")) {
RobMeades 0:5cffef3371f6 684 done = true;
RobMeades 0:5cffef3371f6 685 if (strcmp(pinstr, "SIM PIN") == 0) {
RobMeades 0:5cffef3371f6 686 _sim_pin_check_enabled = true;
RobMeades 0:5cffef3371f6 687 if (_at->send("AT+CPIN=\"%s\"", _pin)) {
RobMeades 0:5cffef3371f6 688 if (_at->recv("OK")) {
RobMeades 0:5cffef3371f6 689 tr_info("PIN correct");
RobMeades 0:5cffef3371f6 690 success = true;
RobMeades 0:5cffef3371f6 691 } else {
RobMeades 0:5cffef3371f6 692 tr_error("Incorrect PIN");
RobMeades 0:5cffef3371f6 693 }
RobMeades 0:5cffef3371f6 694 }
RobMeades 0:5cffef3371f6 695 } else if (strcmp(pinstr, "READY") == 0) {
RobMeades 0:5cffef3371f6 696 _sim_pin_check_enabled = false;
RobMeades 0:5cffef3371f6 697 tr_info("No PIN required");
RobMeades 0:5cffef3371f6 698 success = true;
RobMeades 0:5cffef3371f6 699 } else {
RobMeades 0:5cffef3371f6 700 tr_debug("Unexpected response from SIM: \"%s\"", pinstr);
RobMeades 0:5cffef3371f6 701 }
RobMeades 0:5cffef3371f6 702 }
RobMeades 0:5cffef3371f6 703
RobMeades 0:5cffef3371f6 704 /* wait for a second before retry */
RobMeades 0:5cffef3371f6 705 wait_ms(1000);
RobMeades 0:5cffef3371f6 706 }
RobMeades 0:5cffef3371f6 707
RobMeades 0:5cffef3371f6 708 if (done) {
RobMeades 0:5cffef3371f6 709 tr_info("SIM Ready.");
RobMeades 0:5cffef3371f6 710 } else {
RobMeades 0:5cffef3371f6 711 tr_error("SIM not ready.");
RobMeades 0:5cffef3371f6 712 }
RobMeades 0:5cffef3371f6 713
RobMeades 0:5cffef3371f6 714 UNLOCK();
RobMeades 0:5cffef3371f6 715 return success;
RobMeades 0:5cffef3371f6 716 }
RobMeades 0:5cffef3371f6 717
RobMeades 0:5cffef3371f6 718 /**********************************************************************
RobMeades 0:5cffef3371f6 719 * PUBLIC METHODS
RobMeades 0:5cffef3371f6 720 **********************************************************************/
RobMeades 0:5cffef3371f6 721
RobMeades 0:5cffef3371f6 722 // Initialise the modem.
RobMeades 0:5cffef3371f6 723 bool UbloxCellularBase::init(const char *pin)
RobMeades 0:5cffef3371f6 724 {
RobMeades 8:a15349fcab25 725 int x;
RobMeades 0:5cffef3371f6 726 MBED_ASSERT(_at != NULL);
RobMeades 0:5cffef3371f6 727
RobMeades 0:5cffef3371f6 728 if (!_modem_initialised) {
RobMeades 0:5cffef3371f6 729 if (power_up()) {
RobMeades 0:5cffef3371f6 730 tr_info("Modem Ready.");
RobMeades 0:5cffef3371f6 731 if (pin != NULL) {
RobMeades 0:5cffef3371f6 732 _pin = pin;
RobMeades 0:5cffef3371f6 733 }
RobMeades 0:5cffef3371f6 734 if (initialise_sim_card()) {
RobMeades 0:5cffef3371f6 735 if (set_device_identity(&_dev_info.dev) && // Set up device identity
rob.meades@u-blox.com 6:8fadf1e49487 736 device_init(_dev_info.dev)) {// Initialise this device
rob.meades@u-blox.com 6:8fadf1e49487 737 // Get the integrated circuit ID of the SIM
rob.meades@u-blox.com 6:8fadf1e49487 738 if (get_iccid()) {
rob.meades@u-blox.com 6:8fadf1e49487 739 // Try a few times to get the IMSI (since on some modems this can
rob.meades@u-blox.com 6:8fadf1e49487 740 // take a while to be retrieved, especially if a SIM PIN
rob.meades@u-blox.com 6:8fadf1e49487 741 // was set)
RobMeades 8:a15349fcab25 742 for (x = 0; (x < 3) && !get_imsi(); x++) {
rob.meades@u-blox.com 6:8fadf1e49487 743 wait_ms(1000);
rob.meades@u-blox.com 6:8fadf1e49487 744 }
rob.meades@u-blox.com 6:8fadf1e49487 745
RobMeades 8:a15349fcab25 746 if (x < 3) { // If we got the IMSI, can get the others
rob.meades@u-blox.com 6:8fadf1e49487 747 if (get_imei() && // Get international mobile equipment identifier
rob.meades@u-blox.com 6:8fadf1e49487 748 get_meid() && // Probably the same as the IMEI
rob.meades@u-blox.com 6:8fadf1e49487 749 set_sms()) { // And set up SMS
rob.meades@u-blox.com 6:8fadf1e49487 750 // The modem is initialised.
rob.meades@u-blox.com 6:8fadf1e49487 751 _modem_initialised = true;
rob.meades@u-blox.com 6:8fadf1e49487 752 }
rob.meades@u-blox.com 6:8fadf1e49487 753 }
rob.meades@u-blox.com 6:8fadf1e49487 754 }
RobMeades 0:5cffef3371f6 755 }
RobMeades 0:5cffef3371f6 756 }
RobMeades 0:5cffef3371f6 757 }
RobMeades 0:5cffef3371f6 758 }
RobMeades 0:5cffef3371f6 759
RobMeades 0:5cffef3371f6 760 return _modem_initialised;
RobMeades 0:5cffef3371f6 761 }
RobMeades 0:5cffef3371f6 762
RobMeades 0:5cffef3371f6 763 // Perform registration.
RobMeades 0:5cffef3371f6 764 bool UbloxCellularBase::nwk_registration()
RobMeades 0:5cffef3371f6 765 {
RobMeades 0:5cffef3371f6 766 bool atSuccess = false;
RobMeades 0:5cffef3371f6 767 bool registered = false;
RobMeades 0:5cffef3371f6 768 int status;
RobMeades 0:5cffef3371f6 769 int at_timeout;
RobMeades 0:5cffef3371f6 770 LOCK();
RobMeades 0:5cffef3371f6 771
RobMeades 0:5cffef3371f6 772 at_timeout = _at_timeout; // Has to be inside LOCK()s
RobMeades 0:5cffef3371f6 773
RobMeades 0:5cffef3371f6 774 MBED_ASSERT(_at != NULL);
RobMeades 0:5cffef3371f6 775
RobMeades 0:5cffef3371f6 776 if (!is_registered_psd() && !is_registered_csd() && !is_registered_eps()) {
RobMeades 0:5cffef3371f6 777 tr_info("Searching Network...");
RobMeades 0:5cffef3371f6 778 // Enable the packet switched and network registration unsolicited result codes
RobMeades 0:5cffef3371f6 779 if (_at->send("AT+CREG=1") && _at->recv("OK") &&
RobMeades 0:5cffef3371f6 780 _at->send("AT+CGREG=1") && _at->recv("OK")) {
RobMeades 0:5cffef3371f6 781 atSuccess = true;
RobMeades 0:5cffef3371f6 782 if (_at->send("AT+CEREG=1")) {
RobMeades 0:5cffef3371f6 783 _at->recv("OK");
RobMeades 0:5cffef3371f6 784 // Don't check return value as this works for LTE only
RobMeades 0:5cffef3371f6 785 }
RobMeades 0:5cffef3371f6 786
RobMeades 0:5cffef3371f6 787 if (atSuccess) {
RobMeades 0:5cffef3371f6 788 // See if we are already in automatic mode
RobMeades 0:5cffef3371f6 789 if (_at->send("AT+COPS?") && _at->recv("+COPS: %d", &status) &&
RobMeades 0:5cffef3371f6 790 _at->recv("OK")) {
RobMeades 0:5cffef3371f6 791 // If not, set it
RobMeades 0:5cffef3371f6 792 if (status != 0) {
RobMeades 0:5cffef3371f6 793 // Don't check return code here as there's not much
RobMeades 0:5cffef3371f6 794 // we can do if this fails.
RobMeades 0:5cffef3371f6 795 _at->send("AT+COPS=0") && _at->recv("OK");
RobMeades 0:5cffef3371f6 796 }
RobMeades 0:5cffef3371f6 797 }
RobMeades 0:5cffef3371f6 798
RobMeades 0:5cffef3371f6 799 // Query the registration status directly as well,
RobMeades 0:5cffef3371f6 800 // just in case
RobMeades 0:5cffef3371f6 801 if (_at->send("AT+CREG?") && _at->recv("OK")) {
RobMeades 0:5cffef3371f6 802 // Answer will be processed by URC
RobMeades 0:5cffef3371f6 803 }
RobMeades 0:5cffef3371f6 804 if (_at->send("AT+CGREG?") && _at->recv("OK")) {
RobMeades 0:5cffef3371f6 805 // Answer will be processed by URC
RobMeades 0:5cffef3371f6 806 }
RobMeades 0:5cffef3371f6 807 if (_at->send("AT+CEREG?")) {
RobMeades 0:5cffef3371f6 808 _at->recv("OK");
RobMeades 0:5cffef3371f6 809 // Don't check return value as this works for LTE only
RobMeades 0:5cffef3371f6 810 }
RobMeades 0:5cffef3371f6 811 }
RobMeades 0:5cffef3371f6 812 }
RobMeades 0:5cffef3371f6 813 // Wait for registration to succeed
RobMeades 0:5cffef3371f6 814 at_set_timeout(1000);
RobMeades 0:5cffef3371f6 815 for (int waitSeconds = 0; !registered && (waitSeconds < 180); waitSeconds++) {
RobMeades 0:5cffef3371f6 816 registered = is_registered_psd() || is_registered_csd() || is_registered_eps();
RobMeades 0:5cffef3371f6 817 _at->recv(UNNATURAL_STRING);
RobMeades 0:5cffef3371f6 818 }
RobMeades 0:5cffef3371f6 819 at_set_timeout(at_timeout);
RobMeades 0:5cffef3371f6 820
RobMeades 0:5cffef3371f6 821 if (registered) {
RobMeades 0:5cffef3371f6 822 // This should return quickly but sometimes the status field is not returned
RobMeades 0:5cffef3371f6 823 // so make the timeout short
RobMeades 0:5cffef3371f6 824 at_set_timeout(1000);
RobMeades 0:5cffef3371f6 825 if (_at->send("AT+COPS?") && _at->recv("+COPS: %*d,%*d,\"%*[^\"]\",%d\n", &status)) {
RobMeades 0:5cffef3371f6 826 set_rat(status);
RobMeades 0:5cffef3371f6 827 }
RobMeades 0:5cffef3371f6 828 at_set_timeout(at_timeout);
RobMeades 0:5cffef3371f6 829 }
RobMeades 0:5cffef3371f6 830 } else {
RobMeades 0:5cffef3371f6 831 registered = true;
RobMeades 0:5cffef3371f6 832 }
RobMeades 0:5cffef3371f6 833
RobMeades 0:5cffef3371f6 834 UNLOCK();
RobMeades 0:5cffef3371f6 835 return registered;
RobMeades 0:5cffef3371f6 836 }
RobMeades 0:5cffef3371f6 837
RobMeades 0:5cffef3371f6 838 bool UbloxCellularBase::is_registered_csd()
RobMeades 0:5cffef3371f6 839 {
RobMeades 0:5cffef3371f6 840 return (_dev_info.reg_status_csd == CSD_REGISTERED) ||
RobMeades 0:5cffef3371f6 841 (_dev_info.reg_status_csd == CSD_REGISTERED_ROAMING) ||
RobMeades 0:5cffef3371f6 842 (_dev_info.reg_status_csd == CSD_CSFB_NOT_PREFERRED);
RobMeades 0:5cffef3371f6 843 }
RobMeades 0:5cffef3371f6 844
RobMeades 0:5cffef3371f6 845 bool UbloxCellularBase::is_registered_psd()
RobMeades 0:5cffef3371f6 846 {
RobMeades 0:5cffef3371f6 847 return (_dev_info.reg_status_psd == PSD_REGISTERED) ||
RobMeades 0:5cffef3371f6 848 (_dev_info.reg_status_psd == PSD_REGISTERED_ROAMING);
RobMeades 0:5cffef3371f6 849 }
RobMeades 0:5cffef3371f6 850
RobMeades 0:5cffef3371f6 851 bool UbloxCellularBase::is_registered_eps()
RobMeades 0:5cffef3371f6 852 {
RobMeades 0:5cffef3371f6 853 return (_dev_info.reg_status_eps == EPS_REGISTERED) ||
RobMeades 0:5cffef3371f6 854 (_dev_info.reg_status_eps == EPS_REGISTERED_ROAMING);
RobMeades 0:5cffef3371f6 855 }
RobMeades 0:5cffef3371f6 856
RobMeades 0:5cffef3371f6 857 // Perform deregistration.
RobMeades 0:5cffef3371f6 858 bool UbloxCellularBase::nwk_deregistration()
RobMeades 0:5cffef3371f6 859 {
RobMeades 0:5cffef3371f6 860 bool success = false;
RobMeades 0:5cffef3371f6 861 LOCK();
RobMeades 0:5cffef3371f6 862
RobMeades 0:5cffef3371f6 863 MBED_ASSERT(_at != NULL);
RobMeades 0:5cffef3371f6 864
RobMeades 0:5cffef3371f6 865 if (_at->send("AT+COPS=2") && _at->recv("OK")) {
RobMeades 0:5cffef3371f6 866 _dev_info.reg_status_csd = CSD_NOT_REGISTERED_NOT_SEARCHING;
RobMeades 0:5cffef3371f6 867 _dev_info.reg_status_psd = PSD_NOT_REGISTERED_NOT_SEARCHING;
RobMeades 0:5cffef3371f6 868 _dev_info.reg_status_eps = EPS_NOT_REGISTERED_NOT_SEARCHING;
RobMeades 0:5cffef3371f6 869 success = true;
RobMeades 0:5cffef3371f6 870 }
RobMeades 0:5cffef3371f6 871
RobMeades 0:5cffef3371f6 872 UNLOCK();
RobMeades 0:5cffef3371f6 873 return success;
RobMeades 0:5cffef3371f6 874 }
RobMeades 0:5cffef3371f6 875
RobMeades 0:5cffef3371f6 876 // Put the modem into its lowest power state.
RobMeades 0:5cffef3371f6 877 void UbloxCellularBase::deinit()
RobMeades 0:5cffef3371f6 878 {
RobMeades 0:5cffef3371f6 879 power_down();
RobMeades 0:5cffef3371f6 880 _modem_initialised = false;
RobMeades 0:5cffef3371f6 881 }
RobMeades 0:5cffef3371f6 882
RobMeades 0:5cffef3371f6 883 // Set the PIN.
RobMeades 0:5cffef3371f6 884 void UbloxCellularBase::set_pin(const char *pin) {
RobMeades 0:5cffef3371f6 885 _pin = pin;
RobMeades 0:5cffef3371f6 886 }
RobMeades 0:5cffef3371f6 887
RobMeades 0:5cffef3371f6 888 // Enable or disable SIM pin checking.
rob.meades@u-blox.com 4:2e640a101db1 889 bool UbloxCellularBase::sim_pin_check_enable(bool enableNotDisable)
RobMeades 0:5cffef3371f6 890 {
RobMeades 0:5cffef3371f6 891 bool success = false;;
RobMeades 0:5cffef3371f6 892 LOCK();
RobMeades 0:5cffef3371f6 893
RobMeades 0:5cffef3371f6 894 MBED_ASSERT(_at != NULL);
RobMeades 0:5cffef3371f6 895
RobMeades 0:5cffef3371f6 896 if (_pin != NULL) {
RobMeades 0:5cffef3371f6 897 if (_sim_pin_check_enabled && !enableNotDisable) {
RobMeades 0:5cffef3371f6 898 // Disable the SIM lock
RobMeades 0:5cffef3371f6 899 if (_at->send("AT+CLCK=\"SC\",0,\"%s\"", _pin) && _at->recv("OK")) {
RobMeades 0:5cffef3371f6 900 _sim_pin_check_enabled = false;
RobMeades 0:5cffef3371f6 901 success = true;
RobMeades 0:5cffef3371f6 902 }
RobMeades 0:5cffef3371f6 903 } else if (!_sim_pin_check_enabled && enableNotDisable) {
RobMeades 0:5cffef3371f6 904 // Enable the SIM lock
RobMeades 0:5cffef3371f6 905 if (_at->send("AT+CLCK=\"SC\",1,\"%s\"", _pin) && _at->recv("OK")) {
RobMeades 0:5cffef3371f6 906 _sim_pin_check_enabled = true;
RobMeades 0:5cffef3371f6 907 success = true;
RobMeades 0:5cffef3371f6 908 }
RobMeades 0:5cffef3371f6 909 } else {
RobMeades 0:5cffef3371f6 910 success = true;
RobMeades 0:5cffef3371f6 911 }
RobMeades 0:5cffef3371f6 912 }
RobMeades 0:5cffef3371f6 913
RobMeades 0:5cffef3371f6 914 UNLOCK();
RobMeades 0:5cffef3371f6 915 return success;
RobMeades 0:5cffef3371f6 916 }
RobMeades 0:5cffef3371f6 917
RobMeades 0:5cffef3371f6 918 // Change the pin code for the SIM card.
RobMeades 0:5cffef3371f6 919 bool UbloxCellularBase::change_sim_pin(const char *pin)
RobMeades 0:5cffef3371f6 920 {
RobMeades 0:5cffef3371f6 921 bool success = false;;
RobMeades 0:5cffef3371f6 922 LOCK();
RobMeades 0:5cffef3371f6 923
RobMeades 0:5cffef3371f6 924 MBED_ASSERT(_at != NULL);
RobMeades 0:5cffef3371f6 925
RobMeades 0:5cffef3371f6 926 // Change the SIM pin
RobMeades 0:5cffef3371f6 927 if ((pin != NULL) && (_pin != NULL)) {
RobMeades 0:5cffef3371f6 928 if (_at->send("AT+CPWD=\"SC\",\"%s\",\"%s\"", _pin, pin) && _at->recv("OK")) {
RobMeades 0:5cffef3371f6 929 _pin = pin;
RobMeades 0:5cffef3371f6 930 success = true;
RobMeades 0:5cffef3371f6 931 }
RobMeades 0:5cffef3371f6 932 }
RobMeades 0:5cffef3371f6 933
RobMeades 0:5cffef3371f6 934 UNLOCK();
RobMeades 0:5cffef3371f6 935 return success;
RobMeades 0:5cffef3371f6 936 }
RobMeades 0:5cffef3371f6 937
RobMeades 0:5cffef3371f6 938 // End of File
RobMeades 0:5cffef3371f6 939