#include "mbim.h"
-uint8_t mbim_buffer[MBIM_BUFFER_SIZE];
+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");
- uloop_end();
+ mbim_end();
}
static struct uloop_timeout tout = {
struct mbim_message_header *hdr = (struct mbim_message_header *) mbim_buffer;
int ret = 0;
- if (le32toh(hdr->length) > MBIM_BUFFER_SIZE) {
+ if (le32toh(hdr->length) > mbim_bufsize) {
fprintf(stderr, "message too big %d\n", le32toh(hdr->length));
return -1;
}
static void
mbim_recv(struct uloop_fd *u, unsigned int events)
{
- ssize_t cnt = read(u->fd, mbim_buffer, MBIM_BUFFER_SIZE);
+ 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;
mbim_send_close_msg();
break;
case MBIM_MESSAGE_TYPE_CLOSE_DONE:
- uloop_end();
+ mbim_end();
break;
case MBIM_MESSAGE_TYPE_FUNCTION_ERROR:
no_close = 0;
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();
+}
{
struct command_message *cmd = (struct command_message *) mbim_buffer;
- memset(mbim_buffer, 0, MBIM_BUFFER_SIZE);
+ if (!mbim_buffer)
+ return NULL;
+ memset(mbim_buffer, 0, mbim_bufsize);
cmd->fragment_header.total = htole32(1);
cmd->fragment_header.current = htole32(0);
cmd->buffer_length = htole32(len);
payload_offset = len;
- payload_free = MBIM_BUFFER_SIZE - (sizeof(*cmd) + len);
+ payload_free = mbim_bufsize - (sizeof(*cmd) + len);
payload_len = 0;
payload_buffer = cmd->buffer;
{
struct command_message *cmd = (struct command_message *) mbim_buffer;
+ if (!mbim_buffer)
+ return 0;
if (payload_len & 0x3) {
payload_len &= ~0x3;
payload_len += 4;
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);
+ msg->max_control_transfer = htole32(mbim_bufsize);
return mbim_send();
}
{
struct mbim_message_header *hdr = (struct mbim_message_header *) mbim_buffer;
- if (no_close) {
- uloop_end();
+ if (no_close || !mbim_buffer) {
+ mbim_end();
return 0;
}
mbim_setup_header(hdr, MBIM_MESSAGE_TYPE_CLOSE, sizeof(*hdr));