Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
feat(M2M): save mqtt prefix in configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <[email protected]>
  • Loading branch information
btry committed May 3, 2018
1 parent 1817d12 commit 405ac63
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions inc/agent.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,8 @@ public function getTopic() {
* @return bool
*/
public function getByTopic($topic) {
$topic = PluginFlyvemdmCommon::removeMqttPrefix($topic);

$mqttPath = explode('/', $topic);
if (!isset($mqttPath[2])) {
return false;
Expand Down
22 changes: 22 additions & 0 deletions inc/common.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,26 @@ public static function endsWith($haystack, $needle) {
$needle, $temp) !== false);
}

/**
* Removes the mqtt prefix from
*
* @param string $topic Topic to modify
* @param boolean $readConfig set to True to force reading the configuration
*
* @return string|false the topic without its prefix, or false if an error occured
*/
public static function removeMqttPrefix($topic, $readConfig = false) {
static $prefix = null;
if ($prefix === null || $readConfig) {
$config = Config::getConfigurationValues('flyvemdm', ['mqtt_prefix']);
$prefix = $config['mqtt_prefix'];
}

if (!PluginFlyvemdmCommon::startsWith($topic, $prefix)) {
return false;
}
$topic = substr($topic, strlen($prefix));

return $topic;
}
}
7 changes: 7 additions & 0 deletions inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,13 @@ public static function configUpdate($input) {
}
}

if (isset($input['mqtt_prefix']) && strlen($input['mqtt_prefix']) > 0) {
// Ensure there is a trailing slash
if (strrpos($input['mqtt_prefix'], '/') != strlen($input['mqtt_prefix']) - 1) {
$input['mqtt_prefix'] .= '/';
}
}

if (isset($_SESSION['plugin_flyvemdm_wizard_step'])) {
$input = static::processStep($input);
if (count($input) > 0 && $input !== false) {
Expand Down
3 changes: 3 additions & 0 deletions inc/mqtthandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public function pingresp(\sskaje\mqtt\MQTT $mqtt, \sskaje\mqtt\Message\PINGRESP
*/
public function publish(\sskaje\mqtt\MQTT $mqtt, \sskaje\mqtt\Message\PUBLISH $publish_object) {
$topic = $publish_object->getTopic();
$topic = PluginFlyvemdmCommon::removeMqttPrefix($topic);
$message = $publish_object->getMessage();
$this->log->saveIngoingMqttMessage($topic, $message);

Expand All @@ -138,6 +139,8 @@ public function publish(\sskaje\mqtt\MQTT $mqtt, \sskaje\mqtt\Message\PUBLISH $p
}
} else if ($topic === 'FlyvemdmManifest/Status/Version') {
$this->publishFlyveManifest($mqtt, $message);
// Force update of mqtt prefix, in case of configuration change
PluginFlyvemdmCommon::removeMqttPrefix('', true);
}
}

Expand Down
1 change: 1 addition & 0 deletions install/installer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ protected function createInitialConfig() {
'mqtt_broker_tls_ciphers' => self::DEFAULT_CIPHERS_LIST,
'mqtt_user' => self::BACKEND_MQTT_USER,
'mqtt_passwd' => $MdmMqttPassword,
'mqtt_prefix' => '',
'instance_id' => $instanceId,
'registered_profiles_id' => '',
'guest_profiles_id' => '',
Expand Down
1 change: 1 addition & 0 deletions install/upgrade/update_to_dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function plugin_flyvemdm_update_to_dev(Migration $migration) {
'android_bugcollector_login' => '',
'android_bugcollector_passwd' => '',
'invitation_deeplink' => PLUGIN_FLYVEMDM_DEEPLINK,
'mqtt_prefix' => '',
]);

$config = Config::getConfigurationValues('flyvemdm', ['mqtt_broker_tls']);
Expand Down
49 changes: 49 additions & 0 deletions tests/suite-unit/PluginFlyvemdmCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,53 @@ public function testGenerateUUID() {
->matches('/\w{8}-\w{4}-4\w{3}-[8,9,A,B]\w{3}-\w{12}/i');
}

public function providerRemoveMqttPrefix() {
return [
[
'prefix' => '/',
'topic' => '/1/agent/42/some/sub/topic',
'expected' => '1/agent/42/some/sub/topic',
],
[
'prefix' => '/',
'topic' => '1/agent/42/some/sub/topic',
'expected' => false,
],
[
'prefix' => 'some/prefix/',
'topic' => 'some/prefix/1/agent/42/some/sub/topic',
'expected' => '1/agent/42/some/sub/topic',
],
[
'prefix' => 'some/prefix/',
'topic' => 'some/invalid/prefix/1/agent/42/some/sub/topic',
'expected' => false,
],

// Tests with empty prefix msut be at the end to not impact next tests
[
'prefix' => '',
'topic' => '1/agent/42/some/sub/topic',
'expected' => '1/agent/42/some/sub/topic',
],
[
'prefix' => '',
'topic' => '/1/agent/42/some/sub/topic',
'expected' => '/1/agent/42/some/sub/topic',
],
];
}

/**
* @dataProvider providerRemoveMqttPrefix
*/
public function testRemoveMqttPrefix($prefix, $topic, $expected) {
\Config::setConfigurationValues('flyvemdm', ['mqtt_prefix' => $prefix]);
$output = \PluginFlyvemdmCommon::removeMqttPrefix($topic, true);
if ($expected === false) {
$this->boolean($output)->isFalse();
} else {
$this->string($output)->isEqualTo($expected);
}
}
}
5 changes: 5 additions & 0 deletions tpl/config-messagequeue.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
min="1" max="65535"></td>
<td>{{ __('A port number between 1025 and 65535, standard port is 1883', 'flyvemdm') }}</td>
</tr>
<tr class="tab_bg_1">
<td>{{ __('MQTT prefix', 'flyvemdm') }}</td>
<td><input type="text" name="mqtt_prefix" value="{{ config.mqtt_prefix }}"></td>
<td>{{ __('Prepend all topics with a prefix (useful if a MQTT broker is mutualized). The mqtt daemon must be restarted.', 'flyvemdm') }}</td>
</tr>
<tr class="tab_bg_1">
<td>{{ __('MQTT broker port for TLS', 'flyvemdm') }}</td>
<td><input type="number" name="mqtt_broker_tls_port" value="{{ config.mqtt_broker_tls_port }}"
Expand Down

0 comments on commit 405ac63

Please sign in to comment.