1/* 2 * Author: Jon Trulson <jtrulson@ics.com> 3 * Copyright (c) 2015 Intel Corporation. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be 14 * included in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 25#include <unistd.h> 26#include <iostream> 27#include <signal.h> 28#include <stdio.h> 29#include "hm11.h" 30 31using namespace std; 32using namespace upm; 33 34void printUsage(char *progname) 35{ 36 cout << "Usage: " << progname << " [AT command]" << endl; 37 cout << endl; 38 39 cout << "If an argument is supplied on the command line, that argument is" 40 << endl; 41 cout << "sent to the module and the response is printed out." << endl; 42 cout << endl; 43 cout << "If no argument is used, then the address and PIN of the module" 44 << endl; 45 cout << "are queried and the results printed out." << endl; 46 cout << endl; 47 cout << endl; 48} 49 50// simple helper function to send a command and wait for a response 51void sendCommand(upm::HM11* ble, char *cmd) 52{ 53 char buffer[BUFSIZ]; 54 ble->writeData(cmd, strlen(cmd)); 55 56 // wait up to 1 second 57 if (ble->dataAvailable(1000)) 58 { 59 memset(buffer, 0, BUFSIZ); 60 61 ble->readData(buffer, BUFSIZ - 1); 62 cout << "Returned: " << buffer << endl; 63 } 64 else 65 { 66 cerr << "Timed out waiting for response" << endl; 67 } 68} 69 70 71int main (int argc, char **argv) 72{ 73//! [Interesting] 74 char buffer[BUFSIZ]; 75 // Instantiate a HM11 BLE Module on UART 0 76 77 upm::HM11* ble = new upm::HM11(0); 78 79 // make sure port is initialized properly. 9600 baud is the default. 80 if (!ble->setupTty(B9600)) 81 { 82 cerr << "Failed to setup tty port parameters" << endl; 83 return 1; 84 } 85 86 printUsage(argv[0]); 87 88 if (argc > 1) 89 { 90 cout << "Sending command line argument (" << argv[1] << ")..." << endl; 91 sendCommand(ble, argv[1]); 92 } 93 else 94 { 95 // query the module address 96 char addr[] = "AT+ADDR?"; 97 cout << "Querying module address (" << addr << ")..." << endl; 98 sendCommand(ble, addr); 99 100 sleep(1); 101 102 // query the module address 103 char pin[] = "AT+PASS?"; 104 cout << "Querying module PIN (" << pin << ")..." << endl; 105 sendCommand(ble, pin); 106 107 // Other potentially useful commands are: 108 // 109 // AT+VERS? - query module version 110 // AT+ROLE0 - set as slave 111 // AT+ROLE1 - set as master 112 // AT+CLEAR - clear all previous settings 113 // AT+RESET - restart the device 114 // 115 // A comprehensive list is available from the datasheet at: 116 // http://www.seeedstudio.com/wiki/images/c/cd/Bluetooth4_en.pdf 117 } 118 119//! [Interesting] 120 121 delete ble; 122 return 0; 123} 124