/*
 * Copyright (c) 2006 Jakub Vana
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/** @addtogroup generic
 * @{
 */
/** @file
 */

#include <sysinfo/sysinfo.h>
#include <mm/slab.h>
#include <print.h>
#include <syscall/copy.h>
#include <synch/mutex.h>
#include <arch/asm.h>
#include <errno.h>

/** Maximal sysinfo path length */
#define SYSINFO_MAX_PATH  2048

bool fb_exported = false;

/** Global sysinfo tree root item */
static sysinfo_item_t *global_root = NULL;

/** Sysinfo SLAB cache */
static slab_cache_t *sysinfo_item_slab;

/** Sysinfo lock */
static mutex_t sysinfo_lock;

/** Sysinfo item constructor
 *
 */
static int sysinfo_item_constructor(void *obj, int kmflag)
{
	sysinfo_item_t *item = (sysinfo_item_t *) obj;
	
	item->name = NULL;
	item->val_type = SYSINFO_VAL_UNDEFINED;
	item->subtree_type = SYSINFO_SUBTREE_NONE;
	item->subtree.table = NULL;
	item->next = NULL;
	
	return 0;
}

/** Sysinfo item destructor
 *
 * Note that the return value is not perfectly correct
 * since more space might get actually freed thanks
 * to the disposal of item->name
 *
 */
static int sysinfo_item_destructor(void *obj)
{
	sysinfo_item_t *item = (sysinfo_item_t *) obj;
	
	if (item->name != NULL)
		free(item->name);
	
	return 0;
}

/** Initialize sysinfo subsystem
 *
 * Create SLAB cache for sysinfo items.
 *
 */
void sysinfo_init(void)
{
	sysinfo_item_slab = slab_cache_create("sysinfo_item_slab",
	    sizeof(sysinfo_item_t), 0, sysinfo_item_constructor,
	    sysinfo_item_destructor, SLAB_CACHE_MAGDEFERRED);

	mutex_initialize(&sysinfo_lock, MUTEX_ACTIVE);
}

/** Recursively find an item in sysinfo tree
 *
 * Should be called with sysinfo_lock held.
 *
 * @param name    Current sysinfo path suffix.
 * @param subtree Current sysinfo (sub)tree root item.
 * @param ret     If the return value is NULL, this argument
 *                can be either also NULL (i.e. no item was
 *                found and no data was generated) or the
 *                original pointer is used to store the value
 *                generated by a generated subtree function.
 * @param dry_run Do not actually get any generated
 *                binary data, just calculate the size.
 *
 * @return Found item or NULL if no item in the fixed tree
 *         was found (N.B. ret).
 *
 */
static sysinfo_item_t *sysinfo_find_item(const char *name,
    sysinfo_item_t *subtree, sysinfo_return_t **ret, bool dry_run)
{
	ASSERT(subtree != NULL);
	ASSERT(ret != NULL);
	
	sysinfo_item_t *cur = subtree;
	
	/* Walk all siblings */
	while (cur != NULL) {
		size_t i = 0;
		
		/* Compare name with path */
		while ((cur->name[i] != 0) && (name[i] == cur->name[i]))
			i++;
		
		/* Check for perfect name and path match */
		if ((name[i] == 0) && (cur->name[i] == 0))
			return cur;
		
		/* Partial match up to the delimiter */
		if ((name[i] == '.') && (cur->name[i] == 0)) {
			/* Look into the subtree */
			switch (cur->subtree_type) {
			case SYSINFO_SUBTREE_TABLE:
				/* Recursively find in subtree */
				return sysinfo_find_item(name + i + 1,
				    cur->subtree.table, ret, dry_run);
			case SYSINFO_SUBTREE_FUNCTION:
				/* Get generated data */
				**ret = cur->subtree.get_data(name + i + 1, dry_run);
				return NULL;
			default:
				/* Not found, no data generated */
				*ret = NULL;
				return NULL;
			}
		}
		
		cur = cur->next;
	}
	
	/* Not found, no data generated */
	*ret = NULL;
	return NULL;
}

