|
7 | 7 | #include <zephyr/kernel.h> |
8 | 8 | #include <assert.h> |
9 | 9 | #include <stdio.h> |
| 10 | +#include <stdlib.h> |
10 | 11 | #include <zephyr/logging/log.h> |
11 | 12 | #include <zephyr/sys_clock.h> |
12 | 13 | #include <date_time.h> |
@@ -320,41 +321,52 @@ static void agnss_requestor(struct k_work *) |
320 | 321 | { |
321 | 322 | int err; |
322 | 323 | struct nrf_modem_gnss_agnss_data_frame req; |
323 | | - static char agnss_rest_data_buf[NRF_CLOUD_AGNSS_MAX_DATA_SIZE]; |
324 | 324 | struct nrf_cloud_coap_agnss_request request = { |
325 | 325 | NRF_CLOUD_COAP_AGNSS_REQ_CUSTOM, |
326 | 326 | &req, |
327 | 327 | }; |
| 328 | + char *agnss_rest_data_buf = calloc(1, NRF_CLOUD_AGNSS_MAX_DATA_SIZE); |
| 329 | + |
| 330 | + if (!agnss_rest_data_buf) { |
| 331 | + LOG_ERR("Failed to allocate A-GNSS data buffer."); |
| 332 | + return; |
| 333 | + } |
328 | 334 |
|
329 | 335 | err = read_agnss_req(&req); |
330 | 336 | if (err) { |
331 | 337 | LOG_ERR("Failed to read A-GNSS request (%d).", err); |
332 | | - return; |
| 338 | + goto cleanup; |
333 | 339 | } |
334 | 340 |
|
335 | | - struct nrf_cloud_coap_agnss_result result = {agnss_rest_data_buf, |
336 | | - sizeof(agnss_rest_data_buf), 0}; |
| 341 | + struct nrf_cloud_coap_agnss_result result = { |
| 342 | + agnss_rest_data_buf, |
| 343 | + NRF_CLOUD_AGNSS_MAX_DATA_SIZE, |
| 344 | + 0 |
| 345 | + }; |
337 | 346 | struct lte_lc_cells_info net_info = {0}; |
338 | 347 |
|
339 | 348 | err = get_single_cell_info(&net_info.current_cell); |
340 | 349 | if (err) { |
341 | 350 | LOG_ERR("Failed to obtain single-cell cellular network information (%d).", err); |
342 | | - return; |
| 351 | + goto cleanup; |
343 | 352 | } |
344 | 353 | request.net_info = &net_info; |
345 | 354 |
|
346 | 355 | err = nrf_cloud_coap_agnss_data_get(&request, &result); |
347 | 356 | if (err) { |
348 | 357 | LOG_ERR("Failed to request A-GNSS data via CoAP (%d).", err); |
349 | | - return; |
| 358 | + goto cleanup; |
350 | 359 | } |
351 | 360 |
|
352 | 361 | err = nrf_cloud_agnss_process(result.buf, result.agnss_sz); |
353 | 362 | if (err) { |
354 | 363 | LOG_ERR("Failed to process A-GNSS data, error: %d", err); |
355 | | - return; |
| 364 | + goto cleanup; |
356 | 365 | } |
357 | 366 | LOG_INF("A-GNSS data received via CoAP."); |
| 367 | + |
| 368 | +cleanup: |
| 369 | + free(agnss_rest_data_buf); |
358 | 370 | } |
359 | 371 | #endif /* CONFIG_NRF_CLOUD_AGNSS */ |
360 | 372 |
|
|
0 commit comments