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

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