From 53ef3085e3b17e56499e0075aed5a8fd5cc70443 Mon Sep 17 00:00:00 2001 From: Steven Kuhn Date: Thu, 7 Mar 2024 09:06:20 -0600 Subject: [PATCH] Standardize on C# 10, no implicit usings, and file-scope namespaces --- .../Devices/AddDeviceRequest.cs | 18 +- .../Devices/AddDeviceResponse.cs | 18 +- .../Firmware/FirmwareClient.cs | 6 +- .../Firmware/GetFirmwareVersionResponse.cs | 18 +- .../Firmware/GetFirmwareVersionsResponse.cs | 12 +- .../Identity/IdentityManager.cs | 14 +- .../Meadow.Cloud.Client.csproj | 31 +- .../MeadowCloudResponse.cs | 23 +- .../Services/ApiTokenService.cs | 84 ++- .../Users/GetOrganizationResponse.cs | 15 +- .../Users/GetUserResponse.cs | 21 +- .../Meadow.Cloud.Client/Users/UserClient.cs | 2 +- Source/v2/Meadow.Cloud.Client/Usings.cs | 7 + Source/v2/Meadow.Dfu/DfuSharp.cs | 6 +- Source/v2/Meadow.Dfu/DfuUtils.cs | 5 + Source/v2/Meadow.Dfu/Meadow.Dfu.csproj | 1 - Source/v2/Meadow.Hcom/CobsTools.cs | 157 +++-- Source/v2/Meadow.Hcom/ConnectionManager.cs | 39 +- Source/v2/Meadow.Hcom/ConnectionState.cs | 13 +- .../Meadow.Hcom/Connections/ConnectionBase.cs | 4 +- .../Connections/LocalConnection.cs | 4 +- .../SerialConnection.ListenerProc.cs | 3 +- .../Connections/SerialConnection.cs | 5 +- .../Connections/SimulatorConnection.cs | 4 +- .../Meadow.Hcom/Connections/TcpConnection.cs | 5 +- .../Meadow.Hcom/Debugging/DebuggingServer.cs | 624 +++++++++--------- Source/v2/Meadow.Hcom/DeviceInfo.cs | 155 +++-- .../v2/Meadow.Hcom/DeviceNotFoundException.cs | 9 +- .../Firmware/DownloadFileStream.cs | 4 +- .../Meadow.Hcom/Firmware/ReleaseMetadata.cs | 4 +- .../HttpResponses/DeviceInfoHttpResponse.cs | 4 +- Source/v2/Meadow.Hcom/IConnectionListener.cs | 23 +- Source/v2/Meadow.Hcom/IMeadowConnection.cs | 99 ++- Source/v2/Meadow.Hcom/IMeadowDevice.cs | 59 +- Source/v2/Meadow.Hcom/Meadow.HCom.csproj | 2 - Source/v2/Meadow.Hcom/MeadowDevice.cs | 267 ++++---- Source/v2/Meadow.Hcom/Protocol.cs | 71 +- Source/v2/Meadow.Hcom/ProtocolType.cs | 47 +- .../SerialRequests/DebuggerDataRequest.cs | 27 +- .../SerialRequests/FileDeleteRequest.cs | 4 +- .../SerialRequests/FileInitialBytesRequest.cs | 4 +- .../SerialRequests/GetDeviceInfoRequest.cs | 23 +- .../SerialRequests/GetFileListRequest.cs | 4 +- .../SerialRequests/InitFileReadRequest.cs | 4 +- .../SerialRequests/InitFileWriteRequest.cs | 4 +- .../SerialRequests/RequestBuilder.cs | 27 +- .../SerialRequests/ResetDeviceRequest.cs | 23 +- .../SerialRequests/SetRtcTimeRequest.cs | 4 +- .../SerialRequests/StartDebuggingRequest.cs | 9 +- .../DeviceInfoSerialResponse.cs | 4 +- .../FileDownloadFailedResponse.cs | 4 +- .../FileInitialBytesSerialResponse.cs | 4 +- .../FileWriteInitFailedSerialResponse.cs | 5 +- .../ReconnectRequiredResponse.cs | 4 +- .../RequestErrorTextResponse.cs | 4 +- .../SerialResponses/TextAcceptedResponse.cs | 4 +- .../SerialResponses/TextCrcMemberResponse.cs | 4 +- .../TextInformationResponse.cs | 4 +- .../SerialResponses/TextListMemberResponse.cs | 4 +- .../TextRequestRejectedResponse.cs | 5 +- .../SerialResponses/TextRequestResponse.cs | 4 +- .../SerialResponses/TextStdErrResponse.cs | 4 +- .../SerialResponses/TextStdOutResponse.cs | 4 +- Source/v2/Meadow.Hcom/Usings.cs | 12 + 64 files changed, 1063 insertions(+), 1023 deletions(-) create mode 100644 Source/v2/Meadow.Hcom/Usings.cs diff --git a/Source/v2/Meadow.Cloud.Client/Devices/AddDeviceRequest.cs b/Source/v2/Meadow.Cloud.Client/Devices/AddDeviceRequest.cs index b0aacbf4..a548bf59 100644 --- a/Source/v2/Meadow.Cloud.Client/Devices/AddDeviceRequest.cs +++ b/Source/v2/Meadow.Cloud.Client/Devices/AddDeviceRequest.cs @@ -1,7 +1,14 @@ namespace Meadow.Cloud.Client.Devices; -public class AddDeviceRequest(string id, string orgId, string publicKey) +public class AddDeviceRequest { + public AddDeviceRequest(string id, string orgId, string publicKey) + { + Id = id; + OrgId = orgId; + PublicKey = publicKey; + } + public AddDeviceRequest(string id, string name, string orgId, string publicKey) : this(id, orgId, publicKey) { @@ -9,24 +16,23 @@ public AddDeviceRequest(string id, string name, string orgId, string publicKey) } public AddDeviceRequest(string id, string name, string orgId, string collectionId, string publicKey) - : this(id, orgId, publicKey) + : this(id, name, orgId, publicKey) { - Name = name; CollectionId = collectionId; } [JsonPropertyName("id")] - public string Id { get; set; } = id; + public string Id { get; set; } [JsonPropertyName("name")] public string? Name { get; set; } [JsonPropertyName("orgId")] - public string OrgId { get; set; } = orgId; + public string OrgId { get; set; } [JsonPropertyName("collectionId")] public string? CollectionId { get; set; } [JsonPropertyName("publicKey")] - public string PublicKey { get; set; } = publicKey; + public string PublicKey { get; set; } } diff --git a/Source/v2/Meadow.Cloud.Client/Devices/AddDeviceResponse.cs b/Source/v2/Meadow.Cloud.Client/Devices/AddDeviceResponse.cs index 529576da..4894fb76 100644 --- a/Source/v2/Meadow.Cloud.Client/Devices/AddDeviceResponse.cs +++ b/Source/v2/Meadow.Cloud.Client/Devices/AddDeviceResponse.cs @@ -1,16 +1,24 @@ namespace Meadow.Cloud.Client.Devices; -public class AddDeviceResponse(string id, string name, string orgId, string collectionId) +public class AddDeviceResponse { + public AddDeviceResponse(string id, string name, string orgId, string collectionId) + { + Id = id; + Name = name; + OrgId = orgId; + CollectionId = collectionId; + } + [JsonPropertyName("id")] - public string Id { get; set; } = id; + public string Id { get; set; } [JsonPropertyName("name")] - public string? Name { get; set; } = name; + public string? Name { get; set; } [JsonPropertyName("orgId")] - public string OrgId { get; set; } = orgId; + public string OrgId { get; set; } [JsonPropertyName("collectionId")] - public string? CollectionId { get; set; } = collectionId; + public string? CollectionId { get; set; } } \ No newline at end of file diff --git a/Source/v2/Meadow.Cloud.Client/Firmware/FirmwareClient.cs b/Source/v2/Meadow.Cloud.Client/Firmware/FirmwareClient.cs index e6ab9142..452d27e6 100644 --- a/Source/v2/Meadow.Cloud.Client/Firmware/FirmwareClient.cs +++ b/Source/v2/Meadow.Cloud.Client/Firmware/FirmwareClient.cs @@ -1,4 +1,6 @@ -namespace Meadow.Cloud.Client.Firmware; +using System.Linq; + +namespace Meadow.Cloud.Client.Firmware; public class FirmwareClient : MeadowCloudClientBase, IFirmwareClient { @@ -19,7 +21,7 @@ public async Task> GetVersions(string t if (response.StatusCode == HttpStatusCode.NotFound) { - return []; + return Enumerable.Empty(); } return await ProcessResponse>(response, cancellationToken); diff --git a/Source/v2/Meadow.Cloud.Client/Firmware/GetFirmwareVersionResponse.cs b/Source/v2/Meadow.Cloud.Client/Firmware/GetFirmwareVersionResponse.cs index ad1a2e88..f377cc68 100644 --- a/Source/v2/Meadow.Cloud.Client/Firmware/GetFirmwareVersionResponse.cs +++ b/Source/v2/Meadow.Cloud.Client/Firmware/GetFirmwareVersionResponse.cs @@ -1,16 +1,24 @@ namespace Meadow.Cloud.Client.Firmware; -public class GetFirmwareVersionResponse(string version, string minCLIVersion, string downloadUrl, string networkDownloadUrl) +public class GetFirmwareVersionResponse { + public GetFirmwareVersionResponse(string version, string minCLIVersion, string downloadUrl, string networkDownloadUrl) + { + Version = version; + MinCLIVersion = minCLIVersion; + DownloadUrl = downloadUrl; + NetworkDownloadUrl = networkDownloadUrl; + } + [JsonPropertyName("version")] - public string Version { get; set; } = version; + public string Version { get; set; } [JsonPropertyName("minCLIVersion")] - public string MinCLIVersion { get; set; } = minCLIVersion; + public string MinCLIVersion { get; set; } [JsonPropertyName("downloadUrl")] - public string DownloadUrl { get; set; } = downloadUrl; + public string DownloadUrl { get; set; } [JsonPropertyName("networkDownloadUrl")] - public string NetworkDownloadUrl { get; set; } = networkDownloadUrl; + public string NetworkDownloadUrl { get; set; } } diff --git a/Source/v2/Meadow.Cloud.Client/Firmware/GetFirmwareVersionsResponse.cs b/Source/v2/Meadow.Cloud.Client/Firmware/GetFirmwareVersionsResponse.cs index 6a4e066a..f3bf5561 100644 --- a/Source/v2/Meadow.Cloud.Client/Firmware/GetFirmwareVersionsResponse.cs +++ b/Source/v2/Meadow.Cloud.Client/Firmware/GetFirmwareVersionsResponse.cs @@ -1,10 +1,16 @@ namespace Meadow.Cloud.Client.Firmware; -public class GetFirmwareVersionsResponse(string version, DateTimeOffset lastModifiedAt) +public class GetFirmwareVersionsResponse { + public GetFirmwareVersionsResponse(string version, DateTimeOffset lastModifiedAt) + { + Version = version; + LastModifiedAt = lastModifiedAt; + } + [JsonPropertyName("version")] - public string Version { get; set; } = version; + public string Version { get; set; } [JsonPropertyName("lastModifiedAt")] - public DateTimeOffset LastModifiedAt { get; set; } = lastModifiedAt; + public DateTimeOffset LastModifiedAt { get; set; } } diff --git a/Source/v2/Meadow.Cloud.Client/Identity/IdentityManager.cs b/Source/v2/Meadow.Cloud.Client/Identity/IdentityManager.cs index 117d7cb3..3e6665f4 100644 --- a/Source/v2/Meadow.Cloud.Client/Identity/IdentityManager.cs +++ b/Source/v2/Meadow.Cloud.Client/Identity/IdentityManager.cs @@ -313,12 +313,18 @@ private void OpenBrowser(string url) } } - private class AccessToken(string token, DateTimeOffset expiresAt, string emailAddress) + private class AccessToken { + public AccessToken(string token, DateTimeOffset expiresAt, string emailAddress) + { + Token = token; + ExpiresAtUtc = expiresAt; + EmailAddress = emailAddress; + } - public string Token { get; } = token; - public DateTimeOffset ExpiresAtUtc { get; } = expiresAt; - public string EmailAddress { get; } = emailAddress; + public string Token { get; } + public DateTimeOffset ExpiresAtUtc { get; } + public string EmailAddress { get; } public bool IsValid => !string.IsNullOrWhiteSpace(Token) && ExpiresAtUtc > DateTimeOffset.UtcNow.AddMinutes(30); } diff --git a/Source/v2/Meadow.Cloud.Client/Meadow.Cloud.Client.csproj b/Source/v2/Meadow.Cloud.Client/Meadow.Cloud.Client.csproj index a441e711..c0d51f6e 100644 --- a/Source/v2/Meadow.Cloud.Client/Meadow.Cloud.Client.csproj +++ b/Source/v2/Meadow.Cloud.Client/Meadow.Cloud.Client.csproj @@ -1,18 +1,19 @@  - - netstandard2.0 - enable - enable - True - 12 - + + + netstandard2.0 + enable + True + 10 + - - - - - - - - + + + + + + + + + diff --git a/Source/v2/Meadow.Cloud.Client/MeadowCloudResponse.cs b/Source/v2/Meadow.Cloud.Client/MeadowCloudResponse.cs index d4ee19e5..97b85d81 100644 --- a/Source/v2/Meadow.Cloud.Client/MeadowCloudResponse.cs +++ b/Source/v2/Meadow.Cloud.Client/MeadowCloudResponse.cs @@ -1,17 +1,28 @@ namespace Meadow.Cloud.Client; -public class MeadowCloudResponse(HttpStatusCode statusCode, IReadOnlyDictionary> headers) +public class MeadowCloudResponse { + public MeadowCloudResponse(HttpStatusCode statusCode, IReadOnlyDictionary> headers) + { + StatusCode = statusCode; + Headers = headers; + } + public MeadowCloudResponse(HttpStatusCode statusCode) : this(statusCode, new Dictionary>()) { } - public HttpStatusCode StatusCode { get; } = statusCode; - public IReadOnlyDictionary> Headers { get; } = headers; + public HttpStatusCode StatusCode { get; } + public IReadOnlyDictionary> Headers { get; } } -public class MeadowCloudResponse(HttpStatusCode statusCode, IReadOnlyDictionary> headers, TResult result) - : MeadowCloudResponse(statusCode, headers) +public class MeadowCloudResponse : MeadowCloudResponse { + public MeadowCloudResponse(HttpStatusCode statusCode, IReadOnlyDictionary> headers, TResult result) + : base(statusCode, headers) + { + Result = result; + } + public MeadowCloudResponse(HttpStatusCode statusCode, TResult result) : this(statusCode, new Dictionary>(), result) { } - public TResult Result { get; } = result; + public TResult Result { get; } } diff --git a/Source/v2/Meadow.Cloud.Client/Services/ApiTokenService.cs b/Source/v2/Meadow.Cloud.Client/Services/ApiTokenService.cs index 621e2d20..1aa41c76 100644 --- a/Source/v2/Meadow.Cloud.Client/Services/ApiTokenService.cs +++ b/Source/v2/Meadow.Cloud.Client/Services/ApiTokenService.cs @@ -68,40 +68,78 @@ public async Task DeleteApiToken(string id, string host, CancellationToken? canc } } -public class GetApiTokenResponse(string id, string name, DateTimeOffset expiresAt, string[] scopes) +public class GetApiTokenResponse { - public string Id { get; set; } = id; - public string Name { get; set; } = name; - public DateTimeOffset ExpiresAt { get; set; } = expiresAt; - public string[] Scopes { get; set; } = scopes; + public GetApiTokenResponse(string id, string name, DateTimeOffset expiresAt, string[] scopes) + { + Id = id; + Name = name; + ExpiresAt = expiresAt; + Scopes = scopes; + } + + public string Id { get; set; } + public string Name { get; set; } + public DateTimeOffset ExpiresAt { get; set; } + public string[] Scopes { get; set; } } -public class CreateApiTokenRequest(string name, int duration, string[] scopes) +public class CreateApiTokenRequest { - public string Name { get; set; } = name; - public int Duration { get; set; } = duration; - public string[] Scopes { get; set; } = scopes; + public CreateApiTokenRequest(string name, int duration, string[] scopes) + { + Name = name; + Duration = duration; + Scopes = scopes; + } + + public string Name { get; set; } + public int Duration { get; set; } + public string[] Scopes { get; set; } } -public class CreateApiTokenResponse(string id, string name, DateTimeOffset expiresAt, string[] scopes, string token) +public class CreateApiTokenResponse { - public string Id { get; set; } = id; - public string Name { get; set; } = name; - public DateTimeOffset ExpiresAt { get; set; } = expiresAt; - public string[] Scopes { get; set; } = scopes; - public string Token { get; set; } = token; + public CreateApiTokenResponse(string id, string name, DateTimeOffset expiresAt, string[] scopes, string token) + { + Id = id; + Name = name; + ExpiresAt = expiresAt; + Scopes = scopes; + Token = token; + } + + public string Id { get; set; } + public string Name { get; set; } + public DateTimeOffset ExpiresAt { get; set; } + public string[] Scopes { get; set; } + public string Token { get; set; } } -public class UpdateApiTokenRequest(string name, string[] scopes) +public class UpdateApiTokenRequest { - public string Name { get; set; } = name; - public string[] Scopes { get; set; } = scopes; + public UpdateApiTokenRequest(string name, string[] scopes) + { + Name = name; + Scopes = scopes; + } + + public string Name { get; set; } + public string[] Scopes { get; set; } } -public class UpdateApiTokenResponse(string id, string name, DateTimeOffset expiresAt, string[] scopes) +public class UpdateApiTokenResponse { - public string Id { get; set; } = id; - public string Name { get; set; } = name; - public DateTimeOffset ExpiresAt { get; set; } = expiresAt; - public string[] Scopes { get; set; } = scopes; + public UpdateApiTokenResponse(string id, string name, DateTimeOffset expiresAt, string[] scopes) + { + Id = id; + Name = name; + ExpiresAt = expiresAt; + Scopes = scopes; + } + + public string Id { get; set; } + public string Name { get; set; } + public DateTimeOffset ExpiresAt { get; set; } + public string[] Scopes { get; set; } } diff --git a/Source/v2/Meadow.Cloud.Client/Users/GetOrganizationResponse.cs b/Source/v2/Meadow.Cloud.Client/Users/GetOrganizationResponse.cs index b690d49f..a974ffaa 100644 --- a/Source/v2/Meadow.Cloud.Client/Users/GetOrganizationResponse.cs +++ b/Source/v2/Meadow.Cloud.Client/Users/GetOrganizationResponse.cs @@ -1,13 +1,20 @@ namespace Meadow.Cloud.Client.Users; -public class GetOrganizationResponse(string id, string name, string defaultCollectionId) +public class GetOrganizationResponse { + public GetOrganizationResponse(string id, string name, string defaultCollectionId) + { + Id = id; + Name = name; + DefaultCollectionId = defaultCollectionId; + } + [JsonPropertyName("id")] - public string Id { get; set; } = id; + public string Id { get; set; } [JsonPropertyName("name")] - public string Name { get; set; } = name; + public string Name { get; set; } [JsonPropertyName("defaultCollectionId")] - public string DefaultCollectionId { get; set; } = defaultCollectionId; + public string DefaultCollectionId { get; set; } } diff --git a/Source/v2/Meadow.Cloud.Client/Users/GetUserResponse.cs b/Source/v2/Meadow.Cloud.Client/Users/GetUserResponse.cs index f0078852..fbe1de23 100644 --- a/Source/v2/Meadow.Cloud.Client/Users/GetUserResponse.cs +++ b/Source/v2/Meadow.Cloud.Client/Users/GetUserResponse.cs @@ -1,19 +1,28 @@ namespace Meadow.Cloud.Client.Users; -public class GetUserResponse(string id, string email, string firstName, string lastName, string fullName) +public class GetUserResponse { + public GetUserResponse(string id, string email, string firstName, string lastName, string fullName) + { + Id = id; + Email = email; + FirstName = firstName; + LastName = lastName; + FullName = fullName; + } + [JsonPropertyName("id")] - public string Id { get; set; } = id; + public string Id { get; set; } [JsonPropertyName("email")] - public string Email { get; set; } = email; + public string Email { get; set; } [JsonPropertyName("firstName")] - public string FirstName { get; set; } = firstName; + public string FirstName { get; set; } [JsonPropertyName("lastName")] - public string LastName { get; set; } = lastName; + public string LastName { get; set; } [JsonPropertyName("fullName")] - public string FullName { get; set; } = fullName; + public string FullName { get; set; } } diff --git a/Source/v2/Meadow.Cloud.Client/Users/UserClient.cs b/Source/v2/Meadow.Cloud.Client/Users/UserClient.cs index 377c7b64..81637206 100644 --- a/Source/v2/Meadow.Cloud.Client/Users/UserClient.cs +++ b/Source/v2/Meadow.Cloud.Client/Users/UserClient.cs @@ -14,7 +14,7 @@ public async Task> GetOrganizations(Cancell if (response.StatusCode == HttpStatusCode.NotFound) { - return []; + return Enumerable.Empty(); } return await ProcessResponse>(response, cancellationToken); diff --git a/Source/v2/Meadow.Cloud.Client/Usings.cs b/Source/v2/Meadow.Cloud.Client/Usings.cs index 1afab391..82ef8b3a 100644 --- a/Source/v2/Meadow.Cloud.Client/Usings.cs +++ b/Source/v2/Meadow.Cloud.Client/Usings.cs @@ -1,10 +1,17 @@ global using Meadow.Cloud.Client.Identity; global using Microsoft.Extensions.Logging; global using Microsoft.Extensions.Logging.Abstractions; +global using System; +global using System.Collections.Generic; global using System.Diagnostics; +global using System.IO; +global using System.Linq; global using System.Net; +global using System.Net.Http; global using System.Net.Http.Headers; global using System.Net.Http.Json; global using System.Text; global using System.Text.Json; global using System.Text.Json.Serialization; +global using System.Threading; +global using System.Threading.Tasks; diff --git a/Source/v2/Meadow.Dfu/DfuSharp.cs b/Source/v2/Meadow.Dfu/DfuSharp.cs index 13faa55f..027fff53 100644 --- a/Source/v2/Meadow.Dfu/DfuSharp.cs +++ b/Source/v2/Meadow.Dfu/DfuSharp.cs @@ -1,5 +1,9 @@ -using System.Diagnostics; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; using System.Runtime.InteropServices; +using System.Threading; namespace DfuSharp { diff --git a/Source/v2/Meadow.Dfu/DfuUtils.cs b/Source/v2/Meadow.Dfu/DfuUtils.cs index 3d6611a6..954bf6f0 100644 --- a/Source/v2/Meadow.Dfu/DfuUtils.cs +++ b/Source/v2/Meadow.Dfu/DfuUtils.cs @@ -1,10 +1,15 @@ using Meadow.Hcom; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using System; using System.ComponentModel; using System.Diagnostics; +using System.IO; using System.IO.Compression; +using System.Net.Http; using System.Runtime.InteropServices; +using System.Threading; +using System.Threading.Tasks; namespace Meadow.CLI.Core.Internals.Dfu; diff --git a/Source/v2/Meadow.Dfu/Meadow.Dfu.csproj b/Source/v2/Meadow.Dfu/Meadow.Dfu.csproj index 9f78e4cc..c00495da 100644 --- a/Source/v2/Meadow.Dfu/Meadow.Dfu.csproj +++ b/Source/v2/Meadow.Dfu/Meadow.Dfu.csproj @@ -2,7 +2,6 @@ netstandard2.0 - enable enable 10 diff --git a/Source/v2/Meadow.Hcom/CobsTools.cs b/Source/v2/Meadow.Hcom/CobsTools.cs index 8736353a..4949f366 100644 --- a/Source/v2/Meadow.Hcom/CobsTools.cs +++ b/Source/v2/Meadow.Hcom/CobsTools.cs @@ -1,101 +1,100 @@ -namespace Meadow.Hcom +namespace Meadow.Hcom; + +internal static class CobsTools { - internal static class CobsTools + //============================================================== + // Consistent Overhead Byte Stuffing (COBS) is a scheme to take binary data + // replace an arbituary byte value, usually 0x00, with an encoding that replaces + // this value, in a way, that allows the orginal data can be recovered while + // creating frames around the data. + // + // The following C# code was ported from a 'C' example licensed under MIT License + // https://github.com/bakercp/PacketSerial/blob/master/src/Encoding/COBS.h. + // After porting, I found errors. I referred to the original authors paper at + // http://conferences.sigcomm.org/sigcomm/1997/papers/p062.pdf for additional insights. + // Modifications were needed and adding a starting offset to support large buffers was + // added to allow sub-segments to be encoded. + // + public static int CobsEncoding(byte[] source, int startingSkip, int length, + ref byte[] encoded, int encodeSkip) { - //============================================================== - // Consistent Overhead Byte Stuffing (COBS) is a scheme to take binary data - // replace an arbituary byte value, usually 0x00, with an encoding that replaces - // this value, in a way, that allows the orginal data can be recovered while - // creating frames around the data. - // - // The following C# code was ported from a 'C' example licensed under MIT License - // https://github.com/bakercp/PacketSerial/blob/master/src/Encoding/COBS.h. - // After porting, I found errors. I referred to the original authors paper at - // http://conferences.sigcomm.org/sigcomm/1997/papers/p062.pdf for additional insights. - // Modifications were needed and adding a starting offset to support large buffers was - // added to allow sub-segments to be encoded. - // - public static int CobsEncoding(byte[] source, int startingSkip, int length, - ref byte[] encoded, int encodeSkip) + int sourceOffset = startingSkip; // Offset into source buffer + // Add 1 because first byte filled with first replaceValue value + int encodedOffset = 1; // Offset into destination buffer + int replaceOffset = 0; // Offset where replaceValue is being tracked + byte replaceValue = 1; // Value of the offset to the next delimiter + try { - int sourceOffset = startingSkip; // Offset into source buffer - // Add 1 because first byte filled with first replaceValue value - int encodedOffset = 1; // Offset into destination buffer - int replaceOffset = 0; // Offset where replaceValue is being tracked - byte replaceValue = 1; // Value of the offset to the next delimiter - try + while (sourceOffset < length + startingSkip) { - while (sourceOffset < length + startingSkip) + // Is source value the one we must replace? + if (source[sourceOffset] == 0x00) + { + encoded[encodeSkip + replaceOffset] = replaceValue; // Replace original value with offset to replaceValue + replaceOffset = encodedOffset++; // Update replace offset and bump encoded offset + replaceValue = 1; // Reset replaceValue + } + else { - // Is source value the one we must replace? - if (source[sourceOffset] == 0x00) + if (encodeSkip + encodedOffset == encoded.Length) { - encoded[encodeSkip + replaceOffset] = replaceValue; // Replace original value with offset to replaceValue - replaceOffset = encodedOffset++; // Update replace offset and bump encoded offset - replaceValue = 1; // Reset replaceValue + Console.WriteLine($"encodeSkip + encodedOffset == encoded.Length"); + return -1; } - else - { - if (encodeSkip + encodedOffset == encoded.Length) - { - Console.WriteLine($"encodeSkip + encodedOffset == encoded.Length"); - return -1; - } - encoded[encodeSkip + encodedOffset++] = source[sourceOffset]; // Just copy original value - replaceValue++; // Keep zero offset tracker + encoded[encodeSkip + encodedOffset++] = source[sourceOffset]; // Just copy original value + replaceValue++; // Keep zero offset tracker - // replaceValue has been tracking the delimiter offset. If it's 0xff then - // special action is needed, because we reached the end of a 254 byte block - // of data. And now encoding starts like at the first. - if (replaceValue == 0xff) // Signals maximum possible offset - { - encoded[encodeSkip + replaceOffset] = replaceValue; - replaceOffset = encodedOffset++; - replaceValue = 1; - } + // replaceValue has been tracking the delimiter offset. If it's 0xff then + // special action is needed, because we reached the end of a 254 byte block + // of data. And now encoding starts like at the first. + if (replaceValue == 0xff) // Signals maximum possible offset + { + encoded[encodeSkip + replaceOffset] = replaceValue; + replaceOffset = encodedOffset++; + replaceValue = 1; } - sourceOffset++; // Point to next source value } + sourceOffset++; // Point to next source value } - catch (Exception except) - { - Console.WriteLine($"An exception was caught: {except}"); - Thread.Sleep(10 * 60 * 1000); // Sleep for 10 minutes - throw; - } - - // Last character - encoded[encodeSkip + replaceOffset] = replaceValue; - return encodedOffset; // Number of bytes written to result buffer } - - //--------------------------------------------------------------------------- - public static int CobsDecoding(byte[] encoded, int length, ref byte[] decoded) + catch (Exception except) { - int encodedOffset = 0; // Offset into original (encoded) buffer - int decodedOffset = 0; // Offset into destination (decoded) buffer - byte replaceValue = 0; // Value that will be inserted to indicate replaced value + Console.WriteLine($"An exception was caught: {except}"); + Thread.Sleep(10 * 60 * 1000); // Sleep for 10 minutes + throw; + } - while (encodedOffset < length) - { - replaceValue = encoded[encodedOffset]; // Grab next byte + // Last character + encoded[encodeSkip + replaceOffset] = replaceValue; + return encodedOffset; // Number of bytes written to result buffer + } + + //--------------------------------------------------------------------------- + public static int CobsDecoding(byte[] encoded, int length, ref byte[] decoded) + { + int encodedOffset = 0; // Offset into original (encoded) buffer + int decodedOffset = 0; // Offset into destination (decoded) buffer + byte replaceValue = 0; // Value that will be inserted to indicate replaced value - if (((encodedOffset + replaceValue) > length) && (replaceValue != 1)) - return 0; + while (encodedOffset < length) + { + replaceValue = encoded[encodedOffset]; // Grab next byte - encodedOffset++; // Point to next source + if (((encodedOffset + replaceValue) > length) && (replaceValue != 1)) + return 0; - // Copy all unchanged bytes - // C# would Array.Copy be noticably better? - for (int i = 1; i < replaceValue; i++) - decoded[decodedOffset++] = encoded[encodedOffset++]; + encodedOffset++; // Point to next source - // Sometimes don't need a trailing delimiter added - if (replaceValue < 0xff && encodedOffset != length) - decoded[decodedOffset++] = 0x00; - } + // Copy all unchanged bytes + // C# would Array.Copy be noticably better? + for (int i = 1; i < replaceValue; i++) + decoded[decodedOffset++] = encoded[encodedOffset++]; - return decodedOffset; + // Sometimes don't need a trailing delimiter added + if (replaceValue < 0xff && encodedOffset != length) + decoded[decodedOffset++] = 0x00; } + + return decodedOffset; } } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/ConnectionManager.cs b/Source/v2/Meadow.Hcom/ConnectionManager.cs index a9dc569b..5ce7bcee 100644 --- a/Source/v2/Meadow.Hcom/ConnectionManager.cs +++ b/Source/v2/Meadow.Hcom/ConnectionManager.cs @@ -1,29 +1,28 @@ -namespace Meadow.Hcom +namespace Meadow.Hcom; + +public class ConnectionManager { - public class ConnectionManager + private static readonly List _connections = new List(); + + public static TConnection GetConnection(string connectionName) + where TConnection : class, IMeadowConnection { - private static readonly List _connections = new List(); + // see if it already is known + var existing = _connections.FirstOrDefault(c => c.Name == connectionName) as TConnection; + if (existing != null) return existing; - public static TConnection GetConnection(string connectionName) - where TConnection : class, IMeadowConnection + // otherwise create + switch (typeof(TConnection)) { - // see if it already is known - var existing = _connections.FirstOrDefault(c => c.Name == connectionName) as TConnection; - if (existing != null) return existing; - - // otherwise create - switch (typeof(TConnection)) - { - case Type t when t == typeof(SerialConnection): - var c = new SerialConnection(connectionName); - _connections.Add(c); + case Type t when t == typeof(SerialConnection): + var c = new SerialConnection(connectionName); + _connections.Add(c); #pragma warning disable 8603 - return c as TConnection; + return c as TConnection; #pragma warning restore - default: - throw new NotSupportedException(); - }; + default: + throw new NotSupportedException(); + }; - } } } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/ConnectionState.cs b/Source/v2/Meadow.Hcom/ConnectionState.cs index b0c3e15b..30434bd1 100644 --- a/Source/v2/Meadow.Hcom/ConnectionState.cs +++ b/Source/v2/Meadow.Hcom/ConnectionState.cs @@ -1,9 +1,8 @@ -namespace Meadow.Hcom +namespace Meadow.Hcom; + +public enum ConnectionState { - public enum ConnectionState - { - Disconnected, - Connected, - MeadowAttached - } + Disconnected, + Connected, + MeadowAttached } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/Connections/ConnectionBase.cs b/Source/v2/Meadow.Hcom/Connections/ConnectionBase.cs index 7149f0ee..f21b1717 100755 --- a/Source/v2/Meadow.Hcom/Connections/ConnectionBase.cs +++ b/Source/v2/Meadow.Hcom/Connections/ConnectionBase.cs @@ -1,6 +1,4 @@ -using Microsoft.Extensions.Logging; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; public abstract class ConnectionBase : IMeadowConnection, IDisposable { diff --git a/Source/v2/Meadow.Hcom/Connections/LocalConnection.cs b/Source/v2/Meadow.Hcom/Connections/LocalConnection.cs index 76ce68ce..7332976f 100755 --- a/Source/v2/Meadow.Hcom/Connections/LocalConnection.cs +++ b/Source/v2/Meadow.Hcom/Connections/LocalConnection.cs @@ -1,6 +1,4 @@ -using Microsoft.Extensions.Logging; -using System.Diagnostics; -using System.Runtime.InteropServices; +using System.Runtime.InteropServices; namespace Meadow.Hcom; diff --git a/Source/v2/Meadow.Hcom/Connections/SerialConnection.ListenerProc.cs b/Source/v2/Meadow.Hcom/Connections/SerialConnection.ListenerProc.cs index 6c6e02fc..0750e764 100644 --- a/Source/v2/Meadow.Hcom/Connections/SerialConnection.ListenerProc.cs +++ b/Source/v2/Meadow.Hcom/Connections/SerialConnection.ListenerProc.cs @@ -1,5 +1,4 @@ -using System.Diagnostics; -using System.Runtime.InteropServices; +using System.Runtime.InteropServices; namespace Meadow.Hcom { diff --git a/Source/v2/Meadow.Hcom/Connections/SerialConnection.cs b/Source/v2/Meadow.Hcom/Connections/SerialConnection.cs index b58556c1..8d785113 100755 --- a/Source/v2/Meadow.Hcom/Connections/SerialConnection.cs +++ b/Source/v2/Meadow.Hcom/Connections/SerialConnection.cs @@ -1,10 +1,7 @@ -using Microsoft.Extensions.Logging; -using System.Buffers; -using System.Diagnostics; +using System.Buffers; using System.IO.Ports; using System.Net; using System.Security.Cryptography; -using System.Text; namespace Meadow.Hcom; diff --git a/Source/v2/Meadow.Hcom/Connections/SimulatorConnection.cs b/Source/v2/Meadow.Hcom/Connections/SimulatorConnection.cs index 0574b533..77fb9b2f 100755 --- a/Source/v2/Meadow.Hcom/Connections/SimulatorConnection.cs +++ b/Source/v2/Meadow.Hcom/Connections/SimulatorConnection.cs @@ -1,6 +1,4 @@ -using Microsoft.Extensions.Logging; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; public class SimulatorConnection : ConnectionBase { diff --git a/Source/v2/Meadow.Hcom/Connections/TcpConnection.cs b/Source/v2/Meadow.Hcom/Connections/TcpConnection.cs index 631dc4e0..e6964cb8 100755 --- a/Source/v2/Meadow.Hcom/Connections/TcpConnection.cs +++ b/Source/v2/Meadow.Hcom/Connections/TcpConnection.cs @@ -1,7 +1,4 @@ -using Microsoft.Extensions.Logging; -using System.Text.Json; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; public class TcpConnection : ConnectionBase { diff --git a/Source/v2/Meadow.Hcom/Debugging/DebuggingServer.cs b/Source/v2/Meadow.Hcom/Debugging/DebuggingServer.cs index 3de0a43c..a8f3549b 100644 --- a/Source/v2/Meadow.Hcom/Debugging/DebuggingServer.cs +++ b/Source/v2/Meadow.Hcom/Debugging/DebuggingServer.cs @@ -1,178 +1,176 @@ -using Microsoft.Extensions.Logging; -using System.Buffers; -using System.Diagnostics; -using System.Net; -using System.Net.Sockets; -using System.Security.Cryptography; - -namespace Meadow.Hcom; - -// This TCP server directly interacts with Visual Studio debugging. -// What it receives from Visual Studio it forwards to Meadow. -// What it receives from Meadow it forwards to Visual Studio. -public class DebuggingServer : IDisposable -{ - // VS 2019 - 4024 - // VS 2017 - 4022 - // VS 2015 - 4020 - public IPEndPoint LocalEndpoint { get; private set; } - - private readonly object _lck = new(); - private CancellationTokenSource? _cancellationTokenSource; - private readonly ILogger? _logger; - private readonly IMeadowDevice _meadow; - private ActiveClient? _activeClient; - private int _activeClientCount = 0; - private readonly TcpListener _listener; - private Task? _listenerTask; - private bool _isReady; - public bool Disposed; - - // Constructor - /// - /// Create a new DebuggingServer for proxying debug data between VS and Meadow - /// - /// The to debug - /// The to listen for incoming debugger connections - /// The to logging state information - public DebuggingServer(IMeadowDevice meadow, IPEndPoint localEndpoint, ILogger? logger) - { - LocalEndpoint = localEndpoint; - _meadow = meadow; - _logger = logger; - _listener = new TcpListener(LocalEndpoint); - } - - /// - /// Start the debugging server - /// - /// A that is linked internally to the running task - /// A representing the startup operation - public async Task StartListening(CancellationToken cancellationToken) - { - if (cancellationToken != null) - { - _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); - _listenerTask = Task.Factory.StartNew(StartListener, TaskCreationOptions.LongRunning); - var startTimeout = DateTime.UtcNow.AddSeconds(60); - while (DateTime.UtcNow < startTimeout) - { - if (_isReady) - { - return; - } - - await Task.Delay(100, cancellationToken); - } - - throw new Exception("DebuggingServer did not start listening within the 60 second timeout."); - } - } - - /// - /// Stop the - /// - /// A representing the shutdown operation - public async Task StopListening() - { - _listener?.Stop(); - +using System.Buffers; +using System.Net; +using System.Net.Sockets; +using System.Security.Cryptography; + +namespace Meadow.Hcom; + +// This TCP server directly interacts with Visual Studio debugging. +// What it receives from Visual Studio it forwards to Meadow. +// What it receives from Meadow it forwards to Visual Studio. +public class DebuggingServer : IDisposable +{ + // VS 2019 - 4024 + // VS 2017 - 4022 + // VS 2015 - 4020 + public IPEndPoint LocalEndpoint { get; private set; } + + private readonly object _lck = new(); + private CancellationTokenSource? _cancellationTokenSource; + private readonly ILogger? _logger; + private readonly IMeadowDevice _meadow; + private ActiveClient? _activeClient; + private int _activeClientCount = 0; + private readonly TcpListener _listener; + private Task? _listenerTask; + private bool _isReady; + public bool Disposed; + + // Constructor + /// + /// Create a new DebuggingServer for proxying debug data between VS and Meadow + /// + /// The to debug + /// The to listen for incoming debugger connections + /// The to logging state information + public DebuggingServer(IMeadowDevice meadow, IPEndPoint localEndpoint, ILogger? logger) + { + LocalEndpoint = localEndpoint; + _meadow = meadow; + _logger = logger; + _listener = new TcpListener(LocalEndpoint); + } + + /// + /// Start the debugging server + /// + /// A that is linked internally to the running task + /// A representing the startup operation + public async Task StartListening(CancellationToken cancellationToken) + { + if (cancellationToken != null) + { + _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + _listenerTask = Task.Factory.StartNew(StartListener, TaskCreationOptions.LongRunning); + var startTimeout = DateTime.UtcNow.AddSeconds(60); + while (DateTime.UtcNow < startTimeout) + { + if (_isReady) + { + return; + } + + await Task.Delay(100, cancellationToken); + } + + throw new Exception("DebuggingServer did not start listening within the 60 second timeout."); + } + } + + /// + /// Stop the + /// + /// A representing the shutdown operation + public async Task StopListening() + { + _listener?.Stop(); + _cancellationTokenSource?.Cancel(false); - - if (_listenerTask != null) - { - await _listenerTask; - } - } - - private async Task StartListener() - { - try - { - _listener.Start(); - LocalEndpoint = (IPEndPoint)_listener.LocalEndpoint; - _logger?.LogInformation($"Listening for Visual Studio to connect on {LocalEndpoint.Address}:{LocalEndpoint.Port}" + Environment.NewLine); - _isReady = true; - - // This call will wait for the client to connect, before continuing. We shouldn't need a loop. - TcpClient tcpClient = await _listener.AcceptTcpClientAsync(); - OnConnect(tcpClient); - } - catch (SocketException soex) - { - _logger?.LogError("A Socket error occurred. The port may already be in use. Try rebooting to free up the port."); - _logger?.LogError($"Error:\n{soex.Message} \nStack Trace:\n{soex.StackTrace}"); - } - catch (Exception ex) - { - _logger?.LogError("An unhandled exception occurred while listening for debugging connections."); - _logger?.LogError($"Error:\n{ex.Message} \nStack Trace:\n{ex.StackTrace}"); - } - } - - private void OnConnect(TcpClient tcpClient) - { - try - { - lock (_lck) - { - _logger?.LogInformation("Visual Studio has Connected" + Environment.NewLine); - if (_activeClientCount > 0 && _activeClient?.Disposed == false) - { - _logger?.LogDebug("Closing active client"); - Debug.Assert(_activeClientCount == 1); - Debug.Assert(_activeClient != null); - CloseActiveClient(); - } - - _activeClient = new ActiveClient(_meadow, tcpClient, _logger, _cancellationTokenSource?.Token); - _activeClientCount++; - } - } - catch (Exception ex) - { - _logger?.LogError(ex, "An error occurred while connecting to Visual Studio"); - } - } - - internal void CloseActiveClient() - { - _activeClient?.Dispose(); - _activeClient = null; - _activeClientCount = 0; - } - - public void Dispose() - { - lock (_lck) - { + + if (_listenerTask != null) + { + await _listenerTask; + } + } + + private async Task StartListener() + { + try + { + _listener.Start(); + LocalEndpoint = (IPEndPoint)_listener.LocalEndpoint; + _logger?.LogInformation($"Listening for Visual Studio to connect on {LocalEndpoint.Address}:{LocalEndpoint.Port}" + Environment.NewLine); + _isReady = true; + + // This call will wait for the client to connect, before continuing. We shouldn't need a loop. + TcpClient tcpClient = await _listener.AcceptTcpClientAsync(); + OnConnect(tcpClient); + } + catch (SocketException soex) + { + _logger?.LogError("A Socket error occurred. The port may already be in use. Try rebooting to free up the port."); + _logger?.LogError($"Error:\n{soex.Message} \nStack Trace:\n{soex.StackTrace}"); + } + catch (Exception ex) + { + _logger?.LogError("An unhandled exception occurred while listening for debugging connections."); + _logger?.LogError($"Error:\n{ex.Message} \nStack Trace:\n{ex.StackTrace}"); + } + } + + private void OnConnect(TcpClient tcpClient) + { + try + { + lock (_lck) + { + _logger?.LogInformation("Visual Studio has Connected" + Environment.NewLine); + if (_activeClientCount > 0 && _activeClient?.Disposed == false) + { + _logger?.LogDebug("Closing active client"); + Debug.Assert(_activeClientCount == 1); + Debug.Assert(_activeClient != null); + CloseActiveClient(); + } + + _activeClient = new ActiveClient(_meadow, tcpClient, _logger, _cancellationTokenSource?.Token); + _activeClientCount++; + } + } + catch (Exception ex) + { + _logger?.LogError(ex, "An error occurred while connecting to Visual Studio"); + } + } + + internal void CloseActiveClient() + { + _activeClient?.Dispose(); + _activeClient = null; + _activeClientCount = 0; + } + + public void Dispose() + { + lock (_lck) + { if (Disposed) { return; - } - _cancellationTokenSource?.Cancel(false); - _activeClient?.Dispose(); - _listenerTask?.Dispose(); - Disposed = true; - } - } - - // Embedded class - private class ActiveClient : IDisposable - { - private readonly IMeadowDevice _meadow; - private readonly TcpClient _tcpClient; - private readonly NetworkStream _networkStream; - - private readonly CancellationTokenSource _cts; - private readonly Task _receiveVsDebugDataTask; - private readonly Task _receiveMeadowDebugDataTask; - private readonly ILogger? _logger; - public bool Disposed = false; - - // Constructor - internal ActiveClient(IMeadowDevice meadow, TcpClient tcpClient, ILogger? logger, CancellationToken? cancellationToken) - { + } + _cancellationTokenSource?.Cancel(false); + _activeClient?.Dispose(); + _listenerTask?.Dispose(); + Disposed = true; + } + } + + // Embedded class + private class ActiveClient : IDisposable + { + private readonly IMeadowDevice _meadow; + private readonly TcpClient _tcpClient; + private readonly NetworkStream _networkStream; + + private readonly CancellationTokenSource _cts; + private readonly Task _receiveVsDebugDataTask; + private readonly Task _receiveMeadowDebugDataTask; + private readonly ILogger? _logger; + public bool Disposed = false; + + // Constructor + internal ActiveClient(IMeadowDevice meadow, TcpClient tcpClient, ILogger? logger, CancellationToken? cancellationToken) + { if (cancellationToken != null) { _cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken.Value); @@ -182,153 +180,153 @@ internal ActiveClient(IMeadowDevice meadow, TcpClient tcpClient, ILogger? logger _cts = new CancellationTokenSource(); } - _logger = logger; - _meadow = meadow; - _tcpClient = tcpClient; - _networkStream = tcpClient.GetStream(); - _logger?.LogDebug("Starting receive task"); - _receiveVsDebugDataTask = Task.Factory.StartNew(SendToMeadowAsync, TaskCreationOptions.LongRunning); - _receiveMeadowDebugDataTask = Task.Factory.StartNew(SendToVisualStudio, TaskCreationOptions.LongRunning); - } - - private const int RECEIVE_BUFFER_SIZE = 256; - - private async Task SendToMeadowAsync() - { - try - { - using var md5 = MD5.Create(); - // Receive from Visual Studio and send to Meadow - var receiveBuffer = ArrayPool.Shared.Rent(RECEIVE_BUFFER_SIZE); - var meadowBuffer = Array.Empty(); - - while (!_cts.IsCancellationRequested) - { - if (_networkStream != null && _networkStream.CanRead) - { - int bytesRead; - do - { - bytesRead = await _networkStream.ReadAsync(receiveBuffer, 0, receiveBuffer.Length, _cts.Token); - - if (bytesRead == 0 || _cts.IsCancellationRequested) - { + _logger = logger; + _meadow = meadow; + _tcpClient = tcpClient; + _networkStream = tcpClient.GetStream(); + _logger?.LogDebug("Starting receive task"); + _receiveVsDebugDataTask = Task.Factory.StartNew(SendToMeadowAsync, TaskCreationOptions.LongRunning); + _receiveMeadowDebugDataTask = Task.Factory.StartNew(SendToVisualStudio, TaskCreationOptions.LongRunning); + } + + private const int RECEIVE_BUFFER_SIZE = 256; + + private async Task SendToMeadowAsync() + { + try + { + using var md5 = MD5.Create(); + // Receive from Visual Studio and send to Meadow + var receiveBuffer = ArrayPool.Shared.Rent(RECEIVE_BUFFER_SIZE); + var meadowBuffer = Array.Empty(); + + while (!_cts.IsCancellationRequested) + { + if (_networkStream != null && _networkStream.CanRead) + { + int bytesRead; + do + { + bytesRead = await _networkStream.ReadAsync(receiveBuffer, 0, receiveBuffer.Length, _cts.Token); + + if (bytesRead == 0 || _cts.IsCancellationRequested) + { continue; - } - - var destIndex = meadowBuffer.Length; - Array.Resize(ref meadowBuffer, destIndex + bytesRead); - Array.Copy(receiveBuffer, 0, meadowBuffer, destIndex, bytesRead); - - // Forward the RECIEVE_BUFFER_SIZE chunk to Meadow immediately - _logger?.LogTrace("Received {count} bytes from VS, will forward to HCOM/Meadow. {hash}", - meadowBuffer.Length, - BitConverter.ToString(md5.ComputeHash(meadowBuffer)) - .Replace("-", string.Empty) - .ToLowerInvariant()); - - await _meadow.SendDebuggerData(meadowBuffer, 0, _cts.Token); - meadowBuffer = Array.Empty(); - - // Ensure we read all the data in this message before passing it along - // I'm not sure this is actually needed, the whole message should get read at once. - } while (_networkStream.DataAvailable); - } - else - { - // User probably hit stop - _logger?.LogInformation("Unable to Read Data from Visual Studio"); - _logger?.LogTrace("Unable to Read Data from Visual Studio"); - } - } - } - catch (IOException ioe) - { - // VS client probably died - _logger?.LogInformation("Visual Studio has Disconnected" + Environment.NewLine); - _logger?.LogTrace(ioe, "Visual Studio has Disconnected"); - } - catch (ObjectDisposedException ode) - { - // User probably hit stop - _logger?.LogInformation("Visual Studio has stopped debugging" + Environment.NewLine); - _logger?.LogTrace(ode, "Visual Studio has stopped debugging"); - } - catch (Exception ex) - { - _logger?.LogError($"Error receiving data from Visual Studio.{Environment.NewLine}Error: {ex.Message}{Environment.NewLine}StackTrace:{Environment.NewLine}{ex.StackTrace}"); - throw; - } - } - - private Task SendToVisualStudio() - { - try - { - while (!_cts.IsCancellationRequested) - { - if (_networkStream != null && _networkStream.CanWrite) - { - /* TODO while (_meadow.DataProcessor.DebuggerMessages.Count > 0) - { - var byteData = _meadow.DataProcessor.DebuggerMessages.Take(_cts.Token); - _logger?.LogTrace("Received {count} bytes from Meadow, will forward to VS", byteData.Length); - if (!_tcpClient.Connected) - { - _logger?.LogDebug("Cannot forward data, Visual Studio is not connected"); - return; - } - - await _networkStream.WriteAsync(byteData, 0, byteData.Length, _cts.Token); - _logger?.LogTrace("Forwarded {count} bytes to VS", byteData.Length); - }*/ - } - else - { - // User probably hit stop - _logger?.LogInformation("Unable to Write Data from Visual Studio"); - } - } - } - catch (OperationCanceledException oce) - { - // User probably hit stop; Removed logging as User doesn't need to see this - // Keeping it as a TODO in case we find a side effect that needs logging. - _logger?.LogInformation("Operation Cancelled"); - _logger?.LogTrace(oce, "Operation Cancelled"); - } - catch (Exception ex) - { - _logger?.LogError($"Error sending data to Visual Studio.{Environment.NewLine}Error: {ex.Message}{Environment.NewLine}StackTrace:{Environment.NewLine}{ex.StackTrace}"); - + } + + var destIndex = meadowBuffer.Length; + Array.Resize(ref meadowBuffer, destIndex + bytesRead); + Array.Copy(receiveBuffer, 0, meadowBuffer, destIndex, bytesRead); + + // Forward the RECIEVE_BUFFER_SIZE chunk to Meadow immediately + _logger?.LogTrace("Received {count} bytes from VS, will forward to HCOM/Meadow. {hash}", + meadowBuffer.Length, + BitConverter.ToString(md5.ComputeHash(meadowBuffer)) + .Replace("-", string.Empty) + .ToLowerInvariant()); + + await _meadow.SendDebuggerData(meadowBuffer, 0, _cts.Token); + meadowBuffer = Array.Empty(); + + // Ensure we read all the data in this message before passing it along + // I'm not sure this is actually needed, the whole message should get read at once. + } while (_networkStream.DataAvailable); + } + else + { + // User probably hit stop + _logger?.LogInformation("Unable to Read Data from Visual Studio"); + _logger?.LogTrace("Unable to Read Data from Visual Studio"); + } + } + } + catch (IOException ioe) + { + // VS client probably died + _logger?.LogInformation("Visual Studio has Disconnected" + Environment.NewLine); + _logger?.LogTrace(ioe, "Visual Studio has Disconnected"); + } + catch (ObjectDisposedException ode) + { + // User probably hit stop + _logger?.LogInformation("Visual Studio has stopped debugging" + Environment.NewLine); + _logger?.LogTrace(ode, "Visual Studio has stopped debugging"); + } + catch (Exception ex) + { + _logger?.LogError($"Error receiving data from Visual Studio.{Environment.NewLine}Error: {ex.Message}{Environment.NewLine}StackTrace:{Environment.NewLine}{ex.StackTrace}"); + throw; + } + } + + private Task SendToVisualStudio() + { + try + { + while (!_cts.IsCancellationRequested) + { + if (_networkStream != null && _networkStream.CanWrite) + { + /* TODO while (_meadow.DataProcessor.DebuggerMessages.Count > 0) + { + var byteData = _meadow.DataProcessor.DebuggerMessages.Take(_cts.Token); + _logger?.LogTrace("Received {count} bytes from Meadow, will forward to VS", byteData.Length); + if (!_tcpClient.Connected) + { + _logger?.LogDebug("Cannot forward data, Visual Studio is not connected"); + return; + } + + await _networkStream.WriteAsync(byteData, 0, byteData.Length, _cts.Token); + _logger?.LogTrace("Forwarded {count} bytes to VS", byteData.Length); + }*/ + } + else + { + // User probably hit stop + _logger?.LogInformation("Unable to Write Data from Visual Studio"); + } + } + } + catch (OperationCanceledException oce) + { + // User probably hit stop; Removed logging as User doesn't need to see this + // Keeping it as a TODO in case we find a side effect that needs logging. + _logger?.LogInformation("Operation Cancelled"); + _logger?.LogTrace(oce, "Operation Cancelled"); + } + catch (Exception ex) + { + _logger?.LogError($"Error sending data to Visual Studio.{Environment.NewLine}Error: {ex.Message}{Environment.NewLine}StackTrace:{Environment.NewLine}{ex.StackTrace}"); + if (_cts.IsCancellationRequested) { throw; - } - } - return Task.CompletedTask; - } - - public void Dispose() - { - lock (_tcpClient) - { + } + } + return Task.CompletedTask; + } + + public void Dispose() + { + lock (_tcpClient) + { if (Disposed) { return; - } - - _logger?.LogTrace("Disposing ActiveClient"); - _cts.Cancel(false); - _receiveVsDebugDataTask.Wait(TimeSpan.FromSeconds(10)); - _receiveMeadowDebugDataTask.Wait(TimeSpan.FromSeconds(10)); - _receiveVsDebugDataTask?.Dispose(); - _receiveMeadowDebugDataTask?.Dispose(); - _tcpClient.Dispose(); - _networkStream.Dispose(); - _cts.Dispose(); - Disposed = true; - } - } - } + } + + _logger?.LogTrace("Disposing ActiveClient"); + _cts.Cancel(false); + _receiveVsDebugDataTask.Wait(TimeSpan.FromSeconds(10)); + _receiveMeadowDebugDataTask.Wait(TimeSpan.FromSeconds(10)); + _receiveVsDebugDataTask?.Dispose(); + _receiveMeadowDebugDataTask?.Dispose(); + _tcpClient.Dispose(); + _networkStream.Dispose(); + _cts.Dispose(); + Disposed = true; + } + } + } } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/DeviceInfo.cs b/Source/v2/Meadow.Hcom/DeviceInfo.cs index 40e20e1e..1634da7a 100644 --- a/Source/v2/Meadow.Hcom/DeviceInfo.cs +++ b/Source/v2/Meadow.Hcom/DeviceInfo.cs @@ -1,100 +1,97 @@ -using System.Text; +namespace Meadow.Hcom; -namespace Meadow.Hcom +public class DeviceInfo { - public class DeviceInfo + public Dictionary Properties { get; } + + internal DeviceInfo(Dictionary properties) { - public Dictionary Properties { get; } + Properties = properties; + } - internal DeviceInfo(Dictionary properties) + public string? this[string propname] + { + get { - Properties = properties; + return Properties.Keys.Contains(propname) ? Properties[propname] : null; } + } - public string? this[string propname] - { - get - { - return Properties.Keys.Contains(propname) ? Properties[propname] : null; - } - } + public string? Product => this["Product"]; + public string? Model => this["Model"]; + public string? ProcessorType => this["ProcessorType"]; + public string? CoprocessorType => this["CoprocessorType"]; + public string? OsVersion => this["OSVersion"]; + public string? CoprocessorOsVersion => this["CoprocessorVersion"]; + public string? ProcessorId => this["ProcessorId"]; + public string? HardwareVersion => this["Hardware"]; + public string? DeviceName => this["DeviceName"]; + public string? RuntimeVersion => this["MonoVersion"]; + public string? SerialNumber => this["SerialNo"]; + public string? MacAddress => this["WiFiMAC"]; + public string? SoftAPMacAddress => this["SoftAPMac"]; - public string? Product => this["Product"]; - public string? Model => this["Model"]; - public string? ProcessorType => this["ProcessorType"]; - public string? CoprocessorType => this["CoprocessorType"]; - public string? OsVersion => this["OSVersion"]; - public string? CoprocessorOsVersion => this["CoprocessorVersion"]; - public string? ProcessorId => this["ProcessorId"]; - public string? HardwareVersion => this["Hardware"]; - public string? DeviceName => this["DeviceName"]; - public string? RuntimeVersion => this["MonoVersion"]; - public string? SerialNumber => this["SerialNo"]; - public string? MacAddress => this["WiFiMAC"]; - public string? SoftAPMacAddress => this["SoftAPMac"]; + /// + /// String representation of an unknown MAC address. + /// + private const string UNKNOWN_MAC_ADDRESS = "00:00:00:00:00:00"; - /// - /// String representation of an unknown MAC address. - /// - private const string UNKNOWN_MAC_ADDRESS = "00:00:00:00:00:00"; + public override string ToString() + { + var deviceInfo = new StringBuilder(); - public override string ToString() + if (Product != null && Product.Contains(" by Wilderness Labs")) { - var deviceInfo = new StringBuilder(); - - if (Product != null && Product.Contains(" by Wilderness Labs")) - { - deviceInfo.AppendLine(Product); - } - else - { - deviceInfo.AppendLine($"{Product} by Wilderness Labs"); - } + deviceInfo.AppendLine(Product); + } + else + { + deviceInfo.AppendLine($"{Product} by Wilderness Labs"); + } - deviceInfo.AppendLine("Board Information "); - deviceInfo.AppendLine($" Model: {Model}"); - deviceInfo.AppendLine($" Hardware version: {HardwareVersion}"); - deviceInfo.AppendLine($" Device name: {DeviceName}"); - deviceInfo.AppendLine(); - deviceInfo.AppendLine($"Hardware Information "); - deviceInfo.AppendLine($" Processor type: {ProcessorType}"); - deviceInfo.AppendLine($" ID: {ProcessorId}"); - deviceInfo.AppendLine($" Serial number: {SerialNumber}"); - deviceInfo.AppendLine($" Coprocessor type: {CoprocessorType}"); + deviceInfo.AppendLine("Board Information "); + deviceInfo.AppendLine($" Model: {Model}"); + deviceInfo.AppendLine($" Hardware version: {HardwareVersion}"); + deviceInfo.AppendLine($" Device name: {DeviceName}"); + deviceInfo.AppendLine(); + deviceInfo.AppendLine($"Hardware Information "); + deviceInfo.AppendLine($" Processor type: {ProcessorType}"); + deviceInfo.AppendLine($" ID: {ProcessorId}"); + deviceInfo.AppendLine($" Serial number: {SerialNumber}"); + deviceInfo.AppendLine($" Coprocessor type: {CoprocessorType}"); - string macAddresses = string.Empty; - int macCount = 0; - if (!string.IsNullOrEmpty(MacAddress) && MacAddress != UNKNOWN_MAC_ADDRESS) - { - macCount++; - macAddresses += $"\tWiFi: {MacAddress}{Environment.NewLine}"; - } - if (!string.IsNullOrEmpty(SoftAPMacAddress) && SoftAPMacAddress != UNKNOWN_MAC_ADDRESS) + string macAddresses = string.Empty; + int macCount = 0; + if (!string.IsNullOrEmpty(MacAddress) && MacAddress != UNKNOWN_MAC_ADDRESS) + { + macCount++; + macAddresses += $"\tWiFi: {MacAddress}{Environment.NewLine}"; + } + if (!string.IsNullOrEmpty(SoftAPMacAddress) && SoftAPMacAddress != UNKNOWN_MAC_ADDRESS) + { + macCount++; + macAddresses += $"\tAP: {SoftAPMacAddress}{Environment.NewLine}"; + } + if (macCount > 0) + { + if (macCount > 1) { - macCount++; - macAddresses += $"\tAP: {SoftAPMacAddress}{Environment.NewLine}"; + deviceInfo.AppendLine(" MAC Addresses - "); } - if (macCount > 0) + else { - if (macCount > 1) - { - deviceInfo.AppendLine(" MAC Addresses - "); - } - else - { - deviceInfo.AppendLine(" MAC Address - "); - } - deviceInfo.AppendLine($"{macAddresses}"); + deviceInfo.AppendLine(" MAC Address - "); } + deviceInfo.AppendLine($"{macAddresses}"); + } - deviceInfo.AppendLine(); - deviceInfo.AppendLine($"Firmware Versions "); - deviceInfo.AppendLine($" OS: {OsVersion}"); - deviceInfo.AppendLine($" Runtime: {RuntimeVersion}"); - deviceInfo.AppendLine($" Coprocessor: {CoprocessorOsVersion}"); - deviceInfo.AppendLine($" Protocol: {Protocol.HCOM_PROTOCOL_HCOM_VERSION_NUMBER}"); + deviceInfo.AppendLine(); + deviceInfo.AppendLine($"Firmware Versions "); + deviceInfo.AppendLine($" OS: {OsVersion}"); + deviceInfo.AppendLine($" Runtime: {RuntimeVersion}"); + deviceInfo.AppendLine($" Coprocessor: {CoprocessorOsVersion}"); + deviceInfo.AppendLine($" Protocol: {Protocol.HCOM_PROTOCOL_HCOM_VERSION_NUMBER}"); - return deviceInfo.ToString(); - } + return deviceInfo.ToString(); } } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/DeviceNotFoundException.cs b/Source/v2/Meadow.Hcom/DeviceNotFoundException.cs index 3a822bc0..eff36cb8 100644 --- a/Source/v2/Meadow.Hcom/DeviceNotFoundException.cs +++ b/Source/v2/Meadow.Hcom/DeviceNotFoundException.cs @@ -1,7 +1,6 @@ -namespace Meadow.Hcom +namespace Meadow.Hcom; + +public class DeviceNotFoundException : Exception { - public class DeviceNotFoundException : Exception - { - internal DeviceNotFoundException() : base() { } - } + internal DeviceNotFoundException() : base() { } } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/Firmware/DownloadFileStream.cs b/Source/v2/Meadow.Hcom/Firmware/DownloadFileStream.cs index 4c37a41a..ce60adb9 100644 --- a/Source/v2/Meadow.Hcom/Firmware/DownloadFileStream.cs +++ b/Source/v2/Meadow.Hcom/Firmware/DownloadFileStream.cs @@ -1,6 +1,4 @@ -using Microsoft.Extensions.Logging; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; public class DownloadFileStream : Stream, IDisposable { diff --git a/Source/v2/Meadow.Hcom/Firmware/ReleaseMetadata.cs b/Source/v2/Meadow.Hcom/Firmware/ReleaseMetadata.cs index 823f1e01..723c6d8a 100644 --- a/Source/v2/Meadow.Hcom/Firmware/ReleaseMetadata.cs +++ b/Source/v2/Meadow.Hcom/Firmware/ReleaseMetadata.cs @@ -1,6 +1,4 @@ -using System.Text.Json.Serialization; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; public class ReleaseMetadata { diff --git a/Source/v2/Meadow.Hcom/HttpResponses/DeviceInfoHttpResponse.cs b/Source/v2/Meadow.Hcom/HttpResponses/DeviceInfoHttpResponse.cs index ccdfe3bd..c0f8f224 100644 --- a/Source/v2/Meadow.Hcom/HttpResponses/DeviceInfoHttpResponse.cs +++ b/Source/v2/Meadow.Hcom/HttpResponses/DeviceInfoHttpResponse.cs @@ -1,6 +1,4 @@ -using System.Text.Json.Serialization; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal class DeviceInfoHttpResponse { diff --git a/Source/v2/Meadow.Hcom/IConnectionListener.cs b/Source/v2/Meadow.Hcom/IConnectionListener.cs index 09e5e94c..522ed7a2 100644 --- a/Source/v2/Meadow.Hcom/IConnectionListener.cs +++ b/Source/v2/Meadow.Hcom/IConnectionListener.cs @@ -1,14 +1,13 @@ -namespace Meadow.Hcom +namespace Meadow.Hcom; + +public interface IConnectionListener { - public interface IConnectionListener - { - void OnInformationMessageReceived(string message); - void OnStdOutReceived(string message); - void OnStdErrReceived(string message); - void OnDeviceInformationMessageReceived(Dictionary deviceInfo); - void OnTextListReceived(string[] list); - void OnErrorTextReceived(string message); - void OnFileError(); - void OnTextMessageConcluded(int requestType); - } + void OnInformationMessageReceived(string message); + void OnStdOutReceived(string message); + void OnStdErrReceived(string message); + void OnDeviceInformationMessageReceived(Dictionary deviceInfo); + void OnTextListReceived(string[] list); + void OnErrorTextReceived(string message); + void OnFileError(); + void OnTextMessageConcluded(int requestType); } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/IMeadowConnection.cs b/Source/v2/Meadow.Hcom/IMeadowConnection.cs index 5fd0300b..20037b24 100755 --- a/Source/v2/Meadow.Hcom/IMeadowConnection.cs +++ b/Source/v2/Meadow.Hcom/IMeadowConnection.cs @@ -1,54 +1,51 @@ -using Microsoft.Extensions.Logging; +namespace Meadow.Hcom; -namespace Meadow.Hcom +public interface IMeadowConnection : IDisposable { - public interface IMeadowConnection : IDisposable - { - event EventHandler<(string message, string? source)> DeviceMessageReceived; - event EventHandler ConnectionError; - event EventHandler ConnectionMessage; - event EventHandler<(string fileName, long completed, long total)> FileWriteProgress; - event EventHandler FileWriteFailed; - - string Name { get; } - IMeadowDevice? Device { get; } - Task Attach(CancellationToken? cancellationToken = null, int timeoutSeconds = 10); - void Detach(); - Task WaitForMeadowAttach(CancellationToken? cancellationToken = null); - ConnectionState State { get; } - - Task WriteFile(string localFileName, string? meadowFileName = null, CancellationToken? cancellationToken = null); - Task ReadFile(string meadowFileName, string? localFileName = null, CancellationToken? cancellationToken = null); - Task ReadFileString(string fileName, CancellationToken? cancellationToken = null); - Task DeleteFile(string meadowFileName, CancellationToken? cancellationToken = null); - Task GetDeviceInfo(CancellationToken? cancellationToken = null); - Task GetFileList(string folder, bool includeCrcs, CancellationToken? cancellationToken = null); - Task ResetDevice(CancellationToken? cancellationToken = null); - Task IsRuntimeEnabled(CancellationToken? cancellationToken = null); - Task RuntimeDisable(CancellationToken? cancellationToken = null); - Task RuntimeEnable(CancellationToken? cancellationToken = null); - Task GetRtcTime(CancellationToken? cancellationToken = null); - Task SetRtcTime(DateTimeOffset dateTime, CancellationToken? cancellationToken = null); - - Task WriteRuntime(string localFileName, CancellationToken? cancellationToken = null); - Task WriteCoprocessorFile(string localFileName, int destinationAddress, CancellationToken? cancellationToken = null); - - Task TraceEnable(CancellationToken? cancellationToken = null); - Task TraceDisable(CancellationToken? cancellationToken = null); - Task SetTraceLevel(int level, CancellationToken? cancellationToken = null); - - Task SetDeveloperParameter(ushort parameter, uint value, CancellationToken? cancellationToken = null); - - Task UartTraceEnable(CancellationToken? cancellationToken = null); - Task UartTraceDisable(CancellationToken? cancellationToken = null); - Task UartProfilerEnable(CancellationToken? cancellationToken = null); - Task UartProfilerDisable(CancellationToken? cancellationToken = null); - - - Task EraseFlash(CancellationToken? cancellationToken = null); - Task GetPublicKey(CancellationToken? cancellationToken = null); - Task StartDebuggingSession(int port, ILogger? logger, CancellationToken cancellationToken); - Task StartDebugging(int port, ILogger? logger, CancellationToken? cancellationToken); - Task SendDebuggerData(byte[] debuggerData, uint userData, CancellationToken? cancellationToken); - } + event EventHandler<(string message, string? source)> DeviceMessageReceived; + event EventHandler ConnectionError; + event EventHandler ConnectionMessage; + event EventHandler<(string fileName, long completed, long total)> FileWriteProgress; + event EventHandler FileWriteFailed; + + string Name { get; } + IMeadowDevice? Device { get; } + Task Attach(CancellationToken? cancellationToken = null, int timeoutSeconds = 10); + void Detach(); + Task WaitForMeadowAttach(CancellationToken? cancellationToken = null); + ConnectionState State { get; } + + Task WriteFile(string localFileName, string? meadowFileName = null, CancellationToken? cancellationToken = null); + Task ReadFile(string meadowFileName, string? localFileName = null, CancellationToken? cancellationToken = null); + Task ReadFileString(string fileName, CancellationToken? cancellationToken = null); + Task DeleteFile(string meadowFileName, CancellationToken? cancellationToken = null); + Task GetDeviceInfo(CancellationToken? cancellationToken = null); + Task GetFileList(string folder, bool includeCrcs, CancellationToken? cancellationToken = null); + Task ResetDevice(CancellationToken? cancellationToken = null); + Task IsRuntimeEnabled(CancellationToken? cancellationToken = null); + Task RuntimeDisable(CancellationToken? cancellationToken = null); + Task RuntimeEnable(CancellationToken? cancellationToken = null); + Task GetRtcTime(CancellationToken? cancellationToken = null); + Task SetRtcTime(DateTimeOffset dateTime, CancellationToken? cancellationToken = null); + + Task WriteRuntime(string localFileName, CancellationToken? cancellationToken = null); + Task WriteCoprocessorFile(string localFileName, int destinationAddress, CancellationToken? cancellationToken = null); + + Task TraceEnable(CancellationToken? cancellationToken = null); + Task TraceDisable(CancellationToken? cancellationToken = null); + Task SetTraceLevel(int level, CancellationToken? cancellationToken = null); + + Task SetDeveloperParameter(ushort parameter, uint value, CancellationToken? cancellationToken = null); + + Task UartTraceEnable(CancellationToken? cancellationToken = null); + Task UartTraceDisable(CancellationToken? cancellationToken = null); + Task UartProfilerEnable(CancellationToken? cancellationToken = null); + Task UartProfilerDisable(CancellationToken? cancellationToken = null); + + + Task EraseFlash(CancellationToken? cancellationToken = null); + Task GetPublicKey(CancellationToken? cancellationToken = null); + Task StartDebuggingSession(int port, ILogger? logger, CancellationToken cancellationToken); + Task StartDebugging(int port, ILogger? logger, CancellationToken? cancellationToken); + Task SendDebuggerData(byte[] debuggerData, uint userData, CancellationToken? cancellationToken); } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/IMeadowDevice.cs b/Source/v2/Meadow.Hcom/IMeadowDevice.cs index 39d17a9e..25eed90a 100755 --- a/Source/v2/Meadow.Hcom/IMeadowDevice.cs +++ b/Source/v2/Meadow.Hcom/IMeadowDevice.cs @@ -1,34 +1,31 @@ -using Microsoft.Extensions.Logging; +namespace Meadow.Hcom; -namespace Meadow.Hcom +public interface IMeadowDevice { - public interface IMeadowDevice - { - Task Reset(CancellationToken? cancellationToken = null); - Task RuntimeDisable(CancellationToken? cancellationToken = null); - Task RuntimeEnable(CancellationToken? cancellationToken = null); - Task IsRuntimeEnabled(CancellationToken? cancellationToken = null); - Task GetDeviceInfo(CancellationToken? cancellationToken = null); - Task GetFileList(string folder, bool includeCrcs, CancellationToken? cancellationToken = null); - Task ReadFile(string meadowFileName, string? localFileName = null, CancellationToken? cancellationToken = null); - Task WriteFile(string localFileName, string? meadowFileName = null, CancellationToken? cancellationToken = null); - Task DeleteFile(string meadowFileName, CancellationToken? cancellationToken = null); - Task WriteRuntime(string localFileName, CancellationToken? cancellationToken = null); - Task GetRtcTime(CancellationToken? cancellationToken = null); - Task SetRtcTime(DateTimeOffset dateTime, CancellationToken? cancellationToken = null); - Task WriteCoprocessorFiles(string[] localFileNames, CancellationToken? cancellationToken = null); - Task TraceEnable(CancellationToken? cancellationToken = null); - Task TraceDisable(CancellationToken? cancellationToken = null); - Task SetTraceLevel(int level, CancellationToken? cancellationToken = null); - Task SetDeveloperParameter(ushort parameter, uint value, CancellationToken? cancellationToken = null); - Task UartTraceEnable(CancellationToken? cancellationToken = null); - Task UartTraceDisable(CancellationToken? cancellationToken = null); - Task UartProfilerEnable(CancellationToken? cancellationToken = null); - Task UartProfilerDisable(CancellationToken? cancellationToken = null); - Task EraseFlash(CancellationToken? cancellationToken = null); - Task ReadFileString(string fileName, CancellationToken? cancellationToken = null); - Task GetPublicKey(CancellationToken? cancellationToken = null); - Task StartDebugging(int port, ILogger? logger, CancellationToken? cancellationToken); - Task SendDebuggerData(byte[] debuggerData, uint userData, CancellationToken? cancellationToken); - } + Task Reset(CancellationToken? cancellationToken = null); + Task RuntimeDisable(CancellationToken? cancellationToken = null); + Task RuntimeEnable(CancellationToken? cancellationToken = null); + Task IsRuntimeEnabled(CancellationToken? cancellationToken = null); + Task GetDeviceInfo(CancellationToken? cancellationToken = null); + Task GetFileList(string folder, bool includeCrcs, CancellationToken? cancellationToken = null); + Task ReadFile(string meadowFileName, string? localFileName = null, CancellationToken? cancellationToken = null); + Task WriteFile(string localFileName, string? meadowFileName = null, CancellationToken? cancellationToken = null); + Task DeleteFile(string meadowFileName, CancellationToken? cancellationToken = null); + Task WriteRuntime(string localFileName, CancellationToken? cancellationToken = null); + Task GetRtcTime(CancellationToken? cancellationToken = null); + Task SetRtcTime(DateTimeOffset dateTime, CancellationToken? cancellationToken = null); + Task WriteCoprocessorFiles(string[] localFileNames, CancellationToken? cancellationToken = null); + Task TraceEnable(CancellationToken? cancellationToken = null); + Task TraceDisable(CancellationToken? cancellationToken = null); + Task SetTraceLevel(int level, CancellationToken? cancellationToken = null); + Task SetDeveloperParameter(ushort parameter, uint value, CancellationToken? cancellationToken = null); + Task UartTraceEnable(CancellationToken? cancellationToken = null); + Task UartTraceDisable(CancellationToken? cancellationToken = null); + Task UartProfilerEnable(CancellationToken? cancellationToken = null); + Task UartProfilerDisable(CancellationToken? cancellationToken = null); + Task EraseFlash(CancellationToken? cancellationToken = null); + Task ReadFileString(string fileName, CancellationToken? cancellationToken = null); + Task GetPublicKey(CancellationToken? cancellationToken = null); + Task StartDebugging(int port, ILogger? logger, CancellationToken? cancellationToken); + Task SendDebuggerData(byte[] debuggerData, uint userData, CancellationToken? cancellationToken); } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/Meadow.HCom.csproj b/Source/v2/Meadow.Hcom/Meadow.HCom.csproj index 999b91b0..afe28480 100644 --- a/Source/v2/Meadow.Hcom/Meadow.HCom.csproj +++ b/Source/v2/Meadow.Hcom/Meadow.HCom.csproj @@ -2,7 +2,6 @@ netstandard2.0 - enable enable 10 @@ -18,5 +17,4 @@ - diff --git a/Source/v2/Meadow.Hcom/MeadowDevice.cs b/Source/v2/Meadow.Hcom/MeadowDevice.cs index 2415a598..bb51e176 100755 --- a/Source/v2/Meadow.Hcom/MeadowDevice.cs +++ b/Source/v2/Meadow.Hcom/MeadowDevice.cs @@ -1,175 +1,172 @@ -using Microsoft.Extensions.Logging; +namespace Meadow.Hcom; -namespace Meadow.Hcom +public partial class MeadowDevice : IMeadowDevice { - public partial class MeadowDevice : IMeadowDevice + private readonly IMeadowConnection _connection; + + internal MeadowDevice(IMeadowConnection connection) { - private readonly IMeadowConnection _connection; + _connection = connection; + } - internal MeadowDevice(IMeadowConnection connection) - { - _connection = connection; - } + public async Task IsRuntimeEnabled(CancellationToken? cancellationToken = null) + { + return await _connection.IsRuntimeEnabled(cancellationToken); + } - public async Task IsRuntimeEnabled(CancellationToken? cancellationToken = null) - { - return await _connection.IsRuntimeEnabled(cancellationToken); - } + public async Task Reset(CancellationToken? cancellationToken = null) + { + await _connection.ResetDevice(cancellationToken); + } - public async Task Reset(CancellationToken? cancellationToken = null) - { - await _connection.ResetDevice(cancellationToken); - } + public async Task RuntimeDisable(CancellationToken? cancellationToken = null) + { + await _connection.RuntimeDisable(cancellationToken); + } - public async Task RuntimeDisable(CancellationToken? cancellationToken = null) - { - await _connection.RuntimeDisable(cancellationToken); - } + public async Task RuntimeEnable(CancellationToken? cancellationToken = null) + { + await _connection.RuntimeEnable(cancellationToken); + } - public async Task RuntimeEnable(CancellationToken? cancellationToken = null) - { - await _connection.RuntimeEnable(cancellationToken); - } + public async Task GetDeviceInfo(CancellationToken? cancellationToken = null) + { + return await _connection.GetDeviceInfo(cancellationToken); + } - public async Task GetDeviceInfo(CancellationToken? cancellationToken = null) - { - return await _connection.GetDeviceInfo(cancellationToken); - } + public async Task GetFileList(string folder, bool includeCrcs, CancellationToken? cancellationToken = null) + { + return await _connection.GetFileList(folder, includeCrcs, cancellationToken); + } - public async Task GetFileList(string folder, bool includeCrcs, CancellationToken? cancellationToken = null) - { - return await _connection.GetFileList(folder, includeCrcs, cancellationToken); - } + public async Task ReadFile(string meadowFileName, string? localFileName = null, CancellationToken? cancellationToken = null) + { + return await _connection.ReadFile(meadowFileName, localFileName, cancellationToken); + } - public async Task ReadFile(string meadowFileName, string? localFileName = null, CancellationToken? cancellationToken = null) - { - return await _connection.ReadFile(meadowFileName, localFileName, cancellationToken); - } + public async Task WriteFile(string localFileName, string? meadowFileName = null, CancellationToken? cancellationToken = null) + { + return await _connection.WriteFile(localFileName, meadowFileName, cancellationToken); + } - public async Task WriteFile(string localFileName, string? meadowFileName = null, CancellationToken? cancellationToken = null) - { - return await _connection.WriteFile(localFileName, meadowFileName, cancellationToken); - } + public async Task WriteRuntime(string localFileName, CancellationToken? cancellationToken = null) + { + return await _connection.WriteRuntime(localFileName, cancellationToken); + } - public async Task WriteRuntime(string localFileName, CancellationToken? cancellationToken = null) + public async Task WriteCoprocessorFiles(string[] localFileNames, CancellationToken? cancellationToken = null) + { + foreach (var file in localFileNames) { - return await _connection.WriteRuntime(localFileName, cancellationToken); - } + var result = await _connection.WriteCoprocessorFile( + file, + GetFileTargetAddress(file), + cancellationToken); - public async Task WriteCoprocessorFiles(string[] localFileNames, CancellationToken? cancellationToken = null) - { - foreach (var file in localFileNames) + if (!result) { - var result = await _connection.WriteCoprocessorFile( - file, - GetFileTargetAddress(file), - cancellationToken); - - if (!result) - { - return false; - } + return false; } - - return true; } - public async Task UartTraceEnable(CancellationToken? cancellationToken = null) - { - await _connection.UartTraceEnable(cancellationToken); - } + return true; + } - public async Task UartTraceDisable(CancellationToken? cancellationToken = null) - { - await _connection.UartTraceDisable(cancellationToken); - } + public async Task UartTraceEnable(CancellationToken? cancellationToken = null) + { + await _connection.UartTraceEnable(cancellationToken); + } - public async Task UartProfilerEnable(CancellationToken? cancellationToken = null) - { - await _connection.UartProfilerEnable(cancellationToken); - } + public async Task UartTraceDisable(CancellationToken? cancellationToken = null) + { + await _connection.UartTraceDisable(cancellationToken); + } - public async Task UartProfilerDisable(CancellationToken? cancellationToken = null) - { - await _connection.UartProfilerDisable(cancellationToken); - } + public async Task UartProfilerEnable(CancellationToken? cancellationToken = null) + { + await _connection.UartProfilerEnable(cancellationToken); + } - private int GetFileTargetAddress(string fileName) - { - // TODO: determine device type so we can map the file names to target locations - // for now we only support the F7 so these are static and well-known + public async Task UartProfilerDisable(CancellationToken? cancellationToken = null) + { + await _connection.UartProfilerDisable(cancellationToken); + } - var fn = Path.GetFileName(fileName).ToLower(); - switch (fn) - { - case "meadowcomms.bin": - return 0x10000; - case "bootloader.bin": - return 0x1000; - case "partition-table.bin": - return 0x8000; - default: throw new NotSupportedException($"Unsupported coprocessor file: '{fn}'"); - } - } + private int GetFileTargetAddress(string fileName) + { + // TODO: determine device type so we can map the file names to target locations + // for now we only support the F7 so these are static and well-known - public async Task GetRtcTime(CancellationToken? cancellationToken = null) + var fn = Path.GetFileName(fileName).ToLower(); + switch (fn) { - return await _connection.GetRtcTime(cancellationToken); + case "meadowcomms.bin": + return 0x10000; + case "bootloader.bin": + return 0x1000; + case "partition-table.bin": + return 0x8000; + default: throw new NotSupportedException($"Unsupported coprocessor file: '{fn}'"); } + } - public async Task SetRtcTime(DateTimeOffset dateTime, CancellationToken? cancellationToken = null) - { - await _connection.SetRtcTime(dateTime, cancellationToken); - } + public async Task GetRtcTime(CancellationToken? cancellationToken = null) + { + return await _connection.GetRtcTime(cancellationToken); + } - public async Task TraceEnable(CancellationToken? cancellationToken = null) - { - await _connection.TraceEnable(cancellationToken); - } + public async Task SetRtcTime(DateTimeOffset dateTime, CancellationToken? cancellationToken = null) + { + await _connection.SetRtcTime(dateTime, cancellationToken); + } - public async Task TraceDisable(CancellationToken? cancellationToken = null) - { - await _connection.TraceDisable(cancellationToken); - } + public async Task TraceEnable(CancellationToken? cancellationToken = null) + { + await _connection.TraceEnable(cancellationToken); + } - public async Task SetTraceLevel(int level, CancellationToken? cancellationToken = null) - { - await _connection.SetTraceLevel(level, cancellationToken); - } + public async Task TraceDisable(CancellationToken? cancellationToken = null) + { + await _connection.TraceDisable(cancellationToken); + } - public async Task SetDeveloperParameter(ushort parameter, uint value, CancellationToken? cancellationToken = null) - { - await _connection.SetDeveloperParameter(parameter, value, cancellationToken); - } + public async Task SetTraceLevel(int level, CancellationToken? cancellationToken = null) + { + await _connection.SetTraceLevel(level, cancellationToken); + } - public async Task DeleteFile(string meadowFileName, CancellationToken? cancellationToken = null) - { - await _connection.DeleteFile(meadowFileName, cancellationToken); - } + public async Task SetDeveloperParameter(ushort parameter, uint value, CancellationToken? cancellationToken = null) + { + await _connection.SetDeveloperParameter(parameter, value, cancellationToken); + } - public async Task EraseFlash(CancellationToken? cancellationToken = null) - { - await _connection.EraseFlash(cancellationToken); - } + public async Task DeleteFile(string meadowFileName, CancellationToken? cancellationToken = null) + { + await _connection.DeleteFile(meadowFileName, cancellationToken); + } - public async Task ReadFileString(string fileName, CancellationToken? cancellationToken = null) - { - return await _connection.ReadFileString(fileName, cancellationToken); - } + public async Task EraseFlash(CancellationToken? cancellationToken = null) + { + await _connection.EraseFlash(cancellationToken); + } - public async Task GetPublicKey(CancellationToken? cancellationToken = null) - { - return await _connection.GetPublicKey(cancellationToken); - } + public async Task ReadFileString(string fileName, CancellationToken? cancellationToken = null) + { + return await _connection.ReadFileString(fileName, cancellationToken); + } - public async Task StartDebugging(int port, ILogger? logger, CancellationToken? cancellationToken) - { - await _connection.StartDebugging(port, logger, cancellationToken); - } + public async Task GetPublicKey(CancellationToken? cancellationToken = null) + { + return await _connection.GetPublicKey(cancellationToken); + } - public async Task SendDebuggerData(byte[] debuggerData, uint userData, CancellationToken? cancellationToken) - { - await _connection.SendDebuggerData(debuggerData, userData, cancellationToken); - } + public async Task StartDebugging(int port, ILogger? logger, CancellationToken? cancellationToken) + { + await _connection.StartDebugging(port, logger, cancellationToken); + } + + public async Task SendDebuggerData(byte[] debuggerData, uint userData, CancellationToken? cancellationToken) + { + await _connection.SendDebuggerData(debuggerData, userData, cancellationToken); } } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/Protocol.cs b/Source/v2/Meadow.Hcom/Protocol.cs index 0e7feb0d..61cf2731 100644 --- a/Source/v2/Meadow.Hcom/Protocol.cs +++ b/Source/v2/Meadow.Hcom/Protocol.cs @@ -1,38 +1,37 @@ -namespace Meadow.Hcom +namespace Meadow.Hcom; + +internal class Protocol { - internal class Protocol - { - // There is no length field. Since the packet boundaries are delimited and the - // header is fixed length. Therefore, any additional data length is easily - // determined. - public const UInt16 HCOM_PROTOCOL_HCOM_VERSION_NUMBER = 0x0008; - - // COBS needs a specific delimiter. Zero seems to be traditional. - public const UInt16 HCOM_PROTOCOL_COBS_ENCODING_DELIMITER_VALUE = 0x00; - - // What sequence number is used to identify a non-data message? - public const UInt16 HCOM_PROTOCOL_NON_DATA_SEQUENCE_NUMBER = 0; - - // Note: while the MD5 hash is 128-bits (16-bytes), it is 32 character - // hex string from ESP32 - public const UInt16 HCOM_PROTOCOL_COMMAND_MD5_HASH_LENGTH = 32; - - // Define the absolute maximum packet sizes for sent and receive. - // Note: The length on the wire will be longer because it's encoded. - public const int HCOM_PROTOCOL_PACKET_MAX_SIZE = 8192; - public const int HCOM_PROTOCOL_ENCODED_MAX_SIZE = HCOM_PROTOCOL_PACKET_MAX_SIZE; - - // The maximum payload is max packet - header (12 bytes) - public const int HCOM_PROTOCOL_DATA_MAX_SIZE = HCOM_PROTOCOL_PACKET_MAX_SIZE - 12; - - //static public int HcomProtoHdrMessageSize() - //{ - // return Marshal.SizeOf(typeof(HcomProtoHdrMessage)); - //} - - //static public int HcomProtoFSInfoMsgSize() - //{ - // return Marshal.SizeOf(typeof(HcomProtoFSInfoMsg)); - //} - } + // There is no length field. Since the packet boundaries are delimited and the + // header is fixed length. Therefore, any additional data length is easily + // determined. + public const UInt16 HCOM_PROTOCOL_HCOM_VERSION_NUMBER = 0x0008; + + // COBS needs a specific delimiter. Zero seems to be traditional. + public const UInt16 HCOM_PROTOCOL_COBS_ENCODING_DELIMITER_VALUE = 0x00; + + // What sequence number is used to identify a non-data message? + public const UInt16 HCOM_PROTOCOL_NON_DATA_SEQUENCE_NUMBER = 0; + + // Note: while the MD5 hash is 128-bits (16-bytes), it is 32 character + // hex string from ESP32 + public const UInt16 HCOM_PROTOCOL_COMMAND_MD5_HASH_LENGTH = 32; + + // Define the absolute maximum packet sizes for sent and receive. + // Note: The length on the wire will be longer because it's encoded. + public const int HCOM_PROTOCOL_PACKET_MAX_SIZE = 8192; + public const int HCOM_PROTOCOL_ENCODED_MAX_SIZE = HCOM_PROTOCOL_PACKET_MAX_SIZE; + + // The maximum payload is max packet - header (12 bytes) + public const int HCOM_PROTOCOL_DATA_MAX_SIZE = HCOM_PROTOCOL_PACKET_MAX_SIZE - 12; + + //static public int HcomProtoHdrMessageSize() + //{ + // return Marshal.SizeOf(typeof(HcomProtoHdrMessage)); + //} + + //static public int HcomProtoFSInfoMsgSize() + //{ + // return Marshal.SizeOf(typeof(HcomProtoFSInfoMsg)); + //} } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/ProtocolType.cs b/Source/v2/Meadow.Hcom/ProtocolType.cs index 7addac69..a4245bff 100644 --- a/Source/v2/Meadow.Hcom/ProtocolType.cs +++ b/Source/v2/Meadow.Hcom/ProtocolType.cs @@ -1,31 +1,30 @@ -namespace Meadow.Hcom +namespace Meadow.Hcom; + +//-------------------------------------------------------------------------- +// HCOM Protocol message type definitions +//-------------------------------------------------------------------------- +public enum ProtocolType : UInt16 { - //-------------------------------------------------------------------------- - // HCOM Protocol message type definitions - //-------------------------------------------------------------------------- - public enum ProtocolType : UInt16 - { - // When the time comes the following Major types should reflect the - // name of the above structure is used to send it. - HCOM_PROTOCOL_HEADER_UNDEFINED_TYPE = 0x0000, + // When the time comes the following Major types should reflect the + // name of the above structure is used to send it. + HCOM_PROTOCOL_HEADER_UNDEFINED_TYPE = 0x0000, - // The header of all mesasges include a 4-byte field called user data. The - // User data field's meaning is determined by the message type + // The header of all mesasges include a 4-byte field called user data. The + // User data field's meaning is determined by the message type - // Header only request types, - HCOM_PROTOCOL_HEADER_ONLY_TYPE = 0x0100, + // Header only request types, + HCOM_PROTOCOL_HEADER_ONLY_TYPE = 0x0100, - // File related types includes 4-byte user data (used for the destination - // partition id), 4-byte file size, 4-byte checksum, 4-byte destination address - // and variable length destination file name. Note: The 4-byte destination address - // is currently only used for the STM32F7 to ESP32 downloads. - HCOM_PROTOCOL_HEADER_FILE_START_TYPE = 0x0200, + // File related types includes 4-byte user data (used for the destination + // partition id), 4-byte file size, 4-byte checksum, 4-byte destination address + // and variable length destination file name. Note: The 4-byte destination address + // is currently only used for the STM32F7 to ESP32 downloads. + HCOM_PROTOCOL_HEADER_FILE_START_TYPE = 0x0200, - // Simple text is a header followed by text without a terminating NULL. - HCOM_PROTOCOL_HEADER_SIMPLE_TEXT_TYPE = 0x0300, + // Simple text is a header followed by text without a terminating NULL. + HCOM_PROTOCOL_HEADER_SIMPLE_TEXT_TYPE = 0x0300, - // Simple binary is a header followed by binary data. The size of the data - // can be up to HCOM_PROTOCOL_PACKET_MAX_SIZE minus header size - HCOM_PROTOCOL_HEADER_SIMPLE_BINARY_TYPE = 0x0400, - } + // Simple binary is a header followed by binary data. The size of the data + // can be up to HCOM_PROTOCOL_PACKET_MAX_SIZE minus header size + HCOM_PROTOCOL_HEADER_SIMPLE_BINARY_TYPE = 0x0400, } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/SerialRequests/DebuggerDataRequest.cs b/Source/v2/Meadow.Hcom/SerialRequests/DebuggerDataRequest.cs index b41fc660..4a725113 100644 --- a/Source/v2/Meadow.Hcom/SerialRequests/DebuggerDataRequest.cs +++ b/Source/v2/Meadow.Hcom/SerialRequests/DebuggerDataRequest.cs @@ -1,20 +1,19 @@ -namespace Meadow.Hcom +namespace Meadow.Hcom; + +internal class DebuggerDataRequest : Request { - internal class DebuggerDataRequest : Request - { - public override RequestType RequestType => RequestType.HCOM_MDOW_REQUEST_DEBUGGING_DEBUGGER_DATA; + public override RequestType RequestType => RequestType.HCOM_MDOW_REQUEST_DEBUGGING_DEBUGGER_DATA; - public byte[] DebuggerData + public byte[] DebuggerData + { + get + { + if (Payload == null) return new byte[0]; + return Payload; + } + set { - get - { - if (Payload == null) return new byte[0]; - return Payload; - } - set - { - Payload = value; - } + Payload = value; } } } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/SerialRequests/FileDeleteRequest.cs b/Source/v2/Meadow.Hcom/SerialRequests/FileDeleteRequest.cs index 857d8e6d..ff98608f 100644 --- a/Source/v2/Meadow.Hcom/SerialRequests/FileDeleteRequest.cs +++ b/Source/v2/Meadow.Hcom/SerialRequests/FileDeleteRequest.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal class FileDeleteRequest : Request { diff --git a/Source/v2/Meadow.Hcom/SerialRequests/FileInitialBytesRequest.cs b/Source/v2/Meadow.Hcom/SerialRequests/FileInitialBytesRequest.cs index f9aeb82a..8745e3f7 100644 --- a/Source/v2/Meadow.Hcom/SerialRequests/FileInitialBytesRequest.cs +++ b/Source/v2/Meadow.Hcom/SerialRequests/FileInitialBytesRequest.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal class FileInitialBytesRequest : Request { diff --git a/Source/v2/Meadow.Hcom/SerialRequests/GetDeviceInfoRequest.cs b/Source/v2/Meadow.Hcom/SerialRequests/GetDeviceInfoRequest.cs index 7c01c5d7..fd750ef1 100644 --- a/Source/v2/Meadow.Hcom/SerialRequests/GetDeviceInfoRequest.cs +++ b/Source/v2/Meadow.Hcom/SerialRequests/GetDeviceInfoRequest.cs @@ -1,16 +1,15 @@ -namespace Meadow.Hcom +namespace Meadow.Hcom; + +internal class GetDeviceInfoRequest : Request { - internal class GetDeviceInfoRequest : Request - { - public override RequestType RequestType => RequestType.HCOM_MDOW_REQUEST_GET_DEVICE_INFORMATION; + public override RequestType RequestType => RequestType.HCOM_MDOW_REQUEST_GET_DEVICE_INFORMATION; - public GetDeviceInfoRequest() - { - } - // Serialized example: - // message - // 01-00-07-00-12-01-00-00-00-00-00-00" - // encoded - // 00-02-2A-02-06-03-12-01-01-01-01-01-01-01-00 + public GetDeviceInfoRequest() + { } + // Serialized example: + // message + // 01-00-07-00-12-01-00-00-00-00-00-00" + // encoded + // 00-02-2A-02-06-03-12-01-01-01-01-01-01-01-00 } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/SerialRequests/GetFileListRequest.cs b/Source/v2/Meadow.Hcom/SerialRequests/GetFileListRequest.cs index 7d8b4ee9..09f83b83 100644 --- a/Source/v2/Meadow.Hcom/SerialRequests/GetFileListRequest.cs +++ b/Source/v2/Meadow.Hcom/SerialRequests/GetFileListRequest.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom +namespace Meadow.Hcom { internal class GetFileListRequest : Request { diff --git a/Source/v2/Meadow.Hcom/SerialRequests/InitFileReadRequest.cs b/Source/v2/Meadow.Hcom/SerialRequests/InitFileReadRequest.cs index cdd9001d..666eae41 100644 --- a/Source/v2/Meadow.Hcom/SerialRequests/InitFileReadRequest.cs +++ b/Source/v2/Meadow.Hcom/SerialRequests/InitFileReadRequest.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal class InitFileReadRequest : Request { diff --git a/Source/v2/Meadow.Hcom/SerialRequests/InitFileWriteRequest.cs b/Source/v2/Meadow.Hcom/SerialRequests/InitFileWriteRequest.cs index ad6cb803..6c5480c9 100644 --- a/Source/v2/Meadow.Hcom/SerialRequests/InitFileWriteRequest.cs +++ b/Source/v2/Meadow.Hcom/SerialRequests/InitFileWriteRequest.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal static class NuttxCrc { diff --git a/Source/v2/Meadow.Hcom/SerialRequests/RequestBuilder.cs b/Source/v2/Meadow.Hcom/SerialRequests/RequestBuilder.cs index c20117a2..b3279eae 100644 --- a/Source/v2/Meadow.Hcom/SerialRequests/RequestBuilder.cs +++ b/Source/v2/Meadow.Hcom/SerialRequests/RequestBuilder.cs @@ -1,19 +1,18 @@ -namespace Meadow.Hcom +namespace Meadow.Hcom; + +public static class RequestBuilder { - public static class RequestBuilder - { - //private static uint _sequenceNumber; + //private static uint _sequenceNumber; - public static T Build(uint userData = 0, ushort extraData = 0, ushort protocol = Protocol.HCOM_PROTOCOL_HCOM_VERSION_NUMBER) - where T : Request, new() + public static T Build(uint userData = 0, ushort extraData = 0, ushort protocol = Protocol.HCOM_PROTOCOL_HCOM_VERSION_NUMBER) + where T : Request, new() + { + return new T { - return new T - { - SequenceNumber = 0, - ProtocolVersion = protocol, - UserData = userData, - ExtraData = extraData - }; - } + SequenceNumber = 0, + ProtocolVersion = protocol, + UserData = userData, + ExtraData = extraData + }; } } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/SerialRequests/ResetDeviceRequest.cs b/Source/v2/Meadow.Hcom/SerialRequests/ResetDeviceRequest.cs index b9529e52..ce9ff926 100644 --- a/Source/v2/Meadow.Hcom/SerialRequests/ResetDeviceRequest.cs +++ b/Source/v2/Meadow.Hcom/SerialRequests/ResetDeviceRequest.cs @@ -1,16 +1,15 @@ -namespace Meadow.Hcom +namespace Meadow.Hcom; + +internal class ResetDeviceRequest : Request { - internal class ResetDeviceRequest : Request - { - public override RequestType RequestType => RequestType.HCOM_MDOW_REQUEST_RESTART_PRIMARY_MCU; + public override RequestType RequestType => RequestType.HCOM_MDOW_REQUEST_RESTART_PRIMARY_MCU; - public ResetDeviceRequest() - { - } - // Serialized example: - // message - // 01-00-07-00-12-01-00-00-00-00-00-00" - // encoded - // 00-02-2A-02-06-03-12-01-01-01-01-01-01-01-00 + public ResetDeviceRequest() + { } + // Serialized example: + // message + // 01-00-07-00-12-01-00-00-00-00-00-00" + // encoded + // 00-02-2A-02-06-03-12-01-01-01-01-01-01-01-00 } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/SerialRequests/SetRtcTimeRequest.cs b/Source/v2/Meadow.Hcom/SerialRequests/SetRtcTimeRequest.cs index 41bb7400..9ddf2b4b 100644 --- a/Source/v2/Meadow.Hcom/SerialRequests/SetRtcTimeRequest.cs +++ b/Source/v2/Meadow.Hcom/SerialRequests/SetRtcTimeRequest.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal class SetRtcTimeRequest : Request { diff --git a/Source/v2/Meadow.Hcom/SerialRequests/StartDebuggingRequest.cs b/Source/v2/Meadow.Hcom/SerialRequests/StartDebuggingRequest.cs index 7bcdd5a5..e2a45dcf 100644 --- a/Source/v2/Meadow.Hcom/SerialRequests/StartDebuggingRequest.cs +++ b/Source/v2/Meadow.Hcom/SerialRequests/StartDebuggingRequest.cs @@ -1,7 +1,6 @@ -namespace Meadow.Hcom +namespace Meadow.Hcom; + +internal class StartDebuggingRequest : Request { - internal class StartDebuggingRequest : Request - { - public override RequestType RequestType => RequestType.HCOM_MDOW_REQUEST_MONO_START_DBG_SESSION; - } + public override RequestType RequestType => RequestType.HCOM_MDOW_REQUEST_MONO_START_DBG_SESSION; } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/SerialResponses/DeviceInfoSerialResponse.cs b/Source/v2/Meadow.Hcom/SerialResponses/DeviceInfoSerialResponse.cs index bd6bd967..72b127de 100644 --- a/Source/v2/Meadow.Hcom/SerialResponses/DeviceInfoSerialResponse.cs +++ b/Source/v2/Meadow.Hcom/SerialResponses/DeviceInfoSerialResponse.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal class DeviceInfoSerialResponse : SerialResponse { diff --git a/Source/v2/Meadow.Hcom/SerialResponses/FileDownloadFailedResponse.cs b/Source/v2/Meadow.Hcom/SerialResponses/FileDownloadFailedResponse.cs index 26e08104..4437e016 100644 --- a/Source/v2/Meadow.Hcom/SerialResponses/FileDownloadFailedResponse.cs +++ b/Source/v2/Meadow.Hcom/SerialResponses/FileDownloadFailedResponse.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal class FileDownloadFailedResponse : SerialResponse { diff --git a/Source/v2/Meadow.Hcom/SerialResponses/FileInitialBytesSerialResponse.cs b/Source/v2/Meadow.Hcom/SerialResponses/FileInitialBytesSerialResponse.cs index 40705237..cc3003e0 100644 --- a/Source/v2/Meadow.Hcom/SerialResponses/FileInitialBytesSerialResponse.cs +++ b/Source/v2/Meadow.Hcom/SerialResponses/FileInitialBytesSerialResponse.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal class TextPayloadSerialResponse : SerialResponse { diff --git a/Source/v2/Meadow.Hcom/SerialResponses/FileWriteInitFailedSerialResponse.cs b/Source/v2/Meadow.Hcom/SerialResponses/FileWriteInitFailedSerialResponse.cs index 2135f2ea..48b815d9 100644 --- a/Source/v2/Meadow.Hcom/SerialResponses/FileWriteInitFailedSerialResponse.cs +++ b/Source/v2/Meadow.Hcom/SerialResponses/FileWriteInitFailedSerialResponse.cs @@ -1,7 +1,4 @@ -using System.Diagnostics; -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal class FileWriteInitFailedSerialResponse : SerialResponse { diff --git a/Source/v2/Meadow.Hcom/SerialResponses/ReconnectRequiredResponse.cs b/Source/v2/Meadow.Hcom/SerialResponses/ReconnectRequiredResponse.cs index fb7cd09c..2fdf16c3 100644 --- a/Source/v2/Meadow.Hcom/SerialResponses/ReconnectRequiredResponse.cs +++ b/Source/v2/Meadow.Hcom/SerialResponses/ReconnectRequiredResponse.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal class ReconnectRequiredResponse : SerialResponse { diff --git a/Source/v2/Meadow.Hcom/SerialResponses/RequestErrorTextResponse.cs b/Source/v2/Meadow.Hcom/SerialResponses/RequestErrorTextResponse.cs index 4b152af2..2f6dc9d9 100644 --- a/Source/v2/Meadow.Hcom/SerialResponses/RequestErrorTextResponse.cs +++ b/Source/v2/Meadow.Hcom/SerialResponses/RequestErrorTextResponse.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal class RequestErrorTextResponse : SerialResponse { diff --git a/Source/v2/Meadow.Hcom/SerialResponses/TextAcceptedResponse.cs b/Source/v2/Meadow.Hcom/SerialResponses/TextAcceptedResponse.cs index 54e044c5..4a0e9798 100644 --- a/Source/v2/Meadow.Hcom/SerialResponses/TextAcceptedResponse.cs +++ b/Source/v2/Meadow.Hcom/SerialResponses/TextAcceptedResponse.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal class TextAcceptedResponse : SerialResponse { diff --git a/Source/v2/Meadow.Hcom/SerialResponses/TextCrcMemberResponse.cs b/Source/v2/Meadow.Hcom/SerialResponses/TextCrcMemberResponse.cs index ef3d64bb..64a7284b 100644 --- a/Source/v2/Meadow.Hcom/SerialResponses/TextCrcMemberResponse.cs +++ b/Source/v2/Meadow.Hcom/SerialResponses/TextCrcMemberResponse.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal class TextCrcMemberResponse : SerialResponse { diff --git a/Source/v2/Meadow.Hcom/SerialResponses/TextInformationResponse.cs b/Source/v2/Meadow.Hcom/SerialResponses/TextInformationResponse.cs index f18d80ed..93d89564 100644 --- a/Source/v2/Meadow.Hcom/SerialResponses/TextInformationResponse.cs +++ b/Source/v2/Meadow.Hcom/SerialResponses/TextInformationResponse.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; /// /// An unsolicited text response sent by HCOM (i.e. typically a Console.Write) diff --git a/Source/v2/Meadow.Hcom/SerialResponses/TextListMemberResponse.cs b/Source/v2/Meadow.Hcom/SerialResponses/TextListMemberResponse.cs index d6a89661..a44a7c5f 100644 --- a/Source/v2/Meadow.Hcom/SerialResponses/TextListMemberResponse.cs +++ b/Source/v2/Meadow.Hcom/SerialResponses/TextListMemberResponse.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal class TextListMemberResponse : SerialResponse { diff --git a/Source/v2/Meadow.Hcom/SerialResponses/TextRequestRejectedResponse.cs b/Source/v2/Meadow.Hcom/SerialResponses/TextRequestRejectedResponse.cs index 2f6e0023..0c5075e2 100644 --- a/Source/v2/Meadow.Hcom/SerialResponses/TextRequestRejectedResponse.cs +++ b/Source/v2/Meadow.Hcom/SerialResponses/TextRequestRejectedResponse.cs @@ -1,7 +1,4 @@ -using System.Diagnostics; -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal class TextRequestRejectedResponse : SerialResponse { diff --git a/Source/v2/Meadow.Hcom/SerialResponses/TextRequestResponse.cs b/Source/v2/Meadow.Hcom/SerialResponses/TextRequestResponse.cs index aee3a25c..6ed62390 100644 --- a/Source/v2/Meadow.Hcom/SerialResponses/TextRequestResponse.cs +++ b/Source/v2/Meadow.Hcom/SerialResponses/TextRequestResponse.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; /// /// A text response to a solicited host request diff --git a/Source/v2/Meadow.Hcom/SerialResponses/TextStdErrResponse.cs b/Source/v2/Meadow.Hcom/SerialResponses/TextStdErrResponse.cs index e19173bc..2b7677fa 100644 --- a/Source/v2/Meadow.Hcom/SerialResponses/TextStdErrResponse.cs +++ b/Source/v2/Meadow.Hcom/SerialResponses/TextStdErrResponse.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal class TextStdErrResponse : SerialResponse { diff --git a/Source/v2/Meadow.Hcom/SerialResponses/TextStdOutResponse.cs b/Source/v2/Meadow.Hcom/SerialResponses/TextStdOutResponse.cs index 2d40264f..4c2218ec 100644 --- a/Source/v2/Meadow.Hcom/SerialResponses/TextStdOutResponse.cs +++ b/Source/v2/Meadow.Hcom/SerialResponses/TextStdOutResponse.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Meadow.Hcom; +namespace Meadow.Hcom; internal class TextStdOutResponse : SerialResponse { diff --git a/Source/v2/Meadow.Hcom/Usings.cs b/Source/v2/Meadow.Hcom/Usings.cs new file mode 100644 index 00000000..ffaad68c --- /dev/null +++ b/Source/v2/Meadow.Hcom/Usings.cs @@ -0,0 +1,12 @@ +global using Microsoft.Extensions.Logging; +global using System; +global using System.Collections.Generic; +global using System.Diagnostics; +global using System.IO; +global using System.Linq; +global using System.Net.Http; +global using System.Text; +global using System.Text.Json; +global using System.Text.Json.Serialization; +global using System.Threading; +global using System.Threading.Tasks;