view rvinterf/etmsync/fileio.c @ 650:a432bcc79039

gsm-fw/ccd: Makefile created, all core library modules compile!
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 21 Sep 2014 00:59:02 +0000
parents ffeea2f9d149
children d1333db6385f
line wrap: on
line source

/*
 * FFS2 file descriptor I/O operations
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "etm.h"
#include "ffs.h"
#include "tmffs2.h"
#include "limits.h"
#include "localtypes.h"
#include "localstruct.h"
#include "exitcodes.h"

extern u_char rvi_msg[];
extern int rvi_msg_len;

fd_open(pathname, flags, fdrtn)
	char *pathname;
	int flags, *fdrtn;
{
	u_char cmdpkt[MAX_PKT_TO_TARGET], *dp;
	int rc, slen;

	slen = strlen(pathname);
	if (slen >= TMFFS_STRING_SIZE) {
		printf("error: pathname arg exceeds string length limit\n");
		return(ERROR_USAGE);
	}
	dp = cmdpkt + 1;
	*dp++ = ETM_FFS2;
	*dp++ = TMFFS_OPEN;
	*dp++ = slen + 1;
	strcpy(dp, pathname);
	dp += slen + 1;
	*dp++ = flags;
	rc = etm_pkt_exch(cmdpkt, dp - cmdpkt - 1);
	if (rc)
		return(rc);
	if (rvi_msg[3]) {
		report_ffs_err("open fd", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	if (rvi_msg_len != 6) {
		printf("error: open fd response has wrong length\n");
		return(ERROR_TARGET);
	}
	*fdrtn = rvi_msg[4];
	return(0);
}

fd_read(fd, databuf, rdsize, rdret)
	u_char *databuf;
	int fd, rdsize, *rdret;
{
	u_char cmdpkt[6];
	int rc, sz;

	if (rdsize > MAX_READ_DATA) {
		printf("error: # of bytes to read may not exceed %d\n",
			MAX_READ_DATA);
		return(ERROR_USAGE);
	}
	cmdpkt[1] = ETM_FFS2;
	cmdpkt[2] = TMFFS_READ;
	cmdpkt[3] = fd;
	cmdpkt[4] = rdsize;
	rc = etm_pkt_exch(cmdpkt, 4);
	if (rc)
		return(rc);
	if (rvi_msg[3]) {
		report_ffs_err("read fd", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	if (rvi_msg_len < 6) {
		*rdret = 0;
		return(0);
	}
	sz = rvi_msg[4];
	if (rvi_msg_len != sz + 6 || sz > rdsize) {
		printf("error: read fd response has wrong length\n");
		return(ERROR_TARGET);
	}
	bcopy(rvi_msg + 5, databuf, sz);
	*rdret = sz;
	return(0);
}

fd_write(fd, data, wrsize)
	u_char *data;
{
	u_char cmdpkt[MAX_PKT_TO_TARGET], *dp;
	int rc;

	if (wrsize > MAX_PKT_TO_TARGET - 6) {
		printf("error: fd write data fails to fit in the packet\n");
		return(ERROR_USAGE);
	}
	dp = cmdpkt + 1;
	*dp++ = ETM_FFS2;
	*dp++ = TMFFS_WRITE;
	*dp++ = fd;
	*dp++ = wrsize;
	bcopy(data, dp, wrsize);
	dp += wrsize;
	rc = etm_pkt_exch(cmdpkt, dp - cmdpkt - 1);
	if (rc)
		return(rc);
	if (rvi_msg[3]) {
		report_ffs_err("fd write", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	if (rvi_msg_len != 6) {
		printf("error: TMFFS_WRITE response has wrong length\n");
		return(ERROR_TARGET);
	}
	if (rvi_msg[4] != wrsize) {
		printf("fd write error: # of bytes written != requested\n");
		return(ERROR_TARGET);
	}
	return(0);
}

fd_close(fd)
{
	u_char cmdpkt[5];
	int rc;

	cmdpkt[1] = ETM_FFS2;
	cmdpkt[2] = TMFFS_CLOSE;
	cmdpkt[3] = fd;
	rc = etm_pkt_exch(cmdpkt, 3);
	if (rc)
		return(rc);
	if (rvi_msg[3]) {
		report_ffs_err("close fd", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	if (rvi_msg_len != 5) {
		printf("error: close fd response has wrong length\n");
		return(ERROR_TARGET);
	}
	return(0);
}

do_file_read(pathname, databuf, rdsize, rdret)
	char *pathname;
	u_char *databuf;
	int rdsize, *rdret;
{
	u_char cmdpkt[MAX_PKT_TO_TARGET], *dp;
	int rc, slen, sz;

	slen = strlen(pathname);
	if (slen >= TMFFS_STRING_SIZE) {
		printf("error: pathname arg exceeds string length limit\n");
		return(ERROR_USAGE);
	}
	if (rdsize > MAX_READ_DATA) {
		printf("error: # of bytes to read may not exceed %d\n",
			MAX_READ_DATA);
		return(ERROR_USAGE);
	}
	dp = cmdpkt + 1;
	*dp++ = ETM_FFS2;
	*dp++ = TMFFS_FILE_READ;
	*dp++ = slen + 1;
	strcpy(dp, pathname);
	dp += slen + 1;
	*dp++ = rdsize;
	rc = etm_pkt_exch(cmdpkt, dp - cmdpkt - 1);
	if (rc)
		return(rc);
	if (rvi_msg[3]) {
		report_ffs_err("read file", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	if (rvi_msg_len < 6) {
		*rdret = 0;
		return(0);
	}
	sz = rvi_msg[4];
	if (rvi_msg_len != sz + 6 || sz > rdsize) {
		printf("error: read file response has wrong length\n");
		return(ERROR_TARGET);
	}
	bcopy(rvi_msg + 5, databuf, sz);
	*rdret = sz;
	return(0);
}

max_short_file_write(pathname)
	char *pathname;
{
	return MAX_PKT_TO_TARGET - 3 - (strlen(pathname) + 2) - 3;
}

do_short_fwrite(pathname, data, datalen)
	char *pathname;
	u_char *data;
{
	u_char cmdpkt[MAX_PKT_TO_TARGET], *dp;
	int rc, slen;

	slen = strlen(pathname);
	if (slen >= TMFFS_STRING_SIZE) {
		printf("error: pathname arg exceeds string length limit\n");
		return(ERROR_USAGE);
	}
	if (datalen > max_short_file_write(pathname)) {
		printf("error: short write data fails to fit in the packet\n");
		return(ERROR_USAGE);
	}
	dp = cmdpkt + 1;
	*dp++ = ETM_FFS2;
	*dp++ = TMFFS_FILE_WRITE;
	*dp++ = slen + 1;
	strcpy(dp, pathname);
	dp += slen + 1;
	*dp++ = datalen;
	bcopy(data, dp, datalen);
	dp += datalen;
	*dp++ = FFS_O_CREATE | FFS_O_TRUNC;
	rc = etm_pkt_exch(cmdpkt, dp - cmdpkt - 1);
	if (rc)
		return(rc);
	if (rvi_msg_len != 5) {
		printf("error: TMFFS_FILE_WRITE response has wrong length\n");
		return(ERROR_TARGET);
	}
	if (rvi_msg[3]) {
		report_ffs_err("short file write", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	return(0);
}