po_hi_storage.c 18.8 KB
Newer Older
jdelange's avatar
jdelange committed
1
2
3
4
5
6
7
/*
 * This is a part of PolyORB-HI-C distribution, a minimal
 * middleware written for generated code from AADL models.
 * You should use it with the Ocarina toolsuite.
 *
 * For more informations, please visit http://assert-project.net/taste
 *
jhugues's avatar
jhugues committed
8
 * Copyright (C) 2011-2012 ESA & ISAE.
jdelange's avatar
jdelange committed
9
10
 */

11
#include <po_hi_config.h>
jdelange's avatar
jdelange committed
12
13
#include <string.h>
#include <po_hi_debug.h>
jdelange's avatar
jdelange committed
14
15
16
17
18
19
20
21
22
#include <po_hi_storage.h>
#include <po_hi_returns.h>

/*
 * Files operations
 */

int __po_hi_storage_file_open (const char* filename, __po_hi_storage_file_t* file)
{
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
   int len;
   int fd;

   if (file == NULL)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_open: invalid file argument\n");
      return __PO_HI_INVALID;
   }

   len = strlen (filename);

   memset (file->filename, 0, __PO_HI_STORAGE_FILENAME_MAXLENGTH);

   if (len >= __PO_HI_STORAGE_FILENAME_MAXLENGTH)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_open: file failed to be opened\n");
      return __PO_HI_INVALID;
   }

   strncpy (file->filename, filename, len);

#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   /*
    * If the file already exist, we open it for reading/writing
    */

49
   fd = open (filename, O_RDWR | O_SYNC);
50
51
52

   if (fd == -1)
   {
53
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_open: warning, file %s does not exist, continue anyway\n", filename);
54
55
56
57
58
59
60
61
   }
   else
   {
      file->fd = fd;
   }

   return __PO_HI_SUCCESS;
#endif
jdelange's avatar
jdelange committed
62
63
64
   return __PO_HI_NOTIMPLEMENTED;
}

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

int __po_hi_storage_file_close (__po_hi_storage_file_t* file)
{
   if ( (file == NULL) || (file->filename == NULL))
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_close: invalid file argument\n");
      return __PO_HI_INVALID;
   }

#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   if (file->fd == -1)
   {
      __DEBUGMSG ("[STORAGE] Warning, file %s does not exist, continue anyway\n", file->filename);
      return __PO_HI_INVALID;
   }

   if ( close (file->fd))
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_close: cannot close file\n", file->filename);
      return __PO_HI_ERROR_UNKNOWN;
   }

   file->fd = -1;

   return __PO_HI_SUCCESS;
#endif

   return __PO_HI_NOTIMPLEMENTED;
}


96
int __po_hi_storage_file_create (__po_hi_storage_file_t* file)
jdelange's avatar
jdelange committed
97
{
98
99
100
101
102
103
104
105
106
107
108
109
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   struct stat ss;
   int fd;
#endif

   if ((file == NULL) || (file->filename == NULL))
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_create: invalid file parameter\n");
      return __PO_HI_INVALID;
   }

#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
110
   if (stat (file->filename, &ss) == 0)
111
   {
112
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_create: file %s already exists\n", file->filename);
113
114
115
116
117
118
119
120
121
      return __PO_HI_ERROR_EXISTS;
   }

   /*
    * We assume the file is not open previously by a call to open().
    * Otherwise, we assume this is an error.
    */
   if (file->fd != -1)
   {
122
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_create: file already opened (%d)\n", file->fd);
123
124
   }

125
   fd = open (file->filename, O_RDWR | O_CREAT | O_SYNC, S_IRWXU | S_IRGRP | S_IROTH);
126
127
128
129
130
131
132
133
134
135
136

   if (fd == -1)
   {
      __DEBUGMSG ("[STORAGE] Warning, cannot open file %s with create attributes\n");
      return __PO_HI_INVALID;
   }

   file->fd = fd;

   return __PO_HI_SUCCESS;
#endif
jdelange's avatar
jdelange committed
137
138
139
140
141
   return __PO_HI_NOTIMPLEMENTED;
}

int __po_hi_storage_file_read (const __po_hi_storage_file_t* file, char* buf, int bufsize)
{
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
   int n;

   if ((file == NULL) || (file->filename == NULL))
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_read: invalid file parameter\n");
      return __PO_HI_INVALID;
   }

#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   if (file->fd == -1)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_read: invalid file handle (%d)\n", file->fd);
      return __PO_HI_INVALID;
   }

   n = read (file->fd, buf, bufsize);

   if (n != bufsize)
   {
      return __PO_HI_ERROR_UNKNOWN;
   }

   return __PO_HI_SUCCESS;
