nancy.liao
昨天 11f2f2e329ef404d0e9c022cb2f9fbbb45bae285
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#include "vxi11.h"
#include <memory.h>
#include <netinet/in.h>
#include <rpc/pmap_clnt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
 
#include "../common/scpi-def.h"
#include "scpi/scpi.h"
 
#ifndef SIG_PF
#define SIG_PF void (*)(int)
#endif
 
#define RX_BUF_SIZE 512
 
/* a global output buffer to collect output data */
#define SCPI_OUPUT_BUFFER_SIZE (256)
char SCPI_outputBuffer[SCPI_OUPUT_BUFFER_SIZE];
unsigned int SCPI_outputBuffer_wr_idx = 0;
unsigned int SCPI_outputBuffer_rd_idx = 0;
 
char available_bytes = 0;
 
enum {
    VXI11_CORE_ERROR_NO_ERROR = 0,
    VXI11_CORE_ERROR_SYNTAX_ERROR = 1,
    VXI11_CORE_ERROR_DEVICE_NOT_ACCESSIBLE = 3,
    VXI11_CORE_ERROR_INVALID_ID = 4,
    VXI11_CORE_ERROR_PARAMETER_ERROR = 5,
    VXI11_CORE_ERROR_CHANNEL_NOT_ESTABLISHED = 6,
    VXI11_CORE_ERROR_OPERATION_NOT_SUPPORTED = 8,
    VXI11_CORE_ERROR_OUT_OF_RESOURCES = 9,
    VXI11_CORE_ERROR_DEVICE_LOCKED = 11,
    VXI11_CORE_ERROR_NO_LOCK_HELD = 12,
    VXI11_CORE_ERROR_IO_TIMEOUT = 15,
    VXI11_CORE_ERROR_IO_ERROR = 17,
    VXI11_CORE_ERROR_INVALID_ADDRESS = 21,
    VXI11_CORE_ERROR_ABORT = 23,
    VXI11_CORE_ERROR_CHANNEL_ALREADY_ESTABLISHED = 29
};
 
enum {
    VXI11_REASON_REQCNT = 1,
    VXI11_REASON_CHR = 2,
    VXI11_REASON_END = 4
};
 
size_t SCPI_Write(scpi_t* context, const char* data, size_t len)
{
    if (STB_MAV & SCPI_RegGet(context, SCPI_REG_STB)) {
        SCPI_RegClearBits(context, SCPI_REG_STB, STB_MAV);
        SCPI_ErrorPush(context, SCPI_ERROR_QUERY_INTERRUPTED);
        // Restart output buffer index
        SCPI_outputBuffer_wr_idx = 0;
        SCPI_outputBuffer_rd_idx = 0;
    }
 
    if ((SCPI_outputBuffer_wr_idx + len) > (SCPI_OUPUT_BUFFER_SIZE - 1)) {
        len = (SCPI_OUPUT_BUFFER_SIZE - 1) - SCPI_outputBuffer_wr_idx; /* limit length to left over space */
        /* apparently there is no mechanism to cope with buffers that are too small */
    }
    memcpy(&SCPI_outputBuffer[SCPI_outputBuffer_wr_idx], data, len);
    SCPI_outputBuffer_wr_idx += len;
 
    SCPI_outputBuffer[SCPI_outputBuffer_wr_idx] = '\0';
    return 0;
}
 
scpi_result_t SCPI_Flush(scpi_t* context)
{
    SCPI_RegSetBits(context, SCPI_REG_STB, STB_MAV);
    SCPI_outputBuffer_rd_idx = 0;
    return SCPI_RES_OK;
}
 
int SCPI_Error(scpi_t* context, int_fast16_t err)
{
    (void)context;
    /* BEEP */
    fprintf(stderr, "**ERROR: %d, \"%s\"\r\n", (int16_t)err, SCPI_ErrorTranslate(err));
    return 0;
}
 
scpi_result_t SCPI_Control(scpi_t* context, scpi_ctrl_name_t ctrl, scpi_reg_val_t val)
{
    (void)context;
 
    if (SCPI_CTRL_SRQ == ctrl) {
        fprintf(stderr, "**SRQ: 0x%X (%d)\r\n", val, val);
    } else {
        fprintf(stderr, "**CTRL %02x: 0x%X (%d)\r\n", ctrl, val, val);
    }
    return SCPI_RES_OK;
}
 
