//---------------------------------------------------------------------------- // Dr.Frucht Systems Ltd. // Copyright © 2000-2016. All Rights Reserved. // Version 2.0 3/30/2016 // Last modification: 3/30/2016 // PROJECT: // SUBSYSTEM: // FILE: basicEvent.cpp // AUTHOR: gagua // OVERVIEW // ~~~~~~~~ // Header file of basicEvent.cpp //---------------------------------------------------------------------------- // #include "basicEvent.h" #include "basicFunctionality.h" void utilizeBasicEvent(EVENT_CONTAINER & Evt) { if (Evt.m_pHumanReadableMsg != NULL) { free( Evt.m_pHumanReadableMsg ); // The mem is allocated by malloc!! Evt.m_pHumanReadableMsg = NULL; }; if (Evt.m_pEvtData != NULL) { switch (Evt.m_eType) { case EVT_FH_MISSION_BEGIN: case EVT_FH_BUSY: case EVT_FH_CANCEL_ACTION : free(Evt.m_pEvtData); break; // allocated with malloc everywhere case EVT_FH_FLY_TO : case EVT_FH_WAYPOINT : case EVT_FH_DEST_REACHED : case EVT_FH_JAMER_ON : case EVT_FH_JAMER_OFF : case EVT_FH_TARGET_LOCKED : case EVT_FH_INTERCEPT_BEGIN: case EVT_FH_INTERCEPT_OK : case EVT_FH_INTERCEPT_NOK : case EVT_FH_MISSION_END : free(Evt.m_pEvtData); break; // allocated with malloc everywhere case EVT_TARGET_1D: case EVT_TARGET_2D_WHITE: case EVT_TARGET_2D_BLACK: case EVT_TARGET_3D_WHITE: case EVT_TARGET_3D_BLACK: case EVT_CAMERA_MOVE_TO: { free(Evt.m_pEvtData); // allocated with malloc everywhere (delete was an allocator mismatch) break; }; default: free(Evt.m_pEvtData); }; }; Evt.m_pEvtData = NULL; Evt.m_eType = EVT_NOINIT; } void utilizeBasicEvent(EVENT_CONTAINER *& Evt) { if (Evt == NULL) { return; } if (Evt->m_pHumanReadableMsg != NULL) { free( Evt->m_pHumanReadableMsg ); // The mem is allocated by malloc!! Evt->m_pHumanReadableMsg = NULL; }; if (Evt->m_pEvtData != NULL) { switch (Evt->m_eType) { case EVT_FH_MISSION_BEGIN: case EVT_FH_BUSY: case EVT_FH_CANCEL_ACTION : free(Evt->m_pEvtData); break; // allocated with malloc everywhere case EVT_FH_FLY_TO : case EVT_FH_WAYPOINT : case EVT_FH_DEST_REACHED : case EVT_FH_JAMER_ON : case EVT_FH_JAMER_OFF : case EVT_FH_TARGET_LOCKED : case EVT_FH_INTERCEPT_BEGIN: case EVT_FH_INTERCEPT_OK : case EVT_FH_INTERCEPT_NOK : case EVT_FH_MISSION_END : free(Evt->m_pEvtData); break; // allocated with malloc everywhere case EVT_TARGET_1D: case EVT_TARGET_2D_WHITE: case EVT_TARGET_2D_BLACK: case EVT_TARGET_3D_WHITE: case EVT_TARGET_3D_BLACK: case EVT_CAMERA_MOVE_TO: { free(Evt->m_pEvtData); // allocated with malloc everywhere (delete was an allocator mismatch) break; }; default: free(Evt->m_pEvtData); }; }; free(Evt); Evt = NULL; } static uint32_t EventSeed_Counter = 0; EVENT_CONTAINER * createBasicEvent(uint64_t SRC, basicEventClassID Class, basicEventTypeID Type, char * RefUUID, char * Msg, void * Data, uint32_t tss, uint16_t tms) { EVENT_CONTAINER * tEvent; tEvent = (EVENT_CONTAINER *) malloc(sizeof(EVENT_CONTAINER)); if (tEvent) { if (tss == 0) { time_t t = time(NULL); localtime_r( &t, &tEvent->sTS );// MT safe tEvent->m_UMSTS = getMilliseconds(); } else { time_t t = (time_t) tss; localtime_r( &t, &tEvent->sTS );// MT safe tEvent->m_UMSTS = tss; tEvent->m_UMSTS = tEvent->m_UMSTS * 1000 + tms; }; tEvent->m_eClass = Class; CSTR tUUID = getUniqID_UUID(EventSeed_Counter++); if (tUUID) { sprintf(tEvent->m_UUID, "%s", tUUID); free(tUUID); } else { memset(tEvent->m_UUID, 0, 65); }; tEvent->m_ref_UUID[0] = (char) 0; // malloc'ed struct: was left uninitialised without RefUUID tEvent->m_db_id = 0; tEvent->m_ref_id = 0; if (RefUUID) { snprintf(tEvent->m_ref_UUID, sizeof(tEvent->m_ref_UUID), "%s", RefUUID); }; tEvent->m_eType = Type; tEvent->m_nDestinationNodeID = 0; // Broadcast tEvent->m_nSourceNodeID = SRC; tEvent->m_pEvtData = Data; if (Msg != NULL) { tEvent->m_pHumanReadableMsg = (char *) malloc(strlen(Msg) + 1); if (tEvent->m_pHumanReadableMsg) { strcpy(tEvent->m_pHumanReadableMsg, Msg); }; } else { tEvent->m_pHumanReadableMsg = NULL; }; }; return tEvent; } EVENT_CONTAINER * createBasicTargetedEvent(uint64_t SRC, uint64_t DST, basicEventClassID Class, basicEventTypeID Type, char * RefUUID, char * Msg, void * Data, uint32_t tss, uint16_t tms) { EVENT_CONTAINER * tEvent; tEvent = (EVENT_CONTAINER *) malloc(sizeof(EVENT_CONTAINER)); if (tEvent) { if (tss == 0) { time_t t = time(NULL); localtime_r( &t, &tEvent->sTS );// MT safe tEvent->m_UMSTS = getMilliseconds(); } else { time_t t = (time_t) tss; localtime_r( &t, &tEvent->sTS );// MT safe tEvent->m_UMSTS = tss; tEvent->m_UMSTS = tEvent->m_UMSTS * 1000 + tms; }; tEvent->m_eClass = Class; CSTR tUUID = getUniqID_UUID(); if (tUUID) { sprintf(tEvent->m_UUID, "%s", tUUID); free(tUUID); } else { memset(tEvent->m_UUID, 0, 65); }; tEvent->m_ref_UUID[0] = (char) 0; // malloc'ed struct: was left uninitialised without RefUUID tEvent->m_db_id = 0; tEvent->m_ref_id = 0; if (RefUUID) { snprintf(tEvent->m_ref_UUID, sizeof(tEvent->m_ref_UUID), "%s", RefUUID); }; tEvent->m_eType = Type; tEvent->m_nDestinationNodeID = DST; tEvent->m_nSourceNodeID = SRC; tEvent->m_pEvtData = Data; if (Msg != NULL) { tEvent->m_pHumanReadableMsg = (char *) malloc(strlen(Msg) + 1); if (tEvent->m_pHumanReadableMsg) { strcpy(tEvent->m_pHumanReadableMsg, Msg); }; } else { tEvent->m_pHumanReadableMsg = NULL; }; }; return tEvent; }