Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dozord/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ on_message_t socket_message_callback(CommandResponse * response, uint8_t * data,
short int found = getNextCommandIdx(commands);
if (found != -1) {
snprintf(logMessage, sizeof(logMessage), "Sending cmd - \"%s\"", commands->items[found].value);
prettyLogger(LOG_LEVEL_INFO, "-", logMessage);
prettyLogger(LOG_LEVEL_INFO, clientIp, logMessage);
res = dozor_pack(response, crypto, commands->items[found].id, commands->items[found].value);

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

// @todo handle error code value
free(payload->deviceIp);
Expand Down
54 changes: 36 additions & 18 deletions libdozor/device-event.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,45 @@
#include "liblogger.h"
#include "device-event.h"

const unsigned char MSGDATASIZE[68] = {
static const unsigned char MSGDATASIZE[68] = {
// 0 - 4
0, 2, 2, 1, 1,
// 5 - 9
1, 1, 1, 1, 1,
// 10 - 14
1, 1, 1, 1, 1,
// 15 - 19
1, 1, 4, 0, 0,
// 20 - 24
0, 1, 1, 0, 0,
// 25 - 29
0, 1, 1, 1, 1,
// 30 - 34
1, 1, 1, 0, 2,
// 35 - 39
2, 4, 4, 0, 16,
// 40 - 44
1, 0, 17, 1, 1,
// 45 - 49
1, 1, 1, 1, 1,
// 50 - 54
1, 1, 1, 1, 1,
// 55 - 59
1, 1, 4, 4, 1,
// 60 - 64
1, 1, 1, 5, 3,
// 65 - 68
37, 4, 73 };

static void getDeviceEvent(DeviceEvent * deviceEvent, const uint8_t * raw, unsigned short int eventDataSize)
{
deviceEvent->type = raw[0];
deviceEvent->dataLength = eventDataSize;

memcpy(&deviceEvent->time, &raw[1], sizeof(deviceEvent->time));
memcpy(&deviceEvent->data, &raw[MESSAGE_ALIGN_SIZE], (size_t) eventDataSize);
}

unsigned short int getDeviceEvents(const uint8_t * raw, long int bufSize, DeviceEvent events[])
{
long int totalLength = bufSize;
Expand All @@ -52,7 +75,7 @@ unsigned short int getDeviceEvents(const uint8_t * raw, long int bufSize, Device
if (deviceEvent == NULL)
{
fprintf(stderr, "***device-event.c: Unable to allocate memory for device event: %s\n", strerror(errno));
return 64;
return 0;
}

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

getDeviceEvent(deviceEvent, &raw[index], eventSize);
memcpy(&events[eventCount], deviceEvent, sizeof(DeviceEvent));
char *hexStr = (char *)malloc(eventSize * 2 + 1);
if (hexStr) {

blobToHexStr(hexStr, &raw[index], eventSize);

logger(LOG_LEVEL_DEBUG, "device-event(getDeviceEvents)", "event id 0x%x: %s", raw[index], hexStr);

logger(LOG_LEVEL_DEBUG, "device-event(getDeviceEvents)", "event id 0x%x at position %d, size - %d\n", raw[index], index, eventSize);
free(hexStr);
}

getDeviceEvent(deviceEvent, &raw[index], MSGDATASIZE[raw[index]]);
memcpy(&events[eventCount], deviceEvent, sizeof(DeviceEvent));

eventCount += 1;
index = index + eventSize;
Expand All @@ -82,16 +113,3 @@ unsigned short int getDeviceEvents(const uint8_t * raw, long int bufSize, Device
free(deviceEvent);
return eventCount;
}

void getDeviceEvent(DeviceEvent * deviceEvent, const uint8_t * raw, unsigned short int eventSize)
{
unsigned short int eventDataSize = 0;

eventDataSize = eventSize - sizeof(deviceEvent->time);

deviceEvent->type = raw[0];
deviceEvent->dataLength = eventDataSize;

memcpy(&deviceEvent->time, &raw[5 + eventSize - sizeof(deviceEvent->time)], sizeof(deviceEvent->time));
memcpy(&deviceEvent->data, &raw[5], (size_t) eventDataSize);
}
2 changes: 1 addition & 1 deletion libdozor/device-event.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#define MAX_DEVICE_EVENT_DATA_SIZE 80
#define MAX_EVENTS_PER_DEVICE 256
// 4 bytes for date, 1 byte for event type id
#define MESSAGE_ALIGN_SIZE 5

typedef struct EVENT {
Expand All @@ -32,6 +33,5 @@ typedef struct EVENT {
} DeviceEvent;

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

#endif
81 changes: 56 additions & 25 deletions libdozor/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,19 @@ void convertDeviceEventToCommon(EventInfo* eventInfo, uint8_t site, DeviceEvent*
eventInfo->eventType = ENUM_EVENT_TYPE_COMMONEVENT;

const char * template = "{\"site\":%d,\"typeId\":%d,\"timestamp\":\"%s\",\"data\":\"";
char * timestamp = malloc(sizeof(char) * 25);
char * timestamp = "";
char * res = malloc(sizeof(char) * 1024);
unsigned short int index;
char * temp = malloc(sizeof(char) * 3);

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

char * parsedEvent;
char * parsedSubEvent;

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

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

Expand All @@ -241,12 +243,14 @@ void convertDeviceEventToCommon(EventInfo* eventInfo, uint8_t site, DeviceEvent*
case 0xd:
case 0xf:
logger(LOG_LEVEL_DEBUG, "event.c", "handle zone event");
parsedEvent = getZoneEventData(deviceEvent->type, deviceEvent->data, deviceEvent->dataLength);

strcat(res, parsedEvent);
sprintf(eventInfo->sourceId, "%s", getData(deviceEvent->data, DEFAULT_DATA_POSITION, deviceEvent->dataLength));
parsedSubEvent = getZoneEventData(deviceEvent->type, deviceEvent->data, deviceEvent->dataLength);
parsedEvent = getData(deviceEvent->data, DEFAULT_DATA_POSITION, deviceEvent->dataLength);

strcat(res, parsedSubEvent);
sprintf(eventInfo->sourceId, "%s", parsedEvent);
eventInfo->eventType = ENUM_EVENT_TYPE_ZONEINFO;

free(parsedSubEvent);
free(parsedEvent);

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

strcat(res, parsedEvent);
sprintf(eventInfo->sourceId, "%s", getData(deviceEvent->data, DEFAULT_DATA_POSITION, deviceEvent->dataLength));
strcat(res, parsedSubEvent);
sprintf(eventInfo->sourceId, "%s", parsedEvent);
eventInfo->eventType = ENUM_EVENT_TYPE_SECTIONINFO;

free(parsedSubEvent);
free(parsedEvent);

break;

// AuthenticationEvent
case 0x1b:
logger(LOG_LEVEL_DEBUG, "event.c", "handling authentication event");
parsedEvent = getAuthEventData(deviceEvent->type, deviceEvent->data, deviceEvent->dataLength);
strcat(res, parsedEvent);
sprintf(eventInfo->sourceId, "%s", getData(deviceEvent->data, DEFAULT_DATA_POSITION, deviceEvent->dataLength));
parsedSubEvent = getAuthEventData(deviceEvent->type, deviceEvent->data, deviceEvent->dataLength);
parsedEvent = getData(deviceEvent->data, DEFAULT_DATA_POSITION, deviceEvent->dataLength);

strcat(res, parsedSubEvent);
sprintf(eventInfo->sourceId, "%s", parsedEvent);

free(parsedSubEvent);
free(parsedEvent);

break;
Expand All @@ -284,13 +293,16 @@ void convertDeviceEventToCommon(EventInfo* eventInfo, uint8_t site, DeviceEvent*
case 0x39:
case 0x3a:
logger(LOG_LEVEL_DEBUG, "event.c", "handling arm/disarm event");
parsedEvent = getSecurityEventData(deviceEvent->type, deviceEvent->data, deviceEvent->dataLength);
strcat(res, parsedEvent);
sprintf(eventInfo->sourceId, "%s", getData(deviceEvent->data, USER_DATA_POSITION, deviceEvent->dataLength));
parsedSubEvent = getSecurityEventData(deviceEvent->type, deviceEvent->data, deviceEvent->dataLength);
parsedEvent = getData(deviceEvent->data, USER_DATA_POSITION, deviceEvent->dataLength);

strcat(res, parsedSubEvent);
sprintf(eventInfo->sourceId, "%s", parsedEvent);
eventInfo->eventType = ENUM_EVENT_TYPE_ARM_DISARM;

free(parsedSubEvent);
free(parsedEvent);

break;

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

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

free(subVersion);
free(subVersionData);

return res;
}

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

free(cmdResult);
free(cmdId);

return res;
}

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

sprintf(res, template, getData(data, REPORT_TEMP_DATA_POSITION, len), getEventNameByType(type));
sprintf(res, template, extractedData, getEventNameByType(type));

free(extractedData);

return res;
}

static char * getAuthEventData(uint8_t type, uint8_t * data, uint8_t len)
{
char * template = ",\"user\":%s,\"event\":\"%s\",\"scope\":\"Auth\"";
char * res = malloc(sizeof(char) * (strlen(template) + MAX_EVENT_NAME_LENGTH + 2));
char * extractedData = getData(data, DEFAULT_DATA_POSITION, len);

sprintf(res, template, extractedData, getEventNameByType(type));

sprintf(res, template, getData(data, DEFAULT_DATA_POSITION, len), getEventNameByType(type));
free(extractedData);

return res;
}
Expand All @@ -414,28 +439,34 @@ static char * getSecurityEventData(uint8_t type, uint8_t * data, uint8_t len)
{
char * template = ",\"user\":%s,\"event\":\"%s\",\"scope\":\"Security\"";
char * res = malloc(sizeof(char) * (strlen(template) + MAX_EVENT_NAME_LENGTH + 2));

sprintf(res, template, getData(data, USER_DATA_POSITION, len), getEventNameByType(type));
char * extractedData = getData(data, USER_DATA_POSITION, len);

sprintf(res, template, extractedData, getEventNameByType(type));

free(extractedData);
return res;
}

static char * getZoneEventData(uint8_t type, uint8_t * data, uint8_t len)
{
char * template = ",\"zone\":%s,\"event\":\"%s\",\"scope\":\"Zone\"";
char * res = malloc(sizeof(char) * (strlen(template) + MAX_EVENT_NAME_LENGTH + 2));

sprintf(res, template, getData(data, DEFAULT_DATA_POSITION, len), getEventNameByType(type));
char * extractedData = getData(data, DEFAULT_DATA_POSITION, len);
sprintf(res, template, extractedData, getEventNameByType(type));

free(extractedData);
return res;
}

static char * getSectionEventData(uint8_t type, uint8_t * data, uint8_t len)
{
char * template = ",\"section\":%s,\"event\":\"%s\",\"scope\":\"Section\"";
char * res = malloc(sizeof(char) * (strlen(template) + MAX_EVENT_NAME_LENGTH + 2));
char * extractedData = getData(data, DEFAULT_DATA_POSITION, len);

sprintf(res, template, extractedData, getEventNameByType(type));

sprintf(res, template, getData(data, DEFAULT_DATA_POSITION, len), getEventNameByType(type));
free(extractedData);

return res;
}
Expand Down
Loading
Loading