#include #include "syslog.h" #include "rpc/rpc_server.h" #include "io/io_base.h" #include "io/io_gsm.h" #include "utils.h" #include "fatfs/ff.h" // Run an AT command, log failures static bool AT_command(const char *command) { char buffer[80]; fputs(command, FILE_GSM); syslog("at command '%20s'", (unsigned)command, 0); for (;;) { if (!fgets(buffer, 80, FILE_GSM)) { // Passing the string might fail if it is not a ROM constant, // but even in that case %20s avoids crash and causes just // garbage to be printed. syslog("at command '%20s' reply timeout", (unsigned)command, 0); return false; } if (strncmp(buffer, "OK", 2) == 0) return true; if (strncmp(buffer, "ERROR", 5) == 0) { syslog("at command '%20s' error", (unsigned)command, 0); return false; } } } static bool HTTP_command(const char *command) { char buffer[80]; while (fread2(buffer, sizeof(buffer), FILE_GSM, 5)); // Flush buffers fputs(command, FILE_GSM); bool status_ok = false; do { if (!fgets(buffer, 80, FILE_GSM)) { syslog("http command '%20s' reply timeout", (unsigned)command, 0); return false; } if (strstr(buffer, " 200 ") != NULL) status_ok = true; } while (strcspn(buffer, "\r\n") != 0); // Empty line terminates HTTP reply return status_ok; } /* GPRS connection */ void GSM_task( void *pvParameters ) { FIL file; f_open(&file, "/diallog", FA_WRITE | FA_OPEN_ALWAYS); f_lseek(&file, f_size(&file)); insert_io_clone(FILE_GSM, (FILE*)&file); for(;;) { fwrite("S\n", 2, 1, (FILE*)&file); f_sync(&file); // Turn on the GSM module if (!io_gsm_poweron()) { syslog("io_gsm_poweron failed", 0, 0); continue; } // Dial up // Ignore errors, the modem may be already on. AT_command("ATE0\r\n"); AT_command("AT+CPIN=\"8502\"\r\n"); AT_command("AT+COPS=0\r\n"); if (!(AT_command("AT+CGDCONT=1,\"IP\",\"wap\"\r\n") && AT_command("AT$DESTINFO=\"10.1.1.1\",1,8080\r\n") && AT_command("ATD*97#\r\n"))) { syslog("GPRS dialup failed", 0, 0); continue; } // Connect to server through HTTP proxy if (!HTTP_command("CONNECT lakka.kapsi.fi:50140 HTTP/1.1\r\n" "Host: lakka.kapsi.fi:50140\r\n\r\n")) { syslog("HTTP connect failed", 0, 0); continue; } remove_io_clone(FILE_GSM, (FILE*)&file); rpc_chat(FILE_GSM); } }