diff --git a/dotnet/IEC61850forCSharp/IEC61850ClientAPI.cs b/dotnet/IEC61850forCSharp/IEC61850ClientAPI.cs index 6a0f8ae9..fb87d7c5 100644 --- a/dotnet/IEC61850forCSharp/IEC61850ClientAPI.cs +++ b/dotnet/IEC61850forCSharp/IEC61850ClientAPI.cs @@ -62,11 +62,14 @@ namespace IEC61850 [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] private static extern void MmsServerIdentity_destroy(IntPtr self); - [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] - private static extern void MmsConnection_setLocalDetail(IntPtr self, Int32 localDetail); + [DllImport ("iec61850", CallingConvention=CallingConvention.Cdecl)] + private static extern void MmsConnection_setLocalDetail (IntPtr self, Int32 localDetail); + + [DllImport ("iec61850", CallingConvention=CallingConvention.Cdecl)] + private static extern Int32 MmsConnection_getLocalDetail (IntPtr self); [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] - private static extern Int32 MmsConnection_getLocalDetail(IntPtr self); + private static extern Int32 MmsConnection_setRequestTimeout(IntPtr self, UInt32 timeoutInMs); [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr MmsConnection_readMultipleVariables(IntPtr self, out int mmsError, @@ -162,6 +165,15 @@ namespace IEC61850 return MmsConnection_getLocalDetail(self); } + /// + /// Sets the request timeout + /// + /// request timeout in milliseconds + public void SetRequestTimeout(uint timeoutMs) + { + MmsConnection_setRequestTimeout(self, timeoutMs); + } + /// /// Reads multipe MMS variables from the same domain /// @@ -378,7 +390,7 @@ namespace IEC61850 [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] static extern float MmsValue_toFloat(IntPtr self); - [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.I1)] static extern bool MmsValue_getBoolean(IntPtr self); @@ -405,8 +417,8 @@ namespace IEC61850 static extern IntPtr IedConnection_createWithTlsSupport(IntPtr tlsConfig); [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] - static extern void IedConnection_destroy(IntPtr self); - + static extern void IedConnection_destroy(IntPtr self); + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] static extern int Connection_getState(IedConnection self); @@ -444,17 +456,17 @@ namespace IEC61850 static extern IntPtr IedConnection_getLogicalNodeDirectory(IntPtr self, out int error, string logicalNodeReference, int acsiClass); [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] - static extern IntPtr IedConnection_getServerDirectory(IntPtr self, out int error, [MarshalAs(UnmanagedType.I1)] bool getFileNames); - - [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] + static extern IntPtr IedConnection_getServerDirectory(IntPtr self, out int error, [MarshalAs(UnmanagedType.I1)] bool getFileNames); + + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] static extern void IedConnection_getDeviceModelFromServer(IntPtr self, out int error); [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] static extern IntPtr IedConnection_getLogicalDeviceDirectory(IntPtr self, out int error, string logicalDeviceName); [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] - static extern IntPtr IedConnection_getVariableSpecification(IntPtr self, out int error, string objectReference, int fc); - + static extern IntPtr IedConnection_getVariableSpecification(IntPtr self, out int error, string objectReference, int fc); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void InternalConnectionClosedHandler(IntPtr parameter,IntPtr Iedconnection); @@ -479,24 +491,24 @@ namespace IEC61850 [return: MarshalAs(UnmanagedType.Bool)] static extern bool IedConnection_deleteDataSet(IntPtr self, out int error, string dataSetReference); - [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] static extern IntPtr IedConnection_getDataSetDirectory(IntPtr self, out int error, string dataSetReference, [MarshalAs(UnmanagedType.I1)] out bool isDeletable); [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] static extern IntPtr IedConnection_getMmsConnection(IntPtr self); [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] - static extern IntPtr MmsConnection_getIsoConnectionParameters(IntPtr mmsConnection); - - [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] + static extern IntPtr MmsConnection_getIsoConnectionParameters(IntPtr mmsConnection); + + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] static extern IntPtr IedConnection_getFileDirectory(IntPtr self, out int error, string directoryName); [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] static extern IntPtr IedConnection_getFileDirectoryEx(IntPtr self, out int error, string directoryName, string continueAfter - , [MarshalAs(UnmanagedType.I1)] out bool moreFollows); - - [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] + , [MarshalAs(UnmanagedType.I1)] out bool moreFollows); + + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] static extern void IedConnection_deleteFile(IntPtr self, out int error, string fileName); [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] @@ -506,7 +518,7 @@ namespace IEC61850 [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] static extern IntPtr IedConnection_queryLogByTime(IntPtr self, out int error, string logReference, ulong startTime, ulong endTime, [MarshalAs(UnmanagedType.I1)] out bool moreFollows); - + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] static extern IntPtr IedConnection_getRCBValues(IntPtr connection, out int error, string rcbReference, IntPtr updateRcb); @@ -632,11 +644,11 @@ namespace IEC61850 IedConnection_deleteFileAsync(IntPtr self, out int error, string fileName, IedConnection_GenericServiceHandler handler, IntPtr parameter); - + /******************** * FileDirectoryEntry - *********************/ - [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] + *********************/ + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] static extern void FileDirectoryEntry_destroy(IntPtr self); /**************** @@ -649,11 +661,11 @@ namespace IEC61850 static extern IntPtr LinkedList_getData(IntPtr self); [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] - static extern void LinkedList_destroy(IntPtr self); - - [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] - static extern void LinkedList_destroyStatic(IntPtr self); - + static extern void LinkedList_destroy(IntPtr self); + + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] + static extern void LinkedList_destroyStatic(IntPtr self); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void LinkedListValueDeleteFunction(IntPtr pointer); @@ -711,8 +723,8 @@ namespace IEC61850 ~IedConnection () { Dispose(); - } - + } + private IsoConnectionParameters isoConnectionParameters = null; /// @@ -720,16 +732,16 @@ namespace IEC61850 /// /// The connection parameters public IsoConnectionParameters GetConnectionParameters() - { + { if (isoConnectionParameters == null) - { - IntPtr mmsConnection = IedConnection_getMmsConnection(connection); - - IntPtr parameters = MmsConnection_getIsoConnectionParameters(mmsConnection); - - isoConnectionParameters = new IsoConnectionParameters(parameters); - } - + { + IntPtr mmsConnection = IedConnection_getMmsConnection(connection); + + IntPtr parameters = MmsConnection_getIsoConnectionParameters(mmsConnection); + + isoConnectionParameters = new IsoConnectionParameters(parameters); + } + return isoConnectionParameters; } @@ -833,7 +845,7 @@ namespace IEC61850 ControlObject controlObject = new ControlObject(objectReference, connection, this); return controlObject; - } + } /// /// Creates a new SampledValuesControlBlock instance. @@ -860,15 +872,15 @@ namespace IEC61850 /// /// Updates the device model by quering the server. - /// + /// public void UpdateDeviceModel() - { - int error; - - IedConnection_getDeviceModelFromServer(connection, out error); - + { + int error; + + IedConnection_getDeviceModelFromServer(connection, out error); + if (error != 0) - throw new IedConnectionException("UpdateDeviceModel failed", error); + throw new IedConnectionException("UpdateDeviceModel failed", error); } @@ -1442,40 +1454,40 @@ namespace IEC61850 } return invokeId; - } - - /// Read the content of a file directory. - /// The name of the directory. - /// This exception is thrown if there is a connection or service error + } + + /// Read the content of a file directory. + /// The name of the directory. + /// This exception is thrown if there is a connection or service error public List GetFileDirectory(string directoryName) - { - int error; - - IntPtr fileEntryList = IedConnection_getFileDirectory(connection, out error, directoryName); - + { + int error; + + IntPtr fileEntryList = IedConnection_getFileDirectory(connection, out error, directoryName); + if (error != 0) - throw new IedConnectionException("Reading file directory failed", error); - - List fileDirectory = new List(); - - IntPtr element = LinkedList_getNext(fileEntryList); - + throw new IedConnectionException("Reading file directory failed", error); + + List fileDirectory = new List(); + + IntPtr element = LinkedList_getNext(fileEntryList); + while (element != IntPtr.Zero) - { - IntPtr elementData = LinkedList_getData(element); - - FileDirectoryEntry entry = new FileDirectoryEntry(elementData); - - fileDirectory.Add(entry); - - FileDirectoryEntry_destroy(elementData); - - element = LinkedList_getNext(element); - } - - LinkedList_destroyStatic(fileEntryList); - - return fileDirectory; + { + IntPtr elementData = LinkedList_getData(element); + + FileDirectoryEntry entry = new FileDirectoryEntry(elementData); + + fileDirectory.Add(entry); + + FileDirectoryEntry_destroy(elementData); + + element = LinkedList_getNext(element); + } + + LinkedList_destroyStatic(fileEntryList); + + return fileDirectory; } /// Read the content of a file directory. - single request version @@ -1513,42 +1525,42 @@ namespace IEC61850 LinkedList_destroyStatic(fileEntryList); return fileDirectory; - } + } [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - [return: MarshalAs(UnmanagedType.I1)] - private delegate bool InternalIedClientGetFileHandler(IntPtr parameter,IntPtr buffer,UInt32 bytesRead); - + [return: MarshalAs(UnmanagedType.I1)] + private delegate bool InternalIedClientGetFileHandler(IntPtr parameter,IntPtr buffer,UInt32 bytesRead); + private bool iedClientGetFileHandler(IntPtr parameter, IntPtr buffer, UInt32 bytesRead) - { - GCHandle handle = GCHandle.FromIntPtr(parameter); - - GetFileCallback getFileCallback = (GetFileCallback)handle.Target; - - byte[] bytes = new byte[bytesRead]; - - Marshal.Copy(buffer, bytes, 0, (int)bytesRead); - - return getFileCallback.handler(getFileCallback.parameter, bytes); - } - - [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] - static extern UInt32 IedConnection_getFile(IntPtr self, out int error, string fileName, InternalIedClientGetFileHandler handler, + { + GCHandle handle = GCHandle.FromIntPtr(parameter); + + GetFileCallback getFileCallback = (GetFileCallback)handle.Target; + + byte[] bytes = new byte[bytesRead]; + + Marshal.Copy(buffer, bytes, 0, (int)bytesRead); + + return getFileCallback.handler(getFileCallback.parameter, bytes); + } + + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] + static extern UInt32 IedConnection_getFile(IntPtr self, out int error, string fileName, InternalIedClientGetFileHandler handler, IntPtr handlerParameter); - public delegate bool GetFileHandler(object parameter,byte[] data); - + public delegate bool GetFileHandler(object parameter,byte[] data); + private class GetFileCallback - { + { public GetFileCallback(GetFileHandler handler, object parameter) - { - this.handler = handler; - this.parameter = parameter; - } - - public GetFileHandler handler; - public object parameter; + { + this.handler = handler; + this.parameter = parameter; + } + + public GetFileHandler handler; + public object parameter; } /// @@ -1565,20 +1577,20 @@ namespace IEC61850 /// /// This exception is thrown if there is a connection or service error public void GetFile(string fileName, GetFileHandler handler, object parameter) - { - int error; - - GetFileCallback getFileCallback = new GetFileCallback(handler, parameter); - - GCHandle handle = GCHandle.Alloc(getFileCallback); - - IedConnection_getFile(connection, out error, fileName, new InternalIedClientGetFileHandler(iedClientGetFileHandler), - GCHandle.ToIntPtr(handle)); - + { + int error; + + GetFileCallback getFileCallback = new GetFileCallback(handler, parameter); + + GCHandle handle = GCHandle.Alloc(getFileCallback); + + IedConnection_getFile(connection, out error, fileName, new InternalIedClientGetFileHandler(iedClientGetFileHandler), + GCHandle.ToIntPtr(handle)); + if (error != 0) - throw new IedConnectionException("Error reading file", error); - - handle.Free(); + throw new IedConnectionException("Error reading file", error); + + handle.Free(); } [UnmanagedFunctionPointer(CallingConvention.Cdecl)] @@ -1942,28 +1954,28 @@ namespace IEC61850 throw new IedConnectionException("Failed to delete data set", error); return isDeleted; - } - - /// - /// Get the directory of the data set. - /// - /// This function returns a list of object references with appended functional constraints (FC) of the data set elemenents. - /// The object reference of the data set - /// the list of object references - /// This exception is thrown if there is a connection or service error + } + + /// + /// Get the directory of the data set. + /// + /// This function returns a list of object references with appended functional constraints (FC) of the data set elemenents. + /// The object reference of the data set + /// the list of object references + /// This exception is thrown if there is a connection or service error public List GetDataSetDirectory(string dataSetReference) - { - bool isDeletable; - - return GetDataSetDirectory(dataSetReference, out isDeletable); - } - - /// - /// Get the directory of the data set. - /// - /// This function returns a list of object references with appended functional constraints (FC) of the data set elemenents. - /// The object reference of the data set - /// Indication if this data set is permanent or deletable. + { + bool isDeletable; + + return GetDataSetDirectory(dataSetReference, out isDeletable); + } + + /// + /// Get the directory of the data set. + /// + /// This function returns a list of object references with appended functional constraints (FC) of the data set elemenents. + /// The object reference of the data set + /// Indication if this data set is permanent or deletable. /// the list of object references /// This exception is thrown if there is a connection or service error public List GetDataSetDirectory(string dataSetReference, out bool isDeletable) @@ -2568,44 +2580,44 @@ namespace IEC61850 { return (IedClientError)this.errorCode; } - } - + } + public class FileDirectoryEntry - { - [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr FileDirectoryEntry_getFileName(IntPtr self); - - [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] - private static extern UInt32 FileDirectoryEntry_getFileSize(IntPtr self); - - [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] - private static extern UInt64 FileDirectoryEntry_getLastModified(IntPtr self); - - private string fileName; - private UInt32 fileSize; - private UInt64 lastModified; - + { + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr FileDirectoryEntry_getFileName(IntPtr self); + + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] + private static extern UInt32 FileDirectoryEntry_getFileSize(IntPtr self); + + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] + private static extern UInt64 FileDirectoryEntry_getLastModified(IntPtr self); + + private string fileName; + private UInt32 fileSize; + private UInt64 lastModified; + internal FileDirectoryEntry(IntPtr nativeFileDirectoryEntry) - { - fileName = Marshal.PtrToStringAnsi(FileDirectoryEntry_getFileName(nativeFileDirectoryEntry)); - fileSize = FileDirectoryEntry_getFileSize(nativeFileDirectoryEntry); - lastModified = FileDirectoryEntry_getLastModified(nativeFileDirectoryEntry); - } - + { + fileName = Marshal.PtrToStringAnsi(FileDirectoryEntry_getFileName(nativeFileDirectoryEntry)); + fileSize = FileDirectoryEntry_getFileSize(nativeFileDirectoryEntry); + lastModified = FileDirectoryEntry_getLastModified(nativeFileDirectoryEntry); + } + public string GetFileName() - { - return fileName; - } - + { + return fileName; + } + public UInt32 GetFileSize() - { - return fileSize; - } - + { + return fileSize; + } + public UInt64 GetLastModified() - { - return lastModified; - } + { + return lastModified; + } } ///