Mercurial > public > ostc4
comparison Discovery/Src/cv_heartbeat.c @ 1033:5f66e44d69f0 Puls_Integration
Added functionality needed for subscription of standard Bluetooth pulse service notifications
| author | Ideenmodellierer |
|---|---|
| date | Sat, 02 Aug 2025 22:42:51 +0200 |
| parents | 33b91584d827 |
| children | 195bfbdf961d |
comparison
equal
deleted
inserted
replaced
| 1032:33b91584d827 | 1033:5f66e44d69f0 |
|---|---|
| 39 static uint32_t startDetection_ms; | 39 static uint32_t startDetection_ms; |
| 40 | 40 |
| 41 #define MAX_BT_DEVICE 10 /* max number of device which may be handled */ | 41 #define MAX_BT_DEVICE 10 /* max number of device which may be handled */ |
| 42 static btDdeviceData_t btDeviceList[MAX_BT_DEVICE]; | 42 static btDdeviceData_t btDeviceList[MAX_BT_DEVICE]; |
| 43 static btDeviceService_t curDeviceService[10]; | 43 static btDeviceService_t curDeviceService[10]; |
| 44 static btDeviceCharacteristic_t curDevCharacteristic[10]; | |
| 45 static btDeviceDescriptor_t curDevDescriptor; | |
| 46 | |
| 47 | |
| 48 static uint8_t curCharacteristicIndex = 0; | |
| 44 static uint8_t curServiceIndex = 0; | 49 static uint8_t curServiceIndex = 0; |
| 45 static uint8_t curBtIndex = 0; | 50 static uint8_t curBtIndex = 0; |
| 46 static uint8_t connHandle = ' '; | 51 static uint8_t connHandle = ' '; |
| 52 static uint8_t evaluateDevIndex = 0xFF; | |
| 53 static uint8_t evaluateSrvIndex = 0xFF; | |
| 54 static uint8_t evaluateCharIndex = 0xFF; | |
| 55 static uint8_t evaluateDescIndex = 0xFF; | |
| 47 | 56 |
| 48 static indicatior_t checkIndicators(uint8_t* pdata) | 57 static indicatior_t checkIndicators(uint8_t* pdata) |
| 49 { | 58 { |
| 50 #if 0 | |
| 51 static uint8_t foundRSSI = 0; | |
| 52 static uint8_t foundNAME = 0; | |
| 53 static uint8_t foundMNF = 0; | |
| 54 static uint8_t foundUUID = 0; | |
| 55 static uint8_t foundOK = 0; | |
| 56 #endif | |
| 57 indicatior_t ret = NO_INDICATOR; | 59 indicatior_t ret = NO_INDICATOR; |
| 58 | 60 |
| 59 if(strcmp((char*)pdata,"+UBTD:") == 0) | 61 if(strcmp((char*)pdata,"+UBTD:") == 0) |
| 60 { | 62 { |
| 61 ret = DEVICE_INDICATOR; | 63 ret = DEVICE_INDICATOR; |
| 66 } | 68 } |
| 67 else if(strcmp((char*)pdata,"+UBTGDP:") == 0) | 69 else if(strcmp((char*)pdata,"+UBTGDP:") == 0) |
| 68 { | 70 { |
| 69 ret = SERVICE_INDICATOR; | 71 ret = SERVICE_INDICATOR; |
| 70 } | 72 } |
| 73 else if(strcmp((char*)pdata,"+UBTGDCS:") == 0) | |
| 74 { | |
| 75 ret = CHARACTERISTIC_INDICATOR; | |
| 76 } | |
| 77 else if(strcmp((char*)pdata,"+UBTGDCD:") == 0) | |
| 78 { | |
| 79 ret = DESCRIPTOR_INDICATOR; | |
| 80 } | |
| 71 | 81 |
| 72 return ret; | 82 return ret; |
| 73 } | 83 } |
| 74 | 84 |
| 75 static void handleOK() | 85 static void handleOK() |
| 76 { | 86 { |
| 87 uint8_t index = 0; | |
| 88 | |
| 77 switch(heartbeatState) | 89 switch(heartbeatState) |
| 78 { | 90 { |
| 79 case SENSOR_HB_ENABLE_BLE: heartbeatState = SENSOR_HB_CHECK_CONFIG; | 91 case SENSOR_HB_ENABLE_BLE: heartbeatState = SENSOR_HB_CHECK_CONFIG; |
| 80 break; | 92 break; |
| 81 case SENSOR_HB_CHECK_CONFIG: heartbeatState = SENSOR_HB_DISCOVER; | 93 case SENSOR_HB_CHECK_CONFIG: heartbeatState = SENSOR_HB_DISCOVER; |
| 82 break; | 94 break; |
| 83 case SENSOR_HB_RESTART: heartbeatState = SENSOR_HB_OFFLINE; | 95 case SENSOR_HB_RESTART: heartbeatState = SENSOR_HB_OFFLINE; |
| 84 break; | 96 break; |
| 85 case SENSOR_HB_DISCOVER: heartbeatState = SENSOR_HB_CONNECT; | 97 case SENSOR_HB_DISCOVER: if(curBtIndex > 0) |
| 86 break; | 98 { |
| 87 case SENSOR_HB_SERVICES: heartbeatState = SENSOR_HB_OFFLINE; | 99 heartbeatState = SENSOR_HB_CONNECT; |
| 88 break; | 100 evaluateDevIndex = 0; |
| 101 } | |
| 102 else | |
| 103 { | |
| 104 heartbeatState = SENSOR_HB_OFFLINE; | |
| 105 } | |
| 106 | |
| 107 break; | |
| 108 case SENSOR_HB_SERVICES: evaluateSrvIndex = 0xFF; | |
| 109 if(curServiceIndex != 0) | |
| 110 { | |
| 111 for(index = 0; index <= curServiceIndex; index++) | |
| 112 { | |
| 113 if(strcmp((char*)curDeviceService[index].uuid,"180D") == 0) | |
| 114 { | |
| 115 heartbeatState = SENSOR_HB_CHARACTERISTIC; | |
| 116 evaluateSrvIndex = index; | |
| 117 curCharacteristicIndex = 0; | |
| 118 break; | |
| 119 } | |
| 120 } | |
| 121 } | |
| 122 if(evaluateSrvIndex == 0xFF) /* device does not provide heartbeat data => disconnect */ | |
| 123 { | |
| 124 heartbeatState = SENSOR_HB_DISCONNECT; | |
| 125 } | |
| 126 break; | |
| 127 case SENSOR_HB_CHARACTERISTIC: evaluateCharIndex = 0xFF; | |
| 128 if(curCharacteristicIndex != 0) | |
| 129 { | |
| 130 for(index = 0; index < curCharacteristicIndex; index++) | |
| 131 { | |
| 132 if(strcmp((char*)curDevCharacteristic[index].uuid,"2A37") == 0) | |
| 133 { | |
| 134 heartbeatState = SENSOR_HB_DESCRIPTOR; | |
| 135 evaluateCharIndex = index; | |
| 136 break; | |
| 137 } | |
| 138 } | |
| 139 } | |
| 140 if(evaluateCharIndex == 0xFF) /* device does not provide heartbeat data => disconnect */ | |
| 141 { | |
| 142 heartbeatState = SENSOR_HB_DISCONNECT; | |
| 143 } | |
| 144 break; | |
| 145 case SENSOR_HB_DESCRIPTOR: if(strcmp((char*)curDevDescriptor.uuid,"2902") == 0) | |
| 146 { | |
| 147 heartbeatState = SENSOR_HB_SUBSCRIBE; | |
| 148 } | |
| 149 else | |
| 150 { | |
| 151 heartbeatState = SENSOR_HB_DISCONNECT; | |
| 152 } | |
| 153 break; | |
| 154 case SENSOR_HB_DISCONNECT: evaluateDevIndex++; | |
| 155 connHandle= ' '; | |
| 156 if(evaluateDevIndex < curBtIndex) /* more devices to be evaluated? */ | |
| 157 { | |
| 158 heartbeatState = SENSOR_HB_CONNECT; | |
| 159 } | |
| 160 else | |
| 161 { | |
| 162 heartbeatState = SENSOR_HB_OFFLINE; | |
| 163 } | |
| 164 break; | |
| 165 case SENSOR_HB_CONNECT: /* handled in data rx section */ | |
| 89 default: | 166 default: |
| 90 break; | 167 break; |
| 91 } | 168 } |
| 92 } | 169 } |
| 93 | 170 |
| 94 static void handleERROR() | 171 static void handleERROR() |
| 95 { | 172 { |
| 96 | 173 switch(heartbeatState) |
| 97 } | 174 { |
| 98 | 175 case SENSOR_HB_DISCONNECT: evaluateDevIndex++; |
| 99 static uint8_t getDeviceList() | 176 connHandle= ' '; |
| 177 if(evaluateDevIndex < curBtIndex) /* more devices to be evaluated? */ | |
| 178 { | |
| 179 heartbeatState = SENSOR_HB_CONNECT; | |
| 180 } | |
| 181 else | |
| 182 { | |
| 183 heartbeatState = SENSOR_HB_OFFLINE; | |
| 184 } | |
| 185 break; | |
| 186 default: | |
| 187 break; | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 uint8_t cv_heartbeat_HandleData() | |
| 100 { | 192 { |
| 101 static uint8_t firstDevice = 1; | 193 static uint8_t firstDevice = 1; |
| 102 static uint8_t curLine[MAX_CHAR_PER_LINE]; /* holds complete line and is used for logging */ | 194 static uint8_t curLine[MAX_CHAR_PER_LINE]; /* holds complete line and is used for logging */ |
| 103 static uint8_t curLineIndex = 0; | 195 static uint8_t curLineIndex = 0; |
| 104 static uint8_t parameter[40]; /* content of the parameter in read state */ | 196 static uint8_t parameter[40]; /* content of the parameter in read state */ |
| 111 uint8_t data = 0; | 203 uint8_t data = 0; |
| 112 data = UART_getChar(); | 204 data = UART_getChar(); |
| 113 | 205 |
| 114 while((data != 0) && (complete == 0)) | 206 while((data != 0) && (complete == 0)) |
| 115 { | 207 { |
| 116 if(curLineIndex == MAX_CHAR_PER_LINE) /* avoid overflow */ | 208 if(curLineIndex == MAX_CHAR_PER_LINE - 1) /* avoid overflow */ |
| 117 { | 209 { |
| 118 InfoLogger_writeLine(curLine,curLineIndex,0); | 210 InfoLogger_writeLine(curLine,curLineIndex,0); |
| 119 memset(curLine,0,sizeof(curLine)); | 211 memset(curLine,0,sizeof(curLine)); |
| 120 curLineIndex = 0; | 212 curLineIndex = 0; |
| 121 } | 213 } |
| 141 case BT_READ_DEVICE_NAME: if(writeIndex < BLUEMOD_NAME_SIZE) | 233 case BT_READ_DEVICE_NAME: if(writeIndex < BLUEMOD_NAME_SIZE) |
| 142 { | 234 { |
| 143 memcpy (btDeviceList[curBtIndex].name, parameter, writeIndex); | 235 memcpy (btDeviceList[curBtIndex].name, parameter, writeIndex); |
| 144 } | 236 } |
| 145 break; | 237 break; |
| 146 case BT_READ_SERV_UUID: if(writeIndex < 50) | 238 case BT_READ_SERV_UUID: if((writeIndex < 50) && (curServiceIndex < 10)) |
| 147 { | 239 { |
| 148 memcpy(curDeviceService[curServiceIndex].uuid, parameter, writeIndex); | 240 memcpy(curDeviceService[curServiceIndex].uuid, parameter, writeIndex); |
| 149 } | 241 } |
| 150 curServiceIndex++; | 242 curServiceIndex++; |
| 243 break; | |
| 244 case BT_READ_CHAR_UUID: if(writeIndex < 50) | |
| 245 { | |
| 246 memcpy(curDevCharacteristic[curCharacteristicIndex].uuid, parameter, writeIndex); | |
| 247 curCharacteristicIndex++; | |
| 248 } | |
| 249 break; | |
| 250 case BT_READ_DESC_UUID: if(writeIndex < 50) | |
| 251 { | |
| 252 memcpy(curDevDescriptor.uuid, parameter, writeIndex); | |
| 253 } | |
| 151 break; | 254 break; |
| 152 default: | 255 default: |
| 153 break; | 256 break; |
| 154 } | 257 } |
| 155 curLineIndex = 0; | 258 curLineIndex = 0; |
| 169 break; | 272 break; |
| 170 case CONNECTION_INDICATOR: readType = BT_READ_CON_DETAILS; | 273 case CONNECTION_INDICATOR: readType = BT_READ_CON_DETAILS; |
| 171 break; | 274 break; |
| 172 case SERVICE_INDICATOR: readType = BT_READ_SERV_HANDLE; | 275 case SERVICE_INDICATOR: readType = BT_READ_SERV_HANDLE; |
| 173 break; | 276 break; |
| 277 case CHARACTERISTIC_INDICATOR: readType = BT_READ_CHAR_CONHANDLE; | |
| 278 // snprintf(text,40,"Found Char"); | |
| 279 // InfoLogger_writeLine((uint8_t*)text,strlen(text),0); | |
| 280 break; | |
| 281 case DESCRIPTOR_INDICATOR: readType = BT_READ_DESC_CONHANDLE; | |
| 282 break; | |
| 283 | |
| 174 default: | 284 default: |
| 175 break; | 285 break; |
| 176 } | 286 } |
| 177 writeIndex = 0; | 287 writeIndex = 0; |
| 178 memset(parameter,0,sizeof(parameter)); | 288 memset(parameter,0,sizeof(parameter)); |
| 211 readType = BT_READ_NOTHING; | 321 readType = BT_READ_NOTHING; |
| 212 break; | 322 break; |
| 213 case BT_READ_CON_DETAILS: connHandle = parameter[0]; | 323 case BT_READ_CON_DETAILS: connHandle = parameter[0]; |
| 214 heartbeatState = SENSOR_HB_SERVICES; | 324 heartbeatState = SENSOR_HB_SERVICES; |
| 215 readType = BT_READ_NOTHING; | 325 readType = BT_READ_NOTHING; |
| 326 curServiceIndex = 0; | |
| 216 break; | 327 break; |
| 217 case BT_READ_SERV_HANDLE: curDeviceService[curServiceIndex].handle = parameter[0]; | 328 case BT_READ_SERV_HANDLE: curDeviceService[curServiceIndex].handle = parameter[0]; |
| 218 readType = BT_READ_SERV_START; | 329 readType = BT_READ_SERV_START; |
| 219 break; | 330 break; |
| 220 case BT_READ_SERV_START: if(writeIndex < 6) | 331 case BT_READ_SERV_START: if(writeIndex < 6) |
| 227 { | 338 { |
| 228 memcpy(curDeviceService[curServiceIndex].end, parameter, writeIndex); | 339 memcpy(curDeviceService[curServiceIndex].end, parameter, writeIndex); |
| 229 } | 340 } |
| 230 readType = BT_READ_SERV_UUID; | 341 readType = BT_READ_SERV_UUID; |
| 231 break; | 342 break; |
| 232 | 343 case BT_READ_CHAR_CONHANDLE: curDevCharacteristic[curCharacteristicIndex].conHandle = parameter[0]; |
| 344 readType = BT_READ_CHAR_ATTRIBUTE; | |
| 345 break; | |
| 346 case BT_READ_CHAR_ATTRIBUTE: if(writeIndex < 10) | |
| 347 { | |
| 348 memcpy(curDevCharacteristic[curCharacteristicIndex].attrHandle, parameter, writeIndex); | |
| 349 } | |
| 350 readType = BT_READ_CHAR_PROPERTY; | |
| 351 break; | |
| 352 case BT_READ_CHAR_PROPERTY: if(writeIndex < 10) | |
| 353 { | |
| 354 memcpy(curDevCharacteristic[curCharacteristicIndex].properties, parameter, writeIndex); | |
| 355 } | |
| 356 readType = BT_READ_CHAR_VALUEHANDLE; | |
| 357 break; | |
| 358 case BT_READ_CHAR_VALUEHANDLE: if(writeIndex < 10) | |
| 359 { | |
| 360 memcpy(curDevCharacteristic[curCharacteristicIndex].valueHandle, parameter, writeIndex); | |
| 361 } | |
| 362 readType = BT_READ_CHAR_UUID; | |
| 363 break; | |
| 364 case BT_READ_DESC_CONHANDLE: curDevDescriptor.conHandle = parameter[0]; | |
| 365 readType = BT_READ_DESC_CHARHANDLE; | |
| 366 break; | |
| 367 case BT_READ_DESC_CHARHANDLE: if(writeIndex < 10) | |
| 368 { | |
| 369 memcpy(curDevDescriptor.charHandle, parameter, writeIndex); | |
| 370 } | |
| 371 readType = BT_READ_DESC_DESCHANDLE; | |
| 372 break; | |
| 373 case BT_READ_DESC_DESCHANDLE: if(writeIndex < 10) | |
| 374 { | |
| 375 memcpy(curDevDescriptor.descHandle, parameter, writeIndex); | |
| 376 } | |
| 377 readType = BT_READ_DESC_UUID; | |
| 378 break; | |
| 233 default: readType = BT_READ_NOTHING; | 379 default: readType = BT_READ_NOTHING; |
| 234 break; | 380 break; |
| 235 } | 381 } |
| 236 writeIndex = 0; | 382 writeIndex = 0; |
| 237 memset(parameter,0 , sizeof(parameter)); | 383 memset(parameter,0 , sizeof(parameter)); |
| 297 void cv_heartbeat_Control(void) | 443 void cv_heartbeat_Control(void) |
| 298 { | 444 { |
| 299 static uint8_t action = 0; | 445 static uint8_t action = 0; |
| 300 static uint8_t retry = 0; | 446 static uint8_t retry = 0; |
| 301 static uint8_t lastState = 0; | 447 static uint8_t lastState = 0; |
| 302 static uint8_t devicesIndex = 0; | |
| 303 | |
| 304 | |
| 305 char cmd[50]; | 448 char cmd[50]; |
| 306 | 449 |
| 307 cmd[0] = 0; | 450 cmd[0] = 0; |
| 308 | |
| 309 getDeviceList(); | |
| 310 | 451 |
| 311 if(action == 3) | 452 if(action == 3) |
| 312 { | 453 { |
| 313 action = 0; | 454 action = 0; |
| 314 switch(heartbeatState) | 455 switch(heartbeatState) |
| 334 #endif | 475 #endif |
| 335 break; | 476 break; |
| 336 case SENSOR_HB_DISCOVER: if(lastState != SENSOR_HB_DISCOVER) | 477 case SENSOR_HB_DISCOVER: if(lastState != SENSOR_HB_DISCOVER) |
| 337 { | 478 { |
| 338 snprintf(cmd, sizeof(cmd), "AT+UBTD=2,1\r\n" ); //+UBTLE=1 //"AT+UBTD=2,1,5000\r\n" | 479 snprintf(cmd, sizeof(cmd), "AT+UBTD=2,1\r\n" ); //+UBTLE=1 //"AT+UBTD=2,1,5000\r\n" |
| 339 devicesIndex = 0; | 480 curBtIndex = 0; |
| 340 } | 481 } |
| 341 | 482 |
| 342 //snprintf(cmd, sizeof(cmd), "AT&W\r\n" ); // AT+UBTD=2,1\r\n "AT+UBTD=2,1,5000\r\n" | 483 //snprintf(cmd, sizeof(cmd), "AT&W\r\n" ); // AT+UBTD=2,1\r\n "AT+UBTD=2,1,5000\r\n" |
| 343 break; | 484 break; |
| 344 #if 0 | 485 #if 0 |
| 345 case SENSOR_HB_RESTART: snprintf(cmd, sizeof(cmd), "AT+UBTD=2,1\r\n" ); //+UBTLE=1 //"AT+UBTD=2,1,5000\r\n" | 486 case SENSOR_HB_RESTART: snprintf(cmd, sizeof(cmd), "AT+UBTD=2,1\r\n" ); //+UBTLE=1 //"AT+UBTD=2,1,5000\r\n" |
| 346 | 487 |
| 347 // snprintf(cmd, sizeof(cmd), "AT+CPWROFF\r\n" ); // AT+UBTD=2,1\r\n "AT+UBTD=2,1,5000\r\n" | 488 // snprintf(cmd, sizeof(cmd), "AT+CPWROFF\r\n" ); // AT+UBTD=2,1\r\n "AT+UBTD=2,1,5000\r\n" |
| 348 break; | 489 break; |
| 349 #endif | 490 #endif |
| 350 case SENSOR_HB_CONNECT: if(curBtIndex != devicesIndex) | 491 case SENSOR_HB_CONNECT: if(evaluateDevIndex < curBtIndex) |
| 351 { | 492 { |
| 352 snprintf(cmd, sizeof(cmd), "AT+UBTACLC=%s\r\n",btDeviceList[devicesIndex].address); | 493 snprintf(cmd, sizeof(cmd), "AT+UBTACLC=%s\r\n",btDeviceList[evaluateDevIndex].address); |
| 353 devicesIndex++; | 494 // evaluateDevIndex = devicesIndex; |
| 354 } | 495 // devicesIndex++; |
| 496 } | |
| 497 break; | |
| 498 case SENSOR_HB_DISCONNECT: snprintf(cmd, sizeof(cmd), "AT+UBTACLD=%c\r\n",connHandle); | |
| 355 break; | 499 break; |
| 356 case SENSOR_HB_SERVICES: if((connHandle >= '0') && (connHandle <= '9') && (lastState != SENSOR_HB_SERVICES)) | 500 case SENSOR_HB_SERVICES: if((connHandle >= '0') && (connHandle <= '9') && (lastState != SENSOR_HB_SERVICES)) |
| 357 { | 501 { |
| 358 snprintf(cmd, sizeof(cmd), "AT+UBTGDP=0%c\r\n",connHandle); | 502 snprintf(cmd, sizeof(cmd), "AT+UBTGDP=%c\r\n",connHandle); |
| 359 } | 503 memset(curDeviceService, 0, sizeof(curDeviceService)); |
| 360 else | 504 } |
| 361 { | 505 break; |
| 362 heartbeatState = SENSOR_HB_OFFLINE; | 506 case SENSOR_HB_CHARACTERISTIC: snprintf(cmd, sizeof(cmd), "AT+UBTGDCS=%c,%s,%s\r\n",connHandle,curDeviceService[evaluateSrvIndex].start |
| 363 } | 507 ,curDeviceService[evaluateSrvIndex].end); |
| 364 break; | 508 memset(curDevCharacteristic, 0, sizeof(curDevCharacteristic)); |
| 365 | 509 break; |
| 510 case SENSOR_HB_DESCRIPTOR: snprintf(cmd, sizeof(cmd), "AT+UBTGDCD=%c,%s,%s\r\n",connHandle,curDevCharacteristic[evaluateCharIndex].valueHandle | |
| 511 ,curDeviceService[evaluateSrvIndex].end); | |
| 512 //memset(curDevDescriptor, 0, sizeof(curDevCharacteristic)); | |
| 513 break; | |
| 514 case SENSOR_HB_SUBSCRIBE: snprintf(cmd, sizeof(cmd), "AT+UBTGWC=%c,%s,1\r\n",connHandle,curDevDescriptor.descHandle); | |
| 515 break; | |
| 366 default: | 516 default: |
| 367 break; | 517 break; |
| 368 } | 518 } |
| 369 if(cmd[0] != 0) | 519 if(cmd[0] != 0) |
| 370 { | 520 { |
