Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

Commit 2479eef

Browse files
author
Andrew
authored
Merge pull request #38 from anmenaga/BetterNonRootErrorMessage
Fixed error message about root privileges requirement
2 parents 325fff0 + d971fdf commit 2479eef

File tree

6 files changed

+545
-76
lines changed

6 files changed

+545
-76
lines changed

src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.cs

Lines changed: 142 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,18 @@ public GetI2CDevice()
118118

119119
protected override void ProcessRecord()
120120
{
121-
WriteObject(new I2CDevice(Unosquare.RaspberryIO.Pi.I2C.AddDevice(this.Id), this.Id, this.FriendlyName));
121+
try
122+
{
123+
WriteObject(new I2CDevice(Unosquare.RaspberryIO.Pi.I2C.AddDevice(this.Id), this.Id, this.FriendlyName));
124+
}
125+
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
126+
{
127+
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
128+
{
129+
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
130+
}
131+
throw;
132+
}
122133
}
123134
}
124135

@@ -145,33 +156,44 @@ public GetI2CRegister()
145156

146157
protected override void ProcessRecord()
147158
{
148-
if (this.ByteCount > 1)
159+
try
149160
{
150-
this.Device.device.Write((byte)this.Register);
151-
byte[] value = this.Device.device.Read(this.ByteCount);
152-
if (this.Raw)
161+
if (this.ByteCount > 1)
153162
{
154-
WriteObject(value);
163+
this.Device.device.Write((byte)this.Register);
164+
byte[] value = this.Device.device.Read(this.ByteCount);
165+
if (this.Raw)
166+
{
167+
WriteObject(value);
168+
}
169+
else
170+
{
171+
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register);
172+
result.Data = value;
173+
WriteObject(result);
174+
}
155175
}
156176
else
157177
{
158-
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register);
159-
result.Data = value;
160-
WriteObject(result);
178+
byte value = this.Device.device.ReadAddressByte(this.Register);
179+
if (this.Raw)
180+
{
181+
WriteObject(value);
182+
}
183+
else
184+
{
185+
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register, new byte[1] { value });
186+
WriteObject(result);
187+
}
161188
}
162189
}
163-
else
190+
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
164191
{
165-
byte value = this.Device.device.ReadAddressByte(this.Register);
166-
if (this.Raw)
192+
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
167193
{
168-
WriteObject(value);
169-
}
170-
else
171-
{
172-
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register, new byte[1] { value });
173-
WriteObject(result);
194+
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
174195
}
196+
throw;
175197
}
176198
}
177199
}
@@ -193,11 +215,22 @@ public class SetI2CRegister : Cmdlet
193215

194216
protected override void ProcessRecord()
195217
{
196-
this.Device.device.WriteAddressByte(this.Register, this.Data[0]);
197-
if (this.PassThru)
218+
try
219+
{
220+
this.Device.device.WriteAddressByte(this.Register, this.Data[0]);
221+
if (this.PassThru)
222+
{
223+
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register, this.Data);
224+
WriteObject(result);
225+
}
226+
}
227+
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
198228
{
199-
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register, this.Data);
200-
WriteObject(result);
229+
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
230+
{
231+
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
232+
}
233+
throw;
201234
}
202235
}
203236
}
@@ -216,20 +249,31 @@ public class SetGpioPin : Cmdlet
216249

217250
protected override void ProcessRecord()
218251
{
219-
if (this.Id != null)
252+
try
220253
{
221-
foreach (int pinId in this.Id)
254+
if (this.Id != null)
222255
{
223-
var pin = Unosquare.RaspberryIO.Pi.Gpio[pinId];
224-
pin.PinMode = Unosquare.RaspberryIO.Gpio.GpioPinDriveMode.Output;
225-
pin.Write((Unosquare.RaspberryIO.Gpio.GpioPinValue)this.Value);
226-
if (this.PassThru)
256+
foreach (int pinId in this.Id)
227257
{
228-
GpioPinData pinData = new GpioPinData(pinId, this.Value, pin);
229-
WriteObject(pinData);
258+
var pin = Unosquare.RaspberryIO.Pi.Gpio[pinId];
259+
pin.PinMode = Unosquare.RaspberryIO.Gpio.GpioPinDriveMode.Output;
260+
pin.Write((Unosquare.RaspberryIO.Gpio.GpioPinValue)this.Value);
261+
if (this.PassThru)
262+
{
263+
GpioPinData pinData = new GpioPinData(pinId, this.Value, pin);
264+
WriteObject(pinData);
265+
}
230266
}
231267
}
232268
}
269+
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
270+
{
271+
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
272+
{
273+
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
274+
}
275+
throw;
276+
}
233277
}
234278
}
235279

