Test
source/main.cpp
- Committer:
- HelGast95
- Date:
- 2019-01-22
- Revision:
- 73:a91805f9e9f0
- Parent:
- 43:fb2855f7754b
- Child:
- 74:12b9444a2fb4
File content as of revision 73:a91805f9e9f0:
#define END_OF_JSON 0xFE
#include "mbed.h"
#include "picojson.h"
#include "stats_report.h"
/* Librerías para BLE - Servicio GATT */
#include "ble/BLE.h"
#include "ble/Gap.h"
#include "ble/DiscoveredCharacteristic.h"
#include "ble/DiscoveredService.h"
DiscoveredCharacteristic testServiceptr;
bool serviceDiscovered = false;
DigitalOut led3Test(LED3);
DigitalOut led4BLE(LED4);
Serial pcSerial(USBTX, USBRX); // Abrimos conexión serial con el puerto USB
EventQueue eventQueue;
Thread threadLED(osPriorityAboveNormal1, 400);
//Thread threadSerial(osPriorityAboveNormal2, 2000);
Thread threadBLE(osPriorityRealtime3, 2000);
int getNum(char ch)
{
int num=0;
if(ch>='0' && ch<='9')
{
num=ch-0x30;
}
else
{
switch(ch)
{
case 'A': case 'a': num=10; break;
case 'B': case 'b': num=11; break;
case 'C': case 'c': num=12; break;
case 'D': case 'd': num=13; break;
case 'E': case 'e': num=14; break;
case 'F': case 'f': num=15; break;
default: num=0;
}
}
return num;
}
//function : hex2int
//this function will return integer value against
//hexValue - which is in string format
unsigned int hex2int(unsigned char hex[])
{
unsigned int x=0;
x=(getNum(hex[0]))*16+(getNum(hex[1]));
return x;
}
/**
* Tarea encargada de parpadear un LED continuamente
*/
void blinkLED3() {
while(true) {
led3Test = !led3Test;
wait(0.2);
}
}
/**
* Método encargado de enviar un string por el puerto serie char a char
*/
void sendCharArrayToSerial(char const *array, Serial *serial) {
int i = 0;
while(array[i] != '\0') {
serial->putc(array[i]);
i++;
}
serial->putc('\0');
}
/**
* Tarea encragada de enviar JSONs por el puerto serie
*/
void sendJsonOverSerial() {
/* Contruimos el objeto JSON */
picojson::object worker;
picojson::object vehicle;
picojson::object device;
picojson::object event;
picojson::object data;
string tmpValue = "1234";
char tmp[1024];
string str;
char final = END_OF_JSON; // Caracter que indica el final del JSON
while(true) {
worker["idDevice"] = picojson::value(tmpValue);
tmpValue = "12-12-2019";
worker["tsLastSeen"] = picojson::value(tmpValue);
tmpValue = "Baby Driver";
worker["role"] = picojson::value(tmpValue);
tmpValue = "12345";
worker["idVehicle"] = picojson::value(tmpValue);
tmpValue = "123456";
vehicle["idDevice"] = picojson::value(tmpValue);
tmpValue = "18-12-2019";
vehicle["tsLastSeen"] = picojson::value(tmpValue);
tmpValue = "11111";
device["idDevice"] = picojson::value(tmpValue);
device["battLevel"] = picojson::value(45.0);
tmpValue = "Pulsera";
device["deviceType"] = picojson::value(tmpValue);
tmpValue = "256745";
event["idEvent"] = picojson::value(tmpValue);
event["hazardousDevice"] = picojson::value(false);
event["affectedDevice"] = picojson::value(true);
tmpValue = "Critical";
event["eventType"] = picojson::value(tmpValue);
tmpValue = "19-12-2018";
event["tsEvent"] = picojson::value(tmpValue);
data["worker"] = picojson::value(worker);
data["vehicle"] = picojson::value(vehicle);
data["device"] = picojson::value(device);
data["event"] = picojson::value(event);
str = picojson::value(data).serialize();
// Convertimos el string a char *
strncpy(tmp, str.c_str(), sizeof(tmp));
strncat(tmp, &final, sizeof(final)); // Añadimos el caracter al final
tmp[sizeof(tmp) - 1] = 0;
//sendCharArrayToSerial(tmp, &pcSerial);
wait(0.5);
}
}
void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
if (params->peerAddr[0] != 0x8E) return;
printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
params->rssi, params->isScanResponse, params->type);
BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
}
void serviceDiscoveryCallback(const DiscoveredService *service) {
if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());
} else {
printf("S UUID-");
const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID();
for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
printf("%02x", longUUIDBytes[i]);
}
printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());
}
}
void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
if (characteristicP->getUUID().getShortUUID() == 0xa001) { /* !ALERT! Alter this filter to suit your device. */
testServiceptr = *characteristicP;
serviceDiscovered = true;
}
}
void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
printf("terminated SD for handle %u\r\n", connectionHandle);
}
void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
if (params->role == Gap::CENTRAL) {
BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xA000, 0xA001);
}
}
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
/* Si se desconecta el dispositivo, volvemos a entrar en estado Advertising*/
(void) params;
printf("Desconectado. Se comienza la fase de escaneo de nuevo\n\r");
serviceDiscovered = false;
BLE::Instance().gap().startScan(advertisementCallback);
}
void onDataReadClientCallback(const GattReadCallbackParams *response) {
if (response->handle == testServiceptr.getValueHandle()) {
printf("onDataReadClientCallback: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len);
for (unsigned index = 0; index < response->len; index++) {
printf("[%02x]", response->data[index]);
}
printf("\r\n");
}
}
/**
* Esta función se llama si ha habido algún error en el proceso de inicialización del BLE
*/
void onBleInitError(BLE &ble, ble_error_t error) {
printf("Ha ocurrido un error al inicializar la configuracion del BLE\n");
}
void printMacAddress() {
/* Print out device MAC address to the console*/
Gap::AddressType_t addr_type;
Gap::Address_t address;
BLE::Instance().gap().getAddress(&addr_type, address);
printf("DEVICE MAC ADDRESS: ");
for (int i = 5; i >= 1; i--){
printf("%02x:", address[i]);
}
printf("%02x\r\n", address[0]);
}
/**
* Callback triggered when the ble initialization process has finished
*/
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
BLE& ble = params->ble;
ble_error_t error = params->error;
if (error != BLE_ERROR_NONE) {
/* In case of error, forward the error handling to onBleInitError */
onBleInitError(ble, error);
return;
}
/* Ensure that it is the default instance of BLE */
if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
return;
}
ble.gap().onConnection(connectionCallback);
ble.gap().onDisconnection(disconnectionCallback);
ble.gattClient().onDataRead(onDataReadClientCallback);
ble.gap().setScanParams(500, 400);
ble.gap().startScan(advertisementCallback);
//printMacAddress();
}
void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
BLE &ble = BLE::Instance();
eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
}
void readVoltageValue() {
BLE &ble = BLE::Instance();
if (serviceDiscovered && !ble.gattClient().isServiceDiscoveryActive()) {
testServiceptr.read();
}
}
void BLEServiceManagment() {
eventQueue.call_every(2000, readVoltageValue);
BLE &ble = BLE::Instance();
ble.onEventsToProcess(scheduleBleEventsProcessing);
ble.init(bleInitComplete);
eventQueue.dispatch_forever();
}
int main() {
threadLED.start(blinkLED3);
threadBLE.start(BLEServiceManagment);
//threadSerial.start(sendJsonOverSerial);
threadLED.join();
threadBLE.join();
//threadSerial.join();
return 0;
}