public void Five_HSP_AG()
        {
            // HeadsetAG == Rfcomm/2xStdSvcClass/BtPDL
            ServiceRecordBuilder bldr = new ServiceRecordBuilder();

            bldr.AddServiceClass(BluetoothService.HeadsetAudioGateway);
            bldr.AddServiceClass(BluetoothService.GenericAudio);
            bldr.AddBluetoothProfileDescriptor(BluetoothService.Headset, 1, 0);
            DoTest(ServiceRecordBuilderTests_Data.Five_HSPv1_1_AG, bldr);
            Assert.AreEqual(BluetoothProtocolDescriptorType.Rfcomm, bldr.ProtocolType);
        }
        public void Five_HSPv1_2()
        {
            // Headset == Rfcomm/2xStdSvcClass/BtPDL/1xCustom
            ServiceRecordBuilder bldr = new ServiceRecordBuilder();

            bldr.AddServiceClass(BluetoothService.HeadsetHeadset);
            bldr.AddServiceClass(BluetoothService.GenericAudio);
            bldr.AddBluetoothProfileDescriptor(BluetoothService.Headset, 1, 2);
            bldr.AddCustomAttribute(new ServiceAttribute(
                                        HeadsetProfileAttributeId.RemoteAudioVolumeControl,
                                        new ServiceElement(ElementType.Boolean, false)));
            DoTest(ServiceRecordBuilderTests_Data.Five_HSPv1_2_HS, bldr);
            Assert.AreEqual(BluetoothProtocolDescriptorType.Rfcomm, bldr.ProtocolType);
        }
        public void BadProtoTypeWierd()
        {
            ServiceRecordBuilder bldr = new ServiceRecordBuilder();

            bldr.AddServiceClass(0x1101);
            bldr.ProtocolType = (BluetoothProtocolDescriptorType)9999;
            DoTestFails(bldr);
        }
        public void Four()
        {
            // None/Svc16
            ServiceRecordBuilder bldr = new ServiceRecordBuilder();

            bldr.AddServiceClass(-1);
            bldr.ProtocolType = BluetoothProtocolDescriptorType.None;
            DoTest(ServiceRecordBuilderTests_Data.Four, bldr);
        }
        public void One()
        {
            // Rfcomm/StdSvcClass
            ServiceRecordBuilder bldr = new ServiceRecordBuilder();

            bldr.AddServiceClass(BluetoothService.SerialPort);
            DoTest(ServiceRecordBuilderTests_Data.One, bldr);
            Assert.AreEqual(BluetoothProtocolDescriptorType.Rfcomm, bldr.ProtocolType);
        }
        public void OnePlusName()
        {
            // Rfcomm/StdSvcClass/SvcName
            ServiceRecordBuilder bldr = new ServiceRecordBuilder();

            bldr.AddServiceClass(BluetoothService.SerialPort);
            bldr.ServiceName = "Hello World!";
            DoTest(ServiceRecordBuilderTests_Data.OnePlusName, bldr);
            Assert.AreEqual("Hello World!", bldr.ServiceName);
        }
Example #7
0
        //TODO move to base class.  Maybe also make 'virtual' AddSimpleServiceRecord method.
        static ServiceRecord CreateSimpleServiceRecord(Guid serviceClass, string serviceName)
        {
            ServiceRecordBuilder bldr = new ServiceRecordBuilder();

            bldr.AddServiceClass(serviceClass);
            bldr.ServiceName = serviceName;
            var simpleSR = bldr.ServiceRecord;

            return(simpleSR);
        }
        public void CustomDuplicateBuiltIn()
        {
            // Note: not checking here WHEN the exception is thrown...
            ServiceRecordBuilder bldr = new ServiceRecordBuilder();

            bldr.AddServiceClass(0x1101);
            bldr.AddCustomAttributes(new ServiceAttribute(UniversalAttributeId.ServiceClassIdList,
                                                          new ServiceElement(ElementType.TextString, "DUMMY")));
            DoTestFailsBuilderOrLater(bldr);
        }
