00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <fvutils/ipc/shm_lut.h>
00025 #include <fvutils/ipc/shm_exceptions.h>
00026 #include <utils/system/console_colors.h>
00027
00028 #include <iostream>
00029 #include <cstring>
00030 #include <cstdlib>
00031 #include <cstdio>
00032
00033 using namespace std;
00034 using namespace fawkes;
00035
00036 namespace firevision {
00037 #if 0
00038 }
00039 #endif
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 SharedMemoryLookupTable::SharedMemoryLookupTable(const char *lut_id,
00059 unsigned int width,
00060 unsigned int height,
00061 unsigned int depth,
00062 unsigned int bytes_per_cell)
00063 : SharedMemory(FIREVISION_SHM_LUT_MAGIC_TOKEN, false, true, true)
00064 {
00065 constructor(lut_id, width, height, depth, bytes_per_cell, false);
00066 }
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 SharedMemoryLookupTable::SharedMemoryLookupTable(const char *lut_id,
00078 bool is_read_only)
00079 : SharedMemory(FIREVISION_SHM_LUT_MAGIC_TOKEN, is_read_only, false, false)
00080 {
00081 constructor(lut_id, 0, 0, 0, 0, is_read_only);
00082 }
00083
00084
00085 void
00086 SharedMemoryLookupTable::constructor(const char *lut_id,
00087 unsigned int width, unsigned int height,
00088 unsigned int depth,
00089 unsigned int bytes_per_cell,
00090 bool is_read_only)
00091 {
00092 _is_read_only = is_read_only;
00093 __lut_id = strdup(lut_id);
00094 __width = width;
00095 __height = height;
00096 __depth = depth;
00097 __bytes_per_cell = bytes_per_cell;
00098
00099 __priv_header = new SharedMemoryLookupTableHeader(__lut_id, __width, __height, __depth, __bytes_per_cell);
00100 _header = __priv_header;
00101 attach();
00102 __raw_header = __priv_header->raw_header();
00103
00104 if (_memptr == NULL) {
00105 throw Exception("Could not create shared memory segment");
00106 }
00107 }
00108
00109
00110
00111 SharedMemoryLookupTable::~SharedMemoryLookupTable()
00112 {
00113 delete __priv_header;
00114 ::free(__lut_id);
00115 }
00116
00117
00118
00119
00120
00121 const char *
00122 SharedMemoryLookupTable::lut_id() const
00123 {
00124 return __lut_id;
00125 }
00126
00127
00128
00129
00130
00131
00132 bool
00133 SharedMemoryLookupTable::set_lut_id(const char *lut_id)
00134 {
00135 free();
00136 ::free(__lut_id);
00137 __lut_id = strdup(lut_id);
00138 __priv_header->set_lut_id(__lut_id);
00139 attach();
00140 return (_memptr != NULL);
00141 }
00142
00143
00144
00145
00146
00147 unsigned char *
00148 SharedMemoryLookupTable::buffer() const
00149 {
00150 return (unsigned char *)_memptr;
00151 }
00152
00153
00154
00155
00156
00157 unsigned int
00158 SharedMemoryLookupTable::width() const
00159 {
00160 return __raw_header->width;
00161 }
00162
00163
00164
00165
00166
00167 unsigned int
00168 SharedMemoryLookupTable::height() const
00169 {
00170 return __raw_header->height;
00171 }
00172
00173
00174
00175
00176
00177 unsigned int
00178 SharedMemoryLookupTable::depth() const
00179 {
00180 return __raw_header->depth;
00181 }
00182
00183
00184
00185
00186
00187 unsigned int
00188 SharedMemoryLookupTable::bytes_per_cell() const
00189 {
00190 return __raw_header->bytes_per_cell;
00191 }
00192
00193
00194
00195 void
00196 SharedMemoryLookupTable::list()
00197 {
00198 SharedMemoryLookupTableLister *lister = new SharedMemoryLookupTableLister();
00199 SharedMemoryLookupTableHeader *h = new SharedMemoryLookupTableHeader();
00200
00201 SharedMemory::list(FIREVISION_SHM_LUT_MAGIC_TOKEN, h, lister);
00202
00203 delete lister;
00204 delete h;
00205 }
00206
00207
00208
00209
00210
00211
00212 void
00213 SharedMemoryLookupTable::cleanup(bool use_lister)
00214 {
00215 SharedMemoryLookupTableLister *lister = NULL;
00216 SharedMemoryLookupTableHeader *h = new SharedMemoryLookupTableHeader();
00217
00218 if ( use_lister ) {
00219 lister = new SharedMemoryLookupTableLister();
00220 }
00221
00222 SharedMemory::erase_orphaned(FIREVISION_SHM_LUT_MAGIC_TOKEN, h, lister);
00223
00224 delete lister;
00225 delete h;
00226 }
00227
00228
00229
00230
00231
00232
00233 bool
00234 SharedMemoryLookupTable::exists(const char *lut_id)
00235 {
00236 SharedMemoryLookupTableHeader *h = new SharedMemoryLookupTableHeader(lut_id, 0, 0, 0, 0);
00237 bool ex = SharedMemory::exists(FIREVISION_SHM_LUT_MAGIC_TOKEN, h);
00238 delete h;
00239 return ex;
00240 }
00241
00242
00243
00244
00245
00246 void
00247 SharedMemoryLookupTable::wipe(const char *lut_id)
00248 {
00249 SharedMemoryLookupTableHeader *h = new SharedMemoryLookupTableHeader(lut_id, 0, 0, 0, 0);
00250 SharedMemory::erase(FIREVISION_SHM_LUT_MAGIC_TOKEN, h, NULL);
00251 delete h;
00252 }
00253
00254
00255
00256
00257
00258
00259
00260
00261 SharedMemoryLookupTableHeader::SharedMemoryLookupTableHeader()
00262 {
00263 __lut_id = NULL;
00264 __width = 0;
00265 __height = 0;
00266 __depth = 0;
00267 __bytes_per_cell = 0;
00268 __header = NULL;
00269 }
00270
00271
00272
00273
00274
00275
00276
00277
00278 SharedMemoryLookupTableHeader::SharedMemoryLookupTableHeader(const char *lut_id,
00279 unsigned int width,
00280 unsigned int height,
00281 unsigned int bytes_per_cell)
00282 {
00283 __lut_id = strdup(lut_id);
00284 __width = width;
00285 __height = height;
00286 __bytes_per_cell = bytes_per_cell;
00287
00288 __header = NULL;
00289 }
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 SharedMemoryLookupTableHeader::SharedMemoryLookupTableHeader(const char *lut_id,
00300 unsigned int width,
00301 unsigned int height,
00302 unsigned int depth,
00303 unsigned int bytes_per_cell)
00304 {
00305 __lut_id = strdup(lut_id);
00306 __width = width;
00307 __height = height;
00308 __depth = depth;
00309 __bytes_per_cell = bytes_per_cell;
00310
00311 __header = NULL;
00312 }
00313
00314
00315
00316
00317
00318 SharedMemoryLookupTableHeader::SharedMemoryLookupTableHeader(const SharedMemoryLookupTableHeader *h)
00319 {
00320 if( h->__lut_id != NULL ) {
00321 __lut_id = strdup(h->__lut_id);
00322 } else {
00323 __lut_id = NULL;
00324 }
00325 __width = h->__width;
00326 __height = h->__height;
00327 __depth = h->__depth;
00328 __bytes_per_cell = h->__bytes_per_cell;
00329
00330 __header = NULL;
00331 }
00332
00333
00334
00335 SharedMemoryLookupTableHeader::~SharedMemoryLookupTableHeader()
00336 {
00337 __header = NULL;
00338 if ( __lut_id != NULL ) {
00339 free(__lut_id);
00340 __lut_id = NULL;
00341 }
00342 }
00343
00344
00345 SharedMemoryHeader *
00346 SharedMemoryLookupTableHeader::clone() const
00347 {
00348 return new SharedMemoryLookupTableHeader(this);
00349 }
00350
00351
00352 size_t
00353 SharedMemoryLookupTableHeader::size()
00354 {
00355 return sizeof(SharedMemoryLookupTable_header_t);
00356 }
00357
00358
00359 size_t
00360 SharedMemoryLookupTableHeader::data_size()
00361 {
00362 if (__header == NULL) {
00363 return __width * __height * __depth * __bytes_per_cell;
00364 } else {
00365 return __header->width * __header->height * __header->depth * __header->bytes_per_cell;
00366 }
00367 }
00368
00369
00370 bool
00371 SharedMemoryLookupTableHeader::matches(void *memptr)
00372 {
00373 SharedMemoryLookupTable_header_t *h = (SharedMemoryLookupTable_header_t *)memptr;
00374
00375 if (__lut_id == NULL) {
00376 return true;
00377
00378 } else if (strncmp(h->lut_id, __lut_id, LUT_ID_MAX_LENGTH) == 0) {
00379
00380 if ( (__width == 0) ||
00381 (__height == 0) ||
00382 (__depth == 0) ||
00383 (__bytes_per_cell == 0) ||
00384 ( (h->width == __width) &&
00385 (h->height == __height) &&
00386 (h->depth == __depth) &&
00387 (h->bytes_per_cell == __bytes_per_cell) )
00388 ) {
00389 return true;
00390 } else {
00391 throw InconsistentLUTException("Inconsistent lookup table found in memory (meta)");
00392 }
00393 } else {
00394 return false;
00395 }
00396
00397 }
00398
00399
00400
00401 void
00402 SharedMemoryLookupTableHeader::print_info()
00403 {
00404 if (__header == NULL) {
00405 cout << "No image set" << endl;
00406 return;
00407 }
00408 cout << "SharedMemory Lookup Table Info: " << endl
00409 << " LUT ID: " << __header->lut_id << endl
00410 << " dimensions: " << __header->width << "x" << __header->height << "x"
00411 << __header->depth << endl
00412 << " bytes per cell: " << __header->bytes_per_cell << endl;
00413 }
00414
00415
00416
00417
00418
00419
00420 bool
00421 SharedMemoryLookupTableHeader::create()
00422 {
00423 return ( (__width > 0) &&
00424 (__height > 0) &&
00425 (__depth > 0) &&
00426 (__bytes_per_cell > 0) );
00427 }
00428
00429
00430 void
00431 SharedMemoryLookupTableHeader::initialize(void *memptr)
00432 {
00433 __header = (SharedMemoryLookupTable_header_t *)memptr;
00434 memset(memptr, 0, sizeof(SharedMemoryLookupTable_header_t));
00435
00436 strncpy(__header->lut_id, __lut_id, LUT_ID_MAX_LENGTH);
00437 __header->width = __width;
00438 __header->height = __height;
00439 __header->depth = __depth;
00440 __header->bytes_per_cell = __bytes_per_cell;
00441 }
00442
00443
00444 void
00445 SharedMemoryLookupTableHeader::set(void *memptr)
00446 {
00447 __header = (SharedMemoryLookupTable_header_t *)memptr;
00448 }
00449
00450
00451 void
00452 SharedMemoryLookupTableHeader::reset()
00453 {
00454 __header = NULL;
00455 }
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467 bool
00468 SharedMemoryLookupTableHeader::operator==(const SharedMemoryHeader &s) const
00469 {
00470 const SharedMemoryLookupTableHeader *h = dynamic_cast<const SharedMemoryLookupTableHeader *>(&s);
00471 if ( ! h ) {
00472 return false;
00473 } else {
00474 return ( (strncmp(__lut_id, h->__lut_id, LUT_ID_MAX_LENGTH) == 0) &&
00475 (__width == h->__width) &&
00476 (__height == h->__height) &&
00477 (__depth == h->__depth) &&
00478 (__bytes_per_cell == h->__bytes_per_cell) );
00479 }
00480 }
00481
00482
00483
00484
00485
00486 unsigned int
00487 SharedMemoryLookupTableHeader::width() const
00488 {
00489 if (__header == NULL) return 0;
00490 return __header->width;
00491 }
00492
00493
00494
00495
00496
00497 unsigned int
00498 SharedMemoryLookupTableHeader::height() const
00499 {
00500 if (__header == NULL) return 0;
00501 return __header->height;
00502 }
00503
00504
00505
00506
00507
00508 unsigned int
00509 SharedMemoryLookupTableHeader::depth() const
00510 {
00511 if (__header == NULL) return 0;
00512 return __header->depth;
00513 }
00514
00515
00516
00517
00518
00519 unsigned int
00520 SharedMemoryLookupTableHeader::bytes_per_cell() const
00521 {
00522 if (__header == NULL) return 0;
00523 return __header->bytes_per_cell;
00524 }
00525
00526
00527
00528
00529
00530 const char *
00531 SharedMemoryLookupTableHeader::lut_id() const
00532 {
00533 if (__header == NULL) return NULL;
00534 return __header->lut_id;
00535 }
00536
00537
00538
00539
00540
00541 void
00542 SharedMemoryLookupTableHeader::set_lut_id(const char *lut_id)
00543 {
00544 if ( __lut_id ) free(__lut_id);
00545 __lut_id = strdup(lut_id);
00546 }
00547
00548
00549
00550
00551
00552 SharedMemoryLookupTable_header_t *
00553 SharedMemoryLookupTableHeader::raw_header()
00554 {
00555 return __header;
00556 }
00557
00558
00559
00560
00561
00562
00563
00564 SharedMemoryLookupTableLister::SharedMemoryLookupTableLister()
00565 {
00566 }
00567
00568
00569
00570 SharedMemoryLookupTableLister::~SharedMemoryLookupTableLister()
00571 {
00572 }
00573
00574
00575 void
00576 SharedMemoryLookupTableLister::print_header()
00577 {
00578 cout << endl << cgreen << "FireVision Shared Memory Segments - Lookup Tables"
00579 << cnormal << endl
00580 << "========================================================================================" << endl
00581 << cdarkgray;
00582 printf ("%-23s %-10s %-10s %-10s %-9s %-9s %-9s\n",
00583 "LUT ID", "ShmID", "Semaphore", "Bytes", "Width", "Height", "State");
00584 cout << cnormal
00585 << "----------------------------------------------------------------------------------------" << endl;
00586 }
00587
00588
00589 void
00590 SharedMemoryLookupTableLister::print_footer()
00591 {
00592 }
00593
00594
00595 void
00596 SharedMemoryLookupTableLister::print_no_segments()
00597 {
00598 cout << "No FireVision shared memory segments containing lookup tables found" << endl;
00599 }
00600
00601
00602
00603
00604 void
00605 SharedMemoryLookupTableLister::print_no_orphaned_segments()
00606 {
00607 cout << "No orphaned FireVision shared memory segments containing lookup tables found" << endl;
00608 }
00609
00610 void
00611 SharedMemoryLookupTableLister::print_info(const SharedMemoryHeader *header,
00612 int shm_id, int semaphore,
00613 unsigned int mem_size,
00614 const void *memptr)
00615 {
00616
00617 SharedMemoryLookupTableHeader *h = (SharedMemoryLookupTableHeader *)header;
00618
00619 printf("%-23s %-10d %-10d %-10u %-9u %-9u %s%s\n",
00620 h->lut_id(), shm_id, semaphore, mem_size,
00621 h->width(), h->height(),
00622 (SharedMemory::is_swapable(shm_id) ? "S" : ""),
00623 (SharedMemory::is_destroyed(shm_id) ? "D" : "")
00624 );
00625 }
00626
00627 }