LTE link controller library

The LTE link controller library is a layer above the nRF Modem library and provides functionality to control the LTE link on the nRF9160.

Enabling the library

1. To enable the LTE link controller library in your application, enable the following Kconfig:

2. Disable the automatic init and connect functionality of the library

Note

The library supports the functionality to automatically initialize and connect to the modem before the application starts. This functionality can reduce complexity, but it also increases the time until the application starts while blocking all other operations.

3. Include the header file of the LTE link controller library in your source code.

#include <modem/lte_lc.h>

Defining a callback function

One advantage to using the LTE link controller library is having a callback function that allows the application to get callbacks from the library, thereby offloading this process.

4. Define the callback function lte_handler().

For this lesson and subsequent exercise, LTE_LC_EVT_NW_REG_STATUS is the only event where the callback function executes something. This event carries information about the modem’s network registration status, which can tell us if an LTE link has been established.

In the function below, we check the registration status of evt->nw_reg_status of type lte_lc_nw_reg_status and proceed to unblock the main() function by giving the semaphore only if the registration status is of type registered home or registered roaming, indicating an LTE connection.

static void lte_handler(const struct lte_lc_evt *const evt)
{
	switch (evt->type) {
	case LTE_LC_EVT_NW_REG_STATUS:
		if ((evt->nw_reg_status != LTE_LC_NW_REG_REGISTERED_HOME) &&
			(evt->nw_reg_status != LTE_LC_NW_REG_REGISTERED_ROAMING)) {
			break;
		}
		LOG_INF("Network registration status: %s",
				evt->nw_reg_status == LTE_LC_NW_REG_REGISTERED_HOME ?
				"Connected - home network" : "Connected - roaming");
		k_sem_give(&lte_connected);
		break;
	default:
		break;
	}
}

The documentation for enum lte_lc_evt_type explains all the event types in the LTE link controller library.

Initialize and connect

5. Initialize and connect to LTE network, using the function lte_lc_init_and_connect_async(), which has the following signature.

lte_lc_init_and_connect_async() signature
lte_lc_init_and_connect_async(lte_handler);

Using a semaphore

To ensure the application doesn’t proceed with other operations until an LTE connection is established, we will add a semaphore.

6. Define the semaphore lte_connected using K_SEM_DEFINE().

Since this semaphore will only be taken once, after the LTE connection function is called and given once, when the LTE connection is established, we pass initial_count 0 and count_limit 1.

K_SEM_DEFINE(lte_connected, 0, 1);

7. After calling lte_lc_init_and_connect_async(), take the semaphore using k_sem_take().

k_sem_take(&lte_connected, K_FOREVER);

8. To make sure the application doesn’t proceed until an LTE connection is established, we want to give the semaphore, using k_sem_give(), in the event handler, after the registration status is confirmed as connected.

     case LTE_LC_EVT_NW_REG_STATUS:
             if ((evt->nw_reg_status != LTE_LC_NW_REG_REGISTERED_HOME) &&
             (evt->nw_reg_status != LTE_LC_NW_REG_REGISTERED_ROAMING)) {
                     break;
             }

             printk("Connected to: %s network\n",
             evt->nw_reg_status == LTE_LC_NW_REG_REGISTERED_HOME ? "home" : "roaming");
             k_sem_give(&lte_connected);
             break;

Even though we are using a semaphore to make sure the application doesn’t proceed until an LTE connection is established, this is a non-blocking method because the application can call functions and create threads after lte_lc_init_and_connect_async() has been called.

Register an account
Already have an account? Log in
(All fields are required unless specified optional)

Forgot your password?
Enter your email address, and we will send a link to reset your password.