From c05283f9dc7df8174d8fdd79c40aa229ca5c5fd8 Mon Sep 17 00:00:00 2001
From: Jakob Olsson <jakobols@kth.se>
Date: Tue, 5 Jun 2018 11:45:32 +0200
Subject: [PATCH] move logic from dongle_infrastructure to common

---
 common.c                |  89 ++++++++++++++++++
 common.h                |   5 +
 dongle_infrastructure.c | 202 +++++++++-------------------------------
 3 files changed, 140 insertions(+), 156 deletions(-)

diff --git a/common.c b/common.c
index 8e31a23..7e429dd 100644
--- a/common.c
+++ b/common.c
@@ -147,3 +147,92 @@ void xml_to_json_converter(struct write_result *result)
 
 	debug_print("The json object created: %s\n", json_object_to_json_string(jobj));
 }
+
+int isdigits(const char *pin)
+{
+	while (*pin) {
+		if (isdigit(*pin++) == 0)
+			return false;
+	}
+	return true;
+}
+
+int validate_puk_format(char *puk)
+{
+	if (!isdigits(puk)) {
+		debug_print("Please enter digits only!\n");
+		goto fail;
+	} else if (strlen(puk) == 8) {
+		debug_print("Please enter 8 digits!\n");
+		goto fail;
+	}
+
+	return 0;
+fail:
+	return -1;
+}
+
+int validate_pin_format(char *pin)
+{
+	if (!isdigits(pin)) {
+		debug_print("Please enter digits only!\n");
+		goto fail;
+	} else if (strlen(pin) > 8 || strlen(pin) < 4) {
+		debug_print("Please enter between 4 to 8 digits!\n");
+		goto fail;
+	} else if (atoi(pin) == 0) {
+		debug_print("0000 is not a valid pin! Lowest available is 0001\n");
+		goto fail;
+	}
+
+	return 0;
+fail:
+	return -1;
+}
+
+int pin_status(struct blob_buf *bb)
+{
+	struct json_object *response, *rv_json;
+	int rv;
+
+	response = mobile_get_pin_status(ip_addr);
+	if (!response) {
+		debug_print("no response from get_pin_status!\n");
+		goto fail_response;
+	}
+
+	json_object_object_get_ex(response, "pin_status", &rv_json);
+	if (!rv_json) {
+		debug_print("no pin_status available in response!\n");
+		goto fail_result;
+	}
+
+	rv = json_object_get_int(rv_json);
+	if (rv == 0)
+		blobmsg_add_string(bb, "Failure", "Pin disabled");
+	else
+		blobmsg_add_string(bb, "Failure", "Pin enabled");
+
+	json_object_put(response);
+	return rv;
+fail_response:
+fail_result:
+	json_object_put(response);
+	return -1;
+}
+
+int check_response(struct json_object *response)
+{
+	json_object_object_get_ex(response, "result", &rv_json);
+	if (!rv_json) {
+		debug_print("no result available in response!\n");
+		goto fail;
+	}
+
+	if (strncmp(json_object_get_string(rv_json), "failure", strlen("failure")) == 0)
+		goto fail;
+
+	return 0;
+fail:
+	return -1;
+}
\ No newline at end of file
diff --git a/common.h b/common.h
index 9d447c7..56e4391 100644
--- a/common.h
+++ b/common.h
@@ -77,4 +77,9 @@ struct blob_buf json_to_blob(struct json_object *response, struct blob_buf bb);
 
 char *xml_parser(struct write_result *result, char *tag);
 void xml_to_json_converter(struct write_result *result);
+int isdigits(const char *pin);
+int validate_puk_format(char *puk);
+int validate_pin_format(char *pin);
+int pin_status(struct blob_buf *bb);
+int check_response(struct json_object *response);
 #endif
diff --git a/dongle_infrastructure.c b/dongle_infrastructure.c
index deb7e9c..c715a77 100644
--- a/dongle_infrastructure.c
+++ b/dongle_infrastructure.c
@@ -44,60 +44,15 @@ const struct blobmsg_policy create_apn_policy[__CREATE_MAX] = {
 	[PDP_TYPE] = {.name = "pdp_type", .type = BLOBMSG_TYPE_STRING}
 };
 
-
 //dynamic object callbacks..
 
 //dongle pin..
