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

Cannot remove the custom service

MCU: nRF52832
SDK VERSION: 17.0.0_9d13099
SOFTDEVICE: 7.0.1 (s132)

I have an application with some custom services (4) with different characteristics number and behavior (read/write/notify) and everything works just fine. The problem started when I wanted to create different modes of operation in which not every service is initialized e.g. mode 1 when everything is operational and mode 2 where only DIS, BAS and one of the custom services is on. When entering mode 2 (which now is just flashing new code with services init lines commented) and trying to connect, I am getting error

"Failed to get services (BLE_ERROR_INVALID_CONN_HANDLE). 

Funny is, if I don't init standard services everything is just fine. The problem only appears when I don't init my custom service/s. I've tested resetting the device, removing power from the board and full erasing memory and flashing everything (bootloader, softdevice and application) again - the same result. I've tried with sd_ble_uuid_vs_remove but also with no result.

The only option to achieve my goal is to call custom service init and within it comment the service and characteristics adding lines. So the "fake" initialization looks like this:

void StsService_init(void){
	ret_code_t	errorCode;
	ble_sts_init_t	stsInitStructure;
	errorsNotyficationsEnabled = false;
	statusNotyficationsEnabled = false;
	memset(&stsInitStructure, 0, sizeof(stsInitStructure));
	stsInitStructure.evt_handler = stsEventHandler;

	BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&stsInitStructure.sts_error_value_char_attr_md.cccd_write_perm);
	BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&stsInitStructure.sts_error_value_char_attr_md.read_perm);

	BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&stsInitStructure.sts_status_value_char_attr_md.cccd_write_perm);
	BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&stsInitStructure.sts_status_value_char_attr_md.read_perm);

	errorCode = ble_sts_init(&m_sts, &stsInitStructure);
	APP_ERROR_CHECK(errorCode);
}

and my ble_sts_init is: 

uint32_t ble_sts_init(ble_sts_t * p_sts, const ble_sts_init_t * p_sts_init){
	if (p_sts == NULL || p_sts_init == NULL){
		return NRF_ERROR_NULL;
	}
	uint32_t   err_code;
	ble_uuid_t ble_uuid;

	// Initialize service structure
	p_sts->conn_handle = BLE_CONN_HANDLE_INVALID;
	p_sts->evt_handler = p_sts_init->evt_handler;

	// Add Status Service UUID
//	ble_uuid128_t base_uuid = {STATUS_SERVICE_UUID_BASE};
//	err_code =  sd_ble_uuid_vs_add(&base_uuid, &p_sts->uuid_type);
//	VERIFY_SUCCESS(err_code);
//	ble_uuid.type = p_sts->uuid_type;
//	ble_uuid.uuid = STATUS_SERVICE_UUID;

//	// Add the Status Service
//	err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_sts->service_handle);
//	if (err_code != NRF_SUCCESS){
//		return err_code;
//	}
//	// Add the States Characteristic
//	err_code = sts_status_char_add(p_sts,p_sts_init);
//	if (err_code != NRF_SUCCESS){
//		return err_code;
//	}
//	// Add the Errors Characteristic
//	err_code = sts_error_char_add(p_sts, p_sts_init);
//	if (err_code != NRF_SUCCESS){
//		return err_code;
//	}

	return NRF_SUCCESS;
}

This produces the result I wanted - the service isn't visible. My question is - why simply removing StsService_init() from code doesn't work? Why do I have to do special workaround? If i comment line 9 and 10 in ble_sts_init it will also produce error. 

Any help will be appreciated. 

Regards

