Kaynağa Gözat

chore: split out lwm2m

JianBo He 2 yıl önce
ebeveyn
işleme
b58ce09658
35 değiştirilmiş dosya ile 576 ekleme ve 338 silme
  1. 0 125
      apps/emqx_gateway/i18n/emqx_gateway_schema_i18n.conf
  2. 1 1
      apps/emqx_gateway/include/emqx_gateway.hrl
  3. 0 5
      apps/emqx_gateway/src/emqx_gateway_app.erl
  4. 2 147
      apps/emqx_gateway/src/emqx_gateway_schema.erl
  5. 0 25
      apps/emqx_gateway/src/lwm2m/.gitignore
  6. 19 0
      apps/emqx_lwm2m/.gitignore
  7. 191 0
      apps/emqx_lwm2m/LICENSE
  8. 0 1
      apps/emqx_gateway/src/lwm2m/README.md
  9. 127 0
      apps/emqx_lwm2m/i18n/emqx_lwm2m_schema.conf
  10. 0 0
      apps/emqx_lwm2m/include/emqx_lwm2m.hrl
  11. 0 0
      apps/emqx_lwm2m/lwm2m_xml/LWM2M_Access_Control-v1_0_1.xml
  12. 0 0
      apps/emqx_lwm2m/lwm2m_xml/LWM2M_Connectivity_Statistics-v1_0_1.xml
  13. 0 0
      apps/emqx_lwm2m/lwm2m_xml/LWM2M_Device-v1_0_1.xml
  14. 0 0
      apps/emqx_lwm2m/lwm2m_xml/LWM2M_Firmware_Update-v1_0_1.xml
  15. 0 0
      apps/emqx_lwm2m/lwm2m_xml/LWM2M_Location-v1_0.xml
  16. 0 0
      apps/emqx_lwm2m/lwm2m_xml/LWM2M_Security-v1_0.xml
  17. 0 0
      apps/emqx_lwm2m/lwm2m_xml/LWM2M_Server-v1_0.xml
  18. 2 0
      apps/emqx_lwm2m/rebar.config
  19. 0 0
      apps/emqx_lwm2m/src/binary_util.erl
  20. 10 0
      apps/emqx_lwm2m/src/emqx_lwm2m.app.src
  21. 21 19
      apps/emqx_gateway/src/lwm2m/emqx_lwm2m_impl.erl
  22. 0 0
      apps/emqx_lwm2m/src/emqx_lwm2m_api.erl
  23. 1 1
      apps/emqx_gateway/src/lwm2m/emqx_lwm2m_channel.erl
  24. 1 1
      apps/emqx_gateway/src/lwm2m/emqx_lwm2m_cmd.erl
  25. 1 1
      apps/emqx_gateway/src/lwm2m/emqx_lwm2m_message.erl
  26. 184 0
      apps/emqx_lwm2m/src/emqx_lwm2m_schema.erl
  27. 1 1
      apps/emqx_gateway/src/lwm2m/emqx_lwm2m_session.erl
  28. 1 1
      apps/emqx_gateway/src/lwm2m/emqx_lwm2m_tlv.erl
  29. 1 1
      apps/emqx_gateway/src/lwm2m/emqx_lwm2m_xml_object.erl
  30. 1 1
      apps/emqx_gateway/src/lwm2m/emqx_lwm2m_xml_object_db.erl
  31. 3 2
      apps/emqx_gateway/test/emqx_lwm2m_SUITE.erl
  32. 4 3
      apps/emqx_gateway/test/emqx_lwm2m_api_SUITE.erl
  33. 2 2
      apps/emqx_gateway/test/emqx_tlv_SUITE.erl
  34. 1 0
      mix.exs
  35. 2 1
      rebar.config.erl

+ 0 - 125
apps/emqx_gateway/i18n/emqx_gateway_schema_i18n.conf

