Components / BluetoothSerial

Dependents:   Shield_Seeed_Bluetooth

Fork of BluetoothSerial by Yihui Xiong

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BluetoothSerial.cpp Source File

BluetoothSerial.cpp

00001 #include "BluetoothSerial.h"
00002 #include <string.h>
00003 
00004 #define LOG(args...)   // std::printf(args)
00005 
00006 BluetoothSerial::BluetoothSerial(PinName tx, PinName rx) : _serial(tx, rx)
00007 {   
00008 
00009 }
00010 
00011 void BluetoothSerial::setup()
00012 {
00013      _serial.baud(BLUETOOTH_SERIAL_DEFAULT_BAUD);
00014 }
00015 
00016 void BluetoothSerial::setup(int baud)
00017 {
00018      _serial.baud(baud);
00019 }
00020 
00021 
00022 void BluetoothSerial::master(const char *name, uint8_t autoc)
00023 {
00024     _serial.puts("\r\n+STWMOD=1\r\n");
00025     _serial.printf("\r\n+STNA=%s\r\n", name);
00026     _serial.printf("\r\n+STAUTO=%d\r\n", autoc ? 1 : 0);
00027 }
00028 
00029 
00030 void BluetoothSerial::slave(const char *name, uint8_t autoc, uint8_t oaut)
00031 {
00032     _serial.puts("\r\n+STWMOD=0\r\n");
00033     _serial.printf("\r\n+STNA=%s\r\n", name);
00034     _serial.printf("\r\n+STOAUT=%d\r\n", oaut ? 1 : 0);
00035     _serial.printf("\r\n+STAUTO=%d\r\n", autoc ? 1 : 0);
00036 }
00037 
00038 void BluetoothSerial::pin(int pin)
00039 {
00040     _serial.puts("\r\n+STWMOD=0\r\n");
00041     _serial.printf("\r\n+STPIN=%s\r\n", pin);
00042 }
00043 
00044 void BluetoothSerial::pin(const char *pin)
00045 {
00046     _serial.puts("\r\n+STWMOD=0\r\n");
00047     _serial.printf("\r\n+STPIN=%s\r\n", pin);
00048 }
00049 
00050 int BluetoothSerial::connect()
00051 {
00052     clear();
00053     _serial.puts("\r\n+INQ=1\r\n"); // Make the bluetooth module inquirable
00054     LOG("BT: INQUIRING\r\n");
00055     
00056     const char *prefix = "CONNECT:";
00057     uint8_t prefix_len = sizeof("CONNECT:") - 1;
00058     for (uint8_t i = 0; i < 12; i++) {
00059         int len = readline(_buf, sizeof(_buf));
00060         if (len > 0) {
00061             LOG("%s\r\n", _buf);
00062             if (!memcmp(_buf, prefix, prefix_len)) {    // check prefix
00063                 const char *suffix = "OK";
00064                 uint8_t suffix_len = sizeof("OK") - 1;
00065                 
00066                 if (!memcmp(_buf + prefix_len, suffix, suffix_len)) { // check suffix
00067                     LOG("BT: CONNECTED\r\n");
00068                     return 1;
00069                 }
00070                 
00071                 suffix = "FAIL";
00072                 suffix_len = sizeof("FAIL") - 1;
00073                 
00074                 if (!memcmp(_buf + prefix_len, suffix, suffix_len)) { // check suffix
00075                     return 0;
00076                 }
00077             }
00078         }
00079     }
00080     
00081     return 0;
00082 }
00083 
00084 int BluetoothSerial::connect(const char *name)
00085 {
00086     char *mac;
00087     int name_len = strlen(name);
00088     
00089     clear();
00090     _serial.puts("\r\n+INQ=1\r\n");
00091     LOG("BT: INQUERING\r\n");
00092     while (1) {
00093         int len = readline(_buf, sizeof(_buf));     // +RTINQ=XX,XX,X,X,X,X;DEVICE_NAME
00094         if (len > 0) {
00095             LOG("%s\r\n", _buf);
00096             if (!memcmp(_buf, "+RTINQ=", sizeof("+RTINQ=") - 1)) {  // check prefix
00097                 
00098                 if (!memcmp(_buf + len - name_len, name, name_len)) { // check suffix
00099                     _buf[len - name_len - 1] = '\0';
00100                     mac = (char*)_buf + sizeof("+RTINQ=") - 1;
00101                     LOG("Connecting device: %s\r\n", mac);
00102                     
00103                     break;
00104                 }
00105             }
00106         }
00107     }
00108     
00109     LOG("BT: CONNECTING\r\n");
00110     _serial.printf("\r\n+CONN=%s\r\n", mac);
00111     
00112     const char *prefix = "CONNECT:";
00113     int prefix_len = sizeof("CONNECT:") - 1;
00114     for (uint8_t i = 0; i < 6; i++) {
00115         int len = readline(_buf, sizeof(_buf), 0);
00116         if (len >= 0) {
00117             LOG("%s\r\n", _buf);
00118             if (!memcmp(_buf, prefix, prefix_len)) {    // check prefix
00119                 const char *suffix = "OK";
00120                 uint8_t suffix_len = sizeof("OK") - 1;
00121                 
00122                 if (!memcmp(_buf + prefix_len, suffix, suffix_len)) { // check suffix
00123                     LOG("BT: CONNECTED\r\n");
00124                     return 1;
00125                 }
00126                 
00127                 suffix = "FAIL";
00128                 suffix_len = sizeof("FAIL") - 1;
00129                 
00130                 if (!memcmp(_buf + prefix_len, suffix, suffix_len)) { // check suffix
00131                     LOG("TB: CONNECTION FAILED\r\n");
00132                     return 0;
00133                 }
00134             }
00135         }
00136     }
00137     
00138     return 0;
00139 }
00140     
00141 
00142 int BluetoothSerial::_getc()
00143 {
00144     return _serial.getc();
00145 }
00146 
00147 int BluetoothSerial::_putc(int c)
00148 {
00149     return _serial.putc(c);
00150 }
00151 
00152 int BluetoothSerial::readline(uint8_t *buf, int len, uint32_t timeout)
00153 {
00154     int get = 0;
00155     int count = timeout;
00156     while (count >= 0) {
00157         if (_serial.readable()) {
00158             char c = _serial.getc();
00159             buf[get] = c;
00160             if (c == '\n' && get && buf[get - 1] == '\r') {
00161                 buf[get - 1] = '\0';
00162                 return get - 1;
00163             }
00164             get++;
00165             if (get >= len) {
00166                 LOG("Too long line, the buffer is not enough\r\n");
00167                 return -(get + 1);
00168             }
00169 
00170             count = timeout;
00171         }
00172         
00173         if (timeout != 0) {
00174             count--;
00175         }
00176     }
00177 
00178     return -(get + 1);
00179 }
00180 
00181 void BluetoothSerial::clear()
00182 {
00183     int count = 0;
00184     
00185     LOG("Clear previous command output\r\n");
00186     do {
00187         count++;
00188         if (_serial.readable()) {
00189             int get = _serial.getc();
00190             count = 0;
00191             
00192             LOG("%c", get);
00193         }
00194     } while (count < BLUETOOTH_SERIAL_TIMEOUT);
00195     LOG("done\r\n");
00196 }
00197 
00198 
00199