Test

Committer:
HelGast95
Date:
Tue Jan 22 08:36:23 2019 +0000
Revision:
73:a91805f9e9f0
Parent:
43:fb2855f7754b
Child:
74:12b9444a2fb4
Primera version concurrencia con servicio GATT BLE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HelGast95 73:a91805f9e9f0 1 #define END_OF_JSON 0xFE
mbed_official 1:72c60abef7e7 2
HelGast95 73:a91805f9e9f0 3 #include "mbed.h"
HelGast95 73:a91805f9e9f0 4 #include "picojson.h"
HelGast95 73:a91805f9e9f0 5 #include "stats_report.h"
HelGast95 73:a91805f9e9f0 6
HelGast95 73:a91805f9e9f0 7 /* Librerías para BLE - Servicio GATT */
mbed_official 1:72c60abef7e7 8 #include "ble/BLE.h"
mbed_official 1:72c60abef7e7 9 #include "ble/Gap.h"
HelGast95 73:a91805f9e9f0 10 #include "ble/DiscoveredCharacteristic.h"
HelGast95 73:a91805f9e9f0 11 #include "ble/DiscoveredService.h"
HelGast95 73:a91805f9e9f0 12
HelGast95 73:a91805f9e9f0 13 DiscoveredCharacteristic testServiceptr;
HelGast95 73:a91805f9e9f0 14
HelGast95 73:a91805f9e9f0 15 bool serviceDiscovered = false;
HelGast95 73:a91805f9e9f0 16
HelGast95 73:a91805f9e9f0 17 DigitalOut led3Test(LED3);
HelGast95 73:a91805f9e9f0 18 DigitalOut led4BLE(LED4);
HelGast95 73:a91805f9e9f0 19 Serial pcSerial(USBTX, USBRX); // Abrimos conexión serial con el puerto USB
HelGast95 73:a91805f9e9f0 20
HelGast95 73:a91805f9e9f0 21 EventQueue eventQueue;
mbed_official 1:72c60abef7e7 22
HelGast95 73:a91805f9e9f0 23 Thread threadLED(osPriorityAboveNormal1, 400);
HelGast95 73:a91805f9e9f0 24 //Thread threadSerial(osPriorityAboveNormal2, 2000);
HelGast95 73:a91805f9e9f0 25 Thread threadBLE(osPriorityRealtime3, 2000);
HelGast95 73:a91805f9e9f0 26
HelGast95 73:a91805f9e9f0 27 int getNum(char ch)
HelGast95 73:a91805f9e9f0 28 {
HelGast95 73:a91805f9e9f0 29 int num=0;
HelGast95 73:a91805f9e9f0 30 if(ch>='0' && ch<='9')
HelGast95 73:a91805f9e9f0 31 {
HelGast95 73:a91805f9e9f0 32 num=ch-0x30;
HelGast95 73:a91805f9e9f0 33 }
HelGast95 73:a91805f9e9f0 34 else
HelGast95 73:a91805f9e9f0 35 {
HelGast95 73:a91805f9e9f0 36 switch(ch)
HelGast95 73:a91805f9e9f0 37 {
HelGast95 73:a91805f9e9f0 38 case 'A': case 'a': num=10; break;
HelGast95 73:a91805f9e9f0 39 case 'B': case 'b': num=11; break;
HelGast95 73:a91805f9e9f0 40 case 'C': case 'c': num=12; break;
HelGast95 73:a91805f9e9f0 41 case 'D': case 'd': num=13; break;
HelGast95 73:a91805f9e9f0 42 case 'E': case 'e': num=14; break;
HelGast95 73:a91805f9e9f0 43 case 'F': case 'f': num=15; break;
HelGast95 73:a91805f9e9f0 44 default: num=0;
HelGast95 73:a91805f9e9f0 45 }
HelGast95 73:a91805f9e9f0 46 }
HelGast95 73:a91805f9e9f0 47 return num;
HelGast95 73:a91805f9e9f0 48 }
HelGast95 73:a91805f9e9f0 49
HelGast95 73:a91805f9e9f0 50 //function : hex2int
HelGast95 73:a91805f9e9f0 51 //this function will return integer value against
HelGast95 73:a91805f9e9f0 52 //hexValue - which is in string format
HelGast95 73:a91805f9e9f0 53
HelGast95 73:a91805f9e9f0 54 unsigned int hex2int(unsigned char hex[])
HelGast95 73:a91805f9e9f0 55 {
HelGast95 73:a91805f9e9f0 56 unsigned int x=0;
HelGast95 73:a91805f9e9f0 57 x=(getNum(hex[0]))*16+(getNum(hex[1]));
HelGast95 73:a91805f9e9f0 58 return x;
HelGast95 73:a91805f9e9f0 59
HelGast95 73:a91805f9e9f0 60 }
HelGast95 73:a91805f9e9f0 61
HelGast95 73:a91805f9e9f0 62 /**
HelGast95 73:a91805f9e9f0 63 * Tarea encargada de parpadear un LED continuamente
HelGast95 73:a91805f9e9f0 64 */
HelGast95 73:a91805f9e9f0 65 void blinkLED3() {
HelGast95 73:a91805f9e9f0 66 while(true) {
HelGast95 73:a91805f9e9f0 67 led3Test = !led3Test;
HelGast95 73:a91805f9e9f0 68 wait(0.2);
HelGast95 73:a91805f9e9f0 69 }
HelGast95 73:a91805f9e9f0 70 }
HelGast95 73:a91805f9e9f0 71
HelGast95 73:a91805f9e9f0 72 /**
HelGast95 73:a91805f9e9f0 73 * Método encargado de enviar un string por el puerto serie char a char
HelGast95 73:a91805f9e9f0 74 */
HelGast95 73:a91805f9e9f0 75 void sendCharArrayToSerial(char const *array, Serial *serial) {
HelGast95 73:a91805f9e9f0 76 int i = 0;
HelGast95 73:a91805f9e9f0 77 while(array[i] != '\0') {
HelGast95 73:a91805f9e9f0 78 serial->putc(array[i]);
HelGast95 73:a91805f9e9f0 79 i++;
HelGast95 73:a91805f9e9f0 80 }
HelGast95 73:a91805f9e9f0 81 serial->putc('\0');
HelGast95 73:a91805f9e9f0 82 }
mbed_official 1:72c60abef7e7 83
HelGast95 73:a91805f9e9f0 84 /**
HelGast95 73:a91805f9e9f0 85 * Tarea encragada de enviar JSONs por el puerto serie
HelGast95 73:a91805f9e9f0 86 */
HelGast95 73:a91805f9e9f0 87 void sendJsonOverSerial() {
HelGast95 73:a91805f9e9f0 88 /* Contruimos el objeto JSON */
HelGast95 73:a91805f9e9f0 89 picojson::object worker;
HelGast95 73:a91805f9e9f0 90 picojson::object vehicle;
HelGast95 73:a91805f9e9f0 91 picojson::object device;
HelGast95 73:a91805f9e9f0 92 picojson::object event;
HelGast95 73:a91805f9e9f0 93 picojson::object data;
HelGast95 73:a91805f9e9f0 94
HelGast95 73:a91805f9e9f0 95 string tmpValue = "1234";
HelGast95 73:a91805f9e9f0 96 char tmp[1024];
HelGast95 73:a91805f9e9f0 97 string str;
HelGast95 73:a91805f9e9f0 98 char final = END_OF_JSON; // Caracter que indica el final del JSON
HelGast95 73:a91805f9e9f0 99
HelGast95 73:a91805f9e9f0 100 while(true) {
HelGast95 73:a91805f9e9f0 101 worker["idDevice"] = picojson::value(tmpValue);
HelGast95 73:a91805f9e9f0 102 tmpValue = "12-12-2019";
HelGast95 73:a91805f9e9f0 103 worker["tsLastSeen"] = picojson::value(tmpValue);
HelGast95 73:a91805f9e9f0 104 tmpValue = "Baby Driver";
HelGast95 73:a91805f9e9f0 105 worker["role"] = picojson::value(tmpValue);
HelGast95 73:a91805f9e9f0 106 tmpValue = "12345";
HelGast95 73:a91805f9e9f0 107 worker["idVehicle"] = picojson::value(tmpValue);
HelGast95 73:a91805f9e9f0 108
HelGast95 73:a91805f9e9f0 109 tmpValue = "123456";
HelGast95 73:a91805f9e9f0 110 vehicle["idDevice"] = picojson::value(tmpValue);
HelGast95 73:a91805f9e9f0 111 tmpValue = "18-12-2019";
HelGast95 73:a91805f9e9f0 112 vehicle["tsLastSeen"] = picojson::value(tmpValue);
HelGast95 73:a91805f9e9f0 113
HelGast95 73:a91805f9e9f0 114 tmpValue = "11111";
HelGast95 73:a91805f9e9f0 115 device["idDevice"] = picojson::value(tmpValue);
HelGast95 73:a91805f9e9f0 116 device["battLevel"] = picojson::value(45.0);
HelGast95 73:a91805f9e9f0 117 tmpValue = "Pulsera";
HelGast95 73:a91805f9e9f0 118 device["deviceType"] = picojson::value(tmpValue);
HelGast95 73:a91805f9e9f0 119
HelGast95 73:a91805f9e9f0 120 tmpValue = "256745";
HelGast95 73:a91805f9e9f0 121 event["idEvent"] = picojson::value(tmpValue);
HelGast95 73:a91805f9e9f0 122 event["hazardousDevice"] = picojson::value(false);
HelGast95 73:a91805f9e9f0 123 event["affectedDevice"] = picojson::value(true);
HelGast95 73:a91805f9e9f0 124 tmpValue = "Critical";
HelGast95 73:a91805f9e9f0 125 event["eventType"] = picojson::value(tmpValue);
HelGast95 73:a91805f9e9f0 126 tmpValue = "19-12-2018";
HelGast95 73:a91805f9e9f0 127 event["tsEvent"] = picojson::value(tmpValue);
HelGast95 73:a91805f9e9f0 128
HelGast95 73:a91805f9e9f0 129 data["worker"] = picojson::value(worker);
HelGast95 73:a91805f9e9f0 130 data["vehicle"] = picojson::value(vehicle);
HelGast95 73:a91805f9e9f0 131 data["device"] = picojson::value(device);
HelGast95 73:a91805f9e9f0 132 data["event"] = picojson::value(event);
HelGast95 73:a91805f9e9f0 133
HelGast95 73:a91805f9e9f0 134 str = picojson::value(data).serialize();
HelGast95 73:a91805f9e9f0 135
HelGast95 73:a91805f9e9f0 136 // Convertimos el string a char *
HelGast95 73:a91805f9e9f0 137 strncpy(tmp, str.c_str(), sizeof(tmp));
HelGast95 73:a91805f9e9f0 138 strncat(tmp, &final, sizeof(final)); // Añadimos el caracter al final
HelGast95 73:a91805f9e9f0 139 tmp[sizeof(tmp) - 1] = 0;
mbed_official 1:72c60abef7e7 140
HelGast95 73:a91805f9e9f0 141 //sendCharArrayToSerial(tmp, &pcSerial);
HelGast95 73:a91805f9e9f0 142
HelGast95 73:a91805f9e9f0 143 wait(0.5);
HelGast95 73:a91805f9e9f0 144 }
HelGast95 73:a91805f9e9f0 145 }
HelGast95 73:a91805f9e9f0 146
HelGast95 73:a91805f9e9f0 147 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
HelGast95 73:a91805f9e9f0 148 if (params->peerAddr[0] != 0x8E) return;
HelGast95 73:a91805f9e9f0 149 printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
HelGast95 73:a91805f9e9f0 150 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
HelGast95 73:a91805f9e9f0 151 params->rssi, params->isScanResponse, params->type);
HelGast95 73:a91805f9e9f0 152
HelGast95 73:a91805f9e9f0 153 BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
HelGast95 73:a91805f9e9f0 154 }
mbed_official 1:72c60abef7e7 155
HelGast95 73:a91805f9e9f0 156 void serviceDiscoveryCallback(const DiscoveredService *service) {
HelGast95 73:a91805f9e9f0 157 if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
HelGast95 73:a91805f9e9f0 158 printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());
HelGast95 73:a91805f9e9f0 159 } else {
HelGast95 73:a91805f9e9f0 160 printf("S UUID-");
HelGast95 73:a91805f9e9f0 161 const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID();
HelGast95 73:a91805f9e9f0 162 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
HelGast95 73:a91805f9e9f0 163 printf("%02x", longUUIDBytes[i]);
HelGast95 73:a91805f9e9f0 164 }
HelGast95 73:a91805f9e9f0 165 printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());
HelGast95 73:a91805f9e9f0 166 }
HelGast95 73:a91805f9e9f0 167 }
HelGast95 73:a91805f9e9f0 168
HelGast95 73:a91805f9e9f0 169 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
HelGast95 73:a91805f9e9f0 170 printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
HelGast95 73:a91805f9e9f0 171 if (characteristicP->getUUID().getShortUUID() == 0xa001) { /* !ALERT! Alter this filter to suit your device. */
HelGast95 73:a91805f9e9f0 172 testServiceptr = *characteristicP;
HelGast95 73:a91805f9e9f0 173 serviceDiscovered = true;
HelGast95 73:a91805f9e9f0 174 }
HelGast95 73:a91805f9e9f0 175 }
HelGast95 73:a91805f9e9f0 176
HelGast95 73:a91805f9e9f0 177 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
HelGast95 73:a91805f9e9f0 178 printf("terminated SD for handle %u\r\n", connectionHandle);
HelGast95 73:a91805f9e9f0 179 }
HelGast95 73:a91805f9e9f0 180
HelGast95 73:a91805f9e9f0 181 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
HelGast95 73:a91805f9e9f0 182 if (params->role == Gap::CENTRAL) {
HelGast95 73:a91805f9e9f0 183 BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
HelGast95 73:a91805f9e9f0 184 BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xA000, 0xA001);
HelGast95 73:a91805f9e9f0 185 }
HelGast95 73:a91805f9e9f0 186 }
mbed_official 1:72c60abef7e7 187
mbed_official 1:72c60abef7e7 188 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
mbed_official 1:72c60abef7e7 189 {
HelGast95 73:a91805f9e9f0 190 /* Si se desconecta el dispositivo, volvemos a entrar en estado Advertising*/
HelGast95 73:a91805f9e9f0 191 (void) params;
HelGast95 73:a91805f9e9f0 192 printf("Desconectado. Se comienza la fase de escaneo de nuevo\n\r");
HelGast95 73:a91805f9e9f0 193 serviceDiscovered = false;
HelGast95 73:a91805f9e9f0 194 BLE::Instance().gap().startScan(advertisementCallback);
mbed_official 1:72c60abef7e7 195 }
mbed_official 1:72c60abef7e7 196
HelGast95 73:a91805f9e9f0 197 void onDataReadClientCallback(const GattReadCallbackParams *response) {
HelGast95 73:a91805f9e9f0 198 if (response->handle == testServiceptr.getValueHandle()) {
HelGast95 73:a91805f9e9f0 199 printf("onDataReadClientCallback: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len);
HelGast95 73:a91805f9e9f0 200 for (unsigned index = 0; index < response->len; index++) {
HelGast95 73:a91805f9e9f0 201 printf("[%02x]", response->data[index]);
HelGast95 73:a91805f9e9f0 202 }
HelGast95 73:a91805f9e9f0 203 printf("\r\n");
mbed_official 1:72c60abef7e7 204 }
mbed_official 1:72c60abef7e7 205 }
mbed_official 1:72c60abef7e7 206
HelGast95 73:a91805f9e9f0 207 /**
HelGast95 73:a91805f9e9f0 208 * Esta función se llama si ha habido algún error en el proceso de inicialización del BLE
HelGast95 73:a91805f9e9f0 209 */
HelGast95 73:a91805f9e9f0 210 void onBleInitError(BLE &ble, ble_error_t error) {
HelGast95 73:a91805f9e9f0 211 printf("Ha ocurrido un error al inicializar la configuracion del BLE\n");
mbed_official 1:72c60abef7e7 212 }
mbed_official 1:72c60abef7e7 213
HelGast95 73:a91805f9e9f0 214 void printMacAddress() {
mbed_official 43:fb2855f7754b 215 /* Print out device MAC address to the console*/
mbed_official 43:fb2855f7754b 216 Gap::AddressType_t addr_type;
mbed_official 43:fb2855f7754b 217 Gap::Address_t address;
mbed_official 43:fb2855f7754b 218 BLE::Instance().gap().getAddress(&addr_type, address);
mbed_official 43:fb2855f7754b 219 printf("DEVICE MAC ADDRESS: ");
mbed_official 43:fb2855f7754b 220 for (int i = 5; i >= 1; i--){
mbed_official 43:fb2855f7754b 221 printf("%02x:", address[i]);
mbed_official 43:fb2855f7754b 222 }
mbed_official 43:fb2855f7754b 223 printf("%02x\r\n", address[0]);
mbed_official 43:fb2855f7754b 224 }
mbed_official 43:fb2855f7754b 225
HelGast95 73:a91805f9e9f0 226 /**
HelGast95 73:a91805f9e9f0 227 * Callback triggered when the ble initialization process has finished
HelGast95 73:a91805f9e9f0 228 */
mbed_official 1:72c60abef7e7 229 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
mbed_official 1:72c60abef7e7 230 {
mbed_official 1:72c60abef7e7 231 BLE& ble = params->ble;
mbed_official 1:72c60abef7e7 232 ble_error_t error = params->error;
mbed_official 1:72c60abef7e7 233
mbed_official 1:72c60abef7e7 234 if (error != BLE_ERROR_NONE) {
HelGast95 73:a91805f9e9f0 235 /* In case of error, forward the error handling to onBleInitError */
mbed_official 1:72c60abef7e7 236 onBleInitError(ble, error);
mbed_official 1:72c60abef7e7 237 return;
mbed_official 1:72c60abef7e7 238 }
mbed_official 1:72c60abef7e7 239
HelGast95 73:a91805f9e9f0 240 /* Ensure that it is the default instance of BLE */
HelGast95 73:a91805f9e9f0 241 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
mbed_official 1:72c60abef7e7 242 return;
mbed_official 1:72c60abef7e7 243 }
HelGast95 73:a91805f9e9f0 244
HelGast95 73:a91805f9e9f0 245 ble.gap().onConnection(connectionCallback);
mbed_official 1:72c60abef7e7 246 ble.gap().onDisconnection(disconnectionCallback);
mbed_official 1:72c60abef7e7 247
HelGast95 73:a91805f9e9f0 248 ble.gattClient().onDataRead(onDataReadClientCallback);
mbed_official 1:72c60abef7e7 249
HelGast95 73:a91805f9e9f0 250 ble.gap().setScanParams(500, 400);
HelGast95 73:a91805f9e9f0 251 ble.gap().startScan(advertisementCallback);
HelGast95 73:a91805f9e9f0 252
HelGast95 73:a91805f9e9f0 253 //printMacAddress();
mbed_official 1:72c60abef7e7 254 }
mbed_official 1:72c60abef7e7 255
mbed_official 1:72c60abef7e7 256 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
mbed_official 1:72c60abef7e7 257 BLE &ble = BLE::Instance();
mbed_official 10:ac3615194d04 258 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
HelGast95 73:a91805f9e9f0 259 }
HelGast95 73:a91805f9e9f0 260
HelGast95 73:a91805f9e9f0 261 void readVoltageValue() {
HelGast95 73:a91805f9e9f0 262 BLE &ble = BLE::Instance();
HelGast95 73:a91805f9e9f0 263 if (serviceDiscovered && !ble.gattClient().isServiceDiscoveryActive()) {
HelGast95 73:a91805f9e9f0 264 testServiceptr.read();
HelGast95 73:a91805f9e9f0 265 }
mbed_official 1:72c60abef7e7 266 }
mbed_official 1:72c60abef7e7 267
HelGast95 73:a91805f9e9f0 268 void BLEServiceManagment() {
HelGast95 73:a91805f9e9f0 269 eventQueue.call_every(2000, readVoltageValue);
HelGast95 73:a91805f9e9f0 270
mbed_official 1:72c60abef7e7 271 BLE &ble = BLE::Instance();
HelGast95 73:a91805f9e9f0 272 ble.onEventsToProcess(scheduleBleEventsProcessing);
mbed_official 1:72c60abef7e7 273 ble.init(bleInitComplete);
HelGast95 73:a91805f9e9f0 274
mbed_official 10:ac3615194d04 275 eventQueue.dispatch_forever();
HelGast95 73:a91805f9e9f0 276 }
mbed_official 1:72c60abef7e7 277
HelGast95 73:a91805f9e9f0 278 int main() {
HelGast95 73:a91805f9e9f0 279 threadLED.start(blinkLED3);
HelGast95 73:a91805f9e9f0 280 threadBLE.start(BLEServiceManagment);
HelGast95 73:a91805f9e9f0 281 //threadSerial.start(sendJsonOverSerial);
HelGast95 73:a91805f9e9f0 282
HelGast95 73:a91805f9e9f0 283 threadLED.join();
HelGast95 73:a91805f9e9f0 284 threadBLE.join();
HelGast95 73:a91805f9e9f0 285 //threadSerial.join();
HelGast95 73:a91805f9e9f0 286
mbed_official 1:72c60abef7e7 287 return 0;
HelGast95 73:a91805f9e9f0 288 }