
  Sony GBP Mode Control Library 3.1    by Jiri Hruska (HruskaJiri@seznam.cz)
 ============================================================================

   You can use this DLL to communicate with the Sony mobile phones in the
 GBP mode to manage phonebook, scheduler (calendar) and modify the NVM
 files ("User data"). It can read, write or erase them and can access
 the phone on all serial ports present (COMx). I've made both a C header
 file & library and Delphi unit, so almost everybody can use it in his
 favourite language.
   This library is FREEWARE. It means that you can freely use it in your
 own programs, but you should at least include me in the credits. If you
 are making a shareware product, you haven't to send me money, but you
 should send me a copy of your program. Thanks.
   If you have any comments or questions, related to this file or the
 DLL functionality, feel free to mail me. If you encounter any communication
 related problems, please include a log file captured by PortMon(tm) by
 System Internals(R) <www.sysinternals.com> in your e-mail, it will help
 me a lot when finding the bugs.
   The phonebook and scheduler structures are now explained here too, if
 want to know more, see the Delphi TPBRecord or TCalRecord object's methods.

    IF YOU ARE A VISUAL BASIC PROGRAMMER AND YOU MADE A VB INCLUDE FILE
    FOR THE LIBRARY, PLEASE SEND IT TO ME, I'LL INCLUDE IT IN THE PACK!

  *** CREDITS:  r00t - I can experiment with his phone
                Hasas81 - For beta-testing with different type of cable
                Inj and Bodhi3 - For VB module

  (Best viewed in DOS, in Windows it's recomended to use a fixed width font)


 ============================================================================


  HISTORY OF PREVIOUS VERSIONS:
  -----------------------------

    1.0 -=- XX/XX/2002 -=- Basic functions (open/close port), NVM files
                           Anybody knows the release date?
    2.0 -=- 21/05/2002 -=- Added PhoneBook functions and lock clearing
                           Error codes revised and unified
    3.0 -=- 12/10/2002 -=- Scheduler (calendar) functions
                           PhoneBook & scheduler structures explained
    3.1 -=- 11/09/2004 -=- Fixed reading big (>21Kb) sections


 ============================================================================


  FUNCTIONS REFERENCE:
  --------------------

   The library provides only a few functions, but no more are requied. Here
 is a detail list of them.


 * InitGBPMode (TGBPControl.Init)

     This function to initializes the GBP mode by sending an AT command
   "AT-SGBP" to the specified port.

       PARAMETERS: PortNum = The serial port number (1=COM1, 2=COM2,...)

    RETURN VALUES: GBPE_NOERROR = Everything OK, GBP mode is initialized.
                   GBPE_ALREADYOPENED = The library is already accessed to
                       some port, it cannot use two ports at once. Call the
                       EndGBPMode first.
                   GBPE_CANNOTOPENPORT = Unable to access the specified
                       device (port). May be the bad port number (i.e.
                       it does not exist)
                   GBPE_NOATRESPONSE = The phone does not respond to AT
                       commands. May be it's not properly connected.


 * EndGBPMode (TGBPControl.Close)

     Use this to end the GBP mode and switch the mobile back to normal modem,
   data mode.

       PARAMETERS:       (none)

    RETURN VALUES: GBPE_NOERROR = Everything OK, GBP mode closed.
                   GBPE_MUSTBEOPENED = No port is open now, you can call
                       this only after successfully initialize of GBP mode.


 * ClearNVMFile (TGBPControl.ClearNVMFile)

     This deletes the specified NVM file. It's used primary before the
   the WriteNVMFile, because the NVM must be cleared before you can write
   the new contents, but it can be useful when you simply want to clear
   some entry, too.

       PARAMETERS:     Sec = The NVM file section number, together with
                    Subsec / the subsection number, describes the data
                             you want to clear/read/write. This parameters
                             are requied by all functions that is doing
                             some with the NVM files. Look at the end of
                             this file at a table with some known section/
                             /subsection pairs on J5.

    RETURN VALUES: GBPE_NOERROR = Everything OK, the file is successfully
                       cleared. This value is returned when the file is
                       already clean too.
                   GBPE_MUSTBEOPENED = The port is not open. You can call
                       this only with GBP mode properly initialized.
                   GBPE_COMMERROR = Communication error. This means that
                       some went wrong in the communication protocol, i.e.
                       wrong checksum, timeout, etc. or the specified
                       section/subsection does not exist.
                   -1xx = The status of GBP frame wasn't zero (OK). This may
                       occur when the specified section/subsection
                       does not exist, but it can mean anything.


 * ReadNVMFile (TGBPControl.ReadNVMFile)

     Use this to read a NVM file from the phone. The maximum length of one
   NVM file is about 21000 Bytes, but the output buffer can be smaller, due
   to type of data in the file.

       PARAMETERS:       Sec = The NVM section and subsection to read. See
                      Subsec / the ClearNVMFile function for details.
                      OutBuf = The output buffer to storing the received data.
                               Make sure it's large enough for the data (for
                               the bytes specified by the MaxLength parameter).
                   MaxLength = This is the maximum length of the data to
                               receive, i.e. the size of the destination
                               buffer. Use zero if you can handle any size
                               of the data (then you have to allocate
                               21000 Bytes).

    RETURN VALUES: =>0 = When the return value is larger than zero, it
                       is the real size of received NVM file. Zero means
                       zero-length section.
                   GBPE_MUSTBEOPENED = The GBP mode is not initialized. Call
                       the InitGBPMode function first.
                   GBPE_COMMERROR = Communication error. An error occured in
                       the GBP protocol (bad checksum etc.) or the specified
                       section/subsection does not exist.
                   GBPE_INVALIDARGS = Some argument passed to function is
                         invalid (the OutBuf is NULL).
                   -1xx = The status byte of the GBP frame wasn't zero. This
                       may means that the specified NVM file is clean or
                       does not exist.


 * WriteNVMFile (TGBPControl.WriteNVMFile)

     By calling this function you can write a new contents of the specified
   NVM file. Be careful when doing this, you can lock up your phone and it
   might need a complete firmware update.

       PARAMETERS:      Sec = The NVM section and subsection to write to.
                     Subsec / See the ClearNVMFile function for details.
                      InBuf = This is the pointer to buffer from which
                              will be taken the data for writing. Make sure
                              it's in a format in which the phone expects
                              the data in specified section.
                     Length = The length of the data to write. Cannot be
                              larger than 21000 Bytes. Don't try to write
                              more here!

    RETURN VALUES: GBPE_NOERROR = Everything OK, your data are now stored
                       in the phone.
                   GBPE_MUSTBEOPENED = No port is open. You have to
                        initialize the GBP mode on some port to call this.
                   GBPE_COMMERROR = An communication error occured (received
                        a bad GBP frame, no response, etc.) or the specified
                        section/subsection does not exist.
                   GBPE_INVALIDARGS = Some argument passed to function is
                        invalid (the InBuf is NULL).
                   -1xx = The status byte of the GBP frame wasn't zero. That
                        means an error in the parameters or the phone state.

 * GetNVMFileSize (TGBPControl.GetNVMSize)

     This function retrieves the size of specified section. It should be
   used for testing if some data is present (such as wallpapers) in the
   section. If you want the contents of NVM file, use ReadNVMFile instead.
   It does the same, then, if the size is greater than zero, reads the
   file's contents.

       PARAMETERS:      Sec = The NVM section and subsection to write to.
                     Subsec / See the ClearNVMFile function for details.

    RETURN VALUES: =>0 = When the return value is larger or equal to
                       zero, it is the size of specified NVM file.
                   GBPE_MUSTBEOPENED = The GBP mode isn't initialized.
                   GBPE_COMMERROR = Communication error. This means that
                       some went wrong in the communication protocol, i.e.
                       wrong checksum, timeout, etc. or the specified
                       section/subsection does not exist.
                   -1xx = The status of GBP frame wasn't zero (OK). May
                       occur when the specified section/subsection does
                       not exist, but can mean anything.


 * ClearLock (TGBPControl.ClearLock)

     This function simply clears the security lock code for accessing some
   options in mobile to factory value, i.e. 0000.

       PARAMETERS: (none)

    RETURN VALUES: GBPE_NOERROR = Everything OK, security lock is cleared.
                   GBPE_MUSTBEOPENED = The GBP mode have to be initialized.
                   GBPE_COMMERROR = Communication error occured.


 * ReadPBDirectory (TGBPControl.ReadPBDirectory)

     Use this function to read unique identification numbers of the whole
   phonebook (both in the phone and on the SIM card) to use with most of
   PB operations. The entries' identification numbers are stored in PBENTRY
   structure.

       PARAMETERS:        pbe = Pointer to array of PBENTRY structures, where
                                the loaded ID's will be stored.
                   MaxEntries = Maximum number of entries to store in the pbe
                                (in fact the size of your array). At the
                                moment the maximum size is hard-set to 800
                                in the DLL, so it's useless to allocate more.

    RETURN VALUES: =>0 = The return value is the number of entries loaded and
                       and stored in your buffer.
                   GBPE_MUSTBEOPENED = The GBP mode have to be initialized.
                   GBPE_COMMERROR = An communication error occured.
                   GBPE_INVALIDARGS = The specified pointer to the array is
                       NULL and so it's invalid.


 * ReadPBEntry (TGBPControl.ReadPBEntry)

     This function is used to read one phonebook from the mobile. The entry
   data are stored in PBRECORD structure. The record to read is specified
   by unique identifier got by ReadPBDirectory or CreatePBEntry.

       PARAMETERS: pbe = Pointer to PBENTRY structure containing the
                         identifier of record to load.
                   pbr = Pointer to destination PBRECORD structure, where
                         the read data will be stored.

    RETURN VALUES: GBPE_NOERROR = The record is read succesfully, no error.
                   GBPE_MUSTBEOPENED = The GBP mode have to be initialized.
                   GBPE_COMMERROR = Some communication error occured. May be
                                    the entry with specified ID does not
                                    exist in the phone.
                   GBPE_INVALIDARGS = Some of specified pointers are invalid.


 * WritePBEntry (TGBPControl.WritePBEntry)

     You use this to store the modified phonebook record to the phone, i.e.
   storing it instead of last entry with specified ID. A valid PBENTRY must
   be supplied. To make a new record, call CreatePBEntry function.

       PARAMETERS: pbe = Pointer to PBENTRY structure containing the
                         identifier of record to write to.
                   pbr = Pointer to source PBRECORD structure, from which
                         will be get the data to write.

    RETURN VALUES: GBPE_NOERROR = The record is written succesfully, no error.
                   GBPE_MUSTBEOPENED = The GBP mode have to be initialized.
                   GBPE_COMMERROR = An communication error occured. May be
                                    data in the PBRECORD structure is bad.
                   GBPE_INVALIDARGS = Some of specified pointers are invalid.


 * CreatePBEntry (TGBPControl.CreatePBEntry)

     To create a new phonebook entry, call this function. It will make a
   new record from the PBRECORD structure and returns it's ID in PBENTRY.

       PARAMETERS: pbe = Pointer to PBENTRY structure, in which will be
                         written the identifier of the created record.
                   pbr = Pointer to source PBRECORD structure.

    RETURN VALUES: GBPE_NOERROR = The record is created succesfully, no error.
                   GBPE_MUSTBEOPENED = The GBP mode have to be initialized.
                   GBPE_COMMERROR = An communication error occured. May be
                                    data in the PBRECORD structure is bad.
                   GBPE_INVALIDARGS = Some of specified pointers are invalid.


 * ErasePBEntry (TGBPControl.ErasePBEntry)

     Used to erase a phonebook entry with specified ID.

       PARAMETERS: pbe = Pointer to PBENTRY structure with identifier
                         of the entry to delete.

    RETURN VALUES: GBPE_NOERROR = The record has been erased, no error
                                  occured.
                   GBPE_MUSTBEOPENED = The GBP mode have to be initialized.
                   GBPE_COMMERROR = An communication error occured. May be
                                    the entry with specified ID does not
                                    exists in the phone.
                   GBPE_INVALIDARGS = The pbe pointer is invalid.


  * ReadCalDirectory (TGBPControl.ReadCalDirectory)

     You use this function to load identification structures for all
   scheduler entries stored in the phone (both events and to-dos).
   CALENTRY structures you'll get in the array by this function are
   used by all other calendar functions (except creating new entry).

       PARAMETERS:         ce = Pointer to array of CALENTRY structures
                                to which received IDs will be stored.
                   MaxEntries = This specifies the maximum number of
                                entry IDs store in the array (actually
                                the size of your array in items). At this
                                time this is set to 1000 internally, so it's
                                useless to allocate more in your programs.

    RETURN VALUES: =>0 = The return value is the number of entries loaded and
                         and stored in your array.
                   GBPE_MUSTBEOPENED = The GBP mode have to be initialized.
                   GBPE_COMMERROR = An communication error occured.
                   GBPE_INVALIDARGS = The specified pointer to the array is
                       NULL and so it's invalid.


  * ReadCalEntry (TGBPControl.ReadCalEntry)

     This function gets all data of a scheduler entry with specified ID
   from the phone.

       PARAMETERS: ce = This is a poninter to the CALENTRY structure which
                        identifies the entry to load. You obtain it by
                        calling ReadCalDirectory or CreateCalEntry.
                   cr = To CALRECORD structure at which points this
                        parameter will be filled with data from the phone.

    RETURN VALUES: GBPE_NOERROR = The record is read succesfully, no error.
                   GBPE_MUSTBEOPENED = The GBP mode have to be initialized.
                   GBPE_COMMERROR = Some communication error occured. May be
                                    the entry with specified ID does not
                                    exist in the phone.
                   GBPE_INVALIDARGS = Some of specified pointers are invalid.

  * WriteCalEntry (TGBPControl.WriteCalEntry)

     You use this to store the modified calendar event/to-do record back to
   the phone, i.e. storing it instead of current entry with specified ID.
   A valid CALENTRY structure which identifies the entry to replace must
   be supplied. To make a new entry, call CreateCalEntry function.

       PARAMETERS: ce = Pointer to CALENTRY structure containing the
                        identifier of record to modify.
                   cr = Pointer to source CALRECORD structure, from which
                        will be get the new data to write.

    RETURN VALUES: GBPE_NOERROR = The record is written succesfully, no error.
                   GBPE_MUSTBEOPENED = The GBP mode have to be initialized.
                   GBPE_COMMERROR = An communication error occured. May be
                                    data in the CALRECORD structure is bad.
                   GBPE_INVALIDARGS = Some of specified pointers are invalid.

 * CreateCalEntry (TGBPControl.CreateCalEntry)

     To create a new scheduler entry (To-Do or event), call this function.
   It will make a new record from the CALRECORD structure and stores the
   ID of the created record in specified CALENTRY structure.

       PARAMETERS: ce = Pointer to CALENTRY structure, to which will be
                        written the identifier of the created record.
                   cr = Pointer to CALRECORD structure with data about
                        the new entry.

    RETURN VALUES: GBPE_NOERROR = The record is created succesfully, no error.
                   GBPE_MUSTBEOPENED = The GBP mode have to be initialized.
                   GBPE_COMMERROR = An communication error occured. May be
                                    data in the CALRECORD structure are bad.
                   GBPE_INVALIDARGS = Some of specified pointers are invalid.

  * EraseCalEntry (TGBPControl.EraseCalEntry)

     This functions simply erases the calendar entry with ID in passed
   CALENTRY structure.

       PARAMETERS: ce = Pointer to CALENTRY structure with identifier
                        of the entry to delete.

    RETURN VALUES: GBPE_NOERROR = The record has been erased, no error
                                  occured.
                   GBPE_MUSTBEOPENED = The GBP mode have to be initialized.
                   GBPE_COMMERROR = An communication error occured. May be
                                    the entry with specified ID does not
                                    exists in the phone.
                   GBPE_INVALIDARGS = The ce pointer is invalid.


 ============================================================================


  STRUCTURES REFERENCE:
  ---------------------

   I will use standard Windows types according to following table:
     DWORD == unsigned long  == 32 bit unsigned number
      WORD == unsigned short == 16 bit unsigned number
      BYTE == unsigned char  == 8 bit unsigned number
     WCHAR == unsigned short == UNICODE (16-bit) character (arrays used as
                                null terminated strings)
      char == un/signed char == ASCII (8-bit) character (and strings)

  PBENTRY *** This structure identifies one PhoneBook entry in the phone.
   + DWORD UID - Both this and next structure fields is internally used
   |             by the phone. May be some of these is used to sort entries.
   + WORD  CID - See the comment above.
   + WORD  Loc - This number is the real location of the entry in the phone,
                 for in-phone entries it's linear, for SIM-card it specifies
                 the current position of that entry.

  PBRECORD *** In this structure you'll get data of each PhoneBook entry.
   + BYTE OnSIM - Set to true (1) if this entry is on the SIM card
   + BYTE Group - Group of this contact (different ringings and icons)
   + WORD HasName,HasNum1,HasNum2,HasNum3,HasNote1,HasNote2 - If there is a
   |              value of 1 in any of these fields, it means that data
   |              field for named thing contains a valid information.
   |              HasName should be always set and Name field filled.
   + WORD WordAlignPad - In this must be value 0xA2A1.
   + WCHAR Name[20] - Name of this entry (max. 20 UNICODE characters, if less
   |                  padded with zeroes).
   + PBNUMBER Num1,Num2,Num3 - Three additional numbers
   |  + BYTE Icon - Icon of this number (mobile/home/office/...)
   |  + char Number[41] - Number to call saved as ASCIIZ string, should
   |                      contain only numbers (0123456789) and +#*P
   |                      Maximum length is 40 characters WITHOUT
   |                      terminating null character (then stored in 41st
   |                      byte), padded with zeroes.
   + PBNOTE Note1,Note2 - Two additional notes
      + WORD Icon - Icon of this note.
      + WORD Length - Length of the note text (1-20).
      + WCHAR Text[40] - Note text stored as UNICODE string.

  CALENTRY *** This structure identifies one scheduler entry in the phone.
   + WORD UID - Unique identifier of the entry.
   + WORD Date - Date of this entry, bit-coded:
   |                bits 0..4 = Day
   |                bits 5..8 = Month
   |               bits 9..15 = Year (2000+x)
   + DWORD State - I don't know all about this field, I found only that
                     0 there means event,
                     1 is for completed To-Do and
                     2 stands for not completed yet To-Do.

  CALRECORD *** Contains full info about scheduler entry.
   + DWORD IsToDo - Set to TRUE (1) if this is a To-Do.
   + DWORD DateTimeFlags - Bit-coded value with date, time and flags:
   |    bits  0..6 = Year (2000+x)
   |         7..10 = Month
   |        11..15 = Day
   |        16..20 = Hour
   |        21..26 = Minute
   |        27..28 = Alarm type (0=Off, 1=On, 2=5 min.before, 3=1 day before)
   |            29 = n/a
   |            30 = To-Do completed
   |            31 = n/a
   + WORD Icon - Icon of this entry. Bad thing is that it's stored as
   |             internal (and I thing firmware dependent) icon ID.
   + WCHAR Text[21] - Text stored as UNICODE string, null terminated,
                      max. 20 characters long (it must end with null).

 ============================================================================

  ERROR CODES REFERENCE:
  ----------------------

     0 (GBPE_NOERROR)
         This code means that everything went OK and no error occured.

    -1 (GBPE_ALREADYOPENED)
         Returned from InitGBPMode function, this means that you have
       already initialized GBP mode on some port. The library cannot use
       more than one port at once.

    -2 (GBPE_CANNOTOPENPORT)
         Means that specified port cannot be opened. Probably it does not
       exist or it is opened by another application.

    -3 (GBPE_NOATRESPONSE)
         This error is returned when the phone does not respond to AT
       commands.

    -4 (GBPE_MUSTBEOPENED)
         This means that you tried to call some function that uses the
       GPB mode, but you haven't initialize it yet by InitGBPMode.

    -5 (GBPE_COMMERROR)
         Returned when some error occured in the communication protocol,
       mostly when received some packet with bad checksum, or no packet
       at all.

    -6 (GBPE_NOMEMORY)
         This code is returned when the system hasn't enough memory to
       allocate for buffering (used in some functions).

    -7 (GBPE_INVALIDARGS)
         You'll get this error when you pass some invalid arguments to
       a function, mainly specifing a NULL pointer as a Buffer.


 ============================================================================


     Here are some known section/subsection pairs of the NVM files in J5.
  If you've found the same for other models (J6,J7), please, send it to me.

    01-03 AL Animation                17-02 Recorded sound
    01-04 AL Magic Key                17-03 Recorded sound 1
    01-05 AL Reserved1                17-04 Recorded sound 2
    01-06 AL RTC                      17-05 Recorded sound 3
    01-07 AL T9 config Info           17-06 Recorded sound 4
    01-08 AL Reserved2                17-07 Load Melody
    01-09 AL In-Call List             17-08 Load Melody 1
    01-10 AL Out-Call List            17-09 Load Melody 2
    01-11 AL Miss-Call List           17-10 Load Melody 3
    01-12 AL Volume                   17-11 Load Melody 4
    01-13 AL Config IMSI              17-12 Load Melody 5
    01-14 AL Config                   17-13 Load Melody 6
    02-00 Wap Cookies                 17-14 Load Melody 7
    02-01 Wap Favorites               17-15 Load Melody 8
    02-02 Wap Sony1                   17-16 Load Melody 9
    02-03 Wap Email                   17-17 Load Melody 10
    02-04 Wap ISP                     17-18 Sand Art
    02-05 Wap Proxy                   18-00 PBFREE_ME?
    02-06 Wap Sony2                   18-01 PBFREE_SIM?
    02-07 Wap WAP?                    18-02 Scheduler To Do
    02-08 WAP Structure               18-03 Scheduler event
    16-02 Phone book card             18-04 Recorded Ringer
    16-03 Phone book name             18-05 Recorded Ringer 1
    16-04 Wallpaper                   18-06 Recorded Ringer 2
    16-07 Phone book sort             18-07 Recorded Ringer 3
    17-00 Scheduler sort              18-08 Recorded Ringer 4
    17-01 Phone book number