scpi_result_t SCPI_Reset(scpi_t* context)
{
    (void)context;
 
    fprintf(stderr, "**Reset\r\n");
    return SCPI_RES_OK;
}
 
scpi_result_t SCPI_SystemCommTcpipControlQ(scpi_t* context)
{
    (void)context;
 
    return SCPI_RES_ERR;
}
 
bool_t
device_abort_1_svc(Device_Link* argp, Device_Error* result, struct svc_req* rqstp)
{
    bool_t retval;
    result->error = VXI11_CORE_ERROR_NO_ERROR;
    return 1;
}
 
int device_async_1_freeresult(SVCXPRT* transp, xdrproc_t xdr_result, caddr_t result)
{
    xdr_free(xdr_result, result);
    return 1;
}
 
bool_t
create_link_1_svc(Create_LinkParms* argp, Create_LinkResp* result, struct svc_req* rqstp)
{
    result->lid = 0;
    result->maxRecvSize = RX_BUF_SIZE;
    result->abortPort = rqstp->rq_xprt->xp_port;
    result->error = VXI11_CORE_ERROR_NO_ERROR;
    return 1;
}
 
bool_t
device_write_1_svc(Device_WriteParms* argp, Device_WriteResp* result, struct svc_req* rqstp)
{
    SCPI_Input(&scpi_context, argp->data.data_val, argp->data.data_len);
    result->size = argp->data.data_len;
    result->error = VXI11_CORE_ERROR_NO_ERROR;
    return 1;
}
 
bool_t
device_read_1_svc(Device_ReadParms* argp, Device_ReadResp* result, struct svc_req* rqstp)
{
    if (STB_MAV & SCPI_RegGet(&scpi_context, SCPI_REG_STB)) {
 
        char* data = NULL;
 
        uint32_t in_buffer = (SCPI_outputBuffer_wr_idx - SCPI_outputBuffer_rd_idx);
        uint32_t to_copy = (argp->requestSize > in_buffer) ? in_buffer : argp->requestSize;
 
        data = (unsigned char*)malloc(to_copy);
        if (NULL == data) {
            result->error = VXI11_CORE_ERROR_IO_ERROR;
            return 1;
        }
 
        memcpy(data, &SCPI_outputBuffer[SCPI_outputBuffer_rd_idx], to_copy);
        SCPI_outputBuffer_rd_idx += to_copy;
 
        result->data.data_val = (char*)data;
        result->data.data_len = (u_int)to_copy;
 
        result->reason = (to_copy == argp->requestSize) ? VXI11_REASON_REQCNT : 0;
 
        fprintf(stderr, "in_buffer: %d\n", in_buffer);
        fprintf(stderr, "to_copy: %d\n", to_copy);
        fprintf(stderr, "SCPI_outputBuffer_wr_idx: %d\n", SCPI_outputBuffer_wr_idx);
        fprintf(stderr, "SCPI_outputBuffer_rd_idx: %d\n", SCPI_outputBuffer_rd_idx);
 
        if (SCPI_outputBuffer_rd_idx == SCPI_outputBuffer_wr_idx) {
            SCPI_RegClearBits(&scpi_context, SCPI_REG_STB, STB_MAV);
            SCPI_outputBuffer_wr_idx = 0;
            SCPI_outputBuffer_rd_idx = 0;
            result->reason |= VXI11_REASON_END;
        }
 
        result->error = VXI11_CORE_ERROR_NO_ERROR;
    } else {
        result->error = VXI11_CORE_ERROR_IO_TIMEOUT;
    }
 
    return 1;
}
 
bool_t
device_readstb_1_svc(Device_GenericParms* argp, Device_ReadStbResp* result, struct svc_req* rqstp)
{
    (void)argp;
    (void)rqstp;
    result->stb = (u_char)SCPI_RegGet(&scpi_context, SCPI_REG_STB);
    result->error = VXI11_CORE_ERROR_NO_ERROR;
    return 1;
}
 
bool_t
device_trigger_1_svc(Device_GenericParms* argp, Device_Error* result, struct svc_req* rqstp)
{
    (void)argp;
    (void)rqstp;
    result->error = VXI11_CORE_ERROR_NO_ERROR;
    return 1;
}
 
