Example #1
0
void uart_init(void) {

#define BAUD UART_BPS
//defines UBRRL_VALUE, UBRRH_VALUE and USE_2X
#include <util/setbaud.h>

	outputBuf = atomq_alloc(UART_OUTPUT_BUF_LEN, sizeof(char));
	inputBuf = atomq_alloc(UART_INPUT_BUF_LEN, sizeof(char));

	outputBuf->cbDidEnqueue = uart_outputBuf_didEnqueue;

	UBRR0H = UBRRH_VALUE;
    UBRR0L = UBRRL_VALUE;

#if USE_2X
    UCSR0A |= _BV(U2X0);
#else
    UCSR0A &= ~(_BV(U2X0));
#endif

    UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
    UCSR0B = _BV(RXEN0) | _BV(TXEN0);   /* Enable RX and TX */

    message_set_buffer(outputBuf);
    command_set_buffer(inputBuf);

    UART_RXC_ENABLE;

#undef BAUD
}
Example #2
0
int32_t _broadcast_direct( struct iolayer * self, uint8_t index, struct session_manager * manager, struct message * msg )
{
    uint32_t i = 0;
    int32_t count = 0;

    // 数据改造
    if ( self->transform != NULL )
    {
        // 数据需要改造
        char * buffer = NULL;
        uint32_t nbytes = message_get_length( msg );
        buffer = self->transform( self->context, message_get_buffer(msg), &nbytes );
        if ( buffer == NULL )
        {
            // 数据改造失败
            message_destroy( msg );
            return -1;
        }
        if ( buffer != message_get_buffer(msg) )
        {
            // 数据改造成功
            message_set_buffer( msg, buffer, nbytes );
        }
    }

    for ( i = 0; i < sidlist_count(msg->tolist); ++i )
    {
        sid_t id = sidlist_get(msg->tolist, i);

        if ( SID_INDEX(id) != index )
        {
            message_add_failure( msg, id );
            continue;
        }

        struct session * session = session_manager_get( manager, id );
        if ( unlikely(session == NULL) )
        {
            message_add_failure( msg, id );
            continue;
        }

        if ( session_append(session, msg) >= 0 )
        {
            // 尝试单独发送
            // 添加到发送队列成功
            ++count;
        }
    }

    // 消息发送完毕, 直接销毁
    if ( message_is_complete(msg) )
    {
        message_destroy( msg );
    }

    return count;
}