Skip to content

Commit cb16a31

Browse files
committed
Review fixes #2
1 parent fabb777 commit cb16a31

3 files changed

Lines changed: 47 additions & 39 deletions

File tree

app/src/sm_at_gnss.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,22 @@ static void agnss_requestor(struct k_work *)
343343
NRF_CLOUD_AGNSS_MAX_DATA_SIZE,
344344
0
345345
};
346-
struct lte_lc_cells_info net_info = {0};
347346

348-
sm_at_nrfcloud_ncellmeas(1, &net_info);
349-
if (net_info.current_cell.id == LTE_LC_CELL_EUTRAN_ID_INVALID) {
350-
goto cleanup;
351-
}
352-
request.net_info = &net_info;
347+
#if defined(CONFIG_SM_NRF_CLOUD_LOCATION)
348+
struct lte_lc_cells_info *net_info = sm_at_nrfcloud_ncellmeas(1, false);
353349

350+
if (net_info != NULL && net_info->current_cell.id != LTE_LC_CELL_EUTRAN_ID_INVALID) {
351+
request.net_info = net_info;
352+
} else {
353+
LOG_WRN("Requesting A-GNSS data without location assistance");
354+
free(net_info);
355+
}
356+
#endif
354357
err = nrf_cloud_coap_agnss_data_get(&request, &result);
358+
#if defined(CONFIG_SM_NRF_CLOUD_LOCATION)
359+
free(request.net_info);
360+
request.net_info = NULL;
361+
#endif
355362
if (err) {
356363
LOG_ERR("Failed to request A-GNSS data via CoAP (%d).", err);
357364
goto cleanup;

app/src/sm_at_nrfcloud.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -523,27 +523,32 @@ static void nrfcloud_cell_data_cleanup(void)
523523
nrfcloud_cell_data = NULL;
524524
}
525525