@@ -247,48 +291,59 @@ public class GetGpioPin : Cmdlet
247291

248292
protected override void ProcessRecord()
249293
{
250-
ArrayList pinList = new ArrayList();
251-
252-
if ((this.Id == null) || (this.Id.Length <= 0))
294+
try
253295
{
254-
foreach (var pin in Unosquare.RaspberryIO.Pi.Gpio.Pins)
255-
{
256-
pinList.Add(pin.PinNumber);
257-
}
258-
}
259-
else
260-
{
261-
pinList.AddRange(this.Id);
262-
}
296+
ArrayList pinList = new ArrayList();
263297

264-
foreach(int pinId in pinList)
265-
{
266-
var pin = Unosquare.RaspberryIO.Pi.Gpio[pinId];
267-
try
298+
if ((this.Id == null) || (this.Id.Length <= 0))
268299
{
269-
pin.PinMode = Unosquare.RaspberryIO.Gpio.GpioPinDriveMode.Input;
270-
if (this.PullMode.HasValue)
300+
foreach (var pin in Unosquare.RaspberryIO.Pi.Gpio.Pins)
271301
{
272-
pin.InputPullMode = (Unosquare.RaspberryIO.Gpio.GpioPinResistorPullMode)this.PullMode.Value;
273-
};
302+
pinList.Add(pin.PinNumber);
303+
}
274304
}
275-
catch (System.NotSupportedException)
305+
else
276306
{
277-
// We want to avoid errors like
278-
// System.NotSupportedException : Get - GpioPin : Pin Pin15 'BCM 14 (UART Transmit)' does not support mode 'Input'.Pin capabilities are limited to: UARTTXD
279-
// at the same time we need to return PinInfo for such pins, so we need to continue processing
307+
pinList.AddRange(this.Id);
280308
}
281-
bool pinBoolValue = pin.Read();
282309

283-
if (this.Raw)
310+
foreach (int pinId in pinList)
284311
{
285-
WriteObject(pinBoolValue ? SignalLevel.High : SignalLevel.Low);
312+
var pin = Unosquare.RaspberryIO.Pi.Gpio[pinId];
313+
try
314+
{
315+
pin.PinMode = Unosquare.RaspberryIO.Gpio.GpioPinDriveMode.Input;
316+
if (this.PullMode.HasValue)
317+
{
318+
pin.InputPullMode = (Unosquare.RaspberryIO.Gpio.GpioPinResistorPullMode)this.PullMode.Value;
319+
};
320+
}
321+
catch (System.NotSupportedException)
322+
{
323+
// We want to avoid errors like
324+
// System.NotSupportedException : Get - GpioPin : Pin Pin15 'BCM 14 (UART Transmit)' does not support mode 'Input'.Pin capabilities are limited to: UARTTXD
325+
// at the same time we need to return PinInfo for such pins, so we need to continue processing
326+
}
327+
bool pinBoolValue = pin.Read();
328+
329+
if (this.Raw)
330+
{
331+
WriteObject(pinBoolValue ? SignalLevel.High : SignalLevel.Low);
332+
}
333+
else
334+
{
335+
GpioPinData pinData = new GpioPinData(pinId, pinBoolValue ? SignalLevel.High : SignalLevel.Low, pin);
336+
WriteObject(pinData);
337+
}
286338
}
287-
else
339+
}
340+
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
341+
{
342+
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
288343
{
289-
GpioPinData pinData = new GpioPinData(pinId, pinBoolValue ? SignalLevel.High : SignalLevel.Low, pin);
290-
WriteObject(pinData);
344+
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
291345
}
346+
throw;
292347
}
293348
}
294349
}
@@ -316,26 +371,37 @@ public SendSPIData()
316371

