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         int err;
00176     nameList *allNames = NULL;
00177     nameList *currentName = NULL;
00178     nameList *tempName = NULL;
00179     char* qualityString = NULL;
00180     int index_string;
00181     /* the componentpath contains a single or multiple directories
00182      * and is is colon separated like env variables in Linux
00183      */
00184 
00185     qualityString = malloc(4096);
00186     buffer = malloc(8192);
00187     while (!pathconsumed) {
00188         index = 0;
00189         currentgiven = 0;
00190         while (!currentgiven) {
00191             if (*(currentpath + index) == '\0') {
00192                 pathconsumed = 1;
00193             }
00194             if ((*(currentpath + index) == ':') || (*(currentpath + index) =='\0')) {
00195                 currentgiven = 1;
00196                 if (*(currentpath + index - 1) != '/') {
00197                     actual = malloc(index + 2);
00198                     *(actual + index) = '/';
00199                     *(actual+index + 1) = '\0';
00200                 } else {
00201                     actual = malloc(index + 1);
00202                     *(actual+index) = '\0';
00203                 }
00204                 strncpy(actual, currentpath, index);
00205                 currentpath = currentpath + index + 1;
00206             }
00207             index++;
00208         }
00209         /* Populate the registry file */
00210         dirp = opendir(actual);
00211         if (verbose) {
00212             printf("\n Scanning directory %s\n", actual);
00213         }
00214         if(dirp == NULL){
00215             free(actual);
00216             DEBUG(DEB_LEV_SIMPLE_SEQ, "Cannot open directory %s\n", actual);
00217             continue;
00218         }
00219         while((dp = readdir(dirp)) != NULL) {
00220             int len = strlen(dp->d_name);
00221 
00222             if(len >= 3){
00223 
00224 
00225                 if(strncmp(dp->d_name+len-3, ".so", 3) == 0){
00226                     char lib_absolute_path[strlen(actual) + len + 1];
00227 
00228                     strcpy(lib_absolute_path, actual);
00229                     strcat(lib_absolute_path, dp->d_name);
00230 
00231                     if((handle = dlopen(lib_absolute_path, RTLD_NOW)) == NULL) {
00232                         DEBUG(DEB_LEV_ERR, "could not load %s: %s\n", lib_absolute_path, dlerror());
00233                     } else {
00234                         if (verbose) {
00235                             printf("\n Scanning library %s\n", lib_absolute_path);
00236                         }
00237                         if ((fptr = dlsym(handle, "omx_component_library_Setup")) == NULL) {
00238                             DEBUG(DEB_LEV_SIMPLE_SEQ, "the library %s is not compatible with ST static component loader - %s\n", lib_absolute_path, dlerror());
00239                             continue;
00240                         }
00241                         num_of_libraries++;
00242                         num_of_comp = fptr(NULL);
00243                         stComponents = malloc(num_of_comp * sizeof(stLoaderComponentType*));
00244                         for (i = 0; i<num_of_comp; i++) {
00245                             stComponents[i] = calloc(1,sizeof(stLoaderComponentType));
00246                             stComponents[i]->nqualitylevels = 0;
00247                             stComponents[i]->multiResourceLevel = NULL;
00248                         }
00249                         fptr(stComponents);
00250                         err = fwrite(lib_absolute_path, 1, strlen(lib_absolute_path), omxregistryfp);
00251                         err = fwrite("\n", 1, 1, omxregistryfp);
00252 
00253 
00254                         for (i = 0; i<num_of_comp; i++) {
00255                             tempName = allNames;
00256                             if (tempName != NULL) {
00257                                 do  {
00258                                     if (!strcmp(tempName->name, stComponents[i]->name)) {
00259                                         DEBUG(DEB_LEV_ERR, "Component %s already registered. Skip\n", stComponents[i]->name);
00260                                         break;
00261                                     }
00262                                     tempName = tempName->next;
00263                                 } while(tempName != NULL);
00264                                 if (tempName != NULL) {
00265                                     continue;
00266                                 }
00267                             }
00268                             if (allNames == NULL) {
00269                                 allNames = malloc(sizeof(nameList));
00270                                 currentName = allNames;
00271                             } else {
00272                                 currentName->next = malloc(sizeof(nameList));
00273                                 currentName = currentName->next;
00274                             }
00275                             currentName->next = NULL;
00276                             currentName->name = malloc(strlen(stComponents[i]->name) + 1);
00277                             strcpy(currentName->name, stComponents[i]->name);
00278                             *(currentName->name + strlen(currentName->name)) = '\0';
00279 
00280                             DEBUG(DEB_LEV_PARAMS, "Found component %s version=%d.%d.%d.%d in shared object %s\n",
00281                                 stComponents[i]->name,
00282                                 stComponents[i]->componentVersion.s.nVersionMajor,
00283                                 stComponents[i]->componentVersion.s.nVersionMinor,
00284                                 stComponents[i]->componentVersion.s.nRevision,
00285                                 stComponents[i]->componentVersion.s.nStep,
00286                                 lib_absolute_path);
00287                             if (verbose) {
00288                                 printf("Component %s registered with %i quality levels\n", stComponents[i]->name, (int)stComponents[i]->nqualitylevels);
00289                             }
00290                             if (stComponents[i]->nqualitylevels > 0) {
00291                                 index_string = 0;
00292                                 sprintf((qualityString + index_string), "%i ", (int)stComponents[i]->nqualitylevels);
00293                                 index_string = index_string + int2strlen(stComponents[i]->nqualitylevels) + 1;
00294                                 for (qi=0; qi<stComponents[i]->nqualitylevels; qi++) {
00295                                     sprintf((qualityString + index_string), "%i,%i ",
00296                                             stComponents[i]->multiResourceLevel[qi]->CPUResourceRequested,
00297                                             stComponents[i]->multiResourceLevel[qi]->MemoryResourceRequested);
00298                                     index_string = index_string + 2 +
00299                                         int2strlen(stComponents[i]->multiResourceLevel[qi]->CPUResourceRequested) +
00300                                         int2strlen(stComponents[i]->multiResourceLevel[qi]->MemoryResourceRequested);
00301                                 }
00302                                 index_string--;
00303                                 *(qualityString + index_string) = '\0';
00304                             }
00305                             // insert first of all the name of the library
00306                             strcpy(buffer, arrow);
00307                             strcat(buffer, stComponents[i]->name);
00308                             if (stComponents[i]->name_specific_length>0) {
00309                                 nroles += stComponents[i]->name_specific_length;
00310                                 strcat(buffer, arrow);
00311                                 for(j=0;j<stComponents[i]->name_specific_length;j++){
00312                                     if (verbose) {
00313                                         printf("  Specific role %s registered\n", stComponents[i]->name_specific[j]);
00314                                     }
00315                                     strcat(buffer, stComponents[i]->name_specific[j]);
00316                                     strcat(buffer, ":");
00317                                 }
00318                             }
00319 
00320                             if ((qualityString != NULL) && (qualityString[0] != '\0')) {
00321                                 strcat(buffer, arrow);
00322                                 strcat(buffer, qualityString);
00323                             }
00324                             qualityString[0] = '\0';
00325                             strcat(buffer, "\n");
00326                             err = fwrite(buffer, 1, strlen(buffer), omxregistryfp);
00327                             ncomponents++;
00328                         }
00329                         for (i = 0; i < num_of_comp; i++) {
00330                             free(stComponents[i]->name);
00331                             for (k=0; k<stComponents[i]->name_specific_length; k++) {
00332                                 free(stComponents[i]->name_specific[k]);
00333                                 free(stComponents[i]->role_specific[k]);
00334                             }
00335                             if (stComponents[i]->name_specific_length > 0) {
00336                                 free(stComponents[i]->name_specific);
00337                                 free(stComponents[i]->role_specific);
00338                             }
00339                             for (k=0; k<stComponents[i]->nqualitylevels; k++) {
00340                                 free(stComponents[i]->multiResourceLevel[k]);
00341                             }
00342                             if (stComponents[i]->multiResourceLevel) {
00343                                 free(stComponents[i]->multiResourceLevel);
00344                             }
00345                             free(stComponents[i]);
00346                         }
00347                         free(stComponents);
00348                     }
00349                 }
00350             }
00351         }
00352         free(actual);
00353         closedir(dirp);
00354     }
00355     if (verbose) {
00356         printf("\n %i OpenMAX IL ST static components in %i libraries succesfully scanned\n", ncomponents, num_of_libraries);
00357     } else {
00358         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);
00359     }
00360     free(qualityString);
00361     free(buffer);
00362     return 0;
00363 }
00364 
00365 static void usage(const char *app) {
00366     char *registry_filename;
00367     registry_filename = componentsRegistryGetFilename();
00368 
00369     printf(
00370       "Usage: %s [-l] [-v] [-h] [componentspath[:other_components_path]]...\n"
00371       "\n"
00372       "Version 0.9.2\n"
00373       "\n"
00374       "This programs scans for a given list of directory searching for any OpenMAX\n"
00375       "component compatible with the ST static component loader.\n"
00376             "The registry is saved under %s. (can be changed via OMX_BELLAGIO_REGISTRY\n"
00377             "environment variable)\n"
00378       "\n"
00379       "The following options are supported:\n"
00380       "\n"
00381       "        -v   display a verbose output, listing all the components registered\n"
00382       "        -l   list only the components already registered. If -l is specified \n"
00383       "             all the other parameters are ignored and only the register file\n"
00384       "             is checked\n"
00385       "        -h   display this message\n"
00386       "\n"
00387       "         componentspath: a searching path for components can be specified.\n"
00388       "         If this parameter is omitted, the components are searched in the\n"
00389       "         locations specified by the environment variable BELLAGIO_SEARCH_PATH.If it \n"
00390       "         is not defined the components are searched in the default %s directory \n"
00391       "\n",
00392             app, registry_filename, OMXILCOMPONENTSPATH);
00393 
00394   free(registry_filename);
00395 }
00396 
00402 int main(int argc, char *argv[]) {
00403     int found;
00404     int err, i;
00405     int verbose=0;
00406     FILE *omxregistryfp;
00407     char *registry_filename;
00408     char *dir,*dirp;
00409     char *buffer;
00410     int isListOnly = 0;
00411 
00412     for(i = 1; i < argc; i++) {
00413         if(*(argv[i]) != '-') {
00414             continue;
00415         }
00416         if (*(argv[i]+1) == 'v') {
00417             verbose = 1;
00418         } else if (*(argv[i]+1) == 'l') {
00419             isListOnly = 1;
00420         } else {
00421             usage(argv[0]);
00422             exit(*(argv[i]+1) == 'h' ? 0 : -EINVAL);
00423         }
00424     }
00425 
00426     registry_filename = componentsRegistryGetFilename();
00427 
00428     /* make sure the registry directory exists */
00429     dir = strdup(registry_filename);
00430     if (dir == NULL) {
00431         exit(EXIT_FAILURE);
00432     }
00433     dirp = strrchr(dir, '/');
00434     if (dirp != NULL) {
00435         *dirp = '\0';
00436         if (makedir(dir)) {
00437             DEBUG(DEB_LEV_ERR, "Cannot create OpenMAX registry directory %s\n", dir);
00438             exit(EXIT_FAILURE);
00439         }
00440     }
00441     free(dir);
00442 
00443     if (isListOnly) {
00444         omxregistryfp = fopen(registry_filename, "r");
00445     } else {
00446         omxregistryfp = fopen(registry_filename, "w");
00447     }
00448     if (omxregistryfp == NULL){
00449         DEBUG(DEB_LEV_ERR, "Cannot open OpenMAX registry file %s\n", registry_filename);
00450         exit(EXIT_FAILURE);
00451     }
00452 
00453     free(registry_filename);
00454     if (isListOnly) {
00455         err = showComponentsList(omxregistryfp);
00456         if(err) {
00457             DEBUG(DEB_LEV_ERR, "Error reading omxregister file\n");
00458         }
00459         exit(0);
00460     }
00461 
00462     for(i = 1, found = 0; i < argc; i++) {
00463         if(*(argv[i]) == '-') {
00464             continue;
00465         }
00466 
00467         found = 1;
00468         err = buildComponentsList(omxregistryfp, argv[i], verbose);
00469         if(err) {
00470             DEBUG(DEB_LEV_ERR, "Error registering OpenMAX components with ST static component loader %s\n", strerror(err));
00471             continue;
00472         }
00473     }
00474 
00475     if (found == 0) {
00476         buffer=getenv("BELLAGIO_SEARCH_PATH");
00477         if (buffer!=NULL&&*buffer!='\0') {
00478             err = buildComponentsList(omxregistryfp, buffer, verbose);
00479             if(err) {
00480                 DEBUG(DEB_LEV_ERR, "Error registering OpenMAX components with ST static component loader %s\n", strerror(err));
00481             }
00482         } else {
00483             err = buildComponentsList(omxregistryfp, OMXILCOMPONENTSPATH, verbose);
00484             if(err) {
00485                 DEBUG(DEB_LEV_ERR, "Error registering OpenMAX components with ST static component loader %s\n", strerror(err));
00486             }
00487         }
00488     }
00489 
00490     fclose(omxregistryfp);
00491 
00492     return 0;
00493 }