Skip to content

Commit

Permalink
Expose SupportBlockSizes in Block1/2 and add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
NZSmartie committed Mar 14, 2018
1 parent 5a0b28b commit fe8f892
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/CoAPNet/CoapBlockStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class CoapBlockStream : Stream
public static int DefaultBlockSize
{
get => _defaultBlockSize;
set => _defaultBlockSize = BlockBase.SupportedBlockSizes.Any(b => b.Item2 == value)
set => _defaultBlockSize = BlockBase.InternalSupportedBlockSizes.Any(b => b.Item2 == value)
? value
: throw new ArgumentOutOfRangeException();
}
Expand Down Expand Up @@ -75,8 +75,8 @@ public int BlockSize
if (value > _blockSize)
throw new ArgumentOutOfRangeException($"Can not increase blocksize from {_blockSize} to {value}");

if (BlockBase.SupportedBlockSizes.All(b => b.Item2 != value))
throw new ArgumentOutOfRangeException($"Unsupported blocksize {value}. Expecting block sizes in ({string.Join(", ", Options.BlockBase.SupportedBlockSizes.Select(b => b.Item2))})");
if (BlockBase.InternalSupportedBlockSizes.All(b => b.Item2 != value))
throw new ArgumentOutOfRangeException($"Unsupported blocksize {value}. Expecting block sizes in ({string.Join(", ", Options.BlockBase.InternalSupportedBlockSizes.Select(b => b.Item2))})");

_blockSize = value;
}
Expand Down
73 changes: 67 additions & 6 deletions src/CoAPNet/Options/BlockWise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

namespace CoAPNet.Options
{
/// <summary>
/// Base class used with <see cref="Block1"/> and <see cref="Block2"/> as both sublasses share similarities.
/// </summary>
public class BlockBase : CoapOption
{
internal static readonly List<Tuple<int, int>> SupportedBlockSizes = new List<Tuple<int, int>>
internal static readonly List<Tuple<int, int>> InternalSupportedBlockSizes = new List<Tuple<int, int>>
{
Tuple.Create(0, 16),
Tuple.Create(1, 32),
Expand All @@ -19,7 +22,19 @@ public class BlockBase : CoapOption
// Value of 7, 2048 is reserved
};

public BlockBase(int optionNumber, int blockNumber, int blockSize, bool more) : this(optionNumber)
/// <summary>
/// A list of sizes that are supported (as defined in RFC 7959)
/// </summary>
public static readonly IReadOnlyList<int> SupportedBlockSizes = InternalSupportedBlockSizes.Select(t => t.Item2).ToList();

/// <summary>
/// Mostly for internal use unless a custom block-wise CoAP Option is requried.
/// </summary>
/// <param name="optionNumber">The CoAP Option number.</param>
/// <param name="blockNumber">The current block number.</param>
/// <param name="blockSize">The size of the Block-Wise transfer blocks</param>
/// <param name="more">Flag to indicate or request more blocks.</param>
protected BlockBase(int optionNumber, int blockNumber, int blockSize, bool more) : this(optionNumber)
{
BlockNumber = blockNumber;
BlockSize = blockSize;
Expand All @@ -32,21 +47,30 @@ internal BlockBase(int optionNumber)

private int _blockSize = 1024;

/// <summary>
/// Gets or sets the block size. See <see cref="SupportedBlockSizes"/>
/// </summary>
public int BlockSize
{
get => _blockSize;
set
{
if (!SupportedBlockSizes.Any(b => b.Item2 == value))
throw new ArgumentOutOfRangeException($"Unsupported blocksize {value}. Expecting block sizes in ({string.Join(", ", Options.BlockBase.SupportedBlockSizes.Select(b => b.Item2))})");
if (!InternalSupportedBlockSizes.Any(b => b.Item2 == value))
throw new ArgumentOutOfRangeException($"Unsupported blocksize {value}. Expecting block sizes in ({string.Join(", ", Options.BlockBase.InternalSupportedBlockSizes.Select(b => b.Item2))})");
_blockSize = value;
}
}

/// <summary>
/// Gets or sets a flag to indicate more blocks to follow in the transfer.
/// </summary>
public bool IsMoreFollowing { get; set; }

private int _blockNumber = 0;

/// <summary>
/// Gets or Sets the current block number in the transfer.
/// </summary>
public virtual int BlockNumber
{
get => _blockNumber;
Expand All @@ -61,6 +85,7 @@ public virtual int BlockNumber
}
}

/// <inheritdoc/>
public override int Length
{
get
Expand All @@ -77,6 +102,7 @@ public override int Length
}
}

/// <inheritdoc/>
public override void FromBytes(byte[] data)
{
base.FromBytes(data);
Expand Down Expand Up @@ -109,12 +135,13 @@ public override void FromBytes(byte[] data)

IsMoreFollowing = (last & 0x01) > 0;
var szx = (int)((last >> 1));
BlockSize = SupportedBlockSizes.First(b => b.Item1 == szx).Item2;
BlockSize = InternalSupportedBlockSizes.First(b => b.Item1 == szx).Item2;
}

/// <inheritdoc/>
public override byte[] GetBytes()
{
var szx = SupportedBlockSizes.First(b => b.Item2 == BlockSize).Item1;
var szx = InternalSupportedBlockSizes.First(b => b.Item2 == BlockSize).Item1;
var last = (byte)((szx & 0x07) << 5) | (IsMoreFollowing ? 0x10 : 0x00);

if (BlockNumber <= 0x0F)
Expand All @@ -134,6 +161,9 @@ public override byte[] GetBytes()
}
}

