~ubuntu-branches/ubuntu/trusty/libticalcs/trusty-proposed

« back to all changes in this revision

Viewing changes to src/nsp_rpkt.c

  • Committer: Package Import Robot
  • Author(s): Andreas B. Mundt
  • Date: 2013-08-27 19:58:21 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20130827195821-biurlicyqb65gj3g
Tags: 1.1.8+dfsg2-2
* Provide original upstream source, but patch away pre-compiled
  binaries to be policy-compliant.
* Remove unnecessary dependency on 'autopoint', use autoreconf.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Hey EMACS -*- linux-c -*- */
2
 
/* $Id: packets.c 1404 2005-07-20 20:39:39Z roms $ */
3
 
 
4
 
/*  libticalcs - Ti Calculator library, a part of the TiLP project
5
 
 *  Copyright (C) 2007 Romain Li�vin
6
 
 *
7
 
 *  This program is free software; you can redistribute it and/or modify
8
 
 *  it under the terms of the GNU General Public License as published by
9
 
 *  the Free Software Foundation; either version 2 of the License, or
10
 
 *  (at your option) any later version.
11
 
 *
12
 
 *  This program is distributed in the hope that it will be useful,
13
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 *  GNU General Public License for more details.
16
 
 *
17
 
 *  You should have received a copy of the GNU General Public License
18
 
 *  along with this program; if not, write to the Free Software
19
 
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
 
 */
21
 
 
22
 
/*
23
 
        This unit manages raw packets from/to NSpire thru DirectLink cable.
24
 
        Documentation & credits can be found at <http://hackspire.unsads.com/USB_Protocol>.
25
 
*/
26
 
 
27
 
#include <stdio.h>
28
 
#include <string.h>
29
 
 
30
 
#include "ticalcs.h"
31
 
#include "nsp_rpkt.h"
32
 
#include "logging.h"
33
 
#include "error.h"
34
 
#include "macros.h"
35
 
 
36
 
#define VPKT_DBG        2       // 1 = verbose, 2 = more verbose
37
 
 
38
 
// CRC implementation from O. Armand (ExtendeD)
39
 
static uint16_t compute_crc(uint8_t *data, uint32_t size)
40
 
{
41
 
        uint16_t acc = 0;
42
 
        int i;
43
 
 
44
 
        if(size == 0)
45
 
                return 0;
46
 
 
47
 
        for(i = 0; i < (int)size; i++)
48
 
        {
49
 
                uint16_t first, second, third;
50
 
 
51
 
                first = (data[i] << 8) | (acc >> 8); 
52
 
                acc &= 0xff;
53
 
                second = (((acc & 0x0f) << 4) ^ acc) << 8;
54
 
                third = second >> 5;
55
 
                acc = third >> 7;
56
 
                acc = (acc ^ first ^ second ^ third);
57
 
        }
58
 
 
59
 
        return acc;
60
 
}
61
 
 
62
 
static int hexdump(uint8_t *data, uint32_t size)
63
 
{
64
 
#if (VPKT_DBG == 2)
65
 
        char *str = (char *)g_malloc(3*size + 8 + 10);
66
 
        int i, j, k;
67
 
        int step = 12;
68
 
 
69
 
        for(k = 0; k < 4; k++)
70
 
                        str[k] = ' ';
71
 
 
72
 
        for (i = j = 0; i < (int)size; i++, j++)
73
 
        {
74
 
                if(i && !(i % step))
75
 
                {
76
 
                        ticalcs_info(str);
77
 
                        j = 0;
78
 
                }
79
 
 
80
 
                sprintf(&str[3*j+4], "%02X ", 0xff & data[i]);
81
 
        }
82
 
        ticalcs_info(str);
83
 
        
84
 
        g_free(str);
85
 
#endif
86
 
  return 0;
87
 
}
88
 
 
89
 
uint8_t         nsp_seq_ti;
90
 
uint8_t         nsp_seq_pc;
91
 
