14 #error TnbCeBluetoothCom.h is only supported on Windows CE platforms.
48 : m_hVirCom(NULL), m_spdHandle(0), m_btAddr(0), m_hServerCom(INVALID_HANDLE_VALUE)
75 m_hVirCom = ms_Create(m_port, m_btAddr,
false, 0, channel, service);
76 return m_hVirCom != NULL;
94 m_hVirCom = ms_Create(portNo, m_btAddr,
true, portFlag, 0);
95 if ( m_hVirCom != NULL )
97 DWORD desiredAccess = GENERIC_READ | GENERIC_WRITE;
98 DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
99 DWORD flag = FILE_ATTRIBUTE_NORMAL;
100 m_hServerCom = ::CreateFile(portNo.
GetPortName(), desiredAccess, shareMode, NULL, OPEN_EXISTING, flag, NULL);
101 m_spdHandle = ms_RegisterSDP(m_hServerCom);
103 return m_hVirCom != NULL;
113 return m_hVirCom != NULL;
142 BOOL r = ::DeviceIoControl(m_hServerCom, IOCTL_BLUETOOTH_GET_PEER_DEVICE, NULL, 0, &_btAddr,
sizeof(_btAddr), &dwSizeOut, NULL);
153 if ( m_hServerCom != INVALID_HANDLE_VALUE )
155 ::CloseHandle(m_hServerCom);
156 m_hServerCom = INVALID_HANDLE_VALUE;
158 if ( m_spdHandle != 0 )
160 ms_DeRegisterSDP(m_spdHandle);
163 if ( m_hVirCom != NULL )
165 ::DeregisterDevice(m_hVirCom);
172 static HANDLE ms_Create(
CRs232c::CPort portNo,
const BT_ADDR& btAddr,
bool isServer,
int portFlag,
int channel
173 , GUID service = SerialPortServiceClass_UUID)
178 DEVMGR_DEVICE_INFORMATION ddi = { 0 };
179 ddi.dwSize =
sizeof(DEVMGR_DEVICE_INFORMATION);
180 HANDLE hh = ::FindFirstDevice(DeviceSearchByDeviceName
181 ,
CStr::Fmt(_T(
"%s%d"), prefix, port), &ddi);
182 if ( hh != INVALID_HANDLE_VALUE )
184 TTRACE1(
"found hDevice = 0x%08X\n", ddi.hDevice);
185 TTRACE1(
" hParent = 0x%08X\n", ddi.hParentDevice);
186 TTRACE1(
" szDeviceName = %s\n", ddi.szDeviceName);
187 TTRACE1(
" szLegacyName = %s\n", ddi.szLegacyName);
188 TTRACE1(
" szBusName = %s\n", ddi.szBusName);
189 TTRACE1(
" szDeviceKey = %s\n", ddi.szDeviceKey);
190 if ( ddi.szDeviceKey[0] == 0 )
192 if ( ! ::DeregisterDevice(ddi.hDevice) )
194 _GetLastError(
"DeregisterDevice");
200 PORTEMUPortParams pp = { 0 };
202 pp.flocal = isServer;
203 pp.uiportflags = portFlag;
206 pp.channel = RFCOMM_CHANNEL_MULTIPLE;
212 pp.channel = channel;
217 pp.uuidService = service;
220 DWORD info =
reinterpret_cast<DWORD
>(&pp);
221 HANDLE h = ::RegisterDevice(prefix, port, _T(
"btd.dll"), info);
224 _GetLastError(
"RegisterDevice");
230 static ULONG ms_RegisterSDP(HANDLE handle)
233 const int SDP_RECORD_SIZE = 0x0000004d;
234 const int SDP_CHANNEL_OFFSET = 26;
235 static const BYTE rgbSdpRecordSPP[SDP_RECORD_SIZE] =
237 0x35, 0x4b, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19,
238 0x11, 0x01, 0x09, 0x00, 0x04, 0x35, 0x0c, 0x35,
239 0x03, 0x19, 0x01, 0x00, 0x35, 0x05, 0x19, 0x00,
242 0x09, 0x00, 0x06, 0x35, 0x09,
243 0x09, 0x65, 0x6e, 0x09, 0x00, 0x6a, 0x09, 0x01,
244 0x00, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06,
245 0x19, 0x11, 0x01, 0x09, 0x01, 0x00, 0x09, 0x01,
246 0x00, 0x25, 0x12,
'S',
'h',
'a',
'r',
'e',
247 'd',
' ',
'S',
'e',
'r',
'i',
'a',
'l',
248 ' ',
'P',
'o',
'r',
't'
250 ASSERT( rgbSdpRecordSPP[SDP_CHANNEL_OFFSET - 1] == 0x08 );
251 ASSERT( rgbSdpRecordSPP[SDP_CHANNEL_OFFSET ] == 0x01 );
252 ASSERT( rgbSdpRecordSPP[SDP_CHANNEL_OFFSET + 1] == 0x09 );
253 ULONG recordHandle = 0;
256 if ( ! ::DeviceIoControl(handle, IOCTL_BLUETOOTH_GET_RFCOMM_CHANNEL, NULL, 0, &port,
sizeof(port), &dwSizeOut, NULL))
258 TRACE1(
"BLUETOOTH GATEWAY:: Failed to retrieve port server channel, error = %d\n", GetLastError());
264 unsigned char uca[SDP_RECORD_SIZE];
266 ULONG ulSdpVersion = BTH_SDP_VERSION;
267 bigBlob.b.pRecordHandle = &recordHandle;
268 bigBlob.b.pSdpVersion = &ulSdpVersion;
269 bigBlob.b.fSecurity = 0;
270 bigBlob.b.fOptions = 0;
271 bigBlob.b.ulRecordLength = SDP_RECORD_SIZE;
272 memcpy(bigBlob.b.pRecord, rgbSdpRecordSPP, SDP_RECORD_SIZE);
273 bigBlob.b.pRecord[SDP_CHANNEL_OFFSET] =
static_cast<BYTE
>(port);
275 blob.cbSize =
sizeof(BTHNS_SETBLOB) + SDP_RECORD_SIZE - 1;
276 blob.pBlobData =
reinterpret_cast<BYTE*
>(&bigBlob);
277 WSAQUERYSET service = { 0 };
278 service.dwSize =
sizeof(service);
279 service.lpBlob = &blob;
280 service.dwNameSpace = NS_BTH;
281 int r = api.BthNsSetService(&service, RNRSERVICE_REGISTER, 0);
282 if ( r != ERROR_SUCCESS )
284 TRACE1(
"BLUETOOTH GATEWAY:: BthNsSetService fails with status %d\n", r);
287 TRACE2(
"BLUETOOTH GATEWAY:: Created SDP record 0x%08x, channel %d\n", recordHandle, port);
292 static void ms_DeRegisterSDP(ULONG recordHandle)
295 ULONG ulSdpVersion = BTH_SDP_VERSION;
296 BTHNS_SETBLOB delBlob = { 0 };
297 delBlob.pRecordHandle = &recordHandle;
298 delBlob.pSdpVersion = &ulSdpVersion;
300 blob.cbSize =
sizeof(BTHNS_SETBLOB);
301 blob.pBlobData = (PBYTE) &delBlob;
302 WSAQUERYSET service = { 0 };
303 service.dwSize =
sizeof(service);
304 service.lpBlob = &blob;
305 service.dwNameSpace = NS_BTH;
306 int r = api.BthNsSetService (&service, RNRSERVICE_DELETE, 0);
307 TRACE2(
"BLUETOOTH GATEWAY: removed SDP record 0x%08x (%d)\n", recordHandle, r);
373 m_portFlag = portFlag;
410 _super::GetParameter(comPort, dwBaudRate, bByteSize, eParity, eStopBits);
423 return _super::Open();
473 if ( list[j].spec == SDP_ST_UUID16 && list[j].uuid16 == 3 )
475 if ( j + 1 < list.
GetSize() && list[j + 1].spec == SDP_ST_UINT8 )
477 pp.
channel = list[j + 1].uint8;
485 if ( l2[j].type == SDP_TYPE_STRING )
#define loop(VAR, CNT)
loop構文.
Bluetooth(CE) クライアントシリアルアクセスクラス.
void SetServerParameter(int portFlag=RFCOMM_PORT_FLAGS_AUTHENTICATE|RFCOMM_PORT_FLAGS_ENCRYPT)
[設定] サーバ設定.
virtual bool Open(void)
[処理] オープン.
~CBluetoothCom(void)
デストラクタ
bool GetPeerAddress(BT_ADDR &_btAddr) const
[取得] 相手アドレス取得.
CVectorT< TChannel > CChannels
チャネル情報リスト型
CBluetoothCom(void)
コンストラクタ
virtual void Close(void)
[処理] クローズ.
void SetClientParameter(const BT_ADDR &btAddr, int channel=-1)
[設定] クライアント設定.
static CChannels FindChannels(const BT_ADDR &btAddr, WORD classId=SerialPortServiceClassID_UUID16)
[取得] RFCOMMチャネル検索.
bool HasService(CWorkMem &_raw, WORD uuid16) const
[確認] サービス確認.
bool IsValid(void) const
[確認] 有効確認.
const TValuesList & GetAttributeData(WORD attributeId) const
[取得] 属性IDデータ取得.
bool SearchRecord(INDEX record)
[検索] レコード検索.
size_t GetRecordCount(void) const
[取得] 保持レコード数.
Bluetooth(CE) 仮想COMポート管理クラス.
CBluetoothVirtualComPort(void)
コンストラクタ
bool CreateServerPort(CRs232c::CPort portNo, int portFlag=RFCOMM_PORT_FLAGS_AUTHENTICATE|RFCOMM_PORT_FLAGS_ENCRYPT)
[作成] サーバポート作成.
bool IsValid(void) const
[確認] 有効確認.
const CRs232c::CPort & GetCreatedPort(void) const
[取得] 作成ポート名取得.
bool GetPeerAddress(BT_ADDR &_btAddr) const
[取得] 相手アドレス取得.
void Delete(void)
[破棄] ポート破棄.
bool CreateClientPort(CRs232c::CPort portNo, const BT_ADDR &btAddr, int channel=-1, GUID service=SerialPortServiceClass_UUID)
[作成] クライアントポート作成.
~CBluetoothVirtualComPort(void)
デストラクタ
int GetNumber(void) const
[取得] 数値取得.
CStr GetPrefix(void) const
[取得] プリフィックス取得
const CStr & GetPortName(void) const
[取得] ポート名取得.
static CStrT Fmt(const TCHAR *lpszFormat,...)
[作成] 書式付き文字列作成
virtual size_t GetSize(void) const
[取得] サイズ取得
virtual INDEX Add(const TYP &t)
[追加] 要素一つ追加.
ULONGLONG BT_ADDR
Bluetooth(CE) アドレス.
Bluetooth(CE) クライアントシリアルアクセスのチャネル情報.