OpenMAXBellagio  0.9.3
omxregister.c
Go to the documentation of this file.
1 
39 #include <dlfcn.h>
40 #include <dirent.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <errno.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 
48 #include "common.h"
49 
50 #define DEFAULT_LINE_LENGHT 500
51 
54 static const char arrow[] = " ==> ";
55 
56 int int2strlen(int value) {
57  int ret = 0;
58  if (value<0) return -1;
59  while(value>0) {
60  value = value/10;
61  ret++;
62  }
63  return ret;
64 }
68 static int showComponentsList(FILE* omxregistryfp) {
69  char* buffer;
70  char* temp_buffer, *temp_rules;
71  char *comp_name, *temp_name, *comp_rules;
72  char* checkChar;
73  int data_read;
74  int allocation_length = DEFAULT_LINE_LENGHT;
75  long int start_pos, end_pos;
76  long int offset;
77  int i;
78 
79  buffer = malloc(allocation_length+1);
80  comp_name = malloc(DEFAULT_LINE_LENGHT);
81  temp_name = malloc(DEFAULT_LINE_LENGHT);
82  comp_rules = malloc(DEFAULT_LINE_LENGHT);
83  checkChar = malloc(2);
84 
85  printf("*********************************\n");
86  printf("* List of registered components *\n");
87  printf("*********************************\n");
88  while(1) {
89  //read line
90  start_pos = ftell(omxregistryfp);
91  do {
92  data_read = fread(checkChar, 1, 1, omxregistryfp);
93  } while ((*checkChar != '\n') && (data_read > 0));
94  if (feof(omxregistryfp)) {
95  break;
96  }
97  end_pos = ftell(omxregistryfp);
98  offset = (end_pos - start_pos);
99  fseek(omxregistryfp, start_pos, SEEK_SET);
100  data_read = fread(buffer, offset, 1, omxregistryfp);
101  buffer[offset] = '\0';
102  if (buffer[0] == '/') {
103  continue;
104  }
105  temp_buffer = buffer+5;
106  i = 0;
107  while ((temp_buffer[i] != '\0') && (temp_buffer[i] != ' ')) {
108  i++;
109  }
110  strncpy(comp_name, temp_buffer, i);
111  comp_name[i] = '\0';
112  temp_buffer += i;
113  if (*temp_buffer != '\0') {
114  temp_buffer += 5;
115  i = 0;
116  while ((temp_buffer[i] != '\n') && (temp_buffer[i] != ' ')) {
117  i++;
118  }
119  strncpy(comp_rules, temp_buffer, i);
120  comp_rules[i] = '\0';
121  } else {
122  comp_rules[0] = '\0';
123  }
124  printf("Component %s\n", comp_name);
125  if (comp_rules[0] != '\0') {
126  temp_rules = comp_rules;
127  printf(" supported formats:\n");
128  i = 0;
129  while (*(temp_rules+i) != '\0') {
130  i++;
131  if (*(temp_rules+i) == ':') {
132  strncpy(temp_name, temp_rules, i);
133  temp_name[i] = '\0';
134  temp_rules += i+1;
135  printf(" %s\n", temp_name);
136  i = 0;
137  }
138  }
139  }
140  printf("\n");
141  }
142 
143  free(buffer);
144  free(comp_name);
145  free(temp_name);
146  free(comp_rules);
147  free(checkChar);
148 
149  return 0;
150 }
159 static int buildComponentsList(FILE* omxregistryfp, char *componentspath, int verbose) {
160  DIR *dirp;
161  struct dirent *dp;
162  void *handle = NULL;
163  int i, num_of_comp, k, qi;
164  int num_of_libraries = 0;
165  unsigned int j;
166  char *buffer = NULL;
167  int (*fptr)(void *);
168  stLoaderComponentType **stComponents;
169  int ncomponents = 0, nroles=0;
170  int pathconsumed = 0;
171  int currentgiven;
172  int index;
173  char* currentpath = componentspath;
174  char* actual;
175  nameList *allNames = NULL;
176  nameList *currentName = NULL;
177  nameList *tempName = NULL;
178  char* qualityString = NULL;
179  int index_string;
180  /* the componentpath contains a single or multiple directories
181  * and is is colon separated like env variables in Linux
182  */
183 
184  qualityString = malloc(4096);
185  buffer = malloc(8192);
186  while (!pathconsumed) {
187  index = 0;
188  currentgiven = 0;
189  while (!currentgiven) {
190  if (*(currentpath + index) == '\0') {
191  pathconsumed = 1;
192  }
193  if ((*(currentpath + index) == ':') || (*(currentpath + index) =='\0')) {
194  currentgiven = 1;
195  if (*(currentpath + index - 1) != '/') {
196  actual = malloc(index + 2);
197  *(actual + index) = '/';
198  *(actual+index + 1) = '\0';
199  } else {
200  actual = malloc(index + 1);
201  *(actual+index) = '\0';
202  }
203  strncpy(actual, currentpath, index);
204  currentpath = currentpath + index + 1;
205  }
206  index++;
207  }
208  /* Populate the registry file */
209  dirp = opendir(actual);
210  if (verbose) {
211  printf("\n Scanning directory %s\n", actual);
212  }
213  if(dirp == NULL){
214  free(actual);
215  DEBUG(DEB_LEV_SIMPLE_SEQ, "Cannot open directory %s\n", actual);
216  continue;
217  }
218  while((dp = readdir(dirp)) != NULL) {
219  int len = strlen(dp->d_name);
220 
221  if(len >= 3){
222 
223 
224  if(strncmp(dp->d_name+len-3, ".so", 3) == 0){
225  char lib_absolute_path[strlen(actual) + len + 1];
226 
227  strcpy(lib_absolute_path, actual);
228  strcat(lib_absolute_path, dp->d_name);
229 
230  if((handle = dlopen(lib_absolute_path, RTLD_NOW)) == NULL) {
231  DEBUG(DEB_LEV_ERR, "could not load %s: %s\n", lib_absolute_path, dlerror());
232  } else {
233  if (verbose) {
234  printf("\n Scanning library %s\n", lib_absolute_path);
235  }
236  if ((fptr = dlsym(handle, "omx_component_library_Setup")) == NULL) {
237  DEBUG(DEB_LEV_SIMPLE_SEQ, "the library %s is not compatible with ST static component loader - %s\n", lib_absolute_path, dlerror());
238  continue;
239  }
240  num_of_libraries++;
241  num_of_comp = fptr(NULL);
242  stComponents = malloc(num_of_comp * sizeof(stLoaderComponentType*));
243  for (i = 0; i<num_of_comp; i++) {
244  stComponents[i] = calloc(1,sizeof(stLoaderComponentType));
245  stComponents[i]->nqualitylevels = 0;
246  stComponents[i]->multiResourceLevel = NULL;
247  }
248  fptr(stComponents);
249  fwrite(lib_absolute_path, 1, strlen(lib_absolute_path), omxregistryfp);
250  fwrite("\n", 1, 1, omxregistryfp);
251 
252 
253  for (i = 0; i<num_of_comp; i++) {
254  tempName = allNames;
255  if (tempName != NULL) {
256  do {
257  if (!strcmp(tempName->name, stComponents[i]->name)) {
258  DEBUG(DEB_LEV_ERR, "Component %s already registered. Skip\n", stComponents[i]->name);
259  break;
260  }
261  tempName = tempName->next;
262  } while(tempName != NULL);
263  if (tempName != NULL) {
264  continue;
265  }
266  }
267  if (allNames == NULL) {
268  allNames = malloc(sizeof(nameList));
269  currentName = allNames;
270  } else {
271  currentName->next = malloc(sizeof(nameList));
272  currentName = currentName->next;
273  }
274  currentName->next = NULL;
275  currentName->name = malloc(strlen(stComponents[i]->name) + 1);
276  strcpy(currentName->name, stComponents[i]->name);
277  *(currentName->name + strlen(currentName->name)) = '\0';
278 
279  DEBUG(DEB_LEV_PARAMS, "Found component %s version=%d.%d.%d.%d in shared object %s\n",
280  stComponents[i]->name,
281  stComponents[i]->componentVersion.s.nVersionMajor,
282  stComponents[i]->componentVersion.s.nVersionMinor,
283  stComponents[i]->componentVersion.s.nRevision,
284  stComponents[i]->componentVersion.s.nStep,
285  lib_absolute_path);
286  if (verbose) {
287  printf("Component %s registered with %i quality levels\n", stComponents[i]->name, (int)stComponents[i]->nqualitylevels);
288  }
289  if (stComponents[i]->nqualitylevels > 0) {
290  index_string = 0;
291  sprintf((qualityString + index_string), "%i ", (int)stComponents[i]->nqualitylevels);
292  index_string = index_string + int2strlen(stComponents[i]->nqualitylevels) + 1;
293  for (qi=0; qi<stComponents[i]->nqualitylevels; qi++) {
294  sprintf((qualityString + index_string), "%i,%i ",
295  stComponents[i]->multiResourceLevel[qi]->CPUResourceRequested,
296  stComponents[i]->multiResourceLevel[qi]->MemoryResourceRequested);
297  index_string = index_string + 2 +
298  int2strlen(stComponents[i]->multiResourceLevel[qi]->CPUResourceRequested) +
299  int2strlen(stComponents[i]->multiResourceLevel[qi]->MemoryResourceRequested);
300  }
301  index_string--;
302  *(qualityString + index_string) = '\0';
303  }
304  // insert first of all the name of the library
305  strcpy(buffer, arrow);
306  strcat(buffer, stComponents[i]->name);
307  if (stComponents[i]->name_specific_length>0) {
308  nroles += stComponents[i]->name_specific_length;
309  strcat(buffer, arrow);
310  for(j=0;j<stComponents[i]->name_specific_length;j++){
311  if (verbose) {
312  printf(" Specific role %s registered\n", stComponents[i]->name_specific[j]);
313  }
314  strcat(buffer, stComponents[i]->name_specific[j]);
315  strcat(buffer, ":");
316  }
317  }
318 
319  if ((qualityString != NULL) && (qualityString[0] != '\0')) {
320  strcat(buffer, arrow);
321  strcat(buffer, qualityString);
322  }
323  qualityString[0] = '\0';
324  strcat(buffer, "\n");
325  fwrite(buffer, 1, strlen(buffer), omxregistryfp);
326  ncomponents++;
327  }
328  for (i = 0; i < num_of_comp; i++) {
329  free(stComponents[i]->name);
330  for (k=0; k<stComponents[i]->name_specific_length; k++) {
331  free(stComponents[i]->name_specific[k]);
332  free(stComponents[i]->role_specific[k]);
333  }
334  if (stComponents[i]->name_specific_length > 0) {
335  free(stComponents[i]->name_specific);
336  free(stComponents[i]->role_specific);
337  }
338  for (k=0; k<stComponents[i]->nqualitylevels; k++) {
339  free(stComponents[i]->multiResourceLevel[k]);
340  }
341  if (stComponents[i]->multiResourceLevel) {
342  free(stComponents[i]->multiResourceLevel);
343  }
344  free(stComponents[i]);
345  }
346  free(stComponents);
347  }
348  }
349  }
350  }
351  free(actual);
352  closedir(dirp);
353  }
354  if (verbose) {
355  printf("\n %i OpenMAX IL ST static components in %i libraries succesfully scanned\n", ncomponents, num_of_libraries);
356  } else {
357  DEBUG(DEB_LEV_SIMPLE_SEQ, "\n %i OpenMAX IL ST static components with %i roles in %i libraries succesfully scanned\n", ncomponents, nroles, num_of_libraries);
358  }
359  free(qualityString);
360  free(buffer);
361  return 0;
362 }
363 
364 static void usage(const char *app) {
365  char *registry_filename;
366  registry_filename = componentsRegistryGetFilename();
367 
368  printf(
369  "Usage: %s [-l] [-v] [-h] [componentspath[:other_components_path]]...\n"
370  "\n"
371  "Version 0.9.2\n"
372  "\n"
373  "This programs scans for a given list of directory searching for any OpenMAX\n"
374  "component compatible with the ST static component loader.\n"
375  "The registry is saved under %s. (can be changed via OMX_BELLAGIO_REGISTRY\n"
376  "environment variable)\n"
377  "\n"
378  "The following options are supported:\n"
379  "\n"
380  " -v display a verbose output, listing all the components registered\n"
381  " -l list only the components already registered. If -l is specified \n"
382  " all the other parameters are ignored and only the register file\n"
383  " is checked\n"
384  " -h display this message\n"
385  "\n"
386  " componentspath: a searching path for components can be specified.\n"
387  " If this parameter is omitted, the components are searched in the\n"
388  " locations specified by the environment variable BELLAGIO_SEARCH_PATH.If it \n"
389  " is not defined the components are searched in the default %s directory \n"
390  "\n",
391  app, registry_filename, OMXILCOMPONENTSPATH);
392 
393  free(registry_filename);
394 }
395 
401 int main(int argc, char *argv[]) {
402  int found;
403  int err, i;
404  int verbose=0;
405  FILE *omxregistryfp;
406  char *registry_filename;
407  char *dir,*dirp;
408  char *buffer;
409  int isListOnly = 0;
410 
411  for(i = 1; i < argc; i++) {
412  if(*(argv[i]) != '-') {
413  continue;
414  }
415  if (*(argv[i]+1) == 'v') {
416  verbose = 1;
417  } else if (*(argv[i]+1) == 'l') {
418  isListOnly = 1;
419  } else {
420  usage(argv[0]);
421  exit(*(argv[i]+1) == 'h' ? 0 : -EINVAL);
422  }
423  }
424 
425  registry_filename = componentsRegistryGetFilename();
426 
427  /* make sure the registry directory exists */
428  dir = strdup(registry_filename);
429  if (dir == NULL) {
430  exit(EXIT_FAILURE);
431  }
432  dirp = strrchr(dir, '/');
433  if (dirp != NULL) {
434  *dirp = '\0';
435  if (makedir(dir)) {
436  DEBUG(DEB_LEV_ERR, "Cannot create OpenMAX registry directory %s\n", dir);
437  exit(EXIT_FAILURE);
438  }
439  }
440  free(dir);
441 
442  if (isListOnly) {
443  omxregistryfp = fopen(registry_filename, "r");
444  } else {
445  omxregistryfp = fopen(registry_filename, "w");
446  }
447  if (omxregistryfp == NULL){
448  DEBUG(DEB_LEV_ERR, "Cannot open OpenMAX registry file %s\n", registry_filename);
449  exit(EXIT_FAILURE);
450  }
451 
452  free(registry_filename);
453  if (isListOnly) {
454  err = showComponentsList(omxregistryfp);
455  if(err) {
456  DEBUG(DEB_LEV_ERR, "Error reading omxregister file\n");
457  }
458  exit(0);
459  }
460 
461  for(i = 1, found = 0; i < argc; i++) {
462  if(*(argv[i]) == '-') {
463  continue;
464  }
465 
466  found = 1;
467  err = buildComponentsList(omxregistryfp, argv[i], verbose);
468  if(err) {
469  DEBUG(DEB_LEV_ERR, "Error registering OpenMAX components with ST static component loader %s\n", strerror(err));
470  continue;
471  }
472  }
473 
474  if (found == 0) {
475  buffer=getenv("BELLAGIO_SEARCH_PATH");
476  if (buffer!=NULL&&*buffer!='\0') {
477  err = buildComponentsList(omxregistryfp, buffer, verbose);
478  if(err) {
479  DEBUG(DEB_LEV_ERR, "Error registering OpenMAX components with ST static component loader %s\n", strerror(err));
480  }
481  } else {
482  err = buildComponentsList(omxregistryfp, OMXILCOMPONENTSPATH, verbose);
483  if(err) {
484  DEBUG(DEB_LEV_ERR, "Error registering OpenMAX components with ST static component loader %s\n", strerror(err));
485  }
486  }
487  }
488 
489  fclose(omxregistryfp);
490 
491  return 0;
492 }
DEBUG
#define DEBUG(n, fmt, args...)
Definition: omx_comp_debug_levels.h:77
nameList
Definition: common.h:39
nameList::name
char * name
Definition: common.h:40
DEFAULT_LINE_LENGHT
#define DEFAULT_LINE_LENGHT
Definition: omxregister.c:50
componentsRegistryGetFilename
char * componentsRegistryGetFilename(void)
Get registry filename This function returns the name of the registry file for the components loaded w...
Definition: common.c:47
DEB_LEV_ERR
#define DEB_LEV_ERR
Definition: omx_comp_debug_levels.h:39
main
int main(int argc, char *argv[])
execution of registration function
Definition: omxregister.c:401
stLoaderComponentType::nqualitylevels
OMX_U32 nqualitylevels
Definition: st_static_component_loader.h:47
int2strlen
int int2strlen(int value)
Definition: omxregister.c:56
st_static_component_loader.h
DEB_LEV_SIMPLE_SEQ
#define DEB_LEV_SIMPLE_SEQ
Definition: omx_comp_debug_levels.h:48
handle
OMX_HANDLETYPE handle
Definition: omxvolcontroltest.c:35
stLoaderComponentType::multiResourceLevel
multiResourceDescriptor ** multiResourceLevel
Definition: st_static_component_loader.h:48
err
OMX_ERRORTYPE err
Definition: omxvolcontroltest.c:34
common.h
stLoaderComponentType::name_specific_length
unsigned int name_specific_length
Definition: st_static_component_loader.h:42
stLoaderComponentType
the private data structure handled by the ST static loader that described an OpenMAX component
Definition: st_static_component_loader.h:39
DEB_LEV_PARAMS
#define DEB_LEV_PARAMS
Definition: omx_comp_debug_levels.h:43
nameList::next
struct nameList * next
Definition: common.h:41
makedir
int makedir(const char *newdir)
Definition: common.c:150

Generated for OpenMAX Bellagio rel. 0.9.3 by  doxygen 1.5.1
SourceForge.net Logo