bool_t
device_clear_1_svc(Device_GenericParms* argp, Device_Error* result, struct svc_req* rqstp)
{
    (void)argp;
    (void)rqstp;
    result->error = VXI11_CORE_ERROR_NO_ERROR;
    return 1;
}
 
bool_t
device_remote_1_svc(Device_GenericParms* argp, Device_Error* result, struct svc_req* rqstp)
{
    (void)argp;
    (void)rqstp;
    result->error = VXI11_CORE_ERROR_NO_ERROR;
    return 1;
}
 
bool_t
device_local_1_svc(Device_GenericParms* argp, Device_Error* result, struct svc_req* rqstp)
{
    (void)argp;
    (void)rqstp;
    result->error = VXI11_CORE_ERROR_NO_ERROR;
    return 1;
}
 
bool_t
device_lock_1_svc(Device_LockParms* argp, Device_Error* result, struct svc_req* rqstp)
{
    (void)argp;
    (void)rqstp;
    result->error = VXI11_CORE_ERROR_NO_ERROR;
    return 1;
}
 
bool_t
device_unlock_1_svc(Device_Link* argp, Device_Error* result, struct svc_req* rqstp)
{
    (void)argp;
    (void)rqstp;
    result->error = VXI11_CORE_ERROR_NO_ERROR;
    return 1;
}
 
bool_t
device_enable_srq_1_svc(Device_EnableSrqParms* argp, Device_Error* result, struct svc_req* rqstp)
{
    (void)argp;
    (void)rqstp;
    result->error = VXI11_CORE_ERROR_NO_ERROR;
    return 1;
}
 
bool_t
device_docmd_1_svc(Device_DocmdParms* argp, Device_DocmdResp* result, struct svc_req* rqstp)
{
    (void)argp;
    (void)rqstp;
    result->error = VXI11_CORE_ERROR_NO_ERROR;
    return 1;
}
 
bool_t
destroy_link_1_svc(Device_Link* argp, Device_Error* result, struct svc_req* rqstp)
{
    (void)argp;
    (void)rqstp;
    result->error = VXI11_CORE_ERROR_NO_ERROR;
    return 1;
}
 
bool_t
create_intr_chan_1_svc(Device_RemoteFunc* argp, Device_Error* result, struct svc_req* rqstp)
{
    (void)argp;
    (void)rqstp;
    result->error = VXI11_CORE_ERROR_NO_ERROR;
    return 1;
}
 
bool_t
destroy_intr_chan_1_svc(void* argp, Device_Error* result, struct svc_req* rqstp)
{
    (void)argp;
    (void)rqstp;
    result->error = VXI11_CORE_ERROR_NO_ERROR;
    return 1;
}
 
int device_core_1_freeresult(SVCXPRT* transp, xdrproc_t xdr_result, caddr_t result)
{
    (void)transp;
    xdr_free(xdr_result, result);
    return 1;
}
 
bool_t
device_intr_srq_1_svc(Device_SrqParms* argp, void* result, struct svc_req* rqstp)
{
    (void)argp;
    (void)result;
    (void)rqstp;
    return 1;
}
 
int device_intr_1_freeresult(SVCXPRT* transp, xdrproc_t xdr_result, caddr_t result)
{
    (void)transp;
    xdr_free(xdr_result, result);
    return 1;
}
 
static void
device_async_1(struct svc_req* rqstp, register SVCXPRT* transp)
{
    union {
        Device_Link device_abort_1_arg;
    } argument;
    union {
        Device_Error device_abort_1_res;
    } result;
    bool_t retval;
    xdrproc_t _xdr_argument, _xdr_result;
    bool_t (*local)(char*, void*, struct svc_req*);
 
    switch (rqstp->rq_proc) {
    case NULLPROC:
        (void)svc_sendreply(transp, (xdrproc_t)xdr_void, (char*)NULL);
        return;
 
    case device_abort:
        _xdr_argument = (xdrproc_t)xdr_Device_Link;
        _xdr_result = (xdrproc_t)xdr_Device_Error;
        local = (bool_t(*)(char*, void*, struct svc_req*))device_abort_1_svc;
        break;
 
    default:
        svcerr_noproc(transp);
        return;
    }
    memset((char*)&argument, 0, sizeof(argument));
    if (!svc_getargs(transp, (xdrproc_t)_xdr_argument, (caddr_t)&argument)) {
        svcerr_decode(transp);
        return;
    }
    retval = (bool_t)(*local)((char*)&argument, (void*)&result, rqstp);
    if (retval > 0 && !svc_sendreply(transp, (xdrproc_t)_xdr_result, (char*)&result)) {
        svcerr_systemerr(transp);
    }
    if (!svc_freeargs(transp, (xdrproc_t)_xdr_argument, (caddr_t)&argument)) {
        fprintf(stderr, "%s", "unable to free arguments");
        exit(1);
    }
    if (!device_async_1_freeresult(transp, _xdr_result, (caddr_t)&result))
        fprintf(stderr, "%s", "unable to free results");
 
    return;
}
 
