File: interface_util.c

package info (click to toggle)
player 3.0.2%2Bdfsg-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 16,968 kB
  • sloc: cpp: 118,349; ansic: 34,116; python: 1,710; ruby: 269; tcl: 265; java: 189; makefile: 113; sh: 30; php: 3
file content (310 lines) | stat: -rw-r--r-- 7,889 bytes parent folder | download
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 *  Player - One Hell of a Robot Server
 *  Copyright (C) 2000
 *     Brian Gerkey, Kasper Stoy, Richard Vaughan, & Andrew Howard
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
/********************************************************************
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 ********************************************************************/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#if defined WIN32
  #define snprintf _snprintf
  #define strdup _strdup
#endif

#include <libplayerinterface/player.h>  // for interface constants
#include <libplayerinterface/interface_util.h> // for player_interface_t type

#include "interface_table.h"

static player_interface_t* itable;
static unsigned int itable_len;

/*
 * A string table of the message types, used to print type strings in error
 * messages instead of type codes.
 * Must be kept in numerical order with respect to the numeric values of the
 * PLAYER_MSGTYPE_ #defines.
 */
static char* msgTypeStrTable[7]=
{
  "",           // nothing
  "data",       // PLAYER_MSGTYPE_DATA
  "command",    // PLAYER_MSGTYPE_CMD
  "request",    // PLAYER_MSGTYPE_REQ
  "resp_ack",   // PLAYER_MSGTYPE_RESP_ACK
  "sync",       // PLAYER_MSGTYPE_SYNCH
  "resp_nack",  // PLAYER_MSGTYPE_RESP_NACK

};

/*
 * Initialises the interface names/codes table.
 */
int itable_init (void)
{
  unsigned int ii;

  // An indirect way of avoiding duplicate initialization.  It would 
  // probably be better to delete the old table, but there may be some
  // pointers hanging around that refer to the old table.
  if(itable)
    return(0);

  for (itable_len = 0; interfaces[itable_len].interf; itable_len++);

  if ((itable = (player_interface_t*) calloc (itable_len, sizeof (player_interface_t))) == NULL)
  {
    printf ("itable_init: Failed to allocate memory for interface table\n");
    return -1;
  }

  for (ii = 0; ii < itable_len; ii++)
  {
    itable[ii].interf = interfaces[ii].interf;
    itable[ii].name = strdup (interfaces[ii].name);
  }

  return 0;
}

/*
 * Grows the interface table to newSize, filling each interface between the
 * old end and the new end with (0xFFFF, "nointerfXX").
 */
int itable_grow (int newSize)
{
  int ii;

  if ((itable = (player_interface_t*) realloc (itable, (newSize * sizeof (player_interface_t)))) == NULL)
  {
    printf("itable_grow: Failed to reallocate table memory\n");
    return -1;
  }
  // Fill in from the old end to the new end with undefined interfaces
  for (ii = itable_len; ii < newSize; ii++)
  {
    itable[ii].interf = 0xFFFF;
    if ((itable[ii].name = (char*) malloc (12)) == NULL)
    {
      printf("itable_grow: Failed to allocate memory for name\n");
      return -1;
    }
    snprintf (itable[ii].name, 12, "nointerf%d", ii);
  }
  // Set the new length
  itable_len = newSize;
  return 0;
}

/*
 * Destroys the interface names/codes table.
 */
void itable_destroy (void)
{
  unsigned int ii;

  for (ii = 0; ii < itable_len; ii++)
  {
    if (itable[ii].name != NULL)
      free (itable[ii].name);
  }
  free (itable);
}

/*
 * Add a new interface to the interface table.
 */
int itable_add (const char *name, unsigned int code, int replace)
{
  if(code < itable_len)
  {
    // It's already in the table.  Did the caller say to replace?
    if(!replace)
    {
      // Nope; return an error
      return(-1);
    }
    else
    {
      // Yes; replace
      if ((itable[code].name = strdup (name)) == NULL)
      {
        printf("itable_add: Failed to allocate memory for name\n");
        return -1;
      }

      return 0;
    }
  }
  else
  {
    // Not in the table; add it
    if (itable_grow (code + 1) < 0)
    {
      printf ("itable_add: Failed to grow interface table\n");
      return -1;
    }
    itable[code].interf = code;
    if ((itable[code].name = strdup (name)) == NULL)
    {
      printf("itable_add: Failed to allocate memory for name\n");
      return -1;
    }
    return 0;
  }
  return 0;
}

/*
 * looks through the array of available interfaces for one which the given
 * name.  if found, interface is filled out (the caller must provide storage)
 * and zero is returned.  otherwise, -1 is returned.
 */
int
lookup_interface(const char* name, player_interface_t* interf)
{
  unsigned int i;
  for(i=0; i<itable_len; i++)
  {
    if(!strcmp(name, itable[i].name))
    {
      *interf = itable[i];
      return 0;
    }
  }
  return -1;
}

/*
 * looks through the array of available interfaces for one which the given
 * code.  if found, interface is filled out (the caller must provide storage)
 * and zero is returned.  otherwise, -1 is returned.
 */
int
lookup_interface_code(int code, player_interface_t* interf)
{
  unsigned int i;
  for(i=0; i<itable_len; i++)
  {
    if(code == itable[i].interf)
    {
      *interf = itable[i];
      return 0;
    }
  }
  return -1;
}

/*
 * looks through the array of interfaces, starting at startpos, for the first
 * entry that has the given code, and return the name.
 * leturns 0 when the end of the * array is reached.
 */
const char*
lookup_interface_name(unsigned int startpos, int code)
{
  unsigned int i;
  if(startpos > itable_len)
    return 0;
  for(i = startpos; i<itable_len; i++)
  {
    if(code == itable[i].interf)
    {
      return itable[i].name;
    }
  }
  return 0;
}

/*
 * Returns the name of an interface given its code. The result string must
 * not be altered.
 */
const char* interf_to_str(uint16_t code)
{
//   static char unknownstring[15];
  if (code < itable_len)
  {
    if (itable[code].interf != 0xFFFF)
      return itable[code].name;
  }
//   snprintf (unknownstring, 15, "unknown[%d]", code);
  return "unknown";
}

/*
 * Returns the code for an interface, given a string.
 */
uint16_t
str_to_interf(const char *name)
{
  unsigned int ii;
  for(ii=0; ii<itable_len; ii++)
  {
    if(!strcmp(name, itable[ii].name))
      return itable[ii].interf;
  }
  return 0xFFFF;
}

/*
 * Returns the name of a message type given its code. The result string must
 * not be altered.
 */
const char* msgtype_to_str(uint8_t code)
{
//   static char unknownstring[13];
  if (code > 0 && code < 7)
    return msgTypeStrTable[code];
//   snprintf (unknownstring, 15, "unknown[%d]", code);
  return "unknown";
}

/*
 * Returns the code for a message type, given a string.
 */
uint8_t
str_to_msgtype(const char *name)
{
  unsigned int ii;
  for(ii=1; ii < 7; ii++)
  {
    if(!strcmp(name, msgTypeStrTable[ii]))
      return ii;
  }
  return 0xFF;
}