summaryrefslogtreecommitdiff
path: root/io/hpmud/hpmud.c
blob: 8be7b7878439636679956cabfbe542145613f4c4 (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
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*****************************************************************************\

  hpmud.cpp - multi-point transport driver
 
  (c) 2004-2007 Copyright Hewlett-Packard Development Company, LP

  Permission is hereby granted, free of charge, to any person obtaining a copy 
  of this software and associated documentation files (the "Software"), to deal 
  in the Software without restriction, including without limitation the rights 
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
  of the Software, and to permit persons to whom the Software is furnished to do 
  so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  Author: Naga Samrat Chowdary Narla,
\*****************************************************************************/

#include "hpmud.h"
#include "hpmudi.h"

/* Client data. */
mud_session ms __attribute__ ((visibility ("hidden")));      /* mud session, one per client */
mud_session *msp __attribute__ ((visibility ("hidden"))) = &ms;

/*
 * sysdump() originally came from http://sws.dett.de/mini/hexdump-c , steffen@dett.de .  
 */
void __attribute__ ((visibility ("hidden"))) sysdump(const void *data, int size)
{
    /* Dump size bytes of *data. Output looks like:
     * [0000] 75 6E 6B 6E 6F 77 6E 20 30 FF 00 00 00 00 39 00 unknown 0.....9.
     */

    unsigned char *p = (unsigned char *)data;
    unsigned char c;
    int n;
    char bytestr[4] = {0};
    char addrstr[10] = {0};
    char hexstr[16*3 + 5] = {0};
    char charstr[16*1 + 5] = {0};
    for(n=1;n<=size;n++) {
        if (n%16 == 1) {
            /* store address for this line */
            snprintf(addrstr, sizeof(addrstr), "%.4d", (int)((p-(unsigned char *)data) & 0xffff));
        }
            
        c = *p;
        if (isprint(c) == 0) {
            c = '.';
        }

        /* store hex str (for left side) */
        snprintf(bytestr, sizeof(bytestr), "%02X ", *p);
        strncat(hexstr, bytestr, sizeof(hexstr)-strlen(hexstr)-1);

        /* store char str (for right side) */
        snprintf(bytestr, sizeof(bytestr), "%c", c);
        strncat(charstr, bytestr, sizeof(charstr)-strlen(charstr)-1);

        if(n%16 == 0) { 
            /* line completed */
            DBG_SZ("[%4.4s]   %-50.50s  %s\n", addrstr, hexstr, charstr);
            hexstr[0] = 0;
            charstr[0] = 0;
        }
        p++; /* next byte */
    }

    if (strlen(hexstr) > 0) {
        /* print rest of buffer if not empty */
        DBG_SZ("[%4.4s]   %-50.50s  %s\n", addrstr, hexstr, charstr);
    }
}

/* Given the IEEE 1284 device id string, determine if this is a HP product. */
int __attribute__ ((visibility ("hidden"))) is_hp(const char *id)
{
   char *pMf;

   if ((pMf = strstr(id, "MFG:")) != NULL)
      pMf+=4;
   else if ((pMf = strstr(id, "MANUFACTURER:")) != NULL)
      pMf+=13;
   else
      return 0;

   if ((strncasecmp(pMf, "HEWLETT-PACKARD", 15) == 0) ||
      (strncasecmp(pMf, "APOLLO", 6) == 0) || (strncasecmp(pMf, "HP", 2) == 0))
   {
      return 1;  /* found HP product */
   }
   return 0;   
}

int __attribute__ ((visibility ("hidden"))) generalize_model(const char *sz, char *buf, int bufSize)
{
   const char *pMd=sz;
   int i, j, dd=0;

   for (i=0; pMd[i] == ' ' && i < bufSize; i++);  /* eat leading white space */

   for (j=0; (pMd[i] != 0) && (pMd[i] != ';') && (j < bufSize); i++)
   {
      if (pMd[i]==' ' || pMd[i]=='/')
      {
         /* Remove double spaces. */
         if (!dd)
         { 
            buf[j++] = '_';   /* convert space to "_" */
            dd=1;              
         }
      }
      else
      {
         buf[j++] = pMd[i];
         dd=0;       
      }
   }

   for (j--; buf[j] == '_' && j > 0; j--);  /* eat trailing white space */

   buf[++j] = 0;

   return j;   /* length does not include zero termination */
}

int __attribute__ ((visibility ("hidden"))) generalize_serial(const char *sz, char *buf, int bufSize)
{
   const char *pMd=sz;
   int i, j;

   for (i=0; pMd[i] == ' ' && i < bufSize; i++);  /* eat leading white space */

   for (j=0; (pMd[i] != 0) && (i < bufSize); i++)
   {
      buf[j++] = pMd[i];
   }

   for (i--; buf[i] == ' ' && i > 0; i--);  /* eat trailing white space */

   buf[++i] = 0;

   return i;   /* length does not include zero termination */
}

/* Parse serial number from uri string. */
int __attribute__ ((visibility ("hidden"))) get_uri_serial(const char *uri, char *buf, int bufSize)
{
   char *p;
   int i;

   buf[0] = 0;

   if ((p = strcasestr(uri, "serial=")) != NULL)
      p+=7;
   else
      return 0;

   for (i=0; (p[i] != 0) && (p[i] != '+') && (i < bufSize); i++)
      buf[i] = p[i];

   buf[i] = 0;

   return i;
}

enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) service_to_channel(mud_device *pd, const char *sn, HPMUD_CHANNEL *index)
{
   enum HPMUD_RESULT stat;

   *index=-1;
   
   /* Check for valid service requests. */
   if (strncasecmp(sn, "print", 5) == 0)
   {
      *index = HPMUD_PRINT_CHANNEL;
   }
   else if (strncasecmp(sn, "hp-ews-ledm", 11) == 0)
   {
      *index = HPMUD_EWS_LEDM_CHANNEL;
   }
   else if (strncasecmp(sn, "hp-ews", 6) == 0)
   {
      *index = HPMUD_EWS_CHANNEL;
   }
   else if (strncasecmp(sn, "hp-soap-scan", 12) == 0)
   {
      *index = HPMUD_SOAPSCAN_CHANNEL;
   }
   else if (strncasecmp(sn, "hp-soap-fax", 11) == 0)
   {
      *index = HPMUD_SOAPFAX_CHANNEL;
   }
   else if (strncasecmp(sn, "hp-marvell-scan", 15) == 0)
   {
      *index = HPMUD_MARVELL_SCAN_CHANNEL;
   }
   else if (strncasecmp(sn, "hp-marvell-fax", 14) == 0)
   {
      *index = HPMUD_MARVELL_FAX_CHANNEL;
   }
   else if (strncasecmp(sn, "hp-ledm-scan", 12) == 0)
   {
      *index = HPMUD_LEDM_SCAN_CHANNEL;
      BUG("io/hpmud/hpmud.c: service_to_channel(): sn=%s\n", sn);   
   }
   /* All the following services require MLC/1284.4. */
   else if (pd->io_mode == HPMUD_RAW_MODE || pd->io_mode == HPMUD_UNI_MODE)
   {
      BUG("invalid channel_open state, current io_mode=raw/uni service=%s %s\n", sn, pd->uri);
      stat = HPMUD_R_INVALID_STATE;
      goto bugout;
   }
   else if (strncasecmp(sn, "hp-message", 10) == 0)
   {
      *index = HPMUD_PML_CHANNEL;
   }
   else if (strncasecmp(sn, "hp-scan", 7) == 0)
   {
      *index = HPMUD_SCAN_CHANNEL;
   }
   else if (strncasecmp(sn, "hp-fax-send", 11) == 0)
   {
      *index = HPMUD_FAX_SEND_CHANNEL;
   }
   else if (strncasecmp(sn, "hp-card-access", 14) == 0)
   {
      *index = HPMUD_MEMORY_CARD_CHANNEL;
   }
   else if (strncasecmp(sn, "hp-configuration-upload", 23) == 0)
   {
      *index = HPMUD_CONFIG_UPLOAD_CHANNEL;
   }
   else if (strncasecmp(sn, "hp-configuration-download", 25) == 0)
   {
      *index = HPMUD_CONFIG_DOWNLOAD_CHANNEL;
   }
   else if (strncasecmp(sn, "hp-devmgmt", 10) == 0)
   {
      *index = HPMUD_DEVMGMT_CHANNEL;
   }
   else if (strncasecmp(sn, "hp-wificonfig", 13) == 0)
   {
      *index = HPMUD_WIFI_CHANNEL;
   }
   else
   {
      BUG("invalid service=%s %s\n", sn, pd->uri);
      stat = HPMUD_R_INVALID_SN;
      goto bugout;
   }

   stat = HPMUD_R_OK;

bugout:
   return stat;
}