static void
device_core_1(struct svc_req* rqstp, register SVCXPRT* transp)
{
    union {
        Create_LinkParms create_link_1_arg;
        Device_WriteParms device_write_1_arg;
        Device_ReadParms device_read_1_arg;
        Device_GenericParms device_readstb_1_arg;
        Device_GenericParms device_trigger_1_arg;
        Device_GenericParms device_clear_1_arg;
        Device_GenericParms device_remote_1_arg;
        Device_GenericParms device_local_1_arg;
        Device_LockParms device_lock_1_arg;
        Device_Link device_unlock_1_arg;
        Device_EnableSrqParms device_enable_srq_1_arg;
        Device_DocmdParms device_docmd_1_arg;
        Device_Link destroy_link_1_arg;
        Device_RemoteFunc create_intr_chan_1_arg;
    } argument;
    union {
        Create_LinkResp create_link_1_res;
        Device_WriteResp device_write_1_res;
        Device_ReadResp device_read_1_res;
        Device_ReadStbResp device_readstb_1_res;
        Device_Error device_trigger_1_res;
        Device_Error device_clear_1_res;
        Device_Error device_remote_1_res;
        Device_Error device_local_1_res;
        Device_Error device_lock_1_res;
        Device_Error device_unlock_1_res;
        Device_Error device_enable_srq_1_res;
        Device_DocmdResp device_docmd_1_res;
        Device_Error destroy_link_1_res;
        Device_Error create_intr_chan_1_res;
        Device_Error destroy_intr_chan_1_res;
    } result;
    bool_t retval;
    xdrproc_t _xdr_argument, _xdr_result;
    bool_t (*local)(char*, void*, struct svc_req*);
 
    switch (rqstp->rq_proc) {
    case NULLPROC:
        (void)svc_sendreply(transp, (xdrproc_t)xdr_void, (char*)NULL);
        return;
 
    case create_link:
        _xdr_argument = (xdrproc_t)xdr_Create_LinkParms;
        _xdr_result = (xdrproc_t)xdr_Create_LinkResp;
        local = (bool_t(*)(char*, void*, struct svc_req*))create_link_1_svc;
        break;
 
    case device_write:
        _xdr_argument = (xdrproc_t)xdr_Device_WriteParms;
        _xdr_result = (xdrproc_t)xdr_Device_WriteResp;
        local = (bool_t(*)(char*, void*, struct svc_req*))device_write_1_svc;
        break;
 
    case device_read:
        _xdr_argument = (xdrproc_t)xdr_Device_ReadParms;
        _xdr_result = (xdrproc_t)xdr_Device_ReadResp;
        local = (bool_t(*)(char*, void*, struct svc_req*))device_read_1_svc;
        break;
 
    case device_readstb:
        _xdr_argument = (xdrproc_t)xdr_Device_GenericParms;
        _xdr_result = (xdrproc_t)xdr_Device_ReadStbResp;
        local = (bool_t(*)(char*, void*, struct svc_req*))device_readstb_1_svc;
        break;
 
    case device_trigger:
        _xdr_argument = (xdrproc_t)xdr_Device_GenericParms;
        _xdr_result = (xdrproc_t)xdr_Device_Error;
        local = (bool_t(*)(char*, void*, struct svc_req*))device_trigger_1_svc;
        break;
 
    case device_clear:
        _xdr_argument = (xdrproc_t)xdr_Device_GenericParms;
        _xdr_result = (xdrproc_t)xdr_Device_Error;
        local = (bool_t(*)(char*, void*, struct svc_req*))device_clear_1_svc;
        break;
 
    case device_remote:
        _xdr_argument = (xdrproc_t)xdr_Device_GenericParms;
        _xdr_result = (xdrproc_t)xdr_Device_Error;
        local = (bool_t(*)(char*, void*, struct svc_req*))device_remote_1_svc;
        break;
 
    case device_local:
        _xdr_argument = (xdrproc_t)xdr_Device_GenericParms;
        _xdr_result = (xdrproc_t)xdr_Device_Error;
        local = (bool_t(*)(char*, void*, struct svc_req*))device_local_1_svc;
        break;
 
    case device_lock:
        _xdr_argument = (xdrproc_t)xdr_Device_LockParms;
        _xdr_result = (xdrproc_t)xdr_Device_Error;
        local = (bool_t(*)(char*, void*, struct svc_req*))device_lock_1_svc;
        break;
 
    case device_unlock:
        _xdr_argument = (xdrproc_t)xdr_Device_Link;
        _xdr_result = (xdrproc_t)xdr_Device_Error;
        local = (bool_t(*)(char*, void*, struct svc_req*))device_unlock_1_svc;
        break;
 
    case device_enable_srq:
        _xdr_argument = (xdrproc_t)xdr_Device_EnableSrqParms;
        _xdr_result = (xdrproc_t)xdr_Device_Error;
        local = (bool_t(*)(char*, void*, struct svc_req*))device_enable_srq_1_svc;
        break;
 
    case device_docmd:
        _xdr_argument = (xdrproc_t)xdr_Device_DocmdParms;
        _xdr_result = (xdrproc_t)xdr_Device_DocmdResp;
        local = (bool_t(*)(char*, void*, struct svc_req*))device_docmd_1_svc;
        break;
 
    case destroy_link:
        _xdr_argument = (xdrproc_t)xdr_Device_Link;
        _xdr_result = (xdrproc_t)xdr_Device_Error;
        local = (bool_t(*)(char*, void*, struct svc_req*))destroy_link_1_svc;
        break;
 
    case create_intr_chan:
        _xdr_argument = (xdrproc_t)xdr_Device_RemoteFunc;
        _xdr_result = (xdrproc_t)xdr_Device_Error;
        local = (bool_t(*)(char*, void*, struct svc_req*))create_intr_chan_1_svc;
        break;
 
    case destroy_intr_chan:
        _xdr_argument = (xdrproc_t)xdr_void;
        _xdr_result = (xdrproc_t)xdr_Device_Error;
        local = (bool_t(*)(char*, void*, struct svc_req*))destroy_intr_chan_1_svc;
        break;
 
    default:
        svcerr_noproc(transp);
        return;
    }
    memset((char*)&argument, 0, sizeof(argument));
    if (!svc_getargs(transp, (xdrproc_t)_xdr_argument, (caddr_t)&argument)) {
        svcerr_decode(transp);
        return;
    }
    retval = (bool_t)(*local)((char*)&argument, (void*)&result, rqstp);
    if (retval > 0 && !svc_sendreply(transp, (xdrproc_t)_xdr_result, (char*)&result)) {
        svcerr_systemerr(transp);
    }
    if (!svc_freeargs(transp, (xdrproc_t)_xdr_argument, (caddr_t)&argument)) {
        fprintf(stderr, "%s", "unable to free arguments");
        exit(1);
    }
    if (!device_core_1_freeresult(transp, _xdr_result, (caddr_t)&result))
        fprintf(stderr, "%s", "unable to free results");
 
    return;
}
 
