summaryrefslogtreecommitdiffstats
path: root/qmi-client.c
blob: ab473252992a3da5dc996ff147a86ef1882b9bf9 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * This file is part of QMI-RIL.
 *
 * Copyright (C) 2017 Wolfgang Wiedmeyer <wolfgit@wiedmeyer.de>
 *
 * QMI-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.
 *
 * QMI-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 QMI-RIL.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>

#define LOG_TAG "RIL"
#include <utils/Log.h>
#include <telephony/ril.h>

#include <gio/gio.h>

#include <qmi-ril.h>

#define CDC_PATH "/dev/cdc-wdm2"

static GCancellable *cancellable;
static GFile *file;
static QmiDevice *device;

QmiService qmi_services[] = {
	QMI_SERVICE_DMS,
	QMI_SERVICE_NAS,
	QMI_SERVICE_UIM,
	QMI_SERVICE_WDS,
	QMI_SERVICE_WDA,
};

unsigned int qmi_service_count = sizeof(qmi_services)
	/ sizeof(QmiService);
unsigned int qmi_indications_count = 3;

static unsigned int qmi_client_count = 0;
static unsigned int qmi_client_failures = 0;
static unsigned int qmi_indications_ready = 0;

static void all_indications_ready()
{
	if (qmi_indications_ready < qmi_indications_count) {
		RIL_LOGD("%d indications ready", qmi_indications_ready);
		return;
	} else {
		RIL_LOGD("registered indications");
		// initialization finished, main loop can now process
		// requests
		ril_radio_state_update(RADIO_STATE_OFF);
	}
}

static void nas_set_event_report_ready(QmiClientNas *client,
				       GAsyncResult *res)
{
	QmiMessageNasSetEventReportOutput *output;
	GError *error = NULL;

	output = qmi_client_nas_set_event_report_finish(client, res, &error);
	if (!output) {
		RIL_LOGE("%s: error: operation failed: %s\n", __func__,
			 error->message);
		g_error_free(error);
		return;
	}

	if (!qmi_message_nas_set_event_report_output_get_result(output, &error)) {
		RIL_LOGE("Couldn't set nas event report: %s", error->message);
		g_error_free(error);
		qmi_message_nas_set_event_report_output_unref(output);

		return;
	}

	qmi_message_nas_set_event_report_output_unref(output);

	ctx->nas_event_report_indication_id =
		g_signal_connect(ctx->NasClient, "event-report",
				 G_CALLBACK(nas_event_report_indication_cb),
				 NULL);

	qmi_indications_ready++;
	all_indications_ready();
}

static void nas_register_indications_ready(QmiClientNas *client,
					   GAsyncResult *res)
{
	QmiMessageNasRegisterIndicationsOutput *output;
	GError *error = NULL;

	output = qmi_client_nas_register_indications_finish(client, res, &error);
	if (!output) {
		RIL_LOGE("%s: error: operation failed: %s", __func__,
			 error->message);
		g_error_free(error);
		return;
	}

	if (!qmi_message_nas_register_indications_output_get_result(output, &error)) {
		RIL_LOGE("Couldn't register nas indications: %s", error->message);
		g_error_free(error);
		qmi_message_nas_register_indications_output_unref(output);

		return;
	}

	qmi_message_nas_register_indications_output_unref(output);

	ctx->serving_system_indication_id =
		g_signal_connect(ctx->NasClient, "serving-system",
				 G_CALLBACK(serving_system_indication_cb),
				 NULL);

	qmi_indications_ready++;
	all_indications_ready();
}

static void dms_set_event_report_ready(QmiClientDms *client,
				       GAsyncResult *res)
{
	QmiMessageDmsSetEventReportOutput *output;
	GError *error = NULL;

	output = qmi_client_dms_set_event_report_finish(client, res, &error);

	if (!output) {
		RIL_LOGE("%s: error: operation failed: %s", __func__,
			 error->message);
		g_error_free(error);
		return;
	}

	if (!qmi_message_dms_set_event_report_output_get_result(output, &error)) {
		RIL_LOGE("Couldn't set dms event report: %s", error->message);
		g_error_free(error);
		qmi_message_dms_set_event_report_output_unref(output);
		return;
	}

	qmi_message_dms_set_event_report_output_unref(output);

	ctx->dms_event_report_indication_id = g_signal_connect(
		ctx->DmsClient,
		"event-report",
		G_CALLBACK(dms_event_report_indication_cb),
		NULL);

	qmi_indications_ready++;
	all_indications_ready();
}

