Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion BACnet.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2008
VisualStudioVersion = 15.0.27130.2003
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BACnet", "BACnet.csproj", "{66832876-01FC-4B7C-8D92-54195773FABF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BACnet.Tests", "Tests\BACnet.Tests.csproj", "{5DC7DC72-E57B-418C-ABE7-8A26B28AD953}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,8 +17,15 @@ Global
{66832876-01FC-4B7C-8D92-54195773FABF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66832876-01FC-4B7C-8D92-54195773FABF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66832876-01FC-4B7C-8D92-54195773FABF}.Release|Any CPU.Build.0 = Release|Any CPU
{5DC7DC72-E57B-418C-ABE7-8A26B28AD953}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5DC7DC72-E57B-418C-ABE7-8A26B28AD953}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5DC7DC72-E57B-418C-ABE7-8A26B28AD953}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5DC7DC72-E57B-418C-ABE7-8A26B28AD953}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {32CA8FD5-D852-4706-BDBF-1A8DBD5D9A4A}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions BACnet.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=VMAC/@EntryIndexedValue">VMAC</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Constants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AA_BB" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=EnumMember/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AA_BB" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=MethodPropertyEvent/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb"&gt;&lt;ExtraRule Prefix="" Suffix="_test" Style="aa_bb" /&gt;&lt;/Policy&gt;</s:String>
</wpf:ResourceDictionary>
4 changes: 2 additions & 2 deletions Base/BacnetBitString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ public uint ConvertToInt()
: 0;
}

public static BacnetBitString ConvertFromInt(uint value)
public static BacnetBitString ConvertFromInt(uint value, byte? bitsUsed = null)
{
return new BacnetBitString
{
value = BitConverter.GetBytes(value),
bits_used = (byte)Math.Ceiling(Math.Log(value, 2))
bits_used = bitsUsed ?? (byte)(Math.Log(value, 2) + 1)
};
}
};
Expand Down
8 changes: 4 additions & 4 deletions Base/BacnetLogRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public struct BacnetLogRecord
private object any_value;
/* } */

public BacnetBitString statusFlags;
public BacnetStatusFlags statusFlags;

public BacnetLogRecord(BacnetTrendLogValueType type, object value, DateTime stamp, uint status)
public BacnetLogRecord(BacnetTrendLogValueType type, object value, DateTime timestamp, BacnetStatusFlags status)
{
this.type = type;
timestamp = stamp;
statusFlags = BacnetBitString.ConvertFromInt(status);
this.timestamp = timestamp;
statusFlags = status;
any_value = null;
Value = value;
}
Expand Down
10 changes: 6 additions & 4 deletions Serialize/Services.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2316,10 +2316,11 @@ public static void EncodeLogRecord(EncodeBuffer buffer, BacnetLogRecord record)
}