/** Recursively create items in sysinfo tree
 *
 * Should be called with sysinfo_lock held.
 *
 * @param name     Current sysinfo path suffix.
 * @param psubtree Pointer to an already existing (sub)tree root
 *                 item or where to store a new tree root item.
 *
 * @return Existing or newly allocated sysinfo item or NULL
 *         if the current tree configuration does not allow to
 *         create a new item.
 *
 */
static sysinfo_item_t *sysinfo_create_path(const char *name,
    sysinfo_item_t **psubtree)
{
	ASSERT(psubtree != NULL);
	
	if (*psubtree == NULL) {
		/* No parent */
		
		size_t i = 0;
		
		/* Find the first delimiter in name */
		while ((name[i] != 0) && (name[i] != '.'))
			i++;
		
		*psubtree =
		    (sysinfo_item_t *) slab_alloc(sysinfo_item_slab, 0);
		ASSERT(*psubtree);
		
		/* Fill in item name up to the delimiter */
		(*psubtree)->name = str_ndup(name, i);
		ASSERT((*psubtree)->name);
		
		/* Create subtree items */
		if (name[i] == '.') {
			(*psubtree)->subtree_type = SYSINFO_SUBTREE_TABLE;
			return sysinfo_create_path(name + i + 1,
			    &((*psubtree)->subtree.table));
		}
		
		/* No subtree needs to be created */
		return *psubtree;
	}
	
	sysinfo_item_t *cur = *psubtree;
	
	/* Walk all siblings */
	while (cur != NULL) {
		size_t i = 0;
		
		/* Compare name with path */
		while ((cur->name[i] != 0) && (name[i] == cur->name[i]))
			i++;
		
		/* Check for perfect name and path match
		 * -> item is already present.
		 */
		if ((name[i] == 0) && (cur->name[i] == 0))
			return cur;
		
		/* Partial match up to the delimiter */
		if ((name[i] == '.') && (cur->name[i] == 0)) {
			switch (cur->subtree_type) {
			case SYSINFO_SUBTREE_NONE:
				/* No subtree yet, create one */
				cur->subtree_type = SYSINFO_SUBTREE_TABLE;
				return sysinfo_create_path(name + i + 1,
				    &(cur->subtree.table));
			case SYSINFO_SUBTREE_TABLE:
				/* Subtree already created, add new sibling */
				return sysinfo_create_path(name + i + 1,
				    &(cur->subtree.table));
			default:
				/* Subtree items handled by a function, this
				 * cannot be overriden by a constant item.
				 */
				return NULL;
			}
		}
		
		/* No match and no more siblings to check
		 * -> create a new sibling item.
		 */
		if (cur->next == NULL) {
			/* Find the first delimiter in name */
			i = 0;
			while ((name[i] != 0) && (name[i] != '.'))
				i++;
			
			sysinfo_item_t *item =
			    (sysinfo_item_t *) slab_alloc(sysinfo_item_slab, 0);
			ASSERT(item);
			
			cur->next = item;
			
			/* Fill in item name up to the delimiter */
			item->name = str_ndup(name, i);
			ASSERT(item->name);
			
			/* Create subtree items */
			if (name[i] == '.') {
				item->subtree_type = SYSINFO_SUBTREE_TABLE;
				return sysinfo_create_path(name + i + 1,
				    &(item->subtree.table));
			}
			
			/* No subtree needs to be created */
			return item;
		}
		
		cur = cur->next;
	}
	
	/* Unreachable */
	ASSERT(false);
	return NULL;
}

/** Set sysinfo item with a constant numeric value
 *
 * @param name Sysinfo path.
 * @param root Pointer to the root item or where to store
 *             a new root item (NULL for global sysinfo root).
 * @param val  Value to store in the item.
 *
 */