@@ -1,130 +1,5 @@
 emqx_gateway_schema {
 
-    lwm2m {
-        desc {
-            en: """The LwM2M Gateway configuration. This gateway only supports the v1.0.1 protocol."""
-            zh: """LwM2M 网关配置。仅支持 v1.0.1 协议。"""
-        }
-    }
-
-    lwm2m_xml_dir {
-        desc {
-            en: """The Directory for LwM2M Resource definition."""
-            zh: """LwM2M Resource 定义的 XML 文件目录路径。"""
-        }
-    }
-
-    lwm2m_lifetime_min {
-        desc {
-            en: """Minimum value of lifetime allowed to be set by the LwM2M client."""
-            zh: """允许 LwM2M 客户端允许设置的心跳最小值。"""
-        }
-    }
-
-    lwm2m_lifetime_max {
-        desc {
-            en: """Maximum value of lifetime allowed to be set by the LwM2M client."""
-            zh: """允许 LwM2M 客户端允许设置的心跳最大值。"""
-        }
-    }
-
-    lwm2m_qmode_time_window {
-        desc {
-            en: """The value of the time window during which the network link is considered valid by the LwM2M Gateway in QMode mode.
-For example, after receiving an update message from a client, any messages within this time window are sent directly to the LwM2M client, and all messages beyond this time window are temporarily stored in memory."""
-
-            zh: """在QMode模式下,LwM2M网关认为网络链接有效的时间窗口的值。
-例如,在收到客户端的更新信息后,在这个时间窗口内的任何信息都会直接发送到LwM2M客户端,而超过这个时间窗口的所有信息都会暂时储存在内存中。"""
-        }
-    }
-
-    lwm2m_auto_observe {
-        desc {
-            en: """Automatically observe the object list of REGISTER packet."""
-            zh: """自动 Observe REGISTER 数据包的 Object 列表。"""
-        }
-    }
-
-    lwm2m_update_msg_publish_condition {
-        desc {
-            en: """Policy for publishing UPDATE event message.<br/>
-  - always: send update events as long as the UPDATE request is received.<br/>
-  - contains_object_list: send update events only if the UPDATE request carries any Object List"""
-            zh: """发布UPDATE事件消息的策略。<br/>
-  - always: 只要收到 UPDATE 请求,就发送更新事件。<br/>
-  - contains_object_list: 仅当 UPDATE 请求携带 Object 列表时才发送更新事件。"""
-        }
-    }
-
-    lwm2m_translators {
-        desc {
-            en: """Topic configuration for LwM2M's gateway publishing and subscription."""
-            zh: """LwM2M 网关订阅/发布消息的主题映射配置。"""
-        }
-    }
-
-    lwm2m_translators_command {
-        desc {
-            en: """The topic for receiving downstream commands.
-For each new LwM2M client that succeeds in going online, the gateway creates a subscription relationship to receive downstream commands and send it to the LwM2M client"""
-
-            zh: """下行命令主题。
-对于每个成功上线的新 LwM2M 客户端,网关会创建一个订阅关系来接收下行消息并将其发送给客户端。"""
-        }
-    }
-
-    lwm2m_translators_response {
-        desc {
-            en: """The topic for gateway to publish the acknowledge events from LwM2M client"""
-            zh: """用于网关发布来自 LwM2M 客户端的确认事件的主题。"""
-        }
-    }
-
-    lwm2m_translators_notify {
-        desc {
-            en: """The topic for gateway to publish the notify events from LwM2M client.
-After succeed observe a resource of LwM2M client, Gateway will send the notify events via this topic, if the client reports any resource changes"""
-
-            zh: """用于发布来自 LwM2M 客户端的通知事件的主题。
-在成功 Observe 到 LwM2M 客户端的资源后,如果客户端报告任何资源状态的变化,网关将通过该主题发送通知事件。"""
-        }
-    }
-
-    lwm2m_translators_register {
-        desc {
-            en: """The topic for gateway to publish the register events from LwM2M client."""
-            zh: """用于发布来自 LwM2M 客户端的注册事件的主题。"""
-        }
-    }
-
-    lwm2m_translators_update {
-        desc {
-            en: """The topic for gateway to publish the update events from LwM2M client"""
-            zh: """用于发布来自LwM2M客户端的更新事件的主题。"""
-        }
-    }
-
-    translator {
-        desc {
-            en: """MQTT topic that corresponds to a particular type of event."""
-            zh: """配置某网关客户端对于发布消息或订阅的主题和 QoS 等级。"""
-        }
-    }
-
-    translator_topic {
-        desc {
-            en: """Topic Name"""
-            zh: """主题名称"""
-        }
-    }
-
-    translator_qos {
-        desc {
-            en: """QoS Level"""
-            zh: """QoS 等级"""
-        }
-    }
-
     exproto {
         desc {
             en: """The Extension Protocol configuration"""

+ 1 - 1
apps/emqx_gateway/include/emqx_gateway.hrl

@@ -42,6 +42,6 @@
         name := gateway_name(),
         callback_module := module(),
         config_schema_module := module()
-     }.
+    }.
 
 -endif.

+ 0 - 5
apps/emqx_gateway/src/emqx_gateway_app.erl

@@ -42,11 +42,6 @@ stop(_State) ->
 
 load_default_gateway_applications() ->
     BuiltInGateways = [
-        #{
-            name => lwm2m,
-            callback_module => emqx_lwm2m_impl,
-            config_schema_module => emqx_lwm2m_schema
-        },
         #{
             name => exproto,
             callback_module => emqx_exproto_impl,

+ 2 - 147
apps/emqx_gateway/src/emqx_gateway_schema.erl

@@ -64,14 +64,6 @@ roots() -> [gateway].
 
 fields(gateway) ->
     [
-        {lwm2m,
-            sc(
-                ref(lwm2m),
-                #{
-                    required => {false, recursively},
-                    desc => ?DESC(lwm2m)
-                }
-            )},
         {exproto,
             sc(
                 ref(exproto),
@@ -81,75 +73,6 @@ fields(gateway) ->
                 }
             )}
     ] ++ gateway_schemas();
