View Javadoc

1   /*
2    *  Copyright 2004-2006 Stefan Reuter
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package org.asteriskjava.live;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  /***
23   * Asterisk hangup cause.<p>
24   * Definitions from <code>/include/asterisk/causes.h</code>.
25   * 
26   * @author srt
27   * @version $Id: HangupCause.java 729 2007-05-26 05:16:57Z sprior $
28   */
29  public enum HangupCause
30  {
31      AST_CAUSE_UNALLOCATED(1),
32      AST_CAUSE_NO_ROUTE_TRANSIT_NET(2),
33      AST_CAUSE_NO_ROUTE_DESTINATION(3),
34      AST_CAUSE_CHANNEL_UNACCEPTABLE(6),
35      AST_CAUSE_CALL_AWARDED_DELIVERED(7),
36      AST_CAUSE_NORMAL_CLEARING(16),
37      AST_CAUSE_USER_BUSY(17),
38      AST_CAUSE_NO_USER_RESPONSE(18),
39      AST_CAUSE_NO_ANSWER(19),
40      AST_CAUSE_CALL_REJECTED(21),
41      AST_CAUSE_NUMBER_CHANGED(22),
42      AST_CAUSE_DESTINATION_OUT_OF_ORDER(27),
43      AST_CAUSE_INVALID_NUMBER_FORMAT(28),
44      AST_CAUSE_FACILITY_REJECTED(29),
45      AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY(30),
46      AST_CAUSE_NORMAL_UNSPECIFIED(31),
47      AST_CAUSE_NORMAL_CIRCUIT_CONGESTION(34),
48      AST_CAUSE_NETWORK_OUT_OF_ORDER(38),
49      AST_CAUSE_NORMAL_TEMPORARY_FAILURE(41),
50      AST_CAUSE_SWITCH_CONGESTION(42),
51      AST_CAUSE_ACCESS_INFO_DISCARDED(43),
52      AST_CAUSE_REQUESTED_CHAN_UNAVAIL(44),
53      AST_CAUSE_PRE_EMPTED(45),
54      AST_CAUSE_FACILITY_NOT_SUBSCRIBED(50),
55      AST_CAUSE_OUTGOING_CALL_BARRED(52),
56      AST_CAUSE_INCOMING_CALL_BARRED(54),
57      AST_CAUSE_BEARERCAPABILITY_NOTAUTH(57),
58      AST_CAUSE_BEARERCAPABILITY_NOTAVAIL(58),
59      AST_CAUSE_BEARERCAPABILITY_NOTIMPL(65),
60      AST_CAUSE_CHAN_NOT_IMPLEMENTED(66),
61      AST_CAUSE_FACILITY_NOT_IMPLEMENTED(69),
62      AST_CAUSE_INVALID_CALL_REFERENCE(81),
63      AST_CAUSE_INCOMPATIBLE_DESTINATION(88),
64      AST_CAUSE_INVALID_MSG_UNSPECIFIED(95),
65      AST_CAUSE_MANDATORY_IE_MISSING(96),
66      AST_CAUSE_MESSAGE_TYPE_NONEXIST(97),
67      AST_CAUSE_WRONG_MESSAGE(98),
68      AST_CAUSE_IE_NONEXIST(99),
69      AST_CAUSE_INVALID_IE_CONTENTS(100),
70      AST_CAUSE_WRONG_CALL_STATE(101),
71      AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE(102),
72      AST_CAUSE_MANDATORY_IE_LENGTH_ERROR(103),
73      AST_CAUSE_PROTOCOL_ERROR(111),
74      AST_CAUSE_INTERWORKING(127),
75  
76      /* Special Asterisk aliases */
77      AST_CAUSE_BUSY(AST_CAUSE_USER_BUSY),
78      AST_CAUSE_FAILURE(AST_CAUSE_NETWORK_OUT_OF_ORDER),
79      AST_CAUSE_NORMAL(AST_CAUSE_NORMAL_CLEARING),
80      AST_CAUSE_NOANSWER(AST_CAUSE_NO_ANSWER),
81      AST_CAUSE_CONGESTION(AST_CAUSE_NORMAL_CIRCUIT_CONGESTION),
82      AST_CAUSE_UNREGISTERED(AST_CAUSE_NO_ROUTE_DESTINATION),
83      AST_CAUSE_NOTDEFINED(0),
84      AST_CAUSE_NOSUCHDRIVER(AST_CAUSE_CHAN_NOT_IMPLEMENTED);
85  
86      private HangupCause(int code)
87      {
88          this.code = code;
89      }
90  
91      private HangupCause(HangupCause cause)
92      {
93          this.code = cause.code;
94      }
95  
96      /***
97       * Returns the numeric cause code.<p>
98       * Using this method in client code is discouraged.
99       * 
100      * @return the numeric cause code.
101      */
102     public int getCode()
103     {
104         return code;
105     }
106 
107     /***
108      * Returns the HangupCode by its numeric cause code.<p>
109      * Using this method in client code is discouraged.
110      * 
111      * @param code the numeric cause code.
112      * @return the corresponding HangupCode enum or 
113      *         <code>null</code> if there is no such HangupCause.
114      */
115     public static synchronized HangupCause getByCode(int code)
116     {
117         if (causes == null)
118         {
119             causes = new HashMap<Integer, HangupCause>();
120             for (HangupCause cause : values())
121             {
122                 causes.put(cause.code, cause);
123             }
124         }
125         
126         return causes.get(code);
127     }
128 
129     @Override
130    public String toString()
131     {
132         if (name().startsWith("AST_CAUSE_"))
133         {
134             return name().substring(10);
135         }
136         return name();
137     }
138 
139     private int code;
140     private static Map<Integer, HangupCause> causes;
141 }