uint8_t         nsp_seq;
92
 
 
93
 
int nsp_send(CalcHandle* handle, RawPacket* pkt)
94
 
{
95
 
        uint8_t buf[sizeof(RawPacket)] = { 0 };
96
 
        uint32_t size = pkt->data_size + HEADER_SIZE;
97
 
        
98
 
        pkt->data_sum = compute_crc(pkt->data, pkt->data_size);
99
 
 
100
 
        if(pkt->src_port == 0x00fe || pkt->src_port == 0x00ff || pkt->src_port == 0x00d3)
101
 
        {
102
 
                pkt->ack = 0x0a;
103
 
                pkt->seq = nsp_seq;
104
 
        }
105
 
        else
106
 
        {
107
 
                if(!nsp_seq_pc) nsp_seq_pc++;
108
 
                pkt->seq = nsp_seq_pc;
109
 
        }
110
 
 
111
 
        ticalcs_info("   %04x:%04x->%04x:%04x AK=%02x SQ=%02x HC=%02x DC=%04x (%i bytes)", 
112
 
                        pkt->src_addr, pkt->src_port, pkt->dst_addr, pkt->dst_port, 
113
 
                        pkt->ack, pkt->seq, pkt->hdr_sum, pkt->data_sum, pkt->data_size);
114
 
        if(pkt->data_size)
115
 
                hexdump(pkt->data, pkt->data_size);
116
 
        
117
 
        buf[0] = 0x54;
118
 
        buf[1] = 0xFD;
119
 
        buf[2] = MSB(pkt->src_addr);
120
 
        buf[3] = LSB(pkt->src_addr);
121
 
        buf[4] = MSB(pkt->src_port);
122
 
        buf[5] = LSB(pkt->src_port);
123
 
        buf[6] = MSB(pkt->dst_addr);
124
 
        buf[7] = LSB(pkt->dst_addr);
125
 
        buf[8] = MSB(pkt->dst_port);
126
 
        buf[9] = LSB(pkt->dst_port);
127
 
        buf[10] = MSB(pkt->data_sum);
128
 
        buf[11] = LSB(pkt->data_sum);
129
 
        buf[12] = pkt->data_size;
130
 
        buf[13] = pkt->ack;
131
 
        buf[14] = pkt->seq;
132
 
        buf[15] = pkt->hdr_sum = tifiles_checksum(buf, HEADER_SIZE-1) & 0xff;
133
 
 
134
 
        memcpy(buf + HEADER_SIZE, pkt->data, pkt->data_size);
135
 
 
136
 
        ticables_progress_reset(handle->cable);
137
 
        TRYF(ticables_cable_send(handle->cable, buf, size));
138
 
        if(size >= 128)
139
 
                ticables_progress_get(handle->cable, NULL, NULL, &handle->updat->rate);
140
 
 
141
 
        if (handle->updat->cancel)
142
 
                return ERR_ABORT;
143
 
 
144
 
        return 0;
145
 
}
146
 
 
147
 
int nsp_recv(CalcHandle* handle, RawPacket* pkt)
148
 
