This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

About Notify Data Size

Hi All,

I asked this question before but I accidentally pressed close.

The engineer told me that for nRF5 SDK ( I assume as you said Segger), you can send 10 byte long notification even you have maximum length 50 byte. You do not need to change the maximum length.

According to this answer I have a problem that my array has a mantissa problem.

Generally speaking, I will send 100Bytes of data regularly, but the last Packet may be less than 100Bytes.

If my last Packet is only 20Byte, it will still send 100Byte.

If my last Packet is only 20Byte, it will still send 100Byte.

Best regards,

Kai

Parents
  • Hello Kai,

    Please share relevant code here so I can see how you are sending the notification. As noted in the other case, there should be no problem to send a notification packet as long as it does not exceed the maximum length. 

    Best regards,

    Vidar

  • Hi Vidar,

    Please share relevant code here so I can see how you are sending the notification.

    Sorry I just can only provide you with a partial program because it involves company property.

    Here is my process from initialization - gathering data into buffer - sending.Please let me know if there is anything else you need to know.

    Init => Here is the length used by my characteristic.

    // Add Error Messages characteristic.
        initial_error_messages = p_cs_init->initial_error_msg;
    
        memset(&add_char_params, 0, sizeof(add_char_params));
        add_char_params.uuid                = CS_UUID_ERROR_MESSAGES_CHAR;
        add_char_params.uuid_type           = p_cs->uuid_type;
        //add_char_params.init_len            = sizeof(uint8_t);  //for 1byte
        //add_char_params.max_len             = sizeof(uint8_t);  //for 1byte
        #ifdef BT_TxTest
        add_char_params.init_len            = 10;  //for array
        add_char_params.max_len             = 10;  //for array
        #else
        add_char_params.init_len            = 100;  //for array
        add_char_params.max_len             = 100;  //for array
        #endif
        add_char_params.p_init_value        = &initial_error_messages;
        //add_char_params.char_props.notify   = p_cs->is_notification_supported;
        add_char_params.char_props.notify   = 1;
        add_char_params.char_props.read   = 1;
    
        add_char_params.read_access       = SEC_OPEN;
        add_char_params.cccd_write_access = SEC_OPEN;
    
        user_desc_param.p_char_user_desc = (uint8_t *) user_desc2;
        user_desc_param.max_size = strlen(user_desc2);
        user_desc_param.size = strlen(user_desc2);
        user_desc_param.read_access  = SEC_OPEN;
    
        add_char_params.p_user_descr = &user_desc_param;
    
        err_code = characteristic_add(p_cs->service_handle,
                                      &add_char_params,
                                      &p_cs->error_char_handles);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }

    gathering data into buffer => I will collect 100 Bytes to send, in which you will see [if ((EUSART1_Tx_Buff[0] == 0x66) && (EUSART1_Tx_Buff[9] == 0x88))] Here is the last piece of data I want to collect.

    And there will be a mantissa problem here. If my remaining data is 20Byte, 80Byte will be garbled after I send it. At present, the program will clear the array after sending, so that these garbled characters are cleared to 0.

    void BLE_PROCESS(void) 
    {
        ret_code_t err_code;
    
        BLE_i = BLEBufInx * BLE_Y;            
        BLE_ptr = BLE_i + &BLE_Buf[0];                  
        for (BLE_i = 0; BLE_i < BLE_Y; BLE_i++) 
        {                                                                     
          *BLE_ptr = EUSART1_Tx_Buff[BLE_i];            
          BLE_ptr = BLE_ptr + 1;                                 
        }                                                                
        BLEBufInx++;                                         
        if (BLEBufInx >= BLE_X)                   
        {                                                              
          BLEBufInx = 0;  
            
          err_code = ble_cs_error_check_update(&m_cs, BLE_Buf, ARRAY_SIZE(BLE_Buf), BLE_CONN_HANDLE_ALL);  //for array  //20220223 Kai
          if ((err_code != NRF_SUCCESS) &&  //
              (err_code != NRF_ERROR_INVALID_STATE) &&  //
              (err_code != NRF_ERROR_RESOURCES) &&  //
              (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)  //
             )  //
          {  //
              APP_ERROR_HANDLER(err_code);  //
          }  //   
          
          memset(&BLE_Buf, 0, sizeof(BLE_Buf));                               
        }
        else
        {
          if ((EUSART1_Tx_Buff[0] == 0x66) && (EUSART1_Tx_Buff[9] == 0x88))
          {
              err_code = ble_cs_error_check_update(&m_cs, BLE_Buf, ARRAY_SIZE(BLE_Buf), BLE_CONN_HANDLE_ALL);  //for array  //20220223 Kai
              if ((err_code != NRF_SUCCESS) &&  //
                  (err_code != NRF_ERROR_INVALID_STATE) &&  //
                  (err_code != NRF_ERROR_RESOURCES) &&  //
                  (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)  //
                 )  //
              {  //
                  APP_ERROR_HANDLER(err_code);  //
              }  //     
          }                             
        }

    sending => Send 100Byte,here is the reference to the writing of Battery Service.

    ret_code_t ble_cs_error_check_update(ble_cs_t * p_cs,  uint8_t* error_messages, uint16_t len, uint16_t   conn_handle)  //for array
    {
        if (p_cs == NULL)
        {
            return NRF_ERROR_NULL;
        }
    
        ret_code_t         err_code = NRF_SUCCESS;
        ble_gatts_value_t  gatts_value;
    
        memset(&gatts_value, 0, sizeof(gatts_value));
    
        //gatts_value.len     = sizeof(uint8_t);  //for 1byte
        gatts_value.len     = len;                //for array
        gatts_value.offset  = 0;
        //gatts_value.p_value = &error_messages;  //for 1byte
        gatts_value.p_value = error_messages;     //for array
    
     
    
        // Send value if connected and notifying.
        if (p_cs->is_notification_supported)
        {
            ble_gatts_hvx_params_t hvx_params;
    
            memset(&hvx_params, 0, sizeof(hvx_params));
    
            hvx_params.handle = p_cs->error_char_handles.value_handle;
            hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
            hvx_params.offset = gatts_value.offset;
            hvx_params.p_len  = &gatts_value.len;
            hvx_params.p_data = gatts_value.p_value;
    
            if (conn_handle == BLE_CONN_HANDLE_ALL)
            {
                ble_conn_state_conn_handle_list_t conn_handles = ble_conn_state_conn_handles();
    
                // Try sending notifications to all valid connection handles.
                for (uint32_t i = 0; i < conn_handles.len; i++)
                {
                    if (ble_conn_state_status(conn_handles.conn_handles[i]) == BLE_CONN_STATUS_CONNECTED)
                    {
                        if (err_code == NRF_SUCCESS)
                        {
                            err_code = error_messages_send(&hvx_params,  conn_handles.conn_handles[i]);
                        }
                        else
                        {
                            // Preserve the first non-zero error code
                            UNUSED_RETURN_VALUE(error_messages_send(&hvx_params,  conn_handles.conn_handles[i]));
                        }
                    }
                }
            }
            else
            {
                err_code = error_messages_send(&hvx_params, conn_handle);
            }
        }
        else
        {
            err_code = NRF_ERROR_INVALID_STATE;
        }
    
        return err_code;
    }

    static ret_code_t error_messages_send(ble_gatts_hvx_params_t * const p_hvx_params,  uint16_t conn_handle)
    {
        ret_code_t err_code = sd_ble_gatts_hvx(conn_handle, p_hvx_params);
        if (err_code == NRF_SUCCESS)
        {
            NRF_LOG_INFO("Success,Packet = %d",PKT_cnt++)
        }
        else
        {
            //NRF_LOG_INFO("Fail,Packet = %d",PKT_cnt++)
        }
        return err_code;
        
    }

    Best regards,

    Kai

  • Hi Kai,

    It looks like you are passing ARRAY_SIZE(BLE_Buf) as the length to ble_cs_error_check_update() regardless of how much data you have stored in the buffer. You have to set the length to 20 if you want the notification to be 20 bytes, and not 100.

    Best regards,

    Vidar

  • Hi Vidar,

    I have change ARRAY_SIZE(BLE_Buf) to BLE_Len.

    BLE_Len mean this time how many data I want to send.

    But it's still send 100Byte.

    void BLE_PROCESS(void) 
    {
        ret_code_t err_code;
        uint8_t BLE_Len;
    
        BLE_i = BLEBufInx * BLE_Y;            
        BLE_ptr = BLE_i + &BLE_Buf[0];                  
        for (BLE_i = 0; BLE_i < BLE_Y; BLE_i++) 
        {                                                                     
          *BLE_ptr = EUSART1_Tx_Buff[BLE_i];            
          BLE_ptr = BLE_ptr + 1;                                 
        }        
        BLE_Len = BLEBufInx*BLE_Y;                                                        
        BLEBufInx++;                                         
        if (BLEBufInx >= BLE_X)                   
        {                                                              
          BLEBufInx = 0;  
            
          err_code = ble_cs_error_check_update(&m_cs, BLE_Buf, BLE_Len , BLE_CONN_HANDLE_ALL);  //for array  //20220223 Kai
          if ((err_code != NRF_SUCCESS) &&  //
              (err_code != NRF_ERROR_INVALID_STATE) &&  //
              (err_code != NRF_ERROR_RESOURCES) &&  //
              (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)  //
             )  //
          {  //
              APP_ERROR_HANDLER(err_code);  //
          }  //   
          
          memset(&BLE_Buf, 0, sizeof(BLE_Buf));                               
        }
        else
        {
          if ((EUSART1_Tx_Buff[0] == 0x66) && (EUSART1_Tx_Buff[9] == 0x88))
          {
              err_code = ble_cs_error_check_update(&m_cs, BLE_Buf, BLE_Len , BLE_CONN_HANDLE_ALL);  //for array  //20220223 Kai
              if ((err_code != NRF_SUCCESS) &&  //
                  (err_code != NRF_ERROR_INVALID_STATE) &&  //
                  (err_code != NRF_ERROR_RESOURCES) &&  //
                  (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)  //
                 )  //
              {  //
                  APP_ERROR_HANDLER(err_code);  //
              }  //     
          }                             
        }

  • The sd_ble_gatts_hvx() function will not send more data than what is specified by your BLE_Len (ble_gatts_hvx_params_t::p_len) input. So I would suggest you debug the code to check why the BLE_Len is always set to 100 bytes

  • I have tested that changing this will change the amount received each time => add_char_params.max_len = 100; //for array

    This seems to have been mandated to send 100Byte each time.

Reply Children
Related