static void
device_intr_1(struct svc_req* rqstp, register SVCXPRT* transp)
{
    union {
        Device_SrqParms device_intr_srq_1_arg;
    } argument;
    union {
    } result;
    bool_t retval;
    xdrproc_t _xdr_argument, _xdr_result;
    bool_t (*local)(char*, void*, struct svc_req*);
 
    switch (rqstp->rq_proc) {
    case NULLPROC:
        (void)svc_sendreply(transp, (xdrproc_t)xdr_void, (char*)NULL);
        return;
 
    case device_intr_srq:
        _xdr_argument = (xdrproc_t)xdr_Device_SrqParms;
        _xdr_result = (xdrproc_t)xdr_void;
        local = (bool_t(*)(char*, void*, struct svc_req*))device_intr_srq_1_svc;
        break;
 
    default:
        svcerr_noproc(transp);
        return;
    }
    memset((char*)&argument, 0, sizeof(argument));
    if (!svc_getargs(transp, (xdrproc_t)_xdr_argument, (caddr_t)&argument)) {
        svcerr_decode(transp);
        return;
    }
    retval = (bool_t)(*local)((char*)&argument, (void*)&result, rqstp);
    if (retval > 0 && !svc_sendreply(transp, (xdrproc_t)_xdr_result, (char*)&result)) {
        svcerr_systemerr(transp);
    }
    if (!svc_freeargs(transp, (xdrproc_t)_xdr_argument, (caddr_t)&argument)) {
        fprintf(stderr, "%s", "unable to free arguments");
        exit(1);
    }
    if (!device_intr_1_freeresult(transp, _xdr_result, (caddr_t)&result))
        fprintf(stderr, "%s", "unable to free results");
 
    return;
}
 
