summaryrefslogtreecommitdiff
path: root/mbim-dev.c
blob: 34bb2c228eb047f8c05391873e016dd6f9a9ded0 (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
/*
 * 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 <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

#include <libubox/uloop.h>

#include "mbim.h"

size_t mbim_bufsize = 0;
uint8_t *mbim_buffer = NULL;
static struct uloop_fd mbim_fd;
static uint32_t expected;
int no_close;

static void mbim_msg_tout_cb(struct uloop_timeout *t)
{
	fprintf(stderr, "ERROR: mbim message timeout\n");
	mbim_end();
}

static struct uloop_timeout tout = {
	.cb = mbim_msg_tout_cb,
};

int
mbim_send(void)
{
	struct mbim_message_header *hdr = (struct mbim_message_header *) mbim_buffer;
	int ret = 0;

	if (le32toh(hdr->length) > mbim_bufsize) {
		fprintf(stderr, "message too big %d\n", le32toh(hdr->length));
		return -1;
	}

	if (verbose) {
		fprintf(stderr, "sending (%d): ", le32toh(hdr->length));
		for (ret = 0; ret < le32toh(hdr->length); ret++)
			printf("%02x ", ((uint8_t *) mbim_buffer)[ret]);
		printf("\n");
		printf("  header_type: %04X\n", le32toh(hdr->type));
		printf("  header_length: %04X\n", le32toh(hdr->length));
		printf("  header_transaction: %04X\n", le32toh(hdr->transaction_id));
	}

	ret = write(mbim_fd.fd, mbim_buffer, le32toh(hdr->length));
	if (!ret) {
		perror("writing data failed: ");
	} else {
		expected = le32toh(hdr->type) | 0x80000000;
		uloop_timeout_set(&tout, 15000);
	}
	return ret;
}

static void
mbim_recv(struct uloop_fd *u, unsigned int events)
{
	ssize_t cnt = read(u->fd, mbim_buffer, mbim_bufsize);
	struct mbim_message_header *hdr = (struct mbim_message_header *) mbim_buffer;
	struct command_done_message *msg = (struct command_done_message *) (hdr + 1);
	int i;

	if (cnt < 0)
		return;

	if (cnt < sizeof(struct mbim_message_header)) {
		perror("failed to read() data: ");
		return;
	}
	if (verbose) {
		printf("reading (%zu): ", cnt);
		for (i = 0; i < cnt; i++)
			printf("%02x ", mbim_buffer[i]);
		printf("\n");
		printf("  header_type: %04X\n", le32toh(hdr->type));
		printf("  header_length: %04X\n", le32toh(hdr->length));
		printf("  header_transaction: %04X\n", le32toh(hdr->transaction_id));
	}

	if (le32toh(hdr->type) == expected)
		uloop_timeout_cancel(&tout);

	switch(le32toh(hdr->type)) {
	case MBIM_MESSAGE_TYPE_OPEN_DONE:
		if (current_handler->request() < 0)
			mbim_send_close_msg();
		break;
	case MBIM_MESSAGE_TYPE_COMMAND_DONE:
		if (verbose) {
			printf("  command_id: %04X\n", le32toh(msg->command_id));
			printf("  status_code: %04X\n", le32toh(msg->status_code));
		}
		if (msg->status_code && !msg->buffer_length)
			return_code = -le32toh(msg->status_code);
		else
			return_code = current_handler->response(msg->buffer, le32toh(msg->buffer_length));
		if (return_code < 0)
			no_close = 0;
		mbim_send_close_msg();
		break;
	case MBIM_MESSAGE_TYPE_CLOSE_DONE:
		mbim_end();
		break;
	case MBIM_MESSAGE_TYPE_FUNCTION_ERROR:
		no_close = 0;
		mbim_send_close_msg();
		return_code = -1;
		break;
	}
}

void
mbim_open(const char *path)
{
	mbim_fd.cb = mbim_recv;
	mbim_fd.fd = open(path, O_RDWR);
	if (mbim_fd.fd < 1) {
		perror("open failed: ");
		exit(-1);
	}
	mbim_bufsize = MBIM_BUFFER_SIZE;
	mbim_buffer = malloc(mbim_bufsize);
	uloop_fd_add(&mbim_fd, ULOOP_READ);
}

void
mbim_end(void)
{
	if (mbim_buffer) {
		free(mbim_buffer);
		mbim_bufsize = 0;
		mbim_buffer = NULL;
	}
	uloop_end();
}