private static void ExpectComCleanUp(IBluesoleilApi api, UInt32 comSerialNum, byte comPort8) { Expect.Once.On(api).Method("Btsdk_DeinitCommObj") .With(comPort8) .Will(Return.Value(BtSdkError.OK)); Expect.Once.On(api).Method("Btsdk_PlugOutVComm") .With(comSerialNum, StackConsts.COMM_SET.Record); }
void ExpectConnectSetup(IBluesoleilApi api) { Expect.Exactly(4).On(api).Method("Btsdk_RegisterCallback4ThirdParty") .WithAnyArguments() .Will(Return.Value(BtSdkError.OK)); Expect.Once.On(api).Method("Btsdk_RegisterGetStatusInfoCB4ThirdParty") .WithAnyArguments() .Will(Return.Value(BtSdkError.OK)); Expect.Once.On(api).Method("Btsdk_SetStatusInfoFlag") .WithAnyArguments() .Will(Return.Value(BtSdkError.OK)); }
//-------- private static void Create_BtCli(out Mockery mkry, out IBluesoleilApi api, out BluesoleilFactory fcty, out IBluetoothClient cli) { mkry = new Mockery(); api = mkry.NewMock <IBluesoleilApi>(); // Expect.Exactly(2).On(api).Method("Btsdk_IsSDKInitialized") .WithAnyArguments() .Will(Return.Value(false)); Expect.Once.On(api).Method("Btsdk_IsServerConnected") .WithAnyArguments() .Will(Return.Value(false)); // Expect.AtLeastOnce.On(api).Method("Btsdk_Init") .WithAnyArguments() .Will(Return.Value(BtSdkError.OK)); // //Expect.AtLeastOnce.On(api).Method("Btsdk_IsBluetoothReady") // .WithAnyArguments() // .Will(Return.Value(true)); Expect.Once.On(api).Method("Btsdk_IsServerConnected") .WithAnyArguments() .Will(Return.Value(true)); Expect.Once.On(api).Method("Btsdk_IsBluetoothHardwareExisted") .WithAnyArguments() .Will(Return.Value(true)); Expect.AtLeastOnce.On(api).Method("Btsdk_IsSDKInitialized") .WithAnyArguments() .Will(Return.Value(true)); Expect.AtLeastOnce.On(api).Method("Btsdk_IsServerConnected") .WithAnyArguments() .Will(Return.Value(true)); // Expect.Once.On(api).Method("Btsdk_GetLocalDeviceAddress") .WithAnyArguments() .Will(Return.Value(BtSdkError.OK)); Expect.Once.On(api).Method("Btsdk_GetLocalName") .WithAnyArguments() .Will(Return.Value(BtSdkError.OK)); Expect.Once.On(api).Method("Btsdk_GetLocalDeviceClass") .With(Is.Out) .Will(Return.Value(BtSdkError.OK), new NMock2.Actions.SetIndexedParameterAction(0, (uint)0)); Expect.Once.On(api).Method("Btsdk_GetLocalLMPInfo") .WithAnyArguments() .Will(Return.Value(BtSdkError.OK)); // fcty = new BluesoleilFactory(api); mkry.VerifyAllExpectationsHaveBeenMet(); cli = fcty.DoGetBluetoothClient(); mkry.VerifyAllExpectationsHaveBeenMet(); }
private void ExpectGetDeviceInfo(IBluesoleilApi api, UInt32 hDev) { Expect.Once.On(api).Method("Btsdk_GetRemoteDeviceHandle") .With(ArrayMatcher.MatchContentExactly(AddrABytes)) .Will(Return.Value(hDev)); Expect.Once.On(api).Method("Btsdk_GetRemoteDeviceAddress") .WithAnyArguments() .Will(Return.Value(BtSdkError.OK), FillArrayIndexedParameterAction.Fill(1, AddrABytes, true) ); Expect.Once.On(api).Method("Btsdk_GetRemoteDeviceClass") .WithAnyArguments() .Will(Return.Value(BtSdkError.NOTFOUND), //??what code new NMock2.Actions.SetIndexedParameterAction(1, (uint)0) ); }
internal BluesoleilFactory(IBluesoleilApi api) { if (api == null) { throw new ArgumentNullException("api"); } _api = api; _records = new Records(api); Debug.Assert(!api.Btsdk_IsSDKInitialized(), "IsSDKInitialized unexectedly true!"); SdkInit(); if (!Api.Btsdk_IsBluetoothHardwareExisted()) { throw new PlatformNotSupportedException("BlueSoleil Bluetooth stack not supported (HardwareExisted)."); } _inquiryHandler = new BluesoleilInquiry(this); _sec = new BluesoleilSecurity(this); GetAllRadios(); }
private void ExpectNewWay(bool doMockSerialPort, out Mockery mkry, out IBluesoleilApi api, out BluesoleilFactory fcty, out IBluetoothClient cli, out UInt32 comSerialNum, out byte comPort8) { Create_BtCli(out mkry, out api, out fcty, out cli); if (doMockSerialPort) { ((BluesoleilClient)cli).CreateSerialPortMethod = delegate() { //var sp = mkry.NewMock<ISerialPortWrapper>(); var sp = new InTheHand.Net.Tests.BlueSoleil.TestSerialPortWrapper(); return(sp); }; } // ExpectConnectSetup(api); // UInt32 hDev = 0x123; ExpectGetDeviceInfo(api, hDev); //-- comSerialNum = 0x1000; UInt32 comPort = 250; comPort8 = checked ((byte)comPort); UInt32 hConn = 0x1000000; // Expect.Once.On(api).Method("Btsdk_GetASerialNum") .WithNoArguments() .Will(Return.Value(comSerialNum)); Expect.Once.On(api).Method("Btsdk_PlugInVComm") .WithAnyArguments() .Will(Return.Value(true), new NMock2.Actions.SetIndexedParameterAction(1, comPort)); Expect.Once.On(api).Method("Btsdk_InitCommObj") .WithAnyArguments() .Will(Return.Value(BtSdkError.OK)); Expect.Once.On(api).Method("Btsdk_ConnectEx") .WithAnyArguments() .Will(Return.Value(BtSdkError.OK), new NMock2.Actions.SetIndexedParameterAction(3, hConn) ); }
void HackVerifyShutdown(Mockery mkry, IBluesoleilApi api, BluesoleilFactory fcty) { GC.KeepAlive(fcty); Expect.Once.On(api).Method("Btsdk_Disconnect") .WithAnyArguments() .Will(Return.Value(BtSdkError.OK)); #if true Expect.AtLeast(0).On(api).Method("Btsdk_Done") .WithAnyArguments() .Will(Return.Value(BtSdkError.OK)); // #else fcty = null; cli = null; Expect.Once.On(api).Method("Btsdk_Done") .WithAnyArguments() .Will(Return.Value(BtSdkError.OK)); WaitForFinalizers(); WaitForFinalizers(); mkry.VerifyAllExpectationsHaveBeenMet(); #endif }
public ServiceRecord[] GetServiceRecords(Guid service) { BtSdkError ret; var search = new Structs.BtSdkSDPSearchPatternStru[1]; search[0] = new Structs.BtSdkSDPSearchPatternStru(service); UInt32[] recordHandles = new uint[MaxServiceRecordsLookup]; int num = recordHandles.Length; // Fetch the matching records (the handles of). int numSearch = search.Length; ret = _factory.Api.Btsdk_BrowseRemoteServicesEx(_hDev, search, numSearch, recordHandles, ref num); if (ret == BtSdkError.NO_SERVICE) // None { return(new ServiceRecord[0]); } BluesoleilUtils.CheckAndThrow(ret, "Btsdk_BrowseRemoteServicesEx"); // Fetch the records' attributes and convert to our format. var results = new List <ServiceRecord>(); for (int i = 0; i < num; ++i) { var hRcd = recordHandles[i]; var attrs = new Structs.BtSdkRemoteServiceAttrStru( StackConsts.AttributeLookup.ServiceName | StackConsts.AttributeLookup.ExtAttributes); // Use 'Get' here and not 'Refresh' as the 'Browse' call above // should have retrieved the attributes. ret = _factory.Api.Btsdk_GetRemoteServiceAttributes(hRcd, ref attrs); BluesoleilUtils.CheckAndThrow(ret, "Btsdk_RefreshRemoteServiceAttributes"); Debug.Assert(attrs.dev_hdl == _hDev); // IBluesoleilApi hackApi = _factory.Api; ServiceRecord sr = CreateServiceRecord(ref attrs, hackApi); results.Add(sr); } return(results.ToArray()); }
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); }
internal Records(IBluesoleilApi api) { _api = api; }
private void SetupNoSearchAppExtSPPService(IBluesoleilApi hackApi) { Expect.Once.On(hackApi).Method("Btsdk_SearchAppExtSPPService") .WithAnyArguments().Will(Return.Value(BtSdkError.NO_SERVICE)); }