317372
protected override void ProcessRecord()
318373
{
319-
var spiChannel = Unosquare.RaspberryIO.Pi.Spi.Channel0;
320-
if (this.Channel == 1)
321-
{
322-
spiChannel = Unosquare.RaspberryIO.Pi.Spi.Channel1;
323-
Unosquare.RaspberryIO.Pi.Spi.Channel1Frequency = (int)this.Frequency;
324-
}
325-
else
374+
try
326375
{
327-
Unosquare.RaspberryIO.Pi.Spi.Channel0Frequency = (int)this.Frequency;
328-
};
376+
var spiChannel = Unosquare.RaspberryIO.Pi.Spi.Channel0;
377+
if (this.Channel == 1)
378+
{
379+
spiChannel = Unosquare.RaspberryIO.Pi.Spi.Channel1;
380+
Unosquare.RaspberryIO.Pi.Spi.Channel1Frequency = (int)this.Frequency;
381+
}
382+
else
383+
{
384+
Unosquare.RaspberryIO.Pi.Spi.Channel0Frequency = (int)this.Frequency;
385+
};
329386

330-
var response = spiChannel.SendReceive(this.Data);
331-
if (this.Raw)
332-
{
333-
WriteObject(response);
387+
var response = spiChannel.SendReceive(this.Data);
388+
if (this.Raw)
389+
{
390+
WriteObject(response);
391+
}
392+
else
393+
{
394+
SPIData spiData = new SPIData(this.Channel, this.Frequency, this.Data, response);
395+
WriteObject(spiData);
396+
}
334397
}
335-
else
398+
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
336399
{
337-
SPIData spiData = new SPIData(this.Channel, this.Frequency, this.Data, response);
338-
WriteObject(spiData);
400+
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
401+
{
402+
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
403+
}
404+
throw;
339405
}
340406
}
341407
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
//------------------------------------------------------------------------------
3+
// <auto-generated>
4+
// This code was generated by a dotnet run from src\ResGen folder.
5+
// To add or remove a member, edit your .resx file then rerun src\ResGen.
6+
//
7+
// Changes to this file may cause incorrect behavior and will be lost if
8+
// the code is regenerated.
9+
// </auto-generated>
10+
//------------------------------------------------------------------------------
11+
12+
13+
using System;
14+
using System.Reflection;
15+
16+
/// <summary>
17+
/// A strongly-typed resource class, for looking up localized strings, etc.
18+
/// </summary>
19+
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
20+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
21+
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
22+
23+
internal class Resources {
24+
25+
private static global::System.Resources.ResourceManager resourceMan;
26+
27+
private static global::System.Globalization.CultureInfo resourceCulture;
28+
29+
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
30+
internal Resources() {
31+
}
32+
33+
/// <summary>
34+
/// Returns the cached ResourceManager instance used by this class.
35+
/// </summary>
36+
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
37+
internal static global::System.Resources.ResourceManager ResourceManager {
38+
get {
39+
if (object.ReferenceEquals(resourceMan, null)) {
40+
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Microsoft.PowerShell.IoT.resources.Resources", typeof(Resources).Assembly);
41+
resourceMan = temp;
42+
}
43+
return resourceMan;
44+
}
45+
}
46+
47+
/// <summary>
48+
/// Overrides the current threads CurrentUICulture property for all
49+
/// resource lookups using this strongly typed resource class.
50+
/// </summary>
51+
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
52+
internal static global::System.Globalization.CultureInfo Culture {
53+
get {
54+
return resourceCulture;
55+
}
56+
set {
57+
resourceCulture = value;
58+
}
59+
}
60+
61+
62+
/// <summary>
63+
/// Looks up a localized string similar to
64+
/// This cmdlet requires PowerShell to be started with root privileges.
65+
///
66+
/// </summary>
67+
internal static string ErrNeedRootPrivileges {
68+
get {
69+
return ResourceManager.GetString("ErrNeedRootPrivileges", resourceCulture);
70+
}
71+
}
72+
73+
}
74+

0 commit comments

Comments
 (0)