Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
770 user890104 1
//
2
//
898 user890104 3
//    Copyright 2013 user890104
770 user890104 4
//
5
//
6
//    This file is part of emCORE.
7
//
8
//    emCORE is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    emCORE is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
#include "global.h"
24
 
25
#include "util.h"
26
 
898 user890104 27
const char *libusb_error_messages[13] = {
770 user890104 28
    "No error",                /* LIBUSB_SUCCESS = 0               */
29
    "Input/output error",      /* LIBUSB_ERROR_IO = -1             */
30
    "Invalid parameter",       /* LIBUSB_ERROR_INVALID_PARAM = -2  */
31
    "Access denied",           /* LIBUSB_ERROR_ACCESS = -3         */
32
    "No such device",          /* LIBUSB_ERROR_NO_DEVICE = -4      */
33
    "Entity not found",        /* LIBUSB_ERROR_NOT_FOUND = -5      */
34
    "Resource busy",           /* LIBUSB_ERROR_BUSY = -6           */
35
    "Operation timed out",     /* LIBUSB_ERROR_TIMEOUT = -7        */
36
    "Overflow",                /* LIBUSB_ERROR_OVERFLOW = -8       */
37
    "Pipe error",              /* LIBUSB_ERROR_PIPE = -9           */
38
    "System call interrupted", /* LIBUSB_ERROR_INTERRUPTED = -10   */
39
    "Insufficient memory",     /* LIBUSB_ERROR_NO_MEM = -11        */
40
    "Operation not supported", /* LIBUSB_ERROR_NOT_SUPPORTED = -12 */
41
};
42
 
898 user890104 43
const char *emcore_error_messages[10] = {
770 user890104 44
    "No error",               /* EMCORE_SUCCESS = 0               */
45
    "Invalid command",        /* EMCORE_ERROR_INVALID = 1         */
46
    "Command not supported",  /* EMCORE_ERROR_NOT_SUPPORTED = 2   */
47
    "Device is busy",         /* EMCORE_ERROR_BUSY = 3            */
48
    "No such device",         /* EMCORE_ERROR_NO_DEVICE = 4       */
49
    "Incomplete transfer",    /* EMCORE_ERROR_INCOMPLETE = 5      */
50
    "Overflow",               /* EMCORE_ERROR_OVERFLOW = 6        */
51
    "Not implemented",        /* EMCORE_ERROR_NOT_IMPLEMENTED = 7 */
52
    "No more entries in dir", /* EMCORE_ERROR_NO_MORE_ENTRIES = 8 */
53
    "I/O error",              /* EMCORE_ERROR_IO = 9              */
54
};
55
 
898 user890104 56
void dump_packet(const void *data, size_t length) {
770 user890104 57
    static size_t i;
58
 
898 user890104 59
    for (i = 0; i < length; ++i) {
60
        fprintf(stderr, "%02x ", *((uint8_t *)(data + i)));
770 user890104 61
 
898 user890104 62
        if (i % 4 == 3) {
770 user890104 63
            fprintf(stderr, " ");
64
        }
65
 
898 user890104 66
        if (i % 16 == 15 && i + 1 < length) {
770 user890104 67
            fprintf(stderr, "\n");
68
        }
69
    }
70
 
71
    fprintf(stderr, "\n");
72
}
73
 
898 user890104 74
time_t fat_time_to_unix_ts(uint16_t time, uint16_t date) {
770 user890104 75
    struct tm result;
76
    /*
77
        Example time: 0x9365
78
        in memory:
79
        M M M S S S S S   H H H H H M M M
80
        as a number:
81
        H H H H H M M M   M M M S S S S S
82
 
83
        Example date: 0x3d37
84
        in memory:
85
        M M M D D D D D   Y Y Y Y Y Y Y M
86
        as a number:
87
        Y Y Y Y Y Y Y M   M M M D D D D D
88
    */
89
 
898 user890104 90
    result.tm_sec = ((time & 0x1f) << 1);
91
    result.tm_min = (time >> 5) & 0x3f;
92
    result.tm_hour = (time >> 11) & 0x1f;
93
    result.tm_mday = date & 0x1f;
94
    result.tm_mon = ((date >> 5) & 0xf) - 1;
95
    result.tm_year = ((date >> 9) & 0x7f) + 80;
770 user890104 96
 
97
    return mktime(&result);
98
}
99
 
898 user890104 100
int32_t unix_ts_to_fat_time(time_t datetime) {
101
    struct tm *tm_ptr;
770 user890104 102
 
103
    tm_ptr = localtime(&datetime);
104
 
105
    return (tm_ptr->tm_sec >> 1) |
106
           (tm_ptr->tm_min << 5) |
107
           (tm_ptr->tm_hour << 0xb) |
108
           (tm_ptr->tm_mday << 0x10) |
109
           ((tm_ptr->tm_mon + 1) << 0x15) |
110
           ((tm_ptr->tm_year - 80) << 0x19);
111
}
112
 
898 user890104 113
void print_error(int32_t code) {
114
    if (code > 0) {
770 user890104 115
        fprintf(stderr, "emcore error: %s\n", emcore_error_messages[code]);
116
    }
898 user890104 117
    else {
770 user890104 118
        fprintf(stderr, "libusb error: %s\n", (
898 user890104 119
            code == LIBUSB_ERROR_OTHER ?
770 user890104 120
                "Other error" :
121
                libusb_error_messages[-code]
122
        ));
123
    }
124
}