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