aboutsummaryrefslogtreecommitdiff
path: root/libqcdm/src/logs.c
blob: b024b966a21daf1fb492165d6bda5ff13c40a6d2 (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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * Copyright (C) 2010 Red Hat, Inc.
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>
#include <stdlib.h>
#include <endian.h>

#include "log-items.h"
#include "logs.h"
#include "errors.h"
#include "dm-commands.h"
#include "result-private.h"
#include "utils.h"


/**********************************************************************/

static qcdmbool
check_log_item (const char *buf, size_t len, uint16_t log_code, size_t min_len, int *out_error)
{
    DMCmdLog *log_cmd = (DMCmdLog *) buf;

    if (len < sizeof (DMCmdLog)) {
        qcdm_err (0, "DM log item malformed (must be at least %zu bytes in length)", sizeof (DMCmdLog));
        if (out_error)
            *out_error = -QCDM_ERROR_RESPONSE_MALFORMED;
        return FALSE;
    }

    if (buf[0] != DIAG_CMD_LOG) {
        if (out_error)
            *out_error = -QCDM_ERROR_RESPONSE_UNEXPECTED;
        return FALSE;
    }

    if (le16toh (log_cmd->log_code) != log_code) {
        if (out_error)
            *out_error = -QCDM_ERROR_RESPONSE_UNEXPECTED;
        return FALSE;
    }

    if (len < sizeof (DMCmdLog) + min_len) {
        qcdm_err (0, "DM log item response not long enough (got %zu, expected "
                  "at least %zu).", len, sizeof (DMCmdLog) + min_len);
        if (out_error)
            *out_error = -QCDM_ERROR_RESPONSE_BAD_LENGTH;
        return FALSE;
    }

    return TRUE;
}

/**********************************************************************/

#define PILOT_SETS_LOG_ACTIVE_SET    "active-set"
#define PILOT_SETS_LOG_CANDIDATE_SET "candidate-set"
#define PILOT_SETS_LOG_REMAINING_SET  "remaining-set"

static const char *
set_num_to_str (uint32_t num)
{
    if (num == QCDM_LOG_ITEM_EVDO_PILOT_SETS_V2_TYPE_ACTIVE)
        return PILOT_SETS_LOG_ACTIVE_SET;
    if (num == QCDM_LOG_ITEM_EVDO_PILOT_SETS_V2_TYPE_CANDIDATE)
        return PILOT_SETS_LOG_CANDIDATE_SET;
    if (num == QCDM_LOG_ITEM_EVDO_PILOT_SETS_V2_TYPE_REMAINING)
        return PILOT_SETS_LOG_REMAINING_SET;
    return NULL;
}

QcdmResult *
qcdm_log_item_evdo_pilot_sets_v2_new (const char *buf, size_t len, int *out_error)
{
    QcdmResult *result = NULL;
    DMLogItemEvdoPilotSetsV2 *pilot_sets;
    DMCmdLog *log_cmd = (DMCmdLog *) buf;
    size_t sets_len;

    qcdm_return_val_if_fail (buf != NULL, NULL);

    if (!check_log_item (buf, len, DM_LOG_ITEM_EVDO_PILOT_SETS_V2, sizeof (DMLogItemEvdoPilotSetsV2), out_error))
        return NULL;

    pilot_sets = (DMLogItemEvdoPilotSetsV2 *) log_cmd->data;

    result = qcdm_result_new ();

    sets_len = pilot_sets->active_count * sizeof (DMLogItemEvdoPilotSetsV2Pilot);
    if (sets_len > 0) {
        qcdm_result_add_u8_array (result,
                                  PILOT_SETS_LOG_ACTIVE_SET,
                                  (const uint8_t *) &pilot_sets->sets[0],
                                  sets_len);
    }

    sets_len = pilot_sets->candidate_count * sizeof (DMLogItemEvdoPilotSetsV2Pilot);
    if (sets_len > 0) {
        qcdm_result_add_u8_array (result,
                                  PILOT_SETS_LOG_CANDIDATE_SET,
                                  (const uint8_t *) &pilot_sets->sets[pilot_sets->active_count],
                                  sets_len);
    }

    sets_len = pilot_sets->remaining_count * sizeof (DMLogItemEvdoPilotSetsV2Pilot);
    if (sets_len > 0) {
        qcdm_result_add_u8_array (result,
                                  PILOT_SETS_LOG_REMAINING_SET,
                                  (const uint8_t *) &pilot_sets->sets[pilot_sets->active_count + pilot_sets->candidate_count],
                                  sets_len);
    }

    return result;

}

qcdmbool
qcdm_log_item_evdo_pilot_sets_v2_get_num (QcdmResult *result,
                                          uint32_t set_type,
                                          uint32_t *out_num)
{
    const char *set_name;
    const uint8_t *array = NULL;
    size_t array_len = 0;

    qcdm_return_val_if_fail (result != NULL, FALSE);

    set_name = set_num_to_str (set_type);
    qcdm_return_val_if_fail (set_name != NULL, FALSE);

    if (qcdm_result_get_u8_array (result, set_name, &array, &array_len))
        return FALSE;

    *out_num = array_len / sizeof (DMLogItemEvdoPilotSetsV2Pilot);
    return TRUE;
}

#define MAX(a, b)  (((a) > (b)) ? (a) : (b))

qcdmbool
qcdm_log_item_evdo_pilot_sets_v2_get_pilot (QcdmResult *result,
                                            uint32_t set_type,
                                            uint32_t num,
                                            uint32_t *out_pilot_pn,
                                            uint32_t *out_pilot_energy,
                                            int32_t *out_rssi_dbm)
{
    const char *set_name;
    DMLogItemEvdoPilotSetsV2Pilot *pilot;
    const uint8_t *array = NULL;
    size_t array_len = 0;

    qcdm_return_val_if_fail (result != NULL, FALSE);

    set_name = set_num_to_str (set_type);
    qcdm_return_val_if_fail (set_name != NULL, FALSE);

    if (qcdm_result_get_u8_array (result, set_name, &array, &array_len))
        return FALSE;

    qcdm_return_val_if_fail (num < array_len / sizeof (DMLogItemEvdoPilotSetsV2Pilot), FALSE);

    pilot = (DMLogItemEvdoPilotSetsV2Pilot *) &array[num * sizeof (DMLogItemEvdoPilotSetsV2Pilot)];
    *out_pilot_pn = le16toh (pilot->pilot_pn);
    *out_pilot_energy = le16toh (pilot->pilot_energy);
    *out_rssi_dbm = (int32_t) (-110.0 + ((float) MAX (le16toh (pilot->pilot_energy) - 50, 0) / 14.0));
    return TRUE;
}

/**********************************************************************/