You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2021/12/15 21:24:37 UTC

[plc4x] 03/09: - Finish implementing the WriteBuffer

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch feature/plc4net2
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit 089e0cbc137ec31c9102712eca8ce25b9f40cc3d
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Thu Dec 3 14:52:08 2020 +0100

    - Finish implementing the WriteBuffer
---
 sandbox/plc4net/api/api/value/IPlcValue.cs         | 19 +++++
 sandbox/plc4net/spi/spi/generation/ReadBuffer.cs   |  2 +-
 sandbox/plc4net/spi/spi/generation/WriteBuffer.cs  | 97 +++++++++++++++++++++-
 .../plc4net/spi/spi/model/values/PlcBitString.cs   | 19 +++++
 .../model/values/PlcSimpleNumericValueAdapter.cs   | 19 +++++
 .../spi/spi/model/values/PlcSimpleValueAdapter.cs  | 19 +++++
 .../spi/spi/model/values/PlcValueAdapter.cs        | 19 +++++
 7 files changed, 189 insertions(+), 5 deletions(-)

diff --git a/sandbox/plc4net/api/api/value/IPlcValue.cs b/sandbox/plc4net/api/api/value/IPlcValue.cs
index e3e29a4..a6682b9 100644
--- a/sandbox/plc4net/api/api/value/IPlcValue.cs
+++ b/sandbox/plc4net/api/api/value/IPlcValue.cs
@@ -1,3 +1,22 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
 using System;
 using System.Collections.Generic;
 
diff --git a/sandbox/plc4net/spi/spi/generation/ReadBuffer.cs b/sandbox/plc4net/spi/spi/generation/ReadBuffer.cs
index a14ff8c..f727fba 100644
--- a/sandbox/plc4net/spi/spi/generation/ReadBuffer.cs
+++ b/sandbox/plc4net/spi/spi/generation/ReadBuffer.cs
@@ -196,7 +196,7 @@ namespace org.apache.plc4net.spi.generation
 
         public string ReadString(int bitLength, Encoding encoding)
         {
-            return "";
+            throw new NotImplementedException("This encoding is currently not supported");
         }
 
     }
diff --git a/sandbox/plc4net/spi/spi/generation/WriteBuffer.cs b/sandbox/plc4net/spi/spi/generation/WriteBuffer.cs
index 5f4b3b1..c6e8f82 100644
--- a/sandbox/plc4net/spi/spi/generation/WriteBuffer.cs
+++ b/sandbox/plc4net/spi/spi/generation/WriteBuffer.cs
@@ -17,72 +17,161 @@
 // under the License.
 //
 
