This commit was generated by cvs2svn to compensate for changes in r150,
[gnokii.git] / common / devices / unixirda.c
1 /*
2  * $Id$
3  *
4  * G N O K I I
5  *
6  * A Linux/Unix toolset and driver for Nokia mobile phones.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <sys/time.h>
31 #include <sys/poll.h>
32 #include <sys/socket.h>
33 #include <sys/ioctl.h>
34
35 #include "devices/unixirda.h"
36 #include "devices/linuxirda.h"
37
38
39 #ifndef AF_IRDA
40 #define AF_IRDA 23
41 #endif
42
43 #define INFO_LEN                22
44 #define DISCOVERY_TIMEOUT       60.0
45 #define DISCOVERY_SLEEP         0.4
46
47 static char *phone[] = {
48         "Nokia 5210",
49         "Nokia 6210", "Nokia 6250", "Nokia 6310",
50         "Nokia 7110",
51         "Nokia 8210", "Nokia 8310", "Nokia 8850"
52 };
53
54 double d_time(void)
55 {
56         double          time;
57         struct timeval  tv;
58         
59         gettimeofday(&tv, NULL);
60         
61         time = tv.tv_sec + (((double)tv.tv_usec) / 1000000.0);
62         
63         return time;
64 }
65
66 double d_sleep(double s)
67 {
68         double          time;
69         struct timeval  tv1, tv2;
70         
71         gettimeofday(&tv1, NULL);
72         usleep(s * 1000000);
73         gettimeofday(&tv2, NULL);
74         
75         time = tv2.tv_sec - tv1.tv_sec + (((double)(tv2.tv_usec - tv1.tv_usec)) / 1000000.0);
76         
77         return time;
78 }
79
80 static int irda_discover_device(int fd)
81 {
82
83         struct irda_device_list *list;
84         struct irda_device_info *dev;
85         unsigned char           *buf;
86         int                     s, len, i, j;
87         int                     daddr = -1;
88         double                  t1, t2;
89         int phones = sizeof(phone) / sizeof(*phone);
90         
91         len = sizeof(*list) + sizeof(*dev) * 10;        // 10 = max devices in discover
92         buf = malloc(len);
93         list = (struct irda_device_list *)buf;
94         dev = list->dev;
95         
96         t1 = d_time();
97         
98         do {
99                 s = len;
100                 memset(buf, 0, s);
101                 
102                 if (getsockopt(fd, SOL_IRLMP, IRLMP_ENUMDEVICES, buf, &s) == 0) {
103                         for (i = 0; (i < list->len) && (daddr == -1); i++) {
104                                 for (j = 0; (j < phones) && (daddr == -1); j++) {
105                                         if (strncmp(dev[i].info, phone[j], INFO_LEN) == 0) {
106                                                 daddr = dev[i].daddr;
107 #ifdef DEBUG
108                                                 fprintf(stdout,_("%s\t%x\n"), dev[i].info, dev[i].daddr);
109 #endif
110                                         }
111                                 }
112                                 if (daddr == -1) {
113 #ifdef DEBUG
114                                         fprintf(stdout,_("unknown: %s\t%x\n"), dev[i].info, dev[i].daddr);
115 #endif
116                                 }
117                         }
118                 }
119                 
120                 if (daddr == -1) {
121                         d_sleep(DISCOVERY_SLEEP);
122                 }
123                 
124                 t2 = d_time();
125                 
126         } while ((t2 - t1 < DISCOVERY_TIMEOUT) && (daddr == -1));
127         
128         free(buf);
129         
130         return daddr;
131 }
132
133 int irda_open(void)
134 {
135         struct sockaddr_irda    peer;
136         int                     fd = -1, daddr;
137         int                     pgrp;         
138         
139         
140         fd = socket(AF_IRDA, SOCK_STREAM, 0);   /* Create socket */
141         if (fd == -1) {
142                 perror("socket");
143                 exit(1);
144              }
145
146         /* discover the devices */ 
147         daddr = irda_discover_device(fd);
148         if (daddr == -1)  {
149                         printf("irda_discover: no nokia devices found");
150                         exit(1);
151                      }
152
153         /* Arrange for the current process to receive
154            SIGIO when the state of the socket changes. */
155         pgrp = getpid();
156         if (fcntl (fd, F_SETOWN, pgrp) < 0)
157         perror("F_SETOWN");
158
159         /*  Set the socket state for Asynchronous  */
160         if (fcntl (fd, F_SETFL, FASYNC) < 0) {
161                 perror("fcntl");
162                         exit(1);
163                      }
164
165         peer.sir_family = AF_IRDA;
166         peer.sir_lsap_sel = LSAP_ANY;
167         peer.sir_addr = daddr;
168         strcpy(peer.sir_name, "Nokia:PhoNet");
169                 
170         if (connect(fd, (struct sockaddr *)&peer, sizeof(peer))) {      /* Connect to service "Nokia:PhoNet" */
171                 perror("connect");
172                 close(fd);
173                 fd = -1;
174                 }
175         
176         return fd;
177 }
178
179 int irda_close(int fd)
180 {
181         shutdown(fd, 0);
182         return close(fd);
183 }
184
185 int irda_write(int __fd, __const __ptr_t __bytes, int __size)
186 {
187   int actual,ret;
188
189    actual = 0;
190    
191    do {
192     if ((ret = send(__fd, __bytes, __size - actual, 0)) < 0)
193         return(actual);
194
195         actual += ret;
196         __bytes += ret;
197
198     } while (actual < __size);
199
200     return (actual);
201 }
202
203 int irda_read(int __fd, __ptr_t __bytes, int __size)
204 {
205         return (recv(__fd, __bytes, __size, 0));
206 }
207
208 int irda_select(int fd, struct timeval *timeout)
209 {
210         fd_set readfds;
211
212         FD_ZERO(&readfds);
213         FD_SET(fd, &readfds);
214
215         return (select(fd + 1, &readfds, NULL, NULL, timeout));
216 }