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.
18 #include <sys/types.h>
28 #include "gsm-common.h"
32 /* FIXME: I have timersub defined in sys/time.h :-( PJ
33 FIXME: Jano wants this function too... PJ
35 int timersub(struct timeval *a, struct timeval *b, struct timeval *result) {
37 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;
38 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;
39 if ((result)->tv_usec < 0) {
41 (result)->tv_usec += 1000000;
49 int GetLine(FILE *File, char *Line, int count) {
53 if (fgets(Line, count, File)) {
54 ptr=Line+strlen(Line)-1;
56 while ( (*ptr == '\n' || *ptr == '\r') && ptr>=Line) *ptr--='\0';
65 * like atoi, but of a non-null-terminated string of a specified portion
67 int mem_to_int(const char str[], int len)
71 strncpy(aux, str, len);
79 * make hexdump of Message
82 void hexdump(u16 MessageLength, u8 *MessageBuffer)
92 for (count = 0; count < MessageLength; count ++)
96 switch (MessageBuffer[count]) {
98 sprintf(hex1,"%02x ",MessageBuffer[count]);
102 if (isprint(MessageBuffer[count]))
103 sprintf(hex1,"%02x%c ",MessageBuffer[count],MessageBuffer[count]);
105 sprintf(hex1,"%02x ",MessageBuffer[count]);
107 if (isprint(MessageBuffer[count])) sprintf(hex2,"%c",MessageBuffer[count]);
108 else strcpy(hex2,".");
112 if ( n!=15 && count != MessageLength-1 ) hex1[3]='|';
114 strcat(string1,hex1);
115 strcat(string2,hex2);
117 if ( n==15 || count == MessageLength-1 )
119 fprintf(stdout,"%-60s%03x %s\n",string1,count+1,string2);
126 if (n!=0) fprintf (stdout,_("\n"));
131 void txhexdump(u16 MessageLength, u8 *MessageBuffer)
136 for (count = 0; count < MessageLength; count ++)
139 fprintf(stdout,_("%02x"),MessageBuffer[count]);
140 switch (MessageBuffer[count]) {
142 fprintf(stdout,_(" |"));
145 if (isprint(MessageBuffer[count])) fprintf(stdout, _("%c|"),MessageBuffer[count]);
146 else fprintf(stdout,_(" |"));
152 fprintf (stdout,_("\n"));
157 if (n!=0) fprintf (stdout,_("\n"));
167 #define max_buf_len 128
168 #define lock_path "/var/lock/LCK.."
170 /* Lock the device. Return allocated string with a lock name */
171 char *lock_device(const char* port)
173 char *lock_file = NULL;
174 char buffer[max_buf_len];
175 char *aux = rindex(port, '/');
176 int fd, len = strlen(aux) + strlen(lock_path);
178 memset(buffer, 0, sizeof(buffer));
179 lock_file = calloc(len + 1, 1);
181 fprintf(stderr, _("Cannot lock device\n"));
184 /* I think we don't need to use strncpy, as we should have enough
185 * buffer due to strlen results
187 strcpy(lock_file, lock_path);
188 strcat(lock_file, aux);
190 /* Check for the stale lockfile.
191 * The code taken from minicom by Miquel van Smoorenburg */
192 if ((fd = open(lock_file, O_RDONLY)) >= 0) {
193 char buf[max_buf_len];
196 n = read(fd, buf, sizeof(buf) - 1);
201 /* Kermit-style lockfile. */
204 /* Ascii lockfile. */
206 sscanf(buf, "%d", &pid);
208 if (pid > 0 && kill((pid_t)pid, 0) < 0 && errno == ESRCH) {
209 fprintf(stderr, _("Lockfile is stale. Overriding it..\n"));
217 fprintf(stderr, _("Device is already locked.\n"));
222 /* Try to create a new file, with 0644 mode */
223 fd = open(lock_file, O_CREAT | O_EXCL, 0644);
226 fprintf(stderr, _("Cannot lock device\n"));
229 sprintf(buffer, "%10ld gnokii\n", (long)getpid());
230 write(fd, buffer, strlen(buffer));
235 /* Removes lock and frees memory */
236 static bool unlock_device(char *lock_file)
241 fprintf(stderr, _("Cannot unlock device\n"));
244 err = unlink(lock_file);