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

Calling inbuilt_key functions from inside uart callback fails

I have a setup similar to this

void program_uart_cb(struct uart_event *evt, void *data)
{
    printk("before");
    nrf_sec_tag_t sec_tag = (nrf_sec_tag_t) sec_tag_list[0];
    nrf_inbuilt_key_write(sec_tag,
            NRF_KEY_MGMT_CRED_TYPE_CA_CHAIN,
            "Hello",
            strlen("Hello"));
    printk("after");
}


void main(void)
{
    struct device *uart = device_get_binding("UART_1");
    uart_callback_set(uart, program_uart_cb, uart);
    uart_rx_enable(uart, rx_buf, sizeof(rx_buf), 50);
    while (true) {
        k_sleep(100);
    }
}

When the callback is executed, the execution will enter the inbuilt_key function and then the output will stop and the execution will halt. This seems to be the case for all nrf_inbuilt_key functions. Any help would be appreciated, thanks.

Parents Reply Children
  • Hi, thanks for the response.

    I believe the bsdlib is enabled, and I have tested with running bsd_init() first to the same result, and the same thing with the updated modem key management function. See updated code:

    #include <zephyr.h>
    ...
    #include <nrf_key_mgmt.h>
    #include <nrf_inbuilt_key.h>
    #include <modem/modem_key_mgmt.h>
    
    
    static u8_t rx_buf[2048];
    static sec_tag_t sec_tag_list[] = { CONFIG_SEC_TAG };
    /* UART Callback */
    void program_uart_cb(struct uart_event *evt, void *data)
    {
        nrf_sec_tag_t sec_tag = (nrf_sec_tag_t) sec_tag_list[0];
        modem_key_mgmt_write(sec_tag,
                NRF_KEY_MGMT_CRED_TYPE_CA_CHAIN,
                "Hello",
                strlen("Hello"));
    }
    
    
    void main(void)
    {
        struct device *uart = device_get_binding("UART_1");
        int counter = 0;
        bsd_init();
        printf("alive! %d\n", counter);
        uart_callback_set(uart, program_uart_cb, uart);
        uart_rx_enable(uart, rx_buf, sizeof(rx_buf), 50);
    
        while (true) {
            counter++;
            k_sleep(1000);
            printf("alive! %d\n", counter);
        }
    }

    Removing the modem_key_mgmt_write line does provide the intended behavior. It seems to be that key management functions from an IRQ context will cause the program to halt and error. Currently I am attempting to write to a buffer and then do the key write outside the IRQ but I'm running into some other, unrelated errors when I do 3 certs at once (perhaps memory errors).

    Thanks again.

  • Hi Sunkyl,
    I would highly recommend that you look into the https_client sample and use the cert_provision() as a reference.

  • I've opted to store the cert in a buffer during the IRQ and do the cert write function after the IRQ. All seems to work fine that way. Thanks for your time.

Related