mirror of
https://github.com/tsukumijima/px4_drv.git
synced 2025-07-23 04:03:01 +02:00
Add some files
This commit is contained in:
9
README
Normal file
9
README
Normal file
@@ -0,0 +1,9 @@
|
||||
PLEX PX-W3U4/W3PE4 Unofficial Linux driver
|
||||
|
||||
(現在開発中です。)
|
||||
|
||||
PLEX PX-W3U4/W3PE4の非公式版Linux用ドライバです。
|
||||
PLEX社のサイト(http://plex-net.co.jp)にて配布されているドライバのソースコードではありませんのでご注意ください。
|
||||
|
||||
このドライバを使用するには、別途ファームウェアファイルを用意する必要があります。
|
||||
fwtoolを使用すると、簡単にファームウェアファイルを用意することができます。
|
1
etc/90-px4.rules
Normal file
1
etc/90-px4.rules
Normal file
@@ -0,0 +1 @@
|
||||
KERNEL=="px4video*" GROUP="video", MODE="0664"
|
18
fwtool/Makefile
Normal file
18
fwtool/Makefile
Normal file
@@ -0,0 +1,18 @@
|
||||
CC := gcc
|
||||
CFLAGS := -O2 -Wall
|
||||
|
||||
TARGET := fwtool
|
||||
OBJS := fwtool.o tsv.o crc32.o
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
clean:
|
||||
rm -vf $(TARGET) $(OBJS)
|
||||
|
||||
depend:
|
||||
$(CC) -MM $(OBJS:.o=.c) > Makefile.dep
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
$(CC) -o $@ $(OBJS)
|
||||
|
||||
-include Makefile.dep
|
3
fwtool/Makefile.dep
Normal file
3
fwtool/Makefile.dep
Normal file
@@ -0,0 +1,3 @@
|
||||
fwtool.o: fwtool.c tsv.h crc32.h
|
||||
tsv.o: tsv.c tsv.h
|
||||
crc32.o: crc32.c crc32.h
|
91
fwtool/crc32.c
Normal file
91
fwtool/crc32.c
Normal file
@@ -0,0 +1,91 @@
|
||||
// crc32.c
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "crc32.h"
|
||||
|
||||
static const uint32_t crc32_table[256] = {
|
||||
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
|
||||
0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
|
||||
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
|
||||
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
|
||||
0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
|
||||
0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
|
||||
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
|
||||
0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
|
||||
0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
|
||||
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
|
||||
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
|
||||
0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
|
||||
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
|
||||
0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
|
||||
0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
|
||||
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
|
||||
0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
|
||||
0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
|
||||
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
|
||||
0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
|
||||
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
|
||||
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
|
||||
0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
|
||||
0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
|
||||
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
|
||||
0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
|
||||
0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
|
||||
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
|
||||
0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
|
||||
0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
|
||||
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
|
||||
0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
|
||||
0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
|
||||
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
|
||||
0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
|
||||
0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
|
||||
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
|
||||
0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
|
||||
0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
|
||||
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
|
||||
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
|
||||
0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
|
||||
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
|
||||
0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
|
||||
0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
|
||||
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
|
||||
0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
|
||||
0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
|
||||
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
|
||||
0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
|
||||
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
|
||||
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
|
||||
0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
|
||||
0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
|
||||
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
|
||||
0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
|
||||
0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
|
||||
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
|
||||
0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
|
||||
0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
|
||||
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
|
||||
0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
|
||||
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
|
||||
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
|
||||
};
|
||||
|
||||
uint32_t crc32_calc(const void *buf, size_t size)
|
||||
{
|
||||
const uint8_t *p;
|
||||
size_t s;
|
||||
uint32_t crc;
|
||||
|
||||
p = buf;
|
||||
s = size;
|
||||
crc = 0xffffffff;
|
||||
|
||||
while (s) {
|
||||
crc = (crc >> 8) ^ crc32_table[(crc ^ *p++) & 0xff];
|
||||
s--;
|
||||
}
|
||||
|
||||
return ~crc;
|
||||
}
|
8
fwtool/crc32.h
Normal file
8
fwtool/crc32.h
Normal file
@@ -0,0 +1,8 @@
|
||||
// crc32.h
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
uint32_t crc32_calc(const void *buf, size_t size);
|
4
fwtool/fwinfo.tsv
Normal file
4
fwtool/fwinfo.tsv
Normal file
@@ -0,0 +1,4 @@
|
||||
# tab width: 4
|
||||
description size crc32 align firmware_code firmware_segment firmware_partition firmware_crc32
|
||||
PX-W3U4 BDA Ver.1.0 32bit 167296 4c2cf43f 4 00022528 00022da8 00022da1 0b41a994
|
||||
PX-W3U4 BDA Ver.1.0 64bit 189440 5743d95f 4 000287d0 00029050 00028666 0b41a994
|
Can't render this file because it has a wrong number of fields in line 2.
|
322
fwtool/fwtool.c
Normal file
322
fwtool/fwtool.c
Normal file
@@ -0,0 +1,322 @@
|
||||
// fwtool.c
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "tsv.h"
|
||||
#include "crc32.h"
|
||||
|
||||
#define le32_load(p) (*((uint8_t *)(p)) | *(((uint8_t *)(p)) + 1) << 8 | *(((uint8_t *)(p)) + 2) << 16 | *(((uint8_t *)(p)) + 3) << 24)
|
||||
|
||||
struct fwinfo {
|
||||
char *desc;
|
||||
long size;
|
||||
uint32_t crc32;
|
||||
uint8_t align;
|
||||
uint32_t code_ofs;
|
||||
uint32_t segment_ofs;
|
||||
uint32_t partition_ofs;
|
||||
uint32_t fw_crc32;
|
||||
};
|
||||
|
||||
static const char *name[] = {
|
||||
"description",
|
||||
"size",
|
||||
"crc32",
|
||||
"align",
|
||||
"firmware_code",
|
||||
"firmware_segment",
|
||||
"firmware_partition",
|
||||
"firmware_crc32"
|
||||
};
|
||||
|
||||
#define NAME_NUM (sizeof(name) / sizeof(name[0]))
|
||||
|
||||
static int load_file(const char *path, char **buf, long *size)
|
||||
{
|
||||
int ret = -1, fd;
|
||||
FILE *fp;
|
||||
struct stat stbuf;
|
||||
off_t sz;
|
||||
char *b;
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
fd = open(path, O_RDONLY | O_BINARY);
|
||||
#else
|
||||
fd = open(path, O_RDONLY);
|
||||
#endif
|
||||
if (fd == -1) {
|
||||
fprintf(stderr, "Couldn't open file '%s' to read.\n", path);
|
||||
goto end;
|
||||
}
|
||||
|
||||
fp = fdopen(fd, "rb");
|
||||
if (!fp) {
|
||||
fprintf(stderr, "fdopen() failed.\n");
|
||||
close(fd);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (fstat(fd, &stbuf) == -1) {
|
||||
fprintf(stderr, "fstat() failed.\n");
|
||||
goto end2;
|
||||
}
|
||||
|
||||
sz = stbuf.st_size;
|
||||
|
||||
b = malloc(sz);
|
||||
if (b == NULL) {
|
||||
fprintf(stderr, "No enough memory.\n");
|
||||
goto end2;
|
||||
}
|
||||
|
||||
if (fread(b, sz, 1, fp) < 1) {
|
||||
fprintf(stderr, "Incorrect read size.\n");
|
||||
free(b);
|
||||
}
|
||||
else {
|
||||
*buf = b;
|
||||
*size = sz;
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
end2:
|
||||
fclose(fp);
|
||||
end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int load_tsv_file(struct tsv_data **tsv)
|
||||
{
|
||||
int ret = -1;
|
||||
char *buf;
|
||||
long size;
|
||||
|
||||
ret = load_file("fwinfo.tsv", &buf, &size);
|
||||
if (ret == -1) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = tsv_load(buf, size, tsv);
|
||||
if (ret == -1) {
|
||||
fprintf(stderr, "File 'fwinfo.tsv' is invalid.\n");
|
||||
}
|
||||
|
||||
free(buf);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int load_fwinfo(struct tsv_data *tsv, struct fwinfo *fi, int num)
|
||||
{
|
||||
int i, j;
|
||||
int col_num = tsv->col_num;
|
||||
int name_map[NAME_NUM];
|
||||
|
||||
for (i = 0; i < NAME_NUM; i++) {
|
||||
name_map[i] = -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < col_num; i++) {
|
||||
for (j = 0; j < NAME_NUM; j++) {
|
||||
if (name_map[j] == -1 && !strcmp(tsv->name[i], name[j])) {
|
||||
name_map[j] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < NAME_NUM; i++) {
|
||||
if (name_map[i] == -1) {
|
||||
fprintf(stderr, "No enough columns in 'fwinfo.tsv'.\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < num; i++) {
|
||||
fi[i].desc = tsv->field[i][name_map[0]];
|
||||
fi[i].size = strtol(tsv->field[i][name_map[1]], NULL, 10);
|
||||
fi[i].crc32 = strtoul(tsv->field[i][name_map[2]], NULL, 16);
|
||||
fi[i].align = strtoul(tsv->field[i][name_map[3]], NULL, 10);
|
||||
fi[i].code_ofs = strtoul(tsv->field[i][name_map[4]], NULL, 16);
|
||||
fi[i].segment_ofs = strtoul(tsv->field[i][name_map[5]], NULL, 16);
|
||||
fi[i].partition_ofs = strtoul(tsv->field[i][name_map[6]], NULL, 16);
|
||||
fi[i].fw_crc32 = strtoul(tsv->field[i][name_map[7]], NULL, 16);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int output_firmware(struct fwinfo *fi, const char *buf, long size, const char *path)
|
||||
{
|
||||
int i, n;
|
||||
uint8_t align;
|
||||
uint32_t partition_ofs, segment_ofs, code_ofs, crc32;
|
||||
size_t code_len = 0;
|
||||
FILE *fp;
|
||||
|
||||
align = fi->align;
|
||||
partition_ofs = fi->partition_ofs;
|
||||
segment_ofs = fi->segment_ofs;
|
||||
code_ofs = fi->code_ofs;
|
||||
|
||||
if (align & (align - 1)) {
|
||||
fprintf(stderr, "Invalid alignment value.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (align < 4) {
|
||||
align = 4;
|
||||
}
|
||||
|
||||
if (size < partition_ofs + 1) {
|
||||
fprintf(stderr, "Invalid file size.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
n = buf[partition_ofs];
|
||||
|
||||
if (size < segment_ofs + (n * align * 2)) {
|
||||
fprintf(stderr, "Invalid file size.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
uint32_t type;
|
||||
|
||||
type = le32_load(&buf[segment_ofs + (i * align * 2)]);
|
||||
|
||||
if (type != 0x01) {
|
||||
fprintf(stderr, "This driver's firmware is not supported.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
code_len += le32_load(&buf[segment_ofs + (i * align * 2) + (align * 1)]);
|
||||
}
|
||||
|
||||
if (size < code_ofs + code_len) {
|
||||
fprintf(stderr, "Invalid file size.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
crc32 = crc32_calc(&buf[code_ofs], code_len);
|
||||
|
||||
fprintf(stderr, "Firmware length: %lu %s\n", code_len, (code_len == 1) ? "byte" : "bytes");
|
||||
fprintf(stderr, "Firmware CRC32: %08x\n", crc32);
|
||||
|
||||
if (fi->fw_crc32 && crc32 != fi->fw_crc32) {
|
||||
fprintf(stderr, "Incorrect CRC32!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fp = fopen(path, "wb");
|
||||
if (!fp) {
|
||||
fprintf(stderr, "Couldn't open file '%s' to write.\n", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fwrite(&buf[code_ofs], code_len, 1, fp) < 1) {
|
||||
fprintf(stderr, "Incorrect write size.\n");
|
||||
fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void usage()
|
||||
{
|
||||
fprintf(stderr, "usage: fwtool <driver binary> <output>\n");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ret, num, i;
|
||||
struct tsv_data *tsv;
|
||||
struct fwinfo *fi;
|
||||
char *buf;
|
||||
long size;
|
||||
|
||||
fprintf(stderr, "fwtool for px4 drivers\n\n");
|
||||
|
||||
if (argc < 3) {
|
||||
usage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
fprintf(stderr, "Driver file: %s\n", argv[1]);
|
||||
fprintf(stderr, "Output file: %s\n\n", argv[2]);
|
||||
|
||||
ret = load_tsv_file(&tsv);
|
||||
if (ret) {
|
||||
fprintf(stderr, "Failed to load firmware information file.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
num = tsv->row_num;
|
||||
|
||||
if (!num) {
|
||||
fprintf(stderr, "No rows in 'fwinfo.tsv'.\n");
|
||||
goto end;
|
||||
}
|
||||
|
||||
fi = malloc(sizeof(struct fwinfo) * num);
|
||||
if (!fi) {
|
||||
fprintf(stderr, "No enough memory.\n");
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = load_fwinfo(tsv, fi, num);
|
||||
if (ret) {
|
||||
fprintf(stderr, "Failed to load firmware information.\n");
|
||||
goto end2;
|
||||
}
|
||||
|
||||
ret = load_file(argv[1], &buf, &size);
|
||||
if (ret == -1) {
|
||||
fprintf(stderr, "Failed to load driver file.\n");
|
||||
goto end2;
|
||||
}
|
||||
|
||||
for (i = 0; i < num; i++) {
|
||||
if (size == fi[i].size && crc32_calc(buf, size) == fi[i].crc32) {
|
||||
fprintf(stderr, "Driver description: %s\n", fi[i].desc);
|
||||
ret = output_firmware(&fi[i], buf, size, argv[2]);
|
||||
if (!ret) {
|
||||
fprintf(stderr, "OK.\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (i >= num) {
|
||||
fprintf(stderr, "Unknown driver file.\n");
|
||||
goto end3;
|
||||
}
|
||||
|
||||
free(buf);
|
||||
free(fi);
|
||||
tsv_free(tsv);
|
||||
|
||||
return 0;
|
||||
|
||||
end3:
|
||||
free(buf);
|
||||
end2:
|
||||
free(fi);
|
||||
end:
|
||||
tsv_free(tsv);
|
||||
|
||||
return 1;
|
||||
}
|
176
fwtool/tsv.c
Normal file
176
fwtool/tsv.c
Normal file
@@ -0,0 +1,176 @@
|
||||
// tsv.c
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "tsv.h"
|
||||
|
||||
int load(const char *buf, size_t buf_len, struct tsv_data *tsv, void *str_pool, size_t *str_poolsize)
|
||||
{
|
||||
const char *p = buf;
|
||||
char *sp = str_pool;
|
||||
size_t l = buf_len, npl = 0, pl = 0;
|
||||
int col_num = 0, row_num = 0;
|
||||
|
||||
if (sp && str_poolsize) {
|
||||
pl = *str_poolsize;
|
||||
}
|
||||
|
||||
while (l && *p != '\0') {
|
||||
int n = 0; // number of columns (current line)
|
||||
int newline = 0;
|
||||
|
||||
if (*p == '#') {
|
||||
// comment
|
||||
p++;
|
||||
l--;
|
||||
while (l && *p != '\0') {
|
||||
p++;
|
||||
l--;
|
||||
if (*(p - 1) == '\r') {
|
||||
if (l && *p == '\n') {
|
||||
p++;
|
||||
l--;
|
||||
}
|
||||
break;
|
||||
} else if (*(p - 1) == '\n') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
do {
|
||||
const char *pb, *pp = NULL;
|
||||
|
||||
pb = p;
|
||||
|
||||
while(l && *p != '\0') {
|
||||
if (*p == '\t') {
|
||||
pp = p;
|
||||
while(--l && *++p == '\t')
|
||||
;
|
||||
break;
|
||||
} else if (*p == '\r') {
|
||||
pp = p;
|
||||
p++;
|
||||
l--;
|
||||
newline = 1;
|
||||
if (l && *p == '\n') {
|
||||
p++;
|
||||
l--;
|
||||
}
|
||||
break;
|
||||
} else if (*p == '\n') {
|
||||
pp = p;
|
||||
p++;
|
||||
l--;
|
||||
newline = 1;
|
||||
break;
|
||||
}
|
||||
p++;
|
||||
l--;
|
||||
}
|
||||
|
||||
if (!pp) {
|
||||
pp = p;
|
||||
}
|
||||
|
||||
if (pp > pb) {
|
||||
size_t sl;
|
||||
|
||||
sl = pp - pb;
|
||||
|
||||
if (pl) {
|
||||
strncpy(sp, pb, sl);
|
||||
sp[sl] = '\0';
|
||||
|
||||
if (!col_num) {
|
||||
tsv->name[n] = sp;
|
||||
} else {
|
||||
tsv->field[row_num - 1][n] = sp;
|
||||
}
|
||||
sp += sl + 1;
|
||||
pl -= sl + 1;
|
||||
}
|
||||
npl += sl + 1;
|
||||
n++;
|
||||
}
|
||||
} while(!newline && l && *p != '\0');
|
||||
|
||||
if (n) {
|
||||
if (!col_num) {
|
||||
col_num = n;
|
||||
} else if (col_num != n) {
|
||||
errno = EBADMSG;
|
||||
return -1;
|
||||
}
|
||||
|
||||
row_num++;
|
||||
}
|
||||
}
|
||||
|
||||
tsv->col_num = col_num;
|
||||
tsv->row_num = row_num - 1;
|
||||
|
||||
if (!str_pool && str_poolsize) {
|
||||
*str_poolsize = npl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int tsv_load(const char *buf, size_t len, struct tsv_data **tsv)
|
||||
{
|
||||
int ret = 0, i;
|
||||
struct tsv_data tsv_tmp, *tsv_ret;
|
||||
char *pool;
|
||||
size_t array_poolsize, str_poolsize;
|
||||
|
||||
if (!buf || !len || !tsv) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = load(buf, len, &tsv_tmp, NULL, &str_poolsize);
|
||||
if (ret) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
array_poolsize = /* name */(sizeof(char **) * tsv_tmp.col_num) + /* field */((sizeof(char ***) * tsv_tmp.row_num) + (sizeof(char **) * tsv_tmp.col_num * tsv_tmp.row_num));
|
||||
|
||||
tsv_ret = (struct tsv_data *)malloc(sizeof(*tsv_ret) + array_poolsize + str_poolsize);
|
||||
if (!tsv_ret) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
pool = (char *)(tsv_ret + 1);
|
||||
|
||||
tsv_ret->name = (char **)pool;
|
||||
pool += sizeof(char **) * tsv_tmp.col_num;
|
||||
|
||||
tsv_ret->field = (char ***)pool;
|
||||
pool += sizeof(char ***) * tsv_tmp.row_num;
|
||||
|
||||
for (i = 0; i < tsv_tmp.row_num; i++) {
|
||||
tsv_ret->field[i] = (char **)pool;
|
||||
pool += sizeof(char **) * tsv_tmp.col_num;
|
||||
}
|
||||
|
||||
ret = load(buf, len, tsv_ret, pool, &str_poolsize);
|
||||
if (ret) {
|
||||
free(tsv_ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
*tsv = tsv_ret;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void tsv_free(struct tsv_data *tsv)
|
||||
{
|
||||
if (tsv)
|
||||
free(tsv);
|
||||
}
|
13
fwtool/tsv.h
Normal file
13
fwtool/tsv.h
Normal file
@@ -0,0 +1,13 @@
|
||||
// tsv.h
|
||||
|
||||
#pragma once
|
||||
|
||||
struct tsv_data {
|
||||
int col_num;
|
||||
int row_num;
|
||||
char **name;
|
||||
char ***field;
|
||||
};
|
||||
|
||||
int tsv_load(const char *buf, size_t len, struct tsv_data **tsv);
|
||||
void tsv_free(struct tsv_data *tsv);
|
16
include/ptx_ioctl.h
Normal file
16
include/ptx_ioctl.h
Normal file
@@ -0,0 +1,16 @@
|
||||
// ptx_ioctl.h
|
||||
|
||||
#ifndef __PTX_IOCTL_H__
|
||||
#define __PTX_IOCTL_H__
|
||||
|
||||
struct ptx_freq {
|
||||
int freq_no;
|
||||
int slot;
|
||||
};
|
||||
|
||||
#define PTX_SET_CHANNEL _IOW(0x8d, 0x01, struct ptx_freq)
|
||||
#define PTX_START_STREAM _IO(0x8d, 0x02)
|
||||
#define PTX_STOP_STREAM _IO(0x8d, 0x03)
|
||||
#define PTX_GET_SIGNAL_STRENGTH _IOR(0x8d, 0x04, int *)
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user