summaryrefslogtreecommitdiff
path: root/cesar/cp/src/trace.c
blob: 538aaa1955cf048dc68996a8195bb9f00251d6cc (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/src/trace.c
 * \brief   Define CP trace events.
 * \ingroup cp
 */
#include "common/std.h"

#include <string.h>
#include <stdio.h>

#include "cp/inc/context.h"
#include "cp/fsm/inc/tables.h"
#include "cp/msg/inc/allowed_mme.h"
#include "ce/tx/mme.h"
#include "mac/common/tonemap.h"

/**
 * Table to associate an MME error code and its string translation.
 */
const char *cp_trace_ce_tx_mme_error_code_table[] =
{
    "CE_TX_MME_OK",
    "CE_TX_MME_BROADCAST",
    "CE_TX_MME_UNKNOWN_STA",
    "CE_TX_MME_HEADER",
    "CE_TX_MME_TMI_LIST",
    "CE_TX_MME_DEFAULT_TMI_DISABLED",
    "CE_TX_MME_DEFAULT_TMI",
    "CE_TX_MME_INTERVAL",
    "CE_TX_MME_NEW_TMI",
    "CE_TX_MME_NEW_TMI_DISABLED",
    "CE_TX_MME_BAD_TM",
    "CE_TX_MME_NEW_TMI_ROBO",
    "CE_TX_MME_TOO_SHORT",
    "CE_TX_MME_OLD_TMI",
    "CE_TX_MME_OLD_TMI_NOT_FOUND",
    "UNKNOWN",
};

/**
 * Table to associate an MME dispatch error code and its string translation.
 */
const char *cp_trace_rx_mme_error_code_table[] =
{
    "OK",
    "NOT_HANDLED",
    "PHY_NOT_ALLOWED",
    "H1_NOT_ALLOWED",
    "STA_STATUS_NOK",
    "SPC_OUI_NOT_SPIDCOM",
    "BAD_ENCRYPTION",
    "UNKNOWN"
};


/**
 * Format a FSM state.
 * \see trace_format_t.
 */
static int
cp_trace_format_fsm_state (char *text, uint text_size, int data)
{
    const char *state_name;
    if (data < 0 || data >= CP_FSM_STATE_NB)
        state_name = "UNKNOWN";
    else
        state_name = cp_fsm_state_names_table[data];
    uint state_name_len = strlen (state_name);
    if (state_name_len > text_size)
        return -1;
    else
    {
        memcpy (text, state_name, state_name_len);
        return state_name_len;
    }
}

/**
 * Format a FSM event.
 * \see trace_format_t.
 */
static int
cp_trace_format_fsm_event (char *text, uint text_size, int data)
{
    const char *event_name;
    if (data < 0 || data >= CP_FSM_EVENT_TYPE_NB)
        event_name = "UNKNOWN";
    else
        event_name = cp_fsm_event_names_table[data];
    uint event_name_len = strlen (event_name);
    if (event_name_len > text_size)
        return -1;
    else
    {
        memcpy (text, event_name, event_name_len);
        return event_name_len;
    }
}

/**
 * Format an MMType.
 * \see trace_format_t.
 */
static int
cp_trace_format_mmtype (char *text, uint text_size, int data)
{
    const char *mmtype_name = "UNKNOWN";

    /* Try to find the MMType. */
    uint i;
    mmtype_t mmtype = data;
    DICHOTOMY_SEARCH (0, cp_msg_mme_allowed_count, i,
                      mmtype <= cp_msg_mme_allowed[i].mmtype);

    /* If found. */
    if ((i < cp_msg_mme_allowed_count)
        && (mmtype == cp_msg_mme_allowed[i].mmtype))
        mmtype_name = cp_msg_mme_allowed[i].mmtype_name;

    /* Copy if we can. */
    uint mmtype_name_len = strlen (mmtype_name);
    if (mmtype_name_len > text_size)
        return -1;
    else
    {
        memcpy (text, mmtype_name, mmtype_name_len);
        return mmtype_name_len;
    }
}

/**
 * Copy an error into trace buffer.
 * \param  error_name  the error to trace.
 * \return  error_name length on success, -1 otherwise.
 */
static int
cp_trace_format_copy_error (char *text, uint text_size,
                            const char *error_name)
{
    /* Copy if we can. */
    uint error_name_len = strlen (error_name);
    if (error_name_len > text_size)
        return -1;
    else
    {
        memcpy (text, error_name, error_name_len);
        return error_name_len;
    }
}

/**
 * Format an error of CE MME.
 * \see trace_format_t.
 */
static int
cp_trace_format_ce_mme_error (char *text, uint text_size, int data)
{
    const char *error_name;

    /* Handle unknown error. */
    if (data > CE_TX_MME_NB || data < 0)
        data = CE_TX_MME_NB;

    /* Sanity check. */
    dbg_assert (COUNT (cp_trace_ce_tx_mme_error_code_table)
                == CE_TX_MME_NB + 1);
    dbg_assert ((uint) data <= COUNT (cp_trace_ce_tx_mme_error_code_table));
    /* Get error message. */
    error_name = cp_trace_ce_tx_mme_error_code_table[data];
    return cp_trace_format_copy_error (text, text_size, error_name);
}

/**
 * Format list of TMIs enabled.
 * \see trace_format_t.
 */
static int
cp_trace_format_tm_enabled_list (char *text, uint text_size, int data)
{
#define STRING_LEN 255
    char tmi_enabled_list[STRING_LEN] = "";
    static const char sep_empty[] = "";
    static const char sep[] = ", ";
    const char *s = sep_empty;
    uint i;

    for (i = TONEMAP_INDEX_NEGOTIATED_FIRST; i < TONEMAP_INDEX_NB; i++)
    {
        /* Tone map index enabled? */
        if (data & (1 << i))
        {
            /* Build 'separator and tmi' string. */
            char tmp[STRING_LEN];
            snprintf (tmp, STRING_LEN, "%s%i", s, i);
            /* Add it to main string. */
            strcat (tmi_enabled_list, tmp);
            /* Update separator. */
            s = sep;
        }
    }
    return cp_trace_format_copy_error (text, text_size, tmi_enabled_list);
#undef STRING_LEN
}

/**
 * Format an dispatch error of cp_msg_dispatch.
 * \see trace_format_t
 */
static int
cp_trace_format_msg_dispatch_error (char *text, uint text_size, int data)
{
    const char *error_name;
    /* Sanity check. */
    dbg_assert ((uint) data <= COUNT (cp_trace_rx_mme_error_code_table));
    /* Get error message. */
    error_name = cp_trace_rx_mme_error_code_table[data];
    return cp_trace_format_copy_error (text, text_size, error_name);
}

void
cp_trace_common_init (cp_t *ctx, trace_namespace_t *ns,
                      const trace_event_id_t *event_ids,
                      const uint event_ids_nb)
{
    dbg_assert (ctx);
    dbg_assert (ns);
    dbg_assert (event_ids);

    trace_namespace_init (ns, event_ids, event_ids_nb);
    trace_namespace_register_format (ns, 'S',
                                     cp_trace_format_fsm_state);
    trace_namespace_register_format (ns, 'E',
                                     cp_trace_format_fsm_event);
    trace_namespace_register_format (ns, 'M',
                                     cp_trace_format_mmtype);
    trace_namespace_register_format (ns, 'Z',
                                     cp_trace_format_ce_mme_error);
    trace_namespace_register_format (ns, 'T',
                                     cp_trace_format_tm_enabled_list);
    trace_namespace_register_format (ns, 'Y',
                                     cp_trace_format_msg_dispatch_error);
    trace_buffer_add (&ctx->trace, "cp", 8, 1, false, ns);
    trace_buffer_add (&ctx->trace_verbose, "cp-verbose", 8, 8, true, ns);
}

void
cp_trace_uninit (cp_t *ctx)
{
    dbg_assert (ctx);
    trace_buffer_remove (&ctx->trace_verbose);
    trace_buffer_remove (&ctx->trace);
}