1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.marre.sms;
36
37 /***
38 * Represents a SMS DCS (Data Coding Scheme).
39 *
40 * @version $Id: SmsDcs.java,v 1.3 2005/05/06 13:55:11 c95men Exp $
41 * @author Markus Eriksson
42 */
43 public class SmsDcs
44 {
45 /***
46 * Alphabet as defined in GSM 03.38. It contains all characters needed for most
47 * Western European languages. It also contains upper case Greek characters.
48 */
49 public static final int ALPHABET_GSM = 0;
50 /*** ISO 8859-1 (ISO Latin-1). */
51 public static final int ALPHABET_8BIT = 1;
52 /*** Unicode UCS-2. */
53 public static final int ALPHABET_UCS2 = 2;
54 /*** Reserved. */
55 public static final int ALPHABET_RESERVED = 3;
56 /*** Unknown. */
57 public static final int ALPHABET_UNKNOWN = 4;
58
59 /*** Class 0 SMS. Sometimes called FLASH message. */
60 public static final byte MSG_CLASS_0 = 0;
61 /*** Class 1 SMS. Default meaning: ME-specific. */
62 public static final byte MSG_CLASS_1 = 1;
63 /*** Class 2 SMS, SIM specific message. */
64 public static final byte MSG_CLASS_2 = 2;
65 /*** Class 3 SMS. Default meaning: TE specific (See GSM TS 07.05). */
66 public static final byte MSG_CLASS_3 = 3;
67 /*** Message with no specific message class (Often handled as an class 1 SMS). */
68 public static final byte MSG_CLASS_UNKNOWN = 4;
69
70 /*** DCS general data coding indication group. 00xxxxxx. */
71 public static final int GROUP_GENERAL_DATA_CODING = 0;
72 /*** DCS message waiting indication group: discard message. 1100xxxx. */
73 public static final int GROUP_MESSAGE_WAITING_DISCARD = 1;
74 /*** DCS message waiting indication group: store message (gsm). 1101xxxx. */
75 public static final int GROUP_MESSAGE_WAITING_STORE_GSM = 2;
76 /*** DCS message waiting indication group: store message (ucs2). 1110xxxx. */
77 public static final int GROUP_MESSAGE_WAITING_STORE_UCS2 = 3;
78 /*** DCS data coding/message class: 1111xxxx. */
79 public static final int GROUP_DATA_CODING_MESSAGE = 4;
80 /*** DCS group unknown. */
81 public static final int GROUP_UNKNOWN = 5;
82
83 /*** Message waiting indication type - voicemail. */
84 public static final byte DCS_MSG_WAITING_VOICEMAIL = 0;
85 /*** Message waiting indication type - fax. */
86 public static final byte DCS_MSG_WAITING_FAX = 1;
87 /*** Message waiting indication type - email. */
88 public static final byte DCS_MSG_WAITING_EMAIL = 2;
89 /*** Message waiting indication type - other. Should not be used. */
90 public static final byte DCS_MSG_WAITING_OTHER = 3;
91
92 /*** The encoded dcs. */
93 protected byte dcs_;
94
95 /***
96 * Creates a specific DCS.
97 *
98 * @param dcs The dcs.
99 */
100 public SmsDcs(byte dcs)
101 {
102 dcs_ = dcs;
103 }
104
105 /***
106 * Returns the encoded dcs.
107 *
108 * @return The dcs.
109 */
110 public byte getValue()
111 {
112 return dcs_;
113 }
114
115 /***
116 * Builds a general-data-coding dcs.
117 *
118 * @param alphabet The alphabet. Possible values are ALPHABET_GSM, ALPHABET_8BIT, ALPHABET_UCS2 and ALPHABET_RESERVED.
119 * @param messageClass The message class. Possible values are MSG_CLASS_0, MSG_CLASS_1, MSG_CLASS_2 and MSG_CLASS_3.
120 *
121 * @return A valid general data coding DCS.
122 */
123 public static SmsDcs getGeneralDataCodingDcs(int alphabet, byte messageClass)
124 {
125 byte dcs = 0x00;
126
127
128
129
130
131
132
133 switch (alphabet)
134 {
135 case ALPHABET_GSM: dcs |= 0x00; break;
136 case ALPHABET_8BIT: dcs |= 0x04; break;
137 case ALPHABET_UCS2: dcs |= 0x08; break;
138 case ALPHABET_RESERVED: dcs |= 0x0C; break;
139
140 case ALPHABET_UNKNOWN:
141 default:
142 throw new IllegalArgumentException("Invalid alphabet");
143 }
144
145 switch (messageClass)
146 {
147 case MSG_CLASS_0: dcs |= 0x10; break;
148 case MSG_CLASS_1: dcs |= 0x11; break;
149 case MSG_CLASS_2: dcs |= 0x12; break;
150 case MSG_CLASS_3: dcs |= 0x13; break;
151 case MSG_CLASS_UNKNOWN: dcs |= 0x00; break;
152
153 default:
154 throw new IllegalArgumentException("Invalid message class");
155 }
156
157 return new SmsDcs(dcs);
158 }
159
160 /***
161 * Decodes the given dcs and returns the alphabet.
162 *
163 * <pre>
164 * Return value can be one of:
165 * - ALPHABET_GSM
166 * - ALPHABET_8BIT
167 * - ALPHABET_UCS2
168 * - ALPHABET_RESERVED
169 * - ALPHABET_UNKNOWN
170 * </pre>
171 *
172 * @return Returns the alphabet.
173 */
174 public int getAlphabet()
175 {
176 switch (getGroup())
177 {
178 case GROUP_GENERAL_DATA_CODING:
179
180 if (dcs_ == 0x00)
181 {
182 return ALPHABET_GSM;
183 }
184
185 switch (dcs_ & 0x0C)
186 {
187 case 0x00: return ALPHABET_GSM;
188 case 0x04: return ALPHABET_8BIT;
189 case 0x08: return ALPHABET_UCS2;
190 case 0x0C: return ALPHABET_RESERVED;
191 default: return ALPHABET_UNKNOWN;
192 }
193
194 case GROUP_MESSAGE_WAITING_STORE_GSM:
195 return ALPHABET_GSM;
196
197 case GROUP_MESSAGE_WAITING_STORE_UCS2:
198 return ALPHABET_UCS2;
199
200 case GROUP_DATA_CODING_MESSAGE:
201 switch (dcs_ & 0x04)
202 {
203 case 0x00: return ALPHABET_GSM;
204 case 0x04: return ALPHABET_8BIT;
205 default: return ALPHABET_UNKNOWN;
206 }
207
208 default:
209 return ALPHABET_UNKNOWN;
210 }
211 }
212
213 /***
214 * What group (type of message) is the given dcs.
215 *
216 * @param theDcs the dcs to test
217 * @return Any of the GROUP_ constants.
218 */
219 public int getGroup()
220 {
221 if ((dcs_ & 0xC0) == 0x00)
222 {
223 return GROUP_GENERAL_DATA_CODING;
224 }
225
226 switch ((dcs_ & 0xF0))
227 {
228 case 0xC0: return GROUP_MESSAGE_WAITING_DISCARD;
229 case 0xD0: return GROUP_MESSAGE_WAITING_STORE_GSM;
230 case 0xE0: return GROUP_MESSAGE_WAITING_STORE_UCS2;
231 case 0xF0: return GROUP_DATA_CODING_MESSAGE;
232 default: return GROUP_UNKNOWN;
233 }
234 }
235
236 /***
237 * Get the message class.
238 *
239 * <pre>
240 * Return value can be one of:
241 * - MSG_CLASS_UNKNOWN
242 * - MSG_CLASS_0
243 * - MSG_CLASS_1
244 * - MSG_CLASS_2
245 * - MSG_CLASS_3
246 * </pre>
247 *
248 * @return Returns the message class.
249 */
250 public int getMessageClass()
251 {
252 switch (getGroup())
253 {
254 case GROUP_GENERAL_DATA_CODING:
255
256 if (dcs_ == 0x00)
257 {
258 return MSG_CLASS_UNKNOWN;
259 }
260
261 switch (dcs_ & 0x13)
262 {
263 case 0x10: return MSG_CLASS_0;
264 case 0x11: return MSG_CLASS_1;
265 case 0x12: return MSG_CLASS_2;
266 case 0x13: return MSG_CLASS_3;
267 default: return MSG_CLASS_UNKNOWN;
268 }
269
270 case GROUP_DATA_CODING_MESSAGE:
271
272 switch (dcs_ & 0x03)
273 {
274 case 0x00: return MSG_CLASS_0;
275 case 0x01: return MSG_CLASS_1;
276 case 0x02: return MSG_CLASS_2;
277 case 0x03: return MSG_CLASS_3;
278 default: return MSG_CLASS_UNKNOWN;
279 }
280
281 default:
282 return MSG_CLASS_UNKNOWN;
283 }
284 }
285 }