This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Differentiate between different writes

I have a few characteristics under a service, and I'm trying to store the value if any of these characteristics is written to. However I run into an issue where if I enable services on the Master Control Panel, my code thinks that the value of the characteristic is being written.

How do I differentiate between a write to the actual data value of my characteristic and whatever the master control panel is doing when I enable services?

static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
{
    dm_ble_evt_handler(p_ble_evt);
    on_ble_evt(p_ble_evt);
    check_status(p_ble_evt);
}

static void check_status(ble_evt_t * p_ble_evt){	
    
    ble_gatts_evt_write_t *p_evt_write = &p_ble_evt -> evt.gatts_evt.params.write;
    
    switch (p_ble_evt -> header.evt_id)
        {
        case BLE_GATTS_EVT_WRITE:
            if (p_evt_write -> context.srvc_uuid.uuid == DEVICE_STATUS_SERVICE_UUID){
                /* If mode characteristic was written to update the status value */
                if (p_evt_write -> context.char_uuid.uuid == DEVICE_STATUS_MODE_UUID){
                    m_status.mode = p_evt_write -> data[0];
                }
                /* If datarate characteristic was written to update the status value */
                else if (p_evt_write -> context.char_uuid.uuid == DEVICE_STATUS_DATARATE_UUID){
                    m_status.datarate = p_evt_write -> data[0];
                }
                advertising_init();     /* Update advertising data with new mode and datarate */
            }
        break;
    }
}

Once I enable services, both m_status.mode and m_status.datarate are set to 0x03. If I write to both of these characteristics after enabling services the correct values are written to them. But for example if I only modify one, the other one will have 0x03 stored in it still (even though the attribute is storing 0x0).

Parents
  • You should be able use the GATT Server Attribute Types:

    /** @defgroup BLE_GATTS_ATTR_TYPES GATT Server Attribute Types
     * @{ */
    #define BLE_GATTS_ATTR_TYPE_INVALID         0x00  /**< Invalid Attribute Type. */
    #define BLE_GATTS_ATTR_TYPE_PRIM_SRVC_DECL  0x01  /**< Primary Service Declaration. */
    #define BLE_GATTS_ATTR_TYPE_SEC_SRVC_DECL   0x02  /**< Secondary Service Declaration. */
    #define BLE_GATTS_ATTR_TYPE_INC_DECL        0x03  /**< Include Declaration. */
    #define BLE_GATTS_ATTR_TYPE_CHAR_DECL       0x04  /**< Characteristic Declaration. */
    #define BLE_GATTS_ATTR_TYPE_CHAR_VAL        0x05  /**< Characteristic Value. */
    #define BLE_GATTS_ATTR_TYPE_DESC            0x06  /**< Descriptor. */
    #define BLE_GATTS_ATTR_TYPE_OTHER           0x07  /**< Other, non-GATT specific type. */
    /** @} */
    

    When you get a write event check that the write is to the characteristic value, not the descriptor.

    if(p_ble_evt->evt.gatts_evt.params.write.context.type == BLE_GATTS_ATTR_TYPE_CHAR_VAL)
    {
          //Your code
    }
    
Reply
  • You should be able use the GATT Server Attribute Types:

    /** @defgroup BLE_GATTS_ATTR_TYPES GATT Server Attribute Types
     * @{ */
    #define BLE_GATTS_ATTR_TYPE_INVALID         0x00  /**< Invalid Attribute Type. */
    #define BLE_GATTS_ATTR_TYPE_PRIM_SRVC_DECL  0x01  /**< Primary Service Declaration. */
    #define BLE_GATTS_ATTR_TYPE_SEC_SRVC_DECL   0x02  /**< Secondary Service Declaration. */
    #define BLE_GATTS_ATTR_TYPE_INC_DECL        0x03  /**< Include Declaration. */
    #define BLE_GATTS_ATTR_TYPE_CHAR_DECL       0x04  /**< Characteristic Declaration. */
    #define BLE_GATTS_ATTR_TYPE_CHAR_VAL        0x05  /**< Characteristic Value. */
    #define BLE_GATTS_ATTR_TYPE_DESC            0x06  /**< Descriptor. */
    #define BLE_GATTS_ATTR_TYPE_OTHER           0x07  /**< Other, non-GATT specific type. */
    /** @} */
    

    When you get a write event check that the write is to the characteristic value, not the descriptor.

    if(p_ble_evt->evt.gatts_evt.params.write.context.type == BLE_GATTS_ATTR_TYPE_CHAR_VAL)
    {
          //Your code
    }
    
Children
Related