+using System;
+using Ayx.BitIO;
+
 namespace org.apache.plc4net.spi.generation
 {
     public class WriteBuffer
     {
-        
+        private readonly BitWriter _writer;
+
+        public WriteBuffer()
+        {
+            _writer = new BitWriter();
+        }
+
         public int GetPos()
         {
-            return 0;
+            return _writer.Length;
         }
 
         public byte[] GetBytes()
         {
-            return null;
+            return _writer.GetBytes();
         }
     
         public int GetTotalBytes() 
         {
-            return 0;
+            return _writer.Length / 8 + (_writer.Length % 8 != 0 ? 1 : 0);
         }
 
         public void WriteBit(bool value)
         {
+            _writer.WriteBool(value);
         }
 
         public void WriteByte(int bitLength, byte value)
         {
+            if (bitLength > 8)
+            {
+                throw new ArgumentOutOfRangeException();
+            }
+            _writer.WriteByte(value, bitLength);
         }
         
         public void WriteUshort(int bitLength, ushort value)
         {
+            if (bitLength > 16)
+            {
+                throw new ArgumentOutOfRangeException();
+            }
+            _writer.WriteInt(value, bitLength);
         }
 
         public void WriteUint(int bitLength, uint value)
         {
+            if (bitLength > 32)
+            {
+                throw new ArgumentOutOfRangeException();
+            }
+            _writer.WriteInt((int) value, bitLength);
         }
 
         public void WriteUlong(int bitLength, ulong value)
         {
+            if (bitLength > 64)
+            {
+                throw new ArgumentOutOfRangeException();
+            }
+            var highInt = (int) ((value & 0xFFFFFFFF00000000) >> 32);
+            var lowInt = (int) (value & 0xFFFFFFFF);
+            if (bitLength > 32)
+            {
+                _writer.WriteInt(highInt, bitLength - 32);
+                _writer.WriteInt(lowInt, 32);
+            }
+            else
+            {
+                _writer.WriteInt(lowInt, bitLength);
+            }
         }
 
         public void WriteSbyte(int bitLength, sbyte value)
         {
+            if (bitLength > 8)
+            {
+                throw new ArgumentOutOfRangeException();
+            }
+            _writer.WriteInt(value, bitLength);
         }
         
         public void WriteShort(int bitLength, short value)
         {
+            if (bitLength > 16)
+            {
+                throw new ArgumentOutOfRangeException();
+            }
+            _writer.WriteInt(value, bitLength);
         }
 
         public void WriteInt(int bitLength, int value)
         {
+            if (bitLength > 32)
+            {
+                throw new ArgumentOutOfRangeException();
+            }
+            _writer.WriteInt(value, bitLength);
         }
 
         public void WriteLong(int bitLength, long value)
         {
+            if (bitLength > 64)
+            {
+                throw new ArgumentOutOfRangeException();
+            }
+            WriteUlong(bitLength, (ulong) value);
         }
 
         public void WriteFloat(int bitLength, float value)
         {
+            if (bitLength == 32)
+            {
+                var bytes = BitConverter.GetBytes(value);
+                for (var i = 0; i < bytes.Length; i++)
+                {
+                    _writer.WriteByte(8, bytes[i]);
+                }
+            }
+            else if (bitLength == 16)
+            {
+                
+            }
+            else
+            {
+                throw new NotImplementedException("This encoding is currently not supported");
+            }
         }
 
         public void WriteDouble(int bitLength, double value)
         {
+            if (bitLength == 64)
+            {
+                var bytes = BitConverter.GetBytes(value);
+                for (var i = 0; i < bytes.Length; i++)
+                {
+                    _writer.WriteByte(8, bytes[i]);
+                }
+            }
+            else
+            {
+                throw new NotImplementedException("This encoding is currently not supported");
+            }
         }
 
         public void WriteString(int bitLength, string encoding, string value)
         {
+            throw new NotImplementedException("This encoding is currently not supported");
         }
 
     }
diff --git a/sandbox/plc4net/spi/spi/model/values/PlcBitString.cs b/sandbox/plc4net/spi/spi/model/values/PlcBitString.cs
index 4bcbcba..d5747e9 100644
--- a/sandbox/plc4net/spi/spi/model/values/PlcBitString.cs
+++ b/sandbox/plc4net/spi/spi/model/values/PlcBitString.cs
@@ -1,3 +1,22 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
 namespace org.apache.plc4net.spi.model.values
 {
     public class PlcBitString : PlcValueAdapter
diff --git a/sandbox/plc4net/spi/spi/model/values/PlcSimpleNumericValueAdapter.cs b/sandbox/plc4net/spi/spi/model/values/PlcSimpleNumericValueAdapter.cs
index 2f0cbba..122cc0f 100644
--- a/sandbox/plc4net/spi/spi/model/values/PlcSimpleNumericValueAdapter.cs
+++ b/sandbox/plc4net/spi/spi/model/values/PlcSimpleNumericValueAdapter.cs
@@ -1,3 +1,22 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
 using System;
 
 namespace org.apache.plc4net.spi.model.values
diff --git a/sandbox/plc4net/spi/spi/model/values/PlcSimpleValueAdapter.cs b/sandbox/plc4net/spi/spi/model/values/PlcSimpleValueAdapter.cs
index 52a64b9..787fe45 100644
--- a/sandbox/plc4net/spi/spi/model/values/PlcSimpleValueAdapter.cs
+++ b/sandbox/plc4net/spi/spi/model/values/PlcSimpleValueAdapter.cs
@@ -1,3 +1,22 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
 namespace org.apache.plc4net.spi.model.values
 {
     public abstract class PlcSimpleValueAdapter : PlcValueAdapter
diff --git a/sandbox/plc4net/spi/spi/model/values/PlcValueAdapter.cs b/sandbox/plc4net/spi/spi/model/values/PlcValueAdapter.cs
index f5227f6..afd5e82 100644
--- a/sandbox/plc4net/spi/spi/model/values/PlcValueAdapter.cs
+++ b/sandbox/plc4net/spi/spi/model/values/PlcValueAdapter.cs
@@ -1,3 +1,22 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
 using System;
 using System.Collections.Generic;
 using org.apache.plc4net.api.value;