@@ -29,8 +29,13 @@ static char nrfcloud_device_id[NRF_CLOUD_CLIENT_ID_MAX_LEN];
2929bool sm_nrf_cloud_ready ;
3030bool sm_nrf_cloud_send_location ;
3131
32+ static void nrfcloud_conn_work_fn (struct k_work * work );
33+ K_WORK_DEFINE (nrfcloud_conn_work , nrfcloud_conn_work_fn );
34+
3235#if defined(CONFIG_SM_NRF_CLOUD_LOCATION )
3336
37+ #define NRFCLOUDPOS_TIMEOUT_SEC 120
38+
3439/* NCELLMEAS notification parameters */
3540#define AT_NCELLMEAS_STATUS_INDEX 1
3641#define AT_NCELLMEAS_CELL_ID_INDEX 2
@@ -59,15 +64,13 @@ static bool nrfcloud_wifi_pos;
5964/* Whether a location request is currently being sent to nRF Cloud. */
6065static bool nrfcloud_sending_loc_req ;
6166
62- static uint8_t search_type ;
63- static uint8_t gci_count ;
67+ static uint8_t ncellmeas_search_type ;
68+ static uint8_t ncellmeas_gci_count ;
6469
65- static void nrfcloud_conn_work_fn (struct k_work * work );
6670static void nrfcloud_loc_req_work_fn (struct k_work * work );
6771static void sm_at_nrfcloud_ncellmeas_work_fn (struct k_work * work );
6872static void ncellmeas_timeout_backup_work_fn (struct k_work * work );
6973
70- K_WORK_DEFINE (nrfcloud_conn_work , nrfcloud_conn_work_fn );
7174K_WORK_DEFINE (nrfcloud_loc_req_work , nrfcloud_loc_req_work_fn );
7275K_WORK_DEFINE (sm_at_nrfcloud_ncellmeas_work , sm_at_nrfcloud_ncellmeas_work_fn );
7376K_WORK_DELAYABLE_DEFINE (ncellmeas_timeout_backup_work , ncellmeas_timeout_backup_work_fn );
@@ -265,15 +268,20 @@ static void sm_at_nrfcloud_init(int ret, void *ctx)
265268 if (initialized ) {
266269 return ;
267270 }
268- initialized = true;
269271
270272 err = nrf_cloud_coap_init ();
271273 if (err ) {
272274 LOG_ERR ("Failed to initialize nRF Cloud CoAP library: %d" , err );
273275 return ;
274276 }
275277
276- nrf_cloud_client_id_get (nrfcloud_device_id , sizeof (nrfcloud_device_id ));
278+ err = nrf_cloud_client_id_get (nrfcloud_device_id , sizeof (nrfcloud_device_id ));
279+ if (err ) {
280+ LOG_ERR ("Failed to get nRF Cloud client ID: %d" , err );
281+ return ;
282+ }
283+
284+ initialized = true;
277285}
278286NRF_MODEM_LIB_ON_INIT (sm_nrfcloud_init_hook , sm_at_nrfcloud_init , NULL );
279287
@@ -538,7 +546,7 @@ void sm_at_nrfcloud_ncellmeas(uint8_t cell_count, struct lte_lc_cells_info *cell
538546 nrfcloud_cell_data -> gci_cells_count = 0 ;
539547
540548 /* Start backup timeout for 120 seconds to handle missing NCELLMEAS notification. */
541- k_work_schedule (& ncellmeas_timeout_backup_work , K_SECONDS (120 ));
549+ k_work_schedule (& ncellmeas_timeout_backup_work , K_SECONDS (NRFCLOUDPOS_TIMEOUT_SEC ));
542550 at_monitor_resume (& sm_ncellmeas );
543551
544552 LOG_DBG ("Triggering cell measurements cell_count=%d" , cell_count );
@@ -548,13 +556,13 @@ void sm_at_nrfcloud_ncellmeas(uint8_t cell_count, struct lte_lc_cells_info *cell
548556 * In addition neighbor cells are received.
549557 */
550558 LOG_DBG ("Normal neighbor search (NCELLMEAS=1)" );
551- search_type = 1 ;
559+ ncellmeas_search_type = 1 ;
552560 err = sm_util_at_printf ("AT%%NCELLMEAS=1" );
553561 if (err ) {
554562 LOG_ERR ("Failed to initiate neighbor cell measurements: %d" , err );
555563 goto end ;
556564 }
557- err = k_sem_take (& ncellmeas_sem_ncellmeas_evt , K_FOREVER );
565+ err = k_sem_take (& ncellmeas_sem_ncellmeas_evt , K_SECONDS ( NRFCLOUDPOS_TIMEOUT_SEC ) );
558566 if (err ) {
559567 /* Semaphore was reset so stop search procedure */
560568 err = 0 ;
@@ -588,7 +596,6 @@ void sm_at_nrfcloud_ncellmeas(uint8_t cell_count, struct lte_lc_cells_info *cell
588596 }
589597 }
590598
591-
592599 /*****
593600 * 2nd: GCI history search to get GCI cells we can quickly search and measure.
594601 * Because history search is quick and very power efficient, we request
@@ -605,8 +612,8 @@ void sm_at_nrfcloud_ncellmeas(uint8_t cell_count, struct lte_lc_cells_info *cell
605612 }
606613 nrfcloud_cell_data -> gci_cells = cells ;
607614
608- search_type = 3 ;
609- gci_count = ncellmeas3_cell_count ;
615+ ncellmeas_search_type = 3 ;
616+ ncellmeas_gci_count = ncellmeas3_cell_count ;
610617 err = sm_util_at_printf ("AT%%NCELLMEAS=3,%d" , ncellmeas3_cell_count );
611618 if (err ) {
612619 LOG_WRN ("Failed to initiate GCI cell measurements: %d" , err );
@@ -616,7 +623,7 @@ void sm_at_nrfcloud_ncellmeas(uint8_t cell_count, struct lte_lc_cells_info *cell
616623 err = 0 ;
617624 goto end ;
618625 }
619- err = k_sem_take (& ncellmeas_sem_ncellmeas_evt , K_FOREVER );
626+ err = k_sem_take (& ncellmeas_sem_ncellmeas_evt , K_SECONDS ( NRFCLOUDPOS_TIMEOUT_SEC ) );
620627 if (err ) {
621628 /* Semaphore was reset so stop search procedure */
622629 err = 0 ;
@@ -635,8 +642,8 @@ void sm_at_nrfcloud_ncellmeas(uint8_t cell_count, struct lte_lc_cells_info *cell
635642 */
636643 LOG_DBG ("GCI regional search (NCELLMEAS=4,%d)" , cell_count );
637644
638- search_type = 4 ;
639- gci_count = cell_count ;
645+ ncellmeas_search_type = 4 ;
646+ ncellmeas_gci_count = cell_count ;
640647 err = sm_util_at_printf ("AT%%NCELLMEAS=4,%d" , cell_count );
641648 if (err ) {
642649 LOG_WRN ("Failed to initiate GCI cell measurements: %d" , err );
@@ -646,7 +653,7 @@ void sm_at_nrfcloud_ncellmeas(uint8_t cell_count, struct lte_lc_cells_info *cell
646653 err = 0 ;
647654 goto end ;
648655 }
649- k_sem_take (& ncellmeas_sem_ncellmeas_evt , K_FOREVER );
656+ k_sem_take (& ncellmeas_sem_ncellmeas_evt , K_SECONDS ( NRFCLOUDPOS_TIMEOUT_SEC ) );
650657
651658end :
652659 at_monitor_pause (& sm_ncellmeas );
@@ -688,7 +695,11 @@ static void nrfcloud_loc_req_work_fn(struct k_work *work)
688695 const struct nrf_cloud_coap_location_request request = {
689696 .config = NULL ,
690697 .cell_info = nrfcloud_cell_count ? nrfcloud_cell_data : NULL ,
691- .wifi_info = nrfcloud_wifi_pos ? & nrfcloud_wifi_data : NULL };
698+ .wifi_info = nrfcloud_wifi_pos ? & nrfcloud_wifi_data : NULL
699+ };
700+
701+ // TODO: Check if we ncellmeas failed and no wifi aps.
702+ //urc_send_to(nrfcloud_pipe, "\r\n#XNRFCLOUDPOS: -1\r\n");
692703
693704 err = nrf_cloud_coap_location_get (& request , & result );
694705 if (err == 0 ) {
@@ -883,7 +894,7 @@ static int parse_ncellmeas_gci(const char *at_response, struct lte_lc_cells_info
883894
884895 /* Go through the cells */
885896 for (i = 0 ; curr_index < (param_count - (AT_NCELLMEAS_GCI_CELL_PARAMS_COUNT + 1 )) &&
886- i < gci_count ;
897+ i < ncellmeas_gci_count ;
887898 i ++ ) {
888899 struct lte_lc_cell parsed_cell ;
889900 bool is_serving_cell ;
@@ -1021,7 +1032,7 @@ static int parse_ncellmeas_gci(const char *at_response, struct lte_lc_cells_info
10211032 if (is_serving_cell ) {
10221033 /* This the current/serving cell.
10231034 * In practice the <neighbor_count> is always 0 for other than
1024- * the serving cell, i.e. no neigbour cell list is available.
1035+ * the serving cell, i.e. no neigbor cell list is available.
10251036 * Thus, handle neighbor cells only for the serving cell.
10261037 */
10271038 cells -> current_cell = parsed_cell ;
@@ -1213,7 +1224,7 @@ static void at_handler_ncellmeas(const char *response)
12131224 return ;
12141225 }
12151226
1216- if (search_type > 2 ) {
1227+ if (ncellmeas_search_type > 2 ) {
12171228 err = parse_ncellmeas_gci (response , nrfcloud_cell_data );
12181229 } else {
12191230 err = parse_ncellmeas (response , nrfcloud_cell_data );
@@ -1231,4 +1242,5 @@ static void at_handler_ncellmeas(const char *response)
12311242
12321243 k_sem_give (& ncellmeas_sem_ncellmeas_evt );
12331244}
1245+
12341246#endif /* CONFIG_SM_NRF_CLOUD_LOCATION */
0 commit comments