526-
void sm_at_nrfcloud_ncellmeas(uint8_t cell_count, struct lte_lc_cells_info *cell_data)
526+
struct lte_lc_cells_info *sm_at_nrfcloud_ncellmeas(uint8_t cell_count, bool send_loc_req)
527527
{
528528
int err;
529529
uint8_t ncellmeas3_cell_count;
530530
int rrc_mode;
531+
struct lte_lc_cells_info *cell_data = NULL;
531532
struct lte_lc_cell *cells = NULL;
532533

533-
nrfcloud_cell_data = cell_data;
534+
/* This is needed when this is called from sm_at_gnss.c */
535+
nrfcloud_sending_loc_req = true;
536+
537+
/* Allocate main cell data structure.
538+
* Neighbor cells structure is only allocated when they are found.
539+
*/
540+
cell_data = calloc(1, sizeof(struct lte_lc_cells_info));
534541
if (cell_data == NULL) {
535-
/* Allocate main cell data structure.
536-
* Neighbor cells structure is only allocated when they are found.
537-
*/
538-
nrfcloud_cell_data = calloc(1, sizeof(struct lte_lc_cells_info));
539-
if (nrfcloud_cell_data == NULL) {
540-
LOG_ERR("Failed to allocate memory for the nRF Cloud cell data");
541-
return;
542-
}
542+
LOG_ERR("Failed to allocate memory for the nRF Cloud cell data");
543+
/* Called as this cleans up resources */
544+
k_work_submit_to_queue(&sm_work_q, &nrfcloud_loc_req_work);
545+
return NULL;
543546
}
544-
nrfcloud_cell_data->current_cell.id = LTE_LC_CELL_EUTRAN_ID_INVALID;
545-
nrfcloud_cell_data->ncells_count = 0;
546-
nrfcloud_cell_data->gci_cells_count = 0;
547+
cell_data->current_cell.id = LTE_LC_CELL_EUTRAN_ID_INVALID;
548+
cell_data->ncells_count = 0;
549+
cell_data->gci_cells_count = 0;
550+
/* Needs to set global variable so that NCELLMEAS parsing can access it */
551+
nrfcloud_cell_data = cell_data;
547552

548553
/* Start backup timeout for 120 seconds to handle missing NCELLMEAS notification. */
549554
k_work_schedule(&ncellmeas_timeout_backup_work, K_SECONDS(NRFCLOUDPOS_TIMEOUT_SEC));
@@ -610,7 +615,7 @@ void sm_at_nrfcloud_ncellmeas(uint8_t cell_count, struct lte_lc_cells_info *cell
610615
LOG_ERR("Failed to allocate memory for the GCI cells");
611616
goto end;
612617
}
613-
nrfcloud_cell_data->gci_cells = cells;
618+
cell_data->gci_cells = cells;
614619

615620
ncellmeas_search_type = 3;
616621
ncellmeas_gci_count = ncellmeas3_cell_count;
@@ -631,7 +636,7 @@ void sm_at_nrfcloud_ncellmeas(uint8_t cell_count, struct lte_lc_cells_info *cell
631636
}
632637

633638
/* If we received already enough GCI cells including current cell */
634-
if (nrfcloud_cell_data->gci_cells_count + 1 >= cell_count) {
639+
if (cell_data->gci_cells_count + 1 >= cell_count) {
635640
goto end;
636641
}
637642

@@ -657,27 +662,22 @@ void sm_at_nrfcloud_ncellmeas(uint8_t cell_count, struct lte_lc_cells_info *cell
657662

658663
end:
659664
at_monitor_pause(&sm_ncellmeas);
660-
if (err == 0) {
665+
if (send_loc_req) {
661666
k_work_submit_to_queue(&sm_work_q, &nrfcloud_loc_req_work);
662667
} else {
663-
if (cell_data == NULL) {
664-
/* If cell_data is not provided, cleanup the cell data in error cases */
665-
nrfcloud_cell_data_cleanup();
666-
}
667-
if (nrfcloud_wifi_pos) {
668-
free(nrfcloud_wifi_data.ap_info);
669-
nrfcloud_wifi_data.ap_info = NULL;
670-
}
671668
nrfcloud_sending_loc_req = false;
669+
/* Clear global variable since the ownership moves to the caller */
670+
nrfcloud_cell_data = NULL;
672671
}
673672
k_work_cancel_delayable(&ncellmeas_timeout_backup_work);
673+
return cell_data;
674674
}
675675

676676
static void sm_at_nrfcloud_ncellmeas_work_fn(struct k_work *work)
677677
{
678678
ARG_UNUSED(work);
679679

680-
sm_at_nrfcloud_ncellmeas(nrfcloud_cell_count, nrfcloud_cell_data);
680+
nrfcloud_cell_data = sm_at_nrfcloud_ncellmeas(nrfcloud_cell_count, true);
681681
}
682682

683683
static void ncellmeas_timeout_backup_work_fn(struct k_work *work)
@@ -698,7 +698,7 @@ static void nrfcloud_loc_req_work_fn(struct k_work *work)
698698
.wifi_info = nrfcloud_wifi_pos ? &nrfcloud_wifi_data : NULL
699699
};
700700

701-
// TODO: Check if we ncellmeas failed and no wifi aps.
701+
// TODO: Check if ncellmeas failed and no wifi aps were found.
702702
//urc_send_to(nrfcloud_pipe, "\r\n#XNRFCLOUDPOS: -1\r\n");
703703

704704
err = nrf_cloud_coap_location_get(&request, &result);
@@ -833,6 +833,10 @@ static int parse_ncellmeas_gci(const char *at_response, struct lte_lc_cells_info
833833
int curr_index;
834834
size_t i = 0, k = 0;
835835

836+
__ASSERT_NO_MSG(at_response != NULL);
837+
__ASSERT_NO_MSG(cells != NULL);
838+
__ASSERT_NO_MSG(cells->gci_cells != NULL);
839+
836840
/* Count the actual number of parameters in the AT response before
837841
* allocating heap for it. This may save quite a bit of heap as the
838842
* worst case scenario is 96 elements.
@@ -841,10 +845,6 @@ static int parse_ncellmeas_gci(const char *at_response, struct lte_lc_cells_info
841845
*/
842846
size_t param_count = get_char_frequency(at_response, ',') + 3;
843847

844-
__ASSERT_NO_MSG(at_response != NULL);
845-
__ASSERT_NO_MSG(cells != NULL);
846-
__ASSERT_NO_MSG(cells->gci_cells != NULL);
847-
848848
/* We don't want to clear old current cell since it's not always returned but
849849
* we want to overwrite old GCI cells.
850850
*/

app/src/sm_at_nrfcloud.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ extern bool sm_nrf_cloud_send_location;
2525
* @brief Execute cellular neighbor cell measurements.
2626
*
2727
* @param[in] cell_count Number of cells to search for.
28-
* @param[in,out] cell_data Cell data structure to be filled.
29-
* NULL means that the function will allocate the memory.
28+
* @param[in] send_log_req Whether to send a location request to cloud after neighbor measurements.
29+
*
30+
* @return Cell search results allocated from the heap and caller must release it with free().
3031
*/
31-
void sm_at_nrfcloud_ncellmeas(uint8_t cell_count, struct lte_lc_cells_info *cell_data);
32+
struct lte_lc_cells_info *sm_at_nrfcloud_ncellmeas(uint8_t cell_count, bool send_loc_req);
3233

3334
/** @} */
3435
#endif /* SM_AT_NRFCLOUD_ */

0 commit comments

Comments
 (0)