File: handshake.c

package info (click to toggle)
libnjb 2.2.5-4.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,528 kB
  • ctags: 1,071
  • sloc: ansic: 11,860; sh: 8,611; makefile: 139
file content (155 lines) | stat: -rw-r--r-- 3,876 bytes parent folder | download | duplicates (8)
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
#include "common.h"

int main (int argc, char **argv)
{
  njb_t njbs[NJB_MAX_DEVICES];
  njb_t *njb;
  int i, n, opt, debug;
  extern char *optarg;
  u_int8_t sdmiid[16];
  
  debug = 0;
  while ( (opt= getopt(argc, argv, "D:")) != -1 ) {
    switch (opt) {
    case 'D':
      debug= atoi(optarg);
      break;
    default:
      fprintf(stderr, "usage: handshake [ -D debuglvl ]\n");
      return 1;
    }
  }
  
  if ( debug ) NJB_Set_Debug(debug);
  
  if (NJB_Discover(njbs, 0, &n) == -1) {
    fprintf(stderr, "could not locate any jukeboxes\n");
    return 1;
  }
  
  if ( n == 0 ) {
    fprintf(stderr, "No NJB devices found\n");
    return 1;
  }
  
  printf("Found %u devices.\n", n);
  
  for (i = 0; i < n; i++) {
    njb_keyval_t *key;
    int j;
    int chargestat;
    int auxpowstat;
    const char *devname;
    const char *prodname;

    njb = &njbs[i];
    
    printf("Device %u: ", i+1);
    printf("\n    Player device type: ");

    
    njb = &njbs[i];
    
    if (NJB_Open(njb) == -1) {
      NJB_Error_Dump(njb, stderr);
      return 1;
    }

    if ((devname = NJB_Get_Device_Name(njb, 0)) != NULL) {
      printf("%s\n", devname);
    } else {
      printf("Error getting device name.\n");
      return 1;
    }

    /* Ping the device */
    NJB_Ping(njb);
    
    /* Retrieve the device ID */
    if (NJB_Get_SDMI_ID(njb, (u_int8_t *) &sdmiid) == 0) {
      printf("    Jukebox SDMI ID: ");
      for (j = 0; j < 16; j++) {
	printf("%02X", sdmiid[j]);
      }
      printf("\n");
    } else {
      printf("    Error getting SDMI ID.\n");
    }
    

    if (njb->device_type == NJB_DEVICE_NJB1) {
      u_int8_t major, minor, release;

      if (NJB_Get_Firmware_Revision(njb, &major, &minor, &release) == 0) {
	printf("    Firmware: %u.%u\n", major, minor);
      }
#ifdef __WIN32__
      printf("    Library counter: %I64u\n", NJB_Get_NJB1_Libcounter(njb));
#else
      printf("    Library counter: %llu\n", NJB_Get_NJB1_Libcounter(njb));
#endif
    }
    else {
      u_int8_t major, minor, release;
      
      if (NJB_Get_Firmware_Revision(njb, &major, &minor, &release) == 0) {
	printf("    Firmware: %u.%u.%u\n", major, minor, release);
      }
      if (NJB_Get_Hardware_Revision(njb, &major, &minor, &release) == 0) {
	printf("    Hardware: %u.%u.%u\n", major, minor, release);
      }
    }

    if ((prodname = NJB_Get_Device_Name(njb, 1)) != NULL) {
      printf("    Product name: %s\n", prodname);
    } else {
      printf("    Error getting product name.\n");
      return 1;
    }
    
    /* Auxiliary power status */
    auxpowstat = NJB_Get_Auxpower(njb);
    if (auxpowstat == -1) {
      printf("    Failure getting auxilary power status\n");
      return 1;
    } else if (auxpowstat == 1) {
      printf("    Device is on auxiliary power (AC charger or USB)\n");
    } else if (auxpowstat == 0) {
      printf("    Device is disconnected from auxiliary power\n");      
    }

    /* Battery charging status */
    chargestat = NJB_Get_Battery_Charging(njb);
    if (chargestat == -1) {
      printf("    Failure getting battery charging status\n");
      return 1;
    } else if (chargestat == 1) {
      printf("    Battery is charging\n");
    } else if (chargestat == 0) {
      printf("    Battery is not charging\n");      
    }    
    
    if (njb->device_type != NJB_DEVICE_NJB1) {
      /* This is only well implemented for the series 3 devices. */
      int battery_level = NJB_Get_Battery_Level(njb);
      if (battery_level == -1) {
	printf("    Failure getting battery level\n");
	return 1;
      } else {
	printf("    Battery level: %d%%\n", battery_level);
      }
    }
    
    key = NJB_Get_Keys(njb);
    while (key != NULL) {
      printf("    Device key: %s = %08X, %08X\n", key->key, key->value1, key->value2);
      key = key->next;
    }
    
    NJB_Close(njb);
    
  }
  
  return 0;
}