/*
* Asterisk -- An open source telephony toolkit.
*
* UNISTIM channel driver for asterisk
*
* Copyright (C) 2005 - 2007, Cedric Hans
*
* Cedric Hans <cedric.hans@mlkj.net>
*
* Asterisk 1.4 patch by Peter Be
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*!
* \file
*
* \brief chan_unistim channel driver for Asterisk
* \author Cedric Hans <cedric.hans@mlkj.net>
*
* Unistim (Unified Networks IP Stimulus) channel driver
* for Nortel i2002, i2004 and i2050
*
* \ingroup channel_drivers
*/
/*** MODULEINFO
<support_level>extended</support_level>
***/
#include "asterisk.h"
#include <sys/stat.h>
#include <signal.h>
#if defined(__CYGWIN__) || defined(__NetBSD__)
/*
* cygwin headers are partly inconsistent. struct iovec is defined in sys/uio.h
* which is not included by default by sys/socket.h - in_pktinfo is defined in
* w32api/ws2tcpip.h but this probably has compatibility problems with sys/socket.h
* So for the time being we simply disable HAVE_PKTINFO when building under cygwin.
* This should be done in some common header, but for now this is the only file
* using iovec and in_pktinfo so it suffices to apply the fix here.
*/
#ifdef HAVE_PKTINFO
#undef HAVE_PKTINFO
#endif
#endif /* __CYGWIN__ || __NetBSD__ */
#include "asterisk/paths.h" /* ast_config_AST_LOG_DIR used in (too ?) many places */
#include "asterisk/network.h"
#include "asterisk/channel.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/rtp_engine.h"
#include "asterisk/netsock2.h"
#include "asterisk/acl.h"
#include "asterisk/callerid.h"
#include "asterisk/cli.h"
#include "asterisk/app.h"
#include "asterisk/musiconhold.h"
#include "asterisk/causes.h"
#include "asterisk/indications.h"
#include "asterisk/pickup.h"
#include "asterisk/astobj2.h"
#include "asterisk/astdb.h"
#include "asterisk/features_config.h"
#include "asterisk/bridge.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/format_cache.h"
#define DEFAULTCONTEXT "default"
#define DEFAULTCALLERID "Unknown"
#define DEFAULTCALLERNAME " "
#define DEFAULTHEIGHT 3
#define USTM_LOG_DIR "unistimHistory"
#define USTM_LANG_DIR "unistimLang"
/*! Size of the transmit buffer */
#define MAX_BUF_SIZE 64
/*! Number of slots for the transmit queue */
#define MAX_BUF_NUMBER 150
/*! Number of digits displayed on screen */
#define MAX_SCREEN_NUMBER 15
/*! Length of month label size */
#define MONTH_LABEL_SIZE 3
/*! Try x times before removing the phone */
#define NB_MAX_RETRANSMIT 8
/*! Nb of milliseconds waited when no events are scheduled */
#define IDLE_WAIT 1000
/*! Wait x milliseconds before resending a packet */
#define RETRANSMIT_TIMER 2000
/*! How often the mailbox is checked for new messages */
#define TIMER_MWI 5000
/*! Timeout value for entered number being dialed */
#define DEFAULT_INTERDIGIT_TIMER 4000
/*! Not used */
#define DEFAULT_CODEC 0x00
#define SIZE_PAGE 4096
#define DEVICE_NAME_LEN 16
#define AST_CONFIG_MAX_PATH 255
#define MAX_ENTRY_LOG 30
#define SUB_REAL 0
#define SUB_RING 1
#define SUB_THREEWAY 2
struct ast_format_cap *global_cap;
enum autoprovision {
AUTOPROVISIONING_NO = 0,
AUTOPROVISIONING_YES,
AUTOPROVISIONING_TN
};
enum autoprov_extn {
/*! Do not create an extension into the default dialplan */
EXTENSION_NONE = 0,
/*! Prompt user for an extension number and register it */
EXTENSION_ASK,
/*! Register an extension with the line=> value */
EXTENSION_LINE,
/*! Used with AUTOPROVISIONING_TN */
EXTENSION_TN
};
#define OUTPUT_HANDSET 0xC0
#define OUTPUT_HEADPHONE 0xC1
#define OUTPUT_SPEAKER 0xC2
#define VOLUME_LOW 0x01
#define VOLUME_LOW_SPEAKER 0x03
#define VOLUME_NORMAL 0x02
#define VOLUME_INSANELY_LOUD 0x07
#define MUTE_OFF 0x00
#define MUTE_ON 0xFF
#define MUTE_ON_DISCRET 0xCE
#define LED_BAR_OFF 0x00 /* bar off */
#define LED_BAR_ON 0x01 /* bar on */
#define LED_BAR_P2 0x02 /* bar 1s on/1s */
#define LED_BAR_P3 0x03 /* bar 2.5s on/0.5s off */
#define LED_BAR_P4 0x04 /* bar 0.6s on/0.3s off */
#define LED_BAR_P5 0x05 /* bar 0.5s on/0.5s off */
#define LED_BAR_P6 0x06 /* bar 2s on/0.5s off */
#define LED_BAR_P7 0x07 /* bar off */
#define LED_SPEAKER_OFF 0x08
#define LED_SPEAKER_ON 0x09
#define LED_HEADPHONE_OFF 0x010
#define LED_HEADPHONE_ON 0x011
#define LED_MUTE_OFF 0x018
#define LED_MUTE_ON 0x019
#define LED_MUTE_BLINK 0x1A
#define SIZE_HEADER 6
#define SIZE_MAC_ADDR 17
#define TEXT_LENGTH_MAX 24
#define TEXT_LINE0 0x00
#define TEXT_LINE1 0x20
#define TEXT_LINE2 0x40
#define TEXT_NORMAL 0x05
#define TEXT_INVERSE 0x25
#define STATUS_LENGTH_MAX 28
#define FAV_ICON_NONE 0x00
#define FAV_ICON_ONHOOK_BLACK 0x20
#define FAV_ICON_ONHOOK_WHITE 0x21
#define FAV_ICON_SPEAKER_ONHOOK_BLACK 0x22
#define FAV_ICON_SPEAKER_ONHOOK_WHITE 0x23
#define FAV_ICON_OFFHOOK_BLACK 0x24
#define FAV_ICON_OFFHOOK_WHITE 0x25
#define FAV_ICON_ONHOLD_BLACK 0x26
#define FAV_ICON_ONHOLD_WHITE 0x27
#define FAV_ICON_SPEAKER_OFFHOOK_BLACK 0x28
#define FAV_ICON_SPEAKER_OFFHOOK_WHITE 0x29
#define FAV_ICON_PHONE_BLACK 0x2A
#define FAV_ICON_PHONE_WHITE 0x2B
#define FAV_ICON_SPEAKER_ONHOLD_BLACK 0x2C
#define FAV_ICON_SPEAKER_ONHOLD_WHITE 0x2D
#define FAV_ICON_HEADPHONES 0x2E
#define FAV_ICON_HEADPHONES_ONHOLD 0x2F
#define FAV_ICON_HOME 0x30
#define FAV_ICON_CITY 0x31
#define FAV_ICON_SHARP 0x32
#define FAV_ICON_PAGER 0x33
#define FAV_ICON_CALL_CENTER 0x34
#define FAV_ICON_FAX 0x35
#define FAV_ICON_MAILBOX 0x36
#define FAV_ICON_REFLECT 0x37
#define FAV_ICON_COMPUTER 0x38
#define FAV_ICON_FORWARD 0x39
#define FAV_ICON_LOCKED 0x3A
#define FAV_ICON_TRASH 0x3B
#define FAV_ICON_INBOX 0x3C
#define FAV_ICON_OUTBOX 0x3D
#define FAV_ICON_MEETING 0x3E
#define FAV_ICON_BOX 0x3F
#define FAV_BLINK_FAST 0x20
#define FAV_BLINK_SLOW 0x40
#define FAV_MAX_LENGTH 0x0A
#define FAVNUM 6
#define EXPNUM 24
#define FAV_LINE_ICON FAV_ICON_ONHOOK_BLACK
static void dummy(char *unused, ...)
{
return;
}
/*! \brief Global jitterbuffer configuration - by default, jb is disabled
* \note Values shown here match the defaults shown in unistim.conf.sample */
static struct ast_jb_conf default_jbconf =
{
.flags = 0,
.max_size = 200,
.resync_threshold = 1000,
.impl = "fixed",
.target_extra = 40,
};
static struct ast_jb_conf global_jbconf;
/* #define DUMP_PACKET 1 */
/* #define DEBUG_TIMER ast_verbose */
#define DEBUG_TIMER dummy
/*! Enable verbose output. can also be set with the CLI */
static int unistimdebug = 0;
static int unistim_port;
static enum autoprovision autoprovisioning = AUTOPROVISIONING_NO;
static int unistim_keepalive;
static int unistimsock = -1;
static struct {
unsigned int tos;
unsigned int tos_audio;
unsigned int cos;
unsigned int cos_audio;
} qos = { 0, 0, 0, 0 };
static struct io_context *io;
static struct ast_sched_context *sched;
static struct sockaddr_in public_ip = { 0, };
static unsigned char *buff; /*! Receive buffer address */
static int unistim_reloading = 0;
AST_MUTEX_DEFINE_STATIC(unistim_reload_lock);
/*! This is the thread for the monitor which checks for input on the channels
* which are not currently in use. */
static pthread_t monitor_thread = AST_PTHREADT_NULL;
/*! Protect the monitoring thread, so only one process can kill or start it, and not
* when it's doing something critical. */
AST_MUTEX_DEFINE_STATIC(monlock);
/*! Protect the session list */
AST_MUTEX_DEFINE_STATIC(sessionlock);
/*! Protect the device list */
AST_MUTEX_DEFINE_STATIC(devicelock);
enum phone_state {
STATE_INIT,
STATE_AUTHDENY,
STATE_MAINPAGE,
STATE_EXTENSION,
STATE_DIALPAGE,
STATE_RINGING,
STATE_CALL,
STATE_SELECTOPTION,
STATE_SELECTCODEC,
STATE_SELECTLANGUAGE,
STATE_CLEANING,
STATE_HISTORY
};
enum handset_state {
STATE_ONHOOK,
STATE_OFFHOOK,
};
enum phone_key {
KEY_0 = 0x40,
KEY_1 = 0x41,
KEY_2 = 0x42,
KEY_3 = 0x43,
KEY_4 = 0x44,
KEY_5 = 0x45,
KEY_6 = 0x46,
KEY_7 = 0x47,
KEY_8 = 0x48,
KEY_9 = 0x49,
KEY_STAR = 0x4a,
KEY_SHARP = 0x4b,
KEY_UP = 0x4c,
KEY_DOWN = 0x4d,
KEY_RIGHT = 0x4e,
KEY_LEFT = 0x4f,
KEY_QUIT = 0x50,
KEY_COPY = 0x51,
KEY_FUNC1 = 0x54,
KEY_FUNC2 = 0x55,
KEY_FUNC3 = 0x56,
KEY_FUNC4 = 0x57,
KEY_ONHOLD = 0x5b,
KEY_HANGUP = 0x5c,
KEY_MUTE = 0x5d,
KEY_HEADPHN = 0x5e,
KEY_LOUDSPK = 0x5f,
KEY_FAV0 = 0x60,
KEY_FAV1 = 0x61,
KEY_FAV2 = 0x62,
KEY_FAV3 = 0x63,
KEY_FAV4 = 0x64,
KEY_FAV5 = 0x65,
KEY_COMPUTR = 0x7b,
KEY_CONF = 0x7c,
KEY_SNDHIST = 0x7d,
KEY_RCVHIST = 0x7e,
KEY_INDEX = 0x7f
};
enum charset {
LANG_DEFAULT,
ISO_8859_1,
ISO_8859_2,
ISO_8859_4,
ISO_8859_5,
ISO_2022_JP,
};
static const int dtmf_row[] = { 697, 770, 852, 941 };
static const float dtmf_col[] = { 1209, 1336, 1477, 1633 };
struct wsabuf {
u_long len;
unsigned char *buf;
};
struct unistim_subchannel {
ast_mutex_t lock;
unsigned int subtype; /*! SUB_REAL, SUB_RING or SUB_THREEWAY */
struct ast_channel *owner; /*! Asterisk channel used by the subchannel */
struct unistim_line *parent; /*! Unistim line */
struct ast_rtp_instance *rtp; /*! RTP handle */
int softkey; /*! Softkey assigned */
pthread_t ss_thread; /*! unistim_ss thread handle */
int alreadygone;
int holding; /*! this subchannel holds someone */
signed char ringvolume;
signed char ringstyle;
int moh; /*!< Music on hold in progress */
AST_LIST_ENTRY(unistim_subchannel) list;
};
/*!
* \todo Convert to stringfields
*/
struct unistim_line {
ast_mutex_t lock;
char name[80]; /*! Like 200 */
char fullname[101]; /*! Like USTM/200\@black */
char exten[AST_MAX_EXTENSION]; /*! Extension where to start */
char cid_num[AST_MAX_EXTENSION]; /*! CallerID Number */
char mailbox[AST_MAX_EXTENSION]; /*! Mailbox for MWI */
char musicclass[MAX_MUSICCLASS]; /*! MusicOnHold class */
ast_group_t callgroup; /*! Call group */
ast_group_t pickupgroup; /*! Pickup group */
char accountcode[AST_MAX_ACCOUNT_CODE]; /*! Account code (for billing) */
int amaflags; /*! AMA flags (for billing) */
struct ast_format_cap *cap; /*! Codec supported */
char parkinglot[AST_MAX_CONTEXT]; /*! Parkinglot */
struct unistim_line *next;
struct unistim_device *parent;
AST_LIST_ENTRY(unistim_line) list;
};
/*!
* \brief A device containing one or more lines
*/
static struct unistim_device {
ast_mutex_t lock;
int receiver_state; /*!< state of the receiver (see ReceiverState) */
int size_phone_number; /*!< size of the phone number */
char context[AST_MAX_EXTENSION]; /*!< Context to start in */
char phone_number[AST_MAX_EXTENSION]; /*!< the phone number entered by the user */
char redial_number[AST_MAX_EXTENSION]; /*!< the last phone number entered by the user */
char id[18]; /*!< mac address of the current phone in ascii */
char name[DEVICE_NAME_LEN]; /*!< name of the device */
int hasexp; /*!< if device have expansion connected */
char expsoftkeylabel[EXPNUM][11]; /*!< soft key label */
char softkeylabel[FAVNUM][11]; /*!< soft key label */
char softkeynumber[FAVNUM][AST_MAX_EXTENSION]; /*!< number dialed when the soft key is pressed */
char softkeyicon[FAVNUM]; /*!< icon number */
char softkeydevice[FAVNUM][16]; /*!< name of the device monitored */
struct unistim_subchannel *ssub[FAVNUM];
struct unistim_line *sline[FAVNUM];
struct unistim_device *sp[FAVNUM]; /*!< pointer to the device monitored by this soft key */
char language[MAX_LANGUAGE]; /*!< Language for asterisk sounds */
int height; /*!< The number of lines the phone can display */
char maintext0[25]; /*!< when the phone is idle, display this string on line 0 */
char maintext1[25]; /*!< when the phone is idle, display this string on line 1 */
char maintext2[25]; /*!< when the phone is idle, display this string on line 2 */
char titledefault[13]; /*!< title (text before date/time) */
char datetimeformat; /*!< format used for displaying time/date */
signed char contrast; /*!< contrast */
char country[3]; /*!< country used for dial tone frequency */
struct ast_tone_zone *tz; /*!< Tone zone for res_indications (ring, busy, congestion) */
signed char ringvolume; /*!< Ring volume */
signed char ringstyle; /*!< Ring melody */
signed char cwvolume; /*!< Ring volume on call waiting */
signed char cwstyle; /*!< Ring melody on call waiting */
int interdigit_timer; /*!< Interdigit timer for dialing number by timeout */
int dtmfduration; /*!< DTMF playback duration */
time_t nextdial; /*!< Timer used for dial by timeout */
int rtp_port; /*!< RTP port used by the phone */
int rtp_method; /*!< Select the unistim data used to establish a RTP session */
int status_method; /*!< Select the unistim packet used for sending status text */
char codec_number; /*!< The current codec used to make calls */
int missed_call; /*!< Number of call unanswered */
int callhistory; /*!< Allowed to record call history */
int sharp_dial; /*!< Execute Dial on '#' or not */
char lst_cid[TEXT_LENGTH_MAX]; /*!< Last callerID received */
char lst_cnm[TEXT_LENGTH_MAX]; /*!< Last callername recevied */
char call_forward[AST_MAX_EXTENSION]; /*!< Forward number */
int output; /*!< Handset, headphone or speaker */
int previous_output; /*!< Previous output */
int volume; /*!< Default volume */
int selected; /*!< softkey selected */
int microphone; /*!< Microphone mode (audio tx) */
int lastmsgssent; /*! Used by MWI */
time_t nextmsgcheck; /*! Used by MWI */
int nat; /*!< Used by the obscure ast_rtp_setnat */
enum autoprov_extn extension; /*!< See ifdef EXTENSION for valid values */
char extension_number[11]; /*!< Extension number entered by the user */
signed char to_delete; /*!< Used in reload */
struct ast_silence_generator *silence_generator;
AST_LIST_HEAD(,unistim_subchannel) subs; /*!< pointer to our current connection, channel... */
AST_LIST_HEAD(,unistim_line) lines;
struct ast_ha *ha;
struct unistimsession *session;
struct unistim_device *next;
} *devices = NULL;
static struct unistimsession {
ast_mutex_t lock;
struct sockaddr_in sin; /*!< IP address of the phone */
struct sockaddr_in sout; /*!< IP address of server */
int timeout; /*!< time-out in ticks : resend packet if no ack was received before the timeout occured */
unsigned short seq_phone; /*!< sequence number for the next packet (when we receive a request) */
unsigned short seq_server; /*!< sequence number for the next packet (when we send a request) */
unsigned short last_seq_ack; /*!< sequence number of the last ACK received */
unsigned long tick_next_ping; /*!< time for the next ping */
int last_buf_available; /*!< number of a free slot */
int nb_retransmit; /*!< number of retransmition */
int state; /*!< state of the phone (see phone_state) */
int size_buff_entry; /*!< size of the buffer used to enter datas */
char buff_entry[16]; /*!< Buffer for temporary datas */
char macaddr[18]; /*!< mac address of the phone (not always available) */
char firmware[8]; /*!< firmware of the phone (not always available) */
struct wsabuf wsabufsend[MAX_BUF_NUMBER]; /*!< Size of each paquet stored in the buffer array & pointer to this buffer */
unsigned char buf[MAX_BUF_NUMBER][MAX_BUF_SIZE]; /*!< Buffer array used to keep the lastest non-acked paquets */
struct unistim_device *device;
struct unistimsession *next;
} *sessions = NULL;
/*! Store on screen phone menu item (label and handler function) */
struct unistim_menu_item {
char *label;
int state;
void (*handle_option)(struct unistimsession *);
};
/*! Language item for currently existed translations */
struct unistim_languages {
char *label;
char *lang_short;
int encoding;
struct ao2_container *trans;
};
/*!
* \page Unistim datagram formats
*
* Format of datagrams :
* bytes 0 & 1 : ffff for discovery packet, 0000 for everything else
* byte 2 : sequence number (high part)
* byte 3 : sequence number (low part)
* byte 4 : 2 = ask question or send info, 1 = answer or ACK, 0 = retransmit request
* byte 5 : direction, 1 = server to phone, 2 = phone to server arguments
*/
static const unsigned char packet_rcv_discovery[] =
{ 0xff, 0xff, 0xff, 0xff, 0x02, 0x02, 0xff, 0xff, 0xff, 0xff, 0x9e, 0x03, 0x08 };
static const unsigned char packet_send_discovery_ack[] =
{ 0x00, 0x00, /*Initial Seq (2 bytes) */ 0x00, 0x00, 0x00, 0x01 };
static const unsigned char packet_recv_firm_version[] =
{ 0x00, 0x00, 0x00, 0x13, 0x9a, 0x0a, 0x02 };
static const unsigned char packet_recv_it_type[] =
{ 0x00, 0x00, 0x00, 0x13, 0x9a, 0x04, 0x03 };
static const unsigned char packet_recv_pressed_key[] =
{ 0x00, 0x00, 0x00, 0x13, 0x99, 0x04, 0x00 };
static const unsigned char packet_recv_pick_up[] =
{ 0x00, 0x00, 0x00, 0x13, 0x99, 0x03, 0x04 };
static const unsigned char packet_recv_hangup[] =
{ 0x00, 0x00, 0x00, 0x13, 0x99, 0x03, 0x03 };
static const unsigned char packet_recv_r2[] = { 0x00, 0x00, 0x00, 0x13, 0x96, 0x03, 0x03 };
/*! Expansion module (i2004 KEM) */
static const unsigned char packet_recv_expansion_pressed_key[] =
{ 0x00, 0x00, 0x00, 0x13, 0x89, 0x04, 0x59 };
static const unsigned char packet_send_expansion_next[] = { 0x09, 0x03, 0x17 };
static const unsigned char packet_send_expansion_icon[] = { 0x09, 0x06, 0x59, 0x05, /*pos */ 0x47, /*icon */ 0x20 }; /* display an icon in front of the text zone */
static const unsigned char packet_send_expansion_text[] = { 0x09, 0x0f, 0x57, 0x19, /*pos */ 0x47, /*text */ 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 /*end_text */ };
/*! TransportAdapter */
static const unsigned char packet_recv_resume_connection_with_server[] =
{ 0xff, 0xff, 0xff, 0xff, 0x9e, 0x03, 0x08 };
static const unsigned char packet_recv_mac_addr[] =
{ 0xff, 0xff, 0xff, 0xff, 0x9a, 0x0d, 0x07 /*MacAddr */ };
static const unsigned char packet_send_date_time3[] =
{ 0x11, 0x09, 0x02, 0x02, /*Month */ 0x05, /*Day */ 0x06, /*Hour */ 0x07,
/*Minutes */ 0x08, 0x32
};
static const unsigned char packet_send_date_time[] =
{ 0x11, 0x09, 0x02, 0x0a, /*Month */ 0x05, /*Day */ 0x06, /*Hour */ 0x07, /*Minutes */
0x08, 0x32, 0x17, 0x04, 0x24, 0x07, 0x19,
0x04, 0x07, 0x00, 0x19, 0x05, 0x09, 0x3e, 0x0f, 0x16, 0x05, 0x00, 0x80, 0x00, 0x1e,
0x05, 0x12, 0x00, 0x78
};
static const unsigned char packet_send_no_ring[] =
{ 0x16, 0x04, 0x1a, 0x00, 0x16, 0x04, 0x11, 0x00 };
static const unsigned char packet_send_s4[] =
{ 0x16, 0x04, 0x1a, 0x00, 0x16, 0x04, 0x11, 0x00, 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff,
0x16, 0x05, 0x1c, 0x00, 0x00, 0x17, 0x05,
0x0b, 0x00, 0x00, 0x19, 0x04, 0x00, 0x00, 0x19, 0x04, 0x00, 0x08, 0x19, 0x04, 0x00,
0x10, 0x19, 0x04, 0x00, 0x18, 0x16, 0x05,
0x31, 0x00, 0x00, 0x16, 0x05, 0x04, 0x00, 0x00
};
static const unsigned char packet_send_call[] =
{ 0x16, 0x04, 0x1a, 0x00, 0x16, 0x04, 0x11, 0x00, 0x16, 0x06, 0x32, 0xdf,
0x00, 0xff, 0x16, 0x05, 0x1c, 0x00, 0x00, 0x16, 0x0a, 0x38, 0x00, 0x12, 0xca, 0x03,
0xc0, 0xc3, 0xc5, 0x16, 0x16, 0x30, 0x00,
0x00, /*codec */ 0x12, 0x12, /* frames per packet */ 0x01, 0x5c, 0x00, /*port RTP */
0x0f, 0xa0, /* port RTCP */ 0x9c, 0x41,
/*port RTP */ 0x0f, 0xa0, /* port RTCP */ 0x9c, 0x41, /* IP Address */ 0x0a, 0x01,
0x16, 0x66
};
static const unsigned char packet_send_stream_based_tone_off[] =
{ 0x16, 0x05, 0x1c, 0x00, 0x00 };
static const unsigned char packet_send_mute[] = { 0x16, 0x05, 0x04, 0x00, 0x00 };
#ifdef NOT_USED
static const unsigned char packet_send_CloseAudioStreamRX[] = { 0x16, 0x05, 0x31, 0x00, 0xff };
static const unsigned char packet_send_CloseAudioStreamTX[] = { 0x16, 0x05, 0x31, 0xff, 0x00 };
#endif
static const unsigned char packet_send_stream_based_tone_on[] =
{ 0x16, 0x06, 0x1b, 0x00, 0x00, 0x05 };
static const unsigned char packet_send_stream_based_tone_single_freq[] =
{ 0x16, 0x06, 0x1d, 0x00, 0x01, 0xb8 };
static const unsigned char packet_send_stream_based_tone_dial_freq[] =
{ 0x16, 0x08, 0x1d, 0x00, 0x01, 0xb8, 0x01, 0x5e };
static const unsigned char packet_send_select_output[] =
{ 0x16, 0x06, 0x32, 0xc0, 0x01, 0x00 };
static const unsigned char packet_send_ring[] =
{ 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff, 0x16, 0x05, 0x1c, 0x00, 0x00, 0x16,
0x04, 0x1a, 0x01, 0x16, 0x05, 0x12, 0x13 /* Ring type 10 to 17 */ , 0x18, 0x16, 0x04, 0x18, /* volume 00, 10, 20... */
0x20, 0x16, 0x04, 0x10, 0x00
};
//static const unsigned char packet_send_end_call[] =
// { 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff, 0x16, 0x05, 0x31, 0x00, 0x00, /* Headset LED off */ 0x19, 0x04, 0x00,
//0x10, /* Mute LED off */ 0x19, 0x04, 0x00, 0x18,/* Stream unmute */ 0x16, 0x05, 0x04, 0x00, 0x00, /* Query RTCP */ 0x16, 0x04, 0x37, 0x10 };
static const unsigned char packet_send_end_call[] =
{ 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff, 0x16, 0x05, 0x31, 0x00, 0x00, /* Query RTCP */ 0x16, 0x04, 0x37, 0x10 };
static const unsigned char packet_send_s9[] =
{ 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff, 0x19, 0x04, 0x00, 0x10, 0x16, 0x05, 0x1c, 0x00,
0x00 };
static const unsigned char packet_send_rtp_packet_size[] =
{ 0x16, 0x08, 0x38, 0x00, 0x00, 0xe0, 0x00, 0xa0 };
static const unsigned char packet_send_jitter_buffer_conf[] =
{ 0x16, 0x0e, 0x3a, 0x00, /* jitter */ 0x02, /* high water mark */ 0x04, 0x00, 0x00,
/* early packet resync 2 bytes */ 0x3e, 0x80,
0x00, 0x00, /* late packet resync 2 bytes */ 0x3e, 0x80
};
/* Duration in ms div 2 (0x20 = 64ms, 0x08 = 16ms)
static unsigned char packet_send_StreamBasedToneCad[] =
{ 0x16, 0x0a, 0x1e, 0x00, duration on 0x0a, duration off 0x0d, duration on 0x0a, duration off 0x0d, duration on 0x0a, duration off 0x2b }; */
static const unsigned char packet_send_open_audio_stream_rx[] =
{ 0x16, 0x1a, 0x30, 0x00, 0xff, /* Codec */ 0x00, 0x00, 0x01, 0x00, 0xb8, 0xb8, 0x0e,
0x0e, 0x01, /* Port */ 0x14, 0x50, 0x00,
0x00, /* Port */ 0x14, 0x50, 0x00, 0x00, /* Dest IP */ 0x0a, 0x93, 0x69, 0x05
};
static const unsigned char packet_send_open_audio_stream_tx[] =
{ 0x16, 0x1a, 0x30, 0xff, 0x00, 0x00, /* Codec */ 0x00, 0x01, 0x00, 0xb8, 0xb8, 0x0e,
0x0e, 0x01, /* Local port */ 0x14, 0x50,
0x00, 0x00, /* Rmt Port */ 0x14, 0x50, 0x00, 0x00, /* Dest IP */ 0x0a, 0x93, 0x69, 0x05
};
static const unsigned char packet_send_open_audio_stream_rx3[] =
{ 0x16, 0x1a, 0x30, 0x00, 0xff, /* Codec */ 0x00, 0x00, 0x02, 0x01, 0xb8, 0xb8, 0x06,
0x06, 0x81, /* RTP Port */ 0x14, 0x50,
/* RTCP Port */ 0x14,
0x51, /* RTP Port */ 0x14, 0x50, /* RTCP Port */ 0x00, 0x00, /* Dest IP */ 0x0a, 0x93,
0x69, 0x05
};
static const unsigned char packet_send_open_audio_stream_tx3[] =
{ 0x16, 0x1a, 0x30, 0xff, 0x00, 0x00, /* Codec */ 0x00, 0x02, 0x01, 0xb8, 0xb8, 0x06,
0x06, 0x81, /* RTP Local port */ 0x14, 0x50,
/* RTCP Port */ 0x00, 0x00, /* RTP Rmt Port */ 0x14, 0x50, /* RTCP Port */ 0x00, 0x00,
/* Dest IP */ 0x0a, 0x93, 0x69, 0x05
};
static const unsigned char packet_send_arrow[] = { 0x17, 0x04, 0x04, 0x00 };
static const unsigned char packet_send_blink_cursor[] = { 0x17, 0x04, 0x10, 0x86 };
static const unsigned char packet_send_date_time2[] = { 0x17, 0x04, 0x17, 0x3d, 0x11, 0x09, 0x02, 0x0a, /*Month */ 0x05, /*Day */
0x06, /*Hour */ 0x07, /*Minutes */ 0x08, 0x32
};
static const unsigned char packet_send_Contrast[] =
{ 0x17, 0x04, 0x24, /*Contrast */ 0x08 };
static const unsigned char packet_send_start_timer[] =
{ 0x17, 0x05, 0x0b, /*Timer option*/0x05, /* Timer ID */0x00, 0x17, 0x08, 0x16,
/* Text */ 'T', 'i', 'm', 'e', 'r' };
static const unsigned char packet_send_stop_timer[] = { 0x17, 0x05, 0x0b, 0x02, 0x00 };
static const unsigned char packet_send_icon[] = { 0x17, 0x05, 0x14, /*pos */ 0x00, /*icon */ 0x25 }; /* display an icon in front of the text zone */
static const unsigned char packet_send_S7[] = { 0x17, 0x06, 0x0f, 0x30, 0x07, 0x07 };
static const unsigned char packet_send_set_pos_cursor[] =
{ 0x17, 0x06, 0x10, 0x81, 0x04, /*pos */ 0x20 };
static unsigned char monthlabels[] =
{ 'J', 'a', 'n', 'F', 'e', 'b', 'M', 'a', 'r', 'A', 'p', 'r', 'M', 'a', 'y', 'J', 'u', 'n',
'J', 'u', 'l', 'A', 'u', 'g', 'S', 'e', 'p', 'O', 'c', 't', 'N', 'o', 'v', 'D', 'e', 'c' };
static unsigned char packet_send_monthlabels_download[] =
{ 0x17, 0x0a, 0x15, /* Month (3 char) */ '-', '-', '-', '-', '-', '-', 0x20 };
static const unsigned char packet_send_favorite[] =
{ 0x17, 0x0f, 0x19, 0x10, /*pos */ 0x01, /*name */ 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, /*end_name */ 0x19,
0x05, 0x0f, /*pos */ 0x01, /*icone */ 0x00
};
static const unsigned char packet_send_title[] =
{ 0x17, 0x10, 0x19, 0x02, /*text */ 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20 /*end_text */ };
static const unsigned char packet_send_text[] =
{ 0x17, 0x1e, 0x1b, 0x04, /*pos */ 0x00, /*inverse */ 0x25, /*text */ 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
/*end_text */ 0x17, 0x04, 0x10, 0x87
};
static const unsigned char packet_send_status[] =
{ 0x17, 0x20, 0x19, 0x08, /*text */ 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 /*end_text */
};
static const unsigned char packet_send_status2[] =
{ 0x17, 0x0b, 0x19, /* pos [08|28|48|68] */ 0x00, /* text */ 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20 /* end_text */ };
/* Multiple character set support */
/* ISO-8859-1 - Western European) */
static const unsigned char packet_send_charset_iso_8859_1[] =
{ 0x17, 0x08, 0x21, 0x1b, 0x2d, 0x41, 0x1b, 0x00 };
/* ISO-8859-2 - Central European) */
static const unsigned char packet_send_charset_iso_8859_2[] =
{ 0x17, 0x08, 0x21, 0x1b, 0x2d, 0x42, 0x1b, 0x00 };
/* ISO-8859-4 - Baltic) */
static const unsigned char packet_send_charset_iso_8859_4[] =
{ 0x17, 0x08, 0x21, 0x1b, 0x2d, 0x44, 0x1b, 0x00 };
/* ISO 8859-5 - cyrilic */
static const unsigned char packet_send_charset_iso_8859_5[] =
{ 0x17, 0x08, 0x21, 0x1b, 0x2d, 0x4c, 0x1b, 0x00 };
/* Japaneese (ISO-2022-JP ?) */
static const unsigned char packet_send_charset_iso_2022_jp[] =
{ 0x17, 0x08, 0x21, 0x1b, 0x29, 0x49, 0x1b, 0x7e };
static const unsigned char packet_send_led_update[] = { 0x19, 0x04, 0x00, 0x00 };
static const unsigned char packet_send_query_basic_manager_04[] = { 0x1a, 0x04, 0x01, 0x04 };
static const unsigned char packet_send_query_mac_address[] = { 0x1a, 0x04, 0x01, 0x08 };
static const unsigned char packet_send_query_basic_manager_10[] = { 0x1a, 0x04, 0x01, 0x10 };
static const unsigned char packet_send_S1[] = { 0x1a, 0x07, 0x07, 0x00, 0x00, 0x00, 0x13 };
static unsigned char packet_send_ping[] =
{ 0x1e, 0x05, 0x12, 0x00, /*Watchdog timer */ 0x78 };
#define BUFFSEND unsigned char buffsend[64] = { 0x00, 0x00, 0xaa, 0xbb, 0x02, 0x01 }
static const char tdesc[] = "UNISTIM Channel Driver";
static const char channel_type[] = "USTM";
/*! Protos */
static struct ast_channel *unistim_new(struct unistim_subchannel *sub, int state, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor);
static int load_module(void);
static int reload(void);
static int unload_module(void);
static int reload_config(void);
static void unistim_set_owner(struct unistim_subchannel *sub, struct ast_channel *chan);
static void show_main_page(struct unistimsession *pte);
static struct ast_channel *unistim_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);
static int unistim_call(struct ast_channel *ast, const char *dest, int timeout);
static int unistim_hangup(struct ast_channel *ast);
static int unistim_answer(struct ast_channel *ast);
static struct ast_frame *unistim_read(struct ast_channel *ast);
static int unistim_write(struct ast_channel *ast, struct ast_frame *frame);
static int unistim_indicate(struct ast_channel *ast, int ind, const void *data,
size_t datalen);
static int unistim_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
static int unistim_senddigit_begin(struct ast_channel *ast, char digit);
static int unistim_senddigit_end(struct ast_channel *ast, char digit,
unsigned int duration);
static int unistim_sendtext(struct ast_channel *ast, const char *text);
static int write_entry_history(struct unistimsession *pte, FILE * f, char c,
char *line1);
static void change_callerid(struct unistimsession *pte, int type, char *callerid);
static struct ast_channel_tech unistim_tech = {
.type = channel_type,
.description = tdesc,
.properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
.requester = unistim_request,
.call = unistim_call,
.hangup = unistim_hangup,
.answer = unistim_answer,
.read = unistim_read,
.write = unistim_write,
.indicate = unistim_indicate,
.fixup = unistim_fixup,
.send_digit_begin = unistim_senddigit_begin,
.send_digit_end = unistim_senddigit_end,
.send_text = unistim_sendtext,
};
static void send_start_rtp(struct unistim_subchannel *);
static void send_callerid_screen(struct unistimsession *, struct unistim_subchannel *);
static void key_favorite(struct unistimsession *, char);
static void handle_select_codec(struct unistimsession *);
static void handle_select_language(struct unistimsession *);
static int find_language(const char*);
static int unistim_free_sub(struct unistim_subchannel *);
static struct unistim_menu_item options_menu[] =
{
{"Change codec", STATE_SELECTCODEC, handle_select_codec},
{"Language", STATE_SELECTLANGUAGE, handle_select_language},
{NULL, 0, NULL}
};
static struct unistim_languages options_languages[] =
{
{"English", "en", ISO_8859_1, NULL},
{"French", "fr", ISO_8859_1, NULL},
{"Russian", "ru", ISO_8859_5, NULL},
{NULL, NULL, 0, NULL}
};
static char ustm_strcopy[1024];
struct ustm_lang_entry {
const char *str_orig;
const char *str_trans;
};
static int lang_hash_fn(const void *obj, const int flags)
{
const struct ustm_lang_entry *entry = obj;
return ast_str_hash(entry->str_orig);
}
static int lang_cmp_fn(void *obj, void *arg, int flags)
{
struct ustm_lang_entry *entry1 = obj;
struct ustm_lang_entry *entry2 = arg;
return (!strcmp(entry1->str_orig, entry2->str_orig)) ? (CMP_MATCH | CMP_STOP) : 0;
}
static const char *ustmtext(const char *str, struct unistimsession *pte)
{
struct ustm_lang_entry *lang_entry;
struct ustm_lang_entry le_search;
struct unistim_languages *lang = NULL;
int size;
if (pte->device) {
lang = &options_languages[find_language(pte->device->language)];
}
if (!lang) {
return str;
}
/* Check if specified language exists */
if (!lang->trans) {
char tmp[1024], *p, *p_orig = NULL, *p_trans = NULL;
FILE *f;
lang->trans = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 8,
lang_hash_fn, NULL, lang_cmp_fn);
if (!lang->trans) {
ast_log(LOG_ERROR, "Unable to allocate container for translation!\n");
return str;
}
snprintf(tmp, sizeof(tmp), "%s/%s/%s.po", ast_config_AST_VAR_DIR,
USTM_LANG_DIR, lang->lang_short);
f = fopen(tmp, "r");
if (!f) {
ast_log(LOG_WARNING, "There is no translation file for '%s'\n", lang->lang_short);
return str;
}
while (fgets(tmp, sizeof(tmp), f)) {
if (!(p = strchr(tmp, '\n'))) {
ast_log(LOG_ERROR, "Too long line found in language file - truncated!\n");
continue;
}
*p = '\0';
if (!(p = strchr(tmp, '"'))) {
continue;
}
if (tmp == strstr(tmp, "msgid")) {
p_orig = ast_strdup(p + 1);
p = strchr(p_orig, '"');
} else if (tmp == strstr(tmp, "msgstr")) {
p_trans = ast_strdup(p + 1);
p = strchr(p_trans, '"');
} else {
continue;
}
*p = '\0';
if (!p_trans || !p_orig) {
continue;
}
if (ast_strlen_zero(p_trans)) {
ast_free(p_trans);
ast_free(p_orig);
p_trans = NULL;
p_orig = NULL;
continue;
}
if (!(lang_entry = ao2_alloc(sizeof(*lang_entry), NULL))) {
fclose(f);
return str;
}
lang_entry->str_trans = p_trans;
lang_entry->str_orig = p_orig;
ao2_link(lang->trans, lang_entry);
p_trans = NULL;
p_orig = NULL;
}
fclose(f);
}
le_search.str_orig = str;
if ((lang_entry = ao2_find(lang->trans, &le_search, OBJ_POINTER))) {
size = strlen(lang_entry->str_trans)+1;
if (size > 1024) {
size = 1024;
}
memcpy(ustm_strcopy, lang_entry->str_trans, size);
ao2_ref(lang_entry, -1);
return ustm_strcopy;
}
return str;
}
static void display_last_error(const char *sz_msg)
{
/* Display the error message */
ast_log(LOG_WARNING, "%s : (%d) %s\n", sz_msg, errno, strerror(errno));
}
static unsigned int get_tick_count(void)
{
struct timeval now = ast_tvnow();
return (now.tv_sec * 1000) + (now.tv_usec / 1000);
}
/* Send data to a phone without retransmit nor buffering */
static void send_raw_client(int size, const unsigned char *data, struct sockaddr_in *addr_to,
const struct sockaddr_in *addr_ourip)
{
#ifdef HAVE_PKTINFO
struct iovec msg_iov;
struct msghdr msg;
char buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
struct cmsghdr *ip_msg = (struct cmsghdr *) buffer;
struct in_pktinfo *pki = (struct in_pktinfo *) CMSG_DATA(ip_msg);
/* cast this to a non-const pointer, since the sendmsg() API
* does not provide read-only and write-only flavors of the
* structures used for its arguments, but in this case we know
* the data will not be modified
*/
msg_iov.iov_base = (char *) data;
msg_iov.iov_len = size;
msg.msg_name = addr_to; /* optional address */
msg.msg_namelen = sizeof(struct sockaddr_in); /* size of address */
msg.msg_iov = &msg_iov; /* scatter/gather array */
msg.msg_iovlen = 1; /* # elements in msg_iov */
msg.msg_control = ip_msg; /* ancillary data */
msg.msg_controllen = sizeof(buffer); /* ancillary data buffer len */
msg.msg_flags = 0; /* flags on received message */
ip_msg->cmsg_len = CMSG_LEN(sizeof(*pki));
ip_msg->cmsg_level = IPPROTO_IP;
ip_msg->cmsg_type = IP_PKTINFO;
pki->ipi_ifindex = 0; /* Interface index, 0 = use interface specified in routing table */
pki->ipi_spec_dst.s_addr = addr_ourip->sin_addr.s_addr; /* Local address */
/* pki->ipi_addr = ; Header Destination address - ignored by kernel */
#ifdef DUMP_PACKET
if (unistimdebug) {
int tmp;
ast_verb(0, "\n**> From %s sending %d bytes to %s ***\n",
ast_inet_ntoa(addr_ourip->sin_addr), (int) size,
ast_inet_ntoa(addr_to->sin_addr));
for (tmp = 0; tmp < size; tmp++)
ast_verb(0, "%02hhx ", data[tmp]);
ast_verb(0, "\n******************************************\n");
}
#endif
if (sendmsg(unistimsock, &msg, 0) == -1) {
display_last_error("Error sending datas");
}
#else
if (sendto(unistimsock, data, size, 0, (struct sockaddr *) addr_to, sizeof(*addr_to))
== -1)
display_last_error("Error sending datas");
#endif
}
static void send_client(int size, const unsigned char *data, struct unistimsession *pte)
{
unsigned int tick;
int buf_pos;
unsigned short seq = ntohs(++pte->seq_server);
ast_mutex_lock(&pte->lock);
buf_pos = pte->last_buf_available;
if (buf_pos >= MAX_BUF_NUMBER) {
ast_log(LOG_WARNING, "Error : send queue overflow\n");
ast_mutex_unlock(&pte->lock);
return;
}
memcpy((void *)data + sizeof(unsigned short), (void *)&seq, sizeof(unsigned short));
pte->wsabufsend[buf_pos].len = size;
memcpy(pte->wsabufsend[buf_pos].buf, data, size);
tick = get_tick_count();
pte->timeout = tick + RETRANSMIT_TIMER;
/*#ifdef DUMP_PACKET */
if (unistimdebug) {
ast_verb(0, "Sending datas with seq #0x%04x Using slot #%d :\n", (unsigned)pte->seq_server, buf_pos);
}
/*#endif */
send_raw_client(pte->wsabufsend[buf_pos].len, pte->wsabufsend[buf_pos].buf, &(pte->sin),
&(pte->sout));
pte->last_buf_available++;
ast_mutex_unlock(&pte->lock);
}
static void send_ping(struct unistimsession *pte)
{
BUFFSEND;
if (unistimdebug) {
ast_verb(0, "Sending ping\n");
}
pte->tick_next_ping = get_tick_count() + unistim_keepalive;
memcpy(buffsend + SIZE_HEADER, packet_send_ping, sizeof(packet_send_ping));
send_client(SIZE_HEADER + sizeof(packet_send_ping), buffsend, pte);
}
static int get_to_address(int fd, struct sockaddr_in *toAddr)
{
#ifdef HAVE_PKTINFO
int err;
struct msghdr msg;
struct {
struct cmsghdr cm;
int len;
struct in_addr address;
} ip_msg;
/* Zero out the structures before we use them */
/* This sets several key values to NULL */
memset(&msg, 0, sizeof(msg));
memset(&ip_msg, 0, sizeof(ip_msg));
/* Initialize the message structure */
msg.msg_control = &ip_msg;
msg.msg_controllen = sizeof(ip_msg);
/* Get info about the incoming packet */
err = recvmsg(fd, &msg, MSG_PEEK);
if (err == -1) {
ast_log(LOG_WARNING, "recvmsg returned an error: %s\n", strerror(errno));
}
memcpy(&toAddr->sin_addr, &ip_msg.address, sizeof(struct in_addr));
return err;
#else
memcpy(toAddr, &public_ip, sizeof(*toAddr));
return 0;
#endif
}
/* Allocate memory & initialize structures for a new phone */
/* addr_from : ip address of the phone */
static struct unistimsession *create_client(const struct sockaddr_in *addr_from)
{
int tmp;
struct unistimsession *s;
if (!(s = ast_calloc(1, sizeof(*s))))
return NULL;
memcpy(&s->sin, addr_from, sizeof(struct sockaddr_in));
get_to_address(unistimsock, &s->sout);
s->sout.sin_family = AF_INET;
if (unistimdebug) {
ast_verb(0, "Creating a new entry for the phone from %s received via server ip %s\n",
ast_inet_ntoa(addr_from->sin_addr), ast_inet_ntoa(s->sout.sin_addr));
}
ast_mutex_init(&s->lock);
ast_mutex_lock(&sessionlock);
s->next = sessions;
sessions = s;
s->timeout = get_tick_count() + RETRANSMIT_TIMER;
s->state = STATE_INIT;
s->tick_next_ping = get_tick_count() + unistim_keepalive;
/* Initialize struct wsabuf */
for (tmp = 0; tmp < MAX_BUF_NUMBER; tmp++) {
s->wsabufsend[tmp].buf = s->buf[tmp];
}
ast_mutex_unlock(&sessionlock);
return s;
}
static void send_end_call(struct unistimsession *pte)
{
BUFFSEND;
if (unistimdebug) {
ast_verb(0, "Sending end call\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_end_call, sizeof(packet_send_end_call));
send_client(SIZE_HEADER + sizeof(packet_send_end_call), buffsend, pte);
}
static void set_ping_timer(struct unistimsession *pte)
{
unsigned int tick = 0; /* XXX what is this for, anyways */
pte->timeout = pte->tick_next_ping;
DEBUG_TIMER("tick = %u next ping at %u tick\n", tick, pte->timeout);
return;
}
/* Checking if our send queue is empty,
* if true, setting up a timer for keepalive */
static void check_send_queue(struct unistimsession *pte)
{
/* Check if our send queue contained only one element */
if (pte->last_buf_available == 1) {
if (unistimdebug) {
ast_verb(0, "Our single packet was ACKed.\n");
}
pte->last_buf_available--;
set_ping_timer(pte);
return;
}
/* Check if this ACK catch up our latest packet */
else if (pte->last_seq_ack + 1 == pte->seq_server + 1) {
if (unistimdebug) {
ast_verb(0, "Our send queue is completely ACKed.\n");
}
pte->last_buf_available = 0; /* Purge the send queue */
set_ping_timer(pte);
return;
}
if (unistimdebug) {
ast_verb(0, "We still have packets in our send queue\n");
}
return;
}
static void send_start_timer(struct unistimsession *pte)
{
BUFFSEND;
if (unistimdebug) {
ast_verb(0, "Sending start timer\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_start_timer, sizeof(packet_send_start_timer));
send_client(SIZE_HEADER + sizeof(packet_send_start_timer), buffsend, pte);
}
static void send_stop_timer(struct unistimsession *pte)
{
BUFFSEND;
if (unistimdebug) {
ast_verb(0, "Sending stop timer\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_stop_timer, sizeof(packet_send_stop_timer));
send_client(SIZE_HEADER + sizeof(packet_send_stop_timer), buffsend, pte);
}
static void send_icon(unsigned char pos, unsigned char status, struct unistimsession *pte)
{
BUFFSEND;
if (unistimdebug) {
ast_verb(0, "Sending icon pos %d with status 0x%02hhx\n", pos, status);
}
memcpy(buffsend + SIZE_HEADER, packet_send_icon, sizeof(packet_send_icon));
buffsend[9] = pos;
buffsend[10] = status;
send_client(SIZE_HEADER + sizeof(packet_send_icon), buffsend, pte);
}
static void send_expansion_next(struct unistimsession *pte)
{
BUFFSEND;
memcpy(buffsend + SIZE_HEADER, packet_send_expansion_next, sizeof(packet_send_expansion_next));
send_client(SIZE_HEADER + sizeof(packet_send_expansion_next), buffsend, pte);
}
static void send_expansion_icon(unsigned char pos, unsigned char status, struct unistimsession *pte)
{
BUFFSEND;
if (unistimdebug) {
ast_verb(0, "Sending expansion icon pos %d with status 0x%02hhx\n", pos, status);
}
memcpy(buffsend + SIZE_HEADER, packet_send_expansion_icon, sizeof(packet_send_expansion_icon));
buffsend[10] = pos;
buffsend[11] = status;
send_client(SIZE_HEADER + sizeof(packet_send_expansion_icon), buffsend, pte);
}
/* inverse : TEXT_INVERSE : yes, TEXT_NORMAL : no */
static void send_expansion_text(unsigned char pos, struct unistimsession *pte, const char *text)
{
int i;
BUFFSEND;
if (!text) {
ast_log(LOG_ERROR, "[expansion] Asked to display NULL text (pos %d)\n", pos);
return;
}
if (unistimdebug) {
ast_verb(0, "[expansion] Sending text at pos %d\n", pos);
}
memcpy(buffsend + SIZE_HEADER, packet_send_expansion_text, sizeof(packet_send_expansion_text));
buffsend[10] = pos;
i = strlen(text);
if (i > TEXT_LENGTH_MAX) {
i = TEXT_LENGTH_MAX;
}
memcpy(buffsend + 11, text, i);
send_client(SIZE_HEADER + sizeof(packet_send_expansion_text), buffsend, pte);
}
static void send_tone(struct unistimsession *pte, uint16_t tone1, uint16_t tone2)
{
BUFFSEND;
if (!tone1) {
if (unistimdebug) {
ast_verb(0, "Sending Stream Based Tone Off\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_off,
sizeof(packet_send_stream_based_tone_off));
send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_off), buffsend, pte);
return;
}
/* Since most of the world use a continuous tone, it's useless
if (unistimdebug)
ast_verb(0, "Sending Stream Based Tone Cadence Download\n");
memcpy (buffsend + SIZE_HEADER, packet_send_StreamBasedToneCad, sizeof (packet_send_StreamBasedToneCad));
send_client (SIZE_HEADER + sizeof (packet_send_StreamBasedToneCad), buffsend, pte); */
if (unistimdebug) {
ast_verb(0, "Sending Stream Based Tone Frequency Component List Download %d %d\n", tone1, tone2);
}
tone1 *= 8;
if (!tone2) {
memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_single_freq,
sizeof(packet_send_stream_based_tone_single_freq));
buffsend[10] = (tone1 & 0xff00) >> 8;
buffsend[11] = (tone1 & 0x00ff);
send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_single_freq), buffsend,
pte);
} else {
tone2 *= 8;
memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_dial_freq,
sizeof(packet_send_stream_based_tone_dial_freq));
buffsend[10] = (tone1 & 0xff00) >> 8;
buffsend[11] = (tone1 & 0x00ff);
buffsend[12] = (tone2 & 0xff00) >> 8;
buffsend[13] = (tone2 & 0x00ff);
send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_dial_freq), buffsend,
pte);
}
if (unistimdebug) {
ast_verb(0, "Sending Stream Based Tone On\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_on,
sizeof(packet_send_stream_based_tone_on));
send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_on), buffsend, pte);
}
/* Positions for favorites
|--------------------|
| 5 2 | <-- not on screen in i2002
| 4 1 |
| 3 0 |
KEM Positions
|--------------------|
| 12 24 |
| 11 23 |
| 10 22 |
| 9 21 |
| 8 20 |
| 7 19 |
| 6 18 |
| 5 17 |
| 4 16 |
| 3 15 |
| 2 14 |
| 1 13 |
*/
/* status (icons) : 00 = nothing, 2x/3x = see parser.h, 4x/5x = blink fast, 6x/7x = blink slow */
static void
send_favorite(unsigned char pos, unsigned char status, struct unistimsession *pte,
const char *text)
{
BUFFSEND;
int i;
if (unistimdebug) {
ast_verb(0, "Sending favorite pos %d with status 0x%02hhx\n", pos, status);
}
memcpy(buffsend + SIZE_HEADER, packet_send_favorite, sizeof(packet_send_favorite));
buffsend[10] = pos;
buffsend[24] = pos;
buffsend[25] = status;
i = strlen(ustmtext(text, pte));
if (i > FAV_MAX_LENGTH) {
i = FAV_MAX_LENGTH;
}
memcpy(buffsend + FAV_MAX_LENGTH + 1, ustmtext(text, pte), i);
send_client(SIZE_HEADER + sizeof(packet_send_favorite), buffsend, pte);
}
static void send_favorite_short(unsigned char pos, unsigned char status, struct unistimsession *pte) {
send_favorite(pos, status, pte, pte->device->softkeylabel[pos]);
return;
}
static void send_favorite_selected(unsigned char status, struct unistimsession *pte) {
if (pte->device->selected != -1) {
send_favorite(pte->device->selected, status, pte, pte->device->softkeylabel[pte->device->selected]);
}
return;
}
static void send_expansion_short(unsigned char pos, unsigned char status, struct unistimsession *pte) {
send_expansion_icon(pos, status, pte);
send_expansion_text(pos, pte, ustmtext(pte->device->expsoftkeylabel[pos], pte));
send_expansion_next(pte);
return;
}
static int soft_key_visible(struct unistim_device* d, unsigned char num)
{
if(d->height == 1 && num % 3 == 2) {
return 0;
}
return 1;
}
static void refresh_all_favorite(struct unistimsession *pte)
{
unsigned char i = 0;
char data[256];
struct unistim_line *line;
line = AST_LIST_FIRST(&pte->device->lines);
if (unistimdebug) {
ast_verb(0, "Refreshing all favorite\n");
}
for (i = 0; i < FAVNUM; i++) {
unsigned char status = pte->device->softkeyicon[i];
if (!soft_key_visible(pte->device, i)) {
continue;
}
if (!strcasecmp(pte->device->softkeylabel[i], "DND") && line) {
if (!ast_db_get("DND", line->name, data, sizeof(data))) {
status = FAV_ICON_SPEAKER_ONHOOK_WHITE;
}
}
send_favorite_short(i, status, pte);
}
if (pte->device->hasexp) {
for (i = 0; i < EXPNUM; i++) {
send_expansion_short(i, FAV_ICON_NONE, pte);
}
}
}
static int is_key_favorite(struct unistim_device *d, int fav)
{
if ((fav < 0) && (fav > 5)) {
return 0;
}
if (d->sline[fav]) {
return 0;
}
if (d->softkeynumber[fav][0] == '\0') {
return 0;
}
return 1;
}
static int is_key_line(struct unistim_device *d, int fav)
{
if ((fav < 0) && (fav > 5)) {
return 0;
}
if (!d->sline[fav]) {
return 0;
}
if (is_key_favorite(d, fav)) {
return 0;
}
return 1;
}
static int get_active_softkey(struct unistimsession *pte)
{
return pte->device->selected;
}
static int get_avail_softkey(struct unistimsession *pte, const char* name)
{
int i;
if (!is_key_line(pte->device, pte->device->selected)) {
pte->device->selected = -1;
}
for (i = 0; i < FAVNUM; i++) {
if (pte->device->selected != -1 && pte->device->selected != i) {
continue;
}
if (!soft_key_visible(pte->device, i)) {
continue;
}
if (pte->device->ssub[i]) {
continue;
}
if (is_key_line(pte->device, i)) {
if (name && strcmp(name, pte->device->sline[i]->name)) {
continue;
}
if (unistimdebug) {
ast_verb(0, "Found softkey %d for device %s\n", i, name);
}
return i;
}
}
return -1;
}
/* Change the status for this phone (pte) and update for each phones where pte is bookmarked
* use FAV_ICON_*_BLACK constant in status parameters */
static void change_favorite_icon(struct unistimsession *pte, unsigned char status)
{
struct unistim_device *d = devices;
int i;
/* Update the current phone line softkey icon */
if (pte->state != STATE_CLEANING) {
int softkeylinepos = get_active_softkey(pte);
if (softkeylinepos != -1) {
send_favorite_short(softkeylinepos, status, pte);
}
}
/* Notify other phones if we're in their bookmark */
while (d) {
for (i = 0; i < FAVNUM; i++) {
if (d->sp[i] == pte->device) { /* It's us ? */
if (d->softkeyicon[i] != status) { /* Avoid resending the same icon */
d->softkeyicon[i] = status;
if (d->session) {
send_favorite(i, status + 1, d->session, d->softkeylabel[i]);
}
}
}
}
d = d->next;
}
}
static int register_extension(const struct unistimsession *pte)
{
struct unistim_line *line;
line = AST_LIST_FIRST(&pte->device->lines);
if (unistimdebug) {
ast_verb(0, "Trying to register extension '%s' into context '%s' to %s\n",
pte->device->extension_number, pte->device->context,
line->fullname);
}
return ast_add_extension(pte->device->context, 0,
pte->device->extension_number, 1, NULL, NULL, "Dial",
line->fullname, 0, "Unistim");
}
static int unregister_extension(const struct unistimsession *pte)
{
if (unistimdebug) {
ast_verb(0, "Trying to unregister extension '%s' context '%s'\n",
pte->device->extension_number, pte->device->context);
}
return ast_context_remove_extension(pte->device->context,
pte->device->extension_number, 1, "Unistim");
}
/* Free memory allocated for a phone */
static void close_client(struct unistimsession *s)
{
struct unistim_subchannel *sub = NULL;
struct unistimsession *cur, *prev = NULL;
ast_mutex_lock(&sessionlock);
cur = sessions;
/* Looking for the session in the linked chain */
while (cur) {
if (cur == s) {
break;
}
prev = cur;
cur = cur->next;
}
if (cur) { /* Session found ? */
if (cur->device) { /* This session was registered ? */
s->state = STATE_CLEANING;
if (unistimdebug) {
ast_verb(0, "close_client session %p device %p\n", s, s->device);
}
change_favorite_icon(s, FAV_ICON_NONE);
ast_mutex_lock(&s->device->lock);
AST_LIST_LOCK(&s->device->subs);
AST_LIST_TRAVERSE_SAFE_BEGIN(&s->device->subs, sub, list) {
if (!sub) {
continue;
}
if (sub->owner) { /* Call in progress ? */
if (unistimdebug) {
ast_verb(0, "Aborting call\n");
}
ast_queue_hangup_with_cause(sub->owner, AST_CAUSE_NETWORK_OUT_OF_ORDER);
} else {
if (unistimdebug) {
ast_debug(1, "Released sub %u of channel %s@%s\n", sub->subtype, sub->parent->name, s->device->name);
}
AST_LIST_REMOVE_CURRENT(list);
unistim_free_sub(sub);
}
}
AST_LIST_TRAVERSE_SAFE_END;
AST_LIST_UNLOCK(&s->device->subs);
if (!ast_strlen_zero(s->device->extension_number)) {
unregister_extension(s);
}
cur->device->session = NULL;
ast_mutex_unlock(&s->device->lock);
} else {
if (unistimdebug) {
ast_verb(0, "Freeing an unregistered client\n");
}
}
if (prev) {
prev->next = cur->next;
} else {
sessions = cur->next;
}
ast_mutex_destroy(&s->lock);
ast_free(s);
} else {
ast_log(LOG_WARNING, "Trying to delete non-existent session %p?\n", s);
}
ast_mutex_unlock(&sessionlock);
return;
}
/* Return 1 if the session chained link was modified */
static int send_retransmit(struct unistimsession *pte)
{
int i;
ast_mutex_lock(&pte->lock);
if (++pte->nb_retransmit >= NB_MAX_RETRANSMIT) {
if (unistimdebug) {
ast_verb(0, "Too many retransmit - freeing client\n");
}
ast_mutex_unlock(&pte->lock);
close_client(pte);
return 1;
}
pte->timeout = get_tick_count() + RETRANSMIT_TIMER;
for (i = pte->last_buf_available - (pte->seq_server - pte->last_seq_ack);
i < pte->last_buf_available; i++) {
if (i < 0) {
ast_log(LOG_WARNING,
"Asked to retransmit an ACKed slot ! last_buf_available=%d, seq_server = #0x%04x last_seq_ack = #0x%04x\n",
pte->last_buf_available, (unsigned)pte->seq_server, (unsigned)pte->last_seq_ack);
continue;
}
if (unistimdebug) {
unsigned short *sbuf = (unsigned short *) pte->wsabufsend[i].buf;
unsigned short seq;
seq = ntohs(sbuf[1]);
ast_verb(0, "Retransmit slot #%d (seq=#0x%04x), last ack was #0x%04x\n", i,
(unsigned)seq, (unsigned)pte->last_seq_ack);
}
send_raw_client(pte->wsabufsend[i].len, pte->wsabufsend[i].buf, &pte->sin,
&pte->sout);
}
ast_mutex_unlock(&pte->lock);
return 0;
}
/* inverse : TEXT_INVERSE : yes, TEXT_NORMAL : no */
static void send_text(unsigned char pos, unsigned char inverse, struct unistimsession *pte,
const char *text)
{
int i;
BUFFSEND;
if (!text) {
ast_log(LOG_ERROR, "Asked to display NULL text (pos %d, inverse flag %d)\n", pos, inverse);
return;
}
if (pte->device && pte->device->height == 1 && pos != TEXT_LINE0) {
return;
}
if (unistimdebug) {
ast_verb(0, "Sending text at pos %d, inverse flag %d\n", pos, inverse);
}
memcpy(buffsend + SIZE_HEADER, packet_send_text, sizeof(packet_send_text));
buffsend[10] = pos;
buffsend[11] = inverse;
i = strlen(text);
if (i > TEXT_LENGTH_MAX) {
i = TEXT_LENGTH_MAX;
}
memcpy(buffsend + 12, text, i);
send_client(SIZE_HEADER + sizeof(packet_send_text), buffsend, pte);
}
static void send_text_status(struct unistimsession *pte, const char *text)
{
BUFFSEND;
int i;
if (unistimdebug) {
ast_verb(0, "Sending status text\n");
}
if (pte->device) {
if (pte->device->status_method == 1) { /* For new firmware and i2050 soft phone */
int n = strlen(text);
/* Must send individual button separately */
int j;
for (i = 0, j = 0; i < 4; i++, j += 7) {
int pos = 0x08 + (i * 0x20);
memcpy(buffsend + SIZE_HEADER, packet_send_status2,
sizeof(packet_send_status2));
buffsend[9] = pos;
memcpy(buffsend + 10, (j < n) ? (text + j) : " ", 7);
send_client(SIZE_HEADER + sizeof(packet_send_status2), buffsend, pte);
}
return;
}
}
memcpy(buffsend + SIZE_HEADER, packet_send_status, sizeof(packet_send_status));
i = strlen(text);
if (i > STATUS_LENGTH_MAX) {
i = STATUS_LENGTH_MAX;
}
memcpy(buffsend + 10, text, i);
send_client(SIZE_HEADER + sizeof(packet_send_status), buffsend, pte);
}
static void send_led_update(struct unistimsession *pte, unsigned char led)
{
BUFFSEND;
if (unistimdebug) {
ast_verb(0, "Sending led_update (%x)\n", (unsigned)led);
}
memcpy(buffsend + SIZE_HEADER, packet_send_led_update, sizeof(packet_send_led_update));
buffsend[9] = led;
send_client(SIZE_HEADER + sizeof(packet_send_led_update), buffsend, pte);
}
static void send_mute(struct unistimsession *pte, unsigned char mute)
{
/*
0x00 = unmute TX, 0x01 = mute TX
0x20 = unmute RX, 0x21 = mute RX
*/
BUFFSEND;
if (unistimdebug) {
ast_verb(0, "Sending mute packet (%x)\n", (unsigned)mute);
}
memcpy(buffsend + SIZE_HEADER, packet_send_mute, sizeof(packet_send_mute));
buffsend[9] = mute;
send_client(SIZE_HEADER + sizeof(packet_send_mute), buffsend, pte);
}
/* output = OUTPUT_HANDSET, OUTPUT_HEADPHONE or OUTPUT_SPEAKER
* volume = VOLUME_LOW, VOLUME_NORMAL, VOLUME_INSANELY_LOUD
* mute = MUTE_OFF, MUTE_ON */
static void
send_select_output(struct unistimsession *pte, unsigned char output, unsigned char volume,
unsigned char mute)
{
BUFFSEND;
int mute_icon = -1;
if (unistimdebug) {
ast_verb(0, "Sending select output packet output=%x volume=%x mute=%x\n",
(unsigned)output, (unsigned)volume, (unsigned)mute);
}
memcpy(buffsend + SIZE_HEADER, packet_send_select_output,
sizeof(packet_send_select_output));
buffsend[9] = output;
if (output == OUTPUT_SPEAKER && volume == VOLUME_LOW) {
volume = VOLUME_LOW_SPEAKER;
}
buffsend[10] = volume;
if (mute == MUTE_ON_DISCRET) {
buffsend[11] = MUTE_ON;
} else {
buffsend[11] = mute;
}
send_client(SIZE_HEADER + sizeof(packet_send_select_output), buffsend, pte);
if (output == OUTPUT_HANDSET) {
mute_icon = (mute == MUTE_ON) ? FAV_ICON_ONHOLD_BLACK : FAV_ICON_OFFHOOK_BLACK;
send_led_update(pte, LED_SPEAKER_OFF);
send_led_update(pte, LED_HEADPHONE_OFF);
} else if (output == OUTPUT_HEADPHONE) {
mute_icon = (mute == MUTE_ON)? FAV_ICON_HEADPHONES_ONHOLD : FAV_ICON_HEADPHONES;
send_led_update(pte, LED_SPEAKER_OFF);
send_led_update(pte, LED_HEADPHONE_ON);
} else if (output == OUTPUT_SPEAKER) {
send_led_update(pte, LED_SPEAKER_ON);
send_led_update(pte, LED_HEADPHONE_OFF);
if (pte->device->receiver_state == STATE_OFFHOOK) {
mute_icon = (mute == MUTE_ON)? FAV_ICON_SPEAKER_ONHOLD_BLACK : FAV_ICON_SPEAKER_ONHOOK_BLACK;
} else {
mute_icon = (mute == MUTE_ON)? FAV_ICON_SPEAKER_ONHOLD_BLACK : FAV_ICON_SPEAKER_OFFHOOK_BLACK;
}
} else {
ast_log(LOG_WARNING, "Invalid output (%d)\n", output);
}
if (mute_icon != -1) {
change_favorite_icon(pte, mute_icon);
}
if (output != pte->device->output) {
pte->device->previous_output = pte->device->output;
}
pte->device->output = output;
}
static void send_ring(struct unistimsession *pte, signed char volume, signed char style)
{
BUFFSEND;
if (unistimdebug) {
ast_verb(0, "Sending ring packet\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_ring, sizeof(packet_send_ring));
buffsend[24] = style + 0x10;
buffsend[29] = volume * 0x10;
send_client(SIZE_HEADER + sizeof(packet_send_ring), buffsend, pte);
}
static void send_no_ring(struct unistimsession *pte)
{
BUFFSEND;
if (unistimdebug) {
ast_verb(0, "Sending no ring packet\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_no_ring, sizeof(packet_send_no_ring));
send_client(SIZE_HEADER + sizeof(packet_send_no_ring), buffsend, pte);
}
static void send_texttitle(struct unistimsession *pte, const char *text)
{
BUFFSEND;
int i;
if (unistimdebug) {
ast_verb(0, "Sending title text\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_title, sizeof(packet_send_title));
i = strlen(text);
if (i > 12) {
i = 12;
}
memcpy(buffsend + 10, text, i);
send_client(SIZE_HEADER + sizeof(packet_send_title), buffsend, pte);
}
static void send_idle_clock(struct unistimsession *pte)
{
send_text(TEXT_LINE0, TEXT_NORMAL, pte, "");
}
static void send_month_labels(struct unistimsession *pte, int month)
{
BUFFSEND;
char month_name[MONTH_LABEL_SIZE + 1];
int i = 0;
if (unistimdebug) {
ast_verb(0, "Sending Month Labels\n");
}
month_name[MONTH_LABEL_SIZE] = '\0';
memcpy(buffsend + SIZE_HEADER, packet_send_monthlabels_download, sizeof(packet_send_monthlabels_download));
while (i < 2) {
memcpy(month_name, &monthlabels[month * MONTH_LABEL_SIZE], MONTH_LABEL_SIZE);
memcpy(buffsend + SIZE_HEADER + 3 + i*MONTH_LABEL_SIZE, ustmtext(month_name, pte), MONTH_LABEL_SIZE);
ast_log(LOG_WARNING,"%s\n", month_name);
ast_log(LOG_WARNING,"%s\n", ustmtext(month_name, pte));
month = (month + 1)%12;
i++;
}
send_client(SIZE_HEADER + sizeof(packet_send_monthlabels_download), buffsend, pte);
}
static void send_date_time(struct unistimsession *pte)
{
BUFFSEND;
struct timeval now = ast_tvnow();
struct ast_tm atm = { 0, };
if (unistimdebug) {
ast_verb(0, "Sending Time & Date\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_date_time, sizeof(packet_send_date_time));
ast_localtime(&now, &atm, NULL);
buffsend[10] = (unsigned char) atm.tm_mon + 1;
buffsend[11] = (unsigned char) atm.tm_mday;
buffsend[12] = (unsigned char) atm.tm_hour;
buffsend[13] = (unsigned char) atm.tm_min;
send_client(SIZE_HEADER + sizeof(packet_send_date_time), buffsend, pte);
send_month_labels(pte, atm.tm_mon);
}
static void send_date_time2(struct unistimsession *pte)
{
BUFFSEND;
struct timeval now = ast_tvnow();
struct ast_tm atm = { 0, };
if (unistimdebug) {
ast_verb(0, "Sending Time & Date #2\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_date_time2, sizeof(packet_send_date_time2));
ast_localtime(&now, &atm, NULL);
if (pte->device) {
buffsend[9] = pte->device->datetimeformat;
} else {
buffsend[9] = 61;
}
buffsend[14] = (unsigned char) atm.tm_mon + 1;
buffsend[15] = (unsigned char) atm.tm_mday;
buffsend[16] = (unsigned char) atm.tm_hour;
buffsend[17] = (unsigned char) atm.tm_min;
send_client(SIZE_HEADER + sizeof(packet_send_date_time2), buffsend, pte);
}
static void send_date_time3(struct unistimsession *pte)
{
BUFFSEND;
struct timeval now = ast_tvnow();
struct ast_tm atm = { 0, };
if (unistimdebug) {
ast_verb(0, "Sending Time & Date #3\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_date_time3, sizeof(packet_send_date_time3));
ast_localtime(&now, &atm, NULL);
buffsend[10] = (unsigned char) atm.tm_mon + 1;
buffsend[11] = (unsigned char) atm.tm_mday;
buffsend[12] = (unsigned char) atm.tm_hour;
buffsend[13] = (unsigned char) atm.tm_min;
send_client(SIZE_HEADER + sizeof(packet_send_date_time3), buffsend, pte);
}
static void send_blink_cursor(struct unistimsession *pte)
{
BUFFSEND;
if (unistimdebug) {
ast_verb(0, "Sending set blink\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_blink_cursor, sizeof(packet_send_blink_cursor));
send_client(SIZE_HEADER + sizeof(packet_send_blink_cursor), buffsend, pte);
return;
}
/* pos : 0xab (a=0/2/4 = line ; b = row) */
static void send_cursor_pos(struct unistimsession *pte, unsigned char pos)
{
BUFFSEND;
if (unistimdebug) {
ast_verb(0, "Sending set cursor position\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_set_pos_cursor,
sizeof(packet_send_set_pos_cursor));
buffsend[11] = pos;
send_client(SIZE_HEADER + sizeof(packet_send_set_pos_cursor), buffsend, pte);
return;
}
static void send_charset_update(struct unistimsession *pte, int charset)
{
const unsigned char* packet_send_charset;
int packet_size;
BUFFSEND;
if (unistimdebug) {
ast_verb(0, "Sending set default charset\n");
}
if (charset == LANG_DEFAULT) {
charset = options_languages[find_language(pte->device->language)].encoding;
}
switch (charset) {
case ISO_8859_2:
packet_send_charset = packet_send_charset_iso_8859_2;
packet_size = sizeof(packet_send_charset_iso_8859_2);
break;
case ISO_8859_4:
packet_send_charset = packet_send_charset_iso_8859_4;
packet_size = sizeof(packet_send_charset_iso_8859_4);
break;
case ISO_8859_5:
packet_send_charset = packet_send_charset_iso_8859_5;
packet_size = sizeof(packet_send_charset_iso_8859_5);
break;
case ISO_2022_JP:
packet_send_charset = packet_send_charset_iso_2022_jp;
packet_size = sizeof(packet_send_charset_iso_2022_jp);
break;
case ISO_8859_1:
default:
packet_send_charset = packet_send_charset_iso_8859_1;
packet_size = sizeof(packet_send_charset_iso_8859_1);
}
memcpy(buffsend + SIZE_HEADER, packet_send_charset, packet_size);
send_client(SIZE_HEADER + packet_size, buffsend, pte);
return;
}
static void rcv_resume_connection_with_server(struct unistimsession *pte)
{
BUFFSEND;
if (unistimdebug) {
ast_verb(0, "ResumeConnectionWithServer received\n");
ast_verb(0, "Sending packet_send_query_mac_address\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_query_mac_address,
sizeof(packet_send_query_mac_address));
send_client(SIZE_HEADER + sizeof(packet_send_query_mac_address), buffsend, pte);
return;
}
static int unistim_register(struct unistimsession *s)
{
struct unistim_device *d;
ast_mutex_lock(&devicelock);
d = devices;
while (d) {
if (!strcasecmp(s->macaddr, d->id)) {
/* XXX Deal with IP authentication */
s->device = d;
d->session = s;
d->codec_number = DEFAULT_CODEC;
d->missed_call = 0;
d->receiver_state = STATE_ONHOOK;
break;
}
d = d->next;
}
ast_mutex_unlock(&devicelock);
if (!d) {
return 0;
}
return 1;
}
static void unistim_line_copy(struct unistim_line *dst, struct unistim_line *src)
{
struct ast_format_cap *tmp = src->cap;
memcpy(dst, src, sizeof(*dst)); /* this over writes the cap ptr, so we have to reset it */
src->cap = tmp;
ast_format_cap_append_from_cap(src->cap, dst->cap, AST_MEDIA_TYPE_UNKNOWN);
}
static struct unistim_line *unistim_line_destroy(struct unistim_line *l)
{
if (!l) {
return NULL;
}
ao2_ref(l->cap, -1);
ast_free(l);
return NULL;
}
static struct unistim_line *unistim_line_alloc(void)
{
struct unistim_line *l;
if (!(l = ast_calloc(1, sizeof(*l)))) {
return NULL;
}
if (!(l->cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
ast_free(l);
return NULL;
}
return l;
}
static int unistim_free_sub(struct unistim_subchannel *sub) {
if (unistimdebug) {
ast_debug(1, "Released sub %u of channel %s@%s\n", sub->subtype, sub->parent->name, sub->parent->parent->name);
}
ast_mutex_destroy(&sub->lock);
ast_free(sub);
return 0;
}
static struct unistim_subchannel *unistim_alloc_sub(struct unistim_device *d, int x)
{
struct unistim_subchannel *sub;
if (!(sub = ast_calloc(1, sizeof(*sub)))) {
return NULL;
}
if (unistimdebug) {
ast_verb(3, "Allocating UNISTIM subchannel #%d on %s ptr=%p\n", x, d->name, sub);
}
sub->ss_thread = AST_PTHREADT_NULL;
sub->subtype = x;
AST_LIST_LOCK(&d->subs);
AST_LIST_INSERT_TAIL(&d->subs, sub, list);
AST_LIST_UNLOCK(&d->subs);
ast_mutex_init(&sub->lock);
return sub;
}
static int unistim_unalloc_sub(struct unistim_device *d, struct unistim_subchannel *sub)
{
struct unistim_subchannel *s;
AST_LIST_LOCK(&d->subs);
AST_LIST_TRAVERSE_SAFE_BEGIN(&d->subs, s, list) {
if (!s) {
continue;
}
if (s != sub) {
continue;
}
AST_LIST_REMOVE_CURRENT(list);
unistim_free_sub(sub);
}
AST_LIST_TRAVERSE_SAFE_END;
AST_LIST_UNLOCK(&d->subs);
return 0;
}
static const char *subtype_tostr(const int type)
{
switch (type) {
case SUB_REAL:
return "REAL";
case SUB_RING:
return "RINGING";
case SUB_THREEWAY:
return "THREEWAY";
}
return "UNKNOWN";
}
static const char *ptestate_tostr(const int type)
{
switch (type) {
case STATE_INIT:
return "INIT";
case STATE_AUTHDENY:
return "AUTHDENY";
case STATE_MAINPAGE:
return "MAINPAGE";
case STATE_EXTENSION:
return "EXTENSION";
case STATE_DIALPAGE:
return "DIALPAGE";
case STATE_RINGING:
return "RINGING";
case STATE_CALL:
return "CALL";
case STATE_SELECTOPTION:
return "SELECTOPTION";
case STATE_SELECTCODEC:
return "SELECTCODEC";
case STATE_SELECTLANGUAGE:
return "SELECTLANGUAGE";
case STATE_CLEANING:
return "CLEARING";
case STATE_HISTORY:
return "HISTORY";
}
return "UNKNOWN";
}
static void rcv_mac_addr(struct unistimsession *pte, const unsigned char *buf)
{
BUFFSEND;
int tmp, i = 0;
char addrmac[19];
int res = 0;
for (tmp = 15; tmp < 15 + SIZE_HEADER; tmp++) {
sprintf(&addrmac[i], "%02hhx", buf[tmp]);
i += 2;
}
if (unistimdebug) {
ast_verb(0, "MAC Address received: %s\n", addrmac);
}
strcpy(pte->macaddr, addrmac);
res = unistim_register(pte);
if (!res) {
switch (autoprovisioning) {
case AUTOPROVISIONING_NO:
ast_log(LOG_WARNING, "No entry found for this phone : %s\n", addrmac);
pte->state = STATE_AUTHDENY;
break;
case AUTOPROVISIONING_YES:
{
struct unistim_device *d = NULL, *newd = NULL;
struct unistim_line *newl = NULL, *l = NULL;
if (unistimdebug) {
ast_verb(0, "New phone, autoprovisioning on\n");
}
/* First : locate the [template] section */
ast_mutex_lock(&devicelock);
d = devices;
while (d) {
if (strcasecmp(d->name, "template")) {
d = d->next;
continue;
}
/* Found, cloning this entry */
if (!(newd = ast_malloc(sizeof(*newd)))) {
ast_mutex_unlock(&devicelock);
return;
}
memcpy(newd, d, sizeof(*newd));
ast_mutex_init(&newd->lock);
newd->lines.first = NULL;
newd->lines.last = NULL;
AST_LIST_LOCK(&d->lines);
AST_LIST_TRAVERSE(&d->lines, l, list) {
if (!(newl = unistim_line_alloc())) {
break;
}
unistim_line_copy(l, newl);
newl->parent = newd;
ast_copy_string(newl->name, l->name, sizeof(newl->name));
snprintf(newl->fullname, sizeof(newl->fullname), "USTM/%s@%s",
newl->name, newd->name);
snprintf(l->name, sizeof(l->name), "%d", atoi(l->name) + 1);
AST_LIST_LOCK(&newd->lines);
AST_LIST_INSERT_TAIL(&newd->lines, newl, list);
AST_LIST_UNLOCK(&newd->lines);
}
AST_LIST_UNLOCK(&d->lines);
if (!newl) {
ast_free(newd);
ast_mutex_unlock(&devicelock);
}
/* Ok, now updating some fields */
ast_copy_string(newd->id, addrmac, sizeof(newd->id));
ast_copy_string(newd->name, addrmac, sizeof(newd->name));
if (newd->extension == EXTENSION_NONE) {
newd->extension = EXTENSION_ASK;
}
newd->receiver_state = STATE_ONHOOK;
newd->session = pte;
newd->language[0] = '\0';
newd->to_delete = -1;
newd->next = NULL;
pte->device = newd;
/* Go to the end of the linked chain */
while (d->next) {
d = d->next;
}
d->next = newd;
d = newd;
break;
}
ast_mutex_unlock(&devicelock);
if (!d) {
ast_log(LOG_WARNING, "No entry [template] found in unistim.conf\n");
pte->state = STATE_AUTHDENY;
}
}
break;
case AUTOPROVISIONING_TN:
pte->state = STATE_AUTHDENY;
break;
default:
ast_log(LOG_WARNING, "Internal error : unknown autoprovisioning value = %u\n",
autoprovisioning);
}
}
if (pte->state != STATE_AUTHDENY) {
struct unistim_line *line;
struct unistim_subchannel *sub;
ast_verb(3, "Device '%s' successfuly registered\n", pte->device->name);
AST_LIST_LOCK(&pte->device->subs);
AST_LIST_TRAVERSE_SAFE_BEGIN(&pte->device->subs, sub, list) {
if (sub) {
ast_log(LOG_ERROR, "Subchannel lost sice reboot. Hanged channel may apear!\n");
AST_LIST_REMOVE_CURRENT(list);
ast_free(sub);
}
}
AST_LIST_TRAVERSE_SAFE_END;
AST_LIST_UNLOCK(&pte->device->subs);
switch (pte->device->extension) {
case EXTENSION_NONE:
pte->state = STATE_MAINPAGE;
break;
case EXTENSION_ASK:
/* Checking if we already have an extension number */
if (ast_strlen_zero(pte->device->extension_number)) {
pte->state = STATE_EXTENSION;
} else {
/* Yes, because of a phone reboot. We don't ask again for the TN */
if (register_extension(pte)) {
pte->state = STATE_EXTENSION;
} else {
pte->state = STATE_MAINPAGE;
}
}
break;
case EXTENSION_LINE:
line = AST_LIST_FIRST(&pte->device->lines);
ast_copy_string(pte->device->extension_number, line->name,
sizeof(pte->device->extension_number));
if (register_extension(pte)) {
pte->state = STATE_EXTENSION;
} else {
pte->state = STATE_MAINPAGE;
}
break;
case EXTENSION_TN:
/* If we are here, it's because of a phone reboot */
pte->state = STATE_MAINPAGE;
break;
default:
ast_log(LOG_WARNING, "Internal error, extension value unknown : %u\n",
pte->device->extension);
pte->state = STATE_AUTHDENY;
break;
}
}
if (pte->state == STATE_EXTENSION) {
if (pte->device->extension != EXTENSION_TN) {
pte->device->extension = EXTENSION_ASK;
}
pte->device->extension_number[0] = '\0';
}
if (unistimdebug) {
ast_verb(0, "\nSending S1\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_S1, sizeof(packet_send_S1));
send_client(SIZE_HEADER + sizeof(packet_send_S1), buffsend, pte);
if (unistimdebug) {
ast_verb(0, "Sending query_basic_manager_04\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_query_basic_manager_04,
sizeof(packet_send_query_basic_manager_04));
send_client(SIZE_HEADER + sizeof(packet_send_query_basic_manager_04), buffsend, pte);
if (unistimdebug) {
ast_verb(0, "Sending query_basic_manager_10\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_query_basic_manager_10,
sizeof(packet_send_query_basic_manager_10));
send_client(SIZE_HEADER + sizeof(packet_send_query_basic_manager_10), buffsend, pte);
send_date_time(pte);
return;
}
static int write_entry_history(struct unistimsession *pte, FILE * f, char c, char *line1)
{
if (fwrite(&c, 1, 1, f) != 1) {
display_last_error("Unable to write history log header.");
return -1;
}
if (fwrite(line1, TEXT_LENGTH_MAX, 1, f) != 1) {
display_last_error("Unable to write history entry - date.");
return -1;
}
if (fwrite(pte->device->lst_cid, TEXT_LENGTH_MAX, 1, f) != 1) {
display_last_error("Unable to write history entry - callerid.");
return -1;
}
if (fwrite(pte->device->lst_cnm, TEXT_LENGTH_MAX, 1, f) != 1) {
display_last_error("Unable to write history entry - callername.");
return -1;
}
return 0;
}
static int write_history(struct unistimsession *pte, char way, char ismissed)
{
char tmp[AST_CONFIG_MAX_PATH], tmp2[AST_CONFIG_MAX_PATH];
char line1[TEXT_LENGTH_MAX + 1];
char count = 0, *histbuf;
int size;
FILE *f, *f2;
struct timeval now = ast_tvnow();
struct ast_tm atm = { 0, };
if (!pte->device) {
return -1;
}
if (!pte->device->callhistory) {
return 0;
}
if (strchr(pte->device->name, '/') || (pte->device->name[0] == '.')) {
ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n",
pte->device->name);
return -1;
}
snprintf(tmp, sizeof(tmp), "%s/%s", ast_config_AST_LOG_DIR, USTM_LOG_DIR);
if (ast_mkdir(tmp, 0770)) {
ast_log(LOG_WARNING, "Unable to create directory for history\n");
return -1;
}
ast_localtime(&now, &atm, NULL);
if (ismissed) {
if (way == 'i') {
ast_copy_string(tmp2, ustmtext("Miss", pte), sizeof(tmp2));
} else {
ast_copy_string(tmp2, ustmtext("Fail", pte), sizeof(tmp2));
}
} else {
ast_copy_string(tmp2, ustmtext("Answ", pte), sizeof(tmp2));
}
snprintf(line1, sizeof(line1), "%04d/%02d/%02d %02d:%02d:%02d %s",
atm.tm_year + 1900, atm.tm_mon + 1, atm.tm_mday, atm.tm_hour,
atm.tm_min, atm.tm_sec, tmp2);
snprintf(tmp, sizeof(tmp), "%s/%s/%s-%c.csv", ast_config_AST_LOG_DIR,
USTM_LOG_DIR, pte->device->name, way);
if ((f = fopen(tmp, "r"))) {
struct stat bufstat;
if (stat(tmp, &bufstat)) {
display_last_error("Unable to stat history log.");
fclose(f);
return -1;
}
size = 1 + (MAX_ENTRY_LOG * TEXT_LENGTH_MAX * 3);
if (bufstat.st_size != size) {
ast_log(LOG_WARNING,
"History file %s has an incorrect size (%d instead of %d). It will be replaced by a new one.",
tmp, (int) bufstat.st_size, size);
fclose(f);
f = NULL;
count = 1;
}
}
/* If we can't open the log file, we create a brand new one */
if (!f) {
char c = 1;
int i;
if ((errno != ENOENT) && (count == 0)) {
display_last_error("Unable to open history log.");
return -1;
}
f = fopen(tmp, "w");
if (!f) {
display_last_error("Unable to create history log.");
return -1;
}
if (write_entry_history(pte, f, c, line1)) {
fclose(f);
return -1;
}
memset(line1, ' ', TEXT_LENGTH_MAX);
for (i = 3; i < MAX_ENTRY_LOG * 3; i++) {
if (fwrite(line1, TEXT_LENGTH_MAX, 1, f) != 1) {
display_last_error("Unable to write history entry - stuffing.");
fclose(f);
return -1;
}
}
if (fclose(f)) {
display_last_error("Unable to close history - creation.");
}
return 0;
}
/* We can open the log file, we create a temporary one, we add our entry and copy the rest */
if (fread(&count, 1, 1, f) != 1) {
display_last_error("Unable to read history header.");
fclose(f);
return -1;
}
if (count > MAX_ENTRY_LOG) {
ast_log(LOG_WARNING, "Invalid count in history header of %s (%d max %d)\n", tmp,
count, MAX_ENTRY_LOG);
fclose(f);
return -1;
}
snprintf(tmp2, sizeof(tmp2), "%s/%s/%s-%c.csv.tmp", ast_config_AST_LOG_DIR,
USTM_LOG_DIR, pte->device->name, way);
if (!(f2 = fopen(tmp2, "w"))) {
display_last_error("Unable to create temporary history log.");
fclose(f);
return -1;
}
if (++count > MAX_ENTRY_LOG) {
count = MAX_ENTRY_LOG;
}
if (write_entry_history(pte, f2, count, line1)) {
fclose(f);
fclose(f2);
return -1;
}
size = (MAX_ENTRY_LOG - 1) * TEXT_LENGTH_MAX * 3;
if (!(histbuf = ast_malloc(size))) {
fclose(f);
fclose(f2);
return -1;
}
if (fread(histbuf, size, 1, f) != 1) {
ast_free(histbuf);
fclose(f);
fclose(f2);
display_last_error("Unable to read previous history entries.");
return -1;
}
if (fwrite(histbuf, size, 1, f2) != 1) {
ast_free(histbuf);
fclose(f);
fclose(f2);
display_last_error("Unable to write previous history entries.");
return -1;
}
ast_free(histbuf);
if (fclose(f)) {
display_last_error("Unable to close history log.");
}
if (fclose(f2)) {
display_last_error("Unable to close temporary history log.");
}
if (unlink(tmp)) {
display_last_error("Unable to remove old history log.");
}
if (rename(tmp2, tmp)) {
display_last_error("Unable to rename new history log.");
}
return 0;
}
static int attempt_transfer(struct unistim_subchannel *p1, struct unistim_subchannel *p2)
{
RAII_VAR(struct ast_channel *, chana, NULL, ast_channel_unref);
RAII_VAR(struct ast_channel *, chanb, NULL, ast_channel_unref);
if (!p1->owner || !p2->owner) {
ast_log(LOG_WARNING, "Transfer attempted without dual ownership?\n");
return -1;
}
chana = ast_channel_ref(p1->owner);
chanb = ast_channel_ref(p2->owner);
switch (ast_bridge_transfer_attended(chana, chanb)) {
case AST_BRIDGE_TRANSFER_INVALID:
ast_log(LOG_WARNING, "Transfer failed. Invalid bridge setup\n");
break;
case AST_BRIDGE_TRANSFER_NOT_PERMITTED:
ast_log(LOG_WARNING, "Transfer not permitted\n");
break;
case AST_BRIDGE_TRANSFER_FAIL:
ast_log(LOG_WARNING, "Transfer encountered internal error\n");
break;
case AST_BRIDGE_TRANSFER_SUCCESS:
return 0;
}
/* Control only reaches this point if transfer has failed */
ast_softhangup_nolock(chana, AST_SOFTHANGUP_DEV);
ast_softhangup_nolock(chanb, AST_SOFTHANGUP_DEV);
return -1;
}
void change_callerid(struct unistimsession *pte, int type, char *callerid)
{
char *data;
int size;
if (type) {
data = pte->device->lst_cnm;
} else {
data = pte->device->lst_cid;
}
/* This is very nearly strncpy(), except that the remaining buffer
* is padded with ' ', instead of '\0' */
memset(data, ' ', TEXT_LENGTH_MAX);
size = strlen(callerid);
if (size > TEXT_LENGTH_MAX) {
size = TEXT_LENGTH_MAX;
}
memcpy(data, callerid, size);
}
static struct unistim_subchannel* get_sub(struct unistim_device *device, int type)
{
struct unistim_subchannel *sub = NULL;
AST_LIST_LOCK(&device->subs);
AST_LIST_TRAVERSE(&device->subs, sub, list) {
if (!sub) {
continue;
}
if (sub->subtype == type) {
break;
}
}
AST_LIST_UNLOCK(&device->subs);
return sub;
}
static struct unistim_subchannel* get_sub_holding(struct unistim_device *device, int type, int holding)
{
struct unistim_subchannel *sub = NULL;
AST_LIST_LOCK(&device->subs);
AST_LIST_TRAVERSE(&device->subs, sub, list) {
if (!sub) {
continue;
}
if (sub->subtype == type && sub->holding == holding) {
break;
}
}
AST_LIST_UNLOCK(&device->subs);
return sub;
}
static void sub_start_silence(struct unistimsession *pte, struct unistim_subchannel *sub)
{
/* Silence our channel */
if (!pte->device->silence_generator) {
pte->device->silence_generator =
ast_channel_start_silence_generator(sub->owner);
if (pte->device->silence_generator == NULL) {
ast_log(LOG_WARNING, "Unable to start a silence generator.\n");
} else if (unistimdebug) {
ast_verb(0, "Starting silence generator\n");
}
}
}
static void sub_stop_silence(struct unistimsession *pte, struct unistim_subchannel *sub)
{
/* Stop the silence generator */
if (pte->device->silence_generator) {
if (unistimdebug) {
ast_verb(0, "Stopping silence generator\n");
}
if (sub->owner) {
ast_channel_stop_silence_generator(sub->owner, pte->device->silence_generator);
} else {
ast_log(LOG_WARNING, "Trying to stop silence generator on a null channel!\n");
}
pte->device->silence_generator = NULL;
}
}
static void sub_hold(struct unistimsession *pte, struct unistim_subchannel *sub)
{
if (!sub) {
return;
}
sub->moh = 1;
sub->holding = 1;
send_favorite_short(sub->softkey, FAV_ICON_ONHOLD_BLACK + FAV_BLINK_SLOW, pte);
send_select_output(pte, pte->device->output, pte->device->volume, MUTE_ON);
send_stop_timer(pte);
if (sub->owner) {
ast_queue_hold(sub->owner, NULL);
}
return;
}
static void sub_unhold(struct unistimsession *pte, struct unistim_subchannel *sub)
{
struct unistim_subchannel *sub_real;
sub_real = get_sub(pte->device, SUB_REAL);
if (sub_real) {
sub_hold(pte, sub_real);
}
sub->moh = 0;
sub->holding = 0;
send_favorite_short(sub->softkey, FAV_ICON_OFFHOOK_BLACK, pte);
send_select_output(pte, pte->device->output, pte->device->volume, MUTE_OFF);
send_start_timer(pte);
if (sub->owner) {
ast_queue_unhold(sub->owner);
if (sub->rtp) {
send_start_rtp(sub);
}
}
return;
}
static void close_call(struct unistimsession *pte)
{
struct unistim_subchannel *sub, *sub_transf;
sub = get_sub(pte->device, SUB_REAL);
sub_transf = get_sub(pte->device, SUB_THREEWAY);
send_stop_timer(pte);
if (!sub) {
ast_log(LOG_WARNING, "Close call without sub\n");
return;
}
send_favorite_short(sub->softkey, FAV_LINE_ICON, pte);
if (sub->owner) {
sub->alreadygone = 1;
if (sub_transf) {
sub_transf->alreadygone = 1;
if (attempt_transfer(sub, sub_transf) < 0) {
ast_verb(0, "attempt_transfer failed.\n");
}
} else {
ast_queue_hangup(sub->owner);
}
} else {
if (sub_transf) {
if (sub_transf->owner) {
ast_queue_hangup_with_cause(sub_transf->owner, AST_CAUSE_NORMAL_CLEARING);
} else {
ast_log(LOG_WARNING, "threeway sub without owner\n");
}
} else {
ast_verb(0, "USTM(%s@%s-%d) channel already destroyed\n", sub->parent->name,
pte->device->name, sub->softkey);
}
}
change_callerid(pte, 0, pte->device->redial_number);
change_callerid(pte, 1, "");
write_history(pte, 'o', pte->device->missed_call);
pte->device->missed_call = 0;
show_main_page(pte);
return;
}
static void ignore_call(struct unistimsession *pte)
{
send_no_ring(pte);
return;
}
static void discard_call(struct unistimsession *pte)
{
struct unistim_subchannel* sub;
sub = get_sub(pte->device, SUB_RING);
if (!sub) {
return;
}
ast_queue_hangup_with_cause(sub->owner, AST_CAUSE_NORMAL_CLEARING);
return;
}
static void *unistim_ss(void *data)
{
struct ast_channel *chan = data;
struct unistim_subchannel *sub = ast_channel_tech_pvt(chan);
struct unistim_line *l = sub->parent;
struct unistimsession *s = l->parent->session;
int res;
if (!s) {
return NULL;
}
ast_verb(3, "Starting switch on '%s@%s-%d' to %s\n", l->name, l->parent->name, sub->softkey, s->device->phone_number);
ast_channel_lock(chan);
ast_channel_exten_set(chan, s->device->phone_number);
ast_setstate(chan, AST_STATE_RING);
ast_channel_unlock(chan);
ast_copy_string(s->device->redial_number, s->device->phone_number,
sizeof(s->device->redial_number));
res = ast_pbx_run(chan);
if (res) {
ast_log(LOG_WARNING, "PBX exited non-zero\n");
send_tone(s, 1000, 0);
}
return NULL;
}
static int find_rtp_port(struct unistim_subchannel *s)
{
struct unistim_subchannel *sub = NULL;
int rtp_start = s->parent->parent->rtp_port;
struct ast_sockaddr us_tmp;
struct sockaddr_in us = { 0, };
AST_LIST_LOCK(&s->parent->parent->subs);
AST_LIST_TRAVERSE(&s->parent->parent->subs, sub, list) {
if (!sub) {
continue;
}
if (sub->rtp) {
ast_rtp_instance_get_remote_address(sub->rtp, &us_tmp);
ast_sockaddr_to_sin(&us_tmp, &us);
if (htons(us.sin_port)) {
rtp_start = htons(us.sin_port) + 1;
break;
}
}
}
AST_LIST_UNLOCK(&s->parent->parent->subs);
return rtp_start;
}
static void send_start_rtp(struct unistim_subchannel *sub)
{
BUFFSEND;
int codec;
struct sockaddr_in public = { 0, };
struct sockaddr_in us = { 0, };
struct sockaddr_in sin = { 0, };
struct ast_sockaddr us_tmp;
struct ast_sockaddr sin_tmp;
struct unistimsession *pte;
ast_rtp_instance_get_local_address(sub->rtp, &us_tmp);
ast_sockaddr_to_sin(&us_tmp, &us);
ast_rtp_instance_get_remote_address(sub->rtp, &sin_tmp);
ast_sockaddr_to_sin(&sin_tmp, &sin);
/* Setting up RTP of the phone */
if (public_ip.sin_family == 0) { /* NAT IP override ? */
memcpy(&public, &us, sizeof(public)); /* No defined, using IP from recvmsg */
} else {
memcpy(&public, &public_ip, sizeof(public)); /* override */
}
if (unistimdebug) {
ast_verb(0, "RTP started : Our IP/port is : %s:%hd with codec %s\n",
ast_inet_ntoa(us.sin_addr),
htons(us.sin_port), ast_format_get_name(ast_channel_readformat(sub->owner)));
ast_verb(0, "Starting phone RTP stack. Our public IP is %s\n",
ast_inet_ntoa(public.sin_addr));
}
pte = sub->parent->parent->session;
codec = ast_rtp_codecs_payload_code_tx(ast_rtp_instance_get_codecs(sub->rtp),
1, ast_channel_readformat(sub->owner), 0);
if ((ast_format_cmp(ast_channel_readformat(sub->owner), ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) ||
(ast_format_cmp(ast_channel_readformat(sub->owner), ast_format_alaw) == AST_FORMAT_CMP_EQUAL)) {
if (unistimdebug) {
ast_verb(0, "Sending packet_send_rtp_packet_size for codec %d\n", codec);
}
memcpy(buffsend + SIZE_HEADER, packet_send_rtp_packet_size,
sizeof(packet_send_rtp_packet_size));
buffsend[10] = (int) codec & 0xffffffffLL;
send_client(SIZE_HEADER + sizeof(packet_send_rtp_packet_size), buffsend, pte);
}
if (unistimdebug) {
ast_verb(0, "Sending Jitter Buffer Parameters Configuration\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_jitter_buffer_conf,
sizeof(packet_send_jitter_buffer_conf));
send_client(SIZE_HEADER + sizeof(packet_send_jitter_buffer_conf), buffsend, pte);
if (pte->device->rtp_method != 0) {
uint16_t rtcpsin_port = htons(us.sin_port) + 1; /* RTCP port is RTP + 1 */
if (unistimdebug) {
ast_verb(0, "Sending OpenAudioStreamTX using method #%d\n", pte->device->rtp_method);
}
if (pte->device->rtp_method == 3) {
memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_tx3,
sizeof(packet_send_open_audio_stream_tx3));
} else {
memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_tx,
sizeof(packet_send_open_audio_stream_tx));
}
if (pte->device->rtp_method != 2) {
memcpy(buffsend + 28, &public.sin_addr, sizeof(public.sin_addr));
buffsend[20] = (htons(sin.sin_port) & 0xff00) >> 8;
buffsend[21] = (htons(sin.sin_port) & 0x00ff);
buffsend[23] = (rtcpsin_port & 0x00ff);
buffsend[22] = (rtcpsin_port & 0xff00) >> 8;
buffsend[25] = (us.sin_port & 0xff00) >> 8;
buffsend[24] = (us.sin_port & 0x00ff);
buffsend[27] = (rtcpsin_port & 0x00ff);
buffsend[26] = (rtcpsin_port & 0xff00) >> 8;
} else {
memcpy(buffsend + 23, &public.sin_addr, sizeof(public.sin_addr));
buffsend[15] = (htons(sin.sin_port) & 0xff00) >> 8;
buffsend[16] = (htons(sin.sin_port) & 0x00ff);
buffsend[20] = (us.sin_port & 0xff00) >> 8;
buffsend[19] = (us.sin_port & 0x00ff);
}
buffsend[11] = codec; /* rx */
buffsend[12] = codec; /* tx */
send_client(SIZE_HEADER + sizeof(packet_send_open_audio_stream_tx), buffsend, pte);
if (unistimdebug) {
ast_verb(0, "Sending OpenAudioStreamRX\n");
}
if (pte->device->rtp_method == 3) {
memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_rx3,
sizeof(packet_send_open_audio_stream_rx3));
} else {
memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_rx,
sizeof(packet_send_open_audio_stream_rx));
}
if (pte->device->rtp_method != 2) {
memcpy(buffsend + 28, &public.sin_addr, sizeof(public.sin_addr));
buffsend[20] = (htons(sin.sin_port) & 0xff00) >> 8;
buffsend[21] = (htons(sin.sin_port) & 0x00ff);
buffsend[23] = (rtcpsin_port & 0x00ff);
buffsend[22] = (rtcpsin_port & 0xff00) >> 8;
buffsend[25] = (us.sin_port & 0xff00) >> 8;
buffsend[24] = (us.sin_port & 0x00ff);
buffsend[27] = (rtcpsin_port & 0x00ff);
buffsend[26] = (rtcpsin_port & 0xff00) >> 8;
} else {
memcpy(buffsend + 23, &public.sin_addr, sizeof(public.sin_addr));
buffsend[15] = (htons(sin.sin_port) & 0xff00) >> 8;
buffsend[16] = (htons(sin.sin_port) & 0x00ff);
buffsend[20] = (us.sin_port & 0xff00) >> 8;
buffsend[19] = (us.sin_port & 0x00ff);
}
buffsend[11] = codec; /* rx */
buffsend[12] = codec; /* tx */
send_client(SIZE_HEADER + sizeof(packet_send_open_audio_stream_rx), buffsend, pte);
} else {
uint16_t rtcpsin_port = htons(us.sin_port) + 1; /* RTCP port is RTP + 1 */
if (unistimdebug) {
ast_verb(0, "Sending packet_send_call default method\n");
}
memcpy(buffsend + SIZE_HEADER, packet_send_call, sizeof(packet_send_call));
memcpy(buffsend + 53, &public.sin_addr, sizeof(public.sin_addr));
/* Destination port when sending RTP */
buffsend[49] = (us.sin_port & 0x00ff);
buffsend[50] = (us.sin_port & 0xff00) >> 8;
/* Destination port when sending RTCP */
buffsend[52] = (rtcpsin_port & 0x00ff);
buffsend[51] = (rtcpsin_port & 0xff00) >> 8;
/* Codec */
buffsend[40] = codec;
buffsend[41] = codec;
if (ast_format_cmp(ast_channel_readformat(sub->owner), ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) {
buffsend[42] = 1; /* 1 = 20ms (160 bytes), 2 = 40ms (320 bytes) */
} else if (ast_format_cmp(ast_channel_readformat(sub->owner), ast_format_alaw) == AST_FORMAT_CMP_EQUAL) {
buffsend[42] = 1; /* 1 = 20ms (160 bytes), 2 = 40ms (320 bytes) */
} else if (ast_format_cmp(ast_channel_readformat(sub->owner), ast_format_g723) == AST_FORMAT_CMP_EQUAL) {
buffsend[42] = 2; /* 1 = 30ms (24 bytes), 2 = 60 ms (48 bytes) */
} else if (ast_format_cmp(ast_channel_readformat(sub->owner), ast_format_g729) == AST_FORMAT_CMP_EQUAL) {
buffsend[42] = 2; /* 1 = 10ms (10 bytes), 2 = 20ms (20 bytes) */
} else {
ast_log(LOG_WARNING, "Unsupported codec %s!\n",
ast_format_get_name(ast_channel_readformat(sub->owner)));
}
/* Source port for transmit RTP and Destination port for receiving RTP */
buffsend[45] = (htons(sin.sin_port) & 0xff00) >> 8;
buffsend[46] = (htons(sin.sin_port) & 0x00ff);
buffsend[47] = (rtcpsin_port & 0xff00) >> 8;
buffsend[48] = (rtcpsin_port & 0x00ff);
send_client(SIZE_HEADER + sizeof(packet_send_call), buffsend, pte);
}
}
static void start_rtp(struct unistim_subchannel *sub)
{
struct sockaddr_in sin = { 0, };
struct sockaddr_in sout = { 0, };
struct ast_sockaddr sin_tmp;
struct ast_sockaddr sout_tmp;
/* Sanity checks */
if (!sub) {
ast_log(LOG_WARNING, "start_rtp with a null subchannel !\n");
return;
}
if (!sub->parent) {
ast_log(LOG_WARNING, "start_rtp with a null line!\n");
return;
}
if (!sub->parent->parent) {
ast_log(LOG_WARNING, "start_rtp with a null device!\n");
return;
}
if (!sub->parent->parent->session) {
ast_log(LOG_WARNING, "start_rtp with a null session!\n");
return;
}
if (!sub->owner) {
ast_log(LOG_WARNING, "start_rtp with a null asterisk channel!\n");
return;
}
sout = sub->parent->parent->session->sout;
ast_mutex_lock(&sub->lock);
/* Allocate the RTP */
if (unistimdebug) {
ast_verb(0, "Starting RTP. Bind on %s\n", ast_inet_ntoa(sout.sin_addr));
}
ast_sockaddr_from_sin(&sout_tmp, &sout);
sub->rtp = ast_rtp_instance_new("asterisk", sched, &sout_tmp, NULL);
if (!sub->rtp) {
ast_log(LOG_WARNING, "Unable to create RTP session: %s binaddr=%s\n",
strerror(errno), ast_inet_ntoa(sout.sin_addr));
ast_mutex_unlock(&sub->lock);
return;
}
ast_rtp_instance_set_prop(sub->rtp, AST_RTP_PROPERTY_RTCP, 1);
ast_rtp_instance_set_channel_id(sub->rtp, ast_channel_uniqueid(sub->owner));
ast_channel_internal_fd_set(sub->owner, 0, ast_rtp_instance_fd(sub->rtp, 0));
ast_channel_internal_fd_set(sub->owner, 1, ast_rtp_instance_fd(sub->rtp, 1));
ast_rtp_instance_set_qos(sub->rtp, qos.tos_audio, qos.cos_audio, "UNISTIM RTP");
ast_rtp_instance_set_prop(sub->rtp, AST_RTP_PROPERTY_NAT, sub->parent->parent->nat);
/* Create the RTP connection */
sin.sin_family = AF_INET;
/* Setting up RTP for our side */
memcpy(&sin.sin_addr, &sub->parent->parent->session->sin.sin_addr,
sizeof(sin.sin_addr));
sin.sin_port = htons(find_rtp_port(sub));
ast_sockaddr_from_sin(&sin_tmp, &sin);
ast_rtp_instance_set_remote_address(sub->rtp, &sin_tmp);
if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(sub->owner), ast_channel_readformat(sub->owner)) == AST_FORMAT_CMP_NOT_EQUAL) {
struct ast_format *tmpfmt;
struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
tmpfmt = ast_format_cap_get_format(ast_channel_nativeformats(sub->owner), 0);
ast_log(LOG_WARNING,
"Our read/writeformat has been changed to something incompatible: %s, using %s best codec from %s\n",
ast_format_get_name(ast_channel_readformat(sub->owner)),
ast_format_get_name(tmpfmt),
ast_format_cap_get_names(ast_channel_nativeformats(sub->owner), &cap_buf));
ast_channel_set_readformat(sub->owner, tmpfmt);
ast_channel_set_writeformat(sub->owner, tmpfmt);
ao2_ref(tmpfmt, -1);
}
send_start_rtp(sub);
ast_mutex_unlock(&sub->lock);
}
static void send_dial_tone(struct unistimsession *pte)
{
struct ast_tone_zone_sound *ts = NULL;
struct ast_tone_zone_part tone_data;
char *s = NULL;
char *ind;
if ((ts = ast_get_indication_tone(pte->device->tz, "dial"))) {
ind = ast_strdupa(ts->data);
s = strsep(&ind, ",");
ast_tone_zone_part_parse(s, &tone_data);
send_tone(pte, tone_data.freq1, tone_data.freq2);
if (unistimdebug) {
ast_verb(0, "Country code found (%s), freq1=%u freq2=%u\n",
pte->device->tz->country, tone_data.freq1, tone_data.freq2);
}
ts = ast_tone_zone_sound_unref(ts);
}
}
static void show_phone_number(struct unistimsession *pte)
{
char tmp[TEXT_LENGTH_MAX + 1];
const char *tmp_number = ustmtext("Number:", pte);
int line, tmp_copy, offset = 0, i;
pte->device->phone_number[pte->device->size_phone_number] = '\0';
if (pte->device->size_phone_number > MAX_SCREEN_NUMBER) {
offset = pte->device->size_phone_number - MAX_SCREEN_NUMBER - 1;
if (offset > strlen(tmp_number)) {
offset = strlen(tmp_number);
}
tmp_copy = strlen(tmp_number) - offset + 1;
if (tmp_copy > sizeof(tmp)) {
tmp_copy = sizeof(tmp);
}
memcpy(tmp, tmp_number + offset, tmp_copy);
} else {
ast_copy_string(tmp, tmp_number, sizeof(tmp));
}
offset = (pte->device->size_phone_number >= TEXT_LENGTH_MAX) ? (pte->device->size_phone_number - TEXT_LENGTH_MAX +1) : 0;
if (pte->device->size_phone_number) {
memcpy(tmp + strlen(tmp), pte->device->phone_number + offset, pte->device->size_phone_number - offset + 1);
}
offset = strlen(tmp);
for (i = strlen(tmp); i < TEXT_LENGTH_MAX; i++) {
tmp[i] = '.';
}
tmp[i] = '\0';
line = (pte->device->height == 1) ? TEXT_LINE0 : TEXT_LINE2;
send_text(line, TEXT_NORMAL, pte, tmp);
send_blink_cursor(pte);
send_cursor_pos(pte, (unsigned char) (line + offset));
send_led_update(pte, LED_BAR_OFF);
}
static void handle_dial_page(struct unistimsession *pte)
{
pte->state = STATE_DIALPAGE;
if (pte->device->call_forward[0] == -1) {
send_text(TEXT_LINE0, TEXT_NORMAL, pte, "");
send_text(TEXT_LINE1, TEXT_NORMAL, pte, ustmtext("Enter forward", pte));
send_text_status(pte, ustmtext("Fwd Cancel BackSp Erase", pte));
if (pte->device->call_forward[1] != 0) {
ast_copy_string(pte->device->phone_number, pte->device->call_forward + 1,
sizeof(pte->device->phone_number));
show_phone_number(pte);
send_led_update(pte, LED_BAR_OFF);
return;
}
} else {
if ((pte->device->output == OUTPUT_HANDSET) &&
(pte->device->receiver_state == STATE_ONHOOK)) {
send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
} else {
send_select_output(pte, pte->device->output, pte->device->volume, MUTE_OFF);
}
send_dial_tone(pte);
if (pte->device->height > 1) {
send_text(TEXT_LINE0, TEXT_NORMAL, pte, ustmtext("Enter the number to dial", pte));
send_text(TEXT_LINE1, TEXT_NORMAL, pte, ustmtext("and press Call", pte));
}
if (ast_strlen_zero(pte->device->redial_number)) {
send_text_status(pte, ustmtext("Call BackSp Erase", pte));
} else {
send_text_status(pte, ustmtext("Call Redial BackSp Erase", pte));
}
}
pte->device->size_phone_number = 0;
pte->device->phone_number[0] = 0;
show_phone_number(pte);
change_favorite_icon(pte, FAV_ICON_PHONE_BLACK);
send_icon(TEXT_LINE0, FAV_ICON_NONE, pte);
pte->device->missed_call = 0;
send_led_update(pte, LED_BAR_OFF);
pte->device->lastmsgssent = -1;
return;
}
static void swap_subs(struct unistim_subchannel *a, struct unistim_subchannel *b)
{
struct ast_rtp_instance *rtp;
int fds;
if (unistimdebug) {
ast_verb(0, "Swapping %p and %p\n", a, b);
}
if ((!a->owner) || (!b->owner)) {
ast_log(LOG_WARNING,
"Attempted to swap subchannels with a null owner : sub #%p=%p sub #%p=%p\n",
a, a->owner, b, b->owner);
return;
}
rtp = a->rtp;
a->rtp = b->rtp;
b->rtp = rtp;
fds = ast_channel_fd(a->owner, 0);
ast_channel_internal_fd_set(a->owner, 0, ast_channel_fd(b->owner, 0));
ast_channel_internal_fd_set(b->owner, 0, fds);
fds = ast_channel_fd(a->owner, 1);
ast_channel_internal_fd_set(a->owner, 1, ast_channel_fd(b->owner, 1));
ast_channel_internal_fd_set(b->owner, 1, fds);
}
/* Step 1 : Music On Hold for peer, Dialing screen for us */
static void transfer_call_step1(struct unistimsession *pte)
{
struct unistim_subchannel *sub /*, *sub_trans */;
struct unistim_device *d = pte->device;
sub = get_sub(d, SUB_REAL);
/* sub_trans = get_sub(d, SUB_THREEWAY); */
if (!sub || !sub->owner) {
ast_log(LOG_WARNING, "Unable to find subchannel for music on hold\n");
return;
}
/* Start music on hold if appropriate */
if (sub->moh) {
ast_log(LOG_WARNING, "Transfer with peer already listening music on hold\n");
} else {
ast_queue_hold(sub->owner, sub->parent->musicclass);
sub->moh = 1;
sub->subtype = SUB_THREEWAY;
}
sub_start_silence(pte, sub);
handle_dial_page(pte);
}
static void transfer_cancel_step2(struct unistimsession *pte)
{
struct unistim_subchannel *sub, *sub_trans;
struct unistim_device *d = pte->device;
sub = get_sub(d, SUB_REAL);
sub_trans = get_sub(d, SUB_THREEWAY);
if (!sub || !sub->owner) {
ast_log(LOG_WARNING, "Unable to find subchannel for music on hold\n");
return;
}
if (sub_trans) {
if (unistimdebug) {
ast_verb(0, "Transfer canceled, hangup our threeway channel\n");
}
if (sub->owner) {
swap_subs(sub, sub_trans);
ast_queue_unhold(sub_trans->owner);
sub_trans->moh = 0;
sub_trans->subtype = SUB_REAL;
sub->subtype = SUB_THREEWAY;
ast_queue_hangup_with_cause(sub->owner, AST_CAUSE_NORMAL_CLEARING);
} else {
ast_log(LOG_WARNING, "Canceling a threeway channel without owner\n");
}
return;
}
}
/* From phone to PBX */
static void handle_call_outgoing(struct unistimsession *s)
{
struct ast_channel *c;
struct unistim_subchannel *sub;
int softkey;
s->state = STATE_CALL;
sub = get_sub(s->device, SUB_THREEWAY);
if (sub) {
/* If sub for threway call created than we use transfer behaviuor */
struct unistim_subchannel *sub_trans = NULL;
struct unistim_device *d = s->device;
sub_trans = get_sub(d, SUB_REAL);
if (sub_trans) {
ast_log(LOG_WARNING, "Can't transfer while active subchannel exists!\n");
return;
}
if (!sub->owner) {
ast_log(LOG_WARNING, "Unable to find subchannel with music on hold\n");
return;
}
sub_trans = unistim_alloc_sub(d, SUB_REAL);
if (!sub_trans) {
ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
return;
}
sub_trans->parent = sub->parent;
sub_stop_silence(s, sub);
send_tone(s, 0, 0);
/* Make new channel */
c = unistim_new(sub_trans, AST_STATE_DOWN, NULL, NULL);
if (!c) {
ast_log(LOG_WARNING, "Cannot allocate new structure on channel %p\n", sub->parent);
return;
}
/* Swap things around between the three-way and real call */
swap_subs(sub, sub_trans);
send_select_output(s, s->device->output, s->device->volume, MUTE_OFF);
if (s->device->height == 1) {
send_text(TEXT_LINE0, TEXT_NORMAL, s, s->device->phone_number);
} else {
send_text(TEXT_LINE0, TEXT_NORMAL, s, ustmtext("Calling (pre-transfer)", s));
send_text(TEXT_LINE1, TEXT_NORMAL, s, s->device->phone_number);
send_text(TEXT_LINE2, TEXT_NORMAL, s, ustmtext("Dialing...", s));
}
send_text_status(s, ustmtext("TransfrCancel", s));
if (ast_pthread_create(&sub->ss_thread, NULL, unistim_ss, c)) {
ast_log(LOG_WARNING, "Unable to start simple switch on channel %p\n", c);
sub->ss_thread = AST_PTHREADT_NULL;
ast_hangup(c);
return;
}
if (unistimdebug) {
ast_verb(0, "Started three way call on channel %p (%s) subchan %u\n",
sub_trans->owner, ast_channel_name(sub_trans->owner), sub_trans->subtype);
}
return;
}
softkey = get_avail_softkey(s, NULL);
if (softkey == -1) {
ast_log(LOG_WARNING, "Have no avail softkey for calling\n");
return;
}
sub = get_sub(s->device, SUB_REAL);
if (sub) { /* have already call assigned */
sub_hold(s, sub); /* Need to put on hold */
}
if (!(sub = unistim_alloc_sub(s->device, SUB_REAL))) {
ast_log(LOG_WARNING, "Unable to allocate subchannel!\n");
return;
}
sub->parent = s->device->sline[softkey];
s->device->ssub[softkey] = sub;
sub->softkey = softkey;
if (unistimdebug) {
ast_verb(0, "Using softkey %d, line %p\n", sub->softkey, sub->parent);
}
send_favorite_short(sub->softkey, FAV_ICON_OFFHOOK_BLACK, s);
s->device->selected = -1;
if (!sub->owner) { /* A call is already in progress ? */
RAII_VAR(struct ast_features_pickup_config *, pickup_cfg, NULL, ao2_cleanup);
const char *pickupexten;
c = unistim_new(sub, AST_STATE_DOWN, NULL, NULL); /* No, starting a new one */
if (!sub->rtp) { /* Need to start RTP before calling ast_pbx_run */
start_rtp(sub);
}
if (c) {
ast_channel_lock(c);
pickup_cfg = ast_get_chan_features_pickup_config(c);
if (!pickup_cfg) {
ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n");
pickupexten = "";
} else {
pickupexten = ast_strdupa(pickup_cfg->pickupexten);
}
ast_channel_unlock(c);
}
if (c && !strcmp(s->device->phone_number, pickupexten)) {
if (unistimdebug) {
ast_verb(0, "Try to pickup in unistim_new\n");
}
send_text(TEXT_LINE0, TEXT_NORMAL, s, "");
send_text_status(s, ustmtext(" Transf Hangup", s));
send_start_timer(s);
if (ast_pickup_call(c)) {
ast_log(LOG_NOTICE, "Nothing to pick up\n");
ast_channel_hangupcause_set(c, AST_CAUSE_CALL_REJECTED);
} else {
ast_channel_hangupcause_set(c, AST_CAUSE_NORMAL_CLEARING);
}
ast_hangup(c);
c = NULL;
} else if (c) {
send_select_output(s, s->device->output, s->device->volume, MUTE_OFF);
send_tone(s, 0, 0); /* Dialing empty number should also stop dial tone */
if (s->device->height == 1) {
if (strlen(s->device->phone_number) > 0) {
send_text(TEXT_LINE0, TEXT_NORMAL, s, s->device->phone_number);
} else {
send_text(TEXT_LINE0, TEXT_NORMAL, s, ustmtext("Calling...", s));
}
} else {
send_text(TEXT_LINE0, TEXT_NORMAL, s, ustmtext("Calling :", s));
send_text(TEXT_LINE1, TEXT_NORMAL, s, s->device->phone_number);
send_text(TEXT_LINE2, TEXT_NORMAL, s, ustmtext("Dialing...", s));
}
send_text_status(s, ustmtext(" Hangup", s));
/* start switch */
if (ast_pthread_create(&sub->ss_thread, NULL, unistim_ss, c)) {
ast_log(LOG_WARNING, "Unable to create switch thread\n");
sub->ss_thread = AST_PTHREADT_NULL;
ast_queue_hangup_with_cause(c, AST_CAUSE_SWITCH_CONGESTION);
}
} else
ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n",
sub->parent->name, s->device->name);
}