Testing the module's internet connection (spoiler: it doesn't work) WARNING: this code has been written in a hurry during an hackathon. It's total crap.

Dependencies:   GPS_CanSat mbed

Testing the module's internet connection (spoiler: it doesn't work) WARNING: this code has been written in a hurry during an hackathon. It's total crap.

Committer:
gipmad
Date:
Sat Sep 19 14:47:39 2015 +0000
Revision:
1:180ed9f09775
Parent:
0:8f9b472ff818
-

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gipmad 0:8f9b472ff818 1 /*
gipmad 0:8f9b472ff818 2 modem.cpp
gipmad 0:8f9b472ff818 3 2014 Copyright (c) Seeed Technology Inc. All right reserved.
gipmad 0:8f9b472ff818 4
gipmad 0:8f9b472ff818 5 Author:lawliet zou(lawliet.zou@gmail.com)
gipmad 0:8f9b472ff818 6 2014-2-24
gipmad 0:8f9b472ff818 7
gipmad 0:8f9b472ff818 8 This library is free software; you can redistribute it and/or
gipmad 0:8f9b472ff818 9 modify it under the terms of the GNU Lesser General Public
gipmad 0:8f9b472ff818 10 License as published by the Free Software Foundation; either
gipmad 0:8f9b472ff818 11 version 2.1 of the License, or (at your option) any later version.
gipmad 0:8f9b472ff818 12
gipmad 0:8f9b472ff818 13 This library is distributed in the hope that it will be useful,
gipmad 0:8f9b472ff818 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
gipmad 0:8f9b472ff818 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
gipmad 0:8f9b472ff818 16 Lesser General Public License for more details.
gipmad 0:8f9b472ff818 17
gipmad 0:8f9b472ff818 18 You should have received a copy of the GNU Lesser General Public
gipmad 0:8f9b472ff818 19 License along with this library; if not, write to the Free Software
gipmad 0:8f9b472ff818 20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
gipmad 0:8f9b472ff818 21 */
gipmad 0:8f9b472ff818 22
gipmad 0:8f9b472ff818 23 #include "modem.h"
gipmad 0:8f9b472ff818 24
gipmad 0:8f9b472ff818 25 char Modem::readByte(void)
gipmad 0:8f9b472ff818 26 {
gipmad 0:8f9b472ff818 27 return serialModem.getc();
gipmad 0:8f9b472ff818 28 }
gipmad 0:8f9b472ff818 29
gipmad 0:8f9b472ff818 30 bool Modem::readable()
gipmad 0:8f9b472ff818 31 {
gipmad 0:8f9b472ff818 32 return serialModem.readable();
gipmad 0:8f9b472ff818 33 }
gipmad 0:8f9b472ff818 34
gipmad 0:8f9b472ff818 35 int Modem::readBuffer(char *buffer,int count, unsigned int timeOut)
gipmad 0:8f9b472ff818 36 {
gipmad 0:8f9b472ff818 37 int i = 0;
gipmad 0:8f9b472ff818 38 timeCnt.start();
gipmad 0:8f9b472ff818 39 while(1) {
gipmad 0:8f9b472ff818 40 while (serialModem.readable()) {
gipmad 0:8f9b472ff818 41 char c = serialModem.getc();
gipmad 0:8f9b472ff818 42 buffer[i++] = c;
gipmad 0:8f9b472ff818 43 if(i >= count)break;
gipmad 0:8f9b472ff818 44 }
gipmad 0:8f9b472ff818 45 if(i >= count)break;
gipmad 0:8f9b472ff818 46 if(timeCnt.read() > timeOut) {
gipmad 0:8f9b472ff818 47 timeCnt.stop();
gipmad 0:8f9b472ff818 48 timeCnt.reset();
gipmad 0:8f9b472ff818 49 break;
gipmad 0:8f9b472ff818 50 }
gipmad 0:8f9b472ff818 51 }
gipmad 0:8f9b472ff818 52 return 0;
gipmad 0:8f9b472ff818 53 }
gipmad 0:8f9b472ff818 54
gipmad 0:8f9b472ff818 55 void Modem::cleanBuffer(char *buffer, int count)
gipmad 0:8f9b472ff818 56 {
gipmad 0:8f9b472ff818 57 for(int i=0; i < count; i++) {
gipmad 0:8f9b472ff818 58 buffer[i] = '\0';
gipmad 0:8f9b472ff818 59 }
gipmad 0:8f9b472ff818 60 }
gipmad 0:8f9b472ff818 61
gipmad 0:8f9b472ff818 62 void Modem::sendCmd(const char* cmd)
gipmad 0:8f9b472ff818 63 {
gipmad 0:8f9b472ff818 64 serialModem.puts(cmd);
gipmad 0:8f9b472ff818 65 }
gipmad 0:8f9b472ff818 66
gipmad 0:8f9b472ff818 67 void Modem::sendATTest(void)
gipmad 0:8f9b472ff818 68 {
gipmad 0:8f9b472ff818 69 sendCmdAndWaitForResp("AT\r\n","OK",DEFAULT_TIMEOUT,CMD);
gipmad 0:8f9b472ff818 70 }
gipmad 0:8f9b472ff818 71
gipmad 0:8f9b472ff818 72 bool Modem::respCmp(const char *resp, unsigned int len, unsigned int timeout)
gipmad 0:8f9b472ff818 73 {
gipmad 0:8f9b472ff818 74 int sum=0;
gipmad 0:8f9b472ff818 75 timeCnt.start();
gipmad 0:8f9b472ff818 76
gipmad 0:8f9b472ff818 77 while(1) {
gipmad 0:8f9b472ff818 78 if(serialModem.readable()) {
gipmad 0:8f9b472ff818 79 char c = serialModem.getc();
gipmad 0:8f9b472ff818 80 sum = (c==resp[sum]) ? sum+1 : 0;
gipmad 0:8f9b472ff818 81 if(sum == len)break;
gipmad 0:8f9b472ff818 82 }
gipmad 0:8f9b472ff818 83 if(timeCnt.read() > timeout) {
gipmad 0:8f9b472ff818 84 timeCnt.stop();
gipmad 0:8f9b472ff818 85 timeCnt.reset();
gipmad 0:8f9b472ff818 86 return false;
gipmad 0:8f9b472ff818 87 }
gipmad 0:8f9b472ff818 88 }
gipmad 0:8f9b472ff818 89 timeCnt.stop();
gipmad 0:8f9b472ff818 90 timeCnt.reset();
gipmad 0:8f9b472ff818 91
gipmad 0:8f9b472ff818 92 return true;
gipmad 0:8f9b472ff818 93 }
gipmad 0:8f9b472ff818 94
gipmad 0:8f9b472ff818 95 int Modem::waitForResp(const char *resp, unsigned int timeout,DataType type)
gipmad 0:8f9b472ff818 96 {
gipmad 0:8f9b472ff818 97 int len = strlen(resp);
gipmad 0:8f9b472ff818 98 int sum=0;
gipmad 0:8f9b472ff818 99 timeCnt.start();
gipmad 0:8f9b472ff818 100
gipmad 0:8f9b472ff818 101 while(1) {
gipmad 0:8f9b472ff818 102 if(serialModem.readable()) {
gipmad 0:8f9b472ff818 103 char c = serialModem.getc();
gipmad 0:8f9b472ff818 104
gipmad 0:8f9b472ff818 105
gipmad 0:8f9b472ff818 106 pc.putc(c);
gipmad 0:8f9b472ff818 107
gipmad 0:8f9b472ff818 108
gipmad 0:8f9b472ff818 109 sum = (c==resp[sum]) ? sum+1 : 0;
gipmad 0:8f9b472ff818 110 if(sum == len)break;
gipmad 0:8f9b472ff818 111 }
gipmad 0:8f9b472ff818 112 if(timeCnt.read() > timeout) {
gipmad 0:8f9b472ff818 113 timeCnt.stop();
gipmad 0:8f9b472ff818 114 timeCnt.reset();
gipmad 0:8f9b472ff818 115 return -1;
gipmad 0:8f9b472ff818 116 }
gipmad 0:8f9b472ff818 117 }
gipmad 0:8f9b472ff818 118 timeCnt.stop();
gipmad 0:8f9b472ff818 119 timeCnt.reset();
gipmad 0:8f9b472ff818 120
gipmad 0:8f9b472ff818 121 if(type == CMD) {
gipmad 0:8f9b472ff818 122 while(serialModem.readable()) {
gipmad 0:8f9b472ff818 123 char c = serialModem.getc();
gipmad 0:8f9b472ff818 124 }
gipmad 0:8f9b472ff818 125 }
gipmad 0:8f9b472ff818 126
gipmad 0:8f9b472ff818 127 return 0;
gipmad 0:8f9b472ff818 128 }
gipmad 0:8f9b472ff818 129
gipmad 0:8f9b472ff818 130 int Modem::sendCmdAndWaitForResp(const char* data, const char *resp, unsigned timeout,DataType type)
gipmad 0:8f9b472ff818 131 {
gipmad 0:8f9b472ff818 132 sendCmd(data);
gipmad 0:8f9b472ff818 133 return waitForResp(resp,timeout,type);
gipmad 0:8f9b472ff818 134 }