diff options
Diffstat (limited to 'examples/tinyb')
-rw-r--r-- | examples/tinyb/TinyBTest01.cpp | 106 | ||||
-rw-r--r-- | examples/tinyb/asynctinyb.cpp | 161 | ||||
-rw-r--r-- | examples/tinyb/checkinit.cpp | 44 | ||||
-rw-r--r-- | examples/tinyb/esstinyb.cpp | 209 | ||||
-rw-r--r-- | examples/tinyb/hellotinyb.cpp | 214 | ||||
-rw-r--r-- | examples/tinyb/list_mfg.cpp | 127 | ||||
-rw-r--r-- | examples/tinyb/notifications.cpp | 154 | ||||
-rw-r--r-- | examples/tinyb/uuid.cpp | 69 |
8 files changed, 0 insertions, 1084 deletions
diff --git a/examples/tinyb/TinyBTest01.cpp b/examples/tinyb/TinyBTest01.cpp deleted file mode 100644 index c50ad776..00000000 --- a/examples/tinyb/TinyBTest01.cpp +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Author: Petre Eftime <[email protected]> - * Copyright (c) 2016 Intel Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include <tinyb.hpp> - -#include <vector> -#include <iostream> -#include <thread> -#include <csignal> -#include <condition_variable> - -using namespace tinyb; - -int main(int argc, char **argv) -{ - if (argc < 2) { - std::cerr << "Run as: " << argv[0] << " <device_address>" << std::endl; - exit(1); - } - - BluetoothManager *manager = nullptr; - try { - manager = BluetoothManager::get_bluetooth_manager(); - } catch(const std::runtime_error& e) { - std::cerr << "Error while initializing libtinyb: " << e.what() << std::endl; - exit(1); - } - - /* Start the discovery of devices */ - bool ret = manager->start_discovery(); - std::cout << "Started = " << (ret ? "true" : "false") << std::endl; - - std::unique_ptr<BluetoothGattService> temperature_service; - - std::string device_mac(argv[1]); - auto sensor_tag = manager->find<BluetoothDevice>(nullptr, &device_mac, nullptr, std::chrono::seconds(10)); - if (sensor_tag == nullptr) { - std::cout << "Device not found" << std::endl; - return 1; - } - sensor_tag->enable_connected_notifications([] (BluetoothDevice &d, bool connected, void *usedata) - { if (connected) std::cout << "Connected " << d.get_name() << std::endl; }, NULL); - - if (sensor_tag != nullptr) { - /* Connect to the device and get the list of services exposed by it */ - sensor_tag->connect(); - std::string service_uuid("f000aa00-0451-4000-b000-000000000000"); - std::cout << "Waiting for service " << service_uuid << "to be discovered" << std::endl; - temperature_service = sensor_tag->find(&service_uuid); - } else { - ret = manager->stop_discovery(); - std::cerr << "SensorTag not found after 30 seconds, exiting" << std::endl; - return 1; - } - - /* Stop the discovery (the device was found or timeout was over) */ - ret = manager->stop_discovery(); - std::cout << "Stopped = " << (ret ? "true" : "false") << std::endl; - - auto value_uuid = std::string("f000aa01-0451-4000-b000-000000000000"); - auto temp_value = temperature_service->find(&value_uuid); - - auto config_uuid = std::string("f000aa02-0451-4000-b000-000000000000"); - auto temp_config = temperature_service->find(&config_uuid); - - auto period_uuid = std::string("f000aa03-0451-4000-b000-000000000000"); - auto temp_period = temperature_service->find(&period_uuid); - - /* Activate the temperature measurements */ - std::vector<unsigned char> config_on {0x01}; - temp_config->write_value(config_on); - temp_period->write_value({100}); - temp_value->enable_value_notifications(data_callback, nullptr); - - std::mutex m; - std::unique_lock<std::mutex> lock(m); - - std::signal(SIGINT, signal_handler); - - cv.wait(lock); - - /* Disconnect from the device */ - if (sensor_tag != nullptr) - sensor_tag->disconnect(); -} diff --git a/examples/tinyb/asynctinyb.cpp b/examples/tinyb/asynctinyb.cpp deleted file mode 100644 index f2d5756c..00000000 --- a/examples/tinyb/asynctinyb.cpp +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Author: Petre Eftime <[email protected]> - * Copyright (c) 2015 Intel Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include <tinyb.hpp> - -#include <vector> -#include <iostream> -#include <thread> -#include <atomic> -#include <csignal> - -using namespace tinyb; - - - -/** Converts a raw temperature read from the sensor to a Celsius value. - * @param[in] raw_temp The temperature read from the sensor (two bytes) - * @return The Celsius value of the temperature - */ -static float celsius_temp(uint16_t raw_temp) -{ - const float SCALE_LSB = 0.03125; - return ((float)(raw_temp >> 2)) * SCALE_LSB; -} - -std::atomic<bool> running(true); - -void signal_handler(int signum) -{ - if (signum == SIGINT) { - running = false; - } -} - -/** This program reads the temperature from a - * TI Sensor Tag(http://www.ti.com/ww/en/wireless_connectivity/sensortag2015/?INTC=SensorTag&HQS=sensortag) - * Pass the MAC address of the sensor as the first parameter of the program. - */ -int main(int argc, char **argv) -{ - if (argc < 2) { - std::cerr << "Run as: " << argv[0] << " <device_address>" << std::endl; - exit(1); - } - - BluetoothManager *manager = nullptr; - try { - manager = BluetoothManager::get_bluetooth_manager(); - } catch(const std::runtime_error& e) { - std::cerr << "Error while initializing libtinyb: " << e.what() << std::endl; - exit(1); - } - - /* Start the discovery of devices */ - bool ret = manager->start_discovery(); - std::cout << "Started = " << (ret ? "true" : "false") << std::endl; - - std::unique_ptr<BluetoothGattService> temperature_service; - - std::string device_mac(argv[1]); - auto sensor_tag = manager->find<BluetoothDevice>(nullptr, &device_mac, nullptr, std::chrono::seconds(10)); - if (sensor_tag == nullptr) { - std::cout << "Device not found" << std::endl; - return 1; - } - - if (sensor_tag == nullptr) { - ret = manager->stop_discovery(); - std::cerr << "SensorTag not found after 30 seconds, exiting" << std::endl; - return 1; - } - - /* Connect to the device and get the list of services exposed by it */ - sensor_tag->connect(); - std::string service_uuid("f000aa00-0451-4000-b000-000000000000"); - std::cout << "Waiting for service " << service_uuid << " to be discovered" << std::endl; - temperature_service = sensor_tag->find(&service_uuid); - - /* Stop the discovery (the device was found or timeout was over) */ - ret = manager->stop_discovery(); - std::cout << "Stopped = " << (ret ? "true" : "false") << std::endl; - - auto value_uuid = std::string("f000aa01-0451-4000-b000-000000000000"); - auto temp_value = temperature_service->find(&value_uuid); - - auto config_uuid = std::string("f000aa02-0451-4000-b000-000000000000"); - auto temp_config = temperature_service->find(&config_uuid); - - auto period_uuid = std::string("f000aa03-0451-4000-b000-000000000000"); - auto temp_period = temperature_service->find(&period_uuid); - - /* Activate the temperature measurements */ - try { - std::vector<unsigned char> config_on {0x01}; - temp_config->write_value(config_on); - std::signal(SIGINT, signal_handler); - } catch (std::exception &e) { - std::cout << "Error: " << e.what() << std::endl; - running = false; - } - - while (running) { - /* Read temperature data and display it */ - try { - std::vector<unsigned char> response = temp_value->read_value(); - unsigned char *data; - unsigned int size = response.size(); - if (size > 0) { - data = response.data(); - - std::cout << "Raw data=["; - for (unsigned i = 0; i < response.size(); i++) - std::cout << std::hex << static_cast<int>(data[i]) << ", "; - std::cout << "] "; - - uint16_t ambient_temp, object_temp; - object_temp = data[0] | (data[1] << 8); - ambient_temp = data[2] | (data[3] << 8); - - std::cout << "Ambient temp: " << celsius_temp(ambient_temp) << "C "; - std::cout << "Object temp: " << celsius_temp(object_temp) << "C "; - std::cout << std::endl; - } - - std::this_thread::sleep_for(std::chrono::seconds(1)); - - } catch (std::exception &e) { - std::cout << "Error: " << e.what() << std::endl; - break; - } - } - - /* Disconnect from the device */ - try { - sensor_tag->disconnect(); - } catch (std::exception &e) { - std::cout << "Error: " << e.what() << std::endl; - } - return 0; -} diff --git a/examples/tinyb/checkinit.cpp b/examples/tinyb/checkinit.cpp deleted file mode 100644 index e437ff4a..00000000 --- a/examples/tinyb/checkinit.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Author: Petre Eftime <[email protected]> - * Copyright (c) 2015 Intel Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include <tinyb.hpp> -#include <iostream> - -using namespace tinyb; - -int main(int argc, char **argv) -{ - (void)argc; - (void)argv; - try { - BluetoothManager *manager = BluetoothManager::get_bluetooth_manager(); - (void)manager; - } catch(const std::runtime_error& e) { - std::cout << "Expected error: " << e.what() << std::endl; - return 1; - } - - std::cout << "Initialization was succesful." << std::endl; - return 0; -} diff --git a/examples/tinyb/esstinyb.cpp b/examples/tinyb/esstinyb.cpp deleted file mode 100644 index 9aa302cc..00000000 --- a/examples/tinyb/esstinyb.cpp +++ /dev/null @@ -1,209 +0,0 @@ -/* - * Author: Henry Bruce <[email protected]> - * Copyright (c) 2016 Intel Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -/* - * This sample looks for a device that implements the Environmental Sensing - * Service and supports temperatures notifications. It then starts notfication - * updates and displays samples until CRTL+C is hit. - * Sample has been tested with the following devices: - * - Zephyr Environmental Sensing Profile sample running on Arduino 101 - */ - -#include <tinyb.hpp> -#include <tinyb/BluetoothException.hpp> -#include <jau/basic_types.hpp> -#include <vector> -#include <algorithm> -#include <iostream> -#include <iomanip> -#include <thread> -#include <csignal> -#include <condition_variable> - -using namespace tinyb; - -#define BT_MEAS_INTERVAL_INDEX 6 - -struct es_measurement { - uint16_t reserved; - uint8_t sampling_func; - uint32_t meas_period; - uint32_t update_interval; - uint8_t application; - uint8_t meas_uncertainty; -}; - - -const std::string BT_UUID_ESS = "0000181a-0000-1000-8000-00805f9b34fb"; -const std::string BT_UUID_CUD = "00002901-0000-1000-8000-00805f9b34fb"; -const std::string BT_UUID_TEMPERATURE = "00002a6e-0000-1000-8000-00805f9b34fb"; -const std::string BT_UUID_MEASUREMENT = "0000290c-0000-1000-8000-00805f9b34fb"; -const std::string BT_NOFITY_FLAG = "notify"; - -std::condition_variable cv; - - -static void signal_handler(int signum) -{ - if (signum == SIGINT) { - cv.notify_all(); - } -} - - -static void wait_ctrl_c() -{ - std::mutex m; - std::unique_lock<std::mutex> lock(m); - std::signal(SIGINT, signal_handler); - cv.wait(lock); -} - - -void data_callback(BluetoothGattCharacteristic &c, std::vector<unsigned char> &data, void *userdata) -{ - (void)c; - (void)userdata; - // unsigned char *data_c; - unsigned int size = data.size(); - if (size == 2) { - jau::packed_t<int16_t>* raw_data = reinterpret_cast<jau::packed_t<int16_t>*>( data.data() ); - // int16_t* raw_data = reinterpret_cast<int16_t*>(data.data()); - std::cout << "Raw data = " << std::hex << std::setfill('0') << std::setw(4) << raw_data->store << ". "; - uint16_t temp = (raw_data->store + 50) / 100; - std::cout << "Temperature = " << std::dec << temp << "C " << std::endl; - } -} - - -/** This program reads the temperature from a device running the Environmental Sensing Senvice. - */ -int main(int argc, char **argv) -{ - (void)argc; - (void)argv; - BluetoothManager *manager = nullptr; - try { - manager = BluetoothManager::get_bluetooth_manager(); - } catch(const std::runtime_error& e) { - std::cerr << "Error while initializing libtinyb: " << e.what() << std::endl; - exit(1); - } - - std::unique_ptr<BluetoothAdapter> adapter = manager->get_default_adapter(); - BluetoothDevice *ess_device = NULL; - - // Start the discovery of devices */ - bool ret = manager->start_discovery(); - if (!ret) { - std::cout << "Failed to start discovery" << std::endl; - return 1; - } - std::cout << "Discovering BLE devices"; - size_t num_devices = 0; - bool no_new_devices = false; - std::vector<std::unique_ptr<BluetoothDevice>> list_devices; - while (ess_device == NULL && !no_new_devices) { - list_devices = manager->get_devices(); - std::cout << "." << std::flush; - if (list_devices.size() > 0 && list_devices.size() == num_devices) - no_new_devices = true; - num_devices = list_devices.size(); - - // Look for active ESS device - for (auto it = list_devices.begin(); it != list_devices.end(); ++it) { - if ((*it)->get_rssi() != 0) { - auto list_uuids = (*it)->get_uuids(); - for (auto uuit = list_uuids.begin(); uuit != list_uuids.end(); ++uuit) { - if (*(uuit) == BT_UUID_ESS) { - ess_device = (*it).release(); - break; - } - } - } - } - - if (ess_device == NULL) - std::this_thread::sleep_for(std::chrono::seconds(4)); - } - ret = manager->stop_discovery(); - std::cout << std::endl; - - // Now try to connect - if (ess_device != NULL) { - std::cout << "Connecting to " << ess_device->get_name() << " with addr " << ess_device->get_address() << std::flush; - try { - if (ess_device->connect()) { - std::cout << ". Connected" << std::endl; - } - else - std::cout << ". Failed" << std::endl; - } catch (BluetoothException& e) { - std::cout << std::endl << e.what() << std::endl; - return 1; - } - } else { - std::cout << "ESS device not found." << std::endl; - delete ess_device; - return 1; - } - - if (!ess_device->get_connected()) { - delete ess_device; - return 1; - } - - std::cout << "Getting environmental service" << std::endl; - std::unique_ptr<BluetoothGattService> environmental_service = ess_device->find(const_cast<std::string*>(&BT_UUID_ESS)); - std::cout << "Getting temperature characteristic" << std::endl; - std::unique_ptr<BluetoothGattCharacteristic> temp_characteristic = environmental_service->find(const_cast<std::string*>(&BT_UUID_TEMPERATURE)); - - std::vector<std::string> list_flags = temp_characteristic->get_flags(); - if (std::find(list_flags.begin(), list_flags.end(), BT_NOFITY_FLAG) != list_flags.end()) { - std::unique_ptr<BluetoothGattDescriptor> meas = temp_characteristic->find(const_cast<std::string*>(&BT_UUID_MEASUREMENT)); - std::unique_ptr<BluetoothGattDescriptor> cud = temp_characteristic->find(const_cast<std::string*>(&BT_UUID_CUD)); - std::vector<unsigned char> name_bytes = cud->read_value(); - std::string name(reinterpret_cast<char *>(name_bytes.data()), name_bytes.size()); - std::cout << "Sensor name is '" << name << "'" << std::endl; - std::vector<unsigned char> meas_bytes = meas->read_value(); - int notification_interval = meas_bytes[BT_MEAS_INTERVAL_INDEX]; - std::cout << "Temperature notification interval = " << notification_interval << " secs" << std::endl; - std::cout << "Starting temperature notifications. " << std::endl; - temp_characteristic->enable_value_notifications(data_callback, nullptr); - wait_ctrl_c(); - temp_characteristic->disable_value_notifications(); - } else { - std::cout << "Sensor does not support notifications" << std::endl; - } - - /* Disconnect from the device */ - std::cout << "Disconnecting" << std::endl; - try { - ess_device->disconnect(); - delete ess_device; - } catch (std::exception &e) { - std::cout << "Error: " << e.what() << std::endl; - } - return 0; -} diff --git a/examples/tinyb/hellotinyb.cpp b/examples/tinyb/hellotinyb.cpp deleted file mode 100644 index 49886195..00000000 --- a/examples/tinyb/hellotinyb.cpp +++ /dev/null @@ -1,214 +0,0 @@ -/* - * Author: Petre Eftime <[email protected]> - * Copyright (c) 2015 Intel Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include <tinyb.hpp> - -#include <vector> -#include <iostream> -#include <thread> -#include <atomic> -#include <csignal> - -using namespace tinyb; - -/** Converts a raw temperature read from the sensor to a Celsius value. - * @param[in] raw_temp The temperature read from the sensor (two bytes) - * @return The Celsius value of the temperature - */ -static float celsius_temp(uint16_t raw_temp) -{ - const float SCALE_LSB = 0.03125; - return ((float)(raw_temp >> 2)) * SCALE_LSB; -} - - -std::atomic<bool> running(true); - -void signal_handler(int signum) -{ - if (signum == SIGINT) { - running = false; - } -} - -/** This program reads the temperature from a - * TI Sensor Tag(http://www.ti.com/ww/en/wireless_connectivity/sensortag2015/?INTC=SensorTag&HQS=sensortag) - * Pass the MAC address of the sensor as the first parameter of the program. - */ -int main(int argc, char **argv) -{ - if (argc < 2) { - std::cerr << "Run as: " << argv[0] << " <device_address>" << std::endl; - exit(1); - } - - BluetoothManager *manager = nullptr; - try { - manager = BluetoothManager::get_bluetooth_manager(); - } catch(const std::runtime_error& e) { - std::cerr << "Error while initializing libtinyb: " << e.what() << std::endl; - exit(1); - } - - /* Start the discovery of devices */ - bool ret = manager->start_discovery(); - std::cout << "Started = " << (ret ? "true" : "false") << std::endl; - - BluetoothDevice *sensor_tag = NULL; - BluetoothGattService *temperature_service = NULL; - - for (int i = 0; i < 15; ++i) { - std::cout << "Discovered devices: " << std::endl; - /* Get the list of devices */ - auto list = manager->get_devices(); - - for (auto it = list.begin(); it != list.end(); ++it) { - - std::cout << "Class = " << (*it)->get_class_name() << " "; - std::cout << "Path = " << (*it)->get_object_path() << " "; - std::cout << "Name = " << (*it)->get_name() << " "; - std::cout << "Connected = " << (*it)->get_connected() << " "; - std::cout << std::endl; - - /* Search for the device with the address given as a parameter to the program */ - if ((*it)->get_address() == argv[1]) - sensor_tag = (*it).release(); - } - - /* Free the list of devices and stop if the device was found */ - if (sensor_tag != nullptr) - break; - /* If not, wait and try again */ - std::this_thread::sleep_for(std::chrono::seconds(4)); - std::cout << std::endl; - } - - /* Stop the discovery (the device was found or number of tries ran out */ - ret = manager->stop_discovery(); - std::cout << "Stopped = " << (ret ? "true" : "false") << std::endl; - - if (sensor_tag == nullptr) { - std::cout << "Could not find device " << argv[1] << std::endl; - return 1; - } - - /* Connect to the device and get the list of services exposed by it */ - sensor_tag->connect(); - std::cout << "Discovered services: " << std::endl; - while (true) { - /* Wait for the device to come online */ - std::this_thread::sleep_for(std::chrono::seconds(4)); - - auto list = sensor_tag->get_services(); - if (list.empty()) - continue; - - for (auto it = list.begin(); it != list.end(); ++it) { - std::cout << "Class = " << (*it)->get_class_name() << " "; - std::cout << "Path = " << (*it)->get_object_path() << " "; - std::cout << "UUID = " << (*it)->get_uuid() << " "; - std::cout << "Device = " << (*it)->get_device().get_object_path() << " "; - std::cout << std::endl; - - /* Search for the temperature service, by UUID */ - if ((*it)->get_uuid() == "f000aa00-0451-4000-b000-000000000000") - temperature_service = (*it).release(); - } - break; - } - - if (temperature_service == nullptr) { - std::cout << "Could not find service f000aa00-0451-4000-b000-000000000000" << std::endl; - return 1; - } - - BluetoothGattCharacteristic *temp_value = nullptr; - BluetoothGattCharacteristic *temp_config = nullptr; - BluetoothGattCharacteristic *temp_period = nullptr; - - /* If there is a temperature service on the device with the given UUID, - * get it's characteristics, by UUID again */ - auto list = temperature_service->get_characteristics(); - std::cout << "Discovered characteristics: " << std::endl; - for (auto it = list.begin(); it != list.end(); ++it) { - - std::cout << "Class = " << (*it)->get_class_name() << " "; - std::cout << "Path = " << (*it)->get_object_path() << " "; - std::cout << "UUID = " << (*it)->get_uuid() << " "; - std::cout << "Service = " << (*it)->get_service().get_object_path() << " "; - std::cout << std::endl; - - if ((*it)->get_uuid() == "f000aa01-0451-4000-b000-000000000000") - temp_value = (*it).release(); - else if ((*it)->get_uuid() =="f000aa02-0451-4000-b000-000000000000") - temp_config = (*it).release(); - else if ((*it)->get_uuid() == "f000aa03-0451-4000-b000-000000000000") - temp_period = (*it).release(); - } - - if (temp_config == nullptr || temp_value == nullptr || temp_period == nullptr) { - std::cout << "Could not find characteristics." << std::endl; - return 1; - } - - /* Activate the temperature measurements */ - try { - std::vector<unsigned char> config_on {0x01}; - temp_config->write_value(config_on); - while (running) { - /* Read temperature data and display it */ - std::vector<unsigned char> response = temp_value->read_value(); - unsigned char *data; - unsigned int size = response.size(); - if (size > 0) { - data = response.data(); - - std::cout << "Raw data=["; - for (unsigned i = 0; i < response.size(); i++) - std::cout << std::hex << static_cast<int>(data[i]) << ", "; - std::cout << "] "; - - uint16_t ambient_temp, object_temp; - object_temp = data[0] | (data[1] << 8); - ambient_temp = data[2] | (data[3] << 8); - - std::cout << "Ambient temp: " << celsius_temp(ambient_temp) << "C "; - std::cout << "Object temp: " << celsius_temp(object_temp) << "C "; - std::cout << std::endl; - } - - std::this_thread::sleep_for(std::chrono::seconds(1)); - } - } catch (std::exception &e) { - std::cout << "Error: " << e.what() << std::endl; - } - - /* Disconnect from the device */ - try { - sensor_tag->disconnect(); - } catch (std::exception &e) { - std::cout << "Error: " << e.what() << std::endl; - } - return 0; -} diff --git a/examples/tinyb/list_mfg.cpp b/examples/tinyb/list_mfg.cpp deleted file mode 100644 index d978aa0a..00000000 --- a/examples/tinyb/list_mfg.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Author: Petre Eftime <[email protected]> - * Copyright (c) 2015 Intel Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include <tinyb.hpp> - -#include <vector> -#include <iostream> -#include <thread> -#include <atomic> -#include <csignal> - -using namespace tinyb; - -/** Converts a raw temperature read from the sensor to a Celsius value. - * @param[in] raw_temp The temperature read from the sensor (two bytes) - * @return The Celsius value of the temperature - */ -float celsius_temp(uint16_t raw_temp) -{ - const float SCALE_LSB = 0.03125; - return ((float)(raw_temp >> 2)) * SCALE_LSB; -} - - -std::atomic<bool> running(true); - -void signal_handler(int signum) -{ - if (signum == SIGINT) { - running = false; - } -} - -/** This program reads the temperature from a - * TI Sensor Tag(http://www.ti.com/ww/en/wireless_connectivity/sensortag2015/?INTC=SensorTag&HQS=sensortag) - * Pass the MAC address of the sensor as the first parameter of the program. - */ -int main(int argc, char **argv) -{ - (void)argc; // FIXME - (void)argv; // FIXME - - BluetoothManager *manager = nullptr; - try { - manager = BluetoothManager::get_bluetooth_manager(); - } catch(const std::runtime_error& e) { - std::cerr << "Error while initializing libtinyb: " << e.what() << std::endl; - exit(1); - } - - /* Start the discovery of devices */ - bool ret = manager->start_discovery(); - std::cout << "Started = " << (ret ? "true" : "false") << std::endl; - - BluetoothDevice *sensor_tag = NULL; - BluetoothGattService *temperature_service = NULL; - - (void)sensor_tag; // FIXME - (void)temperature_service; // FIXME - - for (;;) { - std::cout << "Discovered devices: " << std::endl; - /* Get the list of devices */ - auto list = manager->get_devices(); - - for (auto it = list.begin(); it != list.end(); ++it) { - - std::cout << "Class = " << (*it)->get_class_name() << " "; - std::cout << "Path = " << (*it)->get_object_path() << " "; - std::cout << "Name = " << (*it)->get_name() << " "; - std::cout << "Connected = " << (*it)->get_connected() << " "; - std::cout << std::endl; - - auto mfg = (*it)->get_manufacturer_data(); - - if (!mfg.empty()) { - std::cout << "MFG" << std::endl; - for(auto it2: mfg) { - std::cout << "\t" << it2.first << " = [ "; - for (auto arr_it: it2.second) { - std::cout << (int) arr_it << ", "; - } - std::cout << "]" << std::endl; - } - } - - auto service_data = (*it)->get_service_data(); - - if (!service_data.empty()) { - std::cout << "Service Data" << std::endl; - for(auto it2: service_data) { - std::cout << "\t" << it2.first << " = [ "; - for (auto arr_it: it2.second) { - std::cout << (int) arr_it << ", "; - } - std::cout << "]" << std::endl; - } - } - - } - - /* If not, wait and try again */ - std::this_thread::sleep_for(std::chrono::seconds(4)); - std::cout << std::endl; - } -} diff --git a/examples/tinyb/notifications.cpp b/examples/tinyb/notifications.cpp deleted file mode 100644 index 52290ffc..00000000 --- a/examples/tinyb/notifications.cpp +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Author: Petre Eftime <[email protected]> - * Copyright (c) 2016 Intel Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include <tinyb.hpp> - -#include <vector> -#include <iostream> -#include <thread> -#include <csignal> -#include <condition_variable> - -using namespace tinyb; - -std::condition_variable cv; - -/** Converts a raw temperature read from the sensor to a Celsius value. - * @param[in] raw_temp The temperature read from the sensor (two bytes) - * @return The Celsius value of the temperature - */ -static float celsius_temp(uint16_t raw_temp) -{ - const float SCALE_LSB = 0.03125; - return ((float)(raw_temp >> 2)) * SCALE_LSB; -} - -void data_callback(BluetoothGattCharacteristic &c, std::vector<unsigned char> &data, void *userdata) -{ - (void)c; - (void)userdata; - /* Read temperature data and display it */ - unsigned char *data_c; - unsigned int size = data.size(); - if (size > 0) { - data_c = data.data(); - - std::cout << "Raw data=["; - for (unsigned i = 0; i < size; i++) - std::cout << std::hex << static_cast<int>(data_c[i]) << ", "; - std::cout << "] "; - - uint16_t ambient_temp, object_temp; - object_temp = data_c[0] | (data_c[1] << 8); - ambient_temp = data_c[2] | (data_c[3] << 8); - - std::cout << "Ambient temp: " << celsius_temp(ambient_temp) << "C "; - std::cout << "Object temp: " << celsius_temp(object_temp) << "C "; - std::cout << std::endl; - } -} - -void signal_handler(int signum) -{ - if (signum == SIGINT) { - cv.notify_all(); - } -} - -/** This program reads the temperature from a - * TI Sensor Tag(http://www.ti.com/ww/en/wireless_connectivity/sensortag2015/?INTC=SensorTag&HQS=sensortag) - * Pass the MAC address of the sensor as the first parameter of the program. - */ -int main(int argc, char **argv) -{ - if (argc < 2) { - std::cerr << "Run as: " << argv[0] << " <device_address>" << std::endl; - exit(1); - } - - BluetoothManager *manager = nullptr; - try { - manager = BluetoothManager::get_bluetooth_manager(); - } catch(const std::runtime_error& e) { - std::cerr << "Error while initializing libtinyb: " << e.what() << std::endl; - exit(1); - } - - /* Start the discovery of devices */ - bool ret = manager->start_discovery(); - std::cout << "Started = " << (ret ? "true" : "false") << std::endl; - - std::unique_ptr<BluetoothGattService> temperature_service; - - std::string device_mac(argv[1]); - auto sensor_tag = manager->find<BluetoothDevice>(nullptr, &device_mac, nullptr, std::chrono::seconds(10)); - if (sensor_tag == nullptr) { - std::cout << "Device not found" << std::endl; - return 1; - } - sensor_tag->enable_connected_notifications([] (BluetoothDevice &d, bool connected, void *userdata) - { (void)userdata; if (connected) std::cout << "Connected " << d.get_name() << std::endl; }, NULL); - - if (sensor_tag != nullptr) { - /* Connect to the device and get the list of services exposed by it */ - sensor_tag->connect(); - std::string service_uuid("f000aa00-0451-4000-b000-000000000000"); - std::cout << "Waiting for service " << service_uuid << "to be discovered" << std::endl; - temperature_service = sensor_tag->find(&service_uuid); - } else { - ret = manager->stop_discovery(); - std::cerr << "SensorTag not found after 30 seconds, exiting" << std::endl; - return 1; - } - - /* Stop the discovery (the device was found or timeout was over) */ - ret = manager->stop_discovery(); - std::cout << "Stopped = " << (ret ? "true" : "false") << std::endl; - - auto value_uuid = std::string("f000aa01-0451-4000-b000-000000000000"); - auto temp_value = temperature_service->find(&value_uuid); - - auto config_uuid = std::string("f000aa02-0451-4000-b000-000000000000"); - auto temp_config = temperature_service->find(&config_uuid); - - auto period_uuid = std::string("f000aa03-0451-4000-b000-000000000000"); - auto temp_period = temperature_service->find(&period_uuid); - - /* Activate the temperature measurements */ - std::vector<unsigned char> config_on {0x01}; - temp_config->write_value(config_on); - temp_period->write_value({100}); - temp_value->enable_value_notifications(data_callback, nullptr); - - std::mutex m; - std::unique_lock<std::mutex> lock(m); - - std::signal(SIGINT, signal_handler); - - cv.wait(lock); - - /* Disconnect from the device */ - if (sensor_tag != nullptr) - sensor_tag->disconnect(); -} diff --git a/examples/tinyb/uuid.cpp b/examples/tinyb/uuid.cpp deleted file mode 100644 index db5452bb..00000000 --- a/examples/tinyb/uuid.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include<string> -#include<string.h> -#include<cstdlib> -#include<iostream> -#include<stdio.h> -#include<iomanip> -#include<stdint.h> -#include <tinyb.hpp> - -using namespace tinyb; - -int main(int argc, char **argv) { - - if( argc <= 2 ) { - exit(1); - } - std::string uuid_string(argv[1]); - - std::cout << uuid_string << std::endl; -/* - uint64_t uuid[2]; - if (uuid_string.size() == 4 || uuid_string.size() == 8) { - // 16bit UUID - uuid[0] = strtoul(uuid_string.c_str(), NULL, 16) << 32 | 0x00001000UL; - uuid[1] = (0x80000080ULL << 32) | 0x5f9b34fbUL; - } else if (uuid_string.size() == 36) { - // 128bit UUID - char u[37]; - strcpy(u, uuid_string.c_str()); - - if (u[9] == '-') { - u[9] = ' '; - uuid[0] = strtoul(u + 0, NULL, 16) << 32; - } else - return 1; - if (u[13] == '-') { - u[13] = ' '; - uuid[0] = uuid[0] | strtoul(u + 10, NULL, 16) << 16; - } else - return 1; - if (u[17] == '-') { - u[17] = ' '; - uuid[0] = uuid[0] | strtoul(u + 14, NULL, 16); - } else - return 1; - - if (u[21] == '-') { - u[21] = ' '; - uuid[1] = strtoul(u + 18, NULL, 16) << 48; - } else - return 1; - uuid[1] = uuid[1] | strtoul(u + 22, NULL, 16); - } else - return 1; - - printf("%08lx-%04lx-%04lx-%04lx-%012lx\n", - (uuid[0] >> 32), - ((uuid[0] >> 16) & 0xFFFFULL), - (uuid[0] & 0xFFFFULL), - (uuid[1] >> 48), - (uuid[1] & ~(0xFFFFULL << 48))); -*/ - BluetoothUUID uuid1(uuid_string); - BluetoothUUID uuid2(argv[1]); - - std::cout << uuid1.get_string() << " " << uuid2.get_string() << std::endl; - - return 0; -} |