Commit Graph

86 Commits

Author SHA1 Message Date
Abtin Keshavarzian 5cae26e22b [coap] simplify Coap::Message implementation (#12313)
This change simplifies the `Coap::Message` implementation and removes
the fragile `HelpData` struct which was used to cache header
information within reserved portion of message.

The `Coap::Msg` class is updated to hold the parsed CoAP header
information (type, code, message ID, token). It now inherits from a
new `HeaderInfo` class which contains the parsed fields. This change
helps to simplify many of the call sites which previously had to parse
the header information themselves.

The key changes are:
- The `HelpData` struct is removed from `Coap::Message`.
- `Coap::Msg` is updated to track parsed header info in `HeaderInfo`.
- `otCoapMessageInit()` and `otCoapMessageInitResponse()` now return an
  `otError`.
- Methods are renamed to harmonize their names.
- A new unit test `test_coap_message.cpp` is added to verify the
  `Coap::Message` implementation.
2026-01-23 10:52:42 -08:00
Abtin Keshavarzian f4816375c0 [coap] introduce Coap::Msg to simplify method calls (#12285)
This commit introduces `Coap::Msg`, a class that encapsulates
`Coap::Message` and its associated `Ip6::MessageInfo`.

`Coap` methods and TMF resource handlers are updated to use
`Coap::Msg` when handling received messages. This change simplifies
method signatures by reducing the number of parameters and enables
future extensibility for tracking additional information related to
received CoAP messages.
2026-01-14 10:18:17 -08:00
Abtin Keshavarzian c0f4cccc65 [tmf] introduce DeclareTmfResponseHandlerIn helper macros (#12187)
This commit introduces two new macros, `DeclareTmfResponseHandlerIn`
and `DeclareTmfResponseHandlerFullParamIn`, to simplify the
definition of TMF/CoAP response handlers.

These macros generate the boilerplate code for the `static` callback
method within a class and delegate the call to a non-static member
method with the same name. This removes the repetitive pattern of
manually defining the `static` wrapper in each class.

The existing response handlers across various core components are
updated to use the new helper macros.
2025-12-01 10:21:01 -08:00
Abtin Keshavarzian 27932f2006 [net] move slaac module from utils to net (#11955)
This commit moves the `SlaacAddress` module from `src/core/utils`
to `src/core/net`.

This change also updates the namespace from `ot::Utils` to
`ot::Ip6` and updates all includes and usages throughout the
codebase.

The `SlaacAddress` module was originally placed in `core/utils`
as SLAAC management was historically handled by the platform.
Now that the OpenThread stack manages SLAAC directly, this logic
is better placed under `core/net`.
2025-09-22 19:29:01 -07:00
Abtin Keshavarzian fd7746b1e1 [core] harmonize access to MLE component using Get<Mle::Mle>() (#11874)
This change updates multiple modules to consistently use
`Get<Mle::Mle>()` for accessing the MLE component, removing the need
for local `Mle::Mle &mle` references.

This approach aligns with the common `Get<Module>()` access pattern
used across the codebase, improving consistency.
2025-08-28 14:29:48 -07:00
Abtin Keshavarzian b6f1252bd8 [netdata] enhance FindContext() methods and Lowpan::Context (#11836)
This commit enhances the `FindContext()` methods and converts the
`Lowpan::Context` struct into a class.

The `FindContext()` methods are updated as follows:
- Renamed to `FindContextForAddress()` and `FindContextForId()` to
  more accurately reflect their function.
- The return type is changed from `Error` to `void`. Success is now
  indicated by checking the `IsValid()` state of the output `Context`
  object (matching how `Lowpan` class uses the `Context`). This
  change simplifies the callers and harmonizes the context check
  across different modules.

The `Lowpan::Context` struct is converted into a class, encapsulating
its members by making them private and introducing public getters.
2025-08-22 14:30:14 -07:00
Abtin Keshavarzian 7ab5c5c042 [mle] simplify ThreadStatusTlv handling (#11782)
The Status TLV is used in several TMF messages (Address Solicit, DUA
Registration, MLR), and the meaning of its value is
context-dependent.

This commit refactors the TLV definitions by moving the status enums
from the generic `ThreadStatusTlv` class into the modules where they
are used.

The main changes are:
- The Address Solicit status is split into two more specific enums:
  - `RouterUpgradeReason` for Address Solicit requests.
  - `AddrSolicitResponse` for Address Solicit responses.
- `MlrStatus` and `DuaStatus` enums are moved to `mlr` and `dua`
  modules respectively.
- `ThreadStatusTlv` is simplified to a `typedef`.

This change improves code clarity, modularity, and type safety by
ensuring that status codes are defined and used within their proper
context.
2025-08-06 19:02:01 -07:00
Abtin Keshavarzian 0cf1ee09eb [mle] simplify WillBecomeRouterSoon for clarity (#11768)
This commit renames `IsExpectedToBecomeRouterSoon()` to
`WillBecomeRouterSoon()` to better reflect its behavior.

The implementation is simplified by using the `VerifyOrExit` pattern
instead of a single complex boolean expression, which improves code
readability.
2025-08-01 11:10:52 -07:00
Abtin Keshavarzian f73d64e611 [settings] change save/delete methods to return void (#11735)
This commit simplifies the `Settings` API by modifying all "Save"
and "Delete" methods to return `void` instead of `Error`.

Settings operations are required for a Thread device to function, so a
failure to save or delete from non-volatile storage should be treated
as a critical error. Previously, the code effectively ignored these
errors using `IgnoreError()`. This change instead treats any such
failure as a critical error, triggering an assert within the
`Settings` module.

The key changes include:
- `Settings::Save<T>()`, `Delete<T>()`, `DeleteAllChildInfo()`, and
  similar methods now return `void`.
- Internal `Settings` methods use `SuccessOrAssert()` to assert on
  errors.
- The responsibility for asserting on `kErrorNotImplemented` is moved
  to the `SettingsDriver` layer.

This update simplifies the caller logic by removing the need for
`IgnoreError()` at many call sites. Consequently, several methods
that primarily wrapped `Settings` calls, such as `Mle::Store()` and
`BorderAgent::SetId()`, have also been updated to return `void`.
2025-07-21 22:46:19 -07:00
Jonathan Hui 65098ebcc1 [dua] suppress warning logs on normal behaviors (#11483)
In `PerformNextRegistration()`, there are many reasons why no
registration message will be sent. For example, device is not attached
or there are no DUAs to register.

This commit suppresses warn-level log messages when no registration
message is expected and normal behavior.
2025-05-08 20:38:50 -07:00
Abtin Keshavarzian 0c1dfa0796 [mle] refactor MleRouter and Mle classes into a single Mle class (#11411)
This commit refactors the `Mle` modules and combines the `MleRouter`
and `Mle` classes into a single `Mle` class which now handles both
FTD and MTD functionalities.

The `MleRouter` and `Mle` classes were originally intended as
sub-classes, where the base class `Mle` would provide MTD and common
behaviors, and `MleRouter` would implement FTD-specific behaviors.
However, over the years and as new features were implemented, these
two classes became more intertwined, and the `Mle` class began to
include many FTD-related functions and interactions with `MleRouter`
private variables and methods.

This commit simplifies the code by combining the two into a single
class. The previous `mle_router.cpp` file is also renamed to
`mle_ftd.cpp` to indicate that it implements FTD-specific MLE
behaviors.
2025-04-18 14:28:47 -07:00
Yakun Xu dc4acf0346 [coap] use otError in C handlers (#11115)
This commit changes CoAP handlers to use otError in the function or
method definitions. This ensures the definitions are consistent with
the declaration in coap.h.
2025-01-07 15:48:38 -08:00
Abtin Keshavarzian 072e53740b [message] simplify accessing of indirect tx ChildMask bit-set (#10824)
This commit renames the `BitVector<>` class to `BitSet<>` and updates
its methods, introducing simpler methods such as `Add()`, `Remove()`,
and `IsEmpty()`. This aligns better with the intended use of this
class as a bit-set, e.g., as a `ChildMask` to track the set of
sleepy children to which a message is scheduled for indirect
transmission.

The `Message` class now provides the `GetIndirectTxChildMask()`
method, which returns a reference to the `ChildMask` bit-set.
2024-10-14 12:12:09 -07:00
Abtin Keshavarzian 9f6921cf04 [core] optimize #include directives in core cpp files (#10717)
This commit removes unnecessary `#include` directives in core `cpp`
files that already include `instance.hpp`. The `instance.hpp` header
itself includes all necessary OT core headers and definitions as they
are contained within an `ot::Instance`.
2024-09-18 12:03:54 -07:00
Abtin Keshavarzian 5ff30d7cef [core] move locator Get<Type> definitions to instance.hpp (#10711)
This commit moves the locator `GetProvider::Get<Type>()` and its
related `Timer` and `Tasklet` methods to `instance.hpp`. With this
change we can remove `locator_getters.hpp` header fully thus
simplifying the code.
2024-09-16 10:10:51 -07:00
Rongli Sun cbd35e34a7 [dua] resume registration when fails to become router (#10559) 2024-08-01 08:15:10 -07:00
Abtin Keshavarzian 5127e7e5c0 [mle] enhance RLOC and ALOC address generation (#10391)
This commit enhances the generation of RLOC and ALOC addresses in the
`Mle` class.

Methods retrieving RLOC/ALOC addresses no longer return an `Error` and
are now `void`. The previous check for a valid `GetRloc16()` value,
which returned `kErrorDetach` if unset, is removed because this
scenario only occurred before the device's initial attachment.
Post-attachment, the RLOC16 remains valid even if the device is
detached. This change simplifies the code since RLOC/ALOC address
retrieval happens after the initial attachment.

Additional enhancements include:

-   Changing the input parameter order in `GetCommissionerAloc()` to
    match `GetServiceAloc()`.
-   Using `Ip6::Address::SetToRoutingLocator()` to construct RLOC
    addresses, harmonizing address generation and improving code
    readability.
2024-06-17 09:23:43 -07:00
Abtin Keshavarzian 3315c44bb3 [mle] rename mesh local address methods to use Rloc and Eid (#10385)
This commit renames the methods for retrieving mesh local addresses to
`GetMeshLocalRloc()` and `GetMeshLocalEid()` (from `GetMeshLocal16()`
and `GetMeshLocal64()`, respectively) to align with the terminology
used in the Thread specification. Member and local variables are also
renamed accordingly.
2024-06-14 10:06:25 -07:00
Abtin Keshavarzian ca7dd8288b [mle] simplify signaling of DUA address change (#10296)
This commit simplifies how `ProcessAddressRegistrationTlv()` signals
DUA address changes. Signaling now occurs after all child addresses
are registered, using a new `SignalDuaAddressEvent()` method. This
method checks the old and new DUA addresses to determine the
appropriate event to signal.

Additionally, `Child::GetDomainUnicastAddress()` is updated to return
an `Error` and copy the DUA address into a provided `Ip6::Address`
reference.
2024-05-31 12:00:30 -07:00
Abtin Keshavarzian 223935bf0c [logging] introduce LogWarnOnError() for standardized error logging (#9996)
This commit adds `LogWarnOnError()` to emit warning-level logs on
errors. The emitted log includes the error code. This replaces custom
`LogError()` across modules, simplifying the code.
2024-04-05 09:07:19 -07:00
Abtin Keshavarzian a05954b01e [slaac] simplify adding/removing addresses and other enhancements (#9579)
This commit makes the following smaller enhancements in `Slaac`
class:
- Adds a new `ShouldUseForSlaac()` method to check if a network data
  prefix should be used for SLAAC, checking flags and applying the
  filter if set.
- Introduces separate `RemoveAddresses()` and `AddAddresses()` methods
  to manage SLAAC addresses, replacing the previous `Update()` method.
- Adds helper methods to `RemoveAllAddresses()`, `RemoveAddress()` to
  remove a specific address, and `AddAddressFor(prefix)` to  generate
  and add an address for a given prefix.
- Simplifies `GenerateIid()` by removing unused input parameters.
2023-11-07 12:43:02 -08:00
Abtin Keshavarzian 1528c8831d [core] define core/instance folder for instance modules (#9561)
This commit moves the `instance` module to a newly added folder
`core/instance` (from `core/common`.  Header file `extension.hpp`
and its example is also moved to the same folder.
2023-10-26 05:08:24 -07:00
Abtin Keshavarzian 50e20c8958 [mle-router] add RouterRoleTransition nested class (#9490)
This commit adds `RouterRoleTransition` nested class to `MleRouter`.
This class tracks the timeout and jitter intervals for router role
transitions, i.e., device upgrading to router role from REED or
downgrading from router to REED. It provides helper methods like
`IsPending()` and `StartTimeout()` to check if role transition
is pending or to start the timeout countdown. These methods help to
simplify the code and make it more readable.
2023-10-06 12:09:48 -07:00
Abtin Keshavarzian 82b31adfd0 [bbr] simplify DomainPrefixEvent (#9152)
- Rename to `DomainPrefixEvent` from `DomainPrefixState`
- Remove the unused `kDomainPrefixNone` enumeration
2023-06-08 19:54:59 -07:00
Abtin Keshavarzian 982057e095 [mle] simplify tracking of registered DUA address by child (#9137) 2023-06-06 15:06:21 -07:00
Abtin Keshavarzian 161a7fddcf [bbr] move BBR constants to related source files (#9129)
This commit moves BBR and DUA related constants to the related source
files (from `mle_types.hpp`). Additionally, unused constant variables
are removed. Some of the constants are renamed to make the definition
more clear.
2023-06-05 12:34:28 -07:00
Jonathan Hui 3d39d24a21 [style] fix spelling errors (#8939) 2023-04-11 22:39:35 -07:00
Abtin Keshavarzian adde085fce [tmf] add UriToString() for logging (#8930)
This commit adds a helper function `UriToString<Uri>()` to convert
a given `Uri` enumeration value to a human-readable string. This is
used for logging.
2023-04-05 18:00:30 -07:00
Suvesh Pratapa 07d69f30ce [dua] remove DUA address if network data no longer contains DUA prefix (#8874)
Fix for the following bug:

Leader/Parent (OTBR) is reset (taken off the network), leading to its
MTD child being detached. When the parent is brought back on the
network without configuring the previous DUA prefix, the MTD asserts
complaining that its DUA prefix is not valid anymore.

1. We don't assert anymore in this situation, rather the MTD just
   won't include the DUA address in its address registration TLV.

2. More importantly, we remove a "cached" DUA address if we find that
   the network leader is no longer configured with a DUA prefix.
2023-03-21 22:14:31 -07:00
doru91 c7925dd1df [dua] avoid unnecessary time ticker processing (#8872)
If there is no DUA request sent/scheduled, then there is no need to
trigger the periodic one second ticker timer.

This helps SEDs to be more power efficient and to avoid unnecessary
wakeups.

Signed-off-by: Doru Gucea <doru-cristian.gucea@nxp.com>
2023-03-21 17:28:42 -07:00
Abtin Keshavarzian 108ce57b9d [backbone-router] define C++ enum/types mirroring public definitions (#8809)
This commit defines C++ style types for use in core module mirroring
the public OT C definitions. It also contains renames and smaller
enhancements in BackBone Router modules.
2023-02-27 09:48:56 -08:00
Jonathan Hui 9c467a23ae [clang-format] apply v14 changes (#8490) 2022-12-07 16:23:20 -08:00
Jonathan Hui ba826ffb2a [clang-tidy] add readability-redundant-control-flow (#8365) 2022-11-04 16:34:37 -07:00
Abtin Keshavarzian 4a0d069ee8 [log] fix incorrect format strings (#8255) 2022-10-07 21:51:06 -07:00
Abtin Keshavarzian 812c36f658 [tmf] new model for handling TMF URI resources (#8233)
This commit updates and simplifies the handling of TMF URI resources.
The new model centralizes the processing and dispatching of URI
handlers in `Tmf::Agent`. It should help reduce memory (RAM and
code) use as well.

- A new enumeration `Uri` type is defined specifying all the TMF
  URIs along with functions to map between `Uri` and the related
  URI path string.
- We use binary search in a sorted table to match a path string
  (e.g., from a received CoAP message) to its corresponding URI.
- `Coap` class is updated to allow sub-classes to register an
   optional `ResourceHandler` to handle URI resources.
- `Tmf::Agent` uses the new `ResourceHandler` to process the TMF URI
  resources and invoke the related handlers on the proper module
  (e.g., passing `kUriAddressQuery` to `AddressResolver` module,
  `kUriActiveSet` to `MeshCoP::ActiveDatasetManager`, etc).
- The TMF handlers are defined as `HandleTmf<Uri>()` template method
  specializations which are defined on the proper module.
- Most URI handlers can be processed at any time but some perform
  additional checks, e.g., `kUriAddressSolicit` is passed to
  `Mle::MleRouter` which checks that device role is leader before
  processing the request.
2022-10-06 18:32:22 -07:00
Abtin Keshavarzian 9699d319af [takelet] add template TaskeltIn to simplify handler functions (#8064)
This class adds a template sub-class of `Tasklet` which allows
us to directly specify an `Owner` of tasklet along with handler
callback as a member method of `Owner`. This helps simplify the
use of `Tasklet` in core module.
2022-08-24 10:44:48 -07:00
Abtin Keshavarzian cc003b3a42 [coap] add new helper to allocate and init CoAP message (#7631)
This commit adds new helper methods in `Coap` to allocate a new
message and initialize it as a confirmable or non-confirmable post to
a given URI path. It also adds a new helper method to allocate a CoAP
response message for a given request. These methods help simplify the
preparation of CoAP and TMF messages.
2022-05-06 10:27:55 -07:00
Abtin Keshavarzian ba5ebc7296 [tmf] add Tmf::MessageInfo (#7589)
This commit adds `Tmf::MessageInfo` which is a sub-class of the
`Ip6::MessageInfo` class intended for use when sending TMF messages.
The peer port number is by default set to the TMF port from the
constructor. This class also provides helper methods for commonly
used patterns, e.g., source or `SockAddr` set to device's RLOC
address and/or the destination or `PeerAddr` set to leader ALOC or
RLOC, or a given address.
2022-04-14 09:26:43 -07:00
Abtin Keshavarzian 564982c818 [log] implement new logging model with module name support (#7385)
This commit implements new logging model in OpenThread. Each core
module can specify its own module name using `RegisterLogModule()`.
The registered log module name is then included in the all the log
messages emitted from the specific file. This model replaces and
enhances the log region model.
2022-02-17 15:50:45 -08:00
Abtin Keshavarzian 0298586f97 [core] use AsCoreType() in core modules (#7091)
This commit uses `AsCoreType()` and other functions from header
`as_core_types.hpp` to cast between public OT C style definitions to
the related C++ core types in the core modules.
2021-10-25 13:21:26 -07:00
Simon Lin 06ca16a38f [dua] fix potential issue in DUA.rsp & DUA.ntf processing (#6816)
There is a potential bug in
DuaManager::ProcessDuaResponse(Coap::Message &aMessage).

The original code finds the Child by Child *child =
Get<ChildTable>().GetChildAtIndex(mChildIndexDuaRegistering);, which
assumes that the DUA.rsp must be targeting the current registering
Child. This is true for DUA.rsp, but not correct for DUA.ntf because
BBR may send DUA.ntf at any time to any device.

So, this commit changes DuaManager::ProcessDuaResponse code to search
for the correct Child with the target DUA instead of using
mChildIndexDuaRegistering.

This commit also contains other refactoring on
mChildIndexDuaRegistering usage.
2021-08-12 09:27:22 -07:00
Simon Lin 8679d997a5 [dua] fix infinite DUA.req loop (#6797)
Verified that this commit fixes the infinite DUA.req loop.

This is a quick fix. We still need to reactor
mChildIndexDuaRegistering because there are still potential issues.
2021-07-07 12:49:18 -07:00
Abtin Keshavarzian a24b67ef30 [settings] simplify and enhance Settings (#6683)
This commit updates the `Settings` implementation. It adds template
versions of methods `Read<EntryType>()`, `Save<EntryType>()`, and
`Delete<EntryType>()` which make it easier for other modules to use,
while ensuing to share a common underlying implementation for all
entry types.

The settings entry types (e.g., `NetworkInfo`, `ParentInfo`, etc.) are
changed in this commit to provide a constant `kKey` specifying the
associated setting key with the entry. For simple entries, a related
class is added to define the `kKey` constant and the associated value
type (e.g., `OmrPrefix` defines `Ip6::Prefix` as its `ValueType`).
This model makes it easier to add new setting keys and entries in
future.

This commit also simplifies the logging in `Settings`. The entry types
now provide `Log()` method to log their data and a common
`Settings::Log()` method is added which handles all logging (on
success or in case of an error).

Finally, this commit moves the `SettingsDriver` definition to its own
`common/settings_driver.hpp` header file and and ensures that its
methods are defined as inline.
2021-05-27 15:57:06 -07:00
Simon Lin cf21d5760a [dua] fix Parent failed to send reregister for MTD Child on receiving ST_DUA_REREGISTER (#6637)
This commit enhances DUA response handling for ST_DUA_REREGISTER:
- Parent will stop registering DUA for MTD Child until the next Child
  Update Request.
- MTD Child will re-register its DUA by Child Update Request or
  DUA.req.
2021-05-17 22:22:53 -07:00
Simon Lin 822f464b9e [dua] send DUA.ntf to notify Child of unsuccessful DUA registration (#6625)
Parent should send DUA.ntf to Child that requested the DUA when status
is not ST_DUA_SUCCESS, according to Thread 1.2 Spec. 5.23.6.2.

Fixes DUA-TC-17.
2021-05-14 10:23:26 -07:00
Jonathan Hui 2f637e99fd [network-data] limit router upgrade delay when registering (#6613)
If a device is a REED and waiting the random delay to upgrade to a
router, the device will wait to register its network data, since it
will have to register again after upgrading to a router. However, the
maximum delay for becoming a router is up to 2 minutes, which is a
long time to wait.

This commit limits the delay that a device will wait to register its
network data.
2021-05-12 11:48:14 -07:00
Rongli Sun a7b429c193 [dua] fix unnessary reregistration (#6591) 2021-05-10 21:38:31 -07:00
Rongli Sun a74e0a35c4 [dua] avoid unnecessary log (#6591) 2021-05-10 21:38:31 -07:00
Rongli Sun e99d16e81c [dua/mlr-manager] refine logs (#6590) 2021-05-07 19:27:11 -07:00
Abtin Keshavarzian 8a0e7b8c4e [dua-manager] avoid null dereference in CoAP response handler (#6509)
This commit changes `DuaManager::HandleDuaResponse()` to use a pointer
to `aMessage` and `aMessageInfo` (instead of reference) Note that in
case of a failure, the `Coap` module may invoke the response handler
with `nullptr` for these parameters.
2021-04-25 21:41:34 -07:00