static void setup_indications()
{
	QmiMessageDmsSetEventReportInput *dms_event_report_input;
	QmiMessageNasRegisterIndicationsInput *nas_register_input;
	QmiMessageNasSetEventReportInput *nas_event_report_input;
	static const gint8 thresholds_data[] = {-80, -40, 0, 40, 80};
	GArray *thresholds;

	dms_event_report_input = qmi_message_dms_set_event_report_input_new();
	qmi_message_dms_set_event_report_input_set_operating_mode_reporting(
		dms_event_report_input, TRUE, NULL);
	qmi_client_dms_set_event_report(ctx->DmsClient,
					dms_event_report_input, 5, NULL,
					(GAsyncReadyCallback)dms_set_event_report_ready,
					NULL);
	qmi_message_dms_set_event_report_input_unref(dms_event_report_input);

	nas_register_input = qmi_message_nas_register_indications_input_new();
	qmi_message_nas_register_indications_input_set_serving_system_events(
		nas_register_input, TRUE, NULL);
	qmi_client_nas_register_indications(ctx->NasClient,
					    nas_register_input, 5, NULL,
					    (GAsyncReadyCallback)nas_register_indications_ready,
					    NULL);
	qmi_message_nas_register_indications_input_unref(nas_register_input);

	nas_event_report_input = qmi_message_nas_set_event_report_input_new();
	thresholds = g_array_sized_new(FALSE, FALSE, sizeof (gint8),
				       G_N_ELEMENTS(thresholds_data));
	g_array_append_vals(thresholds, thresholds_data,
			    G_N_ELEMENTS(thresholds_data));
	qmi_message_nas_set_event_report_input_set_signal_strength_indicator(
		nas_event_report_input, TRUE, thresholds, NULL);
	g_array_unref(thresholds);
	qmi_client_nas_set_event_report(ctx->NasClient,
					nas_event_report_input, 5, NULL,
					(GAsyncReadyCallback)nas_set_event_report_ready,
					NULL);
	qmi_message_nas_set_event_report_input_unref(nas_event_report_input);
}

static void allocate_client_ready(QmiDevice *dev, GAsyncResult *res)
{
	QmiClient *client;
	QmiService service;
	GError *error = NULL;

	client = qmi_device_allocate_client_finish(dev, res, &error);
	if (!client) {
		RIL_LOGE("%s: error: couldn't create client: %s", __func__,
			 error->message);
		qmi_client_failures++;
		RIL_LOGE("increased failure counter to %d", qmi_client_failures);
		return;
	}

	service = qmi_client_get_service(client);

	switch (service) {
	case QMI_SERVICE_DMS:
		ctx->DmsClient = g_object_ref(QMI_CLIENT_DMS(client));
		break;
	case QMI_SERVICE_NAS:
		ctx->NasClient = g_object_ref(QMI_CLIENT_NAS(client));
		break;
	case QMI_SERVICE_UIM:
		ctx->UimClient = g_object_ref(QMI_CLIENT_UIM(client));
		break;
	case QMI_SERVICE_WDS:
		ctx->WdsClient = g_object_ref(QMI_CLIENT_WDS(client));
		break;
	case QMI_SERVICE_WDA:
		ctx->WdaClient = g_object_ref(QMI_CLIENT_WDA(client));
		// configure it right away
		qmi_set_raw_ip_mode();
		break;
	default:
		RIL_LOGE("unknown service");
		return;
	}

	RIL_LOGD("QMI client for service %s allocated",
		 qmi_service_get_string(service));
	RIL_LOGD("%d of %d clients allocated", qmi_client_count,
		 qmi_service_count);

	qmi_client_count++;

	if (qmi_client_failures)
		RIL_LOGD("%d failures", qmi_client_failures);

	if (qmi_client_count == qmi_service_count)
		setup_indications();
}

static void device_allocate_client(QmiDevice *dev)
{
	guint8 cid = QMI_CID_NONE;
	unsigned int i;

	ctx = g_slice_new(Context);
	ctx->file = g_object_ref(file);
	ctx->device = g_object_ref(device);
	if (cancellable)
		ctx->cancellable = g_object_ref(cancellable);

	for (i = 0; i < qmi_service_count; i++)
		qmi_device_allocate_client(dev, qmi_services[i], cid,
					   10, cancellable,
					   (GAsyncReadyCallback)allocate_client_ready,
					   NULL);

	RIL_LOGD("Started allocating QMI clients");
}

static void device_open_ready(QmiDevice *dev, GAsyncResult *res)
{
	GError *error = NULL;

	if (!qmi_device_open_finish(dev, res, &error)) {
		RIL_LOGE("error: couldn't open the QmiDevice: %s",
			 error->message);
		qmi_client_failures++;
		return;
	}

	RIL_LOGD("QMI Device at '%s' ready",
		 qmi_device_get_path_display(dev));

	device_allocate_client(dev);
}

static void device_new_ready(GObject *unused, GAsyncResult *res)
{
	QmiDeviceOpenFlags open_flags = QMI_DEVICE_OPEN_FLAGS_NONE;
	GError *error = NULL;

	device = qmi_device_new_finish(res, &error);
	if (!device) {
		RIL_LOGE("error: couldn't create QmiDevice: %s",
			 error->message);
		qmi_client_failures++;
		return;
	}

	open_flags |= QMI_DEVICE_OPEN_FLAGS_AUTO;

	/* Open the device */
	qmi_device_open(device, open_flags, 15, cancellable,
			(GAsyncReadyCallback)device_open_ready, NULL);
}

int create_qmi_clients(void)
{
	cancellable = g_cancellable_new();

	file = g_file_new_for_path(CDC_PATH);

	if(g_file_query_exists(file, NULL))
		qmi_device_new(file, cancellable,
			       (GAsyncReadyCallback)device_new_ready,
			       NULL);
	else {
		RIL_LOGE("error opening file path");
		return -1;
	}

	RIL_LOGD("started creating QMI clients");

	return 0;
}

int all_clients_running(void)
{
	if (qmi_client_failures) {
		return -1;
	} else if (qmi_client_count < qmi_service_count
		   || qmi_indications_ready < qmi_indications_count
		   || ril_data->data_connection.raw_ip_mode == FALSE)
		return 0;
	else
		return 1;
}