Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug #38

Merged
merged 7 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor debug prints into mfrc522_debug.c
  • Loading branch information
n1tram1 committed Apr 27, 2021
commit e0c0d8f9e77023918b915855dab8330781e39a6c
6 changes: 5 additions & 1 deletion module/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ endif
obj-$(RELEASE) += mfrc522.o
obj-$(TEST) += mfrc522_tests.o

mfrc522-objs += mfrc522_module.o mfrc522_parser.o mfrc522_user_command.o mfrc522_spi.o
mfrc522-objs += mfrc522_module.o \
mfrc522_parser.o \
mfrc522_user_command.o \
mfrc522_spi.o \
mfrc522_debug.o

MAKE = make -C ../linux/ M=$(PWD)

Expand Down
57 changes: 57 additions & 0 deletions module/mfrc522_debug.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <linux/kernel.h>

#include "mfrc522_debug.h"

static void __print_bytes(const char *bytes, int len)
{
int i;
char line[16];
int write_head = 0;

for (i = 1; i < len + 1; i++) {
write_head +=
snprintf(line + write_head, 16, "%02x", bytes[i - 1]);

if (i % 5 == 0) {
pr_info("%s\n", line);
write_head = 0;
} else {
write_head += snprintf(line + write_head, 16, " ");
}
}

if (write_head > 0)
pr_info("%s\n", line);
}

static void do_debug_read(const char *answer, int answer_size)
{
pr_info("RD\n");

__print_bytes(answer, answer_size);
}

static void do_debug_write(const char *cmd)
{
pr_info("WR\n");

__print_bytes(cmd, strlen(cmd));

}

void do_debug(const struct mfrc522_command *cmd, const char *answer,
int answer_size)
{
switch (cmd->cmd) {
case MFRC522_CMD_MEM_READ:
do_debug_read(answer, answer_size);
break;
case MFRC522_CMD_MEM_WRITE:
do_debug_write(cmd->data);
break;
default:
/* Do nothing */
break;
}
}

11 changes: 11 additions & 0 deletions module/mfrc522_debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* SPDX-License-Identifier: GPL-2.0 */

#ifndef MFRC522_DEBUG
#define MFRC522_DEBUG

#include "mfrc522_user_command.h"

void do_debug(const struct mfrc522_command *cmd, const char *answer,
int answer_size);

#endif /* ! MFRC522_DEBUG */
65 changes: 1 addition & 64 deletions module/mfrc522_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "mfrc522_user_command.h"
#include "mfrc522_parser.h"
#include "mfrc522_spi.h"
#include "mfrc522_debug.h"

#define MFRC522_VERSION_BASE 0x90
#define MFRC522_VERSION_1 0x91
Expand All @@ -24,70 +25,6 @@ MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("ks0n");
MODULE_DESCRIPTION("Driver for the MFRC522 RFID Chip");

static void do_debug_read(const char *answer, int answer_size)
{
int i;
char line[16];
int write_head = 0;

pr_info("RD\n");

for (i = 1; i < answer_size + 1; i++) {
write_head +=
snprintf(line + write_head, 16, "%02x", answer[i - 1]);

if (i % 5 == 0) {
pr_info("%s\n", line);
write_head = 0;
} else {
write_head += snprintf(line + write_head, 16, " ");
}
}

if (write_head > 0)
pr_info("%s\n", line);
}

static void do_debug_write(const char *cmd)
{
int i;
char line[16];
int write_head = 0;

pr_info("WR\n");

for (i = 1; cmd[i - 1]; i++) {
write_head +=
snprintf(line + write_head, 16, "%02x", cmd[i - 1]);

if (i % 5 == 0) {
pr_info("%s\n", line);
write_head = 0;
} else {
write_head += snprintf(line + write_head, 16, " ");
}
}

if (write_head > 0)
pr_info("%s\n", line);
}

static void do_debug(const struct mfrc522_command *cmd, const char *answer,
int answer_size)
{
switch (cmd->cmd) {
case MFRC522_CMD_MEM_READ:
do_debug_read(answer, answer_size);
break;
case MFRC522_CMD_MEM_WRITE:
do_debug_write(cmd->data);
break;
default:
/* Do nothing */
break;
}
}

static ssize_t __mfrc522_write(struct mfrc522_state *state, const char *buffer,
size_t len)
{
Expand Down