{
149
 
        uint8_t buf[HEADER_SIZE];
150
 
 
151
 
        ticables_progress_reset(handle->cable);
152
 
        TRYF(ticables_cable_recv(handle->cable, buf, HEADER_SIZE));
153
 
 
154
 
        pkt->unused             = (buf[0] << 8) | buf[1];
155
 
        pkt->src_addr   = (buf[2] << 8) | buf[3];
156
 
        pkt->src_port   = (buf[4] << 8) | buf[5];
157
 
        pkt->dst_addr   = (buf[6] << 8) | buf[7];
158
 
        pkt->dst_port   = (buf[8] << 8) | buf[9];
159
 
        pkt->data_sum   = (buf[10] << 8) | buf[11];
160
 
        pkt->data_size  = buf[12];
161
 
        pkt->ack                = buf[13];
162
 
        pkt->seq                = buf[14];
163
 
        pkt->hdr_sum    = buf[15];
164
 
 
165
 
        if(pkt->src_port == 0x00fe || pkt->src_port == 0x00ff || pkt->src_port == 0x00d3)
166
 
                nsp_seq_pc++;
167
 
        else
168
 
                nsp_seq = pkt->seq;
169
 
 
170
 
        // Next, follows data
171
 
        if(pkt->data_size)
172
 
        {
173
 
                TRYF(ticables_cable_recv(handle->cable, pkt->data, pkt->data_size));
174
 
                if(pkt->data_size >= 128)
175
 
                        ticables_progress_get(handle->cable, NULL, NULL, &handle->updat->rate);
176
 
        }
177
 
                        
178
 
        if (handle->updat->cancel)
179
 
                return ERR_ABORT;
180
 
 
181
 
        ticalcs_info("   %04x:%04x->%04x:%04x AK=%02x SQ=%02x HC=%02x DC=%04x (%i bytes)", 
182
 
                        pkt->src_addr, pkt->src_port, pkt->dst_addr, pkt->dst_port, 
183
 
                        pkt->ack, pkt->seq, pkt->hdr_sum, pkt->data_sum, pkt->data_size);
184
 
        if(pkt->data_size)
185
 
                hexdump(pkt->data, pkt->data_size);
186
 
 
187
 
        return 0;
188
 
}
189
 
 
 
1
/* Hey EMACS -*- linux-c -*- */
 
2
/* $Id: packets.c 1404 2005-07-20 20:39:39Z roms $ */
 
3
 
 
4
/*  libticalcs - Ti Calculator library, a part of the TiLP project
 
5
 *  Copyright (C) 2007 Romain Li�vin
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License as published by
 
9
 *  the Free Software Foundation; either version 2 of the License, or
 
10
 *  (at your option) any later version.
 
11
 *
 
12
 *  This program is distributed in the hope that it will be useful,
 
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *  GNU General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU General Public License
 
18
 *  along with this program; if not, write to the Free Software Foundation,
 
19
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
20
 */
 
21
 
 
22
/*
 
23
        This unit manages raw packets from/to NSpire thru DirectLink cable.
 
24
        Documentation & credits can be found at <http://hackspire.unsads.com/USB_Protocol>.
 
25
*/
 
26
 
 
27
#include <stdio.h>
 
28
#include <string.h>
 
29
 
 
30
#include "ticalcs.h"
 
31
#include "logging.h"
 
32
#include "error.h"
 
33
#include "macros.h"
 
34
 
 
35
#define VPKT_DBG        1       // 1 = verbose, 2 = more verbose
 
36
 
 
37
// CRC implementation from O. Armand (ExtendeD)
 
38
static uint16_t compute_crc(uint8_t *data, uint32_t size)
 
39
{
 
40
        uint16_t acc = 0;
 
41
        uint32_t i;
 
42
 
 
43
        if(size == 0)
 
44
                return 0;
 
45
 
 
46
        for(i = 0; i < size; i++)
 
47
        {
 
48
                uint16_t first, second, third;
 
49
 
 
50
                first = (data[i] << 8) | (acc >> 8); 
 
51
                acc &= 0xff;
 
52
                second = (((acc & 0x0f) << 4) ^ acc) << 8;
 
53
                third = second >> 5;
 
54
                acc = third >> 7;
 
55
                acc = (acc ^ first ^ second ^ third);
 
56
        }
 
57
 
 
58
        return acc;
 
59
}
 
60
 
 
61
static int hexdump(uint8_t *data, uint32_t size)
 
