Advertisement
Guest User

sendAndWaitForReply

a guest
Mar 23rd, 2017
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.63 KB | None | 0 0
  1. SINT32 mmsclnTst_SmiClient_SendReceive(SMI_SERVER_INFO *pSrvInfo, VOID *pData, UINT32 DataLen, SMI_MSG *ReplyMsg, MMSCLNT_SERVICE SrvType) {
  2.  
  3.     SINT32 ret = 0; /* return value             */
  4.     SMI_MSG Msg; /* structure of SMI messages*/
  5.     VOID *pCall = NULL;
  6.     CHAR ntoaStr[INET_ADDR_LEN];
  7.     struct in_addr IpAddr;
  8.     SINT32 WaitForReply = 1;
  9.  
  10.     memset(&Msg, 0, sizeof(Msg)); //MODIFIED added
  11.  
  12.     printf("DEBUG: SmiClient_SendReceive() TYPE OF CALL: %s \n",SERVICETYPES[SrvType]);
  13.  
  14.     /*
  15.      * If the RemoteModNb is zero (meaning invalid), the RemoteModNb has to
  16.      * be requested from the remote target (ResourceManager on the remote
  17.      * CPU), before a SMI-Call can be sent to the specific remote SW-Module.
  18.      */
  19.     if (!pSrvInfo->ModNb) {
  20.         printf("%s_SMICLIENT_SEND: Cpu 0x%8x ModName %s ModNb 0x%08x\n", mmsclnTst_AppName, pSrvInfo->Cpu, pSrvInfo->ModName, pSrvInfo->ModNb);
  21.         ret = GetRemoteModNumber(pSrvInfo); //moduleNumber is inserted into pSrvInfo
  22.                     //printf(" return GetRemoteModNumber  %d \n",ret);
  23.                     //printdf("%s_SMICLIENT_SEND: Cpu 0x%8x ModName %s ModNb 0x%08x\n",
  24.                     //        mmsclnTst_AppName, pSrvInfo->Cpu, pSrvInfo->ModName, pSrvInfo->ModNb);
  25.         if (ret < 0)
  26.             return -1;
  27.     }
  28.  
  29.     /* __________SPECIAL case ? */
  30.     if (pData == NULL || DataLen == 0) {
  31.         printf("DEBUG: sendReceive special case... \n");
  32.  
  33.  
  34.         DataLen = sizeof(MMSCLNT_CONN_INFO_C); /* ping the MMSCLNT SMI service at the remote server */
  35.         pCall = smi_MemAlloc(DataLen);
  36.  
  37.         if (pCall == NULL) {
  38.             printf("pCall is NULL \n");
  39.             log_Err("%s no memory, line: %i", __FILE__, __LINE__);
  40.  
  41.             return -1;
  42.         }
  43.  
  44.         if (pCall) {
  45.  
  46.             bzero((char*) pCall, DataLen);
  47.             //printf(" pCall prepare info call \n");
  48.             /* prepare info call */
  49.             ((MMSCLNT_CONN_INFO_C*) pCall)->Hdr.SrvType = MMSCLNT_CONNINFO;
  50.             ((MMSCLNT_CONN_INFO_C*) pCall)->Hdr.User = 0;
  51.             ((MMSCLNT_CONN_INFO_C*) pCall)->Hdr.ConnId = 0;
  52.             //  printf(" pCall prepare info call \n");
  53.         }
  54.  
  55.     /*________ DEFAULT case ! */
  56.     } else {
  57.         printf("DEBUG: sendReceive() NO special case... \n");
  58.         pCall = smi_MemAlloc(DataLen); /* Allocate memory for SMI call */
  59.  
  60.         //no memory
  61.         if (pCall == NULL) {
  62.             log_Err("%s no memory, line: %i", __FILE__, __LINE__);
  63.  
  64.             smi_MemFreeDebug(pCall); //MODIFIED added free
  65.             pCall = NULL; //MODIFIED added null assign
  66.  
  67.             return -1;
  68.         }
  69.  
  70.         if (pCall) {
  71.             memcpy(pCall, pData, DataLen); /* insert data */
  72.             //printf("memcpy(pCall, pData, DataLen)\n");
  73.         }
  74.     }
  75.  
  76.     if (pCall == NULL) {
  77.         log_Err("%s_SMICLIENT_SEND: Not enough memory!", mmsclnTst_AppName);
  78.         return -1;
  79.     }
  80.  
  81.     /* dispatch request to remote resource manager for module number */
  82.     SmiClientInfo.XId++;
  83.  
  84.     printf("DEBUG: sendReceive() -> smi_SendCall(..) ... \n");
  85.  
  86.     /*______ SEND the call */
  87.     if (smi_SendCall(SmiClientInfo.SmiId, pSrvInfo->Cpu, SmiClientInfo.XId, pSrvInfo->ModNb, SmiClientInfo.Protvers, MMSCLNT_PROC_MMSSRV, pCall, DataLen)) {
  88.  
  89.         IpAddr.s_addr = ntohl(pSrvInfo->Cpu);
  90.         inet_ntoa_b(IpAddr, ntoaStr);
  91.  
  92.         log_Err("%s_SMICLIENT_SEND: Cannot send call to module '%s' on CPU %s!", mmsclnTst_AppName, pSrvInfo->ModName, ntoaStr);
  93.         if (mmsclnTst_Debug & APP_DBG_INFO1) {
  94.             DEBUGPRINTL;
  95.         }
  96.  
  97.         smi_MemFreeDebug(pCall); //MODIFIED added this free
  98.         pCall = NULL; //MODIFIED added this null asign
  99.  
  100.         return -1;
  101.     } else {
  102.         printf("DEBUG: sendReceive -> smi_SendCall(..): OK \n");
  103.     }
  104.  
  105.     printf("DEBUG: sendReceive waiting for reply...  \n");
  106.  
  107.     /*__________ receive reply from remote software module */
  108.     while (WaitForReply) {
  109.         /* fill &Msg with the received Msg.. */
  110.         ret = smi_Receive(SmiClientInfo.SmiId, &Msg, WAIT_FOREVER); //SmiClientInfo.Timeout);
  111.         WaitForReply = 0;
  112.  
  113.         /* _________REPLY RECEIVED (OK!)  */
  114.         if (ret == 0) {
  115.             MMSCLNT_R_HDR *pReplyHdr = (MMSCLNT_R_HDR *) Msg.Data;
  116.  
  117.             /* Copy reply structure */
  118.             if (ReplyMsg == NULL) {
  119.                 log_Err("%s_SMICLIENT_SEND: No reply structure available !", mmsclnTst_AppName);
  120.                 smi_FreeDataDebug(&Msg);
  121.                 memset(&Msg, 0, sizeof(Msg));       //MODIFIED added this nullassign
  122.  
  123.                 smi_MemFreeDebug(pCall);            //MODIFIED added this free
  124.                 pCall = NULL;                       //MODIFIED added this null asign
  125.  
  126.                 return (-100);
  127.             }
  128.  
  129.             if (pReplyHdr == NULL) {
  130.                 log_Err("pReplyHdr == NULL !");
  131.                 log_Err("ProcRetCode 0x%x, XId %u %u Type 0x%x DataLen %u", Msg.ProcRetCode, (Msg.XId & 0xffff), SmiClientInfo.XId, Msg.Type, Msg.DataLen);
  132.  
  133.                 smi_FreeDataDebug(&Msg);
  134.                 memset(&Msg, 0, sizeof(Msg)); //MODIFIED added this nullassign
  135.  
  136.                 // smi_MemFreeDebug(pCall); //MODIFIED added this free
  137.                 // pCall = NULL; //MODIFIED added this null asign
  138.  
  139.                 return (-102);
  140.             }
  141.  
  142.             /* Check, if it is the correct reply to the request */
  143.             if (pReplyHdr->SrvType != SrvType) {
  144.                 printf("DEBUG: sendReceive waitForReply-Loop got wrong reply to request: \n  request: %s \n  reqReply: %s \n   =>waitForReply=1; continue; \n", SERVICETYPES[SrvType],SERVICETYPES[pReplyHdr->SrvType]);
  145.                 WaitForReply = 1;
  146.                 continue;
  147.             }
  148.  
  149.             memcpy(ReplyMsg, &Msg, sizeof(SMI_MSG));
  150.  
  151.             if (Msg.DataLen > 0) {
  152.                 ReplyMsg->Data = sys_MemPAlloc(mmsclnTst_MemPart, Msg.DataLen); //TODO FIXME
  153.                 if (ReplyMsg->Data == NULL) {
  154.                     log_Err("%s_SMICLIENT_SEND: No memory !", mmsclnTst_AppName);
  155.                     if (mmsclnTst_Debug & APP_DBG_INFO1) {
  156.                         DEBUGPRINTL;
  157.                     }
  158.                     smi_FreeDataDebug(&Msg);
  159.                     memset(&Msg, 0, sizeof(Msg)); //MODIFIED added this nullassign
  160.  
  161.                     smi_MemFreeDebug(pCall); //MODIFIED added this free
  162.                     pCall = NULL; //MODIFIED added this null asign
  163.  
  164.                     return (-101);
  165.                 } else {
  166.                     memcpy(ReplyMsg->Data, Msg.Data, Msg.DataLen);
  167.                 }
  168.             }
  169.  
  170.             /* Free SMI Data. */
  171.             smi_FreeDataDebug(&Msg);
  172.             memset(&Msg, 0, sizeof(Msg)); //MODIFIED added this nullassign
  173.  
  174.         /*____________ REPLY RECEIVED (ERROR = Timeout) */
  175.         } else if (ret == -1) {
  176.             SINT32 ErrorNo = errnoGet();
  177.  
  178.             IpAddr.s_addr = ntohl(pSrvInfo->Cpu);
  179.             inet_ntoa_b(IpAddr, ntoaStr);
  180.  
  181.             if (ErrorNo == S_objLib_OBJ_TIMEOUT) {
  182.                 log_Err("%s_SMICLIENT_SEND: Timeout in smi_Receive() from %s:%s!", mmsclnTst_AppName, pSrvInfo->ModName, ntoaStr);
  183.                 if (mmsclnTst_Debug & APP_DBG_INFO1) {
  184.                     DEBUGPRINTL;
  185.                 }
  186.             } else if (ErrorNo == S_objLib_OBJ_UNAVAILABLE) {
  187.                 log_Err("%s_SMICLIENT_SEND: Object unavailable in smi_Receive() from %s:%s!", mmsclnTst_AppName, pSrvInfo->ModName, ntoaStr);
  188.                 if (mmsclnTst_Debug & APP_DBG_INFO1) {
  189.                     DEBUGPRINTL;
  190.                 }
  191.             } else {
  192.                 log_Err("%s_SMICLIENT_SEND: ErrorNo %ld in smi_Receive() from %s:%s!", mmsclnTst_AppName, ErrorNo, pSrvInfo->ModName, ntoaStr);
  193.                 if (mmsclnTst_Debug & APP_DBG_INFO1) {
  194.                     DEBUGPRINTL;
  195.                 }
  196.             }
  197.  
  198.             pSrvInfo->ModNb = 0; /* reset module number */
  199.  
  200.         /*____________ REPLY RECEIVED (ERROR in Function) */
  201.         } else if (ret == -2) {
  202.             IpAddr.s_addr = ntohl(pSrvInfo->Cpu);
  203.             inet_ntoa_b(IpAddr, ntoaStr);
  204.             log_Err("%s_SMICLIENT_SEND: Queuing error in smi_Receive() from %s:%s!", mmsclnTst_AppName, pSrvInfo->ModName, ntoaStr);
  205.             if (mmsclnTst_Debug & APP_DBG_INFO1) {
  206.                 DEBUGPRINTL;
  207.             }
  208.         /*____________ REPLY RECEIVED (ERROR unknown) */
  209.         } else {
  210.             IpAddr.s_addr = ntohl(pSrvInfo->Cpu);
  211.             inet_ntoa_b(IpAddr, ntoaStr);
  212.             log_Err("%s_SMICLIENT_SEND: unexpected error in smi_Receive()!", mmsclnTst_AppName, pSrvInfo->ModName, ntoaStr);
  213.             if (mmsclnTst_Debug & APP_DBG_INFO1) {
  214.                 DEBUGPRINTL;
  215.             }
  216.         }
  217.     }
  218.     printf("DEBUG: sendReceive() free(&Msg) at %p....  \n", &Msg);
  219.     smi_FreeDataDebug(&Msg); //MODIFIED commented this out...
  220.     //memset(&Msg, 0, sizeof(Msg)); //MODIFIED added this nullassign
  221.  
  222. //  smi_MemFreeDebug(pCall); //MODIFIED added this free
  223. //  pCall = NULL; //MODIFIED added this null asign
  224.     printf("DEBUG: sendReceive() returns now \n");
  225.  
  226.     return (ret);
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement