Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed Rejestrator
F401RE-USBHost/USBHostRSSI/USBHostRSSI.cpp@0:fa31f8461c63, 2015-04-18 (annotated)
- Committer:
- Waldek
- Date:
- Sat Apr 18 17:01:57 2015 +0000
- Revision:
- 0:fa31f8461c63
working version, stop
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Waldek | 0:fa31f8461c63 | 1 | #include "USBHostRSSI.h" |
Waldek | 0:fa31f8461c63 | 2 | |
Waldek | 0:fa31f8461c63 | 3 | USBHostRSSI::USBHostRSSI() |
Waldek | 0:fa31f8461c63 | 4 | { |
Waldek | 0:fa31f8461c63 | 5 | host = USBHost::getHostInst(); |
Waldek | 0:fa31f8461c63 | 6 | init(); |
Waldek | 0:fa31f8461c63 | 7 | } |
Waldek | 0:fa31f8461c63 | 8 | |
Waldek | 0:fa31f8461c63 | 9 | void USBHostRSSI::init() |
Waldek | 0:fa31f8461c63 | 10 | { |
Waldek | 0:fa31f8461c63 | 11 | dev = NULL; |
Waldek | 0:fa31f8461c63 | 12 | int_in = NULL; |
Waldek | 0:fa31f8461c63 | 13 | onUpdate = NULL; |
Waldek | 0:fa31f8461c63 | 14 | dev_connected = false; |
Waldek | 0:fa31f8461c63 | 15 | bluetooth_device_found = false; |
Waldek | 0:fa31f8461c63 | 16 | bluetooth_intf = -1; |
Waldek | 0:fa31f8461c63 | 17 | seq = 0; |
Waldek | 0:fa31f8461c63 | 18 | |
Waldek | 0:fa31f8461c63 | 19 | } |
Waldek | 0:fa31f8461c63 | 20 | |
Waldek | 0:fa31f8461c63 | 21 | bool USBHostRSSI::connected() { |
Waldek | 0:fa31f8461c63 | 22 | return dev_connected; |
Waldek | 0:fa31f8461c63 | 23 | } |
Waldek | 0:fa31f8461c63 | 24 | |
Waldek | 0:fa31f8461c63 | 25 | bool USBHostRSSI::connect() { |
Waldek | 0:fa31f8461c63 | 26 | |
Waldek | 0:fa31f8461c63 | 27 | if (dev_connected) { |
Waldek | 0:fa31f8461c63 | 28 | return true; |
Waldek | 0:fa31f8461c63 | 29 | } |
Waldek | 0:fa31f8461c63 | 30 | |
Waldek | 0:fa31f8461c63 | 31 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
Waldek | 0:fa31f8461c63 | 32 | if ((dev = host->getDevice(i)) != NULL) { |
Waldek | 0:fa31f8461c63 | 33 | if(host->enumerate(dev, this)) { |
Waldek | 0:fa31f8461c63 | 34 | break; |
Waldek | 0:fa31f8461c63 | 35 | } |
Waldek | 0:fa31f8461c63 | 36 | if (bluetooth_device_found) { |
Waldek | 0:fa31f8461c63 | 37 | int_in = dev->getEndpoint(bluetooth_intf, INTERRUPT_ENDPOINT, IN); |
Waldek | 0:fa31f8461c63 | 38 | USB_DBG("int_in=%p", int_in); |
Waldek | 0:fa31f8461c63 | 39 | if (!int_in) { |
Waldek | 0:fa31f8461c63 | 40 | break; |
Waldek | 0:fa31f8461c63 | 41 | } |
Waldek | 0:fa31f8461c63 | 42 | USB_INFO("New Bluetooth device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, bluetooth_intf); |
Waldek | 0:fa31f8461c63 | 43 | int_in->attach(this, &USBHostRSSI::rxHandler); |
Waldek | 0:fa31f8461c63 | 44 | int rc = host->interruptRead(dev, int_in, int_buf, sizeof(int_buf), false); |
Waldek | 0:fa31f8461c63 | 45 | USB_TEST_ASSERT(rc != USB_TYPE_ERROR); |
Waldek | 0:fa31f8461c63 | 46 | rc = cmdSend(HCI_OP_RESET); |
Waldek | 0:fa31f8461c63 | 47 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
Waldek | 0:fa31f8461c63 | 48 | dev_connected = true; |
Waldek | 0:fa31f8461c63 | 49 | return true; |
Waldek | 0:fa31f8461c63 | 50 | } |
Waldek | 0:fa31f8461c63 | 51 | } |
Waldek | 0:fa31f8461c63 | 52 | } |
Waldek | 0:fa31f8461c63 | 53 | init(); |
Waldek | 0:fa31f8461c63 | 54 | return false; |
Waldek | 0:fa31f8461c63 | 55 | } |
Waldek | 0:fa31f8461c63 | 56 | |
Waldek | 0:fa31f8461c63 | 57 | void USBHostRSSI::rxHandler() { |
Waldek | 0:fa31f8461c63 | 58 | event(int_buf, int_in->getLengthTransferred()); |
Waldek | 0:fa31f8461c63 | 59 | if (dev) { |
Waldek | 0:fa31f8461c63 | 60 | host->interruptRead(dev, int_in, int_buf, sizeof(int_buf), false); |
Waldek | 0:fa31f8461c63 | 61 | } |
Waldek | 0:fa31f8461c63 | 62 | } |
Waldek | 0:fa31f8461c63 | 63 | |
Waldek | 0:fa31f8461c63 | 64 | /*virtual*/ void USBHostRSSI::setVidPid(uint16_t vid, uint16_t pid) |
Waldek | 0:fa31f8461c63 | 65 | { |
Waldek | 0:fa31f8461c63 | 66 | USB_DBG("vid:%04x pid:%04x", vid, pid); |
Waldek | 0:fa31f8461c63 | 67 | // we don't check VID/PID for mouse driver |
Waldek | 0:fa31f8461c63 | 68 | } |
Waldek | 0:fa31f8461c63 | 69 | |
Waldek | 0:fa31f8461c63 | 70 | /*virtual*/ bool USBHostRSSI::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed |
Waldek | 0:fa31f8461c63 | 71 | { |
Waldek | 0:fa31f8461c63 | 72 | USB_DBG("intf: %d class: %02x %02x %02x", intf_nb, intf_class, intf_subclass, intf_protocol); |
Waldek | 0:fa31f8461c63 | 73 | if (bluetooth_intf == -1 && intf_class == 0xe0) { |
Waldek | 0:fa31f8461c63 | 74 | bluetooth_intf = intf_nb; |
Waldek | 0:fa31f8461c63 | 75 | return true; |
Waldek | 0:fa31f8461c63 | 76 | } |
Waldek | 0:fa31f8461c63 | 77 | return false; |
Waldek | 0:fa31f8461c63 | 78 | } |
Waldek | 0:fa31f8461c63 | 79 | |
Waldek | 0:fa31f8461c63 | 80 | /*virtual*/ bool USBHostRSSI::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used |
Waldek | 0:fa31f8461c63 | 81 | { |
Waldek | 0:fa31f8461c63 | 82 | USB_DBG("intf_nb=%d type=%d dir=%d", intf_nb, type, dir); |
Waldek | 0:fa31f8461c63 | 83 | |
Waldek | 0:fa31f8461c63 | 84 | if (intf_nb == bluetooth_intf) { |
Waldek | 0:fa31f8461c63 | 85 | if (type == INTERRUPT_ENDPOINT && dir == IN) { |
Waldek | 0:fa31f8461c63 | 86 | bluetooth_device_found = true; |
Waldek | 0:fa31f8461c63 | 87 | return true; |
Waldek | 0:fa31f8461c63 | 88 | } |
Waldek | 0:fa31f8461c63 | 89 | } |
Waldek | 0:fa31f8461c63 | 90 | return false; |
Waldek | 0:fa31f8461c63 | 91 | } |
Waldek | 0:fa31f8461c63 | 92 | |
Waldek | 0:fa31f8461c63 | 93 | void USBHostRSSI::event(uint8_t* data, int len) { |
Waldek | 0:fa31f8461c63 | 94 | CTASSERT(sizeof(BD_ADDR) == 6); |
Waldek | 0:fa31f8461c63 | 95 | CTASSERT(sizeof(inquiry_with_rssi_info) == 14); |
Waldek | 0:fa31f8461c63 | 96 | inquiry_with_rssi_info* info; |
Waldek | 0:fa31f8461c63 | 97 | int max_period_length = 25; |
Waldek | 0:fa31f8461c63 | 98 | int min_period_length = 20; |
Waldek | 0:fa31f8461c63 | 99 | int inquiry_length = 15; |
Waldek | 0:fa31f8461c63 | 100 | USB_TYPE rc; |
Waldek | 0:fa31f8461c63 | 101 | if (len > 0) { |
Waldek | 0:fa31f8461c63 | 102 | USB_DBG_HEX(data, len); |
Waldek | 0:fa31f8461c63 | 103 | hci_event* event = reinterpret_cast<hci_event*>(data); |
Waldek | 0:fa31f8461c63 | 104 | switch(event->evt) { |
Waldek | 0:fa31f8461c63 | 105 | case HCI_EV_CMD_COMPLETE: |
Waldek | 0:fa31f8461c63 | 106 | switch(event->c.op) { |
Waldek | 0:fa31f8461c63 | 107 | case HCI_OP_RESET: |
Waldek | 0:fa31f8461c63 | 108 | wait_ms(500); |
Waldek | 0:fa31f8461c63 | 109 | rc = cmdSend(HCI_OP_WRITE_INQUIRY_MODE, "B", 0x01); // with RSSI |
Waldek | 0:fa31f8461c63 | 110 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
Waldek | 0:fa31f8461c63 | 111 | break; |
Waldek | 0:fa31f8461c63 | 112 | case HCI_OP_WRITE_INQUIRY_MODE: |
Waldek | 0:fa31f8461c63 | 113 | rc = cmdSend(HCI_OP_PERIODIC_INQUIRY, "HHBBBBB", |
Waldek | 0:fa31f8461c63 | 114 | max_period_length, min_period_length, 0x33, 0x8B, 0x9E, inquiry_length, 0); |
Waldek | 0:fa31f8461c63 | 115 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
Waldek | 0:fa31f8461c63 | 116 | break; |
Waldek | 0:fa31f8461c63 | 117 | default: |
Waldek | 0:fa31f8461c63 | 118 | break; |
Waldek | 0:fa31f8461c63 | 119 | } |
Waldek | 0:fa31f8461c63 | 120 | break; |
Waldek | 0:fa31f8461c63 | 121 | case HCI_EV_INQUIRY_RESULT_WITH_RSSI: |
Waldek | 0:fa31f8461c63 | 122 | //USB_DBG_HEX(buf, r); |
Waldek | 0:fa31f8461c63 | 123 | info = reinterpret_cast<inquiry_with_rssi_info*>(event->c.data); |
Waldek | 0:fa31f8461c63 | 124 | if (onUpdate) { |
Waldek | 0:fa31f8461c63 | 125 | (*onUpdate)(info); |
Waldek | 0:fa31f8461c63 | 126 | } |
Waldek | 0:fa31f8461c63 | 127 | break; |
Waldek | 0:fa31f8461c63 | 128 | default: |
Waldek | 0:fa31f8461c63 | 129 | break; |
Waldek | 0:fa31f8461c63 | 130 | } |
Waldek | 0:fa31f8461c63 | 131 | } |
Waldek | 0:fa31f8461c63 | 132 | } |
Waldek | 0:fa31f8461c63 | 133 | |
Waldek | 0:fa31f8461c63 | 134 | USB_TYPE USBHostRSSI::cmdSend(uint16_t op) |
Waldek | 0:fa31f8461c63 | 135 | { |
Waldek | 0:fa31f8461c63 | 136 | return cmdSendSub(op, NULL, 0); |
Waldek | 0:fa31f8461c63 | 137 | } |
Waldek | 0:fa31f8461c63 | 138 | |
Waldek | 0:fa31f8461c63 | 139 | USB_TYPE USBHostRSSI::cmdSend(uint16_t op, const char* fmt, ...) |
Waldek | 0:fa31f8461c63 | 140 | { |
Waldek | 0:fa31f8461c63 | 141 | va_list vl; |
Waldek | 0:fa31f8461c63 | 142 | va_start(vl, fmt); |
Waldek | 0:fa31f8461c63 | 143 | uint8_t buf[255]; |
Waldek | 0:fa31f8461c63 | 144 | int pos = 0; |
Waldek | 0:fa31f8461c63 | 145 | char* name; |
Waldek | 0:fa31f8461c63 | 146 | int name_len; |
Waldek | 0:fa31f8461c63 | 147 | uint16_t h; |
Waldek | 0:fa31f8461c63 | 148 | BD_ADDR* bdaddr; |
Waldek | 0:fa31f8461c63 | 149 | for(int i = 0; fmt[i]; i++) { |
Waldek | 0:fa31f8461c63 | 150 | switch(fmt[i]) { |
Waldek | 0:fa31f8461c63 | 151 | case 's': |
Waldek | 0:fa31f8461c63 | 152 | name = va_arg(vl, char*); |
Waldek | 0:fa31f8461c63 | 153 | name_len = strlen(name)+1; |
Waldek | 0:fa31f8461c63 | 154 | memcpy(buf+pos, name, name_len); |
Waldek | 0:fa31f8461c63 | 155 | pos += name_len; |
Waldek | 0:fa31f8461c63 | 156 | break; |
Waldek | 0:fa31f8461c63 | 157 | case 'B': |
Waldek | 0:fa31f8461c63 | 158 | buf[pos++] = va_arg(vl, int); |
Waldek | 0:fa31f8461c63 | 159 | break; |
Waldek | 0:fa31f8461c63 | 160 | case 'H': |
Waldek | 0:fa31f8461c63 | 161 | h = va_arg(vl, int); |
Waldek | 0:fa31f8461c63 | 162 | buf[pos++] = h; |
Waldek | 0:fa31f8461c63 | 163 | buf[pos++] = h>>8; |
Waldek | 0:fa31f8461c63 | 164 | break; |
Waldek | 0:fa31f8461c63 | 165 | case 'A': |
Waldek | 0:fa31f8461c63 | 166 | bdaddr = va_arg(vl, BD_ADDR*); |
Waldek | 0:fa31f8461c63 | 167 | memcpy(buf+pos, bdaddr, 6); |
Waldek | 0:fa31f8461c63 | 168 | pos += 6; |
Waldek | 0:fa31f8461c63 | 169 | break; |
Waldek | 0:fa31f8461c63 | 170 | default: |
Waldek | 0:fa31f8461c63 | 171 | USB_DBG("op=%04X fmt=%s i=%d", op, fmt, i); |
Waldek | 0:fa31f8461c63 | 172 | break; |
Waldek | 0:fa31f8461c63 | 173 | } |
Waldek | 0:fa31f8461c63 | 174 | } |
Waldek | 0:fa31f8461c63 | 175 | return cmdSendSub(op, buf, pos); |
Waldek | 0:fa31f8461c63 | 176 | } |
Waldek | 0:fa31f8461c63 | 177 | |
Waldek | 0:fa31f8461c63 | 178 | USB_TYPE USBHostRSSI::cmdSendSub(uint16_t op, const uint8_t* data, int size) |
Waldek | 0:fa31f8461c63 | 179 | { |
Waldek | 0:fa31f8461c63 | 180 | uint8_t* buf = new uint8_t[size+3]; |
Waldek | 0:fa31f8461c63 | 181 | buf[0] = op; |
Waldek | 0:fa31f8461c63 | 182 | buf[1] = op>>8; |
Waldek | 0:fa31f8461c63 | 183 | buf[2] = size; |
Waldek | 0:fa31f8461c63 | 184 | if (data) { |
Waldek | 0:fa31f8461c63 | 185 | memcpy(buf+3, data, size); |
Waldek | 0:fa31f8461c63 | 186 | } |
Waldek | 0:fa31f8461c63 | 187 | USB_TYPE rc = host->controlWrite(dev, USB_REQUEST_TYPE_CLASS, 0, 0, 0, buf, size+3); |
Waldek | 0:fa31f8461c63 | 188 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
Waldek | 0:fa31f8461c63 | 189 | delete[] buf; |
Waldek | 0:fa31f8461c63 | 190 | return rc; |
Waldek | 0:fa31f8461c63 | 191 | } |