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"
70 #include "asterisk/event.h"
72 /*************************************
73 * Skinny/Asterisk Protocol Settings *
74 *************************************/
75 static const char tdesc[] = "Skinny Client Control Protocol (Skinny)";
76 static const char config[] = "skinny.conf";
78 static int default_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW;
79 static struct ast_codec_pref default_prefs;
82 SKINNY_CODEC_ALAW = 2,
83 SKINNY_CODEC_ULAW = 4,
84 SKINNY_CODEC_G723_1 = 9,
85 SKINNY_CODEC_G729A = 12,
86 SKINNY_CODEC_G726_32 = 82, /* XXX Which packing order does this translate to? */
87 SKINNY_CODEC_H261 = 100,
88 SKINNY_CODEC_H263 = 101
91 #define DEFAULT_SKINNY_PORT 2000
92 #define DEFAULT_SKINNY_BACKLOG 2
93 #define SKINNY_MAX_PACKET 1000
95 static unsigned int tos = 0;
96 static unsigned int tos_audio = 0;
97 static unsigned int tos_video = 0;
98 static unsigned int cos = 0;
99 static unsigned int cos_audio = 0;
100 static unsigned int cos_video = 0;
102 static int keep_alive = 120;
103 static char vmexten[AST_MAX_EXTENSION]; /* Voicemail pilot number */
104 static char used_context[AST_MAX_EXTENSION]; /* Voicemail pilot number */
105 static char regcontext[AST_MAX_CONTEXT]; /* Context for auto-extension */
106 static char date_format[6] = "D-M-Y";
107 static char version_id[16] = "P002F202";
109 #if __BYTE_ORDER == __LITTLE_ENDIAN
110 #define letohl(x) (x)
111 #define letohs(x) (x)
112 #define htolel(x) (x)
113 #define htoles(x) (x)
115 #if defined(HAVE_BYTESWAP_H)
116 #include <byteswap.h>
117 #define letohl(x) bswap_32(x)
118 #define letohs(x) bswap_16(x)
119 #define htolel(x) bswap_32(x)
120 #define htoles(x) bswap_16(x)
121 #elif defined(HAVE_SYS_ENDIAN_SWAP16)
122 #include <sys/endian.h>
123 #define letohl(x) __swap32(x)
124 #define letohs(x) __swap16(x)
125 #define htolel(x) __swap32(x)
126 #define htoles(x) __swap16(x)
127 #elif defined(HAVE_SYS_ENDIAN_BSWAP16)
128 #include <sys/endian.h>
129 #define letohl(x) bswap32(x)
130 #define letohs(x) bswap16(x)
131 #define htolel(x) bswap32(x)
132 #define htoles(x) bswap16(x)
134 #define __bswap_16(x) \
135 ((((x) & 0xff00) >> 8) | \
136 (((x) & 0x00ff) << 8))
137 #define __bswap_32(x) \
138 ((((x) & 0xff000000) >> 24) | \
139 (((x) & 0x00ff0000) >> 8) | \
140 (((x) & 0x0000ff00) << 8) | \
141 (((x) & 0x000000ff) << 24))
142 #define letohl(x) __bswap_32(x)
143 #define letohs(x) __bswap_16(x)
144 #define htolel(x) __bswap_32(x)
145 #define htoles(x) __bswap_16(x)
149 /*! Global jitterbuffer configuration - by default, jb is disabled */
150 static struct ast_jb_conf default_jbconf =
154 .resync_threshold = -1,
157 static struct ast_jb_conf global_jbconf;
159 AST_THREADSTORAGE(device2str_threadbuf);
160 #define DEVICE2STR_BUFSIZE 15
162 AST_THREADSTORAGE(control2str_threadbuf);
163 #define CONTROL2STR_BUFSIZE 100
165 /*********************
166 * Protocol Messages *
167 *********************/
169 #define KEEP_ALIVE_MESSAGE 0x0000
170 /* no additional struct */
172 #define REGISTER_MESSAGE 0x0001
173 struct register_message {
182 #define IP_PORT_MESSAGE 0x0002
184 #define KEYPAD_BUTTON_MESSAGE 0x0003
185 struct keypad_button_message {
187 uint32_t lineInstance;
188 uint32_t callReference;
192 #define ENBLOC_CALL_MESSAGE 0x0004
193 struct enbloc_call_message {
194 char calledParty[24];
197 #define STIMULUS_MESSAGE 0x0005
198 struct stimulus_message {
200 uint32_t stimulusInstance;
201 uint32_t callreference;
204 #define OFFHOOK_MESSAGE 0x0006
205 struct offhook_message {
210 #define ONHOOK_MESSAGE 0x0007
211 struct onhook_message {
216 #define CAPABILITIES_RES_MESSAGE 0x0010
217 struct station_capabilities {
226 #define SKINNY_MAX_CAPABILITIES 18
228 struct capabilities_res_message {
230 struct station_capabilities caps[SKINNY_MAX_CAPABILITIES];
233 #define SPEED_DIAL_STAT_REQ_MESSAGE 0x000A
234 struct speed_dial_stat_req_message {
235 uint32_t speedDialNumber;
238 #define LINE_STATE_REQ_MESSAGE 0x000B
239 struct line_state_req_message {
243 #define TIME_DATE_REQ_MESSAGE 0x000D
244 #define BUTTON_TEMPLATE_REQ_MESSAGE 0x000E
245 #define VERSION_REQ_MESSAGE 0x000F
246 #define SERVER_REQUEST_MESSAGE 0x0012
248 #define ALARM_MESSAGE 0x0020
249 struct alarm_message {
250 uint32_t alarmSeverity;
251 char displayMessage[80];
252 uint32_t alarmParam1;
253 uint32_t alarmParam2;
256 #define OPEN_RECEIVE_CHANNEL_ACK_MESSAGE 0x0022
257 struct open_receive_channel_ack_message {
264 #define SOFT_KEY_SET_REQ_MESSAGE 0x0025
266 #define SOFT_KEY_EVENT_MESSAGE 0x0026
267 struct soft_key_event_message {
268 uint32_t softKeyEvent;
270 uint32_t callreference;
273 #define UNREGISTER_MESSAGE 0x0027
274 #define SOFT_KEY_TEMPLATE_REQ_MESSAGE 0x0028
275 #define HEADSET_STATUS_MESSAGE 0x002B
276 #define REGISTER_AVAILABLE_LINES_MESSAGE 0x002D
278 #define REGISTER_ACK_MESSAGE 0x0081
279 struct register_ack_message {
281 char dateTemplate[6];
283 uint32_t secondaryKeepAlive;
287 #define START_TONE_MESSAGE 0x0082
288 struct start_tone_message {
295 #define STOP_TONE_MESSAGE 0x0083
296 struct stop_tone_message {
301 #define SET_RINGER_MESSAGE 0x0085
302 struct set_ringer_message {
304 uint32_t unknown1; /* See notes in transmit_ringer_mode */
309 #define SET_LAMP_MESSAGE 0x0086
310 struct set_lamp_message {
312 uint32_t stimulusInstance;
313 uint32_t deviceStimulus;
316 #define SET_SPEAKER_MESSAGE 0x0088
317 struct set_speaker_message {
321 /* XXX When do we need to use this? */
322 #define SET_MICROPHONE_MESSAGE 0x0089
323 struct set_microphone_message {
327 #define START_MEDIA_TRANSMISSION_MESSAGE 0x008A
328 struct media_qualifier {
335 struct start_media_transmission_message {
336 uint32_t conferenceId;
337 uint32_t passThruPartyId;
341 uint32_t payloadType;
342 struct media_qualifier qualifier;
346 #define STOP_MEDIA_TRANSMISSION_MESSAGE 0x008B
347 struct stop_media_transmission_message {
348 uint32_t conferenceId;
349 uint32_t passThruPartyId;
353 #define CALL_INFO_MESSAGE 0x008F
354 struct call_info_message {
355 char callingPartyName[40];
356 char callingParty[24];
357 char calledPartyName[40];
358 char calledParty[24];
362 char originalCalledPartyName[40];
363 char originalCalledParty[24];
364 char lastRedirectingPartyName[40];
365 char lastRedirectingParty[24];
366 uint32_t originalCalledPartyRedirectReason;
367 uint32_t lastRedirectingReason;
368 char callingPartyVoiceMailbox[24];
369 char calledPartyVoiceMailbox[24];
370 char originalCalledPartyVoiceMailbox[24];
371 char lastRedirectingVoiceMailbox[24];
375 #define FORWARD_STAT_MESSAGE 0x0090
376 struct forward_stat_message {
377 uint32_t activeforward;
383 uint32_t fwdnoanswer;
384 char fwdnoanswernum[24];
387 #define SPEED_DIAL_STAT_RES_MESSAGE 0x0091
388 struct speed_dial_stat_res_message {
389 uint32_t speedDialNumber;
390 char speedDialDirNumber[24];
391 char speedDialDisplayName[40];
394 #define LINE_STAT_RES_MESSAGE 0x0092
395 struct line_stat_res_message {
397 char lineDirNumber[24];
398 char lineDisplayName[24];
402 #define DEFINETIMEDATE_MESSAGE 0x0094
403 struct definetimedate_message {
404 uint32_t year; /* since 1900 */
406 uint32_t dayofweek; /* monday = 1 */
411 uint32_t milliseconds;
415 #define BUTTON_TEMPLATE_RES_MESSAGE 0x0097
416 struct button_definition {
417 uint8_t instanceNumber;
418 uint8_t buttonDefinition;
421 struct button_definition_template {
422 uint8_t buttonDefinition;
423 /* for now, anything between 0xB0 and 0xCF is custom */
427 #define STIMULUS_REDIAL 0x01
428 #define STIMULUS_SPEEDDIAL 0x02
429 #define STIMULUS_HOLD 0x03
430 #define STIMULUS_TRANSFER 0x04
431 #define STIMULUS_FORWARDALL 0x05
432 #define STIMULUS_FORWARDBUSY 0x06
433 #define STIMULUS_FORWARDNOANSWER 0x07
434 #define STIMULUS_DISPLAY 0x08
435 #define STIMULUS_LINE 0x09
436 #define STIMULUS_VOICEMAIL 0x0F
437 #define STIMULUS_AUTOANSWER 0x11
438 #define STIMULUS_DND 0x3F
439 #define STIMULUS_CONFERENCE 0x7D
440 #define STIMULUS_CALLPARK 0x7E
441 #define STIMULUS_CALLPICKUP 0x7F
442 #define STIMULUS_NONE 0xFF
445 #define BT_REDIAL STIMULUS_REDIAL
446 #define BT_SPEEDDIAL STIMULUS_SPEEDDIAL
447 #define BT_HOLD STIMULUS_HOLD
448 #define BT_TRANSFER STIMULUS_TRANSFER
449 #define BT_FORWARDALL STIMULUS_FORWARDALL
450 #define BT_FORWARDBUSY STIMULUS_FORWARDBUSY
451 #define BT_FORWARDNOANSWER STIMULUS_FORWARDNOANSWER
452 #define BT_DISPLAY STIMULUS_DISPLAY
453 #define BT_LINE STIMULUS_LINE
454 #define BT_VOICEMAIL STIMULUS_VOICEMAIL
455 #define BT_AUTOANSWER STIMULUS_AUTOANSWER
456 #define BT_DND STIMULUS_DND
457 #define BT_CONFERENCE STIMULUS_CONFERENCE
458 #define BT_CALLPARK STIMULUS_CALLPARK
459 #define BT_CALLPICKUP STIMULUS_CALLPICKUP
462 /* Custom button types - add our own between 0xB0 and 0xCF.
463 This may need to be revised in the future,
464 if stimuluses are ever added in this range. */
465 #define BT_CUST_LINESPEEDDIAL 0xB0 /* line or speeddial with/without hint */
466 #define BT_CUST_LINE 0xB1 /* line or speeddial with hint only */
468 struct button_template_res_message {
469 uint32_t buttonOffset;
470 uint32_t buttonCount;
471 uint32_t totalButtonCount;
472 struct button_definition definition[42];
475 #define VERSION_RES_MESSAGE 0x0098
476 struct version_res_message {
480 #define DISPLAYTEXT_MESSAGE 0x0099
481 struct displaytext_message {
485 #define CLEAR_NOTIFY_MESSAGE 0x0115
486 #define CLEAR_DISPLAY_MESSAGE 0x009A
488 #define CAPABILITIES_REQ_MESSAGE 0x009B
490 #define REGISTER_REJ_MESSAGE 0x009D
491 struct register_rej_message {
495 #define SERVER_RES_MESSAGE 0x009E
496 struct server_identifier {
500 struct server_res_message {
501 struct server_identifier server[5];
502 uint32_t serverListenPort[5];
503 uint32_t serverIpAddr[5];
506 #define RESET_MESSAGE 0x009F
507 struct reset_message {
511 #define KEEP_ALIVE_ACK_MESSAGE 0x0100
513 #define OPEN_RECEIVE_CHANNEL_MESSAGE 0x0105
514 struct open_receive_channel_message {
515 uint32_t conferenceId;
524 #define CLOSE_RECEIVE_CHANNEL_MESSAGE 0x0106
525 struct close_receive_channel_message {
526 uint32_t conferenceId;
531 #define SOFT_KEY_TEMPLATE_RES_MESSAGE 0x0108
533 struct soft_key_template_definition {
534 char softKeyLabel[16];
535 uint32_t softKeyEvent;
538 #define KEYDEF_ONHOOK 0
539 #define KEYDEF_CONNECTED 1
540 #define KEYDEF_ONHOLD 2
541 #define KEYDEF_RINGIN 3
542 #define KEYDEF_OFFHOOK 4
543 #define KEYDEF_CONNWITHTRANS 5
544 #define KEYDEF_DADFD 6 /* Digits After Dialing First Digit */
545 #define KEYDEF_CONNWITHCONF 7
546 #define KEYDEF_RINGOUT 8
547 #define KEYDEF_OFFHOOKWITHFEAT 9
548 #define KEYDEF_UNKNOWN 10
550 #define SOFTKEY_NONE 0x00
551 #define SOFTKEY_REDIAL 0x01
552 #define SOFTKEY_NEWCALL 0x02
553 #define SOFTKEY_HOLD 0x03
554 #define SOFTKEY_TRNSFER 0x04
555 #define SOFTKEY_CFWDALL 0x05
556 #define SOFTKEY_CFWDBUSY 0x06
557 #define SOFTKEY_CFWDNOANSWER 0x07
558 #define SOFTKEY_BKSPC 0x08
559 #define SOFTKEY_ENDCALL 0x09
560 #define SOFTKEY_RESUME 0x0A
561 #define SOFTKEY_ANSWER 0x0B
562 #define SOFTKEY_INFO 0x0C
563 #define SOFTKEY_CONFRN 0x0D
564 #define SOFTKEY_PARK 0x0E
565 #define SOFTKEY_JOIN 0x0F
566 #define SOFTKEY_MEETME 0x10
567 #define SOFTKEY_PICKUP 0x11
568 #define SOFTKEY_GPICKUP 0x12
569 #define SOFTKEY_DND 0x13
570 #define SOFTKEY_IDIVERT 0x14
572 struct soft_key_template_definition soft_key_template_default[] = {
573 { "\200\001", SOFTKEY_REDIAL },
574 { "\200\002", SOFTKEY_NEWCALL },
575 { "\200\003", SOFTKEY_HOLD },
576 { "\200\004", SOFTKEY_TRNSFER },
577 { "\200\005", SOFTKEY_CFWDALL },
578 { "\200\006", SOFTKEY_CFWDBUSY },
579 { "\200\007", SOFTKEY_CFWDNOANSWER },
580 { "\200\010", SOFTKEY_BKSPC },
581 { "\200\011", SOFTKEY_ENDCALL },
582 { "\200\012", SOFTKEY_RESUME },
583 { "\200\013", SOFTKEY_ANSWER },
584 { "\200\014", SOFTKEY_INFO },
585 { "\200\015", SOFTKEY_CONFRN },
586 { "\200\016", SOFTKEY_PARK },
587 { "\200\017", SOFTKEY_JOIN },
588 { "\200\020", SOFTKEY_MEETME },
589 { "\200\021", SOFTKEY_PICKUP },
590 { "\200\022", SOFTKEY_GPICKUP },
591 { "\200\077", SOFTKEY_DND },
592 { "\200\120", SOFTKEY_IDIVERT },
595 /* Localized message "codes" (in octal)
596 Below is en_US (taken from a 7970)
618 \023: Your current options
635 \044: You Have VoiceMail
637 \046: Can Not Complete Conference
638 \047: No Conference Bridge
639 \050: Can Not Hold Primary Control
640 \051: Invalid Conference Participant
641 \052: In Conference Already
642 \053: No Participant Info
643 \054: Exceed Maximum Parties
644 \055: Key Is Not Active
645 \056: Error No License
648 \061: Error Pass Limit
654 \067: Not Enough Bandwidth
665 \102: Network congestion,rerouting
667 \104: Failed to setup Barge
668 \105: Another Barge exists
669 \106: Incompatible device type
670 \107: No Park Number Available
671 \110: CallPark Reversion
672 \111: Service is not Active
673 \112: High Traffic Try Again Later
681 \122: Can Not Complete Transfer
682 \123: Can Not Join Calls
683 \124: Mcid Successful
684 \125: Number Not Configured
686 \127: Video Bandwidth Unavailable
688 \131: Max Call Duration Timeout
689 \132: Max Hold Duration Timeout
696 \141: External Transfer Restricted
706 \153: Default Router 1
707 \154: Default Router 2
708 \155: Default Router 3
709 \156: Default Router 4
710 \157: Default Router 5
716 \165: Operational VLAN Id
723 \174: Information URL
724 \175: Directories URL
729 struct soft_key_definitions {
731 const uint8_t *defaults;
735 static const uint8_t soft_key_default_onhook[] = {
745 static const uint8_t soft_key_default_connected[] = {
754 static const uint8_t soft_key_default_onhold[] = {
761 static const uint8_t soft_key_default_ringin[] = {
767 static const uint8_t soft_key_default_offhook[] = {
775 static const uint8_t soft_key_default_connwithtrans[] = {
784 static const uint8_t soft_key_default_dadfd[] = {
789 static const uint8_t soft_key_default_connwithconf[] = {
793 static const uint8_t soft_key_default_ringout[] = {
798 static const uint8_t soft_key_default_offhookwithfeat[] = {
803 static const uint8_t soft_key_default_unknown[] = {
807 static const struct soft_key_definitions soft_key_default_definitions[] = {
808 {KEYDEF_ONHOOK, soft_key_default_onhook, sizeof(soft_key_default_onhook) / sizeof(uint8_t)},
809 {KEYDEF_CONNECTED, soft_key_default_connected, sizeof(soft_key_default_connected) / sizeof(uint8_t)},
810 {KEYDEF_ONHOLD, soft_key_default_onhold, sizeof(soft_key_default_onhold) / sizeof(uint8_t)},
811 {KEYDEF_RINGIN, soft_key_default_ringin, sizeof(soft_key_default_ringin) / sizeof(uint8_t)},
812 {KEYDEF_OFFHOOK, soft_key_default_offhook, sizeof(soft_key_default_offhook) / sizeof(uint8_t)},
813 {KEYDEF_CONNWITHTRANS, soft_key_default_connwithtrans, sizeof(soft_key_default_connwithtrans) / sizeof(uint8_t)},
814 {KEYDEF_DADFD, soft_key_default_dadfd, sizeof(soft_key_default_dadfd) / sizeof(uint8_t)},
815 {KEYDEF_CONNWITHCONF, soft_key_default_connwithconf, sizeof(soft_key_default_connwithconf) / sizeof(uint8_t)},
816 {KEYDEF_RINGOUT, soft_key_default_ringout, sizeof(soft_key_default_ringout) / sizeof(uint8_t)},
817 {KEYDEF_OFFHOOKWITHFEAT, soft_key_default_offhookwithfeat, sizeof(soft_key_default_offhookwithfeat) / sizeof(uint8_t)},
818 {KEYDEF_UNKNOWN, soft_key_default_unknown, sizeof(soft_key_default_unknown) / sizeof(uint8_t)}
821 struct soft_key_template_res_message {
822 uint32_t softKeyOffset;
823 uint32_t softKeyCount;
824 uint32_t totalSoftKeyCount;
825 struct soft_key_template_definition softKeyTemplateDefinition[32];
828 #define SOFT_KEY_SET_RES_MESSAGE 0x0109
830 struct soft_key_set_definition {
831 uint8_t softKeyTemplateIndex[16];
832 uint16_t softKeyInfoIndex[16];
835 struct soft_key_set_res_message {
836 uint32_t softKeySetOffset;
837 uint32_t softKeySetCount;
838 uint32_t totalSoftKeySetCount;
839 struct soft_key_set_definition softKeySetDefinition[16];
843 #define SELECT_SOFT_KEYS_MESSAGE 0x0110
844 struct select_soft_keys_message {
847 uint32_t softKeySetIndex;
848 uint32_t validKeyMask;
851 #define CALL_STATE_MESSAGE 0x0111
852 struct call_state_message {
854 uint32_t lineInstance;
855 uint32_t callReference;
859 #define DISPLAY_PROMPT_STATUS_MESSAGE 0x0112
860 struct display_prompt_status_message {
861 uint32_t messageTimeout;
862 char promptMessage[32];
863 uint32_t lineInstance;
864 uint32_t callReference;
867 #define CLEAR_PROMPT_MESSAGE 0x0113
868 struct clear_prompt_message {
869 uint32_t lineInstance;
870 uint32_t callReference;
873 #define DISPLAY_NOTIFY_MESSAGE 0x0114
874 struct display_notify_message {
875 uint32_t displayTimeout;
876 char displayMessage[100];
879 #define ACTIVATE_CALL_PLANE_MESSAGE 0x0116
880 struct activate_call_plane_message {
881 uint32_t lineInstance;
884 #define DIALED_NUMBER_MESSAGE 0x011D
885 struct dialed_number_message {
886 char dialedNumber[24];
887 uint32_t lineInstance;
888 uint32_t callReference;
892 struct alarm_message alarm;
893 struct speed_dial_stat_req_message speeddialreq;
894 struct register_message reg;
895 struct register_ack_message regack;
896 struct register_rej_message regrej;
897 struct capabilities_res_message caps;
898 struct version_res_message version;
899 struct button_template_res_message buttontemplate;
900 struct displaytext_message displaytext;
901 struct display_prompt_status_message displaypromptstatus;
902 struct clear_prompt_message clearpromptstatus;
903 struct definetimedate_message definetimedate;
904 struct start_tone_message starttone;
905 struct stop_tone_message stoptone;
906 struct speed_dial_stat_res_message speeddial;
907 struct line_state_req_message line;
908 struct line_stat_res_message linestat;
909 struct soft_key_set_res_message softkeysets;
910 struct soft_key_template_res_message softkeytemplate;
911 struct server_res_message serverres;
912 struct reset_message reset;
913 struct set_lamp_message setlamp;
914 struct set_ringer_message setringer;
915 struct call_state_message callstate;
916 struct keypad_button_message keypad;
917 struct select_soft_keys_message selectsoftkey;
918 struct activate_call_plane_message activatecallplane;
919 struct stimulus_message stimulus;
920 struct offhook_message offhook;
921 struct onhook_message onhook;
922 struct set_speaker_message setspeaker;
923 struct set_microphone_message setmicrophone;
924 struct call_info_message callinfo;
925 struct start_media_transmission_message startmedia;
926 struct stop_media_transmission_message stopmedia;
927 struct open_receive_channel_message openreceivechannel;
928 struct open_receive_channel_ack_message openreceivechannelack;
929 struct close_receive_channel_message closereceivechannel;
930 struct display_notify_message displaynotify;
931 struct dialed_number_message dialednumber;
932 struct soft_key_event_message softkeyeventmessage;
933 struct enbloc_call_message enbloccallmessage;
934 struct forward_stat_message forwardstat;
937 /* packet composition */
942 union skinny_data data;
945 /* XXX This is the combined size of the variables above. (len, res, e)
946 If more are added, this MUST change.
947 (sizeof(skinny_req) - sizeof(skinny_data)) DOES NOT WORK on all systems (amd64?). */
948 int skinny_header_size = 12;
950 /*****************************
951 * Asterisk specific globals *
952 *****************************/
954 static int skinnydebug = 0;
956 /* a hostname, portnumber, socket and such is usefull for VoIP protocols */
957 static struct sockaddr_in bindaddr;
958 static char ourhost[256];
960 static struct in_addr __ourip;
961 struct ast_hostent ahp;
963 static int skinnysock = -1;
964 static pthread_t accept_t;
965 static char context[AST_MAX_CONTEXT] = "default";
966 static char language[MAX_LANGUAGE] = "";
967 static char mohinterpret[MAX_MUSICCLASS] = "default";
968 static char mohsuggest[MAX_MUSICCLASS] = "";
969 static char cid_num[AST_MAX_EXTENSION] = "";
970 static char cid_name[AST_MAX_EXTENSION] = "";
971 static char linelabel[AST_MAX_EXTENSION] ="";
972 static char parkinglot[AST_MAX_CONTEXT] ="";
974 static ast_group_t cur_callergroup = 0;
975 static ast_group_t cur_pickupgroup = 0;
976 static int immediate = 0;
977 static int callwaiting = 0;
978 static int callreturn = 0;
979 static int threewaycalling = 0;
980 static int mwiblink = 0;
981 /* This is for flashhook transfers */
982 static int transfer = 0;
983 static int cancallforward = 0;
984 /* static int busycount = 3;*/
985 static char accountcode[AST_MAX_ACCOUNT_CODE] = "";
986 static char mailbox[AST_MAX_EXTENSION];
987 static char regexten[AST_MAX_EXTENSION];
988 static int amaflags = 0;
989 static int callnums = 1;
990 static int canreinvite = 0;
992 #define SKINNY_DEVICE_UNKNOWN -1
993 #define SKINNY_DEVICE_NONE 0
994 #define SKINNY_DEVICE_30SPPLUS 1
995 #define SKINNY_DEVICE_12SPPLUS 2
996 #define SKINNY_DEVICE_12SP 3
997 #define SKINNY_DEVICE_12 4
998 #define SKINNY_DEVICE_30VIP 5
999 #define SKINNY_DEVICE_7910 6
1000 #define SKINNY_DEVICE_7960 7
1001 #define SKINNY_DEVICE_7940 8
1002 #define SKINNY_DEVICE_7935 9
1003 #define SKINNY_DEVICE_ATA186 12 /* Cisco ATA-186 */
1004 #define SKINNY_DEVICE_7941 115
1005 #define SKINNY_DEVICE_7971 119
1006 #define SKINNY_DEVICE_7914 124 /* Expansion module */
1007 #define SKINNY_DEVICE_7985 302
1008 #define SKINNY_DEVICE_7911 307
1009 #define SKINNY_DEVICE_7961GE 308
1010 #define SKINNY_DEVICE_7941GE 309
1011 #define SKINNY_DEVICE_7931 348
1012 #define SKINNY_DEVICE_7921 365
1013 #define SKINNY_DEVICE_7906 369
1014 #define SKINNY_DEVICE_7962 404 /* Not found */
1015 #define SKINNY_DEVICE_7937 431
1016 #define SKINNY_DEVICE_7942 434
1017 #define SKINNY_DEVICE_7945 435
1018 #define SKINNY_DEVICE_7965 436
1019 #define SKINNY_DEVICE_7975 437
1020 #define SKINNY_DEVICE_7905 20000
1021 #define SKINNY_DEVICE_7920 30002
1022 #define SKINNY_DEVICE_7970 30006
1023 #define SKINNY_DEVICE_7912 30007
1024 #define SKINNY_DEVICE_7902 30008
1025 #define SKINNY_DEVICE_CIPC 30016 /* Cisco IP Communicator */
1026 #define SKINNY_DEVICE_7961 30018
1027 #define SKINNY_DEVICE_7936 30019
1028 #define SKINNY_DEVICE_SCCPGATEWAY_AN 30027 /* ??? */
1029 #define SKINNY_DEVICE_SCCPGATEWAY_BRI 30028 /* ??? */
1031 #define SKINNY_SPEAKERON 1
1032 #define SKINNY_SPEAKEROFF 2
1034 #define SKINNY_MICON 1
1035 #define SKINNY_MICOFF 2
1037 #define SKINNY_OFFHOOK 1
1038 #define SKINNY_ONHOOK 2
1039 #define SKINNY_RINGOUT 3
1040 #define SKINNY_RINGIN 4
1041 #define SKINNY_CONNECTED 5
1042 #define SKINNY_BUSY 6
1043 #define SKINNY_CONGESTION 7
1044 #define SKINNY_HOLD 8
1045 #define SKINNY_CALLWAIT 9
1046 #define SKINNY_TRANSFER 10
1047 #define SKINNY_PARK 11
1048 #define SKINNY_PROGRESS 12
1049 #define SKINNY_CALLREMOTEMULTILINE 13
1050 #define SKINNY_INVALID 14
1052 #define SKINNY_SILENCE 0x00
1053 #define SKINNY_DIALTONE 0x21
1054 #define SKINNY_BUSYTONE 0x23
1055 #define SKINNY_ALERT 0x24
1056 #define SKINNY_REORDER 0x25
1057 #define SKINNY_CALLWAITTONE 0x2D
1058 #define SKINNY_NOTONE 0x7F
1060 #define SKINNY_LAMP_OFF 1
1061 #define SKINNY_LAMP_ON 2
1062 #define SKINNY_LAMP_WINK 3
1063 #define SKINNY_LAMP_FLASH 4
1064 #define SKINNY_LAMP_BLINK 5
1066 #define SKINNY_RING_OFF 1
1067 #define SKINNY_RING_INSIDE 2
1068 #define SKINNY_RING_OUTSIDE 3
1069 #define SKINNY_RING_FEATURE 4
1071 #define SKINNY_CFWD_ALL (1 << 0)
1072 #define SKINNY_CFWD_BUSY (1 << 1)
1073 #define SKINNY_CFWD_NOANSWER (1 << 2)
1075 #define TYPE_TRUNK 1
1078 /* Skinny rtp stream modes. Do we really need this? */
1079 #define SKINNY_CX_SENDONLY 0
1080 #define SKINNY_CX_RECVONLY 1
1081 #define SKINNY_CX_SENDRECV 2
1082 #define SKINNY_CX_CONF 3
1083 #define SKINNY_CX_CONFERENCE 3
1084 #define SKINNY_CX_MUTE 4
1085 #define SKINNY_CX_INACTIVE 4
1088 static char *skinny_cxmodes[] = {
1097 /* driver scheduler */
1098 static struct sched_context *sched = NULL;
1099 static struct io_context *io;
1101 /* Protect the monitoring thread, so only one process can kill or start it, and not
1102 when it's doing something critical. */
1103 AST_MUTEX_DEFINE_STATIC(monlock);
1104 /* Protect the network socket */
1105 AST_MUTEX_DEFINE_STATIC(netlock);
1106 /* Protect the session list */
1107 AST_MUTEX_DEFINE_STATIC(sessionlock);
1108 /* Protect the device list */
1109 AST_MUTEX_DEFINE_STATIC(devicelock);
1111 /* Protect the paging device list */
1112 AST_MUTEX_DEFINE_STATIC(pagingdevicelock);
1115 /* This is the thread for the monitor which checks for input on the channels
1116 which are not currently in use. */
1117 static pthread_t monitor_thread = AST_PTHREADT_NULL;
1119 /* Wait up to 16 seconds for first digit */
1120 static int firstdigittimeout = 16000;
1122 /* How long to wait for following digits */
1123 static int gendigittimeout = 8000;
1125 /* How long to wait for an extra digit, if there is an ambiguous match */
1126 static int matchdigittimeout = 3000;
1128 struct skinny_subchannel {
1130 struct ast_channel *owner;
1131 struct ast_rtp *rtp;
1132 struct ast_rtp *vrtp;
1133 unsigned int callid;
1134 /* time_t lastouttime; */ /* Unused */
1138 /* int lastout; */ /* Unused */
1144 struct skinny_subchannel *next;
1145 struct skinny_line *parent;
1148 struct skinny_line {
1151 char label[24]; /* Label that shows next to the line buttons */
1152 char accountcode[AST_MAX_ACCOUNT_CODE];
1153 char exten[AST_MAX_EXTENSION]; /* Extension where to start */
1154 char context[AST_MAX_CONTEXT];
1155 char language[MAX_LANGUAGE];
1156 char cid_num[AST_MAX_EXTENSION]; /* Caller*ID */
1157 char cid_name[AST_MAX_EXTENSION]; /* Caller*ID */
1158 char lastcallerid[AST_MAX_EXTENSION]; /* Last Caller*ID */
1160 char call_forward_all[AST_MAX_EXTENSION];
1161 char call_forward_busy[AST_MAX_EXTENSION];
1162 char call_forward_noanswer[AST_MAX_EXTENSION];
1163 char mailbox[AST_MAX_EXTENSION];
1164 char vmexten[AST_MAX_EXTENSION];
1165 char regexten[AST_MAX_EXTENSION]; /* Extension for auto-extensions */
1166 char regcontext[AST_MAX_CONTEXT]; /* Context for auto-extensions */
1167 char parkinglot[AST_MAX_CONTEXT]; /* Parkinglot for parkedcalls */
1168 char mohinterpret[MAX_MUSICCLASS];
1169 char mohsuggest[MAX_MUSICCLASS];
1170 char lastnumberdialed[AST_MAX_EXTENSION]; /* Last number that was dialed - used for redial */
1171 int curtone; /* Current tone being played */
1172 ast_group_t callgroup;
1173 ast_group_t pickupgroup;
1174 struct ast_event_sub *mwi_event_sub; /* Event based MWI */
1177 int threewaycalling;
1182 int dnd; /* How does this affect callwait? Do we just deny a skinny_request if we're dnd? */
1191 int nonCodecCapability;
1193 int msgstate; /* voicemail message state */
1199 struct ast_codec_pref prefs;
1200 struct skinny_subchannel *sub;
1201 struct skinny_line *next;
1202 struct skinny_device *parent;
1203 struct ast_variable *chanvars; /*!< Channel variables to set for inbound call */
1206 struct skinny_speeddial {
1209 char context[AST_MAX_CONTEXT];
1210 char exten[AST_MAX_EXTENSION];
1216 struct skinny_speeddial *next;
1217 struct skinny_device *parent;
1220 struct skinny_addon {
1224 struct skinny_addon *next;
1225 struct skinny_device *parent;
1228 static struct skinny_device {
1229 /* A device containing one or more lines */
1232 char version_id[16];
1233 char exten[AST_MAX_EXTENSION]; /* Cruddy variable name, pick a better one */
1236 int lastlineinstance;
1237 int lastcallreference;
1240 struct sockaddr_in addr;
1241 struct in_addr ourip;
1242 struct skinny_line *lines;
1243 struct skinny_speeddial *speeddials;
1244 struct skinny_addon *addons;
1245 struct ast_codec_pref prefs;
1247 struct skinnysession *session;
1248 struct skinny_device *next;
1251 struct skinny_paging_device {
1254 struct skinny_device ** devices;
1255 struct skinny_paging_device *next;
1258 static struct skinnysession {
1261 struct sockaddr_in sin;
1263 char inbuf[SKINNY_MAX_PACKET];
1264 char outbuf[SKINNY_MAX_PACKET];
1265 struct skinny_device *device;
1266 struct skinnysession *next;
1269 static struct ast_channel *skinny_request(const char *type, int format, void *data, int *cause);
1270 static int skinny_devicestate(void *data);
1271 static int skinny_call(struct ast_channel *ast, char *dest, int timeout);
1272 static int skinny_hangup(struct ast_channel *ast);
1273 static int skinny_answer(struct ast_channel *ast);
1274 static struct ast_frame *skinny_read(struct ast_channel *ast);
1275 static int skinny_write(struct ast_channel *ast, struct ast_frame *frame);
1276 static int skinny_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen);
1277 static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
1278 static int skinny_senddigit_begin(struct ast_channel *ast, char digit);
1279 static int skinny_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
1280 static int handle_time_date_req_message(struct skinny_req *req, struct skinnysession *s);
1282 static const struct ast_channel_tech skinny_tech = {
1284 .description = tdesc,
1285 .capabilities = AST_FORMAT_AUDIO_MASK,
1286 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
1287 .requester = skinny_request,
1288 .devicestate = skinny_devicestate,
1289 .call = skinny_call,
1290 .hangup = skinny_hangup,
1291 .answer = skinny_answer,
1292 .read = skinny_read,
1293 .write = skinny_write,
1294 .indicate = skinny_indicate,
1295 .fixup = skinny_fixup,
1296 .send_digit_begin = skinny_senddigit_begin,
1297 .send_digit_end = skinny_senddigit_end,
1298 .bridge = ast_rtp_bridge,
1301 static int skinny_extensionstate_cb(char *context, char* exten, int state, void *data);
1303 static void *get_button_template(struct skinnysession *s, struct button_definition_template *btn)
1305 struct skinny_device *d = s->device;
1306 struct skinny_addon *a = d->addons;
1310 case SKINNY_DEVICE_30SPPLUS:
1311 case SKINNY_DEVICE_30VIP:
1312 /* 13 rows, 2 columns */
1313 for (i = 0; i < 4; i++)
1314 (btn++)->buttonDefinition = BT_CUST_LINE;
1315 (btn++)->buttonDefinition = BT_REDIAL;
1316 (btn++)->buttonDefinition = BT_VOICEMAIL;
1317 (btn++)->buttonDefinition = BT_CALLPARK;
1318 (btn++)->buttonDefinition = BT_FORWARDALL;
1319 (btn++)->buttonDefinition = BT_CONFERENCE;
1320 for (i = 0; i < 4; i++)
1321 (btn++)->buttonDefinition = BT_NONE;
1322 for (i = 0; i < 13; i++)
1323 (btn++)->buttonDefinition = BT_SPEEDDIAL;
1326 case SKINNY_DEVICE_12SPPLUS:
1327 case SKINNY_DEVICE_12SP:
1328 case SKINNY_DEVICE_12:
1329 /* 6 rows, 2 columns */
1330 for (i = 0; i < 2; i++)
1331 (btn++)->buttonDefinition = BT_CUST_LINE;
1332 for (i = 0; i < 4; i++)
1333 (btn++)->buttonDefinition = BT_SPEEDDIAL;
1334 (btn++)->buttonDefinition = BT_HOLD;
1335 (btn++)->buttonDefinition = BT_REDIAL;
1336 (btn++)->buttonDefinition = BT_TRANSFER;
1337 (btn++)->buttonDefinition = BT_FORWARDALL;
1338 (btn++)->buttonDefinition = BT_CALLPARK;
1339 (btn++)->buttonDefinition = BT_VOICEMAIL;
1341 case SKINNY_DEVICE_7910:
1342 (btn++)->buttonDefinition = BT_LINE;
1343 (btn++)->buttonDefinition = BT_HOLD;
1344 (btn++)->buttonDefinition = BT_TRANSFER;
1345 (btn++)->buttonDefinition = BT_DISPLAY;
1346 (btn++)->buttonDefinition = BT_VOICEMAIL;
1347 (btn++)->buttonDefinition = BT_CONFERENCE;
1348 (btn++)->buttonDefinition = BT_FORWARDALL;
1349 for (i = 0; i < 2; i++)
1350 (btn++)->buttonDefinition = BT_SPEEDDIAL;
1351 (btn++)->buttonDefinition = BT_REDIAL;
1353 case SKINNY_DEVICE_7960:
1354 case SKINNY_DEVICE_7961:
1355 case SKINNY_DEVICE_7961GE:
1356 case SKINNY_DEVICE_7962:
1357 case SKINNY_DEVICE_7965:
1358 for (i = 0; i < 6; i++)
1359 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1361 case SKINNY_DEVICE_7940:
1362 case SKINNY_DEVICE_7941:
1363 case SKINNY_DEVICE_7941GE:
1364 case SKINNY_DEVICE_7942:
1365 case SKINNY_DEVICE_7945:
1366 for (i = 0; i < 2; i++)
1367 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1369 case SKINNY_DEVICE_7935:
1370 case SKINNY_DEVICE_7936:
1371 for (i = 0; i < 2; i++)
1372 (btn++)->buttonDefinition = BT_LINE;
1374 case SKINNY_DEVICE_ATA186:
1375 (btn++)->buttonDefinition = BT_LINE;
1377 case SKINNY_DEVICE_7970:
1378 case SKINNY_DEVICE_7971:
1379 case SKINNY_DEVICE_7975:
1380 case SKINNY_DEVICE_CIPC:
1381 for (i = 0; i < 8; i++)
1382 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1384 case SKINNY_DEVICE_7985:
1385 /* XXX I have no idea what the buttons look like on these. */
1386 ast_log(LOG_WARNING, "Unsupported device type '%d (7985)' found.\n", d->type);
1388 case SKINNY_DEVICE_7912:
1389 case SKINNY_DEVICE_7911:
1390 case SKINNY_DEVICE_7905:
1391 (btn++)->buttonDefinition = BT_LINE;
1392 (btn++)->buttonDefinition = BT_HOLD;
1394 case SKINNY_DEVICE_7920:
1395 /* XXX I don't know if this is right. */
1396 for (i = 0; i < 4; i++)
1397 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1399 case SKINNY_DEVICE_7921:
1400 for (i = 0; i < 6; i++)
1401 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1403 case SKINNY_DEVICE_7902:
1404 ast_log(LOG_WARNING, "Unsupported device type '%d (7902)' found.\n", d->type);
1406 case SKINNY_DEVICE_7906:
1407 ast_log(LOG_WARNING, "Unsupported device type '%d (7906)' found.\n", d->type);
1409 case SKINNY_DEVICE_7931:
1410 ast_log(LOG_WARNING, "Unsupported device type '%d (7931)' found.\n", d->type);
1412 case SKINNY_DEVICE_7937:
1413 ast_log(LOG_WARNING, "Unsupported device type '%d (7937)' found.\n", d->type);
1415 case SKINNY_DEVICE_7914:
1416 ast_log(LOG_WARNING, "Unsupported device type '%d (7914)' found. Expansion module registered by itself?\n", d->type);
1418 case SKINNY_DEVICE_SCCPGATEWAY_AN:
1419 case SKINNY_DEVICE_SCCPGATEWAY_BRI:
1420 ast_log(LOG_WARNING, "Unsupported device type '%d (SCCP gateway)' found.\n", d->type);
1423 ast_log(LOG_WARNING, "Unknown device type '%d' found.\n", d->type);
1427 for (a = d->addons; a; a = a->next) {
1428 if (!strcasecmp(a->type, "7914")) {
1429 for (i = 0; i < 14; i++)
1430 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1432 ast_log(LOG_WARNING, "Unknown addon type '%s' found. Skipping.\n", a->type);
1439 static struct skinny_req *req_alloc(size_t size, int response_message)
1441 struct skinny_req *req;
1443 if (!(req = ast_calloc(1, skinny_header_size + size + 4)))
1446 req->len = htolel(size+4);
1447 req->e = htolel(response_message);
1452 static struct skinny_line *find_line_by_instance(struct skinny_device *d, int instance)
1454 struct skinny_line *l;
1456 /*Dialing from on hook or on a 7920 uses instance 0 in requests
1457 but we need to start looking at instance 1 */
1462 for (l = d->lines; l; l = l->next) {
1463 if (l->instance == instance)
1468 ast_log(LOG_WARNING, "Could not find line with instance '%d' on device '%s'\n", instance, d->name);
1473 static struct skinny_line *find_line_by_name(const char *dest)
1475 struct skinny_line *l;
1476 struct skinny_line *tmpl = NULL;
1477 struct skinny_device *d;
1481 int checkdevice = 0;
1483 ast_copy_string(line, dest, sizeof(line));
1484 at = strchr(line, '@');
1489 if (!ast_strlen_zero(device))
1492 ast_mutex_lock(&devicelock);
1493 for (d = devices; d; d = d->next) {
1494 if (checkdevice && tmpl)
1496 else if (!checkdevice) {
1497 /* This is a match, since we're checking for line on every device. */
1498 } else if (!strcasecmp(d->name, device)) {
1500 ast_verb(2, "Found device: %s\n", d->name);
1504 /* Found the device (or we don't care which device) */
1505 for (l = d->lines; l; l = l->next) {
1506 /* Search for the right line */
1507 if (!strcasecmp(l->name, line)) {
1509 ast_verb(2, "Ambiguous line name: %s\n", line);
1510 ast_mutex_unlock(&devicelock);
1517 ast_mutex_unlock(&devicelock);
1522 * implement the setvar config line
1524 static struct ast_variable *add_var(const char *buf, struct ast_variable *list)
1526 struct ast_variable *tmpvar = NULL;
1527 char *varname = ast_strdupa(buf), *varval = NULL;
1529 if ((varval = strchr(varname,'='))) {
1531 if ((tmpvar = ast_variable_new(varname, varval, ""))) {
1532 tmpvar->next = list;
1539 /* It's quicker/easier to find the subchannel when we know the instance number too */
1540 static struct skinny_subchannel *find_subchannel_by_instance_reference(struct skinny_device *d, int instance, int reference)
1542 struct skinny_line *l = find_line_by_instance(d, instance);
1543 struct skinny_subchannel *sub;
1549 /* 7920 phones set call reference to 0, so use the first
1550 sub-channel on the list.
1551 This MIGHT need more love to be right */
1555 for (sub = l->sub; sub; sub = sub->next) {
1556 if (sub->callid == reference)
1561 ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s'\n", reference, d->name);
1566 /* Find the subchannel when we only have the callid - this shouldn't happen often */
1567 static struct skinny_subchannel *find_subchannel_by_reference(struct skinny_device *d, int reference)
1569 struct skinny_line *l;
1570 struct skinny_subchannel *sub = NULL;
1572 for (l = d->lines; l; l = l->next) {
1573 for (sub = l->sub; sub; sub = sub->next) {
1574 if (sub->callid == reference)
1582 ast_log(LOG_WARNING, "Could not find any lines that contained a subchannel with reference '%d' on device '%s'\n", reference, d->name);
1585 ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s@%s'\n", reference, l->name, d->name);
1591 static struct skinny_speeddial *find_speeddial_by_instance(struct skinny_device *d, int instance, int isHint)
1593 struct skinny_speeddial *sd;
1595 for (sd = d->speeddials; sd; sd = sd->next) {
1596 if (sd->isHint == isHint && sd->instance == instance)
1601 ast_log(LOG_WARNING, "Could not find speeddial with instance '%d' on device '%s'\n", instance, d->name);
1606 static int codec_skinny2ast(enum skinny_codecs skinnycodec)
1608 switch (skinnycodec) {
1609 case SKINNY_CODEC_ALAW:
1610 return AST_FORMAT_ALAW;
1611 case SKINNY_CODEC_ULAW:
1612 return AST_FORMAT_ULAW;
1613 case SKINNY_CODEC_G723_1:
1614 return AST_FORMAT_G723_1;
1615 case SKINNY_CODEC_G729A:
1616 return AST_FORMAT_G729A;
1617 case SKINNY_CODEC_G726_32:
1618 return AST_FORMAT_G726_AAL2; /* XXX Is this right? */
1619 case SKINNY_CODEC_H261:
1620 return AST_FORMAT_H261;
1621 case SKINNY_CODEC_H263:
1622 return AST_FORMAT_H263;
1628 static int codec_ast2skinny(int astcodec)
1631 case AST_FORMAT_ALAW:
1632 return SKINNY_CODEC_ALAW;
1633 case AST_FORMAT_ULAW:
1634 return SKINNY_CODEC_ULAW;
1635 case AST_FORMAT_G723_1:
1636 return SKINNY_CODEC_G723_1;
1637 case AST_FORMAT_G729A:
1638 return SKINNY_CODEC_G729A;
1639 case AST_FORMAT_G726_AAL2: /* XXX Is this right? */
1640 return SKINNY_CODEC_G726_32;
1641 case AST_FORMAT_H261:
1642 return SKINNY_CODEC_H261;
1643 case AST_FORMAT_H263:
1644 return SKINNY_CODEC_H263;
1650 static int set_callforwards(struct skinny_line *l, const char *cfwd, int cfwdtype)
1655 if (!ast_strlen_zero(cfwd)) {
1656 if (cfwdtype & SKINNY_CFWD_ALL) {
1657 l->cfwdtype |= SKINNY_CFWD_ALL;
1658 ast_copy_string(l->call_forward_all, cfwd, sizeof(l->call_forward_all));
1660 if (cfwdtype & SKINNY_CFWD_BUSY) {
1661 l->cfwdtype |= SKINNY_CFWD_BUSY;
1662 ast_copy_string(l->call_forward_busy, cfwd, sizeof(l->call_forward_busy));
1664 if (cfwdtype & SKINNY_CFWD_NOANSWER) {
1665 l->cfwdtype |= SKINNY_CFWD_NOANSWER;
1666 ast_copy_string(l->call_forward_noanswer, cfwd, sizeof(l->call_forward_noanswer));
1669 if (cfwdtype & SKINNY_CFWD_ALL) {
1670 l->cfwdtype &= ~SKINNY_CFWD_ALL;
1671 memset(l->call_forward_all, 0, sizeof(l->call_forward_all));
1673 if (cfwdtype & SKINNY_CFWD_BUSY) {
1674 l->cfwdtype &= ~SKINNY_CFWD_BUSY;
1675 memset(l->call_forward_busy, 0, sizeof(l->call_forward_busy));
1677 if (cfwdtype & SKINNY_CFWD_NOANSWER) {
1678 l->cfwdtype &= ~SKINNY_CFWD_NOANSWER;
1679 memset(l->call_forward_noanswer, 0, sizeof(l->call_forward_noanswer));
1685 static void cleanup_stale_contexts(char *new, char *old)
1687 char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
1689 while ((oldcontext = strsep(&old, "&"))) {
1690 stalecontext = '\0';
1691 ast_copy_string(newlist, new, sizeof(newlist));
1693 while ((newcontext = strsep(&stringp, "&"))) {
1694 if (strcmp(newcontext, oldcontext) == 0) {
1695 /* This is not the context you're looking for */
1696 stalecontext = '\0';
1698 } else if (strcmp(newcontext, oldcontext)) {
1699 stalecontext = oldcontext;
1704 ast_context_destroy(ast_context_find(stalecontext), "Skinny");
1708 static void register_exten(struct skinny_line *l)
1711 char *stringp, *ext, *context;
1713 if (ast_strlen_zero(regcontext))
1716 ast_copy_string(multi, S_OR(l->regexten, l->name), sizeof(multi));
1718 while ((ext = strsep(&stringp, "&"))) {
1719 if ((context = strchr(ext, '@'))) {
1720 *context++ = '\0'; /* split ext@context */
1721 if (!ast_context_find(context)) {
1722 ast_log(LOG_WARNING, "Context %s must exist in regcontext= in skinny.conf!\n", context);
1726 context = regcontext;
1728 ast_add_extension(context, 1, ext, 1, NULL, NULL, "Noop",
1729 ast_strdup(l->name), ast_free_ptr, "Skinny");
1733 static void unregister_exten(struct skinny_line *l)
1736 char *stringp, *ext, *context;
1738 if (ast_strlen_zero(regcontext))
1741 ast_copy_string(multi, S_OR(l->regexten, l->name), sizeof(multi));
1743 while ((ext = strsep(&stringp, "&"))) {
1744 if ((context = strchr(ext, '@'))) {
1745 *context++ = '\0'; /* split ext@context */
1746 if (!ast_context_find(context)) {
1747 ast_log(LOG_WARNING, "Context %s must exist in regcontext= in skinny.conf!\n", context);
1751 context = regcontext;
1753 ast_context_remove_extension(context, ext, 1, NULL);
1757 static int skinny_register(struct skinny_req *req, struct skinnysession *s)
1759 struct skinny_device *d;
1760 struct skinny_line *l;
1761 struct skinny_speeddial *sd;
1762 struct sockaddr_in sin;
1765 ast_mutex_lock(&devicelock);
1766 for (d = devices; d; d = d->next) {
1767 if (!strcasecmp(req->data.reg.name, d->id)
1768 && ast_apply_ha(d->ha, &(s->sin))) {
1770 d->type = letohl(req->data.reg.type);
1771 if (ast_strlen_zero(d->version_id)) {
1772 ast_copy_string(d->version_id, version_id, sizeof(d->version_id));
1778 if (getsockname(s->fd, (struct sockaddr *)&sin, &slen)) {
1779 ast_log(LOG_WARNING, "Cannot get socket name\n");
1780 sin.sin_addr = __ourip;
1782 d->ourip = sin.sin_addr;
1784 for (sd = d->speeddials; sd; sd = sd->next) {
1785 sd->stateid = ast_extension_state_add(sd->context, sd->exten, skinny_extensionstate_cb, sd);
1787 for (l = d->lines; l; l = l->next) {
1789 ast_device_state_changed("Skinny/%s@%s", l->name, d->name);
1794 ast_mutex_unlock(&devicelock);
1801 static int skinny_unregister(struct skinny_req *req, struct skinnysession *s)
1803 struct skinny_device *d;
1804 struct skinny_line *l;
1805 struct skinny_speeddial *sd;
1813 for (sd = d->speeddials; sd; sd = sd->next) {
1814 if (sd->stateid > -1)
1815 ast_extension_state_del(sd->stateid, NULL);
1817 for (l = d->lines; l; l = l->next) {
1818 unregister_exten(l);
1819 ast_device_state_changed("Skinny/%s@%s", l->name, d->name);
1823 return -1; /* main loop will destroy the session */
1826 static int transmit_response(struct skinnysession *s, struct skinny_req *req)
1831 ast_log(LOG_WARNING, "Asked to transmit to a non-existent session!\n");
1835 ast_mutex_lock(&s->lock);
1838 ast_log(LOG_VERBOSE, "writing packet type %04X (%d bytes) to socket %d\n", letohl(req->e), letohl(req->len)+8, s->fd);
1840 if (letohl(req->len > SKINNY_MAX_PACKET) || letohl(req->len < 0)) {
1841 ast_log(LOG_WARNING, "transmit_response: the length of the request is out of bounds\n");
1845 memset(s->outbuf,0,sizeof(s->outbuf));
1846 memcpy(s->outbuf, req, skinny_header_size);
1847 memcpy(s->outbuf+skinny_header_size, &req->data, letohl(req->len));
1849 res = write(s->fd, s->outbuf, letohl(req->len)+8);
1851 if (res != letohl(req->len)+8) {
1852 ast_log(LOG_WARNING, "Transmit: write only sent %d out of %d bytes: %s\n", res, letohl(req->len)+8, strerror(errno));
1855 ast_log(LOG_WARNING, "Transmit: Skinny Client was lost, unregistering\n");
1856 skinny_unregister(NULL, s);
1861 ast_mutex_unlock(&s->lock);
1865 static void transmit_speaker_mode(struct skinnysession *s, int mode)
1867 struct skinny_req *req;
1869 if (!(req = req_alloc(sizeof(struct set_speaker_message), SET_SPEAKER_MESSAGE)))
1872 req->data.setspeaker.mode = htolel(mode);
1873 transmit_response(s, req);
1876 static void transmit_microphone_mode(struct skinnysession *s, int mode)
1878 struct skinny_req *req;
1880 if (!(req = req_alloc(sizeof(struct set_microphone_message), SET_MICROPHONE_MESSAGE)))
1883 req->data.setmicrophone.mode = htolel(mode);
1884 transmit_response(s, req);
1888 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)
1890 struct skinny_req *req;
1892 if (!(req = req_alloc(sizeof(struct call_info_message), CALL_INFO_MESSAGE)))
1896 ast_verb(1, "Setting Callinfo to %s(%s) from %s(%s) on %s(%d)\n", fromname, fromnum, toname, tonum, s->device->name, instance);
1899 ast_copy_string(req->data.callinfo.callingPartyName, fromname, sizeof(req->data.callinfo.callingPartyName));
1902 ast_copy_string(req->data.callinfo.callingParty, fromnum, sizeof(req->data.callinfo.callingParty));
1905 ast_copy_string(req->data.callinfo.calledPartyName, toname, sizeof(req->data.callinfo.calledPartyName));
1908 ast_copy_string(req->data.callinfo.calledParty, tonum, sizeof(req->data.callinfo.calledParty));
1910 req->data.callinfo.instance = htolel(instance);
1911 req->data.callinfo.reference = htolel(callid);
1912 req->data.callinfo.type = htolel(calltype);
1913 transmit_response(s, req);
1916 static void transmit_connect(struct skinnysession *s, struct skinny_subchannel *sub)
1918 struct skinny_req *req;
1919 struct skinny_line *l = sub->parent;
1920 struct ast_format_list fmt;
1922 if (!(req = req_alloc(sizeof(struct open_receive_channel_message), OPEN_RECEIVE_CHANNEL_MESSAGE)))
1925 fmt = ast_codec_pref_getsize(&l->prefs, ast_best_codec(l->capability));
1927 req->data.openreceivechannel.conferenceId = htolel(sub->callid);
1928 req->data.openreceivechannel.partyId = htolel(sub->callid);
1929 req->data.openreceivechannel.packets = htolel(fmt.cur_ms);
1930 req->data.openreceivechannel.capability = htolel(codec_ast2skinny(fmt.bits));
1931 req->data.openreceivechannel.echo = htolel(0);
1932 req->data.openreceivechannel.bitrate = htolel(0);
1933 transmit_response(s, req);
1936 static void transmit_tone(struct skinnysession *s, int tone, int instance, int reference)
1938 struct skinny_req *req;
1940 if (tone == SKINNY_NOTONE) {
1941 /* This is bad, mmm'kay? */
1946 if (!(req = req_alloc(sizeof(struct start_tone_message), START_TONE_MESSAGE)))
1948 req->data.starttone.tone = htolel(tone);
1949 req->data.starttone.instance = htolel(instance);
1950 req->data.starttone.reference = htolel(reference);
1952 if (!(req = req_alloc(sizeof(struct stop_tone_message), STOP_TONE_MESSAGE)))
1954 req->data.stoptone.instance = htolel(instance);
1955 req->data.stoptone.reference = htolel(reference);
1959 req->data.starttone.tone = htolel(tone);
1961 transmit_response(s, req);
1964 static void transmit_selectsoftkeys(struct skinnysession *s, int instance, int callid, int softkey)
1966 struct skinny_req *req;
1968 if (!(req = req_alloc(sizeof(struct select_soft_keys_message), SELECT_SOFT_KEYS_MESSAGE)))
1971 req->data.selectsoftkey.instance = htolel(instance);
1972 req->data.selectsoftkey.reference = htolel(callid);
1973 req->data.selectsoftkey.softKeySetIndex = htolel(softkey);
1974 req->data.selectsoftkey.validKeyMask = htolel(0xFFFFFFFF);
1975 transmit_response(s, req);
1978 static void transmit_lamp_indication(struct skinnysession *s, int stimulus, int instance, int indication)
1980 struct skinny_req *req;
1982 if (!(req = req_alloc(sizeof(struct set_lamp_message), SET_LAMP_MESSAGE)))
1985 req->data.setlamp.stimulus = htolel(stimulus);
1986 req->data.setlamp.stimulusInstance = htolel(instance);
1987 req->data.setlamp.deviceStimulus = htolel(indication);
1988 transmit_response(s, req);
1991 static void transmit_ringer_mode(struct skinnysession *s, int mode)
1993 struct skinny_req *req;
1996 ast_verb(1, "Setting ringer mode to '%d'.\n", mode);
1998 if (!(req = req_alloc(sizeof(struct set_ringer_message), SET_RINGER_MESSAGE)))
2001 req->data.setringer.ringerMode = htolel(mode);
2002 /* XXX okay, I don't quite know what this is, but here's what happens (on a 7960).
2003 Note: The phone will always show as ringing on the display.
2005 1: phone will audibly ring over and over
2006 2: phone will audibly ring only once
2007 any other value, will NOT cause the phone to audibly ring
2009 req->data.setringer.unknown1 = htolel(1);
2010 /* XXX the value here doesn't seem to change anything. Must be higher than 0.
2011 Perhaps a packet capture can shed some light on this. */
2012 req->data.setringer.unknown2 = htolel(1);
2013 transmit_response(s, req);
2016 static void transmit_displaymessage(struct skinnysession *s, const char *text, int instance, int reference)
2018 struct skinny_req *req;
2021 if (!(req = req_alloc(0, CLEAR_DISPLAY_MESSAGE)))
2024 req->data.clearpromptstatus.lineInstance = instance;
2025 req->data.clearpromptstatus.callReference = reference;
2028 ast_verb(1, "Clearing Display\n");
2030 if (!(req = req_alloc(sizeof(struct displaytext_message), DISPLAYTEXT_MESSAGE)))
2033 ast_copy_string(req->data.displaytext.text, text, sizeof(req->data.displaytext.text));
2035 ast_verb(1, "Displaying message '%s'\n", req->data.displaytext.text);
2038 transmit_response(s, req);
2041 static void transmit_displaynotify(struct skinnysession *s, const char *text, int t)
2043 struct skinny_req *req;
2045 if (!(req = req_alloc(sizeof(struct display_notify_message), DISPLAY_NOTIFY_MESSAGE)))
2048 ast_copy_string(req->data.displaynotify.displayMessage, text, sizeof(req->data.displaynotify.displayMessage));
2049 req->data.displaynotify.displayTimeout = htolel(t);
2052 ast_verb(1, "Displaying notify '%s'\n", text);
2054 transmit_response(s, req);
2057 static void transmit_displaypromptstatus(struct skinnysession *s, const char *text, int t, int instance, int callid)
2059 struct skinny_req *req;
2062 if (!(req = req_alloc(sizeof(struct clear_prompt_message), CLEAR_PROMPT_MESSAGE)))
2065 req->data.clearpromptstatus.lineInstance = htolel(instance);
2066 req->data.clearpromptstatus.callReference = htolel(callid);
2069 ast_verb(1, "Clearing Prompt\n");
2071 if (!(req = req_alloc(sizeof(struct display_prompt_status_message), DISPLAY_PROMPT_STATUS_MESSAGE)))
2074 ast_copy_string(req->data.displaypromptstatus.promptMessage, text, sizeof(req->data.displaypromptstatus.promptMessage));
2075 req->data.displaypromptstatus.messageTimeout = htolel(t);
2076 req->data.displaypromptstatus.lineInstance = htolel(instance);
2077 req->data.displaypromptstatus.callReference = htolel(callid);
2080 ast_verb(1, "Displaying Prompt Status '%s'\n", text);
2083 transmit_response(s, req);
2086 static void transmit_dialednumber(struct skinnysession *s, const char *text, int instance, int callid)
2088 struct skinny_req *req;
2090 if (!(req = req_alloc(sizeof(struct dialed_number_message), DIALED_NUMBER_MESSAGE)))
2093 ast_copy_string(req->data.dialednumber.dialedNumber, text, sizeof(req->data.dialednumber.dialedNumber));
2094 req->data.dialednumber.lineInstance = htolel(instance);
2095 req->data.dialednumber.callReference = htolel(callid);
2097 transmit_response(s, req);
2100 static void transmit_callstate(struct skinnysession *s, int instance, int state, unsigned callid)
2102 struct skinny_req *req;
2104 if (state == SKINNY_ONHOOK) {
2105 if (!(req = req_alloc(sizeof(struct close_receive_channel_message), CLOSE_RECEIVE_CHANNEL_MESSAGE)))
2108 req->data.closereceivechannel.conferenceId = htolel(callid);
2109 req->data.closereceivechannel.partyId = htolel(callid);
2110 transmit_response(s, req);
2112 if (!(req = req_alloc(sizeof(struct stop_media_transmission_message), STOP_MEDIA_TRANSMISSION_MESSAGE)))
2115 req->data.stopmedia.conferenceId = htolel(callid);
2116 req->data.stopmedia.passThruPartyId = htolel(callid);
2117 transmit_response(s, req);
2119 transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
2121 transmit_displaypromptstatus(s, NULL, 0, instance, callid);
2124 if (!(req = req_alloc(sizeof(struct call_state_message), CALL_STATE_MESSAGE)))
2127 req->data.callstate.callState = htolel(state);
2128 req->data.callstate.lineInstance = htolel(instance);
2129 req->data.callstate.callReference = htolel(callid);
2130 transmit_response(s, req);
2132 if (state == SKINNY_ONHOOK) {
2133 transmit_selectsoftkeys(s, 0, 0, KEYDEF_ONHOOK);
2136 if (state == SKINNY_OFFHOOK || state == SKINNY_ONHOOK) {
2137 if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
2140 req->data.activatecallplane.lineInstance = htolel(instance);
2141 transmit_response(s, req);
2146 static void transmit_cfwdstate(struct skinnysession *s, struct skinny_line *l)
2148 struct skinny_req *req;
2151 if (!(req = req_alloc(sizeof(struct forward_stat_message), FORWARD_STAT_MESSAGE)))
2154 if (l->cfwdtype & SKINNY_CFWD_ALL) {
2155 if (!ast_strlen_zero(l->call_forward_all)) {
2156 ast_copy_string(req->data.forwardstat.fwdallnum, l->call_forward_all, sizeof(req->data.forwardstat.fwdallnum));
2157 req->data.forwardstat.fwdall = htolel(1);
2160 req->data.forwardstat.fwdall = htolel(0);
2163 if (l->cfwdtype & SKINNY_CFWD_BUSY) {
2164 if (!ast_strlen_zero(l->call_forward_busy)) {
2165 ast_copy_string(req->data.forwardstat.fwdbusynum, l->call_forward_busy, sizeof(req->data.forwardstat.fwdbusynum));
2166 req->data.forwardstat.fwdbusy = htolel(1);
2169 req->data.forwardstat.fwdbusy = htolel(0);
2172 if (l->cfwdtype & SKINNY_CFWD_NOANSWER) {
2173 if (!ast_strlen_zero(l->call_forward_noanswer)) {
2174 ast_copy_string(req->data.forwardstat.fwdnoanswernum, l->call_forward_noanswer, sizeof(req->data.forwardstat.fwdnoanswernum));
2175 req->data.forwardstat.fwdnoanswer = htolel(1);
2178 req->data.forwardstat.fwdnoanswer = htolel(0);
2181 req->data.forwardstat.lineNumber = htolel(l->instance);
2183 req->data.forwardstat.activeforward = htolel(7);
2185 req->data.forwardstat.activeforward = htolel(0);
2187 transmit_response(s, req);
2190 static int skinny_extensionstate_cb(char *context, char *exten, int state, void *data)
2192 struct skinny_speeddial *sd = data;
2193 struct skinny_device *d = sd->parent;
2194 struct skinnysession *s = d->session;
2195 char hint[AST_MAX_EXTENSION];
2196 int callstate = SKINNY_CALLREMOTEMULTILINE;
2197 int lamp = SKINNY_LAMP_OFF;
2200 case AST_EXTENSION_DEACTIVATED: /* Retry after a while */
2201 case AST_EXTENSION_REMOVED: /* Extension is gone */
2202 ast_verb(2, "Extension state: Watcher for hint %s %s. Notify Device %s\n", exten, state == AST_EXTENSION_DEACTIVATED ? "deactivated" : "removed", d->name);
2204 callstate = SKINNY_ONHOOK;
2205 lamp = SKINNY_LAMP_OFF;
2207 case AST_EXTENSION_RINGING:
2208 case AST_EXTENSION_UNAVAILABLE:
2209 callstate = SKINNY_RINGIN;
2210 lamp = SKINNY_LAMP_BLINK;
2212 case AST_EXTENSION_BUSY: /* callstate = SKINNY_BUSY wasn't wanting to work - I'll settle for this */
2213 case AST_EXTENSION_INUSE:
2214 callstate = SKINNY_CALLREMOTEMULTILINE;
2215 lamp = SKINNY_LAMP_ON;
2217 case AST_EXTENSION_ONHOLD:
2218 callstate = SKINNY_HOLD;
2219 lamp = SKINNY_LAMP_WINK;
2221 case AST_EXTENSION_NOT_INUSE:
2223 callstate = SKINNY_ONHOOK;
2224 lamp = SKINNY_LAMP_OFF;
2228 if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, sd->context, sd->exten)) {
2229 /* If they are not registered, we will override notification and show no availability */
2230 if (ast_device_state(hint) == AST_DEVICE_UNAVAILABLE) {
2231 callstate = SKINNY_ONHOOK;
2232 lamp = SKINNY_LAMP_FLASH;
2236 transmit_lamp_indication(s, STIMULUS_LINE, sd->instance, lamp);
2237 transmit_callstate(s, sd->instance, callstate, 0);
2238 sd->laststate = state;
2243 static void mwi_event_cb(const struct ast_event *event, void *userdata)
2245 /* This module does not handle MWI in an event-based manner. However, it
2246 * subscribes to MWI for each mailbox that is configured so that the core
2247 * knows that we care about it. Then, chan_skinny will get the MWI from the
2248 * event cache instead of checking the mailbox directly. */
2251 static int has_voicemail(struct skinny_line *l)
2254 struct ast_event *event;
2255 char *mailbox, *context;
2257 context = mailbox = ast_strdupa(l->mailbox);
2258 strsep(&context, "@");
2259 if (ast_strlen_zero(context))
2260 context = "default";
2262 event = ast_event_get_cached(AST_EVENT_MWI,
2263 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
2264 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
2265 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
2269 new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
2270 ast_event_destroy(event);
2272 new_msgs = ast_app_has_voicemail(l->mailbox, NULL);
2277 static void do_housekeeping(struct skinnysession *s)
2279 int device_lamp = 0;
2280 struct skinny_device *d = s->device;
2281 struct skinny_line *l;
2283 /* Update time on device */
2284 handle_time_date_req_message(NULL, s);
2286 /* Set MWI on individual lines */
2287 for (l = d->lines; l; l = l->next) {
2288 if (has_voicemail(l)) {
2290 ast_verb(1, "Checking for voicemail Skinny %s@%s\n", l->name, d->name);
2292 ast_verb(1, "Skinny %s@%s has voicemail!\n", l->name, d->name);
2293 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, l->instance, l->mwiblink?SKINNY_LAMP_BLINK:SKINNY_LAMP_ON);
2296 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, l->instance, SKINNY_LAMP_OFF);
2299 /* If at least one line has VM, turn the device level lamp on */
2301 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, 0, SKINNY_LAMP_ON);
2303 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, 0, SKINNY_LAMP_OFF);
2306 /* I do not believe skinny can deal with video.
2307 Anyone know differently? */
2308 /* Yes, it can. Currently 7985 and Cisco VT Advantage do video. */
2309 static enum ast_rtp_get_result skinny_get_vrtp_peer(struct ast_channel *c, struct ast_rtp **rtp)
2311 struct skinny_subchannel *sub = NULL;
2313 if (!(sub = c->tech_pvt) || !(sub->vrtp))
2314 return AST_RTP_GET_FAILED;
2318 return AST_RTP_TRY_NATIVE;
2321 static enum ast_rtp_get_result skinny_get_rtp_peer(struct ast_channel *c, struct ast_rtp **rtp)
2323 struct skinny_subchannel *sub = NULL;
2324 struct skinny_line *l;
2325 enum ast_rtp_get_result res = AST_RTP_TRY_NATIVE;
2328 ast_verb(1, "skinny_get_rtp_peer() Channel = %s\n", c->name);
2331 if (!(sub = c->tech_pvt))
2332 return AST_RTP_GET_FAILED;
2334 ast_mutex_lock(&sub->lock);
2337 ast_mutex_unlock(&sub->lock);
2338 return AST_RTP_GET_FAILED;
2345 if (!l->canreinvite || l->nat){
2346 res = AST_RTP_TRY_PARTIAL;
2348 ast_verb(1, "skinny_get_rtp_peer() Using AST_RTP_TRY_PARTIAL \n");
2351 ast_mutex_unlock(&sub->lock);
2357 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)
2359 struct skinny_subchannel *sub;
2360 struct skinny_line *l;
2361 struct skinny_device *d;
2362 struct skinnysession *s;
2363 struct ast_format_list fmt;
2364 struct sockaddr_in us;
2365 struct sockaddr_in them;
2366 struct skinny_req *req;
2370 if (c->_state != AST_STATE_UP)
2382 ast_rtp_get_peer(rtp, &them);
2384 /* Shutdown any early-media or previous media on re-invite */
2385 if (!(req = req_alloc(sizeof(struct stop_media_transmission_message), STOP_MEDIA_TRANSMISSION_MESSAGE)))
2388 req->data.stopmedia.conferenceId = htolel(sub->callid);
2389 req->data.stopmedia.passThruPartyId = htolel(sub->callid);
2390 transmit_response(s, req);
2393 ast_verb(1, "Peerip = %s:%d\n", ast_inet_ntoa(them.sin_addr), ntohs(them.sin_port));
2395 if (!(req = req_alloc(sizeof(struct start_media_transmission_message), START_MEDIA_TRANSMISSION_MESSAGE)))
2398 fmt = ast_codec_pref_getsize(&l->prefs, ast_best_codec(l->capability));
2401 ast_verb(1, "Setting payloadType to '%d' (%d ms)\n", fmt.bits, fmt.cur_ms);
2403 req->data.startmedia.conferenceId = htolel(sub->callid);
2404 req->data.startmedia.passThruPartyId = htolel(sub->callid);
2405 if (!(l->canreinvite) || (l->nat)){
2406 ast_rtp_get_us(rtp, &us);
2407 req->data.startmedia.remoteIp = htolel(d->ourip.s_addr);
2408 req->data.startmedia.remotePort = htolel(ntohs(us.sin_port));
2410 req->data.startmedia.remoteIp = htolel(them.sin_addr.s_addr);
2411 req->data.startmedia.remotePort = htolel(ntohs(them.sin_port));
2413 req->data.startmedia.packetSize = htolel(fmt.cur_ms);
2414 req->data.startmedia.payloadType = htolel(codec_ast2skinny(fmt.bits));
2415 req->data.startmedia.qualifier.precedence = htolel(127);
2416 req->data.startmedia.qualifier.vad = htolel(0);
2417 req->data.startmedia.qualifier.packets = htolel(0);
2418 req->data.startmedia.qualifier.bitRate = htolel(0);
2419 transmit_response(s, req);
2423 /* Need a return here to break the bridge */
2427 static struct ast_rtp_protocol skinny_rtp = {
2429 .get_rtp_info = skinny_get_rtp_peer,
2430 .get_vrtp_info = skinny_get_vrtp_peer,
2431 .set_rtp_peer = skinny_set_rtp_peer,
2434 static char *handle_skinny_set_debug_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2438 e->command = "skinny set debug [off]";
2440 "Usage: skinny set debug [off]\n"
2441 " Enables/Disables dumping of Skinny packets for debugging purposes\n";
2447 if (a->argc < 3 || a->argc > 4)
2448 return CLI_SHOWUSAGE;
2452 ast_cli(a->fd, "Skinny Debugging Enabled\n");
2454 } else if (!strncasecmp(a->argv[3], "off", 3)) {
2456 ast_cli(a->fd, "Skinny Debugging Disabled\n");
2459 return CLI_SHOWUSAGE;
2463 static char *handle_skinny_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2467 e->command = "skinny set debug {on|off}";
2469 "Usage: skinny set debug {on|off}\n"
2470 " Enables/Disables dumping of Skinny packets for debugging purposes\n";
2476 if (a->argc != e->args)
2477 return CLI_SHOWUSAGE;
2479 if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
2481 ast_cli(a->fd, "Skinny Debugging Enabled\n");
2483 } else if (!strncasecmp(a->argv[e->args - 1], "off", 3)) {
2485 ast_cli(a->fd, "Skinny Debugging Disabled\n");
2488 return CLI_SHOWUSAGE;
2492 static char *complete_skinny_devices(const char *word, int state)
2494 struct skinny_device *d;
2495 char *result = NULL;
2496 int wordlen = strlen(word), which = 0;
2498 for (d = devices; d && !result; d = d->next) {
2499 if (!strncasecmp(word, d->id, wordlen) && ++which > state)
2500 result = ast_strdup(d->id);
2506 static char *complete_skinny_show_device(const char *line, const char *word, int pos, int state)
2508 return (pos == 3 ? ast_strdup(complete_skinny_devices(word, state)) : NULL);
2511 static char *complete_skinny_reset(const char *line, const char *word, int pos, int state)
2513 return (pos == 2 ? ast_strdup(complete_skinny_devices(word, state)) : NULL);
2516 static char *complete_skinny_show_line(const char *line, const char *word, int pos, int state)
2518 struct skinny_device *d;
2519 struct skinny_line *l;
2520 char *result = NULL;
2521 int wordlen = strlen(word), which = 0;
2526 for (d = devices; d && !result; d = d->next) {
2527 for (l = d->lines; l && !result; l = l->next) {
2528 if (!strncasecmp(word, l->name, wordlen) && ++which > state)
2529 result = ast_strdup(l->name);
2536 static char *handle_skinny_reset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2538 struct skinny_device *d;
2539 struct skinny_req *req;
2543 e->command = "skinny reset";
2545 "Usage: skinny reset <DeviceId|DeviceName|all> [restart]\n"
2546 " Causes a Skinny device to reset itself, optionally with a full restart\n";
2549 return complete_skinny_reset(a->line, a->word, a->pos, a->n);
2552 if (a->argc < 3 || a->argc > 4)
2553 return CLI_SHOWUSAGE;
2555 ast_mutex_lock(&devicelock);
2557 for (d = devices; d; d = d->next) {
2558 int fullrestart = 0;
2559 if (!strcasecmp(a->argv[2], d->id) || !strcasecmp(a->argv[2], d->name) || !strcasecmp(a->argv[2], "all")) {
2563 if (!(req = req_alloc(sizeof(struct reset_message), RESET_MESSAGE)))
2566 if (a->argc == 4 && !strcasecmp(a->argv[3], "restart"))
2570 req->data.reset.resetType = 2;
2572 req->data.reset.resetType = 1;
2574 ast_verb(3, "%s device %s.\n", (fullrestart) ? "Restarting" : "Resetting", d->id);
2575 transmit_response(d->session, req);
2578 ast_mutex_unlock(&devicelock);
2582 static char *device2str(int type)
2587 case SKINNY_DEVICE_NONE:
2589 case SKINNY_DEVICE_30SPPLUS:
2591 case SKINNY_DEVICE_12SPPLUS:
2593 case SKINNY_DEVICE_12SP:
2595 case SKINNY_DEVICE_12:
2597 case SKINNY_DEVICE_30VIP:
2599 case SKINNY_DEVICE_7910:
2601 case SKINNY_DEVICE_7960:
2603 case SKINNY_DEVICE_7940:
2605 case SKINNY_DEVICE_7935:
2607 case SKINNY_DEVICE_ATA186:
2609 case SKINNY_DEVICE_7941:
2611 case SKINNY_DEVICE_7971:
2613 case SKINNY_DEVICE_7914:
2615 case SKINNY_DEVICE_7985:
2617 case SKINNY_DEVICE_7911:
2619 case SKINNY_DEVICE_7961GE:
2621 case SKINNY_DEVICE_7941GE:
2623 case SKINNY_DEVICE_7931:
2625 case SKINNY_DEVICE_7921:
2627 case SKINNY_DEVICE_7906:
2629 case SKINNY_DEVICE_7962:
2631 case SKINNY_DEVICE_7937:
2633 case SKINNY_DEVICE_7942:
2635 case SKINNY_DEVICE_7945:
2637 case SKINNY_DEVICE_7965:
2639 case SKINNY_DEVICE_7975:
2641 case SKINNY_DEVICE_7905:
2643 case SKINNY_DEVICE_7920:
2645 case SKINNY_DEVICE_7970:
2647 case SKINNY_DEVICE_7912:
2649 case SKINNY_DEVICE_7902:
2651 case SKINNY_DEVICE_CIPC:
2652 return "IP Communicator";
2653 case SKINNY_DEVICE_7961:
2655 case SKINNY_DEVICE_7936:
2657 case SKINNY_DEVICE_SCCPGATEWAY_AN:
2658 return "SCCPGATEWAY_AN";
2659 case SKINNY_DEVICE_SCCPGATEWAY_BRI:
2660 return "SCCPGATEWAY_BRI";
2661 case SKINNY_DEVICE_UNKNOWN:
2664 if (!(tmp = ast_threadstorage_get(&device2str_threadbuf, DEVICE2STR_BUFSIZE)))
2666 snprintf(tmp, DEVICE2STR_BUFSIZE, "UNKNOWN-%d", type);
2671 /*! \brief Print codec list from preference to CLI/manager */
2672 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
2676 for(x = 0; x < 32 ; x++) {
2677 codec = ast_codec_pref_index(pref, x);
2680 ast_cli(fd, "%s", ast_getformatname(codec));
2681 ast_cli(fd, ":%d", pref->framing[x]);
2682 if (x < 31 && ast_codec_pref_index(pref, x + 1))
2686 ast_cli(fd, "none");
2689 static char *handle_skinny_show_devices(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2691 struct skinny_device *d;
2692 struct skinny_line *l;
2696 e->command = "skinny show devices";
2698 "Usage: skinny show devices\n"
2699 " Lists all devices known to the Skinny subsystem.\n";
2706 return CLI_SHOWUSAGE;
2708 ast_mutex_lock(&devicelock);
2710 ast_cli(a->fd, "Name DeviceId IP Type R NL\n");
2711 ast_cli(a->fd, "-------------------- ---------------- --------------- --------------- - --\n");
2713 for (d = devices; d; d = d->next) {
2716 for (l = d->lines; l; l = l->next)
2719 ast_cli(a->fd, "%-20s %-16s %-15s %-15s %c %2d\n",
2722 d->session?ast_inet_ntoa(d->session->sin.sin_addr):"",
2723 device2str(d->type),
2724 d->registered?'Y':'N',
2728 ast_mutex_unlock(&devicelock);
2733 /*! \brief Show device information */
2734 static char *handle_skinny_show_device(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2736 struct skinny_device *d;
2737 struct skinny_line *l;
2738 struct skinny_speeddial *sd;
2739 struct skinny_addon *sa;
2743 e->command = "skinny show device";
2745 "Usage: skinny show device <DeviceId|DeviceName>\n"
2746 " Lists all deviceinformation of a specific device known to the Skinny subsystem.\n";
2749 return complete_skinny_show_device(a->line, a->word, a->pos, a->n);
2753 return CLI_SHOWUSAGE;
2755 ast_mutex_lock(&devicelock);
2756 for (d = devices; d; d = d->next) {
2757 if (!strcasecmp(a->argv[3], d->id) || !strcasecmp(a->argv[3], d->name)) {
2758 int numlines = 0, numaddons = 0, numspeeddials = 0;
2760 for (l = d->lines; l; l = l->next)
2763 ast_cli(a->fd, "Name: %s\n", d->name);
2764 ast_cli(a->fd, "Id: %s\n", d->id);
2765 ast_cli(a->fd, "version: %s\n", S_OR(d->version_id, "Unknown"));
2766 ast_cli(a->fd, "Ip address: %s\n", (d->session ? ast_inet_ntoa(d->session->sin.sin_addr) : "Unknown"));
2767 ast_cli(a->fd, "Port: %d\n", (d->session ? ntohs(d->session->sin.sin_port) : 0));
2768 ast_cli(a->fd, "Device Type: %s\n", device2str(d->type));
2769 ast_cli(a->fd, "Registered: %s\n", (d->registered ? "Yes" : "No"));
2770 ast_cli(a->fd, "Lines: %d\n", numlines);
2771 for (l = d->lines; l; l = l->next)
2772 ast_cli(a->fd, " %s (%s)\n", l->name, l->label);
2773 for (sa = d->addons; sa; sa = sa->next)
2775 ast_cli(a->fd, "Addons: %d\n", numaddons);
2776 for (sa = d->addons; sa; sa = sa->next)
2777 ast_cli(a->fd, " %s\n", sa->type);
2778 for (sd = d->speeddials; sd; sd = sd->next)
2780 ast_cli(a->fd, "Speeddials: %d\n", numspeeddials);
2781 for (sd = d->speeddials; sd; sd = sd->next)
2782 ast_cli(a->fd, " %s (%s) ishint: %d\n", sd->exten, sd->label, sd->isHint);
2785 ast_mutex_unlock(&devicelock);
2789 static char *handle_skinny_show_lines(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2791 struct skinny_device *d;
2792 struct skinny_line *l;
2796 e->command = "skinny show lines";
2798 "Usage: skinny show lines\n"
2799 " Lists all lines known to the Skinny subsystem.\n";
2806 return CLI_SHOWUSAGE;
2808 ast_mutex_lock(&devicelock);
2810 ast_cli(a->fd, "Device Name Instance Name Label \n");
2811 ast_cli(a->fd, "-------------------- -------- -------------------- --------------------\n");
2812 for (d = devices; d; d = d->next) {
2813 for (l = d->lines; l; l = l->next) {
2814 ast_cli(a->fd, "%-20s %8d %-20s %-20s\n",
2822 ast_mutex_unlock(&devicelock);
2826 /*! \brief List line information. */
2827 static char *handle_skinny_show_line(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2829 struct skinny_device *d;
2830 struct skinny_line *l;
2831 char codec_buf[512];
2832 char group_buf[256];
2836 e->command = "skinny show line";
2838 "Usage: skinny show line <Line> [ on <DeviceID|DeviceName> ]\n"
2839 " List all lineinformation of a specific line known to the Skinny subsystem.\n";
2842 return complete_skinny_show_line(a->line, a->word, a->pos, a->n);
2846 return CLI_SHOWUSAGE;
2848 ast_mutex_lock(&devicelock);
2850 /* Show all lines matching the one supplied */
2851 for (d = devices; d; d = d->next) {
2852 if (a->argc == 6 && (strcasecmp(a->argv[5], d->id) && strcasecmp(a->argv[5], d->name)))
2854 for (l = d->lines; l; l = l->next) {
2855 if (strcasecmp(a->argv[3], l->name))
2857 ast_cli(a->fd, "Line: %s\n", l->name);
2858 ast_cli(a->fd, "On Device: %s\n", d->name);
2859 ast_cli(a->fd, "Line Label: %s\n", l->label);
2860 ast_cli(a->fd, "Extension: %s\n", S_OR(l->exten, "<not set>"));
2861 ast_cli(a->fd, "Context: %s\n", l->context);
2862 ast_cli(a->fd, "CallGroup: %s\n", ast_print_group(group_buf, sizeof(group_buf), l->callgroup));
2863 ast_cli(a->fd, "PickupGroup: %s\n", ast_print_group(group_buf, sizeof(group_buf), l->pickupgroup));
2864 ast_cli(a->fd, "Language: %s\n", S_OR(l->language, "<not set>"));
2865 ast_cli(a->fd, "Accountcode: %s\n", S_OR(l->accountcode, "<not set>"));
2866 ast_cli(a->fd, "AmaFlag: %s\n", ast_cdr_flags2str(l->amaflags));
2867 ast_cli(a->fd, "CallerId Number: %s\n", S_OR(l->cid_num, "<not set>"));
2868 ast_cli(a->fd, "CallerId Name: %s\n", S_OR(l->cid_name, "<not set>"));
2869 ast_cli(a->fd, "Hide CallerId: %s\n", (l->hidecallerid ? "Yes" : "No"));
2870 ast_cli(a->fd, "CFwdAll: %s\n", S_COR((l->cfwdtype & SKINNY_CFWD_ALL), l->call_forward_all, "<not set>"));
2871 ast_cli(a->fd, "CFwdBusy: %s\n", S_COR((l->cfwdtype & SKINNY_CFWD_BUSY), l->call_forward_busy, "<not set>"));
2872 ast_cli(a->fd, "CFwdNoAnswer: %s\n", S_COR((l->cfwdtype & SKINNY_CFWD_NOANSWER), l->call_forward_noanswer, "<not set>"));
2873 ast_cli(a->fd, "VoicemailBox: %s\n", S_OR(l->mailbox, "<not set>"));
2874 ast_cli(a->fd, "VoicemailNumber: %s\n", S_OR(l->vmexten, "<not set>"));
2875 ast_cli(a->fd, "MWIblink: %d\n", l->mwiblink);
2876 ast_cli(a->fd, "Regextension: %s\n", S_OR(l->regexten, "<not set>"));
2877 ast_cli(a->fd, "Regcontext: %s\n", S_OR(l->regcontext, "<not set>"));
2878 ast_cli(a->fd, "MoHInterpret: %s\n", S_OR(l->mohinterpret, "<not set>"));
2879 ast_cli(a->fd, "MoHSuggest: %s\n", S_OR(l->mohsuggest, "<not set>"));
2880 ast_cli(a->fd, "Last dialed nr: %s\n", S_OR(l->lastnumberdialed, "<no calls made yet>"));
2881 ast_cli(a->fd, "Last CallerID: %s\n", S_OR(l->lastcallerid, "<not set>"));
2882 ast_cli(a->fd, "Transfer enabled: %s\n", (l->transfer ? "Yes" : "No"));
2883 ast_cli(a->fd, "Callwaiting: %s\n", (l->callwaiting ? "Yes" : "No"));
2884 ast_cli(a->fd, "3Way Calling: %s\n", (l->threewaycalling ? "Yes" : "No"));
2885 ast_cli(a->fd, "Can forward: %s\n", (l->cancallforward ? "Yes" : "No"));
2886 ast_cli(a->fd, "Do Not Disturb: %s\n", (l->dnd ? "Yes" : "No"));
2887 ast_cli(a->fd, "NAT: %s\n", (l->nat ? "Yes" : "No"));
2888 ast_cli(a->fd, "immediate: %s\n", (l->immediate ? "Yes" : "No"));
2889 ast_cli(a->fd, "Group: %d\n", l->group);
2890 ast_cli(a->fd, "Codecs: ");
2891 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) - 1, l->capability);
2892 ast_cli(a->fd, "%s\n", codec_buf);
2893 ast_cli(a->fd, "Codec Order: (");
2894 print_codec_to_cli(a->fd, &l->prefs);
2895 ast_cli(a->fd, ")\n");
2896 ast_cli(a->fd, "\n");
2900 ast_mutex_unlock(&devicelock);
2904 /*! \brief List global settings for the Skinny subsystem. */
2905 static char *handle_skinny_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2909 e->command = "skinny show settings";
2911 "Usage: skinny show settings\n"
2912 " Lists all global configuration settings of the Skinny subsystem.\n";
2919 return CLI_SHOWUSAGE;
2921 ast_cli(a->fd, "\nGlobal Settings:\n");
2922 ast_cli(a->fd, " Skinny Port: %d\n", ntohs(bindaddr.sin_port));
2923 ast_cli(a->fd, " Bindaddress: %s\n", ast_inet_ntoa(bindaddr.sin_addr));
2924 ast_cli(a->fd, " KeepAlive: %d\n", keep_alive);
2925 ast_cli(a->fd, " Date Format: %s\n", date_format);
2926 ast_cli(a->fd, " Voice Mail Extension: %s\n", S_OR(vmexten, "(not set)"));
2927 ast_cli(a->fd, " Reg. context: %s\n", S_OR(regcontext, "(not set)"));
2928 ast_cli(a->fd, " Jitterbuffer enabled: %s\n", (ast_test_flag(&global_jbconf, AST_JB_ENABLED) ? "Yes" : "No"));
2929 ast_cli(a->fd, " Jitterbuffer forced: %s\n", (ast_test_flag(&global_jbconf, AST_JB_FORCED) ? "Yes" : "No"));
2930 ast_cli(a->fd, " Jitterbuffer max size: %ld\n", global_jbconf.max_size);
2931 ast_cli(a->fd, " Jitterbuffer resync: %ld\n", global_jbconf.resync_threshold);
2932 ast_cli(a->fd, " Jitterbuffer impl: %s\n", global_jbconf.impl);
2933 ast_cli(a->fd, " Jitterbuffer log: %s\n", (ast_test_flag(&global_jbconf, AST_JB_LOG) ? "Yes" : "No"));
2938 static struct ast_cli_entry cli_skinny_set_debug_deprecated = AST_CLI_DEFINE(handle_skinny_set_debug_deprecated, "Enable/Disable Skinny debugging");
2939 static struct ast_cli_entry cli_skinny[] = {
2940 AST_CLI_DEFINE(handle_skinny_show_devices, "List defined Skinny devices"),
2941 AST_CLI_DEFINE(handle_skinny_show_device, "List Skinny device information"),
2942 AST_CLI_DEFINE(handle_skinny_show_lines, "List defined Skinny lines per device"),
2943 AST_CLI_DEFINE(handle_skinny_show_line, "List Skinny line information"),
2944 AST_CLI_DEFINE(handle_skinny_show_settings, "List global Skinny settings"),
2945 AST_CLI_DEFINE(handle_skinny_set_debug, "Enable/Disable Skinny debugging", .deprecate_cmd = &cli_skinny_set_debug_deprecated),
2946 AST_CLI_DEFINE(handle_skinny_reset, "Reset Skinny device(s)"),
2950 static struct skinny_paging_device *build_paging_device(const char *cat, struct ast_variable *v)
2956 static struct skinny_device *build_device(const char *cat, struct ast_variable *v)
2958 struct skinny_device *d;
2959 struct skinny_line *l;
2960 struct skinny_speeddial *sd;
2961 struct skinny_addon *a;
2962 char device_vmexten[AST_MAX_EXTENSION];
2963 struct ast_variable *chanvars = NULL;
2964 int lineInstance = 1;
2965 int speeddialInstance = 1;
2968 if (!(d = ast_calloc(1, sizeof(*d)))) {
2971 ast_copy_string(d->name, cat, sizeof(d->name));
2972 d->lastlineinstance = 1;
2973 d->capability = default_capability;
2974 d->prefs = default_prefs;
2975 if (!ast_strlen_zero(vmexten))
2976 ast_copy_string(device_vmexten, vmexten, sizeof(device_vmexten));
2978 memset(device_vmexten, 0, sizeof(device_vmexten));
2982 if (!strcasecmp(v->name, "host")) {
2983 if (ast_get_ip(&d->addr, v->value)) {
2987 } else if (!strcasecmp(v->name, "port")) {
2988 d->addr.sin_port = htons(atoi(v->value));
2989 } else if (!strcasecmp(v->name, "device")) {
2990 ast_copy_string(d->id, v->value, sizeof(d->id));
2991 } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
2992 d->ha = ast_append_ha(v->name, v->value, d->ha, NULL);
2993 } else if (!strcasecmp(v->name, "vmexten")) {
2994 ast_copy_string(device_vmexten, v->value, sizeof(device_vmexten));
2995 } else if (!strcasecmp(v->name, "context")) {
2996 ast_copy_string(context, v->value, sizeof(context));
2997 } else if (!strcasecmp(v->name, "regexten")) {
2998 ast_copy_string(regexten, v->value, sizeof(regexten));
2999 } else if (!strcasecmp(v->name, "allow")) {
3000 ast_parse_allow_disallow(&d->prefs, &d->capability, v->value, 1);
3001 } else if (!strcasecmp(v->name, "disallow")) {
3002 ast_parse_allow_disallow(&d->prefs, &d->capability, v->value, 0);
3003 } else if (!strcasecmp(v->name, "version")) {
3004 ast_copy_string(d->version_id, v->value, sizeof(d->version_id));
3005 } else if (!strcasecmp(v->name, "canreinvite")) {
3006 canreinvite = ast_true(v->value);
3007 } else if (!strcasecmp(v->name, "earlyrtp")) {
3008 d->earlyrtp = ast_true(v->value);
3009 } else if (!strcasecmp(v->name, "nat")) {
3010 nat = ast_true(v->value);
3011 } else if (!strcasecmp(v->name, "callerid")) {
3012 if (!strcasecmp(v->value, "asreceived")) {
3016 ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
3018 } else if (!strcasecmp(v->name, "language")) {
3019 ast_copy_string(language, v->value, sizeof(language));
3020 } else if (!strcasecmp(v->name, "accountcode")) {
3021 ast_copy_string(accountcode, v->value, sizeof(accountcode));
3022 } else if (!strcasecmp(v->name, "amaflags")) {
3023 y = ast_cdr_amaflags2int(v->value);
3025 ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno);
3029 } else if (!strcasecmp(v->name, "mohinterpret") || !strcasecmp(v->name, "musiconhold")) {
3030 ast_copy_string(mohinterpret, v->value, sizeof(mohinterpret));
3031 } else if (!strcasecmp(v->name, "mohsuggest")) {
3032 ast_copy_string(mohsuggest, v->value, sizeof(mohsuggest));
3033 } else if (!strcasecmp(v->name, "callgroup")) {
3034 cur_callergroup = ast_get_group(v->value);
3035 } else if (!strcasecmp(v->name, "pickupgroup")) {
3036 cur_pickupgroup = ast_get_group(v->value);
3037 } else if (!strcasecmp(v->name, "immediate")) {
3038 immediate = ast_true(v->value);
3039 } else if (!strcasecmp(v->name, "cancallforward")) {
3040 cancallforward = ast_true(v->value);
3041 } else if (!strcasecmp(v->name, "mailbox")) {
3042 ast_copy_string(mailbox, v->value, sizeof(mailbox));
3043 } else if (!strcasecmp(v->name, "callreturn")) {
3044 callreturn = ast_true(v->value);
3045 } else if (!strcasecmp(v->name, "callwaiting")) {
3046 callwaiting = ast_true(v->value);
3047 } else if (!strcasecmp(v->name, "transfer")) {
3048 transfer = ast_true(v->value);
3049 } else if (!strcasecmp(v->name, "threewaycalling")) {
3050 threewaycalling = ast_true(v->value);
3051 } else if (!strcasecmp(v->name, "mwiblink")) {
3052 mwiblink = ast_true(v->value);
3053 } else if (!strcasecmp(v->name, "linelabel")) {
3054 ast_copy_string(linelabel, v->value, sizeof(linelabel));
3055 } else if (!strcasecmp(v->name, "setvar")) {
3056 chanvars = add_var(v->value, chanvars);
3057 } else if ( !strcasecmp(v->name, "parkinglot")) {
3058 ast_copy_string(parkinglot, v->value, sizeof(parkinglot));
3059 } else if (!strcasecmp(v->name, "speeddial")) {
3060 if (!(sd = ast_calloc(1, sizeof(*sd)))) {
3064 char *stringp = buf, *exten, *context, *label;
3066 ast_copy_string(buf, v->value, sizeof(buf));
3067 exten = strsep(&stringp, ",");
3068 if ((context = strchr(exten, '@'))) {
3072 ast_mutex_init(&sd->lock);
3073 ast_copy_string(sd->exten, exten, sizeof(sd->exten));
3074 if (!ast_strlen_zero(context)) {
3076 sd->instance = lineInstance++;
3077 ast_copy_string(sd->context, context, sizeof(sd->context));
3080 sd->instance = speeddialInstance++;
3081 sd->context[0] = '\0';
3083 ast_copy_string(sd->label, S_OR(label, exten), sizeof(sd->label));
3087 sd->next = d->speeddials;
3090 } else if (!strcasecmp(v->name, "addon")) {
3091 if (!(a = ast_calloc(1, sizeof(*a)))) {
3094 ast_mutex_init(&a->lock);
3095 ast_copy_string(a->type, v->value, sizeof(a->type));
3097 a->next = d->addons;
3100 } else if (!strcasecmp(v->name, "trunk") || !strcasecmp(v->name, "line")) {
3101 if (!(l = ast_calloc(1, sizeof(*l)))) {
3104 ast_mutex_init(&l->lock);
3105 ast_copy_string(l->name, v->value, sizeof(l->name));
3107 /* XXX Should we check for uniqueness?? XXX */
3108 ast_copy_string(l->context, context, sizeof(l->context));
3109 ast_copy_string(l->cid_num, cid_num, sizeof(l->cid_num));
3110 ast_copy_string(l->cid_name, cid_name, sizeof(l->cid_name));
3111 ast_copy_string(l->label, linelabel, sizeof(l->label));
3112 ast_copy_string(l->parkinglot, parkinglot, sizeof(l->parkinglot));
3113 ast_copy_string(l->language, language, sizeof(l->language));
3114 ast_copy_string(l->mohinterpret, mohinterpret, sizeof(l->mohinterpret));
3115 ast_copy_string(l->mohsuggest, mohsuggest, sizeof(l->mohsuggest));
3116 ast_copy_string(l->regexten, regexten, sizeof(l->regexten));
3117 ast_copy_string(l->mailbox, mailbox, sizeof(l->mailbox));
3118 if (!ast_strlen_zero(mailbox)) {
3119 char *cfg_mailbox, *cfg_context;
3120 cfg_context = cfg_mailbox = ast_strdupa(l->mailbox);
3121 ast_verb(3, "Setting mailbox '%s' on %s@%s\n", cfg_mailbox, d->name, l->name);
3122 strsep(&cfg_context, "@");
3123 if (ast_strlen_zero(cfg_context))
3124 cfg_context = "default";
3125 l->mwi_event_sub = ast_event_subscribe(AST_EVENT_MWI, mwi_event_cb, NULL,
3126 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, cfg_mailbox,
3127 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, cfg_context,
3128 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
3131 ast_copy_string(l->vmexten, device_vmexten, sizeof(vmexten));
3132 l->chanvars = chanvars;
3134 l->capability = d->capability;
3135 l->prefs = d->prefs;
3137 if (!strcasecmp(v->name, "trunk")) {
3138 l->type = TYPE_TRUNK;
3140 l->type = TYPE_LINE;
3142 l->immediate = immediate;
3143 l->callgroup = cur_callergroup;
3144 l->pickupgroup = cur_pickupgroup;
3145 l->callreturn = callreturn;
3146 l->cancallforward = cancallforward;
3148 set_callforwards(l, NULL, 0);
3149 l->callwaiting = callwaiting;
3150 l->transfer = transfer;
3151 l->threewaycalling = threewaycalling;
3152 l->mwiblink = mwiblink;
3153 l->onhooktime = time(NULL);
3154 l->instance = lineInstance++;
3155 /* ASSUME we're onhook at this point */
3156 l->hookstate = SKINNY_ONHOOK;
3158 l->canreinvite = canreinvite;
3164 ast_log(LOG_WARNING, "Don't know keyword '%s' at line %d\n", v->name, v->lineno);
3170 ast_log(LOG_ERROR, "A Skinny device must have at least one line!\n");
3173 if (/*d->addr.sin_addr.s_addr && */!ntohs(d->addr.sin_port)) {
3174 d->addr.sin_port = htons(DEFAULT_SKINNY_PORT);
3177 /* I don't think we need this anymore at all, since d->ourip is set in skinny_register now */
3178 if (d->addr.sin_addr.s_addr) {
3179 /* XXX See note above, in 'host' option. */
3180 if (ast_ouraddrfor(&d->addr.sin_addr, &d->ourip)) {