Example #9
0
        private ServiceRecord NewServiceRecord(Guid serviceId)
        {
            ServiceRecordBuilder recordBuilder = new ServiceRecordBuilder();

            recordBuilder.AddServiceClass(serviceId);
            recordBuilder.ProtocolType       = BluetoothProtocolDescriptorType.Rfcomm;
            recordBuilder.ProviderName       = "MobileAuthentication Inc.";
            recordBuilder.ServiceDescription = "Keep your computer safe from ilegal access. Lock and Unlock it without pressing a single key.";
            recordBuilder.ServiceName        = "BP-Authenticator";
            return(recordBuilder.ServiceRecord);
        }
        /// <inheritdoc />
        internal override void StartListening(EndPoint desiredLocalListenEndPoint, bool useRandomPortFailOver)
        {
            if (IsListening) throw new InvalidOperationException("Attempted to call StartListening when already listening.");
            if(!(desiredLocalListenEndPoint is BluetoothEndPoint)) throw new ArgumentException("Bluetooth connections can only be made from a local BluetoothEndPoint", "desiredLocalListenIPEndPoint");

            try
            {
                ServiceRecordBuilder bldr = new ServiceRecordBuilder();
                bldr.AddServiceClass((desiredLocalListenEndPoint as BluetoothEndPoint).Service);
                if (IsDiscoverable)
                    bldr.AddCustomAttribute(new ServiceAttribute(NetworkCommsBTAttributeId.NetworkCommsEndPoint, ServiceElement.CreateNumericalServiceElement(ElementType.UInt8, 1)));

                listenerInstance = new BluetoothListener(desiredLocalListenEndPoint as BluetoothEndPoint, bldr.ServiceRecord);

                listenerInstance.Start();
                listenerInstance.BeginAcceptBluetoothClient(BluetoothConnectionReceivedAsync, null);
            }
            catch (SocketException)
            {
                //If the port we wanted is not available
                if (useRandomPortFailOver)
                {
                    try
                    {
                        Guid service = Guid.NewGuid();

                        ServiceRecordBuilder bldr = new ServiceRecordBuilder();
                        bldr.AddServiceClass(service);
                        if (IsDiscoverable)
                            bldr.AddCustomAttribute(new ServiceAttribute(NetworkCommsBTAttributeId.NetworkCommsEndPoint, ServiceElement.CreateNumericalServiceElement(ElementType.UInt8, 1)));

                        listenerInstance = new BluetoothListener(new BluetoothEndPoint((desiredLocalListenEndPoint as BluetoothEndPoint).Address, service), bldr.ServiceRecord);
                        listenerInstance.Start();
                        listenerInstance.BeginAcceptBluetoothClient(BluetoothConnectionReceivedAsync, null);
                    }
                    catch (SocketException)
                    {
                        //If we get another socket exception this appears to be a bad IP. We will just ignore this IP
                        if (NetworkComms.LoggingEnabled) NetworkComms.Logger.Error("It was not possible to open a random port on " + desiredLocalListenEndPoint + ". This endPoint may not support listening or possibly try again using a different port.");
                        throw new CommsSetupShutdownException("It was not possible to open a random port on " + desiredLocalListenEndPoint + ". This endPoint may not support listening or possibly try again using a different port.");
                    }
                }
                else
                {
                    if (NetworkComms.LoggingEnabled) NetworkComms.Logger.Error("It was not possible to listen on " + desiredLocalListenEndPoint.ToString() + ". This endPoint may not support listening or possibly try again using a different port.");
                    throw new CommsSetupShutdownException("It was not possible to listen on " + desiredLocalListenEndPoint.ToString() + ". This endPoint may not support listening or possibly try again using a different port.");
                }
            }

            this.LocalListenEndPoint = desiredLocalListenEndPoint;

            this.IsListening = true;
        }
Example #11
0
    static ServiceRecord Documentation_SRB_Simple_()
    {
        ServiceRecordBuilder bldr = new ServiceRecordBuilder();

        bldr.AddServiceClass(BluetoothService.SerialPort);
        bldr.ServiceName = "Alan's SPP service";
        bldr.AddCustomAttribute(new ServiceAttribute(0x8001,
                                                     ServiceElement.CreateNumericalServiceElement(ElementType.UInt16, 0xFEDC)));
        //
        ServiceRecord record = bldr.ServiceRecord;

        return(record);
    }
        public void CustomOne()
        {
            // Rfcomm/StdSvcClass/SvcName
            ServiceRecordBuilder bldr = new ServiceRecordBuilder();

            bldr.AddServiceClass(BluetoothService.SerialPort);
            bldr.ServiceName = "Hello World!";
            ServiceAttribute attr = new ServiceAttribute(
                UniversalAttributeId.ServiceAvailability,
                ServiceElement.CreateNumericalServiceElement(ElementType.UInt8, 255));

            bldr.AddCustomAttribute(attr);
            DoTest(ServiceRecordBuilderTests_Data.OnePlusNamePlusCustomOne, bldr);
        }
        public void Three()
        {
            // Geop/StdSvcClass/PrvName/SvcDescr
            ServiceRecordBuilder bldr = new ServiceRecordBuilder();

            bldr.AddServiceClass(BluetoothService.ObexObjectPush);
            bldr.ProtocolType       = BluetoothProtocolDescriptorType.GeneralObex;
            bldr.ProviderName       = "Alan enterprises inc.";
            bldr.ServiceDescription = "\u2020 daggers to you";
            DoTest(ServiceRecordBuilderTests_Data.Three, bldr);
            Assert.AreEqual(BluetoothProtocolDescriptorType.GeneralObex, bldr.ProtocolType);
            Assert.AreEqual("Alan enterprises inc.", bldr.ProviderName);
            Assert.AreEqual("\u2020 daggers to you", bldr.ServiceDescription);
        }
