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 
00053 static const char arrow[] =  " ==> ";
00054 
00063 static int buildComponentsList(FILE* omxregistryfp, char *componentspath, int verbose) {
00064   DIR *dirp;
00065     struct dirent *dp;
00066     void *handle;
00067     int i, num_of_comp;
00068     unsigned int j;
00069     char *buffer = NULL;
00070     int (*fptr)(void *);
00071     stLoaderComponentType **stComponents;
00072     int ncomponents = 0, nroles=0;
00073 
00074     /* Populate the registry file */
00075     dirp = opendir(componentspath);
00076     if(dirp == NULL){
00077     int err = errno;
00078         DEBUG(DEB_LEV_ERR, "Cannot open directory %s\n", componentspath);
00079         return err;
00080     }
00081 
00082     while((dp = readdir(dirp)) != NULL) {
00083     int len = strlen(dp->d_name);
00084 
00085         if(len >= 3){
00086 
00087 
00088       if(strncmp(dp->d_name+len-3, ".so", 3) == 0){
00089         char lib_absolute_path[strlen(componentspath) + len + 1];
00090 
00091                 strcpy(lib_absolute_path, componentspath);
00092                 strcat(lib_absolute_path, dp->d_name);
00093 
00094                 if((handle = dlopen(lib_absolute_path, RTLD_NOW)) == NULL) {
00095                     DEBUG(DEB_LEV_ERR, "could not load %s: %s\n", lib_absolute_path, dlerror());
00096                 } else {
00097                     if ((fptr = dlsym(handle, "omx_component_library_Setup")) == NULL) {
00098                         DEBUG(DEB_LEV_ERR, "the library %s is not compatible with ST static component loader - %s\n", lib_absolute_path, dlerror());
00099                         continue;
00100                     }
00101                     num_of_comp = fptr(NULL);
00102                     stComponents = malloc(num_of_comp * sizeof(stLoaderComponentType*));
00103                     for (i = 0; i<num_of_comp; i++) {
00104                         stComponents[i] = malloc(sizeof(stLoaderComponentType));
00105                     }
00106                     fptr(stComponents);
00107 
00108                     fwrite(lib_absolute_path, 1, strlen(lib_absolute_path), omxregistryfp);
00109                     fwrite("\n", 1, 1, omxregistryfp);
00110 
00111 
00112                     for (i = 0; i<num_of_comp; i++) {
00113                         DEBUG(DEB_LEV_PARAMS, "Found component %s version=%d.%d.%d.%d in shared object %s\n",
00114                                 stComponents[i]->name,
00115                                 stComponents[i]->componentVersion.s.nVersionMajor,
00116                                 stComponents[i]->componentVersion.s.nVersionMinor,
00117                                 stComponents[i]->componentVersion.s.nRevision,
00118                                 stComponents[i]->componentVersion.s.nStep,
00119                                 lib_absolute_path);
00120                         if (verbose)
00121               printf("Component %s registered\n", stComponents[i]->name);
00122 
00123                         // allocate max memory
00124             len = sizeof(arrow)                 /* arrow */
00125                  +strlen(stComponents[i]->name) /* component name */
00126                  +sizeof(arrow)                 /* arrow */
00127                  +1                             /* '\n' */
00128                  +1                             /* '\0' */;
00129             buffer = realloc(buffer, len);
00130 
00131             // insert first of all the name of the library
00132                         strcpy(buffer, arrow);
00133                         strcat(buffer, stComponents[i]->name);
00134 
00135                         if (stComponents[i]->name_specific_length>0) {
00136                             nroles += stComponents[i]->name_specific_length;
00137                             strcat(buffer, arrow);
00138                             for(j=0;j<stComponents[i]->name_specific_length;j++){
00139                                 if (verbose)
00140                   printf("  Specific role %s registered\n", stComponents[i]->name_specific[j]);
00141                 len += strlen(stComponents[i]->name_specific[j]) /* specific role */
00142                       +1                                         /* ':' */;
00143                 buffer = realloc(buffer, len);
00144                                 strcat(buffer, stComponents[i]->name_specific[j]);
00145                                 strcat(buffer, ":");
00146                             }
00147                         }
00148                         strcat(buffer, "\n");
00149                         fwrite(buffer, 1, strlen(buffer), omxregistryfp);
00150                         ncomponents++;
00151                     }
00152                     for (i = 0; i < num_of_comp; i++) {
00153                         free(stComponents[i]);
00154                     }
00155                     free(stComponents);
00156                 }
00157             }
00158         }
00159     }
00160     free(buffer);
00161     closedir(dirp);
00162 
00163     if (verbose) {
00164         printf("\n %i OpenMAX IL ST static components with %i roles succesfully scanned\n", ncomponents, nroles);
00165     } else {
00166         DEBUG(DEB_LEV_SIMPLE_SEQ, "\n %i OpenMAX IL ST static components with %i roles succesfully scanned\n", ncomponents, nroles);
00167     }
00168 
00169     return 0;
00170 }
00171 
00172 static void usage(const char *app) {
00173     char *registry_filename;
00174     registry_filename = registryGetFilename();
00175 
00176     printf(
00177       "Usage: %s [-v] [-h] [componentspath]...\n"
00178       "This programs scans for a given list of directory searching for any OpenMAX\n"
00179       "component compatible with the ST static component loader.\n"
00180             "The registry is saved under %s. (can be changed via OMX_BELLAGIO_REGISTRY\n"
00181             "environment variable)\n"
00182       "\n"
00183       "The following options are supported:\n"
00184       "\n"
00185       "        -v   display a verbose output, listing all the components registered\n"
00186       "        -h   display this message\n"
00187       "\n"
00188       "         componentspath: a searching path for components can be specified.\n"
00189       "         If this parameter is omitted, the components are searched in the\n"
00190       "         default %s directory\n"
00191       "\n",
00192             app, registry_filename, OMXILCOMPONENTSPATH);
00193 
00194   free(registry_filename);
00195 }
00196 
00197 static int makedir (const char *newdir)
00198 {
00199   char *buffer;
00200   char *p;
00201   int err;
00202   size_t len;
00203 
00204   buffer = strdup(newdir);
00205   len = strlen(buffer);
00206 
00207   if (len == 0) {
00208     free(buffer);
00209     return 1;
00210   }
00211   if (buffer[len-1] == '/') {
00212     buffer[len-1] = '\0';
00213   }
00214 
00215     err = mkdir(buffer, 0755);
00216     if (err == 0 || (( err == -1) && (errno == EEXIST))) {
00217         free(buffer);
00218         return 0;
00219     }
00220 
00221   p = buffer+1;
00222   while (1) {
00223         char hold;
00224 
00225         while(*p && *p != '\\' && *p != '/')
00226             p++;
00227         hold = *p;
00228         *p = 0;
00229         if ((mkdir(buffer, 0755) == -1) && (errno == ENOENT)) {
00230             free(buffer);
00231             return 1;
00232         }
00233         if (hold == 0)
00234             break;
00235         *p++ = hold;
00236     }
00237   free(buffer);
00238   return 0;
00239 }
00240 
00246 int main(int argc, char *argv[]) {
00247     int found;
00248     int err, i;
00249     int verbose=0;
00250     FILE *omxregistryfp;
00251     char *registry_filename;
00252     char *dir,*dirp;
00253 
00254     for(i = 1; i < argc; i++) {
00255         if(*(argv[i]) != '-') {
00256             continue;
00257         }
00258         if (*(argv[i]+1) == 'v') {
00259             verbose = 1;
00260         } else {
00261             usage(argv[0]);
00262             exit(*(argv[i]+1) == 'h' ? 0 : -EINVAL);
00263         }
00264   }
00265 
00266     registry_filename = registryGetFilename();
00267 
00268     /* make sure the registry directory exists */
00269     dir = strdup(registry_filename);
00270     if (dir == NULL)
00271         exit(EXIT_FAILURE);
00272     dirp = strrchr(dir, '/');
00273     if (dirp != NULL) {
00274         *dirp = '\0';
00275         if (makedir(dir)) {
00276             DEBUG(DEB_LEV_ERR, "Cannot create OpenMAX registry directory %s\n", dir);
00277             exit(EXIT_FAILURE);
00278         }
00279     }
00280     free (dir);
00281 
00282     omxregistryfp = fopen(registry_filename, "w");
00283     if (omxregistryfp == NULL){
00284         DEBUG(DEB_LEV_ERR, "Cannot open OpenMAX registry file %s\n", registry_filename);
00285         exit(EXIT_FAILURE);
00286     }
00287 
00288     free(registry_filename);
00289 
00290     for(i = 1, found = 0; i < argc; i++) {
00291         if(*(argv[i]) == '-') {
00292             continue;
00293         }
00294 
00295         found = 1;
00296         err = buildComponentsList(omxregistryfp, argv[i], verbose);
00297         if(err) {
00298             DEBUG(DEB_LEV_ERR, "Error registering OpenMAX components with ST static component loader %s\n", strerror(err));
00299             continue;
00300         }
00301     }
00302 
00303     if (found == 0) {
00304         err = buildComponentsList(omxregistryfp, OMXILCOMPONENTSPATH, verbose);
00305         if(err) {
00306             DEBUG(DEB_LEV_ERR, "Error registering OpenMAX components with ST static component loader %s\n", strerror(err));
00307         }
00308     }
00309 
00310     fclose(omxregistryfp);
00311 
00312     return 0;
00313 }

Generated for OpenMAX Bellagio rel. 0.3.5-svn by  doxygen 1.5.1
SourceForge.net Logo