62
{
 
63
#if (VPKT_DBG == 1)
 
64
        char str[64];
 
65
        uint32_t i;
 
66
 
 
67
        str[0] = 0;
 
68
        if (size <= 12)
 
69
        {
 
70
                str[0] = ' '; str[1] = ' '; str[2] = ' '; str[3] = ' ';
 
71
 
 
72
                for (i = 0; i < size; i++)
 
73
                {
 
74
                        sprintf(&str[3*i+4], "%02X ", data[i]);
 
75
                }
 
76
        }
 
77
        else
 
78
        {
 
79
                sprintf(str, "    %02X %02X %02X %02X %02X ..... %02X %02X %02X %02X %02X",
 
80
                             data[0], data[1], data[2], data[3], data[4],
 
81
                             data[size-5], data[size-4], data[size-3], data[size-2], data[size-1]);
 
82
        }
 
83
        ticalcs_info(str);
 
84
#endif
 
85
#if (VPKT_DBG == 2)
 
86
        char *str = (char *)g_malloc(3*size + 8 + 10);
 
87
        uint32_t i, j, k;
 
88
        int step = 12;
 
89
 
 
90
        for(k = 0; k < 4; k++)
 
91
        {
 
92
                str[k] = ' ';
 
93
        }
 
94
 
 
95
        for (i = j = 0; i < size; i++, j++)
 
96
        {
 
97
                if(i && !(i % step))
 
98
                {
 
99
                        ticalcs_info(str);
 
100
                        j = 0;
 
101
                }
 
102
 
 
103
                sprintf(&str[3*j+4], "%02X ", data[i]);
 
104
        }
 
105
        ticalcs_info(str);
 
106
 
 
107
        g_free(str);
 
108
#endif
 
109
  return 0;
 
110
}
 
111
 
 
112
uint8_t         nsp_seq_ti;
 
113
uint8_t         nsp_seq_pc;
 
114
uint8_t         nsp_seq;
 
115
 
 
116
TIEXPORT3 int TICALL nsp_send(CalcHandle* handle, NSPRawPacket* pkt)
 
117
{
 
118
        uint8_t buf[sizeof(NSPRawPacket)] = { 0 };
 
119
        uint32_t size;
 
120
 
 
121
        if (handle == NULL)
 
122
        {
 
123
                ticalcs_critical("%s: handle is NULL", __FUNCTION__);
 
124
                return ERR_INVALID_HANDLE;
 
125
        }
 
126
        if (pkt == NULL)
 
127
        {
 
128
                ticalcs_critical("%s: pkt is NULL", __FUNCTION__);
 
129
                return ERR_INVALID_PACKET;
 
130
        }
 
131
 
 
132
        size = pkt->data_size + NSP_HEADER_SIZE;
 
133
        pkt->data_sum = compute_crc(pkt->data, pkt->data_size);
 
134
 
 
135
        if(pkt->src_port == 0x00fe || pkt->src_port == 0x00ff || pkt->src_port == 0x00d3)
 
136
        {
 
137
                pkt->ack = 0x0a;
 
138
                pkt->seq = nsp_seq;
 
139
        }
 
140
        else
 
141
        {
 
142
                if(!nsp_seq_pc) nsp_seq_pc++;
 
143
                pkt->seq = nsp_seq_pc;
 
144
        }
 
145
 
 
146
        ticalcs_info("   %04x:%04x->%04x:%04x AK=%02x SQ=%02x HC=%02x DC=%04x (%i bytes)", 
 
147
                        pkt->src_addr, pkt->src_port, pkt->dst_addr, pkt->dst_port, 
 
148
                        pkt->ack, pkt->seq, pkt->hdr_sum, pkt->data_sum, pkt->data_size);
 
149
        if(pkt->data_size)
 
150
                hexdump(pkt->data, pkt->data_size);
 
151
        
 
152
        buf[0] = 0x54;
 
153
        buf[1] = 0xFD;
 
154
        buf[2] = MSB(pkt->src_addr);
 
155
        buf[3] = LSB(pkt->src_addr);
 
156
        buf[4] = MSB(pkt->src_port);
 
157
        buf[5] = LSB(pkt->src_port);
 
158
        buf[6] = MSB(pkt->dst_addr);
 
159
        buf[7] = LSB(pkt->dst_addr);
 
160
        buf[8] = MSB(pkt->dst_port);
 
161
        buf[9] = LSB(pkt->dst_port);
 
162
        buf[10] = MSB(pkt->data_sum);
 
163
        buf[11] = LSB(pkt->data_sum);
 
164
        buf[12] = pkt->data_size;
 
165
        buf[13] = pkt->ack;
 
166
        buf[14] = pkt->seq;
 
167
        buf[15] = pkt->hdr_sum = tifiles_checksum(buf, NSP_HEADER_SIZE-1) & 0xff;
 
168
 
 
169
        memcpy(buf + NSP_HEADER_SIZE, pkt->data, pkt->data_size);
 
170
 
 
171
        ticables_progress_reset(handle->cable);
 
172
        TRYF(ticables_cable_send(handle->cable, buf, size));
 
173
        if(size >= 128)
 
174
                ticables_progress_get(handle->cable, NULL, NULL, &handle->updat->rate);
 
175
 
 
176
        if (handle->updat->cancel)
 
177
                return ERR_ABORT;
 
178
 
 
179
        return 0;
 
180
}
 
