This repository has been archived by the owner on Dec 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
jk_info.c
226 lines (211 loc) · 6.01 KB
/
jk_info.c
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
/*
Copyright (c) 2021, Stephen P. Shoecraft
All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
*/
#include <unistd.h>
#include <stdint.h>
#include "jk_info.h"
#include "debug.h"
#include "utils.h"
#define _getshort(p) (short)(*(p) | (*((p)+1) << 8))
#define _getint(p) (int)(*(p) | (*((p)+1) << 8) | (*((p)+2) << 16) | (*((p)+3) << 24))
static void _getvolts(jk_info_t *info, uint8_t *data) {
int i,j;
if (debug >= 5) bindump("getvolts",data,300);
i = 6;
for(j=0; j < 24; j++) {
info->cellvolt[j] = _getshort(&data[i]) / 1000.0;
if (!info->cellvolt[j]) break;
dprintf(1,"cellvolt[%02d] = data[%02d] = %.3f\n", j, i, (_getshort(&data[i]) / 1000.0));
i += 2;
}
i = 64;
for(j=0; j < 24; j++) {
info->cellres[j] = _getshort(&data[i]) / 1000.0;
if (!info->cellres[j]) break;
dprintf(1,"cellres[%02d] = data[%02d] = %.3f\n", j, i, (_getshort(&data[i]) / 1000.0));
i += 2;
}
info->strings = j;
dprintf(1,"srings: %d\n", info->strings);
info->voltage = ((unsigned short)_getshort(&data[118])) / 1000.0;
dprintf(1,"voltage: %.2f\n", info->voltage);
info->current = _getshort(&data[126]) / 1000.0;
dprintf(1,"current: %.2f\n", info->current);
info->probes = 3;
dprintf(1,"probes: %d\n", info->probes);
info->temps[0] = ((unsigned short)_getshort(&data[130])) / 10.0;
dprintf(1,"temp[0]: %.1f\n", info->temps[0]);
info->temps[1] = ((unsigned short)_getshort(&data[132])) / 10.0;
dprintf(1,"temp[1]: %.1f\n", info->temps[1]);
info->temps[2] = ((unsigned short)_getshort(&data[134])) / 10.0;
dprintf(1,"temp[2]: %.1f\n", info->temps[2]);
}
static void _getinfo(jk_info_t *info, uint8_t *data) {
int i,j,uptime,unk;
if (debug >= 5) bindump("info",data,300);
j=0;
/* Model */
for(i=6; i < 300 && data[i]; i++) {
info->model[j++] = data[i];
if (j >= sizeof(info->model)-1) break;
}
info->model[j] = 0;
dprintf(1,"Model: %s\n", info->model);
/* Skip 0s */
while(i < 300 && !data[i]) i++;
/* HWVers */
dprintf(1,"i: %x\n", i);
j=0;
while(i < 300 && data[i]) {
info->hwvers[j++] = data[i++];
if (j >= sizeof(info->hwvers)-1) break;
}
info->hwvers[j] = 0;
dprintf(1,"HWVers: %s\n", info->hwvers);
/* Skip 0s */
while(i < 300 && !data[i]) i++;
/* SWVers */
j=0;
dprintf(1,"i: %x\n", i);
while(i < 300 && data[i]) {
info->swvers[j++] = data[i++];
if (j >= sizeof(info->swvers)-1) break;
}
info->swvers[j] = 0;
dprintf(1,"SWVers: %s\n", info->swvers);
/* Skip 0s */
while(i < 300 && !data[i]) i++;
dprintf(1,"i: %x\n", i);
uptime = _getint(&data[i]);
dprintf(1,"uptime: %04x\n", uptime);
i += 4;
unk = _getint(&data[i]);
dprintf(1,"unk: %04x\n", unk);
i += 4;
/* Device */
j=0;
dprintf(1,"i: %x\n", i);
while(i < 300 && data[i]) {
info->device[j++] = data[i++];
if (j >= sizeof(info->device)-1) break;
}
info->device[j] = 0;
dprintf(1,"Device: %s\n", info->device);
/* Skip 0s */
while(i < 300 && !data[i]) i++;
dprintf(1,"i: %x\n", i);
j=0;
dprintf(1,"i: %x\n", i);
while(i < 300 && data[i]) {
info->pin[j++] = data[i++];
if (j >= sizeof(info->pin)-1) break;
}
info->pin[j] = 0;
dprintf(1,"Pin: %s\n", info->pin);
/* Skip 0s */
while(i < 300 && !data[i]) i++;
dprintf(1,"i: %x\n", i);
j=0;
dprintf(1,"i: %x\n", i);
while(i < 300 && data[i]) {
info->num1[j++] = data[i++];
if (j >= sizeof(info->num1)-1) break;
}
info->num1[j] = 0;
dprintf(1,"Number: %s\n", info->num1);
/* Skip 0s */
while(i < 300 && !data[i]) i++;
dprintf(1,"i: %x\n", i);
j=0;
dprintf(1,"i: %x\n", i);
while(i < 300 && data[i]) {
info->num2[j++] = data[i++];
if (j >= sizeof(info->num2)-1) break;
}
info->num2[j] = 0;
dprintf(1,"Another Number: %s\n", info->num2);
/* Skip 0s */
while(i < 300 && !data[i]) i++;
dprintf(1,"i: %x\n", i);
/* LOL skip "Input User data */
while(i < 300 && data[i]) i++;
dprintf(1,"i: %x\n", i);
/* Skip 0s */
while(i < 300 && !data[i]) i++;
dprintf(1,"i: %x\n", i);
j=0;
dprintf(1,"i: %x\n", i);
while(i < 300 && data[i]) {
info->pass[j++] = data[i++];
if (j >= sizeof(info->pass)-1) break;
}
info->pass[j] = 0;
dprintf(1,"passcode: %s\n", info->pass);
}
#define GOT_RES 0x01
#define GOT_VOLT 0x02
#define GOT_INFO 0x04
static int getdata(jk_info_t *info, uint8_t *data, int bytes) {
uint8_t sig[] = { 0x55,0xAA,0xEB,0x90 };
int i,j,start,r;
r = 0;
// bindump("data",data,bytes);
for(i=j=0; i < bytes; i++) {
dprintf(4,"data[%d]: %02x, sig[%d]: %02x\n", i, data[i], j, sig[j]);
if (data[i] == sig[j]) {
if (j == 0) start = i;
j++;
if (j >= sizeof(sig) && (start + 300) < bytes) {
dprintf(1,"found sig\n");
if (data[i+1] == 1) {
// _getres(info,&data[start]);
r |= GOT_RES;
} else if (data[i+1] == 2) {
_getvolts(info,&data[start]);
r |= GOT_VOLT;
} else if (data[i+1] == 3) {
_getinfo(info,&data[start]);
r |= GOT_INFO;
}
i += 300;
j = 0;
}
}
if (r & GOT_VOLT) break;
}
dprintf(4,"returning: %d\n", r);
return r;
}
int jk_get_info(jk_session_t *s, jk_info_t *info) {
unsigned char getInfo[] = { 0xaa,0x55,0x90,0xeb,0x97,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x11 };
unsigned char getCellInfo[] = { 0xaa,0x55,0x90,0xeb,0x96,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10 };
uint8_t data[2048];
int bytes,r,retries;
/* Have to getInfo before can getCellInfo ... */
retries=5;
while(retries--) {
bytes = s->tp->write(s->tp_handle,getInfo,sizeof(getInfo));
dprintf(4,"bytes: %d\n", bytes);
if (bytes < 0) return -1;
bytes = s->tp->read(s->tp_handle,data,sizeof(data));
dprintf(4,"bytes: %d\n", bytes);
if (bytes < 0) return -1;
r = getdata(info,data,bytes);
if (r & GOT_INFO) break;
sleep(1);
}
retries=5;
bytes = s->tp->write(s->tp_handle,getCellInfo,sizeof(getCellInfo));
dprintf(4,"bytes: %d\n", bytes);
if (bytes < 0) return -1;
while(retries--) {
bytes = s->tp->read(s->tp_handle,data,sizeof(data));
dprintf(4,"bytes: %d\n", bytes);
r = getdata(info,data,bytes);
if (r & GOT_VOLT) break;
}
return (retries < 1 ? -1 : 0);
}