static int new_device(const char *uri, enum HPMUD_IO_MODE mode, int *result)
{
   int index=0;      /* device[0] is unused */
   int i=1;

   if (uri[0] == 0)
      return 0;

   pthread_mutex_lock(&msp->mutex);

   if (msp->device[i].index)
   {
      BUG("invalid device_open state\n");        /* device is already open for this client, one device per session */
      *result = HPMUD_R_INVALID_STATE;
      goto bugout;
   }

   index = i;      /* currently only support one device per client or process */

   /* Based on uri, set local session attributes. */
   if (strcasestr(uri, ":/usb") != NULL)
   {
      msp->device[i].vf = musb_mud_device_vf;
   }
#ifdef HAVE_LIBNETSNMP
   else if (strcasestr(uri, ":/net") != NULL)
   {
      msp->device[i].vf = jd_mud_device_vf;
   }
#endif
#ifdef HAVE_PPORT
   else if (strcasestr(uri, ":/par") != NULL)
   {
      msp->device[i].vf = pp_mud_device_vf;
   }
#endif
   else
   {
      BUG("invalid uri %s\n", uri);
      *result = HPMUD_R_INVALID_URI;
      index = 0;
      goto bugout;
   }
   msp->device[i].io_mode = mode;
   msp->device[i].index = index;
   msp->device[i].channel_cnt = 0;
   msp->device[i].open_fd = -1;
   strcpy(msp->device[i].uri, uri);

bugout:
   pthread_mutex_unlock(&msp->mutex);

   return index;  /* return device index */
}

