1 /*
2  * Copyright (C) 2021 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.services.telephony.rcs;
18 
19 import android.telephony.ims.SipMessage;
20 
21 public class SipMessageUtils {
22 
23     public static final String INVITE_SIP_METHOD = "INVITE";
24     public static final String CANCEL_SIP_METHOD = "CANCEL";
25     public static final String BYE_SIP_METHOD = "BYE";
26     public static final String ACK_SIP_METHOD = "ACK";
27     public static final String BASE_ADDRESS = "client.example.com";
28 
29     private static final String PARAM_SEPARATOR = ";";
30     private static final String PARAM_KEY_VALUE_SEPARATOR = "=";
31     private static final String BASE_VIA_HEADER_VALUE = "SIP/2.0/TCP " + BASE_ADDRESS + ":5060";
32 
33     /**
34      * @return A new SIP request from the given parameters.
35      */
generateSipRequest(String requestMethod, String fromContact, String toContact, String toUri, String branchId, String callId, String fromTag, String toTag)36     public static SipMessage generateSipRequest(String requestMethod, String fromContact,
37             String toContact, String toUri, String branchId, String callId, String fromTag,
38             String toTag) {
39         String header = "Via: " + addParamToHeader(BASE_VIA_HEADER_VALUE, "branch", branchId);
40         header += "\n";
41         header += "From: " + addParamToHeader(fromContact, "tag", fromTag);
42         header += "\n";
43         // To tag is optional
44         header += "To: " + ((toTag != null)
45                 ? addParamToHeader(toContact, "tag", toTag) : toContact);
46         header += "\n";
47         header += "Call-ID: " + callId;
48         return new SipMessage(requestMethod + " " + toUri + " SIP/2.0", header, new byte[0]);
49     }
50 
51     /**
52      * @return Generates a SIP response.
53      */
generateSipResponse(String statusCode, String statusString, String fromContact, String toContact, String branchId, String callId, String fromTag, String toTag)54     public static SipMessage generateSipResponse(String statusCode, String statusString,
55             String fromContact, String toContact, String branchId, String callId, String fromTag,
56             String toTag) {
57         String header = "Via: " + addParamToHeader(BASE_VIA_HEADER_VALUE, "branch", branchId);
58         header += "\n";
59         header += "From: " + addParamToHeader(fromContact, "tag", fromTag);
60         header += "\n";
61         // To tag is optional
62         header += "To: " + ((toTag != null)
63                 ? addParamToHeader(toContact, "tag", toTag) : toContact);
64         header += "\n";
65         header += "Call-ID: " + callId;
66         return new SipMessage("SIP/2.0 " + statusCode + " " + statusString, header,
67                 new byte[0]);
68     }
69 
addParamToHeader(String headerValue, String paramKey, String paramValue)70     private static String addParamToHeader(String headerValue, String paramKey, String paramValue) {
71         headerValue += PARAM_SEPARATOR + paramKey.trim() + PARAM_KEY_VALUE_SEPARATOR
72                 + paramValue.trim();
73         return headerValue;
74     }
75 }
76