jtagsock.c

Go to the documentation of this file.
00001 // JTAGSOCK.C
00002 //
00003 // Handles the socket interface for the JTAG emalation
00004 // project.
00005 //
00006 // Based in large on the BSD sockets primer.
00007 //
00008 // Copyright (c) 2002, Jason Riffel - TotalEmbedded LLC.
00009 // All rights reserved.
00010 //
00011 // Redistribution and use in source and binary forms, with or without 
00012 // modification, are permitted provided that the following conditions 
00013 // are met:
00014 //
00015 // Redistributions of source code must retain the above copyright 
00016 // notice, this list of conditions and the following disclaimer. 
00017 //
00018 // Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in 
00020 // the documentation and/or other materials provided with the
00021 // distribution. 
00022 //
00023 // Neither the name of TotalEmbedded nor the names of its 
00024 // contributors may be used to endorse or promote products derived 
00025 // from this software without specific prior written permission.
00026 // 
00027 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00028 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00029 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
00030 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
00031 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
00032 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
00033 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
00034 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00035 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00036 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
00037 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00038 // POSSIBILITY OF SUCH DAMAGE.
00039 //
00040 
00041 #include <errno.h>
00042 #include <signal.h>
00043 #include <stdio.h>
00044 #include <unistd.h>
00045 #include <sys/types.h>
00046 #include <sys/socket.h>
00047 #include <sys/wait.h>
00048 #include <netinet/in.h>
00049 #include <netdb.h>
00050 #include <asm/ioctls.h>
00051 #include "jtagsock.h"
00052 extern int config_gdbserver_quitgdb;
00053 
00054 //
00055 // fJTAGSOCK_Establish
00056 //
00057 // Attaches process to a TCP/IP port but does not wait
00058 // for a connection.
00059 //
00060 int fJTAGSOCK_Establish (unsigned short us_portnum)
00061 { 
00062   char                c_myname[dJTAGSOCK_MAXHOSTNAME+1];
00063   int                 i_socket;
00064   struct sockaddr_in  st_sa;
00065   struct hostent     *st_hp;
00066 
00067   // Clear the address structure
00068   memset(&st_sa, 0, sizeof(struct sockaddr_in));
00069 
00070   // Get our own machine name
00071   gethostname(c_myname, dJTAGSOCK_MAXHOSTNAME);
00072 
00073   // Retrieve our address information
00074   st_hp=gethostbyname(c_myname);
00075   if (st_hp == NULL) return(-1);        // Error condition
00076 
00077   st_sa.sin_family = st_hp->h_addrtype;    // Our host address
00078   st_sa.sin_port   = htons(us_portnum); // Our port number
00079 
00080   // Create the socket
00081   i_socket = socket(AF_INET, SOCK_STREAM, 0);
00082   if (i_socket < 0) return(-1);         // Failed to create socket
00083 
00084   // Bind to the socket to our port
00085   if (bind(i_socket, (struct sockaddr *)&st_sa, sizeof(struct sockaddr_in)) < 0) 
00086   {
00087     // Failed to bind
00088     close(i_socket);
00089     return(-1);
00090   }
00091 
00092   // Set listen for maximum number of connections to queue
00093   listen(i_socket, dJTAGSOCK_NUM_CONNECTIONS);
00094   return(i_socket);
00095 }
00096 
00097 //
00098 // fJTAGSOCK_GetConnection
00099 //
00100 // wait for a connection to occur on a socket created with fJTAGSOCK_Establish()
00101 //
00102 int fJTAGSOCK_GetConnection(int i_socket)
00103 { 
00104   int i_newsocket; // Handle for new socket
00105 
00106   // Attempt to accept a connection
00107   do{
00108      int n;
00109      char buf[100];
00110   i_newsocket = accept(i_socket,NULL,NULL);
00111         ioctl(0,FIONREAD,&n);
00112         if(n)
00113         {
00114         read(0,buf,n);
00115         buf[n] = 0;
00116         do_cmd(buf);
00117         if(config_gdbserver_quitgdb)return -1;
00118         }
00119   }
00120   while(i_newsocket == -1);
00121 
00122   if (i_newsocket < 0) return(-1); // Failed to accept
00123 
00124   return(i_newsocket);
00125 }
00126 
00127 //
00128 // fJTAGSOCK_ReadByte
00129 //
00130 // Reads 1 byte of data from the socket
00131 //
00132 int fJTAGSOCK_ReadByte (int i_socket, char *pc_byte)
00133 { 
00134   return(read(i_socket, pc_byte, 0x01));
00135 }
00136 
00137 //
00138 // fJTAGSOCK_WriteByte
00139 //
00140 // Writes 1 byte of data to the socket
00141 //
00142 int fJTAGSOCK_WriteByte (int i_socket, char c_byte)
00143 {
00144         #if 1
00145   while(write(i_socket, &c_byte, 0x01) != 0x01);
00146         #else
00147         volatile int test=0;
00148         printf("haha-1\n");
00149         test = write(i_socket, &c_byte, 0x01);
00150         printf("haha-2 : test : %d\n",test);
00151         while(test != 0x01)
00152         {
00153                 printf("haha-3\n");     
00154                 test = write(i_socket, &c_byte, 0x01); 
00155                 printf("haha-4 test : %d\n",test);      
00156         }
00157 #endif
00158   return(1);
00159 }

Generated on Fri Nov 5 07:24:46 2010 for ejtag debug tools for loongson 232 cpu by  doxygen 1.5.6