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

Commit fec4531

Browse files
author
Andrew Menagarishvili
committed
Fixed error message about root privileges requirement
1 parent 329928f commit fec4531

File tree

6 files changed

+539
-76
lines changed

6 files changed

+539
-76
lines changed

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

Lines changed: 136 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,17 @@ 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+
}
122132
}
123133
}
124134

@@ -145,32 +155,42 @@ public GetI2CRegister()
145155

146156
protected override void ProcessRecord()
147157
{
148-
if (this.ByteCount > 1)
158+
try
149159
{
150-
this.Device.device.Write((byte)this.Register);
151-
byte[] value = this.Device.device.Read(this.ByteCount);
152-
if (this.Raw)
160+
if (this.ByteCount > 1)
153161
{
154-
WriteObject(value);
162+
this.Device.device.Write((byte)this.Register);
163+
byte[] value = this.Device.device.Read(this.ByteCount);
164+
if (this.Raw)
165+
{
166+
WriteObject(value);
167+
}
168+
else
169+
{
170+
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register);
171+
result.Data = value;
172+
WriteObject(result);
173+
}
155174
}
156175
else
157176
{
158-
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register);
159-
result.Data = value;
160-
WriteObject(result);
177+
byte value = this.Device.device.ReadAddressByte(this.Register);
178+
if (this.Raw)
179+
{
180+
WriteObject(value);
181+
}
182+
else
183+
{
184+
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register, new byte[1] { value });
185+
WriteObject(result);
186+
}
161187
}
162188
}
163-
else
189+
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
164190
{
165-
byte value = this.Device.device.ReadAddressByte(this.Register);
166-
if (this.Raw)
191+
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
167192
{
168-
WriteObject(value);
169-
}
170-
else
171-
{
172-
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register, new byte[1] { value });
173-
WriteObject(result);
193+
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
174194
}
175195
}
176196
}
@@ -193,11 +213,21 @@ public class SetI2CRegister : Cmdlet
193213

194214
protected override void ProcessRecord()
195215
{
196-
this.Device.device.WriteAddressByte(this.Register, this.Data[0]);
197-
if (this.PassThru)
216+
try
217+
{
218+
this.Device.device.WriteAddressByte(this.Register, this.Data[0]);
219+
if (this.PassThru)
220+
{
221+
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register, this.Data);
222+
WriteObject(result);
223+
}
224+
}
225+
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
198226
{
199-
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register, this.Data);
200-
WriteObject(result);
227+
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
228+
{
229+
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
230+
}
201231
}
202232
}
203233
}
@@ -216,20 +246,30 @@ public class SetGpioPin : Cmdlet
216246

217247
protected override void ProcessRecord()
218248
{
219-
if (this.Id != null)
249+
try
220250
{
221-
foreach (int pinId in this.Id)
251+
if (this.Id != null)
222252
{
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)
253+
foreach (int pinId in this.Id)
227254
{
228-
GpioPinData pinData = new GpioPinData(pinId, this.Value, pin);
229-
WriteObject(pinData);
255+
var pin = Unosquare.RaspberryIO.Pi.Gpio[pinId];
256+
pin.PinMode = Unosquare.RaspberryIO.Gpio.GpioPinDriveMode.Output;
257+
pin.Write((Unosquare.RaspberryIO.Gpio.GpioPinValue)this.Value);
258+
if (this.PassThru)
259+
{
260+
GpioPinData pinData = new GpioPinData(pinId, this.Value, pin);
261+
WriteObject(pinData);
262+
}
230263
}
231264
}
232265
}
266+
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
267+
{
268+
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
269+
{
270+
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
271+
}
272+
}
233273
}
234274
}
235275

@@ -247,47 +287,57 @@ public class GetGpioPin : Cmdlet
247287

248288
protected override void ProcessRecord()
249289
{
250-
ArrayList pinList = new ArrayList();
251-
252-
if ((this.Id == null) || (this.Id.Length <= 0))
290+
try
253291
{
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-
}
292+
ArrayList pinList = new ArrayList();
263293

264-
foreach(int pinId in pinList)
265-
{
266-
var pin = Unosquare.RaspberryIO.Pi.Gpio[pinId];
267-
try
294+
if ((this.Id == null) || (this.Id.Length <= 0))
268295
{
269-
pin.PinMode = Unosquare.RaspberryIO.Gpio.GpioPinDriveMode.Input;
270-
if (this.PullMode.HasValue)
296+
foreach (var pin in Unosquare.RaspberryIO.Pi.Gpio.Pins)
271297
{
272-
pin.InputPullMode = (Unosquare.RaspberryIO.Gpio.GpioPinResistorPullMode)this.PullMode.Value;
273-
};
298+
pinList.Add(pin.PinNumber);
299+
}
274300
}
275-
catch (System.NotSupportedException)
301+
else
276302
{
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
303+
pinList.AddRange(this.Id);
280304
}
281-
bool pinBoolValue = pin.Read();
282305

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

317367
protected override void ProcessRecord()
318368
{
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
369+
try
326370
{
327-
Unosquare.RaspberryIO.Pi.Spi.Channel0Frequency = (int)this.Frequency;
328-
};
371+
var spiChannel = Unosquare.RaspberryIO.Pi.Spi.Channel0;
372+
if (this.Channel == 1)
373+
{
374+
spiChannel = Unosquare.RaspberryIO.Pi.Spi.Channel1;
375+
Unosquare.RaspberryIO.Pi.Spi.Channel1Frequency = (int)this.Frequency;
376+
}
377+
else
378+
{
379+
Unosquare.RaspberryIO.Pi.Spi.Channel0Frequency = (int)this.Frequency;
380+
};
329381

330-
var response = spiChannel.SendReceive(this.Data);
331-
if (this.Raw)
332-
{
333-
WriteObject(response);
382+
var response = spiChannel.SendReceive(this.Data);
383+
if (this.Raw)
384+
{
385+
WriteObject(response);
386+
}
387+
else
388+
{
389+
SPIData spiData = new SPIData(this.Channel, this.Frequency, this.Data, response);
390+
WriteObject(spiData);
391+
}
334392
}
335-
else
393+
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
336394
{
337-
SPIData spiData = new SPIData(this.Channel, this.Frequency, this.Data, response);
338-
WriteObject(spiData);
395+
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
396+
{
397+
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
398+
}
339399
}
340400
}
341401
}
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)