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