hf waveshare loadbmp - now uses cliparser

This commit is contained in:
iceman1001 2020-10-06 11:44:55 +02:00
commit 701b924f03

View file

@ -13,6 +13,7 @@
#include "util.h" #include "util.h"
#include "fileutils.h" #include "fileutils.h"
#include "util_posix.h" // msleep #include "util_posix.h" // msleep
#include "cliparser.h"
// Currently the largest pixel 880*528 only needs 58.08K bytes // Currently the largest pixel 880*528 only needs 58.08K bytes
#define WSMAPSIZE 60000 #define WSMAPSIZE 60000
@ -90,23 +91,6 @@ static model_t models[] = {
static int CmdHelp(const char *Cmd); static int CmdHelp(const char *Cmd);
static int usage_hf_waveshare_loadbmp(void) {
PrintAndLogEx(NORMAL, "Load BMP file to Waveshare NFC ePaper.");
PrintAndLogEx(NORMAL, "Usage: hf waveshare loadbmp [h] f <filename[.bmp]> m <model_nr> [s]");
PrintAndLogEx(NORMAL, " Options :");
PrintAndLogEx(NORMAL, " f <fn> : " _YELLOW_("filename[.bmp]") " to upload to tag");
PrintAndLogEx(NORMAL, " m <nr> : " _YELLOW_("model number") " of your tag");
PrintAndLogEx(NORMAL, " s : save dithered version in filename-[n].bmp, only for RGB BMP");
for (uint8_t i = 0; i < MEND; i++) {
PrintAndLogEx(NORMAL, " m %2i : %-30s (%i*%i)", i, models[i].desc, models[i].width, models[i].height);
}
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, _YELLOW_(" hf waveshare loadbmp m 0 f myfile"));
PrintAndLogEx(NORMAL, "");
return PM3_SUCCESS;
}
static int picture_bit_depth(const uint8_t *bmp, const size_t bmpsize, const uint8_t model_nr) { static int picture_bit_depth(const uint8_t *bmp, const size_t bmpsize, const uint8_t model_nr) {
if (bmpsize < sizeof(BMP_HEADER)) if (bmpsize < sizeof(BMP_HEADER))
return PM3_ESOFT; return PM3_ESOFT;
@ -976,50 +960,59 @@ static int start_drawing(uint8_t model_nr, uint8_t *black, uint8_t *red) {
static int CmdHF14AWSLoadBmp(const char *Cmd) { static int CmdHF14AWSLoadBmp(const char *Cmd) {
char filename[FILE_PATH_SIZE] = {0}; char desc[800] = {0};
uint8_t cmdp = 0; for (uint8_t i = 0; i < MEND; i++) {
bool errors = false; snprintf(desc + strlen(desc),
size_t filenamelen = 0; sizeof(desc) - strlen(desc),
uint8_t model_nr = 0xff; "hf waveshare loadbmp -f myfile -m %2u -> %s ( %u, %u )\n",
bool save_conversions = false; i,
while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { models[i].desc,
switch (tolower(param_getchar(Cmd, cmdp))) { models[i].width,
case 'h': models[i].height
return usage_hf_waveshare_loadbmp(); );
case 'f':
filenamelen = param_getstr(Cmd, cmdp + 1, filename, FILE_PATH_SIZE);
if (filenamelen > FILE_PATH_SIZE - 5)
filenamelen = FILE_PATH_SIZE - 5;
cmdp += 2;
break;
case 'm':
model_nr = param_get8(Cmd, cmdp + 1);
cmdp += 2;
break;
case 's':
save_conversions = true;
cmdp += 1;
break;
default:
PrintAndLogEx(WARNING, "Unknown parameter: " _RED_("'%c'"), param_getchar(Cmd, cmdp));
errors = true;
break;
}
} }
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf waveshare loadbmp",
"Load BMP file to Waveshare NFC ePaper.",
desc
);
char modeldesc[40];
snprintf(modeldesc, sizeof(modeldesc), "model number [0 - %u] of your tag", MEND - 1);
void *argtable[] = {
arg_param_begin,
arg_int1("m", NULL, "<nr>", modeldesc),
arg_lit0("s", "save", "save dithered version in filename-[n].bmp, only for RGB BMP"),
arg_strx0("f", "file", "<filename>", "filename[.bmp] to upload to tag"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, false);
int model_nr = arg_get_int_def(ctx, 1, -1);
bool save_conversions = arg_get_lit(ctx, 2);
int fnlen = 0;
char filename[FILE_PATH_SIZE] = {0};
CLIParamStrToBuf(arg_get_str(ctx, 3), (uint8_t*)filename, FILE_PATH_SIZE, &fnlen);
CLIParserFree(ctx);
//Validations //Validations
if (filenamelen < 1) { if (fnlen < 1) {
PrintAndLogEx(WARNING, "Missing filename"); PrintAndLogEx(WARNING, "Missing filename");
errors = true; return PM3_EINVARG;
} }
if (model_nr == 0xff) { if (model_nr == -1) {
PrintAndLogEx(WARNING, "Missing model"); PrintAndLogEx(WARNING, "Missing model");
errors = true; return PM3_EINVARG;
} else if (model_nr >= MEND) {
PrintAndLogEx(WARNING, "Unknown model");
errors = true;
} }
if (errors || cmdp == 0) return usage_hf_waveshare_loadbmp(); if (model_nr >= MEND) {
PrintAndLogEx(WARNING, "Unknown model");
return PM3_EINVARG;
}
uint8_t *bmp = NULL; uint8_t *bmp = NULL;
uint8_t *black = NULL; uint8_t *black = NULL;