Kris Scholte Lubberink
/
SSS_Ble
Ble for smart sOlutions
Diff: source/BleDeviceCentral.h
- Revision:
- 6:ee9c86f06eae
- Child:
- 7:9cda1b0f25ae
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/BleDeviceCentral.h Mon May 20 09:55:38 2019 +0200 @@ -0,0 +1,143 @@ +// +// Created by kris on 20-4-19. +// + +#ifndef SSS_BLE_BLEDEVICECENTRAL_H +#define SSS_BLE_BLEDEVICECENTRAL_H + +#include <AdvertisingDataParser.h> +#include "BleDevice.h" +#include "list" + +class BleDeviceCentral : public BleDevice { +public: + BleDeviceCentral(BLE &ble, events::EventQueue &event_queue) + : BleDevice(ble, event_queue) {} + std::list<ble::address_t::byte_array_t> addresses ; + + virtual void start() + { + ble::ScanParameters params; + ble_error_t error = _ble.gap().setScanParameters(params); + + if (error) { + print_error(error, "Error in Gap::startScan %d\r\n"); + return; + } +// +// /* start scanning, results will be handled by onAdvertisingReport */ + error = _ble.gap().startScan(); + + + if (error) { + print_error(error, "Error in Gap::startScan %d\r\n"); + return; + } + } + virtual void stop(){ + printf("[CENTRAL]\t Stopping \r\n"); + //TODO: Destroy the list with addresses + //FIXME: Out-of-memory error + addresses.clear(); + _ble.gap().stopScan(); + if (_ble.hasInitialized()) { + _ble.shutdown(); + } + } + +private: + /* Gap::EventHandler */ + + /** Look at scan payload to find a peer device and connect to it */ + virtual void onAdvertisingReport(const ble::AdvertisingReportEvent &event) + { + /* don't bother with analysing scan result if we're already connecting */ + if (_is_connecting) { + return; + } +// printf("found someone "); + /* parse the advertising payload, looking for a discoverable device */ + //TODO: Hier checken ofhet een andere SSS wearable is. Zo niet, gewoon tellen. + bool found = false; + ble::address_t::byte_array_t actualdata = event.getPeerAddress().data(); + + //TODO: Start service discovery. + ble::AdvertisingDataParser adv_data(event.getPayload()); + while (adv_data.hasNext()) { +// printf("\n\t\t Has next"); + ble::AdvertisingDataParser::element_t field = adv_data.next(); + + /* connect to a discoverable device */ + //Deze if-statement kijkt naar de naam en MEMCMP vergelijkt de advertising-naam met de target-naam (sssble) + //Als het SSS BLE is, moet ik dus de services gaan ontdekken en de interest-service uitlezen. Dit is altijd een array van 5 bytes, allemaal + /*if (field.type == ble::adv_data_type_t::COMPLETE_LOCAL_NAME && + field.value.size() == strlen("SSS BLE") && + (memcmp(field.value.data(), "SSS BLE", field.value.size()) == 0)) {*/ + if (field.type == ble::adv_data_type_t::COMPLETE_LOCAL_NAME){ + if (field.type == ble::adv_data_type_t::COMPLETE_LOCAL_NAME && + field.value.size() == strlen("SSS BLE") && + (memcmp(field.value.data(), "SSS BLE", field.value.size()) == 0)) { + printf("Found another wearable, let's try connecting to him."); + + + ble_error_t error = _ble.gap().stopScan(); + + if (error) { + print_error(error, "Error caused by Gap::stopScan"); + return; + } + + const ble::ConnectionParameters connection_params; + + + error = _ble.gap().connect( + event.getPeerAddressType(), + event.getPeerAddress(), + connection_params + ); + + if (error) { + _ble.gap().startScan(); + return; + } + + } + + char test[field.value.size()]; + memcpy(test, field.value.data(), field.value.size()); + + printf("\n\t\t Adv from: "); + printf("%s", test); + printf(" rssi: %d, scan response: %u, connectable: %u\r\n", + event.getRssi(), event.getType().scan_response(), event.getType().connectable()); + + + /* we may have already scan events waiting + * to be processed so we need to remember + * that we are already connecting and ignore them */ +// _is_connecting = true; + + return; + } + } + + if(addresses.size() > 0){ + found = (std::find(addresses.begin(), addresses.end(),actualdata ) != addresses.end()); + } + + if (!found) { + //TODO: Clear the list to prevent memory overflow + addresses.push_back(actualdata); + printf("\r\n Amount devices: %d \r\n", addresses.size()); + +// printf("%02x:%02x:%02x:%02x:%02x:%02x\r\n", actualdata[5], actualdata[4], actualdata[3], actualdata[2], actualdata[1], actualdata[0]); + return; + } else { +// TODO: Increment internal counter + } + } + +}; + + +#endif //SSS_BLE_BLEDEVICECENTRAL_H