[136] | 1 | /*
|
---|
| 2 | This file is part of the GSM3 communications library for Arduino
|
---|
| 3 | -- Multi-transport communications platform
|
---|
| 4 | -- Fully asynchronous
|
---|
| 5 | -- Includes code for the Arduino-Telefonica GSM/GPRS Shield V1
|
---|
| 6 | -- Voice calls
|
---|
| 7 | -- SMS
|
---|
| 8 | -- TCP/IP connections
|
---|
| 9 | -- HTTP basic clients
|
---|
| 10 |
|
---|
| 11 | This library has been developed by Telefónica Digital - PDI -
|
---|
| 12 | - Physical Internet Lab, as part as its collaboration with
|
---|
| 13 | Arduino and the Open Hardware Community.
|
---|
| 14 |
|
---|
| 15 | September-December 2012
|
---|
| 16 |
|
---|
| 17 | This library is free software; you can redistribute it and/or
|
---|
| 18 | modify it under the terms of the GNU Lesser General Public
|
---|
| 19 | License as published by the Free Software Foundation; either
|
---|
| 20 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 21 |
|
---|
| 22 | This library is distributed in the hope that it will be useful,
|
---|
| 23 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 25 | Lesser General Public License for more details.
|
---|
| 26 |
|
---|
| 27 | You should have received a copy of the GNU Lesser General Public
|
---|
| 28 | License along with this library; if not, write to the Free Software
|
---|
| 29 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
| 30 |
|
---|
| 31 | The latest version of this library can always be found at
|
---|
| 32 | https://github.com/BlueVia/Official-Arduino
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <GSM3ShieldV1ScanNetworks.h>
|
---|
| 36 |
|
---|
| 37 | GSM3ShieldV1ScanNetworks::GSM3ShieldV1ScanNetworks(bool trace): modem(trace)
|
---|
| 38 | {
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | GSM3_NetworkStatus_t GSM3ShieldV1ScanNetworks::begin()
|
---|
| 42 | {
|
---|
| 43 | modem.begin();
|
---|
| 44 | modem.restartModem();
|
---|
| 45 | // check modem response
|
---|
| 46 | modem.writeModemCommand("AT", 1000);
|
---|
| 47 | modem.writeModemCommand("ATE0", 1000);
|
---|
| 48 | return IDLE;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | String GSM3ShieldV1ScanNetworks::getCurrentCarrier()
|
---|
| 52 | {
|
---|
| 53 | String modemResponse = modem.writeModemCommand("AT+COPS?", 2000);
|
---|
| 54 |
|
---|
| 55 | // Parse and check response
|
---|
| 56 | char res_to_split[modemResponse.length()];
|
---|
| 57 | modemResponse.toCharArray(res_to_split, modemResponse.length());
|
---|
| 58 | if(strstr(res_to_split,"ERROR") == NULL){
|
---|
| 59 | // Tokenizer
|
---|
| 60 | char *ptr_token;
|
---|
| 61 | ptr_token = strtok(res_to_split, "\"");
|
---|
| 62 | ptr_token = strtok(NULL, "\"");
|
---|
| 63 | String final_result = ptr_token;
|
---|
| 64 | return final_result;
|
---|
| 65 | }else{
|
---|
| 66 | return String(NULL);
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | String GSM3ShieldV1ScanNetworks::getSignalStrength()
|
---|
| 71 | {
|
---|
| 72 | String modemResponse = modem.writeModemCommand("AT+CSQ", 2000);
|
---|
| 73 | char res_to_split[modemResponse.length()];
|
---|
| 74 | modemResponse.toCharArray(res_to_split, modemResponse.length());
|
---|
| 75 | if((strstr(res_to_split,"ERROR") == NULL) | (strstr(res_to_split,"99") == NULL)){
|
---|
| 76 | // Tokenizer
|
---|
| 77 | char *ptr_token;
|
---|
| 78 | ptr_token = strtok(res_to_split, ":");
|
---|
| 79 | ptr_token = strtok(NULL, ":");
|
---|
| 80 | ptr_token = strtok(ptr_token, ",");
|
---|
| 81 | String final_result = ptr_token;
|
---|
| 82 | final_result.trim();
|
---|
| 83 | return final_result;
|
---|
| 84 | }else{
|
---|
| 85 | return String(NULL);
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | String GSM3ShieldV1ScanNetworks::readNetworks()
|
---|
| 90 | {
|
---|
| 91 | String modemResponse = modem.writeModemCommand("AT+COPS=?",20000);
|
---|
| 92 | String result;
|
---|
| 93 | bool inQuotes=false;
|
---|
| 94 | int quoteCounter=0;
|
---|
| 95 | for(unsigned int i=0; i<modemResponse.length();i++)
|
---|
| 96 | {
|
---|
| 97 | if(modemResponse[i]=='"')
|
---|
| 98 | {
|
---|
| 99 | if(!inQuotes)
|
---|
| 100 | {
|
---|
| 101 | inQuotes=true;
|
---|
| 102 | quoteCounter++;
|
---|
| 103 | if(quoteCounter==1)
|
---|
| 104 | result+="> ";
|
---|
| 105 | }
|
---|
| 106 | else
|
---|
| 107 | {
|
---|
| 108 | inQuotes=false;
|
---|
| 109 | if(quoteCounter==3)
|
---|
| 110 | quoteCounter=0;
|
---|
| 111 | if(quoteCounter==1)
|
---|
| 112 | result+="\n";
|
---|
| 113 |
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | else
|
---|
| 117 | {
|
---|
| 118 | if(inQuotes&&(quoteCounter==1))
|
---|
| 119 | {
|
---|
| 120 | result+=modemResponse[i];
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | return result;
|
---|
| 125 | }
|
---|
| 126 |
|
---|