From 443074f77953c29d78457f346fc694182d0f1920 Mon Sep 17 00:00:00 2001 From: Michael Zillgith Date: Thu, 18 Dec 2025 18:08:26 +0000 Subject: [PATCH] - changed server side and GOOSE pub/sub timeout handling from real time clock to use monotonic clock (LIB61850-535) --- src/goose/goose_publisher.c | 10 +- src/goose/goose_receiver.c | 20 ++-- src/goose/goose_receiver_internal.h | 2 +- src/goose/goose_subscriber.c | 2 +- src/iec61850/inc_private/control.h | 4 +- .../inc_private/mms_mapping_internal.h | 3 +- src/iec61850/server/mms_mapping/control.c | 93 +++++++++++-------- src/iec61850/server/mms_mapping/logging.c | 8 +- src/iec61850/server/mms_mapping/mms_mapping.c | 25 +++-- src/iec61850/server/mms_mapping/reporting.c | 58 ++++++------ src/mms/iso_mms/server/mms_file_service.c | 14 +-- 11 files changed, 132 insertions(+), 107 deletions(-) diff --git a/src/goose/goose_publisher.c b/src/goose/goose_publisher.c index fa276ec1..533aa2e4 100644 --- a/src/goose/goose_publisher.c +++ b/src/goose/goose_publisher.c @@ -1,7 +1,7 @@ /* * goose_publisher.c * - * Copyright 2013-2024 Michael Zillgith + * Copyright 2013-2025 Michael Zillgith * * This file is part of libIEC61850. * @@ -79,7 +79,8 @@ GoosePublisher_createRemote(RSession session, uint16_t appId) { GoosePublisher self = (GoosePublisher) GLOBAL_CALLOC(1, sizeof(struct sGoosePublisher)); - if (self) { + if (self) + { self->remoteSession = session; self->buffer = (uint8_t*) GLOBAL_MALLOC(GOOSE_MAX_MESSAGE_SIZE); @@ -139,7 +140,8 @@ GoosePublisher_destroy(GoosePublisher self) if (self) { #if (CONFIG_IEC61850_L2_GOOSE == 1) - if (self->ethernetSocket) { + if (self->ethernetSocket) + { Ethernet_destroySocket(self->ethernetSocket); } #endif /* (CONFIG_IEC61850_L2_GOOSE == 1) */ @@ -298,7 +300,7 @@ prepareGooseBuffer(GoosePublisher self, CommParameters* parameters, const char* int bufPos = 12; - if (useVlanTags) + if (useVlanTags) { /* Priority tag - IEEE 802.1Q */ self->buffer[bufPos++] = 0x81; diff --git a/src/goose/goose_receiver.c b/src/goose/goose_receiver.c index a9c8d690..3164c114 100644 --- a/src/goose/goose_receiver.c +++ b/src/goose/goose_receiver.c @@ -1,7 +1,7 @@ /* * goose_receiver.c * - * Copyright 2014-2024 Michael Zillgith + * Copyright 2014-2025 Michael Zillgith * * This file is part of libIEC61850. * @@ -100,6 +100,12 @@ GooseReceiver_create() if (self) { self->buffer = (uint8_t*) GLOBAL_MALLOC(ETH_BUFFER_LENGTH); + + if (self->buffer == NULL) + { + GooseReceiver_destroy(self); + return NULL; + } } return self; @@ -165,7 +171,7 @@ createNewStringFromBufferElement(MmsValue* value, uint8_t* bufferSrc, int elemen { if (DEBUG_GOOSE_SUBSCRIBER) printf("GOOSE_SUBSCRIBER: failed to allocate memory for visible string\n"); - + value->value.visibleString.size = 0; } } @@ -612,7 +618,7 @@ parseAllDataUnknownValue(GooseSubscriber self, uint8_t* buffer, int allDataLengt goto exit_with_error; } - else + else { value = MmsValue_newBitString(rawBitLength - padding); memcpy(value->value.bitString.buf, buffer + bufPos + 1, elementLength - 1); @@ -956,7 +962,7 @@ parseGoosePayload(GooseReceiver self, uint8_t* buffer, int apduLength) MmsValue_setUtcTime(matchingSubscriber->timestamp, 0); } - + if (matchingSubscriber->isObserver && matchingSubscriber->dataSetValues != NULL) { MmsValue_delete(matchingSubscriber->dataSetValues); @@ -990,7 +996,7 @@ parseGoosePayload(GooseReceiver self, uint8_t* buffer, int apduLength) matchingSubscriber->stNum = stNum; matchingSubscriber->sqNum = sqNum; - matchingSubscriber->invalidityTime = Hal_getTimeInMs() + timeAllowedToLive; + matchingSubscriber->invalidityTime = Hal_getMonotonicTimeInMs() + timeAllowedToLive; if (matchingSubscriber->listener != NULL) matchingSubscriber->listener(matchingSubscriber, matchingSubscriber->listenerParameter); @@ -1042,7 +1048,7 @@ parseGooseMessage(GooseReceiver self, uint8_t* buffer, int numbytes) return; if (buffer[bufPos++] != 0xb8) return; - + uint8_t srcMac[6]; memcpy(srcMac,&buffer[6],6); @@ -1094,7 +1100,7 @@ parseGooseMessage(GooseReceiver self, uint8_t* buffer, int numbytes) while (element) { GooseSubscriber subscriber = (GooseSubscriber) LinkedList_getData(element); - + if (subscriber->isObserver) { subscriber->appId = appId; diff --git a/src/goose/goose_receiver_internal.h b/src/goose/goose_receiver_internal.h index a429c675..8b63d8f9 100644 --- a/src/goose/goose_receiver_internal.h +++ b/src/goose/goose_receiver_internal.h @@ -47,7 +47,7 @@ struct sGooseSubscriber { bool simulation; bool ndsCom; - uint64_t invalidityTime; + uint64_t invalidityTime; /* time when the data became invalid as monotonic time in milliseconds */ bool stateValid; GooseParseError parseError; diff --git a/src/goose/goose_subscriber.c b/src/goose/goose_subscriber.c index 019c2b7e..6b43cd44 100644 --- a/src/goose/goose_subscriber.c +++ b/src/goose/goose_subscriber.c @@ -70,7 +70,7 @@ GooseSubscriber_isValid(GooseSubscriber self) if (self->stateValid == false) return false; - if (Hal_getTimeInMs() > self->invalidityTime) + if (Hal_getMonotonicTimeInMs() > self->invalidityTime) return false; return true; diff --git a/src/iec61850/inc_private/control.h b/src/iec61850/inc_private/control.h index adf56731..943b8ebd 100644 --- a/src/iec61850/inc_private/control.h +++ b/src/iec61850/inc_private/control.h @@ -111,8 +111,8 @@ struct sControlObject MmsValue* error; MmsValue* addCause; - uint64_t selectTime; - uint32_t selectTimeout; + uint64_t selectTime; /* select time of a control as monotonic time in ms */ + uint32_t selectTimeout; /* select timeout period in ms */ MmsValue* sboClass; MmsValue* sboTimeout; diff --git a/src/iec61850/inc_private/mms_mapping_internal.h b/src/iec61850/inc_private/mms_mapping_internal.h index 1830b87c..f0752bbc 100644 --- a/src/iec61850/inc_private/mms_mapping_internal.h +++ b/src/iec61850/inc_private/mms_mapping_internal.h @@ -285,7 +285,8 @@ struct sMmsMapping { #endif LinkedList controlObjects; - uint64_t nextControlTimeout; /* next timeout in one of the control state machines */ + uint64_t nextControlTimeout; /* next monotonic time timeout in one of the control state machines (e.g. for select timeout) */ + uint64_t nextRealTimeControlTimeout; /* next real time clock based timeout in one of the control state machines (e.g. for operTm)*/ LinkedList attributeAccessHandlers; diff --git a/src/iec61850/server/mms_mapping/control.c b/src/iec61850/server/mms_mapping/control.c index db741399..fc475c10 100644 --- a/src/iec61850/server/mms_mapping/control.c +++ b/src/iec61850/server/mms_mapping/control.c @@ -1,7 +1,7 @@ /* * control.c * - * Copyright 2013-2022 Michael Zillgith + * Copyright 2013-2025 Michael Zillgith * * This file is part of libIEC61850. * @@ -436,6 +436,13 @@ updateNextControlTimeout(MmsMapping* self, uint64_t timeout) self->nextControlTimeout = timeout; } +static void +updateNextRealTimeControlTimeout(MmsMapping* self, uint64_t timeout) +{ + if (timeout < self->nextRealTimeControlTimeout) + self->nextRealTimeControlTimeout = timeout; +} + static void setState(ControlObject* self, int newState) { @@ -554,7 +561,7 @@ unselectObject(ControlObject* self, SelectStateChangedReason reason, MmsMapping* } static void -checkSelectTimeout(ControlObject* self, uint64_t currentTime, MmsMapping* mmsMapping) +checkSelectTimeout(ControlObject* self, uint64_t currentMonotonicTimeInMs, MmsMapping* mmsMapping) { if ((self->ctlModel == 2) || (self->ctlModel == 4)) { @@ -562,7 +569,7 @@ checkSelectTimeout(ControlObject* self, uint64_t currentTime, MmsMapping* mmsMap { if (self->selectTimeout > 0) { - if (currentTime > (self->selectTime + self->selectTimeout)) + if (currentMonotonicTimeInMs > (self->selectTime + self->selectTimeout)) { if (DEBUG_IED_SERVER) printf("IED_SERVER: select-timeout (timeout-val = %u) for control %s/%s.%s\n", @@ -729,7 +736,7 @@ resetAddCause(ControlObject* self) } static void -executeControlTask(MmsMapping* self, ControlObject* controlObject, uint64_t currentTimeInMs) +executeControlTask(MmsMapping* self, ControlObject* controlObject, uint64_t currentMonotonicTimeInMs) { int state; @@ -761,7 +768,7 @@ executeStateMachine: { LinkedList_add(values, controlObject->sbo); - selectObject(controlObject, Hal_getTimeInMs(), controlObject->mmsConnection, self); + selectObject(controlObject, currentMonotonicTimeInMs, controlObject->mmsConnection, self); #if (CONFIG_IEC61850_SERVICE_TRACKING == 1) updateGenericTrackingObjectValues(self, controlObject, IEC61850_SERVICE_TYPE_SELECT, IEC61850_SERVICE_ERROR_NO_ERROR); @@ -787,7 +794,7 @@ executeStateMachine: { if (checkHandlerResult == CONTROL_ACCEPTED) { - selectObject(controlObject, Hal_getTimeInMs(), controlObject->mmsConnection, self); + selectObject(controlObject, currentMonotonicTimeInMs, controlObject->mmsConnection, self); if (controlObject->ctlNumSt) MmsValue_update(controlObject->ctlNumSt, controlObject->ctlNum); @@ -833,7 +840,7 @@ executeStateMachine: } else { - updateNextControlTimeout(self, Hal_getTimeInMs() + 100); + updateNextControlTimeout(self, currentMonotonicTimeInMs + 100); } } @@ -914,22 +921,20 @@ executeStateMachine: setState(controlObject, STATE_OPERATE); - setOpOk(controlObject, true, currentTimeInMs); + setOpOk(controlObject, true, Hal_getTimeInMs()); goto executeStateMachine; } else { - updateNextControlTimeout(self, Hal_getTimeInMs() + 10); + updateNextControlTimeout(self, currentMonotonicTimeInMs + 10); } } break; case STATE_OPERATE: { - uint64_t currentTime = Hal_getTimeInMs(); - - ControlHandlerResult result = operateControl(controlObject, controlObject->ctlVal, currentTime, controlObject->testMode); + ControlHandlerResult result = operateControl(controlObject, controlObject->ctlVal, currentMonotonicTimeInMs, controlObject->testMode); if (result != CONTROL_RESULT_WAITING) { @@ -965,13 +970,13 @@ executeStateMachine: exitControlTask(controlObject); - setOpOk(controlObject, false, currentTimeInMs); + setOpOk(controlObject, false, Hal_getTimeInMs()); resetAddCause(controlObject); } else { - updateNextControlTimeout(self, currentTimeInMs + 10); + updateNextControlTimeout(self, currentMonotonicTimeInMs + 10); } } break; @@ -1481,16 +1486,21 @@ ControlObject_updateControlModel(ControlObject* self, ControlModel value, DataOb } void -Control_processControlActions(MmsMapping* self, uint64_t currentTimeInMs) +Control_processControlActions(MmsMapping* self, uint64_t currentMonotonicTimeInMs) { - if (currentTimeInMs >= self->nextControlTimeout) + uint64_t currentRealTimeInMs = Hal_getTimeInMs(); + + if ((currentMonotonicTimeInMs >= self->nextControlTimeout) || (currentRealTimeInMs >= self->nextRealTimeControlTimeout)) { /* invalidate nextControlTimeout */ self->nextControlTimeout = (uint64_t) 0xFFFFFFFFFFFFFFFFLLU; + /* invalidate nextRealTimeControlTimeout */ + self->nextRealTimeControlTimeout = (uint64_t) 0xFFFFFFFFFFFFFFFFLLU; + LinkedList element = LinkedList_getNext(self->controlObjects); - while (element != NULL) + while (element) { ControlObject* controlObject = (ControlObject*) element->data; @@ -1507,7 +1517,7 @@ Control_processControlActions(MmsMapping* self, uint64_t currentTimeInMs) if (controlObject->state == STATE_WAIT_FOR_ACTIVATION_TIME) { - if (controlObject->operateTime <= currentTimeInMs) + if (controlObject->operateTime <= Hal_getTimeInMs()) { /* enter state Perform Test */ setOpRcvd(controlObject, true); @@ -1539,7 +1549,7 @@ Control_processControlActions(MmsMapping* self, uint64_t currentTimeInMs) /* leave state Perform Test */ setOpRcvd(controlObject, false); - executeControlTask(self, controlObject, currentTimeInMs); + executeControlTask(self, controlObject, currentMonotonicTimeInMs); } else { @@ -1560,18 +1570,21 @@ Control_processControlActions(MmsMapping* self, uint64_t currentTimeInMs) resetAddCause(controlObject); } } - else { - updateNextControlTimeout(self, controlObject->operateTime); + else + { + updateNextRealTimeControlTimeout(self, controlObject->operateTime); } } /* if (controlObject->state == STATE_WAIT_FOR_ACTICATION_TIME) */ - else if (!((controlObject->state == STATE_UNSELECTED) || (controlObject->state == STATE_READY))) { - executeControlTask(self, controlObject, currentTimeInMs); - } - else if (controlObject->state == STATE_READY) { - checkSelectTimeout(controlObject, currentTimeInMs, self); + else if (!((controlObject->state == STATE_UNSELECTED) || (controlObject->state == STATE_READY))) + { + executeControlTask(self, controlObject, currentMonotonicTimeInMs); } + else if (controlObject->state == STATE_READY) + { + checkSelectTimeout(controlObject, currentMonotonicTimeInMs, self); } + } ControlObject_handlePendingEvents(controlObject); @@ -1900,13 +1913,13 @@ Control_readAccessControlObject(MmsMapping* self, MmsDomain* domain, char* varia { if (controlObject->ctlModel == 2) { - uint64_t currentTime = Hal_getTimeInMs(); + uint64_t currentMonotonicTime = Hal_getMonotonicTimeInMs(); value = &emptyString; if (isDirectAccess == true) { - checkSelectTimeout(controlObject, currentTime, self); + checkSelectTimeout(controlObject, currentMonotonicTime, self); if (getState(controlObject) == STATE_UNSELECTED) { @@ -1931,7 +1944,7 @@ Control_readAccessControlObject(MmsMapping* self, MmsDomain* domain, char* varia if (checkResult == CONTROL_ACCEPTED) { - selectObject(controlObject, currentTime, connection, self); + selectObject(controlObject, currentMonotonicTime, connection, self); value = controlObject->sbo; #if (CONFIG_IEC61850_SERVICE_TRACKING == 1) @@ -2148,9 +2161,9 @@ Control_writeAccessControlObject(MmsMapping* self, MmsDomain* domain, const char int state = getState(controlObject); - uint64_t currentTime = Hal_getTimeInMs(); + uint64_t currentMonotonicTime = Hal_getMonotonicTimeInMs(); - checkSelectTimeout(controlObject, currentTime, self); + checkSelectTimeout(controlObject, currentMonotonicTime, self); if (state != STATE_UNSELECTED) { @@ -2212,7 +2225,7 @@ Control_writeAccessControlObject(MmsMapping* self, MmsDomain* domain, const char if (checkResult == CONTROL_ACCEPTED) { - selectObject(controlObject, currentTime, connection, self); + selectObject(controlObject, currentMonotonicTime, connection, self); indication = DATA_ACCESS_ERROR_SUCCESS; @@ -2230,7 +2243,7 @@ Control_writeAccessControlObject(MmsMapping* self, MmsDomain* domain, const char setState(controlObject, STATE_WAIT_FOR_SELECT); - updateNextControlTimeout(self, Hal_getTimeInMs() + 100); + updateNextControlTimeout(self, Hal_getMonotonicTimeInMs() + 100); indication = DATA_ACCESS_ERROR_NO_RESPONSE; } @@ -2295,9 +2308,7 @@ Control_writeAccessControlObject(MmsMapping* self, MmsDomain* domain, const char goto free_and_return; } - uint64_t currentTime = Hal_getTimeInMs(); - - checkSelectTimeout(controlObject, currentTime, self); + checkSelectTimeout(controlObject, Hal_getMonotonicTimeInMs(), self); int state = getState(controlObject); @@ -2361,11 +2372,11 @@ Control_writeAccessControlObject(MmsMapping* self, MmsDomain* domain, const char MmsValue* operTm = getOperParameterOperTime(value); - if (operTm != NULL) + if (operTm) { controlObject->operateTime = MmsValue_getUtcTimeInMs(operTm); - if (controlObject->operateTime > currentTime) + if (controlObject->operateTime > Hal_getTimeInMs()) { controlObject->timeActivatedOperate = true; controlObject->synchroCheck = synchroCheck; @@ -2373,7 +2384,7 @@ Control_writeAccessControlObject(MmsMapping* self, MmsDomain* domain, const char controlObject->mmsConnection = connection; CheckHandlerResult checkResult = CONTROL_ACCEPTED; - if (controlObject->checkHandler != NULL) + if (controlObject->checkHandler) { /* perform operative tests */ @@ -2387,7 +2398,7 @@ Control_writeAccessControlObject(MmsMapping* self, MmsDomain* domain, const char setState(controlObject, STATE_WAIT_FOR_ACTIVATION_TIME); - updateNextControlTimeout(self, controlObject->operateTime); + updateNextRealTimeControlTimeout(self, controlObject->operateTime); if (DEBUG_IED_SERVER) printf("IED_SERVER: Oper - activate time activated control\n"); @@ -2441,7 +2452,7 @@ Control_writeAccessControlObject(MmsMapping* self, MmsDomain* domain, const char initiateControlTask(controlObject); - updateNextControlTimeout(self, currentTime); + updateNextControlTimeout(self, Hal_getMonotonicTimeInMs()); } else { diff --git a/src/iec61850/server/mms_mapping/logging.c b/src/iec61850/server/mms_mapping/logging.c index a874d0ab..099b267b 100644 --- a/src/iec61850/server/mms_mapping/logging.c +++ b/src/iec61850/server/mms_mapping/logging.c @@ -275,7 +275,7 @@ enableLogging(LogControl* self) self->enabled = true; if ((self->triggerOps & TRG_OPT_INTEGRITY) && (self->intgPd != 0)) - self->nextIntegrityScan = Hal_getTimeInMs(); + self->nextIntegrityScan = Hal_getMonotonicTimeInMs(); else self->nextIntegrityScan = 0; @@ -545,7 +545,7 @@ LIBIEC61850_LOG_SVC_writeAccessLogControlBlock(MmsMapping* self, MmsDomain* doma { return DATA_ACCESS_ERROR_OBJECT_NONE_EXISTENT; } - else + else { if (self->controlBlockAccessHandler) { @@ -811,7 +811,7 @@ LIBIEC61850_LOG_SVC_readAccessControlBlock(MmsMapping* self, MmsDomain* domain, LogControl* logControl = lookupLogControl(self, domain, lnName, objectName); - if (logControl) + if (logControl) { bool allowAccess = true; @@ -901,7 +901,7 @@ createLogControlBlock(MmsMapping* self, LogControlBlock* logControlBlock, LogControl* logControl) { MmsVariableSpecification* lcb = (MmsVariableSpecification*) GLOBAL_CALLOC(1, sizeof(MmsVariableSpecification)); - + if (lcb) { lcb->name = StringUtils_copyString(logControlBlock->name); diff --git a/src/iec61850/server/mms_mapping/mms_mapping.c b/src/iec61850/server/mms_mapping/mms_mapping.c index cca93cb7..b6415dce 100644 --- a/src/iec61850/server/mms_mapping/mms_mapping.c +++ b/src/iec61850/server/mms_mapping/mms_mapping.c @@ -2269,6 +2269,7 @@ MmsMapping_create(IedModel* model, IedServer iedServer) #if (CONFIG_IEC61850_CONTROL_SERVICE == 1) self->controlObjects = LinkedList_create(); self->nextControlTimeout = 0xffffffffffffffffLLU; + self->nextRealTimeControlTimeout = 0xffffffffffffffffLLU; #endif #if (CONFIG_IEC61850_SETTING_GROUPS == 1) @@ -3077,7 +3078,7 @@ mmsWriteHandler(void* parameter, MmsDomain* domain, const char* variableId, int sg->sgcb->editSG = val; sg->editingClient = connection; - sg->reservationTimeout = Hal_getTimeInMs() + (sg->sgcb->resvTms * 1000); + sg->reservationTimeout = Hal_getMonotonicTimeInMs() + (sg->sgcb->resvTms * 1000); MmsValue* editSg = MmsValue_getElement(sg->sgcbMmsValues, 2); @@ -4822,27 +4823,27 @@ GOOSE_processGooseEvents(MmsMapping* self, uint64_t currentTimeInMs) static void processPeriodicTasks(MmsMapping* self) { - uint64_t currentTimeInMs = Hal_getTimeInMs(); + uint64_t currentMonotonicTimeInMs = Hal_getMonotonicTimeInMs(); #if (CONFIG_INCLUDE_GOOSE_SUPPORT == 1) if (self->useIntegratedPublisher) - GOOSE_processGooseEvents(self, currentTimeInMs); + GOOSE_processGooseEvents(self, currentMonotonicTimeInMs); #endif #if (CONFIG_IEC61850_CONTROL_SERVICE == 1) - Control_processControlActions(self, currentTimeInMs); + Control_processControlActions(self, currentMonotonicTimeInMs); #endif #if (CONFIG_IEC61850_REPORT_SERVICE == 1) - Reporting_processReportEvents(self, currentTimeInMs); + Reporting_processReportEvents(self, Hal_getTimeInMs()); #endif #if (CONFIG_IEC61850_SETTING_GROUPS == 1) - MmsMapping_checkForSettingGroupReservationTimeouts(self, currentTimeInMs); + MmsMapping_checkForSettingGroupReservationTimeouts(self, currentMonotonicTimeInMs); #endif #if (CONFIG_IEC61850_LOG_SERVICE == 1) - Logging_processIntegrityLogs(self, currentTimeInMs); + Logging_processIntegrityLogs(self, currentMonotonicTimeInMs); #endif /* handle low priority MMS backgound tasks (like file upload...) */ @@ -4883,8 +4884,12 @@ MmsMapping_startEventWorkerThread(MmsMapping* self) self->reportThreadRunning = true; Thread thread = Thread_create((ThreadExecutionFunction)eventWorkerThread, self, false); - self->reportWorkerThread = thread; - Thread_start(thread); + + if (thread) + { + self->reportWorkerThread = thread; + Thread_start(thread); + } } void @@ -4934,7 +4939,7 @@ MmsMapping_createDataSetByNamedVariableList(MmsMapping* self, MmsNamedVariableLi DataSetEntry* lastDataSetEntry = NULL; - while (element != NULL) + while (element) { MmsAccessSpecifier* listEntry = (MmsAccessSpecifier*)element->data; diff --git a/src/iec61850/server/mms_mapping/reporting.c b/src/iec61850/server/mms_mapping/reporting.c index 2a230c28..44293dfb 100644 --- a/src/iec61850/server/mms_mapping/reporting.c +++ b/src/iec61850/server/mms_mapping/reporting.c @@ -1844,7 +1844,7 @@ checkReservationTimeout(MmsMapping* self, ReportControl* rc) { if (rc->reservationTimeout > 0) { - if (Hal_getTimeInMs() > rc->reservationTimeout) + if (Hal_getMonotonicTimeInMs() > rc->reservationTimeout) { if (rc->resvTms != -1) rc->resvTms = 0; @@ -2123,7 +2123,7 @@ Reporting_RCBWriteAccessHandler(MmsMapping* self, ReportControl* rc, const char* { rc->reserved = true; rc->clientConnection = connection; - rc->reservationTimeout = Hal_getTimeInMs() + (rc->resvTms * 1000); + rc->reservationTimeout = Hal_getMonotonicTimeInMs() + (rc->resvTms * 1000); if (self->rcbEventHandler) { @@ -2141,7 +2141,7 @@ Reporting_RCBWriteAccessHandler(MmsMapping* self, ReportControl* rc, const char* { if (rc->clientConnection == connection) { - rc->reservationTimeout = Hal_getTimeInMs() + (rc->resvTms * 1000); + rc->reservationTimeout = Hal_getMonotonicTimeInMs() + (rc->resvTms * 1000); } } } @@ -2726,7 +2726,7 @@ Reporting_RCBWriteAccessHandler(MmsMapping* self, ReportControl* rc, const char* } else { - rc->reservationTimeout = Hal_getTimeInMs() + (rc->resvTms * 1000); + rc->reservationTimeout = Hal_getMonotonicTimeInMs() + (rc->resvTms * 1000); reserveRcb(rc, connection); @@ -2755,7 +2755,7 @@ Reporting_RCBWriteAccessHandler(MmsMapping* self, ReportControl* rc, const char* } else { - rc->reservationTimeout = Hal_getTimeInMs() + (RESV_TMS_IMPLICIT_VALUE * 1000); + rc->reservationTimeout = Hal_getMonotonicTimeInMs() + (RESV_TMS_IMPLICIT_VALUE * 1000); reserveRcb(rc, connection); @@ -2845,7 +2845,7 @@ exit_function: { if (rc->buffered) { - rc->reservationTimeout = Hal_getTimeInMs() + (RESV_TMS_IMPLICIT_VALUE * 1000); + rc->reservationTimeout = Hal_getMonotonicTimeInMs() + (RESV_TMS_IMPLICIT_VALUE * 1000); if (rc->resvTms == 0) { @@ -2989,7 +2989,7 @@ Reporting_disableReportControlInstance(MmsMapping* self, ReportControl* rc) updateOwner(rc, NULL); else if (rc->resvTms > 0) { - rc->reservationTimeout = Hal_getTimeInMs() + (rc->resvTms * 1000); + rc->reservationTimeout = Hal_getMonotonicTimeInMs() + (rc->resvTms * 1000); } } @@ -4360,7 +4360,7 @@ Reporting_activateBufferedReports(MmsMapping* self) } static void -processEventsForReport(ReportControl* rc, uint64_t currentTimeInMs) +processEventsForReport(ReportControl* rc, uint64_t currentRealTimeInMs) { if ((rc->enabled) || (rc->isBuffering)) { @@ -4372,10 +4372,10 @@ processEventsForReport(ReportControl* rc, uint64_t currentTimeInMs) if (rc->triggered) { rc->triggered = false; - enqueueReport(rc, false, false, currentTimeInMs); + enqueueReport(rc, false, false, currentRealTimeInMs); } - enqueueReport(rc, false, true, currentTimeInMs); + enqueueReport(rc, false, true, currentRealTimeInMs); rc->gi = false; @@ -4387,18 +4387,18 @@ processEventsForReport(ReportControl* rc, uint64_t currentTimeInMs) { if (rc->intgPd > 0) { - if (currentTimeInMs >= rc->nextIntgReportTime) + if (currentRealTimeInMs >= rc->nextIntgReportTime) { /* send current events in event buffer before integrity report */ if (rc->triggered) { - enqueueReport(rc, false, false, currentTimeInMs); + enqueueReport(rc, false, false, currentRealTimeInMs); rc->triggered = false; } if (rc->server->syncIntegrityReportTimes) { - rc->nextIntgReportTime = getNextRoundedStartTime(currentTimeInMs, rc->intgPd); + rc->nextIntgReportTime = getNextRoundedStartTime(currentRealTimeInMs, rc->intgPd); } else { @@ -4406,36 +4406,36 @@ processEventsForReport(ReportControl* rc, uint64_t currentTimeInMs) } /* check for system time change effects */ - if ((rc->nextIntgReportTime < currentTimeInMs) || - (rc->nextIntgReportTime > currentTimeInMs + rc->intgPd)) + if ((rc->nextIntgReportTime < currentRealTimeInMs) || + (rc->nextIntgReportTime > currentRealTimeInMs + rc->intgPd)) { if (rc->server->syncIntegrityReportTimes) { - rc->nextIntgReportTime = getNextRoundedStartTime(currentTimeInMs, rc->intgPd); + rc->nextIntgReportTime = getNextRoundedStartTime(currentRealTimeInMs, rc->intgPd); } else { - rc->nextIntgReportTime = currentTimeInMs + rc->intgPd; + rc->nextIntgReportTime = currentRealTimeInMs + rc->intgPd; } } - enqueueReport(rc, true, false, currentTimeInMs); + enqueueReport(rc, true, false, currentRealTimeInMs); rc->triggered = false; } else { /* check for system time change effects */ - if ((rc->nextIntgReportTime < currentTimeInMs) || - (rc->nextIntgReportTime > currentTimeInMs + rc->intgPd)) + if ((rc->nextIntgReportTime < currentRealTimeInMs) || + (rc->nextIntgReportTime > currentRealTimeInMs + rc->intgPd)) { if (rc->server->syncIntegrityReportTimes) { - rc->nextIntgReportTime = getNextRoundedStartTime(currentTimeInMs, rc->intgPd); + rc->nextIntgReportTime = getNextRoundedStartTime(currentRealTimeInMs, rc->intgPd); } else { - rc->nextIntgReportTime = currentTimeInMs + rc->intgPd; + rc->nextIntgReportTime = currentRealTimeInMs + rc->intgPd; } } } @@ -4444,9 +4444,9 @@ processEventsForReport(ReportControl* rc, uint64_t currentTimeInMs) if (rc->triggered) { - if (currentTimeInMs >= rc->reportTime) + if (Hal_getMonotonicTimeInMs() >= rc->reportTime) { - enqueueReport(rc, false, false, currentTimeInMs); + enqueueReport(rc, false, false, currentRealTimeInMs); rc->triggered = false; } @@ -4571,7 +4571,7 @@ ReportControl_valueUpdated(ReportControl* self, int dataSetEntryIndex, int flag, if (self->inclusionFlags[dataSetEntryIndex] & flag) { /* report for this data set entry is already pending (bypass BufTm and send report immediately) */ - self->reportTime = Hal_getTimeInMs(); + self->reportTime = Hal_getMonotonicTimeInMs(); if (modelLocked) { @@ -4579,7 +4579,7 @@ ReportControl_valueUpdated(ReportControl* self, int dataSetEntryIndex, int flag, copyValuesToReportBuffer(self); } - processEventsForReport(self, self->reportTime); + processEventsForReport(self, Hal_getTimeInMs()); } if (modelLocked) @@ -4598,11 +4598,9 @@ ReportControl_valueUpdated(ReportControl* self, int dataSetEntryIndex, int flag, if (self->triggered == false) { - uint64_t currentTime = Hal_getTimeInMs(); + MmsValue_setBinaryTime(self->timeOfEntry, Hal_getTimeInMs()); - MmsValue_setBinaryTime(self->timeOfEntry, currentTime); - - self->reportTime = currentTime + self->bufTm; + self->reportTime = Hal_getMonotonicTimeInMs() + self->bufTm; } self->triggered = true; diff --git a/src/mms/iso_mms/server/mms_file_service.c b/src/mms/iso_mms/server/mms_file_service.c index 12a83ef0..5f463ef4 100644 --- a/src/mms/iso_mms/server/mms_file_service.c +++ b/src/mms/iso_mms/server/mms_file_service.c @@ -477,7 +477,7 @@ mmsServer_fileUploadTask(MmsServer self, MmsObtainFileTask task, int taskState) break; case MMS_FILE_UPLOAD_STATE_FILE_OPEN_SENT: { - if (Hal_getTimeInMs() > task->nextTimeout) + if (Hal_getMonotonicTimeInMs() > task->nextTimeout) { if (DEBUG_MMS_SERVER) printf("MMS_SERVER: file open timeout!\n"); @@ -489,6 +489,7 @@ mmsServer_fileUploadTask(MmsServer self, MmsObtainFileTask task, int taskState) FileSystem_closeFile(task->fileHandle); task->fileHandle = NULL; } + deleteFile(MmsServer_getFilesystemBasepath(self), task->destinationFilename); } } @@ -502,14 +503,14 @@ mmsServer_fileUploadTask(MmsServer self, MmsObtainFileTask task, int taskState) task->state = MMS_FILE_UPLOAD_STATE_FILE_READ_SENT; IsoConnection_sendMessage(task->connection->isoConnection, message); - task->nextTimeout = Hal_getTimeInMs() + self->requestTimeoutMs; + task->nextTimeout = Hal_getMonotonicTimeInMs() + self->requestTimeoutMs; } break; case MMS_FILE_UPLOAD_STATE_FILE_READ_SENT: - if (Hal_getTimeInMs() > task->nextTimeout) + if (Hal_getMonotonicTimeInMs() > task->nextTimeout) { if (DEBUG_MMS_SERVER) printf("MMS_SERVER: file read timeout!\n"); @@ -521,6 +522,7 @@ mmsServer_fileUploadTask(MmsServer self, MmsObtainFileTask task, int taskState) FileSystem_closeFile(task->fileHandle); task->fileHandle = NULL; } + deleteFile(MmsServer_getFilesystemBasepath(self), task->destinationFilename); } @@ -535,13 +537,13 @@ mmsServer_fileUploadTask(MmsServer self, MmsObtainFileTask task, int taskState) IsoConnection_sendMessage(task->connection->isoConnection, message); - task->nextTimeout = Hal_getTimeInMs() + self->requestTimeoutMs; + task->nextTimeout = Hal_getMonotonicTimeInMs() + self->requestTimeoutMs; } break; case MMS_FILE_UPLOAD_STATE_FILE_CLOSE_SENT: - if (Hal_getTimeInMs() > task->nextTimeout) + if (Hal_getMonotonicTimeInMs() > task->nextTimeout) { if (DEBUG_MMS_SERVER) printf("MMS_SERVER: file close timeout!\n"); @@ -813,7 +815,7 @@ mmsServer_handleObtainFileRequest(MmsServerConnection connection, uint8_t* buffe MmsServer_releaseTransmitBuffer(connection->server); - task->nextTimeout = Hal_getTimeInMs() + connection->server->requestTimeoutMs; + task->nextTimeout = Hal_getMonotonicTimeInMs() + connection->server->requestTimeoutMs; task->state = MMS_FILE_UPLOAD_STATE_FILE_OPEN_SENT; }