# Logging

The SDT features a logging mechanism allowing to record devices interaction over the day. Data are stored in the filesystem and can be accessed over the air. A gateway can access those data. A reference implementation is provided and described in the [Gateway](gateway.html) of this document.

## GATT interface

The logging service is the following:

|Type    | Access| UUID                                | Description           |
|-------:|------:|------------------------------------:|----------------------:|
|Service | N.A.  |3112fb86-86c7-11ea-bc55-0242ac130003 | Logging service       |
|Property| Read  |3112fb86-86c7-11ea-bc55-000000000003 | ZONE_X_LOG            |
|Property| Write |3112fb86-86c7-11ea-bc55-000000000004 | ZONE selection        |
|Property| Write |3112fb86-86c7-11ea-bc55-000000000005 | PTR_CONFIG            |
|Property| Write |3112fb86-86c7-11ea-bc55-000000000006 | FLD_RESET             |

Zone enumeration:

| Name    | Value |
|--------:|------:|
| Danger  | 0     |
| Warning | 1     |
| Coarse  | 2     |
| Limit   | 3     |

In order to read the logs for a specific zone, the gateway should issue a write to the
ZONE selection characteristic then a read to the ZONE_X_LOG characteristic. The SDT
will then send as many data as there are in the log file for that specific zone. The 
transmission will be terminated by the transmission of a packet of length zero. In the
case the transmission does not complete, the gateway can reinitialize the transmission 
with by writing zero in the PTR_CONFIG characteristic. A write in the FLD_RESET characteristic
will clear the log file for the selected zone.

## Logging format

The logging format is highly flexible as it is implemented through a set of macros in the application
leaving freedom for the end application to modify it at will. For each event , the reference
implementation logs the using the format described in the following table.

```eval_rst
+--------+-----------------+-------------------------------+---------------------+-------------+
| Byte   | Item            | Detail                        | Data Type           | Byte Order  |
+========+=================+===============================+=====================+=============+
| 0      | Log Entry Start | 0x7C "|"                      | ASCII char/ uint8_t |             |
+--------+-----------------+-------------------------------+---------------------+-------------+
| 1      | SDT Type        | | 0 – Wearable                |                     |             |
|        |                 | | X – Reserved                |                     |             |
+--------+-----------------+-------------------------------+---------------------+-------------+
| 2      | SDT ID          |                               | uint32_t            | LSB         |
+--------+                 +                               +                     +-------------+
| 3      |                 |                               |                     |             |
+--------+                 +                               +                     +-------------+
| 4      |                 |                               |                     |             |
+--------+                 +                               +                     +-------------+
| 5      |                 |                               |                     | MSB         |
+--------+-----------------+-------------------------------+---------------------+-------------+
| 6      | Event Year      | e.g.2020                      |uint16_t             | LSB         |
+--------+                 +                               +                     +-------------+
| 7      |                 |                               |                     | MSB         |
+--------+-----------------+-------------------------------+---------------------+-------------+
| 8      | Event Month     | e.g. 4 for April              | uint8_t             |             |
+--------+-----------------+-------------------------------+---------------------+-------------+
| 9      | Event Day       | 25                            | uint8_t             |             |
+--------+-----------------+-------------------------------+---------------------+-------------+
| 10     | Event Hour      | 13                            | uint8_t             |             |
+--------+-----------------+-------------------------------+---------------------+-------------+
| 11     | Event Minute    | 37                            | uint8_t             |             |
+--------+-----------------+-------------------------------+---------------------+-------------+
| 12     | Event Seconds   | 37                            | uint8_t             |             |
+--------+-----------------+-------------------------------+---------------------+-------------+
| 13     | Zone            | | 0 - Danger                  | uint8_t             |             |
|        |                 | | 1 – Warning                 |                     |             |
|        |                 | | 2 – Coarse                  |                     |             |
|        |                 | | 3 – Limit                   |                     |             |
+--------+-----------------+-------------------------------+---------------------+-------------+
| 14     | Remote SDT Type | | 0 – Wearable                |                     |             |
|        |                 | | X – Reserved                |                     |             |
+--------+-----------------+-------------------------------+---------------------+-------------+
| 15     | Remote SDT ID   |                               | uint32_t            | LSB         |
+--------+                 +                               +                     +-------------+
| 16     |                 |                               |                     |             |
+--------+                 +                               +                     +-------------+
| 17     |                 |                               |                     |             |
+--------+                 +                               +                     +-------------+
| 18     |                 |                               |                     | MSB         |
+--------+-----------------+-------------------------------+---------------------+-------------+
| 19     | Local Battery % |                               | uint8_t             |             |
+--------+-----------------+-------------------------------+---------------------+-------------+
```
