Upgrade ofono to 1.2
[profile/ivi/ofono.git] / gatchat / gatrawip.c
1 /*
2  *
3  *  AT chat library with GLib integration
4  *
5  *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <sys/ioctl.h>
30 #include <net/if.h>
31 #include <linux/if_tun.h>
32
33 #include <glib.h>
34
35 #include "ringbuffer.h"
36 #include "gatrawip.h"
37
38 struct _GAtRawIP {
39         gint ref_count;
40         GAtIO *io;
41         GAtIO *tun_io;
42         char *ifname;
43         struct ring_buffer *write_buffer;
44         struct ring_buffer *tun_write_buffer;
45         GAtDebugFunc debugf;
46         gpointer debug_data;
47 };
48
49 GAtRawIP *g_at_rawip_new(GIOChannel *channel)
50 {
51         GAtRawIP *rawip;
52         GAtIO *io;
53
54         io = g_at_io_new(channel);
55         if (io == NULL)
56                 return NULL;
57
58         rawip = g_at_rawip_new_from_io(io);
59
60         g_at_io_unref(io);
61
62         return rawip;
63 }
64
65 GAtRawIP *g_at_rawip_new_from_io(GAtIO *io)
66 {
67         GAtRawIP *rawip;
68
69         rawip = g_try_new0(GAtRawIP, 1);
70         if (rawip == NULL)
71                 return NULL;
72
73         rawip->ref_count = 1;
74
75         rawip->write_buffer = NULL;
76         rawip->tun_write_buffer = NULL;
77
78         rawip->io = g_at_io_ref(io);
79
80         return rawip;
81 }
82
83 GAtRawIP *g_at_rawip_ref(GAtRawIP *rawip)
84 {
85         if (rawip == NULL)
86                 return NULL;
87
88         g_atomic_int_inc(&rawip->ref_count);
89
90         return rawip;
91 }
92
93 void g_at_rawip_unref(GAtRawIP *rawip)
94 {
95         if (rawip == NULL)
96                 return;
97
98         if (g_atomic_int_dec_and_test(&rawip->ref_count) == FALSE)
99                 return;
100
101         g_at_rawip_shutdown(rawip);
102
103         g_at_io_unref(rawip->io);
104         rawip->io = NULL;
105
106         g_free(rawip->ifname);
107         rawip->ifname = NULL;
108
109         g_free(rawip);
110 }
111
112 static gboolean can_write_data(gpointer data)
113 {
114         GAtRawIP *rawip = data;
115         unsigned int len;
116         unsigned char *buf;
117         gsize bytes_written;
118
119         if (rawip->write_buffer == NULL)
120                 return FALSE;
121
122         len = ring_buffer_len_no_wrap(rawip->write_buffer);
123         buf = ring_buffer_read_ptr(rawip->write_buffer, 0);
124
125         bytes_written = g_at_io_write(rawip->io, (gchar *) buf, len);
126         ring_buffer_drain(rawip->write_buffer, bytes_written);
127
128         if (ring_buffer_len(rawip->write_buffer) > 0)
129                 return TRUE;
130
131         rawip->write_buffer = NULL;
132
133         return FALSE;
134 }
135
136 static gboolean tun_write_data(gpointer data)
137 {
138         GAtRawIP *rawip = data;
139         unsigned int len;
140         unsigned char *buf;
141         gsize bytes_written;
142
143         if (rawip->tun_write_buffer == NULL)
144                 return FALSE;
145
146         len = ring_buffer_len_no_wrap(rawip->tun_write_buffer);
147         buf = ring_buffer_read_ptr(rawip->tun_write_buffer, 0);
148
149         bytes_written = g_at_io_write(rawip->tun_io, (gchar *) buf, len);
150         ring_buffer_drain(rawip->tun_write_buffer, bytes_written);
151
152         if (ring_buffer_len(rawip->tun_write_buffer) > 0)
153                 return TRUE;
154
155         rawip->tun_write_buffer = NULL;
156
157         return FALSE;
158 }
159
160 static void new_bytes(struct ring_buffer *rbuf, gpointer user_data)
161 {
162         GAtRawIP *rawip = user_data;
163
164         rawip->tun_write_buffer = rbuf;
165
166         g_at_io_set_write_handler(rawip->tun_io, tun_write_data, rawip);
167 }
168
169 static void tun_bytes(struct ring_buffer *rbuf, gpointer user_data)
170 {
171         GAtRawIP *rawip = user_data;
172
173         rawip->write_buffer = rbuf;
174
175         g_at_io_set_write_handler(rawip->io, can_write_data, rawip);
176 }
177
178 static void create_tun(GAtRawIP *rawip)
179 {
180         GIOChannel *channel;
181         struct ifreq ifr;
182         int fd, err;
183
184         fd = open("/dev/net/tun", O_RDWR);
185         if (fd < 0)
186                 return;
187
188         memset(&ifr, 0, sizeof(ifr));
189         ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
190         strcpy(ifr.ifr_name, "gprs%d");
191
192         err = ioctl(fd, TUNSETIFF, (void *) &ifr);
193         if (err < 0) {
194                 close(fd);
195                 return;
196         }
197
198         rawip->ifname = g_strdup(ifr.ifr_name);
199
200         channel = g_io_channel_unix_new(fd);
201         if (channel == NULL) {
202                 close(fd);
203                 return;
204         }
205
206         rawip->tun_io = g_at_io_new(channel);
207
208         g_io_channel_unref(channel);
209 }
210
211 void g_at_rawip_open(GAtRawIP *rawip)
212 {
213         if (rawip == NULL)
214                 return;
215
216         create_tun(rawip);
217
218         if (rawip->tun_io == NULL)
219                 return;
220
221         g_at_io_set_read_handler(rawip->io, new_bytes, rawip);
222         g_at_io_set_read_handler(rawip->tun_io, tun_bytes, rawip);
223 }
224
225 void g_at_rawip_shutdown(GAtRawIP *rawip)
226 {
227         if (rawip == NULL)
228                 return;
229
230         if (rawip->tun_io == NULL)
231                 return;
232
233         g_at_io_set_read_handler(rawip->io, NULL, NULL);
234         g_at_io_set_read_handler(rawip->tun_io, NULL, NULL);
235
236         rawip->write_buffer = NULL;
237         rawip->tun_write_buffer = NULL;
238
239         g_at_io_unref(rawip->tun_io);
240         rawip->tun_io = NULL;
241 }
242
243 const char *g_at_rawip_get_interface(GAtRawIP *rawip)
244 {
245         if (rawip == NULL)
246                 return NULL;
247
248         return rawip->ifname;
249 }
250
251 void g_at_rawip_set_debug(GAtRawIP *rawip, GAtDebugFunc func,
252                                                 gpointer user_data)
253 {
254         if (rawip == NULL)
255                 return;
256
257         rawip->debugf = func;
258         rawip->debug_data = user_data;
259 }