kb_machblue_core_socket.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:12k
- //*****************************************************************************
- //File Name: kb_machblue_core_socket.c
- //
- //Description: socket function
- //
- // used by Machblue to access the platform's socket api
- //
- //Author: steven
- //
- //Date: 2006.12.29
- //
- //Version: v1.0
- //*****************************************************************************
- #include "osp.h"
- #include "tcpip.h"
- #include "machblue_defines.h"
- #include "machblue_customer.h"
- #include "machblue_porting_core.h"
- #define SOCKET_BUF_LEN 1024
- typedef enum
- {
- sDataSend,
- sDataReceive,
- sDataNo
- }sStatus;
- typedef struct _nonBlockSocket_s
- {
- BOOL used;
- BOOL dError;
- sStatus dSR;
- mb_socket_t s;
- unsigned char buf[SOCKET_BUF_LEN];
- }nonBlockSocket_t;
- /*
- * ======================= Assistant Function ===========================================
- * realize non blocking mode
- */
- #define SOCKET_NB_PRIORITY 3*16
- #define SOCKET_NB_NUM 50
-
- static nonBlockSocket_t nbSocket[SOCKET_NB_NUM];
- /**
- * init socket non blocking mode task
- * @return MB_SUCCESS or MB_FAILURE.
- */
- mb_error_t mb_socket_nb_init(void)
- {
- //unsigned long socketNbTId;
- //KB_OSPRet ret;
- unsigned int index;
- for(index=0;index<SOCKET_NB_NUM;index++)
- {
- nbSocket[index].s=MB_INVALID_SOCKET;
- nbSocket[index].used=FALSE;
- nbSocket[index].dError=FALSE;
- nbSocket[index].dSR=sDataNo;
- mb_memcpy(nbSocket[index].buf,"0xFF",SOCKET_BUF_LEN);
- }
- return MB_SUCCESS;
- }
- /**
- * add non blocking socket status
- * @return MB_SUCCESS or MB_FAILURE.
- */
- mb_error_t mb_socket_nb_status_add(mb_socket_t s)
- {
- unsigned int index;
- for(index=0;index<SOCKET_NB_NUM;index++)
- {
- if(nbSocket[index].used==FALSE)
- break;
- }
- if(index==SOCKET_NB_NUM)
- {
- mb_printf("n[Machblue]:Socket nb add status error.");
- return MB_FAILURE;
- }
- nbSocket[index].s=s;
- nbSocket[index].used=TRUE;
- nbSocket[index].dError=FALSE;
- nbSocket[index].dSR=sDataNo;
- return MB_SUCCESS;
- }
- /**
- * remove non blocking socket status
- * @return MB_SUCCESS or MB_FAILURE.
- */
- mb_error_t mb_socket_nb_status_remove(mb_socket_t s)
- {
- unsigned int index;
- for(index=0;index<SOCKET_NB_NUM;index++)
- {
- if(nbSocket[index].s==s)
- {
- nbSocket[index].s=MB_INVALID_SOCKET;
- nbSocket[index].used=FALSE;
- nbSocket[index].dError=FALSE;
- nbSocket[index].dSR=sDataNo;
- mb_memcpy(nbSocket[index].buf,"0xFF",SOCKET_BUF_LEN);
- }
- }
- return MB_SUCCESS;
- }
- /**
- * set non blocking socket status
- * @return MB_SUCCESS or MB_FAILURE.
- */
- mb_error_t mb_socket_nb_status_set(mb_socket_t s,sStatus status)
- {
- unsigned int index;
- BOOL setStatus=FALSE;
- for(index=0;index<SOCKET_NB_NUM;index++)
- {
- if((nbSocket[index].s==s)&&(nbSocket[index].used))
- {
- nbSocket[index].dSR=status;
- setStatus=TRUE;
- break;
- }
- }
- if(setStatus==FALSE)
- {
- mb_printf("n[Machblue]:Socket nb set status error.");
- return MB_FAILURE;
- }
- return MB_SUCCESS;
- }
- /**
- * set non blocking socket status
- * @return MB_SUCCESS or MB_FAILURE.
- */
- mb_error_t mb_socket_nb_error_set(mb_socket_t s,BOOL sError)
- {
- unsigned int index;
- //BOOL setStatus=FALSE;
- for(index=0;index<SOCKET_NB_NUM;index++)
- {
- if((nbSocket[index].s==s)&&(nbSocket[index].used))
- {
- nbSocket[index].dError=sError;
- break;
- }
- }
- return MB_SUCCESS;
- }
- /**
- * get non blocking socket status
- * @return MB_SUCCESS or MB_FAILURE.
- */
- mb_error_t mb_socket_nb_error_get(mb_socket_t s,BOOL *sError)
- {
- unsigned int index;
- //BOOL getStatus=FALSE;
- for(index=0;index<SOCKET_NB_NUM;index++)
- {
- if((nbSocket[index].s==s)&&(nbSocket[index].used))
- {
- *sError=nbSocket[index].dError;
- break;
- }
- }
- return MB_SUCCESS;
- }
- /**
- * get non blocking socket status
- * @return MB_SUCCESS or MB_FAILURE.
- */
- mb_error_t mb_socket_nb_status_get(mb_socket_t s,sStatus *status)
- {
- unsigned int index;
- BOOL getStatus=FALSE;
- for(index=0;index<SOCKET_NB_NUM;index++)
- {
- if((nbSocket[index].s==s)&&(nbSocket[index].used))
- {
- *status=nbSocket[index].dSR;
- getStatus=TRUE;
- break;
- }
- }
- if(getStatus==FALSE)
- {
- mb_printf("n[Machblue]:Socket nb get status error.");
- return MB_FAILURE;
- }
- return MB_SUCCESS;
- }
- /**
- * get non blocking socket status
- * @return MB_SUCCESS or MB_FAILURE.
- */
- unsigned char *mb_socket_nb_buf_get(mb_socket_t s)
- {
- unsigned int index;
- for(index=0;index<SOCKET_NB_NUM;index++)
- {
- if((nbSocket[index].s==s)&&(nbSocket[index].used))
- {
- return nbSocket[index].buf;
- }
- }
- return NULL;
- }
- /**
- * get host
- * @return integer value of host,0 is failure
- */
- unsigned long mb_socket_host_get(const mb_char_t *hostname)
- {
- unsigned long host=0x00000000,data=0;
- unsigned char index,validDcount=0,decimalIndex=0;
- for(index=0;index<50;index++)
- {
- //'.'
- if(hostname[index]==0x2E)
- {
- validDcount+=1;
- switch(validDcount)
- {
- case 1:
- host+=((data<<24)&0xFF000000);
- break;
- case 2:
- host+=((data<<16)&0xFF0000);
- break;
- case 3:
- host+=((data<<8)&0xFF00);
- break;
- }
- data=0;
- decimalIndex=0;
- continue;
- }
- //' '
- if(hostname[index]==0x0)
- {
- validDcount+=1;
- if(validDcount==4)
- host+=(data&0xFF);
- break;
- }
- data=data*(unsigned long)decimalIndex+(unsigned long)((unsigned long)hostname[index]-48);
- if(decimalIndex==0)
- decimalIndex=10;
- }
-
- if(validDcount!=4)
- {
- mb_printf("n[Machblue]:Socket get host error.");
- return 0;
- }
- return host;
- }
- /**
- * Creates a new communication socket.
- * s < pointer to socket to create >
- * socket_type < socket type to create >
-
- * @return MB_SCCESS and updates "s" on success or MB_FAILURE on failure.
- */
- mb_error_t mb_socket_create(mb_socket_t *s,mb_socket_type_t socket_type)
- {
- int sType,dSocket;
- if(socket_type==MB_SOCKET_TYPE_STREAM)
- sType=SOCK_STREAM ;
- else
- sType=SOCK_DGRAM ;
-
- dSocket=socket(AF_INET,sType,0);
- if(dSocket<0)
- {
- mb_printf("n[Machblue]:Socket create error.");
- return MB_FAILURE;
- }
- if(mb_socket_nb_status_add((mb_socket_t)dSocket)==MB_FAILURE)
- {
- mb_printf("n[Machblue]:Socket create add status error.");
- return MB_FAILURE;
- }
- *s=(mb_socket_t)dSocket;
-
- return MB_SUCCESS;
- }
- /**
- * Deletes a new communication socket.
- * s < socket to delete >
-
- * @return MB_SUCCESS on success and MB_FAILURE on failure.
- */
- mb_error_t mb_socket_delete(mb_socket_t s)
- {
- if(s==MB_INVALID_SOCKET)
- {
- mb_printf("n[Machblue]:Socket delete error.");
- return MB_FAILURE;
- }
- mb_socket_nb_status_remove(s);
-
- return MB_SUCCESS;
- }
- /**
- * Connects a stream socket to a remote host.
- * s < socket to connect >
- * hostname < tcp/ip hostname of the host to connect to >
- * port < tcp/ip port to connect to >
-
- * @return MB_SUCCESS on success and MB_FAILURE on failure.
- */
- mb_error_t mb_socket_connect(mb_socket_t s,const mb_char_t *hostname,unsigned int port)
- {
- struct sockaddr_in addr;
-
- mb_memset(&addr,0,sizeof(struct sockaddr_in));
- addr.sin_family=AF_INET;
- addr.sin_port=htons(port);
- addr.sin_addr.s_addr=(u32)mb_socket_host_get(hostname);
- if(connect((int)s,(struct sockaddr*)&addr,(int)sizeof(struct sockaddr_in))!=0)
- {
- mb_printf("n[Machblue]:Socket connect error.");
- return MB_FAILURE;
- }
- if(fcntl((int)s,(int)1,(int)2)!=0)
- {
- mb_socket_disconnect(s);
- mb_printf("n[Machblue]:Socket connect mode switch error.");
- return MB_FAILURE;
- }
-
- return MB_SUCCESS;
- }
- #define SHUTDOWN_RECEIVE 0
- #define SHUTDOWN_SEND 1
- #define SHUTDOWN_ALL 2
- /**
- * Disconnects a stream socket from a remote host. It is legal
- * for the client to disconnect a socket already disconnected by the
- * server (and vice-versa).
- *
- * s < socket to disconnect >
-
- * @return MB_SUCCESS on success and MB_FAILURE on failure.
- */
- mb_error_t mb_socket_disconnect(mb_socket_t s)
- {
- int ret;
-
- ret=shutdown((int)s,(int)SHUTDOWN_ALL);
- if(ret!=1)
- {
- mb_printf("n[Machblue]:Socket disconnect shutdown error.");
- return MB_FAILURE;
- }
- ret=close((int)s);
- if(ret!=1)
- {
- mb_printf("n[Machblue]:Socket disconnect close error.");
- return MB_FAILURE;
- }
-
- return MB_SUCCESS;
- }
- /**
- * Polls a socket for a given event.
- * s < socket to poll >
- * event < event to poll for >
-
- * @return MB_SUCCESS if the event condition polled for is current, MB_FAILURE otherwise.
- */
- mb_error_t mb_socket_poll(mb_socket_t s,mb_socket_event_t event)
- {
- mb_error_t ret;
- BOOL sError;
- sStatus status;
- ret=mb_socket_nb_status_get(s,&status);
- if(ret!=MB_SUCCESS)
- {
- mb_printf("n[Machblue]:Socket poll get status error.");
- return MB_FAILURE;
- }
-
- switch(event)
- {
- case MB_SOCKET_EVENT_DATA_IN:
- //socket is sending data
- if(status==sDataSend)
- {
- mb_printf("n[Machblue]:Socket poll data in error.");
- return MB_FAILURE;
- }
- else
- {
- int size;
- unsigned char *buf=mb_socket_nb_buf_get(s);
- if(buf==NULL)
- {
- mb_printf("n[Machblue]:Socket poll data in buf NULL.");
- return MB_FAILURE;
- }
- size=recv((int)s,(void*)buf,(int)1,(unsigned int)0);
- if(size!=1)
- {
- mb_printf("n[Machblue]:Socket poll data in data NULL.");
- return MB_FAILURE;
- }
-
- mb_socket_nb_status_set(s,sDataReceive);
- return MB_SUCCESS;
- }
-
- case MB_SOCKET_EVENT_DATA_OUT:
- //socket is receiving data
- if(status==sDataReceive)
- {
- mb_printf("n[Machblue]:Socket poll data out error.");
- return MB_FAILURE;
- }
- else
- {
- mb_socket_nb_status_set(s,sDataSend);
- return MB_SUCCESS;
- }
-
- case MB_SOCKET_EVENT_ERROR:
- mb_socket_nb_error_get(s,&sError);
- if(sError)
- {
- mb_socket_nb_error_set(s,FALSE);
- return MB_SUCCESS;
- }
- else
- return MB_FAILURE;
- default:
- mb_printf("n[Machblue]:Socket poll event type error.");
- }
-
- return MB_FAILURE;
- }
- /**
- * Sends data over a connected stream socket.
- * s < socket to send data on >
- * msg < message to send >
- * length < size of the message to send in bytes >
-
- * @return the amount of data sent on success and a strcitly negative number on failure.
- */
- mb_ssize_t mb_socket_send(mb_socket_t s,const void *msg,mb_size_t length)
- {
- //mb_error_t ret;
- //KB_OSPMsgNode nbMsg;
- int size;
- unsigned char *buf=mb_socket_nb_buf_get(s);
- if(buf==NULL)
- {
- mb_printf("n[Machblue]:Socket poll data in buf NULL.");
- return (mb_ssize_t)0;
- }
-
- size=send((int)s,(void*)buf,(int)length,(unsigned int)0);
- if(size==-1)
- {
- mb_socket_nb_error_set(s,TRUE);
- return (mb_ssize_t)0;
- }
- if(size==length)
- mb_socket_nb_status_set(s,sDataNo);
- return (mb_ssize_t)size;
- }
- /**
- * Receives data from a connected stream socket.
- * s < socket to receive data from >
- * buf < buffer to receive data in >
- * length < length in bytes of buffer >
-
- * @return the amount of data read on success and a strcitly negative number on failure.
- */
- mb_ssize_t mb_socket_recv(mb_socket_t s,void *buf,mb_size_t length)
- {
- //mb_error_t ret;
- int size;
- unsigned char *sBuf=mb_socket_nb_buf_get(s);
- unsigned short rLen=length-1;
- if(length==1)
- {
- mb_memcpy(buf,sBuf,1);
- mb_socket_nb_status_set(s,sDataNo);
- return length;
- }
- else
- {
- if(length>SOCKET_BUF_LEN)
- rLen=SOCKET_BUF_LEN-1;
- size=recv((int)s,(void*)&sBuf[1],(int)1,(unsigned int)0);
- if(size==-1)
- {
- mb_socket_nb_error_set(s,TRUE);
- return (mb_ssize_t)0;
- }
- mb_memcpy(buf,sBuf,size+1);
- if((size==(length-1))&&(length<=SOCKET_BUF_LEN))
- mb_socket_nb_status_set(s,sDataNo);
- return (mb_ssize_t)size;
- }
- }
- /**
- * socket init
- * @return MB_SUCCESS or MB_FAILURE.
- */
- mb_error_t mb_socket_init(void)
- {
- unsigned char hwMac[6]={'00','11','22','33','44','55'};
- unsigned long ip,mask,gw;
- int ret;
- if(mb_socket_nb_init()!=MB_SUCCESS)
- {
- mb_printf("n[Machblue]:Socket init error.");
- return MB_FAILURE;
- }
- ip=0xC0A80170;
- mask=0xFFFFFF00;
- gw=0xC0A80101;
- ret=netif_init((unsigned int)ip,(unsigned int)mask,(unsigned int)gw,hwMac);
- if(ret!=1)
- {
- mb_printf("n[Machblue]:Socket init error.");
- return MB_FAILURE;
- }
-
- return MB_SUCCESS;
- }