khronosfws/openmax_al/src/common/xadynintmgmtitf.c
changeset 12 5a06f39ad45b
child 25 6f7ceef7b1d1
equal deleted inserted replaced
0:71ca22bcf22a 12:5a06f39ad45b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include <stdlib.h>
       
    19 #include <assert.h>
       
    20 
       
    21 #include "openmaxalwrapper.h"
       
    22 #include "xadynintmgmtitf.h"
       
    23 #include "xadebug.h"
       
    24 
       
    25 /* XADIMItfImpl* GetImpl(XADIMItf self)
       
    26  * Description: Validate interface pointer and cast it to implementation pointer.
       
    27  */
       
    28 static XADIMItfImpl* GetImpl(XADynamicInterfaceManagementItf self)
       
    29 {
       
    30     if( self )
       
    31     {
       
    32         XADIMItfImpl* impl = (XADIMItfImpl*)(*self);
       
    33         if( impl && (impl == impl->self) )
       
    34         {
       
    35             return impl;
       
    36         }
       
    37     }
       
    38     return NULL;
       
    39 }
       
    40 
       
    41 /**
       
    42  * Base Interface XADynamicInterfaceManagementItf implementation
       
    43  **/
       
    44 
       
    45 /* XAresult XADIMItfImpl_AddInterface
       
    46  * Optionally asynchronous method for exposing an interface on an object.
       
    47  */
       
    48 XAresult XADIMItfImpl_AddInterface(XADynamicInterfaceManagementItf self,
       
    49                                    const XAInterfaceID iid,
       
    50                                    XAboolean async)
       
    51 {
       
    52     XADIMItfImpl* impl = GetImpl(self);
       
    53     XAresult retval = XA_RESULT_SUCCESS;
       
    54     DEBUG_API("->XADIMItfImpl_AddInterface");
       
    55     if( !impl )
       
    56     {
       
    57         DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
       
    58         DEBUG_API("<-XADIMItfImpl_AddInterface");
       
    59         return XA_RESULT_PARAMETER_INVALID;
       
    60     }
       
    61 
       
    62     if( async )
       
    63     {
       
    64         /* invoke asynchronously */
       
    65         /* check if another async operation is processed */
       
    66         if(impl->asyncOngoing)
       
    67         {
       
    68             /* abort ongoing operation */
       
    69             XAImpl_CancelThread(impl->asyncThr);
       
    70         }
       
    71         impl->asyncOngoing = XA_BOOLEAN_TRUE;
       
    72         impl->asyncID = iid;
       
    73         if(XAImpl_StartThread(impl->asyncThr, NULL,
       
    74                 &XADIMItfImpl_AsyncAddItf,(void*)(self)))
       
    75         {
       
    76             impl->asyncOngoing = XA_BOOLEAN_FALSE;
       
    77             retval = XA_RESULT_RESOURCE_ERROR;
       
    78         }
       
    79         else
       
    80         {
       
    81             retval = XA_RESULT_SUCCESS;
       
    82         }
       
    83     }
       
    84     else
       
    85     {
       
    86         /* invoke synchronously */
       
    87         retval = XADIMItfImpl_DoAddItf(impl, iid);
       
    88     }
       
    89 
       
    90     DEBUG_API_A1("<-XADIMItfImpl_AddInterface %lu", retval);
       
    91     return retval;
       
    92 }
       
    93 
       
    94 /* XAresult XADIMItfImpl_RemoveInterface
       
    95  * Synchronous method for removing a dynamically exposed interface on an object.
       
    96  */
       
    97 XAresult XADIMItfImpl_RemoveInterface(XADynamicInterfaceManagementItf self,
       
    98                                           const XAInterfaceID iid)
       
    99 {
       
   100     XADIMItfImpl* impl = GetImpl(self);
       
   101     XAresult retval = XA_RESULT_SUCCESS;
       
   102     DEBUG_API("->XADIMItfImpl_RemoveInterface");
       
   103 
       
   104     if( impl )
       
   105     {
       
   106         XAObjItfMapEntry *mapEntry = NULL;
       
   107         if(impl->parent)
       
   108         {
       
   109             mapEntry = XAObjectItfImpl_GetItfEntry(impl->parent, iid);
       
   110         }
       
   111         if( mapEntry && mapEntry->isDynamic )
       
   112         {
       
   113             if( (mapEntry->pItf) )
       
   114             {
       
   115                 if( impl->DoRemoveItfImpl )
       
   116                 {
       
   117                     retval = impl->DoRemoveItfImpl(impl->parent, mapEntry);
       
   118                 }
       
   119                 else
       
   120                 {
       
   121                     DEBUG_ERR("XADIMItfImpl_RemoveInterface: Object has specified dynamic itfs but not implemented DoRemoveItfImpl!!!");
       
   122                     retval = XA_RESULT_INTERNAL_ERROR;
       
   123                 }
       
   124             }
       
   125             else
       
   126             {
       
   127                 DEBUG_ERR("XADIMItfImpl_RemoveInterface interface not exposed");
       
   128                 retval = XA_RESULT_PRECONDITIONS_VIOLATED;
       
   129             }
       
   130         }
       
   131         else /*( mapEntry && mapEntry->isDynamic )*/
       
   132         {   /* interface is not supported for dynamic management */
       
   133             DEBUG_ERR("XADIMItfImpl_RemoveInterface interface not supported");
       
   134             retval = XA_RESULT_FEATURE_UNSUPPORTED;
       
   135         }
       
   136     }
       
   137     else
       
   138     {
       
   139         retval = XA_RESULT_PARAMETER_INVALID;
       
   140         DEBUG_ERR("XADIMItfImpl_RemoveInterface: INVALID args");
       
   141     }
       
   142 
       
   143     DEBUG_API_A1("<-XADIMItfImpl_RemoveInterface %lu", retval);
       
   144     return retval;
       
   145 }
       
   146 
       
   147 /* XAresult XADIMItfImpl_ResumeInterface
       
   148  * Optionally asynchronous method for resuming a dynamically exposed interface on the object.
       
   149  */
       
   150 XAresult XADIMItfImpl_ResumeInterface(XADynamicInterfaceManagementItf self,
       
   151                                       const XAInterfaceID iid,
       
   152                                       XAboolean async)
       
   153 {
       
   154     XADIMItfImpl* impl = GetImpl(self);
       
   155     XAresult retval = XA_RESULT_SUCCESS;
       
   156     DEBUG_API("->XADIMItfImpl_ResumeInterface");
       
   157 
       
   158     if( !impl )
       
   159     {
       
   160         DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
       
   161         DEBUG_API("<-XADIMItfImpl_ResumeInterface");
       
   162         return XA_RESULT_PARAMETER_INVALID;
       
   163     }
       
   164 
       
   165     if( async )
       
   166     {
       
   167         /* invoke asynchronously */
       
   168         /* check if another async operation is processed */
       
   169         if(impl->asyncOngoing)
       
   170         {
       
   171             /* abort ongoing operation */
       
   172             XAImpl_CancelThread(impl->asyncThr);
       
   173         }
       
   174         impl->asyncOngoing = XA_BOOLEAN_TRUE;
       
   175         impl->asyncID = iid;
       
   176         if(XAImpl_StartThread(impl->asyncThr, NULL,
       
   177                 &XADIMItfImpl_AsyncResumeItf,(void*)(self)))
       
   178         {
       
   179             impl->asyncOngoing = XA_BOOLEAN_FALSE;
       
   180             retval = XA_RESULT_RESOURCE_ERROR;
       
   181         }
       
   182         else
       
   183         {
       
   184             retval = XA_RESULT_SUCCESS;
       
   185         }
       
   186     }
       
   187     else
       
   188     {
       
   189         /* invoke synchronously */
       
   190         retval = XADIMItfImpl_DoResumeItf(impl, iid);
       
   191     }
       
   192 
       
   193     DEBUG_API_A1("<-XADIMItfImpl_ResumeInterface %lu", retval);
       
   194     return retval;
       
   195 }
       
   196 
       
   197 /* XAresult XADIMItfImpl_RegisterCallback
       
   198  * Registers a callback on the object
       
   199  */
       
   200 XAresult XADIMItfImpl_RegisterCallback(XADynamicInterfaceManagementItf self,
       
   201                                        xaDynamicInterfaceManagementCallback callback,
       
   202                                        void * pContext)
       
   203 {
       
   204     XADIMItfImpl* impl = GetImpl(self);
       
   205     DEBUG_API("->XADIMItfImpl_RegisterCallback");
       
   206 
       
   207     if( !impl )
       
   208     {
       
   209         DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
       
   210         DEBUG_API("<-XADIMItfImpl_RegisterCallback");
       
   211         return XA_RESULT_PARAMETER_INVALID;
       
   212     }
       
   213     impl->dimCb = callback;
       
   214     impl->dimCbContext = pContext;
       
   215     impl->dimCbPtrToSelf = self;
       
   216 
       
   217     DEBUG_API("<-XADIMItfImpl_RegisterCallback");
       
   218     return XA_RESULT_SUCCESS;
       
   219 }
       
   220 
       
   221 /**
       
   222  * XADIMItfImpl -specific methods
       
   223  **/
       
   224 
       
   225 /* XADIMItfImpl* XADIMItfImpl_Create()
       
   226  * Description: Allocate and initialize DIMItfImpl
       
   227  */
       
   228 XADIMItfImpl* XADIMItfImpl_Create()
       
   229 {
       
   230     XADIMItfImpl *self = (XADIMItfImpl*)calloc(1,sizeof(XADIMItfImpl));
       
   231     DEBUG_API("->XADIMItfImpl_Create");
       
   232 
       
   233     if( self )
       
   234     {
       
   235         /* init itf default implementation */
       
   236         self->itf.AddInterface = XADIMItfImpl_AddInterface;
       
   237         self->itf.RemoveInterface = XADIMItfImpl_RemoveInterface;
       
   238         self->itf.ResumeInterface = XADIMItfImpl_ResumeInterface;
       
   239         self->itf.RegisterCallback = XADIMItfImpl_RegisterCallback;
       
   240 
       
   241         /* init variables */
       
   242         self->asyncOngoing = XA_BOOLEAN_FALSE;
       
   243         self->dimCbPtrToSelf = NULL;
       
   244 
       
   245         self->self = self;
       
   246         if( XAImpl_CreateThreadHandle(&(self->asyncThr)) != XA_RESULT_SUCCESS )
       
   247         {
       
   248             free(self);
       
   249             self = NULL;
       
   250         }
       
   251     }
       
   252     DEBUG_API("<-XADIMItfImpl_Create");
       
   253     return self;
       
   254 }
       
   255 
       
   256 /* void XADIMItfImpl_Free(XADIMItfImpl* self)
       
   257  * Description: Free all resources reserved at XADIMItfImpl_Create
       
   258  */
       
   259 void XADIMItfImpl_Free(XADIMItfImpl* self)
       
   260 {
       
   261     DEBUG_API("->XADIMItfImpl_Free");
       
   262     assert(self==self->self);
       
   263     XAImpl_DeleteThreadHandle(self->asyncThr);
       
   264     free(self);
       
   265     DEBUG_API("<-XADIMItfImpl_Free");
       
   266 }
       
   267 
       
   268 /* void XADIMItfImpl_Init
       
   269  * Description: Initialize DIM interface. Mandatory to call if dynamic
       
   270  * interfaces are supported by object.
       
   271  */
       
   272 void XADIMItfImpl_Init( XADIMItfImpl* self,
       
   273                            XAObjectItf parent,
       
   274                            xaDoAddItfImpl doAdd,
       
   275                            xaDoResumeItfImpl doResume,
       
   276                            xaDoRemoveItfImpl doRemove)
       
   277 {
       
   278     DEBUG_API("->XADIMItfImpl_Init");
       
   279 
       
   280     assert(self==self->self && parent && doAdd && doResume && doRemove);
       
   281     self->parent = parent;
       
   282     self->DoAddItfImpl = doAdd;
       
   283     self->DoResumeItfImpl = doResume;
       
   284     self->DoRemoveItfImpl = doRemove;
       
   285 
       
   286     DEBUG_API("<-XADIMItfImpl_Init");
       
   287 }
       
   288 
       
   289 /* void XADIMItfImpl_SendCbEvent
       
   290  * Description: Object can use this method to send resource events
       
   291  */
       
   292 void XADIMItfImpl_SendCbEvent( XADIMItfImpl* self,
       
   293                                    XAuint32 event,
       
   294                                    XAresult result,
       
   295                                    const XAInterfaceID iid )
       
   296 {
       
   297     DEBUG_API("->XADIMItfImpl_SendCbEvent");
       
   298     if( self->dimCb )
       
   299      {
       
   300         self->dimCb((XADynamicInterfaceManagementItf)(self), self->dimCbContext,
       
   301                       event, result, iid);
       
   302      }
       
   303     DEBUG_API("<-XADIMItfImpl_SendCbEvent");
       
   304 }
       
   305 
       
   306 /* void* XADIMItfImpl_AsyncAddItf
       
   307  * Description: Launch AddItf from async thread.
       
   308  */
       
   309 void* XADIMItfImpl_AsyncAddItf(void* args)
       
   310 {
       
   311     XAresult retval = XA_RESULT_SUCCESS;
       
   312     XADIMItfImpl* impl = GetImpl(args);
       
   313     DEBUG_API("->XADIMItfImpl_AsyncAddItf");
       
   314 
       
   315     if( impl )
       
   316     {
       
   317         retval = XADIMItfImpl_DoAddItf(impl, impl->asyncID);
       
   318         if( impl->dimCb )
       
   319         {
       
   320             impl->dimCb((XADynamicInterfaceManagementItf)(args), impl->dimCbContext,
       
   321                          XA_DYNAMIC_ITF_EVENT_ASYNC_TERMINATION, retval, impl->asyncID);
       
   322         }
       
   323         impl->asyncOngoing = XA_BOOLEAN_FALSE;
       
   324         XAImpl_ExitThread(impl->asyncThr);
       
   325     }
       
   326     else
       
   327     {
       
   328         DEBUG_ERR("XADIMItfImpl_AsyncAddItf: INVALID args");
       
   329     }
       
   330     
       
   331 
       
   332     DEBUG_API("<-XADIMItfImpl_AsyncAddItf");
       
   333     return NULL;
       
   334 }
       
   335 
       
   336 /* XAresult XADIMItfImpl_DoAddItf
       
   337  * Description: Handle Itf dynamic addition and call object-specific parts.
       
   338  *              Launched either from async thread or directly.
       
   339  */
       
   340 XAresult XADIMItfImpl_DoAddItf(XADIMItfImpl* impl,
       
   341                                 const XAInterfaceID iid)
       
   342 {
       
   343     XAresult retval = XA_RESULT_SUCCESS;
       
   344     DEBUG_API("->XADIMItfImpl_DoAddItf");
       
   345 
       
   346     if( impl )
       
   347     {
       
   348         XAObjItfMapEntry *mapEntry = NULL;
       
   349         if(impl->parent)
       
   350         {
       
   351             mapEntry = XAObjectItfImpl_GetItfEntry(impl->parent, iid);
       
   352         }
       
   353         if( mapEntry && mapEntry->isDynamic )
       
   354         {
       
   355             if( !(mapEntry->pItf) )
       
   356             {
       
   357                 if( impl->DoAddItfImpl )
       
   358                 {
       
   359                     retval = impl->DoAddItfImpl(impl->parent, mapEntry);
       
   360                 }
       
   361                 else
       
   362                 {
       
   363                     DEBUG_ERR("XADIMItfImpl_DoAddItf: Object has specified dynamic itfs but not implemented DoAddItfImpl!!!");
       
   364                     retval = XA_RESULT_INTERNAL_ERROR;
       
   365                 }
       
   366             }
       
   367             else
       
   368             {
       
   369                 DEBUG_ERR("XADIMItfImpl_DoAddItf interface already exposed");
       
   370                 retval = XA_RESULT_PRECONDITIONS_VIOLATED;
       
   371             }
       
   372         }
       
   373         else /*( mapEntry && mapEntry->isDynamic )*/
       
   374         {   /* interface is not supported for dynamic management */
       
   375             DEBUG_ERR("XADIMItfImpl_DoAddItf interface not supported");
       
   376             retval = XA_RESULT_FEATURE_UNSUPPORTED;
       
   377         }
       
   378     }
       
   379     else
       
   380     {
       
   381         retval = XA_RESULT_INTERNAL_ERROR;
       
   382         DEBUG_ERR("XADIMItfImpl_DoAddItf: INVALID args");
       
   383     }
       
   384 
       
   385     DEBUG_API("<-XADIMItfImpl_DoAddItf");
       
   386     return retval;
       
   387 }
       
   388 
       
   389 /* void* XADIMItfImpl_AsyncResumeItf
       
   390  * Description: Launch ResumeItf from async thread.
       
   391  */
       
   392 void* XADIMItfImpl_AsyncResumeItf(void* args)
       
   393 {
       
   394     XAresult retval = XA_RESULT_SUCCESS;
       
   395     XADIMItfImpl* impl = GetImpl(args);
       
   396     DEBUG_API("->XADIMItfImpl_AsyncResumeItf");
       
   397 
       
   398     if( impl )
       
   399     {
       
   400         retval = XADIMItfImpl_DoResumeItf(impl, impl->asyncID);
       
   401         if( impl->dimCb )
       
   402         {
       
   403             impl->dimCb((XADynamicInterfaceManagementItf)(args), impl->dimCbContext,
       
   404                          XA_DYNAMIC_ITF_EVENT_ASYNC_TERMINATION, retval, impl->asyncID);
       
   405         }
       
   406         impl->asyncOngoing = XA_BOOLEAN_FALSE;
       
   407         XAImpl_ExitThread(impl->asyncThr);
       
   408     }
       
   409     else
       
   410     {
       
   411         DEBUG_ERR("XADIMItfImpl_AsyncResumeItf: INVALID args");
       
   412     }
       
   413     
       
   414 
       
   415     DEBUG_API("<-XADIMItfImpl_AsyncResumeItf");
       
   416     return NULL;
       
   417 }
       
   418 
       
   419 /* void XADIMItfImpl_DoResumeItf
       
   420  * Description: Handle Itf dynamic resuming and call object-specific parts.
       
   421  *              Launched either from async thread or directly.
       
   422  */
       
   423 XAresult XADIMItfImpl_DoResumeItf(XADIMItfImpl* impl,
       
   424                                 const XAInterfaceID iid)
       
   425 {
       
   426     XAresult retval = XA_RESULT_SUCCESS;
       
   427     DEBUG_API("->XADIMItfImpl_DoResumeItf");
       
   428 
       
   429     if( impl )
       
   430     {
       
   431         XAObjItfMapEntry *mapEntry = XAObjectItfImpl_GetItfEntry(impl->parent, iid);
       
   432         if( mapEntry && mapEntry->isDynamic )
       
   433         {
       
   434             if( (!mapEntry->pItf) )
       
   435             {
       
   436                 if( impl->DoResumeItfImpl )
       
   437                 {
       
   438                     retval = impl->DoResumeItfImpl(impl->parent, mapEntry);
       
   439                 }
       
   440                 else
       
   441                 {
       
   442                     DEBUG_ERR("XADIMItfImpl_DoResumeItf: Object has specified dynamic itfs but not implemented DoResumeItfImpl!!!");
       
   443                     retval = XA_RESULT_INTERNAL_ERROR;
       
   444                 }
       
   445             }
       
   446             else
       
   447             {
       
   448                 DEBUG_ERR("XADIMItfImpl_DoResumeItf interface already exposed");
       
   449                 retval = XA_RESULT_PRECONDITIONS_VIOLATED;
       
   450             }
       
   451         }
       
   452         else /*( mapEntry && mapEntry->isDynamic )*/
       
   453         {   /* interface is not supported for dynamic management */
       
   454             DEBUG_ERR("XADIMItfImpl_DoResumeItf interface not supported");
       
   455             retval = XA_RESULT_FEATURE_UNSUPPORTED;
       
   456         }
       
   457     }
       
   458     else
       
   459     {
       
   460         retval = XA_RESULT_INTERNAL_ERROR;
       
   461         DEBUG_ERR("XADIMItfImpl_DoResumeItf: INVALID args");
       
   462     }
       
   463 
       
   464     DEBUG_API("<-XADIMItfImpl_DoResumeItf");
       
   465     return retval;
       
   466 }
       
   467