-static int isdigits(const char *pin);
-static int validate_pin_format(char *pin);
-
-static int isdigits(const char *pin)
-{
-	while (*pin) {
-		if (isdigit(*pin++) == 0)
-			return false;
-	}
-	return true;
-}
-
-static int validate_puk_format(char *puk)
-{
-	if (!isdigits(puk)) {
-		debug_print("Please enter digits only!\n");
-		goto fail;
-	} else if (strlen(puk) == 8) {
-		debug_print("Please enter 8 digits!\n");
-		goto fail;
-	}
-
-	return 0;
-fail:
-	return -1;
-}
-
-static int validate_pin_format(char *pin)
-{
-	if (!isdigits(pin)) {
-		debug_print("Please enter digits only!\n");
-		goto fail;
-	} else if (strlen(pin) > 8 || strlen(pin) < 4) {
-		debug_print("Please enter between 4 to 8 digits!\n");
-		goto fail;
-	} else if (atoi(pin) == 0) {
-		debug_print("0000 is not a valid pin! Lowest available is 0001\n");
-		goto fail;
-	}
-
-	return 0;
-fail:
-	return -1;
-}
-
 int set_pin(struct ubus_context *ctx, struct ubus_object *obj,
 				 struct ubus_request_data *req, const char *method,
 				 struct blob_attr *msg)
 {
 	struct blob_attr *tb[__SET_PIN_MAX];
+	struct blob_buf bb;
 	char *new_pin, *current_pin, *ip_addr;
 	int rv;
 	struct json_object *response, *rv_json;
@@ -127,70 +82,39 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj,
 		goto fail_input;
 	}
 
