summaryrefslogtreecommitdiffabout
path: root/gammu/emb/common/device/devfunc.c
Unidiff
Diffstat (limited to 'gammu/emb/common/device/devfunc.c') (more/less context) (ignore whitespace changes)
-rw-r--r--gammu/emb/common/device/devfunc.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/gammu/emb/common/device/devfunc.c b/gammu/emb/common/device/devfunc.c
index d31ebbf..c58a01f 100644
--- a/gammu/emb/common/device/devfunc.c
+++ b/gammu/emb/common/device/devfunc.c
@@ -1,48 +1,56 @@
1/* Some source from Gnokii (www.gnokii.org)
2 * (C) 1999-2000 Hugh Blemings & Pavel Janik ml. (C) 2001-2004 Pawel Kot
3 * GNU GPL version 2 or later
4 */
5/* Some source from Minicom (http://alioth.debian.org/projects/minicom)
6 * (C) 1991,1992,1993,1994,1995,1996 by Miquel van Smoorenburg
7 * GNU GPL version 2
8 */
1 9
2#include <string.h> 10#include <string.h>
3#ifdef WIN32 11#ifdef WIN32
4# include <io.h> 12# include <io.h>
5#else 13#else
6# include <errno.h> 14# include <errno.h>
7# include <signal.h> 15# include <signal.h>
8#endif 16#endif
9 17
10#include "../gsmstate.h" 18#include "../gsmstate.h"
11 19
12#ifdef GSM_ENABLE_BLUETOOTHDEVICE 20#ifdef GSM_ENABLE_BLUETOOTHDEVICE
13#ifdef BLUETOOTH_RF_SEARCHING 21#ifdef BLUETOOTH_RF_SEARCHING
14 22
15GSM_Error bluetooth_checkservicename(GSM_StateMachine *s, char *name) 23GSM_Error bluetooth_checkservicename(GSM_StateMachine *s, char *name)
16{ 24{
17 if (s->ConnectionType == GCT_BLUEPHONET && strstr(name,"Nokia PC Suite")!=NULL) return ERR_NONE; 25 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; 26 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; 27 if (s->ConnectionType == GCT_BLUEAT && strstr(name,"COM 1") !=NULL) return ERR_NONE;
20 return ERR_UNKNOWN; 28 return ERR_UNKNOWN;
21} 29}
22 30
23#endif 31#endif
24#endif 32#endif
25 33
26#if defined (GSM_ENABLE_BLUETOOTHDEVICE) || defined (GSM_ENABLE_IRDADEVICE) 34#if defined (GSM_ENABLE_BLUETOOTHDEVICE) || defined (GSM_ENABLE_IRDADEVICE)
27 35
28int socket_read(GSM_StateMachine *s, void *buf, size_t nbytes, int hPhone) 36int socket_read(GSM_StateMachine *s, void *buf, size_t nbytes, int hPhone)
29{ 37{
30 fd_set readfds; 38 fd_set readfds;
31#ifdef WIN32 39#ifdef WIN32
32 struct timeval timer; 40 struct timeval timer;
33#endif 41#endif
34 42
35 FD_ZERO(&readfds); 43 FD_ZERO(&readfds);
36 FD_SET(hPhone, &readfds); 44 FD_SET(hPhone, &readfds);
37#ifndef WIN32 45#ifndef WIN32
38 if (select(hPhone+1, &readfds, NULL, NULL, 0)) { 46 if (select(hPhone+1, &readfds, NULL, NULL, 0)) {
39 return(read(hPhone, buf, nbytes)); 47 return(read(hPhone, buf, nbytes));
40 } 48 }
41#else 49#else
42 memset(&timer,0,sizeof(timer)); 50 memset(&timer,0,sizeof(timer));
43 if (select(0, &readfds, NULL, NULL, &timer) != 0) { 51 if (select(0, &readfds, NULL, NULL, &timer) != 0) {
44 return(recv(hPhone, buf, nbytes, 0)); 52 return(recv(hPhone, buf, nbytes, 0));
45 } 53 }
46#endif 54#endif
47 return 0; 55 return 0;
48} 56}
@@ -60,97 +68,97 @@ int socket_write(GSM_StateMachine *s, void *buf, size_t nbytes, int hPhone)
60 ret = send(hPhone, buf, nbytes - actual, 0); 68 ret = send(hPhone, buf, nbytes - actual, 0);
61 if (ret < 0) { 69 if (ret < 0) {
62 if (actual != nbytes) GSM_OSErrorInfo(s,"socket_write"); 70 if (actual != nbytes) GSM_OSErrorInfo(s,"socket_write");
63 return actual; 71 return actual;
64 } 72 }
65 actual += ret; 73 actual += ret;
66 buf += ret; 74 buf += ret;
67 } while (actual < nbytes); 75 } while (actual < nbytes);
68 76
69 return actual; 77 return actual;
70} 78}
71 79
72GSM_Error socket_close(GSM_StateMachine *s, int hPhone) 80GSM_Error socket_close(GSM_StateMachine *s, int hPhone)
73{ 81{
74 shutdown(hPhone, 0); 82 shutdown(hPhone, 0);
75#ifdef WIN32 83#ifdef WIN32
76 closesocket(hPhone); /*FIXME: error checking */ 84 closesocket(hPhone); /*FIXME: error checking */
77#else 85#else
78 close(hPhone); /*FIXME: error checking */ 86 close(hPhone); /*FIXME: error checking */
79#endif 87#endif
80 return ERR_NONE; 88 return ERR_NONE;
81} 89}
82 90
83#endif 91#endif
84 92
85#ifdef ENABLE_LGPL 93#ifdef ENABLE_LGPL
86 94
87GSM_Error lock_device(const char* port, char **lock_device) 95GSM_Error lock_device(const char* port, char **lock_device)
88{ 96{
89 *lock_device = 0; 97 *lock_device = 0;
90 return ERR_NONE; 98 return ERR_NONE;
91} 99}
92 100
93bool unlock_device(char **lock_file) 101bool unlock_device(char **lock_file)
94{ 102{
95 return true; 103 return true;
96} 104}
97 105
98#else 106#else
99 107
100 #define max_buf_len 128 108 #define max_buf_len 128
101 #define lock_path "/var/lock/LCK.." 109 #define lock_path "/var/lock/LCK.."
102 110
103/* Lock the device. Allocated string with a lock name is returned 111/* Lock the device. Allocated string with a lock name is returned
104 * in lock_device 112 * in lock_device
105 */ 113 */
106GSM_Error lock_device(const char* port, char **lock_device) 114GSM_Error lock_device(const char* port, char **lock_device)
107{ 115{
108#ifndef WIN32 116#if !defined(WIN32) && !defined(DJGPP)
109 char *lock_file = NULL; 117 char *lock_file = NULL;
110 char buffer[max_buf_len]; 118 char buffer[max_buf_len];
111 const char *aux; 119 const char *aux;
112 int fd, len; 120 int fd, len;
113 GSM_Errorerror = ERR_NONE; 121 GSM_Errorerror = ERR_NONE;
114 122
115 dbgprintf("Locking device\n"); 123 dbgprintf("Locking device\n");
116 124
117 aux = strrchr(port, '/'); 125 aux = strrchr(port, '/');
118 /* Remove leading '/' */ 126 /* Remove leading '/' */
119 if (aux) { 127 if (aux) {
120 aux++; 128 aux++;
121 } else { 129 } else {
122 /* No / in port */ 130 /* No / in port */
123 aux = port; 131 aux = port;
124 } 132 }
125 len = strlen(aux) + strlen(lock_path); 133 len = strlen(aux) + strlen(lock_path);
126 134
127 memset(buffer, 0, sizeof(buffer)); 135 memset(buffer, 0, sizeof(buffer));
128 lock_file = calloc(len + 1, 1); 136 lock_file = calloc(len + 1, 1);
129 if (!lock_file) { 137 if (!lock_file) {
130 dbgprintf("Out of memory error while locking device\n"); 138 dbgprintf("Out of memory error while locking device\n");
131 return ERR_MOREMEMORY; 139 return ERR_MOREMEMORY;
132 } 140 }
133 /* I think we don't need to use strncpy, as we should have enough 141 /* I think we don't need to use strncpy, as we should have enough
134 * buffer due to strlen results 142 * buffer due to strlen results
135 */ 143 */
136 strcpy(lock_file, lock_path); 144 strcpy(lock_file, lock_path);
137 strcat(lock_file, aux); 145 strcat(lock_file, aux);
138 146
139 /* Check for the stale lockfile. 147 /* Check for the stale lockfile.
140 * The code taken from minicom by Miquel van Smoorenburg */ 148 * The code taken from minicom by Miquel van Smoorenburg */
141 if ((fd = open(lock_file, O_RDONLY)) >= 0) { 149 if ((fd = open(lock_file, O_RDONLY)) >= 0) {
142 char buf[max_buf_len]; 150 char buf[max_buf_len];
143 int pid, n = 0; 151 int pid, n = 0;
144 152
145 n = read(fd, buf, sizeof(buf) - 1); 153 n = read(fd, buf, sizeof(buf) - 1);
146 close(fd); 154 close(fd);
147 if (n > 0) { 155 if (n > 0) {
148 pid = -1; 156 pid = -1;
149 if (n == 4) 157 if (n == 4)
150 /* Kermit-style lockfile. */ 158 /* Kermit-style lockfile. */
151 pid = *(int *)buf; 159 pid = *(int *)buf;
152 else { 160 else {
153 /* Ascii lockfile. */ 161 /* Ascii lockfile. */
154 buf[n] = 0; 162 buf[n] = 0;
155 sscanf(buf, "%d", &pid); 163 sscanf(buf, "%d", &pid);
156 } 164 }
@@ -173,94 +181,94 @@ GSM_Error lock_device(const char* port, char **lock_device)
173 /* no wrong permissions are set. only reason could be */ 181 /* no wrong permissions are set. only reason could be */
174 /* flock/lockf or a empty lockfile due to a broken binary */ 182 /* flock/lockf or a empty lockfile due to a broken binary */
175 /* which is more likely */ 183 /* which is more likely */
176 if (n == 0) { 184 if (n == 0) {
177 dbgprintf("Unable to read lockfile %s.\n", lock_file); 185 dbgprintf("Unable to read lockfile %s.\n", lock_file);
178 dbgprintf("Please check for reason and remove the lockfile by hand.\n"); 186 dbgprintf("Please check for reason and remove the lockfile by hand.\n");
179 dbgprintf("Cannot lock device\n"); 187 dbgprintf("Cannot lock device\n");
180 error = ERR_UNKNOWN; 188 error = ERR_UNKNOWN;
181 goto failed; 189 goto failed;
182 } 190 }
183 } 191 }
184 192
185 /* Try to create a new file, with 0644 mode */ 193 /* Try to create a new file, with 0644 mode */
186 fd = open(lock_file, O_CREAT | O_EXCL | O_WRONLY, 0644); 194 fd = open(lock_file, O_CREAT | O_EXCL | O_WRONLY, 0644);
187 if (fd == -1) { 195 if (fd == -1) {
188 if (errno == EEXIST) { 196 if (errno == EEXIST) {
189 dbgprintf("Device seems to be locked by unknown process\n"); 197 dbgprintf("Device seems to be locked by unknown process\n");
190 error = ERR_DEVICEOPENERROR; 198 error = ERR_DEVICEOPENERROR;
191 } else if (errno == EACCES) { 199 } else if (errno == EACCES) {
192 dbgprintf("Please check permission on lock directory\n"); 200 dbgprintf("Please check permission on lock directory\n");
193 error = ERR_PERMISSION; 201 error = ERR_PERMISSION;
194 } else if (errno == ENOENT) { 202 } else if (errno == ENOENT) {
195 dbgprintf("Cannot create lockfile %s. Please check for existence of path\n", lock_file); 203 dbgprintf("Cannot create lockfile %s. Please check for existence of path\n", lock_file);
196 error = ERR_UNKNOWN; 204 error = ERR_UNKNOWN;
197 } else { 205 } else {
198 dbgprintf("Unknown error with creating lockfile %s\n", lock_file); 206 dbgprintf("Unknown error with creating lockfile %s\n", lock_file);
199 error = ERR_UNKNOWN; 207 error = ERR_UNKNOWN;
200 } 208 }
201 goto failed; 209 goto failed;
202 } 210 }
203 sprintf(buffer, "%10ld gammu\n", (long)getpid()); 211 sprintf(buffer, "%10ld gammu\n", (long)getpid());
204 write(fd, buffer, strlen(buffer)); 212 write(fd, buffer, strlen(buffer));
205 close(fd); 213 close(fd);
206 *lock_device = lock_file; 214 *lock_device = lock_file;
207 return ERR_NONE; 215 return ERR_NONE;
208failed: 216failed:
209 free(lock_file); 217 free(lock_file);
210 *lock_device = 0; 218 *lock_device = 0;
211 return error; 219 return error;
212#else 220#else
213 *lock_device = 0; 221 *lock_device = 0;
214 return ERR_NONE; 222 return ERR_NONE;
215#endif 223#endif
216} 224}
217 225
218/* Removes lock and frees memory */ 226/* Removes lock and frees memory */
219bool unlock_device(char **lock_file) 227bool unlock_device(char **lock_file)
220{ 228{
221#ifndef WIN32 229#if !defined(WIN32) && !defined(DJGPP)
222 int err; 230 int err;
223 231
224 if (!lock_file) { 232 if (!lock_file) {
225 dbgprintf("Cannot unlock device\n"); 233 dbgprintf("Cannot unlock device\n");
226 return false; 234 return false;
227 } 235 }
228 err = unlink(*lock_file); 236 err = unlink(*lock_file);
229 free(*lock_file); 237 free(*lock_file);
230 *lock_file = NULL; 238 *lock_file = NULL;
231 return (err + 1); 239 return (err + 1);
232#else 240#else
233 return true; 241 return true;
234#endif 242#endif
235} 243}
236 244
237#endif 245#endif
238 246
239int FindSerialSpeed(char *buffer) 247int FindSerialSpeed(char *buffer)
240{ 248{
241 switch (atoi(buffer)) { 249 switch (atoi(buffer)) {
242 case 50 : return 50; 250 case 50 : return 50;
243 case 75 : return 75; 251 case 75 : return 75;
244 case 110: return 110; 252 case 110: return 110;
245 case 134: return 134; 253 case 134: return 134;
246 case 150: return 150; 254 case 150: return 150;
247 case 200: return 200; 255 case 200: return 200;
248 case 300: return 300; 256 case 300: return 300;
249 case 600: return 600; 257 case 600: return 600;
250 case 1200: return 1200; 258 case 1200: return 1200;
251 case 1800: return 1800; 259 case 1800: return 1800;
252 case 2400: return 2400; 260 case 2400: return 2400;
253 case 4800: return 4800; 261 case 4800: return 4800;
254 case 9600: return 9600; 262 case 9600: return 9600;
255 case 19200: return 19200; 263 case 19200: return 19200;
256 case 38400: return 38400; 264 case 38400: return 38400;
257 case 57600: return 57600; 265 case 57600: return 57600;
258 case 115200: return 115200; 266 case 115200: return 115200;
259 case 230400: return 230400; 267 case 230400: return 230400;
260 default : return 0; 268 default : return 0;
261 } 269 }
262} 270}
263 271
264/* How should editor hadle tabs in this file? Add editor commands here. 272/* How should editor hadle tabs in this file? Add editor commands here.
265 * vim: noexpandtab sw=8 ts=8 sts=8: 273 * vim: noexpandtab sw=8 ts=8 sts=8:
266 */ 274 */