Asterisk - The Open Source Telephony Project  18.5.0
chan_skinny.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * chan_skinny was developed by Jeremy McNamara & Florian Overkamp
7  * chan_skinny was heavily modified/fixed by North Antara
8  *
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.
14  *
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.
18  */
19 
20 /*! \file
21  *
22  * \brief Implementation of the Skinny protocol
23  *
24  * \author Jeremy McNamara & Florian Overkamp & North Antara
25  * \ingroup channel_drivers
26  */
27 
28 /*! \li \ref chan_skinny.c uses the configuration file \ref skinny.conf
29  * \addtogroup configuration_file
30  */
31 
32 /*! \page skinny.conf skinny.conf
33  * \verbinclude skinny.conf.sample
34  */
35 
36 /*** MODULEINFO
37  <support_level>extended</support_level>
38  ***/
39 
40 #include "asterisk.h"
41 
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <netinet/tcp.h>
45 #include <sys/ioctl.h>
46 #include <net/if.h>
47 #include <fcntl.h>
48 #include <netdb.h>
49 #include <arpa/inet.h>
50 #include <signal.h>
51 #include <ctype.h>
52 
53 #include "asterisk/lock.h"
54 #include "asterisk/channel.h"
55 #include "asterisk/config.h"
56 #include "asterisk/module.h"
57 #include "asterisk/pbx.h"
58 #include "asterisk/sched.h"
59 #include "asterisk/io.h"
60 #include "asterisk/rtp_engine.h"
61 #include "asterisk/netsock2.h"
62 #include "asterisk/acl.h"
63 #include "asterisk/callerid.h"
64 #include "asterisk/cli.h"
65 #include "asterisk/manager.h"
66 #include "asterisk/say.h"
67 #include "asterisk/astdb.h"
68 #include "asterisk/causes.h"
69 #include "asterisk/pickup.h"
70 #include "asterisk/app.h"
71 #include "asterisk/mwi.h"
72 #include "asterisk/musiconhold.h"
73 #include "asterisk/utils.h"
74 #include "asterisk/dsp.h"
75 #include "asterisk/stringfields.h"
76 #include "asterisk/abstract_jb.h"
77 #include "asterisk/threadstorage.h"
78 #include "asterisk/devicestate.h"
79 #include "asterisk/indications.h"
80 #include "asterisk/linkedlists.h"
82 #include "asterisk/bridge.h"
83 #include "asterisk/parking.h"
85 #include "asterisk/format_cache.h"
86 
87 /*** DOCUMENTATION
88  <manager name="SKINNYdevices" language="en_US">
89  <synopsis>
90  List SKINNY devices (text format).
91  </synopsis>
92  <syntax>
93  <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
94  </syntax>
95  <description>
96  <para>Lists Skinny devices in text format with details on current status.
97  Devicelist will follow as separate events, followed by a final event called
98  DevicelistComplete.</para>
99  </description>
100  </manager>
101  <manager name="SKINNYshowdevice" language="en_US">
102  <synopsis>
103  Show SKINNY device (text format).
104  </synopsis>
105  <syntax>
106  <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
107  <parameter name="Device" required="true">
108  <para>The device name you want to check.</para>
109  </parameter>
110  </syntax>
111  <description>
112  <para>Show one SKINNY device with details on current status.</para>
113  </description>
114  </manager>
115  <manager name="SKINNYlines" language="en_US">
116  <synopsis>
117  List SKINNY lines (text format).
118  </synopsis>
119  <syntax>
120  <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
121  </syntax>
122  <description>
123  <para>Lists Skinny lines in text format with details on current status.
124  Linelist will follow as separate events, followed by a final event called
125  LinelistComplete.</para>
126  </description>
127  </manager>
128  <manager name="SKINNYshowline" language="en_US">
129  <synopsis>
130  Show SKINNY line (text format).
131  </synopsis>
132  <syntax>
133  <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
134  <parameter name="Line" required="true">
135  <para>The line name you want to check.</para>
136  </parameter>
137  </syntax>
138  <description>
139  <para>Show one SKINNY line with details on current status.</para>
140  </description>
141  </manager>
142  ***/
143 
144 /* Skinny debugging only available if asterisk configured with --enable-dev-mode */
145 #ifdef AST_DEVMODE
146 static int skinnydebug = 0;
147 char dbgcli_buf[256];
148 #define DEBUG_GENERAL (1 << 1)
149 #define DEBUG_SUB (1 << 2)
150 #define DEBUG_PACKET (1 << 3)
151 #define DEBUG_AUDIO (1 << 4)
152 #define DEBUG_LOCK (1 << 5)
153 #define DEBUG_TEMPLATE (1 << 6)
154 #define DEBUG_THREAD (1 << 7)
155 #define DEBUG_HINT (1 << 8)
156 #define DEBUG_KEEPALIVE (1 << 9)
157 #define SKINNY_DEBUG(type, verb_level, text, ...) \
158  do{ \
159  if (skinnydebug & (type)) { \
160  ast_verb(verb_level, "[%d] " text, ast_get_tid(), ##__VA_ARGS__); \
161  } \
162  }while(0)
163 #else
164 #define SKINNY_DEBUG(type, verb_level, text, ...)
165 #endif
166 
167 /*************************************
168  * Skinny/Asterisk Protocol Settings *
169  *************************************/
170 static const char tdesc[] = "Skinny Client Control Protocol (Skinny)";
171 static const char config[] = "skinny.conf";
172 
174 
181  SKINNY_CODEC_G726_32 = 82, /* XXX Which packing order does this translate to? */
184 };
185 
186 #define DEFAULT_SKINNY_PORT 2000
187 #define DEFAULT_SKINNY_BACKLOG 2
188 #define SKINNY_MAX_PACKET 2000
189 #define DEFAULT_AUTH_TIMEOUT 30
190 #define DEFAULT_AUTH_LIMIT 50
191 
192 static struct {
193  unsigned int tos;
194  unsigned int tos_audio;
195  unsigned int tos_video;
196  unsigned int cos;
197  unsigned int cos_audio;
198  unsigned int cos_video;
199 } qos = { 0, 0, 0, 0, 0, 0 };
200 
201 static int keep_alive = 120;
204 static int unauth_sessions = 0;
205 static char immed_dialchar;
206 static char vmexten[AST_MAX_EXTENSION]; /* Voicemail pilot number */
207 static char used_context[AST_MAX_EXTENSION]; /* placeholder to check if context are already used in regcontext */
208 static char regcontext[AST_MAX_CONTEXT]; /* Context for auto-extension */
209 static char date_format[6] = "D-M-Y";
210 static char version_id[16] = "P002F202";
211 
212 #if __BYTE_ORDER == __LITTLE_ENDIAN
213 #define letohl(x) (x)
214 #define letohs(x) (x)
215 #define htolel(x) (x)
216 #define htoles(x) (x)
217 #else
218 #if defined(HAVE_BYTESWAP_H)
219 #include <byteswap.h>
220 #define letohl(x) bswap_32(x)
221 #define letohs(x) bswap_16(x)
222 #define htolel(x) bswap_32(x)
223 #define htoles(x) bswap_16(x)
224 #elif defined(HAVE_SYS_ENDIAN_SWAP16)
225 #include <sys/endian.h>
226 #define letohl(x) __swap32(x)
227 #define letohs(x) __swap16(x)
228 #define htolel(x) __swap32(x)
229 #define htoles(x) __swap16(x)
230 #elif defined(HAVE_SYS_ENDIAN_BSWAP16)
231 #include <sys/endian.h>
232 #define letohl(x) bswap32(x)
233 #define letohs(x) bswap16(x)
234 #define htolel(x) bswap32(x)
235 #define htoles(x) bswap16(x)
236 #else
237 #define __bswap_16(x) \
238  ((((x) & 0xff00) >> 8) | \
239  (((x) & 0x00ff) << 8))
240 #define __bswap_32(x) \
241  ((((x) & 0xff000000) >> 24) | \
242  (((x) & 0x00ff0000) >> 8) | \
243  (((x) & 0x0000ff00) << 8) | \
244  (((x) & 0x000000ff) << 24))
245 #define letohl(x) __bswap_32(x)
246 #define letohs(x) __bswap_16(x)
247 #define htolel(x) __bswap_32(x)
248 #define htoles(x) __bswap_16(x)
249 #endif
250 #endif
251 
252 /*! Global jitterbuffer configuration - by default, jb is disabled
253  * \note Values shown here match the defaults shown in skinny.conf.sample */
255 {
256  .flags = 0,
257  .max_size = 200,
258  .resync_threshold = 1000,
259  .impl = "fixed",
260  .target_extra = 40,
261 };
263 
264 #ifdef AST_DEVMODE
266 #define MESSAGE2STR_BUFSIZE 35
267 #endif
268 
270 #define DEVICE2STR_BUFSIZE 15
271 
273 #define CONTROL2STR_BUFSIZE 100
274 
276 #define SUBSTATE2STR_BUFSIZE 15
277 
279 #define CALLSTATE2STR_BUFSIZE 15
280 
281 /*********************
282  * Protocol Messages *
283  *********************/
284 /* message types */
285 #define KEEP_ALIVE_MESSAGE 0x0000
286 /* no additional struct */
287 
288 #define REGISTER_MESSAGE 0x0001
290  char name[16];
291  uint32_t userId;
292  uint32_t instance;
293  uint32_t ip;
294  uint32_t type;
295  uint32_t maxStreams;
296  uint32_t space;
298  /*! \brief space2 is used for newer version of skinny */
299  char space2[3];
300 };
301 
302 #define IP_PORT_MESSAGE 0x0002
303 
304 #define KEYPAD_BUTTON_MESSAGE 0x0003
306  uint32_t button;
307  uint32_t lineInstance;
308  uint32_t callReference;
309 };
310 
311 
312 #define ENBLOC_CALL_MESSAGE 0x0004
314  char calledParty[24];
315 };
316 
317 #define STIMULUS_MESSAGE 0x0005
319  uint32_t stimulus;
321  uint32_t callreference;
322 };
323 
324 #define OFFHOOK_MESSAGE 0x0006
326  uint32_t instance;
327  uint32_t reference;
328 };
329 
330 #define ONHOOK_MESSAGE 0x0007
332  uint32_t instance;
333  uint32_t reference;
334 };
335 
336 #define CAPABILITIES_RES_MESSAGE 0x0010
338  uint32_t codec; /* skinny codec, not ast codec */
339  uint32_t frames;
340  union {
341  char res[8];
342  uint32_t rate;
343  } payloads;
344 };
345 
346 #define SKINNY_MAX_CAPABILITIES 18
347 
349  uint32_t count;
351 };
352 
353 #define SPEED_DIAL_STAT_REQ_MESSAGE 0x000A
355  uint32_t speedDialNumber;
356 };
357 
358 #define LINE_STATE_REQ_MESSAGE 0x000B
360  uint32_t lineNumber;
361 };
362 
363 #define TIME_DATE_REQ_MESSAGE 0x000D
364 #define BUTTON_TEMPLATE_REQ_MESSAGE 0x000E
365 #define VERSION_REQ_MESSAGE 0x000F
366 #define SERVER_REQUEST_MESSAGE 0x0012
367 
368 #define ALARM_MESSAGE 0x0020
370  uint32_t alarmSeverity;
371  char displayMessage[80];
372  uint32_t alarmParam1;
373  uint32_t alarmParam2;
374 };
375 
376 #define OPEN_RECEIVE_CHANNEL_ACK_MESSAGE 0x0022
378  uint32_t status;
379  uint32_t ipAddr;
380  uint32_t port;
381  uint32_t callReference;
382 };
384  uint32_t status;
385  uint32_t space;
386  char ipAddr[16];
387  uint32_t port;
388  uint32_t callReference;
389 };
390 
391 #define SOFT_KEY_SET_REQ_MESSAGE 0x0025
392 
393 #define SOFT_KEY_EVENT_MESSAGE 0x0026
395  uint32_t softKeyEvent;
396  uint32_t instance;
397  uint32_t callreference;
398 };
399 
400 #define UNREGISTER_MESSAGE 0x0027
401 #define SOFT_KEY_TEMPLATE_REQ_MESSAGE 0x0028
402 #define HEADSET_STATUS_MESSAGE 0x002B
403 #define REGISTER_AVAILABLE_LINES_MESSAGE 0x002D
404 
405 #define SERVICEURL_STATREQ_MESSAGE 0x0033
407  uint32_t instance;
408 };
409 
410 #define REGISTER_ACK_MESSAGE 0x0081
412  uint32_t keepAlive;
413  char dateTemplate[6];
414  char res[2];
416  char res2[4];
417 };
418 
419 #define START_TONE_MESSAGE 0x0082
421  uint32_t tone;
422  uint32_t space;
423  uint32_t instance;
424  uint32_t reference;
425 };
426 
427 #define STOP_TONE_MESSAGE 0x0083
429  uint32_t instance;
430  uint32_t reference;
431  uint32_t space;
432 };
433 
434 #define SET_RINGER_MESSAGE 0x0085
436  uint32_t ringerMode;
437  uint32_t unknown1; /* See notes in transmit_ringer_mode */
438  uint32_t unknown2;
439  uint32_t space[2];
440 };
441 
442 #define SET_LAMP_MESSAGE 0x0086
444  uint32_t stimulus;
446  uint32_t deviceStimulus;
447 };
448 
449 #define SET_SPEAKER_MESSAGE 0x0088
451  uint32_t mode;
452 };
453 
454 /* XXX When do we need to use this? */
455 #define SET_MICROPHONE_MESSAGE 0x0089
457  uint32_t mode;
458 };
459 
460 #define START_MEDIA_TRANSMISSION_MESSAGE 0x008A
462  uint32_t precedence;
463  uint32_t vad;
464  uint32_t packets;
465  uint32_t bitRate;
466 };
467 
469  uint32_t conferenceId;
470  uint32_t passThruPartyId;
471  uint32_t remoteIp;
472  uint32_t remotePort;
473  uint32_t packetSize;
474  uint32_t payloadType;
475  struct media_qualifier qualifier;
476  uint32_t space[19];
477 };
478 
480  uint32_t conferenceId;
481  uint32_t passThruPartyId;
482  uint32_t space;
483  char remoteIp[16];
484  uint32_t remotePort;
485  uint32_t packetSize;
486  uint32_t payloadType;
487  struct media_qualifier qualifier;
488  /*! \brief space2 is used for newer version of skinny */
489  uint32_t space2[19];
490 };
491 
492 #define STOP_MEDIA_TRANSMISSION_MESSAGE 0x008B
494  uint32_t conferenceId;
495  uint32_t passThruPartyId;
496  uint32_t space[3];
497 };
498 
499 #define CALL_INFO_MESSAGE 0x008F
501  char callingPartyName[40];
502  char callingParty[24];
503  char calledPartyName[40];
504  char calledParty[24];
505  uint32_t instance;
506  uint32_t reference;
507  uint32_t type;
508  char originalCalledPartyName[40];
509  char originalCalledParty[24];
510  char lastRedirectingPartyName[40];
511  char lastRedirectingParty[24];
514  char callingPartyVoiceMailbox[24];
515  char calledPartyVoiceMailbox[24];
516  char originalCalledPartyVoiceMailbox[24];
517  char lastRedirectingVoiceMailbox[24];
518  uint32_t space[3];
519 };
520 
521 #define FORWARD_STAT_MESSAGE 0x0090
523  uint32_t activeforward;
524  uint32_t lineNumber;
525  uint32_t fwdall;
526  char fwdallnum[24];
527  uint32_t fwdbusy;
528  char fwdbusynum[24];
529  uint32_t fwdnoanswer;
530  char fwdnoanswernum[24];
531 };
532 
533 #define SPEED_DIAL_STAT_RES_MESSAGE 0x0091
535  uint32_t speedDialNumber;
536  char speedDialDirNumber[24];
537  char speedDialDisplayName[40];
538 };
539 
540 #define LINE_STAT_RES_MESSAGE 0x0092
542  uint32_t lineNumber;
543  char lineDirNumber[24];
544  char lineDisplayName[24];
545  uint32_t space[15];
546 };
547 
548 #define DEFINETIMEDATE_MESSAGE 0x0094
550  uint32_t year; /* since 1900 */
551  uint32_t month;
552  uint32_t dayofweek; /* monday = 1 */
553  uint32_t day;
554  uint32_t hour;
555  uint32_t minute;
556  uint32_t seconds;
557  uint32_t milliseconds;
558  uint32_t timestamp;
559 };
560 
561 #define BUTTON_TEMPLATE_RES_MESSAGE 0x0097
563  uint8_t instanceNumber;
565 };
566 
569  /* for now, anything between 0xB0 and 0xCF is custom */
570  /*int custom;*/
571 };
572 
573 #define STIMULUS_REDIAL 0x01
574 #define STIMULUS_SPEEDDIAL 0x02
575 #define STIMULUS_HOLD 0x03
576 #define STIMULUS_TRANSFER 0x04
577 #define STIMULUS_FORWARDALL 0x05
578 #define STIMULUS_FORWARDBUSY 0x06
579 #define STIMULUS_FORWARDNOANSWER 0x07
580 #define STIMULUS_DISPLAY 0x08
581 #define STIMULUS_LINE 0x09
582 #define STIMULUS_VOICEMAIL 0x0F
583 #define STIMULUS_AUTOANSWER 0x11
584 #define STIMULUS_SERVICEURL 0x14
585 #define STIMULUS_DND 0x3F
586 #define STIMULUS_CONFERENCE 0x7D
587 #define STIMULUS_CALLPARK 0x7E
588 #define STIMULUS_CALLPICKUP 0x7F
589 #define STIMULUS_NONE 0xFF
590 
591 /* Button types */
592 #define BT_REDIAL STIMULUS_REDIAL
593 #define BT_SPEEDDIAL STIMULUS_SPEEDDIAL
594 #define BT_HOLD STIMULUS_HOLD
595 #define BT_TRANSFER STIMULUS_TRANSFER
596 #define BT_FORWARDALL STIMULUS_FORWARDALL
597 #define BT_FORWARDBUSY STIMULUS_FORWARDBUSY
598 #define BT_FORWARDNOANSWER STIMULUS_FORWARDNOANSWER
599 #define BT_DISPLAY STIMULUS_DISPLAY
600 #define BT_LINE STIMULUS_LINE
601 #define BT_VOICEMAIL STIMULUS_VOICEMAIL
602 #define BT_AUTOANSWER STIMULUS_AUTOANSWER
603 #define BT_SERVICEURL STIMULUS_SERVICEURL
604 #define BT_DND STIMULUS_DND
605 #define BT_CONFERENCE STIMULUS_CONFERENCE
606 #define BT_CALLPARK STIMULUS_CALLPARK
607 #define BT_CALLPICKUP STIMULUS_CALLPICKUP
608 #define BT_NONE 0x00
609 
610 /* Custom button types - add our own between 0xB0 and 0xCF.
611  This may need to be revised in the future,
612  if stimuluses are ever added in this range. */
613 #define BT_CUST_LINESPEEDDIAL 0xB0 /* line or speeddial with/without hint */
614 #define BT_CUST_LINE 0xB1 /* line or speeddial with hint only */
615 
617  uint32_t buttonOffset;
618  uint32_t buttonCount;
620  struct button_definition definition[42];
621 };
622 
623 #define VERSION_RES_MESSAGE 0x0098
625  char version[16];
626 };
627 
628 #define DISPLAYTEXT_MESSAGE 0x0099
630  char text[40];
631 };
632 
633 #define CLEAR_NOTIFY_MESSAGE 0x0115
634 #define CLEAR_DISPLAY_MESSAGE 0x009A
636  uint32_t space;
637 };
638 
639 #define CAPABILITIES_REQ_MESSAGE 0x009B
640 
641 #define REGISTER_REJ_MESSAGE 0x009D
643  char errMsg[33];
644 };
645 
646 #define SERVER_RES_MESSAGE 0x009E
648  char serverName[48];
649 };
650 
652  struct server_identifier server[5];
653  uint32_t serverListenPort[5];
654  uint32_t serverIpAddr[5];
655 };
656 
657 #define RESET_MESSAGE 0x009F
659  uint32_t resetType;
660 };
661 
662 #define KEEP_ALIVE_ACK_MESSAGE 0x0100
663 
664 #define OPEN_RECEIVE_CHANNEL_MESSAGE 0x0105
666  uint32_t conferenceId;
667  uint32_t partyId;
668  uint32_t packets;
669  uint32_t capability;
670  uint32_t echo;
671  uint32_t bitrate;
672  uint32_t space[36];
673 };
674 
675 #define CLOSE_RECEIVE_CHANNEL_MESSAGE 0x0106
677  uint32_t conferenceId;
678  uint32_t partyId;
679  uint32_t space[2];
680 };
681 
682 #define SOFT_KEY_TEMPLATE_RES_MESSAGE 0x0108
684  char softKeyLabel[16];
685  uint32_t softKeyEvent;
686 };
687 
688 #define BKSP_REQ_MESSAGE 0x0119
690  uint32_t instance;
691  uint32_t callreference;
692 };
693 
694 #define KEYDEF_ONHOOK 0
695 #define KEYDEF_CONNECTED 1
696 #define KEYDEF_ONHOLD 2
697 #define KEYDEF_RINGIN 3
698 #define KEYDEF_OFFHOOK 4
699 #define KEYDEF_CONNWITHTRANS 5
700 #define KEYDEF_DADFD 6 /* Digits After Dialing First Digit */
701 #define KEYDEF_CONNWITHCONF 7
702 #define KEYDEF_RINGOUT 8
703 #define KEYDEF_OFFHOOKWITHFEAT 9
704 #define KEYDEF_UNKNOWN 10
705 #define KEYDEF_SLAHOLD 11
706 #define KEYDEF_SLACONNECTEDNOTACTIVE 12
707 #define KEYDEF_RINGOUTWITHTRANS 13
708 
709 #define SOFTKEY_NONE 0x00
710 #define SOFTKEY_REDIAL 0x01
711 #define SOFTKEY_NEWCALL 0x02
712 #define SOFTKEY_HOLD 0x03
713 #define SOFTKEY_TRNSFER 0x04
714 #define SOFTKEY_CFWDALL 0x05
715 #define SOFTKEY_CFWDBUSY 0x06
716 #define SOFTKEY_CFWDNOANSWER 0x07
717 #define SOFTKEY_BKSPC 0x08
718 #define SOFTKEY_ENDCALL 0x09
719 #define SOFTKEY_RESUME 0x0A
720 #define SOFTKEY_ANSWER 0x0B
721 #define SOFTKEY_INFO 0x0C
722 #define SOFTKEY_CONFRN 0x0D
723 #define SOFTKEY_PARK 0x0E
724 #define SOFTKEY_JOIN 0x0F
725 #define SOFTKEY_MEETME 0x10
726 #define SOFTKEY_PICKUP 0x11
727 #define SOFTKEY_GPICKUP 0x12
728 #define SOFTKEY_DND 0x13
729 #define SOFTKEY_IDIVERT 0x14
730 #define SOFTKEY_FORCEDIAL 0x15
731 
732 #define KEYMASK_ALL 0xFFFFFFFF
733 #define KEYMASK_NONE (1 << 0)
734 #define KEYMASK_REDIAL (1 << 1)
735 #define KEYMASK_NEWCALL (1 << 2)
736 #define KEYMASK_HOLD (1 << 3)
737 #define KEYMASK_TRNSFER (1 << 4)
738 #define KEYMASK_CFWDALL (1 << 5)
739 #define KEYMASK_CFWDBUSY (1 << 6)
740 #define KEYMASK_CFWDNOANSWER (1 << 7)
741 #define KEYMASK_BKSPC (1 << 8)
742 #define KEYMASK_ENDCALL (1 << 9)
743 #define KEYMASK_RESUME (1 << 10)
744 #define KEYMASK_ANSWER (1 << 11)
745 #define KEYMASK_INFO (1 << 12)
746 #define KEYMASK_CONFRN (1 << 13)
747 #define KEYMASK_PARK (1 << 14)
748 #define KEYMASK_JOIN (1 << 15)
749 #define KEYMASK_MEETME (1 << 16)
750 #define KEYMASK_PICKUP (1 << 17)
751 #define KEYMASK_GPICKUP (1 << 18)
752 #define KEYMASK_DND (1 << 29)
753 #define KEYMASK_IDIVERT (1 << 20)
754 #define KEYMASK_FORCEDIAL (1 << 21)
755 
756 /* Localized message "codes" (in octal)
757  Below is en_US (taken from a 7970) */
758 
759 /* "\200\000" ??? */
760 #define OCTAL_REDIAL "\200\001" /* Redial */
761 #define OCTAL_NEWCALL "\200\002" /* New Call */
762 #define OCTAL_HOLD "\200\003" /* Hold */
763 #define OCTAL_TRANSFER "\200\004" /* Transfer */
764 #define OCTAL_CFWDALL "\200\005" /* CFwdALL */
765 #define OCTAL_CFWDBUSY "\200\006" /* CFwdBusy */
766 #define OCTAL_CFWDNOAN "\200\007" /* CFwdNoAnswer */
767 #define OCTAL_BKSPC "\200\010" /* << */
768 #define OCTAL_ENDCALL "\200\011" /* EndCall */
769 #define OCTAL_RESUME "\200\012" /* Resume */
770 #define OCTAL_ANSWER "\200\013" /* Answer */
771 #define OCTAL_INFO "\200\014" /* Info */
772 #define OCTAL_CONFRN "\200\015" /* Confrn */
773 #define OCTAL_PARK "\200\016" /* Park */
774 #define OCTAL_JOIN "\200\017" /* Join */
775 #define OCTAL_MEETME "\200\020" /* MeetMe */
776 #define OCTAL_PICKUP "\200\021" /* PickUp */
777 #define OCTAL_GPICKUP "\200\022" /* GPickUp */
778 #define OCTAL_CUROPTS "\200\023" /* Your current options */
779 #define OCTAL_OFFHOOK "\200\024" /* Off Hook */
780 #define OCTAL_ONHOOK "\200\025" /* On Hook */
781 #define OCTAL_RINGOUT "\200\026" /* Ring out */
782 #define OCTAL_FROM "\200\027" /* From */
783 #define OCTAL_CONNECTED "\200\030" /* Connected */
784 #define OCTAL_BUSY "\200\031" /* Busy */
785 #define OCTAL_LINEINUSE "\200\032" /* Line In Use */
786 #define OCTAL_CALLWAITING "\200\033" /* Call Waiting */
787 #define OCTAL_CALLXFER "\200\034" /* Call Transfer */
788 #define OCTAL_CALLPARK "\200\035" /* Call Park */
789 #define OCTAL_CALLPROCEED "\200\036" /* Call Proceed */
790 #define OCTAL_INUSEREMOTE "\200\037" /* In Use Remote */
791 #define OCTAL_ENTRNUM "\200\040" /* Enter number */
792 #define OCTAL_PARKAT "\200\041" /* Call park At */
793 #define OCTAL_PRIMONLY "\200\042" /* Primary Only */
794 #define OCTAL_TMPFAIL "\200\043" /* Temp Fail */
795 #define OCTAL_HAVEVMAIL "\200\044" /* You Have VoiceMail */
796 #define OCTAL_FWDEDTO "\200\045" /* Forwarded to */
797 #define OCTAL_CANTCOMPCNF "\200\046" /* Can Not Complete Conference */
798 #define OCTAL_NOCONFBRDG "\200\047" /* No Conference Bridge */
799 #define OCTAL_NOPRIMARYCTL "\200\050" /* Can Not Hold Primary Control */
800 #define OCTAL_INVALCONFPART "\200\051" /* Invalid Conference Participant */
801 #define OCTAL_INCONFALREADY "\200\052" /* In Conference Already */
802 #define OCTAL_NOPARTINFO "\200\053" /* No Participant Info */
803 #define OCTAL_MAXPARTEXCEED "\200\054" /* Exceed Maximum Parties */
804 #define OCTAL_KEYNOTACTIVE "\200\055" /* Key Is Not Active */
805 #define OCTAL_ERRNOLIC "\200\056" /* Error No License */
806 #define OCTAL_ERRDBCFG "\200\057" /* Error DBConfig */
807 #define OCTAL_ERRDB "\200\060" /* Error Database */
808 #define OCTAL_ERRPASSLMT "\200\061" /* Error Pass Limit */
809 #define OCTAL_ERRUNK "\200\062" /* Error Unknown */
810 #define OCTAL_ERRMISMATCH "\200\063" /* Error Mismatch */
811 #define OCTAL_CONFERENCE "\200\064" /* Conference */
812 #define OCTAL_PARKNO "\200\065" /* Park Number */
813 #define OCTAL_PRIVATE "\200\066" /* Private */
814 #define OCTAL_INSUFBANDW "\200\067" /* Not Enough Bandwidth */
815 #define OCTAL_UNKNUM "\200\070" /* Unknown Number */
816 #define OCTAL_RMLSTC "\200\071" /* RmLstC */
817 #define OCTAL_VOICEMAIL "\200\072" /* Voicemail */
818 #define OCTAL_IMMDIV "\200\073" /* ImmDiv */
819 #define OCTAL_INTRCPT "\200\074" /* Intrcpt */
820 #define OCTAL_SETWTCH "\200\075" /* SetWtch */
821 #define OCTAL_TRNSFVM "\200\076" /* TrnsfVM */
822 #define OCTAL_DND "\200\077" /* DND */
823 #define OCTAL_DIVALL "\200\100" /* DivAll */
824 #define OCTAL_CALLBACK "\200\101" /* CallBack */
825 #define OCTAL_NETCNGREROUT "\200\102" /* Network congestion,rerouting */
826 #define OCTAL_BARGE "\200\103" /* Barge */
827 #define OCTAL_BARGEFAIL "\200\104" /* Failed to setup Barge */
828 #define OCTAL_BARGEEXIST "\200\105" /* Another Barge exists */
829 #define OCTAL_INCOMPATDEV "\200\106" /* Incompatible device type */
830 #define OCTAL_PARKNONUM "\200\107" /* No Park Number Available */
831 #define OCTAL_PARKREVERSION "\200\110" /* CallPark Reversion */
832 #define OCTAL_SRVNOTACTIVE "\200\111" /* Service is not Active */
833 #define OCTAL_HITRAFFIC "\200\112" /* High Traffic Try Again Later */
834 #define OCTAL_QRT "\200\113" /* QRT */
835 #define OCTAL_MCID "\200\114" /* MCID */
836 #define OCTAL_DIRTRFR "\200\115" /* DirTrfr */
837 #define OCTAL_SELECT "\200\116" /* Select */
838 #define OCTAL_CONFLIST "\200\117" /* ConfList */
839 #define OCTAL_IDIVERT "\200\120" /* iDivert */
840 #define OCTAL_CBARGE "\200\121" /* cBarge */
841 #define OCTAL_CANTCOMPLXFER "\200\122" /* Can Not Complete Transfer */
842 #define OCTAL_CANTJOINCALLS "\200\123" /* Can Not Join Calls */
843 #define OCTAL_MCIDSUCCESS "\200\124" /* Mcid Successful */
844 #define OCTAL_NUMNOTCFG "\200\125" /* Number Not Configured */
845 #define OCTAL_SECERROR "\200\126" /* Security Error */
846 #define OCTAL_VIDBANDWNA "\200\127" /* Video Bandwidth Unavailable */
847 #define OCTAL_VIDMODE "\200\130" /* VidMode */
848 #define OCTAL_CALLDURTIMEOUT "\200\131" /* Max Call Duration Timeout */
849 #define OCTAL_HOLDDURTIMEOUT "\200\132" /* Max Hold Duration Timeout */
850 #define OCTAL_OPICKUP "\200\133" /* OPickUp */
851 /* "\200\134" ??? */
852 /* "\200\135" ??? */
853 /* "\200\136" ??? */
854 /* "\200\137" ??? */
855 /* "\200\140" ??? */
856 #define OCTAL_EXTXFERRESTRICT "\200\141" /* External Transfer Restricted */
857 /* "\200\142" ??? */
858 /* "\200\143" ??? */
859 /* "\200\144" ??? */
860 #define OCTAL_MACADD "\200\145" /* Mac Address */
861 #define OCTAL_HOST "\200\146" /* Host Name */
862 #define OCTAL_DOMAIN "\200\147" /* Domain Name */
863 #define OCTAL_IPADD "\200\150" /* IP Address */
864 #define OCTAL_SUBMASK "\200\151" /* Subnet Mask */
865 #define OCTAL_TFTP1 "\200\152" /* TFTP Server 1 */
866 #define OCTAL_ROUTER1 "\200\153" /* Default Router 1 */
867 #define OCTAL_ROUTER2 "\200\154" /* Default Router 2 */
868 #define OCTAL_ROUTER3 "\200\155" /* Default Router 3 */
869 #define OCTAL_ROUTER4 "\200\156" /* Default Router 4 */
870 #define OCTAL_ROUTER5 "\200\157" /* Default Router 5 */
871 #define OCTAL_DNS1 "\200\160" /* DNS Server 1 */
872 #define OCTAL_DNS2 "\200\161" /* DNS Server 2 */
873 #define OCTAL_DNS3 "\200\162" /* DNS Server 3 */
874 #define OCTAL_DNS4 "\200\163" /* DNS Server 4 */
875 #define OCTAL_DNS5 "\200\164" /* DNS Server 5 */
876 #define OCTAL_VLANOPID "\200\165" /* Operational VLAN Id */
877 #define OCTAL_VLANADID "\200\166" /* Admin. VLAN Id */
878 #define OCTAL_CM1 "\200\167" /* CallManager 1 */
879 #define OCTAL_CM2 "\200\170" /* CallManager 2 */
880 #define OCTAL_CM3 "\200\171" /* CallManager 3 */
881 #define OCTAL_CM4 "\200\172" /* CallManager 4 */
882 #define OCTAL_CM5 "\200\173" /* CallManager 5 */
883 #define OCTAL_URLINFO "\200\174" /* Information URL */
884 #define OCTAL_URLDIRS "\200\175" /* Directories URL */
885 #define OCTAL_URLMSGS "\200\176" /* Messages URL */
886 #define OCTAL_URLSRVS "\200\177" /* Services URL */
887 
907  { OCTAL_DND, SOFTKEY_DND },
909  { "Dial", SOFTKEY_FORCEDIAL},
910 };
911 
913  const uint8_t mode;
914  const uint8_t *defaults;
915  const int count;
916 };
917 
918 static const uint8_t soft_key_default_onhook[] = {
924  SOFTKEY_DND,
926  /*SOFTKEY_CONFRN,*/
927 };
928 
929 static const uint8_t soft_key_default_connected[] = {
930  SOFTKEY_HOLD,
933  SOFTKEY_PARK,
937 };
938 
939 static const uint8_t soft_key_default_onhold[] = {
944 };
945 
946 static const uint8_t soft_key_default_ringin[] = {
950 };
951 
952 static const uint8_t soft_key_default_offhook[] = {
960 };
961 
962 static const uint8_t soft_key_default_connwithtrans[] = {
963  SOFTKEY_HOLD,
966  SOFTKEY_PARK,
970 };
971 
972 static const uint8_t soft_key_default_dadfd[] = {
977 };
978 
979 static const uint8_t soft_key_default_connwithconf[] = {
980  SOFTKEY_NONE,
981 };
982 
983 static const uint8_t soft_key_default_ringout[] = {
984  SOFTKEY_NONE,
986 };
987 
988 static const uint8_t soft_key_default_ringoutwithtransfer[] = {
989  SOFTKEY_NONE,
992 };
993 
994 static const uint8_t soft_key_default_offhookwithfeat[] = {
998 };
999 
1000 static const uint8_t soft_key_default_unknown[] = {
1001  SOFTKEY_NONE,
1002 };
1003 
1004 static const uint8_t soft_key_default_SLAhold[] = {
1008 };
1009 
1010 static const uint8_t soft_key_default_SLAconnectednotactive[] = {
1013  SOFTKEY_JOIN,
1014 };
1015 
1017  {KEYDEF_ONHOOK, soft_key_default_onhook, sizeof(soft_key_default_onhook) / sizeof(uint8_t)},
1019  {KEYDEF_ONHOLD, soft_key_default_onhold, sizeof(soft_key_default_onhold) / sizeof(uint8_t)},
1020  {KEYDEF_RINGIN, soft_key_default_ringin, sizeof(soft_key_default_ringin) / sizeof(uint8_t)},
1023  {KEYDEF_DADFD, soft_key_default_dadfd, sizeof(soft_key_default_dadfd) / sizeof(uint8_t)},
1031 };
1032 
1034  uint32_t softKeyOffset;
1035  uint32_t softKeyCount;
1037  struct soft_key_template_definition softKeyTemplateDefinition[32];
1038 };
1039 
1040 #define SOFT_KEY_SET_RES_MESSAGE 0x0109
1041 
1043  uint8_t softKeyTemplateIndex[16];
1044  uint16_t softKeyInfoIndex[16];
1045 };
1046 
1051  struct soft_key_set_definition softKeySetDefinition[16];
1052  uint32_t res;
1053 };
1054 
1055 #define SELECT_SOFT_KEYS_MESSAGE 0x0110
1057  uint32_t instance;
1058  uint32_t reference;
1060  uint32_t validKeyMask;
1061 };
1062 
1063 #define CALL_STATE_MESSAGE 0x0111
1065  uint32_t callState;
1066  uint32_t lineInstance;
1067  uint32_t callReference;
1068  uint32_t space[3];
1069 };
1070 
1071 #define DISPLAY_PROMPT_STATUS_MESSAGE 0x0112
1073  uint32_t messageTimeout;
1074  char promptMessage[32];
1075  uint32_t lineInstance;
1076  uint32_t callReference;
1077  uint32_t space[3];
1078 };
1079 
1080 #define CLEAR_PROMPT_MESSAGE 0x0113
1082  uint32_t lineInstance;
1083  uint32_t callReference;
1084 };
1085 
1086 #define DISPLAY_NOTIFY_MESSAGE 0x0114
1088  uint32_t displayTimeout;
1089  char displayMessage[100];
1090 };
1091 
1092 #define ACTIVATE_CALL_PLANE_MESSAGE 0x0116
1094  uint32_t lineInstance;
1095 };
1096 
1097 #define DIALED_NUMBER_MESSAGE 0x011D
1099  char dialedNumber[24];
1100  uint32_t lineInstance;
1101  uint32_t callReference;
1102 };
1103 
1104 #define MAX_SERVICEURL 256
1105 #define SERVICEURL_STAT_MESSAGE 0x012F
1107  uint32_t instance;
1109  char displayName[40];
1110 };
1111 
1112 #define MAXCALLINFOSTR 256
1113 #define MAXDISPLAYNOTIFYSTR 32
1114 
1115 #define DISPLAY_PRINOTIFY_MESSAGE 0x0120
1117  uint32_t timeout;
1118  uint32_t priority;
1120 };
1121 
1122 #define CLEAR_PRINOTIFY_MESSAGE 0x0121
1124  uint32_t priority;
1125 };
1126 
1127 #define CALL_INFO_MESSAGE_VARIABLE 0x014A
1129  uint32_t instance;
1130  uint32_t callreference;
1131  uint32_t calldirection;
1132  uint32_t unknown1;
1133  uint32_t unknown2;
1134  uint32_t unknown3;
1135  uint32_t unknown4;
1136  uint32_t unknown5;
1137  char calldetails[MAXCALLINFOSTR];
1138 };
1139 
1140 #define DISPLAY_PRINOTIFY_MESSAGE_VARIABLE 0x0144
1142  uint32_t timeout;
1143  uint32_t priority;
1145 };
1146 
1147 #define DISPLAY_PROMPT_STATUS_MESSAGE_VARIABLE 0x0145
1149  uint32_t unknown;
1150  uint32_t lineInstance;
1151  uint32_t callReference;
1152  char promptMessage[MAXCALLINFOSTR];
1153 };
1154 
1157  struct speed_dial_stat_req_message speeddialreq;
1158  struct register_message reg;
1159  struct register_ack_message regack;
1160  struct register_rej_message regrej;
1163  struct button_template_res_message buttontemplate;
1164  struct displaytext_message displaytext;
1166  struct display_prompt_status_message displaypromptstatus;
1167  struct clear_prompt_message clearpromptstatus;
1168  struct definetimedate_message definetimedate;
1169  struct start_tone_message starttone;
1170  struct stop_tone_message stoptone;
1173  struct line_stat_res_message linestat;
1174  struct soft_key_set_res_message softkeysets;
1175  struct soft_key_template_res_message softkeytemplate;
1176  struct server_res_message serverres;
1177  struct reset_message reset;
1178  struct set_lamp_message setlamp;
1179  struct set_ringer_message setringer;
1180  struct call_state_message callstate;
1181  struct keypad_button_message keypad;
1182  struct select_soft_keys_message selectsoftkey;
1183  struct activate_call_plane_message activatecallplane;
1185  struct offhook_message offhook;
1186  struct onhook_message onhook;
1187  struct set_speaker_message setspeaker;
1188  struct set_microphone_message setmicrophone;
1189  struct call_info_message callinfo;
1193  struct open_receive_channel_message openreceivechannel;
1194  struct open_receive_channel_ack_message_ip4 openreceivechannelack_ip4;
1195  struct open_receive_channel_ack_message_ip6 openreceivechannelack_ip6;
1196  struct close_receive_channel_message closereceivechannel;
1197  struct display_notify_message displaynotify;
1198  struct dialed_number_message dialednumber;
1199  struct soft_key_event_message softkeyeventmessage;
1200  struct enbloc_call_message enbloccallmessage;
1201  struct forward_stat_message forwardstat;
1202  struct bksp_req_message bkspmessage;
1203  struct call_info_message_variable callinfomessagevariable;
1204  struct display_prompt_status_message_variable displaypromptstatusvar;
1205  struct serviceurl_stat_message serviceurlmessage;
1206  struct clear_prinotify_message clearprinotify;
1207  struct display_prinotify_message displayprinotify;
1208  struct display_prinotify_message_variable displayprinotifyvar;
1209 };
1210 
1211 /* packet composition */
1212 struct skinny_req {
1213  uint32_t len;
1214  uint32_t res;
1215  uint32_t e;
1216  union skinny_data data;
1217 };
1218 
1219 /* XXX This is the combined size of the variables above. (len, res, e)
1220  If more are added, this MUST change.
1221  (sizeof(skinny_req) - sizeof(skinny_data)) DOES NOT WORK on all systems (amd64?). */
1222 static int skinny_header_size = 12;
1223 
1224 /*****************************
1225  * Asterisk specific globals *
1226  *****************************/
1227 
1228 static int skinnyreload = 0;
1229 
1230 /* a hostname, portnumber, socket and such is usefull for VoIP protocols */
1231 static struct sockaddr_in bindaddr;
1232 static char ourhost[256];
1233 static int ourport;
1234 static struct in_addr __ourip;
1235 static struct ast_hostent ahp;
1236 static struct hostent *hp;
1237 static int skinnysock = -1;
1238 static pthread_t accept_t;
1239 static int callnums = 1;
1240 
1241 #define SKINNY_DEVICE_UNKNOWN -1
1242 #define SKINNY_DEVICE_NONE 0
1243 #define SKINNY_DEVICE_30SPPLUS 1
1244 #define SKINNY_DEVICE_12SPPLUS 2
1245 #define SKINNY_DEVICE_12SP 3
1246 #define SKINNY_DEVICE_12 4
1247 #define SKINNY_DEVICE_30VIP 5
1248 #define SKINNY_DEVICE_7910 6
1249 #define SKINNY_DEVICE_7960 7
1250 #define SKINNY_DEVICE_7940 8
1251 #define SKINNY_DEVICE_7935 9
1252 #define SKINNY_DEVICE_ATA186 12 /* Cisco ATA-186 */
1253 #define SKINNY_DEVICE_7941 115
1254 #define SKINNY_DEVICE_7971 119
1255 #define SKINNY_DEVICE_7914 124 /* Expansion module */
1256 #define SKINNY_DEVICE_7985 302
1257 #define SKINNY_DEVICE_7911 307
1258 #define SKINNY_DEVICE_7961GE 308
1259 #define SKINNY_DEVICE_7941GE 309
1260 #define SKINNY_DEVICE_7931 348
1261 #define SKINNY_DEVICE_7921 365
1262 #define SKINNY_DEVICE_7906 369
1263 #define SKINNY_DEVICE_7962 404 /* Not found */
1264 #define SKINNY_DEVICE_7937 431
1265 #define SKINNY_DEVICE_7942 434
1266 #define SKINNY_DEVICE_7945 435
1267 #define SKINNY_DEVICE_7965 436
1268 #define SKINNY_DEVICE_7975 437
1269 #define SKINNY_DEVICE_7905 20000
1270 #define SKINNY_DEVICE_7920 30002
1271 #define SKINNY_DEVICE_7970 30006
1272 #define SKINNY_DEVICE_7912 30007
1273 #define SKINNY_DEVICE_7902 30008
1274 #define SKINNY_DEVICE_CIPC 30016 /* Cisco IP Communicator */
1275 #define SKINNY_DEVICE_7961 30018
1276 #define SKINNY_DEVICE_7936 30019
1277 #define SKINNY_DEVICE_SCCPGATEWAY_AN 30027 /* Analog gateway */
1278 #define SKINNY_DEVICE_SCCPGATEWAY_BRI 30028 /* BRI gateway */
1279 
1280 #define SKINNY_SPEAKERON 1
1281 #define SKINNY_SPEAKEROFF 2
1282 
1283 #define SKINNY_MICON 1
1284 #define SKINNY_MICOFF 2
1285 
1286 #define SKINNY_OFFHOOK 1
1287 #define SKINNY_ONHOOK 2
1288 #define SKINNY_RINGOUT 3
1289 #define SKINNY_RINGIN 4
1290 #define SKINNY_CONNECTED 5
1291 #define SKINNY_BUSY 6
1292 #define SKINNY_CONGESTION 7
1293 #define SKINNY_HOLD 8
1294 #define SKINNY_CALLWAIT 9
1295 #define SKINNY_TRANSFER 10
1296 #define SKINNY_PARK 11
1297 #define SKINNY_PROGRESS 12
1298 #define SKINNY_CALLREMOTEMULTILINE 13
1299 #define SKINNY_INVALID 14
1300 
1301 #define SKINNY_INCOMING 1
1302 #define SKINNY_OUTGOING 2
1303 
1304 #define SKINNY_SILENCE 0x00 /* Note sure this is part of the protocol, remove? */
1305 #define SKINNY_DIALTONE 0x21
1306 #define SKINNY_BUSYTONE 0x23
1307 #define SKINNY_ALERT 0x24
1308 #define SKINNY_REORDER 0x25
1309 #define SKINNY_CALLWAITTONE 0x2D
1310 #define SKINNY_ZIPZIP 0x31
1311 #define SKINNY_ZIP 0x32
1312 #define SKINNY_BEEPBONK 0x33
1313 #define SKINNY_BARGIN 0x43
1314 #define SKINNY_NOTONE 0x7F
1315 
1316 #define SKINNY_LAMP_OFF 1
1317 #define SKINNY_LAMP_ON 2
1318 #define SKINNY_LAMP_WINK 3
1319 #define SKINNY_LAMP_FLASH 4
1320 #define SKINNY_LAMP_BLINK 5
1321 
1322 #define SKINNY_RING_OFF 1
1323 #define SKINNY_RING_INSIDE 2
1324 #define SKINNY_RING_OUTSIDE 3
1325 #define SKINNY_RING_FEATURE 4
1326 
1327 #define SKINNY_CFWD_ALL (1 << 0)
1328 #define SKINNY_CFWD_BUSY (1 << 1)
1329 #define SKINNY_CFWD_NOANSWER (1 << 2)
1330 
1331 /* Skinny rtp stream modes. Do we really need this? */
1332 #define SKINNY_CX_SENDONLY 0
1333 #define SKINNY_CX_RECVONLY 1
1334 #define SKINNY_CX_SENDRECV 2
1335 #define SKINNY_CX_CONF 3
1336 #define SKINNY_CX_CONFERENCE 3
1337 #define SKINNY_CX_MUTE 4
1338 #define SKINNY_CX_INACTIVE 4
1339 
1340 #if 0
1341 static const char * const skinny_cxmodes[] = {
1342  "sendonly",
1343  "recvonly",
1344  "sendrecv",
1345  "confrnce",
1346  "inactive"
1347 };
1348 #endif
1349 
1350 /* driver scheduler */
1351 static struct ast_sched_context *sched = NULL;
1352 
1353 /* Protect the network socket */
1355 
1356 /* Wait up to 16 seconds for first digit */
1357 static int firstdigittimeout = 16000;
1358 
1359 /* How long to wait for following digits */
1360 static int gendigittimeout = 8000;
1361 
1362 /* How long to wait for an extra digit, if there is an ambiguous match */
1363 static int matchdigittimeout = 3000;
1364 
1365 /*!
1366  * To apease the stupid compiler option on ast_sched_del()
1367  * since we don't care about the return value.
1368  */
1369 static int not_used;
1370 
1371 #define SUBSTATE_UNSET 0
1372 #define SUBSTATE_OFFHOOK 1
1373 #define SUBSTATE_ONHOOK 2
1374 #define SUBSTATE_RINGOUT 3
1375 #define SUBSTATE_RINGIN 4
1376 #define SUBSTATE_CONNECTED 5
1377 #define SUBSTATE_BUSY 6
1378 #define SUBSTATE_CONGESTION 7
1379 #define SUBSTATE_HOLD 8
1380 #define SUBSTATE_CALLWAIT 9
1381 #define SUBSTATE_PROGRESS 12
1382 #define SUBSTATE_DIALING 101
1383 
1384 #define DIALTYPE_NORMAL 1<<0
1385 #define DIALTYPE_CFWD 1<<1
1386 #define DIALTYPE_XFER 1<<2
1387 
1392  unsigned int callid;
1394  /* time_t lastouttime; */ /* Unused */
1396  int ringing;
1397  /* int lastout; */ /* Unused */
1398  int cxmode;
1399  int nat;
1402  int xferor;
1405  int aa_beep;
1406  int aa_mute;
1411  char *origtonum;
1412  char *origtoname;
1413 
1418 };
1419 
1420 #define SKINNY_LINE_OPTIONS \
1421  char name[80]; \
1422  char label[24]; \
1423  char accountcode[AST_MAX_ACCOUNT_CODE]; \
1424  char exten[AST_MAX_EXTENSION]; \
1425  char context[AST_MAX_CONTEXT]; \
1426  char language[MAX_LANGUAGE]; \
1427  char cid_num[AST_MAX_EXTENSION]; \
1428  char cid_name[AST_MAX_EXTENSION]; \
1429  char lastcallerid[AST_MAX_EXTENSION]; \
1430  int cfwdtype; \
1431  char call_forward_all[AST_MAX_EXTENSION]; \
1432  char call_forward_busy[AST_MAX_EXTENSION]; \
1433  char call_forward_noanswer[AST_MAX_EXTENSION]; \
1434  char mailbox[AST_MAX_MAILBOX_UNIQUEID]; \
1435  char vmexten[AST_MAX_EXTENSION]; \
1436  char regexten[AST_MAX_EXTENSION]; \
1437  char regcontext[AST_MAX_CONTEXT]; \
1438  char parkinglot[AST_MAX_CONTEXT]; \
1439  char mohinterpret[MAX_MUSICCLASS]; \
1440  char mohsuggest[MAX_MUSICCLASS]; \
1441  char lastnumberdialed[AST_MAX_EXTENSION]; \
1442  char dialoutexten[AST_MAX_EXTENSION]; \
1443  char dialoutcontext[AST_MAX_CONTEXT]; \
1444  ast_group_t callgroup; \
1445  ast_group_t pickupgroup; \
1446  struct ast_namedgroups *named_callgroups; \
1447  struct ast_namedgroups *named_pickupgroups; \
1448  int callwaiting; \
1449  int transfer; \
1450  int threewaycalling; \
1451  int mwiblink; \
1452  int cancallforward; \
1453  int callfwdtimeout; \
1454  int dnd; \
1455  int hidecallerid; \
1456  int amaflags; \
1457  int instance; \
1458  int group; \
1459  int nonCodecCapability; \
1460  int immediate; \
1461  int nat; \
1462  int directmedia; \
1463  int prune;
1464 
1465 struct skinny_line {
1469  struct ast_mwi_subscriber *mwi_event_sub; /* Event based MWI */
1478  struct ast_variable *chanvars; /*!< Channel variables to set for inbound call */
1479  int newmsgs;
1480 };
1481 
1482 static struct skinny_line_options{
1484 } default_line_struct = {
1485  .callwaiting = 1,
1486  .transfer = 1,
1487  .mwiblink = 0,
1488  .dnd = 0,
1489  .hidecallerid = 0,
1490  .amaflags = 0,
1491  .instance = 0,
1492  .directmedia = 0,
1493  .nat = 0,
1494  .callfwdtimeout = 20000,
1495  .prune = 0,
1496 };
1498 
1500 
1506  char name[80];
1509  char stname[AST_MAX_EXTENSION];
1510  char lnname[AST_MAX_EXTENSION];
1511  char ourName[40];
1512  char ourNum[24];
1513  char theirName[40];
1514  char theirNum[24];
1518  unsigned int callid;
1519 };
1520 
1524  char label[42];
1528  int stateid;
1530  int isHint;
1531 
1534 };
1535 
1539  char displayName[40];
1542 };
1543 
1544 #define SKINNY_DEVICECONTAINER 1
1545 #define SKINNY_LINECONTAINER 2
1546 #define SKINNY_SUBLINECONTAINER 3
1547 #define SKINNY_SDCONTAINER 4
1548 
1550  int type;
1551  void *data;
1552 };
1553 
1556  char type[10];
1559 };
1560 
1561 #define SKINNY_DEVICE_OPTIONS \
1562  char name[80]; \
1563  char id[16]; \
1564  char version_id[16]; \
1565  char vmexten[AST_MAX_EXTENSION]; \
1566  int type; \
1567  int protocolversion; \
1568  int hookstate; \
1569  int lastlineinstance; \
1570  int lastcallreference; \
1571  int earlyrtp; \
1572  int transfer; \
1573  int callwaiting; \
1574  int mwiblink; \
1575  int dnd; \
1576  int prune;
1577 
1580  struct type *first;
1581  struct type *last;
1583  struct sockaddr_in addr;
1584  struct in_addr ourip;
1585  struct ast_ha *ha;
1596 };
1597 
1598 static struct skinny_device_options {
1601  .transfer = 1,
1602  .earlyrtp = 1,
1603  .callwaiting = 1,
1604  .mwiblink = 0,
1605  .dnd = 0,
1606  .prune = 0,
1607  .hookstate = SKINNY_ONHOOK,
1608 };
1610 
1612 
1614  pthread_t t;
1616  struct timeval start;
1617  struct sockaddr_in sin;
1618  int fd;
1619  char outbuf[SKINNY_MAX_PACKET];
1622  int lockstate; /* Only for use in the skinny_session thread */
1625  struct timeval last_keepalive;
1627 };
1628 
1629 static struct ast_channel *skinny_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *dest, int *cause);
1631 
1632 static int skinny_devicestate(const char *data);
1633 static int skinny_call(struct ast_channel *ast, const char *dest, int timeout);
1634 static int skinny_hangup(struct ast_channel *ast);
1635 static int skinny_answer(struct ast_channel *ast);
1636 static struct ast_frame *skinny_read(struct ast_channel *ast);
1637 static int skinny_write(struct ast_channel *ast, struct ast_frame *frame);
1638 static int skinny_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen);
1639 static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
1640 static int skinny_senddigit_begin(struct ast_channel *ast, char digit);
1641 static int skinny_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
1642 static void mwi_event_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg);
1643 static int skinny_dialer_cb(const void *data);
1644 static int skinny_reload(void);
1645 
1646 static void skinny_set_owner(struct skinny_subchannel* sub, struct ast_channel* chan);
1647 static void setsubstate(struct skinny_subchannel *sub, int state);
1648 static void dumpsub(struct skinny_subchannel *sub, int forcehangup);
1649 static void activatesub(struct skinny_subchannel *sub, int state);
1650 static void dialandactivatesub(struct skinny_subchannel *sub, char exten[AST_MAX_EXTENSION]);
1651 static int skinny_nokeepalive_cb(const void *data);
1652 
1653 static void transmit_definetimedate(struct skinny_device *d);
1654 
1656  .type = "Skinny",
1657  .description = tdesc,
1659  .requester = skinny_request,
1660  .devicestate = skinny_devicestate,
1661  .call = skinny_call,
1662  .hangup = skinny_hangup,
1663  .answer = skinny_answer,
1664  .read = skinny_read,
1665  .write = skinny_write,
1666  .indicate = skinny_indicate,
1667  .fixup = skinny_fixup,
1668  .send_digit_begin = skinny_senddigit_begin,
1669  .send_digit_end = skinny_senddigit_end,
1670 };
1671 
1672 static int skinny_extensionstate_cb(const char *context, const char *exten, struct ast_state_cb_info *info, void *data);
1673 
1674 static struct skinny_line *skinny_line_alloc(void)
1675 {
1676  struct skinny_line *l;
1677  if (!(l = ast_calloc(1, sizeof(*l)))) {
1678  return NULL;
1679  }
1680 
1683  if (!l->cap || !l->confcap) {
1684  ao2_cleanup(l->cap);
1685  ao2_cleanup(l->confcap);
1686  ast_free(l);
1687  return NULL;
1688  }
1689  return l;
1690 }
1692 {
1693  ao2_ref(l->cap, -1);
1694  ao2_ref(l->confcap, -1);
1695  l->named_callgroups = ast_unref_namedgroups(l->named_callgroups);
1696  l->named_pickupgroups = ast_unref_namedgroups(l->named_pickupgroups);
1697  ast_free(l->container);
1698  ast_free(l);
1699  return NULL;
1700 }
1701 static struct skinny_device *skinny_device_alloc(const char *dname)
1702 {
1703  struct skinny_device *d;
1704  if (!(d = ast_calloc(1, sizeof(*d)))) {
1705  return NULL;
1706  }
1707 
1710  d->endpoint = ast_endpoint_create("Skinny", dname);
1711  if (!d->cap || !d->confcap || !d->endpoint) {
1712  ao2_cleanup(d->cap);
1713  ao2_cleanup(d->confcap);
1714  ast_free(d);
1715  return NULL;
1716  }
1717 
1718  ast_copy_string(d->name, dname, sizeof(d->name));
1719 
1720  return d;
1721 }
1723 {
1724  ao2_ref(d->cap, -1);
1725  ao2_ref(d->confcap, -1);
1727  ast_free(d);
1728  return NULL;
1729 }
1730 
1732 {
1733  struct skinny_device *d = s->device;
1734  struct skinny_addon *a;
1735  int i;
1736 
1737  switch (d->type) {
1739  case SKINNY_DEVICE_30VIP:
1740  /* 13 rows, 2 columns */
1741  for (i = 0; i < 4; i++)
1742  (btn++)->buttonDefinition = BT_CUST_LINE;
1743  (btn++)->buttonDefinition = BT_REDIAL;
1744  (btn++)->buttonDefinition = BT_VOICEMAIL;
1745  (btn++)->buttonDefinition = BT_CALLPARK;
1746  (btn++)->buttonDefinition = BT_FORWARDALL;
1747  (btn++)->buttonDefinition = BT_CONFERENCE;
1748  for (i = 0; i < 4; i++)
1749  (btn++)->buttonDefinition = BT_NONE;
1750  for (i = 0; i < 13; i++)
1751  (btn++)->buttonDefinition = BT_SPEEDDIAL;
1752 
1753  break;
1755  case SKINNY_DEVICE_12SP:
1756  case SKINNY_DEVICE_12:
1757  /* 6 rows, 2 columns */
1758  for (i = 0; i < 2; i++)
1759  (btn++)->buttonDefinition = BT_CUST_LINE;
1760  for (i = 0; i < 4; i++)
1761  (btn++)->buttonDefinition = BT_SPEEDDIAL;
1762  (btn++)->buttonDefinition = BT_HOLD;
1763  (btn++)->buttonDefinition = BT_REDIAL;
1764  (btn++)->buttonDefinition = BT_TRANSFER;
1765  (btn++)->buttonDefinition = BT_FORWARDALL;
1766  (btn++)->buttonDefinition = BT_CALLPARK;
1767  (btn++)->buttonDefinition = BT_VOICEMAIL;
1768  break;
1769  case SKINNY_DEVICE_7910:
1770  (btn++)->buttonDefinition = BT_LINE;
1771  (btn++)->buttonDefinition = BT_HOLD;
1772  (btn++)->buttonDefinition = BT_TRANSFER;
1773  (btn++)->buttonDefinition = BT_DISPLAY;
1774  (btn++)->buttonDefinition = BT_VOICEMAIL;
1775  (btn++)->buttonDefinition = BT_CONFERENCE;
1776  (btn++)->buttonDefinition = BT_FORWARDALL;
1777  for (i = 0; i < 2; i++)
1778  (btn++)->buttonDefinition = BT_SPEEDDIAL;
1779  (btn++)->buttonDefinition = BT_REDIAL;
1780  break;
1781  case SKINNY_DEVICE_7960:
1782  case SKINNY_DEVICE_7961:
1783  case SKINNY_DEVICE_7961GE:
1784  case SKINNY_DEVICE_7962:
1785  case SKINNY_DEVICE_7965:
1786  for (i = 0; i < 6; i++)
1787  (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1788  break;
1789  case SKINNY_DEVICE_7940:
1790  case SKINNY_DEVICE_7941:
1791  case SKINNY_DEVICE_7941GE:
1792  case SKINNY_DEVICE_7942:
1793  case SKINNY_DEVICE_7945:
1794  for (i = 0; i < 2; i++)
1795  (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1796  break;
1797  case SKINNY_DEVICE_7935:
1798  case SKINNY_DEVICE_7936:
1799  for (i = 0; i < 2; i++)
1800  (btn++)->buttonDefinition = BT_LINE;
1801  break;
1802  case SKINNY_DEVICE_ATA186:
1803  (btn++)->buttonDefinition = BT_LINE;
1804  break;
1805  case SKINNY_DEVICE_7970:
1806  case SKINNY_DEVICE_7971:
1807  case SKINNY_DEVICE_7975:
1808  case SKINNY_DEVICE_CIPC:
1809  for (i = 0; i < 8; i++)
1810  (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1811  break;
1812  case SKINNY_DEVICE_7985:
1813  /* XXX I have no idea what the buttons look like on these. */
1814  ast_log(LOG_WARNING, "Unsupported device type '%d (7985)' found.\n", d->type);
1815  break;
1816  case SKINNY_DEVICE_7912:
1817  case SKINNY_DEVICE_7911:
1818  case SKINNY_DEVICE_7905:
1819  (btn++)->buttonDefinition = BT_LINE;
1820  (btn++)->buttonDefinition = BT_HOLD;
1821  break;
1822  case SKINNY_DEVICE_7920:
1823  /* XXX I don't know if this is right. */
1824  for (i = 0; i < 4; i++)
1825  (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1826  break;
1827  case SKINNY_DEVICE_7921:
1828  for (i = 0; i < 6; i++)
1829  (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1830  break;
1831  case SKINNY_DEVICE_7902:
1832  ast_log(LOG_WARNING, "Unsupported device type '%d (7902)' found.\n", d->type);
1833  break;
1834  case SKINNY_DEVICE_7906:
1835  ast_log(LOG_WARNING, "Unsupported device type '%d (7906)' found.\n", d->type);
1836  break;
1837  case SKINNY_DEVICE_7931:
1838  ast_log(LOG_WARNING, "Unsupported device type '%d (7931)' found.\n", d->type);
1839  break;
1840  case SKINNY_DEVICE_7937:
1841  ast_log(LOG_WARNING, "Unsupported device type '%d (7937)' found.\n", d->type);
1842  break;
1843  case SKINNY_DEVICE_7914:
1844  ast_log(LOG_WARNING, "Unsupported device type '%d (7914)' found. Expansion module registered by itself?\n", d->type);
1845  break;
1848  ast_log(LOG_WARNING, "Unsupported device type '%d (SCCP gateway)' found.\n", d->type);
1849  break;
1850  default:
1851  ast_log(LOG_WARNING, "Unknown device type '%d' found.\n", d->type);
1852  break;
1853  }
1854 
1855  AST_LIST_LOCK(&d->addons);
1856  AST_LIST_TRAVERSE(&d->addons, a, list) {
1857  if (!strcasecmp(a->type, "7914")) {
1858  for (i = 0; i < 14; i++)
1859  (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1860  } else {
1861  ast_log(LOG_WARNING, "Unknown addon type '%s' found. Skipping.\n", a->type);
1862  }
1863  }
1864  AST_LIST_UNLOCK(&d->addons);
1865 
1866  return btn;
1867 }
1868 
1869 static struct skinny_req *req_alloc(size_t size, int response_message)
1870 {
1871  struct skinny_req *req;
1872 
1873  if (!(req = ast_calloc(1, skinny_header_size + size + 4)))
1874  return NULL;
1875 
1876  req->len = htolel(size+4);
1877  req->e = htolel(response_message);
1878 
1879  return req;
1880 }
1881 
1882 static struct skinny_line *find_line_by_instance(struct skinny_device *d, int instance)
1883 {
1884  struct skinny_line *l;
1885 
1886  /*Dialing from on hook or on a 7920 uses instance 0 in requests
1887  but we need to start looking at instance 1 */
1888 
1889  if (!instance)
1890  instance = 1;
1891 
1892  AST_LIST_TRAVERSE(&d->lines, l, list){
1893  if (l->instance == instance)
1894  break;
1895  }
1896 
1897  if (!l) {
1898  ast_log(LOG_WARNING, "Could not find line with instance '%d' on device '%s'\n", instance, d->name);
1899  }
1900  return l;
1901 }
1902 
1903 static struct skinny_line *find_line_by_name(const char *dest)
1904 {
1905  struct skinny_line *l;
1906  struct skinny_line *tmpl = NULL;
1907  struct skinny_device *d;
1908  char line[256];
1909  char *at;
1910  char *device;
1911  int checkdevice = 0;
1912 
1913  ast_copy_string(line, dest, sizeof(line));
1914  at = strchr(line, '@');
1915  if (at)
1916  *at++ = '\0';
1917  device = at;
1918 
1919  if (!ast_strlen_zero(device))
1920  checkdevice = 1;
1921 
1924  if (checkdevice && tmpl)
1925  break;
1926  else if (!checkdevice) {
1927  /* This is a match, since we're checking for line on every device. */
1928  } else if (!strcasecmp(d->name, device)) {
1929  } else
1930  continue;
1931 
1932  /* Found the device (or we don't care which device) */
1933  AST_LIST_TRAVERSE(&d->lines, l, list){
1934  /* Search for the right line */
1935  if (!strcasecmp(l->name, line)) {
1936  if (tmpl) {
1937  ast_log(LOG_WARNING, "Ambiguous line name: %s\n", line);
1939  return NULL;
1940  } else
1941  tmpl = l;
1942  }
1943  }
1944  }
1946  return tmpl;
1947 }
1948 
1949 static struct skinny_subline *find_subline_by_name(const char *dest)
1950 {
1951  struct skinny_line *l;
1952  struct skinny_subline *subline;
1953  struct skinny_subline *tmpsubline = NULL;
1954  struct skinny_device *d;
1955 
1958  AST_LIST_TRAVERSE(&d->lines, l, list){
1959  AST_LIST_TRAVERSE(&l->sublines, subline, list){
1960  if (!strcasecmp(subline->name, dest)) {
1961  if (tmpsubline) {
1962  ast_verb(2, "Ambiguous subline name: %s\n", dest);
1964  return NULL;
1965  } else
1966  tmpsubline = subline;
1967  }
1968  }
1969  }
1970  }
1972  return tmpsubline;
1973 }
1974 
1976 {
1977  struct skinny_subline *subline;
1978  struct skinny_line *l;
1979 
1980  AST_LIST_TRAVERSE(&d->lines, l, list){
1981  AST_LIST_TRAVERSE(&l->sublines, subline, list){
1982  if (subline->callid == callid) {
1983  return subline;
1984  }
1985  }
1986  }
1987  return NULL;
1988 }
1989 
1990 /*!
1991  * implement the setvar config line
1992  */
1993 static struct ast_variable *add_var(const char *buf, struct ast_variable *list)
1994 {
1995  struct ast_variable *tmpvar = NULL;
1996  char *varname = ast_strdupa(buf), *varval = NULL;
1997 
1998  if ((varval = strchr(varname,'='))) {
1999  *varval++ = '\0';
2000  if ((tmpvar = ast_variable_new(varname, varval, ""))) {
2001  tmpvar->next = list;
2002  list = tmpvar;
2003  }
2004  }
2005  return list;
2006 }
2007 
2009 {
2010  if (sub && sub->owner) {
2011  ast_channel_lock(sub->owner);
2012  }
2013 }
2014 
2016 {
2017  if (sub && sub->owner) {
2018  ast_channel_unlock(sub->owner);
2019  }
2020 }
2021 
2023 {
2024  SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Deleting SCHED %d\n",
2025  sub->callid, sched_id);
2026  return ast_sched_del(sched, sched_id);
2027 }
2028 
2029 static int skinny_sched_add(int when, ast_sched_cb callback, struct skinny_subchannel *sub)
2030 {
2031  int ret;
2032  ret = ast_sched_add(sched, when, callback, sub);
2033  SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Added SCHED %d\n",
2034  sub->callid, ret);
2035  return ret;
2036 }
2037 
2038 /* It's quicker/easier to find the subchannel when we know the instance number too */
2039 static struct skinny_subchannel *find_subchannel_by_instance_reference(struct skinny_device *d, int instance, int reference)
2040 {
2041  struct skinny_line *l = find_line_by_instance(d, instance);
2042  struct skinny_subchannel *sub;
2043 
2044  if (!l) {
2045  return NULL;
2046  }
2047 
2048  /* 7920 phones set call reference to 0, so use the first
2049  sub-channel on the list.
2050  This MIGHT need more love to be right */
2051  if (!reference)
2052  sub = AST_LIST_FIRST(&l->sub);
2053  else {
2054  AST_LIST_TRAVERSE(&l->sub, sub, list) {
2055  if (sub->callid == reference)
2056  break;
2057  }
2058  }
2059  if (!sub) {
2060  ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s'\n", reference, d->name);
2061  }
2062  return sub;
2063 }
2064 
2065 /* Find the subchannel when we only have the callid - this shouldn't happen often */
2066 static struct skinny_subchannel *find_subchannel_by_reference(struct skinny_device *d, int reference)
2067 {
2068  struct skinny_line *l;
2069  struct skinny_subchannel *sub = NULL;
2070 
2071  AST_LIST_TRAVERSE(&d->lines, l, list){
2072  AST_LIST_TRAVERSE(&l->sub, sub, list){
2073  if (sub->callid == reference)
2074  break;
2075  }
2076  if (sub)
2077  break;
2078  }
2079 
2080  if (!l) {
2081  ast_log(LOG_WARNING, "Could not find any lines that contained a subchannel with reference '%d' on device '%s'\n", reference, d->name);
2082  } else {
2083  if (!sub) {
2084  ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s@%s'\n", reference, l->name, d->name);
2085  }
2086  }
2087  return sub;
2088 }
2089 
2091 {
2092  struct skinny_speeddial *sd;
2093 
2094  AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
2095  if (sd->isHint == isHint && sd->instance == instance)
2096  break;
2097  }
2098 
2099  if (!sd) {
2100  ast_log(LOG_WARNING, "Could not find speeddial with instance '%d' on device '%s'\n", instance, d->name);
2101  }
2102  return sd;
2103 }
2104 
2105 static struct ast_format *codec_skinny2ast(enum skinny_codecs skinnycodec)
2106 {
2107  switch (skinnycodec) {
2108  case SKINNY_CODEC_ALAW:
2109  return ast_format_alaw;
2110  case SKINNY_CODEC_ULAW:
2111  return ast_format_ulaw;
2112  case SKINNY_CODEC_G722:
2113  return ast_format_g722;
2114  case SKINNY_CODEC_G723_1:
2115  return ast_format_g723;
2116  case SKINNY_CODEC_G729A:
2117  return ast_format_g729;
2118  case SKINNY_CODEC_G726_32:
2119  return ast_format_g726; /* XXX Is this right? */
2120  case SKINNY_CODEC_H261:
2121  return ast_format_h261;
2122  case SKINNY_CODEC_H263:
2123  return ast_format_h263;
2124  default:
2125  return ast_format_none;
2126  }
2127 }
2128 
2129 static int codec_ast2skinny(const struct ast_format *astcodec)
2130 {
2132  return SKINNY_CODEC_ALAW;
2133  } else if (ast_format_cmp(astcodec, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) {
2134  return SKINNY_CODEC_ULAW;
2135  } else if (ast_format_cmp(astcodec, ast_format_g722) == AST_FORMAT_CMP_EQUAL) {
2136  return SKINNY_CODEC_G722;
2137  } else if (ast_format_cmp(astcodec, ast_format_g723) == AST_FORMAT_CMP_EQUAL) {
2138  return SKINNY_CODEC_G723_1;
2139  } else if (ast_format_cmp(astcodec, ast_format_g729) == AST_FORMAT_CMP_EQUAL) {
2140  return SKINNY_CODEC_G729A;
2141  } else if (ast_format_cmp(astcodec, ast_format_g726) == AST_FORMAT_CMP_EQUAL) {
2142  return SKINNY_CODEC_G726_32;
2143  } else if (ast_format_cmp(astcodec, ast_format_h261) == AST_FORMAT_CMP_EQUAL) {
2144  return SKINNY_CODEC_H261;
2145  } else if (ast_format_cmp(astcodec, ast_format_h263) == AST_FORMAT_CMP_EQUAL) {
2146  return SKINNY_CODEC_H263;
2147  } else {
2148  return 0;
2149  }
2150 }
2151 
2152 static int set_callforwards(struct skinny_line *l, const char *cfwd, int cfwdtype)
2153 {
2154  if (!l)
2155  return 0;
2156 
2157  if (!ast_strlen_zero(cfwd)) {
2158  if (cfwdtype & SKINNY_CFWD_ALL) {
2159  l->cfwdtype |= SKINNY_CFWD_ALL;
2160  ast_copy_string(l->call_forward_all, cfwd, sizeof(l->call_forward_all));
2161  }
2162  if (cfwdtype & SKINNY_CFWD_BUSY) {
2163  l->cfwdtype |= SKINNY_CFWD_BUSY;
2164  ast_copy_string(l->call_forward_busy, cfwd, sizeof(l->call_forward_busy));
2165  }
2166  if (cfwdtype & SKINNY_CFWD_NOANSWER) {
2167  l->cfwdtype |= SKINNY_CFWD_NOANSWER;
2168  ast_copy_string(l->call_forward_noanswer, cfwd, sizeof(l->call_forward_noanswer));
2169  }
2170  } else {
2171  if (cfwdtype & SKINNY_CFWD_ALL) {
2172  l->cfwdtype &= ~SKINNY_CFWD_ALL;
2173  memset(l->call_forward_all, 0, sizeof(l->call_forward_all));
2174  }
2175  if (cfwdtype & SKINNY_CFWD_BUSY) {
2176  l->cfwdtype &= ~SKINNY_CFWD_BUSY;
2177  memset(l->call_forward_busy, 0, sizeof(l->call_forward_busy));
2178  }
2179  if (cfwdtype & SKINNY_CFWD_NOANSWER) {
2180  l->cfwdtype &= ~SKINNY_CFWD_NOANSWER;
2181  memset(l->call_forward_noanswer, 0, sizeof(l->call_forward_noanswer));
2182  }
2183  }
2184  return l->cfwdtype;
2185 }
2186 
2187 static void cleanup_stale_contexts(char *new, char *old)
2188 {
2189  char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
2190 
2191  while ((oldcontext = strsep(&old, "&"))) {
2192  stalecontext = NULL;
2193  ast_copy_string(newlist, new, sizeof(newlist));
2194  stringp = newlist;
2195  while ((newcontext = strsep(&stringp, "&"))) {
2196  if (strcmp(newcontext, oldcontext) == 0) {
2197  /* This is not the context you're looking for */
2198  stalecontext = NULL;
2199  break;
2200  } else if (strcmp(newcontext, oldcontext)) {
2201  stalecontext = oldcontext;
2202  }
2203 
2204  }
2205  ast_context_destroy_by_name(stalecontext, "Skinny");
2206  }
2207 }
2208 
2209 static void register_exten(struct skinny_line *l)
2210 {
2211  char multi[256];
2212  char *stringp, *ext, *context;
2213 
2215  return;
2216 
2217  ast_copy_string(multi, S_OR(l->regexten, l->name), sizeof(multi));
2218  stringp = multi;
2219  while ((ext = strsep(&stringp, "&"))) {
2220  if ((context = strchr(ext, '@'))) {
2221  *context++ = '\0'; /* split ext@context */
2222  if (!ast_context_find(context)) {
2223  ast_log(LOG_WARNING, "Context %s must exist in regcontext= in skinny.conf!\n", context);
2224  continue;
2225  }
2226  } else {
2227  context = regcontext;
2228  }
2229  ast_add_extension(context, 1, ext, 1, NULL, NULL, "Noop",
2230  ast_strdup(l->name), ast_free_ptr, "Skinny");
2231  }
2232 }
2233 
2234 static void unregister_exten(struct skinny_line *l)
2235 {
2236  char multi[256];
2237  char *stringp, *ext, *context;
2238 
2240  return;
2241 
2242  ast_copy_string(multi, S_OR(l->regexten, l->name), sizeof(multi));
2243  stringp = multi;
2244  while ((ext = strsep(&stringp, "&"))) {
2245  if ((context = strchr(ext, '@'))) {
2246  *context++ = '\0'; /* split ext@context */
2247  if (!ast_context_find(context)) {
2248  ast_log(LOG_WARNING, "Context %s must exist in regcontext= in skinny.conf!\n", context);
2249  continue;
2250  }
2251  } else {
2252  context = regcontext;
2253  }
2254  ast_context_remove_extension(context, ext, 1, NULL);
2255  }
2256 }
2257 
2258 static int skinny_register(struct skinny_req *req, struct skinnysession *s)
2259 {
2260  struct skinny_device *d;
2261  struct skinny_line *l;
2262  struct skinny_subline *subline;
2263  struct skinny_speeddial *sd;
2264  struct sockaddr_in sin;
2265  socklen_t slen;
2266  int instance;
2267  int res = -1;
2268 
2269  if (-1 < s->auth_timeout_sched) {
2271  s->auth_timeout_sched = -1;
2272  }
2273 
2275  AST_LIST_TRAVERSE(&devices, d, list){
2276  struct ast_sockaddr addr;
2277  ast_sockaddr_from_sin(&addr, &s->sin);
2278  if (!strcasecmp(req->data.reg.name, d->id)
2279  && ast_apply_ha(d->ha, &addr)) {
2280  RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
2281 
2282  if (d->session) {
2283  ast_log(LOG_WARNING, "Device already registered.\n");
2285  res = 0;
2286  break;
2287  }
2288  s->device = d;
2289  d->type = letohl(req->data.reg.type);
2290  d->protocolversion = letohl(req->data.reg.protocolVersion);
2291  if (ast_strlen_zero(d->version_id)) {
2292  ast_copy_string(d->version_id, version_id, sizeof(d->version_id));
2293  }
2294  d->session = s;
2295 
2296  slen = sizeof(sin);
2297  if (getsockname(s->fd, (struct sockaddr *)&sin, &slen)) {
2298  ast_log(LOG_WARNING, "Cannot get socket name\n");
2299  sin.sin_addr = __ourip;
2300  }
2301  d->ourip = sin.sin_addr;
2302 
2303  AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
2305  }
2306  instance = 0;
2307  AST_LIST_TRAVERSE(&d->lines, l, list) {
2308  instance++;
2309  }
2310  AST_LIST_TRAVERSE(&d->lines, l, list) {
2312  /* l->capability = d->capability; */
2313  l->instance = instance;
2314  l->newmsgs = ast_app_has_voicemail(l->mailbox, NULL);
2316  register_exten(l);
2317  /* initialize MWI on line and device */
2318  mwi_event_cb(l, NULL, NULL);
2319  AST_LIST_TRAVERSE(&l->sublines, subline, list) {
2321  }
2323  --instance;
2324  }
2326  blob = ast_json_pack("{s: s}", "peer_status", "Registered");
2328  res = 1;
2329  break;
2330  }
2331  }
2333  return res;
2334 }
2335 
2336 static void end_session(struct skinnysession *s)
2337 {
2338  pthread_cancel(s->t);
2339 }
2340 
2341 #ifdef AST_DEVMODE
2342 static char *callstate2str(int ind)
2343 {
2344  char *tmp;
2345 
2346  switch (ind) {
2347  case SKINNY_OFFHOOK:
2348  return "SKINNY_OFFHOOK";
2349  case SKINNY_ONHOOK:
2350  return "SKINNY_ONHOOK";
2351  case SKINNY_RINGOUT:
2352  return "SKINNY_RINGOUT";
2353  case SKINNY_RINGIN:
2354  return "SKINNY_RINGIN";
2355  case SKINNY_CONNECTED:
2356  return "SKINNY_CONNECTED";
2357  case SKINNY_BUSY:
2358  return "SKINNY_BUSY";
2359  case SKINNY_CONGESTION:
2360  return "SKINNY_CONGESTION";
2361  case SKINNY_PROGRESS:
2362  return "SKINNY_PROGRESS";
2363  case SKINNY_HOLD:
2364  return "SKINNY_HOLD";
2365  case SKINNY_CALLWAIT:
2366  return "SKINNY_CALLWAIT";
2367  default:
2369  return "Unknown";
2370  snprintf(tmp, CALLSTATE2STR_BUFSIZE, "UNKNOWN-%d", ind);
2371  return tmp;
2372  }
2373 }
2374 
2375 #endif
2376 
2377 static int transmit_response_bysession(struct skinnysession *s, struct skinny_req *req)
2378 {
2379  int res = 0;
2380  unsigned long len;
2381 
2382  if (!s) {
2383  ast_log(LOG_WARNING, "Asked to transmit to a non-existent session!\n");
2384  return -1;
2385  }
2386 
2387  ast_mutex_lock(&s->lock);
2388 
2389  /* Don't optimize out assigning letohl() to len. It is necessary to guarantee that the comparison will always catch invalid values.
2390  * letohl() may or may not return a signed value depending upon which definition is used. */
2391  len = letohl(req->len);
2392  if (SKINNY_MAX_PACKET < len) {
2393  ast_log(LOG_WARNING, "transmit_response: the length of the request (%u) is out of bounds (%d)\n", letohl(req->len), SKINNY_MAX_PACKET);
2394  ast_mutex_unlock(&s->lock);
2395  return -1;
2396  }
2397 
2398  memset(s->outbuf, 0, sizeof(s->outbuf));
2399  memcpy(s->outbuf, req, skinny_header_size);
2400  memcpy(s->outbuf+skinny_header_size, &req->data, letohl(req->len));
2401 
2402  res = write(s->fd, s->outbuf, letohl(req->len)+8);
2403 
2404  if (res != letohl(req->len)+8) {
2405  ast_log(LOG_WARNING, "Transmit: write only sent %d out of %u bytes: %s\n", res, letohl(req->len)+8, strerror(errno));
2406  if (res == -1) {
2407  ast_log(LOG_WARNING, "Transmit: Skinny Client was lost, unregistering\n");
2408  end_session(s);
2409  }
2410 
2411  }
2412 
2413  ast_free(req);
2414  ast_mutex_unlock(&s->lock);
2415  return 1;
2416 }
2417 
2418 static void transmit_response(struct skinny_device *d, struct skinny_req *req)
2419 {
2421 }
2422 
2423 static void transmit_registerrej(struct skinnysession *s)
2424 {
2425  struct skinny_req *req;
2426  char name[16];
2427 
2428  if (!(req = req_alloc(sizeof(struct register_rej_message), REGISTER_REJ_MESSAGE)))
2429  return;
2430 
2431  memcpy(&name, req->data.reg.name, sizeof(name));
2432  snprintf(req->data.regrej.errMsg, sizeof(req->data.regrej.errMsg), "No Authority: %s", name);
2433 
2434  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting REGISTER_REJ_MESSAGE to UNKNOWN_DEVICE\n");
2436 }
2437 
2438 static void transmit_speaker_mode(struct skinny_device *d, int mode)
2439 {
2440  struct skinny_req *req;
2441 
2442  if (!(req = req_alloc(sizeof(struct set_speaker_message), SET_SPEAKER_MESSAGE)))
2443  return;
2444 
2445  req->data.setspeaker.mode = htolel(mode);
2446 
2447  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SET_SPEAKER_MESSAGE to %s, mode %d\n", d->name, mode);
2448  transmit_response(d, req);
2449 }
2450 
2451 static void transmit_microphone_mode(struct skinny_device *d, int mode)
2452 {
2453  struct skinny_req *req;
2454 
2455  if (!(req = req_alloc(sizeof(struct set_microphone_message), SET_MICROPHONE_MESSAGE)))
2456  return;
2457 
2458  req->data.setmicrophone.mode = htolel(mode);
2459 
2460  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SET_MICROPHONE_MESSAGE to %s, mode %d\n", d->name, mode);
2461  transmit_response(d, req);
2462 }
2463 
2464 //static void transmit_callinfo(struct skinny_subchannel *sub)
2465 static void transmit_callinfo(struct skinny_device *d, int instance, int callid,
2466  char *fromname, char *fromnum, char *toname, char *tonum, int calldirection, char *origtonum, char *origtoname)
2467 {
2468  struct skinny_req *req;
2469 
2470  if (!(req = req_alloc(sizeof(struct call_info_message), CALL_INFO_MESSAGE)))
2471  return;
2472 
2474  ast_copy_string(req->data.callinfo.callingParty, fromnum, sizeof(req->data.callinfo.callingParty));
2476  ast_copy_string(req->data.callinfo.calledParty, tonum, sizeof(req->data.callinfo.calledParty));
2477  if (origtoname) {
2479  }
2480  if (origtonum) {
2482  }
2483 
2484  req->data.callinfo.instance = htolel(instance);
2485  req->data.callinfo.reference = htolel(callid);
2486  req->data.callinfo.type = htolel(calldirection);
2487 
2488  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CALL_INFO_MESSAGE to %s, to %s(%s) from %s(%s), origto %s(%s) (dir=%d) on %s(%d)\n",
2489  d->name, toname, tonum, fromname, fromnum, origtoname, origtonum, calldirection, d->name, instance);
2490  transmit_response(d, req);
2491 }
2492 
2493 static void transmit_callinfo_variable(struct skinny_device *d, int instance, int callreference,
2494  char *fromname, char *fromnum, char *toname, char *tonum, int calldirection, char *origtonum, char *origtoname)
2495 {
2496  struct skinny_req *req;
2497  char *strptr;
2498  char *thestrings[13];
2499  int i;
2500  int callinfostrleft = MAXCALLINFOSTR;
2501 
2502  if (!(req = req_alloc(sizeof(struct call_info_message_variable), CALL_INFO_MESSAGE_VARIABLE)))
2503  return;
2504 
2505  req->data.callinfomessagevariable.instance = htolel(instance);
2506  req->data.callinfomessagevariable.callreference = htolel(callreference);
2507  req->data.callinfomessagevariable.calldirection = htolel(calldirection);
2508 
2514 
2515  thestrings[0] = fromnum;
2516  thestrings[1] = ""; /* Appears to be origfrom */
2517  if (calldirection == SKINNY_OUTGOING) {
2518  thestrings[2] = tonum;
2519  thestrings[3] = origtonum;
2520  } else {
2521  thestrings[2] = "";
2522  thestrings[3] = "";
2523  }
2524  thestrings[4] = "";
2525  thestrings[5] = "";
2526  thestrings[6] = "";
2527  thestrings[7] = "";
2528 
2529  thestrings[8] = "";
2530  thestrings[9] = fromname;
2531  thestrings[10] = toname;
2532  thestrings[11] = origtoname;
2533  thestrings[12] = "";
2534 
2536 
2537  for(i = 0; i < 13; i++) {
2538  if (thestrings[i]) {
2539  ast_copy_string(strptr, thestrings[i], callinfostrleft);
2540  strptr += strlen(thestrings[i]) + 1;
2541  callinfostrleft -= strlen(thestrings[i]) + 1;
2542  } else {
2543  ast_copy_string(strptr, "", callinfostrleft);
2544  strptr++;
2545  callinfostrleft--;
2546  }
2547  }
2548 
2549  req->len = req->len - (callinfostrleft & ~0x3);
2550 
2551  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CALL_INFO_MESSAGE_VARIABLE to %s, to %s(%s) from %s(%s), origto %s(%s) (dir=%d) on %s(%d)\n",
2552  d->name, toname, tonum, fromname, fromnum, origtoname, origtonum, calldirection, d->name, instance);
2553  transmit_response(d, req);
2554 }
2555 
2557 {
2558  struct ast_channel *ast;
2559  struct skinny_device *d;
2560  struct skinny_line *l;
2561  struct ast_party_id connected_id;
2562  char *fromname;
2563  char *fromnum;
2564  char *toname;
2565  char *tonum;
2566 
2567  if (!sub || !sub->owner || !sub->line || !sub->line->device) {
2568  return;
2569  }
2570 
2571  ast = sub->owner;
2572  l = sub->line;
2573  d = l->device;
2574  connected_id = ast_channel_connected_effective_id(ast);
2575 
2576  if (sub->calldirection == SKINNY_INCOMING) {
2578  fromname = S_COR(connected_id.name.valid, connected_id.name.str, "");
2579  fromnum = S_COR(connected_id.number.valid, connected_id.number.str, "");
2580  } else {
2581  fromname = "";
2582  fromnum = "";
2583  }
2584  toname = S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, "");
2585  tonum = S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, "");
2586  } else if (sub->calldirection == SKINNY_OUTGOING) {
2587  fromname = S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, "");
2588  fromnum = S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, "");
2589  toname = S_COR(ast_channel_connected(ast)->id.name.valid, ast_channel_connected(ast)->id.name.str, "");
2590  tonum = S_COR(ast_channel_connected(ast)->id.number.valid, ast_channel_connected(ast)->id.number.str, l->lastnumberdialed);
2591  } else {
2592  ast_verb(1, "Error sending Callinfo to %s(%d) - No call direction in sub\n", d->name, l->instance);
2593  return;
2594  }
2595 
2596  if (d->protocolversion < 17) {
2597  transmit_callinfo(d, l->instance, sub->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
2598  } else {
2599  transmit_callinfo_variable(d, l->instance, sub->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
2600  }
2601 }
2602 
2603 static void push_callinfo(struct skinny_subline *subline, struct skinny_subchannel *sub)
2604 {
2605  struct ast_channel *ast;
2606  struct skinny_device *d;
2607  struct skinny_line *l;
2608  struct ast_party_id connected_id;
2609  char *fromname;
2610  char *fromnum;
2611  char *toname;
2612  char *tonum;
2613 
2614  if (!sub || !sub->owner || !sub->line || !sub->line->device) {
2615  return;
2616  }
2617 
2618  ast = sub->owner;
2619  l = sub->line;
2620  d = l->device;
2621  connected_id = ast_channel_connected_effective_id(ast);
2622 
2623  if (sub->calldirection == SKINNY_INCOMING) {
2625  fromname = S_COR(connected_id.name.valid, connected_id.name.str, "");
2626  fromnum = S_COR(connected_id.number.valid, connected_id.number.str, "");
2627  } else {
2628  fromname = "";
2629  fromnum = "";
2630  }
2631  toname = S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, "");
2632  tonum = S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, "");
2633  } else if (sub->calldirection == SKINNY_OUTGOING) {
2634  fromname = S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, "");
2635  fromnum = S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, "");
2636  toname = S_COR(ast_channel_connected(ast)->id.name.valid, ast_channel_connected(ast)->id.name.str, "");
2637  tonum = S_COR(ast_channel_connected(ast)->id.number.valid, ast_channel_connected(ast)->id.number.str, l->lastnumberdialed);
2638  } else {
2639  ast_verb(1, "Error sending Callinfo to %s(%d) - No call direction in sub\n", d->name, l->instance);
2640  return;
2641  }
2642 
2643  if (d->protocolversion < 17) {
2644  transmit_callinfo(subline->line->device, subline->line->instance, subline->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
2645  } else {
2646  transmit_callinfo_variable(subline->line->device, subline->line->instance, subline->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
2647  }
2648 }
2649 
2651 {
2652  struct skinny_req *req;
2653  struct skinny_line *l = sub->line;
2654  struct ast_format *tmpfmt;
2655  unsigned int framing;
2656 
2658  return;
2659 
2660  tmpfmt = ast_format_cap_get_format(l->cap, 0);
2661  framing = ast_format_cap_get_format_framing(l->cap, tmpfmt);
2662 
2665  req->data.openreceivechannel.packets = htolel(framing);
2667  req->data.openreceivechannel.echo = htolel(0);
2669 
2670  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting OPEN_RECEIVE_CHANNEL_MESSAGE to %s, confid %u, partyid %u, ms %u, fmt %d, echo %d, brate %d\n",
2671  d->name, sub->callid, sub->callid, framing, codec_ast2skinny(tmpfmt), 0, 0);
2672 
2673  ao2_ref(tmpfmt, -1);
2674 
2675  transmit_response(d, req);
2676 }
2677 
2678 static void transmit_start_tone(struct skinny_device *d, int tone, int instance, int reference)
2679 {
2680  struct skinny_req *req;
2681  if (!(req = req_alloc(sizeof(struct start_tone_message), START_TONE_MESSAGE)))
2682  return;
2683  req->data.starttone.tone = htolel(tone);
2684  req->data.starttone.instance = htolel(instance);
2685  req->data.starttone.reference = htolel(reference);
2686 
2687  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting START_TONE_MESSAGE to %s, tone %d, inst %d, ref %d\n",
2688  d->name, tone, instance, reference);
2689  transmit_response(d, req);
2690 }
2691 
2692 static void transmit_stop_tone(struct skinny_device *d, int instance, int reference)
2693 {
2694  struct skinny_req *req;
2695  if (!(req = req_alloc(sizeof(struct stop_tone_message), STOP_TONE_MESSAGE)))
2696  return;
2697  req->data.stoptone.instance = htolel(instance);
2698  req->data.stoptone.reference = htolel(reference);
2699 
2700  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting STOP_TONE_MESSAGE to %s, inst %d, ref %d\n",
2701  d->name, instance, reference);
2702  transmit_response(d, req);
2703 }
2704 
2705 static int keyset_translatebitmask(int keyset, int intmask)
2706 {
2707  int extmask = 0;
2708  int x, y;
2709  const struct soft_key_definitions *softkeymode = soft_key_default_definitions;
2710 
2711  for(x = 0; x < ARRAY_LEN(soft_key_default_definitions); x++) {
2712  if (softkeymode[x].mode == keyset) {
2713  const uint8_t *defaults = softkeymode[x].defaults;
2714 
2715  for (y = 0; y < softkeymode[x].count; y++) {
2716  if (intmask & (1 << (defaults[y]))) {
2717  extmask |= (1 << ((y)));
2718  }
2719  }
2720  break;
2721  }
2722  }
2723 
2724  return extmask;
2725 }
2726 
2727 static void transmit_selectsoftkeys(struct skinny_device *d, int instance, int callid, int softkey, int mask)
2728 {
2729  struct skinny_req *req;
2730  int newmask;
2731 
2732  if (!(req = req_alloc(sizeof(struct select_soft_keys_message), SELECT_SOFT_KEYS_MESSAGE)))
2733  return;
2734 
2735  newmask = keyset_translatebitmask(softkey, mask);
2736  req->data.selectsoftkey.instance = htolel(instance);
2737  req->data.selectsoftkey.reference = htolel(callid);
2738  req->data.selectsoftkey.softKeySetIndex = htolel(softkey);
2739  req->data.selectsoftkey.validKeyMask = htolel(newmask);
2740 
2741  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SELECT_SOFT_KEYS_MESSAGE to %s, inst %d, callid %d, softkey %d, mask 0x%08x\n",
2742  d->name, instance, callid, softkey, (unsigned)newmask);
2743  transmit_response(d, req);
2744 }
2745 
2746 static void transmit_lamp_indication(struct skinny_device *d, int stimulus, int instance, int indication)
2747 {
2748  struct skinny_req *req;
2749 
2750  if (!(req = req_alloc(sizeof(struct set_lamp_message), SET_LAMP_MESSAGE)))
2751  return;
2752 
2753  req->data.setlamp.stimulus = htolel(stimulus);
2754  req->data.setlamp.stimulusInstance = htolel(instance);
2755  req->data.setlamp.deviceStimulus = htolel(indication);
2756 
2757  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SET_LAMP_MESSAGE to %s, stim %d, inst %d, ind %d\n",
2758  d->name, stimulus, instance, indication);
2759  transmit_response(d, req);
2760 }
2761 
2762 static void transmit_ringer_mode(struct skinny_device *d, int mode)
2763 {
2764  struct skinny_req *req;
2765 
2766  if (!(req = req_alloc(sizeof(struct set_ringer_message), SET_RINGER_MESSAGE)))
2767  return;
2768 
2769  req->data.setringer.ringerMode = htolel(mode);
2770  /* XXX okay, I don't quite know what this is, but here's what happens (on a 7960).
2771  Note: The phone will always show as ringing on the display.
2772 
2773  1: phone will audibly ring over and over
2774  2: phone will audibly ring only once
2775  any other value, will NOT cause the phone to audibly ring
2776  */
2777  req->data.setringer.unknown1 = htolel(1);
2778  /* XXX the value here doesn't seem to change anything. Must be higher than 0.
2779  Perhaps a packet capture can shed some light on this. */
2780  req->data.setringer.unknown2 = htolel(1);
2781 
2782  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SET_RINGER_MESSAGE to %s, mode %d, unk1 1, unk2 1\n",
2783  d->name, mode);
2784  transmit_response(d, req);
2785 }
2786 
2787 static void transmit_clear_display_message(struct skinny_device *d, int instance, int reference)
2788 {
2789  struct skinny_req *req;
2790  if (!(req = req_alloc(sizeof(struct clear_display_message), CLEAR_DISPLAY_MESSAGE)))
2791  return;
2792 
2793  //what do we want hear CLEAR_DISPLAY_MESSAGE or CLEAR_PROMPT_STATUS???
2794  //if we are clearing the display, it appears there is no instance and refernece info (size 0)
2795  //req->data.clearpromptstatus.lineInstance = instance;
2796  //req->data.clearpromptstatus.callReference = reference;
2797 
2798  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CLEAR_DISPLAY_MESSAGE to %s\n", d->name);
2799  transmit_response(d, req);
2800 }
2801 
2802 /* This function is not currently used, but will be (wedhorn)*/
2803 /* static void transmit_display_message(struct skinny_device *d, const char *text, int instance, int reference)
2804 {
2805  struct skinny_req *req;
2806 
2807  if (text == 0) {
2808  ast_verb(1, "Bug, Asked to display empty message\n");
2809  return;
2810  }
2811 
2812  if (!(req = req_alloc(sizeof(struct displaytext_message), DISPLAYTEXT_MESSAGE)))
2813  return;
2814 
2815  ast_copy_string(req->data.displaytext.text, text, sizeof(req->data.displaytext.text));
2816  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAYTEXT_MESSAGE to %s, text %s\n", d->name, text);
2817  transmit_response(d, req);
2818 } */
2819 
2820 static void transmit_displaynotify(struct skinny_device *d, const char *text, int t)
2821 {
2822  struct skinny_req *req;
2823 
2824  if (!(req = req_alloc(sizeof(struct display_notify_message), DISPLAY_NOTIFY_MESSAGE)))
2825  return;
2826 
2829 
2830  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_NOTIFY_MESSAGE to %s, text %s\n", d->name, text);
2831  transmit_response(d, req);
2832 }
2833 
2835 {
2836  struct skinny_req *req;
2837 
2838  if (!(req = req_alloc(sizeof(struct clear_prinotify_message), CLEAR_PRINOTIFY_MESSAGE)))
2839  return;
2840 
2841  req->data.clearprinotify.priority = htolel(priority);
2842 
2843  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CLEAR_PRINOTIFY_MESSAGE to %s, priority %d\n", d->name, priority);
2844  transmit_response(d, req);
2845 }
2846 
2847 static void _transmit_displayprinotify(struct skinny_device *d, const char *text, const char *extratext, int timeout, int priority)
2848 {
2849  struct skinny_req *req;
2850 
2851  if (!(req = req_alloc(sizeof(struct display_prinotify_message), DISPLAY_PRINOTIFY_MESSAGE)))
2852  return;
2853 
2854  req->data.displayprinotify.timeout = htolel(timeout);
2855  req->data.displayprinotify.priority = htolel(priority);
2856 
2857  if ((char)*text == '\200') {
2858  int octalstrlen = strlen(text);
2860  ast_copy_string(req->data.displayprinotify.text+octalstrlen, extratext, sizeof(req->data.displayprinotify.text)-octalstrlen);
2861  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PRINOTIFY_MESSAGE to %s, '\\%03o\\%03o', '%s', timeout=%d, priority=%d\n",
2862  d->name, (unsigned)*text, (unsigned)*(text+1), extratext, timeout, priority);
2863  } else {
2865  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PRINOTIFY_MESSAGE to %s, '%s', timeout=%d, priority=%d\n",
2866  d->name, text, timeout, priority);
2867  }
2868 
2869  transmit_response(d, req);
2870 }
2871 
2872 static void _transmit_displayprinotifyvar(struct skinny_device *d, const char *text, const char *extratext, int timeout, int priority)
2873 {
2874  struct skinny_req *req;
2875  int packetlen;
2876 
2878  return;
2879 
2880  req->data.displayprinotifyvar.timeout = htolel(timeout);
2881  req->data.displayprinotifyvar.priority = htolel(priority);
2882 
2883  if ((char)*text == '\200') {
2884  int octalstrlen = strlen(text);
2886  ast_copy_string(req->data.displayprinotifyvar.text+octalstrlen, extratext, sizeof(req->data.displayprinotifyvar.text)-octalstrlen);
2887  packetlen = req->len - MAXDISPLAYNOTIFYSTR + strlen(text) + strlen(extratext);
2888  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PRINOTIFY_MESSAGE_VARIABLE to %s, '\\%03o\\%03o', '%s', timeout=%d, priority=%d\n",
2889  d->name, (unsigned)*text, (unsigned)*(text+1), extratext, timeout, priority);
2890  } else {
2892  packetlen = req->len - MAXDISPLAYNOTIFYSTR + strlen(text);
2893  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PRINOTIFY_MESSAGE_VARIABLE to %s, '%s', timeout=%d, priority=%d\n",
2894  d->name, text, timeout, priority);
2895  }
2896 
2897  req->len = (packetlen & ~0x3) + 4;
2898 
2899  transmit_response(d, req);
2900 }
2901 
2902 static void send_displayprinotify(struct skinny_device *d, const char *text, const char *extratext, int timeout, int priority)
2903 {
2904  if (d->protocolversion < 17) {
2905  _transmit_displayprinotify(d, text, extratext, timeout, priority);
2906  } else {
2907  _transmit_displayprinotifyvar(d, text, extratext, timeout, priority);
2908  }
2909 }
2910 
2911 static void transmit_displaypromptstatus(struct skinny_device *d, const char *text, const char *extratext, int t, int instance, int callid)
2912 {
2913  struct skinny_req *req;
2914 
2916  return;
2917 
2919  req->data.displaypromptstatus.lineInstance = htolel(instance);
2921 
2922  if ((char)*text == '\200') {
2923  int octalstrlen = strlen(text);
2925  ast_copy_string(req->data.displaypromptstatus.promptMessage+octalstrlen, extratext, sizeof(req->data.displaypromptstatus.promptMessage)-octalstrlen);
2926  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PROMPT_STATUS_MESSAGE to %s, '\\%03o\\%03o', '%s'\n",
2927  d->name, (unsigned)*text, (unsigned)*(text+1), extratext);
2928  } else {
2930  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PROMPT_STATUS_MESSAGE to %s, '%s'\n",
2931  d->name, text);
2932  }
2933  transmit_response(d, req);
2934 }
2935 
2936 static void transmit_displaypromptstatusvar(struct skinny_device *d, const char *text, const char *extratext, int t, int instance, int callid)
2937 {
2938  struct skinny_req *req;
2939  int packetlen;
2940 
2942  return;
2943 
2946 
2947  if ((char)*text == '\200') {
2948  int octalstrlen = strlen(text);
2950  ast_copy_string(req->data.displaypromptstatusvar.promptMessage+octalstrlen, extratext, sizeof(req->data.displaypromptstatusvar.promptMessage)-octalstrlen);
2951  packetlen = req->len - MAXCALLINFOSTR + strlen(text) + strlen(extratext);
2952  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PROMPT_STATUS_MESSAGE_VARIABLE to %s, '\\%03o\\%03o', '%s'\n",
2953  d->name, (unsigned)*text, (unsigned)*(text+1), extratext);
2954  } else {
2956  packetlen = req->len - MAXCALLINFOSTR + strlen(text);
2957  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PROMPT_STATUS_MESSAGE_VARIABLE to %s, '%s'\n",
2958  d->name, text);
2959  }
2960  req->len = (packetlen & ~0x3) + 4;
2961 
2962  transmit_response(d, req);
2963 }
2964 
2965 static void send_displaypromptstatus(struct skinny_device *d, const char *text, const char *extratext, int t, int instance, int callid)
2966 {
2967  if (d->protocolversion < 17) {
2968  transmit_displaypromptstatus(d, text, extratext, t, instance, callid);
2969  } else {
2970  transmit_displaypromptstatusvar(d, text, extratext, t, instance, callid);
2971  }
2972 }
2973 
2974 static void transmit_clearpromptmessage(struct skinny_device *d, int instance, int callid)
2975 {
2976  struct skinny_req *req;
2977 
2978  if (!(req = req_alloc(sizeof(struct clear_prompt_message), CLEAR_PROMPT_MESSAGE)))
2979  return;
2980 
2981  req->data.clearpromptstatus.lineInstance = htolel(instance);
2982  req->data.clearpromptstatus.callReference = htolel(callid);
2983 
2984  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CLEAR_PROMPT_MESSAGE to %s, inst %d, callid %d\n",
2985  d->name, instance, callid);
2986  transmit_response(d, req);
2987 }
2988 
2989 static void transmit_dialednumber(struct skinny_device *d, const char *text, int instance, int callid)
2990 {
2991  struct skinny_req *req;
2992 
2993  if (!(req = req_alloc(sizeof(struct dialed_number_message), DIALED_NUMBER_MESSAGE)))
2994  return;
2995 
2997  req->data.dialednumber.lineInstance = htolel(instance);
2998  req->data.dialednumber.callReference = htolel(callid);
2999 
3000  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DIALED_NUMBER_MESSAGE to %s, num %s, inst %d, callid %d\n",
3001  d->name, text, instance, callid);
3002  transmit_response(d, req);
3003 }
3004 
3006 {
3007  struct skinny_req *req;
3008 
3010  return;
3011 
3014 
3015  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CLOSE_RECEIVE_CHANNEL_MESSAGE to %s, confid %d, callid %u\n",
3016  d->name, 0, sub->callid);
3017  transmit_response(d, req);
3018 }
3019 
3021 {
3022  struct skinny_req *req;
3023 
3025  return;
3026 
3027  req->data.stopmedia.conferenceId = htolel(0);
3029 
3030  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting STOP_MEDIA_TRANSMISSION_MESSAGE to %s, confid %d, passthrupartyid %u\n",
3031  d->name, 0, sub->callid);
3032  transmit_response(d, req);
3033 }
3034 
3035 static void transmit_startmediatransmission(struct skinny_device *d, struct skinny_subchannel *sub, struct sockaddr_in dest,
3036  struct ast_format *format, unsigned int framing)
3037 {
3038  struct skinny_req *req;
3039 
3040  if (d->protocolversion < 17) {
3042  return;
3045  req->data.startmedia_ip4.remoteIp = dest.sin_addr.s_addr;
3046  req->data.startmedia_ip4.remotePort = htolel(ntohs(dest.sin_port));
3047  req->data.startmedia_ip4.packetSize = htolel(framing);
3053  } else {
3055  return;
3058  memcpy(req->data.startmedia_ip6.remoteIp, &dest.sin_addr.s_addr, sizeof(dest.sin_addr.s_addr));
3059  req->data.startmedia_ip6.remotePort = htolel(ntohs(dest.sin_port));
3060  req->data.startmedia_ip6.packetSize = htolel(framing);
3066  }
3067 
3068  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting START_MEDIA_TRANSMISSION_MESSAGE to %s, callid %u, passthrupartyid %u, ip %s:%d, ms %u, fmt %d, prec 127\n",
3069  d->name, sub->callid, sub->callid, ast_inet_ntoa(dest.sin_addr), dest.sin_port, framing, codec_ast2skinny(format));
3070  transmit_response(d, req);
3071 }
3072 
3074 {
3075  struct skinny_req *req;
3076 
3078  return;
3079 
3080  req->data.activatecallplane.lineInstance = htolel(l->instance);
3081 
3082  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting ACTIVATE_CALL_PLANE_MESSAGE to %s, inst %d\n",
3083  d->name, l->instance);
3084  transmit_response(d, req);
3085 }
3086 
3087 static void transmit_callstate(struct skinny_device *d, int buttonInstance, unsigned callid, int state)
3088 {
3089  struct skinny_req *req;
3090 
3091  if (!(req = req_alloc(sizeof(struct call_state_message), CALL_STATE_MESSAGE)))
3092  return;
3093 
3094  req->data.callstate.callState = htolel(state);
3095  req->data.callstate.lineInstance = htolel(buttonInstance);
3096  req->data.callstate.callReference = htolel(callid);
3097 
3098  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CALL_STATE_MESSAGE to %s, state %s, inst %d, callid %u\n",
3099  d->name, callstate2str(state), buttonInstance, callid);
3100  transmit_response(d, req);
3101 }
3102 
3103 static void transmit_cfwdstate(struct skinny_device *d, struct skinny_line *l)
3104 {
3105  struct skinny_req *req;
3106  int anyon = 0;
3107 
3108  if (!(req = req_alloc(sizeof(struct forward_stat_message), FORWARD_STAT_MESSAGE)))
3109  return;
3110 
3111  if (l->cfwdtype & SKINNY_CFWD_ALL) {
3112  if (!ast_strlen_zero(l->call_forward_all)) {
3113  ast_copy_string(req->data.forwardstat.fwdallnum, l->call_forward_all, sizeof(req->data.forwardstat.fwdallnum));
3114  req->data.forwardstat.fwdall = htolel(1);
3115  anyon++;
3116  } else {
3117  req->data.forwardstat.fwdall = htolel(0);
3118  }
3119  }
3120  if (l->cfwdtype & SKINNY_CFWD_BUSY) {
3121  if (!ast_strlen_zero(l->call_forward_busy)) {
3122  ast_copy_string(req->data.forwardstat.fwdbusynum, l->call_forward_busy, sizeof(req->data.forwardstat.fwdbusynum));
3123  req->data.forwardstat.fwdbusy = htolel(1);
3124  anyon++;
3125  } else {
3126  req->data.forwardstat.fwdbusy = htolel(0);
3127  }
3128  }
3129  if (l->cfwdtype & SKINNY_CFWD_NOANSWER) {
3130  if (!ast_strlen_zero(l->call_forward_noanswer)) {
3131  ast_copy_string(req->data.forwardstat.fwdnoanswernum, l->call_forward_noanswer, sizeof(req->data.forwardstat.fwdnoanswernum));
3132  req->data.forwardstat.fwdnoanswer = htolel(1);
3133  anyon++;
3134  } else {
3135  req->data.forwardstat.fwdnoanswer = htolel(0);
3136  }
3137  }
3138  req->data.forwardstat.lineNumber = htolel(l->instance);
3139  if (anyon)
3141  else
3143 
3144  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting FORWARD_STAT_MESSAGE to %s, inst %d, all %s, busy %s, noans %s, acitve %d\n",
3145  d->name, l->instance, l->call_forward_all, l->call_forward_busy, l->call_forward_noanswer, anyon ? 7 : 0);
3146  transmit_response(d, req);
3147 }
3148 
3150 {
3151  struct skinny_req *req;
3152 
3154  return;
3155 
3159 
3160  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SPEED_DIAL_STAT_RES_MESSAGE to %s, inst %d, dir %s, display %s\n",
3161  d->name, sd->instance, sd->exten, sd->label);
3162  transmit_response(d, req);
3163 }
3164 
3165 //static void transmit_linestatres(struct skinny_device *d, struct skinny_line *l)
3166 static void transmit_linestatres(struct skinny_device *d, int instance)
3167 {
3168  struct skinny_req *req;
3169  struct skinny_line *l;
3170  struct skinny_speeddial *sd;
3171 
3172  if (!(req = req_alloc(sizeof(struct line_stat_res_message), LINE_STAT_RES_MESSAGE)))
3173  return;
3174 
3175  if ((l = find_line_by_instance(d, instance))) {
3176  req->data.linestat.lineNumber = letohl(l->instance);
3177  memcpy(req->data.linestat.lineDirNumber, l->name, sizeof(req->data.linestat.lineDirNumber));
3178  memcpy(req->data.linestat.lineDisplayName, l->label, sizeof(req->data.linestat.lineDisplayName));
3179  } else if ((sd = find_speeddial_by_instance(d, instance, 1))) {
3180  req->data.linestat.lineNumber = letohl(sd->instance);
3181  memcpy(req->data.linestat.lineDirNumber, sd->label, sizeof(req->data.linestat.lineDirNumber));
3182  memcpy(req->data.linestat.lineDisplayName, sd->label, sizeof(req->data.linestat.lineDisplayName));
3183  }
3184 
3185  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting LINE_STAT_RES_MESSAGE to %s, inst %d, num %s, label %s\n",
3186  d->name, l->instance, req->data.linestat.lineDirNumber, req->data.linestat.lineDisplayName);
3187  transmit_response(d, req);
3188 }
3189 
3191 {
3192  struct skinny_req *req;
3193  struct timeval now = ast_tvnow();
3194  struct ast_tm cmtime;
3195 
3196  if (!(req = req_alloc(sizeof(struct definetimedate_message), DEFINETIMEDATE_MESSAGE)))
3197  return;
3198 
3199  ast_localtime(&now, &cmtime, NULL);
3200  req->data.definetimedate.year = htolel(cmtime.tm_year+1900);
3201  req->data.definetimedate.month = htolel(cmtime.tm_mon+1);
3202  req->data.definetimedate.dayofweek = htolel(cmtime.tm_wday);
3203  req->data.definetimedate.day = htolel(cmtime.tm_mday);
3204  req->data.definetimedate.hour = htolel(cmtime.tm_hour);
3205  req->data.definetimedate.minute = htolel(cmtime.tm_min);
3206  req->data.definetimedate.seconds = htolel(cmtime.tm_sec);
3207  req->data.definetimedate.milliseconds = htolel(cmtime.tm_usec / 1000);
3208  req->data.definetimedate.timestamp = htolel(now.tv_sec);
3209 
3210  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DEFINETIMEDATE_MESSAGE to %s, date %u %u %u dow %u time %u:%u:%u.%u\n",
3213  transmit_response(d, req);
3214 }
3215 
3217 {
3218  struct skinny_req *req;
3219  if (!(req = req_alloc(sizeof(struct version_res_message), VERSION_RES_MESSAGE)))
3220  return;
3221 
3222  ast_copy_string(req->data.version.version, d->version_id, sizeof(req->data.version.version));
3223 
3224  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting VERSION_RES_MESSAGE to %s, version %s\n", d->name, d->version_id);
3225  transmit_response(d, req);
3226 }
3227 
3228 static void transmit_serverres(struct skinny_device *d)
3229 {
3230  struct skinny_req *req;
3231  if (!(req = req_alloc(sizeof(struct server_res_message), SERVER_RES_MESSAGE)))
3232  return;
3233 
3234  memcpy(req->data.serverres.server[0].serverName, ourhost,
3235  sizeof(req->data.serverres.server[0].serverName));
3237  req->data.serverres.serverIpAddr[0] = htolel(d->ourip.s_addr);
3238 
3239  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SERVER_RES_MESSAGE to %s, srvname %s %s:%d\n",
3240  d->name, ourhost, ast_inet_ntoa(d->ourip), ourport);
3241  transmit_response(d, req);
3242 }
3243 
3245 {
3246  struct skinny_req *req;
3247  int i;
3248  int x;
3249  int y;
3250  int keydefcount;
3251  const struct soft_key_definitions *softkeymode = soft_key_default_definitions;
3252 
3253  if (!(req = req_alloc(sizeof(struct soft_key_set_res_message), SOFT_KEY_SET_RES_MESSAGE)))
3254  return;
3255 
3256  SKINNY_DEBUG(DEBUG_TEMPLATE, 3, "Creating Softkey Template\n");
3257 
3258  keydefcount = ARRAY_LEN(soft_key_default_definitions);
3260  req->data.softkeysets.softKeySetCount = htolel(keydefcount);
3261  req->data.softkeysets.totalSoftKeySetCount = htolel(keydefcount);
3262  for (x = 0; x < keydefcount; x++) {
3263  const uint8_t *defaults = softkeymode->defaults;
3264  /* XXX I wanted to get the size of the array dynamically, but that wasn't wanting to work.
3265  This will have to do for now. */
3266  for (y = 0; y < softkeymode->count; y++) {
3267  for (i = 0; i < (sizeof(soft_key_template_default) / sizeof(struct soft_key_template_definition)); i++) {
3268  if (defaults[y] == i+1) {
3269  req->data.softkeysets.softKeySetDefinition[softkeymode->mode].softKeyTemplateIndex[y] = (i+1);
3270  req->data.softkeysets.softKeySetDefinition[softkeymode->mode].softKeyInfoIndex[y] = htoles(i+301);
3271  SKINNY_DEBUG(DEBUG_TEMPLATE, 4, "softKeySetDefinition : softKeyTemplateIndex: %d softKeyInfoIndex: %d\n",
3272  i+1, i+301);
3273  }
3274  }
3275  }
3276  softkeymode++;
3277  }
3278 
3279  SKINNY_DEBUG(DEBUG_PACKET | DEBUG_TEMPLATE, 3, "Transmitting SOFT_KEY_SET_RES_MESSAGE to %s, template data\n",
3280  d->name);
3281  transmit_response(d, req);
3282 }
3283 
3285 {
3286  struct skinny_req *req;
3287 
3289  return;
3290 
3292  req->data.softkeytemplate.softKeyCount = htolel(sizeof(soft_key_template_default) / sizeof(struct soft_key_template_definition));
3293  req->data.softkeytemplate.totalSoftKeyCount = htolel(sizeof(soft_key_template_default) / sizeof(struct soft_key_template_definition));
3295  soft_key_template_default,
3296  sizeof(soft_key_template_default));
3297 
3298  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SOFT_KEY_TEMPLATE_RES_MESSAGE to %s, offset 0, keycnt %u, totalkeycnt %u, template data\n",
3300  transmit_response(d, req);
3301 }
3302 
3303 static void transmit_reset(struct skinny_device *d, int fullrestart)
3304 {
3305  struct skinny_req *req;
3306 
3307  if (!(req = req_alloc(sizeof(struct reset_message), RESET_MESSAGE)))
3308  return;
3309 
3310  if (fullrestart)
3311  req->data.reset.resetType = 2;
3312  else
3313  req->data.reset.resetType = 1;
3314 
3315  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting RESET_MESSAGE to %s, type %s\n",
3316  d->name, (fullrestart) ? "Restarting" : "Resetting");
3317  transmit_response(d, req);
3318 }
3319 
3320 static void transmit_keepaliveack(struct skinnysession *s)
3321 {
3322  struct skinny_req *req;
3323 
3324  if (!(req = req_alloc(0, KEEP_ALIVE_ACK_MESSAGE)))
3325  return;
3326 
3327 #ifdef AST_DEVMODE
3328  {
3329  struct skinny_device *d = s->device;
3330  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting KEEP_ALIVE_ACK_MESSAGE to %s\n", (d ? d->name : "unregistered"));
3331  }
3332 #endif
3334 }
3335 
3337 {
3338  struct skinny_req *req;
3339 
3340  if (!(req = req_alloc(sizeof(struct register_ack_message), REGISTER_ACK_MESSAGE)))
3341  return;
3342 
3343  req->data.regack.res[0] = '0';
3344  req->data.regack.res[1] = '\0';
3346  memcpy(req->data.regack.dateTemplate, date_format, sizeof(req->data.regack.dateTemplate));
3347  req->data.regack.res2[0] = '0';
3348  req->data.regack.res2[1] = '\0';
3350 
3351 #ifdef AST_DEVMODE
3352  {
3353  short res = req->data.regack.res[0] << 8 | req->data.regack.res[1];
3354  unsigned int res2 = req->data.regack.res2[0] << 24 | req->data.regack.res2[1] << 16 | req->data.regack.res2[2] << 8 | req->data.regack.res2[3];
3355  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting REGISTER_ACK_MESSAGE to %s, keepalive %d, datetemplate %s, seckeepalive %d, res 0x%04x, res2 0x%08x\n",
3356  d->name, keep_alive, date_format, keep_alive, (unsigned)res, res2);
3357  }
3358 #endif
3359 
3360  transmit_response(d, req);
3361 }
3362 
3364 {
3365  struct skinny_req *req;
3366 
3367  if (!(req = req_alloc(0, CAPABILITIES_REQ_MESSAGE)))
3368  return;
3369 
3370  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CAPABILITIES_REQ_MESSAGE to %s\n", d->name);
3371  transmit_response(d, req);
3372 }
3373 
3374 static void transmit_backspace(struct skinny_device *d, int instance, unsigned callid)
3375 {
3376  struct skinny_req *req;
3377 
3378  if (!(req = req_alloc(sizeof(struct bksp_req_message), BKSP_REQ_MESSAGE)))
3379  return;
3380 
3381  req->data.bkspmessage.instance = htolel(instance);
3382  req->data.bkspmessage.callreference = htolel(callid);
3383 
3384  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting BKSP_REQ_MESSAGE to %s, inst %d, callid %u \n",
3385  d->name, instance, callid);
3386  transmit_response(d, req);
3387 }
3388 
3389 static void transmit_serviceurlstat(struct skinny_device *d, int instance)
3390 {
3391  struct skinny_req *req;
3392  struct skinny_serviceurl *surl;
3393 
3394  if (!(req = req_alloc(sizeof(struct serviceurl_stat_message), SERVICEURL_STAT_MESSAGE)))
3395  return;
3396 
3397  AST_LIST_TRAVERSE(&d->serviceurls, surl, list) {
3398  if (surl->instance == instance) {
3399  break;
3400  }
3401  }
3402 
3403  if (surl) {
3405  memcpy(req->data.serviceurlmessage.url, surl->url, sizeof(req->data.serviceurlmessage.url));
3406  }
3407  req->data.serviceurlmessage.instance = htolel(instance);
3408 
3409  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SERVICEURL_STAT_MESSAGE to %s, inst %d\n",
3410  d->name, instance);
3411  transmit_response(d, req);
3412 }
3413 
3414 static int skinny_extensionstate_cb(const char *context, const char *exten, struct ast_state_cb_info *info, void *data)
3415 {
3416  struct skinny_container *container = data;
3417  struct skinny_device *d = NULL;
3418  char hint[AST_MAX_EXTENSION];
3419  int state = info->exten_state;
3420 
3421  /* only interested in device state here */
3422  if (info->reason != AST_HINT_UPDATE_DEVICE) {
3423  return 0;
3424  }
3425 
3426  if (container->type == SKINNY_SDCONTAINER) {
3427  struct skinny_speeddial *sd = container->data;
3428  d = sd->parent;
3429 
3430  SKINNY_DEBUG(DEBUG_HINT, 3, "Got hint %s on speeddial %s\n", ast_extension_state2str(state), sd->label);
3431 
3432  if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, sd->context, sd->exten)) {
3433  /* If they are not registered, we will override notification and show no availability */
3437  return 0;
3438  }
3439  switch (state) {
3440  case AST_EXTENSION_DEACTIVATED: /* Retry after a while */
3441  case AST_EXTENSION_REMOVED: /* Extension is gone */
3442  SKINNY_DEBUG(DEBUG_HINT, 3, "Extension state: Watcher for hint %s %s. Notify Device %s\n",
3443  exten, state == AST_EXTENSION_DEACTIVATED ? "deactivated" : "removed", d->name);
3444  sd->stateid = -1;
3447  break;
3448  case AST_EXTENSION_RINGING:
3452  break;
3453  case AST_EXTENSION_BUSY: /* callstate = SKINNY_BUSY wasn't wanting to work - I'll settle for this */
3454  case AST_EXTENSION_INUSE:
3457  break;
3458  case AST_EXTENSION_ONHOLD:
3461  break;
3463  default:
3466  break;
3467  }
3468  }
3469  sd->laststate = state;
3470  } else if (container->type == SKINNY_SUBLINECONTAINER) {
3471  struct skinny_subline *subline = container->data;
3472  struct skinny_line *l = subline->line;
3473  d = l->device;
3474 
3475  SKINNY_DEBUG(DEBUG_HINT, 3, "Got hint %s on subline %s (%s@%s)\n", ast_extension_state2str(state), subline->name, exten, context);
3476 
3477  subline->extenstate = state;
3478 
3479  if (subline->callid == 0) {
3480  return 0;
3481  }
3482 
3483  switch (state) {
3484  case AST_EXTENSION_RINGING: /* Handled by normal ringin */
3485  break;
3486  case AST_EXTENSION_INUSE:
3487  if (subline->sub && (subline->sub->substate == SKINNY_CONNECTED)) { /* Device has a real call */
3488  transmit_callstate(d, l->instance, subline->callid, SKINNY_CONNECTED);
3489  transmit_selectsoftkeys(d, l->instance, subline->callid, KEYDEF_CONNECTED, KEYMASK_ALL);
3490  send_displaypromptstatus(d, OCTAL_CONNECTED, "", 0, l->instance, subline->callid);
3491  } else { /* Some other device has active call */
3492  transmit_callstate(d, l->instance, subline->callid, SKINNY_CALLREMOTEMULTILINE);
3494  send_displaypromptstatus(d, "In Use", "", 0, l->instance, subline->callid);
3495  }
3499  break;
3500  case AST_EXTENSION_ONHOLD:
3501  transmit_callstate(d, l->instance, subline->callid, SKINNY_HOLD);
3502  transmit_selectsoftkeys(d, l->instance, subline->callid, KEYDEF_SLAHOLD, KEYMASK_ALL);
3503  send_displaypromptstatus(d, "Hold", "", 0, l->instance, subline->callid);
3506  break;
3508  transmit_callstate(d, l->instance, subline->callid, SKINNY_ONHOOK);
3509  transmit_selectsoftkeys(d, l->instance, subline->callid, KEYDEF_ONHOOK, KEYMASK_ALL);
3510  transmit_clearpromptmessage(d, l->instance, subline->callid);
3513  subline->callid = 0;
3514  break;
3515  default:
3516  ast_log(LOG_WARNING, "AST_EXTENSION_STATE %s not configured\n", ast_extension_state2str(state));
3517  }
3518  } else {
3519  ast_log(LOG_WARNING, "Invalid data supplied to skinny_extensionstate_cb\n");
3520  }
3521 
3522  return 0;
3523 }
3524 
3525 static void update_connectedline(struct skinny_subchannel *sub, const void *data, size_t datalen)
3526 {
3527  struct ast_channel *c = sub->owner;
3528  struct skinny_line *l = sub->line;
3529  struct skinny_device *d = l->device;
3530 
3531  if (!d->session) {
3532  return;
3533  }
3534 
3535  if (sub->calldirection == SKINNY_OUTGOING && !sub->origtonum) {
3536  /* Do not set origtonum before here or origtoname won't be set */
3537  sub->origtonum = ast_strdup(sub->exten);
3538  if (ast_channel_connected(c)->id.name.valid) {
3539  sub->origtoname = ast_strdup(ast_channel_connected(c)->id.name.str);
3540  }
3541  }
3542 
3543  if (!ast_channel_caller(c)->id.number.valid
3545  || !ast_channel_connected(c)->id.number.valid
3547  return;
3548 
3549  SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Updating\n", sub->callid);
3550 
3551  send_callinfo(sub);
3552 }
3553 
3554 static void mwi_event_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg)
3555 {
3556  struct skinny_line *l = userdata;
3557  struct skinny_device *d = l->device;
3558  struct skinny_line *l2;
3559  int dev_msgs = 0;
3560 
3561  if (!d || !d->session) {
3562  return;
3563  }
3564 
3565  if (msg && ast_mwi_state_type() == stasis_message_type(msg)) {
3566  struct ast_mwi_state *mwi_state = stasis_message_data(msg);
3567  l->newmsgs = mwi_state->new_msgs;
3568  }
3569 
3570  if (l->newmsgs) {
3572  } else {
3574  }
3575 
3576  /* find out wether the device lamp should be on or off */
3577  AST_LIST_TRAVERSE(&d->lines, l2, list) {
3578  if (l2->newmsgs) {
3579  dev_msgs++;
3580  }
3581  }
3582 
3583  if (dev_msgs) {
3585  } else {
3587  }
3588  ast_verb(3, "Skinny mwi_event_cb found %d new messages\n", l->newmsgs);
3589 }
3590 
3591 /* I do not believe skinny can deal with video.
3592  Anyone know differently? */
3593 /* Yes, it can. Currently 7985 and Cisco VT Advantage do video. */
3595 {
3596  struct skinny_subchannel *sub = NULL;
3597 
3598  if (!(sub = ast_channel_tech_pvt(c)) || !(sub->vrtp))
3600 
3601  ao2_ref(sub->vrtp, +1);
3602  *instance = sub->vrtp;
3603 
3605 }
3606 
3608 {
3609  struct skinny_subchannel *sub = NULL;
3610  struct skinny_line *l;
3612 
3613  SKINNY_DEBUG(DEBUG_AUDIO, 4, "skinny_get_rtp_peer() Channel = %s\n", ast_channel_name(c));
3614 
3615  if (!(sub = ast_channel_tech_pvt(c)))
3617 
3618  skinny_locksub(sub);
3619 
3620  if (!(sub->rtp)){
3621  skinny_unlocksub(sub);
3623  }
3624 
3625  ao2_ref(sub->rtp, +1);
3626  *instance = sub->rtp;
3627 
3628  l = sub->line;
3629 
3630  if (!l->directmedia || l->nat){
3632  SKINNY_DEBUG(DEBUG_AUDIO, 4, "skinny_get_rtp_peer() Using AST_RTP_GLUE_RESULT_LOCAL \n");
3633  }
3634 
3635  skinny_unlocksub(sub);
3636 
3637  return res;
3638 
3639 }
3640 
3641 static int skinny_set_rtp_peer(struct ast_channel *c, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, struct ast_rtp_instance *trtp, const struct ast_format_cap *codecs, int nat_active)
3642 {
3643  struct skinny_subchannel *sub;
3644  struct skinny_line *l;
3645  struct skinny_device *d;
3646  struct sockaddr_in us = { 0, };
3647  struct sockaddr_in them = { 0, };
3648  struct ast_sockaddr them_tmp;
3649  struct ast_sockaddr us_tmp;
3650 
3651  sub = ast_channel_tech_pvt(c);
3652 
3653  if (ast_channel_state(c) != AST_STATE_UP)
3654  return 0;
3655 
3656  if (!sub) {
3657  return -1;
3658  }
3659 
3660  l = sub->line;
3661  d = l->device;
3662 
3663  if (rtp){
3664  struct ast_format *tmpfmt;
3665  unsigned int framing;
3666  ast_rtp_instance_get_remote_address(rtp, &them_tmp);
3667  ast_sockaddr_to_sin(&them_tmp, &them);
3668 
3669  /* Shutdown any early-media or previous media on re-invite */
3671 
3672  SKINNY_DEBUG(DEBUG_AUDIO, 4, "Peerip = %s:%d\n", ast_inet_ntoa(them.sin_addr), ntohs(them.sin_port));
3673 
3674  tmpfmt = ast_format_cap_get_format(l->cap, 0);
3675  framing = ast_format_cap_get_format_framing(l->cap, tmpfmt);
3676 
3677  SKINNY_DEBUG(DEBUG_AUDIO, 4, "Setting payloadType to '%s' (%u ms)\n", ast_format_get_name(tmpfmt), framing);
3678 
3679  if (!(l->directmedia) || (l->nat)){
3680  ast_rtp_instance_get_local_address(rtp, &us_tmp);
3681  ast_sockaddr_to_sin(&us_tmp, &us);
3682  us.sin_addr.s_addr = us.sin_addr.s_addr ? us.sin_addr.s_addr : d->ourip.s_addr;
3683  transmit_startmediatransmission(d, sub, us, tmpfmt, framing);
3684  } else {
3685  transmit_startmediatransmission(d, sub, them, tmpfmt, framing);
3686  }
3687 
3688  ao2_ref(tmpfmt, -1);
3689 
3690  return 0;
3691  }
3692  /* Need a return here to break the bridge */
3693  return 0;
3694 }
3695 
3697  .type = "Skinny",
3698  .get_rtp_info = skinny_get_rtp_peer,
3699  .get_vrtp_info = skinny_get_vrtp_peer,
3700  .update_peer = skinny_set_rtp_peer,
3701 };
3702 
3703 #ifdef AST_DEVMODE
3704 static char *skinny_debugs(void)
3705 {
3706  char *ptr;
3707  int posn = 0;
3708 
3709  ptr = dbgcli_buf;
3710  ptr[0] = '\0';
3711  if (skinnydebug & DEBUG_GENERAL) {
3712  strcpy(ptr, "general "); /* SAFE */
3713  posn += 8;
3714  ptr += 8;
3715  }
3716  if (skinnydebug & DEBUG_SUB) {
3717  strcpy(ptr, "sub "); /* SAFE */
3718  posn += 4;
3719  ptr += 4;
3720  }
3721  if (skinnydebug & DEBUG_AUDIO) {
3722  strcpy(ptr, "audio "); /* SAFE */
3723  posn += 6;
3724  ptr += 6;
3725  }
3726  if (skinnydebug & DEBUG_PACKET) {
3727  strcpy(ptr, "packet "); /* SAFE */
3728  posn += 7;
3729  ptr += 7;
3730  }
3731  if (skinnydebug & DEBUG_LOCK) {
3732  strcpy(ptr, "lock "); /* SAFE */
3733  posn += 5;
3734  ptr += 5;
3735  }
3736  if (skinnydebug & DEBUG_TEMPLATE) {
3737  strcpy(ptr, "template "); /* SAFE */
3738  posn += 9;
3739  ptr += 9;
3740  }
3741  if (skinnydebug & DEBUG_THREAD) {
3742  strcpy(ptr, "thread "); /* SAFE */
3743  posn += 7;
3744  ptr += 7;
3745  }
3746  if (skinnydebug & DEBUG_HINT) {
3747  strcpy(ptr, "hint "); /* SAFE */
3748  posn += 5;
3749  ptr += 5;
3750  }
3751  if (skinnydebug & DEBUG_KEEPALIVE) {
3752  strcpy(ptr, "keepalive "); /* SAFE */
3753  posn += 10;
3754  ptr += 10;
3755  }
3756  if (posn > 0) {
3757  strncpy(--ptr, "\0", 1);
3758  }
3759  return dbgcli_buf;
3760 }
3761 
3762 static char *complete_skinny_debug(const char *line, const char *word, int pos, int state)
3763 {
3764  const char *debugOpts[]={ "all","audio","hint","keepalive","lock","off","packet","show","sub","template","thread",NULL };
3765  char *wordptr = (char *)word;
3766  char buf[32];
3767  char *bufptr = buf;
3768  int buflen = sizeof(buf);
3769  int wordlen;
3770  int which = 0;
3771  int i = 0;
3772 
3773  if (*word == '+' || *word == '-' || *word == '!') {
3774  *bufptr = *word;
3775  wordptr++;
3776  bufptr++;
3777  buflen--;
3778  }
3779  wordlen = strlen(wordptr);
3780 
3781  while (debugOpts[i]) {
3782  if (!strncasecmp(wordptr, debugOpts[i], wordlen) && ++which > state) {
3783  ast_copy_string(bufptr, debugOpts[i], buflen);
3784  return ast_strdup(buf);
3785  }
3786  i++;
3787  }
3788 
3789  return NULL;
3790 }
3791 
3792 static char *handle_skinny_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3793 {
3794  int i;
3795  int result = 0;
3796  const char *arg;
3797  int bitmask;
3798  int negate;
3799 
3800  switch (cmd) {
3801  case CLI_INIT:
3802  e->command = "skinny debug";
3803  e->usage =
3804  "Usage: skinny debug {audio|hint|keepalive|lock|off|packet|show|sub|template|thread}\n"
3805  " Enables/Disables various Skinny debugging messages\n";
3806  return NULL;
3807  case CLI_GENERATE:
3808  return complete_skinny_debug(a->line, a->word, a->pos, a->n);
3809 
3810  }
3811 
3812  if (a->argc < 3)
3813  return CLI_SHOWUSAGE;
3814 
3815  if (a->argc == 3 && !strncasecmp(a->argv[e->args-1], "show", 4)) {
3816  ast_cli(a->fd, "Skinny Debugging - %s\n", skinny_debugs());
3817  return CLI_SUCCESS;
3818  }
3819 
3820  for(i = e->args; i < a->argc; i++) {
3821  result++;
3822  arg = a->argv[i];
3823 
3824  if (!strncasecmp(arg, "off", 3)) {
3825  skinnydebug = 0;
3826  continue;
3827  }
3828 
3829  if (!strncasecmp(arg, "all", 3)) {
3831  continue;
3832  }
3833 
3834  if (!strncasecmp(arg, "-", 1) || !strncasecmp(arg, "!", 1)) {
3835  negate = 1;
3836  arg++;
3837  } else if (!strncasecmp(arg, "+", 1)) {
3838  negate = 0;
3839  arg++;
3840  } else {
3841  negate = 0;
3842  }
3843 
3844  if (!strncasecmp(arg, "general", 7)) {
3845  bitmask = DEBUG_GENERAL;
3846  } else if (!strncasecmp(arg, "sub", 3)) {
3847  bitmask = DEBUG_SUB;
3848  } else if (!strncasecmp(arg, "packet", 6)) {
3849  bitmask = DEBUG_PACKET;
3850  } else if (!strncasecmp(arg, "audio", 5)) {
3851  bitmask = DEBUG_AUDIO;
3852  } else if (!strncasecmp(arg, "lock", 4)) {
3853  bitmask = DEBUG_LOCK;
3854  } else if (!strncasecmp(arg, "template", 8)) {
3855  bitmask = DEBUG_TEMPLATE;
3856  } else if (!strncasecmp(arg, "thread", 6)) {
3857  bitmask = DEBUG_THREAD;
3858  } else if (!strncasecmp(arg, "hint", 4)) {
3859  bitmask = DEBUG_HINT;
3860  } else if (!strncasecmp(arg, "keepalive", 9)) {
3861  bitmask = DEBUG_KEEPALIVE;
3862  } else {
3863  ast_cli(a->fd, "Skinny Debugging - option '%s' unknown\n", a->argv[i]);
3864  result--;
3865  continue;
3866  }
3867 
3868  if (negate) {
3869  skinnydebug &= ~bitmask;
3870  } else {
3871  skinnydebug |= bitmask;
3872  }
3873  }
3874  if (result) {
3875  ast_cli(a->fd, "Skinny Debugging - %s\n", skinnydebug ? skinny_debugs() : "off");
3876  return CLI_SUCCESS;
3877  } else {
3878  return CLI_SHOWUSAGE;
3879  }
3880 }
3881 #endif
3882 
3883 static char *handle_skinny_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3884 {
3885  switch (cmd) {
3886  case CLI_INIT:
3887  e->command = "skinny reload";
3888  e->usage =
3889  "Usage: skinny reload\n"
3890  " Reloads the chan_skinny configuration\n";
3891  return NULL;
3892  case CLI_GENERATE:
3893  return NULL;
3894  }
3895 
3896  if (a->argc != e->args)
3897  return CLI_SHOWUSAGE;
3898 
3899  skinny_reload();
3900  return CLI_SUCCESS;
3901 
3902 }
3903 
3904 static char *complete_skinny_devices(const char *word, int state)
3905 {
3906  struct skinny_device *d;
3907  int wordlen = strlen(word), which = 0;
3908 
3910  if (!strncasecmp(word, d->name, wordlen) && ++which > state) {
3911  return ast_strdup(d->name);
3912  }
3913  }
3914 
3915  return NULL;
3916 }
3917 
3918 static char *complete_skinny_show_device(const char *line, const char *word, int pos, int state)
3919 {
3920  return (pos == 3 ? complete_skinny_devices(word, state) : NULL);
3921 }
3922 
3923 static char *complete_skinny_reset(const char *line, const char *word, int pos, int state)
3924 {
3925  if (pos == 2) {
3926  static const char * const completions[] = { "all", NULL };
3927  char *ret = ast_cli_complete(word, completions, state);
3928  if (!ret) {
3929  ret = complete_skinny_devices(word, state - 1);
3930  }
3931  return ret;
3932  } else if (pos == 3) {
3933  static const char * const completions[] = { "restart", NULL };
3934  return ast_cli_complete(word, completions, state);
3935  }
3936 
3937  return NULL;
3938 }
3939 
3940 static char *complete_skinny_show_line(const char *line, const char *word, int pos, int state)
3941 {
3942  if (pos == 3) {
3943  struct skinny_device *d;
3944  struct skinny_line *l;
3945  int wordlen = strlen(word), which = 0;
3946 
3948  AST_LIST_TRAVERSE(&d->lines, l, list) {
3949  if (!strncasecmp(word, l->name, wordlen) && ++which > state) {
3950  return ast_strdup(l->name);
3951  }
3952  }
3953  }
3954  } else if (pos == 4) {
3955  static const char * const completions[] = { "on", NULL };
3956  return ast_cli_complete(word, completions, state);
3957  } else if (pos == 5) {
3958  return complete_skinny_devices(word, state);
3959  }
3960 
3961  return NULL;
3962 }
3963 
3964 static char *handle_skinny_reset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3965 {
3966  struct skinny_device *d;
3967 
3968  switch (cmd) {
3969  case CLI_INIT:
3970  e->command = "skinny reset";
3971  e->usage =
3972  "Usage: skinny reset <DeviceId|DeviceName|all> [restart]\n"
3973  " Causes a Skinny device to reset itself, optionally with a full restart\n";
3974  return NULL;
3975  case CLI_GENERATE:
3976  return complete_skinny_reset(a->line, a->word, a->pos, a->n);
3977  }
3978 
3979  if (a->argc < 3 || a->argc > 4)
3980  return CLI_SHOWUSAGE;
3981 
3984  int resetonly = 1;
3985  if (!strcasecmp(a->argv[2], d->id) || !strcasecmp(a->argv[2], d->name) || !strcasecmp(a->argv[2], "all")) {
3986  if (!(d->session))
3987  continue;
3988 
3989  if (a->argc == 4 && !strcasecmp(a->argv[3], "restart"))
3990  resetonly = 0;
3991 
3992  transmit_reset(d, resetonly);
3993  }
3994  }
3996  return CLI_SUCCESS;
3997 }
3998 
3999 static char *device2str(int type)
4000 {
4001  char *tmp;
4002 
4003  switch (type) {
4004  case SKINNY_DEVICE_NONE:
4005  return "No Device";
4007  return "30SP Plus";
4009  return "12SP Plus";
4010  case SKINNY_DEVICE_12SP:
4011  return "12SP";
4012  case SKINNY_DEVICE_12:
4013  return "12";
4014  case SKINNY_DEVICE_30VIP:
4015  return "30VIP";
4016  case SKINNY_DEVICE_7910:
4017  return "7910";
4018  case SKINNY_DEVICE_7960:
4019  return "7960";
4020  case SKINNY_DEVICE_7940:
4021  return "7940";
4022  case SKINNY_DEVICE_7935:
4023  return "7935";
4024  case SKINNY_DEVICE_ATA186:
4025  return "ATA186";
4026  case SKINNY_DEVICE_7941:
4027  return "7941";
4028  case SKINNY_DEVICE_7971:
4029  return "7971";
4030  case SKINNY_DEVICE_7914:
4031  return "7914";
4032  case SKINNY_DEVICE_7985:
4033  return "7985";
4034  case SKINNY_DEVICE_7911:
4035  return "7911";
4036  case SKINNY_DEVICE_7961GE:
4037  return "7961GE";
4038  case SKINNY_DEVICE_7941GE:
4039  return "7941GE";
4040  case SKINNY_DEVICE_7931:
4041  return "7931";
4042  case SKINNY_DEVICE_7921:
4043  return "7921";
4044  case SKINNY_DEVICE_7906:
4045  return "7906";
4046  case SKINNY_DEVICE_7962:
4047  return "7962";
4048  case SKINNY_DEVICE_7937:
4049  return "7937";
4050  case SKINNY_DEVICE_7942:
4051  return "7942";
4052  case SKINNY_DEVICE_7945:
4053  return "7945";
4054  case SKINNY_DEVICE_7965:
4055  return "7965";
4056  case SKINNY_DEVICE_7975:
4057  return "7975";
4058  case SKINNY_DEVICE_7905:
4059  return "7905";
4060  case SKINNY_DEVICE_7920:
4061  return "7920";
4062  case SKINNY_DEVICE_7970:
4063  return "7970";
4064  case SKINNY_DEVICE_7912:
4065  return "7912";
4066  case SKINNY_DEVICE_7902:
4067  return "7902";
4068  case SKINNY_DEVICE_CIPC:
4069  return "IP Communicator";
4070  case SKINNY_DEVICE_7961:
4071  return "7961";
4072  case SKINNY_DEVICE_7936:
4073  return "7936";
4075  return "SCCPGATEWAY_AN";
4077  return "SCCPGATEWAY_BRI";
4078  case SKINNY_DEVICE_UNKNOWN:
4079  return "Unknown";
4080  default:
4082  return "Unknown";
4083  snprintf(tmp, DEVICE2STR_BUFSIZE, "UNKNOWN-%d", type);
4084  return tmp;
4085  }
4086 }
4087 
4088 static char *_skinny_show_devices(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char * const *argv)
4089 {
4090  struct skinny_device *d;
4091  struct skinny_line *l;
4092  const char *id;
4093  char idtext[256] = "";
4094  int total_devices = 0;
4095 
4096  if (s) { /* Manager - get ActionID */
4097  id = astman_get_header(m, "ActionID");
4098  if (!ast_strlen_zero(id))
4099  snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
4100  }
4101 
4102  switch (argc) {
4103  case 3:
4104  break;
4105  default:
4106  return CLI_SHOWUSAGE;
4107  }
4108 
4109  if (!s) {
4110  ast_cli(fd, "Name DeviceId IP Type R NL\n");
4111  ast_cli(fd, "-------------------- ---------------- --------------- --------------- - --\n");
4112  }
4113 
4116  int numlines = 0;
4117  total_devices++;
4118  AST_LIST_TRAVERSE(&d->lines, l, list) {
4119  numlines++;
4120  }
4121  if (!s) {
4122  ast_cli(fd, "%-20s %-16s %-15s %-15s %c %2d\n",
4123  d->name,
4124  d->id,
4125  d->session?ast_inet_ntoa(d->session->sin.sin_addr):"",
4126  device2str(d->type),
4127  d->session?'Y':'N',
4128  numlines);
4129  } else {
4130  astman_append(s,
4131  "Event: DeviceEntry\r\n%s"
4132  "Channeltype: SKINNY\r\n"
4133  "ObjectName: %s\r\n"
4134  "ChannelObjectType: device\r\n"
4135  "DeviceId: %s\r\n"
4136  "IPaddress: %s\r\n"
4137  "Type: %s\r\n"
4138  "Devicestatus: %s\r\n"
4139  "NumberOfLines: %d\r\n",
4140  idtext,
4141  d->name,
4142  d->id,
4143  d->session?ast_inet_ntoa(d->session->sin.sin_addr):"-none-",
4144  device2str(d->type),
4145  d->session?"registered":"unregistered",
4146  numlines);
4147  }
4148  }
4150 
4151  if (total)
4152  *total = total_devices;
4153 
4154  return CLI_SUCCESS;
4155 }
4156 
4157 /*! \brief Show SKINNY devices in the manager API */
4158 /* Inspired from chan_sip */
4159 static int manager_skinny_show_devices(struct mansession *s, const struct message *m)
4160 {
4161  const char *a[] = {"skinny", "show", "devices"};
4162  int total = 0;
4163 
4164  astman_send_listack(s, m, "Device status list will follow", "start");
4165 
4166  /* List the devices in separate manager events */
4167  _skinny_show_devices(-1, &total, s, m, 3, a);
4168 
4169  /* Send final confirmation */
4170  astman_send_list_complete_start(s, m, "DevicelistComplete", total);
4172  return 0;
4173 }
4174 
4175 static char *handle_skinny_show_devices(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
4176 {
4177 
4178  switch (cmd) {
4179  case CLI_INIT:
4180  e->command = "skinny show devices";
4181  e->usage =
4182  "Usage: skinny show devices\n"
4183  " Lists all devices known to the Skinny subsystem.\n";
4184  return NULL;
4185  case CLI_GENERATE:
4186  return NULL;
4187  }
4188 
4189  return _skinny_show_devices(a->fd, NULL, NULL, NULL, a->argc, a->argv);
4190 }
4191 
4192 static char *_skinny_show_device(int type, int fd, struct mansession *s, const struct message *m, int argc, const char * const *argv)
4193 {
4194  struct skinny_device *d;
4195  struct skinny_line *l;
4196  struct skinny_speeddial *sd;
4197  struct skinny_addon *sa;
4198  struct skinny_serviceurl *surl;
4199  struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
4200 
4201  if (argc < 4) {
4202  return CLI_SHOWUSAGE;
4203  }
4204 
4206  AST_LIST_TRAVERSE(&devices, d, list) {
4207  if (!strcasecmp(argv[3], d->id) || !strcasecmp(argv[3], d->name)) {
4208  int numlines = 0, numaddons = 0, numspeeddials = 0, numserviceurls = 0;
4209 
4210  AST_LIST_TRAVERSE(&d->lines, l, list){
4211  numlines++;
4212  }
4213 
4214  AST_LIST_TRAVERSE(&d->addons, sa, list) {
4215  numaddons++;
4216  }
4217 
4218  AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
4219  numspeeddials++;
4220  }
4221 
4222  AST_LIST_TRAVERSE(&d->serviceurls, surl, list) {
4223  numserviceurls++;
4224  }
4225 
4226  if (type == 0) { /* CLI */
4227  ast_cli(fd, "Name: %s\n", d->name);
4228  ast_cli(fd, "Id: %s\n", d->id);
4229  ast_cli(fd, "version: %s\n", S_OR(d->version_id, "Unknown"));
4230  ast_cli(fd, "Ip address: %s\n", (d->session ? ast_inet_ntoa(d->session->sin.sin_addr) : "Unknown"));
4231  ast_cli(fd, "Port: %d\n", (d->session ? ntohs(d->session->sin.sin_port) : 0));
4232  ast_cli(fd, "Device Type: %s\n", device2str(d->type));
4233  ast_cli(fd, "Conf Codecs: %s\n", ast_format_cap_get_names(d->confcap, &codec_buf));
4234  ast_cli(fd, "Neg Codecs: %s\n", ast_format_cap_get_names(d->cap, &codec_buf));
4235  ast_cli(fd, "Registered: %s\n", (d->session ? "Yes" : "No"));
4236  ast_cli(fd, "Lines: %d\n", numlines);
4237  AST_LIST_TRAVERSE(&d->lines, l, list) {
4238  ast_cli(fd, " %s (%s)\n", l->name, l->label);
4239  }
4240  ast_cli(fd, "Addons: %d\n", numaddons);
4241  AST_LIST_TRAVERSE(&d->addons, sa, list) {
4242  ast_cli(fd, " %s\n", sa->type);
4243  }
4244  ast_cli(fd, "Speeddials: %d\n", numspeeddials);
4245  AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
4246  ast_cli(fd, " %s (%s) ishint: %d\n", sd->exten, sd->label, sd->isHint);
4247  }
4248  ast_cli(fd, "ServiceURLs: %d\n", numserviceurls);
4249  AST_LIST_TRAVERSE(&d->