summaryrefslogtreecommitdiff
path: root/portland/dapi/lib/callbacks.c
blob: 62f402dfaa81f02da4ced6f56c67cd9cb3b00474 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "callbacks.h"

#include <stdio.h>
#include <stdlib.h>

#include "comm_internal.h"

#include <dapi/callbacks_generated.c>

DapiGenericCallback dapi_setGenericCallback( DapiConnection* conn, DapiGenericCallback callback )
    {
    DapiGenericCallback ret = conn->generic_callback;
    conn->generic_callback = callback;
    return ret;
    }

void dapi_processData( DapiConnection* conn )
    {
    while( dapi_hasData( conn ))
        {
        int command;
        int seq;
        if( !dapi_readCommand( conn, &command, &seq ))
            return; /* TODO error-handling? */
        conn->generic_callback( conn, command, seq );
        }
    }

void dapi_genericCallback( DapiConnection* conn, int command, int seq )
    {
    DapiCallbackData* pos;
    DapiCallbackData* prev = NULL;
    for( pos = conn->callbacks;
         pos != NULL;
         prev = pos, pos = pos->next )
        {
        if( pos->seq == seq )
            {
            if( pos->callback != NULL )
                genericCallbackDispatch( conn, pos, command, seq );
            if( prev != NULL )
                prev = pos->next;
            else
                conn->callbacks = pos->next;
            free( pos );
            return;
            }
        }
/* TODO handle unhandled */
    }