Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

utils.h File Reference


Detailed Description

Useful utility routines.

 *
 * $Log$
 *
 * 

Definition in file utils.h.

#include <sys/socket.h>

Include dependency graph for utils.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

__BEGIN_DECLS int TcpGetBuffer (TCPSOCKET *sock, char *buff, u_int size, u_int *status)
 Get a specified number of characters from TCP socket connection.
int TcpGetLine (TCPSOCKET *sock, char *line, u_short size)
 Get line from TCP socket connection.
int TcpPutString (TCPSOCKET *sock, char *str)
 Send complete string via TCP.
int TcpGetHeaderLines (TCPSOCKET *sock, char ***array)
 Get header line array from TCP socket connection.
void TcpReleaseHeaderLines (char **array)
 Release previously allocated memory for the header lines.
void Led0 (int on)
 Switch LED0 on or off.


Function Documentation

__BEGIN_DECLS int TcpGetBuffer TCPSOCKET *  sock,
char *  buff,
u_int  size,
u_int *  status
 

Get a specified number of characters from TCP socket connection.

The low level NutTcpReceive() routine may return less than the number of bytes specified. This routine makes sure, that the complete buffer is filled. However, an additional pointer to a status is passed to this routine. If the status changes, the routine will be aborted.

Any TCP receive timeout is ignored, but required in order to detect status changes.

Parameters:
sock Socket descriptor. This pointer must have been retrieved by calling NutTcpCreateSocket(). In addition a connection must have been established by calling NutTcpConnect() or NutTcpAccept().
line Pointer to the buffer.
size Size of the buffer.
status Pointer to a status.
Returns:
0 if the buffer is filled. Otherwise -1 is returned.

Definition at line 70 of file utils.c.

