5 A Linux/Unix toolset and driver for Nokia mobile phones.
7 Released under the terms of the GNU GPL, see file COPYING for more details.
16 #include <sys/types.h>
26 #include "gsm-common.h"
30 /* FIXME: I have timersub defined in sys/time.h :-( PJ
31 FIXME: Jano wants this function too... PJ
33 int timersub(struct timeval *a, struct timeval *b, struct timeval *result) {
35 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;
36 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;
37 if ((result)->tv_usec < 0) {
39 (result)->tv_usec += 1000000;
47 int GetLine(FILE *File, char *Line, int count) {
51 if (fgets(Line, count, File)) {
52 ptr=Line+strlen(Line)-1;
54 while ( (*ptr == '\n' || *ptr == '\r') && ptr>=Line) *ptr--='\0';
61 * like atoi, but of a non-null-terminated string of a specified portion
63 int mem_to_int(const char str[], int len)
67 strncpy(aux, str, len);
73 * make hexdump of Message
76 void hexdump(u16 MessageLength, u8 *MessageBuffer)
88 for (count = 0; count < MessageLength; count ++)
92 switch (MessageBuffer[count]) {
94 sprintf(hex1,"%02x ",MessageBuffer[count]);
98 if (isprint(MessageBuffer[count]))
99 sprintf(hex1,"%02x%c ",MessageBuffer[count],MessageBuffer[count]);
101 sprintf(hex1,"%02x ",MessageBuffer[count]);
103 if (isprint(MessageBuffer[count])) sprintf(hex2,"%c",MessageBuffer[count]);
104 else strcpy(hex2,".");
108 if ( n!=15 && count != MessageLength-1 ) hex1[3]='|';
110 strcat(string1,hex1);
111 strcat(string2,hex2);
113 if ( n==15 || count == MessageLength-1 )
115 fprintf(stdout,"%-60s%03x %s\n",string1,count+1,string2);
122 if (n!=0) fprintf (stdout,_("\n"));
127 void txhexdump(u16 MessageLength, u8 *MessageBuffer)
132 for (count = 0; count < MessageLength; count ++)
135 fprintf(stdout,_("%02x"),MessageBuffer[count]);
136 switch (MessageBuffer[count]) {
138 fprintf(stdout,_(" |"));
141 if (isprint(MessageBuffer[count])) fprintf(stdout, _("%c|"),MessageBuffer[count]);
142 else fprintf(stdout,_(" |"));
148 fprintf (stdout,_("\n"));
153 if (n!=0) fprintf (stdout,_("\n"));
161 #define max_buf_len 128
162 #define lock_path "/var/lock/LCK.."
164 /* Lock the device. Return allocated string with a lock name */
165 char *lock_device(const char* port)
167 char *lock_file = NULL;
168 char buffer[max_buf_len];
169 char *aux = rindex(port, '/');
170 int fd, len = strlen(aux) + strlen(lock_path);
172 memset(buffer, 0, sizeof(buffer));
173 lock_file = calloc(len + 1, 1);
175 fprintf(stderr, _("Cannot lock device\n"));
178 /* I think we don't need to use strncpy, as we should have enough
179 * buffer due to strlen results
181 strcpy(lock_file, lock_path);
182 strcat(lock_file, aux);
184 /* Check for the stale lockfile.
185 * The code taken from minicom by Miquel van Smoorenburg */
186 if ((fd = open(lock_file, O_RDONLY)) >= 0) {
187 char buf[max_buf_len];
190 n = read(fd, buf, sizeof(buf) - 1);
195 /* Kermit-style lockfile. */
198 /* Ascii lockfile. */
200 sscanf(buf, "%d", &pid);
202 if (pid > 0 && kill((pid_t)pid, 0) < 0 && errno == ESRCH) {
203 fprintf(stderr, _("Lockfile is stale. Overriding it..\n"));
211 fprintf(stderr, _("Device is already locked.\n"));
216 /* Try to create a new file, with 0644 mode */
217 fd = open(lock_file, O_CREAT | O_EXCL, 0644);
220 fprintf(stderr, _("Cannot lock device\n"));
223 sprintf(buffer, "%10ld gnokii\n", (long)getpid());
224 write(fd, buffer, strlen(buffer));
229 /* Removes lock and frees memory */
230 bool unlock_device(char *lock_file)
235 fprintf(stderr, _("Cannot unlock device\n"));
238 err = unlink(lock_file);