summaryrefslogtreecommitdiff
path: root/drivers/huaweimodem/radio-settings.c
blob: ff7be48a8dc19aff71c66954a99918726b2d44fc (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2011  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

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

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

#include <glib.h>

#include <ofono/log.h>
#include <ofono/modem.h>
#include <ofono/radio-settings.h>

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

#include "huaweimodem.h"

static const char *none_prefix[] = { NULL };
static const char *syscfg_prefix[] = { "^SYSCFG:", NULL };
static const char *syscfgex_prefix[] = { "^SYSCFGEX:", NULL };

#define HUAWEI_BAND_ANY 0x3FFFFFFF

struct radio_settings_data {
	GAtChat *chat;
	ofono_bool_t syscfgex_cap;
};

static const struct huawei_band_gsm_table {
	enum ofono_radio_band_gsm band_gsm;
	unsigned int band_huawei;
} huawei_band_gsm_table[] = {
	{ OFONO_RADIO_BAND_GSM_ANY, 0x80000 | 0x200 | 0x100 | 0x80 | 0x200000 },
	{ OFONO_RADIO_BAND_GSM_850, 0x80000 },
	{ OFONO_RADIO_BAND_GSM_900P, 0x200 },
	{ OFONO_RADIO_BAND_GSM_900E, 0x100 },
	{ OFONO_RADIO_BAND_GSM_1800, 0x80 },
	{ OFONO_RADIO_BAND_GSM_1900, 0x200000 },
};

static const struct huawei_band_umts_table {
	enum ofono_radio_band_umts band_umts;
	unsigned int band_huawei;
} huawei_band_umts_table[] = {
	{ OFONO_RADIO_BAND_UMTS_ANY, 0x4000000 | 0x20000 | 800000 | 400000 },
	{ OFONO_RADIO_BAND_UMTS_850, 0x4000000 },
	{ OFONO_RADIO_BAND_UMTS_900, 0x20000 },
	{ OFONO_RADIO_BAND_UMTS_1900, 0x800000 },
	{ OFONO_RADIO_BAND_UMTS_2100, 0x400000 },
};

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

static unsigned int band_gsm_to_huawei(enum ofono_radio_band_gsm band)
{
	size_t i;

	for (i = 0; i < ARRAY_SIZE(huawei_band_gsm_table); i++) {
		if (huawei_band_gsm_table[i].band_gsm == band)
			return huawei_band_gsm_table[i].band_huawei;
	}

	return 0;
}

static unsigned int band_umts_to_huawei(enum ofono_radio_band_umts band)
{
	size_t i;

	for (i = 0; i < ARRAY_SIZE(huawei_band_umts_table); i++) {
		if (huawei_band_umts_table[i].band_umts == band)
			return huawei_band_umts_table[i].band_huawei;
	}

	return 0;
}

static enum ofono_radio_band_gsm band_gsm_from_huawei(unsigned int band)
{
	size_t i;

	if (band == HUAWEI_BAND_ANY)
		return OFONO_RADIO_BAND_UMTS_ANY;

	for (i = ARRAY_SIZE(huawei_band_gsm_table) - 1; i > 0; i--) {
		if (huawei_band_gsm_table[i].band_huawei & band)
			return huawei_band_gsm_table[i].band_gsm;
	}

	return OFONO_RADIO_BAND_GSM_ANY;
}

static enum ofono_radio_band_umts band_umts_from_huawei(unsigned int band)
{
	size_t i;

	if (band == HUAWEI_BAND_ANY)
		return OFONO_RADIO_BAND_UMTS_ANY;

	for (i = ARRAY_SIZE(huawei_band_umts_table) - 1; i > 0; i--) {
		if (huawei_band_umts_table[i].band_huawei & band)
			return huawei_band_umts_table[i].band_umts;
	}

	return OFONO_RADIO_BAND_UMTS_ANY;
}

static void syscfg_query_mode_cb(gboolean ok, GAtResult *result,
					gpointer user_data)
{
	struct cb_data *cbd = user_data;
	ofono_radio_settings_rat_mode_query_cb_t cb = cbd->cb;
	enum ofono_radio_access_mode mode;
	struct ofono_error error;
	GAtResultIter iter;
	int value;

	decode_at_error(&error, g_at_result_final_response(result));

	if (!ok) {
		cb(&error, -1, cbd->data);
		return;
	}

	g_at_result_iter_init(&iter, result);

	if (g_at_result_iter_next(&iter, "^SYSCFG:") == FALSE)
		goto error;

	if (g_at_result_iter_next_number(&iter, &value) == FALSE)
		goto error;

	switch (value) {
	case 2:
		mode = OFONO_RADIO_ACCESS_MODE_ANY;
		break;
	case 13:
		mode = OFONO_RADIO_ACCESS_MODE_GSM;
		break;
	case 14:
		mode = OFONO_RADIO_ACCESS_MODE_UMTS;
		break;
	default:
		CALLBACK_WITH_FAILURE(cb, -1, cbd->data);
		return;
	}

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

	return;

error:
	CALLBACK_WITH_FAILURE(cb, -1, cbd->data);
}

static void syscfgex_query_mode_cb(gboolean ok, GAtResult *result,
					gpointer user_data)
{
	struct cb_data *cbd = user_data;
	ofono_radio_settings_rat_mode_query_cb_t cb = cbd->cb;
	enum ofono_radio_access_mode mode;
	struct ofono_error error;
	GAtResultIter iter;
	const char *acqorder;

	decode_at_error(&error, g_at_result_final_response(result));

	if (!ok) {
		cb(&error, -1, cbd->data);
		return;
	}

	g_at_result_iter_init(&iter, result);

	if (g_at_result_iter_next(&iter, "^SYSCFGEX:") == FALSE)
		goto error;

	if (g_at_result_iter_next_string(&iter, &acqorder) == FALSE)
		goto error;

	if ((strcmp(acqorder, "00") == 0) ||
			(strstr(acqorder, "01") &&
				strstr(acqorder, "02") &&
				strstr(acqorder, "03")))
		mode = OFONO_RADIO_ACCESS_MODE_ANY;
	else if (strstr(acqorder, "0302"))
		mode = (OFONO_RADIO_ACCESS_MODE_LTE | OFONO_RADIO_ACCESS_MODE_UMTS);
	else if (strstr(acqorder, "0201"))
		mode = (OFONO_RADIO_ACCESS_MODE_UMTS | OFONO_RADIO_ACCESS_MODE_LTE);
	else if (strstr(acqorder, "03"))
		mode = OFONO_RADIO_ACCESS_MODE_LTE;
	else if (strstr(acqorder, "02"))
		mode = OFONO_RADIO_ACCESS_MODE_UMTS;
	else if (strstr(acqorder, "01"))
		mode = OFONO_RADIO_ACCESS_MODE_GSM;
	else
		goto error;

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

	return;

error:
	CALLBACK_WITH_FAILURE(cb, -1, cbd->data);
}

static void huawei_query_rat_mode(struct ofono_radio_settings *rs,
			ofono_radio_settings_rat_mode_query_cb_t cb, void *data)
{
	struct radio_settings_data *rsd = ofono_radio_settings_get_data(rs);
	struct cb_data *cbd = cb_data_new(cb, data);

	if (rsd->syscfgex_cap && g_at_chat_send(rsd->chat, "AT^SYSCFGEX?",
					syscfgex_prefix,
					syscfgex_query_mode_cb,
					cbd, g_free) > 0)
		return;

	if (!rsd->syscfgex_cap && g_at_chat_send(rsd->chat, "AT^SYSCFG?",
					syscfg_prefix,
					syscfg_query_mode_cb,
					cbd, g_free) > 0)
		return;

	CALLBACK_WITH_FAILURE(cb, -1, data);
	g_free(cbd);
}

static void syscfgxx_modify_mode_cb(gboolean ok, GAtResult *result,
							gpointer user_data)
{
	struct cb_data *cbd = user_data;
	ofono_radio_settings_rat_mode_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 syscfg_set_rat_mode(struct radio_settings_data *rsd,
				enum ofono_radio_access_mode mode,
				ofono_radio_settings_rat_mode_set_cb_t cb,
				void *data)
{
	struct cb_data *cbd = cb_data_new(cb, data);
	char buf[40];
	unsigned int value = 2, acq_order = 0;

	switch (mode) {
	case OFONO_RADIO_ACCESS_MODE_ANY:
		value = 2;
		acq_order = 0;
		break;
	case OFONO_RADIO_ACCESS_MODE_GSM:
		value = 13;
		acq_order = 1;
		break;
	case OFONO_RADIO_ACCESS_MODE_UMTS:
		value = 14;
		acq_order = 2;
		break;
	case OFONO_RADIO_ACCESS_MODE_LTE:
		goto error;
	default:
		goto error;
	}

	snprintf(buf, sizeof(buf), "AT^SYSCFG=%u,%u,40000000,2,4",
							value, acq_order);

	if (g_at_chat_send(rsd->chat, buf, none_prefix,
				syscfgxx_modify_mode_cb, cbd, g_free) > 0)
		return;

error:
	CALLBACK_WITH_FAILURE(cb, data);
	g_free(cbd);
}

static void syscfgex_set_rat_mode(struct radio_settings_data *rsd,
				enum ofono_radio_access_mode mode,
				ofono_radio_settings_rat_mode_set_cb_t cb,
				void *data)
{
	struct cb_data *cbd = cb_data_new(cb, data);
	char buf[50];
	char *atcmd = "AT^SYSCFGEX=\"%s\",40000000,2,4,40000000,,";
	char *acqorder = "030201";

	switch (mode) {
	case OFONO_RADIO_ACCESS_MODE_ANY:
		acqorder = "00";
		break;
	case OFONO_RADIO_ACCESS_MODE_GSM:
		acqorder = "01";
		break;
	case OFONO_RADIO_ACCESS_MODE_UMTS:
		acqorder = "02";
		break;
	case OFONO_RADIO_ACCESS_MODE_LTE:
		acqorder = "03";
		break;
	default:
		break;
	}

	if (mode == (OFONO_RADIO_ACCESS_MODE_LTE | OFONO_RADIO_ACCESS_MODE_UMTS))
		acqorder = "0302";
	else if (mode == (OFONO_RADIO_ACCESS_MODE_UMTS | OFONO_RADIO_ACCESS_MODE_LTE))
		acqorder = "0201";

	snprintf(buf, sizeof(buf), atcmd, acqorder);

	if (g_at_chat_send(rsd->chat, buf, none_prefix,
			syscfgxx_modify_mode_cb, cbd, g_free) > 0)
		return;

	CALLBACK_WITH_FAILURE(cb, data);
	g_free(cbd);
}

static void huawei_set_rat_mode(struct ofono_radio_settings *rs,
				enum ofono_radio_access_mode mode,
				ofono_radio_settings_rat_mode_set_cb_t cb,
				void *data)
{
	struct radio_settings_data *rsd = ofono_radio_settings_get_data(rs);

	if (rsd->syscfgex_cap)
		syscfgex_set_rat_mode(rsd, mode, cb, data);
	else
		syscfg_set_rat_mode(rsd, mode, cb, data);
}

static void syscfgxx_modify_band_cb(gboolean ok, GAtResult *result,
							gpointer user_data)
{
	struct cb_data *cbd = user_data;
	ofono_radio_settings_band_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 syscfgex_set_band(struct radio_settings_data *rsd,
					enum ofono_radio_band_gsm band_gsm,
					enum ofono_radio_band_umts band_umts,
					ofono_radio_settings_band_set_cb_t cb,
					void *data)
{
	struct cb_data *cbd = cb_data_new(cb, data);
	char buf[50];
	char *atcmd = "AT^SYSCFGEX=\"99\",%x,2,4,40000000,,";
	unsigned int huawei_band;

	if (band_gsm == OFONO_RADIO_BAND_GSM_ANY
			&& band_umts == OFONO_RADIO_BAND_UMTS_ANY) {
		huawei_band = HUAWEI_BAND_ANY;
	} else {
		unsigned int huawei_band_gsm;
		unsigned int huawei_band_umts;

		huawei_band_gsm = band_gsm_to_huawei(band_gsm);

		if (!huawei_band_gsm)
			goto error;

		huawei_band_umts = band_umts_to_huawei(band_umts);

		if (!huawei_band_umts)
			goto error;

		huawei_band = huawei_band_gsm | huawei_band_umts;
	}

	snprintf(buf, sizeof(buf), atcmd, huawei_band);

	if (g_at_chat_send(rsd->chat, buf, none_prefix,
			syscfgxx_modify_band_cb, cbd, g_free) > 0)
		return;

error:
	CALLBACK_WITH_FAILURE(cb, data);
	g_free(cbd);
}

static void syscfg_set_band(struct radio_settings_data *rsd,
					enum ofono_radio_band_gsm band_gsm,
					enum ofono_radio_band_umts band_umts,
					ofono_radio_settings_band_set_cb_t cb,
					void *data)
{
	struct cb_data *cbd = cb_data_new(cb, data);
	char buf[40];
	unsigned int huawei_band;

	if (band_gsm == OFONO_RADIO_BAND_GSM_ANY
			&& band_umts == OFONO_RADIO_BAND_UMTS_ANY) {
		huawei_band = HUAWEI_BAND_ANY;
	} else {
		unsigned int huawei_band_gsm;
		unsigned int huawei_band_umts;

		huawei_band_gsm = band_gsm_to_huawei(band_gsm);

		if (!huawei_band_gsm)
			goto error;

		huawei_band_umts = band_umts_to_huawei(band_umts);

		if (!huawei_band_umts)
			goto error;

		huawei_band = huawei_band_gsm | huawei_band_umts;
	}

	snprintf(buf, sizeof(buf), "AT^SYSCFG=16,3,%x,2,4", huawei_band);

	if (g_at_chat_send(rsd->chat, buf, none_prefix,
				syscfgxx_modify_band_cb, cbd, g_free) > 0)
		return;

error:
	CALLBACK_WITH_FAILURE(cb, data);
	g_free(cbd);
}

static void huawei_set_band(struct ofono_radio_settings *rs,
					enum ofono_radio_band_gsm band_gsm,
					enum ofono_radio_band_umts band_umts,
					ofono_radio_settings_band_set_cb_t cb,
					void *data)
{
	struct radio_settings_data *rsd = ofono_radio_settings_get_data(rs);

	if (rsd->syscfgex_cap)
		syscfgex_set_band(rsd, band_gsm, band_umts, cb, data);
	else
		syscfg_set_band(rsd, band_gsm, band_umts, cb, data);
}

static void syscfg_query_band_cb(gboolean ok, GAtResult *result,
					gpointer user_data)
{
	struct cb_data *cbd = user_data;
	ofono_radio_settings_band_query_cb_t cb = cbd->cb;
	enum ofono_radio_band_gsm band_gsm;
	enum ofono_radio_band_umts band_umts;
	struct ofono_error error;
	GAtResultIter iter;
	unsigned int band;
	const char *band_str;

	decode_at_error(&error, g_at_result_final_response(result));

	if (!ok) {
		cb(&error, -1, -1, cbd->data);
		return;
	}

	g_at_result_iter_init(&iter, result);

	if (g_at_result_iter_next(&iter, "^SYSCFG:") == FALSE)
		goto error;

	if (g_at_result_iter_skip_next(&iter) == FALSE)
		goto error;

	if (g_at_result_iter_skip_next(&iter) == FALSE)
		goto error;

	if(g_at_result_iter_next_unquoted_string(&iter, &band_str) == FALSE)
		goto error;

	sscanf((const char *) band_str, "%x", &band);

	band_gsm = band_gsm_from_huawei(band);
	band_umts = band_umts_from_huawei(band);

	cb(&error, band_gsm, band_umts, cbd->data);

	return;

error:
	CALLBACK_WITH_FAILURE(cb, -1, -1, cbd->data);
}

static void huawei_query_band(struct ofono_radio_settings *rs,
		ofono_radio_settings_band_query_cb_t cb, void *data)
{
	struct radio_settings_data *rsd = ofono_radio_settings_get_data(rs);
	struct cb_data *cbd = cb_data_new(cb, data);

	if (g_at_chat_send(rsd->chat, "AT^SYSCFG?", syscfg_prefix,
				syscfg_query_band_cb, cbd, g_free) == 0) {
		CALLBACK_WITH_FAILURE(cb, -1, -1, data);
		g_free(cbd);
	}
}

static void syscfg_support_cb(gboolean ok, GAtResult *result,
				gpointer user_data)
{
	struct ofono_radio_settings *rs = user_data;

	if (!ok) {
		ofono_radio_settings_remove(rs);
		return;
	}

	ofono_radio_settings_register(rs);
}

static void syscfgex_support_cb(gboolean ok, GAtResult *result,
				gpointer user_data)
{
	struct ofono_radio_settings *rs = user_data;
	struct radio_settings_data *rsd = ofono_radio_settings_get_data(rs);

	if (!ok) {
		g_at_chat_send(rsd->chat, "AT^SYSCFG=?", syscfg_prefix,
					syscfg_support_cb, rs, NULL);
		return;
	}

	rsd->syscfgex_cap = 1;
	ofono_radio_settings_register(rs);
}

static int huawei_radio_settings_probe(struct ofono_radio_settings *rs,
					unsigned int vendor, void *data)
{
	GAtChat *chat = data;
	struct radio_settings_data *rsd;

	rsd = g_try_new0(struct radio_settings_data, 1);
	if (rsd == NULL)
		return -ENOMEM;

	rsd->chat = g_at_chat_clone(chat);

	ofono_radio_settings_set_data(rs, rsd);

	g_at_chat_send(rsd->chat, "AT^SYSCFGEX=?", syscfgex_prefix,
					syscfgex_support_cb, rs, NULL);

	return 0;
}

static void huawei_radio_settings_remove(struct ofono_radio_settings *rs)
{
	struct radio_settings_data *rsd = ofono_radio_settings_get_data(rs);

	ofono_radio_settings_set_data(rs, NULL);

	g_at_chat_unref(rsd->chat);
	g_free(rsd);
}

static const struct ofono_radio_settings_driver driver = {
	.name			= "huaweimodem",
	.probe			= huawei_radio_settings_probe,
	.remove			= huawei_radio_settings_remove,
	.query_rat_mode		= huawei_query_rat_mode,
	.set_rat_mode		= huawei_set_rat_mode,
	.query_band		= huawei_query_band,
	.set_band		= huawei_set_band,
};

void huawei_radio_settings_init(void)
{
	ofono_radio_settings_driver_register(&driver);
}

void huawei_radio_settings_exit(void)
{
	ofono_radio_settings_driver_unregister(&driver);
}