Skip to content
Snippets Groups Projects
common.c 5.50 KiB
#include "common.h"

int print_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req)
{
	struct blob_buf bb;

	memset(&bb, 0, sizeof(bb));
	blob_buf_init(&bb, 0);
	bb = json_to_blob(parsed_response, bb);
	ubus_send_reply(ctx, req, bb.head);
	blob_buf_free(&bb);

	json_object_put(parsed_response);
	return 0;
}

struct blob_buf json_to_blob(struct json_object *response, struct blob_buf bb)
{
	void *arr, *obj;
	int i;

	json_object_object_foreach(response, key, val) {
		int val_type = json_object_get_type(val);

		switch (val_type) {
		case json_type_int:
			blobmsg_add_u32(&bb, key, json_object_get_int(val));
			break;
		case json_type_double:
			blobmsg_add_double(&bb, key, json_object_get_double(val));
			break;
		case json_type_string:
			blobmsg_add_string(&bb, key, json_object_get_string(val));
			break;
		case json_type_array:
			arr = blobmsg_open_array(&bb, key);
			for (i = 0; i < json_object_array_length(val); i++) {
				// add open table
				bb = json_to_blob(json_object_array_get_idx(val, i), bb);
				// add close table
			}
			blobmsg_close_array(&bb, arr);
			break;
		case json_type_object:
			obj = blobmsg_open_table(&bb, key);
			bb = json_to_blob(val, bb);
			blobmsg_close_table(&bb, obj);
			break;
		}
	}

	return bb;
}

//need to free tag content..
char *xml_parser(struct write_result *result, char *tag)
{
	char *tag_content;

	tag_content = malloc(TAG_CONTENT_SIZE);
	if (!tag_content)
		goto leave;

	xmlDocPtr doc_ptr = NULL;
	xmlXPathContextPtr xpathctx_ptr;
	xmlXPathObjectPtr xpathobj_ptr;

	doc_ptr = xmlParseMemory(result->data, result->pos);
	xpathctx_ptr = doc_ptr ? xmlXPathNewContext(doc_ptr) : NULL;
	xpathobj_ptr = xpathctx_ptr ? xmlXPathEvalExpression((xmlChar *)tag, xpathctx_ptr) : NULL;
	if (xpathobj_ptr) {
		int n;

		xmlNodeSetPtr nodeset_ptr = xpathobj_ptr->nodesetval;
		for (n = 0; nodeset_ptr && n < xmlXPathNodeSetGetLength(nodeset_ptr); n++) {
			//if (DEBUG)
		//debug_print("n value: %d\n", n);
			xmlNodePtr node_ptr = nodeset_ptr->nodeTab[n];

			if (node_ptr->type != XML_ATTRIBUTE_NODE && node_ptr->type != XML_ELEMENT_NODE && node_ptr->type != XML_CDATA_SECTION_NODE)
				continue;
			if (node_ptr->children) {
				//tag_content = (char*) node_ptr->children->content;
				strcpy(tag_content, (char *)node_ptr->children->content);
				debug_print("tag content: %s\n", tag_content);
				break;
			}
		}
	}

	if (xpathobj_ptr)
		xmlXPathFreeObject(xpathobj_ptr);
	if (xpathctx_ptr)
		xmlXPathFreeContext(xpathctx_ptr);
	if (doc_ptr)
		xmlFreeDoc(doc_ptr);

	return tag_content;

leave:
	return NULL;
}

void xml_to_json(xmlNode *anode, json_object *jobj)
{
	xmlNodePtr cur_node = NULL;
	json_object *cur_jstr = NULL;

	for (cur_node = anode; cur_node; cur_node = cur_node->next) {
		debug_print("child address: %p\n", cur_node);
		debug_print("next addres: %p\n", cur_node->next);
		debug_print("root child content: %s\n", (uint8_t *)xmlNodeGetContent(cur_node));

		if (cur_node->type == XML_ELEMENT_NODE) {
			if (xmlChildElementCount(cur_node) == 0) {
				cur_jstr = json_object_new_string((char *)xmlNodeGetContent(cur_node));

				debug_print("xmlNodeGet name: %sn", (char *)cur_node->name);
				debug_print("xmlNodeGetContent: %sn", (char *)xmlNodeGetContent(cur_node));

				json_object_object_add(jobj, (char *)cur_node->name, cur_jstr);
			}
		}

		xml_to_json(cur_node->children, jobj);
	}
}

void xml_to_json_converter(struct write_result *result)
{
	json_object *jobj;
	xmlDocPtr doc_ptr = NULL;
	xmlNode *origin_node = NULL;

	jobj = json_object_new_object();

	doc_ptr = xmlParseMemory(result->data, result->pos);
	origin_node = doc_ptr ? xmlDocGetRootElement(doc_ptr) : NULL;
	jobj = json_object_new_object();
	xml_to_json(origin_node, jobj);

	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, char *ip_addr)
{
	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)
{
	struct json_object *rv_json;

	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;
}