2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * chan_skinny was developed by Jeremy McNamara & Florian Overkamp
7 * chan_skinny was heavily modified/fixed by North Antara
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
22 * \brief Implementation of the Skinny protocol
24 * \author Jeremy McNamara & Florian Overkamp & North Antara
25 * \ingroup channel_drivers
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
36 #include <sys/ioctl.h>
40 #include <arpa/inet.h>
41 #include <sys/signal.h>
45 #include "asterisk/lock.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/config.h"
48 #include "asterisk/module.h"
49 #include "asterisk/pbx.h"
50 #include "asterisk/sched.h"
51 #include "asterisk/io.h"
52 #include "asterisk/rtp.h"
53 #include "asterisk/netsock.h"
54 #include "asterisk/acl.h"
55 #include "asterisk/callerid.h"
56 #include "asterisk/cli.h"
57 #include "asterisk/say.h"
58 #include "asterisk/cdr.h"
59 #include "asterisk/astdb.h"
60 #include "asterisk/features.h"
61 #include "asterisk/app.h"
62 #include "asterisk/musiconhold.h"
63 #include "asterisk/utils.h"
64 #include "asterisk/dsp.h"
65 #include "asterisk/stringfields.h"
66 #include "asterisk/astobj.h"
67 #include "asterisk/abstract_jb.h"
68 #include "asterisk/threadstorage.h"
69 #include "asterisk/devicestate.h"
71 /*************************************
72 * Skinny/Asterisk Protocol Settings *
73 *************************************/
74 static const char tdesc[] = "Skinny Client Control Protocol (Skinny)";
75 static const char config[] = "skinny.conf";
77 static int default_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW;
78 static struct ast_codec_pref default_prefs;
81 SKINNY_CODEC_ALAW = 2,
82 SKINNY_CODEC_ULAW = 4,
83 SKINNY_CODEC_G723_1 = 9,
84 SKINNY_CODEC_G729A = 12,
85 SKINNY_CODEC_G726_32 = 82, /* XXX Which packing order does this translate to? */
86 SKINNY_CODEC_H261 = 100,
87 SKINNY_CODEC_H263 = 101
90 #define DEFAULT_SKINNY_PORT 2000
91 #define DEFAULT_SKINNY_BACKLOG 2
92 #define SKINNY_MAX_PACKET 1000
94 static unsigned int tos = 0;
95 static unsigned int tos_audio = 0;
96 static unsigned int tos_video = 0;
97 static unsigned int cos = 0;
98 static unsigned int cos_audio = 0;
99 static unsigned int cos_video = 0;
101 static int keep_alive = 120;
102 static char vmexten[AST_MAX_EXTENSION]; /* Voicemail pilot number */
103 static char used_context[AST_MAX_EXTENSION]; /* Voicemail pilot number */
104 static char regcontext[AST_MAX_CONTEXT]; /* Context for auto-extension */
105 static char date_format[6] = "D-M-Y";
106 static char version_id[16] = "P002F202";
108 #if __BYTE_ORDER == __LITTLE_ENDIAN
109 #define letohl(x) (x)
110 #define letohs(x) (x)
111 #define htolel(x) (x)
112 #define htoles(x) (x)
114 #if defined(HAVE_BYTESWAP_H)
115 #include <byteswap.h>
116 #define letohl(x) bswap_32(x)
117 #define letohs(x) bswap_16(x)
118 #define htolel(x) bswap_32(x)
119 #define htoles(x) bswap_16(x)
120 #elif defined(HAVE_SYS_ENDIAN_SWAP16)
121 #include <sys/endian.h>
122 #define letohl(x) __swap32(x)
123 #define letohs(x) __swap16(x)
124 #define htolel(x) __swap32(x)
125 #define htoles(x) __swap16(x)
126 #elif defined(HAVE_SYS_ENDIAN_BSWAP16)
127 #include <sys/endian.h>
128 #define letohl(x) bswap32(x)
129 #define letohs(x) bswap16(x)
130 #define htolel(x) bswap32(x)
131 #define htoles(x) bswap16(x)
133 #define __bswap_16(x) \
134 ((((x) & 0xff00) >> 8) | \
135 (((x) & 0x00ff) << 8))
136 #define __bswap_32(x) \
137 ((((x) & 0xff000000) >> 24) | \
138 (((x) & 0x00ff0000) >> 8) | \
139 (((x) & 0x0000ff00) << 8) | \
140 (((x) & 0x000000ff) << 24))
141 #define letohl(x) __bswap_32(x)
142 #define letohs(x) __bswap_16(x)
143 #define htolel(x) __bswap_32(x)
144 #define htoles(x) __bswap_16(x)
148 /*! Global jitterbuffer configuration - by default, jb is disabled */
149 static struct ast_jb_conf default_jbconf =
153 .resync_threshold = -1,
156 static struct ast_jb_conf global_jbconf;
158 AST_THREADSTORAGE(device2str_threadbuf);
159 #define DEVICE2STR_BUFSIZE 15
161 AST_THREADSTORAGE(control2str_threadbuf);
162 #define CONTROL2STR_BUFSIZE 100
164 /*********************
165 * Protocol Messages *
166 *********************/
168 #define KEEP_ALIVE_MESSAGE 0x0000
169 /* no additional struct */
171 #define REGISTER_MESSAGE 0x0001
172 struct register_message {
181 #define IP_PORT_MESSAGE 0x0002
183 #define KEYPAD_BUTTON_MESSAGE 0x0003
184 struct keypad_button_message {
186 uint32_t lineInstance;
187 uint32_t callReference;
191 #define ENBLOC_CALL_MESSAGE 0x0004
192 struct enbloc_call_message {
193 char calledParty[24];
196 #define STIMULUS_MESSAGE 0x0005
197 struct stimulus_message {
199 uint32_t stimulusInstance;
200 uint32_t callreference;
203 #define OFFHOOK_MESSAGE 0x0006
204 struct offhook_message {
209 #define ONHOOK_MESSAGE 0x0007
210 struct onhook_message {
215 #define CAPABILITIES_RES_MESSAGE 0x0010
216 struct station_capabilities {
225 #define SKINNY_MAX_CAPABILITIES 18
227 struct capabilities_res_message {
229 struct station_capabilities caps[SKINNY_MAX_CAPABILITIES];
232 #define SPEED_DIAL_STAT_REQ_MESSAGE 0x000A
233 struct speed_dial_stat_req_message {
234 uint32_t speedDialNumber;
237 #define LINE_STATE_REQ_MESSAGE 0x000B
238 struct line_state_req_message {
242 #define TIME_DATE_REQ_MESSAGE 0x000D
243 #define BUTTON_TEMPLATE_REQ_MESSAGE 0x000E
244 #define VERSION_REQ_MESSAGE 0x000F
245 #define SERVER_REQUEST_MESSAGE 0x0012
247 #define ALARM_MESSAGE 0x0020
248 struct alarm_message {
249 uint32_t alarmSeverity;
250 char displayMessage[80];
251 uint32_t alarmParam1;
252 uint32_t alarmParam2;
255 #define OPEN_RECEIVE_CHANNEL_ACK_MESSAGE 0x0022
256 struct open_receive_channel_ack_message {
263 #define SOFT_KEY_SET_REQ_MESSAGE 0x0025
265 #define SOFT_KEY_EVENT_MESSAGE 0x0026
266 struct soft_key_event_message {
267 uint32_t softKeyEvent;
269 uint32_t callreference;
272 #define UNREGISTER_MESSAGE 0x0027
273 #define SOFT_KEY_TEMPLATE_REQ_MESSAGE 0x0028
274 #define HEADSET_STATUS_MESSAGE 0x002B
275 #define REGISTER_AVAILABLE_LINES_MESSAGE 0x002D
277 #define REGISTER_ACK_MESSAGE 0x0081
278 struct register_ack_message {
280 char dateTemplate[6];
282 uint32_t secondaryKeepAlive;
286 #define START_TONE_MESSAGE 0x0082
287 struct start_tone_message {
294 #define STOP_TONE_MESSAGE 0x0083
295 struct stop_tone_message {
300 #define SET_RINGER_MESSAGE 0x0085
301 struct set_ringer_message {
303 uint32_t unknown1; /* See notes in transmit_ringer_mode */
308 #define SET_LAMP_MESSAGE 0x0086
309 struct set_lamp_message {
311 uint32_t stimulusInstance;
312 uint32_t deviceStimulus;
315 #define SET_SPEAKER_MESSAGE 0x0088
316 struct set_speaker_message {
320 /* XXX When do we need to use this? */
321 #define SET_MICROPHONE_MESSAGE 0x0089
322 struct set_microphone_message {
326 #define START_MEDIA_TRANSMISSION_MESSAGE 0x008A
327 struct media_qualifier {
334 struct start_media_transmission_message {
335 uint32_t conferenceId;
336 uint32_t passThruPartyId;
340 uint32_t payloadType;
341 struct media_qualifier qualifier;
345 #define STOP_MEDIA_TRANSMISSION_MESSAGE 0x008B
346 struct stop_media_transmission_message {
347 uint32_t conferenceId;
348 uint32_t passThruPartyId;
352 #define CALL_INFO_MESSAGE 0x008F
353 struct call_info_message {
354 char callingPartyName[40];
355 char callingParty[24];
356 char calledPartyName[40];
357 char calledParty[24];
361 char originalCalledPartyName[40];
362 char originalCalledParty[24];
363 char lastRedirectingPartyName[40];
364 char lastRedirectingParty[24];
365 uint32_t originalCalledPartyRedirectReason;
366 uint32_t lastRedirectingReason;
367 char callingPartyVoiceMailbox[24];
368 char calledPartyVoiceMailbox[24];
369 char originalCalledPartyVoiceMailbox[24];
370 char lastRedirectingVoiceMailbox[24];
374 #define FORWARD_STAT_MESSAGE 0x0090
375 struct forward_stat_message {
376 uint32_t activeforward;
382 uint32_t fwdnoanswer;
383 char fwdnoanswernum[24];
386 #define SPEED_DIAL_STAT_RES_MESSAGE 0x0091
387 struct speed_dial_stat_res_message {
388 uint32_t speedDialNumber;
389 char speedDialDirNumber[24];
390 char speedDialDisplayName[40];
393 #define LINE_STAT_RES_MESSAGE 0x0092
394 struct line_stat_res_message {
396 char lineDirNumber[24];
397 char lineDisplayName[24];
401 #define DEFINETIMEDATE_MESSAGE 0x0094
402 struct definetimedate_message {
403 uint32_t year; /* since 1900 */
405 uint32_t dayofweek; /* monday = 1 */
410 uint32_t milliseconds;
414 #define BUTTON_TEMPLATE_RES_MESSAGE 0x0097
415 struct button_definition {
416 uint8_t instanceNumber;
417 uint8_t buttonDefinition;
420 struct button_definition_template {
421 uint8_t buttonDefinition;
422 /* for now, anything between 0xB0 and 0xCF is custom */
426 #define STIMULUS_REDIAL 0x01
427 #define STIMULUS_SPEEDDIAL 0x02
428 #define STIMULUS_HOLD 0x03
429 #define STIMULUS_TRANSFER 0x04
430 #define STIMULUS_FORWARDALL 0x05
431 #define STIMULUS_FORWARDBUSY 0x06
432 #define STIMULUS_FORWARDNOANSWER 0x07
433 #define STIMULUS_DISPLAY 0x08
434 #define STIMULUS_LINE 0x09
435 #define STIMULUS_VOICEMAIL 0x0F
436 #define STIMULUS_AUTOANSWER 0x11
437 #define STIMULUS_DND 0x3F
438 #define STIMULUS_CONFERENCE 0x7D
439 #define STIMULUS_CALLPARK 0x7E
440 #define STIMULUS_CALLPICKUP 0x7F
441 #define STIMULUS_NONE 0xFF
444 #define BT_REDIAL STIMULUS_REDIAL
445 #define BT_SPEEDDIAL STIMULUS_SPEEDDIAL
446 #define BT_HOLD STIMULUS_HOLD
447 #define BT_TRANSFER STIMULUS_TRANSFER
448 #define BT_FORWARDALL STIMULUS_FORWARDALL
449 #define BT_FORWARDBUSY STIMULUS_FORWARDBUSY
450 #define BT_FORWARDNOANSWER STIMULUS_FORWARDNOANSWER
451 #define BT_DISPLAY STIMULUS_DISPLAY
452 #define BT_LINE STIMULUS_LINE
453 #define BT_VOICEMAIL STIMULUS_VOICEMAIL
454 #define BT_AUTOANSWER STIMULUS_AUTOANSWER
455 #define BT_DND STIMULUS_DND
456 #define BT_CONFERENCE STIMULUS_CONFERENCE
457 #define BT_CALLPARK STIMULUS_CALLPARK
458 #define BT_CALLPICKUP STIMULUS_CALLPICKUP
461 /* Custom button types - add our own between 0xB0 and 0xCF.
462 This may need to be revised in the future,
463 if stimuluses are ever added in this range. */
464 #define BT_CUST_LINESPEEDDIAL 0xB0 /* line or speeddial with/without hint */
465 #define BT_CUST_LINE 0xB1 /* line or speeddial with hint only */
467 struct button_template_res_message {
468 uint32_t buttonOffset;
469 uint32_t buttonCount;
470 uint32_t totalButtonCount;
471 struct button_definition definition[42];
474 #define VERSION_RES_MESSAGE 0x0098
475 struct version_res_message {
479 #define DISPLAYTEXT_MESSAGE 0x0099
480 struct displaytext_message {
484 #define CLEAR_NOTIFY_MESSAGE 0x0115
485 #define CLEAR_DISPLAY_MESSAGE 0x009A
487 #define CAPABILITIES_REQ_MESSAGE 0x009B
489 #define REGISTER_REJ_MESSAGE 0x009D
490 struct register_rej_message {
494 #define SERVER_RES_MESSAGE 0x009E
495 struct server_identifier {
499 struct server_res_message {
500 struct server_identifier server[5];
501 uint32_t serverListenPort[5];
502 uint32_t serverIpAddr[5];
505 #define RESET_MESSAGE 0x009F
506 struct reset_message {
510 #define KEEP_ALIVE_ACK_MESSAGE 0x0100
512 #define OPEN_RECEIVE_CHANNEL_MESSAGE 0x0105
513 struct open_receive_channel_message {
514 uint32_t conferenceId;
523 #define CLOSE_RECEIVE_CHANNEL_MESSAGE 0x0106
524 struct close_receive_channel_message {
525 uint32_t conferenceId;
530 #define SOFT_KEY_TEMPLATE_RES_MESSAGE 0x0108
532 struct soft_key_template_definition {
533 char softKeyLabel[16];
534 uint32_t softKeyEvent;
537 #define KEYDEF_ONHOOK 0
538 #define KEYDEF_CONNECTED 1
539 #define KEYDEF_ONHOLD 2
540 #define KEYDEF_RINGIN 3
541 #define KEYDEF_OFFHOOK 4
542 #define KEYDEF_CONNWITHTRANS 5
543 #define KEYDEF_DADFD 6 /* Digits After Dialing First Digit */
544 #define KEYDEF_CONNWITHCONF 7
545 #define KEYDEF_RINGOUT 8
546 #define KEYDEF_OFFHOOKWITHFEAT 9
547 #define KEYDEF_UNKNOWN 10
549 #define SOFTKEY_NONE 0x00
550 #define SOFTKEY_REDIAL 0x01
551 #define SOFTKEY_NEWCALL 0x02
552 #define SOFTKEY_HOLD 0x03
553 #define SOFTKEY_TRNSFER 0x04
554 #define SOFTKEY_CFWDALL 0x05
555 #define SOFTKEY_CFWDBUSY 0x06
556 #define SOFTKEY_CFWDNOANSWER 0x07
557 #define SOFTKEY_BKSPC 0x08
558 #define SOFTKEY_ENDCALL 0x09
559 #define SOFTKEY_RESUME 0x0A
560 #define SOFTKEY_ANSWER 0x0B
561 #define SOFTKEY_INFO 0x0C
562 #define SOFTKEY_CONFRN 0x0D
563 #define SOFTKEY_PARK 0x0E
564 #define SOFTKEY_JOIN 0x0F
565 #define SOFTKEY_MEETME 0x10
566 #define SOFTKEY_PICKUP 0x11
567 #define SOFTKEY_GPICKUP 0x12
568 #define SOFTKEY_DND 0x13
569 #define SOFTKEY_IDIVERT 0x14
571 struct soft_key_template_definition soft_key_template_default[] = {
572 { "\200\001", SOFTKEY_REDIAL },
573 { "\200\002", SOFTKEY_NEWCALL },
574 { "\200\003", SOFTKEY_HOLD },
575 { "\200\004", SOFTKEY_TRNSFER },
576 { "\200\005", SOFTKEY_CFWDALL },
577 { "\200\006", SOFTKEY_CFWDBUSY },
578 { "\200\007", SOFTKEY_CFWDNOANSWER },
579 { "\200\010", SOFTKEY_BKSPC },
580 { "\200\011", SOFTKEY_ENDCALL },
581 { "\200\012", SOFTKEY_RESUME },
582 { "\200\013", SOFTKEY_ANSWER },
583 { "\200\014", SOFTKEY_INFO },
584 { "\200\015", SOFTKEY_CONFRN },
585 { "\200\016", SOFTKEY_PARK },
586 { "\200\017", SOFTKEY_JOIN },
587 { "\200\020", SOFTKEY_MEETME },
588 { "\200\021", SOFTKEY_PICKUP },
589 { "\200\022", SOFTKEY_GPICKUP },
590 { "\200\077", SOFTKEY_DND },
591 { "\200\120", SOFTKEY_IDIVERT },
594 /* Localized message "codes" (in octal)
595 Below is en_US (taken from a 7970)
617 \023: Your current options
634 \044: You Have VoiceMail
636 \046: Can Not Complete Conference
637 \047: No Conference Bridge
638 \050: Can Not Hold Primary Control
639 \051: Invalid Conference Participant
640 \052: In Conference Already
641 \053: No Participant Info
642 \054: Exceed Maximum Parties
643 \055: Key Is Not Active
644 \056: Error No License
647 \061: Error Pass Limit
653 \067: Not Enough Bandwidth
664 \102: Network congestion,rerouting
666 \104: Failed to setup Barge
667 \105: Another Barge exists
668 \106: Incompatible device type
669 \107: No Park Number Available
670 \110: CallPark Reversion
671 \111: Service is not Active
672 \112: High Traffic Try Again Later
680 \122: Can Not Complete Transfer
681 \123: Can Not Join Calls
682 \124: Mcid Successful
683 \125: Number Not Configured
685 \127: Video Bandwidth Unavailable
687 \131: Max Call Duration Timeout
688 \132: Max Hold Duration Timeout
695 \141: External Transfer Restricted
705 \153: Default Router 1
706 \154: Default Router 2
707 \155: Default Router 3
708 \156: Default Router 4
709 \157: Default Router 5
715 \165: Operational VLAN Id
722 \174: Information URL
723 \175: Directories URL
728 struct soft_key_definitions {
730 const uint8_t *defaults;
734 static const uint8_t soft_key_default_onhook[] = {
744 static const uint8_t soft_key_default_connected[] = {
753 static const uint8_t soft_key_default_onhold[] = {
760 static const uint8_t soft_key_default_ringin[] = {
766 static const uint8_t soft_key_default_offhook[] = {
774 static const uint8_t soft_key_default_connwithtrans[] = {
783 static const uint8_t soft_key_default_dadfd[] = {
788 static const uint8_t soft_key_default_connwithconf[] = {
792 static const uint8_t soft_key_default_ringout[] = {
797 static const uint8_t soft_key_default_offhookwithfeat[] = {
802 static const uint8_t soft_key_default_unknown[] = {
806 static const struct soft_key_definitions soft_key_default_definitions[] = {
807 {KEYDEF_ONHOOK, soft_key_default_onhook, sizeof(soft_key_default_onhook) / sizeof(uint8_t)},
808 {KEYDEF_CONNECTED, soft_key_default_connected, sizeof(soft_key_default_connected) / sizeof(uint8_t)},
809 {KEYDEF_ONHOLD, soft_key_default_onhold, sizeof(soft_key_default_onhold) / sizeof(uint8_t)},
810 {KEYDEF_RINGIN, soft_key_default_ringin, sizeof(soft_key_default_ringin) / sizeof(uint8_t)},
811 {KEYDEF_OFFHOOK, soft_key_default_offhook, sizeof(soft_key_default_offhook) / sizeof(uint8_t)},
812 {KEYDEF_CONNWITHTRANS, soft_key_default_connwithtrans, sizeof(soft_key_default_connwithtrans) / sizeof(uint8_t)},
813 {KEYDEF_DADFD, soft_key_default_dadfd, sizeof(soft_key_default_dadfd) / sizeof(uint8_t)},
814 {KEYDEF_CONNWITHCONF, soft_key_default_connwithconf, sizeof(soft_key_default_connwithconf) / sizeof(uint8_t)},
815 {KEYDEF_RINGOUT, soft_key_default_ringout, sizeof(soft_key_default_ringout) / sizeof(uint8_t)},
816 {KEYDEF_OFFHOOKWITHFEAT, soft_key_default_offhookwithfeat, sizeof(soft_key_default_offhookwithfeat) / sizeof(uint8_t)},
817 {KEYDEF_UNKNOWN, soft_key_default_unknown, sizeof(soft_key_default_unknown) / sizeof(uint8_t)}
820 struct soft_key_template_res_message {
821 uint32_t softKeyOffset;
822 uint32_t softKeyCount;
823 uint32_t totalSoftKeyCount;
824 struct soft_key_template_definition softKeyTemplateDefinition[32];
827 #define SOFT_KEY_SET_RES_MESSAGE 0x0109
829 struct soft_key_set_definition {
830 uint8_t softKeyTemplateIndex[16];
831 uint16_t softKeyInfoIndex[16];
834 struct soft_key_set_res_message {
835 uint32_t softKeySetOffset;
836 uint32_t softKeySetCount;
837 uint32_t totalSoftKeySetCount;
838 struct soft_key_set_definition softKeySetDefinition[16];
842 #define SELECT_SOFT_KEYS_MESSAGE 0x0110
843 struct select_soft_keys_message {
846 uint32_t softKeySetIndex;
847 uint32_t validKeyMask;
850 #define CALL_STATE_MESSAGE 0x0111
851 struct call_state_message {
853 uint32_t lineInstance;
854 uint32_t callReference;
858 #define DISPLAY_PROMPT_STATUS_MESSAGE 0x0112
859 struct display_prompt_status_message {
860 uint32_t messageTimeout;
861 char promptMessage[32];
862 uint32_t lineInstance;
863 uint32_t callReference;
866 #define CLEAR_PROMPT_MESSAGE 0x0113
867 struct clear_prompt_message {
868 uint32_t lineInstance;
869 uint32_t callReference;
872 #define DISPLAY_NOTIFY_MESSAGE 0x0114
873 struct display_notify_message {
874 uint32_t displayTimeout;
875 char displayMessage[100];
878 #define ACTIVATE_CALL_PLANE_MESSAGE 0x0116
879 struct activate_call_plane_message {
880 uint32_t lineInstance;
883 #define DIALED_NUMBER_MESSAGE 0x011D
884 struct dialed_number_message {
885 char dialedNumber[24];
886 uint32_t lineInstance;
887 uint32_t callReference;
891 struct alarm_message alarm;
892 struct speed_dial_stat_req_message speeddialreq;
893 struct register_message reg;
894 struct register_ack_message regack;
895 struct register_rej_message regrej;
896 struct capabilities_res_message caps;
897 struct version_res_message version;
898 struct button_template_res_message buttontemplate;
899 struct displaytext_message displaytext;
900 struct display_prompt_status_message displaypromptstatus;
901 struct clear_prompt_message clearpromptstatus;
902 struct definetimedate_message definetimedate;
903 struct start_tone_message starttone;
904 struct stop_tone_message stoptone;
905 struct speed_dial_stat_res_message speeddial;
906 struct line_state_req_message line;
907 struct line_stat_res_message linestat;
908 struct soft_key_set_res_message softkeysets;
909 struct soft_key_template_res_message softkeytemplate;
910 struct server_res_message serverres;
911 struct reset_message reset;
912 struct set_lamp_message setlamp;
913 struct set_ringer_message setringer;
914 struct call_state_message callstate;
915 struct keypad_button_message keypad;
916 struct select_soft_keys_message selectsoftkey;
917 struct activate_call_plane_message activatecallplane;
918 struct stimulus_message stimulus;
919 struct offhook_message offhook;
920 struct onhook_message onhook;
921 struct set_speaker_message setspeaker;
922 struct set_microphone_message setmicrophone;
923 struct call_info_message callinfo;
924 struct start_media_transmission_message startmedia;
925 struct stop_media_transmission_message stopmedia;
926 struct open_receive_channel_message openreceivechannel;
927 struct open_receive_channel_ack_message openreceivechannelack;
928 struct close_receive_channel_message closereceivechannel;
929 struct display_notify_message displaynotify;
930 struct dialed_number_message dialednumber;
931 struct soft_key_event_message softkeyeventmessage;
932 struct enbloc_call_message enbloccallmessage;
933 struct forward_stat_message forwardstat;
936 /* packet composition */
941 union skinny_data data;
944 /* XXX This is the combined size of the variables above. (len, res, e)
945 If more are added, this MUST change.
946 (sizeof(skinny_req) - sizeof(skinny_data)) DOES NOT WORK on all systems (amd64?). */
947 int skinny_header_size = 12;
949 /*****************************
950 * Asterisk specific globals *
951 *****************************/
953 static int skinnydebug = 0;
955 /* a hostname, portnumber, socket and such is usefull for VoIP protocols */
956 static struct sockaddr_in bindaddr;
957 static char ourhost[256];
959 static struct in_addr __ourip;
960 struct ast_hostent ahp;
962 static int skinnysock = -1;
963 static pthread_t accept_t;
964 static char context[AST_MAX_CONTEXT] = "default";
965 static char language[MAX_LANGUAGE] = "";
966 static char mohinterpret[MAX_MUSICCLASS] = "default";
967 static char mohsuggest[MAX_MUSICCLASS] = "";
968 static char cid_num[AST_MAX_EXTENSION] = "";
969 static char cid_name[AST_MAX_EXTENSION] = "";
970 static char linelabel[AST_MAX_EXTENSION] ="";
972 static ast_group_t cur_callergroup = 0;
973 static ast_group_t cur_pickupgroup = 0;
974 static int immediate = 0;
975 static int callwaiting = 0;
976 static int callreturn = 0;
977 static int threewaycalling = 0;
978 static int mwiblink = 0;
979 /* This is for flashhook transfers */
980 static int transfer = 0;
981 static int cancallforward = 0;
982 /* static int busycount = 3;*/
983 static char accountcode[AST_MAX_ACCOUNT_CODE] = "";
984 static char mailbox[AST_MAX_EXTENSION];
985 static char regexten[AST_MAX_EXTENSION];
986 static int amaflags = 0;
987 static int callnums = 1;
988 static int canreinvite = 0;
990 #define SKINNY_DEVICE_UNKNOWN -1
991 #define SKINNY_DEVICE_NONE 0
992 #define SKINNY_DEVICE_30SPPLUS 1
993 #define SKINNY_DEVICE_12SPPLUS 2
994 #define SKINNY_DEVICE_12SP 3
995 #define SKINNY_DEVICE_12 4
996 #define SKINNY_DEVICE_30VIP 5
997 #define SKINNY_DEVICE_7910 6
998 #define SKINNY_DEVICE_7960 7
999 #define SKINNY_DEVICE_7940 8
1000 #define SKINNY_DEVICE_7935 9
1001 #define SKINNY_DEVICE_ATA186 12 /* Cisco ATA-186 */
1002 #define SKINNY_DEVICE_7941 115
1003 #define SKINNY_DEVICE_7971 119
1004 #define SKINNY_DEVICE_7914 124 /* Expansion module */
1005 #define SKINNY_DEVICE_7985 302
1006 #define SKINNY_DEVICE_7911 307
1007 #define SKINNY_DEVICE_7961GE 308
1008 #define SKINNY_DEVICE_7941GE 309
1009 #define SKINNY_DEVICE_7931 348
1010 #define SKINNY_DEVICE_7921 365
1011 #define SKINNY_DEVICE_7906 369
1012 #define SKINNY_DEVICE_7962 404 /* Not found */
1013 #define SKINNY_DEVICE_7937 431
1014 #define SKINNY_DEVICE_7942 434
1015 #define SKINNY_DEVICE_7945 435
1016 #define SKINNY_DEVICE_7965 436
1017 #define SKINNY_DEVICE_7975 437
1018 #define SKINNY_DEVICE_7905 20000
1019 #define SKINNY_DEVICE_7920 30002
1020 #define SKINNY_DEVICE_7970 30006
1021 #define SKINNY_DEVICE_7912 30007
1022 #define SKINNY_DEVICE_7902 30008
1023 #define SKINNY_DEVICE_CIPC 30016 /* Cisco IP Communicator */
1024 #define SKINNY_DEVICE_7961 30018
1025 #define SKINNY_DEVICE_7936 30019
1026 #define SKINNY_DEVICE_SCCPGATEWAY_AN 30027 /* ??? */
1027 #define SKINNY_DEVICE_SCCPGATEWAY_BRI 30028 /* ??? */
1029 #define SKINNY_SPEAKERON 1
1030 #define SKINNY_SPEAKEROFF 2
1032 #define SKINNY_MICON 1
1033 #define SKINNY_MICOFF 2
1035 #define SKINNY_OFFHOOK 1
1036 #define SKINNY_ONHOOK 2
1037 #define SKINNY_RINGOUT 3
1038 #define SKINNY_RINGIN 4
1039 #define SKINNY_CONNECTED 5
1040 #define SKINNY_BUSY 6
1041 #define SKINNY_CONGESTION 7
1042 #define SKINNY_HOLD 8
1043 #define SKINNY_CALLWAIT 9
1044 #define SKINNY_TRANSFER 10
1045 #define SKINNY_PARK 11
1046 #define SKINNY_PROGRESS 12
1047 #define SKINNY_CALLREMOTEMULTILINE 13
1048 #define SKINNY_INVALID 14
1050 #define SKINNY_SILENCE 0x00
1051 #define SKINNY_DIALTONE 0x21
1052 #define SKINNY_BUSYTONE 0x23
1053 #define SKINNY_ALERT 0x24
1054 #define SKINNY_REORDER 0x25
1055 #define SKINNY_CALLWAITTONE 0x2D
1056 #define SKINNY_NOTONE 0x7F
1058 #define SKINNY_LAMP_OFF 1
1059 #define SKINNY_LAMP_ON 2
1060 #define SKINNY_LAMP_WINK 3
1061 #define SKINNY_LAMP_FLASH 4
1062 #define SKINNY_LAMP_BLINK 5
1064 #define SKINNY_RING_OFF 1
1065 #define SKINNY_RING_INSIDE 2
1066 #define SKINNY_RING_OUTSIDE 3
1067 #define SKINNY_RING_FEATURE 4
1069 #define SKINNY_CFWD_ALL (1 << 0)
1070 #define SKINNY_CFWD_BUSY (1 << 1)
1071 #define SKINNY_CFWD_NOANSWER (1 << 2)
1073 #define TYPE_TRUNK 1
1076 /* Skinny rtp stream modes. Do we really need this? */
1077 #define SKINNY_CX_SENDONLY 0
1078 #define SKINNY_CX_RECVONLY 1
1079 #define SKINNY_CX_SENDRECV 2
1080 #define SKINNY_CX_CONF 3
1081 #define SKINNY_CX_CONFERENCE 3
1082 #define SKINNY_CX_MUTE 4
1083 #define SKINNY_CX_INACTIVE 4
1086 static char *skinny_cxmodes[] = {
1095 /* driver scheduler */
1096 static struct sched_context *sched = NULL;
1097 static struct io_context *io;
1099 /* Protect the monitoring thread, so only one process can kill or start it, and not
1100 when it's doing something critical. */
1101 AST_MUTEX_DEFINE_STATIC(monlock);
1102 /* Protect the network socket */
1103 AST_MUTEX_DEFINE_STATIC(netlock);
1104 /* Protect the session list */
1105 AST_MUTEX_DEFINE_STATIC(sessionlock);
1106 /* Protect the device list */
1107 AST_MUTEX_DEFINE_STATIC(devicelock);
1109 /* Protect the paging device list */
1110 AST_MUTEX_DEFINE_STATIC(pagingdevicelock);
1113 /* This is the thread for the monitor which checks for input on the channels
1114 which are not currently in use. */
1115 static pthread_t monitor_thread = AST_PTHREADT_NULL;
1117 /* Wait up to 16 seconds for first digit */
1118 static int firstdigittimeout = 16000;
1120 /* How long to wait for following digits */
1121 static int gendigittimeout = 8000;
1123 /* How long to wait for an extra digit, if there is an ambiguous match */
1124 static int matchdigittimeout = 3000;
1126 struct skinny_subchannel {
1128 struct ast_channel *owner;
1129 struct ast_rtp *rtp;
1130 struct ast_rtp *vrtp;
1131 unsigned int callid;
1132 /* time_t lastouttime; */ /* Unused */
1136 /* int lastout; */ /* Unused */
1142 struct skinny_subchannel *next;
1143 struct skinny_line *parent;
1146 struct skinny_line {
1149 char label[24]; /* Label that shows next to the line buttons */
1150 char accountcode[AST_MAX_ACCOUNT_CODE];
1151 char exten[AST_MAX_EXTENSION]; /* Extension where to start */
1152 char context[AST_MAX_CONTEXT];
1153 char language[MAX_LANGUAGE];
1154 char cid_num[AST_MAX_EXTENSION]; /* Caller*ID */
1155 char cid_name[AST_MAX_EXTENSION]; /* Caller*ID */
1156 char lastcallerid[AST_MAX_EXTENSION]; /* Last Caller*ID */
1158 char call_forward_all[AST_MAX_EXTENSION];
1159 char call_forward_busy[AST_MAX_EXTENSION];
1160 char call_forward_noanswer[AST_MAX_EXTENSION];
1161 char mailbox[AST_MAX_EXTENSION];
1162 char vmexten[AST_MAX_EXTENSION];
1163 char regexten[AST_MAX_EXTENSION]; /* Extension for auto-extensions */
1164 char regcontext[AST_MAX_CONTEXT]; /* Context for auto-extensions */
1165 char mohinterpret[MAX_MUSICCLASS];
1166 char mohsuggest[MAX_MUSICCLASS];
1167 char lastnumberdialed[AST_MAX_EXTENSION]; /* Last number that was dialed - used for redial */
1168 int curtone; /* Current tone being played */
1169 ast_group_t callgroup;
1170 ast_group_t pickupgroup;
1173 int threewaycalling;
1178 int dnd; /* How does this affect callwait? Do we just deny a skinny_request if we're dnd? */
1187 int nonCodecCapability;
1189 int msgstate; /* voicemail message state */
1195 struct ast_codec_pref prefs;
1196 struct skinny_subchannel *sub;
1197 struct skinny_line *next;
1198 struct skinny_device *parent;
1199 struct ast_variable *chanvars; /*!< Channel variables to set for inbound call */
1202 struct skinny_speeddial {
1205 char context[AST_MAX_CONTEXT];
1206 char exten[AST_MAX_EXTENSION];
1212 struct skinny_speeddial *next;
1213 struct skinny_device *parent;
1216 struct skinny_addon {
1220 struct skinny_addon *next;
1221 struct skinny_device *parent;
1224 static struct skinny_device {
1225 /* A device containing one or more lines */
1228 char version_id[16];
1229 char exten[AST_MAX_EXTENSION]; /* Cruddy variable name, pick a better one */
1232 int lastlineinstance;
1233 int lastcallreference;
1236 struct sockaddr_in addr;
1237 struct in_addr ourip;
1238 struct skinny_line *lines;
1239 struct skinny_speeddial *speeddials;
1240 struct skinny_addon *addons;
1241 struct ast_codec_pref prefs;
1243 struct skinnysession *session;
1244 struct skinny_device *next;
1247 struct skinny_paging_device {
1250 struct skinny_device ** devices;
1251 struct skinny_paging_device *next;
1254 static struct skinnysession {
1257 struct sockaddr_in sin;
1259 char inbuf[SKINNY_MAX_PACKET];
1260 char outbuf[SKINNY_MAX_PACKET];
1261 struct skinny_device *device;
1262 struct skinnysession *next;
1265 static struct ast_channel *skinny_request(const char *type, int format, void *data, int *cause);
1266 static int skinny_devicestate(void *data);
1267 static int skinny_call(struct ast_channel *ast, char *dest, int timeout);
1268 static int skinny_hangup(struct ast_channel *ast);
1269 static int skinny_answer(struct ast_channel *ast);
1270 static struct ast_frame *skinny_read(struct ast_channel *ast);
1271 static int skinny_write(struct ast_channel *ast, struct ast_frame *frame);
1272 static int skinny_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen);
1273 static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
1274 static int skinny_senddigit_begin(struct ast_channel *ast, char digit);
1275 static int skinny_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
1276 static int handle_time_date_req_message(struct skinny_req *req, struct skinnysession *s);
1278 static const struct ast_channel_tech skinny_tech = {
1280 .description = tdesc,
1281 .capabilities = AST_FORMAT_AUDIO_MASK,
1282 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
1283 .requester = skinny_request,
1284 .devicestate = skinny_devicestate,
1285 .call = skinny_call,
1286 .hangup = skinny_hangup,
1287 .answer = skinny_answer,
1288 .read = skinny_read,
1289 .write = skinny_write,
1290 .indicate = skinny_indicate,
1291 .fixup = skinny_fixup,
1292 .send_digit_begin = skinny_senddigit_begin,
1293 .send_digit_end = skinny_senddigit_end,
1294 .bridge = ast_rtp_bridge,
1297 static int skinny_extensionstate_cb(char *context, char* exten, int state, void *data);
1299 static void *get_button_template(struct skinnysession *s, struct button_definition_template *btn)
1301 struct skinny_device *d = s->device;
1302 struct skinny_addon *a = d->addons;
1306 case SKINNY_DEVICE_30SPPLUS:
1307 case SKINNY_DEVICE_30VIP:
1308 /* 13 rows, 2 columns */
1309 for (i = 0; i < 4; i++)
1310 (btn++)->buttonDefinition = BT_CUST_LINE;
1311 (btn++)->buttonDefinition = BT_REDIAL;
1312 (btn++)->buttonDefinition = BT_VOICEMAIL;
1313 (btn++)->buttonDefinition = BT_CALLPARK;
1314 (btn++)->buttonDefinition = BT_FORWARDALL;
1315 (btn++)->buttonDefinition = BT_CONFERENCE;
1316 for (i = 0; i < 4; i++)
1317 (btn++)->buttonDefinition = BT_NONE;
1318 for (i = 0; i < 13; i++)
1319 (btn++)->buttonDefinition = BT_SPEEDDIAL;
1322 case SKINNY_DEVICE_12SPPLUS:
1323 case SKINNY_DEVICE_12SP:
1324 case SKINNY_DEVICE_12:
1325 /* 6 rows, 2 columns */
1326 for (i = 0; i < 2; i++)
1327 (btn++)->buttonDefinition = BT_CUST_LINE;
1328 for (i = 0; i < 4; i++)
1329 (btn++)->buttonDefinition = BT_SPEEDDIAL;
1330 (btn++)->buttonDefinition = BT_HOLD;
1331 (btn++)->buttonDefinition = BT_REDIAL;
1332 (btn++)->buttonDefinition = BT_TRANSFER;
1333 (btn++)->buttonDefinition = BT_FORWARDALL;
1334 (btn++)->buttonDefinition = BT_CALLPARK;
1335 (btn++)->buttonDefinition = BT_VOICEMAIL;
1337 case SKINNY_DEVICE_7910:
1338 (btn++)->buttonDefinition = BT_LINE;
1339 (btn++)->buttonDefinition = BT_HOLD;
1340 (btn++)->buttonDefinition = BT_TRANSFER;
1341 (btn++)->buttonDefinition = BT_DISPLAY;
1342 (btn++)->buttonDefinition = BT_VOICEMAIL;
1343 (btn++)->buttonDefinition = BT_CONFERENCE;
1344 (btn++)->buttonDefinition = BT_FORWARDALL;
1345 for (i = 0; i < 2; i++)
1346 (btn++)->buttonDefinition = BT_SPEEDDIAL;
1347 (btn++)->buttonDefinition = BT_REDIAL;
1349 case SKINNY_DEVICE_7960:
1350 case SKINNY_DEVICE_7961:
1351 case SKINNY_DEVICE_7961GE:
1352 case SKINNY_DEVICE_7962:
1353 case SKINNY_DEVICE_7965:
1354 for (i = 0; i < 6; i++)
1355 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1357 case SKINNY_DEVICE_7940:
1358 case SKINNY_DEVICE_7941:
1359 case SKINNY_DEVICE_7941GE:
1360 case SKINNY_DEVICE_7942:
1361 case SKINNY_DEVICE_7945:
1362 for (i = 0; i < 2; i++)
1363 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1365 case SKINNY_DEVICE_7935:
1366 case SKINNY_DEVICE_7936:
1367 for (i = 0; i < 2; i++)
1368 (btn++)->buttonDefinition = BT_LINE;
1370 case SKINNY_DEVICE_ATA186:
1371 (btn++)->buttonDefinition = BT_LINE;
1373 case SKINNY_DEVICE_7970:
1374 case SKINNY_DEVICE_7971:
1375 case SKINNY_DEVICE_7975:
1376 case SKINNY_DEVICE_CIPC:
1377 for (i = 0; i < 8; i++)
1378 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1380 case SKINNY_DEVICE_7985:
1381 /* XXX I have no idea what the buttons look like on these. */
1382 ast_log(LOG_WARNING, "Unsupported device type '%d (7985)' found.\n", d->type);
1384 case SKINNY_DEVICE_7912:
1385 case SKINNY_DEVICE_7911:
1386 case SKINNY_DEVICE_7905:
1387 (btn++)->buttonDefinition = BT_LINE;
1388 (btn++)->buttonDefinition = BT_HOLD;
1390 case SKINNY_DEVICE_7920:
1391 /* XXX I don't know if this is right. */
1392 for (i = 0; i < 4; i++)
1393 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1395 case SKINNY_DEVICE_7921:
1396 for (i = 0; i < 6; i++)
1397 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1399 case SKINNY_DEVICE_7902:
1400 ast_log(LOG_WARNING, "Unsupported device type '%d (7902)' found.\n", d->type);
1402 case SKINNY_DEVICE_7906:
1403 ast_log(LOG_WARNING, "Unsupported device type '%d (7906)' found.\n", d->type);
1405 case SKINNY_DEVICE_7931:
1406 ast_log(LOG_WARNING, "Unsupported device type '%d (7931)' found.\n", d->type);
1408 case SKINNY_DEVICE_7937:
1409 ast_log(LOG_WARNING, "Unsupported device type '%d (7937)' found.\n", d->type);
1411 case SKINNY_DEVICE_SCCPGATEWAY_AN:
1412 case SKINNY_DEVICE_SCCPGATEWAY_BRI:
1413 ast_log(LOG_WARNING, "Unsupported device type '%d (SCCP gateway)' found.\n", d->type);
1416 ast_log(LOG_WARNING, "Unknown device type '%d' found.\n", d->type);
1420 for (a = d->addons; a; a = a->next) {
1421 if (!strcasecmp(a->type, "7914")) {
1422 for (i = 0; i < 14; i++)
1423 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1425 ast_log(LOG_WARNING, "Unknown addon type '%s' found. Skipping.\n", a->type);
1432 static struct skinny_req *req_alloc(size_t size, int response_message)
1434 struct skinny_req *req;
1436 if (!(req = ast_calloc(1, skinny_header_size + size + 4)))
1439 req->len = htolel(size+4);
1440 req->e = htolel(response_message);
1445 static struct skinny_line *find_line_by_instance(struct skinny_device *d, int instance)
1447 struct skinny_line *l;
1449 /*Dialing from on hook or on a 7920 uses instance 0 in requests
1450 but we need to start looking at instance 1 */
1455 for (l = d->lines; l; l = l->next) {
1456 if (l->instance == instance)
1461 ast_log(LOG_WARNING, "Could not find line with instance '%d' on device '%s'\n", instance, d->name);
1466 static struct skinny_line *find_line_by_name(const char *dest)
1468 struct skinny_line *l;
1469 struct skinny_line *tmpl = NULL;
1470 struct skinny_device *d;
1474 int checkdevice = 0;
1476 ast_copy_string(line, dest, sizeof(line));
1477 at = strchr(line, '@');
1482 if (!ast_strlen_zero(device))
1485 ast_mutex_lock(&devicelock);
1486 for (d = devices; d; d = d->next) {
1487 if (checkdevice && tmpl)
1489 else if (!checkdevice) {
1490 /* This is a match, since we're checking for line on every device. */
1491 } else if (!strcasecmp(d->name, device)) {
1493 ast_verb(2, "Found device: %s\n", d->name);
1497 /* Found the device (or we don't care which device) */
1498 for (l = d->lines; l; l = l->next) {
1499 /* Search for the right line */
1500 if (!strcasecmp(l->name, line)) {
1502 ast_verb(2, "Ambiguous line name: %s\n", line);
1503 ast_mutex_unlock(&devicelock);
1510 ast_mutex_unlock(&devicelock);
1515 * implement the setvar config line
1517 static struct ast_variable *add_var(const char *buf, struct ast_variable *list)
1519 struct ast_variable *tmpvar = NULL;
1520 char *varname = ast_strdupa(buf), *varval = NULL;
1522 if ((varval = strchr(varname,'='))) {
1524 if ((tmpvar = ast_variable_new(varname, varval, ""))) {
1525 tmpvar->next = list;
1532 /* It's quicker/easier to find the subchannel when we know the instance number too */
1533 static struct skinny_subchannel *find_subchannel_by_instance_reference(struct skinny_device *d, int instance, int reference)
1535 struct skinny_line *l = find_line_by_instance(d, instance);
1536 struct skinny_subchannel *sub;
1542 /* 7920 phones set call reference to 0, so use the first
1543 sub-channel on the list.
1544 This MIGHT need more love to be right */
1548 for (sub = l->sub; sub; sub = sub->next) {
1549 if (sub->callid == reference)
1554 ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s'\n", reference, d->name);
1559 /* Find the subchannel when we only have the callid - this shouldn't happen often */
1560 static struct skinny_subchannel *find_subchannel_by_reference(struct skinny_device *d, int reference)
1562 struct skinny_line *l;
1563 struct skinny_subchannel *sub = NULL;
1565 for (l = d->lines; l; l = l->next) {
1566 for (sub = l->sub; sub; sub = sub->next) {
1567 if (sub->callid == reference)
1575 ast_log(LOG_WARNING, "Could not find any lines that contained a subchannel with reference '%d' on device '%s'\n", reference, d->name);
1578 ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s@%s'\n", reference, l->name, d->name);
1584 static struct skinny_speeddial *find_speeddial_by_instance(struct skinny_device *d, int instance, int isHint)
1586 struct skinny_speeddial *sd;
1588 for (sd = d->speeddials; sd; sd = sd->next) {
1589 if (sd->isHint == isHint && sd->instance == instance)
1594 ast_log(LOG_WARNING, "Could not find speeddial with instance '%d' on device '%s'\n", instance, d->name);
1599 static int codec_skinny2ast(enum skinny_codecs skinnycodec)
1601 switch (skinnycodec) {
1602 case SKINNY_CODEC_ALAW:
1603 return AST_FORMAT_ALAW;
1604 case SKINNY_CODEC_ULAW:
1605 return AST_FORMAT_ULAW;
1606 case SKINNY_CODEC_G723_1:
1607 return AST_FORMAT_G723_1;
1608 case SKINNY_CODEC_G729A:
1609 return AST_FORMAT_G729A;
1610 case SKINNY_CODEC_G726_32:
1611 return AST_FORMAT_G726_AAL2; /* XXX Is this right? */
1612 case SKINNY_CODEC_H261:
1613 return AST_FORMAT_H261;
1614 case SKINNY_CODEC_H263:
1615 return AST_FORMAT_H263;
1621 static int codec_ast2skinny(int astcodec)
1624 case AST_FORMAT_ALAW:
1625 return SKINNY_CODEC_ALAW;
1626 case AST_FORMAT_ULAW:
1627 return SKINNY_CODEC_ULAW;
1628 case AST_FORMAT_G723_1:
1629 return SKINNY_CODEC_G723_1;
1630 case AST_FORMAT_G729A:
1631 return SKINNY_CODEC_G729A;
1632 case AST_FORMAT_G726_AAL2: /* XXX Is this right? */
1633 return SKINNY_CODEC_G726_32;
1634 case AST_FORMAT_H261:
1635 return SKINNY_CODEC_H261;
1636 case AST_FORMAT_H263:
1637 return SKINNY_CODEC_H263;
1643 static int set_callforwards(struct skinny_line *l, const char *cfwd, int cfwdtype)
1648 if (!ast_strlen_zero(cfwd)) {
1649 if (cfwdtype & SKINNY_CFWD_ALL) {
1650 l->cfwdtype |= SKINNY_CFWD_ALL;
1651 ast_copy_string(l->call_forward_all, cfwd, sizeof(l->call_forward_all));
1653 if (cfwdtype & SKINNY_CFWD_BUSY) {
1654 l->cfwdtype |= SKINNY_CFWD_BUSY;
1655 ast_copy_string(l->call_forward_busy, cfwd, sizeof(l->call_forward_busy));
1657 if (cfwdtype & SKINNY_CFWD_NOANSWER) {
1658 l->cfwdtype |= SKINNY_CFWD_NOANSWER;
1659 ast_copy_string(l->call_forward_noanswer, cfwd, sizeof(l->call_forward_noanswer));
1662 if (cfwdtype & SKINNY_CFWD_ALL) {
1663 l->cfwdtype &= ~SKINNY_CFWD_ALL;
1664 memset(l->call_forward_all, 0, sizeof(l->call_forward_all));
1666 if (cfwdtype & SKINNY_CFWD_BUSY) {
1667 l->cfwdtype &= ~SKINNY_CFWD_BUSY;
1668 memset(l->call_forward_busy, 0, sizeof(l->call_forward_busy));
1670 if (cfwdtype & SKINNY_CFWD_NOANSWER) {
1671 l->cfwdtype &= ~SKINNY_CFWD_NOANSWER;
1672 memset(l->call_forward_noanswer, 0, sizeof(l->call_forward_noanswer));
1678 static void cleanup_stale_contexts(char *new, char *old)
1680 char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
1682 while ((oldcontext = strsep(&old, "&"))) {
1683 stalecontext = '\0';
1684 ast_copy_string(newlist, new, sizeof(newlist));
1686 while ((newcontext = strsep(&stringp, "&"))) {
1687 if (strcmp(newcontext, oldcontext) == 0) {
1688 /* This is not the context you're looking for */
1689 stalecontext = '\0';
1691 } else if (strcmp(newcontext, oldcontext)) {
1692 stalecontext = oldcontext;
1697 ast_context_destroy(ast_context_find(stalecontext), "Skinny");
1701 static void register_exten(struct skinny_line *l)
1704 char *stringp, *ext, *context;
1706 if (ast_strlen_zero(regcontext))
1709 ast_copy_string(multi, S_OR(l->regexten, l->name), sizeof(multi));
1711 while ((ext = strsep(&stringp, "&"))) {
1712 if ((context = strchr(ext, '@'))) {
1713 *context++ = '\0'; /* split ext@context */
1714 if (!ast_context_find(context)) {
1715 ast_log(LOG_WARNING, "Context %s must exist in regcontext= in skinny.conf!\n", context);
1719 context = regcontext;
1721 ast_add_extension(context, 1, ext, 1, NULL, NULL, "Noop",
1722 ast_strdup(l->name), ast_free_ptr, "Skinny");
1726 static void unregister_exten(struct skinny_line *l)
1729 char *stringp, *ext, *context;
1731 if (ast_strlen_zero(regcontext))
1734 ast_copy_string(multi, S_OR(l->regexten, l->name), sizeof(multi));
1736 while ((ext = strsep(&stringp, "&"))) {
1737 if ((context = strchr(ext, '@'))) {
1738 *context++ = '\0'; /* split ext@context */
1739 if (!ast_context_find(context)) {
1740 ast_log(LOG_WARNING, "Context %s must exist in regcontext= in skinny.conf!\n", context);
1744 context = regcontext;
1746 ast_context_remove_extension(context, ext, 1, NULL);
1750 static int skinny_register(struct skinny_req *req, struct skinnysession *s)
1752 struct skinny_device *d;
1753 struct skinny_line *l;
1754 struct skinny_speeddial *sd;
1755 struct sockaddr_in sin;
1758 ast_mutex_lock(&devicelock);
1759 for (d = devices; d; d = d->next) {
1760 if (!strcasecmp(req->data.reg.name, d->id)
1761 && ast_apply_ha(d->ha, &(s->sin))) {
1763 d->type = letohl(req->data.reg.type);
1764 if (ast_strlen_zero(d->version_id)) {
1765 ast_copy_string(d->version_id, version_id, sizeof(d->version_id));
1771 if (getsockname(s->fd, (struct sockaddr *)&sin, &slen)) {
1772 ast_log(LOG_WARNING, "Cannot get socket name\n");
1773 sin.sin_addr = __ourip;
1775 d->ourip = sin.sin_addr;
1777 for (sd = d->speeddials; sd; sd = sd->next) {
1778 sd->stateid = ast_extension_state_add(sd->context, sd->exten, skinny_extensionstate_cb, sd);
1780 for (l = d->lines; l; l = l->next) {
1782 ast_device_state_changed("Skinny/%s@%s", l->name, d->name);
1787 ast_mutex_unlock(&devicelock);
1794 static int skinny_unregister(struct skinny_req *req, struct skinnysession *s)
1796 struct skinny_device *d;
1797 struct skinny_line *l;
1798 struct skinny_speeddial *sd;
1806 for (sd = d->speeddials; sd; sd = sd->next) {
1807 if (sd->stateid > -1)
1808 ast_extension_state_del(sd->stateid, NULL);
1810 for (l = d->lines; l; l = l->next) {
1811 unregister_exten(l);
1812 ast_device_state_changed("Skinny/%s@%s", l->name, d->name);
1816 return -1; /* main loop will destroy the session */
1819 static int transmit_response(struct skinnysession *s, struct skinny_req *req)
1824 ast_log(LOG_WARNING, "Asked to transmit to a non-existant session!\n");
1828 ast_mutex_lock(&s->lock);
1831 ast_log(LOG_VERBOSE, "writing packet type %04X (%d bytes) to socket %d\n", letohl(req->e), letohl(req->len)+8, s->fd);
1833 if (letohl(req->len > SKINNY_MAX_PACKET) || letohl(req->len < 0)) {
1834 ast_log(LOG_WARNING, "transmit_response: the length of the request is out of bounds\n");
1838 memset(s->outbuf,0,sizeof(s->outbuf));
1839 memcpy(s->outbuf, req, skinny_header_size);
1840 memcpy(s->outbuf+skinny_header_size, &req->data, letohl(req->len));
1842 res = write(s->fd, s->outbuf, letohl(req->len)+8);
1844 if (res != letohl(req->len)+8) {
1845 ast_log(LOG_WARNING, "Transmit: write only sent %d out of %d bytes: %s\n", res, letohl(req->len)+8, strerror(errno));
1848 ast_log(LOG_WARNING, "Transmit: Skinny Client was lost, unregistering\n");
1849 skinny_unregister(NULL, s);
1854 ast_mutex_unlock(&s->lock);
1858 static void transmit_speaker_mode(struct skinnysession *s, int mode)
1860 struct skinny_req *req;
1862 if (!(req = req_alloc(sizeof(struct set_speaker_message), SET_SPEAKER_MESSAGE)))
1865 req->data.setspeaker.mode = htolel(mode);
1866 transmit_response(s, req);
1869 static void transmit_microphone_mode(struct skinnysession *s, int mode)
1871 struct skinny_req *req;
1873 if (!(req = req_alloc(sizeof(struct set_microphone_message), SET_MICROPHONE_MESSAGE)))
1876 req->data.setmicrophone.mode = htolel(mode);
1877 transmit_response(s, req);
1881 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)
1883 struct skinny_req *req;
1885 if (!(req = req_alloc(sizeof(struct call_info_message), CALL_INFO_MESSAGE)))
1889 ast_debug(1, "Setting Callinfo to %s(%s) from %s(%s) on %s(%d)\n", fromname, fromnum, toname, tonum, s->device->name, instance);
1892 ast_copy_string(req->data.callinfo.callingPartyName, fromname, sizeof(req->data.callinfo.callingPartyName));
1895 ast_copy_string(req->data.callinfo.callingParty, fromnum, sizeof(req->data.callinfo.callingParty));
1898 ast_copy_string(req->data.callinfo.calledPartyName, toname, sizeof(req->data.callinfo.calledPartyName));
1901 ast_copy_string(req->data.callinfo.calledParty, tonum, sizeof(req->data.callinfo.calledParty));
1903 req->data.callinfo.instance = htolel(instance);
1904 req->data.callinfo.reference = htolel(callid);
1905 req->data.callinfo.type = htolel(calltype);
1906 transmit_response(s, req);
1909 static void transmit_connect(struct skinnysession *s, struct skinny_subchannel *sub)
1911 struct skinny_req *req;
1912 struct skinny_line *l = sub->parent;
1913 struct ast_format_list fmt;
1915 if (!(req = req_alloc(sizeof(struct open_receive_channel_message), OPEN_RECEIVE_CHANNEL_MESSAGE)))
1918 fmt = ast_codec_pref_getsize(&l->prefs, ast_best_codec(l->capability));
1920 req->data.openreceivechannel.conferenceId = htolel(sub->callid);
1921 req->data.openreceivechannel.partyId = htolel(sub->callid);
1922 req->data.openreceivechannel.packets = htolel(fmt.cur_ms);
1923 req->data.openreceivechannel.capability = htolel(codec_ast2skinny(fmt.bits));
1924 req->data.openreceivechannel.echo = htolel(0);
1925 req->data.openreceivechannel.bitrate = htolel(0);
1926 transmit_response(s, req);
1929 static void transmit_tone(struct skinnysession *s, int tone, int instance, int reference)
1931 struct skinny_req *req;
1933 if (tone == SKINNY_NOTONE) {
1934 /* This is bad, mmm'kay? */
1939 if (!(req = req_alloc(sizeof(struct start_tone_message), START_TONE_MESSAGE)))
1941 req->data.starttone.tone = htolel(tone);
1942 req->data.starttone.instance = htolel(instance);
1943 req->data.starttone.reference = htolel(reference);
1945 if (!(req = req_alloc(sizeof(struct stop_tone_message), STOP_TONE_MESSAGE)))
1947 req->data.stoptone.instance = htolel(instance);
1948 req->data.stoptone.reference = htolel(reference);
1952 req->data.starttone.tone = htolel(tone);
1954 transmit_response(s, req);
1957 static void transmit_selectsoftkeys(struct skinnysession *s, int instance, int callid, int softkey)
1959 struct skinny_req *req;
1961 if (!(req = req_alloc(sizeof(struct select_soft_keys_message), SELECT_SOFT_KEYS_MESSAGE)))
1964 req->data.selectsoftkey.instance = htolel(instance);
1965 req->data.selectsoftkey.reference = htolel(callid);
1966 req->data.selectsoftkey.softKeySetIndex = htolel(softkey);
1967 req->data.selectsoftkey.validKeyMask = htolel(0xFFFFFFFF);
1968 transmit_response(s, req);
1971 static void transmit_lamp_indication(struct skinnysession *s, int stimulus, int instance, int indication)
1973 struct skinny_req *req;
1975 if (!(req = req_alloc(sizeof(struct set_lamp_message), SET_LAMP_MESSAGE)))
1978 req->data.setlamp.stimulus = htolel(stimulus);
1979 req->data.setlamp.stimulusInstance = htolel(instance);
1980 req->data.setlamp.deviceStimulus = htolel(indication);
1981 transmit_response(s, req);
1984 static void transmit_ringer_mode(struct skinnysession *s, int mode)
1986 struct skinny_req *req;
1989 ast_debug(1, "Setting ringer mode to '%d'.\n", mode);
1991 if (!(req = req_alloc(sizeof(struct set_ringer_message), SET_RINGER_MESSAGE)))
1994 req->data.setringer.ringerMode = htolel(mode);
1995 /* XXX okay, I don't quite know what this is, but here's what happens (on a 7960).
1996 Note: The phone will always show as ringing on the display.
1998 1: phone will audibly ring over and over
1999 2: phone will audibly ring only once
2000 any other value, will NOT cause the phone to audibly ring
2002 req->data.setringer.unknown1 = htolel(1);
2003 /* XXX the value here doesn't seem to change anything. Must be higher than 0.
2004 Perhaps a packet capture can shed some light on this. */
2005 req->data.setringer.unknown2 = htolel(1);
2006 transmit_response(s, req);
2009 static void transmit_displaymessage(struct skinnysession *s, const char *text, int instance, int reference)
2011 struct skinny_req *req;
2014 if (!(req = req_alloc(0, CLEAR_DISPLAY_MESSAGE)))
2017 req->data.clearpromptstatus.lineInstance = instance;
2018 req->data.clearpromptstatus.callReference = reference;
2021 ast_debug(1, "Clearing Display\n");
2023 if (!(req = req_alloc(sizeof(struct displaytext_message), DISPLAYTEXT_MESSAGE)))
2026 ast_copy_string(req->data.displaytext.text, text, sizeof(req->data.displaytext.text));
2028 ast_debug(1, "Displaying message '%s'\n", req->data.displaytext.text);
2031 transmit_response(s, req);
2034 static void transmit_displaynotify(struct skinnysession *s, const char *text, int t)
2036 struct skinny_req *req;
2038 if (!(req = req_alloc(sizeof(struct display_notify_message), DISPLAY_NOTIFY_MESSAGE)))
2041 ast_copy_string(req->data.displaynotify.displayMessage, text, sizeof(req->data.displaynotify.displayMessage));
2042 req->data.displaynotify.displayTimeout = htolel(t);
2045 ast_debug(1, "Displaying notify '%s'\n", text);
2047 transmit_response(s, req);
2050 static void transmit_displaypromptstatus(struct skinnysession *s, const char *text, int t, int instance, int callid)
2052 struct skinny_req *req;
2055 if (!(req = req_alloc(sizeof(struct clear_prompt_message), CLEAR_PROMPT_MESSAGE)))
2058 req->data.clearpromptstatus.lineInstance = htolel(instance);
2059 req->data.clearpromptstatus.callReference = htolel(callid);
2062 ast_debug(1, "Clearing Prompt\n");
2064 if (!(req = req_alloc(sizeof(struct display_prompt_status_message), DISPLAY_PROMPT_STATUS_MESSAGE)))
2067 ast_copy_string(req->data.displaypromptstatus.promptMessage, text, sizeof(req->data.displaypromptstatus.promptMessage));
2068 req->data.displaypromptstatus.messageTimeout = htolel(t);
2069 req->data.displaypromptstatus.lineInstance = htolel(instance);
2070 req->data.displaypromptstatus.callReference = htolel(callid);
2073 ast_debug(1, "Displaying Prompt Status '%s'\n", text);
2076 transmit_response(s, req);
2079 static void transmit_dialednumber(struct skinnysession *s, const char *text, int instance, int callid)
2081 struct skinny_req *req;
2083 if (!(req = req_alloc(sizeof(struct dialed_number_message), DIALED_NUMBER_MESSAGE)))
2086 ast_copy_string(req->data.dialednumber.dialedNumber, text, sizeof(req->data.dialednumber.dialedNumber));
2087 req->data.dialednumber.lineInstance = htolel(instance);
2088 req->data.dialednumber.callReference = htolel(callid);
2090 transmit_response(s, req);
2093 static void transmit_callstate(struct skinnysession *s, int instance, int state, unsigned callid)
2095 struct skinny_req *req;
2097 if (state == SKINNY_ONHOOK) {
2098 if (!(req = req_alloc(sizeof(struct close_receive_channel_message), CLOSE_RECEIVE_CHANNEL_MESSAGE)))
2101 req->data.closereceivechannel.conferenceId = htolel(callid);
2102 req->data.closereceivechannel.partyId = htolel(callid);
2103 transmit_response(s, req);
2105 if (!(req = req_alloc(sizeof(struct stop_media_transmission_message), STOP_MEDIA_TRANSMISSION_MESSAGE)))
2108 req->data.stopmedia.conferenceId = htolel(callid);
2109 req->data.stopmedia.passThruPartyId = htolel(callid);
2110 transmit_response(s, req);
2112 transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
2114 transmit_displaypromptstatus(s, NULL, 0, instance, callid);
2117 if (!(req = req_alloc(sizeof(struct call_state_message), CALL_STATE_MESSAGE)))
2120 req->data.callstate.callState = htolel(state);
2121 req->data.callstate.lineInstance = htolel(instance);
2122 req->data.callstate.callReference = htolel(callid);
2123 transmit_response(s, req);
2125 if (state == SKINNY_ONHOOK) {
2126 transmit_selectsoftkeys(s, 0, 0, KEYDEF_ONHOOK);
2129 if (state == SKINNY_OFFHOOK || state == SKINNY_ONHOOK) {
2130 if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
2133 req->data.activatecallplane.lineInstance = htolel(instance);
2134 transmit_response(s, req);
2139 static void transmit_cfwdstate(struct skinnysession *s, struct skinny_line *l)
2141 struct skinny_req *req;
2144 if (!(req = req_alloc(sizeof(struct forward_stat_message), FORWARD_STAT_MESSAGE)))
2147 if (l->cfwdtype & SKINNY_CFWD_ALL) {
2148 if (!ast_strlen_zero(l->call_forward_all)) {
2149 ast_copy_string(req->data.forwardstat.fwdallnum, l->call_forward_all, sizeof(req->data.forwardstat.fwdallnum));
2150 req->data.forwardstat.fwdall = htolel(1);
2153 req->data.forwardstat.fwdall = htolel(0);
2156 if (l->cfwdtype & SKINNY_CFWD_BUSY) {
2157 if (!ast_strlen_zero(l->call_forward_busy)) {
2158 ast_copy_string(req->data.forwardstat.fwdbusynum, l->call_forward_busy, sizeof(req->data.forwardstat.fwdbusynum));
2159 req->data.forwardstat.fwdbusy = htolel(1);
2162 req->data.forwardstat.fwdbusy = htolel(0);
2165 if (l->cfwdtype & SKINNY_CFWD_NOANSWER) {
2166 if (!ast_strlen_zero(l->call_forward_noanswer)) {
2167 ast_copy_string(req->data.forwardstat.fwdnoanswernum, l->call_forward_noanswer, sizeof(req->data.forwardstat.fwdnoanswernum));
2168 req->data.forwardstat.fwdnoanswer = htolel(1);
2171 req->data.forwardstat.fwdnoanswer = htolel(0);
2174 req->data.forwardstat.lineNumber = htolel(l->instance);
2176 req->data.forwardstat.activeforward = htolel(7);
2178 req->data.forwardstat.activeforward = htolel(0);
2180 transmit_response(s, req);
2183 static int skinny_extensionstate_cb(char *context, char *exten, int state, void *data)
2185 struct skinny_speeddial *sd = data;
2186 struct skinny_device *d = sd->parent;
2187 struct skinnysession *s = d->session;
2188 char hint[AST_MAX_EXTENSION];
2189 int callstate = SKINNY_CALLREMOTEMULTILINE;
2190 int lamp = SKINNY_LAMP_OFF;
2193 case AST_EXTENSION_DEACTIVATED: /* Retry after a while */
2194 case AST_EXTENSION_REMOVED: /* Extension is gone */
2195 ast_verb(2, "Extension state: Watcher for hint %s %s. Notify Device %s\n", exten, state == AST_EXTENSION_DEACTIVATED ? "deactivated" : "removed", d->name);
2197 callstate = SKINNY_ONHOOK;
2198 lamp = SKINNY_LAMP_OFF;
2200 case AST_EXTENSION_RINGING:
2201 case AST_EXTENSION_UNAVAILABLE:
2202 callstate = SKINNY_RINGIN;
2203 lamp = SKINNY_LAMP_BLINK;
2205 case AST_EXTENSION_BUSY: /* callstate = SKINNY_BUSY wasn't wanting to work - I'll settle for this */
2206 case AST_EXTENSION_INUSE:
2207 callstate = SKINNY_CALLREMOTEMULTILINE;
2208 lamp = SKINNY_LAMP_ON;
2210 case AST_EXTENSION_ONHOLD:
2211 callstate = SKINNY_HOLD;
2212 lamp = SKINNY_LAMP_WINK;
2214 case AST_EXTENSION_NOT_INUSE:
2216 callstate = SKINNY_ONHOOK;
2217 lamp = SKINNY_LAMP_OFF;
2221 if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, sd->context, sd->exten)) {
2222 /* If they are not registered, we will override notification and show no availability */
2223 if (ast_device_state(hint) == AST_DEVICE_UNAVAILABLE) {
2224 callstate = SKINNY_ONHOOK;
2225 lamp = SKINNY_LAMP_FLASH;
2229 transmit_lamp_indication(s, STIMULUS_LINE, sd->instance, lamp);
2230 transmit_callstate(s, sd->instance, callstate, 0);
2231 sd->laststate = state;
2236 static int has_voicemail(struct skinny_line *l)
2238 return ast_app_has_voicemail(l->mailbox, NULL);
2241 static void do_housekeeping(struct skinnysession *s)
2245 int device_lamp = 0;
2246 struct skinny_device *d = s->device;
2247 struct skinny_line *l;
2249 /* Update time on device */
2250 handle_time_date_req_message(NULL, s);
2252 /* Set MWI on individual lines */
2253 for (l = d->lines; l; l = l->next) {
2254 if (has_voicemail(l)) {
2256 ast_debug(1, "Checking for voicemail Skinny %s@%s\n", l->name, d->name);
2257 ast_app_inboxcount(l->mailbox, &new, &old);
2259 ast_debug(1, "Skinny %s@%s has voicemail!\n", l->name, d->name);
2260 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, l->instance, l->mwiblink?SKINNY_LAMP_BLINK:SKINNY_LAMP_ON);
2263 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, l->instance, SKINNY_LAMP_OFF);
2266 /* If at least one line has VM, turn the device level lamp on */
2268 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, 0, SKINNY_LAMP_ON);
2270 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, 0, SKINNY_LAMP_OFF);
2273 /* I do not believe skinny can deal with video.
2274 Anyone know differently? */
2275 /* Yes, it can. Currently 7985 and Cisco VT Advantage do video. */
2276 static enum ast_rtp_get_result skinny_get_vrtp_peer(struct ast_channel *c, struct ast_rtp **rtp)
2278 struct skinny_subchannel *sub = NULL;
2280 if (!(sub = c->tech_pvt) || !(sub->vrtp))
2281 return AST_RTP_GET_FAILED;
2285 return AST_RTP_TRY_NATIVE;
2288 static enum ast_rtp_get_result skinny_get_rtp_peer(struct ast_channel *c, struct ast_rtp **rtp)
2290 struct skinny_subchannel *sub = NULL;
2291 struct skinny_line *l;
2292 enum ast_rtp_get_result res = AST_RTP_TRY_NATIVE;
2295 ast_debug(1, "skinny_get_rtp_peer() Channel = %s\n", c->name);
2298 if (!(sub = c->tech_pvt))
2299 return AST_RTP_GET_FAILED;
2301 ast_mutex_lock(&sub->lock);
2304 ast_mutex_unlock(&sub->lock);
2305 return AST_RTP_GET_FAILED;
2312 if (!l->canreinvite || l->nat){
2313 res = AST_RTP_TRY_PARTIAL;
2315 ast_debug(1, "skinny_get_rtp_peer() Using AST_RTP_TRY_PARTIAL \n");
2318 ast_mutex_unlock(&sub->lock);
2324 static int skinny_set_rtp_peer(struct ast_channel *c, struct ast_rtp *rtp, struct ast_rtp *vrtp, struct ast_rtp *trtp, int codecs, int nat_active)
2326 struct skinny_subchannel *sub;
2327 struct skinny_line *l;
2328 struct skinny_device *d;
2329 struct skinnysession *s;
2330 struct ast_format_list fmt;
2331 struct sockaddr_in us;
2332 struct sockaddr_in them;
2333 struct skinny_req *req;
2337 if (c->_state != AST_STATE_UP)
2349 ast_rtp_get_peer(rtp, &them);
2351 /* Shutdown any early-media or previous media on re-invite */
2352 if (!(req = req_alloc(sizeof(struct stop_media_transmission_message), STOP_MEDIA_TRANSMISSION_MESSAGE)))
2355 req->data.stopmedia.conferenceId = htolel(sub->callid);
2356 req->data.stopmedia.passThruPartyId = htolel(sub->callid);
2357 transmit_response(s, req);
2360 ast_debug(1, "Peerip = %s:%d\n", ast_inet_ntoa(them.sin_addr), ntohs(them.sin_port));
2362 if (!(req = req_alloc(sizeof(struct start_media_transmission_message), START_MEDIA_TRANSMISSION_MESSAGE)))
2365 fmt = ast_codec_pref_getsize(&l->prefs, ast_best_codec(l->capability));
2368 ast_debug(1, "Setting payloadType to '%d' (%d ms)\n", fmt.bits, fmt.cur_ms);
2370 req->data.startmedia.conferenceId = htolel(sub->callid);
2371 req->data.startmedia.passThruPartyId = htolel(sub->callid);
2372 if (!(l->canreinvite) || (l->nat)){
2373 ast_rtp_get_us(rtp, &us);
2374 req->data.startmedia.remoteIp = htolel(d->ourip.s_addr);
2375 req->data.startmedia.remotePort = htolel(ntohs(us.sin_port));
2377 req->data.startmedia.remoteIp = htolel(them.sin_addr.s_addr);
2378 req->data.startmedia.remotePort = htolel(ntohs(them.sin_port));
2380 req->data.startmedia.packetSize = htolel(fmt.cur_ms);
2381 req->data.startmedia.payloadType = htolel(codec_ast2skinny(fmt.bits));
2382 req->data.startmedia.qualifier.precedence = htolel(127);
2383 req->data.startmedia.qualifier.vad = htolel(0);
2384 req->data.startmedia.qualifier.packets = htolel(0);
2385 req->data.startmedia.qualifier.bitRate = htolel(0);
2386 transmit_response(s, req);
2390 /* Need a return here to break the bridge */
2394 static struct ast_rtp_protocol skinny_rtp = {
2396 .get_rtp_info = skinny_get_rtp_peer,
2397 .get_vrtp_info = skinny_get_vrtp_peer,
2398 .set_rtp_peer = skinny_set_rtp_peer,
2401 static char *handle_skinny_set_debug_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2405 e->command = "skinny set debug [off]";
2407 "Usage: skinny set debug [off]\n"
2408 " Enables/Disables dumping of Skinny packets for debugging purposes\n";
2414 if (a->argc < 3 || a->argc > 4)
2415 return CLI_SHOWUSAGE;
2419 ast_cli(a->fd, "Skinny Debugging Enabled\n");
2421 } else if (!strncasecmp(a->argv[3], "off", 3)) {
2423 ast_cli(a->fd, "Skinny Debugging Disabled\n");
2426 return CLI_SHOWUSAGE;
2430 static char *handle_skinny_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2434 e->command = "skinny set debug {on|off}";
2436 "Usage: skinny set debug {on|off}\n"
2437 " Enables/Disables dumping of Skinny packets for debugging purposes\n";
2443 if (a->argc != e->args)
2444 return CLI_SHOWUSAGE;
2446 if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
2448 ast_cli(a->fd, "Skinny Debugging Enabled\n");
2450 } else if (!strncasecmp(a->argv[e->args - 1], "off", 3)) {
2452 ast_cli(a->fd, "Skinny Debugging Disabled\n");
2455 return CLI_SHOWUSAGE;
2459 static char *complete_skinny_devices(const char *word, int state)
2461 struct skinny_device *d;
2462 char *result = NULL;
2463 int wordlen = strlen(word), which = 0;
2465 for (d = devices; d && !result; d = d->next) {
2466 if (!strncasecmp(word, d->id, wordlen) && ++which > state)
2467 result = ast_strdup(d->id);
2473 static char *complete_skinny_show_device(const char *line, const char *word, int pos, int state)
2475 return (pos == 3 ? ast_strdup(complete_skinny_devices(word, state)) : NULL);
2478 static char *complete_skinny_reset(const char *line, const char *word, int pos, int state)
2480 return (pos == 2 ? ast_strdup(complete_skinny_devices(word, state)) : NULL);
2483 static char *complete_skinny_show_line(const char *line, const char *word, int pos, int state)
2485 struct skinny_device *d;
2486 struct skinny_line *l;
2487 char *result = NULL;
2488 int wordlen = strlen(word), which = 0;
2493 for (d = devices; d && !result; d = d->next) {
2494 for (l = d->lines; l && !result; l = l->next) {
2495 if (!strncasecmp(word, l->name, wordlen) && ++which > state)
2496 result = ast_strdup(l->name);
2503 static char *handle_skinny_reset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2505 struct skinny_device *d;
2506 struct skinny_req *req;
2510 e->command = "skinny reset";
2512 "Usage: skinny reset <DeviceId|DeviceName|all> [restart]\n"
2513 " Causes a Skinny device to reset itself, optionally with a full restart\n";
2516 return complete_skinny_reset(a->line, a->word, a->pos, a->n);
2519 if (a->argc < 3 || a->argc > 4)
2520 return CLI_SHOWUSAGE;
2522 ast_mutex_lock(&devicelock);
2524 for (d = devices; d; d = d->next) {
2525 int fullrestart = 0;
2526 if (!strcasecmp(a->argv[2], d->id) || !strcasecmp(a->argv[2], d->name) || !strcasecmp(a->argv[2], "all")) {
2530 if (!(req = req_alloc(sizeof(struct reset_message), RESET_MESSAGE)))
2533 if (a->argc == 4 && !strcasecmp(a->argv[3], "restart"))
2537 req->data.reset.resetType = 2;
2539 req->data.reset.resetType = 1;
2541 ast_verb(3, "%s device %s.\n", (fullrestart) ? "Restarting" : "Resetting", d->id);
2542 transmit_response(d->session, req);
2545 ast_mutex_unlock(&devicelock);
2549 static char *device2str(int type)
2554 case SKINNY_DEVICE_NONE:
2556 case SKINNY_DEVICE_30SPPLUS:
2558 case SKINNY_DEVICE_12SPPLUS:
2560 case SKINNY_DEVICE_12SP:
2562 case SKINNY_DEVICE_12:
2564 case SKINNY_DEVICE_30VIP:
2566 case SKINNY_DEVICE_7910:
2568 case SKINNY_DEVICE_7960:
2570 case SKINNY_DEVICE_7940:
2572 case SKINNY_DEVICE_7935:
2574 case SKINNY_DEVICE_ATA186:
2576 case SKINNY_DEVICE_7941:
2578 case SKINNY_DEVICE_7971:
2580 case SKINNY_DEVICE_7985:
2582 case SKINNY_DEVICE_7911:
2584 case SKINNY_DEVICE_7961GE:
2586 case SKINNY_DEVICE_7941GE:
2588 case SKINNY_DEVICE_7921:
2590 case SKINNY_DEVICE_7905:
2592 case SKINNY_DEVICE_7920:
2594 case SKINNY_DEVICE_7970:
2596 case SKINNY_DEVICE_7912:
2598 case SKINNY_DEVICE_7902:
2600 case SKINNY_DEVICE_CIPC:
2601 return "IP Communicator";
2602 case SKINNY_DEVICE_7961:
2604 case SKINNY_DEVICE_7936:
2606 case SKINNY_DEVICE_SCCPGATEWAY_AN:
2607 return "SCCPGATEWAY_AN";
2608 case SKINNY_DEVICE_SCCPGATEWAY_BRI:
2609 return "SCCPGATEWAY_BRI";
2610 case SKINNY_DEVICE_UNKNOWN:
2613 if (!(tmp = ast_threadstorage_get(&device2str_threadbuf, DEVICE2STR_BUFSIZE)))
2615 snprintf(tmp, DEVICE2STR_BUFSIZE, "UNKNOWN-%d", type);
2620 /*! \brief Print codec list from preference to CLI/manager */
2621 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
2625 for(x = 0; x < 32 ; x++) {
2626 codec = ast_codec_pref_index(pref, x);
2629 ast_cli(fd, "%s", ast_getformatname(codec));
2630 ast_cli(fd, ":%d", pref->framing[x]);
2631 if (x < 31 && ast_codec_pref_index(pref, x + 1))
2635 ast_cli(fd, "none");
2638 static char *handle_skinny_show_devices(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2640 struct skinny_device *d;
2641 struct skinny_line *l;
2645 e->command = "skinny show devices";
2647 "Usage: skinny show devices\n"
2648 " Lists all devices known to the Skinny subsystem.\n";
2655 return CLI_SHOWUSAGE;
2657 ast_mutex_lock(&devicelock);
2659 ast_cli(a->fd, "Name DeviceId IP Type R NL\n");
2660 ast_cli(a->fd, "-------------------- ---------------- --------------- --------------- - --\n");
2662 for (d = devices; d; d = d->next) {
2665 for (l = d->lines; l; l = l->next)
2668 ast_cli(a->fd, "%-20s %-16s %-15s %-15s %c %2d\n",
2671 d->session?ast_inet_ntoa(d->session->sin.sin_addr):"",
2672 device2str(d->type),
2673 d->registered?'Y':'N',
2677 ast_mutex_unlock(&devicelock);
2682 /*! \brief Show device information */
2683 static char *handle_skinny_show_device(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2685 struct skinny_device *d;
2686 struct skinny_line *l;
2687 struct skinny_speeddial *sd;
2688 struct skinny_addon *sa;
2692 e->command = "skinny show device";
2694 "Usage: skinny show device <DeviceId|DeviceName>\n"
2695 " Lists all deviceinformation of a specific device known to the Skinny subsystem.\n";
2698 return complete_skinny_show_device(a->line, a->word, a->pos, a->n);
2702 return CLI_SHOWUSAGE;
2704 ast_mutex_lock(&devicelock);
2705 for (d = devices; d; d = d->next) {
2706 if (!strcasecmp(a->argv[3], d->id) || !strcasecmp(a->argv[3], d->name)) {
2707 int numlines = 0, numaddons = 0, numspeeddials = 0;
2709 for (l = d->lines; l; l = l->next)
2712 ast_cli(a->fd, "Name: %s\n", d->name);
2713 ast_cli(a->fd, "Id: %s\n", d->id);
2714 ast_cli(a->fd, "version: %s\n", S_OR(d->version_id, "Unknown"));
2715 ast_cli(a->fd, "Ip address: %s\n", (d->session ? ast_inet_ntoa(d->session->sin.sin_addr) : "Unknown"));
2716 ast_cli(a->fd, "Port: %d\n", (d->session ? ntohs(d->session->sin.sin_port) : 0));
2717 ast_cli(a->fd, "Device Type: %s\n", device2str(d->type));
2718 ast_cli(a->fd, "Registered: %s\n", (d->registered ? "Yes" : "No"));
2719 ast_cli(a->fd, "Lines: %d\n", numlines);
2720 for (l = d->lines; l; l = l->next)
2721 ast_cli(a->fd, " %s (%s)\n", l->name, l->label);
2722 for (sa = d->addons; sa; sa = sa->next)
2724 ast_cli(a->fd, "Addons: %d\n", numaddons);
2725 for (sa = d->addons; sa; sa = sa->next)
2726 ast_cli(a->fd, " %s\n", sa->type);
2727 for (sd = d->speeddials; sd; sd = sd->next)
2729 ast_cli(a->fd, "Speeddials: %d\n", numspeeddials);
2730 for (sd = d->speeddials; sd; sd = sd->next)
2731 ast_cli(a->fd, " %s (%s) ishint: %d\n", sd->exten, sd->label, sd->isHint);
2734 ast_mutex_unlock(&devicelock);
2738 static char *handle_skinny_show_lines(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2740 struct skinny_device *d;
2741 struct skinny_line *l;
2745 e->command = "skinny show lines";
2747 "Usage: skinny show lines\n"
2748 " Lists all lines known to the Skinny subsystem.\n";
2755 return CLI_SHOWUSAGE;
2757 ast_mutex_lock(&devicelock);
2759 ast_cli(a->fd, "Device Name Instance Name Label \n");
2760 ast_cli(a->fd, "-------------------- -------- -------------------- --------------------\n");
2761 for (d = devices; d; d = d->next) {
2762 for (l = d->lines; l; l = l->next) {
2763 ast_cli(a->fd, "%-20s %8d %-20s %-20s\n",
2771 ast_mutex_unlock(&devicelock);
2775 /*! \brief List line information. */
2776 static char *handle_skinny_show_line(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2778 struct skinny_device *d;
2779 struct skinny_line *l;
2780 char codec_buf[512];
2781 char group_buf[256];
2785 e->command = "skinny show line";
2787 "Usage: skinny show line <Line> [ on <DeviceID|DeviceName> ]\n"
2788 " List all lineinformation of a specific line known to the Skinny subsystem.\n";
2791 return complete_skinny_show_line(a->line, a->word, a->pos, a->n);
2795 return CLI_SHOWUSAGE;
2797 ast_mutex_lock(&devicelock);
2799 /* Show all lines matching the one supplied */
2800 for (d = devices; d; d = d->next) {
2801 if (a->argc == 6 && (strcasecmp(a->argv[5], d->id) && strcasecmp(a->argv[5], d->name)))
2803 for (l = d->lines; l; l = l->next) {
2804 if (strcasecmp(a->argv[3], l->name))
2806 ast_cli(a->fd, "Line: %s\n", l->name);
2807 ast_cli(a->fd, "On Device: %s\n", d->name);
2808 ast_cli(a->fd, "Line Label: %s\n", l->label);
2809 ast_cli(a->fd, "Extension: %s\n", S_OR(l->exten, "<not set>"));
2810 ast_cli(a->fd, "Context: %s\n", l->context);
2811 ast_cli(a->fd, "CallGroup: %s\n", ast_print_group(group_buf, sizeof(group_buf), l->callgroup));
2812 ast_cli(a->fd, "PickupGroup: %s\n", ast_print_group(group_buf, sizeof(group_buf), l->pickupgroup));
2813 ast_cli(a->fd, "Language: %s\n", S_OR(l->language, "<not set>"));
2814 ast_cli(a->fd, "Accountcode: %s\n", S_OR(l->accountcode, "<not set>"));
2815 ast_cli(a->fd, "AmaFlag: %s\n", ast_cdr_flags2str(l->amaflags));
2816 ast_cli(a->fd, "CallerId Number: %s\n", S_OR(l->cid_num, "<not set>"));
2817 ast_cli(a->fd, "CallerId Name: %s\n", S_OR(l->cid_name, "<not set>"));
2818 ast_cli(a->fd, "Hide CallerId: %s\n", (l->hidecallerid ? "Yes" : "No"));
2819 ast_cli(a->fd, "CFwdAll: %s\n", S_COR((l->cfwdtype & SKINNY_CFWD_ALL), l->call_forward_all, "<not set>"));
2820 ast_cli(a->fd, "CFwdBusy: %s\n", S_COR((l->cfwdtype & SKINNY_CFWD_BUSY), l->call_forward_busy, "<not set>"));
2821 ast_cli(a->fd, "CFwdNoAnswer: %s\n", S_COR((l->cfwdtype & SKINNY_CFWD_NOANSWER), l->call_forward_noanswer, "<not set>"));
2822 ast_cli(a->fd, "VoicemailBox: %s\n", S_OR(l->mailbox, "<not set>"));
2823 ast_cli(a->fd, "VoicemailNumber: %s\n", S_OR(l->vmexten, "<not set>"));
2824 ast_cli(a->fd, "MWIblink: %d\n", l->mwiblink);
2825 ast_cli(a->fd, "Regextension: %s\n", S_OR(l->regexten, "<not set>"));
2826 ast_cli(a->fd, "Regcontext: %s\n", S_OR(l->regcontext, "<not set>"));
2827 ast_cli(a->fd, "MoHInterpret: %s\n", S_OR(l->mohinterpret, "<not set>"));
2828 ast_cli(a->fd, "MoHSuggest: %s\n", S_OR(l->mohsuggest, "<not set>"));
2829 ast_cli(a->fd, "Last dialed nr: %s\n", S_OR(l->lastnumberdialed, "<no calls made yet>"));
2830 ast_cli(a->fd, "Last CallerID: %s\n", S_OR(l->lastcallerid, "<not set>"));
2831 ast_cli(a->fd, "Transfer enabled: %s\n", (l->transfer ? "Yes" : "No"));
2832 ast_cli(a->fd, "Callwaiting: %s\n", (l->callwaiting ? "Yes" : "No"));
2833 ast_cli(a->fd, "3Way Calling: %s\n", (l->threewaycalling ? "Yes" : "No"));
2834 ast_cli(a->fd, "Can forward: %s\n", (l->cancallforward ? "Yes" : "No"));
2835 ast_cli(a->fd, "Do Not Disturb: %s\n", (l->dnd ? "Yes" : "No"));
2836 ast_cli(a->fd, "NAT: %s\n", (l->nat ? "Yes" : "No"));
2837 ast_cli(a->fd, "immediate: %s\n", (l->immediate ? "Yes" : "No"));
2838 ast_cli(a->fd, "Group: %d\n", l->group);
2839 ast_cli(a->fd, "Codecs: ");
2840 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) - 1, l->capability);
2841 ast_cli(a->fd, "%s\n", codec_buf);
2842 ast_cli(a->fd, "Codec Order: (");
2843 print_codec_to_cli(a->fd, &l->prefs);
2844 ast_cli(a->fd, ")\n");
2845 ast_cli(a->fd, "\n");
2849 ast_mutex_unlock(&devicelock);
2853 /*! \brief List global settings for the Skinny subsystem. */
2854 static char *handle_skinny_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2858 e->command = "skinny show settings";
2860 "Usage: skinny show settings\n"
2861 " Lists all global configuration settings of the Skinny subsystem.\n";
2868 return CLI_SHOWUSAGE;
2870 ast_cli(a->fd, "\nGlobal Settings:\n");
2871 ast_cli(a->fd, " Skinny Port: %d\n", ntohs(bindaddr.sin_port));
2872 ast_cli(a->fd, " Bindaddress: %s\n", ast_inet_ntoa(bindaddr.sin_addr));
2873 ast_cli(a->fd, " KeepAlive: %d\n", keep_alive);
2874 ast_cli(a->fd, " Date Format: %s\n", date_format);
2875 ast_cli(a->fd, " Voice Mail Extension: %s\n", S_OR(vmexten, "(not set)"));
2876 ast_cli(a->fd, " Reg. context: %s\n", S_OR(regcontext, "(not set)"));
2877 ast_cli(a->fd, " Jitterbuffer enabled: %s\n", (ast_test_flag(&global_jbconf, AST_JB_ENABLED) ? "Yes" : "No"));
2878 ast_cli(a->fd, " Jitterbuffer forced: %s\n", (ast_test_flag(&global_jbconf, AST_JB_FORCED) ? "Yes" : "No"));
2879 ast_cli(a->fd, " Jitterbuffer max size: %ld\n", global_jbconf.max_size);
2880 ast_cli(a->fd, " Jitterbuffer resync: %ld\n", global_jbconf.resync_threshold);
2881 ast_cli(a->fd, " Jitterbuffer impl: %s\n", global_jbconf.impl);
2882 ast_cli(a->fd, " Jitterbuffer log: %s\n", (ast_test_flag(&global_jbconf, AST_JB_LOG) ? "Yes" : "No"));
2887 static struct ast_cli_entry cli_skinny_set_debug_deprecated = AST_CLI_DEFINE(handle_skinny_set_debug_deprecated, "Enable/Disable Skinny debugging");
2888 static struct ast_cli_entry cli_skinny[] = {
2889 AST_CLI_DEFINE(handle_skinny_show_devices, "List defined Skinny devices"),
2890 AST_CLI_DEFINE(handle_skinny_show_device, "List Skinny device information"),
2891 AST_CLI_DEFINE(handle_skinny_show_lines, "List defined Skinny lines per device"),
2892 AST_CLI_DEFINE(handle_skinny_show_line, "List Skinny line information"),
2893 AST_CLI_DEFINE(handle_skinny_show_settings, "List global Skinny settings"),
2894 AST_CLI_DEFINE(handle_skinny_set_debug, "Enable/Disable Skinny debugging", .deprecate_cmd = &cli_skinny_set_debug_deprecated),
2895 AST_CLI_DEFINE(handle_skinny_reset, "Reset Skinny device(s)"),
2899 static struct skinny_paging_device *build_paging_device(const char *cat, struct ast_variable *v)
2905 static struct skinny_device *build_device(const char *cat, struct ast_variable *v)
2907 struct skinny_device *d;
2908 struct skinny_line *l;
2909 struct skinny_speeddial *sd;
2910 struct skinny_addon *a;
2911 char device_vmexten[AST_MAX_EXTENSION];
2912 struct ast_variable *chanvars = NULL;
2913 int lineInstance = 1;
2914 int speeddialInstance = 1;
2917 if (!(d = ast_calloc(1, sizeof(*d)))) {
2920 ast_copy_string(d->name, cat, sizeof(d->name));
2921 d->lastlineinstance = 1;
2922 d->capability = default_capability;
2923 d->prefs = default_prefs;
2924 if (!ast_strlen_zero(vmexten))
2925 ast_copy_string(device_vmexten, vmexten, sizeof(device_vmexten));
2927 memset(device_vmexten, 0, sizeof(device_vmexten));
2931 if (!strcasecmp(v->name, "host")) {
2932 if (ast_get_ip(&d->addr, v->value)) {
2936 } else if (!strcasecmp(v->name, "port")) {
2937 d->addr.sin_port = htons(atoi(v->value));
2938 } else if (!strcasecmp(v->name, "device")) {
2939 ast_copy_string(d->id, v->value, sizeof(d->id));
2940 } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
2941 d->ha = ast_append_ha(v->name, v->value, d->ha, NULL);
2942 } else if (!strcasecmp(v->name, "vmexten")) {
2943 ast_copy_string(device_vmexten, v->value, sizeof(device_vmexten));
2944 } else if (!strcasecmp(v->name, "context")) {
2945 ast_copy_string(context, v->value, sizeof(context));
2946 } else if (!strcasecmp(v->name, "regexten")) {
2947 ast_copy_string(regexten, v->value, sizeof(regexten));
2948 } else if (!strcasecmp(v->name, "allow")) {
2949 ast_parse_allow_disallow(&d->prefs, &d->capability, v->value, 1);
2950 } else if (!strcasecmp(v->name, "disallow")) {
2951 ast_parse_allow_disallow(&d->prefs, &d->capability, v->value, 0);
2952 } else if (!strcasecmp(v->name, "version")) {
2953 ast_copy_string(d->version_id, v->value, sizeof(d->version_id));
2954 } else if (!strcasecmp(v->name, "canreinvite")) {
2955 canreinvite = ast_true(v->value);
2956 } else if (!strcasecmp(v->name, "earlyrtp")) {
2957 d->earlyrtp = ast_true(v->value);
2958 } else if (!strcasecmp(v->name, "nat")) {
2959 nat = ast_true(v->value);
2960 } else if (!strcasecmp(v->name, "callerid")) {
2961 if (!strcasecmp(v->value, "asreceived")) {
2965 ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
2967 } else if (!strcasecmp(v->name, "language")) {
2968 ast_copy_string(language, v->value, sizeof(language));
2969 } else if (!strcasecmp(v->name, "accountcode")) {
2970 ast_copy_string(accountcode, v->value, sizeof(accountcode));
2971 } else if (!strcasecmp(v->name, "amaflags")) {
2972 y = ast_cdr_amaflags2int(v->value);
2974 ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno);
2978 } else if (!strcasecmp(v->name, "mohinterpret") || !strcasecmp(v->name, "musiconhold")) {
2979 ast_copy_string(mohinterpret, v->value, sizeof(mohinterpret));
2980 } else if (!strcasecmp(v->name, "mohsuggest")) {
2981 ast_copy_string(mohsuggest, v->value, sizeof(mohsuggest));
2982 } else if (!strcasecmp(v->name, "callgroup")) {
2983 cur_callergroup = ast_get_group(v->value);
2984 } else if (!strcasecmp(v->name, "pickupgroup")) {
2985 cur_pickupgroup = ast_get_group(v->value);
2986 } else if (!strcasecmp(v->name, "immediate")) {
2987 immediate = ast_true(v->value);
2988 } else if (!strcasecmp(v->name, "cancallforward")) {
2989 cancallforward = ast_true(v->value);
2990 } else if (!strcasecmp(v->name, "mailbox")) {
2991 ast_copy_string(mailbox, v->value, sizeof(mailbox));
2992 } else if (!strcasecmp(v->name, "callreturn")) {
2993 callreturn = ast_true(v->value);
2994 } else if (!strcasecmp(v->name, "callwaiting")) {
2995 callwaiting = ast_true(v->value);
2996 } else if (!strcasecmp(v->name, "transfer")) {
2997 transfer = ast_true(v->value);
2998 } else if (!strcasecmp(v->name, "threewaycalling")) {
2999 threewaycalling = ast_true(v->value);
3000 } else if (!strcasecmp(v->name, "mwiblink")) {
3001 mwiblink = ast_true(v->value);
3002 } else if (!strcasecmp(v->name, "linelabel")) {
3003 ast_copy_string(linelabel, v->value, sizeof(linelabel));
3004 } else if (!strcasecmp(v->name, "setvar")) {
3005 chanvars = add_var(v->value, chanvars);
3006 } else if (!strcasecmp(v->name, "speeddial")) {
3007 if (!(sd = ast_calloc(1, sizeof(*sd)))) {
3011 char *stringp = buf, *exten, *context, *label;
3013 ast_copy_string(buf, v->value, sizeof(buf));
3014 exten = strsep(&stringp, ",");
3015 if ((context = strchr(exten, '@'))) {
3019 ast_mutex_init(&sd->lock);
3020 ast_copy_string(sd->exten, exten, sizeof(sd->exten));
3021 if (!ast_strlen_zero(context)) {
3023 sd->instance = lineInstance++;
3024 ast_copy_string(sd->context, context, sizeof(sd->context));
3027 sd->instance = speeddialInstance++;
3028 sd->context[0] = '\0';
3030 ast_copy_string(sd->label, S_OR(label, exten), sizeof(sd->label));
3034 sd->next = d->speeddials;
3037 } else if (!strcasecmp(v->name, "addon")) {
3038 if (!(a = ast_calloc(1, sizeof(*a)))) {
3041 ast_mutex_init(&a->lock);
3042 ast_copy_string(a->type, v->value, sizeof(a->type));
3044 a->next = d->addons;
3047 } else if (!strcasecmp(v->name, "trunk") || !strcasecmp(v->name, "line")) {
3048 if (!(l = ast_calloc(1, sizeof(*l)))) {
3051 ast_mutex_init(&l->lock);
3052 ast_copy_string(l->name, v->value, sizeof(l->name));
3054 /* XXX Should we check for uniqueness?? XXX */
3055 ast_copy_string(l->context, context, sizeof(l->context));
3056 ast_copy_string(l->cid_num, cid_num, sizeof(l->cid_num));
3057 ast_copy_string(l->cid_name, cid_name, sizeof(l->cid_name));
3058 ast_copy_string(l->label, linelabel, sizeof(l->label));
3059 ast_copy_string(l->language, language, sizeof(l->language));
3060 ast_copy_string(l->mohinterpret, mohinterpret, sizeof(l->mohinterpret));
3061 ast_copy_string(l->mohsuggest, mohsuggest, sizeof(l->mohsuggest));
3062 ast_copy_string(l->regexten, regexten, sizeof(l->regexten));
3063 ast_copy_string(l->mailbox, mailbox, sizeof(l->mailbox));
3064 if (!ast_strlen_zero(mailbox))
3065 ast_verb(3, "Setting mailbox '%s' on %s@%s\n", mailbox, d->name, l->name);
3066 ast_copy_string(l->vmexten, device_vmexten, sizeof(vmexten));
3067 l->chanvars = chanvars;
3069 l->capability = d->capability;
3070 l->prefs = d->prefs;
3072 if (!strcasecmp(v->name, "trunk")) {
3073 l->type = TYPE_TRUNK;
3075 l->type = TYPE_LINE;
3077 l->immediate = immediate;
3078 l->callgroup = cur_callergroup;
3079 l->pickupgroup = cur_pickupgroup;
3080 l->callreturn = callreturn;
3081 l->cancallforward = cancallforward;
3083 set_callforwards(l, NULL, 0);
3084 l->callwaiting = callwaiting;
3085 l->transfer = transfer;
3086 l->threewaycalling = threewaycalling;
3087 l->mwiblink = mwiblink;
3088 l->onhooktime = time(NULL);
3089 l->instance = lineInstance++;
3090 /* ASSUME we're onhook at this point */
3091 l->hookstate = SKINNY_ONHOOK;
3093 l->canreinvite = canreinvite;
3099 ast_log(LOG_WARNING, "Don't know keyword '%s' at line %d\n", v->name, v->lineno);
3105 ast_log(LOG_ERROR, "A Skinny device must have at least one line!\n");
3108 if (/*d->addr.sin_addr.s_addr && */!ntohs(d->addr.sin_port)) {
3109 d->addr.sin_port = htons(DEFAULT_SKINNY_PORT);
3112 /* I don't think we need this anymore at all, since d->ourip is set in skinny_register now */
3113 if (d->addr.sin_addr.s_addr) {
3114 /* XXX See note above, in 'host' option. */
3115 if (ast_ouraddrfor(&d->addr.sin_addr, &d->ourip)) {
3126 static void start_rtp(struct skinny_subchannel *sub)
3128 struct skinny_line *l = sub->parent;
3129 struct skinny_device *d = l->parent;
3132 ast_mutex_lock(&sub->lock);
3133 /* Allocate the RTP */
3134 sub->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
3136 sub->vrtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
3138 if (sub->rtp && sub->owner) {
3139 ast_channel_set_fd(sub->owner, 0, ast_rtp_fd(sub->rtp));
3140 ast_channel_set_fd(sub->owner, 1, ast_rtcp_fd(sub->rtp));
3142 if (hasvideo && sub->vrtp && sub->owner) {
3143 ast_channel_set_fd(sub->owner, 2, ast_rtp_fd(sub->vrtp));
3144 ast_channel_set_fd(sub->owner, 3, ast_rtcp_fd(sub->vrtp));
3147 ast_rtp_setqos(sub->rtp, tos_audio, cos_audio, "Skinny RTP");
3148 ast_rtp_setnat(sub->rtp, l->nat);
3151 ast_rtp_setqos(sub->vrtp, tos_video, cos_video, "Skinny VRTP");
3152 ast_rtp_setnat(sub->vrtp, l->nat);
3154 /* Set Frame packetization */
3156 ast_rtp_codec_setpref(sub->rtp, &l->prefs);
3158 /* Create the RTP connection */
3159 transmit_connect(d->session, sub);
3160 ast_mutex_unlock(&sub->lock);
3163 static void *skinny_newcall(void *data)
3165 struct ast_channel *c = data;
3166 struct skinny_subchannel *sub = c->tech_pvt;
3167 struct skinny_line *l = sub->parent;
3168 struct skinny_device *d = l->parent;
3169 struct skinnysession *s = d->session;
3172 ast_copy_string(l->lastnumberdialed, c->exten, sizeof(l->lastnumberdialed));
3174 l->hidecallerid ? "" : l->cid_num,
3175 l->hidecallerid ? "" : l->cid_name,
3176 c->cid.cid_ani ? NULL : l->cid_num);
3177 ast_setstate(c, AST_STATE_RING);
3181 res = ast_pbx_run(c);
3183 ast_log(LOG_WARNING, "PBX exited non-zero\n");
3184 transmit_tone(s, SKINNY_REORDER, l->instance, sub->callid);
3189 static void *skinny_ss(void *data)
3191 struct ast_channel *c = data;
3192 struct skinny_subchannel *sub = c->tech_pvt;
3193 struct skinny_line *l = sub->parent;
3194 struct skinny_device *d = l->parent;
3195 struct skinnysession *s = d->session;