Example #14
0
        protected override void AddSimpleServiceRecord(out ServiceRecord fullServiceRecord,
                                                       int livePort, Guid serviceClass, string serviceName)
        {
            var livePortB = checked ((byte)livePort);

            m_sdpService = SdpService.CreateRfcomm(
                serviceClass,
                serviceName, livePortB, m_factory);
            ServiceRecordBuilder bldrDummy = new ServiceRecordBuilder();

            bldrDummy.AddServiceClass(serviceClass);
            bldrDummy.ServiceName = serviceName;
            fullServiceRecord     = bldrDummy.ServiceRecord;
            ServiceRecordHelper.SetRfcommChannelNumber(fullServiceRecord,
                                                       livePortB);
        }
        public void Two()
        {
            // Rfcomm/AllSvcClassTypesForms
            ServiceRecordBuilder bldr = new ServiceRecordBuilder();

            bldr.AddServiceClass(BluetoothService.SerialPort);
            bldr.AddServiceClass(new Guid("{00112233-4455-6677-8899-aabbccddeeff}"));
            bldr.AddServiceClass((UInt16)0x1106);
            bldr.AddServiceClass(0x7654);
            bldr.AddServiceClass(0x9901);
            bldr.AddServiceClass(0x123456);
            bldr.AddServiceClass(0x98761234);
            DoTest(ServiceRecordBuilderTests_Data.Two, bldr);
        }
        public void CustomTwoParamArray()
        {
            // Rfcomm/StdSvcClass/SvcName
            ServiceRecordBuilder bldr = new ServiceRecordBuilder();

            bldr.AddServiceClass(BluetoothService.SerialPort);
            bldr.ServiceName = "Hello World!";
            bldr.AddCustomAttributes(
                new ServiceAttribute(
                    UniversalAttributeId.ServiceAvailability,
                    ServiceElement.CreateNumericalServiceElement(ElementType.UInt8, 255)),
                new ServiceAttribute(
                    UniversalAttributeId.ServiceInfoTimeToLive,
                    ServiceElement.CreateNumericalServiceElement(ElementType.UInt32, 56623104))
                );
            DoTest(ServiceRecordBuilderTests_Data.OnePlusNamePlusCustomTwo, bldr);
        }
        private static ServiceRecord CreateAVariousRecord()
        {
            MyFunc <ElementType, ServiceAttributeId> createId = delegate(ElementType etX) {
                return((ServiceAttributeId)0x4000 + checked ((byte)etX));
            };
            ServiceRecordBuilder bldr = new ServiceRecordBuilder();

            bldr.AddServiceClass(BluetoothService.HealthDevice);
            bldr.ServiceName = "alan";
            IList <ServiceAttribute> attrList = new List <ServiceAttribute>();
            ElementType et_;

#if SUPPORT_NIL
            et_ = ElementType.Nil;
            attrList.Add(new ServiceAttribute(createId(et_), new ServiceElement(et_, null)));
#endif
            et_ = ElementType.Boolean;
            attrList.Add(new ServiceAttribute(createId(et_), new ServiceElement(et_, true)));
            ElementType[] weee =
            {
                ElementType.UInt8, ElementType.UInt16, ElementType.UInt32, //UInt64, UInt128,
                ElementType.Int8,  ElementType.Int16, ElementType.Int32,   //Int64, Int128,
            };
            foreach (ElementType et in weee)
            {
                attrList.Add(new ServiceAttribute(
                                 createId(et),
                                 ServiceElement.CreateNumericalServiceElement(et, (uint)et)));
            }
            et_ = ElementType.Uuid16;
            attrList.Add(new ServiceAttribute(createId(et_),
                                              new ServiceElement(et_, (UInt16)et_)));
            et_ = ElementType.Uuid32;
            attrList.Add(new ServiceAttribute(createId(et_),
                                              new ServiceElement(et_, (UInt32)et_)));
            et_ = ElementType.Uuid128;
            attrList.Add(new ServiceAttribute(createId(et_),
                                              new ServiceElement(et_, BluetoothService.CreateBluetoothUuid((int)et_))));
            bldr.AddCustomAttributes(attrList);
            bldr.AddCustomAttributes(ElementsAndVariableAndFixedInDeepTree1());
            ServiceRecord record = bldr.ServiceRecord;
            return(record);
        }
        public void CustomDuplicateTwoEnumNonGeneric()
        {
            // Note: not checking here WHEN the exception is thrown...
            ServiceRecordBuilder bldr = new ServiceRecordBuilder();

            bldr.AddServiceClass(BluetoothService.SerialPort);
            bldr.ServiceName = "Hello World!";
            ServiceAttribute[] array = new ServiceAttribute[] {
                new ServiceAttribute(
                    UniversalAttributeId.ServiceAvailability,
                    ServiceElement.CreateNumericalServiceElement(ElementType.UInt8, 255)),
                new ServiceAttribute(
                    UniversalAttributeId.ServiceAvailability,
                    ServiceElement.CreateNumericalServiceElement(ElementType.UInt8, 0x55))
            };
            System.Collections.IEnumerable eble = array;
            bldr.AddCustomAttributes(eble);
            DoTestFailsBuilderOrLater(bldr);
        }
Example #19
0
        protected override void AddSimpleServiceRecord(out ServiceRecord fullServiceRecord,
                                                       int livePort, Guid serviceClass, string serviceName)
        {
#if SdpService_CreateL2Cap
            var livePort16 = checked ((UInt16)livePort);
            //m_sdpService = SdpService.CreateL2Cap(
            //    serviceClass,
            //    serviceName, livePort16, m_factory);
#endif
            ServiceRecordBuilder bldrDummy = new ServiceRecordBuilder();
            bldrDummy.ProtocolType = BluetoothProtocolDescriptorType.L2Cap;
            bldrDummy.AddServiceClass(serviceClass);
            bldrDummy.ServiceName = serviceName;
            fullServiceRecord     = bldrDummy.ServiceRecord;
#if SdpService_CreateL2Cap
            ServiceRecordHelper.SetL2CapChannelNumber(fullServiceRecord,
                                                      livePort16);
#else
            AddCustomServiceRecord(ref fullServiceRecord, livePort);
#endif
        }
