Parcel.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _SUPPORT_PARCEL_H
00014 #define _SUPPORT_PARCEL_H
00015 
00021 #include <support/SupportDefs.h>
00022 #include <support/IBinder.h>
00023 #include <support/IByteStream.h>
00024 #include <support/ITextStream.h>
00025 #include <support/Vector.h>
00026 
00027 #if TARGET_HOST == TARGET_HOST_PALMOS
00028 #include <Kernel.h>
00029 #include <support/KeyID.h>
00030 #endif
00031 
00032 BNS(namespace palmos {)
00033 BNS(namespace support {)
00034 
00035 
00039 class IBinder;
00040 
00041 struct binder_ipc_info;
00042 struct flat_binder_object;
00043 
00044 struct small_flat_data;
00045 struct large_flat_header;
00046 
00047 /*-------------------------------------------------------------*/
00048 
00050 class SParcel
00051 {
00052     public:
00053         static  SParcel*        GetParcel(void);
00054         static  void            PutParcel(SParcel *);
00055     
00056         typedef void            (*free_func)(   const void* data,
00057                                                 ssize_t len,
00058                                                 void* context);
00059         typedef status_t        (*reply_func)(  const SParcel& buffer,
00060                                                 void* context);
00061     
00062                                 SParcel(ssize_t bufferSize = -1);
00063                                 SParcel(sptr<IByteOutput> output,
00064                                         sptr<IByteInput> input,
00065                                         sptr<IByteSeekable> seek,
00066                                         ssize_t bufferSize = -1);
00067                                 SParcel(reply_func replyFunc,
00068                                         void* replyContext = NULL);
00069                                 SParcel(const void* data, ssize_t len,
00070                                         free_func freeFunc = NULL,
00071                                         void* freeContext = NULL,
00072                                         reply_func replyFunc = NULL,
00073                                         void* replyContext = NULL);
00074         virtual                 ~SParcel();
00075     
00077                 const void*     Data() const;
00078                 
00080                 void*           EditData();
00081                 
00083                 ssize_t         Length() const;
00084                 
00086                 ssize_t         Avail() const;
00087                 
00089 
00093                 ssize_t         AvailBinders() const;
00094                 
00096                 status_t        ErrorCheck() const;
00097                 
00099                 bool            IsCacheable() const;
00100 
00102                 void            Reference(  const void* data, ssize_t len,
00103                                             free_func freeFunc = NULL,
00104                                             void* context = NULL);
00105                 
00107                 status_t        Copy(const void* data, ssize_t len);
00108                 
00110                 status_t        Copy(const SParcel& src);
00111                 
00113                 status_t        Reserve(ssize_t len);
00114                 
00116                 void*           Alloc(ssize_t len);
00117                 
00119                 void*           ReAlloc(ssize_t len);
00120 
00122                 void            Reset();
00123                 
00125                 status_t        SetValues(const SValue* value1, ...);
00126                 
00128                 int32_t         CountValues() const;
00129                 
00131                 int32_t         GetValues(int32_t maxCount, SValue* outValues) const;
00132                 
00134                 void            Transfer(SParcel* src);
00135                 
00137 
00138                 bool            ReplyRequested() const;
00139                 status_t        Reply();
00141 
00143                 void            Free();
00144                 
00145                 off_t           Position() const;
00146                 void            SetPosition(off_t pos);
00147                 status_t        SetLength(ssize_t len);
00148                 
00156 
00158 
00162                 ssize_t         WriteTypeHeader(type_code type, size_t amount);
00164 
00166                 ssize_t         WriteTypeHeaderAndData(type_code type, const void *data, size_t amount);
00167                 
00169 
00171                 ssize_t         MarshalFixedData(type_code type, const void *data, size_t amount);
00173 
00176                 status_t        UnmarshalFixedData(type_code type, void *data, size_t amount);
00177 
00179                 ssize_t         WriteSmallData(uint32_t packedType, uint32_t packedData);
00181                 ssize_t         WriteSmallData(const small_flat_data& flat);
00183 
00184                 ssize_t         WriteLargeData(uint32_t packedType, size_t length, const void* data);
00186                 ssize_t         WriteBinder(const flat_binder_object& val);
00187 
00189                 ssize_t         ReadSmallData(small_flat_data* out);
00190 
00192 
00195                 ssize_t         ReadSmallDataOrObject(small_flat_data* out, const void* who);
00196 
00198                 ssize_t         ReadFlatBinderObject(flat_binder_object* out);
00199                 
00201 
00204                 ssize_t         WriteTypedData(type_code type, const void *data);
00206 
00215                 status_t        ReadTypedData(type_code type, void *data);
00216 
00218         static  ssize_t         TypedDataSize(type_code type, const void *data);
00219                 
00221 
00222                 ssize_t         WriteValue(const SValue& val);
00224 
00225                 SValue          ReadValue();
00226 
00228 
00231                 status_t        SkipValue();
00232 
00234 
00240 
00241         static  ssize_t         BinderSize(const sptr<IBinder>& val);
00242         static  ssize_t         WeakBinderSize(const wptr<IBinder>& val);
00243 
00244                 ssize_t         WriteBinder(const sptr<IBinder>& val);
00245                 ssize_t         WriteWeakBinder(const wptr<IBinder>& val);
00246                 ssize_t         WriteBinder(const small_flat_data& val);
00247 #if TARGET_HOST == TARGET_HOST_PALMOS
00248                 ssize_t         WriteKeyID(const sptr<SKeyID>& val);
00249 #endif
00250 
00251                 
00252                 sptr<IBinder>   ReadBinder();
00253                 wptr<IBinder>   ReadWeakBinder();
00254 #if TARGET_HOST == TARGET_HOST_PALMOS
00255                 sptr<SKeyID>    ReadKeyID();
00256 #endif
00257 
00259 
00263 
00264                 ssize_t         Write(const void* buffer, size_t amount);
00265                 void *          WriteInPlace(size_t amount); 
00266                 ssize_t         WritePadded(const void* buffer, size_t amount); 
00267                 ssize_t         WritePadding(); 
00268                 ssize_t         WriteBool(bool val);
00269                 ssize_t         WriteInt8(int8_t val);
00270                 ssize_t         WriteInt16(int16_t val);
00271                 ssize_t         WriteInt32(int32_t val);
00272                 ssize_t         WriteInt64(int64_t val);
00273                 ssize_t         WriteUInt8(uint8_t val) {return WriteInt8((int8_t)val);}
00274                 ssize_t         WriteUInt16(uint16_t val) {return WriteInt16((int16_t)val);}
00275                 ssize_t         WriteUInt32(uint32_t val) {return WriteInt32((int32_t)val);}
00276                 ssize_t         WriteUInt64(uint64_t val) {return WriteInt64((int64_t)val);}
00277                 ssize_t         WriteFloat(float val);
00278                 ssize_t         WriteDouble(double val);
00279                 ssize_t         WriteString(const SString& val);
00280                 ssize_t         WriteString(const char* val);
00281 
00282                 ssize_t         Read(void* buffer, size_t amount);
00283                 const void*     ReadInPlace(size_t amount);
00284                 ssize_t         ReadPadded(void* buffer, size_t amount);
00285                 ssize_t         Drain(size_t amount);
00286                 ssize_t         DrainPadding(); 
00287                 bool            ReadBool();
00288                 int8_t          ReadInt8();
00289                 int16_t         ReadInt16();
00290                 int32_t         ReadInt32();
00291                 int64_t         ReadInt64();
00292                 uint8_t         ReadUInt8() {return (uint8_t)ReadInt8();}
00293                 uint16_t        ReadUInt16() {return (uint16_t)ReadInt16();}
00294                 uint32_t        ReadUInt32() {return (uint32_t)ReadInt32();}
00295                 uint64_t        ReadUInt64() {return (uint64_t)ReadInt64();}
00296                 float           ReadFloat();
00297                 double          ReadDouble();
00298                 SString         ReadString();
00299                 
00300                 ssize_t         Flush();
00301                 ssize_t         Sync();
00302                 
00304 
00308                 const void*     BinderOffsetsData() const;
00309                 size_t          BinderOffsetsLength() const;
00310 #if TARGET_HOST == TARGET_HOST_PALMOS
00311                 status_t        SetBinderOffsets(   binder_ipc_info const * const offsets,
00312                                                     size_t count,
00313                                                     bool takeRefs = true);
00314 #else
00315                 status_t        SetBinderOffsets(   const void* offsets,
00316                                                     size_t length,
00317                                                     bool takeRefs = true);
00318 #endif
00319 
00320                 
00321                 void            PrintToStream(const sptr<ITextOutput>& io, uint32_t flags = 0) const;
00322     
00323     protected:
00324     
00325         virtual ssize_t         WriteBuffer(const void* buffer, size_t len) const;
00326         virtual ssize_t         ReadBuffer(void* buffer, size_t len);
00327                                                 
00328     private:
00329                                 SParcel(const SParcel& o);
00330                 SParcel&        operator=(const SParcel& o);
00331                 
00332                 void            do_free();
00333                 void            acquire_binders();
00334                 void            release_binders();
00335                 ssize_t         finish_write(ssize_t amt);
00336                 
00337                 uint8_t*            m_data;
00338                 ssize_t             m_length;
00339                 ssize_t             m_avail;
00340                 
00341                 free_func           m_free;
00342                 void*               m_freeContext;
00343                 
00344                 reply_func          m_reply;
00345                 void*               m_replyContext;
00346                 
00347                 off_t               m_base;
00348                 ssize_t             m_pos;
00349                 sptr<IByteOutput>   m_out;
00350                 sptr<IByteInput>    m_in;
00351                 sptr<IByteSeekable> m_seek;
00352                 
00353                 uint32_t                m_dirty : 1;
00354                 uint32_t                m_ownsBinders : 1;
00355                 uint32_t                m_reserved: 30;
00356                 
00357                 SVector<size_t>     m_binders;
00358                 
00359                 uint8_t             m_inline[32];
00360 };
00361 
00362 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, const SParcel& value);
00363 
00366 /*-------------------------------------------------------------*/
00367 
00368 BNS(} }) // namespace palmos::support
00369 
00370 #endif  // _SUPPORT_PARCEL_H