chan_skinny.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * chan_skinny was developed by Jeremy McNamara & Florian Overkamp
00007  * chan_skinny was heavily modified/fixed by North Antara
00008  *
00009  * See http://www.asterisk.org for more information about
00010  * the Asterisk project. Please do not directly contact
00011  * any of the maintainers of this project for assistance;
00012  * the project provides a web site, mailing lists and IRC
00013  * channels for your use.
00014  *
00015  * This program is free software, distributed under the terms of
00016  * the GNU General Public License Version 2. See the LICENSE file
00017  * at the top of the source tree.
00018  */
00019 
00020 /*! \file
00021  *
00022  * \brief Implementation of the Skinny protocol
00023  *
00024  * \author Jeremy McNamara & Florian Overkamp & North Antara
00025  * \ingroup channel_drivers
00026  */
00027 
00028 /*! \li \ref chan_skinny.c uses the configuration file \ref skinny.conf
00029  * \addtogroup configuration_file
00030  */
00031 
00032 /*! \page skinny.conf skinny.conf
00033  * \verbinclude skinny.conf.sample
00034  */
00035 
00036 /*** MODULEINFO
00037    <support_level>extended</support_level>
00038  ***/
00039 
00040 #include "asterisk.h"
00041 
00042 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 434471 $")
00043 
00044 #include <sys/socket.h>
00045 #include <netinet/in.h>
00046 #include <netinet/tcp.h>
00047 #include <sys/ioctl.h>
00048 #include <net/if.h>
00049 #include <fcntl.h>
00050 #include <netdb.h>
00051 #include <arpa/inet.h>
00052 #include <sys/signal.h>
00053 #include <signal.h>
00054 #include <ctype.h>
00055 
00056 #include "asterisk/lock.h"
00057 #include "asterisk/channel.h"
00058 #include "asterisk/config.h"
00059 #include "asterisk/module.h"
00060 #include "asterisk/pbx.h"
00061 #include "asterisk/sched.h"
00062 #include "asterisk/io.h"
00063 #include "asterisk/rtp_engine.h"
00064 #include "asterisk/netsock2.h"
00065 #include "asterisk/acl.h"
00066 #include "asterisk/callerid.h"
00067 #include "asterisk/cli.h"
00068 #include "asterisk/manager.h"
00069 #include "asterisk/say.h"
00070 #include "asterisk/astdb.h"
00071 #include "asterisk/causes.h"
00072 #include "asterisk/pickup.h"
00073 #include "asterisk/app.h"
00074 #include "asterisk/musiconhold.h"
00075 #include "asterisk/utils.h"
00076 #include "asterisk/dsp.h"
00077 #include "asterisk/stringfields.h"
00078 #include "asterisk/abstract_jb.h"
00079 #include "asterisk/threadstorage.h"
00080 #include "asterisk/devicestate.h"
00081 #include "asterisk/indications.h"
00082 #include "asterisk/linkedlists.h"
00083 #include "asterisk/stasis_endpoints.h"
00084 #include "asterisk/bridge.h"
00085 #include "asterisk/parking.h"
00086 #include "asterisk/stasis_channels.h"
00087 #include "asterisk/format_cache.h"
00088 
00089 /*** DOCUMENTATION
00090    <manager name="SKINNYdevices" language="en_US">
00091       <synopsis>
00092          List SKINNY devices (text format).
00093       </synopsis>
00094       <syntax>
00095          <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
00096       </syntax>
00097       <description>
00098          <para>Lists Skinny devices in text format with details on current status.
00099          Devicelist will follow as separate events, followed by a final event called
00100          DevicelistComplete.</para>
00101       </description>
00102    </manager>
00103    <manager name="SKINNYshowdevice" language="en_US">
00104       <synopsis>
00105          Show SKINNY device (text format).
00106       </synopsis>
00107       <syntax>
00108          <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
00109          <parameter name="Device" required="true">
00110             <para>The device name you want to check.</para>
00111          </parameter>
00112       </syntax>
00113       <description>
00114          <para>Show one SKINNY device with details on current status.</para>
00115       </description>
00116    </manager>
00117    <manager name="SKINNYlines" language="en_US">
00118       <synopsis>
00119          List SKINNY lines (text format).
00120       </synopsis>
00121       <syntax>
00122          <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
00123       </syntax>
00124       <description>
00125          <para>Lists Skinny lines in text format with details on current status.
00126          Linelist will follow as separate events, followed by a final event called
00127          LinelistComplete.</para>
00128       </description>
00129    </manager>
00130    <manager name="SKINNYshowline" language="en_US">
00131       <synopsis>
00132          Show SKINNY line (text format).
00133       </synopsis>
00134       <syntax>
00135          <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
00136          <parameter name="Line" required="true">
00137             <para>The line name you want to check.</para>
00138          </parameter>
00139       </syntax>
00140       <description>
00141          <para>Show one SKINNY line with details on current status.</para>
00142       </description>
00143    </manager>
00144  ***/
00145 
00146 /* Skinny debugging only available if asterisk configured with --enable-dev-mode */
00147 #ifdef AST_DEVMODE
00148 static int skinnydebug = 0;
00149 char dbgcli_buf[256];
00150 #define DEBUG_GENERAL   (1 << 1)
00151 #define DEBUG_SUB (1 << 2)
00152 #define DEBUG_PACKET (1 << 3)
00153 #define DEBUG_AUDIO  (1 << 4)
00154 #define DEBUG_LOCK   (1 << 5)
00155 #define DEBUG_TEMPLATE  (1 << 6)
00156 #define DEBUG_THREAD (1 << 7)
00157 #define DEBUG_HINT   (1 << 8)
00158 #define DEBUG_KEEPALIVE (1 << 9)
00159 #define SKINNY_DEBUG(type, verb_level, text, ...)                 \
00160    do{                                 \
00161       if (skinnydebug & (type)) {                     \
00162          ast_verb(verb_level, "[%d] " text, ast_get_tid(), ##__VA_ARGS__); \
00163       }                             \
00164    }while(0)
00165 #else
00166 #define SKINNY_DEBUG(type, verb_level, text, ...)
00167 #endif
00168 
00169 /*************************************
00170  * Skinny/Asterisk Protocol Settings *
00171  *************************************/
00172 static const char tdesc[] = "Skinny Client Control Protocol (Skinny)";
00173 static const char config[] = "skinny.conf";
00174 
00175 static struct ast_format_cap *default_cap;
00176 
00177 enum skinny_codecs {
00178    SKINNY_CODEC_ALAW = 2,
00179    SKINNY_CODEC_ULAW = 4,
00180    SKINNY_CODEC_G722 = 6,
00181    SKINNY_CODEC_G723_1 = 9,
00182    SKINNY_CODEC_G729A = 12,
00183    SKINNY_CODEC_G726_32 = 82, /* XXX Which packing order does this translate to? */
00184    SKINNY_CODEC_H261 = 100,
00185    SKINNY_CODEC_H263 = 101
00186 };
00187 
00188 #define DEFAULT_SKINNY_PORT 2000
00189 #define DEFAULT_SKINNY_BACKLOG 2
00190 #define SKINNY_MAX_PACKET 2000
00191 #define DEFAULT_AUTH_TIMEOUT 30
00192 #define DEFAULT_AUTH_LIMIT 50
00193 
00194 static struct {
00195    unsigned int tos;
00196    unsigned int tos_audio;
00197    unsigned int tos_video;
00198    unsigned int cos;
00199    unsigned int cos_audio;
00200    unsigned int cos_video;
00201 } qos = { 0, 0, 0, 0, 0, 0 };
00202 
00203 static int keep_alive = 120;
00204 static int auth_timeout = DEFAULT_AUTH_TIMEOUT;
00205 static int auth_limit = DEFAULT_AUTH_LIMIT;
00206 static int unauth_sessions = 0;
00207 static char immed_dialchar;
00208 static char vmexten[AST_MAX_EXTENSION];      /* Voicemail pilot number */
00209 static char used_context[AST_MAX_EXTENSION]; /* placeholder to check if context are already used in regcontext */
00210 static char regcontext[AST_MAX_CONTEXT];     /* Context for auto-extension */
00211 static char date_format[6] = "D-M-Y";
00212 static char version_id[16] = "P002F202";
00213 
00214 #if __BYTE_ORDER == __LITTLE_ENDIAN
00215 #define letohl(x) (x)
00216 #define letohs(x) (x)
00217 #define htolel(x) (x)
00218 #define htoles(x) (x)
00219 #else
00220 #if defined(HAVE_BYTESWAP_H)
00221 #include <byteswap.h>
00222 #define letohl(x) bswap_32(x)
00223 #define letohs(x) bswap_16(x)
00224 #define htolel(x) bswap_32(x)
00225 #define htoles(x) bswap_16(x)
00226 #elif defined(HAVE_SYS_ENDIAN_SWAP16)
00227 #include <sys/endian.h>
00228 #define letohl(x) __swap32(x)
00229 #define letohs(x) __swap16(x)
00230 #define htolel(x) __swap32(x)
00231 #define htoles(x) __swap16(x)
00232 #elif defined(HAVE_SYS_ENDIAN_BSWAP16)
00233 #include <sys/endian.h>
00234 #define letohl(x) bswap32(x)
00235 #define letohs(x) bswap16(x)
00236 #define htolel(x) bswap32(x)
00237 #define htoles(x) bswap16(x)
00238 #else
00239 #define __bswap_16(x) \
00240    ((((x) & 0xff00) >> 8) | \
00241     (((x) & 0x00ff) << 8))
00242 #define __bswap_32(x) \
00243    ((((x) & 0xff000000) >> 24) | \
00244     (((x) & 0x00ff0000) >>  8) | \
00245     (((x) & 0x0000ff00) <<  8) | \
00246     (((x) & 0x000000ff) << 24))
00247 #define letohl(x) __bswap_32(x)
00248 #define letohs(x) __bswap_16(x)
00249 #define htolel(x) __bswap_32(x)
00250 #define htoles(x) __bswap_16(x)
00251 #endif
00252 #endif
00253 
00254 /*! Global jitterbuffer configuration - by default, jb is disabled
00255  *  \note Values shown here match the defaults shown in skinny.conf.sample */
00256 static struct ast_jb_conf default_jbconf =
00257 {
00258    .flags = 0,
00259    .max_size = 200,
00260    .resync_threshold = 1000,
00261    .impl = "fixed",
00262    .target_extra = 40,
00263 };
00264 static struct ast_jb_conf global_jbconf;
00265 
00266 #ifdef AST_DEVMODE
00267 AST_THREADSTORAGE(message2str_threadbuf);
00268 #define MESSAGE2STR_BUFSIZE   35
00269 #endif
00270 
00271 AST_THREADSTORAGE(device2str_threadbuf);
00272 #define DEVICE2STR_BUFSIZE   15
00273 
00274 AST_THREADSTORAGE(control2str_threadbuf);
00275 #define CONTROL2STR_BUFSIZE   100
00276 
00277 AST_THREADSTORAGE(substate2str_threadbuf);
00278 #define SUBSTATE2STR_BUFSIZE   15
00279 
00280 AST_THREADSTORAGE(callstate2str_threadbuf);
00281 #define CALLSTATE2STR_BUFSIZE   15
00282 
00283 /*********************
00284  * Protocol Messages *
00285  *********************/
00286 /* message types */
00287 #define KEEP_ALIVE_MESSAGE 0x0000
00288 /* no additional struct */
00289 
00290 #define REGISTER_MESSAGE 0x0001
00291 struct register_message {
00292    char name[16];
00293    uint32_t userId;
00294    uint32_t instance;
00295    uint32_t ip;
00296    uint32_t type;
00297    uint32_t maxStreams;
00298    uint32_t space;
00299    uint8_t protocolVersion;
00300    /*! \brief space2 is used for newer version of skinny */
00301    char space2[3];
00302 };
00303 
00304 #define IP_PORT_MESSAGE 0x0002
00305 
00306 #define KEYPAD_BUTTON_MESSAGE 0x0003
00307 struct keypad_button_message {
00308    uint32_t button;
00309    uint32_t lineInstance;
00310    uint32_t callReference;
00311 };
00312 
00313 
00314 #define ENBLOC_CALL_MESSAGE 0x0004
00315 struct enbloc_call_message {
00316    char calledParty[24];
00317 };
00318 
00319 #define STIMULUS_MESSAGE 0x0005
00320 struct stimulus_message {
00321    uint32_t stimulus;
00322    uint32_t stimulusInstance;
00323    uint32_t callreference;
00324 };
00325 
00326 #define OFFHOOK_MESSAGE 0x0006
00327 struct offhook_message {
00328    uint32_t instance;
00329    uint32_t reference;
00330 };
00331 
00332 #define ONHOOK_MESSAGE 0x0007
00333 struct onhook_message {
00334    uint32_t instance;
00335    uint32_t reference;
00336 };
00337 
00338 #define CAPABILITIES_RES_MESSAGE 0x0010
00339 struct station_capabilities {
00340    uint32_t codec; /* skinny codec, not ast codec */
00341    uint32_t frames;
00342    union {
00343       char res[8];
00344       uint32_t rate;
00345    } payloads;
00346 };
00347 
00348 #define SKINNY_MAX_CAPABILITIES 18
00349 
00350 struct capabilities_res_message {
00351    uint32_t count;
00352    struct station_capabilities caps[SKINNY_MAX_CAPABILITIES];
00353 };
00354 
00355 #define SPEED_DIAL_STAT_REQ_MESSAGE 0x000A
00356 struct speed_dial_stat_req_message {
00357    uint32_t speedDialNumber;
00358 };
00359 
00360 #define LINE_STATE_REQ_MESSAGE 0x000B
00361 struct line_state_req_message {
00362    uint32_t lineNumber;
00363 };
00364 
00365 #define TIME_DATE_REQ_MESSAGE 0x000D
00366 #define BUTTON_TEMPLATE_REQ_MESSAGE 0x000E
00367 #define VERSION_REQ_MESSAGE 0x000F
00368 #define SERVER_REQUEST_MESSAGE 0x0012
00369 
00370 #define ALARM_MESSAGE 0x0020
00371 struct alarm_message {
00372    uint32_t alarmSeverity;
00373    char displayMessage[80];
00374    uint32_t alarmParam1;
00375    uint32_t alarmParam2;
00376 };
00377 
00378 #define OPEN_RECEIVE_CHANNEL_ACK_MESSAGE 0x0022
00379 struct open_receive_channel_ack_message_ip4 {
00380    uint32_t status;
00381    uint32_t ipAddr;
00382    uint32_t port;
00383    uint32_t callReference;
00384 };
00385 struct open_receive_channel_ack_message_ip6 {
00386    uint32_t status;
00387    uint32_t space;
00388    char ipAddr[16];
00389    uint32_t port;
00390    uint32_t callReference;
00391 };
00392 
00393 #define SOFT_KEY_SET_REQ_MESSAGE 0x0025
00394 
00395 #define SOFT_KEY_EVENT_MESSAGE 0x0026
00396 struct soft_key_event_message {
00397    uint32_t softKeyEvent;
00398    uint32_t instance;
00399    uint32_t callreference;
00400 };
00401 
00402 #define UNREGISTER_MESSAGE 0x0027
00403 #define SOFT_KEY_TEMPLATE_REQ_MESSAGE 0x0028
00404 #define HEADSET_STATUS_MESSAGE 0x002B
00405 #define REGISTER_AVAILABLE_LINES_MESSAGE 0x002D
00406 
00407 #define SERVICEURL_STATREQ_MESSAGE 0x0033
00408 struct serviceurl_statreq_message {
00409    uint32_t instance;
00410 };
00411 
00412 #define REGISTER_ACK_MESSAGE 0x0081
00413 struct register_ack_message {
00414    uint32_t keepAlive;
00415    char dateTemplate[6];
00416    char res[2];
00417    uint32_t secondaryKeepAlive;
00418    char res2[4];
00419 };
00420 
00421 #define START_TONE_MESSAGE 0x0082
00422 struct start_tone_message {
00423    uint32_t tone;
00424    uint32_t space;
00425    uint32_t instance;
00426    uint32_t reference;
00427 };
00428 
00429 #define STOP_TONE_MESSAGE 0x0083
00430 struct stop_tone_message {
00431    uint32_t instance;
00432    uint32_t reference;
00433    uint32_t space;
00434 };
00435 
00436 #define SET_RINGER_MESSAGE 0x0085
00437 struct set_ringer_message {
00438    uint32_t ringerMode;
00439    uint32_t unknown1; /* See notes in transmit_ringer_mode */
00440    uint32_t unknown2;
00441    uint32_t space[2];
00442 };
00443 
00444 #define SET_LAMP_MESSAGE 0x0086
00445 struct set_lamp_message {
00446    uint32_t stimulus;
00447    uint32_t stimulusInstance;
00448    uint32_t deviceStimulus;
00449 };
00450 
00451 #define SET_SPEAKER_MESSAGE 0x0088
00452 struct set_speaker_message {
00453    uint32_t mode;
00454 };
00455 
00456 /* XXX When do we need to use this? */
00457 #define SET_MICROPHONE_MESSAGE 0x0089
00458 struct set_microphone_message {
00459    uint32_t mode;
00460 };
00461 
00462 #define START_MEDIA_TRANSMISSION_MESSAGE 0x008A
00463 struct media_qualifier {
00464    uint32_t precedence;
00465    uint32_t vad;
00466    uint32_t packets;
00467    uint32_t bitRate;
00468 };
00469 
00470 struct start_media_transmission_message_ip4 {
00471    uint32_t conferenceId;
00472    uint32_t passThruPartyId;
00473    uint32_t remoteIp;
00474    uint32_t remotePort;
00475    uint32_t packetSize;
00476    uint32_t payloadType;
00477    struct media_qualifier qualifier;
00478    uint32_t space[19];
00479 };
00480 
00481 struct start_media_transmission_message_ip6 {
00482    uint32_t conferenceId;
00483    uint32_t passThruPartyId;
00484    uint32_t space;
00485    char remoteIp[16];
00486    uint32_t remotePort;
00487    uint32_t packetSize;
00488    uint32_t payloadType;
00489    struct media_qualifier qualifier;
00490    /*! \brief space2 is used for newer version of skinny */
00491    uint32_t space2[19];
00492 };
00493 
00494 #define STOP_MEDIA_TRANSMISSION_MESSAGE 0x008B
00495 struct stop_media_transmission_message {
00496    uint32_t conferenceId;
00497    uint32_t passThruPartyId;
00498    uint32_t space[3];
00499 };
00500 
00501 #define CALL_INFO_MESSAGE 0x008F
00502 struct call_info_message {
00503    char callingPartyName[40];
00504    char callingParty[24];
00505    char calledPartyName[40];
00506    char calledParty[24];
00507    uint32_t instance;
00508    uint32_t reference;
00509    uint32_t type;
00510    char originalCalledPartyName[40];
00511    char originalCalledParty[24];
00512    char lastRedirectingPartyName[40];
00513    char lastRedirectingParty[24];
00514    uint32_t originalCalledPartyRedirectReason;
00515    uint32_t lastRedirectingReason;
00516    char callingPartyVoiceMailbox[24];
00517    char calledPartyVoiceMailbox[24];
00518    char originalCalledPartyVoiceMailbox[24];
00519    char lastRedirectingVoiceMailbox[24];
00520    uint32_t space[3];
00521 };
00522 
00523 #define FORWARD_STAT_MESSAGE 0x0090
00524 struct forward_stat_message {
00525    uint32_t activeforward;
00526    uint32_t lineNumber;
00527    uint32_t fwdall;
00528    char fwdallnum[24];
00529    uint32_t fwdbusy;
00530    char fwdbusynum[24];
00531    uint32_t fwdnoanswer;
00532    char fwdnoanswernum[24];
00533 };
00534 
00535 #define SPEED_DIAL_STAT_RES_MESSAGE 0x0091
00536 struct speed_dial_stat_res_message {
00537    uint32_t speedDialNumber;
00538    char speedDialDirNumber[24];
00539    char speedDialDisplayName[40];
00540 };
00541 
00542 #define LINE_STAT_RES_MESSAGE 0x0092
00543 struct line_stat_res_message {
00544    uint32_t lineNumber;
00545    char lineDirNumber[24];
00546    char lineDisplayName[24];
00547    uint32_t space[15];
00548 };
00549 
00550 #define DEFINETIMEDATE_MESSAGE 0x0094
00551 struct definetimedate_message {
00552    uint32_t year; /* since 1900 */
00553    uint32_t month;
00554    uint32_t dayofweek; /* monday = 1 */
00555    uint32_t day;
00556    uint32_t hour;
00557    uint32_t minute;
00558    uint32_t seconds;
00559    uint32_t milliseconds;
00560    uint32_t timestamp;
00561 };
00562 
00563 #define BUTTON_TEMPLATE_RES_MESSAGE 0x0097
00564 struct button_definition {
00565    uint8_t instanceNumber;
00566    uint8_t buttonDefinition;
00567 };
00568 
00569 struct button_definition_template {
00570    uint8_t buttonDefinition;
00571    /* for now, anything between 0xB0 and 0xCF is custom */
00572    /*int custom;*/
00573 };
00574 
00575 #define STIMULUS_REDIAL 0x01
00576 #define STIMULUS_SPEEDDIAL 0x02
00577 #define STIMULUS_HOLD 0x03
00578 #define STIMULUS_TRANSFER 0x04
00579 #define STIMULUS_FORWARDALL 0x05
00580 #define STIMULUS_FORWARDBUSY 0x06
00581 #define STIMULUS_FORWARDNOANSWER 0x07
00582 #define STIMULUS_DISPLAY 0x08
00583 #define STIMULUS_LINE 0x09
00584 #define STIMULUS_VOICEMAIL 0x0F
00585 #define STIMULUS_AUTOANSWER 0x11
00586 #define STIMULUS_SERVICEURL 0x14
00587 #define STIMULUS_DND 0x3F
00588 #define STIMULUS_CONFERENCE 0x7D
00589 #define STIMULUS_CALLPARK 0x7E
00590 #define STIMULUS_CALLPICKUP 0x7F
00591 #define STIMULUS_NONE 0xFF
00592 
00593 /* Button types */
00594 #define BT_REDIAL STIMULUS_REDIAL
00595 #define BT_SPEEDDIAL STIMULUS_SPEEDDIAL
00596 #define BT_HOLD STIMULUS_HOLD
00597 #define BT_TRANSFER STIMULUS_TRANSFER
00598 #define BT_FORWARDALL STIMULUS_FORWARDALL
00599 #define BT_FORWARDBUSY STIMULUS_FORWARDBUSY
00600 #define BT_FORWARDNOANSWER STIMULUS_FORWARDNOANSWER
00601 #define BT_DISPLAY STIMULUS_DISPLAY
00602 #define BT_LINE STIMULUS_LINE
00603 #define BT_VOICEMAIL STIMULUS_VOICEMAIL
00604 #define BT_AUTOANSWER STIMULUS_AUTOANSWER
00605 #define BT_SERVICEURL STIMULUS_SERVICEURL
00606 #define BT_DND STIMULUS_DND
00607 #define BT_CONFERENCE STIMULUS_CONFERENCE
00608 #define BT_CALLPARK STIMULUS_CALLPARK
00609 #define BT_CALLPICKUP STIMULUS_CALLPICKUP
00610 #define BT_NONE 0x00
00611 
00612 /* Custom button types - add our own between 0xB0 and 0xCF.
00613    This may need to be revised in the future,
00614    if stimuluses are ever added in this range. */
00615 #define BT_CUST_LINESPEEDDIAL 0xB0 /* line or speeddial with/without hint */
00616 #define BT_CUST_LINE 0xB1          /* line or speeddial with hint only */
00617 
00618 struct button_template_res_message {
00619    uint32_t buttonOffset;
00620    uint32_t buttonCount;
00621    uint32_t totalButtonCount;
00622    struct button_definition definition[42];
00623 };
00624 
00625 #define VERSION_RES_MESSAGE 0x0098
00626 struct version_res_message {
00627    char version[16];
00628 };
00629 
00630 #define DISPLAYTEXT_MESSAGE 0x0099
00631 struct displaytext_message {
00632    char text[40];
00633 };
00634 
00635 #define CLEAR_NOTIFY_MESSAGE  0x0115
00636 #define CLEAR_DISPLAY_MESSAGE 0x009A
00637 struct clear_display_message {
00638    uint32_t space;
00639 };
00640 
00641 #define CAPABILITIES_REQ_MESSAGE 0x009B
00642 
00643 #define REGISTER_REJ_MESSAGE 0x009D
00644 struct register_rej_message {
00645    char errMsg[33];
00646 };
00647 
00648 #define SERVER_RES_MESSAGE 0x009E
00649 struct server_identifier {
00650    char serverName[48];
00651 };
00652 
00653 struct server_res_message {
00654    struct server_identifier server[5];
00655    uint32_t serverListenPort[5];
00656    uint32_t serverIpAddr[5];
00657 };
00658 
00659 #define RESET_MESSAGE 0x009F
00660 struct reset_message {
00661    uint32_t resetType;
00662 };
00663 
00664 #define KEEP_ALIVE_ACK_MESSAGE 0x0100
00665 
00666 #define OPEN_RECEIVE_CHANNEL_MESSAGE 0x0105
00667 struct open_receive_channel_message {
00668    uint32_t conferenceId;
00669    uint32_t partyId;
00670    uint32_t packets;
00671    uint32_t capability;
00672    uint32_t echo;
00673    uint32_t bitrate;
00674    uint32_t space[36];
00675 };
00676 
00677 #define CLOSE_RECEIVE_CHANNEL_MESSAGE 0x0106
00678 struct close_receive_channel_message {
00679    uint32_t conferenceId;
00680    uint32_t partyId;
00681    uint32_t space[2];
00682 };
00683 
00684 #define SOFT_KEY_TEMPLATE_RES_MESSAGE 0x0108
00685 struct soft_key_template_definition {
00686    char softKeyLabel[16];
00687    uint32_t softKeyEvent;
00688 };
00689 
00690 #define BKSP_REQ_MESSAGE 0x0119
00691 struct bksp_req_message {
00692    uint32_t instance;
00693    uint32_t callreference;
00694 };
00695 
00696 #define KEYDEF_ONHOOK 0
00697 #define KEYDEF_CONNECTED 1
00698 #define KEYDEF_ONHOLD 2
00699 #define KEYDEF_RINGIN 3
00700 #define KEYDEF_OFFHOOK 4
00701 #define KEYDEF_CONNWITHTRANS 5
00702 #define KEYDEF_DADFD 6 /* Digits After Dialing First Digit */
00703 #define KEYDEF_CONNWITHCONF 7
00704 #define KEYDEF_RINGOUT 8
00705 #define KEYDEF_OFFHOOKWITHFEAT 9
00706 #define KEYDEF_UNKNOWN 10
00707 #define KEYDEF_SLAHOLD 11
00708 #define KEYDEF_SLACONNECTEDNOTACTIVE 12
00709 #define KEYDEF_RINGOUTWITHTRANS 13
00710 
00711 #define SOFTKEY_NONE 0x00
00712 #define SOFTKEY_REDIAL 0x01
00713 #define SOFTKEY_NEWCALL 0x02
00714 #define SOFTKEY_HOLD 0x03
00715 #define SOFTKEY_TRNSFER 0x04
00716 #define SOFTKEY_CFWDALL 0x05
00717 #define SOFTKEY_CFWDBUSY 0x06
00718 #define SOFTKEY_CFWDNOANSWER 0x07
00719 #define SOFTKEY_BKSPC 0x08
00720 #define SOFTKEY_ENDCALL 0x09
00721 #define SOFTKEY_RESUME 0x0A
00722 #define SOFTKEY_ANSWER 0x0B
00723 #define SOFTKEY_INFO 0x0C
00724 #define SOFTKEY_CONFRN 0x0D
00725 #define SOFTKEY_PARK 0x0E
00726 #define SOFTKEY_JOIN 0x0F
00727 #define SOFTKEY_MEETME 0x10
00728 #define SOFTKEY_PICKUP 0x11
00729 #define SOFTKEY_GPICKUP 0x12
00730 #define SOFTKEY_DND 0x13
00731 #define SOFTKEY_IDIVERT 0x14
00732 #define SOFTKEY_FORCEDIAL 0x15
00733 
00734 #define KEYMASK_ALL            0xFFFFFFFF
00735 #define KEYMASK_NONE           (1 << 0)
00736 #define KEYMASK_REDIAL         (1 << 1)
00737 #define KEYMASK_NEWCALL        (1 << 2)
00738 #define KEYMASK_HOLD           (1 << 3)
00739 #define KEYMASK_TRNSFER        (1 << 4)
00740 #define KEYMASK_CFWDALL        (1 << 5)
00741 #define KEYMASK_CFWDBUSY       (1 << 6)
00742 #define KEYMASK_CFWDNOANSWER   (1 << 7)
00743 #define KEYMASK_BKSPC          (1 << 8)
00744 #define KEYMASK_ENDCALL        (1 << 9)
00745 #define KEYMASK_RESUME         (1 << 10)
00746 #define KEYMASK_ANSWER         (1 << 11)
00747 #define KEYMASK_INFO           (1 << 12)
00748 #define KEYMASK_CONFRN         (1 << 13)
00749 #define KEYMASK_PARK           (1 << 14)
00750 #define KEYMASK_JOIN           (1 << 15)
00751 #define KEYMASK_MEETME         (1 << 16)
00752 #define KEYMASK_PICKUP         (1 << 17)
00753 #define KEYMASK_GPICKUP        (1 << 18)
00754 #define KEYMASK_DND            (1 << 29)
00755 #define KEYMASK_IDIVERT        (1 << 20)
00756 #define KEYMASK_FORCEDIAL      (1 << 21)
00757 
00758 /* Localized message "codes" (in octal)
00759    Below is en_US (taken from a 7970) */
00760 
00761 /*                            "\200\000"        ??? */
00762 #define OCTAL_REDIAL          "\200\001"     /* Redial */
00763 #define OCTAL_NEWCALL         "\200\002"     /* New Call */
00764 #define OCTAL_HOLD            "\200\003"     /* Hold */
00765 #define OCTAL_TRANSFER        "\200\004"     /* Transfer */
00766 #define OCTAL_CFWDALL         "\200\005"     /* CFwdALL */
00767 #define OCTAL_CFWDBUSY        "\200\006"     /* CFwdBusy */
00768 #define OCTAL_CFWDNOAN        "\200\007"     /* CFwdNoAnswer */
00769 #define OCTAL_BKSPC           "\200\010"     /* << */
00770 #define OCTAL_ENDCALL         "\200\011"     /* EndCall */
00771 #define OCTAL_RESUME          "\200\012"     /* Resume */
00772 #define OCTAL_ANSWER          "\200\013"     /* Answer */
00773 #define OCTAL_INFO            "\200\014"     /* Info */
00774 #define OCTAL_CONFRN          "\200\015"     /* Confrn */
00775 #define OCTAL_PARK            "\200\016"     /* Park */
00776 #define OCTAL_JOIN            "\200\017"     /* Join */
00777 #define OCTAL_MEETME          "\200\020"     /* MeetMe */
00778 #define OCTAL_PICKUP          "\200\021"     /* PickUp */
00779 #define OCTAL_GPICKUP         "\200\022"     /* GPickUp */
00780 #define OCTAL_CUROPTS         "\200\023"     /* Your current options */
00781 #define OCTAL_OFFHOOK         "\200\024"     /* Off Hook */
00782 #define OCTAL_ONHOOK          "\200\025"     /* On Hook */
00783 #define OCTAL_RINGOUT         "\200\026"     /* Ring out */
00784 #define OCTAL_FROM            "\200\027"     /* From */
00785 #define OCTAL_CONNECTED       "\200\030"     /* Connected */
00786 #define OCTAL_BUSY            "\200\031"     /* Busy */
00787 #define OCTAL_LINEINUSE       "\200\032"     /* Line In Use */
00788 #define OCTAL_CALLWAITING     "\200\033"     /* Call Waiting */
00789 #define OCTAL_CALLXFER        "\200\034"     /* Call Transfer */
00790 #define OCTAL_CALLPARK        "\200\035"     /* Call Park */
00791 #define OCTAL_CALLPROCEED     "\200\036"     /* Call Proceed */
00792 #define OCTAL_INUSEREMOTE     "\200\037"     /* In Use Remote */
00793 #define OCTAL_ENTRNUM         "\200\040"     /* Enter number */
00794 #define OCTAL_PARKAT          "\200\041"     /* Call park At */
00795 #define OCTAL_PRIMONLY        "\200\042"     /* Primary Only */
00796 #define OCTAL_TMPFAIL         "\200\043"     /* Temp Fail */
00797 #define OCTAL_HAVEVMAIL       "\200\044"     /* You Have VoiceMail */
00798 #define OCTAL_FWDEDTO         "\200\045"     /* Forwarded to */
00799 #define OCTAL_CANTCOMPCNF     "\200\046"     /* Can Not Complete Conference */
00800 #define OCTAL_NOCONFBRDG      "\200\047"     /* No Conference Bridge */
00801 #define OCTAL_NOPRIMARYCTL    "\200\050"     /* Can Not Hold Primary Control */
00802 #define OCTAL_INVALCONFPART   "\200\051"     /* Invalid Conference Participant */
00803 #define OCTAL_INCONFALREADY   "\200\052"     /* In Conference Already */
00804 #define OCTAL_NOPARTINFO      "\200\053"     /* No Participant Info */
00805 #define OCTAL_MAXPARTEXCEED   "\200\054"     /* Exceed Maximum Parties */
00806 #define OCTAL_KEYNOTACTIVE    "\200\055"     /* Key Is Not Active */
00807 #define OCTAL_ERRNOLIC        "\200\056"     /* Error No License */
00808 #define OCTAL_ERRDBCFG        "\200\057"     /* Error DBConfig */
00809 #define OCTAL_ERRDB           "\200\060"     /* Error Database */
00810 #define OCTAL_ERRPASSLMT      "\200\061"     /* Error Pass Limit */
00811 #define OCTAL_ERRUNK          "\200\062"     /* Error Unknown */
00812 #define OCTAL_ERRMISMATCH     "\200\063"     /* Error Mismatch */
00813 #define OCTAL_CONFERENCE      "\200\064"     /* Conference */
00814 #define OCTAL_PARKNO          "\200\065"     /* Park Number */
00815 #define OCTAL_PRIVATE         "\200\066"     /* Private */
00816 #define OCTAL_INSUFBANDW      "\200\067"     /* Not Enough Bandwidth */
00817 #define OCTAL_UNKNUM          "\200\070"     /* Unknown Number */
00818 #define OCTAL_RMLSTC          "\200\071"     /* RmLstC */
00819 #define OCTAL_VOICEMAIL       "\200\072"     /* Voicemail */
00820 #define OCTAL_IMMDIV          "\200\073"     /* ImmDiv */
00821 #define OCTAL_INTRCPT         "\200\074"     /* Intrcpt */
00822 #define OCTAL_SETWTCH         "\200\075"     /* SetWtch */
00823 #define OCTAL_TRNSFVM         "\200\076"     /* TrnsfVM */
00824 #define OCTAL_DND             "\200\077"     /* DND */
00825 #define OCTAL_DIVALL          "\200\100"     /* DivAll */
00826 #define OCTAL_CALLBACK        "\200\101"     /* CallBack */
00827 #define OCTAL_NETCNGREROUT    "\200\102"     /* Network congestion,rerouting */
00828 #define OCTAL_BARGE           "\200\103"     /* Barge */
00829 #define OCTAL_BARGEFAIL       "\200\104"     /* Failed to setup Barge */
00830 #define OCTAL_BARGEEXIST      "\200\105"     /* Another Barge exists */
00831 #define OCTAL_INCOMPATDEV     "\200\106"     /* Incompatible device type */
00832 #define OCTAL_PARKNONUM       "\200\107"     /* No Park Number Available */
00833 #define OCTAL_PARKREVERSION   "\200\110"     /* CallPark Reversion */
00834 #define OCTAL_SRVNOTACTIVE    "\200\111"     /* Service is not Active */
00835 #define OCTAL_HITRAFFIC       "\200\112"     /* High Traffic Try Again Later */
00836 #define OCTAL_QRT             "\200\113"     /* QRT */
00837 #define OCTAL_MCID            "\200\114"     /* MCID */
00838 #define OCTAL_DIRTRFR         "\200\115"     /* DirTrfr */
00839 #define OCTAL_SELECT          "\200\116"     /* Select */
00840 #define OCTAL_CONFLIST        "\200\117"     /* ConfList */
00841 #define OCTAL_IDIVERT         "\200\120"     /* iDivert */
00842 #define OCTAL_CBARGE          "\200\121"     /* cBarge */
00843 #define OCTAL_CANTCOMPLXFER   "\200\122"     /* Can Not Complete Transfer */
00844 #define OCTAL_CANTJOINCALLS   "\200\123"     /* Can Not Join Calls */
00845 #define OCTAL_MCIDSUCCESS     "\200\124"     /* Mcid Successful */
00846 #define OCTAL_NUMNOTCFG       "\200\125"     /* Number Not Configured */
00847 #define OCTAL_SECERROR        "\200\126"     /* Security Error */
00848 #define OCTAL_VIDBANDWNA      "\200\127"     /* Video Bandwidth Unavailable */
00849 #define OCTAL_VIDMODE         "\200\130"     /* VidMode */
00850 #define OCTAL_CALLDURTIMEOUT  "\200\131"     /* Max Call Duration Timeout */
00851 #define OCTAL_HOLDDURTIMEOUT  "\200\132"     /* Max Hold Duration Timeout */
00852 #define OCTAL_OPICKUP         "\200\133"     /* OPickUp */
00853 /*                            "\200\134"        ??? */
00854 /*                            "\200\135"        ??? */
00855 /*                            "\200\136"        ??? */
00856 /*                            "\200\137"        ??? */
00857 /*                            "\200\140"        ??? */
00858 #define OCTAL_EXTXFERRESTRICT "\200\141"     /* External Transfer Restricted */
00859 /*                            "\200\142"        ??? */
00860 /*                            "\200\143"        ??? */
00861 /*                            "\200\144"        ??? */
00862 #define OCTAL_MACADD          "\200\145"     /* Mac Address */
00863 #define OCTAL_HOST            "\200\146"     /* Host Name */
00864 #define OCTAL_DOMAIN          "\200\147"     /* Domain Name */
00865 #define OCTAL_IPADD           "\200\150"     /* IP Address */
00866 #define OCTAL_SUBMASK         "\200\151"     /* Subnet Mask */
00867 #define OCTAL_TFTP1           "\200\152"     /* TFTP Server 1 */
00868 #define OCTAL_ROUTER1         "\200\153"     /* Default Router 1 */
00869 #define OCTAL_ROUTER2         "\200\154"     /* Default Router 2 */
00870 #define OCTAL_ROUTER3         "\200\155"     /* Default Router 3 */
00871 #define OCTAL_ROUTER4         "\200\156"     /* Default Router 4 */
00872 #define OCTAL_ROUTER5         "\200\157"     /* Default Router 5 */
00873 #define OCTAL_DNS1            "\200\160"     /* DNS Server 1 */
00874 #define OCTAL_DNS2            "\200\161"     /* DNS Server 2 */
00875 #define OCTAL_DNS3            "\200\162"     /* DNS Server 3 */
00876 #define OCTAL_DNS4            "\200\163"     /* DNS Server 4 */
00877 #define OCTAL_DNS5            "\200\164"     /* DNS Server 5 */
00878 #define OCTAL_VLANOPID        "\200\165"     /* Operational VLAN Id */
00879 #define OCTAL_VLANADID        "\200\166"     /* Admin. VLAN Id */
00880 #define OCTAL_CM1             "\200\167"     /* CallManager 1 */
00881 #define OCTAL_CM2             "\200\170"     /* CallManager 2 */
00882 #define OCTAL_CM3             "\200\171"     /* CallManager 3 */
00883 #define OCTAL_CM4             "\200\172"     /* CallManager 4 */
00884 #define OCTAL_CM5             "\200\173"     /* CallManager 5 */
00885 #define OCTAL_URLINFO         "\200\174"     /* Information URL */
00886 #define OCTAL_URLDIRS         "\200\175"     /* Directories URL */
00887 #define OCTAL_URLMSGS         "\200\176"     /* Messages URL */
00888 #define OCTAL_URLSRVS         "\200\177"     /* Services URL */
00889 
00890 static struct soft_key_template_definition soft_key_template_default[] = {
00891    { OCTAL_REDIAL, SOFTKEY_REDIAL },
00892    { OCTAL_NEWCALL, SOFTKEY_NEWCALL },
00893    { OCTAL_HOLD, SOFTKEY_HOLD },
00894    { OCTAL_TRANSFER, SOFTKEY_TRNSFER },
00895    { OCTAL_CFWDALL, SOFTKEY_CFWDALL },
00896    { OCTAL_CFWDBUSY, SOFTKEY_CFWDBUSY },
00897    { OCTAL_CFWDNOAN, SOFTKEY_CFWDNOANSWER },
00898    { OCTAL_BKSPC, SOFTKEY_BKSPC },
00899    { OCTAL_ENDCALL, SOFTKEY_ENDCALL },
00900    { OCTAL_RESUME, SOFTKEY_RESUME },
00901    { OCTAL_ANSWER, SOFTKEY_ANSWER },
00902    { OCTAL_INFO, SOFTKEY_INFO },
00903    { OCTAL_CONFRN, SOFTKEY_CONFRN },
00904    { OCTAL_PARK, SOFTKEY_PARK },
00905    { OCTAL_JOIN, SOFTKEY_JOIN },
00906    { OCTAL_MEETME, SOFTKEY_MEETME },
00907    { OCTAL_PICKUP, SOFTKEY_PICKUP },
00908    { OCTAL_GPICKUP, SOFTKEY_GPICKUP },
00909    { OCTAL_DND, SOFTKEY_DND },
00910    { OCTAL_IDIVERT, SOFTKEY_IDIVERT },
00911    { "Dial", SOFTKEY_FORCEDIAL},
00912 };
00913 
00914 struct soft_key_definitions {
00915    const uint8_t mode;
00916    const uint8_t *defaults;
00917    const int count;
00918 };
00919 
00920 static const uint8_t soft_key_default_onhook[] = {
00921    SOFTKEY_REDIAL,
00922    SOFTKEY_NEWCALL,
00923    SOFTKEY_CFWDALL,
00924    SOFTKEY_CFWDBUSY,
00925    SOFTKEY_CFWDNOANSWER,
00926    SOFTKEY_DND,
00927    SOFTKEY_GPICKUP,
00928    /*SOFTKEY_CONFRN,*/
00929 };
00930 
00931 static const uint8_t soft_key_default_connected[] = {
00932    SOFTKEY_HOLD,
00933    SOFTKEY_ENDCALL,
00934    SOFTKEY_TRNSFER,
00935    SOFTKEY_PARK,
00936    SOFTKEY_CFWDALL,
00937    SOFTKEY_CFWDBUSY,
00938    SOFTKEY_CFWDNOANSWER,
00939 };
00940 
00941 static const uint8_t soft_key_default_onhold[] = {
00942    SOFTKEY_RESUME,
00943    SOFTKEY_NEWCALL,
00944    SOFTKEY_ENDCALL,
00945    SOFTKEY_TRNSFER,
00946 };
00947 
00948 static const uint8_t soft_key_default_ringin[] = {
00949    SOFTKEY_ANSWER,
00950    SOFTKEY_ENDCALL,
00951    SOFTKEY_TRNSFER,
00952 };
00953 
00954 static const uint8_t soft_key_default_offhook[] = {
00955    SOFTKEY_REDIAL,
00956    SOFTKEY_ENDCALL,
00957    SOFTKEY_TRNSFER,
00958    SOFTKEY_CFWDALL,
00959    SOFTKEY_CFWDBUSY,
00960    SOFTKEY_CFWDNOANSWER,
00961    SOFTKEY_GPICKUP,
00962 };
00963 
00964 static const uint8_t soft_key_default_connwithtrans[] = {
00965    SOFTKEY_HOLD,
00966    SOFTKEY_ENDCALL,
00967    SOFTKEY_TRNSFER,
00968    SOFTKEY_PARK,
00969    SOFTKEY_CFWDALL,
00970    SOFTKEY_CFWDBUSY,
00971    SOFTKEY_CFWDNOANSWER,
00972 };
00973 
00974 static const uint8_t soft_key_default_dadfd[] = {
00975    SOFTKEY_BKSPC,
00976    SOFTKEY_ENDCALL,
00977    SOFTKEY_TRNSFER,
00978    SOFTKEY_FORCEDIAL,
00979 };
00980 
00981 static const uint8_t soft_key_default_connwithconf[] = {
00982    SOFTKEY_NONE,
00983 };
00984 
00985 static const uint8_t soft_key_default_ringout[] = {
00986    SOFTKEY_NONE,
00987    SOFTKEY_ENDCALL,
00988 };
00989 
00990 static const uint8_t soft_key_default_ringoutwithtransfer[] = {
00991    SOFTKEY_NONE,
00992    SOFTKEY_ENDCALL,
00993    SOFTKEY_TRNSFER,
00994 };
00995 
00996 static const uint8_t soft_key_default_offhookwithfeat[] = {
00997    SOFTKEY_REDIAL,
00998    SOFTKEY_ENDCALL,
00999    SOFTKEY_TRNSFER,
01000 };
01001 
01002 static const uint8_t soft_key_default_unknown[] = {
01003    SOFTKEY_NONE,
01004 };
01005 
01006 static const uint8_t soft_key_default_SLAhold[] = {
01007    SOFTKEY_REDIAL,
01008    SOFTKEY_NEWCALL,
01009    SOFTKEY_RESUME,
01010 };
01011 
01012 static const uint8_t soft_key_default_SLAconnectednotactive[] = {
01013    SOFTKEY_REDIAL,
01014    SOFTKEY_NEWCALL,
01015    SOFTKEY_JOIN,
01016 };
01017 
01018 static const struct soft_key_definitions soft_key_default_definitions[] = {
01019    {KEYDEF_ONHOOK, soft_key_default_onhook, sizeof(soft_key_default_onhook) / sizeof(uint8_t)},
01020    {KEYDEF_CONNECTED, soft_key_default_connected, sizeof(soft_key_default_connected) / sizeof(uint8_t)},
01021    {KEYDEF_ONHOLD, soft_key_default_onhold, sizeof(soft_key_default_onhold) / sizeof(uint8_t)},
01022    {KEYDEF_RINGIN, soft_key_default_ringin, sizeof(soft_key_default_ringin) / sizeof(uint8_t)},
01023    {KEYDEF_OFFHOOK, soft_key_default_offhook, sizeof(soft_key_default_offhook) / sizeof(uint8_t)},
01024    {KEYDEF_CONNWITHTRANS, soft_key_default_connwithtrans, sizeof(soft_key_default_connwithtrans) / sizeof(uint8_t)},
01025    {KEYDEF_DADFD, soft_key_default_dadfd, sizeof(soft_key_default_dadfd) / sizeof(uint8_t)},
01026    {KEYDEF_CONNWITHCONF, soft_key_default_connwithconf, sizeof(soft_key_default_connwithconf) / sizeof(uint8_t)},
01027    {KEYDEF_RINGOUT, soft_key_default_ringout, sizeof(soft_key_default_ringout) / sizeof(uint8_t)},
01028    {KEYDEF_RINGOUTWITHTRANS, soft_key_default_ringoutwithtransfer, sizeof(soft_key_default_ringoutwithtransfer) / sizeof(uint8_t)},
01029    {KEYDEF_OFFHOOKWITHFEAT, soft_key_default_offhookwithfeat, sizeof(soft_key_default_offhookwithfeat) / sizeof(uint8_t)},
01030    {KEYDEF_UNKNOWN, soft_key_default_unknown, sizeof(soft_key_default_unknown) / sizeof(uint8_t)},
01031    {KEYDEF_SLAHOLD, soft_key_default_SLAhold, sizeof(soft_key_default_SLAhold) / sizeof(uint8_t)},
01032    {KEYDEF_SLACONNECTEDNOTACTIVE, soft_key_default_SLAconnectednotactive, sizeof(soft_key_default_SLAconnectednotactive) / sizeof(uint8_t)}
01033 };
01034 
01035 struct soft_key_template_res_message {
01036    uint32_t softKeyOffset;
01037    uint32_t softKeyCount;
01038    uint32_t totalSoftKeyCount;
01039    struct soft_key_template_definition softKeyTemplateDefinition[32];
01040 };
01041 
01042 #define SOFT_KEY_SET_RES_MESSAGE 0x0109
01043 
01044 struct soft_key_set_definition {
01045    uint8_t softKeyTemplateIndex[16];
01046    uint16_t softKeyInfoIndex[16];
01047 };
01048 
01049 struct soft_key_set_res_message {
01050    uint32_t softKeySetOffset;
01051    uint32_t softKeySetCount;
01052    uint32_t totalSoftKeySetCount;
01053    struct soft_key_set_definition softKeySetDefinition[16];
01054    uint32_t res;
01055 };
01056 
01057 #define SELECT_SOFT_KEYS_MESSAGE 0x0110
01058 struct select_soft_keys_message {
01059    uint32_t instance;
01060    uint32_t reference;
01061    uint32_t softKeySetIndex;
01062    uint32_t validKeyMask;
01063 };
01064 
01065 #define CALL_STATE_MESSAGE 0x0111
01066 struct call_state_message {
01067    uint32_t callState;
01068    uint32_t lineInstance;
01069    uint32_t callReference;
01070    uint32_t space[3];
01071 };
01072 
01073 #define DISPLAY_PROMPT_STATUS_MESSAGE 0x0112
01074 struct display_prompt_status_message {
01075    uint32_t messageTimeout;
01076    char promptMessage[32];
01077    uint32_t lineInstance;
01078    uint32_t callReference;
01079    uint32_t space[3];
01080 };
01081 
01082 #define CLEAR_PROMPT_MESSAGE  0x0113
01083 struct clear_prompt_message {
01084    uint32_t lineInstance;
01085    uint32_t callReference;
01086 };
01087 
01088 #define DISPLAY_NOTIFY_MESSAGE 0x0114
01089 struct display_notify_message {
01090    uint32_t displayTimeout;
01091    char displayMessage[100];
01092 };
01093 
01094 #define ACTIVATE_CALL_PLANE_MESSAGE 0x0116
01095 struct activate_call_plane_message {
01096    uint32_t lineInstance;
01097 };
01098 
01099 #define DIALED_NUMBER_MESSAGE 0x011D
01100 struct dialed_number_message {
01101    char dialedNumber[24];
01102    uint32_t lineInstance;
01103    uint32_t callReference;
01104 };
01105 
01106 #define MAX_SERVICEURL 256
01107 #define SERVICEURL_STAT_MESSAGE 0x012F
01108 struct serviceurl_stat_message {
01109    uint32_t instance;
01110    char url[MAX_SERVICEURL];
01111    char displayName[40];
01112 };
01113 
01114 #define MAXCALLINFOSTR 256
01115 #define MAXDISPLAYNOTIFYSTR 32
01116 
01117 #define DISPLAY_PRINOTIFY_MESSAGE 0x0120
01118 struct display_prinotify_message {
01119    uint32_t timeout;
01120    uint32_t priority;
01121    char text[MAXDISPLAYNOTIFYSTR];
01122 };
01123 
01124 #define CLEAR_PRINOTIFY_MESSAGE 0x0121
01125 struct clear_prinotify_message {
01126    uint32_t priority;
01127 };
01128 
01129 #define CALL_INFO_MESSAGE_VARIABLE 0x014A
01130 struct call_info_message_variable {
01131    uint32_t instance;
01132    uint32_t callreference;
01133    uint32_t calldirection;
01134    uint32_t unknown1;
01135    uint32_t unknown2;
01136    uint32_t unknown3;
01137    uint32_t unknown4;
01138    uint32_t unknown5;
01139    char calldetails[MAXCALLINFOSTR];
01140 };
01141 
01142 #define DISPLAY_PRINOTIFY_MESSAGE_VARIABLE 0x0144
01143 struct display_prinotify_message_variable {
01144    uint32_t timeout;
01145    uint32_t priority;
01146    char text[MAXDISPLAYNOTIFYSTR];
01147 };
01148 
01149 #define DISPLAY_PROMPT_STATUS_MESSAGE_VARIABLE 0x0145
01150 struct display_prompt_status_message_variable {
01151    uint32_t unknown;
01152    uint32_t lineInstance;
01153    uint32_t callReference;
01154    char promptMessage[MAXCALLINFOSTR];
01155 };
01156 
01157 union skinny_data {
01158    struct alarm_message alarm;
01159    struct speed_dial_stat_req_message speeddialreq;
01160    struct register_message reg;
01161    struct register_ack_message regack;
01162    struct register_rej_message regrej;
01163    struct capabilities_res_message caps;
01164    struct version_res_message version;
01165    struct button_template_res_message buttontemplate;
01166    struct displaytext_message displaytext;
01167    struct clear_display_message cleardisplay;
01168    struct display_prompt_status_message displaypromptstatus;
01169    struct clear_prompt_message clearpromptstatus;
01170    struct definetimedate_message definetimedate;
01171    struct start_tone_message starttone;
01172    struct stop_tone_message stoptone;
01173    struct speed_dial_stat_res_message speeddial;
01174    struct line_state_req_message line;
01175    struct line_stat_res_message linestat;
01176    struct soft_key_set_res_message softkeysets;
01177    struct soft_key_template_res_message softkeytemplate;
01178    struct server_res_message serverres;
01179    struct reset_message reset;
01180    struct set_lamp_message setlamp;
01181    struct set_ringer_message setringer;
01182    struct call_state_message callstate;
01183    struct keypad_button_message keypad;
01184    struct select_soft_keys_message selectsoftkey;
01185    struct activate_call_plane_message activatecallplane;
01186    struct stimulus_message stimulus;
01187    struct offhook_message offhook;
01188    struct onhook_message onhook;
01189    struct set_speaker_message setspeaker;
01190    struct set_microphone_message setmicrophone;
01191    struct call_info_message callinfo;
01192    struct start_media_transmission_message_ip4 startmedia_ip4;
01193    struct start_media_transmission_message_ip6 startmedia_ip6;
01194    struct stop_media_transmission_message stopmedia;
01195    struct open_receive_channel_message openreceivechannel;
01196    struct open_receive_channel_ack_message_ip4 openreceivechannelack_ip4;
01197    struct open_receive_channel_ack_message_ip6 openreceivechannelack_ip6;
01198    struct close_receive_channel_message closereceivechannel;
01199    struct display_notify_message displaynotify;
01200    struct dialed_number_message dialednumber;
01201    struct soft_key_event_message softkeyeventmessage;
01202    struct enbloc_call_message enbloccallmessage;
01203    struct forward_stat_message forwardstat;
01204    struct bksp_req_message bkspmessage;
01205    struct call_info_message_variable callinfomessagevariable;
01206    struct display_prompt_status_message_variable displaypromptstatusvar;
01207    struct serviceurl_stat_message serviceurlmessage;
01208    struct clear_prinotify_message clearprinotify;
01209    struct display_prinotify_message displayprinotify;
01210    struct display_prinotify_message_variable displayprinotifyvar;
01211 };
01212 
01213 /* packet composition */
01214 struct skinny_req {
01215    uint32_t len;
01216    uint32_t res;
01217    uint32_t e;
01218    union skinny_data data;
01219 };
01220 
01221 /* XXX This is the combined size of the variables above.  (len, res, e)
01222    If more are added, this MUST change.
01223    (sizeof(skinny_req) - sizeof(skinny_data)) DOES NOT WORK on all systems (amd64?). */
01224 static int skinny_header_size = 12;
01225 
01226 /*****************************
01227  * Asterisk specific globals *
01228  *****************************/
01229 
01230 static int skinnyreload = 0;
01231 
01232 /* a hostname, portnumber, socket and such is usefull for VoIP protocols */
01233 static struct sockaddr_in bindaddr;
01234 static char ourhost[256];
01235 static int ourport;
01236 static struct in_addr __ourip;
01237 static struct ast_hostent ahp;
01238 static struct hostent *hp;
01239 static int skinnysock = -1;
01240 static pthread_t accept_t;
01241 static int callnums = 1;
01242 
01243 #define SKINNY_DEVICE_UNKNOWN -1
01244 #define SKINNY_DEVICE_NONE 0
01245 #define SKINNY_DEVICE_30SPPLUS 1
01246 #define SKINNY_DEVICE_12SPPLUS 2
01247 #define SKINNY_DEVICE_12SP 3
01248 #define SKINNY_DEVICE_12 4
01249 #define SKINNY_DEVICE_30VIP 5
01250 #define SKINNY_DEVICE_7910 6
01251 #define SKINNY_DEVICE_7960 7
01252 #define SKINNY_DEVICE_7940 8
01253 #define SKINNY_DEVICE_7935 9
01254 #define SKINNY_DEVICE_ATA186 12 /* Cisco ATA-186 */
01255 #define SKINNY_DEVICE_7941 115
01256 #define SKINNY_DEVICE_7971 119
01257 #define SKINNY_DEVICE_7914 124 /* Expansion module */
01258 #define SKINNY_DEVICE_7985 302
01259 #define SKINNY_DEVICE_7911 307
01260 #define SKINNY_DEVICE_7961GE 308
01261 #define SKINNY_DEVICE_7941GE 309
01262 #define SKINNY_DEVICE_7931 348
01263 #define SKINNY_DEVICE_7921 365
01264 #define SKINNY_DEVICE_7906 369
01265 #define SKINNY_DEVICE_7962 404 /* Not found */
01266 #define SKINNY_DEVICE_7937 431
01267 #define SKINNY_DEVICE_7942 434
01268 #define SKINNY_DEVICE_7945 435
01269 #define SKINNY_DEVICE_7965 436
01270 #define SKINNY_DEVICE_7975 437
01271 #define SKINNY_DEVICE_7905 20000
01272 #define SKINNY_DEVICE_7920 30002
01273 #define SKINNY_DEVICE_7970 30006
01274 #define SKINNY_DEVICE_7912 30007
01275 #define SKINNY_DEVICE_7902 30008
01276 #define SKINNY_DEVICE_CIPC 30016 /* Cisco IP Communicator */
01277 #define SKINNY_DEVICE_7961 30018
01278 #define SKINNY_DEVICE_7936 30019
01279 #define SKINNY_DEVICE_SCCPGATEWAY_AN 30027 /* Analog gateway */
01280 #define SKINNY_DEVICE_SCCPGATEWAY_BRI 30028 /* BRI gateway */
01281 
01282 #define SKINNY_SPEAKERON 1
01283 #define SKINNY_SPEAKEROFF 2
01284 
01285 #define SKINNY_MICON 1
01286 #define SKINNY_MICOFF 2
01287 
01288 #define SKINNY_OFFHOOK 1
01289 #define SKINNY_ONHOOK 2
01290 #define SKINNY_RINGOUT 3
01291 #define SKINNY_RINGIN 4
01292 #define SKINNY_CONNECTED 5
01293 #define SKINNY_BUSY 6
01294 #define SKINNY_CONGESTION 7
01295 #define SKINNY_HOLD 8
01296 #define SKINNY_CALLWAIT 9
01297 #define SKINNY_TRANSFER 10
01298 #define SKINNY_PARK 11
01299 #define SKINNY_PROGRESS 12
01300 #define SKINNY_CALLREMOTEMULTILINE 13
01301 #define SKINNY_INVALID 14
01302 
01303 #define SKINNY_INCOMING 1
01304 #define SKINNY_OUTGOING 2
01305 
01306 #define SKINNY_SILENCE 0x00      /* Note sure this is part of the protocol, remove? */
01307 #define SKINNY_DIALTONE 0x21
01308 #define SKINNY_BUSYTONE 0x23
01309 #define SKINNY_ALERT 0x24
01310 #define SKINNY_REORDER 0x25
01311 #define SKINNY_CALLWAITTONE 0x2D
01312 #define SKINNY_ZIPZIP 0x31
01313 #define SKINNY_ZIP 0x32
01314 #define SKINNY_BEEPBONK 0x33
01315 #define SKINNY_BARGIN 0x43
01316 #define SKINNY_NOTONE 0x7F
01317 
01318 #define SKINNY_LAMP_OFF 1
01319 #define SKINNY_LAMP_ON 2
01320 #define SKINNY_LAMP_WINK 3
01321 #define SKINNY_LAMP_FLASH 4
01322 #define SKINNY_LAMP_BLINK 5
01323 
01324 #define SKINNY_RING_OFF 1
01325 #define SKINNY_RING_INSIDE 2
01326 #define SKINNY_RING_OUTSIDE 3
01327 #define SKINNY_RING_FEATURE 4
01328 
01329 #define SKINNY_CFWD_ALL       (1 << 0)
01330 #define SKINNY_CFWD_BUSY      (1 << 1)
01331 #define SKINNY_CFWD_NOANSWER  (1 << 2)
01332 
01333 /* Skinny rtp stream modes. Do we really need this? */
01334 #define SKINNY_CX_SENDONLY 0
01335 #define SKINNY_CX_RECVONLY 1
01336 #define SKINNY_CX_SENDRECV 2
01337 #define SKINNY_CX_CONF 3
01338 #define SKINNY_CX_CONFERENCE 3
01339 #define SKINNY_CX_MUTE 4
01340 #define SKINNY_CX_INACTIVE 4
01341 
01342 #if 0
01343 static const char * const skinny_cxmodes[] = {
01344    "sendonly",
01345    "recvonly",
01346    "sendrecv",
01347    "confrnce",
01348    "inactive"
01349 };
01350 #endif
01351 
01352 /* driver scheduler */
01353 static struct ast_sched_context *sched = NULL;
01354 
01355 /* Protect the network socket */
01356 AST_MUTEX_DEFINE_STATIC(netlock);
01357 
01358 /* Wait up to 16 seconds for first digit */
01359 static int firstdigittimeout = 16000;
01360 
01361 /* How long to wait for following digits */
01362 static int gendigittimeout = 8000;
01363 
01364 /* How long to wait for an extra digit, if there is an ambiguous match */
01365 static int matchdigittimeout = 3000;
01366 
01367 #define SUBSTATE_UNSET 0
01368 #define SUBSTATE_OFFHOOK 1
01369 #define SUBSTATE_ONHOOK 2
01370 #define SUBSTATE_RINGOUT 3
01371 #define SUBSTATE_RINGIN 4
01372 #define SUBSTATE_CONNECTED 5
01373 #define SUBSTATE_BUSY 6
01374 #define SUBSTATE_CONGESTION 7
01375 #define SUBSTATE_HOLD 8
01376 #define SUBSTATE_CALLWAIT 9
01377 #define SUBSTATE_PROGRESS 12
01378 #define SUBSTATE_DIALING 101
01379 
01380 #define DIALTYPE_NORMAL      1<<0
01381 #define DIALTYPE_CFWD        1<<1
01382 #define DIALTYPE_XFER        1<<2
01383 
01384 struct skinny_subchannel {
01385    struct ast_channel *owner;
01386    struct ast_rtp_instance *rtp;
01387    struct ast_rtp_instance *vrtp;
01388    unsigned int callid;
01389    char exten[AST_MAX_EXTENSION];
01390    /* time_t lastouttime; */ /* Unused */
01391    int progress;
01392    int ringing;
01393    /* int lastout; */ /* Unused */
01394    int cxmode;
01395    int nat;
01396    int calldirection;
01397    int blindxfer;
01398    int xferor;
01399    int substate;
01400    int aa_sched;
01401    int aa_beep;
01402    int aa_mute;
01403    int dialer_sched;
01404    int cfwd_sched;
01405    int dialType;
01406    int getforward;
01407    char *origtonum;
01408    char *origtoname;
01409 
01410    AST_LIST_ENTRY(skinny_subchannel) list;
01411    struct skinny_subchannel *related;
01412    struct skinny_line *line;
01413    struct skinny_subline *subline;
01414 };
01415 
01416 #define SKINNY_LINE_OPTIONS            \
01417    char name[80];             \
01418    char label[24];               \
01419    char accountcode[AST_MAX_ACCOUNT_CODE];      \
01420    char exten[AST_MAX_EXTENSION];         \
01421    char context[AST_MAX_CONTEXT];         \
01422    char language[MAX_LANGUAGE];        \
01423    char cid_num[AST_MAX_EXTENSION];    \
01424    char cid_name[AST_MAX_EXTENSION];      \
01425    char lastcallerid[AST_MAX_EXTENSION];     \
01426    int cfwdtype;              \
01427    char call_forward_all[AST_MAX_EXTENSION]; \
01428    char call_forward_busy[AST_MAX_EXTENSION];   \
01429    char call_forward_noanswer[AST_MAX_EXTENSION];  \
01430    char mailbox[AST_MAX_MAILBOX_UNIQUEID];      \
01431    char vmexten[AST_MAX_EXTENSION];    \
01432    char regexten[AST_MAX_EXTENSION];      \
01433    char regcontext[AST_MAX_CONTEXT];      \
01434    char parkinglot[AST_MAX_CONTEXT];      \
01435    char mohinterpret[MAX_MUSICCLASS];     \
01436    char mohsuggest[MAX_MUSICCLASS];    \
01437    char lastnumberdialed[AST_MAX_EXTENSION]; \
01438    char dialoutexten[AST_MAX_EXTENSION];     \
01439    char dialoutcontext[AST_MAX_CONTEXT];     \
01440    ast_group_t callgroup;           \
01441    ast_group_t pickupgroup;         \
01442    struct ast_namedgroups *named_callgroups; \
01443    struct ast_namedgroups *named_pickupgroups;  \
01444    int callwaiting;           \
01445    int transfer;              \
01446    int threewaycalling;          \
01447    int mwiblink;              \
01448    int cancallforward;           \
01449    int callfwdtimeout;           \
01450    int dnd;             \
01451    int hidecallerid;          \
01452    int amaflags;              \
01453    int instance;              \
01454    int group;              \
01455    int nonCodecCapability;          \
01456    int immediate;             \
01457    int nat;             \
01458    int directmedia;           \
01459    int prune;
01460 
01461 struct skinny_line {
01462    SKINNY_LINE_OPTIONS
01463    ast_mutex_t lock;
01464    struct skinny_container *container;
01465    struct stasis_subscription *mwi_event_sub; /* Event based MWI */
01466    struct skinny_subchannel *activesub;
01467    AST_LIST_HEAD(, skinny_subchannel) sub;
01468    AST_LIST_HEAD(, skinny_subline) sublines;
01469    AST_LIST_ENTRY(skinny_line) list;
01470    AST_LIST_ENTRY(skinny_line) all;
01471    struct skinny_device *device;
01472    struct ast_format_cap *cap;
01473    struct ast_format_cap *confcap;
01474    struct ast_variable *chanvars; /*!< Channel variables to set for inbound call */
01475    int newmsgs;
01476 };
01477 
01478 static struct skinny_line_options{
01479    SKINNY_LINE_OPTIONS
01480 } default_line_struct = {
01481    .callwaiting = 1,
01482    .transfer = 1,
01483    .mwiblink = 0,
01484    .dnd = 0,
01485    .hidecallerid = 0,
01486    .amaflags = 0,
01487    .instance = 0,
01488    .directmedia = 0,
01489    .nat = 0,
01490    .callfwdtimeout = 20000,
01491    .prune = 0,
01492 };
01493 static struct skinny_line_options *default_line = &default_line_struct;
01494 
01495 static AST_LIST_HEAD_STATIC(lines, skinny_line);
01496 
01497 struct skinny_subline {
01498    struct skinny_container *container;
01499    struct skinny_line *line;
01500    struct skinny_subchannel *sub;
01501    AST_LIST_ENTRY(skinny_subline) list;
01502    char name[80];
01503    char context[AST_MAX_CONTEXT];
01504    char exten[AST_MAX_EXTENSION];
01505    char stname[AST_MAX_EXTENSION];
01506    char lnname[AST_MAX_EXTENSION];
01507    char ourName[40];
01508    char ourNum[24];
01509    char theirName[40];
01510    char theirNum[24];
01511    int calldirection;
01512    int substate;
01513    int extenstate;
01514    unsigned int callid;
01515 };
01516 
01517 struct skinny_speeddial {
01518    ast_mutex_t lock;
01519    struct skinny_container *container;
01520    char label[42];
01521    char context[AST_MAX_CONTEXT];
01522    char exten[AST_MAX_EXTENSION];
01523    int instance;
01524    int stateid;
01525    int laststate;
01526    int isHint;
01527 
01528    AST_LIST_ENTRY(skinny_speeddial) list;
01529    struct skinny_device *parent;
01530 };
01531 
01532 struct skinny_serviceurl {
01533    int instance;
01534    char url[MAX_SERVICEURL];
01535    char displayName[40];
01536    AST_LIST_ENTRY(skinny_serviceurl) list;
01537    struct skinny_device *device;
01538 };
01539 
01540 #define SKINNY_DEVICECONTAINER 1
01541 #define SKINNY_LINECONTAINER 2
01542 #define SKINNY_SUBLINECONTAINER 3
01543 #define SKINNY_SDCONTAINER 4
01544 
01545 struct skinny_container {
01546    int type;
01547    void *data;
01548 };
01549 
01550 struct skinny_addon {
01551    ast_mutex_t lock;
01552    char type[10];
01553    AST_LIST_ENTRY(skinny_addon) list;
01554    struct skinny_device *parent;
01555 };
01556 
01557 #define SKINNY_DEVICE_OPTIONS             \
01558    char name[80];                \
01559    char id[16];                  \
01560    char version_id[16];             \
01561    char vmexten[AST_MAX_EXTENSION];       \
01562    int type;                  \
01563    int protocolversion;          \
01564    int hookstate;             \
01565    int lastlineinstance;               \
01566    int lastcallreference;              \
01567    int earlyrtp;                 \
01568    int transfer;                 \
01569    int callwaiting;              \
01570    int mwiblink;                 \
01571    int dnd;                \
01572    int prune;
01573 
01574 struct skinny_device {
01575    SKINNY_DEVICE_OPTIONS
01576    struct type *first;
01577    struct type *last;
01578    ast_mutex_t lock;
01579    struct sockaddr_in addr;
01580    struct in_addr ourip;
01581    struct ast_ha *ha;
01582    struct skinnysession *session;
01583    struct skinny_line *activeline;
01584    struct ast_format_cap *cap;
01585    struct ast_format_cap *confcap;
01586    struct ast_endpoint *endpoint;
01587    AST_LIST_HEAD(, skinny_line) lines;
01588    AST_LIST_HEAD(, skinny_speeddial) speeddials;
01589    AST_LIST_HEAD(, skinny_serviceurl) serviceurls;
01590    AST_LIST_HEAD(, skinny_addon) addons;
01591    AST_LIST_ENTRY(skinny_device) list;
01592 };
01593 
01594 static struct skinny_device_options {
01595    SKINNY_DEVICE_OPTIONS
01596 } default_device_struct = {
01597    .transfer = 1,
01598    .earlyrtp = 1,
01599    .callwaiting = 1,
01600    .mwiblink = 0,
01601    .dnd = 0,
01602    .prune = 0,
01603    .hookstate = SKINNY_ONHOOK,
01604 };
01605 static struct skinny_device_options *default_device = &default_device_struct;
01606 
01607 static AST_LIST_HEAD_STATIC(devices, skinny_device);
01608 
01609 struct skinnysession {
01610    pthread_t t;
01611    ast_mutex_t lock;
01612    struct timeval start;
01613    struct sockaddr_in sin;
01614    int fd;
01615    char outbuf[SKINNY_MAX_PACKET];
01616    struct skinny_device *device;
01617    AST_LIST_ENTRY(skinnysession) list;
01618    int lockstate; /* Only for use in the skinny_session thread */
01619    int auth_timeout_sched;
01620    int keepalive_timeout_sched;
01621    struct timeval last_keepalive;
01622    int keepalive_count;
01623 };
01624 
01625 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);
01626 static AST_LIST_HEAD_STATIC(sessions, skinnysession);
01627 
01628 static int skinny_devicestate(const char *data);
01629 static int skinny_call(struct ast_channel *ast, const char *dest, int timeout);
01630 static int skinny_hangup(struct ast_channel *ast);
01631 static int skinny_answer(struct ast_channel *ast);
01632 static struct ast_frame *skinny_read(struct ast_channel *ast);
01633 static int skinny_write(struct ast_channel *ast, struct ast_frame *frame);
01634 static int skinny_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen);
01635 static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
01636 static int skinny_senddigit_begin(struct ast_channel *ast, char digit);
01637 static int skinny_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
01638 static void mwi_event_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg);
01639 static int skinny_dialer_cb(const void *data);
01640 static int skinny_reload(void);
01641 
01642 static void skinny_set_owner(struct skinny_subchannel* sub, struct ast_channel* chan);
01643 static void setsubstate(struct skinny_subchannel *sub, int state);
01644 static void dumpsub(struct skinny_subchannel *sub, int forcehangup);
01645 static void activatesub(struct skinny_subchannel *sub, int state);
01646 static void dialandactivatesub(struct skinny_subchannel *sub, char exten[AST_MAX_EXTENSION]);
01647 static int skinny_nokeepalive_cb(const void *data);
01648 
01649 static void transmit_definetimedate(struct skinny_device *d);
01650 
01651 static struct ast_channel_tech skinny_tech = {
01652    .type = "Skinny",
01653    .description = tdesc,
01654    .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
01655    .requester = skinny_request,
01656    .devicestate = skinny_devicestate,
01657    .call = skinny_call,
01658    .hangup = skinny_hangup,
01659    .answer = skinny_answer,
01660    .read = skinny_read,
01661    .write = skinny_write,
01662    .indicate = skinny_indicate,
01663    .fixup = skinny_fixup,
01664    .send_digit_begin = skinny_senddigit_begin,
01665    .send_digit_end = skinny_senddigit_end,
01666 };
01667 
01668 static int skinny_extensionstate_cb(char *context, char *id, struct ast_state_cb_info *info, void *data);
01669 
01670 static struct skinny_line *skinny_line_alloc(void)
01671 {
01672    struct skinny_line *l;
01673    if (!(l = ast_calloc(1, sizeof(*l)))) {
01674       return NULL;
01675    }
01676 
01677    l->cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
01678    l->confcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
01679    if (!l->cap || !l->confcap) {
01680       ao2_cleanup(l->cap);
01681       ao2_cleanup(l->confcap);
01682       ast_free(l);
01683       return NULL;
01684    }
01685    return l;
01686 }
01687 static struct skinny_line *skinny_line_destroy(struct skinny_line *l)
01688 {
01689    ao2_ref(l->cap, -1);
01690    ao2_ref(l->confcap, -1);
01691    l->named_callgroups = ast_unref_namedgroups(l->named_callgroups);
01692    l->named_pickupgroups = ast_unref_namedgroups(l->named_pickupgroups);
01693    ast_free(l->container);
01694    ast_free(l);
01695    return NULL;
01696 }
01697 static struct skinny_device *skinny_device_alloc(const char *dname)
01698 {
01699    struct skinny_device *d;
01700    if (!(d = ast_calloc(1, sizeof(*d)))) {
01701       return NULL;
01702    }
01703 
01704    d->cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
01705    d->confcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
01706    d->endpoint = ast_endpoint_create("Skinny", dname);
01707    if (!d->cap || !d->confcap || !d->endpoint) {
01708       ao2_cleanup(d->cap);
01709       ao2_cleanup(d->confcap);
01710       ast_free(d);
01711       return NULL;
01712    }
01713 
01714    ast_copy_string(d->name, dname, sizeof(d->name));
01715 
01716    return d;
01717 }
01718 static struct skinny_device *skinny_device_destroy(struct skinny_device *d)
01719 {
01720    ao2_ref(d->cap, -1);
01721    ao2_ref(d->confcap, -1);
01722    ast_endpoint_shutdown(d->endpoint);
01723    ast_free(d);
01724    return NULL;
01725 }
01726 
01727 static void *get_button_template(struct skinnysession *s, struct button_definition_template *btn)
01728 {
01729    struct skinny_device *d = s->device;
01730    struct skinny_addon *a;
01731    int i;
01732 
01733    switch (d->type) {
01734       case SKINNY_DEVICE_30SPPLUS:
01735       case SKINNY_DEVICE_30VIP:
01736          /* 13 rows, 2 columns */
01737          for (i = 0; i < 4; i++)
01738             (btn++)->buttonDefinition = BT_CUST_LINE;
01739          (btn++)->buttonDefinition = BT_REDIAL;
01740          (btn++)->buttonDefinition = BT_VOICEMAIL;
01741          (btn++)->buttonDefinition = BT_CALLPARK;
01742          (btn++)->buttonDefinition = BT_FORWARDALL;
01743          (btn++)->buttonDefinition = BT_CONFERENCE;
01744          for (i = 0; i < 4; i++)
01745             (btn++)->buttonDefinition = BT_NONE;
01746          for (i = 0; i < 13; i++)
01747             (btn++)->buttonDefinition = BT_SPEEDDIAL;
01748 
01749          break;
01750       case SKINNY_DEVICE_12SPPLUS:
01751       case SKINNY_DEVICE_12SP:
01752       case SKINNY_DEVICE_12:
01753          /* 6 rows, 2 columns */
01754          for (i = 0; i < 2; i++)
01755             (btn++)->buttonDefinition = BT_CUST_LINE;
01756          for (i = 0; i < 4; i++)
01757             (btn++)->buttonDefinition = BT_SPEEDDIAL;
01758          (btn++)->buttonDefinition = BT_HOLD;
01759          (btn++)->buttonDefinition = BT_REDIAL;
01760          (btn++)->buttonDefinition = BT_TRANSFER;
01761          (btn++)->buttonDefinition = BT_FORWARDALL;
01762          (btn++)->buttonDefinition = BT_CALLPARK;
01763          (btn++)->buttonDefinition = BT_VOICEMAIL;
01764          break;
01765       case SKINNY_DEVICE_7910:
01766          (btn++)->buttonDefinition = BT_LINE;
01767          (btn++)->buttonDefinition = BT_HOLD;
01768          (btn++)->buttonDefinition = BT_TRANSFER;
01769          (btn++)->buttonDefinition = BT_DISPLAY;
01770          (btn++)->buttonDefinition = BT_VOICEMAIL;
01771          (btn++)->buttonDefinition = BT_CONFERENCE;
01772          (btn++)->buttonDefinition = BT_FORWARDALL;
01773          for (i = 0; i < 2; i++)
01774             (btn++)->buttonDefinition = BT_SPEEDDIAL;
01775          (btn++)->buttonDefinition = BT_REDIAL;
01776          break;
01777       case SKINNY_DEVICE_7960:
01778       case SKINNY_DEVICE_7961:
01779       case SKINNY_DEVICE_7961GE:
01780       case SKINNY_DEVICE_7962:
01781       case SKINNY_DEVICE_7965:
01782          for (i = 0; i < 6; i++)
01783             (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
01784          break;
01785       case SKINNY_DEVICE_7940:
01786       case SKINNY_DEVICE_7941:
01787       case SKINNY_DEVICE_7941GE:
01788       case SKINNY_DEVICE_7942:
01789       case SKINNY_DEVICE_7945:
01790          for (i = 0; i < 2; i++)
01791             (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
01792          break;
01793       case SKINNY_DEVICE_7935:
01794       case SKINNY_DEVICE_7936:
01795          for (i = 0; i < 2; i++)
01796             (btn++)->buttonDefinition = BT_LINE;
01797          break;
01798       case SKINNY_DEVICE_ATA186:
01799          (btn++)->buttonDefinition = BT_LINE;
01800          break;
01801       case SKINNY_DEVICE_7970:
01802       case SKINNY_DEVICE_7971:
01803       case SKINNY_DEVICE_7975:
01804       case SKINNY_DEVICE_CIPC:
01805          for (i = 0; i < 8; i++)
01806             (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
01807          break;
01808       case SKINNY_DEVICE_7985:
01809          /* XXX I have no idea what the buttons look like on these. */
01810          ast_log(LOG_WARNING, "Unsupported device type '%d (7985)' found.\n", d->type);
01811          break;
01812       case SKINNY_DEVICE_7912:
01813       case SKINNY_DEVICE_7911:
01814       case SKINNY_DEVICE_7905:
01815          (btn++)->buttonDefinition = BT_LINE;
01816          (btn++)->buttonDefinition = BT_HOLD;
01817          break;
01818       case SKINNY_DEVICE_7920:
01819          /* XXX I don't know if this is right. */
01820          for (i = 0; i < 4; i++)
01821             (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
01822          break;
01823       case SKINNY_DEVICE_7921:
01824          for (i = 0; i < 6; i++)
01825             (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
01826          break;
01827       case SKINNY_DEVICE_7902:
01828          ast_log(LOG_WARNING, "Unsupported device type '%d (7902)' found.\n", d->type);
01829          break;
01830       case SKINNY_DEVICE_7906:
01831          ast_log(LOG_WARNING, "Unsupported device type '%d (7906)' found.\n", d->type);
01832          break;
01833       case SKINNY_DEVICE_7931:
01834          ast_log(LOG_WARNING, "Unsupported device type '%d (7931)' found.\n", d->type);
01835          break;
01836       case SKINNY_DEVICE_7937:
01837          ast_log(LOG_WARNING, "Unsupported device type '%d (7937)' found.\n", d->type);
01838          break;
01839       case SKINNY_DEVICE_7914:
01840          ast_log(LOG_WARNING, "Unsupported device type '%d (7914)' found.  Expansion module registered by itself?\n", d->type);
01841          break;
01842       case SKINNY_DEVICE_SCCPGATEWAY_AN:
01843       case SKINNY_DEVICE_SCCPGATEWAY_BRI:
01844          ast_log(LOG_WARNING, "Unsupported device type '%d (SCCP gateway)' found.\n", d->type);
01845          break;
01846       default:
01847          ast_log(LOG_WARNING, "Unknown device type '%d' found.\n", d->type);
01848          break;
01849    }
01850 
01851    AST_LIST_LOCK(&d->addons);
01852    AST_LIST_TRAVERSE(&d->addons, a, list) {
01853       if (!strcasecmp(a->type, "7914")) {
01854          for (i = 0; i < 14; i++)
01855             (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
01856       } else {
01857          ast_log(LOG_WARNING, "Unknown addon type '%s' found.  Skipping.\n", a->type);
01858       }
01859    }
01860    AST_LIST_UNLOCK(&d->addons);
01861 
01862    return btn;
01863 }
01864 
01865 static struct skinny_req *req_alloc(size_t size, int response_message)
01866 {
01867    struct skinny_req *req;
01868 
01869    if (!(req = ast_calloc(1, skinny_header_size + size + 4)))
01870       return NULL;
01871 
01872    req->len = htolel(size+4);
01873    req->e = htolel(response_message);
01874 
01875    return req;
01876 }
01877 
01878 static struct skinny_line *find_line_by_instance(struct skinny_device *d, int instance)
01879 {
01880    struct skinny_line *l;
01881 
01882    /*Dialing from on hook or on a 7920 uses instance 0 in requests
01883      but we need to start looking at instance 1 */
01884 
01885    if (!instance)
01886       instance = 1;
01887 
01888    AST_LIST_TRAVERSE(&d->lines, l, list){
01889       if (l->instance == instance)
01890          break;
01891    }
01892 
01893    if (!l) {
01894       ast_log(LOG_WARNING, "Could not find line with instance '%d' on device '%s'\n", instance, d->name);
01895    }
01896    return l;
01897 }
01898 
01899 static struct skinny_line *find_line_by_name(const char *dest)
01900 {
01901    struct skinny_line *l;
01902    struct skinny_line *tmpl = NULL;
01903    struct skinny_device *d;
01904    char line[256];
01905    char *at;
01906    char *device;
01907    int checkdevice = 0;
01908 
01909    ast_copy_string(line, dest, sizeof(line));
01910    at = strchr(line, '@');
01911    if (at)
01912       *at++ = '\0';
01913    device = at;
01914 
01915    if (!ast_strlen_zero(device))
01916       checkdevice = 1;
01917 
01918    AST_LIST_LOCK(&devices);
01919    AST_LIST_TRAVERSE(&devices, d, list){
01920       if (checkdevice && tmpl)
01921          break;
01922       else if (!checkdevice) {
01923          /* This is a match, since we're checking for line on every device. */
01924       } else if (!strcasecmp(d->name, device)) {
01925       } else
01926          continue;
01927 
01928       /* Found the device (or we don't care which device) */
01929       AST_LIST_TRAVERSE(&d->lines, l, list){
01930          /* Search for the right line */
01931          if (!strcasecmp(l->name, line)) {
01932             if (tmpl) {
01933                ast_log(LOG_WARNING, "Ambiguous line name: %s\n", line);
01934                AST_LIST_UNLOCK(&devices);
01935                return NULL;
01936             } else
01937                tmpl = l;
01938          }
01939       }
01940    }
01941    AST_LIST_UNLOCK(&devices);
01942    return tmpl;
01943 }
01944 
01945 static struct skinny_subline *find_subline_by_name(const char *dest)
01946 {
01947    struct skinny_line *l;
01948    struct skinny_subline *subline;
01949    struct skinny_subline *tmpsubline = NULL;
01950    struct skinny_device *d;
01951 
01952    AST_LIST_LOCK(&devices);
01953    AST_LIST_TRAVERSE(&devices, d, list){
01954       AST_LIST_TRAVERSE(&d->lines, l, list){
01955          AST_LIST_TRAVERSE(&l->sublines, subline, list){
01956             if (!strcasecmp(subline->name, dest)) {
01957                if (tmpsubline) {
01958                   ast_verb(2, "Ambiguous subline name: %s\n", dest);
01959                   AST_LIST_UNLOCK(&devices);
01960                   return NULL;
01961                } else
01962                   tmpsubline = subline;
01963             }
01964          }
01965       }
01966    }
01967    AST_LIST_UNLOCK(&devices);
01968    return tmpsubline;
01969 }
01970 
01971 static struct skinny_subline *find_subline_by_callid(struct skinny_device *d, int callid)
01972 {
01973    struct skinny_subline *subline;
01974    struct skinny_line *l;
01975 
01976    AST_LIST_TRAVERSE(&d->lines, l, list){
01977       AST_LIST_TRAVERSE(&l->sublines, subline, list){
01978          if (subline->callid == callid) {
01979             return subline;
01980          }
01981       }
01982    }
01983    return NULL;
01984 }
01985 
01986 /*!
01987  * implement the setvar config line
01988  */
01989 static struct ast_variable *add_var(const char *buf, struct ast_variable *list)
01990 {
01991    struct ast_variable *tmpvar = NULL;
01992    char *varname = ast_strdupa(buf), *varval = NULL;
01993 
01994    if ((varval = strchr(varname,'='))) {
01995       *varval++ = '\0';
01996       if ((tmpvar = ast_variable_new(varname, varval, ""))) {
01997          tmpvar->next = list;
01998          list = tmpvar;
01999       }
02000    }
02001    return list;
02002 }
02003 
02004 static void skinny_locksub(struct skinny_subchannel *sub)
02005 {
02006    if (sub && sub->owner) {
02007       ast_channel_lock(sub->owner);
02008    }
02009 }
02010 
02011 static void skinny_unlocksub(struct skinny_subchannel *sub)
02012 {
02013    if (sub && sub->owner) {
02014       ast_channel_unlock(sub->owner);
02015    }
02016 }
02017 
02018 static int skinny_sched_del(int sched_id, struct skinny_subchannel *sub)
02019 {
02020    SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Deleting SCHED %d\n",
02021       sub->callid, sched_id);
02022    return ast_sched_del(sched, sched_id);
02023 }
02024 
02025 static int skinny_sched_add(int when, ast_sched_cb callback, struct skinny_subchannel *sub)
02026 {
02027    int ret;
02028    ret = ast_sched_add(sched, when, callback, sub);
02029    SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Added SCHED %d\n",
02030       sub->callid, ret);
02031    return ret;
02032 }
02033 
02034 /* It's quicker/easier to find the subchannel when we know the instance number too */
02035 static struct skinny_subchannel *find_subchannel_by_instance_reference(struct skinny_device *d, int instance, int reference)
02036 {
02037    struct skinny_line *l = find_line_by_instance(d, instance);
02038    struct skinny_subchannel *sub;
02039 
02040    if (!l) {
02041       return NULL;
02042    }
02043 
02044    /* 7920 phones set call reference to 0, so use the first
02045       sub-channel on the list.
02046            This MIGHT need more love to be right */
02047    if (!reference)
02048       sub = AST_LIST_FIRST(&l->sub);
02049    else {
02050       AST_LIST_TRAVERSE(&l->sub, sub, list) {
02051          if (sub->callid == reference)
02052             break;
02053       }
02054    }
02055    if (!sub) {
02056       ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s'\n", reference, d->name);
02057    }
02058    return sub;
02059 }
02060 
02061 /* Find the subchannel when we only have the callid - this shouldn't happen often */
02062 static struct skinny_subchannel *find_subchannel_by_reference(struct skinny_device *d, int reference)
02063 {
02064    struct skinny_line *l;
02065    struct skinny_subchannel *sub = NULL;
02066 
02067    AST_LIST_TRAVERSE(&d->lines, l, list){
02068       AST_LIST_TRAVERSE(&l->sub, sub, list){
02069          if (sub->callid == reference)
02070             break;
02071       }
02072       if (sub)
02073          break;
02074    }
02075 
02076    if (!l) {
02077       ast_log(LOG_WARNING, "Could not find any lines that contained a subchannel with reference '%d' on device '%s'\n", reference, d->name);
02078    } else {
02079       if (!sub) {
02080          ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s@%s'\n", reference, l->name, d->name);
02081       }
02082    }
02083    return sub;
02084 }
02085 
02086 static struct skinny_speeddial *find_speeddial_by_instance(struct skinny_device *d, int instance, int isHint)
02087 {
02088    struct skinny_speeddial *sd;
02089 
02090    AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
02091       if (sd->isHint == isHint && sd->instance == instance)
02092          break;
02093    }
02094 
02095    if (!sd) {
02096       ast_log(LOG_WARNING, "Could not find speeddial with instance '%d' on device '%s'\n", instance, d->name);
02097    }
02098    return sd;
02099 }
02100 
02101 static struct ast_format *codec_skinny2ast(enum skinny_codecs skinnycodec)
02102 {
02103    switch (skinnycodec) {
02104    case SKINNY_CODEC_ALAW:
02105       return ast_format_alaw;
02106    case SKINNY_CODEC_ULAW:
02107       return ast_format_ulaw;
02108    case SKINNY_CODEC_G722:
02109       return ast_format_g722;
02110    case SKINNY_CODEC_G723_1:
02111       return ast_format_g723;
02112    case SKINNY_CODEC_G729A:
02113       return ast_format_g729;
02114    case SKINNY_CODEC_G726_32:
02115       return ast_format_g726; /* XXX Is this right? */
02116    case SKINNY_CODEC_H261:
02117       return ast_format_h261;
02118    case SKINNY_CODEC_H263:
02119       return ast_format_h263;
02120    default:
02121       return ast_format_none;
02122    }
02123 }
02124 
02125 static int codec_ast2skinny(const struct ast_format *astcodec)
02126 {
02127    if (ast_format_cmp(astcodec, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) {
02128       return SKINNY_CODEC_ALAW;
02129    } else if (ast_format_cmp(astcodec, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) {
02130       return SKINNY_CODEC_ULAW;
02131    } else if (ast_format_cmp(astcodec, ast_format_g722) == AST_FORMAT_CMP_EQUAL) {
02132       return SKINNY_CODEC_G722;
02133    } else if (ast_format_cmp(astcodec, ast_format_g723) == AST_FORMAT_CMP_EQUAL) {
02134       return SKINNY_CODEC_G723_1;
02135    } else if (ast_format_cmp(astcodec, ast_format_g729) == AST_FORMAT_CMP_EQUAL) {
02136       return SKINNY_CODEC_G729A;
02137    } else if (ast_format_cmp(astcodec, ast_format_g726) == AST_FORMAT_CMP_EQUAL) {
02138       return SKINNY_CODEC_G726_32;
02139    } else if (ast_format_cmp(astcodec, ast_format_h261) == AST_FORMAT_CMP_EQUAL) {
02140       return SKINNY_CODEC_H261;
02141    } else if (ast_format_cmp(astcodec, ast_format_h263) == AST_FORMAT_CMP_EQUAL) {
02142       return SKINNY_CODEC_H263;
02143    } else {
02144       return 0;
02145    }
02146 }
02147 
02148 static int set_callforwards(struct skinny_line *l, const char *cfwd, int cfwdtype)
02149 {
02150    if (!l)
02151       return 0;
02152 
02153    if (!ast_strlen_zero(cfwd)) {
02154       if (cfwdtype & SKINNY_CFWD_ALL) {
02155          l->cfwdtype |= SKINNY_CFWD_ALL;
02156          ast_copy_string(l->call_forward_all, cfwd, sizeof(l->call_forward_all));
02157       }
02158       if (cfwdtype & SKINNY_CFWD_BUSY) {
02159          l->cfwdtype |= SKINNY_CFWD_BUSY;
02160          ast_copy_string(l->call_forward_busy, cfwd, sizeof(l->call_forward_busy));
02161       }
02162       if (cfwdtype & SKINNY_CFWD_NOANSWER) {
02163          l->cfwdtype |= SKINNY_CFWD_NOANSWER;
02164          ast_copy_string(l->call_forward_noanswer, cfwd, sizeof(l->call_forward_noanswer));
02165       }
02166    } else {
02167       if (cfwdtype & SKINNY_CFWD_ALL) {
02168          l->cfwdtype &= ~SKINNY_CFWD_ALL;
02169          memset(l->call_forward_all, 0, sizeof(l->call_forward_all));
02170       }
02171       if (cfwdtype & SKINNY_CFWD_BUSY) {
02172          l->cfwdtype &= ~SKINNY_CFWD_BUSY;
02173          memset(l->call_forward_busy, 0, sizeof(l->call_forward_busy));
02174       }
02175       if (cfwdtype & SKINNY_CFWD_NOANSWER) {
02176          l->cfwdtype &= ~SKINNY_CFWD_NOANSWER;
02177          memset(l->call_forward_noanswer, 0, sizeof(l->call_forward_noanswer));
02178       }
02179    }
02180    return l->cfwdtype;
02181 }
02182 
02183 static void cleanup_stale_contexts(char *new, char *old)
02184 {
02185    char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
02186 
02187    while ((oldcontext = strsep(&old, "&"))) {
02188       stalecontext = NULL;
02189       ast_copy_string(newlist, new, sizeof(newlist));
02190       stringp = newlist;
02191       while ((newcontext = strsep(&stringp, "&"))) {
02192          if (strcmp(newcontext, oldcontext) == 0) {
02193             /* This is not the context you're looking for */
02194             stalecontext = NULL;
02195             break;
02196          } else if (strcmp(newcontext, oldcontext)) {
02197             stalecontext = oldcontext;
02198          }
02199 
02200       }
02201       if (stalecontext)
02202          ast_context_destroy(ast_context_find(stalecontext), "Skinny");
02203    }
02204 }
02205 
02206 static void register_exten(struct skinny_line *l)
02207 {
02208    char multi[256];
02209    char *stringp, *ext, *context;
02210 
02211    if (ast_strlen_zero(regcontext))
02212       return;
02213 
02214    ast_copy_string(multi, S_OR(l->regexten, l->name), sizeof(multi));
02215    stringp = multi;
02216    while ((ext = strsep(&stringp, "&"))) {
02217       if ((context = strchr(ext, '@'))) {
02218          *context++ = '\0'; /* split ext@context */
02219          if (!ast_context_find(context)) {
02220             ast_log(LOG_WARNING, "Context %s must exist in regcontext= in skinny.conf!\n", context);
02221             continue;
02222          }
02223       } else {
02224          context = regcontext;
02225       }
02226       ast_add_extension(context, 1, ext, 1, NULL, NULL, "Noop",
02227           ast_strdup(l->name), ast_free_ptr, "Skinny");
02228    }
02229 }
02230 
02231 static void unregister_exten(struct skinny_line *l)
02232 {
02233    char multi[256];
02234    char *stringp, *ext, *context;
02235 
02236    if (ast_strlen_zero(regcontext))
02237       return;
02238 
02239    ast_copy_string(multi, S_OR(l->regexten, l->name), sizeof(multi));
02240    stringp = multi;
02241    while ((ext = strsep(&stringp, "&"))) {
02242       if ((context = strchr(ext, '@'))) {
02243          *context++ = '\0'; /* split ext@context */
02244          if (!ast_context_find(context)) {
02245             ast_log(LOG_WARNING, "Context %s must exist in regcontext= in skinny.conf!\n", context);
02246             continue;
02247          }
02248       } else {
02249          context = regcontext;
02250       }
02251       ast_context_remove_extension(context, ext, 1, NULL);
02252    }
02253 }
02254 
02255 static int skinny_register(struct skinny_req *req, struct skinnysession *s)
02256 {
02257    struct skinny_device *d;
02258    struct skinny_line *l;
02259    struct skinny_subline *subline;
02260    struct skinny_speeddial *sd;
02261    struct sockaddr_in sin;
02262    socklen_t slen;
02263    int instance;
02264    int res = -1;
02265 
02266    if (s->auth_timeout_sched && ast_sched_del(sched, s->auth_timeout_sched)) {
02267       return 0;
02268    }
02269    s->auth_timeout_sched = 0;
02270 
02271    AST_LIST_LOCK(&devices);
02272    AST_LIST_TRAVERSE(&devices, d, list){
02273       struct ast_sockaddr addr;
02274       ast_sockaddr_from_sin(&addr, &s->sin);
02275       if (!strcasecmp(req->data.reg.name, d->id)
02276             && ast_apply_ha(d->ha, &addr)) {
02277          RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
02278 
02279          if (d->session) {
02280             ast_log(LOG_WARNING, "Device already registered.\n");
02281             transmit_definetimedate(d);
02282             res = 0;
02283             break;
02284          }
02285          s->device = d;
02286          d->type = letohl(req->data.reg.type);
02287          d->protocolversion = letohl(req->data.reg.protocolVersion);
02288          if (ast_strlen_zero(d->version_id)) {
02289             ast_copy_string(d->version_id, version_id, sizeof(d->version_id));
02290          }
02291          d->session = s;
02292 
02293          slen = sizeof(sin);
02294          if (getsockname(s->fd, (struct sockaddr *)&sin, &slen)) {
02295             ast_log(LOG_WARNING, "Cannot get socket name\n");
02296             sin.sin_addr = __ourip;
02297          }
02298          d->ourip = sin.sin_addr;
02299 
02300          AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
02301             sd->stateid = ast_extension_state_add(sd->context, sd->exten, skinny_extensionstate_cb, sd->container);
02302          }
02303          instance = 0;
02304          AST_LIST_TRAVERSE(&d->lines, l, list) {
02305             instance++;
02306          }
02307          AST_LIST_TRAVERSE(&d->lines, l, list) {
02308             ast_format_cap_get_compatible(l->confcap, d->cap, l->cap);
02309             /* l->capability = d->capability; */
02310             l->instance = instance;
02311             l->newmsgs = ast_app_has_voicemail(l->mailbox, NULL);
02312             set_callforwards(l, NULL, SKINNY_CFWD_ALL|SKINNY_CFWD_BUSY|SKINNY_CFWD_NOANSWER);
02313             register_exten(l);
02314             /* initialize MWI on line and device */
02315             mwi_event_cb(l, NULL, NULL);
02316             AST_LIST_TRAVERSE(&l->sublines, subline, list) {
02317                ast_extension_state_add(subline->context, subline->exten, skinny_extensionstate_cb, subline->container);
02318             }
02319             ast_devstate_changed(AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE, "Skinny/%s", l->name);
02320             --instance;
02321          }
02322          ast_endpoint_set_state(d->endpoint, AST_ENDPOINT_ONLINE);
02323          blob = ast_json_pack("{s: s}", "peer_status", "Registered");
02324          ast_endpoint_blob_publish(d->endpoint, ast_endpoint_state_type(), blob);
02325          res = 1;
02326          break;
02327       }
02328    }
02329    AST_LIST_UNLOCK(&devices);
02330    return res;
02331 }
02332 
02333 static void end_session(struct skinnysession *s)
02334 {
02335    pthread_cancel(s->t);
02336 }
02337 
02338 #ifdef AST_DEVMODE
02339 static char *callstate2str(int ind)
02340 {
02341    char *tmp;
02342 
02343    switch (ind) {
02344    case SKINNY_OFFHOOK:
02345       return "SKINNY_OFFHOOK";
02346    case SKINNY_ONHOOK:
02347       return "SKINNY_ONHOOK";
02348    case SKINNY_RINGOUT:
02349       return "SKINNY_RINGOUT";
02350    case SKINNY_RINGIN:
02351       return "SKINNY_RINGIN";
02352    case SKINNY_CONNECTED:
02353       return "SKINNY_CONNECTED";
02354    case SKINNY_BUSY:
02355       return "SKINNY_BUSY";
02356    case SKINNY_CONGESTION:
02357       return "SKINNY_CONGESTION";
02358    case SKINNY_PROGRESS:
02359       return "SKINNY_PROGRESS";
02360    case SKINNY_HOLD:
02361       return "SKINNY_HOLD";
02362    case SKINNY_CALLWAIT:
02363       return "SKINNY_CALLWAIT";
02364    default:
02365       if (!(tmp = ast_threadstorage_get(&callstate2str_threadbuf, CALLSTATE2STR_BUFSIZE)))
02366                         return "Unknown";
02367       snprintf(tmp, CALLSTATE2STR_BUFSIZE, "UNKNOWN-%d", ind);
02368       return tmp;
02369    }
02370 }
02371 
02372 #endif
02373 
02374 static int transmit_response_bysession(struct skinnysession *s, struct skinny_req *req)
02375 {
02376    int res = 0;
02377    unsigned long len;
02378 
02379    if (!s) {
02380       ast_log(LOG_WARNING, "Asked to transmit to a non-existent session!\n");
02381       return -1;
02382    }
02383 
02384    ast_mutex_lock(&s->lock);
02385 
02386    /* Don't optimize out assigning letohl() to len. It is necessary to guarantee that the comparison will always catch invalid values.
02387     * letohl() may or may not return a signed value depending upon which definition is used. */
02388    len = letohl(req->len);
02389    if (SKINNY_MAX_PACKET < len) {
02390       ast_log(LOG_WARNING, "transmit_response: the length of the request (%u) is out of bounds (%d)\n", letohl(req->len), SKINNY_MAX_PACKET);
02391       ast_mutex_unlock(&s->lock);
02392       return -1;
02393    }
02394 
02395    memset(s->outbuf, 0, sizeof(s->outbuf));
02396    memcpy(s->outbuf, req, skinny_header_size);
02397    memcpy(s->outbuf+skinny_header_size, &req->data, letohl(req->len));
02398 
02399    res = write(s->fd, s->outbuf, letohl(req->len)+8);
02400 
02401    if (res != letohl(req->len)+8) {
02402       ast_log(LOG_WARNING, "Transmit: write only sent %d out of %u bytes: %s\n", res, letohl(req->len)+8, strerror(errno));
02403       if (res == -1) {
02404          ast_log(LOG_WARNING, "Transmit: Skinny Client was lost, unregistering\n");
02405          end_session(s);
02406       }
02407 
02408    }
02409 
02410    ast_free(req);
02411    ast_mutex_unlock(&s->lock);
02412    return 1;
02413 }
02414 
02415 static void transmit_response(struct skinny_device *d, struct skinny_req *req)
02416 {
02417    transmit_response_bysession(d->session, req);
02418 }
02419 
02420 static void transmit_registerrej(struct skinnysession *s)
02421 {
02422    struct skinny_req *req;
02423    char name[16];
02424 
02425    if (!(req = req_alloc(sizeof(struct register_rej_message), REGISTER_REJ_MESSAGE)))
02426       return;
02427 
02428    memcpy(&name, req->data.reg.name, sizeof(name));
02429    snprintf(req->data.regrej.errMsg, sizeof(req->data.regrej.errMsg), "No Authority: %s", name);
02430 
02431    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting REGISTER_REJ_MESSAGE to UNKNOWN_DEVICE\n");
02432    transmit_response_bysession(s, req);
02433 }
02434 
02435 static void transmit_speaker_mode(struct skinny_device *d, int mode)
02436 {
02437    struct skinny_req *req;
02438 
02439    if (!(req = req_alloc(sizeof(struct set_speaker_message), SET_SPEAKER_MESSAGE)))
02440       return;
02441 
02442    req->data.setspeaker.mode = htolel(mode);
02443 
02444    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SET_SPEAKER_MESSAGE to %s, mode %d\n", d->name, mode);
02445    transmit_response(d, req);
02446 }
02447 
02448 static void transmit_microphone_mode(struct skinny_device *d, int mode)
02449 {
02450    struct skinny_req *req;
02451 
02452    if (!(req = req_alloc(sizeof(struct set_microphone_message), SET_MICROPHONE_MESSAGE)))
02453       return;
02454 
02455    req->data.setmicrophone.mode = htolel(mode);
02456 
02457    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SET_MICROPHONE_MESSAGE to %s, mode %d\n", d->name, mode);
02458    transmit_response(d, req);
02459 }
02460 
02461 //static void transmit_callinfo(struct skinny_subchannel *sub)
02462 static void transmit_callinfo(struct skinny_device *d, int instance, int callid,
02463    char *fromname, char *fromnum, char *toname, char *tonum, int calldirection, char *origtonum, char *origtoname)
02464 {
02465    struct skinny_req *req;
02466 
02467    if (!(req = req_alloc(sizeof(struct call_info_message), CALL_INFO_MESSAGE)))
02468       return;
02469 
02470    ast_copy_string(req->data.callinfo.callingPartyName, fromname, sizeof(req->data.callinfo.callingPartyName));
02471    ast_copy_string(req->data.callinfo.callingParty, fromnum, sizeof(req->data.callinfo.callingParty));
02472    ast_copy_string(req->data.callinfo.calledPartyName, toname, sizeof(req->data.callinfo.calledPartyName));
02473    ast_copy_string(req->data.callinfo.calledParty, tonum, sizeof(req->data.callinfo.calledParty));
02474    if (origtoname) {
02475       ast_copy_string(req->data.callinfo.originalCalledPartyName, origtoname, sizeof(req->data.callinfo.originalCalledPartyName));
02476    }
02477    if (origtonum) {
02478       ast_copy_string(req->data.callinfo.originalCalledParty, origtonum, sizeof(req->data.callinfo.originalCalledParty));
02479    }
02480 
02481    req->data.callinfo.instance = htolel(instance);
02482    req->data.callinfo.reference = htolel(callid);
02483    req->data.callinfo.type = htolel(calldirection);
02484 
02485    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",
02486       d->name, toname, tonum, fromname, fromnum, origtoname, origtonum, calldirection, d->name, instance);
02487    transmit_response(d, req);
02488 }
02489 
02490 static void transmit_callinfo_variable(struct skinny_device *d, int instance, int callreference,
02491    char *fromname, char *fromnum, char *toname, char *tonum, int calldirection, char *origtonum, char *origtoname)
02492 {
02493    struct skinny_req *req;
02494    char *strptr;
02495    char *thestrings[13];
02496    int i;
02497    int callinfostrleft = MAXCALLINFOSTR;
02498 
02499    if (!(req = req_alloc(sizeof(struct call_info_message_variable), CALL_INFO_MESSAGE_VARIABLE)))
02500       return;
02501 
02502    req->data.callinfomessagevariable.instance = htolel(instance);
02503    req->data.callinfomessagevariable.callreference = htolel(callreference);
02504    req->data.callinfomessagevariable.calldirection = htolel(calldirection);
02505 
02506    req->data.callinfomessagevariable.unknown1 = htolel(0x00);
02507    req->data.callinfomessagevariable.unknown2 = htolel(0x00);
02508    req->data.callinfomessagevariable.unknown3 = htolel(0x00);
02509    req->data.callinfomessagevariable.unknown4 = htolel(0x00);
02510    req->data.callinfomessagevariable.unknown5 = htolel(0x00);
02511 
02512    thestrings[0] = fromnum;
02513    thestrings[1] = "";                     /* Appears to be origfrom */
02514    if (calldirection == SKINNY_OUTGOING) {
02515       thestrings[2] = tonum;
02516       thestrings[3] = origtonum;
02517    } else {
02518       thestrings[2] = "";
02519       thestrings[3] = "";
02520    }
02521    thestrings[4] = "";
02522    thestrings[5] = "";
02523    thestrings[6] = "";
02524    thestrings[7] = "";
02525 
02526    thestrings[8] = "";
02527    thestrings[9] = fromname;
02528    thestrings[10] = toname;
02529    thestrings[11] = origtoname;
02530    thestrings[12] = "";
02531 
02532    strptr = req->data.callinfomessagevariable.calldetails;
02533 
02534    for(i = 0; i < 13; i++) {
02535       if (thestrings[i]) {
02536          ast_copy_string(strptr, thestrings[i], callinfostrleft);
02537          strptr += strlen(thestrings[i]) + 1;
02538          callinfostrleft -= strlen(thestrings[i]) + 1;
02539       } else {
02540          ast_copy_string(strptr, "", callinfostrleft);
02541          strptr++;
02542          callinfostrleft--;
02543       }
02544    }
02545 
02546    req->len = req->len - (callinfostrleft & ~0x3);
02547 
02548    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",
02549       d->name, toname, tonum, fromname, fromnum, origtoname, origtonum, calldirection, d->name, instance);
02550    transmit_response(d, req);
02551 }
02552 
02553 static void send_callinfo(struct skinny_subchannel *sub)
02554 {
02555    struct ast_channel *ast;
02556    struct skinny_device *d;
02557    struct skinny_line *l;
02558    struct ast_party_id connected_id;
02559    char *fromname;
02560    char *fromnum;
02561    char *toname;
02562    char *tonum;
02563 
02564    if (!sub || !sub->owner || !sub->line || !sub->line->device) {
02565       return;
02566    }
02567 
02568    ast = sub->owner;
02569    l = sub->line;
02570    d = l->device;
02571    connected_id = ast_channel_connected_effective_id(ast);
02572 
02573    if (sub->calldirection == SKINNY_INCOMING) {
02574       if ((ast_party_id_presentation(&connected_id) & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
02575          fromname = S_COR(connected_id.name.valid, connected_id.name.str, "");
02576          fromnum = S_COR(connected_id.number.valid, connected_id.number.str, "");
02577       } else {
02578          fromname = "";
02579          fromnum = "";
02580       }
02581       toname = S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, "");
02582       tonum = S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, "");
02583    } else if (sub->calldirection == SKINNY_OUTGOING) {
02584       fromname = S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, "");
02585       fromnum = S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, "");
02586       toname = S_COR(ast_channel_connected(ast)->id.name.valid, ast_channel_connected(ast)->id.name.str, "");
02587       tonum = S_COR(ast_channel_connected(ast)->id.number.valid, ast_channel_connected(ast)->id.number.str, l->lastnumberdialed);
02588    } else {
02589       ast_verb(1, "Error sending Callinfo to %s(%d) - No call direction in sub\n", d->name, l->instance);
02590       return;
02591    }
02592 
02593    if (d->protocolversion < 17) {
02594       transmit_callinfo(d, l->instance, sub->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
02595    } else {
02596       transmit_callinfo_variable(d, l->instance, sub->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
02597    }
02598 }
02599 
02600 static void push_callinfo(struct skinny_subline *subline, struct skinny_subchannel *sub)
02601 {
02602    struct ast_channel *ast;
02603    struct skinny_device *d;
02604    struct skinny_line *l;
02605    struct ast_party_id connected_id;
02606    char *fromname;
02607    char *fromnum;
02608    char *toname;
02609    char *tonum;
02610 
02611    if (!sub || !sub->owner || !sub->line || !sub->line->device) {
02612       return;
02613    }
02614 
02615    ast = sub->owner;
02616    l = sub->line;
02617    d = l->device;
02618    connected_id = ast_channel_connected_effective_id(ast);
02619 
02620    if (sub->calldirection == SKINNY_INCOMING) {
02621       if((ast_party_id_presentation(&connected_id) & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
02622          fromname = S_COR(connected_id.name.valid, connected_id.name.str, "");
02623          fromnum = S_COR(connected_id.number.valid, connected_id.number.str, "");
02624       } else {
02625          fromname = "";
02626          fromnum = "";
02627       }
02628       toname = S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, "");
02629       tonum = S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, "");
02630    } else if (sub->calldirection == SKINNY_OUTGOING) {
02631       fromname = S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, "");
02632       fromnum = S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, "");
02633       toname = S_COR(ast_channel_connected(ast)->id.name.valid, ast_channel_connected(ast)->id.name.str, "");
02634       tonum = S_COR(ast_channel_connected(ast)->id.number.valid, ast_channel_connected(ast)->id.number.str, l->lastnumberdialed);
02635    } else {
02636       ast_verb(1, "Error sending Callinfo to %s(%d) - No call direction in sub\n", d->name, l->instance);
02637       return;
02638    }
02639 
02640    if (d->protocolversion < 17) {
02641       transmit_callinfo(subline->line->device, subline->line->instance, subline->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
02642    } else {
02643       transmit_callinfo_variable(subline->line->device, subline->line->instance, subline->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
02644    }
02645 }
02646 
02647 static void transmit_connect(struct skinny_device *d, struct skinny_subchannel *sub)
02648 {
02649    struct skinny_req *req;
02650    struct skinny_line *l = sub->line;
02651    struct ast_format *tmpfmt;
02652    unsigned int framing;
02653 
02654    if (!(req = req_alloc(sizeof(struct open_receive_channel_message), OPEN_RECEIVE_CHANNEL_MESSAGE)))
02655       return;
02656 
02657    tmpfmt = ast_format_cap_get_format(l->cap, 0);
02658    framing = ast_format_cap_get_format_framing(l->cap, tmpfmt);
02659 
02660    req->data.openreceivechannel.conferenceId = htolel(sub->callid);
02661    req->data.openreceivechannel.partyId = htolel(sub->callid);
02662    req->data.openreceivechannel.packets = htolel(framing);
02663    req->data.openreceivechannel.capability = htolel(codec_ast2skinny(tmpfmt));
02664    req->data.openreceivechannel.echo = htolel(0);
02665    req->data.openreceivechannel.bitrate = htolel(0);
02666 
02667    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",
02668       d->name, sub->callid, sub->callid, framing, codec_ast2skinny(tmpfmt), 0, 0);
02669 
02670    ao2_ref(tmpfmt, -1);
02671 
02672    transmit_response(d, req);
02673 }
02674 
02675 static void transmit_start_tone(struct skinny_device *d, int tone, int instance, int reference)
02676 {
02677    struct skinny_req *req;
02678    if (!(req = req_alloc(sizeof(struct start_tone_message), START_TONE_MESSAGE)))
02679       return;
02680    req->data.starttone.tone = htolel(tone);
02681    req->data.starttone.instance = htolel(instance);
02682    req->data.starttone.reference = htolel(reference);
02683 
02684    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting START_TONE_MESSAGE to %s, tone %d, inst %d, ref %d\n",
02685       d->name, tone, instance, reference);
02686    transmit_response(d, req);
02687 }
02688 
02689 static void transmit_stop_tone(struct skinny_device *d, int instance, int reference)
02690 {
02691    struct skinny_req *req;
02692    if (!(req = req_alloc(sizeof(struct stop_tone_message), STOP_TONE_MESSAGE)))
02693       return;
02694    req->data.stoptone.instance = htolel(instance);
02695    req->data.stoptone.reference = htolel(reference);
02696 
02697    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting STOP_TONE_MESSAGE to %s, inst %d, ref %d\n",
02698       d->name, instance, reference);
02699    transmit_response(d, req);
02700 }
02701 
02702 static int keyset_translatebitmask(int keyset, int intmask)
02703 {
02704    int extmask = 0;
02705    int x, y;
02706    const struct soft_key_definitions *softkeymode = soft_key_default_definitions;
02707 
02708    for(x = 0; x < ARRAY_LEN(soft_key_default_definitions); x++) {
02709       if (softkeymode[x].mode == keyset) {
02710          const uint8_t *defaults = softkeymode[x].defaults;
02711 
02712          for (y = 0; y < softkeymode[x].count; y++) {
02713             if (intmask & (1 << (defaults[y]))) {
02714                extmask |= (1 << ((y)));
02715             }
02716          }
02717          break;
02718       }
02719    }
02720 
02721    return extmask;
02722 }
02723 
02724 static void transmit_selectsoftkeys(struct skinny_device *d, int instance, int callid, int softkey, int mask)
02725 {
02726    struct skinny_req *req;
02727    int newmask;
02728 
02729    if (!(req = req_alloc(sizeof(struct select_soft_keys_message), SELECT_SOFT_KEYS_MESSAGE)))
02730       return;
02731 
02732    newmask = keyset_translatebitmask(softkey, mask);
02733    req->data.selectsoftkey.instance = htolel(instance);
02734    req->data.selectsoftkey.reference = htolel(callid);
02735    req->data.selectsoftkey.softKeySetIndex = htolel(softkey);
02736    req->data.selectsoftkey.validKeyMask = htolel(newmask);
02737 
02738    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SELECT_SOFT_KEYS_MESSAGE to %s, inst %d, callid %d, softkey %d, mask 0x%08x\n",
02739       d->name, instance, callid, softkey, (unsigned)newmask);
02740    transmit_response(d, req);
02741 }
02742 
02743 static void transmit_lamp_indication(struct skinny_device *d, int stimulus, int instance, int indication)
02744 {
02745    struct skinny_req *req;
02746 
02747    if (!(req = req_alloc(sizeof(struct set_lamp_message), SET_LAMP_MESSAGE)))
02748       return;
02749 
02750    req->data.setlamp.stimulus = htolel(stimulus);
02751    req->data.setlamp.stimulusInstance = htolel(instance);
02752    req->data.setlamp.deviceStimulus = htolel(indication);
02753 
02754    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SET_LAMP_MESSAGE to %s, stim %d, inst %d, ind %d\n",
02755       d->name, stimulus, instance, indication);
02756    transmit_response(d, req);
02757 }
02758 
02759 static void transmit_ringer_mode(struct skinny_device *d, int mode)
02760 {
02761    struct skinny_req *req;
02762 
02763    if (!(req = req_alloc(sizeof(struct set_ringer_message), SET_RINGER_MESSAGE)))
02764       return;
02765 
02766    req->data.setringer.ringerMode = htolel(mode);
02767    /* XXX okay, I don't quite know what this is, but here's what happens (on a 7960).
02768       Note: The phone will always show as ringing on the display.
02769 
02770       1: phone will audibly ring over and over
02771       2: phone will audibly ring only once
02772       any other value, will NOT cause the phone to audibly ring
02773    */
02774    req->data.setringer.unknown1 = htolel(1);
02775    /* XXX the value here doesn't seem to change anything.  Must be higher than 0.
02776       Perhaps a packet capture can shed some light on this. */
02777    req->data.setringer.unknown2 = htolel(1);
02778 
02779    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SET_RINGER_MESSAGE to %s, mode %d, unk1 1, unk2 1\n",
02780       d->name, mode);
02781    transmit_response(d, req);
02782 }
02783 
02784 static void transmit_clear_display_message(struct skinny_device *d, int instance, int reference)
02785 {
02786    struct skinny_req *req;
02787    if (!(req = req_alloc(sizeof(struct clear_display_message), CLEAR_DISPLAY_MESSAGE)))
02788       return;
02789 
02790    //what do we want hear CLEAR_DISPLAY_MESSAGE or CLEAR_PROMPT_STATUS???
02791    //if we are clearing the display, it appears there is no instance and refernece info (size 0)
02792    //req->data.clearpromptstatus.lineInstance = instance;
02793    //req->data.clearpromptstatus.callReference = reference;
02794 
02795    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CLEAR_DISPLAY_MESSAGE to %s\n", d->name);
02796    transmit_response(d, req);
02797 }
02798 
02799 /* This function is not currently used, but will be (wedhorn)*/
02800 /* static void transmit_display_message(struct skinny_device *d, const char *text, int instance, int reference)
02801 {
02802    struct skinny_req *req;
02803 
02804    if (text == 0) {
02805       ast_verb(1, "Bug, Asked to display empty message\n");
02806       return;
02807    }
02808 
02809    if (!(req = req_alloc(sizeof(struct displaytext_message), DISPLAYTEXT_MESSAGE)))
02810       return;
02811 
02812    ast_copy_string(req->data.displaytext.text, text, sizeof(req->data.displaytext.text));
02813    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAYTEXT_MESSAGE to %s, text %s\n", d->name, text);
02814    transmit_response(d, req);
02815 } */
02816 
02817 static void transmit_displaynotify(struct skinny_device *d, const char *text, int t)
02818 {
02819    struct skinny_req *req;
02820 
02821    if (!(req = req_alloc(sizeof(struct display_notify_message), DISPLAY_NOTIFY_MESSAGE)))
02822       return;
02823 
02824    ast_copy_string(req->data.displaynotify.displayMessage, text, sizeof(req->data.displaynotify.displayMessage));
02825    req->data.displaynotify.displayTimeout = htolel(t);
02826 
02827    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_NOTIFY_MESSAGE to %s, text %s\n", d->name, text);
02828    transmit_response(d, req);
02829 }
02830 
02831 static void transmit_clearprinotify(struct skinny_device *d, int priority)
02832 {
02833    struct skinny_req *req;
02834 
02835    if (!(req = req_alloc(sizeof(struct clear_prinotify_message), CLEAR_PRINOTIFY_MESSAGE)))
02836       return;
02837 
02838    req->data.clearprinotify.priority = htolel(priority);
02839 
02840    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CLEAR_PRINOTIFY_MESSAGE to %s, priority %d\n", d->name, priority);
02841    transmit_response(d, req);
02842 }
02843 
02844 static void _transmit_displayprinotify(struct skinny_device *d, const char *text, const char *extratext, int timeout, int priority)
02845 {
02846    struct skinny_req *req;
02847 
02848    if (!(req = req_alloc(sizeof(struct display_prinotify_message), DISPLAY_PRINOTIFY_MESSAGE)))
02849       return;
02850 
02851    req->data.displayprinotify.timeout = htolel(timeout);
02852    req->data.displayprinotify.priority = htolel(priority);
02853 
02854    if ((char)*text == '\200') {
02855       int octalstrlen = strlen(text);
02856       ast_copy_string(req->data.displayprinotify.text, text, sizeof(req->data.displayprinotify.text));
02857       ast_copy_string(req->data.displayprinotify.text+octalstrlen, extratext, sizeof(req->data.displayprinotify.text)-octalstrlen);
02858       SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PRINOTIFY_MESSAGE to %s, '\\%03o\\%03o', '%s', timeout=%d, priority=%d\n",
02859          d->name, (unsigned)*text, (unsigned)*(text+1), extratext, timeout, priority);
02860    } else {
02861       ast_copy_string(req->data.displayprinotify.text, text, sizeof(req->data.displayprinotify.text));
02862       SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PRINOTIFY_MESSAGE to %s, '%s', timeout=%d, priority=%d\n",
02863          d->name, text, timeout, priority);
02864    }
02865 
02866    transmit_response(d, req);
02867 }
02868 
02869 static void _transmit_displayprinotifyvar(struct skinny_device *d, const char *text, const char *extratext, int timeout, int priority)
02870 {
02871    struct skinny_req *req;
02872    int packetlen;
02873 
02874    if (!(req = req_alloc(sizeof(struct display_prinotify_message_variable), DISPLAY_PRINOTIFY_MESSAGE_VARIABLE)))
02875       return;
02876 
02877    req->data.displayprinotifyvar.timeout = htolel(timeout);
02878    req->data.displayprinotifyvar.priority = htolel(priority);
02879 
02880    if ((char)*text == '\200') {
02881       int octalstrlen = strlen(text);
02882       ast_copy_string(req->data.displayprinotifyvar.text, text, sizeof(req->data.displayprinotifyvar.text));
02883       ast_copy_string(req->data.displayprinotifyvar.text+octalstrlen, extratext, sizeof(req->data.displayprinotifyvar.text)-octalstrlen);
02884       packetlen = req->len - MAXDISPLAYNOTIFYSTR + strlen(text) + strlen(extratext);
02885       SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PRINOTIFY_MESSAGE_VARIABLE to %s, '\\%03o\\%03o', '%s', timeout=%d, priority=%d\n",
02886          d->name, (unsigned)*text, (unsigned)*(text+1), extratext, timeout, priority);
02887    } else {
02888       ast_copy_string(req->data.displayprinotifyvar.text, text, sizeof(req->data.displayprinotifyvar.text));
02889       packetlen = req->len - MAXDISPLAYNOTIFYSTR + strlen(text);
02890       SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PRINOTIFY_MESSAGE_VARIABLE to %s, '%s', timeout=%d, priority=%d\n",
02891          d->name, text, timeout, priority);
02892    }
02893 
02894    req->len = (packetlen & ~0x3) + 4;
02895 
02896    transmit_response(d, req);
02897 }
02898 
02899 static void send_displayprinotify(struct skinny_device *d, const char *text, const char *extratext, int timeout, int priority)
02900 {
02901    if (d->protocolversion < 17) {
02902       _transmit_displayprinotify(d, text, extratext, timeout, priority);
02903    } else {
02904       _transmit_displayprinotifyvar(d, text, extratext, timeout, priority);
02905    }
02906 }
02907 
02908 static void transmit_displaypromptstatus(struct skinny_device *d, const char *text, const char *extratext, int t, int instance, int callid)
02909 {
02910    struct skinny_req *req;
02911 
02912    if (!(req = req_alloc(sizeof(struct display_prompt_status_message), DISPLAY_PROMPT_STATUS_MESSAGE)))
02913       return;
02914 
02915    req->data.displaypromptstatus.messageTimeout = htolel(t);
02916    req->data.displaypromptstatus.lineInstance = htolel(instance);
02917    req->data.displaypromptstatus.callReference = htolel(callid);
02918 
02919    if ((char)*text == '\200') {
02920       int octalstrlen = strlen(text);
02921       ast_copy_string(req->data.displaypromptstatus.promptMessage, text, sizeof(req->data.displaypromptstatusvar.promptMessage));
02922       ast_copy_string(req->data.displaypromptstatus.promptMessage+octalstrlen, extratext, sizeof(req->data.displaypromptstatus.promptMessage)-octalstrlen);
02923       SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PROMPT_STATUS_MESSAGE to %s, '\\%03o\\%03o', '%s'\n",
02924          d->name, (unsigned)*text, (unsigned)*(text+1), extratext);
02925    } else {
02926       ast_copy_string(req->data.displaypromptstatus.promptMessage, text, sizeof(req->data.displaypromptstatus.promptMessage));
02927       SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PROMPT_STATUS_MESSAGE to %s, '%s'\n",
02928          d->name, text);
02929    }
02930    transmit_response(d, req);
02931 }
02932 
02933 static void transmit_displaypromptstatusvar(struct skinny_device *d, const char *text, const char *extratext, int t, int instance, int callid)
02934 {
02935    struct skinny_req *req;
02936    int packetlen;
02937 
02938    if (!(req = req_alloc(sizeof(struct display_prompt_status_message_variable), DISPLAY_PROMPT_STATUS_MESSAGE_VARIABLE)))
02939       return;
02940 
02941    req->data.displaypromptstatusvar.lineInstance = htolel(instance);
02942    req->data.displaypromptstatusvar.callReference = htolel(callid);
02943 
02944    if ((char)*text == '\200') {
02945       int octalstrlen = strlen(text);
02946       ast_copy_string(req->data.displaypromptstatusvar.promptMessage, text, sizeof(req->data.displaypromptstatusvar.promptMessage));
02947       ast_copy_string(req->data.displaypromptstatusvar.promptMessage+octalstrlen, extratext, sizeof(req->data.displaypromptstatusvar.promptMessage)-octalstrlen);
02948       packetlen = req->len - MAXCALLINFOSTR + strlen(text) + strlen(extratext);
02949       SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PROMPT_STATUS_MESSAGE_VARIABLE to %s, '\\%03o\\%03o', '%s'\n",
02950          d->name, (unsigned)*text, (unsigned)*(text+1), extratext);
02951    } else {
02952       ast_copy_string(req->data.displaypromptstatusvar.promptMessage, text, sizeof(req->data.displaypromptstatus.promptMessage));
02953       packetlen = req->len - MAXCALLINFOSTR + strlen(text);
02954       SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PROMPT_STATUS_MESSAGE_VARIABLE to %s, '%s'\n",
02955          d->name, text);
02956    }
02957    req->len = (packetlen & ~0x3) + 4;
02958 
02959    transmit_response(d, req);
02960 }
02961 
02962 static void send_displaypromptstatus(struct skinny_device *d, const char *text, const char *extratext, int t, int instance, int callid)
02963 {
02964    if (d->protocolversion < 17) {
02965       transmit_displaypromptstatus(d, text, extratext, t, instance, callid);
02966    } else {
02967       transmit_displaypromptstatusvar(d, text, extratext, t, instance, callid);
02968    }
02969 }
02970 
02971 static void transmit_clearpromptmessage(struct skinny_device *d, int instance, int callid)
02972 {
02973    struct skinny_req *req;
02974 
02975    if (!(req = req_alloc(sizeof(struct clear_prompt_message), CLEAR_PROMPT_MESSAGE)))
02976       return;
02977 
02978    req->data.clearpromptstatus.lineInstance = htolel(instance);
02979    req->data.clearpromptstatus.callReference = htolel(callid);
02980 
02981    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CLEAR_PROMPT_MESSAGE to %s, inst %d, callid %d\n",
02982       d->name, instance, callid);
02983    transmit_response(d, req);
02984 }
02985 
02986 static void transmit_dialednumber(struct skinny_device *d, const char *text, int instance, int callid)
02987 {
02988    struct skinny_req *req;
02989 
02990    if (!(req = req_alloc(sizeof(struct dialed_number_message), DIALED_NUMBER_MESSAGE)))
02991       return;
02992 
02993    ast_copy_string(req->data.dialednumber.dialedNumber, text, sizeof(req->data.dialednumber.dialedNumber));
02994    req->data.dialednumber.lineInstance = htolel(instance);
02995    req->data.dialednumber.callReference = htolel(callid);
02996 
02997    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DIALED_NUMBER_MESSAGE to %s, num %s, inst %d, callid %d\n",
02998       d->name, text, instance, callid);
02999    transmit_response(d, req);
03000 }
03001 
03002 static void transmit_closereceivechannel(struct skinny_device *d, struct skinny_subchannel *sub)
03003 {
03004    struct skinny_req *req;
03005 
03006    if (!(req = req_alloc(sizeof(struct close_receive_channel_message), CLOSE_RECEIVE_CHANNEL_MESSAGE)))
03007       return;
03008 
03009    req->data.closereceivechannel.conferenceId = htolel(0);
03010    req->data.closereceivechannel.partyId = htolel(sub->callid);
03011 
03012    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CLOSE_RECEIVE_CHANNEL_MESSAGE to %s, confid %d, callid %u\n",
03013       d->name, 0, sub->callid);
03014    transmit_response(d, req);
03015 }
03016 
03017 static void transmit_stopmediatransmission(struct skinny_device *d, struct skinny_subchannel *sub)
03018 {
03019    struct skinny_req *req;
03020 
03021    if (!(req = req_alloc(sizeof(struct stop_media_transmission_message), STOP_MEDIA_TRANSMISSION_MESSAGE)))
03022       return;
03023 
03024    req->data.stopmedia.conferenceId = htolel(0);
03025    req->data.stopmedia.passThruPartyId = htolel(sub->callid);
03026 
03027    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting STOP_MEDIA_TRANSMISSION_MESSAGE to %s, confid %d, passthrupartyid %u\n",
03028       d->name, 0, sub->callid);
03029    transmit_response(d, req);
03030 }
03031 
03032 static void transmit_startmediatransmission(struct skinny_device *d, struct skinny_subchannel *sub, struct sockaddr_in dest,
03033    struct ast_format *format, unsigned int framing)
03034 {
03035    struct skinny_req *req;
03036 
03037    if (d->protocolversion < 17) {
03038       if (!(req = req_alloc(sizeof(struct start_media_transmission_message_ip4), START_MEDIA_TRANSMISSION_MESSAGE)))
03039          return;
03040       req->data.startmedia_ip4.conferenceId = htolel(sub->callid);
03041       req->data.startmedia_ip4.passThruPartyId = htolel(sub->callid);
03042       req->data.startmedia_ip4.remoteIp = dest.sin_addr.s_addr;
03043       req->data.startmedia_ip4.remotePort = htolel(ntohs(dest.sin_port));
03044       req->data.startmedia_ip4.packetSize = htolel(framing);
03045       req->data.startmedia_ip4.payloadType = htolel(codec_ast2skinny(format));
03046       req->data.startmedia_ip4.qualifier.precedence = htolel(127);
03047       req->data.startmedia_ip4.qualifier.vad = htolel(0);
03048       req->data.startmedia_ip4.qualifier.packets = htolel(0);
03049       req->data.startmedia_ip4.qualifier.bitRate = htolel(0);
03050    } else {
03051       if (!(req = req_alloc(sizeof(struct start_media_transmission_message_ip6), START_MEDIA_TRANSMISSION_MESSAGE)))
03052          return;
03053       req->data.startmedia_ip6.conferenceId = htolel(sub->callid);
03054       req->data.startmedia_ip6.passThruPartyId = htolel(sub->callid);
03055       memcpy(req->data.startmedia_ip6.remoteIp, &dest.sin_addr.s_addr, sizeof(dest.sin_addr.s_addr));
03056       req->data.startmedia_ip6.remotePort = htolel(ntohs(dest.sin_port));
03057       req->data.startmedia_ip6.packetSize = htolel(framing);
03058       req->data.startmedia_ip6.payloadType = htolel(codec_ast2skinny(format));
03059       req->data.startmedia_ip6.qualifier.precedence = htolel(127);
03060       req->data.startmedia_ip6.qualifier.vad = htolel(0);
03061       req->data.startmedia_ip6.qualifier.packets = htolel(0);
03062       req->data.startmedia_ip6.qualifier.bitRate = htolel(0);
03063    }
03064 
03065    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",
03066       d->name, sub->callid, sub->callid, ast_inet_ntoa(dest.sin_addr), dest.sin_port, framing, codec_ast2skinny(format));
03067    transmit_response(d, req);
03068 }
03069 
03070 static void transmit_activatecallplane(struct skinny_device *d, struct skinny_line *l)
03071 {
03072    struct skinny_req *req;
03073 
03074    if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
03075       return;
03076 
03077    req->data.activatecallplane.lineInstance = htolel(l->instance);
03078 
03079    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting ACTIVATE_CALL_PLANE_MESSAGE to %s, inst %d\n",
03080       d->name, l->instance);
03081    transmit_response(d, req);
03082 }
03083 
03084 static void transmit_callstate(struct skinny_device *d, int buttonInstance, unsigned callid, int state)
03085 {
03086    struct skinny_req *req;
03087 
03088    if (!(req = req_alloc(sizeof(struct call_state_message), CALL_STATE_MESSAGE)))
03089       return;
03090 
03091    req->data.callstate.callState = htolel(state);
03092    req->data.callstate.lineInstance = htolel(buttonInstance);
03093    req->data.callstate.callReference = htolel(callid);
03094 
03095    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CALL_STATE_MESSAGE to %s, state %s, inst %d, callid %u\n",
03096       d->name, callstate2str(state), buttonInstance, callid);
03097    transmit_response(d, req);
03098 }
03099 
03100 static void transmit_cfwdstate(struct skinny_device *d, struct skinny_line *l)
03101 {
03102    struct skinny_req *req;
03103    int anyon = 0;
03104 
03105    if (!(req = req_alloc(sizeof(struct forward_stat_message), FORWARD_STAT_MESSAGE)))
03106       return;
03107 
03108    if (l->cfwdtype & SKINNY_CFWD_ALL) {
03109       if (!ast_strlen_zero(l->call_forward_all)) {
03110          ast_copy_string(req->data.forwardstat.fwdallnum, l->call_forward_all, sizeof(req->data.forwardstat.fwdallnum));
03111          req->data.forwardstat.fwdall = htolel(1);
03112          anyon++;
03113       } else {
03114          req->data.forwardstat.fwdall = htolel(0);
03115       }
03116    }
03117    if (l->cfwdtype & SKINNY_CFWD_BUSY) {
03118       if (!ast_strlen_zero(l->call_forward_busy)) {
03119          ast_copy_string(req->data.forwardstat.fwdbusynum, l->call_forward_busy, sizeof(req->data.forwardstat.fwdbusynum));
03120          req->data.forwardstat.fwdbusy = htolel(1);
03121          anyon++;
03122       } else {
03123          req->data.forwardstat.fwdbusy = htolel(0);
03124       }
03125    }
03126    if (l->cfwdtype & SKINNY_CFWD_NOANSWER) {
03127       if (!ast_strlen_zero(l->call_forward_noanswer)) {
03128          ast_copy_string(req->data.forwardstat.fwdnoanswernum, l->call_forward_noanswer, sizeof(req->data.forwardstat.fwdnoanswernum));
03129          req->data.forwardstat.fwdnoanswer = htolel(1);
03130          anyon++;
03131       } else {
03132          req->data.forwardstat.fwdnoanswer = htolel(0);
03133       }
03134    }
03135    req->data.forwardstat.lineNumber = htolel(l->instance);
03136    if (anyon)
03137       req->data.forwardstat.activeforward = htolel(7);
03138    else
03139       req->data.forwardstat.activeforward = htolel(0);
03140 
03141    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting FORWARD_STAT_MESSAGE to %s, inst %d, all %s, busy %s, noans %s, acitve %d\n",
03142       d->name, l->instance, l->call_forward_all, l->call_forward_busy, l->call_forward_noanswer, anyon ? 7 : 0);
03143    transmit_response(d, req);
03144 }
03145 
03146 static void transmit_speeddialstatres(struct skinny_device *d, struct skinny_speeddial *sd)
03147 {
03148    struct skinny_req *req;
03149 
03150    if (!(req = req_alloc(sizeof(struct speed_dial_stat_res_message), SPEED_DIAL_STAT_RES_MESSAGE)))
03151       return;
03152 
03153    req->data.speeddialreq.speedDialNumber = htolel(sd->instance);
03154    ast_copy_string(req->data.speeddial.speedDialDirNumber, sd->exten, sizeof(req->data.speeddial.speedDialDirNumber));
03155    ast_copy_string(req->data.speeddial.speedDialDisplayName, sd->label, sizeof(req->data.speeddial.speedDialDisplayName));
03156 
03157    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SPEED_DIAL_STAT_RES_MESSAGE to %s, inst %d, dir %s, display %s\n",
03158       d->name, sd->instance, sd->exten, sd->label);
03159    transmit_response(d, req);
03160 }
03161 
03162 //static void transmit_linestatres(struct skinny_device *d, struct skinny_line *l)
03163 static void transmit_linestatres(struct skinny_device *d, int instance)
03164 {
03165    struct skinny_req *req;
03166    struct skinny_line *l;
03167    struct skinny_speeddial *sd;
03168 
03169    if (!(req = req_alloc(sizeof(struct line_stat_res_message), LINE_STAT_RES_MESSAGE)))
03170       return;
03171 
03172    if ((l = find_line_by_instance(d, instance))) {
03173       req->data.linestat.lineNumber = letohl(l->instance);
03174       memcpy(req->data.linestat.lineDirNumber, l->name, sizeof(req->data.linestat.lineDirNumber));
03175       memcpy(req->data.linestat.lineDisplayName, l->label, sizeof(req->data.linestat.lineDisplayName));
03176    } else if ((sd = find_speeddial_by_instance(d, instance, 1))) {
03177       req->data.linestat.lineNumber = letohl(sd->instance);
03178       memcpy(req->data.linestat.lineDirNumber, sd->label, sizeof(req->data.linestat.lineDirNumber));
03179       memcpy(req->data.linestat.lineDisplayName, sd->label, sizeof(req->data.linestat.lineDisplayName));
03180    }
03181 
03182    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting LINE_STAT_RES_MESSAGE to %s, inst %d, num %s, label %s\n",
03183       d->name, l->instance, req->data.linestat.lineDirNumber, req->data.linestat.lineDisplayName);
03184    transmit_response(d, req);
03185 }
03186 
03187 static void transmit_definetimedate(struct skinny_device *d)
03188 {
03189    struct skinny_req *req;
03190    struct timeval now = ast_tvnow();
03191    struct ast_tm cmtime;
03192 
03193    if (!(req = req_alloc(sizeof(struct definetimedate_message), DEFINETIMEDATE_MESSAGE)))
03194       return;
03195 
03196    ast_localtime(&now, &cmtime, NULL);
03197    req->data.definetimedate.year = htolel(cmtime.tm_year+1900);
03198    req->data.definetimedate.month = htolel(cmtime.tm_mon+1);
03199    req->data.definetimedate.dayofweek = htolel(cmtime.tm_wday);
03200    req->data.definetimedate.day = htolel(cmtime.tm_mday);
03201    req->data.definetimedate.hour = htolel(cmtime.tm_hour);
03202    req->data.definetimedate.minute = htolel(cmtime.tm_min);
03203    req->data.definetimedate.seconds = htolel(cmtime.tm_sec);
03204    req->data.definetimedate.milliseconds = htolel(cmtime.tm_usec / 1000);
03205    req->data.definetimedate.timestamp = htolel(now.tv_sec);
03206 
03207    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DEFINETIMEDATE_MESSAGE to %s, date %u %u %u dow %u time %u:%u:%u.%u\n",
03208       d->name, req->data.definetimedate.year, req->data.definetimedate.month, req->data.definetimedate.day, req->data.definetimedate.dayofweek,
03209       req->data.definetimedate.hour, req->data.definetimedate.minute, req->data.definetimedate.seconds, req->data.definetimedate.milliseconds);
03210    transmit_response(d, req);
03211 }
03212 
03213 static void transmit_versionres(struct skinny_device *d)
03214 {
03215    struct skinny_req *req;
03216    if (!(req = req_alloc(sizeof(struct version_res_message), VERSION_RES_MESSAGE)))
03217       return;
03218 
03219    ast_copy_string(req->data.version.version, d->version_id, sizeof(req->data.version.version));
03220 
03221    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting VERSION_RES_MESSAGE to %s, version %s\n", d->name, d->version_id);
03222    transmit_response(d, req);
03223 }
03224 
03225 static void transmit_serverres(struct skinny_device *d)
03226 {
03227    struct skinny_req *req;
03228    if (!(req = req_alloc(sizeof(struct server_res_message), SERVER_RES_MESSAGE)))
03229       return;
03230 
03231    memcpy(req->data.serverres.server[0].serverName, ourhost,
03232          sizeof(req->data.serverres.server[0].serverName));
03233    req->data.serverres.serverListenPort[0] = htolel(ourport);
03234    req->data.serverres.serverIpAddr[0] = htolel(d->ourip.s_addr);
03235 
03236    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SERVER_RES_MESSAGE to %s, srvname %s %s:%d\n",
03237       d->name, ourhost, ast_inet_ntoa(d->ourip), ourport);
03238    transmit_response(d, req);
03239 }
03240 
03241 static void transmit_softkeysetres(struct skinny_device *d)
03242 {
03243    struct skinny_req *req;
03244    int i;
03245    int x;
03246    int y;
03247    int keydefcount;
03248    const struct soft_key_definitions *softkeymode = soft_key_default_definitions;
03249 
03250    if (!(req = req_alloc(sizeof(struct soft_key_set_res_message), SOFT_KEY_SET_RES_MESSAGE)))
03251       return;
03252 
03253    SKINNY_DEBUG(DEBUG_TEMPLATE, 3, "Creating Softkey Template\n");
03254 
03255    keydefcount = ARRAY_LEN(soft_key_default_definitions);
03256    req->data.softkeysets.softKeySetOffset = htolel(0);
03257    req->data.softkeysets.softKeySetCount = htolel(keydefcount);
03258    req->data.softkeysets.totalSoftKeySetCount = htolel(keydefcount);
03259    for (x = 0; x < keydefcount; x++) {
03260       const uint8_t *defaults = softkeymode->defaults;
03261       /* XXX I wanted to get the size of the array dynamically, but that wasn't wanting to work.
03262          This will have to do for now. */
03263       for (y = 0; y < softkeymode->count; y++) {
03264          for (i = 0; i < (sizeof(soft_key_template_default) / sizeof(struct soft_key_template_definition)); i++) {
03265             if (defaults[y] == i+1) {
03266                req->data.softkeysets.softKeySetDefinition[softkeymode->mode].softKeyTemplateIndex[y] = (i+1);
03267                req->data.softkeysets.softKeySetDefinition[softkeymode->mode].softKeyInfoIndex[y] = htoles(i+301);
03268                SKINNY_DEBUG(DEBUG_TEMPLATE, 4, "softKeySetDefinition : softKeyTemplateIndex: %d softKeyInfoIndex: %d\n",
03269                   i+1, i+301);
03270             }
03271          }
03272       }
03273       softkeymode++;
03274    }
03275 
03276    SKINNY_DEBUG(DEBUG_PACKET | DEBUG_TEMPLATE, 3, "Transmitting SOFT_KEY_SET_RES_MESSAGE to %s, template data\n",
03277       d->name);
03278    transmit_response(d, req);
03279 }
03280 
03281 static void transmit_softkeytemplateres(struct skinny_device *d)
03282 {
03283    struct skinny_req *req;
03284 
03285    if (!(req = req_alloc(sizeof(struct soft_key_template_res_message), SOFT_KEY_TEMPLATE_RES_MESSAGE)))
03286       return;
03287 
03288    req->data.softkeytemplate.softKeyOffset = htolel(0);
03289    req->data.softkeytemplate.softKeyCount = htolel(sizeof(soft_key_template_default) / sizeof(struct soft_key_template_definition));
03290    req->data.softkeytemplate.totalSoftKeyCount = htolel(sizeof(soft_key_template_default) / sizeof(struct soft_key_template_definition));
03291    memcpy(req->data.softkeytemplate.softKeyTemplateDefinition,
03292       soft_key_template_default,
03293       sizeof(soft_key_template_default));
03294 
03295    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SOFT_KEY_TEMPLATE_RES_MESSAGE to %s, offset 0, keycnt %u, totalkeycnt %u, template data\n",
03296       d->name, req->data.softkeytemplate.softKeyCount, req->data.softkeytemplate.totalSoftKeyCount);
03297    transmit_response(d, req);
03298 }
03299 
03300 static void transmit_reset(struct skinny_device *d, int fullrestart)
03301 {
03302    struct skinny_req *req;
03303 
03304    if (!(req = req_alloc(sizeof(struct reset_message), RESET_MESSAGE)))
03305       return;
03306 
03307    if (fullrestart)
03308       req->data.reset.resetType = 2;
03309    else
03310       req->data.reset.resetType = 1;
03311 
03312    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting RESET_MESSAGE to %s, type %s\n",
03313       d->name, (fullrestart) ? "Restarting" : "Resetting");
03314    transmit_response(d, req);
03315 }
03316 
03317 static void transmit_keepaliveack(struct skinnysession *s)
03318 {
03319    struct skinny_req *req;
03320 
03321    if (!(req = req_alloc(0, KEEP_ALIVE_ACK_MESSAGE)))
03322       return;
03323 
03324 #ifdef AST_DEVMODE
03325    {
03326    struct skinny_device *d = s->device;
03327    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting KEEP_ALIVE_ACK_MESSAGE to %s\n", (d ? d->name : "unregistered"));
03328    }
03329 #endif
03330    transmit_response_bysession(s, req);
03331 }
03332 
03333 static void transmit_registerack(struct skinny_device *d)
03334 {
03335    struct skinny_req *req;
03336 
03337    if (!(req = req_alloc(sizeof(struct register_ack_message), REGISTER_ACK_MESSAGE)))
03338       return;
03339 
03340    req->data.regack.res[0] = '0';
03341    req->data.regack.res[1] = '\0';
03342    req->data.regack.keepAlive = htolel(keep_alive);
03343    memcpy(req->data.regack.dateTemplate, date_format, sizeof(req->data.regack.dateTemplate));
03344    req->data.regack.res2[0] = '0';
03345    req->data.regack.res2[1] = '\0';
03346    req->data.regack.secondaryKeepAlive = htolel(keep_alive);
03347 
03348 #ifdef AST_DEVMODE
03349    {
03350    short res = req->data.regack.res[0] << 8 | req->data.regack.res[1];
03351    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];
03352    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting REGISTER_ACK_MESSAGE to %s, keepalive %d, datetemplate %s, seckeepalive %d, res 0x%04x, res2 0x%08x\n",
03353       d->name, keep_alive, date_format, keep_alive, (unsigned)res, res2);
03354    }
03355 #endif
03356 
03357    transmit_response(d, req);
03358 }
03359 
03360 static void transmit_capabilitiesreq(struct skinny_device *d)
03361 {
03362    struct skinny_req *req;
03363 
03364    if (!(req = req_alloc(0, CAPABILITIES_REQ_MESSAGE)))
03365       return;
03366 
03367    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CAPABILITIES_REQ_MESSAGE to %s\n", d->name);
03368    transmit_response(d, req);
03369 }
03370 
03371 static void transmit_backspace(struct skinny_device *d, int instance, unsigned callid)
03372 {
03373    struct skinny_req *req;
03374 
03375    if (!(req = req_alloc(sizeof(struct bksp_req_message), BKSP_REQ_MESSAGE)))
03376       return;
03377 
03378    req->data.bkspmessage.instance = htolel(instance);
03379    req->data.bkspmessage.callreference = htolel(callid);
03380 
03381    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting BKSP_REQ_MESSAGE to %s, inst %d, callid %u \n",
03382       d->name, instance, callid);
03383    transmit_response(d, req);
03384 }
03385 
03386 static void transmit_serviceurlstat(struct skinny_device *d, int instance)
03387 {
03388    struct skinny_req *req;
03389    struct skinny_serviceurl *surl;
03390 
03391    if (!(req = req_alloc(sizeof(struct serviceurl_stat_message), SERVICEURL_STAT_MESSAGE)))
03392       return;
03393 
03394    AST_LIST_TRAVERSE(&d->serviceurls, surl, list) {
03395       if (surl->instance == instance) {
03396          break;
03397       }
03398    }
03399 
03400    if (surl) {
03401       memcpy(req->data.serviceurlmessage.displayName, surl->displayName, sizeof(req->data.serviceurlmessage.displayName));
03402       memcpy(req->data.serviceurlmessage.url, surl->url, sizeof(req->data.serviceurlmessage.url));
03403    }
03404    req->data.serviceurlmessage.instance = htolel(instance);
03405 
03406    SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SERVICEURL_STAT_MESSAGE to %s, inst %d\n",
03407       d->name, instance);
03408    transmit_response(d, req);
03409 }
03410 
03411 static int skinny_extensionstate_cb(char *context, char *exten, struct ast_state_cb_info *info, void *data)
03412 {
03413    struct skinny_container *container = data;
03414    struct skinny_device *d = NULL;
03415    char hint[AST_MAX_EXTENSION];
03416    int state = info->exten_state;
03417 
03418    /* only interested in device state here */
03419    if (info->reason != AST_HINT_UPDATE_DEVICE) {
03420       return 0;
03421    }
03422 
03423    if (container->type == SKINNY_SDCONTAINER) {
03424       struct skinny_speeddial *sd = container->data;
03425       d = sd->parent;
03426 
03427       SKINNY_DEBUG(DEBUG_HINT, 3, "Got hint %s on speeddial %s\n", ast_extension_state2str(state), sd->label);
03428 
03429       if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, sd->context, sd->exten)) {
03430          /* If they are not registered, we will override notification and show no availability */
03431          if (ast_device_state(hint) == AST_DEVICE_UNAVAILABLE) {
03432             transmit_lamp_indication(d, STIMULUS_LINE, sd->instance, SKINNY_LAMP_FLASH);
03433             transmit_callstate(d, sd->instance, 0, SKINNY_ONHOOK);
03434             return 0;
03435          }
03436          switch (state) {
03437          case AST_EXTENSION_DEACTIVATED: /* Retry after a while */
03438          case AST_EXTENSION_REMOVED:     /* Extension is gone */
03439             SKINNY_DEBUG(DEBUG_HINT, 3, "Extension state: Watcher for hint %s %s. Notify Device %s\n",
03440                exten, state == AST_EXTENSION_DEACTIVATED ? "deactivated" : "removed", d->name);
03441             sd->stateid = -1;
03442             transmit_lamp_indication(d, STIMULUS_LINE, sd->instance, SKINNY_LAMP_OFF);
03443             transmit_callstate(d, sd->instance, 0, SKINNY_ONHOOK);
03444             break;
03445          case AST_EXTENSION_RINGING:
03446          case AST_EXTENSION_UNAVAILABLE:
03447             transmit_lamp_indication(d, STIMULUS_LINE, sd->instance, SKINNY_LAMP_BLINK);
03448             transmit_callstate(d, sd->instance, 0, SKINNY_RINGIN);
03449             break;
03450          case AST_EXTENSION_BUSY: /* callstate = SKINNY_BUSY wasn't wanting to work - I'll settle for this */
03451          case AST_EXTENSION_INUSE:
03452             transmit_lamp_indication(d, STIMULUS_LINE, sd->instance, SKINNY_LAMP_ON);
03453             transmit_callstate(d, sd->instance, 0, SKINNY_CALLREMOTEMULTILINE);
03454             break;
03455          case AST_EXTENSION_ONHOLD:
03456             transmit_lamp_indication(d, STIMULUS_LINE, sd->instance, SKINNY_LAMP_WINK);
03457             transmit_callstate(d, sd->instance, 0, SKINNY_HOLD);
03458             break;
03459          case AST_EXTENSION_NOT_INUSE:
03460          default:
03461             transmit_lamp_indication(d, STIMULUS_LINE, sd->instance, SKINNY_LAMP_OFF);
03462             transmit_callstate(d, sd->instance, 0, SKINNY_ONHOOK);
03463             break;
03464          }
03465       }
03466       sd->laststate = state;
03467    } else if (container->type == SKINNY_SUBLINECONTAINER) {
03468       struct skinny_subline *subline = container->data;
03469       struct skinny_line *l = subline->line;
03470       d = l->device;
03471 
03472       SKINNY_DEBUG(DEBUG_HINT, 3, "Got hint %s on subline %s (%s@%s)\n", ast_extension_state2str(state), subline->name, exten, context);
03473 
03474       subline->extenstate = state;
03475 
03476       if (subline->callid == 0) {
03477          return 0;
03478       }
03479 
03480       switch (state) {
03481       case AST_EXTENSION_RINGING: /* Handled by normal ringin */
03482          break;
03483       case AST_EXTENSION_INUSE:
03484          if (subline->sub && (subline->sub->substate == SKINNY_CONNECTED)) { /* Device has a real call */
03485             transmit_callstate(d, l->instance, subline->callid, SKINNY_CONNECTED);
03486             transmit_selectsoftkeys(d, l->instance, subline->callid, KEYDEF_CONNECTED, KEYMASK_ALL);
03487             send_displaypromptstatus(d, OCTAL_CONNECTED, "", 0, l->instance, subline->callid);
03488          } else { /* Some other device has active call */
03489             transmit_callstate(d, l->instance, subline->callid, SKINNY_CALLREMOTEMULTILINE);
03490             transmit_selectsoftkeys(d, l->instance, subline->callid, KEYDEF_SLACONNECTEDNOTACTIVE, KEYMASK_ALL);
03491             send_displaypromptstatus(d, "In Use", "", 0, l->instance, subline->callid);
03492          }
03493          transmit_lamp_indication(d, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
03494          transmit_ringer_mode(d, SKINNY_RING_OFF);
03495          transmit_activatecallplane(d, l);
03496          break;
03497       case AST_EXTENSION_ONHOLD:
03498          transmit_callstate(d, l->instance, subline->callid, SKINNY_HOLD);
03499          transmit_selectsoftkeys(d, l->instance, subline->callid, KEYDEF_SLAHOLD, KEYMASK_ALL);
03500          send_displaypromptstatus(d, "Hold", "", 0, l->instance, subline->callid);
03501          transmit_lamp_indication(d, STIMULUS_LINE, l->instance, SKINNY_LAMP_BLINK);
03502          transmit_activatecallplane(d, l);
03503          break;
03504       case AST_EXTENSION_NOT_INUSE:
03505          transmit_callstate(d, l->instance, subline->callid, SKINNY_ONHOOK);
03506          transmit_selectsoftkeys(d, l->instance, subline->callid, KEYDEF_ONHOOK, KEYMASK_ALL);
03507          transmit_clearpromptmessage(d, l->instance, subline->callid);
03508          transmit_lamp_indication(d, STIMULUS_LINE, l->instance, SKINNY_LAMP_OFF);
03509          transmit_activatecallplane(d, l);
03510          subline->callid = 0;
03511          break;
03512       default:
03513          ast_log(LOG_WARNING, "AST_EXTENSION_STATE %s not configured\n", ast_extension_state2str(state));
03514       }
03515    } else {
03516       ast_log(LOG_WARNING, "Invalid data supplied to skinny_extensionstate_cb\n");
03517    }
03518 
03519    return 0;
03520 }
03521 
03522 static void update_connectedline(struct skinny_subchannel *sub, const void *data, size_t datalen)
03523 {
03524    struct ast_channel *c = sub->owner;
03525    struct skinny_line *l = sub->line;
03526    struct skinny_device *d = l->device;
03527 
03528    if (!d->session) {
03529       return;
03530    }
03531 
03532    if (sub->calldirection == SKINNY_OUTGOING && !sub->origtonum) {
03533       /* Do not set origtonum before here or origtoname won't be set */
03534       sub->origtonum = ast_strdup(sub->exten);
03535       if (ast_channel_connected(c)->id.name.valid) {
03536          sub->origtoname = ast_strdup(ast_channel_connected(c)->id.name.str);
03537       }
03538    }
03539 
03540    if (!ast_channel_caller(c)->id.number.valid
03541       || ast_strlen_zero(ast_channel_caller(c)->id.number.str)
03542       || !ast_channel_connected(c)->id.number.valid
03543       || ast_strlen_zero(ast_channel_connected(c)->id.number.str))
03544       return;
03545 
03546    SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Updating\n", sub->callid);
03547 
03548    send_callinfo(sub);
03549 }
03550 
03551 static void mwi_event_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg)
03552 {
03553    struct skinny_line *l = userdata;
03554    struct skinny_device *d = l->device;
03555    struct skinny_line *l2;
03556    int dev_msgs = 0;
03557 
03558    if (!d || !d->session) {
03559       return;
03560    }
03561 
03562    if (msg && ast_mwi_state_type() == stasis_message_type(msg)) {
03563       struct ast_mwi_state *mwi_state = stasis_message_data(msg);
03564       l->newmsgs = mwi_state->new_msgs;
03565    }
03566 
03567    if (l->newmsgs) {
03568       transmit_lamp_indication(d, STIMULUS_VOICEMAIL, l->instance, l->mwiblink?SKINNY_LAMP_BLINK:SKINNY_LAMP_ON);
03569    } else {
03570       transmit_lamp_indication(d, STIMULUS_VOICEMAIL, l->instance, SKINNY_LAMP_OFF);
03571    }
03572 
03573    /* find out wether the device lamp should be on or off */
03574    AST_LIST_TRAVERSE(&d->lines, l2, list) {
03575       if (l2->newmsgs) {
03576          dev_msgs++;
03577       }
03578    }
03579 
03580    if (dev_msgs) {
03581       transmit_lamp_indication(d, STIMULUS_VOICEMAIL, 0, d->mwiblink?SKINNY_LAMP_BLINK:SKINNY_LAMP_ON);
03582    } else {
03583       transmit_lamp_indication(d, STIMULUS_VOICEMAIL, 0, SKINNY_LAMP_OFF);
03584    }
03585    ast_verb(3, "Skinny mwi_event_cb found %d new messages\n", l->newmsgs);
03586 }
03587 
03588 /* I do not believe skinny can deal with video.
03589    Anyone know differently? */
03590 /* Yes, it can.  Currently 7985 and Cisco VT Advantage do video. */
03591 static enum ast_rtp_glue_result skinny_get_vrtp_peer(struct ast_channel *c, struct ast_rtp_instance **instance)
03592 {
03593    struct skinny_subchannel *sub = NULL;
03594 
03595    if (!(sub = ast_channel_tech_pvt(c)) || !(sub->vrtp))
03596       return AST_RTP_GLUE_RESULT_FORBID;
03597 
03598    ao2_ref(sub->vrtp, +1);
03599    *instance = sub->vrtp;
03600 
03601    return AST_RTP_GLUE_RESULT_REMOTE;
03602 }
03603 
03604 static enum ast_rtp_glue_result skinny_get_rtp_peer(struct ast_channel *c, struct ast_rtp_instance **instance)
03605 {
03606    struct skinny_subchannel *sub = NULL;
03607    struct skinny_line *l;
03608    enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_REMOTE;
03609 
03610    SKINNY_DEBUG(DEBUG_AUDIO, 4, "skinny_get_rtp_peer() Channel = %s\n", ast_channel_name(c));
03611 
03612    if (!(sub = ast_channel_tech_pvt(c)))
03613       return AST_RTP_GLUE_RESULT_FORBID;
03614 
03615    skinny_locksub(sub);
03616 
03617    if (!(sub->rtp)){
03618       skinny_unlocksub(sub);
03619       return AST_RTP_GLUE_RESULT_FORBID;
03620    }
03621 
03622    ao2_ref(sub->rtp, +1);
03623    *instance = sub->rtp;
03624 
03625    l = sub->line;
03626 
03627    if (!l->directmedia || l->nat){
03628       res = AST_RTP_GLUE_RESULT_LOCAL;
03629       SKINNY_DEBUG(DEBUG_AUDIO, 4, "skinny_get_rtp_peer() Using AST_RTP_GLUE_RESULT_LOCAL \n");
03630    }
03631 
03632    skinny_unlocksub(sub);
03633 
03634    return res;
03635 
03636 }
03637 
03638 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)
03639 {
03640    struct skinny_subchannel *sub;
03641    struct skinny_line *l;
03642    struct skinny_device *d;
03643    struct sockaddr_in us = { 0, };
03644    struct sockaddr_in them = { 0, };
03645    struct ast_sockaddr them_tmp;
03646    struct ast_sockaddr us_tmp;
03647 
03648    sub = ast_channel_tech_pvt(c);
03649 
03650    if (ast_channel_state(c) != AST_STATE_UP)
03651       return 0;
03652 
03653    if (!sub) {
03654       return -1;
03655    }
03656 
03657    l = sub->line;
03658    d = l->device;
03659 
03660    if (rtp){
03661       struct ast_format *tmpfmt;
03662       unsigned int framing;
03663       ast_rtp_instance_get_remote_address(rtp, &them_tmp);
03664       ast_sockaddr_to_sin(&them_tmp, &them);
03665 
03666       /* Shutdown any early-media or previous media on re-invite */
03667       transmit_stopmediatransmission(d, sub);
03668 
03669       SKINNY_DEBUG(DEBUG_AUDIO, 4, "Peerip = %s:%d\n", ast_inet_ntoa(them.sin_addr), ntohs(them.sin_port));
03670 
03671       tmpfmt = ast_format_cap_get_format(l->cap, 0);
03672       framing = ast_format_cap_get_format_framing(l->cap, tmpfmt);
03673 
03674       SKINNY_DEBUG(DEBUG_AUDIO, 4, "Setting payloadType to '%s' (%u ms)\n", ast_format_get_name(tmpfmt), framing);
03675 
03676       if (!(l->directmedia) || (l->nat)){
03677          ast_rtp_instance_get_local_address(rtp, &us_tmp);
03678          ast_sockaddr_to_sin(&us_tmp, &us);
03679          us.sin_addr.s_addr = us.sin_addr.s_addr ? us.sin_addr.s_addr : d->ourip.s_addr;
03680          transmit_startmediatransmission(d, sub, us, tmpfmt, framing);
03681       } else {
03682          transmit_startmediatransmission(d, sub, them, tmpfmt, framing);
03683       }
03684 
03685       ao2_ref(tmpfmt, -1);
03686 
03687       return 0;
03688    }
03689    /* Need a return here to break the bridge */
03690    return 0;
03691 }
03692 
03693 static struct ast_rtp_glue skinny_rtp_glue = {
03694    .type = "Skinny",
03695    .get_rtp_info = skinny_get_rtp_peer,
03696    .get_vrtp_info = skinny_get_vrtp_peer,
03697    .update_peer = skinny_set_rtp_peer,
03698 };
03699 
03700 #ifdef AST_DEVMODE
03701 static char *skinny_debugs(void)
03702 {
03703    char *ptr;
03704    int posn = 0;
03705 
03706    ptr = dbgcli_buf;
03707    strncpy(ptr, "\0", 1);
03708    if (skinnydebug & DEBUG_GENERAL) {
03709       strncpy(ptr, "general ", 8);
03710       posn += 8;
03711       ptr += 8;
03712    }
03713    if (skinnydebug & DEBUG_SUB) {
03714       strncpy(ptr, "sub ", 4);
03715       posn += 4;
03716       ptr += 4;
03717    }
03718    if (skinnydebug & DEBUG_AUDIO) {
03719       strncpy(ptr, "audio ", 6);
03720       posn += 6;
03721       ptr += 6;
03722    }
03723    if (skinnydebug & DEBUG_PACKET) {
03724       strncpy(ptr, "packet ", 7);
03725       posn += 7;
03726       ptr += 7;
03727    }
03728    if (skinnydebug & DEBUG_LOCK) {
03729       strncpy(ptr, "lock ", 5);
03730       posn += 5;
03731       ptr += 5;
03732    }
03733    if (skinnydebug & DEBUG_TEMPLATE) {
03734       strncpy(ptr, "template ", 9);
03735       posn += 9;
03736       ptr += 9;
03737    }
03738    if (skinnydebug & DEBUG_THREAD) {
03739       strncpy(ptr, "thread ", 7);
03740       posn += 7;
03741       ptr += 7;
03742    }
03743    if (skinnydebug & DEBUG_HINT) {
03744       strncpy(ptr, "hint ", 5);
03745       posn += 5;
03746       ptr += 5;
03747    }
03748    if (skinnydebug & DEBUG_KEEPALIVE) {
03749       strncpy(ptr, "keepalive ", 10);
03750       posn += 10;
03751       ptr += 10;
03752    }
03753    if (posn > 0) {
03754       strncpy(--ptr, "\0", 1);
03755    }
03756    return dbgcli_buf;
03757 }
03758 
03759 static char *complete_skinny_debug(const char *line, const char *word, int pos, int state)
03760 {
03761    const char *debugOpts[]={ "all","audio","hint","keepalive","lock","off","packet","show","sub","template","thread",NULL };
03762    char *wordptr = (char *)word;
03763    char buf[32];
03764    char *bufptr = buf;
03765    int buflen = sizeof(buf);
03766    int wordlen;
03767    int which = 0;
03768    int i = 0;
03769 
03770    if (*word == '+' || *word == '-' || *word == '!') {
03771       *bufptr = *word;
03772       wordptr++;
03773       bufptr++;
03774       buflen--;
03775    }
03776    wordlen = strlen(wordptr);
03777 
03778    while (debugOpts[i]) {
03779       if (!strncasecmp(wordptr, debugOpts[i], wordlen) && ++which > state) {
03780          ast_copy_string(bufptr, debugOpts[i], buflen);
03781          return ast_strdup(buf);
03782       }
03783       i++;
03784    }
03785 
03786    return NULL;
03787 }
03788 
03789 static char *handle_skinny_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
03790 {
03791    int i;
03792    int result = 0;
03793    const char *arg;
03794    int bitmask;
03795    int negate;
03796 
03797    switch (cmd) {
03798    case CLI_INIT:
03799       e->command = "skinny debug";
03800       e->usage =
03801          "Usage: skinny debug {audio|hint|keepalive|lock|off|packet|show|sub|template|thread}\n"
03802          "       Enables/Disables various Skinny debugging messages\n";
03803       return NULL;
03804    case CLI_GENERATE:
03805       return complete_skinny_debug(a->line, a->word, a->pos, a->n);
03806 
03807    }
03808 
03809    if (a->argc < 3)
03810       return CLI_SHOWUSAGE;
03811 
03812    if (a->argc == 3 && !strncasecmp(a->argv[e->args-1], "show", 4)) {
03813       ast_cli(a->fd, "Skinny Debugging - %s\n", skinny_debugs());
03814       return CLI_SUCCESS;
03815    }
03816 
03817    for(i = e->args; i < a->argc; i++) {
03818       result++;
03819       arg = a->argv[i];
03820 
03821       if (!strncasecmp(arg, "off", 3)) {
03822          skinnydebug = 0;
03823          continue;
03824       }
03825 
03826       if (!strncasecmp(arg, "all", 3)) {
03827          skinnydebug = DEBUG_GENERAL|DEBUG_SUB|DEBUG_PACKET|DEBUG_AUDIO|DEBUG_LOCK|DEBUG_TEMPLATE|DEBUG_THREAD|DEBUG_HINT|DEBUG_KEEPALIVE;
03828          continue;
03829       }
03830 
03831       if (!strncasecmp(arg, "-", 1) || !strncasecmp(arg, "!", 1)) {
03832          negate = 1;
03833          arg++;
03834       } else if (!strncasecmp(arg, "+", 1)) {
03835          negate = 0;
03836          arg++;
03837       } else {
03838          negate = 0;
03839       }
03840 
03841       if (!strncasecmp(arg, "general", 7)) {
03842          bitmask = DEBUG_GENERAL;
03843       } else if (!strncasecmp(arg, "sub", 3)) {
03844          bitmask = DEBUG_SUB;
03845       } else if (!strncasecmp(arg, "packet", 6)) {
03846          bitmask = DEBUG_PACKET;
03847       } else if (!strncasecmp(arg, "audio", 5)) {
03848          bitmask = DEBUG_AUDIO;
03849       } else if (!strncasecmp(arg, "lock", 4)) {
03850          bitmask = DEBUG_LOCK;
03851       } else if (!strncasecmp(arg, "template", 8)) {
03852          bitmask = DEBUG_TEMPLATE;
03853       } else if (!strncasecmp(arg, "thread", 6)) {
03854          bitmask = DEBUG_THREAD;
03855       } else if (!strncasecmp(arg, "hint", 4)) {
03856          bitmask = DEBUG_HINT;
03857       } else if (!strncasecmp(arg, "keepalive", 9)) {
03858          bitmask = DEBUG_KEEPALIVE;
03859       } else {
03860          ast_cli(a->fd, "Skinny Debugging - option '%s' unknown\n", a->argv[i]);
03861          result--;
03862          continue;
03863       }
03864 
03865       if (negate) {
03866          skinnydebug &= ~bitmask;
03867       } else {
03868          skinnydebug |= bitmask;
03869       }
03870    }
03871    if (result) {
03872       ast_cli(a->fd, "Skinny Debugging - %s\n", skinnydebug ? skinny_debugs() : "off");
03873       return CLI_SUCCESS;
03874    } else {
03875       return CLI_SHOWUSAGE;
03876    }
03877 }
03878 #endif
03879 
03880 static char *handle_skinny_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
03881 {
03882    switch (cmd) {
03883    case CLI_INIT:
03884       e->command = "skinny reload";
03885       e->usage =
03886          "Usage: skinny reload\n"
03887          "       Reloads the chan_skinny configuration\n";
03888       return NULL;
03889    case CLI_GENERATE:
03890       return NULL;
03891    }
03892 
03893    if (a->argc != e->args)
03894       return CLI_SHOWUSAGE;
03895 
03896    skinny_reload();
03897    return CLI_SUCCESS;
03898 
03899 }
03900 
03901 static char *complete_skinny_devices(const char *word, int state)
03902 {
03903    struct skinny_device *d;
03904    int wordlen = strlen(word), which = 0;
03905 
03906    AST_LIST_TRAVERSE(&devices, d, list) {
03907       if (!strncasecmp(word, d->name, wordlen) && ++which > state) {
03908                        return ast_strdup(d->name);
03909                }
03910    }
03911 
03912    return NULL;
03913 }
03914 
03915 static char *complete_skinny_show_device(const char *line, const char *word, int pos, int state)
03916 {
03917    return (pos == 3 ? complete_skinny_devices(word, state) : NULL);
03918 }
03919 
03920 static char *complete_skinny_reset(const char *line, const char *word, int pos, int state)
03921 {
03922    return (pos == 2 ? complete_skinny_devices(word, state) : NULL);
03923 }
03924 
03925 static char *complete_skinny_show_line(const char *line, const char *word, int pos, int state)
03926 {
03927    struct skinny_device *d;
03928    struct skinny_line *l;
03929    int wordlen = strlen(word), which = 0;
03930 
03931    if (pos != 3)
03932       return NULL;
03933 
03934    AST_LIST_TRAVERSE(&devices, d, list) {
03935       AST_LIST_TRAVERSE(&d->lines, l, list) {
03936          if (!strncasecmp(word, l->name, wordlen) && ++which > state) {
03937             return ast_strdup(l->name);
03938          }
03939       }
03940    }
03941 
03942    return NULL;
03943 }
03944 
03945 static char *handle_skinny_reset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
03946 {
03947    struct skinny_device *d;
03948 
03949    switch (cmd) {
03950    case CLI_INIT:
03951       e->command = "skinny reset";
03952       e->usage =
03953          "Usage: skinny reset <DeviceId|DeviceName|all> [restart]\n"
03954          "       Causes a Skinny device to reset itself, optionally with a full restart\n";
03955       return NULL;
03956    case CLI_GENERATE:
03957       return complete_skinny_reset(a->line, a->word, a->pos, a->n);
03958    }
03959 
03960    if (a->argc < 3 || a->argc > 4)
03961       return CLI_SHOWUSAGE;
03962 
03963    AST_LIST_LOCK(&devices);
03964    AST_LIST_TRAVERSE(&devices, d, list) {
03965       int resetonly = 1;
03966       if (!strcasecmp(a->argv[2], d->id) || !strcasecmp(a->argv[2], d->name) || !strcasecmp(a->argv[2], "all")) {
03967          if (!(d->session))
03968             continue;
03969 
03970          if (a->argc == 4 && !strcasecmp(a->argv[3], "restart"))
03971             resetonly = 0;
03972 
03973          transmit_reset(d, resetonly);
03974       }
03975    }
03976    AST_LIST_UNLOCK(&devices);
03977    return CLI_SUCCESS;
03978 }
03979 
03980 static char *device2str(int type)
03981 {
03982    char *tmp;
03983 
03984    switch (type) {
03985    case SKINNY_DEVICE_NONE:
03986       return "No Device";
03987    case SKINNY_DEVICE_30SPPLUS:
03988       return "30SP Plus";
03989    case SKINNY_DEVICE_12SPPLUS:
03990       return "12SP Plus";
03991    case SKINNY_DEVICE_12SP:
03992       return "12SP";
03993    case SKINNY_DEVICE_12:
03994       return "12";
03995    case SKINNY_DEVICE_30VIP:
03996       return "30VIP";
03997    case SKINNY_DEVICE_7910:
03998       return "7910";
03999    case SKINNY_DEVICE_7960:
04000       return "7960";
04001    case SKINNY_DEVICE_7940:
04002       return "7940";
04003    case SKINNY_DEVICE_7935:
04004       return "7935";
04005    case SKINNY_DEVICE_ATA186:
04006       return "ATA186";
04007    case SKINNY_DEVICE_7941:
04008       return "7941";
04009    case SKINNY_DEVICE_7971:
04010       return "7971";
04011    case SKINNY_DEVICE_7914:
04012       return "7914";
04013    case SKINNY_DEVICE_7985:
04014       return "7985";
04015    case SKINNY_DEVICE_7911:
04016       return "7911";
04017    case SKINNY_DEVICE_7961GE:
04018       return "7961GE";
04019    case SKINNY_DEVICE_7941GE:
04020       return "7941GE";
04021    case SKINNY_DEVICE_7931:
04022       return "7931";
04023    case SKINNY_DEVICE_7921:
04024       return "7921";
04025    case SKINNY_DEVICE_7906:
04026       return "7906";
04027    case SKINNY_DEVICE_7962:
04028       return "7962";
04029    case SKINNY_DEVICE_7937:
04030       return "7937";
04031    case SKINNY_DEVICE_7942:
04032       return "7942";
04033    case SKINNY_DEVICE_7945:
04034       return "7945";
04035    case SKINNY_DEVICE_7965:
04036       return "7965";
04037    case SKINNY_DEVICE_7975:
04038       return "7975";
04039    case SKINNY_DEVICE_7905:
04040       return "7905";
04041    case SKINNY_DEVICE_7920:
04042       return "7920";
04043    case SKINNY_DEVICE_7970:
04044       return "7970";
04045    case SKINNY_DEVICE_7912:
04046       return "7912";
04047    case SKINNY_DEVICE_7902:
04048       return "7902";
04049    case SKINNY_DEVICE_CIPC:
04050       return "IP Communicator";
04051    case SKINNY_DEVICE_7961:
04052       return "7961";
04053    case SKINNY_DEVICE_7936:
04054       return "7936";
04055    case SKINNY_DEVICE_SCCPGATEWAY_AN:
04056       return "SCCPGATEWAY_AN";
04057    case SKINNY_DEVICE_SCCPGATEWAY_BRI:
04058       return "SCCPGATEWAY_BRI";
04059    case SKINNY_DEVICE_UNKNOWN:
04060       return "Unknown";
04061    default:
04062       if (!(tmp = ast_threadstorage_get(&device2str_threadbuf, DEVICE2STR_BUFSIZE)))
04063          return "Unknown";
04064       snprintf(tmp, DEVICE2STR_BUFSIZE, "UNKNOWN-%d", type);
04065       return tmp;
04066    }
04067 }
04068 
04069 static char *_skinny_show_devices(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char * const *argv)
04070 {
04071    struct skinny_device *d;
04072    struct skinny_line *l;
04073    const char *id;
04074    char idtext[256] = "";
04075    int total_devices = 0;
04076 
04077    if (s) { /* Manager - get ActionID */
04078       id = astman_get_header(m, "ActionID");
04079       if (!ast_strlen_zero(id))
04080          snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
04081    }
04082 
04083    switch (argc) {
04084    case 3:
04085       break;
04086    default:
04087       return CLI_SHOWUSAGE;
04088    }
04089 
04090    if (!s) {
04091       ast_cli(fd, "Name                 DeviceId         IP              Type            R NL\n");
04092       ast_cli(fd, "-------------------- ---------------- --------------- --------------- - --\n");
04093    }
04094 
04095    AST_LIST_LOCK(&devices);
04096    AST_LIST_TRAVERSE(&devices, d, list) {
04097       int numlines = 0;
04098       total_devices++;
04099       AST_LIST_TRAVERSE(&d->lines, l, list) {
04100          numlines++;
04101       }
04102       if (!s) {
04103          ast_cli(fd, "%-20s %-16s %-15s %-15s %c %2d\n",
04104             d->name,
04105             d->id,
04106             d->session?ast_inet_ntoa(d->session->sin.sin_addr):"",
04107             device2str(d->type),
04108             d->session?'Y':'N',
04109             numlines);
04110       } else {
04111          astman_append(s,
04112             "Event: DeviceEntry\r\n%s"
04113             "Channeltype: SKINNY\r\n"
04114             "ObjectName: %s\r\n"
04115             "ChannelObjectType: device\r\n"
04116             "DeviceId: %s\r\n"
04117             "IPaddress: %s\r\n"
04118             "Type: %s\r\n"
04119             "Devicestatus: %s\r\n"
04120             "NumberOfLines: %d\r\n",
04121             idtext,
04122             d->name,
04123             d->id,
04124             d->session?ast_inet_ntoa(d->session->sin.sin_addr):"-none-",
04125             device2str(d->type),
04126             d->session?"registered":"unregistered",
04127             numlines);
04128       }
04129    }
04130    AST_LIST_UNLOCK(&devices);
04131 
04132    if (total)
04133       *total = total_devices;
04134 
04135    return CLI_SUCCESS;
04136 }
04137 
04138 /*! \brief  Show SKINNY devices in the manager API */
04139 /*    Inspired from chan_sip */
04140 static int manager_skinny_show_devices(struct mansession *s, const struct message *m)
04141 {
04142    const char *a[] = {"skinny", "show", "devices"};
04143    int total = 0;
04144 
04145    astman_send_listack(s, m, "Device status list will follow", "start");
04146 
04147    /* List the devices in separate manager events */
04148    _skinny_show_devices(-1, &total, s, m, 3, a);
04149 
04150    /* Send final confirmation */
04151    astman_send_list_complete_start(s, m, "DevicelistComplete", total);
04152    astman_send_list_complete_end(s);
04153    return 0;
04154 }
04155 
04156 static char *handle_skinny_show_devices(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04157 {
04158 
04159    switch (cmd) {
04160    case CLI_INIT:
04161       e->command = "skinny show devices";
04162       e->usage =
04163          "Usage: skinny show devices\n"
04164          "       Lists all devices known to the Skinny subsystem.\n";
04165       return NULL;
04166    case CLI_GENERATE:
04167       return NULL;
04168    }
04169 
04170    return _skinny_show_devices(a->fd, NULL, NULL, NULL, a->argc, a->argv);
04171 }
04172 
04173 static char *_skinny_show_device(int type, int fd, struct mansession *s, const struct message *m, int argc, const char * const *argv)
04174 {
04175    struct skinny_device *d;
04176    struct skinny_line *l;
04177    struct skinny_speeddial *sd;
04178    struct skinny_addon *sa;
04179    struct skinny_serviceurl *surl;
04180    struct ast_str *codec_buf = ast_str_alloca(64);
04181 
04182    if (argc < 4) {
04183       return CLI_SHOWUSAGE;
04184    }
04185 
04186    AST_LIST_LOCK(&devices);
04187    AST_LIST_TRAVERSE(&devices, d, list) {
04188       if (!strcasecmp(argv[3], d->id) || !strcasecmp(argv[3], d->name)) {
04189          int numlines = 0, numaddons = 0, numspeeddials = 0, numserviceurls = 0;
04190 
04191          AST_LIST_TRAVERSE(&d->lines, l, list){
04192             numlines++;
04193          }
04194 
04195          AST_LIST_TRAVERSE(&d->addons, sa, list) {
04196             numaddons++;
04197          }
04198 
04199          AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
04200             numspeeddials++;
04201          }
04202 
04203          AST_LIST_TRAVERSE(&d->serviceurls, surl, list) {
04204             numserviceurls++;
04205          }
04206 
04207          if (type == 0) { /* CLI */
04208             ast_cli(fd, "Name:        %s\n", d->name);
04209             ast_cli(fd, "Id:          %s\n", d->id);
04210             ast_cli(fd, "version:     %s\n", S_OR(d->version_id, "Unknown"));
04211             ast_cli(fd, "Ip address:  %s\n", (d->session ? ast_inet_ntoa(d->session->sin.sin_addr) : "Unknown"));
04212             ast_cli(fd, "Port:        %d\n", (d->session ? ntohs(d->session->sin.sin_port) : 0));
04213             ast_cli(fd, "Device Type: %s\n", device2str(d->type));
04214             ast_cli(fd, "Conf Codecs: %s\n", ast_format_cap_get_names(d->confcap, &codec_buf));
04215             ast_cli(fd, "Neg Codecs: %s\n", ast_format_cap_get_names(d->cap, &codec_buf));
04216             ast_cli(fd, "Registered:  %s\n", (d->session ? "Yes" : "No"));
04217             ast_cli(fd, "Lines:       %d\n", numlines);
04218             AST_LIST_TRAVERSE(&d->lines, l, list) {
04219                ast_cli(fd, "  %s (%s)\n", l->name, l->label);
04220             }
04221             ast_cli(fd, "Addons:      %d\n", numaddons);
04222             AST_LIST_TRAVERSE(&d->addons, sa, list) {
04223                ast_cli(fd, "  %s\n", sa->type);
04224             }
04225             ast_cli(fd, "Speeddials:  %d\n", numspeeddials);
04226             AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
04227                ast_cli(fd, "  %s (%s) ishint: %d\n", sd->exten, sd->label, sd->isHint);
04228             }
04229             ast_cli(fd, "ServiceURLs:  %d\n", numserviceurls);
04230             AST_LIST_TRAVERSE(&d->serviceurls, surl, list) {
04231                ast_cli(fd, "  %s (%s)\n", surl->displayName, surl->url);
04232             }
04233          } else { /* manager */
04234             astman_append(s, "Channeltype: SKINNY\r\n");
04235             astman_append(s, "ObjectName: %s\r\n", d->name);
04236             astman_append(s, "ChannelObjectType: device\r\n");
04237             astman_append(s, "Id: %s\r\n", d->id);
04238             astman_append(s, "version: %s\r\n", S_OR(d->version_id, "Unknown"));
04239             astman_append(s, "Ipaddress: %s\r\n", (d->session ? ast_inet_ntoa(d->session->sin.sin_addr) : "Unknown"));
04240             astman_append(s, "Port: %d\r\n", (d->session ? ntohs(d->session->sin.sin_port) : 0));
04241             astman_append(s, "DeviceType: %s\r\n", device2str(d->type));
04242             astman_append(s, "Codecs: %s\r\n", ast_format_cap_get_names(d->confcap, &codec_buf));
04243             astman_append(s, "CodecOrder: %s\r\n", ast_format_cap_get_names(d->cap, &codec_buf));
04244             astman_append(s, "Devicestatus: %s\r\n", (d->session?"registered":"unregistered"));
04245             astman_append(s, "NumberOfLines: %d\r\n", numlines);
04246             AST_LIST_TRAVERSE(&d->lines, l, list) {
04247                astman_append(s, "Line: %s (%s)\r\n", l->name, l->label);
04248             }
04249             astman_append(s, "NumberOfAddons: %d\r\n", numaddons);
04250             AST_LIST_TRAVERSE(&d->addons, sa, list) {
04251                astman_append(s, "Addon: %s\r\n", sa->type);
04252             }
04253             astman_append(s, "NumberOfSpeeddials: %d\r\n", numspeeddials);
04254             AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
04255                astman_append(s, "Speeddial: %s (%s) ishint: %d\r\n", sd->exten, sd->label, sd->isHint);
04256             }
04257             astman_append(s, "ServiceURLs:  %d\r\n", numserviceurls);
04258             AST_LIST_TRAVERSE(&d->serviceurls, surl, list) {
04259                astman_append(s, "  %s (%s)\r\n", surl->displayName, surl->url);
04260             }
04261          }
04262       }
04263    }
04264    AST_LIST_UNLOCK(&devices);
04265    return CLI_SUCCESS;
04266 }
04267 
04268 static int manager_skinny_show_device(struct mansession *s, const struct message *m)
04269 {
04270    const char *a[4];
04271    const char *device;
04272 
04273    device = astman_get_header(m, "Device");
04274    if (ast_strlen_zero(device)) {
04275       astman_send_error(s, m, "Device: <name> missing.");
04276       return 0;
04277    }
04278    a[0] = "skinny";
04279    a[1] = "show";
04280    a[2] = "device";
04281    a[3] = device;
04282 
04283    _skinny_show_device(1, -1, s, m, 4, a);
04284    astman_append(s, "\r\n\r\n" );
04285    return 0;
04286 }
04287 
04288 /*! \brief Show device information */
04289 static char *handle_skinny_show_device(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04290 {
04291    switch (cmd) {
04292    case CLI_INIT:
04293       e->command = "skinny show device";
04294       e->usage =
04295          "Usage: skinny show device <DeviceId|DeviceName>\n"
04296          "       Lists all deviceinformation of a specific device known to the Skinny subsystem.\n";
04297       return NULL;
04298    case CLI_GENERATE:
04299       return complete_skinny_show_device(a->line, a->word, a->pos, a->n);
04300    }
04301 
04302    return _skinny_show_device(0, a->fd, NULL, NULL, a->argc, a->argv);
04303 }
04304 
04305 static char *_skinny_show_lines(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char * const *argv)
04306 {
04307    struct skinny_line *l;
04308    struct skinny_subchannel *sub;
04309    int total_lines = 0;
04310    int verbose = 0;
04311    const char *id;
04312    char idtext[256] = "";
04313 
04314    if (s) { /* Manager - get ActionID */
04315       id = astman_get_header(m, "ActionID");
04316       if (!ast_strlen_zero(id))
04317          snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
04318    }
04319 
04320    switch (argc) {
04321    case 4:
04322       verbose = 1;
04323       break;
04324    case 3:
04325       verbose = 0;
04326       break;
04327    default:
04328       return CLI_SHOWUSAGE;
04329    }
04330 
04331    if (!s) {
04332       ast_cli(fd, "Name                 Device Name          Instance Label               \n");
04333       ast_cli(fd, "-------------------- -------------------- -------- --------------------\n");
04334    }
04335    AST_LIST_LOCK(&lines);
04336    AST_LIST_TRAVERSE(&lines, l, all) {
04337       total_lines++;
04338       if (!s) {
04339          ast_cli(fd, "%-20s %-20s %8d %-20s\n",
04340             l->name,
04341             (l->device ? l->device->name : "Not connected"),
04342             l->instance,
04343             l->label);
04344          if (verbose) {
04345             AST_LIST_TRAVERSE(&l->sub, sub, list) {
04346                RAII_VAR(struct ast_channel *, bridged, ast_channel_bridge_peer(sub->owner), ao2_cleanup);
04347 
04348                ast_cli(fd, "  %s> %s to %s\n",
04349                   (sub == l->activesub?"Active  ":"Inactive"),
04350                   ast_channel_name(sub->owner),
04351                   bridged ? ast_channel_name(bridged) : ""
04352                );
04353             }
04354          }
04355       } else {
04356          astman_append(s,
04357             "Event: LineEntry\r\n%s"
04358             "Channeltype: SKINNY\r\n"
04359             "ObjectName: %s\r\n"
04360             "ChannelObjectType: line\r\n"
04361             "Device: %s\r\n"
04362             "Instance: %d\r\n"
04363             "Label: %s\r\n",
04364             idtext,
04365             l->name,
04366             (l->device ? l->device->name : "None"),
04367             l->instance,
04368             l->label);
04369       }
04370    }
04371    AST_LIST_UNLOCK(&lines);
04372 
04373    if (total) {
04374       *total = total_lines;
04375    }
04376 
04377    return CLI_SUCCESS;
04378 }
04379 
04380 /*! \brief  Show Skinny lines in the manager API */
04381 /*    Inspired from chan_sip */
04382 static int manager_skinny_show_lines(struct mansession *s, const struct message *m)
04383 {
04384    const char *a[] = {"skinny", "show", "lines"};
04385    int total = 0;
04386 
04387    astman_send_listack(s, m, "Line status list will follow", "start");
04388 
04389    /* List the lines in separate manager events */
04390    _skinny_show_lines(-1, &total, s, m, 3, a);
04391 
04392    /* Send final confirmation */
04393    astman_send_list_complete_start(s, m, "LinelistComplete", total);
04394    astman_send_list_complete_end(s);
04395    return 0;
04396 }
04397 
04398 static char *handle_skinny_show_lines(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04399 {
04400    switch (cmd) {
04401    case CLI_INIT:
04402       e->command = "skinny show lines [verbose]";
04403       e->usage =
04404          "Usage: skinny show lines\n"
04405          "       Lists all lines known to the Skinny subsystem.\n"
04406          "       If 'verbose' is specified, the output includes\n"
04407          "       information about subs for each line.\n";
04408       return NULL;
04409    case CLI_GENERATE:
04410       return NULL;
04411    }
04412 
04413    if (a->argc == e->args) {
04414       if (strcasecmp(a->argv[e->args-1], "verbose")) {
04415          return CLI_SHOWUSAGE;
04416       }
04417    } else if (a->argc != e->args - 1) {
04418       return CLI_SHOWUSAGE;
04419    }
04420 
04421    return _skinny_show_lines(a->fd, NULL, NULL, NULL, a->argc, a->argv);
04422 }
04423 
04424 static char *_skinny_show_line(int type, int fd, struct mansession *s, const struct message *m, int argc, const char * const *argv)
04425 {
04426    struct skinny_device *d;
04427    struct skinny_line *l;
04428    struct skinny_subline *subline;
04429    struct ast_str *codec_buf = ast_str_alloca(64);
04430    char group_buf[256];
04431    char cbuf[256];
04432 
04433    switch (argc) {
04434    case 4:
04435       break;
04436    case 6:
04437       break;
04438    default:
04439       return CLI_SHOWUSAGE;
04440    }
04441 
04442    AST_LIST_LOCK(&devices);
04443 
04444    /* Show all lines matching the one supplied */
04445    AST_LIST_TRAVERSE(&devices, d, list) {
04446       if (argc == 6 && (strcasecmp(argv[5], d->id) && strcasecmp(argv[5], d->name))) {
04447          continue;
04448       }
04449       AST_LIST_TRAVERSE(&d->lines, l, list) {
04450          struct ast_str *tmp_str = ast_str_alloca(512);
04451 
04452          if (strcasecmp(argv[3], l->name)) {
04453             continue;
04454          }
04455          if (type == 0) { /* CLI */
04456             ast_cli(fd, "Line:             %s\n", l->name);
04457             ast_cli(fd, "On Device:        %s\n", d->name);
04458             ast_cli(fd, "Line Label:       %s\n", l->label);
04459             ast_cli(fd, "Extension:        %s\n", S_OR(l->exten, "<not set>"));
04460             ast_cli(fd, "Context:          %s\n", l->context);
04461             ast_cli(fd, "CallGroup:        %s\n", ast_print_group(group_buf, sizeof(group_buf), l->callgroup));
04462             ast_cli(fd, "PickupGroup:      %s\n", ast_print_group(group_buf, sizeof(group_buf), l->pickupgroup));
04463             ast_cli(fd, "NamedCallGroup:   %s\n", ast_print_namedgroups(&tmp_str, l->named_callgroups));
04464             ast_str_reset(tmp_str);
04465             ast_cli(fd, "NamedPickupGroup: %s\n", ast_print_namedgroups(&tmp_str, l->named_pickupgroups));
04466             ast_str_reset(tmp_str);
04467             ast_cli(fd, "Language:         %s\n", S_OR(l->language, "<not set>"));
04468             ast_cli(fd, "Accountcode:      %s\n", S_OR(l->accountcode, "<not set>"));
04469             ast_cli(fd, "AmaFlag:          %s\n", ast_channel_amaflags2string(l->amaflags));
04470             ast_cli(fd, "CallerId Number:  %s\n", S_OR(l->cid_num, "<not set>"));
04471             ast_cli(fd, "CallerId Name:    %s\n", S_OR(l->cid_name, "<not set>"));
04472             ast_cli(fd, "Hide CallerId:    %s\n", (l->hidecallerid ? "Yes" : "No"));
04473             ast_cli(fd, "CFwdAll:          %s\n", S_COR((l->cfwdtype & SKINNY_CFWD_ALL), l->call_forward_all, "<not set>"));
04474             ast_cli(fd, "CFwdBusy:         %s\n", S_COR((l->cfwdtype & SKINNY_CFWD_BUSY), l->call_forward_busy, "<not set>"));
04475             ast_cli(fd, "CFwdNoAnswer:     %s\n", S_COR((l->cfwdtype & SKINNY_CFWD_NOANSWER), l->call_forward_noanswer, "<not set>"));
04476             ast_cli(fd, "CFwdTimeout:      %dms\n", l->callfwdtimeout);
04477             ast_cli(fd, "VoicemailBox:     %s\n", S_OR(l->mailbox, "<not set>"));
04478             ast_cli(fd, "VoicemailNumber:  %s\n", S_OR(l->vmexten, "<not set>"));
04479             ast_cli(fd, "MWIblink:         %d\n", l->mwiblink);
04480             ast_cli(fd, "Regextension:     %s\n", S_OR(l->regexten, "<not set>"));
04481             ast_cli(fd, "Regcontext:       %s\n", S_OR(l->regcontext, "<not set>"));
04482             ast_cli(fd, "MoHInterpret:     %s\n", S_OR(l->mohinterpret, "<not set>"));
04483             ast_cli(fd, "MoHSuggest:       %s\n", S_OR(l->mohsuggest, "<not set>"));
04484             ast_cli(fd, "Last dialed nr:   %s\n", S_OR(l->lastnumberdialed, "<no calls made yet>"));
04485             ast_cli(fd, "Last CallerID:    %s\n", S_OR(l->lastcallerid, "<not set>"));
04486             ast_cli(fd, "Transfer enabled: %s\n", (l->transfer ? "Yes" : "No"));
04487             ast_cli(fd, "Callwaiting:      %s\n", (l->callwaiting ? "Yes" : "No"));
04488             ast_cli(fd, "3Way Calling:     %s\n", (l->threewaycalling ? "Yes" : "No"));
04489             ast_cli(fd, "Can forward:      %s\n", (l->cancallforward ? "Yes" : "No"));
04490             ast_cli(fd, "Do Not Disturb:   %s\n", (l->dnd ? "Yes" : "No"));
04491             ast_cli(fd, "NAT:              %s\n", (l->nat ? "Yes" : "No"));
04492             ast_cli(fd, "immediate:        %s\n", (l->immediate ? "Yes" : "No"));
04493             ast_cli(fd, "Group:            %d\n", l->group);
04494             ast_cli(fd, "Parkinglot:       %s\n", S_OR(l->parkinglot, "<not set>"));
04495             ast_cli(fd, "Conf Codecs:      %s\n", ast_format_cap_get_names(l->confcap, &codec_buf));
04496             ast_cli(fd, "Neg Codecs:       %s\n", ast_format_cap_get_names(l->cap, &codec_buf));
04497             if  (AST_LIST_FIRST(&l->sublines)) {
04498                ast_cli(fd, "Sublines:\n");
04499                AST_LIST_TRAVERSE(&l->sublines, subline, list) {
04500                   ast_cli(fd, "     %s, %s@%s\n", subline->name, subline->exten, subline->context);
04501                }
04502             }
04503             ast_cli(fd, "\n");
04504          } else { /* manager */
04505             astman_append(s, "Channeltype: SKINNY\r\n");
04506             astman_append(s, "ObjectName: %s\r\n", l->name);
04507             astman_append(s, "ChannelObjectType: line\r\n");
04508             astman_append(s, "Device: %s\r\n", d->name);
04509             astman_append(s, "LineLabel: %s\r\n", l->label);
04510             astman_append(s, "Extension: %s\r\n", S_OR(l->exten, "<not set>"));
04511             astman_append(s, "Context: %s\r\n", l->context);
04512             astman_append(s, "CallGroup: %s\r\n", ast_print_group(group_buf, sizeof(group_buf), l->callgroup));
04513             astman_append(s, "PickupGroup: %s\r\n", ast_print_group(group_buf, sizeof(group_buf), l->pickupgroup));
04514             astman_append(s, "NamedCallGroup: %s\r\n", ast_print_namedgroups(&tmp_str, l->named_callgroups));
04515             ast_str_reset(tmp_str);
04516             astman_append(s, "NamedPickupGroup: %s\r\n", ast_print_namedgroups(&tmp_str, l->named_pickupgroups));
04517             ast_str_reset(tmp_str);
04518             astman_append(s, "Language: %s\r\n", S_OR(l->language, "<not set>"));
04519             astman_append(s, "Accountcode: %s\r\n", S_OR(l->accountcode, "<not set>"));
04520             astman_append(s, "AMAflags: %s\r\n", ast_channel_amaflags2string(l->amaflags));
04521             astman_append(s, "Callerid: %s\r\n", ast_callerid_merge(cbuf, sizeof(cbuf), l->cid_name, l->cid_num, ""));
04522             astman_append(s, "HideCallerId: %s\r\n", (l->hidecallerid ? "Yes" : "No"));
04523             astman_append(s, "CFwdAll: %s\r\n", S_COR((l->cfwdtype & SKINNY_CFWD_ALL), l->call_forward_all, "<not set>"));
04524             astman_append(s, "CFwdBusy: %s\r\n", S_COR((l->cfwdtype & SKINNY_CFWD_BUSY), l->call_forward_busy, "<not set>"));
04525             astman_append(s, "CFwdNoAnswer: %s\r\n", S_COR((l->cfwdtype & SKINNY_CFWD_NOANSWER), l->call_forward_noanswer, "<not set>"));
04526             astman_append(s, "VoicemailBox: %s\r\n", S_OR(l->mailbox, "<not set>"));
04527             astman_append(s, "VoicemailNumber: %s\r\n", S_OR(l->vmexten, "<not set>"));
04528             astman_append(s, "MWIblink: %d\r\n", l->mwiblink);
04529             astman_append(s, "RegExtension: %s\r\n", S_OR(l->regexten, "<not set>"));
04530             astman_append(s, "Regcontext: %s\r\n", S_OR(l->regcontext, "<not set>"));
04531             astman_append(s, "MoHInterpret: %s\r\n", S_OR(l->mohinterpret, "<not set>"));
04532             astman_append(s, "MoHSuggest: %s\r\n", S_OR(l->mohsuggest, "<not set>"));
04533             astman_append(s, "LastDialedNr: %s\r\n", S_OR(l->lastnumberdialed, "<no calls made yet>"));
04534             astman_append(s, "LastCallerID: %s\r\n", S_OR(l->lastcallerid, "<not set>"));
04535             astman_append(s, "Transfer: %s\r\n", (l->transfer ? "Yes" : "No"));
04536             astman_append(s, "Callwaiting: %s\r\n", (l->callwaiting ? "Yes" : "No"));
04537             astman_append(s, "3WayCalling: %s\r\n", (l->threewaycalling ? "Yes" : "No"));
04538             astman_append(s, "CanForward: %s\r\n", (l->cancallforward ? "Yes" : "No"));
04539             astman_append(s, "DoNotDisturb: %s\r\n", (l->dnd ? "Yes" : "No"));
04540             astman_append(s, "NAT: %s\r\n", (l->nat ? "Yes" : "No"));
04541             astman_append(s, "immediate: %s\r\n", (l->immediate ? "Yes" : "No"));
04542             astman_append(s, "Group: %d\r\n", l->group);
04543             astman_append(s, "Parkinglot: %s\r\n", S_OR(l->parkinglot, "<not set>"));
04544             astman_append(s, "Codecs: %s\r\n", ast_format_cap_get_names(l->confcap, &codec_buf));
04545             astman_append(s, "\r\n");
04546          }
04547       }
04548    }
04549 
04550    AST_LIST_UNLOCK(&devices);
04551    return CLI_SUCCESS;
04552 }
04553 
04554 static int manager_skinny_show_line(struct mansession *s, const struct message *m)
04555 {
04556    const char *a[4];
04557    const char *line;
04558 
04559    line = astman_get_header(m, "Line");
04560    if (ast_strlen_zero(line)) {
04561       astman_send_error(s, m, "Line: <name> missing.");
04562       return 0;
04563    }
04564    a[0] = "skinny";
04565    a[1] = "show";
04566    a[2] = "line";
04567    a[3] = line;
04568 
04569    _skinny_show_line(1, -1, s, m, 4, a);
04570    astman_append(s, "\r\n\r\n" );
04571    return 0;
04572 }
04573 
04574 /*! \brief List line information. */
04575 static char *handle_skinny_show_line(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04576 {
04577    switch (cmd) {
04578    case CLI_INIT:
04579       e->command = "skinny show line";
04580       e->usage =
04581          "Usage: skinny show line <Line> [ on <DeviceID|DeviceName> ]\n"
04582          "       List all lineinformation of a specific line known to the Skinny subsystem.\n";
04583       return NULL;
04584    case CLI_GENERATE:
04585       return complete_skinny_show_line(a->line, a->word, a->pos, a->n);
04586    }
04587 
04588    return _skinny_show_line(0, a->fd, NULL, NULL, a->argc, a->argv);
04589 }
04590 
04591 /*! \brief List global settings for the Skinny subsystem. */
04592 static char *handle_skinny_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04593 {
04594    char immed_str[2] = {immed_dialchar, '\0'};
04595 
04596    switch (cmd) {
04597    case CLI_INIT:
04598       e->command = "skinny show settings";
04599       e->usage =
04600          "Usage: skinny show settings\n"
04601          "       Lists all global configuration settings of the Skinny subsystem.\n";
04602       return NULL;
04603    case CLI_GENERATE:
04604       return NULL;
04605    }
04606 
04607    if (a->argc != 3)
04608       return CLI_SHOWUSAGE;
04609 
04610    ast_cli(a->fd, "\nGlobal Settings:\n");
04611    ast_cli(a->fd, "  Skinny Port:            %d\n", ntohs(bindaddr.sin_port));
04612    ast_cli(a->fd, "  Bindaddress:            %s\n", ast_inet_ntoa(bindaddr.sin_addr));
04613    ast_cli(a->fd, "  KeepAlive:              %d\n", keep_alive);
04614    ast_cli(a->fd, "  Date Format:            %s\n", date_format);
04615    ast_cli(a->fd, "  Voice Mail Extension:   %s\n", S_OR(vmexten, "(not set)"));
04616    ast_cli(a->fd, "  Reg. context:           %s\n", S_OR(regcontext, "(not set)"));
04617    ast_cli(a->fd, "  Immed. Dial Key:        %s\n", S_OR(immed_str, "(not set)"));
04618    ast_cli(a->fd, "  Jitterbuffer enabled:   %s\n", AST_CLI_YESNO(ast_test_flag(&global_jbconf, AST_JB_ENABLED)));
04619     if (ast_test_flag(&global_jbconf, AST_JB_ENABLED)) {
04620       ast_cli(a->fd, "  Jitterbuffer forced:    %s\n", AST_CLI_YESNO(ast_test_flag(&global_jbconf, AST_JB_FORCED)));
04621       ast_cli(a->fd, "  Jitterbuffer max size:  %ld\n", global_jbconf.max_size);
04622       ast_cli(a->fd, "  Jitterbuffer resync:    %ld\n", global_jbconf.resync_threshold);
04623       ast_cli(a->fd, "  Jitterbuffer impl:      %s\n", global_jbconf.impl);
04624       if (!strcasecmp(global_jbconf.impl, "adaptive")) {
04625          ast_cli(a->fd, "  Jitterbuffer tgt extra: %ld\n", global_jbconf.target_extra);
04626       }
04627       ast_cli(a->fd, "  Jitterbuffer log:       %s\n", AST_CLI_YESNO(ast_test_flag(&global_jbconf, AST_JB_LOG)));
04628    }
04629 
04630    return CLI_SUCCESS;
04631 }
04632 
04633 static char *_skinny_message_set(int type, int fd, struct mansession *s, const struct message *m, int argc, const char * const *argv)
04634 {
04635    struct skinny_device *d;
04636    char text_buf[32];
04637 
04638    if (argc < 7) {
04639       return CLI_SHOWUSAGE;
04640    }
04641 
04642    AST_LIST_LOCK(&devices);
04643    AST_LIST_TRAVERSE(&devices, d, list) {
04644       if (!strcasecmp(argv[3], d->name)) {
04645          int i;
04646          char *strp = text_buf;
04647          int charleft = sizeof(text_buf);
04648          int priority = atoi(argv[4]);
04649          int timeout = atoi(argv[5]);
04650          ast_copy_string(strp, argv[6], charleft);
04651          charleft -= strlen(strp);
04652          strp += strlen(strp);
04653          for(i=7; i<argc; i++) {
04654             ast_copy_string(strp++, " ", charleft--);
04655             ast_copy_string(strp, argv[i], charleft);
04656             charleft -= strlen(strp);
04657             strp += strlen(strp);
04658          }
04659          send_displayprinotify(d, text_buf, "", timeout, priority);
04660       }
04661    }
04662    AST_LIST_UNLOCK(&devices);
04663    return CLI_SUCCESS;
04664 }
04665 
04666 /*! \brief Handle sending messages to devices. */
04667 static char *handle_skinny_message_set(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04668 {
04669    switch (cmd) {
04670    case CLI_INIT:
04671       e->command = "skinny message set";
04672       e->usage =
04673          "Usage: skinny message set <device> <priority> <timeout> <message>\n"
04674          "       Set the current priority level message on a device.\n";
04675       return NULL;
04676    case CLI_GENERATE:
04677       return complete_skinny_show_device(a->line, a->word, a->pos, a->n);
04678    }
04679 
04680    return _skinny_message_set(0, a->fd, NULL, NULL, a->argc, a->argv);
04681 }
04682 
04683 static char *_skinny_message_clear(int type, int fd, struct mansession *s, const struct message *m, int argc, const char * const *argv)
04684 {
04685    struct skinny_device *d;
04686 
04687    if (argc != 5) {
04688       return CLI_SHOWUSAGE;
04689    }
04690 
04691    AST_LIST_LOCK(&devices);
04692    AST_LIST_TRAVERSE(&devices, d, list) {
04693       if (!strcasecmp(argv[3], d->name)) {
04694          int priority = atoi(argv[4]);
04695          transmit_clearprinotify(d, priority);
04696       }
04697    }
04698    AST_LIST_UNLOCK(&devices);
04699    return CLI_SUCCESS;
04700 }
04701 
04702 /*! \brief Handle clearing messages to devices. */
04703 static char *handle_skinny_message_clear(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04704 {
04705    switch (cmd) {
04706    case CLI_INIT:
04707       e->command = "skinny message clear";
04708       e->usage =
04709          "Usage: skinny message clear <device> <priority>\n"
04710          "       Clear the current priority level message on device.\n";
04711       return NULL;
04712    case CLI_GENERATE:
04713       return complete_skinny_show_device(a->line, a->word, a->pos, a->n);
04714    }
04715 
04716    return _skinny_message_clear(0, a->fd, NULL, NULL, a->argc, a->argv);
04717 }
04718 
04719 static struct ast_cli_entry cli_skinny[] = {
04720    AST_CLI_DEFINE(handle_skinny_show_devices, "List defined Skinny devices"),
04721    AST_CLI_DEFINE(handle_skinny_show_device, "List Skinny device information"),
04722    AST_CLI_DEFINE(handle_skinny_show_lines, "List defined Skinny lines per device"),
04723    AST_CLI_DEFINE(handle_skinny_show_line, "List Skinny line information"),
04724    AST_CLI_DEFINE(handle_skinny_show_settings, "List global Skinny settings"),
04725 #ifdef AST_DEVMODE
04726    AST_CLI_DEFINE(handle_skinny_set_debug, "Enable/Disable Skinny debugging"),
04727 #endif
04728    AST_CLI_DEFINE(handle_skinny_reset, "Reset Skinny device(s)"),
04729    AST_CLI_DEFINE(handle_skinny_reload, "Reload Skinny config"),
04730    AST_CLI_DEFINE(handle_skinny_message_set, "Send message to devices"),
04731    AST_CLI_DEFINE(handle_skinny_message_clear, "Clear message to devices"),
04732 };
04733 
04734 static void start_rtp(struct skinny_subchannel *sub)
04735 {
04736    struct skinny_line *l = sub->line;
04737    struct skinny_device *d = l->device;
04738    int hasvideo = 0;
04739    struct ast_sockaddr bindaddr_tmp;
04740 
04741    skinny_locksub(sub);
04742    SKINNY_DEBUG(DEBUG_AUDIO, 3, "Sub %u - Starting RTP\n", sub->callid);
04743    ast_sockaddr_from_sin(&bindaddr_tmp, &bindaddr);
04744    sub->rtp = ast_rtp_instance_new("asterisk", sched, &bindaddr_tmp, NULL);
04745    if (hasvideo)
04746       sub->vrtp = ast_rtp_instance_new("asterisk", sched, &bindaddr_tmp, NULL);
04747 
04748    if (sub->rtp) {
04749       ast_rtp_instance_set_prop(sub->rtp, AST_RTP_PROPERTY_RTCP, 1);
04750    }
04751    if (sub->vrtp) {
04752       ast_rtp_instance_set_prop(sub->vrtp, AST_RTP_PROPERTY_RTCP, 1);
04753    }
04754 
04755    if (sub->rtp && sub->owner) {
04756       ast_rtp_instance_set_channel_id(sub->rtp, ast_channel_uniqueid(sub->owner));
04757       ast_channel_set_fd(sub->owner, 0, ast_rtp_instance_fd(sub->rtp, 0));
04758       ast_channel_set_fd(sub->owner, 1, ast_rtp_instance_fd(sub->rtp, 1));
04759    }
04760    if (hasvideo && sub->vrtp && sub->owner) {
04761       ast_rtp_instance_set_channel_id(sub->vrtp, ast_channel_uniqueid(sub->owner));
04762       ast_channel_set_fd(sub->owner, 2, ast_rtp_instance_fd(sub->vrtp, 0));
04763       ast_channel_set_fd(sub->owner, 3, ast_rtp_instance_fd(sub->vrtp, 1));
04764    }
04765    if (sub->rtp) {
04766       ast_rtp_instance_set_qos(sub->rtp, qos.tos_audio, qos.cos_audio, "Skinny RTP");
04767       ast_rtp_instance_set_prop(sub->rtp, AST_RTP_PROPERTY_NAT, l->nat);
04768       /* Set frame packetization */
04769       ast_rtp_codecs_set_framing(ast_rtp_instance_get_codecs(sub->rtp),
04770          ast_format_cap_get_framing(l->cap));
04771    }
04772    if (sub->vrtp) {
04773       ast_rtp_instance_set_qos(sub->vrtp, qos.tos_video, qos.cos_video, "Skinny VRTP");
04774       ast_rtp_instance_set_prop(sub->vrtp, AST_RTP_PROPERTY_NAT, l->nat);
04775    }
04776 
04777    /* Create the RTP connection */
04778    transmit_connect(d, sub);
04779    skinny_unlocksub(sub);
04780 }
04781 
04782 static void destroy_rtp(struct skinny_subchannel *sub)
04783 {
04784    if (sub->rtp) {
04785       SKINNY_DEBUG(DEBUG_AUDIO, 3, "Sub %u - Destroying RTP\n", sub->callid);
04786       ast_rtp_instance_stop(sub->rtp);
04787       ast_rtp_instance_destroy(sub->rtp);
04788       sub->rtp = NULL;
04789    }
04790    if (sub->vrtp) {
04791       SKINNY_DEBUG(DEBUG_AUDIO, 3, "Sub %u - Destroying VRTP\n", sub->callid);
04792       ast_rtp_instance_stop(sub->vrtp);
04793       ast_rtp_instance_destroy(sub->vrtp);
04794       sub->vrtp = NULL;
04795    }
04796 }
04797 
04798 static void *skinny_newcall(void *data)
04799 {
04800    struct ast_channel *c = data;
04801    struct skinny_subchannel *sub = ast_channel_tech_pvt(c);
04802    struct skinny_line *l = sub->line;
04803    struct skinny_device *d = l->device;
04804    int res = 0;
04805 
04806    ast_channel_lock(c);
04807    ast_set_callerid(c,
04808       l->hidecallerid ? "" : l->cid_num,
04809       l->hidecallerid ? "" : l->cid_name,
04810       ast_channel_caller(c)->ani.number.valid ? NULL : l->cid_num);
04811 #if 1 /* XXX This code is probably not necessary */
04812    ast_party_number_free(&ast_channel_connected(c)->id.number);
04813    ast_party_number_init(&ast_channel_connected(c)->id.number);
04814    ast_channel_connected(c)->id.number.valid = 1;
04815    ast_channel_connected(c)->id.number.str = ast_strdup(ast_channel_exten(c));
04816    ast_party_name_free(&ast_channel_connected(c)->id.name);
04817    ast_party_name_init(&ast_channel_connected(c)->id.name);
04818 #endif
04819    ast_setstate(c, AST_STATE_RING);
04820    ast_channel_unlock(c);
04821    if (!sub->rtp) {
04822       start_rtp(sub);
04823    }
04824    ast_verb(3, "Sub %u - Calling %s@%s\n", sub->callid, ast_channel_exten(c), ast_channel_context(c));
04825    res = ast_pbx_run(c);
04826    if (res) {
04827       ast_log(LOG_WARNING, "PBX exited non-zero\n");
04828       transmit_start_tone(d, SKINNY_REORDER, l->instance, sub->callid);
04829    }
04830    return NULL;
04831 }
04832 
04833 static void skinny_dialer(struct skinny_subchannel *sub, int timedout)
04834 {
04835    struct ast_channel *c = sub->owner;
04836    struct skinny_line *l = sub->line;
04837    struct skinny_device *d = l->device;
04838 
04839    if (timedout || !ast_matchmore_extension(c, ast_channel_context(c), sub->exten, 1, l->cid_num)) {
04840       SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Force dialing '%s' because of %s\n", sub->callid, sub->exten, (timedout ? "timeout" : "exactmatch"));
04841       if (ast_exists_extension(c, ast_channel_context(c), sub->exten, 1, l->cid_num)) {
04842          if (sub->substate == SUBSTATE_OFFHOOK) {
04843             dialandactivatesub(sub, sub->exten);
04844          }
04845       } else {
04846          if (d->hookstate == SKINNY_OFFHOOK) {
04847             // FIXME: redundant because below will onhook before the sound plays, but it correct to send it.
04848             transmit_start_tone(d, SKINNY_REORDER, l->instance, sub->callid);
04849          }
04850          dumpsub(sub, 0);
04851       }
04852    } else {
04853       SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Wait for more digits\n", sub->callid);
04854       if (ast_exists_extension(c, ast_channel_context(c), sub->exten, 1, l->cid_num)) {
04855          transmit_selectsoftkeys(d, l->instance, sub->callid, KEYDEF_DADFD, KEYMASK_ALL);
04856          sub->dialer_sched = skinny_sched_add(matchdigittimeout, skinny_dialer_cb, sub);
04857       } else {
04858          sub->dialer_sched = skinny_sched_add(gendigittimeout, skinny_dialer_cb, sub);
04859       }
04860    }
04861 }
04862 
04863 static int skinny_dialer_cb(const void *data)
04864 {
04865    struct skinny_subchannel *sub = (struct skinny_subchannel *)data;
04866    SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Dialer called from SCHED %d\n", sub->callid, sub->dialer_sched);
04867    sub->dialer_sched = 0;
04868    skinny_dialer(sub, 1);
04869    return 0;
04870 }
04871 
04872 static int skinny_autoanswer_cb(const void *data)
04873 {
04874    struct skinny_subchannel *sub = (struct skinny_subchannel *)data;
04875    skinny_locksub(sub);
04876    sub->aa_sched = 0;
04877    setsubstate(sub, SKINNY_CONNECTED);
04878    skinny_unlocksub(sub);
04879    return 0;
04880 }
04881 
04882 static int skinny_cfwd_cb(const void *data)
04883 {
04884    struct skinny_subchannel *sub = (struct skinny_subchannel *)data;
04885    struct skinny_line *l = sub->line;
04886    sub->cfwd_sched = 0;
04887    SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - CFWDNOANS to %s.\n", sub->callid, l->call_forward_noanswer);
04888    ast_channel_call_forward_set(sub->owner, l->call_forward_noanswer);
04889    ast_queue_control(sub->owner, AST_CONTROL_REDIRECTING);
04890    return 0;
04891 }
04892 
04893 static int skinny_call(struct ast_channel *ast, const char *dest, int timeout)
04894 {
04895    int res = 0;
04896    struct skinny_subchannel *sub = ast_channel_tech_pvt(ast);
04897    struct skinny_line *l = sub->line;
04898    struct skinny_device *d = l->device;
04899    struct ast_var_t *current;
04900    int doautoanswer = 0;
04901 
04902    if (!d || !d->session) {
04903       ast_log(LOG_WARNING, "Device not registered, cannot call %s\n", dest);
04904       return -1;
04905    }
04906 
04907    if ((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) {
04908       ast_log(LOG_WARNING, "skinny_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
04909       return -1;
04910    }
04911 
04912    SKINNY_DEBUG(DEBUG_SUB, 3, "Skinny Call (%s) - Sub %u\n",
04913       ast_channel_name(ast), sub->callid);
04914 
04915    if (l->dnd) {
04916       ast_queue_control(ast, AST_CONTROL_BUSY);
04917       return -1;
04918    }
04919 
04920    if (AST_LIST_NEXT(sub,list) && !l->callwaiting) {
04921       ast_queue_control(ast, AST_CONTROL_BUSY);
04922       return -1;
04923    }
04924 
04925    skinny_locksub(sub);
04926    AST_LIST_TRAVERSE(ast_channel_varshead(ast), current, entries) {
04927       if (!(strcmp(ast_var_name(current), "SKINNY_AUTOANSWER"))) {
04928          if (d->hookstate == SKINNY_ONHOOK && !sub->aa_sched) {
04929             char buf[24];
04930             int aatime;
04931             char *stringp = buf, *curstr;
04932             ast_copy_string(buf, ast_var_value(current), sizeof(buf));
04933             curstr = strsep(&stringp, ":");
04934             aatime = atoi(curstr);
04935             while ((curstr = strsep(&stringp, ":"))) {
04936                if (!(strcasecmp(curstr,"BEEP"))) {
04937                   sub->aa_beep = 1;
04938                } else if (!(strcasecmp(curstr,"MUTE"))) {
04939                   sub->aa_mute = 1;
04940                }
04941             }
04942             SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - setting autoanswer time=%dms %s%s\n",
04943                sub->callid, aatime, sub->aa_beep ? "BEEP " : "", sub->aa_mute ? "MUTE" : "");
04944             if (aatime) {
04945                //sub->aa_sched = ast_sched_add(sched, aatime, skinny_autoanswer_cb, sub);
04946                sub->aa_sched = skinny_sched_add(aatime, skinny_autoanswer_cb, sub);
04947             } else {
04948                doautoanswer = 1;
04949             }
04950          }
04951       }
04952    }
04953 
04954    setsubstate(sub, SUBSTATE_RINGIN);
04955    if (doautoanswer) {
04956       setsubstate(sub, SUBSTATE_CONNECTED);
04957    }
04958    skinny_unlocksub(sub);
04959    return res;
04960 }
04961 
04962 static int skinny_hangup(struct ast_channel *ast)
04963 {
04964    struct skinny_subchannel *sub = ast_channel_tech_pvt(ast);
04965 
04966    if (!sub) {
04967       ast_debug(1, "Asked to hangup channel not connected\n");
04968       return 0;
04969    }
04970 
04971    dumpsub(sub, 1);
04972 
04973    SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Destroying\n", sub->callid);
04974 
04975    skinny_set_owner(sub, NULL);
04976    ast_channel_tech_pvt_set(ast, NULL);
04977    destroy_rtp(sub);
04978    ast_free(sub->origtonum);
04979    ast_free(sub->origtoname);
04980    ast_free(sub);
04981    ast_module_unref(ast_module_info->self);
04982    return 0;
04983 }
04984 
04985 static int skinny_answer(struct ast_channel *ast)
04986 {
04987    int res = 0;
04988    struct skinny_subchannel *sub = ast_channel_tech_pvt(ast);
04989 
04990    sub->cxmode = SKINNY_CX_SENDRECV;
04991 
04992    SKINNY_DEBUG(DEBUG_SUB, 3, "skinny_answer(%s) on %s@%s-%u\n",
04993       ast_channel_name(ast), sub->line->name, sub->line->device->name, sub->callid);
04994 
04995    setsubstate(sub, SUBSTATE_CONNECTED);
04996 
04997    return res;
04998 }
04999 
05000 /* Retrieve audio/etc from channel.  Assumes sub->lock is already held. */
05001 static struct ast_frame *skinny_rtp_read(struct skinny_subchannel *sub)
05002 {
05003    struct ast_channel *ast = sub->owner;
05004    struct ast_frame *f;
05005 
05006    if (!sub->rtp) {
05007       /* We have no RTP allocated for this channel */
05008       return &ast_null_frame;
05009    }
05010 
05011    switch(ast_channel_fdno(ast)) {
05012    case 0:
05013       f = ast_rtp_instance_read(sub->rtp, 0); /* RTP Audio */
05014       break;
05015    case 1:
05016       f = ast_rtp_instance_read(sub->rtp, 1); /* RTCP Control Channel */
05017       break;
05018    case 2:
05019       f = ast_rtp_instance_read(sub->vrtp, 0); /* RTP Video */
05020       break;
05021    case 3:
05022       f = ast_rtp_instance_read(sub->vrtp, 1); /* RTCP Control Channel for video */
05023       break;
05024 #if 0
05025    case 5:
05026       /* Not yet supported */
05027       f = ast_udptl_read(sub->udptl); /* UDPTL for T.38 */
05028       break;
05029 #endif
05030    default:
05031       f = &ast_null_frame;
05032    }
05033 
05034    if (ast) {
05035       /* We already hold the channel lock */
05036       if (f->frametype == AST_FRAME_VOICE) {
05037          if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), f->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
05038             struct ast_format_cap *caps;
05039 
05040             ast_debug(1, "Oooh, format changed to %s\n", ast_format_get_name(f->subclass.format));
05041 
05042             caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
05043             if (caps) {
05044                ast_format_cap_append(caps, f->subclass.format, 0);
05045                ast_channel_nativeformats_set(ast, caps);
05046                ao2_ref(caps, -1);
05047             }
05048             ast_set_read_format(ast, ast_channel_readformat(ast));
05049             ast_set_write_format(ast, ast_channel_writeformat(ast));
05050          }
05051       }
05052    }
05053    return f;
05054 }
05055 
05056 static struct ast_frame *skinny_read(struct ast_channel *ast)
05057 {
05058    struct ast_frame *fr;
05059    struct skinny_subchannel *sub = ast_channel_tech_pvt(ast);
05060    skinny_locksub(sub);
05061    fr = skinny_rtp_read(sub);
05062    skinny_unlocksub(sub);
05063    return fr;
05064 }
05065 
05066 static int skinny_write(struct ast_channel *ast, struct ast_frame *frame)
05067 {
05068    struct skinny_subchannel *sub = ast_channel_tech_pvt(ast);
05069    int res = 0;
05070    if (frame->frametype != AST_FRAME_VOICE) {
05071       if (frame->frametype == AST_FRAME_IMAGE) {
05072          return 0;
05073       } else {
05074          ast_log(LOG_WARNING, "Can't send %u type frames with skinny_write\n", frame->frametype);
05075          return 0;
05076       }
05077    } else {
05078       if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), frame->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
05079          struct ast_str *codec_buf = ast_str_alloca(64);
05080          ast_log(LOG_WARNING, "Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
05081             ast_format_get_name(frame->subclass.format),
05082             ast_format_cap_get_names(ast_channel_nativeformats(ast), &codec_buf),
05083             ast_format_get_name(ast_channel_readformat(ast)),
05084             ast_format_get_name(ast_channel_writeformat(ast)));
05085          return -1;
05086       }
05087    }
05088    if (sub) {
05089       skinny_locksub(sub);
05090       if (sub->rtp) {
05091          res = ast_rtp_instance_write(sub->rtp, frame);
05092       }
05093       skinny_unlocksub(sub);
05094    }
05095    return res;
05096 }
05097 
05098 static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
05099 {
05100    struct skinny_subchannel *sub = ast_channel_tech_pvt(newchan);
05101    ast_log(LOG_NOTICE, "skinny_fixup(%s, %s)\n", ast_channel_name(oldchan), ast_channel_name(newchan));
05102    if (sub->owner != oldchan) {
05103       ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, sub->owner);
05104       return -1;
05105    }
05106    skinny_set_owner(sub, newchan);
05107    return 0;
05108 }
05109 
05110 static int skinny_senddigit_begin(struct ast_channel *ast, char digit)
05111 {
05112    return -1; /* Start inband indications */
05113 }
05114 
05115 static int skinny_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
05116 {
05117 #if 0
05118    struct skinny_subchannel *sub = ast->tech_pvt;
05119    struct skinny_line *l = sub->line;
05120    struct skinny_device *d = l->device;
05121    int tmp;
05122    /* not right */
05123    sprintf(tmp, "%d", digit);
05124    //transmit_tone(d, digit, l->instance, sub->callid);
05125 #endif
05126    return -1; /* Stop inband indications */
05127 }
05128 
05129 static int get_devicestate(struct skinny_line *l)
05130 {
05131    struct skinny_subchannel *sub;
05132    int res = AST_DEVICE_UNKNOWN;
05133 
05134    if (!l)
05135       res = AST_DEVICE_INVALID;
05136    else if (!l->device || !l->device->session)
05137       res = AST_DEVICE_UNAVAILABLE;
05138    else if (l->dnd)
05139       res = AST_DEVICE_BUSY;
05140    else {
05141       if (l->device->hookstate == SKINNY_ONHOOK) {
05142          res = AST_DEVICE_NOT_INUSE;
05143       } else {
05144          res = AST_DEVICE_INUSE;
05145       }
05146 
05147       AST_LIST_TRAVERSE(&l->sub, sub, list) {
05148          if (sub->substate == SUBSTATE_HOLD) {
05149             res = AST_DEVICE_ONHOLD;
05150             break;
05151          }
05152       }
05153    }
05154 
05155    return res;
05156 }
05157 
05158 static char *control2str(int ind) {
05159    char *tmp;
05160 
05161    switch (ind) {
05162    case AST_CONTROL_HANGUP:
05163       return "Other end has hungup";
05164    case AST_CONTROL_RING:
05165       return "Local ring";
05166    case AST_CONTROL_RINGING:
05167       return "Remote end is ringing";
05168    case AST_CONTROL_ANSWER:
05169       return "Remote end has answered";
05170    case AST_CONTROL_BUSY:
05171       return "Remote end is busy";
05172    case AST_CONTROL_TAKEOFFHOOK:
05173       return "Make it go off hook";
05174    case AST_CONTROL_OFFHOOK:
05175       return "Line is off hook";
05176    case AST_CONTROL_CONGESTION:
05177       return "Congestion (circuits busy)";
05178    case AST_CONTROL_FLASH:
05179       return "Flash hook";
05180    case AST_CONTROL_WINK:
05181       return "Wink";
05182    case AST_CONTROL_OPTION:
05183       return "Set a low-level option";
05184    case AST_CONTROL_RADIO_KEY:
05185       return "Key Radio";
05186    case AST_CONTROL_RADIO_UNKEY:
05187       return "Un-Key Radio";
05188    case AST_CONTROL_PROGRESS:
05189       return "Remote end is making Progress";
05190    case AST_CONTROL_PROCEEDING:
05191       return "Remote end is proceeding";
05192    case AST_CONTROL_HOLD:
05193       return "Hold";
05194    case AST_CONTROL_UNHOLD:
05195       return "Unhold";
05196    case AST_CONTROL_VIDUPDATE:
05197       return "VidUpdate";
05198    case AST_CONTROL_SRCUPDATE:
05199       return "Media Source Update";
05200    case AST_CONTROL_TRANSFER:
05201       return "Transfer";
05202    case AST_CONTROL_CONNECTED_LINE:
05203       return "Connected Line";
05204    case AST_CONTROL_REDIRECTING:
05205       return "Redirecting";
05206    case AST_CONTROL_T38_PARAMETERS:
05207       return "T38_Parameters";
05208    case AST_CONTROL_CC:
05209       return "CC Not Possible";
05210    case AST_CONTROL_SRCCHANGE:
05211       return "Media Source Change";
05212    case AST_CONTROL_INCOMPLETE:
05213       return "Incomplete";
05214    case -1:
05215       return "Stop tone";
05216    default:
05217       if (!(tmp = ast_threadstorage_get(&control2str_threadbuf, CONTROL2STR_BUFSIZE)))
05218                         return "Unknown";
05219       snprintf(tmp, CONTROL2STR_BUFSIZE, "UNKNOWN-%d", ind);
05220       return tmp;
05221    }
05222 }
05223 
05224 static void skinny_transfer_attended(struct skinny_subchannel *sub)
05225 {
05226    struct skinny_subchannel *xferee;
05227    struct skinny_subchannel *xferor;
05228    enum ast_transfer_result res;
05229 
05230    if (sub->xferor) {
05231       xferor = sub;
05232       xferee = sub->related;
05233    } else {
05234       xferor = sub;
05235       xferee = sub->related;
05236    }
05237 
05238    ast_queue_control(xferee->owner, AST_CONTROL_UNHOLD);
05239    if (ast_channel_state(xferor->owner) == AST_STATE_RINGING) {
05240       ast_queue_control(xferee->owner, AST_CONTROL_RINGING);
05241    }
05242    res = ast_bridge_transfer_attended(xferee->owner, xferor->owner);
05243 
05244    if (res != AST_BRIDGE_TRANSFER_SUCCESS) {
05245       SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u failed to transfer %u to '%s'@'%s' - %u\n",
05246          xferor->callid, xferee->callid, xferor->exten, xferor->line->context, res);
05247       send_displayprinotify(xferor->line->device, "Transfer failed", NULL, 10, 5);
05248       ast_queue_control(xferee->owner, AST_CONTROL_HOLD);
05249    }
05250 }
05251 
05252 static void skinny_transfer_blind(struct skinny_subchannel *sub)
05253 {
05254    struct skinny_subchannel *xferee = sub->related;
05255    enum ast_transfer_result res;
05256 
05257    sub->related = NULL;
05258    xferee->related = NULL;
05259 
05260    ast_queue_control(xferee->owner, AST_CONTROL_UNHOLD);
05261    res = ast_bridge_transfer_blind(1, xferee->owner, sub->exten, sub->line->context, NULL, NULL);
05262 
05263    if (res != AST_BRIDGE_TRANSFER_SUCCESS) {
05264       SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u failed to blind transfer %u to '%s'@'%s' - %u\n",
05265          sub->callid, xferee->callid, sub->exten, sub->line->context, res);
05266       send_displayprinotify(sub->line->device, "Transfer failed", NULL, 10, 5);
05267       ast_queue_control(xferee->owner, AST_CONTROL_HOLD);
05268    }
05269    dumpsub(sub, 1);
05270 }
05271 
05272 static int skinny_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen)
05273 {
05274    struct skinny_subchannel *sub = ast_channel_tech_pvt(ast);
05275    struct skinny_line *l = sub->line;
05276    struct skinny_device *d = l->device;
05277    struct skinnysession *s = d->session;
05278 
05279    if (!s) {
05280       ast_log(LOG_NOTICE, "Asked to indicate '%s' condition on channel %s, but session does not exist.\n", control2str(ind), ast_channel_name(ast));
05281       return -1;
05282    }
05283 
05284    SKINNY_DEBUG(DEBUG_SUB, 3, "Asked to indicate '%s' condition on channel %s (Sub %u)\n",
05285       control2str(ind), ast_channel_name(ast), sub->callid);
05286    switch(ind) {
05287    case AST_CONTROL_RINGING:
05288       setsubstate(sub, SUBSTATE_RINGOUT);
05289       return (d->earlyrtp ? -1 : 0); /* Tell asterisk to provide inband signalling if rtp started */
05290    case AST_CONTROL_BUSY:
05291       setsubstate(sub, SUBSTATE_BUSY);
05292       return (d->earlyrtp ? -1 : 0); /* Tell asterisk to provide inband signalling if rtp started */
05293    case AST_CONTROL_INCOMPLETE:
05294       /* Support for incomplete not supported for chan_skinny; treat as congestion */
05295    case AST_CONTROL_CONGESTION:
05296       setsubstate(sub, SUBSTATE_CONGESTION);
05297       return (d->earlyrtp ? -1 : 0); /* Tell asterisk to provide inband signalling if rtp started */
05298    case AST_CONTROL_PROGRESS:
05299       setsubstate(sub, SUBSTATE_PROGRESS);
05300       return (d->earlyrtp ? -1 : 0); /* Tell asterisk to provide inband signalling if rtp started */
05301    case -1:  /* STOP_TONE */
05302       transmit_stop_tone(d, l->instance, sub->callid);
05303       break;
05304    case AST_CONTROL_HOLD:
05305       ast_moh_start(ast, data, l->mohinterpret);
05306       break;
05307    case AST_CONTROL_UNHOLD:
05308       ast_moh_stop(ast);
05309       break;
05310    case AST_CONTROL_PROCEEDING:
05311       break;
05312    case AST_CONTROL_SRCUPDATE:
05313       if (sub->rtp) {
05314          ast_rtp_instance_update_source(sub->rtp);
05315       }
05316       break;
05317    case AST_CONTROL_SRCCHANGE:
05318       if (sub->rtp) {
05319          ast_rtp_instance_change_source(sub->rtp);
05320       }
05321       break;
05322    case AST_CONTROL_CONNECTED_LINE:
05323       update_connectedline(sub, data, datalen);
05324       break;
05325    default:
05326       ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", ind);
05327       /* fallthrough */
05328    case AST_CONTROL_PVT_CAUSE_CODE:
05329    case AST_CONTROL_MASQUERADE_NOTIFY:
05330       return -1; /* Tell asterisk to provide inband signalling */
05331    }
05332    return 0;
05333 }
05334 
05335 static void skinny_set_owner(struct skinny_subchannel* sub, struct ast_channel* chan)
05336 {
05337    sub->owner = chan;
05338    if (sub->rtp) {
05339       ast_rtp_instance_set_channel_id(sub->rtp, sub->owner ?