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

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