local fix version of myBlueUSB (http://mbed.org/users/networker/code/myBlueUSB/). - merge deleted files which are required to compile. - enable echo back of received data via RFCOMM.
Dependencies: AvailableMemory FatFileSystem mbed myUSBHost
ftclasslibusbdevbt.cpp@2:9f25a7fa1a54, 2013-12-08 (annotated)
- Committer:
- nobukuma
- Date:
- Sun Dec 08 21:52:09 2013 +0000
- Revision:
- 2:9f25a7fa1a54
- Parent:
- 0:003889bc474f
???BT??????????????????; ?????????????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nobukuma | 0:003889bc474f | 1 | #include "mbed.h" |
nobukuma | 0:003889bc474f | 2 | #include <vector> |
nobukuma | 0:003889bc474f | 3 | #include "Utils.h" |
nobukuma | 0:003889bc474f | 4 | #include "hci.h" |
nobukuma | 0:003889bc474f | 5 | #include "ftclasslibusbdevbt.h" |
nobukuma | 0:003889bc474f | 6 | |
nobukuma | 0:003889bc474f | 7 | //extern HCI* gHCI; |
nobukuma | 0:003889bc474f | 8 | class application; |
nobukuma | 0:003889bc474f | 9 | extern application App; |
nobukuma | 0:003889bc474f | 10 | |
nobukuma | 0:003889bc474f | 11 | void printf(const BD_ADDR* addr); |
nobukuma | 0:003889bc474f | 12 | |
nobukuma | 0:003889bc474f | 13 | void ftdev::receive(int socket, SocketState state, const u8* data, int len) { |
nobukuma | 0:003889bc474f | 14 | printf("ftdev::receive was called: socket %d, state=%d, length=%d\n", socket, state, len); |
nobukuma | 0:003889bc474f | 15 | // unsigned char req[] = "\xdget_ser_num\xd"; |
nobukuma | 0:003889bc474f | 16 | unsigned char req[] = {0xbe, 0xef, 6, 0xfc, 0,0,0,0,0, 0xaf}; |
nobukuma | 0:003889bc474f | 17 | if (len==0) { |
nobukuma | 0:003889bc474f | 18 | switch (state) { |
nobukuma | 0:003889bc474f | 19 | case SocketState_Opening: |
nobukuma | 0:003889bc474f | 20 | break; |
nobukuma | 0:003889bc474f | 21 | case SocketState_Open: |
nobukuma | 0:003889bc474f | 22 | printf("sending request \n%s\n", req); |
nobukuma | 0:003889bc474f | 23 | Socket_Send(sock, req, sizeof(req)); |
nobukuma | 0:003889bc474f | 24 | break; |
nobukuma | 0:003889bc474f | 25 | case SocketState_Closing: |
nobukuma | 0:003889bc474f | 26 | case SocketState_Closed: |
nobukuma | 0:003889bc474f | 27 | return; |
nobukuma | 0:003889bc474f | 28 | } |
nobukuma | 0:003889bc474f | 29 | } else { |
nobukuma | 0:003889bc474f | 30 | //printHex(data, len); |
nobukuma | 0:003889bc474f | 31 | parse(data, len); |
nobukuma | 0:003889bc474f | 32 | if (state==SocketState_Open) |
nobukuma | 0:003889bc474f | 33 | ;//Socket_Close(sock);//replace with ft primitive |
nobukuma | 0:003889bc474f | 34 | } |
nobukuma | 0:003889bc474f | 35 | } |
nobukuma | 0:003889bc474f | 36 | |
nobukuma | 0:003889bc474f | 37 | unsigned short ftdev::chksum() { |
nobukuma | 0:003889bc474f | 38 | unsigned short sum = (X1_len & 0xFF) + (X1_len >> 8); |
nobukuma | 0:003889bc474f | 39 | for (int i = 0; i < X1_len; i++) |
nobukuma | 0:003889bc474f | 40 | sum += X1_pkt[i]; |
nobukuma | 0:003889bc474f | 41 | return -sum; |
nobukuma | 0:003889bc474f | 42 | } |
nobukuma | 0:003889bc474f | 43 | |
nobukuma | 0:003889bc474f | 44 | void ftdev::parse (const unsigned char *buf, unsigned len) { |
nobukuma | 0:003889bc474f | 45 | unsigned i = 0; |
nobukuma | 0:003889bc474f | 46 | while (i < len) { |
nobukuma | 0:003889bc474f | 47 | char c = buf[i++]; |
nobukuma | 0:003889bc474f | 48 | switch (parseState) { |
nobukuma | 0:003889bc474f | 49 | case 0: //ascii state |
nobukuma | 0:003889bc474f | 50 | if (c==2) |
nobukuma | 0:003889bc474f | 51 | parseState = 1; |
nobukuma | 0:003889bc474f | 52 | else |
nobukuma | 0:003889bc474f | 53 | putc(c, stdout); |
nobukuma | 0:003889bc474f | 54 | break; |
nobukuma | 0:003889bc474f | 55 | case 1: |
nobukuma | 0:003889bc474f | 56 | if (c==0x55) |
nobukuma | 0:003889bc474f | 57 | parseState = 2; |
nobukuma | 0:003889bc474f | 58 | else { |
nobukuma | 0:003889bc474f | 59 | parseState = 0; |
nobukuma | 0:003889bc474f | 60 | printf("expected 0x55 in X1 header, found %02X\n", c); |
nobukuma | 0:003889bc474f | 61 | } |
nobukuma | 0:003889bc474f | 62 | break; |
nobukuma | 0:003889bc474f | 63 | case 2: |
nobukuma | 0:003889bc474f | 64 | X1_len = c<<8; |
nobukuma | 0:003889bc474f | 65 | parseState= 3; |
nobukuma | 0:003889bc474f | 66 | break; |
nobukuma | 0:003889bc474f | 67 | case 3: |
nobukuma | 0:003889bc474f | 68 | X1_len += c; |
nobukuma | 0:003889bc474f | 69 | parseState= 4; |
nobukuma | 0:003889bc474f | 70 | X1_pkt = new unsigned char[X1_len]; |
nobukuma | 0:003889bc474f | 71 | X1_pos = 0; |
nobukuma | 0:003889bc474f | 72 | break; |
nobukuma | 0:003889bc474f | 73 | case 4: |
nobukuma | 0:003889bc474f | 74 | if (X1_pos < X1_len) X1_pkt[X1_pos++] = c; |
nobukuma | 0:003889bc474f | 75 | else parseState = 5; |
nobukuma | 0:003889bc474f | 76 | break; |
nobukuma | 0:003889bc474f | 77 | case 5: |
nobukuma | 0:003889bc474f | 78 | X1_crc = c<<8; |
nobukuma | 0:003889bc474f | 79 | parseState= 6; |
nobukuma | 0:003889bc474f | 80 | break; |
nobukuma | 0:003889bc474f | 81 | case 6: |
nobukuma | 0:003889bc474f | 82 | X1_crc += c; |
nobukuma | 0:003889bc474f | 83 | parseState= 7; |
nobukuma | 0:003889bc474f | 84 | break; |
nobukuma | 0:003889bc474f | 85 | case 7: |
nobukuma | 0:003889bc474f | 86 | if (c == 3 && X1_crc == chksum()) { |
nobukuma | 0:003889bc474f | 87 | //handlePkt(); |
nobukuma | 0:003889bc474f | 88 | printHex(X1_pkt, X1_len); |
nobukuma | 0:003889bc474f | 89 | }else |
nobukuma | 0:003889bc474f | 90 | printf("framing or checksum error, end char = %02X\n", c); |
nobukuma | 0:003889bc474f | 91 | |
nobukuma | 0:003889bc474f | 92 | parseState = 0; |
nobukuma | 0:003889bc474f | 93 | break; |
nobukuma | 0:003889bc474f | 94 | } |
nobukuma | 0:003889bc474f | 95 | } |
nobukuma | 0:003889bc474f | 96 | } |
nobukuma | 0:003889bc474f | 97 | |
nobukuma | 0:003889bc474f | 98 | vector<ftbtdev*> ft_devs; |
nobukuma | 0:003889bc474f | 99 | ftdev _ftdev; //single instance, just for test |
nobukuma | 0:003889bc474f | 100 | |
nobukuma | 0:003889bc474f | 101 | int GetNrOfFtBtDevices() { |
nobukuma | 0:003889bc474f | 102 | return ft_devs.size(); |
nobukuma | 0:003889bc474f | 103 | } |
nobukuma | 0:003889bc474f | 104 | |
nobukuma | 0:003889bc474f | 105 | unsigned InitFtBtDeviceList() {//assume inquiry has been done |
nobukuma | 0:003889bc474f | 106 | static char FtDevClass[3] = {0x00, 0x1F, 0x82 }; |
nobukuma | 0:003889bc474f | 107 | BTDevice* devs[8]; |
nobukuma | 0:003889bc474f | 108 | int count = ((HCI*)&App)->GetDevices(devs,8); |
nobukuma | 0:003889bc474f | 109 | int n = 0; |
nobukuma | 0:003889bc474f | 110 | for (int i = 0; i < count; i++) { |
nobukuma | 0:003889bc474f | 111 | if (memcmp(devs[i]->_info.dev_class, FtDevClass, 3)==0) { |
nobukuma | 0:003889bc474f | 112 | ft_devs.push_back(new ftbtdev(&devs[i]->_info)); |
nobukuma | 0:003889bc474f | 113 | printf("%d: %s\n", n++, devs[i]->_name); |
nobukuma | 0:003889bc474f | 114 | } |
nobukuma | 0:003889bc474f | 115 | // printf("device %d (handle=%d) ", i, devs[i]->_handle); |
nobukuma | 0:003889bc474f | 116 | // printfBytes("devclass: ", devs[i]->_info.dev_class, 3); |
nobukuma | 0:003889bc474f | 117 | } |
nobukuma | 0:003889bc474f | 118 | return n; |
nobukuma | 0:003889bc474f | 119 | } |
nobukuma | 0:003889bc474f | 120 | |
nobukuma | 0:003889bc474f | 121 | ftbtdev* GetFtUsbDeviceHandle(unsigned Num) { |
nobukuma | 0:003889bc474f | 122 | if (Num < ft_devs.size()) { |
nobukuma | 0:003889bc474f | 123 | return ft_devs[Num]; |
nobukuma | 0:003889bc474f | 124 | } |
nobukuma | 0:003889bc474f | 125 | return 0; |
nobukuma | 0:003889bc474f | 126 | } |
nobukuma | 0:003889bc474f | 127 | |
nobukuma | 0:003889bc474f | 128 | unsigned OpenFtBtDevice(ftbtdev* d) { |
nobukuma | 0:003889bc474f | 129 | BD_ADDR* bd = d->BtAddr(); |
nobukuma | 0:003889bc474f | 130 | printf("Connecting to "); |
nobukuma | 0:003889bc474f | 131 | printf(bd); |
nobukuma | 0:003889bc474f | 132 | printf("\n"); |
nobukuma | 0:003889bc474f | 133 | return _ftdev.Open(bd, 1); //TODO: everything can go wrong here |
nobukuma | 0:003889bc474f | 134 | } |