Boot Linux faster!

Check our new training course

Boot Linux faster!

Check our new training course
and Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

   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
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
/*
 * This file is part of oFono - Open Source Telephony
 *
 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 *
 * Contact: Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * 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

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>

#include <glib.h>

#include <gisi/netlink.h>
#include <gisi/client.h>
#include <gisi/iter.h>

#include <ofono/log.h>
#include <ofono/modem.h>
#include <ofono/voicecall.h>

#include "isimodem.h"
#include "isiutil.h"
#include "call.h"
#include "debug.h"

struct isi_call {
	uint8_t id, call_id, status, mode, mode_info, cause_type, cause;
	uint8_t addr_type, presentation;
	uint8_t reason;
	char address[20], addr_pad[4];
};

struct isi_voicecall {
	GIsiClient *client;

	struct isi_call_req_context *queue;

	struct isi_call calls[8];
};

/* ------------------------------------------------------------------------- */

static void isi_call_notify(struct ofono_voicecall *ovc,
				struct isi_call *call);
static void isi_call_release(struct ofono_voicecall *, struct isi_call *);
static struct ofono_call isi_call_as_ofono_call(struct isi_call const *);
static int isi_call_status_to_clcc(struct isi_call const *call);
static struct isi_call *isi_call_set_idle(struct isi_call *call);

typedef void GIsiIndication(GIsiClient *client,
		const void *restrict data, size_t len,
		uint16_t object, void *opaque);

typedef void GIsiVerify(GIsiClient *client, gboolean alive, uint16_t object,
				void *opaque);

typedef gboolean GIsiResponse(GIsiClient *client,
				void const *restrict data, size_t len,
				uint16_t object, void *opaque);

static GIsiVerify isi_call_verify_cb;
static gboolean isi_call_register(gpointer);

enum {
	ISI_CALL_TIMEOUT = 1000,
};

/* ------------------------------------------------------------------------- */
/* Request context for voicecall cb */

struct isi_call_req_context;

typedef void isi_call_req_step(struct isi_call_req_context *, int reason);

struct isi_call_req_context {
	struct isi_call_req_context *next, **prev;
	isi_call_req_step *step;
	struct ofono_voicecall *ovc;
	ofono_voicecall_cb_t cb;
	void *data;
};

static struct isi_call_req_context *
isi_call_req(struct ofono_voicecall *ovc,
		void const *restrict req,
		size_t len,
		GIsiResponse *handler,
		ofono_voicecall_cb_t cb, void *data)
{
	struct isi_voicecall *ivc;
	struct isi_call_req_context *irc;

	ivc = ofono_voicecall_get_data(ovc);

	irc = g_try_new0(struct isi_call_req_context, 1);

	if (irc) {
		irc->ovc = ovc;
		irc->cb = cb;
		irc->data = data;

		if (g_isi_request_make(ivc->client, req, len,
					ISI_CALL_TIMEOUT, handler, irc))
			return irc;
	}

	g_free(irc);

	if (cb)
		CALLBACK_WITH_FAILURE(cb, data);

	return NULL;
}

static void isi_ctx_queue(struct isi_call_req_context *irc,
				isi_call_req_step *next)
{
	if (irc->prev == NULL) {
		struct isi_voicecall *ivc = ofono_voicecall_get_data(irc->ovc);

		if (ivc->queue) {
			irc->next = ivc->queue;
			irc->next->prev = &irc->next;
		}
		irc->prev = &ivc->queue;
		ivc->queue = irc;
	}

	irc->step = next;
}

static void isi_ctx_remove(struct isi_call_req_context *irc)
{
	if (irc->prev) {
		*irc->prev = irc->next;

		if (irc->next) {
			irc->next->prev = irc->prev;
			irc->next = NULL;
		}
		irc->prev = NULL;
	}
}

static void isi_ctx_free(struct isi_call_req_context *irc)
{
	if (irc) {
		isi_ctx_remove(irc);
		g_free(irc);
	}
}

static gboolean isi_ctx_return(struct isi_call_req_context *irc,
				enum ofono_error_type type,
				int error)
{
	if (!irc)
		return TRUE;

	if (irc->cb) {
		struct ofono_error e = { .type = type, .error = error };
		irc->cb(&e, irc->data);
	}

	isi_ctx_free(irc);

	return TRUE;
}

static gboolean isi_ctx_return_failure(struct isi_call_req_context *irc)
{
	return isi_ctx_return(irc, OFONO_ERROR_TYPE_FAILURE, 0);
}

static gboolean isi_ctx_return_success(struct isi_call_req_context *irc)
{
	if (irc && irc->step) {
		irc->step(irc, 0);
		return TRUE;
	}

	return isi_ctx_return(irc, OFONO_ERROR_TYPE_NO_ERROR, 0);
}

/* ------------------------------------------------------------------------- */
/* Decoding subblocks */

static void isi_call_any_address_sb_proc(struct isi_voicecall *ivc,
						struct isi_call *call,
						GIsiSubBlockIter const *sb)
{
	uint8_t addr_type, presentation, addr_len;
	char *address;

	if (!g_isi_sb_iter_get_byte(sb, &addr_type, 2) ||
		!g_isi_sb_iter_get_byte(sb, &presentation, 3) ||
		/* fillerbyte */
		!g_isi_sb_iter_get_byte(sb, &addr_len, 5) ||
		!g_isi_sb_iter_get_alpha_tag(sb, &address, 2 * addr_len, 6))
		return;

	call->addr_type = addr_type | 0x80;
	call->presentation = presentation;
	strncpy(call->address, address, sizeof call->address);

	g_free(address);
}

