Exemple #1
0
        /// <summary>
        /// Allocates the specified pin.
        /// </summary>
        /// <param name="pin">The pin.</param>
        /// <param name="direction">The direction.</param>
        public void Allocate(ProcessorPin pin, PinDirection direction)
        {
            var gpioId = string.Format("gpio{0}", (int)pin);

            if (Directory.Exists(Path.Combine(gpioPath, gpioId)))
            {
                // Reinitialize pin virtual file
                using (var streamWriter = new StreamWriter(Path.Combine(gpioPath, "unexport"), false))
                {
                    streamWriter.Write((int)pin);
                    streamWriter.Close();
                }
            }

            // Export pin for file mode
            using (var streamWriter = new StreamWriter(Path.Combine(gpioPath, "export"), false))
            {
                streamWriter.Write((int)pin);
                streamWriter.Close();
            }

            // Set the direction on the pin and update the exported list
            SetPinMode(pin, direction == PinDirection.Input ? Interop.BCM2835_GPIO_FSEL_INPT : Interop.BCM2835_GPIO_FSEL_OUTP);

            if (Environment.OSVersion.Platform == PlatformID.Unix)
            {
                UnixDirectoryInfo udi = new UnixDirectoryInfo(Path.Combine(gpioPath, gpioId));

                while ((udi.OwnerGroup.GroupName != "gpio") &&
                       (!udi.CanAccess(AccessModes.W_OK)))
                {
                    Thread.Sleep(1);
                    udi.Refresh();
                }
            }
            // Set direction in pin virtual file
            var filePath = Path.Combine(gpioId, "direction");

            using (var streamWriter = new StreamWriter(Path.Combine(gpioPath, filePath), false))
            {
                streamWriter.Write(direction == PinDirection.Input ? "in" : "out");
            }

            if (direction == PinDirection.Input)
            {
                PinResistor pinResistor;
                if (!pinResistors.TryGetValue(pin, out pinResistor) || pinResistor != PinResistor.None)
                {
                    SetPinResistor(pin, PinResistor.None);
                }

                SetPinDetectedEdges(pin, PinDetectedEdges.Both);
                InitializePoll(pin);
            }
        }
Exemple #2
0
        private bool DirectoryIsSuitable(string folderToTest)
        {
            // A directory with invalid characters isn't suitable
            string pathToTest = null;

            try
            {
                pathToTest = Path.GetFullPath(folderToTest);
            }
            catch (Exception)
            {
                return(false);
            }
            // A directory doesn't have to exist yet to be suitable but if the root directory isn't suitable that's not good
            while (!Directory.Exists(pathToTest))
            {
                if (String.IsNullOrEmpty(pathToTest.Trim()))
                {
                    return(false);
                }
                pathToTest = Path.GetDirectoryName(pathToTest);
            }
            if (!MiscUtils.IsUnix)
            {
                // Check the OS file permissions for the folder
                var accessControlList = Directory.GetAccessControl(pathToTest);
                var accessRules       = accessControlList.GetAccessRules(true, true, typeof(SecurityIdentifier));
                var readAllowed       = false;
                var writeAllowed      = false;
                foreach (FileSystemAccessRule rule in accessRules)
                {
                    //If we find one that matches the identity we are looking for
                    using (var currentUserIdentity = WindowsIdentity.GetCurrent())
                    {
                        var userName = currentUserIdentity.User.Value;
                        if (
                            rule.IdentityReference.Value.Equals(userName, StringComparison.CurrentCultureIgnoreCase) ||
                            currentUserIdentity.Groups.Contains(rule.IdentityReference))
                        {
                            if ((FileSystemRights.Read & rule.FileSystemRights) == FileSystemRights.Read)
                            {
                                if (rule.AccessControlType == AccessControlType.Allow)
                                {
                                    readAllowed = true;
                                }
                                if (rule.AccessControlType == AccessControlType.Deny)
                                {
                                    return(false);
                                }
                            }
                            if ((FileSystemRights.Write & rule.FileSystemRights) == FileSystemRights.Write)
                            {
                                if (rule.AccessControlType == AccessControlType.Allow)
                                {
                                    writeAllowed = true;
                                }
                                if (rule.AccessControlType == AccessControlType.Deny)
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                    return(readAllowed && writeAllowed);
                }
            }
#if __MonoCS__
            var ufi = new UnixDirectoryInfo(pathToTest);
            return(ufi.CanAccess(Mono.Unix.Native.AccessModes.R_OK) && ufi.CanAccess(Mono.Unix.Native.AccessModes.W_OK)); // accessible for writing
#else
            return(false);                                                                                                // unreachable in practice
#endif
        }