internal IrDADeviceInfo(IrDAAddress id, string name, IrDAHints hints, IrDACharacterSet charset)
		{
			this.address = id;
			this.name = name;
            this.hints = hints;
            this.charset = charset;
		}
Exemple #2
0
        public static IrDADeviceInfo[] ParseDeviceList(byte[] buffer)
        {
            if (buffer == null) {
                throw new ArgumentNullException("buffer");
            }
            const int BytesInAnInt32 = 4;
            if (buffer.Length < BytesInAnInt32) {
                throw new ArgumentException("DEVICE_LIST buffer must be at least four bytes long.");
            }
            //
			int count = BitConverter.ToInt32(buffer, 0);

			//create array for results
			IrDADeviceInfo[] idia = new IrDADeviceInfo[count];
            const int devInfoLen = 29;

            //We could check that the buffer is big enough to suit its 'count' value.
            //If this ever happened currently we would just fail with IndexOutOfRangeException
            //int expectedLength = BytesInAnInt32 + count * devInfoLen;
            //if (expectedLength > buffer.Length) {
            //    throw new ArgumentException("Buffer is smaller than the count of items requires.");
            //}
            
			for(int iDev = 0; iDev < count; iDev++)
			{
				byte[] id = new byte[4];
				Buffer.BlockCopy(buffer, 4 + (iDev*devInfoLen), id, 0, 4);
				IrDAAddress devid = new IrDAAddress(id);

                //hints
                IrDAHints hints = (IrDAHints)BitConverter.ToInt16(buffer, 30 + (iDev * devInfoLen));
                //charset
                IrDACharacterSet charset = (IrDACharacterSet)buffer[32 + (iDev * devInfoLen)];
                
                //name
                Encoding e = null;
                switch (charset)
                {
                    case IrDACharacterSet.ASCII:
                        e = Encoding.ASCII;
                        break;
                    case IrDACharacterSet.Unicode:
                        e = Encoding.Unicode;
                        break;
                    default:
                        e = Encoding.GetEncoding(28590 + (int)charset);
                        break;
                }
				string name = e.GetString(buffer, 8 + (iDev*devInfoLen), 22);
                int nullIndex = name.IndexOf('\0');
				//trim nulls
				if(nullIndex > -1)
				{
					name = name.Substring(0, nullIndex);
				}
#if PocketPC
                // PPC doesn't fill the charset field! :-(  We'll attempt
                // to detect the Unicode encoding, but not the ISO-8859-X ones:
                // as their strings will display at least partially -- dropping
                // the high chars, but also because those encodings are not
                // really supported by CF anyway.
                if (Environment.OSVersion.Platform == PlatformID.WinCE) {
                    // This assert is valid, but very annoying when running the 
                    // unit-tests so we'll remove it for now.
                    // System.Diagnostics.Debug.Assert(charset == 0, "should charset==0 as field unset on CE");
                    try {
                        e = Encoding.Unicode;
                        string nameGuessUnicode = e.GetString(buffer, 8 + (iDev * devInfoLen), 22);
                        //trim nulls
                        int nullIndexGU = nameGuessUnicode.IndexOf('\0');
                        if (nullIndexGU > -1) {
                            nameGuessUnicode = nameGuessUnicode.Substring(0, nullIndexGU);
                        }
                        // If more sense was made of the bytes as Unicode, then return
                        // that string.
                        // e.g. a unicode string "abc" is 61-00-62-00-63-00 which
                        // ASCII will see as "A" and Unicode as "ABC"
                        if (nameGuessUnicode.Length > name.Length) {
                            name = nameGuessUnicode;
                        }
                    } catch { }
                }
#endif

                idia[iDev] = new IrDADeviceInfo(devid, name, hints, charset);			
				
			}

			return idia;
        }