Diffstat (limited to 'gammu/emb/common/device/irda') (more/less context) (ignore whitespace changes)
-rw-r--r-- | gammu/emb/common/device/irda/irda.c | 187 | ||||
-rw-r--r-- | gammu/emb/common/device/irda/irda.h | 22 | ||||
-rw-r--r-- | gammu/emb/common/device/irda/irda_unx.h | 61 | ||||
-rw-r--r-- | gammu/emb/common/device/irda/irda_w32.h | 35 |
4 files changed, 305 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 | */ | ||
diff --git a/gammu/emb/common/device/irda/irda.h b/gammu/emb/common/device/irda/irda.h new file mode 100644 index 0000000..455e6af --- a/dev/null +++ b/gammu/emb/common/device/irda/irda.h | |||
@@ -0,0 +1,22 @@ | |||
1 | |||
2 | #ifndef DJGPP | ||
3 | #ifndef unixirda_h | ||
4 | #define unixirda_h | ||
5 | |||
6 | #ifndef WIN32 | ||
7 | # include "irda_unx.h" | ||
8 | #else | ||
9 | # include "irda_w32.h" | ||
10 | #endif | ||
11 | |||
12 | typedef struct { | ||
13 | int hPhone; | ||
14 | struct sockaddr_irdapeer; | ||
15 | } GSM_Device_IrdaData; | ||
16 | |||
17 | #endif | ||
18 | #endif | ||
19 | |||
20 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
21 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
22 | */ | ||
diff --git a/gammu/emb/common/device/irda/irda_unx.h b/gammu/emb/common/device/irda/irda_unx.h new file mode 100644 index 0000000..8dbcb97 --- a/dev/null +++ b/gammu/emb/common/device/irda/irda_unx.h | |||
@@ -0,0 +1,61 @@ | |||
1 | /* part of irda.h available in Linux kernel source */ | ||
2 | |||
3 | /********************************************************************* | ||
4 | * | ||
5 | * Filename: irda.h | ||
6 | * Version: | ||
7 | * Description: | ||
8 | * Status: Experimental. | ||
9 | * Author: Dag Brattli <dagb@cs.uit.no> | ||
10 | * Created at: Mon Mar 8 14:06:12 1999 | ||
11 | * Modified at: Sat Dec 25 16:06:42 1999 | ||
12 | * Modified by: Dag Brattli <dagb@cs.uit.no> | ||
13 | * | ||
14 | * Copyright (c) 1999 Dag Brattli, All Rights Reserved. | ||
15 | * | ||
16 | * This program is free software; you can redistribute it and/or | ||
17 | * modify it under the terms of the GNU General Public License as | ||
18 | * published by the Free Software Foundation; either version 2 of | ||
19 | * the License, or (at your option) any later version. | ||
20 | * | ||
21 | * Neither Dag Brattli nor University of Tromsų admit liability nor | ||
22 | * provide warranty for any of this software. This material is | ||
23 | * provided "AS-IS" and at no charge. | ||
24 | * | ||
25 | ********************************************************************/ | ||
26 | |||
27 | #ifndef __irda_unx_h | ||
28 | #define __irda_unx_h | ||
29 | |||
30 | #include <sys/types.h> | ||
31 | #include <sys/socket.h> | ||
32 | |||
33 | #define SOL_IRLMP 266 /* Same as SOL_IRDA for now */ | ||
34 | #define IRLMP_ENUMDEVICES 1 /* Return discovery log */ | ||
35 | #define LSAP_ANY 0xff | ||
36 | |||
37 | struct sockaddr_irda { | ||
38 | sa_family_t irdaAddressFamily;/* AF_IRDA */ | ||
39 | u_int8_t sir_lsap_sel; /* LSAP selector */ | ||
40 | u_int32_t irdaDeviceID; /* Device address */ | ||
41 | char irdaServiceName[25]; /* Usually <service>:IrDA:TinyTP */ | ||
42 | }; | ||
43 | |||
44 | struct irda_device_info { | ||
45 | u_int32_t saddr; /* Address of local interface */ | ||
46 | u_int32_t irdaDeviceID; /* Address of remote device */ | ||
47 | char irdaDeviceName[22]; /* Description */ | ||
48 | u_int8_t charset; /* Charset used for description */ | ||
49 | u_int8_t hints[2]; /* Hint bits */ | ||
50 | }; | ||
51 | |||
52 | struct irda_device_list { | ||
53 | u_int32_t numDevice; | ||
54 | struct irda_device_info Device[1]; | ||
55 | }; | ||
56 | |||
57 | #endif | ||
58 | |||
59 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
60 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
61 | */ | ||
diff --git a/gammu/emb/common/device/irda/irda_w32.h b/gammu/emb/common/device/irda/irda_w32.h new file mode 100644 index 0000000..daffa37 --- a/dev/null +++ b/gammu/emb/common/device/irda/irda_w32.h | |||
@@ -0,0 +1,35 @@ | |||
1 | |||
2 | /* MS Platform SDK */ | ||
3 | |||
4 | #ifndef __irda_w32_h | ||
5 | #define __irda_w32_h | ||
6 | |||
7 | #define AF_IRDA 26 | ||
8 | #define SOL_IRLMP 0x00FF | ||
9 | #define IRLMP_ENUMDEVICES 0x00000010 | ||
10 | #define IRLMP_9WIRE_MODE 0x00000016 | ||
11 | |||
12 | struct sockaddr_irda { | ||
13 | unsigned short irdaAddressFamily; | ||
14 | unsigned char irdaDeviceID[4]; | ||
15 | char irdaServiceName[25]; | ||
16 | }; | ||
17 | |||
18 | struct irda_device_info { | ||
19 | unsigned char irdaDeviceID[4]; | ||
20 | char irdaDeviceName[22]; | ||
21 | unsigned char irdaDeviceHints1; | ||
22 | unsigned char irdaDeviceHints2; | ||
23 | unsigned char irdaCharSet; | ||
24 | }; | ||
25 | |||
26 | struct irda_device_list { | ||
27 | ULONG numDevice; | ||
28 | struct irda_device_info Device[1]; | ||
29 | }; | ||
30 | |||
31 | #endif | ||
32 | |||
33 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
34 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
35 | */ | ||