2 #if (USE_STM32_USB_HOST_MODE || USE_STM32_USB_USE_DEVICE_MODE || USE_STM32_USB_OTG_MODE)
3 #include "config/usb_conf.h"
4 #if defined(USE_HOST_MODE)
10 static const uint32_t USE_KEEPALIVE = 0;
11 #define ANDROSENSOR_KEEPALIVE_TIME_MS (500)
13 static const uint8_t ANDROSENSOR_SOF[] = {0xff,
'a',
's',0xff,0x00,0x00,0x00,0x01};
15 enum androsensor_msg_type
17 MSG_TYPE_KEEPALIVE = 0x33,
21 struct androsensor_msg_t
23 uint8_t sof[
sizeof(ANDROSENSOR_SOF)];
24 uint8_t len[
sizeof(uint32_t)];
25 uint8_t type[
sizeof(uint32_t)];
29 static void encode_32(uint8_t *data, uint32_t value)
31 *data++ = value >> 24;
32 *data++ = value >> 16;
37 static uint32_t decode_32(uint8_t *data)
51 static uint32_t buffer[(128+
sizeof(
struct androsensor_msg_t)+sizeof(uint32_t)-1) / sizeof(uint32_t)];
53 static status_e msg_send(
struct androsensor_t *as,
enum androsensor_msg_type type,
const uint8_t *data, uint32_t len)
55 struct androsensor_msg_t *msg = (
struct androsensor_msg_t *)buffer;
57 if (!androsensor_active(as))
62 if (len +
sizeof(
struct androsensor_msg_t) >
sizeof(buffer))
67 memcpy(msg->sof, ANDROSENSOR_SOF,
sizeof(ANDROSENSOR_SOF));
68 encode_32(msg->len, len);
69 encode_32(msg->type, type);
70 memcpy(msg->data, data, len);
72 USBH_ADK_write(&USB_OTG_Core, (uint8_t*)msg,
sizeof(*msg) + len);
77 void androsensor_process(
struct androsensor_t *as)
80 const uint8_t keepalive_msg[] = {0x00,0x00};
81 struct androsensor_msg_t *msg = (
struct androsensor_msg_t *)&as->inbuff;
83 USBH_Process(&USB_OTG_Core , &USB_Host);
85 if (USE_KEEPALIVE &&
time_elapsed(as->keepalive_out_timeout))
88 msg_send(as, MSG_TYPE_KEEPALIVE, keepalive_msg,
sizeof(keepalive_msg));
91 if (USE_KEEPALIVE &&
time_elapsed(as->keepalive_in_timeout))
95 if (as->keepalivefatal)
97 as->state = STATE_UNKNOWN;
101 if (!USBH_ADK_Connected())
106 status = USBH_ADK_read(&USB_OTG_Core, (uint8_t *)as->inbuff,
sizeof(as->inbuff), &as->insize);
117 if (
memcmp(msg->sof, ANDROSENSOR_SOF,
sizeof(msg->sof)) )
123 if (decode_32(msg->type) == MSG_TYPE_KEEPALIVE)
126 as->state = STATE_CONNECTED;
131 as->insize = decode_32(msg->len);
132 if (decode_32(msg->len) > as->insize)
138 memcpy(as->inbuff, msg->data, as->insize);
141 status_e androsensor_init_master(
struct androsensor_t *as,
const char* manufacture,
const char* model,
const char* description,
const char* version,
const char* uri,
const char* serial)
146 as->keepalivefatal = 0;
161 uint32_t androsensor_active(
struct androsensor_t *as)
163 if (!USBH_ADK_Connected())
165 as->state = STATE_UNKNOWN;
169 as->state = STATE_CONNECTED;
172 if (as->state != STATE_CONNECTED)
180 status_e androsensor_write(
struct androsensor_t *as,
const uint8_t *data,
size_t len)
182 return msg_send(as, MSG_TYPE_DATA, data, len);
185 status_e androsensor_read(
struct androsensor_t *as, uint8_t *data,
size_t len,
size_t *rlen)
190 memcpy(data, as->inbuff, len);
192 memset(as->inbuff, 0x00, as->insize);
void * memcpy(void *dest, const void *src, size_t n)
void * memset(void *dest, int n, size_t n)
libheivs configuration file
This file contains all the prototypes for the usbh_adk_core.c.
static uint32_t time_elapsed(timeout_t timeout)
Is this time passed?
Function called at bad time.
timeout_t time_set_timeout_ms(uint32_t ms)
Set an obscure time at least ms milliseconds in the future.
int memcmp(const void *s1, const void *s2, size_t n)