static void isi_call_origin_address_sb_proc(struct isi_voicecall *ivc,
						struct isi_call *call,
						GIsiSubBlockIter const *sb)
{
	if (!call->address[0])
		isi_call_any_address_sb_proc(ivc, call, sb);
}

static void isi_call_destination_address_sb_proc(struct isi_voicecall *ivc,
						struct isi_call *call,
						GIsiSubBlockIter const *sb)
{
	if (!call->address[0])
		isi_call_any_address_sb_proc(ivc, call, sb);
}

static void isi_call_mode_sb_proc(struct isi_voicecall *ivc,
					struct isi_call *call,
					GIsiSubBlockIter const *sb)
{
	uint8_t mode, mode_info;

	if (!g_isi_sb_iter_get_byte(sb, &mode, 2) ||
			!g_isi_sb_iter_get_byte(sb, &mode_info, 3))
		return;

	call->mode = mode;
	call->mode_info = mode_info;
}

static void isi_call_cause_sb_proc(struct isi_voicecall *ivc,
					struct isi_call *call,
					GIsiSubBlockIter const *sb)
{
	uint8_t cause_type, cause;

	if (!g_isi_sb_iter_get_byte(sb, &cause_type, 2) ||
			!g_isi_sb_iter_get_byte(sb, &cause, 3))
		return;

	call->cause_type = cause_type;
	call->cause = cause;
}

static void isi_call_status_sb_proc(struct isi_voicecall *ivc,
					struct isi_call *call,
					GIsiSubBlockIter const *sb)
{
	uint8_t status;

	if (!g_isi_sb_iter_get_byte(sb, &status, 2))
		return;

	call->status = status;
}

static struct isi_call *
isi_call_status_info_sb_proc(struct isi_voicecall *ivc,
				GIsiSubBlockIter const *sb)
{
	struct isi_call *call = NULL;
	int i;
	uint8_t call_id;
	uint8_t mode;
	uint8_t mode_info;
	uint8_t status;

	if (!g_isi_sb_iter_get_byte(sb, &call_id, 2) ||
			!g_isi_sb_iter_get_byte(sb, &mode, 3) ||
			!g_isi_sb_iter_get_byte(sb, &mode_info, 4) ||
			!g_isi_sb_iter_get_byte(sb, &status, 5))
		return NULL;

	i = call_id & 7;

	if (1 <= i && i <= 7) {
		call = &ivc->calls[i];
		call->call_id = call_id;
		call->status = status;
		call->mode = mode;
		call->mode_info = mode_info;
	}

	return call;
}

static struct isi_call *
isi_call_addr_and_status_info_sb_proc(struct isi_voicecall *ivc,
					GIsiSubBlockIter const *sb)
{
	struct isi_call *call = NULL;
	int i;
	uint8_t call_id;
	uint8_t mode;
	uint8_t mode_info;
	uint8_t status;
	uint8_t addr_type;
	uint8_t presentation;
	uint8_t addr_len;
	char *address;

	if (!g_isi_sb_iter_get_byte(sb, &call_id, 2) ||
		!g_isi_sb_iter_get_byte(sb, &mode, 3) ||
		!g_isi_sb_iter_get_byte(sb, &mode_info, 4) ||
		!g_isi_sb_iter_get_byte(sb, &status, 5) ||
		!g_isi_sb_iter_get_byte(sb, &addr_type, 8) ||
		!g_isi_sb_iter_get_byte(sb, &presentation, 9) ||
		!g_isi_sb_iter_get_byte(sb, &addr_len, 11) ||
		!g_isi_sb_iter_get_alpha_tag(sb, &address, 2 * addr_len, 12))
		return NULL;

	i = call_id & 7;

	if (1 <= i && i <= 7) {
		call = &ivc->calls[i];
		call->call_id = call_id;
		call->status = status;
		call->mode = mode;
		call->mode_info = mode_info;
		call->addr_type = addr_type | 0x80;
		call->presentation = presentation;
		strncpy(call->address, address, sizeof call->address);
	}

	free(address);

	return call;
}

/* ------------------------------------------------------------------------- */
/* PN_CALL messages */

static GIsiResponse isi_call_status_resp,
	isi_call_create_resp,
	isi_call_answer_resp,
	isi_call_release_resp,
	isi_call_control_resp,
	isi_call_dtmf_send_resp;

static struct isi_call_req_context *
isi_call_create_req(struct ofono_voicecall *ovc,
			uint8_t presentation,
			uint8_t addr_type,
			char const address[21],
			ofono_voicecall_cb_t cb,
			void *data)
{
	size_t addr_len = strlen(address);
	size_t sub_len = (6 + 2 * addr_len + 3) & ~3;
	size_t i, offset = 3 + 4 + 8 + 6;
	uint8_t req[3 + 4 + 8 + 6 + 40] = {
		CALL_CREATE_REQ,
		0,		/* No id */
		3,		/* Mode, Clir, Number */
		/* MODE SB */
		CALL_MODE, 4, CALL_MODE_SPEECH, CALL_MODE_INFO_NONE,
		/* ORIGIN_INFO SB */
		CALL_ORIGIN_INFO, 8, presentation, 0, 0, 0, 0, 0,
		/* DESTINATION_ADDRESS SB */
		CALL_DESTINATION_ADDRESS,
		sub_len,
		addr_type & 0x7F,
		0, 0,
		addr_len,
		/* uint16_t addr[20] */
	};
	size_t rlen = 3 + 4 + 8 + sub_len;

	if (addr_len > 20) {
		CALLBACK_WITH_FAILURE(cb, data);
		return NULL;
	}

	for (i = 0; i < addr_len; i++)
		req[offset + 2 * i + 1] = address[i];

	return isi_call_req(ovc, req, rlen, isi_call_create_resp, cb, data);
}

