20 files changed, 2131 insertions, 0 deletions
diff --git a/gammu/emb/common/device/bluetoth/affix.c b/gammu/emb/common/device/bluetoth/affix.c new file mode 100644 index 0000000..30a917b --- a/dev/null +++ b/gammu/emb/common/device/bluetoth/affix.c | |||
@@ -0,0 +1,44 @@ | |||
1 | /* (c) 2004 by Marcin Wiacek */ | ||
2 | |||
3 | #include "../../gsmstate.h" | ||
4 | |||
5 | #ifdef GSM_ENABLE_BLUETOOTHDEVICE | ||
6 | #ifdef GSM_ENABLE_AFFIX | ||
7 | |||
8 | #include <stdlib.h> | ||
9 | #include <stdio.h> | ||
10 | #include <fcntl.h> | ||
11 | #include <errno.h> | ||
12 | #include <string.h> | ||
13 | #include <sys/socket.h> | ||
14 | #include <sys/time.h> | ||
15 | #include <unistd.h> | ||
16 | |||
17 | #include "../../gsmcomon.h" | ||
18 | #include "../devfunc.h" | ||
19 | #include "bluetoth.h" | ||
20 | |||
21 | GSM_Error bluetooth_connect(GSM_StateMachine *s, int port, char *device) | ||
22 | { | ||
23 | d->hPhone = fd; | ||
24 | return ERR_NONE; | ||
25 | } | ||
26 | |||
27 | #ifdef BLUETOOTH_RF_SEARCHING | ||
28 | |||
29 | static GSM_Error bluetooth_checkdevice(GSM_StateMachine *s, bdaddr_t *bdaddr, struct search_context *context) | ||
30 | { | ||
31 | } | ||
32 | |||
33 | GSM_Error bluetooth_findchannel(GSM_StateMachine *s) | ||
34 | { | ||
35 | return error; | ||
36 | } | ||
37 | |||
38 | #endif | ||
39 | #endif | ||
40 | #endif | ||
41 | |||
42 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
43 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
44 | */ | ||
diff --git a/gammu/emb/common/device/bluetoth/affix.h b/gammu/emb/common/device/bluetoth/affix.h new file mode 100644 index 0000000..896ecbe --- a/dev/null +++ b/gammu/emb/common/device/bluetoth/affix.h | |||
@@ -0,0 +1,3 @@ | |||
1 | |||
2 | GSM_Error bluetooth_connect(GSM_StateMachine *s, int port, char *device); | ||
3 | GSM_Error bluetooth_findchannel(GSM_StateMachine *s); | ||
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 | */ | ||
diff --git a/gammu/emb/common/device/bluetoth/blue_w32.h b/gammu/emb/common/device/bluetoth/blue_w32.h new file mode 100644 index 0000000..e457c92 --- a/dev/null +++ b/gammu/emb/common/device/bluetoth/blue_w32.h | |||
@@ -0,0 +1,188 @@ | |||
1 | |||
2 | GSM_Error bluetooth_connect(GSM_StateMachine *s, int port, char *device); | ||
3 | GSM_Error bluetooth_findchannel(GSM_StateMachine *s); | ||
4 | |||
5 | /* MS Platform SDK */ | ||
6 | |||
7 | #ifndef __blue_w32_h | ||
8 | #define __blue_w32_h | ||
9 | |||
10 | #include <pshpack1.h> // Without it compiled code hangs up BT stack | ||
11 | |||
12 | typedef ULONGLONG BTH_ADDR, *PBTH_ADDR; | ||
13 | |||
14 | #define NAP_MASK ((ULONGLONG) 0xFFFF00000000) | ||
15 | #define SAP_MASK ((ULONGLONG) 0x0000FFFFFFFF) | ||
16 | |||
17 | #define NAP_BIT_OFFSET (8 * 4) | ||
18 | #define SAP_BIT_OFFSET (0) | ||
19 | |||
20 | #define GET_NAP(_bth_addr) ((USHORT) (((_bth_addr) & NAP_MASK) >> NAP_BIT_OFFSET)) | ||
21 | #define GET_SAP(_bth_addr) ((ULONG) (((_bth_addr) & SAP_MASK) >> SAP_BIT_OFFSET)) | ||
22 | |||
23 | #ifndef AF_BTH | ||
24 | #define AF_BTH 32 | ||
25 | #endif | ||
26 | |||
27 | typedef struct _SOCKADDR_BTH { | ||
28 | USHORT addressFamily; // Always AF_BTH | ||
29 | BTH_ADDR btAddr; // Bluetooth device address | ||
30 | GUID serviceClassId; // [OPTIONAL] system will query SDP for port | ||
31 | ULONG port; // RFCOMM channel or L2CAP PSM | ||
32 | } SOCKADDR_BTH, *PSOCKADDR_BTH; | ||
33 | |||
34 | #define BTHPROTO_RFCOMM 0x0003 | ||
35 | |||
36 | #ifdef BLUETOOTH_RF_SEARCHING | ||
37 | |||
38 | typedef struct _SOCKET_ADDRESS { | ||
39 | LPSOCKADDR lpSockaddr ; | ||
40 | INT iSockaddrLength ; | ||
41 | } SOCKET_ADDRESS, *PSOCKET_ADDRESS, FAR * LPSOCKET_ADDRESS ; | ||
42 | |||
43 | typedef struct _CSADDR_INFO { | ||
44 | SOCKET_ADDRESS LocalAddr ; | ||
45 | SOCKET_ADDRESS RemoteAddr ; | ||
46 | INT iSocketType ; | ||
47 | INT iProtocol ; | ||
48 | } CSADDR_INFO, *PCSADDR_INFO, FAR * LPCSADDR_INFO ; | ||
49 | |||
50 | typedef struct _AFPROTOCOLS { | ||
51 | INT iAddressFamily; | ||
52 | INT iProtocol; | ||
53 | } AFPROTOCOLS, *PAFPROTOCOLS, *LPAFPROTOCOLS; | ||
54 | |||
55 | typedef enum _WSAEcomparator | ||
56 | { | ||
57 | COMP_EQUAL = 0, | ||
58 | COMP_NOTLESS | ||
59 | } WSAECOMPARATOR, *PWSAECOMPARATOR, *LPWSAECOMPARATOR; | ||
60 | |||
61 | typedef struct _WSAVersion | ||
62 | { | ||
63 | DWORD dwVersion; | ||
64 | WSAECOMPARATOR ecHow; | ||
65 | }WSAVERSION, *PWSAVERSION, *LPWSAVERSION; | ||
66 | |||
67 | typedef struct _WSAQuerySetA | ||
68 | { | ||
69 | DWORD dwSize; | ||
70 | LPSTR lpszServiceInstanceName; | ||
71 | LPGUID lpServiceClassId; | ||
72 | LPWSAVERSION lpVersion; | ||
73 | LPSTR lpszComment; | ||
74 | DWORD dwNameSpace; | ||
75 | LPGUID lpNSProviderId; | ||
76 | LPSTR lpszContext; | ||
77 | DWORD dwNumberOfProtocols; | ||
78 | LPAFPROTOCOLS lpafpProtocols; | ||
79 | LPSTR lpszQueryString; | ||
80 | DWORD dwNumberOfCsAddrs; | ||
81 | LPCSADDR_INFO lpcsaBuffer; | ||
82 | DWORD dwOutputFlags; | ||
83 | LPBLOB lpBlob; | ||
84 | } WSAQUERYSET, WSAQUERYSETA, *PWSAQUERYSETA, *LPWSAQUERYSETA; | ||
85 | |||
86 | DEFINE_GUID(L2CAP_PROTOCOL_UUID, 0x00000100, 0x0000, 0x1000, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB); | ||
87 | |||
88 | #ifndef NS_BTH | ||
89 | # define NS_BTH 16 | ||
90 | #endif | ||
91 | |||
92 | #define LUP_CONTAINERS 0x0002 | ||
93 | #define LUP_RETURN_NAME 0x0010 | ||
94 | #define LUP_RETURN_TYPE 0x0020 | ||
95 | #define LUP_RETURN_COMMENT 0x0080 | ||
96 | #define LUP_RETURN_ADDR 0x0100 | ||
97 | #define LUP_RETURN_BLOB 0x0200 | ||
98 | #define LUP_FLUSHCACHE 0x1000 | ||
99 | #define LUP_RES_SERVICE 0x8000 | ||
100 | |||
101 | #define WSAAPI FAR PASCAL | ||
102 | |||
103 | #ifndef WINSOCK_API_LINKAGE | ||
104 | #ifdef DECLSPEC_IMPORT | ||
105 | #define WINSOCK_API_LINKAGE DECLSPEC_IMPORT | ||
106 | #else | ||
107 | #define WINSOCK_API_LINKAGE | ||
108 | #endif | ||
109 | #endif | ||
110 | |||
111 | WINSOCK_API_LINKAGE INT WSAAPI | ||
112 | WSALookupServiceBeginA( | ||
113 | IN LPWSAQUERYSETA lpqsRestrictions, | ||
114 | IN DWORD dwControlFlags, | ||
115 | OUT LPHANDLE lphLookup | ||
116 | ); | ||
117 | |||
118 | #define WSALookupServiceBegin WSALookupServiceBeginA | ||
119 | |||
120 | WINSOCK_API_LINKAGE INT WSAAPI | ||
121 | WSALookupServiceNextA( | ||
122 | IN HANDLE hLookup, | ||
123 | IN DWORD dwControlFlags, | ||
124 | IN OUT LPDWORD lpdwBufferLength, | ||
125 | OUT LPWSAQUERYSETA lpqsResults | ||
126 | ); | ||
127 | |||
128 | #define WSALookupServiceNext WSALookupServiceNextA | ||
129 | |||
130 | WINSOCK_API_LINKAGE INT WSAAPI WSALookupServiceEnd(IN HANDLE hLookup); | ||
131 | |||
132 | #define MAX_PROTOCOL_CHAIN 7 | ||
133 | |||
134 | typedef struct _WSAPROTOCOLCHAIN { | ||
135 | int ChainLen; /* the length of the chain, */ | ||
136 | /* length = 0 means layered protocol, */ | ||
137 | /* length = 1 means base protocol, */ | ||
138 | /* length > 1 means protocol chain */ | ||
139 | DWORD ChainEntries[MAX_PROTOCOL_CHAIN]; /* a list of dwCatalogEntryIds */ | ||
140 | } WSAPROTOCOLCHAIN, FAR * LPWSAPROTOCOLCHAIN; | ||
141 | |||
142 | #define WSAPROTOCOL_LEN 255 | ||
143 | |||
144 | typedef struct _WSAPROTOCOL_INFOA { | ||
145 | DWORD dwServiceFlags1; | ||
146 | DWORD dwServiceFlags2; | ||
147 | DWORD dwServiceFlags3; | ||
148 | DWORD dwServiceFlags4; | ||
149 | DWORD dwProviderFlags; | ||
150 | GUID ProviderId; | ||
151 | DWORD dwCatalogEntryId; | ||
152 | WSAPROTOCOLCHAIN ProtocolChain; | ||
153 | int iVersion; | ||
154 | int iAddressFamily; | ||
155 | int iMaxSockAddr; | ||
156 | int iMinSockAddr; | ||
157 | int iSocketType; | ||
158 | int iProtocol; | ||
159 | int iProtocolMaxOffset; | ||
160 | int iNetworkByteOrder; | ||
161 | int iSecurityScheme; | ||
162 | DWORD dwMessageSize; | ||
163 | DWORD dwProviderReserved; | ||
164 | CHAR szProtocol[WSAPROTOCOL_LEN+1]; | ||
165 | } WSAPROTOCOL_INFOA, FAR * LPWSAPROTOCOL_INFOA; | ||
166 | |||
167 | typedef WSAPROTOCOL_INFOA WSAPROTOCOL_INFO; | ||
168 | |||
169 | #define SO_PROTOCOL_INFOA 0x2004 /* WSAPROTOCOL_INFOA structure */ | ||
170 | #define SO_PROTOCOL_INFO SO_PROTOCOL_INFOA | ||
171 | |||
172 | WINSOCK_API_LINKAGE INT WSAAPI | ||
173 | WSAAddressToStringA( | ||
174 | IN LPSOCKADDR lpsaAddress, | ||
175 | IN DWORD dwAddressLength, | ||
176 | IN LPWSAPROTOCOL_INFOA lpProtocolInfo, | ||
177 | IN OUT LPSTR lpszAddressString, | ||
178 | IN OUT LPDWORD lpdwAddressStringLength | ||
179 | ); | ||
180 | |||
181 | #define WSAAddressToString WSAAddressToStringA | ||
182 | |||
183 | #endif | ||
184 | #endif | ||
185 | |||
186 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
187 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
188 | */ | ||
diff --git a/gammu/emb/common/device/bluetoth/bluetoth.c b/gammu/emb/common/device/bluetoth/bluetoth.c new file mode 100644 index 0000000..c20e04b --- a/dev/null +++ b/gammu/emb/common/device/bluetoth/bluetoth.c | |||
@@ -0,0 +1,85 @@ | |||
1 | /* (c) 2003-2004 by Marcin Wiacek and Marcel Holtmann and others */ | ||
2 | |||
3 | #include "../../gsmstate.h" | ||
4 | |||
5 | #ifdef GSM_ENABLE_BLUETOOTHDEVICE | ||
6 | #ifndef DJGPP | ||
7 | |||
8 | #include <stdlib.h> | ||
9 | #include <stdio.h> | ||
10 | #include <fcntl.h> | ||
11 | #include <errno.h> | ||
12 | #include <string.h> | ||
13 | |||
14 | #include "../../misc/coding/coding.h" | ||
15 | #include "../../gsmcomon.h" | ||
16 | #include "../devfunc.h" | ||
17 | #include "bluetoth.h" | ||
18 | |||
19 | #ifdef GSM_ENABLE_BLUEZ | ||
20 | # include "bluez.h" | ||
21 | #endif | ||
22 | #ifdef GSM_ENABLE_AFFIX | ||
23 | # include "affix.h" | ||
24 | #endif | ||
25 | #ifdef WIN32 | ||
26 | # include "blue_w32.h" | ||
27 | #endif | ||
28 | |||
29 | GSM_Error bluetooth_findrfchannel(GSM_StateMachine *s) | ||
30 | { | ||
31 | GSM_Error error; | ||
32 | |||
33 | #ifdef BLUETOOTH_RF_SEARCHING | ||
34 | if (!mystrncasecmp(s->CurrentConfig->Connection, "bluerf", 6)) return bluetooth_findchannel(s); | ||
35 | #endif | ||
36 | |||
37 | switch (s->ConnectionType) { | ||
38 | case GCT_BLUEAT: | ||
39 | return bluetooth_connect(s,1,s->CurrentConfig->Device); | ||
40 | case GCT_BLUEOBEX: | ||
41 | return bluetooth_connect(s,9,s->CurrentConfig->Device); | ||
42 | case GCT_BLUEPHONET: | ||
43 | error = bluetooth_connect(s,14,s->CurrentConfig->Device); //older Series 40 - 8910, 6310 | ||
44 | if (error == ERR_NONE) return error; | ||
45 | return bluetooth_connect(s,15,s->CurrentConfig->Device); //new Series 40 - 6310i, 6230 | ||
46 | default: | ||
47 | return ERR_UNKNOWN; | ||
48 | } | ||
49 | } | ||
50 | |||
51 | static int bluetooth_read(GSM_StateMachine *s, void *buf, size_t nbytes) | ||
52 | { | ||
53 | return socket_read(s, buf, nbytes, s->Device.Data.BlueTooth.hPhone); | ||
54 | } | ||
55 | |||
56 | #ifdef WIN32 | ||
57 | static int bluetooth_write(GSM_StateMachine *s, unsigned char *buf, size_t nbytes) | ||
58 | #else | ||
59 | static int bluetooth_write(GSM_StateMachine *s, void *buf, size_t nbytes) | ||
60 | #endif | ||
61 | { | ||
62 | return socket_write(s, buf, nbytes, s->Device.Data.BlueTooth.hPhone); | ||
63 | } | ||
64 | |||
65 | static GSM_Error bluetooth_close(GSM_StateMachine *s) | ||
66 | { | ||
67 | return socket_close(s, s->Device.Data.BlueTooth.hPhone); | ||
68 | } | ||
69 | |||
70 | GSM_Device_Functions BlueToothDevice = { | ||
71 | bluetooth_findrfchannel, | ||
72 | bluetooth_close, | ||
73 | NONEFUNCTION, | ||
74 | NONEFUNCTION, | ||
75 | NONEFUNCTION, | ||
76 | bluetooth_read, | ||
77 | bluetooth_write | ||
78 | }; | ||
79 | |||
80 | #endif | ||
81 | #endif | ||
82 | |||
83 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
84 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
85 | */ | ||
diff --git a/gammu/emb/common/device/bluetoth/bluetoth.h b/gammu/emb/common/device/bluetoth/bluetoth.h new file mode 100644 index 0000000..8a7fdf4 --- a/dev/null +++ b/gammu/emb/common/device/bluetoth/bluetoth.h | |||
@@ -0,0 +1,15 @@ | |||
1 | |||
2 | #ifndef DJGPP | ||
3 | #ifndef unixbluetooth_h | ||
4 | #define unixbluetooth_h | ||
5 | |||
6 | typedef struct { | ||
7 | int hPhone; | ||
8 | } GSM_Device_BlueToothData; | ||
9 | |||
10 | #endif | ||
11 | #endif | ||
12 | |||
13 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
14 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
15 | */ | ||
diff --git a/gammu/emb/common/device/bluetoth/bluez.c b/gammu/emb/common/device/bluetoth/bluez.c new file mode 100644 index 0000000..8a4807e --- a/dev/null +++ b/gammu/emb/common/device/bluetoth/bluez.c | |||
@@ -0,0 +1,207 @@ | |||
1 | /* Based on work by Marcel Holtmann and other authors of Bluez */ | ||
2 | |||
3 | #include "../../gsmstate.h" | ||
4 | |||
5 | #ifdef GSM_ENABLE_BLUETOOTHDEVICE | ||
6 | #ifdef GSM_ENABLE_BLUEZ | ||
7 | |||
8 | #include <stdlib.h> | ||
9 | #include <stdio.h> | ||
10 | #include <fcntl.h> | ||
11 | #include <errno.h> | ||
12 | #include <string.h> | ||
13 | #include <sys/socket.h> | ||
14 | #include <sys/time.h> | ||
15 | #include <unistd.h> | ||
16 | #include <bluetooth/bluetooth.h> | ||
17 | #include <bluetooth/rfcomm.h> | ||
18 | #include <bluetooth/sdp.h> | ||
19 | #include <bluetooth/sdp_lib.h> | ||
20 | |||
21 | #include "../../gsmcomon.h" | ||
22 | #include "../devfunc.h" | ||
23 | #include "bluetoth.h" | ||
24 | |||
25 | GSM_Error bluetooth_connect(GSM_StateMachine *s, int port, char *device) | ||
26 | { | ||
27 | GSM_Device_BlueToothData *d = &s->Device.Data.BlueTooth; | ||
28 | struct sockaddr_rc laddr, raddr; | ||
29 | bdaddr_t bdaddr; | ||
30 | int fd; | ||
31 | |||
32 | smprintf(s, "Connecting to RF channel %i\n",port); | ||
33 | |||
34 | fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); | ||
35 | if (fd < 0) { | ||
36 | dbgprintf("Can't create socket\n"); | ||
37 | return ERR_DEVICENODRIVER; | ||
38 | } | ||
39 | |||
40 | bacpy(&laddr.rc_bdaddr, BDADDR_ANY); | ||
41 | laddr.rc_family = AF_BLUETOOTH; | ||
42 | laddr.rc_channel = 0; | ||
43 | |||
44 | if (bind(fd, (struct sockaddr *)&laddr, sizeof(laddr)) < 0) { | ||
45 | dbgprintf("Can't bind socket\n"); | ||
46 | close(fd); | ||
47 | return ERR_DEVICEOPENERROR; | ||
48 | } | ||
49 | |||
50 | str2ba(device, &bdaddr); | ||
51 | bacpy(&raddr.rc_bdaddr, &bdaddr); | ||
52 | raddr.rc_family = AF_BLUETOOTH; | ||
53 | raddr.rc_channel = port; | ||
54 | |||
55 | if (connect(fd, (struct sockaddr *)&raddr, sizeof(raddr)) < 0) { | ||
56 | dbgprintf("Can't connect\n"); | ||
57 | close(fd); | ||
58 | return ERR_DEVICEOPENERROR; | ||
59 | } | ||
60 | |||
61 | d->hPhone = fd; | ||
62 | return ERR_NONE; | ||
63 | } | ||
64 | |||
65 | #ifdef BLUETOOTH_RF_SEARCHING | ||
66 | |||
67 | struct search_context { | ||
68 | char *svc; | ||
69 | uuid_t group; | ||
70 | int tree; | ||
71 | uint32_t handle; | ||
72 | }; | ||
73 | |||
74 | static void print_service_desc(void *value, void *user) | ||
75 | { | ||
76 | sdp_data_t *p = (sdp_data_t *)value; | ||
77 | int i = 0, proto = 0, *channel = (int *)user; | ||
78 | |||
79 | for (; p; p = p->next, i++) { | ||
80 | switch (p->dtd) { | ||
81 | case SDP_UUID16: | ||
82 | case SDP_UUID32: | ||
83 | case SDP_UUID128: | ||
84 | proto = 1;//sdp_uuid_to_proto(&p->val.uuid); | ||
85 | break; | ||
86 | case SDP_UINT8: | ||
87 | if (proto == RFCOMM_UUID) { | ||
88 | (*channel) = p->val.uint8; | ||
89 | return; | ||
90 | } | ||
91 | break; | ||
92 | } | ||
93 | } | ||
94 | } | ||
95 | |||
96 | void print_access_protos(value, user) | ||
97 | { | ||
98 | sdp_list_t *protDescSeq = (sdp_list_t *)value; | ||
99 | int *channel = (int *)user; | ||
100 | |||
101 | sdp_list_foreach(protDescSeq,print_service_desc,channel); | ||
102 | } | ||
103 | |||
104 | static GSM_Error bluetooth_checkdevice(GSM_StateMachine *s, bdaddr_t *bdaddr, struct search_context *context) | ||
105 | { | ||
106 | sdp_session_t *sess; | ||
107 | sdp_list_t *attrid, *search, *seq, *next, *proto = 0; | ||
108 | uint32_t range = 0x0000ffff; | ||
109 | char str[20]; | ||
110 | sdp_record_t *rec; | ||
111 | sdp_data_t *d; | ||
112 | bdaddr_t interface; | ||
113 | struct search_context subcontext; | ||
114 | int channel,channel2; | ||
115 | |||
116 | bacpy(&interface,BDADDR_ANY); | ||
117 | |||
118 | ba2str(bdaddr, str); | ||
119 | smprintf(s,"%s\n", str); | ||
120 | |||
121 | sess = sdp_connect(&interface, bdaddr, SDP_RETRY_IF_BUSY); | ||
122 | if (!sess) { | ||
123 | dbgprintf("Failed to connect to SDP server on %s: %s\n", str, strerror(errno)); | ||
124 | return ERR_UNKNOWN; | ||
125 | } | ||
126 | |||
127 | attrid = sdp_list_append(0, &range); | ||
128 | search = sdp_list_append(0, &context->group); | ||
129 | if (sdp_service_search_attr_req(sess, search, SDP_ATTR_REQ_RANGE, attrid, &seq)) { | ||
130 | dbgprintf("Service Search failed: %s\n", strerror(errno)); | ||
131 | sdp_close(sess); | ||
132 | return ERR_UNKNOWN; | ||
133 | } | ||
134 | sdp_list_free(attrid, 0); | ||
135 | sdp_list_free(search, 0); | ||
136 | |||
137 | channel2 = -1; | ||
138 | for (; seq; seq = next) { | ||
139 | rec = (sdp_record_t *) seq->data; | ||
140 | |||
141 | if (channel2 == -1) { | ||
142 | if (!context->tree) { | ||
143 | d = sdp_data_get(rec,SDP_ATTR_SVCNAME_PRIMARY); | ||
144 | |||
145 | if (false) { | ||
146 | channel = -1; | ||
147 | sdp_list_foreach(proto,print_access_protos,&channel); | ||
148 | //sdp_list_free(proto,(sdp_free_func_t)sdp_data_free); | ||
149 | } | ||
150 | smprintf(s,"Channel %i",channel); | ||
151 | if (d) smprintf(s," - \"%s\"",d->val.str); | ||
152 | smprintf(s,"\n"); | ||
153 | if (channel2 == -1 && bluetooth_checkservicename(s, d->val.str) == ERR_NONE) { | ||
154 | channel2 = channel; | ||
155 | } | ||
156 | } | ||
157 | if (sdp_get_group_id(rec,&subcontext.group) != -1) { | ||
158 | memcpy(&subcontext, context, sizeof(struct search_context)); | ||
159 | if (subcontext.group.value.uuid16 != context->group.value.uuid16) bluetooth_checkdevice(s,bdaddr,&subcontext); | ||
160 | } | ||
161 | } | ||
162 | |||
163 | next = seq->next; | ||
164 | free(seq); | ||
165 | //sdp_record_free(rec); | ||
166 | } | ||
167 | sdp_close(sess); | ||
168 | |||
169 | if (channel2 != -1) return bluetooth_connect(s, channel2, str); | ||
170 | |||
171 | return ERR_UNKNOWN; | ||
172 | } | ||
173 | |||
174 | GSM_Error bluetooth_findchannel(GSM_StateMachine *s) | ||
175 | { | ||
176 | inquiry_info ii[20]; | ||
177 | uint8_t count = 0; | ||
178 | int i; | ||
179 | struct search_context context; | ||
180 | GSM_Error error = ERR_NOTSUPPORTED; | ||
181 | |||
182 | memset(&context, '\0', sizeof(struct search_context)); | ||
183 | //sdp_uuid16_create(&(context.group),PUBLIC_BROWSE_GROUP); | ||
184 | |||
185 | if (!strcmp(s->CurrentConfig->Device,"/dev/ttyS1")) { | ||
186 | dbgprintf("Searching for devices\n"); | ||
187 | if (sdp_general_inquiry(ii, 20, 8, &count) < 0) { | ||
188 | return ERR_UNKNOWN; | ||
189 | } | ||
190 | } else { | ||
191 | count = 1; | ||
192 | str2ba(s->CurrentConfig->Device,&ii[0].bdaddr); | ||
193 | } | ||
194 | for (i=0;i<count;i++) { | ||
195 | error = bluetooth_checkdevice(s,&ii[i].bdaddr,&context); | ||
196 | if (error == ERR_NONE) return error; | ||
197 | } | ||
198 | return error; | ||
199 | } | ||
200 | |||
201 | #endif | ||
202 | #endif | ||
203 | #endif | ||
204 | |||
205 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
206 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
207 | */ | ||
diff --git a/gammu/emb/common/device/bluetoth/bluez.h b/gammu/emb/common/device/bluetoth/bluez.h new file mode 100644 index 0000000..896ecbe --- a/dev/null +++ b/gammu/emb/common/device/bluetoth/bluez.h | |||
@@ -0,0 +1,3 @@ | |||
1 | |||
2 | GSM_Error bluetooth_connect(GSM_StateMachine *s, int port, char *device); | ||
3 | GSM_Error bluetooth_findchannel(GSM_StateMachine *s); | ||
diff --git a/gammu/emb/common/device/devfunc.c b/gammu/emb/common/device/devfunc.c new file mode 100644 index 0000000..d31ebbf --- a/dev/null +++ b/gammu/emb/common/device/devfunc.c | |||
@@ -0,0 +1,266 @@ | |||
1 | |||
2 | #include <string.h> | ||
3 | #ifdef WIN32 | ||
4 | # include <io.h> | ||
5 | #else | ||
6 | # include <errno.h> | ||
7 | # include <signal.h> | ||
8 | #endif | ||
9 | |||
10 | #include "../gsmstate.h" | ||
11 | |||
12 | #ifdef GSM_ENABLE_BLUETOOTHDEVICE | ||
13 | #ifdef BLUETOOTH_RF_SEARCHING | ||
14 | |||
15 | GSM_Error bluetooth_checkservicename(GSM_StateMachine *s, char *name) | ||
16 | { | ||
17 | if (s->ConnectionType == GCT_BLUEPHONET && strstr(name,"Nokia PC Suite")!=NULL) return ERR_NONE; | ||
18 | if (s->ConnectionType == GCT_BLUEOBEX && strstr(name,"OBEX") !=NULL) return ERR_NONE; | ||
19 | if (s->ConnectionType == GCT_BLUEAT && strstr(name,"COM 1") !=NULL) return ERR_NONE; | ||
20 | return ERR_UNKNOWN; | ||
21 | } | ||
22 | |||
23 | #endif | ||
24 | #endif | ||
25 | |||
26 | #if defined (GSM_ENABLE_BLUETOOTHDEVICE) || defined (GSM_ENABLE_IRDADEVICE) | ||
27 | |||
28 | int socket_read(GSM_StateMachine *s, void *buf, size_t nbytes, int hPhone) | ||
29 | { | ||
30 | fd_set readfds; | ||
31 | #ifdef WIN32 | ||
32 | struct timeval timer; | ||
33 | #endif | ||
34 | |||
35 | FD_ZERO(&readfds); | ||
36 | FD_SET(hPhone, &readfds); | ||
37 | #ifndef WIN32 | ||
38 | if (select(hPhone+1, &readfds, NULL, NULL, 0)) { | ||
39 | return(read(hPhone, buf, nbytes)); | ||
40 | } | ||
41 | #else | ||
42 | memset(&timer,0,sizeof(timer)); | ||
43 | if (select(0, &readfds, NULL, NULL, &timer) != 0) { | ||
44 | return(recv(hPhone, buf, nbytes, 0)); | ||
45 | } | ||
46 | #endif | ||
47 | return 0; | ||
48 | } | ||
49 | |||
50 | #ifdef WIN32 | ||
51 | int socket_write(GSM_StateMachine *s, unsigned char *buf, size_t nbytes, int hPhone) | ||
52 | #else | ||
53 | int socket_write(GSM_StateMachine *s, void *buf, size_t nbytes, int hPhone) | ||
54 | #endif | ||
55 | { | ||
56 | int ret; | ||
57 | size_t actual = 0; | ||
58 | |||
59 | do { | ||
60 | ret = send(hPhone, buf, nbytes - actual, 0); | ||
61 | if (ret < 0) { | ||
62 | if (actual != nbytes) GSM_OSErrorInfo(s,"socket_write"); | ||
63 | return actual; | ||
64 | } | ||
65 | actual += ret; | ||
66 | buf += ret; | ||
67 | } while (actual < nbytes); | ||
68 | |||
69 | return actual; | ||
70 | } | ||
71 | |||
72 | GSM_Error socket_close(GSM_StateMachine *s, int hPhone) | ||
73 | { | ||
74 | shutdown(hPhone, 0); | ||
75 | #ifdef WIN32 | ||
76 | closesocket(hPhone); /*FIXME: error checking */ | ||
77 | #else | ||
78 | close(hPhone); /*FIXME: error checking */ | ||
79 | #endif | ||
80 | return ERR_NONE; | ||
81 | } | ||
82 | |||
83 | #endif | ||
84 | |||
85 | #ifdef ENABLE_LGPL | ||
86 | |||
87 | GSM_Error lock_device(const char* port, char **lock_device) | ||
88 | { | ||
89 | *lock_device = 0; | ||
90 | return ERR_NONE; | ||
91 | } | ||
92 | |||
93 | bool unlock_device(char **lock_file) | ||
94 | { | ||
95 | return true; | ||
96 | } | ||
97 | |||
98 | #else | ||
99 | |||
100 | #define max_buf_len 128 | ||
101 | #define lock_path "/var/lock/LCK.." | ||
102 | |||
103 | /* Lock the device. Allocated string with a lock name is returned | ||
104 | * in lock_device | ||
105 | */ | ||
106 | GSM_Error lock_device(const char* port, char **lock_device) | ||
107 | { | ||
108 | #ifndef WIN32 | ||
109 | char *lock_file = NULL; | ||
110 | char buffer[max_buf_len]; | ||
111 | const char *aux; | ||
112 | int fd, len; | ||
113 | GSM_Errorerror = ERR_NONE; | ||
114 | |||
115 | dbgprintf("Locking device\n"); | ||
116 | |||
117 | aux = strrchr(port, '/'); | ||
118 | /* Remove leading '/' */ | ||
119 | if (aux) { | ||
120 | aux++; | ||
121 | } else { | ||
122 | /* No / in port */ | ||
123 | aux = port; | ||
124 | } | ||
125 | len = strlen(aux) + strlen(lock_path); | ||
126 | |||
127 | memset(buffer, 0, sizeof(buffer)); | ||
128 | lock_file = calloc(len + 1, 1); | ||
129 | if (!lock_file) { | ||
130 | dbgprintf("Out of memory error while locking device\n"); | ||
131 | return ERR_MOREMEMORY; | ||
132 | } | ||
133 | /* I think we don't need to use strncpy, as we should have enough | ||
134 | * buffer due to strlen results | ||
135 | */ | ||
136 | strcpy(lock_file, lock_path); | ||
137 | strcat(lock_file, aux); | ||
138 | |||
139 | /* Check for the stale lockfile. | ||
140 | * The code taken from minicom by Miquel van Smoorenburg */ | ||
141 | if ((fd = open(lock_file, O_RDONLY)) >= 0) { | ||
142 | char buf[max_buf_len]; | ||
143 | int pid, n = 0; | ||
144 | |||
145 | n = read(fd, buf, sizeof(buf) - 1); | ||
146 | close(fd); | ||
147 | if (n > 0) { | ||
148 | pid = -1; | ||
149 | if (n == 4) | ||
150 | /* Kermit-style lockfile. */ | ||
151 | pid = *(int *)buf; | ||
152 | else { | ||
153 | /* Ascii lockfile. */ | ||
154 | buf[n] = 0; | ||
155 | sscanf(buf, "%d", &pid); | ||
156 | } | ||
157 | if (pid > 0 && kill((pid_t)pid, 0) < 0 && errno == ESRCH) { | ||
158 | dbgprintf("Lockfile %s is stale. Overriding it..\n", lock_file); | ||
159 | sleep(1); | ||
160 | if (unlink(lock_file) == -1) { | ||
161 | dbgprintf("Overriding failed, please check the permissions\n"); | ||
162 | dbgprintf("Cannot lock device\n"); | ||
163 | error = ERR_PERMISSION; | ||
164 | goto failed; | ||
165 | } | ||
166 | } else { | ||
167 | dbgprintf("Device already locked by PID %d.\n", pid); | ||
168 | error = ERR_DEVICELOCKED; | ||
169 | goto failed; | ||
170 | } | ||
171 | } | ||
172 | /* this must not happen. because we could open the file */ | ||
173 | /* no wrong permissions are set. only reason could be */ | ||
174 | /* flock/lockf or a empty lockfile due to a broken binary */ | ||
175 | /* which is more likely */ | ||
176 | if (n == 0) { | ||
177 | dbgprintf("Unable to read lockfile %s.\n", lock_file); | ||
178 | dbgprintf("Please check for reason and remove the lockfile by hand.\n"); | ||
179 | dbgprintf("Cannot lock device\n"); | ||
180 | error = ERR_UNKNOWN; | ||
181 | goto failed; | ||
182 | } | ||
183 | } | ||
184 | |||
185 | /* Try to create a new file, with 0644 mode */ | ||
186 | fd = open(lock_file, O_CREAT | O_EXCL | O_WRONLY, 0644); | ||
187 | if (fd == -1) { | ||
188 | if (errno == EEXIST) { | ||
189 | dbgprintf("Device seems to be locked by unknown process\n"); | ||
190 | error = ERR_DEVICEOPENERROR; | ||
191 | } else if (errno == EACCES) { | ||
192 | dbgprintf("Please check permission on lock directory\n"); | ||
193 | error = ERR_PERMISSION; | ||
194 | } else if (errno == ENOENT) { | ||
195 | dbgprintf("Cannot create lockfile %s. Please check for existence of path\n", lock_file); | ||
196 | error = ERR_UNKNOWN; | ||
197 | } else { | ||
198 | dbgprintf("Unknown error with creating lockfile %s\n", lock_file); | ||
199 | error = ERR_UNKNOWN; | ||
200 | } | ||
201 | goto failed; | ||
202 | } | ||
203 | sprintf(buffer, "%10ld gammu\n", (long)getpid()); | ||
204 | write(fd, buffer, strlen(buffer)); | ||
205 | close(fd); | ||
206 | *lock_device = lock_file; | ||
207 | return ERR_NONE; | ||
208 | failed: | ||
209 | free(lock_file); | ||
210 | *lock_device = 0; | ||
211 | return error; | ||
212 | #else | ||
213 | *lock_device = 0; | ||
214 | return ERR_NONE; | ||
215 | #endif | ||
216 | } | ||
217 | |||
218 | /* Removes lock and frees memory */ | ||
219 | bool unlock_device(char **lock_file) | ||
220 | { | ||
221 | #ifndef WIN32 | ||
222 | int err; | ||
223 | |||
224 | if (!lock_file) { | ||
225 | dbgprintf("Cannot unlock device\n"); | ||
226 | return false; | ||
227 | } | ||
228 | err = unlink(*lock_file); | ||
229 | free(*lock_file); | ||
230 | *lock_file = NULL; | ||
231 | return (err + 1); | ||
232 | #else | ||
233 | return true; | ||
234 | #endif | ||
235 | } | ||
236 | |||
237 | #endif | ||
238 | |||
239 | int FindSerialSpeed(char *buffer) | ||
240 | { | ||
241 | switch (atoi(buffer)) { | ||
242 | case 50 : return 50; | ||
243 | case 75 : return 75; | ||
244 | case 110: return 110; | ||
245 | case 134: return 134; | ||
246 | case 150: return 150; | ||
247 | case 200: return 200; | ||
248 | case 300: return 300; | ||
249 | case 600: return 600; | ||
250 | case 1200: return 1200; | ||
251 | case 1800: return 1800; | ||
252 | case 2400: return 2400; | ||
253 | case 4800: return 4800; | ||
254 | case 9600: return 9600; | ||
255 | case 19200: return 19200; | ||
256 | case 38400: return 38400; | ||
257 | case 57600: return 57600; | ||
258 | case 115200: return 115200; | ||
259 | case 230400: return 230400; | ||
260 | default : return 0; | ||
261 | } | ||
262 | } | ||
263 | |||
264 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
265 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
266 | */ | ||
diff --git a/gammu/emb/common/device/devfunc.h b/gammu/emb/common/device/devfunc.h new file mode 100644 index 0000000..6e8fb62 --- a/dev/null +++ b/gammu/emb/common/device/devfunc.h | |||
@@ -0,0 +1,38 @@ | |||
1 | |||
2 | #ifndef device_functions_h | ||
3 | #define device_functions_h | ||
4 | |||
5 | #include "../gsmstate.h" | ||
6 | |||
7 | #ifdef GSM_ENABLE_BLUETOOTHDEVICE | ||
8 | #ifdef BLUETOOTH_RF_SEARCHING | ||
9 | |||
10 | GSM_Error bluetooth_checkservicename(GSM_StateMachine *s, char *name); | ||
11 | |||
12 | #endif | ||
13 | #endif | ||
14 | |||
15 | #if defined (GSM_ENABLE_BLUETOOTHDEVICE) || defined (GSM_ENABLE_IRDADEVICE) | ||
16 | |||
17 | int socket_read(GSM_StateMachine *s, void *buf, size_t nbytes, int hPhone); | ||
18 | |||
19 | #ifdef WIN32 | ||
20 | int socket_write(GSM_StateMachine *s, unsigned char *buf, size_t nbytes, int hPhone); | ||
21 | #else | ||
22 | int socket_write(GSM_StateMachine *s, void *buf, size_t nbytes, int hPhone); | ||
23 | #endif | ||
24 | |||
25 | GSM_Error socket_close(GSM_StateMachine *s, int hPhone); | ||
26 | |||
27 | #endif | ||
28 | |||
29 | GSM_Error lock_device(const char* port, char **lock_device); | ||
30 | bool unlock_device(char **lock_file); | ||
31 | |||
32 | int FindSerialSpeed(char *buffer); | ||
33 | |||
34 | #endif | ||
35 | |||
36 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
37 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
38 | */ | ||
diff --git a/gammu/emb/common/device/irda/irda.c b/gammu/emb/common/device/irda/irda.c new file mode 100644 index 0000000..fef50ac --- a/dev/null +++ b/gammu/emb/common/device/irda/irda.c | |||
@@ -0,0 +1,187 @@ | |||
1 | /* (c) 2001-2004 by Marcin Wiacek */ | ||
2 | /* based on some work from Ralf Thelen and MyGnokii */ | ||
3 | /* based on some work from Gnokii and MSDN */ | ||
4 | |||
5 | /* You have to include wsock32.lib library to MS VC project to compile it */ | ||
6 | |||
7 | #include "../../gsmstate.h" | ||
8 | |||
9 | #ifdef GSM_ENABLE_IRDADEVICE | ||
10 | #ifndef DJGPP | ||
11 | |||
12 | #ifndef WIN32 | ||
13 | # include <stdlib.h> | ||
14 | # include <unistd.h> | ||
15 | # include <stdio.h> | ||
16 | # include <fcntl.h> | ||
17 | # include <errno.h> | ||
18 | # include <string.h> | ||
19 | # include <sys/time.h> | ||
20 | # include <sys/poll.h> | ||
21 | # include <sys/socket.h> | ||
22 | # include <sys/ioctl.h> | ||
23 | #else | ||
24 | # include <windows.h> | ||
25 | # include <io.h> | ||
26 | #endif | ||
27 | |||
28 | #include "../../gsmcomon.h" | ||
29 | #include "../devfunc.h" | ||
30 | #include "irda.h" | ||
31 | |||
32 | static bool irda_discover_device(GSM_StateMachine *state) | ||
33 | { | ||
34 | GSM_Device_IrdaData *d = &state->Device.Data.Irda; | ||
35 | struct irda_device_list*list; | ||
36 | unsigned char *buf; | ||
37 | unsigned int sec; | ||
38 | int s, z, len, fd, i; | ||
39 | GSM_DateTime Date; | ||
40 | bool founddevice = false; | ||
41 | #ifdef WIN32 | ||
42 | int index; | ||
43 | #endif | ||
44 | |||
45 | fd = socket(AF_IRDA, SOCK_STREAM, 0); | ||
46 | |||
47 | /* can handle maximally 10 devices during discovering */ | ||
48 | len = sizeof(struct irda_device_list) + sizeof(struct irda_device_info) * 10; | ||
49 | buf = malloc(len); | ||
50 | list = (struct irda_device_list *)buf; | ||
51 | |||
52 | /* Trying to find device during 2 seconds */ | ||
53 | for (z=0;z<2;z++) { | ||
54 | GSM_GetCurrentDateTime (&Date); | ||
55 | sec = Date.Second; | ||
56 | while (sec==Date.Second) { | ||
57 | s = len; | ||
58 | memset(buf, 0, s); | ||
59 | |||
60 | if (getsockopt(fd, SOL_IRLMP, IRLMP_ENUMDEVICES, buf, &s) == 0) { | ||
61 | for (i = 0; i < (int)list->numDevice; i++) { | ||
62 | dbgprintf("Irda: found device \"%s\" (address %x) - ",list->Device[i].irdaDeviceName,list->Device[i].irdaDeviceID); | ||
63 | if (strcmp(GetModelData(NULL,NULL,list->Device[i].irdaDeviceName)->number,"") != 0) { | ||
64 | founddevice = true; | ||
65 | /* Model AUTO */ | ||
66 | if (state->CurrentConfig->Model[0]==0) strcpy(state->Phone.Data.Model,GetModelData(NULL,NULL,list->Device[i].irdaDeviceName)->number); | ||
67 | state->Phone.Data.ModelInfo = GetModelData(NULL,state->Phone.Data.Model,NULL); | ||
68 | } | ||
69 | if (founddevice) { | ||
70 | dbgprintf("correct\n"); | ||
71 | #ifdef WIN32 | ||
72 | for(index=0; index <= 3; index++) | ||
73 | d->peer.irdaDeviceID[index] = list->Device[i].irdaDeviceID[index]; | ||
74 | #else | ||
75 | d->peer.irdaDeviceID = list->Device[i].irdaDeviceID; | ||
76 | #endif | ||
77 | break; | ||
78 | } | ||
79 | dbgprintf("\n"); | ||
80 | } | ||
81 | } | ||
82 | if (founddevice) break; | ||
83 | my_sleep(10); | ||
84 | GSM_GetCurrentDateTime(&Date); | ||
85 | } | ||
86 | if (founddevice) break; | ||
87 | } | ||
88 | free(buf); | ||
89 | close(fd); | ||
90 | |||
91 | return founddevice; | ||
92 | } | ||
93 | |||
94 | static GSM_Error irda_open (GSM_StateMachine *s) | ||
95 | { | ||
96 | GSM_Device_IrdaData *d = &s->Device.Data.Irda; | ||
97 | int fd = -1; | ||
98 | #ifdef WIN32 | ||
99 | int Enable9WireMode = 1; | ||
100 | WSADATA wsaData; | ||
101 | |||
102 | WSAStartup(MAKEWORD(1,1), &wsaData); | ||
103 | #else | ||
104 | if (s->ConnectionType == GCT_IRDAAT) return ERR_SOURCENOTAVAILABLE; | ||
105 | #endif | ||
106 | |||
107 | /* discovering devices */ | ||
108 | if (irda_discover_device(s)==false) return ERR_TIMEOUT; | ||
109 | |||
110 | /* Creating socket */ | ||
111 | fd = socket(AF_IRDA, SOCK_STREAM, 0); | ||
112 | |||
113 | d->peer.irdaAddressFamily = AF_IRDA; | ||
114 | #ifndef WIN32 | ||
115 | d->peer.sir_lsap_sel = LSAP_ANY; | ||
116 | #endif | ||
117 | switch (s->ConnectionType) { | ||
118 | case GCT_IRDAAT: | ||
119 | strcpy(d->peer.irdaServiceName, "IrDA:IrCOMM"); | ||
120 | |||
121 | #ifdef WIN32 | ||
122 | if (setsockopt(fd, SOL_IRLMP, IRLMP_9WIRE_MODE, (const char *) &Enable9WireMode, | ||
123 | sizeof(int))==SOCKET_ERROR) return ERR_UNKNOWN; | ||
124 | #endif | ||
125 | break; | ||
126 | case GCT_IRDAPHONET: | ||
127 | strcpy(d->peer.irdaServiceName, "Nokia:PhoNet"); | ||
128 | break; | ||
129 | case GCT_IRDAOBEX: | ||
130 | /* IrDA:OBEX not supported by N3650 */ | ||
131 | // strcpy(d->peer.irdaServiceName, "IrDA:OBEX"); | ||
132 | |||
133 | strcpy(d->peer.irdaServiceName, "OBEX"); | ||
134 | |||
135 | /* Alternative server is "OBEX:IrXfer" */ | ||
136 | break; | ||
137 | default: | ||
138 | return ERR_UNKNOWN; | ||
139 | } | ||
140 | |||
141 | /* Connecting to service */ | ||
142 | if (connect(fd, (struct sockaddr *)&d->peer, sizeof(d->peer))) { | ||
143 | dbgprintf("Can't connect to service %s\n",d->peer.irdaServiceName); | ||
144 | close(fd); | ||
145 | return ERR_NOTSUPPORTED; | ||
146 | } | ||
147 | |||
148 | d->hPhone=fd; | ||
149 | |||
150 | return ERR_NONE; | ||
151 | } | ||
152 | |||
153 | static int irda_read(GSM_StateMachine *s, void *buf, size_t nbytes) | ||
154 | { | ||
155 | return socket_read(s, buf, nbytes, s->Device.Data.Irda.hPhone); | ||
156 | } | ||
157 | |||
158 | #ifdef WIN32 | ||
159 | static int irda_write(GSM_StateMachine *s, unsigned char *buf, size_t nbytes) | ||
160 | #else | ||
161 | static int irda_write(GSM_StateMachine *s, void *buf, size_t nbytes) | ||
162 | #endif | ||
163 | { | ||
164 | return socket_write(s, buf, nbytes, s->Device.Data.Irda.hPhone); | ||
165 | } | ||
166 | |||
167 | static GSM_Error irda_close(GSM_StateMachine *s) | ||
168 | { | ||
169 | return socket_close(s, s->Device.Data.Irda.hPhone); | ||
170 | } | ||
171 | |||
172 | GSM_Device_Functions IrdaDevice = { | ||
173 | irda_open, | ||
174 | irda_close, | ||
175 | NONEFUNCTION, | ||
176 | NONEFUNCTION, | ||
177 | NONEFUNCTION, | ||
178 | irda_read, | ||
179 | irda_write | ||
180 | }; | ||
181 | |||
182 | #endif | ||
183 | #endif | ||
184 | |||
185 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
186 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
187 | */ | ||
diff --git a/gammu/emb/common/device/irda/irda.h b/gammu/emb/common/device/irda/irda.h new file mode 100644 index 0000000..455e6af --- a/dev/null +++ b/gammu/emb/common/device/irda/irda.h | |||
@@ -0,0 +1,22 @@ | |||
1 | |||
2 | #ifndef DJGPP | ||
3 | #ifndef unixirda_h | ||
4 | #define unixirda_h | ||
5 | |||
6 | #ifndef WIN32 | ||
7 | # include "irda_unx.h" | ||
8 | #else | ||
9 | # include "irda_w32.h" | ||
10 | #endif | ||
11 | |||
12 | typedef struct { | ||
13 | int hPhone; | ||
14 | struct sockaddr_irdapeer; | ||
15 | } GSM_Device_IrdaData; | ||
16 | |||
17 | #endif | ||
18 | #endif | ||
19 | |||
20 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
21 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
22 | */ | ||
diff --git a/gammu/emb/common/device/irda/irda_unx.h b/gammu/emb/common/device/irda/irda_unx.h new file mode 100644 index 0000000..8dbcb97 --- a/dev/null +++ b/gammu/emb/common/device/irda/irda_unx.h | |||
@@ -0,0 +1,61 @@ | |||
1 | /* part of irda.h available in Linux kernel source */ | ||
2 | |||
3 | /********************************************************************* | ||
4 | * | ||
5 | * Filename: irda.h | ||
6 | * Version: | ||
7 | * Description: | ||
8 | * Status: Experimental. | ||
9 | * Author: Dag Brattli <dagb@cs.uit.no> | ||
10 | * Created at: Mon Mar 8 14:06:12 1999 | ||
11 | * Modified at: Sat Dec 25 16:06:42 1999 | ||
12 | * Modified by: Dag Brattli <dagb@cs.uit.no> | ||
13 | * | ||
14 | * Copyright (c) 1999 Dag Brattli, All Rights Reserved. | ||
15 | * | ||
16 | * This program is free software; you can redistribute it and/or | ||
17 | * modify it under the terms of the GNU General Public License as | ||
18 | * published by the Free Software Foundation; either version 2 of | ||
19 | * the License, or (at your option) any later version. | ||
20 | * | ||
21 | * Neither Dag Brattli nor University of Tromsø admit liability nor | ||
22 | * provide warranty for any of this software. This material is | ||
23 | * provided "AS-IS" and at no charge. | ||
24 | * | ||
25 | ********************************************************************/ | ||
26 | |||
27 | #ifndef __irda_unx_h | ||
28 | #define __irda_unx_h | ||
29 | |||
30 | #include <sys/types.h> | ||
31 | #include <sys/socket.h> | ||
32 | |||
33 | #define SOL_IRLMP 266 /* Same as SOL_IRDA for now */ | ||
34 | #define IRLMP_ENUMDEVICES 1 /* Return discovery log */ | ||
35 | #define LSAP_ANY 0xff | ||
36 | |||
37 | struct sockaddr_irda { | ||
38 | sa_family_t irdaAddressFamily;/* AF_IRDA */ | ||
39 | u_int8_t sir_lsap_sel; /* LSAP selector */ | ||
40 | u_int32_t irdaDeviceID; /* Device address */ | ||
41 | char irdaServiceName[25]; /* Usually <service>:IrDA:TinyTP */ | ||
42 | }; | ||
43 | |||
44 | struct irda_device_info { | ||
45 | u_int32_t saddr; /* Address of local interface */ | ||
46 | u_int32_t irdaDeviceID; /* Address of remote device */ | ||
47 | char irdaDeviceName[22]; /* Description */ | ||
48 | u_int8_t charset; /* Charset used for description */ | ||
49 | u_int8_t hints[2]; /* Hint bits */ | ||
50 | }; | ||
51 | |||
52 | struct irda_device_list { | ||
53 | u_int32_t numDevice; | ||
54 | struct irda_device_info Device[1]; | ||
55 | }; | ||
56 | |||
57 | #endif | ||
58 | |||
59 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
60 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
61 | */ | ||
diff --git a/gammu/emb/common/device/irda/irda_w32.h b/gammu/emb/common/device/irda/irda_w32.h new file mode 100644 index 0000000..daffa37 --- a/dev/null +++ b/gammu/emb/common/device/irda/irda_w32.h | |||
@@ -0,0 +1,35 @@ | |||
1 | |||
2 | /* MS Platform SDK */ | ||
3 | |||
4 | #ifndef __irda_w32_h | ||
5 | #define __irda_w32_h | ||
6 | |||
7 | #define AF_IRDA 26 | ||
8 | #define SOL_IRLMP 0x00FF | ||
9 | #define IRLMP_ENUMDEVICES 0x00000010 | ||
10 | #define IRLMP_9WIRE_MODE 0x00000016 | ||
11 | |||
12 | struct sockaddr_irda { | ||
13 | unsigned short irdaAddressFamily; | ||
14 | unsigned char irdaDeviceID[4]; | ||
15 | char irdaServiceName[25]; | ||
16 | }; | ||
17 | |||
18 | struct irda_device_info { | ||
19 | unsigned char irdaDeviceID[4]; | ||
20 | char irdaDeviceName[22]; | ||
21 | unsigned char irdaDeviceHints1; | ||
22 | unsigned char irdaDeviceHints2; | ||
23 | unsigned char irdaCharSet; | ||
24 | }; | ||
25 | |||
26 | struct irda_device_list { | ||
27 | ULONG numDevice; | ||
28 | struct irda_device_info Device[1]; | ||
29 | }; | ||
30 | |||
31 | #endif | ||
32 | |||
33 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
34 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
35 | */ | ||
diff --git a/gammu/emb/common/device/serial/ser_djg.c b/gammu/emb/common/device/serial/ser_djg.c new file mode 100644 index 0000000..ac9d7c8 --- a/dev/null +++ b/gammu/emb/common/device/serial/ser_djg.c | |||
@@ -0,0 +1,74 @@ | |||
1 | |||
2 | #include "../../gsmstate.h" | ||
3 | |||
4 | #ifdef GSM_ENABLE_SERIALDEVICE | ||
5 | #ifdef DJGPP | ||
6 | |||
7 | #include "../../gsmcomon.h" | ||
8 | #include "ser_djg.h" | ||
9 | |||
10 | static GSM_Error serial_close(GSM_StateMachine *s) | ||
11 | { | ||
12 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
13 | |||
14 | return ERR_NONE; | ||
15 | } | ||
16 | |||
17 | static GSM_Error serial_open (GSM_StateMachine *s) | ||
18 | { | ||
19 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
20 | |||
21 | return ERR_NONE; | ||
22 | } | ||
23 | |||
24 | static GSM_Error serial_setparity(GSM_StateMachine *s, bool parity) | ||
25 | { | ||
26 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
27 | |||
28 | return ERR_NONE; | ||
29 | } | ||
30 | |||
31 | static GSM_Error serial_setdtrrts(GSM_StateMachine *s, bool dtr, bool rts) | ||
32 | { | ||
33 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
34 | |||
35 | return ERR_NONE; | ||
36 | } | ||
37 | |||
38 | static GSM_Error serial_setspeed(GSM_StateMachine *s, int speed) | ||
39 | { | ||
40 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
41 | |||
42 | return ERR_NONE; | ||
43 | } | ||
44 | |||
45 | static int serial_read(GSM_StateMachine *s, void *buf, size_t nbytes) | ||
46 | { | ||
47 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
48 | |||
49 | return 0; | ||
50 | } | ||
51 | |||
52 | static int serial_write(GSM_StateMachine *s, void *buf, size_t nbytes) | ||
53 | { | ||
54 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
55 | |||
56 | return 0; | ||
57 | } | ||
58 | |||
59 | GSM_Device_Functions SerialDevice = { | ||
60 | serial_open, | ||
61 | serial_close, | ||
62 | serial_setparity, | ||
63 | serial_setdtrrts, | ||
64 | serial_setspeed, | ||
65 | serial_read, | ||
66 | serial_write | ||
67 | }; | ||
68 | |||
69 | #endif | ||
70 | #endif | ||
71 | |||
72 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
73 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
74 | */ | ||
diff --git a/gammu/emb/common/device/serial/ser_djg.h b/gammu/emb/common/device/serial/ser_djg.h new file mode 100644 index 0000000..b35b282 --- a/dev/null +++ b/gammu/emb/common/device/serial/ser_djg.h | |||
@@ -0,0 +1,15 @@ | |||
1 | |||
2 | #ifdef DJGPP | ||
3 | #ifndef djgppserial_h | ||
4 | #define djgppserial_h | ||
5 | |||
6 | typedef struct { | ||
7 | int hPhone; | ||
8 | } GSM_Device_SerialData; | ||
9 | |||
10 | #endif | ||
11 | #endif | ||
12 | |||
13 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
14 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
15 | */ | ||
diff --git a/gammu/emb/common/device/serial/ser_unx.c b/gammu/emb/common/device/serial/ser_unx.c new file mode 100644 index 0000000..2a87b11 --- a/dev/null +++ b/gammu/emb/common/device/serial/ser_unx.c | |||
@@ -0,0 +1,291 @@ | |||
1 | /* (c) 2002-2004 by Marcin Wiacek */ | ||
2 | /* locking device and settings all speeds by Michal Cihar */ | ||
3 | |||
4 | #include "../../gsmstate.h" | ||
5 | |||
6 | #ifdef GSM_ENABLE_SERIALDEVICE | ||
7 | #ifndef WIN32 | ||
8 | #ifndef DJGPP | ||
9 | |||
10 | #include <sys/file.h> | ||
11 | #include <sys/time.h> | ||
12 | #include <string.h> | ||
13 | #include <termios.h> | ||
14 | #include <errno.h> | ||
15 | |||
16 | #include "../../gsmcomon.h" | ||
17 | #include "ser_unx.h" | ||
18 | |||
19 | static GSM_Error serial_close(GSM_StateMachine *s) | ||
20 | { | ||
21 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
22 | |||
23 | /* Restores old settings */ | ||
24 | tcsetattr(d->hPhone, TCSANOW, &d->old_settings); | ||
25 | |||
26 | /* Closes device */ | ||
27 | close(d->hPhone); | ||
28 | |||
29 | return ERR_NONE; | ||
30 | } | ||
31 | |||
32 | #ifndef O_NONBLOCK | ||
33 | # define O_NONBLOCK 0 | ||
34 | #endif | ||
35 | |||
36 | static GSM_Error serial_open (GSM_StateMachine *s) | ||
37 | { | ||
38 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
39 | struct termios t; | ||
40 | int i; | ||
41 | |||
42 | /* O_NONBLOCK MUST is required to avoid waiting for DCD */ | ||
43 | d->hPhone = open(s->CurrentConfig->Device, O_RDWR | O_NOCTTY | O_NONBLOCK); | ||
44 | if (d->hPhone < 0) { | ||
45 | i = errno; | ||
46 | GSM_OSErrorInfo(s,"open in serial_open"); | ||
47 | if (i == 2) return ERR_DEVICENOTEXIST; //no such file or directory | ||
48 | if (i == 13) return ERR_DEVICENOPERMISSION;//permission denied | ||
49 | return ERR_DEVICEOPENERROR; | ||
50 | } | ||
51 | |||
52 | #ifdef TIOCEXCL | ||
53 | /* open() calls from other applications shall fail now */ | ||
54 | ioctl(d->hPhone, TIOCEXCL, (char *) 0); | ||
55 | #endif | ||
56 | |||
57 | if (tcgetattr(d->hPhone, &d->old_settings) == -1) { | ||
58 | close(d->hPhone); | ||
59 | GSM_OSErrorInfo(s,"tcgetattr in serial_open"); | ||
60 | return ERR_DEVICEREADERROR; | ||
61 | } | ||
62 | |||
63 | if (tcflush(d->hPhone, TCIOFLUSH) == -1) { | ||
64 | serial_close(s); | ||
65 | GSM_OSErrorInfo(s,"tcflush in serial_open"); | ||
66 | return ERR_DEVICEOPENERROR; | ||
67 | } | ||
68 | |||
69 | memcpy(&t, &d->old_settings, sizeof(struct termios)); | ||
70 | |||
71 | /* Opening without parity */ | ||
72 | t.c_iflag = IGNPAR; | ||
73 | t.c_oflag = 0; | ||
74 | /* disconnect line, 8 bits, enable receiver, | ||
75 | * ignore modem lines,lower modem line after disconnect | ||
76 | */ | ||
77 | t.c_cflag = B0 | CS8 | CREAD | CLOCAL | HUPCL; | ||
78 | /* enable hardware (RTS/CTS) flow control (NON POSIX) */ | ||
79 | /* t.c_cflag |= CRTSCTS; */ | ||
80 | t.c_lflag = 0; | ||
81 | t.c_cc[VMIN] = 1; | ||
82 | t.c_cc[VTIME] = 0; | ||
83 | |||
84 | if (tcsetattr(d->hPhone, TCSANOW, &t) == -1) { | ||
85 | serial_close(s); | ||
86 | GSM_OSErrorInfo(s,"tcsetattr in serial_open"); | ||
87 | return ERR_DEVICEOPENERROR; | ||
88 | } | ||
89 | |||
90 | /* Making file descriptor asynchronous. */ | ||
91 | if (fcntl(d->hPhone, F_SETFL, FASYNC | FNONBLOCK) == -1) { | ||
92 | serial_close(s); | ||
93 | GSM_OSErrorInfo(s,"fcntl in serial_open"); | ||
94 | return ERR_DEVICEOPENERROR; | ||
95 | } | ||
96 | |||
97 | return ERR_NONE; | ||
98 | } | ||
99 | |||
100 | static GSM_Error serial_setparity(GSM_StateMachine *s, bool parity) | ||
101 | { | ||
102 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
103 | struct termios t; | ||
104 | |||
105 | if (tcgetattr(d->hPhone, &t)) { | ||
106 | GSM_OSErrorInfo(s,"tcgetattr in serial_setparity"); | ||
107 | return ERR_DEVICEREADERROR; | ||
108 | } | ||
109 | |||
110 | if (parity) { | ||
111 | t.c_cflag |= (PARENB | PARODD); | ||
112 | t.c_iflag = 0; | ||
113 | } else { | ||
114 | t.c_iflag = IGNPAR; | ||
115 | } | ||
116 | |||
117 | if (tcsetattr(d->hPhone, TCSANOW, &t) == -1){ | ||
118 | serial_close(s); | ||
119 | GSM_OSErrorInfo(s,"tcsetattr in serial_setparity"); | ||
120 | return ERR_DEVICEPARITYERROR; | ||
121 | } | ||
122 | |||
123 | return ERR_NONE; | ||
124 | } | ||
125 | |||
126 | static GSM_Error serial_setdtrrts(GSM_StateMachine *s, bool dtr, bool rts) | ||
127 | { | ||
128 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
129 | struct termios t; | ||
130 | unsigned int flags; | ||
131 | |||
132 | if (tcgetattr(d->hPhone, &t)) { | ||
133 | GSM_OSErrorInfo(s,"tcgetattr in serial_setdtrrts"); | ||
134 | return ERR_DEVICEREADERROR; | ||
135 | } | ||
136 | |||
137 | #ifdef CRTSCTS | ||
138 | /* Disabling hardware flow control */ | ||
139 | t.c_cflag &= ~CRTSCTS; | ||
140 | #endif | ||
141 | |||
142 | if (tcsetattr(d->hPhone, TCSANOW, &t) == -1) { | ||
143 | serial_close(s); | ||
144 | GSM_OSErrorInfo(s,"tcsetattr in serial_setdtrrts"); | ||
145 | return ERR_DEVICEDTRRTSERROR; | ||
146 | } | ||
147 | |||
148 | flags = TIOCM_DTR; | ||
149 | if (dtr) { | ||
150 | ioctl(d->hPhone, TIOCMBIS, &flags); | ||
151 | } else { | ||
152 | ioctl(d->hPhone, TIOCMBIC, &flags); | ||
153 | } | ||
154 | |||
155 | flags = TIOCM_RTS; | ||
156 | if (rts) { | ||
157 | ioctl(d->hPhone, TIOCMBIS, &flags); | ||
158 | } else { | ||
159 | ioctl(d->hPhone, TIOCMBIC, &flags); | ||
160 | } | ||
161 | |||
162 | flags = 0; | ||
163 | ioctl(d->hPhone, TIOCMGET, &flags); | ||
164 | |||
165 | dbgprintf("Serial device:"); | ||
166 | dbgprintf(" DTR is %s", flags&TIOCM_DTR?"up":"down"); | ||
167 | dbgprintf(", RTS is %s", flags&TIOCM_RTS?"up":"down"); | ||
168 | dbgprintf(", CAR is %s", flags&TIOCM_CAR?"up":"down"); | ||
169 | dbgprintf(", CTS is %s\n", flags&TIOCM_CTS?"up":"down"); | ||
170 | if (((flags&TIOCM_DTR)==TIOCM_DTR) != dtr) return ERR_DEVICEDTRRTSERROR; | ||
171 | if (((flags&TIOCM_RTS)==TIOCM_RTS) != rts) return ERR_DEVICEDTRRTSERROR; | ||
172 | |||
173 | return ERR_NONE; | ||
174 | } | ||
175 | |||
176 | static GSM_Error serial_setspeed(GSM_StateMachine *s, int speed) | ||
177 | { | ||
178 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
179 | struct termios t; | ||
180 | int speed2 = B19200; | ||
181 | |||
182 | if (tcgetattr(d->hPhone, &t)) { | ||
183 | GSM_OSErrorInfo(s,"tcgetattr in serial_setspeed"); | ||
184 | return ERR_DEVICEREADERROR; | ||
185 | } | ||
186 | |||
187 | smprintf(s, "Setting speed to %d\n", speed); | ||
188 | |||
189 | switch (speed) { | ||
190 | case 50: speed2 = B50; break; | ||
191 | case 75: speed2 = B75; break; | ||
192 | case 110: speed2 = B110; break; | ||
193 | case 134: speed2 = B134; break; | ||
194 | case 150: speed2 = B150; break; | ||
195 | case 200: speed2 = B200; break; | ||
196 | case 300: speed2 = B300; break; | ||
197 | case 600: speed2 = B600; break; | ||
198 | case 1200: speed2 = B1200; break; | ||
199 | case 1800: speed2 = B1800; break; | ||
200 | case 2400: speed2 = B2400; break; | ||
201 | case 4800: speed2 = B4800; break; | ||
202 | case 9600: speed2 = B9600; break; | ||
203 | case 19200: speed2 = B19200;break; | ||
204 | case 38400: speed2 = B38400;break; | ||
205 | case 57600: speed2 = B57600;break; | ||
206 | case 115200: speed2 = B115200;break; | ||
207 | case 230400: speed2 = B230400;break; | ||
208 | case 460800: speed2 = B460800;break; | ||
209 | case 500000: speed2 = B500000;break; | ||
210 | case 576000: speed2 = B576000;break; | ||
211 | case 921600: speed2 = B921600;break; | ||
212 | case 1000000: speed2 = B1000000;break; | ||
213 | case 1152000: speed2 = B1152000;break; | ||
214 | case 1500000: speed2 = B1500000;break; | ||
215 | case 2000000: speed2 = B2000000;break; | ||
216 | case 2500000: speed2 = B2500000;break; | ||
217 | case 3000000: speed2 = B3000000;break; | ||
218 | case 3500000: speed2 = B3500000;break; | ||
219 | case 4000000: speed2 = B4000000; break; | ||
220 | } | ||
221 | |||
222 | /* This should work on all systems because it is done according to POSIX */ | ||
223 | cfsetispeed(&t, speed2); | ||
224 | cfsetospeed(&t, speed2); | ||
225 | |||
226 | if (tcsetattr(d->hPhone, TCSADRAIN, &t) == -1) { | ||
227 | serial_close(s); | ||
228 | GSM_OSErrorInfo(s,"tcsetattr in serial_setspeed"); | ||
229 | return ERR_DEVICECHANGESPEEDERROR; | ||
230 | } | ||
231 | |||
232 | return ERR_NONE; | ||
233 | } | ||
234 | |||
235 | static int serial_read(GSM_StateMachine *s, void *buf, size_t nbytes) | ||
236 | { | ||
237 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
238 | struct timeval timeout2; | ||
239 | fd_set readfds; | ||
240 | int actual = 0; | ||
241 | |||
242 | FD_ZERO(&readfds); | ||
243 | FD_SET(d->hPhone, &readfds); | ||
244 | |||
245 | timeout2.tv_sec = 0; | ||
246 | timeout2.tv_usec = 1; | ||
247 | |||
248 | if (select(d->hPhone+1, &readfds, NULL, NULL, &timeout2)) { | ||
249 | actual = read(d->hPhone, buf, nbytes); | ||
250 | if (actual == -1) GSM_OSErrorInfo(s,"serial_read"); | ||
251 | } | ||
252 | return actual; | ||
253 | } | ||
254 | |||
255 | static int serial_write(GSM_StateMachine *s, void *buf, size_t nbytes) | ||
256 | { | ||
257 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
258 | int ret; | ||
259 | size_t actual = 0; | ||
260 | |||
261 | do { | ||
262 | ret = write(d->hPhone, (unsigned char *)buf, nbytes - actual); | ||
263 | if (ret < 0 && errno == EAGAIN) continue; | ||
264 | if (ret < 0) { | ||
265 | if (actual != nbytes) GSM_OSErrorInfo(s,"serial_write"); | ||
266 | return actual; | ||
267 | } | ||
268 | actual += ret; | ||
269 | buf += ret; | ||
270 | if (s->ConnectionType == GCT_FBUS2PL2303) my_sleep(1); | ||
271 | } while (actual < nbytes); | ||
272 | return actual; | ||
273 | } | ||
274 | |||
275 | GSM_Device_Functions SerialDevice = { | ||
276 | serial_open, | ||
277 | serial_close, | ||
278 | serial_setparity, | ||
279 | serial_setdtrrts, | ||
280 | serial_setspeed, | ||
281 | serial_read, | ||
282 | serial_write | ||
283 | }; | ||
284 | |||
285 | #endif | ||
286 | #endif | ||
287 | #endif | ||
288 | |||
289 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
290 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
291 | */ | ||
diff --git a/gammu/emb/common/device/serial/ser_unx.h b/gammu/emb/common/device/serial/ser_unx.h new file mode 100644 index 0000000..ca8fe5a --- a/dev/null +++ b/gammu/emb/common/device/serial/ser_unx.h | |||
@@ -0,0 +1,22 @@ | |||
1 | |||
2 | #ifndef WIN32 | ||
3 | #ifndef DJGPP | ||
4 | #ifndef unixserial_h | ||
5 | #define unixserial_h | ||
6 | |||
7 | #include <fcntl.h> | ||
8 | #include <sys/ioctl.h> | ||
9 | #include <termios.h> | ||
10 | |||
11 | typedef struct { | ||
12 | int hPhone; | ||
13 | struct termios old_settings; | ||
14 | } GSM_Device_SerialData; | ||
15 | |||
16 | #endif | ||
17 | #endif | ||
18 | #endif | ||
19 | |||
20 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
21 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
22 | */ | ||
diff --git a/gammu/emb/common/device/serial/ser_w32.c b/gammu/emb/common/device/serial/ser_w32.c new file mode 100644 index 0000000..9fa0135 --- a/dev/null +++ b/gammu/emb/common/device/serial/ser_w32.c | |||
@@ -0,0 +1,340 @@ | |||
1 | /* (c) 2002-2004 by Marcin Wiacek */ | ||
2 | /* based on some work from Gnokii, MSDN and others */ | ||
3 | |||
4 | #include "../../gsmstate.h" | ||
5 | |||
6 | #ifdef GSM_ENABLE_SERIALDEVICE | ||
7 | #ifdef WIN32 | ||
8 | |||
9 | #include <windows.h> | ||
10 | #include <string.h> | ||
11 | #include <stdio.h> | ||
12 | #include <io.h> | ||
13 | #include <memory.h> | ||
14 | |||
15 | #include "../../gsmcomon.h" | ||
16 | #include "ser_w32.h" | ||
17 | |||
18 | static GSM_Error serial_close(GSM_StateMachine *s) | ||
19 | { | ||
20 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
21 | |||
22 | /* Disables all monitored events for device */ | ||
23 | SetCommMask(d->hPhone, 0); | ||
24 | |||
25 | /* Discards all characters from input/output buffer and terminates | ||
26 | * pending read/write operations | ||
27 | */ | ||
28 | PurgeComm(d->hPhone, PURGE_TXABORT | PURGE_RXABORT | | ||
29 | PURGE_TXCLEAR | PURGE_RXCLEAR); | ||
30 | |||
31 | /* Clears the DTR (data-terminal-ready) signal */ | ||
32 | EscapeCommFunction(d->hPhone, CLRDTR); | ||
33 | |||
34 | /* Restores old settings */ | ||
35 | if (SetCommState(d->hPhone, &d->old_settings)==0) { | ||
36 | GSM_OSErrorInfo(s, "SetCommState in serial_close"); | ||
37 | } | ||
38 | |||
39 | /* Closes device */ | ||
40 | if (CloseHandle(d->hPhone)==0) { | ||
41 | GSM_OSErrorInfo(s, "CloseHandle in serial_close"); | ||
42 | } | ||
43 | |||
44 | return ERR_NONE; | ||
45 | } | ||
46 | |||
47 | static GSM_Error serial_open (GSM_StateMachine *s) | ||
48 | { | ||
49 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
50 | DCB dcb; | ||
51 | unsigned char DeviceName[80],DeviceName2[80]; | ||
52 | int i; | ||
53 | #ifdef GSM_ENABLE_FBUS2DKU5 | ||
54 | HKEY hKey; | ||
55 | DWORD DeviceNameLen, KeyNameLen; | ||
56 | unsigned char KeyName[100]; | ||
57 | #endif | ||
58 | |||
59 | strcpy(DeviceName2,s->CurrentConfig->Device); | ||
60 | |||
61 | #ifdef GSM_ENABLE_FBUS2DKU5 | ||
62 | if (s->ConnectionType == GCT_FBUS2DKU5) { | ||
63 | smprintf(s,"Reading DKU5 device\n"); | ||
64 | DeviceName2[0] = 0; | ||
65 | if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS) { | ||
66 | smprintf(s,"Error opening key\n"); | ||
67 | return ERR_DEVICENOTWORK; | ||
68 | } | ||
69 | i = 0; | ||
70 | while(1) { | ||
71 | DeviceNameLen = 80; | ||
72 | KeyNameLen = 100; | ||
73 | if (RegEnumValue(hKey,i,KeyName,&KeyNameLen,NULL,NULL,DeviceName2,&DeviceNameLen) != ERROR_SUCCESS) { | ||
74 | smprintf(s,"Error reading key value\n"); | ||
75 | return ERR_DEVICENOTWORK; | ||
76 | } | ||
77 | // smprintf(s,"Key name is %s, value is %s\n",KeyName,DeviceName2); | ||
78 | if (!strncmp(KeyName,"\\Device\\AtmelVirtualPort",24)) break; | ||
79 | i++; | ||
80 | } | ||
81 | RegCloseKey(hKey); | ||
82 | if (strlen(DeviceName2) == 0) return ERR_DEVICENOTWORK; | ||
83 | smprintf(s,"DKU5 device is \"%s\"\n",DeviceName2); | ||
84 | //nodriver | ||
85 | } | ||
86 | #endif | ||
87 | |||
88 | if ((s->ConnectionType == GCT_FBUS2DKU5) || | ||
89 | (!strncmp(DeviceName2,"com",3) && strlen(DeviceName2)>3)) { | ||
90 | sprintf(DeviceName,"\\\\.\\COM%i",atoi(DeviceName2+3)); | ||
91 | } else { | ||
92 | strcpy(DeviceName,DeviceName2); | ||
93 | } | ||
94 | |||
95 | smprintf(s,"Device is %s\n",DeviceName); | ||
96 | |||
97 | /* Allows for reading/writing, no device sharing */ | ||
98 | d->hPhone = CreateFile(DeviceName, | ||
99 | GENERIC_READ | GENERIC_WRITE, | ||
100 | 0, | ||
101 | 0, | ||
102 | OPEN_EXISTING, | ||
103 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, | ||
104 | NULL); | ||
105 | |||
106 | if (d->hPhone == INVALID_HANDLE_VALUE) { | ||
107 | i = GetLastError(); | ||
108 | GSM_OSErrorInfo(s, "CreateFile in serial_open"); | ||
109 | if (i == 2) return ERR_DEVICENOTWORK; //can't find specified file | ||
110 | if (i == 5) return ERR_DEVICEBUSY; //access denied | ||
111 | if (i == 31) return ERR_DEVICENOTWORK; //attached device not working | ||
112 | if (i == 123) return ERR_DEVICENOTEXIST; | ||
113 | return ERR_DEVICEOPENERROR; | ||
114 | } | ||
115 | |||
116 | d->old_settings.DCBlength = sizeof(DCB); | ||
117 | if (GetCommState(d->hPhone, &d->old_settings)==0) { | ||
118 | GSM_OSErrorInfo(s, "ReadDevice in serial_open"); | ||
119 | return ERR_DEVICEREADERROR; | ||
120 | } | ||
121 | |||
122 | /* When char will be received, we will receive notifications */ | ||
123 | SetCommMask(d->hPhone, EV_RXCHAR); | ||
124 | |||
125 | /* Sets size for input/output buffer */ | ||
126 | SetupComm(d->hPhone, 4096, 4096); | ||
127 | |||
128 | /* Discards all characters from input/output buffer and terminates | ||
129 | * pending read/write operations | ||
130 | */ | ||
131 | PurgeComm(d->hPhone, PURGE_TXABORT | PURGE_RXABORT | | ||
132 | PURGE_TXCLEAR | PURGE_RXCLEAR); | ||
133 | |||
134 | memcpy(&dcb, &d->old_settings, sizeof(DCB)); | ||
135 | |||
136 | dcb.ByteSize = 8; | ||
137 | dcb.Parity = NOPARITY; | ||
138 | dcb.StopBits = ONESTOPBIT; | ||
139 | |||
140 | /* No Xon/Xof flow control */ | ||
141 | // dcb.fOutX = false; | ||
142 | // dcb.fInX = false; | ||
143 | |||
144 | /* Hardware flow control */ | ||
145 | //dcb.fOutxDsrFlow = true; | ||
146 | //dcb.fOutxCtsFlow = true; | ||
147 | //dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; | ||
148 | //dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; | ||
149 | |||
150 | /* Initialise the port settings */ | ||
151 | if (SetCommState(d->hPhone, &dcb)==0) { | ||
152 | GSM_OSErrorInfo(s, "WriteDevice in serial_open"); | ||
153 | return ERR_DEVICEOPENERROR; | ||
154 | } | ||
155 | |||
156 | return ERR_NONE; | ||
157 | } | ||
158 | |||
159 | static GSM_Error serial_setparity (GSM_StateMachine *s, bool parity) | ||
160 | { | ||
161 | DCB dcb; | ||
162 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
163 | |||
164 | dcb.DCBlength = sizeof(DCB); | ||
165 | if (GetCommState(d->hPhone, &dcb)==0) { | ||
166 | GSM_OSErrorInfo(s, "ReadDevice in serial_setparity"); | ||
167 | return ERR_DEVICEREADERROR; | ||
168 | } | ||
169 | |||
170 | if (parity) { | ||
171 | dcb.Parity = ODDPARITY; | ||
172 | } else { | ||
173 | dcb.Parity = NOPARITY; | ||
174 | } | ||
175 | |||
176 | if (SetCommState(d->hPhone, &dcb)==0) { | ||
177 | GSM_OSErrorInfo(s, "WriteDevice in serial_setparity"); | ||
178 | return ERR_DEVICEPARITYERROR; | ||
179 | } | ||
180 | |||
181 | return ERR_NONE; | ||
182 | } | ||
183 | |||
184 | static GSM_Error serial_setdtrrts(GSM_StateMachine *s, bool dtr, bool rts) | ||
185 | { | ||
186 | DCB dcb; | ||
187 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
188 | |||
189 | dcb.DCBlength = sizeof(DCB); | ||
190 | if (GetCommState(d->hPhone, &dcb)==0) { | ||
191 | GSM_OSErrorInfo(s, "ReadDevice in serial_setdtrrts"); | ||
192 | return ERR_DEVICEREADERROR; | ||
193 | } | ||
194 | |||
195 | dcb.fOutxDsrFlow = 0; | ||
196 | dcb.fDtrControl = DTR_CONTROL_DISABLE; | ||
197 | if (dtr) dcb.fDtrControl = DTR_CONTROL_ENABLE; | ||
198 | |||
199 | dcb.fOutxCtsFlow = 0; | ||
200 | dcb.fRtsControl = RTS_CONTROL_DISABLE; | ||
201 | if (rts) dcb.fRtsControl = RTS_CONTROL_ENABLE; | ||
202 | |||
203 | /* no software (Xon/Xof) flow control */ | ||
204 | dcb.fInX = dcb.fOutX = 0; | ||
205 | |||
206 | if (SetCommState(d->hPhone, &dcb)==0) { | ||
207 | GSM_OSErrorInfo(s, "WriteDevice in serial_setdtrrts"); | ||
208 | return ERR_DEVICEDTRRTSERROR; | ||
209 | } | ||
210 | |||
211 | /* the rest of function checks, if setting was really done */ | ||
212 | |||
213 | dcb.DCBlength = sizeof(DCB); | ||
214 | GetCommState(d->hPhone, &dcb); | ||
215 | |||
216 | dbgprintf("Serial device:"); | ||
217 | dbgprintf(" DTR is "); | ||
218 | switch (dcb.fDtrControl) { | ||
219 | case DTR_CONTROL_ENABLE : dbgprintf("up"); break; | ||
220 | case DTR_CONTROL_DISABLE : dbgprintf("down"); break; | ||
221 | case DTR_CONTROL_HANDSHAKE : dbgprintf("handshake"); break; | ||
222 | } | ||
223 | dbgprintf(", RTS is "); | ||
224 | switch (dcb.fRtsControl) { | ||
225 | case RTS_CONTROL_ENABLE : dbgprintf("up"); break; | ||
226 | case RTS_CONTROL_DISABLE : dbgprintf("down"); break; | ||
227 | case RTS_CONTROL_HANDSHAKE : dbgprintf("handshake"); break; | ||
228 | case RTS_CONTROL_TOGGLE : dbgprintf("toggle"); break; | ||
229 | } | ||
230 | dbgprintf("\n"); | ||
231 | if ( dtr && dcb.fDtrControl != DTR_CONTROL_ENABLE ) return ERR_DEVICEDTRRTSERROR; | ||
232 | if (!dtr && dcb.fDtrControl != DTR_CONTROL_DISABLE) return ERR_DEVICEDTRRTSERROR; | ||
233 | if ( rts && dcb.fRtsControl != RTS_CONTROL_ENABLE ) return ERR_DEVICEDTRRTSERROR; | ||
234 | if (!rts && dcb.fRtsControl != RTS_CONTROL_DISABLE) return ERR_DEVICEDTRRTSERROR; | ||
235 | |||
236 | return ERR_NONE; | ||
237 | } | ||
238 | |||
239 | static GSM_Error serial_setspeed(GSM_StateMachine *s, int speed) | ||
240 | { | ||
241 | DCB dcb; | ||
242 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
243 | |||
244 | dcb.DCBlength = sizeof(DCB); | ||
245 | if (GetCommState(d->hPhone, &dcb)==0) { | ||
246 | GSM_OSErrorInfo(s, "ReadDevice in serial_setspeed"); | ||
247 | return ERR_DEVICEREADERROR; | ||
248 | } | ||
249 | |||
250 | dcb.BaudRate = speed; | ||
251 | |||
252 | if (SetCommState(d->hPhone, &dcb)==0) { | ||
253 | GSM_OSErrorInfo(s, "WriteDevice in serial_setspeed"); | ||
254 | return ERR_DEVICECHANGESPEEDERROR; | ||
255 | } | ||
256 | |||
257 | return ERR_NONE; | ||
258 | } | ||
259 | |||
260 | static int serial_read(GSM_StateMachine *s, void *buf, size_t nbytes) | ||
261 | { | ||
262 | COMSTAT ComStat; | ||
263 | DWORD ErrorFlags, Length; | ||
264 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
265 | |||
266 | /* Gets information about a communications error and | ||
267 | * current status of device | ||
268 | */ | ||
269 | ClearCommError(d->hPhone, &ErrorFlags, &ComStat); | ||
270 | Length = ComStat.cbInQue; | ||
271 | |||
272 | /* Nothing to read */ | ||
273 | if (Length <= 0) return Length; | ||
274 | |||
275 | /* Read without problems */ | ||
276 | if (ReadFile(d->hPhone, buf, Length, &Length, &d->osRead)) return Length; | ||
277 | |||
278 | if (GetLastError() != ERROR_IO_PENDING) { | ||
279 | GSM_OSErrorInfo(s, "serial_read1"); | ||
280 | Length = 0; | ||
281 | ClearCommError(d->hPhone, &ErrorFlags, &ComStat); | ||
282 | return Length; | ||
283 | } | ||
284 | |||
285 | while(1) { | ||
286 | if (GetOverlappedResult(d->hPhone,&d->osRead, &Length, TRUE)) break; | ||
287 | if (GetLastError() != ERROR_IO_INCOMPLETE) { | ||
288 | GSM_OSErrorInfo(s, "serial_read2"); | ||
289 | /* an error occurred, try to recover */ | ||
290 | ClearCommError(d->hPhone, &ErrorFlags, &ComStat); | ||
291 | break; | ||
292 | } | ||
293 | } | ||
294 | return Length; | ||
295 | } | ||
296 | |||
297 | static int serial_write(GSM_StateMachine *s, void *buf, size_t nbytes) | ||
298 | { | ||
299 | DWORD BytesWritten,ErrorFlags,BytesSent=0; | ||
300 | COMSTAT ComStat; | ||
301 | GSM_Device_SerialData *d = &s->Device.Data.Serial; | ||
302 | |||
303 | if (WriteFile(d->hPhone, buf, nbytes, &BytesSent, &d->osWrite)) return BytesSent; | ||
304 | |||
305 | if (GetLastError() != ERROR_IO_PENDING) { | ||
306 | GSM_OSErrorInfo(s, "serial_write1"); | ||
307 | ClearCommError(d->hPhone, &ErrorFlags, &ComStat); | ||
308 | return BytesSent; | ||
309 | } | ||
310 | |||
311 | while (1) { | ||
312 | if (GetOverlappedResult(d->hPhone, &d->osWrite, &BytesWritten, TRUE)) break; | ||
313 | if (GetLastError() != ERROR_IO_INCOMPLETE) { | ||
314 | GSM_OSErrorInfo(s, "serial_write2"); | ||
315 | ClearCommError(d->hPhone, &ErrorFlags, &ComStat); | ||
316 | break; | ||
317 | } | ||
318 | BytesSent += BytesWritten; | ||
319 | } | ||
320 | BytesSent += BytesWritten; | ||
321 | |||
322 | return BytesSent; | ||
323 | } | ||
324 | |||
325 | GSM_Device_Functions SerialDevice = { | ||
326 | serial_open, | ||
327 | serial_close, | ||
328 | serial_setparity, | ||
329 | serial_setdtrrts, | ||
330 | serial_setspeed, | ||
331 | serial_read, | ||
332 | serial_write | ||
333 | }; | ||
334 | |||
335 | #endif | ||
336 | #endif | ||
337 | |||
338 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
339 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
340 | */ | ||
diff --git a/gammu/emb/common/device/serial/ser_w32.h b/gammu/emb/common/device/serial/ser_w32.h new file mode 100644 index 0000000..d226f32 --- a/dev/null +++ b/gammu/emb/common/device/serial/ser_w32.h | |||
@@ -0,0 +1,19 @@ | |||
1 | |||
2 | #ifdef WIN32 | ||
3 | #ifndef winserial_h | ||
4 | #define winserial_h | ||
5 | |||
6 | #include <windows.h> | ||
7 | |||
8 | typedef struct { | ||
9 | HANDLE hPhone; | ||
10 | DCB old_settings; | ||
11 | OVERLAPPED osWrite,osRead; | ||
12 | } GSM_Device_SerialData; | ||
13 | |||
14 | #endif | ||
15 | #endif | ||
16 | |||
17 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
18 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
19 | */ | ||