summaryrefslogtreecommitdiff
path: root/mbim-msg.c
blob: ad5a2d5dcd9adcc026e8dd3347916c2895d0b93f (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
/*
 * umbim
 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * 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.
 */

#include <sys/types.h>
#include <sys/stat.h>

#include <alloca.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <libubox/utils.h>
#include <libubox/uloop.h>

#include "mbim.h"

#include "data/mbim-service-basic-connect.h"

int transaction_id = 1;
uint8_t basic_connect[16] = { 0xa2, 0x89, 0xcc, 0x33, 0xbc, 0xbb, 0x8b, 0x4f,
		     0xb6, 0xb0, 0x13, 0x3e, 0xc2, 0xaa, 0xe6,0xdf };
static int payload_offset, payload_free, payload_len;
static uint8_t *payload_buffer;

int
mbim_add_payload(uint8_t len)
{
	uint32_t offset = payload_offset;

	if (payload_free < len)
		return 0;

	payload_free -= len;
	payload_offset += len;
	payload_len += len;

	return offset;
}

int
mbim_encode_string(struct mbim_string *str, char *in)
{
	int l = strlen(in);
	int s = mbim_add_payload(l * 2);
	uint8_t *p = &payload_buffer[s];
	int i;

	if (!s)
		return -1;

	str->offset = htole32(s);
	str->length = htole32(l * 2);
	for (i = 0; i < l; i++)
		p[i * 2] = in[i];

	return 0;
}


char *
mbim_get_string(struct mbim_string *str, char *in)
{
	char *p = &in[le32toh(str->offset)];
	int i;

	if (!le32toh(str->offset))
		return NULL;

	if (le32toh(str->length)) {
		for (i = 0; i < le32toh(str->length) / 2; i++)
			p[i] = p[i * 2];
		p[i] = '\0';
		str->length = 0;
	}

	return p;
}

void
mbim_get_ipv4(void *buffer, char *out, uint32_t offset)
{
	uint8_t *b = buffer + offset;

	snprintf(out, 16, "%d.%d.%d.%d", b[0], b[1], b[2], b[3]);
}

void
mbim_get_ipv6(void *buffer, char *out, uint32_t offset)
{
	uint8_t *b = buffer + offset;

	snprintf(out, 40, "%x:%x:%x:%x:%x:%x:%x:%x", b[0] << 8 | b[1],
		 b[2] << 8 | b[3], b[4] << 8 | b[5], b[6] << 8 | b[7],
		 b[8] << 8 | b[9], b[10] << 8 | b[11], b[12] << 8 | b[13],
		 b[14] << 8 | b[15]);
}

uint32_t
mbim_get_int(void *buffer, uint32_t offset)
{
	uint32_t *i = buffer + offset;

	return le32toh(*i);
}

const char*
mbim_enum_string(struct mbim_enum *e, uint32_t key)
{
	while (e->skey) {
		if (key == e->key)
			return e->val;
		e++;
	}
	return NULL;
}

void
mbim_setup_header(struct mbim_message_header *hdr, MbimMessageType type, int length)
{
	if (length < 16)
		length = 16;

	hdr->transaction_id = htole32(transaction_id++);
	hdr->type = htole32(type);
	hdr->length = htole32(length);
}

uint8_t*
mbim_setup_command_msg(uint8_t *uuid, uint32_t type, uint32_t command_id, int len)
{
	struct command_message *cmd = (struct command_message *) mbim_buffer;

	memset(mbim_buffer, 0, MBIM_BUFFER_SIZE);

	cmd->fragment_header.total = htole32(1);
	cmd->fragment_header.current = htole32(0);
	memcpy(cmd->service_id, uuid, 16);
	cmd->command_id = htole32(command_id);
	cmd->command_type = htole32(type);
	cmd->buffer_length = htole32(len);

	payload_offset = len;
	payload_free = MBIM_BUFFER_SIZE - (sizeof(*cmd) + len);
	payload_len = 0;
	payload_buffer = cmd->buffer;

	return cmd->buffer;
}

int
mbim_send_command_msg(void)
{
	struct command_message *cmd = (struct command_message *) mbim_buffer;

	if (payload_len & 0x3) {
		payload_len &= ~0x3;
		payload_len += 4;
	}

        cmd->buffer_length = htole32(le32toh(cmd->buffer_length) + payload_len);
	mbim_setup_header(&cmd->header, MBIM_MESSAGE_TYPE_COMMAND, sizeof(*cmd) + le32toh(cmd->buffer_length));

	return mbim_send();
}

int
mbim_send_open_msg(void)
{
	struct mbim_open_message *msg = (struct mbim_open_message *) mbim_buffer;

	mbim_setup_header(&msg->header, MBIM_MESSAGE_TYPE_OPEN, sizeof(*msg));
	msg->max_control_transfer = htole32(MBIM_BUFFER_SIZE);

	return mbim_send();
}

int
mbim_send_close_msg(void)
{
	struct mbim_message_header *hdr = (struct mbim_message_header *) mbim_buffer;

	if (no_close) {
		uloop_end();
		return 0;
	}
	mbim_setup_header(hdr, MBIM_MESSAGE_TYPE_CLOSE, sizeof(*hdr));

	return mbim_send();
}