|
5 | 5 |
|
6 | 6 | #include "chip_db.h" |
7 | 7 | #include <cstring> |
| 8 | +#include <QFile> |
| 9 | +#include <QDebug> |
| 10 | +#include <QStandardPaths> |
| 11 | +#include <QDir> |
| 12 | +#include <QMessageBox> |
8 | 13 |
|
9 | | -static ChipInfo chipDB[] = |
10 | | -{ |
11 | | - /* id, name, pageSize, blockSize, size, tCS, tCLS, tALS, tCLR, tAR, tWP, tRP, tDS, tCH, tCLH, tALH, tWC, tRC, tREA */ |
12 | | - { CHIP_ID_NONE, "No Chip", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
13 | | - { CHIP_ID_K9F2G08U0C, "K9F2G08U0C", 0x800, 0x20000, 0x10000000, 20, 12, 12, 10, 10, 12, 12, 12, 5, 5, 5, 25, 25, 20 }, |
14 | | -}; |
| 14 | +#define CHIP_DB_FILE_NAME "nando_chip_db.csv" |
15 | 15 |
|
16 | | -uint32_t chipDbGet(ChipInfo *&db) |
| 16 | +QString ChipDb::findFile() |
17 | 17 | { |
18 | | - db = chipDB; |
| 18 | + QString fileName = CHIP_DB_FILE_NAME; |
| 19 | + |
| 20 | + if (!QFileInfo(fileName).exists() && |
| 21 | + (fileName = QStandardPaths::locate(QStandardPaths::ConfigLocation, |
| 22 | + CHIP_DB_FILE_NAME)).isNull()) |
| 23 | + { |
| 24 | + QMessageBox::critical(nullptr, tr("Error"), tr("Chip DB file %1 was not" |
| 25 | + " found in %2;%3").arg(CHIP_DB_FILE_NAME).arg(QDir::currentPath()). |
| 26 | + arg(QStandardPaths::standardLocations(QStandardPaths:: |
| 27 | + ConfigLocation).join(';'))); |
| 28 | + return QString(); |
| 29 | + } |
19 | 30 |
|
20 | | - return CHIP_ID_LAST; |
| 31 | + return fileName; |
21 | 32 | } |
22 | 33 |
|
23 | | -ChipInfo *chipInfoGetByName(char *name) |
| 34 | +int ChipDb::stringToChipInfo(const QString &file, const QString &s, |
| 35 | + ChipInfo &ci) |
24 | 36 | { |
25 | | - for (int id = 0; id < CHIP_ID_LAST; id++) |
| 37 | + int paramNum; |
| 38 | + QStringList paramsList; |
| 39 | + |
| 40 | + paramsList = s.split(','); |
| 41 | + paramNum = paramsList.size(); |
| 42 | + if (paramNum != CHIP_PARAM_NUM) |
26 | 43 | { |
27 | | - if (!strcmp(name, chipDB[id].name)) |
28 | | - return &chipDB[id]; |
| 44 | + QMessageBox::critical(nullptr, tr("Error"), |
| 45 | + tr("Failed to read chip DB entry from %1. Expected %2 parameters, " |
| 46 | + "but read %3").arg(file).arg(CHIP_PARAM_NUM).arg(paramNum)); |
| 47 | + return -1; |
| 48 | + } |
| 49 | + |
| 50 | + ci.name = paramsList[CHIP_PARAM_NAME]; |
| 51 | + for (int i = CHIP_PARAM_NAME + 1; i < CHIP_PARAM_NUM; i++) |
| 52 | + { |
| 53 | + bool ok; |
| 54 | + |
| 55 | + ci.params[i] = paramsList[i].toUInt(&ok); |
| 56 | + if (!ok) |
| 57 | + { |
| 58 | + QMessageBox::critical(nullptr, tr("Error"), tr("Failed to parse" |
| 59 | + " parameter %1 in %2").arg(paramsList[i]).arg(file)); |
| 60 | + return -1; |
| 61 | + } |
29 | 62 | } |
30 | 63 |
|
31 | 64 | return 0; |
32 | 65 | } |
33 | 66 |
|
34 | | -ChipInfo *chipInfoGetById(uint32_t id) |
| 67 | +void ChipDb::readFromCvs(void) |
35 | 68 | { |
36 | | - if (id == CHIP_ID_NONE || id >= CHIP_ID_LAST) |
37 | | - return NULL; |
| 69 | + ChipInfo chipInfo; |
| 70 | + QFile dbFile; |
| 71 | + QString fileName = findFile(); |
| 72 | + |
| 73 | + if (fileName.isNull()) |
| 74 | + return; |
| 75 | + |
| 76 | + dbFile.setFileName(fileName); |
| 77 | + if (!dbFile.open(QIODevice::ReadOnly | QIODevice::Text)) |
| 78 | + { |
| 79 | + QMessageBox::critical(nullptr, tr("Error"), tr("Failed to open chip DB " |
| 80 | + "file: %1, error: %2").arg(fileName).arg(dbFile.errorString())); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + QTextStream in(&dbFile); |
| 85 | + while (!in.atEnd()) |
| 86 | + { |
| 87 | + QString line = in.readLine(); |
| 88 | + if (line.isEmpty()) |
| 89 | + continue; |
| 90 | + if (*line.data() == '#') |
| 91 | + continue; |
| 92 | + if (stringToChipInfo(fileName, line, chipInfo)) |
| 93 | + return; |
| 94 | + chipInfoVector.append(chipInfo); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +ChipDb::ChipDb(QObject *parent) : QObject(parent) |
| 99 | +{ |
| 100 | + readFromCvs(); |
| 101 | +} |
| 102 | + |
| 103 | +QStringList *ChipDb::getNames() |
| 104 | +{ |
| 105 | + QStringList *namesList = new QStringList; |
| 106 | + |
| 107 | + for (int i = 0; i < chipInfoVector.size(); i++) |
| 108 | + namesList->append(chipInfoVector[i].name); |
| 109 | + |
| 110 | + return namesList; |
| 111 | +} |
| 112 | + |
| 113 | +ChipInfo *ChipDb::chipInfoGetByName(const QString &name) |
| 114 | +{ |
| 115 | + for (int i = 0; i < chipInfoVector.size(); i++) |
| 116 | + { |
| 117 | + if (!chipInfoVector[i].name.compare(name)) |
| 118 | + return &chipInfoVector[i]; |
| 119 | + } |
38 | 120 |
|
39 | | - return &chipDB[id]; |
| 121 | + return nullptr; |
40 | 122 | } |
41 | 123 |
|
42 | | -uint32_t chipPageSizeGet(uint32_t id) |
| 124 | +uint32_t ChipDb::pageSizeGetByName(const QString &name) |
43 | 125 | { |
44 | | - ChipInfo *info = chipInfoGetById(id); |
| 126 | + ChipInfo *info = chipInfoGetByName(name); |
45 | 127 |
|
46 | | - return info ? info->pageSize : 0; |
| 128 | + return info ? info->params[CHIP_PARAM_PAGE_SIZE] : 0; |
47 | 129 | } |
0 commit comments