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