00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "asterisk.h"
00030
00031 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00032
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <unistd.h>
00037 #include <sys/socket.h>
00038 #include <netinet/in.h>
00039 #include <netinet/tcp.h>
00040 #include <sys/ioctl.h>
00041 #include <net/if.h>
00042 #include <errno.h>
00043 #include <fcntl.h>
00044 #include <netdb.h>
00045 #include <arpa/inet.h>
00046 #include <sys/signal.h>
00047 #include <signal.h>
00048 #include <ctype.h>
00049
00050 #include "asterisk/lock.h"
00051 #include "asterisk/channel.h"
00052 #include "asterisk/config.h"
00053 #include "asterisk/logger.h"
00054 #include "asterisk/module.h"
00055 #include "asterisk/pbx.h"
00056 #include "asterisk/options.h"
00057 #include "asterisk/lock.h"
00058 #include "asterisk/sched.h"
00059 #include "asterisk/io.h"
00060 #include "asterisk/rtp.h"
00061 #include "asterisk/acl.h"
00062 #include "asterisk/callerid.h"
00063 #include "asterisk/cli.h"
00064 #include "asterisk/say.h"
00065 #include "asterisk/cdr.h"
00066 #include "asterisk/astdb.h"
00067 #include "asterisk/features.h"
00068 #include "asterisk/app.h"
00069 #include "asterisk/musiconhold.h"
00070 #include "asterisk/utils.h"
00071 #include "asterisk/dsp.h"
00072 #include "asterisk/stringfields.h"
00073 #include "asterisk/astobj.h"
00074 #include "asterisk/abstract_jb.h"
00075 #include "asterisk/threadstorage.h"
00076
00077
00078
00079
00080 static const char tdesc[] = "Skinny Client Control Protocol (Skinny)";
00081 static const char config[] = "skinny.conf";
00082
00083 static int default_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW;
00084 static struct ast_codec_pref default_prefs;
00085
00086 enum skinny_codecs {
00087 SKINNY_CODEC_ALAW = 2,
00088 SKINNY_CODEC_ULAW = 4,
00089 SKINNY_CODEC_G723_1 = 9,
00090 SKINNY_CODEC_G729A = 12,
00091 SKINNY_CODEC_G726_32 = 82,
00092 SKINNY_CODEC_H261 = 100,
00093 SKINNY_CODEC_H263 = 101
00094 };
00095
00096 #define DEFAULT_SKINNY_PORT 2000
00097 #define DEFAULT_SKINNY_BACKLOG 2
00098 #define SKINNY_MAX_PACKET 1000
00099
00100 static int keep_alive = 120;
00101 static char date_format[6] = "D-M-Y";
00102 static char version_id[16] = "P002F202";
00103
00104 #if __BYTE_ORDER == __LITTLE_ENDIAN
00105 #define letohl(x) (x)
00106 #define letohs(x) (x)
00107 #define htolel(x) (x)
00108 #define htoles(x) (x)
00109 #else
00110 #if defined(SOLARIS) || defined(__Darwin__) || defined(__NetBSD__)
00111 #define __bswap_16(x) \
00112 ((((x) & 0xff00) >> 8) | \
00113 (((x) & 0x00ff) << 8))
00114 #define __bswap_32(x) \
00115 ((((x) & 0xff000000) >> 24) | \
00116 (((x) & 0x00ff0000) >> 8) | \
00117 (((x) & 0x0000ff00) << 8) | \
00118 (((x) & 0x000000ff) << 24))
00119 #else
00120 #include <bits/byteswap.h>
00121 #endif
00122 #define letohl(x) __bswap_32(x)
00123 #define letohs(x) __bswap_16(x)
00124 #define htolel(x) __bswap_32(x)
00125 #define htoles(x) __bswap_16(x)
00126 #endif
00127
00128
00129 static struct ast_jb_conf default_jbconf =
00130 {
00131 .flags = 0,
00132 .max_size = -1,
00133 .resync_threshold = -1,
00134 .impl = ""
00135 };
00136 static struct ast_jb_conf global_jbconf;
00137
00138 AST_THREADSTORAGE(device2str_threadbuf, device2str_threadbuf_init);
00139 #define DEVICE2STR_BUFSIZE 15
00140
00141 AST_THREADSTORAGE(control2str_threadbuf, control2str_threadbuf_init);
00142 #define CONTROL2STR_BUFSIZE 100
00143
00144
00145
00146
00147
00148 #define KEEP_ALIVE_MESSAGE 0x0000
00149
00150
00151 #define REGISTER_MESSAGE 0x0001
00152 struct register_message {
00153 char name[16];
00154 uint32_t userId;
00155 uint32_t instance;
00156 uint32_t ip;
00157 uint32_t type;
00158 uint32_t maxStreams;
00159 };
00160
00161 #define IP_PORT_MESSAGE 0x0002
00162
00163 #define KEYPAD_BUTTON_MESSAGE 0x0003
00164 struct keypad_button_message {
00165 uint32_t button;
00166 uint32_t lineInstance;
00167 uint32_t callReference;
00168 };
00169
00170 #define STIMULUS_MESSAGE 0x0005
00171 struct stimulus_message {
00172 uint32_t stimulus;
00173 uint32_t stimulusInstance;
00174 uint32_t unknown1;
00175 };
00176
00177 #define OFFHOOK_MESSAGE 0x0006
00178 struct offhook_message {
00179 uint32_t unknown1;
00180 uint32_t unknown2;
00181 };
00182
00183 #define ONHOOK_MESSAGE 0x0007
00184 struct onhook_message {
00185 uint32_t unknown1;
00186 uint32_t unknown2;
00187 };
00188
00189 #define CAPABILITIES_RES_MESSAGE 0x0010
00190 struct station_capabilities {
00191 uint32_t codec;
00192 uint32_t frames;
00193 union {
00194 char res[8];
00195 uint32_t rate;
00196 } payloads;
00197 };
00198
00199 struct capabilities_res_message {
00200 uint32_t count;
00201 struct station_capabilities caps[18];
00202 };
00203
00204 #define SPEED_DIAL_STAT_REQ_MESSAGE 0x000A
00205 struct speed_dial_stat_req_message {
00206 uint32_t speedDialNumber;
00207 };
00208
00209 #define LINE_STATE_REQ_MESSAGE 0x000B
00210 struct line_state_req_message {
00211 uint32_t lineNumber;
00212 };
00213
00214 #define TIME_DATE_REQ_MESSAGE 0x000D
00215 #define BUTTON_TEMPLATE_REQ_MESSAGE 0x000E
00216 #define VERSION_REQ_MESSAGE 0x000F
00217 #define SERVER_REQUEST_MESSAGE 0x0012
00218
00219 #define ALARM_MESSAGE 0x0020
00220 struct alarm_message {
00221 uint32_t alarmSeverity;
00222 char displayMessage[80];
00223 uint32_t alarmParam1;
00224 uint32_t alarmParam2;
00225 };
00226
00227 #define OPEN_RECEIVE_CHANNEL_ACK_MESSAGE 0x0022
00228 struct open_receive_channel_ack_message {
00229 uint32_t status;
00230 uint32_t ipAddr;
00231 uint32_t port;
00232 uint32_t passThruId;
00233 };
00234
00235 #define SOFT_KEY_SET_REQ_MESSAGE 0x0025
00236
00237 #define SOFT_KEY_EVENT_MESSAGE 0x0026
00238 struct soft_key_event_message {
00239 uint32_t softKeyEvent;
00240 uint32_t instance;
00241 uint32_t reference;
00242 };
00243
00244 #define UNREGISTER_MESSAGE 0x0027
00245 #define SOFT_KEY_TEMPLATE_REQ_MESSAGE 0x0028
00246 #define HEADSET_STATUS_MESSAGE 0x002B
00247 #define REGISTER_AVAILABLE_LINES_MESSAGE 0x002D
00248
00249 #define REGISTER_ACK_MESSAGE 0x0081
00250 struct register_ack_message {
00251 uint32_t keepAlive;
00252 char dateTemplate[6];
00253 char res[2];
00254 uint32_t secondaryKeepAlive;
00255 char res2[4];
00256 };
00257
00258 #define START_TONE_MESSAGE 0x0082
00259 struct start_tone_message {
00260 uint32_t tone;
00261 };
00262
00263 #define STOP_TONE_MESSAGE 0x0083
00264
00265 #define SET_RINGER_MESSAGE 0x0085
00266 struct set_ringer_message {
00267 uint32_t ringerMode;
00268 uint32_t unknown1;
00269 uint32_t unknown2;
00270 };
00271
00272 #define SET_LAMP_MESSAGE 0x0086
00273 struct set_lamp_message {
00274 uint32_t stimulus;
00275 uint32_t stimulusInstance;
00276 uint32_t deviceStimulus;
00277 };
00278
00279 #define SET_SPEAKER_MESSAGE 0x0088
00280 struct set_speaker_message {
00281 uint32_t mode;
00282 };
00283
00284
00285 #define SET_MICROPHONE_MESSAGE 0x0089
00286 struct set_microphone_message {
00287 uint32_t mode;
00288 };
00289
00290 #define START_MEDIA_TRANSMISSION_MESSAGE 0x008A
00291 struct media_qualifier {
00292 uint32_t precedence;
00293 uint32_t vad;
00294 uint32_t packets;
00295 uint32_t bitRate;
00296 };
00297
00298 struct start_media_transmission_message {
00299 uint32_t conferenceId;
00300 uint32_t passThruPartyId;
00301 uint32_t remoteIp;
00302 uint32_t remotePort;
00303 uint32_t packetSize;
00304 uint32_t payloadType;
00305 struct media_qualifier qualifier;
00306 };
00307
00308 #define STOP_MEDIA_TRANSMISSION_MESSAGE 0x008B
00309 struct stop_media_transmission_message {
00310 uint32_t conferenceId;
00311 uint32_t passThruPartyId;
00312 };
00313
00314 #define CALL_INFO_MESSAGE 0x008F
00315 struct call_info_message {
00316 char callingPartyName[40];
00317 char callingParty[24];
00318 char calledPartyName[40];
00319 char calledParty[24];
00320 uint32_t instance;
00321 uint32_t reference;
00322 uint32_t type;
00323 char originalCalledPartyName[40];
00324 char originalCalledParty[24];
00325 char lastRedirectingPartyName[40];
00326 char lastRedirectingParty[24];
00327 uint32_t originalCalledPartyRedirectReason;
00328 uint32_t lastRedirectingReason;
00329 char callingPartyVoiceMailbox[24];
00330 char calledPartyVoiceMailbox[24];
00331 char originalCalledPartyVoiceMailbox[24];
00332 char lastRedirectingVoiceMailbox[24];
00333 };
00334
00335 #define SPEED_DIAL_STAT_RES_MESSAGE 0x0091
00336 struct speed_dial_stat_res_message {
00337 uint32_t speedDialNumber;
00338 char speedDialDirNumber[24];
00339 char speedDialDisplayName[40];
00340 };
00341
00342 #define LINE_STAT_RES_MESSAGE 0x0092
00343 struct line_stat_res_message {
00344 uint32_t lineNumber;
00345 char lineDirNumber[24];
00346 char lineDisplayName[42];
00347 uint32_t space;
00348 };
00349
00350 #define DEFINETIMEDATE_MESSAGE 0x0094
00351 struct definetimedate_message {
00352 uint32_t year;
00353 uint32_t month;
00354 uint32_t dayofweek;
00355 uint32_t day;
00356 uint32_t hour;
00357 uint32_t minute;
00358 uint32_t seconds;
00359 uint32_t milliseconds;
00360 uint32_t timestamp;
00361 };
00362
00363 #define BUTTON_TEMPLATE_RES_MESSAGE 0x0097
00364 struct button_definition {
00365 uint8_t instanceNumber;
00366 uint8_t buttonDefinition;
00367 };
00368
00369 struct button_definition_template {
00370 uint8_t buttonDefinition;
00371
00372
00373 };
00374
00375 #define STIMULUS_REDIAL 0x01
00376 #define STIMULUS_SPEEDDIAL 0x02
00377 #define STIMULUS_HOLD 0x03
00378 #define STIMULUS_TRANSFER 0x04
00379 #define STIMULUS_FORWARDALL 0x05
00380 #define STIMULUS_FORWARDBUSY 0x06
00381 #define STIMULUS_FORWARDNOANSWER 0x07
00382 #define STIMULUS_DISPLAY 0x08
00383 #define STIMULUS_LINE 0x09
00384 #define STIMULUS_VOICEMAIL 0x0F
00385 #define STIMULUS_AUTOANSWER 0x11
00386 #define STIMULUS_CONFERENCE 0x7D
00387 #define STIMULUS_CALLPARK 0x7E
00388 #define STIMULUS_CALLPICKUP 0x7F
00389 #define STIMULUS_NONE 0xFF
00390
00391
00392 #define BT_REDIAL STIMULUS_REDIAL
00393 #define BT_SPEEDDIAL STIMULUS_SPEEDDIAL
00394 #define BT_HOLD STIMULUS_HOLD
00395 #define BT_TRANSFER STIMULUS_TRANSFER
00396 #define BT_FORWARDALL STIMULUS_FORWARDALL
00397 #define BT_FORWARDBUSY STIMULUS_FORWARDBUSY
00398 #define BT_FORWARDNOANSWER STIMULUS_FORWARDNOANSWER
00399 #define BT_DISPLAY STIMULUS_DISPLAY
00400 #define BT_LINE STIMULUS_LINE
00401 #define BT_VOICEMAIL STIMULUS_VOICEMAIL
00402 #define BT_AUTOANSWER STIMULUS_AUTOANSWER
00403 #define BT_CONFERENCE STIMULUS_CONFERENCE
00404 #define BT_CALLPARK STIMULUS_CALLPARK
00405 #define BT_CALLPICKUP STIMULUS_CALLPICKUP
00406 #define BT_NONE 0x00
00407
00408
00409
00410
00411 #define BT_CUST_LINESPEEDDIAL 0xB0
00412 #define BT_CUST_HINT 0xB1
00413
00414 struct button_template_res_message {
00415 uint32_t buttonOffset;
00416 uint32_t buttonCount;
00417 uint32_t totalButtonCount;
00418 struct button_definition definition[42];
00419 };
00420
00421 #define VERSION_RES_MESSAGE 0x0098
00422 struct version_res_message {
00423 char version[16];
00424 };
00425
00426 #define DISPLAYTEXT_MESSAGE 0x0099
00427 struct displaytext_message {
00428 char text[40];
00429 };
00430
00431 #define CLEAR_NOTIFY_MESSAGE 0x0115
00432 #define CLEAR_PROMPT_MESSAGE 0x0113
00433 #define CLEAR_DISPLAY_MESSAGE 0x009A
00434
00435 #define CAPABILITIES_REQ_MESSAGE 0x009B
00436
00437 #define REGISTER_REJ_MESSAGE 0x009D
00438 struct register_rej_message {
00439 char errMsg[33];
00440 };
00441
00442 #define SERVER_RES_MESSAGE 0x009E
00443 struct server_identifier {
00444 char serverName[48];
00445 };
00446
00447 struct server_res_message {
00448 struct server_identifier server[5];
00449 uint32_t serverListenPort[5];
00450 uint32_t serverIpAddr[5];
00451 };
00452
00453 #define RESET_MESSAGE 0x009F
00454 struct reset_message {
00455 uint32_t resetType;
00456 };
00457
00458 #define KEEP_ALIVE_ACK_MESSAGE 0x0100
00459
00460 #define OPEN_RECEIVE_CHANNEL_MESSAGE 0x0105
00461 struct open_receive_channel_message {
00462 uint32_t conferenceId;
00463 uint32_t partyId;
00464 uint32_t packets;
00465 uint32_t capability;
00466 uint32_t echo;
00467 uint32_t bitrate;
00468 };
00469
00470 #define CLOSE_RECEIVE_CHANNEL_MESSAGE 0x0106
00471 struct close_receive_channel_message {
00472 uint32_t conferenceId;
00473 uint32_t partyId;
00474 };
00475
00476 #define SOFT_KEY_TEMPLATE_RES_MESSAGE 0x0108
00477
00478 struct soft_key_template_definition {
00479 char softKeyLabel[16];
00480 uint32_t softKeyEvent;
00481 };
00482
00483 #define KEYDEF_ONHOOK 0
00484 #define KEYDEF_CONNECTED 1
00485 #define KEYDEF_ONHOLD 2
00486 #define KEYDEF_RINGIN 3
00487 #define KEYDEF_OFFHOOK 4
00488 #define KEYDEF_CONNWITHTRANS 5
00489 #define KEYDEF_DADFD 6
00490 #define KEYDEF_CONNWITHCONF 7
00491 #define KEYDEF_RINGOUT 8
00492 #define KEYDEF_OFFHOOKWITHFEAT 9
00493 #define KEYDEF_UNKNOWN 10
00494
00495 #define SOFTKEY_NONE 0x00
00496 #define SOFTKEY_REDIAL 0x01
00497 #define SOFTKEY_NEWCALL 0x02
00498 #define SOFTKEY_HOLD 0x03
00499 #define SOFTKEY_TRNSFER 0x04
00500 #define SOFTKEY_CFWDALL 0x05
00501 #define SOFTKEY_CFWDBUSY 0x06
00502 #define SOFTKEY_CFWDNOANSWER 0x07
00503 #define SOFTKEY_BKSPC 0x08
00504 #define SOFTKEY_ENDCALL 0x09
00505 #define SOFTKEY_RESUME 0x0A
00506 #define SOFTKEY_ANSWER 0x0B
00507 #define SOFTKEY_INFO 0x0C
00508 #define SOFTKEY_CONFRN 0x0D
00509 #define SOFTKEY_PARK 0x0E
00510 #define SOFTKEY_JOIN 0x0F
00511 #define SOFTKEY_MEETME 0x10
00512 #define SOFTKEY_PICKUP 0x11
00513 #define SOFTKEY_GPICKUP 0x12
00514
00515 struct soft_key_template_definition soft_key_template_default[] = {
00516 { "Redial", 0x01 },
00517 { "NewCall", 0x02 },
00518 { "Hold", 0x03 },
00519 { "Trnsfer", 0x04 },
00520 { "CFwdAll", 0x05 },
00521 { "CFwdBusy", 0x06 },
00522 { "CFwdNoAnswer", 0x07 },
00523 { "<<", 0x08 },
00524 { "EndCall", 0x09 },
00525 { "Resume", 0x0A },
00526 { "Answer", 0x0B },
00527 { "Info", 0x0C },
00528 { "Confrn", 0x0D },
00529 { "Park", 0x0E },
00530 { "Join", 0x0F },
00531 { "MeetMe", 0x10 },
00532 { "PickUp", 0x11 },
00533 { "GPickUp", 0x12 },
00534 };
00535
00536 struct soft_key_definitions {
00537 const uint8_t mode;
00538 const uint8_t *defaults;
00539 const int count;
00540 };
00541
00542 static const uint8_t soft_key_default_onhook[] = {
00543 SOFTKEY_REDIAL,
00544 SOFTKEY_CFWDALL,
00545 SOFTKEY_CFWDBUSY,
00546 SOFTKEY_GPICKUP,
00547 SOFTKEY_CONFRN,
00548 };
00549
00550 static const uint8_t soft_key_default_connected[] = {
00551 SOFTKEY_HOLD,
00552 SOFTKEY_ENDCALL,
00553 SOFTKEY_TRNSFER,
00554 SOFTKEY_PARK,
00555 SOFTKEY_CFWDALL,
00556 SOFTKEY_CFWDBUSY,
00557 };
00558
00559 static const uint8_t soft_key_default_onhold[] = {
00560 SOFTKEY_RESUME,
00561 SOFTKEY_NEWCALL,
00562 SOFTKEY_ENDCALL,
00563 SOFTKEY_TRNSFER,
00564 };
00565
00566 static const uint8_t soft_key_default_ringin[] = {
00567 SOFTKEY_ANSWER,
00568 SOFTKEY_ENDCALL,
00569 SOFTKEY_TRNSFER,
00570 };
00571
00572 static const uint8_t soft_key_default_offhook[] = {
00573 SOFTKEY_REDIAL,
00574 SOFTKEY_ENDCALL,
00575 SOFTKEY_CFWDALL,
00576 SOFTKEY_CFWDBUSY,
00577 SOFTKEY_GPICKUP,
00578 };
00579
00580 static const uint8_t soft_key_default_connwithtrans[] = {
00581 SOFTKEY_HOLD,
00582 SOFTKEY_ENDCALL,
00583 SOFTKEY_TRNSFER,
00584 SOFTKEY_PARK,
00585 SOFTKEY_CFWDALL,
00586 SOFTKEY_CFWDBUSY,
00587 };
00588
00589 static const uint8_t soft_key_default_dadfd[] = {
00590 SOFTKEY_BKSPC,
00591 SOFTKEY_ENDCALL,
00592 };
00593
00594 static const uint8_t soft_key_default_connwithconf[] = {
00595 SOFTKEY_NONE,
00596 };
00597
00598 static const uint8_t soft_key_default_ringout[] = {
00599 SOFTKEY_ENDCALL,
00600 SOFTKEY_TRNSFER,
00601 SOFTKEY_CFWDALL,
00602 SOFTKEY_CFWDBUSY,
00603 };
00604
00605 static const uint8_t soft_key_default_offhookwithfeat[] = {
00606 SOFTKEY_REDIAL,
00607 SOFTKEY_ENDCALL,
00608 };
00609
00610 static const uint8_t soft_key_default_unknown[] = {
00611 SOFTKEY_NONE,
00612 };
00613
00614 static const struct soft_key_definitions soft_key_default_definitions[] = {
00615 {KEYDEF_ONHOOK, soft_key_default_onhook, sizeof(soft_key_default_onhook) / sizeof(uint8_t)},
00616 {KEYDEF_CONNECTED, soft_key_default_connected, sizeof(soft_key_default_connected) / sizeof(uint8_t)},
00617 {KEYDEF_ONHOLD, soft_key_default_onhold, sizeof(soft_key_default_onhold) / sizeof(uint8_t)},
00618 {KEYDEF_RINGIN, soft_key_default_ringin, sizeof(soft_key_default_ringin) / sizeof(uint8_t)},
00619 {KEYDEF_OFFHOOK, soft_key_default_offhook, sizeof(soft_key_default_offhook) / sizeof(uint8_t)},
00620 {KEYDEF_CONNWITHTRANS, soft_key_default_connwithtrans, sizeof(soft_key_default_connwithtrans) / sizeof(uint8_t)},
00621 {KEYDEF_DADFD, soft_key_default_dadfd, sizeof(soft_key_default_dadfd) / sizeof(uint8_t)},
00622 {KEYDEF_CONNWITHCONF, soft_key_default_connwithconf, sizeof(soft_key_default_connwithconf) / sizeof(uint8_t)},
00623 {KEYDEF_RINGOUT, soft_key_default_ringout, sizeof(soft_key_default_ringout) / sizeof(uint8_t)},
00624 {KEYDEF_OFFHOOKWITHFEAT, soft_key_default_offhookwithfeat, sizeof(soft_key_default_offhookwithfeat) / sizeof(uint8_t)},
00625 {KEYDEF_UNKNOWN, soft_key_default_unknown, sizeof(soft_key_default_unknown) / sizeof(uint8_t)}
00626 };
00627
00628 struct soft_key_template_res_message {
00629 uint32_t softKeyOffset;
00630 uint32_t softKeyCount;
00631 uint32_t totalSoftKeyCount;
00632 struct soft_key_template_definition softKeyTemplateDefinition[32];
00633 };
00634
00635 #define SOFT_KEY_SET_RES_MESSAGE 0x0109
00636
00637 struct soft_key_set_definition {
00638 uint8_t softKeyTemplateIndex[16];
00639 uint16_t softKeyInfoIndex[16];
00640 };
00641
00642 struct soft_key_set_res_message {
00643 uint32_t softKeySetOffset;
00644 uint32_t softKeySetCount;
00645 uint32_t totalSoftKeySetCount;
00646 struct soft_key_set_definition softKeySetDefinition[16];
00647 uint32_t res;
00648 };
00649
00650 #define SELECT_SOFT_KEYS_MESSAGE 0x0110
00651 struct select_soft_keys_message {
00652 uint32_t instance;
00653 uint32_t reference;
00654 uint32_t softKeySetIndex;
00655 uint32_t validKeyMask;
00656 };
00657
00658 #define CALL_STATE_MESSAGE 0x0111
00659 struct call_state_message {
00660 uint32_t callState;
00661 uint32_t lineInstance;
00662 uint32_t callReference;
00663 };
00664
00665 #define DISPLAY_PROMPT_STATUS_MESSAGE 0x0112
00666 struct display_prompt_status_message {
00667 uint32_t messageTimeout;
00668 char promptMessage[32];
00669 uint32_t lineInstance;
00670 uint32_t callReference;
00671 };
00672
00673 #define DISPLAY_NOTIFY_MESSAGE 0x0114
00674 struct display_notify_message {
00675 uint32_t displayTimeout;
00676 char displayMessage[100];
00677 };
00678
00679 #define ACTIVATE_CALL_PLANE_MESSAGE 0x0116
00680 struct activate_call_plane_message {
00681 uint32_t lineInstance;
00682 };
00683
00684 #define DIALED_NUMBER_MESSAGE 0x011D
00685 struct dialed_number_message {
00686 char dialedNumber[24];
00687 uint32_t lineInstance;
00688 uint32_t callReference;
00689 };
00690
00691 union skinny_data {
00692 struct alarm_message alarm;
00693 struct speed_dial_stat_req_message speeddialreq;
00694 struct register_message reg;
00695 struct register_ack_message regack;
00696 struct register_rej_message regrej;
00697 struct capabilities_res_message caps;
00698 struct version_res_message version;
00699 struct button_template_res_message buttontemplate;
00700 struct displaytext_message displaytext;
00701 struct display_prompt_status_message displaypromptstatus;
00702 struct definetimedate_message definetimedate;
00703 struct start_tone_message starttone;
00704 struct speed_dial_stat_res_message speeddial;
00705 struct line_state_req_message line;
00706 struct line_stat_res_message linestat;
00707 struct soft_key_set_res_message softkeysets;
00708 struct soft_key_template_res_message softkeytemplate;
00709 struct server_res_message serverres;
00710 struct reset_message reset;
00711 struct set_lamp_message setlamp;
00712 struct set_ringer_message setringer;
00713 struct call_state_message callstate;
00714 struct keypad_button_message keypad;
00715 struct select_soft_keys_message selectsoftkey;
00716 struct activate_call_plane_message activatecallplane;
00717 struct stimulus_message stimulus;
00718 struct offhook_message offhook;
00719 struct onhook_message onhook;
00720 struct set_speaker_message setspeaker;
00721 struct set_microphone_message setmicrophone;
00722 struct call_info_message callinfo;
00723 struct start_media_transmission_message startmedia;
00724 struct stop_media_transmission_message stopmedia;
00725 struct open_receive_channel_message openreceivechannel;
00726 struct open_receive_channel_ack_message openreceivechannelack;
00727 struct close_receive_channel_message closereceivechannel;
00728 struct display_notify_message displaynotify;
00729 struct dialed_number_message dialednumber;
00730 struct soft_key_event_message softkeyeventmessage;
00731 };
00732
00733
00734 struct skinny_req {
00735 int len;
00736 int res;
00737 int e;
00738 union skinny_data data;
00739 };
00740
00741
00742
00743
00744 int skinny_header_size = 12;
00745
00746
00747
00748
00749
00750 static int skinnydebug = 0;
00751
00752
00753 static struct sockaddr_in bindaddr;
00754 static char ourhost[256];
00755 static int ourport;
00756 static struct in_addr __ourip;
00757 struct ast_hostent ahp;
00758 struct hostent *hp;
00759 static int skinnysock = -1;
00760 static pthread_t accept_t;
00761 static char context[AST_MAX_CONTEXT] = "default";
00762 static char language[MAX_LANGUAGE] = "";
00763 static char mohinterpret[MAX_MUSICCLASS] = "default";
00764 static char mohsuggest[MAX_MUSICCLASS] = "";
00765 static char cid_num[AST_MAX_EXTENSION] = "";
00766 static char cid_name[AST_MAX_EXTENSION] = "";
00767 static char linelabel[AST_MAX_EXTENSION] ="";
00768 static int nat = 0;
00769 static ast_group_t cur_callergroup = 0;
00770 static ast_group_t cur_pickupgroup = 0;
00771 static int immediate = 0;
00772 static int callwaiting = 0;
00773 static int callreturn = 0;
00774 static int threewaycalling = 0;
00775 static int mwiblink = 0;
00776
00777 static int transfer = 0;
00778 static int cancallforward = 0;
00779
00780 static char accountcode[AST_MAX_ACCOUNT_CODE] = "";
00781 static char mailbox[AST_MAX_EXTENSION];
00782 static int amaflags = 0;
00783 static int callnums = 1;
00784
00785 #define SKINNY_DEVICE_UNKNOWN -1
00786 #define SKINNY_DEVICE_NONE 0
00787 #define SKINNY_DEVICE_30SPPLUS 1
00788 #define SKINNY_DEVICE_12SPPLUS 2
00789 #define SKINNY_DEVICE_12SP 3
00790 #define SKINNY_DEVICE_12 4
00791 #define SKINNY_DEVICE_30VIP 5
00792 #define SKINNY_DEVICE_7910 6
00793 #define SKINNY_DEVICE_7960 7
00794 #define SKINNY_DEVICE_7940 8
00795 #define SKINNY_DEVICE_7935 9
00796 #define SKINNY_DEVICE_ATA186 12
00797 #define SKINNY_DEVICE_7941 115
00798 #define SKINNY_DEVICE_7971 119
00799 #define SKINNY_DEVICE_7985 302
00800 #define SKINNY_DEVICE_7911 307
00801 #define SKINNY_DEVICE_7961GE 308
00802 #define SKINNY_DEVICE_7941GE 309
00803 #define SKINNY_DEVICE_7905 20000
00804 #define SKINNY_DEVICE_7920 30002
00805 #define SKINNY_DEVICE_7970 30006
00806 #define SKINNY_DEVICE_7912 30007
00807 #define SKINNY_DEVICE_7902 30008
00808 #define SKINNY_DEVICE_CIPC 30016
00809 #define SKINNY_DEVICE_7961 30018
00810 #define SKINNY_DEVICE_7936 30019
00811 #define SKINNY_DEVICE_SCCPGATEWAY_AN 30027
00812 #define SKINNY_DEVICE_SCCPGATEWAY_BRI 30028
00813
00814 #define SKINNY_SPEAKERON 1
00815 #define SKINNY_SPEAKEROFF 2
00816
00817 #define SKINNY_MICON 1
00818 #define SKINNY_MICOFF 2
00819
00820 #define SKINNY_OFFHOOK 1
00821 #define SKINNY_ONHOOK 2
00822 #define SKINNY_RINGOUT 3
00823 #define SKINNY_RINGIN 4
00824 #define SKINNY_CONNECTED 5
00825 #define SKINNY_BUSY 6
00826 #define SKINNY_CONGESTION 7
00827 #define SKINNY_HOLD 8
00828 #define SKINNY_CALLWAIT 9
00829 #define SKINNY_TRANSFER 10
00830 #define SKINNY_PARK 11
00831 #define SKINNY_PROGRESS 12
00832 #define SKINNY_INVALID 14
00833
00834 #define SKINNY_SILENCE 0x00
00835 #define SKINNY_DIALTONE 0x21
00836 #define SKINNY_BUSYTONE 0x23
00837 #define SKINNY_ALERT 0x24
00838 #define SKINNY_REORDER 0x25
00839 #define SKINNY_CALLWAITTONE 0x2D
00840 #define SKINNY_NOTONE 0x7F
00841
00842 #define SKINNY_LAMP_OFF 1
00843 #define SKINNY_LAMP_ON 2
00844 #define SKINNY_LAMP_WINK 3
00845 #define SKINNY_LAMP_FLASH 4
00846 #define SKINNY_LAMP_BLINK 5
00847
00848 #define SKINNY_RING_OFF 1
00849 #define SKINNY_RING_INSIDE 2
00850 #define SKINNY_RING_OUTSIDE 3
00851 #define SKINNY_RING_FEATURE 4
00852
00853 #define TYPE_TRUNK 1
00854 #define TYPE_LINE 2
00855
00856
00857 #define SKINNY_CX_SENDONLY 0
00858 #define SKINNY_CX_RECVONLY 1
00859 #define SKINNY_CX_SENDRECV 2
00860 #define SKINNY_CX_CONF 3
00861 #define SKINNY_CX_CONFERENCE 3
00862 #define SKINNY_CX_MUTE 4
00863 #define SKINNY_CX_INACTIVE 4
00864
00865 #if 0
00866 static char *skinny_cxmodes[] = {
00867 "sendonly",
00868 "recvonly",
00869 "sendrecv",
00870 "confrnce",
00871 "inactive"
00872 };
00873 #endif
00874
00875
00876 static struct sched_context *sched;
00877 static struct io_context *io;
00878
00879
00880
00881 AST_MUTEX_DEFINE_STATIC(monlock);
00882
00883 AST_MUTEX_DEFINE_STATIC(netlock);
00884
00885 AST_MUTEX_DEFINE_STATIC(sessionlock);
00886
00887 AST_MUTEX_DEFINE_STATIC(devicelock);
00888 #if 0
00889
00890 AST_MUTEX_DEFINE_STATIC(pagingdevicelock);
00891 #endif
00892
00893
00894
00895 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00896
00897
00898 static int firstdigittimeout = 16000;
00899
00900
00901 static int gendigittimeout = 8000;
00902
00903
00904 static int matchdigittimeout = 3000;
00905
00906 struct skinny_subchannel {
00907 ast_mutex_t lock;
00908 struct ast_channel *owner;
00909 struct ast_rtp *rtp;
00910 struct ast_rtp *vrtp;
00911 unsigned int callid;
00912
00913 int progress;
00914 int ringing;
00915 int onhold;
00916
00917 int cxmode;
00918 int nat;
00919 int outgoing;
00920 int alreadygone;
00921
00922 struct skinny_subchannel *next;
00923 struct skinny_line *parent;
00924 };
00925
00926 struct skinny_line {
00927 ast_mutex_t lock;
00928 char name[80];
00929 char label[42];
00930 char accountcode[AST_MAX_ACCOUNT_CODE];
00931 char exten[AST_MAX_EXTENSION];
00932 char context[AST_MAX_CONTEXT];
00933 char language[MAX_LANGUAGE];
00934 char cid_num[AST_MAX_EXTENSION];
00935 char cid_name[AST_MAX_EXTENSION];
00936 char lastcallerid[AST_MAX_EXTENSION];
00937 char call_forward[AST_MAX_EXTENSION];
00938 char mailbox[AST_MAX_EXTENSION];
00939 char mohinterpret[MAX_MUSICCLASS];
00940 char mohsuggest[MAX_MUSICCLASS];
00941 char lastnumberdialed[AST_MAX_EXTENSION];
00942 int curtone;
00943 ast_group_t callgroup;
00944 ast_group_t pickupgroup;
00945 int callwaiting;
00946 int transfer;
00947 int threewaycalling;
00948 int mwiblink;
00949 int cancallforward;
00950 int callreturn;
00951 int dnd;
00952 int hascallerid;
00953 int hidecallerid;
00954 int amaflags;
00955 int type;
00956 int instance;
00957 int group;
00958 int needdestroy;
00959 int capability;
00960 int nonCodecCapability;
00961 int onhooktime;
00962 int msgstate;
00963 int immediate;
00964 int hookstate;
00965 int nat;
00966
00967 struct ast_codec_pref prefs;
00968 struct skinny_subchannel *sub;
00969 struct skinny_line *next;
00970 struct skinny_device *parent;
00971 };
00972
00973 struct skinny_speeddial {
00974 ast_mutex_t lock;
00975 char label[42];
00976 char exten[AST_MAX_EXTENSION];
00977 int instance;
00978
00979 struct skinny_speeddial *next;
00980 struct skinny_device *parent;
00981 };
00982
00983 struct skinny_addon {
00984 ast_mutex_t lock;
00985 char type[10];
00986
00987 struct skinny_addon *next;
00988 struct skinny_device *parent;
00989 };
00990
00991 static struct skinny_device {
00992
00993 char name[80];
00994 char id[16];
00995 char version_id[16];
00996 int type;
00997 int registered;
00998 int lastlineinstance;
00999 int lastcallreference;
01000 int capability;
01001 struct sockaddr_in addr;
01002 struct in_addr ourip;
01003 struct skinny_line *lines;
01004 struct skinny_speeddial *speeddials;
01005 struct skinny_addon *addons;
01006 struct ast_codec_pref prefs;
01007 struct ast_ha *ha;
01008 struct skinnysession *session;
01009 struct skinny_device *next;
01010 } *devices = NULL;
01011
01012 struct skinny_paging_device {
01013 char name[80];
01014 char id[16];
01015 struct skinny_device ** devices;
01016 struct skinny_paging_device *next;
01017 };
01018
01019 static struct skinnysession {
01020 pthread_t t;
01021 ast_mutex_t lock;
01022 struct sockaddr_in sin;
01023 int fd;
01024 char inbuf[SKINNY_MAX_PACKET];
01025 char outbuf[SKINNY_MAX_PACKET];
01026 struct skinny_device *device;
01027 struct skinnysession *next;
01028 } *sessions = NULL;
01029
01030 static struct ast_channel *skinny_request(const char *type, int format, void *data, int *cause);
01031 static int skinny_call(struct ast_channel *ast, char *dest, int timeout);
01032 static int skinny_hangup(struct ast_channel *ast);
01033 static int skinny_answer(struct ast_channel *ast);
01034 static struct ast_frame *skinny_read(struct ast_channel *ast);
01035 static int skinny_write(struct ast_channel *ast, struct ast_frame *frame);
01036 static int skinny_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen);
01037 static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
01038 static int skinny_senddigit_begin(struct ast_channel *ast, char digit);
01039 static int skinny_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
01040
01041 static const struct ast_channel_tech skinny_tech = {
01042 .type = "Skinny",
01043 .description = tdesc,
01044 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
01045 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
01046 .requester = skinny_request,
01047 .call = skinny_call,
01048 .hangup = skinny_hangup,
01049 .answer = skinny_answer,
01050 .read = skinny_read,
01051 .write = skinny_write,
01052 .indicate = skinny_indicate,
01053 .fixup = skinny_fixup,
01054 .send_digit_begin = skinny_senddigit_begin,
01055 .send_digit_end = skinny_senddigit_end,
01056
01057 };
01058
01059 static void *get_button_template(struct skinnysession *s, struct button_definition_template *btn)
01060 {
01061 struct skinny_device *d = s->device;
01062 struct skinny_addon *a = d->addons;
01063 int i;
01064
01065 switch (d->type) {
01066 case SKINNY_DEVICE_30SPPLUS:
01067 case SKINNY_DEVICE_30VIP:
01068
01069 for (i = 0; i < 4; i++)
01070 (btn++)->buttonDefinition = BT_LINE;
01071 (btn++)->buttonDefinition = BT_REDIAL;
01072 (btn++)->buttonDefinition = BT_VOICEMAIL;
01073 (btn++)->buttonDefinition = BT_CALLPARK;
01074 (btn++)->buttonDefinition = BT_FORWARDALL;
01075 (btn++)->buttonDefinition = BT_CONFERENCE;
01076 for (i = 0; i < 4; i++)
01077 (btn++)->buttonDefinition = BT_NONE;
01078 for (i = 0; i < 13; i++)
01079 (btn++)->buttonDefinition = BT_SPEEDDIAL;
01080
01081 break;
01082 case SKINNY_DEVICE_12SPPLUS:
01083 case SKINNY_DEVICE_12SP:
01084 case SKINNY_DEVICE_12:
01085
01086 for (i = 0; i < 2; i++)
01087 (btn++)->buttonDefinition = BT_LINE;
01088 (btn++)->buttonDefinition = BT_REDIAL;
01089 for (i = 0; i < 3; i++)
01090 (btn++)->buttonDefinition = BT_SPEEDDIAL;
01091 (btn++)->buttonDefinition = BT_HOLD;
01092 (btn++)->buttonDefinition = BT_TRANSFER;
01093 (btn++)->buttonDefinition = BT_FORWARDALL;
01094 (btn++)->buttonDefinition = BT_CALLPARK;
01095 (btn++)->buttonDefinition = BT_VOICEMAIL;
01096 (btn++)->buttonDefinition = BT_CONFERENCE;
01097 break;
01098 case SKINNY_DEVICE_7910:
01099 (btn++)->buttonDefinition = BT_LINE;
01100 (btn++)->buttonDefinition = BT_HOLD;
01101 (btn++)->buttonDefinition = BT_TRANSFER;
01102 (btn++)->buttonDefinition = BT_DISPLAY;
01103 (btn++)->buttonDefinition = BT_VOICEMAIL;
01104 (btn++)->buttonDefinition = BT_CONFERENCE;
01105 (btn++)->buttonDefinition = BT_FORWARDALL;
01106 for (i = 0; i < 2; i++)
01107 (btn++)->buttonDefinition = BT_SPEEDDIAL;
01108 (btn++)->buttonDefinition = BT_REDIAL;
01109 break;
01110 case SKINNY_DEVICE_7960:
01111 case SKINNY_DEVICE_7961:
01112 case SKINNY_DEVICE_7961GE:
01113 for (i = 0; i < 6; i++)
01114 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
01115 break;
01116 case SKINNY_DEVICE_7940:
01117 case SKINNY_DEVICE_7941:
01118 case SKINNY_DEVICE_7941GE:
01119 for (i = 0; i < 2; i++)
01120 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
01121 break;
01122 case SKINNY_DEVICE_7935:
01123 case SKINNY_DEVICE_7936:
01124 for (i = 0; i < 2; i++)
01125 (btn++)->buttonDefinition = BT_LINE;
01126 break;
01127 case SKINNY_DEVICE_ATA186:
01128 (btn++)->buttonDefinition = BT_LINE;
01129 break;
01130 case SKINNY_DEVICE_7970:
01131 case SKINNY_DEVICE_7971:
01132 case SKINNY_DEVICE_CIPC:
01133 for (i = 0; i < 8; i++)
01134 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
01135 break;
01136 case SKINNY_DEVICE_7985:
01137
01138 ast_log(LOG_WARNING, "Unsupported device type '%d (7985)' found.\n", d->type);
01139 break;
01140 case SKINNY_DEVICE_7912:
01141 case SKINNY_DEVICE_7911:
01142 case SKINNY_DEVICE_7905:
01143 (btn++)->buttonDefinition = BT_LINE;
01144 (btn++)->buttonDefinition = BT_HOLD;
01145 break;
01146 case SKINNY_DEVICE_7920:
01147
01148 for (i = 0; i < 4; i++)
01149 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
01150 break;
01151 case SKINNY_DEVICE_7902:
01152 ast_log(LOG_WARNING, "Unsupported device type '%d (7902)' found.\n", d->type);
01153 break;
01154 case SKINNY_DEVICE_SCCPGATEWAY_AN:
01155 case SKINNY_DEVICE_SCCPGATEWAY_BRI:
01156 ast_log(LOG_WARNING, "Unsupported device type '%d (SCCP gateway)' found.\n", d->type);
01157 break;
01158 default:
01159 ast_log(LOG_WARNING, "Unknown device type '%d' found.\n", d->type);
01160 break;
01161 }
01162
01163 for (a = d->addons; a; a = a->next) {
01164 if (!strcasecmp(a->type, "7914")) {
01165 for (i = 0; i < 14; i++)
01166 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
01167 } else {
01168 ast_log(LOG_WARNING, "Unknown addon type '%s' found. Skipping.\n", a->type);
01169 }
01170 }
01171
01172 return btn;
01173 }
01174
01175 static struct skinny_req *req_alloc(size_t size, int response_message)
01176 {
01177 struct skinny_req *req;
01178
01179 if (!(req = ast_calloc(1, skinny_header_size + size + 4)))
01180 return NULL;
01181
01182 req->len = htolel(size+4);
01183 req->e = htolel(response_message);
01184
01185 return req;
01186 }
01187
01188 static struct skinny_line *find_line_by_instance(struct skinny_device *d, int instance)
01189 {
01190 struct skinny_line *l;
01191
01192 for (l = d->lines; l; l = l->next) {
01193 if (l->instance == instance)
01194 break;
01195 }
01196
01197 if (!l) {
01198 ast_log(LOG_WARNING, "Could not find line with instance '%d' on device '%s'\n", instance, d->name);
01199 }
01200 return l;
01201 }
01202
01203 static struct skinny_line *find_line_by_name(const char *dest)
01204 {
01205 struct skinny_line *l;
01206 struct skinny_device *d;
01207 char line[256];
01208 char *at;
01209 char *device;
01210
01211 ast_copy_string(line, dest, sizeof(line));
01212 at = strchr(line, '@');
01213 if (!at) {
01214 ast_log(LOG_NOTICE, "Device '%s' has no @ (at) sign!\n", dest);
01215 return NULL;
01216 }
01217 *at++ = '\0';
01218 device = at;
01219 ast_mutex_lock(&devicelock);
01220 for (d = devices; d; d = d->next) {
01221 if (!strcasecmp(d->name, device)) {
01222 if (skinnydebug)
01223 ast_verbose("Found device: %s\n", d->name);
01224
01225 for (l = d->lines; l; l = l->next) {
01226
01227 if (!strcasecmp(l->name, line)) {
01228 ast_mutex_unlock(&devicelock);
01229 return l;
01230 }
01231 }
01232 }
01233 }
01234
01235 ast_mutex_unlock(&devicelock);
01236 return NULL;
01237 }
01238
01239
01240 static struct skinny_subchannel *find_subchannel_by_instance_reference(struct skinny_device *d, int instance, int reference)
01241 {
01242 struct skinny_line *l = find_line_by_instance(d, instance);
01243 struct skinny_subchannel *sub;
01244
01245 if (!l) {
01246 return NULL;
01247 }
01248
01249 for (sub = l->sub; sub; sub = sub->next) {
01250 if (sub->callid == reference)
01251 break;
01252 }
01253
01254 if (!sub) {
01255 ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s'\n", reference, d->name);
01256 }
01257 return sub;
01258 }
01259
01260
01261 static struct skinny_subchannel *find_subchannel_by_reference(struct skinny_device *d, int reference)
01262 {
01263 struct skinny_line *l;
01264 struct skinny_subchannel *sub = NULL;
01265
01266 for (l = d->lines; l; l = l->next) {
01267 for (sub = l->sub; sub; sub = sub->next) {
01268 if (sub->callid == reference)
01269 break;
01270 }
01271 if (sub)
01272 break;
01273 }
01274
01275 if (!l) {
01276 ast_log(LOG_WARNING, "Could not find any lines that contained a subchannel with reference '%d' on device '%s'\n", reference, d->name);
01277 } else {
01278 if (!sub) {
01279 ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s@%s'\n", reference, l->name, d->name);
01280 }
01281 }
01282 return sub;
01283 }
01284
01285 static struct skinny_speeddial *find_speeddial_by_instance(struct skinny_device *d, int instance)
01286 {
01287 struct skinny_speeddial *sd;
01288
01289 for (sd = d->speeddials; sd; sd = sd->next) {
01290 if (sd->instance == instance)
01291 break;
01292 }
01293
01294 if (!sd) {
01295 ast_log(LOG_WARNING, "Could not find speeddial with instance '%d' on device '%s'\n", instance, d->name);
01296 }
01297 return sd;
01298 }
01299
01300 static int codec_skinny2ast(enum skinny_codecs skinnycodec)
01301 {
01302 switch (skinnycodec) {
01303 case SKINNY_CODEC_ALAW:
01304 return AST_FORMAT_ALAW;
01305 case SKINNY_CODEC_ULAW:
01306 return AST_FORMAT_ULAW;
01307 case SKINNY_CODEC_G723_1:
01308 return AST_FORMAT_G723_1;
01309 case SKINNY_CODEC_G729A:
01310 return AST_FORMAT_G729A;
01311 case SKINNY_CODEC_G726_32:
01312 return AST_FORMAT_G726_AAL2;
01313 case SKINNY_CODEC_H261:
01314 return AST_FORMAT_H261;
01315 case SKINNY_CODEC_H263:
01316 return AST_FORMAT_H263;
01317 default:
01318 return 0;
01319 }
01320 }
01321
01322 static int codec_ast2skinny(int astcodec)
01323 {
01324 switch (astcodec) {
01325 case AST_FORMAT_ALAW:
01326 return SKINNY_CODEC_ALAW;
01327 case AST_FORMAT_ULAW:
01328 return SKINNY_CODEC_ULAW;
01329 case AST_FORMAT_G723_1:
01330 return SKINNY_CODEC_G723_1;
01331 case AST_FORMAT_G729A:
01332 return SKINNY_CODEC_G729A;
01333 case AST_FORMAT_G726_AAL2:
01334 return SKINNY_CODEC_G726_32;
01335 case AST_FORMAT_H261:
01336 return SKINNY_CODEC_H261;
01337 case AST_FORMAT_H263:
01338 return SKINNY_CODEC_H263;
01339 default:
01340 return 0;
01341 }
01342 }
01343
01344
01345 static int skinny_register(struct skinny_req *req, struct skinnysession *s)
01346 {
01347 struct skinny_device *d;
01348 struct sockaddr_in sin;
01349 socklen_t slen;
01350
01351 ast_mutex_lock(&devicelock);
01352 for (d = devices; d; d = d->next) {
01353 if (!strcasecmp(req->data.reg.name, d->id)
01354 && ast_apply_ha(d->ha, &(s->sin))) {
01355 s->device = d;
01356 d->type = letohl(req->data.reg.type);
01357 if (ast_strlen_zero(d->version_id)) {
01358 ast_copy_string(d->version_id, version_id, sizeof(d->version_id));
01359 }
01360 d->registered = 1;
01361 d->session = s;
01362
01363 slen = sizeof(sin);
01364 if (getsockname(s->fd, (struct sockaddr *)&sin, &slen)) {
01365 ast_log(LOG_WARNING, "Cannot get socket name\n");
01366 sin.sin_addr = __ourip;
01367 }
01368 d->ourip = sin.sin_addr;
01369 break;
01370 }
01371 }
01372 ast_mutex_unlock(&devicelock);
01373 if (!d) {
01374 return 0;
01375 }
01376 return 1;
01377 }
01378
01379 static int skinny_unregister(struct skinny_req *req, struct skinnysession *s)
01380 {
01381 struct skinny_device *d;
01382
01383 d = s->device;
01384
01385 if (d) {
01386 d->session = NULL;
01387 d->registered = 0;
01388 }
01389
01390 return -1;
01391 }
01392
01393 static int transmit_response(struct skinnysession *s, struct skinny_req *req)
01394 {
01395 int res = 0;
01396
01397 if (!s) {
01398 ast_log(LOG_WARNING, "Asked to transmit to a non-existant session!\n");
01399 return -1;
01400 }
01401
01402 ast_mutex_lock(&s->lock);
01403
01404 if (skinnydebug)
01405 ast_log(LOG_VERBOSE, "writing packet type %04X (%d bytes) to socket %d\n", letohl(req->e), letohl(req->len)+8, s->fd);
01406
01407 if (letohl(req->len > SKINNY_MAX_PACKET) || letohl(req->len < 0)) {
01408 ast_log(LOG_WARNING, "transmit_response: the length of the request is out of bounds\n");
01409 return -1;
01410 }
01411
01412 memset(s->outbuf,0,sizeof(s->outbuf));
01413 memcpy(s->outbuf, req, skinny_header_size);
01414 memcpy(s->outbuf+skinny_header_size, &req->data, letohl(req->len));
01415
01416 res = write(s->fd, s->outbuf, letohl(req->len)+8);
01417
01418 if (res != letohl(req->len)+8) {
01419 ast_log(LOG_WARNING, "Transmit: write only sent %d out of %d bytes: %s\n", res, letohl(req->len)+8, strerror(errno));
01420 if (res == -1) {
01421 if (skinnydebug)
01422 ast_log(LOG_WARNING, "Transmit: Skinny Client was lost, unregistering\n");
01423 skinny_unregister(NULL, s);
01424 }
01425
01426 }
01427
01428 ast_mutex_unlock(&s->lock);
01429 return 1;
01430 }
01431
01432 static void transmit_speaker_mode(struct skinnysession *s, int mode)
01433 {
01434 struct skinny_req *req;
01435
01436 if (!(req = req_alloc(sizeof(struct set_speaker_message), SET_SPEAKER_MESSAGE)))
01437 return;
01438
01439 req->data.setspeaker.mode = htolel(mode);
01440 transmit_response(s, req);
01441 }
01442
01443
01444
01445
01446
01447
01448
01449
01450
01451
01452
01453
01454 static void transmit_callstate(struct skinnysession *s, int instance, int state, unsigned callid)
01455 {
01456 struct skinny_req *req;
01457
01458 if (!(req = req_alloc(sizeof(struct call_state_message), CALL_STATE_MESSAGE)))
01459 return;
01460
01461 if (state == SKINNY_ONHOOK) {
01462 transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
01463 }
01464 req->data.callstate.callState = htolel(state);
01465 req->data.callstate.lineInstance = htolel(instance);
01466 req->data.callstate.callReference = htolel(callid);
01467 transmit_response(s, req);
01468 if (state == SKINNY_OFFHOOK) {
01469 if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
01470 return;
01471
01472 req->data.activatecallplane.lineInstance = htolel(instance);
01473 transmit_response(s, req);
01474 } else if (state == SKINNY_ONHOOK) {
01475 if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
01476 return;
01477
01478 req->data.activatecallplane.lineInstance = htolel(instance);
01479 transmit_response(s, req);
01480
01481 if (!(req = req_alloc(sizeof(struct close_receive_channel_message), CLOSE_RECEIVE_CHANNEL_MESSAGE)))
01482 return;
01483
01484 req->data.closereceivechannel.conferenceId = 0;
01485 req->data.closereceivechannel.partyId = htolel(callid);
01486 transmit_response(s, req);
01487
01488 if (!(req = req_alloc(sizeof(struct stop_media_transmission_message), STOP_MEDIA_TRANSMISSION_MESSAGE)))
01489 return;
01490
01491 req->data.stopmedia.conferenceId = 0;
01492 req->data.stopmedia.passThruPartyId = htolel(callid);
01493 transmit_response(s, req);
01494 }
01495 }
01496
01497 static void transmit_callinfo(struct skinnysession *s, const char *fromname, const char *fromnum, const char *toname, const char *tonum, int instance, int callid, int calltype)
01498 {
01499 struct skinny_req *req;
01500
01501 if (!(req = req_alloc(sizeof(struct call_info_message), CALL_INFO_MESSAGE)))
01502 return;
01503
01504 if (skinnydebug)
01505 ast_verbose("Setting Callinfo to %s(%s) from %s(%s) on %s(%d)\n", fromname, fromnum, toname, tonum, s->device->name, instance);
01506
01507 if (fromname) {
01508 ast_copy_string(req->data.callinfo.callingPartyName, fromname, sizeof(req->data.callinfo.callingPartyName));
01509 }
01510 if (fromnum) {
01511 ast_copy_string(req->data.callinfo.callingParty, fromnum, sizeof(req->data.callinfo.callingParty));
01512 }
01513 if (toname) {
01514 ast_copy_string(req->data.callinfo.calledPartyName, toname, sizeof(req->data.callinfo.calledPartyName));
01515 }
01516 if (tonum) {
01517 ast_copy_string(req->data.callinfo.calledParty, tonum, sizeof(req->data.callinfo.calledParty));
01518 }
01519 req->data.callinfo.instance = htolel(instance);
01520 req->data.callinfo.reference = htolel(callid);
01521 req->data.callinfo.type = htolel(calltype);
01522 transmit_response(s, req);
01523 }
01524
01525 static void transmit_connect(struct skinnysession *s, struct skinny_subchannel *sub)
01526 {
01527 struct skinny_req *req;
01528 struct skinny_line *l = sub->parent;
01529 struct ast_format_list fmt;
01530
01531 if (!(req = req_alloc(sizeof(struct open_receive_channel_message), OPEN_RECEIVE_CHANNEL_MESSAGE)))
01532 return;
01533
01534 fmt = ast_codec_pref_getsize(&l->prefs, ast_best_codec(l->capability));
01535
01536 req->data.openreceivechannel.conferenceId = htolel(0);
01537 req->data.openreceivechannel.partyId = htolel(sub->callid);
01538 req->data.openreceivechannel.packets = htolel(fmt.cur_ms);
01539 req->data.openreceivechannel.capability = htolel(codec_ast2skinny(fmt.bits));
01540 req->data.openreceivechannel.echo = htolel(0);
01541 req->data.openreceivechannel.bitrate = htolel(0);
01542 transmit_response(s, req);
01543 }
01544
01545 static void transmit_tone(struct skinnysession *s, int tone)
01546 {
01547 struct skinny_req *req;
01548
01549 if (tone == SKINNY_NOTONE) {
01550
01551 return;
01552 }
01553
01554 if (tone > 0) {
01555 if (!(req = req_alloc(sizeof(struct start_tone_message), START_TONE_MESSAGE)))
01556 return;
01557 } else {
01558 if (!(req = req_alloc(0, STOP_TONE_MESSAGE)))
01559 return;
01560 }
01561
01562 if (tone > 0) {
01563 req->data.starttone.tone = htolel(tone);
01564 }
01565 transmit_response(s, req);
01566 }
01567
01568 static void transmit_selectsoftkeys(struct skinnysession *s, int instance, int callid, int softkey)
01569 {
01570 struct skinny_req *req;
01571
01572 if (!(req = req_alloc(sizeof(struct select_soft_keys_message), SELECT_SOFT_KEYS_MESSAGE)))
01573 return;
01574
01575 req->data.selectsoftkey.instance = htolel(instance);
01576 req->data.selectsoftkey.reference = htolel(callid);
01577 req->data.selectsoftkey.softKeySetIndex = htolel(softkey);
01578 req->data.selectsoftkey.validKeyMask = htolel(0xFFFFFFFF);
01579 transmit_response(s, req);
01580 }
01581
01582 static void transmit_lamp_indication(struct skinnysession *s, int stimulus, int instance, int indication)
01583 {
01584 struct skinny_req *req;
01585
01586 if (!(req = req_alloc(sizeof(struct set_lamp_message), SET_LAMP_MESSAGE)))
01587 return;
01588
01589 req->data.setlamp.stimulus = htolel(stimulus);
01590 req->data.setlamp.stimulusInstance = htolel(instance);
01591 req->data.setlamp.deviceStimulus = htolel(indication);
01592 transmit_response(s, req);
01593 }
01594
01595 static void transmit_ringer_mode(struct skinnysession *s, int mode)
01596 {
01597 struct skinny_req *req;
01598
01599 if (skinnydebug)
01600 ast_verbose("Setting ringer mode to '%d'.\n", mode);
01601
01602 if (!(req = req_alloc(sizeof(struct set_ringer_message), SET_RINGER_MESSAGE)))
01603 return;
01604
01605 req->data.setringer.ringerMode = htolel(mode);
01606
01607
01608
01609
01610
01611
01612
01613 req->data.setringer.unknown1 = htolel(1);
01614
01615
01616 req->data.setringer.unknown2 = htolel(1);
01617 transmit_response(s, req);
01618 }
01619
01620 static void transmit_displaymessage(struct skinnysession *s, const char *text)
01621 {
01622 struct skinny_req *req;
01623
01624 if (text == 0) {
01625 if (!(req = req_alloc(0, CLEAR_DISPLAY_MESSAGE)))
01626 return;
01627
01628 if (skinnydebug)
01629 ast_verbose("Clearing Display\n");
01630 } else {
01631 if (!(req = req_alloc(sizeof(struct displaytext_message), DISPLAYTEXT_MESSAGE)))
01632 return;
01633
01634 ast_copy_string(req->data.displaytext.text, text, sizeof(req->data.displaytext.text));
01635 if (skinnydebug)
01636 ast_verbose("Displaying message '%s'\n", req->data.displaytext.text);
01637 }
01638
01639 transmit_response(s, req);
01640 }
01641
01642 static void transmit_displaynotify(struct skinnysession *s, const char *text, int t)
01643 {
01644 struct skinny_req *req;
01645
01646 if (!(req = req_alloc(sizeof(struct display_notify_message), DISPLAY_NOTIFY_MESSAGE)))
01647 return;
01648
01649 ast_copy_string(req->data.displaynotify.displayMessage, text, sizeof(req->data.displaynotify.displayMessage));
01650 req->data.displaynotify.displayTimeout = htolel(t);
01651
01652 if (skinnydebug)
01653 ast_verbose("Displaying notify '%s'\n", text);
01654
01655 transmit_response(s, req);
01656 }
01657
01658 static void transmit_displaypromptstatus(struct skinnysession *s, const char *text, int t, int instance, int callid)
01659 {
01660 struct skinny_req *req;
01661
01662 if (!(req = req_alloc(sizeof(struct display_prompt_status_message), DISPLAY_PROMPT_STATUS_MESSAGE)))
01663 return;
01664
01665 ast_copy_string(req->data.displaypromptstatus.promptMessage, text, sizeof(req->data.displaypromptstatus.promptMessage));
01666 req->data.displaypromptstatus.messageTimeout = htolel(t);
01667 req->data.displaypromptstatus.lineInstance = htolel(instance);
01668 req->data.displaypromptstatus.callReference = htolel(callid);
01669
01670 if (skinnydebug)
01671 ast_verbose("Displaying Prompt Status '%s'\n", text);
01672
01673 transmit_response(s, req);
01674 }
01675
01676 static void transmit_dialednumber(struct skinnysession *s, const char *text, int instance, int callid)
01677 {
01678 struct skinny_req *req;
01679
01680 if (!(req = req_alloc(sizeof(struct dialed_number_message), DIALED_NUMBER_MESSAGE)))
01681 return;
01682
01683 ast_copy_string(req->data.dialednumber.dialedNumber, text, sizeof(req->data.dialednumber.dialedNumber));
01684 req->data.dialednumber.lineInstance = htolel(instance);
01685 req->data.dialednumber.callReference = htolel(callid);
01686
01687 transmit_response(s, req);
01688 }
01689
01690
01691
01692
01693
01694
01695
01696
01697 static void do_housekeeping(struct skinnysession *s)
01698 {
01699
01700
01701
01702
01703
01704
01705
01706 transmit_displaymessage(s, NULL);
01707
01708
01709
01710
01711
01712
01713
01714
01715
01716
01717
01718
01719
01720
01721
01722 }
01723
01724
01725
01726
01727 static enum ast_rtp_get_result skinny_get_vrtp_peer(struct ast_channel *c, struct ast_rtp **rtp)
01728 {
01729 struct skinny_subchannel *sub = NULL;
01730
01731 if (!(sub = c->tech_pvt) || !(sub->vrtp))
01732 return AST_RTP_GET_FAILED;
01733
01734 *rtp = sub->vrtp;
01735
01736 return AST_RTP_TRY_NATIVE;
01737 }
01738
01739 static enum ast_rtp_get_result skinny_get_rtp_peer(struct ast_channel *c, struct ast_rtp **rtp)
01740 {
01741 struct skinny_subchannel *sub = NULL;
01742
01743 if (!(sub = c->tech_pvt) || !(sub->rtp))
01744 return AST_RTP_GET_FAILED;
01745
01746 *rtp = sub->rtp;
01747
01748 return AST_RTP_TRY_NATIVE;
01749 }
01750
01751 static int skinny_set_rtp_peer(struct ast_channel *c, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active)
01752 {
01753 struct skinny_subchannel *sub;
01754 sub = c->tech_pvt;
01755 if (sub) {
01756
01757 return 0;
01758 }
01759 return -1;
01760 }
01761
01762 static struct ast_rtp_protocol skinny_rtp = {
01763 .type = "Skinny",
01764 .get_rtp_info = skinny_get_rtp_peer,
01765 .get_vrtp_info = skinny_get_vrtp_peer,
01766 .set_rtp_peer = skinny_set_rtp_peer,
01767 };
01768
01769 static int skinny_do_debug(int fd, int argc, char *argv[])
01770 {
01771 if (argc != 3) {
01772 return RESULT_SHOWUSAGE;
01773 }
01774 skinnydebug = 1;
01775 ast_cli(fd, "Skinny Debugging Enabled\n");
01776 return RESULT_SUCCESS;
01777 }
01778
01779 static int skinny_no_debug(int fd, int argc, char *argv[])
01780 {
01781 if (argc != 4) {
01782 return RESULT_SHOWUSAGE;
01783 }
01784 skinnydebug = 0;
01785 ast_cli(fd, "Skinny Debugging Disabled\n");
01786 return RESULT_SUCCESS;
01787 }
01788
01789 static char *complete_skinny_reset(const char *line, const char *word, int pos, int state)
01790 {
01791 struct skinny_device *d;
01792
01793 char *result = NULL;
01794 int wordlen = strlen(word);
01795 int which = 0;
01796
01797 if (pos == 2) {
01798 for (d = devices; d && !result; d = d->next) {
01799 if (!strncasecmp(word, d->id, wordlen) && ++which > state)
01800 result = ast_strdup(d->id);
01801 }
01802 }
01803
01804 return result;
01805 }
01806
01807 static int skinny_reset_device(int fd, int argc, char *argv[])
01808 {
01809 struct skinny_device *d;
01810 struct skinny_req *req;
01811
01812 if (argc < 3 || argc > 4) {
01813 return RESULT_SHOWUSAGE;
01814 }
01815 ast_mutex_lock(&devicelock);
01816
01817 for (d = devices; d; d = d->next) {
01818 int fullrestart = 0;
01819 if (!strcasecmp(argv[2], d->id) || !strcasecmp(argv[2], "all")) {
01820 if (!(d->session))
01821 continue;
01822
01823 if (!(req = req_alloc(sizeof(struct reset_message), RESET_MESSAGE)))
01824 continue;
01825
01826 if (argc == 4 && !strcasecmp(argv[3], "restart"))
01827 fullrestart = 1;
01828
01829 if (fullrestart)
01830 req->data.reset.resetType = 2;
01831 else
01832 req->data.reset.resetType = 1;
01833
01834 if (option_verbose > 2)
01835 ast_verbose(VERBOSE_PREFIX_3 "%s device %s.\n", (fullrestart) ? "Restarting" : "Resetting", d->id);
01836 transmit_response(d->session, req);
01837 }
01838 }
01839 ast_mutex_unlock(&devicelock);
01840 return RESULT_SUCCESS;
01841 }
01842
01843 static char *device2str(int type)
01844 {
01845 char *tmp;
01846
01847 switch (type) {
01848 case SKINNY_DEVICE_NONE:
01849 return "No Device";
01850 case SKINNY_DEVICE_30SPPLUS:
01851 return "30SP Plus";
01852 case SKINNY_DEVICE_12SPPLUS:
01853 return "12SP Plus";
01854 case SKINNY_DEVICE_12SP:
01855 return "12SP";
01856 case SKINNY_DEVICE_12:
01857 return "12";
01858 case SKINNY_DEVICE_30VIP:
01859 return "30VIP";
01860 case SKINNY_DEVICE_7910:
01861 return "7910";
01862 case SKINNY_DEVICE_7960:
01863 return "7960";
01864 case SKINNY_DEVICE_7940:
01865 return "7940";
01866 case SKINNY_DEVICE_7935:
01867 return "7935";
01868 case SKINNY_DEVICE_ATA186:
01869 return "ATA186";
01870 case SKINNY_DEVICE_7941:
01871 return "7941";
01872 case SKINNY_DEVICE_7971:
01873 return "7971";
01874 case SKINNY_DEVICE_7985:
01875 return "7985";
01876 case SKINNY_DEVICE_7911:
01877 return "7911";
01878 case SKINNY_DEVICE_7961GE:
01879 return "7961GE";
01880 case SKINNY_DEVICE_7941GE:
01881 return "7941GE";
01882 case SKINNY_DEVICE_7905:
01883 return "7905";
01884 case SKINNY_DEVICE_7920:
01885 return "7920";
01886 case SKINNY_DEVICE_7970:
01887 return "7970";
01888 case SKINNY_DEVICE_7912:
01889 return "7912";
01890 case SKINNY_DEVICE_7902:
01891 return "7902";
01892 case SKINNY_DEVICE_CIPC:
01893 return "IP Communicator";
01894 case SKINNY_DEVICE_7961:
01895 return "7961";
01896 case SKINNY_DEVICE_7936:
01897 return "7936";
01898 case SKINNY_DEVICE_SCCPGATEWAY_AN:
01899 return "SCCPGATEWAY_AN";
01900 case SKINNY_DEVICE_SCCPGATEWAY_BRI:
01901 return "SCCPGATEWAY_BRI";
01902 case SKINNY_DEVICE_UNKNOWN:
01903 return "Unknown";
01904 default:
01905 if (!(tmp = ast_threadstorage_get(&device2str_threadbuf, DEVICE2STR_BUFSIZE)))
01906 return "Unknown";
01907 snprintf(tmp, DEVICE2STR_BUFSIZE, "UNKNOWN-%d", type);
01908 return tmp;
01909 }
01910 }
01911
01912 static int skinny_show_devices(int fd, int argc, char *argv[])
01913 {
01914 struct skinny_device *d;
01915 struct skinny_line *l;
01916 int numlines = 0;
01917
01918 if (argc != 3) {
01919 return RESULT_SHOWUSAGE;
01920 }
01921 ast_mutex_lock(&devicelock);
01922
01923 ast_cli(fd, "Name DeviceId IP Type R NL\n");
01924 ast_cli(fd, "-------------------- ---------------- --------------- --------------- - --\n");
01925 for (d = devices; d; d = d->next) {
01926 numlines = 0;
01927 for (l = d->lines; l; l = l->next) {
01928 numlines++;
01929 }
01930
01931 ast_cli(fd, "%-20s %-16s %-15s %-15s %c %2d\n",
01932 d->name,
01933 d->id,
01934 d->session?ast_inet_ntoa(d->session->sin.sin_addr):"",
01935 device2str(d->type),
01936 d->registered?'Y':'N',
01937 numlines);
01938 }
01939 ast_mutex_unlock(&devicelock);
01940 return RESULT_SUCCESS;
01941 }
01942
01943 static int skinny_show_lines(int fd, int argc, char *argv[])
01944 {
01945 struct skinny_device *d;
01946 struct skinny_line *l;
01947
01948 if (argc != 3) {
01949 return RESULT_SHOWUSAGE;
01950 }
01951 ast_mutex_lock(&devicelock);
01952
01953 ast_cli(fd, "Device Name Instance Name Label \n");
01954 ast_cli(fd, "-------------------- -------- -------------------- --------------------\n");
01955 for (d = devices; d; d = d->next) {
01956 for (l = d->lines; l; l = l->next) {
01957 ast_cli(fd, "%-20s %8d %-20s %-20s\n",
01958 d->name,
01959 l->instance,
01960 l->name,
01961 l->label);
01962 }
01963 }
01964
01965 ast_mutex_unlock(&devicelock);
01966 return RESULT_SUCCESS;
01967 }
01968
01969 static char show_devices_usage[] =
01970 "Usage: skinny show devices\n"
01971 " Lists all devices known to the Skinny subsystem.\n";
01972
01973 static char show_lines_usage[] =
01974 "Usage: skinny show lines\n"
01975 " Lists all lines known to the Skinny subsystem.\n";
01976
01977 static char debug_usage[] =
01978 "Usage: skinny set debug\n"
01979 " Enables dumping of Skinny packets for debugging purposes\n";
01980
01981 static char no_debug_usage[] =
01982 "Usage: skinny set debug off\n"
01983 " Disables dumping of Skinny packets for debugging purposes\n";
01984
01985 static char reset_usage[] =
01986 "Usage: skinny reset <DeviceId|all> [restart]\n"
01987 " Causes a Skinny device to reset itself, optionally with a full restart\n";
01988
01989 static struct ast_cli_entry cli_skinny[] = {
01990 { { "skinny", "show", "devices", NULL },
01991 skinny_show_devices, "List defined Skinny devices",
01992 show_devices_usage },
01993
01994 { { "skinny", "show", "lines", NULL },
01995 skinny_show_lines, "List defined Skinny lines per device",
01996 show_lines_usage },
01997
01998 { { "skinny", "set", "debug", NULL },
01999 skinny_do_debug, "Enable Skinny debugging",
02000 debug_usage },
02001
02002 { { "skinny", "set", "debug", "off", NULL },
02003 skinny_no_debug, "Disable Skinny debugging",
02004 no_debug_usage },
02005
02006 { { "skinny", "reset", NULL },
02007 skinny_reset_device, "Reset Skinny device(s)",
02008 reset_usage, complete_skinny_reset },
02009 };
02010
02011 #if 0
02012 static struct skinny_paging_device *build_paging_device(const char *cat, struct ast_variable *v)
02013 {
02014 return NULL;
02015 }
02016 #endif
02017
02018 static struct skinny_device *build_device(const char *cat, struct ast_variable *v)
02019 {
02020 struct skinny_device *d;
02021 struct skinny_line *l;
02022 struct skinny_speeddial *sd;
02023 struct skinny_addon *a;
02024 int lineInstance = 1;
02025 int speeddialInstance = 1;
02026 int y = 0;
02027
02028 if (!(d = ast_calloc(1, sizeof(struct skinny_device)))) {
02029 return NULL;
02030 } else {
02031 ast_copy_string(d->name, cat, sizeof(d->name));
02032 d->lastlineinstance = 1;
02033 d->capability = default_capability;
02034 d->prefs = default_prefs;
02035 while(v) {
02036 if (!strcasecmp(v->name, "host")) {
02037 if (ast_get_ip(&d->addr, v->value)) {
02038 free(d);
02039 return NULL;
02040 }
02041 } else if (!strcasecmp(v->name, "port")) {
02042 d->addr.sin_port = htons(atoi(v->value));
02043 } else if (!strcasecmp(v->name, "device")) {
02044 ast_copy_string(d->id, v->value, sizeof(d->id));
02045 } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
02046 d->ha = ast_append_ha(v->name, v->value, d->ha);
02047 } else if (!strcasecmp(v->name, "context")) {
02048 ast_copy_string(context, v->value, sizeof(context));
02049 } else if (!strcasecmp(v->name, "allow")) {
02050 ast_parse_allow_disallow(&d->prefs, &d->capability, v->value, 1);
02051 } else if (!strcasecmp(v->name, "disallow")) {
02052 ast_parse_allow_disallow(&d->prefs, &d->capability, v->value, 0);
02053 } else if (!strcasecmp(v->name, "version")) {
02054 ast_copy_string(d->version_id, v->value, sizeof(d->version_id));
02055 } else if (!strcasecmp(v->name, "nat")) {
02056 nat = ast_true(v->value);
02057 } else if (!strcasecmp(v->name, "callerid")) {
02058 if (!strcasecmp(v->value, "asreceived")) {
02059 cid_num[0] = '\0';
02060 cid_name[0] = '\0';
02061 } else {
02062 ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
02063 }
02064 } else if (!strcasecmp(v->name, "language")) {
02065 ast_copy_string(language, v->value, sizeof(language));
02066 } else if (!strcasecmp(v->name, "accountcode")) {
02067 ast_copy_string(accountcode, v->value, sizeof(accountcode));
02068 } else if (!strcasecmp(v->name, "amaflags")) {
02069 y = ast_cdr_amaflags2int(v->value);
02070 if (y < 0) {
02071 ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno);
02072 } else {
02073 amaflags = y;
02074 }
02075 } else if (!strcasecmp(v->name, "mohinterpret") || !strcasecmp(v->name, "musiconhold")) {
02076 ast_copy_string(mohinterpret, v->value, sizeof(mohinterpret));
02077 } else if (!strcasecmp(v->name, "mohsuggest")) {
02078 ast_copy_string(mohsuggest, v->value, sizeof(mohsuggest));
02079 } else if (!strcasecmp(v->name, "callgroup")) {
02080 cur_callergroup = ast_get_group(v->value);
02081 } else if (!strcasecmp(v->name, "pickupgroup")) {
02082 cur_pickupgroup = ast_get_group(v->value);
02083 } else if (!strcasecmp(v->name, "immediate")) {
02084 immediate = ast_true(v->value);
02085 } else if (!strcasecmp(v->name, "cancallforward")) {
02086 cancallforward = ast_true(v->value);
02087 } else if (!strcasecmp(v->name, "mailbox")) {
02088 ast_copy_string(mailbox, v->value, sizeof(mailbox));
02089 } else if (!strcasecmp(v->name, "callreturn")) {
02090 callreturn = ast_true(v->value);
02091 } else if (!strcasecmp(v->name, "callwaiting")) {
02092 callwaiting = ast_true(v->value);
02093 } else if (!strcasecmp(v->name, "transfer")) {
02094 transfer = ast_true(v->value);
02095 } else if (!strcasecmp(v->name, "threewaycalling")) {
02096 threewaycalling = ast_true(v->value);
02097 } else if (!strcasecmp(v->name, "mwiblink")) {
02098 mwiblink = ast_true(v->value);
02099 } else if (!strcasecmp(v->name, "linelabel")) {
02100 ast_copy_string(linelabel, v->value, sizeof(linelabel));
02101 } else if (!strcasecmp(v->name, "speeddial")) {
02102 if (!(sd = ast_calloc(1, sizeof(struct skinny_speeddial)))) {
02103 return NULL;
02104 } else {
02105 char *stringp, *exten, *label;
02106 stringp = v->value;
02107 exten = strsep(&stringp, ",");
02108 label = strsep(&stringp, ",");
02109 ast_mutex_init(&sd->lock);
02110 ast_copy_string(sd->exten, exten, sizeof(sd->exten));
02111 if (label)
02112 ast_copy_string(sd->label, label, sizeof(sd->label));
02113 else
02114 ast_copy_string(sd->label, exten, sizeof(sd->label));
02115 sd->instance = speeddialInstance++;
02116
02117 sd->parent = d;
02118
02119 sd->next = d->speeddials;
02120 d->speeddials = sd;
02121 }
02122 } else if (!strcasecmp(v->name, "addon")) {
02123 if (!(a = ast_calloc(1, sizeof(struct skinny_addon)))) {
02124 return NULL;
02125 } else {
02126 ast_mutex_init(&a->lock);
02127 ast_copy_string(a->type, v->value, sizeof(a->type));
02128
02129 a->next = d->addons;
02130 d->addons = a;
02131 }
02132 } else if (!strcasecmp(v->name, "trunk") || !strcasecmp(v->name, "line")) {
02133 if (!(l = ast_calloc(1, sizeof(struct skinny_line)))) {
02134 return NULL;
02135 } else {
02136 ast_mutex_init(&l->lock);
02137 ast_copy_string(l->name, v->value, sizeof(l->name));
02138
02139
02140 ast_copy_string(l->context, context, sizeof(l->context));
02141 ast_copy_string(l->cid_num, cid_num, sizeof(l->cid_num));
02142 ast_copy_string(l->cid_name, cid_name, sizeof(l->cid_name));
02143 ast_copy_string(l->label, linelabel, sizeof(l->label));
02144 ast_copy_string(l->language, language, sizeof(l->language));
02145 ast_copy_string(l->mohinterpret, mohinterpret, sizeof(l->mohinterpret));
02146 ast_copy_string(l->mohsuggest, mohsuggest, sizeof(l->mohsuggest));
02147 ast_copy_string(l->mailbox, mailbox, sizeof(l->mailbox));
02148 ast_copy_string(l->mailbox, mailbox, sizeof(l->mailbox));
02149 if (!ast_strlen_zero(mailbox)) {
02150 if (option_verbose > 2)
02151 ast_verbose(VERBOSE_PREFIX_3 "Setting mailbox '%s' on %s@%s\n", mailbox, d->name, l->name);
02152 }
02153 l->msgstate = -1;
02154 l->capability = d->capability;
02155 l->prefs = d->prefs;
02156 l->parent = d;
02157 if (!strcasecmp(v->name, "trunk")) {
02158 l->type = TYPE_TRUNK;
02159 } else {
02160 l->type = TYPE_LINE;
02161 }
02162 l->immediate = immediate;
02163 l->callgroup = cur_callergroup;
02164 l->pickupgroup = cur_pickupgroup;
02165 l->callreturn = callreturn;
02166 l->cancallforward = cancallforward;
02167 l->callwaiting = callwaiting;
02168 l->transfer = transfer;
02169 l->threewaycalling = threewaycalling;
02170 l->mwiblink = mwiblink;
02171 l->onhooktime = time(NULL);
02172 l->instance = lineInstance++;
02173
02174 l->hookstate = SKINNY_ONHOOK;
02175 l->nat = nat;
02176
02177 l->next = d->lines;
02178 d->lines = l;
02179 }
02180 } else {
02181 ast_log(LOG_WARNING, "Don't know keyword '%s' at line %d\n", v->name, v->lineno);
02182 }
02183 v = v->next;
02184 }
02185
02186 if (!d->lines) {
02187 ast_log(LOG_ERROR, "A Skinny device must have at least one line!\n");
02188 return NULL;
02189 }
02190 if (!ntohs(d->addr.sin_port)) {
02191 d->addr.sin_port = htons(DEFAULT_SKINNY_PORT);
02192 }
02193 #if 0
02194
02195 if (d->addr.sin_addr.s_addr) {
02196
02197 if (ast_ouraddrfor(&d->addr.sin_addr, &d->ourip)) {
02198 d->ourip = __ourip;
02199 }
02200 } else {
02201 d->ourip = __ourip;
02202 }
02203 #endif
02204 }
02205 return d;
02206 }
02207
02208 static void start_rtp(struct skinny_subchannel *sub)
02209 {
02210 struct skinny_line *l = sub->parent;
02211 struct skinny_device *d = l->parent;
02212 int hasvideo = 0;
02213
02214 ast_mutex_lock(&sub->lock);
02215
02216 sub->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
02217 if (hasvideo)
02218 sub->vrtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
02219
02220 if (sub->rtp && sub->owner) {
02221 sub->owner->fds[0] = ast_rtp_fd(sub->rtp);
02222 sub->owner->fds[1] = ast_rtcp_fd(sub->rtp);
02223 }
02224 if (hasvideo && sub->vrtp && sub->owner) {
02225 sub->owner->fds[2] = ast_rtp_fd(sub->vrtp);
02226 sub->owner->fds[3] = ast_rtcp_fd(sub->vrtp);
02227 }
02228 if (sub->rtp) {
02229 ast_rtp_setnat(sub->rtp, l->nat);
02230 }
02231 if (sub->vrtp) {
02232 ast_rtp_setnat(sub->vrtp, l->nat);
02233 }
02234
02235 if (sub->rtp)
02236 ast_rtp_codec_setpref(sub->rtp, &l->prefs);
02237
02238
02239 transmit_connect(d->session, sub);
02240 ast_mutex_unlock(&sub->lock);
02241 }
02242
02243 static void *skinny_newcall(void *data)
02244 {
02245 struct ast_channel *c = data;
02246 struct skinny_subchannel *sub = c->tech_pvt;
02247 struct skinny_line *l = sub->parent;
02248 struct skinny_device *d = l->parent;
02249 struct skinnysession *s = d->session;
02250 int res = 0;
02251
02252 ast_copy_string(l->lastnumberdialed, c->exten, sizeof(l->lastnumberdialed));
02253 ast_set_callerid(c,
02254 l->hidecallerid ? "" : l->cid_num,
02255 l->hidecallerid ? "" : l->cid_name,
02256 c->cid.cid_ani ? NULL : l->cid_num);
02257 ast_setstate(c, AST_STATE_RING);
02258 res = ast_pbx_run(c);
02259 if (res) {
02260 ast_log(LOG_WARNING, "PBX exited non-zero\n");
02261 transmit_tone(s, SKINNY_REORDER);
02262 }
02263 return NULL;
02264 }
02265
02266 static void *skinny_ss(void *data)
02267 {
02268 struct ast_channel *c = data;
02269 struct skinny_subchannel *sub = c->tech_pvt;
02270 struct skinny_line *l = sub->parent;
02271 struct skinny_device *d = l->parent;
02272 struct skinnysession *s = d->session;
02273 char exten[AST_MAX_EXTENSION] = "";
02274 int len = 0;
02275 int timeout = firstdigittimeout;
02276 int res;
02277 int getforward=0;
02278
02279 if (option_verbose > 2)
02280 ast_verbose( VERBOSE_PREFIX_3 "Starting simple switch on '%s@%s'\n", l->name, d->name);
02281
02282 while (len < AST_MAX_EXTENSION-1) {
02283 res = ast_waitfordigit(c, timeout);
02284 timeout = 0;
02285 if (res < 0) {
02286 if (skinnydebug)
02287 ast_verbose("Skinny(%s@%s): waitfordigit returned < 0\n", l->name, d->name);
02288 ast_indicate(c, -1);
02289 ast_hangup(c);
02290 return NULL;
02291 } else if (res) {
02292 exten[len++]=res;
02293 exten[len] = '\0';
02294 }
02295 if (!ast_ignore_pattern(c->context, exten)) {
02296 transmit_tone(s, SKINNY_SILENCE);
02297 }
02298 if (ast_exists_extension(c, c->context, exten, 1, l->cid_num)) {
02299 if (!res || !ast_matchmore_extension(c, c->context, exten, 1, l->cid_num)) {
02300 if (getforward) {
02301
02302 ast_copy_string(l->call_forward, exten, sizeof(l->call_forward));
02303 if (option_verbose > 2)
02304 ast_verbose(VERBOSE_PREFIX_3 "Setting call forward to '%s' on channel %s\n",
02305 l->call_forward, c->name);
02306 transmit_tone(s, SKINNY_DIALTONE);
02307 if (res) {
02308 break;
02309 }
02310 ast_safe_sleep(c, 500);
02311 ast_indicate(c, -1);
02312 ast_safe_sleep(c, 1000);
02313 memset(exten, 0, sizeof(exten));
02314 transmit_tone(s, SKINNY_DIALTONE);
02315 len = 0;
02316 getforward = 0;
02317 } else {
02318 ast_copy_string(c->exten, exten, sizeof(c->exten));
02319 ast_copy_string(l->lastnumberdialed, exten, sizeof(l->lastnumberdialed));
02320 skinny_newcall(c);
02321 return NULL;
02322 }
02323 } else {
02324
02325
02326 timeout = matchdigittimeout;
02327 }
02328 } else if (res == 0) {
02329 ast_log(LOG_DEBUG, "Not enough digits (and no ambiguous match)...\n");
02330 transmit_tone(s, SKINNY_REORDER);
02331 ast_hangup(c);
02332 return NULL;
02333 } else if (!ast_canmatch_extension(c, c->context, exten, 1, c->cid.cid_num) &&
02334 ((exten[0] != '*') || (!ast_strlen_zero(exten) > 2))) {
02335 ast_log(LOG_WARNING, "Can't match [%s] from '%s' in context %s\n", exten, c->cid.cid_num ? c->cid.cid_num : "<Unknown Caller>", c->context);
02336 transmit_tone(s, SKINNY_REORDER);
02337
02338 ast_safe_sleep(c, 3000);
02339 break;
02340 }
02341 if (!timeout) {
02342 timeout = gendigittimeout;
02343 }
02344 if (len && !ast_ignore_pattern(c->context, exten)) {
02345 ast_indicate(c, -1);
02346 }
02347 }
02348 ast_hangup(c);
02349 return NULL;
02350 }
02351
02352
02353
02354 static int skinny_call(struct ast_channel *ast, char *dest, int timeout)
02355 {
02356 int res = 0;
02357 int tone = 0;
02358 struct skinny_subchannel *sub = ast->tech_pvt;
02359 struct skinny_line *l = sub->parent;
02360 struct skinny_device *d = l->parent;
02361 struct skinnysession *s = d->session;
02362
02363 if (!d->registered) {
02364 ast_log(LOG_ERROR, "Device not registered, cannot call %s\n", dest);
02365 return -1;
02366 }
02367
02368 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
02369 ast_log(LOG_WARNING, "skinny_call called on %s, neither down nor reserved\n", ast->name);
02370 return -1;
02371 }
02372
02373 if (skinnydebug)
02374 ast_verbose(VERBOSE_PREFIX_3 "skinny_call(%s)\n", ast->name);
02375
02376 if (l->dnd) {
02377 ast_queue_control(ast, AST_CONTROL_BUSY);
02378 return -1;
02379 }
02380
02381 switch (l->hookstate) {
02382 case SKINNY_OFFHOOK:
02383 tone = SKINNY_CALLWAITTONE;
02384 break;
02385 case SKINNY_ONHOOK:
02386 tone = SKINNY_ALERT;
02387 break;
02388 default:
02389 ast_log(LOG_ERROR, "Don't know how to deal with hookstate %d\n", l->hookstate);
02390 break;
02391 }
02392
02393 transmit_callstate(s, l->instance, SKINNY_RINGIN, sub->callid);
02394 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_RINGIN);
02395 transmit_displaypromptstatus(s, "Ring-In", 0, l->instance, sub->callid);
02396 transmit_callinfo(s, ast->cid.cid_name, ast->cid.cid_num, l->cid_name, l->cid_num, l->instance, sub->callid, 1);
02397 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_BLINK);
02398 transmit_ringer_mode(s, SKINNY_RING_INSIDE);
02399
02400 ast_setstate(ast, AST_STATE_RINGING);
02401 ast_queue_control(ast, AST_CONTROL_RINGING);
02402 sub->outgoing = 1;
02403 return res;
02404 }
02405
02406 static int skinny_hangup(struct ast_channel *ast)
02407 {
02408 struct skinny_subchannel *sub = ast->tech_pvt;
02409 struct skinny_line *l;
02410 struct skinny_device *d;
02411 struct skinnysession *s;
02412
02413 if (!sub) {
02414 ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
02415 return 0;
02416 }
02417 l = sub->parent;
02418 d = l->parent;
02419 s = d->session;
02420 if (skinnydebug)
02421 ast_verbose("skinny_hangup(%s) on %s@%s\n", ast->name, l->name, d->name);
02422
02423 if (d->registered) {
02424 if ((l->type = TYPE_LINE) && (l->hookstate == SKINNY_OFFHOOK)) {
02425 l->hookstate = SKINNY_ONHOOK;
02426 transmit_callstate(s, l->instance, SKINNY_ONHOOK, sub->callid);
02427 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_OFF);
02428 transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
02429 } else if ((l->type = TYPE_LINE) && (l->hookstate == SKINNY_ONHOOK)) {
02430 transmit_callstate(s, l->instance, SKINNY_ONHOOK, sub->callid);
02431 transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
02432 transmit_ringer_mode(s, SKINNY_RING_OFF);
02433 transmit_tone(s, SKINNY_SILENCE);
02434 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_OFF);
02435 do_housekeeping(s);
02436 }
02437 }
02438 ast_mutex_lock(&sub->lock);
02439 sub->owner = NULL;
02440 ast->tech_pvt = NULL;
02441 sub->alreadygone = 0;
02442 sub->outgoing = 0;
02443 if (sub->rtp) {
02444 ast_rtp_destroy(sub->rtp);
02445 sub->rtp = NULL;
02446 }
02447 ast_mutex_unlock(&sub->lock);
02448 return 0;
02449 }
02450
02451 static int skinny_answer(struct ast_channel *ast)
02452 {
02453 int res = 0;
02454 struct skinny_subchannel *sub = ast->tech_pvt;
02455 struct skinny_line *l = sub->parent;
02456 struct skinny_device *d = l->parent;
02457 struct skinnysession *s = d->session;
02458
02459 sub->cxmode = SKINNY_CX_SENDRECV;
02460 if (!sub->rtp) {
02461 start_rtp(sub);
02462 }
02463 if (skinnydebug)
02464 ast_verbose("skinny_answer(%s) on %s@%s-%d\n", ast->name, l->name, d->name, sub->callid);
02465 if (ast->_state != AST_STATE_UP) {
02466 ast_setstate(ast, AST_STATE_UP);
02467 }
02468
02469 transmit_tone(s, SKINNY_SILENCE);
02470
02471
02472
02473 transmit_callinfo(s, ast->cid.cid_name, ast->cid.cid_num, ast->exten, ast->exten, l->instance, sub->callid, 2);
02474 transmit_callstate(s, l->instance, SKINNY_CONNECTED, sub->callid);
02475 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_CONNECTED);
02476 transmit_displaypromptstatus(s, "Connected", 0, l->instance, sub->callid);
02477 return res;
02478 }
02479
02480
02481 static struct ast_frame *skinny_rtp_read(struct skinny_subchannel *sub)
02482 {
02483 struct ast_channel *ast = sub->owner;
02484 struct ast_frame *f;
02485
02486 if (!sub->rtp) {
02487
02488 return &ast_null_frame;
02489 }
02490
02491 switch(ast->fdno) {
02492 case 0:
02493 f = ast_rtp_read(sub->rtp);
02494 break;
02495 case 1:
02496 f = ast_rtcp_read(sub->rtp);
02497 break;
02498 case 2:
02499 f = ast_rtp_read(sub->vrtp);
02500 break;
02501 case 3:
02502 f = ast_rtcp_read(sub->vrtp);
02503 break;
02504 #if 0
02505 case 5:
02506
02507 f = ast_udptl_read(sub->udptl);
02508 break;
02509 #endif
02510 default:
02511 f = &ast_null_frame;
02512 }
02513
02514 if (ast) {
02515
02516 if (f->frametype == AST_FRAME_VOICE) {
02517 if (f->subclass != ast->nativeformats) {
02518 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
02519 ast->nativeformats = f->subclass;
02520 ast_set_read_format(ast, ast->readformat);
02521 ast_set_write_format(ast, ast->writeformat);
02522 }
02523 }
02524 }
02525 return f;
02526 }
02527
02528 static struct ast_frame *skinny_read(struct ast_channel *ast)
02529 {
02530 struct ast_frame *fr;
02531 struct skinny_subchannel *sub = ast->tech_pvt;
02532 ast_mutex_lock(&sub->lock);
02533 fr = skinny_rtp_read(sub);
02534 ast_mutex_unlock(&sub->lock);
02535 return fr;
02536 }
02537
02538 static int skinny_write(struct ast_channel *ast, struct ast_frame *frame)
02539 {
02540 struct skinny_subchannel *sub = ast->tech_pvt;
02541 int res = 0;
02542 if (frame->frametype != AST_FRAME_VOICE) {
02543 if (frame->frametype == AST_FRAME_IMAGE) {
02544 return 0;
02545 } else {
02546 ast_log(LOG_WARNING, "Can't send %d type frames with skinny_write\n", frame->frametype);
02547 return 0;
02548 }
02549 } else {
02550 if (!(frame->subclass & ast->nativeformats)) {
02551 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
02552 frame->subclass, ast->nativeformats, ast->readformat, ast->writeformat);
02553 return -1;
02554 }
02555 }
02556 if (sub) {
02557 ast_mutex_lock(&sub->lock);
02558 if (sub->rtp) {
02559 res = ast_rtp_write(sub->rtp, frame);
02560 }
02561 ast_mutex_unlock(&sub->lock);
02562 }
02563 return res;
02564 }
02565
02566 static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
02567 {
02568 struct skinny_subchannel *sub = newchan->tech_pvt;
02569 ast_log(LOG_NOTICE, "skinny_fixup(%s, %s)\n", oldchan->name, newchan->name);
02570 if (sub->owner != oldchan) {
02571 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, sub->owner);
02572 return -1;
02573 }
02574 sub->owner = newchan;
02575 return 0;
02576 }
02577
02578 static int skinny_senddigit_begin(struct ast_channel *ast, char digit)
02579 {
02580 return -1;
02581 }
02582
02583 static int skinny_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
02584 {
02585 #if 0
02586 struct skinny_subchannel *sub = ast->tech_pvt;
02587 struct skinny_line *l = sub->parent;
02588 struct skinny_device *d = l->parent;
02589 int tmp;
02590
02591 sprintf(tmp, "%d", digit);
02592 transmit_tone(d->session, digit);
02593 #endif
02594 return -1;
02595 }
02596
02597 static char *control2str(int ind) {
02598 char *tmp;
02599
02600 switch (ind) {
02601 case AST_CONTROL_HANGUP:
02602 return "Other end has hungup";
02603 case AST_CONTROL_RING:
02604 return "Local ring";
02605 case AST_CONTROL_RINGING:
02606 return "Remote end is ringing";
02607 case AST_CONTROL_ANSWER:
02608 return "Remote end has answered";
02609 case AST_CONTROL_BUSY:
02610 return "Remote end is busy";
02611 case AST_CONTROL_TAKEOFFHOOK:
02612 return "Make it go off hook";
02613 case AST_CONTROL_OFFHOOK:
02614 return "Line is off hook";
02615 case AST_CONTROL_CONGESTION:
02616 return "Congestion (circuits busy)";
02617 case AST_CONTROL_FLASH:
02618 return "Flash hook";
02619 case AST_CONTROL_WINK:
02620 return "Wink";
02621 case AST_CONTROL_OPTION:
02622 return "Set a low-level option";
02623 case AST_CONTROL_RADIO_KEY:
02624 return "Key Radio";
02625 case AST_CONTROL_RADIO_UNKEY:
02626 return "Un-Key Radio";
02627 case AST_CONTROL_PROGRESS:
02628 return "Remote end is making Progress";
02629 case AST_CONTROL_PROCEEDING:
02630 return "Remote end is proceeding";
02631 case AST_CONTROL_HOLD:
02632 return "Hold";
02633 case AST_CONTROL_UNHOLD:
02634 return "Unhold";
02635 case -1:
02636 return "Stop tone";
02637 default:
02638 if (!(tmp = ast_threadstorage_get(&control2str_threadbuf, CONTROL2STR_BUFSIZE)))
02639 return "Unknown";
02640 snprintf(tmp, CONTROL2STR_BUFSIZE, "UNKNOWN-%d", ind);
02641 return tmp;
02642 }
02643 }
02644
02645
02646 static int skinny_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen)
02647 {
02648 struct skinny_subchannel *sub = ast->tech_pvt;
02649 struct skinny_line *l = sub->parent;
02650 struct skinny_device *d = l->parent;
02651 struct skinnysession *s = d->session;
02652
02653 if (skinnydebug)
02654 ast_verbose(VERBOSE_PREFIX_3 "Asked to indicate '%s' condition on channel %s\n", control2str(ind), ast->name);
02655 switch(ind) {
02656 case AST_CONTROL_RINGING:
02657 if (ast->_state != AST_STATE_UP) {
02658 if (!sub->progress) {
02659 transmit_tone(s, SKINNY_ALERT);
02660 transmit_callstate(s, l->instance, SKINNY_RINGOUT, sub->callid);
02661 transmit_dialednumber(s, ast->exten, l->instance, sub->callid);
02662 transmit_displaypromptstatus(s, "Ring Out", 0, l->instance, sub->callid);
02663 transmit_callinfo(s, ast->cid.cid_name, ast->cid.cid_num, ast->exten, ast->exten, l->instance, sub->callid, 2);
02664 sub->ringing = 1;
02665 break;
02666 }
02667 }
02668 return -1;
02669 case AST_CONTROL_BUSY:
02670 if (ast->_state != AST_STATE_UP) {
02671 transmit_tone(s, SKINNY_BUSYTONE);
02672 transmit_callstate(s, l->instance, SKINNY_BUSY, sub->callid);
02673 sub->alreadygone = 1;
02674 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
02675 break;
02676 }
02677 return -1;
02678 case AST_CONTROL_CONGESTION:
02679 if (ast->_state != AST_STATE_UP) {
02680 transmit_tone(s, SKINNY_REORDER);
02681 transmit_callstate(s, l->instance, SKINNY_CONGESTION, sub->callid);
02682 sub->alreadygone = 1;
02683 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
02684 break;
02685 }
02686 return -1;
02687 case AST_CONTROL_PROGRESS:
02688 if ((ast->_state != AST_STATE_UP) && !sub->progress && !sub->outgoing) {
02689 transmit_tone(s, SKINNY_ALERT);
02690 transmit_callstate(s, l->instance, SKINNY_PROGRESS, sub->callid);
02691 transmit_displaypromptstatus(s, "Call Progress", 0, l->instance, sub->callid);
02692 transmit_callinfo(s, ast->cid.cid_name, ast->cid.cid_num, ast->exten, ast->exten, l->instance, sub->callid, 2);
02693 sub->progress = 1;
02694 break;
02695 }
02696 return -1;
02697 case -1:
02698 transmit_tone(s, SKINNY_SILENCE);
02699 break;
02700 case AST_CONTROL_HOLD:
02701 ast_moh_start(ast, data, l->mohinterpret);
02702 break;
02703 case AST_CONTROL_UNHOLD:
02704 ast_moh_stop(ast);
02705 break;
02706 case AST_CONTROL_PROCEEDING:
02707 break;
02708 default:
02709 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", ind);
02710 return -1;
02711 }
02712 return 0;
02713 }
02714
02715 static struct ast_channel *skinny_new(struct skinny_line *l, int state)
02716 {
02717 struct ast_channel *tmp;
02718 struct skinny_subchannel *sub;
02719 struct skinny_device *d = l->parent;
02720 int fmt;
02721
02722 tmp = ast_channel_alloc(1, state, l->cid_num, l->cid_name, l->accountcode, l->exten, l->context, l->amaflags, "Skinny/%s@%s-%d", l->name, d->name, callnums);
02723 if (!tmp) {
02724 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
02725 return NULL;
02726 } else {
02727 sub = ast_calloc(1, sizeof(struct skinny_subchannel));
02728 if (!sub) {
02729 ast_log(LOG_WARNING, "Unable to allocate Skinny subchannel\n");
02730 return NULL;
02731 } else {
02732 ast_mutex_init(&sub->lock);
02733
02734 sub->owner = tmp;
02735 sub->callid = callnums++;
02736 d->lastlineinstance = l->instance;
02737 d->lastcallreference = sub->callid;
02738 sub->cxmode = SKINNY_CX_INACTIVE;
02739 sub->nat = l->nat;
02740 sub->parent = l;
02741 sub->onhold = 0;
02742
02743 sub->next = l->sub;
02744 l->sub = sub;
02745 }
02746 tmp->tech = &skinny_tech;
02747 tmp->tech_pvt = sub;
02748 tmp->nativeformats = l->capability;
02749 if (!tmp->nativeformats)
02750 tmp->nativeformats = default_capability;
02751 fmt = ast_best_codec(tmp->nativeformats);
02752 if (skinnydebug)
02753 ast_verbose("skinny_new: tmp->nativeformats=%d fmt=%d\n", tmp->nativeformats, fmt);
02754 if (sub->rtp) {
02755 tmp->fds[0] = ast_rtp_fd(sub->rtp);
02756 }
02757 if (state == AST_STATE_RING) {
02758 tmp->rings = 1;
02759 }
02760 tmp->writeformat = fmt;
02761 tmp->rawwriteformat = fmt;
02762 tmp->readformat = fmt;
02763 tmp->rawreadformat = fmt;
02764 if (!ast_strlen_zero(l->language))
02765 ast_string_field_set(tmp, language, l->language);
02766 if (!ast_strlen_zero(l->accountcode))
02767 ast_string_field_set(tmp, accountcode, l->accountcode);
02768 if (l->amaflags)
02769 tmp->amaflags = l->amaflags;
02770
02771 ast_module_ref(ast_module_info->self);
02772 tmp->callgroup = l->callgroup;
02773 tmp->pickupgroup = l->pickupgroup;
02774 ast_string_field_set(tmp, call_forward, l->call_forward);
02775 ast_copy_string(tmp->context, l->context, sizeof(tmp->context));
02776 ast_copy_string(tmp->exten, l->exten, sizeof(tmp->exten));
02777
02778
02779
02780 tmp->cid.cid_num = ast_strdup(l->cid_num);
02781 tmp->cid.cid_ani = ast_strdup(l->cid_num);
02782 tmp->cid.cid_name = ast_strdup(l->cid_name);
02783
02784 tmp->priority = 1;
02785 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
02786
02787 if (sub->rtp)
02788 ast_jb_configure(tmp, &global_jbconf);
02789
02790 if (state != AST_STATE_DOWN) {
02791 if (ast_pbx_start(tmp)) {
02792 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
02793 ast_hangup(tmp);
02794 tmp = NULL;
02795 }
02796 }
02797 }
02798 return tmp;
02799 }
02800
02801 static int skinny_hold(struct skinny_subchannel *sub)
02802 {
02803 struct skinny_line *l = sub->parent;
02804 struct skinny_device *d = l->parent;
02805 struct skinnysession *s = d->session;
02806 struct skinny_req *req;
02807
02808
02809 if (skinnydebug)
02810 ast_verbose("Putting on Hold(%d)\n", l->instance);
02811
02812 ast_queue_control_data(sub->owner, AST_CONTROL_HOLD,
02813 S_OR(l->mohsuggest, NULL),
02814 !ast_strlen_zero(l->mohsuggest) ? strlen(l->mohsuggest) + 1 : 0);
02815
02816 if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
02817 return 0;
02818
02819 req->data.activatecallplane.lineInstance = htolel(l->instance);
02820 transmit_response(s, req);
02821
02822 if (!(req = req_alloc(sizeof(struct close_receive_channel_message), CLOSE_RECEIVE_CHANNEL_MESSAGE)))
02823 return 0;
02824
02825 req->data.closereceivechannel.conferenceId = htolel(0);
02826 req->data.closereceivechannel.partyId = htolel(sub->callid);
02827 transmit_response(s, req);
02828
02829 if (!(req = req_alloc(sizeof(struct stop_media_transmission_message), STOP_MEDIA_TRANSMISSION_MESSAGE)))
02830 return 0;
02831
02832 req->data.stopmedia.conferenceId = htolel(0);
02833 req->data.stopmedia.passThruPartyId = htolel(sub->callid);
02834 transmit_response(s, req);
02835
02836 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_BLINK);
02837 sub->onhold = 1;
02838 return 1;
02839 }
02840
02841 static int skinny_unhold(struct skinny_subchannel *sub)
02842 {
02843 struct skinny_line *l = sub->parent;
02844 struct skinny_device *d = l->parent;
02845 struct skinnysession *s = d->session;
02846 struct skinny_req *req;
02847
02848
02849 if (skinnydebug)
02850 ast_verbose("Taking off Hold(%d)\n", l->instance);
02851
02852 ast_queue_control(sub->owner, AST_CONTROL_UNHOLD);
02853
02854 if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
02855 return 0;
02856
02857 req->data.activatecallplane.lineInstance = htolel(l->instance);
02858 transmit_response(s, req);
02859
02860 transmit_connect(s, sub);
02861 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
02862 sub->onhold = 0;
02863 return 1;
02864 }
02865
02866 static int handle_keep_alive_message(struct skinny_req *req, struct skinnysession *s)
02867 {
02868 if (!(req = req_alloc(0, KEEP_ALIVE_ACK_MESSAGE)))
02869 return -1;
02870
02871 transmit_response(s, req);
02872 do_housekeeping(s);
02873 return 1;
02874 }
02875
02876 static int handle_register_message(struct skinny_req *req, struct skinnysession *s)
02877 {
02878 char name[16];
02879 int res;
02880
02881 memcpy(&name, req->data.reg.name, sizeof(name));
02882
02883 res = skinny_register(req, s);
02884 if (!res) {
02885 ast_log(LOG_ERROR, "Rejecting Device %s: Device not found\n", name);
02886 if (!(req = req_alloc(sizeof(struct register_rej_message), REGISTER_REJ_MESSAGE)))
02887 return -1;
02888
02889 snprintf(req->data.regrej.errMsg, sizeof(req->data.regrej.errMsg), "No Authority: %s", name);
02890 transmit_response(s, req);
02891 return 0;
02892 }
02893 if (option_verbose > 2)
02894 ast_verbose(VERBOSE_PREFIX_3 "Device '%s' successfully registered\n", name);
02895
02896 if (!(req = req_alloc(sizeof(struct register_ack_message), REGISTER_ACK_MESSAGE)))
02897 return -1;
02898
02899 req->data.regack.res[0] = '0';
02900 req->data.regack.res[1] = '\0';
02901 req->data.regack.keepAlive = htolel(keep_alive);
02902 ast_copy_string(req->data.regack.dateTemplate, date_format, sizeof(req->data.regack.dateTemplate));
02903 req->data.regack.res2[0] = '0';
02904 req->data.regack.res2[1] = '\0';
02905 req->data.regack.secondaryKeepAlive = htolel(keep_alive);
02906 transmit_response(s, req);
02907 if (skinnydebug)
02908 ast_verbose("Requesting capabilities\n");
02909
02910 if (!(req = req_alloc(0, CAPABILITIES_REQ_MESSAGE)))
02911 return -1;
02912
02913 transmit_response(s, req);
02914
02915 return res;
02916 }
02917
02918 static int handle_ip_port_message(struct skinny_req *req, struct skinnysession *s)
02919 {
02920
02921 return 1;
02922 }
02923
02924 static int handle_keypad_button_message(struct skinny_req *req, struct skinnysession *s)
02925 {
02926 struct skinny_subchannel *sub = NULL;
02927 struct skinny_line *l;
02928 struct skinny_device *d = s->device;
02929 struct ast_frame f = { 0, };
02930 char dgt;
02931 int digit;
02932 int lineInstance;
02933 int callReference;
02934
02935 digit = letohl(req->data.keypad.button);
02936 lineInstance = letohl(req->data.keypad.lineInstance);
02937 callReference = letohl(req->data.keypad.callReference);
02938
02939 if (digit == 14) {
02940 dgt = '*';
02941 } else if (digit == 15) {
02942 dgt = '#';
02943 } else if (digit >= 0 && digit <= 9) {
02944 dgt = '0' + digit;
02945 } else {
02946
02947
02948
02949
02950
02951
02952
02953 dgt = '0' + digit;
02954 ast_log(LOG_WARNING, "Unsupported digit %d\n", digit);
02955 }
02956
02957 f.subclass = dgt;
02958
02959 f.src = "skinny";
02960
02961 if (lineInstance && callReference)
02962 sub = find_subchannel_by_instance_reference(d, lineInstance, callReference);
02963 else
02964 sub = find_subchannel_by_instance_reference(d, d->lastlineinstance, d->lastcallreference);
02965
02966 if (!sub)
02967 return 0;
02968
02969 l = sub->parent;
02970 if (sub->owner) {
02971 if (sub->owner->_state == 0) {
02972 f.frametype = AST_FRAME_DTMF_BEGIN;
02973 ast_queue_frame(sub->owner, &f);
02974 }
02975
02976 f.frametype = AST_FRAME_DTMF_END;
02977 ast_queue_frame(sub->owner, &f);
02978
02979 if (sub->next && sub->next->owner) {
02980 if (sub->owner->_state == 0) {
02981 f.frametype = AST_FRAME_DTMF_BEGIN;
02982 ast_queue_frame(sub->next->owner, &f);
02983 }
02984 f.frametype = AST_FRAME_DTMF_END;
02985 ast_queue_frame(sub->next->owner, &f);
02986 }
02987 } else {
02988 if (skinnydebug)
02989 ast_verbose("No owner: %s\n", l->name);
02990 }
02991 return 1;
02992 }
02993
02994 static int handle_stimulus_message(struct skinny_req *req, struct skinnysession *s)
02995 {
02996 struct skinny_device *d = s->device;
02997 struct skinny_line *l;
02998 struct skinny_subchannel *sub;
02999
03000 struct ast_channel *c;
03001 pthread_t t;
03002 int event;
03003 int instance;
03004 int unknown1;
03005
03006
03007 event = letohl(req->data.stimulus.stimulus);
03008 instance = letohl(req->data.stimulus.stimulusInstance);
03009 unknown1 = letohl(req->data.stimulus.unknown1);
03010 if (skinnydebug)
03011 ast_verbose("unknown1 in handle_stimulus_message is '%d'\n", unknown1);
03012
03013 sub = find_subchannel_by_instance_reference(d, d->lastlineinstance, d->lastcallreference);
03014
03015 if (!sub) {
03016 l = find_line_by_instance(d, d->lastlineinstance);
03017 if (!l) {
03018 return 0;
03019 }
03020 } else {
03021 l = sub->parent;
03022 }
03023
03024 switch(event) {
03025 case STIMULUS_REDIAL:
03026 if (skinnydebug)
03027 ast_verbose("Received Stimulus: Redial(%d)\n", instance);
03028
03029 #if 0
03030 c = skinny_new(l, AST_STATE_DOWN);
03031 if(!c) {
03032 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
03033 } else {
03034 sub = c->tech_pvt;
03035 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
03036 if (skinnydebug)
03037 ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
03038 transmit_displaymessage(s, NULL);
03039 transmit_tone(s, SKINNY_DIALTONE);
03040
03041 if (ast_strlen_zero(l->lastnumberdialed)) {
03042 ast_log(LOG_WARNING, "Attempted redial, but no previously dialed number found.\n");
03043 return 0;
03044 }
03045 if (!ast_ignore_pattern(c->context, l->lastnumberdialed)) {
03046 transmit_tone(s, SKINNY_SILENCE);
03047 }
03048 ast_copy_string(c->exten, l->lastnumberdialed, sizeof(c->exten));
03049 if (ast_pthread_create(&t, NULL, skinny_newcall, c)) {
03050 ast_log(LOG_WARNING, "Unable to create new call thread: %s\n", strerror(errno));
03051 ast_hangup(c);
03052 }
03053 }
03054 #endif
03055 break;
03056 case STIMULUS_SPEEDDIAL:
03057 if (skinnydebug)
03058 ast_verbose("Received Stimulus: SpeedDial(%d)\n", instance);
03059
03060 #if 0
03061 if (!(sd = find_speeddial_by_instance(d, instance))) {
03062 return 0;
03063 }
03064
03065 c = skinny_new(l, AST_STATE_DOWN);
03066 if(c) {
03067 sub = c->tech_pvt;
03068 l = sub->parent;
03069 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
03070 if (skinnydebug)
03071 ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
03072 transmit_displaymessage(s, NULL);
03073 transmit_tone(s, SKINNY_DIALTONE);
03074
03075 if (!ast_ignore_pattern(c->context, sd->exten)) {
03076 transmit_tone(s, SKINNY_SILENCE);
03077 }
03078 if (ast_exists_extension(c, c->context, sd->exten, 1, l->cid_num)) {
03079 if (!ast_matchmore_extension(c, c->context, sd->exten, 1, l->cid_num)) {
03080 ast_copy_string(c->exten, sd->exten, sizeof(c->exten));
03081 ast_copy_string(l->lastnumberdialed, sd->exten, sizeof(l->lastnumberdialed));
03082 skinny_newcall(c);
03083 break;
03084 }
03085 }
03086 } else {
03087 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
03088 }
03089 #endif
03090 break;
03091 case STIMULUS_HOLD:
03092 if (skinnydebug)
03093 ast_verbose("Received Stimulus: Hold(%d)\n", instance);
03094
03095 if (!sub)
03096 break;
03097
03098 if (sub->onhold) {
03099 skinny_unhold(sub);
03100 } else {
03101 skinny_hold(sub);
03102 }
03103 break;
03104 case STIMULUS_TRANSFER:
03105 if (skinnydebug)
03106 ast_verbose("Received Stimulus: Transfer(%d)\n", instance);
03107 transmit_tone(s, SKINNY_DIALTONE);
03108
03109 break;
03110 case STIMULUS_CONFERENCE:
03111 if (skinnydebug)
03112 ast_verbose("Received Stimulus: Conference(%d)\n", instance);
03113 transmit_tone(s, SKINNY_DIALTONE);
03114
03115 break;
03116 case STIMULUS_VOICEMAIL:
03117 if (skinnydebug)
03118 ast_verbose("Received Stimulus: Voicemail(%d)\n", instance);
03119
03120 break;
03121 case STIMULUS_CALLPARK:
03122 if (skinnydebug)
03123 ast_verbose("Received Stimulus: Park Call(%d)\n", instance);
03124
03125 break;
03126 case STIMULUS_FORWARDALL:
03127 if (skinnydebug)
03128 ast_verbose("Received Stimulus: Forward All(%d)\n", instance);
03129
03130
03131
03132
03133 transmit_tone(s, SKINNY_DIALTONE);
03134 if (l->dnd != 0){
03135 if (option_verbose > 2)
03136 ast_verbose(VERBOSE_PREFIX_3 "Disabling DND on %s@%s\n", l->name, d->name);
03137 l->dnd = 0;
03138 transmit_lamp_indication(s, STIMULUS_FORWARDALL, 1, SKINNY_LAMP_ON);
03139 transmit_displaynotify(s, "DnD disabled", 10);
03140 } else {
03141 if (option_verbose > 2)
03142 ast_verbose(VERBOSE_PREFIX_3 "Enabling DND on %s@%s\n", l->name, d->name);
03143 l->dnd = 1;
03144 transmit_lamp_indication(s, STIMULUS_FORWARDALL, 1, SKINNY_LAMP_OFF);
03145 transmit_displaynotify(s, "DnD enabled", 10);
03146 }
03147 break;
03148 case STIMULUS_FORWARDBUSY:
03149 if (skinnydebug)
03150 ast_verbose("Received Stimulus: Forward Busy (%d)\n", instance);
03151 break;
03152 case STIMULUS_FORWARDNOANSWER:
03153 if (skinnydebug)
03154 ast_verbose("Received Stimulus: Forward No Answer (%d)\n", instance);
03155 break;
03156 case STIMULUS_DISPLAY:
03157
03158 if (skinnydebug)
03159 ast_verbose("Received Stimulus: Display(%d)\n", instance);
03160 break;
03161 case STIMULUS_LINE:
03162 if (skinnydebug)
03163 ast_verbose("Received Stimulus: Line(%d)\n", instance);
03164
03165 l = find_line_by_instance(s->device, instance);
03166
03167 if (!l) {
03168 return 0;
03169 }
03170
03171
03172 transmit_speaker_mode(s, SKINNY_SPEAKERON);
03173 transmit_ringer_mode(s, SKINNY_RING_OFF);
03174 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
03175
03176 l->hookstate = SKINNY_OFFHOOK;
03177
03178 if (sub && sub->outgoing) {
03179
03180 ast_queue_control(sub->owner, AST_CONTROL_ANSWER);
03181 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
03182 transmit_tone(s, SKINNY_SILENCE);
03183 transmit_callstate(s, l->instance, SKINNY_CONNECTED, sub->callid);
03184 transmit_displaypromptstatus(s, "Connected", 0, l->instance, sub->callid);
03185 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_CONNECTED);
03186 start_rtp(sub);
03187 ast_setstate(sub->owner, AST_STATE_UP);
03188 } else {
03189 if (sub && sub->owner) {
03190 ast_log(LOG_DEBUG, "Current subchannel [%s] already has owner\n", sub->owner->name);
03191 } else {
03192 c = skinny_new(l, AST_STATE_DOWN);
03193 if(c) {
03194 sub = c->tech_pvt;
03195 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
03196 if (skinnydebug)
03197 ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
03198 transmit_displaymessage(s, NULL);
03199 transmit_tone(s, SKINNY_DIALTONE);
03200 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_OFFHOOK);
03201
03202
03203 if (ast_pthread_create(&t, NULL, skinny_ss, c)) {
03204 ast_log(LOG_WARNING, "Unable to create switch thread: %s\n", strerror(errno));
03205 ast_hangup(c);
03206 }
03207 } else {
03208 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
03209 }
03210 }
03211 }
03212 break;
03213 default:
03214 if (skinnydebug)
03215 ast_verbose("RECEIVED UNKNOWN STIMULUS: %d(%d)\n", event, instance);
03216 break;
03217 }
03218 return 1;
03219 }
03220
03221 static int handle_offhook_message(struct skinny_req *req, struct skinnysession *s)
03222 {
03223 struct skinny_device *d = s->device;
03224 struct skinny_line *l;
03225 struct skinny_subchannel *sub;
03226 struct ast_channel *c;
03227 pthread_t t;
03228 int unknown1;
03229 int unknown2;
03230
03231 unknown1 = letohl(req->data.offhook.unknown1);
03232 unknown2 = letohl(req->data.offhook.unknown2);
03233
03234 sub = find_subchannel_by_instance_reference(d, d->lastlineinstance, d->lastcallreference);
03235
03236 if (!sub) {
03237 l = find_line_by_instance(d, d->lastlineinstance);
03238 if (!l) {
03239 return 0;
03240 }
03241 } else {
03242 l = sub->parent;
03243 }
03244
03245 if (sub && sub->onhold) {
03246 transmit_ringer_mode(s, SKINNY_RING_OFF);
03247 l->hookstate = SKINNY_OFFHOOK;
03248 return 1;
03249 }
03250
03251 transmit_ringer_mode(s, SKINNY_RING_OFF);
03252 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
03253 l->hookstate = SKINNY_OFFHOOK;
03254
03255 if (sub && sub->outgoing) {
03256
03257 ast_queue_control(sub->owner, AST_CONTROL_ANSWER);
03258 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
03259 transmit_tone(s, SKINNY_SILENCE);
03260 transmit_callstate(s, l->instance, SKINNY_CONNECTED, sub->callid);
03261 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_CONNECTED);
03262 start_rtp(sub);
03263 ast_setstate(sub->owner, AST_STATE_UP);
03264 } else {
03265 if (sub && sub->owner) {
03266 ast_log(LOG_DEBUG, "Current sub [%s] already has owner\n", sub->owner->name);
03267 } else {
03268 c = skinny_new(l, AST_STATE_DOWN);
03269 if(c) {
03270 sub = c->tech_pvt;
03271 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
03272 if (skinnydebug)
03273 ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
03274 transmit_displaymessage(s, NULL);
03275 transmit_tone(s, SKINNY_DIALTONE);
03276 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_OFFHOOK);
03277
03278
03279 if (ast_pthread_create(&t, NULL, skinny_ss, c)) {
03280 ast_log(LOG_WARNING, "Unable to create switch thread: %s\n", strerror(errno));
03281 ast_hangup(c);
03282 }
03283 } else {
03284 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
03285 }
03286 }
03287 }
03288 return 1;
03289 }
03290
03291 static int handle_onhook_message(struct skinny_req *req, struct skinnysession *s)
03292 {
03293 struct skinny_device *d = s->device;
03294 struct skinny_line *l;
03295 struct skinny_subchannel *sub;
03296 int unknown1;
03297 int unknown2;
03298
03299 unknown1 = letohl(req->data.onhook.unknown1);
03300 unknown2 = letohl(req->data.onhook.unknown2);
03301
03302 sub = find_subchannel_by_instance_reference(d, d->lastlineinstance, d->lastcallreference);
03303
03304 if (!sub) {
03305 return 0;
03306 }
03307 l = sub->parent;
03308
03309 if (sub->onhold) {
03310 l->hookstate = SKINNY_ONHOOK;
03311 return 0;
03312 }
03313
03314 if (l->hookstate == SKINNY_ONHOOK) {
03315
03316 return 0;
03317 }
03318 sub->cxmode = SKINNY_CX_RECVONLY;
03319 l->hookstate = SKINNY_ONHOOK;
03320 transmit_callstate(s, l->instance, l->hookstate, sub->callid);
03321 if (skinnydebug)
03322 ast_verbose("Skinny %s@%s went on hook\n", l->name, d->name);
03323 if (l->transfer && (sub->owner && sub->next && sub->next->owner) && ((!sub->outgoing) || (sub->next && !sub->next->outgoing))) {
03324
03325
03326
03327 #if 0
03328 if ((res = attempt_transfer(p)) < 0) {
03329 if (sub->next && sub->next->owner) {
03330 sub->next->alreadygone = 1;
03331 ast_queue_hangup(sub->next->owner,1);
03332 }
03333 } else if (res) {
03334 ast_log(LOG_WARNING, "Transfer attempt failed\n");
03335 return 0;
03336 }
03337 #endif
03338 } else {
03339
03340
03341 if (sub->owner) {
03342 sub->alreadygone = 1;
03343 ast_queue_hangup(sub->owner);
03344 } else {
03345 ast_log(LOG_WARNING, "Skinny(%s@%s-%d) channel already destroyed\n",
03346 l->name, d->name, sub->callid);
03347 }
03348 }
03349 if ((l->hookstate == SKINNY_ONHOOK) && (sub->next && !sub->next->rtp)) {
03350 do_housekeeping(s);
03351 }
03352 return 1;
03353 }
03354
03355 static int handle_capabilities_res_message(struct skinny_req *req, struct skinnysession *s)
03356 {
03357 struct skinny_device *d = s->device;
03358 struct skinny_line *l;
03359 int count = 0;
03360 int codecs = 0;
03361 int i;
03362
03363 count = letohl(req->data.caps.count);
03364
03365 for (i = 0; i < count; i++) {
03366 int acodec = 0;
03367 int scodec = 0;
03368 scodec = letohl(req->data.caps.caps[i].codec);
03369 acodec = codec_skinny2ast(scodec);
03370 if (skinnydebug)
03371 ast_verbose("Adding codec capability '%d (%d)'\n", acodec, scodec);
03372 codecs |= acodec;
03373 }
03374
03375 d->capability &= codecs;
03376 ast_verbose("Device capability set to '%d'\n", d->capability);
03377 for (l = d->lines; l; l = l->next) {
03378 ast_mutex_lock(&l->lock);
03379 l->capability = d->capability;
03380 ast_mutex_unlock(&l->lock);
03381 }
03382
03383 return 1;
03384 }
03385
03386 static int handle_speed_dial_stat_req_message(struct skinny_req *req, struct skinnysession *s)
03387 {
03388 struct skinny_device *d = s->device;
03389 struct skinny_speeddial *sd;
03390 int instance;
03391
03392 instance = letohl(req->data.speeddialreq.speedDialNumber);
03393
03394 sd = find_speeddial_by_instance(d, instance);
03395
03396 if (!sd) {
03397 return 0;
03398 }
03399
03400 if (!(req = req_alloc(sizeof(struct speed_dial_stat_res_message), SPEED_DIAL_STAT_RES_MESSAGE)))
03401 return -1;
03402
03403 req->data.speeddialreq.speedDialNumber = htolel(instance);
03404 snprintf(req->data.speeddial.speedDialDirNumber, sizeof(req->data.speeddial.speedDialDirNumber), sd->exten);
03405 snprintf(req->data.speeddial.speedDialDisplayName, sizeof(req->data.speeddial.speedDialDisplayName), sd->label);
03406
03407 transmit_response(s, req);
03408 return 1;
03409 }
03410
03411 static int handle_line_state_req_message(struct skinny_req *req, struct skinnysession *s)
03412 {
03413 struct skinny_device *d = s->device;
03414 struct skinny_line *l;
03415 int instance;
03416
03417 instance = letohl(req->data.line.lineNumber);
03418
03419 ast_mutex_lock(&devicelock);
03420
03421 l = find_line_by_instance(d, instance);
03422
03423 if (!l) {
03424 return 0;
03425 }
03426
03427 ast_mutex_unlock(&devicelock);
03428
03429 if (!(req = req_alloc(sizeof(struct line_stat_res_message), LINE_STAT_RES_MESSAGE)))
03430 return -1;
03431
03432 req->data.linestat.lineNumber = letohl(instance);
03433 memcpy(req->data.linestat.lineDirNumber, l->name,
03434 sizeof(req->data.linestat.lineDirNumber));
03435 memcpy(req->data.linestat.lineDisplayName, l->label,
03436 sizeof(req->data.linestat.lineDisplayName));
03437 transmit_response(s,req);
03438 return 1;
03439 }
03440
03441 static int handle_time_date_req_message(struct skinny_req *req, struct skinnysession *s)
03442 {
03443 time_t timer;
03444 struct tm *cmtime;
03445
03446 if (!(req = req_alloc(sizeof(struct definetimedate_message), DEFINETIMEDATE_MESSAGE)))
03447 return -1;
03448
03449 timer = time(NULL);
03450 cmtime = localtime(&timer);
03451 req->data.definetimedate.year = htolel(cmtime->tm_year+1900);
03452 req->data.definetimedate.month = htolel(cmtime->tm_mon+1);
03453 req->data.definetimedate.dayofweek = htolel(cmtime->tm_wday);
03454 req->data.definetimedate.day = htolel(cmtime->tm_mday);
03455 req->data.definetimedate.hour = htolel(cmtime->tm_hour);
03456 req->data.definetimedate.minute = htolel(cmtime->tm_min);
03457 req->data.definetimedate.seconds = htolel(cmtime->tm_sec);
03458 transmit_response(s, req);
03459 return 1;
03460 }
03461
03462 static int handle_button_template_req_message(struct skinny_req *req, struct skinnysession *s)
03463 {
03464 struct skinny_device *d = s->device;
03465 struct skinny_line *l;
03466 int i;
03467
03468 struct skinny_speeddial *sd;
03469 struct button_definition_template btn[42];
03470 int lineInstance = 1;
03471 int speeddialInstance = 1;
03472 int buttonCount = 0;
03473
03474 if (!(req = req_alloc(sizeof(struct button_template_res_message), BUTTON_TEMPLATE_RES_MESSAGE)))
03475 return -1;
03476
03477 memset(&btn, 0, sizeof(btn));
03478
03479 get_button_template(s, btn);
03480
03481 for (i=0; i<42; i++) {
03482 int btnSet = 0;
03483 switch (btn[i].buttonDefinition) {
03484 case BT_CUST_LINESPEEDDIAL:
03485
03486 req->data.buttontemplate.definition[i].buttonDefinition = BT_NONE;
03487 req->data.buttontemplate.definition[i].instanceNumber = htolel(0);
03488
03489 for (l = d->lines; l; l = l->next) {
03490 if (l->instance == lineInstance) {
03491 ast_verbose("Adding button: %d, %d\n", BT_LINE, lineInstance);
03492 req->data.buttontemplate.definition[i].buttonDefinition = BT_LINE;
03493 req->data.buttontemplate.definition[i].instanceNumber = htolel(lineInstance);
03494 lineInstance++;
03495 buttonCount++;
03496 btnSet = 1;
03497 break;
03498 }
03499 }
03500
03501 if (!btnSet) {
03502 for (sd = d->speeddials; sd; sd = sd->next) {
03503 if (sd->instance == speeddialInstance) {
03504 ast_verbose("Adding button: %d, %d\n", BT_SPEEDDIAL, speeddialInstance);
03505 req->data.buttontemplate.definition[i].buttonDefinition = BT_SPEEDDIAL;
03506 req->data.buttontemplate.definition[i].instanceNumber = htolel(speeddialInstance);
03507 speeddialInstance++;
03508 buttonCount++;
03509 btnSet = 1;
03510 break;
03511 }
03512 }
03513 }
03514 break;
03515 case BT_LINE:
03516 req->data.buttontemplate.definition[i].buttonDefinition = htolel(BT_NONE);
03517 req->data.buttontemplate.definition[i].instanceNumber = htolel(0);
03518
03519 for (l = d->lines; l; l = l->next) {
03520 if (l->instance == lineInstance) {
03521 ast_verbose("Adding button: %d, %d\n", BT_LINE, lineInstance);
03522 req->data.buttontemplate.definition[i].buttonDefinition = BT_LINE;
03523 req->data.buttontemplate.definition[i].instanceNumber = htolel(lineInstance);
03524 lineInstance++;
03525 buttonCount++;
03526 btnSet = 1;
03527 break;
03528 }
03529 }
03530 break;
03531 case BT_SPEEDDIAL:
03532 req->data.buttontemplate.definition[i].buttonDefinition = BT_NONE;
03533 req->data.buttontemplate.definition[i].instanceNumber = 0;
03534
03535 for (sd = d->speeddials; sd; sd = sd->next) {
03536 if (sd->instance == speeddialInstance) {
03537 ast_verbose("Adding button: %d, %d\n", BT_SPEEDDIAL, speeddialInstance);
03538 req->data.buttontemplate.definition[i].buttonDefinition = BT_SPEEDDIAL;
03539 req->data.buttontemplate.definition[i].instanceNumber = htolel(speeddialInstance);
03540 speeddialInstance++;
03541 buttonCount++;
03542 btnSet = 1;
03543 break;
03544 }
03545 }
03546 break;
03547 case BT_CUST_HINT:
03548 break;
03549 case BT_NONE:
03550 break;
03551 default:
03552 ast_verbose("Adding button: %d, %d\n", btn[i].buttonDefinition, 0);
03553 req->data.buttontemplate.definition[i].buttonDefinition = htolel(btn[i].buttonDefinition);
03554 req->data.buttontemplate.definition[i].instanceNumber = htolel(0);
03555 buttonCount++;
03556 btnSet = 1;
03557 break;
03558 }
03559 }
03560
03561 req->data.buttontemplate.buttonOffset = htolel(0);
03562 req->data.buttontemplate.buttonCount = htolel(buttonCount);
03563 req->data.buttontemplate.totalButtonCount = htolel(buttonCount);
03564
03565 if (skinnydebug)
03566 ast_verbose("Sending %d template to %s\n",
03567 d->type,
03568 d->name);
03569 transmit_response(s, req);
03570 return 1;
03571 }
03572
03573 static int handle_version_req_message(struct skinny_req *req, struct skinnysession *s)
03574 {
03575 struct skinny_device *d = s->device;
03576 if (!(req = req_alloc(sizeof(struct version_res_message), VERSION_RES_MESSAGE)))
03577 return -1;
03578
03579 snprintf(req->data.version.version, sizeof(req->data.version.version), d->version_id);
03580 transmit_response(s, req);
03581 return 1;
03582 }
03583
03584 static int handle_server_request_message(struct skinny_req *req, struct skinnysession *s)
03585 {
03586 struct skinny_device *d = s->device;
03587 if (!(req = req_alloc(sizeof(struct server_res_message), SERVER_RES_MESSAGE)))
03588 return -1;
03589
03590 memcpy(req->data.serverres.server[0].serverName, ourhost,
03591 sizeof(req->data.serverres.server[0].serverName));
03592 req->data.serverres.serverListenPort[0] = htolel(ourport);
03593 req->data.serverres.serverIpAddr[0] = htolel(d->ourip.s_addr);
03594 transmit_response(s, req);
03595 return 1;
03596 }
03597
03598 static int handle_alarm_message(struct skinny_req *req, struct skinnysession *s)
03599 {
03600
03601 if (skinnydebug)
03602 ast_verbose("Received Alarm Message: %s\n", req->data.alarm.displayMessage);
03603
03604 return 1;
03605 }
03606
03607 static int handle_open_receive_channel_ack_message(struct skinny_req *req, struct skinnysession *s)
03608 {
03609 struct skinny_device *d = s->device;
03610 struct skinny_line *l;
03611 struct skinny_subchannel *sub;
03612 struct ast_format_list fmt;
03613 struct sockaddr_in sin;
03614 struct sockaddr_in us;
03615 uint32_t addr;
03616 int port;
03617 int status;
03618 int passthruid;
03619
03620 status = letohl(req->data.openreceivechannelack.status);
03621 if (status) {
03622 ast_log(LOG_ERROR, "Open Receive Channel Failure\n");
03623 return 0;
03624 }
03625 addr = letohl(req->data.openreceivechannelack.ipAddr);
03626 port = letohl(req->data.openreceivechannelack.port);
03627 passthruid = letohl(req->data.openreceivechannelack.passThruId);
03628
03629 sin.sin_family = AF_INET;
03630 sin.sin_addr.s_addr = addr;
03631 sin.sin_port = htons(port);
03632
03633 sub = find_subchannel_by_reference(d, passthruid);
03634
03635 if (!sub)
03636 return 0;
03637
03638 l = sub->parent;
03639
03640 if (sub->rtp) {
03641 ast_rtp_set_peer(sub->rtp, &sin);
03642 ast_rtp_get_us(sub->rtp, &us);
03643 } else {
03644 ast_log(LOG_ERROR, "No RTP structure, this is very bad\n");
03645 return 0;
03646 }
03647
03648 if (skinnydebug) {
03649 ast_verbose("ipaddr = %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
03650 ast_verbose("ourip = %s:%d\n", ast_inet_ntoa(d->ourip), ntohs(us.sin_port));
03651 }
03652
03653 if (!(req = req_alloc(sizeof(struct start_media_transmission_message), START_MEDIA_TRANSMISSION_MESSAGE)))
03654 return -1;
03655
03656 fmt = ast_codec_pref_getsize(&l->prefs, ast_best_codec(l->capability));
03657
03658 if (skinnydebug)
03659 ast_verbose("Setting payloadType to '%d' (%d ms)\n", fmt.bits, fmt.cur_ms);
03660
03661 req->data.startmedia.conferenceId = htolel(0);
03662 req->data.startmedia.passThruPartyId = htolel(sub->callid);
03663 req->data.startmedia.remoteIp = htolel(d->ourip.s_addr);
03664 req->data.startmedia.remotePort = htolel(ntohs(us.sin_port));
03665 req->data.startmedia.packetSize = htolel(fmt.cur_ms);
03666 req->data.startmedia.payloadType = htolel(codec_ast2skinny(fmt.bits));
03667 req->data.startmedia.qualifier.precedence = htolel(127);
03668 req->data.startmedia.qualifier.vad = htolel(0);
03669 req->data.startmedia.qualifier.packets = htolel(0);
03670 req->data.startmedia.qualifier.bitRate = htolel(0);
03671 transmit_response(s, req);
03672
03673 return 1;
03674 }
03675
03676 static int handle_soft_key_set_req_message(struct skinny_req *req, struct skinnysession *s)
03677 {
03678 int i;
03679 int x;
03680 int y;
03681 const struct soft_key_definitions *softkeymode = soft_key_default_definitions;
03682
03683 if (!(req = req_alloc(sizeof(struct soft_key_set_res_message), SOFT_KEY_SET_RES_MESSAGE)))
03684 return -1;
03685
03686 req->data.softkeysets.softKeySetOffset = htolel(0);
03687 req->data.softkeysets.softKeySetCount = htolel(11);
03688 req->data.softkeysets.totalSoftKeySetCount = htolel(11);
03689 for (x = 0; x < sizeof(soft_key_default_definitions) / sizeof(struct soft_key_definitions); x++) {
03690 const uint8_t *defaults = softkeymode->defaults;
03691
03692
03693 for (y = 0; y < softkeymode->count; y++) {
03694 for (i = 0; i < (sizeof(soft_key_template_default) / sizeof(struct soft_key_template_definition)); i++) {
03695 if (defaults[y] == i+1) {
03696 req->data.softkeysets.softKeySetDefinition[softkeymode->mode].softKeyTemplateIndex[y] = htolel(i+1);
03697 }
03698 }
03699 }
03700 softkeymode++;
03701 }
03702 transmit_response(s,req);
03703 transmit_selectsoftkeys(s, 0, 0, KEYDEF_ONHOOK);
03704 return 1;
03705 }
03706
03707 static int handle_soft_key_event_message(struct skinny_req *req, struct skinnysession *s)
03708 {
03709 struct skinny_device *d = s->device;
03710 struct skinny_line *l;
03711 struct skinny_subchannel *sub = NULL;
03712 struct ast_channel *c;
03713 pthread_t t;
03714 int event;
03715 int instance;
03716 int reference;
03717
03718 event = letohl(req->data.softkeyeventmessage.softKeyEvent);
03719 instance = letohl(req->data.softkeyeventmessage.instance);
03720 reference = letohl(req->data.softkeyeventmessage.reference);
03721
03722 if (instance) {
03723 l = find_line_by_instance(d, instance);
03724 if (reference) {
03725 sub = find_subchannel_by_instance_reference(d, instance, reference);
03726 } else {
03727 sub = find_subchannel_by_instance_reference(d, instance, d->lastcallreference);
03728 }
03729 } else {
03730 l = find_line_by_instance(d, d->lastlineinstance);
03731 }
03732
03733 if (!l) {
03734 if (skinnydebug)
03735 ast_verbose("Received Softkey Event: %d(%d)\n", event, instance);
03736 return 0;
03737 }
03738
03739 switch(event) {
03740 case SOFTKEY_NONE:
03741 if (skinnydebug)
03742 ast_verbose("Received Softkey Event: None(%d)\n", instance);
03743 break;
03744 case SOFTKEY_REDIAL:
03745 if (skinnydebug)
03746 ast_verbose("Received Softkey Event: Redial(%d)\n", instance);
03747
03748 #if 0
03749 if (!sub || !sub->owner) {
03750 c = skinny_new(l, AST_STATE_DOWN);
03751 } else {
03752 c = sub->owner;
03753 }
03754
03755 if(!c) {
03756 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
03757 } else {
03758 sub = c->tech_pvt;
03759 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
03760 if (skinnydebug)
03761 ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
03762 transmit_displaymessage(s, NULL);
03763 transmit_tone(s, SKINNY_DIALTONE);
03764
03765 if (ast_strlen_zero(l->lastnumberdialed)) {
03766 ast_log(LOG_WARNING, "Attempted redial, but no previously dialed number found.\n");
03767 break;
03768 }
03769 if (!ast_ignore_pattern(c->context, l->lastnumberdialed)) {
03770 transmit_tone(s, SKINNY_SILENCE);
03771 }
03772 ast_copy_string(c->exten, l->lastnumberdialed, sizeof(c->exten));
03773 if (ast_pthread_create(&t, NULL, skinny_newcall, c)) {
03774 ast_log(LOG_WARNING, "Unable to create new call thread: %s\n", strerror(errno));
03775 ast_hangup(c);
03776 }
03777 }
03778 #endif
03779 break;
03780 case SOFTKEY_NEWCALL:
03781
03782 if (skinnydebug)
03783 ast_verbose("Received Softkey Event: New Call(%d)\n", instance);
03784
03785 transmit_ringer_mode(s,SKINNY_RING_OFF);
03786 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
03787
03788 l->hookstate = SKINNY_OFFHOOK;
03789
03790 if (sub) {
03791 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
03792 if (skinnydebug)
03793 ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
03794 transmit_displaymessage(s, NULL);
03795 transmit_tone(s, SKINNY_DIALTONE);
03796 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_OFFHOOK);
03797 c = skinny_new(l, AST_STATE_DOWN);
03798 if(c) {
03799
03800 if (ast_pthread_create(&t, NULL, skinny_ss, c)) {
03801 ast_log(LOG_WARNING, "Unable to create switch thread: %s\n", strerror(errno));
03802 ast_hangup(c);
03803 }
03804 } else {
03805 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
03806 }
03807 }
03808 break;
03809 case SOFTKEY_HOLD:
03810 if (skinnydebug)
03811 ast_verbose("Received Softkey Event: Hold(%d)\n", instance);
03812
03813 if (sub) {
03814 if (sub->onhold) {
03815 skinny_unhold(sub);
03816 } else {
03817 skinny_hold(sub);
03818 }
03819 }
03820
03821 break;
03822 case SOFTKEY_TRNSFER:
03823 if (skinnydebug)
03824 ast_verbose("Received Softkey Event: Transfer(%d)\n", instance);
03825 transmit_tone(s, SKINNY_DIALTONE);
03826
03827 break;
03828 case SOFTKEY_CFWDALL:
03829 if (skinnydebug)
03830 ast_verbose("Received Softkey Event: Forward All(%d)\n", instance);
03831
03832
03833 transmit_tone(s, SKINNY_DIALTONE);
03834 if (l->dnd != 0){
03835 if (option_verbose > 2)
03836 ast_verbose(VERBOSE_PREFIX_3 "Disabling DND on %s@%s\n", l->name, d->name);
03837 l->dnd = 0;
03838 transmit_lamp_indication(s, STIMULUS_FORWARDALL, 1, SKINNY_LAMP_ON);
03839 transmit_displaynotify(s, "DnD disabled", 10);
03840 } else {
03841 if (option_verbose > 2)
03842 ast_verbose(VERBOSE_PREFIX_3 "Enabling DND on %s@%s\n", l->name, d->name);
03843 l->dnd = 1;
03844 transmit_lamp_indication(s, STIMULUS_FORWARDALL, 1, SKINNY_LAMP_OFF);
03845 transmit_displaynotify(s, "DnD enabled", 10);
03846 }
03847 break;
03848 case SOFTKEY_CFWDBUSY:
03849 if (skinnydebug)
03850 ast_verbose("Received Softkey Event: Forward Busy (%d)\n", instance);
03851 break;
03852 case SOFTKEY_CFWDNOANSWER:
03853 if (skinnydebug)
03854 ast_verbose("Received Softkey Event: Forward No Answer (%d)\n", instance);
03855 break;
03856 case SOFTKEY_BKSPC:
03857 if (skinnydebug)
03858 ast_verbose("Received Softkey Event: Backspace(%d)\n", instance);
03859 break;
03860 case SOFTKEY_ENDCALL:
03861 if (skinnydebug)
03862 ast_verbose("Received Softkey Event: End Call(%d)\n", instance);
03863
03864 if (l->hookstate == SKINNY_ONHOOK) {
03865
03866 break;
03867 }
03868 if (sub) {
03869 sub->cxmode = SKINNY_CX_RECVONLY;
03870 l->hookstate = SKINNY_ONHOOK;
03871 transmit_callstate(s, l->instance, l->hookstate, sub->callid);
03872 if (skinnydebug)
03873 ast_verbose("Skinny %s@%s went on hook\n", l->name, d->name);
03874 if (l->transfer && (sub->owner && sub->next && sub->next->owner) && ((!sub->outgoing) || (sub->next && !sub->next->outgoing))) {
03875
03876
03877
03878 #if 0
03879 if ((res = attempt_transfer(p)) < 0) {
03880 if (sub->next && sub->next->owner) {
03881 sub->next->alreadygone = 1;
03882 ast_queue_hangup(sub->next->owner, 1);
03883 }
03884 } else if (res) {
03885 ast_log(LOG_WARNING, "Transfer attempt failed\n");
03886 break;
03887 }
03888 #endif
03889 } else {
03890
03891
03892 if (sub->owner) {
03893 sub->alreadygone = 1;
03894 ast_queue_hangup(sub->owner);
03895 } else {
03896 ast_log(LOG_WARNING, "Skinny(%s@%s-%d) channel already destroyed\n",
03897 l->name, d->name, sub->callid);
03898 }
03899 }
03900 if ((l->hookstate == SKINNY_ONHOOK) && (sub->next && !sub->next->rtp)) {
03901 do_housekeeping(s);
03902 }
03903 }
03904 break;
03905 case SOFTKEY_RESUME:
03906 if (skinnydebug)
03907 ast_verbose("Received Softkey Event: Resume(%d)\n", instance);
03908 break;
03909 case SOFTKEY_ANSWER:
03910 if (skinnydebug)
03911 ast_verbose("Received Softkey Event: Answer(%d)\n", instance);
03912
03913 transmit_ringer_mode(s,SKINNY_RING_OFF);
03914 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
03915
03916 l->hookstate = SKINNY_OFFHOOK;
03917
03918 if (sub && sub->outgoing) {
03919
03920 ast_queue_control(sub->owner, AST_CONTROL_ANSWER);
03921 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
03922 transmit_tone(s, SKINNY_SILENCE);
03923 transmit_callstate(s, l->instance, SKINNY_CONNECTED, sub->callid);
03924 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_CONNECTED);
03925 start_rtp(sub);
03926 ast_setstate(sub->owner, AST_STATE_UP);
03927 }
03928 break;
03929 case SOFTKEY_INFO:
03930 if (skinnydebug)
03931 ast_verbose("Received Softkey Event: Info(%d)\n", instance);
03932 break;
03933 case SOFTKEY_CONFRN:
03934 if (skinnydebug)
03935 ast_verbose("Received Softkey Event: Transfer(%d)\n", instance);
03936 transmit_tone(s, SKINNY_DIALTONE);
03937
03938 break;
03939 case SOFTKEY_PARK:
03940 if (skinnydebug)
03941 ast_verbose("Received Softkey Event: Park Call(%d)\n", instance);
03942
03943 break;
03944 case SOFTKEY_JOIN:
03945 if (skinnydebug)
03946 ast_verbose("Received Softkey Event: Join(%d)\n", instance);
03947 break;
03948 case SOFTKEY_MEETME:
03949
03950 if (skinnydebug)
03951 ast_verbose("Received Softkey Event: Meetme(%d)\n", instance);
03952 break;
03953 case SOFTKEY_PICKUP:
03954 if (skinnydebug)
03955 ast_verbose("Received Softkey Event: None(%d)\n", instance);
03956 break;
03957 case SOFTKEY_GPICKUP:
03958 if (skinnydebug)
03959 ast_verbose("Received Softkey Event: Group Pickup (%d)\n", instance);
03960 break;
03961 default:
03962 if (skinnydebug)
03963 ast_verbose("Received unknown Softkey Event: %d(%d)\n", event, instance);
03964 break;
03965 }
03966 return 1;
03967 }
03968
03969 static int handle_unregister_message(struct skinny_req *req, struct skinnysession *s)
03970 {
03971 return skinny_unregister(req, s);
03972 }
03973
03974 static int handle_soft_key_template_req_message(struct skinny_req *req, struct skinnysession *s)
03975 {
03976 if (!(req = req_alloc(sizeof(struct soft_key_template_res_message), SOFT_KEY_TEMPLATE_RES_MESSAGE)))
03977 return -1;
03978
03979 req->data.softkeytemplate.softKeyOffset = htolel(0);
03980 req->data.softkeytemplate.softKeyCount = htolel(sizeof(soft_key_template_default) / sizeof(struct soft_key_template_definition));
03981 req->data.softkeytemplate.totalSoftKeyCount = htolel(sizeof(soft_key_template_default) / sizeof(struct soft_key_template_definition));
03982 memcpy(req->data.softkeytemplate.softKeyTemplateDefinition,
03983 soft_key_template_default,
03984 sizeof(soft_key_template_default));
03985 transmit_response(s,req);
03986 return 1;
03987 }
03988
03989 static int handle_headset_status_message(struct skinny_req *req, struct skinnysession *s)
03990 {
03991
03992 return 1;
03993 }
03994
03995 static int handle_register_available_lines_message(struct skinny_req *req, struct skinnysession *s)
03996 {
03997
03998 return 1;
03999 }
04000
04001 static int handle_message(struct skinny_req *req, struct skinnysession *s)
04002 {
04003 int res = 0;
04004
04005 if ((!s->device) && (letohl(req->e) != REGISTER_MESSAGE && letohl(req->e) != ALARM_MESSAGE)) {
04006 ast_log(LOG_WARNING, "Client sent message #%d without first registering.\n", req->e);
04007 free(req);
04008 return 0;
04009 }
04010
04011 switch(letohl(req->e)) {
04012 case KEEP_ALIVE_MESSAGE:
04013 res = handle_keep_alive_message(req, s);
04014 break;
04015 case REGISTER_MESSAGE:
04016 if (skinnydebug)
04017 ast_verbose("Device %s is attempting to register\n", req->data.reg.name);
04018
04019 res = handle_register_message(req, s);
04020 break;
04021 case IP_PORT_MESSAGE:
04022 res = handle_ip_port_message(req, s);
04023 break;
04024 case KEYPAD_BUTTON_MESSAGE:
04025 if (skinnydebug)
04026 ast_verbose("Collected digit: [%d]\n", letohl(req->data.keypad.button));
04027
04028 res = handle_keypad_button_message(req, s);
04029 break;
04030 case STIMULUS_MESSAGE:
04031 res = handle_stimulus_message(req, s);
04032 break;
04033 case OFFHOOK_MESSAGE:
04034 res = handle_offhook_message(req, s);
04035 break;
04036 case ONHOOK_MESSAGE:
04037 res = handle_onhook_message(req, s);
04038 break;
04039 case CAPABILITIES_RES_MESSAGE:
04040 if (skinnydebug)
04041 ast_verbose("Received CapabilitiesRes\n");
04042
04043 res = handle_capabilities_res_message(req, s);
04044 break;
04045 case SPEED_DIAL_STAT_REQ_MESSAGE:
04046 if (skinnydebug)
04047 ast_verbose("Received SpeedDialStatRequest\n");
04048
04049 res = handle_speed_dial_stat_req_message(req, s);
04050 break;
04051 case LINE_STATE_REQ_MESSAGE:
04052 res = handle_line_state_req_message(req, s);
04053 break;
04054 case TIME_DATE_REQ_MESSAGE:
04055 if (skinnydebug)
04056 ast_verbose("Received Time/Date Request\n");
04057
04058 res = handle_time_date_req_message(req, s);
04059 break;
04060 case BUTTON_TEMPLATE_REQ_MESSAGE:
04061 if (skinnydebug)
04062 ast_verbose("Buttontemplate requested\n");
04063
04064 res = handle_button_template_req_message(req, s);
04065 break;
04066 case VERSION_REQ_MESSAGE:
04067 if (skinnydebug)
04068 ast_verbose("Version Request\n");
04069
04070 res = handle_version_req_message(req, s);
04071 break;
04072 case SERVER_REQUEST_MESSAGE:
04073 if (skinnydebug)
04074 ast_verbose("Received Server Request\n");
04075
04076 res = handle_server_request_message(req, s);
04077 break;
04078 case ALARM_MESSAGE:
04079 res = handle_alarm_message(req, s);
04080 break;
04081 case OPEN_RECEIVE_CHANNEL_ACK_MESSAGE:
04082 if (skinnydebug)
04083 ast_verbose("Received Open Receive Channel Ack\n");
04084
04085 res = handle_open_receive_channel_ack_message(req, s);
04086 break;
04087 case SOFT_KEY_SET_REQ_MESSAGE:
04088 if (skinnydebug)
04089 ast_verbose("Received SoftKeySetReq\n");
04090
04091 res = handle_soft_key_set_req_message(req, s);
04092 break;
04093 case SOFT_KEY_EVENT_MESSAGE:
04094 res = handle_soft_key_event_message(req, s);
04095 break;
04096 case UNREGISTER_MESSAGE:
04097 if (skinnydebug)
04098 ast_verbose("Received Unregister Request\n");
04099
04100 res = handle_unregister_message(req, s);
04101 break;
04102 case SOFT_KEY_TEMPLATE_REQ_MESSAGE:
04103 if (skinnydebug)
04104 ast_verbose("Received SoftKey Template Request\n");
04105
04106 res = handle_soft_key_template_req_message(req, s);
04107 break;
04108 case HEADSET_STATUS_MESSAGE:
04109 res = handle_headset_status_message(req, s);
04110 break;
04111 case REGISTER_AVAILABLE_LINES_MESSAGE:
04112 res = handle_register_available_lines_message(req, s);
04113 break;
04114 default:
04115 if (skinnydebug)
04116 ast_verbose("RECEIVED UNKNOWN MESSAGE TYPE: %x\n", letohl(req->e));
04117 break;
04118 }
04119 if (res >= 0 && req)
04120 free(req);
04121 return res;
04122 }
04123
04124 static void destroy_session(struct skinnysession *s)
04125 {
04126 struct skinnysession *cur, *prev = NULL;
04127 ast_mutex_lock(&sessionlock);
04128 cur = sessions;
04129 while(cur) {
04130 if (cur == s) {
04131 break;
04132 }
04133 prev = cur;
04134 cur = cur->next;
04135 }
04136 if (cur) {
04137 if (prev) {
04138 prev->next = cur->next;
04139 } else {
04140 sessions = cur->next;
04141 }
04142 if (s->fd > -1) {
04143 close(s->fd);
04144 }
04145 ast_mutex_destroy(&s->lock);
04146 free(s);
04147 } else {
04148 ast_log(LOG_WARNING, "Trying to delete nonexistent session %p?\n", s);
04149 }
04150 ast_mutex_unlock(&sessionlock);
04151 }
04152
04153 static int get_input(struct skinnysession *s)
04154 {
04155 int res;
04156 int dlen = 0;
04157 struct pollfd fds[1];
04158
04159 fds[0].fd = s->fd;
04160 fds[0].events = POLLIN;
04161 fds[0].revents = 0;
04162 res = poll(fds, 1, -1);
04163
04164 if (res < 0) {
04165 if (errno != EINTR)
04166 {
04167 ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
04168 return res;
04169 }
04170 }
04171 if (fds[0].revents) {
04172 ast_mutex_lock(&s->lock);
04173 memset(s->inbuf,0,sizeof(s->inbuf));
04174 res = read(s->fd, s->inbuf, 4);
04175 if (res < 0) {
04176 ast_log(LOG_WARNING, "read() returned error: %s\n", strerror(errno));
04177
04178 if (skinnydebug)
04179 ast_verbose("Skinny Client was lost, unregistering\n");
04180
04181 skinny_unregister(NULL,s);
04182 ast_mutex_unlock(&s->lock);
04183 return res;
04184 } else if (res != 4) {
04185 ast_log(LOG_WARNING, "Skinny Client sent less data than expected. Expected 4 but got %d.\n", res);
04186 ast_mutex_unlock(&s->lock);
04187
04188 if (res == 0) {
04189 if (skinnydebug)
04190 ast_verbose("Skinny Client was lost, unregistering\n");
04191 skinny_unregister(NULL, s);
04192 }
04193
04194 return -1;
04195 }
04196
04197 dlen = letohl(*(int *)s->inbuf);
04198 if (dlen < 0) {
04199 ast_log(LOG_WARNING, "Skinny Client sent invalid data.\n");
04200 ast_mutex_unlock(&s->lock);
04201 return -1;
04202 }
04203 if (dlen+8 > sizeof(s->inbuf)) {
04204 dlen = sizeof(s->inbuf) - 8;
04205 }
04206 *(int *)s->inbuf = htolel(dlen);
04207
04208 res = read(s->fd, s->inbuf+4, dlen+4);
04209 ast_mutex_unlock(&s->lock);
04210 if (res < 0) {
04211 ast_log(LOG_WARNING, "read() returned error: %s\n", strerror(errno));
04212 return res;
04213 } else if (res != (dlen+4)) {
04214 ast_log(LOG_WARNING, "Skinny Client sent less data than expected.\n");
04215 return -1;
04216 }
04217 return res;
04218 }
04219 return 0;
04220 }
04221
04222 static struct skinny_req *skinny_req_parse(struct skinnysession *s)
04223 {
04224 struct skinny_req *req;
04225
04226 if (!(req = ast_calloc(1, SKINNY_MAX_PACKET)))
04227 return NULL;
04228
04229 ast_mutex_lock(&s->lock);
04230 memcpy(req, s->inbuf, skinny_header_size);
04231 memcpy(&req->data, s->inbuf+skinny_header_size, letohl(*(int*)(s->inbuf))-4);
04232
04233 ast_mutex_unlock(&s->lock);
04234
04235 if (letohl(req->e) < 0) {
04236 ast_log(LOG_ERROR, "Event Message is NULL from socket %d, This is bad\n", s->fd);
04237 free(req);
04238 return NULL;
04239 }
04240
04241 return req;
04242 }
04243
04244 static void *skinny_session(void *data)
04245 {
04246 int res;
04247 struct skinny_req *req;
04248 struct skinnysession *s = data;
04249
04250 if (option_verbose > 2)
04251 ast_verbose(VERBOSE_PREFIX_3 "Starting Skinny session from %s\n", ast_inet_ntoa(s->sin.sin_addr));
04252
04253 for (;;) {
04254 res = get_input(s);
04255 if (res < 0) {
04256 break;
04257 }
04258
04259 if (res > 0)
04260 {
04261 if (!(req = skinny_req_parse(s))) {
04262 destroy_session(s);
04263 return NULL;
04264 }
04265
04266 res = handle_message(req, s);
04267 if (res < 0) {
04268 destroy_session(s);
04269 return NULL;
04270 }
04271 }
04272 }
04273 ast_log(LOG_NOTICE, "Skinny Session returned: %s\n", strerror(errno));
04274
04275 if (s)
04276 destroy_session(s);
04277
04278 return 0;
04279 }
04280
04281 static void *accept_thread(void *ignore)
04282 {
04283 int as;
04284 struct sockaddr_in sin;
04285 socklen_t sinlen;
04286 struct skinnysession *s;
04287 struct protoent *p;
04288 int arg = 1;
04289 pthread_attr_t attr;
04290 pthread_t tcp_thread;
04291
04292 pthread_attr_init(&attr);
04293 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
04294
04295 for (;;) {
04296 sinlen = sizeof(sin);
04297 as = accept(skinnysock, (struct sockaddr *)&sin, &sinlen);
04298 if (as < 0) {
04299 ast_log(LOG_NOTICE, "Accept returned -1: %s\n", strerror(errno));
04300 continue;
04301 }
04302 p = getprotobyname("tcp");
04303 if(p) {
04304 if( setsockopt(as, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) {
04305 ast_log(LOG_WARNING, "Failed to set Skinny tcp connection to TCP_NODELAY mode: %s\n", strerror(errno));
04306 }
04307 }
04308 if (!(s = ast_calloc(1, sizeof(struct skinnysession))))
04309 continue;
04310
04311 memcpy(&s->sin, &sin, sizeof(sin));
04312 ast_mutex_init(&s->lock);
04313 s->fd = as;
04314 ast_mutex_lock(&sessionlock);
04315 s->next = sessions;
04316 sessions = s;
04317 ast_mutex_unlock(&sessionlock);
04318
04319 if (ast_pthread_create(&tcp_thread, &attr, skinny_session, s)) {
04320 destroy_session(s);
04321 }
04322 }
04323 if (skinnydebug)
04324 ast_verbose("killing accept thread\n");
04325 close(as);
04326 pthread_attr_destroy(&attr);
04327 return 0;
04328 }
04329
04330 static void *do_monitor(void *data)
04331 {
04332 int res;
04333
04334
04335
04336
04337 for(;;) {
04338 pthread_testcancel();
04339
04340 res = ast_sched_wait(sched);
04341 if ((res < 0) || (res > 1000)) {
04342 res = 1000;
04343 }
04344 res = ast_io_wait(io, res);
04345 ast_mutex_lock(&monlock);
04346 if (res >= 0) {
04347 ast_sched_runq(sched);
04348 }
04349 ast_mutex_unlock(&monlock);
04350 }
04351
04352 return NULL;
04353
04354 }
04355
04356 static int restart_monitor(void)
04357 {
04358
04359 if (monitor_thread == AST_PTHREADT_STOP)
04360 return 0;
04361
04362 ast_mutex_lock(&monlock);
04363 if (monitor_thread == pthread_self()) {
04364 ast_mutex_unlock(&monlock);
04365 ast_log(LOG_WARNING, "Cannot kill myself\n");
04366 return -1;
04367 }
04368 if (monitor_thread != AST_PTHREADT_NULL) {
04369
04370 pthread_kill(monitor_thread, SIGURG);
04371 } else {
04372
04373 if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
04374 ast_mutex_unlock(&monlock);
04375 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
04376 return -1;
04377 }
04378 }
04379 ast_mutex_unlock(&monlock);
04380 return 0;
04381 }
04382
04383 static struct ast_channel *skinny_request(const char *type, int format, void *data, int *cause)
04384 {
04385 int oldformat;
04386
04387 struct skinny_line *l;
04388 struct ast_channel *tmpc = NULL;
04389 char tmp[256];
04390 char *dest = data;
04391
04392 oldformat = format;
04393
04394 if (!(format &= ((AST_FORMAT_MAX_AUDIO << 1) - 1))) {
04395 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
04396 return NULL;
04397 }
04398
04399 ast_copy_string(tmp, dest, sizeof(tmp));
04400 if (ast_strlen_zero(tmp)) {
04401 ast_log(LOG_NOTICE, "Skinny channels require a device\n");
04402 return NULL;
04403 }
04404 l = find_line_by_name(tmp);
04405 if (!l) {
04406 ast_log(LOG_NOTICE, "No available lines on: %s\n", dest);
04407 return NULL;
04408 }
04409 if (option_verbose > 2) {
04410 ast_verbose(VERBOSE_PREFIX_3 "skinny_request(%s)\n", tmp);
04411 }
04412 tmpc = skinny_new(l, AST_STATE_DOWN);
04413 if (!tmpc) {
04414 ast_log(LOG_WARNING, "Unable to make channel for '%s'\n", tmp);
04415 }
04416 restart_monitor();
04417 return tmpc;
04418 }
04419
04420 static int reload_config(void)
04421 {
04422 int on = 1;
04423 struct ast_config *cfg;
04424 struct ast_variable *v;
04425 char *cat;
04426 struct skinny_device *d;
04427 int oldport = ntohs(bindaddr.sin_port);
04428
04429 if (gethostname(ourhost, sizeof(ourhost))) {
04430 ast_log(LOG_WARNING, "Unable to get hostname, Skinny disabled\n");
04431 return 0;
04432 }
04433 cfg = ast_config_load(config);
04434
04435
04436 if (!cfg) {
04437 ast_log(LOG_NOTICE, "Unable to load config %s, Skinny disabled\n", config);
04438 return -1;
04439 }
04440 memset(&bindaddr, 0, sizeof(bindaddr));
04441 memset(&default_prefs, 0, sizeof(default_prefs));
04442
04443
04444 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
04445
04446
04447 v = ast_variable_browse(cfg, "general");
04448 while (v) {
04449
04450 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value)) {
04451 v = v->next;
04452 continue;
04453 }
04454
04455
04456 if (!strcasecmp(v->name, "bindaddr")) {
04457 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
04458 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
04459 } else {
04460 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
04461 }
04462 } else if (!strcasecmp(v->name, "keepalive")) {
04463 keep_alive = atoi(v->value);
04464 } else if (!strcasecmp(v->name, "dateformat")) {
04465 ast_copy_string(date_format, v->value, sizeof(date_format));
04466 } else if (!strcasecmp(v->name, "allow")) {
04467 ast_parse_allow_disallow(&default_prefs, &default_capability, v->value, 1);
04468 } else if (!strcasecmp(v->name, "disallow")) {
04469 ast_parse_allow_disallow(&default_prefs, &default_capability, v->value, 0);
04470 } else if (!strcasecmp(v->name, "bindport") || !strcasecmp(v->name, "port")) {
04471 if (sscanf(v->value, "%d", &ourport) == 1) {
04472 bindaddr.sin_port = htons(ourport);
04473 } else {
04474 ast_log(LOG_WARNING, "Invalid bindport '%s' at line %d of %s\n", v->value, v->lineno, config);
04475 }
04476 if (!strcasecmp(v->name, "port")) {
04477 ast_log(LOG_WARNING, "Option 'port' at line %d of %s has been deprecated. Please use 'bindport' instead.\n", v->lineno, config);
04478 }
04479 }
04480 v = v->next;
04481 }
04482
04483 if (ntohl(bindaddr.sin_addr.s_addr)) {
04484 __ourip = bindaddr.sin_addr;
04485 } else {
04486 hp = ast_gethostbyname(ourhost, &ahp);
04487 if (!hp) {
04488 ast_log(LOG_WARNING, "Unable to get our IP address, Skinny disabled\n");
04489 ast_config_destroy(cfg);
04490 return 0;
04491 }
04492 memcpy(&__ourip, hp->h_addr, sizeof(__ourip));
04493 }
04494 if (!ntohs(bindaddr.sin_port)) {
04495 bindaddr.sin_port = ntohs(DEFAULT_SKINNY_PORT);
04496 }
04497 bindaddr.sin_family = AF_INET;
04498
04499
04500 cat = ast_category_browse(cfg, NULL);
04501 while(cat) {
04502 if (!strcasecmp(cat, "general")) {
04503
04504 #if 0
04505 } else if (!strncasecmp(cat, "paging-", 7)) {
04506 p = build_paging_device(cat, ast_variable_browse(cfg, cat));
04507 if (p) {
04508 }
04509 #endif
04510 } else {
04511 d = build_device(cat, ast_variable_browse(cfg, cat));
04512 if (d) {
04513 if (option_verbose > 2)
04514 ast_verbose(VERBOSE_PREFIX_3 "Added device '%s'\n", d->name);
04515 ast_mutex_lock(&devicelock);
04516 d->next = devices;
04517 devices = d;
04518 ast_mutex_unlock(&devicelock);
04519 }
04520 }
04521 cat = ast_category_browse(cfg, cat);
04522 }
04523 ast_mutex_lock(&netlock);
04524 if ((skinnysock > -1) && (ntohs(bindaddr.sin_port) != oldport)) {
04525 close(skinnysock);
04526 skinnysock = -1;
04527 }
04528 if (skinnysock < 0) {
04529 skinnysock = socket(AF_INET, SOCK_STREAM, 0);
04530 if(setsockopt(skinnysock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
04531 ast_log(LOG_ERROR, "Set Socket Options failed: errno %d, %s\n", errno, strerror(errno));
04532 ast_config_destroy(cfg);
04533 return 0;
04534 }
04535 if (skinnysock < 0) {
04536 ast_log(LOG_WARNING, "Unable to create Skinny socket: %s\n", strerror(errno));
04537 } else {
04538 if (bind(skinnysock, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
04539 ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
04540 ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port),
04541 strerror(errno));
04542 close(skinnysock);
04543 skinnysock = -1;
04544 ast_config_destroy(cfg);
04545 return 0;
04546 }
04547 if (listen(skinnysock,DEFAULT_SKINNY_BACKLOG)) {
04548 ast_log(LOG_WARNING, "Failed to start listening to %s:%d: %s\n",
04549 ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port),
04550 strerror(errno));
04551 close(skinnysock);
04552 skinnysock = -1;
04553 ast_config_destroy(cfg);
04554 return 0;
04555 }
04556 if (option_verbose > 1)
04557 ast_verbose(VERBOSE_PREFIX_2 "Skinny listening on %s:%d\n",
04558 ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port));
04559 ast_pthread_create_background(&accept_t,NULL, accept_thread, NULL);
04560 }
04561 }
04562 ast_mutex_unlock(&netlock);
04563 ast_config_destroy(cfg);
04564 return 1;
04565 }
04566
04567 static void delete_devices(void)
04568 {
04569 struct skinny_device *d, *dlast;
04570 struct skinny_line *l, *llast;
04571 struct skinny_speeddial *sd, *sdlast;
04572 struct skinny_addon *a, *alast;
04573
04574 ast_mutex_lock(&devicelock);
04575
04576
04577 for (d=devices;d;) {
04578
04579 for (l=d->lines;l;) {
04580 llast = l;
04581 l = l->next;
04582 ast_mutex_destroy(&llast->lock);
04583 free(llast);
04584 }
04585
04586 for (sd=d->speeddials;sd;) {
04587 sdlast = sd;
04588 sd = sd->next;
04589 ast_mutex_destroy(&sdlast->lock);
04590 free(sdlast);
04591 }
04592
04593 for (a=d->addons;a;) {
04594 alast = a;
04595 a = a->next;
04596 ast_mutex_destroy(&alast->lock);
04597 free(alast);
04598 }
04599 dlast = d;
04600 d = d->next;
04601 free(dlast);
04602 }
04603 devices=NULL;
04604 ast_mutex_unlock(&devicelock);
04605 }
04606
04607 #if 0
04608
04609
04610
04611
04612 static int reload(void)
04613 {
04614 delete_devices();
04615 reload_config();
04616 restart_monitor();
04617 return 0;
04618 }
04619 #endif
04620
04621 static int load_module(void)
04622 {
04623 int res = 0;
04624
04625 for (; res < (sizeof(soft_key_template_default) / sizeof(soft_key_template_default[0])); res++) {
04626 soft_key_template_default[res].softKeyEvent = htolel(soft_key_template_default[res].softKeyEvent);
04627 }
04628
04629 res = reload_config();
04630 if (res == -1) {
04631 return AST_MODULE_LOAD_DECLINE;
04632 }
04633
04634
04635 if (ast_channel_register(&skinny_tech)) {
04636 ast_log(LOG_ERROR, "Unable to register channel class 'Skinny'\n");
04637 return -1;
04638 }
04639
04640 ast_rtp_proto_register(&skinny_rtp);
04641 ast_cli_register_multiple(cli_skinny, sizeof(cli_skinny) / sizeof(struct ast_cli_entry));
04642 sched = sched_context_create();
04643 if (!sched) {
04644 ast_log(LOG_WARNING, "Unable to create schedule context\n");
04645 }
04646 io = io_context_create();
04647 if (!io) {
04648 ast_log(LOG_WARNING, "Unable to create I/O context\n");
04649 }
04650
04651 restart_monitor();
04652
04653 return res;
04654 }
04655
04656 static int unload_module(void)
04657 {
04658 struct skinnysession *s, *slast;
04659 struct skinny_device *d;
04660 struct skinny_line *l;
04661 struct skinny_subchannel *sub;
04662
04663 ast_mutex_lock(&sessionlock);
04664
04665 s = sessions;
04666 while(s) {
04667 slast = s;
04668 s = s->next;
04669 for (d = slast->device; d; d = d->next) {
04670 for (l = d->lines; l; l = l->next) {
04671 ast_mutex_lock(&l->lock);
04672 for (sub = l->sub; sub; sub = sub->next) {
04673 ast_mutex_lock(&sub->lock);
04674 if (sub->owner) {
04675 sub->alreadygone = 1;
04676 ast_softhangup(sub->owner, AST_SOFTHANGUP_APPUNLOAD);
04677 }
04678 ast_mutex_unlock(&sub->lock);
04679 }
04680 ast_mutex_unlock(&l->lock);
04681 }
04682 }
04683 if (slast->fd > -1)
04684 close(slast->fd);
04685 ast_mutex_destroy(&slast->lock);
04686 free(slast);
04687 }
04688 sessions = NULL;
04689 ast_mutex_unlock(&sessionlock);
04690
04691 delete_devices();
04692
04693 ast_mutex_lock(&monlock);
04694 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
04695 pthread_cancel(monitor_thread);
04696 pthread_kill(monitor_thread, SIGURG);
04697 pthread_join(monitor_thread, NULL);
04698 }
04699 monitor_thread = AST_PTHREADT_STOP;
04700 ast_mutex_unlock(&monlock);
04701
04702 ast_mutex_lock(&netlock);
04703 if (accept_t && (accept_t != AST_PTHREADT_STOP)) {
04704 pthread_cancel(accept_t);
04705 pthread_kill(accept_t, SIGURG);
04706 pthread_join(accept_t, NULL);
04707 }
04708 accept_t = AST_PTHREADT_STOP;
04709 ast_mutex_unlock(&netlock);
04710
04711 ast_rtp_proto_unregister(&skinny_rtp);
04712 ast_channel_unregister(&skinny_tech);
04713 ast_cli_unregister_multiple(cli_skinny, sizeof(cli_skinny) / sizeof(struct ast_cli_entry));
04714
04715 close(skinnysock);
04716 sched_context_destroy(sched);
04717
04718 return 0;
04719 }
04720
04721 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Skinny Client Control Protocol (Skinny)",
04722 .load = load_module,
04723 .unload = unload_module,
04724 );