Parents
  • Hi,

    This produces the result I wanted - the service isn't visible. My question is - why simply removing StsService_init() from code doesn't work? Why do I have to do special workaround? If i comment line 9 and 10 in ble_sts_init it will also produce error. 

    You always start with an empty set of services etc when you initialize the SoftDevice. If you do not add a service/characteristic/anything, it will not be there. Also, there is no way to remove a service from the SoftDevice without re-enabling it.

    I would be interesting in knowing how you find that the service has not been removed? The only two reasons I can think of without knowing more is that either the peer has cached the services and therefor you think they are still there even though they are not, or that you have some code that runs and adds the service(s) even that you have somehow missed. I do not see anything in what you write or your small code snippets that indicate the latter, though.

  • Hello.

    Thanks for reply. As I mentioned, I've even fully erased chip and programmed everything again with commented initialization line. The same result. I thought that maybe I need to reset BLE dongle and NRF connect but it didn't help me at all. 

    As for your question. When I've commented "StsInit" in code I couldn't connect to the device due to mentioned error. The only way working for me in this moment is to call StsInit with bool parameter which is passed to ble_sts_init. The code looks like below.

    uint32_t ble_sts_init(ble_sts_t * p_sts, const ble_sts_init_t * p_sts_init, bool enableService){
    	if (p_sts == NULL || p_sts_init == NULL){
    		return NRF_ERROR_NULL;
    	}
    	uint32_t   err_code;
    	ble_uuid_t ble_uuid;
    
    	// Initialize service structure
    	p_sts->conn_handle = BLE_CONN_HANDLE_INVALID;
    	p_sts->evt_handler = p_sts_init->evt_handler;
    
    	//TODO workaround to be changed later
    	if (!enableService)
    		return NRF_SUCCESS;
    
    	// Add Status Service UUID
    	ble_uuid128_t base_uuid = {STATUS_SERVICE_UUID_BASE};
    	err_code =  sd_ble_uuid_vs_add(&base_uuid, &p_sts->uuid_type);
    	VERIFY_SUCCESS(err_code);
    	ble_uuid.type = p_sts->uuid_type;
    	ble_uuid.uuid = STATUS_SERVICE_UUID;
    
    	// Add the Status Service
    	err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_sts->service_handle);
    	if (err_code != NRF_SUCCESS){
    		return err_code;
    	}
    	// Add the States Characteristic
    	err_code = sts_status_char_add(p_sts,p_sts_init);
    	if (err_code != NRF_SUCCESS){
    		return err_code;
    	}
    	// Add the Errors Characteristic
    	err_code = sts_error_char_add(p_sts, p_sts_init);
    	if (err_code != NRF_SUCCESS){
    		return err_code;
    	}
    
    	return NRF_SUCCESS;
    }

    When I want to start application in mode 2 I just init this service with false parameter. 

    So, if I enable mode 1 I can see standard services like BAS, DIS and my 4 custom services. When I enter mode 2 I see only BAS, DIS and 1 of my custom services. This is exactly what I wanted to achieve but I don't quite understand why I have to do it that way so this lead me to ask this question :)

  • Hi,

    Cwanci said:
    Thanks for reply. As I mentioned, I've even fully erased chip and programmed everything again with commented initialization line. The same result. I thought that maybe I need to reset BLE dongle and NRF connect but it didn't help me at all. 

    As mentioned none of this really matters, as everything is configured again when the SoftDevice is configured. It does not "remember" previous configuration, services etc. As long as you reset the device, the new configuration will be whatever your current firmware configures regardless of what was there before. However, phones etc. will cache a lot of information so you cannot rely on that. Therefore I was very interested in knowing how you see that old services are still there? But reading your last post I suspect an issue with your code.

    Cwanci said:
    As for your question. When I've commented "StsInit" in code I couldn't connect to the device due to mentioned error.

     This error: "Failed to get services (BLE_ERROR_INVALID_CONN_HANDLE)."? Where do you get this error?

    Cwanci said:

    When I want to start application in mode 2 I just init this service with false parameter. 

    So, if I enable mode 1 I can see standard services like BAS, DIS and my 4 custom services. When I enter mode 2 I see only BAS, DIS and 1 of my custom services. This is exactly what I wanted to achieve but I don't quite understand why I have to do it that way so this lead me to ask this question

    I see. This makes sense. If you do not add the service, it does not show (as expected). What is the problem with this? Again, if the service is there it absolutely must be because you configure it. So to not have the service you need to make sure to not run the code that adds it. Once added, the only way to remove it is to re-initialize the SoftDevice (typically after a reset).

  • This error: "Failed to get services (BLE_ERROR_INVALID_CONN_HANDLE)."? Where do you get this error?

    I get this error in NRF Connect app, when I click "Connect" button. The device doesn't connect or rather connects and disconnects immediately and I get this popup. 

    I see. This makes sense. If you do not add the service, it does not show (as expected). What is the problem with this? Again, if the service is there it absolutely must be because you configure it. So to not have the service you need to make sure to not run the code that adds it. Once added, the only way to remove it is to re-initialize the SoftDevice (typically after a reset).

    I thought that when I comment the StsInit line and erase memory + program this code, the SoftDevice would be "reinitialized" without this service. This service isn't added in any other code line - only StsInit which calls ble_sts_init. As you see in ble_sts_init service is enabled and characteristics are added.

    What is the problem with this?

    None. Everything works as I want, but I wanted to understand why it doesn't work with simple StsInit removal and e.g. disconnecting power from the board. 

  • Hi,

    Cwanci said:
    I get this error in NRF Connect app, when I click "Connect" button. The device doesn't connect or rather connects and disconnects immediately and I get this popup. 

    Ah, I see. Then this is probably the phone caching the services. I expect you would see that things work as expected if you use for instance nRF Connect for desktop (no caching behind the scenes) or change the BLE address of the nRF so that the phone would see it as a new device. You could also try to toggle bluetooth off and on in the phone, though if and how that works differ a bit.

    Cwanci said:
    thought that when I comment the StsInit line and erase memory + program this code, the SoftDevice would be "reinitialized" without this service.

    Yes, that is the case.

    Cwanci said:
    None. Everything works as I want, but I wanted to understand why it doesn't work with simple StsInit removal and e.g. disconnecting power from the board. 

    Must be phone caching, but as mentioned you can easily confirm by using nrf connect for desktop.

  • Ah, I see. Then this is probably the phone caching the services.

    Wrong guess Wink As I mentioned before: 

    I thought that maybe I need to reset BLE dongle and NRF connect but it didn't help me at all. 

    I am using USB dongle and NRF Connect for desktop all the time . I don't quite trust mobile app... to be honest Slight smile So as I mentioned I thought maybe I need to delete bond information or just replug dongle and reset NRFConnect. Didn't help. Full chip erase deleted bonds in the device and reset app deleted bonds in it. I guess it must be something else. 

    Thanks for responses anyway. It works as I wanted so I moving forward. But I'll keep in mind to investigate this further in the future. 

Reply
  • Ah, I see. Then this is probably the phone caching the services.

    Wrong guess Wink As I mentioned before: 

    I thought that maybe I need to reset BLE dongle and NRF connect but it didn't help me at all. 

    I am using USB dongle and NRF Connect for desktop all the time . I don't quite trust mobile app... to be honest Slight smile So as I mentioned I thought maybe I need to delete bond information or just replug dongle and reset NRFConnect. Didn't help. Full chip erase deleted bonds in the device and reset app deleted bonds in it. I guess it must be something else. 

    Thanks for responses anyway. It works as I wanted so I moving forward. But I'll keep in mind to investigate this further in the future. 

Children
No Data
Related