Diffstat (limited to 'gammu/emb/common/device/irda/irda.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | gammu/emb/common/device/irda/irda.c | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/gammu/emb/common/device/irda/irda.c b/gammu/emb/common/device/irda/irda.c new file mode 100644 index 0000000..fef50ac --- a/dev/null +++ b/gammu/emb/common/device/irda/irda.c | |||
@@ -0,0 +1,187 @@ | |||
1 | /* (c) 2001-2004 by Marcin Wiacek */ | ||
2 | /* based on some work from Ralf Thelen and MyGnokii */ | ||
3 | /* based on some work from Gnokii and MSDN */ | ||
4 | |||
5 | /* You have to include wsock32.lib library to MS VC project to compile it */ | ||
6 | |||
7 | #include "../../gsmstate.h" | ||
8 | |||
9 | #ifdef GSM_ENABLE_IRDADEVICE | ||
10 | #ifndef DJGPP | ||
11 | |||
12 | #ifndef WIN32 | ||
13 | # include <stdlib.h> | ||
14 | # include <unistd.h> | ||
15 | # include <stdio.h> | ||
16 | # include <fcntl.h> | ||
17 | # include <errno.h> | ||
18 | # include <string.h> | ||
19 | # include <sys/time.h> | ||
20 | # include <sys/poll.h> | ||
21 | # include <sys/socket.h> | ||
22 | # include <sys/ioctl.h> | ||
23 | #else | ||
24 | # include <windows.h> | ||
25 | # include <io.h> | ||
26 | #endif | ||
27 | |||
28 | #include "../../gsmcomon.h" | ||
29 | #include "../devfunc.h" | ||
30 | #include "irda.h" | ||
31 | |||
32 | static bool irda_discover_device(GSM_StateMachine *state) | ||
33 | { | ||
34 | GSM_Device_IrdaData *d = &state->Device.Data.Irda; | ||
35 | struct irda_device_list*list; | ||
36 | unsigned char *buf; | ||
37 | unsigned int sec; | ||
38 | int s, z, len, fd, i; | ||
39 | GSM_DateTime Date; | ||
40 | bool founddevice = false; | ||
41 | #ifdef WIN32 | ||
42 | int index; | ||
43 | #endif | ||
44 | |||
45 | fd = socket(AF_IRDA, SOCK_STREAM, 0); | ||
46 | |||
47 | /* can handle maximally 10 devices during discovering */ | ||
48 | len = sizeof(struct irda_device_list) + sizeof(struct irda_device_info) * 10; | ||
49 | buf = malloc(len); | ||
50 | list = (struct irda_device_list *)buf; | ||
51 | |||
52 | /* Trying to find device during 2 seconds */ | ||
53 | for (z=0;z<2;z++) { | ||
54 | GSM_GetCurrentDateTime (&Date); | ||
55 | sec = Date.Second; | ||
56 | while (sec==Date.Second) { | ||
57 | s = len; | ||
58 | memset(buf, 0, s); | ||
59 | |||
60 | if (getsockopt(fd, SOL_IRLMP, IRLMP_ENUMDEVICES, buf, &s) == 0) { | ||
61 | for (i = 0; i < (int)list->numDevice; i++) { | ||
62 | dbgprintf("Irda: found device \"%s\" (address %x) - ",list->Device[i].irdaDeviceName,list->Device[i].irdaDeviceID); | ||
63 | if (strcmp(GetModelData(NULL,NULL,list->Device[i].irdaDeviceName)->number,"") != 0) { | ||
64 | founddevice = true; | ||
65 | /* Model AUTO */ | ||
66 | if (state->CurrentConfig->Model[0]==0) strcpy(state->Phone.Data.Model,GetModelData(NULL,NULL,list->Device[i].irdaDeviceName)->number); | ||
67 | state->Phone.Data.ModelInfo = GetModelData(NULL,state->Phone.Data.Model,NULL); | ||
68 | } | ||
69 | if (founddevice) { | ||
70 | dbgprintf("correct\n"); | ||
71 | #ifdef WIN32 | ||
72 | for(index=0; index <= 3; index++) | ||
73 | d->peer.irdaDeviceID[index] = list->Device[i].irdaDeviceID[index]; | ||
74 | #else | ||
75 | d->peer.irdaDeviceID = list->Device[i].irdaDeviceID; | ||
76 | #endif | ||
77 | break; | ||
78 | } | ||
79 | dbgprintf("\n"); | ||
80 | } | ||
81 | } | ||
82 | if (founddevice) break; | ||
83 | my_sleep(10); | ||
84 | GSM_GetCurrentDateTime(&Date); | ||
85 | } | ||
86 | if (founddevice) break; | ||
87 | } | ||
88 | free(buf); | ||
89 | close(fd); | ||
90 | |||
91 | return founddevice; | ||
92 | } | ||
93 | |||
94 | static GSM_Error irda_open (GSM_StateMachine *s) | ||
95 | { | ||
96 | GSM_Device_IrdaData *d = &s->Device.Data.Irda; | ||
97 | int fd = -1; | ||
98 | #ifdef WIN32 | ||
99 | int Enable9WireMode = 1; | ||
100 | WSADATA wsaData; | ||
101 | |||
102 | WSAStartup(MAKEWORD(1,1), &wsaData); | ||
103 | #else | ||
104 | if (s->ConnectionType == GCT_IRDAAT) return ERR_SOURCENOTAVAILABLE; | ||
105 | #endif | ||
106 | |||
107 | /* discovering devices */ | ||
108 | if (irda_discover_device(s)==false) return ERR_TIMEOUT; | ||
109 | |||
110 | /* Creating socket */ | ||
111 | fd = socket(AF_IRDA, SOCK_STREAM, 0); | ||
112 | |||
113 | d->peer.irdaAddressFamily = AF_IRDA; | ||
114 | #ifndef WIN32 | ||
115 | d->peer.sir_lsap_sel = LSAP_ANY; | ||
116 | #endif | ||
117 | switch (s->ConnectionType) { | ||
118 | case GCT_IRDAAT: | ||
119 | strcpy(d->peer.irdaServiceName, "IrDA:IrCOMM"); | ||
120 | |||
121 | #ifdef WIN32 | ||
122 | if (setsockopt(fd, SOL_IRLMP, IRLMP_9WIRE_MODE, (const char *) &Enable9WireMode, | ||
123 | sizeof(int))==SOCKET_ERROR) return ERR_UNKNOWN; | ||
124 | #endif | ||
125 | break; | ||
126 | case GCT_IRDAPHONET: | ||
127 | strcpy(d->peer.irdaServiceName, "Nokia:PhoNet"); | ||
128 | break; | ||
129 | case GCT_IRDAOBEX: | ||
130 | /* IrDA:OBEX not supported by N3650 */ | ||
131 | // strcpy(d->peer.irdaServiceName, "IrDA:OBEX"); | ||
132 | |||
133 | strcpy(d->peer.irdaServiceName, "OBEX"); | ||
134 | |||
135 | /* Alternative server is "OBEX:IrXfer" */ | ||
136 | break; | ||
137 | default: | ||
138 | return ERR_UNKNOWN; | ||
139 | } | ||
140 | |||
141 | /* Connecting to service */ | ||
142 | if (connect(fd, (struct sockaddr *)&d->peer, sizeof(d->peer))) { | ||
143 | dbgprintf("Can't connect to service %s\n",d->peer.irdaServiceName); | ||
144 | close(fd); | ||
145 | return ERR_NOTSUPPORTED; | ||
146 | } | ||
147 | |||
148 | d->hPhone=fd; | ||
149 | |||
150 | return ERR_NONE; | ||
151 | } | ||
152 | |||
153 | static int irda_read(GSM_StateMachine *s, void *buf, size_t nbytes) | ||
154 | { | ||
155 | return socket_read(s, buf, nbytes, s->Device.Data.Irda.hPhone); | ||
156 | } | ||
157 | |||
158 | #ifdef WIN32 | ||
159 | static int irda_write(GSM_StateMachine *s, unsigned char *buf, size_t nbytes) | ||
160 | #else | ||
161 | static int irda_write(GSM_StateMachine *s, void *buf, size_t nbytes) | ||
162 | #endif | ||
163 | { | ||
164 | return socket_write(s, buf, nbytes, s->Device.Data.Irda.hPhone); | ||
165 | } | ||
166 | |||
167 | static GSM_Error irda_close(GSM_StateMachine *s) | ||
168 | { | ||
169 | return socket_close(s, s->Device.Data.Irda.hPhone); | ||
170 | } | ||
171 | |||
172 | GSM_Device_Functions IrdaDevice = { | ||
173 | irda_open, | ||
174 | irda_close, | ||
175 | NONEFUNCTION, | ||
176 | NONEFUNCTION, | ||
177 | NONEFUNCTION, | ||
178 | irda_read, | ||
179 | irda_write | ||
180 | }; | ||
181 | |||
182 | #endif | ||
183 | #endif | ||
184 | |||
185 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
186 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
187 | */ | ||