void verify_bufhdr(OMX_BUFFERHEADERTYPE* c)
{
    pthread_mutex_lock(&list_mutex);
        int idx = get_bufhdr_idx(c);
    pthread_mutex_unlock(&list_mutex);
    if (idx == -1) {
        fprintf(stderr, "verify_bufhdr failed for %X\n", c);
        error_found();
    }
}
void verify_component_and_keep_lock(struct comp* c)
{
    pthread_mutex_lock(&list_mutex);
        int idx = get_component_idx(c);

    if (idx == -1) {
        fprintf(stderr, "verify_component failed for %X\n", c);
        error_found();
    }
}
예제 #3
0
tParams * parser_parse(char * buff) {
	error_reset();
	parser_generate_args(buff);
	if(!error_found()) {
		params.args_amount = args_amount;
		params.args = args;
		return &params;
	} else
		return NULL;
}
static void send_message(int fd, struct msg* msg)
{
    pthread_mutex_lock(&il_srv_fd_mutex);
    int t=0;
    while (t < (int) sizeof(*msg)) {
        int w = send(fd, ((char*)msg)+t, sizeof(*msg)-t, MSG_NOSIGNAL);
        if (w == -1) {
            fprintf(stderr, "send of event: Socket %d not working (error: '%s')\n", fd, strerror(errno));
            error_found();
        }
        t += w;
    }
    pthread_mutex_unlock(&il_srv_fd_mutex);
}
void add_component(struct comp* c)
{
    pthread_mutex_lock(&list_mutex);
        if (num_valid_comps >= MAX_COMPONENTS) { fprintf(stderr, "Too many components\n"); error_found(); }
        valid_comps[num_valid_comps++] = c;
    pthread_mutex_unlock(&list_mutex);
}
static void handle_msg(int fd, struct msg* m)
{
    switch (m->type) {
    case MSG_GET_HANDLE: {
        struct comp* c = (struct comp*) malloc(sizeof(struct comp));
        fprintf(stderr, "ILSRV: GetHandle '%s'\n", m->cmd.get_handle.name);
        m->result = OMX_GetHandle(&c->real_handle, m->cmd.get_handle.name, c, &callbacks);
        m->cmd.get_handle.out_srv_handle = (int) c;
        c->client_handle = m->cmd.get_handle.in_client_handle;
        c->client_fd     = fd;
        c->state         = OMX_StateLoaded;
        strncpy(c->name, m->cmd.get_handle.name, sizeof(c->name));
        c->name[sizeof(c->name)-1]=0;
        add_component(c);
        fprintf(stderr, "ILSRV: %08X [%20s] --> %s%s\n", c->real_handle, last_part(c->name), m->result == OMX_ErrorNone ? "" : "ERROR ", adm_log_err2str(m->result));
        return;
    }

    case MSG_SET_PARAM:{
        struct comp* c = (struct comp*) m->cmd.param.srv_handle;
        verify_component(c);
        fprintf(stderr, "ILSRV: %08X [%20s] SetParam %s\n", c->real_handle, last_part(c->name), adm_log_index2str(m->cmd.param.index));
        m->result = OMX_SetParameter(c->real_handle, (OMX_INDEXTYPE) m->cmd.param.index, m->cmd.param.buf); // TODO: Protect
        fprintf(stderr, "ILSRV: --> %s %s\n", m->result == OMX_ErrorNone ? "" : "ERROR ", adm_log_err2str(m->result));
        return;
    }
    case MSG_GET_PARAM:{
        struct comp* c = (struct comp*) m->cmd.param.srv_handle;
        verify_component(c);
        fprintf(stderr, "ILSRV: %08X [%20s] GetParam %s\n", c->real_handle, last_part(c->name), adm_log_index2str(m->cmd.param.index));
        m->result = OMX_GetParameter(c->real_handle, (OMX_INDEXTYPE)m->cmd.param.index, m->cmd.param.buf); // TODO: Protect
        fprintf(stderr, "ILSRV: --> %s %s\n", m->result == OMX_ErrorNone ? "" : "ERROR ", adm_log_err2str(m->result));
        return;
    }

    case MSG_GET_CONFIG:{
        struct comp* c = (struct comp*) m->cmd.param.srv_handle;
        verify_component(c);
        fprintf(stderr, "ILSRV: %08X [%20s] GetConfig %s\n", c->real_handle, last_part(c->name), adm_log_index2str(m->cmd.param.index));
        m->result = OMX_GetConfig(c->real_handle, (OMX_INDEXTYPE)m->cmd.param.index, m->cmd.param.buf); // TODO: Protect
        fprintf(stderr, "ILSRV: --> %s %s\n", m->result == OMX_ErrorNone ? "" : "ERROR ", adm_log_err2str(m->result));
        return;
    }

    case MSG_SET_CONFIG: {
        struct comp* c = (struct comp*) m->cmd.param.srv_handle;
        verify_component(c);
        fprintf(stderr, "ILSRV: %08X [%20s] SetConfig %s\n", c->real_handle, last_part(c->name), adm_log_index2str(m->cmd.param.index));
        m->result = OMX_SetConfig(c->real_handle, (OMX_INDEXTYPE)m->cmd.param.index, m->cmd.param.buf); // TODO: Protect
        fprintf(stderr, "ILSRV: --> %s %s\n", m->result == OMX_ErrorNone ? "" : "ERROR ", adm_log_err2str(m->result));
        return;
    }

    case MSG_FREE_HANDLE: {
        struct comp* c = (struct comp*) m->cmd.free_handle.srv_handle;
        verify_component(c);
        if (c->state != OMX_StateLoaded) {
            fprintf(stderr, "ILSRV: FreeHandle called for %X which is in wrong state (%s)\m", c->real_handle, adm_log_state2str(c->state));
            error_found();
        }
        fprintf(stderr, "ILSRV: %08X [%20s] Freehandle (internal=%X)\n", c->real_handle, last_part(c->name), c);
        m->result = OMX_FreeHandle(c->real_handle);
        fprintf(stderr, "ILSRV: --> %s %s\n", m->result == OMX_ErrorNone ? "" : "ERROR ", adm_log_err2str(m->result));
        del_component(c);
        memset(c, 0xFE, sizeof(*c));
        free(c);
        return;
    }

    case MSG_SEND_COMMAND: {
        struct comp* c = (struct comp*) m->cmd.free_handle.srv_handle;
        verify_component(c);
        if (m->cmd.command.cmd == OMX_CommandStateSet) {
            fprintf(stderr, "ILSRV: %08X [%20s] SendCommand StateSet %s\n", c->real_handle, last_part(c->name), adm_log_state2str((OMX_STATETYPE) m->cmd.command.nParam1));
        } else {
            fprintf(stderr, "ILSRV: %08X [%20s] SendCommand StateSet %s 0x%X\n", c->real_handle, last_part(c->name), adm_log_cmd2str(m->cmd.command.cmd), m->cmd.command.nParam1);
        }

        m->result = OMX_SendCommand(c->real_handle, m->cmd.command.cmd, m->cmd.command.nParam1, 0);
        fprintf(stderr, "ILSRV: --> %s %s\n", m->result == OMX_ErrorNone ? "" : "ERROR ", adm_log_err2str(m->result));
        return;
    }

    case MSG_SETUP_TUNNEL: {
        struct comp* c_in = (struct comp*) m->cmd.tunnel.srv_handle_in;
        struct comp* c_out = (struct comp*) m->cmd.tunnel.srv_handle_out;
        verify_component(c_in);
        verify_component(c_out);

        fprintf(stderr, "ILSRV: SetupTunnel %08X [%20s].%d --> %08X [%20s].%d\n", c_out->real_handle, last_part(c_out->name), m->cmd.tunnel.port_out, c_in->real_handle, last_part(c_in->name), m->cmd.tunnel.port_in);
        m->result = OMX_SetupTunnel(c_out->real_handle, m->cmd.tunnel.port_out, c_in->real_handle, m->cmd.tunnel.port_in);
        fprintf(stderr, "ILSRV: --> %s %s\n", m->result == OMX_ErrorNone ? "" : "ERROR ", adm_log_err2str(m->result));
        return;
    }

    case MSG_GET_STATE: {
        struct comp* c = (struct comp*) m->cmd.get_state.in_srv_handle;
        verify_component(c);
        fprintf(stderr, "ILSRV: %08X [%20s] GetState\n", c->real_handle, last_part(c->name));
        m->result = OMX_GetState(c->real_handle, &m->cmd.get_state.out_state);
        fprintf(stderr, "ILSRV: --> %s %s %s\n", m->result == OMX_ErrorNone ? "" : "ERROR ", adm_log_err2str(m->result), adm_log_state2str(m->cmd.get_state.out_state));
        return;
    }

    case MSG_ALLOC_BUFFER: {
        struct comp* c = (struct comp*) m->cmd.alloc_buffer.in_srv_handle;
        verify_component(c);

        OMX_BUFFERHEADERTYPE* bufhdr;
        fprintf(stderr, "ILSRV: %08X [%20s] AllocBuf port=%d size=%d\n", c->real_handle, last_part(c->name), m->cmd.alloc_buffer.port, m->cmd.alloc_buffer.size);
        m->result = OMX_AllocateBuffer(c->real_handle, &bufhdr, m->cmd.alloc_buffer.port,
                     (void*) m->cmd.alloc_buffer.in_client_buf_hdr, m->cmd.alloc_buffer.size);

        if (m->result == OMX_ErrorNone) {
            memcpy(&m->cmd.alloc_buffer.out_buf_hdr, bufhdr, sizeof(OMX_BUFFERHEADERTYPE));
            m->cmd.alloc_buffer.out_srv_buf_hdr = (int) bufhdr;
            add_bufhdr(bufhdr);
        }
        fprintf(stderr, "ILSRV: --> %s %s\n", m->result == OMX_ErrorNone ? "" : "ERROR ", adm_log_err2str(m->result));
        return;
    }

    case MSG_EMPTY_BUFFER: {
        struct comp* c = (struct comp*) m->cmd.empty_buffer.in_srv_handle;
        verify_component(c);

        OMX_BUFFERHEADERTYPE* client_bufhdr = (OMX_BUFFERHEADERTYPE*) &m->cmd.empty_buffer.in_buf_hdr;
        OMX_BUFFERHEADERTYPE* bufhdr = (OMX_BUFFERHEADERTYPE*) client_bufhdr->pPlatformPrivate;

        verify_bufhdr(bufhdr);

        if (bufhdr->nAllocLen <= sizeof(m->cmd.empty_buffer.in_buf)) {
            bufhdr->nOffset    = client_bufhdr->nOffset;
            bufhdr->nTimeStamp = client_bufhdr->nTimeStamp;
            bufhdr->nFilledLen = client_bufhdr->nFilledLen;
            bufhdr->nFlags     = client_bufhdr->nFlags;

            memcpy(bufhdr->pBuffer, &m->cmd.empty_buffer.in_buf, bufhdr->nAllocLen);
            fprintf(stderr, "ILSRV: %08X [%20s] EmptyBuffer port=%d size=%d\n", c->real_handle, last_part(c->name), bufhdr->nInputPortIndex, bufhdr->nAllocLen);
            m->result = OMX_EmptyThisBuffer(c->real_handle, bufhdr);
            fprintf(stderr, "ILSRV: --> %s %s\n", m->result == OMX_ErrorNone ? "" : "ERROR ", adm_log_err2str(m->result));
        } else {
            assert(0);
        }

        return;
    }

    case MSG_FILL_BUFFER: {
        struct comp* c = (struct comp*) m->cmd.empty_buffer.in_srv_handle;
        verify_component(c);

        OMX_BUFFERHEADERTYPE* client_bufhdr = (OMX_BUFFERHEADERTYPE*) &m->cmd.fill_buffer.in_buf_hdr;
        OMX_BUFFERHEADERTYPE* bufhdr = (OMX_BUFFERHEADERTYPE*) client_bufhdr->pPlatformPrivate;

        verify_bufhdr(bufhdr);

        fprintf(stderr, "ILSRV: %08X [%20s] FillBuffer port=%d size=%d\n", c->real_handle, last_part(c->name), bufhdr->nOutputPortIndex, bufhdr->nAllocLen);
        m->result = OMX_FillThisBuffer(c->real_handle, bufhdr);
        fprintf(stderr, "ILSRV: --> %s %s\n", m->result == OMX_ErrorNone ? "" : "ERROR ", adm_log_err2str(m->result));

        return;
    }

    case MSG_FREE_BUFFER: {
        struct comp* c = (struct comp*) m->cmd.empty_buffer.in_srv_handle;
        verify_component(c);

        OMX_BUFFERHEADERTYPE* bufhdr = (OMX_BUFFERHEADERTYPE*) m->cmd.free_buffer.in_srv_buf_hdr;
        verify_bufhdr(bufhdr);

        del_bufhdr(bufhdr);
        fprintf(stderr, "ILSRV: %08X [%20s] FreeBuffer\n", c->real_handle, last_part(c->name));
        m->result = OMX_FreeBuffer(c->real_handle, m->cmd.free_buffer.in_port, bufhdr);
        fprintf(stderr, "ILSRV: --> %s %s\n", m->result == OMX_ErrorNone ? "" : "ERROR ", adm_log_err2str(m->result));
        return;
    }

    default:
       fprintf(stderr, "ILSRV: Unhandled request received\n");
       m->result = OMX_ErrorNotImplemented;
       error_found();
       return;
    }
int main()
{
    fprintf(stderr, "IL server starting up\n");
    OMX_Init();

    struct sockaddr_un local_sockaddr_un;
    int listener_sck_fd = socket(AF_UNIX, SOCK_STREAM, 0);
    memset(&local_sockaddr_un, 0, sizeof(local_sockaddr_un));
    local_sockaddr_un.sun_family = AF_UNIX;
    strcpy(local_sockaddr_un.sun_path, "/tmp/il-server-socket");

    /* Unlink any previous socket */
    unlink("/tmp/il-server-socket");

    if (bind(listener_sck_fd,
         (struct sockaddr *) &local_sockaddr_un,
         sizeof(local_sockaddr_un)) < 0) {
        fprintf(stderr, "bind() %s\n", strerror(errno));
        error_found();
    }

    if (listen(listener_sck_fd, 10)) {
        fprintf(stderr, "listen() %s\n", strerror(errno));
        error_found();
    }

    fd_set master_socket_fds;
    int highest_fd = listener_sck_fd;
    FD_ZERO(&master_socket_fds);
    FD_SET(listener_sck_fd, &master_socket_fds);

    signal(SIGPIPE, SIG_IGN);

    for (;;) {
        int num_active_fds;
        fd_set tmp_read_socket_fds;
        tmp_read_socket_fds = master_socket_fds;

        /* Poll for new requests on the FD_sets */
        num_active_fds =
            select(highest_fd+1, &tmp_read_socket_fds, NULL, NULL, NULL);

        if (num_active_fds == -1) {
            fprintf(stderr, "num_active_fds == -1\n");
            exit(10);
            }

        /* If listener socket is ready for read, this means a
        client is trying to connect to it */
        if (FD_ISSET(listener_sck_fd, &tmp_read_socket_fds)) {
            int client_sck_fd;

            /* Try to accept the client connection. We need to always accept
            and then close connection if we have an overflow of clients */
            client_sck_fd = accept(listener_sck_fd,
                                    NULL,/* Don't care remote address */
                                    NULL /* Don't care remote size */);

            if (listener_sck_fd == -1) {
                fprintf(stderr, "accept() %s\n", strerror(errno));
                error_found();
            }

            /* check if we need to update upper bound used by the select()
            function */
            if (client_sck_fd > highest_fd) {
                highest_fd = client_sck_fd;
            }

            /* add the socket descriptor to the master set */
            FD_SET(client_sck_fd, &master_socket_fds);
            fprintf(stderr, "New client connected; got fd=%d\n", client_sck_fd);
            continue;
        }

        int cur_fd;
        for (cur_fd=3 ; cur_fd <= highest_fd ; cur_fd++) {
            if (FD_ISSET(cur_fd, &tmp_read_socket_fds)) {
                handle_request(cur_fd);
            }
        }
    }

    return 0;
}
OMX_ERRORTYPE ste_adm_omx_callback_event_handler(
                            OMX_IN OMX_HANDLETYPE   component_handle,
                            OMX_IN OMX_PTR          app_data_p,
                            OMX_IN OMX_EVENTTYPE    ev,
                            OMX_IN OMX_U32          data1,
                            OMX_IN OMX_U32          data2,
                            OMX_IN OMX_PTR          event_data_p)
{
    if (event_data_p != NULL) { fprintf(stderr, "Received event with event_data_p != NULL\n"); error_found(); }

    fprintf(stderr, "ILSRV: Received event: %X, %X, %X, %X, %X, %X\n", component_handle, app_data_p,
           ev, data1, data2, event_data_p);

    struct comp* c = (struct comp*) app_data_p;
    verify_component_and_keep_lock(c);


    struct msg m;
    memset(&m,0,sizeof(m));
    m.type = MSG_EVENT;
    m.cmd.event.client_handle = c->client_handle;
    m.cmd.event.ev            = ev;
    m.cmd.event.nData1        = data1;
    m.cmd.event.nData2        = data2;


    if (ev == OMX_EventCmdComplete) {
        if (data1 == OMX_CommandStateSet) {
            c->state = (OMX_STATETYPE) data2;
            fprintf(stderr, "ILSRV: %08X [%20s] Dispatching CmdComplete StateSet %s\n", component_handle, last_part(c->name), adm_log_state2str((OMX_STATETYPE) data2));
        } else  if (data1 == OMX_CommandPortDisable) {
            fprintf(stderr, "ILSRV: %08X [%20s] Dispatching CmdComplete PortDisable %d\n", component_handle, last_part(c->name), data2);
        } else  if (data1 == OMX_CommandPortEnable) {
            fprintf(stderr, "ILSRV: %08X [%20s] Dispatching CmdComplete PortEnable %d\n", component_handle, last_part(c->name), data2);
        } else {
            fprintf(stderr, "ILSRV: %08X [%20s] Dispatching %s nData2=%X\n", component_handle, last_part(c->name), adm_log_cmd2str((OMX_COMMANDTYPE) data1), data2);
        }
    } else if (ev == OMX_EventError) {
        fprintf(stderr, "ILSRV: %08X [%20s] Dispatching EventError %s nData2=%d\n", component_handle, last_part(c->name), adm_log_err2str((OMX_ERRORTYPE) data1), data2);
    } else {
        fprintf(stderr, "ILSRV: %08X [%20s] Dispatching Event %s nData1=%X nData2=%X\n", component_handle, last_part(c->name), adm_log_event2str(ev), data1,data2);
    }

    send_message(c->client_fd, &m);
    pthread_mutex_unlock(&list_mutex);

    struct timeval tv;
    gettimeofday(&tv, NULL);
    fprintf(stderr, "ILSRV: (%d.%d) Ready to dispatch new events\n", tv.tv_sec, tv.tv_usec/1000);

    return OMX_ErrorNone; // Pointless
}
void add_bufhdr(OMX_BUFFERHEADERTYPE* c)
{
    pthread_mutex_lock(&list_mutex);
        if (num_valid_bufhdrs >= MAX_BUFHDRS) { fprintf(stderr, "Too many buffers\n"); error_found(); }
        valid_bufhdrs[num_valid_bufhdrs++] = c;
    pthread_mutex_unlock(&list_mutex);
}