kb_socket.c File Reference

Include dependency graph for kb_socket.c:

Go to the source code of this file.

Functions

void DieWithError (char *errorMessage)
int ksock_init (char terminator, unsigned buffer_len)
int ksock_server_open (ksock_t *server, unsigned short port)
int ksock_next_connection (ksock_t *server)
int ksock_add_command (const char *name, int min_param, int max_param, int(*function)(int argc, char *argv[], void *data))
int ksock_remove_command (char *name)
void list_command ()
int ksock_send_command (int socket, char *cmd,...)
int ksock_send_answer (int *socketStorage, char *cmd)
int ksock_exec_command_pending (int clntSock, char *cmd)
int ksock_exec_command (char *cmd)
int ksock_get_command (int clntSocket, char *cmdBuffer, unsigned bufSize)
int ksock_connect (char *servIP, unsigned short servPort)
int ksock_server_close (ksock_t *server)

Variables

static char ksock_command_terminator = '\n'
static char * ksock_buf_snd = NULL
static unsigned ksock_buf_len = 0
static struct kb_command_sksock_cmd_list = NULL
unsigned ksock_cmd_n


Function Documentation

void DieWithError ( char *  errorMessage  ) 

int ksock_add_command ( const char *  name,
int  min_param,
int  max_param,
int(*)(int argc, char *argv[], void *data)  function 
)

Add a network command to the command list. The number of argument does include the name of the command itself. That means a command without args should have 1 as both min_param and max_param.

Returns:
  • KB_ERROR_NOINIT if the socket module is not initialize
  • 0 if successful

Definition at line 113 of file kb_socket.c.

References KB_ALLOC, KB_ERROR, KB_ERROR_NOINIT, KB_REALLOC, ksock_cmd_n, kb_command_s::maxParam, kb_command_s::minParam, kb_command_s::name, and kb_command_s::parse.

Referenced by main().

int ksock_connect ( char *  servIP,
unsigned short  servPort 
)

Try to connect to the given server

Parameters:
clntSocket The connection id
cmdBuffer The receive buffer allocated by the caller
bufSize The size of the receive buffer
Returns:
  • the socket descriptor

Definition at line 370 of file kb_socket.c.

References DieWithError(), and sock.

Referenced by main().

Here is the call graph for this function:

int ksock_exec_command ( char *  cmd  ) 

Execute a network command

Parameters:
cmd the command line
Returns:
  • the return value of the command if succesful
  • KB_ERROR_INVAL if the command is invalid
  • KB_ERROR_TOOMANYARGS if number of args is invalid
  • KB_ERROR_UNKCMD if the command is unknown

Definition at line 285 of file kb_socket.c.

References KB_ERROR, KB_ERROR_NOINIT, and kb_parse_command().

Referenced by main().

Here is the call graph for this function:

int ksock_exec_command_pending ( int  clntSock,
char *  cmd 
)

Send a network command through the given socket and wait for an acknowledge from the server. If the acknowledge is not received before the given timeout, the function returns with an error code.

Parameters:
socket the receiving socket
cmd the command line
Execute a network command that require an answer

The application should send the corresponding answer using ksock_send_answer.

Such network commands must use the following syntax: The fisrt word is the command name. The second word is the request id (unsigned long integer). The following words are the command parameters.

Parameters:
cmd the command line
clntSock the connection id
Returns:
  • the return value of the command if succesful
  • KB_ERROR_INVAL if the command is invalid
  • KB_ERROR_TOOMANYARGS if number of args is invalid
  • KB_ERROR_UNKCMD if the command is unknown

Definition at line 260 of file kb_socket.c.

References KB_ALLOC, KB_ERROR, KB_ERROR_NOINIT, and kb_parse_command().

Referenced by main().

Here is the call graph for this function:

int ksock_get_command ( int  clntSocket,
char *  cmdBuffer,
unsigned  bufSize 
)

Get a command from the given connection.

Parameters:
clntSocket The connection id
cmdBuffer The receive buffer allocated by the caller
bufSize The size of the receive buffer
Returns:
  • The received command length
  • 0 if the socket is non-blocking and no data is available
  • -1 if a full command is not available
  • -2 if a coherency problem is detected
  • -3 if the given client socket has been disconnected