#endif
jdelange's avatar
jdelange committed
166
167
168
169
170
   return __PO_HI_NOTIMPLEMENTED;
}

int __po_hi_storage_file_write (const __po_hi_storage_file_t* file, char* buf, int bufsize)
{
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
   int n;

   if ((file == NULL) || (file->filename == NULL))
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_write: invalid file parameter\n");
      return __PO_HI_INVALID;
   }

#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   if (file->fd == -1)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_write: invalid file handle (%d)\n", file->fd);
      return __PO_HI_INVALID;
   }

   n = write (file->fd, buf, bufsize);

   if (n != bufsize)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_write: invalid buffer size\n");
      return __PO_HI_ERROR_UNKNOWN;
   }

   return __PO_HI_SUCCESS;
#endif
jdelange's avatar
jdelange committed
196
197
198
199
200
   return __PO_HI_NOTIMPLEMENTED;
}

int __po_hi_storage_file_delete (const __po_hi_storage_file_t* file)
{
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
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   struct stat ss;
#endif

   if ((file == NULL) || (file->filename == NULL))
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_delete: invalid file parameter\n");
      return __PO_HI_INVALID;
   }

#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   if (stat (file->filename, &ss) != 0)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_delete: file does not exist\n");
      return __PO_HI_ERROR_EXISTS;
   }

   if (file->fd != -1)
   {
      if (close (file->fd))
      {
         __DEBUGMSG ("[STORAGE] __po_hi_storage_file_delete: error when trying to close the file (fd=%d)\n", file->fd);
         return __PO_HI_ERROR_UNKNOWN;
      }
   }

   if (unlink (file->filename))
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_delete: error when trying to delete the file\n");
      return __PO_HI_ERROR_UNKNOWN;
   }
   return __PO_HI_SUCCESS;
#endif
jdelange's avatar
jdelange committed
234
235
236
   return __PO_HI_NOTIMPLEMENTED;
}

237
int __po_hi_storage_file_rename (const __po_hi_storage_file_t* oldfile, const __po_hi_storage_file_t* newfile)
jdelange's avatar
jdelange committed
238
{
239
240
241
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   struct stat ss;
#endif
242
243
244
245
246
247
248
249
250
251
252
253

   if ((oldfile == NULL) || (oldfile->filename == NULL))
   {
      return __PO_HI_INVALID;
   }

   if ((newfile == NULL) || (newfile->filename == NULL))
   {
      return __PO_HI_INVALID;
   }

#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
254
   if (stat (newfile->filename, &ss) == 0)
255
   {
256
257
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_rename: destination file %s already exists\n", newfile->filename);
      return __PO_HI_ERROR_EXISTS;
258
259
   }

260
   if (stat (oldfile->filename, &ss))
261
   {
262
263
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_rename: source file %s does not exist\n", oldfile->filename);
      return __PO_HI_ERROR_NOEXISTS;
264
265
   }

266
   if (! S_ISREG (ss.st_mode))
267
   {
268
269
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_rename: source file %s is not a regular file\n", oldfile->filename);
      return __PO_HI_ERROR_NOEXISTS;
270
271
   }

272
   if (rename (oldfile->filename, newfile->filename))
273
274
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_rename: error when trying to delete old file\n");
275
      return __PO_HI_ERROR_UNKNOWN;
276
277
278
279
280
   }

   return __PO_HI_SUCCESS;
#endif

jdelange's avatar
jdelange committed
281
282
283
284
285
   return __PO_HI_NOTIMPLEMENTED;
}

int __po_hi_storage_file_append (const __po_hi_storage_file_t* file, char* buf, int bufsize)
{
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
311
312
313
314
315
   int ret;

   if ((file == NULL) || (file->filename == NULL))
   {
      return __PO_HI_INVALID;
   }

#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   if (file->fd == -1)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_append: file %s does not have an appropriate descriptor\n", file->filename);
      return __PO_HI_INVALID;
   }

   if (lseek (file->fd, 0, SEEK_END))
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_append: error when trying to set file offset\n");
      return __PO_HI_ERROR_UNKNOWN;
   }

   ret = write (file->fd, buf, bufsize);

   if (ret != bufsize)
   { 
      __DEBUGMSG ("[STORAGE] __po_hi_storage_file_append: cannot write %d bytes\n", bufsize);
      return __PO_HI_ERROR_UNKNOWN;
   }

   return __PO_HI_SUCCESS;
#endif
jdelange's avatar
jdelange committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
   return __PO_HI_NOTIMPLEMENTED;
}