void sysinfo_set_item_val(const char *name, sysinfo_item_t **root,
    unative_t val)
{
	/* Protect sysinfo tree consistency */
	mutex_lock(&sysinfo_lock);
	
	if (root == NULL)
		root = &global_root;
	
	sysinfo_item_t *item = sysinfo_create_path(name, root);
	if (item != NULL) {
		item->val_type = SYSINFO_VAL_VAL;
		item->val.val = val;
	}
	
	mutex_unlock(&sysinfo_lock);
}

/** Set sysinfo item with a constant binary data
 *
 * Note that sysinfo only stores the pointer to the
 * binary data and does not touch it in any way. The
 * data should be static and immortal.
 *
 * @param name Sysinfo path.
 * @param root Pointer to the root item or where to store
 *             a new root item (NULL for global sysinfo root).
 * @param data Binary data.
 * @param size Size of the binary data.
 *
 */
void sysinfo_set_item_data(const char *name, sysinfo_item_t **root,
    void *data, size_t size)
{
	/* Protect sysinfo tree consistency */
	mutex_lock(&sysinfo_lock);
	
	if (root == NULL)
		root = &global_root;
	
	sysinfo_item_t *item = sysinfo_create_path(name, root);
	if (item != NULL) {
		item->val_type = SYSINFO_VAL_DATA;
		item->val.data.data = data;
		item->val.data.size = size;
	}
	
	mutex_unlock(&sysinfo_lock);
}

/** Set sysinfo item with a generated numeric value
 *
 * @param name Sysinfo path.
 * @param root Pointer to the root item or where to store
 *             a new root item (NULL for global sysinfo root).
 * @param fn   Numeric value generator function.
 *
 */
void sysinfo_set_item_fn_val(const char *name, sysinfo_item_t **root,
    sysinfo_fn_val_t fn)
{
	/* Protect sysinfo tree consistency */
	mutex_lock(&sysinfo_lock);
	
	if (root == NULL)
		root = &global_root;
	
	sysinfo_item_t *item = sysinfo_create_path(name, root);
	if (item != NULL) {
		item->val_type = SYSINFO_VAL_FUNCTION_VAL;
		item->val.fn_val = fn;
	}
	
	mutex_unlock(&sysinfo_lock);
}

/** Set sysinfo item with a generated binary data
 *
 * Note that each time the generator function is called
 * it is supposed to return a new dynamically allocated
 * data. This data is then freed by sysinfo in the context
 * of the current sysinfo request.
 *
 * @param name Sysinfo path.
 * @param root Pointer to the root item or where to store
 *             a new root item (NULL for global sysinfo root).
 * @param fn   Binary data generator function.
 *
 */
void sysinfo_set_item_fn_data(const char *name, sysinfo_item_t **root,
    sysinfo_fn_data_t fn)
{
	/* Protect sysinfo tree consistency */
	mutex_lock(&sysinfo_lock);
	
	if (root == NULL)
		root = &global_root;
	
	sysinfo_item_t *item = sysinfo_create_path(name, root);
	if (item != NULL) {
		item->val_type = SYSINFO_VAL_FUNCTION_DATA;
		item->val.fn_data = fn;
	}
	
	mutex_unlock(&sysinfo_lock);
}

/** Set sysinfo item with an undefined value
 *
 * @param name Sysinfo path.
 * @param root Pointer to the root item or where to store
 *             a new root item (NULL for global sysinfo root).
 *
 */
void sysinfo_set_item_undefined(const char *name, sysinfo_item_t **root)
{
	/* Protect sysinfo tree consistency */
	mutex_lock(&sysinfo_lock);
	
	if (root == NULL)
		root = &global_root;
	
	sysinfo_item_t *item = sysinfo_create_path(name, root);
	if (item != NULL)
		item->val_type = SYSINFO_VAL_UNDEFINED;
	
	mutex_unlock(&sysinfo_lock);
}

