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