aaaa

Dependencies:   SeeedStudioTFTv2 TFT_fonts mbed

Fork of Seeed_TFT_Touch_Shield by Bhavik Bhuta

Committer:
uswickra
Date:
Sun Oct 26 08:41:39 2014 +0000
Revision:
11:575dfd388778
Parent:
7:5cf5aeae1954
working version..

Who changed what in which revision?

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