/* 
 * Copyright 2011 Marian Ohligs, Chair for Operating Systems,
 *                               RWTH Aachen University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * This file is part of MetalSVM. 
 */

#include <metalsvm/stdlib.h>
#include <metalsvm/string.h>
#include <metalsvm/stdio.h>
#include <metalsvm/errno.h>
#include <metalsvm/fs.h>
#include <asm/kb.h>
#include <metalsvm/tasks.h>
#include <metalsvm/spinlock.h>

/* Functions of a simple null device */

static ssize_t null_read(fildes_t* file, uint8_t* buffer, size_t size)
{
	memset(buffer, 0x00, size);

	file->offset += size;
	return size;
}

static ssize_t null_write(fildes_t* file, uint8_t* buffer, size_t size)
{
	return size;
}

static int null_open(fildes_t* file, const char* name)
{
	return 0;
}

static int null_close(fildes_t* file)
{
	return 0;
}

/* Read Function of a stdio device */

static ssize_t stdio_read(fildes_t* file, uint8_t* buffer, size_t size)
{
#ifdef CONFIG_KEYBOARD
	kb_init(size, per_core(current_task)->id);

	block_current_task();
	reschedule();
	size = kb_buffer.size;
	memcpy(buffer, kb_buffer.buffer, size); 

	/*cleaning  up */
	kb_finish();

	//kprintf("Size: %i, offset: %i, buffer: %s", size, buffer, offset);
	file->offset += size;
	return size;
#else
	return 0;
#endif
}

/* Write Function of a stdio device */

static ssize_t stdio_write(fildes_t* file, uint8_t* buffer, size_t size)
{
	int i;
	for (i = 0; i<size; i++, buffer++) {
		kputchar(*buffer);
	}

	file->offset += size;
	return size;
}

/* Init Functions */

int null_init(vfs_node_t* node, const char* name)
{
	uint32_t i, j;
	vfs_node_t* new_node;
	dir_block_t* blockdir;
	dirent_t* dirent;
	block_list_t* blist;

	if (BUILTIN_EXPECT(!node || !name, 0))
		return -EINVAL;

	if (BUILTIN_EXPECT(node->type != FS_DIRECTORY, 0))
		return -EINVAL;

	if (finddir_fs(node, name))
		return -EINVAL;

	new_node = kmalloc(sizeof(vfs_node_t));
	if (BUILTIN_EXPECT(!new_node, 0))
		return -ENOMEM;

	memset(new_node, 0x00, sizeof(vfs_node_t));
	new_node->type = FS_CHARDEVICE;
	new_node->open = &null_open;
	new_node->close = &null_close;
	new_node->read = &null_read;
	new_node->write = &null_write;
	spinlock_init(&new_node->lock);

	blist= &node->block_list;
	do {
		for(i=0; i<MAX_DATABLOCKS; i++) {
			if (blist->data[i]) {
				blockdir = (dir_block_t*) blist->data[i];
				for(j=0; j<MAX_DIRENTRIES; j++) {
					dirent = &blockdir->entries[j];
					if (!dirent->vfs_node) {
						dirent->vfs_node = new_node;
						strncpy(dirent->name, name, MAX_FNAME);
						return 0;
					}
				}
			}
		}

		if (!blist->next) {
			blist->next = (block_list_t*) kmalloc(sizeof(block_list_t));
			if (blist->next)
				memset(blist->next, 0x00, sizeof(block_list_t));
		}

	} while(blist);

	kfree(new_node, sizeof(vfs_node_t));

	return -ENOMEM;
}


