1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2018 iceman <iceman at iuse.se>
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
10 #include "cmdwiegand.h"
15 #include "cmdparser.h" // command_t
16 #include "cliparser.h"
19 #include "protocols.h"
20 #include "parity.h" // oddparity
21 #include "cmdhflist.h" // annotations
22 #include "wiegand_formats.h"
23 #include "wiegand_formatutils.h"
26 static int CmdHelp(const char *Cmd
);
28 int CmdWiegandList(const char *Cmd
) {
30 CLIParserContext
*ctx
;
31 CLIParserInit(&ctx
, "wiegand info",
32 "List available wiegand formats",
40 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
47 int CmdWiegandEncode(const char *Cmd
) {
49 CLIParserContext
*ctx
;
50 CLIParserInit(&ctx
, "wiegand encode",
51 "Encode wiegand formatted number to raw hex",
52 "wiegand encode --fc 101 --cn 1337 -> show all formats\n"
53 "wiegand encode -w H10301 --fc 101 --cn 1337 -> H10301 format "
58 arg_u64_0(NULL
, "fc", "<dec>", "facility number"),
59 arg_u64_1(NULL
, "cn", "<dec>", "card number"),
60 arg_u64_0(NULL
, "issue", "<dec>", "issue level"),
61 arg_u64_0(NULL
, "oem", "<dec>", "OEM code"),
62 arg_str0("w", "wiegand", "<format>", "see `wiegand list` for available formats"),
63 arg_lit0(NULL
, "pre", "add HID ProxII preamble to wiegand output"),
66 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
69 memset(&data
, 0, sizeof(wiegand_card_t
));
71 data
.FacilityCode
= arg_get_u32_def(ctx
, 1, 0);
72 data
.CardNumber
= arg_get_u64_def(ctx
, 2, 0);
73 data
.IssueLevel
= arg_get_u32_def(ctx
, 3, 0);
74 data
.OEM
= arg_get_u32_def(ctx
, 4, 0);
77 char format
[16] = {0};
78 CLIParamStrToBuf(arg_get_str(ctx
, 5), (uint8_t *)format
, sizeof(format
), &len
);
79 bool preamble
= arg_get_lit(ctx
, 6);
84 idx
= HIDFindCardFormat(format
);
86 PrintAndLogEx(WARNING
, "Unknown format: %s", format
);
92 wiegand_message_t packed
;
93 memset(&packed
, 0, sizeof(wiegand_message_t
));
94 if (HIDPack(idx
, &data
, &packed
, preamble
) == false) {
95 PrintAndLogEx(WARNING
, "The card data could not be encoded in the selected format.");
98 print_wiegand_code(&packed
);
100 // try all formats and print only the ones that work.
101 HIDPackTryAll(&data
, preamble
);
106 int CmdWiegandDecode(const char *Cmd
) {
108 CLIParserContext
*ctx
;
109 CLIParserInit(&ctx
, "wiegand decode",
110 "Decode raw hex or binary to wiegand format",
111 "wiegand decode --raw 2006f623ae"
116 arg_strx0("r", "raw", "<hex>", "raw hex to be decoded"),
117 arg_str0("b", "bin", "<bin>", "binary string to be decoded"),
120 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
123 CLIParamStrToBuf(arg_get_str(ctx
, 1), (uint8_t *)hex
, sizeof(hex
), &hlen
);
126 uint8_t binarr
[100] = {0x00};
127 int res
= CLIParamBinToBuf(arg_get_str(ctx
, 2), binarr
, sizeof(binarr
), &blen
);
131 PrintAndLogEx(FAILED
, "Error parsing binary string");
135 uint32_t top
= 0, mid
= 0, bot
= 0;
138 res
= hexstring_to_u96(&top
, &mid
, &bot
, hex
);
140 PrintAndLogEx(ERR
, "hex string contains none hex chars");
144 int n
= binarray_to_u96(&top
, &mid
, &bot
, binarr
, blen
);
146 PrintAndLogEx(ERR
, "Binary string contains none <0|1> chars");
150 PrintAndLogEx(ERR
, "empty input");
153 wiegand_message_t packed
= initialize_message_object(top
, mid
, bot
, blen
);
154 HIDTryUnpack(&packed
);
158 static command_t CommandTable
[] = {
159 {"help", CmdHelp
, AlwaysAvailable
, "This help"},
160 {"list", CmdWiegandList
, AlwaysAvailable
, "List available wiegand formats"},
161 {"encode", CmdWiegandEncode
, AlwaysAvailable
, "Encode to wiegand raw hex (currently for HID Prox)"},
162 {"decode", CmdWiegandDecode
, AlwaysAvailable
, "Convert raw hex to decoded wiegand format (currently for HID Prox)"},
163 {NULL
, NULL
, NULL
, NULL
}
166 static int CmdHelp(const char *Cmd
) {
167 (void)Cmd
; // Cmd is not used so far
168 CmdsHelp(CommandTable
);
172 int CmdWiegand(const char *Cmd
) {
173 clearCommandBuffer();
174 return CmdsParse(CommandTable
, Cmd
);