// // Created by ilya on 14.02.23. // #include "AtGsm_GetTime.h" #include #include #include #include "AtGsmListCurrentCalls.h" #include "AtCmdCommonProtected.h" static void parser(char *buf, struct tm *time) { char *dasd; uint32_t year; uint32_t month; uint32_t day; uint32_t hour; uint32_t min; uint32_t sec; char buf_year[2]; buf_year[0] = buf[1]; buf_year[1] = buf[2]; char buf_month[2]; buf_month[0] = buf[4]; buf_month[1] = buf[5]; char buf_day[2]; buf_day[0] = buf[7]; buf_day[1] = buf[8]; char buf_hour[2]; buf_hour[0] = buf[10]; buf_hour[1] = buf[11]; char buf_min[2]; buf_min[0] = buf[13]; buf_min[1] = buf[14]; char buf_sec[2]; buf_sec[0] = buf[16]; buf_sec[1] = buf[17]; year = strtol(buf_year, &dasd, 10); month = strtol(buf_month, &dasd, 10); day = strtol(buf_day, &dasd, 10); hour = strtol(buf_hour, &dasd, 10); min = strtol(buf_min, &dasd, 10); sec = strtol(buf_sec, &dasd, 10); time->tm_year = year; time->tm_mon = month; time->tm_mday = day; time->tm_hour = hour; time->tm_min = min; time->tm_sec = sec; } static AtCommandResult AtGsm_GetTime(tAtCmd *env, struct tm *time) { char buf[20]; uint32_t timeout = 5000; uint32_t endMs = SystemGetMs() + timeout; uint32_t leftMs = timeout; AtCmdPrepare(env); AtCmdSendStatic(env, "AT+CCLK?\r\n"); while ((AtCmdReceiveNextLine(env, leftMs) == AT_OK) && (SystemGetMs() < endMs)) { leftMs = endMs - SystemGetMs(); if (AtCmdRxBeginWithStatic(env, "AT+CCLK")) { AtCmdRxClear(env); continue; } else if (AtCmdRxBeginWithStatic(env, "OK")) { AtCmdRxClear(env); return AT_OK; } else if (AtCmdRxBeginWithStatic(env, "ERROR")) { AtCmdRxClear(env); return AT_ERROR; } else if (AtCmdRxBeginWithStatic(env, "+CCLK: ")) { char *div = env->rxBuffer.data + sizeof("+CCLK:"); for (int i = 0; i < 18; i++) { char *tmpadd = div + i; buf[i] = *tmpadd; } parser(buf, time); AtCmdRxClear(env); } else { AtCmdProcessUnresolvedLine(env); AtCmdRxClear(env); continue; } } return AT_ERROR; } AtCommandResult AtGsm_GetTimestamp(tAtCmd *env, time_t *timestamp) { struct tm tmValue; memset(&tmValue, 0, sizeof(tmValue)); AtCommandResult res = AtGsm_GetTime(env, &tmValue); if (res != AT_OK) { return res; } tmValue.tm_mon -= 1; tmValue.tm_year += 100; *timestamp = mktime(&tmValue); return AT_OK; }