static gboolean isi_call_create_resp(GIsiClient *client,
					void const *restrict data,
					size_t len,
					uint16_t object,
					void *irc)
{
	struct {
		uint8_t message_id, call_id, sub_blocks;
	} const *m = data;

	if (m != NULL && len < (sizeof *m))
		return FALSE;
	if (m == NULL || m->message_id == CALL_COMMON_MESSAGE)
		return isi_ctx_return_failure(irc);
	if (m->message_id != CALL_CREATE_RESP)
		return FALSE;

	if (m->call_id != CALL_ID_NONE && m->sub_blocks == 0)
		return isi_ctx_return_success(irc);

	/* Cause ? */
	return isi_ctx_return_failure(irc);
}

static void isi_call_status_ind_cb(GIsiClient *client,
					void const *restrict data,
					size_t len,
					uint16_t object,
					void *_ovc)
{
	struct ofono_voicecall *ovc = _ovc;
	struct isi_voicecall *ivc = ofono_voicecall_get_data(ovc);
	struct {
		uint8_t message_id, call_id, sub_blocks;
	} const *m = data;
	struct isi_call *call;
	uint8_t old;
	GIsiSubBlockIter sb[1];

	if (len < 3)
		return;		/* runt */

	if ((m->call_id & 7) == 0)
		return;

	call = &ivc->calls[m->call_id & 7];

	old = call->status;
	call->call_id = m->call_id;

	for (g_isi_sb_iter_init(sb, data, len, (sizeof *m));
			g_isi_sb_iter_is_valid(sb);
				g_isi_sb_iter_next(sb)) {
		switch (g_isi_sb_iter_get_id(sb)) {
		case CALL_STATUS:
			isi_call_status_sb_proc(ivc, call, sb);
			break;

		case CALL_MODE:
			isi_call_mode_sb_proc(ivc, call, sb);
			break;

		case CALL_CAUSE:
			isi_call_cause_sb_proc(ivc, call, sb);
			break;

		case CALL_DESTINATION_ADDRESS:
			isi_call_destination_address_sb_proc(ivc, call, sb);
			break;

		case CALL_ORIGIN_ADDRESS:
			isi_call_origin_address_sb_proc(ivc, call, sb);
			break;

		case CALL_GSM_DETAILED_CAUSE:
		case CALL_DESTINATION_PRE_ADDRESS:
		case CALL_DESTINATION_POST_ADDRESS:
		case CALL_DESTINATION_SUBADDRESS:
		case CALL_GSM_EVENT_INFO:
		case CALL_NW_CAUSE:
			break;
		}
	}

	if (old != call->status) {
		if (call->status == CALL_STATUS_IDLE) {
			call->status = CALL_STATUS_TERMINATED;
			isi_call_notify(ovc, call);
			isi_call_set_idle(call);
			return;
		}
	}

	isi_call_notify(ovc, call);
}

static struct isi_call_req_context *
isi_call_answer_req(struct ofono_voicecall *ovc,
			uint8_t call_id, ofono_voicecall_cb_t cb, void *data)
{
	uint8_t const req[] = {
		CALL_ANSWER_REQ, call_id, 0
	};
	size_t rlen = sizeof req;

	return isi_call_req(ovc, req, rlen, isi_call_answer_resp, cb, data);
}

static gboolean isi_call_answer_resp(GIsiClient *client,
					void const *restrict data,
					size_t len,
					uint16_t object,
					void *irc)
{
	struct {
		uint8_t message_id, call_id, sub_blocks;
	} const *m = data;

	if (m != NULL && len < (sizeof *m))
		return FALSE;
	if (m == NULL || m->message_id == CALL_COMMON_MESSAGE)
		return isi_ctx_return_failure(irc);
	if (m->message_id != CALL_ANSWER_RESP)
		return FALSE;

	if (m->call_id != CALL_ID_NONE && m->sub_blocks == 0)
		return isi_ctx_return_success(irc);

	/* Cause ? */
	return isi_ctx_return_failure(irc);
}

static struct isi_call_req_context *
isi_call_release_req(struct ofono_voicecall *ovc,
			uint8_t call_id, enum call_cause_type cause_type,
			uint8_t cause, ofono_voicecall_cb_t cb, void *data)
{
	uint8_t const req[] = {
		CALL_RELEASE_REQ, call_id, 1,
		CALL_CAUSE, 4, cause_type, cause,
	};
	size_t rlen = sizeof req;

	return isi_call_req(ovc, req, rlen, isi_call_release_resp, cb, data);
}

