OpenMAXBellagio  0.9.3
omxregister.c
Go to the documentation of this file.
00001 
00039 #include <dlfcn.h>
00040 #include <dirent.h>
00041 #include <stdlib.h>
00042 #include <string.h>
00043 #include <errno.h>
00044 #include <sys/stat.h>
00045 #include <sys/types.h>
00046 
00047 #include "st_static_component_loader.h"
00048 #include "common.h"
00049 
00050 #define DEFAULT_LINE_LENGHT 500
00051 
00054 static const char arrow[] =  " ==> ";
00055 
00056 int int2strlen(int value) {
00057     int ret = 0;
00058     if (value<0) return -1;
00059     while(value>0) {
00060         value = value/10;
00061         ret++;
00062     }
00063     return ret;
00064 }
00068 static int showComponentsList(FILE* omxregistryfp) {
00069     char* buffer;
00070     char* temp_buffer, *temp_rules;
00071     char *comp_name, *temp_name, *comp_rules;
00072     char* checkChar;
00073     int data_read;
00074     int allocation_length = DEFAULT_LINE_LENGHT;
00075     long int start_pos, end_pos;
00076     long int offset;
00077     int i;
00078 
00079     buffer = malloc(allocation_length+1);
00080     comp_name = malloc(DEFAULT_LINE_LENGHT);
00081     temp_name = malloc(DEFAULT_LINE_LENGHT);
00082     comp_rules = malloc(DEFAULT_LINE_LENGHT);
00083     checkChar = malloc(2);
00084 
00085     printf("*********************************\n");
00086     printf("* List of registered components *\n");
00087     printf("*********************************\n");
00088     while(1) {
00089         //read line
00090         start_pos = ftell(omxregistryfp);
00091         do {
00092             data_read = fread(checkChar, 1, 1, omxregistryfp);
00093         } while ((*checkChar != '\n') && (data_read > 0));
00094         if (feof(omxregistryfp)) {
00095             break;
00096         }
00097         end_pos = ftell(omxregistryfp);
00098         offset = (end_pos - start_pos);
00099         fseek(omxregistryfp, start_pos, SEEK_SET);
00100         data_read = fread(buffer, offset, 1, omxregistryfp);
00101         buffer[offset] = '\0';
00102         if (buffer[0] == '/') {
00103             continue;
00104         }
00105         temp_buffer = buffer+5;
00106         i = 0;
00107         while ((temp_buffer[i] != '\0') && (temp_buffer[i] != ' ')) {
00108             i++;
00109         }
00110         strncpy(comp_name, temp_buffer, i);
00111         comp_name[i] = '\0';
00112         temp_buffer += i;
00113         if (*temp_buffer != '\0') {
00114             temp_buffer += 5;
00115             i = 0;
00116             while ((temp_buffer[i] != '\n') && (temp_buffer[i] != ' ')) {
00117                 i++;
00118             }
00119             strncpy(comp_rules, temp_buffer, i);
00120             comp_rules[i] = '\0';
00121         } else {
00122             comp_rules[0] = '\0';
00123         }
00124         printf("Component %s\n", comp_name);
00125         if (comp_rules[0] != '\0') {
00126             temp_rules = comp_rules;
00127             printf("          supported formats:\n");
00128             i = 0;
00129             while (*(temp_rules+i) != '\0') {
00130                 i++;
00131                 if (*(temp_rules+i) == ':') {
00132                     strncpy(temp_name, temp_rules, i);
00133                     temp_name[i] = '\0';
00134                     temp_rules += i+1;
00135                     printf("             %s\n", temp_name);
00136                     i = 0;
00137                 }
00138             }
00139         }
00140         printf("\n");
00141     }
00142 
00143     free(buffer);
00144     free(comp_name);
00145     free(temp_name);
00146     free(comp_rules);
00147     free(checkChar);
00148 
00149     return 0;
00150 }
00159 static int buildComponentsList(FILE* omxregistryfp, char *componentspath, int verbose) {
00160   DIR *dirp;
00161     struct dirent *dp;
00162     void *handle = NULL;
00163     int i, num_of_comp, k, qi;
00164     int num_of_libraries = 0;
00165     unsigned int j;
00166     char *buffer = NULL;
00167     int (*fptr)(void *);
00168     stLoaderComponentType **stComponents;
00169     int ncomponents = 0, nroles=0;
00170     int pathconsumed = 0;
00171     int currentgiven;
00172     int index;
00173     char* currentpath = componentspath;
00174     char* actual;
00175     nameList *allNames = NULL;
00176     nameList *currentName = NULL;
00177     nameList *tempName = NULL;
00178     char* qualityString = NULL;
00179     int index_string;
00180     /* the componentpath contains a single or multiple directories
00181      * and is is colon separated like env variables in Linux
00182      */
00183 
00184     qualityString = malloc(4096);
00185     buffer = malloc(8192);
00186     while (!pathconsumed) {
00187         index = 0;
00188         currentgiven = 0;
00189         while (!currentgiven) {
00190             if (*(currentpath + index) == '\0') {
00191                 pathconsumed = 1;
00192             }
00193             if ((*(currentpath + index) == ':') || (*(currentpath + index) =='\0')) {
00194                 currentgiven = 1;
00195                 if (*(currentpath + index - 1) != '/') {
00196                     actual = malloc(index + 2);
00197                     *(actual + index) = '/';
00198                     *(actual+index + 1) = '\0';
00199                 } else {
00200                     actual = malloc(index + 1);
00201                     *(actual+index) = '\0';
00202                 }
00203                 strncpy(actual, currentpath, index);
00204                 currentpath = currentpath + index + 1;
00205             }
00206             index++;
00207         }
00208         /* Populate the registry file */
00209         dirp = opendir(actual);
00210         if (verbose) {
00211             printf("\n Scanning directory %s\n", actual);
00212         }
00213         if(dirp == NULL){
00214             free(actual);
00215             DEBUG(DEB_LEV_SIMPLE_SEQ, "Cannot open directory %s\n", actual);
00216             continue;
00217         }
00218         while((dp = readdir(dirp)) != NULL) {
00219             int len = strlen(dp->d_name);
00220 
00221             if(len >= 3){
00222 
00223 
00224                 if(strncmp(dp->d_name+len-3, ".so", 3) == 0){
00225                     char lib_absolute_path[strlen(actual) + len + 1];
00226 
00227                     strcpy(lib_absolute_path, actual);
00228                     strcat(lib_absolute_path, dp->d_name);
00229 
00230                     if((handle = dlopen(lib_absolute_path, RTLD_NOW)) == NULL) {
00231                         DEBUG(DEB_LEV_ERR, "could not load %s: %s\n", lib_absolute_path, dlerror());
00232                     } else {
00233                         if (verbose) {
00234                             printf("\n Scanning library %s\n", lib_absolute_path);
00235                         }
00236                         if ((fptr = dlsym(handle, "omx_component_library_Setup")) == NULL) {
00237                             DEBUG(DEB_LEV_SIMPLE_SEQ, "the library %s is not compatible with ST static component loader - %s\n", lib_absolute_path, dlerror());
00238                             continue;
00239                         }
00240                         num_of_libraries++;
00241                         num_of_comp = fptr(NULL);
00242                         stComponents = malloc(num_of_comp * sizeof(stLoaderComponentType*));
00243                         for (i = 0; i<num_of_comp; i++) {
00244                             stComponents[i] = calloc(1,sizeof(stLoaderComponentType));
00245                             stComponents[i]->nqualitylevels = 0;
00246                             stComponents[i]->multiResourceLevel = NULL;
00247                         }
00248                         fptr(stComponents);
00249                         fwrite(lib_absolute_path, 1, strlen(lib_absolute_path), omxregistryfp);
00250                         fwrite("\n", 1, 1, omxregistryfp);
00251 
00252 
00253                         for (i = 0; i<num_of_comp; i++) {
00254                             tempName = allNames;
00255                             if (tempName != NULL) {
00256                                 do  {
00257                                     if (!strcmp(tempName->name, stComponents[i]->name)) {
00258                                         DEBUG(DEB_LEV_ERR, "Component %s already registered. Skip\n", stComponents[i]->name);
00259                                         break;
00260                                     }
00261                                     tempName = tempName->next;
00262                                 } while(tempName != NULL);
00263                                 if (tempName != NULL) {
00264                                     continue;
00265                                 }
00266                             }
00267                             if (allNames == NULL) {
00268                                 allNames = malloc(sizeof(nameList));
00269                                 currentName = allNames;
00270                             } else {
00271                                 currentName->next = malloc(sizeof(nameList));
00272                                 currentName = currentName->next;
00273                             }
00274                             currentName->next = NULL;
00275                             currentName->name = malloc(strlen(stComponents[i]->name) + 1);
00276                             strcpy(currentName->name, stComponents[i]->name);
00277                             *(currentName->name + strlen(currentName->name)) = '\0';
00278 
00279                             DEBUG(DEB_LEV_PARAMS, "Found component %s version=%d.%d.%d.%d in shared object %s\n",
00280                                 stComponents[i]->name,
00281                                 stComponents[i]->componentVersion.s.nVersionMajor,
00282                                 stComponents[i]->componentVersion.s.nVersionMinor,
00283                                 stComponents[i]->componentVersion.s.nRevision,
00284                                 stComponents[i]->componentVersion.s.nStep,
00285                                 lib_absolute_path);
00286                             if (verbose) {
00287                                 printf("Component %s registered with %i quality levels\n", stComponents[i]->name, (int)stComponents[i]->nqualitylevels);
00288                             }
00289                             if (stComponents[i]->nqualitylevels > 0) {
00290                                 index_string = 0;
00291                                 sprintf((qualityString + index_string), "%i ", (int)stComponents[i]->nqualitylevels);
00292                                 index_string = index_string + int2strlen(stComponents[i]->nqualitylevels) + 1;
00293                                 for (qi=0; qi<stComponents[i]->nqualitylevels; qi++) {
00294                                     sprintf((qualityString + index_string), "%i,%i ",
00295                                             stComponents[i]->multiResourceLevel[qi]->CPUResourceRequested,
00296                                             stComponents[i]->multiResourceLevel[qi]->MemoryResourceRequested);
00297                                     index_string = index_string + 2 +
00298                                         int2strlen(stComponents[i]->multiResourceLevel[qi]->CPUResourceRequested) +
00299                                         int2strlen(stComponents[i]->multiResourceLevel[qi]->MemoryResourceRequested);
00300                                 }
00301                                 index_string--;
00302                                 *(qualityString + index_string) = '\0';
00303                             }
00304                             // insert first of all the name of the library
00305                             strcpy(buffer, arrow);
00306                             strcat(buffer, stComponents[i]->name);
00307                             if (stComponents[i]->name_specific_length>0) {
00308                                 nroles += stComponents[i]->name_specific_length;
00309                                 strcat(buffer, arrow);
00310                                 for(j=0;j<stComponents[i]->name_specific_length;j++){
00311                                     if (verbose) {
00312                                         printf("  Specific role %s registered\n", stComponents[i]->name_specific[j]);
00313                                     }
00314                                     strcat(buffer, stComponents[i]->name_specific[j]);
00315                                     strcat(buffer, ":");
00316                                 }
00317                             }
00318 
00319                             if ((qualityString != NULL) && (qualityString[0] != '\0')) {
00320                                 strcat(buffer, arrow);
00321                                 strcat(buffer, qualityString);
00322                             }
00323                             qualityString[0] = '\0';
00324                             strcat(buffer, "\n");
00325                             fwrite(buffer, 1, strlen(buffer), omxregistryfp);
00326                             ncomponents++;
00327                         }
00328                         for (i = 0; i < num_of_comp; i++) {
00329                             free(stComponents[i]->name);
00330                             for (k=0; k<stComponents[i]->name_specific_length; k++) {
00331                                 free(stComponents[i]->name_specific[k]);
00332                                 free(stComponents[i]->role_specific[k]);
00333                             }
00334                             if (stComponents[i]->name_specific_length > 0) {
00335                                 free(stComponents[i]->name_specific);
00336                                 free(stComponents[i]->role_specific);
00337                             }
00338                             for (k=0; k<stComponents[i]->nqualitylevels; k++) {
00339                                 free(stComponents[i]->multiResourceLevel[k]);
00340                             }
00341                             if (stComponents[i]->multiResourceLevel) {
00342                                 free(stComponents[i]->multiResourceLevel);
00343                             }
00344                             free(stComponents[i]);
00345                         }
00346                         free(stComponents);
00347                     }
00348                 }
00349             }
00350         }
00351         free(actual);
00352         closedir(dirp);
00353     }
00354     if (verbose) {
00355         printf("\n %i OpenMAX IL ST static components in %i libraries succesfully scanned\n", ncomponents, num_of_libraries);
00356     } else {
00357         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);
00358     }
00359     free(qualityString);
00360     free(buffer);
00361     return 0;
00362 }
00363 
00364 static void usage(const char *app) {
00365     char *registry_filename;
00366     registry_filename = componentsRegistryGetFilename();
00367 
00368     printf(
00369       "Usage: %s [-l] [-v] [-h] [componentspath[:other_components_path]]...\n"
00370       "\n"
00371       "Version 0.9.2\n"
00372       "\n"
00373       "This programs scans for a given list of directory searching for any OpenMAX\n"
00374       "component compatible with the ST static component loader.\n"
00375             "The registry is saved under %s. (can be changed via OMX_BELLAGIO_REGISTRY\n"
00376             "environment variable)\n"
00377       "\n"
00378       "The following options are supported:\n"
00379       "\n"
00380       "        -v   display a verbose output, listing all the components registered\n"
00381       "        -l   list only the components already registered. If -l is specified \n"
00382       "             all the other parameters are ignored and only the register file\n"
00383       "             is checked\n"
00384       "        -h   display this message\n"
00385       "\n"
00386       "         componentspath: a searching path for components can be specified.\n"
00387       "         If this parameter is omitted, the components are searched in the\n"
00388       "         locations specified by the environment variable BELLAGIO_SEARCH_PATH.If it \n"
00389       "         is not defined the components are searched in the default %s directory \n"
00390       "\n",
00391             app, registry_filename, OMXILCOMPONENTSPATH);
00392 
00393   free(registry_filename);
00394 }
00395 
00401 int main(int argc, char *argv[]) {
00402     int found;
00403     int err, i;
00404     int verbose=0;
00405     FILE *omxregistryfp;
00406     char *registry_filename;
00407     char *dir,*dirp;
00408     char *buffer;
00409     int isListOnly = 0;
00410 
00411     for(i = 1; i < argc; i++) {
00412         if(*(argv[i]) != '-') {
00413             continue;
00414         }
00415         if (*(argv[i]+1) == 'v') {
00416             verbose = 1;
00417         } else if (*(argv[i]+1) == 'l') {
00418             isListOnly = 1;
00419         } else {
00420             usage(argv[0]);
00421             exit(*(argv[i]+1) == 'h' ? 0 : -EINVAL);
00422         }
00423     }
00424 
00425     registry_filename = componentsRegistryGetFilename();
00426 
00427     /* make sure the registry directory exists */
00428     dir = strdup(registry_filename);
00429     if (dir == NULL) {
00430         exit(EXIT_FAILURE);
00431     }
00432     dirp = strrchr(dir, '/');
00433     if (dirp != NULL) {
00434         *dirp = '\0';
00435         if (makedir(dir)) {
00436             DEBUG(DEB_LEV_ERR, "Cannot create OpenMAX registry directory %s\n", dir);
00437             exit(EXIT_FAILURE);
00438         }
00439     }
00440     free(dir);
00441 
00442     if (isListOnly) {
00443         omxregistryfp = fopen(registry_filename, "r");
00444     } else {
00445         omxregistryfp = fopen(registry_filename, "w");
00446     }
00447     if (omxregistryfp == NULL){
00448         DEBUG(DEB_LEV_ERR, "Cannot open OpenMAX registry file %s\n", registry_filename);
00449         exit(EXIT_FAILURE);
00450     }
00451 
00452     free(registry_filename);
00453     if (isListOnly) {
00454         err = showComponentsList(omxregistryfp);
00455         if(err) {
00456             DEBUG(DEB_LEV_ERR, "Error reading omxregister file\n");
00457         }
00458         exit(0);
00459     }
00460 
00461     for(i = 1, found = 0; i < argc; i++) {
00462         if(*(argv[i]) == '-') {
00463             continue;
00464         }
00465 
00466         found = 1;
00467         err = buildComponentsList(omxregistryfp, argv[i], verbose);
00468         if(err) {
00469             DEBUG(DEB_LEV_ERR, "Error registering OpenMAX components with ST static component loader %s\n", strerror(err));
00470             continue;
00471         }
00472     }
00473 
00474     if (found == 0) {
00475         buffer=getenv("BELLAGIO_SEARCH_PATH");
00476         if (buffer!=NULL&&*buffer!='\0') {
00477             err = buildComponentsList(omxregistryfp, buffer, verbose);
00478             if(err) {
00479                 DEBUG(DEB_LEV_ERR, "Error registering OpenMAX components with ST static component loader %s\n", strerror(err));
00480             }
00481         } else {
00482             err = buildComponentsList(omxregistryfp, OMXILCOMPONENTSPATH, verbose);
00483             if(err) {
00484                 DEBUG(DEB_LEV_ERR, "Error registering OpenMAX components with ST static component loader %s\n", strerror(err));
00485             }
00486         }
00487     }
00488 
00489     fclose(omxregistryfp);
00490 
00491     return 0;
00492 }