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 = calloc(allocation_length+1, sizeof(char));
80  comp_name = calloc(DEFAULT_LINE_LENGHT, sizeof(char));
81  temp_name = calloc(DEFAULT_LINE_LENGHT, sizeof(char));
82  comp_rules = calloc(DEFAULT_LINE_LENGHT, sizeof(char));
83  checkChar = calloc(2, sizeof(char));
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 = calloc(4096, sizeof(char));
186  buffer = calloc(8192, sizeof(char));
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 = calloc(index + 2, sizeof(char));
198  *(actual + index) = '/';
199  *(actual+index + 1) = '\0';
200  } else {
201  actual = calloc(index + 1, sizeof(char));
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 = calloc(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  if (err != strlen(lib_absolute_path)) {
252  DEBUG(DEB_LEV_ERR, "Failed to write %zu bytes to fd %d\n", strlen(lib_absolute_path), fileno(omxregistryfp));
253  continue;
254  }
255  err = fwrite("\n", 1, strlen(buffer), omxregistryfp);
256  if (err != strlen(buffer)) {
257  DEBUG(DEB_LEV_ERR, "Failed to write %zu bytes to fd %d\n", strlen(buffer), fileno(omxregistryfp));
258  continue;
259  }
260 
261 
262  for (i = 0; i<num_of_comp; i++) {
263  tempName = allNames;
264  if (tempName != NULL) {
265  do {
266  if (!strcmp(tempName->name, stComponents[i]->name)) {
267  DEBUG(DEB_LEV_ERR, "Component %s already registered. Skip\n", stComponents[i]->name);
268  break;
269  }
270  tempName = tempName->next;
271  } while(tempName != NULL);
272  if (tempName != NULL) {
273  continue;
274  }
275  }
276  if (allNames == NULL) {
277  allNames = calloc(1, sizeof(nameList));
278  currentName = allNames;
279  } else {
280  currentName->next = calloc(1, sizeof(nameList));
281  currentName = currentName->next;
282  }
283  currentName->next = NULL;
284  currentName->name = calloc(strlen(stComponents[i]->name) + 1, sizeof(char));
285  strcpy(currentName->name, stComponents[i]->name);
286  *(currentName->name + strlen(currentName->name)) = '\0';
287 
288  DEBUG(DEB_LEV_PARAMS, "Found component %s version=%d.%d.%d.%d in shared object %s\n",
289  stComponents[i]->name,
290  stComponents[i]->componentVersion.s.nVersionMajor,
291  stComponents[i]->componentVersion.s.nVersionMinor,
292  stComponents[i]->componentVersion.s.nRevision,
293  stComponents[i]->componentVersion.s.nStep,
294  lib_absolute_path);
295  if (verbose) {
296  printf("Component %s registered with %i quality levels\n", stComponents[i]->name, (int)stComponents[i]->nqualitylevels);
297  }
298  if (stComponents[i]->nqualitylevels > 0) {
299  index_string = 0;
300  sprintf((qualityString + index_string), "%i ", (int)stComponents[i]->nqualitylevels);
301  index_string = index_string + int2strlen(stComponents[i]->nqualitylevels) + 1;
302  for (qi=0; qi<stComponents[i]->nqualitylevels; qi++) {
303  sprintf((qualityString + index_string), "%i,%i ",
304  stComponents[i]->multiResourceLevel[qi]->CPUResourceRequested,
305  stComponents[i]->multiResourceLevel[qi]->MemoryResourceRequested);
306  index_string = index_string + 2 +
307  int2strlen(stComponents[i]->multiResourceLevel[qi]->CPUResourceRequested) +
308  int2strlen(stComponents[i]->multiResourceLevel[qi]->MemoryResourceRequested);
309  }
310  index_string--;
311  *(qualityString + index_string) = '\0';
312  }
313  // insert first of all the name of the library
314  strcpy(buffer, arrow);
315  strcat(buffer, stComponents[i]->name);
316  if (stComponents[i]->name_specific_length>0) {
317  nroles += stComponents[i]->name_specific_length;
318  strcat(buffer, arrow);
319  for(j=0;j<stComponents[i]->name_specific_length;j++){
320  if (verbose) {
321  printf(" Specific role %s registered\n", stComponents[i]->name_specific[j]);
322  }
323  strcat(buffer, stComponents[i]->name_specific[j]);
324  strcat(buffer, ":");
325  }
326  }
327 
328  if ((qualityString != NULL) && (qualityString[0] != '\0')) {
329  strcat(buffer, arrow);
330  strcat(buffer, qualityString);
331  }
332  qualityString[0] = '\0';
333  strcat(buffer, "\n");
334  err = fwrite(buffer, 1, strlen(buffer), omxregistryfp);
335  ncomponents++;
336  }
337  for (i = 0; i < num_of_comp; i++) {
338  free(stComponents[i]->name);
339  for (k=0; k<stComponents[i]->name_specific_length; k++) {
340  free(stComponents[i]->name_specific[k]);
341  free(stComponents[i]->role_specific[k]);
342  }
343  if (stComponents[i]->name_specific_length > 0) {
344  free(stComponents[i]->name_specific);
345  free(stComponents[i]->role_specific);
346  }
347  for (k=0; k<stComponents[i]->nqualitylevels; k++) {
348  free(stComponents[i]->multiResourceLevel[k]);
349  }
350  if (stComponents[i]->multiResourceLevel) {
351  free(stComponents[i]->multiResourceLevel);
352  }
353  free(stComponents[i]);
354  }
355  free(stComponents);
356  }
357  }
358  }
359  }
360  free(actual);
361  closedir(dirp);
362  }
363  if (verbose) {
364  printf("\n %i OpenMAX IL ST static components in %i libraries succesfully scanned\n", ncomponents, num_of_libraries);
365  } else {
366  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);
367  }
368  free(qualityString);
369  free(buffer);
370  return 0;
371 }
372 
373 static void usage(const char *app) {
374  char *registry_filename;
375  registry_filename = componentsRegistryGetFilename();
376 
377  printf(
378  "Usage: %s [-l] [-v] [-h] [componentspath[:other_components_path]]...\n"
379  "\n"
380  "Version 0.9.2\n"
381  "\n"
382  "This programs scans for a given list of directory searching for any OpenMAX\n"
383  "component compatible with the ST static component loader.\n"
384  "The registry is saved under %s. (can be changed via OMX_BELLAGIO_REGISTRY\n"
385  "environment variable)\n"
386  "\n"
387  "The following options are supported:\n"
388  "\n"
389  " -v display a verbose output, listing all the components registered\n"
390  " -l list only the components already registered. If -l is specified \n"
391  " all the other parameters are ignored and only the register file\n"
392  " is checked\n"
393  " -h display this message\n"
394  "\n"
395  " componentspath: a searching path for components can be specified.\n"
396  " If this parameter is omitted, the components are searched in the\n"
397  " locations specified by the environment variable BELLAGIO_SEARCH_PATH.If it \n"
398  " is not defined the components are searched in the default %s directory \n"
399  "\n",
400  app, registry_filename, OMXILCOMPONENTSPATH);
401 
402  free(registry_filename);
403 }
404 
410 int main(int argc, char *argv[]) {
411  int found;
412  int err, i;
413  int verbose=0;
414  FILE *omxregistryfp;
415  char *registry_filename;
416  char *dir,*dirp;
417  char *buffer;
418  int isListOnly = 0;
419 
420  for(i = 1; i < argc; i++) {
421  if(*(argv[i]) != '-') {
422  continue;
423  }
424  if (*(argv[i]+1) == 'v') {
425  verbose = 1;
426  } else if (*(argv[i]+1) == 'l') {
427  isListOnly = 1;
428  } else {
429  usage(argv[0]);
430  exit(*(argv[i]+1) == 'h' ? 0 : -EINVAL);
431  }
432  }
433 
434  registry_filename = componentsRegistryGetFilename();
435 
436  /* make sure the registry directory exists */
437  dir = strdup(registry_filename);
438  if (dir == NULL) {
439  exit(EXIT_FAILURE);
440  }
441  dirp = strrchr(dir, '/');
442  if (dirp != NULL) {
443  *dirp = '\0';
444  if (makedir(dir)) {
445  DEBUG(DEB_LEV_ERR, "Cannot create OpenMAX registry directory %s\n", dir);
446  exit(EXIT_FAILURE);
447  }
448  }
449  free(dir);
450 
451  if (isListOnly) {
452  omxregistryfp = fopen(registry_filename, "r");
453  } else {
454  omxregistryfp = fopen(registry_filename, "w");
455  }
456  if (omxregistryfp == NULL){
457  DEBUG(DEB_LEV_ERR, "Cannot open OpenMAX registry file %s\n", registry_filename);
458  exit(EXIT_FAILURE);
459  }
460 
461  free(registry_filename);
462  if (isListOnly) {
463  err = showComponentsList(omxregistryfp);
464  if(err) {
465  DEBUG(DEB_LEV_ERR, "Error reading omxregister file\n");
466  }
467  exit(0);
468  }
469 
470  for(i = 1, found = 0; i < argc; i++) {
471  if(*(argv[i]) == '-') {
472  continue;
473  }
474 
475  found = 1;
476  err = buildComponentsList(omxregistryfp, argv[i], verbose);
477  if(err) {
478  DEBUG(DEB_LEV_ERR, "Error registering OpenMAX components with ST static component loader %s\n", strerror(err));
479  continue;
480  }
481  }
482 
483  if (found == 0) {
484  buffer=getenv("BELLAGIO_SEARCH_PATH");
485  if (buffer!=NULL&&*buffer!='\0') {
486  err = buildComponentsList(omxregistryfp, buffer, verbose);
487  if(err) {
488  DEBUG(DEB_LEV_ERR, "Error registering OpenMAX components with ST static component loader %s\n", strerror(err));
489  }
490  } else {
491  err = buildComponentsList(omxregistryfp, OMXILCOMPONENTSPATH, verbose);
492  if(err) {
493  DEBUG(DEB_LEV_ERR, "Error registering OpenMAX components with ST static component loader %s\n", strerror(err));
494  }
495  }
496  }
497 
498  fclose(omxregistryfp);
499 
500  return 0;
501 }
DEBUG
#define DEBUG(n, fmt, args...)
Definition: omx_comp_debug_levels.h:77
nameList
Definition: common.h:38
nameList::name
char * name
Definition: common.h:39
DEFAULT_LINE_LENGHT
#define DEFAULT_LINE_LENGHT
Definition: omxregister.c:50
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:410
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:40
componentsRegistryGetFilename
char * componentsRegistryGetFilename()
Get registry filename This function returns the name of the registry file for the components loaded w...
Definition: common.c:46
makedir
int makedir(const char *newdir)
Definition: common.c:118

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