aboutsummaryrefslogtreecommitdiffstats
path: root/test/libdvben50221/test-session.c
blob: 14dc13ee24bbf66c306f4d429cd816cd26554dd2 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
    en50221 encoder An implementation for libdvb
    an implementation for the en50221 transport layer

    Copyright (C) 2004, 2005 Manu Abraham (manu@kromtek.com)
    Copyright (C) 2005 Julian Scheel (julian at jusst dot de)
    Copyright (C) 2006 Andrew de Quincey (adq_dvb@lidskialf.net)

    This library is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as
    published by the Free Software Foundation; either version 2.1 of
    the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
*/

#include <stdio.h>
#include <unistd.h>
#include <libdvben50221/en50221_session.h>
#include <libdvben50221/en50221_app_utils.h>
#include <libdvbapi/dvbca.h>
#include <pthread.h>

void *stackthread_func(void* arg);
int test_lookup_callback(void *arg, uint8_t slot_id, uint32_t requested_resource_id,
                         en50221_sl_resource_callback *callback_out, void **arg_out, uint32_t *connected_resource_id);
int test_session_callback(void *arg, int reason, uint8_t slot_id, uint16_t session_number, uint32_t resource_id);


int shutdown_stackthread = 0;

#define DEFAULT_SLOT 0

int main(int argc, char * argv[])
{
    (void)argc;
    (void)argv;

    int i;
    pthread_t stackthread;

    // create transport layer
    struct en50221_transport_layer *tl = en50221_tl_create(5, 32);
    if (tl == NULL) {
        fprintf(stderr, "Failed to create transport layer\n");
        exit(1);
    }

    // find CAMs
    int slot_count = 0;
    int cafd= -1;
    for(i=0; i<20; i++) {
        if ((cafd = dvbca_open(i, 0)) > 0) {
            if (dvbca_get_cam_state(cafd, DEFAULT_SLOT) == DVBCA_CAMSTATE_MISSING) {
                close(cafd);
                continue;
            }

            // reset it and wait
            dvbca_reset(cafd, DEFAULT_SLOT);
            printf("Found a CAM on adapter%i... waiting...\n", i);
            while(dvbca_get_cam_state(cafd, DEFAULT_SLOT) != DVBCA_CAMSTATE_READY) {
                usleep(1000);
            }

            // register it with the CA stack
            int slot_id = 0;
            if ((slot_id = en50221_tl_register_slot(tl, cafd, DEFAULT_SLOT, 1000, 100)) < 0) {
                fprintf(stderr, "Slot registration failed\n");
                exit(1);
            }
            printf("slotid: %i\n", slot_id);
            slot_count++;
        }
    }

    // create session layer
    struct en50221_session_layer *sl = en50221_sl_create(tl, 256);
    if (sl == NULL) {
        fprintf(stderr, "Failed to create session layer\n");
        exit(1);
    }

    // start another thread running the stack
    pthread_create(&stackthread, NULL, stackthread_func, tl);

    // register callbacks
    en50221_sl_register_lookup_callback(sl, test_lookup_callback, sl);
    en50221_sl_register_session_callback(sl, test_session_callback, sl);

    // create a new connection
    for(i=0; i<slot_count; i++) {
        int tc = en50221_tl_new_tc(tl, i);
        printf("tcid: %i\n", tc);
    }

    // wait
    printf("Press a key to exit\n");
    getchar();

    // destroy slots
    for(i=0; i<slot_count; i++) {
        en50221_tl_destroy_slot(tl, i);
    }
    shutdown_stackthread = 1;
    pthread_join(stackthread, NULL);

    // destroy session layer
    en50221_sl_destroy(sl);

    // destroy transport layer
    en50221_tl_destroy(tl);

    return 0;
}

int test_lookup_callback(void *arg, uint8_t slot_id, uint32_t requested_resource_id,
                         en50221_sl_resource_callback *callback_out, void **arg_out, uint32_t *connected_resource_id)
{
    (void)arg;
    (void)callback_out;
    (void)arg_out;
    (void)connected_resource_id;

    struct en50221_app_public_resource_id resid;

    if (en50221_app_decode_public_resource_id(&resid, requested_resource_id)) {
        printf("Public resource lookup callback %i %i %i %i\n", slot_id,
               resid.resource_class, resid.resource_type, resid.resource_version);
    } else {
        printf("Private resource lookup callback %i %08x\n", slot_id, requested_resource_id);
    }

    return -1;
}

int test_session_callback(void *arg, int reason, uint8_t slot_id, uint16_t session_number, uint32_t resource_id)
{
   (void)arg;

    printf("Session callback %i %i %i %04x\n", slot_id, session_number, reason, resource_id);

    return -1;
}

void *stackthread_func(void* arg) {
    struct en50221_transport_layer *tl = arg;
    int lasterror = 0;

    while(!shutdown_stackthread) {
        int error;
        if ((error = en50221_tl_poll(tl)) != 0) {
            if (error != lasterror) {
                fprintf(stderr, "Error reported by stack slot:%i error:%i\n",
                        en50221_tl_get_error_slot(tl),
                        en50221_tl_get_error(tl));
            }
            lasterror = error;
        }
    }

    shutdown_stackthread = 0;
    return 0;
}