static BOOL Init(void) { struct system_t *pSystemInfo = &gSystemInfo; //初始化audio info结构体 if(!InitAudioStruct(pSystemInfo,TRUE)) { DBGE((TEXT("[System DLL] init struct err.\r\n"))); return FALSE; } if (!CreateClientSocket(9980)) { DBGE((TEXT("[System DLL] audio create client socket err.\r\n"))); return FALSE; } pSystemInfo->bInit = TRUE; BYTE buf[2] = {0x01,0x01}; SocketWrite(buf,2); DBGI((TEXT("\r\n[System DLL] Init OK time:"))); DBGI((TEXT(__TIME__))); DBGI((TEXT(" data:"))); DBGI((TEXT(__DATE__))); DBGI((TEXT("\r\n"))); return TRUE; }
static ssize_t __check_copy(int fd, int fd_new, size_t count) { char buf[256]; size_t n, offset = 0; while (offset < count) { if ((n = count - offset) > 256) n = 256; if (readn(fd, buf, n) != n) { DBGE("read(%zu %zu %zu) from %d failed", count, offset, n, fd); return -1; } if (fd_new >= 0 && write(fd_new, buf, n) != n) { DBGE("write buf %zu failed", n); return -1; } offset += n; } return 0; }
static ssize_t __cmp_key(int fd, const void *key, size_t key_len) { char buf[256]; size_t n, offset = 0; while (offset < key_len) { if ((n = key_len - offset) > 256) n = 256; if (readn(fd, buf, n) != n) { DBGE("read key(offset = %zu, n = %zu) failed", offset, n); return -2; } if (memcmp(buf, key + offset, n)) { offset += n; if (lseek(fd, -(ssize_t)offset, SEEK_CUR) < 0) { DBGE("lseek %zd failed", -(ssize_t)offset); return -2; } return -1; } offset += n; } return 0; }
int DataBase::write(QString filePath) { DBGS(PRINT_START("filePath: %s", qPrintable(filePath))); int rv = ERROR_UNKNOWN_ERROR; updateDOMKsilit(); updateDOMJotterHeader(); updateDOMJotterBody(rootItem); QString xmlString = domDoc.toString(); if (!xmlString.isEmpty()) { QFile fileOut(filePath); if (fileOut.open(QIODevice::WriteOnly)) { QTextStream outStream(&fileOut); outStream << xmlString; fileOut.close(); rv = ALL_OK; } else { DBGE(PRINT_ERROR("Error opening file: %s", qPrintable(filePath))); rv = ERROR_OPENING_FILE; } } else { DBGE(PRINT_ERROR("xmlString is empty!")); } DBGR(PRINT_RETURN("rv: %i", rv)); return rv; }
/** * Open a file in read-only mode and mmaps it * @param fname filename to open * @param fd Returns the file descriptor in this arg * @param length Returns the length of the file in this arg * @param data Returns the mmap pointer of the file in this arg * @return 0 if OK, <0 on error */ int file_open_mmap(char* fname, int *fd, int *length, char** data) { *fd = open(fname, O_RDONLY); if (*fd == -1) { DBGE("Can't open input file: %s\n", strerror(errno)); return -1; } /* Get file size and rewind */ *length = (int)lseek(*fd, 0, SEEK_END); if (*length == -1) { DBGE("Can't determinate file size: %s\n", strerror(errno)); goto fail; } *data = mmap(NULL, *length, PROT_READ, MAP_SHARED, *fd, 0); if (*data == MAP_FAILED) { DBGE("Can't mmap input file: %s\n", strerror(errno)); goto fail; } return 0; fail: close(*fd); return -1; }
/* create directory recursively */ int mkdir_p(const char *path) { int n; char buf[PATH_MAX], *p = buf; assert(path); if ((n = strlen(path)) >= PATH_MAX) { DBGP("over length: %d, %d", n, PATH_MAX); return -1; } DBGP("len=%d; path='%s'\n", n, path); strcpy(buf, path); while ((p = strchr(p+1, '/'))) { struct stat sb; *p = '\0'; if (stat(buf, &sb) && mkdir(buf, ACCESSPERMS)) { DBGE("stat/create '%s' failed", buf); return -1; } *p = '/'; } return n; }
ssize_t conf_read_bin(const char *file, const void *key, size_t key_len, void *value, size_t value_len) { int fd; ssize_t r = -1; if ((fd = open(file, O_RDONLY)) < 0) { DBGE("open %s failed", file); return -1; } if (__check_head(fd) < 0) goto RTN; if (__find_key(fd, -1, key, key_len, value_len) < 0) goto RTN; if (readn(fd, value, value_len) != value_len) goto RTN; r = 0; RTN: close(fd); return r; }
static ssize_t __find_key(int fd, int fd_new, const void *key, size_t key_len, size_t value_len) { uint32_t magic, klen, vlen; ssize_t r; struct bin_item bi; while ((r = readn(fd, &bi, sizeof(bi))) == sizeof(bi)) { magic = le32toh(bi.magic); klen = le32toh(bi.key_len); vlen = le32toh(bi.value_len); if (magic != MAGIC_ITEM) { DBGP("magic(%x) dismatch, check format", magic); return -2; } if (klen != key_len || vlen != value_len || (r = __cmp_key(fd, key, key_len))) { if (r == -2) /* lseek failed */ return r; if (__check_write(fd_new, &bi, sizeof(bi)) < 0 || __check_copy(fd, fd_new, klen + vlen) < 0) { DBGE("__check_write/__check_copy failed"); return -2; } continue; } return 0; /* match success */ } return -1; /* item non-exists */ }
/** * Reads a bootfile from the device * Rounds output up to flash page size for simplicity * (cmd_read_flash_page can only read full pages) * @param di Device info struct of opened and inited device * @param bi Bootfile info struct of the bootfile you want * @param filename to dump to * @returns 0 if OK, <0 on error */ int file_bootfile_read(devinfo_t *di, bootfile_info_t *bi, char* fname) { int fd, ret; char *file; int filesize = di->ps * (((bi->size - 1) / di->ps) + 1); fd = open(fname, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (fd == -1) { DBGE("Can't open output file: %s\n", strerror(errno)); return -1; } file = malloc(filesize); if (file == NULL) { DBGE("Can't allocate space for bootfile\n"); close(fd); return -1; } ret = image_get_bootfile_usb(di, bi->patpage, file); if (ret) { DBGE("Can't read bootfile\n"); goto fail; } ret = write(fd, file, filesize); if (ret < filesize) { DBGE("Can't write to output file\n"); goto fail; } ret = 0; fail: free(file); close(fd); return ret; }
ssize_t conf_op_bin(int type, const char *file, const void *key, size_t key_len, const void *value, size_t value_len) { int fd = -1, fd_new; char file_tmp[PATH_MAX]; ssize_t r = -1; if (pathname_tmp(file, file_tmp, PATH_MAX) < 0) return -1; if ((fd_new = open(file_tmp, O_WRONLY|O_CREAT, ACCESSPERMS)) < 0) { DBGE("open %s failed", file_tmp); return -1; } if ((r = __write_head(fd_new)) < 0) goto RTN; if ((fd = open(file, O_RDONLY)) < 0) { if (ENOENT == errno) goto NEW_ITEM; goto RTN; } if (__check_head(fd) < 0) goto RTN; while(!(r = __find_key(fd, fd_new, key, key_len, value_len))) __check_copy(fd, -1, value_len); /* discard value here */ if (r == -2) goto RTN; NEW_ITEM: if (CONF_WRITE == type) r = __write_item(fd_new, key, key_len, value, value_len); /* r = __copy_left(fd, fd_new); */ RTN: close(fd_new); if (fd >= 0) close(fd); if (r >= 0) r = rename(file_tmp, file); if (r < 0) unlink(file_tmp); return r; }
static inline ssize_t __write_head(int fd) { struct bin_head bh = { .magic = htole32(MAGIC_HEAD), .version = htole32(CONF_VERSION), }; if (writen(fd, &bh, sizeof(bh)) != sizeof(bh)) { DBGE("writen bin_head failed"); return -1; } return 0; }
//Returns 0 if jot not found Jot *Jotter::getJot(int id) { DBGS(PRINT_START("id: %i", id)); Jot *jot = 0; if (map.contains(id)) { jot = map.value(id); } else { DBGE(PRINT_ERROR("Jot with id: %i not found!", id)); } DBGR(PRINT_RETURN("jot: 0x%08x", jot)); return jot; }
QString Jotter::getText(int id) { DBGS(PRINT_START("id: %i", id)); QString text; Jot *jot = getJot(id); if (jot) { text = jot->getText(); } else { DBGE(PRINT_ERROR("Jot with id: %i not found!", id)); } DBGR(PRINT_RETURN("text: %s", qPrintable(text))); return text; }
QString Jotter::getName(int id) { DBGS(PRINT_START("id: %i", id)); QString name; Jot *jot = getJot(id); if (jot) { name = jot->getName(); } else { DBGE(PRINT_ERROR("Jot with id: %i not found!", id)); } DBGR(PRINT_RETURN("Found name: %s", qPrintable(name))); return name; }
int Jotter::setName(int id, QString jotName) { DBGS(PRINT_START("id: %i, jotName: %s", id, qPrintable(jotName))); int rv = ERROR_UNKNOWN_ERROR; Jot *jot = getJot(id); if (jot) { jot->setName(jotName); rv = ALL_OK; } else { DBGE(PRINT_ERROR("Jot with id: %i not found!", id)); } DBGR(PRINT_RETURN("rv: %i", rv)); return rv; }
ssize_t conf_erase(const char *file, const char *key) { FILE *stream, *stream_tmp; char *beg, line[LINE_MAX], file_tmp[PATH_MAX]; ssize_t key_len, rtn = -1; if ((key_len = __key_check_len(key)) < 0) return -1; if (!pathname_tmp(file, file_tmp, PATH_MAX)) return -1; if (!(stream = fopen(file, "r"))) return -1; if (!(stream_tmp = fopen(file_tmp, "w+"))) { DBGE("open %s failed", file_tmp); goto ERR_fopen_new; } rtn = 0; while ((beg=fgets(line, LINE_MAX, stream))) { while (isspace(*beg)) ++beg; if (0 == memcmp(key, beg, key_len)) { ++rtn; continue; /* delete one */ } if (fputs(line, stream_tmp) != EOF) continue; /* failed */ rtn = -1; break; } fclose(stream_tmp); ERR_fopen_new: fclose(stream); if (rtn != -1 && rename(file_tmp, file)) rtn = -1; return rtn; }
int DataBase::readJotterHeader() { DBGS(PRINT_START("")); int rv = ERROR_UNKNOWN_ERROR; QString headerName = KSILIT_JOTTER_DOM_HEADER_TAG; QDomNodeList headerList = domDoc.elementsByTagName(headerName); QDomNode headerNode = headerList.at(0); QDomElement headerElement = headerNode.toElement(); QString attributeJotCounter = headerElement.attribute(KSILIT_JOTTER_DOM_JOT_COUNTER); int jotCounter = attributeJotCounter.toInt(); rv = jotter->setJotCount(jotCounter); if (rv != ALL_OK) { DBGE(PRINT_ERROR("Error setting jotCounter!")); } DBGR(PRINT_RETURN("rv: %i", rv)); return rv; }
int main(int argc, char *argv[]) { DBGS(PRINT_START()); int rv = 0; rv = unitTestProcess(); if (rv == 0) { QApplication a(argc, argv); MainWindow w; w.show(); a.exec(); } else { DBGE(PRINT_ERROR("Unit tests fail!")); } DBGR(PRINT_RETURN("rv: %i", rv)); return rv; }
static ssize_t __write_item(int fd, const void *key, size_t key_len, const void *value, size_t value_len) { struct bin_item bi = { .magic = htole32(MAGIC_ITEM), .key_len = htole32(key_len), .value_len = htole32(value_len), .place_holder = 0, }; if (writen(fd, &bi, sizeof(bi)) != sizeof(bi)) return -1; if (writen(fd, key, key_len) != key_len) return -1; if (writen(fd, value, value_len) != value_len) return -1; return 0; } #if 0 static ssize_t __copy_left(int fd, int fd_new) { char buf[256]; size_t n; while ((n = readn(fd, buf, 256)) > 0) { if (write(fd_new, buf, n) != n) { DBGE("write buf %zu failed", n); return -1; } } return 0; }
static ssize_t __check_head(int fd) { uint32_t magic, version; ssize_t r; struct bin_head bh; if ((r = readn(fd, &bh, sizeof(bh))) != sizeof(bh)) { if (r == 0) return 0; /* empty file ? */ DBGE("readn bin_head %zd failed", r); return -1; } magic = le32toh(bh.magic); version = le32toh(bh.version); if (magic != MAGIC_HEAD || version < CONF_VERSION) { DBGP("magic(%x) or version(%x) error", magic, version); return -1; } return 0; }
/** * Writes a file to a flash or mem region * @param di Device info struct of opened and inited device * @param addr Address to write to * @param fname Path and filename to write * @returns 0 if OK, <0 on error */ int file_flash_write(devinfo_t *di, int addr, char* fname) { int fd, length; int ret; char *data; ret = file_open_mmap(fname, &fd, &length, &data); if (ret) return -1; ret = image_write_random_usb(di, addr, data, length); if (ret) { DBGE("Can't write file to flash\n"); goto out; } ret = 0; out: munmap(data, length); close(fd); return ret; }
/** * Writes a bootfile to flash including its PAT table * @param di Device info struct of opened and inited device * @param id ID to use in PAT table * @param patpage Number of the page to write the PAT to * @param datapage Number of the page to write the data to * @param filename name of the file to write */ int file_bootfile_write(devinfo_t *di, uint32_t id, int patpage, int datapage, char* fname) { int fd, length; int ret; char *data; ret = file_open_mmap(fname, &fd, &length, &data); if (ret) return -1; ret = image_write_bootfile_usb(di, id, patpage, datapage, data, length); if (ret) { DBGE("Can't write bootfile\n"); goto out; } ret = 0; out: munmap(data, length); close(fd); return ret; }
ssize_t conf_write(const char *file, const char *key, const char *value_fmt, ...) { va_list ap; FILE *stream, *stream_tmp; char *beg, line[LINE_MAX], file_tmp[PATH_MAX]; bool bwrote = false; ssize_t key_len, rtn = -1; if ((key_len = __key_check_len(key)) < 0) return -1; if (!pathname_tmp(file, file_tmp, PATH_MAX)) return -1; if (!(stream_tmp = fopen(file_tmp, "w+"))) { DBGE("open %s failed", file_tmp); return -1; } va_start(ap, value_fmt); if (!(stream = fopen(file, "r"))) goto WRITE_NEW; while ((beg=fgets(line, LINE_MAX, stream))) { if (bwrote) goto WRITE_DIRECT; while (isspace(*beg)) ++beg; if (*beg == '\0' || *beg == '\n' || *beg == '#' || memcmp(key, beg, key_len)) goto WRITE_DIRECT; if ((rtn = __gen_line_new(line, key, value_fmt, ap)) < 0) goto WRITE_FAIL; bwrote = true; WRITE_DIRECT: if ((rtn=fputs(line, stream_tmp)) == EOF) goto WRITE_FAIL; } WRITE_NEW: if (!bwrote) { if ((rtn = __gen_line_new(line, key, value_fmt, ap)) < 0) goto WRITE_FAIL; rtn = fputs(line, stream_tmp); } WRITE_FAIL: va_end(ap); fclose(stream_tmp); if (stream) fclose(stream); if (rtn != -1 && rename(file_tmp, file)) rtn = -1; /* return the value which returned by __gen_line_new() */ return rtn; }
/** * Dumps a flash or mem region to a file * @param di Device info struct of opened and inited device * @param ramflash FILE_DUMP_RAM or FILE_DUMP_FLASH * @param addr Address to start dump from * @param len Length in bytes to dump * @param fname Path and filename to write to * @returns 0 if OK, <0 on error */ static int file_mem_dump(devinfo_t *di, enum memtype ramflash, int addr, int len, char* fname) { int fd, ret; int wl = 0, poi = 0, i = 0; char *pagebuf; flashoffsets_t fo; fd = open(fname, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (fd == -1) { DBGE("Can't open output file: %s\n", strerror(errno)); return -1; } pagebuf = malloc(di->ps); if (pagebuf == NULL) { DBGE("Can't allocate space for page buffer\n"); close(fd); return -1; } if (ramflash == FLASH) { flash_offset_calc(di, &fo, addr, len); poi = len; } else fo.np = 0; DBG2("addr: %08X, len.%08X, fp: %08X, np: %08X\n", addr, len, fo.fp, fo.np); while ((poi < len) || i < fo.np) { if (ramflash == FLASH) { ret = cmd_read_flash_page(di, fo.fp + i, pagebuf); /* Whole page needs to be read, but only write part * of the last page if len is not multiple pagesize */ if (((i + 1) * di->ps) > len) wl = len - ((i - 1) * di->ps); else wl = di->ps; /* Next page */ i++; } else { /* Only read part of mem at end if len is not multiple * page size */ if ((poi + di->ps) > len) wl = len - poi; else wl = di->ps; ret = cmd_read_mem(di, addr + poi, wl, pagebuf); poi += wl; } if (ret) { DBGE("Can't read %s\n", (ramflash == FLASH) ? "flash" : "memory"); goto fail; } ret = write(fd, pagebuf, wl); if (ret < wl) { DBGE("Can't write to output file: %s\n", strerror(errno)); ret = -1; goto fail; } } ret = 0; fail: free(pagebuf); close(fd); return ret; }
int DataBase::read(QString filePath) { DBGS(PRINT_START("filePath: %s", qPrintable(filePath))); int rv = ERROR_UNKNOWN_ERROR; QFile fileIn(filePath); if (fileIn.open(QIODevice::ReadOnly)) { if (domDoc.setContent(&fileIn)) { rv = readJotterHeader(); if (rv == ALL_OK) { QString bodyName = KSILIT_JOTTER_DOM_BODY_TAG; QDomNodeList bodyList = domDoc.elementsByTagName(bodyName); QDomNode bodyNode = bodyList.at(0); QDomElement bodyElement = bodyNode.toElement(); if (!bodyElement.isNull()) { int rootId = 0; QString rootNodeName = KSILIT_JOTTER_DOM_ELEMENT_TAG; rootNodeName += QString::number(rootId); QDomNodeList rootNodeList = domDoc.elementsByTagName(rootNodeName); QDomNode rootNode = rootNodeList.at(0); QDomElement rootElement = rootNode.toElement(); QDomNode childNode = rootElement.firstChild(); while (!childNode.isNull()) { QDomElement childElement = childNode.toElement(); QString childTag = childElement.tagName(); DBG3(PRINT_DBG("childTag: %s", qPrintable(childTag))); QString attributeChildId = childElement.attribute(KSILIT_JOTTER_DOM_ELEMENT_ATTRIBUTE_ID); DBG3(PRINT_DBG("attributeChildId: %s", qPrintable(attributeChildId))); QString attributeChildName = childElement.attribute(KSILIT_JOTTER_DOM_ELEMENT_ATTRIBUTE_NAME); DBG3(PRINT_DBG("attributeChildName: %s", qPrintable(attributeChildName))); QString attributeChildText = childElement.attribute(KSILIT_JOTTER_DOM_ELEMENT_ATTRIBUTE_TEXT); DBG3(PRINT_DBG("attributeChildText: %s", qPrintable(attributeChildText))); QDomNode parentNode = childElement.parentNode(); if (!parentNode.isNull()) { QDomElement parentElement = parentNode.toElement(); if (!parentElement.isNull()) { int parentId = 0; QString attributeParentId = parentElement.attribute(KSILIT_JOTTER_DOM_ELEMENT_ATTRIBUTE_ID); if (!attributeParentId.isEmpty()) { parentId = attributeParentId.toInt(); } DBG3(PRINT_DBG("parentId: %i", parentId)); QStandardItem *parentItem = rootItem; if (parentId) { parentItem = getItemById(rootItem, parentId); } if (parentItem){ QStandardItem *childItem = new QStandardItem(); parentItem->appendRow(childItem); QVariant childData = attributeChildId; childItem->setData(childData); childItem->setText(attributeChildName); int childId = attributeChildId.toInt(); jotter->createJot(childId); jotter->setName(childId, attributeChildName); jotter->setText(childId, attributeChildText); } else { DBGE(PRINT_ERROR("parentItem not found!")); } } else { DBGE(PRINT_ERROR("Error converting parent node to element!")); } } else { DBGE(PRINT_ERROR("parentNode is null!")); } childNode = getNextNode(childNode, rootNode); if (childNode.isNull()) { rv = ALL_OK; } } } else { DBGE(PRINT_ERROR("rootElement is null!")); } } else { DBGE(PRINT_ERROR("Error reading jotter header!")); } } else { DBGE(PRINT_ERROR("Error setting DOM document content!")); } } else { DBGE(PRINT_ERROR("Error opening file: %s", qPrintable(filePath))); rv = ERROR_OPENING_FILE; } fileIn.close(); DBGR(PRINT_RETURN("rv: %i", rv)); return rv; }
void DataBase::updateDOMJotterBody(QStandardItem *item) { if (item != rootItem) { int parentId = 0; QStandardItem *parentItem = item->parent(); if (parentItem) { QVariant parentData = parentItem->data(); DBG3(PRINT_DBG("parentData: %s", qPrintable(parentData.toString()))); if (!parentData.isNull()) { parentId = parentData.toInt(); } DBG3(PRINT_DBG("parentId: %i", parentId)); } //If parentId still equal zero, than parent of the item is invisible root QString parentTag = KSILIT_JOTTER_DOM_ELEMENT_TAG; parentTag += QString::number(parentId); QDomNodeList parentNodeList = domDoc.elementsByTagName(parentTag); if (!parentNodeList.isEmpty()) { if (parentNodeList.count() == 1) { QDomNode parentNode = parentNodeList.at(0); QDomElement parentElement = parentNode.toElement(); if (!parentElement.isNull()) { QVariant childData = item->data(); int childId = childData.toInt(); QString childTag = KSILIT_JOTTER_DOM_ELEMENT_TAG; childTag += QString::number(childId); QDomElement childElement = domDoc.createElement(childTag); QString attributeChildId = QString::number(childId); DBG3(PRINT_DBG("attributeChildId: %s", qPrintable(attributeChildId))); childElement.setAttribute(KSILIT_JOTTER_DOM_ELEMENT_ATTRIBUTE_ID, attributeChildId); QString attributeChildName = jotter->getName(childId); DBG3(PRINT_DBG("attributeChildName: %s", qPrintable(attributeChildName))); childElement.setAttribute(KSILIT_JOTTER_DOM_ELEMENT_ATTRIBUTE_NAME, attributeChildName); QString attributeChildText = jotter->getText(childId); DBG3(PRINT_DBG("attributeChildText: %s", qPrintable(attributeChildText))); childElement.setAttribute(KSILIT_JOTTER_DOM_ELEMENT_ATTRIBUTE_TEXT, attributeChildText); parentElement.appendChild(childElement); } else { DBGE(PRINT_ERROR("Can't convert parentNode: %s", qPrintable(parentTag))); } } else { DBGE(PRINT_ERROR("Multiple difinition of parent tag: %s", qPrintable(parentTag))); } } else { DBGE(PRINT_ERROR("parentNodeList is empty! Parent tag: %s", qPrintable(parentTag))); } } else {//Current item is invisible root item QString ksilitName = KSILIT_DOM_TAG; QDomNodeList ksilitNodeList = domDoc.elementsByTagName(ksilitName); QDomNode ksilitNode = ksilitNodeList.at(0); QDomElement ksilitElement = ksilitNode.toElement(); QString bodyName = KSILIT_JOTTER_DOM_BODY_TAG; QDomElement bodyElement = domDoc.createElement(bodyName); ksilitElement.appendChild(bodyElement); int rootId = 0; QString rootName = KSILIT_JOTTER_DOM_ELEMENT_TAG; rootName += QString::number(rootId); QDomElement rootElement = domDoc.createElement(rootName); bodyElement.appendChild(rootElement); } int childCount = item->rowCount(); for (int i = 0; i < childCount; ++i) { QStandardItem *nextItem = item->child(i); updateDOMJotterBody(nextItem); } }