Hi,
I would like to verify that I do socket close properly if its shared between multiple tasks. Please advice because after closing the socket I can't reopen it because I think that it was not properly closed. Please note if I set in comment " fdShare" in other tasks for the socket , I can reopen the socket (perform communication recovery as required). When fdShare is used in otger tasks and socket is closed by main task, I'm fail reopen it.
My main task:
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);
//UARTprintf("TCP App new msg\n");
UARTprintf("TCP_App rec NewMsg: SocketN = 0x%x, Size = %d\n", tcpAppWorkerclientfd, nbytes);
UARTprintf(" DS,");
for (int i = 0; i < 10; i++)
{
UARTprintf("%d,", ptrReceivedBuffer[i]);
}
UARTprintf("DE\n");
if (nbytes > 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());
UARTprintf("Close tcp App worker\n");
/*
* Since deleteTerminatedTasks is set in the cfg file,
* the Task will be deleted when the idle task runs.
*/
Task_exit();
}
Other tasks which use thi s socket, say for transmitting messages, I’ve added fdshare(0 function, although I’m not sure that correctly.
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(bNewAppSocketRegister == true)
{
if(((CBoardSubSystem*)g_pBoard)->Appclientfd==INVALID_SOCKET)
{
fdCloseSession(TaskSelf());
bNewAppSocketRegister = false;
}
}
switch (myRcvMessage->MsgType)
{
case SUBSYSTEM_CMD_BLUEBOX:
// Post the BlueBox Semaphore
Semaphore_post(SEM_BB_WaitOnDataReceived);
break;
case APP_C28_M3_CMD:
{
if((myRcvMessage->Data[1].IntData== NEW_LPU_CLIENT_CONNECTED) /*&& (myRcvMessage->Data[2].IntData== CLIENT_MAIN_APP)*/)
{
if(((CBoardSubSystem*)g_pBoard)->Appclientfd!=INVALID_SOCKET)
{
bNewAppSocketRegister = true;
fdOpenSession(TaskSelf());
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;
//break;
default:
break;
}
//Free the received message
IPC_freeMsg (myRcvMessage);
}
}
Void TaskTransmitLogToLPU(UArg arg0, UArg arg1)
{
static Bool bNewAppSocketRegister = false;
while(1)
{
Semaphore_pend(SEM_TRANSMIT_LOG_TO_LPU_VIA_QUE, BIOS_WAIT_FOREVER);
pTransmitLOGMessageFromQue currentTransmitLOGMessageFromQue = NULL;
Uint16 data[20] = {0};
char uiAppCmd = 0;
char uiMsgSize = 0;
//Set registration to socket
if(bNewAppSocketRegister == false)
{
if(((CBoardSubSystem*)g_pBoard)->Appclientfd!=INVALID_SOCKET)
{
fdOpenSession(TaskSelf());
fdShare((SOCKET)((CBoardSubSystem*)g_pBoard)->Appclientfd);
bNewAppSocketRegister = true;
}
else
continue;
}
else
{
if( ((CBoardSubSystem*)g_pBoard)->Appclientfd == INVALID_SOCKET )
{
fdCloseSession(TaskSelf());
bNewAppSocketRegister = false;
continue;
}
}
//Enter gate
UInt gateKey = GateMutex_enter(gateMutex6);
do
{
//get data from the que
pTransmitLOGMessageFromQue currentTransmitLOGMessageFromQue = (pTransmitLOGMessageFromQue)Queue_get(QUE_TRANSMIT_LOG_TO_LPU_MESSAGES);
if(currentTransmitLOGMessageFromQue == NULL)
{
UARTprintf("!!! Error currentTransmitLOGMessageFromQue = 0\n");
break;
}
uiAppCmd = currentTransmitLOGMessageFromQue->uiAppCmd;
uiMsgSize = currentTransmitLOGMessageFromQue->uiMsgSize;
if ( (uiMsgSize <= 0) || (uiMsgSize > 20) )
{
UARTprintf("!!! Error TaskTransmitLogToLPU/ uiDataSize = 0\n");
break;
}
memcpy(data, currentTransmitLOGMessageFromQue->pData, sizeof(data[0])*uiMsgSize);
TransmitLOGMessage_freeMsg(currentTransmitLOGMessageFromQue);
}while(0);
GateMutex_leave(gateMutex6, gateKey);
if(uiMsgSize > 0)
{
((CBoardSubSystem*)g_pBoard)->LPUAppSendData(uiAppCmd, uiMsgSize, &data[0]);
}
}
}
This task calls :
- 1. fdOpenSession(TaskSelf());
- 2. fdShare((SOCKET)((CBoardSubSystem*)g_pBoard)->Appclientfd);
Please note that when in tcpAppWorker socket is closed, the following is called:
fdClose(tcpAppWorkerclientfd);
flag = FALSE;
tcpAppWorkerclientfd=INVALID_SOCKET;
((CBoardSubSystem*)g_pBoard)->Appclientfd = tcpAppWorkerclientfd;
fdCloseSession(TaskSelf());
, then in my other tasks (TaskTransmitLogToLPU , IPC_TaskReceiveLPUMsg) is detected that socket is not valid anymore the following is called (please take a look at the tasks):
fdCloseSession(TaskSelf());
bNewAppSocketRegister = false;
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.
Can you please avise me when I shall call in other task which uses LAN socket the following functions:
fdOpenSession(TaskSelf());
fdShare((SOCKET)((CBoardSubSystem*)g_pBoard)->Appclientfd);
fdCloseSession(TaskSelf());
and maybe fdClose(tcpAppWorkerclientfd);
How shall I synchronize open/close socket between my main task tcpAppWorker (which uses this socket- fiscally determines that there is a problem on communication and closes the socket) and my other tasks which use this socket to send messages to PC through this socket.
Thank you in advance,
Hope to hear from you soon
Regards, AS