-
Notifications
You must be signed in to change notification settings - Fork 1
/
qmi_tlv.c
219 lines (173 loc) · 3.82 KB
/
qmi_tlv.c
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
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include "libqrtr.h"
#include "util.h"
struct qmi_tlv *qmi_tlv_init(uint16_t txn, uint32_t msg_id, uint32_t msg_type)
{
struct qmi_header *pkt;
struct qmi_tlv *tlv;
tlv = malloc(sizeof(struct qmi_tlv));
memset(tlv, 0, sizeof(struct qmi_tlv));
tlv->size = sizeof(struct qmi_header);
tlv->allocated = malloc(tlv->size);
tlv->buf = tlv->allocated;
pkt = tlv->buf;
pkt->type = msg_type;
pkt->txn_id = txn;
pkt->msg_id = msg_id;
pkt->msg_len = 0;
return tlv;
}
struct qmi_tlv *qmi_tlv_decode(void *buf, size_t len)
{
struct qmi_tlv *tlv;
tlv = malloc(sizeof(struct qmi_tlv));
memset(tlv, 0, sizeof(struct qmi_tlv));
tlv->buf = buf;
tlv->size = len;
return tlv;
}
void *qmi_tlv_encode(struct qmi_tlv *tlv, size_t *len)
{
struct qmi_header *pkt;
if (!tlv || tlv->error)
return NULL;
pkt = tlv->buf;
pkt->msg_len = tlv->size - sizeof(struct qmi_header);
*len = tlv->size;
return tlv->buf;
}
void qmi_tlv_free(struct qmi_tlv *tlv)
{
free(tlv->allocated);
free(tlv);
}
static struct qmi_tlv_item *qmi_tlv_get_item(struct qmi_tlv *tlv, unsigned id)
{
struct qmi_tlv_item *item;
struct qmi_header *pkt;
unsigned offset = 0;
void *pkt_data;
pkt = tlv->buf;
pkt_data = &pkt[1];
while (offset < tlv->size) {
item = pkt_data + offset;
if (item->key == id)
return pkt_data + offset;
offset += sizeof(struct qmi_tlv_item) + item->len;
}
return NULL;
}
void *qmi_tlv_get(struct qmi_tlv *tlv, uint8_t id, size_t *len)
{
struct qmi_tlv_item *item;
item = qmi_tlv_get_item(tlv, id);
if (!item)
return NULL;
if (len)
*len = item->len;
return item->data;
}
void *qmi_tlv_get_array(struct qmi_tlv *tlv, uint8_t id, size_t len_size,
size_t *len, size_t *size)
{
struct qmi_tlv_item *item;
unsigned count;
void *ptr;
item = qmi_tlv_get_item(tlv, id);
if (!item)
return NULL;
ptr = item->data;
switch (len_size) {
case 4:
count = *(uint32_t *)ptr++;
break;
case 2:
count = *(uint16_t *)ptr++;
break;
case 1:
count = *(uint8_t *)ptr++;
break;
default:
return NULL;
}
*len = count;
*size = (item->len - len_size) / count;
return ptr;
}
static struct qmi_tlv_item *qmi_tlv_alloc_item(struct qmi_tlv *tlv, unsigned id,
size_t len)
{
struct qmi_tlv_item *item;
size_t new_size;
bool migrate;
void *newp;
/* If using user provided buffer, migrate data */
migrate = !tlv->allocated;
new_size = tlv->size + sizeof(struct qmi_tlv_item) + len;
newp = realloc(tlv->allocated, new_size);
if (!newp)
return NULL;
if (migrate)
memcpy(newp, tlv->buf, tlv->size);
item = newp + tlv->size;
item->key = id;
item->len = len;
tlv->buf = tlv->allocated = newp;
tlv->size = new_size;
return item;
}
int qmi_tlv_set(struct qmi_tlv *tlv, uint8_t id, void *buf, size_t len)
{
struct qmi_tlv_item *item;
if (!tlv)
return -EINVAL;
item = qmi_tlv_alloc_item(tlv, id, len);
if (!item) {
tlv->error = ENOMEM;
return -ENOMEM;
}
memcpy(item->data, buf, len);
return 0;
}
int qmi_tlv_set_array(struct qmi_tlv *tlv, uint8_t id, size_t len_size,
void *buf, size_t len, size_t size)
{
struct qmi_tlv_item *item;
size_t array_size;
void *ptr;
if (!tlv)
return -EINVAL;
array_size = len * size;
item = qmi_tlv_alloc_item(tlv, id, len_size + array_size);
if (!item) {
tlv->error = ENOMEM;
return -ENOMEM;
}
ptr = item->data;
switch (len_size) {
case 4:
*(uint32_t *)ptr++ = len;
break;
case 2:
*(uint16_t *)ptr++ = len;
break;
case 1:
*(uint8_t *)ptr++ = len;
break;
}
memcpy(ptr, buf, array_size);
return 0;
}
struct qmi_response_type_v01 qmi_tlv_get_result(struct qmi_tlv *tlv)
{
struct qmi_response_type_v01 *r = qmi_tlv_get(tlv, 2, NULL);
if (r)
return *r;
return (struct qmi_response_type_v01){0, 0};
}