|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, The OpenThread Authors. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * 1. Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * 3. Neither the name of the copyright holder nor the |
| 13 | + * names of its contributors may be used to endorse or promote products |
| 14 | + * derived from this software without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + */ |
| 28 | + |
| 29 | +#include <stdarg.h> |
| 30 | +#include <stdio.h> |
| 31 | +#include <string.h> |
| 32 | + |
| 33 | +#include "platform/nexus_core.hpp" |
| 34 | +#include "platform/nexus_node.hpp" |
| 35 | + |
| 36 | +namespace ot { |
| 37 | +namespace Nexus { |
| 38 | + |
| 39 | +static bool sRequestHandlerCalled = false; |
| 40 | +static bool sNotificationReceived = false; |
| 41 | +static uint32_t sObserveValue = 0; |
| 42 | +static char sReceivedPayload[32]; |
| 43 | +static bool sSubscriptionActive = false; |
| 44 | + |
| 45 | +static Coap::Token sSubscriberToken; |
| 46 | +static otIp6Address sSubscriberAddr; |
| 47 | +static uint16_t sSubscriberPort; |
| 48 | +static bool sSubscriberPresent = false; |
| 49 | + |
| 50 | +static void HandleRequest(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo) |
| 51 | +{ |
| 52 | + Instance &instance = *static_cast<Instance *>(aContext); |
| 53 | + Coap::Message &message = AsCoapMessage(aMessage); |
| 54 | + Coap::Code code = static_cast<Coap::Code>(message.ReadCode()); |
| 55 | + Coap::Message *response = nullptr; |
| 56 | + |
| 57 | + Log("HandleRequest called"); |
| 58 | + |
| 59 | + sRequestHandlerCalled = true; |
| 60 | + |
| 61 | + if (code == Coap::kCodeGet) |
| 62 | + { |
| 63 | + response = instance.Get<Coap::ApplicationCoap>().NewMessage(); |
| 64 | + VerifyOrQuit(response != nullptr); |
| 65 | + |
| 66 | + SuccessOrQuit(response->InitAsResponse(Coap::kTypeAck, Coap::kCodeContent, message)); |
| 67 | + |
| 68 | + Coap::Option::Iterator iterator; |
| 69 | + SuccessOrQuit(iterator.Init(message, Coap::kOptionObserve)); |
| 70 | + if (iterator.GetOption() != nullptr) |
| 71 | + { |
| 72 | + uint64_t observe = 0; |
| 73 | + SuccessOrQuit(iterator.ReadOptionValue(observe)); |
| 74 | + |
| 75 | + if (observe == 0) |
| 76 | + { |
| 77 | + // New subscriber |
| 78 | + sSubscriberAddr = aMessageInfo->mPeerAddr; |
| 79 | + sSubscriberPort = aMessageInfo->mPeerPort; |
| 80 | + SuccessOrQuit(message.ReadToken(sSubscriberToken)); |
| 81 | + sSubscriberPresent = true; |
| 82 | + |
| 83 | + // Append Observe option to response |
| 84 | + SuccessOrQuit(response->AppendObserveOption(0)); |
| 85 | + } |
| 86 | + else if (observe == 1) |
| 87 | + { |
| 88 | + // Cancel subscription |
| 89 | + sSubscriberPresent = false; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + SuccessOrQuit(response->AppendPayloadMarker()); |
| 94 | + SuccessOrQuit(response->AppendBytes("Test123", 7)); |
| 95 | + |
| 96 | + SuccessOrQuit(instance.Get<Coap::ApplicationCoap>().SendMessage(*response, AsCoreType(aMessageInfo))); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +static void HandleNotification(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aResult) |
| 101 | +{ |
| 102 | + OT_UNUSED_VARIABLE(aContext); |
| 103 | + OT_UNUSED_VARIABLE(aMessageInfo); |
| 104 | + |
| 105 | + Log("HandleNotification called with result %d", aResult); |
| 106 | + |
| 107 | + if (!sSubscriptionActive) |
| 108 | + { |
| 109 | + Log("Subscription inactive, ignoring message"); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + if (aResult != OT_ERROR_NONE) |
| 114 | + { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + sNotificationReceived = true; |
| 119 | + |
| 120 | + uint16_t length = AsCoapMessage(aMessage).GetLength() - AsCoapMessage(aMessage).GetOffset(); |
| 121 | + Log("Message length: %u, offset: %u", AsCoapMessage(aMessage).GetLength(), AsCoapMessage(aMessage).GetOffset()); |
| 122 | + |
| 123 | + if (length > 0) |
| 124 | + { |
| 125 | + length = Min(length, static_cast<uint16_t>(sizeof(sReceivedPayload) - 1)); |
| 126 | + SuccessOrQuit(AsCoapMessage(aMessage).Read(AsCoapMessage(aMessage).GetOffset(), sReceivedPayload, length)); |
| 127 | + sReceivedPayload[length] = '\0'; |
| 128 | + } |
| 129 | + |
| 130 | + // Only check options if it's a notification (payload starts with "msg") |
| 131 | + if (strncmp(sReceivedPayload, "msg", 3) == 0) |
| 132 | + { |
| 133 | + Coap::Option::Iterator iterator; |
| 134 | + SuccessOrQuit(iterator.Init(AsCoapMessage(aMessage), Coap::kOptionObserve)); |
| 135 | + if (iterator.GetOption() != nullptr) |
| 136 | + { |
| 137 | + uint64_t observe = 0; |
| 138 | + SuccessOrQuit(iterator.ReadOptionValue(observe)); |
| 139 | + sObserveValue = static_cast<uint32_t>(observe); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +void TestCoapObserve(void) |
| 145 | +{ |
| 146 | + Core nexus; |
| 147 | + |
| 148 | + Node &leader = nexus.CreateNode(); |
| 149 | + Node &router = nexus.CreateNode(); |
| 150 | + |
| 151 | + nexus.AdvanceTime(0); |
| 152 | + |
| 153 | + SuccessOrQuit(Instance::SetGlobalLogLevel(kLogLevelInfo)); |
| 154 | + |
| 155 | + Log("Form network"); |
| 156 | + leader.Form(); |
| 157 | + nexus.AdvanceTime(13 * 1000); |
| 158 | + VerifyOrQuit(leader.Get<Mle::Mle>().IsLeader()); |
| 159 | + |
| 160 | + router.Join(leader); |
| 161 | + nexus.AdvanceTime(10 * 1000); |
| 162 | + VerifyOrQuit(router.Get<Mle::Mle>().IsChild() || router.Get<Mle::Mle>().IsRouter()); |
| 163 | + |
| 164 | + // Start CoAP on Leader |
| 165 | + SuccessOrQuit(leader.Get<Coap::ApplicationCoap>().Start(OT_DEFAULT_COAP_PORT)); |
| 166 | + |
| 167 | + Coap::Resource resource("test", &HandleRequest, &leader.GetInstance()); |
| 168 | + leader.Get<Coap::ApplicationCoap>().AddResource(resource); |
| 169 | + |
| 170 | + // Start CoAP on Router |
| 171 | + SuccessOrQuit(router.Get<Coap::ApplicationCoap>().Start(OT_DEFAULT_COAP_PORT)); |
| 172 | + |
| 173 | + // Router sends Observe request |
| 174 | + Coap::Message *message = router.Get<Coap::ApplicationCoap>().NewMessage(); |
| 175 | + VerifyOrQuit(message != nullptr); |
| 176 | + SuccessOrQuit(message->Init(Coap::kTypeConfirmable, Coap::kCodeGet)); |
| 177 | + SuccessOrQuit(message->AppendObserveOption(0)); |
| 178 | + SuccessOrQuit(message->AppendUriPathOptions("test")); |
| 179 | + |
| 180 | + Coap::Token token; |
| 181 | + SuccessOrQuit(token.SetToken(reinterpret_cast<const uint8_t *>("12345678"), 8)); |
| 182 | + IgnoreError(message->WriteToken(token)); |
| 183 | + |
| 184 | + Ip6::MessageInfo messageInfo; |
| 185 | + messageInfo.SetPeerAddr(leader.Get<Mle::Mle>().GetMeshLocalEid()); |
| 186 | + messageInfo.SetPeerPort(OT_DEFAULT_COAP_PORT); |
| 187 | + |
| 188 | + sRequestHandlerCalled = false; |
| 189 | + sSubscriberPresent = false; |
| 190 | + sSubscriptionActive = true; |
| 191 | + |
| 192 | + SuccessOrQuit(router.Get<Coap::ApplicationCoap>().SendMessageWithResponseHandlerSeparateParams( |
| 193 | + *message, messageInfo, nullptr, &HandleNotification, nullptr, nullptr, nullptr)); |
| 194 | + |
| 195 | + nexus.AdvanceTime(5 * 1000); |
| 196 | + |
| 197 | + VerifyOrQuit(sRequestHandlerCalled); |
| 198 | + VerifyOrQuit(sSubscriberPresent); |
| 199 | + VerifyOrQuit(sNotificationReceived); // Initial response acts as first notification |
| 200 | + VerifyOrQuit(strcmp(sReceivedPayload, "Test123") == 0); |
| 201 | + |
| 202 | + // Now Leader sends a notification |
| 203 | + sNotificationReceived = false; |
| 204 | + |
| 205 | + Coap::Message *notification = leader.Get<Coap::ApplicationCoap>().NewMessage(); |
| 206 | + VerifyOrQuit(notification != nullptr); |
| 207 | + SuccessOrQuit(notification->Init(Coap::kTypeNonConfirmable, Coap::kCodeContent)); |
| 208 | + SuccessOrQuit(notification->WriteToken(sSubscriberToken)); |
| 209 | + SuccessOrQuit(notification->AppendObserveOption(1)); |
| 210 | + SuccessOrQuit(notification->AppendPayloadMarker()); |
| 211 | + SuccessOrQuit(notification->AppendBytes("msg0", 4)); |
| 212 | + |
| 213 | + messageInfo.SetPeerAddr(AsCoreType(&sSubscriberAddr)); |
| 214 | + messageInfo.SetPeerPort(sSubscriberPort); |
| 215 | + |
| 216 | + SuccessOrQuit(leader.Get<Coap::ApplicationCoap>().SendMessageWithResponseHandlerSeparateParams( |
| 217 | + *notification, messageInfo, nullptr, nullptr, nullptr, nullptr, nullptr)); |
| 218 | + |
| 219 | + nexus.AdvanceTime(5 * 1000); |
| 220 | + |
| 221 | + VerifyOrQuit(sNotificationReceived); |
| 222 | + VerifyOrQuit(sObserveValue == 1); |
| 223 | + VerifyOrQuit(strcmp(sReceivedPayload, "msg0") == 0); |
| 224 | + |
| 225 | + // Router cancels subscription |
| 226 | + message = router.Get<Coap::ApplicationCoap>().NewMessage(); |
| 227 | + VerifyOrQuit(message != nullptr); |
| 228 | + SuccessOrQuit(message->Init(Coap::kTypeConfirmable, Coap::kCodeGet)); |
| 229 | + SuccessOrQuit(message->WriteToken(sSubscriberToken)); // Use same token |
| 230 | + SuccessOrQuit(message->AppendObserveOption(1)); // Observe=1 means cancel |
| 231 | + SuccessOrQuit(message->AppendUriPathOptions("test")); |
| 232 | + |
| 233 | + sRequestHandlerCalled = false; |
| 234 | + sSubscriptionActive = false; |
| 235 | + |
| 236 | + messageInfo.SetPeerAddr(leader.Get<Mle::Mle>().GetMeshLocalEid()); |
| 237 | + messageInfo.SetPeerPort(OT_DEFAULT_COAP_PORT); |
| 238 | + |
| 239 | + SuccessOrQuit(router.Get<Coap::ApplicationCoap>().SendMessageWithResponseHandlerSeparateParams( |
| 240 | + *message, messageInfo, nullptr, &HandleNotification, nullptr, nullptr, nullptr)); |
| 241 | + |
| 242 | + nexus.AdvanceTime(5 * 1000); |
| 243 | + |
| 244 | + VerifyOrQuit(sRequestHandlerCalled); |
| 245 | + VerifyOrQuit(!sSubscriberPresent); |
| 246 | + |
| 247 | + leader.Get<Coap::ApplicationCoap>().RemoveResource(resource); |
| 248 | + IgnoreError(leader.Get<Coap::ApplicationCoap>().Stop()); |
| 249 | + IgnoreError(router.Get<Coap::ApplicationCoap>().Stop()); |
| 250 | +} |
| 251 | + |
| 252 | +} // namespace Nexus |
| 253 | +} // namespace ot |
| 254 | + |
| 255 | +int main(void) |
| 256 | +{ |
| 257 | + ot::Nexus::TestCoapObserve(); |
| 258 | + printf("All tests passed\n"); |
| 259 | + return 0; |
| 260 | +} |
0 commit comments