File: call-forwarding.c

package info (click to toggle)
ofono 2.18-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,064 kB
  • sloc: ansic: 224,979; sh: 5,012; python: 4,040; makefile: 956
file content (262 lines) | stat: -rw-r--r-- 6,042 bytes parent folder | download | duplicates (2)
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
/*
 * oFono - Open Source Telephony
 * Copyright (C) 2008-2011  Intel Corporation
 *
 * SPDX-License-Identifier: GPL-2.0-only
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include <glib.h>

#include <ofono/log.h>
#include <ofono/modem.h>
#include <ofono/call-forwarding.h>

#include <drivers/atmodem/atutil.h>

#include "gatchat.h"
#include "gatresult.h"

static const char *none_prefix[] = { NULL };
static const char *ccfc_prefix[] = { "+CCFC:", NULL };

static void ccfc_query_cb(gboolean ok, GAtResult *result, gpointer user_data)
{
	struct cb_data *cbd = user_data;
	ofono_call_forwarding_query_cb_t cb = cbd->cb;
	struct ofono_error error;
	GAtResultIter iter;
	int num = 0;
	struct ofono_call_forwarding_condition *list = NULL;
	int i;
	int maxlen;

	decode_at_error(&error, g_at_result_final_response(result));

	if (!ok)
		goto out;

	g_at_result_iter_init(&iter, result);

	while (g_at_result_iter_next(&iter, "+CCFC:"))
		num += 1;

	/* Specification is really unclear about this
	 * generate status=0 for all classes just in case
	 */
	if (num == 0) {
		list = g_new0(struct ofono_call_forwarding_condition, 1);
		num = 1;

		list->status = 0;
		list->cls = GPOINTER_TO_INT(cbd->user);

		goto out;
	}

	list = g_new(struct ofono_call_forwarding_condition, num);

	g_at_result_iter_init(&iter, result);

	maxlen = OFONO_MAX_PHONE_NUMBER_LENGTH;

	for (num = 0; g_at_result_iter_next(&iter, "+CCFC:"); num++) {
		const char *str;

		g_at_result_iter_next_number(&iter, &(list[num].status));
		g_at_result_iter_next_number(&iter, &(list[num].cls));

		list[num].phone_number.number[0] = '\0';
		list[num].phone_number.type = 129;
		list[num].time = 20;

		if (!g_at_result_iter_next_string(&iter, &str))
			continue;

		strncpy(list[num].phone_number.number, str, maxlen);
		list[num].phone_number.number[maxlen] = '\0';

		g_at_result_iter_next_number(&iter,
						&(list[num].phone_number.type));

		if (!g_at_result_iter_skip_next(&iter))
			continue;

		if (!g_at_result_iter_skip_next(&iter))
			continue;

		g_at_result_iter_next_number(&iter, &(list[num].time));
	}

	for (i = 0; i < num; i++)
		DBG("ccfc_cb: %d, %d, %s(%d) - %d sec",
			list[i].status, list[i].cls,
			list[i].phone_number.number,
			list[i].phone_number.type, list[i].time);

out:
	cb(&error, num, list, cbd->data);
	g_free(list);
}

static void at_ccfc_query(struct ofono_call_forwarding *cf, int type, int cls,
				ofono_call_forwarding_query_cb_t cb, void *data)
{
	GAtChat *chat = ofono_call_forwarding_get_data(cf);
	struct cb_data *cbd = cb_data_new(cb, data);
	char buf[64];

	cbd->user = GINT_TO_POINTER(cls);

	if (cls == 7)
		snprintf(buf, sizeof(buf), "AT+CCFC=%d,2", type);
	else
		snprintf(buf, sizeof(buf), "AT+CCFC=%d,2,,,%d", type, cls);

	if (g_at_chat_send(chat, buf, ccfc_prefix,
				ccfc_query_cb, cbd, g_free) > 0)
		return;

	g_free(cbd);

	CALLBACK_WITH_FAILURE(cb, 0, NULL, data);
}

