cancel
Showing results for 
Search instead for 
Did you mean: 

Problem while sending String Commands via BLE to STM32WB (attribute_modified->Attr)

yeyoboe
Associate II

Hello, I'm new at developing Wireless applications with the STM32WB and can not find why old data from old commands stays somewhere in my code and affects my new commands sent. 

Im writing the command strings into Myble_Characteristic_Command with the ST BLE Toolbox. Everything works fine as long as the Command has the termination character ("!"), but if the command does not have this and a longer command was sent before, the new command is now joined with the following old characters until the old termination character. I can not find where is this old data being stored. 

For example if I send first the command: "C:Hello!", the received command is "C:Hello!" but if then I send a shorter command without the termination character ("!") like: "C:Hi", the received command is "C:Hillo!" which of course is not correct. I found the old data is comming from attribute_modified->Attr_Data but can not find the way of setting it to empty characters before the write event occurs. I will appreciate someone's help. Here is the part of the code im having problems with: 

Thanks in advance!

static SVCCTL_EvtAckStatus_t Custom_STM_Event_Handler(void *Event)
{
SVCCTL_EvtAckStatus_t return_value;
hci_event_pckt *event_pckt;
evt_blecore_aci *blecore_evt;
aci_gatt_attribute_modified_event_rp0 *attribute_modified;
Custom_STM_App_Notification_evt_t Notification;
/* USER CODE BEGIN Custom_STM_Event_Handler_1 */

/* USER CODE END Custom_STM_Event_Handler_1 */

return_value = SVCCTL_EvtNotAck;
event_pckt = (hci_event_pckt *)(((hci_uart_pckt*)Event)->data);

switch (event_pckt->evt)
{
case HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE:
blecore_evt = (evt_blecore_aci*)event_pckt->data;
switch (blecore_evt->ecode)
{
case ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE:
/* USER CODE BEGIN EVT_BLUE_GATT_ATTRIBUTE_MODIFIED_BEGIN */

/* USER CODE END EVT_BLUE_GATT_ATTRIBUTE_MODIFIED_BEGIN */
attribute_modified = (aci_gatt_attribute_modified_event_rp0*)blecore_evt->data;
if (attribute_modified->Attr_Handle == (CustomContext.CustomMycharnotifyHdle + CHARACTERISTIC_DESCRIPTOR_ATTRIBUTE_OFFSET))
{
return_value = SVCCTL_EvtAckFlowEnable;
/* USER CODE BEGIN CUSTOM_STM_Service_1_Char_2 */

/* USER CODE END CUSTOM_STM_Service_1_Char_2 */
switch (attribute_modified->Attr_Data[0])
{
/* USER CODE BEGIN CUSTOM_STM_Service_1_Char_2_attribute_modified */

/* USER CODE END CUSTOM_STM_Service_1_Char_2_attribute_modified */

/* Disabled Notification management */
case (!(COMSVC_Notification)):
/* USER CODE BEGIN CUSTOM_STM_Service_1_Char_2_Disabled_BEGIN */

/* USER CODE END CUSTOM_STM_Service_1_Char_2_Disabled_BEGIN */
Notification.Custom_Evt_Opcode = CUSTOM_STM_MYCHARNOTIFY_NOTIFY_DISABLED_EVT;
Custom_STM_App_Notification(&Notification);
/* USER CODE BEGIN CUSTOM_STM_Service_1_Char_2_Disabled_END */

/* USER CODE END CUSTOM_STM_Service_1_Char_2_Disabled_END */
break;

/* Enabled Notification management */
case COMSVC_Notification:
/* USER CODE BEGIN CUSTOM_STM_Service_1_Char_2_COMSVC_Notification_BEGIN */

/* USER CODE END CUSTOM_STM_Service_1_Char_2_COMSVC_Notification_BEGIN */
Notification.Custom_Evt_Opcode = CUSTOM_STM_MYCHARNOTIFY_NOTIFY_ENABLED_EVT;
Custom_STM_App_Notification(&Notification);
/* USER CODE BEGIN CUSTOM_STM_Service_1_Char_2_COMSVC_Notification_END */

/* USER CODE END CUSTOM_STM_Service_1_Char_2_COMSVC_Notification_END */
break;

default:
/* USER CODE BEGIN CUSTOM_STM_Service_1_Char_2_default */

/* USER CODE END CUSTOM_STM_Service_1_Char_2_default */
break;
}
} /* if (attribute_modified->Attr_Handle == (CustomContext.CustomMycharnotifyHdle + CHARACTERISTIC_DESCRIPTOR_ATTRIBUTE_OFFSET))*/

else if (attribute_modified->Attr_Handle == (CustomContext.CustomMycharwriteHdle + CHARACTERISTIC_VALUE_ATTRIBUTE_OFFSET))
{
return_value = SVCCTL_EvtAckFlowEnable;
/* USER CODE BEGIN CUSTOM_STM_Service_1_Char_1_ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE */
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
/* USER CODE END CUSTOM_STM_Service_1_Char_1_ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE */
} /* if (attribute_modified->Attr_Handle == (CustomContext.CustomMycharwriteHdle + CHARACTERISTIC_VALUE_ATTRIBUTE_OFFSET))*/
else if (attribute_modified->Attr_Handle == (CustomContext.CustomMyble_Characteristic_CommandHdle + CHARACTERISTIC_VALUE_ATTRIBUTE_OFFSET))
{
return_value = SVCCTL_EvtAckFlowEnable;
/* USER CODE BEGIN CUSTOM_STM_Service_1_Char_3_ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE */

uint8_t receivedDataBuffer[10] = "";
uint16_t receivedDataLength = 0;

memset(receivedDataBuffer, 0, sizeof(receivedDataBuffer));

uint8_t i = 0;
uint8_t CmdTerminator_Position = 0;

while (i < attribute_modified->Attr_Data_Length)
{
if(attribute_modified->Attr_Data[i] != '!')
{
receivedDataBuffer[receivedDataLength++] = attribute_modified->Attr_Data[i];
}
else
{
receivedDataBuffer[receivedDataLength++] = attribute_modified->Attr_Data[i];
CmdTerminator_Position = i;
i = attribute_modified->Attr_Data_Length;
}
i++;
}

if(receivedDataBuffer[0] == 'C' && receivedDataBuffer[1] == ':' && receivedDataBuffer[CmdTerminator_Position] == '!')
{
//Print String Command
char dataString[100]; // Adjust the size as needed
sprintf(dataString, "Command Received: %s\r\n", receivedDataBuffer);
HAL_UART_Transmit(&huart1, (uint8_t*)dataString, strlen(dataString), 10);
}
else
{
char dataString[100]; // Adjust the size as needed
sprintf(dataString, "Command Format Error\r\n");
HAL_UART_Transmit(&huart1, (uint8_t*)dataString, strlen(dataString), 10);

hci_disconnect(CurrentConnectionHandle, 0x1A);
}

HAL_Delay(1);

/* USER CODE END CUSTOM_STM_Service_1_Char_3_ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE */
} /* if (attribute_modified->Attr_Handle == (CustomContext.CustomMyble_Characteristic_CommandHdle + CHARACTERISTIC_VALUE_ATTRIBUTE_OFFSET))*/
/* USER CODE BEGIN EVT_BLUE_GATT_ATTRIBUTE_MODIFIED_END */

/* USER CODE END EVT_BLUE_GATT_ATTRIBUTE_MODIFIED_END */
break;

case ACI_GATT_READ_PERMIT_REQ_VSEVT_CODE :
/* USER CODE BEGIN EVT_BLUE_GATT_READ_PERMIT_REQ_BEGIN */

/* USER CODE END EVT_BLUE_GATT_READ_PERMIT_REQ_BEGIN */
/* USER CODE BEGIN EVT_BLUE_GATT_READ_PERMIT_REQ_END */

/* USER CODE END EVT_BLUE_GATT_READ_PERMIT_REQ_END */
break;

case ACI_GATT_WRITE_PERMIT_REQ_VSEVT_CODE:
/* USER CODE BEGIN EVT_BLUE_GATT_WRITE_PERMIT_REQ_BEGIN */

/* USER CODE END EVT_BLUE_GATT_WRITE_PERMIT_REQ_BEGIN */
/* USER CODE BEGIN EVT_BLUE_GATT_WRITE_PERMIT_REQ_END */

/* USER CODE END EVT_BLUE_GATT_WRITE_PERMIT_REQ_END */
break;
/* USER CODE BEGIN BLECORE_EVT */

/* USER CODE END BLECORE_EVT */
default:
/* USER CODE BEGIN EVT_DEFAULT */

/* USER CODE END EVT_DEFAULT */
break;
}
/* USER CODE BEGIN EVT_VENDOR*/

/* USER CODE END EVT_VENDOR*/
break; /* HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE */

/* USER CODE BEGIN EVENT_PCKT_CASES*/

/* USER CODE END EVENT_PCKT_CASES*/

default:
/* USER CODE BEGIN EVENT_PCKT*/

/* USER CODE END EVENT_PCKT*/
break;
}

/* USER CODE BEGIN Custom_STM_Event_Handler_2 */

/* USER CODE END Custom_STM_Event_Handler_2 */

return(return_value);
}/* end Custom_STM_Event_Handler */

1 ACCEPTED SOLUTION

Accepted Solutions
yeyoboe
Associate II

The issue was solved by updating the BLE characteristic value after reading the sent command: 

uint8_t ClearBuffer[10];
memset(ClearBuffer, 0, sizeof(ClearBuffer));

//Clear Command Characteristic value
aci_gatt_update_char_value(CustomContext.CustomMysvcHdle, CustomContext.CustomMyble_Characteristic_CommandHdle, 0, sizeof(ClearBuffer), ClearBuffer);

 

View solution in original post

1 REPLY 1
yeyoboe
Associate II

The issue was solved by updating the BLE characteristic value after reading the sent command: 

uint8_t ClearBuffer[10];
memset(ClearBuffer, 0, sizeof(ClearBuffer));

//Clear Command Characteristic value
aci_gatt_update_char_value(CustomContext.CustomMysvcHdle, CustomContext.CustomMyble_Characteristic_CommandHdle, 0, sizeof(ClearBuffer), ClearBuffer);