From Maynard, Chris :
[obnox/wireshark/wip.git] / epan / dissectors / packet-ansi_637.c
1 /* packet-ansi_637.c
2  * Routines for ANSI IS-637-A (SMS) dissection
3  *
4  * Copyright 2003, Michael Lum <mlum [AT] telostech.com>
5  * In association with Telos Technology Inc.
6  *
7  * Title                3GPP2                   Other
8  *
9  *   Short Message Service
10  *                      3GPP2 C.S0015-0         TIA/EIA-637-A
11  *
12  * $Id$
13  *
14  * Ethereal - Network traffic analyzer
15  * By Gerald Combs <gerald@ethereal.com>
16  * Copyright 1998 Gerald Combs
17  *
18  * This program is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU General Public License
20  * as published by the Free Software Foundation; either version 2
21  * of the License, or (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31  */
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <stdio.h>
38 /* #include <stdlib.h> */
39 #include <gmodule.h>
40
41 #ifdef HAVE_SYS_TYPES_H
42 # include <sys/types.h>
43 #endif
44
45 #ifdef HAVE_NETINET_IN_H
46 # include <netinet/in.h>
47 #endif
48
49 #include <string.h>
50
51 #include "epan/packet.h"
52
53
54 static char *ansi_proto_name_tele = "ANSI IS-637-A (SMS) Teleservice Layer";
55 static char *ansi_proto_name_trans = "ANSI IS-637-A (SMS) Transport Layer";
56 static char *ansi_proto_name_short = "IS-637-A";
57
58 static const value_string ansi_srvc_cat_strings[] = {
59     { 0x0000,   "Unknown or unspecified" },
60     { 0x0001,   "Emergency Broadcasts" },
61     { 0x0002,   "Administrative" },
62     { 0x0003,   "Maintenance" },
63     { 0x0004,   "General News - Local" },
64     { 0x0005,   "General News - Regional" },
65     { 0x0006,   "General News - National" },
66     { 0x0007,   "General News - International" },
67     { 0x0008,   "Business/Financial News - Local" },
68     { 0x0009,   "Business/Financial News - Regional" },
69     { 0x000A,   "Business/Financial News - National" },
70     { 0x000B,   "Business/Financial News - International" },
71     { 0x000C,   "Sports News - Local" },
72     { 0x000D,   "Sports News - Regional" },
73     { 0x000E,   "Sports News - National" },
74     { 0x000F,   "Sports News - International" },
75     { 0x0010,   "Entertainment News - Local" },
76     { 0x0011,   "Entertainment News - Regional" },
77     { 0x0012,   "Entertainment News - National" },
78     { 0x0013,   "Entertainment News - International" },
79     { 0x0014,   "Local Weather" },
80     { 0x0015,   "Area Traffic Reports" },
81     { 0x0016,   "Local Airport Flight Schedules" },
82     { 0x0017,   "Restaurants" },
83     { 0x0018,   "Lodgings" },
84     { 0x0019,   "Retail Directory" },
85     { 0x001A,   "Advertisements" },
86     { 0x001B,   "Stock Quotes" },
87     { 0x001C,   "Employment Opportunities" },
88     { 0x001D,   "Medical/Health/Hospitals" },
89     { 0x001E,   "Technology News" },
90     { 0x001F,   "Multi-category" },
91     { 0, NULL },
92 };
93
94 static const value_string ansi_tele_msg_type_strings[] = {
95     { 1,        "Deliver (mobile-terminated only)" },
96     { 2,        "Submit (mobile-originated only)" },
97     { 3,        "Cancellation (mobile-originated only)" },
98     { 4,        "Delivery Acknowledgement (mobile-terminated only)" },
99     { 5,        "User Acknowledgement (either direction)" },
100     { 0, NULL },
101 };
102
103 static const value_string ansi_tele_id_strings[] = {
104     { 1,        "Reserved for maintenance" },
105     { 4096,     "AMPS Extended Protocol Enhanced Services" },
106     { 4097,     "CDMA Cellular Paging Teleservice" },
107     { 4098,     "CDMA Cellular Messaging Teleservice" },
108     { 4099,     "CDMA Voice Mail Notification" },
109     { 32513,    "TDMA Cellular Messaging Teleservice" },
110     { 32520,    "TDMA System Assisted Mobile Positioning through Satellite (SAMPS)" },
111     { 32584,    "TDMA Segmented System Assisted Mobile Positioning Service" },
112     { 0, NULL },
113 };
114
115 static const value_string ansi_tele_param_strings[] = {
116     { 0x00,     "Message Identifier" },
117     { 0x01,     "User Data" },
118     { 0x02,     "User Response Code" },
119     { 0x03,     "Message Center Time Stamp" },
120     { 0x04,     "Validity Period - Absolute" },
121     { 0x05,     "Validity Period - Relative" },
122     { 0x06,     "Deferred Delivery Time - Absolute" },
123     { 0x07,     "Deferred Delivery Time - Relative" },
124     { 0x08,     "Priority Indicator" },
125     { 0x09,     "Privacy Indicator" },
126     { 0x0a,     "Reply Option" },
127     { 0x0b,     "Number of Messages" },
128     { 0x0c,     "Alert on Message Delivery" },
129     { 0x0d,     "Language Indicator" },
130     { 0x0e,     "Call-Back Number" },
131     { 0x0f,     "Message Display Mode" },
132     { 0x10,     "Multiple Encoding User Data" },
133     { 0, NULL },
134 };
135
136 static const value_string ansi_trans_msg_type_strings[] = {
137     { 0,        "Point-to-Point" },
138     { 1,        "Broadcast" },
139     { 2,        "Acknowledge" },
140     { 0, NULL },
141 };
142
143 static const value_string ansi_trans_param_strings[] = {
144     { 0x00,     "Teleservice Identifier" },
145     { 0x01,     "Service Category" },
146     { 0x02,     "Originating Address" },
147     { 0x03,     "Originating Subaddress" },
148     { 0x04,     "Destination Address" },
149     { 0x05,     "Destination Subaddress" },
150     { 0x06,     "Bearer Reply Option" },
151     { 0x07,     "Cause Codes" },
152     { 0x08,     "Bearer Data" },
153     { 0, NULL },
154 };
155
156 /*
157  * from Table 2.7.1.3.2.4-4. Representation of DTMF Digits
158  * 3GPP2 C.S0005-C (IS-2000 aka cdma2000)
159  */
160 static unsigned char air_digits[] = {
161   /*  0   1   2   3   4   5   6   7   8   9   a   b   c   d   e */
162      '?','1','2','3','4','5','6','7','8','9','0','*','#','?','?'
163 };
164
165 /* Initialize the protocol and registered fields */
166 static int proto_ansi_637_tele = -1;
167 static int proto_ansi_637_trans = -1;
168 static int hf_ansi_637_none = -1;
169 static int hf_ansi_637_length = -1;
170 static int hf_ansi_637_bin_addr = -1;
171 static int hf_ansi_637_tele_msg_type = -1;
172 static int hf_ansi_637_tele_msg_id = -1;
173 static int hf_ansi_637_tele_msg_rsvd = -1;
174 static int hf_ansi_637_tele_subparam_id = -1;
175 static int hf_ansi_637_trans_msg_type = -1;
176 static int hf_ansi_637_trans_param_id = -1;
177
178 /* Initialize the subtree pointers */
179 static gint ett_ansi_637_tele = -1;
180 static gint ett_ansi_637_trans = -1;
181 static gint ett_params = -1;
182
183 static guint32 ansi_637_trans_tele_id;
184 static char ansi_637_bigbuf[1024];
185 static gchar ansi_637_add_string[1024];
186 /* static dissector_handle_t data_handle; */
187 static dissector_table_t tele_dissector_table;
188 static packet_info *g_pinfo;
189 static proto_tree *g_tree;
190
191 /* FUNCTIONS */
192
193 static void
194 decode_7_bits(tvbuff_t *tvb, guint32 *offset, guint8 num_fields, guint8 *last_oct, guint8 *last_bit, gchar *buf)
195 {
196     guint8      oct, oct2, bit;
197     /* guint32  saved_offset; */
198     guint32     i;
199
200
201     if (num_fields == 0)
202     {
203         return;
204     }
205
206     /* saved_offset = *offset; */
207     oct = oct2 = *last_oct;
208     bit = *last_bit;
209
210     if (bit == 1)
211     {
212         oct2 = tvb_get_guint8(tvb, *offset);
213         (*offset)++;
214     }
215
216     for (i=0; i < num_fields; i++)
217     {
218         if (bit != 1)
219         {
220             oct = oct2;
221
222             /*
223              * cannot grab an octet if we are getting
224              * the last field and bit is 7 or 8
225              * because there may not be another octet
226              */
227             if (((i + 1) != num_fields) ||
228                 ((bit != 7) && (bit != 8)))
229             {
230                 oct2 = tvb_get_guint8(tvb, *offset);
231                 (*offset)++;
232             }
233         }
234
235         switch (bit)
236         {
237         case 1:
238             buf[i] = ((oct & 0x01) << 6) | ((oct2 & 0xfc) >> 2);
239             break;
240
241         case 2:
242             buf[i] = ((oct & 0x03) << 5) | ((oct2 & 0xf8) >> 3);
243             break;
244
245         case 3:
246             buf[i] = ((oct & 0x07) << 4) | ((oct2 & 0xf0) >> 4);
247             break;
248
249         case 4:
250             buf[i] = ((oct & 0x0f) << 3) | ((oct2 & 0xe0) >> 5);
251             break;
252
253         case 5:
254             buf[i] = ((oct & 0x1f) << 2) | ((oct2 & 0xc0) >> 6);
255             break;
256
257         case 6:
258             buf[i] = ((oct & 0x3f) << 1) | ((oct2 & 0x80) >> 7);
259             break;
260
261         case 7:
262             buf[i] = oct & 0x7f;
263             break;
264
265         case 8:
266             buf[i] = (oct & 0xfe) >> 1;
267             break;
268         }
269
270         bit = (bit % 8) + 1;
271     }
272
273     buf[i] = '\0';
274     *last_bit = bit;
275     *last_oct = (bit == 1) ? oct : oct2;
276 }
277
278 /* PARAM FUNCTIONS */
279
280 #define EXTRANEOUS_DATA_CHECK(edc_len, edc_max_len) \
281     if ((edc_len) > (edc_max_len)) \
282     { \
283         proto_tree_add_text(tree, tvb, offset, \
284             (edc_len) - (edc_max_len), "Extraneous Data"); \
285     }
286
287 #define SHORT_DATA_CHECK(sdc_len, sdc_min_len) \
288     if ((sdc_len) < (sdc_min_len)) \
289     { \
290         proto_tree_add_text(tree, tvb, offset, \
291             (sdc_len), "Short Data (?)"); \
292         return; \
293     }
294
295 #define EXACT_DATA_CHECK(edc_len, edc_eq_len) \
296     if ((edc_len) != (edc_eq_len)) \
297     { \
298         proto_tree_add_text(tree, tvb, offset, \
299             (edc_len), "Unexpected Data Length"); \
300         return; \
301     }
302
303 static void
304 tele_param_user_data(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset)
305 {
306     guint8      oct, oct2;
307     guint8      encoding;
308     guint8      msg_type;
309     guint8      num_fields;
310     guint8      used;
311     guint8      bit;
312     guint32     required_octs;
313     guint32     saved_offset;
314     guint32     i;
315     gchar       *str = NULL;
316
317     SHORT_DATA_CHECK(len, 2);
318
319     /*
320      * message encoding
321      */
322     oct = tvb_get_guint8(tvb, offset);
323     oct2 = 0;
324     msg_type = 0;
325     used = 0;
326
327     encoding = ((oct & 0xf8) >> 3);
328     switch (encoding)
329     {
330     case 0x00: str = "Octet, unspecified"; break;
331     case 0x01: str = "Extended Protocol Message";
332         oct2 = tvb_get_guint8(tvb, offset+1);
333         msg_type = ((oct & 0x07) << 5) | ((oct2 & 0xf8) >> 3);
334         break;
335     case 0x02: str = "7-bit ASCII"; break;
336     case 0x03: str = "IA5"; break;
337     case 0x04: str = "UNICODE"; break;
338     case 0x05: str = "Shift-JIS"; break;
339     case 0x06: str = "Korean"; break;
340     case 0x07: str = "Latin/Hebrew"; break;
341     case 0x08: str = "Latin"; break;
342     case 0x09: str = "GSM 7-bit default alphabet"; break;
343     default: str = "Reserved"; break;
344     }
345
346     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0xf8, 8);
347     proto_tree_add_text(tree, tvb, offset, 1,
348         "%s :  Encoding: %s",
349         ansi_637_bigbuf,
350         str);
351
352     if (encoding == 0x01)
353     {
354         other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x07, 8);
355         proto_tree_add_text(tree, tvb, offset, 1,
356             "%s :  Message type: see TIA/EIA/IS-91 (%d)",
357             ansi_637_bigbuf,
358             msg_type);
359
360         other_decode_bitfield_value(ansi_637_bigbuf, oct2, 0xf8, 8);
361         proto_tree_add_text(tree, tvb, offset+1, 1,
362             "%s :  Message type",
363             ansi_637_bigbuf);
364
365         oct = oct2;
366         offset++;
367         used++;
368     }
369
370     offset++;
371     used++;
372
373     /*
374      * number of fields
375      */
376     oct2 = tvb_get_guint8(tvb, offset);
377     num_fields = ((oct & 0x07) << 5) | ((oct2 & 0xf8) >> 3);
378
379     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x07, 8);
380     proto_tree_add_text(tree, tvb, offset-1, 1,
381         "%s :  Number of fields (MSB): %d",
382         ansi_637_bigbuf,
383         num_fields);
384
385     other_decode_bitfield_value(ansi_637_bigbuf, oct2, 0xf8, 8);
386     proto_tree_add_text(tree, tvb, offset, 1,
387         "%s :  Number of fields (LSB)",
388         ansi_637_bigbuf);
389
390     other_decode_bitfield_value(ansi_637_bigbuf, oct2, 0x07, 8);
391     proto_tree_add_text(tree, tvb, offset, 1,
392         "%s :  Most significant bits of first field",
393         ansi_637_bigbuf);
394
395     offset++;
396     used++;
397     oct = oct2;
398
399     /* NOTE: there are now 3 bits remaining in 'oct' */
400
401     if (len <= used) return;
402
403     /*
404      * decode rest if 7-bit ASCII
405      */
406     if (encoding == 0x02)
407     {
408         /*
409          * magic numbers:
410          *      3 bits remaining from last octet
411          *      7 bit encoding
412          *      8 bits per octet
413          */
414         i = (num_fields * 7) - 3;
415         required_octs = (i / 8) + ((i % 8) ? 1 : 0);
416
417         if (required_octs + used > len)
418         {
419             proto_tree_add_text(tree, tvb, offset, 1,
420                 "Missing %d octet(s) for number of fields",
421                 (required_octs + used) - len);
422
423             return;
424         }
425
426         bit = 3;
427         saved_offset = offset;
428
429         decode_7_bits(tvb, &offset, num_fields, &oct, &bit, ansi_637_bigbuf);
430
431         proto_tree_add_text(tree, tvb, saved_offset, offset - saved_offset,
432             "Encoded user data: %s",
433             ansi_637_bigbuf);
434
435         switch (bit)
436         {
437         case 1: oct2 = 0x01; break;
438         case 2: oct2 = 0x03; break;
439         case 3: oct2 = 0x07; break;
440         case 4: oct2 = 0x0f; break;
441         case 5: oct2 = 0x1f; break;
442         case 6: oct2 = 0x3f; break;
443         case 7: oct2 = 0x7f; break;
444         }
445
446         if (bit != 8)
447         {
448             other_decode_bitfield_value(ansi_637_bigbuf, oct, oct2, 8);
449             proto_tree_add_text(tree, tvb, offset - 1, 1,
450                 "%s :  Reserved",
451                 ansi_637_bigbuf);
452         }
453     }
454     else
455     {
456         proto_tree_add_text(tree, tvb, offset, len - used,
457             "Encoded user data");
458     }
459 }
460
461 static void
462 tele_param_rsp_code(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset)
463 {
464     guint8      oct;
465
466     EXACT_DATA_CHECK(len, 1);
467
468     /*
469      * response code
470      */
471     oct = tvb_get_guint8(tvb, offset);
472
473     proto_tree_add_text(tree, tvb, offset, 1,
474         "Response code: %d",
475         oct);
476 }
477
478 static void
479 tele_param_timestamp(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset)
480 {
481     guint8      oct, oct2, oct3;
482
483     EXACT_DATA_CHECK(len, 6);
484
485     oct = tvb_get_guint8(tvb, offset);
486     oct2 = tvb_get_guint8(tvb, offset+1);
487     oct3 = tvb_get_guint8(tvb, offset+2);
488
489     proto_tree_add_text(tree, tvb, offset, 3,
490         "Year %d%d, Month %d%d, Day %d%d",
491         (oct & 0xf0) >> 4,
492         oct & 0x0f,
493         (oct2 & 0xf0) >> 4,
494         oct2 & 0x0f,
495         (oct3 & 0xf0) >> 4,
496         oct3 & 0x0f);
497
498     offset += 3;
499
500     oct = tvb_get_guint8(tvb, offset);
501     oct2 = tvb_get_guint8(tvb, offset+1);
502     oct3 = tvb_get_guint8(tvb, offset+2);
503
504     proto_tree_add_text(tree, tvb, offset, 3,
505         "Hour %d%d, Minutes %d%d, Seconds %d%d",
506         (oct & 0xf0) >> 4,
507         oct & 0x0f,
508         (oct2 & 0xf0) >> 4,
509         oct2 & 0x0f,
510         (oct3 & 0xf0) >> 4,
511         oct3 & 0x0f);
512 }
513
514 static void
515 tele_param_rel_timestamp(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset)
516 {
517     guint8      oct;
518     guint32     value = 0;
519     gchar       *str = NULL;
520     gchar       *str2 = NULL;
521
522     EXACT_DATA_CHECK(len, 1);
523
524     oct = tvb_get_guint8(tvb, offset);
525
526     switch (oct)
527     {
528     case 245: str = "Indefinite"; break;
529     case 246: str = "Immediate"; break;
530     case 247: str = "Valid until mobile becomes inactive/Deliver when mobile next becomes active"; break;
531     case 248: str = "Valid until registration area changes, discard if not registered" ; break;
532     default:
533         if (oct <= 143) { value = (oct + 1) * 5; str2 = "Minutes"; break; }
534         else if ((oct >= 144) && (oct <= 167)) { value = (oct - 143) * 30; str2 = "Minutes + 12 Hours"; break; }
535         else if ((oct >= 168) && (oct <= 196)) { value = oct - 166; str2 = "Days"; break; }
536         else if ((oct >= 197) && (oct <= 244)) { value = oct - 192; str2 = "Weeks"; break; }
537         else { str = "Reserved"; break; }
538     }
539
540     if (str == NULL)
541     {
542         proto_tree_add_text(tree, tvb, offset, 1,
543             str2);
544     }
545     else
546     {
547         proto_tree_add_text(tree, tvb, offset, 1,
548             "%d %s",
549             value, str2);
550     }
551 }
552
553 static void
554 tele_param_pri_ind(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset)
555 {
556     guint8      oct;
557     gchar       *str = NULL;
558
559     EXACT_DATA_CHECK(len, 1);
560
561     oct = tvb_get_guint8(tvb, offset);
562
563     switch ((oct & 0xc0) >> 6)
564     {
565     case 0: str = "Normal"; break;
566     case 1: str = "Interactive"; break;
567     case 2: str = "Urgent"; break;
568     case 3: str = "Emergency"; break;
569     }
570
571     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0xc0, 8);
572     proto_tree_add_text(tree, tvb, offset, 1,
573         "%s :  %s",
574         ansi_637_bigbuf,
575         str);
576
577     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x3f, 8);
578     proto_tree_add_text(tree, tvb, offset, 1,
579         "%s :  Reserved",
580         ansi_637_bigbuf);
581 }
582
583 static void
584 tele_param_priv_ind(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset)
585 {
586     guint8      oct;
587     gchar       *str = NULL;
588
589     EXACT_DATA_CHECK(len, 1);
590
591     oct = tvb_get_guint8(tvb, offset);
592
593     switch ((oct & 0xc0) >> 6)
594     {
595     case 0: str = "Not restricted (privacy level 0)"; break;
596     case 1: str = "Restricted (privacy level 1)"; break;
597     case 2: str = "Confidential (privacy level 2)"; break;
598     case 3: str = "Secret (privacy level 3)"; break;
599     }
600
601     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0xc0, 8);
602     proto_tree_add_text(tree, tvb, offset, 1,
603         "%s :  %s",
604         ansi_637_bigbuf,
605         str);
606
607     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x3f, 8);
608     proto_tree_add_text(tree, tvb, offset, 1,
609         "%s :  Reserved",
610         ansi_637_bigbuf);
611 }
612
613 static void
614 tele_param_reply_opt(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset)
615 {
616     guint8      oct;
617
618     EXACT_DATA_CHECK(len, 1);
619
620     oct = tvb_get_guint8(tvb, offset);
621
622     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x80, 8);
623     proto_tree_add_text(tree, tvb, offset, 1,
624         "%s :  %s (manual) acknowledgment is requested",
625         ansi_637_bigbuf,
626         (oct & 0x80) ? "User" : "No user");
627
628     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x40, 8);
629     proto_tree_add_text(tree, tvb, offset, 1,
630         "%s :  %s acknowledgment requested",
631         ansi_637_bigbuf,
632         (oct & 0x40) ? "Delivery" : "No delivery");
633
634     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x3f, 8);
635     proto_tree_add_text(tree, tvb, offset, 1,
636         "%s :  Reserved",
637         ansi_637_bigbuf);
638 }
639
640 static void
641 tele_param_num_messages(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset)
642 {
643     guint8      oct;
644
645     EXACT_DATA_CHECK(len, 1);
646
647     oct = tvb_get_guint8(tvb, offset);
648
649     proto_tree_add_text(tree, tvb, offset, 1,
650         "Number of voice mail messages: %d%d",
651         (oct & 0xf0) >> 4,
652         oct & 0x0f);
653 }
654
655 static void
656 tele_param_alert(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset)
657 {
658     guint8      oct;
659     gchar       *str = NULL;
660
661     EXACT_DATA_CHECK(len, 1);
662
663     oct = tvb_get_guint8(tvb, offset);
664
665     switch ((oct & 0xc0) >> 6)
666     {
667     case 0: str = "Use Mobile default alert"; break;
668     case 1: str = "Use Low-priority alert"; break;
669     case 2: str = "Use Medium-priority alert"; break;
670     case 3: str = "Use High-priority alert"; break;
671     }
672
673     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0xc0, 8);
674     proto_tree_add_text(tree, tvb, offset, 1,
675         "%s :  %s",
676         ansi_637_bigbuf,
677         str);
678
679     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x3f, 8);
680     proto_tree_add_text(tree, tvb, offset, 1,
681         "%s :  Reserved",
682         ansi_637_bigbuf);
683 }
684
685 static void
686 tele_param_lang_ind(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset)
687 {
688     guint8      oct;
689     gchar       *str = NULL;
690
691     EXACT_DATA_CHECK(len, 1);
692
693     oct = tvb_get_guint8(tvb, offset);
694
695     switch (oct)
696     {
697     case 0x00: str = "Unknown or unspecified"; break;
698     case 0x01: str = "English"; break;
699     case 0x02: str = "French"; break;
700     case 0x03: str = "Spanish"; break;
701     case 0x04: str = "Japanese"; break;
702     case 0x05: str = "Korean"; break;
703     case 0x06: str = "Chinese"; break;
704     case 0x07: str = "Hebrew"; break;
705     default: str = "Reserved"; break;
706     }
707
708     proto_tree_add_text(tree, tvb, offset, 1,
709         str);
710 }
711
712 static void
713 tele_param_cb_num(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset)
714 {
715     guint8      oct, oct2, num_fields, odd;
716     guint32     saved_offset;
717     guint32     required_octs;
718     guint32     i;
719
720     SHORT_DATA_CHECK(len, 2);
721
722     oct = tvb_get_guint8(tvb, offset);
723
724     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x80, 8);
725     proto_tree_add_text(tree, tvb, offset, 1,
726         "%s :  Digit mode: %s",
727         ansi_637_bigbuf,
728         (oct & 0x80) ? "8-bit ASCII" : "4-bit DTMF");
729
730     if (oct & 0x80)
731     {
732         other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x70, 8);
733         proto_tree_add_text(tree, tvb, offset, 1,
734             "%s :  Type of number: (%d)",
735             ansi_637_bigbuf,
736             (oct & 0x70) >> 4);
737
738         other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x0f, 8);
739         proto_tree_add_text(tree, tvb, offset, 1,
740             "%s :  Numbering plan: (%d)",
741             ansi_637_bigbuf,
742             oct & 0x0f);
743
744         offset++;
745         num_fields = tvb_get_guint8(tvb, offset);
746
747         other_decode_bitfield_value(ansi_637_bigbuf, oct, 0xff, 8);
748         proto_tree_add_text(tree, tvb, offset, 1,
749             "%s :  Number of fields: (%d)",
750             ansi_637_bigbuf,
751             num_fields);
752
753         if (num_fields == 0) return;
754
755         if (num_fields > (len - 2))
756         {
757             proto_tree_add_text(tree, tvb, offset, 1,
758                 "Missing %d octet(s) for number of fields",
759                 (num_fields + 2) - len);
760
761             return;
762         }
763
764         offset++;
765
766         i = 0;
767         while (i < num_fields)
768         {
769             ansi_637_bigbuf[i] = tvb_get_guint8(tvb, offset+i) & 0x7f;
770             i++;
771         }
772         ansi_637_bigbuf[i] = '\0';
773
774         proto_tree_add_text(tree, tvb, offset, num_fields,
775             "Number: %s",
776             ansi_637_bigbuf);
777     }
778     else
779     {
780         offset++;
781         num_fields = (oct & 0x7f) << 1;
782         oct2 = tvb_get_guint8(tvb, offset);
783         num_fields |= ((oct2 & 0x80) >> 7);
784
785         other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x7f, 8);
786         proto_tree_add_text(tree, tvb, offset-1, 1,
787             "%s :  Number of fields (MBS): (%d)",
788             ansi_637_bigbuf,
789             num_fields);
790
791         other_decode_bitfield_value(ansi_637_bigbuf, oct2, 0x80, 8);
792         proto_tree_add_text(tree, tvb, offset, 1,
793             "%s :  Number of fields (LSB)",
794             ansi_637_bigbuf);
795
796         oct = oct2;
797         odd = FALSE;
798
799         if (num_fields > 0)
800         {
801             i = (num_fields - 1) * 4;
802             required_octs = (i / 8) + ((i % 8) ? 1 : 0);
803
804             if (required_octs + 2 > len)
805             {
806                 proto_tree_add_text(tree, tvb, offset, 1,
807                     "Missing %d octet(s) for number of fields",
808                     (required_octs + 2) - len);
809
810                 return;
811             }
812
813             odd = num_fields & 0x01;
814             memset((void *) ansi_637_bigbuf, 0, sizeof(ansi_637_bigbuf));
815             saved_offset = offset;
816             offset++;
817
818             i = 0;
819             while (i < num_fields)
820             {
821                 ansi_637_bigbuf[i] =
822                     air_digits[(oct & 0x78) >> 3];
823
824                 i++;
825                 if (i >= num_fields) break;
826
827                 oct2 = tvb_get_guint8(tvb, offset);
828                 offset++;
829
830                 ansi_637_bigbuf[i] =
831                     air_digits[((oct & 0x07) << 1) | ((oct2 & 0x80) >> 7)];
832
833                 oct = oct2;
834
835                 i++;
836             }
837
838             proto_tree_add_text(tree, tvb, saved_offset, offset - saved_offset,
839                 "Number: %s",
840                 ansi_637_bigbuf);
841         }
842
843         other_decode_bitfield_value(ansi_637_bigbuf, oct, odd ? 0x07: 0x7f, 8);
844         proto_tree_add_text(tree, tvb, offset, 1,
845             "%s :  Reserved",
846             ansi_637_bigbuf);
847     }
848 }
849
850 static void
851 tele_param_disp_mode(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset)
852 {
853     guint8      oct;
854     gchar       *str = NULL;
855
856     EXACT_DATA_CHECK(len, 1);
857
858     oct = tvb_get_guint8(tvb, offset);
859
860     switch ((oct & 0xc0) >> 6)
861     {
862     case 0: str = "Immediate Display: The mobile station is to display the received message as soon as possible."; break;
863     case 1: str = "Mobile default setting: The mobile station is to display the received message based on a pre-defined mode in the mobile station."; break;
864     case 2: str = "User Invoke: The mobile station is to display the received message based on the mode selected by the user."; break;
865     case 3: str = "Reserved"; break;
866     }
867
868     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0xc0, 8);
869     proto_tree_add_text(tree, tvb, offset, 1,
870         "%s :  %s",
871         ansi_637_bigbuf,
872         str);
873
874     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x3f, 8);
875     proto_tree_add_text(tree, tvb, offset, 1,
876         "%s :  Reserved",
877         ansi_637_bigbuf);
878 }
879
880 #define NUM_TELE_PARAM (sizeof(ansi_tele_param_strings)/sizeof(value_string))
881 static gint ett_ansi_637_tele_param[NUM_TELE_PARAM];
882 static void (*ansi_637_tele_param_fcn[])(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset) = {
883     NULL,                       /* Message Identifier */
884     tele_param_user_data,       /* User Data */
885     tele_param_rsp_code,        /* User Response Code */
886     tele_param_timestamp,       /* Message Center Time Stamp */
887     tele_param_timestamp,       /* Validity Period  Absolute */
888     tele_param_rel_timestamp,   /* Validity Period  Relative */
889     tele_param_timestamp,       /* Deferred Delivery Time - Absolute */
890     tele_param_rel_timestamp,   /* Deferred Delivery Time - Relative */
891     tele_param_pri_ind,         /* Priority Indicator */
892     tele_param_priv_ind,        /* Privacy Indicator */
893     tele_param_reply_opt,       /* Reply Option */
894     tele_param_num_messages,    /* Number of Messages */
895     tele_param_alert,           /* Alert on Message Delivery */
896     tele_param_lang_ind,        /* Language Indicator */
897     tele_param_cb_num,          /* Call-Back Number */
898     tele_param_disp_mode,       /* Message Display Mode */
899     NULL,                       /* Multiple Encoding User Data */
900     NULL,                       /* NONE */
901 };
902
903 static void
904 trans_param_tele_id(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset, gchar *add_string)
905 {
906     guint32     value;
907     const gchar *str = NULL;
908
909     EXACT_DATA_CHECK(len, 2);
910
911     value = tvb_get_ntohs(tvb, offset);
912
913     ansi_637_trans_tele_id = value;
914
915     str = match_strval(value, ansi_tele_id_strings);
916
917     if (NULL == str) str = "Unrecognized Teleservice ID";
918
919     proto_tree_add_text(tree, tvb, offset, 2,
920         "%s (%d)",
921         str,
922         value);
923
924     sprintf(add_string, " - %s (%d)", str, value);
925 }
926
927 static void
928 trans_param_srvc_cat(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset, gchar *add_string)
929 {
930     guint32     value;
931     const gchar *str = NULL;
932
933     EXACT_DATA_CHECK(len, 2);
934
935     value = tvb_get_ntohs(tvb, offset);
936
937     str = match_strval(value, ansi_srvc_cat_strings);
938
939     if (NULL == str) str = "Reserved";
940
941     proto_tree_add_text(tree, tvb, offset, 2,
942         str);
943
944     sprintf(add_string, " - %s (%d)", str, value);
945 }
946
947 static void
948 trans_param_address(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset, gchar *add_string)
949 {
950     guint8      oct, oct2, num_fields, odd;
951     gboolean    email_addr;
952     guint32     saved_offset;
953     guint32     required_octs;
954     guint32     i;
955     gchar       *str;
956
957     SHORT_DATA_CHECK(len, 2);
958
959     add_string = add_string;
960     email_addr = FALSE;
961
962     oct = tvb_get_guint8(tvb, offset);
963
964     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x80, 8);
965     proto_tree_add_text(tree, tvb, offset, 1,
966         "%s :  Digit mode: %s",
967         ansi_637_bigbuf,
968         (oct & 0x80) ? "8-bit ASCII" : "4-bit DTMF");
969
970     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x40, 8);
971     proto_tree_add_text(tree, tvb, offset, 1,
972         "%s :  Number mode: %s",
973         ansi_637_bigbuf,
974         (oct & 0x40) ? "Data Network Address" : "ANSI T1.607");
975
976     if (oct & 0x80)
977     {
978         if (oct & 0x40)
979         {
980             switch ((oct & 0x38) >> 3)
981             {
982             case 0: str = "Unknown"; break;
983             case 1: str = "Internet Protocol (RFC 791)"; break;
984             case 2: str = "Internet Email Address (RFC 822)"; email_addr = TRUE; break;
985             default:
986                 str = "Reserved";
987                 break;
988             }
989
990             other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x38, 8);
991             proto_tree_add_text(tree, tvb, offset, 1,
992                 "%s :  Type of number: %s (%d)",
993                 ansi_637_bigbuf,
994                 str,
995                 (oct & 0x38) >> 3);
996
997             offset++;
998             num_fields = (oct & 0x07) << 5;
999             oct2 = tvb_get_guint8(tvb, offset);
1000             num_fields |= ((oct2 & 0xf8) >> 3);
1001
1002             other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x07, 8);
1003             proto_tree_add_text(tree, tvb, offset-1, 1,
1004                 "%s :  Number of fields (MSB): (%d)",
1005                 ansi_637_bigbuf,
1006                 num_fields);
1007
1008             other_decode_bitfield_value(ansi_637_bigbuf, oct2, 0xf8, 8);
1009             proto_tree_add_text(tree, tvb, offset, 1,
1010                 "%s :  Number of fields (LSB)",
1011                 ansi_637_bigbuf);
1012
1013             if (num_fields == 0) return;
1014
1015             if (num_fields > (len - 2))
1016             {
1017                 proto_tree_add_text(tree, tvb, offset, 1,
1018                     "Missing %d octet(s) for number of fields",
1019                     (num_fields + 2) - len);
1020
1021                 return;
1022             }
1023
1024             other_decode_bitfield_value(ansi_637_bigbuf, oct2, 0x07, 8);
1025             proto_tree_add_text(tree, tvb, offset, 1,
1026                 "%s :  Most significant bits of first field",
1027                 ansi_637_bigbuf);
1028
1029             offset++;
1030             oct = oct2;
1031
1032             i = 0;
1033             while (i < num_fields)
1034             {
1035                 ansi_637_bigbuf[i] = (oct & 0x07) << 5;
1036                 ansi_637_bigbuf[i] |= ((oct = tvb_get_guint8(tvb, offset+i)) & 0xf8) >> 3;
1037                 i++;
1038             }
1039             ansi_637_bigbuf[i] = '\0';
1040
1041             if (email_addr)
1042             {
1043                 proto_tree_add_text(tree, tvb, offset, num_fields - 1,
1044                     "Number: %s",
1045                     ansi_637_bigbuf);
1046             }
1047             else
1048             {
1049                 proto_tree_add_bytes(tree, hf_ansi_637_bin_addr, tvb, offset, num_fields - 1,
1050                     ansi_637_bigbuf);
1051             }
1052
1053             offset += (num_fields - 1);
1054
1055             other_decode_bitfield_value(ansi_637_bigbuf, oct, 0xf8, 8);
1056             proto_tree_add_text(tree, tvb, offset, 1,
1057                 "%s :  Least significant bits of last field",
1058                 ansi_637_bigbuf);
1059
1060             other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x07, 8);
1061             proto_tree_add_text(tree, tvb, offset, 1,
1062                 "%s :  Reserved",
1063                 ansi_637_bigbuf);
1064         }
1065         else
1066         {
1067             other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x38, 8);
1068             proto_tree_add_text(tree, tvb, offset, 1,
1069                 "%s :  Type of number: (%d)",
1070                 ansi_637_bigbuf,
1071                 (oct & 0x38) >> 3);
1072
1073             oct2 = tvb_get_guint8(tvb, offset + 1);
1074
1075             other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x07, 8);
1076             proto_tree_add_text(tree, tvb, offset, 1,
1077                 "%s :  Numbering plan (MSB): (%d)",
1078                 ansi_637_bigbuf,
1079                 ((oct & 0x07) << 1) | ((oct2 & 0x80) >> 7));
1080
1081             offset++;
1082             oct = oct2;
1083
1084             other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x80, 8);
1085             proto_tree_add_text(tree, tvb, offset, 1,
1086                 "%s :  Numbering plan (LSB)",
1087                 ansi_637_bigbuf);
1088
1089             offset++;
1090             num_fields = (oct & 0x7f) << 1;
1091             oct2 = tvb_get_guint8(tvb, offset);
1092             num_fields |= ((oct2 & 0x80) >> 7);
1093
1094             other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x7f, 8);
1095             proto_tree_add_text(tree, tvb, offset-1, 1,
1096                 "%s :  Number of fields (MSB): (%d)",
1097                 ansi_637_bigbuf,
1098                 num_fields);
1099
1100             other_decode_bitfield_value(ansi_637_bigbuf, oct2, 0x80, 8);
1101             proto_tree_add_text(tree, tvb, offset, 1,
1102                 "%s :  Number of fields (LSB)",
1103                 ansi_637_bigbuf);
1104
1105             if (num_fields == 0) return;
1106
1107             if (num_fields > (len - 3))
1108             {
1109                 proto_tree_add_text(tree, tvb, offset, 1,
1110                     "Missing %d octet(s) for number of fields",
1111                     (num_fields + 3) - len);
1112
1113                 return;
1114             }
1115
1116             other_decode_bitfield_value(ansi_637_bigbuf, oct2, 0x7f, 8);
1117             proto_tree_add_text(tree, tvb, offset, 1,
1118                 "%s :  Most significant bits of first field",
1119                 ansi_637_bigbuf);
1120
1121             offset++;
1122             oct = oct2;
1123
1124             i = 0;
1125             while (i < num_fields)
1126             {
1127                 ansi_637_bigbuf[i] = (oct & 0x7f) << 1;
1128                 ansi_637_bigbuf[i] |= ((oct = tvb_get_guint8(tvb, offset+i)) & 0x80) >> 7;
1129                 i++;
1130             }
1131             ansi_637_bigbuf[i] = '\0';
1132
1133             proto_tree_add_text(tree, tvb, offset, num_fields - 1,
1134                 "Number: %s",
1135                 ansi_637_bigbuf);
1136
1137             offset += (num_fields - 1);
1138
1139             other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x80, 8);
1140             proto_tree_add_text(tree, tvb, offset, 1,
1141                 "%s :  Least significant bit of last field",
1142                 ansi_637_bigbuf);
1143
1144             other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x7f, 8);
1145             proto_tree_add_text(tree, tvb, offset, 1,
1146                 "%s :  Reserved",
1147                 ansi_637_bigbuf);
1148         }
1149     }
1150     else
1151     {
1152         offset++;
1153         num_fields = (oct & 0x3f) << 2;
1154         oct2 = tvb_get_guint8(tvb, offset);
1155         num_fields |= ((oct2 & 0xc0) >> 6);
1156
1157         other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x3f, 8);
1158         proto_tree_add_text(tree, tvb, offset-1, 1,
1159             "%s :  Number of fields (MSB): (%d)",
1160             ansi_637_bigbuf,
1161             num_fields);
1162
1163         other_decode_bitfield_value(ansi_637_bigbuf, oct2, 0xc0, 8);
1164         proto_tree_add_text(tree, tvb, offset, 1,
1165             "%s :  Number of fields (LSB)",
1166             ansi_637_bigbuf);
1167
1168         oct = oct2;
1169         odd = FALSE;
1170
1171         if (num_fields > 0)
1172         {
1173             i = (num_fields - 1) * 4;
1174             required_octs = (i / 8) + ((i % 8) ? 1 : 0);
1175
1176             if (required_octs + 2 > len)
1177             {
1178                 proto_tree_add_text(tree, tvb, offset, 1,
1179                     "Missing %d octet(s) for number of fields",
1180                     (required_octs + 2) - len);
1181
1182                 return;
1183             }
1184
1185             odd = num_fields & 0x01;
1186             memset((void *) ansi_637_bigbuf, 0, sizeof(ansi_637_bigbuf));
1187             saved_offset = offset;
1188             offset++;
1189
1190             i = 0;
1191             while (i < num_fields)
1192             {
1193                 ansi_637_bigbuf[i] =
1194                     air_digits[(oct & 0x3c) >> 2];
1195
1196                 i++;
1197                 if (i >= num_fields) break;
1198
1199                 oct2 = tvb_get_guint8(tvb, offset);
1200                 offset++;
1201
1202                 ansi_637_bigbuf[i] =
1203                     air_digits[((oct & 0x03) << 2) | ((oct2 & 0xc0) >> 6)];
1204
1205                 oct = oct2;
1206
1207                 i++;
1208             }
1209
1210             proto_tree_add_text(tree, tvb, saved_offset, offset - saved_offset,
1211                 "Number: %s",
1212                 ansi_637_bigbuf);
1213         }
1214
1215         other_decode_bitfield_value(ansi_637_bigbuf, oct, odd ? 0x03: 0x3f, 8);
1216         proto_tree_add_text(tree, tvb, offset, 1,
1217             "%s :  Reserved",
1218             ansi_637_bigbuf);
1219     }
1220 }
1221
1222 static void
1223 trans_param_subaddress(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset, gchar *add_string)
1224 {
1225     guint8      oct, oct2, num_fields;
1226     guint32     i;
1227     gchar       *str;
1228
1229     SHORT_DATA_CHECK(len, 2);
1230
1231     add_string = add_string;
1232
1233     oct = tvb_get_guint8(tvb, offset);
1234
1235     switch ((oct & 0xe0) >> 5)
1236     {
1237     case 0: str = "NSAP (CCITT Recommendation X.213 or ISO 8348 AD2)"; break;
1238     case 1: str = "User-specified"; break;
1239     default:
1240         str = "Reserved";
1241         break;
1242     }
1243
1244     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0xe0, 8);
1245     proto_tree_add_text(tree, tvb, offset, 1,
1246         "%s :  Type: %s",
1247         ansi_637_bigbuf,
1248         str);
1249
1250     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x10, 8);
1251     proto_tree_add_text(tree, tvb, offset, 1,
1252         "%s :  Odd",
1253         ansi_637_bigbuf);
1254
1255     offset++;
1256     num_fields = (oct & 0x0f) << 4;
1257     oct2 = tvb_get_guint8(tvb, offset);
1258     num_fields |= ((oct2 & 0xf0) >> 4);
1259
1260     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x0f, 8);
1261     proto_tree_add_text(tree, tvb, offset-1, 1,
1262         "%s :  Number of fields (MSB): (%d)",
1263         ansi_637_bigbuf,
1264         num_fields);
1265
1266     other_decode_bitfield_value(ansi_637_bigbuf, oct2, 0xf0, 8);
1267     proto_tree_add_text(tree, tvb, offset, 1,
1268         "%s :  Number of fields (LSB)",
1269         ansi_637_bigbuf);
1270
1271     if (num_fields == 0) return;
1272
1273     if (num_fields > (len - 2))
1274     {
1275         proto_tree_add_text(tree, tvb, offset, 1,
1276             "Missing %d octet(s) for number of fields",
1277             (num_fields + 2) - len);
1278
1279         return;
1280     }
1281
1282     other_decode_bitfield_value(ansi_637_bigbuf, oct2, 0x0f, 8);
1283     proto_tree_add_text(tree, tvb, offset, 1,
1284         "%s :  Most significant bits of first field",
1285         ansi_637_bigbuf);
1286
1287     offset++;
1288     oct = oct2;
1289
1290     i = 0;
1291     while (i < num_fields)
1292     {
1293         ansi_637_bigbuf[i] = (oct & 0x0f) << 4;
1294         ansi_637_bigbuf[i] |= ((oct = tvb_get_guint8(tvb, offset+i)) & 0xf0) >> 4;
1295         i++;
1296     }
1297     ansi_637_bigbuf[i] = '\0';
1298
1299     proto_tree_add_bytes(tree, hf_ansi_637_bin_addr, tvb, offset, num_fields - 1,
1300         ansi_637_bigbuf);
1301
1302     offset += (num_fields - 1);
1303
1304     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0xf0, 8);
1305     proto_tree_add_text(tree, tvb, offset, 1,
1306         "%s :  Least significant bits of last field",
1307         ansi_637_bigbuf);
1308
1309     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x0f, 8);
1310     proto_tree_add_text(tree, tvb, offset, 1,
1311         "%s :  Reserved",
1312         ansi_637_bigbuf);
1313 }
1314
1315 static void
1316 trans_param_bearer_reply_opt(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset, gchar *add_string)
1317 {
1318     guint8      oct;
1319
1320     len = len;
1321     oct = tvb_get_guint8(tvb, offset);
1322
1323     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0xfc, 8);
1324     proto_tree_add_text(tree, tvb, offset, 1,
1325         "%s :  Reply Sequence Number: %d",
1326         ansi_637_bigbuf,
1327         (oct & 0xfc) >> 2);
1328
1329     sprintf(add_string, " - Reply Sequence Number (%d)", (oct & 0xfc) >> 2);
1330
1331     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x03, 8);
1332     proto_tree_add_text(tree, tvb, offset, 1,
1333         "%s :  Reserved",
1334         ansi_637_bigbuf);
1335 }
1336
1337 static void
1338 trans_param_cause_codes(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset, gchar *add_string)
1339 {
1340     guint8      oct;
1341     gchar       *str = NULL;
1342
1343     oct = tvb_get_guint8(tvb, offset);
1344
1345     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0xfc, 8);
1346     proto_tree_add_text(tree, tvb, offset, 1,
1347         "%s :  Reply Sequence Number: %d",
1348         ansi_637_bigbuf,
1349         (oct & 0xfc) >> 2);
1350
1351     switch (oct & 0x03)
1352     {
1353     case 0x00: str = "No error"; break;
1354     case 0x02: str = "Temporary Condition"; break;
1355     case 0x03: str = "Permanent Condition"; break;
1356     default:
1357         str = "Reserved";
1358         break;
1359     }
1360
1361     sprintf(add_string, " - Reply Sequence Number (%d)", (oct & 0xfc) >> 2);
1362
1363     other_decode_bitfield_value(ansi_637_bigbuf, oct, 0x03, 8);
1364     proto_tree_add_text(tree, tvb, offset, 1,
1365         "%s :  Error Class: %s",
1366         ansi_637_bigbuf,
1367         str);
1368
1369     offset++;
1370
1371     if (!(oct & 0x03)) return;
1372
1373     if (len == 1) return;
1374
1375     oct = tvb_get_guint8(tvb, offset);
1376
1377     switch (oct)
1378     {
1379     case 0: str = "Address vacant"; break;
1380     case 1: str = "Address translation failure"; break;
1381     case 2: str = "Network resource shortage"; break;
1382     case 3: str = "Network failure"; break;
1383     case 4: str = "Invalid Teleservice ID"; break;
1384     case 5: str = "Other network problem"; break;
1385     case 6: str = "Unsupported network interface"; break;
1386     case 32: str = "No page response"; break;
1387     case 33: str = "Destination busy"; break;
1388     case 34: str = "No acknowledgement"; break;
1389     case 35: str = "Destination resource shortage"; break;
1390     case 36: str = "SMS delivery postponed"; break;
1391     case 37: str = "Destination out of service"; break;
1392     case 38: str = "Destination no longer at this address"; break;
1393     case 39: str = "Other terminal problem"; break;
1394     case 64: str = "Radio interface resource shortage"; break;
1395     case 65: str = "Radio interface incompatibility"; break;
1396     case 66: str = "Other radio interface problem"; break;
1397     case 67: str = "Unsupported Base Station Capability"; break;
1398     case 96: str = "Encoding problem"; break;
1399     case 97: str = "Service origination denied"; break;
1400     case 98: str = "Service termination denied"; break;
1401     case 99: str = "Supplementary service not supported"; break;
1402     case 100: str = "Service not supported"; break;
1403     case 101: str = "Reserved"; break;
1404     case 102: str = "Missing expected parameter"; break;
1405     case 103: str = "Missing mandatory parameter"; break;
1406     case 104: str = "Unrecognized parameter value"; break;
1407     case 105: str = "Unexpected parameter value"; break;
1408     case 106: str = "User Data size error"; break;
1409     case 107: str = "Other general problems"; break;
1410     case 108: str = "Session not active"; break;
1411     default:
1412         if ((oct >= 7) && (oct <= 31)) { str = "Reserved, treat as Other network problem"; }
1413         else if ((oct >= 40) && (oct <= 47)) { str = "Reserved, treat as Other terminal problem"; }
1414         else if ((oct >= 48) && (oct <= 63)) { str = "Reserved, treat as SMS delivery postponed"; }
1415         else if ((oct >= 68) && (oct <= 95)) { str = "Reserved, treat as Other radio interface problem"; }
1416         else if ((oct >= 109) && (oct <= 223)) { str = "Reserved, treat as Other general problems"; }
1417         else { str = "Reserved for protocol extension, treat as Other general problems"; }
1418         break;
1419     }
1420
1421     proto_tree_add_text(tree, tvb, offset, 1,
1422         str);
1423 }
1424
1425 static void
1426 trans_param_bearer_data(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset, gchar *add_string)
1427 {
1428     tvbuff_t    *tele_tvb;
1429
1430     add_string = add_string;
1431
1432     proto_tree_add_text(tree, tvb, offset, len,
1433         "Bearer Data");
1434
1435     /*
1436      * dissect the embedded teleservice data
1437      */
1438     tele_tvb = tvb_new_subset(tvb, offset, len, len);
1439
1440     dissector_try_port(tele_dissector_table, ansi_637_trans_tele_id,
1441         tele_tvb, g_pinfo, g_tree);
1442 }
1443
1444 #define NUM_TRANS_PARAM (sizeof(ansi_trans_param_strings)/sizeof(value_string))
1445 static gint ett_ansi_637_trans_param[NUM_TRANS_PARAM];
1446 static void (*ansi_637_trans_param_fcn[])(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset, gchar *add_string) = {
1447     trans_param_tele_id,        /* Teleservice Identifier */
1448     trans_param_srvc_cat,       /* Service Category */
1449     trans_param_address,        /* Originating Address */
1450     trans_param_subaddress,     /* Originating Subaddress */
1451     trans_param_address,        /* Destination Address */
1452     trans_param_subaddress,     /* Destination Subaddress */
1453     trans_param_bearer_reply_opt,       /* Bearer Reply Option */
1454     trans_param_cause_codes,    /* Cause Codes */
1455     trans_param_bearer_data,    /* Bearer Data */
1456     NULL,       /* NONE */
1457 };
1458
1459 #define NUM_TRANS_MSG_TYPE (sizeof(ansi_trans_msg_type_strings)/sizeof(value_string))
1460 static gint ett_ansi_637_trans_msg[NUM_TRANS_MSG_TYPE];
1461
1462 /* GENERIC IS-637 DISSECTOR FUNCTIONS */
1463
1464 static gboolean
1465 dissect_ansi_637_tele_param(tvbuff_t *tvb, proto_tree *tree, guint32 *offset)
1466 {
1467     void (*param_fcn)(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset) = NULL;
1468     guint8      oct;
1469     guint8      len;
1470     guint32     curr_offset;
1471     gint        ett_param_idx, idx;
1472     proto_tree  *subtree;
1473     proto_item  *item;
1474     const gchar *str = NULL;
1475
1476
1477     curr_offset = *offset;
1478
1479     oct = tvb_get_guint8(tvb, curr_offset);
1480     str = match_strval_idx((guint32) oct, ansi_tele_param_strings, &idx);
1481
1482     if (NULL == str)
1483     {
1484         return(FALSE);
1485     }
1486
1487     ett_param_idx = ett_ansi_637_tele_param[idx];
1488     param_fcn = ansi_637_tele_param_fcn[idx];
1489
1490     item =
1491         proto_tree_add_text(tree, tvb, curr_offset, -1, str);
1492
1493     subtree = proto_item_add_subtree(item, ett_param_idx);
1494
1495     proto_tree_add_uint(subtree, hf_ansi_637_tele_subparam_id,
1496         tvb, curr_offset, 1, oct);
1497
1498     curr_offset++;
1499
1500     len = tvb_get_guint8(tvb, curr_offset);
1501
1502     proto_item_set_len(item, (curr_offset - *offset) + len + 1);
1503
1504     proto_tree_add_uint(subtree, hf_ansi_637_length,
1505         tvb, curr_offset, 1, len);
1506
1507     curr_offset++;
1508
1509     if (len > 0)
1510     {
1511         if (param_fcn == NULL)
1512         {
1513             proto_tree_add_text(subtree, tvb, curr_offset,
1514                 len, "Parameter Data");
1515         }
1516         else
1517         {
1518             (*param_fcn)(tvb, subtree, len, curr_offset);
1519         }
1520
1521         curr_offset += len;
1522     }
1523
1524     *offset = curr_offset;
1525
1526     return(TRUE);
1527 }
1528
1529 static void
1530 dissect_ansi_637_tele_message(tvbuff_t *tvb, proto_tree *ansi_637_tree)
1531 {
1532     guint8      oct;
1533     guint8      len;
1534     guint32     octs;
1535     guint32     curr_offset;
1536     /* guint32  msg_id; */
1537     guint32     msg_type;
1538     const gchar *str = NULL;
1539     proto_item  *item;
1540     proto_tree  *subtree;
1541
1542
1543     oct = tvb_get_guint8(tvb, 0);
1544     if (oct != 0x00)
1545     {
1546         return;
1547     }
1548
1549     len = tvb_get_guint8(tvb, 1);
1550     if (len != 3)
1551     {
1552         return;
1553     }
1554
1555     octs = tvb_get_ntoh24(tvb, 2);
1556     msg_type = (octs >> 20) & 0x0f;
1557     /* msg_id = (octs >> 4) & 0xffff; */
1558
1559     str = match_strval(msg_type, ansi_tele_msg_type_strings);
1560
1561     /*
1562      * do not append to COL_INFO
1563      */
1564
1565     item =
1566         proto_tree_add_none_format(ansi_637_tree, hf_ansi_637_none,
1567             tvb, 0, -1, str);
1568
1569     subtree = proto_item_add_subtree(item, ett_params);
1570
1571     proto_tree_add_uint(subtree, hf_ansi_637_tele_subparam_id,
1572         tvb, 0, 1, oct);
1573
1574     proto_tree_add_uint(subtree, hf_ansi_637_length,
1575         tvb, 1, 1, len);
1576
1577     proto_tree_add_uint(subtree, hf_ansi_637_tele_msg_type,
1578         tvb, 2, 3, octs);
1579
1580     proto_tree_add_uint(subtree, hf_ansi_637_tele_msg_id,
1581         tvb, 2, 3, octs);
1582
1583     proto_tree_add_uint(subtree, hf_ansi_637_tele_msg_rsvd,
1584         tvb, 2, 3, octs);
1585
1586     proto_item_set_len(item, 2 + len);
1587
1588     curr_offset = 2 + len;
1589     len = tvb_length(tvb);
1590
1591     while ((len - curr_offset) > 0)
1592     {
1593         if (!dissect_ansi_637_tele_param(tvb, ansi_637_tree, &curr_offset))
1594         {
1595             proto_tree_add_text(ansi_637_tree, tvb, curr_offset, len - curr_offset,
1596                 "Unknown Parameter Data");
1597             break;
1598         }
1599     }
1600 }
1601
1602 static void
1603 dissect_ansi_637_tele(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1604 {
1605     proto_item  *ansi_637_item;
1606     proto_tree  *ansi_637_tree = NULL;
1607     const gchar *str = NULL;
1608
1609     if (check_col(pinfo->cinfo, COL_PROTOCOL))
1610     {
1611         col_set_str(pinfo->cinfo, COL_PROTOCOL, ansi_proto_name_short);
1612     }
1613
1614     /* In the interest of speed, if "tree" is NULL, don't do any work not
1615      * necessary to generate protocol tree items.
1616      */
1617     if (tree)
1618     {
1619         /*
1620          * create the ansi_637 protocol tree
1621          */
1622         str = match_strval(pinfo->match_port, ansi_tele_id_strings);
1623
1624         if (NULL == str) str = "Unrecognized Teleservice ID";
1625
1626         ansi_637_item =
1627             proto_tree_add_protocol_format(tree, proto_ansi_637_tele, tvb, 0, -1,
1628                 "%s - %s (%d)",
1629                 ansi_proto_name_tele,
1630                 str,
1631                 pinfo->match_port);
1632
1633         ansi_637_tree =
1634             proto_item_add_subtree(ansi_637_item, ett_ansi_637_tele);
1635
1636         dissect_ansi_637_tele_message(tvb, ansi_637_tree);
1637     }
1638 }
1639
1640 static gboolean
1641 dissect_ansi_637_trans_param(tvbuff_t *tvb, proto_tree *tree, guint32 *offset)
1642 {
1643     void (*param_fcn)(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset, gchar *add_string) = NULL;
1644     guint8      oct;
1645     guint8      len;
1646     guint32     curr_offset;
1647     gint        ett_param_idx, idx;
1648     proto_tree  *subtree;
1649     proto_item  *item;
1650     const gchar *str = NULL;
1651
1652     curr_offset = *offset;
1653
1654     oct = tvb_get_guint8(tvb, curr_offset);
1655     str = match_strval_idx((guint32) oct, ansi_trans_param_strings, &idx);
1656
1657     if (NULL == str)
1658     {
1659         return(FALSE);
1660     }
1661
1662     ett_param_idx = ett_ansi_637_trans_param[idx];
1663     param_fcn = ansi_637_trans_param_fcn[idx];
1664
1665     item =
1666         proto_tree_add_text(tree, tvb, curr_offset, -1, str);
1667
1668     subtree = proto_item_add_subtree(item, ett_param_idx);
1669
1670     proto_tree_add_uint(subtree, hf_ansi_637_trans_param_id,
1671         tvb, curr_offset, 1, oct);
1672
1673     curr_offset++;
1674
1675     len = tvb_get_guint8(tvb, curr_offset);
1676
1677     proto_item_set_len(item, (curr_offset - *offset) + len + 1);
1678
1679     proto_tree_add_uint(subtree, hf_ansi_637_length,
1680         tvb, curr_offset, 1, len);
1681
1682     curr_offset++;
1683
1684     if (len > 0)
1685     {
1686         if (param_fcn == NULL)
1687         {
1688             proto_tree_add_text(subtree, tvb, curr_offset,
1689                 len, "Parameter Data");
1690         }
1691         else
1692         {
1693             ansi_637_add_string[0] = '\0';
1694             (*param_fcn)(tvb, subtree, len, curr_offset, ansi_637_add_string);
1695
1696             if (ansi_637_add_string[0] != '\0')
1697             {
1698                 proto_item_append_text(item, "%s", ansi_637_add_string);
1699             }
1700         }
1701
1702         curr_offset += len;
1703     }
1704
1705     *offset = curr_offset;
1706
1707     return(TRUE);
1708 }
1709
1710 static void
1711 dissect_ansi_637_trans(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1712 {
1713     proto_item  *ansi_637_item;
1714     proto_tree  *ansi_637_tree = NULL;
1715     guint32     curr_offset;
1716     gint        idx;
1717     const gchar *str = NULL;
1718     guint8      oct;
1719     guint8      len;
1720
1721     if (check_col(pinfo->cinfo, COL_PROTOCOL))
1722     {
1723         col_set_str(pinfo->cinfo, COL_PROTOCOL, ansi_proto_name_short);
1724     }
1725
1726     /* In the interest of speed, if "tree" is NULL, don't do any work not
1727      * necessary to generate protocol tree items.
1728      */
1729     if (tree)
1730     {
1731         g_pinfo = pinfo;
1732         g_tree = tree;
1733
1734         /*
1735          * create the ansi_637 protocol tree
1736          */
1737         oct = tvb_get_guint8(tvb, 0);
1738
1739         str = match_strval_idx(oct, ansi_trans_msg_type_strings, &idx);
1740
1741         if (NULL == str)
1742         {
1743             ansi_637_item =
1744                 proto_tree_add_protocol_format(tree, proto_ansi_637_trans, tvb, 0, -1,
1745                     "%s - Unrecognized Transport Layer Message Type (%d)",
1746                     ansi_proto_name_trans,
1747                     oct);
1748
1749             ansi_637_tree =
1750                 proto_item_add_subtree(ansi_637_item, ett_ansi_637_trans);
1751         }
1752         else
1753         {
1754             ansi_637_item =
1755                 proto_tree_add_protocol_format(tree, proto_ansi_637_trans, tvb, 0, -1,
1756                     "%s - %s",
1757                     ansi_proto_name_trans,
1758                     str);
1759
1760             ansi_637_tree =
1761                 proto_item_add_subtree(ansi_637_item, ett_ansi_637_trans_msg[idx]);
1762         }
1763
1764         curr_offset = 1;
1765
1766         len = tvb_length(tvb);
1767
1768         while ((len - curr_offset) > 0)
1769         {
1770             if (!dissect_ansi_637_trans_param(tvb, ansi_637_tree, &curr_offset))
1771             {
1772                 proto_tree_add_text(ansi_637_tree, tvb, curr_offset, len - curr_offset,
1773                     "Unknown Parameter Data");
1774                 break;
1775             }
1776         }
1777     }
1778 }
1779
1780 /* Register the protocol with Ethereal */
1781 void
1782 proto_register_ansi_637(void)
1783 {
1784     guint               i;
1785
1786     /* Setup list of header fields */
1787     static hf_register_info hf[] =
1788     {
1789         { &hf_ansi_637_trans_msg_type,
1790           { "Message Type",
1791             "ansi_637.trans_msg_type",
1792             FT_UINT24, BASE_DEC, VALS(ansi_trans_msg_type_strings), 0xf00000,
1793             "", HFILL }},
1794         { &hf_ansi_637_tele_msg_type,
1795           { "Message Type",
1796             "ansi_637.tele_msg_type",
1797             FT_UINT24, BASE_DEC, VALS(ansi_tele_msg_type_strings), 0xf00000,
1798             "", HFILL }},
1799         { &hf_ansi_637_tele_msg_id,
1800           { "Message ID",
1801             "ansi_637.tele_msg_id",
1802             FT_UINT24, BASE_DEC, NULL, 0x0ffff0,
1803             "", HFILL }},
1804         { &hf_ansi_637_tele_msg_rsvd,
1805           { "Reserved",
1806             "ansi_637.tele_msg_rsvd",
1807             FT_UINT24, BASE_DEC, NULL, 0x00000f,
1808             "", HFILL }},
1809         { &hf_ansi_637_length,
1810             { "Length",         "ansi_637.len",
1811             FT_UINT8, BASE_DEC, NULL, 0,
1812             "", HFILL }
1813         },
1814         { &hf_ansi_637_none,
1815             { "Sub tree",       "ansi_637.none",
1816             FT_NONE, 0, 0, 0,
1817             "", HFILL }
1818         },
1819         { &hf_ansi_637_tele_subparam_id,
1820             { "Teleservice Subparam ID",        "ansi_637.tele_subparam_id",
1821             FT_UINT8, BASE_DEC, VALS(ansi_tele_param_strings), 0,
1822             "", HFILL }
1823         },
1824         { &hf_ansi_637_trans_param_id,
1825             { "Transport Param ID",     "ansi_637.trans_param_id",
1826             FT_UINT8, BASE_DEC, VALS(ansi_trans_param_strings), 0,
1827             "", HFILL }
1828         },
1829         { &hf_ansi_637_bin_addr,
1830             { "Binary Address", "ansi_637.bin_addr",
1831             FT_BYTES, BASE_HEX, 0, 0,
1832             "", HFILL }
1833         },
1834     };
1835
1836     /* Setup protocol subtree array */
1837 #define NUM_INDIVIDUAL_PARAMS   3
1838     static gint *ett[NUM_INDIVIDUAL_PARAMS+NUM_TELE_PARAM+NUM_TRANS_MSG_TYPE+NUM_TRANS_PARAM];
1839
1840     memset((void *) ett, 0, sizeof(ett));
1841
1842     ett[0] = &ett_ansi_637_tele;
1843     ett[1] = &ett_ansi_637_trans;
1844     ett[2] = &ett_params;
1845
1846     for (i=0; i < NUM_TELE_PARAM; i++)
1847     {
1848         ett_ansi_637_tele_param[i] = -1;
1849         ett[NUM_INDIVIDUAL_PARAMS+i] = &ett_ansi_637_tele_param[i];
1850     }
1851
1852     for (i=0; i < NUM_TRANS_MSG_TYPE; i++)
1853     {
1854         ett_ansi_637_trans_msg[i] = -1;
1855         ett[NUM_INDIVIDUAL_PARAMS+NUM_TELE_PARAM+i] = &ett_ansi_637_trans_msg[i];
1856     }
1857
1858     for (i=0; i < NUM_TRANS_PARAM; i++)
1859     {
1860         ett_ansi_637_trans_param[i] = -1;
1861         ett[NUM_INDIVIDUAL_PARAMS+NUM_TELE_PARAM+NUM_TRANS_MSG_TYPE+i] = &ett_ansi_637_trans_param[i];
1862     }
1863
1864     /* Register the protocol name and description */
1865     proto_ansi_637_tele =
1866         proto_register_protocol(ansi_proto_name_tele, "ANSI IS-637-A Teleservice", "ansi_637_tele");
1867
1868     proto_ansi_637_trans =
1869         proto_register_protocol(ansi_proto_name_trans, "ANSI IS-637-A Transport", "ansi_637_trans");
1870
1871     /* Required function calls to register the header fields and subtrees used */
1872     proto_register_field_array(proto_ansi_637_tele, hf, array_length(hf));
1873     proto_register_subtree_array(ett, array_length(ett));
1874
1875     tele_dissector_table =
1876         register_dissector_table("ansi_637.tele_id",
1877             "ANSI IS-637-A Teleservice ID", FT_UINT8, BASE_DEC);
1878 }
1879
1880
1881 void
1882 proto_reg_handoff_ansi_637(void)
1883 {
1884     dissector_handle_t  ansi_637_tele_handle;
1885     dissector_handle_t  ansi_637_trans_handle;
1886     guint               i;
1887
1888     ansi_637_tele_handle = create_dissector_handle(dissect_ansi_637_tele, proto_ansi_637_tele);
1889     ansi_637_trans_handle = create_dissector_handle(dissect_ansi_637_trans, proto_ansi_637_trans);
1890
1891     /*
1892      * register for all known teleservices
1893      * '-1' is to stop before trailing '0' entry
1894      *
1895      * to add teleservices, modify 'ansi_tele_id_strings'
1896      */
1897     for (i=0; i < ((sizeof(ansi_tele_id_strings)/sizeof(value_string))-1); i++)
1898     {
1899         /*
1900          * ANSI MAP dissector will push out teleservice ids
1901          */
1902         dissector_add("ansi_map.tele_id", ansi_tele_id_strings[i].value, ansi_637_tele_handle);
1903
1904         /*
1905          * we will push out teleservice ids after Transport layer decode
1906          */
1907         dissector_add("ansi_637.tele_id", ansi_tele_id_strings[i].value, ansi_637_tele_handle);
1908     }
1909
1910     /*
1911      * ANSI A-interface will push out transport layer data
1912      */
1913     dissector_add("ansi_a.sms", 0, ansi_637_trans_handle);
1914
1915     /* data_handle = find_dissector("data"); */
1916 }