Javier Vargas
/
ControllerBLE
Test
Diff: source/main.cpp
- Revision:
- 76:596c9924e51b
- Parent:
- 75:6606a580ebc4
- Child:
- 77:48c622bf903f
diff -r 6606a580ebc4 -r 596c9924e51b source/main.cpp --- a/source/main.cpp Wed Jan 23 14:17:18 2019 +0000 +++ b/source/main.cpp Thu Jan 24 11:47:57 2019 +0000 @@ -56,66 +56,64 @@ * Thread encargado de enviar JSONs por el puerto serie */ void sendJsonOverSerial() { - picojson::object event; - char tmp[50]; // Vble auxiliar para el tratamiento de cadenas de char. + char tmp[512]; // Vble auxiliar para el tratamiento de cadenas de char. string str; char final = END_OF_JSON; - while(true) { // Esperamos a un mensaje en la cola - osEvent evt = BLEqueue.get(); - if (evt.status == osEventMessage) { - BLEdata_t *BLEdata = (BLEdata_t*) evt.value.p; - - printf("\r\nDato recibido BLE\r\n"); - event["id"] = picojson::value(BLEdata->id); - printf("Id: %s\r\n", BLEdata->id.c_str()); - str = "Voltaje"; - event["type"] = picojson::value(str); - event["data"] = picojson::value(BLEdata->voltaje); - - BLEmpool.free(BLEdata); - - printf("Se serializa el string\r\n"); - str = picojson::value(event).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; - - printf("Se envia un evento por el puesto serie\r\n"); - sendCharArrayToSerial(tmp, &pcSerial); - } + picojson::object event; + picojson::object extraInfo; + int type = rand() % 3; + int idEvent = rand() % 500; + event["idEvent"] = picojson::value((double) idEvent); + event["type"] = picojson::value((double) type); + str = "E9:ED:F4:AC:BF:8E"; + extraInfo["hazardousDevice"] = picojson::value(str); + str = "D5:62:12:BF:B8:45"; + if(type != 0) extraInfo["affectedDevice"] = picojson::value(str); + event["extraInfo"] = picojson::value(extraInfo); - wait(1); + //printf("Se serializa el string\r\n"); + str = picojson::value(event).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; + + //printf("Se envia un evento por el puesto serie\r\n"); + sendCharArrayToSerial(tmp, &pcSerial); + + wait(2); } } void scanCallback(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()); + //printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); } else { - printf("S UUID-"); + //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("%02x", longUUIDBytes[i]); } - printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); + //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()); + //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) { testServiceptr = *characteristicP; serviceDiscovered = true; @@ -123,7 +121,7 @@ } void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { - printf("terminated SD for handle %u\r\n", connectionHandle); + //printf("terminated SD for handle %u\r\n", connectionHandle); } void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { @@ -136,37 +134,28 @@ void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { /* Si se desconecta el dispositivo, volvemos a entrar en estado Advertising*/ - printf("Desconectado. Se comienza la fase de escaneo de nuevo\n\r"); + //printf("Desconectado. Se comienza la fase de escaneo de nuevo\n\r"); serviceDiscovered = false; BLE::Instance().gap().startScan(scanCallback); } void onDataReadClientCallback(const GattReadCallbackParams *response) { if (response->handle == testServiceptr.getValueHandle()) { - printf("\r\n\r\nonDataReadClientCallback: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len); + //printf("\r\n\r\nonDataReadClientCallback: 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("[%02x]", response->data[index]); } - printf("\r\n"); - printf("Que pasa\r\n"); + //printf("\r\n"); BLEdata_t *BLEdata = BLEmpool.alloc(); - printf("Eyyyyy\r\n"); int r = rand() % 500; - printf("Eyyyyy1.5\r\n"); char str[12]; - printf("Eyyyyy1.6\r\n"); sprintf(str, "%d", r); - printf("Eyyyyy1.7\r\n"); BLEdata->id = str; - printf("Eyyyyy2\r\n"); uint16_t tensionaux; tensionaux = ((response->data[1]) << 8) + (response->data[0]); - printf("Eyyyyy2.1\r\n"); BLEdata->voltaje = (tensionaux * 1.0 )/100; - printf("Eyyyyy2.2\r\n"); - BLEqueue.put(BLEdata); - printf("Eyyyyy3\r\n"); + BLEqueue.put(BLEdata); } } @@ -174,7 +163,7 @@ * 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"); + //printf("Ha ocurrido un error al inicializar la configuracion del BLE\n"); } void printMacAddress() { @@ -242,15 +231,10 @@ int main() { srand(time(NULL)); threadLED.start(blinkLED3); - threadBLE.start(BLEServiceManagment); + //threadBLE.start(BLEServiceManagment); threadSerial.start(sendJsonOverSerial); threadLED.join(); threadBLE.join(); threadSerial.join(); - - while(true) { - wait(100); - } - return 0; } \ No newline at end of file