diff --git a/data-src/index.html b/data-src/index.html index 6b3a4b5..0e8f13e 100644 --- a/data-src/index.html +++ b/data-src/index.html @@ -933,6 +933,7 @@ +
diff --git a/data-src/index.js b/data-src/index.js index f30f830a..636c659 100644 --- a/data-src/index.js +++ b/data-src/index.js @@ -1973,13 +1973,16 @@ class Somfy { let config = ProtoViews.radio(proto.schema, radio); ui.toElement(document.getElementById('divTransceiverSettings'), { transceiver: { config: config } }); document.getElementById('divRadioError').style.display = config.radioInit ? 'none' : ''; + this.radioPinsFixed = !!config.fixedPins; + ProtoViews.showRadioPins(document, this.radioPinsFixed); this.checkBootPins(); }); } // Warns of radio pins that can stop the board booting (issue #34). checkBootPins() { let trans = ui.fromElement(document.getElementById('divTransceiverSettings')).transceiver; - let warnings = BootPins.radioWarnings(chipModel(), trans.config); + // The board's wiring is not the user's to move, so there is nothing to warn of. + let warnings = this.radioPinsFixed ? [] : BootPins.radioWarnings(chipModel(), trans.config); let div = document.getElementById('divRadioBootPins'); div.innerText = warnings.join('\n'); div.style.display = warnings.length > 0 ? '' : 'none'; @@ -1993,7 +1996,7 @@ class Somfy { ui.errorMessage('You must select a radio type.'); valid = false; } - // Check to make sure no pins were duplicated and defined + // Check to make sure no pins were duplicated and defined. The board's own wiring is not checked. if (valid) { let fnValDup = (o, name) => { let val = o[name]; @@ -2022,12 +2025,12 @@ class Somfy { } return true; }; - if (valid) valid = fnValDup(trans.config, 'SCKPin'); - if (valid) valid = fnValDup(trans.config, 'CSNPin'); - if (valid) valid = fnValDup(trans.config, 'MOSIPin'); - if (valid) valid = fnValDup(trans.config, 'MISOPin'); - if (valid) valid = fnValDup(trans.config, 'TXPin'); - if (valid) valid = fnValDup(trans.config, 'RXPin'); + if (valid && !this.radioPinsFixed) valid = fnValDup(trans.config, 'SCKPin'); + if (valid && !this.radioPinsFixed) valid = fnValDup(trans.config, 'CSNPin'); + if (valid && !this.radioPinsFixed) valid = fnValDup(trans.config, 'MOSIPin'); + if (valid && !this.radioPinsFixed) valid = fnValDup(trans.config, 'MISOPin'); + if (valid && !this.radioPinsFixed) valid = fnValDup(trans.config, 'TXPin'); + if (valid && !this.radioPinsFixed) valid = fnValDup(trans.config, 'RXPin'); if (valid) { sendOp('saveSettings', ProtoViews.radioSave(trans.config), (err) => { if (err) ui.serviceError(err); diff --git a/data-src/js/views.js b/data-src/js/views.js index e1719b6..85b1b7e 100644 --- a/data-src/js/views.js +++ b/data-src/js/views.js @@ -51,10 +51,20 @@ class ProtoViews { static radioSave(config) { const fields = { section: 'radio', bitLength: config.type }; for (const name of ProtoViews.SETTINGS.radio) { - if (name !== 'bitLength' && name !== 'radioInit') fields[name] = config[name]; + if (name !== 'bitLength' && !ProtoViews.RADIO_SHOWN.includes(name)) fields[name] = config[name]; } return fields; } + // Where the radio is wired to the board (config.fixedPins) its six pins + // can't be changed: the pin selects are disabled and the note shown. + static showRadioPins(doc, fixed) { + for (const id of ProtoViews.RADIO_PIN_SELECTS) { + const el = doc.getElementById(id); + if (el) el.disabled = !!fixed; + } + const note = doc.getElementById(ProtoViews.RADIO_PINS_NOTE); + if (note) note.style.display = fixed ? '' : 'none'; + } // An op's refusal as the UI's error box shows it. A shade whose settings // don't add up carries the reason's number, which the errors table // describes. @@ -66,6 +76,10 @@ class ProtoViews { } ProtoViews.WIFI = ['ssid', 'hasPassphrase', 'roaming', 'hidden']; ProtoViews.ETHERNET = ['boardType', 'phyType', 'CLKMode', 'phyAddress', 'PWRPin', 'MDCPin', 'MDIOPin']; +// The radio fields the device only sends. +ProtoViews.RADIO_SHOWN = ['radioInit', 'fixedPins']; +ProtoViews.RADIO_PIN_SELECTS = ['selTransSCKPin', 'selTransCSNPin', 'selTransMOSIPin', 'selTransMISOPin', 'selTransTXPin', 'selTransRXPin']; +ProtoViews.RADIO_PINS_NOTE = 'divRadioPinsFixed'; ProtoViews.IP = ['dhcp', 'ip', 'gateway', 'subnet', 'dns1', 'dns2']; // The fields of each settings section, as getSettings sends them // (src/DeviceOps.cpp). @@ -75,6 +89,6 @@ ProtoViews.SETTINGS = { mqtt: ['enabled', 'pubDisco', 'protocol', 'hostname', 'port', 'username', 'hasPassword', 'rootTopic', 'discoTopic'], security: ['securityType', 'username', 'hasPassword', 'hasPin'], radio: ['bitLength', 'proto', 'enabled', 'TXPin', 'RXPin', 'SCKPin', 'MOSIPin', 'MISOPin', 'CSNPin', 'frequency', 'rxBandwidth', - 'deviation', 'txPower', 'noiseDetection', 'radioInit'] + 'deviation', 'txPower', 'noiseDetection', 'radioInit', 'fixedPins'] }; if (typeof module !== 'undefined' && module.exports) module.exports = { ProtoViews }; diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md index 0f27463..32b540c 100644 --- a/docs/PROTOCOL.md +++ b/docs/PROTOCOL.md @@ -139,7 +139,7 @@ The settings are in five sections. `getSettings` and `saveSettings` name one in | `security` | `securityType`, `username`, `password`, `pin` | `/getSecurity`, `/saveSecurity` | | `radio` | `bitLength`, `proto`, `enabled`, `TXPin`, `RXPin`, `SCKPin`, `MOSIPin`, `MISOPin`, `CSNPin`, `frequency`, `rxBandwidth`, `deviation`, `txPower`, `noiseDetection` | `/getRadio`, `/saveRadio` | -- **Reading.** `getSettings` replies with the section's fields. A secret is never sent: `hasPassphrase`, `hasPassword` and `hasPin` say whether one is set. `network` also has `hasEthernet`, and has the Ethernet fields only on a device with Ethernet. `radio` also has `radioInit`. +- **Reading.** `getSettings` replies with the section's fields. A secret is never sent: `hasPassphrase`, `hasPassword` and `hasPin` say whether one is set. `network` also has `hasEthernet`, and has the Ethernet fields only on a device with Ethernet. `radio` also has `radioInit`, and `fixedPins` where the radio is wired to the board (the LILYGO T-Embed CC1101), left out on every other build. `fixedPins` means the six pins in the section are the board's and a client should show them as read-only: a save of other pins is applied with the board's pins and still replies as saved. Older clients and Home Assistant ignore the field. - `connMode` is the connection the settings ask for: 1 WiFi, 2 Ethernet, 3 Ethernet with WiFi as the fallback. The header's `connType` is the connection in use. - **Saving.** `saveSettings` takes the section and the fields to change. As with every op, a field left out stays as it is. A secret sent empty keeps the stored one, except alongside a new `ssid` or MQTT `username`, as on the routes. - Every field is checked before anything is saved: @@ -148,7 +148,7 @@ The settings are in five sections. `getSettings` and `saveSettings` name one in - a string's length, which is what the setting holds: `hostname` 31, `pin` 4, and so on; - an address: four numbers 0–255, or `""` for none. - A bad request also comes for a field the section doesn't save (a `has*` field, `radioInit`, a field of another section, Ethernet on a device without it), and for a save with no fields. + A bad request also comes for a field the section doesn't save (a `has*` field, `radioInit`, `fixedPins`, a field of another section, Ethernet on a device without it), and for a save with no fields. - One save runs at a time, whichever client sent it: while one is waiting, another gets busy. - The reply comes once the settings are saved and applied, as their route applies them. A saved time zone takes effect at once, where the route waited for a reboot. - `rebooting` 1 means the device restarts a second later for them. That happens for a new `connMode`, for a new SSID or passphrase while on WiFi with nobody on the soft AP, and for Ethernet settings while on Ethernet. diff --git a/src/DeviceOps.cpp b/src/DeviceOps.cpp index d0a60b0..9b2a834 100644 --- a/src/DeviceOps.cpp +++ b/src/DeviceOps.cpp @@ -109,6 +109,7 @@ static void writeSettings(ProtoWriter &w, settings_section_t section) { w.add(proto_field_t::txPower, cfg.txPower); w.add(proto_field_t::noiseDetection, cfg.noiseDetection); w.add(proto_field_t::radioInit, cfg.radioInit); + writeRadioBoardFields(w, BoardProfile::integratedRadio); break; } } diff --git a/src/ProtoSchema.h b/src/ProtoSchema.h index 85ae094..20637dd 100644 --- a/src/ProtoSchema.h +++ b/src/ProtoSchema.h @@ -251,6 +251,7 @@ FIELD(txPower, "tp", 'i', 0) \ FIELD(noiseDetection, "nd", 'b', 0) \ FIELD(radioInit, "ri", 'b', 0) \ + FIELD(fixedPins, "fx", 'b', 0) \ /* wifiNetworks: each network has ssid, channel and strength as above */ \ FIELD(networks, "nw", 'l', 0) \ FIELD(macAddress, "ma", 's', 0) diff --git a/src/ProtoSettings.cpp b/src/ProtoSettings.cpp index 0cec9da..42435fc 100644 --- a/src/ProtoSettings.cpp +++ b/src/ProtoSettings.cpp @@ -25,7 +25,7 @@ static settings_section_t sectionOf(settings_part_t part) { // What a save may set. lo..hi is a whole number's range, a number's in // hundredths, and hi a string's longest length: what the setting holds, so the // route's fromJSON neither cuts it short nor wraps it. The has* fields and -// radioInit are only ever sent. +// radioInit and fixedPins are only ever sent. struct settings_field_t { settings_part_t part; proto_field_t field; @@ -97,6 +97,10 @@ static const settings_field_t *saved(settings_section_t section, proto_field_t f return nullptr; } +void writeRadioBoardFields(ProtoWriter &w, bool pinsFixed) { + w.add(proto_field_t::fixedPins, pinsFixed); +} + const char *parseSettingsSection(JsonObjectConst d, settings_section_t §ion) { const char *name = protoString(d, proto_field_t::section); if(!name) return "section is missing"; diff --git a/src/ProtoSettings.h b/src/ProtoSettings.h index 8b4aa71..a57e923 100644 --- a/src/ProtoSettings.h +++ b/src/ProtoSettings.h @@ -32,6 +32,11 @@ enum class settings_part_t : uint8_t { radio // transceiver_config_t }; const char *settingsSectionName(settings_section_t section); +// What the radio section says about the board rather than the config: +// fixedPins is set where the radio is wired to the board, so the six pins in +// the section are the board's and a save can't change them. Left out +// otherwise, so a client that doesn't know it never sees it. +void writeRadioBoardFields(ProtoWriter &w, bool pinsFixed); // The section that payload d names in section. nullptr when it is good; // otherwise what is wrong with it, for a bad request reply. const char *parseSettingsSection(JsonObjectConst d, settings_section_t §ion); diff --git a/test/fixtures/proto2/hello.json b/test/fixtures/proto2/hello.json index 9f18062..03f0de3 100644 --- a/test/fixtures/proto2/hello.json +++ b/test/fixtures/proto2/hello.json @@ -3,6 +3,6 @@ "t": 1, "d": { "fw": "v3.0.14", - "sh": "16fdffc7" + "sh": "422f9a6e" } } \ No newline at end of file diff --git a/test/fixtures/proto2/schema.json b/test/fixtures/proto2/schema.json index ce9a228..513c9d2 100644 --- a/test/fixtures/proto2/schema.json +++ b/test/fixtures/proto2/schema.json @@ -1,5 +1,5 @@ { - "hash": "16fdffc7", + "hash": "422f9a6e", "types": { "1": "hello", "2": "reply", @@ -294,6 +294,11 @@ "kind": "s", "default": 0 }, + "fx": { + "name": "fixedPins", + "kind": "b", + "default": 0 + }, "g": { "name": "groupId", "kind": "i", diff --git a/test/fixtures/proto2/sync.json b/test/fixtures/proto2/sync.json index 86fecdc..6147ff3 100644 --- a/test/fixtures/proto2/sync.json +++ b/test/fixtures/proto2/sync.json @@ -1148,6 +1148,12 @@ "b", 0 ], + [ + "fx", + "fixedPins", + "b", + 0 + ], [ "nw", "networks", diff --git a/test/js/views.test.js b/test/js/views.test.js index 30e1775..c30f4a1 100644 --- a/test/js/views.test.js +++ b/test/js/views.test.js @@ -71,9 +71,40 @@ test('the radio section as the panel binds it, and back', () => { assert.equal(save.frequency, 433.42); // Only shown, never saved. assert.equal('radioInit' in save, false); + assert.equal('fixedPins' in save, false); assert.equal('type' in save, false); }); +function fakeDocument() { + const els = {}; + for (const id of ProtoViews.RADIO_PIN_SELECTS) els[id] = { disabled: false }; + els[ProtoViews.RADIO_PINS_NOTE] = { style: { display: 'none' } }; + return { els, getElementById: (id) => els[id] || null }; +} + +test('the radio pins are fixed only where the device says so', () => { + assert.equal(ProtoViews.radio(schema, { bitLength: 80 }).fixedPins, false); + assert.equal(ProtoViews.radio(schema, { bitLength: 80, fixedPins: true }).fixedPins, true); + + const fixed = fakeDocument(); + ProtoViews.showRadioPins(fixed, ProtoViews.radio(schema, { fixedPins: true }).fixedPins); + assert.equal(ProtoViews.RADIO_PIN_SELECTS.length, 6); + for (const id of ProtoViews.RADIO_PIN_SELECTS) assert.equal(fixed.els[id].disabled, true, id); + assert.equal(fixed.els[ProtoViews.RADIO_PINS_NOTE].style.display, ''); + + // Without the flag they are editable, and a page that was fixed becomes editable again. + const free = fakeDocument(); + ProtoViews.showRadioPins(free, true); + ProtoViews.showRadioPins(free, ProtoViews.radio(schema, {}).fixedPins); + for (const id of ProtoViews.RADIO_PIN_SELECTS) assert.equal(free.els[id].disabled, false, id); + assert.equal(free.els[ProtoViews.RADIO_PINS_NOTE].style.display, 'none'); +}); + +test('every pin select and the note are in the page', () => { + const html = require('node:fs').readFileSync(require('node:path').join(__dirname, '../../data-src/index.html'), 'utf8'); + for (const id of ProtoViews.RADIO_PIN_SELECTS.concat(ProtoViews.RADIO_PINS_NOTE)) assert.ok(html.includes(`id="${id}"`), id); +}); + test('an op refusal for the error box, with a shade validation code', () => { assert.deepEqual(ProtoViews.opError('saveShade', { code: 'badRequest', desc: 'Pin in use', data: { validation: -10 } }), { htmlError: 'badRequest', service: 'saveShade', desc: 'Pin in use', code: -10 }); diff --git a/test/test_protosettings/test_protosettings.cpp b/test/test_protosettings/test_protosettings.cpp index 9e6acb3..668ce9b 100644 --- a/test/test_protosettings/test_protosettings.cpp +++ b/test/test_protosettings/test_protosettings.cpp @@ -2,6 +2,7 @@ #include #include #include "ProtoSettings.h" +#include "JsonFormatter.h" // Socket protocol 2's settings ops: the sections, and the settings route's // request that a save's payload becomes (docs/PROTOCOL.md). @@ -161,6 +162,7 @@ static void test_what_is_only_sent_is_refused() { TEST_ASSERT_EQUAL_STRING("hasPassphrase can't be saved in network", expand(R"({"se":"network","hq":1})")); TEST_ASSERT_EQUAL_STRING("hasEthernet can't be saved in network", expand(R"({"se":"network","he":1})")); TEST_ASSERT_EQUAL_STRING("radioInit can't be saved in radio", expand(R"({"se":"radio","ri":1})")); + TEST_ASSERT_EQUAL_STRING("fixedPins can't be saved in radio", expand(R"({"se":"radio","fx":1})")); } static void test_a_field_of_another_section_is_refused() { TEST_ASSERT_EQUAL_STRING("ssid can't be saved in general", expand(R"({"se":"general","sd":"home"})")); @@ -202,6 +204,26 @@ static void test_a_route_name_belongs_to_its_section() { TEST_ASSERT_EQUAL_UINT16(bit(settings_part_t::security), parts()); } +/********************************************************************* + * What the radio section says about the board + ********************************************************************/ +static std::string boardFields(bool pinsFixed) { + static BufferedJsonFormatter fmt; + static char buff[64]; + fmt.setBuffer(buff, sizeof(buff)); + ProtoWriter w(fmt); + fmt.beginObject(); + writeRadioBoardFields(w, pinsFixed); + fmt.endObject(); + return buff; +} +static void test_the_radio_pins_are_fixed_only_where_the_board_wires_them() { + TEST_ASSERT_EQUAL_STRING("{\"fx\":1}", boardFields(true).c_str()); + // A generic board leaves it out, which is false to a client that knows it. + TEST_ASSERT_EQUAL_STRING("{}", boardFields(false).c_str()); + TEST_ASSERT_EQUAL_STRING("fixedPins", protoFieldByKey("fx")->name); +} + int main(int argc, char **argv) { UNITY_BEGIN(); RUN_TEST(test_every_section_by_its_name); @@ -226,5 +248,6 @@ int main(int argc, char **argv) { RUN_TEST(test_ethernet_needs_a_chip_with_it); RUN_TEST(test_a_save_sets_the_parts_it_has_fields_of); RUN_TEST(test_a_route_name_belongs_to_its_section); + RUN_TEST(test_the_radio_pins_are_fixed_only_where_the_board_wires_them); return UNITY_END(); }