Exemplo n.º 1
0
void SendTelemetry(Message *msg) {
    if (telemetryOn) {
        if (USART_DMA_transferComleted) {
            Message_ToByteArray(msg, tele);
            len = Message_Size+2;

            Telemetry_DMA_Init();
        }
    }
    
}
Exemplo n.º 2
0
BROKER_RESULT Broker_Publish(BROKER_HANDLE broker, MODULE_HANDLE source, MESSAGE_HANDLE message)
{
    BROKER_RESULT result;
    /*Codes_SRS_BROKER_13_030: [If broker or message is NULL the function shall return BROKER_INVALIDARG.]*/
    if (broker == NULL || source == NULL || message == NULL)
    {
        result = BROKER_INVALIDARG;
        LogError("Broker handle, source, and/or message handle is NULL");
    }
    else
    {
        BROKER_HANDLE_DATA* broker_data = (BROKER_HANDLE_DATA*)broker;
        /*Codes_SRS_BROKER_17_022: [ Broker_Publish shall Lock the modules lock. ]*/
        if (Lock(broker_data->modules_lock) != LOCK_OK)
        {
            /*Codes_SRS_BROKER_13_053: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
            LogError("Lock on broker_data->modules_lock failed");
            result = BROKER_ERROR;
        }
        else
        {
            int32_t msg_size;
            int32_t buf_size;
            /*Codes_SRS_BROKER_17_007: [ Broker_Publish shall clone the message. ]*/
            MESSAGE_HANDLE msg = Message_Clone(message);
            /*Codes_SRS_BROKER_17_008: [ Broker_Publish shall serialize the message. ]*/
            msg_size = Message_ToByteArray(message, NULL, 0);
            if (msg_size < 0)
            {
                /*Codes_SRS_BROKER_13_053: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
                LogError("unable to serialize a message [%p]", msg);
                Message_Destroy(msg);
                result = BROKER_ERROR;
            }
            else
            {
                /*Codes_SRS_BROKER_17_025: [ Broker_Publish shall allocate a nanomsg buffer the size of the serialized message + sizeof(MODULE_HANDLE). ]*/
                buf_size = msg_size + sizeof(MODULE_HANDLE);
                void* nn_msg = nn_allocmsg(buf_size, 0);
                if (nn_msg == NULL)
                {
                    /*Codes_SRS_BROKER_13_053: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
                    LogError("unable to serialize a message [%p]", msg);
                    result = BROKER_ERROR;
                }
                else
                {
                    /*Codes_SRS_BROKER_17_026: [ Broker_Publish shall copy source into the beginning of the nanomsg buffer. ]*/
                    unsigned char *nn_msg_bytes = (unsigned char *)nn_msg;
                    memcpy(nn_msg_bytes, &source, sizeof(MODULE_HANDLE));
                    /*Codes_SRS_BROKER_17_027: [ Broker_Publish shall serialize the message into the remainder of the nanomsg buffer. ]*/
                    nn_msg_bytes += sizeof(MODULE_HANDLE);
                    Message_ToByteArray(message, nn_msg_bytes, msg_size);

                    /*Codes_SRS_BROKER_17_010: [ Broker_Publish shall send a message on the publish_socket. ]*/
                    int nbytes = nn_send(broker_data->publish_socket, &nn_msg, NN_MSG, 0);
                    if (nbytes != buf_size)
                    {
                        /*Codes_SRS_BROKER_13_053: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
                        LogError("unable to send a message [%p]", msg);
                        /*Codes_SRS_BROKER_17_012: [ Broker_Publish shall free the message. ]*/
                        nn_freemsg(nn_msg);
                        result = BROKER_ERROR;
                    }
                    else
                    {
                        result = BROKER_OK;
                    }
                }
                /*Codes_SRS_BROKER_17_012: [ Broker_Publish shall free the message. ]*/
                Message_Destroy(msg);
                /*Codes_SRS_BROKER_17_011: [ Broker_Publish shall free the serialized message data. ]*/
            }
            /*Codes_SRS_BROKER_17_023: [ Broker_Publish shall Unlock the modules lock. ]*/
            Unlock(broker_data->modules_lock);
        }

    }
    /*Codes_SRS_BROKER_13_037: [ This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise. ]*/
    return result;
}
Exemplo n.º 3
0
/* Codes_SRS_BROKER_17_026: [ N/A - Broker_Publish shall copy source into the beginning of the nanomsg buffer. ] */
BROKER_RESULT
Broker_Publish (
    BROKER_HANDLE broker,
    MODULE_HANDLE source,
    MESSAGE_HANDLE message
) {
    (void)source;
    REMOTE_MODULE_HANDLE remote_module = (REMOTE_MODULE_HANDLE)broker;
    BROKER_RESULT result;

    /* Codes_SRS_BROKER_13_030: [If broker or message is NULL the function shall return BROKER_INVALIDARG.] */
    if (broker == NULL || message == NULL)
    {
        result = BROKER_INVALIDARG;
        LogError("Broker handle and/or message handle is NULL");
    }
    else
    {
        // Send message_ to nanomsg
        int32_t msg_size;
        int32_t buf_size;
        /* Codes_SRS_BROKER_17_007: [ Broker_Publish shall clone the message. ] */
        MESSAGE_HANDLE msg = Message_Clone(message);
        /* Codes_SRS_BROKER_17_008: [ Broker_Publish shall serialize the message. ] */
        msg_size = Message_ToByteArray(message, NULL, 0);
        if (msg_size < 0)
        {
            /* Codes_SRS_BROKER_13_037: [ This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise. ] */
            LogError("unable to serialize a message [%p]", msg);
            Message_Destroy(msg);
            result = BROKER_ERROR;
        }
        else
        {
            /* Codes_SRS_BROKER_17_025: [ Broker_Publish shall allocate a nanomsg buffer the size of the serialized message + sizeof(MODULE_HANDLE). ] */
            buf_size = msg_size;
            void* nn_msg = nn_allocmsg(buf_size, 0);
            if (nn_msg == NULL)
            {
                /* Codes_SRS_BROKER_13_037: [ This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise. ] */
                LogError("unable to serialize a message [%p]", msg);
                result = BROKER_ERROR;
            }
            else
            {
                unsigned char *nn_msg_bytes = (unsigned char *)nn_msg;
                /* Codes_SRS_BROKER_17_027: [ Broker_Publish shall serialize the message into the remainder of the nanomsg buffer. ] */
                Message_ToByteArray(message, nn_msg_bytes, msg_size);

                /* Codes_SRS_BROKER_17_010: [ Broker_Publish shall send a message on the publish_socket. ] */
                int nbytes = nn_really_send(remote_module->message_socket, &nn_msg, NN_MSG, 0);
                if (nbytes != buf_size)
                {
                    /* Codes_SRS_BROKER_13_037: [ This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise. ] */
                    LogError("unable to send a message [%p]", msg);
                    /* Codes_SRS_BROKER_17_012: [ Broker_Publish shall free the message. ] */
                    nn_freemsg(nn_msg);
                    result = BROKER_ERROR;
                }
                else
                {
                    result = BROKER_OK;
                }
            }
            /* Codes_SRS_BROKER_17_012: [ Broker_Publish shall free the message. ] */
            Message_Destroy(msg);
            /* Codes_SRS_BROKER_17_011: [ Broker_Publish shall free the serialized message data. ] */
        }

    }

    /* Codes_SRS_BROKER_13_037: [ This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise. ] */
    return result;
}
static int outprocessOutgoingMessagesThread(void * param)
{
	OUTPROCESS_HANDLE_DATA * handleData = (OUTPROCESS_HANDLE_DATA*)param;
	if (handleData == NULL)
	{
		LogError("outprocess send message thread: parameter is NULL");
	}
	else
	{
		int should_continue = 1;

		while (should_continue)
		{
			/*Codes_SRS_OUTPROCESS_MODULE_17_053: [ This thread shall ensure thread safety on the module data. ]*/
			if (Lock(handleData->message_send_thread.thread_lock) != LOCK_OK)
			{
				LogError("unable to Lock");
				should_continue = 0;
				break;
			}
			if (handleData->message_send_thread.thread_flag == THREAD_FLAG_STOP)
			{
				should_continue = 0;
				(void)Unlock(handleData->message_send_thread.thread_lock);
				break;
			}
			if (Unlock(handleData->message_send_thread.thread_lock) != LOCK_OK)
			{
				should_continue = 0;
				break;
			}
			MESSAGE_HANDLE messageHandle;
			/*Codes_SRS_OUTPROCESS_MODULE_17_053: [ This thread shall ensure thread safety on the module data. ]*/
			if (Lock(handleData->handle_lock) != LOCK_OK)
			{
				LogError("unable to Lock");
				should_continue = 0;
				break;
			}
			
			if (MESSAGE_QUEUE_is_empty(handleData->outgoing_messages))
			{
				messageHandle = NULL;
			}
			else
			{
				/*Codes_SRS_OUTPROCESS_MODULE_17_054: [ This function shall remove the oldest message from the outgoing gateway message queue. ]*/
				messageHandle = MESSAGE_QUEUE_pop(handleData->outgoing_messages);
				if (messageHandle == NULL)
				{
					LogError("bad condition: message handle in queue is NULL");
					(void)Unlock(handleData->handle_lock);
					should_continue = 0;
					break;
				}
			}
			if (Unlock(handleData->handle_lock) != LOCK_OK)
			{
				should_continue = 0;
				break;
			}

			/* forward message to remote */
			if (messageHandle != NULL)
			{
				/*Codes_SRS_OUTPROCESS_MODULE_17_023: [ This function shall serialize the message for transmission on the message channel. ]*/
				int32_t msg_size = Message_ToByteArray(messageHandle, NULL, 0);
				if (msg_size < 0)
				{
					LogError("unable to serialize outgoing message [%p]", messageHandle);
				}
				else
				{
					void* result = nn_allocmsg(msg_size, 0);
					if (result == NULL)
					{
						LogError("unable to allocate buffer for outgoing message [%p]", messageHandle);
					}
					else
					{
						unsigned char *nn_msg_bytes = (unsigned char *)result;
						Message_ToByteArray(messageHandle, nn_msg_bytes, msg_size);
						/*Codes_SRS_OUTPROCESS_MODULE_17_024: [ This function shall send the message on the message channel. ]*/
						int nbytes = nn_really_send(handleData->message_socket, &result, NN_MSG, 0);
						if (nbytes != msg_size)
						{
							LogError("unable to send buffer to remote for message [%p]", messageHandle);
							/*Codes_SRS_OUTPROCESS_MODULE_17_025: [ This function shall free any resources created. ]*/
							nn_freemsg(result);
						}
					}
				}
				// We are finally finished with this message
				/*Codes_SRS_OUTPROCESS_MODULE_17_055: [ This function shall Destroy the message once successfully transmitted. ]*/
				Message_Destroy(messageHandle);
			}
			ThreadAPI_Sleep(1);
		}
	}
	return 0;
}