Skip to content

Commit

Permalink
Flash os improvements (WildernessLabs#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianstevens authored Jun 9, 2021
1 parent ba72b5e commit fd262a6
Show file tree
Hide file tree
Showing 140 changed files with 7,316 additions and 4,448 deletions.
14 changes: 11 additions & 3 deletions Meadow.CLI.Core/CloudServices/DeviceRepository.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
using Meadow.CLI.Core.Auth;
using System.Net.Http;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Meadow.CLI.Core.Identity;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Core.CloudServices
{
public class DeviceRepository
{
private readonly ILogger _logger;

public DeviceRepository(ILogger logger)
{
_logger = logger;
}

public async Task<(bool isSuccess, string message)> AddDevice(string serialNumber)
{
var host = SettingsManager.GetAppSetting("wlApiHost");
var authToken = await new IdentityManager().GetAccessToken();
var authToken = await new IdentityManager(_logger).GetAccessToken();
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);

Expand Down
25 changes: 11 additions & 14 deletions Meadow.CLI.Core/DeviceManagement/AssemblyManager.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using System;
using Mono.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using Mono.Cecil;
using System.Collections.Generic;
using Mono.Collections.Generic;

namespace MeadowCLI.DeviceManagement
namespace Meadow.CLI.Core.DeviceManagement
{
//https://github.com/xamarin/xamarin-macios/blob/main/tools/mtouch/Assembly.mtouch.cs#L54

public static class AssemblyManager
{
static List<string> dependencyMap = new List<string>();
static string folderPath;
static string fileName;
private static readonly List<string> dependencyMap = new List<string>();
private static string? folderPath;
private static string? fileName;

public static List<string> GetDependencies(string file, string path)
{
Expand All @@ -29,23 +28,23 @@ public static List<string> GetDependencies(string file, string path)
return dependencies;
}

static Collection<AssemblyNameReference> GetAssemblyNameReferences(string fileName, string path = null)
static Collection<AssemblyNameReference> GetAssemblyNameReferences(string fileName, string? path = null)
{
if (!string.IsNullOrWhiteSpace(path))
{
fileName = Path.Combine(path, fileName);
fileName = Path.Combine(path!, fileName);
}

if (Path.GetExtension(fileName) != ".exe" && Path.GetExtension(fileName) != ".dll")
if (Path.GetExtension(fileName) != ".exe" &&
Path.GetExtension(fileName) != ".dll")
{
fileName += ".dll";
}

Collection<AssemblyNameReference> references;

if (File.Exists(fileName) == false)
if(File.Exists(fileName) == false)
{
Console.WriteLine($"Could not find {fileName}");
return null;
}

Expand All @@ -65,9 +64,7 @@ static List<string> GetDependencies(Collection<AssemblyNameReference> references
var namedRefs = GetAssemblyNameReferences(ar.Name, folderPath);

if (namedRefs == null)
{
continue;
}

dependencyMap.Add(ar.Name);

Expand Down
37 changes: 0 additions & 37 deletions Meadow.CLI.Core/DeviceManagement/DeploymentManager.cs

This file was deleted.

11 changes: 11 additions & 0 deletions Meadow.CLI.Core/DeviceManagement/DeviceInfoException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Meadow.CLI.Core.DeviceManagement
{
// TODO: Inherit from MeadowDeviceException
public class DeviceInfoException : Exception
{
public DeviceInfoException(Exception? innerException = null) : base("An exception occurred while retrieving the device info", innerException)
{}
}
}
32 changes: 32 additions & 0 deletions Meadow.CLI.Core/DeviceManagement/FileTransferResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Meadow.CLI.Core.DeviceManagement
{
public class FileTransferResult
{
public FileTransferResult(long transferTime, long fileSize, uint checksum)
{
TransferTime = transferTime;
FileSize = fileSize;
Checksum = $"{checksum:x08}";
}
/// <summary>
/// Gets the number of milliseconds it took to transfer the file to the device
/// </summary>
public long TransferTime {get;}

/// <summary>
/// Get the number of bytes written
/// </summary>
public long FileSize {get;}

/// <summary>
/// Get the CRC Checksum of the file
/// </summary>
public string Checksum {get;}

public static FileTransferResult EmptyResult = new FileTransferResult(0, 0, 0);
}
}
35 changes: 35 additions & 0 deletions Meadow.CLI.Core/DeviceManagement/HcomHostRequestType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Meadow.CLI.Core.DeviceManagement
{
/// <summary>
/// Messages sent from meadow to host
/// </summary>
public enum HcomHostRequestType : ushort
{
HCOM_HOST_REQUEST_UNDEFINED_REQUEST = 0x00 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_UNDEFINED,

// Simple with some text message
HCOM_HOST_REQUEST_TEXT_REJECTED = 0x01 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_TEXT,
HCOM_HOST_REQUEST_TEXT_ACCEPTED = 0x02 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_TEXT,
HCOM_HOST_REQUEST_TEXT_CONCLUDED = 0x03 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_TEXT,
HCOM_HOST_REQUEST_TEXT_ERROR = 0x04 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_TEXT,
HCOM_HOST_REQUEST_TEXT_INFORMATION = 0x05 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_TEXT,
HCOM_HOST_REQUEST_TEXT_LIST_HEADER = 0x06 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_TEXT,
HCOM_HOST_REQUEST_TEXT_LIST_MEMBER = 0x07 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_TEXT,
HCOM_HOST_REQUEST_TEXT_CRC_MEMBER = 0x08 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_TEXT,
HCOM_HOST_REQUEST_TEXT_MONO_STDOUT = 0x09 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_TEXT,
HCOM_HOST_REQUEST_TEXT_DEVICE_INFO = 0x0A | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_TEXT,
HCOM_HOST_REQUEST_TEXT_TRACE_MSG = 0x0B | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_TEXT,
HCOM_HOST_REQUEST_TEXT_RECONNECT = 0x0C | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_TEXT,
HCOM_HOST_REQUEST_TEXT_MONO_STDERR = 0x0d | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_TEXT,
HCOM_HOST_REQUEST_FILE_START_OKAY = 0x0e | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_TEXT,
HCOM_HOST_REQUEST_FILE_START_FAIL = 0x0f | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_TEXT,

// Simple with debugger message from Meadow
HCOM_HOST_REQUEST_DEBUGGING_MONO_DATA = 0x01 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_BINARY,
HCOM_HOST_REQUEST_GET_INITIAL_FILE_BYTES = 0x02 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_BINARY,
}
}
60 changes: 60 additions & 0 deletions Meadow.CLI.Core/DeviceManagement/HcomMeadowRequestType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace Meadow.CLI.Core.DeviceManagement
{
/// <summary>
/// Messages to be sent to Meadow board from host
/// </summary>
public enum HcomMeadowRequestType : ushort
{
HCOM_MDOW_REQUEST_UNDEFINED_REQUEST = 0x00 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_UNDEFINED,

HCOM_MDOW_REQUEST_CREATE_ENTIRE_FLASH_FS = 0x01 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_CHANGE_TRACE_LEVEL = 0x02 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_FORMAT_FLASH_FILE_SYS = 0x03 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_END_FILE_TRANSFER = 0x04 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_RESET_PRIMARY_MCU = 0x05 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_VERIFY_ERASED_FLASH = 0x06 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_PARTITION_FLASH_FS = 0x07 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_MOUNT_FLASH_FS = 0x08 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_INITIALIZE_FLASH_FS = 0x09 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_BULK_FLASH_ERASE = 0x0a | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_ENTER_DFU_MODE = 0x0b | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_ENABLE_DISABLE_NSH = 0x0c | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_LIST_PARTITION_FILES = 0x0d | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_LIST_PART_FILES_AND_CRC = 0x0e | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_MONO_DISABLE = 0x0f | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_MONO_ENABLE = 0x10 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_MONO_RUN_STATE = 0x11 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_GET_DEVICE_INFORMATION = 0x12 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_PART_RENEW_FILE_SYS = 0x13 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_NO_TRACE_TO_HOST = 0x14 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_SEND_TRACE_TO_HOST = 0x15 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_END_ESP_FILE_TRANSFER = 0x16 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_READ_ESP_MAC_ADDRESS = 0x17 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_RESTART_ESP32 = 0x18 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_MONO_FLASH = 0x19 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_SEND_TRACE_TO_UART = 0x1a | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_NO_TRACE_TO_UART = 0x1b | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_MONO_UPDATE_RUNTIME = 0x1c | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_MONO_UPDATE_FILE_END = 0x1d | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_MONO_START_DBG_SESSION = 0x1e | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_GET_DEVICE_NAME = 0x1f | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_GET_INITIAL_FILE_BYTES = 0x20 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,

// Only used for testing
HCOM_MDOW_REQUEST_DEVELOPER_1 = 0xf0 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_DEVELOPER_2 = 0xf1 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_DEVELOPER_3 = 0xf2 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_DEVELOPER_4 = 0xf3 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,

HCOM_MDOW_REQUEST_S25FL_QSPI_INIT = 0xf4 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_S25FL_QSPI_WRITE = 0xf5 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,
HCOM_MDOW_REQUEST_S25FL_QSPI_READ = 0xf6 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,

HCOM_MDOW_REQUEST_START_FILE_TRANSFER = 0x01 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_FILE_START,
HCOM_MDOW_REQUEST_DELETE_FILE_BY_NAME = 0x02 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_FILE_START,
HCOM_MDOW_REQUEST_START_ESP_FILE_TRANSFER = 0x03 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_FILE_START,

// Simple debugger message to Meadow
HCOM_MDOW_REQUEST_DEBUGGING_DEBUGGER_DATA = 0x01 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_BINARY,
}
}
15 changes: 15 additions & 0 deletions Meadow.CLI.Core/DeviceManagement/HcomProtocolHeaderOffsets.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Meadow.CLI.Core.DeviceManagement
{
public enum HcomProtocolHeaderOffsets
{
HCOM_PROTOCOL_REQUEST_HEADER_SEQ_OFFSET = 0,
HCOM_PROTOCOL_REQUEST_HEADER_VERSION_OFFSET = 2,
HCOM_PROTOCOL_REQUEST_HEADER_RQST_TYPE_OFFSET = 4,
HCOM_PROTOCOL_REQUEST_HEADER_EXTRA_DATA_OFFSET = 6,
HCOM_PROTOCOL_REQUEST_HEADER_USER_DATA_OFFSET = 8,
}
}
22 changes: 22 additions & 0 deletions Meadow.CLI.Core/DeviceManagement/HcomProtocolHeaderTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Meadow.CLI.Core.DeviceManagement
{
public enum HcomProtocolHeaderTypes : UInt16
{
HCOM_PROTOCOL_HEADER_TYPE_UNDEFINED = 0x0000,
// Simple request types, include 4-byte user data
HCOM_PROTOCOL_HEADER_TYPE_SIMPLE = 0x0100,
// File releted request types, includes 4-byte user data (for the
// destination partition id), 4-byte file size, 4-byte checksum and
// variable length destination file name.
HCOM_PROTOCOL_HEADER_TYPE_FILE_START = 0x0200,
// Simple text.
HCOM_PROTOCOL_HEADER_TYPE_SIMPLE_TEXT = 0x0300,
// 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_TYPE_SIMPLE_BINARY = 0x0400,
}
}
25 changes: 25 additions & 0 deletions Meadow.CLI.Core/DeviceManagement/MeadowDataProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Meadow.CLI.Core.Internals.MeadowCommunication;

namespace Meadow.CLI.Core.DeviceManagement
{
public class MeadowDataProcessor
{
public EventHandler<MeadowMessageEventArgs>? OnReceiveData;
public Func<byte[]?, CancellationToken, Task>? ForwardDebuggingData;
}

public class MeadowMessageEventArgs : EventArgs
{
public string Message { get; private set; }
public MeadowMessageType MessageType { get; private set; }

public MeadowMessageEventArgs(MeadowMessageType messageType, string message = "")
{
Message = message;
MessageType = messageType;
}
}
}
Loading

0 comments on commit fd262a6

Please sign in to comment.