/* * ucon.c * * Copyright (c) 2004 Evgeniy Polyakov * * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "connector.h" #include "../soekris/sc_conn.h" static int need_exit; __u32 seq; static int netlink_send(int s, char *data) { struct nlmsghdr *nlh; unsigned int size; int err; char buf[128]; struct cn_msg *m, *msg; struct sc_conn_data *cmd; size = NLMSG_SPACE(sizeof(struct cn_msg) + sizeof(struct sc_conn_data)); nlh = (struct nlmsghdr *)buf; nlh->nlmsg_seq = seq++; nlh->nlmsg_pid = getpid(); nlh->nlmsg_type = NLMSG_DONE; nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh)); nlh->nlmsg_flags = 0; m = NLMSG_DATA(nlh); msg = (struct cn_msg *)data; cmd = (struct sc_conn_data *)(msg + 1); printf("%s: len=%u, seq=%u, ack=%u, " "sname=%s, lname=%s, idx=0x%x, cmd=%02x [%02x.%02x.%02x].\n", __func__, msg->len, msg->seq, msg->ack, cmd->sname, cmd->lname, cmd->idx, cmd->cmd, cmd->p0, cmd->p1, cmd->p2); memcpy(m, data, sizeof(*m) + msg->len); err = send(s, nlh, size, 0); if (err == -1) fprintf(stderr, "Failed to send: %s [%d].\n", strerror(errno), errno); return err; } int main(int argc, char *argv[]) { int s; char buf[1024]; int len; struct nlmsghdr *reply; struct sockaddr_nl l_local; struct cn_msg *data; struct sc_conn_data *m; FILE *out; time_t tm; struct pollfd pfd; if (argc < 2) out = stdout; else { out = fopen(argv[1], "a+"); if (!out) { fprintf(stderr, "Unable to open %s for writing: %s\n", argv[1], strerror(errno)); out = stdout; } } memset(buf, 0, sizeof(buf)); s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_NFLOG); if (s == -1) { perror("socket"); return -1; } l_local.nl_family = AF_NETLINK; l_local.nl_groups = 1; l_local.nl_pid = getpid(); if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) { perror("bind"); close(s); return -1; } pfd.fd = s; while (!need_exit) { pfd.events = POLLIN; pfd.revents = 0; /*switch (poll(&pfd, 1, -1)) { case 0: need_exit = 1; break; case -1: if (errno != EINTR) { need_exit = 1; break; } continue; } */ if (need_exit) break; data = (struct cn_msg *)buf; data->id.idx = 0xaabb; data->id.val = 0xccdd; data->seq = seq++; data->ack = (seq ^ 0x1234); data->len = sizeof(*m); m = (struct sc_conn_data *)(data + 1); memset(m, 0, sizeof(*m)); m->cmd = SC_CMD_LDEV_READ; m->idx = 0xff; sprintf(m->sname, "PC8736X"); sprintf(m->lname, "GPIO"); m->p0 = 21; len = netlink_send(s, buf); len = recv(s, buf, sizeof(buf), 0); if (len == -1) { perror("recv buf"); close(s); return -1; } reply = (struct nlmsghdr *)buf; switch (reply->nlmsg_type) { case NLMSG_ERROR: fprintf(out, "Error message received.\n"); fflush(out); break; case NLMSG_DONE: data = (struct cn_msg *)NLMSG_DATA(reply); m = (struct sc_conn_data *)(data + 1); time(&tm); fprintf(out, "%.24s : [%x.%x] [seq=%u, ack=%u], sname=%s, lname=%s, idx=%u, cmd=%#02x [%#02x.%#02x.%#02x]\n", ctime(&tm), data->id.idx, data->id.val, data->seq, data->ack, m->sname, m->lname, m->idx, m->cmd, m->p0, m->p1, m->p2); fflush(out); break; default: break; } sleep(1); } close(s); return 0; }