static gboolean isi_call_release_resp(GIsiClient *client,
					void const *restrict data,
					size_t len,
					uint16_t object,
					void *irc)
{
	struct {
		uint8_t message_id, call_id, sub_blocks;
	} const *m = data;
	GIsiSubBlockIter i[1];
	uint8_t cause_type = 0, cause = 0;

	if (m != NULL && len < (sizeof *m))
		return FALSE;
	if (m == NULL || m->message_id == CALL_COMMON_MESSAGE)
		return isi_ctx_return_failure(irc);
	if (m->message_id != CALL_RELEASE_RESP)
		return FALSE;

	for (g_isi_sb_iter_init(i, m, len, (sizeof *m));
			g_isi_sb_iter_is_valid(i);
				g_isi_sb_iter_next(i)) {
		if (g_isi_sb_iter_get_id(i) != CALL_CAUSE ||
				!g_isi_sb_iter_get_byte(i, &cause_type, 2) ||
				!g_isi_sb_iter_get_byte(i, &cause, 3))
			continue;
	}

	if ((cause_type == CALL_CAUSE_TYPE_SERVER ||
			cause_type == CALL_CAUSE_TYPE_CLIENT) &&
			(cause == CALL_CAUSE_RELEASE_BY_USER ||
			cause == CALL_CAUSE_BUSY_USER_REQUEST))
		return isi_ctx_return_success(irc);
	else
		return isi_ctx_return_failure(irc);
}

static struct isi_call_req_context *
isi_call_status_req(struct ofono_voicecall *ovc,
			uint8_t id, uint8_t mode,
			ofono_voicecall_cb_t cb, void *data)
{
	unsigned char req[] = {
		CALL_STATUS_REQ, id, 1,
		CALL_STATUS_MODE, 4, mode, 0,
	};
	size_t rlen = sizeof req;

	return isi_call_req(ovc, req, rlen, isi_call_status_resp, cb, data);
}


static gboolean isi_call_status_resp(GIsiClient *client,
					void const *restrict data,
					size_t len,
					uint16_t object,
					void *_irc)
{
	struct isi_call_req_context *irc = _irc;
	struct ofono_voicecall *ovc = irc->ovc;
	struct isi_voicecall *ivc = ofono_voicecall_get_data(ovc);
	struct {
		uint8_t message_id, call_id, sub_blocks;
	} const *m = data;
	GIsiSubBlockIter sb[1];
	struct isi_call *call = NULL;

	if (m != NULL && len < (sizeof *m))
		return FALSE;
	if (m == NULL || m->message_id == CALL_COMMON_MESSAGE)
		return isi_ctx_return_failure(irc);
	if (m->message_id != CALL_STATUS_RESP)
		return FALSE;

	for (g_isi_sb_iter_init(sb, m, len, (sizeof *m));
			g_isi_sb_iter_is_valid(sb);
				g_isi_sb_iter_next(sb)) {
		switch (g_isi_sb_iter_get_id(sb)) {

		case CALL_STATUS_INFO:
			call = isi_call_status_info_sb_proc(ivc, sb);
			break;

		case CALL_ADDR_AND_STATUS_INFO:
			call = isi_call_addr_and_status_info_sb_proc(ivc, sb);
			if (call)
				isi_call_notify(ovc, call);
			break;

		case CALL_CAUSE:
			if (call)
				isi_call_cause_sb_proc(ivc, call, sb);
			break;
		}
	}

	return isi_ctx_return_success(irc);
}

static struct isi_call_req_context *
isi_call_control_req(struct ofono_voicecall *ovc,
			uint8_t call_id, enum call_operation op, uint8_t info,
			ofono_voicecall_cb_t cb, void *data)
{
	uint8_t const req[] = {
		CALL_CONTROL_REQ, call_id, 1,
		CALL_OPERATION, 4, op, info,
	};
	size_t rlen = sizeof req;

	return isi_call_req(ovc, req, rlen, isi_call_control_resp, cb, data);
}

static struct isi_call_req_context *
isi_call_deflect_req(struct ofono_voicecall *ovc,
			uint8_t call_id, uint8_t address_type,
			char const address[21],
			ofono_voicecall_cb_t cb, void *data)
{
	size_t addr_len = strlen(address);
	size_t sub_len = (6 + 2 * addr_len + 3) & ~3;
	size_t i, offset = 3 + 4 + 6;
	size_t rlen = 3 + 4 + sub_len;
	uint8_t req[3 + 4 + 6 + 40] = {
		CALL_CONTROL_REQ, call_id, 2,
		CALL_OPERATION, 4, CALL_GSM_OP_DEFLECT, 0,
		CALL_GSM_DEFLECTION_ADDRESS, sub_len,
		address_type & 0x7F,
		0x7,		/* default presentation */
		0,		/* filler */
		addr_len,
	};

	if (addr_len > 20) {
		CALLBACK_WITH_FAILURE(cb, data);
		return FALSE;
	}

	for (i = 0; i < addr_len; i++)
		req[offset + 2 * i + 1] = address[i];

	return isi_call_req(ovc, req, rlen, isi_call_control_resp, cb, data);
}

