Device Parameters Download (CSV / emulator XML)

Overview

The endpoint POST /iot-webservice/iotw/Device/parameters/download returns the parameter tree of one device as a file. The Angular portal uses it for the Save parameters button in Device update > Advanced view (DEV-2786, backend part DEV-3621).

The file holds the values that the ACS has stored. These are the same values that the Advanced view shows. The endpoint does not read the device.

Two formats exist:

  • CSV, for every protocol.

  • XML, in the format of the Friendly emulator that matches the protocol of the device.

The file contains secret parameters as decrypted plaintext. The Advanced view shows the same plaintext. An emulator that loads the file receives the credentials of the device. Every user who can open the device page can take these credentials away in a file. Handle the file as a secret.

Exception: the values of an LwM2M multi-instance (array) parameter are written as stored. An encrypted value in such an array stays encrypted.

Request and response

Item Value

Method and path

POST /iot-webservice/iotw/Device/parameters/download

Header

The authentication header, the same as for POST /Device/parameters.

Request body

JSON with the fields in the next table.

Access

An authenticated user whose domain contains the device. This is the rule of the device page.

Field Type Required Description

deviceId

number

yes

Identifier of the device.

format

string

yes

CSV or XML. The value is not case-sensitive. The service removes leading and trailing spaces.

Example request:

{ "deviceId": 123, "format": "CSV" }

A successful response has status 200 and these headers:

Header Value

Content-Type

text/csv;charset=UTF-8 for CSV, application/xml;charset=UTF-8 for XML.

Content-Disposition

attachment; filename="Parameters_<serial>_<yyyyMMdd_HHmmss>.<csv|xml>"

The body holds the file bytes in UTF-8, without a byte order mark. The browser can read Content-Disposition because the security configuration exposes it.

File name

  • The time stamp uses UTC.

  • The service replaces every character of the serial number outside A-Z a-z 0-9 . _ - with _.

  • If the serial number is empty or missing, the name part is device<deviceId>, for example device123.

Example: Parameters_SN-1_20261124_101010.csv.

Errors

An error response has the status below and a JSON body in the standard ErrorDetails shape. The service checks the conditions in the order of the table. The service reads no device data before the format is valid. The service reveals no protocol for a device outside the domain of the user.

Order Status and code Name Cause and content

1

400 / 9117

DEVICE_ID_REQUIRED

deviceId is missing. The message text is "A device ID is required for this operation."

2

400 / 9250

PARAMETERS_EXPORT_FORMAT_UNSUPPORTED

format is missing, blank or not CSV or XML. The message text is "Export format is not supported. Use CSV or XML." The description is "Parameters export format '<sent value>' is not supported". The sent value is cut to 32 characters.

3

500 / 9018

DEVICE_DOMAIN_DENIED

The device is outside the domain of the user. The description names the device identifier. This is the code and status of the device page. The status is 500 on purpose: a 401 or 403 status logs the user out.

3

404 / 9024

DEVICE_NOT_FOUND

The device identifier is unknown.

4

400 / 9251

PARAMETERS_XML_EXPORT_PROTOCOL_UNSUPPORTED

The format is XML and the device protocol has no emulator format. The message text is "XML export is not available for this device protocol. Download the parameters as CSV instead." The description is "Parameters XML export is not supported for protocol '<PROTOCOL NAME>'". CSV works for these protocols.

Notes:

  • The i18n texts of 9250 and 9251 have no placeholder. The variable value travels only in description.

  • The Russian texts are in service_errors_ru.properties.

  • A missing or malformed JSON body returns status 500 with the code INTERNAL_ERROR. The global exception handler does this, and this feature adds no mapping.

  • A missing or invalid authentication token returns status 403 from the security filter. The body is not an ErrorDetails JSON.

CSV format

The CSV file follows the layout of the .NET portal export.

  • The file has no header row.

  • Each row is <name>,<value>.

  • An object row has an empty value: <name>,. The name of an object ends with a dot.

  • A row ends with an LF character, including the last row.

  • The separator is a comma. You cannot configure it.

  • Rows are sorted by full name, case-insensitive. Names that differ only in case keep a fixed order by their exact characters.

  • A null value is an empty cell.

  • An empty tree gives a file of 0 bytes.

Quoting

A cell is wrapped in " if it contains a quote, CR, LF, TAB, a space, a comma or a semicolon. A quote inside a quoted cell is doubled. The rule applies to the name cell and to the value cell.

Formula neutralization

A spreadsheet can run a cell that starts with a formula character. The service therefore changes both cells before it quotes them.

Condition Change

The cell starts with =, +, -, @, TAB or CR, and is not a plain number.

The service adds a leading '.

The cell starts with U+FEFF (the byte order mark character).

The service adds a leading '.

The cell contains ; followed by =, +, -, @, ", TAB or CR.

The service inserts ' after each such ;. This protects spreadsheets that split cells on ;.

The cell contains CR or LF followed by =, +, -, @, ", TAB or CR.

