fundamental_type.c 5.12 KB
Newer Older
1
2
3
4
5
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
Cedric Roux's avatar
Cedric Roux committed
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

Cedric Roux's avatar
 
Cedric Roux committed
22
23
24
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
25
#include <inttypes.h>
Cedric Roux's avatar
 
Cedric Roux committed
26

27
28
#define G_LOG_DOMAIN ("PARSER")

Cedric Roux's avatar
 
Cedric Roux committed
29
30
31
#include "fundamental_type.h"
#include "ui_interface.h"

winckel's avatar
winckel committed
32
uint64_t fundamental_read_from_buffer(types_t *type, buffer_t *buffer, uint32_t offset, uint32_t parent_offset) {
33
    uint64_t value;
Cedric Roux's avatar
 
Cedric Roux committed
34
35

    switch (type->size) {
36
        case 8:
Cedric Roux's avatar
 
Cedric Roux committed
37
38
            value = buffer_get_uint8_t (buffer, offset + parent_offset);
            break;
39
40

        case 16:
Cedric Roux's avatar
 
Cedric Roux committed
41
42
            value = buffer_get_uint16_t (buffer, offset + parent_offset);
            break;
43
44

        case 32:
Cedric Roux's avatar
 
Cedric Roux committed
45
46
            value = buffer_get_uint32_t (buffer, offset + parent_offset);
            break;
47
48
49
50
51

        case 64:
            value = buffer_get_uint64_t (buffer, offset + parent_offset);
            break;

Cedric Roux's avatar
 
Cedric Roux committed
52
53
54
55
56
57
58
59
        default:
            /* ??? */
            value = 0;
            break;
    }
    return value;
}

Cedric Roux's avatar
Cedric Roux committed
60
int fundamental_dissect_from_buffer(
winckel's avatar
winckel committed
61
62
    types_t *type, ui_set_signal_text_cb_t ui_set_signal_text_cb, gpointer user_data,
    buffer_t *buffer, uint32_t offset, uint32_t parent_offset, int indent, gboolean new_line)
Cedric Roux's avatar
Cedric Roux committed
63
64
65
66
{
    int         length = 0;
    char        cbuf[200];
    int         type_unsigned;
67
68
69
70
    uint8_t     value8;
    uint16_t    value16;
    uint32_t    value32;
    uint64_t    value64;
Cedric Roux's avatar
 
Cedric Roux committed
71
72
73
74
75
76
77

    DISPLAY_PARSE_INFO("fundamental", type->name, offset, parent_offset);

    memset (cbuf, 0, 200);

    type_unsigned = strstr (type->name, "unsigned") == NULL ? 0 : 1;

78
    value64 = fundamental_read_from_buffer(type, buffer, offset, parent_offset);
Cedric Roux's avatar
 
Cedric Roux committed
79

winckel's avatar
winckel committed
80
    indent = new_line ? indent : 0;
Cedric Roux's avatar
 
Cedric Roux committed
81
82
83
84
85
    if (indent > 0)
    {
        DISPLAY_TYPE("Fun");
    }
    switch (type->size) {
86
87
        case 8:
            value8 = (uint8_t) value64;
Cedric Roux's avatar
 
Cedric Roux committed
88
            INDENTED_STRING(cbuf, indent,
winckel's avatar
winckel committed
89
                            length = sprintf(cbuf, type_unsigned ? "(0x%02" PRIx8 ") %3" PRIu8 ";\n" : "(0x%02" PRIx8 ") %4" PRId8 ";\n", value8, value8));
Cedric Roux's avatar
 
Cedric Roux committed
90
            break;
91
92
93

        case 16:
            value16 = (uint16_t) value64;
Cedric Roux's avatar
 
Cedric Roux committed
94
            INDENTED_STRING(cbuf, indent,
winckel's avatar
winckel committed
95
                            length = sprintf(cbuf, type_unsigned ? "(0x%04" PRIx16 ") %5" PRIu16 ";\n" : "(0x%04" PRIx16 ") %6" PRId16 ";\n", value16, value16));
Cedric Roux's avatar
 
Cedric Roux committed
96
            break;
97
98
99

        case 32:
            value32 = (uint32_t) value64;
Cedric Roux's avatar
 
Cedric Roux committed
100
            INDENTED_STRING(cbuf, indent,
winckel's avatar
winckel committed
101
                            length = sprintf(cbuf, type_unsigned ? "(0x%08" PRIx32 ") %9" PRIu32 ";\n" : "(0x%08" PRIx32 ") %10" PRId32 ";\n", value32, value32));
Cedric Roux's avatar
 
Cedric Roux committed
102
            break;
103
104
105

        case 64:
            INDENTED_STRING(cbuf, indent,
winckel's avatar
winckel committed
106
                            length = sprintf(cbuf, type_unsigned ? "(0x%016" PRIx64 ") %20" PRIu64 ";\n" : "(0x%016" PRIx64 ") %20" PRId64 ";\n", value64, value64));
107
108
            break;

Cedric Roux's avatar
 
Cedric Roux committed
109
110
111
112
        default:
            /* ??? */
            break;
    }
Cedric Roux's avatar
Cedric Roux committed
113
    ui_set_signal_text_cb(user_data, cbuf, length);
Cedric Roux's avatar
 
Cedric Roux committed
114
115
116
117

    return 0;
}

winckel's avatar
winckel committed
118
int fundamental_type_file_print(types_t *type, int indent, FILE *file) {
Cedric Roux's avatar
 
Cedric Roux committed
119
120
121
122
123
124
125
126
127
128
129
130
131
    if (type == NULL)
        return -1;
    INDENTED(file, indent, fprintf(file, "<Fundamental>\n"));
    INDENTED(file, indent+4, fprintf(file, "Name .......: %s\n", type->name));
    INDENTED(file, indent+4, fprintf(file, "Id .........: %d\n", type->id));
    INDENTED(file, indent+4, fprintf(file, "Size .......: %d\n", type->size));
    INDENTED(file, indent+4, fprintf(file, "Align ......: %d\n", type->align));
    if (type->file_ref != NULL)
        type->file_ref->type_file_print (type->file_ref, indent + 4, file);
    INDENTED(file, indent, fprintf(file, "</Fundamental>\n"));
    return 0;
}

winckel's avatar
winckel committed
132
int fundamental_type_hr_display(types_t *type, int indent) {
Cedric Roux's avatar
 
Cedric Roux committed
133
134
135
136
137
138
139
140
141
142
143
144
    if (type == NULL)
        return -1;
    INDENTED(stdout, indent, printf("<Fundamental>\n"));
    INDENTED(stdout, indent+4, printf("Name .......: %s\n", type->name));
    INDENTED(stdout, indent+4, printf("Id .........: %d\n", type->id));
    INDENTED(stdout, indent+4, printf("Size .......: %d\n", type->size));
    INDENTED(stdout, indent+4, printf("Align ......: %d\n", type->align));
    if (type->file_ref != NULL)
        type->file_ref->type_hr_display (type->file_ref, indent + 4);
    INDENTED(stdout, indent, printf("</Fundamental>\n"));
    return 0;
}