-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevicedb.cpp
152 lines (133 loc) · 3.44 KB
/
devicedb.cpp
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
/* JTAG chain device database
Copyright (C) 2004 Andrew Rogers
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Changes:
Dmitry Teytelman [dimtey@gmail.com] 19 May 2006 [applied 13 Aug 2006]:
Code cleanup for clean -Wall compile.
Modified loadDevice() to ignore 4 MSBs of IDCODE - used for device
revision code.
*/
#ifndef DEVICEDB
#define DEVICEDB "devlist.txt"
#endif
#include <stdlib.h>
#include <string.h>
#include <cstdio>
#include "devicedb.h"
#include "devices.h"
using namespace std;
DeviceDB::DeviceDB(const char *fname) {
device_t id;
char text[256];
int irlen;
uint32_t idr;
int id_cmd;
// Fall back to environment or default if no file specified
if(!fname) {
if(!(fname = getenv("XCDB"))) fname = DEVICEDB;
}
FILE *fp=fopen(fname,"rt");
if(fp)
{
filename = fname;
while(!feof(fp))
{
char buffer[256];
fgets(buffer,256,fp); // Get next line from file
if (sscanf(buffer,"%08x %d %x %s", &idr, &irlen, &id_cmd, text) == 4)
{
id.text = text;
id.idcode = idr & 0x0fffffff; /* Mask out revisions*/
id.irlen = irlen;
id.id_cmd = id_cmd;
id_db.push_back(id);
}
}
fclose(fp);
}
else
{
char buffer[256];
const char *p = fb_string;
filename = "built-in device list";
while(*p)
{
int i;
for(i=0; p[i] && (p[i] != ';'); i++)
{
buffer[i] = p[i];
}
p += i;
buffer[i] = 0;
while(*p && *p == ';')
p++;
if (i && (sscanf(buffer,"%08x %d %x %s", &idr, &irlen, &id_cmd, text) == 4))
{
id.text = text;
id.idcode = idr & 0x0fffffff; /* Mask out revisions*/
id.irlen = irlen;
id.id_cmd = id_cmd;
id_db.push_back(id);
}
}
}
}
int DeviceDB::loadDevice(const uint32_t id)
{
uint32_t i;
for (i=0; i<id_db.size(); i++)
{
if((id & 0x0fffffff) == id_db[i].idcode)
{
device_t dev;
dev.text=id_db[i].text;
dev.idcode=id_db[i].idcode;
dev.irlen=id_db[i].irlen;
dev.id_cmd=id_db[i].id_cmd;
devices.push_back(dev);
return dev.irlen;
}
}
return 0;
}
int DeviceDB::getIRLength(unsigned int i)
{
if(i >= devices.size())return 0;
return devices[i].irlen;
}
int DeviceDB::getIDCmd(unsigned int i)
{
if(i >= devices.size())return 0;
return devices[i].id_cmd;
}
const char *DeviceDB::getDeviceDescription(unsigned int i)
{
if(i>=devices.size())return 0;
return devices[i].text.c_str();
}
int DeviceDB::dumpDevices(FILE *fp_out)
{
unsigned int i;
if (!fp_out)
{
fprintf(stderr," No valid file to dump Cablelist\n");
return 1;
}
for(i = 0; i < id_db.size(); i++)
fprintf(fp_out,"%08x %6d 0x%04x %s\n",
id_db[i].idcode,
id_db[i].irlen,
id_db[i].id_cmd,
id_db[i].text.c_str());
return 0;
}