|
| 1 | +using nanoFramework.Tools.Debugger; |
| 2 | +using nanoFramework.Tools.Debugger.Extensions; |
| 3 | +using Newtonsoft.Json; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using System.Runtime; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace nanoFramework.Tools.FirmwareFlasher.FileDeployment |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// File Deployment Manager class. |
| 16 | + /// </summary> |
| 17 | + public class FileDeploymentManager |
| 18 | + { |
| 19 | + private FileDeploymentConfiguration _configuration; |
| 20 | + private VerbosityLevel _verbosity; |
| 21 | + private string _serialPort; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Creates an instance of FileDeploymentManager. |
| 25 | + /// </summary> |
| 26 | + public FileDeploymentManager(string configFilePath, string originalPort, VerbosityLevel verbosity) |
| 27 | + { |
| 28 | + _configuration = JsonConvert.DeserializeObject<FileDeploymentConfiguration>(File.ReadAllText(configFilePath)); |
| 29 | + _serialPort = string.IsNullOrEmpty(_configuration.SerialPort) ? originalPort : _configuration.SerialPort; |
| 30 | + _verbosity = verbosity; |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Deploys async the files. |
| 35 | + /// </summary> |
| 36 | + /// <returns>An ExitCode error.</returns> |
| 37 | + public async Task<ExitCodes> DeployAsync() |
| 38 | + { |
| 39 | + // number of retries when performing a deploy operation |
| 40 | + const int _numberOfRetries = 5; |
| 41 | + // timeout when performing a deploy operation |
| 42 | + const int _timeoutMiliseconds = 1000; |
| 43 | + |
| 44 | + NanoDeviceBase device = null; |
| 45 | + PortBase serialDebugClient; |
| 46 | + int retryCount = 0; |
| 47 | + |
| 48 | + serialDebugClient = PortBase.CreateInstanceForSerial(false); |
| 49 | + |
| 50 | + try |
| 51 | + { |
| 52 | + serialDebugClient.AddDevice(_serialPort); |
| 53 | + |
| 54 | + device = serialDebugClient.NanoFrameworkDevices[0]; |
| 55 | + } |
| 56 | + catch (Exception ex) |
| 57 | + { |
| 58 | + Console.ForegroundColor = ConsoleColor.Red; |
| 59 | + Console.WriteLine($"Error connecting to nanoDevice on {_serialPort} to deploy files"); |
| 60 | + Console.ForegroundColor = ConsoleColor.White; |
| 61 | + return ExitCodes.E2000; |
| 62 | + } |
| 63 | + |
| 64 | + // check if debugger engine exists |
| 65 | + if (device.DebugEngine == null) |
| 66 | + { |
| 67 | + device.CreateDebugEngine(); |
| 68 | + if (_verbosity >= VerbosityLevel.Normal) |
| 69 | + { |
| 70 | + Console.WriteLine($"Debug engine created."); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + bool deviceIsInInitializeState = false; |
| 75 | + |
| 76 | + retryDebug: |
| 77 | + bool connectResult = device.DebugEngine.Connect(5000, true, true); |
| 78 | + if (retryCount == 0 && _verbosity >= VerbosityLevel.Normal) |
| 79 | + { |
| 80 | + Console.WriteLine($"Device connected and ready for file deployment."); |
| 81 | + } |
| 82 | + else if (_verbosity >= VerbosityLevel.Normal) |
| 83 | + { |
| 84 | + Console.WriteLine($"Device connect result is {connectResult}. Attempt {retryCount}/{_numberOfRetries}"); |
| 85 | + } |
| 86 | + |
| 87 | + if (!connectResult) |
| 88 | + { |
| 89 | + if (retryCount < _numberOfRetries) |
| 90 | + { |
| 91 | + // Give it a bit of time |
| 92 | + await Task.Delay(100); |
| 93 | + retryCount++; |
| 94 | + |
| 95 | + goto retryDebug; |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + Console.ForegroundColor = ConsoleColor.Red; |
| 100 | + Console.WriteLine($"Error connecting to debug engine on nanoDevice on {_serialPort} to deploy files"); |
| 101 | + Console.ForegroundColor = ConsoleColor.White; |
| 102 | + return ExitCodes.E2000; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + retryCount = 0; |
| 107 | + |
| 108 | + // initial check |
| 109 | + if (device.DebugEngine.IsDeviceInInitializeState()) |
| 110 | + { |
| 111 | + if (_verbosity >= VerbosityLevel.Normal) |
| 112 | + { |
| 113 | + Console.ForegroundColor = ConsoleColor.Yellow; |
| 114 | + Console.WriteLine($"Device status verified as being in initialized state. Requesting to resume execution. Attempt {retryCount}/{_numberOfRetries}."); |
| 115 | + Console.ForegroundColor = ConsoleColor.White; |
| 116 | + } |
| 117 | + |
| 118 | + // set flag |
| 119 | + deviceIsInInitializeState = true; |
| 120 | + |
| 121 | + // device is still in initialization state, try resume execution |
| 122 | + device.DebugEngine.ResumeExecution(); |
| 123 | + } |
| 124 | + |
| 125 | + // handle the workflow required to try resuming the execution on the device |
| 126 | + // only required if device is not already there |
| 127 | + // retry 5 times with a 500ms interval between retries |
| 128 | + while (retryCount++ < _numberOfRetries && deviceIsInInitializeState) |
| 129 | + { |
| 130 | + if (!device.DebugEngine.IsDeviceInInitializeState()) |
| 131 | + { |
| 132 | + if (_verbosity >= VerbosityLevel.Diagnostic) |
| 133 | + { |
| 134 | + Console.WriteLine($"Device has completed initialization."); |
| 135 | + } |
| 136 | + |
| 137 | + // done here |
| 138 | + deviceIsInInitializeState = false; |
| 139 | + break; |
| 140 | + } |
| 141 | + |
| 142 | + if (_verbosity >= VerbosityLevel.Diagnostic) |
| 143 | + { |
| 144 | + Console.WriteLine($"Waiting for device to report initialization completed ({retryCount}/{_numberOfRetries})."); |
| 145 | + } |
| 146 | + |
| 147 | + // provide feedback to user on the 1st pass |
| 148 | + if (retryCount == 0) |
| 149 | + { |
| 150 | + if (_verbosity >= VerbosityLevel.Diagnostic) |
| 151 | + { |
| 152 | + Console.WriteLine($"Waiting for device to initialize."); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + if (device.DebugEngine.IsConnectedTonanoBooter) |
| 157 | + { |
| 158 | + if (_verbosity >= VerbosityLevel.Diagnostic) |
| 159 | + { |
| 160 | + Console.WriteLine($"Device reported running nanoBooter. Requesting to load nanoCLR."); |
| 161 | + } |
| 162 | + |
| 163 | + // request nanoBooter to load CLR |
| 164 | + device.DebugEngine.ExecuteMemory(0); |
| 165 | + } |
| 166 | + else if (device.DebugEngine.IsConnectedTonanoCLR) |
| 167 | + { |
| 168 | + if (_verbosity >= VerbosityLevel.Normal) |
| 169 | + { |
| 170 | + Console.ForegroundColor = ConsoleColor.Yellow; |
| 171 | + Console.WriteLine($"Device reported running nanoCLR. Requesting to reboot nanoCLR."); |
| 172 | + Console.ForegroundColor = ConsoleColor.White; |
| 173 | + } |
| 174 | + |
| 175 | + await Task.Run(delegate |
| 176 | + { |
| 177 | + // already running nanoCLR try rebooting the CLR |
| 178 | + device.DebugEngine.RebootDevice(RebootOptions.ClrOnly); |
| 179 | + }); |
| 180 | + } |
| 181 | + |
| 182 | + // wait before next pass |
| 183 | + // use a back-off strategy of increasing the wait time to accommodate slower or less responsive targets (such as networked ones) |
| 184 | + await Task.Delay(TimeSpan.FromMilliseconds(_timeoutMiliseconds * (retryCount + 1))); |
| 185 | + |
| 186 | + await Task.Yield(); |
| 187 | + } |
| 188 | + |
| 189 | + // check if device is still in initialized state |
| 190 | + if (!deviceIsInInitializeState) |
| 191 | + { |
| 192 | + // Deploy each file |
| 193 | + foreach (var file in _configuration.Files) |
| 194 | + { |
| 195 | + try |
| 196 | + { |
| 197 | + if (string.IsNullOrEmpty(file.SourceFilePath)) |
| 198 | + { |
| 199 | + // deleting |
| 200 | + Console.Write($"Deleting file {file.DestinationFilePath}..."); |
| 201 | + if (device.DebugEngine.DeleteStorageFile(file.DestinationFilePath) != Debugger.WireProtocol.StorageOperationErrorCode.NoError) |
| 202 | + { |
| 203 | + Console.ForegroundColor = ConsoleColor.Yellow; |
| 204 | + Console.WriteLine(); |
| 205 | + Console.WriteLine($"Error deleting file {file.DestinationFilePath}, it may not exist on the storage."); |
| 206 | + Console.ForegroundColor = ConsoleColor.White; |
| 207 | + } |
| 208 | + else |
| 209 | + { |
| 210 | + Console.ForegroundColor = ConsoleColor.Green; |
| 211 | + Console.WriteLine($"OK"); |
| 212 | + Console.ForegroundColor = ConsoleColor.White; |
| 213 | + } |
| 214 | + } |
| 215 | + else |
| 216 | + { |
| 217 | + Console.Write($"Deploying file {file.SourceFilePath} to {file.DestinationFilePath}..."); |
| 218 | + var ret = device.DebugEngine.AddStorageFile(file.DestinationFilePath, File.ReadAllBytes(file.SourceFilePath)); |
| 219 | + if (ret != Debugger.WireProtocol.StorageOperationErrorCode.NoError) |
| 220 | + { |
| 221 | + Console.ForegroundColor = ConsoleColor.Red; |
| 222 | + Console.WriteLine(); |
| 223 | + Console.WriteLine($"Error deploying content file {file.SourceFilePath} to {file.DestinationFilePath}"); |
| 224 | + Console.ForegroundColor = ConsoleColor.White; |
| 225 | + } |
| 226 | + else |
| 227 | + { |
| 228 | + Console.ForegroundColor = ConsoleColor.Green; |
| 229 | + Console.WriteLine($"OK"); |
| 230 | + Console.ForegroundColor = ConsoleColor.White; |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | + catch |
| 235 | + { |
| 236 | + if (_verbosity >= VerbosityLevel.Normal) |
| 237 | + { |
| 238 | + Console.ForegroundColor = ConsoleColor.Red; |
| 239 | + Console.WriteLine(); |
| 240 | + Console.WriteLine($"Exception deploying content file {file.SourceFilePath} to {file.DestinationFilePath}"); |
| 241 | + Console.ForegroundColor = ConsoleColor.White; |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + } |
| 246 | + else |
| 247 | + { |
| 248 | + return ExitCodes.E2002; |
| 249 | + } |
| 250 | + |
| 251 | + return ExitCodes.OK; |
| 252 | + } |
| 253 | + } |
| 254 | +} |
0 commit comments