The service inserts ' after each such line break, in the same way as after ;. A spreadsheet starts a new row after a line break inside a quoted cell. In a CR LF pair the ' goes after the LF.

The cell contains TAB followed by =, +, -, @, ", TAB or CR.

The service inserts ' after each such TAB, numbers included. This protects a TAB-delimited import: it opens a quoted cell only at the start of a field, so the text after a TAB inside a quoted cell starts a new cell. For example, x TAB =1+1 becomes x TAB '=1+1, x TAB -5 becomes x TAB '-5, and in the mid-cell run x TAB TAB =1 each TAB gets the '. A standalone cell that starts with TAB also gets a leading ': \t\t=1 becomes '\t'\t'=1.

A plain number is a value such as -5, -0.5, 1e5 or -.5. It stays unchanged in a value cell. A parameter name cell never gets this exception: a name that starts with =, +, -, @, TAB or CR always gets the leading ', even if it looks like a number. A value such as -1+1 or =1+1 is not a plain number and gets the '.

A changed cell is therefore not identical to the stored value: the ' can be at the start of the cell or after a ;, TAB, CR or LF inside it. Keep this in mind if you import the CSV into another system.

The service checks only the characters in the table. It does not protect a reader that skips leading spaces, NUL characters or other zero-width characters, for example U+200B, before it looks for a formula character. A cell such as ` -1.5` with a leading space stays unchanged.

The ' is a literal character in the opened spreadsheet cell, not a hidden text prefix. A value such as +15551234 or -70 dBm therefore appears with a leading apostrophe.

The service never writes a BOM itself. A spreadsheet drops U+FEFF at the very start of a file and reads the next character as the start of the first cell, so a cell that starts with U+FEFF gets the leading '.

The file is comma-separated UTF-8 without a BOM, as the old .NET export. Excel in RU or DE locales may put each row into one column when you open the file by double-click. It may also show non-ASCII text wrongly. Use Data > From Text/CSV, then select UTF-8 and the comma delimiter.

Example for a TR-181 device:

Device.,
Device.DeviceInfo.,
Device.DeviceInfo.Manufacturer,"My Company"
Device.DeviceInfo.UpTime,8640

XML format

The XML file is a device tree for one of four Friendly emulators. The device protocol selects the format.

Device protocol XML format Target

TR-098, TR-181, or no stored protocol

TR069

TR-069 emulator, "Parameter Trees Directory"

USP

IOT_USP

IoT emulator, "Device tree (XML)"

LwM2M

IOT_LWM2M

IoT emulator, "Device tree (XML)"

MQTT

IOT_MQTT

IoT emulator, "Device tree (XML)"

OMA, UNKNOWN, NOT_SET, TR-069

none

Error 9251

A device with no stored protocol is treated as TR-098, the same as in the Advanced view.

The writer uses these rules for every format:

  • A character outside the XML 1.0 character set becomes U+FFFD. The service counts these characters in its log line.

  • In element text, &, <, > and CR are escaped.

  • In an attribute, &, <, >, ", ', TAB, LF and CR are escaped.

  • A null value gives an empty value.

TR069

The file has the root element <device>. It lists leaf parameters only. An object with no parameters does not appear. Each leaf has the name, the value, the stored type and the writable flag. If the type is empty, the type is string.

<?xml version="1.0" encoding="UTF-8"?>
<device>
  <parameter><name>Device.DeviceInfo.Manufacturer</name><value>My Company</value><type>string</type><writable>false</writable></parameter>
  <parameter><name>Device.DeviceInfo.UpTime</name><value>8640</value><type>unsignedInt</type><writable>false</writable></parameter>
</device>

An empty tree gives <device></device>. Rows keep the tree order.

IoT emulator formats

All three IoT formats use the root element <parameters> and one <parameter> element per row. Rows keep the tree order. An empty tree gives <parameters></parameters>.

IOT_USP

The file lists objects and leaves. An object name keeps its trailing dot and has an empty value. The attribute acs_name is _. The type of a leaf is the stored type, unchanged. The type of an object, an empty type or a blank type is string.

<?xml version="1.0" encoding="UTF-8"?>
<parameters>
  <parameter name="Device.DeviceInfo." type="string" acs_name="_" value=""/>
  <parameter name="Device.DeviceInfo.UpTime" type="unsignedInt" acs_name="_" value="8640"/>
</parameters>

IOT_MQTT

The file lists leaves only. The attributes name and acs_name both hold the full dotted name. The writer maps the stored type with the table in Stored type mapping for IoT formats. The default type is STRING.

<?xml version="1.0" encoding="UTF-8"?>
<parameters>
  <parameter name="Root.Sensor.Temperature" type="FLOAT" acs_name="Root.Sensor.Temperature" value="21.5"/>
</parameters>

The names in this example are illustrative.

IOT_LWM2M

The file lists leaves and array headers, with the instances of an array. The file does not list object rows and object instance rows. The attribute acs_name holds the full stored name. The attribute name holds the numeric path.

Row Path in name

Leaf

/<objectId>/<instance>/<resourceId>

