USB Host Driver with Socket Modem support. Works with revision 323 of mbed-src but broken with any later version.

Dependencies:   FATFileSystem

Fork of F401RE-USBHost by Norimasa Okamoto

Committer:
fritz291
Date:
Fri Jun 26 19:30:55 2015 +0000
Revision:
26:53970cabf56d
Parent:
13:8774c07f12a5
Fixed small error in testAT function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 13:8774c07f12a5 1 // Simple USBHost Bluetooth RSSI for FRDM-KL46Z
va009039 13:8774c07f12a5 2 #pragma once
va009039 13:8774c07f12a5 3 #include "USBHost.h"
va009039 13:8774c07f12a5 4 #include <stdarg.h>
va009039 13:8774c07f12a5 5
va009039 13:8774c07f12a5 6 #define HCI_OP_INQUIRY 0x0401
va009039 13:8774c07f12a5 7 #define HCI_OP_INQUIRY_CANCEL 0x0402
va009039 13:8774c07f12a5 8 #define HCI_OP_PERIODIC_INQUIRY 0x0403
va009039 13:8774c07f12a5 9 #define HCI_OP_EXIT_PERIODIC_INQUIRY 0x0404
va009039 13:8774c07f12a5 10 #define HCI_OP_REMOTE_NAME_REQ 0x0419
va009039 13:8774c07f12a5 11 #define HCI_OP_RESET 0x0c03
va009039 13:8774c07f12a5 12 #define HCI_OP_WRITE_LOCAL_NAME 0x0c13
va009039 13:8774c07f12a5 13 #define HCI_OP_WRITE_SCAN_ENABLE 0x0c1a
va009039 13:8774c07f12a5 14 #define HCI_OP_WRITE_CLASS_OF_DEV 0x0c24
va009039 13:8774c07f12a5 15 #define HCI_OP_WRITE_INQUIRY_MODE 0x0c45
va009039 13:8774c07f12a5 16 #define HCI_OP_READ_EXTENDED_INQUIRY_RESPONSE 0x0c51
va009039 13:8774c07f12a5 17 #define HCI_OP_WRITE_EXTENDED_INQUIRY_RESPONSE 0x0c52
va009039 13:8774c07f12a5 18 #define HCI_OP_READ_BD_ADDR 0x1009
va009039 13:8774c07f12a5 19
va009039 13:8774c07f12a5 20 #define HCI_EV_INQUIRY_COMPLETE 0x01
va009039 13:8774c07f12a5 21 #define HCI_EV_INQUIRY_RESULT 0x02
va009039 13:8774c07f12a5 22 #define HCI_EV_REMOTE_NAME 0x07
va009039 13:8774c07f12a5 23 #define HCI_EV_CMD_COMPLETE 0x0e
va009039 13:8774c07f12a5 24 #define HCI_EV_CMD_STATUS 0x0f
va009039 13:8774c07f12a5 25 #define HCI_EV_INQUIRY_RESULT_WITH_RSSI 0x22
va009039 13:8774c07f12a5 26 #define HCI_EV_EXTENDED_INQUIRY_RESULT 0x2f
va009039 13:8774c07f12a5 27
va009039 13:8774c07f12a5 28 #pragma pack(push,1)
va009039 13:8774c07f12a5 29 struct BD_ADDR {
va009039 13:8774c07f12a5 30 uint8_t addr[6];
va009039 13:8774c07f12a5 31 void set(char* s) {
va009039 13:8774c07f12a5 32 char* p = s;
va009039 13:8774c07f12a5 33 for(int i = 5; i >= 0; i--) {
va009039 13:8774c07f12a5 34 addr[i] = strtol(p, &p, 16);
va009039 13:8774c07f12a5 35 if (*p == ':') {
va009039 13:8774c07f12a5 36 p++;
va009039 13:8774c07f12a5 37 }
va009039 13:8774c07f12a5 38 }
va009039 13:8774c07f12a5 39 }
va009039 13:8774c07f12a5 40 void str(char* buf, size_t size) {
va009039 13:8774c07f12a5 41 snprintf(buf, size, "%02X:%02X:%02X:%02X:%02X:%02X", addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
va009039 13:8774c07f12a5 42 }
va009039 13:8774c07f12a5 43 void str_mask(char* buf, size_t size) {
va009039 13:8774c07f12a5 44 snprintf(buf, size, "%02X:%02X:%02X:xx:xx:xx", addr[5], addr[4], addr[3]);
va009039 13:8774c07f12a5 45 }
va009039 13:8774c07f12a5 46 bool eq(BD_ADDR* a) {
va009039 13:8774c07f12a5 47 return memcmp(addr, a->addr, 6) == 0;
va009039 13:8774c07f12a5 48 }
va009039 13:8774c07f12a5 49 bool eq_vendor(BD_ADDR* a) {
va009039 13:8774c07f12a5 50 return memcmp(addr+3, a->addr+3, 3) == 0;
va009039 13:8774c07f12a5 51 }
va009039 13:8774c07f12a5 52 };
va009039 13:8774c07f12a5 53
va009039 13:8774c07f12a5 54 struct inquiry_info {
va009039 13:8774c07f12a5 55 BD_ADDR bdaddr;
va009039 13:8774c07f12a5 56 uint8_t page_scan_repetition_mode;
va009039 13:8774c07f12a5 57 uint8_t reserved[2];
va009039 13:8774c07f12a5 58 uint8_t dev_class[3];
va009039 13:8774c07f12a5 59 uint16_t clock_offset;
va009039 13:8774c07f12a5 60 };
va009039 13:8774c07f12a5 61
va009039 13:8774c07f12a5 62 struct inquiry_with_rssi_info { // offset
va009039 13:8774c07f12a5 63 BD_ADDR bdaddr; // +0
va009039 13:8774c07f12a5 64 uint8_t page_scan_repetition_mode;// +6
va009039 13:8774c07f12a5 65 uint8_t reserved[1]; // +7
va009039 13:8774c07f12a5 66 uint8_t class_of_device[3]; // +8
va009039 13:8774c07f12a5 67 uint16_t clock_offset; // +11
va009039 13:8774c07f12a5 68 int8_t rssi; // +13
va009039 13:8774c07f12a5 69 }; // +14
va009039 13:8774c07f12a5 70
va009039 13:8774c07f12a5 71 struct extended_inquiry_info {
va009039 13:8774c07f12a5 72 BD_ADDR bdaddr;
va009039 13:8774c07f12a5 73 uint8_t page_scan_repetition_mode;
va009039 13:8774c07f12a5 74 uint8_t reserved[1];
va009039 13:8774c07f12a5 75 uint8_t class_of_device[3];
va009039 13:8774c07f12a5 76 uint16_t clock_offset;
va009039 13:8774c07f12a5 77 int8_t rssi;
va009039 13:8774c07f12a5 78 uint8_t extended_inquiry_response[240];
va009039 13:8774c07f12a5 79 };
va009039 13:8774c07f12a5 80
va009039 13:8774c07f12a5 81 struct hci_event {
va009039 13:8774c07f12a5 82 uint8_t evt;
va009039 13:8774c07f12a5 83 uint8_t len;
va009039 13:8774c07f12a5 84 uint8_t status;
va009039 13:8774c07f12a5 85 union {
va009039 13:8774c07f12a5 86 uint16_t op;
va009039 13:8774c07f12a5 87 uint8_t data[];
va009039 13:8774c07f12a5 88 } c;
va009039 13:8774c07f12a5 89 };
va009039 13:8774c07f12a5 90 #pragma pack(pop)
va009039 13:8774c07f12a5 91
va009039 13:8774c07f12a5 92 class USBHostRSSI : public IUSBEnumerator {
va009039 13:8774c07f12a5 93 public:
va009039 13:8774c07f12a5 94
va009039 13:8774c07f12a5 95 /**
va009039 13:8774c07f12a5 96 * Constructor
va009039 13:8774c07f12a5 97 */
va009039 13:8774c07f12a5 98 USBHostRSSI();
va009039 13:8774c07f12a5 99
va009039 13:8774c07f12a5 100 /**
va009039 13:8774c07f12a5 101 * Try to connect a BT device
va009039 13:8774c07f12a5 102 *
va009039 13:8774c07f12a5 103 * @return true if connection was successful
va009039 13:8774c07f12a5 104 */
va009039 13:8774c07f12a5 105 bool connect();
va009039 13:8774c07f12a5 106
va009039 13:8774c07f12a5 107 /**
va009039 13:8774c07f12a5 108 * Check if a mouse is connected
va009039 13:8774c07f12a5 109 *
va009039 13:8774c07f12a5 110 * @returns true if a BT is connected
va009039 13:8774c07f12a5 111 */
va009039 13:8774c07f12a5 112 bool connected();
va009039 13:8774c07f12a5 113
va009039 13:8774c07f12a5 114 void attachEvent(void (*ptr)(inquiry_with_rssi_info* info)) {
va009039 13:8774c07f12a5 115 if (ptr != NULL) {
va009039 13:8774c07f12a5 116 onUpdate = ptr;
va009039 13:8774c07f12a5 117 }
va009039 13:8774c07f12a5 118 }
va009039 13:8774c07f12a5 119 //Report* report;
va009039 13:8774c07f12a5 120
va009039 13:8774c07f12a5 121 protected:
va009039 13:8774c07f12a5 122 //From IUSBEnumerator
va009039 13:8774c07f12a5 123 virtual void setVidPid(uint16_t vid, uint16_t pid);
va009039 13:8774c07f12a5 124 virtual bool 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
va009039 13:8774c07f12a5 125 virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
va009039 13:8774c07f12a5 126
va009039 13:8774c07f12a5 127 private:
va009039 13:8774c07f12a5 128 USBHost * host;
va009039 13:8774c07f12a5 129 USBDeviceConnected * dev;
va009039 13:8774c07f12a5 130 USBEndpoint * int_in;
va009039 13:8774c07f12a5 131 bool dev_connected;
va009039 13:8774c07f12a5 132 bool bluetooth_device_found;
va009039 13:8774c07f12a5 133 int bluetooth_intf;
va009039 13:8774c07f12a5 134
va009039 13:8774c07f12a5 135 void rxHandler();
va009039 13:8774c07f12a5 136 void (*onUpdate)(inquiry_with_rssi_info* info);
va009039 13:8774c07f12a5 137 uint8_t int_buf[64];
va009039 13:8774c07f12a5 138 int seq;
va009039 13:8774c07f12a5 139 void event(uint8_t* data, int size);
va009039 13:8774c07f12a5 140 USB_TYPE cmdSend(uint16_t op);
va009039 13:8774c07f12a5 141 USB_TYPE cmdSend(uint16_t op, const char* fmt, ...);
va009039 13:8774c07f12a5 142 USB_TYPE cmdSendSub(uint16_t op, const uint8_t* data, int size);
va009039 13:8774c07f12a5 143 void init();
va009039 13:8774c07f12a5 144 };
va009039 13:8774c07f12a5 145