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$")
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <netinet/tcp.h>
40 #include <sys/ioctl.h>
45 #include <arpa/inet.h>
46 #include <sys/signal.h>
50 #include "asterisk/lock.h"
51 #include "asterisk/channel.h"
52 #include "asterisk/config.h"
53 #include "asterisk/logger.h"
54 #include "asterisk/module.h"
55 #include "asterisk/pbx.h"
56 #include "asterisk/options.h"
57 #include "asterisk/lock.h"
58 #include "asterisk/sched.h"
59 #include "asterisk/io.h"
60 #include "asterisk/rtp.h"
61 #include "asterisk/acl.h"
62 #include "asterisk/callerid.h"
63 #include "asterisk/cli.h"
64 #include "asterisk/say.h"
65 #include "asterisk/cdr.h"
66 #include "asterisk/astdb.h"
67 #include "asterisk/features.h"
68 #include "asterisk/app.h"
69 #include "asterisk/musiconhold.h"
70 #include "asterisk/utils.h"
71 #include "asterisk/dsp.h"
72 #include "asterisk/stringfields.h"
73 #include "asterisk/astobj.h"
74 #include "asterisk/abstract_jb.h"
75 #include "asterisk/threadstorage.h"
76 #include "asterisk/devicestate.h"
78 /*************************************
79 * Skinny/Asterisk Protocol Settings *
80 *************************************/
81 static const char tdesc[] = "Skinny Client Control Protocol (Skinny)";
82 static const char config[] = "skinny.conf";
84 static int default_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW;
85 static struct ast_codec_pref default_prefs;
88 SKINNY_CODEC_ALAW = 2,
89 SKINNY_CODEC_ULAW = 4,
90 SKINNY_CODEC_G723_1 = 9,
91 SKINNY_CODEC_G729A = 12,
92 SKINNY_CODEC_G726_32 = 82, /* XXX Which packing order does this translate to? */
93 SKINNY_CODEC_H261 = 100,
94 SKINNY_CODEC_H263 = 101
97 #define DEFAULT_SKINNY_PORT 2000
98 #define DEFAULT_SKINNY_BACKLOG 2
99 #define SKINNY_MAX_PACKET 1000
101 static int keep_alive = 120;
102 static char vmexten[AST_MAX_EXTENSION]; /* Voicemail pilot number */
103 static char date_format[6] = "D-M-Y";
104 static char version_id[16] = "P002F202";
106 #if __BYTE_ORDER == __LITTLE_ENDIAN
107 #define letohl(x) (x)
108 #define letohs(x) (x)
109 #define htolel(x) (x)
110 #define htoles(x) (x)
112 #if defined(SOLARIS) || defined(__Darwin__) || defined(__NetBSD__)
113 #define __bswap_16(x) \
114 ((((x) & 0xff00) >> 8) | \
115 (((x) & 0x00ff) << 8))
116 #define __bswap_32(x) \
117 ((((x) & 0xff000000) >> 24) | \
118 (((x) & 0x00ff0000) >> 8) | \
119 (((x) & 0x0000ff00) << 8) | \
120 (((x) & 0x000000ff) << 24))
122 #include <bits/byteswap.h>
124 #define letohl(x) __bswap_32(x)
125 #define letohs(x) __bswap_16(x)
126 #define htolel(x) __bswap_32(x)
127 #define htoles(x) __bswap_16(x)
130 /*! Global jitterbuffer configuration - by default, jb is disabled */
131 static struct ast_jb_conf default_jbconf =
135 .resync_threshold = -1,
138 static struct ast_jb_conf global_jbconf;
140 AST_THREADSTORAGE(device2str_threadbuf);
141 #define DEVICE2STR_BUFSIZE 15
143 AST_THREADSTORAGE(control2str_threadbuf);
144 #define CONTROL2STR_BUFSIZE 100
146 /*********************
147 * Protocol Messages *
148 *********************/
150 #define KEEP_ALIVE_MESSAGE 0x0000
151 /* no additional struct */
153 #define REGISTER_MESSAGE 0x0001
154 struct register_message {
163 #define IP_PORT_MESSAGE 0x0002
165 #define KEYPAD_BUTTON_MESSAGE 0x0003
166 struct keypad_button_message {
168 uint32_t lineInstance;
169 uint32_t callReference;
172 #define STIMULUS_MESSAGE 0x0005
173 struct stimulus_message {
175 uint32_t stimulusInstance;
179 #define OFFHOOK_MESSAGE 0x0006
180 struct offhook_message {
185 #define ONHOOK_MESSAGE 0x0007
186 struct onhook_message {
191 #define CAPABILITIES_RES_MESSAGE 0x0010
192 struct station_capabilities {
201 struct capabilities_res_message {
203 struct station_capabilities caps[18];
206 #define SPEED_DIAL_STAT_REQ_MESSAGE 0x000A
207 struct speed_dial_stat_req_message {
208 uint32_t speedDialNumber;
211 #define LINE_STATE_REQ_MESSAGE 0x000B
212 struct line_state_req_message {
216 #define TIME_DATE_REQ_MESSAGE 0x000D
217 #define BUTTON_TEMPLATE_REQ_MESSAGE 0x000E
218 #define VERSION_REQ_MESSAGE 0x000F
219 #define SERVER_REQUEST_MESSAGE 0x0012
221 #define ALARM_MESSAGE 0x0020
222 struct alarm_message {
223 uint32_t alarmSeverity;
224 char displayMessage[80];
225 uint32_t alarmParam1;
226 uint32_t alarmParam2;
229 #define OPEN_RECEIVE_CHANNEL_ACK_MESSAGE 0x0022
230 struct open_receive_channel_ack_message {
237 #define SOFT_KEY_SET_REQ_MESSAGE 0x0025
239 #define SOFT_KEY_EVENT_MESSAGE 0x0026
240 struct soft_key_event_message {
241 uint32_t softKeyEvent;
246 #define UNREGISTER_MESSAGE 0x0027
247 #define SOFT_KEY_TEMPLATE_REQ_MESSAGE 0x0028
248 #define HEADSET_STATUS_MESSAGE 0x002B
249 #define REGISTER_AVAILABLE_LINES_MESSAGE 0x002D
251 #define REGISTER_ACK_MESSAGE 0x0081
252 struct register_ack_message {
254 char dateTemplate[6];
256 uint32_t secondaryKeepAlive;
260 #define START_TONE_MESSAGE 0x0082
261 struct start_tone_message {
266 #define STOP_TONE_MESSAGE 0x0083
267 struct stop_tone_message {
271 #define SET_RINGER_MESSAGE 0x0085
272 struct set_ringer_message {
274 uint32_t unknown1; /* See notes in transmit_ringer_mode */
279 #define SET_LAMP_MESSAGE 0x0086
280 struct set_lamp_message {
282 uint32_t stimulusInstance;
283 uint32_t deviceStimulus;
286 #define SET_SPEAKER_MESSAGE 0x0088
287 struct set_speaker_message {
291 /* XXX When do we need to use this? */
292 #define SET_MICROPHONE_MESSAGE 0x0089
293 struct set_microphone_message {
297 #define START_MEDIA_TRANSMISSION_MESSAGE 0x008A
298 struct media_qualifier {
305 struct start_media_transmission_message {
306 uint32_t conferenceId;
307 uint32_t passThruPartyId;
311 uint32_t payloadType;
312 struct media_qualifier qualifier;
316 #define STOP_MEDIA_TRANSMISSION_MESSAGE 0x008B
317 struct stop_media_transmission_message {
318 uint32_t conferenceId;
319 uint32_t passThruPartyId;
323 #define CALL_INFO_MESSAGE 0x008F
324 struct call_info_message {
325 char callingPartyName[40];
326 char callingParty[24];
327 char calledPartyName[40];
328 char calledParty[24];
332 char originalCalledPartyName[40];
333 char originalCalledParty[24];
334 char lastRedirectingPartyName[40];
335 char lastRedirectingParty[24];
336 uint32_t originalCalledPartyRedirectReason;
337 uint32_t lastRedirectingReason;
338 char callingPartyVoiceMailbox[24];
339 char calledPartyVoiceMailbox[24];
340 char originalCalledPartyVoiceMailbox[24];
341 char lastRedirectingVoiceMailbox[24];
345 #define SPEED_DIAL_STAT_RES_MESSAGE 0x0091
346 struct speed_dial_stat_res_message {
347 uint32_t speedDialNumber;
348 char speedDialDirNumber[24];
349 char speedDialDisplayName[40];
352 #define LINE_STAT_RES_MESSAGE 0x0092
353 struct line_stat_res_message {
355 char lineDirNumber[24];
356 char lineDisplayName[24];
360 #define DEFINETIMEDATE_MESSAGE 0x0094
361 struct definetimedate_message {
362 uint32_t year; /* since 1900 */
364 uint32_t dayofweek; /* monday = 1 */
369 uint32_t milliseconds;
373 #define BUTTON_TEMPLATE_RES_MESSAGE 0x0097
374 struct button_definition {
375 uint8_t instanceNumber;
376 uint8_t buttonDefinition;
379 struct button_definition_template {
380 uint8_t buttonDefinition;
381 /* for now, anything between 0xB0 and 0xCF is custom */
385 #define STIMULUS_REDIAL 0x01
386 #define STIMULUS_SPEEDDIAL 0x02
387 #define STIMULUS_HOLD 0x03
388 #define STIMULUS_TRANSFER 0x04
389 #define STIMULUS_FORWARDALL 0x05
390 #define STIMULUS_FORWARDBUSY 0x06
391 #define STIMULUS_FORWARDNOANSWER 0x07
392 #define STIMULUS_DISPLAY 0x08
393 #define STIMULUS_LINE 0x09
394 #define STIMULUS_VOICEMAIL 0x0F
395 #define STIMULUS_AUTOANSWER 0x11
396 #define STIMULUS_CONFERENCE 0x7D
397 #define STIMULUS_CALLPARK 0x7E
398 #define STIMULUS_CALLPICKUP 0x7F
399 #define STIMULUS_NONE 0xFF
402 #define BT_REDIAL STIMULUS_REDIAL
403 #define BT_SPEEDDIAL STIMULUS_SPEEDDIAL
404 #define BT_HOLD STIMULUS_HOLD
405 #define BT_TRANSFER STIMULUS_TRANSFER
406 #define BT_FORWARDALL STIMULUS_FORWARDALL
407 #define BT_FORWARDBUSY STIMULUS_FORWARDBUSY
408 #define BT_FORWARDNOANSWER STIMULUS_FORWARDNOANSWER
409 #define BT_DISPLAY STIMULUS_DISPLAY
410 #define BT_LINE STIMULUS_LINE
411 #define BT_VOICEMAIL STIMULUS_VOICEMAIL
412 #define BT_AUTOANSWER STIMULUS_AUTOANSWER
413 #define BT_CONFERENCE STIMULUS_CONFERENCE
414 #define BT_CALLPARK STIMULUS_CALLPARK
415 #define BT_CALLPICKUP STIMULUS_CALLPICKUP
418 /* Custom button types - add our own between 0xB0 and 0xCF.
419 This may need to be revised in the future,
420 if stimuluses are ever added in this range. */
421 #define BT_CUST_LINESPEEDDIAL 0xB0 /* line or speeddial with/without hint */
422 #define BT_CUST_LINE 0xB1 /* line or speeddial with hint only */
424 struct button_template_res_message {
425 uint32_t buttonOffset;
426 uint32_t buttonCount;
427 uint32_t totalButtonCount;
428 struct button_definition definition[42];
431 #define VERSION_RES_MESSAGE 0x0098
432 struct version_res_message {
436 #define DISPLAYTEXT_MESSAGE 0x0099
437 struct displaytext_message {
441 #define CLEAR_NOTIFY_MESSAGE 0x0115
442 #define CLEAR_DISPLAY_MESSAGE 0x009A
444 #define CAPABILITIES_REQ_MESSAGE 0x009B
446 #define REGISTER_REJ_MESSAGE 0x009D
447 struct register_rej_message {
451 #define SERVER_RES_MESSAGE 0x009E
452 struct server_identifier {
456 struct server_res_message {
457 struct server_identifier server[5];
458 uint32_t serverListenPort[5];
459 uint32_t serverIpAddr[5];
462 #define RESET_MESSAGE 0x009F
463 struct reset_message {
467 #define KEEP_ALIVE_ACK_MESSAGE 0x0100
469 #define OPEN_RECEIVE_CHANNEL_MESSAGE 0x0105
470 struct open_receive_channel_message {
471 uint32_t conferenceId;
480 #define CLOSE_RECEIVE_CHANNEL_MESSAGE 0x0106
481 struct close_receive_channel_message {
482 uint32_t conferenceId;
487 #define SOFT_KEY_TEMPLATE_RES_MESSAGE 0x0108
489 struct soft_key_template_definition {
490 char softKeyLabel[16];
491 uint32_t softKeyEvent;
494 #define KEYDEF_ONHOOK 0
495 #define KEYDEF_CONNECTED 1
496 #define KEYDEF_ONHOLD 2
497 #define KEYDEF_RINGIN 3
498 #define KEYDEF_OFFHOOK 4
499 #define KEYDEF_CONNWITHTRANS 5
500 #define KEYDEF_DADFD 6 /* Digits After Dialing First Digit */
501 #define KEYDEF_CONNWITHCONF 7
502 #define KEYDEF_RINGOUT 8
503 #define KEYDEF_OFFHOOKWITHFEAT 9
504 #define KEYDEF_UNKNOWN 10
506 #define SOFTKEY_NONE 0x00
507 #define SOFTKEY_REDIAL 0x01
508 #define SOFTKEY_NEWCALL 0x02
509 #define SOFTKEY_HOLD 0x03
510 #define SOFTKEY_TRNSFER 0x04
511 #define SOFTKEY_CFWDALL 0x05
512 #define SOFTKEY_CFWDBUSY 0x06
513 #define SOFTKEY_CFWDNOANSWER 0x07
514 #define SOFTKEY_BKSPC 0x08
515 #define SOFTKEY_ENDCALL 0x09
516 #define SOFTKEY_RESUME 0x0A
517 #define SOFTKEY_ANSWER 0x0B
518 #define SOFTKEY_INFO 0x0C
519 #define SOFTKEY_CONFRN 0x0D
520 #define SOFTKEY_PARK 0x0E
521 #define SOFTKEY_JOIN 0x0F
522 #define SOFTKEY_MEETME 0x10
523 #define SOFTKEY_PICKUP 0x11
524 #define SOFTKEY_GPICKUP 0x12
526 struct soft_key_template_definition soft_key_template_default[] = {
532 { "CFwdBusy", 0x06 },
533 { "CFwdNoAnswer", 0x07 },
547 struct soft_key_definitions {
549 const uint8_t *defaults;
553 static const uint8_t soft_key_default_onhook[] = {
561 static const uint8_t soft_key_default_connected[] = {
570 static const uint8_t soft_key_default_onhold[] = {
577 static const uint8_t soft_key_default_ringin[] = {
583 static const uint8_t soft_key_default_offhook[] = {
591 static const uint8_t soft_key_default_connwithtrans[] = {
600 static const uint8_t soft_key_default_dadfd[] = {
605 static const uint8_t soft_key_default_connwithconf[] = {
609 static const uint8_t soft_key_default_ringout[] = {
616 static const uint8_t soft_key_default_offhookwithfeat[] = {
621 static const uint8_t soft_key_default_unknown[] = {
625 static const struct soft_key_definitions soft_key_default_definitions[] = {
626 {KEYDEF_ONHOOK, soft_key_default_onhook, sizeof(soft_key_default_onhook) / sizeof(uint8_t)},
627 {KEYDEF_CONNECTED, soft_key_default_connected, sizeof(soft_key_default_connected) / sizeof(uint8_t)},
628 {KEYDEF_ONHOLD, soft_key_default_onhold, sizeof(soft_key_default_onhold) / sizeof(uint8_t)},
629 {KEYDEF_RINGIN, soft_key_default_ringin, sizeof(soft_key_default_ringin) / sizeof(uint8_t)},
630 {KEYDEF_OFFHOOK, soft_key_default_offhook, sizeof(soft_key_default_offhook) / sizeof(uint8_t)},
631 {KEYDEF_CONNWITHTRANS, soft_key_default_connwithtrans, sizeof(soft_key_default_connwithtrans) / sizeof(uint8_t)},
632 {KEYDEF_DADFD, soft_key_default_dadfd, sizeof(soft_key_default_dadfd) / sizeof(uint8_t)},
633 {KEYDEF_CONNWITHCONF, soft_key_default_connwithconf, sizeof(soft_key_default_connwithconf) / sizeof(uint8_t)},
634 {KEYDEF_RINGOUT, soft_key_default_ringout, sizeof(soft_key_default_ringout) / sizeof(uint8_t)},
635 {KEYDEF_OFFHOOKWITHFEAT, soft_key_default_offhookwithfeat, sizeof(soft_key_default_offhookwithfeat) / sizeof(uint8_t)},
636 {KEYDEF_UNKNOWN, soft_key_default_unknown, sizeof(soft_key_default_unknown) / sizeof(uint8_t)}
639 struct soft_key_template_res_message {
640 uint32_t softKeyOffset;
641 uint32_t softKeyCount;
642 uint32_t totalSoftKeyCount;
643 struct soft_key_template_definition softKeyTemplateDefinition[32];
646 #define SOFT_KEY_SET_RES_MESSAGE 0x0109
648 struct soft_key_set_definition {
649 uint8_t softKeyTemplateIndex[16];
650 uint16_t softKeyInfoIndex[16];
653 struct soft_key_set_res_message {
654 uint32_t softKeySetOffset;
655 uint32_t softKeySetCount;
656 uint32_t totalSoftKeySetCount;
657 struct soft_key_set_definition softKeySetDefinition[16];
661 #define SELECT_SOFT_KEYS_MESSAGE 0x0110
662 struct select_soft_keys_message {
665 uint32_t softKeySetIndex;
666 uint32_t validKeyMask;
669 #define CALL_STATE_MESSAGE 0x0111
670 struct call_state_message {
672 uint32_t lineInstance;
673 uint32_t callReference;
677 #define DISPLAY_PROMPT_STATUS_MESSAGE 0x0112
678 struct display_prompt_status_message {
679 uint32_t messageTimeout;
680 char promptMessage[32];
681 uint32_t lineInstance;
682 uint32_t callReference;
685 #define CLEAR_PROMPT_MESSAGE 0x0113
686 struct clear_prompt_message {
687 uint32_t lineInstance;
688 uint32_t callReference;
691 #define DISPLAY_NOTIFY_MESSAGE 0x0114
692 struct display_notify_message {
693 uint32_t displayTimeout;
694 char displayMessage[100];
697 #define ACTIVATE_CALL_PLANE_MESSAGE 0x0116
698 struct activate_call_plane_message {
699 uint32_t lineInstance;
702 #define DIALED_NUMBER_MESSAGE 0x011D
703 struct dialed_number_message {
704 char dialedNumber[24];
705 uint32_t lineInstance;
706 uint32_t callReference;
710 struct alarm_message alarm;
711 struct speed_dial_stat_req_message speeddialreq;
712 struct register_message reg;
713 struct register_ack_message regack;
714 struct register_rej_message regrej;
715 struct capabilities_res_message caps;
716 struct version_res_message version;
717 struct button_template_res_message buttontemplate;
718 struct displaytext_message displaytext;
719 struct display_prompt_status_message displaypromptstatus;
720 struct clear_prompt_message clearpromptstatus;
721 struct definetimedate_message definetimedate;
722 struct start_tone_message starttone;
723 struct speed_dial_stat_res_message speeddial;
724 struct line_state_req_message line;
725 struct line_stat_res_message linestat;
726 struct soft_key_set_res_message softkeysets;
727 struct soft_key_template_res_message softkeytemplate;
728 struct server_res_message serverres;
729 struct reset_message reset;
730 struct set_lamp_message setlamp;
731 struct set_ringer_message setringer;
732 struct call_state_message callstate;
733 struct keypad_button_message keypad;
734 struct select_soft_keys_message selectsoftkey;
735 struct activate_call_plane_message activatecallplane;
736 struct stimulus_message stimulus;
737 struct offhook_message offhook;
738 struct onhook_message onhook;
739 struct set_speaker_message setspeaker;
740 struct set_microphone_message setmicrophone;
741 struct call_info_message callinfo;
742 struct start_media_transmission_message startmedia;
743 struct stop_media_transmission_message stopmedia;
744 struct open_receive_channel_message openreceivechannel;
745 struct open_receive_channel_ack_message openreceivechannelack;
746 struct close_receive_channel_message closereceivechannel;
747 struct display_notify_message displaynotify;
748 struct dialed_number_message dialednumber;
749 struct soft_key_event_message softkeyeventmessage;
752 /* packet composition */
757 union skinny_data data;
760 /* XXX This is the combined size of the variables above. (len, res, e)
761 If more are added, this MUST change.
762 (sizeof(skinny_req) - sizeof(skinny_data)) DOES NOT WORK on all systems (amd64?). */
763 int skinny_header_size = 12;
765 /*****************************
766 * Asterisk specific globals *
767 *****************************/
769 static int skinnydebug = 0;
771 /* a hostname, portnumber, socket and such is usefull for VoIP protocols */
772 static struct sockaddr_in bindaddr;
773 static char ourhost[256];
775 static struct in_addr __ourip;
776 struct ast_hostent ahp;
778 static int skinnysock = -1;
779 static pthread_t accept_t;
780 static char context[AST_MAX_CONTEXT] = "default";
781 static char language[MAX_LANGUAGE] = "";
782 static char mohinterpret[MAX_MUSICCLASS] = "default";
783 static char mohsuggest[MAX_MUSICCLASS] = "";
784 static char cid_num[AST_MAX_EXTENSION] = "";
785 static char cid_name[AST_MAX_EXTENSION] = "";
786 static char linelabel[AST_MAX_EXTENSION] ="";
788 static ast_group_t cur_callergroup = 0;
789 static ast_group_t cur_pickupgroup = 0;
790 static int immediate = 0;
791 static int callwaiting = 0;
792 static int callreturn = 0;
793 static int threewaycalling = 0;
794 static int mwiblink = 0;
795 /* This is for flashhook transfers */
796 static int transfer = 0;
797 static int cancallforward = 0;
798 /* static int busycount = 3;*/
799 static char accountcode[AST_MAX_ACCOUNT_CODE] = "";
800 static char mailbox[AST_MAX_EXTENSION];
801 static int amaflags = 0;
802 static int callnums = 1;
804 #define SKINNY_DEVICE_UNKNOWN -1
805 #define SKINNY_DEVICE_NONE 0
806 #define SKINNY_DEVICE_30SPPLUS 1
807 #define SKINNY_DEVICE_12SPPLUS 2
808 #define SKINNY_DEVICE_12SP 3
809 #define SKINNY_DEVICE_12 4
810 #define SKINNY_DEVICE_30VIP 5
811 #define SKINNY_DEVICE_7910 6
812 #define SKINNY_DEVICE_7960 7
813 #define SKINNY_DEVICE_7940 8
814 #define SKINNY_DEVICE_7935 9
815 #define SKINNY_DEVICE_ATA186 12 /* Cisco ATA-186 */
816 #define SKINNY_DEVICE_7941 115
817 #define SKINNY_DEVICE_7971 119
818 #define SKINNY_DEVICE_7985 302
819 #define SKINNY_DEVICE_7911 307
820 #define SKINNY_DEVICE_7961GE 308
821 #define SKINNY_DEVICE_7941GE 309
822 #define SKINNY_DEVICE_7905 20000
823 #define SKINNY_DEVICE_7920 30002
824 #define SKINNY_DEVICE_7970 30006
825 #define SKINNY_DEVICE_7912 30007
826 #define SKINNY_DEVICE_7902 30008
827 #define SKINNY_DEVICE_CIPC 30016 /* Cisco IP Communicator */
828 #define SKINNY_DEVICE_7961 30018
829 #define SKINNY_DEVICE_7936 30019
830 #define SKINNY_DEVICE_SCCPGATEWAY_AN 30027 /* ??? */
831 #define SKINNY_DEVICE_SCCPGATEWAY_BRI 30028 /* ??? */
833 #define SKINNY_SPEAKERON 1
834 #define SKINNY_SPEAKEROFF 2
836 #define SKINNY_MICON 1
837 #define SKINNY_MICOFF 2
839 #define SKINNY_OFFHOOK 1
840 #define SKINNY_ONHOOK 2
841 #define SKINNY_RINGOUT 3
842 #define SKINNY_RINGIN 4
843 #define SKINNY_CONNECTED 5
844 #define SKINNY_BUSY 6
845 #define SKINNY_CONGESTION 7
846 #define SKINNY_HOLD 8
847 #define SKINNY_CALLWAIT 9
848 #define SKINNY_TRANSFER 10
849 #define SKINNY_PARK 11
850 #define SKINNY_PROGRESS 12
851 #define SKINNY_CALLREMOTEMULTILINE 13
852 #define SKINNY_INVALID 14
854 #define SKINNY_SILENCE 0x00
855 #define SKINNY_DIALTONE 0x21
856 #define SKINNY_BUSYTONE 0x23
857 #define SKINNY_ALERT 0x24
858 #define SKINNY_REORDER 0x25
859 #define SKINNY_CALLWAITTONE 0x2D
860 #define SKINNY_NOTONE 0x7F
862 #define SKINNY_LAMP_OFF 1
863 #define SKINNY_LAMP_ON 2
864 #define SKINNY_LAMP_WINK 3
865 #define SKINNY_LAMP_FLASH 4
866 #define SKINNY_LAMP_BLINK 5
868 #define SKINNY_RING_OFF 1
869 #define SKINNY_RING_INSIDE 2
870 #define SKINNY_RING_OUTSIDE 3
871 #define SKINNY_RING_FEATURE 4
876 /* Skinny rtp stream modes. Do we really need this? */
877 #define SKINNY_CX_SENDONLY 0
878 #define SKINNY_CX_RECVONLY 1
879 #define SKINNY_CX_SENDRECV 2
880 #define SKINNY_CX_CONF 3
881 #define SKINNY_CX_CONFERENCE 3
882 #define SKINNY_CX_MUTE 4
883 #define SKINNY_CX_INACTIVE 4
886 static char *skinny_cxmodes[] = {
895 /* driver scheduler */
896 static struct sched_context *sched;
897 static struct io_context *io;
899 /* Protect the monitoring thread, so only one process can kill or start it, and not
900 when it's doing something critical. */
901 AST_MUTEX_DEFINE_STATIC(monlock);
902 /* Protect the network socket */
903 AST_MUTEX_DEFINE_STATIC(netlock);
904 /* Protect the session list */
905 AST_MUTEX_DEFINE_STATIC(sessionlock);
906 /* Protect the device list */
907 AST_MUTEX_DEFINE_STATIC(devicelock);
909 /* Protect the paging device list */
910 AST_MUTEX_DEFINE_STATIC(pagingdevicelock);
913 /* This is the thread for the monitor which checks for input on the channels
914 which are not currently in use. */
915 static pthread_t monitor_thread = AST_PTHREADT_NULL;
917 /* Wait up to 16 seconds for first digit */
918 static int firstdigittimeout = 16000;
920 /* How long to wait for following digits */
921 static int gendigittimeout = 8000;
923 /* How long to wait for an extra digit, if there is an ambiguous match */
924 static int matchdigittimeout = 3000;
926 struct skinny_subchannel {
928 struct ast_channel *owner;
930 struct ast_rtp *vrtp;
932 /* time_t lastouttime; */ /* Unused */
936 /* int lastout; */ /* Unused */
942 struct skinny_subchannel *next;
943 struct skinny_line *parent;
949 char label[24]; /* Label that shows next to the line buttons */
950 char accountcode[AST_MAX_ACCOUNT_CODE];
951 char exten[AST_MAX_EXTENSION]; /* Extension where to start */
952 char context[AST_MAX_CONTEXT];
953 char language[MAX_LANGUAGE];
954 char cid_num[AST_MAX_EXTENSION]; /* Caller*ID */
955 char cid_name[AST_MAX_EXTENSION]; /* Caller*ID */
956 char lastcallerid[AST_MAX_EXTENSION]; /* Last Caller*ID */
957 char call_forward[AST_MAX_EXTENSION];
958 char mailbox[AST_MAX_EXTENSION];
959 char vmexten[AST_MAX_EXTENSION];
960 char mohinterpret[MAX_MUSICCLASS];
961 char mohsuggest[MAX_MUSICCLASS];
962 char lastnumberdialed[AST_MAX_EXTENSION]; /* Last number that was dialed - used for redial */
963 int curtone; /* Current tone being played */
964 ast_group_t callgroup;
965 ast_group_t pickupgroup;
972 int dnd; /* How does this affect callwait? Do we just deny a skinny_request if we're dnd? */
981 int nonCodecCapability;
983 int msgstate; /* voicemail message state */
988 struct ast_codec_pref prefs;
989 struct skinny_subchannel *sub;
990 struct skinny_line *next;
991 struct skinny_device *parent;
994 struct skinny_speeddial {
997 char context[AST_MAX_CONTEXT];
998 char exten[AST_MAX_EXTENSION];
1004 struct skinny_speeddial *next;
1005 struct skinny_device *parent;
1008 struct skinny_addon {
1012 struct skinny_addon *next;
1013 struct skinny_device *parent;
1016 static struct skinny_device {
1017 /* A device containing one or more lines */
1020 char version_id[16];
1021 char exten[AST_MAX_EXTENSION]; /* Cruddy variable name, pick a better one */
1024 int lastlineinstance;
1025 int lastcallreference;
1027 struct sockaddr_in addr;
1028 struct in_addr ourip;
1029 struct skinny_line *lines;
1030 struct skinny_speeddial *speeddials;
1031 struct skinny_addon *addons;
1032 struct ast_codec_pref prefs;
1034 struct skinnysession *session;
1035 struct skinny_device *next;
1038 struct skinny_paging_device {
1041 struct skinny_device ** devices;
1042 struct skinny_paging_device *next;
1045 static struct skinnysession {
1048 struct sockaddr_in sin;
1050 char inbuf[SKINNY_MAX_PACKET];
1051 char outbuf[SKINNY_MAX_PACKET];
1052 struct skinny_device *device;
1053 struct skinnysession *next;
1056 static struct ast_channel *skinny_request(const char *type, int format, void *data, int *cause);
1057 static int skinny_devicestate(void *data);
1058 static int skinny_call(struct ast_channel *ast, char *dest, int timeout);
1059 static int skinny_hangup(struct ast_channel *ast);
1060 static int skinny_answer(struct ast_channel *ast);
1061 static struct ast_frame *skinny_read(struct ast_channel *ast);
1062 static int skinny_write(struct ast_channel *ast, struct ast_frame *frame);
1063 static int skinny_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen);
1064 static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
1065 static int skinny_senddigit_begin(struct ast_channel *ast, char digit);
1066 static int skinny_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
1067 static int handle_time_date_req_message(struct skinny_req *req, struct skinnysession *s);
1069 static const struct ast_channel_tech skinny_tech = {
1071 .description = tdesc,
1072 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
1073 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
1074 .requester = skinny_request,
1075 .devicestate = skinny_devicestate,
1076 .call = skinny_call,
1077 .hangup = skinny_hangup,
1078 .answer = skinny_answer,
1079 .read = skinny_read,
1080 .write = skinny_write,
1081 .indicate = skinny_indicate,
1082 .fixup = skinny_fixup,
1083 .send_digit_begin = skinny_senddigit_begin,
1084 .send_digit_end = skinny_senddigit_end,
1085 /* .bridge = ast_rtp_bridge, */
1088 static int skinny_extensionstate_cb(char *context, char* exten, int state, void *data);
1090 static void *get_button_template(struct skinnysession *s, struct button_definition_template *btn)
1092 struct skinny_device *d = s->device;
1093 struct skinny_addon *a = d->addons;
1097 case SKINNY_DEVICE_30SPPLUS:
1098 case SKINNY_DEVICE_30VIP:
1099 /* 13 rows, 2 columns */
1100 for (i = 0; i < 4; i++)
1101 (btn++)->buttonDefinition = BT_CUST_LINE;
1102 (btn++)->buttonDefinition = BT_REDIAL;
1103 (btn++)->buttonDefinition = BT_VOICEMAIL;
1104 (btn++)->buttonDefinition = BT_CALLPARK;
1105 (btn++)->buttonDefinition = BT_FORWARDALL;
1106 (btn++)->buttonDefinition = BT_CONFERENCE;
1107 for (i = 0; i < 4; i++)
1108 (btn++)->buttonDefinition = BT_NONE;
1109 for (i = 0; i < 13; i++)
1110 (btn++)->buttonDefinition = BT_SPEEDDIAL;
1113 case SKINNY_DEVICE_12SPPLUS:
1114 case SKINNY_DEVICE_12SP:
1115 case SKINNY_DEVICE_12:
1116 /* 6 rows, 2 columns */
1117 for (i = 0; i < 2; i++)
1118 (btn++)->buttonDefinition = BT_CUST_LINE;
1119 for (i = 0; i < 4; i++)
1120 (btn++)->buttonDefinition = BT_SPEEDDIAL;
1121 (btn++)->buttonDefinition = BT_HOLD;
1122 (btn++)->buttonDefinition = BT_REDIAL;
1123 (btn++)->buttonDefinition = BT_TRANSFER;
1124 (btn++)->buttonDefinition = BT_FORWARDALL;
1125 (btn++)->buttonDefinition = BT_CALLPARK;
1126 (btn++)->buttonDefinition = BT_VOICEMAIL;
1128 case SKINNY_DEVICE_7910:
1129 (btn++)->buttonDefinition = BT_LINE;
1130 (btn++)->buttonDefinition = BT_HOLD;
1131 (btn++)->buttonDefinition = BT_TRANSFER;
1132 (btn++)->buttonDefinition = BT_DISPLAY;
1133 (btn++)->buttonDefinition = BT_VOICEMAIL;
1134 (btn++)->buttonDefinition = BT_CONFERENCE;
1135 (btn++)->buttonDefinition = BT_FORWARDALL;
1136 for (i = 0; i < 2; i++)
1137 (btn++)->buttonDefinition = BT_SPEEDDIAL;
1138 (btn++)->buttonDefinition = BT_REDIAL;
1140 case SKINNY_DEVICE_7960:
1141 case SKINNY_DEVICE_7961:
1142 case SKINNY_DEVICE_7961GE:
1143 for (i = 0; i < 6; i++)
1144 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1146 case SKINNY_DEVICE_7940:
1147 case SKINNY_DEVICE_7941:
1148 case SKINNY_DEVICE_7941GE:
1149 for (i = 0; i < 2; i++)
1150 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1152 case SKINNY_DEVICE_7935:
1153 case SKINNY_DEVICE_7936:
1154 for (i = 0; i < 2; i++)
1155 (btn++)->buttonDefinition = BT_LINE;
1157 case SKINNY_DEVICE_ATA186:
1158 (btn++)->buttonDefinition = BT_LINE;
1160 case SKINNY_DEVICE_7970:
1161 case SKINNY_DEVICE_7971:
1162 case SKINNY_DEVICE_CIPC:
1163 for (i = 0; i < 8; i++)
1164 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1166 case SKINNY_DEVICE_7985:
1167 /* XXX I have no idea what the buttons look like on these. */
1168 ast_log(LOG_WARNING, "Unsupported device type '%d (7985)' found.\n", d->type);
1170 case SKINNY_DEVICE_7912:
1171 case SKINNY_DEVICE_7911:
1172 case SKINNY_DEVICE_7905:
1173 (btn++)->buttonDefinition = BT_LINE;
1174 (btn++)->buttonDefinition = BT_HOLD;
1176 case SKINNY_DEVICE_7920:
1177 /* XXX I don't know if this is right. */
1178 for (i = 0; i < 4; i++)
1179 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1181 case SKINNY_DEVICE_7902:
1182 ast_log(LOG_WARNING, "Unsupported device type '%d (7902)' found.\n", d->type);
1184 case SKINNY_DEVICE_SCCPGATEWAY_AN:
1185 case SKINNY_DEVICE_SCCPGATEWAY_BRI:
1186 ast_log(LOG_WARNING, "Unsupported device type '%d (SCCP gateway)' found.\n", d->type);
1189 ast_log(LOG_WARNING, "Unknown device type '%d' found.\n", d->type);
1193 for (a = d->addons; a; a = a->next) {
1194 if (!strcasecmp(a->type, "7914")) {
1195 for (i = 0; i < 14; i++)
1196 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1198 ast_log(LOG_WARNING, "Unknown addon type '%s' found. Skipping.\n", a->type);
1205 static struct skinny_req *req_alloc(size_t size, int response_message)
1207 struct skinny_req *req;
1209 if (!(req = ast_calloc(1, skinny_header_size + size + 4)))
1212 req->len = htolel(size+4);
1213 req->e = htolel(response_message);
1218 static struct skinny_line *find_line_by_instance(struct skinny_device *d, int instance)
1220 struct skinny_line *l;
1222 /*Dialing from on hook or on a 7920 uses instance 0 in requests
1223 but we need to start looking at instance 1 */
1228 for (l = d->lines; l; l = l->next) {
1229 if (l->instance == instance)
1234 ast_log(LOG_WARNING, "Could not find line with instance '%d' on device '%s'\n", instance, d->name);
1239 static struct skinny_line *find_line_by_name(const char *dest)
1241 struct skinny_line *l;
1242 struct skinny_device *d;
1247 ast_copy_string(line, dest, sizeof(line));
1248 at = strchr(line, '@');
1250 ast_log(LOG_NOTICE, "Device '%s' has no @ (at) sign!\n", dest);
1255 ast_mutex_lock(&devicelock);
1256 for (d = devices; d; d = d->next) {
1257 if (!strcasecmp(d->name, device)) {
1259 ast_verbose("Found device: %s\n", d->name);
1260 /* Found the device */
1261 for (l = d->lines; l; l = l->next) {
1262 /* Search for the right line */
1263 if (!strcasecmp(l->name, line)) {
1264 ast_mutex_unlock(&devicelock);
1270 /* Device not found */
1271 ast_mutex_unlock(&devicelock);
1275 /* It's quicker/easier to find the subchannel when we know the instance number too */
1276 static struct skinny_subchannel *find_subchannel_by_instance_reference(struct skinny_device *d, int instance, int reference)
1278 struct skinny_line *l = find_line_by_instance(d, instance);
1279 struct skinny_subchannel *sub;
1285 /* 7920 phones set call reference to 0, so use the first
1286 sub-channel on the list.
1287 This MIGHT need more love to be right */
1291 for (sub = l->sub; sub; sub = sub->next) {
1292 if (sub->callid == reference)
1297 ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s'\n", reference, d->name);
1302 /* Find the subchannel when we only have the callid - this shouldn't happen often */
1303 static struct skinny_subchannel *find_subchannel_by_reference(struct skinny_device *d, int reference)
1305 struct skinny_line *l;
1306 struct skinny_subchannel *sub = NULL;
1308 for (l = d->lines; l; l = l->next) {
1309 for (sub = l->sub; sub; sub = sub->next) {
1310 if (sub->callid == reference)
1318 ast_log(LOG_WARNING, "Could not find any lines that contained a subchannel with reference '%d' on device '%s'\n", reference, d->name);
1321 ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s@%s'\n", reference, l->name, d->name);
1327 static struct skinny_speeddial *find_speeddial_by_instance(struct skinny_device *d, int instance, int isHint)
1329 struct skinny_speeddial *sd;
1331 for (sd = d->speeddials; sd; sd = sd->next) {
1332 if (sd->isHint == isHint && sd->instance == instance)
1337 ast_log(LOG_WARNING, "Could not find speeddial with instance '%d' on device '%s'\n", instance, d->name);
1342 static int codec_skinny2ast(enum skinny_codecs skinnycodec)
1344 switch (skinnycodec) {
1345 case SKINNY_CODEC_ALAW:
1346 return AST_FORMAT_ALAW;
1347 case SKINNY_CODEC_ULAW:
1348 return AST_FORMAT_ULAW;
1349 case SKINNY_CODEC_G723_1:
1350 return AST_FORMAT_G723_1;
1351 case SKINNY_CODEC_G729A:
1352 return AST_FORMAT_G729A;
1353 case SKINNY_CODEC_G726_32:
1354 return AST_FORMAT_G726_AAL2; /* XXX Is this right? */
1355 case SKINNY_CODEC_H261:
1356 return AST_FORMAT_H261;
1357 case SKINNY_CODEC_H263:
1358 return AST_FORMAT_H263;
1364 static int codec_ast2skinny(int astcodec)
1367 case AST_FORMAT_ALAW:
1368 return SKINNY_CODEC_ALAW;
1369 case AST_FORMAT_ULAW:
1370 return SKINNY_CODEC_ULAW;
1371 case AST_FORMAT_G723_1:
1372 return SKINNY_CODEC_G723_1;
1373 case AST_FORMAT_G729A:
1374 return SKINNY_CODEC_G729A;
1375 case AST_FORMAT_G726_AAL2: /* XXX Is this right? */
1376 return SKINNY_CODEC_G726_32;
1377 case AST_FORMAT_H261:
1378 return SKINNY_CODEC_H261;
1379 case AST_FORMAT_H263:
1380 return SKINNY_CODEC_H263;
1386 static int skinny_register(struct skinny_req *req, struct skinnysession *s)
1388 struct skinny_device *d;
1389 struct skinny_line *l;
1390 struct skinny_speeddial *sd;
1391 struct sockaddr_in sin;
1394 ast_mutex_lock(&devicelock);
1395 for (d = devices; d; d = d->next) {
1396 if (!strcasecmp(req->data.reg.name, d->id)
1397 && ast_apply_ha(d->ha, &(s->sin))) {
1399 d->type = letohl(req->data.reg.type);
1400 if (ast_strlen_zero(d->version_id)) {
1401 ast_copy_string(d->version_id, version_id, sizeof(d->version_id));
1407 if (getsockname(s->fd, (struct sockaddr *)&sin, &slen)) {
1408 ast_log(LOG_WARNING, "Cannot get socket name\n");
1409 sin.sin_addr = __ourip;
1411 d->ourip = sin.sin_addr;
1413 for (sd = d->speeddials; sd; sd = sd->next) {
1414 sd->stateid = ast_extension_state_add(sd->context, sd->exten, skinny_extensionstate_cb, sd);
1416 for (l = d->lines; l; l = l->next) {
1417 ast_device_state_changed("Skinny/%s@%s", l->name, d->name);
1422 ast_mutex_unlock(&devicelock);
1429 static int skinny_unregister(struct skinny_req *req, struct skinnysession *s)
1431 struct skinny_device *d;
1432 struct skinny_line *l;
1433 struct skinny_speeddial *sd;
1441 for (sd = d->speeddials; sd; sd = sd->next) {
1442 if (sd->stateid > -1)
1443 ast_extension_state_del(sd->stateid, NULL);
1445 for (l = d->lines; l; l = l->next) {
1446 ast_device_state_changed("Skinny/%s@%s", l->name, d->name);
1450 return -1; /* main loop will destroy the session */
1453 static int transmit_response(struct skinnysession *s, struct skinny_req *req)
1458 ast_log(LOG_WARNING, "Asked to transmit to a non-existant session!\n");
1462 ast_mutex_lock(&s->lock);
1465 ast_log(LOG_VERBOSE, "writing packet type %04X (%d bytes) to socket %d\n", letohl(req->e), letohl(req->len)+8, s->fd);
1467 if (letohl(req->len > SKINNY_MAX_PACKET) || letohl(req->len < 0)) {
1468 ast_log(LOG_WARNING, "transmit_response: the length of the request is out of bounds\n");
1472 memset(s->outbuf,0,sizeof(s->outbuf));
1473 memcpy(s->outbuf, req, skinny_header_size);
1474 memcpy(s->outbuf+skinny_header_size, &req->data, letohl(req->len));
1476 res = write(s->fd, s->outbuf, letohl(req->len)+8);
1478 if (res != letohl(req->len)+8) {
1479 ast_log(LOG_WARNING, "Transmit: write only sent %d out of %d bytes: %s\n", res, letohl(req->len)+8, strerror(errno));
1482 ast_log(LOG_WARNING, "Transmit: Skinny Client was lost, unregistering\n");
1483 skinny_unregister(NULL, s);
1488 ast_mutex_unlock(&s->lock);
1492 static void transmit_speaker_mode(struct skinnysession *s, int mode)
1494 struct skinny_req *req;
1496 if (!(req = req_alloc(sizeof(struct set_speaker_message), SET_SPEAKER_MESSAGE)))
1499 req->data.setspeaker.mode = htolel(mode);
1500 transmit_response(s, req);
1503 static void transmit_microphone_mode(struct skinnysession *s, int mode)
1505 struct skinny_req *req;
1507 if (!(req = req_alloc(sizeof(struct set_microphone_message), SET_MICROPHONE_MESSAGE)))
1510 req->data.setmicrophone.mode = htolel(mode);
1511 transmit_response(s, req);
1514 static void transmit_callstate(struct skinnysession *s, int instance, int state, unsigned callid)
1516 struct skinny_req *req;
1518 if (!(req = req_alloc(sizeof(struct call_state_message), CALL_STATE_MESSAGE)))
1521 if (state == SKINNY_ONHOOK) {
1522 transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
1524 req->data.callstate.callState = htolel(state);
1525 req->data.callstate.lineInstance = htolel(instance);
1526 req->data.callstate.callReference = htolel(callid);
1527 transmit_response(s, req);
1528 if (state == SKINNY_OFFHOOK) {
1529 if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
1532 req->data.activatecallplane.lineInstance = htolel(instance);
1533 transmit_response(s, req);
1534 } else if (state == SKINNY_ONHOOK) {
1535 if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
1538 req->data.activatecallplane.lineInstance = htolel(instance);
1539 transmit_response(s, req);
1541 if (!(req = req_alloc(sizeof(struct close_receive_channel_message), CLOSE_RECEIVE_CHANNEL_MESSAGE)))
1544 req->data.closereceivechannel.conferenceId = 0;
1545 req->data.closereceivechannel.partyId = htolel(callid);
1546 transmit_response(s, req);
1548 if (!(req = req_alloc(sizeof(struct stop_media_transmission_message), STOP_MEDIA_TRANSMISSION_MESSAGE)))
1551 req->data.stopmedia.conferenceId = 0;
1552 req->data.stopmedia.passThruPartyId = htolel(callid);
1553 transmit_response(s, req);
1557 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)
1559 struct skinny_req *req;
1561 if (!(req = req_alloc(sizeof(struct call_info_message), CALL_INFO_MESSAGE)))
1565 ast_verbose("Setting Callinfo to %s(%s) from %s(%s) on %s(%d)\n", fromname, fromnum, toname, tonum, s->device->name, instance);
1568 ast_copy_string(req->data.callinfo.callingPartyName, fromname, sizeof(req->data.callinfo.callingPartyName));
1571 ast_copy_string(req->data.callinfo.callingParty, fromnum, sizeof(req->data.callinfo.callingParty));
1574 ast_copy_string(req->data.callinfo.calledPartyName, toname, sizeof(req->data.callinfo.calledPartyName));
1577 ast_copy_string(req->data.callinfo.calledParty, tonum, sizeof(req->data.callinfo.calledParty));
1579 req->data.callinfo.instance = htolel(instance);
1580 req->data.callinfo.reference = htolel(callid);
1581 req->data.callinfo.type = htolel(calltype);
1582 transmit_response(s, req);
1585 static void transmit_connect(struct skinnysession *s, struct skinny_subchannel *sub)
1587 struct skinny_req *req;
1588 struct skinny_line *l = sub->parent;
1589 struct ast_format_list fmt;
1591 if (!(req = req_alloc(sizeof(struct open_receive_channel_message), OPEN_RECEIVE_CHANNEL_MESSAGE)))
1594 fmt = ast_codec_pref_getsize(&l->prefs, ast_best_codec(l->capability));
1596 req->data.openreceivechannel.conferenceId = htolel(0);
1597 req->data.openreceivechannel.partyId = htolel(sub->callid);
1598 req->data.openreceivechannel.packets = htolel(fmt.cur_ms);
1599 req->data.openreceivechannel.capability = htolel(codec_ast2skinny(fmt.bits));
1600 req->data.openreceivechannel.echo = htolel(0);
1601 req->data.openreceivechannel.bitrate = htolel(0);
1602 transmit_response(s, req);
1605 static void transmit_tone(struct skinnysession *s, int tone)
1607 struct skinny_req *req;
1609 if (tone == SKINNY_NOTONE) {
1610 /* This is bad, mmm'kay? */
1615 if (!(req = req_alloc(sizeof(struct start_tone_message), START_TONE_MESSAGE)))
1618 if (!(req = req_alloc(sizeof(struct stop_tone_message), STOP_TONE_MESSAGE)))
1623 req->data.starttone.tone = htolel(tone);
1625 transmit_response(s, req);
1628 static void transmit_selectsoftkeys(struct skinnysession *s, int instance, int callid, int softkey)
1630 struct skinny_req *req;
1632 if (!(req = req_alloc(sizeof(struct select_soft_keys_message), SELECT_SOFT_KEYS_MESSAGE)))
1635 req->data.selectsoftkey.instance = htolel(instance);
1636 req->data.selectsoftkey.reference = htolel(callid);
1637 req->data.selectsoftkey.softKeySetIndex = htolel(softkey);
1638 req->data.selectsoftkey.validKeyMask = htolel(0xFFFFFFFF);
1639 transmit_response(s, req);
1642 static void transmit_lamp_indication(struct skinnysession *s, int stimulus, int instance, int indication)
1644 struct skinny_req *req;
1646 if (!(req = req_alloc(sizeof(struct set_lamp_message), SET_LAMP_MESSAGE)))
1649 req->data.setlamp.stimulus = htolel(stimulus);
1650 req->data.setlamp.stimulusInstance = htolel(instance);
1651 req->data.setlamp.deviceStimulus = htolel(indication);
1652 transmit_response(s, req);
1655 static void transmit_ringer_mode(struct skinnysession *s, int mode)
1657 struct skinny_req *req;
1660 ast_verbose("Setting ringer mode to '%d'.\n", mode);
1662 if (!(req = req_alloc(sizeof(struct set_ringer_message), SET_RINGER_MESSAGE)))
1665 req->data.setringer.ringerMode = htolel(mode);
1666 /* XXX okay, I don't quite know what this is, but here's what happens (on a 7960).
1667 Note: The phone will always show as ringing on the display.
1669 1: phone will audibly ring over and over
1670 2: phone will audibly ring only once
1671 any other value, will NOT cause the phone to audibly ring
1673 req->data.setringer.unknown1 = htolel(1);
1674 /* XXX the value here doesn't seem to change anything. Must be higher than 0.
1675 Perhaps a packet capture can shed some light on this. */
1676 req->data.setringer.unknown2 = htolel(1);
1677 transmit_response(s, req);
1680 static void transmit_displaymessage(struct skinnysession *s, const char *text)
1682 struct skinny_req *req;
1685 if (!(req = req_alloc(0, CLEAR_DISPLAY_MESSAGE)))
1689 ast_verbose("Clearing Display\n");
1691 if (!(req = req_alloc(sizeof(struct displaytext_message), DISPLAYTEXT_MESSAGE)))
1694 ast_copy_string(req->data.displaytext.text, text, sizeof(req->data.displaytext.text));
1696 ast_verbose("Displaying message '%s'\n", req->data.displaytext.text);
1699 transmit_response(s, req);
1702 static void transmit_displaynotify(struct skinnysession *s, const char *text, int t)
1704 struct skinny_req *req;
1706 if (!(req = req_alloc(sizeof(struct display_notify_message), DISPLAY_NOTIFY_MESSAGE)))
1709 ast_copy_string(req->data.displaynotify.displayMessage, text, sizeof(req->data.displaynotify.displayMessage));
1710 req->data.displaynotify.displayTimeout = htolel(t);
1713 ast_verbose("Displaying notify '%s'\n", text);
1715 transmit_response(s, req);
1718 static void transmit_displaypromptstatus(struct skinnysession *s, const char *text, int t, int instance, int callid)
1720 struct skinny_req *req;
1722 if (!(req = req_alloc(sizeof(struct display_prompt_status_message), DISPLAY_PROMPT_STATUS_MESSAGE)))
1725 ast_copy_string(req->data.displaypromptstatus.promptMessage, text, sizeof(req->data.displaypromptstatus.promptMessage));
1726 req->data.displaypromptstatus.messageTimeout = htolel(t);
1727 req->data.displaypromptstatus.lineInstance = htolel(instance);
1728 req->data.displaypromptstatus.callReference = htolel(callid);
1731 ast_verbose("Displaying Prompt Status '%s'\n", text);
1733 transmit_response(s, req);
1736 static void transmit_dialednumber(struct skinnysession *s, const char *text, int instance, int callid)
1738 struct skinny_req *req;
1740 if (!(req = req_alloc(sizeof(struct dialed_number_message), DIALED_NUMBER_MESSAGE)))
1743 ast_copy_string(req->data.dialednumber.dialedNumber, text, sizeof(req->data.dialednumber.dialedNumber));
1744 req->data.dialednumber.lineInstance = htolel(instance);
1745 req->data.dialednumber.callReference = htolel(callid);
1747 transmit_response(s, req);
1750 static int skinny_extensionstate_cb(char *context, char *exten, int state, void *data)
1752 struct skinny_speeddial *sd = data;
1753 struct skinny_device *d = sd->parent;
1754 struct skinnysession *s = d->session;
1755 char hint[AST_MAX_EXTENSION];
1756 int callstate = SKINNY_CALLREMOTEMULTILINE;
1757 int lamp = SKINNY_LAMP_OFF;
1760 case AST_EXTENSION_DEACTIVATED: /* Retry after a while */
1761 case AST_EXTENSION_REMOVED: /* Extension is gone */
1762 ast_verbose(VERBOSE_PREFIX_2 "Extension state: Watcher for hint %s %s. Notify Device %s\n", exten, state == AST_EXTENSION_DEACTIVATED ? "deactivated" : "removed", d->name);
1764 callstate = SKINNY_ONHOOK;
1765 lamp = SKINNY_LAMP_OFF;
1767 case AST_EXTENSION_RINGING:
1768 case AST_EXTENSION_UNAVAILABLE:
1769 callstate = SKINNY_RINGIN;
1770 lamp = SKINNY_LAMP_BLINK;
1772 case AST_EXTENSION_BUSY: /* callstate = SKINNY_BUSY wasn't wanting to work - I'll settle for this */
1773 case AST_EXTENSION_INUSE:
1774 callstate = SKINNY_CALLREMOTEMULTILINE;
1775 lamp = SKINNY_LAMP_ON;
1777 case AST_EXTENSION_ONHOLD:
1778 callstate = SKINNY_HOLD;
1779 lamp = SKINNY_LAMP_WINK;
1781 case AST_EXTENSION_NOT_INUSE:
1783 callstate = SKINNY_ONHOOK;
1784 lamp = SKINNY_LAMP_OFF;
1788 if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, sd->context, sd->exten)) {
1789 /* If they are not registered, we will override notification and show no availability */
1790 if (ast_device_state(hint) == AST_DEVICE_UNAVAILABLE) {
1791 callstate = SKINNY_ONHOOK;
1792 lamp = SKINNY_LAMP_FLASH;
1796 transmit_lamp_indication(s, STIMULUS_LINE, sd->instance, lamp);
1797 transmit_callstate(s, sd->instance, callstate, 0);
1798 sd->laststate = state;
1803 static int has_voicemail(struct skinny_line *l)
1805 return ast_app_has_voicemail(l->mailbox, NULL);
1808 static void do_housekeeping(struct skinnysession *s)
1812 int device_lamp = 0;
1813 struct skinny_device *d = s->device;
1814 struct skinny_line *l;
1816 /* Update time on device */
1817 handle_time_date_req_message(NULL, s);
1819 /* Set MWI on individual lines */
1820 for (l = d->lines; l; l = l->next) {
1821 if (has_voicemail(l)) {
1823 ast_verbose("Checking for voicemail Skinny %s@%s\n", l->name, d->name);
1824 ast_app_inboxcount(l->mailbox, &new, &old);
1826 ast_verbose("Skinny %s@%s has voicemail!\n", l->name, d->name);
1827 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, l->instance, l->mwiblink?SKINNY_LAMP_BLINK:SKINNY_LAMP_ON);
1830 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, l->instance, SKINNY_LAMP_OFF);
1833 /* If at least one line has VM, turn the device level lamp on */
1835 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, 0, SKINNY_LAMP_ON);
1837 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, 0, SKINNY_LAMP_OFF);
1840 /* I do not believe skinny can deal with video.
1841 Anyone know differently? */
1842 /* Yes, it can. Currently 7985 and Cisco VT Advantage do video. */
1843 static enum ast_rtp_get_result skinny_get_vrtp_peer(struct ast_channel *c, struct ast_rtp **rtp)
1845 struct skinny_subchannel *sub = NULL;
1847 if (!(sub = c->tech_pvt) || !(sub->vrtp))
1848 return AST_RTP_GET_FAILED;
1852 return AST_RTP_TRY_NATIVE;
1855 static enum ast_rtp_get_result skinny_get_rtp_peer(struct ast_channel *c, struct ast_rtp **rtp)
1857 struct skinny_subchannel *sub = NULL;
1859 if (!(sub = c->tech_pvt) || !(sub->rtp))
1860 return AST_RTP_GET_FAILED;
1864 return AST_RTP_TRY_NATIVE;
1867 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)
1869 struct skinny_subchannel *sub;
1872 /* transmit_modify_with_sdp(sub, rtp); @@FIXME@@ if needed */
1878 static struct ast_rtp_protocol skinny_rtp = {
1880 .get_rtp_info = skinny_get_rtp_peer,
1881 .get_vrtp_info = skinny_get_vrtp_peer,
1882 .set_rtp_peer = skinny_set_rtp_peer,
1885 static int skinny_do_debug(int fd, int argc, char *argv[])
1888 return RESULT_SHOWUSAGE;
1891 ast_cli(fd, "Skinny Debugging Enabled\n");
1892 return RESULT_SUCCESS;
1895 static int skinny_no_debug(int fd, int argc, char *argv[])
1898 return RESULT_SHOWUSAGE;
1901 ast_cli(fd, "Skinny Debugging Disabled\n");
1902 return RESULT_SUCCESS;
1905 static char *complete_skinny_reset(const char *line, const char *word, int pos, int state)
1907 struct skinny_device *d;
1909 char *result = NULL;
1910 int wordlen = strlen(word);
1914 for (d = devices; d && !result; d = d->next) {
1915 if (!strncasecmp(word, d->id, wordlen) && ++which > state)
1916 result = ast_strdup(d->id);
1923 static int skinny_reset_device(int fd, int argc, char *argv[])
1925 struct skinny_device *d;
1926 struct skinny_req *req;
1928 if (argc < 3 || argc > 4) {
1929 return RESULT_SHOWUSAGE;
1931 ast_mutex_lock(&devicelock);
1933 for (d = devices; d; d = d->next) {
1934 int fullrestart = 0;
1935 if (!strcasecmp(argv[2], d->id) || !strcasecmp(argv[2], "all")) {
1939 if (!(req = req_alloc(sizeof(struct reset_message), RESET_MESSAGE)))
1942 if (argc == 4 && !strcasecmp(argv[3], "restart"))
1946 req->data.reset.resetType = 2;
1948 req->data.reset.resetType = 1;
1950 if (option_verbose > 2)
1951 ast_verbose(VERBOSE_PREFIX_3 "%s device %s.\n", (fullrestart) ? "Restarting" : "Resetting", d->id);
1952 transmit_response(d->session, req);
1955 ast_mutex_unlock(&devicelock);
1956 return RESULT_SUCCESS;
1959 static char *device2str(int type)
1964 case SKINNY_DEVICE_NONE:
1966 case SKINNY_DEVICE_30SPPLUS:
1968 case SKINNY_DEVICE_12SPPLUS:
1970 case SKINNY_DEVICE_12SP:
1972 case SKINNY_DEVICE_12:
1974 case SKINNY_DEVICE_30VIP:
1976 case SKINNY_DEVICE_7910:
1978 case SKINNY_DEVICE_7960:
1980 case SKINNY_DEVICE_7940:
1982 case SKINNY_DEVICE_7935:
1984 case SKINNY_DEVICE_ATA186:
1986 case SKINNY_DEVICE_7941:
1988 case SKINNY_DEVICE_7971:
1990 case SKINNY_DEVICE_7985:
1992 case SKINNY_DEVICE_7911:
1994 case SKINNY_DEVICE_7961GE:
1996 case SKINNY_DEVICE_7941GE:
1998 case SKINNY_DEVICE_7905:
2000 case SKINNY_DEVICE_7920:
2002 case SKINNY_DEVICE_7970:
2004 case SKINNY_DEVICE_7912:
2006 case SKINNY_DEVICE_7902:
2008 case SKINNY_DEVICE_CIPC:
2009 return "IP Communicator";
2010 case SKINNY_DEVICE_7961:
2012 case SKINNY_DEVICE_7936:
2014 case SKINNY_DEVICE_SCCPGATEWAY_AN:
2015 return "SCCPGATEWAY_AN";
2016 case SKINNY_DEVICE_SCCPGATEWAY_BRI:
2017 return "SCCPGATEWAY_BRI";
2018 case SKINNY_DEVICE_UNKNOWN:
2021 if (!(tmp = ast_threadstorage_get(&device2str_threadbuf, DEVICE2STR_BUFSIZE)))
2023 snprintf(tmp, DEVICE2STR_BUFSIZE, "UNKNOWN-%d", type);
2028 static int skinny_show_devices(int fd, int argc, char *argv[])
2030 struct skinny_device *d;
2031 struct skinny_line *l;
2035 return RESULT_SHOWUSAGE;
2037 ast_mutex_lock(&devicelock);
2039 ast_cli(fd, "Name DeviceId IP Type R NL\n");
2040 ast_cli(fd, "-------------------- ---------------- --------------- --------------- - --\n");
2041 for (d = devices; d; d = d->next) {
2043 for (l = d->lines; l; l = l->next) {
2047 ast_cli(fd, "%-20s %-16s %-15s %-15s %c %2d\n",
2050 d->session?ast_inet_ntoa(d->session->sin.sin_addr):"",
2051 device2str(d->type),
2052 d->registered?'Y':'N',
2055 ast_mutex_unlock(&devicelock);
2056 return RESULT_SUCCESS;
2059 static int skinny_show_lines(int fd, int argc, char *argv[])
2061 struct skinny_device *d;
2062 struct skinny_line *l;
2065 return RESULT_SHOWUSAGE;
2067 ast_mutex_lock(&devicelock);
2069 ast_cli(fd, "Device Name Instance Name Label \n");
2070 ast_cli(fd, "-------------------- -------- -------------------- --------------------\n");
2071 for (d = devices; d; d = d->next) {
2072 for (l = d->lines; l; l = l->next) {
2073 ast_cli(fd, "%-20s %8d %-20s %-20s\n",
2081 ast_mutex_unlock(&devicelock);
2082 return RESULT_SUCCESS;
2085 static const char show_devices_usage[] =
2086 "Usage: skinny show devices\n"
2087 " Lists all devices known to the Skinny subsystem.\n";
2089 static const char show_lines_usage[] =
2090 "Usage: skinny show lines\n"
2091 " Lists all lines known to the Skinny subsystem.\n";
2093 static const char debug_usage[] =
2094 "Usage: skinny set debug\n"
2095 " Enables dumping of Skinny packets for debugging purposes\n";
2097 static const char no_debug_usage[] =
2098 "Usage: skinny set debug off\n"
2099 " Disables dumping of Skinny packets for debugging purposes\n";
2101 static const char reset_usage[] =
2102 "Usage: skinny reset <DeviceId|all> [restart]\n"
2103 " Causes a Skinny device to reset itself, optionally with a full restart\n";
2105 static struct ast_cli_entry cli_skinny[] = {
2106 { { "skinny", "show", "devices", NULL },
2107 skinny_show_devices, "List defined Skinny devices",
2108 show_devices_usage },
2110 { { "skinny", "show", "lines", NULL },
2111 skinny_show_lines, "List defined Skinny lines per device",
2114 { { "skinny", "set", "debug", NULL },
2115 skinny_do_debug, "Enable Skinny debugging",
2118 { { "skinny", "set", "debug", "off", NULL },
2119 skinny_no_debug, "Disable Skinny debugging",
2122 { { "skinny", "reset", NULL },
2123 skinny_reset_device, "Reset Skinny device(s)",
2124 reset_usage, complete_skinny_reset },
2128 static struct skinny_paging_device *build_paging_device(const char *cat, struct ast_variable *v)
2134 static struct skinny_device *build_device(const char *cat, struct ast_variable *v)
2136 struct skinny_device *d;
2137 struct skinny_line *l;
2138 struct skinny_speeddial *sd;
2139 struct skinny_addon *a;
2140 char device_vmexten[AST_MAX_EXTENSION];
2141 int lineInstance = 1;
2142 int speeddialInstance = 1;
2145 if (!(d = ast_calloc(1, sizeof(*d)))) {
2148 ast_copy_string(d->name, cat, sizeof(d->name));
2149 d->lastlineinstance = 1;
2150 d->capability = default_capability;
2151 d->prefs = default_prefs;
2152 if (!ast_strlen_zero(vmexten))
2153 ast_copy_string(device_vmexten, vmexten, sizeof(device_vmexten));
2155 if (!strcasecmp(v->name, "host")) {
2156 if (ast_get_ip(&d->addr, v->value)) {
2160 } else if (!strcasecmp(v->name, "port")) {
2161 d->addr.sin_port = htons(atoi(v->value));
2162 } else if (!strcasecmp(v->name, "device")) {
2163 ast_copy_string(d->id, v->value, sizeof(d->id));
2164 } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
2165 d->ha = ast_append_ha(v->name, v->value, d->ha, NULL);
2166 } else if (!strcasecmp(v->name, "vmexten")) {
2167 ast_copy_string(device_vmexten, v->value, sizeof(device_vmexten));
2168 } else if (!strcasecmp(v->name, "context")) {
2169 ast_copy_string(context, v->value, sizeof(context));
2170 } else if (!strcasecmp(v->name, "allow")) {
2171 ast_parse_allow_disallow(&d->prefs, &d->capability, v->value, 1);
2172 } else if (!strcasecmp(v->name, "disallow")) {
2173 ast_parse_allow_disallow(&d->prefs, &d->capability, v->value, 0);
2174 } else if (!strcasecmp(v->name, "version")) {
2175 ast_copy_string(d->version_id, v->value, sizeof(d->version_id));
2176 } else if (!strcasecmp(v->name, "nat")) {
2177 nat = ast_true(v->value);
2178 } else if (!strcasecmp(v->name, "callerid")) {
2179 if (!strcasecmp(v->value, "asreceived")) {
2183 ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
2185 } else if (!strcasecmp(v->name, "language")) {
2186 ast_copy_string(language, v->value, sizeof(language));
2187 } else if (!strcasecmp(v->name, "accountcode")) {
2188 ast_copy_string(accountcode, v->value, sizeof(accountcode));
2189 } else if (!strcasecmp(v->name, "amaflags")) {
2190 y = ast_cdr_amaflags2int(v->value);
2192 ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno);
2196 } else if (!strcasecmp(v->name, "mohinterpret") || !strcasecmp(v->name, "musiconhold")) {
2197 ast_copy_string(mohinterpret, v->value, sizeof(mohinterpret));
2198 } else if (!strcasecmp(v->name, "mohsuggest")) {
2199 ast_copy_string(mohsuggest, v->value, sizeof(mohsuggest));
2200 } else if (!strcasecmp(v->name, "callgroup")) {
2201 cur_callergroup = ast_get_group(v->value);
2202 } else if (!strcasecmp(v->name, "pickupgroup")) {
2203 cur_pickupgroup = ast_get_group(v->value);
2204 } else if (!strcasecmp(v->name, "immediate")) {
2205 immediate = ast_true(v->value);
2206 } else if (!strcasecmp(v->name, "cancallforward")) {
2207 cancallforward = ast_true(v->value);
2208 } else if (!strcasecmp(v->name, "mailbox")) {
2209 ast_copy_string(mailbox, v->value, sizeof(mailbox));
2210 } else if (!strcasecmp(v->name, "callreturn")) {
2211 callreturn = ast_true(v->value);
2212 } else if (!strcasecmp(v->name, "callwaiting")) {
2213 callwaiting = ast_true(v->value);
2214 } else if (!strcasecmp(v->name, "transfer")) {
2215 transfer = ast_true(v->value);
2216 } else if (!strcasecmp(v->name, "threewaycalling")) {
2217 threewaycalling = ast_true(v->value);
2218 } else if (!strcasecmp(v->name, "mwiblink")) {
2219 mwiblink = ast_true(v->value);
2220 } else if (!strcasecmp(v->name, "linelabel")) {
2221 ast_copy_string(linelabel, v->value, sizeof(linelabel));
2222 } else if (!strcasecmp(v->name, "speeddial")) {
2223 if (!(sd = ast_calloc(1, sizeof(*sd)))) {
2226 char *stringp, *exten, *context, *label;
2228 exten = strsep(&stringp, ",");
2229 if ((context = strchr(exten, '@'))) {
2233 ast_mutex_init(&sd->lock);
2234 ast_copy_string(sd->exten, exten, sizeof(sd->exten));
2235 if (!ast_strlen_zero(context)) {
2237 sd->instance = lineInstance++;
2238 ast_copy_string(sd->context, context, sizeof(sd->context));
2241 sd->instance = speeddialInstance++;
2242 sd->context[0] = '\0';
2244 ast_copy_string(sd->label, S_OR(label, exten), sizeof(sd->label));
2248 sd->next = d->speeddials;
2251 } else if (!strcasecmp(v->name, "addon")) {
2252 if (!(a = ast_calloc(1, sizeof(*a)))) {
2255 ast_mutex_init(&a->lock);
2256 ast_copy_string(a->type, v->value, sizeof(a->type));
2258 a->next = d->addons;
2261 } else if (!strcasecmp(v->name, "trunk") || !strcasecmp(v->name, "line")) {
2262 if (!(l = ast_calloc(1, sizeof(*l)))) {
2265 ast_mutex_init(&l->lock);
2266 ast_copy_string(l->name, v->value, sizeof(l->name));
2268 /* XXX Should we check for uniqueness?? XXX */
2269 ast_copy_string(l->context, context, sizeof(l->context));
2270 ast_copy_string(l->cid_num, cid_num, sizeof(l->cid_num));
2271 ast_copy_string(l->cid_name, cid_name, sizeof(l->cid_name));
2272 ast_copy_string(l->label, linelabel, sizeof(l->label));
2273 ast_copy_string(l->language, language, sizeof(l->language));
2274 ast_copy_string(l->mohinterpret, mohinterpret, sizeof(l->mohinterpret));
2275 ast_copy_string(l->mohsuggest, mohsuggest, sizeof(l->mohsuggest));
2276 ast_copy_string(l->mailbox, mailbox, sizeof(l->mailbox));
2277 ast_copy_string(l->mailbox, mailbox, sizeof(l->mailbox));
2278 if (!ast_strlen_zero(mailbox)) {
2279 if (option_verbose > 2)
2280 ast_verbose(VERBOSE_PREFIX_3 "Setting mailbox '%s' on %s@%s\n", mailbox, d->name, l->name);
2282 if (!ast_strlen_zero(device_vmexten))
2283 ast_copy_string(l->vmexten, device_vmexten, sizeof(vmexten));
2285 l->capability = d->capability;
2286 l->prefs = d->prefs;
2288 if (!strcasecmp(v->name, "trunk")) {
2289 l->type = TYPE_TRUNK;
2291 l->type = TYPE_LINE;
2293 l->immediate = immediate;
2294 l->callgroup = cur_callergroup;
2295 l->pickupgroup = cur_pickupgroup;
2296 l->callreturn = callreturn;
2297 l->cancallforward = cancallforward;
2298 l->callwaiting = callwaiting;
2299 l->transfer = transfer;
2300 l->threewaycalling = threewaycalling;
2301 l->mwiblink = mwiblink;
2302 l->onhooktime = time(NULL);
2303 l->instance = lineInstance++;
2304 /* ASSUME we're onhook at this point */
2305 l->hookstate = SKINNY_ONHOOK;
2312 ast_log(LOG_WARNING, "Don't know keyword '%s' at line %d\n", v->name, v->lineno);
2318 ast_log(LOG_ERROR, "A Skinny device must have at least one line!\n");
2321 if (/*d->addr.sin_addr.s_addr && */!ntohs(d->addr.sin_port)) {
2322 d->addr.sin_port = htons(DEFAULT_SKINNY_PORT);
2325 /* I don't think we need this anymore at all, since d->ourip is set in skinny_register now */
2326 if (d->addr.sin_addr.s_addr) {
2327 /* XXX See note above, in 'host' option. */
2328 if (ast_ouraddrfor(&d->addr.sin_addr, &d->ourip)) {
2339 static void start_rtp(struct skinny_subchannel *sub)
2341 struct skinny_line *l = sub->parent;
2342 struct skinny_device *d = l->parent;
2345 ast_mutex_lock(&sub->lock);
2346 /* Allocate the RTP */
2347 sub->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
2349 sub->vrtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
2351 if (sub->rtp && sub->owner) {
2352 sub->owner->fds[0] = ast_rtp_fd(sub->rtp);
2353 sub->owner->fds[1] = ast_rtcp_fd(sub->rtp);
2355 if (hasvideo && sub->vrtp && sub->owner) {
2356 sub->owner->fds[2] = ast_rtp_fd(sub->vrtp);
2357 sub->owner->fds[3] = ast_rtcp_fd(sub->vrtp);
2360 ast_rtp_setnat(sub->rtp, l->nat);
2363 ast_rtp_setnat(sub->vrtp, l->nat);
2365 /* Set Frame packetization */
2367 ast_rtp_codec_setpref(sub->rtp, &l->prefs);
2369 /* Create the RTP connection */
2370 transmit_connect(d->session, sub);
2371 ast_mutex_unlock(&sub->lock);
2374 static void *skinny_newcall(void *data)
2376 struct ast_channel *c = data;
2377 struct skinny_subchannel *sub = c->tech_pvt;
2378 struct skinny_line *l = sub->parent;
2379 struct skinny_device *d = l->parent;
2380 struct skinnysession *s = d->session;
2383 ast_copy_string(l->lastnumberdialed, c->exten, sizeof(l->lastnumberdialed));
2385 l->hidecallerid ? "" : l->cid_num,
2386 l->hidecallerid ? "" : l->cid_name,
2387 c->cid.cid_ani ? NULL : l->cid_num);
2388 ast_setstate(c, AST_STATE_RING);
2389 res = ast_pbx_run(c);
2391 ast_log(LOG_WARNING, "PBX exited non-zero\n");
2392 transmit_tone(s, SKINNY_REORDER);
2397 static void *skinny_ss(void *data)
2399 struct ast_channel *c = data;
2400 struct skinny_subchannel *sub = c->tech_pvt;
2401 struct skinny_line *l = sub->parent;
2402 struct skinny_device *d = l->parent;
2403 struct skinnysession *s = d->session;
2405 int timeout = firstdigittimeout;
2408 int loop_pause = 100;
2410 if (option_verbose > 2)
2411 ast_verbose( VERBOSE_PREFIX_3 "Starting simple switch on '%s@%s'\n", l->name, d->name);
2413 len = strlen(d->exten);
2415 while (len < AST_MAX_EXTENSION-1) {
2416 res = 1; /* Assume that we will get a digit */
2417 while (strlen(d->exten) == len){
2418 ast_safe_sleep(c, loop_pause);
2419 timeout -= loop_pause;
2420 if ( (timeout -= loop_pause) <= 0){
2428 len = strlen(d->exten);
2430 if (!ast_ignore_pattern(c->context, d->exten)) {
2431 transmit_tone(s, SKINNY_SILENCE);
2433 if (ast_exists_extension(c, c->context, d->exten, 1, l->cid_num)) {
2434 if (!res || !ast_matchmore_extension(c, c->context, d->exten, 1, l->cid_num)) {
2436 /* Record this as the forwarding extension */
2437 ast_copy_string(l->call_forward, d->exten, sizeof(l->call_forward));
2438 if (option_verbose > 2)
2439 ast_verbose(VERBOSE_PREFIX_3 "Setting call forward to '%s' on channel %s\n",
2440 l->call_forward, c->name);
2441 transmit_tone(s, SKINNY_DIALTONE);
2445 ast_safe_sleep(c, 500);
2446 ast_indicate(c, -1);
2447 ast_safe_sleep(c, 1000);
2448 memset(d->exten, 0, sizeof(d->exten));
2449 transmit_tone(s, SKINNY_DIALTONE);
2453 ast_copy_string(c->exten, d->exten, sizeof(c->exten));
2454 ast_copy_string(l->lastnumberdialed, d->exten, sizeof(l->lastnumberdialed));
2455 memset(d->exten, 0, sizeof(d->exten));
2460 /* It's a match, but they just typed a digit, and there is an ambiguous match,
2461 so just set the timeout to matchdigittimeout and wait some more */
2462 timeout = matchdigittimeout;
2464 } else if (res == 0) {
2465 ast_debug(1, "Not enough digits (%s) (and no ambiguous match)...\n", d->exten);
2466 memset(d->exten, 0, sizeof(d->exten));
2467 transmit_tone(s, SKINNY_REORDER);
2468 if (sub->owner && sub->owner->_state != AST_STATE_UP) {
2469 ast_indicate(c, -1);
2473 } else if (!ast_canmatch_extension(c, c->context, d->exten, 1, c->cid.cid_num) &&
2474 ((d->exten[0] != '*') || (!ast_strlen_zero(d->exten) > 2))) {
2475 ast_log(LOG_WARNING, "Can't match [%s] from '%s' in context %s\n", d->exten, c->cid.cid_num ? c->cid.cid_num : "<Unknown Caller>", c->context);
2476 memset(d->exten, 0, sizeof(d->exten));
2477 transmit_tone(s, SKINNY_REORDER);
2478 /* hang out for 3 seconds to let congestion play */
2479 ast_safe_sleep(c, 3000);
2483 timeout = gendigittimeout;
2485 if (len && !ast_ignore_pattern(c->context, d->exten)) {
2486 ast_indicate(c, -1);
2491 memset(d->exten, 0, sizeof(d->exten));
2497 static int skinny_call(struct ast_channel *ast, char *dest, int timeout)
2501 struct skinny_subchannel *sub = ast->tech_pvt;
2502 struct skinny_line *l = sub->parent;
2503 struct skinny_device *d = l->parent;
2504 struct skinnysession *s = d->session;
2506 if (!d->registered) {
2507 ast_log(LOG_ERROR, "Device not registered, cannot call %s\n", dest);
2511 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
2512 ast_log(LOG_WARNING, "skinny_call called on %s, neither down nor reserved\n", ast->name);
2517 ast_verbose(VERBOSE_PREFIX_3 "skinny_call(%s)\n", ast->name);
2520 ast_queue_control(ast, AST_CONTROL_BUSY);
2524 switch (l->hookstate) {
2525 case SKINNY_OFFHOOK:
2526 tone = SKINNY_CALLWAITTONE;
2529 tone = SKINNY_ALERT;
2532 ast_log(LOG_ERROR, "Don't know how to deal with hookstate %d\n", l->hookstate);
2536 transmit_callstate(s, l->instance, SKINNY_RINGIN, sub->callid);
2537 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_RINGIN);
2538 transmit_displaypromptstatus(s, "Ring-In", 0, l->instance, sub->callid);
2539 transmit_callinfo(s, ast->cid.cid_name, ast->cid.cid_num, l->cid_name, l->cid_num, l->instance, sub->callid, 1);
2540 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_BLINK);
2541 transmit_ringer_mode(s, SKINNY_RING_INSIDE);
2543 ast_setstate(ast, AST_STATE_RINGING);
2544 ast_queue_control(ast, AST_CONTROL_RINGING);
2549 static int skinny_hangup(struct ast_channel *ast)
2551 struct skinny_subchannel *sub = ast->tech_pvt;
2552 struct skinny_line *l;
2553 struct skinny_device *d;
2554 struct skinnysession *s;
2557 ast_debug(1, "Asked to hangup channel not connected\n");
2564 ast_verbose("skinny_hangup(%s) on %s@%s\n", ast->name, l->name, d->name);
2566 if (d->registered) {
2567 if ((l->type = TYPE_LINE) && (l->hookstate == SKINNY_OFFHOOK)) {
2568 l->hookstate = SKINNY_ONHOOK;
2569 transmit_callstate(s, l->instance, SKINNY_ONHOOK, sub->callid);
2570 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_OFF);
2571 transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
2572 } else if ((l->type = TYPE_LINE) && (l->hookstate == SKINNY_ONHOOK)) {
2573 transmit_callstate(s, l->instance, SKINNY_ONHOOK, sub->callid);
2574 transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
2575 transmit_ringer_mode(s, SKINNY_RING_OFF);
2576 transmit_tone(s, SKINNY_SILENCE);
2577 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_OFF);
2581 ast_mutex_lock(&sub->lock);
2583 ast->tech_pvt = NULL;
2584 sub->alreadygone = 0;
2587 ast_rtp_destroy(sub->rtp);
2590 ast_mutex_unlock(&sub->lock);
2594 static int skinny_answer(struct ast_channel *ast)
2597 struct skinny_subchannel *sub = ast->tech_pvt;
2598 struct skinny_line *l = sub->parent;
2599 struct skinny_device *d = l->parent;
2600 struct skinnysession *s = d->session;
2602 sub->cxmode = SKINNY_CX_SENDRECV;
2607 ast_verbose("skinny_answer(%s) on %s@%s-%d\n", ast->name, l->name, d->name, sub->callid);
2608 if (ast->_state != AST_STATE_UP) {
2609 ast_setstate(ast, AST_STATE_UP);
2612 transmit_tone(s, SKINNY_SILENCE);
2613 /* order matters here...
2614 for some reason, transmit_callinfo must be before transmit_callstate,
2615 or you won't get keypad messages in some situations. */
2616 transmit_callinfo(s, ast->cid.cid_name, ast->cid.cid_num, ast->exten, ast->exten, l->instance, sub->callid, 2);
2617 transmit_callstate(s, l->instance, SKINNY_CONNECTED, sub->callid);
2618 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_CONNECTED);
2619 transmit_displaypromptstatus(s, "Connected", 0, l->instance, sub->callid);
2623 /* Retrieve audio/etc from channel. Assumes sub->lock is already held. */
2624 static struct ast_frame *skinny_rtp_read(struct skinny_subchannel *sub)
2626 struct ast_channel *ast = sub->owner;
2627 struct ast_frame *f;
2630 /* We have no RTP allocated for this channel */
2631 return &ast_null_frame;
2636 f = ast_rtp_read(sub->rtp); /* RTP Audio */
2639 f = ast_rtcp_read(sub->rtp); /* RTCP Control Channel */
2642 f = ast_rtp_read(sub->vrtp); /* RTP Video */
2645 f = ast_rtcp_read(sub->vrtp); /* RTCP Control Channel for video */
2649 /* Not yet supported */
2650 f = ast_udptl_read(sub->udptl); /* UDPTL for T.38 */
2654 f = &ast_null_frame;
2658 /* We already hold the channel lock */
2659 if (f->frametype == AST_FRAME_VOICE) {
2660 if (f->subclass != ast->nativeformats) {
2661 ast_debug(1, "Oooh, format changed to %d\n", f->subclass);
2662 ast->nativeformats = f->subclass;
2663 ast_set_read_format(ast, ast->readformat);
2664 ast_set_write_format(ast, ast->writeformat);
2671 static struct ast_frame *skinny_read(struct ast_channel *ast)
2673 struct ast_frame *fr;
2674 struct skinny_subchannel *sub = ast->tech_pvt;
2675 ast_mutex_lock(&sub->lock);
2676 fr = skinny_rtp_read(sub);
2677 ast_mutex_unlock(&sub->lock);
2681 static int skinny_write(struct ast_channel *ast, struct ast_frame *frame)
2683 struct skinny_subchannel *sub = ast->tech_pvt;
2685 if (frame->frametype != AST_FRAME_VOICE) {
2686 if (frame->frametype == AST_FRAME_IMAGE) {
2689 ast_log(LOG_WARNING, "Can't send %d type frames with skinny_write\n", frame->frametype);
2693 if (!(frame->subclass & ast->nativeformats)) {
2694 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
2695 frame->subclass, ast->nativeformats, ast->readformat, ast->writeformat);
2700 ast_mutex_lock(&sub->lock);
2702 res = ast_rtp_write(sub->rtp, frame);
2704 ast_mutex_unlock(&sub->lock);
2709 static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
2711 struct skinny_subchannel *sub = newchan->tech_pvt;
2712 ast_log(LOG_NOTICE, "skinny_fixup(%s, %s)\n", oldchan->name, newchan->name);
2713 if (sub->owner != oldchan) {
2714 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, sub->owner);
2717 sub->owner = newchan;
2721 static int skinny_senddigit_begin(struct ast_channel *ast, char digit)
2723 return -1; /* Start inband indications */
2726 static int skinny_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
2729 struct skinny_subchannel *sub = ast->tech_pvt;
2730 struct skinny_line *l = sub->parent;
2731 struct skinny_device *d = l->parent;
2734 sprintf(tmp, "%d", digit);
2735 transmit_tone(d->session, digit);
2737 return -1; /* Stop inband indications */
2740 static char *control2str(int ind) {
2744 case AST_CONTROL_HANGUP:
2745 return "Other end has hungup";
2746 case AST_CONTROL_RING:
2747 return "Local ring";
2748 case AST_CONTROL_RINGING:
2749 return "Remote end is ringing";
2750 case AST_CONTROL_ANSWER:
2751 return "Remote end has answered";
2752 case AST_CONTROL_BUSY:
2753 return "Remote end is busy";
2754 case AST_CONTROL_TAKEOFFHOOK:
2755 return "Make it go off hook";
2756 case AST_CONTROL_OFFHOOK:
2757 return "Line is off hook";
2758 case AST_CONTROL_CONGESTION:
2759 return "Congestion (circuits busy)";
2760 case AST_CONTROL_FLASH:
2761 return "Flash hook";
2762 case AST_CONTROL_WINK:
2764 case AST_CONTROL_OPTION:
2765 return "Set a low-level option";
2766 case AST_CONTROL_RADIO_KEY:
2768 case AST_CONTROL_RADIO_UNKEY:
2769 return "Un-Key Radio";
2770 case AST_CONTROL_PROGRESS:
2771 return "Remote end is making Progress";
2772 case AST_CONTROL_PROCEEDING:
2773 return "Remote end is proceeding";
2774 case AST_CONTROL_HOLD:
2776 case AST_CONTROL_UNHOLD:
2781 if (!(tmp = ast_threadstorage_get(&control2str_threadbuf, CONTROL2STR_BUFSIZE)))
2783 snprintf(tmp, CONTROL2STR_BUFSIZE, "UNKNOWN-%d", ind);
2789 static int skinny_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen)
2791 struct skinny_subchannel *sub = ast->tech_pvt;
2792 struct skinny_line *l = sub->parent;
2793 struct skinny_device *d = l->parent;
2794 struct skinnysession *s = d->session;
2797 ast_verbose(VERBOSE_PREFIX_3 "Asked to indicate '%s' condition on channel %s\n", control2str(ind), ast->name);
2799 case AST_CONTROL_RINGING:
2800 if (ast->_state != AST_STATE_UP) {
2801 if (!sub->progress) {
2802 transmit_tone(s, SKINNY_ALERT);
2803 transmit_callstate(s, l->instance, SKINNY_RINGOUT, sub->callid);
2804 transmit_dialednumber(s, ast->exten, l->instance, sub->callid);
2805 transmit_displaypromptstatus(s, "Ring Out", 0, l->instance, sub->callid);
2806 transmit_callinfo(s, ast->cid.cid_name, ast->cid.cid_num, ast->exten, ast->exten, l->instance, sub->callid, 2); /* 2 = outgoing from phone */
2812 case AST_CONTROL_BUSY:
2813 if (ast->_state != AST_STATE_UP) {
2814 transmit_tone(s, SKINNY_BUSYTONE);
2815 transmit_callstate(s, l->instance, SKINNY_BUSY, sub->callid);
2816 sub->alreadygone = 1;
2817 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
2821 case AST_CONTROL_CONGESTION:
2822 if (ast->_state != AST_STATE_UP) {
2823 transmit_tone(s, SKINNY_REORDER);
2824 transmit_callstate(s, l->instance, SKINNY_CONGESTION, sub->callid);
2825 sub->alreadygone = 1;
2826 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
2830 case AST_CONTROL_PROGRESS:
2831 if ((ast->_state != AST_STATE_UP) && !sub->progress && !sub->outgoing) {
2832 transmit_tone(s, SKINNY_ALERT);
2833 transmit_callstate(s, l->instance, SKINNY_PROGRESS, sub->callid);
2834 transmit_displaypromptstatus(s, "Call Progress", 0, l->instance, sub->callid);
2835 transmit_callinfo(s, ast->cid.cid_name, ast->cid.cid_num, ast->exten, ast->exten, l->instance, sub->callid, 2); /* 2 = outgoing from phone */
2841 transmit_tone(s, SKINNY_SILENCE);
2843 case AST_CONTROL_HOLD:
2844 ast_moh_start(ast, data, l->mohinterpret);
2846 case AST_CONTROL_UNHOLD:
2849 case AST_CONTROL_PROCEEDING:
2852 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", ind);
2858 static struct ast_channel *skinny_new(struct skinny_line *l, int state)
2860 struct ast_channel *tmp;
2861 struct skinny_subchannel *sub;
2862 struct skinny_device *d = l->parent;
2865 tmp = ast_channel_alloc(1, state, l->cid_num, l->cid_name, l->accountcode, l->exten, l->context, l->amaflags, "Skinny/%s@%s-%d", l->name, d->name, callnums);
2867 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
2870 sub = ast_calloc(1, sizeof(*sub));
2872 ast_log(LOG_WARNING, "Unable to allocate Skinny subchannel\n");
2875 ast_mutex_init(&sub->lock);
2878 sub->callid = callnums++;
2879 d->lastlineinstance = l->instance;
2880 d->lastcallreference = sub->callid;
2881 sub->cxmode = SKINNY_CX_INACTIVE;
2889 tmp->tech = &skinny_tech;
2890 tmp->tech_pvt = sub;
2891 tmp->nativeformats = l->capability;
2892 if (!tmp->nativeformats)
2893 tmp->nativeformats = default_capability;
2894 fmt = ast_best_codec(tmp->nativeformats);
2896 ast_verbose("skinny_new: tmp->nativeformats=%d fmt=%d\n", tmp->nativeformats, fmt);
2898 tmp->fds[0] = ast_rtp_fd(sub->rtp);
2900 if (state == AST_STATE_RING) {
2903 tmp->writeformat = fmt;
2904 tmp->rawwriteformat = fmt;
2905 tmp->readformat = fmt;
2906 tmp->rawreadformat = fmt;
2907 if (!ast_strlen_zero(l->language))
2908 ast_string_field_set(tmp, language, l->language);
2909 if (!ast_strlen_zero(l->accountcode))
2910 ast_string_field_set(tmp, accountcode, l->accountcode);
2912 tmp->amaflags = l->amaflags;
2914 ast_module_ref(ast_module_info->self);
2915 tmp->callgroup = l->callgroup;
2916 tmp->pickupgroup = l->pickupgroup;
2917 ast_string_field_set(tmp, call_forward, l->call_forward);
2918 ast_copy_string(tmp->context, l->context, sizeof(tmp->context));
2919 ast_copy_string(tmp->exten, l->exten, sizeof(tmp->exten));
2921 /* Don't use ast_set_callerid() here because it will
2922 * generate a needless NewCallerID event */
2923 tmp->cid.cid_num = ast_strdup(l->cid_num);
2924 tmp->cid.cid_ani = ast_strdup(l->cid_num);
2925 tmp->cid.cid_name = ast_strdup(l->cid_name);
2928 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
2931 ast_jb_configure(tmp, &global_jbconf);
2933 if (state != AST_STATE_DOWN) {
2934 if (ast_pbx_start(tmp)) {
2935 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
2944 static int skinny_hold(struct skinny_subchannel *sub)
2946 struct skinny_line *l = sub->parent;
2947 struct skinny_device *d = l->parent;
2948 struct skinnysession *s = d->session;
2949 struct skinny_req *req;
2951 /* Channel needs to be put on hold */
2953 ast_verbose("Putting on Hold(%d)\n", l->instance);
2955 ast_queue_control_data(sub->owner, AST_CONTROL_HOLD,
2956 S_OR(l->mohsuggest, NULL),
2957 !ast_strlen_zero(l->mohsuggest) ? strlen(l->mohsuggest) + 1 : 0);
2959 if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
2962 req->data.activatecallplane.lineInstance = htolel(l->instance);
2963 transmit_response(s, req);
2965 if (!(req = req_alloc(sizeof(struct close_receive_channel_message), CLOSE_RECEIVE_CHANNEL_MESSAGE)))
2968 req->data.closereceivechannel.conferenceId = htolel(0);
2969 req->data.closereceivechannel.partyId = htolel(sub->callid);
2970 transmit_response(s, req);
2972 if (!(req = req_alloc(sizeof(struct stop_media_transmission_message), STOP_MEDIA_TRANSMISSION_MESSAGE)))
2975 req->data.stopmedia.conferenceId = htolel(0);
2976 req->data.stopmedia.passThruPartyId = htolel(sub->callid);
2977 transmit_response(s, req);
2979 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_WINK);
2984 static int skinny_unhold(struct skinny_subchannel *sub)
2986 struct skinny_line *l = sub->parent;
2987 struct skinny_device *d = l->parent;
2988 struct skinnysession *s = d->session;
2989 struct skinny_req *req;
2991 /* Channel is on hold, so we will unhold */
2993 ast_verbose("Taking off Hold(%d)\n", l->instance);
2995 ast_queue_control(sub->owner, AST_CONTROL_UNHOLD);
2997 if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
3000 req->data.activatecallplane.lineInstance = htolel(l->instance);
3001 transmit_response(s, req);
3003 transmit_connect(s, sub);
3004 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
3009 static int handle_keep_alive_message(struct skinny_req *req, struct skinnysession *s)
3011 if (!(req = req_alloc(0, KEEP_ALIVE_ACK_MESSAGE)))
3014 transmit_response(s, req);
3019 static int handle_register_message(struct skinny_req *req, struct skinnysession *s)
3024 memcpy(&name, req->data.reg.name, sizeof(name));
3026 res = skinny_register(req, s);
3028 ast_log(LOG_ERROR, "Rejecting Device %s: Device not found\n", name);
3029 if (!(req = req_alloc(sizeof(struct register_rej_message), REGISTER_REJ_MESSAGE)))
3032 snprintf(req->data.regrej.errMsg, sizeof(req->data.regrej.errMsg), "No Authority: %s", name);
3033 transmit_response(s, req);
3036 if (option_verbose > 2)
3037 ast_verbose(VERBOSE_PREFIX_3 "Device '%s' successfully registered\n", name);
3039 if (!(req = req_alloc(sizeof(struct register_ack_message), REGISTER_ACK_MESSAGE)))
3042 req->data.regack.res[0] = '0';
3043 req->data.regack.res[1] = '\0';
3044 req->data.regack.keepAlive = htolel(keep_alive);
3045 ast_copy_string(req->data.regack.dateTemplate, date_format, sizeof(req->data.regack.dateTemplate));
3046 req->data.regack.res2[0] = '0';
3047 req->data.regack.res2[1] = '\0';
3048 req->data.regack.secondaryKeepAlive = htolel(keep_alive);
3049 transmit_response(s, req);
3051 ast_verbose("Requesting capabilities\n");
3053 if (!(req = req_alloc(0, CAPABILITIES_REQ_MESSAGE)))
3056 transmit_response(s, req);
3061 static int handle_ip_port_message(struct skinny_req *req, struct skinnysession *s)
3063 /* no response necessary */
3067 static int handle_keypad_button_message(struct skinny_req *req, struct skinnysession *s)
3069 struct skinny_subchannel *sub = NULL;
3070 struct skinny_line *l;
3071 struct skinny_device *d = s->device;
3072 struct ast_frame f = { 0, };
3078 digit = letohl(req->data.keypad.button);
3079 lineInstance = letohl(req->data.keypad.lineInstance);
3080 callReference = letohl(req->data.keypad.callReference);
3084 } else if (digit == 15) {
3086 } else if (digit >= 0 && digit <= 9) {
3089 /* digit=10-13 (A,B,C,D ?), or
3090 * digit is bad value
3092 * probably should not end up here, but set
3093 * value for backward compatibility, and log
3097 ast_log(LOG_WARNING, "Unsupported digit %d\n", digit);
3104 if (lineInstance && callReference)
3105 sub = find_subchannel_by_instance_reference(d, lineInstance, callReference);
3107 sub = find_subchannel_by_instance_reference(d, d->lastlineinstance, d->lastcallreference);
3114 if (sub->owner->_state == 0) {
3115 f.frametype = AST_FRAME_DTMF_BEGIN;
3116 ast_queue_frame(sub->owner, &f);
3118 /* XXX MUST queue this frame to all lines in threeway call if threeway call is active */
3119 f.frametype = AST_FRAME_DTMF_END;
3120 ast_queue_frame(sub->owner, &f);
3121 /* XXX This seriously needs to be fixed */
3122 if (sub->next && sub->next->owner) {
3123 if (sub->owner->_state == 0) {
3124 f.frametype = AST_FRAME_DTMF_BEGIN;
3125 ast_queue_frame(sub->next->owner, &f);
3127 f.frametype = AST_FRAME_DTMF_END;
3128 ast_queue_frame(sub->next->owner, &f);
3132 ast_verbose("No owner: %s\n", l->name);
3137 static int handle_stimulus_message(struct skinny_req *req, struct skinnysession *s)
3139 struct skinny_device *d = s->device;
3140 struct skinny_line *l;
3141 struct skinny_subchannel *sub;
3142 /*struct skinny_speeddial *sd;*/
3143 struct ast_channel *c;
3150 event = letohl(req->data.stimulus.stimulus);
3151 instance = letohl(req->data.stimulus.stimulusInstance);
3152 unknown1 = letohl(req->data.stimulus.unknown1); /* No clue.. */
3154 ast_verbose("unknown1 in handle_stimulus_message is '%d'\n", unknown1);
3156 sub = find_subchannel_by_instance_reference(d, d->lastlineinstance, d->lastcallreference);
3159 l = find_line_by_instance(d, d->lastlineinstance);
3168 case STIMULUS_REDIAL:
3170 ast_verbose("Received Stimulus: Redial(%d)\n", instance);
3172 c = skinny_new(l, AST_STATE_DOWN);
3174 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
3178 if (l->hookstate == SKINNY_ONHOOK) {
3179 l->hookstate = SKINNY_OFFHOOK;
3180 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
3183 ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
3184 transmit_displaymessage(s, NULL); /* clear display */
3185 transmit_tone(s, SKINNY_DIALTONE);
3187 if (ast_strlen_zero(l->lastnumberdialed)) {
3188 ast_log(LOG_WARNING, "Attempted redial, but no previously dialed number found.\n");
3191 if (!ast_ignore_pattern(c->context, l->lastnumberdialed)) {
3192 transmit_tone(s, SKINNY_SILENCE);
3194 ast_copy_string(c->exten, l->lastnumberdialed, sizeof(c->exten));
3195 if (ast_pthread_create(&t, NULL, skinny_newcall, c)) {
3196 ast_log(LOG_WARNING, "Unable to create new call thread: %s\n", strerror(errno));
3201 case STIMULUS_SPEEDDIAL:
3203 ast_verbose("Received Stimulus: SpeedDial(%d)\n", instance);
3205 struct skinny_speeddial *sd;
3206 if (!(sd = find_speeddial_by_instance(d, instance, 0))) {
3210 if (!sub || !sub->owner)
3211 c = skinny_new(l, AST_STATE_DOWN);
3216 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
3220 if (l->hookstate == SKINNY_ONHOOK) {
3221 l->hookstate = SKINNY_OFFHOOK;
3222 transmit_speaker_mode(s, SKINNY_SPEAKERON);
3223 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
3226 ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
3227 transmit_displaymessage(s, NULL); /* clear display */
3228 transmit_tone(s, SKINNY_DIALTONE);
3230 if (!ast_ignore_pattern(c->context, sd->exten)) {
3231 transmit_tone(s, SKINNY_SILENCE);
3233 if (ast_exists_extension(c, c->context, sd->exten, 1, l->cid_num)) {
3234 ast_copy_string(c->exten, sd->exten, sizeof(c->exten));
3235 ast_copy_string(l->lastnumberdialed, sd->exten, sizeof(l->lastnumberdialed));
3237 if (ast_pthread_create(&t, NULL, skinny_newcall, c)) {
3238 ast_log(LOG_WARNING, "Unable to create new call thread: %s\n", strerror(errno));
3247 ast_verbose("Received Stimulus: Hold(%d)\n", instance);
3258 case STIMULUS_TRANSFER:
3260 ast_verbose("Received Stimulus: Transfer(%d)\n", instance);
3261 transmit_tone(s, SKINNY_DIALTONE);
3262 /* XXX figure out how to transfer */
3264 case STIMULUS_CONFERENCE:
3266 ast_verbose("Received Stimulus: Conference(%d)\n", instance);
3267 transmit_tone(s, SKINNY_DIALTONE);
3268 /* XXX determine the best way to pull off a conference. Meetme? */
3270 case STIMULUS_VOICEMAIL:
3272 ast_verbose("Received Stimulus: Voicemail(%d)\n", instance);
3274 if (!sub || !sub->owner) {
3275 c = skinny_new(l, AST_STATE_DOWN);
3280 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
3285 if (ast_strlen_zero(l->vmexten)) /* Exit the call if no VM pilot */
3288 if (l->hookstate == SKINNY_ONHOOK){
3289 l->hookstate = SKINNY_OFFHOOK;
3290 transmit_speaker_mode(s, SKINNY_SPEAKERON);
3291 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
3295 ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
3297 transmit_displaymessage(s, NULL); /* clear display */
3298 transmit_tone(s, SKINNY_DIALTONE);
3300 if (!ast_ignore_pattern(c->context, vmexten)) {
3301 transmit_tone(s, SKINNY_SILENCE);
3304 if (ast_exists_extension(c, c->context, l->vmexten, 1, l->cid_num)) {
3305 ast_copy_string(c->exten, l->vmexten, sizeof(c->exten));
3306 ast_copy_string(l->lastnumberdialed, l->vmexten, sizeof(l->lastnumberdialed));
3307 if (ast_pthread_create(&t, NULL, skinny_newcall, c)) {
3308 ast_log(LOG_WARNING, "Unable to create new call thread: %s\n", strerror(errno));
3315 case STIMULUS_CALLPARK:
3317 ast_verbose("Received Stimulus: Park Call(%d)\n", instance);
3318 /* XXX Park the call */
3320 case STIMULUS_FORWARDALL:
3322 ast_verbose("Received Stimulus: Forward All(%d)\n", instance);
3323 /* Why is DND under FORWARDALL? */
3324 /* Because it's the same thing. */
3326 /* Do not disturb */
3327 transmit_tone(s, SKINNY_DIALTONE);
3329 if (option_verbose > 2)
3330 ast_verbose(VERBOSE_PREFIX_3 "Disabling DND on %s@%s\n", l->name, d->name);
3332 transmit_lamp_indication(s, STIMULUS_FORWARDALL, 1, SKINNY_LAMP_ON);
3333 transmit_displaynotify(s, "DnD disabled", 10);
3335 if (option_verbose > 2)
3336 ast_verbose(VERBOSE_PREFIX_3 "Enabling DND on %s@%s\n", l->name, d->name);
3338 transmit_lamp_indication(s, STIMULUS_FORWARDALL, 1, SKINNY_LAMP_OFF);
3339 transmit_displaynotify(s, "DnD enabled", 10);
3342 case STIMULUS_FORWARDBUSY:
3344 ast_verbose("Received Stimulus: Forward Busy (%d)\n", instance);
3346 case STIMULUS_FORWARDNOANSWER:
3348 ast_verbose("Received Stimulus: Forward No Answer (%d)\n", instance);
3350 case STIMULUS_DISPLAY:
3351 /* Not sure what this is */
3353 ast_verbose("Received Stimulus: Display(%d)\n", instance);
3357 ast_verbose("Received Stimulus: Line(%d)\n", instance);
3359 l = find_line_by_instance(d, instance);
3365 /* turn the speaker on */
3366 transmit_speaker_mode(s, SKINNY_SPEAKERON);
3367 transmit_ringer_mode(s, SKINNY_RING_OFF);
3368 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
3370 l->hookstate = SKINNY_OFFHOOK;
3372 ast_device_state_changed("Skinny/%s@%s", l->name, d->name);
3374 if (sub && sub->outgoing) {
3375 /* We're answering a ringing call */
3376 ast_queue_control(sub->owner, AST_CONTROL_ANSWER);
3377 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
3378 transmit_tone(s, SKINNY_SILENCE);
3379 transmit_callstate(s, l->instance, SKINNY_CONNECTED, sub->callid);
3380 transmit_displaypromptstatus(s, "Connected", 0, l->instance, sub->callid);
3381 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_CONNECTED);
3383 ast_setstate(sub->owner, AST_STATE_UP);
3385 if (sub && sub->owner) {
3386 ast_debug(1, "Current subchannel [%s] already has owner\n", sub->owner->name);
3388 c = skinny_new(l, AST_STATE_DOWN);
3391 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
3393 ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
3394 transmit_displaymessage(s, NULL); /* clear display */
3395 transmit_tone(s, SKINNY_DIALTONE);
3396 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_OFFHOOK);
3398 /* start the switch thread */
3399 if (ast_pthread_create(&t, NULL, skinny_ss, c)) {
3400 ast_log(LOG_WARNING, "Unable to create switch thread: %s\n", strerror(errno));
3404 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
3411 ast_verbose("RECEIVED UNKNOWN STIMULUS: %d(%d)\n", event, instance);
3414 ast_device_state_changed("Skinny/%s@%s", l->name, d->name);
3419 static int handle_offhook_message(struct skinny_req *req, struct skinnysession *s)
3421 struct skinny_device *d = s->device;
3422 struct skinny_line *l;
3423 struct skinny_subchannel *sub;
3424 struct ast_channel *c;
3429 unknown1 = letohl(req->data.offhook.unknown1);
3430 unknown2 = letohl(req->data.offhook.unknown2);
3432 sub = find_subchannel_by_instance_reference(d, d->lastlineinstance, d->lastcallreference);
3435 l = find_line_by_instance(d, d->lastlineinstance);
3443 transmit_ringer_mode(s, SKINNY_RING_OFF);
3444 l->hookstate = SKINNY_OFFHOOK;
3446 ast_device_state_changed("Skinny/%s@%s", l->name, d->name);
3448 if (sub && sub->onhold) {
3452 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
3454 if (sub && sub->outgoing) {
3455 /* We're answering a ringing call */
3456 ast_queue_control(sub->owner, AST_CONTROL_ANSWER);
3457 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
3458 transmit_tone(s, SKINNY_SILENCE);
3459 transmit_callstate(s, l->instance, SKINNY_CONNECTED, sub->callid);
3460 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_CONNECTED);
3462 ast_setstate(sub->owner, AST_STATE_UP);
3464 if (sub && sub->owner) {
3465 ast_debug(1, "Current sub [%s] already has owner\n", sub->owner->name);
3467 c = skinny_new(l, AST_STATE_DOWN);
3470 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
3472 ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
3473 transmit_displaymessage(s, NULL); /* clear display */
3474 transmit_tone(s, SKINNY_DIALTONE);
3475 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_OFFHOOK);
3477 /* start the switch thread */
3478 if (ast_pthread_create(&t, NULL, skinny_ss, c)) {
3479 ast_log(LOG_WARNING, "Unable to create switch thread: %s\n", strerror(errno));
3483 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
3490 static int handle_onhook_message(struct skinny_req *req, struct skinnysession *s)
3492 struct skinny_device *d = s->device;
3493 struct skinny_line *l;
3494 struct skinny_subchannel *sub;
3498 unknown1 = letohl(req->data.onhook.unknown1);
3499 unknown2 = letohl(req->data.onhook.unknown2);
3501 sub = find_subchannel_by_instance_reference(d, d->lastlineinstance, d->lastcallreference);
3508 if (l->hookstate == SKINNY_ONHOOK) {
3509 /* Something else already put us back on hook */
3512 l->hookstate = SKINNY_ONHOOK;
3514 ast_device_state_changed("Skinny/%s@%s", l->name, d->name);
3520 sub->cxmode = SKINNY_CX_RECVONLY;
3521 transmit_callstate(s, l->instance, l->hookstate, sub->callid);
3523 ast_verbose("Skinny %s@%s went on hook\n", l->name, d->name);
3524 if (l->transfer && (sub->owner && sub->next && sub->next->owner) && ((!sub->outgoing) || (sub->next && !sub->next->outgoing))) {
3525 /* We're allowed to transfer, we have two active calls and
3526 we made at least one of the calls. Let's try and transfer */
3529 if ((res = attempt_transfer(p)) < 0) {
3530 if (sub->next && sub->next->owner) {
3531 sub->next->alreadygone = 1;
3532 ast_queue_hangup(sub->next->owner,1);
3535 ast_log(LOG_WARNING, "Transfer attempt failed\n");
3540 /* Hangup the current call */
3541 /* If there is another active call, skinny_hangup will ring the phone with the other call */
3543 sub->alreadygone = 1;
3544 ast_queue_hangup(sub->owner);
3546 ast_log(LOG_WARNING, "Skinny(%s@%s-%d) channel already destroyed\n",
3547 l->name, d->name, sub->callid);
3550 if ((l->hookstate == SKINNY_ONHOOK) && (sub->next && !sub->next->rtp)) {
3556 static int handle_capabilities_res_message(struct skinny_req *req, struct skinnysession *s)
3558 struct skinny_device *d = s->device;
3559 struct skinny_line *l;
3564 count = letohl(req->data.caps.count);
3566 for (i = 0; i < count; i++) {
3569 scodec = letohl(req->data.caps.caps[i].codec);
3570 acodec = codec_skinny2ast(scodec);
3572 ast_verbose("Adding codec capability '%d (%d)'\n", acodec, scodec);
3576 d->capability &= codecs;
3577 ast_verbose("Device capability set to '%d'\n", d->capability);
3578 for (l = d->lines; l; l = l->next) {
3579 ast_mutex_lock(&l->lock);
3580 l->capability = d->capability;
3581 ast_mutex_unlock(&l->lock);
3587 static int handle_speed_dial_stat_req_message(struct skinny_req *req, struct skinnysession *s)
3589 struct skinny_device *d = s->device;
3590 struct skinny_speeddial *sd;
3593 instance = letohl(req->data.speeddialreq.speedDialNumber);
3595 sd = find_speeddial_by_instance(d, instance, 0);
3601 if (!(req = req_alloc(sizeof(struct speed_dial_stat_res_message), SPEED_DIAL_STAT_RES_MESSAGE)))
3604 req->data.speeddialreq.speedDialNumber = htolel(instance);
3605 snprintf(req->data.speeddial.speedDialDirNumber, sizeof(req->data.speeddial.speedDialDirNumber), sd->exten);
3606 snprintf(req->data.speeddial.speedDialDisplayName, sizeof(req->data.speeddial.speedDialDisplayName), sd->label);
3608 transmit_response(s, req);
3612 static int handle_line_state_req_message(struct skinny_req *req, struct skinnysession *s)
3614 struct skinny_device *d = s->device;
3615 struct skinny_line *l;
3616 struct skinny_speeddial *sd = NULL;
3619 instance = letohl(req->data.line.lineNumber);
3621 ast_mutex_lock(&devicelock);
3623 l = find_line_by_instance(d, instance);
3626 sd = find_speeddial_by_instance(d, instance, 1);
3633 ast_mutex_unlock(&devicelock);
3635 if (!(req = req_alloc(sizeof(struct line_stat_res_message), LINE_STAT_RES_MESSAGE)))
3638 req->data.linestat.lineNumber = letohl(instance);
3640 memcpy(req->data.linestat.lineDirNumber, sd->label, sizeof(req->data.linestat.lineDirNumber));
3641 memcpy(req->data.linestat.lineDisplayName, sd->label, sizeof(req->data.linestat.lineDisplayName));
3643 memcpy(req->data.linestat.lineDirNumber, l->name, sizeof(req->data.linestat.lineDirNumber));
3644 memcpy(req->data.linestat.lineDisplayName, l->label, sizeof(req->data.linestat.lineDisplayName));
3646 transmit_response(s,req);
3650 static int handle_time_date_req_message(struct skinny_req *req, struct skinnysession *s)
3655 if (!(req = req_alloc(sizeof(struct definetimedate_message), DEFINETIMEDATE_MESSAGE)))