Skip to content

Commit 7d0dc6b

Browse files
authored
Improve event timestamp handling, fix memory leaks (#10)
* Fix incoming command handler * Fix memory leaks * Small improvements * Make logger input static * Make getDateTime thread safe * Fix event date timestamp issue * Fix event handling memory leaks * Extract to DEBUG mode logging
1 parent 0650cfe commit 7d0dc6b

File tree

8 files changed

+117
-89
lines changed

8 files changed

+117
-89
lines changed

dozord/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ on_message_t socket_message_callback(CommandResponse * response, uint8_t * data,
184184
short int found = getNextCommandIdx(commands);
185185
if (found != -1) {
186186
snprintf(logMessage, sizeof(logMessage), "Sending cmd - \"%s\"", commands->items[found].value);
187-
prettyLogger(LOG_LEVEL_INFO, "-", logMessage);
187+
prettyLogger(LOG_LEVEL_INFO, clientIp, logMessage);
188188
res = dozor_pack(response, crypto, commands->items[found].id, commands->items[found].value);
189189

190190
// @todo mark command as "sent", make it "done" once device returns command execution result
@@ -213,7 +213,7 @@ on_message_t socket_message_callback(CommandResponse * response, uint8_t * data,
213213
}
214214
} else {
215215
snprintf(logMessage, sizeof(logMessage), "Unable to unpack events! Error code - %d. Freeing memory...", events->errorCode);
216-
prettyLogger(LOG_LEVEL_DEBUG, "-", logMessage);
216+
prettyLogger(LOG_LEVEL_INFO, clientIp, logMessage);
217217

218218
// @todo handle error code value
219219
free(payload->deviceIp);

libdozor/device-event.c

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,45 @@
2323
#include "liblogger.h"
2424
#include "device-event.h"
2525

26-
const unsigned char MSGDATASIZE[68] = {
26+
static const unsigned char MSGDATASIZE[68] = {
27+
// 0 - 4
2728
0, 2, 2, 1, 1,
29+
// 5 - 9
2830
1, 1, 1, 1, 1,
31+
// 10 - 14
2932
1, 1, 1, 1, 1,
33+
// 15 - 19
3034
1, 1, 4, 0, 0,
35+
// 20 - 24
3136
0, 1, 1, 0, 0,
37+
// 25 - 29
3238
0, 1, 1, 1, 1,
39+
// 30 - 34
3340
1, 1, 1, 0, 2,
41+
// 35 - 39
3442
2, 4, 4, 0, 16,
43+
// 40 - 44
3544
1, 0, 17, 1, 1,
45+
// 45 - 49
3646
1, 1, 1, 1, 1,
47+
// 50 - 54
3748
1, 1, 1, 1, 1,
49+
// 55 - 59
3850
1, 1, 4, 4, 1,
51+
// 60 - 64
3952
1, 1, 1, 5, 3,
53+
// 65 - 68
4054
37, 4, 73 };
4155

56+
static void getDeviceEvent(DeviceEvent * deviceEvent, const uint8_t * raw, unsigned short int eventDataSize)
57+
{
58+
deviceEvent->type = raw[0];
59+
deviceEvent->dataLength = eventDataSize;
60+
61+
memcpy(&deviceEvent->time, &raw[1], sizeof(deviceEvent->time));
62+
memcpy(&deviceEvent->data, &raw[MESSAGE_ALIGN_SIZE], (size_t) eventDataSize);
63+
}
64+
4265
unsigned short int getDeviceEvents(const uint8_t * raw, long int bufSize, DeviceEvent events[])
4366
{
4467
long int totalLength = bufSize;
@@ -52,7 +75,7 @@ unsigned short int getDeviceEvents(const uint8_t * raw, long int bufSize, Device
5275
if (deviceEvent == NULL)
5376
{
5477
fprintf(stderr, "***device-event.c: Unable to allocate memory for device event: %s\n", strerror(errno));
55-
return 64;
78+
return 0;
5679
}
5780

5881
memset(deviceEvent, 0, sizeof(DeviceEvent));
@@ -69,10 +92,18 @@ unsigned short int getDeviceEvents(const uint8_t * raw, long int bufSize, Device
6992
{
7093
eventSize = MSGDATASIZE[raw[index]] + MESSAGE_ALIGN_SIZE;
7194

72-
getDeviceEvent(deviceEvent, &raw[index], eventSize);
73-
memcpy(&events[eventCount], deviceEvent, sizeof(DeviceEvent));
95+
char *hexStr = (char *)malloc(eventSize * 2 + 1);
96+
if (hexStr) {
97+
98+
blobToHexStr(hexStr, &raw[index], eventSize);
99+
100+
logger(LOG_LEVEL_DEBUG, "device-event(getDeviceEvents)", "event id 0x%x: %s", raw[index], hexStr);
74101

75-
logger(LOG_LEVEL_DEBUG, "device-event(getDeviceEvents)", "event id 0x%x at position %d, size - %d\n", raw[index], index, eventSize);
102+
free(hexStr);
103+
}
104+
105+
getDeviceEvent(deviceEvent, &raw[index], MSGDATASIZE[raw[index]]);
106+
memcpy(&events[eventCount], deviceEvent, sizeof(DeviceEvent));
76107

77108
eventCount += 1;
78109
index = index + eventSize;
@@ -82,16 +113,3 @@ unsigned short int getDeviceEvents(const uint8_t * raw, long int bufSize, Device
82113
free(deviceEvent);
83114
return eventCount;
84115
}
85-
86-
void getDeviceEvent(DeviceEvent * deviceEvent, const uint8_t * raw, unsigned short int eventSize)
87-
{
88-
unsigned short int eventDataSize = 0;
89-
90-
eventDataSize = eventSize - sizeof(deviceEvent->time);
91-
92-
deviceEvent->type = raw[0];
93-
deviceEvent->dataLength = eventDataSize;
94-
95-
memcpy(&deviceEvent->time, &raw[5 + eventSize - sizeof(deviceEvent->time)], sizeof(deviceEvent->time));
96-
memcpy(&deviceEvent->data, &raw[5], (size_t) eventDataSize);
97-
}

libdozor/device-event.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#define MAX_DEVICE_EVENT_DATA_SIZE 80
2424
#define MAX_EVENTS_PER_DEVICE 256
25+
// 4 bytes for date, 1 byte for event type id
2526
#define MESSAGE_ALIGN_SIZE 5
2627

2728
typedef struct EVENT {
@@ -32,6 +33,5 @@ typedef struct EVENT {
3233
} DeviceEvent;
3334

3435
unsigned short int getDeviceEvents(const uint8_t * raw, long int bufSize, DeviceEvent * events);
35-
void getDeviceEvent(DeviceEvent *, const uint8_t *, unsigned short int eventSize);
3636

3737
#endif

libdozor/event.c

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,19 @@ void convertDeviceEventToCommon(EventInfo* eventInfo, uint8_t site, DeviceEvent*
209209
eventInfo->eventType = ENUM_EVENT_TYPE_COMMONEVENT;
210210

211211
const char * template = "{\"site\":%d,\"typeId\":%d,\"timestamp\":\"%s\",\"data\":\"";
212-
char * timestamp = malloc(sizeof(char) * 25);
212+
char * timestamp = "";
213213
char * res = malloc(sizeof(char) * 1024);
214214
unsigned short int index;
215215
char * temp = malloc(sizeof(char) * 3);
216216

217217
uint8_t * ptr = (uint8_t*) deviceEvent->data;
218218

219219
char * parsedEvent;
220+
char * parsedSubEvent;
220221

221-
memcpy(timestamp, getDateTime(deviceEvent->time), sizeof(char) * 25);
222-
timestamp[24] = 0x0;
222+
if (deviceEvent->time != 0x00000000) {
223+
timestamp = getDateTime(deviceEvent->time);
224+
}
223225

224226
sprintf(res, template, site, deviceEvent->type, timestamp);
225227

@@ -241,12 +243,14 @@ void convertDeviceEventToCommon(EventInfo* eventInfo, uint8_t site, DeviceEvent*
241243
case 0xd:
242244
case 0xf:
243245
logger(LOG_LEVEL_DEBUG, "event.c", "handle zone event");
244-
parsedEvent = getZoneEventData(deviceEvent->type, deviceEvent->data, deviceEvent->dataLength);
245-
246-
strcat(res, parsedEvent);
247-
sprintf(eventInfo->sourceId, "%s", getData(deviceEvent->data, DEFAULT_DATA_POSITION, deviceEvent->dataLength));
246+
parsedSubEvent = getZoneEventData(deviceEvent->type, deviceEvent->data, deviceEvent->dataLength);
247+
parsedEvent = getData(deviceEvent->data, DEFAULT_DATA_POSITION, deviceEvent->dataLength);
248+
249+
strcat(res, parsedSubEvent);
250+
sprintf(eventInfo->sourceId, "%s", parsedEvent);
248251
eventInfo->eventType = ENUM_EVENT_TYPE_ZONEINFO;
249252

253+
free(parsedSubEvent);
250254
free(parsedEvent);
251255

252256
break;
@@ -259,23 +263,28 @@ void convertDeviceEventToCommon(EventInfo* eventInfo, uint8_t site, DeviceEvent*
259263
case 0x35:
260264
case 0x37:
261265
logger(LOG_LEVEL_DEBUG, "event.c", "handling section event\n");
262-
parsedEvent = getSectionEventData(deviceEvent->type, deviceEvent->data, deviceEvent->dataLength);
266+
parsedSubEvent = getSectionEventData(deviceEvent->type, deviceEvent->data, deviceEvent->dataLength);
267+
parsedEvent = getData(deviceEvent->data, DEFAULT_DATA_POSITION, deviceEvent->dataLength);
263268

264-
strcat(res, parsedEvent);
265-
sprintf(eventInfo->sourceId, "%s", getData(deviceEvent->data, DEFAULT_DATA_POSITION, deviceEvent->dataLength));
269+
strcat(res, parsedSubEvent);
270+
sprintf(eventInfo->sourceId, "%s", parsedEvent);
266271
eventInfo->eventType = ENUM_EVENT_TYPE_SECTIONINFO;
267272

273+
free(parsedSubEvent);
268274
free(parsedEvent);
269275

270276
break;
271277

272278
// AuthenticationEvent
273279
case 0x1b:
274280
logger(LOG_LEVEL_DEBUG, "event.c", "handling authentication event");
275-
parsedEvent = getAuthEventData(deviceEvent->type, deviceEvent->data, deviceEvent->dataLength);
276-
strcat(res, parsedEvent);
277-
sprintf(eventInfo->sourceId, "%s", getData(deviceEvent->data, DEFAULT_DATA_POSITION, deviceEvent->dataLength));
281+
parsedSubEvent = getAuthEventData(deviceEvent->type, deviceEvent->data, deviceEvent->dataLength);
282+
parsedEvent = getData(deviceEvent->data, DEFAULT_DATA_POSITION, deviceEvent->dataLength);
283+
284+
strcat(res, parsedSubEvent);
285+
sprintf(eventInfo->sourceId, "%s", parsedEvent);
278286

287+
free(parsedSubEvent);
279288
free(parsedEvent);
280289

281290
break;
@@ -284,13 +293,16 @@ void convertDeviceEventToCommon(EventInfo* eventInfo, uint8_t site, DeviceEvent*
284293
case 0x39:
285294
case 0x3a:
286295
logger(LOG_LEVEL_DEBUG, "event.c", "handling arm/disarm event");
287-
parsedEvent = getSecurityEventData(deviceEvent->type, deviceEvent->data, deviceEvent->dataLength);
288-
strcat(res, parsedEvent);
289-
sprintf(eventInfo->sourceId, "%s", getData(deviceEvent->data, USER_DATA_POSITION, deviceEvent->dataLength));
296+
parsedSubEvent = getSecurityEventData(deviceEvent->type, deviceEvent->data, deviceEvent->dataLength);
297+
parsedEvent = getData(deviceEvent->data, USER_DATA_POSITION, deviceEvent->dataLength);
298+
299+
strcat(res, parsedSubEvent);
300+
sprintf(eventInfo->sourceId, "%s", parsedEvent);
290301
eventInfo->eventType = ENUM_EVENT_TYPE_ARM_DISARM;
291302

303+
free(parsedSubEvent);
292304
free(parsedEvent);
293-
305+
294306
break;
295307

296308
// SecurityEvent
@@ -363,11 +375,15 @@ static char * getFirmwareVersionEventData(uint8_t type, uint8_t * data, uint8_t
363375
char * template = ",\"event\":\"%s\",\"scope\":\"Common\",\"version\":\"%ld.%s\"";
364376
char * res;
365377
char * subVersion = getData(data, DEFAULT_DATA_POSITION, len);
366-
long int version = strtol(getData(data, VERSION_DATA_POSITION, len), 0, 10) & VERSION_MASK;
378+
char * subVersionData = getData(data, VERSION_DATA_POSITION, len);
379+
long int version = strtol(subVersionData, 0, 10) & VERSION_MASK;
367380

368381
res = malloc(sizeof(char) * (strlen(template) + MAX_EVENT_NAME_LENGTH + 4));
369382
sprintf(res, template, getEventNameByType(type), version, subVersion);
370383

384+
free(subVersion);
385+
free(subVersionData);
386+
371387
return res;
372388
}
373389

@@ -387,25 +403,34 @@ static char * getCommandEventData(uint8_t type, uint8_t * data, uint8_t len)
387403
res = malloc(sizeof(char) * (strlen(template) + MAX_EVENT_NAME_LENGTH + MAX_COMMAND_RESULT_NAME_LENGTH + 4));
388404
sprintf(res, template, getEventNameByType(type), cmdId, cmdResult, cmdResultName);
389405

406+
free(cmdResult);
407+
free(cmdId);
408+
390409
return res;
391410
}
392411

393412
static char * getReportEventData(uint8_t type, uint8_t * data, uint8_t len)
394413
{
395414
char * template = ",\"temp\":%s,\"event\":\"%s\",\"scope\":\"Common\"";
396415
char * res = malloc(sizeof(char) * (strlen(template) + MAX_EVENT_NAME_LENGTH + 2));
416+
char * extractedData = getData(data, REPORT_TEMP_DATA_POSITION, len);
397417

398-
sprintf(res, template, getData(data, REPORT_TEMP_DATA_POSITION, len), getEventNameByType(type));
418+
sprintf(res, template, extractedData, getEventNameByType(type));
399419

420+
free(extractedData);
421+
400422
return res;
401423
}
402424

403425
static char * getAuthEventData(uint8_t type, uint8_t * data, uint8_t len)
404426
{
405427
char * template = ",\"user\":%s,\"event\":\"%s\",\"scope\":\"Auth\"";
406428
char * res = malloc(sizeof(char) * (strlen(template) + MAX_EVENT_NAME_LENGTH + 2));
429+
char * extractedData = getData(data, DEFAULT_DATA_POSITION, len);
430+
431+
sprintf(res, template, extractedData, getEventNameByType(type));
407432

408-
sprintf(res, template, getData(data, DEFAULT_DATA_POSITION, len), getEventNameByType(type));
433+
free(extractedData);
409434

410435
return res;
411436
}
@@ -414,28 +439,34 @@ static char * getSecurityEventData(uint8_t type, uint8_t * data, uint8_t len)
414439
{
415440
char * template = ",\"user\":%s,\"event\":\"%s\",\"scope\":\"Security\"";
416441
char * res = malloc(sizeof(char) * (strlen(template) + MAX_EVENT_NAME_LENGTH + 2));
417-
418-
sprintf(res, template, getData(data, USER_DATA_POSITION, len), getEventNameByType(type));
442+
char * extractedData = getData(data, USER_DATA_POSITION, len);
443+
444+
sprintf(res, template, extractedData, getEventNameByType(type));
419445

446+
free(extractedData);
420447
return res;
421448
}
422449

423450
static char * getZoneEventData(uint8_t type, uint8_t * data, uint8_t len)
424451
{
425452
char * template = ",\"zone\":%s,\"event\":\"%s\",\"scope\":\"Zone\"";
426453
char * res = malloc(sizeof(char) * (strlen(template) + MAX_EVENT_NAME_LENGTH + 2));
427-
428-
sprintf(res, template, getData(data, DEFAULT_DATA_POSITION, len), getEventNameByType(type));
454+
char * extractedData = getData(data, DEFAULT_DATA_POSITION, len);
455+
sprintf(res, template, extractedData, getEventNameByType(type));
429456

457+
free(extractedData);
430458
return res;
431459
}
432460

433461
static char * getSectionEventData(uint8_t type, uint8_t * data, uint8_t len)
434462
{
435463
char * template = ",\"section\":%s,\"event\":\"%s\",\"scope\":\"Section\"";
436464
char * res = malloc(sizeof(char) * (strlen(template) + MAX_EVENT_NAME_LENGTH + 2));
465+
char * extractedData = getData(data, DEFAULT_DATA_POSITION, len);
466+
467+
sprintf(res, template, extractedData, getEventNameByType(type));
437468

438-
sprintf(res, template, getData(data, DEFAULT_DATA_POSITION, len), getEventNameByType(type));
469+
free(extractedData);
439470

440471
return res;
441472
}

0 commit comments

Comments
 (0)