#include "opentcp.h" #include "netComm.h" #include "util.h" #include "mcu_port.h" #include #include #include #include "jtag.h" #define CMD_DNLD 0x646e6c64 #define CMD_DELE 0x64656c65 #define CMD_LSFL 0x6c73666c #define CMD_PLAY 0x706c6179 #define CMD_EXIT 0x65786974 #define CMD_ABRT 0x61627274 #define CMD_ERRO 0x6572726f #define CMD_SCSS 0x73637373 #define CMD_LSDV 0x6c736476 #define CMD_PRNT 0x70726e74 #define CMD_PING 0x70696e67 #define CMD_PONG 0x706f6e67 #define MAX_PARAM_LEN 50 #define MAX_PARAMS 5 #define MAX_LABEL_LEN 11 #define MAX_STORED_BYTES 5*1024*1024 #define MAX_FILES 3 #define MAX_SENDBUF 1440 //max mss UINT8 netComm_enabled = 0; UINT8 mSocket; int receivedBytes; int toReceiveBytes; int storedBytes; int isConActive; char *file; char label[MAX_LABEL_LEN + 1]; char sendBuf[MAX_SENDBUF + TCP_APP_OFFSET]; char *toSend; int toSendBytes; int sentBytes; int lastSentBytes; struct{ char label[MAX_LABEL_LEN+1]; int size; char *address; }table[MAX_FILES]; INT8 netComm_init (void){ INT8 soch = tcp_getsocket(TCP_TYPE_SERVER, TCP_TOS_NORMAL, TCP_DEF_TOUT, netComm_listener); mSocket = 0; if(soch < 0){ DEBUGOUT("netComm Server uncapable of getting socket\n"); RESET_SYSTEM(); return(-1); } mSocket=soch; soch = tcp_listen(mSocket, 1337); if(soch < 0){ DEBUGOUT("netComm Server uncapable of setting socket to listening mode\n"); RESET_SYSTEM(); return(-1); } storedBytes=0; file=NULL; receivedBytes = -1; toReceiveBytes = -1; toSendBytes=0; toSend=sendBuf+TCP_APP_OFFSET; sentBytes=0; lastSentBytes=0; isConActive=0; netComm_enabled = 1; return(1); } void netComm_run (void){ if(netComm_enabled == 0) return; if(tcp_getstate(mSocket) < TCP_STATE_LISTENING){ tcp_listen(mSocket, 1337); } if(isConActive!=1) return; if(netComm_send()==0){ netComm_disconn(); } } /** * returns -1 when all data sent or there is no data to send; 0 when cant send any data; 1 when there is more data to send/ waiting for ack. * THIS FUNCTION IS DANGEROUS CAN CAUSE RACE CONDITIONS; DONT CALL DIRECTLY **/ int netComm_send(void){ if(toSendBytes==0) return -1; //nothing to send if(lastSentBytes!=0) return -1; //last block has not been sent if(sentBytes>=toSendBytes){ //transfer complete sentBytes=0; toSendBytes=0; toSend=sendBuf+TCP_APP_OFFSET; return -1; } lastSentBytes=tcp_send(mSocket, toSend+sentBytes, (UINT8)(toSendBytes-sentBytes), (UINT16)(MAX_SENDBUF-sentBytes)); if(lastSentBytes<0){ //cant send data return 0; } return 1; } UINT32 netComm_listener (INT8 cbhandle, UINT8 event, UINT32 par1, UINT32 par2){ int i; char pBuff[50]; if(netComm_enabled == 0) return(-1); if(cbhandle < 0) return(-1); switch( event ){ case TCP_EVENT_CONREQ: printl("conn requested\n"); if(mSocket != 0) return(-1); if(isConActive!=0)return(-1); return(1); case TCP_EVENT_ABORT: printl("conn aborted\n"); netComm_disconn(); return(1); case TCP_EVENT_CONNECTED: printl("connected\n"); isConActive=1; if(mSocket == 0) return(-1); return(1); case TCP_EVENT_CLOSE: printl("connection closed\n"); netComm_disconn(); return(1); case TCP_EVENT_ACK: //printl("acked\n"); sentBytes+=(int)lastSentBytes; lastSentBytes=0; mcu_cs2_off(); //DEBUG return(1); case TCP_EVENT_DATA: tst1_on(); //DEBUG if(isConActive!=1){ printl("Dumping packet.. ignoring traffic 'till reconnect"); eth_stub_skip_buf(&fec_nif, (int)par1); return(-1); } i=netComm_processData((int)par1); //sprintf(pBuff, "got data; process value: %d; ackno: %d\n", i, (int)par2-16777216); printl(pBuff); if(i<0){ toSendBytes=sprintf(toSend, "erro %d\n", -i); if(i<=-1 && i>=-5){ netComm_disconn(); //return(-1); } }else if(i>0){ toSendBytes=sprintf(toSend, "scss %d\n", i); } return(1); case TCP_EVENT_REGENERATE: //printl("regenarating packet"); lastSentBytes=tcp_send(mSocket, toSend+sentBytes, (UINT16)(toSendBytes-sentBytes), (UINT16)(MAX_SENDBUF-sentBytes)); return(1); default: return(-1); } } int netComm_processData(int len){ long cmd=0; char params[MAX_PARAM_LEN]; char *param[MAX_PARAMS]; char *ptr; char pBuff[100]; //debug int i=0, ii=0, paramCount=0; if(toReceiveBytes<=0){ //Its a command if(len<5){ //sprintf(pBuff, "len: %d\n", len); printl(pBuff); return(-1); } cmd = RECEIVE_NETWORK_B(); cmd <<= 8; cmd |= RECEIVE_NETWORK_B(); cmd <<= 8; cmd |= RECEIVE_NETWORK_B(); cmd <<= 8; cmd |= RECEIVE_NETWORK_B(); if(RECEIVE_NETWORK_B()!=' '){ //does not follow protocol. printl("Violated protocol\n"); return(-1); } //printl("command ok\n"); len-=5; for(i=0; iMAX_STORED_BYTES){ //file is too large toReceiveBytes=-1; printl("file too large\n"); return(-2); } file=malloc(toReceiveBytes); if(file==NULL){ //Cannot allocate memory toReceiveBytes=-1; printl("cannot allocate memory\n"); return(-2); } storedBytes+=toReceiveBytes; receivedBytes=len; eth_stub_read_buf(&fec_nif, file, len); //printl("Starting to save file\n"); return(0); case(CMD_DELE): if(paramCount!=1) return(-1); //must have 1 parameter if(netComm_delFile(param[0])){ return(3); } return(-7); case(CMD_PLAY): if(paramCount!=2) return(-1); //must have 2 parameters i=netComm_getIndex(param[0]); if(i<0){ return(-7); } return(jtag_play(table[i].address, table[i].size, param[1])); case(CMD_LSFL): toSendBytes=sprintf(toSend, "lsfl "); ii=toSendBytes; for(i=0; i0){ toSendBytes+=sprintf(toSend+toSendBytes, "%s ", table[i].label); } } if(toSendBytes==i){ toSendBytes+=sprintf(toSend+toSendBytes-1, "\n"); }else{ toSendBytes+=sprintf(toSend+toSendBytes, "\n"); } return(0); case(CMD_PING): toSendBytes=sprintf(toSend, "pong \n"); return(0); case(CMD_PONG): printl("pong\n"); return(0); case(CMD_LSDV): case(CMD_EXIT): case(CMD_ABRT): case(CMD_PRNT): return(-6); //unimplemented command default: //unknown command return(-1); } }else if(file!=NULL){ //Dumping data to "file" if(len+receivedBytes=0){ //adding new strcpy(table[empty].label, label); table[empty].size=size; table[empty].address=address; return(1); } return (-3); //cannot add more files } int netComm_canAddFile(char *label){ int i; for(i=0; i=0 && table[i].address!=NULL){ return(i); } } return (-7); } int netComm_delFile(char *label){ int i; i=netComm_getIndex(label); if(i>0){ free(table[i].address); storedBytes-=table[i].size; table[i].address=NULL; table[i].size=-1; return(1); } return(i); }