static void Handle_AddTestAHandler ( le_msg_MessageRef_t _msgRef ) { // Get the message buffer pointer uint8_t* _msgBufPtr = ((_Message_t*)le_msg_GetPayloadPtr(_msgRef))->buffer; // Needed if we are returning a result or output values uint8_t* _msgBufStartPtr = _msgBufPtr; // Unpack the input parameters from the message void* contextPtr; _msgBufPtr = UnpackData( _msgBufPtr, &contextPtr, sizeof(void*) ); // Create a new server data object and fill it in _ServerData_t* serverDataPtr = le_mem_ForceAlloc(_ServerDataPool); serverDataPtr->clientSessionRef = le_msg_GetSession(_msgRef); serverDataPtr->contextPtr = contextPtr; serverDataPtr->handlerRef = NULL; serverDataPtr->removeHandlerFunc = NULL; contextPtr = serverDataPtr; // Define storage for output parameters // Call the function TestAHandlerRef_t _result; _result = AddTestAHandler ( AsyncResponse_AddTestAHandler, contextPtr ); // Put the handler reference result and a pointer to the associated remove function // into the server data object. This function pointer is needed in case the client // is closed and the handlers need to be removed. serverDataPtr->handlerRef = (le_event_HandlerRef_t)_result; serverDataPtr->removeHandlerFunc = (RemoveHandlerFunc_t)RemoveTestAHandler; // Return a safe reference to the server data object as the reference. _LOCK _result = le_ref_CreateRef(_HandlerRefMap, serverDataPtr); _UNLOCK // Re-use the message buffer for the response _msgBufPtr = _msgBufStartPtr; // Pack the result first _msgBufPtr = PackData( _msgBufPtr, &_result, sizeof(_result) ); // Pack any "out" parameters // Return the response LE_DEBUG("Sending response to client session %p : %ti bytes sent", le_msg_GetSession(_msgRef), _msgBufPtr-_msgBufStartPtr); le_msg_Respond(_msgRef); }
static void Handle_TestCallback ( le_msg_MessageRef_t _msgRef ) { // Get the message buffer pointer uint8_t* _msgBufPtr = ((_Message_t*)le_msg_GetPayloadPtr(_msgRef))->buffer; // Needed if we are returning a result or output values uint8_t* _msgBufStartPtr = _msgBufPtr; // Unpack the input parameters from the message uint32_t someParm; _msgBufPtr = UnpackData( _msgBufPtr, &someParm, sizeof(uint32_t) ); size_t dataArrayNumElements; _msgBufPtr = UnpackData( _msgBufPtr, &dataArrayNumElements, sizeof(size_t) ); uint8_t dataArray[dataArrayNumElements]; _msgBufPtr = UnpackData( _msgBufPtr, dataArray, dataArrayNumElements*sizeof(uint8_t) ); void* contextPtr; _msgBufPtr = UnpackData( _msgBufPtr, &contextPtr, sizeof(void*) ); // Create a new server data object and fill it in _ServerData_t* serverDataPtr = le_mem_ForceAlloc(_ServerDataPool); serverDataPtr->clientSessionRef = le_msg_GetSession(_msgRef); serverDataPtr->contextPtr = contextPtr; serverDataPtr->handlerRef = NULL; serverDataPtr->removeHandlerFunc = NULL; contextPtr = serverDataPtr; // Define storage for output parameters // Call the function int32_t _result; _result = TestCallback ( someParm, dataArray, dataArrayNumElements, AsyncResponse_TestCallback, contextPtr ); // Re-use the message buffer for the response _msgBufPtr = _msgBufStartPtr; // Pack the result first _msgBufPtr = PackData( _msgBufPtr, &_result, sizeof(_result) ); // Pack any "out" parameters // Return the response LE_DEBUG("Sending response to client session %p : %ti bytes sent", le_msg_GetSession(_msgRef), _msgBufPtr-_msgBufStartPtr); le_msg_Respond(_msgRef); }
//-------------------------------------------------------------------------------------------------- void msgService_ProcessMessageFromClient ( le_msg_ServiceRef_t serviceRef, ///< [IN] Reference to the Service object. le_msg_MessageRef_t msgRef ///< [IN] Message reference for the received message. ) //-------------------------------------------------------------------------------------------------- { // Pass the message to the server's registered receive handler, if there is one. if (serviceRef->recvHandler != NULL) { // Set the thread-local received message reference so it can be retrieved by the handler. pthread_setspecific(ThreadLocalRxMsgKey, msgRef); // Call the handler function. serviceRef->recvHandler(msgRef, serviceRef->recvContextPtr); // Clear the thread-local reference. pthread_setspecific(ThreadLocalRxMsgKey, NULL); } // Discard the message if no handler is registered. else { LE_WARN("No service receive handler (%s:%s). Discarding message. Closing session.", serviceRef->id.name, le_msg_GetProtocolIdStr(serviceRef->id.protocolRef)); le_msg_DeleteSession(le_msg_GetSession(msgRef)); le_msg_ReleaseMsg(msgRef); } }
static void ServerMsgRecvHandler ( le_msg_MessageRef_t msgRef, void* contextPtr ) { // Get the message payload so that we can get the message "id" _Message_t* msgPtr = le_msg_GetPayloadPtr(msgRef); // Get the client session ref for the current message. This ref is used by the server to // get info about the client process, such as user id. If there are multiple clients, then // the session ref may be different for each message, hence it has to be queried each time. _ClientSessionRef = le_msg_GetSession(msgRef); // Dispatch to appropriate message handler and get response switch (msgPtr->id) { case _MSGID_AddTestAHandler : Handle_AddTestAHandler(msgRef); break; case _MSGID_RemoveTestAHandler : Handle_RemoveTestAHandler(msgRef); break; case _MSGID_allParameters : Handle_allParameters(msgRef); break; case _MSGID_FileTest : Handle_FileTest(msgRef); break; case _MSGID_TriggerTestA : Handle_TriggerTestA(msgRef); break; case _MSGID_AddBugTestHandler : Handle_AddBugTestHandler(msgRef); break; case _MSGID_RemoveBugTestHandler : Handle_RemoveBugTestHandler(msgRef); break; case _MSGID_TestCallback : Handle_TestCallback(msgRef); break; case _MSGID_TriggerCallbackTest : Handle_TriggerCallbackTest(msgRef); break; default: LE_ERROR("Unknowm msg id = %i", msgPtr->id); } // Clear the client session ref associated with the current message, since the message // has now been processed. _ClientSessionRef = 0; }
//-------------------------------------------------------------------------------------------------- void TriggerCallbackTestRespond ( ServerCmdRef_t _cmdRef ) { LE_ASSERT(_cmdRef != NULL); // Get the message related data le_msg_MessageRef_t _msgRef = (le_msg_MessageRef_t)_cmdRef; _Message_t* _msgPtr = le_msg_GetPayloadPtr(_msgRef); __attribute__((unused)) uint8_t* _msgBufPtr = _msgPtr->buffer; // Ensure the passed in msgRef is for the correct message LE_ASSERT(_msgPtr->id == _MSGID_TriggerCallbackTest); // Ensure that this Respond function has not already been called LE_FATAL_IF( !le_msg_NeedsResponse(_msgRef), "Response has already been sent"); // Pack any "out" parameters // Return the response LE_DEBUG("Sending response to client session %p", le_msg_GetSession(_msgRef)); le_msg_Respond(_msgRef); }
//-------------------------------------------------------------------------------------------------- void allParametersRespond ( ServerCmdRef_t _cmdRef, uint32_t b, size_t outputNumElements, uint32_t* outputPtr, char* response, char* more ) { LE_ASSERT(_cmdRef != NULL); // Get the message related data le_msg_MessageRef_t _msgRef = (le_msg_MessageRef_t)_cmdRef; _Message_t* _msgPtr = le_msg_GetPayloadPtr(_msgRef); __attribute__((unused)) uint8_t* _msgBufPtr = _msgPtr->buffer; // Ensure the passed in msgRef is for the correct message LE_ASSERT(_msgPtr->id == _MSGID_allParameters); // Ensure that this Respond function has not already been called LE_FATAL_IF( !le_msg_NeedsResponse(_msgRef), "Response has already been sent"); // Pack any "out" parameters _msgBufPtr = PackData( _msgBufPtr, &b, sizeof(uint32_t) ); _msgBufPtr = PackData( _msgBufPtr, &outputNumElements, sizeof(size_t) ); _msgBufPtr = PackData( _msgBufPtr, outputPtr, outputNumElements*sizeof(uint32_t) ); _msgBufPtr = PackString( _msgBufPtr, response ); _msgBufPtr = PackString( _msgBufPtr, more ); // Return the response LE_DEBUG("Sending response to client session %p", le_msg_GetSession(_msgRef)); le_msg_Respond(_msgRef); }
//-------------------------------------------------------------------------------------------------- static void MsgRecvHandler ( le_msg_MessageRef_t msgRef, ///< Reference to the received message. void* opaqueContextPtr ///< contextPtr passed to le_msg_SetServiceRecvHandler() ) //-------------------------------------------------------------------------------------------------- { Context_t* contextPtr = opaqueContextPtr; LE_ASSERT(contextPtr != NULL); LE_TEST(contextPtr->strPtr == ContextStr); LE_TEST(strcmp(contextPtr->strPtr, ContextStr) == 0); le_msg_SessionRef_t sessionRef = le_msg_GetSession(msgRef); LE_ASSERT(sessionRef != NULL); burger_Message_t* msgPtr = le_msg_GetPayloadPtr(msgRef); LE_ASSERT(msgPtr != NULL); LE_INFO("Received '%x'", msgPtr->payload); switch (msgPtr->payload) { case 0xBEEFBEEF: LE_TEST(le_msg_NeedsResponse(msgRef) == false); le_msg_ReleaseMsg(msgRef); break; case 0xDEADBEEF: LE_TEST(le_msg_NeedsResponse(msgRef) == true); contextPtr->requestCount++; LE_INFO("Received transaction request (%d/%d).", contextPtr->requestCount, contextPtr->maxRequestCount); // Construct and send response. msgPtr->payload = 0xBEEFDEAD; le_msg_Respond(msgRef); // If we have received the magic number of requests, tell the client to // terminate the test by sending 0xDEADDEAD to the client. if (contextPtr->requestCount >= contextPtr->maxRequestCount) { LE_INFO("Maximum number of request-response transactions reached."); msgRef = le_msg_CreateMsg(sessionRef); msgPtr = le_msg_GetPayloadPtr(msgRef); msgPtr->payload = 0xDEADDEAD; le_msg_Send(msgRef); } break; default: LE_FATAL("Unexpected message payload (%x)", msgPtr->payload); } }
static void Handle_RemoveBugTestHandler ( le_msg_MessageRef_t _msgRef ) { // Get the message buffer pointer uint8_t* _msgBufPtr = ((_Message_t*)le_msg_GetPayloadPtr(_msgRef))->buffer; // Needed if we are returning a result or output values uint8_t* _msgBufStartPtr = _msgBufPtr; // Unpack the input parameters from the message BugTestHandlerRef_t addHandlerRef; _msgBufPtr = UnpackData( _msgBufPtr, &addHandlerRef, sizeof(BugTestHandlerRef_t) ); // The passed in handlerRef is a safe reference for the server data object. Need to get the // real handlerRef from the server data object and then delete both the safe reference and // the object since they are no longer needed. _LOCK _ServerData_t* serverDataPtr = le_ref_Lookup(_HandlerRefMap, addHandlerRef); if ( serverDataPtr == NULL ) { _UNLOCK LE_KILL_CLIENT("Invalid reference"); return; } le_ref_DeleteRef(_HandlerRefMap, addHandlerRef); _UNLOCK addHandlerRef = (BugTestHandlerRef_t)serverDataPtr->handlerRef; le_mem_Release(serverDataPtr); // Define storage for output parameters // Call the function RemoveBugTestHandler ( addHandlerRef ); // Re-use the message buffer for the response _msgBufPtr = _msgBufStartPtr; // Pack any "out" parameters // Return the response LE_DEBUG("Sending response to client session %p : %ti bytes sent", le_msg_GetSession(_msgRef), _msgBufPtr-_msgBufStartPtr); le_msg_Respond(_msgRef); }
static void Handle_TriggerCallbackTest ( le_msg_MessageRef_t _msgRef ) { // Get the message buffer pointer uint8_t* _msgBufPtr = ((_Message_t*)le_msg_GetPayloadPtr(_msgRef))->buffer; // Needed if we are returning a result or output values uint8_t* _msgBufStartPtr = _msgBufPtr; // Unpack the input parameters from the message uint32_t data; _msgBufPtr = UnpackData( _msgBufPtr, &data, sizeof(uint32_t) ); // Define storage for output parameters // Call the function TriggerCallbackTest ( data ); // Re-use the message buffer for the response _msgBufPtr = _msgBufStartPtr; // Pack any "out" parameters // Return the response LE_DEBUG("Sending response to client session %p : %ti bytes sent", le_msg_GetSession(_msgRef), _msgBufPtr-_msgBufStartPtr); le_msg_Respond(_msgRef); }
static void Handle_FileTest ( le_msg_MessageRef_t _msgRef ) { // Get the message buffer pointer uint8_t* _msgBufPtr = ((_Message_t*)le_msg_GetPayloadPtr(_msgRef))->buffer; // Needed if we are returning a result or output values uint8_t* _msgBufStartPtr = _msgBufPtr; // Unpack the input parameters from the message int dataFile; dataFile = le_msg_GetFd(_msgRef); // Define storage for output parameters int dataOut; // Call the function FileTest ( dataFile, &dataOut ); // Re-use the message buffer for the response _msgBufPtr = _msgBufStartPtr; // Pack any "out" parameters le_msg_SetFd(_msgRef, dataOut); // Return the response LE_DEBUG("Sending response to client session %p : %ti bytes sent", le_msg_GetSession(_msgRef), _msgBufPtr-_msgBufStartPtr); le_msg_Respond(_msgRef); }
static void Handle_allParameters ( le_msg_MessageRef_t _msgRef ) { // Get the message buffer pointer uint8_t* _msgBufPtr = ((_Message_t*)le_msg_GetPayloadPtr(_msgRef))->buffer; // Needed if we are returning a result or output values uint8_t* _msgBufStartPtr = _msgBufPtr; // Unpack the input parameters from the message common_EnumExample_t a; _msgBufPtr = UnpackData( _msgBufPtr, &a, sizeof(common_EnumExample_t) ); size_t dataNumElements; _msgBufPtr = UnpackData( _msgBufPtr, &dataNumElements, sizeof(size_t) ); uint32_t data[dataNumElements]; _msgBufPtr = UnpackData( _msgBufPtr, data, dataNumElements*sizeof(uint32_t) ); size_t outputNumElements; _msgBufPtr = UnpackData( _msgBufPtr, &outputNumElements, sizeof(size_t) ); if ( outputNumElements > 10 ) { LE_DEBUG("Adjusting outputNumElements from %zd to 10", outputNumElements); outputNumElements = 10; } char label[21]; _msgBufPtr = UnpackString( _msgBufPtr, label, 21 ); size_t responseNumElements; _msgBufPtr = UnpackData( _msgBufPtr, &responseNumElements, sizeof(size_t) ); size_t moreNumElements; _msgBufPtr = UnpackData( _msgBufPtr, &moreNumElements, sizeof(size_t) ); // Define storage for output parameters uint32_t b; uint32_t output[outputNumElements]; char response[responseNumElements]; response[0]=0; char more[moreNumElements]; more[0]=0; // Call the function allParameters ( a, &b, data, dataNumElements, output, &outputNumElements, label, response, responseNumElements, more, moreNumElements ); // Re-use the message buffer for the response _msgBufPtr = _msgBufStartPtr; // Pack any "out" parameters _msgBufPtr = PackData( _msgBufPtr, &b, sizeof(uint32_t) ); _msgBufPtr = PackData( _msgBufPtr, &outputNumElements, sizeof(size_t) ); _msgBufPtr = PackData( _msgBufPtr, output, outputNumElements*sizeof(uint32_t) ); _msgBufPtr = PackString( _msgBufPtr, response ); _msgBufPtr = PackString( _msgBufPtr, more ); // Return the response LE_DEBUG("Sending response to client session %p : %ti bytes sent", le_msg_GetSession(_msgRef), _msgBufPtr-_msgBufStartPtr); le_msg_Respond(_msgRef); }