Example #20
0
    static void Documentation_SRB_ToByteArray_Pbap()
    {
        ServiceRecordBuilder bldr = new ServiceRecordBuilder();

        bldr.AddServiceClass(BluetoothService.PhonebookAccessPse);
        bldr.ServiceName = "Phonebook Access PSE";
        bldr.AddBluetoothProfileDescriptor(BluetoothService.PhonebookAccess, 1, 0); // v1.0
        const ushort SupportedRepositories = 0x0314;

        bldr.AddCustomAttribute(new ServiceAttribute(SupportedRepositories,
                                                     ServiceElement.CreateNumericalServiceElement(ElementType.UInt8, 0x03)));
        //
        ServiceRecord record = bldr.ServiceRecord;
        //
        var txt = ServiceRecordUtilities.Dump(record);

        Console.WriteLine(txt);
        //
        var byteArr = record.ToByteArray();
        var txtBA   = BitConverter.ToString(byteArr);

        Console.WriteLine(txtBA);
    }
        public void CustomTwoFromOneWithHighAttrIdAdded()
        {
            // Rfcomm/StdSvcClass/SvcName
            ServiceRecordBuilder bldr = new ServiceRecordBuilder();

            bldr.AddServiceClass(BluetoothService.SerialPort);
            bldr.ServiceName = "Hello World!";
            ServiceAttribute attr2 = new ServiceAttribute(
                0x8000,
                ServiceElement.CreateNumericalServiceElement(ElementType.UInt8, 0x80));

            bldr.AddCustomAttribute(attr2);
            ServiceAttribute attr2b = new ServiceAttribute(
                0xFFFF,
                ServiceElement.CreateNumericalServiceElement(ElementType.UInt8, 255));

            bldr.AddCustomAttribute(attr2b);
            ServiceAttribute attr = new ServiceAttribute(
                UniversalAttributeId.ServiceAvailability,
                ServiceElement.CreateNumericalServiceElement(ElementType.UInt8, 8));

            bldr.AddCustomAttribute(attr);
            DoTest(ServiceRecordBuilderTests_Data.CustomTwoFromOneWithHighAttrIdAdded, bldr);
        }
 private static ServiceRecord CreateBasicRfcommRecord(Guid serviceClassUuid, String svcName)
 {
     ServiceRecordBuilder bldr = new ServiceRecordBuilder();
     System.Diagnostics.Debug.Assert(bldr.ProtocolType == BluetoothProtocolDescriptorType.Rfcomm);
     bldr.AddServiceClass(serviceClassUuid);
     if (svcName != null) {
         bldr.ServiceName = svcName;
     }
     return bldr.ServiceRecord;
 }
Example #23
0
        internal static ServiceRecord CreateServiceRecord(ref Structs.BtSdkRemoteServiceAttrStru attrs, IBluesoleilApi api)
        {
            ServiceRecordBuilder bldr = new ServiceRecordBuilder();
            //--
            Guid sc = BluetoothService.CreateBluetoothUuid(attrs.svc_class);

            bldr.AddServiceClass(sc);
            //--
            string name = ParseServiceName(ref attrs);

            if (name.Length != 0)
            {
                bldr.ServiceName = name;
            }
            //
            byte?port   = null;
            var  extras = new List <ServiceAttribute>();

            Debug.Assert(attrs.status == 0, "attrs.status: 0x" + attrs.status.ToString("X"));
            if (attrs.ext_attributes != IntPtr.Zero)
            {
                if (sc == BluetoothService.HumanInterfaceDevice)
                {
                    var hidInfo = (Structs.BtSdkRmtHidSvcExtAttrStru_HACK)Marshal.PtrToStructure(
                        attrs.ext_attributes, typeof(Structs.BtSdkRmtHidSvcExtAttrStru_HACK));
                    Debug.Assert(Marshal.SizeOf(typeof(Structs.BtSdkRmtHidSvcExtAttrStru_HACK))
                                 == Marshal.SizeOf(hidInfo), "SizeOf x2");
                    Debug.Assert(hidInfo.size == Marshal.SizeOf(typeof(Structs.BtSdkRmtHidSvcExtAttrStru_HACK))
                                 + Structs.BtSdkRmtHidSvcExtAttrStru_HACK.StackMiscountsPaddingSize,
                                 "Different sizes!  hidInfo.size: " + hidInfo.size + ", SizeOf(): " + Marshal.SizeOf(typeof(Structs.BtSdkRmtHidSvcExtAttrStru_HACK)));
                    // TO-DO Human Interface (HID) record: Use "mask" field, it's undocumented, check for real life values
                    // With test SdpCreateAHumanInputDeviceRecordsAllTwoOfThree
                    // which adds two out of three of {DeviceReleaseNumber,DeviceSubclass,CountryCode}
                    // mask==0.  So mask apparently applies to other fields!
                    // So we check these three values for zero
                    // and discard them if so!
                    Debug.WriteLine(string.Format(CultureInfo.InvariantCulture,
                                                  "HID.mask: {0:X}", hidInfo.mask));
                    var list = new List <ServiceAttribute>();
                    if (hidInfo.deviceReleaseNumber != 0)
                    {
                        list.Add(
                            new ServiceAttribute(HidProfileAttributeId.DeviceReleaseNumber,
                                                 new ServiceElement(ElementType.UInt16, hidInfo.deviceReleaseNumber)));
                    }
                    if (hidInfo.deviceSubclass != 0)
                    {
                        list.Add(
                            new ServiceAttribute(HidProfileAttributeId.DeviceSubclass,
                                                 new ServiceElement(ElementType.UInt8, hidInfo.deviceSubclass)));
                    }
                    if (hidInfo.countryCode != 0)
                    {
                        list.Add(
                            new ServiceAttribute(HidProfileAttributeId.CountryCode,
                                                 new ServiceElement(ElementType.UInt8, hidInfo.countryCode)));
                    }
                    // TO-DO HID other...
                    extras.AddRange(list);
                }
                else if (sc == BluetoothService.PnPInformation)
                {
                    var deviceInfo = (Structs.BtSdkRmtDISvcExtAttrStru)Marshal.PtrToStructure(
                        attrs.ext_attributes, typeof(Structs.BtSdkRmtDISvcExtAttrStru));
                    Debug.Assert(Marshal.SizeOf(typeof(Structs.BtSdkRmtDISvcExtAttrStru))
                                 == Marshal.SizeOf(deviceInfo), "SizeOf x2");
                    Debug.Assert(deviceInfo.size == Marshal.SizeOf(typeof(Structs.BtSdkRmtDISvcExtAttrStru))
                                 + Structs.BtSdkRmtDISvcExtAttrStru.StackMiscountsPaddingSize,
                                 "Different sizes!  deviceInfo.size: " + deviceInfo.size + ", Marshal.SizeOf: " + Marshal.SizeOf(typeof(Structs.BtSdkRmtDISvcExtAttrStru)));
                    // TO-DO Device Info (PnP) record: Use "mask" field, it's undocumented, check for real life values
                    //Debug.Assert(deviceInfo.mask == 0, "Is mask field in BtSdkRmtDISvcExtAttrStru ever set!!!, is here:" + deviceInfo.mask);
                    Debug.WriteLine(string.Format(CultureInfo.InvariantCulture,
                                                  "PnP/DI.mask: {0:X}", deviceInfo.mask));
                    // Like above (PnP) we see mask==0 for the fields we handle
                    // here (six of).  So we check these values
                    // for zero and discard them if so!
                    var list = new List <ServiceAttribute>();
                    if (deviceInfo.spec_id != 0)
                    {
                        list.Add(
                            new ServiceAttribute(DeviceIdProfileAttributeId.SpecificationId,
                                                 new ServiceElement(ElementType.UInt16, deviceInfo.spec_id)));
                    }
                    if (deviceInfo.vendor_id != 0)
                    {
                        list.Add(
                            new ServiceAttribute(DeviceIdProfileAttributeId.VendorId,
                                                 new ServiceElement(ElementType.UInt16, deviceInfo.vendor_id)));
                    }
                    if (deviceInfo.product_id != 0)
                    {
                        list.Add(
                            new ServiceAttribute(DeviceIdProfileAttributeId.ProductId,
                                                 new ServiceElement(ElementType.UInt16, deviceInfo.product_id)));
                    }
                    if (deviceInfo.version != 0)
                    {
                        list.Add(
                            new ServiceAttribute(DeviceIdProfileAttributeId.Version,
                                                 new ServiceElement(ElementType.UInt16, deviceInfo.version)));
                    }
                    if (true /* Zero means False here!! */)
                    {
                        list.Add(
                            new ServiceAttribute(DeviceIdProfileAttributeId.PrimaryRecord,
                                                 new ServiceElement(ElementType.Boolean, deviceInfo.primary_record)));
                    }
                    if (deviceInfo.vendor_id_source != 0)
                    {
                        list.Add(
                            new ServiceAttribute(DeviceIdProfileAttributeId.VendorIdSource,
                                                 new ServiceElement(ElementType.UInt16, deviceInfo.vendor_id_source)));
                    }
                    // TO-DO URLs...
                    extras.AddRange(list);
                }
                else
                {
                    // On testing we see this never working!  For one device
                    // with an ImagingResponder record the size of 0x18 and
                    // not 0x8 as per definition, and the port value is wrong.
                    // And for its PhonebookAccessPse record the size is
                    // correctly 0x8, but again the port value is wrong!
                    //
                    var sppInfo = (Structs.BtSdkRmtSPPSvcExtAttrStru)Marshal.PtrToStructure(
                        attrs.ext_attributes, typeof(Structs.BtSdkRmtSPPSvcExtAttrStru));
                    Debug.Assert(sppInfo.size == Marshal.SizeOf(typeof(Structs.BtSdkRmtSPPSvcExtAttrStru)),
                                 "Different sizes!");
                    port = sppInfo.server_channel;
                }
                api.Btsdk_FreeMemory(attrs.ext_attributes);
            }//if (attrs.ext_attributes != NULL)
            // Use a different API to try and get the RFCOMM port number as
            // the previous API is quite rubbish at doing that!!
            var svcB   = new Structs.BtSdkAppExtSPPAttrStru(sc);
            var retSpp = api.Btsdk_SearchAppExtSPPService(attrs.dev_hdl, ref svcB);

            if (retSpp == BtSdkError.NO_SERVICE)   // error
            {
            }
            else if (retSpp != BtSdkError.OK)     // error
            {
                Debug.WriteLine("GetSvcRcds Btsdk_SearchAppExtSPPService ret: "
                                + BluesoleilUtils.BtSdkErrorToString(retSpp));
            }
            else     // success
            {
                if (svcB.rf_svr_chnl != 0)
                {
                    byte newPort = svcB.rf_svr_chnl;
                    if (port.HasValue)
                    {
                        Debug.Assert(port.Value == newPort, "port: " + port.Value + ", newPort: " + newPort);
                    }
                    else
                    {
                        port = newPort;
                    }
                }
                if (svcB.sdp_record_handle != 0)
                {
                    bldr.AddCustomAttribute(new ServiceAttribute(
                                                UniversalAttributeId.ServiceRecordHandle,
                                                ServiceElement.CreateNumericalServiceElement(ElementType.UInt32, svcB.sdp_record_handle)));
                }
#if DEBUG
                Debug.Assert(svcB.service_class_128 == sc, "svcSpp.service_class_128: " + svcB.service_class_128 + ", sc: " + sc);
                var snSpp = BluesoleilUtils.FromNameString(svcB.svc_name, StackConsts.BTSDK_SERVICENAME_MAXLENGTH);
                if (snSpp == null)
                {
                    Debug.Assert(name == null || name.Length == 0, "svcSpp.svc_name: null" + ", name: " + name);
                }
                else if (snSpp.Length == 1)
                {
                    // SearchAppExtSPPService doesn't handle Unicode
                    // but Btsdk_BrowseRemoteServicesEx etc does.
                    Debug.Assert(snSpp[0] == name[0], "svcSpp.svc_name: " + snSpp + ", name: " + name);
                }
                else
                {
                    Debug.Assert(snSpp == name, "svcSpp.svc_name: " + snSpp + ", bldr.ServiceName: " + name);
                }
#endif
            }
            //
            if (port.HasValue)
            {
            }
            else
            {
                bldr.ProtocolType = BluetoothProtocolDescriptorType.None;
            }
            if (extras.Count != 0)
            {
                bldr.AddCustomAttributes(extras);
            }
            //
            const ServiceAttributeId FakeDescr = (ServiceAttributeId)(-1);
            bldr.AddCustomAttribute(new ServiceAttribute(FakeDescr,
                                                         new ServiceElement(ElementType.TextString,
                                                                            "<partial BlueSoleil decode>")));
            ServiceRecord sr = bldr.ServiceRecord;
            if (port.HasValue)
            {
                Debug.Assert(bldr.ProtocolType == BluetoothProtocolDescriptorType.Rfcomm,
                             "type=" + bldr.ProtocolType);
                ServiceRecordHelper.SetRfcommChannelNumber(sr, port.Value);
            }
            else
            {
                bldr.ProtocolType = BluetoothProtocolDescriptorType.None;
            }
            return(sr);
        }
