> For the complete documentation index, see [llms.txt](https://sandbox-docs.verifone.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sandbox-docs.verifone.com/adk-5.0-programmers-guide/readme/files/dir_44d926a17fc606d3c25449ba60427c63/dir_048f823fee1bfb22502a8732b9a001aa/main_8h.md).

# main.h

[Functions](#func-members)

`#include <vector>` `#include <string>` `#include <pthread.h>`

Include dependency graph for main.h:

![](/files/Dz9yqvQL0yNSib6B0g4i)

This graph shows which files directly or indirectly include this file:

![](/files/1ajzxDHZTeZo0ARE6Leh)

[Go to the source code of this file.](/adk-5.0-programmers-guide/main_8h_source.md)

|                |                                                                                                                                         |
| -------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| Functions      |                                                                                                                                         |
| void           | [dispatch](#afdcd133c96d68afb81d874f88786367e) (std::vector< unsigned char > \&cmd)                                                     |
| bool           | [process\_side\_command](#a4892e1b795462119ef4d6fa87c860b90) (unsigned char \*cmd, unsigned size)                                       |
| unsigned short | [check\_mac\_decrypt](#ab39e67ca68823580152fc861898a77da) (unsigned char \*cmd, unsigned \&cmdSize, std::vector< unsigned char > \&out) |
| void           | [pm\_setCriticalSection](#a11c5a1b173cf19d42db73a73a1ad6d90) (bool enable)                                                              |
| bool           | [pm\_criticalSection](#a2640a9e85a15b5c3a3e800e532939c67) ()                                                                            |
| void           | [sdi\_exit](#aeeb65deaf7e0b30961e4cb56c8105b41) (int status)                                                                            |

### FunctionDocumentation <a href="#function-documentation" id="function-documentation"></a>

### check\_mac\_decrypt() <a href="#ab39e67ca68823580152fc861898a77da" id="ab39e67ca68823580152fc861898a77da"></a>

unsigned short check\_mac\_decrypt

The function is invoked by SDI connection thread (such as invoking [mADK\_PP\_Prot::set\_command()](/adk-5.0-programmers-guide/readme/annotated/classm_a_d_k___p_p___prot.md#ab390ac45aaaa013d33bcef40d625f142)) to check for an incomming SDI command, if security options in P1 field were enabled. In case with bit 0x40 set: The function calculates the MAC from incomming command and checks if it equals with MAC appended to command. After successful MAC check, the MAC is removed from command tail and not returned in output vector *out*. IN case with bit 0x80 set: The function decrypts the command and provides the plain text command data in output vector *out*. Both option bits can be combined.

**Parameters**

\[in] **cmd** pointer to origin incomming command buffer, which might be encrypted or appended with a MAC. Pointer cmd is allowed to point to \&out\[0], but please note that out.size() must be at least cmdSize in this case. \[in,out] **cmdSize** in: size of the incomming command, cmdSize (size of buffer cmd) must be at least 4 bytes! out: size of valid data returned in output vector out. Note: The real command size is always provided with cmdSize (out.size() might be creater!) \[in,out] **out** decrypted data, always without MAC For incomming vector with out.size() > 0, this size is least kept. This is to keep pre-allocated command vector for dispatch(), which uses this function.

#### Returns

error code 0x9000 if all operations were successful or no security options enabled, else another error code is returned, e.g. 0x6FB9 (decryption error)

{% hint style="info" %}
Invocation of [check\_mac\_decrypt()](#ab39e67ca68823580152fc861898a77da) was moved to [mADK\_PP\_Prot::set\_command()](/adk-5.0-programmers-guide/readme/annotated/classm_a_d_k___p_p___prot.md#ab390ac45aaaa013d33bcef40d625f142), otherwise [dispatch()](#afdcd133c96d68afb81d874f88786367e) would to it a second time after [process\_side\_command()](#a4892e1b795462119ef4d6fa87c860b90), when using protocol type D. See also add\_mac\_encrypt(), which is invoked by [dispatch()](#afdcd133c96d68afb81d874f88786367e) and [process\_side\_command()](#a4892e1b795462119ef4d6fa87c860b90) to handle secure responses.
{% endhint %}

### dispatch() <a href="#afdcd133c96d68afb81d874f88786367e" id="afdcd133c96d68afb81d874f88786367e"></a>

void dispatch

Function to process a command with the SDI main thread synchronously. The function provides the full SDI function scope (e.g. including payment support with ADKEMV, ADKNFC and ADKMSR). Most subfunctions invoked by [dispatch()](#afdcd133c96d68afb81d874f88786367e) are CPU bound or might block, e.g. to wait for user input with UI, thus, the are processed by the a working thread, the SDI main thread. Function [dispatch()](#afdcd133c96d68afb81d874f88786367e) cannot be invoked by multiple threads simultanously. A second thread invoking it at the same time is blocked by a mutex, which will lead to delayed processing consequently (as long as the previous thread hasn't finshed processing) or finally to a processing timeout. Since the used mutex allowes recursive locking with the same thread, it is possible to use [dispatch()](#afdcd133c96d68afb81d874f88786367e) for processing a nested command from within an active SDI callback, if this was invoked by the SDI main thread. Other sibling threads (created by the SDI main thread) will only be able the invoke [dispatch()](#afdcd133c96d68afb81d874f88786367e), if the SDI main thread has unlocked the mutex previously (e.g. as implemented for command Card Detection 23-01).

{% hint style="info" %}
SDI connection threads (such as invoking [mADK\_PP\_Prot::set\_command()](/adk-5.0-programmers-guide/readme/annotated/classm_a_d_k___p_p___prot.md#ab390ac45aaaa013d33bcef40d625f142)) must never invoke function [dispatch()](#afdcd133c96d68afb81d874f88786367e), since these threads are fully decoupled from SDI main thread and must use [process\_side\_command()](#a4892e1b795462119ef4d6fa87c860b90) instead. Even SDI allows using [dispatch()](#afdcd133c96d68afb81d874f88786367e) for asynchronous commands with protocol type 'D' over the main connection, the related connection thread stores the command into command buffer, so that the main thread (calling [mADK\_PP\_Prot::receive()](/adk-5.0-programmers-guide/readme/annotated/classm_a_d_k___p_p___prot.md#ab2e55bafa77bcbdbeda4258ecadd5357) in idle loop) can take ownership to process the command synchronously. Thus, a second simultaneous protocol type 'D' command on the main connection is declined with a busy response 640A, while the previous command is already processed by the main thread.
{% endhint %}

\*\*Parameters\*\*

**cmd** vector as buffer storing the command to be processed

### pm\_criticalSection() <a href="#a2640a9e85a15b5c3a3e800e532939c67" id="a2640a9e85a15b5c3a3e800e532939c67"></a>

bool pm\_criticalSection

check if SDI has entered critical PM section with [pm\_setCriticalSection()](#a11c5a1b173cf19d42db73a73a1ad6d90).

#### Returns

true if SDI has entered critical PM section, else false.

### pm\_setCriticalSection() <a href="#a11c5a1b173cf19d42db73a73a1ad6d90" id="a11c5a1b173cf19d42db73a73a1ad6d90"></a>

void pm\_setCriticalSection

Enter/leave critical section of power management (PM). If true is passed for parameter *enable*, it invokes [sys\_CriticalSectionEnter()](/adk-5.0-programmers-guide/readme/modules/group___power/namespacevfisyspm.md#ac46060410ba78b557470d51eb9c90feb), in case of false [sys\_CriticalSectionExit()](/adk-5.0-programmers-guide/readme/modules/group___power/namespacevfisyspm.md#a1fe354c573d66613fbae37897509cf04) is called. For more details please refer to documentation of ADKPM.

**Parameters**

\[in] **enable** true to enter critical PM section false to leave critical PM section

### process\_side\_command() <a href="#a4892e1b795462119ef4d6fa87c860b90" id="a4892e1b795462119ef4d6fa87c860b90"></a>

bool process\_side\_command

Function to process a command as a side command, which was triggered by a side connection with protocol type 'C' asynchronously or by an asynchronous command with protocol type 'D'. The function is invoked by SDI connection thread (such as invoking [mADK\_PP\_Prot::set\_command()](/adk-5.0-programmers-guide/readme/annotated/classm_a_d_k___p_p___prot.md#ab390ac45aaaa013d33bcef40d625f142)), thus, these threads are fully decoupled from SDI main thread processing (see function [dispatch()](#afdcd133c96d68afb81d874f88786367e)). Subfunctions invoked by [process\_side\_command()](#a4892e1b795462119ef4d6fa87c860b90) are invoked simultaneously, thus, they must be implemented thread-safe!!! If the command is not supported to be executed as a side command, the function returns false, which means that the command requires execution of [dispatch()](#afdcd133c96d68afb81d874f88786367e) with the SDI main thread. Side commands just have restricted access to SDI function scope and do not support intermediate messages like requests (callbacks) or aborts, they just use internal function [mADK\_PP\_Prot::send()](/adk-5.0-programmers-guide/readme/annotated/classm_a_d_k___p_p___prot.md#a1f13b08d59a38adeb5d755179befa6b8) to reply the command. Therefore, side commands are usually short-term in nature.

{% hint style="info" %}
This function is not allowed to use protocol function like [mADK\_PP\_Prot::receive()](/adk-5.0-programmers-guide/readme/annotated/classm_a_d_k___p_p___prot.md#ab2e55bafa77bcbdbeda4258ecadd5357), [mADK\_PP\_Prot::command()](/adk-5.0-programmers-guide/readme/annotated/classm_a_d_k___p_p___prot.md#a345e47376ab38e76aa77b2bd774b104d), [mADK\_PP\_Prot::request()](/adk-5.0-programmers-guide/readme/annotated/classm_a_d_k___p_p___prot.md#a1630f914c3dbf9ebc62ad6a5c7f9ebd9) or [mADK\_PP\_Prot::recv\_response()](/adk-5.0-programmers-guide/readme/annotated/classm_a_d_k___p_p___prot.md#a0bb666307a7aa15fc7aae63d454e4394). These APIs are only allowed for SDI application threads (main thread and siblings), which process commands received on the main connection (see function [dispatch()](#afdcd133c96d68afb81d874f88786367e)).
{% endhint %}

\*\*Parameters\*\*

\[in] **cmd** pointer to command buffer \[in] **size** size of command buffer

#### Returns

true if command was executed and rsp contains a response, false in case invocation is not allowed by a side command and requires execution of [dispatch()](#afdcd133c96d68afb81d874f88786367e) with the SDI main thread.

### sdi\_exit() <a href="#aeeb65deaf7e0b30961e4cb56c8105b41" id="aeeb65deaf7e0b30961e4cb56c8105b41"></a>

void sdi\_exit

depending on platform SDI must call exit() or \_exit(). This function ensures to use the correct variant, therefore, it shall be used instead of using functions of stdlib directly.

**Parameters**

\[in] **status** exit code of SDI


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://sandbox-docs.verifone.com/adk-5.0-programmers-guide/readme/files/dir_44d926a17fc606d3c25449ba60427c63/dir_048f823fee1bfb22502a8732b9a001aa/main_8h.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