/** Set sysinfo item with a generated subtree
 *
 * @param name Sysinfo path.
 * @param root Pointer to the root item or where to store
 *             a new root item (NULL for global sysinfo root).
 * @param fn   Subtree generator function.
 *
 */
void sysinfo_set_subtree_fn(const char *name, sysinfo_item_t **root,
    sysinfo_fn_subtree_t fn)
{
	/* Protect sysinfo tree consistency */
	mutex_lock(&sysinfo_lock);
	
	if (root == NULL)
		root = &global_root;
	
	sysinfo_item_t *item = sysinfo_create_path(name, root);
	
	/* Change the type of the subtree only if it is not already
	   a fixed subtree */
	if ((item != NULL) && (item->subtree_type != SYSINFO_SUBTREE_TABLE)) {
		item->subtree_type = SYSINFO_SUBTREE_FUNCTION;
		item->subtree.get_data = fn;
	}
	
	mutex_unlock(&sysinfo_lock);
}

/** Sysinfo dump indentation helper routine
 *
 * @param depth Number of indentation characters to print.
 *
 */
static void sysinfo_indent(unsigned int depth)
{
	unsigned int i;
	for (i = 0; i < depth; i++)
		printf("  ");
}

/** Dump the structure of sysinfo tree
 *
 * Should be called with sysinfo_lock held.
 *
 * @param root  Root item of the current (sub)tree.
 * @param depth Current depth in the sysinfo tree.
 *
 */
static void sysinfo_dump_internal(sysinfo_item_t *root, unsigned int depth)
{
	sysinfo_item_t *cur = root;
	
	/* Walk all siblings */
	while (cur != NULL) {
		sysinfo_indent(depth);
		
		unative_t val;
		size_t size;
		
		/* Display node value and type */
		switch (cur->val_type) {
		case SYSINFO_VAL_UNDEFINED:
			printf("+ %s\n", cur->name);
			break;
		case SYSINFO_VAL_VAL:
			printf("+ %s -> %" PRIun" (%#" PRIxn ")\n", cur->name,
			    cur->val.val, cur->val.val);
			break;
		case SYSINFO_VAL_DATA:
			printf("+ %s (%" PRIs" bytes)\n", cur->name,
			    cur->val.data.size);
			break;
		case SYSINFO_VAL_FUNCTION_VAL:
			val = cur->val.fn_val(cur);
			printf("+ %s -> %" PRIun" (%#" PRIxn ") [generated]\n",
			    cur->name, val, val);
			break;
		case SYSINFO_VAL_FUNCTION_DATA:
			/* N.B.: No data was actually returned (only a dry run) */
			(void) cur->val.fn_data(cur, &size, true);
			printf("+ %s (%" PRIs" bytes) [generated]\n", cur->name,
			    size);
			break;
		default:
			printf("+ %s [unknown]\n", cur->name);
		}
		
		/* Recursivelly nest into the subtree */
		switch (cur->subtree_type) {
		case SYSINFO_SUBTREE_NONE:
			break;
		case SYSINFO_SUBTREE_TABLE:
			sysinfo_dump_internal(cur->subtree.table, depth + 1);
			break;
		case SYSINFO_SUBTREE_FUNCTION:
			sysinfo_indent(depth + 1);
			printf("+ [generated subtree]\n");
			break;
		default:
			sysinfo_indent(depth + 1);
			printf("+ [unknown subtree]\n");
		}
		
		cur = cur->next;
	}
}

/** Dump the structure of sysinfo tree
 *
 * @param root  Root item of the sysinfo (sub)tree.
 *              If it is NULL then consider the global
 *              sysinfo tree.
 *
 */
