From e332353ccadd10fda96d2c02dddc64de0dbdea49 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Mon, 31 Mar 2008 18:42:22 +0200 Subject: * digital/avr/modules/host: - added client socket code. - added mex Node declarations. --- digital/avr/modules/host/Makefile.module | 2 +- digital/avr/modules/host/mex.h | 120 +++++++++++++++++++++++++++++++ digital/avr/modules/host/mex.host.c | 93 ++++++++++++++++++++++++ digital/avr/modules/host/socket.h | 32 +++++++++ digital/avr/modules/host/socket.host.c | 81 +++++++++++++++++++++ 5 files changed, 327 insertions(+), 1 deletion(-) create mode 100644 digital/avr/modules/host/mex.h create mode 100644 digital/avr/modules/host/mex.host.c create mode 100644 digital/avr/modules/host/socket.h create mode 100644 digital/avr/modules/host/socket.host.c (limited to 'digital/avr/modules') diff --git a/digital/avr/modules/host/Makefile.module b/digital/avr/modules/host/Makefile.module index cf2cf1ee..0b2cd371 100644 --- a/digital/avr/modules/host/Makefile.module +++ b/digital/avr/modules/host/Makefile.module @@ -1 +1 @@ -host_SOURCES = host.host.c +host_SOURCES = host.host.c mex.host.c socket.host.c diff --git a/digital/avr/modules/host/mex.h b/digital/avr/modules/host/mex.h new file mode 100644 index 00000000..a378496c --- /dev/null +++ b/digital/avr/modules/host/mex.h @@ -0,0 +1,120 @@ +#ifndef mex_h +#define mex_h +/* mex.h - mex library support. */ +/* avr.host - Host fonctions modules. {{{ + * + * Copyright (C) 2008 Nicolas Schodet + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * 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. + * + * }}} */ + +/** This module provides mex Node support. In each function, any error will + * stop the program (using assert). */ + +/** Define message type identifiers. */ +enum mex_mtype_t +{ + MEX_MTYPE_IDLE = 0, + MEX_MTYPE_DATE = 1, + MEX_MTYPE_REQ = 2, + MEX_MTYPE_RSP = 3, +}; + +/** Message structure opaque definition. */ +typedef struct mex_msg_t mex_msg_t; + +/** Message handler. */ +typedef void (mex_handler_t) (void *user, mex_msg_t *msg); + +/** Create a new message with the given type. */ +mex_msg_t * +mex_msg_new (u8 mtype); + +/** Release a message. */ +void +mex_msg_delete (mex_msg_t *msg); + +/** Add data to the message payload. + * + * The fmt string describes the provided data which must follow: + * - b: 8 bits. + * - h: 16 bits. + * - l: 32 bits. + * + * Uppercase is used for unsigned (but who cares?). */ +void +mex_msg_push (mex_msg_t *msg, const char *fmt, ...); + +/** Add data to the message payload, using a buffer. */ +void +mex_msg_push_buffer (mex_msg_t *msg, const u8 *buffer, int size); + +/** Get data from the message payload. + * + * The fmt string follows the same syntax as mex_msg_push, but pointers are + * provided as extra arguments. */ +void +mex_msg_pop (mex_msg_t *msg, const char *fmt, ...); + +/** Get remaining payload from the message. */ +const u8 * +mex_msg_pop_buffer (mex_msg_t *msg); + +/** Get payload remaining length. */ +int +mex_msg_len (mex_msg_t *msg); + +/** Connect to the mex Hub. */ +void +mex_node_connect (void); + +/** Close connection. */ +void +mex_node_close (void); + +/** Wait forever. */ +void +mex_node_wait (void); + +/** Wait until a date is reached. */ +void +mex_node_wait_date (u32 date); + +/** Return current date. */ +u32 +mex_node_date (void); + +/** Send a message. */ +void +mex_node_send (mex_msg_t *msg); + +/** Send a request and return response. */ +mex_msg_t * +mex_node_request (mex_msg_t *msg); + +/** Send a response to the currently serviced request. */ +void +mex_node_response (mex_msg_t *msg); + +/** Register a handler for the given message type. */ +void +mex_node_register (u8 mtype, mex_handler_t *handler, void *user); + +#endif /* mex_h */ diff --git a/digital/avr/modules/host/mex.host.c b/digital/avr/modules/host/mex.host.c new file mode 100644 index 00000000..7e9f4362 --- /dev/null +++ b/digital/avr/modules/host/mex.host.c @@ -0,0 +1,93 @@ +/* mex.host.c - mex library support. */ +/* avr.host - Host fonctions modules. {{{ + * + * Copyright (C) 2008 Nicolas Schodet + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * 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 "common.h" +#include "mex.h" +#include "socket.h" + +/** Message structure. + * + * Message buffer is allocated at message creation. Space is reserved at + * buffer start to allow encapsulation. + * + * A message can only be sent if header_tail == payload_head. */ +struct mex_msg_t +{ + /** Message type. */ + u8 mtype; + /** Allocated buffer. */ + u8 *buffer_head, *buffer_tail; + /** Header portion of the message. */ + u8 *header_head, *header_tail; + /** Payload portion of the message. */ + u8 *payload_head, *payload_tail; +}; + +/** Global context structure. */ +struct mex_node_t +{ + /** Connection with the Hub. */ + int socket; + /** Current date. */ + u32 date; + /** Current sequence number, used for Hub synchronisation. */ + u8 seq; + /** Currently handled request, or -1 if none. */ + int req; + /** Table of registered handlers. */ + mex_handler_t *handlers[256]; +}; + +/** Create a new message from a buffer. Buffer will be free'd on message + * deletion. */ +static mex_msg_t * +mex_msg_new_buffer (u8 *buffer, int size); + +/** Encapsulate a message and fill header. */ +static void +mex_msg_encapsulate (mex_msg_t *msg, const char *fmt, ...); + +/** Decapsulate a message, the current payload becomes the full message. */ +static void +mex_msg_decapsulate (mex_msg_t *msg); + +/** Receive one message. */ +static mex_msg_t * +mex_node_recv (void); + +/** Call the right handler for the given message. */ +static void +mex_node_dispatch (mex_msg_t *msg); + +/** Handle an incoming DATE. */ +static void +mex_node_handle_DATE (mex_msg_t *msg); + +/** Handle an incoming REQ. */ +static void +mex_node_handle_REQ (mex_msg_t *msg); + +/** Global context. */ +static struct mex_node_t mex_node_global; + diff --git a/digital/avr/modules/host/socket.h b/digital/avr/modules/host/socket.h new file mode 100644 index 00000000..8d387a35 --- /dev/null +++ b/digital/avr/modules/host/socket.h @@ -0,0 +1,32 @@ +#ifndef socket_h +#define socket_h +/* socket.h */ +/* avr.host - Host fonctions modules. {{{ + * + * Copyright (C) 2008 Nicolas Schodet + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * 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. + * + * }}} */ + +/** Create and connect a client socket. */ +int +socket_client (const char *addr, const char *port); + +#endif /* socket_h */ diff --git a/digital/avr/modules/host/socket.host.c b/digital/avr/modules/host/socket.host.c new file mode 100644 index 00000000..9953b3a9 --- /dev/null +++ b/digital/avr/modules/host/socket.host.c @@ -0,0 +1,81 @@ +/* socket.host.c */ +/* imported from mysock library and modified for avr modules. + * mysock library - client functions. {{{ + * + * Copyright (C) 2001 Nicolas Schodet + * + * 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. + * + * Contact : + * Web: http://ni.fr.eu.org/ + * + * }}} */ +#include "common.h" +#include "socket.h" +#include +#include +#include +#include +#include +#include +#include + +int +socket_client (const char *addr, const char *port) +{ + int s; + struct sockaddr_in saddr; + struct servent *port_info; + struct hostent *host_info; + unsigned short int iport; + unsigned int iaddr; + iport = htons (atoi (port)); + if (!iport) + { + port_info = getservbyname (port, "tcp"); + if (!port_info) + { + fprintf (stderr, "Service %s unknown\n", port); + exit (EXIT_FAILURE); + } + iport = port_info->s_port; + } + iaddr = inet_addr (addr); + if (iaddr == INADDR_NONE) + { + host_info = gethostbyname (addr); + if (!host_info) + { + fprintf (stderr, "Host %s unknown\n", addr); + exit (EXIT_FAILURE); + } + iaddr = * (int *) host_info->h_addr; + } + saddr.sin_family = AF_INET; + saddr.sin_addr.s_addr = iaddr; + saddr.sin_port = iport; + s = socket (AF_INET, SOCK_STREAM, 0); + if (s < 0) + { + perror ("socket"); + exit (EXIT_FAILURE); + } + if (connect (s, (struct sockaddr *) &saddr, sizeof (saddr)) < 0) + { + perror ("connect"); + exit (EXIT_FAILURE); + } + return s; +} -- cgit v1.2.3