/// <summary>
/// Block1 CoAP Option (as Defined in RFC 7959), informs the server of an incomming block-wise transfer request.
/// </summary>
public sealed class Block1 : BlockBase
{
/// <summary>
Expand All @@ -144,15 +174,27 @@ public sealed class Block1 : BlockBase
/// </remarks>
public override int BlockNumber { get => base.BlockNumber; set => base.BlockNumber = value; }

/// <summary>
/// Creates a new Block1 request CoAP option
/// </summary>
public Block1()
: base(CoapRegisteredOptionNumber.Block1)
{ }

/// <summary>
/// Creates a new Block1 request CoAP option
/// </summary>
/// <param name="blockNumber">The current block number.</param>
/// <param name="blockSize">The size of the Block-Wise transfer blocks</param>
/// <param name="more">Flag to indicate or request more blocks.</param>
public Block1(int blockNumber = 0, int blockSize = 256, bool more = false)
: base(CoapRegisteredOptionNumber.Block1, blockNumber, blockSize, more)
{ }
}

/// <summary>
/// Block2 CoAP Option (as Defined in RFC 7959), informs the client of an incomming block-wise transfer response.
/// </summary>
public sealed class Block2 : BlockBase
{
/// <summary>
Expand All @@ -164,20 +206,39 @@ public sealed class Block2 : BlockBase
/// </remarks>
public override int BlockNumber { get => base.BlockNumber; set => base.BlockNumber = value; }

/// <summary>
/// Creates a new Block1 response CoAP option
/// </summary>
public Block2()
: base(CoapRegisteredOptionNumber.Block2)
{ }

/// <summary>
/// Creates a new Block2 response CoAP option
/// </summary>
/// <param name="blockNumber">The current block number.</param>
/// <param name="blockSize">The size of the Block-Wise transfer blocks</param>
/// <param name="more">Flag to indicate or request more blocks.</param>
public Block2(int blockNumber = 0, int blockSize = 256, bool more = false)
: base(CoapRegisteredOptionNumber.Block2, blockNumber, blockSize, more)
{ }
}

/// <summary>
/// A CoAP Option (as defiend in RFC7959), represents a request for the content's size, or (as a response from the server) the final size of the content.
/// </summary>
public sealed class Size2 : CoapOption
{
/// <summary>
/// Creates a new Size2 CoAP Option.
/// </summary>
public Size2() : base(CoapRegisteredOptionNumber.Size2, 0, 4, false, OptionType.UInt, 0u)
{ }

/// <summary>
/// Creates a new Size2 CoAP Option.
/// </summary>
/// <param name="value">The final size of the content.</param>
public Size2(uint value) : this()
{
ValueUInt = value;
Expand Down

0 comments on commit fe8f892

Please sign in to comment.