How do I size the App Scheduler to take advantage of NAKs from the SoftDevice?

This is a follow-up to my previous postI'm developing a BLE Peripheral application on the nRF52832 that uses NRF_SDH_DISPATCH_MODEL_APPSH to receive events from the SoftDevice. My application involves using some software-implemented elliptic-curve cryptographic computations in APP_IRQ_PRIORITY_THREAD, which can take up to 1.5 seconds to run.

I found that if I send enough Characteristic Writes while the nRF52832 is doing the ECC computations, I can cause one of two effects, based on how I've configured the size of the App Scheduler. If it is 50, app_sched_event_put() in SD_EVT_IRQHandler() returns NRF_ERROR_NO_MEM. If it is 60, then I instead see NAKs from the SoftDevice to the Central on my BT sniffer.

My understanding is that this is because if the SoftDevice can buffer more Writes than the App Scheduler, it will overflow the App Scheduler because it can enqueue events from an interrupt context. However, if the App Scheduler is large enough, it will be able to handle all the events from the SoftDevice, and because the ECC computations are preventing another call to nrf_sdh_evts_poll(), the SoftDevice's internal buffers will fill, so it will start to NAK Writes from the Central.

I would like to be able to rely on the NAK behavior, so I need non-experimental reasoning for why there is this change in behavior between the App Scheduler sizes of 50 and 60. What is the maximum number of events that the SoftDevice could buffer from BLE because of, say, a malicious Central?

I am using s132 v5.1.0.

Related