int __po_hi_storage_file_replace (const __po_hi_storage_file_t* oldfile, const __po_hi_storage_file_t* newfile)
{
   return __PO_HI_NOTIMPLEMENTED;
}

int __po_hi_storage_file_lock (const __po_hi_storage_file_t* file)
{
   return __PO_HI_NOTIMPLEMENTED;
}

int __po_hi_storage_file_unlock (const __po_hi_storage_file_t* file)
{
   return __PO_HI_NOTIMPLEMENTED;
}

/*
 * Directory operations
 */

int __po_hi_storage_directory_open (const char* dirname, __po_hi_storage_dir_t* dir)
{
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
   int len;

   if (dirname == NULL)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_directory_open: invalid directory parameter\n");
      return __PO_HI_INVALID;
   }

   if (dir == NULL)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_directory_open: invalid directory parameter\n");
      return __PO_HI_INVALID;
   }

   len = strlen (dirname);

   if (len >= __PO_HI_STORAGE_FILENAME_MAXLENGTH)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_directory_open: name too long\n");
      return __PO_HI_INVALID;
   }

   memset (dir->dirname, 0, __PO_HI_STORAGE_FILENAME_MAXLENGTH);

   memcpy (dir->dirname, dirname, len);

366
367
   dir->nb_files = 0;

368
   return __PO_HI_SUCCESS;
jdelange's avatar
jdelange committed
369
370
371
372
}

int __po_hi_storage_directory_create (const __po_hi_storage_dir_t* dir)
{
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   struct stat ss;
#endif

   if (dir == NULL)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_directory_create: invalid directory parameter\n");
      return __PO_HI_INVALID;
   }

#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   if (stat (dir->dirname, &ss))
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_directory_create: file already exists\n");
      return __PO_HI_ERROR_EXISTS;
   }

   if (mkdir (dir->dirname, S_IRWXU | S_IRGRP | S_IROTH))
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_directory_create: mkdir error\n");
      return __PO_HI_INVALID;
   }
   return __PO_HI_SUCCESS;
#endif

jdelange's avatar
jdelange committed
398
399
400
401
402
   return __PO_HI_NOTIMPLEMENTED;
}

int __po_hi_storage_directory_delete (const __po_hi_storage_dir_t* dir)
{
403
404
405
406
407
408
409
410
411
412
413
414
415
416
   if (dir == NULL)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_directory_delete: invalid directory parameter\n");
      return __PO_HI_INVALID;
   }

#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   if (rmdir (dir->dirname))
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_directory_delete: rmdir error\n");
      return __PO_HI_INVALID;
   }
   return __PO_HI_SUCCESS;
#endif
jdelange's avatar
jdelange committed
417
418
419
   return __PO_HI_NOTIMPLEMENTED;
}

420
int __po_hi_storage_directory_rename (const __po_hi_storage_dir_t* olddir, const __po_hi_storage_dir_t* newdir)
jdelange's avatar
jdelange committed
421
{
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   struct stat ss;
#endif

   if ((olddir == NULL) || (olddir->dirname == NULL))
   {
      return __PO_HI_INVALID;
   }

   if ((newdir == NULL) || (newdir->dirname == NULL))
   {
      return __PO_HI_INVALID;
   }

#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   if (stat (newdir->dirname, &ss) == 0)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_directory_rename: destination directory %s already exists\n", newdir->dirname);
      return __PO_HI_ERROR_EXISTS;
   }

   if (stat (olddir->dirname, &ss))
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_directory_rename: source directory %s does not exist\n", olddir->dirname);
      return __PO_HI_ERROR_NOEXISTS;
   }

   if (! S_ISDIR (ss.st_mode))
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_directory_rename: source directory %s is not a directory\n", olddir->dirname);
      return __PO_HI_ERROR_NOEXISTS;
   }

   if (rename (olddir->dirname, newdir->dirname))
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_directory_rename: error when trying to rename\n");
      return __PO_HI_ERROR_UNKNOWN;
   }

   return __PO_HI_SUCCESS;
#endif

jdelange's avatar
jdelange committed
464
465
466
   return __PO_HI_NOTIMPLEMENTED;
}

