aaaa
Dependencies: SeeedStudioTFTv2 TFT_fonts mbed
Fork of Seeed_TFT_Touch_Shield by
Modem.cpp@7:5cf5aeae1954, 2014-10-25 (annotated)
- Committer:
- uswickra
- Date:
- Sat Oct 25 21:55:05 2014 +0000
- Revision:
- 7:5cf5aeae1954
- Parent:
- 5:c1af1f8fa026
- Child:
- 11:575dfd388778
interrupt handler
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uswickra | 5:c1af1f8fa026 | 1 | #include "Modem.h" |
uswickra | 5:c1af1f8fa026 | 2 | |
uswickra | 7:5cf5aeae1954 | 3 | void cleanBuffer(char *buffer, int count) |
uswickra | 7:5cf5aeae1954 | 4 | { |
uswickra | 7:5cf5aeae1954 | 5 | for(int i=0; i < count; i++) { |
uswickra | 7:5cf5aeae1954 | 6 | buffer[i] = '\0'; |
uswickra | 7:5cf5aeae1954 | 7 | } |
uswickra | 7:5cf5aeae1954 | 8 | } |
uswickra | 7:5cf5aeae1954 | 9 | |
uswickra | 5:c1af1f8fa026 | 10 | Modem::Modem(PinName tx, PinName rx, int baudRate,char *default_number):serial_modem(tx,rx) |
uswickra | 5:c1af1f8fa026 | 11 | { |
uswickra | 5:c1af1f8fa026 | 12 | this->default_number = default_number; |
uswickra | 5:c1af1f8fa026 | 13 | // serial_modem.baud(baudRate); |
uswickra | 5:c1af1f8fa026 | 14 | buffer_p = 0; |
uswickra | 5:c1af1f8fa026 | 15 | read_trail = false; |
uswickra | 5:c1af1f8fa026 | 16 | read_buffer[BUF_SIZE]='\0'; |
uswickra | 7:5cf5aeae1954 | 17 | mIndex = 0 ; |
uswickra | 7:5cf5aeae1954 | 18 | //available types mTypeState = 1 RING mTypeState = 2 SMS |
uswickra | 7:5cf5aeae1954 | 19 | mTypeState = 0 ; |
uswickra | 7:5cf5aeae1954 | 20 | cleanBuffer(messageBuffer, SMS_MAX_LENGTH); |
uswickra | 5:c1af1f8fa026 | 21 | } |
uswickra | 5:c1af1f8fa026 | 22 | |
uswickra | 5:c1af1f8fa026 | 23 | |
uswickra | 5:c1af1f8fa026 | 24 | /*********************************************** |
uswickra | 5:c1af1f8fa026 | 25 | Utility/debug Functions |
uswickra | 5:c1af1f8fa026 | 26 | ***********************************************/ |
uswickra | 7:5cf5aeae1954 | 27 | |
uswickra | 7:5cf5aeae1954 | 28 | int Modem::updateBuffer(char c){ |
uswickra | 7:5cf5aeae1954 | 29 | // messageBuffer[mIndex] = c ; |
uswickra | 7:5cf5aeae1954 | 30 | // mIndex = (mIndex + 1) % SMS_MAX_LENGTH ; |
uswickra | 7:5cf5aeae1954 | 31 | int messageType = MESSAGE_NONE; |
uswickra | 7:5cf5aeae1954 | 32 | // if(mIndex == 0 ){ |
uswickra | 7:5cf5aeae1954 | 33 | // cleanBuffer(messageBuffer, SMS_MAX_LENGTH); |
uswickra | 7:5cf5aeae1954 | 34 | // } |
uswickra | 7:5cf5aeae1954 | 35 | if(mTypeState == 0){ |
uswickra | 7:5cf5aeae1954 | 36 | if(c == 'R'){ |
uswickra | 7:5cf5aeae1954 | 37 | mTypeState = 01 ; |
uswickra | 7:5cf5aeae1954 | 38 | }else if (c == '+'){ |
uswickra | 7:5cf5aeae1954 | 39 | mTypeState = 11 ; |
uswickra | 7:5cf5aeae1954 | 40 | } |
uswickra | 7:5cf5aeae1954 | 41 | } |
uswickra | 7:5cf5aeae1954 | 42 | /* Handling the SMS MESSAGE */ |
uswickra | 7:5cf5aeae1954 | 43 | else if (mTypeState == 11){ |
uswickra | 7:5cf5aeae1954 | 44 | if(c != 'C'){ |
uswickra | 7:5cf5aeae1954 | 45 | mTypeState = 0 ; |
uswickra | 7:5cf5aeae1954 | 46 | }else { |
uswickra | 7:5cf5aeae1954 | 47 | mTypeState = 12 ; |
uswickra | 7:5cf5aeae1954 | 48 | } |
uswickra | 7:5cf5aeae1954 | 49 | } |
uswickra | 7:5cf5aeae1954 | 50 | else if (mTypeState == 12){ |
uswickra | 7:5cf5aeae1954 | 51 | if(c != 'M'){ |
uswickra | 7:5cf5aeae1954 | 52 | mTypeState = 0 ; |
uswickra | 7:5cf5aeae1954 | 53 | }else { |
uswickra | 7:5cf5aeae1954 | 54 | mTypeState = 13 ; |
uswickra | 7:5cf5aeae1954 | 55 | } |
uswickra | 7:5cf5aeae1954 | 56 | } |
uswickra | 7:5cf5aeae1954 | 57 | else if (mTypeState == 13){ |
uswickra | 7:5cf5aeae1954 | 58 | if(c != 'T'){ |
uswickra | 7:5cf5aeae1954 | 59 | mTypeState = 0 ; |
uswickra | 7:5cf5aeae1954 | 60 | }else { |
uswickra | 7:5cf5aeae1954 | 61 | mTypeState = 14 ; |
uswickra | 7:5cf5aeae1954 | 62 | } |
uswickra | 7:5cf5aeae1954 | 63 | } |
uswickra | 7:5cf5aeae1954 | 64 | else if (mTypeState == 14){ |
uswickra | 7:5cf5aeae1954 | 65 | if(c != ':'){ |
uswickra | 7:5cf5aeae1954 | 66 | mTypeState = 0 ; |
uswickra | 7:5cf5aeae1954 | 67 | }else { |
uswickra | 7:5cf5aeae1954 | 68 | mTypeState = 15 ; |
uswickra | 7:5cf5aeae1954 | 69 | } |
uswickra | 5:c1af1f8fa026 | 70 | } |
uswickra | 7:5cf5aeae1954 | 71 | else if (mTypeState == 15){ |
uswickra | 7:5cf5aeae1954 | 72 | messageBuffer[mIndex] = c ; |
uswickra | 7:5cf5aeae1954 | 73 | //next index |
uswickra | 7:5cf5aeae1954 | 74 | mIndex = (mIndex + 1) % SMS_MAX_LENGTH ; |
uswickra | 7:5cf5aeae1954 | 75 | if(c != '\n'){ |
uswickra | 7:5cf5aeae1954 | 76 | mTypeState = 15 ; |
uswickra | 7:5cf5aeae1954 | 77 | }else { |
uswickra | 7:5cf5aeae1954 | 78 | mTypeState = 16 ; |
uswickra | 7:5cf5aeae1954 | 79 | } |
uswickra | 7:5cf5aeae1954 | 80 | } |
uswickra | 7:5cf5aeae1954 | 81 | else if (mTypeState == 16){ |
uswickra | 7:5cf5aeae1954 | 82 | messageBuffer[mIndex] = c ; |
uswickra | 7:5cf5aeae1954 | 83 | //next index |
uswickra | 7:5cf5aeae1954 | 84 | mIndex = (mIndex + 1) % SMS_MAX_LENGTH ; |
uswickra | 7:5cf5aeae1954 | 85 | if(c != '\n'){ |
uswickra | 7:5cf5aeae1954 | 86 | mTypeState = 16 ; |
uswickra | 7:5cf5aeae1954 | 87 | }else { |
uswickra | 7:5cf5aeae1954 | 88 | mTypeState = 0 ; |
uswickra | 7:5cf5aeae1954 | 89 | messageType = MESSAGE_SMS ; |
uswickra | 7:5cf5aeae1954 | 90 | strcpy(messageCopyBuffer,messageBuffer); |
uswickra | 7:5cf5aeae1954 | 91 | cleanBuffer(messageBuffer, SMS_MAX_LENGTH); |
uswickra | 7:5cf5aeae1954 | 92 | } |
uswickra | 7:5cf5aeae1954 | 93 | } |
uswickra | 7:5cf5aeae1954 | 94 | |
uswickra | 7:5cf5aeae1954 | 95 | /*Handling the RING*/ |
uswickra | 7:5cf5aeae1954 | 96 | else if (mTypeState == 01){ |
uswickra | 7:5cf5aeae1954 | 97 | if(c != 'I'){ |
uswickra | 7:5cf5aeae1954 | 98 | mTypeState = 0 ; |
uswickra | 7:5cf5aeae1954 | 99 | }else { |
uswickra | 7:5cf5aeae1954 | 100 | mTypeState = 02 ; |
uswickra | 7:5cf5aeae1954 | 101 | } |
uswickra | 7:5cf5aeae1954 | 102 | }else if (mTypeState == 02){ |
uswickra | 7:5cf5aeae1954 | 103 | if(c != 'N'){ |
uswickra | 7:5cf5aeae1954 | 104 | mTypeState = 0 ; |
uswickra | 7:5cf5aeae1954 | 105 | }else { |
uswickra | 7:5cf5aeae1954 | 106 | mTypeState = 03 ; |
uswickra | 7:5cf5aeae1954 | 107 | } |
uswickra | 7:5cf5aeae1954 | 108 | }else if (mTypeState == 03){ |
uswickra | 7:5cf5aeae1954 | 109 | if(c != 'G'){ |
uswickra | 7:5cf5aeae1954 | 110 | mTypeState = 0 ; |
uswickra | 7:5cf5aeae1954 | 111 | }else { |
uswickra | 7:5cf5aeae1954 | 112 | mTypeState = 0 ; |
uswickra | 7:5cf5aeae1954 | 113 | messageType = MESSAGE_RING ; |
uswickra | 7:5cf5aeae1954 | 114 | } |
uswickra | 7:5cf5aeae1954 | 115 | } |
uswickra | 7:5cf5aeae1954 | 116 | |
uswickra | 7:5cf5aeae1954 | 117 | |
uswickra | 7:5cf5aeae1954 | 118 | return messageType; |
uswickra | 5:c1af1f8fa026 | 119 | } |
uswickra | 5:c1af1f8fa026 | 120 | |
uswickra | 7:5cf5aeae1954 | 121 | |
uswickra | 5:c1af1f8fa026 | 122 | int Modem::loopHandle(void) |
uswickra | 5:c1af1f8fa026 | 123 | { |
uswickra | 5:c1af1f8fa026 | 124 | char gprsBuffer[100]; |
uswickra | 5:c1af1f8fa026 | 125 | int i; |
uswickra | 5:c1af1f8fa026 | 126 | char *s = NULL; |
uswickra | 5:c1af1f8fa026 | 127 | while(serial_modem.readable()) { |
uswickra | 5:c1af1f8fa026 | 128 | char c = serial_modem.getc(); |
uswickra | 5:c1af1f8fa026 | 129 | } |
uswickra | 5:c1af1f8fa026 | 130 | wait(0.5); |
uswickra | 5:c1af1f8fa026 | 131 | START: |
uswickra | 5:c1af1f8fa026 | 132 | cleanBuffer(gprsBuffer,100); |
uswickra | 5:c1af1f8fa026 | 133 | i = 0; |
uswickra | 5:c1af1f8fa026 | 134 | while(1) { |
uswickra | 5:c1af1f8fa026 | 135 | if(serial_modem.readable()) { |
uswickra | 5:c1af1f8fa026 | 136 | timeCnt.start(); // start timer |
uswickra | 5:c1af1f8fa026 | 137 | while(1) { |
uswickra | 5:c1af1f8fa026 | 138 | while (serial_modem.readable()) { |
uswickra | 5:c1af1f8fa026 | 139 | char c = serial_modem.getc(); |
uswickra | 5:c1af1f8fa026 | 140 | if (c == '\r' || c == '\n') c = '$'; |
uswickra | 5:c1af1f8fa026 | 141 | gprsBuffer[i] = c; |
uswickra | 5:c1af1f8fa026 | 142 | i++; |
uswickra | 5:c1af1f8fa026 | 143 | if(i > 100) { |
uswickra | 5:c1af1f8fa026 | 144 | i = 0; |
uswickra | 5:c1af1f8fa026 | 145 | break; |
uswickra | 5:c1af1f8fa026 | 146 | } |
uswickra | 5:c1af1f8fa026 | 147 | } |
uswickra | 5:c1af1f8fa026 | 148 | if(timeCnt.read() > 2) { // time out |
uswickra | 5:c1af1f8fa026 | 149 | timeCnt.stop(); |
uswickra | 5:c1af1f8fa026 | 150 | timeCnt.reset(); |
uswickra | 5:c1af1f8fa026 | 151 | break; |
uswickra | 5:c1af1f8fa026 | 152 | } |
uswickra | 5:c1af1f8fa026 | 153 | } |
uswickra | 5:c1af1f8fa026 | 154 | break; |
uswickra | 5:c1af1f8fa026 | 155 | } |
uswickra | 5:c1af1f8fa026 | 156 | } |
uswickra | 5:c1af1f8fa026 | 157 | if(NULL != strstr(gprsBuffer,"RING")) { |
uswickra | 5:c1af1f8fa026 | 158 | return MESSAGE_RING; |
uswickra | 5:c1af1f8fa026 | 159 | } else if(NULL != (s = strstr(gprsBuffer,"+CMT"))) { //SMS: $$+CMTI: "SM",24$$ |
uswickra | 5:c1af1f8fa026 | 160 | if(NULL != (s = strstr(gprsBuffer,"+32"))) { |
uswickra | 5:c1af1f8fa026 | 161 | s += 6; |
uswickra | 5:c1af1f8fa026 | 162 | int i = 0; |
uswickra | 5:c1af1f8fa026 | 163 | cleanBuffer(messageBuffer,SMS_MAX_LENGTH); |
uswickra | 5:c1af1f8fa026 | 164 | while((*s != '$')&&(i < SMS_MAX_LENGTH-1)) { |
uswickra | 5:c1af1f8fa026 | 165 | messageBuffer[i++] = *(s++); |
uswickra | 5:c1af1f8fa026 | 166 | } |
uswickra | 5:c1af1f8fa026 | 167 | messageBuffer[i] = '\0'; |
uswickra | 5:c1af1f8fa026 | 168 | return MESSAGE_SMS; |
uswickra | 5:c1af1f8fa026 | 169 | } else { |
uswickra | 5:c1af1f8fa026 | 170 | goto START; |
uswickra | 5:c1af1f8fa026 | 171 | } |
uswickra | 5:c1af1f8fa026 | 172 | } else { |
uswickra | 5:c1af1f8fa026 | 173 | goto START; |
uswickra | 5:c1af1f8fa026 | 174 | } |
uswickra | 5:c1af1f8fa026 | 175 | } |
uswickra | 5:c1af1f8fa026 | 176 | |
uswickra | 5:c1af1f8fa026 | 177 | |
uswickra | 5:c1af1f8fa026 | 178 | void Modem::clean_buffer() |
uswickra | 5:c1af1f8fa026 | 179 | { |
uswickra | 5:c1af1f8fa026 | 180 | int i = 0; |
uswickra | 5:c1af1f8fa026 | 181 | for(i = 0 ; i < BUF_SIZE ; i ++) { |
uswickra | 5:c1af1f8fa026 | 182 | read_buffer[i] = '\0'; |
uswickra | 5:c1af1f8fa026 | 183 | } |
uswickra | 5:c1af1f8fa026 | 184 | } |
uswickra | 5:c1af1f8fa026 | 185 | |
uswickra | 5:c1af1f8fa026 | 186 | void Modem::reset_buffer() |
uswickra | 5:c1af1f8fa026 | 187 | { |
uswickra | 5:c1af1f8fa026 | 188 | buffer_p = 0; |
uswickra | 5:c1af1f8fa026 | 189 | clean_buffer(); |
uswickra | 5:c1af1f8fa026 | 190 | } |
uswickra | 5:c1af1f8fa026 | 191 | |
uswickra | 5:c1af1f8fa026 | 192 | void Modem::debug_cmd_buffer() |
uswickra | 5:c1af1f8fa026 | 193 | { |
uswickra | 5:c1af1f8fa026 | 194 | // TFT.printf("[DEBUG] "); |
uswickra | 5:c1af1f8fa026 | 195 | int i = 0 ; |
uswickra | 5:c1af1f8fa026 | 196 | for(i = 0 ; i < BUF_SIZE ; i ++) { |
uswickra | 5:c1af1f8fa026 | 197 | // TFT.putc(read_buffer[i]); |
uswickra | 5:c1af1f8fa026 | 198 | } |
uswickra | 5:c1af1f8fa026 | 199 | // TFT.printf("\r\n"); |
uswickra | 5:c1af1f8fa026 | 200 | } |
uswickra | 5:c1af1f8fa026 | 201 | |
uswickra | 5:c1af1f8fa026 | 202 | int Modem::find_pattern_end(char *pattern, char *buffer, int size) |
uswickra | 5:c1af1f8fa026 | 203 | { |
uswickra | 5:c1af1f8fa026 | 204 | int i = 0; |
uswickra | 5:c1af1f8fa026 | 205 | int final_state = strlen(pattern); |
uswickra | 5:c1af1f8fa026 | 206 | int state = 0; |
uswickra | 5:c1af1f8fa026 | 207 | for (i = 0; i < size; i++) { |
uswickra | 5:c1af1f8fa026 | 208 | char current = buffer[i]; |
uswickra | 5:c1af1f8fa026 | 209 | if (pattern[state] == current) { |
uswickra | 5:c1af1f8fa026 | 210 | state += 1; |
uswickra | 5:c1af1f8fa026 | 211 | continue; |
uswickra | 5:c1af1f8fa026 | 212 | } |
uswickra | 5:c1af1f8fa026 | 213 | if (state == final_state) { |
uswickra | 5:c1af1f8fa026 | 214 | return i; |
uswickra | 5:c1af1f8fa026 | 215 | } |
uswickra | 5:c1af1f8fa026 | 216 | state = 0; |
uswickra | 5:c1af1f8fa026 | 217 | } |
uswickra | 5:c1af1f8fa026 | 218 | |
uswickra | 5:c1af1f8fa026 | 219 | return -1; |
uswickra | 5:c1af1f8fa026 | 220 | } |
uswickra | 5:c1af1f8fa026 | 221 | |
uswickra | 5:c1af1f8fa026 | 222 | /******************************************************* |
uswickra | 5:c1af1f8fa026 | 223 | Control/Init/Setup Functions for SeedsStudto GPRS modem |
uswickra | 5:c1af1f8fa026 | 224 | ********************************************************/ |
uswickra | 5:c1af1f8fa026 | 225 | |
uswickra | 5:c1af1f8fa026 | 226 | int Modem::init() |
uswickra | 5:c1af1f8fa026 | 227 | { |
uswickra | 5:c1af1f8fa026 | 228 | int i = 0; |
uswickra | 5:c1af1f8fa026 | 229 | int ret = 0 ; |
uswickra | 5:c1af1f8fa026 | 230 | for(i = 0 ; i < 10 ; i++) { |
uswickra | 5:c1af1f8fa026 | 231 | ret = check_AT(); |
uswickra | 5:c1af1f8fa026 | 232 | wait(1); |
uswickra | 5:c1af1f8fa026 | 233 | } |
uswickra | 5:c1af1f8fa026 | 234 | |
uswickra | 5:c1af1f8fa026 | 235 | if(ret) { |
uswickra | 5:c1af1f8fa026 | 236 | ret = check_PIN(); |
uswickra | 5:c1af1f8fa026 | 237 | } |
uswickra | 5:c1af1f8fa026 | 238 | if(ret) { |
uswickra | 5:c1af1f8fa026 | 239 | ret = setup_SMS(); |
uswickra | 5:c1af1f8fa026 | 240 | } |
uswickra | 5:c1af1f8fa026 | 241 | return ret; |
uswickra | 5:c1af1f8fa026 | 242 | }; |
uswickra | 5:c1af1f8fa026 | 243 | |
uswickra | 5:c1af1f8fa026 | 244 | int Modem::check_AT() |
uswickra | 5:c1af1f8fa026 | 245 | { |
uswickra | 5:c1af1f8fa026 | 246 | int ret = sendCmdWaitResp("AT\r\n","OK", 5); |
uswickra | 5:c1af1f8fa026 | 247 | if(ret == 0) { |
uswickra | 5:c1af1f8fa026 | 248 | return -1 ; |
uswickra | 5:c1af1f8fa026 | 249 | } |
uswickra | 5:c1af1f8fa026 | 250 | |
uswickra | 5:c1af1f8fa026 | 251 | return 1; |
uswickra | 5:c1af1f8fa026 | 252 | } |
uswickra | 5:c1af1f8fa026 | 253 | |
uswickra | 5:c1af1f8fa026 | 254 | int Modem::check_PIN() |
uswickra | 5:c1af1f8fa026 | 255 | { |
uswickra | 5:c1af1f8fa026 | 256 | int ret = sendCmdWaitResp("AT+CPIN?\r\n","+CPIN: READY", 5); |
uswickra | 5:c1af1f8fa026 | 257 | |
uswickra | 5:c1af1f8fa026 | 258 | //debug code |
uswickra | 5:c1af1f8fa026 | 259 | debug_cmd_buffer(); |
uswickra | 5:c1af1f8fa026 | 260 | |
uswickra | 5:c1af1f8fa026 | 261 | if(ret == 0) { |
uswickra | 5:c1af1f8fa026 | 262 | return -1 ; |
uswickra | 5:c1af1f8fa026 | 263 | } |
uswickra | 5:c1af1f8fa026 | 264 | |
uswickra | 5:c1af1f8fa026 | 265 | return 1; |
uswickra | 5:c1af1f8fa026 | 266 | } |
uswickra | 5:c1af1f8fa026 | 267 | |
uswickra | 5:c1af1f8fa026 | 268 | int Modem::setup_SMS() |
uswickra | 5:c1af1f8fa026 | 269 | { |
uswickra | 5:c1af1f8fa026 | 270 | // sendCmdWaitResp("AT+CPMS=\"SM\",\"BM\"\r\n", "OK", 5); |
uswickra | 5:c1af1f8fa026 | 271 | // int ret = sendCmdWaitResp("AT+CNMI=2,2\r\n", "OK", 5); |
uswickra | 5:c1af1f8fa026 | 272 | int ret = 1 ; |
uswickra | 5:c1af1f8fa026 | 273 | if(ret) { |
uswickra | 5:c1af1f8fa026 | 274 | ret = sendCmdWaitResp("AT+CMGF=1\r\n","OK", 5); |
uswickra | 5:c1af1f8fa026 | 275 | } |
uswickra | 5:c1af1f8fa026 | 276 | //debug code |
uswickra | 5:c1af1f8fa026 | 277 | debug_cmd_buffer(); |
uswickra | 5:c1af1f8fa026 | 278 | if(ret == 0) { |
uswickra | 5:c1af1f8fa026 | 279 | return -1 ; |
uswickra | 5:c1af1f8fa026 | 280 | } |
uswickra | 5:c1af1f8fa026 | 281 | |
uswickra | 5:c1af1f8fa026 | 282 | return 1; |
uswickra | 5:c1af1f8fa026 | 283 | } |
uswickra | 5:c1af1f8fa026 | 284 | |
uswickra | 5:c1af1f8fa026 | 285 | /*********************************************** |
uswickra | 5:c1af1f8fa026 | 286 | Start of Primary Interface Functions |
uswickra | 5:c1af1f8fa026 | 287 | ***********************************************/ |
uswickra | 5:c1af1f8fa026 | 288 | |
uswickra | 5:c1af1f8fa026 | 289 | int Modem::call_phone(char* number) |
uswickra | 5:c1af1f8fa026 | 290 | { |
uswickra | 5:c1af1f8fa026 | 291 | //enable optional caller presentation |
uswickra | 5:c1af1f8fa026 | 292 | sendCmdWaitResp("AT+COLP=1\r\n","OK", 5); |
uswickra | 5:c1af1f8fa026 | 293 | //send actual call |
uswickra | 5:c1af1f8fa026 | 294 | wait(1); |
uswickra | 5:c1af1f8fa026 | 295 | debug_cmd_buffer(); |
uswickra | 5:c1af1f8fa026 | 296 | int ret = 0 ; |
uswickra | 5:c1af1f8fa026 | 297 | char temp[30]; |
uswickra | 5:c1af1f8fa026 | 298 | |
uswickra | 5:c1af1f8fa026 | 299 | if(number == NULL) { |
uswickra | 5:c1af1f8fa026 | 300 | snprintf(temp, 30, "\r\nATD%s;\r\n",default_number); |
uswickra | 5:c1af1f8fa026 | 301 | } else { |
uswickra | 5:c1af1f8fa026 | 302 | snprintf(temp, 30, "\r\nATD%s;\r\n",number); |
uswickra | 5:c1af1f8fa026 | 303 | } |
uswickra | 5:c1af1f8fa026 | 304 | ret = sendCmdWaitResp(temp,"OK", 5); |
uswickra | 5:c1af1f8fa026 | 305 | // ret = sendCmdWaitResp("\r\nATD+13174806512;\r\n","OK", 5); |
uswickra | 5:c1af1f8fa026 | 306 | //debug code |
uswickra | 5:c1af1f8fa026 | 307 | debug_cmd_buffer(); |
uswickra | 5:c1af1f8fa026 | 308 | |
uswickra | 5:c1af1f8fa026 | 309 | if(ret == 0) { |
uswickra | 5:c1af1f8fa026 | 310 | return -1 ; |
uswickra | 5:c1af1f8fa026 | 311 | } |
uswickra | 5:c1af1f8fa026 | 312 | return ret ; |
uswickra | 5:c1af1f8fa026 | 313 | }; |
uswickra | 5:c1af1f8fa026 | 314 | |
uswickra | 5:c1af1f8fa026 | 315 | void Modem::hangup_phone() |
uswickra | 5:c1af1f8fa026 | 316 | { |
uswickra | 5:c1af1f8fa026 | 317 | int ret = sendCmdWaitResp("ATH\r\n","OK", 5); |
uswickra | 5:c1af1f8fa026 | 318 | wait(1); |
uswickra | 5:c1af1f8fa026 | 319 | } |
uswickra | 5:c1af1f8fa026 | 320 | |
uswickra | 5:c1af1f8fa026 | 321 | void Modem::recv_phone() |
uswickra | 5:c1af1f8fa026 | 322 | { |
uswickra | 5:c1af1f8fa026 | 323 | int ret = sendCmdWaitResp("ATA\r\n","OK", 5); |
uswickra | 5:c1af1f8fa026 | 324 | } |
uswickra | 5:c1af1f8fa026 | 325 | |
uswickra | 5:c1af1f8fa026 | 326 | void Modem::get_clck(char* msg, int size) |
uswickra | 5:c1af1f8fa026 | 327 | { |
uswickra | 5:c1af1f8fa026 | 328 | int ret = sendCmdWaitResp("AT+CCLK?\r\n","OK", 5); |
uswickra | 5:c1af1f8fa026 | 329 | //if returned OK |
uswickra | 5:c1af1f8fa026 | 330 | debug_cmd_buffer(); |
uswickra | 5:c1af1f8fa026 | 331 | if(ret == 1) { |
uswickra | 5:c1af1f8fa026 | 332 | char *pattern = "+CCLK:"; |
uswickra | 5:c1af1f8fa026 | 333 | int index = find_pattern_end(pattern, read_buffer, BUF_SIZE); |
uswickra | 5:c1af1f8fa026 | 334 | int state = 0; |
uswickra | 5:c1af1f8fa026 | 335 | int curr = index + 2; |
uswickra | 5:c1af1f8fa026 | 336 | int end = curr + 17 ; |
uswickra | 5:c1af1f8fa026 | 337 | while(curr < end) { |
uswickra | 5:c1af1f8fa026 | 338 | msg[state]=read_buffer[curr]; |
uswickra | 5:c1af1f8fa026 | 339 | state++; |
uswickra | 5:c1af1f8fa026 | 340 | curr++; |
uswickra | 5:c1af1f8fa026 | 341 | } |
uswickra | 5:c1af1f8fa026 | 342 | msg[state] = '\0'; |
uswickra | 5:c1af1f8fa026 | 343 | } |
uswickra | 5:c1af1f8fa026 | 344 | } |
uswickra | 5:c1af1f8fa026 | 345 | |
uswickra | 5:c1af1f8fa026 | 346 | int Modem::send_sms(char* number, char* msg) |
uswickra | 5:c1af1f8fa026 | 347 | { |
uswickra | 5:c1af1f8fa026 | 348 | char temp[60]; |
uswickra | 5:c1af1f8fa026 | 349 | while(read_device_ready()) { |
uswickra | 5:c1af1f8fa026 | 350 | char c = read_device(); |
uswickra | 5:c1af1f8fa026 | 351 | } |
uswickra | 5:c1af1f8fa026 | 352 | |
uswickra | 5:c1af1f8fa026 | 353 | if(number == NULL) { |
uswickra | 5:c1af1f8fa026 | 354 | snprintf(temp, 60, "AT+CMGS=\"%s\"\r\n",default_number); |
uswickra | 5:c1af1f8fa026 | 355 | } else { |
uswickra | 5:c1af1f8fa026 | 356 | snprintf(temp, 60, "AT+CMGS=\"%s\"\r\n",number); |
uswickra | 5:c1af1f8fa026 | 357 | } |
uswickra | 5:c1af1f8fa026 | 358 | |
uswickra | 5:c1af1f8fa026 | 359 | sendCmdWaitResp(temp, ">", 5); |
uswickra | 5:c1af1f8fa026 | 360 | debug_cmd_buffer(); |
uswickra | 5:c1af1f8fa026 | 361 | wait(3); |
uswickra | 5:c1af1f8fa026 | 362 | //now write the actual message |
uswickra | 5:c1af1f8fa026 | 363 | write_device_str(msg); |
uswickra | 5:c1af1f8fa026 | 364 | //final ctrl+z character in ascii |
uswickra | 5:c1af1f8fa026 | 365 | write_device((char)0x1a); |
uswickra | 5:c1af1f8fa026 | 366 | |
uswickra | 5:c1af1f8fa026 | 367 | return 1 ; |
uswickra | 5:c1af1f8fa026 | 368 | } |
uswickra | 5:c1af1f8fa026 | 369 | |
uswickra | 5:c1af1f8fa026 | 370 | void Modem::recv_sms(char* msg, int msg_size ,int index) |
uswickra | 5:c1af1f8fa026 | 371 | { |
uswickra | 5:c1af1f8fa026 | 372 | char temp[30]; |
uswickra | 5:c1af1f8fa026 | 373 | snprintf(temp, 30, "AT+CMGR=%d\r\n",index); |
uswickra | 5:c1af1f8fa026 | 374 | read_trail = true; |
uswickra | 5:c1af1f8fa026 | 375 | int ret = sendCmdWaitResp("AT+CMGL=\"ALL\"\r\n","OK", 30); |
uswickra | 5:c1af1f8fa026 | 376 | read_trail = false; |
uswickra | 5:c1af1f8fa026 | 377 | wait(3); |
uswickra | 5:c1af1f8fa026 | 378 | |
uswickra | 5:c1af1f8fa026 | 379 | debug_cmd_buffer(); |
uswickra | 5:c1af1f8fa026 | 380 | //if returned OK |
uswickra | 5:c1af1f8fa026 | 381 | // if(ret == 1) { |
uswickra | 5:c1af1f8fa026 | 382 | char *pattern = "+CMT:"; |
uswickra | 5:c1af1f8fa026 | 383 | int in = find_pattern_end(pattern, read_buffer, BUF_SIZE); |
uswickra | 5:c1af1f8fa026 | 384 | int state = 0; |
uswickra | 5:c1af1f8fa026 | 385 | int curr = in ; |
uswickra | 5:c1af1f8fa026 | 386 | int end = BUF_SIZE ; |
uswickra | 5:c1af1f8fa026 | 387 | while(curr < end) { |
uswickra | 5:c1af1f8fa026 | 388 | msg[state]=read_buffer[curr]; |
uswickra | 5:c1af1f8fa026 | 389 | state++; |
uswickra | 5:c1af1f8fa026 | 390 | curr++; |
uswickra | 5:c1af1f8fa026 | 391 | } |
uswickra | 5:c1af1f8fa026 | 392 | msg[state] = '\0'; |
uswickra | 5:c1af1f8fa026 | 393 | |
uswickra | 5:c1af1f8fa026 | 394 | // } |
uswickra | 5:c1af1f8fa026 | 395 | wait(1); |
uswickra | 5:c1af1f8fa026 | 396 | // ret = sendCmdWaitResp("AT+CMGD=1,4\r\n","OK", 5); |
uswickra | 5:c1af1f8fa026 | 397 | } |
uswickra | 5:c1af1f8fa026 | 398 | |
uswickra | 5:c1af1f8fa026 | 399 | /*********************************************** |
uswickra | 5:c1af1f8fa026 | 400 | Start of Low Level Functions |
uswickra | 5:c1af1f8fa026 | 401 | ***********************************************/ |
uswickra | 5:c1af1f8fa026 | 402 | |
uswickra | 5:c1af1f8fa026 | 403 | bool Modem::read_device_ready() |
uswickra | 5:c1af1f8fa026 | 404 | { |
uswickra | 5:c1af1f8fa026 | 405 | return serial_modem.readable(); |
uswickra | 5:c1af1f8fa026 | 406 | } |
uswickra | 5:c1af1f8fa026 | 407 | |
uswickra | 5:c1af1f8fa026 | 408 | void Modem::cls() |
uswickra | 5:c1af1f8fa026 | 409 | { |
uswickra | 5:c1af1f8fa026 | 410 | // TFT.locate(0,0); |
uswickra | 5:c1af1f8fa026 | 411 | // TFT.cls(); |
uswickra | 5:c1af1f8fa026 | 412 | |
uswickra | 5:c1af1f8fa026 | 413 | } |
uswickra | 5:c1af1f8fa026 | 414 | |
uswickra | 5:c1af1f8fa026 | 415 | char Modem::read_device() |
uswickra | 5:c1af1f8fa026 | 416 | { |
uswickra | 5:c1af1f8fa026 | 417 | return serial_modem.getc(); |
uswickra | 5:c1af1f8fa026 | 418 | } |
uswickra | 5:c1af1f8fa026 | 419 | |
uswickra | 5:c1af1f8fa026 | 420 | int Modem::write_device(char c) |
uswickra | 5:c1af1f8fa026 | 421 | { |
uswickra | 5:c1af1f8fa026 | 422 | int ret = 0; |
uswickra | 5:c1af1f8fa026 | 423 | while(!serial_modem.writeable()) { |
uswickra | 5:c1af1f8fa026 | 424 | //wait until writeable |
uswickra | 5:c1af1f8fa026 | 425 | } |
uswickra | 5:c1af1f8fa026 | 426 | ret = serial_modem.putc(c); |
uswickra | 5:c1af1f8fa026 | 427 | if(ret != -1) |
uswickra | 5:c1af1f8fa026 | 428 | return 0; |
uswickra | 5:c1af1f8fa026 | 429 | return ret; |
uswickra | 5:c1af1f8fa026 | 430 | }; |
uswickra | 5:c1af1f8fa026 | 431 | |
uswickra | 5:c1af1f8fa026 | 432 | |
uswickra | 5:c1af1f8fa026 | 433 | int Modem::write_device_str(char* str) |
uswickra | 5:c1af1f8fa026 | 434 | { |
uswickra | 5:c1af1f8fa026 | 435 | int ret = 0; |
uswickra | 5:c1af1f8fa026 | 436 | while(!serial_modem.writeable()) { |
uswickra | 5:c1af1f8fa026 | 437 | //wait until writeable |
uswickra | 5:c1af1f8fa026 | 438 | } |
uswickra | 5:c1af1f8fa026 | 439 | |
uswickra | 5:c1af1f8fa026 | 440 | ret = serial_modem.puts(str); |
uswickra | 5:c1af1f8fa026 | 441 | if(ret != -1) |
uswickra | 5:c1af1f8fa026 | 442 | return 0; |
uswickra | 5:c1af1f8fa026 | 443 | |
uswickra | 5:c1af1f8fa026 | 444 | return ret; |
uswickra | 5:c1af1f8fa026 | 445 | } |
uswickra | 5:c1af1f8fa026 | 446 | |
uswickra | 5:c1af1f8fa026 | 447 | int Modem::sendCmdWaitResp(char* cmd, char* response, int timeout) |
uswickra | 5:c1af1f8fa026 | 448 | { |
uswickra | 5:c1af1f8fa026 | 449 | //reset buffer pointer |
uswickra | 5:c1af1f8fa026 | 450 | reset_buffer(); |
uswickra | 5:c1af1f8fa026 | 451 | //send command and wait until respose |
uswickra | 5:c1af1f8fa026 | 452 | write_device_str(cmd); |
uswickra | 5:c1af1f8fa026 | 453 | |
uswickra | 5:c1af1f8fa026 | 454 | //wait |
uswickra | 5:c1af1f8fa026 | 455 | Timer my_timer; |
uswickra | 5:c1af1f8fa026 | 456 | my_timer.start(); |
uswickra | 5:c1af1f8fa026 | 457 | float start = my_timer.read(); |
uswickra | 5:c1af1f8fa026 | 458 | float elapsed = 0 ; |
uswickra | 5:c1af1f8fa026 | 459 | |
uswickra | 5:c1af1f8fa026 | 460 | int expected_resp_len = strlen(response); |
uswickra | 5:c1af1f8fa026 | 461 | int curr_len = 0 ; |
uswickra | 5:c1af1f8fa026 | 462 | while(1) { |
uswickra | 5:c1af1f8fa026 | 463 | //ok start waiting game ;) |
uswickra | 5:c1af1f8fa026 | 464 | if(read_device_ready()) { |
uswickra | 5:c1af1f8fa026 | 465 | char rec_char = read_device(); |
uswickra | 5:c1af1f8fa026 | 466 | // pc.putc(rec_char); |
uswickra | 5:c1af1f8fa026 | 467 | curr_len++; |
uswickra | 5:c1af1f8fa026 | 468 | if(rec_char != response[curr_len - 1]) { |
uswickra | 5:c1af1f8fa026 | 469 | //we haven't recieved expected response |
uswickra | 5:c1af1f8fa026 | 470 | //reset |
uswickra | 5:c1af1f8fa026 | 471 | curr_len = 0 ; |
uswickra | 5:c1af1f8fa026 | 472 | } |
uswickra | 5:c1af1f8fa026 | 473 | //update characters read so far in to the buffer (circular) |
uswickra | 5:c1af1f8fa026 | 474 | read_buffer[buffer_p] = rec_char; |
uswickra | 5:c1af1f8fa026 | 475 | buffer_p = (buffer_p + 1) % BUF_SIZE; |
uswickra | 5:c1af1f8fa026 | 476 | |
uswickra | 5:c1af1f8fa026 | 477 | if(!read_trail && curr_len == expected_resp_len) { |
uswickra | 5:c1af1f8fa026 | 478 | //at this point response is matched and we have waited for the length of response |
uswickra | 5:c1af1f8fa026 | 479 | return 1 ; |
uswickra | 5:c1af1f8fa026 | 480 | } |
uswickra | 5:c1af1f8fa026 | 481 | } |
uswickra | 5:c1af1f8fa026 | 482 | |
uswickra | 5:c1af1f8fa026 | 483 | elapsed = my_timer.read() - start; |
uswickra | 5:c1af1f8fa026 | 484 | if(elapsed > timeout) { |
uswickra | 5:c1af1f8fa026 | 485 | //we have waited long enough to succeed, but failed |
uswickra | 5:c1af1f8fa026 | 486 | break; |
uswickra | 5:c1af1f8fa026 | 487 | } |
uswickra | 5:c1af1f8fa026 | 488 | } |
uswickra | 5:c1af1f8fa026 | 489 | |
uswickra | 5:c1af1f8fa026 | 490 | |
uswickra | 5:c1af1f8fa026 | 491 | while(serial_modem.readable()) { // display the other thing.. |
uswickra | 5:c1af1f8fa026 | 492 | char c = serial_modem.getc(); |
uswickra | 5:c1af1f8fa026 | 493 | //if we enabled reading trailing characters -> read them onto buffer |
uswickra | 5:c1af1f8fa026 | 494 | if(read_trail) { |
uswickra | 5:c1af1f8fa026 | 495 | read_buffer[buffer_p] = c; |
uswickra | 5:c1af1f8fa026 | 496 | buffer_p = (buffer_p + 1) % BUF_SIZE; |
uswickra | 5:c1af1f8fa026 | 497 | } |
uswickra | 5:c1af1f8fa026 | 498 | } |
uswickra | 5:c1af1f8fa026 | 499 | return 0 ; |
uswickra | 5:c1af1f8fa026 | 500 | } |