Based on myBlueUSB and rosserial_mbed
Dependencies: mbed myUSBHost AvailableMemory myBlueUSB
btserial.cpp@0:7684b95768c7, 2011-09-17 (annotated)
- Committer:
- OTL
- Date:
- Sat Sep 17 14:21:35 2011 +0000
- Revision:
- 0:7684b95768c7
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
OTL | 0:7684b95768c7 | 1 | #include "btserial.h" |
OTL | 0:7684b95768c7 | 2 | |
OTL | 0:7684b95768c7 | 3 | btserial::btserial(char ba[6], char ch) { |
OTL | 0:7684b95768c7 | 4 | L2CAPAddr a; |
OTL | 0:7684b95768c7 | 5 | memcpy(&a.bdaddr, ba, 6); |
OTL | 0:7684b95768c7 | 6 | a.psm = ch; |
OTL | 0:7684b95768c7 | 7 | sendptr = 0; |
OTL | 0:7684b95768c7 | 8 | recptrin = 0; |
OTL | 0:7684b95768c7 | 9 | recptrout = bufsize - 1; |
OTL | 0:7684b95768c7 | 10 | free = bufsize; |
OTL | 0:7684b95768c7 | 11 | sock = Socket_Open(SOCKET_RFCOM, &a.hdr, cb, this); |
OTL | 0:7684b95768c7 | 12 | } |
OTL | 0:7684b95768c7 | 13 | |
OTL | 0:7684b95768c7 | 14 | btserial::btserial(char ch) { |
OTL | 0:7684b95768c7 | 15 | sendptr = 0; |
OTL | 0:7684b95768c7 | 16 | recptrin = 0; |
OTL | 0:7684b95768c7 | 17 | recptrout = bufsize - 1; |
OTL | 0:7684b95768c7 | 18 | free = bufsize; |
OTL | 0:7684b95768c7 | 19 | sock = Socket_Listen(SOCKET_RFCOM, ch, cb, this); |
OTL | 0:7684b95768c7 | 20 | } |
OTL | 0:7684b95768c7 | 21 | |
OTL | 0:7684b95768c7 | 22 | void btserial::cb(int socket, SocketState state, const unsigned char *data, int len, void* userData) { |
OTL | 0:7684b95768c7 | 23 | btserial *self = (btserial*)userData; |
OTL | 0:7684b95768c7 | 24 | if (state == SocketState_Open) |
OTL | 0:7684b95768c7 | 25 | if (len > 0) |
OTL | 0:7684b95768c7 | 26 | self->stash(data, len); |
OTL | 0:7684b95768c7 | 27 | else { |
OTL | 0:7684b95768c7 | 28 | //Socket_GetOpt(sock, SO_RECBUF, &recbufsize, sizeof(recbufsize)); |
OTL | 0:7684b95768c7 | 29 | //Socket_GetOpt(sock, SO_SNDBUF, &sndbufsize, sizeof(sndbufsize)); |
OTL | 0:7684b95768c7 | 30 | port_settings ps;//defaults are ok |
OTL | 0:7684b95768c7 | 31 | ps.baud = 3; //9600 baud |
OTL | 0:7684b95768c7 | 32 | ps.bits = 3;//8 bits |
OTL | 0:7684b95768c7 | 33 | ps.stop = 0;//1 bit |
OTL | 0:7684b95768c7 | 34 | ps.par = 0; //no parity |
OTL | 0:7684b95768c7 | 35 | ps.mask = MASK_BITRATE|MASK_DATABITS|MASK_STOPBITS|MASK_PARITYBITS; |
OTL | 0:7684b95768c7 | 36 | //set_remote_port_parameters(sock, &ps); |
OTL | 0:7684b95768c7 | 37 | self->open = true; |
OTL | 0:7684b95768c7 | 38 | } |
OTL | 0:7684b95768c7 | 39 | else if (state == SocketState_Closed) |
OTL | 0:7684b95768c7 | 40 | self->open = false; |
OTL | 0:7684b95768c7 | 41 | } |
OTL | 0:7684b95768c7 | 42 | |
OTL | 0:7684b95768c7 | 43 | void btserial::stash(const unsigned char *data, int len) { |
OTL | 0:7684b95768c7 | 44 | int i = 0; |
OTL | 0:7684b95768c7 | 45 | while (i < len && free>0) { |
OTL | 0:7684b95768c7 | 46 | recbuf[recptrin++] = data[i++]; |
OTL | 0:7684b95768c7 | 47 | if (recptrin == bufsize) recptrin = 0; |
OTL | 0:7684b95768c7 | 48 | free--; |
OTL | 0:7684b95768c7 | 49 | } |
OTL | 0:7684b95768c7 | 50 | } |
OTL | 0:7684b95768c7 | 51 | |
OTL | 0:7684b95768c7 | 52 | int btserial::getc() { |
OTL | 0:7684b95768c7 | 53 | if (free == bufsize || !open) |
OTL | 0:7684b95768c7 | 54 | return -1; |
OTL | 0:7684b95768c7 | 55 | free++; |
OTL | 0:7684b95768c7 | 56 | recptrout++; |
OTL | 0:7684b95768c7 | 57 | if (recptrout == bufsize) recptrout = 0; |
OTL | 0:7684b95768c7 | 58 | return recbuf[recptrout]; |
OTL | 0:7684b95768c7 | 59 | } |
OTL | 0:7684b95768c7 | 60 | |
OTL | 0:7684b95768c7 | 61 | int btserial::putc(int c) { |
OTL | 0:7684b95768c7 | 62 | if (sendptr==bufsize || !open) |
OTL | 0:7684b95768c7 | 63 | return -1; |
OTL | 0:7684b95768c7 | 64 | sendbuf[sendptr++] = c; |
OTL | 0:7684b95768c7 | 65 | if (sendptr==bufsize || c=='\n' || c=='\r') { |
OTL | 0:7684b95768c7 | 66 | Socket_Send(sock, sendbuf, sendptr); |
OTL | 0:7684b95768c7 | 67 | sendptr = 0; |
OTL | 0:7684b95768c7 | 68 | } |
OTL | 0:7684b95768c7 | 69 | return c; |
OTL | 0:7684b95768c7 | 70 | } |
OTL | 0:7684b95768c7 | 71 | |
OTL | 0:7684b95768c7 | 72 | int btserial::write(u8* data, int len) { |
OTL | 0:7684b95768c7 | 73 | Socket_Send(sock, data, len); |
OTL | 0:7684b95768c7 | 74 | } |
OTL | 0:7684b95768c7 | 75 | |
OTL | 0:7684b95768c7 | 76 | void btserial::baud(int br) { |
OTL | 0:7684b95768c7 | 77 | int rates[] = {2400,4800,7200,9600,19200,38400,57600,115200,230400}; |
OTL | 0:7684b95768c7 | 78 | if (!open) return; |
OTL | 0:7684b95768c7 | 79 | for (int i = 0; i < sizeof(rates)/sizeof(int);i++) |
OTL | 0:7684b95768c7 | 80 | if (rates[i] == br) { |
OTL | 0:7684b95768c7 | 81 | port_settings ps; |
OTL | 0:7684b95768c7 | 82 | ps.baud = i; |
OTL | 0:7684b95768c7 | 83 | ps.mask = MASK_BITRATE; |
OTL | 0:7684b95768c7 | 84 | set_remote_port_parameters(sock, &ps); |
OTL | 0:7684b95768c7 | 85 | return; |
OTL | 0:7684b95768c7 | 86 | } |
OTL | 0:7684b95768c7 | 87 | printf("Illegal baudrate requested %d\n", br); |
OTL | 0:7684b95768c7 | 88 | } |
OTL | 0:7684b95768c7 | 89 | |
OTL | 0:7684b95768c7 | 90 | void btserial::format(int bits, Serial::Parity par, int stop) { |
OTL | 0:7684b95768c7 | 91 | if (!open) return; |
OTL | 0:7684b95768c7 | 92 | port_settings ps; |
OTL | 0:7684b95768c7 | 93 | ps.bits = bits-5; |
OTL | 0:7684b95768c7 | 94 | ps.stop = stop-1; |
OTL | 0:7684b95768c7 | 95 | switch (par) { |
OTL | 0:7684b95768c7 | 96 | case Serial::None: |
OTL | 0:7684b95768c7 | 97 | ps.par = 0; |
OTL | 0:7684b95768c7 | 98 | ps.par_t = 0; |
OTL | 0:7684b95768c7 | 99 | break; |
OTL | 0:7684b95768c7 | 100 | case Serial::Odd: |
OTL | 0:7684b95768c7 | 101 | ps.par = 1; |
OTL | 0:7684b95768c7 | 102 | ps.par_t = 0; |
OTL | 0:7684b95768c7 | 103 | break; |
OTL | 0:7684b95768c7 | 104 | case Serial::Even: |
OTL | 0:7684b95768c7 | 105 | ps.par = 1; |
OTL | 0:7684b95768c7 | 106 | ps.par_t = 1; |
OTL | 0:7684b95768c7 | 107 | break; |
OTL | 0:7684b95768c7 | 108 | case Serial::Forced0: |
OTL | 0:7684b95768c7 | 109 | ps.par = 1; |
OTL | 0:7684b95768c7 | 110 | ps.par_t = 3; |
OTL | 0:7684b95768c7 | 111 | break; |
OTL | 0:7684b95768c7 | 112 | case Serial::Forced1: |
OTL | 0:7684b95768c7 | 113 | ps.par = 1; |
OTL | 0:7684b95768c7 | 114 | ps.par_t = 2; |
OTL | 0:7684b95768c7 | 115 | break; |
OTL | 0:7684b95768c7 | 116 | } |
OTL | 0:7684b95768c7 | 117 | ps.mask = MASK_DATABITS|MASK_STOPBITS|MASK_PARITYBITS|MASK_PARITYTYPE; |
OTL | 0:7684b95768c7 | 118 | set_remote_port_parameters(sock, &ps); |
OTL | 0:7684b95768c7 | 119 | } |
OTL | 0:7684b95768c7 | 120 |