locate_root.c 6.73 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
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 <string.h>

25 26
#define G_LOG_DOMAIN ("RESOLVER")

27 28
#include <glib.h>

29
#include "logs.h"
30
#include "rc.h"
Cedric Roux's avatar
 
Cedric Roux committed
31 32

#include "types.h"
Cedric Roux's avatar
Cedric Roux committed
33
#include "enum_type.h"
Cedric Roux's avatar
 
Cedric Roux committed
34
#include "locate_root.h"
Cedric Roux's avatar
Cedric Roux committed
35
#include "xml_parse.h"
Cedric Roux's avatar
 
Cedric Roux committed
36

37 38 39 40 41 42 43 44 45 46
types_t *messages_id_enum;
types_t *lte_time_type;
types_t *lte_time_frame_type;
types_t *lte_time_slot_type;
types_t *origin_task_id_type;
types_t *destination_task_id_type;
types_t *instance_type;
types_t *message_header_type;
types_t *message_type;
types_t *message_size_type;
Cedric Roux's avatar
Cedric Roux committed
47 48

int locate_root(const char *root_name, types_t *head, types_t **root_elm) {
Cedric Roux's avatar
 
Cedric Roux committed
49
    types_t *next_type;
50
    int next_counter = 0;
Cedric Roux's avatar
 
Cedric Roux committed
51 52 53 54 55

    /* The root element is for example : MessageDef.
     * This element is the entry for other sub-types.
     */
    if (!root_name || (strlen (root_name) == 0)) {
56
        g_warning("FATAL: no root element name provided");
Cedric Roux's avatar
 
Cedric Roux committed
57 58 59
        return -1;
    }
    if (!head) {
60
        g_warning("Empty list detected");
Cedric Roux's avatar
 
Cedric Roux committed
61 62
        return -1;
    }
Cedric Roux's avatar
Cedric Roux committed
63
    if (!root_elm) {
64
        g_warning("NULL root reference");
Cedric Roux's avatar
 
Cedric Roux committed
65 66 67 68 69 70 71 72 73 74
        return -1;
    }

    for (next_type = head; next_type; next_type = next_type->next) {
        if (next_type->name == NULL)
            continue;
        if (strcmp (root_name, next_type->name) == 0) {
            /* Matching reference */
            break;
        }
75
        next_counter ++;
Cedric Roux's avatar
 
Cedric Roux committed
76
    }
77 78
    g_info("locate_root: %s %d", root_name, next_counter);

Cedric Roux's avatar
Cedric Roux committed
79
    *root_elm = next_type;
Cedric Roux's avatar
 
Cedric Roux committed
80 81 82 83 84
    return (next_type == NULL) ? -2 : 0;
}