Definition at line 308 of file kb_socket.c.

References DieWithError(), and ksock_command_terminator.

Referenced by main().

Here is the call graph for this function:

int ksock_init ( char  terminator,
unsigned  buffer_len 
)

Initialize the socket library

Parameters:
terminator The termination characters for network commands
buffer_len length of the buffer to send commands

Definition at line 31 of file kb_socket.c.

References KB_ALLOC, ksock_buf_len, ksock_buf_snd, ksock_cmd_n, ksock_command_terminator, kb_command_s::maxParam, kb_command_s::minParam, kb_command_s::name, and kb_command_s::parse.

Referenced by main().

int ksock_next_connection ( ksock_t server  ) 

Get the next connection to the given server

Definition at line 82 of file kb_socket.c.

References DieWithError(), KB_WARN_CONNECT, kb_warning(), and ksock_s::serv_socket.

Referenced by main().

Here is the call graph for this function:

int ksock_remove_command ( char *  name  ) 

Remove a network command from the command list

Definition at line 146 of file kb_socket.c.

References KB_ERROR, KB_ERROR_NOINIT, kb_find_command(), ksock_cmd_n, kb_command_s::maxParam, kb_command_s::minParam, kb_command_s::name, and kb_command_s::parse.

Here is the call graph for this function:

int ksock_send_answer ( int *  socketStorage,
char *  cmd 
)

Send a network answer through the given socket this function should be called from a command function to send the answer back after a ksock_exec_command_pending.

Parameters:
socket the receiving socket
cmd the command line

Definition at line 216 of file kb_socket.c.

References DieWithError(), kb_free, ksock_buf_len, ksock_buf_snd, and ksock_command_terminator.

Referenced by kmot_ipConfigPID(), kmot_ipFirmware(), kmot_ipInitMotor(), kmot_ipMeasure(), kmot_ipResetError(), kmot_ipSetPointSource(), kmot_ipSetPos(), kmot_ipSetSpeed(), and kmot_ipStatus().

Here is the call graph for this function:

int ksock_send_command ( int  socket,
char *  cmd,
  ... 
)

Send a network command through the given socket

Parameters:
socket the receiving socket
cmd the command line

Definition at line 186 of file kb_socket.c.

References DieWithError(), ksock_buf_len, ksock_buf_snd, and ksock_command_terminator.

Referenced by handle_joyaxis(), and handle_joybutton().

Here is the call graph for this function:

int ksock_server_close ( ksock_t server  ) 

Definition at line 392 of file kb_socket.c.

int ksock_server_open ( ksock_t server,
unsigned short  port 
)

Open a server socket

Definition at line 54 of file kb_socket.c.

References DieWithError(), MAXPENDING, ksock_s::serv_addr, and ksock_s::serv_socket.

Referenced by main().

Here is the call graph for this function:

void list_command (  ) 

Definition at line 171 of file kb_socket.c.

References kb_command_s::maxParam, kb_command_s::minParam, and kb_command_s::name.

Referenced by main().


Variable Documentation

unsigned ksock_buf_len = 0 [static]

Definition at line 15 of file kb_socket.c.

Referenced by ksock_init(), ksock_send_answer(), and ksock_send_command().

char* ksock_buf_snd = NULL [static]

Definition at line 14 of file kb_socket.c.

Referenced by ksock_init(), ksock_send_answer(), and ksock_send_command().

struct kb_command_s* ksock_cmd_list = NULL [static]

Definition at line 17 of file kb_socket.c.

unsigned ksock_cmd_n

Definition at line 18 of file kb_socket.c.

Referenced by ksock_add_command(), ksock_init(), and ksock_remove_command().

char ksock_command_terminator = '\n' [static]

Definition at line 13 of file kb_socket.c.

Referenced by ksock_get_command(), ksock_init(), ksock_send_answer(), and ksock_send_command().


Generated on Wed Dec 15 11:49:28 2010 for KoreBot Library by  doxygen 1.5.5