New functionality
With the ACU6 application project set up and an application running, it’s time to add new functionality. Specifically, the application is extended to indicate GNSS location status using an LED, lighting it up when a GNSS position is fixed and turning it off if the position is lost.
Add Location Service to led-example
To interact with the location service
(A_SERVICE_ID_LOCATION), similar to how the application communicates with
the System Service API for LED control, a session must
be opened with the location service.
Modify Main Event Loop
Begin by modifying led_example.c. The main loop still starts by opening a
session to A_SERVICE_ID_SYSTEM. The IPC helper then sequences the
location service startup after the system session is
established and the retry-tracked request slot is free:
A_IPC_RESULT ipc_rc = ipc_send_open_session_req(A_SERVICE_ID_SYSTEM);
if (ipc_rc != A_IPC_RET_OK) {
fprintf(stderr, "Failed to send open session request: %s\n", a_ipc_result_str(ipc_rc));
rc = EXIT_FAILURE;
goto err_close_actia_ipc;
}
Modify IPC Helper
Since the application is now communicating with two services while also
keeping the retry timeout behavior from earlier chapters, ipc.c sequences
startup requests and LED updates through a single scheduler:
static void ipc_schedule_next_request(void)
{
A_IPC_RESULT rc = A_IPC_RET_OK;
bool sent_led_update = false;
if (pending_request != IPC_PENDING_REQUEST_NONE) {
return;
}
if (!ipc_state.system_session_open) {
rc = ipc_send_open_session_req(A_SERVICE_ID_SYSTEM);
} else {
if (!sequence_timer_started) {
if (sequence_timer_arm(1, 1) < 0) {
fprintf(stderr, "Couldn't arm timer\n");
set_event_loop_state(EVENT_LOOP_EXIT_ERROR);
return;
}
sequence_timer_started = true;
}
if (!ipc_state.location_session_open) {
rc = ipc_send_open_session_req(A_SERVICE_ID_LOCATION);
} else if (!location_requested) {
rc = ipc_location_request();
} else if (!location_subscribed) {
rc = ipc_location_subscribe();
} else {
for (size_t led_index = 0; led_index < NUM_LEDS; ++led_index) {
if (desired_led_states[led_index] == actual_led_states[led_index]) {
continue;
}
rc = ipc_set_led(leds[led_index], desired_led_states[led_index]);
sent_led_update = true;
break;
}
if (sent_led_update) {
if (rc != A_IPC_RET_OK) {
fprintf(stderr, "Couldn't send message\n");
set_event_loop_state(EVENT_LOOP_EXIT_ERROR);
}
return;
}
if (stop_requested) {
set_event_loop_state(EVENT_LOOP_EXIT_OK);
}
return;
}
}
if (rc != A_IPC_RET_OK) {
fprintf(stderr, "Couldn't send message\n");
set_event_loop_state(EVENT_LOOP_EXIT_ERROR);
}
}
Incoming messages are still dispatched by type, and open-session responses use
the from parameter from a_ipc_recv() to distinguish system and
location replies:
bool ipc_handle_actia_ipc_event(struct epoll_event *event,
const led_example_options_t *params)
{
if ((event->events & EPOLLIN) && event->data.fd == retry_timer_fd) {
uint64_t timers = 0;
ssize_t read_size = read(retry_timer_fd, &timers, sizeof(timers));
if (read_size != (ssize_t)sizeof(timers)) {
fprintf(stderr, "Failed to read retry timer\n");
set_event_loop_state(EVENT_LOOP_EXIT_ERROR);
return true;
}
ipc_handle_retry_timer_event();
return true;
}
if ((event->events & EPOLLIN) && event->data.fd == sequence_timer_fd) {
uint64_t timers = 0;
ssize_t read_size = read(sequence_timer_fd, &timers, sizeof(timers));
if (read_size != (ssize_t)sizeof(timers)) {
fprintf(stderr, "Failed to read sequence timer\n");
set_event_loop_state(EVENT_LOOP_EXIT_ERROR);
return true;
}
ipc_handle_sequence_timer_event(params);
return true;
}
if (!(event->events & EPOLLIN) || event->data.fd != a_ipc_get_fd(a_ipc)) {
return false;
}
int from = -1;
A_IPC_RESULT rc = a_ipc_recv(a_ipc, &from, msg);
if (rc != A_IPC_RET_OK) {
fprintf(stderr, "Failed to fetch message\n");
set_event_loop_state(EVENT_LOOP_EXIT_ERROR);
return true;
}
switch (msg->info.type) {
case A_IPC_MSG_IPC_OPEN_SESSION_RSP:
ipc_handle_open_session_rsp(&msg->ipc_open_session_rsp, from);
break;
case A_IPC_MSG_LED_SET_RSP:
ipc_handle_led_set_rsp(&msg->led_set_rsp);
break;
case A_IPC_MSG_LOCATION_REQUEST_RSP:
ipc_handle_location_request_rsp();
break;
case A_IPC_MSG_LOCATION_SUBSCRIBE_RSP:
Create functions for sending A_IPC_MSG_LOCATION_REQUEST_REQ to start the
location service and
A_IPC_MSG_LOCATION_SUBSCRIBE_REQ to subscribe to location updates:
A_IPC_RESULT ipc_location_request(void)
{
static char *user = "led-example";
A_IPC_RESULT rc = a_ipc_init_location_request_req(msg, 15);
if (rc != A_IPC_RET_OK) {
fprintf(stderr, "Could not init A_IPC_MSG_LOCATION_REQUEST_REQ message %d\n", rc);
return rc;
}
msg->location_request_req.user.value = user;
msg->location_request_req.user.length = strlen(user);
rc = a_ipc_send(a_ipc, A_SERVICE_ID_LOCATION, msg);
if (rc != A_IPC_RET_OK) {
return rc;
}
if (ipc_mark_request_pending(IPC_PENDING_REQUEST_LOCATION_REQUEST) < 0) {
return A_IPC_RET_ERROR;
}
return A_IPC_RET_OK;
}
A_IPC_RESULT ipc_location_subscribe(void)
{
A_IPC_RESULT rc = a_ipc_init_location_subscribe_req(msg);
if (rc != A_IPC_RET_OK) {
fprintf(stderr, "Could not init A_IPC_MSG_LOCATION_SUBSCRIBE_REQ message %d\n", rc);
return rc;
}
rc = a_ipc_send(a_ipc, A_SERVICE_ID_LOCATION, msg);
if (rc != A_IPC_RET_OK) {
return rc;
}
if (ipc_mark_request_pending(IPC_PENDING_REQUEST_LOCATION_SUBSCRIBE) < 0) {
return A_IPC_RET_ERROR;
}
return A_IPC_RET_OK;
Updating ipc.h
Finally, update ipc.h to reflect the new function prototypes:
int ipc_initialize_actia_ipc(int ipc_id, int epfd);
A_IPC_RESULT ipc_send_open_session_req(int to);
bool ipc_handle_actia_ipc_event(struct epoll_event *event,
const led_example_options_t *params);
A_IPC_RESULT ipc_set_led(a_ipc_enum_led_pin_type led, uint8_t state);
A_IPC_RESULT ipc_location_request(void);
A_IPC_RESULT ipc_location_subscribe(void);
void ipc_destroy_actia_ipc(void);
These updates enable the IPC helper to manage interactions with both the LED and location services, using an LED to indicate the GNSS location status.
Modify LED Example
Changes are made to ipc.c to use the red LED for blinking every second and
the green LED to indicate GNSS location fix status.
Update the IPC response handling to manage the different messages:
static void ipc_handle_open_session_rsp(a_ipc_msg_ipc_open_session_rsp *msg_rsp,
int from)
{
if (msg_rsp->result != A_IPC_ENUM_IPC_OPEN_SESSION_RESULT_OK) {
fprintf(stderr, "Error opening session: %d\n", msg_rsp->result);
set_event_loop_state(EVENT_LOOP_EXIT_ERROR);
return;
}
if ((pending_request == IPC_PENDING_REQUEST_OPEN_SYSTEM_SESSION) && (from == A_SERVICE_ID_SYSTEM)) {
ipc_state.system_session_open = true;
} else if ((pending_request == IPC_PENDING_REQUEST_OPEN_LOCATION_SESSION) &&
(from == A_SERVICE_ID_LOCATION)) {
ipc_state.location_session_open = true;
} else {
return;
}
if (ipc_clear_pending_request() < 0) {
return;
}
ipc_schedule_next_request();
}
static void ipc_handle_led_set_rsp(a_ipc_msg_led_set_rsp *msg_rsp)
{
size_t completed_led_index = pending_led_index;
uint8_t completed_led_state = pending_led_state;
if (pending_request != IPC_PENDING_REQUEST_LED_SET) {
return;
}
if (msg_rsp->result != A_IPC_ENUM_LED_RESULT_OK) {
fprintf(stderr, "Error lighting LED: %d\n", msg_rsp->result);
set_event_loop_state(EVENT_LOOP_EXIT_ERROR);
return;
}
if (ipc_clear_pending_request() < 0) {
return;
}
actual_led_states[completed_led_index] = completed_led_state;
ipc_schedule_next_request();
}
static void ipc_handle_location_request_rsp(void)
{
if (pending_request != IPC_PENDING_REQUEST_LOCATION_REQUEST) {
return;
}
location_requested = true;
if (ipc_clear_pending_request() < 0) {
return;
}
ipc_schedule_next_request();
}
static void ipc_handle_location_subscribe_rsp(void)
{
if (pending_request != IPC_PENDING_REQUEST_LOCATION_SUBSCRIBE) {
return;
}
location_subscribed = true;
if (ipc_clear_pending_request() < 0) {
return;
}
printf("Subscribed to location updates\n");
ipc_schedule_next_request();
}
This updated IPC handling now manages both the blinking of the red LED and the status indication of the green LED while keeping each request-response pair eligible for retry if a response is missed.
Modify Timeout Handler
The timer event handling is updated to blink the red LED and monitor the status of the location updates. The green LED indicates the receipt of new location updates, and it is turned off if no updates are received for a specific duration.
static void ipc_handle_location_publish_ind(a_ipc_msg_location_publish_ind *msg_ind)
{
clock_gettime(CLOCK_MONOTONIC, &ipc_state.last_location_update_time);
printf("Longitude: %d, Latitude: %d, Altitude: %d\n",
msg_ind->longitude,
msg_ind->latitude,
msg_ind->altitude);
desired_led_states[1] = LED_ON;
ipc_schedule_next_request();
}
static void ipc_handle_sequence_timer_event(const led_example_options_t *params)
{
struct timespec current_time;
struct timespec diff_time;
clock_gettime(CLOCK_MONOTONIC, ¤t_time);
diff_time.tv_sec = current_time.tv_sec - ipc_state.last_location_update_time.tv_sec;
diff_time.tv_nsec = current_time.tv_nsec - ipc_state.last_location_update_time.tv_nsec;
if (diff_time.tv_nsec < 0) {
diff_time.tv_sec -= 1;
diff_time.tv_nsec += 1000000000;
}
desired_led_states[0] = (desired_led_states[0] == LED_ON) ? LED_OFF : LED_ON;
if (diff_time.tv_sec > 5) {
desired_led_states[1] = LED_OFF;
}
ipc_state.ticks += 1;
if ((ipc_state.ticks >= params->max_ticks) && (params->max_ticks != 0)) {
stop_requested = true;
if (sequence_timer_disarm() < 0) {
fprintf(stderr, "Couldn't disarm timer\n");
set_event_loop_state(EVENT_LOOP_EXIT_ERROR);
return;
}
}
ipc_schedule_next_request();
This function alternates the red LED between on and off states, representing the system’s active status. It also checks the time elapsed since the last location update. If more than 5 seconds pass without a new update, the green LED is turned off, indicating the loss of a recent location fix. Any resulting LED updates are fed back into the same serialized request scheduler so the retry timer still tracks the correct outstanding request.
Rebuilding the Application for the last time
When a package has been modified it must be rebuilt before building the final target image. By cleaning the package’s current build it will be rebuilt when building the target image.
Clean the build directory:
make buildroot-led-example-dircleanCompile the application:
makeFlash the compiled program onto the target device:
make flashConnect to the device:
ssh acu6proRun the program:
led-example
Observing Application Behavior
Observe the LEDs on the device. The red LED should blink, indicating active operation. When a location fix is obtained, the green LED should light up. The position is also printed in the terminal.
In this chapter, led-example is meant to represent an ongoing application
that reacts to location updates over time. For that reason, both the manual
invocation led-example and the boot-time service are described as
continuous: once started, they keep running until they are explicitly
stopped. This keeps the boot behavior aligned with the location-driven
functionality shown here, instead of treating startup as a short bounded demo.
Caution
Acquiring a location fix indoors can be difficult. If needed, use an external antenna and place it outdoors or near a window.
Future Considerations
For applications that can read NMEA sentences directly, a simpler approach may
be enough. Send A_IPC_MSG_LOCATION_REQUEST_REQ and then read the raw NMEA
stream over TCP/IP from 198.18.2.1:1001.