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

« back to all changes in this revision

Viewing changes to src/dbus_pkt.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
1
/* Hey EMACS -*- linux-c -*- */
2
 
/* $Id: dbus_pkt.c 4166 2008-04-09 15:57:23Z roms $ */
 
2
/* $Id$ */
3
3
 
4
4
/*  libticalcs - Ti Calculator library, a part of the TiLP project
5
5
 *  Copyright (C) 1999-2005  Romain Li�vin
15
15
 *  GNU General Public License for more details.
16
16
 *
17
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.
 
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
20
 */
21
21
 
22
22
/*
26
26
#include <string.h>
27
27
 
28
28
#include "ticalcs.h"
 
29
#include "internal.h"
29
30
#include "dbus_pkt.h"
30
31
#include "logging.h"
31
32
#include "error.h"
32
33
#include "macros.h"
33
34
 
34
35
// We split packets into chucks to get control regularly and update statistics.
35
 
static unsigned int BLK_SIZE;   // refresh pbars every 5%
36
 
static unsigned int MIN_SIZE;   // don't refresh at all if packet is < 512 bytes
 
36
static unsigned int BLK_SIZE; // refresh pbars every 5%
 
37
static unsigned int MIN_SIZE; // don't refresh at all if packet is < 512 bytes
37
38
 
38
39
/*
39
40
    Send a packet from PC (host) to TI (target):
40
41
    - target [in] : a machine ID uint8_t
41
 
    - cmd [in]    : a command ID uint8_t
 
42
    - cmd [in]    : a command ID uint8_t
42
43
    - length [in] : length of buffer
43
 
    - data [in]   : data to send (or 0x00 if NULL)
44
 
    - int [out]   : an error code
 
44
    - data [in]   : data to send (or 0x00 if NULL)
 
45
    - int [out]   : an error code
45
46
*/
46
 
int dbus_send(CalcHandle* handle, uint8_t target, uint8_t cmd, uint16_t len, uint8_t* data)
 
47
TIEXPORT3 int TICALL dbus_send(CalcHandle* handle, uint8_t target, uint8_t cmd, uint16_t len, uint8_t* data)
47
48
{
48
49
        int i;
49
50
        uint16_t sum;
50
 
        uint32_t length = (len == 0x0000) ? 65536 : len;        //  wrap around
51
 
        uint8_t *buf = (uint8_t *)handle->priv2;                        //[65536+6];
 
51
        uint32_t length = (len == 0x0000) ? 65536 : len;   // wrap around
 
52
        uint8_t *buf;
52
53
        int r, q;
53
54
        static int ref = 0;
54
55
 
 
56
        if (handle == NULL)
 
57
        {
 
58
                ticalcs_critical("%s: handle is NULL", __FUNCTION__);
 
59
                return ERR_INVALID_HANDLE;
 
60
        }
 
61
        buf = (uint8_t *)handle->priv2;                    //[65536+6];
 
62
        if (buf == NULL)
 
63
        {
 
64
                ticalcs_critical("%s: handle->priv2 is NULL", __FUNCTION__);
 
65
                return ERR_INVALID_HANDLE;
 
66
        }
 
67
 
55
68
        ticables_progress_reset(handle->cable);
56
69
 
57
70
        if(data == NULL)
62
75
                buf[2] = 0x00;
63
76
                buf[3] = 0x00;
64
77
 
65
 
                TRYF(ticables_cable_send(handle->cable, buf, 4));
 
78
                // TI80 does not use length
 
79
                if(target == PC_TI80)
 
80
                {
 
81
                        TRYF(ticables_cable_send(handle->cable, buf, 2));
 
82
                }
 
83
                else
 
84
                {
 
85
                        TRYF(ticables_cable_send(handle->cable, buf, 4));
 
86
                }
66
87
        }
67
88
        else 
68
89
        {
134
155
  {
135
156
  case CALC_TI73:
136
157
    return TI73_PC;
137
 
    break;
 
158
  case CALC_TI80:
 
159
    return TI80_PC;
138
160
  case CALC_TI82:
139
161
    return TI82_PC;
140
 
    break;
141
162
  case CALC_TI83:
142
163
    return TI83_PC;
143
 
    break;
144
164
  case CALC_TI83P:
145
165
  case CALC_TI84P:
146
166
    return TI83p_PC;
147
 
    break;
148
167
  case CALC_TI85:
149
168
    return TI85_PC;
150
 
    break;
151
169
  case CALC_TI86:
152
170
    return TI86_PC;
153
 
    break;
154
171
  case CALC_TI89:
155
172
  case CALC_TI89T:
156
173
    return TI89_PC;
157
 
    break;
158
174
  case CALC_TI92:
159
175
    return TI92_PC;
160
 
    break;
161
176
  case CALC_TI92P:
162
177
    return TI92p_PC;
163
 
    break;
164
178
  case CALC_V200:
165
179
    return V200_PC;
166
 
    break;
167
180
  default:
168
 
      return 0x00;
169
 
      break;
 
181
    return 0x00;
170
182
  }
171
183
  return 0x00;
172
184
}
180
192
        int r, q;
