Skip to content
Merged
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
70 changes: 56 additions & 14 deletions EnergySoultions.CoAP.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30204.135
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EnergySoultions.CoAP", "EnergySoultions.CoAP\EnergySoultions.CoAP.csproj", "{FECBAFE4-FCD9-4E4C-95B2-EFF7B974CA3A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C2499C75-EDBE-4552-B855-52E2B12BC4E4}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
Expand All @@ -14,6 +12,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
stylecop.json = stylecop.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorldDirect.CoAP", "WorldDirect.CoAP\WorldDirect.CoAP.csproj", "{60BC961D-1A78-4203-9EFB-A7E616170CC5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorldDirect.CoAP.Specs", "WorldDirect.CoAP.Specs\WorldDirect.CoAP.Specs.csproj", "{9832AE15-FBC3-4393-A6B8-365EAA84211A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorldDirect.CoAP.Example.Client", "WorldDirect.CoAP.Example.Client\WorldDirect.CoAP.Example.Client.csproj", "{707A3F6A-961B-4B37-98A8-B7371087F1A9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorldDirect.CoAP.Example.Server", "WorldDirect.CoAP.Example.Server\WorldDirect.CoAP.Example.Server.csproj", "{3EF4EA32-6F5B-4B2E-83BD-08D670D5A361}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -24,18 +30,54 @@ Global
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{FECBAFE4-FCD9-4E4C-95B2-EFF7B974CA3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FECBAFE4-FCD9-4E4C-95B2-EFF7B974CA3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FECBAFE4-FCD9-4E4C-95B2-EFF7B974CA3A}.Debug|x64.ActiveCfg = Debug|Any CPU
{FECBAFE4-FCD9-4E4C-95B2-EFF7B974CA3A}.Debug|x64.Build.0 = Debug|Any CPU
{FECBAFE4-FCD9-4E4C-95B2-EFF7B974CA3A}.Debug|x86.ActiveCfg = Debug|Any CPU
{FECBAFE4-FCD9-4E4C-95B2-EFF7B974CA3A}.Debug|x86.Build.0 = Debug|Any CPU
{FECBAFE4-FCD9-4E4C-95B2-EFF7B974CA3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FECBAFE4-FCD9-4E4C-95B2-EFF7B974CA3A}.Release|Any CPU.Build.0 = Release|Any CPU
{FECBAFE4-FCD9-4E4C-95B2-EFF7B974CA3A}.Release|x64.ActiveCfg = Release|Any CPU
{FECBAFE4-FCD9-4E4C-95B2-EFF7B974CA3A}.Release|x64.Build.0 = Release|Any CPU
{FECBAFE4-FCD9-4E4C-95B2-EFF7B974CA3A}.Release|x86.ActiveCfg = Release|Any CPU
{FECBAFE4-FCD9-4E4C-95B2-EFF7B974CA3A}.Release|x86.Build.0 = Release|Any CPU
{60BC961D-1A78-4203-9EFB-A7E616170CC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{60BC961D-1A78-4203-9EFB-A7E616170CC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60BC961D-1A78-4203-9EFB-A7E616170CC5}.Debug|x64.ActiveCfg = Debug|Any CPU
{60BC961D-1A78-4203-9EFB-A7E616170CC5}.Debug|x64.Build.0 = Debug|Any CPU
{60BC961D-1A78-4203-9EFB-A7E616170CC5}.Debug|x86.ActiveCfg = Debug|Any CPU
{60BC961D-1A78-4203-9EFB-A7E616170CC5}.Debug|x86.Build.0 = Debug|Any CPU
{60BC961D-1A78-4203-9EFB-A7E616170CC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60BC961D-1A78-4203-9EFB-A7E616170CC5}.Release|Any CPU.Build.0 = Release|Any CPU
{60BC961D-1A78-4203-9EFB-A7E616170CC5}.Release|x64.ActiveCfg = Release|Any CPU
{60BC961D-1A78-4203-9EFB-A7E616170CC5}.Release|x64.Build.0 = Release|Any CPU
{60BC961D-1A78-4203-9EFB-A7E616170CC5}.Release|x86.ActiveCfg = Release|Any CPU
{60BC961D-1A78-4203-9EFB-A7E616170CC5}.Release|x86.Build.0 = Release|Any CPU
{9832AE15-FBC3-4393-A6B8-365EAA84211A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9832AE15-FBC3-4393-A6B8-365EAA84211A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9832AE15-FBC3-4393-A6B8-365EAA84211A}.Debug|x64.ActiveCfg = Debug|Any CPU
{9832AE15-FBC3-4393-A6B8-365EAA84211A}.Debug|x64.Build.0 = Debug|Any CPU
{9832AE15-FBC3-4393-A6B8-365EAA84211A}.Debug|x86.ActiveCfg = Debug|Any CPU
{9832AE15-FBC3-4393-A6B8-365EAA84211A}.Debug|x86.Build.0 = Debug|Any CPU
{9832AE15-FBC3-4393-A6B8-365EAA84211A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9832AE15-FBC3-4393-A6B8-365EAA84211A}.Release|Any CPU.Build.0 = Release|Any CPU
{9832AE15-FBC3-4393-A6B8-365EAA84211A}.Release|x64.ActiveCfg = Release|Any CPU
{9832AE15-FBC3-4393-A6B8-365EAA84211A}.Release|x64.Build.0 = Release|Any CPU
{9832AE15-FBC3-4393-A6B8-365EAA84211A}.Release|x86.ActiveCfg = Release|Any CPU
{9832AE15-FBC3-4393-A6B8-365EAA84211A}.Release|x86.Build.0 = Release|Any CPU
{707A3F6A-961B-4B37-98A8-B7371087F1A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{707A3F6A-961B-4B37-98A8-B7371087F1A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{707A3F6A-961B-4B37-98A8-B7371087F1A9}.Debug|x64.ActiveCfg = Debug|Any CPU
{707A3F6A-961B-4B37-98A8-B7371087F1A9}.Debug|x64.Build.0 = Debug|Any CPU
{707A3F6A-961B-4B37-98A8-B7371087F1A9}.Debug|x86.ActiveCfg = Debug|Any CPU
{707A3F6A-961B-4B37-98A8-B7371087F1A9}.Debug|x86.Build.0 = Debug|Any CPU
{707A3F6A-961B-4B37-98A8-B7371087F1A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{707A3F6A-961B-4B37-98A8-B7371087F1A9}.Release|Any CPU.Build.0 = Release|Any CPU
{707A3F6A-961B-4B37-98A8-B7371087F1A9}.Release|x64.ActiveCfg = Release|Any CPU
{707A3F6A-961B-4B37-98A8-B7371087F1A9}.Release|x64.Build.0 = Release|Any CPU
{707A3F6A-961B-4B37-98A8-B7371087F1A9}.Release|x86.ActiveCfg = Release|Any CPU
{707A3F6A-961B-4B37-98A8-B7371087F1A9}.Release|x86.Build.0 = Release|Any CPU
{3EF4EA32-6F5B-4B2E-83BD-08D670D5A361}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3EF4EA32-6F5B-4B2E-83BD-08D670D5A361}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3EF4EA32-6F5B-4B2E-83BD-08D670D5A361}.Debug|x64.ActiveCfg = Debug|Any CPU
{3EF4EA32-6F5B-4B2E-83BD-08D670D5A361}.Debug|x64.Build.0 = Debug|Any CPU
{3EF4EA32-6F5B-4B2E-83BD-08D670D5A361}.Debug|x86.ActiveCfg = Debug|Any CPU
{3EF4EA32-6F5B-4B2E-83BD-08D670D5A361}.Debug|x86.Build.0 = Debug|Any CPU
{3EF4EA32-6F5B-4B2E-83BD-08D670D5A361}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3EF4EA32-6F5B-4B2E-83BD-08D670D5A361}.Release|Any CPU.Build.0 = Release|Any CPU
{3EF4EA32-6F5B-4B2E-83BD-08D670D5A361}.Release|x64.ActiveCfg = Release|Any CPU
{3EF4EA32-6F5B-4B2E-83BD-08D670D5A361}.Release|x64.Build.0 = Release|Any CPU
{3EF4EA32-6F5B-4B2E-83BD-08D670D5A361}.Release|x86.ActiveCfg = Release|Any CPU
{3EF4EA32-6F5B-4B2E-83BD-08D670D5A361}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
12 changes: 12 additions & 0 deletions WorldDirect.CoAP.Example.Client/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace WorldDirect.CoAP.Example.Client
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>
12 changes: 12 additions & 0 deletions WorldDirect.CoAP.Example.Server/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace WorldDirect.CoAP.Example.Server
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>
14 changes: 14 additions & 0 deletions WorldDirect.CoAP.Specs/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using Xunit;

namespace WorldDirect.CoAP.Specs
{
public class UnitTest1
{
[Fact]
public void Test1()
{

}
}
}
16 changes: 16 additions & 0 deletions WorldDirect.CoAP.Specs/WorldDirect.CoAP.Specs.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>

</Project>
140 changes: 140 additions & 0 deletions WorldDirect.CoAP/BlockOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright (c) 2011-2012, Longxiang He <helongxiang@smeshlink.com>,
* SmeshLink Technology Co.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY.
*
* This file is part of the CoAP.NET, a CoAP framework in C#.
* Please see README for more information.
*/

namespace WorldDirect.CoAP
{
using System;

/// <summary>
/// This class describes the block options of the CoAP messages
/// </summary>
public class BlockOption : Option
{
/// <summary>
/// Initializes a block option.
/// </summary>
/// <param name="type">The type of the option</param>
public BlockOption(OptionType type) : base(type)
{
this.IntValue = 0;
}

/// <summary>
/// Initializes a block option.
/// </summary>
/// <param name="type">The type of the option</param>
/// <param name="num">Block number</param>
/// <param name="szx">Block size</param>
/// <param name="m">More flag</param>
public BlockOption(OptionType type, Int32 num, Int32 szx, Boolean m) : base(type)
{
this.IntValue = Encode(num, szx, m);
}

/// <summary>
/// Sets block params.
/// </summary>
/// <param name="num">Block number</param>
/// <param name="szx">Block size</param>
/// <param name="m">More flag</param>
public void SetValue(Int32 num, Int32 szx, Boolean m)
{
this.IntValue = Encode(num, szx, m);
}

/// <summary>
/// Gets or sets the block number.
/// </summary>
public Int32 NUM
{
get { return this.IntValue >> 4; }
set { SetValue(value, SZX, M); }
}

/// <summary>
/// Gets or sets the block size.
/// </summary>
public Int32 SZX
{
get { return this.IntValue & 0x7; }
set { SetValue(NUM, value, M); }
}

/// <summary>
/// Gets or sets the more flag.
/// </summary>
public Boolean M
{
get { return (this.IntValue >> 3 & 0x1) != 0; }
set { SetValue(NUM, SZX, value); }
}

/// <summary>
/// Gets the decoded block size in bytes (B).
/// </summary>
public Int32 Size
{
get { return DecodeSZX(this.SZX); }
}

/// <summary>
///
/// </summary>
/// <returns></returns>
public override String ToString()
{
return String.Format("{0}{1} ({2}B/block [{3}])", NUM, M ? "+" : String.Empty, Size, SZX);
}

/// <summary>
/// Gets the real block size which is 2 ^ (SZX + 4).
/// </summary>
/// <param name="szx"></param>
/// <returns></returns>
public static Int32 DecodeSZX(Int32 szx)
{
return 1 << (szx + 4);
}

/// <summary>
/// Converts a block size into the corresponding SZX.
/// </summary>
/// <param name="blockSize"></param>
/// <returns></returns>
public static Int32 EncodeSZX(Int32 blockSize)
{
if (blockSize < 16)
return 0;
if (blockSize > 1024)
return 6;
return (Int32)(Math.Log(blockSize) / Math.Log(2)) - 4;
}

/// <summary>
/// Checks whether the given SZX is valid or not.
/// </summary>
/// <param name="szx"></param>
/// <returns></returns>
public static Boolean ValidSZX(Int32 szx)
{
return (szx >= 0 && szx <= 6);
}

private static Int32 Encode(Int32 num, Int32 szx, Boolean m)
{
Int32 value = 0;
value |= (szx & 0x7);
value |= (m ? 1 : 0) << 3;
value |= num << 4;
return value;
}
}
}
42 changes: 42 additions & 0 deletions WorldDirect.CoAP/Channel/DataReceivedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2011-2014, Longxiang He <helongxiang@smeshlink.com>,
* SmeshLink Technology Co.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY.
*
* This file is part of the CoAP.NET, a CoAP framework in C#.
* Please see README for more information.
*/

namespace WorldDirect.CoAP.Channel
{
using System;

/// <summary>
/// Provides data for <see cref="IChannel.DataReceived"/> event.
/// </summary>
public class DataReceivedEventArgs : EventArgs
{
readonly Byte[] _data;
readonly System.Net.EndPoint _endPoint;

/// <summary>
/// </summary>
public DataReceivedEventArgs(Byte[] data, System.Net.EndPoint endPoint)
{
_data = data;
_endPoint = endPoint;
}

/// <summary>
/// Gets the received bytes.
/// </summary>
public Byte[] Data { get { return _data; } }

/// <summary>
/// Gets the <see cref="System.Net.EndPoint"/> where the data is received from.
/// </summary>
public System.Net.EndPoint EndPoint { get { return _endPoint; } }
}
}
44 changes: 44 additions & 0 deletions WorldDirect.CoAP/Channel/IChannel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2011-2014, Longxiang He <helongxiang@smeshlink.com>,
* SmeshLink Technology Co.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY.
*
* This file is part of the CoAP.NET, a CoAP framework in C#.
* Please see README for more information.
*/

namespace WorldDirect.CoAP.Channel
{
using System;

/// <summary>
/// Represents a channel where bytes data can flow through.
/// </summary>
public interface IChannel : IDisposable
{
/// <summary>
/// Gets the local endpoint of this channel.
/// </summary>
System.Net.EndPoint LocalEndPoint { get; }
/// <summary>
/// Occurs when some bytes are received in this channel.
/// </summary>
event EventHandler<DataReceivedEventArgs> DataReceived;
/// <summary>
/// Starts this channel.
/// </summary>
void Start();
/// <summary>
/// Stops this channel.
/// </summary>
void Stop();
/// <summary>
/// Sends data through this channel. This method should be non-blocking.
/// </summary>
/// <param name="data">the bytes to send</param>
/// <param name="ep">the target endpoint</param>
void Send(Byte[] data, System.Net.EndPoint ep);
}
}
Loading