static int del_device(HPMUD_DEVICE index)
{
   pthread_mutex_lock(&msp->mutex);

   msp->device[index].index = 0;

   pthread_mutex_unlock(&msp->mutex);

   return 0;
}

/*  Make sure client closed down the device. */
int device_cleanup(mud_session *ps)
{
   int i, dd=1;

   if(!ps->device[dd].index)
      return 0;          /* nothing to do */

   BUG("device_cleanup: device uri=%s\n", ps->device[dd].uri);

   for (i=0; i<HPMUD_CHANNEL_MAX; i++)
   {
      if (ps->device[dd].channel[i].client_cnt)
      {
         BUG("device_cleanup: close channel %d...\n", i);
         hpmud_close_channel(dd, ps->device[dd].channel[i].index);
         BUG("device_cleanup: done closing channel %d\n", i);
      }
   }

   BUG("device_cleanup: close device dd=%d...\n", dd);
   hpmud_close_device(dd);
   BUG("device_cleanup: done closing device dd=%d\n", dd);

   return 0;
}

static void __attribute__ ((constructor)) mud_init(void)
{
   DBG("[%d] hpmud_init()\n", getpid());
}

static void __attribute__ ((destructor)) mud_exit(void)
{
   DBG("[%d] hpmud_exit()\n", getpid());
   device_cleanup(msp);
}

/*******************************************************************************************************************************
 * Helper functions.
 */

/* Parse the model from the IEEE 1284 device id string and generalize the model name */
int hpmud_get_model(const char *id, char *buf, int buf_size)
{
   char *pMd;

   buf[0] = 0;

   if ((pMd = strstr(id, "MDL:")) != NULL)
      pMd+=4;
   else if ((pMd = strstr(id, "MODEL:")) != NULL)
      pMd+=6;
   else
      return 0;

   return generalize_model(pMd, buf, buf_size);
}

/* Parse the model from the IEEE 1284 device id string. */
int hpmud_get_raw_model(char *id, char *raw, int rawSize)
{
   char *pMd;
   int i;

   raw[0] = 0;

   if ((pMd = strstr(id, "MDL:")) != NULL)
      pMd+=4;
   else if ((pMd = strstr(id, "MODEL:")) != NULL)
      pMd+=6;
   else
      return 0;

   for (i=0; (pMd[i] != ';') && (i < rawSize); i++)
      raw[i] = pMd[i];
   raw[i] = 0;

   return i;
}

/* Parse device model from uri string. */
int hpmud_get_uri_model(const char *uri, char *buf, int buf_size)
{
   char *p;
   int i;

   buf[0] = 0;

   if ((p = strstr(uri, "/")) == NULL)
      return 0;
   if ((p = strstr(p+1, "/")) == NULL)
      return 0;
   p++;

   for (i=0; (p[i] != '?') && (i < buf_size); i++)
      buf[i] = p[i];

   buf[i] = 0;

   return i;
}

