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 regcontext[AST_MAX_CONTEXT]; /* Context for auto-extension */
104 static char date_format[6] = "D-M-Y";
105 static char version_id[16] = "P002F202";
107 #if __BYTE_ORDER == __LITTLE_ENDIAN
108 #define letohl(x) (x)
109 #define letohs(x) (x)
110 #define htolel(x) (x)
111 #define htoles(x) (x)
113 #if defined(SOLARIS) || defined(__Darwin__) || defined(__NetBSD__)
114 #define __bswap_16(x) \
115 ((((x) & 0xff00) >> 8) | \
116 (((x) & 0x00ff) << 8))
117 #define __bswap_32(x) \
118 ((((x) & 0xff000000) >> 24) | \
119 (((x) & 0x00ff0000) >> 8) | \
120 (((x) & 0x0000ff00) << 8) | \
121 (((x) & 0x000000ff) << 24))
123 #include <bits/byteswap.h>
125 #define letohl(x) __bswap_32(x)
126 #define letohs(x) __bswap_16(x)
127 #define htolel(x) __bswap_32(x)
128 #define htoles(x) __bswap_16(x)
131 /*! Global jitterbuffer configuration - by default, jb is disabled */
132 static struct ast_jb_conf default_jbconf =
136 .resync_threshold = -1,
139 static struct ast_jb_conf global_jbconf;
141 AST_THREADSTORAGE(device2str_threadbuf);
142 #define DEVICE2STR_BUFSIZE 15
144 AST_THREADSTORAGE(control2str_threadbuf);
145 #define CONTROL2STR_BUFSIZE 100
147 /*********************
148 * Protocol Messages *
149 *********************/
151 #define KEEP_ALIVE_MESSAGE 0x0000
152 /* no additional struct */
154 #define REGISTER_MESSAGE 0x0001
155 struct register_message {
164 #define IP_PORT_MESSAGE 0x0002
166 #define KEYPAD_BUTTON_MESSAGE 0x0003
167 struct keypad_button_message {
169 uint32_t lineInstance;
170 uint32_t callReference;
173 #define STIMULUS_MESSAGE 0x0005
174 struct stimulus_message {
176 uint32_t stimulusInstance;
180 #define OFFHOOK_MESSAGE 0x0006
181 struct offhook_message {
186 #define ONHOOK_MESSAGE 0x0007
187 struct onhook_message {
192 #define CAPABILITIES_RES_MESSAGE 0x0010
193 struct station_capabilities {
202 struct capabilities_res_message {
204 struct station_capabilities caps[18];
207 #define SPEED_DIAL_STAT_REQ_MESSAGE 0x000A
208 struct speed_dial_stat_req_message {
209 uint32_t speedDialNumber;
212 #define LINE_STATE_REQ_MESSAGE 0x000B
213 struct line_state_req_message {
217 #define TIME_DATE_REQ_MESSAGE 0x000D
218 #define BUTTON_TEMPLATE_REQ_MESSAGE 0x000E
219 #define VERSION_REQ_MESSAGE 0x000F
220 #define SERVER_REQUEST_MESSAGE 0x0012
222 #define ALARM_MESSAGE 0x0020
223 struct alarm_message {
224 uint32_t alarmSeverity;
225 char displayMessage[80];
226 uint32_t alarmParam1;
227 uint32_t alarmParam2;
230 #define OPEN_RECEIVE_CHANNEL_ACK_MESSAGE 0x0022
231 struct open_receive_channel_ack_message {
238 #define SOFT_KEY_SET_REQ_MESSAGE 0x0025
240 #define SOFT_KEY_EVENT_MESSAGE 0x0026
241 struct soft_key_event_message {
242 uint32_t softKeyEvent;
247 #define UNREGISTER_MESSAGE 0x0027
248 #define SOFT_KEY_TEMPLATE_REQ_MESSAGE 0x0028
249 #define HEADSET_STATUS_MESSAGE 0x002B
250 #define REGISTER_AVAILABLE_LINES_MESSAGE 0x002D
252 #define REGISTER_ACK_MESSAGE 0x0081
253 struct register_ack_message {
255 char dateTemplate[6];
257 uint32_t secondaryKeepAlive;
261 #define START_TONE_MESSAGE 0x0082
262 struct start_tone_message {
267 #define STOP_TONE_MESSAGE 0x0083
268 struct stop_tone_message {
272 #define SET_RINGER_MESSAGE 0x0085
273 struct set_ringer_message {
275 uint32_t unknown1; /* See notes in transmit_ringer_mode */
280 #define SET_LAMP_MESSAGE 0x0086
281 struct set_lamp_message {
283 uint32_t stimulusInstance;
284 uint32_t deviceStimulus;
287 #define SET_SPEAKER_MESSAGE 0x0088
288 struct set_speaker_message {
292 /* XXX When do we need to use this? */
293 #define SET_MICROPHONE_MESSAGE 0x0089
294 struct set_microphone_message {
298 #define START_MEDIA_TRANSMISSION_MESSAGE 0x008A
299 struct media_qualifier {
306 struct start_media_transmission_message {
307 uint32_t conferenceId;
308 uint32_t passThruPartyId;
312 uint32_t payloadType;
313 struct media_qualifier qualifier;
317 #define STOP_MEDIA_TRANSMISSION_MESSAGE 0x008B
318 struct stop_media_transmission_message {
319 uint32_t conferenceId;
320 uint32_t passThruPartyId;
324 #define CALL_INFO_MESSAGE 0x008F
325 struct call_info_message {
326 char callingPartyName[40];
327 char callingParty[24];
328 char calledPartyName[40];
329 char calledParty[24];
333 char originalCalledPartyName[40];
334 char originalCalledParty[24];
335 char lastRedirectingPartyName[40];
336 char lastRedirectingParty[24];
337 uint32_t originalCalledPartyRedirectReason;
338 uint32_t lastRedirectingReason;
339 char callingPartyVoiceMailbox[24];
340 char calledPartyVoiceMailbox[24];
341 char originalCalledPartyVoiceMailbox[24];
342 char lastRedirectingVoiceMailbox[24];
346 #define SPEED_DIAL_STAT_RES_MESSAGE 0x0091
347 struct speed_dial_stat_res_message {
348 uint32_t speedDialNumber;
349 char speedDialDirNumber[24];
350 char speedDialDisplayName[40];
353 #define LINE_STAT_RES_MESSAGE 0x0092
354 struct line_stat_res_message {
356 char lineDirNumber[24];
357 char lineDisplayName[24];
361 #define DEFINETIMEDATE_MESSAGE 0x0094
362 struct definetimedate_message {
363 uint32_t year; /* since 1900 */
365 uint32_t dayofweek; /* monday = 1 */
370 uint32_t milliseconds;
374 #define BUTTON_TEMPLATE_RES_MESSAGE 0x0097
375 struct button_definition {
376 uint8_t instanceNumber;
377 uint8_t buttonDefinition;
380 struct button_definition_template {
381 uint8_t buttonDefinition;
382 /* for now, anything between 0xB0 and 0xCF is custom */
386 #define STIMULUS_REDIAL 0x01
387 #define STIMULUS_SPEEDDIAL 0x02
388 #define STIMULUS_HOLD 0x03
389 #define STIMULUS_TRANSFER 0x04
390 #define STIMULUS_FORWARDALL 0x05
391 #define STIMULUS_FORWARDBUSY 0x06
392 #define STIMULUS_FORWARDNOANSWER 0x07
393 #define STIMULUS_DISPLAY 0x08
394 #define STIMULUS_LINE 0x09
395 #define STIMULUS_VOICEMAIL 0x0F
396 #define STIMULUS_AUTOANSWER 0x11
397 #define STIMULUS_CONFERENCE 0x7D
398 #define STIMULUS_CALLPARK 0x7E
399 #define STIMULUS_CALLPICKUP 0x7F
400 #define STIMULUS_NONE 0xFF
403 #define BT_REDIAL STIMULUS_REDIAL
404 #define BT_SPEEDDIAL STIMULUS_SPEEDDIAL
405 #define BT_HOLD STIMULUS_HOLD
406 #define BT_TRANSFER STIMULUS_TRANSFER
407 #define BT_FORWARDALL STIMULUS_FORWARDALL
408 #define BT_FORWARDBUSY STIMULUS_FORWARDBUSY
409 #define BT_FORWARDNOANSWER STIMULUS_FORWARDNOANSWER
410 #define BT_DISPLAY STIMULUS_DISPLAY
411 #define BT_LINE STIMULUS_LINE
412 #define BT_VOICEMAIL STIMULUS_VOICEMAIL
413 #define BT_AUTOANSWER STIMULUS_AUTOANSWER
414 #define BT_CONFERENCE STIMULUS_CONFERENCE
415 #define BT_CALLPARK STIMULUS_CALLPARK
416 #define BT_CALLPICKUP STIMULUS_CALLPICKUP
419 /* Custom button types - add our own between 0xB0 and 0xCF.
420 This may need to be revised in the future,
421 if stimuluses are ever added in this range. */
422 #define BT_CUST_LINESPEEDDIAL 0xB0 /* line or speeddial with/without hint */
423 #define BT_CUST_LINE 0xB1 /* line or speeddial with hint only */
425 struct button_template_res_message {
426 uint32_t buttonOffset;
427 uint32_t buttonCount;
428 uint32_t totalButtonCount;
429 struct button_definition definition[42];
432 #define VERSION_RES_MESSAGE 0x0098
433 struct version_res_message {
437 #define DISPLAYTEXT_MESSAGE 0x0099
438 struct displaytext_message {
442 #define CLEAR_NOTIFY_MESSAGE 0x0115
443 #define CLEAR_DISPLAY_MESSAGE 0x009A
445 #define CAPABILITIES_REQ_MESSAGE 0x009B
447 #define REGISTER_REJ_MESSAGE 0x009D
448 struct register_rej_message {
452 #define SERVER_RES_MESSAGE 0x009E
453 struct server_identifier {
457 struct server_res_message {
458 struct server_identifier server[5];
459 uint32_t serverListenPort[5];
460 uint32_t serverIpAddr[5];
463 #define RESET_MESSAGE 0x009F
464 struct reset_message {
468 #define KEEP_ALIVE_ACK_MESSAGE 0x0100
470 #define OPEN_RECEIVE_CHANNEL_MESSAGE 0x0105
471 struct open_receive_channel_message {
472 uint32_t conferenceId;
481 #define CLOSE_RECEIVE_CHANNEL_MESSAGE 0x0106
482 struct close_receive_channel_message {
483 uint32_t conferenceId;
488 #define SOFT_KEY_TEMPLATE_RES_MESSAGE 0x0108
490 struct soft_key_template_definition {
491 char softKeyLabel[16];
492 uint32_t softKeyEvent;
495 #define KEYDEF_ONHOOK 0
496 #define KEYDEF_CONNECTED 1
497 #define KEYDEF_ONHOLD 2
498 #define KEYDEF_RINGIN 3
499 #define KEYDEF_OFFHOOK 4
500 #define KEYDEF_CONNWITHTRANS 5
501 #define KEYDEF_DADFD 6 /* Digits After Dialing First Digit */
502 #define KEYDEF_CONNWITHCONF 7
503 #define KEYDEF_RINGOUT 8
504 #define KEYDEF_OFFHOOKWITHFEAT 9
505 #define KEYDEF_UNKNOWN 10
507 #define SOFTKEY_NONE 0x00
508 #define SOFTKEY_REDIAL 0x01
509 #define SOFTKEY_NEWCALL 0x02
510 #define SOFTKEY_HOLD 0x03
511 #define SOFTKEY_TRNSFER 0x04
512 #define SOFTKEY_CFWDALL 0x05
513 #define SOFTKEY_CFWDBUSY 0x06
514 #define SOFTKEY_CFWDNOANSWER 0x07
515 #define SOFTKEY_BKSPC 0x08
516 #define SOFTKEY_ENDCALL 0x09
517 #define SOFTKEY_RESUME 0x0A
518 #define SOFTKEY_ANSWER 0x0B
519 #define SOFTKEY_INFO 0x0C
520 #define SOFTKEY_CONFRN 0x0D
521 #define SOFTKEY_PARK 0x0E
522 #define SOFTKEY_JOIN 0x0F
523 #define SOFTKEY_MEETME 0x10
524 #define SOFTKEY_PICKUP 0x11
525 #define SOFTKEY_GPICKUP 0x12
527 struct soft_key_template_definition soft_key_template_default[] = {
533 { "CFwdBusy", 0x06 },
534 { "CFwdNoAnswer", 0x07 },
548 struct soft_key_definitions {
550 const uint8_t *defaults;
554 static const uint8_t soft_key_default_onhook[] = {
562 static const uint8_t soft_key_default_connected[] = {
571 static const uint8_t soft_key_default_onhold[] = {
578 static const uint8_t soft_key_default_ringin[] = {
584 static const uint8_t soft_key_default_offhook[] = {
592 static const uint8_t soft_key_default_connwithtrans[] = {
601 static const uint8_t soft_key_default_dadfd[] = {
606 static const uint8_t soft_key_default_connwithconf[] = {
610 static const uint8_t soft_key_default_ringout[] = {
617 static const uint8_t soft_key_default_offhookwithfeat[] = {
622 static const uint8_t soft_key_default_unknown[] = {
626 static const struct soft_key_definitions soft_key_default_definitions[] = {
627 {KEYDEF_ONHOOK, soft_key_default_onhook, sizeof(soft_key_default_onhook) / sizeof(uint8_t)},
628 {KEYDEF_CONNECTED, soft_key_default_connected, sizeof(soft_key_default_connected) / sizeof(uint8_t)},
629 {KEYDEF_ONHOLD, soft_key_default_onhold, sizeof(soft_key_default_onhold) / sizeof(uint8_t)},
630 {KEYDEF_RINGIN, soft_key_default_ringin, sizeof(soft_key_default_ringin) / sizeof(uint8_t)},
631 {KEYDEF_OFFHOOK, soft_key_default_offhook, sizeof(soft_key_default_offhook) / sizeof(uint8_t)},
632 {KEYDEF_CONNWITHTRANS, soft_key_default_connwithtrans, sizeof(soft_key_default_connwithtrans) / sizeof(uint8_t)},
633 {KEYDEF_DADFD, soft_key_default_dadfd, sizeof(soft_key_default_dadfd) / sizeof(uint8_t)},
634 {KEYDEF_CONNWITHCONF, soft_key_default_connwithconf, sizeof(soft_key_default_connwithconf) / sizeof(uint8_t)},
635 {KEYDEF_RINGOUT, soft_key_default_ringout, sizeof(soft_key_default_ringout) / sizeof(uint8_t)},
636 {KEYDEF_OFFHOOKWITHFEAT, soft_key_default_offhookwithfeat, sizeof(soft_key_default_offhookwithfeat) / sizeof(uint8_t)},
637 {KEYDEF_UNKNOWN, soft_key_default_unknown, sizeof(soft_key_default_unknown) / sizeof(uint8_t)}
640 struct soft_key_template_res_message {
641 uint32_t softKeyOffset;
642 uint32_t softKeyCount;
643 uint32_t totalSoftKeyCount;
644 struct soft_key_template_definition softKeyTemplateDefinition[32];
647 #define SOFT_KEY_SET_RES_MESSAGE 0x0109
649 struct soft_key_set_definition {
650 uint8_t softKeyTemplateIndex[16];
651 uint16_t softKeyInfoIndex[16];
654 struct soft_key_set_res_message {
655 uint32_t softKeySetOffset;
656 uint32_t softKeySetCount;
657 uint32_t totalSoftKeySetCount;
658 struct soft_key_set_definition softKeySetDefinition[16];
662 #define SELECT_SOFT_KEYS_MESSAGE 0x0110
663 struct select_soft_keys_message {
666 uint32_t softKeySetIndex;
667 uint32_t validKeyMask;
670 #define CALL_STATE_MESSAGE 0x0111
671 struct call_state_message {
673 uint32_t lineInstance;
674 uint32_t callReference;
678 #define DISPLAY_PROMPT_STATUS_MESSAGE 0x0112
679 struct display_prompt_status_message {
680 uint32_t messageTimeout;
681 char promptMessage[32];
682 uint32_t lineInstance;
683 uint32_t callReference;
686 #define CLEAR_PROMPT_MESSAGE 0x0113
687 struct clear_prompt_message {
688 uint32_t lineInstance;
689 uint32_t callReference;
692 #define DISPLAY_NOTIFY_MESSAGE 0x0114
693 struct display_notify_message {
694 uint32_t displayTimeout;
695 char displayMessage[100];
698 #define ACTIVATE_CALL_PLANE_MESSAGE 0x0116
699 struct activate_call_plane_message {
700 uint32_t lineInstance;
703 #define DIALED_NUMBER_MESSAGE 0x011D
704 struct dialed_number_message {
705 char dialedNumber[24];
706 uint32_t lineInstance;
707 uint32_t callReference;
711 struct alarm_message alarm;
712 struct speed_dial_stat_req_message speeddialreq;
713 struct register_message reg;
714 struct register_ack_message regack;
715 struct register_rej_message regrej;
716 struct capabilities_res_message caps;
717 struct version_res_message version;
718 struct button_template_res_message buttontemplate;
719 struct displaytext_message displaytext;
720 struct display_prompt_status_message displaypromptstatus;
721 struct clear_prompt_message clearpromptstatus;
722 struct definetimedate_message definetimedate;
723 struct start_tone_message starttone;
724 struct speed_dial_stat_res_message speeddial;
725 struct line_state_req_message line;
726 struct line_stat_res_message linestat;
727 struct soft_key_set_res_message softkeysets;
728 struct soft_key_template_res_message softkeytemplate;
729 struct server_res_message serverres;
730 struct reset_message reset;
731 struct set_lamp_message setlamp;
732 struct set_ringer_message setringer;
733 struct call_state_message callstate;
734 struct keypad_button_message keypad;
735 struct select_soft_keys_message selectsoftkey;
736 struct activate_call_plane_message activatecallplane;
737 struct stimulus_message stimulus;
738 struct offhook_message offhook;
739 struct onhook_message onhook;
740 struct set_speaker_message setspeaker;
741 struct set_microphone_message setmicrophone;
742 struct call_info_message callinfo;
743 struct start_media_transmission_message startmedia;
744 struct stop_media_transmission_message stopmedia;
745 struct open_receive_channel_message openreceivechannel;
746 struct open_receive_channel_ack_message openreceivechannelack;
747 struct close_receive_channel_message closereceivechannel;
748 struct display_notify_message displaynotify;
749 struct dialed_number_message dialednumber;
750 struct soft_key_event_message softkeyeventmessage;
753 /* packet composition */
758 union skinny_data data;
761 /* XXX This is the combined size of the variables above. (len, res, e)
762 If more are added, this MUST change.
763 (sizeof(skinny_req) - sizeof(skinny_data)) DOES NOT WORK on all systems (amd64?). */
764 int skinny_header_size = 12;
766 /*****************************
767 * Asterisk specific globals *
768 *****************************/
770 static int skinnydebug = 0;
772 /* a hostname, portnumber, socket and such is usefull for VoIP protocols */
773 static struct sockaddr_in bindaddr;
774 static char ourhost[256];
776 static struct in_addr __ourip;
777 struct ast_hostent ahp;
779 static int skinnysock = -1;
780 static pthread_t accept_t;
781 static char context[AST_MAX_CONTEXT] = "default";
782 static char language[MAX_LANGUAGE] = "";
783 static char mohinterpret[MAX_MUSICCLASS] = "default";
784 static char mohsuggest[MAX_MUSICCLASS] = "";
785 static char cid_num[AST_MAX_EXTENSION] = "";
786 static char cid_name[AST_MAX_EXTENSION] = "";
787 static char linelabel[AST_MAX_EXTENSION] ="";
789 static ast_group_t cur_callergroup = 0;
790 static ast_group_t cur_pickupgroup = 0;
791 static int immediate = 0;
792 static int callwaiting = 0;
793 static int callreturn = 0;
794 static int threewaycalling = 0;
795 static int mwiblink = 0;
796 /* This is for flashhook transfers */
797 static int transfer = 0;
798 static int cancallforward = 0;
799 /* static int busycount = 3;*/
800 static char accountcode[AST_MAX_ACCOUNT_CODE] = "";
801 static char mailbox[AST_MAX_EXTENSION];
802 static char regexten[AST_MAX_EXTENSION];
803 static int amaflags = 0;
804 static int callnums = 1;
806 #define SKINNY_DEVICE_UNKNOWN -1
807 #define SKINNY_DEVICE_NONE 0
808 #define SKINNY_DEVICE_30SPPLUS 1
809 #define SKINNY_DEVICE_12SPPLUS 2
810 #define SKINNY_DEVICE_12SP 3
811 #define SKINNY_DEVICE_12 4
812 #define SKINNY_DEVICE_30VIP 5
813 #define SKINNY_DEVICE_7910 6
814 #define SKINNY_DEVICE_7960 7
815 #define SKINNY_DEVICE_7940 8
816 #define SKINNY_DEVICE_7935 9
817 #define SKINNY_DEVICE_ATA186 12 /* Cisco ATA-186 */
818 #define SKINNY_DEVICE_7941 115
819 #define SKINNY_DEVICE_7971 119
820 #define SKINNY_DEVICE_7985 302
821 #define SKINNY_DEVICE_7911 307
822 #define SKINNY_DEVICE_7961GE 308
823 #define SKINNY_DEVICE_7941GE 309
824 #define SKINNY_DEVICE_7905 20000
825 #define SKINNY_DEVICE_7920 30002
826 #define SKINNY_DEVICE_7970 30006
827 #define SKINNY_DEVICE_7912 30007
828 #define SKINNY_DEVICE_7902 30008
829 #define SKINNY_DEVICE_CIPC 30016 /* Cisco IP Communicator */
830 #define SKINNY_DEVICE_7961 30018
831 #define SKINNY_DEVICE_7936 30019
832 #define SKINNY_DEVICE_SCCPGATEWAY_AN 30027 /* ??? */
833 #define SKINNY_DEVICE_SCCPGATEWAY_BRI 30028 /* ??? */
835 #define SKINNY_SPEAKERON 1
836 #define SKINNY_SPEAKEROFF 2
838 #define SKINNY_MICON 1
839 #define SKINNY_MICOFF 2
841 #define SKINNY_OFFHOOK 1
842 #define SKINNY_ONHOOK 2
843 #define SKINNY_RINGOUT 3
844 #define SKINNY_RINGIN 4
845 #define SKINNY_CONNECTED 5
846 #define SKINNY_BUSY 6
847 #define SKINNY_CONGESTION 7
848 #define SKINNY_HOLD 8
849 #define SKINNY_CALLWAIT 9
850 #define SKINNY_TRANSFER 10
851 #define SKINNY_PARK 11
852 #define SKINNY_PROGRESS 12
853 #define SKINNY_CALLREMOTEMULTILINE 13
854 #define SKINNY_INVALID 14
856 #define SKINNY_SILENCE 0x00
857 #define SKINNY_DIALTONE 0x21
858 #define SKINNY_BUSYTONE 0x23
859 #define SKINNY_ALERT 0x24
860 #define SKINNY_REORDER 0x25
861 #define SKINNY_CALLWAITTONE 0x2D
862 #define SKINNY_NOTONE 0x7F
864 #define SKINNY_LAMP_OFF 1
865 #define SKINNY_LAMP_ON 2
866 #define SKINNY_LAMP_WINK 3
867 #define SKINNY_LAMP_FLASH 4
868 #define SKINNY_LAMP_BLINK 5
870 #define SKINNY_RING_OFF 1
871 #define SKINNY_RING_INSIDE 2
872 #define SKINNY_RING_OUTSIDE 3
873 #define SKINNY_RING_FEATURE 4
878 /* Skinny rtp stream modes. Do we really need this? */
879 #define SKINNY_CX_SENDONLY 0
880 #define SKINNY_CX_RECVONLY 1
881 #define SKINNY_CX_SENDRECV 2
882 #define SKINNY_CX_CONF 3
883 #define SKINNY_CX_CONFERENCE 3
884 #define SKINNY_CX_MUTE 4
885 #define SKINNY_CX_INACTIVE 4
888 static char *skinny_cxmodes[] = {
897 /* driver scheduler */
898 static struct sched_context *sched = NULL;
899 static struct io_context *io;
901 /* Protect the monitoring thread, so only one process can kill or start it, and not
902 when it's doing something critical. */
903 AST_MUTEX_DEFINE_STATIC(monlock);
904 /* Protect the network socket */
905 AST_MUTEX_DEFINE_STATIC(netlock);
906 /* Protect the session list */
907 AST_MUTEX_DEFINE_STATIC(sessionlock);
908 /* Protect the device list */
909 AST_MUTEX_DEFINE_STATIC(devicelock);
911 /* Protect the paging device list */
912 AST_MUTEX_DEFINE_STATIC(pagingdevicelock);
915 /* This is the thread for the monitor which checks for input on the channels
916 which are not currently in use. */
917 static pthread_t monitor_thread = AST_PTHREADT_NULL;
919 /* Wait up to 16 seconds for first digit */
920 static int firstdigittimeout = 16000;
922 /* How long to wait for following digits */
923 static int gendigittimeout = 8000;
925 /* How long to wait for an extra digit, if there is an ambiguous match */
926 static int matchdigittimeout = 3000;
928 struct skinny_subchannel {
930 struct ast_channel *owner;
932 struct ast_rtp *vrtp;
934 /* time_t lastouttime; */ /* Unused */
938 /* int lastout; */ /* Unused */
944 struct skinny_subchannel *next;
945 struct skinny_line *parent;
951 char label[24]; /* Label that shows next to the line buttons */
952 char accountcode[AST_MAX_ACCOUNT_CODE];
953 char exten[AST_MAX_EXTENSION]; /* Extension where to start */
954 char context[AST_MAX_CONTEXT];
955 char language[MAX_LANGUAGE];
956 char cid_num[AST_MAX_EXTENSION]; /* Caller*ID */
957 char cid_name[AST_MAX_EXTENSION]; /* Caller*ID */
958 char lastcallerid[AST_MAX_EXTENSION]; /* Last Caller*ID */
959 char call_forward[AST_MAX_EXTENSION];
960 char mailbox[AST_MAX_EXTENSION];
961 char vmexten[AST_MAX_EXTENSION];
962 char regexten[AST_MAX_EXTENSION]; /* Extension for auto-extensions */
963 char regcontext[AST_MAX_CONTEXT]; /* Context for auto-extensions */
964 char mohinterpret[MAX_MUSICCLASS];
965 char mohsuggest[MAX_MUSICCLASS];
966 char lastnumberdialed[AST_MAX_EXTENSION]; /* Last number that was dialed - used for redial */
967 int curtone; /* Current tone being played */
968 ast_group_t callgroup;
969 ast_group_t pickupgroup;
976 int dnd; /* How does this affect callwait? Do we just deny a skinny_request if we're dnd? */
985 int nonCodecCapability;
987 int msgstate; /* voicemail message state */
992 struct ast_codec_pref prefs;
993 struct skinny_subchannel *sub;
994 struct skinny_line *next;
995 struct skinny_device *parent;
998 struct skinny_speeddial {
1001 char context[AST_MAX_CONTEXT];
1002 char exten[AST_MAX_EXTENSION];
1008 struct skinny_speeddial *next;
1009 struct skinny_device *parent;
1012 struct skinny_addon {
1016 struct skinny_addon *next;
1017 struct skinny_device *parent;
1020 static struct skinny_device {
1021 /* A device containing one or more lines */
1024 char version_id[16];
1025 char exten[AST_MAX_EXTENSION]; /* Cruddy variable name, pick a better one */
1028 int lastlineinstance;
1029 int lastcallreference;
1031 struct sockaddr_in addr;
1032 struct in_addr ourip;
1033 struct skinny_line *lines;
1034 struct skinny_speeddial *speeddials;
1035 struct skinny_addon *addons;
1036 struct ast_codec_pref prefs;
1038 struct skinnysession *session;
1039 struct skinny_device *next;
1042 struct skinny_paging_device {
1045 struct skinny_device ** devices;
1046 struct skinny_paging_device *next;
1049 static struct skinnysession {
1052 struct sockaddr_in sin;
1054 char inbuf[SKINNY_MAX_PACKET];
1055 char outbuf[SKINNY_MAX_PACKET];
1056 struct skinny_device *device;
1057 struct skinnysession *next;
1060 static struct ast_channel *skinny_request(const char *type, int format, void *data, int *cause);
1061 static int skinny_devicestate(void *data);
1062 static int skinny_call(struct ast_channel *ast, char *dest, int timeout);
1063 static int skinny_hangup(struct ast_channel *ast);
1064 static int skinny_answer(struct ast_channel *ast);
1065 static struct ast_frame *skinny_read(struct ast_channel *ast);
1066 static int skinny_write(struct ast_channel *ast, struct ast_frame *frame);
1067 static int skinny_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen);
1068 static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
1069 static int skinny_senddigit_begin(struct ast_channel *ast, char digit);
1070 static int skinny_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
1071 static int handle_time_date_req_message(struct skinny_req *req, struct skinnysession *s);
1073 static const struct ast_channel_tech skinny_tech = {
1075 .description = tdesc,
1076 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
1077 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
1078 .requester = skinny_request,
1079 .devicestate = skinny_devicestate,
1080 .call = skinny_call,
1081 .hangup = skinny_hangup,
1082 .answer = skinny_answer,
1083 .read = skinny_read,
1084 .write = skinny_write,
1085 .indicate = skinny_indicate,
1086 .fixup = skinny_fixup,
1087 .send_digit_begin = skinny_senddigit_begin,
1088 .send_digit_end = skinny_senddigit_end,
1089 /* .bridge = ast_rtp_bridge, */
1092 static int skinny_extensionstate_cb(char *context, char* exten, int state, void *data);
1094 static void *get_button_template(struct skinnysession *s, struct button_definition_template *btn)
1096 struct skinny_device *d = s->device;
1097 struct skinny_addon *a = d->addons;
1101 case SKINNY_DEVICE_30SPPLUS:
1102 case SKINNY_DEVICE_30VIP:
1103 /* 13 rows, 2 columns */
1104 for (i = 0; i < 4; i++)
1105 (btn++)->buttonDefinition = BT_CUST_LINE;
1106 (btn++)->buttonDefinition = BT_REDIAL;
1107 (btn++)->buttonDefinition = BT_VOICEMAIL;
1108 (btn++)->buttonDefinition = BT_CALLPARK;
1109 (btn++)->buttonDefinition = BT_FORWARDALL;
1110 (btn++)->buttonDefinition = BT_CONFERENCE;
1111 for (i = 0; i < 4; i++)
1112 (btn++)->buttonDefinition = BT_NONE;
1113 for (i = 0; i < 13; i++)
1114 (btn++)->buttonDefinition = BT_SPEEDDIAL;
1117 case SKINNY_DEVICE_12SPPLUS:
1118 case SKINNY_DEVICE_12SP:
1119 case SKINNY_DEVICE_12:
1120 /* 6 rows, 2 columns */
1121 for (i = 0; i < 2; i++)
1122 (btn++)->buttonDefinition = BT_CUST_LINE;
1123 for (i = 0; i < 4; i++)
1124 (btn++)->buttonDefinition = BT_SPEEDDIAL;
1125 (btn++)->buttonDefinition = BT_HOLD;
1126 (btn++)->buttonDefinition = BT_REDIAL;
1127 (btn++)->buttonDefinition = BT_TRANSFER;
1128 (btn++)->buttonDefinition = BT_FORWARDALL;
1129 (btn++)->buttonDefinition = BT_CALLPARK;
1130 (btn++)->buttonDefinition = BT_VOICEMAIL;
1132 case SKINNY_DEVICE_7910:
1133 (btn++)->buttonDefinition = BT_LINE;
1134 (btn++)->buttonDefinition = BT_HOLD;
1135 (btn++)->buttonDefinition = BT_TRANSFER;
1136 (btn++)->buttonDefinition = BT_DISPLAY;
1137 (btn++)->buttonDefinition = BT_VOICEMAIL;
1138 (btn++)->buttonDefinition = BT_CONFERENCE;
1139 (btn++)->buttonDefinition = BT_FORWARDALL;
1140 for (i = 0; i < 2; i++)
1141 (btn++)->buttonDefinition = BT_SPEEDDIAL;
1142 (btn++)->buttonDefinition = BT_REDIAL;
1144 case SKINNY_DEVICE_7960:
1145 case SKINNY_DEVICE_7961:
1146 case SKINNY_DEVICE_7961GE:
1147 for (i = 0; i < 6; i++)
1148 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1150 case SKINNY_DEVICE_7940:
1151 case SKINNY_DEVICE_7941:
1152 case SKINNY_DEVICE_7941GE:
1153 for (i = 0; i < 2; i++)
1154 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1156 case SKINNY_DEVICE_7935:
1157 case SKINNY_DEVICE_7936:
1158 for (i = 0; i < 2; i++)
1159 (btn++)->buttonDefinition = BT_LINE;
1161 case SKINNY_DEVICE_ATA186:
1162 (btn++)->buttonDefinition = BT_LINE;
1164 case SKINNY_DEVICE_7970:
1165 case SKINNY_DEVICE_7971:
1166 case SKINNY_DEVICE_CIPC:
1167 for (i = 0; i < 8; i++)
1168 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1170 case SKINNY_DEVICE_7985:
1171 /* XXX I have no idea what the buttons look like on these. */
1172 ast_log(LOG_WARNING, "Unsupported device type '%d (7985)' found.\n", d->type);
1174 case SKINNY_DEVICE_7912:
1175 case SKINNY_DEVICE_7911:
1176 case SKINNY_DEVICE_7905:
1177 (btn++)->buttonDefinition = BT_LINE;
1178 (btn++)->buttonDefinition = BT_HOLD;
1180 case SKINNY_DEVICE_7920:
1181 /* XXX I don't know if this is right. */
1182 for (i = 0; i < 4; i++)
1183 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1185 case SKINNY_DEVICE_7902:
1186 ast_log(LOG_WARNING, "Unsupported device type '%d (7902)' found.\n", d->type);
1188 case SKINNY_DEVICE_SCCPGATEWAY_AN:
1189 case SKINNY_DEVICE_SCCPGATEWAY_BRI:
1190 ast_log(LOG_WARNING, "Unsupported device type '%d (SCCP gateway)' found.\n", d->type);
1193 ast_log(LOG_WARNING, "Unknown device type '%d' found.\n", d->type);
1197 for (a = d->addons; a; a = a->next) {
1198 if (!strcasecmp(a->type, "7914")) {
1199 for (i = 0; i < 14; i++)
1200 (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1202 ast_log(LOG_WARNING, "Unknown addon type '%s' found. Skipping.\n", a->type);
1209 static struct skinny_req *req_alloc(size_t size, int response_message)
1211 struct skinny_req *req;
1213 if (!(req = ast_calloc(1, skinny_header_size + size + 4)))
1216 req->len = htolel(size+4);
1217 req->e = htolel(response_message);
1222 static struct skinny_line *find_line_by_instance(struct skinny_device *d, int instance)
1224 struct skinny_line *l;
1226 /*Dialing from on hook or on a 7920 uses instance 0 in requests
1227 but we need to start looking at instance 1 */
1232 for (l = d->lines; l; l = l->next) {
1233 if (l->instance == instance)
1238 ast_log(LOG_WARNING, "Could not find line with instance '%d' on device '%s'\n", instance, d->name);
1243 static struct skinny_line *find_line_by_name(const char *dest)
1245 struct skinny_line *l;
1246 struct skinny_device *d;
1251 ast_copy_string(line, dest, sizeof(line));
1252 at = strchr(line, '@');
1254 ast_log(LOG_NOTICE, "Device '%s' has no @ (at) sign!\n", dest);
1259 ast_mutex_lock(&devicelock);
1260 for (d = devices; d; d = d->next) {
1261 if (!strcasecmp(d->name, device)) {
1263 ast_verbose("Found device: %s\n", d->name);
1264 /* Found the device */
1265 for (l = d->lines; l; l = l->next) {
1266 /* Search for the right line */
1267 if (!strcasecmp(l->name, line)) {
1268 ast_mutex_unlock(&devicelock);
1274 /* Device not found */
1275 ast_mutex_unlock(&devicelock);
1279 /* It's quicker/easier to find the subchannel when we know the instance number too */
1280 static struct skinny_subchannel *find_subchannel_by_instance_reference(struct skinny_device *d, int instance, int reference)
1282 struct skinny_line *l = find_line_by_instance(d, instance);
1283 struct skinny_subchannel *sub;
1289 /* 7920 phones set call reference to 0, so use the first
1290 sub-channel on the list.
1291 This MIGHT need more love to be right */
1295 for (sub = l->sub; sub; sub = sub->next) {
1296 if (sub->callid == reference)
1301 ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s'\n", reference, d->name);
1306 /* Find the subchannel when we only have the callid - this shouldn't happen often */
1307 static struct skinny_subchannel *find_subchannel_by_reference(struct skinny_device *d, int reference)
1309 struct skinny_line *l;
1310 struct skinny_subchannel *sub = NULL;
1312 for (l = d->lines; l; l = l->next) {
1313 for (sub = l->sub; sub; sub = sub->next) {
1314 if (sub->callid == reference)
1322 ast_log(LOG_WARNING, "Could not find any lines that contained a subchannel with reference '%d' on device '%s'\n", reference, d->name);
1325 ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s@%s'\n", reference, l->name, d->name);
1331 static struct skinny_speeddial *find_speeddial_by_instance(struct skinny_device *d, int instance, int isHint)
1333 struct skinny_speeddial *sd;
1335 for (sd = d->speeddials; sd; sd = sd->next) {
1336 if (sd->isHint == isHint && sd->instance == instance)
1341 ast_log(LOG_WARNING, "Could not find speeddial with instance '%d' on device '%s'\n", instance, d->name);
1346 static int codec_skinny2ast(enum skinny_codecs skinnycodec)
1348 switch (skinnycodec) {
1349 case SKINNY_CODEC_ALAW:
1350 return AST_FORMAT_ALAW;
1351 case SKINNY_CODEC_ULAW:
1352 return AST_FORMAT_ULAW;
1353 case SKINNY_CODEC_G723_1:
1354 return AST_FORMAT_G723_1;
1355 case SKINNY_CODEC_G729A:
1356 return AST_FORMAT_G729A;
1357 case SKINNY_CODEC_G726_32:
1358 return AST_FORMAT_G726_AAL2; /* XXX Is this right? */
1359 case SKINNY_CODEC_H261:
1360 return AST_FORMAT_H261;
1361 case SKINNY_CODEC_H263:
1362 return AST_FORMAT_H263;
1368 static int codec_ast2skinny(int astcodec)
1371 case AST_FORMAT_ALAW:
1372 return SKINNY_CODEC_ALAW;
1373 case AST_FORMAT_ULAW:
1374 return SKINNY_CODEC_ULAW;
1375 case AST_FORMAT_G723_1:
1376 return SKINNY_CODEC_G723_1;
1377 case AST_FORMAT_G729A:
1378 return SKINNY_CODEC_G729A;
1379 case AST_FORMAT_G726_AAL2: /* XXX Is this right? */
1380 return SKINNY_CODEC_G726_32;
1381 case AST_FORMAT_H261:
1382 return SKINNY_CODEC_H261;
1383 case AST_FORMAT_H263:
1384 return SKINNY_CODEC_H263;
1390 static void cleanup_stale_contexts(char *new, char *old)
1392 char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
1394 while ((oldcontext = strsep(&old, "&"))) {
1395 stalecontext = '\0';
1396 ast_copy_string(newlist, new, sizeof(newlist));
1398 while ((newcontext = strsep(&stringp, "&"))) {
1399 if (strcmp(newcontext, oldcontext) == 0) {
1400 /* This is not the context you're looking for */
1401 stalecontext = '\0';
1403 } else if (strcmp(newcontext, oldcontext)) {
1404 stalecontext = oldcontext;
1409 ast_context_destroy(ast_context_find(stalecontext), "Skinny");
1413 static void register_exten(struct skinny_line *l)
1416 char *stringp, *ext, *context;
1418 if (ast_strlen_zero(regcontext))
1421 ast_copy_string(multi, S_OR(l->regexten, l->name), sizeof(multi));
1423 while ((ext = strsep(&stringp, "&"))) {
1424 if ((context = strchr(ext, '@'))) {
1425 *context++ = '\0'; /* split ext@context */
1426 if (!ast_context_find(context)) {
1427 ast_log(LOG_WARNING, "Context %s must exist in regcontext= in skinny.conf!\n", context);
1431 context = regcontext;
1433 ast_add_extension(context, 1, ext, 1, NULL, NULL, "Noop",
1434 ast_strdup(l->name), ast_free, "Skinny");
1438 static void unregister_exten(struct skinny_line *l)
1441 char *stringp, *ext, *context;
1443 if (ast_strlen_zero(regcontext))
1446 ast_copy_string(multi, S_OR(l->regexten, l->name), sizeof(multi));
1448 while ((ext = strsep(&stringp, "&"))) {
1449 if ((context = strchr(ext, '@'))) {
1450 *context++ = '\0'; /* split ext@context */
1451 if (!ast_context_find(context)) {
1452 ast_log(LOG_WARNING, "Context %s must exist in regcontext= in skinny.conf!\n", context);
1456 context = regcontext;
1458 ast_context_remove_extension(context, ext, 1, NULL);
1462 static int skinny_register(struct skinny_req *req, struct skinnysession *s)
1464 struct skinny_device *d;
1465 struct skinny_line *l;
1466 struct skinny_speeddial *sd;
1467 struct sockaddr_in sin;
1470 ast_mutex_lock(&devicelock);
1471 for (d = devices; d; d = d->next) {
1472 if (!strcasecmp(req->data.reg.name, d->id)
1473 && ast_apply_ha(d->ha, &(s->sin))) {
1475 d->type = letohl(req->data.reg.type);
1476 if (ast_strlen_zero(d->version_id)) {
1477 ast_copy_string(d->version_id, version_id, sizeof(d->version_id));
1483 if (getsockname(s->fd, (struct sockaddr *)&sin, &slen)) {
1484 ast_log(LOG_WARNING, "Cannot get socket name\n");
1485 sin.sin_addr = __ourip;
1487 d->ourip = sin.sin_addr;
1489 for (sd = d->speeddials; sd; sd = sd->next) {
1490 sd->stateid = ast_extension_state_add(sd->context, sd->exten, skinny_extensionstate_cb, sd);
1492 for (l = d->lines; l; l = l->next) {
1494 ast_device_state_changed("Skinny/%s@%s", l->name, d->name);
1499 ast_mutex_unlock(&devicelock);
1506 static int skinny_unregister(struct skinny_req *req, struct skinnysession *s)
1508 struct skinny_device *d;
1509 struct skinny_line *l;
1510 struct skinny_speeddial *sd;
1518 for (sd = d->speeddials; sd; sd = sd->next) {
1519 if (sd->stateid > -1)
1520 ast_extension_state_del(sd->stateid, NULL);
1522 for (l = d->lines; l; l = l->next) {
1523 unregister_exten(l);
1524 ast_device_state_changed("Skinny/%s@%s", l->name, d->name);
1528 return -1; /* main loop will destroy the session */
1531 static int transmit_response(struct skinnysession *s, struct skinny_req *req)
1536 ast_log(LOG_WARNING, "Asked to transmit to a non-existant session!\n");
1540 ast_mutex_lock(&s->lock);
1543 ast_log(LOG_VERBOSE, "writing packet type %04X (%d bytes) to socket %d\n", letohl(req->e), letohl(req->len)+8, s->fd);
1545 if (letohl(req->len > SKINNY_MAX_PACKET) || letohl(req->len < 0)) {
1546 ast_log(LOG_WARNING, "transmit_response: the length of the request is out of bounds\n");
1550 memset(s->outbuf,0,sizeof(s->outbuf));
1551 memcpy(s->outbuf, req, skinny_header_size);
1552 memcpy(s->outbuf+skinny_header_size, &req->data, letohl(req->len));
1554 res = write(s->fd, s->outbuf, letohl(req->len)+8);
1556 if (res != letohl(req->len)+8) {
1557 ast_log(LOG_WARNING, "Transmit: write only sent %d out of %d bytes: %s\n", res, letohl(req->len)+8, strerror(errno));
1560 ast_log(LOG_WARNING, "Transmit: Skinny Client was lost, unregistering\n");
1561 skinny_unregister(NULL, s);
1566 ast_mutex_unlock(&s->lock);
1570 static void transmit_speaker_mode(struct skinnysession *s, int mode)
1572 struct skinny_req *req;
1574 if (!(req = req_alloc(sizeof(struct set_speaker_message), SET_SPEAKER_MESSAGE)))
1577 req->data.setspeaker.mode = htolel(mode);
1578 transmit_response(s, req);
1581 static void transmit_microphone_mode(struct skinnysession *s, int mode)
1583 struct skinny_req *req;
1585 if (!(req = req_alloc(sizeof(struct set_microphone_message), SET_MICROPHONE_MESSAGE)))
1588 req->data.setmicrophone.mode = htolel(mode);
1589 transmit_response(s, req);
1592 static void transmit_callstate(struct skinnysession *s, int instance, int state, unsigned callid)
1594 struct skinny_req *req;
1596 if (!(req = req_alloc(sizeof(struct call_state_message), CALL_STATE_MESSAGE)))
1599 if (state == SKINNY_ONHOOK) {
1600 transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
1602 req->data.callstate.callState = htolel(state);
1603 req->data.callstate.lineInstance = htolel(instance);
1604 req->data.callstate.callReference = htolel(callid);
1605 transmit_response(s, req);
1606 if (state == SKINNY_OFFHOOK) {
1607 if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
1610 req->data.activatecallplane.lineInstance = htolel(instance);
1611 transmit_response(s, req);
1612 } else if (state == SKINNY_ONHOOK) {
1613 if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
1616 req->data.activatecallplane.lineInstance = htolel(instance);
1617 transmit_response(s, req);
1619 if (!(req = req_alloc(sizeof(struct close_receive_channel_message), CLOSE_RECEIVE_CHANNEL_MESSAGE)))
1622 req->data.closereceivechannel.conferenceId = 0;
1623 req->data.closereceivechannel.partyId = htolel(callid);
1624 transmit_response(s, req);
1626 if (!(req = req_alloc(sizeof(struct stop_media_transmission_message), STOP_MEDIA_TRANSMISSION_MESSAGE)))
1629 req->data.stopmedia.conferenceId = 0;
1630 req->data.stopmedia.passThruPartyId = htolel(callid);
1631 transmit_response(s, req);
1635 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)
1637 struct skinny_req *req;
1639 if (!(req = req_alloc(sizeof(struct call_info_message), CALL_INFO_MESSAGE)))
1643 ast_verbose("Setting Callinfo to %s(%s) from %s(%s) on %s(%d)\n", fromname, fromnum, toname, tonum, s->device->name, instance);
1646 ast_copy_string(req->data.callinfo.callingPartyName, fromname, sizeof(req->data.callinfo.callingPartyName));
1649 ast_copy_string(req->data.callinfo.callingParty, fromnum, sizeof(req->data.callinfo.callingParty));
1652 ast_copy_string(req->data.callinfo.calledPartyName, toname, sizeof(req->data.callinfo.calledPartyName));
1655 ast_copy_string(req->data.callinfo.calledParty, tonum, sizeof(req->data.callinfo.calledParty));
1657 req->data.callinfo.instance = htolel(instance);
1658 req->data.callinfo.reference = htolel(callid);
1659 req->data.callinfo.type = htolel(calltype);
1660 transmit_response(s, req);
1663 static void transmit_connect(struct skinnysession *s, struct skinny_subchannel *sub)
1665 struct skinny_req *req;
1666 struct skinny_line *l = sub->parent;
1667 struct ast_format_list fmt;
1669 if (!(req = req_alloc(sizeof(struct open_receive_channel_message), OPEN_RECEIVE_CHANNEL_MESSAGE)))
1672 fmt = ast_codec_pref_getsize(&l->prefs, ast_best_codec(l->capability));
1674 req->data.openreceivechannel.conferenceId = htolel(0);
1675 req->data.openreceivechannel.partyId = htolel(sub->callid);
1676 req->data.openreceivechannel.packets = htolel(fmt.cur_ms);
1677 req->data.openreceivechannel.capability = htolel(codec_ast2skinny(fmt.bits));
1678 req->data.openreceivechannel.echo = htolel(0);
1679 req->data.openreceivechannel.bitrate = htolel(0);
1680 transmit_response(s, req);
1683 static void transmit_tone(struct skinnysession *s, int tone)
1685 struct skinny_req *req;
1687 if (tone == SKINNY_NOTONE) {
1688 /* This is bad, mmm'kay? */
1693 if (!(req = req_alloc(sizeof(struct start_tone_message), START_TONE_MESSAGE)))
1696 if (!(req = req_alloc(sizeof(struct stop_tone_message), STOP_TONE_MESSAGE)))
1701 req->data.starttone.tone = htolel(tone);
1703 transmit_response(s, req);
1706 static void transmit_selectsoftkeys(struct skinnysession *s, int instance, int callid, int softkey)
1708 struct skinny_req *req;
1710 if (!(req = req_alloc(sizeof(struct select_soft_keys_message), SELECT_SOFT_KEYS_MESSAGE)))
1713 req->data.selectsoftkey.instance = htolel(instance);
1714 req->data.selectsoftkey.reference = htolel(callid);
1715 req->data.selectsoftkey.softKeySetIndex = htolel(softkey);
1716 req->data.selectsoftkey.validKeyMask = htolel(0xFFFFFFFF);
1717 transmit_response(s, req);
1720 static void transmit_lamp_indication(struct skinnysession *s, int stimulus, int instance, int indication)
1722 struct skinny_req *req;
1724 if (!(req = req_alloc(sizeof(struct set_lamp_message), SET_LAMP_MESSAGE)))
1727 req->data.setlamp.stimulus = htolel(stimulus);
1728 req->data.setlamp.stimulusInstance = htolel(instance);
1729 req->data.setlamp.deviceStimulus = htolel(indication);
1730 transmit_response(s, req);
1733 static void transmit_ringer_mode(struct skinnysession *s, int mode)
1735 struct skinny_req *req;
1738 ast_verbose("Setting ringer mode to '%d'.\n", mode);
1740 if (!(req = req_alloc(sizeof(struct set_ringer_message), SET_RINGER_MESSAGE)))
1743 req->data.setringer.ringerMode = htolel(mode);
1744 /* XXX okay, I don't quite know what this is, but here's what happens (on a 7960).
1745 Note: The phone will always show as ringing on the display.
1747 1: phone will audibly ring over and over
1748 2: phone will audibly ring only once
1749 any other value, will NOT cause the phone to audibly ring
1751 req->data.setringer.unknown1 = htolel(1);
1752 /* XXX the value here doesn't seem to change anything. Must be higher than 0.
1753 Perhaps a packet capture can shed some light on this. */
1754 req->data.setringer.unknown2 = htolel(1);
1755 transmit_response(s, req);
1758 static void transmit_displaymessage(struct skinnysession *s, const char *text)
1760 struct skinny_req *req;
1763 if (!(req = req_alloc(0, CLEAR_DISPLAY_MESSAGE)))
1767 ast_verbose("Clearing Display\n");
1769 if (!(req = req_alloc(sizeof(struct displaytext_message), DISPLAYTEXT_MESSAGE)))
1772 ast_copy_string(req->data.displaytext.text, text, sizeof(req->data.displaytext.text));
1774 ast_verbose("Displaying message '%s'\n", req->data.displaytext.text);
1777 transmit_response(s, req);
1780 static void transmit_displaynotify(struct skinnysession *s, const char *text, int t)
1782 struct skinny_req *req;
1784 if (!(req = req_alloc(sizeof(struct display_notify_message), DISPLAY_NOTIFY_MESSAGE)))
1787 ast_copy_string(req->data.displaynotify.displayMessage, text, sizeof(req->data.displaynotify.displayMessage));
1788 req->data.displaynotify.displayTimeout = htolel(t);
1791 ast_verbose("Displaying notify '%s'\n", text);
1793 transmit_response(s, req);
1796 static void transmit_displaypromptstatus(struct skinnysession *s, const char *text, int t, int instance, int callid)
1798 struct skinny_req *req;
1800 if (!(req = req_alloc(sizeof(struct display_prompt_status_message), DISPLAY_PROMPT_STATUS_MESSAGE)))
1803 ast_copy_string(req->data.displaypromptstatus.promptMessage, text, sizeof(req->data.displaypromptstatus.promptMessage));
1804 req->data.displaypromptstatus.messageTimeout = htolel(t);
1805 req->data.displaypromptstatus.lineInstance = htolel(instance);
1806 req->data.displaypromptstatus.callReference = htolel(callid);
1809 ast_verbose("Displaying Prompt Status '%s'\n", text);
1811 transmit_response(s, req);
1814 static void transmit_dialednumber(struct skinnysession *s, const char *text, int instance, int callid)
1816 struct skinny_req *req;
1818 if (!(req = req_alloc(sizeof(struct dialed_number_message), DIALED_NUMBER_MESSAGE)))
1821 ast_copy_string(req->data.dialednumber.dialedNumber, text, sizeof(req->data.dialednumber.dialedNumber));
1822 req->data.dialednumber.lineInstance = htolel(instance);
1823 req->data.dialednumber.callReference = htolel(callid);
1825 transmit_response(s, req);
1828 static int skinny_extensionstate_cb(char *context, char *exten, int state, void *data)
1830 struct skinny_speeddial *sd = data;
1831 struct skinny_device *d = sd->parent;
1832 struct skinnysession *s = d->session;
1833 char hint[AST_MAX_EXTENSION];
1834 int callstate = SKINNY_CALLREMOTEMULTILINE;
1835 int lamp = SKINNY_LAMP_OFF;
1838 case AST_EXTENSION_DEACTIVATED: /* Retry after a while */
1839 case AST_EXTENSION_REMOVED: /* Extension is gone */
1840 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);
1842 callstate = SKINNY_ONHOOK;
1843 lamp = SKINNY_LAMP_OFF;
1845 case AST_EXTENSION_RINGING:
1846 case AST_EXTENSION_UNAVAILABLE:
1847 callstate = SKINNY_RINGIN;
1848 lamp = SKINNY_LAMP_BLINK;
1850 case AST_EXTENSION_BUSY: /* callstate = SKINNY_BUSY wasn't wanting to work - I'll settle for this */
1851 case AST_EXTENSION_INUSE:
1852 callstate = SKINNY_CALLREMOTEMULTILINE;
1853 lamp = SKINNY_LAMP_ON;
1855 case AST_EXTENSION_ONHOLD:
1856 callstate = SKINNY_HOLD;
1857 lamp = SKINNY_LAMP_WINK;
1859 case AST_EXTENSION_NOT_INUSE:
1861 callstate = SKINNY_ONHOOK;
1862 lamp = SKINNY_LAMP_OFF;
1866 if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, sd->context, sd->exten)) {
1867 /* If they are not registered, we will override notification and show no availability */
1868 if (ast_device_state(hint) == AST_DEVICE_UNAVAILABLE) {
1869 callstate = SKINNY_ONHOOK;
1870 lamp = SKINNY_LAMP_FLASH;
1874 transmit_lamp_indication(s, STIMULUS_LINE, sd->instance, lamp);
1875 transmit_callstate(s, sd->instance, callstate, 0);
1876 sd->laststate = state;
1881 static int has_voicemail(struct skinny_line *l)
1883 return ast_app_has_voicemail(l->mailbox, NULL);
1886 static void do_housekeeping(struct skinnysession *s)
1890 int device_lamp = 0;
1891 struct skinny_device *d = s->device;
1892 struct skinny_line *l;
1894 /* Update time on device */
1895 handle_time_date_req_message(NULL, s);
1897 /* Set MWI on individual lines */
1898 for (l = d->lines; l; l = l->next) {
1899 if (has_voicemail(l)) {
1901 ast_verbose("Checking for voicemail Skinny %s@%s\n", l->name, d->name);
1902 ast_app_inboxcount(l->mailbox, &new, &old);
1904 ast_verbose("Skinny %s@%s has voicemail!\n", l->name, d->name);
1905 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, l->instance, l->mwiblink?SKINNY_LAMP_BLINK:SKINNY_LAMP_ON);
1908 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, l->instance, SKINNY_LAMP_OFF);
1911 /* If at least one line has VM, turn the device level lamp on */
1913 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, 0, SKINNY_LAMP_ON);
1915 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, 0, SKINNY_LAMP_OFF);
1918 /* I do not believe skinny can deal with video.
1919 Anyone know differently? */
1920 /* Yes, it can. Currently 7985 and Cisco VT Advantage do video. */
1921 static enum ast_rtp_get_result skinny_get_vrtp_peer(struct ast_channel *c, struct ast_rtp **rtp)
1923 struct skinny_subchannel *sub = NULL;
1925 if (!(sub = c->tech_pvt) || !(sub->vrtp))
1926 return AST_RTP_GET_FAILED;
1930 return AST_RTP_TRY_NATIVE;
1933 static enum ast_rtp_get_result skinny_get_rtp_peer(struct ast_channel *c, struct ast_rtp **rtp)
1935 struct skinny_subchannel *sub = NULL;
1937 if (!(sub = c->tech_pvt) || !(sub->rtp))
1938 return AST_RTP_GET_FAILED;
1942 return AST_RTP_TRY_NATIVE;
1945 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)
1947 struct skinny_subchannel *sub;
1950 /* transmit_modify_with_sdp(sub, rtp); @@FIXME@@ if needed */
1956 static struct ast_rtp_protocol skinny_rtp = {
1958 .get_rtp_info = skinny_get_rtp_peer,
1959 .get_vrtp_info = skinny_get_vrtp_peer,
1960 .set_rtp_peer = skinny_set_rtp_peer,
1963 static int skinny_do_debug(int fd, int argc, char *argv[])
1966 return RESULT_SHOWUSAGE;
1969 ast_cli(fd, "Skinny Debugging Enabled\n");
1970 return RESULT_SUCCESS;
1973 static int skinny_no_debug(int fd, int argc, char *argv[])
1976 return RESULT_SHOWUSAGE;
1979 ast_cli(fd, "Skinny Debugging Disabled\n");
1980 return RESULT_SUCCESS;
1983 static char *complete_skinny_reset(const char *line, const char *word, int pos, int state)
1985 struct skinny_device *d;
1987 char *result = NULL;
1988 int wordlen = strlen(word);
1992 for (d = devices; d && !result; d = d->next) {
1993 if (!strncasecmp(word, d->id, wordlen) && ++which > state)
1994 result = ast_strdup(d->id);
2001 static int skinny_reset_device(int fd, int argc, char *argv[])
2003 struct skinny_device *d;
2004 struct skinny_req *req;
2006 if (argc < 3 || argc > 4) {
2007 return RESULT_SHOWUSAGE;
2009 ast_mutex_lock(&devicelock);
2011 for (d = devices; d; d = d->next) {
2012 int fullrestart = 0;
2013 if (!strcasecmp(argv[2], d->id) || !strcasecmp(argv[2], "all")) {
2017 if (!(req = req_alloc(sizeof(struct reset_message), RESET_MESSAGE)))
2020 if (argc == 4 && !strcasecmp(argv[3], "restart"))
2024 req->data.reset.resetType = 2;
2026 req->data.reset.resetType = 1;
2028 if (option_verbose > 2)
2029 ast_verbose(VERBOSE_PREFIX_3 "%s device %s.\n", (fullrestart) ? "Restarting" : "Resetting", d->id);
2030 transmit_response(d->session, req);
2033 ast_mutex_unlock(&devicelock);
2034 return RESULT_SUCCESS;
2037 static char *device2str(int type)
2042 case SKINNY_DEVICE_NONE:
2044 case SKINNY_DEVICE_30SPPLUS:
2046 case SKINNY_DEVICE_12SPPLUS:
2048 case SKINNY_DEVICE_12SP:
2050 case SKINNY_DEVICE_12:
2052 case SKINNY_DEVICE_30VIP:
2054 case SKINNY_DEVICE_7910:
2056 case SKINNY_DEVICE_7960:
2058 case SKINNY_DEVICE_7940:
2060 case SKINNY_DEVICE_7935:
2062 case SKINNY_DEVICE_ATA186:
2064 case SKINNY_DEVICE_7941:
2066 case SKINNY_DEVICE_7971:
2068 case SKINNY_DEVICE_7985:
2070 case SKINNY_DEVICE_7911:
2072 case SKINNY_DEVICE_7961GE:
2074 case SKINNY_DEVICE_7941GE:
2076 case SKINNY_DEVICE_7905:
2078 case SKINNY_DEVICE_7920:
2080 case SKINNY_DEVICE_7970:
2082 case SKINNY_DEVICE_7912:
2084 case SKINNY_DEVICE_7902:
2086 case SKINNY_DEVICE_CIPC:
2087 return "IP Communicator";
2088 case SKINNY_DEVICE_7961:
2090 case SKINNY_DEVICE_7936:
2092 case SKINNY_DEVICE_SCCPGATEWAY_AN:
2093 return "SCCPGATEWAY_AN";
2094 case SKINNY_DEVICE_SCCPGATEWAY_BRI:
2095 return "SCCPGATEWAY_BRI";
2096 case SKINNY_DEVICE_UNKNOWN:
2099 if (!(tmp = ast_threadstorage_get(&device2str_threadbuf, DEVICE2STR_BUFSIZE)))
2101 snprintf(tmp, DEVICE2STR_BUFSIZE, "UNKNOWN-%d", type);
2106 static int skinny_show_devices(int fd, int argc, char *argv[])
2108 struct skinny_device *d;
2109 struct skinny_line *l;
2113 return RESULT_SHOWUSAGE;
2115 ast_mutex_lock(&devicelock);
2117 ast_cli(fd, "Name DeviceId IP Type R NL\n");
2118 ast_cli(fd, "-------------------- ---------------- --------------- --------------- - --\n");
2119 for (d = devices; d; d = d->next) {
2121 for (l = d->lines; l; l = l->next) {
2125 ast_cli(fd, "%-20s %-16s %-15s %-15s %c %2d\n",
2128 d->session?ast_inet_ntoa(d->session->sin.sin_addr):"",
2129 device2str(d->type),
2130 d->registered?'Y':'N',
2133 ast_mutex_unlock(&devicelock);
2134 return RESULT_SUCCESS;
2137 static int skinny_show_lines(int fd, int argc, char *argv[])
2139 struct skinny_device *d;
2140 struct skinny_line *l;
2143 return RESULT_SHOWUSAGE;
2145 ast_mutex_lock(&devicelock);
2147 ast_cli(fd, "Device Name Instance Name Label \n");
2148 ast_cli(fd, "-------------------- -------- -------------------- --------------------\n");
2149 for (d = devices; d; d = d->next) {
2150 for (l = d->lines; l; l = l->next) {
2151 ast_cli(fd, "%-20s %8d %-20s %-20s\n",
2159 ast_mutex_unlock(&devicelock);
2160 return RESULT_SUCCESS;
2163 static const char show_devices_usage[] =
2164 "Usage: skinny show devices\n"
2165 " Lists all devices known to the Skinny subsystem.\n";
2167 static const char show_lines_usage[] =
2168 "Usage: skinny show lines\n"
2169 " Lists all lines known to the Skinny subsystem.\n";
2171 static const char debug_usage[] =
2172 "Usage: skinny set debug\n"
2173 " Enables dumping of Skinny packets for debugging purposes\n";
2175 static const char no_debug_usage[] =
2176 "Usage: skinny set debug off\n"
2177 " Disables dumping of Skinny packets for debugging purposes\n";
2179 static const char reset_usage[] =
2180 "Usage: skinny reset <DeviceId|all> [restart]\n"
2181 " Causes a Skinny device to reset itself, optionally with a full restart\n";
2183 static struct ast_cli_entry cli_skinny[] = {
2184 { { "skinny", "show", "devices", NULL },
2185 skinny_show_devices, "List defined Skinny devices",
2186 show_devices_usage },
2188 { { "skinny", "show", "lines", NULL },
2189 skinny_show_lines, "List defined Skinny lines per device",
2192 { { "skinny", "set", "debug", NULL },
2193 skinny_do_debug, "Enable Skinny debugging",
2196 { { "skinny", "set", "debug", "off", NULL },
2197 skinny_no_debug, "Disable Skinny debugging",
2200 { { "skinny", "reset", NULL },
2201 skinny_reset_device, "Reset Skinny device(s)",
2202 reset_usage, complete_skinny_reset },
2206 static struct skinny_paging_device *build_paging_device(const char *cat, struct ast_variable *v)
2212 static struct skinny_device *build_device(const char *cat, struct ast_variable *v)
2214 struct skinny_device *d;
2215 struct skinny_line *l;
2216 struct skinny_speeddial *sd;
2217 struct skinny_addon *a;
2218 char device_vmexten[AST_MAX_EXTENSION];
2219 int lineInstance = 1;
2220 int speeddialInstance = 1;
2223 if (!(d = ast_calloc(1, sizeof(*d)))) {
2226 ast_copy_string(d->name, cat, sizeof(d->name));
2227 d->lastlineinstance = 1;
2228 d->capability = default_capability;
2229 d->prefs = default_prefs;
2230 if (!ast_strlen_zero(vmexten))
2231 ast_copy_string(device_vmexten, vmexten, sizeof(device_vmexten));
2233 if (!strcasecmp(v->name, "host")) {
2234 if (ast_get_ip(&d->addr, v->value)) {
2238 } else if (!strcasecmp(v->name, "port")) {
2239 d->addr.sin_port = htons(atoi(v->value));
2240 } else if (!strcasecmp(v->name, "device")) {
2241 ast_copy_string(d->id, v->value, sizeof(d->id));
2242 } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
2243 d->ha = ast_append_ha(v->name, v->value, d->ha, NULL);
2244 } else if (!strcasecmp(v->name, "vmexten")) {
2245 ast_copy_string(device_vmexten, v->value, sizeof(device_vmexten));
2246 } else if (!strcasecmp(v->name, "context")) {
2247 ast_copy_string(context, v->value, sizeof(context));
2248 } else if (!strcasecmp(v->name, "regexten")) {
2249 ast_copy_string(regexten, v->value, sizeof(regexten));
2250 } else if (!strcasecmp(v->name, "allow")) {
2251 ast_parse_allow_disallow(&d->prefs, &d->capability, v->value, 1);
2252 } else if (!strcasecmp(v->name, "disallow")) {
2253 ast_parse_allow_disallow(&d->prefs, &d->capability, v->value, 0);
2254 } else if (!strcasecmp(v->name, "version")) {
2255 ast_copy_string(d->version_id, v->value, sizeof(d->version_id));
2256 } else if (!strcasecmp(v->name, "nat")) {
2257 nat = ast_true(v->value);
2258 } else if (!strcasecmp(v->name, "callerid")) {
2259 if (!strcasecmp(v->value, "asreceived")) {
2263 ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
2265 } else if (!strcasecmp(v->name, "language")) {
2266 ast_copy_string(language, v->value, sizeof(language));
2267 } else if (!strcasecmp(v->name, "accountcode")) {
2268 ast_copy_string(accountcode, v->value, sizeof(accountcode));
2269 } else if (!strcasecmp(v->name, "amaflags")) {
2270 y = ast_cdr_amaflags2int(v->value);
2272 ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno);
2276 } else if (!strcasecmp(v->name, "mohinterpret") || !strcasecmp(v->name, "musiconhold")) {
2277 ast_copy_string(mohinterpret, v->value, sizeof(mohinterpret));
2278 } else if (!strcasecmp(v->name, "mohsuggest")) {
2279 ast_copy_string(mohsuggest, v->value, sizeof(mohsuggest));
2280 } else if (!strcasecmp(v->name, "callgroup")) {
2281 cur_callergroup = ast_get_group(v->value);
2282 } else if (!strcasecmp(v->name, "pickupgroup")) {
2283 cur_pickupgroup = ast_get_group(v->value);
2284 } else if (!strcasecmp(v->name, "immediate")) {
2285 immediate = ast_true(v->value);
2286 } else if (!strcasecmp(v->name, "cancallforward")) {
2287 cancallforward = ast_true(v->value);
2288 } else if (!strcasecmp(v->name, "mailbox")) {
2289 ast_copy_string(mailbox, v->value, sizeof(mailbox));
2290 } else if (!strcasecmp(v->name, "callreturn")) {
2291 callreturn = ast_true(v->value);
2292 } else if (!strcasecmp(v->name, "callwaiting")) {
2293 callwaiting = ast_true(v->value);
2294 } else if (!strcasecmp(v->name, "transfer")) {
2295 transfer = ast_true(v->value);
2296 } else if (!strcasecmp(v->name, "threewaycalling")) {
2297 threewaycalling = ast_true(v->value);
2298 } else if (!strcasecmp(v->name, "mwiblink")) {
2299 mwiblink = ast_true(v->value);
2300 } else if (!strcasecmp(v->name, "linelabel")) {
2301 ast_copy_string(linelabel, v->value, sizeof(linelabel));
2302 } else if (!strcasecmp(v->name, "speeddial")) {
2303 if (!(sd = ast_calloc(1, sizeof(*sd)))) {
2306 char *stringp, *exten, *context, *label;
2308 exten = strsep(&stringp, ",");
2309 if ((context = strchr(exten, '@'))) {
2313 ast_mutex_init(&sd->lock);
2314 ast_copy_string(sd->exten, exten, sizeof(sd->exten));
2315 if (!ast_strlen_zero(context)) {
2317 sd->instance = lineInstance++;
2318 ast_copy_string(sd->context, context, sizeof(sd->context));
2321 sd->instance = speeddialInstance++;
2322 sd->context[0] = '\0';
2324 ast_copy_string(sd->label, S_OR(label, exten), sizeof(sd->label));
2328 sd->next = d->speeddials;
2331 } else if (!strcasecmp(v->name, "addon")) {
2332 if (!(a = ast_calloc(1, sizeof(*a)))) {
2335 ast_mutex_init(&a->lock);
2336 ast_copy_string(a->type, v->value, sizeof(a->type));
2338 a->next = d->addons;
2341 } else if (!strcasecmp(v->name, "trunk") || !strcasecmp(v->name, "line")) {
2342 if (!(l = ast_calloc(1, sizeof(*l)))) {
2345 ast_mutex_init(&l->lock);
2346 ast_copy_string(l->name, v->value, sizeof(l->name));
2348 /* XXX Should we check for uniqueness?? XXX */
2349 ast_copy_string(l->context, context, sizeof(l->context));
2350 ast_copy_string(l->cid_num, cid_num, sizeof(l->cid_num));
2351 ast_copy_string(l->cid_name, cid_name, sizeof(l->cid_name));
2352 ast_copy_string(l->label, linelabel, sizeof(l->label));
2353 ast_copy_string(l->language, language, sizeof(l->language));
2354 ast_copy_string(l->mohinterpret, mohinterpret, sizeof(l->mohinterpret));
2355 ast_copy_string(l->mohsuggest, mohsuggest, sizeof(l->mohsuggest));
2356 ast_copy_string(l->regexten, regexten, sizeof(l->regexten));
2357 ast_copy_string(l->mailbox, mailbox, sizeof(l->mailbox));
2358 if (!ast_strlen_zero(mailbox)) {
2359 if (option_verbose > 2)
2360 ast_verbose(VERBOSE_PREFIX_3 "Setting mailbox '%s' on %s@%s\n", mailbox, d->name, l->name);
2362 if (!ast_strlen_zero(device_vmexten))
2363 ast_copy_string(l->vmexten, device_vmexten, sizeof(vmexten));
2365 l->capability = d->capability;
2366 l->prefs = d->prefs;
2368 if (!strcasecmp(v->name, "trunk")) {
2369 l->type = TYPE_TRUNK;
2371 l->type = TYPE_LINE;
2373 l->immediate = immediate;
2374 l->callgroup = cur_callergroup;
2375 l->pickupgroup = cur_pickupgroup;
2376 l->callreturn = callreturn;
2377 l->cancallforward = cancallforward;
2378 l->callwaiting = callwaiting;
2379 l->transfer = transfer;
2380 l->threewaycalling = threewaycalling;
2381 l->mwiblink = mwiblink;
2382 l->onhooktime = time(NULL);
2383 l->instance = lineInstance++;
2384 /* ASSUME we're onhook at this point */
2385 l->hookstate = SKINNY_ONHOOK;
2392 ast_log(LOG_WARNING, "Don't know keyword '%s' at line %d\n", v->name, v->lineno);
2398 ast_log(LOG_ERROR, "A Skinny device must have at least one line!\n");
2401 if (/*d->addr.sin_addr.s_addr && */!ntohs(d->addr.sin_port)) {
2402 d->addr.sin_port = htons(DEFAULT_SKINNY_PORT);
2405 /* I don't think we need this anymore at all, since d->ourip is set in skinny_register now */
2406 if (d->addr.sin_addr.s_addr) {
2407 /* XXX See note above, in 'host' option. */
2408 if (ast_ouraddrfor(&d->addr.sin_addr, &d->ourip)) {
2419 static void start_rtp(struct skinny_subchannel *sub)
2421 struct skinny_line *l = sub->parent;
2422 struct skinny_device *d = l->parent;
2425 ast_mutex_lock(&sub->lock);
2426 /* Allocate the RTP */
2427 sub->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
2429 sub->vrtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
2431 if (sub->rtp && sub->owner) {
2432 sub->owner->fds[0] = ast_rtp_fd(sub->rtp);
2433 sub->owner->fds[1] = ast_rtcp_fd(sub->rtp);
2435 if (hasvideo && sub->vrtp && sub->owner) {
2436 sub->owner->fds[2] = ast_rtp_fd(sub->vrtp);
2437 sub->owner->fds[3] = ast_rtcp_fd(sub->vrtp);
2440 ast_rtp_setnat(sub->rtp, l->nat);
2443 ast_rtp_setnat(sub->vrtp, l->nat);
2445 /* Set Frame packetization */
2447 ast_rtp_codec_setpref(sub->rtp, &l->prefs);
2449 /* Create the RTP connection */
2450 transmit_connect(d->session, sub);
2451 ast_mutex_unlock(&sub->lock);
2454 static void *skinny_newcall(void *data)
2456 struct ast_channel *c = data;
2457 struct skinny_subchannel *sub = c->tech_pvt;
2458 struct skinny_line *l = sub->parent;
2459 struct skinny_device *d = l->parent;
2460 struct skinnysession *s = d->session;
2463 ast_copy_string(l->lastnumberdialed, c->exten, sizeof(l->lastnumberdialed));
2465 l->hidecallerid ? "" : l->cid_num,
2466 l->hidecallerid ? "" : l->cid_name,
2467 c->cid.cid_ani ? NULL : l->cid_num);
2468 ast_setstate(c, AST_STATE_RING);
2469 res = ast_pbx_run(c);
2471 ast_log(LOG_WARNING, "PBX exited non-zero\n");
2472 transmit_tone(s, SKINNY_REORDER);
2477 static void *skinny_ss(void *data)
2479 struct ast_channel *c = data;
2480 struct skinny_subchannel *sub = c->tech_pvt;
2481 struct skinny_line *l = sub->parent;
2482 struct skinny_device *d = l->parent;
2483 struct skinnysession *s = d->session;
2485 int timeout = firstdigittimeout;
2488 int loop_pause = 100;
2490 if (option_verbose > 2)
2491 ast_verbose( VERBOSE_PREFIX_3 "Starting simple switch on '%s@%s'\n", l->name, d->name);
2493 len = strlen(d->exten);
2495 while (len < AST_MAX_EXTENSION-1) {
2496 res = 1; /* Assume that we will get a digit */
2497 while (strlen(d->exten) == len){
2498 ast_safe_sleep(c, loop_pause);
2499 timeout -= loop_pause;
2500 if ( (timeout -= loop_pause) <= 0){
2508 len = strlen(d->exten);
2510 if (!ast_ignore_pattern(c->context, d->exten)) {
2511 transmit_tone(s, SKINNY_SILENCE);
2513 if (ast_exists_extension(c, c->context, d->exten, 1, l->cid_num)) {
2514 if (!res || !ast_matchmore_extension(c, c->context, d->exten, 1, l->cid_num)) {
2516 /* Record this as the forwarding extension */
2517 ast_copy_string(l->call_forward, d->exten, sizeof(l->call_forward));
2518 if (option_verbose > 2)
2519 ast_verbose(VERBOSE_PREFIX_3 "Setting call forward to '%s' on channel %s\n",
2520 l->call_forward, c->name);
2521 transmit_tone(s, SKINNY_DIALTONE);
2525 ast_safe_sleep(c, 500);
2526 ast_indicate(c, -1);
2527 ast_safe_sleep(c, 1000);
2528 memset(d->exten, 0, sizeof(d->exten));
2529 transmit_tone(s, SKINNY_DIALTONE);
2533 ast_copy_string(c->exten, d->exten, sizeof(c->exten));
2534 ast_copy_string(l->lastnumberdialed, d->exten, sizeof(l->lastnumberdialed));
2535 memset(d->exten, 0, sizeof(d->exten));
2540 /* It's a match, but they just typed a digit, and there is an ambiguous match,
2541 so just set the timeout to matchdigittimeout and wait some more */
2542 timeout = matchdigittimeout;
2544 } else if (res == 0) {
2545 ast_debug(1, "Not enough digits (%s) (and no ambiguous match)...\n", d->exten);
2546 memset(d->exten, 0, sizeof(d->exten));
2547 transmit_tone(s, SKINNY_REORDER);
2548 if (sub->owner && sub->owner->_state != AST_STATE_UP) {
2549 ast_indicate(c, -1);
2553 } else if (!ast_canmatch_extension(c, c->context, d->exten, 1, c->cid.cid_num) &&
2554 ((d->exten[0] != '*') || (!ast_strlen_zero(d->exten) > 2))) {
2555 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);
2556 memset(d->exten, 0, sizeof(d->exten));
2557 transmit_tone(s, SKINNY_REORDER);
2558 /* hang out for 3 seconds to let congestion play */
2559 ast_safe_sleep(c, 3000);
2563 timeout = gendigittimeout;
2565 if (len && !ast_ignore_pattern(c->context, d->exten)) {
2566 ast_indicate(c, -1);
2571 memset(d->exten, 0, sizeof(d->exten));
2577 static int skinny_call(struct ast_channel *ast, char *dest, int timeout)
2581 struct skinny_subchannel *sub = ast->tech_pvt;
2582 struct skinny_line *l = sub->parent;
2583 struct skinny_device *d = l->parent;
2584 struct skinnysession *s = d->session;
2586 if (!d->registered) {
2587 ast_log(LOG_ERROR, "Device not registered, cannot call %s\n", dest);
2591 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
2592 ast_log(LOG_WARNING, "skinny_call called on %s, neither down nor reserved\n", ast->name);
2597 ast_verbose(VERBOSE_PREFIX_3 "skinny_call(%s)\n", ast->name);
2600 ast_queue_control(ast, AST_CONTROL_BUSY);
2604 switch (l->hookstate) {
2605 case SKINNY_OFFHOOK:
2606 tone = SKINNY_CALLWAITTONE;
2609 tone = SKINNY_ALERT;
2612 ast_log(LOG_ERROR, "Don't know how to deal with hookstate %d\n", l->hookstate);
2616 transmit_callstate(s, l->instance, SKINNY_RINGIN, sub->callid);
2617 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_RINGIN);
2618 transmit_displaypromptstatus(s, "Ring-In", 0, l->instance, sub->callid);
2619 transmit_callinfo(s, ast->cid.cid_name, ast->cid.cid_num, l->cid_name, l->cid_num, l->instance, sub->callid, 1);
2620 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_BLINK);
2621 transmit_ringer_mode(s, SKINNY_RING_INSIDE);
2623 ast_setstate(ast, AST_STATE_RINGING);
2624 ast_queue_control(ast, AST_CONTROL_RINGING);
2629 static int skinny_hangup(struct ast_channel *ast)
2631 struct skinny_subchannel *sub = ast->tech_pvt;
2632 struct skinny_line *l;
2633 struct skinny_device *d;
2634 struct skinnysession *s;
2637 ast_debug(1, "Asked to hangup channel not connected\n");
2644 ast_verbose("skinny_hangup(%s) on %s@%s\n", ast->name, l->name, d->name);
2646 if (d->registered) {
2647 if ((l->type = TYPE_LINE) && (l->hookstate == SKINNY_OFFHOOK)) {
2648 l->hookstate = SKINNY_ONHOOK;
2649 transmit_callstate(s, l->instance, SKINNY_ONHOOK, sub->callid);
2650 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_OFF);
2651 transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
2652 } else if ((l->type = TYPE_LINE) && (l->hookstate == SKINNY_ONHOOK)) {
2653 transmit_callstate(s, l->instance, SKINNY_ONHOOK, sub->callid);
2654 transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
2655 transmit_ringer_mode(s, SKINNY_RING_OFF);
2656 transmit_tone(s, SKINNY_SILENCE);
2657 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_OFF);
2661 ast_mutex_lock(&sub->lock);
2663 ast->tech_pvt = NULL;
2664 sub->alreadygone = 0;
2667 ast_rtp_destroy(sub->rtp);
2670 ast_mutex_unlock(&sub->lock);
2674 static int skinny_answer(struct ast_channel *ast)
2677 struct skinny_subchannel *sub = ast->tech_pvt;
2678 struct skinny_line *l = sub->parent;
2679 struct skinny_device *d = l->parent;
2680 struct skinnysession *s = d->session;
2682 sub->cxmode = SKINNY_CX_SENDRECV;
2687 ast_verbose("skinny_answer(%s) on %s@%s-%d\n", ast->name, l->name, d->name, sub->callid);
2688 if (ast->_state != AST_STATE_UP) {
2689 ast_setstate(ast, AST_STATE_UP);
2692 transmit_tone(s, SKINNY_SILENCE);
2693 /* order matters here...
2694 for some reason, transmit_callinfo must be before transmit_callstate,
2695 or you won't get keypad messages in some situations. */
2696 transmit_callinfo(s, ast->cid.cid_name, ast->cid.cid_num, ast->exten, ast->exten, l->instance, sub->callid, 2);
2697 transmit_callstate(s, l->instance, SKINNY_CONNECTED, sub->callid);
2698 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_CONNECTED);
2699 transmit_displaypromptstatus(s, "Connected", 0, l->instance, sub->callid);
2703 /* Retrieve audio/etc from channel. Assumes sub->lock is already held. */
2704 static struct ast_frame *skinny_rtp_read(struct skinny_subchannel *sub)
2706 struct ast_channel *ast = sub->owner;
2707 struct ast_frame *f;
2710 /* We have no RTP allocated for this channel */
2711 return &ast_null_frame;
2716 f = ast_rtp_read(sub->rtp); /* RTP Audio */
2719 f = ast_rtcp_read(sub->rtp); /* RTCP Control Channel */
2722 f = ast_rtp_read(sub->vrtp); /* RTP Video */
2725 f = ast_rtcp_read(sub->vrtp); /* RTCP Control Channel for video */
2729 /* Not yet supported */
2730 f = ast_udptl_read(sub->udptl); /* UDPTL for T.38 */
2734 f = &ast_null_frame;
2738 /* We already hold the channel lock */
2739 if (f->frametype == AST_FRAME_VOICE) {
2740 if (f->subclass != ast->nativeformats) {
2741 ast_debug(1, "Oooh, format changed to %d\n", f->subclass);
2742 ast->nativeformats = f->subclass;
2743 ast_set_read_format(ast, ast->readformat);
2744 ast_set_write_format(ast, ast->writeformat);
2751 static struct ast_frame *skinny_read(struct ast_channel *ast)
2753 struct ast_frame *fr;
2754 struct skinny_subchannel *sub = ast->tech_pvt;
2755 ast_mutex_lock(&sub->lock);
2756 fr = skinny_rtp_read(sub);
2757 ast_mutex_unlock(&sub->lock);
2761 static int skinny_write(struct ast_channel *ast, struct ast_frame *frame)
2763 struct skinny_subchannel *sub = ast->tech_pvt;
2765 if (frame->frametype != AST_FRAME_VOICE) {
2766 if (frame->frametype == AST_FRAME_IMAGE) {
2769 ast_log(LOG_WARNING, "Can't send %d type frames with skinny_write\n", frame->frametype);
2773 if (!(frame->subclass & ast->nativeformats)) {
2774 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
2775 frame->subclass, ast->nativeformats, ast->readformat, ast->writeformat);
2780 ast_mutex_lock(&sub->lock);
2782 res = ast_rtp_write(sub->rtp, frame);
2784 ast_mutex_unlock(&sub->lock);
2789 static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
2791 struct skinny_subchannel *sub = newchan->tech_pvt;
2792 ast_log(LOG_NOTICE, "skinny_fixup(%s, %s)\n", oldchan->name, newchan->name);
2793 if (sub->owner != oldchan) {
2794 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, sub->owner);
2797 sub->owner = newchan;
2801 static int skinny_senddigit_begin(struct ast_channel *ast, char digit)
2803 return -1; /* Start inband indications */
2806 static int skinny_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
2809 struct skinny_subchannel *sub = ast->tech_pvt;
2810 struct skinny_line *l = sub->parent;
2811 struct skinny_device *d = l->parent;
2814 sprintf(tmp, "%d", digit);
2815 transmit_tone(d->session, digit);
2817 return -1; /* Stop inband indications */
2820 static char *control2str(int ind) {
2824 case AST_CONTROL_HANGUP:
2825 return "Other end has hungup";
2826 case AST_CONTROL_RING:
2827 return "Local ring";
2828 case AST_CONTROL_RINGING:
2829 return "Remote end is ringing";
2830 case AST_CONTROL_ANSWER:
2831 return "Remote end has answered";
2832 case AST_CONTROL_BUSY:
2833 return "Remote end is busy";
2834 case AST_CONTROL_TAKEOFFHOOK:
2835 return "Make it go off hook";
2836 case AST_CONTROL_OFFHOOK:
2837 return "Line is off hook";
2838 case AST_CONTROL_CONGESTION:
2839 return "Congestion (circuits busy)";
2840 case AST_CONTROL_FLASH:
2841 return "Flash hook";
2842 case AST_CONTROL_WINK:
2844 case AST_CONTROL_OPTION:
2845 return "Set a low-level option";
2846 case AST_CONTROL_RADIO_KEY:
2848 case AST_CONTROL_RADIO_UNKEY:
2849 return "Un-Key Radio";
2850 case AST_CONTROL_PROGRESS:
2851 return "Remote end is making Progress";
2852 case AST_CONTROL_PROCEEDING:
2853 return "Remote end is proceeding";
2854 case AST_CONTROL_HOLD:
2856 case AST_CONTROL_UNHOLD:
2861 if (!(tmp = ast_threadstorage_get(&control2str_threadbuf, CONTROL2STR_BUFSIZE)))
2863 snprintf(tmp, CONTROL2STR_BUFSIZE, "UNKNOWN-%d", ind);
2869 static int skinny_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen)
2871 struct skinny_subchannel *sub = ast->tech_pvt;
2872 struct skinny_line *l = sub->parent;
2873 struct skinny_device *d = l->parent;
2874 struct skinnysession *s = d->session;
2877 ast_verbose(VERBOSE_PREFIX_3 "Asked to indicate '%s' condition on channel %s\n", control2str(ind), ast->name);
2879 case AST_CONTROL_RINGING:
2880 if (ast->_state != AST_STATE_UP) {
2881 if (!sub->progress) {
2882 transmit_tone(s, SKINNY_ALERT);
2883 transmit_callstate(s, l->instance, SKINNY_RINGOUT, sub->callid);
2884 transmit_dialednumber(s, ast->exten, l->instance, sub->callid);
2885 transmit_displaypromptstatus(s, "Ring Out", 0, l->instance, sub->callid);
2886 transmit_callinfo(s, ast->cid.cid_name, ast->cid.cid_num, ast->exten, ast->exten, l->instance, sub->callid, 2); /* 2 = outgoing from phone */
2892 case AST_CONTROL_BUSY:
2893 if (ast->_state != AST_STATE_UP) {
2894 transmit_tone(s, SKINNY_BUSYTONE);
2895 transmit_callstate(s, l->instance, SKINNY_BUSY, sub->callid);
2896 sub->alreadygone = 1;
2897 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
2901 case AST_CONTROL_CONGESTION:
2902 if (ast->_state != AST_STATE_UP) {
2903 transmit_tone(s, SKINNY_REORDER);
2904 transmit_callstate(s, l->instance, SKINNY_CONGESTION, sub->callid);
2905 sub->alreadygone = 1;
2906 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
2910 case AST_CONTROL_PROGRESS:
2911 if ((ast->_state != AST_STATE_UP) && !sub->progress && !sub->outgoing) {
2912 transmit_tone(s, SKINNY_ALERT);
2913 transmit_callstate(s, l->instance, SKINNY_PROGRESS, sub->callid);
2914 transmit_displaypromptstatus(s, "Call Progress", 0, l->instance, sub->callid);
2915 transmit_callinfo(s, ast->cid.cid_name, ast->cid.cid_num, ast->exten, ast->exten, l->instance, sub->callid, 2); /* 2 = outgoing from phone */
2921 transmit_tone(s, SKINNY_SILENCE);
2923 case AST_CONTROL_HOLD:
2924 ast_moh_start(ast, data, l->mohinterpret);
2926 case AST_CONTROL_UNHOLD:
2929 case AST_CONTROL_PROCEEDING:
2932 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", ind);
2938 static struct ast_channel *skinny_new(struct skinny_line *l, int state)
2940 struct ast_channel *tmp;
2941 struct skinny_subchannel *sub;
2942 struct skinny_device *d = l->parent;
2945 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);
2947 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
2950 sub = ast_calloc(1, sizeof(*sub));
2952 ast_log(LOG_WARNING, "Unable to allocate Skinny subchannel\n");
2955 ast_mutex_init(&sub->lock);
2958 sub->callid = callnums++;
2959 d->lastlineinstance = l->instance;
2960 d->lastcallreference = sub->callid;
2961 sub->cxmode = SKINNY_CX_INACTIVE;
2969 tmp->tech = &skinny_tech;
2970 tmp->tech_pvt = sub;
2971 tmp->nativeformats = l->capability;
2972 if (!tmp->nativeformats)
2973 tmp->nativeformats = default_capability;
2974 fmt = ast_best_codec(tmp->nativeformats);
2976 ast_verbose("skinny_new: tmp->nativeformats=%d fmt=%d\n", tmp->nativeformats, fmt);
2978 tmp->fds[0] = ast_rtp_fd(sub->rtp);
2980 if (state == AST_STATE_RING) {
2983 tmp->writeformat = fmt;
2984 tmp->rawwriteformat = fmt;
2985 tmp->readformat = fmt;
2986 tmp->rawreadformat = fmt;
2987 if (!ast_strlen_zero(l->language))
2988 ast_string_field_set(tmp, language, l->language);
2989 if (!ast_strlen_zero(l->accountcode))
2990 ast_string_field_set(tmp, accountcode, l->accountcode);
2992 tmp->amaflags = l->amaflags;
2994 ast_module_ref(ast_module_info->self);
2995 tmp->callgroup = l->callgroup;
2996 tmp->pickupgroup = l->pickupgroup;
2997 ast_string_field_set(tmp, call_forward, l->call_forward);
2998 ast_copy_string(tmp->context, l->context, sizeof(tmp->context));
2999 ast_copy_string(tmp->exten, l->exten, sizeof(tmp->exten));
3001 /* Don't use ast_set_callerid() here because it will
3002 * generate a needless NewCallerID event */
3003 tmp->cid.cid_num = ast_strdup(l->cid_num);
3004 tmp->cid.cid_ani = ast_strdup(l->cid_num);
3005 tmp->cid.cid_name = ast_strdup(l->cid_name);
3008 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
3011 ast_jb_configure(tmp, &global_jbconf);
3013 if (state != AST_STATE_DOWN) {
3014 if (ast_pbx_start(tmp)) {
3015 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
3024 static int skinny_hold(struct skinny_subchannel *sub)
3026 struct skinny_line *l = sub->parent;
3027 struct skinny_device *d = l->parent;
3028 struct skinnysession *s = d->session;
3029 struct skinny_req *req;
3031 /* Channel needs to be put on hold */
3033 ast_verbose("Putting on Hold(%d)\n", l->instance);
3035 ast_queue_control_data(sub->owner, AST_CONTROL_HOLD,
3036 S_OR(l->mohsuggest, NULL),
3037 !ast_strlen_zero(l->mohsuggest) ? strlen(l->mohsuggest) + 1 : 0);
3039 if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
3042 req->data.activatecallplane.lineInstance = htolel(l->instance);
3043 transmit_response(s, req);
3045 if (!(req = req_alloc(sizeof(struct close_receive_channel_message), CLOSE_RECEIVE_CHANNEL_MESSAGE)))
3048 req->data.closereceivechannel.conferenceId = htolel(0);
3049 req->data.closereceivechannel.partyId = htolel(sub->callid);
3050 transmit_response(s, req);
3052 if (!(req = req_alloc(sizeof(struct stop_media_transmission_message), STOP_MEDIA_TRANSMISSION_MESSAGE)))
3055 req->data.stopmedia.conferenceId = htolel(0);
3056 req->data.stopmedia.passThruPartyId = htolel(sub->callid);
3057 transmit_response(s, req);
3059 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_WINK);
3064 static int skinny_unhold(struct skinny_subchannel *sub)
3066 struct skinny_line *l = sub->parent;
3067 struct skinny_device *d = l->parent;
3068 struct skinnysession *s = d->session;
3069 struct skinny_req *req;
3071 /* Channel is on hold, so we will unhold */
3073 ast_verbose("Taking off Hold(%d)\n", l->instance);
3075 ast_queue_control(sub->owner, AST_CONTROL_UNHOLD);
3077 if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
3080 req->data.activatecallplane.lineInstance = htolel(l->instance);
3081 transmit_response(s, req);
3083 transmit_connect(s, sub);
3084 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
3089 static int handle_keep_alive_message(struct skinny_req *req, struct skinnysession *s)
3091 if (!(req = req_alloc(0, KEEP_ALIVE_ACK_MESSAGE)))
3094 transmit_response(s, req);
3099 static int handle_register_message(struct skinny_req *req, struct skinnysession *s)
3104 memcpy(&name, req->data.reg.name, sizeof(name));
3106 res = skinny_register(req, s);
3108 ast_log(LOG_ERROR, "Rejecting Device %s: Device not found\n", name);
3109 if (!(req = req_alloc(sizeof(struct register_rej_message), REGISTER_REJ_MESSAGE)))
3112 snprintf(req->data.regrej.errMsg, sizeof(req->data.regrej.errMsg), "No Authority: %s", name);
3113 transmit_response(s, req);
3116 if (option_verbose > 2)
3117 ast_verbose(VERBOSE_PREFIX_3 "Device '%s' successfully registered\n", name);
3119 if (!(req = req_alloc(sizeof(struct register_ack_message), REGISTER_ACK_MESSAGE)))
3122 req->data.regack.res[0] = '0';
3123 req->data.regack.res[1] = '\0';
3124 req->data.regack.keepAlive = htolel(keep_alive);
3125 ast_copy_string(req->data.regack.dateTemplate, date_format, sizeof(req->data.regack.dateTemplate));
3126 req->data.regack.res2[0] = '0';
3127 req->data.regack.res2[1] = '\0';
3128 req->data.regack.secondaryKeepAlive = htolel(keep_alive);
3129 transmit_response(s, req);
3131 ast_verbose("Requesting capabilities\n");
3133 if (!(req = req_alloc(0, CAPABILITIES_REQ_MESSAGE)))
3136 transmit_response(s, req);
3141 static int handle_ip_port_message(struct skinny_req *req, struct skinnysession *s)
3143 /* no response necessary */
3147 static int handle_keypad_button_message(struct skinny_req *req, struct skinnysession *s)
3149 struct skinny_subchannel *sub = NULL;
3150 struct skinny_line *l;
3151 struct skinny_device *d = s->device;
3152 struct ast_frame f = { 0, };
3158 digit = letohl(req->data.keypad.button);
3159 lineInstance = letohl(req->data.keypad.lineInstance);
3160 callReference = letohl(req->data.keypad.callReference);
3164 } else if (digit == 15) {
3166 } else if (digit >= 0 && digit <= 9) {
3169 /* digit=10-13 (A,B,C,D ?), or
3170 * digit is bad value
3172 * probably should not end up here, but set
3173 * value for backward compatibility, and log
3177 ast_log(LOG_WARNING, "Unsupported digit %d\n", digit);
3184 if (lineInstance && callReference)
3185 sub = find_subchannel_by_instance_reference(d, lineInstance, callReference);
3187 sub = find_subchannel_by_instance_reference(d, d->lastlineinstance, d->lastcallreference);
3194 if (sub->owner->_state == 0) {
3195 f.frametype = AST_FRAME_DTMF_BEGIN;
3196 ast_queue_frame(sub->owner, &f);
3198 /* XXX MUST queue this frame to all lines in threeway call if threeway call is active */
3199 f.frametype = AST_FRAME_DTMF_END;
3200 ast_queue_frame(sub->owner, &f);
3201 /* XXX This seriously needs to be fixed */
3202 if (sub->next && sub->next->owner) {
3203 if (sub->owner->_state == 0) {
3204 f.frametype = AST_FRAME_DTMF_BEGIN;
3205 ast_queue_frame(sub->next->owner, &f);
3207 f.frametype = AST_FRAME_DTMF_END;
3208 ast_queue_frame(sub->next->owner, &f);
3212 ast_verbose("No owner: %s\n", l->name);
3217 static int handle_stimulus_message(struct skinny_req *req, struct skinnysession *s)
3219 struct skinny_device *d = s->device;
3220 struct skinny_line *l;
3221 struct skinny_subchannel *sub;
3222 /*struct skinny_speeddial *sd;*/
3223 struct ast_channel *c;
3230 event = letohl(req->data.stimulus.stimulus);
3231 instance = letohl(req->data.stimulus.stimulusInstance);
3232 unknown1 = letohl(req->data.stimulus.unknown1); /* No clue.. */
3234 ast_verbose("unknown1 in handle_stimulus_message is '%d'\n", unknown1);
3236 sub = find_subchannel_by_instance_reference(d, d->lastlineinstance, d->lastcallreference);
3239 l = find_line_by_instance(d, d->lastlineinstance);
3248 case STIMULUS_REDIAL:
3250 ast_verbose("Received Stimulus: Redial(%d)\n", instance);
3252 c = skinny_new(l, AST_STATE_DOWN);
3254 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
3258 if (l->hookstate == SKINNY_ONHOOK) {
3259 l->hookstate = SKINNY_OFFHOOK;
3260 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
3263 ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
3264 transmit_displaymessage(s, NULL); /* clear display */
3265 transmit_tone(s, SKINNY_DIALTONE);
3267 if (ast_strlen_zero(l->lastnumberdialed)) {
3268 ast_log(LOG_WARNING, "Attempted redial, but no previously dialed number found.\n");
3271 if (!ast_ignore_pattern(c->context, l->lastnumberdialed)) {
3272 transmit_tone(s, SKINNY_SILENCE);
3274 ast_copy_string(c->exten, l->lastnumberdialed, sizeof(c->exten));
3275 if (ast_pthread_create(&t, NULL, skinny_newcall, c)) {
3276 ast_log(LOG_WARNING, "Unable to create new call thread: %s\n", strerror(errno));
3281 case STIMULUS_SPEEDDIAL:
3283 struct skinny_speeddial *sd;
3286 ast_verbose("Received Stimulus: SpeedDial(%d)\n", instance);
3287 if (!(sd = find_speeddial_by_instance(d, instance, 0))) {
3291 if (!sub || !sub->owner)
3292 c = skinny_new(l, AST_STATE_DOWN);
3297 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
3301 if (l->hookstate == SKINNY_ONHOOK) {
3302 l->hookstate = SKINNY_OFFHOOK;
3303 transmit_speaker_mode(s, SKINNY_SPEAKERON);
3304 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
3307 ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
3308 transmit_displaymessage(s, NULL); /* clear display */
3309 transmit_tone(s, SKINNY_DIALTONE);
3311 if (!ast_ignore_pattern(c->context, sd->exten)) {
3312 transmit_tone(s, SKINNY_SILENCE);
3314 if (ast_exists_extension(c, c->context, sd->exten, 1, l->cid_num)) {
3315 ast_copy_string(c->exten, sd->exten, sizeof(c->exten));
3316 ast_copy_string(l->lastnumberdialed, sd->exten, sizeof(l->lastnumberdialed));
3318 if (ast_pthread_create(&t, NULL, skinny_newcall, c)) {
3319 ast_log(LOG_WARNING, "Unable to create new call thread: %s\n", strerror(errno));
3329 ast_verbose("Received Stimulus: Hold(%d)\n", instance);
3340 case STIMULUS_TRANSFER:
3342 ast_verbose("Received Stimulus: Transfer(%d)\n", instance);
3343 transmit_tone(s, SKINNY_DIALTONE);
3344 /* XXX figure out how to transfer */
3346 case STIMULUS_CONFERENCE:
3348 ast_verbose("Received Stimulus: Conference(%d)\n", instance);
3349 transmit_tone(s, SKINNY_DIALTONE);
3350 /* XXX determine the best way to pull off a conference. Meetme? */
3352 case STIMULUS_VOICEMAIL:
3354 ast_verbose("Received Stimulus: Voicemail(%d)\n", instance);
3356 if (!sub || !sub->owner) {
3357 c = skinny_new(l, AST_STATE_DOWN);
3362 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
3367 if (ast_strlen_zero(l->vmexten)) /* Exit the call if no VM pilot */
3370 if (l->hookstate == SKINNY_ONHOOK){
3371 l->hookstate = SKINNY_OFFHOOK;
3372 transmit_speaker_mode(s, SKINNY_SPEAKERON);
3373 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
3377 ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
3379 transmit_displaymessage(s, NULL); /* clear display */
3380 transmit_tone(s, SKINNY_DIALTONE);
3382 if (!ast_ignore_pattern(c->context, vmexten)) {
3383 transmit_tone(s, SKINNY_SILENCE);
3386 if (ast_exists_extension(c, c->context, l->vmexten, 1, l->cid_num)) {
3387 ast_copy_string(c->exten, l->vmexten, sizeof(c->exten));
3388 ast_copy_string(l->lastnumberdialed, l->vmexten, sizeof(l->lastnumberdialed));
3389 if (ast_pthread_create(&t, NULL, skinny_newcall, c)) {
3390 ast_log(LOG_WARNING, "Unable to create new call thread: %s\n", strerror(errno));
3397 case STIMULUS_CALLPARK:
3399 ast_verbose("Received Stimulus: Park Call(%d)\n", instance);
3400 /* XXX Park the call */
3402 case STIMULUS_FORWARDALL:
3404 ast_verbose("Received Stimulus: Forward All(%d)\n", instance);
3405 /* Why is DND under FORWARDALL? */
3406 /* Because it's the same thing. */
3408 /* Do not disturb */
3409 transmit_tone(s, SKINNY_DIALTONE);
3411 if (option_verbose > 2)
3412 ast_verbose(VERBOSE_PREFIX_3 "Disabling DND on %s@%s\n", l->name, d->name);
3414 transmit_lamp_indication(s, STIMULUS_FORWARDALL, 1, SKINNY_LAMP_ON);
3415 transmit_displaynotify(s, "DnD disabled", 10);
3417 if (option_verbose > 2)
3418 ast_verbose(VERBOSE_PREFIX_3 "Enabling DND on %s@%s\n", l->name, d->name);
3420 transmit_lamp_indication(s, STIMULUS_FORWARDALL, 1, SKINNY_LAMP_OFF);
3421 transmit_displaynotify(s, "DnD enabled", 10);
3424 case STIMULUS_FORWARDBUSY:
3426 ast_verbose("Received Stimulus: Forward Busy (%d)\n", instance);
3428 case STIMULUS_FORWARDNOANSWER:
3430 ast_verbose("Received Stimulus: Forward No Answer (%d)\n", instance);
3432 case STIMULUS_DISPLAY:
3433 /* Not sure what this is */
3435 ast_verbose("Received Stimulus: Display(%d)\n", instance);
3439 ast_verbose("Received Stimulus: Line(%d)\n", instance);
3441 l = find_line_by_instance(d, instance);
3447 /* turn the speaker on */
3448 transmit_speaker_mode(s, SKINNY_SPEAKERON);
3449 transmit_ringer_mode(s, SKINNY_RING_OFF);
3450 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
3452 l->hookstate = SKINNY_OFFHOOK;
3454 ast_device_state_changed("Skinny/%s@%s", l->name, d->name);
3456 if (sub && sub->outgoing) {
3457 /* We're answering a ringing call */
3458 ast_queue_control(sub->owner, AST_CONTROL_ANSWER);
3459 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
3460 transmit_tone(s, SKINNY_SILENCE);
3461 transmit_callstate(s, l->instance, SKINNY_CONNECTED, sub->callid);
3462 transmit_displaypromptstatus(s, "Connected", 0, l->instance, sub->callid);
3463 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_CONNECTED);
3465 ast_setstate(sub->owner, AST_STATE_UP);
3467 if (sub && sub->owner) {
3468 ast_debug(1, "Current subchannel [%s] already has owner\n", sub->owner->name);
3470 c = skinny_new(l, AST_STATE_DOWN);
3473 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
3475 ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
3476 transmit_displaymessage(s, NULL); /* clear display */
3477 transmit_tone(s, SKINNY_DIALTONE);
3478 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_OFFHOOK);
3480 /* start the switch thread */
3481 if (ast_pthread_create(&t, NULL, skinny_ss, c)) {
3482 ast_log(LOG_WARNING, "Unable to create switch thread: %s\n", strerror(errno));
3486 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
3493 ast_verbose("RECEIVED UNKNOWN STIMULUS: %d(%d)\n", event, instance);
3496 ast_device_state_changed("Skinny/%s@%s", l->name, d->name);
3501 static int handle_offhook_message(struct skinny_req *req, struct skinnysession *s)
3503 struct skinny_device *d = s->device;
3504 struct skinny_line *l;
3505 struct skinny_subchannel *sub;
3506 struct ast_channel *c;
3511 unknown1 = letohl(req->data.offhook.unknown1);
3512 unknown2 = letohl(req->data.offhook.unknown2);
3514 sub = find_subchannel_by_instance_reference(d, d->lastlineinstance, d->lastcallreference);
3517 l = find_line_by_instance(d, d->lastlineinstance);
3525 transmit_ringer_mode(s, SKINNY_RING_OFF);
3526 l->hookstate = SKINNY_OFFHOOK;
3528 ast_device_state_changed("Skinny/%s@%s", l->name, d->name);
3530 if (sub && sub->onhold) {
3534 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
3536 if (sub && sub->outgoing) {
3537 /* We're answering a ringing call */
3538 ast_queue_control(sub->owner, AST_CONTROL_ANSWER);
3539 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
3540 transmit_tone(s, SKINNY_SILENCE);
3541 transmit_callstate(s, l->instance, SKINNY_CONNECTED, sub->callid);
3542 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_CONNECTED);
3544 ast_setstate(sub->owner, AST_STATE_UP);
3546 if (sub && sub->owner) {
3547 ast_debug(1, "Current sub [%s] already has owner\n", sub->owner->name);
3549 c = skinny_new(l, AST_STATE_DOWN);
3552 transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
3554 ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
3555 transmit_displaymessage(s, NULL); /* clear display */
3556 transmit_tone(s, SKINNY_DIALTONE);
3557 transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_OFFHOOK);
3559 /* start the switch thread */
3560 if (ast_pthread_create(&t, NULL, skinny_ss, c)) {
3561 ast_log(LOG_WARNING, "Unable to create switch thread: %s\n", strerror(errno));
3565 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
3572 static int handle_onhook_message(struct skinny_req *req, struct skinnysession *s)
3574 struct skinny_device *d = s->device;
3575 struct skinny_line *l;
3576 struct skinny_subchannel *sub;
3580 unknown1 = letohl(req->data.onhook.unknown1);
3581 unknown2 = letohl(req->data.onhook.unknown2);
3583 sub = find_subchannel_by_instance_reference(d, d->lastlineinstance, d->lastcallreference);
3590 if (l->hookstate == SKINNY_ONHOOK) {
3591 /* Something else already put us back on hook */
3594 l->hookstate = SKINNY_ONHOOK;
3596 ast_device_state_changed("Skinny/%s@%s", l->name, d->name);
3602 sub->cxmode = SKINNY_CX_RECVONLY;
3603 transmit_callstate(s, l->instance, l->hookstate, sub->callid);
3605 ast_verbose("Skinny %s@%s went on hook\n", l->name, d->name);
3606 if (l->transfer && (sub->owner && sub->next && sub->next->owner) && ((!sub->outgoing) || (sub->next && !sub->next->outgoing))) {
3607 /* We're allowed to transfer, we have two active calls and
3608 we made at least one of the calls. Let's try and transfer */
3611 if ((res = attempt_transfer(p)) < 0) {
3612 if (sub->next && sub->next->owner) {
3613 sub->next->alreadygone = 1;
3614 ast_queue_hangup(sub->next->owner,1);
3617 ast_log(LOG_WARNING, "Transfer attempt failed\n");
3622 /* Hangup the current call */
3623 /* If there is another active call, skinny_hangup will ring the phone with the other call */
3625 sub->alreadygone = 1;
3626 ast_queue_hangup(sub->owner);
3628 ast_log(LOG_WARNING, "Skinny(%s@%s-%d) channel already destroyed\n",
3629 l->name, d->name, sub->callid);
3632 if ((l->hookstate == SKINNY_ONHOOK) && (sub->next && !sub->next->rtp)) {
3638 static int handle_capabilities_res_message(struct skinny_req *req, struct skinnysession *s)
3640 struct skinny_device *d = s->device;
3641 struct skinny_line *l;