static gboolean isi_call_control_resp(GIsiClient *client,
					void const *restrict data,
					size_t len,
					uint16_t object,
					void *irc)
{
	struct {
		uint8_t message_id, call_id, sub_blocks;
	} const *m = data;
	GIsiSubBlockIter i[1];
	uint8_t cause_type = 0, cause = 0;

	if (m != NULL && len < sizeof *m)
		return FALSE;
	if (m == NULL || m->message_id == CALL_COMMON_MESSAGE)
		return isi_ctx_return_failure(irc);
	if (m->message_id != CALL_CONTROL_RESP)
		return FALSE;

	for (g_isi_sb_iter_init(i, m, len, (sizeof *m));
			g_isi_sb_iter_is_valid(i);
				g_isi_sb_iter_next(i)) {
		if (g_isi_sb_iter_get_id(i) != CALL_CAUSE ||
				!g_isi_sb_iter_get_byte(i, &cause_type, 2) ||
				!g_isi_sb_iter_get_byte(i, &cause, 3))
			continue;
	}

	if (!cause)
		return isi_ctx_return_success(irc);
	else
		return isi_ctx_return_failure(irc);
}

static struct isi_call_req_context *
isi_call_dtmf_send_req(struct ofono_voicecall *ovc,
			uint8_t call_id, char const *string,
			ofono_voicecall_cb_t cb, void *data)
{
	size_t str_len = strlen(string);
	size_t sub_len = 4 + ((2 * str_len + 3) & ~3);
	size_t i, offset = 3 + 4 + 8 + 4;
	size_t rlen = 3 + 4 + 8 + sub_len;
	uint8_t req[3 + 4 + 8 + (255 & ~3)] = {
		CALL_DTMF_SEND_REQ, call_id, 3,
		CALL_DTMF_INFO, 4, CALL_DTMF_ENABLE_TONE_IND_SEND, 0,
		CALL_DTMF_TIMERS, 8,
		0, 200, /* duration in ms */
		0, 100, /* gap in ms */
		0, 0,	/* filler */
		CALL_DTMF_STRING, sub_len,
		100,     /* pause length */
		str_len,
		/* string */
	};

	if (sub_len >= 256) {
		CALLBACK_WITH_FAILURE(cb, data);
		return FALSE;
	}

	for (i = 0; i < str_len; i++)
		req[offset + 2 * i + 1] = string[i];

	return isi_call_req(ovc, req, rlen, isi_call_dtmf_send_resp, cb, data);
}

static gboolean isi_call_dtmf_send_resp(GIsiClient *client,
					void const *restrict data,
					size_t len,
					uint16_t object,
					void *irc)
{
	struct {
		uint8_t message_id, call_id, sub_blocks;
	} const *m = data;
	GIsiSubBlockIter i[1];
	uint8_t cause_type = 0, cause = 0;

	if (m != NULL && len < (sizeof *m))
		return FALSE;
	if (m == NULL || m->message_id == CALL_COMMON_MESSAGE)
		return isi_ctx_return_failure(irc);
	if (m->message_id != CALL_DTMF_SEND_RESP)
		return FALSE;

	if (m->sub_blocks == 0)
		return isi_ctx_return_success(irc);

	for (g_isi_sb_iter_init(i, m, len, (sizeof *m));
			g_isi_sb_iter_is_valid(i);
				g_isi_sb_iter_next(i)) {
		if (g_isi_sb_iter_get_id(i) != CALL_CAUSE ||
				!g_isi_sb_iter_get_byte(i, &cause_type, 2) ||
				!g_isi_sb_iter_get_byte(i, &cause, 3))
			continue;
	}

	if (!cause)
		return isi_ctx_return_success(irc);
	else
		return isi_ctx_return_failure(irc);
}


/* ------------------------------------------------------------------------- */
/* Notify */

static void isi_call_notify(struct ofono_voicecall *ovc,
				struct isi_call *call)
{
	struct isi_voicecall *ivc = ofono_voicecall_get_data(ovc);
	struct isi_call_req_context *irc, **queue;
	struct ofono_call ocall;

	DBG("called with status=%s (0x%02X)",
		call_status_name(call->status), call->status);

	for (queue = &ivc->queue; (irc = *queue);) {
		irc->step(irc, call->status);
		if (*queue == irc)
			queue = &irc->next;
	}

	switch (call->status) {
	case CALL_STATUS_IDLE:
	case CALL_STATUS_MO_RELEASE:
	case CALL_STATUS_MT_RELEASE:
	case CALL_STATUS_TERMINATED:
		isi_call_release(ovc, call);
		return;
	}

	ocall = isi_call_as_ofono_call(call);

	DBG("id=%u,%s,%u,\"%s\",%u,%u",
		ocall.id,
		ocall.direction ? "terminated" : "originated",
		ocall.status,
		ocall.phone_number.number,
		ocall.phone_number.type,
		ocall.clip_validity);

	ofono_voicecall_notify(ovc, &ocall);
}

static void isi_call_release(struct ofono_voicecall *ovc,
				struct isi_call *call)
{
	struct ofono_error error = { OFONO_ERROR_TYPE_NO_ERROR, 0 };
	enum ofono_disconnect_reason reason;

	switch (call->status) {
	case CALL_STATUS_IDLE:
		reason = OFONO_DISCONNECT_REASON_UNKNOWN;
		break;
	case CALL_STATUS_MO_RELEASE:
		reason = OFONO_DISCONNECT_REASON_LOCAL_HANGUP;
		break;
	case CALL_STATUS_MT_RELEASE:
		reason = OFONO_DISCONNECT_REASON_REMOTE_HANGUP;
		break;
	case CALL_STATUS_TERMINATED:
	default:
		reason = OFONO_DISCONNECT_REASON_ERROR;
		break;
	}

	if (!call->reason) {
		call->reason = reason;
		DBG("disconnected id=%u reason=%u", call->id, reason);
		ofono_voicecall_disconnected(ovc, call->id, reason, &error);
	}

	if (!reason)
		isi_call_set_idle(call);
}

static struct ofono_call isi_call_as_ofono_call(struct isi_call const *call)
{
	struct ofono_call ocall = { call->id };
	struct ofono_phone_number *number = &ocall.phone_number;

	ocall.type = 0;	/* Voice call */
	ocall.direction = call->mode_info & CALL_MODE_ORIGINATOR;
	ocall.status = isi_call_status_to_clcc(call);
	memcpy(number->number, call->address, sizeof number->number);
	number->type = 0x80 | call->addr_type;
	ocall.clip_validity = call->presentation & 3;
	if (ocall.clip_validity == 0 && strlen(number->number) == 0)
		ocall.clip_validity = 2;

	return ocall;
}

/** Get +CLCC status */
static int isi_call_status_to_clcc(struct isi_call const *call)
{
	switch (call->status) {
	case CALL_STATUS_CREATE:
		return 2;
	case CALL_STATUS_COMING:
		return 4;
	case CALL_STATUS_PROCEEDING:
		if ((call->mode_info & CALL_MODE_ORIGINATOR))
			return 4;
		else
			return 2;
	case CALL_STATUS_MO_ALERTING:
		return 3;
	case CALL_STATUS_MT_ALERTING:
		return 4;
	case CALL_STATUS_WAITING:
		return 5;

	case CALL_STATUS_ANSWERED:
	case CALL_STATUS_ACTIVE:
	case CALL_STATUS_MO_RELEASE:
	case CALL_STATUS_MT_RELEASE:
	case CALL_STATUS_HOLD_INITIATED:
		return 0;

	case CALL_STATUS_HOLD:
	case CALL_STATUS_RETRIEVE_INITIATED:
		return 1;

	case CALL_STATUS_RECONNECT_PENDING:
	case CALL_STATUS_TERMINATED:
	case CALL_STATUS_SWAP_INITIATED:
		return 0;
	}

	return 0;
}

static struct isi_call *isi_call_set_idle(struct isi_call *call)
{
	uint8_t id;

	if (call) {
		id = call->id;
		memset(call, 0, sizeof *call);
		call->id = id;
	}

	return call;
}

/* ---------------------------------------------------------------------- */

static void isi_dial(struct ofono_voicecall *ovc,
			const struct ofono_phone_number *restrict number,
			enum ofono_clir_option clir,
			enum ofono_cug_option cug,
			ofono_voicecall_cb_t cb, void *data)
{
	unsigned char presentation = CALL_GSM_PRESENTATION_DEFAULT;

	switch (clir) {
	case OFONO_CLIR_OPTION_DEFAULT:
		presentation = CALL_GSM_PRESENTATION_DEFAULT;
		break;
	case OFONO_CLIR_OPTION_INVOCATION:
		presentation = CALL_PRESENTATION_RESTRICTED;
		break;
	case OFONO_CLIR_OPTION_SUPPRESSION:
		presentation = CALL_PRESENTATION_ALLOWED;
		break;
	}

	switch (cug) {
	case OFONO_CUG_OPTION_DEFAULT:
		break;
	case OFONO_CUG_OPTION_INVOCATION:
		/* Not implemented */
		CALLBACK_WITH_FAILURE(cb, data);
		return;
	}

	isi_call_create_req(ovc, presentation, number->type, number->number,
				cb, data);
}

static void isi_answer(struct ofono_voicecall *ovc,
			ofono_voicecall_cb_t cb, void *data)
{
	isi_call_answer_req(ovc, CALL_ID_ALL, cb, data);
}

static void isi_hangup(struct ofono_voicecall *ovc,
			ofono_voicecall_cb_t cb, void *data)
{
	/* AT+CHUP */
	isi_call_release_req(ovc, CALL_ID_ALL, CALL_CAUSE_TYPE_CLIENT,
				CALL_CAUSE_RELEASE_BY_USER, cb, data);
}

static void isi_release_all_held(struct ofono_voicecall *ovc,
				ofono_voicecall_cb_t cb, void *data)
{
	/* AT+CHLD=0 (w/out incoming calls) */
	isi_call_release_req(ovc, CALL_ID_HOLD, CALL_CAUSE_TYPE_CLIENT,
				CALL_CAUSE_RELEASE_BY_USER, cb, data);
}

static void isi_set_udub(struct ofono_voicecall *ovc,
				ofono_voicecall_cb_t cb, void *data)
{
	/* AT+CHLD=0 (w/ incoming calls) */
	struct isi_voicecall *ivc = ofono_voicecall_get_data(ovc);
	int id = 0;

	for (id = 1; id <= 7; id++) {
		if (ivc->calls[id].status == CALL_STATUS_WAITING)
			break;
		if (ivc->calls[id].status == CALL_STATUS_MT_ALERTING)
			break;
		if (ivc->calls[id].status == CALL_STATUS_COMING)
			break;
	}

	if (id <= 7)
		isi_call_release_req(ovc, id, CALL_CAUSE_TYPE_CLIENT,
					CALL_CAUSE_BUSY_USER_REQUEST,
					cb, data);
	else
		CALLBACK_WITH_FAILURE(cb, data);
}

