00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
#include <config.h>
00034
00035
#include <stdio.h>
00036
#include <stdlib.h>
00037
#include <unistd.h>
00038
#include <string.h>
00039
#include <errno.h>
00040
#include <sys/time.h>
00041
00042
#include "avrerror.h"
00043
#include "avrmalloc.h"
00044
#include "avrclass.h"
00045
#include "utils.h"
00046
00047
00048
00049
int
00050 str2ffmt (
char *str)
00051 {
00052
if (strncmp (str,
"bin", 3) == 0)
00053
return FFMT_BIN;
00054
if (strncmp (str,
"ihex", 4) == 0)
00055
return FFMT_IHEX;
00056
if (strncmp (str,
"elf", 3) == 0)
00057
return FFMT_ELF;
00058
00059
return -1;
00060 }
00061
00062
00063
00064
extern inline uint8_t
set_bit_in_byte (uint8_t src,
int bit,
int val);
00065
00066
00067
00068
extern inline uint16_t
set_bit_in_word (uint16_t src,
int bit,
int val);
00069
00070
00071
00072
00073
00074
00075 uint64_t
00076 get_program_time (
void)
00077 {
00078 uint64_t result;
00079
struct timeval tv;
00080
00081
if (gettimeofday (&tv, NULL) < 0)
00082
avr_error (
"Failed to get program time.");
00083
00084 result = ((uint64_t) tv.tv_sec * 1000) + ((uint64_t) tv.tv_usec / 1000);
00085
00086
return result;
00087 }
00088
00089
00090
00091
00092
00093
00094
00095
static DList *dlist_new_node (AvrClass *data);
00096
static void dlist_construct_node (DList *node, AvrClass *data);
00097
static void dlist_destroy_node (
void *node);
00098
00099
#ifndef DOXYGEN
00100
00101
00102
struct _DList
00103 {
00104 AvrClass parent;
00105
struct _DList *prev;
00106
struct _DList *next;
00107 AvrClass *data;
00108 };
00109
00110
#endif
00111
00112
static DList *
00113 dlist_new_node (AvrClass *data)
00114 {
00115 DList *node;
00116
00117 node =
avr_new (DList, 1);
00118 dlist_construct_node (node, data);
00119
class_overload_destroy ((AvrClass *)node, dlist_destroy_node);
00120
00121
return node;
00122 }
00123
00124
static void
00125 dlist_construct_node (DList *node, AvrClass *data)
00126 {
00127
if (node == NULL)
00128
avr_error (
"passed null ptr");
00129
00130
class_construct ((AvrClass *)node);
00131
00132 node->prev = NULL;
00133 node->next = NULL;
00134
00135 node->data = data;
00136 }
00137
00138
static void
00139 dlist_destroy_node (
void *node)
00140 {
00141 DList *_node = (DList *)node;
00142
00143
if (_node == NULL)
00144
return;
00145
00146
class_unref (_node->data);
00147
00148
class_destroy (node);
00149 }
00150
00151
00152
00153
00154
00155
00156
00157
00158 DList *
00159 dlist_add (DList *head, AvrClass *data, DListFP_Cmp cmp)
00160 {
00161 DList *node = head;
00162
00163
if (head == NULL)
00164
00165
return dlist_new_node (data);
00166
00167
00168
00169
while (node)
00170 {
00171
if (cmp && ((*cmp) (node->data, data) == 0))
00172 {
00173
00174
class_unref (data);
00175
break;
00176 }
00177
00178
if (node->next == NULL)
00179 {
00180
00181 node->next = dlist_new_node (data);
00182 node->next->prev = node;
00183
break;
00184 }
00185
00186
00187 node = node->next;
00188 }
00189
00190
return head;
00191 }
00192
00193
00194
00195 DList *
00196 dlist_add_head (DList *head, AvrClass *data)
00197 {
00198 DList *node = dlist_new_node (data);;
00199
00200
if (head)
00201 {
00202 head->prev = node;
00203 node->next = head;
00204 }
00205
00206
return node;
00207 }
00208
00209
00210
00211
00212
00213
00214 DList *
00215 dlist_delete (DList *head, AvrClass *data, DListFP_Cmp cmp)
00216 {
00217 DList *node = head;
00218
00219
if (cmp == NULL)
00220
avr_error (
"compare function not specified");
00221
00222
while (node)
00223 {
00224
if ((*cmp) (node->data, data) == 0)
00225 {
00226
if ((node->prev == NULL) && (node->next == NULL))
00227 {
00228
00229 head = NULL;
00230 }
00231
else if (node->prev == NULL)
00232 {
00233
00234 node->next->prev = NULL;
00235 head = node->next;
00236 }
00237
else if (node->next == NULL)
00238 {
00239
00240 node->prev->next = NULL;
00241 }
00242
else
00243 {
00244
00245 node->prev->next = node->next;
00246 node->next->prev = node->prev;
00247 }
00248
00249
00250
class_unref ((AvrClass *)node);
00251
00252
return head;
00253 }
00254
00255
00256 node = node->next;
00257 }
00258
00259
00260
return head;
00261 }
00262
00263
00264
00265
void
00266 dlist_delete_all (DList *head)
00267 {
00268 DList *node;
00269
00270
while (head)
00271 {
00272 node = head;
00273 head = head->next;
00274
00275
class_unref ((AvrClass *)node);
00276 }
00277 }
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290 AvrClass *
00291 dlist_lookup (DList *head, AvrClass *data, DListFP_Cmp cmp)
00292 {
00293 DList *node = head;
00294
00295
if (cmp == NULL)
00296
avr_error (
"compare function not specified");
00297
00298
while (node)
00299 {
00300
if ((*cmp) (node->data, data) == 0)
00301
return node->data;
00302
00303 node = node->next;
00304 }
00305
00306
00307
00308
return NULL;
00309 }
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 AvrClass *
00321 dlist_get_head_data (DList *head)
00322 {
00323
00324
if (head == NULL)
00325 {
00326
return NULL;
00327 }
00328
00329
return head->data;
00330 }
00331
00332
00333
00334
static int
00335 dlist_iterator_cmp (AvrClass *n1, AvrClass *n2)
00336 {
00337
00338
00339
00340
00341
return (
int)(n1 - n2);
00342 }
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 DList *
00357 dlist_iterator (DList *head, DListFP_Iter func,
void *user_data)
00358 {
00359 DList *node = head;
00360
00361
if (func == NULL)
00362
avr_error (
"no iteration func supplied");
00363
00364
while (node)
00365 {
00366
if ((*func) (node->data, user_data))
00367 {
00368
00369 head =
dlist_delete (head, node->data, dlist_iterator_cmp);
00370 }
00371
00372 node = node->next;
00373 }
00374
00375
return head;
00376 }