TNB Library
TnbCeBluetoothPan.h
[詳解]
1#pragma once
11#ifndef _WIN32_WCE
12 #error TnbCeBluetoothCom.h is only supported on Windows CE platforms.
13#endif // _WIN32_WCE
14
15
16
17#include "TnbCeBluetooth.h"
18#include "TnbRegistryAccessor.h"
19#include <pm.h>
20
21
22
23#ifndef _TnbDOXYGEN //Document作成用シンボル
24
25//-- bt_ddi.h -----------------------------------------------------------
26#define OID_PAN_CONNECT 0xffff0101
27#define OID_PAN_DISCONNECT 0xffff0102
28#define OID_PAN_AUTHENTICATE 0xffff0103
29#define OID_PAN_ENCRYPT 0xffff0104
30
31//-- ntddndis.h ----------------------------------------------------
32#ifdef UNDER_CE
33#define DD_NDIS_DEVICE_NAME TEXT("NDS0:")
34#else
35#define DD_NDIS_DEVICE_NAME "\\Device\\UNKNOWN"
36#endif
37typedef ULONG NDIS_OID, *PNDIS_OID;
38#define _NDIS_CONTROL_CODE(request,method) \
39 CTL_CODE(FILE_DEVICE_PHYSICAL_NETCARD, request, method, FILE_ANY_ACCESS)
40#define WINCE_IOCTL_START 8
41#define IOCTL_NDIS_BIND_ADAPTER _NDIS_CONTROL_CODE( WINCE_IOCTL_START+4, METHOD_OUT_DIRECT )
42
43//-- nuiouser.h ----------------------------------------------------
44#define NDISUIO_DEVICE_NAME TEXT("UIO1:")
45#define FSCTL_NDISUIO_BASE FILE_DEVICE_NETWORK
46#define _NDISUIO_CTL_CODE(_Function, _Method, _Access) \
47 CTL_CODE(FSCTL_NDISUIO_BASE, _Function, _Method, _Access)
48#define IOCTL_NDISUIO_SET_OID_VALUE \
49 _NDISUIO_CTL_CODE(0x205, METHOD_BUFFERED, FILE_ANY_ACCESS)
50#define IOCTL_NDISUIO_NIC_STATISTICS \
51 _NDISUIO_CTL_CODE(0x209, METHOD_BUFFERED, FILE_ANY_ACCESS)
52typedef struct {
53 ULONG Size;
54 LPCTSTR ptcDeviceName; //PTCHAR
55 ULONG DeviceState;
56 ULONG MediaType;
57 ULONG MediaState;
58 ULONG PhysicalMediaType;
59 ULONG LinkSpeed;
60 ULONGLONG PacketsSent;
61 ULONGLONG PacketsReceived;
62 ULONG InitTime;
63 ULONG ConnectTime;
64 ULONGLONG BytesSent;
65 ULONGLONG BytesReceived;
66 ULONGLONG DirectedBytesReceived;
67 ULONGLONG DirectedPacketsReceived;
68 ULONG PacketsReceiveErrors;
69 ULONG PacketsSendErrors;
70 ULONG ResetCount;
71 ULONG MediaSenseConnectCount;
72 ULONG MediaSenseDisconnectCount;
73} NIC_STATISTICS, *PNIC_STATISTICS;
74
75//-- ndispwr.h ----------------------------------------------------
76#define NDISPWR_DEVICE_NAME TEXT("NPW1:")
77#define FSCTL_NDISPWR_BASE FILE_DEVICE_NETWORK
78#define _NDISPWR_CTL_CODE(_Function, _Method, _Access) \
79 CTL_CODE(FSCTL_NDISPWR_BASE, _Function, _Method, _Access)
80#define IOCTL_NPW_SAVE_POWER_STATE \
81 _NDISPWR_CTL_CODE(0x200, METHOD_BUFFERED, FILE_ANY_ACCESS)
82typedef struct _NDISPWR_SAVEPOWERSTATE
83{
84 LPCWSTR pwcAdapterName;
85 CEDEVICE_POWER_STATE CePowerState;
86} NDISPWR_SAVEPOWERSTATE, *PNDISPWR_SAVEPOWERSTATE;
87#endif // _TnbDOXYGEN
88
89
90
91//TNB Library
92namespace TNB
93{
94
95
96
108{
109public:
110
116 static bool Activate(void)
117 {
118 CBluetoothApi api;
119 DWORD r = api.BthActivatePAN(true);
120 if ( ! IsActivated() )
121 {
122 SetAdapterPower(GetDeviceName(), PwrDeviceUnspecified);
123 r = api.BthActivatePAN(true);
124 }
125 ::SetLastError(r);
126 return r == ERROR_SUCCESS;
127 }
128
134 static bool Deactivate(void)
135 {
136 CBluetoothApi api;
137 DWORD r = api.BthActivatePAN(false);
138 if ( r == ERROR_SUCCESS )
139 {
140 while ( true )
141 {
142 r = api.BthActivatePAN(false);
143 if ( r != ERROR_SUCCESS )
144 {
145 if ( r == ERROR_INVALID_STATE )
146 {
147 r = ERROR_SUCCESS;
148 }
149 break;
150 }
151 }
152 }
153 ::SetLastError(r);
154 return r == ERROR_SUCCESS;
155 }
156
163 static bool IsActivated(void)
164 {
165 NIC_STATISTICS ns;
166 return ms_GetState(ns);
167 }
168
175 static bool IsConnected(void)
176 {
177 NIC_STATISTICS ns;
178 bool r = ms_GetState(ns);
179 if ( r )
180 {
181 ::SetLastError(0);
182 return ns.MediaState == 0/*MEDIA_STATE_CONNECTED*/;
183 }
184 return false;
185 }
186
196 static bool Connect(const BT_ADDR& btAddr, const GUID& serviceId, bool isAuthenticate, bool isEncrypt)
197 {
198 DWORD r = ERROR_SUCCESS;
199 HANDLE h = ms_OpenNdisUio();
200 if ( h == INVALID_HANDLE_VALUE )
201 {
202 r = _GetLastError("CreateFile");
203 }
204 if ( r == ERROR_SUCCESS )
205 {
206 TOidDwordValue v(OID_PAN_AUTHENTICATE, isAuthenticate);
207 r = ms_SetOidValue(h, v);
208 }
209 if ( r == ERROR_SUCCESS )
210 {
211 TOidDwordValue v(OID_PAN_ENCRYPT, isEncrypt);
212 r = ms_SetOidValue(h, v);
213 }
214 if ( r == ERROR_SUCCESS )
215 {
216 TOidBtAddrValue v(OID_PAN_CONNECT, btAddr, serviceId);
217 r = ms_SetOidValue(h, v);
218 }
219 if ( INVALID_HANDLE_VALUE != h )
220 {
221 ::CloseHandle(h);
222 }
223 if ( r == ERROR_SUCCESS )
224 {
225 ::Sleep(50);
226 if ( ! IsConnected() )
227 {
228 r = ERROR_NOT_FOUND;
229 }
230 }
231 ::SetLastError(r);
232 return (r == ERROR_SUCCESS);
233 }
234
247 static bool Connect(const BT_ADDR& btAddr, bool isAuthenticate, bool isEncrypt)
248 {
249 CBluetoothDevice dev = btAddr;
250 if ( dev.HasService(NAPServiceClassID_UUID16) )
251 {
252 return Connect(btAddr, NAPServiceClass_UUID, isAuthenticate, isEncrypt);
253 }
254 if ( dev.HasService(GNServiceClassID_UUID16) )
255 {
256 return Connect(btAddr, GNServiceClass_UUID, isAuthenticate, isEncrypt);
257 }
258 if ( dev.HasService(PANUServiceClassID_UUID16) )
259 {
260 return Connect(btAddr, PANUServiceClass_UUID, isAuthenticate, isEncrypt);
261 }
262 return false;
263 }
264
272 static bool Disconnect(const BT_ADDR& btAddr)
273 {
274 DWORD r = ERROR_SUCCESS;
275 HANDLE h = ms_OpenNdisUio();
276 if ( h == INVALID_HANDLE_VALUE )
277 {
278 r = _GetLastError("CreateFile");
279 }
280 if ( r == ERROR_SUCCESS )
281 {
282 GUID g = { 0 };
283 TOidBtAddrValue v(OID_PAN_DISCONNECT, btAddr, g);
284 r = ms_SetOidValue(h, v);
285 }
286 if ( INVALID_HANDLE_VALUE != h )
287 {
288 ::CloseHandle(h);
289 }
290 if ( r == ERROR_SUCCESS )
291 {
292 ::Sleep(50);
293 if ( IsConnected() )
294 {
295 r = ERROR_NOT_FOUND;
296 }
297 }
298 ::SetLastError(r);
299 return (r == ERROR_SUCCESS);
300 }
301
310 static WORD GetRegistPanType(void)
311 {
312 CRegistryAccessor reg(HKEY_LOCAL_MACHINE, _T("Comm\\BTPAN1\\Parms"), KEY_READ);
313 CRegistryAccessor::CSection sec = reg[_T("")];
314 CStr s = sec.QueryString(_T("AdapterType"));
315 if ( s == _T("NAP") )
316 {
317 return NAPServiceClassID_UUID16;
318 }
319 else if ( s == _T("PANU") )
320 {
321 return PANUServiceClassID_UUID16;
322 }
323 else if ( s == _T("GN") )
324 {
325 return GNServiceClassID_UUID16;
326 }
327 return 0;
328 }
329
338 static bool SetRegistPanType(WORD uuid, bool isPublish)
339 {
340 if ( uuid == NAPServiceClassID_UUID16 || uuid == PANUServiceClassID_UUID16 )
341 {
342 CStr s;
343 CRegistryAccessor reg(HKEY_LOCAL_MACHINE, _T("Comm\\BTPAN1\\Parms"));
344 CRegistryAccessor::CSection sec = reg[_T("")];
345 bool isPanu = (uuid == PANUServiceClassID_UUID16);
346 //
347 GUID id = NAPServiceClass_UUID;
348 CStr adapterType = _T("NAP");
349 CStr friendlyName= _T("Network Access Point");
350 if ( isPanu )
351 {
352 id = PANUServiceClass_UUID;
353 adapterType = _T("PANU");
354 friendlyName= _T("Personal Ad-hoc Network");
355 }
356 bool r = true;
357 r &= sec.WriteString(_T("AdapterType"), adapterType);
358 r &= sec.WriteString(_T("FriendlyName"), friendlyName);
359 r &= sec.WriteString(_T("Description"), CStr::Fmt(_T("Bluetooth %s Service"), adapterType));
360 s.Format(_T("{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}"),
361 id.Data1, id.Data2, id.Data3,
362 id.Data4[0], id.Data4[1], id.Data4[2], id.Data4[3],
363 id.Data4[4], id.Data4[5], id.Data4[6], id.Data4[7]);
364 r &= sec.WriteString(_T("ServiceId"), s);
365 if ( sec.HasKey(_T("ProtocolsToBindTo")) )
366 {
367 CStrVector vs;
368 vs.Add(_T("NOT"));
369 if ( ! isPanu )
370 {
371 vs.Add(_T("NDISUIO"));
372 }
373 r &= sec.WritePluralStrings(_T("ProtocolsToBindTo"), vs);
374 }
375 r &= sec.WriteDword(_T("PublishSdpOnBoot"), isPublish);
376 return r;
377 }
378 return false;
379 }
380
387 static CStr QueryRegistTcpipValue(LPCTSTR lpszName)
388 {
389 CRegistryAccessor reg(HKEY_LOCAL_MACHINE, L"Comm\\BTPAN1\\Parms", KEY_READ);
390 CRegistryAccessor::CSection sec= reg[L"TCPIP"];
391 return sec.QueryString(lpszName);
392 }
393
398 static LPCTSTR GetDeviceName(void)
399 {
400 return _T("BTPAN1");
401 }
402
410 static bool SetAdapterPower(LPCTSTR lpszAdapterName, CEDEVICE_POWER_STATE dx)
411 {
412 // NDISPWR_DEVICE 設定
413 {
414 HANDLE h = ::CreateFile(NDISPWR_DEVICE_NAME, GENERIC_READ | GENERIC_WRITE,
415 FILE_SHARE_READ | FILE_SHARE_WRITE,
416 NULL, OPEN_EXISTING, 0, NULL);
417 if ( h == INVALID_HANDLE_VALUE )
418 {
419 return false;
420 }
421 NDISPWR_SAVEPOWERSTATE savePowerState;
422 savePowerState.pwcAdapterName = lpszAdapterName;
423 savePowerState.CePowerState = dx;
424 BOOL r = ::DeviceIoControl(h, IOCTL_NPW_SAVE_POWER_STATE, &savePowerState, sizeof(NDISPWR_SAVEPOWERSTATE), NULL, 0, NULL, NULL);
425 DWORD e = _GetLastError("DeviceIoControl");
426 ::CloseHandle(h);
427 if ( ! r )
428 {
429 ::SetLastError(e);
430 return false;
431 }
432 }
433 // DD_NDIS_DEVICE 処理
434 {
435 CStr s = lpszAdapterName;
436 size_t sl = (s.GetLength() + 1) * sizeof(TCHAR);
437 HANDLE h = ::CreateFile(DD_NDIS_DEVICE_NAME, GENERIC_READ | GENERIC_WRITE,
438 FILE_SHARE_READ | FILE_SHARE_WRITE,
439 NULL, OPEN_EXISTING, 0, NULL);
440 if ( h == INVALID_HANDLE_VALUE )
441 {
442 return false;
443 }
444 BOOL r = ::DeviceIoControl(h, IOCTL_NDIS_BIND_ADAPTER, s.GetBuffer(), sl, NULL, 0, NULL, NULL);
445 DWORD e = _GetLastError("DeviceIoControl");
446 ::CloseHandle(h);
447 if ( ! r )
448 {
449 ::SetLastError(e);
450 return false;
451 }
452 }
453 return true;
454 }
455
456private:
458 struct TOidValue
459 {
460 NDIS_OID oid;
461 LPCTSTR lpszDeviceName;
462 //コンストラクタ
463 TOidValue(NDIS_OID o) : oid(o), lpszDeviceName(CBluetoothPan::GetDeviceName())
464 {}
465 };
467 struct TOidDwordValue : TOidValue
468 {
469 DWORD data;
470 //コンストラクタ
471 TOidDwordValue(NDIS_OID o, DWORD d) : TOidValue(o), data(d)
472 {}
473 };
476 #pragma pack(push, 1)
477 struct TOidBtAddrValue : TOidValue
478 {
479 union {
480 struct {
481 BYTE addr[6];
482 GUID guid;
483 };
484 BT_ADDR btAddr;
485 };
486 //コンストラクタ
487 TOidBtAddrValue(NDIS_OID o, BT_ADDR a, const GUID& g) : TOidValue(o), btAddr(a)
488 {
489 guid = g;
490 }
491 };
492 #pragma pack(pop)
493 // オープン
494 static HANDLE ms_OpenNdisUio(void)
495 {
496 DWORD fa = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED;
497 HANDLE h = ::CreateFile(NDISUIO_DEVICE_NAME, 0, 0, NULL, OPEN_EXISTING, fa, INVALID_HANDLE_VALUE);
498 return h;
499 }
500 // 設定
501 template<typename VAL>
502 static DWORD ms_SetOidValue(HANDLE h, VAL& v)
503 {
504 DWORD w = 0;
505 DWORD c = IOCTL_NDISUIO_SET_OID_VALUE;
506 BOOL r = ::DeviceIoControl(h, c, &v, sizeof(VAL), NULL, 0, &w, NULL);
507 return r ? ERROR_SUCCESS : _GetLastError("DeviceIoControl");
508 }
509 // 状態取得
510 static DWORD ms_GetStatis(NIC_STATISTICS& _ns, HANDLE h)
511 {
512 DWORD w = 0;
513 DWORD c = IOCTL_NDISUIO_NIC_STATISTICS;
514 Zero(_ns);
515 _ns.Size = sizeof(NIC_STATISTICS);
516 _ns.ptcDeviceName = CBluetoothPan::GetDeviceName();
517 BOOL r = ::DeviceIoControl(h, c, NULL, 0, &_ns, sizeof(NIC_STATISTICS), &w, NULL);
518 return r ? ERROR_SUCCESS : _GetLastError("DeviceIoControl");
519 }
520 // 状態取得
521 static bool ms_GetState(NIC_STATISTICS& _ns)
522 {
523 DWORD r = ERROR_SUCCESS;
524 HANDLE h = ms_OpenNdisUio();
525 if ( h == INVALID_HANDLE_VALUE )
526 {
527 r = _GetLastError("CreateFile");
528 }
529 if ( r == ERROR_SUCCESS )
530 {
531 r = ms_GetStatis(_ns, h);
532 }
533 if ( INVALID_HANDLE_VALUE != h )
534 {
535 ::CloseHandle(h);
536 }
537 ::SetLastError(r);
538 return (r == ERROR_SUCCESS);
539 }
540};
541
542
543
544};//TNB
545
CE 用 Bluetooth関係のヘッダ
レジストリアクセス関係のヘッダ
Bluetooth(CE) デバイス管理.
bool HasService(CWorkMem &_raw, WORD uuid16) const
[確認] サービス確認.
Bluetooth(CE) PAN クラス.
static bool SetRegistPanType(WORD uuid, bool isPublish)
[設定] レジストリ PAN タイプ設定.
static bool Deactivate(void)
[設定] 無効.
static WORD GetRegistPanType(void)
[取得] レジストリ PAN タイプ取得.
static bool IsActivated(void)
[取得] 有効確認.
static bool Disconnect(const BT_ADDR &btAddr)
[設定] 切断.
static CStr QueryRegistTcpipValue(LPCTSTR lpszName)
[設定] レジストリ TCPIP 設定値取得.
static bool Connect(const BT_ADDR &btAddr, const GUID &serviceId, bool isAuthenticate, bool isEncrypt)
[設定] 接続.
static bool Activate(void)
[設定] 有効.
static bool SetAdapterPower(LPCTSTR lpszAdapterName, CEDEVICE_POWER_STATE dx)
[設定] アダプタ電源設定.
static bool Connect(const BT_ADDR &btAddr, bool isAuthenticate, bool isEncrypt)
[設定] 接続.
static bool IsConnected(void)
[取得] 接続確認.
static LPCTSTR GetDeviceName(void)
[取得] デバイス名取得.
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
レジストリアクセスクラス
size_t GetLength(void) const
[取得] 文字列長
Definition: TnbStr.h:518
static CStrT Fmt(const TCHAR *lpszFormat,...)
[作成] 書式付き文字列作成
Definition: TnbStr.h:1206
void Format(const TYP *lpszFormat,...)
[代入] 書式付き文字列代入.
Definition: TnbStr.h:359
TYP * GetBuffer(size_t iLength=0)
[操作] 書き込みバッファ要求.
Definition: TnbStr.h:914
virtual INDEX Add(const TYP &t)
[追加] 要素一つ追加.
Definition: TnbVector.h:383
セクション情報アクセスクラス
Definition: TnbAccessor.h:294
CStr QueryString(LPCTSTR lpszKey, LPCTSTR lpszDefault=NULL) const
[取得] 文字列情報取得
Definition: TnbAccessor.h:506
bool WriteDword(LPCTSTR lpszKey, DWORD dwValue)
[追加] 数値情報記録
Definition: TnbAccessor.h:576
bool WritePluralStrings(LPCTSTR lpszKey, const CStrVector &vs)
[追加] 文字列群情報記録
Definition: TnbAccessor.h:519
bool WriteString(LPCTSTR lpszKey, LPCTSTR lpszValue)
[追加] 文字列情報記録
Definition: TnbAccessor.h:495
bool HasKey(LPCTSTR lpszKey) const
[確認] 情報存在確認
Definition: TnbAccessor.h:450
void Zero(V &value)
[設定] ゼロクリア.
Definition: TnbDef.h:399
TNB Library
Definition: TnbDoxyTitle.txt:2
ULONGLONG BT_ADDR
Bluetooth(CE) アドレス.