diff --git a/examples/mms_utility/mms_utility.c b/examples/mms_utility/mms_utility.c index eac52aa3..b158f631 100644 --- a/examples/mms_utility/mms_utility.c +++ b/examples/mms_utility/mms_utility.c @@ -55,9 +55,9 @@ printJournalEntries(LinkedList journalEntries) MmsJournalEntry journalEntry = (MmsJournalEntry) LinkedList_getData(journalEntriesElem); - MmsValue_printToBuffer(journalEntry->entryID, buf, 1024); + MmsValue_printToBuffer(MmsJournalEntry_getEntryID(journalEntry), buf, 1024); printf("EntryID: %s\n", buf); - MmsValue_printToBuffer(journalEntry->occurenceTime, buf, 1024); + MmsValue_printToBuffer(MmsJournalEntry_getOccurenceTime(journalEntry), buf, 1024); printf(" occurence time: %s\n", buf); LinkedList journalVariableElem = LinkedList_getNext(journalEntry->journalVariables); @@ -66,8 +66,8 @@ printJournalEntries(LinkedList journalEntries) MmsJournalVariable journalVariable = (MmsJournalVariable) LinkedList_getData(journalVariableElem); - printf(" variable-tag: %s\n", journalVariable->tag); - MmsValue_printToBuffer(journalVariable->value, buf, 1024); + printf(" variable-tag: %s\n", MmsJournalVariable_getTag(journalVariable)); + MmsValue_printToBuffer(MmsJournalVariable_getValue(journalVariable), buf, 1024); printf(" variable-value: %s\n", buf); journalVariableElem = LinkedList_getNext(journalVariableElem); @@ -77,26 +77,6 @@ printJournalEntries(LinkedList journalEntries) } } -static void -MmsJournalVariable_destroy(MmsJournalVariable self) -{ - if (self != NULL) { - GLOBAL_FREEMEM(self->tag); - MmsValue_delete(self->value); - GLOBAL_FREEMEM(self); - } -} - -void -MmsJournalEntry_destroy(MmsJournalEntry self) -{ - if (self != NULL) { - MmsValue_delete(self->entryID); - MmsValue_delete(self->occurenceTime); - LinkedList_destroyDeep(self->journalVariables, MmsJournalVariable_destroy); - GLOBAL_FREEMEM(self); - } -} int main(int argc, char** argv) { @@ -265,7 +245,6 @@ int main(int argc, char** argv) { logName[0] = 0; logName++; - uint64_t timestamp = Hal_getTimeInMs(); MmsValue* startTime = MmsValue_newBinaryTime(false); @@ -292,12 +271,13 @@ int main(int argc, char** argv) { LinkedList lastEntry = LinkedList_getLastElement(journalEntries); MmsJournalEntry lastJournalEntry = (MmsJournalEntry) LinkedList_getData(lastEntry); - MmsValue* nextEntryId = MmsValue_clone(lastJournalEntry->entryID); - MmsValue* nextTimestamp = MmsValue_clone(lastJournalEntry->occurenceTime); + MmsValue* nextEntryId = MmsValue_clone(MmsJournalEntry_getEntryID(lastJournalEntry)); + MmsValue* nextTimestamp = MmsValue_clone(MmsJournalEntry_getOccurenceTime(lastJournalEntry)); printJournalEntries(journalEntries); - LinkedList_destroyDeep(journalEntries, MmsJournalEntry_destroy); + LinkedList_destroyDeep(journalEntries, (LinkedListValueDeleteFunction) + MmsJournalEntry_destroy); if (moreFollows) { char buf[100]; diff --git a/src/iec61850/server/mms_mapping/logging.c b/src/iec61850/server/mms_mapping/logging.c index 6df0be28..70d61384 100644 --- a/src/iec61850/server/mms_mapping/logging.c +++ b/src/iec61850/server/mms_mapping/logging.c @@ -177,6 +177,7 @@ LogControl_create(LogicalNode* parentLN, MmsMapping* mmsMapping) self->dataSetRef = NULL; self->logInstance = NULL; self->intgPd = 0; + self->nextIntegrityScan = 0; return self; } @@ -345,7 +346,6 @@ updateLogStatusInLCB(LogControl* self) LogInstance* logInstance = self->logInstance; if (logInstance != NULL) { - MmsValue_setBinaryTime(self->oldEntrTm, logInstance->oldEntryTime); MmsValue_setBinaryTime(self->newEntrTm, logInstance->newEntryTime); @@ -829,7 +829,7 @@ Logging_processIntegrityLogs(MmsMapping* self, uint64_t currentTimeInMs) if (currentTimeInMs >= logControl->nextIntegrityScan) { - if (DEBUG_IED_SERVER) + //if (DEBUG_IED_SERVER) printf("IED_SERVER: INTEGRITY SCAN for log %s\n", logControl->name); LogControl_logAllDatasetEntries(logControl, self->mmsDevice->deviceName); diff --git a/src/iec61850/server/mms_mapping/mms_mapping.c b/src/iec61850/server/mms_mapping/mms_mapping.c index 92f497a8..a5367335 100644 --- a/src/iec61850/server/mms_mapping/mms_mapping.c +++ b/src/iec61850/server/mms_mapping/mms_mapping.c @@ -1995,7 +1995,7 @@ mmsWriteHandler(void* parameter, MmsDomain* domain, SettingGroup* sg = getSettingGroupByMmsDomain(self, domain); if (sg->editingClient != (ClientConnection) connection) - return DATA_ACCESS_ERROR_OBJECT_ACCESS_DENIED; + return DATA_ACCESS_ERROR_TEMPORARILY_UNAVAILABLE; } #endif /* (CONFIG_IEC61850_SETTING_GROUPS == 1) */ diff --git a/src/logging/drivers/sqlite/log_storage_sqlite.c b/src/logging/drivers/sqlite/log_storage_sqlite.c index 13d76138..867e0f08 100644 --- a/src/logging/drivers/sqlite/log_storage_sqlite.c +++ b/src/logging/drivers/sqlite/log_storage_sqlite.c @@ -166,6 +166,8 @@ exit_with_error: static uint64_t SqliteLogStorage_addEntry(LogStorage self, uint64_t timestamp) { + printf("SQLITE-DRIVER: add entry\n"); + SqliteLogStorage* instanceData = (SqliteLogStorage*) (self->instanceData); sqlite3* db = instanceData->db; @@ -366,6 +368,10 @@ SqliteLogStorage_getOldestAndNewestEntries(LogStorage self, uint64_t* newEntry, *oldEntryTime = sqlite3_column_int64(instanceData->getOldEntry, 1); validNewEntry = true; } + else { + *oldEntry = 0; + *oldEntryTime = 0; + } sqlite3_reset(instanceData->getOldEntry); @@ -378,6 +384,10 @@ SqliteLogStorage_getOldestAndNewestEntries(LogStorage self, uint64_t* newEntry, *newEntryTime = sqlite3_column_int64(instanceData->getNewEntry, 1); validOldEntry = true; } + else { + *newEntry = 0; + *newEntryTime = 0; + } sqlite3_reset(instanceData->getNewEntry); diff --git a/src/mms/inc/mms_client_connection.h b/src/mms/inc/mms_client_connection.h index d57bbf68..593f1dea 100644 --- a/src/mms/inc/mms_client_connection.h +++ b/src/mms/inc/mms_client_connection.h @@ -735,6 +735,37 @@ struct sMmsJournalVariable { MmsValue* value; }; +/** + * \brief Destroy a single MmsJournalEntry instance. + * + * This function will destroy the whole MmsJournalEntry object including the attached list + * of MmsJournalVariable objects. It is intended to be used in conjunction with the + * LinkedList_destroyDeep function in order to free the result of MmsConnection_readJournalTimeRange + * or MmsConnection_readJournalStartAfter + * + * LinkedList_destroyDeep(journalEntries, (LinkedListValueDeleteFunction) + * MmsJournalEntry_destroy); + * + * \param self the MmsJournalEntry instance to destroy + */ +void +MmsJournalEntry_destroy(MmsJournalEntry self); + +const MmsValue* +MmsJournalEntry_getEntryID(MmsJournalEntry self); + +const MmsValue* +MmsJournalEntry_getOccurenceTime(MmsJournalEntry self); + +const LinkedList /* */ +MmsJournalEntry_getJournalVariables(MmsJournalEntry self); + +const char* +MmsJournalVariable_getTag(MmsJournalVariable self); + +const MmsValue* +MmsJournalVariable_getValue(MmsJournalVariable self); + LinkedList MmsConnection_readJournalTimeRange(MmsConnection self, MmsError* mmsError, const char* domainId, const char* itemId, diff --git a/src/mms/inc/mms_value.h b/src/mms/inc/mms_value.h index f9177768..61253fb6 100644 --- a/src/mms/inc/mms_value.h +++ b/src/mms/inc/mms_value.h @@ -101,7 +101,7 @@ MmsValue_getArraySize(const MmsValue* self); * \return the element object */ MmsValue* -MmsValue_getElement(MmsValue* array, int index); +MmsValue_getElement(const MmsValue* array, int index); /** * \brief Create an emtpy array. @@ -911,7 +911,7 @@ MmsValue_isDeletable(MmsValue* self); * \param self the MmsValue instance */ MmsType -MmsValue_getType(MmsValue* self); +MmsValue_getType(const MmsValue* self); /** * \brief Get a sub-element of a MMS_STRUCTURE value specified by a path name. @@ -947,8 +947,8 @@ MmsValue_getTypeString(MmsValue* self); * * \return a pointer to the start of the buffer */ -char* -MmsValue_printToBuffer(MmsValue* self, char* buffer, int bufferSize); +const char* +MmsValue_printToBuffer(const MmsValue* self, char* buffer, int bufferSize); /** * \brief create a new MmsValue instance from a BER encoded MMS Data element (deserialize) diff --git a/src/mms/inc_private/mms_client_internal.h b/src/mms/inc_private/mms_client_internal.h index c96cfc3b..fad704a3 100644 --- a/src/mms/inc_private/mms_client_internal.h +++ b/src/mms/inc_private/mms_client_internal.h @@ -260,9 +260,6 @@ int mmsClient_createMmsGetNameListRequestAssociationSpecific(long invokeId, ByteBuffer* writeBuffer, const char* continueAfter); -void -mmsClient_createReadJournalRequest(uint32_t invokeId, ByteBuffer* request, const char* domainId, const char* itemId); - void mmsClient_createReadJournalRequestWithTimeRange(uint32_t invokeId, ByteBuffer* request, const char* domainId, const char* itemId, MmsValue* startingTime, MmsValue* endingTime); @@ -271,4 +268,7 @@ void mmsClient_createReadJournalRequestStartAfter(uint32_t invokeId, ByteBuffer* request, const char* domainId, const char* itemId, MmsValue* timeSpecification, MmsValue* entrySpecification); +bool +mmsClient_parseReadJournalResponse(MmsConnection self, bool* moreFollows, LinkedList* result); + #endif /* MMS_MSG_INTERNAL_H_ */ diff --git a/src/mms/iso_mms/client/mms_client_connection.c b/src/mms/iso_mms/client/mms_client_connection.c index 6006c803..b3a99d24 100644 --- a/src/mms/iso_mms/client/mms_client_connection.c +++ b/src/mms/iso_mms/client/mms_client_connection.c @@ -1667,19 +1667,58 @@ readJournal(MmsConnection self, MmsError* mmsError, uint32_t invokeId, ByteBuff return response; } -#if 0 -LinkedList -MmsConnection_readJournal(MmsConnection self, MmsError* mmsError, const char* domainId, const char* itemId) +static void +MmsJournalVariable_destroy(MmsJournalVariable self) { - ByteBuffer* payload = IsoClientConnection_allocateTransmitBuffer(self->isoClient); + if (self != NULL) { + GLOBAL_FREEMEM(self->tag); + MmsValue_delete(self->value); + GLOBAL_FREEMEM(self); + } +} - uint32_t invokeId = getNextInvokeId(self); +void +MmsJournalEntry_destroy(MmsJournalEntry self) +{ + if (self != NULL) { + MmsValue_delete(self->entryID); + MmsValue_delete(self->occurenceTime); + LinkedList_destroyDeep(self->journalVariables, + (LinkedListValueDeleteFunction) MmsJournalVariable_destroy); + GLOBAL_FREEMEM(self); + } +} + + +const MmsValue* +MmsJournalEntry_getEntryID(MmsJournalEntry self) +{ + return self->entryID; +} - mmsClient_createReadJournalRequest(invokeId, payload, domainId, itemId); +const MmsValue* +MmsJournalEntry_getOccurenceTime(MmsJournalEntry self) +{ + return self->occurenceTime; +} - return readJournal(self, mmsError, invokeId, payload); +const LinkedList /* */ +MmsJournalEntry_getJournalVariables(MmsJournalEntry self) +{ + return self->journalVariables; +} + +const char* +MmsJournalVariable_getTag(MmsJournalVariable self) +{ + return self->tag; +} + +const MmsValue* +MmsJournalVariable_getValue(MmsJournalVariable self) +{ + return self->value; } -#endif LinkedList MmsConnection_readJournalTimeRange(MmsConnection self, MmsError* mmsError, const char* domainId, const char* itemId, diff --git a/src/mms/iso_mms/common/mms_value.c b/src/mms/iso_mms/common/mms_value.c index b096c04b..ce1871bd 100644 --- a/src/mms/iso_mms/common/mms_value.c +++ b/src/mms/iso_mms/common/mms_value.c @@ -1600,7 +1600,12 @@ MmsValue_newBinaryTime(bool timeOfDay) void MmsValue_setBinaryTime(MmsValue* self, uint64_t timestamp) { - uint64_t mmsTime = timestamp - (441763200000LL); + uint64_t mmsTime; + + if (timestamp > 441763200000LL) + mmsTime = timestamp - (441763200000LL); + else + timestamp = 0; uint8_t* binaryTimeBuf = self->value.binaryTime.buf; @@ -1877,7 +1882,7 @@ MmsValue_setElement(MmsValue* complexValue, int index, MmsValue* elementValue) } MmsValue* -MmsValue_getElement(MmsValue* complexValue, int index) +MmsValue_getElement(const MmsValue* complexValue, int index) { if ((complexValue->type != MMS_ARRAY) && (complexValue->type != MMS_STRUCTURE)) return NULL; @@ -1918,7 +1923,7 @@ MmsValue_isDeletable(MmsValue* self) } MmsType -MmsValue_getType(MmsValue* self) +MmsValue_getType(const MmsValue* self) { return self->type; } @@ -1970,8 +1975,8 @@ MmsValue_getTypeString(MmsValue* self) } } -char* -MmsValue_printToBuffer(MmsValue* self, char* buffer, int bufferSize) +const char* +MmsValue_printToBuffer(const MmsValue* self, char* buffer, int bufferSize) { switch (MmsValue_getType(self)) { case MMS_STRUCTURE: @@ -1985,7 +1990,7 @@ MmsValue_printToBuffer(MmsValue* self, char* buffer, int bufferSize) int i; for (i = 0; i < arraySize; i++) { - char* currentStr = MmsValue_printToBuffer(MmsValue_getElement(self, i), buffer + bufPos, bufferSize - bufPos); + const char* currentStr = MmsValue_printToBuffer((const MmsValue*) MmsValue_getElement(self, i), buffer + bufPos, bufferSize - bufPos); bufPos += strlen(currentStr); @@ -2100,7 +2105,7 @@ MmsValue_printToBuffer(MmsValue* self, char* buffer, int bufferSize) case MMS_STRING: case MMS_VISIBLE_STRING: - strncpy(buffer, MmsValue_toString(self), bufferSize); + strncpy(buffer, MmsValue_toString((MmsValue*) self), bufferSize); /* Ensure buffer is always 0 terminated */ if (bufferSize > 0)