author | zautrix <zautrix> | 2004-08-07 17:24:40 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-08-07 17:24:40 (UTC) |
commit | 88b0d33b8b0b1f6ae320cfc863ca6a47fa8fec22 (patch) (unidiff) | |
tree | 6331418973714243beb674abc87692277b83869d /gammu/emb/common/device/bluetoth/blue_w32.c | |
parent | ef8a09ce74ad3f0a51484d03fdf009bd5b3677bf (diff) | |
download | kdepimpi-88b0d33b8b0b1f6ae320cfc863ca6a47fa8fec22.zip kdepimpi-88b0d33b8b0b1f6ae320cfc863ca6a47fa8fec22.tar.gz kdepimpi-88b0d33b8b0b1f6ae320cfc863ca6a47fa8fec22.tar.bz2 |
Initial revision
Diffstat (limited to 'gammu/emb/common/device/bluetoth/blue_w32.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | gammu/emb/common/device/bluetoth/blue_w32.c | 216 |
1 files changed, 216 insertions, 0 deletions
diff --git a/gammu/emb/common/device/bluetoth/blue_w32.c b/gammu/emb/common/device/bluetoth/blue_w32.c new file mode 100644 index 0000000..a631c9f --- a/dev/null +++ b/gammu/emb/common/device/bluetoth/blue_w32.c | |||
@@ -0,0 +1,216 @@ | |||
1 | /* (c) 2003-2004 by Marcin Wiacek and Intra */ | ||
2 | |||
3 | #include "../../gsmstate.h" | ||
4 | |||
5 | #ifdef GSM_ENABLE_BLUETOOTHDEVICE | ||
6 | #ifdef WIN32 | ||
7 | |||
8 | #include <stdlib.h> | ||
9 | #include <stdio.h> | ||
10 | #include <fcntl.h> | ||
11 | #include <errno.h> | ||
12 | #include <string.h> | ||
13 | #include <windows.h> | ||
14 | #include <io.h> | ||
15 | |||
16 | #include "../../misc/coding/coding.h" | ||
17 | #include "../../gsmcomon.h" | ||
18 | #include "../devfunc.h" | ||
19 | #include "bluetoth.h" | ||
20 | #include "blue_w32.h" | ||
21 | |||
22 | GSM_Error bluetooth_connect(GSM_StateMachine *s, int port, char *device) | ||
23 | { | ||
24 | GSM_Device_BlueToothData *d = &s->Device.Data.BlueTooth; | ||
25 | WSADATA wsaData; | ||
26 | SOCKADDR_BTH sab; | ||
27 | int i; | ||
28 | |||
29 | smprintf(s, "Connecting to RF channel %i\n",port); | ||
30 | |||
31 | WSAStartup(MAKEWORD(1,1), &wsaData); | ||
32 | |||
33 | d->hPhone = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); | ||
34 | if (d->hPhone == INVALID_SOCKET) { | ||
35 | i = GetLastError(); | ||
36 | GSM_OSErrorInfo(s, "Socket in bluetooth_open"); | ||
37 | if (i == 10041) return ERR_DEVICENODRIVER;//unknown socket type | ||
38 | return ERR_UNKNOWN; | ||
39 | } | ||
40 | |||
41 | memset (&sab, 0, sizeof(sab)); | ||
42 | sab.port = port; | ||
43 | sab.addressFamily = AF_BTH; | ||
44 | sab.btAddr = 0; | ||
45 | for (i=0;i<(int)strlen(device);i++) { | ||
46 | if (device[i] >='0' && device[i] <='9') { | ||
47 | sab.btAddr = sab.btAddr * 16; | ||
48 | sab.btAddr = sab.btAddr + (device[i]-'0'); | ||
49 | } | ||
50 | if (device[i] >='a' && device[i] <='f') { | ||
51 | sab.btAddr = sab.btAddr * 16; | ||
52 | sab.btAddr = sab.btAddr + (device[i]-'a'+10); | ||
53 | } | ||
54 | if (device[i] >='A' && device[i] <='F') { | ||
55 | sab.btAddr = sab.btAddr * 16; | ||
56 | sab.btAddr = sab.btAddr + (device[i]-'A'+10); | ||
57 | } | ||
58 | } | ||
59 | dbgprintf("Remote Bluetooth device is %04x%08x\n", | ||
60 | GET_NAP(sab.btAddr), GET_SAP(sab.btAddr)); | ||
61 | |||
62 | if (connect (d->hPhone, (struct sockaddr *)&sab, sizeof(sab)) != 0) { | ||
63 | i = GetLastError(); | ||
64 | GSM_OSErrorInfo(s, "Connect in bluetooth_open"); | ||
65 | if (i == 10060) return ERR_TIMEOUT; //remote device failed to respond | ||
66 | if (i == 10050) return ERR_DEVICENOTWORK; //socket operation connected with dead network | ||
67 | //noauth | ||
68 | close(d->hPhone); | ||
69 | return ERR_UNKNOWN; | ||
70 | } | ||
71 | |||
72 | return ERR_NONE; | ||
73 | } | ||
74 | |||
75 | #ifdef BLUETOOTH_RF_SEARCHING | ||
76 | |||
77 | #pragma comment(lib, "irprops.lib") | ||
78 | #pragma comment(lib, "ws2_32.lib") | ||
79 | |||
80 | static GSM_Error bluetooth_checkdevice(GSM_StateMachine *s, char *address, WSAPROTOCOL_INFO *protocolInfo) | ||
81 | { | ||
82 | GSM_Device_BlueToothData *d = &s->Device.Data.BlueTooth; | ||
83 | WSAQUERYSET querySet; | ||
84 | DWORD flags; | ||
85 | GUID protocol; | ||
86 | int i, result; | ||
87 | BYTE buffer[2000]; | ||
88 | char addressAsString[1000]; | ||
89 | DWORD bufferLength, addressSize; | ||
90 | WSAQUERYSET *pResults = (WSAQUERYSET*)&buffer; | ||
91 | HANDLE handle; | ||
92 | GSM_Error error; | ||
93 | |||
94 | memset(&querySet, 0, sizeof(querySet)); | ||
95 | querySet.dwSize = sizeof(querySet); | ||
96 | protocol = L2CAP_PROTOCOL_UUID; | ||
97 | querySet.lpServiceClassId = &protocol; | ||
98 | querySet.dwNameSpace = NS_BTH; | ||
99 | querySet.lpszContext = address; | ||
100 | |||
101 | flags = LUP_FLUSHCACHE | LUP_RETURN_NAME | | ||
102 | LUP_RETURN_TYPE | LUP_RETURN_ADDR | | ||
103 | LUP_RETURN_BLOB | LUP_RETURN_COMMENT; | ||
104 | |||
105 | result = WSALookupServiceBegin(&querySet, flags, &handle); | ||
106 | if (result != 0) return ERR_UNKNOWN; | ||
107 | |||
108 | bufferLength = sizeof(buffer); | ||
109 | while (1) { | ||
110 | result = WSALookupServiceNext(handle, flags, &bufferLength, pResults); | ||
111 | if (result != 0) break; | ||
112 | addressSize = sizeof(addressAsString); | ||
113 | addressAsString[0] = 0; | ||
114 | if (WSAAddressToString(pResults->lpcsaBuffer->RemoteAddr.lpSockaddr, | ||
115 | pResults->lpcsaBuffer->RemoteAddr.iSockaddrLength, protocolInfo, | ||
116 | addressAsString,&addressSize)==0) { | ||
117 | smprintf(s, "%s - ", addressAsString); | ||
118 | } | ||
119 | smprintf(s, "\"%s\"\n", pResults->lpszServiceInstanceName); | ||
120 | if (addressAsString[0] != 0) { | ||
121 | for (i=strlen(addressAsString)-1;i>0;i--) { | ||
122 | if (addressAsString[i] == ':') break; | ||
123 | } | ||
124 | if (bluetooth_checkservicename(s, pResults->lpszServiceInstanceName) == ERR_NONE) { | ||
125 | error = bluetooth_connect(s,atoi(addressAsString+i+1),address+1); | ||
126 | result = WSALookupServiceEnd(handle); | ||
127 | return error; | ||
128 | } | ||
129 | } | ||
130 | } | ||
131 | result = WSALookupServiceEnd(handle); | ||
132 | return ERR_NOTSUPPORTED; | ||
133 | } | ||
134 | |||
135 | GSM_Error bluetooth_findchannel(GSM_StateMachine *s) | ||
136 | { | ||
137 | GSM_Device_BlueToothData *d = &s->Device.Data.BlueTooth; | ||
138 | WSADATA wsaData; | ||
139 | int i, protocolInfoSize, result; | ||
140 | WSAPROTOCOL_INFO protocolInfo; | ||
141 | HANDLE handle; | ||
142 | DWORD flags; | ||
143 | WSAQUERYSET querySet; | ||
144 | BYTE buffer[2000]; | ||
145 | char addressAsString[1000]; | ||
146 | DWORD bufferLength, addressSize; | ||
147 | WSAQUERYSET *pResults = (WSAQUERYSET*)&buffer; | ||
148 | GSM_Error error; | ||
149 | |||
150 | if (WSAStartup(MAKEWORD(2,2), &wsaData)!=0x00) return ERR_DEVICENODRIVER; | ||
151 | |||
152 | d->hPhone = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); | ||
153 | if (d->hPhone == INVALID_SOCKET) { | ||
154 | i = GetLastError(); | ||
155 | GSM_OSErrorInfo(s, "Socket in bluetooth_open"); | ||
156 | if (i == 10041) return ERR_DEVICENODRIVER;//unknown socket type | ||
157 | return ERR_UNKNOWN; | ||
158 | } | ||
159 | |||
160 | protocolInfoSize = sizeof(protocolInfo); | ||
161 | if (getsockopt(d->hPhone, SOL_SOCKET, SO_PROTOCOL_INFO, | ||
162 | (char*)&protocolInfo, &protocolInfoSize) != 0) | ||
163 | { | ||
164 | close(d->hPhone); | ||
165 | return ERR_UNKNOWN; | ||
166 | } | ||
167 | close(d->hPhone); | ||
168 | |||
169 | if (!strcmp(s->CurrentConfig->Device,"com2:")) { | ||
170 | bufferLength = sizeof(buffer); | ||
171 | |||
172 | flags = LUP_RETURN_NAME | LUP_CONTAINERS | | ||
173 | LUP_RETURN_ADDR | LUP_FLUSHCACHE | | ||
174 | LUP_RETURN_TYPE | LUP_RETURN_BLOB | LUP_RES_SERVICE; | ||
175 | |||
176 | memset(&querySet, 0, sizeof(querySet)); | ||
177 | querySet.dwSize = sizeof(querySet); | ||
178 | querySet.dwNameSpace = NS_BTH; | ||
179 | |||
180 | result = WSALookupServiceBegin(&querySet, flags, &handle); | ||
181 | if (result != 0) return ERR_UNKNOWN; | ||
182 | |||
183 | while (1) { | ||
184 | result = WSALookupServiceNext(handle, flags, &bufferLength, pResults); | ||
185 | if (result != 0) break; | ||
186 | |||
187 | smprintf(s, "\"%s\"", pResults->lpszServiceInstanceName); | ||
188 | |||
189 | addressSize = sizeof(addressAsString); | ||
190 | addressAsString[0] = 0; | ||
191 | if (WSAAddressToString(pResults->lpcsaBuffer->RemoteAddr.lpSockaddr, | ||
192 | pResults->lpcsaBuffer->RemoteAddr.iSockaddrLength, &protocolInfo, | ||
193 | addressAsString,&addressSize)==0) { | ||
194 | smprintf(s, " - %s\n", addressAsString); | ||
195 | error = bluetooth_checkdevice(s, addressAsString,&protocolInfo); | ||
196 | if (error == ERR_NONE) { | ||
197 | result = WSALookupServiceEnd(handle); | ||
198 | return error; | ||
199 | } | ||
200 | } else smprintf(s, "\n"); | ||
201 | } | ||
202 | |||
203 | result = WSALookupServiceEnd(handle); | ||
204 | return ERR_NOTSUPPORTED; | ||
205 | } else { | ||
206 | return bluetooth_checkdevice(s, s->CurrentConfig->Device,&protocolInfo); | ||
207 | } | ||
208 | } | ||
209 | |||
210 | #endif | ||
211 | #endif | ||
212 | #endif | ||
213 | |||
214 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
215 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
216 | */ | ||