| 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 |
#include "asterisk.h" |
| 48 |
|
| 49 |
#include <termios.h> |
| 50 |
#include <sys/time.h> |
| 51 |
#include <time.h> |
| 52 |
#include <ctype.h> |
| 53 |
|
| 54 |
#include "asterisk/module.h" |
| 55 |
#include "asterisk/lock.h" |
| 56 |
#include "asterisk/utils.h" |
| 57 |
#define AST_API_MODULE |
| 58 |
#include "asterisk/smdi.h" |
| 59 |
#include "asterisk/config.h" |
| 60 |
#include "asterisk/io.h" |
| 61 |
#include "asterisk/stringfields.h" |
| 62 |
#include "asterisk/linkedlists.h" |
| 63 |
#include "asterisk/app.h" |
| 64 |
#include "asterisk/mwi.h" |
| 65 |
#include "asterisk/pbx.h" |
| 66 |
#include "asterisk/channel.h" |
| 67 |
|
| 68 |
|
| 69 |
#define SMDI_MSG_EXPIRY_TIME 30000 |
| 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 |
static const char config_file[] = "smdi.conf"; |
| 172 |
|
| 173 |
struct ast_smdi_interface { |
| 174 |
char name[SMDI_MAX_FILENAME_LEN]; |
| 175 |
struct ao2_container *md_q; |
| 176 |
ast_mutex_t md_q_lock; |
| 177 |
ast_cond_t md_q_cond; |
| 178 |
struct ao2_container *mwi_q; |
| 179 |
ast_mutex_t mwi_q_lock; |
| 180 |
ast_cond_t mwi_q_cond; |
| 181 |
FILE *file; |
| 182 |
int fd; |
| 183 |
pthread_t thread; |
| 184 |
struct termios mode; |
| 185 |
int msdstrip; |
| 186 |
long msg_expiry; |
| 187 |
}; |
| 188 |
|
| 189 |
static AO2_GLOBAL_OBJ_STATIC(smdi_ifaces); |
| 190 |
|
| 191 |
|
| 192 |
struct mailbox_mapping { |
| 193 |
|
| 194 |
|
| 195 |
unsigned int cur_state:1; |
| 196 |
|
| 197 |
struct ast_smdi_interface *iface; |
| 198 |
AST_DECLARE_STRING_FIELDS( |
| 199 |
|
| 200 |
AST_STRING_FIELD(smdi); |
| 201 |
|
| 202 |
AST_STRING_FIELD(mailbox); |
| 203 |
|
| 204 |
AST_STRING_FIELD(context); |
| 205 |
); |
| 206 |
AST_LIST_ENTRY(mailbox_mapping) entry; |
| 207 |
}; |
| 208 |
|
| 209 |
|
| 210 |
#define DEFAULT_POLLING_INTERVAL 10 |
| 211 |
|
| 212 |
|
| 213 |
static struct { |
| 214 |
|
| 215 |
pthread_t thread; |
| 216 |
ast_mutex_t lock; |
| 217 |
ast_cond_t cond; |
| 218 |
|
| 219 |
AST_LIST_HEAD_NOLOCK(, mailbox_mapping) mailbox_mappings; |
| 220 |
|
| 221 |
unsigned int polling_interval; |
| 222 |
|
| 223 |
unsigned int stop:1; |
| 224 |
|
| 225 |
struct timeval last_poll; |
| 226 |
} mwi_monitor = { |
| 227 |
.thread = AST_PTHREADT_NULL, |
| 228 |
}; |
| 229 |
|
| 230 |
static void smdi_interface_destroy(void *obj) |
| 231 |
{ |
| 232 |
struct ast_smdi_interface *iface = obj; |
| 233 |
int mod_unref_defer = 0; |
| 234 |
|
| 235 |
if (iface->thread != AST_PTHREADT_NULL && iface->thread != AST_PTHREADT_STOP) { |
| 236 |
pthread_cancel(iface->thread); |
| 237 |
pthread_join(iface->thread, NULL); |
| 238 |
mod_unref_defer = 1; |
| 239 |
} |
| 240 |
|
| 241 |
iface->thread = AST_PTHREADT_STOP; |
| 242 |
|
| 243 |
if (iface->file) { |
| 244 |
fclose(iface->file); |
| 245 |
} |
| 246 |
|
| 247 |
ao2_cleanup(iface->md_q); |
| 248 |
ast_mutex_destroy(&iface->md_q_lock); |
| 249 |
ast_cond_destroy(&iface->md_q_cond); |
| 250 |
|
| 251 |
ao2_cleanup(iface->mwi_q); |
| 252 |
ast_mutex_destroy(&iface->mwi_q_lock); |
| 253 |
ast_cond_destroy(&iface->mwi_q_cond); |
| 254 |
|
| 255 |
if (mod_unref_defer) { |
| 256 |
ast_module_unref(ast_module_info->self); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
static void ast_smdi_md_message_push(struct ast_smdi_interface *iface, struct ast_smdi_md_message *md_msg) |
| 267 |
{ |
| 268 |
ast_mutex_lock(&iface->md_q_lock); |
| 269 |
ao2_link(iface->md_q, md_msg); |
| 270 |
ast_cond_broadcast(&iface->md_q_cond); |
| 271 |
ast_mutex_unlock(&iface->md_q_lock); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
static void ast_smdi_mwi_message_push(struct ast_smdi_interface *iface, struct ast_smdi_mwi_message *mwi_msg) |
| 281 |
{ |
| 282 |
ast_mutex_lock(&iface->mwi_q_lock); |
| 283 |
ao2_link(iface->mwi_q, mwi_msg); |
| 284 |
ast_cond_broadcast(&iface->mwi_q_cond); |
| 285 |
ast_mutex_unlock(&iface->mwi_q_lock); |
| 286 |
} |
| 287 |
|
| 288 |
static int smdi_toggle_mwi(struct ast_smdi_interface *iface, const char *mailbox, int on) |
| 289 |
{ |
| 290 |
FILE *file; |
| 291 |
int i; |
| 292 |
|
| 293 |
if (!(file = fopen(iface->name, "w"))) { |
| 294 |
ast_log(LOG_ERROR, "Error opening SMDI interface %s (%s) for writing\n", iface->name, strerror(errno)); |
| 295 |
return 1; |
| 296 |
} |
| 297 |
|
| 298 |
ao2_wrlock(iface); |
| 299 |
|
| 300 |
fprintf(file, "%s:MWI ", on ? "OP" : "RMV"); |
| 301 |
|
| 302 |
for (i = 0; i < iface->msdstrip; i++) |
| 303 |
fprintf(file, "0"); |
| 304 |
|
| 305 |
fprintf(file, "%s!\x04", mailbox); |
| 306 |
|
| 307 |
fclose(file); |
| 308 |
|
| 309 |
ao2_unlock(iface); |
| 310 |
ast_debug(1, "Sent MWI set message for %s on %s\n", mailbox, iface->name); |
| 311 |
|
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
int AST_OPTIONAL_API_NAME(ast_smdi_mwi_set)(struct ast_smdi_interface *iface, const char *mailbox) |
| 316 |
{ |
| 317 |
return smdi_toggle_mwi(iface, mailbox, 1); |
| 318 |
} |
| 319 |
|
| 320 |
int AST_OPTIONAL_API_NAME(ast_smdi_mwi_unset)(struct ast_smdi_interface *iface, const char *mailbox) |
| 321 |
{ |
| 322 |
return smdi_toggle_mwi(iface, mailbox, 0); |
| 323 |
} |
| 324 |
|
| 325 |
enum smdi_message_type { |
| 326 |
SMDI_MWI, |
| 327 |
SMDI_MD, |
| 328 |
}; |
| 329 |
|
| 330 |
static inline int lock_msg_q(struct ast_smdi_interface *iface, enum smdi_message_type type) |
| 331 |
{ |
| 332 |
switch (type) { |
| 333 |
case SMDI_MWI: |
| 334 |
return ast_mutex_lock(&iface->mwi_q_lock); |
| 335 |
case SMDI_MD: |
| 336 |
return ast_mutex_lock(&iface->md_q_lock); |
| 337 |
} |
| 338 |
|
| 339 |
return -1; |
| 340 |
} |
| 341 |
|
| 342 |
static inline int unlock_msg_q(struct ast_smdi_interface *iface, enum smdi_message_type type) |
| 343 |
{ |
| 344 |
switch (type) { |
| 345 |
case SMDI_MWI: |
| 346 |
return ast_mutex_unlock(&iface->mwi_q_lock); |
| 347 |
case SMDI_MD: |
| 348 |
return ast_mutex_unlock(&iface->md_q_lock); |
| 349 |
} |
| 350 |
|
| 351 |
return -1; |
| 352 |
} |
| 353 |
|
| 354 |
static inline void *unlink_from_msg_q(struct ast_smdi_interface *iface, enum smdi_message_type type) |
| 355 |
{ |
| 356 |
switch (type) { |
| 357 |
case SMDI_MWI: |
| 358 |
return ao2_callback(iface->mwi_q, OBJ_UNLINK, NULL, NULL); |
| 359 |
case SMDI_MD: |
| 360 |
return ao2_callback(iface->md_q, OBJ_UNLINK, NULL, NULL); |
| 361 |
} |
| 362 |
|
| 363 |
return NULL; |
| 364 |
} |
| 365 |
|
| 366 |
static inline struct timeval msg_timestamp(void *msg, enum smdi_message_type type) |
| 367 |
{ |
| 368 |
struct ast_smdi_md_message *md_msg = msg; |
| 369 |
struct ast_smdi_mwi_message *mwi_msg = msg; |
| 370 |
|
| 371 |
switch (type) { |
| 372 |
case SMDI_MWI: |
| 373 |
return mwi_msg->timestamp; |
| 374 |
case SMDI_MD: |
| 375 |
return md_msg->timestamp; |
| 376 |
} |
| 377 |
|
| 378 |
return ast_tv(0, 0); |
| 379 |
} |
| 380 |
|
| 381 |
static void purge_old_messages(struct ast_smdi_interface *iface, enum smdi_message_type type) |
| 382 |
{ |
| 383 |
struct timeval now = ast_tvnow(); |
| 384 |
long elapsed = 0; |
| 385 |
void *msg; |
| 386 |
|
| 387 |
lock_msg_q(iface, type); |
| 388 |
msg = unlink_from_msg_q(iface, type); |
| 389 |
unlock_msg_q(iface, type); |
| 390 |
|
| 391 |
|
| 392 |
while (msg) { |
| 393 |
elapsed = ast_tvdiff_ms(now, msg_timestamp(msg, type)); |
| 394 |
|
| 395 |
if (elapsed > iface->msg_expiry) { |
| 396 |
|
| 397 |
ao2_ref(msg, -1); |
| 398 |
ast_log(LOG_NOTICE, "Purged expired message from %s SMDI %s message queue. " |
| 399 |
"Message was %ld milliseconds too old.\n", |
| 400 |
iface->name, (type == SMDI_MD) ? "MD" : "MWI", |
| 401 |
elapsed - iface->msg_expiry); |
| 402 |
|
| 403 |
lock_msg_q(iface, type); |
| 404 |
msg = unlink_from_msg_q(iface, type); |
| 405 |
unlock_msg_q(iface, type); |
| 406 |
} else { |
| 407 |
|
| 408 |
switch (type) { |
| 409 |
case SMDI_MD: |
| 410 |
ast_smdi_md_message_push(iface, msg); |
| 411 |
break; |
| 412 |
case SMDI_MWI: |
| 413 |
ast_smdi_mwi_message_push(iface, msg); |
| 414 |
break; |
| 415 |
} |
| 416 |
ao2_ref(msg, -1); |
| 417 |
break; |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
static void *smdi_msg_pop(struct ast_smdi_interface *iface, enum smdi_message_type type) |
| 423 |
{ |
| 424 |
void *msg; |
| 425 |
|
| 426 |
purge_old_messages(iface, type); |
| 427 |
|
| 428 |
lock_msg_q(iface, type); |
| 429 |
msg = unlink_from_msg_q(iface, type); |
| 430 |
unlock_msg_q(iface, type); |
| 431 |
|
| 432 |
return msg; |
| 433 |
} |
| 434 |
|
| 435 |
enum { |
| 436 |
OPT_SEARCH_TERMINAL = (1 << 0), |
| 437 |
OPT_SEARCH_NUMBER = (1 << 1), |
| 438 |
}; |
| 439 |
|
| 440 |
static void *smdi_msg_find(struct ast_smdi_interface *iface, |
| 441 |
enum smdi_message_type type, const char *search_key, struct ast_flags options) |
| 442 |
{ |
| 443 |
void *msg = NULL; |
| 444 |
|
| 445 |
purge_old_messages(iface, type); |
| 446 |
|
| 447 |
switch (type) { |
| 448 |
case SMDI_MD: |
| 449 |
if (ast_strlen_zero(search_key)) { |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
msg = ao2_callback(iface->md_q, 0, NULL, NULL); |
| 454 |
} else if (ast_test_flag(&options, OPT_SEARCH_TERMINAL)) { |
| 455 |
|
| 456 |
struct ast_smdi_md_message md_msg = { .name = "" }; |
| 457 |
strncpy(md_msg.mesg_desk_term, search_key, SMDI_MESG_DESK_TERM_LEN); |
| 458 |
msg = ao2_find(iface->md_q, &md_msg, OBJ_SEARCH_OBJECT); |
| 459 |
} else if (ast_test_flag(&options, OPT_SEARCH_NUMBER)) { |
| 460 |
|
| 461 |
struct ast_smdi_md_message md_msg = { .name = "" }; |
| 462 |
strncpy(md_msg.mesg_desk_num, search_key, SMDI_MESG_DESK_NUM_LEN); |
| 463 |
msg = ao2_find(iface->md_q, &md_msg, OBJ_SEARCH_OBJECT); |
| 464 |
} else { |
| 465 |
|
| 466 |
msg = ao2_find(iface->md_q, search_key, OBJ_SEARCH_KEY); |
| 467 |
} |
| 468 |
break; |
| 469 |
case SMDI_MWI: |
| 470 |
if (ast_strlen_zero(search_key)) { |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
msg = ao2_callback(iface->mwi_q, 0, NULL, NULL); |
| 475 |
} else { |
| 476 |
msg = ao2_find(iface->mwi_q, search_key, OBJ_SEARCH_KEY); |
| 477 |
} |
| 478 |
break; |
| 479 |
} |
| 480 |
|
| 481 |
return msg; |
| 482 |
} |
| 483 |
|
| 484 |
static void *smdi_message_wait(struct ast_smdi_interface *iface, int timeout, |
| 485 |
enum smdi_message_type type, const char *search_key, struct ast_flags options) |
| 486 |
{ |
| 487 |
struct timeval start; |
| 488 |
long diff = 0; |
| 489 |
void *msg; |
| 490 |
ast_cond_t *cond = NULL; |
| 491 |
ast_mutex_t *lock = NULL; |
| 492 |
|
| 493 |
switch (type) { |
| 494 |
case SMDI_MWI: |
| 495 |
cond = &iface->mwi_q_cond; |
| 496 |
lock = &iface->mwi_q_lock; |
| 497 |
break; |
| 498 |
case SMDI_MD: |
| 499 |
cond = &iface->md_q_cond; |
| 500 |
lock = &iface->md_q_lock; |
| 501 |
break; |
| 502 |
} |
| 503 |
|
| 504 |
start = ast_tvnow(); |
| 505 |
|
| 506 |
while (diff < timeout) { |
| 507 |
struct timespec ts = { 0, }; |
| 508 |
struct timeval wait; |
| 509 |
|
| 510 |
lock_msg_q(iface, type); |
| 511 |
|
| 512 |
if ((msg = smdi_msg_find(iface, type, search_key, options))) { |
| 513 |
unlock_msg_q(iface, type); |
| 514 |
return msg; |
| 515 |
} |
| 516 |
|
| 517 |
wait = ast_tvadd(start, ast_tv(0, timeout)); |
| 518 |
ts.tv_sec = wait.tv_sec; |
| 519 |
ts.tv_nsec = wait.tv_usec * 1000; |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
ast_cond_timedwait(cond, lock, &ts); |
| 525 |
|
| 526 |
if ((msg = smdi_msg_find(iface, type, search_key, options))) { |
| 527 |
unlock_msg_q(iface, type); |
| 528 |
return msg; |
| 529 |
} |
| 530 |
|
| 531 |
unlock_msg_q(iface, type); |
| 532 |
|
| 533 |
|
| 534 |
diff = ast_tvdiff_ms(ast_tvnow(), start); |
| 535 |
} |
| 536 |
|
| 537 |
return NULL; |
| 538 |
} |
| 539 |
|
| 540 |
struct ast_smdi_md_message * AST_OPTIONAL_API_NAME(ast_smdi_md_message_pop)(struct ast_smdi_interface *iface) |
| 541 |
{ |
| 542 |
return smdi_msg_pop(iface, SMDI_MD); |
| 543 |
} |
| 544 |
|
| 545 |
struct ast_smdi_md_message * AST_OPTIONAL_API_NAME(ast_smdi_md_message_wait)(struct ast_smdi_interface *iface, int timeout) |
| 546 |
{ |
| 547 |
struct ast_flags options = { 0 }; |
| 548 |
return smdi_message_wait(iface, timeout, SMDI_MD, NULL, options); |
| 549 |
} |
| 550 |
|
| 551 |
struct ast_smdi_mwi_message * AST_OPTIONAL_API_NAME(ast_smdi_mwi_message_pop)(struct ast_smdi_interface *iface) |
| 552 |
{ |
| 553 |
return smdi_msg_pop(iface, SMDI_MWI); |
| 554 |
} |
| 555 |
|
| 556 |
struct ast_smdi_mwi_message * AST_OPTIONAL_API_NAME(ast_smdi_mwi_message_wait)(struct ast_smdi_interface *iface, int timeout) |
| 557 |
{ |
| 558 |
struct ast_flags options = { 0 }; |
| 559 |
return smdi_message_wait(iface, timeout, SMDI_MWI, NULL, options); |
| 560 |
} |
| 561 |
|
| 562 |
struct ast_smdi_mwi_message * AST_OPTIONAL_API_NAME(ast_smdi_mwi_message_wait_station)(struct ast_smdi_interface *iface, int timeout, |
| 563 |
const char *station) |
| 564 |
{ |
| 565 |
struct ast_flags options = { 0 }; |
| 566 |
return smdi_message_wait(iface, timeout, SMDI_MWI, station, options); |
| 567 |
} |
| 568 |
|
| 569 |
struct ast_smdi_interface * AST_OPTIONAL_API_NAME(ast_smdi_interface_find)(const char *iface_name) |
| 570 |
{ |
| 571 |
struct ao2_container *c; |
| 572 |
struct ast_smdi_interface *iface = NULL; |
| 573 |
|
| 574 |
c = ao2_global_obj_ref(smdi_ifaces); |
| 575 |
if (c) { |
| 576 |
iface = ao2_find(c, iface_name, OBJ_SEARCH_KEY); |
| 577 |
ao2_ref(c, -1); |
| 578 |
} |
| 579 |
|
| 580 |
return iface; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
static void *smdi_read(void *iface_p) |
| 593 |
{ |
| 594 |
struct ast_smdi_interface *iface = iface_p; |
| 595 |
struct ast_smdi_md_message *md_msg; |
| 596 |
struct ast_smdi_mwi_message *mwi_msg; |
| 597 |
char *cp = NULL; |
| 598 |
int i, c; |
| 599 |
int start = 0; |
| 600 |
|
| 601 |
|
| 602 |
while ((c = fgetc(iface->file))) { |
| 603 |
|
| 604 |
|
| 605 |
if (!start) { |
| 606 |
if (c == 'M') { |
| 607 |
ast_debug(1, "Read an 'M' to start an SMDI message\n"); |
| 608 |
start = 1; |
| 609 |
} |
| 610 |
continue; |
| 611 |
} |
| 612 |
|
| 613 |
if (c == 'D') { |
| 614 |
start = 0; |
| 615 |
|
| 616 |
ast_debug(1, "Read a 'D' ... it's an MD message.\n"); |
| 617 |
|
| 618 |
md_msg = ao2_alloc(sizeof(*md_msg), NULL); |
| 619 |
if (!md_msg) { |
| 620 |
return NULL; |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
for (i = 0; i < sizeof(md_msg->mesg_desk_num) - 1; i++) { |
| 625 |
c = fgetc(iface->file); |
| 626 |
if (c == EOF) { |
| 627 |
ast_log(LOG_ERROR, "Unexpected EOF while reading MD message\n"); |
| 628 |
ao2_ref(md_msg, -1); |
| 629 |
return NULL; |
| 630 |
} |
| 631 |
md_msg->mesg_desk_num[i] = (char) c; |
| 632 |
ast_debug(1, "Read a '%c'\n", md_msg->mesg_desk_num[i]); |
| 633 |
} |
| 634 |
|
| 635 |
md_msg->mesg_desk_num[sizeof(md_msg->mesg_desk_num) - 1] = '\0'; |
| 636 |
|
| 637 |
ast_debug(1, "The message desk number is '%s'\n", md_msg->mesg_desk_num); |
| 638 |
|
| 639 |
|
| 640 |
for (i = 0; i < sizeof(md_msg->mesg_desk_term) - 1; i++) { |
| 641 |
c = fgetc(iface->file); |
| 642 |
if (c == EOF) { |
| 643 |
ast_log(LOG_ERROR, "Unexpected EOF while reading SMDI message\n"); |
| 644 |
ao2_ref(md_msg, -1); |
| 645 |
return NULL; |
| 646 |
} |
| 647 |
md_msg->mesg_desk_term[i] = (char) c; |
| 648 |
ast_debug(1, "Read a '%c'\n", md_msg->mesg_desk_term[i]); |
| 649 |
} |
| 650 |
|
| 651 |
md_msg->mesg_desk_term[sizeof(md_msg->mesg_desk_term) - 1] = '\0'; |
| 652 |
|
| 653 |
ast_debug(1, "The message desk terminal is '%s'\n", md_msg->mesg_desk_term); |
| 654 |
|
| 655 |
|
| 656 |
c = fgetc(iface->file); |
| 657 |
if (c == EOF) { |
| 658 |
ast_log(LOG_ERROR, "Unexpected EOF while reading SMDI message\n"); |
| 659 |
ao2_ref(md_msg, -1); |
| 660 |
return NULL; |
| 661 |
} |
| 662 |
md_msg->type = (char) c; |
| 663 |
|
| 664 |
ast_debug(1, "Message type is '%c'\n", md_msg->type); |
| 665 |
|
| 666 |
|
| 667 |
cp = &md_msg->fwd_st[0]; |
| 668 |
for (i = 0; i < sizeof(md_msg->fwd_st) - 1; i++) { |
| 669 |
if ((c = fgetc(iface->file)) == ' ') { |
| 670 |
*cp = '\0'; |
| 671 |
ast_debug(1, "Read a space, done looking for the forwarding station\n"); |
| 672 |
break; |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
if (i >= iface->msdstrip) { |
| 677 |
ast_debug(1, "Read a '%c' and stored it in the forwarding station buffer\n", c); |
| 678 |
*cp++ = c; |
| 679 |
} else { |
| 680 |
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); |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
md_msg->fwd_st[sizeof(md_msg->fwd_st) - 1] = '\0'; |
| 686 |
cp = NULL; |
| 687 |
|
| 688 |
ast_debug(1, "The forwarding station is '%s'\n", md_msg->fwd_st); |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
ast_copy_string(md_msg->name, md_msg->fwd_st, sizeof(md_msg->name)); |
| 693 |
|
| 694 |
|
| 695 |
cp = &md_msg->calling_st[0]; |
| 696 |
for (i = 0; i < sizeof(md_msg->calling_st) - 1; i++) { |
| 697 |
if (!isdigit((c = fgetc(iface->file)))) { |
| 698 |
*cp = '\0'; |
| 699 |
ast_debug(1, "Read a '%c', but didn't store it in the calling station buffer because it's not a digit\n", c); |
| 700 |
if (c == ' ') { |
| 701 |
|
| 702 |
|
| 703 |
i--; |
| 704 |
continue; |
| 705 |
} |
| 706 |
break; |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
if (i >= iface->msdstrip) { |
| 711 |
ast_debug(1, "Read a '%c' and stored it in the calling station buffer\n", c); |
| 712 |
*cp++ = c; |
| 713 |
} else { |
| 714 |
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); |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
md_msg->calling_st[sizeof(md_msg->calling_st) - 1] = '\0'; |
| 720 |
cp = NULL; |
| 721 |
|
| 722 |
ast_debug(1, "The calling station is '%s'\n", md_msg->calling_st); |
| 723 |
|
| 724 |
|
| 725 |
md_msg->timestamp = ast_tvnow(); |
| 726 |
ast_smdi_md_message_push(iface, md_msg); |
| 727 |
ast_debug(1, "Received SMDI MD message on %s\n", iface->name); |
| 728 |
|
| 729 |
ao2_ref(md_msg, -1); |
| 730 |
|
| 731 |
} else if (c == 'W') { |
| 732 |
start = 0; |
| 733 |
|
| 734 |
ast_debug(1, "Read a 'W', it's an MWI message. (No more debug coming for MWI messages)\n"); |
| 735 |
|
| 736 |
mwi_msg = ao2_alloc(sizeof(*mwi_msg), NULL); |
| 737 |
if (!mwi_msg) { |
| 738 |
return NULL; |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
fgetc(iface->file); |
| 743 |
|
| 744 |
|
| 745 |
cp = &mwi_msg->fwd_st[0]; |
| 746 |
for (i = 0; i < sizeof(mwi_msg->fwd_st) - 1; i++) { |
| 747 |
if ((c = fgetc(iface->file)) == ' ') { |
| 748 |
*cp = '\0'; |
| 749 |
break; |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
if (i >= iface->msdstrip) |
| 754 |
*cp++ = c; |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
mwi_msg->fwd_st[sizeof(mwi_msg->fwd_st) - 1] = '\0'; |
| 759 |
cp = NULL; |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
ast_copy_string(mwi_msg->name, mwi_msg->fwd_st, sizeof(mwi_msg->name)); |
| 764 |
|
| 765 |
|
| 766 |
for (i = 0; i < sizeof(mwi_msg->cause) - 1; i++) { |
| 767 |
c = fgetc(iface->file); |
| 768 |
if (c == EOF) { |
| 769 |
ast_log(LOG_ERROR, "Unexpected EOF while reading MWI message\n"); |
| 770 |
ao2_ref(mwi_msg, -1); |
| 771 |
return NULL; |
| 772 |
} |
| 773 |
mwi_msg->cause[i] = (char) c; |
| 774 |
} |
| 775 |
|
| 776 |
mwi_msg->cause[sizeof(mwi_msg->cause) - 1] = '\0'; |
| 777 |
|
| 778 |
|
| 779 |
mwi_msg->timestamp = ast_tvnow(); |
| 780 |
ast_smdi_mwi_message_push(iface, mwi_msg); |
| 781 |
ast_debug(1, "Received SMDI MWI message on %s\n", iface->name); |
| 782 |
|
| 783 |
ao2_ref(mwi_msg, -1); |
| 784 |
} else { |
| 785 |
ast_log(LOG_ERROR, "Unknown SMDI message type received on %s (M%c).\n", iface->name, c); |
| 786 |
start = 0; |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
ast_log(LOG_ERROR, "Error reading from SMDI interface %s, stopping listener thread\n", iface->name); |
| 791 |
return NULL; |
| 792 |
} |
| 793 |
|
| 794 |
static void destroy_mailbox_mapping(struct mailbox_mapping *mm) |
| 795 |
{ |
| 796 |
ast_string_field_free_memory(mm); |
| 797 |
ao2_ref(mm->iface, -1); |
| 798 |
ast_free(mm); |
| 799 |
} |
| 800 |
|
| 801 |
static void destroy_all_mailbox_mappings(void) |
| 802 |
{ |
| 803 |
struct mailbox_mapping *mm; |
| 804 |
|
| 805 |
ast_mutex_lock(&mwi_monitor.lock); |
| 806 |
while ((mm = AST_LIST_REMOVE_HEAD(&mwi_monitor.mailbox_mappings, entry))) |
| 807 |
destroy_mailbox_mapping(mm); |
| 808 |
ast_mutex_unlock(&mwi_monitor.lock); |
| 809 |
} |
| 810 |
|
| 811 |
static void append_mailbox_mapping(struct ast_variable *var, struct ast_smdi_interface *iface) |
| 812 |
{ |
| 813 |
struct mailbox_mapping *mm; |
| 814 |
char *mailbox, *context; |
| 815 |
|
| 816 |
if (!(mm = ast_calloc_with_stringfields(1, struct mailbox_mapping, 32))) |
| 817 |
return; |
| 818 |
|
| 819 |
ast_string_field_set(mm, smdi, var->name); |
| 820 |
|
| 821 |
context = ast_strdupa(var->value); |
| 822 |
mailbox = strsep(&context, "@"); |
| 823 |
if (ast_strlen_zero(context)) |
| 824 |
context = "default"; |
| 825 |
|
| 826 |
ast_string_field_set(mm, mailbox, mailbox); |
| 827 |
ast_string_field_set(mm, context, context); |
| 828 |
|
| 829 |
mm->iface = ao2_bump(iface); |
| 830 |
|
| 831 |
ast_mutex_lock(&mwi_monitor.lock); |
| 832 |
AST_LIST_INSERT_TAIL(&mwi_monitor.mailbox_mappings, mm, entry); |
| 833 |
ast_mutex_unlock(&mwi_monitor.lock); |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
static void poll_mailbox(struct mailbox_mapping *mm) |
| 840 |
{ |
| 841 |
char buf[1024]; |
| 842 |
unsigned int state; |
| 843 |
|
| 844 |
snprintf(buf, sizeof(buf), "%s@%s", mm->mailbox, mm->context); |
| 845 |
|
| 846 |
state = !!ast_app_has_voicemail(mm->mailbox, NULL); |
| 847 |
|
| 848 |
if (state != mm->cur_state) { |
| 849 |
if (state) |
| 850 |
ast_smdi_mwi_set(mm->iface, mm->smdi); |
| 851 |
else |
| 852 |
ast_smdi_mwi_unset(mm->iface, mm->smdi); |
| 853 |
|
| 854 |
mm->cur_state = state; |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
static void *mwi_monitor_handler(void *data) |
| 859 |
{ |
| 860 |
while (!mwi_monitor.stop) { |
| 861 |
struct timespec ts = { 0, }; |
| 862 |
struct timeval polltime; |
| 863 |
struct mailbox_mapping *mm; |
| 864 |
|
| 865 |
ast_mutex_lock(&mwi_monitor.lock); |
| 866 |
|
| 867 |
mwi_monitor.last_poll = ast_tvnow(); |
| 868 |
|
| 869 |
AST_LIST_TRAVERSE(&mwi_monitor.mailbox_mappings, mm, entry) |
| 870 |
poll_mailbox(mm); |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
polltime = ast_tvadd(mwi_monitor.last_poll, ast_tv(mwi_monitor.polling_interval, 0)); |
| 875 |
ts.tv_sec = polltime.tv_sec; |
| 876 |
ts.tv_nsec = polltime.tv_usec * 1000; |
| 877 |
ast_cond_timedwait(&mwi_monitor.cond, &mwi_monitor.lock, &ts); |
| 878 |
|
| 879 |
ast_mutex_unlock(&mwi_monitor.lock); |
| 880 |
} |
| 881 |
|
| 882 |
return NULL; |
| 883 |
} |
| 884 |
|
| 885 |
static int smdi_mwi_q_cmp_fn(void *obj, void *data, int flags) |
| 886 |
{ |
| 887 |
struct ast_smdi_mwi_message *msg = obj; |
| 888 |
char *str = data; |
| 889 |
return !strcmp(msg->name, str) ? CMP_MATCH | CMP_STOP : 0; |
| 890 |
} |
| 891 |
|
| 892 |
static int smdi_md_q_cmp_fn(void *obj, void *arg, int flags) |
| 893 |
{ |
| 894 |
const struct ast_smdi_md_message *msg = obj; |
| 895 |
const struct ast_smdi_md_message *search_msg = arg; |
| 896 |
const char *search_key = arg; |
| 897 |
int cmp = 0; |
| 898 |
|
| 899 |
switch (flags & OBJ_SEARCH_MASK) { |
| 900 |
case OBJ_SEARCH_OBJECT: |
| 901 |
if (!ast_strlen_zero(search_msg->mesg_desk_num)) { |
| 902 |
cmp = strcmp(msg->mesg_desk_num, search_msg->mesg_desk_num); |
| 903 |
} |
| 904 |
if (!ast_strlen_zero(search_msg->mesg_desk_term)) { |
| 905 |
cmp |= strcmp(msg->mesg_desk_term, search_msg->mesg_desk_term); |
| 906 |
} |
| 907 |
break; |
| 908 |
case OBJ_SEARCH_KEY: |
| 909 |
cmp = strcmp(msg->name, search_key); |
| 910 |
break; |
| 911 |
} |
| 912 |
|
| 913 |
if (cmp) { |
| 914 |
return 0; |
| 915 |
} |
| 916 |
|
| 917 |
return CMP_MATCH; |
| 918 |
} |
| 919 |
|
| 920 |
static struct ast_smdi_interface *alloc_smdi_interface(void) |
| 921 |
{ |
| 922 |
struct ast_smdi_interface *iface; |
| 923 |
|
| 924 |
if (!(iface = ao2_alloc(sizeof(*iface), smdi_interface_destroy))) { |
| 925 |
return NULL; |
| 926 |
} |
| 927 |
|
| 928 |
iface->md_q = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, smdi_md_q_cmp_fn); |
| 929 |
iface->mwi_q = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, smdi_mwi_q_cmp_fn); |
| 930 |
|
| 931 |
ast_mutex_init(&iface->md_q_lock); |
| 932 |
ast_cond_init(&iface->md_q_cond, NULL); |
| 933 |
|
| 934 |
ast_mutex_init(&iface->mwi_q_lock); |
| 935 |
ast_cond_init(&iface->mwi_q_cond, NULL); |
| 936 |
|
| 937 |
return iface; |
| 938 |
} |
| 939 |
|
| 940 |
static int smdi_ifaces_cmp_fn(void *obj, void *data, int flags) |
| 941 |
{ |
| 942 |
struct ast_smdi_interface *iface = obj; |
| 943 |
|
| 944 |
char *str = data; |
| 945 |
return !strcmp(iface->name, str) ? CMP_MATCH | CMP_STOP : 0; |
| 946 |
} |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
static int smdi_load(int reload) |
| 959 |
{ |
| 960 |
struct ast_config *conf; |
| 961 |
struct ast_variable *v; |
| 962 |
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; |
| 963 |
int res = 0; |
| 964 |
RAII_VAR(struct ao2_container *, new_ifaces, NULL, ao2_cleanup); |
| 965 |
RAII_VAR(struct ao2_container *, old_ifaces, ao2_global_obj_ref(smdi_ifaces), ao2_cleanup); |
| 966 |
struct ast_smdi_interface *mailbox_iface = NULL; |
| 967 |
|
| 968 |
|
| 969 |
speed_t baud_rate = B9600; |
| 970 |
tcflag_t paritybit = PARENB; |
| 971 |
tcflag_t charsize = CS7; |
| 972 |
int stopbits = 0; |
| 973 |
|
| 974 |
int msdstrip = 0; |
| 975 |
long msg_expiry = SMDI_MSG_EXPIRY_TIME; |
| 976 |
|
| 977 |
if (!(conf = ast_config_load(config_file, config_flags)) || conf == CONFIG_STATUS_FILEINVALID) { |
| 978 |
if (reload) |
| 979 |
ast_log(LOG_NOTICE, "Unable to reload config %s: SMDI untouched\n", config_file); |
| 980 |
else |
| 981 |
ast_log(LOG_NOTICE, "Unable to load config %s: SMDI disabled\n", config_file); |
| 982 |
return 1; |
| 983 |
} else if (conf == CONFIG_STATUS_FILEUNCHANGED) |
| 984 |
return 0; |
| 985 |
|
| 986 |
new_ifaces = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, smdi_ifaces_cmp_fn); |
| 987 |
if (!new_ifaces) { |
| 988 |
ast_config_destroy(conf); |
| 989 |
return -1; |
| 990 |
} |
| 991 |
|
| 992 |
for (v = ast_variable_browse(conf, "interfaces"); v; v = v->next) { |
| 993 |
RAII_VAR(struct ast_smdi_interface *, iface, NULL, ao2_cleanup); |
| 994 |
|
| 995 |
if (!strcasecmp(v->name, "baudrate")) { |
| 996 |
if (!strcasecmp(v->value, "9600")) |
| 997 |
baud_rate = B9600; |
| 998 |
else if (!strcasecmp(v->value, "4800")) |
| 999 |
baud_rate = B4800; |
| 1000 |
else if (!strcasecmp(v->value, "2400")) |
| 1001 |
baud_rate = B2400; |
| 1002 |
else if (!strcasecmp(v->value, "1200")) |
| 1003 |
baud_rate = B1200; |
| 1004 |
else { |
| 1005 |
ast_log(LOG_NOTICE, "Invalid baud rate '%s' specified in %s (line %d), using default\n", v->value, config_file, v->lineno); |
| 1006 |
baud_rate = B9600; |
| 1007 |
} |
| 1008 |
} else if (!strcasecmp(v->name, "msdstrip")) { |
| 1009 |
if (!sscanf(v->value, "%30d", &msdstrip)) { |
| 1010 |
ast_log(LOG_NOTICE, "Invalid msdstrip value in %s (line %d), using default\n", config_file, v->lineno); |
| 1011 |
msdstrip = 0; |
| 1012 |
} else if (0 > msdstrip || msdstrip > 9) { |
| 1013 |
ast_log(LOG_NOTICE, "Invalid msdstrip value in %s (line %d), using default\n", config_file, v->lineno); |
| 1014 |
msdstrip = 0; |
| 1015 |
} |
| 1016 |
} else if (!strcasecmp(v->name, "msgexpirytime")) { |
| 1017 |
if (!sscanf(v->value, "%30ld", &msg_expiry)) { |
| 1018 |
ast_log(LOG_NOTICE, "Invalid msgexpirytime value in %s (line %d), using default\n", config_file, v->lineno); |
| 1019 |
msg_expiry = SMDI_MSG_EXPIRY_TIME; |
| 1020 |
} |
| 1021 |
} else if (!strcasecmp(v->name, "paritybit")) { |
| 1022 |
if (!strcasecmp(v->value, "even")) |
| 1023 |
paritybit = PARENB; |
| 1024 |
else if (!strcasecmp(v->value, "odd")) |
| 1025 |
paritybit = PARENB | PARODD; |
| 1026 |
else if (!strcasecmp(v->value, "none")) |
| 1027 |
paritybit = ~PARENB; |
| 1028 |
else { |
| 1029 |
ast_log(LOG_NOTICE, "Invalid parity bit setting in %s (line %d), using default\n", config_file, v->lineno); |
| 1030 |
paritybit = PARENB; |
| 1031 |
} |
| 1032 |
} else if (!strcasecmp(v->name, "charsize")) { |
| 1033 |
if (!strcasecmp(v->value, "7")) |
| 1034 |
charsize = CS7; |
| 1035 |
else if (!strcasecmp(v->value, "8")) |
| 1036 |
charsize = CS8; |
| 1037 |
else { |
| 1038 |
ast_log(LOG_NOTICE, "Invalid character size setting in %s (line %d), using default\n", config_file, v->lineno); |
| 1039 |
charsize = CS7; |
| 1040 |
} |
| 1041 |
} else if (!strcasecmp(v->name, "twostopbits")) { |
| 1042 |
stopbits = ast_true(v->name); |
| 1043 |
} else if (!strcasecmp(v->name, "smdiport")) { |
| 1044 |
if (reload && old_ifaces) { |
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
if ((iface = ao2_find(old_ifaces, v->value, OBJ_SEARCH_KEY))) { |
| 1056 |
ast_log(LOG_NOTICE, "SMDI interface %s already running, not restarting\n", iface->name); |
| 1057 |
ao2_link(new_ifaces, iface); |
| 1058 |
continue; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
|
| 1062 |
if (!(iface = alloc_smdi_interface())) |
| 1063 |
continue; |
| 1064 |
|
| 1065 |
ast_copy_string(iface->name, v->value, sizeof(iface->name)); |
| 1066 |
|
| 1067 |
iface->thread = AST_PTHREADT_NULL; |
| 1068 |
|
| 1069 |
if (!(iface->file = fopen(iface->name, "r"))) { |
| 1070 |
ast_log(LOG_ERROR, "Error opening SMDI interface %s (%s)\n", iface->name, strerror(errno)); |
| 1071 |
continue; |
| 1072 |
} |
| 1073 |
|
| 1074 |
iface->fd = fileno(iface->file); |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
if (tcgetattr(iface->fd, &iface->mode)) { |
| 1080 |
ast_log(LOG_ERROR, "Error getting attributes of %s (%s)\n", iface->name, strerror(errno)); |
| 1081 |
continue; |
| 1082 |
} |
| 1083 |
|
| 1084 |
|
| 1085 |
if (cfsetispeed(&iface->mode, baud_rate) || cfsetospeed(&iface->mode, baud_rate)) { |
| 1086 |
ast_log(LOG_ERROR, "Error setting baud rate on %s (%s)\n", iface->name, strerror(errno)); |
| 1087 |
continue; |
| 1088 |
} |
| 1089 |
|
| 1090 |
|
| 1091 |
if (stopbits) |
| 1092 |
iface->mode.c_cflag = iface->mode.c_cflag | CSTOPB; |
| 1093 |
else |
| 1094 |
iface->mode.c_cflag = iface->mode.c_cflag & ~CSTOPB; |
| 1095 |
|
| 1096 |
|
| 1097 |
iface->mode.c_cflag = (iface->mode.c_cflag & ~PARENB & ~PARODD) | paritybit; |
| 1098 |
|
| 1099 |
|
| 1100 |
iface->mode.c_cflag = (iface->mode.c_cflag & ~CSIZE) | charsize; |
| 1101 |
|
| 1102 |
|
| 1103 |
if (tcsetattr(iface->fd, TCSAFLUSH, &iface->mode)) { |
| 1104 |
ast_log(LOG_ERROR, "Error setting attributes on %s (%s)\n", iface->name, strerror(errno)); |
| 1105 |
continue; |
| 1106 |
} |
| 1107 |
|
| 1108 |
|
| 1109 |
iface->msdstrip = msdstrip; |
| 1110 |
|
| 1111 |
|
| 1112 |
iface->msg_expiry = msg_expiry; |
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
ast_verb(3, "Starting SMDI monitor thread for %s\n", iface->name); |
| 1122 |
if (ast_pthread_create_background(&iface->thread, NULL, smdi_read, iface)) { |
| 1123 |
ast_log(LOG_ERROR, "Error starting SMDI monitor thread for %s\n", iface->name); |
| 1124 |
continue; |
| 1125 |
} |
| 1126 |
|
| 1127 |
ao2_link(new_ifaces, iface); |
| 1128 |
ast_module_ref(ast_module_info->self); |
| 1129 |
} else { |
| 1130 |
ast_log(LOG_NOTICE, "Ignoring unknown option %s in %s\n", v->name, config_file); |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
destroy_all_mailbox_mappings(); |
| 1135 |
mwi_monitor.polling_interval = DEFAULT_POLLING_INTERVAL; |
| 1136 |
|
| 1137 |
for (v = ast_variable_browse(conf, "mailboxes"); v; v = v->next) { |
| 1138 |
if (!strcasecmp(v->name, "smdiport")) { |
| 1139 |
ao2_cleanup(mailbox_iface); |
| 1140 |
|
| 1141 |
if (!(mailbox_iface = ao2_find(new_ifaces, v->value, OBJ_SEARCH_KEY))) { |
| 1142 |
ast_log(LOG_NOTICE, "SMDI interface %s not found\n", v->value); |
| 1143 |
continue; |
| 1144 |
} |
| 1145 |
} else if (!strcasecmp(v->name, "pollinginterval")) { |
| 1146 |
if (sscanf(v->value, "%30u", &mwi_monitor.polling_interval) != 1) { |
| 1147 |
ast_log(LOG_ERROR, "Invalid value for pollinginterval: %s\n", v->value); |
| 1148 |
mwi_monitor.polling_interval = DEFAULT_POLLING_INTERVAL; |
| 1149 |
} |
| 1150 |
} else { |
| 1151 |
if (!mailbox_iface) { |
| 1152 |
ast_log(LOG_ERROR, "Mailbox mapping ignored, no valid SMDI interface specified in mailboxes section\n"); |
| 1153 |
continue; |
| 1154 |
} |
| 1155 |
append_mailbox_mapping(v, mailbox_iface); |
| 1156 |
} |
| 1157 |
} |
| 1158 |
ao2_cleanup(mailbox_iface); |
| 1159 |
|
| 1160 |
ast_config_destroy(conf); |
| 1161 |
|
| 1162 |
ao2_global_obj_replace_unref(smdi_ifaces, new_ifaces); |
| 1163 |
|
| 1164 |
if (!AST_LIST_EMPTY(&mwi_monitor.mailbox_mappings) && mwi_monitor.thread == AST_PTHREADT_NULL |
| 1165 |
&& ast_pthread_create_background(&mwi_monitor.thread, NULL, mwi_monitor_handler, NULL)) { |
| 1166 |
ast_log(LOG_ERROR, "Failed to start MWI monitoring thread. This module will not operate.\n"); |
| 1167 |
return -1; |
| 1168 |
} |
| 1169 |
|
| 1170 |
if (!ao2_container_count(new_ifaces)) { |
| 1171 |
res = 1; |
| 1172 |
} |
| 1173 |
|
| 1174 |
return res; |
| 1175 |
} |
| 1176 |
|
| 1177 |
struct smdi_msg_datastore { |
| 1178 |
unsigned int id; |
| 1179 |
struct ast_smdi_interface *iface; |
| 1180 |
struct ast_smdi_md_message *md_msg; |
| 1181 |
}; |
| 1182 |
|
| 1183 |
static void smdi_msg_datastore_destroy(void *data) |
| 1184 |
{ |
| 1185 |
struct smdi_msg_datastore *smd = data; |
| 1186 |
|
| 1187 |
ao2_cleanup(smd->iface); |
| 1188 |
ao2_cleanup(smd->md_msg); |
| 1189 |
|
| 1190 |
ast_free(smd); |
| 1191 |
} |
| 1192 |
|
| 1193 |
static const struct ast_datastore_info smdi_msg_datastore_info = { |
| 1194 |
.type = "SMDIMSG", |
| 1195 |
.destroy = smdi_msg_datastore_destroy, |
| 1196 |
}; |
| 1197 |
|
| 1198 |
static int smdi_msg_id; |
| 1199 |
|
| 1200 |
|
| 1201 |
#define SMDI_RETRIEVE_TIMEOUT_DEFAULT 3000 |
| 1202 |
|
| 1203 |
AST_APP_OPTIONS(smdi_msg_ret_options, BEGIN_OPTIONS |
| 1204 |
AST_APP_OPTION('t', OPT_SEARCH_TERMINAL), |
| 1205 |
AST_APP_OPTION('n', OPT_SEARCH_NUMBER), |
| 1206 |
END_OPTIONS ); |
| 1207 |
|
| 1208 |
static int smdi_msg_retrieve_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 1209 |
{ |
| 1210 |
struct ast_module_user *u; |
| 1211 |
AST_DECLARE_APP_ARGS(args, |
| 1212 |
AST_APP_ARG(port); |
| 1213 |
AST_APP_ARG(search_key); |
| 1214 |
AST_APP_ARG(timeout); |
| 1215 |
AST_APP_ARG(options); |
| 1216 |
); |
| 1217 |
struct ast_flags options = { 0 }; |
| 1218 |
unsigned int timeout = SMDI_RETRIEVE_TIMEOUT_DEFAULT; |
| 1219 |
int res = -1; |
| 1220 |
char *parse = NULL; |
| 1221 |
struct smdi_msg_datastore *smd = NULL; |
| 1222 |
struct ast_datastore *datastore = NULL; |
| 1223 |
struct ast_smdi_interface *iface = NULL; |
| 1224 |
struct ast_smdi_md_message *md_msg = NULL; |
| 1225 |
|
| 1226 |
u = ast_module_user_add(chan); |
| 1227 |
|
| 1228 |
if (ast_strlen_zero(data)) { |
| 1229 |
ast_log(LOG_ERROR, "SMDI_MSG_RETRIEVE requires an argument\n"); |
| 1230 |
goto return_error; |
| 1231 |
} |
| 1232 |
|
| 1233 |
if (!chan) { |
| 1234 |
ast_log(LOG_ERROR, "SMDI_MSG_RETRIEVE must be used with a channel\n"); |
| 1235 |
goto return_error; |
| 1236 |
} |
| 1237 |
|
| 1238 |
ast_autoservice_start(chan); |
| 1239 |
|
| 1240 |
parse = ast_strdupa(data); |
| 1241 |
AST_STANDARD_APP_ARGS(args, parse); |
| 1242 |
|
| 1243 |
if (ast_strlen_zero(args.port) || ast_strlen_zero(args.search_key)) { |
| 1244 |
ast_log(LOG_ERROR, "Invalid arguments provided to SMDI_MSG_RETRIEVE\n"); |
| 1245 |
goto return_error; |
| 1246 |
} |
| 1247 |
|
| 1248 |
if (!(iface = ast_smdi_interface_find(args.port))) { |
| 1249 |
ast_log(LOG_ERROR, "SMDI port '%s' not found\n", args.port); |
| 1250 |
goto return_error; |
| 1251 |
} |
| 1252 |
|
| 1253 |
if (!ast_strlen_zero(args.options)) { |
| 1254 |
ast_app_parse_options(smdi_msg_ret_options, &options, NULL, args.options); |
| 1255 |
} |
| 1256 |
|
| 1257 |
if (!ast_strlen_zero(args.timeout)) { |
| 1258 |
if (sscanf(args.timeout, "%30u", &timeout) != 1) { |
| 1259 |
ast_log(LOG_ERROR, "'%s' is not a valid timeout\n", args.timeout); |
| 1260 |
timeout = SMDI_RETRIEVE_TIMEOUT_DEFAULT; |
| 1261 |
} |
| 1262 |
} |
| 1263 |
|
| 1264 |
if (!(md_msg = smdi_message_wait(iface, timeout, SMDI_MD, args.search_key, options))) { |
| 1265 |
ast_log(LOG_WARNING, "No SMDI message retrieved for search key '%s' after " |
| 1266 |
"waiting %u ms.\n", args.search_key, timeout); |
| 1267 |
goto return_error; |
| 1268 |
} |
| 1269 |
|
| 1270 |
if (!(smd = ast_calloc(1, sizeof(*smd)))) |
| 1271 |
goto return_error; |
| 1272 |
|
| 1273 |
smd->iface = ao2_bump(iface); |
| 1274 |
smd->md_msg = ao2_bump(md_msg); |
| 1275 |
smd->id = ast_atomic_fetchadd_int((int *) &smdi_msg_id, 1); |
| 1276 |
snprintf(buf, len, "%u", smd->id); |
| 1277 |
|
| 1278 |
if (!(datastore = ast_datastore_alloc(&smdi_msg_datastore_info, buf))) |
| 1279 |
goto return_error; |
| 1280 |
|
| 1281 |
datastore->data = smd; |
| 1282 |
|
| 1283 |
ast_channel_lock(chan); |
| 1284 |
ast_channel_datastore_add(chan, datastore); |
| 1285 |
ast_channel_unlock(chan); |
| 1286 |
|
| 1287 |
res = 0; |
| 1288 |
|
| 1289 |
return_error: |
| 1290 |
ao2_cleanup(iface); |
| 1291 |
ao2_cleanup(md_msg); |
| 1292 |
|
| 1293 |
if (smd && !datastore) |
| 1294 |
smdi_msg_datastore_destroy(smd); |
| 1295 |
|
| 1296 |
if (parse) |
| 1297 |
ast_autoservice_stop(chan); |
| 1298 |
|
| 1299 |
ast_module_user_remove(u); |
| 1300 |
|
| 1301 |
return res; |
| 1302 |
} |
| 1303 |
|
| 1304 |
static int smdi_msg_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 1305 |
{ |
| 1306 |
struct ast_module_user *u; |
| 1307 |
int res = -1; |
| 1308 |
AST_DECLARE_APP_ARGS(args, |
| 1309 |
AST_APP_ARG(id); |
| 1310 |
AST_APP_ARG(component); |
| 1311 |
); |
| 1312 |
char *parse; |
| 1313 |
struct ast_datastore *datastore = NULL; |
| 1314 |
struct smdi_msg_datastore *smd = NULL; |
| 1315 |
|
| 1316 |
u = ast_module_user_add(chan); |
| 1317 |
|
| 1318 |
if (!chan) { |
| 1319 |
ast_log(LOG_ERROR, "SMDI_MSG can not be called without a channel\n"); |
| 1320 |
goto return_error; |
| 1321 |
} |
| 1322 |
|
| 1323 |
if (ast_strlen_zero(data)) { |
| 1324 |
ast_log(LOG_WARNING, "SMDI_MSG requires an argument\n"); |
| 1325 |
goto return_error; |
| 1326 |
} |
| 1327 |
|
| 1328 |
parse = ast_strdupa(data); |
| 1329 |
AST_STANDARD_APP_ARGS(args, parse); |
| 1330 |
|
| 1331 |
if (ast_strlen_zero(args.id)) { |
| 1332 |
ast_log(LOG_WARNING, "ID must be supplied to SMDI_MSG\n"); |
| 1333 |
goto return_error; |
| 1334 |
} |
| 1335 |
|
| 1336 |
if (ast_strlen_zero(args.component)) { |
| 1337 |
ast_log(LOG_WARNING, "ID must be supplied to SMDI_MSG\n"); |
| 1338 |
goto return_error; |
| 1339 |
} |
| 1340 |
|
| 1341 |
ast_channel_lock(chan); |
| 1342 |
datastore = ast_channel_datastore_find(chan, &smdi_msg_datastore_info, args.id); |
| 1343 |
ast_channel_unlock(chan); |
| 1344 |
|
| 1345 |
if (!datastore) { |
| 1346 |
ast_log(LOG_WARNING, "No SMDI message found for message ID '%s'\n", args.id); |
| 1347 |
goto return_error; |
| 1348 |
} |
| 1349 |
|
| 1350 |
smd = datastore->data; |
| 1351 |
|
| 1352 |
if (!strcasecmp(args.component, "number")) { |
| 1353 |
ast_copy_string(buf, smd->md_msg->mesg_desk_num, len); |
| 1354 |
} else if (!strcasecmp(args.component, "terminal")) { |
| 1355 |
ast_copy_string(buf, smd->md_msg->mesg_desk_term, len); |
| 1356 |
} else if (!strcasecmp(args.component, "station")) { |
| 1357 |
ast_copy_string(buf, smd->md_msg->fwd_st, len); |
| 1358 |
} else if (!strcasecmp(args.component, "callerid")) { |
| 1359 |
ast_copy_string(buf, smd->md_msg->calling_st, len); |
| 1360 |
} else if (!strcasecmp(args.component, "type")) { |
| 1361 |
snprintf(buf, len, "%c", smd->md_msg->type); |
| 1362 |
} else { |
| 1363 |
ast_log(LOG_ERROR, "'%s' is not a valid message component for SMDI_MSG\n", |
| 1364 |
args.component); |
| 1365 |
goto return_error; |
| 1366 |
} |
| 1367 |
|
| 1368 |
res = 0; |
| 1369 |
|
| 1370 |
return_error: |
| 1371 |
ast_module_user_remove(u); |
| 1372 |
|
| 1373 |
return res; |
| 1374 |
} |
| 1375 |
|
| 1376 |
static struct ast_custom_function smdi_msg_retrieve_function = { |
| 1377 |
.name = "SMDI_MSG_RETRIEVE", |
| 1378 |
.read = smdi_msg_retrieve_read, |
| 1379 |
}; |
| 1380 |
|
| 1381 |
static struct ast_custom_function smdi_msg_function = { |
| 1382 |
.name = "SMDI_MSG", |
| 1383 |
.read = smdi_msg_read, |
| 1384 |
}; |
| 1385 |
|
| 1386 |
static int unload_module(void) |
| 1387 |
{ |
| 1388 |
ao2_global_obj_release(smdi_ifaces); |
| 1389 |
|
| 1390 |
destroy_all_mailbox_mappings(); |
| 1391 |
|
| 1392 |
ast_mutex_lock(&mwi_monitor.lock); |
| 1393 |
mwi_monitor.stop = 1; |
| 1394 |
ast_cond_signal(&mwi_monitor.cond); |
| 1395 |
ast_mutex_unlock(&mwi_monitor.lock); |
| 1396 |
|
| 1397 |
if (mwi_monitor.thread != AST_PTHREADT_NULL) { |
| 1398 |
pthread_join(mwi_monitor.thread, NULL); |
| 1399 |
} |
| 1400 |
|
| 1401 |
ast_custom_function_unregister(&smdi_msg_retrieve_function); |
| 1402 |
ast_custom_function_unregister(&smdi_msg_function); |
| 1403 |
|
| 1404 |
return 0; |
| 1405 |
} |
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
static int load_module(void) |
| 1418 |
{ |
| 1419 |
int res; |
| 1420 |
|
| 1421 |
ast_mutex_init(&mwi_monitor.lock); |
| 1422 |
ast_cond_init(&mwi_monitor.cond, NULL); |
| 1423 |
|
| 1424 |
|
| 1425 |
res = smdi_load(0); |
| 1426 |
if (res < 0) { |
| 1427 |
unload_module(); |
| 1428 |
return AST_MODULE_LOAD_DECLINE; |
| 1429 |
} else if (res == 1) { |
| 1430 |
ast_log(LOG_NOTICE, "No SMDI interfaces are available to listen on, not starting SMDI listener.\n"); |
| 1431 |
} |
| 1432 |
|
| 1433 |
ast_custom_function_register(&smdi_msg_retrieve_function); |
| 1434 |
ast_custom_function_register(&smdi_msg_function); |
| 1435 |
|
| 1436 |
return AST_MODULE_LOAD_SUCCESS; |
| 1437 |
} |
| 1438 |
|
| 1439 |
static int reload(void) |
| 1440 |
{ |
| 1441 |
int res; |
| 1442 |
|
| 1443 |
res = smdi_load(1); |
| 1444 |
if (res < 0) { |
| 1445 |
return res; |
| 1446 |
} else if (res == 1) { |
| 1447 |
ast_log(LOG_WARNING, "No SMDI interfaces were specified to listen on, not starting SDMI listener.\n"); |
| 1448 |
} |
| 1449 |
return 0; |
| 1450 |
} |
| 1451 |
|
| 1452 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Simplified Message Desk Interface (SMDI) Resource", |
| 1453 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 1454 |
.load = load_module, |
| 1455 |
.unload = unload_module, |
| 1456 |
.reload = reload, |
| 1457 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND, |
| 1458 |
); |