6fcb64bb5848756fed3de04a7e6daa038048c1b9
[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, interface)
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         p_r_tones = (interface->is_tones == IS_YES);
27
28         /* send new ref to remote socket */
29         memset(&param, 0, sizeof(union parameter));
30         if (type == PORT_TYPE_REMOTE_OUT)
31                 param.newref.direction = 1; /* new ref from lcr */
32         p_r_remote_id = remote_id;
33         if (admin_message_from_lcr(p_r_remote_id, p_r_ref, MESSAGE_NEWREF, &param) < 0)
34                 FATAL("No socket with remote application '%s' found, this shall not happen. because we already created one.\n", p_r_remote_app);
35
36         PDEBUG(DEBUG_PORT, "Created new RemotePort(%s).\n", portname);
37
38 }
39
40 /*
41  * destructor
42  */
43 Premote::~Premote()
44 {
45         PDEBUG(DEBUG_PORT, "Destroyed Remote process(%s).\n", p_name);
46 }
47
48 /*
49  * endpoint sends messages to the port
50  */
51 int Premote::message_epoint(unsigned int epoint_id, int message_type, union parameter *param)
52 {
53         struct epoint_list *epointlist;
54
55         if (Port::message_epoint(epoint_id, message_type, param))
56                 return 1;
57
58         switch (message_type) {
59         case MESSAGE_SETUP:
60                 struct interface *interface;
61                 interface = getinterfacebyname(p_interface_name);
62                 if (!interface) {
63                         PERROR("Cannot find interface %s.\n", p_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                 /* screen */
84                 memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
85                 memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
86                 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_interface_name);
87                 do_screen(1, p_callerinfo.id2, sizeof(p_callerinfo.id2), &p_callerinfo.ntype2, &p_callerinfo.present2, p_interface_name);
88                 do_screen(1, p_redirinfo.id, sizeof(p_redirinfo.id), &p_redirinfo.ntype, &p_redirinfo.present, p_interface_name);
89                 memcpy(&param->setup.callerinfo, &p_callerinfo, sizeof(p_callerinfo));
90                 memcpy(&param->setup.redirinfo, &p_redirinfo, sizeof(p_redirinfo));
91
92                 new_state(PORT_STATE_OUT_SETUP);
93                 break;
94
95         case MESSAGE_PROCEEDING:
96                 new_state(PORT_STATE_IN_PROCEEDING);
97                 break;
98
99         case MESSAGE_ALERTING:
100                 new_state(PORT_STATE_IN_ALERTING);
101                 break;
102
103         case MESSAGE_CONNECT:
104                 new_state(PORT_STATE_CONNECT);
105                 break;
106
107         case MESSAGE_DISCONNECT:
108                 new_state(PORT_STATE_OUT_DISCONNECT);
109                 break;
110
111         case MESSAGE_RELEASE:
112                 new_state(PORT_STATE_RELEASE);
113                 break;
114         }
115
116         /* look for Remote's interface */
117         if (admin_message_from_lcr(p_r_remote_id, p_r_ref, message_type, param)<0) {
118                 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);
119                 return 0;               
120         }
121
122         if (message_type == MESSAGE_RELEASE) {
123                 new_state(PORT_STATE_RELEASE);
124                 delete this;
125                 return 0;
126         }
127
128         return 0;
129 }
130
131 void Premote::message_remote(int message_type, union parameter *param)
132 {
133         class Endpoint *epoint;
134         struct lcr_msg *message;
135         struct interface *interface;
136
137         switch (message_type) {
138         case MESSAGE_TRAFFIC:
139                 bridge_tx(param->traffic.data, param->traffic.len);
140                 if (p_tone_name[0]) {
141                         read_audio(param->traffic.data, param->traffic.len);
142                         admin_message_from_lcr(p_r_remote_id, p_r_ref, MESSAGE_TRAFFIC, param);
143                 }
144                 break;
145
146         case MESSAGE_SETUP:
147                 interface = getinterfacebyname(p_interface_name);
148                 if (!interface) {
149                         PERROR("Cannot find interface %s.\n", p_interface_name);
150                         return;
151                 }
152
153                 /* enable audio path */
154                 if (interface->is_tones == IS_YES) {
155                         union parameter newparam;
156                         memset(&newparam, 0, sizeof(union parameter));
157                         admin_message_from_lcr(p_r_remote_id, p_r_ref, MESSAGE_PATTERN, &newparam);
158                         newparam.audiopath = 1;
159                         admin_message_from_lcr(p_r_remote_id, p_r_ref, MESSAGE_AUDIOPATH, &newparam);
160                 }
161
162                 /* set source interface */
163                 param->setup.callerinfo.itype = p_callerinfo.itype;
164                 SCPY(param->setup.callerinfo.interface, interface->name);
165                 
166                 /* create endpoint */
167                 if (p_epointlist)
168                         FATAL("Incoming call but already got an endpoint.\n");
169                 if (!(epoint = new Endpoint(p_serial, 0)))
170                         FATAL("No memory for Endpoint instance\n");
171                 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
172
173                 epointlist_new(epoint->ep_serial);
174
175                 new_state(PORT_STATE_IN_SETUP);
176                 break;
177
178         case MESSAGE_PROCEEDING:
179                 new_state(PORT_STATE_OUT_PROCEEDING);
180                 break;
181
182         case MESSAGE_ALERTING:
183                 new_state(PORT_STATE_OUT_ALERTING);
184                 break;
185
186         case MESSAGE_CONNECT:
187                 new_state(PORT_STATE_CONNECT);
188                 break;
189
190         case MESSAGE_DISCONNECT:
191                 new_state(PORT_STATE_IN_DISCONNECT);
192                 break;
193
194         case MESSAGE_RELEASE:
195                 new_state(PORT_STATE_RELEASE);
196                 break;
197         }
198
199         /* cannot just forward, because param is not of container "struct lcr_msg" */
200         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, message_type);
201         memcpy(&message->param, param, sizeof(message->param));
202         message_put(message);
203
204         if (message_type == MESSAGE_RELEASE) {
205                 new_state(PORT_STATE_RELEASE);
206                 delete this;
207                 return;
208         }
209 }
210
211 /* receive from remote Port instance */
212 int Premote::bridge_rx(unsigned char *data, int len)
213 {
214         union parameter newparam;
215         int l;
216
217         /* don't send tones, if not enabled or not connected */
218         if (!p_r_tones
219          && p_state != PORT_STATE_CONNECT)
220                 return 0;
221
222         if (p_tone_name[0])
223                 return 0;
224
225         memset(&newparam, 0, sizeof(union parameter));
226         /* split, if exeeds data size */
227         while(len) {
228                 l = (len > (int)sizeof(newparam.traffic.data)) ? sizeof(newparam.traffic.data) : len;
229                 newparam.traffic.len = l;
230                 len -= l;
231                 memcpy(newparam.traffic.data, data, l);
232                 data += l;
233                 admin_message_from_lcr(p_r_remote_id, p_r_ref, MESSAGE_TRAFFIC, &newparam);
234         }
235
236         return 0;
237 }
238
239