usb: native_posix: Move to new logging subsystem

Use new logger log method.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
This commit is contained in:
Andrei Emeltchenko 2018-09-14 09:19:47 +03:00 committed by Anas Nashif
commit 80120871ed
2 changed files with 85 additions and 115 deletions

View file

@ -18,9 +18,9 @@
#include "usb_dc_native_posix_adapt.h"
#define SYS_LOG_LEVEL CONFIG_SYS_LOG_USB_DRIVER_LEVEL
#define SYS_LOG_DOMAIN "usb/native_posix"
#include <logging/sys_log.h>
#define LOG_LEVEL CONFIG_USB_DRIVER_LOG_LEVEL
#include <logging/log.h>
LOG_MODULE_REGISTER(native_posix);
/* convert from endpoint address to hardware endpoint index */
#define USBIP_EP_ADDR2IDX(ep) ((ep) & ~USB_EP_DIR_MASK)
@ -39,7 +39,7 @@ static struct k_thread thread;
static void thread_main(void *a, void *b, void *c)
{
SYS_LOG_DBG("");
LOG_DBG("");
usbip_start();
}
@ -89,7 +89,7 @@ static u8_t usbip_ep_is_enabled(u8_t ep)
{
u8_t ep_idx = USBIP_EP_ADDR2IDX(ep);
SYS_LOG_DBG("ep %x", ep);
LOG_DBG("ep %x", ep);
/* Check if ep enabled */
if ((USBIP_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) &&
@ -105,10 +105,10 @@ static u8_t usbip_ep_is_enabled(u8_t ep)
int usb_dc_attach(void)
{
SYS_LOG_DBG("");
LOG_DBG("");
if (usbip_ctrl.attached) {
SYS_LOG_WRN("Already attached");
LOG_WRN("Already attached");
return 0;
}
@ -124,7 +124,7 @@ int usb_dc_attach(void)
int usb_dc_detach(void)
{
SYS_LOG_DBG("");
LOG_DBG("");
if (!usbip_ctrl.attached) {
return 0;
@ -137,7 +137,7 @@ int usb_dc_detach(void)
int usb_dc_reset(void)
{
SYS_LOG_DBG("");
LOG_DBG("");
/* Clear private data */
memset(&usbip_ctrl, 0, sizeof(usbip_ctrl));
@ -147,7 +147,7 @@ int usb_dc_reset(void)
int usb_dc_set_address(const u8_t addr)
{
SYS_LOG_DBG("");
LOG_DBG("");
return 0;
}
@ -156,28 +156,28 @@ int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data * const cfg)
{
u8_t ep_idx = USBIP_EP_ADDR2IDX(cfg->ep_addr);
SYS_LOG_DBG("ep %x, mps %d, type %d", cfg->ep_addr, cfg->ep_mps,
cfg->ep_type);
LOG_DBG("ep %x, mps %d, type %d", cfg->ep_addr, cfg->ep_mps,
cfg->ep_type);
if ((cfg->ep_type == USB_DC_EP_CONTROL) && ep_idx) {
SYS_LOG_ERR("invalid endpoint configuration");
LOG_ERR("invalid endpoint configuration");
return -1;
}
if (cfg->ep_mps > USBIP_MAX_PACKET_SIZE) {
SYS_LOG_WRN("unsupported packet size");
LOG_WRN("unsupported packet size");
return -1;
}
if ((USBIP_EP_ADDR2DIR(cfg->ep_addr) == USB_EP_DIR_OUT) &&
(ep_idx >= USBIP_OUT_EP_NUM)) {
SYS_LOG_WRN("OUT endpoint address out of range");
LOG_WRN("OUT endpoint address out of range");
return -1;
}
if ((USBIP_EP_ADDR2DIR(cfg->ep_addr) == USB_EP_DIR_IN) &&
(ep_idx >= USBIP_IN_EP_NUM)) {
SYS_LOG_WRN("IN endpoint address out of range");
LOG_WRN("IN endpoint address out of range");
return -1;
}
@ -186,8 +186,8 @@ int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data * const cfg)
int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data * const cfg)
{
SYS_LOG_DBG("ep %x, mps %d, type %d", cfg->ep_addr, cfg->ep_mps,
cfg->ep_type);
LOG_DBG("ep %x, mps %d, type %d", cfg->ep_addr, cfg->ep_mps,
cfg->ep_type);
if (!usbip_ctrl.attached && !usbip_ep_is_valid(cfg->ep_addr)) {
return -EINVAL;
@ -198,7 +198,7 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data * const cfg)
int usb_dc_ep_set_stall(const u8_t ep)
{
SYS_LOG_DBG("ep %x", ep);
LOG_DBG("ep %x", ep);
if (!usbip_ctrl.attached && !usbip_ep_is_valid(ep)) {
return -EINVAL;
@ -214,7 +214,7 @@ int usb_dc_ep_clear_stall(const u8_t ep)
{
u8_t ep_idx = USBIP_EP_ADDR2IDX(ep);
SYS_LOG_DBG("ep %x", ep);
LOG_DBG("ep %x", ep);
if (!usbip_ctrl.attached && !usbip_ep_is_valid(ep)) {
return -EINVAL;
@ -232,7 +232,7 @@ int usb_dc_ep_halt(const u8_t ep)
{
u8_t ep_idx = USBIP_EP_ADDR2IDX(ep);
SYS_LOG_DBG("ep %x", ep);
LOG_DBG("ep %x", ep);
if (!usbip_ctrl.attached && !usbip_ep_is_valid(ep)) {
return -EINVAL;
@ -248,7 +248,7 @@ int usb_dc_ep_halt(const u8_t ep)
int usb_dc_ep_is_stalled(const u8_t ep, u8_t *const stalled)
{
SYS_LOG_DBG("ep %x", ep);
LOG_DBG("ep %x", ep);
if (!usbip_ctrl.attached && !usbip_ep_is_valid(ep)) {
return -EINVAL;
@ -265,7 +265,7 @@ int usb_dc_ep_enable(const u8_t ep)
{
u8_t ep_idx = USBIP_EP_ADDR2IDX(ep);
SYS_LOG_DBG("ep %x", ep);
LOG_DBG("ep %x", ep);
if (!usbip_ctrl.attached && !usbip_ep_is_valid(ep)) {
return -EINVAL;
@ -283,14 +283,14 @@ int usb_dc_ep_enable(const u8_t ep)
int usb_dc_ep_disable(const u8_t ep)
{
SYS_LOG_DBG("ep %x", ep);
LOG_DBG("ep %x", ep);
return 0;
}
int usb_dc_ep_flush(const u8_t ep)
{
SYS_LOG_DBG("ep %x", ep);
LOG_DBG("ep %x", ep);
if (!usbip_ctrl.attached && !usbip_ep_is_valid(ep)) {
return -EINVAL;
@ -307,7 +307,7 @@ int usb_dc_ep_flush(const u8_t ep)
int usb_dc_ep_write(const u8_t ep, const u8_t *const data,
const u32_t data_len, u32_t * const ret_bytes)
{
SYS_LOG_DBG("ep %x len %u", ep, data_len);
LOG_DBG("ep %x len %u", ep, data_len);
if (!usbip_ctrl.attached && !usbip_ep_is_valid(ep)) {
return -EINVAL;
@ -320,6 +320,7 @@ int usb_dc_ep_write(const u8_t ep, const u8_t *const data,
/* Check if ep enabled */
if (!usbip_ep_is_enabled(ep)) {
LOG_WRN("ep %x disabled", ep);
return -EINVAL;
}
@ -343,29 +344,29 @@ int usb_dc_ep_read_wait(u8_t ep, u8_t *data, u32_t max_data_len,
u32_t bytes;
if (!usbip_ctrl.attached && !usbip_ep_is_valid(ep)) {
SYS_LOG_ERR("No valid endpoint");
LOG_ERR("No valid endpoint");
return -EINVAL;
}
/* Check if OUT ep */
if (USBIP_EP_ADDR2DIR(ep) != USB_EP_DIR_OUT) {
SYS_LOG_ERR("Wrong endpoint direction");
LOG_ERR("Wrong endpoint direction");
return -EINVAL;
}
/* Allow to read 0 bytes */
if (!data && max_data_len) {
SYS_LOG_ERR("Wrong arguments");
LOG_ERR("Wrong arguments");
return -EINVAL;
}
/* Check if ep enabled */
if (!usbip_ep_is_enabled(ep)) {
SYS_LOG_ERR("Not enabled endpoint");
LOG_ERR("Not enabled endpoint");
return -EINVAL;
}
SYS_LOG_DBG("ep %x max_data_len %u", ep, max_data_len);
LOG_DBG("ep %x max_data_len %u", ep, max_data_len);
bytes = usbip_recv(data, max_data_len);
@ -381,13 +382,13 @@ int usb_dc_ep_read_continue(u8_t ep)
u8_t ep_idx = USBIP_EP_ADDR2IDX(ep);
if (!usbip_ctrl.attached && !usbip_ep_is_valid(ep)) {
SYS_LOG_ERR("No valid endpoint");
LOG_ERR("No valid endpoint");
return -EINVAL;
}
/* Check if OUT ep */
if (USBIP_EP_ADDR2DIR(ep) != USB_EP_DIR_OUT) {
SYS_LOG_ERR("Wrong endpoint direction");
LOG_ERR("Wrong endpoint direction");
return -EINVAL;
}
@ -402,7 +403,7 @@ int usb_dc_ep_read_continue(u8_t ep)
int usb_dc_ep_read(const u8_t ep, u8_t *const data,
const u32_t max_data_len, u32_t * const read_bytes)
{
SYS_LOG_DBG("ep %x max_data_len %u", ep, max_data_len);
LOG_DBG("ep %x max_data_len %u", ep, max_data_len);
if (usb_dc_ep_read_wait(ep, data, max_data_len, read_bytes) != 0) {
return -EINVAL;
@ -426,7 +427,7 @@ int usb_dc_ep_set_callback(const u8_t ep, const usb_dc_ep_callback cb)
{
u8_t ep_idx = USBIP_EP_ADDR2IDX(ep);
SYS_LOG_DBG("ep %x callback %p", ep, cb);
LOG_DBG("ep %x callback %p", ep, cb);
if (!usbip_ctrl.attached && !usbip_ep_is_valid(ep)) {
return -EINVAL;
@ -464,10 +465,10 @@ int handle_usb_control(struct usbip_header *hdr)
u8_t ep_idx = USBIP_EP_ADDR2IDX(ntohl(hdr->common.ep));
usb_dc_ep_callback ep_cb = usbip_ctrl.out_ep_ctrl[ep_idx].cb;
SYS_LOG_DBG("ep %x idx %u", ntohl(hdr->common.ep), ep_idx);
LOG_DBG("ep %x idx %u", ntohl(hdr->common.ep), ep_idx);
if (ep_cb) {
SYS_LOG_DBG("Call ep_cb");
LOG_DBG("Call ep_cb");
ep_cb(hdr->common.ep, USB_DC_EP_SETUP);
}

View file

@ -27,14 +27,14 @@
#include <kernel.h>
#include <usb/usb_common.h>
#include <usb/usbstruct.h>
#define SYS_LOG_LEVEL CONFIG_SYS_LOG_USB_DRIVER_LEVEL
#define SYS_LOG_DOMAIN "usb/native_posix_adapt"
#include <logging/sys_log.h>
#include <usb/usb_device.h>
#include <posix_board_if.h>
#include "usb_dc_native_posix_adapt.h"
#define LOG_LEVEL CONFIG_USB_DRIVER_LOG_LEVEL
LOG_MODULE_REGISTER(native_posix_adapt);
#define USBIP_PORT 3240
#define USBIP_VERSION 273
@ -47,59 +47,28 @@ int devid_global;
/* Helpers */
#ifdef VERBOSE_DEBUG
static void hexdump(const u8_t *data, size_t len)
{
int n = 0;
while (len--) {
if (n % 16 == 0) {
printf("%08X ", n);
}
printf("%02X ", *data++);
n++;
if (n % 8 == 0) {
if (n % 16 == 0) {
printf("\n");
} else {
printf(" ");
}
}
}
if (n % 16) {
printf("\n");
}
}
static void usbip_header_dump(struct usbip_header *hdr)
{
printf("USBIP header dump:\n");
printf("\tcommand %u\n", ntohl(hdr->common.command));
printf("\tseqnum %u\n", ntohl(hdr->common.seqnum));
printf("\tdevid %x\n", ntohl(hdr->common.devid));
printf("\tdirection %u\n", ntohl(hdr->common.direction));
printf("\tep %x\n", ntohl(hdr->common.ep));
LOG_DBG("cmd %x seq %u dir %u ep %x", ntohl(hdr->common.command),
ntohl(hdr->common.seqnum), ntohl(hdr->common.direction),
ntohl(hdr->common.ep));
switch (ntohl(hdr->common.command)) {
case USBIP_CMD_SUBMIT:
printf("\tflags %x\n", ntohl(hdr->u.submit.transfer_flags));
printf("\tnumber of packets %u\n",
ntohl(hdr->u.submit.number_of_packets));
printf("\tinterval %u\n", ntohl(hdr->u.submit.interval));
printf("\tbuffer length %u\n",
ntohl(hdr->u.submit.transfer_buffer_length));
LOG_DBG("flags %x np %u int %u buflen %u",
ntohl(hdr->u.submit.transfer_flags),
ntohl(hdr->u.submit.number_of_packets),
ntohl(hdr->u.submit.interval),
ntohl(hdr->u.submit.transfer_buffer_length));
break;
case USBIP_CMD_UNLINK:
printf("\tseqnum %d\n", ntohl(hdr->u.unlink.seqnum));
LOG_DBG("seq %d", ntohl(hdr->u.unlink.seqnum));
break;
default:
break;
}
}
#else
#define hexdump(x, y)
#define usbip_header_dump(x)
#endif
@ -107,7 +76,7 @@ void get_interface(u8_t *descriptors)
{
while (descriptors[0]) {
if (descriptors[1] == DESC_INTERFACE) {
SYS_LOG_DBG("interface found");
LOG_DBG("interface found");
}
/* skip to next descriptor */
@ -135,8 +104,7 @@ static int send_interfaces(const u8_t *descriptors, int connfd)
if (send(connfd, &iface, sizeof(iface), 0) !=
sizeof(iface)) {
SYS_LOG_ERR("send() failed: %s",
strerror(errno));
LOG_ERR("send() failed: %s", strerror(errno));
return errno;
}
}
@ -182,7 +150,7 @@ static int send_device(const u8_t *desc, int connfd)
fill_device(&dev, desc);
if (send(connfd, &dev, sizeof(dev), 0) != sizeof(dev)) {
SYS_LOG_ERR("send() device failed: %s", strerror(errno));
LOG_ERR("send() device failed: %s", strerror(errno));
return errno;
}
@ -197,10 +165,10 @@ static int handle_device_list(const u8_t *desc, int connfd)
.status = 0,
};
SYS_LOG_DBG("desc %p", desc);
LOG_DBG("desc %p", desc);
if (send(connfd, &header, sizeof(header), 0) != sizeof(header)) {
SYS_LOG_ERR("send() header failed: %s", strerror(errno));
LOG_ERR("send() header failed: %s", strerror(errno));
return errno;
}
@ -208,7 +176,7 @@ static int handle_device_list(const u8_t *desc, int connfd)
u32_t ndev = htonl(1);
if (send(connfd, &ndev, sizeof(ndev), 0) != sizeof(ndev)) {
SYS_LOG_ERR("send() ndev failed: %s", strerror(errno));
LOG_ERR("send() ndev failed: %s", strerror(errno));
return errno;
}
@ -224,11 +192,11 @@ static void handle_usbip_submit(int connfd, struct usbip_header *hdr)
struct usbip_submit *req = &hdr->u.submit;
int read;
SYS_LOG_DBG("");
LOG_DBG("");
read = recv(connfd, req, sizeof(*req), 0);
if (read != sizeof(*req)) {
SYS_LOG_ERR("recv() failed: %s", strerror(errno));
LOG_ERR("recv() failed: %s", strerror(errno));
return;
}
@ -247,18 +215,18 @@ static void handle_usbip_unlink(int connfd, struct usbip_header *hdr)
u64_t setup_padding;
int read;
SYS_LOG_DBG("");
LOG_DBG("");
read = recv(connfd, req, sizeof(hdr->u), 0);
if (read != sizeof(hdr->u)) {
SYS_LOG_ERR("recv() failed: %s", strerror(errno));
LOG_ERR("recv() failed: %s", strerror(errno));
return;
}
/* Read also padding */
read = recv(connfd, &setup_padding, sizeof(setup_padding), 0);
if (read != sizeof(setup_padding)) {
SYS_LOG_ERR("recv() failed: %s", strerror(errno));
LOG_ERR("recv() failed: %s", strerror(errno));
return;
}
@ -276,15 +244,15 @@ static int handle_import(const u8_t *desc, int connfd)
};
char busid[32];
SYS_LOG_DBG("attach device");
LOG_DBG("attach device");
if (recv(connfd, busid, 32, 0) != sizeof(busid)) {
SYS_LOG_ERR("recv() failed: %s", strerror(errno));
LOG_ERR("recv() failed: %s", strerror(errno));
return errno;
}
if (send(connfd, &header, sizeof(header), 0) != sizeof(header)) {
SYS_LOG_ERR("send() header failed: %s", strerror(errno));
LOG_ERR("send() header failed: %s", strerror(errno));
return errno;
}
@ -303,7 +271,7 @@ void usbip_start(void)
const u8_t *desc;
int reuse = 1;
SYS_LOG_DBG("Starting");
LOG_DBG("Starting");
/*
* Do not use usb_get_device_descriptor();
@ -311,19 +279,19 @@ void usbip_start(void)
*/
desc = (const u8_t *)__usb_descriptor_start;
if (!desc) {
SYS_LOG_ERR("Descriptors are not set");
LOG_ERR("Descriptors are not set");
posix_exit(EXIT_FAILURE);
}
listenfd = socket(PF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (listenfd < 0) {
SYS_LOG_ERR("socket() failed: %s", strerror(errno));
LOG_ERR("socket() failed: %s", strerror(errno));
posix_exit(EXIT_FAILURE);
}
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,
(const char *)&reuse, sizeof(reuse)) < 0) {
SYS_LOG_WRN("setsockopt() failed: %s", strerror(errno));
LOG_WRN("setsockopt() failed: %s", strerror(errno));
}
memset(&srv, 0, sizeof(srv));
@ -332,12 +300,12 @@ void usbip_start(void)
srv.sin_port = htons(USBIP_PORT);
if (bind(listenfd, (struct sockaddr *)&srv, sizeof(srv)) < 0) {
SYS_LOG_ERR("bind() failed: %s", strerror(errno));
LOG_ERR("bind() failed: %s", strerror(errno));
posix_exit(EXIT_FAILURE);
}
if (listen(listenfd, SOMAXCONN) < 0) {
SYS_LOG_ERR("listen() failed: %s", strerror(errno));
LOG_ERR("listen() failed: %s", strerror(errno));
posix_exit(EXIT_FAILURE);
}
@ -355,13 +323,13 @@ void usbip_start(void)
continue;
}
SYS_LOG_ERR("accept() failed: %s", strerror(errno));
LOG_ERR("accept() failed: %s", strerror(errno));
posix_exit(EXIT_FAILURE);
}
connfd_global = connfd;
SYS_LOG_DBG("Connection: %s", inet_ntoa(client_addr.sin_addr));
LOG_DBG("Connection: %s", inet_ntoa(client_addr.sin_addr));
/* Set attached 0 */
attached = 0;
@ -386,15 +354,16 @@ void usbip_start(void)
}
if (read != sizeof(req)) {
SYS_LOG_WRN("wrong length, %d", read);
LOG_WRN("wrong length, %d", read);
/* Closing connection */
break;
}
hexdump((u8_t *)&req, sizeof(req));
LOG_HEXDUMP_DBG((u8_t *)&req, sizeof(req),
"Got request");
SYS_LOG_DBG("Code: 0x%x", ntohs(req.code));
LOG_DBG("Code: 0x%x", ntohs(req.code));
switch (ntohs(req.code)) {
case OP_REQ_DEVLIST:
@ -406,8 +375,8 @@ void usbip_start(void)
}
break;
default:
SYS_LOG_ERR("Unhandled code: 0x%x",
ntohs(req.code));
LOG_ERR("Unhandled code: 0x%x",
ntohs(req.code));
break;
}
@ -426,15 +395,15 @@ void usbip_start(void)
}
}
LOG_HEXDUMP_DBG((u8_t *)hdr, read, "Got cmd");
if (read != sizeof(*hdr)) {
SYS_LOG_ERR("recv wrong length: %d", read);
LOG_ERR("recv wrong length: %d", read);
/* Closing connection */
break;
}
hexdump((u8_t *)hdr, sizeof(*hdr));
devid_global = ntohl(hdr->devid);
seqnum_global = ntohl(hdr->seqnum);
@ -446,14 +415,14 @@ void usbip_start(void)
handle_usbip_unlink(connfd, &cmd);
break;
default:
SYS_LOG_ERR("Unknown command: 0x%x",
ntohl(hdr->command));
LOG_ERR("Unknown command: 0x%x",
ntohl(hdr->command));
close(connfd);
return;
}
}
SYS_LOG_DBG("Closing connection");
LOG_DBG("Closing connection");
close(connfd);
}
}