Skip to content

Commit 49cc214

Browse files
bjdacvinayak
authored andcommitted
drivers: nrf9160_gps: Improve enable_gps function
1. Renamed at_response_list at_resp_list to restrict line lengths. 2. Moved list initialization to start of function. 3. Reduce return paths to one during and one after list initialization. Signed-off-by: Bernt Johan Damslora <[email protected]>
1 parent 53a0385 commit 49cc214

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

drivers/nrf9160_gps/nrf9160_gps.c

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -220,32 +220,37 @@ static int enable_gps(struct device *dev)
220220
{
221221
int err;
222222
char buf[50] = {0};
223-
struct at_param_list at_response_list = {0};
223+
struct at_param_list at_resp_list = {0};
224224
u16_t gps_param_value, functional_mode;
225225

226+
err = at_params_list_init(&at_resp_list, AT_XSYSTEMMODE_PARAMS_COUNT);
227+
if (err) {
228+
LOG_ERR("Could init AT params list, error: %d", err);
229+
return err; /* No need to clean up; the list was never init'd */
230+
}
231+
226232
err = at_cmd_write(AT_XSYSTEMMODE_REQUEST, buf, sizeof(buf), NULL);
227233
if (err) {
228234
LOG_ERR("Could not get modem's system mode");
229-
return -EIO;
235+
err = -EIO;
236+
goto enable_gps_clean_exit;
230237
}
231238

232-
at_params_list_init(&at_response_list, AT_XSYSTEMMODE_PARAMS_COUNT);
233-
234239
err = at_parser_max_params_from_str(
235240
&buf[sizeof(AT_XSYSTEMMODE_RESPONSE)],
236-
&at_response_list,
241+
&at_resp_list,
237242
AT_XSYSTEMMODE_PARAMS_COUNT);
238243
if (err) {
239244
LOG_ERR("Could not parse AT response, error: %d", err);
240-
return err;
245+
goto enable_gps_clean_exit;
241246
}
242247

243-
err = at_params_short_get(&at_response_list,
248+
err = at_params_short_get(&at_resp_list,
244249
AT_XSYSTEMMODE_GPS_PARAM_INDEX,
245250
&gps_param_value);
246251
if (err) {
247252
LOG_ERR("Could not get GPS mode state, error: %d", err);
248-
return err;
253+
goto enable_gps_clean_exit;
249254
}
250255

251256
if (gps_param_value != 1) {
@@ -256,7 +261,7 @@ static int enable_gps(struct device *dev)
256261
LOG_DBG("GPS mode is not enabled, attempting to enable it");
257262

258263
for (size_t i = 0; i < AT_XSYSTEMMODE_PARAMS_COUNT; i++) {
259-
at_params_short_get(&at_response_list, i, &values[i]);
264+
at_params_short_get(&at_resp_list, i, &values[i]);
260265
}
261266

262267
values[AT_XSYSTEMMODE_GPS_PARAM_INDEX] = 1;
@@ -269,7 +274,7 @@ static int enable_gps(struct device *dev)
269274
err = at_cmd_write(cmd, NULL, 0, NULL);
270275
if (err) {
271276
LOG_ERR("Could not enable GPS mode, error: %d", err);
272-
return err;
277+
goto enable_gps_clean_exit;
273278
}
274279
}
275280

@@ -278,23 +283,23 @@ static int enable_gps(struct device *dev)
278283
err = at_cmd_write(AT_CFUN_REQUEST, buf, sizeof(buf), NULL);
279284
if (err) {
280285
LOG_ERR("Could not get functional mode, error: %d", err);
281-
return err;
286+
goto enable_gps_clean_exit;
282287
}
283288

284289
err = at_parser_max_params_from_str(
285290
&buf[sizeof(AT_CFUN_RESPONSE)],
286-
&at_response_list, 1);
291+
&at_resp_list, 1);
287292
if (err) {
288293
LOG_ERR("Could not parse functional mode response, error: %d",
289294
err);
290-
return err;
295+
goto enable_gps_clean_exit;
291296
}
292297

293-
err = at_params_short_get(&at_response_list, 0, &functional_mode);
298+
err = at_params_short_get(&at_resp_list, 0, &functional_mode);
294299
if (err) {
295300
LOG_ERR("Could not get value of functional mode, error: %d",
296301
err);
297-
return err;
302+
goto enable_gps_clean_exit;
298303
}
299304

300305
LOG_DBG("Functional mode: %d", functional_mode);
@@ -307,14 +312,14 @@ static int enable_gps(struct device *dev)
307312
if (err) {
308313
LOG_ERR("Could not set functional mode to %d",
309314
FUNCTIONAL_MODE_ENABLED);
310-
return err;
315+
goto enable_gps_clean_exit;
311316
}
312317
LOG_DBG("Functional mode set to %d", FUNCTIONAL_MODE_ENABLED);
313318
}
314319

315-
at_params_list_free(&at_response_list);
316-
317-
return 0;
320+
enable_gps_clean_exit:
321+
at_params_list_free(&at_resp_list);
322+
return err;
318323
}
319324

320325
static int start(struct device *dev)

0 commit comments

Comments
 (0)