From e12881501583b7366a9fee2bd119730bd2734700 Mon Sep 17 00:00:00 2001 From: Joseph Poirier Date: Fri, 18 Dec 2015 10:43:08 -0600 Subject: [PATCH] export eeprom/info constants, moc testing dev --- moc_test/Makefile | 2 +- moc_test/gortlsdr/librtlsdr.c | 138 +++++++++++++++++++--------------- moc_test/main.go | 90 +++++++++++++++------- rtlsdr.go | 30 ++++---- 4 files changed, 155 insertions(+), 105 deletions(-) diff --git a/moc_test/Makefile b/moc_test/Makefile index dcb0968..5832d6c 100644 --- a/moc_test/Makefile +++ b/moc_test/Makefile @@ -9,7 +9,7 @@ all: cd gortlsdr && CC="gcc -Dmock" go build -o gortlsdr.a rtlsdr.go exports.go go build --ldflags '-extldflags "-L./gortlsdr"' main.go rm ./gortlsdr/rtlsdr.go ./gortlsdr/exports.go - + ./main clean: rm -f main ./gortlsdr/*.o ./gortlsdr/*.a ./gortlsdr/rtlsdr.go ./gortlsdr/exports.go ./gortlsdr/*.0 ./gortlsdr/*.so diff --git a/moc_test/gortlsdr/librtlsdr.c b/moc_test/gortlsdr/librtlsdr.c index 110a691..8c44148 100644 --- a/moc_test/gortlsdr/librtlsdr.c +++ b/moc_test/gortlsdr/librtlsdr.c @@ -22,6 +22,8 @@ #define EEPROM_SIZE (256) #define DEFAULT_BUF_NUMBER (15) #define DEFAULT_BUF_LENGTH (16 * 32 * 512) +#define STRINGS_OFFSET_START (9) +#define MAX_RAW_STR_SZ (2*35+2) // struct rtlsdr_dev { // libusb_context *ctx; @@ -74,9 +76,6 @@ struct rtlsdr_dev { int offset_tuning; enum rtlsdr_tuner type; char eeprom_buffer[256]; - char manufact[37]; // fat pointer - char product[37]; // fat pointer - char serial[37]; // fat pointer int gains[DEVICE_GAIN_CNT]; char xbuf[DEFAULT_BUF_LENGTH]; }; @@ -110,12 +109,6 @@ void do_init(void) { if (is_initialized) return; - s0.manufact[0] = 7; - strcpy(&(s0.manufact[1]), "REALTEK\0"); - s0.product[0] = 7; - strcpy(&(s0.product[1]), "NOOELEC\0") ; - s0.serial[0] = 4; - strcpy(&(s0.serial[1]), "1991\0"); s0 = (struct rtlsdr_dev){ .status = false, .ppm = 50, @@ -132,9 +125,9 @@ void do_init(void) { .direct_sampling_mode = 1, .offset_tuning = 1, .type = RTLSDR_TUNER_R828D, - .eeprom_buffer = {0x28, 0x32, 0x09, 0x01, 0x01, 0x01, 0xA5, 0x03, - 0x1F, 0x03, 'R', 0x00, 'E', 0x00, 'A', 0x00, 'L', 0x00, 'T', 0x00, 'E', 0x00, 'K', 0x00, - 0x1F, 0x03, 'N', 0x00, 'O', 0x00, 'O', 0x00, 'E', 0x00, 'L', 0x00, 'E', 0x00, 'C', 0x00, + .eeprom_buffer = {0x28, 0x32, 0x09, 0x01, 0x01, 0x01, 0xA5, 0x03, 0x00, + 0x10, 0x03, 'R', 0x00, 'E', 0x00, 'A', 0x00, 'L', 0x00, 'T', 0x00, 'E', 0x00, 'K', 0x00, + 0x10, 0x03, 'N', 0x00, 'O', 0x00, 'O', 0x00, 'E', 0x00, 'L', 0x00, 'E', 0x00, 'C', 0x00, 0x0A, 0x03, '1', 0x00, '9', 0x00, '9', 0x00, '1', 0x00}, .gains = { 0, 9, 14, 27, 37, 77, 87, 125, 144, 157, 166, 197, 207, 229, 254, 280, 297, 328, @@ -142,12 +135,6 @@ void do_init(void) { 445, 480, 496} }; - s1.manufact[0] = 7; - strcpy(&(s0.manufact[1]), "REALTEK\0"); - s1.product[0] = 7; - strcpy(&(s0.product[1]), "NOOELEC\0") ; - s1.serial[0] = 4; - strcpy(&(s0.serial[1]), "2992\0"); s1 = (struct rtlsdr_dev){ .status = false, .ppm = 51, @@ -164,9 +151,9 @@ void do_init(void) { .direct_sampling_mode = 0, .offset_tuning = 0, .type = RTLSDR_TUNER_R820T, - .eeprom_buffer = {0x28, 0x32, 0x09, 0x01, 0x01, 0x01, 0xA5, 0x03, - 0x1F, 0x03, 'R', 0x00, 'E', 0x00, 'A', 0x00, 'L', 0x00, 'T', 0x00, 'E', 0x00, 'K', 0x00, - 0x1F, 0x03, 'N', 0x00, 'O', 0x00, 'O', 0x00, 'E', 0x00, 'L', 0x00, 'E', 0x00, 'C', 0x00, + .eeprom_buffer = {0x28, 0x32, 0x09, 0x01, 0x01, 0x01, 0xA5, 0x03, 0x00, + 0x10, 0x03, 'R', 0x00, 'E', 0x00, 'A', 0x00, 'L', 0x00, 'T', 0x00, 'E', 0x00, 'K', 0x00, + 0x10, 0x03, 'N', 0x00, 'O', 0x00, 'O', 0x00, 'E', 0x00, 'L', 0x00, 'E', 0x00, 'C', 0x00, 0x0A, 0x03, '2', 0x00, '9', 0x00, '9', 0x00, '2', 0x00}, .gains = { 0, 9, 14, 27, 37, 77, 87, 125, 144, 157, 166, 197, 207, 229, 254, 280, 297, 328, @@ -174,12 +161,6 @@ void do_init(void) { 445, 480, 496} }; - s2.manufact[0] = 7; - strcpy(&(s0.manufact[1]), "REALTEK\0"); - s2.product[0] = 7; - strcpy(&(s0.product[1]), "NOOELEC\0") ; - s2.serial[0] = 4; - strcpy(&(s0.serial[1]), "3993\0"); s2 = (struct rtlsdr_dev){ .status = false, .ppm = 52, @@ -196,9 +177,9 @@ void do_init(void) { .direct_sampling_mode = 1, .offset_tuning = 1, .type = RTLSDR_TUNER_E4000, - .eeprom_buffer = {0x28, 0x32, 0x09, 0x01, 0x01, 0x01, 0xA5, 0x03, - 0x1F, 0x03, 'R', 0x00, 'E', 0x00, 'A', 0x00, 'L', 0x00, 'T', 0x00, 'E', 0x00, 'K', 0x00, - 0x1F, 0x03, 'N', 0x00, 'O', 0x00, 'O', 0x00, 'E', 0x00, 'L', 0x00, 'E', 0x00, 'C', 0x00, + .eeprom_buffer = {0x28, 0x32, 0x09, 0x01, 0x01, 0x01, 0xA5, 0x03, 0x00, + 0x10, 0x03, 'R', 0x00, 'E', 0x00, 'A', 0x00, 'L', 0x00, 'T', 0x00, 'E', 0x00, 'K', 0x00, + 0x10, 0x03, 'N', 0x00, 'O', 0x00, 'O', 0x00, 'E', 0x00, 'L', 0x00, 'E', 0x00, 'C', 0x00, 0x0A, 0x03, '3', 0x00, '9', 0x00, '9', 0x00, '3', 0x00}, .gains = { 0, 9, 14, 27, 37, 77, 87, 125, 144, 157, 166, 197, 207, 229, 254, 280, 297, 328, @@ -245,36 +226,59 @@ int rtlsdr_get_usb_strings(rtlsdr_dev_t *dev, char *manufact, char *product, cha if (!dev || !dev_valid(dev)) return -1; + char *p; if (dev == &s0) { - memcpy(manufact, &s0.manufact[1], s0.manufact[0]); - memcpy(product, &s0.product[1], s0.product[0]); - memcpy(serial, &s0.serial[1], s0.serial[0]); - } else if (dev == &s1) { - memcpy(manufact, &s1.manufact[1], s1.manufact[0]); - memcpy(product, &s1.product[1], s1.product[0]); - memcpy(serial, &s1.serial[1], s1.serial[0]); + p = &(s0.eeprom_buffer[STRINGS_OFFSET_START]); } else if (dev == &s1) { - memcpy(manufact, &s2.manufact[1], s2.manufact[0]); - memcpy(product, &s2.product[1], s2.product[0]); - memcpy(serial, &s2.serial[1], s2.serial[0]); + p = &(s1.eeprom_buffer[STRINGS_OFFSET_START]); + } else if (dev == &s2) { + p = &(s2.eeprom_buffer[STRINGS_OFFSET_START]); } else { - return -3; + return -2; + } + + int i, j, sz; + + sz = p[0]; + if (manufact) { + for (i = 0, j = 0; j < (sz-2); i++, j+=2) { + manufact[i] = p[2+j]; + } + manufact[i] = '\0'; + } + + p += sz; + sz = p[0]; + if (product) { + for (i = 0, j = 0; j < (sz-2); i++, j+=2) { + product[i] = p[2+j]; + } + product[i] = '\0'; + } + + p += sz; + sz = p[0]; + if (serial) { + for (i = 0, j = 0; j < (sz-2); i++, j+=2) { + serial[i] = p[2+j]; + } + serial[i] = '\0'; + } return 0; } -// TODO: int rtlsdr_write_eeprom(rtlsdr_dev_t *dev, uint8_t *data, uint8_t offset, uint16_t len) { if (!dev || !dev_valid(dev)) return -1; - if ((len + offset) > EEPROM_SIZE) + if (len > (EEPROM_SIZE-offset)) return -2; - // int i; - // for (i = 0; i < len; i++) - // eeprom_buffer[i+offset] = data[i]; + if (memcpy(&(dev->eeprom_buffer[offset]), data, len) == 0) + return -3; + return 0; } @@ -282,7 +286,7 @@ int rtlsdr_read_eeprom(rtlsdr_dev_t *dev, uint8_t *data, uint8_t offset, uint16_ if (!dev || !dev_valid(dev)) return -1; - if ((len + offset) > EEPROM_SIZE) + if (len > (EEPROM_SIZE-offset)) return -2; char *p; @@ -461,23 +465,18 @@ const char *rtlsdr_get_device_name(uint32_t index) { int rtlsdr_get_device_usb_strings(uint32_t index, char *manufact, char *product, char *serial) { do_init(); + rtlsdr_dev_t *dev; if (index == 0) { - memcpy(manufact, &s0.manufact[1], s0.manufact[0]); - memcpy(product, &s0.product[1], s0.product[0]); - memcpy(serial, &s0.serial[1], s0.serial[0]); + dev = &s0; } else if (index == 1) { - memcpy(manufact, &s1.manufact[1], s1.manufact[0]); - memcpy(product, &s1.product[1], s1.product[0]); - memcpy(serial, &s1.serial[1], s1.serial[0]); + dev = &s1; } else if (index == 2) { - memcpy(manufact, &s2.manufact[1], s2.manufact[0]); - memcpy(product, &s2.product[1], s2.product[0]); - memcpy(serial, &s2.serial[1], s2.serial[0]); + dev = &s2; } else { return -1; } - return 0; + return rtlsdr_get_usb_strings(dev, manufact, product, serial); } int rtlsdr_get_index_by_serial(const char *serial) { @@ -486,12 +485,27 @@ int rtlsdr_get_index_by_serial(const char *serial) { if (!serial) return -1; - if (!strcmp(serial, &(s0.serial[1]))) { + char s[40]; + + int e = rtlsdr_get_usb_strings(&s0, 0, 0, &s[0]); + if (e != 0) + return e; + if (!strcmp(serial, &s[0])) { + return 0; + } + + e = rtlsdr_get_usb_strings(&s1, 0, 0, &s[0]); + if (e != 0) + return e; + if (!strcmp(serial, &s[0])) { + return 0; + } + + e = rtlsdr_get_usb_strings(&s2, 0, 0, &s[0]); + if (e != 0) + return e; + if (!strcmp(serial, &s[0])) { return 0; - } else if (!strcmp(serial, &(s1.serial[1]))) { - return 1; - } else if (!strcmp(serial, &(s1.serial[1]))) { - return 2; } return -2; diff --git a/moc_test/main.go b/moc_test/main.go index a9b7af2..45c34c0 100644 --- a/moc_test/main.go +++ b/moc_test/main.go @@ -63,22 +63,39 @@ func GetXtalFreq(d *rtl.Context, i int) { } func GetUsbStrings(d *rtl.Context, i int) { - if _, _, _, err := d.GetUsbStrings(); err != nil { + if m, p, s, err := d.GetUsbStrings(); err != nil { failed++ log.Printf("--- FAILED, GetUsbStrings i:%d - %s\n", i, err) } else { passed++ log.Printf("--- PASSED, GetUsbStrings i:%d\n", i) + fmt.Println(m, p, s) } } -// func WriteEeprom(d *rtl.Context, i int) { -// d.WriteEeprom(data, offset, leng) -// } +func WriteEeprom(d *rtl.Context, i int) { + data := make([]byte, rtl.EepromSize, rtl.EepromSize) + + if err := d.ReadEeprom(data, 0, rtl.EepromSize); err != nil { + failed++ + log.Printf("--- FAILED, ReadEeprom i:%d - %s\n", i, err) + } else { + passed++ + log.Printf("--- PASSED, ReadEeprom i:%d\n", i) + } + + if err := d.WriteEeprom(data, 0, rtl.EepromSize); err != nil { + failed++ + log.Printf("--- FAILED, WriteEeprom i:%d - %s\n", i, err) + } else { + passed++ + log.Printf("--- PASSED, WriteEeprom i:%d\n", i) + } +} func ReadEeprom(d *rtl.Context, i int) { - data := make([]byte, 72, 256) - if err := d.ReadEeprom(data, 9, 72); err != nil { + data := make([]byte, rtl.EepromSize, rtl.EepromSize) + if err := d.ReadEeprom(data, 0, rtl.EepromSize); err != nil { failed++ log.Printf("--- FAILED, ReadEeprom i:%d - %s\n", i, err) } else { @@ -173,6 +190,7 @@ func SetTunerBw(d *rtl.Context, i int) { } } +// not implemented // func GetTunerBw(d *rtl.Context, i int) { // d.GetTunerBw // } @@ -324,13 +342,39 @@ func ReadSync(d *rtl.Context, i int) { // } -// func GetHwInfo(d *rtl.Context, i int) { +func GetHwInfo(d *rtl.Context, i int) { + if info, err := d.GetHwInfo(); err != nil { + failed++ + log.Printf("--- FAILED, GetHwInfo i:%d - %s\n", i, err) + } else { + passed++ + log.Printf("--- PASSED, GetHwInfo i:%d\n", i) + fmt.Println(info) + } +} -// } +func SetHwInfo(d *rtl.Context, i int) { + var err error + var info rtl.HwInfo -// func SetHwInfo(d *rtl.Context, i int) { + if info, err = d.GetHwInfo(); err != nil { + failed++ + log.Printf("--- FAILED, GetHwInfo i:%d - %s\n", i, err) + } else { + passed++ + log.Printf("--- PASSED, GetHwInfo i:%d\n", i) + fmt.Println(info) + } -// } + if err = d.SetHwInfo(info); err != nil { + failed++ + log.Printf("--- FAILED, SetHwInfo i:%d - %s\n", i, err) + } else { + passed++ + log.Printf("--- PASSED, SetHwInfo i:%d\n", i) + fmt.Println(info) + } +} func main() { var cnt int @@ -366,13 +410,14 @@ func main() { serials := make([]string, 3) for i := 0; i < cnt; i++ { - if _, _, s, err := GetDeviceUsbStrings(i); err != nil { + if m, p, s, err := GetDeviceUsbStrings(i); err != nil { failed++ log.Printf("--- FAILED, GetDeviceUsbStrings i:%d, %s\n", i, err) } else { passed++ - serials = append(serials, s) + serials[i] = s log.Printf("--- PASSED, GetDeviceUsbStrings i:%d\n", i) + fmt.Println(m, p, s) } } @@ -396,16 +441,6 @@ func main() { } } - for i, s := range []string{"One", "Two", "Three"} { - if _, err := GetIndexBySerial(s); err == nil { - failed++ - log.Printf("--- FAILED, GetIndexBySerial i:%d - %s\n", i, err) - } else { - passed++ - log.Printf("--- PASSED, GetIndexBySerial i:%d\n", i) - } - } - for i := 5; i < 10; i++ { if d, err := rtl.Open(i); err == nil { failed++ @@ -433,10 +468,10 @@ func main() { GetXtalFreq(d, i) SetXtalFreq(d, i) - GetUsbStrings(d, i) - + WriteEeprom(d, i) ReadEeprom(d, i) - // WriteEeprom(d, i) + + GetUsbStrings(d, i) GetCenterFreq(d, i) SetCenterFreq(d, i) @@ -449,6 +484,7 @@ func main() { GetTunerGains(d, i) SetTunerGain(d, i) + // not implemented // GetTunerBw(d, i) SetTunerBw(d, i) @@ -471,8 +507,8 @@ func main() { ResetBuffer(d, i) - // GetHwInfo(d, i) - // SetHwInfo(d, i) + SetHwInfo(d, i) + GetHwInfo(d, i) ReadSync(d, i) // ReadAsync(d, i) diff --git a/rtlsdr.go b/rtlsdr.go index 147d656..7796579 100644 --- a/rtlsdr.go +++ b/rtlsdr.go @@ -79,11 +79,11 @@ type HwInfo struct { } const ( - eepromSize = 256 - // maxStrSize = (max string length - 2 (header bytes)) \ 2. Where each + EepromSize = 256 + // MaxStrSize = (max string length - 2 (header bytes)) \ 2. Where each // info character is followed by a null char. - maxStrSize = 35 - strOffsetStart = 0x09 + MaxStrSize = 35 + StrOffsetStart = 0x09 ) // SamplingMode is the sampling mode type. @@ -107,7 +107,7 @@ const ( // Note, librtlsdr's SetFreqCorrection returns an error value of // -2 when the current ppm is the same as the requested ppm, but // gortlsdr replaces the -2 with nil. Also, most of librtlsdr's -// functions return 0 on succes and -1 when dev is invalid but +// functions return 0 on success and -1 when dev is invalid but // some return 0 when dev is invalid, go figure. const ( libSuccess = iota * -1 @@ -584,8 +584,8 @@ func (dev *Context) CancelAsync() error { // GetHwInfo gets the dongle's information items. func (dev *Context) GetHwInfo() (info HwInfo, err error) { - data := make([]uint8, eepromSize) - if err = dev.ReadEeprom(data, 0, eepromSize); err != nil { + data := make([]uint8, EepromSize) + if err = dev.ReadEeprom(data, 0, EepromSize); err != nil { return } if (data[0] != 0x28) || (data[1] != 0x32) { @@ -609,7 +609,7 @@ func (dev *Context) GetHwInfo() (info HwInfo, err error) { // SetHwInfo sets the dongle's information items. func (dev *Context) SetHwInfo(info HwInfo) (err error) { - data := make([]uint8, eepromSize) + data := make([]uint8, EepromSize) data[0] = 0x28 data[1] = 0x32 data[2] = uint8(info.VendorID) @@ -629,16 +629,16 @@ func (dev *Context) SetHwInfo(info HwInfo) (err error) { if err = SetStringDescriptors(info, data); err != nil { return err } - return dev.WriteEeprom(data, 0, eepromSize) + return dev.WriteEeprom(data, 0, EepromSize) } // GetStringDescriptors gets the manufacturer, product, and serial // strings from the hardware's eeprom. func GetStringDescriptors(data []uint8) (manufact, product, serial string, err error) { - pos := strOffsetStart + pos := StrOffsetStart for _, v := range []*string{&manufact, &product, &serial} { l := int(data[pos]) - if l > (maxStrSize*2)+2 { + if l > (MaxStrSize*2)+2 { err = errors.New("string value too long") return } @@ -663,20 +663,20 @@ func GetStringDescriptors(data []uint8) (manufact, product, serial string, err e // strings on the hardware's eeprom. func SetStringDescriptors(info HwInfo, data []uint8) (err error) { e := "" - if len(info.Manufact) > maxStrSize { + if len(info.Manufact) > MaxStrSize { e += "Manufact:" } - if len(info.Product) > maxStrSize { + if len(info.Product) > MaxStrSize { e += "Product:" } - if len(info.Serial) > maxStrSize { + if len(info.Serial) > MaxStrSize { e += "Serial:" } if len(e) != 0 { err = errors.New(e + " string/s too long") return } - pos := strOffsetStart + pos := StrOffsetStart for _, v := range []string{info.Manufact, info.Product, info.Serial} { data[pos] = uint8((len(v) * 2) + 2) data[pos+1] = 0x03