Skip to content

Commit

Permalink
Merge pull request #574 from WildernessLabs/nsh_enable_disable
Browse files Browse the repository at this point in the history
Nsh enable disable commands
  • Loading branch information
adrianstevens authored Jun 6, 2024
2 parents ba44c8d + c740663 commit ecb899c
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 12 deletions.
25 changes: 25 additions & 0 deletions Source/v2/Meadow.CLI/Commands/Current/Nsh/NshDisableCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using CliFx.Attributes;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;

[Command("nsh disable", Description = "Disables the Nuttx shell on the Meadow device")]
public class NshDisableCommand : BaseDeviceCommand<RuntimeEnableCommand>
{
public NshDisableCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory)
: base(connectionManager, loggerFactory)
{ }

protected override async ValueTask ExecuteCommand()
{
var device = await GetCurrentDevice();

Logger?.LogInformation("Disabling NSH...");

var state = await device.IsRuntimeEnabled(CancellationToken);

await device.NshDisable(CancellationToken);

Logger?.LogInformation("NSH disabled");
}
}
25 changes: 25 additions & 0 deletions Source/v2/Meadow.CLI/Commands/Current/Nsh/NshEnableCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using CliFx.Attributes;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;

[Command("nsh enable", Description = "Enables the Nuttx shell on the Meadow device")]
public class NshEnableCommand : BaseDeviceCommand<RuntimeEnableCommand>
{
public NshEnableCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory)
: base(connectionManager, loggerFactory)
{ }

