 
     while (*p != '\r') {
       text[i++] = *p++;
+      if (i >= sizeof(text)) break;
     }
 
     uint16_t id = atoi(index);

  };
}

int16_t gsmGetSignalQuality() {
  gsmSendCommand_P(F("AT+CSQ"));

  char* p = strstr(gsmSerialBuffer, "CSQ: ");

  if (p) {
    p += 5;
    uint8_t i = 0;
    char buffer[3];

    while (*p != ',') {
      buffer[i++] = *(p++);
    }

    int n = atoi(buffer);
    if (n == 99 || n == -1) return 0;
    return n * 2 - 114;
  }

  return 0;

uint16_t latDeg, lngDeg;
uint32_t latFrac, lngFrac;
uint32_t date, time, speed, course, altitude, satellites, hdop;
uint32_t age, lastUpload, lastSMSCheck;
char gsmSerialBuffer[256];
uint8_t failureCount;

uint16_t deviceId;
uint16_t EEMEM deviceId_E;

uint32_t interval;
uint16_t EEMEM interval_E;

char apn[25];
char EEMEM apn_E[25];

void gpsUpdateValues();
uint8_t gsmInit();
uint8_t gsmSetup();
uint8_t gsmSendSMS(const char* destination, const char* text);
void gsmProcessSMS(uint16_t id, const char* sender, const char* text);
void gsmCheckSMS();
int8_t gsmGetSignalQuality();
void gsmGetBatteryLevel(uint8_t* percentage, uint16_t* voltage);
uint8_t gsmSendGETRequest(const char* url);
uint8_t gsmSendPOSTRequest(const char* url, const char* params);