/* Parse the data link from a uri string. */
int hpmud_get_uri_datalink(const char *uri, char *buf, int buf_size)
{
   char *p;
   int i;
   int zc=0;
#ifdef HAVE_LIBNETSNMP
   char ip[HPMUD_LINE_SIZE];
#endif

   buf[0] = 0;

   if ((p = strcasestr(uri, "device=")) != NULL)
      p+=7;
   else if ((p = strcasestr(uri, "ip=")) != NULL)
      p+=3;
   else if ((p = strcasestr(uri, "zc=")) != NULL)
   {
      p+=3;
      zc=1;
   }
   else
      return 0;

   if (zc)
   {
#ifdef HAVE_LIBNETSNMP
      if (hpmud_mdns_lookup(p, HPMUD_MDNS_TIMEOUT, ip) != HPMUD_R_OK)
	 return 0;
      for (i=0; (ip[i] != 0) && (i < buf_size); i++)
         buf[i] = ip[i];
#else
      return 0;
#endif
   }
   else {
      for (i=0; (p[i] != 0) && (p[i] != '&') && (i < buf_size); i++)
         buf[i] = p[i];
   }

   buf[i] = 0;

   return i;
}

/***************************************************************************************************
 * Core functions.
 */

enum HPMUD_RESULT hpmud_open_device(const char *uri, enum HPMUD_IO_MODE iomode, HPMUD_DEVICE *dd)
{
   HPMUD_DEVICE index=0;
   enum HPMUD_RESULT stat = HPMUD_R_INVALID_URI;
   int result;

   DBG("[%d,%d,%d,%d,%d,%d] hpmud_device_open() uri=%s iomode=%d\n", getpid(), getppid(), getuid(), geteuid(), getgid(), getegid(), uri, iomode);

   if ((index = new_device(uri, iomode, &result)) == 0)
   {   
      stat = result;
      goto bugout;
   }
   else
   {
      if ((stat = (msp->device[index].vf.open)(&msp->device[index])) != HPMUD_R_OK)
      {
         (msp->device[index].vf.close)(&msp->device[index]);  /* Open failed perform device cleanup. */
         del_device(index);
         goto bugout;
      }
   }

   *dd = index;
   stat = HPMUD_R_OK;

bugout:
   return stat;
}

enum HPMUD_RESULT hpmud_close_device(HPMUD_DEVICE dd)
{
   enum HPMUD_RESULT stat;

   DBG("[%d] hpmud_device_close() dd=%d\n", getpid(), dd);

   if (dd <= 0 || dd > HPMUD_DEVICE_MAX || msp->device[dd].index != dd)
   {
      BUG("invalid device_close state\n");
      stat = HPMUD_R_INVALID_STATE;
   }
   else
   {
      stat = (msp->device[dd].vf.close)(&msp->device[dd]);
      del_device(dd);
   }
   return stat;
}

enum HPMUD_RESULT hpmud_get_device_id(HPMUD_DEVICE dd, char *buf, int size, int *bytes_read)
{
   enum HPMUD_RESULT stat = HPMUD_R_INVALID_STATE;

   DBG("[%d] hpmud_get_device_id() dd=%d\n", getpid(), dd);

   if (dd <= 0 || dd > HPMUD_DEVICE_MAX || msp->device[dd].index != dd)
   {
      BUG("invalid get_device_id state\n");
      goto bugout;
   }

   stat = (msp->device[dd].vf.get_device_id)(&msp->device[dd], buf, size, bytes_read);

bugout:
   return stat;
}

enum HPMUD_RESULT hpmud_get_device_status(HPMUD_DEVICE dd, unsigned int *status)
{
   enum HPMUD_RESULT stat = HPMUD_R_INVALID_STATE;

   DBG("[%d] hpmud_get_device_status() dd=%d\n", getpid(), dd);

   if (dd <= 0 || dd > HPMUD_DEVICE_MAX || msp->device[dd].index != dd)
   {
      BUG("invalid get_device_status state\n");
      goto bugout;
   }

   stat = (msp->device[dd].vf.get_device_status)(&msp->device[dd], status);

bugout:
   return stat;
}

enum HPMUD_RESULT hpmud_probe_devices(enum HPMUD_BUS_ID bus, char *buf, int buf_size, int *cnt, int *bytes_read)
{
   int len=0;

   DBG("[%d] hpmud_probe_devices() bus=%d\n", getpid(), bus);

   buf[0] = 0;
   *cnt = 0;

   if (bus == HPMUD_BUS_USB)
   {
      len = musb_probe_devices(buf, buf_size, cnt);
   }
#ifdef HAVE_PPORT
   else if (bus == HPMUD_BUS_PARALLEL)
   {
      len = pp_probe_devices(buf, buf_size, cnt);
   }
#endif
   else if (bus == HPMUD_BUS_ALL)
   {
      len = musb_probe_devices(buf, buf_size, cnt);
#ifdef HAVE_PPORT
      len += pp_probe_devices(buf+len, buf_size-len, cnt);
#endif
   }