protected override async ValueTask ExecuteCommand()
{
var device = await GetCurrentDevice();

Logger?.LogInformation("Enabling NSH...");

var state = await device.IsRuntimeEnabled(CancellationToken);

await device.NshEnable(CancellationToken);

Logger?.LogInformation("NSH enabled");
}
}
10 changes: 5 additions & 5 deletions Source/v2/Meadow.Cli/Commands/Current/App/AppRunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class AppRunCommand : BaseDeviceCommand<AppRunCommand>
{
private readonly IPackageManager _packageManager;

[CommandOption("no-prefix", 'n', Description = "When set, the message source prefix (e.g. 'stdout>') is suppressed during 'listen'", IsRequired = false)]
public bool NoPrefix { get; init; }
[CommandOption("prefix", 'p', Description = "When set, the message source prefix (e.g. 'stdout>') is shown during 'listen'", IsRequired = false)]
public bool Prefix { get; init; } = false;

[CommandOption('c', Description = Strings.BuildConfiguration, IsRequired = false)]
public string? Configuration { get; private set; }
Expand Down Expand Up @@ -136,13 +136,13 @@ private void OnFileWriteProgress(object? sender, (string fileName, long complete

private void OnDeviceMessageReceived(object? sender, (string message, string? source) e)
{
if (NoPrefix)
if (Prefix)
{
Logger?.LogInformation($"{e.message.TrimEnd('\n', '\r')}");
Logger?.LogInformation($"{e.source}> {e.message.TrimEnd('\n', '\r')}");
}
else
{
Logger?.LogInformation($"{e.source}> {e.message.TrimEnd('\n', '\r')}");
Logger?.LogInformation($"{e.message.TrimEnd('\n', '\r')}");
}
}
}
10 changes: 5 additions & 5 deletions Source/v2/Meadow.Cli/Commands/Current/ListenCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Meadow.CLI.Commands.DeviceManagement;
[Command("listen", Description = "Listen for console output from Meadow")]
public class ListenCommand : BaseDeviceCommand<ListenCommand>
{
[CommandOption("no-prefix", 'n', Description = "When set, the message source prefix (e.g. 'stdout>') is suppressed", IsRequired = false)]
public bool NoPrefix { get; init; }
[CommandOption("prefix", 'p', Description = "When set, the message source prefix (e.g. 'stdout>') is shown", IsRequired = false)]
public bool Prefix { get; init; } = false;

public ListenCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory)
: base(connectionManager, loggerFactory)
Expand All @@ -20,13 +20,13 @@ private void Connection_ConnectionMessage(object? sender, string e)

private void OnDeviceMessageReceived(object? sender, (string message, string? source) e)
{
if (NoPrefix)
if (Prefix)
{
Logger?.LogInformation($"{e.message.TrimEnd('\n', '\r')}");
Logger?.LogInformation($"{e.source}> {e.message.TrimEnd('\n', '\r')}");
}
else
{
Logger?.LogInformation($"{e.source}> {e.message.TrimEnd('\n', '\r')}");
Logger?.LogInformation($"{e.message.TrimEnd('\n', '\r')}");
}
}

Expand Down
3 changes: 3 additions & 0 deletions Source/v2/Meadow.Hcom/Connections/ConnectionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public abstract class ConnectionBase : IMeadowConnection, IDisposable

public abstract Task SendDebuggerData(byte[] debuggerData, uint userData, CancellationToken? cancellationToken);

public abstract Task NshDisable(CancellationToken? cancellationToken = null);
public abstract Task NshEnable(CancellationToken? cancellationToken = null);

public ConnectionBase()
{
}
Expand Down
10 changes: 10 additions & 0 deletions Source/v2/Meadow.Hcom/Connections/LocalConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,14 @@ public override void Detach()
{
throw new NotImplementedException();
}

public override Task NshDisable(CancellationToken? cancellationToken = null)
{
throw new NotImplementedException();
}

public override Task NshEnable(CancellationToken? cancellationToken = null)
{
throw new NotImplementedException();
}
}
29 changes: 28 additions & 1 deletion Source/v2/Meadow.Hcom/Connections/SerialConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ private void CommandManager()
{
while (!_isDisposed)
{
_commandEvent.WaitOne();
_commandEvent.WaitOne(1000);

while (_pendingCommands.Count > 0)
{
Debug.WriteLine($"There are {_pendingCommands.Count} pending commands");
Expand Down Expand Up @@ -711,6 +712,32 @@ public override async Task RuntimeDisable(CancellationToken? cancellationToken =
await WaitForConcluded(null, cancellationToken);
}

public override async Task NshEnable(CancellationToken? cancellationToken = null)
{
var command = RequestBuilder.Build<NshEnableDisableRequest>(1);

InfoMessages.Clear();

_lastRequestConcluded = null;

EnqueueRequest(command);

await WaitForConcluded(null, cancellationToken);
}

public override async Task NshDisable(CancellationToken? cancellationToken = null)
{
var command = RequestBuilder.Build<NshEnableDisableRequest>(0);

InfoMessages.Clear();

_lastRequestConcluded = null;

EnqueueRequest(command);

await WaitForConcluded(null, cancellationToken);
}

public override async Task TraceEnable(CancellationToken? cancellationToken = null)
{
var command = RequestBuilder.Build<TraceEnableRequest>();
Expand Down
10 changes: 10 additions & 0 deletions Source/v2/Meadow.Hcom/Connections/SimulatorConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,14 @@ public override void Detach()
{
throw new NotImplementedException();
}

public override Task NshDisable(CancellationToken? cancellationToken = null)
{
throw new NotImplementedException();
}

public override Task NshEnable(CancellationToken? cancellationToken = null)
{
throw new NotImplementedException();
}
}
10 changes: 10 additions & 0 deletions Source/v2/Meadow.Hcom/Connections/TcpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,14 @@ public override void Detach()
{
throw new NotImplementedException();
}

public override Task NshDisable(CancellationToken? cancellationToken = null)
{
throw new NotImplementedException();
}

public override Task NshEnable(CancellationToken? cancellationToken = null)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private async Task SendToVisualStudio()
{
if (_networkStream != null && _networkStream.CanWrite)
{
_vsDebugDataReady.WaitOne();
_vsDebugDataReady.WaitOne(1000);

while (_debuggerMessages.Count > 0)
{
Expand Down
3 changes: 3 additions & 0 deletions Source/v2/Meadow.Hcom/IMeadowConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ public interface IMeadowConnection : IDisposable
Task<DebuggingServer> StartDebuggingSession(int port, ILogger? logger, CancellationToken cancellationToken);
Task StartDebugging(int port, ILogger? logger, CancellationToken? cancellationToken);
Task SendDebuggerData(byte[] debuggerData, uint userData, CancellationToken? cancellationToken);

Task NshDisable(CancellationToken? cancellationToken = null);
Task NshEnable(CancellationToken? cancellationToken = null);
}
2 changes: 2 additions & 0 deletions Source/v2/Meadow.Hcom/IMeadowDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface IMeadowDevice
Task<string> GetPublicKey(CancellationToken? cancellationToken = null);
Task StartDebugging(int port, ILogger? logger, CancellationToken? cancellationToken);
Task SendDebuggerData(byte[] debuggerData, uint userData, CancellationToken? cancellationToken);
Task NshDisable(CancellationToken? cancellationToken = null);
Task NshEnable(CancellationToken? cancellationToken = null);
}
10 changes: 10 additions & 0 deletions Source/v2/Meadow.Hcom/MeadowDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,14 @@ public async Task SendDebuggerData(byte[] debuggerData, uint userData, Cancellat
{
await _connection.SendDebuggerData(debuggerData, userData, cancellationToken);
}

public async Task NshDisable(CancellationToken? cancellationToken = null)
{
await _connection.NshDisable(cancellationToken);
}

public async Task NshEnable(CancellationToken? cancellationToken = null)
{
await _connection.NshEnable(cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Meadow.Hcom;

internal class NshEnableDisableRequest : Request
{
public override RequestType RequestType => RequestType.HCOM_MDOW_REQUEST_ENABLE_DISABLE_NSH;
}

0 comments on commit ecb899c

Please sign in to comment.