467
int __po_hi_storage_directory_list (__po_hi_storage_dir_t* dir)
jdelange's avatar
jdelange committed
468
{
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   struct dirent* ent;
   DIR*           sdir;
   int            n;
   int            len;
#endif

   if ((dir == NULL) || (dir->dirname == NULL))
   {
      return __PO_HI_INVALID;
   }


#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   sdir = NULL;
   sdir = opendir (dir->dirname);

   if (sdir == NULL)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_directory_list: fail to call opendir on %s\n", dir->dirname);
      return __PO_HI_ERROR_NOEXISTS;
   }

   dir->nb_files = 0;

   while ( ( ( ent = readdir (sdir) ) != NULL ) && (dir->nb_files < __PO_HI_STORAGE_DIRECTORY_MAXFILES) )
   {
      n = dir->nb_files;
      len = strlen (ent->d_name);
      if (len < __PO_HI_STORAGE_FILENAME_MAXLENGTH)
      {
         n = dir->nb_files;
         memset (dir->filename[n], 0, __PO_HI_STORAGE_FILENAME_MAXLENGTH);
         memcpy (dir->filename[n], ent->d_name, len);
         dir->nb_files++;
      }
      else
      {
         __DEBUGMSG ("[STORAGE] __po_hi_storage_directory_list: invalid filename %s\n", ent->d_name);
      }
   }

   if (closedir (sdir))
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_directory_list: fail to call closedir on %s\n", dir->dirname);
      return __PO_HI_ERROR_UNKNOWN;
   }
   
   return __PO_HI_SUCCESS;
#endif

jdelange's avatar
jdelange committed
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
   return __PO_HI_NOTIMPLEMENTED;
}

int __po_hi_storage_directory_lock (const __po_hi_storage_dir_t* dir)
{
   return __PO_HI_NOTIMPLEMENTED;
}

int __po_hi_storage_directory_unlock (const __po_hi_storage_dir_t* dir)
{
   return __PO_HI_NOTIMPLEMENTED;
}

int __po_hi_storage_change_cdir (__po_hi_storage_dir_t* new_current_directory)
{
535
536
537
538
539
540
541
542
543
544
545
   if ( (new_current_directory == NULL) || (new_current_directory->dirname == NULL))
   {
      return __PO_HI_INVALID;
   }
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   if (chdir (new_current_directory->dirname))
   {
      return __PO_HI_ERROR_NOEXISTS;
   }
   return __PO_HI_SUCCESS;
#endif
jdelange's avatar
jdelange committed
546
547
548
549
550
   return __PO_HI_NOTIMPLEMENTED;
}

int __po_hi_storage_get_cdir (__po_hi_storage_dir_t* current_directory)
{
551
552
553
554
555
556
557
558
559
560
561
562
563
564
   if ( (current_directory == NULL) || (current_directory->dirname == NULL))
   {
      return __PO_HI_INVALID;
   }
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   memset (current_directory->dirname, 0, __PO_HI_STORAGE_FILENAME_MAXLENGTH);

   if (getcwd (current_directory->dirname, __PO_HI_STORAGE_FILENAME_MAXLENGTH) == NULL)
   {
      return __PO_HI_ERROR_UNKNOWN;
   }
   return __PO_HI_SUCCESS;
#endif

jdelange's avatar
jdelange committed
565
566
567
568
569
570
571
572
573
574
   return __PO_HI_NOTIMPLEMENTED;
}


/*
 * Packets service
 */

int __po_hi_storage_packet_store_new (__po_hi_storage_packet_store_t* store)
{
jdelange's avatar
jdelange committed
575
576
577
578
579
580
581
582
583
   if (__po_hi_mutex_init (&store->mutex, __PO_HI_MUTEX_REGULAR, 0) != __PO_HI_SUCCESS)
   {
      return __PO_HI_ERROR_MUTEX_CREATE;
   }
   store->capacity   = __PO_HI_STORAGE_MAX_PACKETS;
   store->n_packets  = 0;
   store->read_off   = 0;
   store->write_off  = 0;
   return __PO_HI_SUCCESS;
jdelange's avatar
jdelange committed
584
585
586
587
588
589
590
591
592
593
594
595
596
597
}

int __po_hi_storage_packet_store_new_from_file (__po_hi_storage_packet_store_t* store, __po_hi_storage_file_t* file)
{
   return __PO_HI_NOTIMPLEMENTED;
}

int __po_hi_storage_packet_store_write_to_file (__po_hi_storage_packet_store_t* store, __po_hi_storage_file_t* file)
{
   return __PO_HI_NOTIMPLEMENTED;
}