   *bytes_read = len;

   return HPMUD_R_OK;
}

enum HPMUD_RESULT hpmud_open_channel(HPMUD_DEVICE dd, const char *channel_name, HPMUD_CHANNEL *cd)
{
   enum HPMUD_RESULT stat = HPMUD_R_INVALID_STATE;

   DBG("[%d] hpmud_channel_open() dd=%d name=%s\n", getpid(), dd, channel_name);

   if (dd <= 0 || dd > HPMUD_DEVICE_MAX || msp->device[dd].index != dd)
   {
      BUG("invalid channel_open state\n");
      goto bugout;
   }

   stat = (msp->device[dd].vf.channel_open)(&msp->device[dd], channel_name, cd);

bugout:
   return stat;
}

enum HPMUD_RESULT hpmud_close_channel(HPMUD_DEVICE dd, HPMUD_CHANNEL cd)
{
   enum HPMUD_RESULT stat = HPMUD_R_INVALID_STATE;

   DBG("[%d] hpmud_channel_close() dd=%d cd=%d\n", getpid(), dd, cd);

   if (dd <= 0 || dd > HPMUD_DEVICE_MAX || msp->device[dd].index != dd ||
        cd <=0 || cd > HPMUD_CHANNEL_MAX || msp->device[dd].channel[cd].client_cnt == 0)
   {
      BUG("invalid channel_close state\n");
      goto bugout;
   }

   stat = (msp->device[dd].vf.channel_close)(&msp->device[dd], &msp->device[dd].channel[cd]);

bugout:
   return stat;
}

enum HPMUD_RESULT hpmud_write_channel(HPMUD_DEVICE dd, HPMUD_CHANNEL cd, const void *buf, int size, int sec_timeout, int *bytes_wrote)
{
   enum HPMUD_RESULT stat = HPMUD_R_INVALID_STATE;

   DBG("[%d] hpmud_channel_write() dd=%d cd=%d buf=%p size=%d sectime=%d\n", getpid(), dd, cd, buf, size, sec_timeout);

   if (dd <= 0 || dd > HPMUD_DEVICE_MAX || msp->device[dd].index != dd ||
        cd <=0 || cd > HPMUD_CHANNEL_MAX || msp->device[dd].channel[cd].client_cnt == 0)
   {
      BUG("invalid channel_write state\n");
      goto bugout;
   }

   stat = (msp->device[dd].vf.channel_write)(&msp->device[dd], &msp->device[dd].channel[cd], buf, size, sec_timeout, bytes_wrote);

bugout:
   return stat;
}

enum HPMUD_RESULT hpmud_read_channel(HPMUD_DEVICE dd, HPMUD_CHANNEL cd, void *buf, int size, int sec_timeout, int *bytes_read)
{
   enum HPMUD_RESULT stat = HPMUD_R_INVALID_STATE;
   DBG("[%d] hpmud_channel_read() dd=%d cd=%d buf=%p size=%d sectime=%d\n", getpid(), dd, cd, buf, size, sec_timeout);

   if (dd <= 0 || dd > HPMUD_DEVICE_MAX || msp->device[dd].index != dd ||
        cd <=0 || cd > HPMUD_CHANNEL_MAX || msp->device[dd].channel[cd].client_cnt == 0)
   {
      BUG("invalid channel_read state\n");
      goto bugout;
   }

   stat = (msp->device[dd].vf.channel_read)(&msp->device[dd], &msp->device[dd].channel[cd], buf, size, sec_timeout, bytes_read);

bugout:
   return stat;
}

enum HPMUD_RESULT hpmud_get_dstat(HPMUD_DEVICE dd, struct hpmud_dstat *ds)
{
   enum HPMUD_RESULT stat = HPMUD_R_INVALID_STATE;

   DBG("[%d] hpmud_dstat() dd=%d ds=%p\n", getpid(), dd, ds);

   if (dd <= 0 || dd > HPMUD_DEVICE_MAX)
   {
      BUG("invalid dstat state\n");
      goto bugout;
   }

   strncpy(ds->uri, msp->device[dd].uri, sizeof(ds->uri));
   ds->io_mode = msp->device[dd].io_mode;
   ds->channel_cnt = msp->device[dd].channel_cnt;
   ds->mlc_up = msp->device[dd].mlc_up;

   stat = HPMUD_R_OK;

bugout:
   return stat;
}