/* *****************************************************************
 * (C) Copyright 2007 Benjamin Krill <ben@codiert.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 * MA 02110-1301 USA
 * *****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

int fpgafs_create_context(int fd, const char *lldrv, const char *dir)
{
	char *cdir;
	int res,fd1;

	/* create context directory */
	cdir=malloc(sizeof(dir) + 8);
	sprintf(cdir, "/fpgafs/%s", dir);
	if (mkdir(dir, 0777) == -1) {
		printf("ERROR-1: %s\n", strerror(errno));
		free(cdir);
		return -1;
	}

	/* change to the context directory */
	if (chdir(cdir) == -1) {
		printf("ERROR-2: %s\n", strerror(errno));
		free(cdir);
		return -1;
	}

	free(cdir);
	/* open the load file */
	if ((fd1 = open("load", O_WRONLY, 0)) == -1) {
		printf("ERROR-3: %s\n", strerror(errno));
		return -1;
	}

	/* write the file into load */
	free(cdir);
	cdir=malloc(1024);
	while ((res=read(fd, cdir, 1024)))
		write(fd1, cdir, res);

	free(cdir);
	return 0;
}

int fpgafs_send_data(const char *dir, const void *buf, size_t count)
{
	int fd, res;
	char *cdir;
	cdir=malloc(sizeof(dir) + 8);
	sprintf(cdir, "/fpgafs/%s", dir);

	/* change to the context directory */
	if (chdir(cdir) == -1) {
		printf("ERROR-1: %s\n", strerror(errno));
		free(cdir);
		return -1;
	}

	free(cdir);
	/* open the load file */
	if ((fd = open("dout", O_RDONLY, 0)) == -1) {
		printf("ERROR-2: %s\n", strerror(errno));
		return -1;
	}

	/* write stuff into the file */
	if ((res=write(fd, buf, count)) == -1) {
		printf("ERROR-3: %s\n", strerror(errno));
		return -1;
	}

	return res;
}

int fpgafs_recv_data(const char *dir, void *buf, size_t count)
{
	int fd, res;
	char *cdir;
	cdir=malloc(sizeof(dir) + 8);
	sprintf(cdir, "/fpgafs/%s", dir);

	/* change to the context directory */
	if (chdir(cdir) == -1) {
		printf("ERROR-1: %s\n", strerror(errno));
		free(cdir);
		return -1;
	}

	free(cdir);
	/* open the load file */
	if ((fd = open("din", O_WRONLY, 0)) == -1) {
		printf("ERROR-2: %s\n", strerror(errno));
		return -1;
	}

	/* write stuff into the file */
	if ((res=read(fd, buf, count)) == -1) {
		printf("ERROR-3: %s\n", strerror(errno));
		return -1;
	}

	return res;
}

int fpgafs_send_cmd(const char *dir, unsigned int cmd)
{
	int fd, res;
	char *cdir;
	cdir=malloc(sizeof(dir) + 8);
	sprintf(cdir, "/fpgafs/%s", dir);

	/* change to the context directory */
	if (chdir(cdir) == -1) {
		printf("ERROR-1: %s\n", strerror(errno));
		free(cdir);
		return -1;
	}

	free(cdir);
	/* open the load file */
	if ((fd = open("cmd", O_WRONLY, 0)) == -1) {
		printf("ERROR-2: %s\n", strerror(errno));
		return -1;
	}

	/* write stuff into the file */
	if ((res=write(fd, (const char *)&cmd, 4)) == -1) {
		printf("ERROR-3: %s\n", strerror(errno));
		return -1;
	}

	return res;
}

int fpgafs_get_stat(const char *dir)
{
	int fd;
	char *cdir;
	char buf[4];
	int res;
	cdir=malloc(sizeof(dir) + 8);
	sprintf(cdir, "/fpgafs/%s", dir);

	/* change to the context directory */
	if (chdir(cdir) == -1) {
		printf("ERROR-1: %s\n", strerror(errno));
		free(cdir);
		return -1;
	}

	free(cdir);
	/* open the load file */
	if ((fd = open("stat", O_RDONLY, 0)) == -1) {
		printf("ERROR-2: %s\n", strerror(errno));
		return -1;
	}

	/* write stuff into the file */
	if ((res=read(fd, buf, 4)) == -1) {
		printf("ERROR-3: %s\n", strerror(errno));
		return -1;
	}

	return (int)*(int*)&buf[0];
}

int fpgafs_set_lldrv(const char *dir, const char* name)
{
	int fd;
	char *cdir;
	int res;
	cdir=malloc(sizeof(dir) + 8);
	sprintf(cdir, "/fpgafs/%s", dir);

	/* change to the context directory */
	if (chdir(cdir) == -1) {
		printf("ERROR-1: %s\n", strerror(errno));
		free(cdir);
		return -1;
	}

	free(cdir);
	/* open the load file */
	if ((fd = open("lldrv", O_WRONLY, 0)) == -1) {
		printf("ERROR-2: %s\n", strerror(errno));
		return -1;
	}

	/* write stuff into the file */
	if ((res=write(fd, name, 5)) == -1) {
		printf("ERROR-3: %s\n", strerror(errno));
		return -1;
	}

	return 0;
}
