3 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <sys/types.h>
24 #include <libubox/uloop.h>
28 uint8_t mbim_buffer
[MBIM_BUFFER_SIZE
];
29 static struct uloop_fd mbim_fd
;
30 static uint32_t expected
;
33 static void mbim_msg_tout_cb(struct uloop_timeout
*t
)
35 fprintf(stderr
, "ERROR: mbim message timeout\n");
39 static struct uloop_timeout tout
= {
40 .cb
= mbim_msg_tout_cb
,
46 struct mbim_message_header
*hdr
= (struct mbim_message_header
*) mbim_buffer
;
49 if (le32toh(hdr
->length
) > MBIM_BUFFER_SIZE
) {
50 fprintf(stderr
, "message too big %d\n", le32toh(hdr
->length
));
55 fprintf(stderr
, "sending (%d): ", le32toh(hdr
->length
));
56 for (ret
= 0; ret
< le32toh(hdr
->length
); ret
++)
57 printf("%02x ", ((uint8_t *) mbim_buffer
)[ret
]);
59 printf(" header_type: %04X\n", le32toh(hdr
->type
));
60 printf(" header_length: %04X\n", le32toh(hdr
->length
));
61 printf(" header_transaction: %04X\n", le32toh(hdr
->transaction_id
));
64 ret
= write(mbim_fd
.fd
, mbim_buffer
, le32toh(hdr
->length
));
66 perror("writing data failed: ");
68 expected
= le32toh(hdr
->type
) | 0x80000000;
69 uloop_timeout_set(&tout
, 15000);
75 mbim_recv(struct uloop_fd
*u
, unsigned int events
)
77 ssize_t cnt
= read(u
->fd
, mbim_buffer
, MBIM_BUFFER_SIZE
);
78 struct mbim_message_header
*hdr
= (struct mbim_message_header
*) mbim_buffer
;
79 struct command_message
*msg
= (struct command_message
*) mbim_buffer
;
85 if (cnt
< sizeof(struct mbim_message_header
)) {
86 perror("failed to read() data: ");
90 printf("reading (%zu): ", cnt
);
91 for (i
= 0; i
< cnt
; i
++)
92 printf("%02x ", mbim_buffer
[i
]);
94 printf(" header_type: %04X\n", le32toh(hdr
->type
));
95 printf(" header_length: %04X\n", le32toh(hdr
->length
));
96 printf(" header_transaction: %04X\n", le32toh(hdr
->transaction_id
));
99 if (le32toh(hdr
->type
) == expected
)
100 uloop_timeout_cancel(&tout
);
102 switch(le32toh(hdr
->type
)) {
103 case MBIM_MESSAGE_TYPE_OPEN_DONE
:
104 if (current_handler
->request() < 0)
105 mbim_send_close_msg();
107 case MBIM_MESSAGE_TYPE_COMMAND_DONE
:
108 return_code
= current_handler
->response(msg
->buffer
, le32toh(msg
->buffer_length
));
111 mbim_send_close_msg();
113 case MBIM_MESSAGE_TYPE_CLOSE_DONE
:
116 case MBIM_MESSAGE_TYPE_FUNCTION_ERROR
:
118 mbim_send_close_msg();
125 mbim_open(const char *path
)
127 mbim_fd
.cb
= mbim_recv
;
128 mbim_fd
.fd
= open(path
, O_RDWR
);
129 if (mbim_fd
.fd
< 1) {
130 perror("open failed: ");
133 uloop_fd_add(&mbim_fd
, ULOOP_READ
);