summaryrefslogtreecommitdiff
path: root/ecp/test/init_vconn.c
blob: 88b6fa04abb3d60680be9a76c7ea6e74b18aa2f7 (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
51
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

#include <core.h>
#include <vconn/vconn.h>

void handle_err(ECPConnection *conn, unsigned char mtype, int err) {
    printf("ERR: CTYPE:0x%x MTYPE:0x%x ERR:%d\n", conn->type, mtype, err);
}

static ECPConnection *conn_alloc(ECPSocket *sock, unsigned char type) {
    ECPConnection *conn;
    int rv;

    switch (type) {
        case ECP_CTYPE_VCONN:
            conn = malloc(sizeof(ECPVConn));
            if (conn) rv = ecp_vconn_create_inb((ECPVConn *)conn, sock);
            break;

        case ECP_CTYPE_VLINK:
            conn = malloc(sizeof(ECPConnection));
            if (conn) rv = ecp_vlink_create_inb(conn, sock);
            break;

        default:
            conn = malloc(sizeof(ECPConnection));
            if (conn) rv = ecp_conn_create_inb(conn, sock, type);
            break;
    }

    if (conn == NULL) return NULL;
    if (rv) {
        free(conn);
        return NULL;
    }

    return conn;
}

static void conn_free(ECPConnection *conn) {
    free(conn);
}

int ecp_init(ECPContext *ctx) {
    int rv;

    rv = ecp_ctx_init(ctx, handle_err, NULL, conn_alloc, conn_free);
    return rv;
}