AtGsmCommon/AtCommandsSrc/AtGsm_GetTime.c

94 lines
2.4 KiB
C

//
// Created by ilya on 14.02.23.
//
#include "AtGsm_GetTime.h"
#include <SystemDelayInterface.h>
#include <stdlib.h>
#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 = (int)year + 100;
time->tm_mon = (int)month - 1;
time->tm_mday = (int)day;
time->tm_hour = (int)hour;
time->tm_min = (int)min;
time->tm_sec = (int)sec;
}
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;
}
}
SystemDelayMs(400);
return AT_ERROR;
}