233 lines
5.6 KiB
C
233 lines
5.6 KiB
C
/* ************************************************************************** */
|
|
/* _____ _ */
|
|
/* core/log.c |_ _|__ _ _| |__ ___ _ _ */
|
|
/* | Project: libp7 | |/ _ \| | | | '_ \ / _ \ | | | */
|
|
/* | | (_) | |_| | | | | __/ |_| | */
|
|
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
|
|
/* Last updated: 2016/10/13 07:33:17 |___/ */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#include <libp7/internals.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
/**
|
|
* log_mem_hex:
|
|
* Prints the octal interpretation of a max of two octets.
|
|
*
|
|
* @arg s the string where to put it
|
|
* @arg m the memory zone to print
|
|
* @arg n the size of the memory zone
|
|
*/
|
|
|
|
static void log_mem_hex(char *s, const unsigned char *m, size_t n)
|
|
{
|
|
size_t l = 0;
|
|
while (l < 16) {
|
|
/* put the hex number */
|
|
if (n) { p7_putascii((unsigned char*)s, *m++, 2); s += 2; }
|
|
else { *s++ = ' '; *s++ = ' '; }
|
|
|
|
/* decrement size of the memory zone to go */
|
|
n -= !!n;
|
|
|
|
/* go to next character if s is at the ending of a group */
|
|
if (l++ % 2) s++;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* log_mem_asc:
|
|
* Prints the ascii interpretation of a max of two octets.
|
|
*
|
|
* @arg s the string where to put it
|
|
* @arg m the memory zone to print
|
|
* @arg n the size of the memory zone
|
|
*/
|
|
|
|
static void log_mem_asc(char *s, const unsigned char *m, size_t n)
|
|
{
|
|
size_t l = 0;
|
|
/* for each byte */
|
|
while (n-- && l++ < 16) {
|
|
/* put the character (or a dot if non printable) */
|
|
if (isprint(*m++)) *s++ = *((const char*)m - 1);
|
|
else *s++ = '.';
|
|
}
|
|
/* put the line ending */
|
|
*s++ = '\n';
|
|
*s = '\0';
|
|
}
|
|
|
|
/**
|
|
* p7_log_mem:
|
|
* Print memory zone.
|
|
*
|
|
* @arg prefx the line prefix
|
|
* @arg m the memory zone to print
|
|
* @arg n the size of the memory zone
|
|
*/
|
|
|
|
void p7_log_mem(const char *prefx, const void *m, size_t n)
|
|
{
|
|
/* if nothing, print it directly */
|
|
if (!n) fprintf(stderr, "%s(nothing)\n", prefx);
|
|
|
|
/* prepare line buffer */
|
|
unsigned int lineoff = strlen(prefx);
|
|
char linebuf[lineoff + 58];
|
|
memcpy(linebuf, prefx, lineoff);
|
|
/* - for spaces - */
|
|
memcpy(&linebuf[lineoff], "0000 0000 0000 0000 0000 0000 0000 0000 ", 40);
|
|
|
|
/* then loop-loop-loop-loop-loop */
|
|
const unsigned char *p = m;
|
|
while (n > 0) {
|
|
/* fill in ascii-hex part */
|
|
log_mem_hex(&linebuf[lineoff], p, n);
|
|
/* fill in ascii part */
|
|
log_mem_asc(&linebuf[lineoff + 40], p, n);
|
|
/* then print line */
|
|
fputs(linebuf, stderr);
|
|
/* and increment pointer */
|
|
p += 16;
|
|
n -= min(16, n);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* p7_getcmdstring:
|
|
* Get command code string (useful for logging).
|
|
*
|
|
* @arg code command code
|
|
* @return the string
|
|
*/
|
|
|
|
const char *p7_getcmdstring(p7_commandcode_t code)
|
|
{
|
|
static const char *sys_commands[] = {
|
|
"restart the equipment",
|
|
"get device info",
|
|
"set link settings",
|
|
NULL, NULL, NULL,
|
|
"set link timeout",
|
|
"OS Update verification #1",
|
|
"OS Update verification #2",
|
|
"OS Update verification #3",
|
|
"OS Update verification #4"
|
|
}, *mcs_commands[] = {
|
|
"create directory on mcs",
|
|
"delete directory on mcs",
|
|
"rename directory on mcs",
|
|
"change working directory on mcs",
|
|
"request file transfer from mcs",
|
|
"transfer file on mcs",
|
|
"delete file on mcs",
|
|
"rename file on mcs",
|
|
"copy file on mcs",
|
|
"request all files transfer on mcs",
|
|
"reset mcs",
|
|
"request mcs capacity",
|
|
"transmit mcs capacity",
|
|
"request all mcs file info",
|
|
"transfer all mcs file info",
|
|
"request RAM image",
|
|
"transfer RAM image",
|
|
"request setup entry",
|
|
"transfer setup entry",
|
|
"request all setup entry"
|
|
}, *fls_commands[] = {
|
|
"create directory on flash",
|
|
"delete directory on flash",
|
|
"rename directory on flash",
|
|
"change working directory on flash",
|
|
"request flash file",
|
|
"transfer flash file",
|
|
"delete file on flash",
|
|
"rename file on flash",
|
|
"copy file on flash",
|
|
"request all flash file",
|
|
"reset flash",
|
|
"capacity transmit request",
|
|
"capacity transmit",
|
|
"request flash file info",
|
|
"transfer flash file info",
|
|
"request ROM image",
|
|
"transfer ROM image",
|
|
"optimize flash filesystem",
|
|
|
|
/* more like OS update from now */
|
|
"request CASIOWIN entry",
|
|
"send CASIOWIN entry",
|
|
"request boot code",
|
|
"send boot code",
|
|
"upload and run"
|
|
};
|
|
|
|
/* return the string */
|
|
if (code >= 0x40) return fls_commands[code - 0x40];
|
|
else if (code >= 0x20) return mcs_commands[code - 0x20];
|
|
else return sys_commands[code];
|
|
}
|
|
|
|
/**
|
|
* p7_geterrstring:
|
|
* Get error string (useful for logging).
|
|
*
|
|
* @arg code error code
|
|
* @return the string
|
|
*/
|
|
|
|
const char *p7_geterrstring(p7_errcode_t code)
|
|
{
|
|
static const char *strings[] = {
|
|
"should resend",
|
|
"file exists, overwrite request",
|
|
"will not overwrite",
|
|
"overwrite impossible",
|
|
"memory is full"
|
|
};
|
|
|
|
return (strings[code - 1]);
|
|
}
|
|
|
|
/**
|
|
* p7_gettermstring:
|
|
* Get termination cause string (useful for logging).
|
|
*
|
|
* @arg code cause code
|
|
* @return the string
|
|
*/
|
|
|
|
const char *p7_gettermstring(p7_termtype_t code)
|
|
{
|
|
static const char *strings[] = {
|
|
"default",
|
|
"user requested",
|
|
"timeouts",
|
|
"on overwrite request"
|
|
};
|
|
|
|
return (strings[code]);
|
|
}
|
|
|
|
/**
|
|
* p7_getowstring:
|
|
* Get overwrite string (useful for logging).
|
|
*
|
|
* @arg code overwrite code
|
|
* @return the string
|
|
*/
|
|
|
|
const char *p7_getowstring(p7_commandoverwrite_t code)
|
|
{
|
|
static const char *moo[] = {
|
|
"request confirmation before overwriting",
|
|
"terminate if file exists",
|
|
"force overwrite"
|
|
};
|
|
|
|
return (moo[code]);
|
|
}
|