Hi,
I've looked in the example,
at <NDK_INSTALL_DIR>\packages\ti\ndk\tools\console\contest.
Seems that I'm doing all OK. However problem still persist....
Please note that my tasks for transmit message via LAN (child socket are static and not dynamic. Every time when a new child socket is opened my transmit taks do fdshare(). Please note that , because the threads are static, im not doing anything when child socket is closed.
The problem that would like to connect my MCU via LAN (many time) communication reconnect, however I fail doing it!!!
I have one static task (void tcpHandler), which is listen to open new socket (child socket on well known port), once the request is received , the
clientfd = accept(lSocket, (struct sockaddr*)&client_addr, &addrlen); is activated, and I allocate dynamically a new Thread (void tcpAppWorker) for handle receive messages on child socket.
Please note that in addition I have other static (please not the transmit message via child task are not dynamic tasks) such as IPC_TaskReceiveLPUMsg (IPC Receive task messageQ for resending it to PC via LAN) which is not dynamically allocated. Every time when socket child is created , I call fdshare() on this task.
The problem that when I try to reopen this socket (communication recovery) it can’t be connected any more. Probably I’m using fdshare wrong or the order of closing and releasing socket from various tasks is wrong.
Please look into my code and tell me what I'm doing wrong....
/*
* ======== tcpHandler ========
* Creates new Task to handle new TCP connections. Static listener task
*/
void tcpHandler(UArg arg0, UArg arg1)
{
SOCKET lSocket;
struct sockaddr_in sLocalAddr;
SOCKET clientfd;
struct sockaddr_in client_addr;
Int addrlen=sizeof(client_addr);
Int optval;
Int optlen = sizeof(optval);
Int status;
Task_Handle taskHandle;
Task_Params taskParams;
Task_FuncPtr ptrTskFunc=tcpBBWorker;
//
Int nbytes=0;
char* ptrAppLANReceivedBuffer = (char *)TCPHandlerReceivedBuffer;
fdOpenSession(TaskSelf());
lSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (lSocket < 0) {
UARTprintf("tcpHandler: socket failed\n");
Task_exit();
return;
}
memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));
sLocalAddr.sin_family = AF_INET;
sLocalAddr.sin_len = sizeof(sLocalAddr);
sLocalAddr.sin_addr.s_addr = htonl(INADDR_ANY);
sLocalAddr.sin_port = htons(arg0);
status = bind(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr));
if (status < 0) {
UARTprintf("tcpHandler: bind failed\n");
fdClose(lSocket);
Task_exit();
return;
}
if (listen(lSocket, 5) != 0){
UARTprintf("tcpHandler: listen failed\n");
fdClose(lSocket);
Task_exit();
return;
}
if (setsockopt(lSocket, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
UARTprintf("tcpHandler: setsockopt failed\n");
fdClose(lSocket);
Task_exit();
return;
}
while (1) {
/* Wait for incoming request */
clientfd = accept(lSocket, (struct sockaddr*)&client_addr, &addrlen);
//First bcommand for client ID, check which task to open according to received client request
nbytes = recv(clientfd, (char *)ptrAppLANReceivedBuffer, 50, 0);//receive client ID
if (nbytes > 0) {
if ((TCPHandlerReceivedBuffer->data[0]==7) && (TCPHandlerReceivedBuffer->data[1]==1))
{
if(TCPHandlerReceivedBuffer->data[2]>1) //R&D
ptrTskFunc=(Task_FuncPtr)tcpAppWorker;
else
ptrTskFunc=(Task_FuncPtr)tcpBBWorker;
Task_Params_init(&taskParams);
taskParams.arg0 = (UArg)clientfd;
taskParams.arg1= (UArg)TCPHandlerReceivedBuffer->data[2];//client type: user, service, user application
taskParams.stackSize = 1024;//768;
taskParams.priority=5;
taskHandle = Task_create((Task_FuncPtr)ptrTskFunc, &taskParams, NULL);
if (taskHandle == NULL) {
UARTprintf("tcpHandler: Failed to create new Task\n");
}//end if
}//end if
}//end if
}//end while
}
/*
* ======== tcpAppWorker========
* Creates new child dynamic task to handle new TCP connection (with new client).
*/
void tcpAppWorker(UArg arg0, UArg arg1)
{
Int nbytes;
Bool flag = TRUE;
Error_Block eb;
char* ptrReceivedBuffer = ((CBoardSubSystem*)g_pBoard)->ptrAppLANReceivedBuffer;
Bool ComEstflag = TRUE;
fdOpenSession(TaskSelf());
UARTprintf("Start TCP App work task\n");
tcpAppWorkerclientfd = (SOCKET)arg0;
((CBoardSubSystem*)g_pBoard)->Appclientfd = tcpAppWorkerclientfd;
if(ComEstflag == TRUE)
{
ComEstflag = FALSE;
//Infornm C28 on communication establish
char temp[4];
temp[0] = NEW_LPU_CLIENT_CONNECTED;
if((arg1 > CIENT_ID_BLUBOX) && (arg1 < CIENT_LOG))
{
temp[1] = arg1;//CIENT_ID_BLUBOX =1, CLIENT_MAIN_APP =2, CIENT_RandD =3, CIENT_SERVICE =4, CIENT_LOG =5
((IPCInterface *)(g_pBoard->m_pIPCInterfacePtr))->IPCSendDataFromLPU(APP_M3_C28_CMD, PRI_NORMAL, 3, temp);
}
}
/* Make sure Error_Block is initialized */
Error_init(&eb);
/* Loop while we receive data */
while (flag) {
nbytes = recv(tcpAppWorkerclientfd, (char *)ptrReceivedBuffer, BBTCPPACKETSIZE, 0);
if (nbytes > 0) {
if(ptrReceivedBuffer[0] > 0)
// Handle the App Command
((CBoardSubSystem*)g_pBoard)->PCMessageHandle(nbytes);
}
else {
// Error
fdClose(tcpAppWorkerclientfd);
flag = FALSE;
tcpAppWorkerclientfd=INVALID_SOCKET;
((CBoardSubSystem*)g_pBoard)->Appclientfd = tcpAppWorkerclientfd;
}
}
fdCloseSession(TaskSelf());
Task_exit();
}
/*******************************************************************************
Function Name : IPC_TaskReceiveLPUMsg - static task for handling IPc messages...resend to PC via LAN
Description : This is the IPC task for receiving IPC messages to be sent to LPU
*******************************************************************************/
Void IPC_TaskReceiveLPUMsg(UArg arg0, UArg arg1)
{
Uint16 tempbuff[MAX_LOG_CMD_DATA_SIZE];
Uint16 loop;
static Bool bNewAppSocketRegister = false;
// Init the IPC, M3 <-> C28x communication (for LPU messages)
IPC_init_1();
// Open the file session
fdOpenSession(TaskSelf());
System_printf("CIPCInterface (M3):: before while, after IPC_init_1() \n");
// Check the WD timer "0" interrupt
while (1)
{
// receive a message
myRcvMessage = IPC_recMsgQ1 ();
if(myRcvMessage == NULL)
{
System_printf("CIPCInterface (M3):myRcvMessage == NULL \n");
continue;
}
switch (myRcvMessage->MsgType)
{
case APP_C28_M3_CMD:
{
if(myRcvMessage->Data[1].IntData== NEW_LPU_CLIENT_CONNECTED) {
if(((CBoardSubSystem*)g_pBoard)->Appclientfd!=INVALID_SOCKET)
{
bNewAppSocketRegister = true;
fdShare((SOCKET)((CBoardSubSystem*)g_pBoard)->Appclientfd);
}
}
//Set the same for Log client
}
break;
case CLIENT_MAIN_APP:
{
int i,j;
//copy data
for(i=0,j=0; i<99 && j<MAX_MSG_DATA_SHORT_SIZE; i++, j++)
{
tempDataBuff[i++]= myRcvMessage->Data[j].ShortData.Byte1;
if(i>99)
break;
tempDataBuff[i]= myRcvMessage->Data[j].ShortData.Byte2;
}
((CBoardSubSystem*)g_pBoard)->LPUAppSendData(tempDataBuff[1], tempDataBuff[0], &tempDataBuff[1]);
}
break;
default:
break;
}
//Free the received message
IPC_freeMsg (myRcvMessage);
}
// Close the file session
fdCloseSession(TaskSelf());
}
Please advice.
Thank you
AS