int stdin_init(vfs_node_t* node, const char* name)
{
	uint32_t i, j;
	vfs_node_t* new_node;
	dir_block_t* blockdir;
	dirent_t* dirent;
	block_list_t* blist;

	if (BUILTIN_EXPECT(!node || !name, 0))
		return -EINVAL;

	if (BUILTIN_EXPECT(node->type != FS_DIRECTORY, 0))
		return -EINVAL;

	if (finddir_fs(node, name))
		return -EINVAL;

	new_node = kmalloc(sizeof(vfs_node_t));
	if (BUILTIN_EXPECT(!new_node, 0))
		return -ENOMEM;

	memset(new_node, 0x00, sizeof(vfs_node_t));
	new_node->type = FS_CHARDEVICE;
	new_node->open = &null_open;
	new_node->close = &null_close;
	new_node->read = &stdio_read;
	new_node->write = &null_write;
	spinlock_init(&new_node->lock);

	blist= &node->block_list;
	do {
		for(i=0; i<MAX_DATABLOCKS; i++) {
			if (blist->data[i]) {
				blockdir = (dir_block_t*) blist->data[i];
				for(j=0; j<MAX_DIRENTRIES; j++) {
					dirent = &blockdir->entries[j];
					if (!dirent->vfs_node) {
						dirent->vfs_node = new_node;
						strncpy(dirent->name, name, MAX_FNAME);
						return 0;
					}
				}
			}
		}

		if (!blist->next) {
			blist->next = (block_list_t*) kmalloc(sizeof(block_list_t));
			if (blist->next)
				memset(blist->next, 0x00, sizeof(block_list_t));
		}

	} while(blist);

	kfree(new_node, sizeof(vfs_node_t));

	return -ENOMEM;
}


int stdout_init(vfs_node_t* node, const char* name)
{
	uint32_t i, j;
	vfs_node_t* new_node;
	dir_block_t* blockdir;
	dirent_t* dirent;
	block_list_t* blist;

	if (BUILTIN_EXPECT(!node || !name, 0))
		return -EINVAL;

	if (BUILTIN_EXPECT(node->type != FS_DIRECTORY, 0))
		return -EINVAL;

	if (finddir_fs(node, name))
		return -EINVAL;

	new_node = kmalloc(sizeof(vfs_node_t));
	if (BUILTIN_EXPECT(!new_node, 0))
		return -ENOMEM;

	memset(new_node, 0x00, sizeof(vfs_node_t));
	new_node->type = FS_CHARDEVICE;
	new_node->open = &null_open;
	new_node->close = &null_close;
	new_node->read = &null_read;
	new_node->write = &stdio_write;
	spinlock_init(&new_node->lock);

	blist= &node->block_list;
	do {
		for(i=0; i<MAX_DATABLOCKS; i++) {
			if (blist->data[i]) {
				blockdir = (dir_block_t*) blist->data[i];
				for(j=0; j<MAX_DIRENTRIES; j++) {
					dirent = &blockdir->entries[j];
					if (!dirent->vfs_node) {
						dirent->vfs_node = new_node;
						strncpy(dirent->name, name, MAX_FNAME);
						return 0;
					}
				}
			}
		}

		if (!blist->next) {
			blist->next = (block_list_t*) kmalloc(sizeof(block_list_t));
			if (blist->next)
				memset(blist->next, 0x00, sizeof(block_list_t));
		}

	} while(blist);

	kfree(new_node, sizeof(vfs_node_t));

	return -ENOMEM;
}


int stderr_init(vfs_node_t* node, const char* name)
{
	uint32_t i, j;
	vfs_node_t* new_node;
	dir_block_t* blockdir;
	dirent_t* dirent;
	block_list_t* blist;

	if (BUILTIN_EXPECT(!node || !name, 0))
		return -EINVAL;

	if (BUILTIN_EXPECT(node->type != FS_DIRECTORY, 0))
		return -EINVAL;

	if (finddir_fs(node, name))
		return -EINVAL;

	new_node = kmalloc(sizeof(vfs_node_t));
	if (BUILTIN_EXPECT(!new_node, 0))
		return -ENOMEM;

	memset(new_node, 0x00, sizeof(vfs_node_t));
	new_node->type = FS_CHARDEVICE;
	new_node->open = &null_open;
	new_node->close = &null_close;
	new_node->read = &null_read;
	new_node->write = &stdio_write;
	spinlock_init(&new_node->lock);

	blist= &node->block_list;
	do {
		for(i=0; i<MAX_DATABLOCKS; i++) {
			if (blist->data[i]) {
				blockdir = (dir_block_t*) blist->data[i];
				for(j=0; j<MAX_DIRENTRIES; j++) {
					dirent = &blockdir->entries[j];
					if (!dirent->vfs_node) {
						dirent->vfs_node = new_node;
						strncpy(dirent->name, name, MAX_FNAME);
						return 0;
					}
				}
			}
		}

		if (!blist->next) {
			blist->next = (block_list_t*) kmalloc(sizeof(block_list_t));
			if (blist->next)
				memset(blist->next, 0x00, sizeof(block_list_t));
		}

	} while(blist);

	kfree(new_node, sizeof(vfs_node_t));

	return -ENOMEM;
}