io_codec.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  $RCSfile$
00003                              -------------------
00004     cvs         : $Id: crypttoken.h 1113 2007-01-10 09:14:16Z martin $
00005     begin       : Wed Mar 16 2005
00006     copyright   : (C) 2005 by Martin Preuss
00007     email       : martin@libchipcard.de
00008 
00009  ***************************************************************************
00010  *          Please see toplevel file COPYING for license details           *
00011  ***************************************************************************/
00012 
00013 #ifdef HAVE_CONFIG_H
00014 # include <config.h>
00015 #endif
00016 
00017 
00018 #include "io_codec_p.h"
00019 #include <gwenhywfar/iolayer_be.h>
00020 #include <gwenhywfar/iorequest_be.h>
00021 #include <gwenhywfar/iomanager.h>
00022 
00023 #include "i18n_l.h"
00024 #include <gwenhywfar/misc.h>
00025 #include <gwenhywfar/debug.h>
00026 #include <gwenhywfar/gui.h>
00027 
00028 #include <gwenhywfar/text.h>
00029 
00030 #include <assert.h>
00031 
00032 
00033 
00034 GWEN_INHERIT(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC)
00035 
00036 
00037 
00038 GWEN_IO_LAYER *GWEN_Io_LayerCodec_new(const char *typeName, GWEN_IO_LAYER *baseLayer) {
00039   GWEN_IO_LAYER *io;
00040   GWEN_IO_LAYER_CODEC *xio;
00041 
00042   io=GWEN_Io_Layer_new(typeName, baseLayer);
00043   assert(io);
00044   GWEN_NEW_OBJECT(GWEN_IO_LAYER_CODEC, xio);
00045   assert(xio);
00046   GWEN_INHERIT_SETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io, xio, GWEN_Io_LayerCodec_freeData);
00047 
00048   GWEN_Io_Layer_SetWorkOnRequestsFn(io, GWEN_Io_LayerCodec_WorkOnRequests);
00049   GWEN_Io_Layer_SetAddRequestFn(io, GWEN_Io_LayerCodec_AddRequest);
00050   GWEN_Io_Layer_SetDelRequestFn(io, GWEN_Io_LayerCodec_DelRequest);
00051   GWEN_Io_Layer_SetHasWaitingRequestsFn(io, GWEN_Io_LayerCodec_HasWaitingRequests);
00052 
00053   return io;
00054 }
00055 
00056 
00057 
00058 GWENHYWFAR_CB
00059 void GWEN_Io_LayerCodec_freeData(void *bp, void *p) {
00060   GWEN_IO_LAYER *io;
00061   GWEN_IO_LAYER_CODEC *xio;
00062 
00063   io=(GWEN_IO_LAYER*) bp;
00064   assert(io);
00065   xio=(GWEN_IO_LAYER_CODEC*) p;
00066   assert(xio);
00067 
00068   GWEN_Io_LayerCodec_AbortInRequests(io, GWEN_ERROR_ABORTED);
00069   GWEN_Io_LayerCodec_AbortOutRequests(io);
00070 
00071   GWEN_RingBuffer_free(xio->readBuffer);
00072   GWEN_RingBuffer_free(xio->writeBuffer);
00073 
00074   GWEN_FREE_OBJECT(xio);
00075 }
00076 
00077 
00078 
00079 uint32_t GWEN_Io_LayerCodec_GetCurrentGuiId(const GWEN_IO_LAYER *io) {
00080   GWEN_IO_LAYER_CODEC *xio;
00081 
00082   assert(io);
00083   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00084   assert(xio);
00085 
00086   return xio->currentGuiId;
00087 }
00088 
00089 
00090 
00091 void GWEN_Io_LayerCodec_SetCurrentGuiId(GWEN_IO_LAYER *io, uint32_t guiid) {
00092   GWEN_IO_LAYER_CODEC *xio;
00093 
00094   assert(io);
00095   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00096   assert(xio);
00097 
00098   xio->currentGuiId=guiid;
00099 }
00100 
00101 
00102 
00103 void GWEN_Io_LayerCodec_AbortInRequests(GWEN_IO_LAYER *io, int errorCode) {
00104   GWEN_IO_LAYER_CODEC *xio;
00105 
00106   assert(io);
00107   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00108   assert(xio);
00109 
00110   if (xio->readRequestIn) {
00111     GWEN_IO_REQUEST *r;
00112 
00113     r=xio->readRequestIn;
00114     xio->readRequestIn=NULL;
00115     GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, errorCode);
00116     GWEN_Io_Request_free(r);
00117   }
00118   if (xio->writeRequestIn) {
00119     GWEN_IO_REQUEST *r;
00120 
00121     r=xio->writeRequestIn;
00122     xio->writeRequestIn=NULL;
00123     GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, errorCode);
00124     GWEN_Io_Request_free(r);
00125   }
00126 }
00127 
00128 
00129 
00130 void GWEN_Io_LayerCodec_AbortOutRequests(GWEN_IO_LAYER *io) {
00131   GWEN_IO_LAYER_CODEC *xio;
00132 
00133   assert(io);
00134   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00135   assert(xio);
00136 
00137   if (xio->readRequestOut) {
00138     GWEN_IO_REQUEST *r;
00139 
00140     r=xio->readRequestOut;
00141     GWEN_Io_Layer_DelRequest(GWEN_Io_Layer_GetBaseLayer(io), r);
00142     xio->readRequestOut=NULL;
00143     GWEN_Io_Request_free(r);
00144   }
00145   if (xio->writeRequestOut) {
00146     GWEN_IO_REQUEST *r;
00147 
00148     r=xio->writeRequestOut;
00149     GWEN_Io_Layer_DelRequest(GWEN_Io_Layer_GetBaseLayer(io), r);
00150     xio->writeRequestOut=NULL;
00151     GWEN_Io_Request_free(r);
00152   }
00153 }
00154 
00155 
00156 
00157 int GWEN_Io_LayerCodec_AddRequest(GWEN_IO_LAYER *io, GWEN_IO_REQUEST *r) {
00158   GWEN_IO_LAYER_CODEC *xio;
00159   GWEN_IO_LAYER_STATUS st;
00160 
00161   assert(io);
00162   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00163   assert(xio);
00164 
00165   st=GWEN_Io_Layer_GetStatus(io);
00166 
00167   switch(GWEN_Io_Request_GetType(r)) {
00168   case GWEN_Io_Request_TypeRead:
00169     /* check status */
00170     if (st!=GWEN_Io_Layer_StatusConnected) {
00171       DBG_INFO(GWEN_LOGDOMAIN, "IO layer is not open");
00172       GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, GWEN_ERROR_NOT_OPEN);
00173       return GWEN_ERROR_NOT_OPEN;
00174     }
00175 
00176     /* check whether we already have a read request */
00177     if (xio->readRequestIn) {
00178       DBG_INFO(GWEN_LOGDOMAIN, "There already is a read request");
00179       return GWEN_ERROR_TRY_AGAIN;
00180     }
00181 
00182     if (xio->lastReadOutResult) {
00183       DBG_INFO(GWEN_LOGDOMAIN, "Unable to read (%d)", xio->lastReadOutResult);
00184       GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, xio->lastReadOutResult);
00185       return xio->lastReadOutResult;
00186     }
00187 
00188     /* enqueue request */
00189     xio->readRequestIn=r;
00190     GWEN_Io_Request_Attach(xio->readRequestIn);
00191     break;
00192 
00193   case GWEN_Io_Request_TypeWrite:
00194     /* check status */
00195     if (st!=GWEN_Io_Layer_StatusConnected) {
00196       DBG_INFO(GWEN_LOGDOMAIN, "IO layer is not open");
00197       GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, GWEN_ERROR_NOT_OPEN);
00198       return GWEN_ERROR_NOT_OPEN;
00199     }
00200 
00201     /* check whether we already have a write request */
00202     if (xio->writeRequestIn) {
00203       DBG_INFO(GWEN_LOGDOMAIN, "There already is a write request");
00204       return GWEN_ERROR_TRY_AGAIN;
00205     }
00206 
00207     /* enqueue request */
00208     xio->writeRequestIn=r;
00209     GWEN_Io_Request_Attach(xio->writeRequestIn);
00210     break;
00211 
00212   case GWEN_Io_Request_TypeConnect:
00213     /* check status */
00214     if (st!=GWEN_Io_Layer_StatusUnconnected &&
00215         st!=GWEN_Io_Layer_StatusDisconnected) {
00216       DBG_INFO(GWEN_LOGDOMAIN, "IO layer is not open");
00217       GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, GWEN_ERROR_NOT_OPEN);
00218       return GWEN_ERROR_NOT_OPEN;
00219     }
00220     else {
00221       GWEN_Io_Layer_SetStatus(io, GWEN_Io_Layer_StatusConnected);
00222       GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, 0);
00223     }
00224     break;
00225 
00226   case GWEN_Io_Request_TypeDisconnect:
00227     /* check status */
00228     if (st!=GWEN_Io_Layer_StatusConnected) {
00229       DBG_INFO(GWEN_LOGDOMAIN, "IO layer is not open");
00230       GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, GWEN_ERROR_NOT_OPEN);
00231       return GWEN_ERROR_NOT_OPEN;
00232     }
00233     else {
00234       /* abort incoming requests, if any */
00235       GWEN_Io_LayerCodec_AbortInRequests(io, GWEN_ERROR_ABORTED);
00236       GWEN_Io_LayerCodec_AbortOutRequests(io);
00237 
00238       /* free ring buffers */
00239       GWEN_RingBuffer_free(xio->readBuffer);
00240       xio->readBuffer=NULL;
00241       GWEN_RingBuffer_free(xio->writeBuffer);
00242       xio->writeBuffer=NULL;
00243 
00244       /* closed */
00245       GWEN_Io_Layer_SetStatus(io, GWEN_Io_Layer_StatusDisconnected);
00246       GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, 0);
00247     }
00248     break;
00249 
00250   default:
00251     DBG_INFO(GWEN_LOGDOMAIN, "This request type is not supported (%d)", GWEN_Io_Request_GetType(r));
00252     GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, GWEN_ERROR_NOT_SUPPORTED);
00253     return GWEN_ERROR_NOT_SUPPORTED;
00254 
00255   }
00256 
00257   return 0;
00258 }
00259 
00260 
00261 
00262 int GWEN_Io_LayerCodec_DelRequest(GWEN_IO_LAYER *io, GWEN_IO_REQUEST *r) {
00263   GWEN_IO_LAYER_CODEC *xio;
00264 
00265   assert(io);
00266   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00267   assert(xio);
00268 
00269   switch(GWEN_Io_Request_GetType(r)) {
00270   case GWEN_Io_Request_TypeRead:
00271     if (xio->readRequestIn==r) {
00272       DBG_DEBUG(GWEN_LOGDOMAIN, "Aborted read request");
00273       xio->readRequestIn=NULL;
00274       GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, GWEN_ERROR_ABORTED);
00275       GWEN_Io_Request_free(r);
00276     }
00277     else {
00278       /* not my request */
00279       DBG_INFO(GWEN_LOGDOMAIN, "Read request not registered with this io layer");
00280       return GWEN_ERROR_INVALID;
00281     }
00282     break;
00283 
00284   case GWEN_Io_Request_TypeWrite:
00285     if (xio->writeRequestIn==r) {
00286       DBG_DEBUG(GWEN_LOGDOMAIN, "Aborted write request");
00287       xio->writeRequestIn=NULL;
00288       GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, GWEN_ERROR_ABORTED);
00289       GWEN_Io_Request_free(r);
00290     }
00291     else {
00292       /* not my request */
00293       DBG_INFO(GWEN_LOGDOMAIN, "Write request not registered with this io layer");
00294       return GWEN_ERROR_INVALID;
00295     }
00296     break;
00297 
00298   default:
00299     break;
00300   }
00301 
00302   return 0;
00303 }
00304 
00305 
00306 
00307 int GWEN_Io_LayerCodec_HasWaitingRequests(GWEN_IO_LAYER *io) {
00308   GWEN_IO_LAYER_CODEC *xio;
00309 
00310   assert(io);
00311   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00312   assert(xio);
00313 
00314   if (xio->readRequestIn || xio->writeRequestIn)
00315     return 1;
00316   return 0;
00317 }
00318 
00319 
00320 
00321 int GWEN_Io_LayerCodec_CheckWriteOut(const GWEN_IO_LAYER *io) {
00322   GWEN_IO_LAYER_CODEC *xio;
00323 
00324   assert(io);
00325   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00326   assert(xio);
00327 
00328   if (xio->lastWriteOutResult)
00329     return xio->lastWriteOutResult;
00330 
00331   /*
00332   if (xio->writeRequestOut)
00333     return GWEN_ERROR_TRY_AGAIN;
00334     */
00335 
00336   if (xio->writeBuffer==NULL)
00337     xio->writeBuffer=GWEN_RingBuffer_new(GWEN_IO_LAYER_CODEC_BUFSIZE);
00338 
00339   if (GWEN_RingBuffer_GetMaxUnsegmentedWrite(xio->writeBuffer)==0)
00340     return GWEN_ERROR_TRY_AGAIN;
00341 
00342   return 0;
00343 }
00344 
00345 
00346 
00347 GWEN_RINGBUFFER *GWEN_Io_LayerCodec_GetReadBuffer(const GWEN_IO_LAYER *io) {
00348   GWEN_IO_LAYER_CODEC *xio;
00349 
00350   assert(io);
00351   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00352   assert(xio);
00353 
00354   return xio->readBuffer;
00355 }
00356 
00357 
00358 
00359 GWEN_RINGBUFFER *GWEN_Io_LayerCodec_GetWriteBuffer(const GWEN_IO_LAYER *io) {
00360   GWEN_IO_LAYER_CODEC *xio;
00361 
00362   assert(io);
00363   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00364   assert(xio);
00365 
00366   return xio->writeBuffer;
00367 }
00368 
00369 
00370 
00371 int GWEN_Io_LayerCodec_EnsureReadOk(GWEN_IO_LAYER *io) {
00372   GWEN_IO_LAYER_CODEC *xio;
00373   uint32_t len;
00374 
00375   assert(io);
00376   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00377   assert(xio);
00378 
00379   if (xio->readBuffer==NULL)
00380     xio->readBuffer=GWEN_RingBuffer_new(GWEN_IO_LAYER_CODEC_BUFSIZE);
00381 
00382   len=GWEN_RingBuffer_GetMaxUnsegmentedRead(xio->readBuffer);
00383   if (len==0) {
00384     /* read buffer empty, fill it if possible */
00385     if (xio->readRequestOut)
00386       /* there already is a read request */
00387       return GWEN_ERROR_IN_PROGRESS;
00388     else {
00389       if (xio->lastReadOutResult)
00390         return xio->lastReadOutResult;
00391       else {
00392         GWEN_IO_REQUEST *br;
00393         int rv;
00394 
00395         len=GWEN_RingBuffer_GetMaxUnsegmentedWrite(xio->readBuffer);
00396         assert(len);
00397 
00398         /* there is no outbound write request, create one */
00399         br=GWEN_Io_Request_new(GWEN_Io_Request_TypeRead,
00400                                (uint8_t*)GWEN_RingBuffer_GetWritePointer(xio->readBuffer), len,
00401                                NULL, NULL,
00402                                xio->currentGuiId);
00403         DBG_DEBUG(GWEN_LOGDOMAIN, "Adding read request (%d bytes)", len);
00404         rv=GWEN_Io_Layer_AddRequest(GWEN_Io_Layer_GetBaseLayer(io), br);
00405         if (rv) {
00406           if (rv!=GWEN_ERROR_TRY_AGAIN)
00407             xio->lastReadOutResult=rv;
00408           DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
00409           GWEN_Io_Request_free(br);
00410           return rv;
00411         }
00412         else {
00413           xio->readRequestOut=br;
00414           return GWEN_ERROR_TRY_AGAIN;
00415         }
00416       }
00417     }
00418   }
00419   return 0;
00420 }
00421 
00422 
00423 
00424 GWEN_IO_LAYER_WORKRESULT GWEN_Io_LayerCodec_WorkOnOutRequests(GWEN_IO_LAYER *io) {
00425   GWEN_IO_LAYER_CODEC *xio;
00426   int doneSomething=0;
00427 
00428   assert(io);
00429   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00430   assert(xio);
00431 
00432   if (xio->readRequestOut) {
00433     /* check for finished outbound read request */
00434     if (GWEN_Io_Request_GetStatus(xio->readRequestOut)==GWEN_Io_Request_StatusFinished) {
00435       uint32_t bpos;
00436 
00437       doneSomething=1;
00438 
00439       /* get all available data */
00440       bpos=GWEN_Io_Request_GetBufferPos(xio->readRequestOut);
00441       DBG_INFO(GWEN_LOGDOMAIN, "Read %d bytes into ringbuffer (now %d)",
00442                bpos, GWEN_RingBuffer_GetUsedBytes(xio->readBuffer)+bpos);
00443       GWEN_RingBuffer_SkipBytesWrite(xio->readBuffer, bpos);
00444 
00445       /* save result code */
00446       xio->lastReadOutResult=GWEN_Io_Request_GetResultCode(xio->readRequestOut);
00447       GWEN_Io_Request_free(xio->readRequestOut);
00448       xio->readRequestOut=NULL;
00449     }
00450   }
00451 
00452   if (xio->writeRequestOut) {
00453     /* check for finished outbound write request */
00454     if (GWEN_Io_Request_GetStatus(xio->writeRequestOut)==GWEN_Io_Request_StatusFinished) {
00455       uint32_t bpos;
00456 
00457       doneSomething=1;
00458 
00459       /* handle all available data */
00460       bpos=GWEN_Io_Request_GetBufferPos(xio->writeRequestOut);
00461       DBG_INFO(GWEN_LOGDOMAIN, "Written %d bytes from ringbuffer", bpos);
00462       GWEN_RingBuffer_SkipBytesRead(xio->writeBuffer, bpos);
00463 
00464       /* save result code */
00465       xio->lastWriteOutResult=GWEN_Io_Request_GetResultCode(xio->writeRequestOut);
00466       GWEN_Io_Request_free(xio->writeRequestOut);
00467       xio->writeRequestOut=NULL;
00468     }
00469   }
00470 
00471   if (xio->writeRequestOut==NULL && xio->lastWriteOutResult==0 && xio->writeBuffer) {
00472     uint32_t len;
00473 
00474     len=GWEN_RingBuffer_GetMaxUnsegmentedRead(xio->writeBuffer);
00475     if (len!=0) {
00476       GWEN_IO_REQUEST *br;
00477       int rv;
00478 
00479       /* write buffer not empty, flush it if possible */
00480       DBG_DEBUG(GWEN_LOGDOMAIN, "Write buffer is not empty, flushing");
00481       br=GWEN_Io_Request_new(GWEN_Io_Request_TypeWrite,
00482                              (uint8_t*)GWEN_RingBuffer_GetReadPointer(xio->writeBuffer), len,
00483                              NULL, NULL,
00484                              xio->currentGuiId);
00485       DBG_DEBUG(GWEN_LOGDOMAIN, "Adding write request (%d bytes)", len);
00486       rv=GWEN_Io_Layer_AddRequest(GWEN_Io_Layer_GetBaseLayer(io), br);
00487       if (rv) {
00488         DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
00489         if (rv!=GWEN_ERROR_TRY_AGAIN) {
00490           xio->lastWriteOutResult=rv;
00491           doneSomething=1;
00492         }
00493         GWEN_Io_Request_free(br);
00494       }
00495       else {
00496         xio->writeRequestOut=br;
00497         doneSomething=1;
00498       }
00499     }
00500   }
00501 
00502   return (doneSomething==0)?GWEN_Io_Layer_WorkResultBlocking:GWEN_Io_Layer_WorkResultOk;
00503 }
00504 
00505 
00506 
00507 int GWEN_Io_LayerCodec_Encode(GWEN_IO_LAYER *io, const uint8_t *pBuffer, uint32_t lBuffer) {
00508   GWEN_IO_LAYER_CODEC *xio;
00509 
00510   assert(io);
00511   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00512   assert(xio);
00513 
00514   if (xio->encodeFn)
00515     return xio->encodeFn(io, pBuffer, lBuffer);
00516   else
00517     return GWEN_ERROR_NOT_IMPLEMENTED;
00518 }
00519 
00520 
00521 
00522 int GWEN_Io_LayerCodec_Decode(GWEN_IO_LAYER *io, uint8_t *pBuffer, uint32_t lBuffer) {
00523   GWEN_IO_LAYER_CODEC *xio;
00524 
00525   assert(io);
00526   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00527   assert(xio);
00528 
00529   if (xio->decodeFn)
00530     return xio->decodeFn(io, pBuffer, lBuffer);
00531   else
00532     return GWEN_ERROR_NOT_IMPLEMENTED;
00533 }
00534 
00535 
00536 
00537 GWEN_IO_LAYER_WORKRESULT GWEN_Io_LayerCodec_WorkOnReadRequest(GWEN_IO_LAYER *io) {
00538   GWEN_IO_LAYER_CODEC *xio;
00539   int doneSomething=0;
00540 
00541   assert(io);
00542   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00543   assert(xio);
00544 
00545   if (xio->readRequestIn) {
00546     GWEN_IO_REQUEST *r;
00547     uint32_t maxBytes;
00548     uint32_t bpos;
00549     uint8_t *dst;
00550     int rv;
00551 
00552     r=xio->readRequestIn;
00553     xio->currentGuiId=GWEN_Io_Request_GetGuiId(r);
00554 
00555     /* read raw data */
00556     bpos=GWEN_Io_Request_GetBufferPos(r);
00557     dst=GWEN_Io_Request_GetBufferPtr(r)+bpos;
00558     maxBytes=GWEN_Io_Request_GetBufferSize(r)-bpos;
00559     rv=GWEN_Io_LayerCodec_Decode(io, dst, maxBytes);
00560     if (rv<0) {
00561       if (rv!=GWEN_ERROR_TRY_AGAIN) {
00562         xio->readRequestIn=NULL;
00563         GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, rv);
00564         GWEN_Io_Request_free(r);
00565         doneSomething=1;
00566       }
00567     }
00568     else {
00569       doneSomething=1;
00570       if (rv) {
00571         bpos+=rv;
00572         GWEN_Io_Request_SetBufferPos(r, bpos);
00573         xio->readRequestIn=NULL;
00574 
00575         GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, 0);
00576       }
00577       else {
00578         DBG_INFO(GWEN_LOGDOMAIN, "EOF met");
00579         GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, GWEN_ERROR_EOF);
00580       }
00581       GWEN_Io_Request_free(r);
00582     }
00583   }
00584 
00585   return (doneSomething==0)?GWEN_Io_Layer_WorkResultBlocking:GWEN_Io_Layer_WorkResultOk;
00586 }
00587 
00588 
00589 
00590 GWEN_IO_LAYER_WORKRESULT GWEN_Io_LayerCodec_WorkOnWriteRequest(GWEN_IO_LAYER *io) {
00591   GWEN_IO_LAYER_CODEC *xio;
00592   int doneSomething=0;
00593 
00594   assert(io);
00595   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00596   assert(xio);
00597 
00598   if (xio->writeRequestIn) {
00599     GWEN_IO_REQUEST *r;
00600     uint32_t maxBytes;
00601     uint32_t bpos;
00602     const uint8_t *src;
00603     int rv;
00604 
00605     r=xio->writeRequestIn;
00606     xio->currentGuiId=GWEN_Io_Request_GetGuiId(r);
00607 
00608     /* read raw data */
00609     bpos=GWEN_Io_Request_GetBufferPos(r);
00610     src=GWEN_Io_Request_GetBufferPtr(r)+bpos;
00611     maxBytes=GWEN_Io_Request_GetBufferSize(r)-bpos;
00612     rv=GWEN_Io_LayerCodec_Encode(io, src, maxBytes);
00613     if (rv<0) {
00614       if (rv!=GWEN_ERROR_TRY_AGAIN) {
00615         xio->writeRequestIn=NULL;
00616         GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, rv);
00617         GWEN_Io_Request_free(r);
00618         doneSomething=1;
00619       }
00620     }
00621     else {
00622       doneSomething=1;
00623       bpos+=rv;
00624       GWEN_Io_Request_SetBufferPos(r, bpos);
00625       xio->writeRequestIn=NULL;
00626       GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, 0);
00627       GWEN_Io_Request_free(r);
00628     }
00629   }
00630 
00631   return (doneSomething==0)?GWEN_Io_Layer_WorkResultBlocking:GWEN_Io_Layer_WorkResultOk;
00632 }
00633 
00634 
00635 
00636 GWEN_IO_LAYER_WORKRESULT GWEN_Io_LayerCodec_WorkOnRequests(GWEN_IO_LAYER *io) {
00637   GWEN_IO_LAYER_CODEC *xio;
00638   GWEN_IO_LAYER *baseLayer;
00639   int doneSomething=0;
00640 
00641   assert(io);
00642   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00643   assert(xio);
00644 
00645   baseLayer=GWEN_Io_Layer_GetBaseLayer(io);
00646 
00647   if (GWEN_Io_LayerCodec_WorkOnOutRequests(io)!=GWEN_Io_Layer_WorkResultBlocking)
00648     doneSomething=1;
00649   if (GWEN_Io_LayerCodec_WorkOnReadRequest(io)!=GWEN_Io_Layer_WorkResultBlocking)
00650     doneSomething=1;
00651   if (GWEN_Io_LayerCodec_WorkOnWriteRequest(io)!=GWEN_Io_Layer_WorkResultBlocking)
00652     doneSomething=1;
00653   if (GWEN_Io_LayerCodec_WorkOnOutRequests(io)!=GWEN_Io_Layer_WorkResultBlocking)
00654     doneSomething=1;
00655   if (baseLayer && GWEN_Io_Layer_WorkOnRequests(baseLayer)!=GWEN_Io_Layer_WorkResultBlocking)
00656     doneSomething=1;
00657 
00658   return (doneSomething==0)?GWEN_Io_Layer_WorkResultBlocking:GWEN_Io_Layer_WorkResultOk;
00659 }
00660 
00661 
00662 
00663 void GWEN_Io_LayerCodec_AbortRequests(GWEN_IO_LAYER *io, int errorCode) {
00664   GWEN_IO_LAYER_CODEC *xio;
00665 
00666   assert(io);
00667   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00668   assert(xio);
00669 
00670   /* abort inbound requests */
00671   if (xio->readRequestIn) {
00672     GWEN_IO_REQUEST *r;
00673 
00674     r=xio->readRequestIn;
00675     xio->readRequestIn=NULL;
00676     GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, errorCode);
00677     GWEN_Io_Request_free(r);
00678   }
00679   if (xio->writeRequestIn) {
00680     GWEN_IO_REQUEST *r;
00681 
00682     r=xio->writeRequestIn;
00683     xio->writeRequestIn=NULL;
00684     GWEN_Io_Request_Finished(r, GWEN_Io_Request_StatusFinished, errorCode);
00685     GWEN_Io_Request_free(r);
00686   }
00687 
00688   /* abort outbound requests */
00689   if (xio->readRequestOut) {
00690     GWEN_IO_REQUEST *r;
00691 
00692     r=xio->readRequestOut;
00693     GWEN_Io_Layer_DelRequest(GWEN_Io_Layer_GetBaseLayer(io), r);
00694     xio->readRequestOut=NULL;
00695     GWEN_Io_Request_free(r);
00696   }
00697   if (xio->writeRequestIn) {
00698     GWEN_IO_REQUEST *r;
00699 
00700     r=xio->writeRequestIn;
00701     GWEN_Io_Layer_DelRequest(GWEN_Io_Layer_GetBaseLayer(io), r);
00702     xio->writeRequestIn=NULL;
00703     GWEN_Io_Request_free(r);
00704   }
00705 }
00706 
00707 
00708 
00709 void GWEN_Io_LayerCodec_Reset(GWEN_IO_LAYER *io) {
00710   GWEN_IO_LAYER_CODEC *xio;
00711 
00712   assert(io);
00713   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00714   assert(xio);
00715 
00716   /* abort incoming requests, if any */
00717   GWEN_Io_LayerCodec_AbortInRequests(io, GWEN_ERROR_ABORTED);
00718   GWEN_Io_LayerCodec_AbortOutRequests(io);
00719 
00720   /* free ring buffers */
00721   GWEN_RingBuffer_free(xio->readBuffer);
00722   xio->readBuffer=NULL;
00723   GWEN_RingBuffer_free(xio->writeBuffer);
00724   xio->writeBuffer=NULL;
00725   xio->lastReadOutResult=0;
00726   xio->lastWriteOutResult=0;
00727 }
00728 
00729 
00730 
00731 GWEN_IO_LAYER_CODE_ENCODE_FN GWEN_Io_LayerCodec_SetEncodeFn(GWEN_IO_LAYER *io,
00732                                                             GWEN_IO_LAYER_CODE_ENCODE_FN f) {
00733   GWEN_IO_LAYER_CODE_ENCODE_FN of;
00734   GWEN_IO_LAYER_CODEC *xio;
00735 
00736   assert(io);
00737   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00738   assert(xio);
00739 
00740   of=xio->encodeFn;
00741   xio->encodeFn=f;
00742 
00743   return of;
00744 }
00745 
00746 
00747 
00748 GWEN_IO_LAYER_CODE_DECODE_FN GWEN_Io_LayerCodec_SetDecodeFn(GWEN_IO_LAYER *io,
00749                                                             GWEN_IO_LAYER_CODE_DECODE_FN f) {
00750   GWEN_IO_LAYER_CODE_DECODE_FN of;
00751   GWEN_IO_LAYER_CODEC *xio;
00752 
00753   assert(io);
00754   xio=GWEN_INHERIT_GETDATA(GWEN_IO_LAYER, GWEN_IO_LAYER_CODEC, io);
00755   assert(xio);
00756 
00757   of=xio->decodeFn;
00758   xio->decodeFn=f;
00759 
00760   return of;
00761 }
00762 
00763 
00764 
00765 
00766 
00767 
00768 
00769 

Generated on Thu Aug 20 13:54:38 2009 for gwenhywfar by  doxygen 1.5.9