181
 
 
182
TIEXPORT3 int TICALL nsp_recv(CalcHandle* handle, NSPRawPacket* pkt)
 
183
{
 
184
        uint8_t buf[NSP_HEADER_SIZE];
 
185
 
 
186
        if (handle == NULL)
 
187
        {
 
188
                ticalcs_critical("%s: handle is NULL", __FUNCTION__);
 
189
                return ERR_INVALID_HANDLE;
 
190
        }
 
191
        if (pkt == NULL)
 
192
        {
 
193
                ticalcs_critical("%s: pkt is NULL", __FUNCTION__);
 
194
                return ERR_INVALID_PACKET;
 
195
        }
 
196
 
 
197
        ticables_progress_reset(handle->cable);
 
198
        TRYF(ticables_cable_recv(handle->cable, buf, NSP_HEADER_SIZE));
 
199
 
 
200
        pkt->unused             = (buf[0] << 8) | buf[1];
 
201
        pkt->src_addr   = (buf[2] << 8) | buf[3];
 
202
        pkt->src_port   = (buf[4] << 8) | buf[5];
 
203
        pkt->dst_addr   = (buf[6] << 8) | buf[7];
 
204
        pkt->dst_port   = (buf[8] << 8) | buf[9];
 
205
        pkt->data_sum   = (buf[10] << 8) | buf[11];
 
206
        pkt->data_size  = buf[12];
 
207
        pkt->ack                = buf[13];
 
208
        pkt->seq                = buf[14];
 
209
        pkt->hdr_sum    = buf[15];
 
210
 
 
211
        if(pkt->src_port == 0x00fe || pkt->src_port == 0x00ff || pkt->src_port == 0x00d3)
 
212
                nsp_seq_pc++;
 
213
        else
 
214
                nsp_seq = pkt->seq;
 
215
 
 
216
        // Next, follows data
 
217
        if(pkt->data_size)
 
218
        {
 
219
                TRYF(ticables_cable_recv(handle->cable, pkt->data, pkt->data_size));
 
220
                if(pkt->data_size >= 128)
 
221
                        ticables_progress_get(handle->cable, NULL, NULL, &handle->updat->rate);
 
222
        }
 
223
 
 
224
        if (handle->updat->cancel)
 
225
                return ERR_ABORT;
 
226
 
 
227
        ticalcs_info("   %04x:%04x->%04x:%04x AK=%02x SQ=%02x HC=%02x DC=%04x (%i bytes)", 
 
228
                        pkt->src_addr, pkt->src_port, pkt->dst_addr, pkt->dst_port, 
 
229
                        pkt->ack, pkt->seq, pkt->hdr_sum, pkt->data_sum, pkt->data_size);
 
230
        if(pkt->data_size)
 
231
                hexdump(pkt->data, pkt->data_size);
 
232
 
 
233
        return 0;
 
234
}
 
235