summaryrefslogtreecommitdiffstats
path: root/gprs.c
blob: 99460e6f01180a0a7db398c28733c3efb038ecc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
 * This file is part of samsung-ril.
 *
 * Copyright (C) 2011 Denis 'GNUtoo' Carikli <GNUtoo@no-log.org>
 * Copyright (C) 2011 Paul Kocialkowski <contact@oaulk.fr>
 *
 * samsung-ril is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * samsung-ril is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with samsung-ril.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <netinet/in.h>
#include <arpa/inet.h>

#define LOG_TAG "RIL-GPRS"
#include <utils/Log.h>
#include <cutils/properties.h>
#include <netutils/ifc.h>

#include "samsung-ril.h"
#include "util.h"

/* FIXME: we don't want these hardcoded */
#define IP_STRING_SIZE		15 + 1

void ipc_gprs_pdp_context_complete(struct ipc_message_info *info)
{
	struct ipc_gen_phone_res *phone_res = (struct ipc_gen_phone_res *) info->data;
	int rc;

	rc = ipc_gen_phone_res_check(phone_res);
	if(rc < 0) {
		LOGE("There was an error, aborting PDP context complete");
		// TODO: Fill last fail reason!

		ril_state.tokens.gprs_context = (RIL_Token) 0x00;
		RIL_onRequestComplete(reqGetToken(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
		return;
	}
}

void ipc_gprs_define_pdp_context_complete(struct ipc_message_info *info)
{
	struct ipc_gen_phone_res *phone_res = (struct ipc_gen_phone_res *) info->data;
	int rc;
	int aseq;

	rc = ipc_gen_phone_res_check(phone_res);
	if(rc < 0) {
		LOGE("There was an error, aborting define PDP context complete");
		// TODO: Fill last fail reason!

		ril_state.tokens.gprs_context = (RIL_Token) 0x00;
		RIL_onRequestComplete(reqGetToken(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
		return;
	}

	/* We need to get a clean new aseq here */
	aseq = ril_request_reg_id(reqGetToken(info->aseq));

	ipc_gen_phone_res_expect_to_func(aseq, IPC_GPRS_PDP_CONTEXT,
		ipc_gprs_pdp_context_complete);

	/* activate the connection */
	ipc_fmt_send(IPC_GPRS_PDP_CONTEXT, IPC_TYPE_SET, 
			(void *) &(ril_state.gprs_context), sizeof(struct ipc_gprs_pdp_context), aseq);

	// TODO: if this aborts, last fail cause will be: PDP_FAIL_ERROR_UNSPECIFIED
}

void ipc_gprs_port_list_complete(struct ipc_message_info *info)
{
	struct ipc_gen_phone_res *phone_res = (struct ipc_gen_phone_res *) info->data;
	int rc;
	int aseq;

	rc = ipc_gen_phone_res_check(phone_res);
	if(rc < 0) {
		LOGE("There was an error, aborting port list complete");
		// TODO: Fill last fail reason!

		ril_state.tokens.gprs_context = (RIL_Token) 0x00;
		RIL_onRequestComplete(reqGetToken(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
		return;
	}

	/* We need to get a clean new aseq here */
	aseq = ril_request_reg_id(reqGetToken(info->aseq));

	ipc_gen_phone_res_expect_to_func(aseq, IPC_GPRS_DEFINE_PDP_CONTEXT,
		ipc_gprs_define_pdp_context_complete);

	/* send the struct to the modem */
	ipc_fmt_send(IPC_GPRS_DEFINE_PDP_CONTEXT, IPC_TYPE_SET, 
		(void *) &(ril_state.gprs_define_context), sizeof(struct ipc_gprs_define_pdp_context), aseq);
}

void ril_request_setup_data_call(RIL_Token t, void *data, int length)
{
	struct ipc_client_gprs_capabilities gprs_capabilities;
	struct ipc_gprs_port_list port_list;
	struct ipc_client *ipc_client;

	char *username = NULL;
	char *password = NULL;
	char *apn = NULL;

	ipc_client = ((struct ipc_client_object *) ipc_fmt_client->object)->ipc_client;

	/* get the apn, username and password */
	apn = ((char **) data)[2];
	username = ((char **) data)[3];

	if(username != NULL) {
		if(strlen(username) < 2)
			username = "dummy";
	} else {
		username = "dummy";
	}

	password = ((char **) data)[4];

	if(password != NULL) {
		if(strlen(password) < 2)
			password = "dummy";
	} else {
		password = "dummy";
	}

	LOGD("Requesting data connection to APN '%s'\n", apn);

	if(ril_state.tokens.gprs_context != (RIL_Token) 0x00) {
		LOGE("There is already a data call request going on!");
		// TODO: Fill last fail reason!

		ril_state.tokens.gprs_context = (RIL_Token) 0x00;
		RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
		return;
	}

	ril_state.tokens.gprs_context = t;

	/* create the structs with the apn */
	ipc_gprs_define_pdp_context_setup(&(ril_state.gprs_define_context), apn);

	/* create the structs with the username/password tuple */
	ipc_gprs_pdp_context_setup(&(ril_state.gprs_context), 1, username, password);

	ipc_client_gprs_get_capabilities(ipc_client, &gprs_capabilities);

	// If handlers are available, deal with port list
	if(gprs_capabilities.port_list) {
		ipc_gprs_port_list_setup(&port_list);

		ipc_gen_phone_res_expect_to_func(reqGetId(t), IPC_GPRS_PORT_LIST,
			ipc_gprs_port_list_complete);

		ipc_fmt_send(IPC_GPRS_PORT_LIST, IPC_TYPE_SET, 
			(void *) &port_list, sizeof(struct ipc_gprs_port_list), reqGetId(t));
	} else {
		ipc_gen_phone_res_expect_to_func(reqGetId(t), IPC_GPRS_DEFINE_PDP_CONTEXT,
			ipc_gprs_define_pdp_context_complete);

		/* send the struct to the modem */
		ipc_fmt_send(IPC_GPRS_DEFINE_PDP_CONTEXT, IPC_TYPE_SET, 
			(void *) &(ril_state.gprs_define_context), sizeof(struct ipc_gprs_define_pdp_context), reqGetId(t));
	}
}

void ipc_gprs_ip_configuration(struct ipc_message_info *info)
{
	/* Quick and dirty configuration, TODO: Handle that better */

        struct ipc_gprs_ip_configuration *ip_config = (struct ipc_gprs_ip_configuration *) info->data;
	struct ipc_client *ipc_client;

	char local_ip[IP_STRING_SIZE];
	char gateway[IP_STRING_SIZE];
	char subnet_mask[IP_STRING_SIZE];
	char dns1[IP_STRING_SIZE];
	char dns2[IP_STRING_SIZE];

	char dns_prop_name[PROPERTY_KEY_MAX];
	char gw_prop_name[PROPERTY_KEY_MAX];

	char *interface;

	RIL_Data_Call_Response_v6 data_call_response;
	char *response[3];
	int rc;

	ipc_client = ((struct ipc_client_object *) ipc_fmt_client->object)->ipc_client;

	/* TODO: transform that into some macros */
	snprintf(local_ip, IP_STRING_SIZE, "%i.%i.%i.%i",(ip_config->ip)[0],(ip_config->ip)[1],
						(ip_config->ip)[2],(ip_config->ip)[3]);

        snprintf(gateway, IP_STRING_SIZE, "%i.%i.%i.%i",(ip_config->ip)[0],(ip_config->ip)[1],
                                                (ip_config->ip)[2],(ip_config->ip)[3]);

        snprintf(subnet_mask, IP_STRING_SIZE, "255.255.255.255");

        snprintf(dns1, IP_STRING_SIZE, "%i.%i.%i.%i",(ip_config->dns1)[0],(ip_config->dns1)[1],
                                                (ip_config->dns1)[2],(ip_config->dns1)[3]);

        snprintf(dns2, IP_STRING_SIZE , "%i.%i.%i.%i",(ip_config->dns2)[0],(ip_config->dns2)[1],
                                                (ip_config->dns2)[2],(ip_config->dns2)[3]);

        LOGD("GPRS configuration: ip:%s, gateway:%s, subnet_mask:%s, dns1:%s, dns2:%s",
							local_ip, gateway, subnet_mask ,dns1, dns2);

	if(ipc_client_gprs_handlers_available(ipc_client)) {
		rc = ipc_client_gprs_activate(ipc_client);
		if(rc < 0) {
			// This is not a critical issue
			LOGE("Failed to activate interface!");
		}
	}

	rc = ipc_client_gprs_get_iface(ipc_client, &interface);
	if(rc < 0) {
		// This is not a critical issue, fallback to rmnet0
		LOGE("Failed to get interface name!");
		asprintf(&interface, "rmnet0");
	}

	LOGD("Iface name is %s\n", interface);

	rc = ifc_configure(interface, 
			inet_addr(local_ip),
			ipv4NetmaskToPrefixLength(inet_addr(subnet_mask)),
			inet_addr(gateway),
			inet_addr(dns1),
			inet_addr(dns2));
        LOGD("ifc_configure: %d",rc);
/*
	snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface);
	property_set(dns_prop_name, dns1);
	snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface);
	property_set(dns_prop_name, dns2);
	snprintf(gw_prop_name, sizeof(gw_prop_name), "net.%s.gw", interface);
	property_set(dns_prop_name, gateway);
*/

	data_call_response.status = 0;
	data_call_response.cid = 0;
	data_call_response.active = 1;
	data_call_response.type = "IP";
	data_call_response.ifname = interface;
	data_call_response.addresses = local_ip;
	data_call_response.dnses = dns1;
	data_call_response.gateways = gateway;

	RIL_onRequestComplete(ril_state.tokens.gprs_context, RIL_E_SUCCESS, &data_call_response, sizeof(data_call_response));

	ril_state.tokens.gprs_context = (RIL_Token) 0x00;

	// FIXME: is it wise to free returned data?
	free(interface);
}

void ril_request_deactivate_data_call(RIL_Token t, void *data, int length)
{
	struct ipc_gprs_pdp_context deactivate_message;
	struct ipc_client *ipc_client;
	int rc;

	ipc_client = ((struct ipc_client_object *) ipc_fmt_client->object)->ipc_client;

	memset(&deactivate_message, 0, sizeof(deactivate_message));
	deactivate_message.unk0[1]=1;

	/* send the struct to the modem */
	ipc_fmt_send(IPC_GPRS_PDP_CONTEXT, IPC_TYPE_SET, 
			(void *) &deactivate_message, sizeof(struct ipc_gprs_pdp_context), reqGetId(t));

	if(ipc_client_gprs_handlers_available(ipc_client)) {
		rc = ipc_client_gprs_deactivate(ipc_client);
		if(rc < 0) {
			// This is not a critical issue
			LOGE("Failed to deactivate interface!");
		}
	}

	// Clean the token
	ril_state.tokens.gprs_context = (RIL_Token) 0x00;

	RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
}