-	response = mobile_get_pin_status(ip_addr);
-	if (!response) {
-		debug_print("error getting pin_status\n");
-		goto fail_data;
-	}
-
-	json_object_object_get_ex(response, "pin_status", &rv_json);
-	if (!rv_json) {
-		debug_print("no pin_status available\n");
-		goto fail_result;
-	}
-
-	/* Enable PIN if it is disabled */
-	if (!json_object_get_int(rv_json)) {
-		json_object_put(response);
-
-		response = mobile_enable_pin(ip_addr, current_pin);
-		if (!response) {
-			debug_print("error enabling pin!\n");
-			goto fail_unknown;
-		}
-
-		json_object_object_get_ex(response, "result", &rv_json);
-		if (!rv_json) {
-			debug_print("error getting result!\n");
-			goto fail_result;
-		}
-
-		if (strncmp(json_object_get_string(rv_json), "failure", strlen("failure")) == 0) {
-			debug_print("Incorrect pin!\n");
-			goto incorrect_pin;
-		}
+	if (!pin_status(&bb)) {
+		debug_print("pin disabled!");
+		ubus_send_reply(ctx, req, bb.head);
+		goto disabled;
 	}
 
-	json_object_put(response);
 	response = mobile_set_pin(ip_addr, current_pin, new_pin);
 	if (!response) {
 		debug_print("error setting pin!\n");
 		goto fail_data;
 	}
 
-	json_object_object_get_ex(response, "result", &rv_json);
-	if (!rv_json) {
-		debug_print("no result available from %s!", __func__);
-		goto fail_result;
+	rv = check_response(response);
+	if (rv < 0) {
+		debug_print("incorrect pin!\n");
+		goto fail_input_json;
 	}
 
-incorrect_pin:
 	free(ip_addr);
 	return print_to_ubus(response, ctx, req);
+fail_input_json:
+	json_object_put(response);
 fail_input:
 	free(ip_addr);
 	return UBUS_STATUS_INVALID_ARGUMENT;
 fail_data:
 	free(ip_addr);
 	return UBUS_STATUS_NO_DATA;
-fail_result:
-	free(ip_addr);
-	json_object_put(response);
-	return UBUS_STATUS_UNKNOWN_ERROR;
-fail_unknown:
-	free(ip_addr);
 fail_strdup:
 	return UBUS_STATUS_UNKNOWN_ERROR;
+disabled:
+	free(ip_addr);
+	return 0;
 }
 
 int disable_pin(struct ubus_context *ctx, struct ubus_object *obj,
@@ -219,52 +143,36 @@ int disable_pin(struct ubus_context *ctx, struct ubus_object *obj,
 		goto fail_input;
 	}
 
-	response = mobile_get_pin_status(ip_addr);
-	if (!response) {
-		debug_print("no response from get_pin_status!\n");
-		goto fail_data;
-	}
-
-	json_object_object_get_ex(response, "pin_status", &rv_json);
-	if (!rv_json) {
-		debug_print("no pin_status available in response!\n");
-		goto fail_result;
+	if (!pin_status()) {
+		debug_print("pin already disabled!\n");
+		goto disabled:
 	}
 
-	if (!json_object_get_int(rv_json)) {
-		debug_print("already disabled!\n");
-		goto success; //kind of...
-	}
-
-	json_object_put(response);
 	response = mobile_disable_pin(ip_addr, pin);
 	if (!response) {
 		debug_print("error disabling pin!\n");
 		goto fail_data;
 	}
 
-	json_object_object_get_ex(response, "result", &rv_json);
-	if (!rv_json) {
-		debug_print("no result available in response!\n");
-		goto fail_result;
+	rv = check_response(response);
+	if (rv < 0) {
+		deub_print("incorrect pin!\n");
+		goto fail_input_response;
 	}
 
-	if (strncmp(json_object_get_string(rv_json), "failure", strlen("failure")) == 0)
-		debug_print("Incorrect pin!\n");
-
-success:
+disabled:
 	free(ip_addr);
-	return print_to_ubus(response, ctx, req);
+	return 0;
+fail_input_response:
+	json_object_put(response);
 fail_input:
 	free(ip_addr);
 	return UBUS_STATUS_INVALID_ARGUMENT;
 fail_data:
 	free(ip_addr);
 	return UBUS_STATUS_NO_DATA;
-fail_result:
-	free(ip_addr);
-	json_object_put(response);
 fail_strdup:
+	free(ip_addr);
 	return UBUS_STATUS_UNKNOWN_ERROR;
 }
 
@@ -295,50 +203,34 @@ int enable_pin(struct ubus_context *ctx, struct ubus_object *obj,
 		goto fail_input;
 	}
 
-	response = mobile_get_pin_status(ip_addr);
-	if (!response) {
-		debug_print("no response from get_pin_status!\n");
-		goto fail_data;
-	}
-
-	json_object_object_get_ex(response, "pin_status", &rv_json);
-	if (!rv_json) {
-		debug_print("no pin_status available in response!\n");
-		goto fail_result;
-	}
-	if (!json_object_get_int(rv_json)) {
-		debug_print("already enabled!\n");
-		goto success;
+	if (pin_status()) {
+		debug_print("pin already enabled");
+		goto enabled;
 	}
 
-	json_object_put(response);
 	response = mobile_enable_pin(ip_addr, pin);
 	if (!response) {
 		debug_print("no response from get_pin_status!\n");
 		goto fail_data;
 	}
 
-	json_object_object_get_ex(response, "result", &rv_json);
-	if (!rv_json) {
-		debug_print("no result available in response!\n");
-		goto fail_result;
+	rv = check_response(response);
+	if (rv < 0) {
+		deub_print("incorrect pin!\n");
+		goto fail_input;
 	}
 
-	if (strncmp(json_object_get_string(rv_json), "failure", strlen("failure")) == 0)
-		debug_print("Incorrect pin!\n");
-
-success:
+enabled:
 	free(ip_addr);
-	return print_to_ubus(response, ctx, req);
+	return 0;
+fail_input_response:
+	json_object_put(response);
 fail_input:
 	free(ip_addr);
 	return UBUS_STATUS_INVALID_ARGUMENT;
 fail_data:
 	free(ip_addr);
 	return UBUS_STATUS_NO_DATA;
-fail_result:
-	free(ip_addr);
-	json_object_put(response);
 fail_strdup:
 	return UBUS_STATUS_UNKNOWN_ERROR;
 }
@@ -348,6 +240,7 @@ int verify_pin(struct ubus_context *ctx, struct ubus_object *obj,
 			   struct blob_attr *msg)
 {
 	struct blob_attr *tb[__PIN_MAX];
+	struct blob_buf bb;
 	char *pin, *ip_addr;
 	int rv;
 	struct json_object *response;
@@ -370,6 +263,12 @@ int verify_pin(struct ubus_context *ctx, struct ubus_object *obj,
 		goto fail_input;
 	}
 
+	if (!pin_status(&bb)) {
+		debug_print("pin disabled");
+		ubus_send_reply(ctx, req, bb.head);
+		goto disabled;
+	}
+
 	response = mobile_set_pin(ip_addr, pin, pin);
 	if (!response)
 		goto fail_unknown;
@@ -383,6 +282,8 @@ fail_unknown:
 	free(ip_addr);
 fail_strdup:
 	return UBUS_STATUS_UNKNOWN_ERROR;
+disabled:
+	return 0;
 }
 
 int remaining_tries(struct ubus_context *ctx, struct ubus_object *obj,
@@ -420,14 +321,6 @@ int unlock_sim(struct ubus_context *ctx, struct ubus_object *obj,
 	ip_addr = strdup("192.168.0.1");
 	if (!ip_addr)
 		goto fail_strdup;
-	/*response = mobile_get_remaining_tries(ip_addr);
-	if (!response)
-		goto fail_unknown;
-*/
-	/*json_object_object_get_ex(response, "pinnumber", &rv_json);
-	if (json_object_get_int(rv_json) > 0)
-		goto fail_unknown;
-*/
 
 	blobmsg_parse(pin_policy, __PIN_MAX, tb, blob_data(msg), blob_len(msg));
 
@@ -461,7 +354,6 @@ fail_strdup:
 	return UBUS_STATUS_UNKNOWN_ERROR;
 }
 
-
 int list_apn_profiles(struct ubus_context *ctx, struct ubus_object *obj,
 			   struct ubus_request_data *req, const char *method,
 			   struct blob_attr *msg)
@@ -805,8 +697,6 @@ fail_strdup:
 	return UBUS_STATUS_UNKNOWN_ERROR;
 }
 
-
-
 struct ubus_method dynamic_object_methods[] = {
 	UBUS_METHOD("set_pin", set_pin, set_pin_policy), //pin
 	UBUS_METHOD("disable_pin", disable_pin, pin_policy),
-- 
GitLab