1 /*
2  * Copyright (C) 2008 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.internal.telephony.cdma;
18 
19 import android.hardware.radio.V1_0.CdmaSmsMessage;
20 
21 import com.android.internal.telephony.SmsMessageBase;
22 import com.android.internal.telephony.cdma.sms.CdmaSmsAddress;
23 import com.android.internal.telephony.cdma.sms.CdmaSmsSubaddress;
24 import com.android.internal.telephony.cdma.sms.SmsEnvelope;
25 
26 /**
27  * A Factory class to convert from RIL to Framework SMS
28  *
29  */
30 public class SmsMessageConverter {
31     static final String LOG_TAG = "SmsMessageConverter";
32     static private final String LOGGABLE_TAG = "CDMA:SMS";
33     private static final boolean VDBG = false;
34 
35     /**
36      *  Create a "raw" CDMA SmsMessage from a Parcel that was forged in ril.cpp.
37      *  Note: Only primitive fields are set.
38      */
newCdmaSmsMessageFromRil( CdmaSmsMessage cdmaSmsMessage)39     public static SmsMessage newCdmaSmsMessageFromRil(
40             CdmaSmsMessage cdmaSmsMessage) {
41         // Note: Parcel.readByte actually reads one Int and masks to byte
42         SmsEnvelope env = new SmsEnvelope();
43         CdmaSmsAddress addr = new CdmaSmsAddress();
44         CdmaSmsSubaddress subaddr = new CdmaSmsSubaddress();
45         byte[] data;
46         byte count;
47         int countInt;
48         int addressDigitMode;
49 
50         //currently not supported by the modem-lib: env.mMessageType
51         env.teleService = cdmaSmsMessage.teleserviceId;
52 
53         if (cdmaSmsMessage.isServicePresent) {
54             env.messageType = SmsEnvelope.MESSAGE_TYPE_BROADCAST;
55         }
56         else {
57             if (SmsEnvelope.TELESERVICE_NOT_SET == env.teleService) {
58                 // assume type ACK
59                 env.messageType = SmsEnvelope.MESSAGE_TYPE_ACKNOWLEDGE;
60             } else {
61                 env.messageType = SmsEnvelope.MESSAGE_TYPE_POINT_TO_POINT;
62             }
63         }
64         env.serviceCategory = cdmaSmsMessage.serviceCategory;
65 
66         // address
67         addressDigitMode = cdmaSmsMessage.address.digitMode;
68         addr.digitMode = (byte) (0xFF & addressDigitMode);
69         addr.numberMode = (byte) (0xFF & cdmaSmsMessage.address.numberMode);
70         addr.ton = cdmaSmsMessage.address.numberType;
71         addr.numberPlan = (byte) (0xFF & cdmaSmsMessage.address.numberPlan);
72         count = (byte) cdmaSmsMessage.address.digits.size();
73         addr.numberOfDigits = count;
74         data = new byte[count];
75         for (int index=0; index < count; index++) {
76             data[index] = cdmaSmsMessage.address.digits.get(index);
77 
78             // convert the value if it is 4-bit DTMF to 8 bit
79             if (addressDigitMode == CdmaSmsAddress.DIGIT_MODE_4BIT_DTMF) {
80                 data[index] = SmsMessage.convertDtmfToAscii(data[index]);
81             }
82         }
83 
84         addr.origBytes = data;
85 
86         subaddr.type = cdmaSmsMessage.subAddress.subaddressType;
87         subaddr.odd = (byte) (cdmaSmsMessage.subAddress.odd ? 1 : 0);
88         count = (byte) cdmaSmsMessage.subAddress.digits.size();
89 
90         if (count < 0) {
91             count = 0;
92         }
93 
94         // p_cur->sSubAddress.digits[digitCount] :
95 
96         data = new byte[count];
97 
98         for (int index = 0; index < count; ++index) {
99             data[index] = cdmaSmsMessage.subAddress.digits.get(index);
100         }
101 
102         subaddr.origBytes = data;
103 
104         /* currently not supported by the modem-lib:
105             env.bearerReply
106             env.replySeqNo
107             env.errorClass
108             env.causeCode
109         */
110 
111         // bearer data
112         countInt = cdmaSmsMessage.bearerData.size();
113         if (countInt < 0) {
114             countInt = 0;
115         }
116 
117         data = new byte[countInt];
118         for (int index=0; index < countInt; index++) {
119             data[index] = cdmaSmsMessage.bearerData.get(index);
120         }
121         // BD gets further decoded when accessed in SMSDispatcher
122         env.bearerData = data;
123 
124         // link the the filled objects to the SMS
125         env.origAddress = addr;
126         env.origSubaddress = subaddr;
127 
128         SmsMessage msg = new SmsMessage(addr, env);
129 
130         return msg;
131     }
132 
newSmsMessageFromCdmaSmsMessage( CdmaSmsMessage msg)133     public static android.telephony.SmsMessage newSmsMessageFromCdmaSmsMessage(
134             CdmaSmsMessage msg) {
135         return new android.telephony.SmsMessage((SmsMessageBase)newCdmaSmsMessageFromRil(msg));
136     }
137 }
138