Example #24
0
        /// <inheritdoc />
        internal override void StartListening(EndPoint desiredLocalListenEndPoint, bool useRandomPortFailOver)
        {
            if (IsListening)
            {
                throw new InvalidOperationException("Attempted to call StartListening when already listening.");
            }
            if (!(desiredLocalListenEndPoint is BluetoothEndPoint))
            {
                throw new ArgumentException("Bluetooth connections can only be made from a local BluetoothEndPoint", "desiredLocalListenIPEndPoint");
            }

            try
            {
                ServiceRecordBuilder bldr = new ServiceRecordBuilder();
                bldr.AddServiceClass((desiredLocalListenEndPoint as BluetoothEndPoint).Service);
                if (IsDiscoverable)
                {
                    bldr.AddCustomAttribute(new ServiceAttribute(NetworkCommsBTAttributeId.NetworkCommsEndPoint, ServiceElement.CreateNumericalServiceElement(ElementType.UInt8, 1)));
                }

                listenerInstance = new BluetoothListener(desiredLocalListenEndPoint as BluetoothEndPoint, bldr.ServiceRecord);

                listenerInstance.Start();
                listenerInstance.BeginAcceptBluetoothClient(BluetoothConnectionReceivedAsync, null);
            }
            catch (SocketException)
            {
                //If the port we wanted is not available
                if (useRandomPortFailOver)
                {
                    try
                    {
                        Guid service = Guid.NewGuid();

                        ServiceRecordBuilder bldr = new ServiceRecordBuilder();
                        bldr.AddServiceClass(service);
                        if (IsDiscoverable)
                        {
                            bldr.AddCustomAttribute(new ServiceAttribute(NetworkCommsBTAttributeId.NetworkCommsEndPoint, ServiceElement.CreateNumericalServiceElement(ElementType.UInt8, 1)));
                        }

                        listenerInstance = new BluetoothListener(new BluetoothEndPoint((desiredLocalListenEndPoint as BluetoothEndPoint).Address, service), bldr.ServiceRecord);
                        listenerInstance.Start();
                        listenerInstance.BeginAcceptBluetoothClient(BluetoothConnectionReceivedAsync, null);
                    }
                    catch (SocketException)
                    {
                        //If we get another socket exception this appears to be a bad IP. We will just ignore this IP
                        if (NetworkComms.LoggingEnabled)
                        {
                            NetworkComms.Logger.Error("It was not possible to open a random port on " + desiredLocalListenEndPoint + ". This endPoint may not support listening or possibly try again using a different port.");
                        }
                        throw new CommsSetupShutdownException("It was not possible to open a random port on " + desiredLocalListenEndPoint + ". This endPoint may not support listening or possibly try again using a different port.");
                    }
                }
                else
                {
                    if (NetworkComms.LoggingEnabled)
                    {
                        NetworkComms.Logger.Error("It was not possible to listen on " + desiredLocalListenEndPoint.ToString() + ". This endPoint may not support listening or possibly try again using a different port.");
                    }
                    throw new CommsSetupShutdownException("It was not possible to listen on " + desiredLocalListenEndPoint.ToString() + ". This endPoint may not support listening or possibly try again using a different port.");
                }
            }

            this.LocalListenEndPoint = desiredLocalListenEndPoint;

            this.IsListening = true;
        }
