Exemple #1
0
        /// <summary>
        /// "set DEVICE-ID propName value"
        /// </summary>
        public Hashtable SetDeviceProp(String deviceId, String propName, String propValue)
        {
            Hashtable ret = new Hashtable();

            ret["status"] = "FAILED";

            DataType          type = DataType.UNDEF;
            MtpDevicePropCode code = KeyToPropCode(propName, ref type);

            byte[] val = MtpHelperUtils.StringToBytes(propValue, type);

            Connect(deviceId, () =>
            {
                MtpResponse res = command.Execute(MtpOperationCode.SetDevicePropValue, new uint[1] {
                    (uint)code
                }, val);
                if (res.ResponseCode == WpdMtpLib.MtpResponseCode.OK)
                {
                    ret["status"] = "OK";
                }
                else
                {
                    ret["status"] = String.Format("FAILED({0:X4})", (UInt16)res.ResponseCode);
                }
            });
            return(ret);
        }
Exemple #2
0
        public static void loadConfigFile(String jsonPath)
        {
            var cvu16 = new System.ComponentModel.UInt16Converter();
            Func <String, UInt16> u16cv = (String s) => (UInt16)cvu16.ConvertFromString(s);

            try
            {
                FileStream fs         = new FileStream(jsonPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                var        jsonReader = JsonReaderWriterFactory.CreateJsonReader(fs, new System.Xml.XmlDictionaryReaderQuotas());
                var        root       = XElement.Load(jsonReader);

                _friendlyNames = new List <String>();
                foreach (var item in root.XPathSelectElements("//friendlyNames/*"))
                {
                    _friendlyNames.Add(item.Value);
                }
                foreach (var item in root.XPathSelectElements("//properties/*"))
                {
                    if (!item.HasElements)
                    {
                        continue;
                    }
                    var e_code = item.FirstNode;    //=> "0x500F"
                    if (e_code == null)
                    {
                        continue;
                    }
                    var e_type = e_code.NextNode;   //=> "UINT16"
                    if (e_type == null)
                    {
                        continue;
                    }
                    String name = item.Name.ToString();
                    UInt16 code = u16cv(((XElement)e_code).Value);
                    UInt16 type = (UInt16)MtpHelperUtils.TypeToDataType(((XElement)e_type).Value);
                    _properties[name] = new UInt16[2] {
                        code, type
                    };
                }
                fs.Close();
            }
            catch (System.IO.FileNotFoundException e)
            {
                String exe = System.Reflection.Assembly.GetEntryAssembly().Location;
                Console.Error.WriteLine("{0}: {1}", exe, e.Message);
            }
        }
Exemple #3
0
        static void ProcessCommand(String line)
        {
            String[] args = line.Split(null as Char[], StringSplitOptions.RemoveEmptyEntries);
            try
            {
                _commandLock.WaitOne();
                if (args.Length == 0)
                {
                    throw new MtpHelperRuntimeException("No commands");
                }
                switch (args[0])
                {
                case "deviceList":
                    if (args.Length != 1)
                    {
                        throw new MtpHelperRuntimeException(String.Format("Invalid parameter count({0} for 0)", args.Length - 1));
                    }
                    Console.WriteLine(MtpHelperUtils.ToJSON(_helper.DeviceList()));
                    break;

                case "deviceInfo":
                    if (args.Length != 2)
                    {
                        throw new MtpHelperRuntimeException(String.Format("Invalid parameter count({0} for 1)", args.Length - 1));
                    }
                    Console.WriteLine(MtpHelperUtils.ToJSON(_helper.GetDeviceInfo(args[1])));
                    break;

                case "desc":
                    if (args.Length != 3)
                    {
                        throw new MtpHelperRuntimeException(String.Format("Invalid parameter count({0} for 2)", args.Length - 1));
                    }
                    Console.WriteLine(MtpHelperUtils.ToJSON(_helper.GetDevicePropDesc(args[1], args[2])));
                    break;

                case "get":
                    if (args.Length != 3)
                    {
                        throw new MtpHelperRuntimeException(String.Format("Invalid parameter count({0} for 2)", args.Length - 1));
                    }
                    Console.WriteLine(MtpHelperUtils.ToJSON(_helper.GetDeviceProp(args[1], args[2])));
                    break;

                case "set":
                    if (args.Length != 4)
                    {
                        throw new MtpHelperRuntimeException(String.Format("Invalid parameter count({0} for 3)", args.Length - 1));
                    }
                    Console.WriteLine(MtpHelperUtils.ToJSON(_helper.SetDeviceProp(args[1], args[2], args[3])));
                    break;

                case "sendConfig":
                    if (args.Length != 3)
                    {
                        throw new MtpHelperRuntimeException(String.Format("Invalid parameter count({0} for 2)", args.Length - 1));
                    }
                    Console.WriteLine(MtpHelperUtils.ToJSON(_helper.SendConfigObject(args[1], args[2], false)));
                    break;

                case "getConfig":
                    if (args.Length != 3)
                    {
                        throw new MtpHelperRuntimeException(String.Format("Invalid parameter count({0} for 2)", args.Length - 1));
                    }
                    Console.WriteLine(MtpHelperUtils.ToJSON(_helper.GetConfigObject(args[1], args[2])));
                    break;

                case "firmwareUpdate":
                    if (args.Length != 3)
                    {
                        throw new MtpHelperRuntimeException(String.Format("Invalid parameter count({0} for 2)", args.Length - 1));
                    }
                    Console.WriteLine(MtpHelperUtils.ToJSON(_helper.SendConfigObject(args[1], args[2], true)));
                    break;

                default:
                    throw new MtpHelperRuntimeException("Invalid command");
                }
            }
            catch (MtpHelperRuntimeException e)
            {
                Console.WriteLine("{{\"status\":\"{0}\"}}", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("{{\"status\":\"{0}\"}}", e.Message);
            }
            finally
            {
                _commandLock.Release();
            }
        }