Add global variable for Law encoded silence
[lcr.git] / remote.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** LCR                                                                       **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** mISDN remote                                                              **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13
14 unsigned int new_remote = 1000;
15
16 /*
17  * constructor
18  */
19 Premote::Premote(int type, char *portname, struct port_settings *settings, struct interface *interface, int remote_id) : Port(type, portname, settings)
20 {
21         union parameter param;
22
23         p_callerinfo.itype = (interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
24         p_r_ref = new_remote++;
25         SCPY(p_r_remote_app, interface->remote_app);
26         SCPY(p_r_interface_name, interface->name);
27         p_r_tones = (interface->is_tones == IS_YES);
28
29         /* send new ref to remote socket */
30         memset(&param, 0, sizeof(union parameter));
31         if (type == PORT_TYPE_REMOTE_OUT)
32                 param.newref.direction = 1; /* new ref from lcr */
33         p_r_remote_id = remote_id;
34         if (admin_message_from_lcr(p_r_remote_id, p_r_ref, MESSAGE_NEWREF, &param) < 0)
35                 FATAL("No socket with remote application '%s' found, this shall not happen. because we already created one.\n", p_r_remote_app);
36
37         PDEBUG(DEBUG_PORT, "Created new RemotePort(%s).\n", portname);
38
39 }
40
41 /*
42  * destructor
43  */
44 Premote::~Premote()
45 {
46         PDEBUG(DEBUG_PORT, "Destroyed Remote process(%s).\n", p_name);
47 }
48
49 /*
50  * endpoint sends messages to the port
51  */
52 int Premote::message_epoint(unsigned int epoint_id, int message_type, union parameter *param)
53 {
54         struct epoint_list *epointlist;
55
56         if (Port::message_epoint(epoint_id, message_type, param))
57                 return 1;
58
59         if (message_type == MESSAGE_SETUP) {
60                 struct interface *interface;
61                 interface = getinterfacebyname(p_r_interface_name);
62                 if (!interface) {
63                         PERROR("Cannot find interface %s.\n", p_r_interface_name);
64                         return 0;
65                 }
66                 /* attach only if not already */
67                 epointlist = p_epointlist;
68                 while(epointlist) {
69                         if (epointlist->epoint_id == epoint_id)
70                                 break;
71                         epointlist = epointlist->next;
72                 }
73                 if (!epointlist)
74                         epointlist_new(epoint_id);
75
76                 /* set context to pbx */
77                 if (!param->setup.dialinginfo.context[0]) {
78                         if (interface->remote_context[0])
79                                 SCPY(param->setup.dialinginfo.context, interface->remote_context);
80                         else
81                                 SCPY(param->setup.dialinginfo.context, "lcr");
82                 }
83
84         }
85
86         /* look for Remote's interface */
87         if (admin_message_from_lcr(p_r_remote_id, p_r_ref, message_type, param)<0) {
88                 PERROR("No socket with remote application '%s' found, this shall not happen. Closing socket shall cause release of all remote ports.\n", p_r_remote_app);
89                 return 0;               
90         }
91
92 #if 0
93         /* enable audio path */
94         if (message_type == MESSAGE_SETUP) {
95                 union parameter newparam;
96                 memset(&newparam, 0, sizeof(union parameter));
97                 admin_message_from_lcr(p_r_remote_id, p_r_ref, MESSAGE_PATTERN, &newparam);
98                 newparam.audiopath = 1;
99                 admin_message_from_lcr(p_r_remote_id, p_r_ref, MESSAGE_AUDIOPATH, &newparam);
100         }
101 #endif
102
103         if (message_type == MESSAGE_CONNECT)
104                 new_state(PORT_STATE_CONNECT);
105
106         if (message_type == MESSAGE_RELEASE) {
107                 new_state(PORT_STATE_RELEASE);
108                 delete this;
109                 return 0;
110         }
111
112         return 0;
113 }
114
115 void Premote::message_remote(int message_type, union parameter *param)
116 {
117         class Endpoint *epoint;
118         struct lcr_msg *message;
119         struct interface *interface;
120
121         switch (message_type) {
122         case MESSAGE_TRAFFIC:
123                 bridge_tx(param->traffic.data, param->traffic.len);
124                 break;
125
126         case MESSAGE_SETUP:
127                 interface = getinterfacebyname(p_r_interface_name);
128                 if (!interface) {
129                         PERROR("Cannot find interface %s.\n", p_r_interface_name);
130                         return;
131                 }
132
133                 /* enable audio path */
134                 if (interface->is_tones == IS_YES) {
135                         union parameter newparam;
136                         memset(&newparam, 0, sizeof(union parameter));
137                         admin_message_from_lcr(p_r_remote_id, p_r_ref, MESSAGE_PATTERN, &newparam);
138                         newparam.audiopath = 1;
139                         admin_message_from_lcr(p_r_remote_id, p_r_ref, MESSAGE_AUDIOPATH, &newparam);
140                 }
141
142                 /* set source interface */
143                 param->setup.callerinfo.itype = p_callerinfo.itype;
144                 SCPY(param->setup.callerinfo.interface, interface->name);
145                 
146                 /* create endpoint */
147                 if (p_epointlist)
148                         FATAL("Incoming call but already got an endpoint.\n");
149                 if (!(epoint = new Endpoint(p_serial, 0)))
150                         FATAL("No memory for Endpoint instance\n");
151                 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
152
153                 epointlist_new(epoint->ep_serial);
154                 /* FALL THROUGH: */
155         default:
156                 if (message_type == MESSAGE_CONNECT)
157                         new_state(PORT_STATE_CONNECT);
158                 /* cannot just forward, because param is not of container "struct lcr_msg" */
159                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, message_type);
160                 memcpy(&message->param, param, sizeof(message->param));
161                 message_put(message);
162
163                 if (message_type == MESSAGE_RELEASE) {
164                         new_state(PORT_STATE_RELEASE);
165                         delete this;
166                         return;
167                 }
168         }
169 }
170
171 /* receive from remote Port instance */
172 int Premote::bridge_rx(unsigned char *data, int len)
173 {
174         union parameter newparam;
175         int l;
176
177         /* don't send tones, if not enabled or not connected */
178         if (!p_r_tones
179          && p_state != PORT_STATE_CONNECT)
180                 return 0;
181
182         memset(&newparam, 0, sizeof(union parameter));
183         /* split, if exeeds data size */
184         while(len) {
185                 l = (len > (int)sizeof(newparam.traffic.data)) ? sizeof(newparam.traffic.data) : len;
186                 newparam.traffic.len = l;
187                 len -= l;
188                 memcpy(newparam.traffic.data, data, l);
189                 data += l;
190                 admin_message_from_lcr(p_r_remote_id, p_r_ref, MESSAGE_TRAFFIC, &newparam);
191         }
192
193         return 0;
194 }
195
196