12 #error TnbCeBluetoothCom.h is only supported on Windows CE platforms.
26#define OID_PAN_CONNECT 0xffff0101
27#define OID_PAN_DISCONNECT 0xffff0102
28#define OID_PAN_AUTHENTICATE 0xffff0103
29#define OID_PAN_ENCRYPT 0xffff0104
33#define DD_NDIS_DEVICE_NAME TEXT("NDS0:")
35#define DD_NDIS_DEVICE_NAME "\\Device\\UNKNOWN"
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 )
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)
54 LPCTSTR ptcDeviceName;
58 ULONG PhysicalMediaType;
60 ULONGLONG PacketsSent;
61 ULONGLONG PacketsReceived;
65 ULONGLONG BytesReceived;
66 ULONGLONG DirectedBytesReceived;
67 ULONGLONG DirectedPacketsReceived;
68 ULONG PacketsReceiveErrors;
69 ULONG PacketsSendErrors;
71 ULONG MediaSenseConnectCount;
72 ULONG MediaSenseDisconnectCount;
73} NIC_STATISTICS, *PNIC_STATISTICS;
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
84 LPCWSTR pwcAdapterName;
85 CEDEVICE_POWER_STATE CePowerState;
86} NDISPWR_SAVEPOWERSTATE, *PNDISPWR_SAVEPOWERSTATE;
119 DWORD r = api.BthActivatePAN(
true);
123 r = api.BthActivatePAN(
true);
126 return r == ERROR_SUCCESS;
137 DWORD r = api.BthActivatePAN(
false);
138 if ( r == ERROR_SUCCESS )
142 r = api.BthActivatePAN(
false);
143 if ( r != ERROR_SUCCESS )
145 if ( r == ERROR_INVALID_STATE )
154 return r == ERROR_SUCCESS;
166 return ms_GetState(ns);
178 bool r = ms_GetState(ns);
182 return ns.MediaState == 0;
196 static bool Connect(
const BT_ADDR& btAddr,
const GUID& serviceId,
bool isAuthenticate,
bool isEncrypt)
198 DWORD r = ERROR_SUCCESS;
199 HANDLE h = ms_OpenNdisUio();
200 if ( h == INVALID_HANDLE_VALUE )
202 r = _GetLastError(
"CreateFile");
204 if ( r == ERROR_SUCCESS )
206 TOidDwordValue v(OID_PAN_AUTHENTICATE, isAuthenticate);
207 r = ms_SetOidValue(h, v);
209 if ( r == ERROR_SUCCESS )
211 TOidDwordValue v(OID_PAN_ENCRYPT, isEncrypt);
212 r = ms_SetOidValue(h, v);
214 if ( r == ERROR_SUCCESS )
216 TOidBtAddrValue v(OID_PAN_CONNECT, btAddr, serviceId);
217 r = ms_SetOidValue(h, v);
219 if ( INVALID_HANDLE_VALUE != h )
223 if ( r == ERROR_SUCCESS )
232 return (r == ERROR_SUCCESS);
250 if ( dev.
HasService(NAPServiceClassID_UUID16) )
252 return Connect(btAddr, NAPServiceClass_UUID, isAuthenticate, isEncrypt);
254 if ( dev.
HasService(GNServiceClassID_UUID16) )
256 return Connect(btAddr, GNServiceClass_UUID, isAuthenticate, isEncrypt);
258 if ( dev.
HasService(PANUServiceClassID_UUID16) )
260 return Connect(btAddr, PANUServiceClass_UUID, isAuthenticate, isEncrypt);
274 DWORD r = ERROR_SUCCESS;
275 HANDLE h = ms_OpenNdisUio();
276 if ( h == INVALID_HANDLE_VALUE )
278 r = _GetLastError(
"CreateFile");
280 if ( r == ERROR_SUCCESS )
283 TOidBtAddrValue v(OID_PAN_DISCONNECT, btAddr, g);
284 r = ms_SetOidValue(h, v);
286 if ( INVALID_HANDLE_VALUE != h )
290 if ( r == ERROR_SUCCESS )
299 return (r == ERROR_SUCCESS);
315 if ( s == _T(
"NAP") )
317 return NAPServiceClassID_UUID16;
319 else if ( s == _T(
"PANU") )
321 return PANUServiceClassID_UUID16;
323 else if ( s == _T(
"GN") )
325 return GNServiceClassID_UUID16;
340 if ( uuid == NAPServiceClassID_UUID16 || uuid == PANUServiceClassID_UUID16 )
345 bool isPanu = (uuid == PANUServiceClassID_UUID16);
347 GUID
id = NAPServiceClass_UUID;
348 CStr adapterType = _T(
"NAP");
349 CStr friendlyName= _T(
"Network Access Point");
352 id = PANUServiceClass_UUID;
353 adapterType = _T(
"PANU");
354 friendlyName= _T(
"Personal Ad-hoc Network");
357 r &= sec.
WriteString(_T(
"AdapterType"), adapterType);
358 r &= sec.
WriteString(_T(
"FriendlyName"), friendlyName);
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]);
365 if ( sec.
HasKey(_T(
"ProtocolsToBindTo")) )
371 vs.
Add(_T(
"NDISUIO"));
375 r &= sec.
WriteDword(_T(
"PublishSdpOnBoot"), isPublish);
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 )
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");
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 )
444 BOOL r = ::DeviceIoControl(h, IOCTL_NDIS_BIND_ADAPTER, s.
GetBuffer(), sl, NULL, 0, NULL, NULL);
445 DWORD e = _GetLastError(
"DeviceIoControl");
461 LPCTSTR lpszDeviceName;
467 struct TOidDwordValue : TOidValue
471 TOidDwordValue(NDIS_OID o, DWORD d) : TOidValue(o), data(d)
476 #pragma pack(push, 1)
477 struct TOidBtAddrValue : TOidValue
487 TOidBtAddrValue(NDIS_OID o,
BT_ADDR a,
const GUID& g) : TOidValue(o), btAddr(a)
494 static HANDLE ms_OpenNdisUio(
void)
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);
501 template<
typename VAL>
502 static DWORD ms_SetOidValue(HANDLE h, VAL& v)
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");
510 static DWORD ms_GetStatis(NIC_STATISTICS& _ns, HANDLE h)
513 DWORD c = IOCTL_NDISUIO_NIC_STATISTICS;
515 _ns.Size =
sizeof(NIC_STATISTICS);
517 BOOL r = ::DeviceIoControl(h, c, NULL, 0, &_ns,
sizeof(NIC_STATISTICS), &w, NULL);
518 return r ? ERROR_SUCCESS : _GetLastError(
"DeviceIoControl");
521 static bool ms_GetState(NIC_STATISTICS& _ns)
523 DWORD r = ERROR_SUCCESS;
524 HANDLE h = ms_OpenNdisUio();
525 if ( h == INVALID_HANDLE_VALUE )
527 r = _GetLastError(
"CreateFile");
529 if ( r == ERROR_SUCCESS )
531 r = ms_GetStatis(_ns, h);
533 if ( INVALID_HANDLE_VALUE != h )
538 return (r == ERROR_SUCCESS);
bool HasService(CWorkMem &_raw, WORD uuid16) const
[確認] サービス確認.
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)
[取得] デバイス名取得.
size_t GetLength(void) const
[取得] 文字列長
static CStrT Fmt(const TCHAR *lpszFormat,...)
[作成] 書式付き文字列作成
void Format(const TYP *lpszFormat,...)
[代入] 書式付き文字列代入.
TYP * GetBuffer(size_t iLength=0)
[操作] 書き込みバッファ要求.
virtual INDEX Add(const TYP &t)
[追加] 要素一つ追加.
CStr QueryString(LPCTSTR lpszKey, LPCTSTR lpszDefault=NULL) const
[取得] 文字列情報取得
bool WriteDword(LPCTSTR lpszKey, DWORD dwValue)
[追加] 数値情報記録
bool WritePluralStrings(LPCTSTR lpszKey, const CStrVector &vs)
[追加] 文字列群情報記録
bool WriteString(LPCTSTR lpszKey, LPCTSTR lpszValue)
[追加] 文字列情報記録
bool HasKey(LPCTSTR lpszKey) const
[確認] 情報存在確認
void Zero(V &value)
[設定] ゼロクリア.
ULONGLONG BT_ADDR
Bluetooth(CE) アドレス.