int locate_type(const char *type_name, types_t *head, types_t **type) {
    types_t *next_type;
85
    int next_counter = 0;
Cedric Roux's avatar
 
Cedric Roux committed
86 87 88 89 90

    /* The root element is for example : MessageDef.
     * This element is the entry for other sub-types.
     */
    if (!type_name) {
Cedric Roux's avatar
Cedric Roux committed
91
        g_warning("FATAL: no element name provided");
Cedric Roux's avatar
 
Cedric Roux committed
92 93 94
        return RC_BAD_PARAM;
    }
    if (!head) {
95
        g_warning("Empty list detected");
Cedric Roux's avatar
 
Cedric Roux committed
96 97 98 99 100 101 102 103 104 105
        return RC_BAD_PARAM;
    }

    for (next_type = head; next_type; next_type = next_type->next) {
        if (next_type->name == NULL)
            continue;
        if (strcmp (type_name, next_type->name) == 0) {
            /* Matching reference */
            break;
        }
106
        next_counter ++;
Cedric Roux's avatar
 
Cedric Roux committed
107
    }
108
    g_info("locate_type: %s %d %p", type_name, next_counter, next_type);
109

Cedric Roux's avatar
 
Cedric Roux committed
110 111 112 113 114
    if (type)
        *type = next_type;
    return (next_type == NULL) ? RC_FAIL : RC_OK;
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
int locate_type_children(const char *type_name, types_t *head, types_t **type) {
    types_t *found_type = NULL;
    int i;

    /* The root element is for example : MessageDef.
     * This element is the entry for other sub-types.
     */
    if (!type_name) {
        g_warning("FATAL: no element name provided");
        return RC_BAD_PARAM;
    }
    if (!head) {
        g_warning("Empty list detected");
        return RC_BAD_PARAM;
    }

    for (i = 0; i < head->nb_members; i++) {
        if (head->members_child[i]->name == NULL)
            continue;
        if (strcmp (type_name, head->members_child[i]->name) == 0) {
            /* Matching reference */
            found_type = head->members_child[i];
            break;
        }
    }
    g_info("locate_type: %s %d %p", type_name, i, found_type);

    if (type)
        *type = found_type;
    return (found_type == NULL) ? RC_FAIL : RC_OK;
}

Cedric Roux's avatar
Cedric Roux committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
uint32_t get_message_header_type_size(void)
{
    /* Typedef */
    if (message_header_type->child != NULL) {
        /* Struct */
        if (message_header_type->child->child != NULL) {
            return message_header_type->child->child->size;
        }
    }
    return 0;
}

uint32_t get_message_size(buffer_t *buffer)
{
    uint32_t value = 0;

    if (message_size_type != NULL)
    {
        types_t *temp = message_size_type;

        while (temp->size == -1) {
            temp = temp->child;
        }

        /* Fetch instance value */
        buffer_fetch_bits (buffer, message_size_type->offset, temp->size, &value);
    }

    return value;
}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
uint32_t get_lte_frame(buffer_t *buffer) {
    uint32_t  value = 0;

    if (lte_time_type !=NULL)
    {
        /* Fetch instance value */
        buffer_fetch_bits (buffer, lte_time_type->offset + lte_time_frame_type->offset, lte_time_frame_type->child->child->size, &value);
    }

    return value;
}

uint32_t get_lte_slot(buffer_t *buffer) {
    uint32_t  value = 0;

    if (lte_time_type !=NULL)
    {
        /* Fetch instance value */
        buffer_fetch_bits (buffer, lte_time_type->offset + lte_time_slot_type->offset, lte_time_slot_type->child->child->size, &value);
    }

    return value;
}

202
uint32_t get_message_id(types_t *head, buffer_t *buffer, uint32_t *message_id) {
Cedric Roux's avatar
 
Cedric Roux committed
203 204
    uint32_t value;

Cedric Roux's avatar
Cedric Roux committed
205 206
    g_assert(message_id != NULL);
    g_assert(buffer != NULL);
Cedric Roux's avatar
 
Cedric Roux committed
207 208

    /* MessageId is an offset from start of buffer */
Cedric Roux's avatar
Cedric Roux committed
209
    value = buffer_get_uint32_t(buffer, messages_id_enum->offset);
Cedric Roux's avatar
 
Cedric Roux committed
210 211 212 213

    *message_id = value;
    return RC_OK;
}
Cedric Roux's avatar
Cedric Roux committed
214

215 216
uint32_t get_task_id(buffer_t *buffer, types_t *task_id_type) {
    uint32_t task_id_value;
Cedric Roux's avatar
Cedric Roux committed
217 218

    /* Fetch task id value */
219 220 221
    if (buffer_fetch_bits (buffer, task_id_type->offset, task_id_type->child->size, &task_id_value) != RC_OK)
    {
        return ~0;
Cedric Roux's avatar
Cedric Roux committed
222 223
    }

224
    return task_id_value;
Cedric Roux's avatar
Cedric Roux committed
225 226
}

227 228
char *task_id_to_string(uint32_t task_id_value, types_t *task_id_type) {
    char *task_id = "UNKNOWN";
Cedric Roux's avatar
Cedric Roux committed
229

230 231
    if (task_id_value < ((uint32_t) ~0))
    {
winckel's avatar
winckel committed
232
        /* Search task id name */
233
        task_id = enum_type_get_name_from_value (task_id_type->child, task_id_value);
Cedric Roux's avatar
Cedric Roux committed
234 235
    }

236
    return task_id;
Cedric Roux's avatar
Cedric Roux committed
237 238
}

winckel's avatar
winckel committed
239 240 241 242 243 244 245 246 247
uint32_t get_instance(buffer_t *buffer) {
    uint32_t  instance_value;

    /* Fetch instance value */
    buffer_fetch_bits (buffer, instance_type->offset, instance_type->child->child->child->size, &instance_value);

    return instance_value;
}

Cedric Roux's avatar
Cedric Roux committed
248 249 250 251
char *message_id_to_string(uint32_t message_id)
{
    return enum_type_get_name_from_value(messages_id_enum, message_id);
}