void sysinfo_dump(sysinfo_item_t *root)
{
	/* Avoid other functions to mess with sysinfo
	   while we are dumping it */
	mutex_lock(&sysinfo_lock);
	
	if (root == NULL)
		sysinfo_dump_internal(global_root, 0);
	else
		sysinfo_dump_internal(root, 0);
	
	mutex_unlock(&sysinfo_lock);
}

/** Return sysinfo item value determined by name
 *
 * Should be called with sysinfo_lock held.
 *
 * @param name    Sysinfo path.
 * @param root    Root item of the sysinfo (sub)tree.
 *                If it is NULL then consider the global
 *                sysinfo tree.
 * @param dry_run Do not actually get any generated
 *                binary data, just calculate the size.
 *
 * @return Item value (constant or generated).
 *
 */
static sysinfo_return_t sysinfo_get_item(const char *name,
    sysinfo_item_t **root, bool dry_run)
{
	if (root == NULL)
		root = &global_root;
	
	/* Try to find the item or generate data */
	sysinfo_return_t ret;
	sysinfo_return_t *ret_ptr = &ret;
	sysinfo_item_t *item = sysinfo_find_item(name, *root, &ret_ptr,
	    dry_run);
	
	if (item != NULL) {
		/* Item found in the fixed sysinfo tree */
		
		ret.tag = item->val_type;
		switch (item->val_type) {
		case SYSINFO_VAL_UNDEFINED:
			break;
		case SYSINFO_VAL_VAL:
			ret.val = item->val.val;
			break;
		case SYSINFO_VAL_DATA:
			ret.data = item->val.data;
			break;
		case SYSINFO_VAL_FUNCTION_VAL:
			ret.val = item->val.fn_val(item);
			break;
		case SYSINFO_VAL_FUNCTION_DATA:
			ret.data.data = item->val.fn_data(item, &ret.data.size,
			    dry_run);
			break;
		}
	} else {
		/* No item in the fixed sysinfo tree */
		if (ret_ptr == NULL) {
			/* Even no data was generated */
			ret.tag = SYSINFO_VAL_UNDEFINED;
		}
	}
	
	return ret;
}

/** Return sysinfo item determined by name from user space
 *
 * The path string passed from the user space has to be properly null-terminated
 * (the last passed character must be null).
 *
 * @param ptr     Sysinfo path in the user address space.
 * @param size    Size of the path string.
 * @param dry_run Do not actually get any generated
 *                binary data, just calculate the size.
 *
 */
static sysinfo_return_t sysinfo_get_item_uspace(void *ptr, size_t size,
    bool dry_run)
{
	sysinfo_return_t ret;
	ret.tag = SYSINFO_VAL_UNDEFINED;
	
	if (size > SYSINFO_MAX_PATH)
		return ret;
	
	char *path = (char *) malloc(size + 1, 0);
	ASSERT(path);
	
	if ((copy_from_uspace(path, ptr, size + 1) == 0)
	    && (path[size] == 0)) {
		/*
		 * Prevent other functions from messing with sysinfo while we
		 * are reading it.
		 */
		mutex_lock(&sysinfo_lock);
		ret = sysinfo_get_item(path, NULL, dry_run);
		mutex_unlock(&sysinfo_lock);
	}
	free(path);
	return ret;
}

/** Get the sysinfo value type (syscall)
 *
 * The path string passed from the user space has
 * to be properly null-terminated (the last passed
 * character must be null).
 *
 * @param path_ptr  Sysinfo path in the user address space.
 * @param path_size Size of the path string.
 *
 * @return Item value type.
 *
 */
unative_t sys_sysinfo_get_tag(void *path_ptr, size_t path_size)
{
	/*
	 * Get the item.
	 *
	 * N.B.: There is no need to free any potential generated
	 * binary data since we request a dry run.
	 */
	sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size, true);
	
	/*
	 * Map generated value types to constant types (user space does not care
	 * whether the value is constant or generated).
	 */
	if (ret.tag == SYSINFO_VAL_FUNCTION_VAL)
		ret.tag = SYSINFO_VAL_VAL;
	else if (ret.tag == SYSINFO_VAL_FUNCTION_DATA)
		ret.tag = SYSINFO_VAL_DATA;
	
	return (unative_t) ret.tag;
}

