# Watchdog Usage Guidelines
- [How to Use](#section0719414187)
- [Opening a Watchdog](#section198171379261)
- [Obtaining the Watchdog Status](#section206592910275)
- [Setting the Timeout Duration](#section19605128182714)
- [Obtaining the Timeout Duration](#section11111516208)
- [Starting a Watchdog](#section141174192814)
- [Feeding a Watchdog](#section179101435113910)
- [Stopping a Watchdog](#section15282123192816)
- [Closing a Watchdog](#section7857850173411)
## How to Use
[Figure 1](#fig19134125410189) illustrates the process of using a watchdog.
**Figure 1** Process of using a watchdog

## Opening a Watchdog
Use **WatchdogOpen** to open a watchdog. A system may have multiple watchdogs. You can open a specified watchdog by using the ID.
int32\_t WatchdogOpen\(int16\_t wdtId\);
.
**Table 1** Description of WatchdogOpen
Parameter
|
Description
|
wdtId
|
Watchdog ID.
|
Return Value
|
Description
|
NULL
|
Failed to open the watchdog.
|
DevHandle pointer
|
Pointer to the watchdog handle.
|
```
DevHandle handle = NULL;
handle = WatchdogOpen(0); /* Open watchdog 0.*/
if (handle == NULL) {
HDF_LOGE("WatchdogOpen: failed, ret %d\n", ret);
return;
}
```
## Obtaining the Watchdog Status
int32\_t WatchdogGetStatus\(DevHandle handle, int32\_t \*status\);
.
**Table 2** Description of WatchdogGetStatus
Parameter
|
Description
|
handle
|
Watchdog handle.
|
status
|
Pointer to the watchdog status.
|
Return Value
|
Description
|
0
|
The watchdog status is obtained.
|
Negative value
|
Failed to obtain the watchdog status.
|
```
int32_t ret;
int32_t status;
/* Obtain the watchdog status. */
ret = WatchdogGetStatus(handle, &status);
if (ret != 0) {
HDF_LOGE("WatchdogGetStatus: failed, ret %d\n", ret);
return;
}
```
## Setting the Timeout Duration
int32\_t WatchdogSetTimeout\(PalHandle \*handle, uint32\_t seconds\);
**Table 3** Description of WatchdogSetTimeout
Parameter
|
Description
|
handle
|
Watchdog handle.
|
seconds
|
Timeout duration, in seconds.
|
Return Value
|
Description
|
0
|
The setting is successful.
|
Negative value
|
Setting failed.
|
```
int32_t ret;
uint32_t timeOut = 60;
/* Set the timeout duration, in seconds. */
ret = WatchdogSetTimeout(handle, timeOut);
if (ret != 0) {
HDF_LOGE("WatchdogSetTimeout: failed, ret %d\n", ret);
return;
}
```
## Obtaining the Timeout Duration
int32\_t WatchdogGetTimeout\(PalHandle \*handle, uint32\_t \*seconds\);
**Table 4** Description of WatchdogGetTimeout
Parameter
|
Description
|
handle
|
Watchdog handle.
|
seconds
|
Pointer to the timeout duration, in seconds.
|
Return Value
|
Description
|
0
|
The timeout duration is obtained.
|
Negative value
|
Failed to obtain the watchdog status.
|
```
int32_t ret;
uint32_t timeOut;
/* Obtain the timeout duration, in seconds. */
ret = WatchdogGetTimeout(handle, &timeOut);
if (ret != 0) {
HDF_LOGE("WatchdogGetTimeout: failed, ret %d\n", ret);
return;
}
```
## Starting a Watchdog
int32\_t WatchdogStart\(DevHandle handle\);
**Table 5** Description of WatchdogStart
Parameter
|
Description
|
handle
|
Watchdog handle.
|
Return Value
|
Description
|
0
|
The watchdog is started.
|
Negative value
|
Failed to start the watchdog.
|
```
int32_t ret;
/* Start the watchdog. */
ret = WatchdogStart(handle);
if (ret != 0) {
HDF_LOGE("WatchdogStart: failed, ret %d\n", ret);
return;
}
```
## Feeding a Watchdog
int32\_t WatchdogFeed\(DevHandle handle\);
**Table 6** Description of WatchdogFeed
Parameter
|
Description
|
handle
|
Watchdog handle.
|
Return Value
|
Description
|
0
|
The watchdog is fed.
|
Negative value
|
Failed to feed the watchdog.
|
```
int32_t ret;
/* Feed the watchdog. */
ret = WatchdogFeed(handle);
if (ret != 0) {
HDF_LOGE("WatchdogFeed: failed, ret %d\n", ret);
return;
}
```
## Stopping a Watchdog
int32\_t WatchdogStop\(DevHandle handle\);
**Table 7** Description of WatchdogStop
Parameter
|
Description
|
handle
|
Watchdog handle.
|
Return Value
|
Description
|
0
|
The watchdog is stopped.
|
Negative value
|
Stopping the watchdog failed.
|
```
int32_t ret;
/* Stop the watchdog. */
ret = WatchdogStop(handle);
if (ret != 0) {
HDF_LOGE("WatchdogStop: failed, ret %d\n", ret);
return;
}
```
## Closing a Watchdog
If the watchdog is no longer required, call **WatchdogClose** to close the watchdog handle.
void WatchdogClose\(DevHandle handle\);
**Table 8** Description of WatchdogClose
Parameter
|
Description
|
handle
|
Watchdog handle.
|
```
/* Close the watchdog. */
ret = WatchdogClose(handle);
```