Example #25
0
        public void RecordA()
        {
            var stuff = BluetopiaTesting.InitMockery_SdpCreator();
            var bldr  = new ServiceRecordBuilder();

            bldr.AddServiceClass(BluetoothService.Wap);
            bldr.AddBluetoothProfileDescriptor(BluetoothService.WapClient, 0x1, 0x2);
            //
            Expect.AtLeastOnce.On(stuff.MockApi2).Method("SDP_Add_Attribute")
            .With(
                stuff.StackId, stuff.SrHandle,
                (ushort)UniversalAttributeId.ProtocolDescriptorList,
                //Is.Anything
                new Structs.SDP_Data_Element__Class_ElementArray(
                    StackConsts.SDP_Data_Element_Type.Sequence,
                    2,
                    new Structs.SDP_Data_Element__Class_ElementArray(
                        StackConsts.SDP_Data_Element_Type.Sequence,
                        1,
                        new Structs.SDP_Data_Element__Class_InlineByteArray(
                            StackConsts.SDP_Data_Element_Type.UUID_16,
                            2, new byte[] { 0x01, 0x00 }
                            )
                        ),
                    new Structs.SDP_Data_Element__Class_ElementArray(
                        StackConsts.SDP_Data_Element_Type.Sequence,
                        2,
                        new Structs.SDP_Data_Element__Class_InlineByteArray(
                            StackConsts.SDP_Data_Element_Type.UUID_16,
                            2, new byte[] { 0x00, 0x03 }
                            ),
                        new Structs.SDP_Data_Element__Class_InlineByteArray(
                            StackConsts.SDP_Data_Element_Type.UnsignedInteger1Byte,
                            1, new byte[] { 0 }
                            )
                        )
                    )
                )
            .Will(Return.Value(BluetopiaError.OK));
            Expect.AtLeastOnce.On(stuff.MockApi2).Method("SDP_Add_Attribute")
            .With(
                stuff.StackId, stuff.SrHandle,
                (ushort)UniversalAttributeId.BluetoothProfileDescriptorList,
                new Structs.SDP_Data_Element__Class_ElementArray(
                    StackConsts.SDP_Data_Element_Type.Sequence,
                    1,
                    new Structs.SDP_Data_Element__Class_ElementArray(
                        StackConsts.SDP_Data_Element_Type.Sequence,
                        2,
                        new Structs.SDP_Data_Element__Class_InlineByteArray(
                            StackConsts.SDP_Data_Element_Type.UUID_16,
                            2, new byte[] { 0x11, 0x14, }
                            ),
                        new Structs.SDP_Data_Element__Class_InlineByteArray(
                            StackConsts.SDP_Data_Element_Type.UnsignedInteger2Bytes,
                            2, new byte[] { 0x02, 0x01, }    //endian!
                            )
                        )
                    )
                )
            .Will(Return.Value(BluetopiaError.OK));
            //
            stuff.DutSdpCreator.CreateServiceRecord(bldr.ServiceRecord);
            //--
            VerifyDispose(stuff);
        }
        public ServiceRecord[] GetServiceRecords()
        {
            Debug.Assert(m_request.searchScope == SdpSearchScope.Anywhere, "unexpected searchScope: " + m_request.searchScope);
            int classInSCL, classAnywhere;

            if (m_records == null)
            {
                m_records = new List <ServiceRecord>();
                SdpDiscoveryRecordsBufferBase.SimpleInfo[] infoArr = GetSimpleInfo();
                foreach (SdpDiscoveryRecordsBufferBase.SimpleInfo info in infoArr)
                {
                    classInSCL = classAnywhere = 0;
                    //Utils.MiscUtils.Trace_WriteLine("fake int: {0}", info.fake);
                    ServiceRecordBuilder     bldr      = new ServiceRecordBuilder();
                    const ServiceAttributeId FakeDescr = (ServiceAttributeId)(-1);
                    bldr.AddCustomAttribute(new ServiceAttribute(FakeDescr,
                                                                 new ServiceElement(ElementType.TextString,
                                                                                    "<partial Widcomm decode>")));
                    //--
                    bldr.AddServiceClass(info.serviceUuid);
                    if (m_request.serviceGuid == info.serviceUuid)
                    {
                        ++classInSCL;
                    }
                    //--
                    if (info.serviceNameWchar != IntPtr.Zero)
                    {
                        string name = Marshal.PtrToStringUni(info.serviceNameWchar);
                        if (name.Length != 0)
                        {
                            bldr.ServiceName = name;
                        }
                    }
                    else if (info.serviceNameChar != IntPtr.Zero)
                    {
                        // PtrToStringAnsi is not supported on NETCF.  The field
                        // is not used by the native code there so that's ok.
#if NETCF
                        Debug.Fail("Don't expect 'serviceNameChar' on PPC.");
#else
                        string name = Marshal.PtrToStringAnsi(info.serviceNameChar);
                        if (name.Length != 0)
                        {
                            bldr.ServiceName = name;
                        }
#endif
                    }
                    //--
                    if (info.scn == -1)
                    {
                        bldr.ProtocolType = BluetoothProtocolDescriptorType.None;
                    }
                    //--
                    switch (bldr.ProtocolType)
                    {
                    case BluetoothProtocolDescriptorType.GeneralObex:
                        Debug.Fail("GEOP untested");
                        if (m_request.serviceGuid == BluetoothService.ObexProtocol)
                        {
                            ++classAnywhere;
                        }
                        goto case BluetoothProtocolDescriptorType.Rfcomm;

                    case BluetoothProtocolDescriptorType.Rfcomm:
                        if (m_request.serviceGuid == BluetoothService.RFCommProtocol)
                        {
                            ++classAnywhere;
                        }
                        if (m_request.serviceGuid == BluetoothService.L2CapProtocol)
                        {
                            ++classAnywhere;
                        }
                        break;

                    case BluetoothProtocolDescriptorType.None:
                        // We'd better assume L2CAP in the PDL or we'd skip too many
                        // e.g. the SDP server record!
                        if (m_request.serviceGuid == BluetoothService.L2CapProtocol)
                        {
                            ++classAnywhere;
                        }
                        break;
                    }
                    //--
                    ServiceRecord sr = bldr.ServiceRecord;
                    if (info.scn != -1)
                    {
                        Debug.Assert(bldr.ProtocolType == BluetoothProtocolDescriptorType.Rfcomm,
                                     "type=" + bldr.ProtocolType);
                        ServiceRecordHelper.SetRfcommChannelNumber(sr, checked ((byte)info.scn));
                    }
                    if (classInSCL > 0 || classAnywhere > 0)
                    {
                        Utils.MiscUtils.Trace_WriteLine("Adding record");
                        m_records.Add(sr);
                    }
                    else     // COVERAGE
                    {
                        Utils.MiscUtils.Trace_WriteLine("Skipping record");
                    }
                }
            }
            return(m_records.ToArray());
        }