/* Tag 2: status */
if (record.statusFlags.bits_used > 0)
var recordStatusFlags = BacnetBitString.ConvertFromInt((uint)record.statusFlags, 4);
if (recordStatusFlags.bits_used > 0)
{
ASN1.encode_opening_tag(buffer, 2);
ASN1.encode_application_bitstring(buffer, record.statusFlags);
ASN1.encode_application_bitstring(buffer, recordStatusFlags);
ASN1.encode_closing_tag(buffer, 2);
}
}
Expand Down Expand Up @@ -2363,7 +2364,7 @@ public static int DecodeLogRecord(byte[] buffer, int offset, int length, int nCu
records[curveNumber].Value = sval;
break;
case BacnetTrendLogValueType.TL_TYPE_BOOL:
records[curveNumber].Value = buffer[offset + len] > 0 ? true : false;
records[curveNumber].Value = buffer[offset + len] > 0;
len++;
break;
case BacnetTrendLogValueType.TL_TYPE_REAL:
Expand Down Expand Up @@ -2423,9 +2424,10 @@ public static int DecodeLogRecord(byte[] buffer, int offset, int length, int nCu
return len;

len += l;
len += ASN1.decode_bitstring(buffer, offset + len, 2, out var statusFlags);
len += ASN1.decode_bitstring(buffer, offset + len, 2, out var statusFlagsBits);

//set status to all returns
var statusFlags = (BacnetStatusFlags)statusFlagsBits.ConvertToInt();
for (var curveNumber = 0; curveNumber < nCurves; curveNumber++)
records[curveNumber].statusFlags = statusFlags;

Expand Down
55 changes: 55 additions & 0 deletions Tests/BACnet.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{5DC7DC72-E57B-418C-ABE7-8A26B28AD953}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>System.IO.BACnet</RootNamespace>
<AssemblyName>BACnet.Tests</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework, Version=3.9.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\BACnet\packages\NUnit.3.9.0\lib\net40\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Base\BacnetBitStringTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BACnet.csproj">
<Project>{66832876-01FC-4B7C-8D92-54195773FABF}</Project>
<Name>BACnet</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
92 changes: 92 additions & 0 deletions Tests/Base/BacnetBitStringTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using NUnit.Framework;

namespace System.IO.BACnet.Base
{
public class BacnetBitStringTests
{
[Test]
public void should_set_bit_test()
{
for (byte i = 0; i < 32; i++)
{
var bitString = new BacnetBitString();
bitString.SetBit(i, true);
Assert.AreEqual(true, bitString.GetBit(i));
}
}

[Test]
public void should_calculate_bits_used_when_setting_bit_to_false_test()
{
var bitString = new BacnetBitString();
bitString.SetBit(2, false);
Assert.AreEqual(3, bitString.bits_used);
}

[Test]
public void should_calculate_bits_used_test()
{
for (byte i = 0; i < 32; i++)
{
var bitString = new BacnetBitString();
bitString.SetBit(i, true);
Assert.AreEqual(i + 1, bitString.bits_used);
}
}

[Test]
public void should_convert_to_uint_test()
{
for (byte i = 0; i < 32; i++)
{
var bitString = new BacnetBitString();
bitString.SetBit(i, true);
Assert.AreEqual((uint)(1 << i), bitString.ConvertToInt());
}
}

[Test]
public void should_set_bit_when_converting_from_uint_test()
{
for (byte i = 0; i < 32; i++)
{
var bitString = BacnetBitString.ConvertFromInt((uint)(1 << i));
Assert.AreEqual(true, bitString.GetBit(i));
}
}

[Test]
public void should_respect_provided_bits_used_value_whe_converting_from_uint_test()
{
const byte bitsUsed = 3;
var bitString = BacnetBitString.ConvertFromInt(0, bitsUsed);
Assert.AreEqual(bitsUsed, bitString.bits_used);
}

[TestCase((uint)0b00000000000000000000000000000000, 0)]
[TestCase((uint)0b00000000000000000000000000000001, 1)]
[TestCase((uint)0b01000000000000000000000000000000, 31)]
[TestCase((uint)0b01111111111111111111111111111111, 31)]
[TestCase(0b10101010101010101010101010101010, 32)]
[TestCase(0b10000000000000000000000000000000, 32)]
[TestCase(0b11111111111111111111111111111111, 32)]
public void should_calculate_bits_used_when_converting_from_uint_test(uint value, byte bitsUsed)
{
var bitString = BacnetBitString.ConvertFromInt(value);
Assert.AreEqual(bitsUsed, bitString.bits_used);
}

[TestCase((uint)0b00000000000000000000000000000000)]
[TestCase((uint)0b00000000000000000000000000000001)]
[TestCase((uint)0b01000000000000000000000000000000)]
[TestCase((uint)0b01111111111111111111111111111111)]
[TestCase(0b10101010101010101010101010101010)]
[TestCase(0b10000000000000000000000000000000)]
[TestCase(0b11111111111111111111111111111111)]
public void should_convert_back_to_uint_when_converting_from_uint_test(uint value)
{
var bitString = BacnetBitString.ConvertFromInt(value);
Assert.AreEqual(value, bitString.ConvertToInt());
}
}
}
36 changes: 36 additions & 0 deletions Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("BACnet.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BACnet.Tests")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("5dc7dc72-e57b-418c-abe7-8a26b28ad953")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
4 changes: 4 additions & 0 deletions Tests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="3.9.0" targetFramework="net40" />
</packages>