# Developer documentation

## Backend services

The backend service is responsible for storing received messages and giving the SMSD core messages to send. It is solely up to them how the message will be stored, for example currently Gammu includes backends to store messages on filesystem (Files backend), various databases (MySQL Backend, PostgreSQL Backend, DBI Backend) or backend which does not store anything at all (Null Backend).

### Backend interface

Each backend service needs to support several operations, which are exported in `GSM_SMSDService` structure:

GSM_Error (*Init)(GSM_SMSDConfig *Config)

  Initializes internal state, connect to backend storage.

  Param Config:

    Pointer to SMSD configuration data

  Return:

    Error code.

GSM_Error (*Free)(GSM_SMSDConfig *Config)

  Freeing internal data, disconnect from backend storage.

  Param Config:

    Pointer to SMSD configuration data

  Return:

    Error code.

GSM_Error (*InitAfterConnect)(GSM_SMSDConfig *Config)

  Optional hook called after SMSD is connected to phone, can be used for storing information about phone in backend.

  Param Config:

    Pointer to SMSD configuration data

  Return:

    Error code.

GSM_Error (*SaveInboxSMS)(GSM_MultiSMSMessage *sms, GSM_SMSDConfig *Config, GSM_StringArray *Locations, GSM_StringArray *SentIDs)

  Saves message into inbox.

  Param sms:

    Message data to save

  Param Config:

    Pointer to SMSD configuration data

  Param Locations:

    Array with IDs identifying saved messages.

  Param SentIDs:

    Per-message array with IDs of sent messages matched to delivery reports.

  Return:

    Error code.

GSM_Error (*FindOutboxSMS)(GSM_MultiSMSMessage *sms, GSM_SMSDConfig *Config, char *ID)

  Finds message in outbox suitable for sending.

  Param sms:

    Found outbox message will be stored here

  Param Config:

    Pointer to SMSD configuration data

  Param ID:

    Identification of found message will be stored here, this should be unique for different message, so that repeated attempts to send same message can be detected by SMSD core. Empty string avoids this check.

  Return:

    Error code.

GSM_Error (*MoveSMS)(GSM_MultiSMSMessage *sms, GSM_SMSDConfig *Config, char *ID, gboolean alwaysDelete, gboolean sent)

  Moves sent message from outbox to sent items.

  Param sms:

    Message which should be moved, backend usually can get it by ID as well.

  Param Config:

    Pointer to SMSD configuration data.

  Param ID:

    Identification of message to be moved.

  Param alwaysDelete:

    Whether to delete message from outbox even if moving fails.

  Param sent:

    Whether message was sent (`TRUE`) or there was a failure (`FALSE`).

  Return:

    Error code.

GSM_Error (*CreateOutboxSMS)(GSM_MultiSMSMessage *sms, GSM_SMSDConfig *Config, char *NewID)

  Saves message into outbox queue.

  Param sms:

    Message data to save

  Param Config:

    Pointer to SMSD configuration data

  Param NewID:

    ID of created message will be stored here.

  Return:

    Error code.

GSM_Error (*AddSentSMSInfo)(GSM_MultiSMSMessage *sms, GSM_SMSDConfig *Config, char *ID, int Part, GSM_SMSDSendingError err, int TPMR)

  Logs information about sent message (eg. delivery report).

  Param sms:

    Message which should be moved, backend usually can get it by ID as well.

  Param Config:

    Pointer to SMSD configuration data

  Param ID:

    Identification of message to be marked.

  Param Part:

    Part of the message which is being processed.

  Param err:

    Status of sending message.

  Param TPMR:

    Message reference if available (TPMR).

  Return:

    Error code.

GSM_Error (*RefreshSendStatus)(GSM_SMSDConfig *Config, char *ID)

  Updates sending status in service backend.

  Param Config:

    Pointer to SMSD configuration data

  Param ID:

    Identification of message to be marked.

  Return:

    Error code.

GSM_Error (*RefreshPhoneStatus)(GSM_SMSDConfig *Config)

  Updates information about phone in database (network status, battery, etc.).

  Param Config:

    Pointer to SMSD configuration data

  Return:

    Error code.

GSM_Error (*ReadConfiguration)(GSM_SMSDConfig *Config)

  Reads configuration specific for this backend.

  Param Config:

    Pointer to SMSD configuration data

  Return:

    Error code.

### Message ID

You might have noticed that message ID is often used in the API. The primary reason for this is that it is usually easier for backend to handle message just by it’s internal identification instead of handling message data from `GSM_MultiSMSMessage`.

If the backend does not use any IDs internally, it really does not have to provide them, with only exception of `GSM_SMSDService.FindOutboxSMS`, where ID is used for detection of repeated sending of same message.

The lifetime of ID for sent message:

> - `GSM_SMSDService.CreateOutboxSMS` or direct manipulation with backend storage creates new ID

> - `GSM_SMSDService.FindOutboxSMS` returns ID of message to process

> - `GSM_SMSDService.AddSentSMSInfo` and `GSM_SMSDService.RefreshSendStatus` are then notified using this ID about sending of the message

> - `GSM_SMSDService.MoveSMS` then moves the message based on ID to sent items

The lifetime of ID for incoming messages:

> - `GSM_SMSDService.SaveInboxSMS` generates the message

> - RunOnReceive Directive uses this ID

## Message Sending Workflow

## Message Receiving Workflow