static void isi_retrieve(struct ofono_voicecall *ovc,
				ofono_voicecall_cb_t cb, void *data)
{
	isi_call_control_req(ovc, CALL_ID_HOLD, CALL_OP_RETRIEVE, 0, cb, data);
}

static isi_call_req_step isi_wait_and_answer, isi_wait_and_retrieve;

static void isi_release_all_active(struct ofono_voicecall *ovc,
					ofono_voicecall_cb_t cb, void *data)
{
	/* AT+CHLD=1 */
	struct isi_voicecall *ivc = ofono_voicecall_get_data(ovc);
	int id = 0, waiting = 0, active = 0, hold = 0;

	for (id = 1; id <= 7; id++) {
		if (ivc->calls[id].call_id & CALL_ID_WAITING)
			waiting++;
		if (ivc->calls[id].call_id & CALL_ID_HOLD)
			hold++;
		if (ivc->calls[id].call_id & CALL_ID_ACTIVE)
			active++;
	}

	if (active) {
		struct isi_call_req_context *irc;

		irc = isi_call_release_req(ovc, CALL_ID_ACTIVE,
						CALL_CAUSE_TYPE_CLIENT,
						CALL_CAUSE_RELEASE_BY_USER,
						cb, data);

		if (irc == NULL)
			;
		else if (waiting)
			isi_ctx_queue(irc, isi_wait_and_answer);
		else if (hold)
			isi_ctx_queue(irc, isi_wait_and_retrieve);
	} else
		CALLBACK_WITH_FAILURE(cb, data);
}

static void isi_wait_and_answer(struct isi_call_req_context *irc,
				int event)
{
	DBG("irc=%p event=%u", (void *)irc, event);
	switch (event) {
	case CALL_STATUS_TERMINATED:
		isi_answer(irc->ovc, irc->cb, irc->data);
		isi_ctx_free(irc);
		break;
	}
}

static void isi_wait_and_retrieve(struct isi_call_req_context *irc,
					int event)
{
	DBG("irc=%p event=%u", (void *)irc, event);
	switch (event) {
	case CALL_STATUS_TERMINATED:
		isi_retrieve(irc->ovc, irc->cb, irc->data);
		isi_ctx_free(irc);
		break;
	}
}

static void isi_hold_all_active(struct ofono_voicecall *ovc,
					ofono_voicecall_cb_t cb, void *data)
{
	/* AT+CHLD=2 */
	struct isi_voicecall *ivc = ofono_voicecall_get_data(ovc);
	int id = 0, op = 0, waiting = 0, active = 0, hold = 0;

	for (id = 1; id <= 7; id++) {
		if (ivc->calls[id].call_id & CALL_ID_WAITING)
			waiting++;
		if (ivc->calls[id].call_id & CALL_ID_HOLD)
			hold++;
		if (ivc->calls[id].call_id & CALL_ID_ACTIVE)
			active++;
	}

	if (waiting) {
		isi_call_answer_req(ovc, CALL_ID_WAITING, cb, data);
	} else if (hold) {
		if (active) {
			op = CALL_OP_SWAP;
			id = CALL_ID_ACTIVE;
		} else {
			op = CALL_OP_RETRIEVE;
			id = CALL_ID_HOLD;
		}
		isi_call_control_req(ovc, id, op, 0, cb, data);
	} else if (active) {
		id = CALL_ID_ACTIVE, op = CALL_OP_HOLD;
		isi_call_control_req(ovc, id, op, 0, cb, data);
	} else {
		CALLBACK_WITH_FAILURE(cb, data);
	}
}

static void isi_release_specific(struct ofono_voicecall *ovc, int id,
					ofono_voicecall_cb_t cb, void *data)
{
	/* AT+CHLD=1X */
	struct isi_voicecall *ivc = ofono_voicecall_get_data(ovc);

	if (1 <= id && id <= 7) {
		struct isi_call const *status = &ivc->calls[id];
		uint8_t cause = CALL_CAUSE_RELEASE_BY_USER;

		switch (status->status) {
		case CALL_STATUS_MT_ALERTING:
		case CALL_STATUS_WAITING:
			cause = CALL_CAUSE_BUSY_USER_REQUEST;
			break;
		case CALL_STATUS_PROCEEDING:
			if ((status->mode_info & CALL_MODE_ORIGINATOR))
				cause = CALL_CAUSE_BUSY_USER_REQUEST;
			break;
		}

		isi_call_release_req(ovc, id,
					CALL_CAUSE_TYPE_CLIENT, cause,
					cb, data);
	} else
		CALLBACK_WITH_FAILURE(cb, data);
}

static void isi_private_chat(struct ofono_voicecall *ovc, int id,
				ofono_voicecall_cb_t cb, void *data)
{
	/* AT+CHLD=2X */
	if (1 <= id && id <= 7)
		isi_call_control_req(ovc, id, CALL_OP_CONFERENCE_SPLIT, 0,
					cb, data);
	else
		CALLBACK_WITH_FAILURE(cb, data);
}

static void isi_create_multiparty(struct ofono_voicecall *ovc,
					ofono_voicecall_cb_t cb, void *data)
{
	/* AT+CHLD=3 */
	isi_call_control_req(ovc, CALL_ID_ALL, CALL_OP_CONFERENCE_BUILD, 0,
				cb, data);
}

static void isi_transfer(struct ofono_voicecall *ovc,
			ofono_voicecall_cb_t cb, void *data)
{
	/* AT+CHLD=4 */
	struct isi_voicecall *ivc = ofono_voicecall_get_data(ovc);

	uint8_t id;

	for (id = 1; id <= 7; id++)
		if (ivc->calls[id].status == CALL_STATUS_MO_ALERTING)
			break;
	if (id > 7)
		id = CALL_ID_ACTIVE;

	isi_call_control_req(ovc, id, CALL_GSM_OP_TRANSFER, 0, cb, data);
}

static void isi_deflect(struct ofono_voicecall *ovc,
			const struct ofono_phone_number *ph,
			ofono_voicecall_cb_t cb, void *data)
{
	/* AT+CTFR=<number>,<type> */
	int id = CALL_ID_WAITING;
	isi_call_deflect_req(ovc, id, ph->type, ph->number, cb, data);
}

static void isi_swap_without_accept(struct ofono_voicecall *ovc,
			ofono_voicecall_cb_t cb, void *data)
{
	struct isi_voicecall *ivc = ofono_voicecall_get_data(ovc);
	int id = 0, op = 0, active = 0, hold = 0;

	for (id = 1; id <= 7; id++) {
		if (ivc->calls[id].call_id & CALL_ID_HOLD)
			hold++;
		if (ivc->calls[id].call_id & CALL_ID_ACTIVE)
			active++;
	}

	if (hold && active) {
		id = CALL_ID_ACTIVE, op = CALL_OP_SWAP;
	} else if (active) {
		id = CALL_ID_ACTIVE, op = CALL_OP_HOLD;
	} else if (hold) {
		id = CALL_ID_HOLD, op = CALL_OP_RETRIEVE;
	} else {
		CALLBACK_WITH_FAILURE(cb, data);
		return;
	}

	isi_call_control_req(ovc, id, op, 0, cb, data);
}

static void isi_send_tones(struct ofono_voicecall *ovc, const char *tones,
				ofono_voicecall_cb_t cb, void *data)
{
	isi_call_dtmf_send_req(ovc, CALL_ID_ALL, tones, cb, data);;
}

static int isi_voicecall_probe(struct ofono_voicecall *ovc,
				unsigned int vendor, void *user)
{
	GIsiModem *idx = user;
	struct isi_voicecall *ivc = g_try_new0(struct isi_voicecall, 1);
	int id;

	if (!ivc)
		return -ENOMEM;

	for (id = 0; id <= 7; id++)
		ivc->calls[id].id = id;

	ivc->client = g_isi_client_create(idx, PN_CALL);
	if (!ivc->client) {
		g_free(ivc);
		return -ENOMEM;
	}

	ofono_voicecall_set_data(ovc, ivc);

	if (!g_isi_verify(ivc->client, isi_call_verify_cb, ovc))
		DBG("Unable to verify reachability");

	return 0;
}

static void isi_call_verify_cb(GIsiClient *client,
				gboolean alive, uint16_t object, void *ovc)
{
	if (!alive) {
		DBG("Unable to bootstrap voice call driver");
		return;
	}

	DBG("%s (v%03d.%03d) reachable",
		pn_resource_name(g_isi_client_resource(client)),
		g_isi_version_major(client),
		g_isi_version_minor(client));

	g_idle_add(isi_call_register, ovc);
}

static gboolean isi_call_register(gpointer _ovc)
{
	struct ofono_voicecall *ovc = _ovc;
	struct isi_voicecall *ivc = ofono_voicecall_get_data(ovc);

	const char *debug = getenv("OFONO_ISI_DEBUG");

	if (debug && (strcmp(debug, "all") == 0 || strcmp(debug, "call") == 0))
		g_isi_client_set_debug(ivc->client, call_debug, NULL);

	g_isi_subscribe(ivc->client,
			CALL_STATUS_IND, isi_call_status_ind_cb,
			ovc);

	if (!isi_call_status_req(ovc, CALL_ID_ALL,
					CALL_STATUS_MODE_ADDR_AND_ORIGIN,
					NULL, NULL))
		DBG("Failed to request call status");

	ofono_voicecall_register(ovc);

	return FALSE;
}

static void isi_voicecall_remove(struct ofono_voicecall *call)
{
	struct isi_voicecall *data = ofono_voicecall_get_data(call);

	if (data) {
		g_isi_client_destroy(data->client);
		g_free(data);
	}
}

static struct ofono_voicecall_driver driver = {
	.name			= "isimodem",
	.probe			= isi_voicecall_probe,
	.remove			= isi_voicecall_remove,
	.dial			= isi_dial,
	.answer			= isi_answer,
	.hangup			= isi_hangup,
	.hold_all_active	= isi_hold_all_active,
	.release_all_held	= isi_release_all_held,
	.set_udub		= isi_set_udub,
	.release_all_active	= isi_release_all_active,
	.release_specific	= isi_release_specific,
	.private_chat		= isi_private_chat,
	.create_multiparty	= isi_create_multiparty,
	.transfer		= isi_transfer,
	.deflect		= isi_deflect,
	.swap_without_accept	= isi_swap_without_accept,
	.send_tones		= isi_send_tones,
};

void isi_voicecall_init()
{
	ofono_voicecall_driver_register(&driver);
}

void isi_voicecall_exit()
{
	ofono_voicecall_driver_unregister(&driver);
}