00072 {
00073     u_int initial = *status;
00074     int got;
00075 
00076     while (size) {
00077         if (*status != initial || (got = NutTcpReceive(sock, buff, size)) < 0) {
00078             /* Status change or receive error. */
00079             return -1;
00080         }
00081         size -= got;
00082         buff += got;
00083     }
00084     return 0;

int TcpGetLine TCPSOCKET *  sock,
char *  line,
u_short  size
 

Get line from TCP socket connection.

ASCII codes below 32 or above 127 are discarded.

Parameters:
sock Socket descriptor. This pointer must have been retrieved by calling NutTcpCreateSocket(). In addition a connection must have been established by calling NutTcpConnect() or NutTcpAccept().
line Pointer to the buffer that receives the string.
size Size of the buffer. If the received line doesn't fit, then remaining characters will be discarded.
Returns:
Number of characters stored in the line buffer or -1 in case of an error or timeout.

Definition at line 102 of file utils.c.

00104 {
00105     int rc = 0;
00106     int got;
00107     char *cp = line;
00108 
00109     if (size > 0) {
00110         for (;;) {
00111             if ((got = NutTcpReceive(sock, cp, 1)) <= 0) {
00112                 rc = -1;
00113                 break;
00114             }
00115             if (*cp == '\n') {
00116                 *cp = 0;
00117                 puts(line);
00118                 break;
00119             }
00120             if (*cp >= ' ' && rc < (int) size) {
00121                 rc++;
00122                 cp++;
00123             }
00124         }
00125     }
00126     return rc;

int TcpPutString TCPSOCKET *  sock,
char *  str
 

Send complete string via TCP.

The low level NutTcpSend() routine limits the number of bytes to the maximum segment size of the connection. This routine makes sure, that the complete string is transmitted.

Parameters:
sock Socket descriptor. This pointer must have been retrieved by calling NutTcpCreateSocket(). In addition a connection must have been established by calling NutTcpConnect() or NutTcpAccept().
str Pointer to a buffer containing the string to send.
Returns:
0 on success. Otherwise -1 is returned.

Definition at line 143 of file utils.c.

00145 {
00146     int len = (int)strlen(str);
00147     int c;
00148 
00149     puts(str);
00150     while(len) {
00151         if ((c = NutTcpSend(sock, str, (u_short)len)) <= 0) {
00152             break;
00153         }
00154         len -= c;
00155         str += c;
00156     }
00157     return len ? -1 : 0;

int TcpGetHeaderLines TCPSOCKET *  sock,
char ***  array
 

Get header line array from TCP socket connection.

Receives lines from a TCP stream until the first empty line appears and stores all lines in an array of strings. The empty line is not stored, but the last pointer of the array is set to NULL.

The routine allocates the array and all string. The caller should use TcpReleaseHeaderLines() to release all allocated memory.

Parameters:
sock Socket descriptor. This pointer must have been retrieved by calling NutTcpCreateSocket(). In addition a connection must have been established by calling NutTcpConnect() or NutTcpAccept().
array Pointer to a string array pointer, which receives the address of the newly allocated string array. If no non-empty lines were received or if an error occured, then the string array pointer will be set to NULL.
Returns:
The number of header lines stored. In case of an error, the function result is -1 instead.

Definition at line 181 of file utils.c.

00183 {
00184     int rc = -1;
00185     struct LILI_ {
00186         struct LILI_ *ll_next;
00187         char *ll_line;
00188     };
00189     struct LILI_ *root = NULL;
00190     struct LILI_ *link = NULL;
00191     struct LILI_ **next = &root;
00192     char *buf;
00193     int len;
00194 
00195     /*
00196      * Allocate a line buffer. On success, build a linked list of
00197      * incoming lines. Stop on errors or at the first empty line.
00198      */
00199     if ((buf = malloc(255)) != NULL) {
00200         for (;;) {
00201             if ((len = TcpGetLine(sock, buf, 255)) == 0) {
00202                 /* Empty line. */
00203                 break;
00204             }
00205             if (len < 0) {
00206                 /* Error occured. */
00207                 rc = -1;
00208                 break;
00209             }
00210             /* Allocate a linked list item. */
00211             if ((*next = malloc(sizeof(struct LILI_))) == NULL) {
00212                 break;
00213             }
00214             /* Initially set the next link to NULL. */
00215             (*next)->ll_next = NULL;
00216             /* Copy the line to the linked list item. */
00217             (*next)->ll_line = malloc(len + 1);
00218             memcpy((*next)->ll_line, buf, len);
00219             (*next)->ll_line[len] = '\0';
00220             /* Set pointer to the next link. */
00221             next = &((*next)->ll_next);
00222             rc++;
00223         }
00224         free(buf);
00225     }
00226 
00227     if (rc > 0) {
00228         /* Allocate a new string array. */
00229         *array = malloc((rc + 1) * sizeof(char *));
00230         rc = 0;
00231     }
00232     else {
00233         /* Error or single empty line only. Set array pointer to NULL. */
00234         *array = NULL;
00235     }
00236 
00237     /*
00238      * This loop serves two purposes. It moves the strings from the
00239      * linked list to the string array (if one had been allocated)
00240      * and it releases all memory allocated for the linked list.
00241      */
00242     while (root) {
00243         if (*array) {
00244             /* Move string to the array. */
00245             (*array)[rc] = root->ll_line;
00246             rc++;
00247         }
00248         else {
00249             /* No array. Just release the string. */
00250             free(root->ll_line);
00251         }
00252         /* Get the next link and release the current entry. */
00253         link = root;
00254         root = root->ll_next;
00255         free(link);
00256     }
00257 
00258     /* NULL marks the end of the array. */
00259     if (*array) {
00260         (*array)[rc] = NULL;
00261     }
00262     return rc;

void TcpReleaseHeaderLines char **  array  ) 
 

Release previously allocated memory for the header lines.

Should be called to release all memory allocated by TcpGetHeaderLines().

Parameters:
array String array to be released.

Definition at line 271 of file utils.c.

00273 {
00274     char **ap = array;
00275 
00276     if (ap) {
00277         /* Thanks, Dany. */
00278         while (*ap) {
00279             free(*ap);
00280             ap++;
00281         }
00282         free(array);
00283     }

void Led0 int  on  ) 
 

Switch LED0 on or off.

On the SAM7X-EK the LED labeled DS4 is used.

On the SAM9260-EK we use the only one user LED.

Parameters:
on If 0, then the LED will be switched off. Otherwise it will be lit.

Definition at line 294 of file utils.c.

00296 {
00297 #ifdef AT91SAM7X_EK
00298     outr(PIOB_PER, _BV(22));
00299     outr(PIOB_OER, _BV(22));
00300     if (on) {
00301         outr(PIOB_CODR, _BV(22));
00302     }
00303     else {
00304         outr(PIOB_SODR, _BV(22));
00305     }
00306 #else
00307     outr(PIOA_PER, _BV(6));
00308     outr(PIOA_OER, _BV(6));
00309     if (on) {
00310         outr(PIOA_CODR, _BV(6));
00311     }
00312     else {
00313         outr(PIOA_SODR, _BV(6));
00314     }
00315 #endif


Generated on Fri Feb 23 17:29:01 2007 for SAM Internet Radio by  doxygen 1.4.4