File: trackinformation.c

package info (click to toggle)
linux-minidisc 0.9.13-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,096 kB
  • ctags: 1,530
  • sloc: ansic: 6,345; cpp: 2,569; python: 2,451; perl: 866; sh: 22; makefile: 8
file content (127 lines) | stat: -rw-r--r-- 4,071 bytes parent folder | download | duplicates (2)
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
/*
 * trackinformation.c
 *
 * This file is part of libnetmd, a library for accessing Sony NetMD devices.
 *
 * Copyright (C) 2011 Alexander Sulfrian
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <string.h>
#include <stdio.h>

#include "trackinformation.h"
#include "utils.h"
#include "log.h"

void netmd_get_track_information(netmd_dev_handle *dev, uint16_t track,
                                uint16_t p1, uint16_t p2,
                                unsigned char *data, size_t data_length)
{
    unsigned char cmd[] = { 0x00, 0x18, 0x06, 0x02, 0x20, 0x10, 0x01,  0x00, 0x00,  0x00, 0x00,  0x00, 0x00,
                            0xff, 0x00, 0x00, 0x00, 0x00, 0x00 };
    unsigned char rsp[255];
    unsigned char *buf;
    int length;
    uint16_t real_data_length;
    size_t size;

    buf = cmd + 7;
    netmd_copy_word_to_buffer(&buf, track, 0);
    netmd_copy_word_to_buffer(&buf, p1, 0);
    netmd_copy_word_to_buffer(&buf, p2, 0);

    length = netmd_exch_message(dev, cmd, sizeof(cmd), rsp);
    if (length > 0) {
        uint32_t tmp = (unsigned int)data[19] << 8;
        real_data_length = (tmp + data[20]) & 0xffffU;
        if (real_data_length > data_length) {
            size = data_length;
        }
        else {
            size = real_data_length;
        }

        memcpy(data, rsp + 21, size);
    }
}

int netmd_request_track_bitrate(netmd_dev_handle*dev, const uint16_t track,
                                unsigned char* encoding, unsigned char *channel)
{
    unsigned char info[8] = { 0 };

    netmd_get_track_information(dev, track, 0x3080, 0x0700, info, sizeof(info));
    memcpy(encoding, info + 6, 1);
    memcpy(channel, info + 7, 1);
    return 2;
}

int netmd_request_track_flags(netmd_dev_handle*dev, const uint16_t track, unsigned char* data)
{
    int ret = 0;
    unsigned char request[] = {0x00, 0x18, 0x06, 0x01, 0x20, 0x10,
                               0x01, 0x00, 0x00, 0xff, 0x00, 0x00,
                               0x01, 0x00, 0x08};

    unsigned char *buf;
    unsigned char reply[255];

    buf = request + 7;
    netmd_copy_word_to_buffer(&buf, track, 0);
    ret = netmd_exch_message(dev, request, sizeof(request), reply);
    *data = reply[ret - 1];
    return ret;
}

int netmd_request_title(netmd_dev_handle* dev, const uint16_t track, char* buffer, const size_t size)
{
    int ret = -1;
    size_t title_size = 0;
    unsigned char title_request[] = {0x00, 0x18, 0x06, 0x02, 0x20, 0x18,
                                     0x02, 0x00, 0x00, 0x30, 0x00, 0xa,
                                     0x00, 0xff, 0x00, 0x00, 0x00, 0x00,
                                     0x00};
    unsigned char title[255];
    unsigned char *buf;

    buf = title_request + 7;
    netmd_copy_word_to_buffer(&buf, track, 0);
    ret = netmd_exch_message(dev, title_request, 0x13, title);
    if(ret < 0)
    {
        fprintf(stderr, "bad ret code, returning early\n");
        return -1;
    }

    title_size = (size_t)ret;

    if(title_size == 0 || title_size == 0x13)
        return -1; /* bail early somethings wrong or no track */

    if(title_size > size)
    {
        printf("netmd_request_title: title too large for buffer\n");
        return -1;
    }

    memset(buffer, 0, size);
    memcpy(buffer, (title + 25), title_size - 25);
    buffer[size] = 0;

    return (int)title_size - 25;
}