-fields(lwm2m) ->
-    [
-        {xml_dir,
-            sc(
-                binary(),
-                #{
-                    %% since this is not packaged with emqx, nor
-                    %% present in the packages, we must let the user
-                    %% specify it rather than creating a dynamic
-                    %% default (especially difficult to handle when
-                    %% generating docs).
-                    example => <<"/etc/emqx/lwm2m_xml">>,
-                    required => true,
-                    desc => ?DESC(lwm2m_xml_dir)
-                }
-            )},
-        {lifetime_min,
-            sc(
-                duration(),
-                #{
-                    default => <<"15s">>,
-                    desc => ?DESC(lwm2m_lifetime_min)
-                }
-            )},
-        {lifetime_max,
-            sc(
-                duration(),
-                #{
-                    default => <<"86400s">>,
-                    desc => ?DESC(lwm2m_lifetime_max)
-                }
-            )},
-        {qmode_time_window,
-            sc(
-                duration_s(),
-                #{
-                    default => <<"22s">>,
-                    desc => ?DESC(lwm2m_qmode_time_window)
-                }
-            )},
-        %% TODO: Support config resource path
-        {auto_observe,
-            sc(
-                boolean(),
-                #{
-                    default => false,
-                    desc => ?DESC(lwm2m_auto_observe)
-                }
-            )},
-        %% FIXME: not working now
-        {update_msg_publish_condition,
-            sc(
-                hoconsc:enum([always, contains_object_list]),
-                #{
-                    default => contains_object_list,
-                    desc => ?DESC(lwm2m_update_msg_publish_condition)
-                }
-            )},
-        {translators,
-            sc(
-                ref(lwm2m_translators),
-                #{
-                    required => true,
-                    desc => ?DESC(lwm2m_translators)
-                }
-            )},
-        {mountpoint, mountpoint("lwm2m/${endpoint_name}/")},
-        {listeners, sc(ref(udp_listeners), #{desc => ?DESC(udp_listeners)})}
-    ] ++ gateway_common_options();
 fields(exproto) ->
     [
         {server,
@@ -223,68 +146,6 @@ fields(clientinfo_override) ->
             })},
         {clientid, sc(binary(), #{desc => ?DESC(gateway_common_clientinfo_override_clientid)})}
     ];
-fields(lwm2m_translators) ->
-    [
-        {command,
-            sc(
-                ref(translator),
-                #{
-                    desc => ?DESC(lwm2m_translators_command),
-                    required => true
-                }
-            )},
-        {response,
-            sc(
-                ref(translator),
-                #{
-                    desc => ?DESC(lwm2m_translators_response),
-                    required => true
-                }
-            )},
-        {notify,
-            sc(
-                ref(translator),
-                #{
-                    desc => ?DESC(lwm2m_translators_notify),
-                    required => true
-                }
-            )},
-        {register,
-            sc(
-                ref(translator),
-                #{
-                    desc => ?DESC(lwm2m_translators_register),
-                    required => true
-                }
-            )},
-        {update,
-            sc(
-                ref(translator),
-                #{
-                    desc => ?DESC(lwm2m_translators_update),
-                    required => true
-                }
-            )}
-    ];
-fields(translator) ->
-    [
-        {topic,
-            sc(
-                binary(),
-                #{
-                    required => true,
-                    desc => ?DESC(translator_topic)
-                }
-            )},
-        {qos,
-            sc(
-                emqx_schema:qos(),
-                #{
-                    default => 0,
-                    desc => ?DESC(translator_qos)
-                }
-            )}
-    ];
 fields(udp_listeners) ->
     [
         {udp, sc(map(name, ref(udp_listener)), #{desc => ?DESC(udp_listener)})},
@@ -356,8 +217,6 @@ fields(dtls_opts) ->
 
 desc(gateway) ->
     "EMQX Gateway configuration root.";
-desc(lwm2m) ->
-    "The LwM2M protocol gateway.";
 desc(exproto) ->
     "Settings for EMQX extension protocol (exproto).";
 desc(exproto_grpc_server) ->
@@ -368,10 +227,6 @@ desc(ssl_server_opts) ->
     "SSL configuration for the server.";
 desc(clientinfo_override) ->
     "ClientInfo override.";
-desc(lwm2m_translators) ->
-    "MQTT topics that correspond to LwM2M events.";
-desc(translator) ->
-    "MQTT topic that corresponds to a particular type of event.";
 desc(udp_listeners) ->
     "Settings for the UDP listeners.";
 desc(tcp_listeners) ->
@@ -536,11 +391,11 @@ proxy_protocol_opts() ->
 %% dynamic schemas
 
 %% FIXME: don't hardcode the gateway names
-gateway_schema(lwm2m) -> fields(lwm2m);
 gateway_schema(exproto) -> fields(exproto);
 gateway_schema(stomp) -> emqx_stomp_schema:fields(stomp);
 gateway_schema(mqttsn) -> emqx_mqttsn_schema:fields(mqttsn);
-gateway_schema(coap) -> emqx_coap_schema:fields(coap).
+gateway_schema(coap) -> emqx_coap_schema:fields(coap);
+gateway_schema(lwm2m) -> emqx_lwm2m_schema:fields(lwm2m).
 
 gateway_schemas() ->
     lists:map(

+ 0 - 25
apps/emqx_gateway/src/lwm2m/.gitignore

@@ -1,25 +0,0 @@
-deps/
-ebin/
-_rel/
-.erlang.mk/
-*.d
-*.o
-*.exe
-data/
-*.iml
-.idea/
-logs/
-*.beam
-emqx_coap.d
-erlang.mk
-integration_test/emqx-rel/
-integration_test/build_wakaama/
-integration_test/case*.txt
-integration_test/paho/
-integration_test/wakaama/
-_build/
-rebar.lock
-rebar3.crashdump
-*.conf.rendered
-.rebar3/
-*.swp

+ 19 - 0
apps/emqx_lwm2m/.gitignore

@@ -0,0 +1,19 @@
+.rebar3
+_*
+.eunit
+*.o
+*.beam
+*.plt
+*.swp
+*.swo
+.erlang.cookie
+ebin
+log
+erl_crash.dump
+.rebar
+logs
+_build
+.idea
+*.iml
+rebar3.crashdump
+*~

+ 191 - 0
apps/emqx_lwm2m/LICENSE

@@ -0,0 +1,191 @@
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   Copyright 2023, JianBo He <heeejianbo@gmail.com>.
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+

+ 0 - 1
apps/emqx_gateway/src/lwm2m/README.md

@@ -1,4 +1,3 @@
-
 # LwM2M Gateway
 
 [The LwM2M Specifications](http://www.openmobilealliance.org/release/LightweightM2M) is a Lightweight Machine to Machine protocol.

+ 127 - 0
apps/emqx_lwm2m/i18n/emqx_lwm2m_schema.conf

@@ -0,0 +1,127 @@
+emqx_lwm2m_schema {
+
+    lwm2m {
+        desc {
+            en: """The LwM2M Gateway configuration. This gateway only supports the v1.0.1 protocol."""
+            zh: """LwM2M 网关配置。仅支持 v1.0.1 协议。"""
+        }
+    }
+
+    lwm2m_xml_dir {
+        desc {
+            en: """The Directory for LwM2M Resource definition."""
+            zh: """LwM2M Resource 定义的 XML 文件目录路径。"""
+        }
+    }
+
+    lwm2m_lifetime_min {
+        desc {
+            en: """Minimum value of lifetime allowed to be set by the LwM2M client."""
+            zh: """允许 LwM2M 客户端允许设置的心跳最小值。"""
+        }
+    }
+
+    lwm2m_lifetime_max {
+        desc {
+            en: """Maximum value of lifetime allowed to be set by the LwM2M client."""
+            zh: """允许 LwM2M 客户端允许设置的心跳最大值。"""
+        }
+    }
+
+    lwm2m_qmode_time_window {
+        desc {
+            en: """The value of the time window during which the network link is considered valid by the LwM2M Gateway in QMode mode.
+For example, after receiving an update message from a client, any messages within this time window are sent directly to the LwM2M client, and all messages beyond this time window are temporarily stored in memory."""
+
+            zh: """在QMode模式下,LwM2M网关认为网络链接有效的时间窗口的值。
+例如,在收到客户端的更新信息后,在这个时间窗口内的任何信息都会直接发送到LwM2M客户端,而超过这个时间窗口的所有信息都会暂时储存在内存中。"""
+        }
+    }
+
+    lwm2m_auto_observe {
+        desc {
+            en: """Automatically observe the object list of REGISTER packet."""
+            zh: """自动 Observe REGISTER 数据包的 Object 列表。"""
+        }
+    }
+
+    lwm2m_update_msg_publish_condition {
+        desc {
+            en: """Policy for publishing UPDATE event message.<br/>
+  - always: send update events as long as the UPDATE request is received.<br/>
+  - contains_object_list: send update events only if the UPDATE request carries any Object List"""
+            zh: """发布UPDATE事件消息的策略。<br/>
+  - always: 只要收到 UPDATE 请求,就发送更新事件。<br/>
+  - contains_object_list: 仅当 UPDATE 请求携带 Object 列表时才发送更新事件。"""
+        }
+    }
+
+    lwm2m_translators {
+        desc {
+            en: """Topic configuration for LwM2M's gateway publishing and subscription."""
+            zh: """LwM2M 网关订阅/发布消息的主题映射配置。"""
+        }
+    }
+
+    lwm2m_translators_command {
+        desc {
+            en: """The topic for receiving downstream commands.
+For each new LwM2M client that succeeds in going online, the gateway creates a subscription relationship to receive downstream commands and send it to the LwM2M client"""
+
+            zh: """下行命令主题。
+对于每个成功上线的新 LwM2M 客户端,网关会创建一个订阅关系来接收下行消息并将其发送给客户端。"""
+        }
+    }
+
+    lwm2m_translators_response {
+        desc {
+            en: """The topic for gateway to publish the acknowledge events from LwM2M client"""
+            zh: """用于网关发布来自 LwM2M 客户端的确认事件的主题。"""
+        }
+    }
+
+    lwm2m_translators_notify {
+        desc {
+            en: """The topic for gateway to publish the notify events from LwM2M client.
+After succeed observe a resource of LwM2M client, Gateway will send the notify events via this topic, if the client reports any resource changes"""
+
+            zh: """用于发布来自 LwM2M 客户端的通知事件的主题。
+在成功 Observe 到 LwM2M 客户端的资源后,如果客户端报告任何资源状态的变化,网关将通过该主题发送通知事件。"""
+        }
+    }
+
+    lwm2m_translators_register {
+        desc {
+            en: """The topic for gateway to publish the register events from LwM2M client."""
+            zh: """用于发布来自 LwM2M 客户端的注册事件的主题。"""
+        }
+    }
+
+    lwm2m_translators_update {
+        desc {
+            en: """The topic for gateway to publish the update events from LwM2M client"""
+            zh: """用于发布来自LwM2M客户端的更新事件的主题。"""
+        }
+    }
+
+    translator {
+        desc {
+            en: """MQTT topic that corresponds to a particular type of event."""
+            zh: """配置某网关客户端对于发布消息或订阅的主题和 QoS 等级。"""
+        }
+    }
+
+    translator_topic {
+        desc {
+            en: """Topic Name"""
+            zh: """主题名称"""
+        }
+    }
+
+    translator_qos {
+        desc {
+            en: """QoS Level"""
+            zh: """QoS 等级"""
+        }
+    }
+}

apps/emqx_gateway/src/lwm2m/include/emqx_lwm2m.hrl → apps/emqx_lwm2m/include/emqx_lwm2m.hrl


apps/emqx_gateway/src/lwm2m/lwm2m_xml/LWM2M_Access_Control-v1_0_1.xml → apps/emqx_lwm2m/lwm2m_xml/LWM2M_Access_Control-v1_0_1.xml


apps/emqx_gateway/src/lwm2m/lwm2m_xml/LWM2M_Connectivity_Statistics-v1_0_1.xml → apps/emqx_lwm2m/lwm2m_xml/LWM2M_Connectivity_Statistics-v1_0_1.xml


apps/emqx_gateway/src/lwm2m/lwm2m_xml/LWM2M_Device-v1_0_1.xml → apps/emqx_lwm2m/lwm2m_xml/LWM2M_Device-v1_0_1.xml


apps/emqx_gateway/src/lwm2m/lwm2m_xml/LWM2M_Firmware_Update-v1_0_1.xml → apps/emqx_lwm2m/lwm2m_xml/LWM2M_Firmware_Update-v1_0_1.xml


apps/emqx_gateway/src/lwm2m/lwm2m_xml/LWM2M_Location-v1_0.xml → apps/emqx_lwm2m/lwm2m_xml/LWM2M_Location-v1_0.xml


apps/emqx_gateway/src/lwm2m/lwm2m_xml/LWM2M_Security-v1_0.xml → apps/emqx_lwm2m/lwm2m_xml/LWM2M_Security-v1_0.xml


apps/emqx_gateway/src/lwm2m/lwm2m_xml/LWM2M_Server-v1_0.xml → apps/emqx_lwm2m/lwm2m_xml/LWM2M_Server-v1_0.xml


+ 2 - 0
apps/emqx_lwm2m/rebar.config

@@ -0,0 +1,2 @@
+{erl_opts, [debug_info]}.
+{deps, []}.

apps/emqx_gateway/src/lwm2m/binary_util.erl → apps/emqx_lwm2m/src/binary_util.erl


+ 10 - 0
apps/emqx_lwm2m/src/emqx_lwm2m.app.src

@@ -0,0 +1,10 @@
+{application, emqx_lwm2m, [
+    {description, "LwM2M Gateway"},
+    {vsn, "0.1.0"},
+    {registered, []},
+    {applications, [kernel, stdlib, emqx_gateway, emqx_coap]},
+    {env, []},
+    {modules, []},
+    {licenses, ["Apache 2.0"]},
+    {links, []}
+]}.

+ 21 - 19
apps/emqx_gateway/src/lwm2m/emqx_lwm2m_impl.erl

@@ -14,35 +14,37 @@
 %% limitations under the License.
 %%--------------------------------------------------------------------
 
-%% @doc The LwM2M Gateway Implement interface
--module(emqx_lwm2m_impl).
-
--behaviour(emqx_gateway_impl).
+%% @doc The LwM2M Gateway implement
+-module(emqx_lwm2m).
 
 -include_lib("emqx/include/logger.hrl").
+-include_lib("emqx_gateway/include/emqx_gateway.hrl").
 
-%% APIs
--export([
-    reg/0,
-    unreg/0
-]).
+%% define a gateway named stomp
+-gateway(#{
+    name => lwm2m,
+    callback_module => ?MODULE,
+    config_schema_module => emqx_lwm2m_schema
+}).
+
+%% callback_module must implement the emqx_gateway_impl behaviour
+-behaviour(emqx_gateway_impl).
 
+%% callback for emqx_gateway_impl
 -export([
     on_gateway_load/2,
     on_gateway_update/3,
     on_gateway_unload/2
 ]).
 
-%%--------------------------------------------------------------------
-%% APIs
-%%--------------------------------------------------------------------
-
-reg() ->
-    RegistryOptions = [{cbkmod, ?MODULE}],
-    emqx_gateway_registry:reg(lwm2m, RegistryOptions).
-
-unreg() ->
-    emqx_gateway_registry:unreg(lwm2m).
+-import(
+    emqx_gateway_utils,
+    [
+        normalize_config/1,
+        start_listeners/4,
+        stop_listeners/2
+    ]
+).
 
 %%--------------------------------------------------------------------
 %% emqx_gateway_registry callbacks

apps/emqx_gateway/src/lwm2m/emqx_lwm2m_api.erl → apps/emqx_lwm2m/src/emqx_lwm2m_api.erl


+ 1 - 1
apps/emqx_gateway/src/lwm2m/emqx_lwm2m_channel.erl

@@ -16,9 +16,9 @@
 
 -module(emqx_lwm2m_channel).
 
+-include("emqx_lwm2m.hrl").
 -include_lib("emqx/include/logger.hrl").
 -include_lib("emqx_coap/include/emqx_coap.hrl").
--include("src/lwm2m/include/emqx_lwm2m.hrl").
 
 %% API
 -export([

+ 1 - 1
apps/emqx_gateway/src/lwm2m/emqx_lwm2m_cmd.erl

@@ -16,9 +16,9 @@
 
 -module(emqx_lwm2m_cmd).
 
+-include("emqx_lwm2m.hrl").
 -include_lib("emqx/include/logger.hrl").
 -include_lib("emqx_coap/include/emqx_coap.hrl").
--include("src/lwm2m/include/emqx_lwm2m.hrl").
 
 -export([
     mqtt_to_coap/2,

+ 1 - 1
apps/emqx_gateway/src/lwm2m/emqx_lwm2m_message.erl

@@ -24,7 +24,7 @@
     translate_json/1
 ]).
 
--include("src/lwm2m/include/emqx_lwm2m.hrl").
+-include("emqx_lwm2m.hrl").
 
 tlv_to_json(BaseName, TlvData) ->
     DecodedTlv = emqx_lwm2m_tlv:parse(TlvData),

+ 184 - 0
apps/emqx_lwm2m/src/emqx_lwm2m_schema.erl

@@ -0,0 +1,184 @@
+%%--------------------------------------------------------------------
+%% Copyright (c) 2023 EMQ Technologies Co., Ltd. All Rights Reserved.
+%%
+%% Licensed under the Apache License, Version 2.0 (the "License");
+%% you may not use this file except in compliance with the License.
+%% You may obtain a copy of the License at
+%%
+%%     http://www.apache.org/licenses/LICENSE-2.0
+%%
+%% Unless required by applicable law or agreed to in writing, software
+%% distributed under the License is distributed on an "AS IS" BASIS,
+%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+%% See the License for the specific language governing permissions and
+%% limitations under the License.
+%%--------------------------------------------------------------------
+
+-module(emqx_lwm2m_schema).
+
+-include_lib("hocon/include/hoconsc.hrl").
+-include_lib("typerefl/include/types.hrl").
+
+-type duration() :: non_neg_integer().
+-type duration_s() :: non_neg_integer().
+
+-typerefl_from_string({duration/0, emqx_schema, to_duration}).
+-typerefl_from_string({duration_s/0, emqx_schema, to_duration_s}).
+
+-reflect_type([duration/0, duration_s/0]).
+
+%% config schema provides
+-export([fields/1, desc/1]).
+
+fields(lwm2m) ->
+    [
+        {xml_dir,
+            sc(
+                binary(),
+                #{
+                    %% since this is not packaged with emqx, nor
+                    %% present in the packages, we must let the user
+                    %% specify it rather than creating a dynamic
+                    %% default (especially difficult to handle when
+                    %% generating docs).
+                    example => <<"/etc/emqx/lwm2m_xml">>,
+                    required => true,
+                    desc => ?DESC(lwm2m_xml_dir)
+                }
+            )},
+        {lifetime_min,
+            sc(
+                duration(),
+                #{
+                    default => <<"15s">>,
+                    desc => ?DESC(lwm2m_lifetime_min)
+                }
+            )},
+        {lifetime_max,
+            sc(
+                duration(),
+                #{
+                    default => <<"86400s">>,
+                    desc => ?DESC(lwm2m_lifetime_max)
+                }
+            )},
+        {qmode_time_window,
+            sc(
+                duration_s(),
+                #{
+                    default => <<"22s">>,
+                    desc => ?DESC(lwm2m_qmode_time_window)
+                }
+            )},
+        %% TODO: Support config resource path
+        {auto_observe,
+            sc(
+                boolean(),
+                #{
+                    default => false,
+                    desc => ?DESC(lwm2m_auto_observe)
+                }
+            )},
+        %% FIXME: not working now
+        {update_msg_publish_condition,
+            sc(
+                hoconsc:enum([always, contains_object_list]),
+                #{
+                    default => contains_object_list,
+                    desc => ?DESC(lwm2m_update_msg_publish_condition)
+                }
+            )},
+        {translators,
+            sc(
+                ref(lwm2m_translators),
+                #{
+                    required => true,
+                    desc => ?DESC(lwm2m_translators)
+                }
+            )},
+        {mountpoint, emqx_gateway_schema:mountpoint("lwm2m/${endpoint_name}/")},
+        {listeners, sc(ref(emqx_gateway_schema, udp_listeners), #{desc => ?DESC(udp_listeners)})}
+    ] ++ emqx_gateway_schema:gateway_common_options();
+fields(lwm2m_translators) ->
+    [
+        {command,
+            sc(
+                ref(translator),
+                #{
+                    desc => ?DESC(lwm2m_translators_command),
+                    required => true
+                }
+            )},
+        {response,
+            sc(
+                ref(translator),
+                #{
+                    desc => ?DESC(lwm2m_translators_response),
+                    required => true
+                }
+            )},
+        {notify,
+            sc(
+                ref(translator),
+                #{
+                    desc => ?DESC(lwm2m_translators_notify),
+                    required => true
+                }
+            )},
+        {register,
+            sc(
+                ref(translator),
+                #{
+                    desc => ?DESC(lwm2m_translators_register),
+                    required => true
+                }
+            )},
+        {update,
+            sc(
+                ref(translator),
+                #{
+                    desc => ?DESC(lwm2m_translators_update),
+                    required => true
+                }
+            )}
+    ];
+fields(translator) ->
+    [
+        {topic,
+            sc(
+                binary(),
+                #{
+                    required => true,
+                    desc => ?DESC(translator_topic)
+                }
+            )},
+        {qos,
+            sc(
+                emqx_schema:qos(),
+                #{
+                    default => 0,
+                    desc => ?DESC(translator_qos)
+                }
+            )}
+    ].
+
+desc(lwm2m) ->
+    "The LwM2M protocol gateway.";
+desc(lwm2m_translators) ->
+    "MQTT topics that correspond to LwM2M events.";
+desc(translator) ->
+    "MQTT topic that corresponds to a particular type of event.";
+desc(_) ->
+    undefined.
+
+%%--------------------------------------------------------------------
+%% helpers
+
+sc(Type, Meta) ->
+    hoconsc:mk(Type, Meta).
+
+ref(StructName) ->
+    ref(?MODULE, StructName).
+
+ref(Mod, Field) ->
+    hoconsc:ref(Mod, Field).