Example #27
0
        void DoStart()
        {
            if (handle != IntPtr.Zero)
            {
                throw new InvalidOperationException();
            }

            endPoint = new BluetoothEndPoint(BluetoothAddress.None, serviceUuid);

            if (NativeMethods.IsRunningOnMono())
            {
                socket = new Win32Socket();
                ((Win32Socket)socket).Bind(endPoint);
                Debug.WriteLine(socket.IsBound);
                ((Win32Socket)socket).Listen(1);
                // get endpoint with channel
                endPoint = ((Win32Socket)socket).LocalEndPoint as BluetoothEndPoint;
            }
            else
            {
                socket = new Socket(BluetoothClient.AddressFamilyBluetooth, SocketType.Stream, BluetoothProtocolType.RFComm);
                socket.Bind(endPoint);
                Debug.WriteLine(socket.IsBound);
                socket.Listen(1);
                // get endpoint with channel
                endPoint = socket.LocalEndPoint as BluetoothEndPoint;
            }

            var socketAddressBytes = endPoint.Serialize().ToByteArray();


            WSAQUERYSET qs = new WSAQUERYSET();

            qs.dwSize      = Marshal.SizeOf(qs);
            qs.dwNameSpace = NativeMethods.NS_BTH;
            //var uuidh = GCHandle.Alloc(serviceUuid, GCHandleType.Pinned);
            //qs.lpServiceClassId = uuidh.AddrOfPinnedObject();
            //qs.lpszServiceInstanceName = ServiceName;

            qs.dwNumberOfCsAddrs = 1;

            var csa = new CSADDR_INFO
            {
                lpLocalSockaddr      = Marshal.AllocHGlobal(NativeMethods.BluetoothSocketAddressLength),
                iLocalSockaddrLength = NativeMethods.BluetoothSocketAddressLength,
                iSocketType          = SocketType.Stream,
                iProtocol            = NativeMethods.PROTOCOL_RFCOMM
            };

            Marshal.Copy(socketAddressBytes, 0, csa.lpLocalSockaddr, NativeMethods.BluetoothSocketAddressLength);

            IntPtr pcsa = Marshal.AllocHGlobal(Marshal.SizeOf(csa));

            Marshal.StructureToPtr(csa, pcsa, false);
            qs.lpcsaBuffer = pcsa;

            var blob     = new BLOB();
            var blobData = new BTH_SET_SERVICE();
            int sdpVer   = 1;

            blobData.pSdpVersion = Marshal.AllocHGlobal(IntPtr.Size);
            Marshal.WriteInt32(blobData.pSdpVersion, sdpVer);
            blobData.pRecordHandle = Marshal.AllocHGlobal(IntPtr.Size);
            Marshal.WriteIntPtr(blobData.pRecordHandle, 0, IntPtr.Zero);
            blobData.fCodService = ServiceClass;

            if (ServiceRecord == null)
            {
                ServiceRecordBuilder builder = new ServiceRecordBuilder();
                builder.AddServiceClass(serviceUuid);
                builder.ProtocolType = BluetoothProtocolDescriptorType.Rfcomm;

                if (!string.IsNullOrEmpty(ServiceName))
                {
                    builder.ServiceName = ServiceName;
                }

                ServiceRecord = builder.ServiceRecord;
            }
            ServiceRecordHelper.SetRfcommChannelNumber(ServiceRecord, socketAddressBytes[26]);

            byte[] rawBytes = ServiceRecord.ToByteArray();
            blobData.ulRecordLength = (uint)rawBytes.Length;
            int structSize = (2 * IntPtr.Size) + (7 * 4);

            blob.size     = structSize + rawBytes.Length;
            blob.blobData = Marshal.AllocHGlobal(blob.size);
            Marshal.StructureToPtr(blobData, blob.blobData, false);
            Marshal.Copy(rawBytes, 0, IntPtr.Add(blob.blobData, structSize), rawBytes.Length);

            var blobh = GCHandle.Alloc(blob, GCHandleType.Pinned);

            qs.lpBlob = blobh.AddrOfPinnedObject();

            try
            {
                int result = NativeMethods.WSASetService(ref qs, WSAESETSERVICEOP.RNRSERVICE_REGISTER, 0);

                var newstruct = Marshal.PtrToStructure <BTH_SET_SERVICE>(blob.blobData);
                handle = Marshal.ReadIntPtr(newstruct.pRecordHandle);
                if (result == -1)
                {
                    int werr = Marshal.GetLastWin32Error();
                    throw new SocketException(werr);
                }
            }
            finally
            {
                Marshal.FreeHGlobal(csa.lpLocalSockaddr);
                Marshal.FreeHGlobal(qs.lpcsaBuffer);

                Marshal.FreeHGlobal(blobData.pSdpVersion);
                Marshal.FreeHGlobal(blobData.pRecordHandle);

                Marshal.FreeHGlobal(blob.blobData);
                blobh.Free();
            }
        }