static void ccfc_set_cb(gboolean ok, GAtResult *result, gpointer user_data)
{
	struct cb_data *cbd = user_data;
	ofono_call_forwarding_set_cb_t cb = cbd->cb;
	struct ofono_error error;

	decode_at_error(&error, g_at_result_final_response(result));

	cb(&error, cbd->data);
}

static void at_ccfc_set(struct ofono_call_forwarding *cf, const char *buf,
				ofono_call_forwarding_set_cb_t cb, void *data)
{
	GAtChat *chat = ofono_call_forwarding_get_data(cf);
	struct cb_data *cbd = cb_data_new(cb, data);

	if (g_at_chat_send(chat, buf, none_prefix,
				ccfc_set_cb, cbd, g_free) > 0)
		return;

	g_free(cbd);

	CALLBACK_WITH_FAILURE(cb, data);
}

static void at_ccfc_erasure(struct ofono_call_forwarding *cf,
				int type, int cls,
				ofono_call_forwarding_set_cb_t cb, void *data)
{
	char buf[128];
	int len;

	len = snprintf(buf, sizeof(buf), "AT+CCFC=%d,4", type);

	if (cls != 7)
		snprintf(buf + len, sizeof(buf) - len, ",,,%d", cls);

	at_ccfc_set(cf, buf, cb, data);
}

static void at_ccfc_deactivation(struct ofono_call_forwarding *cf,
					int type, int cls,
					ofono_call_forwarding_set_cb_t cb,
					void *data)
{
	char buf[128];
	int len;

	len = snprintf(buf, sizeof(buf), "AT+CCFC=%d,0", type);

	if (cls != 7)
		snprintf(buf + len, sizeof(buf) - len, ",,,%d", cls);

	at_ccfc_set(cf, buf, cb, data);
}

static void at_ccfc_activation(struct ofono_call_forwarding *cf,
				int type, int cls,
				ofono_call_forwarding_set_cb_t cb, void *data)
{
	char buf[128];
	int len;

	len = snprintf(buf, sizeof(buf), "AT+CCFC=%d,1", type);

	if (cls != 7)
		snprintf(buf + len, sizeof(buf) - len, ",,,%d", cls);

	at_ccfc_set(cf, buf, cb, data);
}

static void at_ccfc_registration(struct ofono_call_forwarding *cf,
					int type, int cls,
					const struct ofono_phone_number *ph,
					int time,
					ofono_call_forwarding_set_cb_t cb,
					void *data)
{
	char buf[128];
	int offset;

	offset = snprintf(buf, sizeof(buf), "AT+CCFC=%d,3,\"%s\",%d,%d", type,
				ph->number, ph->type, cls);

	if (type == 2 || type == 4 || type == 5)
		snprintf(buf+offset, sizeof(buf) - offset, ",,,%d", time);

	at_ccfc_set(cf, buf, cb, data);
}

static gboolean at_ccfc_register(gpointer user)
{
	struct ofono_call_forwarding *cf = user;

	ofono_call_forwarding_register(cf);

	return FALSE;
}

static int at_ccfc_probe(struct ofono_call_forwarding *cf, unsigned int vendor,
				void *data)
{
	GAtChat *chat = data;

	ofono_call_forwarding_set_data(cf, g_at_chat_clone(chat));
	g_idle_add(at_ccfc_register, cf);

	return 0;
}

static void at_ccfc_remove(struct ofono_call_forwarding *cf)
{
	GAtChat *chat = ofono_call_forwarding_get_data(cf);

	g_idle_remove_by_data(cf);
	g_at_chat_unref(chat);
	ofono_call_forwarding_set_data(cf, NULL);
}

static const struct ofono_call_forwarding_driver driver = {
	.probe		= at_ccfc_probe,
	.remove		= at_ccfc_remove,
	.registration	= at_ccfc_registration,
	.activation	= at_ccfc_activation,
	.query		= at_ccfc_query,
	.deactivation	= at_ccfc_deactivation,
	.erasure	= at_ccfc_erasure
};

OFONO_ATOM_DRIVER_BUILTIN(call_forwarding, atmodem, &driver)