+ 1 - 1
apps/emqx_gateway/src/lwm2m/emqx_lwm2m_session.erl

@@ -15,7 +15,7 @@
 %%--------------------------------------------------------------------
 -module(emqx_lwm2m_session).
 
--include("src/lwm2m/include/emqx_lwm2m.hrl").
+-include("emqx_lwm2m.hrl").
 -include_lib("emqx/include/logger.hrl").
 -include_lib("emqx/include/emqx.hrl").
 -include_lib("emqx/include/emqx_mqtt.hrl").

+ 1 - 1
apps/emqx_gateway/src/lwm2m/emqx_lwm2m_tlv.erl

@@ -25,7 +25,7 @@
 -export([binary_to_hex_string/1]).
 -endif.
 
--include("src/lwm2m/include/emqx_lwm2m.hrl").
+-include("emqx_lwm2m.hrl").
 
 -define(TLV_TYPE_OBJECT_INSTANCE, 0).
 -define(TLV_TYPE_RESOURCE_INSTANCE, 1).

+ 1 - 1
apps/emqx_gateway/src/lwm2m/emqx_lwm2m_xml_object.erl

@@ -16,7 +16,7 @@
 
 -module(emqx_lwm2m_xml_object).
 
--include("src/lwm2m/include/emqx_lwm2m.hrl").
+-include("emqx_lwm2m.hrl").
 -include_lib("xmerl/include/xmerl.hrl").
 
 -export([

+ 1 - 1
apps/emqx_gateway/src/lwm2m/emqx_lwm2m_xml_object_db.erl

@@ -16,7 +16,7 @@
 
 -module(emqx_lwm2m_xml_object_db).
 
--include("src/lwm2m/include/emqx_lwm2m.hrl").
+-include("emqx_lwm2m.hrl").
 -include_lib("xmerl/include/xmerl.hrl").
 -include_lib("emqx/include/logger.hrl").
 

+ 3 - 2
apps/emqx_gateway/test/emqx_lwm2m_SUITE.erl

@@ -31,8 +31,8 @@
 
 -define(LOGT(Format, Args), ct:pal("TEST_SUITE: " ++ Format, Args)).
 
--include("src/lwm2m/include/emqx_lwm2m.hrl").
--include("src/coap/include/emqx_coap.hrl").
+-include("emqx_lwm2m.hrl").
+-include_lib("emqx_coap/include/emqx_coap.hrl").
 -include_lib("eunit/include/eunit.hrl").
 -include_lib("common_test/include/ct.hrl").
 -include_lib("snabbkaffe/include/snabbkaffe.hrl").
@@ -134,6 +134,7 @@ groups() ->
 init_per_suite(Config) ->
     %% load application first for minirest api searching
     application:load(emqx_gateway),
+    application:load(emqx_lwm2m),
     emqx_mgmt_api_test_util:init_suite([emqx_conf, emqx_authn]),
     Config.
 

+ 4 - 3
apps/emqx_gateway/test/emqx_lwm2m_api_SUITE.erl

@@ -23,8 +23,8 @@
 
 -define(LOGT(Format, Args), ct:pal("TEST_SUITE: " ++ Format, Args)).
 
--include("src/lwm2m/include/emqx_lwm2m.hrl").
--include("src/coap/include/emqx_coap.hrl").
+-include("emqx_lwm2m.hrl").
+-include("emqx_coap/include/emqx_coap.hrl").
 -include_lib("eunit/include/eunit.hrl").
 -include_lib("common_test/include/ct.hrl").
 
@@ -81,8 +81,9 @@ all() ->
     emqx_common_test_helpers:all(?MODULE).
 
 init_per_suite(Config) ->
-    ok = emqx_common_test_helpers:load_config(emqx_gateway_schema, ?CONF_DEFAULT),
     application:load(emqx_gateway),
+    application:load(emqx_lwm2m),
+    ok = emqx_common_test_helpers:load_config(emqx_gateway_schema, ?CONF_DEFAULT),
     emqx_mgmt_api_test_util:init_suite([emqx_conf, emqx_authn]),
     Config.
 

+ 2 - 2
apps/emqx_gateway/test/emqx_tlv_SUITE.erl

@@ -21,8 +21,8 @@
 
 -define(LOGT(Format, Args), logger:debug("TEST_SUITE: " ++ Format, Args)).
 
--include("src/lwm2m/include/emqx_lwm2m.hrl").
--include("src/coap/include/emqx_coap.hrl").
+-include("emqx_lwm2m.hrl").
+-include("emqx_coap/include/emqx_coap.hrl").
 -include_lib("eunit/include/eunit.hrl").
 
 %%--------------------------------------------------------------------

+ 1 - 0
mix.exs

@@ -284,6 +284,7 @@ defmodule EMQXUmbrella.MixProject do
         emqx_stomp: :permanent,
         emqx_mqttsn: :permanent,
         emqx_coap: :permanent,
+        emqx_lwm2m: :permanent,
         emqx_exhook: :permanent,
         emqx_bridge: :permanent,
         emqx_rule_engine: :permanent,

+ 2 - 1
rebar.config.erl

@@ -392,6 +392,7 @@ relx_apps(ReleaseType, Edition) ->
             emqx_stomp,
             emqx_mqttsn,
             emqx_coap,
+            emqx_lwm2m,
             emqx_exhook,
             emqx_bridge,
             emqx_rule_engine,
@@ -452,7 +453,7 @@ relx_overlay(ReleaseType, Edition) ->
         {copy, "bin/emqx_ctl", "bin/emqx_ctl-{{release_version}}"},
         %% for relup
         {copy, "bin/install_upgrade.escript", "bin/install_upgrade.escript-{{release_version}}"},
-        {copy, "apps/emqx_gateway/src/lwm2m/lwm2m_xml", "etc/lwm2m_xml"},
+        {copy, "apps/emqx_lwm2m/lwm2m_xml", "etc/lwm2m_xml"},
         {copy, "apps/emqx_authz/etc/acl.conf", "etc/acl.conf"},
         {template, "bin/emqx.cmd", "bin/emqx.cmd"},
         {template, "bin/emqx_ctl.cmd", "bin/emqx_ctl.cmd"},