Example #1
0
static void ss_stream_notify(void *opaque)
{
    SlaveBootInt *s = SBI(opaque);
    uint32_t num = 0;
    uint8_t *data;

    while (stream_can_push(s->tx_dev, ss_stream_notify, s)) {
        if (fifo_is_empty(&s->fifo)) {
            break;
        }
        /* num is equal to number of bytes read as its a fifo of width 1byte.
         * the same dosent holds good if width is grater than 1 byte
         */
        data = (uint8_t *) fifo_pop_buf(&s->fifo,
                        4, &num);

        stream_push(s->tx_dev, data, num, 0);
    }
    ss_update_busy_line(s);
    sbi_update_irq(s);
}
bool message_to_stream(uint32_t streamid, stream_t *stream, void *userdata)
{
    struct topic_message *tm = (struct topic_message *)userdata;
    char *s = NULL;

    LOG_DEBUG("rx check: %s %s", stream->topic_patt, tm->topic);

    if (topic_match_string(stream->topic_patt, tm->topic))
    {
        size_t len;

        LOG_DEBUG("%s:%s -> stream %u", tm->topic, tm->msg, streamid);

        len = strlen(tm->topic)*2 + strlen(tm->msg)*2 + 8;  // {"":""} + null // FIXME

        if (NULL == (s = (char *)malloc(len)))
            LOG_ERROR("out of mem");
        else
        {
            uint32_t connid;
            ebb_connection_info *conninfo = NULL;
            bool connected;

            connected = stream_get_connection(stream, &connid);

            if (connected)
            {
                if (NULL == (conninfo = httpd_get_conninfo(connid)))
                {
                    LOG_ERROR("bad connid");
                    return false;
                }
                if (conninfo->finished) // if conn is in process of closing, 
                    connected = false;
            }

            if (conninfo && conninfo->rawmode)
            {
                // just the data
                memcpy(s, tm->msg, strlen(tm->msg)+1);
            }
            else
            {
                // wrapped in JSON obj
                cJSON *mj = NULL;
                char *msgesc;
                char *topicesc;

                if (NULL == (topicesc = json_escape(tm->topic)))
                {
                    conninfo->http_status = 503;
                    httpd_close(connid);
                    return false;
                }

                if (tm->msg[0] == 0)
                {
                    snprintf(s, len, "{%s:\"\"}", topicesc);
                }
                else
                if (0==strcmp(tm->msg, "inf"))
                {
                    snprintf(s, len, "{%s:\"inf\"}", topicesc);
                }
                else
                if (NULL != (mj = cJSON_Parse(tm->msg)))
                {
                    snprintf(s, len, "{%s:%s}", topicesc, cJSON_PrintUnformatted(mj));
                    cJSON_Delete(mj);
                }
                else
                {
                    if (NULL == (msgesc = json_escape(tm->msg)))
                    {
                        conninfo->http_status = 503;
                        httpd_close(connid);
                        free(topicesc);
                        return false;
                    }
                    else
                    {
                        snprintf(s, len, "{%s:%s}", topicesc, msgesc);
                        free(msgesc);
                    }
                }
                free(topicesc);
            }
            if (0 != stream_push(stream, s))
                LOG_ERROR("stream_push %u failed", streamid);
            else
            {
                if (connected)
                {
                    if (!conninfo->rawmode)
                        httpd_printf(connid, "[");
                    stream_drain(stream, stream_drainer, (void *)(uintptr_t)connid);
                    if (!conninfo->rawmode)
                        httpd_printf(connid, "]");
                    stream_clear_connection(stream);
                    httpd_close(connid);
                }
                else
                {
                    LOG_DEBUG("streamid %u not connected", stream->streamid);
                }
            }
        }
    }
    if (NULL != s)
        free(s);
    return false;
}