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

utils.c

Go to the documentation of this file.
00001 
00044 #include <stdlib.h>
00045 #include <stdio.h>
00046 #include <string.h>
00047 
00048 #include "utils.h"
00049 
00071 int TcpGetBuffer(TCPSOCKET * sock, char * buff, u_int size, u_int *status)
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;
00085 }
00086 
00103 int TcpGetLine(TCPSOCKET * sock, char * line, u_short size)
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;
00127 }
00128 
00144 int TcpPutString(TCPSOCKET * sock, char * str)
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;
00158 }
00159 
00182 int TcpGetHeaderLines(TCPSOCKET * sock, char ***array)
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;
00263 }
00264 
00272 void TcpReleaseHeaderLines(char **array)
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     }
00284 }
00285 
00295 void Led0(int on)
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
00316 }
00317 

Generated on Fri Feb 23 17:28:49 2007 for SAM Internet Radio by  doxygen 1.4.4