int __po_hi_storage_packet_store_read (__po_hi_storage_packet_store_t* store, __po_hi_storage_packet_t* packet)
{
jdelange's avatar
jdelange committed
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
   int retcode = __PO_HI_ERROR_UNKNOWN;

   if (store == NULL)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_packet_store_read: store is NULL\n");
      return __PO_HI_INVALID;
   }

   if (packet == NULL)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_packet_store_read: packet is NULL\n");
      return __PO_HI_INVALID;
   }

   if (__po_hi_mutex_lock (&store->mutex) != __PO_HI_SUCCESS)
   {
      return __PO_HI_ERROR_MUTEX_LOCK;
   }

   if (store->n_packets < 1)
   {
      retcode = __PO_HI_UNAVAILABLE;
   }
   else
   {
      memcpy (packet, &(store->packets[store->read_off * __PO_HI_STORAGE_PACKET_SIZE]), __PO_HI_STORAGE_PACKET_SIZE);

      store->read_off   = (store->read_off + 1 % store->capacity);
      store->n_packets  = store->n_packets - 1;

      retcode = __PO_HI_SUCCESS;
   }

   if (__po_hi_mutex_unlock (&store->mutex) != __PO_HI_SUCCESS)
   {
      return __PO_HI_ERROR_MUTEX_UNLOCK;
   }
   return retcode;
jdelange's avatar
jdelange committed
636
637
638
639
}

int __po_hi_storage_packet_store_write (__po_hi_storage_packet_store_t* store, __po_hi_storage_packet_t* packet)
{
jdelange's avatar
jdelange committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
   int retcode = __PO_HI_ERROR_UNKNOWN;

   if (store == NULL)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_packet_store_write: store is NULL\n");
      return __PO_HI_INVALID;
   }

   if (packet == NULL)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_packet_store_write: packet is NULL\n");
      return __PO_HI_INVALID;
   }

   if (__po_hi_mutex_lock (&store->mutex) != __PO_HI_SUCCESS)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_packet_store_write: cannot lock mutex\n");
      return __PO_HI_ERROR_MUTEX_LOCK;
   }

   if (store->n_packets >= store->capacity)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_packet_store_write: store is full\n");
      retcode = __PO_HI_TOOMANY;
   }
   else
   {
      memcpy (&(store->packets[store->write_off * __PO_HI_STORAGE_PACKET_SIZE]), packet, __PO_HI_STORAGE_PACKET_SIZE);

      store->write_off  = (store->write_off + 1 % store->capacity);
      store->n_packets  = store->n_packets + 1;

      retcode = __PO_HI_SUCCESS;
   }

   if (__po_hi_mutex_unlock (&store->mutex) != __PO_HI_SUCCESS)
   {
      __DEBUGMSG ("[STORAGE] __po_hi_storage_packet_store_write: cannot unlock mutex\n");
      return __PO_HI_ERROR_MUTEX_UNLOCK;
   }
   return retcode;
jdelange's avatar
jdelange committed
681
682
683
684
}

int __po_hi_storage_packet_store_free (__po_hi_storage_packet_store_t* store, int n_free)
{
jdelange's avatar
jdelange committed
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
   int retcode = __PO_HI_ERROR_UNKNOWN;

   if (store == NULL)
   {
      return __PO_HI_INVALID;
   }

   if (n_free == 0)
   {
      return __PO_HI_INVALID;
   }

   if (__po_hi_mutex_lock (&store->mutex) != __PO_HI_SUCCESS)
   {
      return __PO_HI_ERROR_MUTEX_LOCK;
   }

   if (store->n_packets < n_free)
   {
      retcode = __PO_HI_UNAVAILABLE;
   }
   else
   {
      store->read_off   = (store->read_off + n_free % store->capacity);
      store->n_packets  = store->n_packets - n_free;

      retcode = __PO_HI_SUCCESS;
   }

   if (__po_hi_mutex_unlock (&store->mutex) != __PO_HI_SUCCESS)
   {
      return __PO_HI_ERROR_MUTEX_UNLOCK;
   }
   return retcode;
jdelange's avatar
jdelange committed
719
720
721
722
}

int __po_hi_storage_packet_store_status (__po_hi_storage_packet_store_t* store, __po_hi_storage_packet_store_status_t* status)
{
jdelange's avatar
jdelange committed
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
   if (store == NULL)
   {
      return __PO_HI_INVALID;
   }

   if (status == NULL)
   {
      return __PO_HI_INVALID;
   }

   if (__po_hi_mutex_lock (&store->mutex) != __PO_HI_SUCCESS)
   {
      return __PO_HI_ERROR_MUTEX_LOCK;
   }

   status->n_packets = store->n_packets;
   status->n_avail   = store->capacity - store->n_packets;
   status->full = (store->n_packets == store->capacity) ? 1 : 0;

   if (__po_hi_mutex_unlock (&store->mutex) != __PO_HI_SUCCESS)
   {
      return __PO_HI_ERROR_MUTEX_UNLOCK;
   }
   return __PO_HI_SUCCESS;
jdelange's avatar
jdelange committed
747
748
}