@@ -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
658663end :
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
676676static 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
683683static 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 */
0 commit comments