Asterisk - The Open Source Telephony Project  GIT-master-a24979a
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 int skinnysock = -1;
1239 static pthread_t accept_t;
1240 static int callnums = 1;
1241 
1242 #define SKINNY_DEVICE_UNKNOWN -1
1243 #define SKINNY_DEVICE_NONE 0
1244 #define SKINNY_DEVICE_30SPPLUS 1
1245 #define SKINNY_DEVICE_12SPPLUS 2
1246 #define SKINNY_DEVICE_12SP 3
1247 #define SKINNY_DEVICE_12 4
1248 #define SKINNY_DEVICE_30VIP 5
1249 #define SKINNY_DEVICE_7910 6
1250 #define SKINNY_DEVICE_7960 7
1251 #define SKINNY_DEVICE_7940 8
1252 #define SKINNY_DEVICE_7935 9
1253 #define SKINNY_DEVICE_ATA186 12 /* Cisco ATA-186 */
1254 #define SKINNY_DEVICE_7941 115
1255 #define SKINNY_DEVICE_7971 119
1256 #define SKINNY_DEVICE_7914 124 /* Expansion module */
1257 #define SKINNY_DEVICE_7985 302
1258 #define SKINNY_DEVICE_7911 307
1259 #define SKINNY_DEVICE_7961GE 308
1260 #define SKINNY_DEVICE_7941GE 309
1261 #define SKINNY_DEVICE_7931 348
1262 #define SKINNY_DEVICE_7921 365
1263 #define SKINNY_DEVICE_7906 369
1264 #define SKINNY_DEVICE_7962 404 /* Not found */
1265 #define SKINNY_DEVICE_7937 431
1266 #define SKINNY_DEVICE_7942 434
1267 #define SKINNY_DEVICE_7945 435
1268 #define SKINNY_DEVICE_7965 436
1269 #define SKINNY_DEVICE_7975 437
1270 #define SKINNY_DEVICE_7905 20000
1271 #define SKINNY_DEVICE_7920 30002
1272 #define SKINNY_DEVICE_7970 30006
1273 #define SKINNY_DEVICE_7912 30007
1274 #define SKINNY_DEVICE_7902 30008
1275 #define SKINNY_DEVICE_CIPC 30016 /* Cisco IP Communicator */
1276 #define SKINNY_DEVICE_7961 30018
1277 #define SKINNY_DEVICE_7936 30019
1278 #define SKINNY_DEVICE_SCCPGATEWAY_AN 30027 /* Analog gateway */
1279 #define SKINNY_DEVICE_SCCPGATEWAY_BRI 30028 /* BRI gateway */
1280 
1281 #define SKINNY_SPEAKERON 1
1282 #define SKINNY_SPEAKEROFF 2
1283 
1284 #define SKINNY_MICON 1
1285 #define SKINNY_MICOFF 2
1286 
1287 #define SKINNY_OFFHOOK 1
1288 #define SKINNY_ONHOOK 2
1289 #define SKINNY_RINGOUT 3
1290 #define SKINNY_RINGIN 4
1291 #define SKINNY_CONNECTED 5
1292 #define SKINNY_BUSY 6
1293 #define SKINNY_CONGESTION 7
1294 #define SKINNY_HOLD 8
1295 #define SKINNY_CALLWAIT 9
1296 #define SKINNY_TRANSFER 10
1297 #define SKINNY_PARK 11
1298 #define SKINNY_PROGRESS 12
1299 #define SKINNY_CALLREMOTEMULTILINE 13
1300 #define SKINNY_INVALID 14
1301 
1302 #define SKINNY_INCOMING 1
1303 #define SKINNY_OUTGOING 2
1304 
1305 #define SKINNY_SILENCE 0x00 /* Note sure this is part of the protocol, remove? */
1306 #define SKINNY_DIALTONE 0x21
1307 #define SKINNY_BUSYTONE 0x23
1308 #define SKINNY_ALERT 0x24
1309 #define SKINNY_REORDER 0x25
1310 #define SKINNY_CALLWAITTONE 0x2D
1311 #define SKINNY_ZIPZIP 0x31
1312 #define SKINNY_ZIP 0x32
1313 #define SKINNY_BEEPBONK 0x33
1314 #define SKINNY_BARGIN 0x43
1315 #define SKINNY_NOTONE 0x7F
1316 
1317 #define SKINNY_LAMP_OFF 1
1318 #define SKINNY_LAMP_ON 2
1319 #define SKINNY_LAMP_WINK 3
1320 #define SKINNY_LAMP_FLASH 4
1321 #define SKINNY_LAMP_BLINK 5
1322 
1323 #define SKINNY_RING_OFF 1
1324 #define SKINNY_RING_INSIDE 2
1325 #define SKINNY_RING_OUTSIDE 3
1326 #define SKINNY_RING_FEATURE 4
1327 
1328 #define SKINNY_CFWD_ALL (1 << 0)
1329 #define SKINNY_CFWD_BUSY (1 << 1)
1330 #define SKINNY_CFWD_NOANSWER (1 << 2)
1331 
1332 /* Skinny rtp stream modes. Do we really need this? */
1333 #define SKINNY_CX_SENDONLY 0
1334 #define SKINNY_CX_RECVONLY 1
1335 #define SKINNY_CX_SENDRECV 2
1336 #define SKINNY_CX_CONF 3
1337 #define SKINNY_CX_CONFERENCE 3
1338 #define SKINNY_CX_MUTE 4
1339 #define SKINNY_CX_INACTIVE 4
1340 
1341 #if 0
1342 static const char * const skinny_cxmodes[] = {
1343  "sendonly",
1344  "recvonly",
1345  "sendrecv",
1346  "confrnce",
1347  "inactive"
1348 };
1349 #endif
1350 
1351 /* driver scheduler */
1352 static struct ast_sched_context *sched = NULL;
1353 
1354 /* Protect the network socket */
1356 
1357 /* Wait up to 16 seconds for first digit */
1358 static int firstdigittimeout = 16000;
1359 
1360 /* How long to wait for following digits */
1361 static int gendigittimeout = 8000;
1362 
1363 /* How long to wait for an extra digit, if there is an ambiguous match */
1364 static int matchdigittimeout = 3000;
1365 
1366 /*!
1367  * To appease the stupid compiler option on ast_sched_del()
1368  * since we don't care about the return value.
1369  */
1370 static int not_used;
1371 
1372 #define SUBSTATE_UNSET 0
1373 #define SUBSTATE_OFFHOOK 1
1374 #define SUBSTATE_ONHOOK 2
1375 #define SUBSTATE_RINGOUT 3
1376 #define SUBSTATE_RINGIN 4
1377 #define SUBSTATE_CONNECTED 5
1378 #define SUBSTATE_BUSY 6
1379 #define SUBSTATE_CONGESTION 7
1380 #define SUBSTATE_HOLD 8
1381 #define SUBSTATE_CALLWAIT 9
1382 #define SUBSTATE_PROGRESS 12
1383 #define SUBSTATE_DIALING 101
1384 
1385 #define DIALTYPE_NORMAL 1<<0
1386 #define DIALTYPE_CFWD 1<<1
1387 #define DIALTYPE_XFER 1<<2
1388 
1393  unsigned int callid;
1395  /* time_t lastouttime; */ /* Unused */
1397  int ringing;
1398  /* int lastout; */ /* Unused */
1399  int cxmode;
1400  int nat;
1403  int xferor;
1406  int aa_beep;
1407  int aa_mute;
1412  char *origtonum;
1413  char *origtoname;
1414 
1419 };
1420 
1421 #define SKINNY_LINE_OPTIONS \
1422  char name[80]; \
1423  char label[24]; \
1424  char accountcode[AST_MAX_ACCOUNT_CODE]; \
1425  char exten[AST_MAX_EXTENSION]; \
1426  char context[AST_MAX_CONTEXT]; \
1427  char language[MAX_LANGUAGE]; \
1428  char cid_num[AST_MAX_EXTENSION]; \
1429  char cid_name[AST_MAX_EXTENSION]; \
1430  char lastcallerid[AST_MAX_EXTENSION]; \
1431  int cfwdtype; \
1432  char call_forward_all[AST_MAX_EXTENSION]; \
1433  char call_forward_busy[AST_MAX_EXTENSION]; \
1434  char call_forward_noanswer[AST_MAX_EXTENSION]; \
1435  char mailbox[AST_MAX_MAILBOX_UNIQUEID]; \
1436  char vmexten[AST_MAX_EXTENSION]; \
1437  char regexten[AST_MAX_EXTENSION]; \
1438  char regcontext[AST_MAX_CONTEXT]; \
1439  char parkinglot[AST_MAX_CONTEXT]; \
1440  char mohinterpret[MAX_MUSICCLASS]; \
1441  char mohsuggest[MAX_MUSICCLASS]; \
1442  char lastnumberdialed[AST_MAX_EXTENSION]; \
1443  char dialoutexten[AST_MAX_EXTENSION]; \
1444  char dialoutcontext[AST_MAX_CONTEXT]; \
1445  ast_group_t callgroup; \
1446  ast_group_t pickupgroup; \
1447  struct ast_namedgroups *named_callgroups; \
1448  struct ast_namedgroups *named_pickupgroups; \
1449  int callwaiting; \
1450  int transfer; \
1451  int threewaycalling; \
1452  int mwiblink; \
1453  int cancallforward; \
1454  int callfwdtimeout; \
1455  int dnd; \
1456  int hidecallerid; \
1457  int amaflags; \
1458  int instance; \
1459  int group; \
1460  int nonCodecCapability; \
1461  int immediate; \
1462  int nat; \
1463  int directmedia; \
1464  int prune;
1465 
1466 struct skinny_line {
1470  struct ast_mwi_subscriber *mwi_event_sub; /* Event based MWI */
1479  struct ast_variable *chanvars; /*!< Channel variables to set for inbound call */
1480  int newmsgs;
1481 };
1482 
1483 static struct skinny_line_options{
1485 } default_line_struct = {
1486  .callwaiting = 1,
1487  .transfer = 1,
1488  .mwiblink = 0,
1489  .dnd = 0,
1490  .hidecallerid = 0,
1491  .amaflags = 0,
1492  .instance = 0,
1493  .directmedia = 0,
1494  .nat = 0,
1495  .callfwdtimeout = 20000,
1496  .prune = 0,
1497 };
1499 
1501 
1507  char name[80];
1512  char ourName[40];
1513  char ourNum[24];
1514  char theirName[40];
1515  char theirNum[24];
1519  unsigned int callid;
1520 };
1521 
1525  char label[42];
1529  int stateid;
1531  int isHint;
1532 
1535 };
1536 
1540  char displayName[40];
1543 };
1544 
1545 #define SKINNY_DEVICECONTAINER 1
1546 #define SKINNY_LINECONTAINER 2
1547 #define SKINNY_SUBLINECONTAINER 3
1548 #define SKINNY_SDCONTAINER 4
1549 
1551  int type;
1552  void *data;
1553 };
1554 
1557  char type[10];
1560 };
1561 
1562 #define SKINNY_DEVICE_OPTIONS \
1563  char name[80]; \
1564  char id[16]; \
1565  char version_id[16]; \
1566  char vmexten[AST_MAX_EXTENSION]; \
1567  int type; \
1568  int protocolversion; \
1569  int hookstate; \
1570  int lastlineinstance; \
1571  int lastcallreference; \
1572  int earlyrtp; \
1573  int transfer; \
1574  int callwaiting; \
1575  int mwiblink; \
1576  int dnd; \
1577  int prune;
1578 
1581  struct type *first;
1582  struct type *last;
1584  struct sockaddr_in addr;
1585  struct in_addr ourip;
1586  struct ast_ha *ha;
1597 };
1598 
1599 static struct skinny_device_options {
1602  .transfer = 1,
1603  .earlyrtp = 1,
1604  .callwaiting = 1,
1605  .mwiblink = 0,
1606  .dnd = 0,
1607  .prune = 0,
1608  .hookstate = SKINNY_ONHOOK,
1609 };
1611 
1613 
1615  pthread_t t;
1617  struct timeval start;
1618  struct sockaddr_in sin;
1619  int fd;
1623  int lockstate; /* Only for use in the skinny_session thread */
1626  struct timeval last_keepalive;
1628 };
1629 
1630 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);
1632 
1633 static int skinny_devicestate(const char *data);
1634 static int skinny_call(struct ast_channel *ast, const char *dest, int timeout);
1635 static int skinny_hangup(struct ast_channel *ast);
1636 static int skinny_answer(struct ast_channel *ast);
1637 static struct ast_frame *skinny_read(struct ast_channel *ast);
1638 static int skinny_write(struct ast_channel *ast, struct ast_frame *frame);
1639 static int skinny_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen);
1640 static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
1641 static int skinny_senddigit_begin(struct ast_channel *ast, char digit);
1642 static int skinny_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
1643 static void mwi_event_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg);
1644 static int skinny_dialer_cb(const void *data);
1645 static int skinny_reload(void);
1646 
1647 static void skinny_set_owner(struct skinny_subchannel* sub, struct ast_channel* chan);
1648 static void setsubstate(struct skinny_subchannel *sub, int state);
1649 static void dumpsub(struct skinny_subchannel *sub, int forcehangup);
1650 static void activatesub(struct skinny_subchannel *sub, int state);
1651 static void dialandactivatesub(struct skinny_subchannel *sub, char exten[AST_MAX_EXTENSION]);
1652 static int skinny_nokeepalive_cb(const void *data);
1653 
1655 
1656 static struct ast_channel_tech skinny_tech = {
1657  .type = "Skinny",
1658  .description = tdesc,
1660  .requester = skinny_request,
1661  .devicestate = skinny_devicestate,
1662  .call = skinny_call,
1663  .hangup = skinny_hangup,
1664  .answer = skinny_answer,
1665  .read = skinny_read,
1666  .write = skinny_write,
1667  .indicate = skinny_indicate,
1668  .fixup = skinny_fixup,
1669  .send_digit_begin = skinny_senddigit_begin,
1670  .send_digit_end = skinny_senddigit_end,
1671 };
1672 
1673 static int skinny_extensionstate_cb(const char *context, const char *exten, struct ast_state_cb_info *info, void *data);
1674 
1675 static struct skinny_line *skinny_line_alloc(void)
1676 {
1677  struct skinny_line *l;
1678  if (!(l = ast_calloc(1, sizeof(*l)))) {
1679  return NULL;
1680  }
1681 
1684  if (!l->cap || !l->confcap) {
1685  ao2_cleanup(l->cap);
1686  ao2_cleanup(l->confcap);
1687  ast_free(l);
1688  return NULL;
1689  }
1690  return l;
1691 }
1693 {
1694  ao2_ref(l->cap, -1);
1695  ao2_ref(l->confcap, -1);
1696  l->named_callgroups = ast_unref_namedgroups(l->named_callgroups);
1697  l->named_pickupgroups = ast_unref_namedgroups(l->named_pickupgroups);
1698  ast_free(l->container);
1699  ast_free(l);
1700  return NULL;
1701 }
1702 static struct skinny_device *skinny_device_alloc(const char *dname)
1703 {
1704  struct skinny_device *d;
1705  if (!(d = ast_calloc(1, sizeof(*d)))) {
1706  return NULL;
1707  }
1708 
1711  d->endpoint = ast_endpoint_create("Skinny", dname);
1712  if (!d->cap || !d->confcap || !d->endpoint) {
1713  ao2_cleanup(d->cap);
1714  ao2_cleanup(d->confcap);
1715  ast_free(d);
1716  return NULL;
1717  }
1718 
1719  ast_copy_string(d->name, dname, sizeof(d->name));
1720 
1721  return d;
1722 }
1724 {
1725  ao2_ref(d->cap, -1);
1726  ao2_ref(d->confcap, -1);
1727  ast_endpoint_shutdown(d->endpoint);
1728  ast_free(d);
1729  return NULL;
1730 }
1731 
1733 {
1734  struct skinny_device *d = s->device;
1735  struct skinny_addon *a;
1736  int i;
1737 
1738  switch (d->type) {
1740  case SKINNY_DEVICE_30VIP:
1741  /* 13 rows, 2 columns */
1742  for (i = 0; i < 4; i++)
1743  (btn++)->buttonDefinition = BT_CUST_LINE;
1744  (btn++)->buttonDefinition = BT_REDIAL;
1745  (btn++)->buttonDefinition = BT_VOICEMAIL;
1746  (btn++)->buttonDefinition = BT_CALLPARK;
1747  (btn++)->buttonDefinition = BT_FORWARDALL;
1748  (btn++)->buttonDefinition = BT_CONFERENCE;
1749  for (i = 0; i < 4; i++)
1750  (btn++)->buttonDefinition = BT_NONE;
1751  for (i = 0; i < 13; i++)
1752  (btn++)->buttonDefinition = BT_SPEEDDIAL;
1753 
1754  break;
1756  case SKINNY_DEVICE_12SP:
1757  case SKINNY_DEVICE_12:
1758  /* 6 rows, 2 columns */
1759  for (i = 0; i < 2; i++)
1760  (btn++)->buttonDefinition = BT_CUST_LINE;
1761  for (i = 0; i < 4; i++)
1762  (btn++)->buttonDefinition = BT_SPEEDDIAL;
1763  (btn++)->buttonDefinition = BT_HOLD;
1764  (btn++)->buttonDefinition = BT_REDIAL;
1765  (btn++)->buttonDefinition = BT_TRANSFER;
1766  (btn++)->buttonDefinition = BT_FORWARDALL;
1767  (btn++)->buttonDefinition = BT_CALLPARK;
1768  (btn++)->buttonDefinition = BT_VOICEMAIL;
1769  break;
1770  case SKINNY_DEVICE_7910:
1771  (btn++)->buttonDefinition = BT_LINE;
1772  (btn++)->buttonDefinition = BT_HOLD;
1773  (btn++)->buttonDefinition = BT_TRANSFER;
1774  (btn++)->buttonDefinition = BT_DISPLAY;
1775  (btn++)->buttonDefinition = BT_VOICEMAIL;
1776  (btn++)->buttonDefinition = BT_CONFERENCE;
1777  (btn++)->buttonDefinition = BT_FORWARDALL;
1778  for (i = 0; i < 2; i++)
1779  (btn++)->buttonDefinition = BT_SPEEDDIAL;
1780  (btn++)->buttonDefinition = BT_REDIAL;
1781  break;
1782  case SKINNY_DEVICE_7960:
1783  case SKINNY_DEVICE_7961:
1784  case SKINNY_DEVICE_7961GE:
1785  case SKINNY_DEVICE_7962:
1786  case SKINNY_DEVICE_7965:
1787  for (i = 0; i < 6; i++)
1788  (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1789  break;
1790  case SKINNY_DEVICE_7940:
1791  case SKINNY_DEVICE_7941:
1792  case SKINNY_DEVICE_7941GE:
1793  case SKINNY_DEVICE_7942:
1794  case SKINNY_DEVICE_7945:
1795  for (i = 0; i < 2; i++)
1796  (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1797  break;
1798  case SKINNY_DEVICE_7935:
1799  case SKINNY_DEVICE_7936:
1800  for (i = 0; i < 2; i++)
1801  (btn++)->buttonDefinition = BT_LINE;
1802  break;
1803  case SKINNY_DEVICE_ATA186:
1804  (btn++)->buttonDefinition = BT_LINE;
1805  break;
1806  case SKINNY_DEVICE_7970:
1807  case SKINNY_DEVICE_7971:
1808  case SKINNY_DEVICE_7975:
1809  case SKINNY_DEVICE_CIPC:
1810  for (i = 0; i < 8; i++)
1811  (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1812  break;
1813  case SKINNY_DEVICE_7985:
1814  /* XXX I have no idea what the buttons look like on these. */
1815  ast_log(LOG_WARNING, "Unsupported device type '%d (7985)' found.\n", d->type);
1816  break;
1817  case SKINNY_DEVICE_7912:
1818  case SKINNY_DEVICE_7911:
1819  case SKINNY_DEVICE_7905:
1820  (btn++)->buttonDefinition = BT_LINE;
1821  (btn++)->buttonDefinition = BT_HOLD;
1822  break;
1823  case SKINNY_DEVICE_7920:
1824  /* XXX I don't know if this is right. */
1825  for (i = 0; i < 4; i++)
1826  (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1827  break;
1828  case SKINNY_DEVICE_7921:
1829  for (i = 0; i < 6; i++)
1830  (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1831  break;
1832  case SKINNY_DEVICE_7902:
1833  ast_log(LOG_WARNING, "Unsupported device type '%d (7902)' found.\n", d->type);
1834  break;
1835  case SKINNY_DEVICE_7906:
1836  ast_log(LOG_WARNING, "Unsupported device type '%d (7906)' found.\n", d->type);
1837  break;
1838  case SKINNY_DEVICE_7931:
1839  ast_log(LOG_WARNING, "Unsupported device type '%d (7931)' found.\n", d->type);
1840  break;
1841  case SKINNY_DEVICE_7937:
1842  ast_log(LOG_WARNING, "Unsupported device type '%d (7937)' found.\n", d->type);
1843  break;
1844  case SKINNY_DEVICE_7914:
1845  ast_log(LOG_WARNING, "Unsupported device type '%d (7914)' found. Expansion module registered by itself?\n", d->type);
1846  break;
1849  ast_log(LOG_WARNING, "Unsupported device type '%d (SCCP gateway)' found.\n", d->type);
1850  break;
1851  default:
1852  ast_log(LOG_WARNING, "Unknown device type '%d' found.\n", d->type);
1853  break;
1854  }
1855 
1856  AST_LIST_LOCK(&d->addons);
1857  AST_LIST_TRAVERSE(&d->addons, a, list) {
1858  if (!strcasecmp(a->type, "7914")) {
1859  for (i = 0; i < 14; i++)
1860  (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
1861  } else {
1862  ast_log(LOG_WARNING, "Unknown addon type '%s' found. Skipping.\n", a->type);
1863  }
1864  }
1865  AST_LIST_UNLOCK(&d->addons);
1866 
1867  return btn;
1868 }
1869 
1870 static struct skinny_req *req_alloc(size_t size, int response_message)
1871 {
1872  struct skinny_req *req;
1873 
1874  if (!(req = ast_calloc(1, skinny_header_size + size + 4)))
1875  return NULL;
1876 
1877  req->len = htolel(size+4);
1878  req->e = htolel(response_message);
1879 
1880  return req;
1881 }
1882 
1883 static struct skinny_line *find_line_by_instance(struct skinny_device *d, int instance)
1884 {
1885  struct skinny_line *l;
1886 
1887  /*Dialing from on hook or on a 7920 uses instance 0 in requests
1888  but we need to start looking at instance 1 */
1889 
1890  if (!instance)
1891  instance = 1;
1892 
1893  AST_LIST_TRAVERSE(&d->lines, l, list){
1894  if (l->instance == instance)
1895  break;
1896  }
1897 
1898  if (!l) {
1899  ast_log(LOG_WARNING, "Could not find line with instance '%d' on device '%s'\n", instance, d->name);
1900  }
1901  return l;
1902 }
1903 
1904 static struct skinny_line *find_line_by_name(const char *dest)
1905 {
1906  struct skinny_line *l;
1907  struct skinny_line *tmpl = NULL;
1908  struct skinny_device *d;
1909  char line[256];
1910  char *at;
1911  char *device;
1912  int checkdevice = 0;
1913 
1914  ast_copy_string(line, dest, sizeof(line));
1915  at = strchr(line, '@');
1916  if (at)
1917  *at++ = '\0';
1918  device = at;
1919 
1920  if (!ast_strlen_zero(device))
1921  checkdevice = 1;
1922 
1925  if (checkdevice && tmpl)
1926  break;
1927  else if (!checkdevice) {
1928  /* This is a match, since we're checking for line on every device. */
1929  } else if (!strcasecmp(d->name, device)) {
1930  } else
1931  continue;
1932 
1933  /* Found the device (or we don't care which device) */
1934  AST_LIST_TRAVERSE(&d->lines, l, list){
1935  /* Search for the right line */
1936  if (!strcasecmp(l->name, line)) {
1937  if (tmpl) {
1938  ast_log(LOG_WARNING, "Ambiguous line name: %s\n", line);
1940  return NULL;
1941  } else
1942  tmpl = l;
1943  }
1944  }
1945  }
1947  return tmpl;
1948 }
1949 
1950 static struct skinny_subline *find_subline_by_name(const char *dest)
1951 {
1952  struct skinny_line *l;
1953  struct skinny_subline *subline;
1954  struct skinny_subline *tmpsubline = NULL;
1955  struct skinny_device *d;
1956 
1959  AST_LIST_TRAVERSE(&d->lines, l, list){
1960  AST_LIST_TRAVERSE(&l->sublines, subline, list){
1961  if (!strcasecmp(subline->name, dest)) {
1962  if (tmpsubline) {
1963  ast_verb(2, "Ambiguous subline name: %s\n", dest);
1965  return NULL;
1966  } else
1967  tmpsubline = subline;
1968  }
1969  }
1970  }
1971  }
1973  return tmpsubline;
1974 }
1975 
1977 {
1978  struct skinny_subline *subline;
1979  struct skinny_line *l;
1980 
1981  AST_LIST_TRAVERSE(&d->lines, l, list){
1982  AST_LIST_TRAVERSE(&l->sublines, subline, list){
1983  if (subline->callid == callid) {
1984  return subline;
1985  }
1986  }
1987  }
1988  return NULL;
1989 }
1990 
1991 /*!
1992  * implement the setvar config line
1993  */
1994 static struct ast_variable *add_var(const char *buf, struct ast_variable *list)
1995 {
1996  struct ast_variable *tmpvar = NULL;
1997  char *varname = ast_strdupa(buf), *varval = NULL;
1998 
1999  if ((varval = strchr(varname,'='))) {
2000  *varval++ = '\0';
2001  if ((tmpvar = ast_variable_new(varname, varval, ""))) {
2002  tmpvar->next = list;
2003  list = tmpvar;
2004  }
2005  }
2006  return list;
2007 }
2008 
2010 {
2011  if (sub && sub->owner) {
2012  ast_channel_lock(sub->owner);
2013  }
2014 }
2015 
2017 {
2018  if (sub && sub->owner) {
2019  ast_channel_unlock(sub->owner);
2020  }
2021 }
2022 
2024 {
2025  SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Deleting SCHED %d\n",
2026  sub->callid, sched_id);
2027  return ast_sched_del(sched, sched_id);
2028 }
2029 
2030 static int skinny_sched_add(int when, ast_sched_cb callback, struct skinny_subchannel *sub)
2031 {
2032  int ret;
2033  ret = ast_sched_add(sched, when, callback, sub);
2034  SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Added SCHED %d\n",
2035  sub->callid, ret);
2036  return ret;
2037 }
2038 
2039 /* It's quicker/easier to find the subchannel when we know the instance number too */
2040 static struct skinny_subchannel *find_subchannel_by_instance_reference(struct skinny_device *d, int instance, int reference)
2041 {
2042  struct skinny_line *l = find_line_by_instance(d, instance);
2043  struct skinny_subchannel *sub;
2044 
2045  if (!l) {
2046  return NULL;
2047  }
2048 
2049  /* 7920 phones set call reference to 0, so use the first
2050  sub-channel on the list.
2051  This MIGHT need more love to be right */
2052  if (!reference)
2053  sub = AST_LIST_FIRST(&l->sub);
2054  else {
2055  AST_LIST_TRAVERSE(&l->sub, sub, list) {
2056  if (sub->callid == reference)
2057  break;
2058  }
2059  }
2060  if (!sub) {
2061  ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s'\n", reference, d->name);
2062  }
2063  return sub;
2064 }
2065 
2066 /* Find the subchannel when we only have the callid - this shouldn't happen often */
2067 static struct skinny_subchannel *find_subchannel_by_reference(struct skinny_device *d, int reference)
2068 {
2069  struct skinny_line *l;
2070  struct skinny_subchannel *sub = NULL;
2071 
2072  AST_LIST_TRAVERSE(&d->lines, l, list){
2073  AST_LIST_TRAVERSE(&l->sub, sub, list){
2074  if (sub->callid == reference)
2075  break;
2076  }
2077  if (sub)
2078  break;
2079  }
2080 
2081  if (!l) {
2082  ast_log(LOG_WARNING, "Could not find any lines that contained a subchannel with reference '%d' on device '%s'\n", reference, d->name);
2083  } else {
2084  if (!sub) {
2085  ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s@%s'\n", reference, l->name, d->name);
2086  }
2087  }
2088  return sub;
2089 }
2090 
2092 {
2093  struct skinny_speeddial *sd;
2094 
2095  AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
2096  if (sd->isHint == isHint && sd->instance == instance)
2097  break;
2098  }
2099 
2100  if (!sd) {
2101  ast_log(LOG_WARNING, "Could not find speeddial with instance '%d' on device '%s'\n", instance, d->name);
2102  }
2103  return sd;
2104 }
2105 
2106 static struct ast_format *codec_skinny2ast(enum skinny_codecs skinnycodec)
2107 {
2108  switch (skinnycodec) {
2109  case SKINNY_CODEC_ALAW:
2110  return ast_format_alaw;
2111  case SKINNY_CODEC_ULAW:
2112  return ast_format_ulaw;
2113  case SKINNY_CODEC_G722:
2114  return ast_format_g722;
2115  case SKINNY_CODEC_G723_1:
2116  return ast_format_g723;
2117  case SKINNY_CODEC_G729A:
2118  return ast_format_g729;
2119  case SKINNY_CODEC_G726_32:
2120  return ast_format_g726; /* XXX Is this right? */
2121  case SKINNY_CODEC_H261:
2122  return ast_format_h261;
2123  case SKINNY_CODEC_H263:
2124  return ast_format_h263;
2125  default:
2126  return ast_format_none;
2127  }
2128 }
2129 
2130 static int codec_ast2skinny(const struct ast_format *astcodec)
2131 {
2133  return SKINNY_CODEC_ALAW;
2134  } else if (ast_format_cmp(astcodec, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) {
2135  return SKINNY_CODEC_ULAW;
2136  } else if (ast_format_cmp(astcodec, ast_format_g722) == AST_FORMAT_CMP_EQUAL) {
2137  return SKINNY_CODEC_G722;
2138  } else if (ast_format_cmp(astcodec, ast_format_g723) == AST_FORMAT_CMP_EQUAL) {
2139  return SKINNY_CODEC_G723_1;
2140  } else if (ast_format_cmp(astcodec, ast_format_g729) == AST_FORMAT_CMP_EQUAL) {
2141  return SKINNY_CODEC_G729A;
2142  } else if (ast_format_cmp(astcodec, ast_format_g726) == AST_FORMAT_CMP_EQUAL) {
2143  return SKINNY_CODEC_G726_32;
2144  } else if (ast_format_cmp(astcodec, ast_format_h261) == AST_FORMAT_CMP_EQUAL) {
2145  return SKINNY_CODEC_H261;
2146  } else if (ast_format_cmp(astcodec, ast_format_h263) == AST_FORMAT_CMP_EQUAL) {
2147  return SKINNY_CODEC_H263;
2148  } else {
2149  return 0;
2150  }
2151 }
2152 
2153 static int set_callforwards(struct skinny_line *l, const char *cfwd, int cfwdtype)
2154 {
2155  if (!l)
2156  return 0;
2157 
2158  if (!ast_strlen_zero(cfwd)) {
2159  if (cfwdtype & SKINNY_CFWD_ALL) {
2160  l->cfwdtype |= SKINNY_CFWD_ALL;
2161  ast_copy_string(l->call_forward_all, cfwd, sizeof(l->call_forward_all));
2162  }
2163  if (cfwdtype & SKINNY_CFWD_BUSY) {
2164  l->cfwdtype |= SKINNY_CFWD_BUSY;
2165  ast_copy_string(l->call_forward_busy, cfwd, sizeof(l->call_forward_busy));
2166  }
2167  if (cfwdtype & SKINNY_CFWD_NOANSWER) {
2168  l->cfwdtype |= SKINNY_CFWD_NOANSWER;
2169  ast_copy_string(l->call_forward_noanswer, cfwd, sizeof(l->call_forward_noanswer));
2170  }
2171  } else {
2172  if (cfwdtype & SKINNY_CFWD_ALL) {
2173  l->cfwdtype &= ~SKINNY_CFWD_ALL;
2174  memset(l->call_forward_all, 0, sizeof(l->call_forward_all));
2175  }
2176  if (cfwdtype & SKINNY_CFWD_BUSY) {
2177  l->cfwdtype &= ~SKINNY_CFWD_BUSY;
2178  memset(l->call_forward_busy, 0, sizeof(l->call_forward_busy));
2179  }
2180  if (cfwdtype & SKINNY_CFWD_NOANSWER) {
2181  l->cfwdtype &= ~SKINNY_CFWD_NOANSWER;
2182  memset(l->call_forward_noanswer, 0, sizeof(l->call_forward_noanswer));
2183  }
2184  }
2185  return l->cfwdtype;
2186 }
2187 
2188 static void cleanup_stale_contexts(char *new, char *old)
2189 {
2190  char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
2191 
2192  while ((oldcontext = strsep(&old, "&"))) {
2193  stalecontext = NULL;
2194  ast_copy_string(newlist, new, sizeof(newlist));
2195  stringp = newlist;
2196  while ((newcontext = strsep(&stringp, "&"))) {
2197  if (strcmp(newcontext, oldcontext) == 0) {
2198  /* This is not the context you're looking for */
2199  stalecontext = NULL;
2200  break;
2201  } else if (strcmp(newcontext, oldcontext)) {
2202  stalecontext = oldcontext;
2203  }
2204 
2205  }
2206  ast_context_destroy_by_name(stalecontext, "Skinny");
2207  }
2208 }
2209 
2210 static void register_exten(struct skinny_line *l)
2211 {
2212  char multi[256];
2213  char *stringp, *ext, *context;
2214 
2216  return;
2217 
2218  ast_copy_string(multi, S_OR(l->regexten, l->name), sizeof(multi));
2219  stringp = multi;
2220  while ((ext = strsep(&stringp, "&"))) {
2221  if ((context = strchr(ext, '@'))) {
2222  *context++ = '\0'; /* split ext@context */
2223  if (!ast_context_find(context)) {
2224  ast_log(LOG_WARNING, "Context %s must exist in regcontext= in skinny.conf!\n", context);
2225  continue;
2226  }
2227  } else {
2228  context = regcontext;
2229  }
2230  ast_add_extension(context, 1, ext, 1, NULL, NULL, "Noop",
2231  ast_strdup(l->name), ast_free_ptr, "Skinny");
2232  }
2233 }
2234 
2235 static void unregister_exten(struct skinny_line *l)
2236 {
2237  char multi[256];
2238  char *stringp, *ext, *context;
2239 
2241  return;
2242 
2243  ast_copy_string(multi, S_OR(l->regexten, l->name), sizeof(multi));
2244  stringp = multi;
2245  while ((ext = strsep(&stringp, "&"))) {
2246  if ((context = strchr(ext, '@'))) {
2247  *context++ = '\0'; /* split ext@context */
2248  if (!ast_context_find(context)) {
2249  ast_log(LOG_WARNING, "Context %s must exist in regcontext= in skinny.conf!\n", context);
2250  continue;
2251  }
2252  } else {
2253  context = regcontext;
2254  }
2256  }
2257 }
2258 
2259 static int skinny_register(struct skinny_req *req, struct skinnysession *s)
2260 {
2261  struct skinny_device *d;
2262  struct skinny_line *l;
2263  struct skinny_subline *subline;
2264  struct skinny_speeddial *sd;
2265  struct sockaddr_in sin;
2266  socklen_t slen;
2267  int instance;
2268  int res = -1;
2269 
2270  if (-1 < s->auth_timeout_sched) {
2272  s->auth_timeout_sched = -1;
2273  }
2274 
2276  AST_LIST_TRAVERSE(&devices, d, list){
2277  struct ast_sockaddr addr;
2278  ast_sockaddr_from_sin(&addr, &s->sin);
2279  if (!strcasecmp(req->data.reg.name, d->id)
2280  && ast_apply_ha(d->ha, &addr)) {
2281  RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
2282 
2283  if (d->session) {
2284  ast_log(LOG_WARNING, "Device already registered.\n");
2286  res = 0;
2287  break;
2288  }
2289  s->device = d;
2290  d->type = letohl(req->data.reg.type);
2291  d->protocolversion = letohl(req->data.reg.protocolVersion);
2292  if (ast_strlen_zero(d->version_id)) {
2293  ast_copy_string(d->version_id, version_id, sizeof(d->version_id));
2294  }
2295  d->session = s;
2296 
2297  slen = sizeof(sin);
2298  if (getsockname(s->fd, (struct sockaddr *)&sin, &slen)) {
2299  ast_log(LOG_WARNING, "Cannot get socket name\n");
2300  sin.sin_addr = __ourip;
2301  }
2302  d->ourip = sin.sin_addr;
2303 
2304  AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
2306  }
2307  instance = 0;
2308  AST_LIST_TRAVERSE(&d->lines, l, list) {
2309  instance++;
2310  }
2311  AST_LIST_TRAVERSE(&d->lines, l, list) {
2313  /* l->capability = d->capability; */
2314  l->instance = instance;
2315  l->newmsgs = ast_app_has_voicemail(l->mailbox, NULL);
2317  register_exten(l);
2318  /* initialize MWI on line and device */
2319  mwi_event_cb(l, NULL, NULL);
2320  AST_LIST_TRAVERSE(&l->sublines, subline, list) {
2322  }
2324  --instance;
2325  }
2327  blob = ast_json_pack("{s: s}", "peer_status", "Registered");
2329  res = 1;
2330  break;
2331  }
2332  }
2334  return res;
2335 }
2336 
2337 static void end_session(struct skinnysession *s)
2338 {
2339  pthread_cancel(s->t);
2340 }
2341 
2342 #ifdef AST_DEVMODE
2343 static char *callstate2str(int ind)
2344 {
2345  char *tmp;
2346 
2347  switch (ind) {
2348  case SKINNY_OFFHOOK:
2349  return "SKINNY_OFFHOOK";
2350  case SKINNY_ONHOOK:
2351  return "SKINNY_ONHOOK";
2352  case SKINNY_RINGOUT:
2353  return "SKINNY_RINGOUT";
2354  case SKINNY_RINGIN:
2355  return "SKINNY_RINGIN";
2356  case SKINNY_CONNECTED:
2357  return "SKINNY_CONNECTED";
2358  case SKINNY_BUSY:
2359  return "SKINNY_BUSY";
2360  case SKINNY_CONGESTION:
2361  return "SKINNY_CONGESTION";
2362  case SKINNY_PROGRESS:
2363  return "SKINNY_PROGRESS";
2364  case SKINNY_HOLD:
2365  return "SKINNY_HOLD";
2366  case SKINNY_CALLWAIT:
2367  return "SKINNY_CALLWAIT";
2368  default:
2370  return "Unknown";
2371  snprintf(tmp, CALLSTATE2STR_BUFSIZE, "UNKNOWN-%d", ind);
2372  return tmp;
2373  }
2374 }
2375 
2376 #endif
2377 
2378 static int transmit_response_bysession(struct skinnysession *s, struct skinny_req *req)
2379 {
2380  int res = 0;
2381  unsigned long len;
2382 
2383  if (!s) {
2384  ast_log(LOG_WARNING, "Asked to transmit to a non-existent session!\n");
2385  return -1;
2386  }
2387 
2388  ast_mutex_lock(&s->lock);
2389 
2390  /* Don't optimize out assigning letohl() to len. It is necessary to guarantee that the comparison will always catch invalid values.
2391  * letohl() may or may not return a signed value depending upon which definition is used. */
2392  len = letohl(req->len);
2393  if (SKINNY_MAX_PACKET < len) {
2394  ast_log(LOG_WARNING, "transmit_response: the length of the request (%u) is out of bounds (%d)\n", letohl(req->len), SKINNY_MAX_PACKET);
2395  ast_mutex_unlock(&s->lock);
2396  return -1;
2397  }
2398 
2399  memset(s->outbuf, 0, sizeof(s->outbuf));
2400  memcpy(s->outbuf, req, skinny_header_size);
2401  memcpy(s->outbuf+skinny_header_size, &req->data, letohl(req->len));
2402 
2403  res = write(s->fd, s->outbuf, letohl(req->len)+8);
2404 
2405  if (res != letohl(req->len)+8) {
2406  ast_log(LOG_WARNING, "Transmit: write only sent %d out of %u bytes: %s\n", res, letohl(req->len)+8, strerror(errno));
2407  if (res == -1) {
2408  ast_log(LOG_WARNING, "Transmit: Skinny Client was lost, unregistering\n");
2409  end_session(s);
2410  }
2411 
2412  }
2413 
2414  ast_free(req);
2415  ast_mutex_unlock(&s->lock);
2416  return 1;
2417 }
2418 
2419 static void transmit_response(struct skinny_device *d, struct skinny_req *req)
2420 {
2421  transmit_response_bysession(d->session, req);
2422 }
2423 
2424 static void transmit_registerrej(struct skinnysession *s)
2425 {
2426  struct skinny_req *req;
2427  char name[16];
2428 
2429  if (!(req = req_alloc(sizeof(struct register_rej_message), REGISTER_REJ_MESSAGE)))
2430  return;
2431 
2432  memcpy(&name, req->data.reg.name, sizeof(name));
2433  snprintf(req->data.regrej.errMsg, sizeof(req->data.regrej.errMsg), "No Authority: %s", name);
2434 
2435  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting REGISTER_REJ_MESSAGE to UNKNOWN_DEVICE\n");
2437 }
2438 
2439 static void transmit_speaker_mode(struct skinny_device *d, int mode)
2440 {
2441  struct skinny_req *req;
2442 
2443  if (!(req = req_alloc(sizeof(struct set_speaker_message), SET_SPEAKER_MESSAGE)))
2444  return;
2445 
2446  req->data.setspeaker.mode = htolel(mode);
2447 
2448  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SET_SPEAKER_MESSAGE to %s, mode %d\n", d->name, mode);
2449  transmit_response(d, req);
2450 }
2451 
2452 static void transmit_microphone_mode(struct skinny_device *d, int mode)
2453 {
2454  struct skinny_req *req;
2455 
2456  if (!(req = req_alloc(sizeof(struct set_microphone_message), SET_MICROPHONE_MESSAGE)))
2457  return;
2458 
2459  req->data.setmicrophone.mode = htolel(mode);
2460 
2461  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SET_MICROPHONE_MESSAGE to %s, mode %d\n", d->name, mode);
2462  transmit_response(d, req);
2463 }
2464 
2465 //static void transmit_callinfo(struct skinny_subchannel *sub)
2466 static void transmit_callinfo(struct skinny_device *d, int instance, int callid,
2467  char *fromname, char *fromnum, char *toname, char *tonum, int calldirection, char *origtonum, char *origtoname)
2468 {
2469  struct skinny_req *req;
2470 
2471  if (!(req = req_alloc(sizeof(struct call_info_message), CALL_INFO_MESSAGE)))
2472  return;
2473 
2475  ast_copy_string(req->data.callinfo.callingParty, fromnum, sizeof(req->data.callinfo.callingParty));
2477  ast_copy_string(req->data.callinfo.calledParty, tonum, sizeof(req->data.callinfo.calledParty));
2478  if (origtoname) {
2480  }
2481  if (origtonum) {
2483  }
2484 
2485  req->data.callinfo.instance = htolel(instance);
2486  req->data.callinfo.reference = htolel(callid);
2487  req->data.callinfo.type = htolel(calldirection);
2488 
2489  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",
2490  d->name, toname, tonum, fromname, fromnum, origtoname, origtonum, calldirection, d->name, instance);
2491  transmit_response(d, req);
2492 }
2493 
2494 static void transmit_callinfo_variable(struct skinny_device *d, int instance, int callreference,
2495  char *fromname, char *fromnum, char *toname, char *tonum, int calldirection, char *origtonum, char *origtoname)
2496 {
2497  struct skinny_req *req;
2498  char *strptr;
2499  char *thestrings[13];
2500  int i;
2501  int callinfostrleft = MAXCALLINFOSTR;
2502 
2503  if (!(req = req_alloc(sizeof(struct call_info_message_variable), CALL_INFO_MESSAGE_VARIABLE)))
2504  return;
2505 
2506  req->data.callinfomessagevariable.instance = htolel(instance);
2507  req->data.callinfomessagevariable.callreference = htolel(callreference);
2508  req->data.callinfomessagevariable.calldirection = htolel(calldirection);
2509 
2515 
2516  thestrings[0] = fromnum;
2517  thestrings[1] = ""; /* Appears to be origfrom */
2518  if (calldirection == SKINNY_OUTGOING) {
2519  thestrings[2] = tonum;
2520  thestrings[3] = origtonum;
2521  } else {
2522  thestrings[2] = "";
2523  thestrings[3] = "";
2524  }
2525  thestrings[4] = "";
2526  thestrings[5] = "";
2527  thestrings[6] = "";
2528  thestrings[7] = "";
2529 
2530  thestrings[8] = "";
2531  thestrings[9] = fromname;
2532  thestrings[10] = toname;
2533  thestrings[11] = origtoname;
2534  thestrings[12] = "";
2535 
2537 
2538  for(i = 0; i < 13; i++) {
2539  if (thestrings[i]) {
2540  ast_copy_string(strptr, thestrings[i], callinfostrleft);
2541  strptr += strlen(thestrings[i]) + 1;
2542  callinfostrleft -= strlen(thestrings[i]) + 1;
2543  } else {
2544  ast_copy_string(strptr, "", callinfostrleft);
2545  strptr++;
2546  callinfostrleft--;
2547  }
2548  }
2549 
2550  req->len = req->len - (callinfostrleft & ~0x3);
2551 
2552  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",
2553  d->name, toname, tonum, fromname, fromnum, origtoname, origtonum, calldirection, d->name, instance);
2554  transmit_response(d, req);
2555 }
2556 
2558 {
2559  struct ast_channel *ast;
2560  struct skinny_device *d;
2561  struct skinny_line *l;
2562  struct ast_party_id connected_id;
2563  char *fromname;
2564  char *fromnum;
2565  char *toname;
2566  char *tonum;
2567 
2568  if (!sub || !sub->owner || !sub->line || !sub->line->device) {
2569  return;
2570  }
2571 
2572  ast = sub->owner;
2573  l = sub->line;
2574  d = l->device;
2575  connected_id = ast_channel_connected_effective_id(ast);
2576 
2577  if (sub->calldirection == SKINNY_INCOMING) {
2579  fromname = S_COR(connected_id.name.valid, connected_id.name.str, "");
2580  fromnum = S_COR(connected_id.number.valid, connected_id.number.str, "");
2581  } else {
2582  fromname = "";
2583  fromnum = "";
2584  }
2585  toname = S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, "");
2586  tonum = S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, "");
2587  } else if (sub->calldirection == SKINNY_OUTGOING) {
2588  fromname = S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, "");
2589  fromnum = S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, "");
2590  toname = S_COR(ast_channel_connected(ast)->id.name.valid, ast_channel_connected(ast)->id.name.str, "");
2591  tonum = S_COR(ast_channel_connected(ast)->id.number.valid, ast_channel_connected(ast)->id.number.str, l->lastnumberdialed);
2592  } else {
2593  ast_verb(1, "Error sending Callinfo to %s(%d) - No call direction in sub\n", d->name, l->instance);
2594  return;
2595  }
2596 
2597  if (d->protocolversion < 17) {
2598  transmit_callinfo(d, l->instance, sub->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
2599  } else {
2600  transmit_callinfo_variable(d, l->instance, sub->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
2601  }
2602 }
2603 
2604 static void push_callinfo(struct skinny_subline *subline, struct skinny_subchannel *sub)
2605 {
2606  struct ast_channel *ast;
2607  struct skinny_device *d;
2608  struct skinny_line *l;
2609  struct ast_party_id connected_id;
2610  char *fromname;
2611  char *fromnum;
2612  char *toname;
2613  char *tonum;
2614 
2615  if (!sub || !sub->owner || !sub->line || !sub->line->device) {
2616  return;
2617  }
2618 
2619  ast = sub->owner;
2620  l = sub->line;
2621  d = l->device;
2622  connected_id = ast_channel_connected_effective_id(ast);
2623 
2624  if (sub->calldirection == SKINNY_INCOMING) {
2626  fromname = S_COR(connected_id.name.valid, connected_id.name.str, "");
2627  fromnum = S_COR(connected_id.number.valid, connected_id.number.str, "");
2628  } else {
2629  fromname = "";
2630  fromnum = "";
2631  }
2632  toname = S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, "");
2633  tonum = S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, "");
2634  } else if (sub->calldirection == SKINNY_OUTGOING) {
2635  fromname = S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, "");
2636  fromnum = S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, "");
2637  toname = S_COR(ast_channel_connected(ast)->id.name.valid, ast_channel_connected(ast)->id.name.str, "");
2638  tonum = S_COR(ast_channel_connected(ast)->id.number.valid, ast_channel_connected(ast)->id.number.str, l->lastnumberdialed);
2639  } else {
2640  ast_verb(1, "Error sending Callinfo to %s(%d) - No call direction in sub\n", d->name, l->instance);
2641  return;
2642  }
2643 
2644  if (d->protocolversion < 17) {
2645  transmit_callinfo(subline->line->device, subline->line->instance, subline->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
2646  } else {
2647  transmit_callinfo_variable(subline->line->device, subline->line->instance, subline->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
2648  }
2649 }
2650 
2652 {
2653  struct skinny_req *req;
2654  struct skinny_line *l = sub->line;
2655  struct ast_format *tmpfmt;
2656  unsigned int framing;
2657 
2659  return;
2660 
2661  tmpfmt = ast_format_cap_get_format(l->cap, 0);
2662  framing = ast_format_cap_get_format_framing(l->cap, tmpfmt);
2663 
2664  req->data.openreceivechannel.conferenceId = htolel(sub->callid);
2665  req->data.openreceivechannel.partyId = htolel(sub->callid);
2666  req->data.openreceivechannel.packets = htolel(framing);
2668  req->data.openreceivechannel.echo = htolel(0);
2670 
2671  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",
2672  d->name, sub->callid, sub->callid, framing, codec_ast2skinny(tmpfmt), 0, 0);
2673 
2674  ao2_ref(tmpfmt, -1);
2675 
2676  transmit_response(d, req);
2677 }
2678 
2679 static void transmit_start_tone(struct skinny_device *d, int tone, int instance, int reference)
2680 {
2681  struct skinny_req *req;
2682  if (!(req = req_alloc(sizeof(struct start_tone_message), START_TONE_MESSAGE)))
2683  return;
2684  req->data.starttone.tone = htolel(tone);
2685  req->data.starttone.instance = htolel(instance);
2686  req->data.starttone.reference = htolel(reference);
2687 
2688  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting START_TONE_MESSAGE to %s, tone %d, inst %d, ref %d\n",
2689  d->name, tone, instance, reference);
2690  transmit_response(d, req);
2691 }
2692 
2693 static void transmit_stop_tone(struct skinny_device *d, int instance, int reference)
2694 {
2695  struct skinny_req *req;
2696  if (!(req = req_alloc(sizeof(struct stop_tone_message), STOP_TONE_MESSAGE)))
2697  return;
2698  req->data.stoptone.instance = htolel(instance);
2699  req->data.stoptone.reference = htolel(reference);
2700 
2701  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting STOP_TONE_MESSAGE to %s, inst %d, ref %d\n",
2702  d->name, instance, reference);
2703  transmit_response(d, req);
2704 }
2705 
2706 static int keyset_translatebitmask(int keyset, int intmask)
2707 {
2708  int extmask = 0;
2709  int x, y;
2710  const struct soft_key_definitions *softkeymode = soft_key_default_definitions;
2711 
2712  for(x = 0; x < ARRAY_LEN(soft_key_default_definitions); x++) {
2713  if (softkeymode[x].mode == keyset) {
2714  const uint8_t *defaults = softkeymode[x].defaults;
2715 
2716  for (y = 0; y < softkeymode[x].count; y++) {
2717  if (intmask & (1 << (defaults[y]))) {
2718  extmask |= (1 << ((y)));
2719  }
2720  }
2721  break;
2722  }
2723  }
2724 
2725  return extmask;
2726 }
2727 
2728 static void transmit_selectsoftkeys(struct skinny_device *d, int instance, int callid, int softkey, int mask)
2729 {
2730  struct skinny_req *req;
2731  int newmask;
2732 
2733  if (!(req = req_alloc(sizeof(struct select_soft_keys_message), SELECT_SOFT_KEYS_MESSAGE)))
2734  return;
2735 
2736  newmask = keyset_translatebitmask(softkey, mask);
2737  req->data.selectsoftkey.instance = htolel(instance);
2738  req->data.selectsoftkey.reference = htolel(callid);
2739  req->data.selectsoftkey.softKeySetIndex = htolel(softkey);
2740  req->data.selectsoftkey.validKeyMask = htolel(newmask);
2741 
2742  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SELECT_SOFT_KEYS_MESSAGE to %s, inst %d, callid %d, softkey %d, mask 0x%08x\n",
2743  d->name, instance, callid, softkey, (unsigned)newmask);
2744  transmit_response(d, req);
2745 }
2746 
2747 static void transmit_lamp_indication(struct skinny_device *d, int stimulus, int instance, int indication)
2748 {
2749  struct skinny_req *req;
2750 
2751  if (!(req = req_alloc(sizeof(struct set_lamp_message), SET_LAMP_MESSAGE)))
2752  return;
2753 
2754  req->data.setlamp.stimulus = htolel(stimulus);
2755  req->data.setlamp.stimulusInstance = htolel(instance);
2756  req->data.setlamp.deviceStimulus = htolel(indication);
2757 
2758  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SET_LAMP_MESSAGE to %s, stim %d, inst %d, ind %d\n",
2759  d->name, stimulus, instance, indication);
2760  transmit_response(d, req);
2761 }
2762 
2763 static void transmit_ringer_mode(struct skinny_device *d, int mode)
2764 {
2765  struct skinny_req *req;
2766 
2767  if (!(req = req_alloc(sizeof(struct set_ringer_message), SET_RINGER_MESSAGE)))
2768  return;
2769 
2770  req->data.setringer.ringerMode = htolel(mode);
2771  /* XXX okay, I don't quite know what this is, but here's what happens (on a 7960).
2772  Note: The phone will always show as ringing on the display.
2773 
2774  1: phone will audibly ring over and over
2775  2: phone will audibly ring only once
2776  any other value, will NOT cause the phone to audibly ring
2777  */
2778  req->data.setringer.unknown1 = htolel(1);
2779  /* XXX the value here doesn't seem to change anything. Must be higher than 0.
2780  Perhaps a packet capture can shed some light on this. */
2781  req->data.setringer.unknown2 = htolel(1);
2782 
2783  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SET_RINGER_MESSAGE to %s, mode %d, unk1 1, unk2 1\n",
2784  d->name, mode);
2785  transmit_response(d, req);
2786 }
2787 
2788 static void transmit_clear_display_message(struct skinny_device *d, int instance, int reference)
2789 {
2790  struct skinny_req *req;
2791  if (!(req = req_alloc(sizeof(struct clear_display_message), CLEAR_DISPLAY_MESSAGE)))
2792  return;
2793 
2794  //what do we want hear CLEAR_DISPLAY_MESSAGE or CLEAR_PROMPT_STATUS???
2795  //if we are clearing the display, it appears there is no instance and reference info (size 0)
2796  //req->data.clearpromptstatus.lineInstance = instance;
2797  //req->data.clearpromptstatus.callReference = reference;
2798 
2799  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CLEAR_DISPLAY_MESSAGE to %s\n", d->name);
2800  transmit_response(d, req);
2801 }
2802 
2803 /* This function is not currently used, but will be (wedhorn)*/
2804 /* static void transmit_display_message(struct skinny_device *d, const char *text, int instance, int reference)
2805 {
2806  struct skinny_req *req;
2807 
2808  if (text == 0) {
2809  ast_verb(1, "Bug, Asked to display empty message\n");
2810  return;
2811  }
2812 
2813  if (!(req = req_alloc(sizeof(struct displaytext_message), DISPLAYTEXT_MESSAGE)))
2814  return;
2815 
2816  ast_copy_string(req->data.displaytext.text, text, sizeof(req->data.displaytext.text));
2817  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAYTEXT_MESSAGE to %s, text %s\n", d->name, text);
2818  transmit_response(d, req);
2819 } */
2820 
2821 static void transmit_displaynotify(struct skinny_device *d, const char *text, int t)
2822 {
2823  struct skinny_req *req;
2824 
2825  if (!(req = req_alloc(sizeof(struct display_notify_message), DISPLAY_NOTIFY_MESSAGE)))
2826  return;
2827 
2830 
2831  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_NOTIFY_MESSAGE to %s, text %s\n", d->name, text);
2832  transmit_response(d, req);
2833 }
2834 
2836 {
2837  struct skinny_req *req;
2838 
2839  if (!(req = req_alloc(sizeof(struct clear_prinotify_message), CLEAR_PRINOTIFY_MESSAGE)))
2840  return;
2841 
2843 
2844  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CLEAR_PRINOTIFY_MESSAGE to %s, priority %d\n", d->name, priority);
2845  transmit_response(d, req);
2846 }
2847 
2848 static void _transmit_displayprinotify(struct skinny_device *d, const char *text, const char *extratext, int timeout, int priority)
2849 {
2850  struct skinny_req *req;
2851 
2852  if (!(req = req_alloc(sizeof(struct display_prinotify_message), DISPLAY_PRINOTIFY_MESSAGE)))
2853  return;
2854 
2855  req->data.displayprinotify.timeout = htolel(timeout);
2857 
2858  if ((char)*text == '\200') {
2859  int octalstrlen = strlen(text);
2861  ast_copy_string(req->data.displayprinotify.text+octalstrlen, extratext, sizeof(req->data.displayprinotify.text)-octalstrlen);
2862  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PRINOTIFY_MESSAGE to %s, '\\%03o\\%03o', '%s', timeout=%d, priority=%d\n",
2863  d->name, (unsigned)*text, (unsigned)*(text+1), extratext, timeout, priority);
2864  } else {
2866  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PRINOTIFY_MESSAGE to %s, '%s', timeout=%d, priority=%d\n",
2867  d->name, text, timeout, priority);
2868  }
2869 
2870  transmit_response(d, req);
2871 }
2872 
2873 static void _transmit_displayprinotifyvar(struct skinny_device *d, const char *text, const char *extratext, int timeout, int priority)
2874 {
2875  struct skinny_req *req;
2876  int packetlen;
2877 
2879  return;
2880 
2881  req->data.displayprinotifyvar.timeout = htolel(timeout);
2883 
2884  if ((char)*text == '\200') {
2885  int octalstrlen = strlen(text);
2887  ast_copy_string(req->data.displayprinotifyvar.text+octalstrlen, extratext, sizeof(req->data.displayprinotifyvar.text)-octalstrlen);
2888  packetlen = req->len - MAXDISPLAYNOTIFYSTR + strlen(text) + strlen(extratext);
2889  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PRINOTIFY_MESSAGE_VARIABLE to %s, '\\%03o\\%03o', '%s', timeout=%d, priority=%d\n",
2890  d->name, (unsigned)*text, (unsigned)*(text+1), extratext, timeout, priority);
2891  } else {
2893  packetlen = req->len - MAXDISPLAYNOTIFYSTR + strlen(text);
2894  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PRINOTIFY_MESSAGE_VARIABLE to %s, '%s', timeout=%d, priority=%d\n",
2895  d->name, text, timeout, priority);
2896  }
2897 
2898  req->len = (packetlen & ~0x3) + 4;
2899 
2900  transmit_response(d, req);
2901 }
2902 
2903 static void send_displayprinotify(struct skinny_device *d, const char *text, const char *extratext, int timeout, int priority)
2904 {
2905  if (d->protocolversion < 17) {
2906  _transmit_displayprinotify(d, text, extratext, timeout, priority);
2907  } else {
2908  _transmit_displayprinotifyvar(d, text, extratext, timeout, priority);
2909  }
2910 }
2911 
2912 static void transmit_displaypromptstatus(struct skinny_device *d, const char *text, const char *extratext, int t, int instance, int callid)
2913 {
2914  struct skinny_req *req;
2915 
2917  return;
2918 
2920  req->data.displaypromptstatus.lineInstance = htolel(instance);
2922 
2923  if ((char)*text == '\200') {
2924  int octalstrlen = strlen(text);
2926  ast_copy_string(req->data.displaypromptstatus.promptMessage+octalstrlen, extratext, sizeof(req->data.displaypromptstatus.promptMessage)-octalstrlen);
2927  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PROMPT_STATUS_MESSAGE to %s, '\\%03o\\%03o', '%s'\n",
2928  d->name, (unsigned)*text, (unsigned)*(text+1), extratext);
2929  } else {
2931  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PROMPT_STATUS_MESSAGE to %s, '%s'\n",
2932  d->name, text);
2933  }
2934  transmit_response(d, req);
2935 }
2936 
2937 static void transmit_displaypromptstatusvar(struct skinny_device *d, const char *text, const char *extratext, int t, int instance, int callid)
2938 {
2939  struct skinny_req *req;
2940  int packetlen;
2941 
2943  return;
2944 
2947 
2948  if ((char)*text == '\200') {
2949  int octalstrlen = strlen(text);
2951  ast_copy_string(req->data.displaypromptstatusvar.promptMessage+octalstrlen, extratext, sizeof(req->data.displaypromptstatusvar.promptMessage)-octalstrlen);
2952  packetlen = req->len - MAXCALLINFOSTR + strlen(text) + strlen(extratext);
2953  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PROMPT_STATUS_MESSAGE_VARIABLE to %s, '\\%03o\\%03o', '%s'\n",
2954  d->name, (unsigned)*text, (unsigned)*(text+1), extratext);
2955  } else {
2957  packetlen = req->len - MAXCALLINFOSTR + strlen(text);
2958  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PROMPT_STATUS_MESSAGE_VARIABLE to %s, '%s'\n",
2959  d->name, text);
2960  }
2961  req->len = (packetlen & ~0x3) + 4;
2962 
2963  transmit_response(d, req);
2964 }
2965 
2966 static void send_displaypromptstatus(struct skinny_device *d, const char *text, const char *extratext, int t, int instance, int callid)
2967 {
2968  if (d->protocolversion < 17) {
2969  transmit_displaypromptstatus(d, text, extratext, t, instance, callid);
2970  } else {
2971  transmit_displaypromptstatusvar(d, text, extratext, t, instance, callid);
2972  }
2973 }
2974 
2975 static void transmit_clearpromptmessage(struct skinny_device *d, int instance, int callid)
2976 {
2977  struct skinny_req *req;
2978 
2979  if (!(req = req_alloc(sizeof(struct clear_prompt_message), CLEAR_PROMPT_MESSAGE)))
2980  return;
2981 
2982  req->data.clearpromptstatus.lineInstance = htolel(instance);
2983  req->data.clearpromptstatus.callReference = htolel(callid);
2984 
2985  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CLEAR_PROMPT_MESSAGE to %s, inst %d, callid %d\n",
2986  d->name, instance, callid);
2987  transmit_response(d, req);
2988 }
2989 
2990 static void transmit_dialednumber(struct skinny_device *d, const char *text, int instance, int callid)
2991 {
2992  struct skinny_req *req;
2993 
2994  if (!(req = req_alloc(sizeof(struct dialed_number_message), DIALED_NUMBER_MESSAGE)))
2995  return;
2996 
2998  req->data.dialednumber.lineInstance = htolel(instance);
2999  req->data.dialednumber.callReference = htolel(callid);
3000 
3001  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DIALED_NUMBER_MESSAGE to %s, num %s, inst %d, callid %d\n",
3002  d->name, text, instance, callid);
3003  transmit_response(d, req);
3004 }
3005 
3007 {
3008  struct skinny_req *req;
3009 
3011  return;
3012 
3014  req->data.closereceivechannel.partyId = htolel(sub->callid);
3015 
3016  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CLOSE_RECEIVE_CHANNEL_MESSAGE to %s, confid %d, callid %u\n",
3017  d->name, 0, sub->callid);
3018  transmit_response(d, req);
3019 }
3020 
3022 {
3023  struct skinny_req *req;
3024 
3026  return;
3027 
3028  req->data.stopmedia.conferenceId = htolel(0);
3029  req->data.stopmedia.passThruPartyId = htolel(sub->callid);
3030 
3031  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting STOP_MEDIA_TRANSMISSION_MESSAGE to %s, confid %d, passthrupartyid %u\n",
3032  d->name, 0, sub->callid);
3033  transmit_response(d, req);
3034 }
3035 
3036 static void transmit_startmediatransmission(struct skinny_device *d, struct skinny_subchannel *sub, struct sockaddr_in dest,
3037  struct ast_format *format, unsigned int framing)
3038 {
3039  struct skinny_req *req;
3040 
3041  if (d->protocolversion < 17) {
3043  return;
3044  req->data.startmedia_ip4.conferenceId = htolel(sub->callid);
3045  req->data.startmedia_ip4.passThruPartyId = htolel(sub->callid);
3046  req->data.startmedia_ip4.remoteIp = dest.sin_addr.s_addr;
3047  req->data.startmedia_ip4.remotePort = htolel(ntohs(dest.sin_port));
3048  req->data.startmedia_ip4.packetSize = htolel(framing);
3054  } else {
3056  return;
3057  req->data.startmedia_ip6.conferenceId = htolel(sub->callid);
3058  req->data.startmedia_ip6.passThruPartyId = htolel(sub->callid);
3059  memcpy(req->data.startmedia_ip6.remoteIp, &dest.sin_addr.s_addr, sizeof(dest.sin_addr.s_addr));
3060  req->data.startmedia_ip6.remotePort = htolel(ntohs(dest.sin_port));
3061  req->data.startmedia_ip6.packetSize = htolel(framing);
3067  }
3068 
3069  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",
3070  d->name, sub->callid, sub->callid, ast_inet_ntoa(dest.sin_addr), dest.sin_port, framing, codec_ast2skinny(format));
3071  transmit_response(d, req);
3072 }
3073 
3075 {
3076  struct skinny_req *req;
3077 
3079  return;
3080 
3081  req->data.activatecallplane.lineInstance = htolel(l->instance);
3082 
3083  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting ACTIVATE_CALL_PLANE_MESSAGE to %s, inst %d\n",
3084  d->name, l->instance);
3085  transmit_response(d, req);
3086 }
3087 
3088 static void transmit_callstate(struct skinny_device *d, int buttonInstance, unsigned callid, int state)
3089 {
3090  struct skinny_req *req;
3091 
3092  if (!(req = req_alloc(sizeof(struct call_state_message), CALL_STATE_MESSAGE)))
3093  return;
3094 
3096  req->data.callstate.lineInstance = htolel(buttonInstance);
3097  req->data.callstate.callReference = htolel(callid);
3098 
3099  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CALL_STATE_MESSAGE to %s, state %s, inst %d, callid %u\n",
3100  d->name, callstate2str(state), buttonInstance, callid);
3101  transmit_response(d, req);
3102 }
3103 
3104 static void transmit_cfwdstate(struct skinny_device *d, struct skinny_line *l)
3105 {
3106  struct skinny_req *req;
3107  int anyon = 0;
3108 
3109  if (!(req = req_alloc(sizeof(struct forward_stat_message), FORWARD_STAT_MESSAGE)))
3110  return;
3111 
3112  if (l->cfwdtype & SKINNY_CFWD_ALL) {
3113  if (!ast_strlen_zero(l->call_forward_all)) {
3114  ast_copy_string(req->data.forwardstat.fwdallnum, l->call_forward_all, sizeof(req->data.forwardstat.fwdallnum));
3115  req->data.forwardstat.fwdall = htolel(1);
3116  anyon++;
3117  } else {
3118  req->data.forwardstat.fwdall = htolel(0);
3119  }
3120  }
3121  if (l->cfwdtype & SKINNY_CFWD_BUSY) {
3122  if (!ast_strlen_zero(l->call_forward_busy)) {
3123  ast_copy_string(req->data.forwardstat.fwdbusynum, l->call_forward_busy, sizeof(req->data.forwardstat.fwdbusynum));
3124  req->data.forwardstat.fwdbusy = htolel(1);
3125  anyon++;
3126  } else {
3127  req->data.forwardstat.fwdbusy = htolel(0);
3128  }
3129  }
3130  if (l->cfwdtype & SKINNY_CFWD_NOANSWER) {
3131  if (!ast_strlen_zero(l->call_forward_noanswer)) {
3132  ast_copy_string(req->data.forwardstat.fwdnoanswernum, l->call_forward_noanswer, sizeof(req->data.forwardstat.fwdnoanswernum));
3133  req->data.forwardstat.fwdnoanswer = htolel(1);
3134  anyon++;
3135  } else {
3136  req->data.forwardstat.fwdnoanswer = htolel(0);
3137  }
3138  }
3139  req->data.forwardstat.lineNumber = htolel(l->instance);
3140  if (anyon)
3142  else
3144 
3145  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting FORWARD_STAT_MESSAGE to %s, inst %d, all %s, busy %s, noans %s, active %d\n",
3146  d->name, l->instance, l->call_forward_all, l->call_forward_busy, l->call_forward_noanswer, anyon ? 7 : 0);
3147  transmit_response(d, req);
3148 }
3149 
3151 {
3152  struct skinny_req *req;
3153 
3155  return;
3156 
3160 
3161  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SPEED_DIAL_STAT_RES_MESSAGE to %s, inst %d, dir %s, display %s\n",
3162  d->name, sd->instance, sd->exten, sd->label);
3163  transmit_response(d, req);
3164 }
3165 
3166 //static void transmit_linestatres(struct skinny_device *d, struct skinny_line *l)
3167 static void transmit_linestatres(struct skinny_device *d, int instance)
3168 {
3169  struct skinny_req *req;
3170  struct skinny_line *l;
3171  struct skinny_speeddial *sd;
3172 
3173  if (!(req = req_alloc(sizeof(struct line_stat_res_message), LINE_STAT_RES_MESSAGE)))
3174  return;
3175 
3176  if ((l = find_line_by_instance(d, instance))) {
3177  req->data.linestat.lineNumber = letohl(l->instance);
3178  memcpy(req->data.linestat.lineDirNumber, l->name, sizeof(req->data.linestat.lineDirNumber));
3179  memcpy(req->data.linestat.lineDisplayName, l->label, sizeof(req->data.linestat.lineDisplayName));
3180  } else if ((sd = find_speeddial_by_instance(d, instance, 1))) {
3181  req->data.linestat.lineNumber = letohl(sd->instance);
3182  memcpy(req->data.linestat.lineDirNumber, sd->label, sizeof(req->data.linestat.lineDirNumber));
3183  memcpy(req->data.linestat.lineDisplayName, sd->label, sizeof(req->data.linestat.lineDisplayName));
3184  }
3185 
3186  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting LINE_STAT_RES_MESSAGE to %s, inst %d, num %s, label %s\n",
3187  d->name, l->instance, req->data.linestat.lineDirNumber, req->data.linestat.lineDisplayName);
3188  transmit_response(d, req);
3189 }
3190 
3192 {
3193  struct skinny_req *req;
3194  struct timeval now = ast_tvnow();
3195  struct ast_tm cmtime;
3196 
3197  if (!(req = req_alloc(sizeof(struct definetimedate_message), DEFINETIMEDATE_MESSAGE)))
3198  return;
3199 
3200  ast_localtime(&now, &cmtime, NULL);
3201  req->data.definetimedate.year = htolel(cmtime.tm_year+1900);
3202  req->data.definetimedate.month = htolel(cmtime.tm_mon+1);
3203  req->data.definetimedate.dayofweek = htolel(cmtime.tm_wday);
3204  req->data.definetimedate.day = htolel(cmtime.tm_mday);
3205  req->data.definetimedate.hour = htolel(cmtime.tm_hour);
3206  req->data.definetimedate.minute = htolel(cmtime.tm_min);
3207  req->data.definetimedate.seconds = htolel(cmtime.tm_sec);
3208  req->data.definetimedate.milliseconds = htolel(cmtime.tm_usec / 1000);
3209  req->data.definetimedate.timestamp = htolel(now.tv_sec);
3210 
3211  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DEFINETIMEDATE_MESSAGE to %s, date %u %u %u dow %u time %u:%u:%u.%u\n",
3214  transmit_response(d, req);
3215 }
3216 
3218 {
3219  struct skinny_req *req;
3220  if (!(req = req_alloc(sizeof(struct version_res_message), VERSION_RES_MESSAGE)))
3221  return;
3222 
3223  ast_copy_string(req->data.version.version, d->version_id, sizeof(req->data.version.version));
3224 
3225  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting VERSION_RES_MESSAGE to %s, version %s\n", d->name, d->version_id);
3226  transmit_response(d, req);
3227 }
3228 
3229 static void transmit_serverres(struct skinny_device *d)
3230 {
3231  struct skinny_req *req;
3232  if (!(req = req_alloc(sizeof(struct server_res_message), SERVER_RES_MESSAGE)))
3233  return;
3234 
3235  memcpy(req->data.serverres.server[0].serverName, ourhost,
3236  sizeof(req->data.serverres.server[0].serverName));
3238  req->data.serverres.serverIpAddr[0] = htolel(d->ourip.s_addr);
3239 
3240  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SERVER_RES_MESSAGE to %s, srvname %s %s:%d\n",
3241  d->name, ourhost, ast_inet_ntoa(d->ourip), ourport);
3242  transmit_response(d, req);
3243 }
3244 
3246 {
3247  struct skinny_req *req;
3248  int i;
3249  int x;
3250  int y;
3251  int keydefcount;
3252  const struct soft_key_definitions *softkeymode = soft_key_default_definitions;
3253 
3254  if (!(req = req_alloc(sizeof(struct soft_key_set_res_message), SOFT_KEY_SET_RES_MESSAGE)))
3255  return;
3256 
3257  SKINNY_DEBUG(DEBUG_TEMPLATE, 3, "Creating Softkey Template\n");
3258 
3259  keydefcount = ARRAY_LEN(soft_key_default_definitions);
3261  req->data.softkeysets.softKeySetCount = htolel(keydefcount);
3262  req->data.softkeysets.totalSoftKeySetCount = htolel(keydefcount);
3263  for (x = 0; x < keydefcount; x++) {
3264  const uint8_t *defaults = softkeymode->defaults;
3265  /* XXX I wanted to get the size of the array dynamically, but that wasn't wanting to work.
3266  This will have to do for now. */
3267  for (y = 0; y < softkeymode->count; y++) {
3268  for (i = 0; i < (sizeof(soft_key_template_default) / sizeof(struct soft_key_template_definition)); i++) {
3269  if (defaults[y] == i+1) {
3270  req->data.softkeysets.softKeySetDefinition[softkeymode->mode].softKeyTemplateIndex[y] = (i+1);
3271  req->data.softkeysets.softKeySetDefinition[softkeymode->mode].softKeyInfoIndex[y] = htoles(i+301);
3272  SKINNY_DEBUG(DEBUG_TEMPLATE, 4, "softKeySetDefinition : softKeyTemplateIndex: %d softKeyInfoIndex: %d\n",
3273  i+1, i+301);
3274  }
3275  }
3276  }
3277  softkeymode++;
3278  }
3279 
3280  SKINNY_DEBUG(DEBUG_PACKET | DEBUG_TEMPLATE, 3, "Transmitting SOFT_KEY_SET_RES_MESSAGE to %s, template data\n",
3281  d->name);
3282  transmit_response(d, req);
3283 }
3284 
3286 {
3287  struct skinny_req *req;
3288 
3290  return;
3291 
3297  sizeof(soft_key_template_default));
3298 
3299  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SOFT_KEY_TEMPLATE_RES_MESSAGE to %s, offset 0, keycnt %u, totalkeycnt %u, template data\n",
3301  transmit_response(d, req);
3302 }
3303 
3304 static void transmit_reset(struct skinny_device *d, int fullrestart)
3305 {
3306  struct skinny_req *req;
3307 
3308  if (!(req = req_alloc(sizeof(struct reset_message), RESET_MESSAGE)))
3309  return;
3310 
3311  if (fullrestart)
3312  req->data.reset.resetType = 2;
3313  else
3314  req->data.reset.resetType = 1;
3315 
3316  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting RESET_MESSAGE to %s, type %s\n",
3317  d->name, (fullrestart) ? "Restarting" : "Resetting");
3318  transmit_response(d, req);
3319 }
3320 
3321 static void transmit_keepaliveack(struct skinnysession *s)
3322 {
3323  struct skinny_req *req;
3324 
3325  if (!(req = req_alloc(0, KEEP_ALIVE_ACK_MESSAGE)))
3326  return;
3327 
3328 #ifdef AST_DEVMODE
3329  {
3330  struct skinny_device *d = s->device;
3331  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting KEEP_ALIVE_ACK_MESSAGE to %s\n", (d ? d->name : "unregistered"));
3332  }
3333 #endif
3335 }
3336 
3338 {
3339  struct skinny_req *req;
3340 
3341  if (!(req = req_alloc(sizeof(struct register_ack_message), REGISTER_ACK_MESSAGE)))
3342  return;
3343 
3344  req->data.regack.res[0] = '0';
3345  req->data.regack.res[1] = '\0';
3347  memcpy(req->data.regack.dateTemplate, date_format, sizeof(req->data.regack.dateTemplate));
3348  req->data.regack.res2[0] = '0';
3349  req->data.regack.res2[1] = '\0';
3351 
3352 #ifdef AST_DEVMODE
3353  {
3354  short res = req->data.regack.res[0] << 8 | req->data.regack.res[1];
3355  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];
3356  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting REGISTER_ACK_MESSAGE to %s, keepalive %d, datetemplate %s, seckeepalive %d, res 0x%04x, res2 0x%08x\n",
3357  d->name, keep_alive, date_format, keep_alive, (unsigned)res, res2);
3358  }
3359 #endif
3360 
3361  transmit_response(d, req);
3362 }
3363 
3365 {
3366  struct skinny_req *req;
3367 
3368  if (!(req = req_alloc(0, CAPABILITIES_REQ_MESSAGE)))
3369  return;
3370 
3371  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CAPABILITIES_REQ_MESSAGE to %s\n", d->name);
3372  transmit_response(d, req);
3373 }
3374 
3375 static void transmit_backspace(struct skinny_device *d, int instance, unsigned callid)
3376 {
3377  struct skinny_req *req;
3378 
3379  if (!(req = req_alloc(sizeof(struct bksp_req_message), BKSP_REQ_MESSAGE)))
3380  return;
3381 
3382  req->data.bkspmessage.instance = htolel(instance);
3383  req->data.bkspmessage.callreference = htolel(callid);
3384 
3385  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting BKSP_REQ_MESSAGE to %s, inst %d, callid %u \n",
3386  d->name, instance, callid);
3387  transmit_response(d, req);
3388 }
3389 
3390 static void transmit_serviceurlstat(struct skinny_device *d, int instance)
3391 {
3392  struct skinny_req *req;
3393  struct skinny_serviceurl *surl;
3394 
3395  if (!(req = req_alloc(sizeof(struct serviceurl_stat_message), SERVICEURL_STAT_MESSAGE)))
3396  return;
3397 
3398  AST_LIST_TRAVERSE(&d->serviceurls, surl, list) {
3399  if (surl->instance == instance) {
3400  break;
3401  }
3402  }
3403 
3404  if (surl) {
3406  memcpy(req->data.serviceurlmessage.url, surl->url, sizeof(req->data.serviceurlmessage.url));
3407  }
3409 
3410  SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SERVICEURL_STAT_MESSAGE to %s, inst %d\n",
3411  d->name, instance);
3412  transmit_response(d, req);
3413 }
3414 
3415 static int skinny_extensionstate_cb(const char *context, const char *exten, struct ast_state_cb_info *info, void *data)
3416 {
3417  struct skinny_container *container = data;
3418  struct skinny_device *d = NULL;
3419  char hint[AST_MAX_EXTENSION];
3420  int state = info->exten_state;
3421 
3422  /* only interested in device state here */
3423  if (info->reason != AST_HINT_UPDATE_DEVICE) {
3424  return 0;
3425  }
3426 
3427  if (container->type == SKINNY_SDCONTAINER) {
3428  struct skinny_speeddial *sd = container->data;
3429  d = sd->parent;
3430 
3431  SKINNY_DEBUG(DEBUG_HINT, 3, "Got hint %s on speeddial %s\n", ast_extension_state2str(state), sd->label);
3432 
3433  if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, sd->context, sd->exten)) {
3434  /* If they are not registered, we will override notification and show no availability */
3438  return 0;
3439  }
3440  switch (state) {
3441  case AST_EXTENSION_DEACTIVATED: /* Retry after a while */
3442  case AST_EXTENSION_REMOVED: /* Extension is gone */
3443  SKINNY_DEBUG(DEBUG_HINT, 3, "Extension state: Watcher for hint %s %s. Notify Device %s\n",
3444  exten, state == AST_EXTENSION_DEACTIVATED ? "deactivated" : "removed", d->name);
3445  sd->stateid = -1;
3448  break;
3449  case AST_EXTENSION_RINGING:
3453  break;
3454  case AST_EXTENSION_BUSY: /* callstate = SKINNY_BUSY wasn't wanting to work - I'll settle for this */
3455  case AST_EXTENSION_INUSE:
3458  break;
3459  case AST_EXTENSION_ONHOLD:
3462  break;
3464  default:
3467  break;
3468  }
3469  }
3470  sd->laststate = state;
3471  } else if (container->type == SKINNY_SUBLINECONTAINER) {
3472  struct skinny_subline *subline = container->data;
3473  struct skinny_line *l = subline->line;
3474  d = l->device;
3475 
3476  SKINNY_DEBUG(DEBUG_HINT, 3, "Got hint %s on subline %s (%s@%s)\n", ast_extension_state2str(state), subline->name, exten, context);
3477 
3478  subline->extenstate = state;
3479 
3480  if (subline->callid == 0) {
3481  return 0;
3482  }
3483 
3484  switch (state) {
3485  case AST_EXTENSION_RINGING: /* Handled by normal ringin */
3486  break;
3487  case AST_EXTENSION_INUSE:
3488  if (subline->sub && (subline->sub->substate == SKINNY_CONNECTED)) { /* Device has a real call */
3489  transmit_callstate(d, l->instance, subline->callid, SKINNY_CONNECTED);
3490  transmit_selectsoftkeys(d, l->instance, subline->callid, KEYDEF_CONNECTED, KEYMASK_ALL);
3491  send_displaypromptstatus(d, OCTAL_CONNECTED, "", 0, l->instance, subline->callid);
3492  } else { /* Some other device has active call */
3493  transmit_callstate(d, l->instance, subline->callid, SKINNY_CALLREMOTEMULTILINE);
3495  send_displaypromptstatus(d, "In Use", "", 0, l->instance, subline->callid);
3496  }
3500  break;
3501  case AST_EXTENSION_ONHOLD:
3502  transmit_callstate(d, l->instance, subline->callid, SKINNY_HOLD);
3503  transmit_selectsoftkeys(d, l->instance, subline->callid, KEYDEF_SLAHOLD, KEYMASK_ALL);
3504  send_displaypromptstatus(d, "Hold", "", 0, l->instance, subline->callid);
3507  break;
3509  transmit_callstate(d, l->instance, subline->callid, SKINNY_ONHOOK);
3510  transmit_selectsoftkeys(d, l->instance, subline->callid, KEYDEF_ONHOOK, KEYMASK_ALL);
3511  transmit_clearpromptmessage(d, l->instance, subline->callid);
3514  subline->callid = 0;
3515  break;
3516  default:
3517  ast_log(LOG_WARNING, "AST_EXTENSION_STATE %s not configured\n", ast_extension_state2str(state));
3518  }
3519  } else {
3520  ast_log(LOG_WARNING, "Invalid data supplied to skinny_extensionstate_cb\n");
3521  }
3522 
3523  return 0;
3524 }
3525 
3526 static void update_connectedline(struct skinny_subchannel *sub, const void *data, size_t datalen)
3527 {
3528  struct ast_channel *c = sub->owner;
3529  struct skinny_line *l = sub->line;
3530  struct skinny_device *d = l->device;
3531 
3532  if (!d->session) {
3533  return;
3534  }
3535 
3536  if (sub->calldirection == SKINNY_OUTGOING && !sub->origtonum) {
3537  /* Do not set origtonum before here or origtoname won't be set */
3538  sub->origtonum = ast_strdup(sub->exten);
3539  if (ast_channel_connected(c)->id.name.valid) {
3540  sub->origtoname = ast_strdup(ast_channel_connected(c)->id.name.str);
3541  }
3542  }
3543 
3544  if (!ast_channel_caller(c)->id.number.valid
3546  || !ast_channel_connected(c)->id.number.valid
3548  return;
3549 
3550  SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Updating\n", sub->callid);
3551 
3552  send_callinfo(sub);
3553 }
3554 
3555 static void mwi_event_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg)
3556 {
3557  struct skinny_line *l = userdata;
3558  struct skinny_device *d = l->device;
3559  struct skinny_line *l2;
3560  int dev_msgs = 0;
3561 
3562  if (!d || !d->session) {
3563  return;
3564  }
3565 
3566  if (msg && ast_mwi_state_type() == stasis_message_type(msg)) {
3567  struct ast_mwi_state *mwi_state = stasis_message_data(msg);
3568  l->newmsgs = mwi_state->new_msgs;
3569  }
3570 
3571  if (l->newmsgs) {
3573  } else {
3575  }
3576 
3577  /* find out whether the device lamp should be on or off */
3578  AST_LIST_TRAVERSE(&d->lines, l2, list) {
3579  if (l2->newmsgs) {
3580  dev_msgs++;
3581  }
3582  }
3583 
3584  if (dev_msgs) {
3586  } else {
3588  }
3589  ast_verb(3, "Skinny mwi_event_cb found %d new messages\n", l->newmsgs);
3590 }
3591 
3592 /* I do not believe skinny can deal with video.
3593  Anyone know differently? */
3594 /* Yes, it can. Currently 7985 and Cisco VT Advantage do video. */
3595 static enum ast_rtp_glue_result skinny_get_vrtp_peer(struct ast_channel *c, struct ast_rtp_instance **instance)
3596 {
3597  struct skinny_subchannel *sub = NULL;
3598 
3599  if (!(sub = ast_channel_tech_pvt(c)) || !(sub->vrtp))
3601 
3602  ao2_ref(sub->vrtp, +1);
3603  *instance = sub->vrtp;
3604 
3606 }
3607 
3608 static enum ast_rtp_glue_result skinny_get_rtp_peer(struct ast_channel *c, struct ast_rtp_instance **instance)
3609 {
3610  struct skinny_subchannel *sub = NULL;
3611  struct skinny_line *l;
3613 
3614  SKINNY_DEBUG(DEBUG_AUDIO, 4, "skinny_get_rtp_peer() Channel = %s\n", ast_channel_name(c));
3615 
3616  if (!(sub = ast_channel_tech_pvt(c)))
3618 
3620 
3621  if (!(sub->rtp)){
3624  }
3625 
3626  ao2_ref(sub->rtp, +1);
3627  *instance = sub->rtp;
3628 
3629  l = sub->line;
3630 
3631  if (!l->directmedia || l->nat){
3633  SKINNY_DEBUG(DEBUG_AUDIO, 4, "skinny_get_rtp_peer() Using AST_RTP_GLUE_RESULT_LOCAL \n");
3634  }
3635 
3637 
3638  return res;
3639 
3640 }
3641 
3642 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)
3643 {
3644  struct skinny_subchannel *sub;
3645  struct skinny_line *l;
3646  struct skinny_device *d;
3647  struct sockaddr_in us = { 0, };
3648  struct sockaddr_in them = { 0, };
3649  struct ast_sockaddr them_tmp;
3650  struct ast_sockaddr us_tmp;
3651 
3653 
3655  return 0;
3656 
3657  if (!sub) {
3658  return -1;
3659  }
3660 
3661  l = sub->line;
3662  d = l->device;
3663 
3664  if (rtp){
3665  struct ast_format *tmpfmt;
3666  unsigned int framing;
3667  ast_rtp_instance_get_remote_address(rtp, &them_tmp);
3668  ast_sockaddr_to_sin(&them_tmp, &them);
3669 
3670  /* Shutdown any early-media or previous media on re-invite */
3672 
3673  SKINNY_DEBUG(DEBUG_AUDIO, 4, "Peerip = %s:%d\n", ast_inet_ntoa(them.sin_addr), ntohs(them.sin_port));
3674 
3675  tmpfmt = ast_format_cap_get_format(l->cap, 0);
3676  framing = ast_format_cap_get_format_framing(l->cap, tmpfmt);
3677 
3678  SKINNY_DEBUG(DEBUG_AUDIO, 4, "Setting payloadType to '%s' (%u ms)\n", ast_format_get_name(tmpfmt), framing);
3679 
3680  if (!(l->directmedia) || (l->nat)){
3681  ast_rtp_instance_get_local_address(rtp, &us_tmp);
3682  ast_sockaddr_to_sin(&us_tmp, &us);
3683  us.sin_addr.s_addr = us.sin_addr.s_addr ? us.sin_addr.s_addr : d->ourip.s_addr;
3684  transmit_startmediatransmission(d, sub, us, tmpfmt, framing);
3685  } else {
3686  transmit_startmediatransmission(d, sub, them, tmpfmt, framing);
3687  }
3688 
3689  ao2_ref(tmpfmt, -1);
3690 
3691  return 0;
3692  }
3693  /* Need a return here to break the bridge */
3694  return 0;
3695 }
3696 
3697 static struct ast_rtp_glue skinny_rtp_glue = {
3698  .type = "Skinny",
3699  .get_rtp_info = skinny_get_rtp_peer,
3700  .get_vrtp_info = skinny_get_vrtp_peer,
3701  .update_peer = skinny_set_rtp_peer,
3702 };
3703 
3704 #ifdef AST_DEVMODE
3705 static char *skinny_debugs(void)
3706 {
3707  char *ptr;
3708  int posn = 0;
3709 
3710  ptr = dbgcli_buf;
3711  ptr[0] = '\0';
3712  if (skinnydebug & DEBUG_GENERAL) {
3713  strcpy(ptr, "general "); /* SAFE */
3714  posn += 8;
3715  ptr += 8;
3716  }
3717  if (skinnydebug & DEBUG_SUB) {
3718  strcpy(ptr, "sub "); /* SAFE */
3719  posn += 4;
3720  ptr += 4;
3721  }
3722  if (skinnydebug & DEBUG_AUDIO) {
3723  strcpy(ptr, "audio "); /* SAFE */
3724  posn += 6;
3725  ptr += 6;
3726  }
3727  if (skinnydebug & DEBUG_PACKET) {
3728  strcpy(ptr, "packet "); /* SAFE */
3729  posn += 7;
3730  ptr += 7;
3731  }
3732  if (skinnydebug & DEBUG_LOCK) {
3733  strcpy(ptr, "lock "); /* SAFE */
3734  posn += 5;
3735  ptr += 5;
3736  }
3737  if (skinnydebug & DEBUG_TEMPLATE) {
3738  strcpy(ptr, "template "); /* SAFE */
3739  posn += 9;
3740  ptr += 9;
3741  }
3742  if (skinnydebug & DEBUG_THREAD) {
3743  strcpy(ptr, "thread "); /* SAFE */
3744  posn += 7;
3745  ptr += 7;
3746  }
3747  if (skinnydebug & DEBUG_HINT) {
3748  strcpy(ptr, "hint "); /* SAFE */
3749  posn += 5;
3750  ptr += 5;
3751  }
3752  if (skinnydebug & DEBUG_KEEPALIVE) {
3753  strcpy(ptr, "keepalive "); /* SAFE */
3754  posn += 10;
3755  ptr += 10;
3756  }
3757  if (posn > 0) {
3758  strncpy(--ptr, "\0", 1);
3759  }
3760  return dbgcli_buf;
3761 }
3762 
3763 static char *complete_skinny_debug(const char *line, const char *word, int pos, int state)
3764 {
3765  const char *debugOpts[]={ "all","audio","hint","keepalive","lock","off","packet","show","sub","template","thread",NULL };
3766  char *wordptr = (char *)word;
3767  char buf[32];
3768  char *bufptr = buf;
3769  int buflen = sizeof(buf);
3770  int wordlen;
3771  int which = 0;
3772  int i = 0;
3773 
3774  if (*word == '+' || *word == '-' || *word == '!') {
3775  *bufptr = *word;
3776  wordptr++;
3777  bufptr++;
3778  buflen--;
3779  }
3780  wordlen = strlen(wordptr);
3781 
3782  while (debugOpts[i]) {
3783  if (!strncasecmp(wordptr, debugOpts[i], wordlen) && ++which > state) {
3784  ast_copy_string(bufptr, debugOpts[i], buflen);
3785  return ast_strdup(buf);
3786  }
3787  i++;
3788  }
3789 
3790  return NULL;
3791 }
3792 
3793 static char *handle_skinny_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3794 {
3795  int i;
3796  int result = 0;
3797  const char *arg;
3798  int bitmask;
3799  int negate;
3800 
3801  switch (cmd) {
3802  case CLI_INIT:
3803  e->command = "skinny debug";
3804  e->usage =
3805  "Usage: skinny debug {audio|hint|keepalive|lock|off|packet|show|sub|template|thread}\n"
3806  " Enables/Disables various Skinny debugging messages\n";
3807  return NULL;
3808  case CLI_GENERATE:
3809  return complete_skinny_debug(a->line, a->word, a->pos, a->n);
3810 
3811  }
3812 
3813  if (a->argc < 3)
3814  return CLI_SHOWUSAGE;
3815 
3816  if (a->argc == 3 && !strncasecmp(a->argv[e->args-1], "show", 4)) {
3817  ast_cli(a->fd, "Skinny Debugging - %s\n", skinny_debugs());
3818  return CLI_SUCCESS;
3819  }
3820 
3821  for(i = e->args; i < a->argc; i++) {
3822  result++;
3823  arg = a->argv[i];
3824 
3825  if (!strncasecmp(arg, "off", 3)) {
3826  skinnydebug = 0;
3827  continue;
3828  }
3829 
3830  if (!strncasecmp(arg, "all", 3)) {
3831  skinnydebug = DEBUG_GENERAL|DEBUG_SUB|DEBUG_PACKET|DEBUG_AUDIO|DEBUG_LOCK|DEBUG_TEMPLATE|DEBUG_THREAD|DEBUG_HINT|DEBUG_KEEPALIVE;
3832  continue;
3833  }
3834 
3835  if (!strncasecmp(arg, "-", 1) || !strncasecmp(arg, "!", 1)) {
3836  negate = 1;
3837  arg++;
3838  } else if (!strncasecmp(arg, "+", 1)) {
3839  negate = 0;
3840  arg++;
3841  } else {
3842  negate = 0;
3843  }
3844 
3845  if (!strncasecmp(arg, "general", 7)) {
3846  bitmask = DEBUG_GENERAL;
3847  } else if (!strncasecmp(arg, "sub", 3)) {
3848  bitmask = DEBUG_SUB;
3849  } else if (!strncasecmp(arg, "packet", 6)) {
3850  bitmask = DEBUG_PACKET;
3851  } else if (!strncasecmp(arg, "audio", 5)) {
3852  bitmask = DEBUG_AUDIO;
3853  } else if (!strncasecmp(arg, "lock", 4)) {
3854  bitmask = DEBUG_LOCK;
3855  } else if (!strncasecmp(arg, "template", 8)) {
3856  bitmask = DEBUG_TEMPLATE;
3857  } else if (!strncasecmp(arg, "thread", 6)) {
3858  bitmask = DEBUG_THREAD;
3859  } else if (!strncasecmp(arg, "hint", 4)) {
3860  bitmask = DEBUG_HINT;
3861  } else if (!strncasecmp(arg, "keepalive", 9)) {
3862  bitmask = DEBUG_KEEPALIVE;
3863  } else {
3864  ast_cli(a->fd, "Skinny Debugging - option '%s' unknown\n", a->argv[i]);
3865  result--;
3866  continue;
3867  }
3868 
3869  if (negate) {
3870  skinnydebug &= ~bitmask;
3871  } else {
3872  skinnydebug |= bitmask;
3873  }
3874  }
3875  if (result) {
3876  ast_cli(a->fd, "Skinny Debugging - %s\n", skinnydebug ? skinny_debugs() : "off");
3877  return CLI_SUCCESS;
3878  } else {
3879  return CLI_SHOWUSAGE;
3880  }
3881 }
3882 #endif
3883 
3884 static char *handle_skinny_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3885 {
3886  switch (cmd) {
3887  case CLI_INIT:
3888  e->command = "skinny reload";
3889  e->usage =
3890  "Usage: skinny reload\n"
3891  " Reloads the chan_skinny configuration\n";
3892  return NULL;
3893  case CLI_GENERATE:
3894  return NULL;
3895  }
3896 
3897  if (a->argc != e->args)
3898  return CLI_SHOWUSAGE;
3899 
3900  skinny_reload();
3901  return CLI_SUCCESS;
3902 
3903 }
3904 
3905 static char *complete_skinny_devices(const char *word, int state)
3906 {
3907  struct skinny_device *d;
3908  int wordlen = strlen(word), which = 0;
3909 
3911  if (!strncasecmp(word, d->name, wordlen) && ++which > state) {
3912  return ast_strdup(d->name);
3913  }
3914  }
3915 
3916  return NULL;
3917 }
3918 
3919 static char *complete_skinny_show_device(const char *line, const char *word, int pos, int state)
3920 {
3921  return (pos == 3 ? complete_skinny_devices(word, state) : NULL);
3922 }
3923 
3924 static char *complete_skinny_reset(const char *line, const char *word, int pos, int state)
3925 {
3926  if (pos == 2) {
3927  static const char * const completions[] = { "all", NULL };
3928  char *ret = ast_cli_complete(word, completions, state);
3929  if (!ret) {
3930  ret = complete_skinny_devices(word, state - 1);
3931  }
3932  return ret;
3933  } else if (pos == 3) {
3934  static const char * const completions[] = { "restart", NULL };
3935  return ast_cli_complete(word, completions, state);
3936  }
3937 
3938  return NULL;
3939 }
3940 
3941 static char *complete_skinny_show_line(const char *line, const char *word, int pos, int state)
3942 {
3943  if (pos == 3) {
3944  struct skinny_device *d;
3945  struct skinny_line *l;
3946  int wordlen = strlen(word), which = 0;
3947 
3949  AST_LIST_TRAVERSE(&d->lines, l, list) {
3950  if (!strncasecmp(word, l->name, wordlen) && ++which > state) {
3951  return ast_strdup(l->name);
3952  }
3953  }
3954  }
3955  } else if (pos == 4) {
3956  static const char * const completions[] = { "on", NULL };
3957  return ast_cli_complete(word, completions, state);
3958  } else if (pos == 5) {
3960  }
3961 
3962  return NULL;
3963 }
3964 
3965 static char *handle_skinny_reset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3966 {
3967  struct skinny_device *d;
3968 
3969  switch (cmd) {
3970  case CLI_INIT:
3971  e->command = "skinny reset";
3972  e->usage =
3973  "Usage: skinny reset <DeviceId|DeviceName|all> [restart]\n"
3974  " Causes a Skinny device to reset itself, optionally with a full restart\n";
3975  return NULL;
3976  case CLI_GENERATE:
3977  return complete_skinny_reset(a->line, a->word, a->pos, a->n);
3978  }
3979 
3980  if (a->argc < 3 || a->argc > 4)
3981  return CLI_SHOWUSAGE;
3982 
3985  int resetonly = 1;
3986