aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/cmd_write.c
blob: d54c0b5d047524a21496f3193ec6c06dcee6b311 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "buf.h"
#include "core.h"

#include "libcmd/cmd.h"
#include "libcmd/cmd_parse.h"

extern file_t *g_file;
extern buf_t *g_buf;
extern int g_flags;

//support masks
int c_write( cmd_arg_t *arg)
{
	/*
	anonymous function
	*/
	uint8_t hex2u8( uint8_t *buf )
	{
		uint8_t ret = 0x00;
		unsigned long int uli;
		char str[3];
		str[0] = buf[0];
		str[1] = buf[1];
		str[2] = 0;

		uli = strtoul( str, NULL, 16 );

		ret = uli;

		return ret;
	}

	int argc = arg->argc;
	char **argv = arg->argv;
	int i;
	uint8_t *buf = NULL;
	int fret;

	if ( argc != 1 )
	{
		printf("One argument needed\n");
		return -1;
	}

	if ( (strlen(argv[0])%2) != 0 )
	{
		printf("Input string should be ( str mod 2 == 0) \n");
		return -1;
	}

	for (i=0;i<strlen(argv[0]);i++)
	{
		if ( !isxdigit(argv[0][i])  )
		{
			printf("CH %c not hexlike at pos %d\n", argv[0][i], i);
			return -1;
		}
	}

	if (strlen(argv[0]) > g_buf->size*2)
	{
		printf("Input param bigger then buffer\n");
		return -1;
	}

	for (i=0; i<strlen(argv[0]); i+=2)
	{
		printf("%02x ",(unsigned char)hex2u8((unsigned char*)&argv[0][i]));
	}
	printf("\n");
	

	buf = malloc(strlen(argv[0])/2);
	for (i=0; i<(strlen(argv[0])/2); i++)
	{
		buf[i] = hex2u8((unsigned char*)&argv[0][i*2]);
	}

	memcpy( g_buf->buf, buf, strlen(argv[0])/2 );
	fret = file_write_blk( g_file, g_buf->buf );
	free( buf );

	if ( fret < 0)
	{
		printf("Couldnt write block to file\n");
		return -1;
	}

	return 0;
}

int h_write( cmd_arg_t *arg )
{
	printf("[HEXBUF] - hex data writen to buf and back to file");
	return 0;
}