1 /*
2    This file contains all data structures and definitions associated
3    with facility message usage and the ROSE components included
4    within those messages.
5 
6    by Matthew Fredrickson <creslin@digium.com>
7    Copyright (C) Digium, Inc. 2004-2005
8 */
9 
10 /*
11  * See http://www.asterisk.org for more information about
12  * the Asterisk project. Please do not directly contact
13  * any of the maintainers of this project for assistance;
14  * the project provides a web site, mailing lists and IRC
15  * channels for your use.
16  *
17  * This program is free software, distributed under the terms of
18  * the GNU General Public License Version 2 as published by the
19  * Free Software Foundation. See the LICENSE file included with
20  * this program for more details.
21  *
22  * In addition, when this program is distributed with Asterisk in
23  * any form that would qualify as a 'combined work' or as a
24  * 'derivative work' (but not mere aggregation), you can redistribute
25  * and/or modify the combination under the terms of the license
26  * provided with that copy of Asterisk, instead of the license
27  * terms granted here.
28  */
29 
30 #ifndef _PRI_FACILITY_H
31 #define _PRI_FACILITY_H
32 #include "pri_q931.h"
33 #include "rose.h"
34 
35 /* Protocol Profile field */
36 #define Q932_PROTOCOL_MASK			0x1F
37 #define Q932_PROTOCOL_ROSE			0x11	/* X.219 & X.229 */
38 #define Q932_PROTOCOL_CMIP			0x12	/* Q.941 */
39 #define Q932_PROTOCOL_ACSE			0x13	/* X.217 & X.227 */
40 #define Q932_PROTOCOL_GAT			0x16
41 #define Q932_PROTOCOL_EXTENSIONS	0x1F
42 
43 /* Q.952 Divert cause */
44 #define Q952_DIVERT_REASON_UNKNOWN		0x00
45 #define Q952_DIVERT_REASON_CFU			0x01
46 #define Q952_DIVERT_REASON_CFB			0x02
47 #define Q952_DIVERT_REASON_CFNR			0x03
48 #define Q952_DIVERT_REASON_CD			0x04
49 #define Q952_DIVERT_REASON_IMMEDIATE	0x05
50 /* Q.SIG Divert cause. Listed in ECMA-174 */
51 #define QSIG_DIVERT_REASON_UNKNOWN		0x00	/* Call forward unknown reason */
52 #define QSIG_DIVERT_REASON_CFU			0x01	/* Call Forward Unconditional (other reason) */
53 #define QSIG_DIVERT_REASON_CFB			0x02	/* Call Forward Busy */
54 #define QSIG_DIVERT_REASON_CFNR			0x03	/* Call Forward No Reply */
55 
56 /* Q.932 Type of number */
57 #define Q932_TON_UNKNOWN				0x00
58 #define Q932_TON_INTERNATIONAL			0x01
59 #define Q932_TON_NATIONAL				0x02
60 #define Q932_TON_NET_SPECIFIC			0x03
61 #define Q932_TON_SUBSCRIBER				0x04
62 #define Q932_TON_ABBREVIATED			0x06
63 
64 /* Q.SIG Subscription Option. Listed in ECMA-174 */
65 #define QSIG_NO_NOTIFICATION						0x00
66 #define QSIG_NOTIFICATION_WITHOUT_DIVERTED_TO_NR	0x01
67 #define QSIG_NOTIFICATION_WITH_DIVERTED_TO_NR		0x02
68 
69 /*! Reasons an APDU callback is called. */
70 enum APDU_CALLBACK_REASON {
71 	/*!
72 	 * \brief Transmit facility ie setup error.  Abort and cleanup.
73 	 * \note The message may or may not actually get sent.
74 	 * \note The callback cannot generate an event subcmd.
75 	 * \note The callback should not send messages.  Out of order messages will result.
76 	 */
77 	APDU_CALLBACK_REASON_ERROR,
78 	/*!
79 	 * \brief Abort and cleanup.
80 	 * \note The APDU queue is being destroyed.
81 	 * \note The callback cannot generate an event subcmd.
82 	 * \note The callback cannot send messages as the call is likely being destroyed.
83 	 */
84 	APDU_CALLBACK_REASON_CLEANUP,
85 	/*!
86 	 * \brief Timeout waiting for responses to the message.
87 	 * \note The callback can generate an event subcmd.
88 	 * \note The callback can send messages.
89 	 */
90 	APDU_CALLBACK_REASON_TIMEOUT,
91 	/*!
92 	 * \brief Received a facility response message.
93 	 * \note The callback can generate an event subcmd.
94 	 * \note The callback can send messages.
95 	 */
96 	APDU_CALLBACK_REASON_MSG_RESULT,
97 	/*!
98 	 * \brief Received a facility error message.
99 	 * \note The callback can generate an event subcmd.
100 	 * \note The callback can send messages.
101 	 */
102 	APDU_CALLBACK_REASON_MSG_ERROR,
103 	/*!
104 	 * \brief Received a facility reject message.
105 	 * \note The callback can generate an event subcmd.
106 	 * \note The callback can send messages.
107 	 */
108 	APDU_CALLBACK_REASON_MSG_REJECT,
109 };
110 
111 struct apdu_msg_data {
112 	/*! Decoded response message contents. */
113 	union {
114 		const struct rose_msg_result *result;
115 		const struct rose_msg_error *error;
116 		const struct rose_msg_reject *reject;
117 	} response;
118 	/*! Q.931 message type the response came in with. */
119 	int type;
120 };
121 
122 union apdu_callback_param {
123 	void *ptr;
124 	long value;
125 	char pad[8];
126 };
127 
128 /* So calls to pri_call_apdu_find() will not find an aliased event. */
129 #define APDU_INVALID_INVOKE_ID  0x10000
130 
131 #define APDU_TIMEOUT_MSGS_ONLY	-1
132 
133 struct apdu_callback_data {
134 	/*! APDU invoke id to match with any response messages. (Result/Error/Reject) */
135 	int invoke_id;
136 	/*!
137 	 * \brief Time to wait for responses to APDU in ms.
138 	 * \note Set to 0 if send the message only.
139 	 * \note Set to APDU_TIMEOUT_MSGS_ONLY to "timeout" with the message_type list only.
140 	 */
141 	int timeout_time;
142 	/*! Number of Q.931 messages the APDU can "timeout" on. */
143 	unsigned num_messages;
144 	/*! Q.931 message list to "timeout" on. */
145 	int message_type[5];
146 	/*!
147 	 * \brief APDU callback function.
148 	 *
149 	 * \param reason Reason callback is called.
150 	 * \param ctrl D channel controller.
151 	 * \param call Q.931 call leg.
152 	 * \param apdu APDU queued entry.  Do not change!
153 	 * \param msg APDU response message data.  (NULL if was not the reason called.)
154 	 *
155 	 * \note
156 	 * A callback must be supplied if the sender cares about any APDU_CALLBACK_REASON.
157 	 *
158 	 * \return TRUE if no more responses are expected.
159 	 */
160 	int (*callback)(enum APDU_CALLBACK_REASON reason, struct pri *ctrl, struct q931_call *call, struct apdu_event *apdu, const struct apdu_msg_data *msg);
161 	/*! \brief Sender data for the callback function to identify the particular APDU. */
162 	union apdu_callback_param user;
163 };
164 
165 struct apdu_event {
166 	/*! Linked list pointer */
167 	struct apdu_event *next;
168 	/*! TRUE if this APDU has been sent. */
169 	int sent;
170 	/*! What message to send the ADPU in */
171 	int message;
172 	/*! Sender supplied information to handle APDU response messages. */
173 	struct apdu_callback_data response;
174 	/*! Q.931 call leg.  (Needed for the APDU timeout.) */
175 	struct q931_call *call;
176 	/*! Response timeout timer. */
177 	int timer;
178 	/*! Length of ADPU */
179 	int apdu_len;
180 	/*! ADPU to send */
181 	unsigned char apdu[255];
182 };
183 
184 void rose_copy_number_to_q931(struct pri *ctrl, struct q931_party_number *q931_number, const struct rosePartyNumber *rose_number);
185 void rose_copy_subaddress_to_q931(struct pri *ctrl, struct q931_party_subaddress *q931_subaddress, const struct rosePartySubaddress *rose_subaddress);
186 void rose_copy_address_to_q931(struct pri *ctrl, struct q931_party_address *q931_address, const struct roseAddress *rose_address);
187 void rose_copy_address_to_id_q931(struct pri *ctrl, struct q931_party_id *q931_address, const struct roseAddress *rose_address);
188 void rose_copy_presented_number_screened_to_q931(struct pri *ctrl, struct q931_party_number *q931_number, const struct rosePresentedNumberScreened *rose_presented);
189 void rose_copy_presented_number_unscreened_to_q931(struct pri *ctrl, struct q931_party_number *q931_number, const struct rosePresentedNumberUnscreened *rose_presented);
190 void rose_copy_presented_address_screened_to_id_q931(struct pri *ctrl, struct q931_party_id *q931_address, const struct rosePresentedAddressScreened *rose_presented);
191 void rose_copy_name_to_q931(struct pri *ctrl, struct q931_party_name *qsig_name, const struct roseQsigName *rose_name);
192 
193 void q931_copy_number_to_rose(struct pri *ctrl, struct rosePartyNumber *rose_number, const struct q931_party_number *q931_number);
194 void q931_copy_subaddress_to_rose(struct pri *ctrl, struct rosePartySubaddress *rose_subaddress, const struct q931_party_subaddress *q931_subaddress);
195 void q931_copy_address_to_rose(struct pri *ctrl, struct roseAddress *rose_address, const struct q931_party_address *q931_address);
196 void q931_copy_id_address_to_rose(struct pri *ctrl, struct roseAddress *rose_address, const struct q931_party_id *q931_address);
197 void q931_copy_presented_number_screened_to_rose(struct pri *ctrl, struct rosePresentedNumberScreened *rose_presented, const struct q931_party_number *q931_number);
198 void q931_copy_presented_number_unscreened_to_rose(struct pri *ctrl, struct rosePresentedNumberUnscreened *rose_presented, const struct q931_party_number *q931_number);
199 void q931_copy_presented_id_address_screened_to_rose(struct pri *ctrl, struct rosePresentedAddressScreened *rose_presented, const struct q931_party_id *q931_address);
200 void q931_copy_name_to_rose(struct pri *ctrl, struct roseQsigName *rose_name, const struct q931_party_name *qsig_name);
201 
202 int rose_error_msg_encode(struct pri *ctrl, q931_call *call, int msgtype, int invoke_id, enum rose_error_code code);
203 int send_facility_error(struct pri *ctrl, q931_call *call, int invoke_id, enum rose_error_code code);
204 int rose_result_ok_encode(struct pri *ctrl, q931_call *call, int msgtype, int invoke_id);
205 int send_facility_result_ok(struct pri *ctrl, q931_call *call, int invoke_id);
206 
207 /* Queues an MWI apdu on a the given call */
208 int mwi_message_send(struct pri *pri, q931_call *call, struct pri_sr *req, int activate);
209 
210 /* starts a 2BCT */
211 int eect_initiate_transfer(struct pri *pri, q931_call *c1, q931_call *c2);
212 
213 int rlt_initiate_transfer(struct pri *pri, q931_call *c1, q931_call *c2);
214 
215 /* starts a QSIG Path Replacement */
216 int anfpr_initiate_transfer(struct pri *pri, q931_call *c1, q931_call *c2);
217 
218 int etsi_initiate_transfer(struct pri *ctrl, q931_call *call_1, q931_call *call_2);
219 
220 int qsig_cf_callrerouting(struct pri *pri, q931_call *c, const char* dest, const char* original, const char* reason);
221 int send_reroute_request(struct pri *ctrl, q931_call *call, const struct q931_party_id *caller, const struct q931_party_redirecting *deflection, int subscription_option);
222 
223 int send_call_transfer_complete(struct pri *pri, q931_call *call, int call_status);
224 int rose_request_subaddress_encode(struct pri *ctrl, struct q931_call *call);
225 int send_subaddress_transfer(struct pri *ctrl, struct q931_call *call);
226 
227 int rose_diverting_leg_information1_encode(struct pri *pri, q931_call *call);
228 int rose_diverting_leg_information3_encode(struct pri *pri, q931_call *call, int messagetype);
229 
230 int rose_connected_name_encode(struct pri *pri, q931_call *call, int messagetype);
231 int rose_called_name_encode(struct pri *pri, q931_call *call, int messagetype);
232 
233 int pri_call_apdu_queue(q931_call *call, int messagetype, const unsigned char *apdu, int apdu_len, struct apdu_callback_data *response);
234 void pri_call_apdu_queue_cleanup(q931_call *call);
235 struct apdu_event *pri_call_apdu_find(struct q931_call *call, int invoke_id);
236 int pri_call_apdu_extract(struct q931_call *call, struct apdu_event *extract);
237 void pri_call_apdu_delete(struct q931_call *call, struct apdu_event *doomed);
238 
239 /* Adds the "standard" APDUs to a call */
240 int pri_call_add_standard_apdus(struct pri *pri, q931_call *call);
241 
242 void asn1_dump(struct pri *ctrl, const unsigned char *start_asn1, const unsigned char *end);
243 
244 void rose_handle_invoke(struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, const struct fac_extension_header *header, const struct rose_msg_invoke *invoke);
245 void rose_handle_result(struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, const struct fac_extension_header *header, const struct rose_msg_result *result);
246 void rose_handle_error(struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, const struct fac_extension_header *header, const struct rose_msg_error *error);
247 void rose_handle_reject(struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, const struct fac_extension_header *header, const struct rose_msg_reject *reject);
248 
249 int pri_cc_interrogate_rsp(struct pri *ctrl, q931_call *call, const struct rose_msg_invoke *invoke);
250 void pri_cc_ptmp_request(struct pri *ctrl, q931_call *call, const struct rose_msg_invoke *invoke);
251 void pri_cc_ptp_request(struct pri *ctrl, q931_call *call, int msgtype, const struct rose_msg_invoke *invoke);
252 void pri_cc_qsig_request(struct pri *ctrl, q931_call *call, int msgtype, const struct rose_msg_invoke *invoke);
253 void pri_cc_qsig_cancel(struct pri *ctrl, q931_call *call, int msgtype, const struct rose_msg_invoke *invoke);
254 void pri_cc_qsig_exec_possible(struct pri *ctrl, q931_call *call, int msgtype, const struct rose_msg_invoke *invoke);
255 
256 int aoc_charging_request_send(struct pri *ctrl, q931_call *c, enum PRI_AOC_REQUEST aoc_request_flag);
257 void aoc_etsi_aoc_request(struct pri *ctrl, q931_call *call, const struct rose_msg_invoke *invoke);
258 void aoc_etsi_aoc_s_currency(struct pri *ctrl, const struct rose_msg_invoke *invoke);
259 void aoc_etsi_aoc_s_special_arrangement(struct pri *ctrl, const struct rose_msg_invoke *invoke);
260 void aoc_etsi_aoc_d_currency(struct pri *ctrl, const struct rose_msg_invoke *invoke);
261 void aoc_etsi_aoc_d_charging_unit(struct pri *ctrl, const struct rose_msg_invoke *invoke);
262 void aoc_etsi_aoc_e_currency(struct pri *ctrl, q931_call *call, const struct rose_msg_invoke *invoke);
263 void aoc_etsi_aoc_e_charging_unit(struct pri *ctrl, q931_call *call, const struct rose_msg_invoke *invoke);
264 
265 #endif /* _PRI_FACILITY_H */
266