6 * A Linux/Unix toolset and driver for Nokia mobile phones.
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.
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.
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
32 #include <sys/socket.h>
33 #include <sys/ioctl.h>
35 #include "devices/unixirda.h"
36 #include "devices/linuxirda.h"
44 #define DISCOVERY_TIMEOUT 60.0
45 #define DISCOVERY_SLEEP 0.4
47 static char *phone[] = {
49 "Nokia 6210", "Nokia 6250", "Nokia 6310",
51 "Nokia 8210", "Nokia 8310", "Nokia 8850"
59 gettimeofday(&tv, NULL);
61 time = tv.tv_sec + (((double)tv.tv_usec) / 1000000.0);
66 double d_sleep(double s)
69 struct timeval tv1, tv2;
71 gettimeofday(&tv1, NULL);
73 gettimeofday(&tv2, NULL);
75 time = tv2.tv_sec - tv1.tv_sec + (((double)(tv2.tv_usec - tv1.tv_usec)) / 1000000.0);
80 static int irda_discover_device(int fd)
83 struct irda_device_list *list;
84 struct irda_device_info *dev;
89 int phones = sizeof(phone) / sizeof(*phone);
91 len = sizeof(*list) + sizeof(*dev) * 10; // 10 = max devices in discover
93 list = (struct irda_device_list *)buf;
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;
108 fprintf(stdout,_("%s\t%x\n"), dev[i].info, dev[i].daddr);
114 fprintf(stdout,_("unknown: %s\t%x\n"), dev[i].info, dev[i].daddr);
121 d_sleep(DISCOVERY_SLEEP);
126 } while ((t2 - t1 < DISCOVERY_TIMEOUT) && (daddr == -1));
135 struct sockaddr_irda peer;
140 fd = socket(AF_IRDA, SOCK_STREAM, 0); /* Create socket */
146 /* discover the devices */
147 daddr = irda_discover_device(fd);
149 printf("irda_discover: no nokia devices found");
153 /* Arrange for the current process to receive
154 SIGIO when the state of the socket changes. */
156 if (fcntl (fd, F_SETOWN, pgrp) < 0)
159 /* Set the socket state for Asynchronous */
160 if (fcntl (fd, F_SETFL, FASYNC) < 0) {
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");
170 if (connect(fd, (struct sockaddr *)&peer, sizeof(peer))) { /* Connect to service "Nokia:PhoNet" */
179 int irda_close(int fd)
185 int irda_write(int __fd, __const __ptr_t __bytes, int __size)
192 if ((ret = send(__fd, __bytes, __size - actual, 0)) < 0)
198 } while (actual < __size);
203 int irda_read(int __fd, __ptr_t __bytes, int __size)
205 return (recv(__fd, __bytes, __size, 0));
208 int irda_select(int fd, struct timeval *timeout)
213 FD_SET(fd, &readfds);
215 return (select(fd + 1, &readfds, NULL, NULL, timeout));