Skip to content

Commit 43e80b3

Browse files
committed
add parse date function instead of sscanf
1 parent 5567b6b commit 43e80b3

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

examples/utility/Provisioning_2.0/CSRHandler.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,42 @@ uint32_t CSRHandlerClass::getTimestamp() {
193193
return ts;
194194
}
195195

196+
bool CSRHandlerClass::parseDateFromStr(char *str) {
197+
char *tok[3];
198+
int i = 1;
199+
tok[0] = strtok(str, "-");
200+
for (; i < 3; i++) {
201+
char *t = strtok(NULL, "-");
202+
if(t == NULL){
203+
break;
204+
}
205+
tok[i] = t;
206+
}
207+
if (i < 3) {
208+
return false;
209+
}
210+
211+
char *day = strtok(tok[2], "T");
212+
char *time = strtok(NULL, "T");
213+
214+
if(time == NULL){
215+
return false;
216+
}
217+
218+
char *hour = strtok(time, ":");
219+
220+
if(strlen(tok[0]) != 4 || strlen(tok[1]) != 2 || strlen(day) != 2 || strlen(hour) != 2){
221+
return false;
222+
}
223+
224+
_issueYear = atoi(tok[0]);
225+
_issueMonth = atoi(tok[1]);
226+
_issueDay = atoi(day);
227+
_issueHour = atoi(hour);
228+
229+
return true;
230+
}
231+
196232
CSRHandlerClass::CSRHandlerStates CSRHandlerClass::handleBuildCSR() {
197233
if (!_certForCSR) {
198234
_certForCSR = new ECP256Certificate();
@@ -296,7 +332,7 @@ CSRHandlerClass::CSRHandlerStates CSRHandlerClass::handleParseResponse() {
296332
if(i < 6 || strlen(token[0]) != 36 || strlen(token[1]) != 40
297333
|| strlen(token[2]) < 10 || strlen(token[3]) != 32
298334
|| strlen(token[4]) != 64 || strlen(token[5]) != 64
299-
|| sscanf(token[2], "%4d-%2d-%2dT%2d", &_issueYear, &_issueMonth, &_issueDay, &_issueHour) != 4){
335+
|| !parseDateFromStr(token[2])){
300336
updateNextRequestAt();
301337
DEBUG_ERROR("CSRH::%s Error parsing response, retrying in %d ms", __FUNCTION__, _nextRequestAt - millis());
302338
return CSRHandlerStates::REQUEST_SIGNATURE;

examples/utility/Provisioning_2.0/CSRHandler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class CSRHandlerClass {
6363
uint32_t jitter(uint32_t base = JITTER_BASE, uint32_t max = JITTER_MAX);
6464
bool postRequest(const char *url, String &postData);
6565
uint32_t getTimestamp();
66+
bool parseDateFromStr(char *str);
6667
CSRHandlerStates handleBuildCSR();
6768
CSRHandlerStates handleRequestSignature();
6869
CSRHandlerStates handleWaitingResponse();

0 commit comments

Comments
 (0)