tester

Fork of GPRSInterface by -deleted-

Committer:
noahmilam
Date:
Fri Sep 25 03:35:58 2015 +0000
Revision:
10:698c04e25c6b
Parent:
8:180feb3ebe62
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lawliet 0:8ccbd963e74d 1 /*
lawliet 0:8ccbd963e74d 2 GPRS.cpp
lawliet 0:8ccbd963e74d 3 2014 Copyright (c) Seeed Technology Inc. All right reserved.
lawliet 0:8ccbd963e74d 4
lawliet 0:8ccbd963e74d 5 Author:lawliet zou(lawliet.zou@gmail.com)
lawliet 0:8ccbd963e74d 6 2014-2-24
lawliet 0:8ccbd963e74d 7
lawliet 0:8ccbd963e74d 8 This library is free software; you can redistribute it and/or
lawliet 0:8ccbd963e74d 9 modify it under the terms of the GNU Lesser General Public
lawliet 0:8ccbd963e74d 10 License as published by the Free Software Foundation; either
lawliet 0:8ccbd963e74d 11 version 2.1 of the License, or (at your option) any later version.
lawliet 0:8ccbd963e74d 12
lawliet 0:8ccbd963e74d 13 This library is distributed in the hope that it will be useful,
lawliet 0:8ccbd963e74d 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
lawliet 0:8ccbd963e74d 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
lawliet 0:8ccbd963e74d 16 Lesser General Public License for more details.
lawliet 0:8ccbd963e74d 17
lawliet 0:8ccbd963e74d 18 You should have received a copy of the GNU Lesser General Public
lawliet 0:8ccbd963e74d 19 License along with this library; if not, write to the Free Software
lawliet 0:8ccbd963e74d 20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
lawliet 0:8ccbd963e74d 21 */
lawliet 0:8ccbd963e74d 22
lawliet 0:8ccbd963e74d 23 #include "mbed.h"
lawliet 0:8ccbd963e74d 24 #include "GPRS.h"
lawliet 0:8ccbd963e74d 25
lawliet 0:8ccbd963e74d 26 GPRS* GPRS::inst;
lawliet 0:8ccbd963e74d 27
lawliet 0:8ccbd963e74d 28 GPRS::GPRS(PinName tx, PinName rx, int baudRate, const char* apn, const char* userName, const char* passWord) : Modem(tx,rx,baudRate)
lawliet 0:8ccbd963e74d 29 {
lawliet 0:8ccbd963e74d 30 inst = this;
lawliet 0:8ccbd963e74d 31 _apn = apn;
lawliet 0:8ccbd963e74d 32 _userName = userName;
lawliet 0:8ccbd963e74d 33 _passWord = passWord;
lawliet 0:8ccbd963e74d 34 socketID = -1;
lawliet 0:8ccbd963e74d 35 }
lawliet 0:8ccbd963e74d 36
lawliet 0:8ccbd963e74d 37 bool GPRS::preInit()
lawliet 0:8ccbd963e74d 38 {
lawliet 0:8ccbd963e74d 39 for(int i = 0; i < 2; i++) {
lawliet 0:8ccbd963e74d 40 sendCmd("AT\r\n");
lawliet 0:8ccbd963e74d 41 wait(1);
lawliet 0:8ccbd963e74d 42 }
lawliet 0:8ccbd963e74d 43 return checkSIMStatus();
lawliet 0:8ccbd963e74d 44 }
noahmilam 10:698c04e25c6b 45 // added by Noah Milam
noahmilam 10:698c04e25c6b 46 void GPRS::start_server()
noahmilam 10:698c04e25c6b 47 {
noahmilam 10:698c04e25c6b 48 sendCmdResp("AT+CGATT?\r\n");
noahmilam 10:698c04e25c6b 49
noahmilam 10:698c04e25c6b 50 sendCmdResp("AT+CIPSERVER=1,\"1234\"\r\n");
noahmilam 10:698c04e25c6b 51 wait_for_sms();
noahmilam 10:698c04e25c6b 52 store_response();
noahmilam 10:698c04e25c6b 53 listen_server();
noahmilam 10:698c04e25c6b 54 }
noahmilam 10:698c04e25c6b 55 void GPRS::listen_server()
noahmilam 10:698c04e25c6b 56 {
noahmilam 10:698c04e25c6b 57 gprs_response();
noahmilam 10:698c04e25c6b 58 }
noahmilam 10:698c04e25c6b 59
noahmilam 10:698c04e25c6b 60 void GPRS::send_SMS(char* IPAdress)
noahmilam 10:698c04e25c6b 61 {
noahmilam 10:698c04e25c6b 62 printf("sending at command\n");
noahmilam 10:698c04e25c6b 63 sendCmdAndWaitForResp("AT+CMGF=1\r\n","OK",DEFAULT_TIMEOUT,CMD);
noahmilam 10:698c04e25c6b 64 wait(2);
noahmilam 10:698c04e25c6b 65 //printf("\032\n");
noahmilam 10:698c04e25c6b 66 sendCmdAndWaitForResp("AT+CMGS=\"+14358306480\"\r\n",">",DEFAULT_TIMEOUT,CMD);
noahmilam 10:698c04e25c6b 67 printf("sent at cmgf\n");
noahmilam 10:698c04e25c6b 68 wait(2);
noahmilam 10:698c04e25c6b 69 //printf("032");
noahmilam 10:698c04e25c6b 70 sendCmd(IPAdress); // sends the address
noahmilam 10:698c04e25c6b 71 sendCmd("\x1a"); // this is like pressing control - z to end the send command
noahmilam 10:698c04e25c6b 72 wait(10); // giving the send enough time to do its thing
noahmilam 10:698c04e25c6b 73 printf("should have been received");
noahmilam 10:698c04e25c6b 74 }
lawliet 0:8ccbd963e74d 75
noahmilam 10:698c04e25c6b 76 void GPRS::read_SMS()
noahmilam 10:698c04e25c6b 77 {
noahmilam 10:698c04e25c6b 78 wait(1);
noahmilam 10:698c04e25c6b 79 sendCmd("AT+CMGF=1\r\n");
noahmilam 10:698c04e25c6b 80 wait(2);
noahmilam 10:698c04e25c6b 81 sendCmd("AT+CMGL=\"ALL\"\r\n");
noahmilam 10:698c04e25c6b 82 store_response();
noahmilam 10:698c04e25c6b 83 }
noahmilam 10:698c04e25c6b 84
noahmilam 10:698c04e25c6b 85 // end of what Noah Milam added
lawliet 0:8ccbd963e74d 86 bool GPRS::checkSIMStatus(void)
lawliet 0:8ccbd963e74d 87 {
lawliet 0:8ccbd963e74d 88 char gprsBuffer[32];
lawliet 0:8ccbd963e74d 89 int count = 0;
lawliet 0:8ccbd963e74d 90 cleanBuffer(gprsBuffer,32);
lawliet 0:8ccbd963e74d 91 while(count < 3) {
lawliet 0:8ccbd963e74d 92 sendCmd("AT+CPIN?\r\n");
lawliet 0:8ccbd963e74d 93 readBuffer(gprsBuffer,32,DEFAULT_TIMEOUT);
lawliet 0:8ccbd963e74d 94 if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) {
lawliet 0:8ccbd963e74d 95 break;
lawliet 0:8ccbd963e74d 96 }
lawliet 0:8ccbd963e74d 97 count++;
lawliet 0:8ccbd963e74d 98 wait(1);
lawliet 0:8ccbd963e74d 99 }
lawliet 0:8ccbd963e74d 100 if(count == 3) {
lawliet 0:8ccbd963e74d 101 return false;
lawliet 0:8ccbd963e74d 102 }
lawliet 0:8ccbd963e74d 103 return true;
lawliet 0:8ccbd963e74d 104 }
lawliet 0:8ccbd963e74d 105
lawliet 0:8ccbd963e74d 106 bool GPRS::join()
lawliet 0:8ccbd963e74d 107 {
lawliet 0:8ccbd963e74d 108 char cmd[64];
lawliet 0:8ccbd963e74d 109 char ipAddr[32];
lawliet 0:8ccbd963e74d 110 //Select multiple connection
lawliet 0:8ccbd963e74d 111 sendCmdAndWaitForResp("AT+CIPMUX=1\r\n","OK",DEFAULT_TIMEOUT,CMD);
lawliet 0:8ccbd963e74d 112
lawliet 0:8ccbd963e74d 113 //set APN
lawliet 0:8ccbd963e74d 114 snprintf(cmd,sizeof(cmd),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",_apn,_userName,_passWord);
lawliet 0:8ccbd963e74d 115 sendCmdAndWaitForResp(cmd, "OK", DEFAULT_TIMEOUT,CMD);
lawliet 0:8ccbd963e74d 116
lawliet 0:8ccbd963e74d 117 //Brings up wireless connection
lawliet 0:8ccbd963e74d 118 sendCmdAndWaitForResp("AT+CIICR\r\n","OK",DEFAULT_TIMEOUT,CMD);
lawliet 0:8ccbd963e74d 119
lawliet 0:8ccbd963e74d 120 //Get local IP address
lawliet 0:8ccbd963e74d 121 sendCmd("AT+CIFSR\r\n");
lawliet 0:8ccbd963e74d 122 readBuffer(ipAddr,32,2);
lawliet 0:8ccbd963e74d 123
lawliet 0:8ccbd963e74d 124 if(NULL != strstr(ipAddr,"AT+CIFSR")) {
lawliet 0:8ccbd963e74d 125 _ip = str_to_ip(ipAddr+12);
lawliet 0:8ccbd963e74d 126 if(_ip != 0) {
lawliet 0:8ccbd963e74d 127 return true;
lawliet 0:8ccbd963e74d 128 }
lawliet 0:8ccbd963e74d 129 }
lawliet 0:8ccbd963e74d 130 return false;
lawliet 0:8ccbd963e74d 131 }
lawliet 0:8ccbd963e74d 132
lawliet 0:8ccbd963e74d 133 bool GPRS::setProtocol(int socket, Protocol p)
lawliet 0:8ccbd963e74d 134 {
lawliet 0:8ccbd963e74d 135 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 136 return false;
lawliet 0:8ccbd963e74d 137 }
lawliet 0:8ccbd963e74d 138 //ToDo: setProtocol
lawliet 0:8ccbd963e74d 139 return true;
lawliet 0:8ccbd963e74d 140 }
lawliet 0:8ccbd963e74d 141
lawliet 0:8ccbd963e74d 142 bool GPRS::connect(int socket, Protocol ptl,const char * host, int port, int timeout)
lawliet 0:8ccbd963e74d 143 {
lawliet 0:8ccbd963e74d 144 char cmd[64];
lawliet 0:8ccbd963e74d 145 char resp[96];
lawliet 0:8ccbd963e74d 146 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 147 return false;
lawliet 0:8ccbd963e74d 148 }
lawliet 0:8ccbd963e74d 149 if(ptl == TCP) {
lawliet 0:8ccbd963e74d 150 sprintf(cmd, "AT+CIPSTART=%d,\"TCP\",\"%s\",%d\r\n",socket, host, port);
lawliet 0:8ccbd963e74d 151 } else if(ptl == UDP) {
lawliet 0:8ccbd963e74d 152 sprintf(cmd, "AT+CIPSTART=%d,\"UDP\",\"%s\",%d\r\n",socket, host, port);
lawliet 0:8ccbd963e74d 153 } else {
lawliet 0:8ccbd963e74d 154 return false;
lawliet 0:8ccbd963e74d 155 }
lawliet 0:8ccbd963e74d 156 sendCmd(cmd);
lawliet 0:8ccbd963e74d 157 readBuffer(resp,96,2*DEFAULT_TIMEOUT);
lawliet 0:8ccbd963e74d 158 if(NULL != strstr(resp,"CONNECT")) { //ALREADY CONNECT or CONNECT OK
lawliet 0:8ccbd963e74d 159 return true;
lawliet 0:8ccbd963e74d 160 }
lawliet 0:8ccbd963e74d 161 return false;//ERROR
lawliet 0:8ccbd963e74d 162 }
lawliet 0:8ccbd963e74d 163
lawliet 0:8ccbd963e74d 164 bool GPRS::gethostbyname(const char* host, uint32_t* ip)
lawliet 0:8ccbd963e74d 165 {
lawliet 0:8ccbd963e74d 166 uint32_t addr = str_to_ip(host);
lawliet 0:8ccbd963e74d 167 char buf[17];
lawliet 0:8ccbd963e74d 168 snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
lawliet 0:8ccbd963e74d 169 if (strcmp(buf, host) == 0) {
lawliet 0:8ccbd963e74d 170 *ip = addr;
lawliet 0:8ccbd963e74d 171 return true;
lawliet 0:8ccbd963e74d 172 }
lawliet 0:8ccbd963e74d 173 return false;
lawliet 0:8ccbd963e74d 174 }
lawliet 0:8ccbd963e74d 175
lawliet 0:8ccbd963e74d 176 bool GPRS::disconnect()
lawliet 0:8ccbd963e74d 177 {
lawliet 0:8ccbd963e74d 178 sendCmd("AT+CIPSHUT\r\n");
lawliet 0:8ccbd963e74d 179 return true;
lawliet 0:8ccbd963e74d 180 }
lawliet 0:8ccbd963e74d 181
lawliet 0:8ccbd963e74d 182 bool GPRS::is_connected(int socket)
lawliet 0:8ccbd963e74d 183 {
lawliet 0:8ccbd963e74d 184 char cmd[16];
lawliet 3:acf2ea413e72 185 char resp[96];
lawliet 3:acf2ea413e72 186 snprintf(cmd,16,"AT+CIPSTATUS=%d\r\n",socket);
lawliet 0:8ccbd963e74d 187 sendCmd(cmd);
lawliet 0:8ccbd963e74d 188 readBuffer(resp,sizeof(resp),DEFAULT_TIMEOUT);
lawliet 0:8ccbd963e74d 189 if(NULL != strstr(resp,"CONNECTED")) {
lawliet 0:8ccbd963e74d 190 //+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CONNECTED"
lawliet 0:8ccbd963e74d 191 return true;
lawliet 0:8ccbd963e74d 192 } else {
lawliet 0:8ccbd963e74d 193 //+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CLOSED"
lawliet 0:8ccbd963e74d 194 //+CIPSTATUS: 0,,"","","","INITIAL"
lawliet 0:8ccbd963e74d 195 return false;
lawliet 0:8ccbd963e74d 196 }
lawliet 0:8ccbd963e74d 197 }
lawliet 0:8ccbd963e74d 198
lawliet 0:8ccbd963e74d 199 void GPRS::reset()
lawliet 0:8ccbd963e74d 200 {
lawliet 0:8ccbd963e74d 201
lawliet 0:8ccbd963e74d 202 }
lawliet 0:8ccbd963e74d 203
lawliet 0:8ccbd963e74d 204 bool GPRS::close(int socket)
lawliet 0:8ccbd963e74d 205 {
lawliet 0:8ccbd963e74d 206 char cmd[16];
lawliet 0:8ccbd963e74d 207 char resp[16];
lawliet 0:8ccbd963e74d 208
lawliet 0:8ccbd963e74d 209 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 210 return false;
lawliet 0:8ccbd963e74d 211 }
lawliet 0:8ccbd963e74d 212 // if not connected, return
lawliet 0:8ccbd963e74d 213 if (is_connected(socket) == false) {
lawliet 0:8ccbd963e74d 214 return true;
lawliet 0:8ccbd963e74d 215 }
lawliet 4:1142bdd07989 216 snprintf(cmd, sizeof(cmd),"AT+CIPCLOSE=%d\r\n",socket);
lawliet 0:8ccbd963e74d 217 snprintf(resp,sizeof(resp),"%d, CLOSE OK",socket);
lawliet 0:8ccbd963e74d 218 if(0 != sendCmdAndWaitForResp(cmd, resp, DEFAULT_TIMEOUT,CMD)) {
lawliet 0:8ccbd963e74d 219 return false;
lawliet 0:8ccbd963e74d 220 }
lawliet 0:8ccbd963e74d 221 return true;
lawliet 0:8ccbd963e74d 222 }
lawliet 0:8ccbd963e74d 223
lawliet 0:8ccbd963e74d 224 bool GPRS::readable(void)
lawliet 0:8ccbd963e74d 225 {
lawliet 0:8ccbd963e74d 226 return readable();
lawliet 0:8ccbd963e74d 227 }
lawliet 0:8ccbd963e74d 228
lawliet 0:8ccbd963e74d 229 int GPRS::wait_readable(int socket, int wait_time)
lawliet 0:8ccbd963e74d 230 {
lawliet 0:8ccbd963e74d 231 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 232 return -1;
lawliet 0:8ccbd963e74d 233 }
lawliet 0:8ccbd963e74d 234 char resp[16];
lawliet 0:8ccbd963e74d 235 snprintf(resp,sizeof(resp),"\r\n\r\n+RECEIVE,%d",socket);//"+RECEIVE:<socketID>,<length>"
lawliet 0:8ccbd963e74d 236 int len = strlen(resp);
lawliet 0:8ccbd963e74d 237
lawliet 0:8ccbd963e74d 238 if(false == respCmp(resp,len,wait_time)) {
lawliet 0:8ccbd963e74d 239 return -1;
lawliet 0:8ccbd963e74d 240 }
lawliet 0:8ccbd963e74d 241 char c = readByte();//','
lawliet 0:8ccbd963e74d 242 char dataLen[4];
lawliet 0:8ccbd963e74d 243 int i = 0;
lawliet 0:8ccbd963e74d 244 c = readByte();
lawliet 0:8ccbd963e74d 245 while((c >= '0') && (c <= '9')) {
lawliet 0:8ccbd963e74d 246 dataLen[i++] = c;
lawliet 0:8ccbd963e74d 247 c = readByte();
lawliet 0:8ccbd963e74d 248 }
lawliet 0:8ccbd963e74d 249 c = readByte();//'\n'
lawliet 0:8ccbd963e74d 250 len = atoi(dataLen);
lawliet 0:8ccbd963e74d 251 return len;
lawliet 0:8ccbd963e74d 252 }
lawliet 0:8ccbd963e74d 253
lawliet 0:8ccbd963e74d 254 int GPRS::wait_writeable(int socket, int req_size)
lawliet 0:8ccbd963e74d 255 {
lawliet 0:8ccbd963e74d 256 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 257 return -1;
lawliet 0:8ccbd963e74d 258 }
lawliet 0:8ccbd963e74d 259 return req_size>256?256:req_size+1;
lawliet 0:8ccbd963e74d 260 }
lawliet 0:8ccbd963e74d 261
lawliet 0:8ccbd963e74d 262 int GPRS::send(int socket, const char * str, int len)
lawliet 0:8ccbd963e74d 263 {
lawliet 0:8ccbd963e74d 264 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 265 return -1;
lawliet 0:8ccbd963e74d 266 }
lawliet 0:8ccbd963e74d 267 char cmd[32];
lawliet 0:8ccbd963e74d 268 wait(1);
lawliet 3:acf2ea413e72 269 if(len > 0){
lawliet 8:180feb3ebe62 270 snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",socket);
lawliet 8:180feb3ebe62 271 if(0 != sendCmdAndWaitForResp(cmd,">",2,CMD)) {
lawliet 3:acf2ea413e72 272 return false;
lawliet 3:acf2ea413e72 273 }
lawliet 8:180feb3ebe62 274 sendCmd(str);
lawliet 8:180feb3ebe62 275 serialModem.putc((char)0x1a);
lawliet 0:8ccbd963e74d 276 }
lawliet 0:8ccbd963e74d 277 return len;
lawliet 0:8ccbd963e74d 278 }
lawliet 0:8ccbd963e74d 279
lawliet 0:8ccbd963e74d 280 int GPRS::recv(int socket, char* buf, int len)
lawliet 0:8ccbd963e74d 281 {
lawliet 0:8ccbd963e74d 282 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 283 return -1;
lawliet 0:8ccbd963e74d 284 }
lawliet 0:8ccbd963e74d 285 cleanBuffer(buf,len);
lawliet 0:8ccbd963e74d 286 readBuffer(buf,len,DEFAULT_TIMEOUT/2);
lawliet 0:8ccbd963e74d 287 return len;
lawliet 0:8ccbd963e74d 288 //return strlen(buf);
lawliet 0:8ccbd963e74d 289 }
lawliet 0:8ccbd963e74d 290
lawliet 0:8ccbd963e74d 291 int GPRS::new_socket()
lawliet 0:8ccbd963e74d 292 {
lawliet 7:1bdcfd6da2d0 293 socketID = 0; //we only support one socket.
lawliet 7:1bdcfd6da2d0 294 return socketID;
lawliet 0:8ccbd963e74d 295 }
lawliet 0:8ccbd963e74d 296
lawliet 0:8ccbd963e74d 297 uint16_t GPRS::new_port()
lawliet 0:8ccbd963e74d 298 {
lawliet 0:8ccbd963e74d 299 uint16_t port = rand();
lawliet 0:8ccbd963e74d 300 port |= 49152;
lawliet 0:8ccbd963e74d 301 return port;
lawliet 0:8ccbd963e74d 302 }
lawliet 0:8ccbd963e74d 303
lawliet 0:8ccbd963e74d 304 uint32_t GPRS::str_to_ip(const char* str)
lawliet 0:8ccbd963e74d 305 {
lawliet 0:8ccbd963e74d 306 uint32_t ip = 0;
lawliet 0:8ccbd963e74d 307 char* p = (char*)str;
lawliet 0:8ccbd963e74d 308 for(int i = 0; i < 4; i++) {
lawliet 0:8ccbd963e74d 309 ip |= atoi(p);
lawliet 0:8ccbd963e74d 310 p = strchr(p, '.');
lawliet 0:8ccbd963e74d 311 if (p == NULL) {
lawliet 0:8ccbd963e74d 312 break;
lawliet 0:8ccbd963e74d 313 }
lawliet 0:8ccbd963e74d 314 ip <<= 8;
lawliet 0:8ccbd963e74d 315 p++;
lawliet 0:8ccbd963e74d 316 }
lawliet 0:8ccbd963e74d 317 return ip;
lawliet 0:8ccbd963e74d 318 }