summaryrefslogtreecommitdiff
path: root/qmi-message.c
blob: bc16923057a35abfa1fd8ecc28d599bfe37f094e (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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "qmi-message.h"

static uint8_t buf[QMI_BUFFER_LEN];
static unsigned int buf_ofs;

uint8_t *__qmi_get_buf(unsigned int *ofs)
{
	*ofs = buf_ofs;
	return buf;
}

void __qmi_alloc_reset(void)
{
	buf_ofs = 0;
}

void *__qmi_alloc_static(unsigned int len)
{
	void *ret;

	if (buf_ofs + len > sizeof(buf)) {
		fprintf(stderr, "ERROR: static buffer for message data too small\n");
		abort();
	}

	ret = &buf[buf_ofs];
	buf_ofs += len;
	memset(ret, 0, len);
	return ret;
}

char *__qmi_copy_string(void *data, unsigned int len)
{
	char *res;

	res = (char *) &buf[buf_ofs];
	buf_ofs += len + 1;
	memcpy(res, data, len);
	res[len] = 0;
	return res;
}

struct tlv *tlv_get_next(void **buf, unsigned int *buflen)
{
	struct tlv *tlv = NULL;
	unsigned int tlv_len;

	if (*buflen < sizeof(*tlv))
		return NULL;

	tlv = *buf;
	tlv_len = le16_to_cpu(tlv->len) + sizeof(*tlv);
	if (tlv_len > *buflen)
		return NULL;

	*buflen -= tlv_len;
	*buf += tlv_len;
	return tlv;
}

static struct tlv *qmi_msg_next_tlv(struct qmi_msg *qm, int add)
{
	int tlv_len;
	void *tlv;

	if (qm->qmux.service == QMI_SERVICE_CTL) {
		tlv = qm->ctl.tlv;
		tlv_len = le16_to_cpu(qm->ctl.tlv_len);
		qm->ctl.tlv_len = cpu_to_le16(tlv_len + add);
	} else {
		tlv = qm->svc.tlv;
		tlv_len = le16_to_cpu(qm->svc.tlv_len);
		qm->svc.tlv_len = cpu_to_le16(tlv_len + add);
	}

	tlv += tlv_len;

	return tlv;
}

void tlv_new(struct qmi_msg *qm, uint8_t type, uint16_t len, void *data)
{
	struct tlv *tlv;

	tlv = qmi_msg_next_tlv(qm, sizeof(*tlv) + len);
	tlv->type = type;
	tlv->len = cpu_to_le16(len);
	memcpy(tlv->data, data, len);
}

void qmi_init_request_message(struct qmi_msg *qm, QmiService service)
{
	memset(qm, 0, sizeof(*qm));
	qm->marker = 1;
	qm->qmux.service = service;
}

int qmi_complete_request_message(struct qmi_msg *qm)
{
	void *tlv_end = qmi_msg_next_tlv(qm, 0);
	void *msg_start = &qm->qmux;

	qm->qmux.len = cpu_to_le16(tlv_end - msg_start);
	return tlv_end - msg_start + 1;
}

int qmi_check_message_status(void *tlv_buf, unsigned int len)
{
	struct tlv *tlv;
	struct {
		uint16_t status;
		uint16_t code;
	} __packed *status;

	while ((tlv = tlv_get_next(&tlv_buf, &len)) != NULL) {
		if (tlv->type != 2)
			continue;

		if (tlv_data_len(tlv) != sizeof(*status))
			return QMI_ERROR_INVALID_DATA;

		status = (void *) tlv->data;
		if (!status->status)
			return 0;

		return le16_to_cpu(status->code);
	}

	return QMI_ERROR_NO_DATA;
}

void *qmi_msg_get_tlv_buf(struct qmi_msg *qm, int *tlv_len)
{
	void *ptr;
	int len;

	if (qm->qmux.service == QMI_SERVICE_CTL) {
		ptr = qm->ctl.tlv;
		len = qm->ctl.tlv_len;
	} else {
		ptr = qm->svc.tlv;
		len = qm->svc.tlv_len;
	}

	if (tlv_len)
		*tlv_len = len;

	return ptr;
}