Parsing a SyncML Document


SyncML®: Synchronizing and Managing Your Mobile Data
By Uwe Hansmann, Riku Mettälä, Apratim Purakayastha, Peter Thompson, Phillipe Kahn
Table of Contents
Chapter 10.  SyncML API and Reference Implementation

To parse an incoming SyncML document, the application needs to initialize a toolkit instance with an assigned workspace buffer and set the instance options (type of ending, workspace size, and workspace name). For parsing documents, it is especially important that the application registers callback functions for different SyncML commands. The callback functions connect the toolkit to the application. Without the callback functions, the toolkit is not able to pass the parsed data to the application.

 static Ret_t handleAddCmdFunc (InstanceID_t    instanceID, VoidPtr_t userData, AddPtr_t param) { // Process received data here (e.g. parse user data and // insert in database ) ... // Free memory allocated to the toolkit smlFreeProtoElement(param); return SML_ERR_OK; } 

The code snippet above shows what a callback function handling a received Add command could look like. The toolkit is passing the payload data to the application. After the data is processed, the application has to release the memory that was allocated by the toolkit to pass the data.

 // This sample registers the callback functions // Define and initialize required variables InstanceID_t      instanceID = 0; Callbacks_t       callbacks; InstanceOptions_t options; // Set callback functions callbacks.addCmdFunc   = &handleAddCmdFunc; callbacks.alertCmdFunc = ... // Continue till all callback functions are set ... // Set instance options instanceOptions.encoding  = SML_WBXML; instanceOptions.workspaceName = "MyWorkspace"; instanceOptions.workspaceSize = 40000; // Initialize the Instance with options as set // above _err = smlInitInstance(&callbacks, &instanceOptions,           &instanceID); // Return in case any error occurred if (_err != SML_ERR_OK) return _err; 

The callback functions from the previous code snippet are registered with the toolkit at the time the instance is created.

The next step is to lock the buffer for exclusive use by the application, using smlLockWriteBuffer. The amount of memory left in the workspace is returned by this function. Another way to do this would be to call smlGetFreeBuffer. The application can now store the received SyncML document from the incoming communication buffer into the referenced workspace buffer using smlLibMemcpy, a macro provided by the toolkit that hides the platform dependent differences. With smlUnlockWriteBuffer, the application unlocks the workspace and returns the control to the toolkit. The size of the data copied into the workspace has to be passed to the toolkit using one of the parameters of smlUnlockWriteBuffer.

Now everything is ready to start parsing the data with smlProcessData: The toolkit can now parse and dispatch the XML code stored in the workspace buffer. For each identified command, SyncML calls the corresponding callback function supplied by the application. This way, the application is able to handle each synchronization command in a customized manner. A parameter of the smlProcessData function defines if only one command should be executed at a time or if all commands should be parsed and dispatched at once.

Figure 10-6. Example API sequence for receiving and parsing a SyncML document


 // Lock the buffer for exclusive use. // The pointer to the memory is returned in pdData, // the available amount of memory in cbDataSize err = smlLockWriteBuffer (instanceID, &pbData,          &cbDataSize); // This is one of the transport functions provided // with the toolkit. // Conn is the handle to the connection // The number of bytes received is returned in // cbReceived iXptRc = xptReceiveData (conn, (MemPtr_t) pbData,             cbDataSize, &cbReceived); // Unlock the workspace smlUnlockWriteBuffer (instanceID, (int)cbReceived; 

This code snippet above shows how the workspace buffer is locked. In the next step, the data received is directly written into the workspace. Finally, the workspace is released and the toolkit informed how many bytes were written into the workspace.

After the smlProcessData function is returned, the application can leave the instance idle or terminate it using smlTerminateInstance.


    SyncML. Synchronizing and Managing Your Mobile Data
    SyncML: Synchronizing and Managing Your Mobile Data
    ISBN: 0130093696
    EAN: 2147483647
    Year: 2001
    Pages: 124 © 2008-2017.
    If you may any questions please contact us: