Beispiel #1
0
        protected virtual void WriteAttribute(ServiceAttribute attr, byte[] buffer, ref int offset)
        {
            int len;

            len     = CreateAttrId(attr.Id, buffer, offset);
            offset += len;
            len     = CreateElement(attr.Value, buffer, offset);
            offset += len;
        }
Beispiel #2
0
        static ServiceElement GetChannelElement(ServiceRecord record, BluetoothProtocolDescriptorType proto)
        {
            if (!record.Contains(UniversalAttributeId.ProtocolDescriptorList))
            {
                goto NotFound;
            }
            ServiceAttribute attr = record.GetAttributeById(UniversalAttributeId.ProtocolDescriptorList);

            bool?isSimpleRfcomm;

            return(GetChannelElement(attr, proto, out isSimpleRfcomm));

NotFound:
            return(null);
        }
Beispiel #3
0
        // TODO GetRfcommChannelElement(ServiceAttribute attr) Could be public -> Tests!
        internal static ServiceElement GetChannelElement(ServiceAttribute attr,
                                                         BluetoothProtocolDescriptorType proto,
                                                         out bool?isSimpleRfcomm)
        {
            if (proto != BluetoothProtocolDescriptorType.L2Cap &&
                proto != BluetoothProtocolDescriptorType.Rfcomm)
            {
                throw new ArgumentException("Can only fetch RFCOMM or L2CAP element.");
            }

            //
            isSimpleRfcomm = true;
            Debug.Assert(attr != null, "attr != null");
            ServiceElement e0 = attr.Value;

            if (e0.ElementType == ElementType.ElementAlternative)
            {
                Trace.WriteLine("Don't support ElementAlternative ProtocolDescriptorList values.");

                goto NotFound;
            }
            else if (e0.ElementType != ElementType.ElementSequence)
            {
                Trace.WriteLine("Bad ProtocolDescriptorList base element.");

                goto NotFound;
            }
            IList <ServiceElement>       protoStack = e0.GetValueAsElementList();
            IEnumerator <ServiceElement> etor       = protoStack.GetEnumerator();
            ServiceElement         layer;
            IList <ServiceElement> layerContent;
            ServiceElement         channelElement;

            // -- L2CAP Layer --
            if (!etor.MoveNext())
            {
                Trace.WriteLine(string.Format(CultureInfo.InvariantCulture,
                                              "Protocol stack truncated before {0}.", "L2CAP"));

                goto NotFound;
            }
            layer        = etor.Current; //cast here are for non-Generic version.
            layerContent = layer.GetValueAsElementList();
            if (layerContent[0].GetValueAsUuid() != BluetoothProtocol.L2CapProtocol)
            {
                Trace.WriteLine(String.Format(CultureInfo.InvariantCulture,
                                              "Bad protocol stack, layer {0} is not {1}.", 1, "L2CAP"));
                goto NotFound;
            }
            bool hasPsmEtc = layerContent.Count != 1;

            // Cast for FX1.1 object
            isSimpleRfcomm = (bool)isSimpleRfcomm && !hasPsmEtc;
            if (proto == BluetoothProtocolDescriptorType.L2Cap)
            {
                if (layerContent.Count < 2)
                {
                    Trace.WriteLine("L2CAP PSM element was requested but the L2CAP layer in this case hasn't a second element.");

                    goto NotFound;
                }
                channelElement = (ServiceElement)layerContent[1];
                goto Success;
            }
            //
            // -- RFCOMM Layer --
            if (!etor.MoveNext())
            {
                Trace.WriteLine(string.Format(CultureInfo.InvariantCulture,
                                              "Protocol stack truncated before {0}.", "RFCOMM"));

                goto NotFound;
            }
            layer        = etor.Current;
            layerContent = layer.GetValueAsElementList();
            if (layerContent[0].GetValueAsUuid() != BluetoothProtocol.RFCommProtocol)
            {
                Trace.WriteLine(String.Format(CultureInfo.InvariantCulture,
                                              "Bad protocol stack, layer {0} is not {1}.", 2, "RFCOMM"));

                goto NotFound;
            }
            //
            if (layerContent.Count < 2)
            {
                Trace.WriteLine(String.Format(CultureInfo.InvariantCulture,
                                              "Bad protocol stack, layer {0} hasn't a second element.", 2));

                goto NotFound;
            }
            channelElement = (ServiceElement)layerContent[1];
            if (channelElement.ElementType != ElementType.UInt8)
            {
                Trace.WriteLine(String.Format(CultureInfo.InvariantCulture,
                                              "Bad protocol stack, layer {0} is not UInt8.", 2));

                goto NotFound;
            }
            // Success
            //
            // -- Any remaining layer(s) --
            bool extraLayers = etor.MoveNext();

            isSimpleRfcomm = (bool)isSimpleRfcomm && !extraLayers;
Success:
            //
            return(channelElement);

NotFound:
            isSimpleRfcomm = null;
            return(null);
        }
 private static string GetIdString(ServiceAttribute attribute)
 {
     return($"0x{attribute.Id:X}");
 }
Beispiel #5
0
 /// <overloads>
 /// Add a custom attribute.
 /// </overloads>
 /// -
 /// <summary>
 /// Add a custom attribute from a given <see cref="T:InTheHand.Net.Bluetooth.ServiceAttribute"/>
 /// </summary>
 /// -
 /// <param name="serviceAttribute">An attribute as a
 /// <see cref="T:InTheHand.Net.Bluetooth.ServiceAttribute"/> instance.
 /// </param>
 public void AddCustomAttribute(ServiceAttribute serviceAttribute)
 {
     this.AddCustomAttributes(new ServiceAttribute[] { serviceAttribute });
 }