181
193
        static int ref = 0;
182
194
 
183
 
        // Any packet has always at least 4 bytes (MID, CID, LEN)
184
 
        TRYF(ticables_cable_recv(handle->cable, buf, 4));
 
195
        if (handle == NULL)
 
196
        {
 
197
                ticalcs_critical("%s: handle is NULL", __FUNCTION__);
 
198
                return ERR_INVALID_HANDLE;
 
199
        }
 
200
        if (host == NULL || cmd == NULL || length == NULL)
 
201
        {
 
202
                ticalcs_critical("%s: an argument is NULL", __FUNCTION__);
 
203
                return ERR_INVALID_PACKET;
 
204
        }
 
205
 
 
206
        // Any packet has always at least 2 bytes (MID, CID)
 
207
        TRYF(ticables_cable_recv(handle->cable, buf, 2));
185
208
 
186
209
        *host = buf[0];
187
210
        *cmd = buf[1];
188
 
        *length = buf[2] | (buf[3] << 8);
 
211
 
 
212
        // Any non-TI80 packet has a length; TI80 data packets also have a length
 
213
        if(*host != TI80_PC || *cmd == CMD_XDP)
 
214
        {
 
215
                TRYF(ticables_cable_recv(handle->cable, buf, 2));
 
216
 
 
217
                *length = buf[0] | (buf[1] << 8);
 
218
        }
 
219
        else *length = 0;
189
220
 
190
221
        //removed for probing (pb here !)
191
222
        //if(host_check && (*host != host_ids(handle))) 
192
223
        //      return ERR_INVALID_HOST;
193
224
 
194
 
        if(*cmd == CMD_ERR ||*cmd == CMD_ERR2) 
 
225
        if(*cmd == CMD_ERR || *cmd == CMD_ERR2)
195
226
                return ERR_CHECKSUM;
196
227
 
197
228
        switch (*cmd) 
202
233
        case CMD_SID:
203
234
        case CMD_REQ:
204
235
        case CMD_IND:
205
 
        case CMD_RTS:           
 
236
        case CMD_RTS:
 
237
                if (data == NULL)
 
238
                {
 
239
                        ticalcs_critical("%s: data is NULL", __FUNCTION__);
 
240
                        return ERR_INVALID_CMD;
 
241
                }
 
242
 
206
243
                // compute chunks*
207
244
                MIN_SIZE = (handle->cable->model == CABLE_GRY) ? 512 : 2048;
208
245
                BLK_SIZE = *length / 20;
280
317
  - data [out]   : received data (depending on command)
281
318
  - int [out]    : an error code
282
319
*/
283
 
int dbus_recv(CalcHandle* handle, uint8_t* host, uint8_t* cmd, uint16_t* length, uint8_t* data)
 
320
TIEXPORT3 int TICALL dbus_recv(CalcHandle* handle, uint8_t* host, uint8_t* cmd, uint16_t* length, uint8_t* data)
284
321
{
285
322
        return dbus_recv_(handle, host, cmd, length, data, !0);
286
323
}
287
324
 
 
325
// used only by probe.c
288
326
int dbus_recv_2(CalcHandle* handle, uint8_t* host, uint8_t* cmd, uint16_t* length, uint8_t* data)
289
327
{
290
328
        return dbus_recv_(handle, host, cmd, length, data, 0);
291
329
}
292
330
 
293
 
/* Fill-up a 8-chars buffer with NUL chars */
294
 
void pad_buffer(uint8_t *varname, uint8_t value)
 
331
//! Fill up to 8 chars the \a varname buffer with chars of value \a value.
 
332
void pad_buffer_to_8_chars(uint8_t *varname, uint8_t value)
295
333
{
296
334
        unsigned int i;
297
 
        unsigned int len = strlen((char*)varname);
298
 
 
299
 
        for (i = len; i < 8; i++)
300
 
                varname[i] = value;
 
335
        unsigned int len;
 
336
 
 
337
        if (varname != NULL)
 
338
        {
 
339
                len = strlen((char*)varname);
 
340
 
 
341
                for (i = len; i < 8; i++)
 
342
                        varname[i] = value;
 
343
        }
301
344
}