Resource instance of an array

/<objectId>/<instance>/<resourceId>/<resourceInstance>

Array header

/<objectId>/<instance>/<resourceId>, with an empty value

The writer reads the stored name with this grammar:

Root.<objectName>.<instance>.<resourceName>[.<resourceInstance>][.]
  • <objectName> is the text between Root. and the first .<digits>. segment.

  • <instance> and <resourceInstance> are digits. The writer copies them as text and never converts them to a number.

  • <resourceName> is the rest, without a final .<digits> and without a final dot.

  • A final dot marks an array header.

<?xml version="1.0" encoding="UTF-8"?>
<parameters>
  <parameter name="/3/0/0" type="STRING" acs_name="Root.Device.0.Manufacturer" value="My Company"/>
</parameters>

The names in this example are illustrative.

Skip rule. The writer resolves the object identifier and the resource identifier in the ACS LwM2M model tables lwm2m_coap_objects and lwm2m_coap_resources. A row is left out of the file in these cases:

  • The name does not follow the grammar.

  • The model has no object with this name, or it has more than one object identifier for it.

  • The model has no resource with this name in the object, or it has more than one resource identifier for it.

The writer never guesses. The service counts the skipped rows. The service writes a WARN line with the count and the first 5 names. If the file looks incomplete, search the log for unresolved LwM2M names.

Type. If the model has exactly one value type for the resource, the row uses it. The value types map as follows: STRING, FLOAT, BOOLEAN, INTEGER, TIME and OPAQUE keep their name. OBJLINK becomes OBJLNK. UNSIGNED_INTEGER becomes INTEGER. OPAQUE_STRING, OBJECT, NONE, METHOD and an unknown value become STRING.

Otherwise, a plain leaf uses the mapped stored type, and an array header or an array instance uses STRING.

Stored type mapping for IoT formats

The comparison is not case-sensitive.

Stored type IoT type

string

STRING

integer, int, unsignedInt, long, unsignedLong

INTEGER

float, double

FLOAT

boolean

BOOLEAN

opaque, base64

OPAQUE

time, dateTime

TIME

objlnk

OBJLNK

Any other type, or no type

STRING

The stored type is the data model type of the parameter. It is not the category that the Advanced view shows. For example, the Advanced view shows unsignedInt as int, and the file keeps unsignedInt.

Consumer rule for the user interface

The portal requests the file with the response type blob. If the request fails, HttpErrorResponse.error is a Blob. The global error interceptor cannot read the code and the message from a Blob. The user would then see the raw "Http failure response" text.

The interceptor must handle this case in one place:

  1. Detect that the error is a Blob with a JSON type.

  2. Read it with text() and parse the result with JSON.parse.

  3. Continue with the normal handling of message and code. This includes the redirect for 9024 and 9018.

If the Blob is not JSON, the interceptor shows its generic message. The portal can offer XML for every device, because 9251 is the defined answer for a protocol without an emulator format. Take the file name from Content-Disposition, or build the same pattern in the browser.

For developers

The code is in services/src/main/java/com/friendly/services/device/parameterstree/export.

Class Role

DeviceController.downloadDeviceParameters

Handler for parameters/download. It returns a ResponseEntity<Resource>.

DeviceParametersExportService

Validates the request in the fixed order 9117, 9250, domain 9018 or 9024, protocol 9251. Then it loads the tree with ParameterService.getDeviceParameters.

ParameterTreeFlattener

Flattens the tree depth first. An LwM2M array parameter becomes a header row <name>. and one row <name>.<key> per pair.

ParametersCsvWriter, CsvFormulaGuard

CSV layout and formula neutralization.

EmulatorXmlWriter, XmlTextEscaper

The four XML formats and the XML escaping.

Lwm2mPathResolver

Resolves a stored LwM2M name to a numeric path. It is built per export from ResourceLwm2mRepository.findLwm2mPathRows().

ParametersExportFileNames

Builds the file name.

  • The request record is DeviceParametersDownloadBody in the commons module.

  • The service reads the LwM2M model tables only for IOT_LWM2M. It makes no write.

  • The service writes one INFO line per export with the fields deviceId, serial, format, variant, rows, skipped and replacedChars. The line has no parameter value.

  • The whole file is built in memory. Memory use is about 3 to 4 times the file size.

  • The cost of an export is the cost of one Advanced view load. The per-device lock of ParameterService serializes it with other reads of the same tree.

  • The value_type column of lwm2m_coap_resources stores the ordinal of the ResourceValueType enum.

Manual QA steps that no automated test covers:

  1. Export a TR-181 device as XML and upload the file to the TR-069 emulator.

  2. Export a USP, an LwM2M and an MQTT device as XML and select each file as "Device tree (XML)" in the IoT emulator.

  3. Call the endpoint with an expired token and with no token. Expect status 403.

  4. Call the endpoint as a user whose domain excludes the device. Expect the 9018 handling of the device page.

  5. Repeat the LwM2M export on an install with MySQL and on an install with Oracle.