1 /*
2  * Copyright (C) 2020 The Android Open Source Project
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 com.android.ims.rcs.uce.util;
18 
19 import android.telephony.ims.RcsUceAdapter;
20 
21 import com.android.ims.rcs.uce.UceController;
22 import com.android.ims.rcs.uce.UceController.RequestType;
23 
24 /**
25  * Define the network sip code and the reason.
26  */
27 public class NetworkSipCode {
28     public static final int SIP_CODE_OK = 200;
29     public static final int SIP_CODE_ACCEPTED = 202;
30     public static final int SIP_CODE_BAD_REQUEST = 400;
31     public static final int SIP_CODE_FORBIDDEN = 403;
32     public static final int SIP_CODE_NOT_FOUND = 404;
33     public static final int SIP_CODE_METHOD_NOT_ALLOWED = 405;
34     public static final int SIP_CODE_REQUEST_TIMEOUT = 408;
35     public static final int SIP_CODE_REQUEST_ENTITY_TOO_LARGE = 413;
36     public static final int SIP_CODE_INTERVAL_TOO_BRIEF = 423;
37     public static final int SIP_CODE_TEMPORARILY_UNAVAILABLE = 480;
38     public static final int SIP_CODE_BAD_EVENT = 489;
39     public static final int SIP_CODE_BUSY = 486;
40     public static final int SIP_CODE_SERVER_INTERNAL_ERROR = 500;
41     public static final int SIP_CODE_SERVICE_UNAVAILABLE = 503;
42     public static final int SIP_CODE_SERVER_TIMEOUT = 504;
43     public static final int SIP_CODE_BUSY_EVERYWHERE = 600;
44     public static final int SIP_CODE_DECLINE = 603;
45     public static final int SIP_CODE_DOES_NOT_EXIST_ANYWHERE = 604;
46 
47     public static final String SIP_OK = "OK";
48     public static final String SIP_ACCEPTED = "Accepted";
49     public static final String SIP_BAD_REQUEST = "Bad Request";
50     public static final String SIP_SERVICE_UNAVAILABLE = "Service Unavailable";
51     public static final String SIP_INTERNAL_SERVER_ERROR = "Internal Server Error";
52     public static final String SIP_NOT_REGISTERED = "User not registered";
53     public static final String SIP_NOT_AUTHORIZED_FOR_PRESENCE = "not authorized for presence";
54 
55     /**
56      * Convert the given SIP CODE to the Contact uce capabilities error.
57      * @param sipCode The SIP code of the request response.
58      * @param reason The reason of the request response.
59      * @param requestType The type of this request.
60      * @return The RCS contact UCE capabilities error which is defined in RcsUceAdapter.
61      */
getCapabilityErrorFromSipCode(int sipCode, String reason, @RequestType int requestType)62     public static int getCapabilityErrorFromSipCode(int sipCode, String reason,
63             @RequestType int requestType) {
64         int uceError;
65         switch (sipCode) {
66             case NetworkSipCode.SIP_CODE_FORBIDDEN:   // 403
67             case NetworkSipCode.SIP_CODE_SERVER_TIMEOUT:   // 504
68                 if(requestType == UceController.REQUEST_TYPE_PUBLISH) {
69                     // Not provisioned for PUBLISH request.
70                     uceError = RcsUceAdapter.ERROR_NOT_AUTHORIZED;
71                 } else {
72                     // Check the reason for CAPABILITY request
73                     if (NetworkSipCode.SIP_NOT_REGISTERED.equalsIgnoreCase(reason)) {
74                         // Not registered with IMS. Device shall register to IMS.
75                         uceError = RcsUceAdapter.ERROR_NOT_REGISTERED;
76                     } else if (NetworkSipCode.SIP_NOT_AUTHORIZED_FOR_PRESENCE.equalsIgnoreCase(
77                             reason)) {
78                         // Not provisioned for EAB. Device shall not retry.
79                         uceError = RcsUceAdapter.ERROR_NOT_AUTHORIZED;
80                     } else {
81                         // The network has responded SIP 403 error with no reason.
82                         uceError = RcsUceAdapter.ERROR_FORBIDDEN;
83                     }
84                 }
85                 break;
86             case NetworkSipCode.SIP_CODE_NOT_FOUND:              // 404
87                 if(requestType == UceController.REQUEST_TYPE_PUBLISH) {
88                     // Not provisioned for PUBLISH request.
89                     uceError = RcsUceAdapter.ERROR_NOT_AUTHORIZED;
90                 } else {
91                     uceError = RcsUceAdapter.ERROR_NOT_FOUND;
92                 }
93                 break;
94             case NetworkSipCode.SIP_CODE_REQUEST_TIMEOUT:        // 408
95                 uceError = RcsUceAdapter.ERROR_REQUEST_TIMEOUT;
96                 break;
97             case NetworkSipCode.SIP_CODE_INTERVAL_TOO_BRIEF:     // 423
98                 // Rejected by the network because the requested expiry interval is too short.
99                 uceError = RcsUceAdapter.ERROR_GENERIC_FAILURE;
100                 break;
101             case NetworkSipCode.SIP_CODE_BAD_EVENT:
102                 uceError = RcsUceAdapter.ERROR_FORBIDDEN;        // 489
103                 break;
104             case NetworkSipCode.SIP_CODE_SERVER_INTERNAL_ERROR:  // 500
105             case NetworkSipCode.SIP_CODE_SERVICE_UNAVAILABLE:    // 503
106                 // The network is temporarily unavailable or busy.
107                 uceError = RcsUceAdapter.ERROR_SERVER_UNAVAILABLE;
108                 break;
109             default:
110                 uceError = RcsUceAdapter.ERROR_GENERIC_FAILURE;
111                 break;
112         }
113         return uceError;
114     }
115 }
116