int main(int argc, char** argv)
{
    register SVCXPRT* transp;
 
    /* user_context will be pointer to socket */
    scpi_context.user_context = NULL;
 
    SCPI_Init(&scpi_context,
        scpi_commands,
        &scpi_interface,
        scpi_units_def,
        SCPI_IDN1, SCPI_IDN2, SCPI_IDN3, SCPI_IDN4,
        scpi_input_buffer, SCPI_INPUT_BUFFER_LENGTH,
        scpi_error_queue_data, SCPI_ERROR_QUEUE_SIZE);
 
    pmap_unset(DEVICE_ASYNC, DEVICE_ASYNC_VERSION);
    pmap_unset(DEVICE_CORE, DEVICE_CORE_VERSION);
    pmap_unset(DEVICE_INTR, DEVICE_INTR_VERSION);
 
    transp = svcudp_create(RPC_ANYSOCK);
    if (transp == NULL) {
        fprintf(stderr, "%s", "cannot create udp service.");
        exit(1);
    }
    if (!svc_register(transp, DEVICE_ASYNC, DEVICE_ASYNC_VERSION, device_async_1, IPPROTO_UDP)) {
        fprintf(stderr, "%s", "unable to register (DEVICE_ASYNC, DEVICE_ASYNC_VERSION, udp).");
        exit(1);
    }
    if (!svc_register(transp, DEVICE_CORE, DEVICE_CORE_VERSION, device_core_1, IPPROTO_UDP)) {
        fprintf(stderr, "%s", "unable to register (DEVICE_CORE, DEVICE_CORE_VERSION, udp).");
        exit(1);
    }
    if (!svc_register(transp, DEVICE_INTR, DEVICE_INTR_VERSION, device_intr_1, IPPROTO_UDP)) {
        fprintf(stderr, "%s", "unable to register (DEVICE_INTR, DEVICE_INTR_VERSION, udp).");
        exit(1);
    }
 
    transp = svctcp_create(RPC_ANYSOCK, 0, 0);
    if (transp == NULL) {
        fprintf(stderr, "%s", "cannot create tcp service.");
        exit(1);
    }
    if (!svc_register(transp, DEVICE_ASYNC, DEVICE_ASYNC_VERSION, device_async_1, IPPROTO_TCP)) {
        fprintf(stderr, "%s", "unable to register (DEVICE_ASYNC, DEVICE_ASYNC_VERSION, tcp).");
        exit(1);
    }
    if (!svc_register(transp, DEVICE_CORE, DEVICE_CORE_VERSION, device_core_1, IPPROTO_TCP)) {
        fprintf(stderr, "%s", "unable to register (DEVICE_CORE, DEVICE_CORE_VERSION, tcp).");
        exit(1);
    }
    if (!svc_register(transp, DEVICE_INTR, DEVICE_INTR_VERSION, device_intr_1, IPPROTO_TCP)) {
        fprintf(stderr, "%s", "unable to register (DEVICE_INTR, DEVICE_INTR_VERSION, tcp).");
        exit(1);
    }
 
    svc_run();
    fprintf(stderr, "%s", "svc_run returned");
    return 0;
}