// 32feet.NET - Personal Area Networking for .NET
//
// InTheHand.Net.Bluetooth.Widcomm.WidcommBluetoothFactoryBase
//
// Copyright (c) 2008-2010 In The Hand Ltd, All rights reserved.
// This source code is licensed under the In The Hand Community License - see License.txt
using System;
using System.Collections.Generic;
using System.Text;
using InTheHand.Net.Sockets;
using System.Diagnostics;
using InTheHand.Net.Bluetooth.Factory;
namespace InTheHand.Net.Bluetooth.Widcomm{
abstract class WidcommBluetoothFactoryBase : BluetoothFactory
{
internal abstract WidcommBtInterface GetWidcommBtInterface();
internal abstract WidcommRfcommStream GetWidcommRfcommStream();
internal abstract WidcommRfcommStream GetWidcommRfcommStreamWithoutRfcommIf();
internal abstract IRfcommPort GetWidcommRfcommPort();
internal abstract IRfCommIf GetWidcommRfCommIf();
internal abstract ISdpService GetWidcommSdpService();
//
#if WIDCOMM_SINGLE_THREADING || WinXP
internal abstract WidcommPortSingleThreader GetSingleThreader();
#endif
}
sealed class WidcommBluetoothFactory : WidcommBluetoothFactoryBase
{
static IBtIf s_btIf;
static WidcommBtInterface s_btInterface;
static WidcommBluetoothSecurity m_btSecurity;
#if WIDCOMM_SINGLE_THREADING || WinXP
static WidcommPortSingleThreader _st;
#endif
public WidcommBluetoothFactory()
{
lock (typeof(WidcommBluetoothFactory)) {
// CBtIf: MUST ONLY be ONE of these, and must be FIRST created!
// "Only one object of this class should be instantiated by the application."
// "This class must be instantiated before any other DK classes are used"
if (s_btIf == null) {
IBtIf btIf = new WidcommBtIf();
#if WIDCOMM_SINGLE_THREADING || WinXP
Debug.Assert(_st == null);
if (_st == null) _st = new WidcommPortSingleThreader();
Debug.Assert(GetSingleThreader() != null);
if (GetSingleThreader() != null) {
btIf = new WidcommStBtIf(this, btIf);
WidcommUtils.Trace_WriteLine("IBtIf using WidcommStBtIf.");
}
#endif
Debug.Assert(s_btInterface == null);
WidcommBtInterface btInterface = new WidcommBtInterface(btIf, this);
// Don't set these until we're sure that initialisation has
// all completed successfully.
s_btIf = btIf;
s_btInterface = btInterface;
} else {
Debug.Assert(s_btInterface != null, "One set but not the other!");
}
}
// Check radio is connected.
this.GetPrimaryRadio();
}
//-----
protected override void Dispose(bool disposing)
{
IDisposable iface;
IDisposable st;
lock (typeof(WidcommBluetoothFactory)) {
iface = (IDisposable)s_btInterface;
s_btIf = null;
s_btInterface = null;
m_btSecurity = null;
#if WIDCOMM_SINGLE_THREADING || WinXP
st = _st;
_st = null;
#else
st = null;
#endif
}//lock
if (iface != null)
iface.Dispose();
if (st != null) // Must NOT dispose this first!
st.Dispose();
}
//-----
#if WIDCOMM_SINGLE_THREADING || WinXP
internal override WidcommPortSingleThreader GetSingleThreader()
{
return _st;
}
#endif
//-----
protected override IBluetoothClient GetBluetoothClient()
{
return new WidcommBluetoothClient(this);
}
protected override IBluetoothClient GetBluetoothClient(System.Net.Sockets.Socket acceptedSocket)
{
throw new NotSupportedException("Cannot create a BluetoothClient from a Socket on the Widcomm stack.");
}
protected override IBluetoothClient GetBluetoothClient(BluetoothEndPoint localEP)
{
return new WidcommBluetoothClient(localEP, this);
}
//----------------
protected override IBluetoothListener GetBluetoothListener()
{
return new WidcommBluetoothListener(this);
}
//----------------
internal override WidcommBtInterface GetWidcommBtInterface()
{
return s_btInterface;
}
internal override WidcommRfcommStream GetWidcommRfcommStream()
{
return new WidcommRfcommStream(GetWidcommRfcommPort(), GetWidcommRfCommIf(), this);
}
internal override WidcommRfcommStream GetWidcommRfcommStreamWithoutRfcommIf()
{
return new WidcommRfcommStream(GetWidcommRfcommPort(), null, this);
}
internal override IRfcommPort GetWidcommRfcommPort()
{
// Handling of single threadedness is done within WidcommRfcommStream.
return new WidcommRfcommPort();
}
internal override IRfCommIf GetWidcommRfCommIf()
{
IRfCommIf inst = new WidcommRfCommIf();
#if WIDCOMM_SINGLE_THREADING || WinXP
if (GetSingleThreader() != null) {
inst = new WidcommStRfCommIf(this, inst);
WidcommUtils.Trace_WriteLine("IRfCommIf using WidcommStRfCommIf.");
}
#endif
return inst;
}
protected override IBluetoothDeviceInfo GetBluetoothDeviceInfo(BluetoothAddress address)
{
return WidcommBluetoothDeviceInfo.CreateFromGivenAddress(address, this);
}
//----------------
protected override IBluetoothRadio GetPrimaryRadio()
{
return new WidcommBluetoothRadio(this);
}
protected override IBluetoothRadio[] GetAllRadios()
{
// Widcomm supports only one radio.
return new IBluetoothRadio[] { GetPrimaryRadio() };
}
//----------------
internal override ISdpService GetWidcommSdpService()
{
return new SdpService();
}
//----------------
protected override IBluetoothSecurity GetBluetoothSecurity()
{
if (m_btSecurity == null) {
m_btSecurity = new WidcommBluetoothSecurity(this);
}
return m_btSecurity;
}
}
}
|