blob: 675db0c973878bf15c44ed2444508ba7794e0c36 [file] [log] [blame]
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +08001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2
3/*
Holger Hans Peter Freytherdcc33122011-03-01 01:24:10 +01004 * (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2009-2011 by On-Waves
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +08006 * All Rights Reserved
7 *
Holger Hans Peter Freytherdcc33122011-03-01 01:24:10 +01008 * This program is free software; you can redistribute it and/or modify
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +01009 * it under the terms of the GNU Affero General Public License as published by
Holger Hans Peter Freytherdcc33122011-03-01 01:24:10 +010010 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +010016 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080017 *
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080020 *
21 */
22
23#ifndef OPENBSC_MGCP_H
24#define OPENBSC_MGCP_H
25
Holger Hans Peter Freytherdcc33122011-03-01 01:24:10 +010026#include "cellmgr_debug.h"
27
Holger Hans Peter Freythercbf7d182010-07-31 05:25:35 +080028#include <osmocore/msgb.h>
Holger Hans Peter Freytherdcc33122011-03-01 01:24:10 +010029#include <osmocore/write_queue.h>
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080030
31#include <arpa/inet.h>
32
33#define RTP_PORT_DEFAULT 4000
Holger Hans Peter Freytherdcc33122011-03-01 01:24:10 +010034#define RTP_PORT_NET_DEFAULT 16000
Holger Hans Peter Freytherd86208d2010-08-04 06:11:27 +080035
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080036/**
37 * Calculate the RTP audio port for the given multiplex
38 * and the direction. This allows a semi static endpoint
39 * to port calculation removing the need for the BSC
40 * and the MediaGateway to communicate.
41 *
42 * Port usage explained:
43 * base + (multiplex * 2) + 0 == local port to wait for network packets
44 * base + (multiplex * 2) + 1 == local port for rtcp
45 *
46 * The above port will receive packets from the BTS that need
47 * to be patched and forwarded to the network.
48 * The above port will receive packets from the network that
49 * need to be patched and forwarded to the BTS.
50 *
51 * We assume to have a static BTS IP address so we can differentiate
52 * network and BTS.
53 *
54 */
55static inline int rtp_calculate_port(int multiplex, int base)
56{
57 return base + (multiplex * 2);
58}
59
60
61/*
62 * Handling of MGCP Endpoints and the MGCP Config
63 */
64struct mgcp_endpoint;
65struct mgcp_config;
Holger Hans Peter Freytherdcc33122011-03-01 01:24:10 +010066struct mgcp_trunk_config;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080067
68#define MGCP_ENDP_CRCX 1
69#define MGCP_ENDP_DLCX 2
70#define MGCP_ENDP_MDCX 3
71
72/*
73 * what to do with the msg?
74 * - continue as usual?
75 * - reject and send a failure code?
76 * - defer? do not send anything
77 */
78#define MGCP_POLICY_CONT 4
79#define MGCP_POLICY_REJECT 5
80#define MGCP_POLICY_DEFER 6
81
Holger Hans Peter Freytherdcc33122011-03-01 01:24:10 +010082typedef int (*mgcp_realloc)(struct mgcp_trunk_config *cfg, int endpoint);
83typedef int (*mgcp_change)(struct mgcp_trunk_config *cfg, int endpoint, int state);
84typedef int (*mgcp_policy)(struct mgcp_trunk_config *cfg, int endpoint, int state, const char *transactio_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080085typedef int (*mgcp_reset)(struct mgcp_config *cfg);
86
Holger Hans Peter Freytherdcc33122011-03-01 01:24:10 +010087#define PORT_ALLOC_STATIC 0
88#define PORT_ALLOC_DYNAMIC 1
89
90/**
91 * This holds information on how to allocate ports
92 */
93struct mgcp_port_range {
94 int mode;
95
96 /* pre-allocated from a base? */
97 int base_port;
98
99 /* dynamically allocated */
100 int range_start;
101 int range_end;
102 int last_port;
103};
104
105struct mgcp_trunk_config {
106 struct llist_head entry;
107
108 struct mgcp_config *cfg;
109
110 int trunk_nr;
111 int trunk_type;
112
113 char *audio_name;
114 int audio_payload;
115 int audio_loop;
116
117 /* spec handling */
118 int force_realloc;
119
120 unsigned int number_endpoints;
121 struct mgcp_endpoint *endpoints;
122
123 /* Special MGW handling */
124 int target_trunk_start;
125 int trunk_base;
126 int endp_offset;
127 int vad_enabled;
128
129 int digital_inp_gain;
130 int digital_out_gain;
131 int upstr_agc_enbl;
132 int upstr_adp_rate;
133 int upstr_max_gain;
134 int upstr_target_lvl;
135 int dwnstr_agc_enbl;
136 int dwnstr_adp_rate;
137 int dwnstr_max_gain;
138 int dwnstr_target_lvl;
139};
140
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800141struct mgcp_config {
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800142 int source_port;
143 char *local_ip;
144 char *source_addr;
145 char *bts_ip;
146 char *call_agent_addr;
147
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800148 struct in_addr bts_in;
Holger Hans Peter Freytherdcc33122011-03-01 01:24:10 +0100149
150 /* transcoder handling */
151 char *transcoder_ip;
152 struct in_addr transcoder_in;
153 int transcoder_remote_base;
154
155 struct write_queue gw_fd;
156
157 struct mgcp_port_range bts_ports;
158 struct mgcp_port_range net_ports;
159 struct mgcp_port_range transcoder_ports;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800160 int endp_dscp;
161
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800162 mgcp_change change_cb;
163 mgcp_policy policy_cb;
164 mgcp_reset reset_cb;
Holger Hans Peter Freythere40bc382010-09-18 03:11:00 +0800165 mgcp_realloc realloc_cb;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800166 void *data;
Holger Hans Peter Freytherd86208d2010-08-04 06:11:27 +0800167
Holger Hans Peter Freyther5b084012010-08-08 07:51:51 +0800168 uint32_t last_call_id;
Holger Hans Peter Freytherdcc33122011-03-01 01:24:10 +0100169
170 /* trunk handling */
171 struct mgcp_trunk_config trunk;
172 struct llist_head trunks;
173
174 /* only used for start with a static configuration */
175 int last_net_port;
176 int last_bts_port;
177
178 /* MGW handling */
179 int configure_trunks;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800180};
181
182/* config management */
183struct mgcp_config *mgcp_config_alloc(void);
184int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg);
185int mgcp_vty_init(void);
Holger Hans Peter Freytherdcc33122011-03-01 01:24:10 +0100186int mgcp_endpoints_allocate(struct mgcp_trunk_config *cfg);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800187void mgcp_free_endp(struct mgcp_endpoint *endp);
Holger Hans Peter Freytherdcc33122011-03-01 01:24:10 +0100188int mgcp_reset_transcoder(struct mgcp_config *cfg);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800189
190/*
191 * format helper functions
192 */
193struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg);
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100194struct msgb *mgcp_create_response_with_data(int code, const char *txt, const char *msg, const char *trans, const char *data);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800195
196/* adc helper */
197static inline int mgcp_timeslot_to_endpoint(int multiplex, int timeslot)
198{
Holger Hans Peter Freytherdcc33122011-03-01 01:24:10 +0100199 if (timeslot == 0) {
200 LOGP(DMGCP, LOGL_ERROR, "Timeslot should not be 0\n");
201 timeslot = 255;
202 }
203
204 return timeslot + (32 * multiplex);
205}
206
207static inline void mgcp_endpoint_to_timeslot(int endpoint, int *multiplex, int *timeslot)
208{
209 *multiplex = endpoint / 32;
210 *timeslot = endpoint % 32;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800211}
212
213
214#endif