| 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 |
#include "asterisk.h" |
| 62 |
|
| 63 |
#include <signal.h> |
| 64 |
|
| 65 |
#include <portaudio.h> |
| 66 |
|
| 67 |
#include "asterisk/module.h" |
| 68 |
#include "asterisk/channel.h" |
| 69 |
#include "asterisk/pbx.h" |
| 70 |
#include "asterisk/causes.h" |
| 71 |
#include "asterisk/cli.h" |
| 72 |
#include "asterisk/musiconhold.h" |
| 73 |
#include "asterisk/callerid.h" |
| 74 |
#include "asterisk/astobj2.h" |
| 75 |
#include "asterisk/stasis_channels.h" |
| 76 |
#include "asterisk/format_cache.h" |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#define SAMPLE_RATE 16000 |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
#define NUM_SAMPLES 320 |
| 97 |
|
| 98 |
|
| 99 |
#define INPUT_CHANNELS 1 |
| 100 |
|
| 101 |
|
| 102 |
#define OUTPUT_CHANNELS 1 |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
#define TEXT_SIZE 256 |
| 110 |
|
| 111 |
|
| 112 |
#define V_BEGIN " --- <(\"<) --- " |
| 113 |
#define V_END " --- (>\")> ---\n" |
| 114 |
|
| 115 |
|
| 116 |
static const char config_file[] = "console.conf"; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
static struct console_pvt { |
| 125 |
AST_DECLARE_STRING_FIELDS( |
| 126 |
|
| 127 |
AST_STRING_FIELD(name); |
| 128 |
AST_STRING_FIELD(input_device); |
| 129 |
AST_STRING_FIELD(output_device); |
| 130 |
|
| 131 |
AST_STRING_FIELD(context); |
| 132 |
|
| 133 |
AST_STRING_FIELD(exten); |
| 134 |
|
| 135 |
AST_STRING_FIELD(cid_num); |
| 136 |
|
| 137 |
AST_STRING_FIELD(cid_name); |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
AST_STRING_FIELD(mohinterpret); |
| 142 |
|
| 143 |
AST_STRING_FIELD(language); |
| 144 |
|
| 145 |
AST_STRING_FIELD(parkinglot); |
| 146 |
); |
| 147 |
|
| 148 |
struct ast_channel *owner; |
| 149 |
|
| 150 |
PaStream *stream; |
| 151 |
|
| 152 |
struct ast_frame fr; |
| 153 |
|
| 154 |
unsigned int streamstate:1; |
| 155 |
|
| 156 |
unsigned int abort:1; |
| 157 |
|
| 158 |
unsigned int hookstate:1; |
| 159 |
|
| 160 |
unsigned int muted:1; |
| 161 |
|
| 162 |
unsigned int autoanswer:1; |
| 163 |
|
| 164 |
unsigned int overridecontext:1; |
| 165 |
|
| 166 |
|
| 167 |
unsigned int destroy:1; |
| 168 |
|
| 169 |
pthread_t thread; |
| 170 |
} globals; |
| 171 |
|
| 172 |
AST_MUTEX_DEFINE_STATIC(globals_lock); |
| 173 |
|
| 174 |
static struct ao2_container *pvts; |
| 175 |
#define NUM_PVT_BUCKETS 7 |
| 176 |
|
| 177 |
static struct console_pvt *active_pvt; |
| 178 |
AST_RWLOCK_DEFINE_STATIC(active_lock); |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
static struct ast_jb_conf default_jbconf = { |
| 187 |
.flags = 0, |
| 188 |
.max_size = 200, |
| 189 |
.resync_threshold = 1000, |
| 190 |
.impl = "fixed", |
| 191 |
.target_extra = 40, |
| 192 |
}; |
| 193 |
static struct ast_jb_conf global_jbconf; |
| 194 |
|
| 195 |
|
| 196 |
static struct ast_channel *console_request(const char *type, struct ast_format_cap *cap, |
| 197 |
const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause); |
| 198 |
static int console_digit_begin(struct ast_channel *c, char digit); |
| 199 |
static int console_digit_end(struct ast_channel *c, char digit, unsigned int duration); |
| 200 |
static int console_text(struct ast_channel *c, const char *text); |
| 201 |
static int console_hangup(struct ast_channel *c); |
| 202 |
static int console_answer(struct ast_channel *c); |
| 203 |
static struct ast_frame *console_read(struct ast_channel *chan); |
| 204 |
static int console_call(struct ast_channel *c, const char *dest, int timeout); |
| 205 |
static int console_write(struct ast_channel *chan, struct ast_frame *f); |
| 206 |
static int console_indicate(struct ast_channel *chan, int cond, |
| 207 |
const void *data, size_t datalen); |
| 208 |
static int console_fixup(struct ast_channel *oldchan, struct ast_channel *newchan); |
| 209 |
|
| 210 |
|
| 211 |
static struct ast_channel_tech console_tech = { |
| 212 |
.type = "Console", |
| 213 |
.description = "Console Channel Driver", |
| 214 |
.requester = console_request, |
| 215 |
.send_digit_begin = console_digit_begin, |
| 216 |
.send_digit_end = console_digit_end, |
| 217 |
.send_text = console_text, |
| 218 |
.hangup = console_hangup, |
| 219 |
.answer = console_answer, |
| 220 |
.read = console_read, |
| 221 |
.call = console_call, |
| 222 |
.write = console_write, |
| 223 |
.indicate = console_indicate, |
| 224 |
.fixup = console_fixup, |
| 225 |
}; |
| 226 |
|
| 227 |
|
| 228 |
#define console_pvt_lock(pvt) ao2_lock(pvt) |
| 229 |
|
| 230 |
|
| 231 |
#define console_pvt_unlock(pvt) ao2_unlock(pvt) |
| 232 |
|
| 233 |
static inline struct console_pvt *ref_pvt(struct console_pvt *pvt) |
| 234 |
{ |
| 235 |
if (pvt) |
| 236 |
ao2_ref(pvt, +1); |
| 237 |
return pvt; |
| 238 |
} |
| 239 |
|
| 240 |
static inline struct console_pvt *unref_pvt(struct console_pvt *pvt) |
| 241 |
{ |
| 242 |
ao2_ref(pvt, -1); |
| 243 |
return NULL; |
| 244 |
} |
| 245 |
|
| 246 |
static struct console_pvt *find_pvt(const char *name) |
| 247 |
{ |
| 248 |
struct console_pvt tmp_pvt = { |
| 249 |
.name = name, |
| 250 |
}; |
| 251 |
|
| 252 |
return ao2_find(pvts, &tmp_pvt, OBJ_POINTER); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
static void *stream_monitor(void *data) |
| 266 |
{ |
| 267 |
struct console_pvt *pvt = data; |
| 268 |
char buf[NUM_SAMPLES * sizeof(int16_t)]; |
| 269 |
PaError res; |
| 270 |
struct ast_frame f = { |
| 271 |
.frametype = AST_FRAME_VOICE, |
| 272 |
.subclass.format = ast_format_slin16, |
| 273 |
.src = "console_stream_monitor", |
| 274 |
.data.ptr = buf, |
| 275 |
.datalen = sizeof(buf), |
| 276 |
.samples = sizeof(buf) / sizeof(int16_t), |
| 277 |
}; |
| 278 |
|
| 279 |
for (;;) { |
| 280 |
console_pvt_lock(pvt); |
| 281 |
res = Pa_ReadStream(pvt->stream, buf, sizeof(buf) / sizeof(int16_t)); |
| 282 |
console_pvt_unlock(pvt); |
| 283 |
|
| 284 |
if (!pvt->owner || pvt->abort) { |
| 285 |
return NULL; |
| 286 |
} |
| 287 |
|
| 288 |
if (res == paNoError) { |
| 289 |
ast_queue_frame(pvt->owner, &f); |
| 290 |
} else { |
| 291 |
ast_log(LOG_WARNING, "Console ReadStream failed: %s\n", Pa_GetErrorText(res)); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
return NULL; |
| 296 |
} |
| 297 |
|
| 298 |
static int open_stream(struct console_pvt *pvt) |
| 299 |
{ |
| 300 |
int res = paInternalError; |
| 301 |
|
| 302 |
if (!strcasecmp(pvt->input_device, "default") && |
| 303 |
!strcasecmp(pvt->output_device, "default")) { |
| 304 |
res = Pa_OpenDefaultStream(&pvt->stream, INPUT_CHANNELS, OUTPUT_CHANNELS, |
| 305 |
paInt16, SAMPLE_RATE, NUM_SAMPLES, NULL, NULL); |
| 306 |
} else { |
| 307 |
PaStreamParameters input_params = { |
| 308 |
.channelCount = 1, |
| 309 |
.sampleFormat = paInt16, |
| 310 |
.suggestedLatency = (1.0 / 50.0), |
| 311 |
.device = paNoDevice, |
| 312 |
}; |
| 313 |
PaStreamParameters output_params = { |
| 314 |
.channelCount = 1, |
| 315 |
.sampleFormat = paInt16, |
| 316 |
.suggestedLatency = (1.0 / 50.0), |
| 317 |
.device = paNoDevice, |
| 318 |
}; |
| 319 |
PaDeviceIndex idx, num_devices, def_input, def_output; |
| 320 |
|
| 321 |
if (!(num_devices = Pa_GetDeviceCount())) |
| 322 |
return res; |
| 323 |
|
| 324 |
def_input = Pa_GetDefaultInputDevice(); |
| 325 |
def_output = Pa_GetDefaultOutputDevice(); |
| 326 |
|
| 327 |
for (idx = 0; |
| 328 |
idx < num_devices && (input_params.device == paNoDevice |
| 329 |
|| output_params.device == paNoDevice); |
| 330 |
idx++) |
| 331 |
{ |
| 332 |
const PaDeviceInfo *dev = Pa_GetDeviceInfo(idx); |
| 333 |
|
| 334 |
if (dev->maxInputChannels) { |
| 335 |
if ( (idx == def_input && !strcasecmp(pvt->input_device, "default")) || |
| 336 |
!strcasecmp(pvt->input_device, dev->name) ) |
| 337 |
input_params.device = idx; |
| 338 |
} |
| 339 |
|
| 340 |
if (dev->maxOutputChannels) { |
| 341 |
if ( (idx == def_output && !strcasecmp(pvt->output_device, "default")) || |
| 342 |
!strcasecmp(pvt->output_device, dev->name) ) |
| 343 |
output_params.device = idx; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
if (input_params.device == paNoDevice) |
| 348 |
ast_log(LOG_ERROR, "No input device found for console device '%s'\n", pvt->name); |
| 349 |
if (output_params.device == paNoDevice) |
| 350 |
ast_log(LOG_ERROR, "No output device found for console device '%s'\n", pvt->name); |
| 351 |
|
| 352 |
res = Pa_OpenStream(&pvt->stream, &input_params, &output_params, |
| 353 |
SAMPLE_RATE, NUM_SAMPLES, paNoFlag, NULL, NULL); |
| 354 |
} |
| 355 |
|
| 356 |
return res; |
| 357 |
} |
| 358 |
|
| 359 |
static int start_stream(struct console_pvt *pvt) |
| 360 |
{ |
| 361 |
PaError res; |
| 362 |
int ret_val = 0; |
| 363 |
|
| 364 |
console_pvt_lock(pvt); |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
if (pvt->streamstate || !pvt->owner) |
| 370 |
goto return_unlock; |
| 371 |
|
| 372 |
pvt->streamstate = 1; |
| 373 |
ast_debug(1, "Starting stream\n"); |
| 374 |
|
| 375 |
res = open_stream(pvt); |
| 376 |
if (res != paNoError) { |
| 377 |
ast_log(LOG_WARNING, "Failed to open stream - (%d) %s\n", |
| 378 |
res, Pa_GetErrorText(res)); |
| 379 |
ret_val = -1; |
| 380 |
goto return_unlock; |
| 381 |
} |
| 382 |
|
| 383 |
res = Pa_StartStream(pvt->stream); |
| 384 |
if (res != paNoError) { |
| 385 |
ast_log(LOG_WARNING, "Failed to start stream - (%d) %s\n", |
| 386 |
res, Pa_GetErrorText(res)); |
| 387 |
ret_val = -1; |
| 388 |
goto return_unlock; |
| 389 |
} |
| 390 |
|
| 391 |
if (ast_pthread_create_background(&pvt->thread, NULL, stream_monitor, pvt)) { |
| 392 |
ast_log(LOG_ERROR, "Failed to start stream monitor thread\n"); |
| 393 |
ret_val = -1; |
| 394 |
} |
| 395 |
|
| 396 |
return_unlock: |
| 397 |
console_pvt_unlock(pvt); |
| 398 |
|
| 399 |
return ret_val; |
| 400 |
} |
| 401 |
|
| 402 |
static int stop_stream(struct console_pvt *pvt) |
| 403 |
{ |
| 404 |
if (!pvt->streamstate || pvt->thread == AST_PTHREADT_NULL) |
| 405 |
return 0; |
| 406 |
|
| 407 |
pvt->abort = 1; |
| 408 |
|
| 409 |
pthread_kill(pvt->thread, SIGURG); |
| 410 |
pthread_join(pvt->thread, NULL); |
| 411 |
|
| 412 |
console_pvt_lock(pvt); |
| 413 |
Pa_AbortStream(pvt->stream); |
| 414 |
Pa_CloseStream(pvt->stream); |
| 415 |
pvt->stream = NULL; |
| 416 |
pvt->streamstate = 0; |
| 417 |
console_pvt_unlock(pvt); |
| 418 |
|
| 419 |
return 0; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
static struct ast_channel *console_new(struct console_pvt *pvt, const char *ext, const char *ctx, int state, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor) |
| 426 |
{ |
| 427 |
struct ast_format_cap *caps; |
| 428 |
struct ast_channel *chan; |
| 429 |
|
| 430 |
caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT); |
| 431 |
if (!caps) { |
| 432 |
return NULL; |
| 433 |
} |
| 434 |
|
| 435 |
if (!(chan = ast_channel_alloc(1, state, pvt->cid_num, pvt->cid_name, NULL, |
| 436 |
ext, ctx, assignedids, requestor, 0, "Console/%s", pvt->name))) { |
| 437 |
ao2_ref(caps, -1); |
| 438 |
return NULL; |
| 439 |
} |
| 440 |
|
| 441 |
ast_channel_stage_snapshot(chan); |
| 442 |
|
| 443 |
ast_channel_tech_set(chan, &console_tech); |
| 444 |
ast_channel_set_readformat(chan, ast_format_slin16); |
| 445 |
ast_channel_set_writeformat(chan, ast_format_slin16); |
| 446 |
ast_format_cap_append(caps, ast_format_slin16, 0); |
| 447 |
ast_channel_nativeformats_set(chan, caps); |
| 448 |
ao2_ref(caps, -1); |
| 449 |
ast_channel_tech_pvt_set(chan, ref_pvt(pvt)); |
| 450 |
|
| 451 |
pvt->owner = chan; |
| 452 |
|
| 453 |
if (!ast_strlen_zero(pvt->language)) |
| 454 |
ast_channel_language_set(chan, pvt->language); |
| 455 |
|
| 456 |
ast_jb_configure(chan, &global_jbconf); |
| 457 |
|
| 458 |
ast_channel_stage_snapshot_done(chan); |
| 459 |
ast_channel_unlock(chan); |
| 460 |
|
| 461 |
if (state != AST_STATE_DOWN) { |
| 462 |
if (ast_pbx_start(chan)) { |
| 463 |
ast_channel_hangupcause_set(chan, AST_CAUSE_SWITCH_CONGESTION); |
| 464 |
ast_hangup(chan); |
| 465 |
chan = NULL; |
| 466 |
} else |
| 467 |
start_stream(pvt); |
| 468 |
} |
| 469 |
|
| 470 |
return chan; |
| 471 |
} |
| 472 |
|
| 473 |
static struct ast_channel *console_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause) |
| 474 |
{ |
| 475 |
struct ast_channel *chan = NULL; |
| 476 |
struct console_pvt *pvt; |
| 477 |
|
| 478 |
if (!(pvt = find_pvt(data))) { |
| 479 |
ast_log(LOG_ERROR, "Console device '%s' not found\n", data); |
| 480 |
return NULL; |
| 481 |
} |
| 482 |
|
| 483 |
if (!(ast_format_cap_iscompatible(cap, console_tech.capabilities))) { |
| 484 |
struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN); |
| 485 |
ast_log(LOG_NOTICE, "Channel requested with unsupported format(s): '%s'\n", |
| 486 |
ast_format_cap_get_names(cap, &cap_buf)); |
| 487 |
goto return_unref; |
| 488 |
} |
| 489 |
|
| 490 |
if (pvt->owner) { |
| 491 |
ast_log(LOG_NOTICE, "Console channel already active!\n"); |
| 492 |
*cause = AST_CAUSE_BUSY; |
| 493 |
goto return_unref; |
| 494 |
} |
| 495 |
|
| 496 |
console_pvt_lock(pvt); |
| 497 |
chan = console_new(pvt, NULL, NULL, AST_STATE_DOWN, assignedids, requestor); |
| 498 |
console_pvt_unlock(pvt); |
| 499 |
|
| 500 |
if (!chan) |
| 501 |
ast_log(LOG_WARNING, "Unable to create new Console channel!\n"); |
| 502 |
|
| 503 |
return_unref: |
| 504 |
unref_pvt(pvt); |
| 505 |
|
| 506 |
return chan; |
| 507 |
} |
| 508 |
|
| 509 |
static int console_digit_begin(struct ast_channel *c, char digit) |
| 510 |
{ |
| 511 |
ast_verb(1, V_BEGIN "Console Received Beginning of Digit %c" V_END, digit); |
| 512 |
|
| 513 |
return -1; |
| 514 |
} |
| 515 |
|
| 516 |
static int console_digit_end(struct ast_channel *c, char digit, unsigned int duration) |
| 517 |
{ |
| 518 |
ast_verb(1, V_BEGIN "Console Received End of Digit %c (duration %u)" V_END, |
| 519 |
digit, duration); |
| 520 |
|
| 521 |
return -1; |
| 522 |
} |
| 523 |
|
| 524 |
static int console_text(struct ast_channel *c, const char *text) |
| 525 |
{ |
| 526 |
ast_verb(1, V_BEGIN "Console Received Text '%s'" V_END, text); |
| 527 |
|
| 528 |
return 0; |
| 529 |
} |
| 530 |
|
| 531 |
static int console_hangup(struct ast_channel *c) |
| 532 |
{ |
| 533 |
struct console_pvt *pvt = ast_channel_tech_pvt(c); |
| 534 |
|
| 535 |
ast_verb(1, V_BEGIN "Hangup on Console" V_END); |
| 536 |
|
| 537 |
pvt->hookstate = 0; |
| 538 |
pvt->owner = NULL; |
| 539 |
stop_stream(pvt); |
| 540 |
|
| 541 |
ast_channel_tech_pvt_set(c, unref_pvt(pvt)); |
| 542 |
|
| 543 |
return 0; |
| 544 |
} |
| 545 |
|
| 546 |
static int console_answer(struct ast_channel *c) |
| 547 |
{ |
| 548 |
struct console_pvt *pvt = ast_channel_tech_pvt(c); |
| 549 |
|
| 550 |
ast_verb(1, V_BEGIN "Call from Console has been Answered" V_END); |
| 551 |
|
| 552 |
ast_setstate(c, AST_STATE_UP); |
| 553 |
|
| 554 |
return start_stream(pvt); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
static struct ast_frame *console_read(struct ast_channel *chan) |
| 578 |
{ |
| 579 |
ast_debug(1, "I should not be called ...\n"); |
| 580 |
|
| 581 |
return &ast_null_frame; |
| 582 |
} |
| 583 |
|
| 584 |
static int console_call(struct ast_channel *c, const char *dest, int timeout) |
| 585 |
{ |
| 586 |
struct console_pvt *pvt = ast_channel_tech_pvt(c); |
| 587 |
enum ast_control_frame_type ctrl; |
| 588 |
|
| 589 |
ast_verb(1, V_BEGIN "Call to device '%s' on console from '%s' <%s>" V_END, |
| 590 |
dest, |
| 591 |
S_COR(ast_channel_caller(c)->id.name.valid, ast_channel_caller(c)->id.name.str, ""), |
| 592 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, "")); |
| 593 |
|
| 594 |
console_pvt_lock(pvt); |
| 595 |
|
| 596 |
if (pvt->autoanswer) { |
| 597 |
pvt->hookstate = 1; |
| 598 |
console_pvt_unlock(pvt); |
| 599 |
ast_verb(1, V_BEGIN "Auto-answered" V_END); |
| 600 |
ctrl = AST_CONTROL_ANSWER; |
| 601 |
} else { |
| 602 |
console_pvt_unlock(pvt); |
| 603 |
ast_verb(1, V_BEGIN "Type 'console answer' to answer, or use the 'autoanswer' option " |
| 604 |
"for future calls" V_END); |
| 605 |
ctrl = AST_CONTROL_RINGING; |
| 606 |
ast_indicate(c, AST_CONTROL_RINGING); |
| 607 |
} |
| 608 |
|
| 609 |
ast_queue_control(c, ctrl); |
| 610 |
|
| 611 |
return start_stream(pvt); |
| 612 |
} |
| 613 |
|
| 614 |
static int console_write(struct ast_channel *chan, struct ast_frame *f) |
| 615 |
{ |
| 616 |
struct console_pvt *pvt = ast_channel_tech_pvt(chan); |
| 617 |
|
| 618 |
console_pvt_lock(pvt); |
| 619 |
Pa_WriteStream(pvt->stream, f->data.ptr, f->samples); |
| 620 |
console_pvt_unlock(pvt); |
| 621 |
|
| 622 |
return 0; |
| 623 |
} |
| 624 |
|
| 625 |
static int console_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen) |
| 626 |
{ |
| 627 |
struct console_pvt *pvt = ast_channel_tech_pvt(chan); |
| 628 |
int res = 0; |
| 629 |
|
| 630 |
switch (cond) { |
| 631 |
case AST_CONTROL_BUSY: |
| 632 |
case AST_CONTROL_CONGESTION: |
| 633 |
case AST_CONTROL_RINGING: |
| 634 |
case AST_CONTROL_INCOMPLETE: |
| 635 |
case AST_CONTROL_PVT_CAUSE_CODE: |
| 636 |
case -1: |
| 637 |
res = -1; |
| 638 |
break; |
| 639 |
case AST_CONTROL_PROGRESS: |
| 640 |
case AST_CONTROL_PROCEEDING: |
| 641 |
case AST_CONTROL_VIDUPDATE: |
| 642 |
case AST_CONTROL_SRCUPDATE: |
| 643 |
break; |
| 644 |
case AST_CONTROL_HOLD: |
| 645 |
ast_verb(1, V_BEGIN "Console Has Been Placed on Hold" V_END); |
| 646 |
ast_moh_start(chan, data, pvt->mohinterpret); |
| 647 |
break; |
| 648 |
case AST_CONTROL_UNHOLD: |
| 649 |
ast_verb(1, V_BEGIN "Console Has Been Retrieved from Hold" V_END); |
| 650 |
ast_moh_stop(chan); |
| 651 |
break; |
| 652 |
default: |
| 653 |
ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", |
| 654 |
cond, ast_channel_name(chan)); |
| 655 |
|
| 656 |
res = -1; |
| 657 |
} |
| 658 |
|
| 659 |
return res; |
| 660 |
} |
| 661 |
|
| 662 |
static int console_fixup(struct ast_channel *oldchan, struct ast_channel *newchan) |
| 663 |
{ |
| 664 |
struct console_pvt *pvt = ast_channel_tech_pvt(newchan); |
| 665 |
|
| 666 |
pvt->owner = newchan; |
| 667 |
|
| 668 |
return 0; |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
static char *ast_ext_ctx(struct console_pvt *pvt, const char *src, char **ext, char **ctx) |
| 683 |
{ |
| 684 |
if (ext == NULL || ctx == NULL) |
| 685 |
return NULL; |
| 686 |
|
| 687 |
*ext = *ctx = NULL; |
| 688 |
|
| 689 |
if (src && *src != '\0') |
| 690 |
*ext = ast_strdup(src); |
| 691 |
|
| 692 |
if (*ext == NULL) |
| 693 |
return NULL; |
| 694 |
|
| 695 |
if (!pvt->overridecontext) { |
| 696 |
|
| 697 |
*ctx = strrchr(*ext, '@'); |
| 698 |
if (*ctx) |
| 699 |
*(*ctx)++ = '\0'; |
| 700 |
} |
| 701 |
|
| 702 |
return *ext; |
| 703 |
} |
| 704 |
|
| 705 |
static struct console_pvt *get_active_pvt(void) |
| 706 |
{ |
| 707 |
struct console_pvt *pvt; |
| 708 |
|
| 709 |
ast_rwlock_rdlock(&active_lock); |
| 710 |
pvt = ref_pvt(active_pvt); |
| 711 |
ast_rwlock_unlock(&active_lock); |
| 712 |
|
| 713 |
return pvt; |
| 714 |
} |
| 715 |
|
| 716 |
static char *cli_console_autoanswer(struct ast_cli_entry *e, int cmd, |
| 717 |
struct ast_cli_args *a) |
| 718 |
{ |
| 719 |
struct console_pvt *pvt; |
| 720 |
char *res = CLI_SUCCESS; |
| 721 |
|
| 722 |
switch (cmd) { |
| 723 |
case CLI_INIT: |
| 724 |
e->command = "console {set|show} autoanswer [on|off]"; |
| 725 |
e->usage = |
| 726 |
"Usage: console {set|show} autoanswer [on|off]\n" |
| 727 |
" Enables or disables autoanswer feature. If used without\n" |
| 728 |
" argument, displays the current on/off status of autoanswer.\n" |
| 729 |
" The default value of autoanswer is in 'oss.conf'.\n"; |
| 730 |
return NULL; |
| 731 |
|
| 732 |
case CLI_GENERATE: |
| 733 |
return NULL; |
| 734 |
} |
| 735 |
|
| 736 |
pvt = get_active_pvt(); |
| 737 |
if (!pvt) { |
| 738 |
ast_cli(a->fd, "No console device is set as active.\n"); |
| 739 |
return CLI_FAILURE; |
| 740 |
} |
| 741 |
|
| 742 |
if (a->argc == e->args - 1) { |
| 743 |
ast_cli(a->fd, "Auto answer is %s.\n", pvt->autoanswer ? "on" : "off"); |
| 744 |
unref_pvt(pvt); |
| 745 |
return CLI_SUCCESS; |
| 746 |
} |
| 747 |
|
| 748 |
if (a->argc != e->args) { |
| 749 |
unref_pvt(pvt); |
| 750 |
return CLI_SHOWUSAGE; |
| 751 |
} |
| 752 |
|
| 753 |
if (!strcasecmp(a->argv[e->args-1], "on")) |
| 754 |
pvt->autoanswer = 1; |
| 755 |
else if (!strcasecmp(a->argv[e->args - 1], "off")) |
| 756 |
pvt->autoanswer = 0; |
| 757 |
else |
| 758 |
res = CLI_SHOWUSAGE; |
| 759 |
|
| 760 |
unref_pvt(pvt); |
| 761 |
|
| 762 |
return res; |
| 763 |
} |
| 764 |
|
| 765 |
static char *cli_console_flash(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 766 |
{ |
| 767 |
struct console_pvt *pvt; |
| 768 |
|
| 769 |
if (cmd == CLI_INIT) { |
| 770 |
e->command = "console flash"; |
| 771 |
e->usage = |
| 772 |
"Usage: console flash\n" |
| 773 |
" Flashes the call currently placed on the console.\n"; |
| 774 |
return NULL; |
| 775 |
} else if (cmd == CLI_GENERATE) { |
| 776 |
return NULL; |
| 777 |
} |
| 778 |
|
| 779 |
if (a->argc != e->args) { |
| 780 |
return CLI_SHOWUSAGE; |
| 781 |
} |
| 782 |
|
| 783 |
pvt = get_active_pvt(); |
| 784 |
if (!pvt) { |
| 785 |
ast_cli(a->fd, "No console device is set as active\n"); |
| 786 |
return CLI_FAILURE; |
| 787 |
} |
| 788 |
|
| 789 |
if (!pvt->owner) { |
| 790 |
ast_cli(a->fd, "No call to flash\n"); |
| 791 |
unref_pvt(pvt); |
| 792 |
return CLI_FAILURE; |
| 793 |
} |
| 794 |
|
| 795 |
pvt->hookstate = 0; |
| 796 |
|
| 797 |
ast_queue_control(pvt->owner, AST_CONTROL_FLASH); |
| 798 |
|
| 799 |
unref_pvt(pvt); |
| 800 |
|
| 801 |
return CLI_SUCCESS; |
| 802 |
} |
| 803 |
|
| 804 |
static char *cli_console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 805 |
{ |
| 806 |
char *s = NULL; |
| 807 |
const char *mye = NULL, *myc = NULL; |
| 808 |
struct console_pvt *pvt; |
| 809 |
|
| 810 |
if (cmd == CLI_INIT) { |
| 811 |
e->command = "console dial"; |
| 812 |
e->usage = |
| 813 |
"Usage: console dial [extension[@context]]\n" |
| 814 |
" Dials a given extension (and context if specified)\n"; |
| 815 |
return NULL; |
| 816 |
} else if (cmd == CLI_GENERATE) { |
| 817 |
return NULL; |
| 818 |
} |
| 819 |
|
| 820 |
if (a->argc > e->args + 1) { |
| 821 |
return CLI_SHOWUSAGE; |
| 822 |
} |
| 823 |
|
| 824 |
pvt = get_active_pvt(); |
| 825 |
if (!pvt) { |
| 826 |
ast_cli(a->fd, "No console device is currently set as active\n"); |
| 827 |
return CLI_FAILURE; |
| 828 |
} |
| 829 |
|
| 830 |
if (pvt->owner) { |
| 831 |
int i; |
| 832 |
struct ast_frame f = { AST_FRAME_DTMF }; |
| 833 |
const char *s; |
| 834 |
|
| 835 |
if (a->argc == e->args) { |
| 836 |
ast_cli(a->fd, "Already in a call. You can only dial digits until you hangup.\n"); |
| 837 |
unref_pvt(pvt); |
| 838 |
return CLI_FAILURE; |
| 839 |
} |
| 840 |
s = a->argv[e->args]; |
| 841 |
|
| 842 |
for (i = 0; i < strlen(s); i++) { |
| 843 |
f.subclass.integer = s[i]; |
| 844 |
ast_queue_frame(pvt->owner, &f); |
| 845 |
} |
| 846 |
unref_pvt(pvt); |
| 847 |
return CLI_SUCCESS; |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
if (a->argc == e->args + 1) { |
| 852 |
char *ext = NULL, *con = NULL; |
| 853 |
s = ast_ext_ctx(pvt, a->argv[e->args], &ext, &con); |
| 854 |
mye = ext; |
| 855 |
myc = con; |
| 856 |
ast_debug(1, "provided '%s', exten '%s' context '%s'\n", |
| 857 |
a->argv[e->args], mye, myc); |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
if (ast_strlen_zero(mye)) |
| 862 |
mye = pvt->exten; |
| 863 |
if (ast_strlen_zero(myc)) |
| 864 |
myc = pvt->context; |
| 865 |
|
| 866 |
if (ast_exists_extension(NULL, myc, mye, 1, NULL)) { |
| 867 |
console_pvt_lock(pvt); |
| 868 |
pvt->hookstate = 1; |
| 869 |
console_new(pvt, mye, myc, AST_STATE_RINGING, NULL, NULL); |
| 870 |
console_pvt_unlock(pvt); |
| 871 |
} else |
| 872 |
ast_cli(a->fd, "No such extension '%s' in context '%s'\n", mye, myc); |
| 873 |
|
| 874 |
ast_free(s); |
| 875 |
|
| 876 |
unref_pvt(pvt); |
| 877 |
|
| 878 |
return CLI_SUCCESS; |
| 879 |
} |
| 880 |
|
| 881 |
static char *cli_console_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 882 |
{ |
| 883 |
struct console_pvt *pvt; |
| 884 |
|
| 885 |
if (cmd == CLI_INIT) { |
| 886 |
e->command = "console hangup"; |
| 887 |
e->usage = |
| 888 |
"Usage: console hangup\n" |
| 889 |
" Hangs up any call currently placed on the console.\n"; |
| 890 |
return NULL; |
| 891 |
} else if (cmd == CLI_GENERATE) { |
| 892 |
return NULL; |
| 893 |
} |
| 894 |
|
| 895 |
if (a->argc != e->args) { |
| 896 |
return CLI_SHOWUSAGE; |
| 897 |
} |
| 898 |
|
| 899 |
pvt = get_active_pvt(); |
| 900 |
if (!pvt) { |
| 901 |
ast_cli(a->fd, "No console device is set as active\n"); |
| 902 |
return CLI_FAILURE; |
| 903 |
} |
| 904 |
|
| 905 |
if (!pvt->owner && !pvt->hookstate) { |
| 906 |
ast_cli(a->fd, "No call to hang up\n"); |
| 907 |
unref_pvt(pvt); |
| 908 |
return CLI_FAILURE; |
| 909 |
} |
| 910 |
|
| 911 |
pvt->hookstate = 0; |
| 912 |
if (pvt->owner) |
| 913 |
ast_queue_hangup(pvt->owner); |
| 914 |
|
| 915 |
unref_pvt(pvt); |
| 916 |
|
| 917 |
return CLI_SUCCESS; |
| 918 |
} |
| 919 |
|
| 920 |
static char *cli_console_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 921 |
{ |
| 922 |
const char *s; |
| 923 |
struct console_pvt *pvt; |
| 924 |
char *res = CLI_SUCCESS; |
| 925 |
|
| 926 |
if (cmd == CLI_INIT) { |
| 927 |
e->command = "console {mute|unmute}"; |
| 928 |
e->usage = |
| 929 |
"Usage: console {mute|unmute}\n" |
| 930 |
" Mute/unmute the microphone.\n"; |
| 931 |
return NULL; |
| 932 |
} else if (cmd == CLI_GENERATE) { |
| 933 |
return NULL; |
| 934 |
} |
| 935 |
|
| 936 |
if (a->argc != e->args) { |
| 937 |
return CLI_SHOWUSAGE; |
| 938 |
} |
| 939 |
|
| 940 |
pvt = get_active_pvt(); |
| 941 |
if (!pvt) { |
| 942 |
ast_cli(a->fd, "No console device is set as active\n"); |
| 943 |
return CLI_FAILURE; |
| 944 |
} |
| 945 |
|
| 946 |
s = a->argv[e->args-1]; |
| 947 |
if (!strcasecmp(s, "mute")) |
| 948 |
pvt->muted = 1; |
| 949 |
else if (!strcasecmp(s, "unmute")) |
| 950 |
pvt->muted = 0; |
| 951 |
else |
| 952 |
res = CLI_SHOWUSAGE; |
| 953 |
|
| 954 |
ast_verb(1, V_BEGIN "The Console is now %s" V_END, |
| 955 |
pvt->muted ? "Muted" : "Unmuted"); |
| 956 |
|
| 957 |
unref_pvt(pvt); |
| 958 |
|
| 959 |
return res; |
| 960 |
} |
| 961 |
|
| 962 |
static char *cli_list_available(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 963 |
{ |
| 964 |
PaDeviceIndex idx, num, def_input, def_output; |
| 965 |
|
| 966 |
if (cmd == CLI_INIT) { |
| 967 |
e->command = "console list available"; |
| 968 |
e->usage = |
| 969 |
"Usage: console list available\n" |
| 970 |
" List all available devices.\n"; |
| 971 |
return NULL; |
| 972 |
} else if (cmd == CLI_GENERATE) |
| 973 |
return NULL; |
| 974 |
|
| 975 |
if (a->argc != e->args) |
| 976 |
return CLI_SHOWUSAGE; |
| 977 |
|
| 978 |
ast_cli(a->fd, "\n" |
| 979 |
"=============================================================\n" |
| 980 |
"=== Available Devices =======================================\n" |
| 981 |
"=============================================================\n" |
| 982 |
"===\n"); |
| 983 |
|
| 984 |
num = Pa_GetDeviceCount(); |
| 985 |
if (!num) { |
| 986 |
ast_cli(a->fd, "(None)\n"); |
| 987 |
return CLI_SUCCESS; |
| 988 |
} |
| 989 |
|
| 990 |
def_input = Pa_GetDefaultInputDevice(); |
| 991 |
def_output = Pa_GetDefaultOutputDevice(); |
| 992 |
for (idx = 0; idx < num; idx++) { |
| 993 |
const PaDeviceInfo *dev = Pa_GetDeviceInfo(idx); |
| 994 |
if (!dev) |
| 995 |
continue; |
| 996 |
ast_cli(a->fd, "=== ---------------------------------------------------------\n" |
| 997 |
"=== Device Name: %s\n", dev->name); |
| 998 |
if (dev->maxInputChannels) |
| 999 |
ast_cli(a->fd, "=== ---> %sInput Device\n", (idx == def_input) ? "Default " : ""); |
| 1000 |
if (dev->maxOutputChannels) |
| 1001 |
ast_cli(a->fd, "=== ---> %sOutput Device\n", (idx == def_output) ? "Default " : ""); |
| 1002 |
ast_cli(a->fd, "=== ---------------------------------------------------------\n===\n"); |
| 1003 |
} |
| 1004 |
|
| 1005 |
ast_cli(a->fd, "=============================================================\n\n"); |
| 1006 |
|
| 1007 |
return CLI_SUCCESS; |
| 1008 |
} |
| 1009 |
|
| 1010 |
static char *cli_list_devices(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 1011 |
{ |
| 1012 |
struct ao2_iterator i; |
| 1013 |
struct console_pvt *pvt; |
| 1014 |
|
| 1015 |
if (cmd == CLI_INIT) { |
| 1016 |
e->command = "console list devices"; |
| 1017 |
e->usage = |
| 1018 |
"Usage: console list devices\n" |
| 1019 |
" List all configured devices.\n"; |
| 1020 |
return NULL; |
| 1021 |
} else if (cmd == CLI_GENERATE) |
| 1022 |
return NULL; |
| 1023 |
|
| 1024 |
if (a->argc != e->args) |
| 1025 |
return CLI_SHOWUSAGE; |
| 1026 |
|
| 1027 |
ast_cli(a->fd, "\n" |
| 1028 |
"=============================================================\n" |
| 1029 |
"=== Configured Devices ======================================\n" |
| 1030 |
"=============================================================\n" |
| 1031 |
"===\n"); |
| 1032 |
|
| 1033 |
i = ao2_iterator_init(pvts, 0); |
| 1034 |
while ((pvt = ao2_iterator_next(&i))) { |
| 1035 |
console_pvt_lock(pvt); |
| 1036 |
|
| 1037 |
ast_cli(a->fd, "=== ---------------------------------------------------------\n" |
| 1038 |
"=== Device Name: %s\n" |
| 1039 |
"=== ---> Active: %s\n" |
| 1040 |
"=== ---> Input Device: %s\n" |
| 1041 |
"=== ---> Output Device: %s\n" |
| 1042 |
"=== ---> Context: %s\n" |
| 1043 |
"=== ---> Extension: %s\n" |
| 1044 |
"=== ---> CallerID Num: %s\n" |
| 1045 |
"=== ---> CallerID Name: %s\n" |
| 1046 |
"=== ---> MOH Interpret: %s\n" |
| 1047 |
"=== ---> Language: %s\n" |
| 1048 |
"=== ---> Parkinglot: %s\n" |
| 1049 |
"=== ---> Muted: %s\n" |
| 1050 |
"=== ---> Auto-Answer: %s\n" |
| 1051 |
"=== ---> Override Context: %s\n" |
| 1052 |
"=== ---------------------------------------------------------\n===\n", |
| 1053 |
pvt->name, (pvt == active_pvt) ? "Yes" : "No", |
| 1054 |
pvt->input_device, pvt->output_device, pvt->context, |
| 1055 |
pvt->exten, pvt->cid_num, pvt->cid_name, pvt->mohinterpret, |
| 1056 |
pvt->language, pvt->parkinglot, pvt->muted ? "Yes" : "No", pvt->autoanswer ? "Yes" : "No", |
| 1057 |
pvt->overridecontext ? "Yes" : "No"); |
| 1058 |
|
| 1059 |
console_pvt_unlock(pvt); |
| 1060 |
unref_pvt(pvt); |
| 1061 |
} |
| 1062 |
ao2_iterator_destroy(&i); |
| 1063 |
|
| 1064 |
ast_cli(a->fd, "=============================================================\n\n"); |
| 1065 |
|
| 1066 |
return CLI_SUCCESS; |
| 1067 |
} |
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
static char *cli_console_answer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 1072 |
{ |
| 1073 |
struct console_pvt *pvt; |
| 1074 |
|
| 1075 |
switch (cmd) { |
| 1076 |
case CLI_INIT: |
| 1077 |
e->command = "console answer"; |
| 1078 |
e->usage = |
| 1079 |
"Usage: console answer\n" |
| 1080 |
" Answers an incoming call on the console channel.\n"; |
| 1081 |
return NULL; |
| 1082 |
|
| 1083 |
case CLI_GENERATE: |
| 1084 |
return NULL; |
| 1085 |
} |
| 1086 |
|
| 1087 |
pvt = get_active_pvt(); |
| 1088 |
if (!pvt) { |
| 1089 |
ast_cli(a->fd, "No console device is set as active\n"); |
| 1090 |
return CLI_FAILURE; |
| 1091 |
} |
| 1092 |
|
| 1093 |
if (a->argc != e->args) { |
| 1094 |
unref_pvt(pvt); |
| 1095 |
return CLI_SHOWUSAGE; |
| 1096 |
} |
| 1097 |
|
| 1098 |
if (!pvt->owner) { |
| 1099 |
ast_cli(a->fd, "No one is calling us\n"); |
| 1100 |
unref_pvt(pvt); |
| 1101 |
return CLI_FAILURE; |
| 1102 |
} |
| 1103 |
|
| 1104 |
pvt->hookstate = 1; |
| 1105 |
|
| 1106 |
ast_indicate(pvt->owner, -1); |
| 1107 |
|
| 1108 |
ast_queue_control(pvt->owner, AST_CONTROL_ANSWER); |
| 1109 |
|
| 1110 |
unref_pvt(pvt); |
| 1111 |
|
| 1112 |
return CLI_SUCCESS; |
| 1113 |
} |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
static char *cli_console_sendtext(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 1122 |
{ |
| 1123 |
char buf[TEXT_SIZE]; |
| 1124 |
struct console_pvt *pvt; |
| 1125 |
struct ast_frame f = { |
| 1126 |
.frametype = AST_FRAME_TEXT, |
| 1127 |
.data.ptr = buf, |
| 1128 |
.src = "console_send_text", |
| 1129 |
}; |
| 1130 |
int len; |
| 1131 |
|
| 1132 |
if (cmd == CLI_INIT) { |
| 1133 |
e->command = "console send text"; |
| 1134 |
e->usage = |
| 1135 |
"Usage: console send text <message>\n" |
| 1136 |
" Sends a text message for display on the remote terminal.\n"; |
| 1137 |
return NULL; |
| 1138 |
} else if (cmd == CLI_GENERATE) { |
| 1139 |
return NULL; |
| 1140 |
} |
| 1141 |
|
| 1142 |
pvt = get_active_pvt(); |
| 1143 |
if (!pvt) { |
| 1144 |
ast_cli(a->fd, "No console device is set as active\n"); |
| 1145 |
return CLI_FAILURE; |
| 1146 |
} |
| 1147 |
|
| 1148 |
if (a->argc < e->args + 1) { |
| 1149 |
unref_pvt(pvt); |
| 1150 |
return CLI_SHOWUSAGE; |
| 1151 |
} |
| 1152 |
|
| 1153 |
if (!pvt->owner) { |
| 1154 |
ast_cli(a->fd, "Not in a call\n"); |
| 1155 |
unref_pvt(pvt); |
| 1156 |
return CLI_FAILURE; |
| 1157 |
} |
| 1158 |
|
| 1159 |
ast_join(buf, sizeof(buf) - 1, a->argv + e->args); |
| 1160 |
if (ast_strlen_zero(buf)) { |
| 1161 |
unref_pvt(pvt); |
| 1162 |
return CLI_SHOWUSAGE; |
| 1163 |
} |
| 1164 |
|
| 1165 |
len = strlen(buf); |
| 1166 |
buf[len] = '\n'; |
| 1167 |
f.datalen = len + 1; |
| 1168 |
|
| 1169 |
ast_queue_frame(pvt->owner, &f); |
| 1170 |
|
| 1171 |
unref_pvt(pvt); |
| 1172 |
|
| 1173 |
return CLI_SUCCESS; |
| 1174 |
} |
| 1175 |
|
| 1176 |
static void set_active(struct console_pvt *pvt, const char *value) |
| 1177 |
{ |
| 1178 |
if (pvt == &globals) { |
| 1179 |
ast_log(LOG_ERROR, "active is only valid as a per-device setting\n"); |
| 1180 |
return; |
| 1181 |
} |
| 1182 |
|
| 1183 |
if (!ast_true(value)) |
| 1184 |
return; |
| 1185 |
|
| 1186 |
ast_rwlock_wrlock(&active_lock); |
| 1187 |
if (active_pvt) |
| 1188 |
unref_pvt(active_pvt); |
| 1189 |
active_pvt = ref_pvt(pvt); |
| 1190 |
ast_rwlock_unlock(&active_lock); |
| 1191 |
} |
| 1192 |
|
| 1193 |
static char *cli_console_active(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 1194 |
{ |
| 1195 |
struct console_pvt *pvt; |
| 1196 |
|
| 1197 |
switch (cmd) { |
| 1198 |
case CLI_INIT: |
| 1199 |
e->command = "console {set|show} active"; |
| 1200 |
e->usage = |
| 1201 |
"Usage: console {set|show} active [<device>]\n" |
| 1202 |
" Set or show the active console device for the Asterisk CLI.\n"; |
| 1203 |
return NULL; |
| 1204 |
case CLI_GENERATE: |
| 1205 |
if (a->pos == e->args) { |
| 1206 |
struct ao2_iterator i; |
| 1207 |
int x = 0; |
| 1208 |
char *res = NULL; |
| 1209 |
i = ao2_iterator_init(pvts, 0); |
| 1210 |
while ((pvt = ao2_iterator_next(&i))) { |
| 1211 |
if (++x > a->n && !strncasecmp(pvt->name, a->word, strlen(a->word))) |
| 1212 |
res = ast_strdup(pvt->name); |
| 1213 |
unref_pvt(pvt); |
| 1214 |
if (res) { |
| 1215 |
ao2_iterator_destroy(&i); |
| 1216 |
return res; |
| 1217 |
} |
| 1218 |
} |
| 1219 |
ao2_iterator_destroy(&i); |
| 1220 |
} |
| 1221 |
return NULL; |
| 1222 |
} |
| 1223 |
|
| 1224 |
if (a->argc < e->args) |
| 1225 |
return CLI_SHOWUSAGE; |
| 1226 |
|
| 1227 |
if (a->argc == 3) { |
| 1228 |
pvt = get_active_pvt(); |
| 1229 |
|
| 1230 |
if (!pvt) |
| 1231 |
ast_cli(a->fd, "No device is currently set as the active console device.\n"); |
| 1232 |
else { |
| 1233 |
console_pvt_lock(pvt); |
| 1234 |
ast_cli(a->fd, "The active console device is '%s'.\n", pvt->name); |
| 1235 |
console_pvt_unlock(pvt); |
| 1236 |
pvt = unref_pvt(pvt); |
| 1237 |
} |
| 1238 |
|
| 1239 |
return CLI_SUCCESS; |
| 1240 |
} |
| 1241 |
|
| 1242 |
if (!(pvt = find_pvt(a->argv[e->args]))) { |
| 1243 |
ast_cli(a->fd, "Could not find a device called '%s'.\n", a->argv[e->args]); |
| 1244 |
return CLI_FAILURE; |
| 1245 |
} |
| 1246 |
|
| 1247 |
set_active(pvt, "yes"); |
| 1248 |
|
| 1249 |
console_pvt_lock(pvt); |
| 1250 |
ast_cli(a->fd, "The active console device has been set to '%s'\n", pvt->name); |
| 1251 |
console_pvt_unlock(pvt); |
| 1252 |
|
| 1253 |
unref_pvt(pvt); |
| 1254 |
|
| 1255 |
return CLI_SUCCESS; |
| 1256 |
} |
| 1257 |
|
| 1258 |
static struct ast_cli_entry cli_console[] = { |
| 1259 |
AST_CLI_DEFINE(cli_console_dial, "Dial an extension from the console"), |
| 1260 |
AST_CLI_DEFINE(cli_console_hangup, "Hangup a call on the console"), |
| 1261 |
AST_CLI_DEFINE(cli_console_mute, "Disable/Enable mic input"), |
| 1262 |
AST_CLI_DEFINE(cli_console_answer, "Answer an incoming console call"), |
| 1263 |
AST_CLI_DEFINE(cli_console_sendtext, "Send text to a connected party"), |
| 1264 |
AST_CLI_DEFINE(cli_console_flash, "Send a flash to the connected party"), |
| 1265 |
AST_CLI_DEFINE(cli_console_autoanswer, "Turn autoanswer on or off"), |
| 1266 |
AST_CLI_DEFINE(cli_list_available, "List available devices"), |
| 1267 |
AST_CLI_DEFINE(cli_list_devices, "List configured devices"), |
| 1268 |
AST_CLI_DEFINE(cli_console_active, "View or Set the active console device"), |
| 1269 |
}; |
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
static void set_pvt_defaults(struct console_pvt *pvt) |
| 1277 |
{ |
| 1278 |
if (pvt == &globals) { |
| 1279 |
ast_string_field_set(pvt, mohinterpret, "default"); |
| 1280 |
ast_string_field_set(pvt, context, "default"); |
| 1281 |
ast_string_field_set(pvt, exten, "s"); |
| 1282 |
ast_string_field_set(pvt, language, ""); |
| 1283 |
ast_string_field_set(pvt, cid_num, ""); |
| 1284 |
ast_string_field_set(pvt, cid_name, ""); |
| 1285 |
ast_string_field_set(pvt, parkinglot, ""); |
| 1286 |
|
| 1287 |
pvt->overridecontext = 0; |
| 1288 |
pvt->autoanswer = 0; |
| 1289 |
} else { |
| 1290 |
ast_mutex_lock(&globals_lock); |
| 1291 |
|
| 1292 |
ast_string_field_set(pvt, mohinterpret, globals.mohinterpret); |
| 1293 |
ast_string_field_set(pvt, context, globals.context); |
| 1294 |
ast_string_field_set(pvt, exten, globals.exten); |
| 1295 |
ast_string_field_set(pvt, language, globals.language); |
| 1296 |
ast_string_field_set(pvt, cid_num, globals.cid_num); |
| 1297 |
ast_string_field_set(pvt, cid_name, globals.cid_name); |
| 1298 |
ast_string_field_set(pvt, parkinglot, globals.parkinglot); |
| 1299 |
|
| 1300 |
pvt->overridecontext = globals.overridecontext; |
| 1301 |
pvt->autoanswer = globals.autoanswer; |
| 1302 |
|
| 1303 |
ast_mutex_unlock(&globals_lock); |
| 1304 |
} |
| 1305 |
} |
| 1306 |
|
| 1307 |
static void store_callerid(struct console_pvt *pvt, const char *value) |
| 1308 |
{ |
| 1309 |
char cid_name[256]; |
| 1310 |
char cid_num[256]; |
| 1311 |
|
| 1312 |
ast_callerid_split(value, cid_name, sizeof(cid_name), |
| 1313 |
cid_num, sizeof(cid_num)); |
| 1314 |
|
| 1315 |
ast_string_field_set(pvt, cid_name, cid_name); |
| 1316 |
ast_string_field_set(pvt, cid_num, cid_num); |
| 1317 |
} |
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
static void store_config_core(struct console_pvt *pvt, const char *var, const char *value) |
| 1325 |
{ |
| 1326 |
if (pvt == &globals && !ast_jb_read_conf(&global_jbconf, var, value)) |
| 1327 |
return; |
| 1328 |
|
| 1329 |
CV_START(var, value); |
| 1330 |
|
| 1331 |
CV_STRFIELD("context", pvt, context); |
| 1332 |
CV_STRFIELD("extension", pvt, exten); |
| 1333 |
CV_STRFIELD("mohinterpret", pvt, mohinterpret); |
| 1334 |
CV_STRFIELD("language", pvt, language); |
| 1335 |
CV_F("callerid", store_callerid(pvt, value)); |
| 1336 |
CV_BOOL("overridecontext", pvt->overridecontext); |
| 1337 |
CV_BOOL("autoanswer", pvt->autoanswer); |
| 1338 |
CV_STRFIELD("parkinglot", pvt, parkinglot); |
| 1339 |
|
| 1340 |
if (pvt != &globals) { |
| 1341 |
CV_F("active", set_active(pvt, value)) |
| 1342 |
CV_STRFIELD("input_device", pvt, input_device); |
| 1343 |
CV_STRFIELD("output_device", pvt, output_device); |
| 1344 |
} |
| 1345 |
|
| 1346 |
ast_log(LOG_WARNING, "Unknown option '%s'\n", var); |
| 1347 |
|
| 1348 |
CV_END; |
| 1349 |
} |
| 1350 |
|
| 1351 |
static void pvt_destructor(void *obj) |
| 1352 |
{ |
| 1353 |
struct console_pvt *pvt = obj; |
| 1354 |
|
| 1355 |
ast_string_field_free_memory(pvt); |
| 1356 |
} |
| 1357 |
|
| 1358 |
static int init_pvt(struct console_pvt *pvt, const char *name) |
| 1359 |
{ |
| 1360 |
pvt->thread = AST_PTHREADT_NULL; |
| 1361 |
|
| 1362 |
if (ast_string_field_init(pvt, 32)) |
| 1363 |
return -1; |
| 1364 |
|
| 1365 |
ast_string_field_set(pvt, name, S_OR(name, "")); |
| 1366 |
|
| 1367 |
return 0; |
| 1368 |
} |
| 1369 |
|
| 1370 |
static void build_device(struct ast_config *cfg, const char *name) |
| 1371 |
{ |
| 1372 |
struct ast_variable *v; |
| 1373 |
struct console_pvt *pvt; |
| 1374 |
int new = 0; |
| 1375 |
|
| 1376 |
if ((pvt = find_pvt(name))) { |
| 1377 |
console_pvt_lock(pvt); |
| 1378 |
set_pvt_defaults(pvt); |
| 1379 |
pvt->destroy = 0; |
| 1380 |
} else { |
| 1381 |
if (!(pvt = ao2_alloc(sizeof(*pvt), pvt_destructor))) |
| 1382 |
return; |
| 1383 |
init_pvt(pvt, name); |
| 1384 |
set_pvt_defaults(pvt); |
| 1385 |
new = 1; |
| 1386 |
} |
| 1387 |
|
| 1388 |
for (v = ast_variable_browse(cfg, name); v; v = v->next) |
| 1389 |
store_config_core(pvt, v->name, v->value); |
| 1390 |
|
| 1391 |
if (new) |
| 1392 |
ao2_link(pvts, pvt); |
| 1393 |
else |
| 1394 |
console_pvt_unlock(pvt); |
| 1395 |
|
| 1396 |
unref_pvt(pvt); |
| 1397 |
} |
| 1398 |
|
| 1399 |
static int pvt_mark_destroy_cb(void *obj, void *arg, int flags) |
| 1400 |
{ |
| 1401 |
struct console_pvt *pvt = obj; |
| 1402 |
pvt->destroy = 1; |
| 1403 |
return 0; |
| 1404 |
} |
| 1405 |
|
| 1406 |
static void destroy_pvts(void) |
| 1407 |
{ |
| 1408 |
struct ao2_iterator i; |
| 1409 |
struct console_pvt *pvt; |
| 1410 |
|
| 1411 |
i = ao2_iterator_init(pvts, 0); |
| 1412 |
while ((pvt = ao2_iterator_next(&i))) { |
| 1413 |
if (pvt->destroy) { |
| 1414 |
ao2_unlink(pvts, pvt); |
| 1415 |
ast_rwlock_wrlock(&active_lock); |
| 1416 |
if (active_pvt == pvt) |
| 1417 |
active_pvt = unref_pvt(pvt); |
| 1418 |
ast_rwlock_unlock(&active_lock); |
| 1419 |
} |
| 1420 |
unref_pvt(pvt); |
| 1421 |
} |
| 1422 |
ao2_iterator_destroy(&i); |
| 1423 |
} |
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
static int load_config(int reload) |
| 1432 |
{ |
| 1433 |
struct ast_config *cfg; |
| 1434 |
struct ast_variable *v; |
| 1435 |
struct ast_flags config_flags = { 0 }; |
| 1436 |
char *context = NULL; |
| 1437 |
|
| 1438 |
|
| 1439 |
memcpy(&global_jbconf, &default_jbconf, sizeof(global_jbconf)); |
| 1440 |
ast_mutex_lock(&globals_lock); |
| 1441 |
set_pvt_defaults(&globals); |
| 1442 |
ast_mutex_unlock(&globals_lock); |
| 1443 |
|
| 1444 |
if (!(cfg = ast_config_load(config_file, config_flags))) { |
| 1445 |
ast_log(LOG_NOTICE, "Unable to open configuration file %s!\n", config_file); |
| 1446 |
return -1; |
| 1447 |
} else if (cfg == CONFIG_STATUS_FILEINVALID) { |
| 1448 |
ast_log(LOG_NOTICE, "Config file %s has an invalid format\n", config_file); |
| 1449 |
return -1; |
| 1450 |
} |
| 1451 |
|
| 1452 |
ao2_callback(pvts, OBJ_NODATA, pvt_mark_destroy_cb, NULL); |
| 1453 |
|
| 1454 |
ast_mutex_lock(&globals_lock); |
| 1455 |
for (v = ast_variable_browse(cfg, "general"); v; v = v->next) |
| 1456 |
store_config_core(&globals, v->name, v->value); |
| 1457 |
ast_mutex_unlock(&globals_lock); |
| 1458 |
|
| 1459 |
while ((context = ast_category_browse(cfg, context))) { |
| 1460 |
if (strcasecmp(context, "general")) |
| 1461 |
build_device(cfg, context); |
| 1462 |
} |
| 1463 |
|
| 1464 |
ast_config_destroy(cfg); |
| 1465 |
|
| 1466 |
destroy_pvts(); |
| 1467 |
|
| 1468 |
return 0; |
| 1469 |
} |
| 1470 |
|
| 1471 |
static int pvt_hash_cb(const void *obj, const int flags) |
| 1472 |
{ |
| 1473 |
const struct console_pvt *pvt = obj; |
| 1474 |
|
| 1475 |
return ast_str_case_hash(pvt->name); |
| 1476 |
} |
| 1477 |
|
| 1478 |
static int pvt_cmp_cb(void *obj, void *arg, int flags) |
| 1479 |
{ |
| 1480 |
struct console_pvt *pvt = obj, *pvt2 = arg; |
| 1481 |
|
| 1482 |
return !strcasecmp(pvt->name, pvt2->name) ? CMP_MATCH | CMP_STOP : 0; |
| 1483 |
} |
| 1484 |
|
| 1485 |
static void stop_streams(void) |
| 1486 |
{ |
| 1487 |
struct console_pvt *pvt; |
| 1488 |
struct ao2_iterator i; |
| 1489 |
|
| 1490 |
i = ao2_iterator_init(pvts, 0); |
| 1491 |
while ((pvt = ao2_iterator_next(&i))) { |
| 1492 |
if (pvt->hookstate) |
| 1493 |
stop_stream(pvt); |
| 1494 |
unref_pvt(pvt); |
| 1495 |
} |
| 1496 |
ao2_iterator_destroy(&i); |
| 1497 |
} |
| 1498 |
|
| 1499 |
static int unload_module(void) |
| 1500 |
{ |
| 1501 |
ao2_ref(console_tech.capabilities, -1); |
| 1502 |
console_tech.capabilities = NULL; |
| 1503 |
ast_channel_unregister(&console_tech); |
| 1504 |
ast_cli_unregister_multiple(cli_console, ARRAY_LEN(cli_console)); |
| 1505 |
|
| 1506 |
stop_streams(); |
| 1507 |
|
| 1508 |
Pa_Terminate(); |
| 1509 |
|
| 1510 |
|
| 1511 |
ao2_ref(pvts, -1); |
| 1512 |
|
| 1513 |
pvt_destructor(&globals); |
| 1514 |
|
| 1515 |
return 0; |
| 1516 |
} |
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
static int load_module(void) |
| 1529 |
{ |
| 1530 |
PaError res; |
| 1531 |
|
| 1532 |
if (!(console_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) { |
| 1533 |
return AST_MODULE_LOAD_DECLINE; |
| 1534 |
} |
| 1535 |
ast_format_cap_append(console_tech.capabilities, ast_format_slin16, 0); |
| 1536 |
|
| 1537 |
init_pvt(&globals, NULL); |
| 1538 |
|
| 1539 |
pvts = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NUM_PVT_BUCKETS, |
| 1540 |
pvt_hash_cb, NULL, pvt_cmp_cb); |
| 1541 |
if (!pvts) |
| 1542 |
goto return_error; |
| 1543 |
|
| 1544 |
if (load_config(0)) |
| 1545 |
goto return_error; |
| 1546 |
|
| 1547 |
res = Pa_Initialize(); |
| 1548 |
if (res != paNoError) { |
| 1549 |
ast_log(LOG_WARNING, "Failed to initialize audio system - (%d) %s\n", |
| 1550 |
res, Pa_GetErrorText(res)); |
| 1551 |
goto return_error_pa_init; |
| 1552 |
} |
| 1553 |
|
| 1554 |
if (ast_channel_register(&console_tech)) { |
| 1555 |
ast_log(LOG_ERROR, "Unable to register channel type 'Console'\n"); |
| 1556 |
goto return_error_chan_reg; |
| 1557 |
} |
| 1558 |
|
| 1559 |
if (ast_cli_register_multiple(cli_console, ARRAY_LEN(cli_console))) |
| 1560 |
goto return_error_cli_reg; |
| 1561 |
|
| 1562 |
return AST_MODULE_LOAD_SUCCESS; |
| 1563 |
|
| 1564 |
return_error_cli_reg: |
| 1565 |
ast_cli_unregister_multiple(cli_console, ARRAY_LEN(cli_console)); |
| 1566 |
return_error_chan_reg: |
| 1567 |
ast_channel_unregister(&console_tech); |
| 1568 |
return_error_pa_init: |
| 1569 |
Pa_Terminate(); |
| 1570 |
return_error: |
| 1571 |
if (pvts) |
| 1572 |
ao2_ref(pvts, -1); |
| 1573 |
pvts = NULL; |
| 1574 |
ao2_ref(console_tech.capabilities, -1); |
| 1575 |
console_tech.capabilities = NULL; |
| 1576 |
pvt_destructor(&globals); |
| 1577 |
|
| 1578 |
return AST_MODULE_LOAD_DECLINE; |
| 1579 |
} |
| 1580 |
|
| 1581 |
static int reload(void) |
| 1582 |
{ |
| 1583 |
return load_config(1); |
| 1584 |
} |
| 1585 |
|
| 1586 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Console Channel Driver", |
| 1587 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 1588 |
.load = load_module, |
| 1589 |
.unload = unload_module, |
| 1590 |
.reload = reload, |
| 1591 |
.load_pri = AST_MODPRI_CHANNEL_DRIVER, |
| 1592 |
); |