Y:/FINAL CODE/UDP_Client_app.c

Go to the documentation of this file.
00001 /******************************************************************************                      
00002  *
00003  *       Copyright (C) 2006 J Team, Inc.
00004  *       All Rights Reserved
00005  *
00006  * File Name      : UDP_Client_app.c
00007  * Project Name   : RFIDXpress.mcp
00008  * Description    : This file contains the actual Ethernet transmission and
00009  *                  Receiving functionality for the RFID Xpress system.  The
00010  *                  SendSerial and EventListener methods are the transmission
00011  *                  and receiving methods, respectively.
00012  *                  
00013  * Authors: Jared Suttles
00014  *          Jennifer Tietz
00015  *          Jonathan Chen
00016  *          Joshua Chapman
00017  *
00018  * Version : 2.0
00019  * Date    : 05/1/06
00020  *
00021  *****************************************************************************/
00022 
00023 
00024 #include "debug.h"
00025 #include "datatypes.h"
00026 #include "globalvariables.h"
00027 #include "system.h"
00028 #include "tcp_ip.h"
00029 
00030 #include "string.h"
00031 #include "MOTTYPES.h"
00032 #include "IO_Map.h"
00033 
00034 
00035 /* The applications that use UDP must implement following function stubs                        */
00036 /* void application_name_init (void) - call once when processor starts                          */
00037 /* void application_name_run (void) - call periodically on main loop                            */
00038 /* INT32 application_name_eventlistener (INT8, UINT8, UINT32, UINT16, UINT16, UINT16)   */
00039 /* - called by TCP input process to inform arriving data, errors etc                            */
00040 
00041 void udp_demo_run(void);
00042 
00043 INT32 udp_demo_eventlistener(INT8 , UINT8 , UINT32 , UINT16 , UINT16 , UINT16 );
00044 INT8 udp_demo_soch;
00045 
00046 #define UDP_DEMO_PORT   2001              /* Port to be used on MICRO */
00047 #define UDP_DEMO_RMTHOST_IP     0xC0A80201      /* IP of Server*/
00048 #define UDP_DEMO_RMTHOST_PRT    2000        /* Port of Server */
00049 
00050 extern INT8 SendData;           //1 if data needs sent. 0 if not.
00051 extern INT8 RcvData;           //1 if data needs sent. 0 if not.
00052 extern char toSend[15];
00053 extern char rcvPacket[31];
00054 
00055 /*
00056  * Sends the data in the toSend array over the ethernet connection
00057  */
00058 
00059 UINT16 SendSerial() {
00060 
00061                 UINT16  i, datalen;
00062       
00063     if (SendData == 1) {
00064       
00065       for (i = 0; i< 14; i++) {
00066         net_buf[UDP_APP_OFFSET+i]= toSend[i]; //'U';
00067       }
00068         datalen = i+1;       
00069                 
00070         i = udp_send(udp_demo_soch,UDP_DEMO_RMTHOST_IP,UDP_DEMO_RMTHOST_PRT,net_buf+UDP_APP_OFFSET,NETWORK_TX_BUFFER_SIZE-UDP_APP_OFFSET,datalen);
00071         
00072         SendData = 0;
00073     }
00074     return 0;
00075 }
00076 
00077 
00078 /*
00079  * Initialize resources needed for the UDP socket application 
00080  */
00081 
00082 void udp_demo_init(void){
00083 
00084         DEBUGOUT("Initializing UDP demo client\r\n");
00085 
00086         /* Get socket:
00087          *      0 - for now not type of service implemented in UDP
00088          *      udp_echo_eventlistener - pointer to listener function
00089          *      UDP_OPT_SEND_CS|UDP_OPT_CHECK_CS - checksum options. Calculate
00090          *              checksum for outgoing packets and check checksum for
00091          *              received packets.
00092          */
00093         udp_demo_soch=udp_getsocket(0 , udp_demo_eventlistener , UDP_OPT_SEND_CS | UDP_OPT_CHECK_CS);
00094         
00095         if(udp_demo_soch == -1){
00096                 DEBUGOUT("No free UDP sockets!! \r\n");
00097                 RESET_SYSTEM();
00098         }
00099         
00100         /* open socket for receiving/sending of the data on defined UDP_DEMO_RMTHOST_PRT*/
00101         (void)udp_open(udp_demo_soch,UDP_DEMO_PORT);
00102         
00103         /* for now no data sending */
00104         SendData = 0;
00105 }
00106 
00107 
00108 /* UDP Demo app main loop that is periodically invoked from the
00109  * main loop (see main_con.c)
00110  */
00111 
00112 void udp_demo_run(void){
00113         void)SendSerial();
00114 }
00115 
00116 
00117 /*
00118  * Event listener invoked when TCP/IP stack receives UDP datagram for
00119  * a given socket. Parameters:
00120  * - cbhandle - handle of the socket this packet is intended for. Check it
00121  *      just to be sure, but in general case not needed
00122  * - event - event that is notified. For UDP, only UDP_EVENT_DATA
00123  * - UDP_DEMO_RMTHOST_IP - IP address of remote host who sent the UDP datagram
00124  * - UDP_DEMO_RMTHOST_PRT - UDP_DEMO_RMTHOST_PRT number of remote host who sent the UDP datagram
00125  * - buffindex - buffer index in RTL8019AS allowing you to read 
00126  *      received data more than once from Ethernet controller by
00127  *      invoking NETWORK_RECEIVE_INITIALIZE(buffindex) and then start
00128  *      reading the bytes all over again
00129  */ 
00130 
00131 extern byte state;
00132 
00133 INT32 udp_demo_eventlistener (INT8 cbhandle, UINT8 event, UINT32 ipaddr, UINT16 port, UINT16 buffindex, UINT16 datalen) {
00134 
00135   int i,j,k;
00136   ipaddr = 0;
00137   buffindex = 0;
00138   port = 0;
00139 
00140         if(cbhandle!=udp_demo_soch){
00141                 DEBUGOUT("Not my handle!!!!");
00142                 return (-1);
00143         }
00144         switch(event){
00145                 case UDP_EVENT_DATA:
00146                         
00147                         /* read data to transmit buffer and send response*/
00148                         if(datalen>(NETWORK_TX_BUFFER_SIZE-UDP_APP_OFFSET))
00149                           datalen=NETWORK_TX_BUFFER_SIZE-UDP_APP_OFFSET;
00150                         
00151                         
00152                         RECEIVE_NETWORK_BUF(net_buf+UDP_APP_OFFSET,datalen);
00153                         
00154                         for (i = 0; i <= datalen; i++){
00155                           rcvPacket[i] = net_buf[UDP_APP_OFFSET+i];
00156                         }
00157 
00158                         break;
00159                 default:
00160                         /* should never get here */
00161                         DEBUGOUT("Unknown UDP event :-(");
00162                         break;
00163   }
00164   if (SendData == 1) {
00165     RcvData = 1;        
00166   }
00167   
00168         return 0;
00169 }

Generated on Sun Apr 30 17:44:01 2006 for RFID by  doxygen 1.4.6-NO