/** Get the sysinfo numerical value (syscall)
 *
 * The path string passed from the user space has
 * to be properly null-terminated (the last passed
 * character must be null).
 *
 * @param path_ptr  Sysinfo path in the user address space.
 * @param path_size Size of the path string.
 * @param value_ptr User space pointer where to store the
 *                  numberical value.
 *
 * @return Error code (EOK in case of no error).
 *
 */
unative_t sys_sysinfo_get_value(void *path_ptr, size_t path_size,
    void *value_ptr)
{
	int rc;

	/*
	 * Get the item.
	 *
	 * N.B.: There is no need to free any potential generated binary data
	 * since we request a dry run.
	 */
	sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size, true);
	
	/* Only constant or generated numerical value is returned */
	if ((ret.tag == SYSINFO_VAL_VAL) || (ret.tag == SYSINFO_VAL_FUNCTION_VAL))
		rc = copy_to_uspace(value_ptr, &ret.val, sizeof(ret.val));
	else
		rc = EINVAL;
	
	return (unative_t) rc;
}

/** Get the sysinfo binary data size (syscall)
 *
 * The path string passed from the user space has
 * to be properly null-terminated (the last passed
 * character must be null).
 *
 * @param path_ptr  Sysinfo path in the user address space.
 * @param path_size Size of the path string.
 * @param size_ptr  User space pointer where to store the
 *                  binary data size.
 *
 * @return Error code (EOK in case of no error).
 *
 */
unative_t sys_sysinfo_get_data_size(void *path_ptr, size_t path_size,
    void *size_ptr)
{
	int rc;
	
	/*
	 * Get the item.
	 *
	 * N.B.: There is no need to free any potential generated binary data
	 * since we request a dry run.
	 */
	sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size, true);
	
	/* Only the size of constant or generated binary data is considered */
	if ((ret.tag == SYSINFO_VAL_DATA) || (ret.tag == SYSINFO_VAL_FUNCTION_DATA))
		rc = copy_to_uspace(size_ptr, &ret.data.size,
		    sizeof(ret.data.size));
	else
		rc = EINVAL;
	
	return (unative_t) rc;
}

/** Get the sysinfo binary data (syscall)
 *
 * The path string passed from the user space has
 * to be properly null-terminated (the last passed
 * character must be null).
 *
 * The user space buffer must be sized exactly according
 * to the size of the binary data, otherwise the request
 * fails.
 *
 * @param path_ptr    Sysinfo path in the user address space.
 * @param path_size   Size of the path string.
 * @param buffer_ptr  User space pointer to the buffer where
 *                    to store the binary data.
 * @param buffer_size User space buffer size.
 *
 * @return Error code (EOK in case of no error).
 *
 */
unative_t sys_sysinfo_get_data(void *path_ptr, size_t path_size,
    void *buffer_ptr, size_t buffer_size)
{
	int rc;
	
	/* Get the item */
	sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size, false);

	/* Only constant or generated binary data is considered */
	if ((ret.tag == SYSINFO_VAL_DATA) || (ret.tag == SYSINFO_VAL_FUNCTION_DATA)) {
		/* Check destination buffer size */
		if (ret.data.size == buffer_size)
			rc = copy_to_uspace(buffer_ptr, ret.data.data,
			    ret.data.size);
		else
			rc = ENOMEM;
	} else
		rc = EINVAL;
	
	/* N.B.: The generated binary data should be freed */
	if ((ret.tag == SYSINFO_VAL_FUNCTION_DATA) && (ret.data.data != NULL))
		free(ret.data.data);
	
	return (unative_t) rc;
}

/** @}
 */
