1 /*
2  * libpri: An implementation of Primary Rate ISDN
3  *
4  * Written by Mark Spencer <markster@digium.com>
5  *
6  * Copyright (C) 2001, Digium, Inc.
7  * All Rights Reserved.
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_Q931_H
31 #define _PRI_Q931_H
32 
33 typedef enum q931_mode {
34 	UNKNOWN_MODE,
35 	CIRCUIT_MODE,
36 	PACKET_MODE
37 } q931_mode;
38 
39 typedef struct q931_h {
40 	unsigned char raw[0];
41 	u_int8_t pd;		/* Protocol Discriminator */
42 #if __BYTE_ORDER == __BIG_ENDIAN
43 	u_int8_t x0:4;
44 	u_int8_t crlen:4;/*!< Call reference length */
45 #else
46 	u_int8_t crlen:4;/*!< Call reference length */
47 	u_int8_t x0:4;
48 #endif
49 	u_int8_t contents[0];
50 	u_int8_t crv[3];/*!< Call reference value */
51 } __attribute__ ((packed)) q931_h;
52 
53 
54 /* Message type header */
55 typedef struct q931_mh {
56 #if __BYTE_ORDER == __BIG_ENDIAN
57 	u_int8_t f:1;
58 	u_int8_t msg:7;
59 #else
60 	u_int8_t msg:7;
61 	u_int8_t f:1;
62 #endif
63 	u_int8_t data[0];
64 } __attribute__ ((packed)) q931_mh;
65 
66 /* Information element format */
67 typedef struct q931_ie {
68 	u_int8_t ie;
69 	u_int8_t len;
70 	u_int8_t data[0];
71 } __attribute__ ((packed)) q931_ie;
72 
73 #define Q931_RES_HAVEEVENT (1 << 0)
74 #define Q931_RES_INERRROR  (1 << 1)
75 
76 #define Q931_PROTOCOL_DISCRIMINATOR 0x08
77 #define GR303_PROTOCOL_DISCRIMINATOR 0x4f
78 /* AT&T Maintenance Protocol Discriminator */
79 #define MAINTENANCE_PROTOCOL_DISCRIMINATOR_1 0x03
80 /* National Maintenance Protocol Discriminator */
81 #define MAINTENANCE_PROTOCOL_DISCRIMINATOR_2 0x43
82 
83 /* Q.931 / National ISDN Message Types */
84 
85 /*! Send this facility APDU on the next message to go out. */
86 #define Q931_ANY_MESSAGE			-1
87 
88 /* Call Establishment Messages */
89 #define Q931_ALERTING 				0x01
90 #define Q931_CALL_PROCEEDING		0x02
91 #define Q931_CONNECT				0x07
92 #define Q931_CONNECT_ACKNOWLEDGE	0x0f
93 #define Q931_PROGRESS				0x03
94 #define Q931_SETUP					0x05
95 #define Q931_SETUP_ACKNOWLEDGE		0x0d
96 
97 /* Call Disestablishment Messages */
98 #define Q931_DISCONNECT				0x45
99 #define Q931_RELEASE				0x4d
100 #define Q931_RELEASE_COMPLETE		0x5a
101 #define Q931_RESTART				0x46
102 #define Q931_RESTART_ACKNOWLEDGE	0x4e
103 
104 /* Miscellaneous Messages */
105 #define Q931_STATUS					0x7d
106 #define Q931_STATUS_ENQUIRY			0x75
107 #define Q931_USER_INFORMATION		0x20
108 #define Q931_SEGMENT				0x60
109 #define Q931_CONGESTION_CONTROL		0x79
110 #define Q931_INFORMATION			0x7b
111 #define Q931_FACILITY				0x62
112 #define Q931_REGISTER				0x64	/* Q.932 */
113 #define Q931_NOTIFY					0x6e
114 
115 /* Call Management Messages */
116 #define Q931_HOLD					0x24
117 #define Q931_HOLD_ACKNOWLEDGE		0x28
118 #define Q931_HOLD_REJECT			0x30
119 #define Q931_RETRIEVE				0x31
120 #define Q931_RETRIEVE_ACKNOWLEDGE	0x33
121 #define Q931_RETRIEVE_REJECT		0x37
122 #define Q931_RESUME					0x26
123 #define Q931_RESUME_ACKNOWLEDGE		0x2e
124 #define Q931_RESUME_REJECT			0x22
125 #define Q931_SUSPEND				0x25
126 #define Q931_SUSPEND_ACKNOWLEDGE	0x2d
127 #define Q931_SUSPEND_REJECT			0x21
128 
129 /* Maintenance messages (codeset 0 only) */
130 #define ATT_SERVICE                 0x0f
131 #define ATT_SERVICE_ACKNOWLEDGE     0x07
132 #define NATIONAL_SERVICE            0x07
133 #define NATIONAL_SERVICE_ACKNOWLEDGE 0x0f
134 
135 #define SERVICE_CHANGE_STATUS_INSERVICE           0
136 #define SERVICE_CHANGE_STATUS_LOOPBACK            1  /* not supported */
137 #define SERVICE_CHANGE_STATUS_OUTOFSERVICE        2
138 #define SERVICE_CHANGE_STATUS_REQCONTINUITYCHECK  3  /* not supported */
139 #define SERVICE_CHANGE_STATUS_SHUTDOWN            4  /* not supported */
140 
141 /* Q.931 / National ISDN Information Elements */
142 #define Q931_LOCKING_SHIFT			0x90
143 #define Q931_NON_LOCKING_SHIFT		0x98
144 #define Q931_BEARER_CAPABILITY		0x04
145 #define Q931_CAUSE					0x08
146 #define Q931_IE_CALL_STATE			0x14
147 #define Q931_CHANNEL_IDENT			0x18
148 #define Q931_PROGRESS_INDICATOR		0x1e
149 #define Q931_NETWORK_SPEC_FAC		0x20
150 #define Q931_CALLING_PARTY_CATEGORY	(0x32 | Q931_CODESET(5))
151 #define Q931_INFORMATION_RATE		0x40
152 #define Q931_TRANSIT_DELAY			0x42
153 #define Q931_TRANS_DELAY_SELECT		0x43
154 #define Q931_BINARY_PARAMETERS		0x44
155 #define Q931_WINDOW_SIZE			0x45
156 #define Q931_PACKET_SIZE			0x46
157 #define Q931_CLOSED_USER_GROUP		0x47
158 #define Q931_REVERSE_CHARGE_INDIC	0x4a
159 #define Q931_CALLING_PARTY_NUMBER	0x6c
160 #define Q931_CALLING_PARTY_SUBADDR	0x6d
161 #define Q931_CALLED_PARTY_NUMBER	0x70
162 #define Q931_CALLED_PARTY_SUBADDR	0x71
163 #define Q931_REDIRECTING_NUMBER		0x74
164 #define Q931_REDIRECTING_SUBADDR	0x75
165 #define Q931_TRANSIT_NET_SELECT		0x78
166 #define Q931_RESTART_INDICATOR		0x79
167 #define Q931_LOW_LAYER_COMPAT		0x7c
168 #define Q931_HIGH_LAYER_COMPAT		0x7d
169 
170 #define Q931_CODESET(x)			((x) << 8)
171 #define Q931_IE_CODESET(x)		((x) >> 8)
172 #define Q931_IE_IE(x)			((x) & 0xff)
173 #define Q931_FULL_IE(codeset, ie)	(((codeset) << 8) | ((ie) & 0xff))
174 
175 #define Q931_DISPLAY					0x28
176 #define Q931_IE_SEGMENTED_MSG			0x00
177 #define Q931_IE_CHANGE_STATUS			0x01
178 #define Q931_IE_ORIGINATING_LINE_INFO		(0x01 | Q931_CODESET(6))
179 #define Q931_IE_CONNECTED_ADDR			0x0c
180 #define Q931_IE_CONNECTED_NUM			0x4c
181 #define Q931_IE_CONNECTED_SUBADDR		0x4d
182 #define Q931_IE_CALL_IDENTITY			0x10
183 #define Q931_IE_FACILITY				0x1c
184 #define Q931_IE_ENDPOINT_ID				0x26
185 #define Q931_IE_NOTIFY_IND				0x27
186 #define Q931_IE_TIME_DATE				0x29
187 #define Q931_IE_KEYPAD_FACILITY			0x2c
188 #define Q931_IE_CALL_STATUS				0x2d
189 #define Q931_IE_UPDATE                  0x31
190 #define Q931_IE_INFO_REQUEST            0x32
191 #define Q931_IE_SIGNAL					0x34
192 #define Q931_IE_SWITCHHOOK				0x36
193 #define Q931_IE_GENERIC_DIGITS			(0x37 | Q931_CODESET(6))
194 #define Q931_IE_FEATURE_ACTIVATE		0x38
195 #define Q931_IE_FEATURE_IND				0x39
196 #define Q931_IE_ORIGINAL_CALLED_NUMBER 	0x73
197 #define Q931_IE_REDIRECTION_NUMBER		0x76
198 #define Q931_IE_REDIRECTION_SUBADDR		0x77
199 #define Q931_IE_USER_USER_FACILITY		0x7A
200 #define Q931_IE_USER_USER				0x7E
201 #define Q931_IE_ESCAPE_FOR_EXT			0x7F
202 
203 
204 /*! Q.931 call states */
205 enum Q931_CALL_STATE {
206 	/*!
207 	 * \details
208 	 *   null state (U0):
209 	 *     No call exists.
210 	 * \details
211 	 *   null state (N0):
212 	 *     No call exists.
213 	 */
214 	Q931_CALL_STATE_NULL = 0,
215 	/*!
216 	 * \details
217 	 *   call initiated (U1):
218 	 *     This state exists for an outgoing call, when the user requests
219 	 *     call establishment from the network.
220 	 * \details
221 	 *   call initiated (N1):
222 	 *     This state exists for an outgoing call when the network has received
223 	 *     a call establishment request but has not yet responded.
224 	 */
225 	Q931_CALL_STATE_CALL_INITIATED = 1,
226 	/*!
227 	 * \details
228 	 *   overlap sending (U2):
229 	 *     This state exists for an outgoing call when the user has
230 	 *     received acknowledgement of the call establishment request which
231 	 *     permits the user to send additional call information to the network
232 	 *     in overlap mode.
233 	 * \details
234 	 *   overlap sending (N2):
235 	 *     This state exists for an outgoing call when the network has acknowledged
236 	 *     the call establishment request and is prepared to receive additional
237 	 *     call information (if any) in overlap mode.
238 	 */
239 	Q931_CALL_STATE_OVERLAP_SENDING = 2,
240 	/*!
241 	 * \details
242 	 *   outgoing call proceeding (U3):
243 	 *     This state exists for an outgoing call when the user has
244 	 *     received acknowledgement that the network has received all
245 	 *     call information necessary to effect call establishment.
246 	 * \details
247 	 *   outgoing call proceeding (N3):
248 	 *     This state exists for an outgoing call when the network has sent
249 	 *     acknowledgement that the network has received all call information
250 	 *     necessary to effect call establishment.
251 	 */
252 	Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING = 3,
253 	/*!
254 	 * \details
255 	 *   call delivered (U4):
256 	 *     This state exists for an outgoing call when the calling user has
257 	 *     received an indication that remote user alerting has been initiated.
258 	 * \details
259 	 *   call delivered (N4):
260 	 *     This state exists for an outgoing call when the network has indicated
261 	 *     that remote user alerting has been initiated.
262 	 */
263 	Q931_CALL_STATE_CALL_DELIVERED = 4,
264 	/*!
265 	 * \details
266 	 *   call present (U6):
267 	 *     This state exists for an incoming call when the user has received a
268 	 *     call establishment request but has not yet responded.
269 	 * \details
270 	 *   call present (N6):
271 	 *     This state exists for an incoming call when the network has sent a
272 	 *     call establishment request but has not yet received a satisfactory
273 	 *     response.
274 	 */
275 	Q931_CALL_STATE_CALL_PRESENT = 6,
276 	/*!
277 	 * \details
278 	 *   call received (U7):
279 	 *     This state exists for an incoming call when the user has indicated
280 	 *     alerting but has not yet answered.
281 	 * \details
282 	 *   call received (N7):
283 	 *     This state exists for an incoming call when the network has received
284 	 *     an indication that the user is alerting but has not yet received an
285 	 *     answer.
286 	 */
287 	Q931_CALL_STATE_CALL_RECEIVED = 7,
288 	/*!
289 	 * \details
290 	 *   connect request (U8):
291 	 *     This state exists for an incoming call when the user has answered
292 	 *     the call and is waiting to be awarded the call.
293 	 * \details
294 	 *   connect request (N8):
295 	 *     This state exists for an incoming call when the network has received
296 	 *     an answer but the network has not yet awarded the call.
297 	 */
298 	Q931_CALL_STATE_CONNECT_REQUEST = 8,
299 	/*!
300 	 * \details
301 	 *   incoming call proceeding (U9):
302 	 *     This state exists for an incoming call when the user has sent
303 	 *     acknowledgement that the user has received all call information
304 	 *     necessary to effect call establishment.
305 	 * \details
306 	 *   incoming call proceeding (N9):
307 	 *     This state exists for an incoming call when the network has received
308 	 *     acknowledgement that the user has received all call information
309 	 *     necessary to effect call establishment.
310 	 */
311 	Q931_CALL_STATE_INCOMING_CALL_PROCEEDING = 9,
312 	/*!
313 	 * \details
314 	 *   active (U10):
315 	 *     This state exists for an incoming call when the user has received
316 	 *     an acknowledgement from the network that the user has been awarded
317 	 *     the call. This state exists for an outgoing call when the user has
318 	 *     received an indication that the remote user has answered the call.
319 	 * \details
320 	 *   active (N10):
321 	 *     This state exists for an incoming call when the network has awarded
322 	 *     the call to the called user. This state exists for an outgoing call
323 	 *     when the network has indicated that the remote user has answered
324 	 *     the call.
325 	 */
326 	Q931_CALL_STATE_ACTIVE = 10,
327 	/*!
328 	 * \details
329 	 *   disconnect request (U11):
330 	 *     This state exists when the user has requested the network to clear
331 	 *     the end-to-end connection (if any) and is waiting for a response.
332 	 * \details
333 	 *   disconnect request (N11):
334 	 *     This state exists when the network has received a request from the
335 	 *     user to clear the end-to-end connection (if any).
336 	 */
337 	Q931_CALL_STATE_DISCONNECT_REQUEST = 11,
338 	/*!
339 	 * \details
340 	 *   disconnect indication (U12):
341 	 *     This state exists when the user has received an invitation to
342 	 *     disconnect because the network has disconnected the end-to-end
343 	 *     connection (if any).
344 	 * \details
345 	 *   disconnect indication (N12):
346 	 *     This state exists when the network has disconnected the end-to-end
347 	 *     connection (if any) and has sent an invitation to disconnect the
348 	 *     user-network connection.
349 	 */
350 	Q931_CALL_STATE_DISCONNECT_INDICATION = 12,
351 	/*!
352 	 * \details
353 	 *   suspend request (U15):
354 	 *     This state exists when the user has requested the network to suspend
355 	 *     the call and is waiting for a response.
356 	 * \details
357 	 *   suspend request (N15):
358 	 *     This state exists when the network has received a request to suspend
359 	 *     the call but has not yet responded.
360 	 */
361 	Q931_CALL_STATE_SUSPEND_REQUEST = 15,
362 	/*!
363 	 * \details
364 	 *   resume request (U17):
365 	 *     This state exists when the user has requested the network to resume
366 	 *     a previously suspended call and is waiting for a response.
367 	 * \details
368 	 *   resume request (N17):
369 	 *     This state exists when the network has received a request to resume
370 	 *     a previously suspended call but has not yet responded.
371 	 */
372 	Q931_CALL_STATE_RESUME_REQUEST = 17,
373 	/*!
374 	 * \details
375 	 *   release request (U19):
376 	 *     This state exists when the user has requested the network to release
377 	 *     and is waiting for a response.
378 	 * \details
379 	 *   release request (N19):
380 	 *     This state exists when the network has requested the user to release
381 	 *     and is waiting for a response.
382 	 */
383 	Q931_CALL_STATE_RELEASE_REQUEST = 19,
384 	/*!
385 	 * \details
386 	 *   call abort (N22):
387 	 *     This state exists for an incoming call for the point-to-multipoint
388 	 *     configuration when the call is being cleared before any user has been
389 	 *     awarded the call.
390 	 */
391 	Q931_CALL_STATE_CALL_ABORT = 22,
392 	/*!
393 	 * \details
394 	 *   overlap receiving (U25):
395 	 *     This state exists for an incoming call when the user has acknowledged
396 	 *     the call establishment request from the network and is prepared to
397 	 *     receive additional call information (if any) in overlap mode.
398 	 * \details
399 	 *   overlap receiving (N25):
400 	 *     This state exists for an incoming call when the network has received
401 	 *     acknowledgement of the call establishment request which permits the
402 	 *     network to send additional call information (if any) in the overlap
403 	 *     mode.
404 	 */
405 	Q931_CALL_STATE_OVERLAP_RECEIVING = 25,
406 	/*!
407 	 * \details
408 	 *   call independent service (U31): (From Q.932)
409 	 *     This state exists when a call independent supplementary service
410 	 *     signalling connection is established.
411 	 * \details
412 	 *   call independent service (N31): (From Q.932)
413 	 *     This state exists when a call independent supplementary service
414 	 *     signalling connection is established.
415 	 */
416 	Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE = 31,
417 	Q931_CALL_STATE_RESTART_REQUEST = 61,
418 	Q931_CALL_STATE_RESTART = 62,
419 	/*!
420 	 * \details
421 	 * Call state has not been set.
422 	 * Call state does not exist.
423 	 * Call state not initialized.
424 	 * Call state internal use only.
425 	 */
426 	Q931_CALL_STATE_NOT_SET = 0xFF,
427 };
428 
429 /*! Q.931 call establishment state ranking for competing calls in PTMP NT mode. */
430 enum Q931_RANKED_CALL_STATE {
431 	/*! Call is present but has no response yet. */
432 	Q931_RANKED_CALL_STATE_PRESENT,
433 	/*! Call is collecting digits. */
434 	Q931_RANKED_CALL_STATE_OVERLAP,
435 	/*! Call routing is happening. */
436 	Q931_RANKED_CALL_STATE_PROCEEDING,
437 	/*! Called party is being alerted of the call. */
438 	Q931_RANKED_CALL_STATE_ALERTING,
439 	/*! Call is connected.  A winner has been declared. */
440 	Q931_RANKED_CALL_STATE_CONNECT,
441 	/*! Call is in some non-call establishment state (likely disconnecting). */
442 	Q931_RANKED_CALL_STATE_OTHER,
443 	/*! Master call is aborting. */
444 	Q931_RANKED_CALL_STATE_ABORT,
445 };
446 
447 /* EuroISDN  */
448 #define Q931_SENDING_COMPLETE		0xa1
449 
450 extern int maintenance_service(struct pri *pri, int span, int channel, int changestatus);
451 
452 
453 /* Q.SIG specific */
454 #define QSIG_IE_TRANSIT_COUNT		0x31
455 
456 int q931_receive(struct q921_link *link, q931_h *h, int len);
457 
458 extern int q931_alerting(struct pri *pri, q931_call *call, int channel, int info);
459 
460 extern int q931_call_progress_with_cause(struct pri *pri, q931_call *call, int channel, int info, int cause);
461 
462 extern int q931_call_progress(struct pri *pri, q931_call *call, int channel, int info);
463 
464 extern int q931_notify(struct pri *pri, q931_call *call, int channel, int info);
465 
466 extern int q931_call_proceeding(struct pri *pri, q931_call *call, int channel, int info);
467 
468 extern int q931_setup_ack(struct pri *ctrl, q931_call *c, int channel, int nonisdn, int inband);
469 
470 extern int q931_information(struct pri *pri, q931_call *call, char digit);
471 
472 extern int q931_keypad_facility(struct pri *pri, q931_call *call, const char *digits);
473 
474 extern int q931_connect(struct pri *pri, q931_call *call, int channel, int nonisdn);
475 int q931_connect_acknowledge(struct pri *ctrl, q931_call *call, int channel);
476 
477 extern int q931_release(struct pri *pri, q931_call *call, int cause);
478 
479 extern int q931_disconnect(struct pri *pri, q931_call *call, int cause);
480 
481 extern int q931_hangup(struct pri *pri, q931_call *call, int cause);
482 
483 extern int q931_restart(struct pri *pri, int channel);
484 
485 extern int q931_facility(struct pri *pri, q931_call *call);
486 
487 extern int q931_call_getcrv(struct pri *pri, q931_call *call, int *callmode);
488 
489 extern int q931_call_setcrv(struct pri *pri, q931_call *call, int crv, int callmode);
490 
491 struct q931_call *q931_new_call(struct pri *ctrl);
492 
493 extern int q931_setup(struct pri *pri, q931_call *c, struct pri_sr *req);
494 
495 int q931_register(struct pri *ctrl, q931_call *call);
496 
497 void q931_dump(struct pri *ctrl, int tei, q931_h *h, int len, int txrx);
498 
499 void q931_destroycall(struct pri *pri, q931_call *c);
500 
501 enum Q931_DL_EVENT {
502 	Q931_DL_EVENT_NONE,
503 	Q931_DL_EVENT_DL_ESTABLISH_IND,
504 	Q931_DL_EVENT_DL_ESTABLISH_CONFIRM,
505 	Q931_DL_EVENT_DL_RELEASE_IND,
506 	Q931_DL_EVENT_DL_RELEASE_CONFIRM,
507 	Q931_DL_EVENT_TEI_REMOVAL,
508 };
509 void q931_dl_event(struct q921_link *link, enum Q931_DL_EVENT event);
510 
511 int q931_send_hold(struct pri *ctrl, struct q931_call *call);
512 int q931_send_hold_ack(struct pri *ctrl, struct q931_call *call);
513 int q931_send_hold_rej(struct pri *ctrl, struct q931_call *call, int cause);
514 
515 int q931_send_retrieve(struct pri *ctrl, struct q931_call *call, int channel);
516 int q931_send_retrieve_ack(struct pri *ctrl, struct q931_call *call, int channel);
517 int q931_send_retrieve_rej(struct pri *ctrl, struct q931_call *call, int cause);
518 
519 #endif
520