Imported gammu 0.90.7
[gammu.git] / common / device / irda / irda.c
blob02710cf61ea428402cad29b2fc94f227d0c3bf5c
2 /* Have to include wsock32.lib library to MS VC6 project to compile it */
4 #include "../../gsmstate.h"
6 #ifdef GSM_ENABLE_IRDADEVICE
7 #ifndef DJGPP
9 #ifndef WIN32
10 # include <stdlib.h>
11 # include <unistd.h>
12 # include <stdio.h>
13 # include <fcntl.h>
14 # include <errno.h>
15 # include <string.h>
16 # include <sys/time.h>
17 # include <sys/poll.h>
18 # include <sys/socket.h>
19 # include <sys/ioctl.h>
20 #else
21 # include <windows.h>
22 # include <io.h>
23 #endif
25 #include "../../gsmcomon.h"
26 #include "../devfunc.h"
27 #include "irda.h"
29 static bool irda_discover_device(GSM_StateMachine *state)
31 GSM_Device_IrdaData *d = &state->Device.Data.Irda;
32 struct irda_device_list *list;
33 unsigned char *buf;
34 unsigned int sec;
36 int s, z, len, fd, i;
37 GSM_DateTime Date;
38 bool founddevice = false;
40 #ifdef WIN32
41 WSADATA wsaData;
42 int index;
44 WSAStartup(MAKEWORD(1,1), &wsaData);
45 #endif
47 fd = socket(AF_IRDA, SOCK_STREAM, 0);
49 /* maximally 10 devices in discover */
50 len = sizeof(struct irda_device_list) + sizeof(struct irda_device_info) * 10;
51 buf = malloc(len);
52 list = (struct irda_device_list *)buf;
54 /* Trying to find device during 2 seconds */
55 for (z=0;z<2;z++) {
56 GSM_GetCurrentDateTime (&Date);
57 sec = Date.Second;
58 while (sec==Date.Second) {
59 s = len;
60 memset(buf, 0, s);
62 if (getsockopt(fd, SOL_IRLMP, IRLMP_ENUMDEVICES, buf, &s) == 0) {
63 for (i = 0; i < (int)list->numDevice; i++) {
64 dbgprintf("Irda: found device \"%s\" (address %x) - ",list->Device[i].irdaDeviceName,list->Device[i].irdaDeviceID);
65 if (strcmp(GetModelData(NULL,NULL,list->Device[i].irdaDeviceName)->number,"") != 0) {
66 founddevice = true;
67 /* Model AUTO */
68 if (state->CurrentConfig->Model[0]==0) strcpy(state->Phone.Data.Model,GetModelData(NULL,NULL,list->Device[i].irdaDeviceName)->number);
69 state->Phone.Data.ModelInfo = GetModelData(NULL,state->Phone.Data.Model,NULL);
71 if (founddevice) {
72 dbgprintf("correct\n");
73 #ifdef WIN32
74 for(index=0; index <= 3; index++)
75 d->peer.irdaDeviceID[index] = list->Device[i].irdaDeviceID[index];
76 #else
77 d->peer.irdaDeviceID = list->Device[i].irdaDeviceID;
78 #endif
79 break;
81 dbgprintf("\n");
84 if (founddevice) break;
85 my_sleep(10);
86 GSM_GetCurrentDateTime(&Date);
88 if (founddevice) break;
90 free(buf);
91 close(fd);
93 return founddevice;
96 static GSM_Error irda_open (GSM_StateMachine *s)
98 #ifdef WIN32
99 int Enable9WireMode = 1;
100 #endif
101 GSM_Device_IrdaData *d = &s->Device.Data.Irda;
102 int fd = -1;
104 #ifndef WIN32
105 if (s->ConnectionType == GCT_IRDAAT) return GE_SOURCENOTAVAILABLE;
106 #endif
108 /* discover the devices */
109 if (irda_discover_device(s)==false) return GE_TIMEOUT;
111 /* Create socket */
112 fd = socket(AF_IRDA, SOCK_STREAM, 0);
114 d->peer.irdaAddressFamily = AF_IRDA;
115 #ifndef WIN32
116 d->peer.sir_lsap_sel = LSAP_ANY;
117 #endif
118 switch (s->ConnectionType) {
119 case GCT_IRDAAT:
120 strcpy(d->peer.irdaServiceName, "IrDA:IrCOMM");
122 #ifdef WIN32
123 if (setsockopt(fd, SOL_IRLMP, IRLMP_9WIRE_MODE, (const char *) &Enable9WireMode,
124 sizeof(int))==SOCKET_ERROR) return GE_UNKNOWN;
125 #endif
126 break;
127 case GCT_IRDAPHONET:
128 strcpy(d->peer.irdaServiceName, "Nokia:PhoNet");
129 break;
130 case GCT_IRDAOBEX:
131 /* IrDA:OBEX not supported by N3650 */
132 // strcpy(d->peer.irdaServiceName, "IrDA:OBEX");
134 strcpy(d->peer.irdaServiceName, "OBEX");
136 /* Alternative server is "OBEX:IrXfer" */
137 break;
138 default:
139 return GE_UNKNOWN;
142 /* Connect to service */
143 if (connect(fd, (struct sockaddr *)&d->peer, sizeof(d->peer))) {
144 dbgprintf("Can't connect to service %s\n",d->peer.irdaServiceName);
145 close(fd);
146 return GE_NOTSUPPORTED;
149 d->hPhone=fd;
151 return GE_NONE;
154 static int irda_read(GSM_StateMachine *s, void *buf, size_t nbytes)
156 return socket_read(s, buf, nbytes, s->Device.Data.Irda.hPhone);
159 #ifdef WIN32
160 static int irda_write(GSM_StateMachine *s, unsigned char *buf, size_t nbytes)
161 #else
162 static int irda_write(GSM_StateMachine *s, void *buf, size_t nbytes)
163 #endif
165 return socket_write(s, buf, nbytes, s->Device.Data.Irda.hPhone);
168 static GSM_Error irda_close(GSM_StateMachine *s)
170 return socket_close(s, s->Device.Data.Irda.hPhone);
173 GSM_Device_Functions IrdaDevice = {
174 irda_open,
175 irda_close,
176 NONEFUNCTION,
177 NONEFUNCTION,
178 NONEFUNCTION,
179 irda_read,
180 irda_write
183 #endif
184 #endif
186 /* How should editor hadle tabs in this file? Add editor commands here.
187 * vim: noexpandtab sw=8 ts=8 sts=8: