aboutsummaryrefslogtreecommitdiffstats
path: root/res/res_smdi.c
blob: 3c1ce4c97ae54b270f80c3e871dec43aeb567028 (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
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
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
/*
 * Asterisk -- A telephony toolkit for Linux.
 *
 * Copyright (C) 2005-2008, Digium, Inc.
 *
 * Matthew A. Nicholson <mnicholson@digium.com>
 * Russell Bryant <russell@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*!
 * \file
 * \brief SMDI support for Asterisk.
 * \author Matthew A. Nicholson <mnicholson@digium.com>
 * \author Russell Bryant <russell@digium.com>
 *
 * Here is a useful mailing list post that describes SMDI protocol details:
 * \ref http://lists.digium.com/pipermail/asterisk-dev/2003-June/000884.html
 *
 * \todo This module currently has its own mailbox monitoring thread.  This should
 * be converted to MWI subscriptions and just let the optional global voicemail
 * polling thread handle it.
 */

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include <termios.h>
#include <sys/time.h>
#include <time.h>
#include <ctype.h>

#include "asterisk/module.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"
#define AST_API_MODULE
#include "asterisk/smdi.h"
#include "asterisk/config.h"
#include "asterisk/astobj.h"
#include "asterisk/io.h"
#include "asterisk/stringfields.h"
#include "asterisk/linkedlists.h"
#include "asterisk/app.h"
#include "asterisk/pbx.h"
#include "asterisk/channel.h"

/* Message expiry time in milliseconds */
#define SMDI_MSG_EXPIRY_TIME	30000 /* 30 seconds */

/*** DOCUMENTATION

	<function name="SMDI_MSG_RETRIEVE" language="en_US">
		<synopsis>
			Retrieve an SMDI message.
		</synopsis>
		<syntax>
			<parameter name="smdi port" required="true" />
			<parameter name="search key" required="true" />
			<parameter name="timeout" />
			<parameter name="options">
				<enumlist>
					<enum name="t">
						<para>Instead of searching on the forwarding station, search on the message desk terminal.</para>
					</enum>
					<enum name="n">
						<para>Instead of searching on the forwarding station, search on the message desk number.</para>
					</enum>
				</enumlist>
			</parameter>
		</syntax>
		<description>
			<para>This function is used to retrieve an incoming SMDI message. It returns
			an ID which can be used with the SMDI_MSG() function to access details of
			the message.  Note that this is a destructive function in the sense that
			once an SMDI message is retrieved using this function, it is no longer in
			the global SMDI message queue, and can not be accessed by any other Asterisk
			channels.  The timeout for this function is optional, and the default is
			3 seconds.  When providing a timeout, it should be in milliseconds.
			</para>
			<para>The default search is done on the forwarding station ID. However, if
			you set one of the search key options in the options field, you can change
			this behavior.
			</para>
		</description>
		<see-also>
			<ref type="function">SMDI_MSG</ref>
		</see-also>
	</function>
	<function name="SMDI_MSG" language="en_US">
		<synopsis>
			Retrieve details about an SMDI message.
		</synopsis>
		<syntax>
			<parameter name="message_id" required="true" />
			<parameter name="component" required="true">
				<para>Valid message components are:</para>
				<enumlist>
					<enum name="number">
						<para>The message desk number</para>
					</enum>
					<enum name="terminal">
						<para>The message desk terminal</para>
					</enum>
					<enum name="station">
						<para>The forwarding station</para>
					</enum>
					<enum name="callerid">
						<para>The callerID of the calling party that was forwarded</para>
					</enum>
					<enum name="type">
						<para>The call type.  The value here is the exact character
						that came in on the SMDI link.  Typically, example values
						are:</para>
						<para>Options:</para>
						<enumlist>
							<enum name="D">
								<para>Direct Calls</para>
							</enum>
							<enum name="A">
								<para>Forward All Calls</para>
							</enum>
							<enum name="B">
								<para>Forward Busy Calls</para>
							</enum>
							<enum name="N">
								<para>Forward No Answer Calls</para>
							</enum>
						</enumlist>
					</enum>
				</enumlist>
			</parameter>
		</syntax>
		<description>
			<para>This function is used to access details of an SMDI message that was
			pulled from the incoming SMDI message queue using the SMDI_MSG_RETRIEVE()
			function.</para>
		</description>
		<see-also>
			<ref type="function">SMDI_MSG_RETRIEVE</ref>
		</see-also>
	</function>
 ***/

static const char config_file[] = "smdi.conf";
static int smdi_loaded;

/*! \brief SMDI message desk message queue. */
struct ast_smdi_md_queue {
	ASTOBJ_CONTAINER_COMPONENTS(struct ast_smdi_md_message);
};

/*! \brief SMDI message waiting indicator message queue. */
struct ast_smdi_mwi_queue {
	ASTOBJ_CONTAINER_COMPONENTS(struct ast_smdi_mwi_message);
};

struct ast_smdi_interface {
	ASTOBJ_COMPONENTS_FULL(struct ast_smdi_interface, SMDI_MAX_FILENAME_LEN, 1);
	struct ast_smdi_md_queue md_q;
	ast_mutex_t md_q_lock;
	ast_cond_t md_q_cond;
	struct ast_smdi_mwi_queue mwi_q;
	ast_mutex_t mwi_q_lock;
	ast_cond_t mwi_q_cond;
	FILE *file;
	int fd;
	pthread_t thread;
	struct termios mode;
	int msdstrip;
	long msg_expiry;
};

/*! \brief SMDI interface container. */
static struct ast_smdi_interface_container {
	ASTOBJ_CONTAINER_COMPONENTS(struct ast_smdi_interface);
} smdi_ifaces;

/*! \brief A mapping between an SMDI mailbox ID and an Asterisk mailbox */
struct mailbox_mapping {
	/*! This is the current state of the mailbox.  It is simply on or
	 *  off to indicate if there are messages waiting or not. */
	unsigned int cur_state:1;
	/*! A Pointer to the appropriate SMDI interface */
	struct ast_smdi_interface *iface;
	AST_DECLARE_STRING_FIELDS(
		/*! The Name of the mailbox for the SMDI link. */
		AST_STRING_FIELD(smdi);
		/*! The name of the mailbox on the Asterisk side */
		AST_STRING_FIELD(mailbox);
		/*! The name of the voicemail context in use */
		AST_STRING_FIELD(context);
	);
	AST_LIST_ENTRY(mailbox_mapping) entry;
};

/*! 10 seconds */
#define DEFAULT_POLLING_INTERVAL 10

/*! \brief Data that gets used by the SMDI MWI monitoring thread */
static struct {
	/*! The thread ID */
	pthread_t thread;
	ast_mutex_t lock;
	ast_cond_t cond;
	/*! A list of mailboxes that need to be monitored */
	AST_LIST_HEAD_NOLOCK(, mailbox_mapping) mailbox_mappings;
	/*! Polling Interval for checking mailbox status */
	unsigned int polling_interval;
	/*! Set to 1 to tell the polling thread to stop */
	unsigned int stop:1;
	/*! The time that the last poll began */
	struct timeval last_poll;
} mwi_monitor = {
	.thread = AST_PTHREADT_NULL,
};

static void ast_smdi_interface_destroy(struct ast_smdi_interface *iface)
{
	if (iface->thread != AST_PTHREADT_NULL && iface->thread != AST_PTHREADT_STOP) {
		pthread_cancel(iface->thread);
		pthread_join(iface->thread, NULL);
	}
	
	iface->thread = AST_PTHREADT_STOP;
	
	if (iface->file) 
		fclose(iface->file);
	
	ASTOBJ_CONTAINER_DESTROYALL(&iface->md_q, ast_smdi_md_message_destroy);
	ASTOBJ_CONTAINER_DESTROYALL(&iface->mwi_q, ast_smdi_mwi_message_destroy);
	ASTOBJ_CONTAINER_DESTROY(&iface->md_q);
	ASTOBJ_CONTAINER_DESTROY(&iface->mwi_q);

	ast_mutex_destroy(&iface->md_q_lock);
	ast_cond_destroy(&iface->md_q_cond);

	ast_mutex_destroy(&iface->mwi_q_lock);
	ast_cond_destroy(&iface->mwi_q_cond);

	free(iface);

	ast_module_unref(ast_module_info->self);
}

void AST_OPTIONAL_API_NAME(ast_smdi_interface_unref)(struct ast_smdi_interface *iface)
{
	ASTOBJ_UNREF(iface, ast_smdi_interface_destroy);
}

/*! 
 * \internal
 * \brief Push an SMDI message to the back of an interface's message queue.
 * \param iface a pointer to the interface to use.
 * \param md_msg a pointer to the message to use.
 */
static void ast_smdi_md_message_push(struct ast_smdi_interface *iface, struct ast_smdi_md_message *md_msg)
{
	ast_mutex_lock(&iface->md_q_lock);
	ASTOBJ_CONTAINER_LINK_END(&iface->md_q, md_msg);
	ast_cond_broadcast(&iface->md_q_cond);
	ast_mutex_unlock(&iface->md_q_lock);
}

/*!
 * \internal
 * \brief Push an SMDI message to the back of an interface's message queue.
 * \param iface a pointer to the interface to use.
 * \param mwi_msg a pointer to the message to use.
 */
static void ast_smdi_mwi_message_push(struct ast_smdi_interface *iface, struct ast_smdi_mwi_message *mwi_msg)
{
	ast_mutex_lock(&iface->mwi_q_lock);
	ASTOBJ_CONTAINER_LINK_END(&iface->mwi_q, mwi_msg);
	ast_cond_broadcast(&iface->mwi_q_cond);
	ast_mutex_unlock(&iface->mwi_q_lock);
}

static int smdi_toggle_mwi(struct ast_smdi_interface *iface, const char *mailbox, int on)
{
	FILE *file;
	int i;

	if (!(file = fopen(iface->name, "w"))) {
		ast_log(LOG_ERROR, "Error opening SMDI interface %s (%s) for writing\n", iface->name, strerror(errno));
		return 1;
	}

	ASTOBJ_WRLOCK(iface);

	fprintf(file, "%s:MWI ", on ? "OP" : "RMV");

	for (i = 0; i < iface->msdstrip; i++)
		fprintf(file, "0");

	fprintf(file, "%s!\x04", mailbox);

	fclose(file);

	ASTOBJ_UNLOCK(iface);
	ast_debug(1, "Sent MWI set message for %s on %s\n", mailbox, iface->name);

	return 0;
}

int AST_OPTIONAL_API_NAME(ast_smdi_mwi_set)(struct ast_smdi_interface *iface, const char *mailbox)
{
	return smdi_toggle_mwi(iface, mailbox, 1);
}

int AST_OPTIONAL_API_NAME(ast_smdi_mwi_unset)(struct ast_smdi_interface *iface, const char *mailbox)
{
	return smdi_toggle_mwi(iface, mailbox, 0);
}

void AST_OPTIONAL_API_NAME(ast_smdi_md_message_putback)(struct ast_smdi_interface *iface, struct ast_smdi_md_message *md_msg)
{
	ast_mutex_lock(&iface->md_q_lock);
	ASTOBJ_CONTAINER_LINK_START(&iface->md_q, md_msg);
	ast_cond_broadcast(&iface->md_q_cond);
	ast_mutex_unlock(&iface->md_q_lock);
}

void AST_OPTIONAL_API_NAME(ast_smdi_mwi_message_putback)(struct ast_smdi_interface *iface, struct ast_smdi_mwi_message *mwi_msg)
{
	ast_mutex_lock(&iface->mwi_q_lock);
	ASTOBJ_CONTAINER_LINK_START(&iface->mwi_q, mwi_msg);
	ast_cond_broadcast(&iface->mwi_q_cond);
	ast_mutex_unlock(&iface->mwi_q_lock);
}

enum smdi_message_type {
	SMDI_MWI,
	SMDI_MD,
};

static inline int lock_msg_q(struct ast_smdi_interface *iface, enum smdi_message_type type)
{
	switch (type) {
	case SMDI_MWI:
		return ast_mutex_lock(&iface->mwi_q_lock);
	case SMDI_MD:	
		return ast_mutex_lock(&iface->md_q_lock);
	}
	
	return -1;
}

static inline int unlock_msg_q(struct ast_smdi_interface *iface, enum smdi_message_type type)
{
	switch (type) {
	case SMDI_MWI:
		return ast_mutex_unlock(&iface->mwi_q_lock);
	case SMDI_MD:
		return ast_mutex_unlock(&iface->md_q_lock);
	}

	return -1;
}

static inline void *unlink_from_msg_q(struct ast_smdi_interface *iface, enum smdi_message_type type)
{
	switch (type) {
	case SMDI_MWI:
		return ASTOBJ_CONTAINER_UNLINK_START(&iface->mwi_q);
	case SMDI_MD:
		return ASTOBJ_CONTAINER_UNLINK_START(&iface->md_q);
	}
	return NULL;
}

static inline struct timeval msg_timestamp(void *msg, enum smdi_message_type type)
{
	struct ast_smdi_md_message *md_msg = msg;
	struct ast_smdi_mwi_message *mwi_msg = msg;

	switch (type) {
	case SMDI_MWI:
		return mwi_msg->timestamp;
	case SMDI_MD:
		return md_msg->timestamp;
	}

	return ast_tv(0, 0);
}

static inline void unref_msg(void *msg, enum smdi_message_type type)
{
	struct ast_smdi_md_message *md_msg = msg;
	struct ast_smdi_mwi_message *mwi_msg = msg;

	switch (type) {
	case SMDI_MWI:
		ASTOBJ_UNREF(mwi_msg, ast_smdi_mwi_message_destroy);
		break;
	case SMDI_MD:
		ASTOBJ_UNREF(md_msg, ast_smdi_md_message_destroy);
		break;
	}
}

static void purge_old_messages(struct ast_smdi_interface *iface, enum smdi_message_type type)
{
	struct timeval now = ast_tvnow();
	long elapsed = 0;
	void *msg;
	
	lock_msg_q(iface, type);
	msg = unlink_from_msg_q(iface, type);
	unlock_msg_q(iface, type);

	/* purge old messages */
	while (msg) {
		elapsed = ast_tvdiff_ms(now, msg_timestamp(msg, type));

		if (elapsed > iface->msg_expiry) {
			/* found an expired message */
			unref_msg(msg, type);
			ast_log(LOG_NOTICE, "Purged expired message from %s SMDI %s message queue.  "
				"Message was %ld milliseconds too old.\n",
				iface->name, (type == SMDI_MD) ? "MD" : "MWI", 
				elapsed - iface->msg_expiry);

			lock_msg_q(iface, type);
			msg = unlink_from_msg_q(iface, type);
			unlock_msg_q(iface, type);
		} else {
			/* good message, put it back and return */
			switch (type) {
			case SMDI_MD:
				ast_smdi_md_message_push(iface, msg);
				break;
			case SMDI_MWI:
				ast_smdi_mwi_message_push(iface, msg);
				break;
			}
			unref_msg(msg, type);
			break;
		}
	}
}

static void *smdi_msg_pop(struct ast_smdi_interface *iface, enum smdi_message_type type)
{
	void *msg;

	purge_old_messages(iface, type);

	lock_msg_q(iface, type);
	msg = unlink_from_msg_q(iface, type);
	unlock_msg_q(iface, type);

	return msg;
}

enum {
	OPT_SEARCH_TERMINAL = (1 << 0),
	OPT_SEARCH_NUMBER   = (1 << 1),
};

static void *smdi_msg_find(struct ast_smdi_interface *iface,
	enum smdi_message_type type, const char *search_key, struct ast_flags options)
{
	void *msg = NULL;

	purge_old_messages(iface, type);

	switch (type) {
	case SMDI_MD:
		if (ast_strlen_zero(search_key)) {
			struct ast_smdi_md_message *md_msg = NULL;

			/* No search key provided (the code from chan_dahdi does this).
			 * Just pop the top message off of the queue. */

			ASTOBJ_CONTAINER_TRAVERSE(&iface->md_q, !md_msg, do {
				md_msg = ASTOBJ_REF(iterator);
			} while (0); );

			msg = md_msg;
		} else if (ast_test_flag(&options, OPT_SEARCH_TERMINAL)) {
			struct ast_smdi_md_message *md_msg = NULL;

			/* Searching by the message desk terminal */

			ASTOBJ_CONTAINER_TRAVERSE(&iface->md_q, !md_msg, do {
				if (!strcasecmp(iterator->mesg_desk_term, search_key))
					md_msg = ASTOBJ_REF(iterator);
			} while (0); );

			msg = md_msg;
		} else if (ast_test_flag(&options, OPT_SEARCH_NUMBER)) {
			struct ast_smdi_md_message *md_msg = NULL;

			/* Searching by the message desk number */

			ASTOBJ_CONTAINER_TRAVERSE(&iface->md_q, !md_msg, do {
				if (!strcasecmp(iterator->mesg_desk_num, search_key))
					md_msg = ASTOBJ_REF(iterator);
			} while (0); );

			msg = md_msg;
		} else {
			/* Searching by the forwarding station */
			msg = ASTOBJ_CONTAINER_FIND(&iface->md_q, search_key);
		}
		break;
	case SMDI_MWI:
		if (ast_strlen_zero(search_key)) {
			struct ast_smdi_mwi_message *mwi_msg = NULL;

			/* No search key provided (the code from chan_dahdi does this).
			 * Just pop the top message off of the queue. */

			ASTOBJ_CONTAINER_TRAVERSE(&iface->mwi_q, !mwi_msg, do {
				mwi_msg = ASTOBJ_REF(iterator);
			} while (0); );

			msg = mwi_msg;
		} else {
			msg = ASTOBJ_CONTAINER_FIND(&iface->mwi_q, search_key);
		}
		break;
	}

	return msg;
}

static void *smdi_message_wait(struct ast_smdi_interface *iface, int timeout, 
	enum smdi_message_type type, const char *search_key, struct ast_flags options)
{
	struct timeval start;
	long diff = 0;
	void *msg;
	ast_cond_t *cond = NULL;
	ast_mutex_t *lock = NULL;

	switch (type) {
	case SMDI_MWI:
		cond = &iface->mwi_q_cond;
		lock = &iface->mwi_q_lock;
		break;
	case SMDI_MD:
		cond = &iface->md_q_cond;
		lock = &iface->md_q_lock;
		break;
	}

	start = ast_tvnow();

	while (diff < timeout) {
		struct timespec ts = { 0, };
		struct timeval wait;

		lock_msg_q(iface, type);

		if ((msg = smdi_msg_find(iface, type, search_key, options))) {
			unlock_msg_q(iface, type);
			return msg;
		}

		wait = ast_tvadd(start, ast_tv(0, timeout));
		ts.tv_sec = wait.tv_sec;
		ts.tv_nsec = wait.tv_usec * 1000;

		/* If there were no messages in the queue, then go to sleep until one
		 * arrives. */

		ast_cond_timedwait(cond, lock, &ts);

		if ((msg = smdi_msg_find(iface, type, search_key, options))) {
			unlock_msg_q(iface, type);
			return msg;
		}

		unlock_msg_q(iface, type);

		/* check timeout */
		diff = ast_tvdiff_ms(ast_tvnow(), start);
	}

	return NULL;
}

struct ast_smdi_md_message * AST_OPTIONAL_API_NAME(ast_smdi_md_message_pop)(struct ast_smdi_interface *iface)
{
	return smdi_msg_pop(iface, SMDI_MD);
}

struct ast_smdi_md_message * AST_OPTIONAL_API_NAME(ast_smdi_md_message_wait)(struct ast_smdi_interface *iface, int timeout)
{
	struct ast_flags options = { 0 };
	return smdi_message_wait(iface, timeout, SMDI_MD, NULL, options);
}

struct ast_smdi_mwi_message * AST_OPTIONAL_API_NAME(ast_smdi_mwi_message_pop)(struct ast_smdi_interface *iface)
{
	return smdi_msg_pop(iface, SMDI_MWI);
}

struct ast_smdi_mwi_message * AST_OPTIONAL_API_NAME(ast_smdi_mwi_message_wait)(struct ast_smdi_interface *iface, int timeout)
{
	struct ast_flags options = { 0 };
	return smdi_message_wait(iface, timeout, SMDI_MWI, NULL, options);
}

struct ast_smdi_mwi_message * AST_OPTIONAL_API_NAME(ast_smdi_mwi_message_wait_station)(struct ast_smdi_interface *iface, int timeout,
	const char *station)
{
	struct ast_flags options = { 0 };
	return smdi_message_wait(iface, timeout, SMDI_MWI, station, options);
}

struct ast_smdi_interface * AST_OPTIONAL_API_NAME(ast_smdi_interface_find)(const char *iface_name)
{
	return (ASTOBJ_CONTAINER_FIND(&smdi_ifaces, iface_name));
}

/*! 
 * \internal
 * \brief Read an SMDI message.
 *
 * \param iface_p the SMDI interface to read from.
 *
 * This function loops and reads from and SMDI interface.  It must be stopped
 * using pthread_cancel().
 */
static void *smdi_read(void *iface_p)
{
	struct ast_smdi_interface *iface = iface_p;
	struct ast_smdi_md_message *md_msg;
	struct ast_smdi_mwi_message *mwi_msg;
	char c = '\0';
	char *cp = NULL;
	int i;
	int start = 0;

	/* read an smdi message */
	while ((c = fgetc(iface->file))) {

		/* check if this is the start of a message */
		if (!start) {
			if (c == 'M') {
				ast_debug(1, "Read an 'M' to start an SMDI message\n");
				start = 1;
			}
			continue;
		}

		if (c == 'D') { /* MD message */
			start = 0;

			ast_debug(1, "Read a 'D' ... it's an MD message.\n");

			if (!(md_msg = ast_calloc(1, sizeof(*md_msg)))) {
				ASTOBJ_UNREF(iface, ast_smdi_interface_destroy);
				return NULL;
			}

			ASTOBJ_INIT(md_msg);

			/* read the message desk number */
			for (i = 0; i < sizeof(md_msg->mesg_desk_num) - 1; i++) {
				md_msg->mesg_desk_num[i] = fgetc(iface->file);
				ast_debug(1, "Read a '%c'\n", md_msg->mesg_desk_num[i]);
			}

			md_msg->mesg_desk_num[sizeof(md_msg->mesg_desk_num) - 1] = '\0';

			ast_debug(1, "The message desk number is '%s'\n", md_msg->mesg_desk_num);

			/* read the message desk terminal number */
			for (i = 0; i < sizeof(md_msg->mesg_desk_term) - 1; i++) {
				md_msg->mesg_desk_term[i] = fgetc(iface->file);
				ast_debug(1, "Read a '%c'\n", md_msg->mesg_desk_term[i]);
			}

			md_msg->mesg_desk_term[sizeof(md_msg->mesg_desk_term) - 1] = '\0';

			ast_debug(1, "The message desk terminal is '%s'\n", md_msg->mesg_desk_term);

			/* read the message type */
			md_msg->type = fgetc(iface->file);

			ast_debug(1, "Message type is '%c'\n", md_msg->type);

			/* read the forwarding station number (may be blank) */
			cp = &md_msg->fwd_st[0];
			for (i = 0; i < sizeof(md_msg->fwd_st) - 1; i++) {
				if ((c = fgetc(iface->file)) == ' ') {
					*cp = '\0';
					ast_debug(1, "Read a space, done looking for the forwarding station\n");
					break;
				}

				/* store c in md_msg->fwd_st */
				if (i >= iface->msdstrip) {
					ast_debug(1, "Read a '%c' and stored it in the forwarding station buffer\n", c);
					*cp++ = c;
				} else {
					ast_debug(1, "Read a '%c', but didn't store it in the fwd station buffer, because of the msdstrip setting (%d < %d)\n", c, i, iface->msdstrip);
				}
			}

			/* make sure the value is null terminated, even if this truncates it */
			md_msg->fwd_st[sizeof(md_msg->fwd_st) - 1] = '\0';
			cp = NULL;

			ast_debug(1, "The forwarding station is '%s'\n", md_msg->fwd_st);

			/* Put the fwd_st in the name field so that we can use ASTOBJ_FIND to look
			 * up a message on this field */
			ast_copy_string(md_msg->name, md_msg->fwd_st, sizeof(md_msg->name));

			/* read the calling station number (may be blank) */
			cp = &md_msg->calling_st[0];
			for (i = 0; i < sizeof(md_msg->calling_st) - 1; i++) {
				if (!isdigit((c = fgetc(iface->file)))) {
					*cp = '\0';
					ast_debug(1, "Read a '%c', but didn't store it in the calling station buffer because it's not a digit\n", c);
					if (c == ' ') {
						/* Don't break on a space.  We may read the space before the calling station
						 * here if the forwarding station buffer filled up. */
						i--; /* We're still on the same character */
						continue;
					}
					break;
				}

				/* store c in md_msg->calling_st */
				if (i >= iface->msdstrip) {
					ast_debug(1, "Read a '%c' and stored it in the calling station buffer\n", c);
					*cp++ = c;
				} else {
					ast_debug(1, "Read a '%c', but didn't store it in the calling station buffer, because of the msdstrip setting (%d < %d)\n", c, i, iface->msdstrip);
				}
			}

			/* make sure the value is null terminated, even if this truncates it */
			md_msg->calling_st[sizeof(md_msg->calling_st) - 1] = '\0';
			cp = NULL;

			ast_debug(1, "The calling station is '%s'\n", md_msg->calling_st);

			/* add the message to the message queue */
			md_msg->timestamp = ast_tvnow();
			ast_smdi_md_message_push(iface, md_msg);
			ast_debug(1, "Received SMDI MD message on %s\n", iface->name);

			ASTOBJ_UNREF(md_msg, ast_smdi_md_message_destroy);

		} else if (c == 'W') { /* MWI message */
			start = 0;

			ast_debug(1, "Read a 'W', it's an MWI message. (No more debug coming for MWI messages)\n");

			if (!(mwi_msg = ast_calloc(1, sizeof(*mwi_msg)))) {
				ASTOBJ_UNREF(iface,ast_smdi_interface_destroy);
				return NULL;
			}

			ASTOBJ_INIT(mwi_msg);

			/* discard the 'I' (from 'MWI') */
			fgetc(iface->file);
			
			/* read the forwarding station number (may be blank) */
			cp = &mwi_msg->fwd_st[0];
			for (i = 0; i < sizeof(mwi_msg->fwd_st) - 1; i++) {
				if ((c = fgetc(iface->file)) == ' ') {
					*cp = '\0';
					break;
				}

				/* store c in md_msg->fwd_st */
				if (i >= iface->msdstrip)
					*cp++ = c;
			}

			/* make sure the station number is null terminated, even if this will truncate it */
			mwi_msg->fwd_st[sizeof(mwi_msg->fwd_st) - 1] = '\0';
			cp = NULL;

			/* Put the fwd_st in the name field so that we can use ASTOBJ_FIND to look
			 * up a message on this field */
			ast_copy_string(mwi_msg->name, mwi_msg->fwd_st, sizeof(mwi_msg->name));

			/* read the mwi failure cause */
			for (i = 0; i < sizeof(mwi_msg->cause) - 1; i++)
				mwi_msg->cause[i] = fgetc(iface->file);

			mwi_msg->cause[sizeof(mwi_msg->cause) - 1] = '\0';

			/* add the message to the message queue */
			mwi_msg->timestamp = ast_tvnow();
			ast_smdi_mwi_message_push(iface, mwi_msg);
			ast_debug(1, "Received SMDI MWI message on %s\n", iface->name);

			ASTOBJ_UNREF(mwi_msg, ast_smdi_mwi_message_destroy);
		} else {
			ast_log(LOG_ERROR, "Unknown SMDI message type received on %s (M%c).\n", iface->name, c);
			start = 0;
		}
	}

	ast_log(LOG_ERROR, "Error reading from SMDI interface %s, stopping listener thread\n", iface->name);
	ASTOBJ_UNREF(iface,ast_smdi_interface_destroy);
	return NULL;
}

void AST_OPTIONAL_API_NAME(ast_smdi_md_message_destroy)(struct ast_smdi_md_message *msg)
{
	ast_free(msg);
}

void AST_OPTIONAL_API_NAME(ast_smdi_mwi_message_destroy)(struct ast_smdi_mwi_message *msg)
{
	ast_free(msg);
}

static void destroy_mailbox_mapping(struct mailbox_mapping *mm)
{
	ast_string_field_free_memory(mm);
	ASTOBJ_UNREF(mm->iface, ast_smdi_interface_destroy);
	free(mm);
}

static void destroy_all_mailbox_mappings(void)
{
	struct mailbox_mapping *mm;

	ast_mutex_lock(&mwi_monitor.lock);
	while ((mm = AST_LIST_REMOVE_HEAD(&mwi_monitor.mailbox_mappings, entry)))
		destroy_mailbox_mapping(mm);
	ast_mutex_unlock(&mwi_monitor.lock);
}

static void append_mailbox_mapping(struct ast_variable *var, struct ast_smdi_interface *iface)
{
	struct mailbox_mapping *mm;
	char *mailbox, *context;

	if (!(mm = ast_calloc_with_stringfields(1, struct mailbox_mapping, 32)))
		return;

	ast_string_field_set(mm, smdi, var->name);

	context = ast_strdupa(var->value);
	mailbox = strsep(&context, "@");
	if (ast_strlen_zero(context))
		context = "default";

	ast_string_field_set(mm, mailbox, mailbox);
	ast_string_field_set(mm, context, context);

	mm->iface = ASTOBJ_REF(iface);

	ast_mutex_lock(&mwi_monitor.lock);
	AST_LIST_INSERT_TAIL(&mwi_monitor.mailbox_mappings, mm, entry);
	ast_mutex_unlock(&mwi_monitor.lock);
}

/*!
 * \note Called with the mwi_monitor.lock locked
 */
static void poll_mailbox(struct mailbox_mapping *mm)
{
	char buf[1024];
	unsigned int state;

	snprintf(buf, sizeof(buf), "%s@%s", mm->mailbox, mm->context);

	state = !!ast_app_has_voicemail(mm->mailbox, NULL);

	if (state != mm->cur_state) {
		if (state)
			ast_smdi_mwi_set(mm->iface, mm->smdi);
		else
			ast_smdi_mwi_unset(mm->iface, mm->smdi);

		mm->cur_state = state;
	}
}

static void *mwi_monitor_handler(void *data)
{
	while (!mwi_monitor.stop) {
		struct timespec ts = { 0, };
		struct timeval polltime;
		struct mailbox_mapping *mm;

		ast_mutex_lock(&mwi_monitor.lock);

		mwi_monitor.last_poll = ast_tvnow();

		AST_LIST_TRAVERSE(&mwi_monitor.mailbox_mappings, mm, entry)
			poll_mailbox(mm);

		/* Sleep up to the configured polling interval.  Allow unload_module()
		 * to signal us to wake up and exit. */
		polltime = ast_tvadd(mwi_monitor.last_poll, ast_tv(mwi_monitor.polling_interval, 0));
		ts.tv_sec = polltime.tv_sec;
		ts.tv_nsec = polltime.tv_usec * 1000;
		ast_cond_timedwait(&mwi_monitor.cond, &mwi_monitor.lock, &ts);

		ast_mutex_unlock(&mwi_monitor.lock);
	}

	return NULL;
}

static struct ast_smdi_interface *alloc_smdi_interface(void)
{
	struct ast_smdi_interface *iface;

	if (!(iface = ast_calloc(1, sizeof(*iface))))
		return NULL;

	ASTOBJ_INIT(iface);
	ASTOBJ_CONTAINER_INIT(&iface->md_q);
	ASTOBJ_CONTAINER_INIT(&iface->mwi_q);

	ast_mutex_init(&iface->md_q_lock);
	ast_cond_init(&iface->md_q_cond, NULL);

	ast_mutex_init(&iface->mwi_q_lock);
	ast_cond_init(&iface->mwi_q_cond, NULL);

	return iface;
}

/*!
 * \internal
 * \brief Load and reload SMDI configuration.
 * \param reload this should be 1 if we are reloading and 0 if not.
 *
 * This function loads/reloads the SMDI configuration and starts and stops
 * interfaces accordingly.
 *
 * \return zero on success, -1 on failure, and 1 if no smdi interfaces were started.
 */
static int smdi_load(int reload)
{
	struct ast_config *conf;
	struct ast_variable *v;
	struct ast_smdi_interface *iface = NULL;
	struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
	int res = 0;

	/* Config options */
	speed_t baud_rate = B9600;     /* 9600 baud rate */
	tcflag_t paritybit = PARENB;   /* even parity checking */
	tcflag_t charsize = CS7;       /* seven bit characters */
	int stopbits = 0;              /* One stop bit */
	
	int msdstrip = 0;              /* strip zero digits */
	long msg_expiry = SMDI_MSG_EXPIRY_TIME;

	if (!(conf = ast_config_load(config_file, config_flags)) || conf == CONFIG_STATUS_FILEINVALID) {
		if (reload)
			ast_log(LOG_NOTICE, "Unable to reload config %s: SMDI untouched\n", config_file);
		else
			ast_log(LOG_NOTICE, "Unable to load config %s: SMDI disabled\n", config_file);
		return 1;
	} else if (conf == CONFIG_STATUS_FILEUNCHANGED)
		return 0;

	/* Mark all interfaces that we are listening on.  We will unmark them
	 * as we find them in the config file, this way we know any interfaces
	 * still marked after we have finished parsing the config file should
	 * be stopped.
	 */
	if (reload)
		ASTOBJ_CONTAINER_MARKALL(&smdi_ifaces);

	for (v = ast_variable_browse(conf, "interfaces"); v; v = v->next) {
		if (!strcasecmp(v->name, "baudrate")) {
			if (!strcasecmp(v->value, "9600"))
				baud_rate = B9600;
			else if (!strcasecmp(v->value, "4800"))
				baud_rate = B4800;
			else if (!strcasecmp(v->value, "2400"))
				baud_rate = B2400;
			else if (!strcasecmp(v->value, "1200"))
				baud_rate = B1200;
			else {
				ast_log(LOG_NOTICE, "Invalid baud rate '%s' specified in %s (line %d), using default\n", v->value, config_file, v->lineno);
				baud_rate = B9600;
			}
		} else if (!strcasecmp(v->name, "msdstrip")) {
			if (!sscanf(v->value, "%30d", &msdstrip)) {
				ast_log(LOG_NOTICE, "Invalid msdstrip value in %s (line %d), using default\n", config_file, v->lineno);
				msdstrip = 0;
			} else if (0 > msdstrip || msdstrip > 9) {
				ast_log(LOG_NOTICE, "Invalid msdstrip value in %s (line %d), using default\n", config_file, v->lineno);
				msdstrip = 0;
			}
		} else if (!strcasecmp(v->name, "msgexpirytime")) {
			if (!sscanf(v->value, "%30ld", &msg_expiry)) {
				ast_log(LOG_NOTICE, "Invalid msgexpirytime value in %s (line %d), using default\n", config_file, v->lineno);
				msg_expiry = SMDI_MSG_EXPIRY_TIME;
			}
		} else if (!strcasecmp(v->name, "paritybit")) {
			if (!strcasecmp(v->value, "even"))
				paritybit = PARENB;
			else if (!strcasecmp(v->value, "odd"))
				paritybit = PARENB | PARODD;
			else if (!strcasecmp(v->value, "none"))
				paritybit = ~PARENB;
			else {
				ast_log(LOG_NOTICE, "Invalid parity bit setting in %s (line %d), using default\n", config_file, v->lineno);
				paritybit = PARENB;
			}
		} else if (!strcasecmp(v->name, "charsize")) {
			if (!strcasecmp(v->value, "7"))
				charsize = CS7;
			else if (!strcasecmp(v->value, "8"))
				charsize = CS8;
			else {
				ast_log(LOG_NOTICE, "Invalid character size setting in %s (line %d), using default\n", config_file, v->lineno);
				charsize = CS7;
			}
		} else if (!strcasecmp(v->name, "twostopbits")) {
			stopbits = ast_true(v->name);
		} else if (!strcasecmp(v->name, "smdiport")) {
			if (reload) {
				/* we are reloading, check if we are already
				 * monitoring this interface, if we are we do
				 * not want to start it again.  This also has
				 * the side effect of not updating different
				 * setting for the serial port, but it should
				 * be trivial to rewrite this section so that
				 * options on the port are changed without
				 * restarting the interface.  Or the interface
				 * could be restarted with out emptying the
				 * queue. */
				if ((iface = ASTOBJ_CONTAINER_FIND(&smdi_ifaces, v->value))) {
					ast_log(LOG_NOTICE, "SMDI interface %s already running, not restarting\n", iface->name);
					ASTOBJ_UNMARK(iface);
					ASTOBJ_UNREF(iface, ast_smdi_interface_destroy);
					continue;
				}
			}
			
			if (!(iface = alloc_smdi_interface()))
				continue;

			ast_copy_string(iface->name, v->value, sizeof(iface->name));

			iface->thread = AST_PTHREADT_NULL;

			if (!(iface->file = fopen(iface->name, "r"))) {
				ast_log(LOG_ERROR, "Error opening SMDI interface %s (%s)\n", iface->name, strerror(errno));
				ASTOBJ_UNREF(iface, ast_smdi_interface_destroy);
				continue;
			}

			iface->fd = fileno(iface->file);

			/* Set the proper attributes for our serial port. */

			/* get the current attributes from the port */
			if (tcgetattr(iface->fd, &iface->mode)) {
				ast_log(LOG_ERROR, "Error getting atributes of %s (%s)\n", iface->name, strerror(errno));
				ASTOBJ_UNREF(iface, ast_smdi_interface_destroy);
				continue;
			}

			/* set the desired speed */
			if (cfsetispeed(&iface->mode, baud_rate) || cfsetospeed(&iface->mode, baud_rate)) {
				ast_log(LOG_ERROR, "Error setting baud rate on %s (%s)\n", iface->name, strerror(errno));
				ASTOBJ_UNREF(iface, ast_smdi_interface_destroy);
				continue;
			}
			
			/* set the stop bits */
			if (stopbits)
				iface->mode.c_cflag = iface->mode.c_cflag | CSTOPB;   /* set two stop bits */
			else
				iface->mode.c_cflag = iface->mode.c_cflag & ~CSTOPB;  /* set one stop bit */
			
			/* set the parity */
			iface->mode.c_cflag = (iface->mode.c_cflag & ~PARENB & ~PARODD) | paritybit;
			
			/* set the character size */
			iface->mode.c_cflag = (iface->mode.c_cflag & ~CSIZE) | charsize;
			
			/* commit the desired attributes */
			if (tcsetattr(iface->fd, TCSAFLUSH, &iface->mode)) {
				ast_log(LOG_ERROR, "Error setting attributes on %s (%s)\n", iface->name, strerror(errno));
				ASTOBJ_UNREF(iface, ast_smdi_interface_destroy);
				continue;
			}

			/* set the msdstrip */
			iface->msdstrip = msdstrip;

			/* set the message expiry time */
			iface->msg_expiry = msg_expiry;

			/* start the listener thread */
			ast_verb(3, "Starting SMDI monitor thread for %s\n", iface->name);
			if (ast_pthread_create_background(&iface->thread, NULL, smdi_read, iface)) {
				ast_log(LOG_ERROR, "Error starting SMDI monitor thread for %s\n", iface->name);
				ASTOBJ_UNREF(iface, ast_smdi_interface_destroy);
				continue;
			}

			ASTOBJ_CONTAINER_LINK(&smdi_ifaces, iface);
			ASTOBJ_UNREF(iface, ast_smdi_interface_destroy);
			ast_module_ref(ast_module_info->self);
		} else {
			ast_log(LOG_NOTICE, "Ignoring unknown option %s in %s\n", v->name, config_file);
		}
	}

	destroy_all_mailbox_mappings();
	mwi_monitor.polling_interval = DEFAULT_POLLING_INTERVAL;
	
	iface = NULL;

	for (v = ast_variable_browse(conf, "mailboxes"); v; v = v->next) {
		if (!strcasecmp(v->name, "smdiport")) {
			if (iface)
				ASTOBJ_UNREF(iface, ast_smdi_interface_destroy);

			if (!(iface = ASTOBJ_CONTAINER_FIND(&smdi_ifaces, v->value))) {
				ast_log(LOG_NOTICE, "SMDI interface %s not found\n", v->value);
				continue;
			}
		} else if (!strcasecmp(v->name, "pollinginterval")) {
			if (sscanf(v->value, "%30u", &mwi_monitor.polling_interval) != 1) {
				ast_log(LOG_ERROR, "Invalid value for pollinginterval: %s\n", v->value);
				mwi_monitor.polling_interval = DEFAULT_POLLING_INTERVAL;
			}
		} else {
			if (!iface) {
				ast_log(LOG_ERROR, "Mailbox mapping ignored, no valid SMDI interface specified in mailboxes section\n");
				continue;
			}
			append_mailbox_mapping(v, iface);
		}
	}

	if (iface)
		ASTOBJ_UNREF(iface, ast_smdi_interface_destroy);

	ast_config_destroy(conf);
	
	if (!AST_LIST_EMPTY(&mwi_monitor.mailbox_mappings) && mwi_monitor.thread == AST_PTHREADT_NULL
		&& ast_pthread_create_background(&mwi_monitor.thread, NULL, mwi_monitor_handler, NULL)) {
		ast_log(LOG_ERROR, "Failed to start MWI monitoring thread.  This module will not operate.\n");
		return AST_MODULE_LOAD_FAILURE;
	}

	/* Prune any interfaces we should no longer monitor. */
	if (reload)
		ASTOBJ_CONTAINER_PRUNE_MARKED(&smdi_ifaces, ast_smdi_interface_destroy);
	
	ASTOBJ_CONTAINER_RDLOCK(&smdi_ifaces);
	/* TODO: this is bad, we need an ASTOBJ method for this! */
	if (!smdi_ifaces.head)
		res = 1;
	ASTOBJ_CONTAINER_UNLOCK(&smdi_ifaces);
	
	return res;
}

struct smdi_msg_datastore {
	unsigned int id;
	struct ast_smdi_interface *iface;
	struct ast_smdi_md_message *md_msg;
};

static void smdi_msg_datastore_destroy(void *data)
{
	struct smdi_msg_datastore *smd = data;

	if (smd->iface)
		ASTOBJ_UNREF(smd->iface, ast_smdi_interface_destroy);

	if (smd->md_msg)
		ASTOBJ_UNREF(smd->md_msg, ast_smdi_md_message_destroy);

	free(smd);
}

static const struct ast_datastore_info smdi_msg_datastore_info = {
	.type = "SMDIMSG",
	.destroy = smdi_msg_datastore_destroy,
};

static int smdi_msg_id;

/*! In milliseconds */
#define SMDI_RETRIEVE_TIMEOUT_DEFAULT 3000

AST_APP_OPTIONS(smdi_msg_ret_options, BEGIN_OPTIONS
	AST_APP_OPTION('t', OPT_SEARCH_TERMINAL),
	AST_APP_OPTION('n', OPT_SEARCH_NUMBER),
END_OPTIONS );

static int smdi_msg_retrieve_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
	struct ast_module_user *u;
	AST_DECLARE_APP_ARGS(args,
		AST_APP_ARG(port);
		AST_APP_ARG(search_key);
		AST_APP_ARG(timeout);
		AST_APP_ARG(options);
	);
	struct ast_flags options = { 0 };
	unsigned int timeout = SMDI_RETRIEVE_TIMEOUT_DEFAULT;
	int res = -1;
	char *parse = NULL;
	struct smdi_msg_datastore *smd = NULL;
	struct ast_datastore *datastore = NULL;
	struct ast_smdi_interface *iface = NULL;
	struct ast_smdi_md_message *md_msg = NULL;

	u = ast_module_user_add(chan);

	if (ast_strlen_zero(data)) {
		ast_log(LOG_ERROR, "SMDI_MSG_RETRIEVE requires an argument\n");
		goto return_error;
	}

	if (!chan) {
		ast_log(LOG_ERROR, "SMDI_MSG_RETRIEVE must be used with a channel\n");
		goto return_error;
	}

	ast_autoservice_start(chan);

	parse = ast_strdupa(data);
	AST_STANDARD_APP_ARGS(args, parse);

	if (ast_strlen_zero(args.port) || ast_strlen_zero(args.search_key)) {
		ast_log(LOG_ERROR, "Invalid arguments provided to SMDI_MSG_RETRIEVE\n");
		goto return_error;
	}

	if (!(iface = ast_smdi_interface_find(args.port))) {
		ast_log(LOG_ERROR, "SMDI port '%s' not found\n", args.port);
		goto return_error;
	}

	if (!ast_strlen_zero(args.options)) {
		ast_app_parse_options(smdi_msg_ret_options, &options, NULL, args.options);
	}

	if (!ast_strlen_zero(args.timeout)) {
		if (sscanf(args.timeout, "%30u", &timeout) != 1) {
			ast_log(LOG_ERROR, "'%s' is not a valid timeout\n", args.timeout);
			timeout = SMDI_RETRIEVE_TIMEOUT_DEFAULT;
		}
	}

	if (!(md_msg = smdi_message_wait(iface, timeout, SMDI_MD, args.search_key, options))) {
		ast_log(LOG_WARNING, "No SMDI message retrieved for search key '%s' after "
			"waiting %u ms.\n", args.search_key, timeout);
		goto return_error;
	}

	if (!(smd = ast_calloc(1, sizeof(*smd))))
		goto return_error;

	smd->iface = ASTOBJ_REF(iface);
	smd->md_msg = ASTOBJ_REF(md_msg);
	smd->id = ast_atomic_fetchadd_int((int *) &smdi_msg_id, 1);
	snprintf(buf, len, "%u", smd->id);

	if (!(datastore = ast_datastore_alloc(&smdi_msg_datastore_info, buf)))
		goto return_error;

	datastore->data = smd;

	ast_channel_lock(chan);
	ast_channel_datastore_add(chan, datastore);
	ast_channel_unlock(chan);

	res = 0;

return_error:
	if (iface)
		ASTOBJ_UNREF(iface, ast_smdi_interface_destroy);

	if (md_msg)
		ASTOBJ_UNREF(md_msg, ast_smdi_md_message_destroy);

	if (smd && !datastore)
		smdi_msg_datastore_destroy(smd);

	if (parse)
		ast_autoservice_stop(chan);

	ast_module_user_remove(u);

	return res;
}

static int smdi_msg_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
	struct ast_module_user *u;
	int res = -1;
	AST_DECLARE_APP_ARGS(args,
		AST_APP_ARG(id);
		AST_APP_ARG(component);
	);
	char *parse;
	struct ast_datastore *datastore = NULL;
	struct smdi_msg_datastore *smd = NULL;

	u = ast_module_user_add(chan);

	if (!chan) {
		ast_log(LOG_ERROR, "SMDI_MSG can not be called without a channel\n");
		goto return_error;
	}

	if (ast_strlen_zero(data)) {
		ast_log(LOG_WARNING, "SMDI_MSG requires an argument\n");
		goto return_error;
	}

	parse = ast_strdupa(data);
	AST_STANDARD_APP_ARGS(args, parse);

	if (ast_strlen_zero(args.id)) {
		ast_log(LOG_WARNING, "ID must be supplied to SMDI_MSG\n");
		goto return_error;
	}

	if (ast_strlen_zero(args.component)) {
		ast_log(LOG_WARNING, "ID must be supplied to SMDI_MSG\n");
		goto return_error;
	}

	ast_channel_lock(chan);
	datastore = ast_channel_datastore_find(chan, &smdi_msg_datastore_info, args.id);
	ast_channel_unlock(chan);
	
	if (!datastore) {
		ast_log(LOG_WARNING, "No SMDI message found for message ID '%s'\n", args.id);
		goto return_error;
	}

	smd = datastore->data;

	if (!strcasecmp(args.component, "number")) {
		ast_copy_string(buf, smd->md_msg->mesg_desk_num, len);
	} else if (!strcasecmp(args.component, "terminal")) {
		ast_copy_string(buf, smd->md_msg->mesg_desk_term, len);
	} else if (!strcasecmp(args.component, "station")) {
		ast_copy_string(buf, smd->md_msg->fwd_st, len);
	} else if (!strcasecmp(args.component, "callerid")) {
		ast_copy_string(buf, smd->md_msg->calling_st, len);
	} else if (!strcasecmp(args.component, "type")) {
		snprintf(buf, len, "%c", smd->md_msg->type);
	} else {
		ast_log(LOG_ERROR, "'%s' is not a valid message component for SMDI_MSG\n",
			args.component);
		goto return_error;
	}

	res = 0;

return_error:
	ast_module_user_remove(u);

	return res;
}

static struct ast_custom_function smdi_msg_retrieve_function = {
	.name = "SMDI_MSG_RETRIEVE",
	.read = smdi_msg_retrieve_read,
};

static struct ast_custom_function smdi_msg_function = {
	.name = "SMDI_MSG",
	.read = smdi_msg_read,
};

static int _unload_module(int fromload);

static int load_module(void)
{
	int res;
	smdi_loaded = 1;

	/* initialize our containers */
	memset(&smdi_ifaces, 0, sizeof(smdi_ifaces));
	ASTOBJ_CONTAINER_INIT(&smdi_ifaces);

	ast_mutex_init(&mwi_monitor.lock);
	ast_cond_init(&mwi_monitor.cond, NULL);

	/* load the config and start the listener threads*/
	res = smdi_load(0);
	if (res < 0) {
		_unload_module(1);
		return res;
	} else if (res == 1) {
		_unload_module(1);
		ast_log(LOG_NOTICE, "No SMDI interfaces are available to listen on, not starting SMDI listener.\n");
		return AST_MODULE_LOAD_DECLINE;
	}

	ast_custom_function_register(&smdi_msg_retrieve_function);
	ast_custom_function_register(&smdi_msg_function);

	return AST_MODULE_LOAD_SUCCESS;
}

static int _unload_module(int fromload)
{
	if (!smdi_loaded) {
		return 0;
	}

	/* this destructor stops any running smdi_read threads */
	ASTOBJ_CONTAINER_DESTROYALL(&smdi_ifaces, ast_smdi_interface_destroy);
	ASTOBJ_CONTAINER_DESTROY(&smdi_ifaces);

	destroy_all_mailbox_mappings();

	ast_mutex_lock(&mwi_monitor.lock);
	mwi_monitor.stop = 1;
	ast_cond_signal(&mwi_monitor.cond);
	ast_mutex_unlock(&mwi_monitor.lock);

	if (mwi_monitor.thread != AST_PTHREADT_NULL) {
		pthread_join(mwi_monitor.thread, NULL);
	}

	if (!fromload) {
		ast_custom_function_unregister(&smdi_msg_retrieve_function);
		ast_custom_function_unregister(&smdi_msg_function);
	}

	smdi_loaded = 0;
	return 0;
}

static int unload_module(void)
{
	return _unload_module(0);
}

static int reload(void)
{
	int res;

	res = smdi_load(1);

	if (res < 0) {
		return res;
	} else if (res == 1) {
		ast_log(LOG_WARNING, "No SMDI interfaces were specified to listen on, not starting SDMI listener.\n");
		return 0;
	} else
		return 0;
}

AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Simplified Message Desk Interface (SMDI) Resource",
		.load = load_module,
		.unload = unload_module,
		.reload = reload,
		.load_pri = AST_MODPRI_CHANNEL_DEPEND,
	       );