Subversion Repositories HelenOS-historic

Rev

Rev 1618 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1596 palkovsky 1
#include <stdio.h>
2
#include <async.h>
3
#include <ipc/ipc.h>
4
#include <ipc/services.h>
5
#include <errno.h>
6
 
7
#define TEST_START       10000
8
#define MAXLIST          4
9
 
10
#define MSG_HANG_ME_UP   2000
11
 
12
static int connections[50];
13
static ipc_callid_t callids[50];
14
static int phones[20];
15
static int myservice = 0;
16
 
17
static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
18
{
19
    ipc_callid_t callid;
20
    ipc_call_t call;
21
    ipcarg_t phonehash = icall->in_phone_hash;
22
    int retval;
23
    int i;
24
 
25
    printf("Connected phone: %P, accepting\n", icall->in_phone_hash);
26
    ipc_answer_fast(iid, 0, 0, 0);
27
    for (i=0;i < 1024;i++)
28
        if (!connections[i]) {
29
            connections[i] = phonehash;
30
            break;
31
        }
32
 
33
    while (1) {
34
        callid = async_get_call(&call);
35
        switch (IPC_GET_METHOD(call)) {
36
        case IPC_M_PHONE_HUNGUP:
37
            printf("Phone (%P) hung up.\n", phonehash);
38
            retval = 0;
39
            break;
40
        default:
41
            printf("Received message from %P: %X\n", phonehash,callid);
42
            for (i=0; i < 1024; i++)
43
                if (!callids[i]) {
44
                    callids[i] = callid;
45
                    break;
46
                }
47
            continue;
48
        }
49
        ipc_answer_fast(callid, retval, 0, 0);
50
    }
51
}
52
 
53
static void printhelp(void)
54
{
55
    printf("? - help\n");
56
    printf("c - connect to other service\n");
57
    printf("h - hangup connection\n");
58
    printf("a - send async message to other service\n");
59
    printf("s - send sync message to other service\n");
60
    printf("d - answer message that we have received\n");
61
    printf("j - jump to endless loop\n");
62
    printf("p - page fault\n");
63
}
64
 
65
static void callback(void *private, int retval, ipc_call_t *data)
66
{
67
    printf("Received response to msg %d - retval: %d.\n", private,
68
           retval);
69
}
70
 
71
static void do_answer_msg(void)
72
{
73
    int i,cnt, errn;
74
    char c;
75
    ipc_callid_t callid;
76
 
77
    cnt = 0;
78
    for (i=0;i < 50;i++) {
79
        if (callids[i]) {
80
            printf("%d: %P\n", cnt, callids[i]);
81
            cnt++;
82
        }
83
        if (cnt >= 10)
84
            break;
85
    }
86
    if (!cnt)
87
        return;
88
    printf("Choose message:\n");
89
    do {
90
        c = getchar();
91
    } while (c < '0' || (c-'0') >= cnt);
92
    cnt = c - '0' + 1;
93
 
94
    for (i=0;cnt;i++)
95
        if (callids[i])
96
            cnt--;
97
    i -= 1;
98
 
99
    printf("Normal (n) or hangup (h) or error(e) message?\n");
100
    do {
101
        c = getchar();
102
    } while (c != 'n' && c != 'h' && c != 'e');
103
    if (c == 'n')
104
        errn = 0;
105
    else if (c == 'h')
106
        errn = EHANGUP;
107
    else if (c == 'e')
108
        errn = ENOENT;
109
    printf("Answering %P\n", callids[i]);
110
    ipc_answer_fast(callids[i], errn, 0, 0);
111
    callids[i] = 0;
112
}
113
 
114
static void do_send_msg(int async)
115
{
116
    int phoneid;
117
    int res;
118
    static int msgid = 1;
119
    char c;
120
 
121
    printf("Select phoneid to send msg: 2-9\n");
122
    do {
123
        c = getchar();
124
    } while (c < '2' || c > '9');
125
    phoneid = c - '0';
126
 
127
    if (async) {
128
        ipc_call_async(phoneid, 2000, 0, (void *)msgid, callback, 1);
129
        printf("Async sent - msg %d\n", msgid);
130
        msgid++;
131
    } else {
132
        printf("Sending msg...");
133
        res = ipc_call_sync_2(phoneid, 2000, 0, 0, NULL, NULL);
134
        printf("done: %d\n", res);
135
    }
136
}
137
 
138
static void do_hangup(void)
139
{
140
    char c;
141
    int res;
142
    int phoneid;
143
 
144
    printf("Select phoneid to hangup: 2-9\n");
145
    do {
146
        c = getchar();
147
    } while (c < '2' || c > '9');
148
    phoneid = c - '0';
149
 
150
    printf("Hanging up...");
151
    res = ipc_hangup(phoneid);
152
    printf("done: %d\n", phoneid);
153
}
154
 
155
static void do_connect(void)
156
{
157
    char c;
158
    int svc;
159
    int phid;
160
 
161
    printf("Choose one service: 0:10000....9:10009\n");
162
    do {
163
        c = getchar();
164
    } while (c < '0' || c > '9');
165
    svc = TEST_START + c - '0';
166
    if (svc == myservice) {
167
        printf("Currently cannot connect to myself, update test\n");
168
        return;
169
    }
170
    printf("Connecting to %d..", svc);
171
    phid = ipc_connect_me_to(PHONE_NS, svc, 0);
172
    if (phid > 0) {
173
        printf("phoneid: %d\n", phid);
174
        phones[phid] = 1;
175
    } else
176
        printf("error: %d\n", phid);
177
}
178
 
179
int main(void)
180
{
181
    ipcarg_t phonead;
182
    int i;
183
    char c;
184
    int res;
185
 
186
    printf("********************************\n");
187
    printf("***********IPC Tester***********\n");
188
    printf("********************************\n");
189
 
190
 
191
    async_set_client_connection(client_connection);
192
 
193
    for (i=TEST_START;i < TEST_START+10;i++) {
194
        res = ipc_connect_to_me(PHONE_NS, i, 0, &phonead);
195
        if (!res)
196
            break;
197
        printf("Failed registering as %d..:%d\n", i, res);
198
    }
199
    printf("Registered as service: %d\n", i);
200
    myservice = i;
201
 
202
    printhelp();
203
    while (1) {
204
        c = getchar();
205
        switch (c) {
206
        case '?':
207
            printhelp();
208
            break;
209
        case 'h':
210
            do_hangup();
211
            break;
212
        case 'c':
213
            do_connect();
214
            break;
215
        case 'a':
216
            do_send_msg(1);
217
            break;
218
        case 's':
219
            do_send_msg(0);
220
            break;
221
        case 'd':
222
            do_answer_msg();
223
            break;
224
        case 'j':
225
            while (1)
226
                ;
227
        case 'p':
228
            printf("Doing page fault\n");
229
            *((char *)0) = 1;
230
            printf("done\n");
231
        }
232
    }
233
}