ev3duder  0.3.0
EV3 Downloader/Uploader
tcp.c File Reference
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <netdb.h>
#include <fcntl.h>
#include <errno.h>
#include "btserial.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defs.h"
#include "tcp.h"

Go to the source code of this file.

Macros

#define INVALID_SOCKET   (-1)
 
#define SOCKET_ERROR   (-1)
 
#define bailout(msg)   perror((msg))
 
#define closesocket   close
 
#define socksetblock(sock, y_n)
 
#define UDP_PORT   3015
 default UDP broadcast port More...
 
#define TCP_PORT   5555
 
#define UDP_RECV_TIMEOUT   6
 
#define TCP_CONNECT_TIMEOUT   1
 

Typedefs

typedef int SOCKET
 BSD Sockets/Winsock2 I/O wrappers. More...
 

Functions

void * tcp_open (const char *serial, unsigned timeout)
 connects to a ev3 device on the same subnet. More...
 
void tcp_close (void *sock)
 releases the socket file descriptor opened by tcp_open() More...
 
const wchar_t * tcp_error (void *fd_)
 Returns an error string describing the last error occured. More...
 

Variables

int(* tcp_write )(void *device, const u8 *buf, size_t count) = bt_write
 writes buf[1] till buf[count - 2] to device More...
 
int(* tcp_read )(void *device, u8 *buf, size_t count, int milliseconds) = bt_read
 writes buf[1] till buf[count - 2] to device More...
 

Macro Definition Documentation

#define bailout (   msg)    perror((msg))

Definition at line 38 of file tcp.c.

#define closesocket   close

Definition at line 39 of file tcp.c.

#define INVALID_SOCKET   (-1)

Definition at line 36 of file tcp.c.

#define SOCKET_ERROR   (-1)

Definition at line 37 of file tcp.c.

#define socksetblock (   sock,
  y_n 
)
Value:
((y_n) ? \
fcntl((sock), F_SETFL, fcntl((sock), F_GETFL, 0) & ~O_NONBLOCK) : \
fcntl((sock), F_SETFL, O_NONBLOCK))

Definition at line 40 of file tcp.c.

#define TCP_CONNECT_TIMEOUT   1

Definition at line 68 of file tcp.c.

#define TCP_PORT   5555

Definition at line 65 of file tcp.c.

#define UDP_PORT   3015

default UDP broadcast port

Definition at line 64 of file tcp.c.

#define UDP_RECV_TIMEOUT   6

Definition at line 67 of file tcp.c.

Typedef Documentation

typedef int SOCKET

BSD Sockets/Winsock2 I/O wrappers.

Author
Ahmad Fatoum

Definition at line 35 of file tcp.c.

Function Documentation

void tcp_close ( void *  sock)

releases the socket file descriptor opened by tcp_open()

Closes the resource opened by bt_open.

Parameters
[in]devicehandle returned by tcp_open()

Definition at line 241 of file tcp.c.

const wchar_t* tcp_error ( void *  fd_)

Returns an error string describing the last error occured.

Parameters
[in]devicehandle returned by tcp_open()
Returns
message An error string
Bug:
it's useless. Could use wprintf and strerror

Definition at line 256 of file tcp.c.

void* tcp_open ( const char *  serial,
unsigned  timeout 
)

connects to a ev3 device on the same subnet.

open a bluetooth device described by device. NULL leads to default action

Parameters
[in]serialIP-Address or Serial-Number of Ev3. NULL connects to the first available
Returns
&fd pointer to file descriptor for use with tcp_{read,write,close,error} If the serial number contains a dot, a direct TCP connection is attempted. The Ev3 broadcasts a UDP-packet every 5 seconds. This functions waits 7 seconds for that to happen. It then compares the broadcasted serial number with the value set should serial be non-NULL. If serial is NULL or the serial numbers match, a udp packet is sent to the source port on the ev3. Afterwards the Ev3 starts listening on the port it broadcasted. Then a GET request is sent and the VM starts listening and business is as usual
See also
http://www.monobrick.dk/guides/how-to-establish-a-wifi-connection-with-the-ev3-brick/

Definition at line 84 of file tcp.c.

Variable Documentation

int(* tcp_read) (void *device, u8 *buf, size_t count, int milliseconds) = bt_read

writes buf[1] till buf[count - 2] to device

Parameters
[in]devicehandle returned by bt_open
[in]bufbuffer to write to
[in]countnumber of characters to be read
[in]millisecondsnumber of milliseconds to wait at maximum. -1 is indefinitely
Returns
status -1 on error. bytes read otherwise.
Bug:
the milliseconds part needs to be tested more throughly
See also
implementation at bt-win.c and bt-unix.c

Definition at line 272 of file tcp.c.

int(* tcp_write) (void *device, const u8 *buf, size_t count) = bt_write

writes buf[1] till buf[count - 2] to device

Parameters
[in]devicehandle returned by bt_open
[in]bufbyte string to write, the first byte is omitted
[in]countnumber of characters to be written (including leading ignored byte)
Returns
status -1 on error. bytes read otherwise.
Bug:
the first byte is omitted for compatiblity with the leading report byte demanded by hid_write. Wrapping HIDAPI could fix this.
See also
implementation at bt-win.c and bt-unix.c

Definition at line 271 of file tcp.c.