1 /* 2 * Copyright (c) 2019, 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 /** 30 * @file 31 * This file includes definitions for ubus API. 32 */ 33 34 #ifndef OTBR_AGENT_OTUBUS_HPP_ 35 #define OTBR_AGENT_OTUBUS_HPP_ 36 37 #include "openthread-br/config.h" 38 39 #include <stdarg.h> 40 #include <time.h> 41 42 #include <openthread/ip6.h> 43 #include <openthread/link.h> 44 #include <openthread/netdiag.h> 45 #include <openthread/udp.h> 46 47 #include "common/code_utils.hpp" 48 #include "common/mainloop.hpp" 49 #include "ncp/rcp_host.hpp" 50 51 extern "C" { 52 #include <libubox/blobmsg_json.h> 53 #include <libubox/uloop.h> 54 #include <libubox/ustream.h> 55 #include <libubox/utils.h> 56 #include <libubus.h> 57 } 58 59 namespace otbr { 60 namespace Ncp { 61 class RcpHost; 62 } 63 64 namespace ubus { 65 66 /** 67 * @namespace otbr::ubus 68 * 69 * @brief 70 * This namespace contains definitions for ubus related instance. 71 */ 72 73 class UbusServer 74 { 75 public: 76 /** 77 * Constructor 78 * 79 * @param[in] aHost A pointer to OpenThread Controller structure. 80 * @param[in] aMutex A pointer to mutex. 81 */ 82 static void Initialize(Ncp::RcpHost *aHost, std::mutex *aMutex); 83 84 /** 85 * This method return the instance of the global UbusServer. 86 * 87 * @retval The reference of the UbusServer Instance. 88 */ 89 static UbusServer &GetInstance(void); 90 91 /** 92 * This method install ubus object onto OpenWRT. 93 */ 94 void InstallUbusObject(void); 95 96 /** 97 * This method handle ubus scan function request. 98 * 99 * @param[in] aContext A pointer to the ubus context. 100 * @param[in] aObj A pointer to the ubus object. 101 * @param[in] aRequest A pointer to the ubus request. 102 * @param[in] aMethod A pointer to the ubus method. 103 * @param[in] aMsg A pointer to the ubus message. 104 * 105 * @retval 0 Successfully handler the request. 106 */ 107 static int UbusScanHandler(struct ubus_context *aContext, 108 struct ubus_object *aObj, 109 struct ubus_request_data *aRequest, 110 const char *aMethod, 111 struct blob_attr *aMsg); 112 113 /** 114 * This method handle ubus get channel function request. 115 * 116 * @param[in] aContext A pointer to the ubus context. 117 * @param[in] aObj A pointer to the ubus object. 118 * @param[in] aRequest A pointer to the ubus request. 119 * @param[in] aMethod A pointer to the ubus method. 120 * @param[in] aMsg A pointer to the ubus message. 121 * 122 * @retval 0 Successfully handler the request. 123 */ 124 static int UbusChannelHandler(struct ubus_context *aContext, 125 struct ubus_object *aObj, 126 struct ubus_request_data *aRequest, 127 const char *aMethod, 128 struct blob_attr *aMsg); 129 130 /** 131 * This method handle ubus set channel function request. 132 * 133 * @param[in] aContext A pointer to the ubus context. 134 * @param[in] aObj A pointer to the ubus object. 135 * @param[in] aRequest A pointer to the ubus request. 136 * @param[in] aMethod A pointer to the ubus method. 137 * @param[in] aMsg A pointer to the ubus message. 138 * 139 * @retval 0 Successfully handler the request. 140 */ 141 static int UbusSetChannelHandler(struct ubus_context *aContext, 142 struct ubus_object *aObj, 143 struct ubus_request_data *aRequest, 144 const char *aMethod, 145 struct blob_attr *aMsg); 146 147 /** 148 * This method handle ubus get networkname function request. 149 * 150 * @param[in] aContext A pointer to the ubus context. 151 * @param[in] aObj A pointer to the ubus object. 152 * @param[in] aRequest A pointer to the ubus request. 153 * @param[in] aMethod A pointer to the ubus method. 154 * @param[in] aMsg A pointer to the ubus message. 155 * 156 * @retval 0 Successfully handler the request. 157 */ 158 static int UbusNetworknameHandler(struct ubus_context *aContext, 159 struct ubus_object *aObj, 160 struct ubus_request_data *aRequest, 161 const char *aMethod, 162 struct blob_attr *aMsg); 163 164 /** 165 * This method handle ubus set networkname function request. 166 * 167 * @param[in] aContext A pointer to the ubus context. 168 * @param[in] aObj A pointer to the ubus object. 169 * @param[in] aRequest A pointer to the ubus request. 170 * @param[in] aMethod A pointer to the ubus method. 171 * @param[in] aMsg A pointer to the ubus message. 172 * 173 * @retval 0 Successfully handler the request. 174 */ 175 static int UbusSetNetworknameHandler(struct ubus_context *aContext, 176 struct ubus_object *aObj, 177 struct ubus_request_data *aRequest, 178 const char *aMethod, 179 struct blob_attr *aMsg); 180 181 /** 182 * This method handle ubus get state function request. 183 * 184 * @param[in] aContext A pointer to the ubus context. 185 * @param[in] aObj A pointer to the ubus object. 186 * @param[in] aRequest A pointer to the ubus request. 187 * @param[in] aMethod A pointer to the ubus method. 188 * @param[in] aMsg A pointer to the ubus message. 189 * 190 * @retval 0 Successfully handler the request. 191 */ 192 static int UbusStateHandler(struct ubus_context *aContext, 193 struct ubus_object *aObj, 194 struct ubus_request_data *aRequest, 195 const char *aMethod, 196 struct blob_attr *aMsg); 197 198 /** 199 * This method handle ubus set state function request. 200 * 201 * @param[in] aContext A pointer to the ubus context. 202 * @param[in] aObj A pointer to the ubus object. 203 * @param[in] aRequest A pointer to the ubus request. 204 * @param[in] aMethod A pointer to the ubus method. 205 * @param[in] aMsg A pointer to the ubus message. 206 * 207 * @retval 0 Successfully handler the request. 208 */ 209 static int UbusMacfilterSetStateHandler(struct ubus_context *aContext, 210 struct ubus_object *aObj, 211 struct ubus_request_data *aRequest, 212 const char *aMethod, 213 struct blob_attr *aMsg); 214 215 /** 216 * This method handle ubus get panid function request. 217 * 218 * @param[in] aContext A pointer to the ubus context. 219 * @param[in] aObj A pointer to the ubus object. 220 * @param[in] aRequest A pointer to the ubus request. 221 * @param[in] aMethod A pointer to the ubus method. 222 * @param[in] aMsg A pointer to the ubus message. 223 * 224 * @retval 0 Successfully handler the request. 225 */ 226 static int UbusPanIdHandler(struct ubus_context *aContext, 227 struct ubus_object *aObj, 228 struct ubus_request_data *aRequest, 229 const char *aMethod, 230 struct blob_attr *aMsg); 231 232 /** 233 * This method handle ubus set panid function request. 234 * 235 * @param[in] aContext A pointer to the ubus context. 236 * @param[in] aObj A pointer to the ubus object. 237 * @param[in] aRequest A pointer to the ubus request. 238 * @param[in] aMethod A pointer to the ubus method. 239 * @param[in] aMsg A pointer to the ubus message. 240 * 241 * @retval 0 Successfully handler the request. 242 */ 243 static int UbusSetPanIdHandler(struct ubus_context *aContext, 244 struct ubus_object *aObj, 245 struct ubus_request_data *aRequest, 246 const char *aMethod, 247 struct blob_attr *aMsg); 248 249 /** 250 * This method handle ubus get pskc function request. 251 * 252 * @param[in] aContext A pointer to the ubus context. 253 * @param[in] aObj A pointer to the ubus object. 254 * @param[in] aRequest A pointer to the ubus request. 255 * @param[in] aMethod A pointer to the ubus method. 256 * @param[in] aMsg A pointer to the ubus message. 257 * 258 * @retval 0 Successfully handler the request. 259 */ 260 static int UbusPskcHandler(struct ubus_context *aContext, 261 struct ubus_object *aObj, 262 struct ubus_request_data *aRequest, 263 const char *aMethod, 264 struct blob_attr *aMsg); 265 266 /** 267 * This method handle ubus set pskc function request. 268 * 269 * @param[in] aContext A pointer to the ubus context. 270 * @param[in] aObj A pointer to the ubus object. 271 * @param[in] aRequest A pointer to the ubus request. 272 * @param[in] aMethod A pointer to the ubus method. 273 * @param[in] aMsg A pointer to the ubus message. 274 * 275 * @retval 0 Successfully handler the request. 276 */ 277 static int UbusSetPskcHandler(struct ubus_context *aContext, 278 struct ubus_object *aObj, 279 struct ubus_request_data *aRequest, 280 const char *aMethod, 281 struct blob_attr *aMsg); 282 283 /** 284 * This method handle ubus get networkkey function request. 285 * 286 * @param[in] aContext A pointer to the ubus context. 287 * @param[in] aObj A pointer to the ubus object. 288 * @param[in] aRequest A pointer to the ubus request. 289 * @param[in] aMethod A pointer to the ubus method. 290 * @param[in] aMsg A pointer to the ubus message. 291 * 292 * @retval 0 Successfully handler the request. 293 */ 294 static int UbusNetworkkeyHandler(struct ubus_context *aContext, 295 struct ubus_object *aObj, 296 struct ubus_request_data *aRequest, 297 const char *aMethod, 298 struct blob_attr *aMsg); 299 300 /** 301 * This method handle ubus set networkkey function request. 302 * 303 * @param[in] aContext A pointer to the ubus context. 304 * @param[in] aObj A pointer to the ubus object. 305 * @param[in] aRequest A pointer to the ubus request. 306 * @param[in] aMethod A pointer to the ubus method. 307 * @param[in] aMsg A pointer to the ubus message. 308 * 309 * @retval 0 Successfully handler the request. 310 */ 311 static int UbusSetNetworkkeyHandler(struct ubus_context *aContext, 312 struct ubus_object *aObj, 313 struct ubus_request_data *aRequest, 314 const char *aMethod, 315 struct blob_attr *aMsg); 316 317 /** 318 * This method handle ubus get rloc16 function request. 319 * 320 * @param[in] aContext A pointer to the ubus context. 321 * @param[in] aObj A pointer to the ubus object. 322 * @param[in] aRequest A pointer to the ubus request. 323 * @param[in] aMethod A pointer to the ubus method. 324 * @param[in] aMsg A pointer to the ubus message. 325 * 326 * @retval 0 Successfully handler the request. 327 */ 328 static int UbusRloc16Handler(struct ubus_context *aContext, 329 struct ubus_object *aObj, 330 struct ubus_request_data *aRequest, 331 const char *aMethod, 332 struct blob_attr *aMsg); 333 334 /** 335 * This method handle ubus get extpanid function request. 336 * 337 * @param[in] aContext A pointer to the ubus context. 338 * @param[in] aObj A pointer to the ubus object. 339 * @param[in] aRequest A pointer to the ubus request. 340 * @param[in] aMethod A pointer to the ubus method. 341 * @param[in] aMsg A pointer to the ubus message. 342 * 343 * @retval 0 Successfully handler the request. 344 */ 345 static int UbusExtPanIdHandler(struct ubus_context *aContext, 346 struct ubus_object *aObj, 347 struct ubus_request_data *aRequest, 348 const char *aMethod, 349 struct blob_attr *aMsg); 350 351 /** 352 * This method handle ubus set extpanid function request. 353 * 354 * @param[in] aContext A pointer to the ubus context. 355 * @param[in] aObj A pointer to the ubus object. 356 * @param[in] aRequest A pointer to the ubus request. 357 * @param[in] aMethod A pointer to the ubus method. 358 * @param[in] aMsg A pointer to the ubus message. 359 * 360 * @retval 0 Successfully handler the request. 361 */ 362 static int UbusSetExtPanIdHandler(struct ubus_context *aContext, 363 struct ubus_object *aObj, 364 struct ubus_request_data *aRequest, 365 const char *aMethod, 366 struct blob_attr *aMsg); 367 368 /** 369 * This method handle ubus get mode function request. 370 * 371 * @param[in] aContext A pointer to the ubus context. 372 * @param[in] aObj A pointer to the ubus object. 373 * @param[in] aRequest A pointer to the ubus request. 374 * @param[in] aMethod A pointer to the ubus method. 375 * @param[in] aMsg A pointer to the ubus message. 376 * 377 * @retval 0 Successfully handler the request. 378 */ 379 static int UbusModeHandler(struct ubus_context *aContext, 380 struct ubus_object *aObj, 381 struct ubus_request_data *aRequest, 382 const char *aMethod, 383 struct blob_attr *aMsg); 384 385 /** 386 * This method handle ubus set mode function request. 387 * 388 * @param[in] aContext A pointer to the ubus context. 389 * @param[in] aObj A pointer to the ubus object. 390 * @param[in] aRequest A pointer to the ubus request. 391 * @param[in] aMethod A pointer to the ubus method. 392 * @param[in] aMsg A pointer to the ubus message. 393 * 394 * @retval 0 Successfully handler the request. 395 */ 396 static int UbusSetModeHandler(struct ubus_context *aContext, 397 struct ubus_object *aObj, 398 struct ubus_request_data *aRequest, 399 const char *aMethod, 400 struct blob_attr *aMsg); 401 402 /** 403 * This method handle ubus get partitionid function request. 404 * 405 * @param[in] aContext A pointer to the ubus context. 406 * @param[in] aObj A pointer to the ubus object. 407 * @param[in] aRequest A pointer to the ubus request. 408 * @param[in] aMethod A pointer to the ubus method. 409 * @param[in] aMsg A pointer to the ubus message. 410 * 411 * @retval 0 Successfully handler the request. 412 */ 413 static int UbusPartitionIdHandler(struct ubus_context *aContext, 414 struct ubus_object *aObj, 415 struct ubus_request_data *aRequest, 416 const char *aMethod, 417 struct blob_attr *aMsg); 418 419 /** 420 * This method handle ubus get leaderdata function request. 421 * 422 * @param[in] aContext A pointer to the ubus context. 423 * @param[in] aObj A pointer to the ubus object. 424 * @param[in] aRequest A pointer to the ubus request. 425 * @param[in] aMethod A pointer to the ubus method. 426 * @param[in] aMsg A pointer to the ubus message. 427 * 428 * @retval 0 Successfully handler the request. 429 */ 430 static int UbusLeaderdataHandler(struct ubus_context *aContext, 431 struct ubus_object *aObj, 432 struct ubus_request_data *aRequest, 433 const char *aMethod, 434 struct blob_attr *aMsg); 435 436 /** 437 * This method handle ubus get networkdata function request. 438 * 439 * @param[in] aContext A pointer to the ubus context. 440 * @param[in] aObj A pointer to the ubus object. 441 * @param[in] aRequest A pointer to the ubus request. 442 * @param[in] aMethod A pointer to the ubus method. 443 * @param[in] aMsg A pointer to the ubus message. 444 * 445 * @retval 0 Successfully handler the request. 446 */ 447 static int UbusNetworkdataHandler(struct ubus_context *aContext, 448 struct ubus_object *aObj, 449 struct ubus_request_data *aRequest, 450 const char *aMethod, 451 struct blob_attr *aMsg); 452 453 /** 454 * This method handle ubus get parent function request. 455 * 456 * @param[in] aContext A pointer to the ubus context. 457 * @param[in] aObj A pointer to the ubus object. 458 * @param[in] aRequest A pointer to the ubus request. 459 * @param[in] aMethod A pointer to the ubus method. 460 * @param[in] aMsg A pointer to the ubus message. 461 * 462 * @retval 0 Successfully handler the request. 463 */ 464 static int UbusParentHandler(struct ubus_context *aContext, 465 struct ubus_object *aObj, 466 struct ubus_request_data *aRequest, 467 const char *aMethod, 468 struct blob_attr *aMsg); 469 470 /** 471 * This method handle ubus get neighbor function request. 472 * 473 * @param[in] aContext A pointer to the ubus context. 474 * @param[in] aObj A pointer to the ubus object. 475 * @param[in] aRequest A pointer to the ubus request. 476 * @param[in] aMethod A pointer to the ubus method. 477 * @param[in] aMsg A pointer to the ubus message. 478 * 479 * @retval 0 Successfully handler the request. 480 */ 481 static int UbusNeighborHandler(struct ubus_context *aContext, 482 struct ubus_object *aObj, 483 struct ubus_request_data *aRequest, 484 const char *aMethod, 485 struct blob_attr *aMsg); 486 487 /** 488 * This method handle ubus start thread function request. 489 * 490 * @param[in] aContext A pointer to the ubus context. 491 * @param[in] aObj A pointer to the ubus object. 492 * @param[in] aRequest A pointer to the ubus request. 493 * @param[in] aMethod A pointer to the ubus method. 494 * @param[in] aMsg A pointer to the ubus message. 495 * 496 * @retval 0 Successfully handler the request. 497 */ 498 static int UbusThreadStartHandler(struct ubus_context *aContext, 499 struct ubus_object *aObj, 500 struct ubus_request_data *aRequest, 501 const char *aMethod, 502 struct blob_attr *aMsg); 503 504 /** 505 * This method handle ubus stop thread function request. 506 * 507 * @param[in] aContext A pointer to the ubus context. 508 * @param[in] aObj A pointer to the ubus object. 509 * @param[in] aRequest A pointer to the ubus request. 510 * @param[in] aMethod A pointer to the ubus method. 511 * @param[in] aMsg A pointer to the ubus message. 512 * 513 * @retval 0 Successfully handler the request. 514 */ 515 static int UbusThreadStopHandler(struct ubus_context *aContext, 516 struct ubus_object *aObj, 517 struct ubus_request_data *aRequest, 518 const char *aMethod, 519 struct blob_attr *aMsg); 520 521 /** 522 * This method handle ubus leave function request. 523 * 524 * @param[in] aContext A pointer to the ubus context. 525 * @param[in] aObj A pointer to the ubus object. 526 * @param[in] aRequest A pointer to the ubus request. 527 * @param[in] aMethod A pointer to the ubus method. 528 * @param[in] aMsg A pointer to the ubus message. 529 * 530 * @retval 0 Successfully handler the request. 531 */ 532 static int UbusLeaveHandler(struct ubus_context *aContext, 533 struct ubus_object *aObj, 534 struct ubus_request_data *aRequest, 535 const char *aMethod, 536 struct blob_attr *aMsg); 537 538 /** 539 * This method handle ubus get macfilter address function request. 540 * 541 * @param[in] aContext A pointer to the ubus context. 542 * @param[in] aObj A pointer to the ubus object. 543 * @param[in] aRequest A pointer to the ubus request. 544 * @param[in] aMethod A pointer to the ubus method. 545 * @param[in] aMsg A pointer to the ubus message. 546 * 547 * @retval 0 Successfully handler the request. 548 */ 549 static int UbusMacfilterAddrHandler(struct ubus_context *aContext, 550 struct ubus_object *aObj, 551 struct ubus_request_data *aRequest, 552 const char *aMethod, 553 struct blob_attr *aMsg); 554 555 /** 556 * This method handle ubus get macfilter state function request. 557 * 558 * @param[in] aContext A pointer to the ubus context. 559 * @param[in] aObj A pointer to the ubus object. 560 * @param[in] aRequest A pointer to the ubus request. 561 * @param[in] aMethod A pointer to the ubus method. 562 * @param[in] aMsg A pointer to the ubus message. 563 * 564 * @retval 0 Successfully handler the request. 565 */ 566 static int UbusMacfilterStateHandler(struct ubus_context *aContext, 567 struct ubus_object *aObj, 568 struct ubus_request_data *aRequest, 569 const char *aMethod, 570 struct blob_attr *aMsg); 571 572 /** 573 * This method handle ubus macfilter address add function request. 574 * 575 * @param[in] aContext A pointer to the ubus context. 576 * @param[in] aObj A pointer to the ubus object. 577 * @param[in] aRequest A pointer to the ubus request. 578 * @param[in] aMethod A pointer to the ubus method. 579 * @param[in] aMsg A pointer to the ubus message. 580 * 581 * @retval 0 Successfully handler the request. 582 */ 583 static int UbusMacfilterAddHandler(struct ubus_context *aContext, 584 struct ubus_object *aObj, 585 struct ubus_request_data *aRequest, 586 const char *aMethod, 587 struct blob_attr *aMsg); 588 589 /** 590 * This method handle ubus macfilter address clear function request. 591 * 592 * @param[in] aContext A pointer to the ubus context. 593 * @param[in] aObj A pointer to the ubus object. 594 * @param[in] aRequest A pointer to the ubus request. 595 * @param[in] aMethod A pointer to the ubus method. 596 * @param[in] aMsg A pointer to the ubus message. 597 * 598 * @retval 0 Successfully handler the request. 599 */ 600 static int UbusMacfilterClearHandler(struct ubus_context *aContext, 601 struct ubus_object *aObj, 602 struct ubus_request_data *aRequest, 603 const char *aMethod, 604 struct blob_attr *aMsg); 605 606 /** 607 * This method handle ubus macfilter address remove function request. 608 * 609 * @param[in] aContext A pointer to the ubus context. 610 * @param[in] aObj A pointer to the ubus object. 611 * @param[in] aRequest A pointer to the ubus request. 612 * @param[in] aMethod A pointer to the ubus method. 613 * @param[in] aMsg A pointer to the ubus message. 614 * 615 * @retval 0 Successfully handler the request. 616 */ 617 static int UbusMacfilterRemoveHandler(struct ubus_context *aContext, 618 struct ubus_object *aObj, 619 struct ubus_request_data *aRequest, 620 const char *aMethod, 621 struct blob_attr *aMsg); 622 623 /** 624 * This method handle ubus start commissioner function request. 625 * 626 * @param[in] aContext A pointer to the ubus context. 627 * @param[in] aObj A pointer to the ubus object. 628 * @param[in] aRequest A pointer to the ubus request. 629 * @param[in] aMethod A pointer to the ubus method. 630 * @param[in] aMsg A pointer to the ubus message. 631 * 632 * @retval 0 Successfully handler the request. 633 */ 634 static int UbusCommissionerStartHandler(struct ubus_context *aContext, 635 struct ubus_object *aObj, 636 struct ubus_request_data *aRequest, 637 const char *aMethod, 638 struct blob_attr *aMsg); 639 640 /** 641 * This method handle ubus add joiner function request. 642 * 643 * @param[in] aContext A pointer to the ubus context. 644 * @param[in] aObj A pointer to the ubus object. 645 * @param[in] aRequest A pointer to the ubus request. 646 * @param[in] aMethod A pointer to the ubus method. 647 * @param[in] aMsg A pointer to the ubus message. 648 * 649 * @retval 0 Successfully handler the request. 650 */ 651 static int UbusJoinerAddHandler(struct ubus_context *aContext, 652 struct ubus_object *aObj, 653 struct ubus_request_data *aRequest, 654 const char *aMethod, 655 struct blob_attr *aMsg); 656 657 /** 658 * This method handle ubus remove joiner function request. 659 * 660 * @param[in] aContext A pointer to the ubus context. 661 * @param[in] aObj A pointer to the ubus object. 662 * @param[in] aRequest A pointer to the ubus request. 663 * @param[in] aMethod A pointer to the ubus method. 664 * @param[in] aMsg A pointer to the ubus message. 665 * 666 * @retval 0 Successfully handler the request. 667 */ 668 static int UbusJoinerRemoveHandler(struct ubus_context *aContext, 669 struct ubus_object *aObj, 670 struct ubus_request_data *aRequest, 671 const char *aMethod, 672 struct blob_attr *aMsg); 673 674 /** 675 * This method handle ubus get joiner information function request. 676 * 677 * @param[in] aContext A pointer to the ubus context. 678 * @param[in] aObj A pointer to the ubus object. 679 * @param[in] aRequest A pointer to the ubus request. 680 * @param[in] aMethod A pointer to the ubus method. 681 * @param[in] aMsg A pointer to the ubus message. 682 * 683 * @retval 0 Successfully handler the request. 684 */ 685 static int UbusJoinerNumHandler(struct ubus_context *aContext, 686 struct ubus_object *aObj, 687 struct ubus_request_data *aRequest, 688 const char *aMethod, 689 struct blob_attr *aMsg); 690 691 /** 692 * This method handle ubus mgmtset function request. 693 * 694 * @param[in] aContext A pointer to the ubus context. 695 * @param[in] aObj A pointer to the ubus object. 696 * @param[in] aRequest A pointer to the ubus request. 697 * @param[in] aMethod A pointer to the ubus method. 698 * @param[in] aMsg A pointer to the ubus message. 699 * 700 * @retval 0 Successfully handler the request. 701 */ 702 static int UbusMgmtsetHandler(struct ubus_context *aContext, 703 struct ubus_object *aObj, 704 struct ubus_request_data *aRequest, 705 const char *aMethod, 706 struct blob_attr *aMsg); 707 708 /** 709 * This method handle ubus interfaceName function request. 710 * 711 * @param[in] aContext A pointer to the ubus context. 712 * @param[in] aObj A pointer to the ubus object. 713 * @param[in] aRequest A pointer to the ubus request. 714 * @param[in] aMethod A pointer to the ubus method. 715 * @param[in] aMsg A pointer to the ubus message. 716 * 717 * @retval 0 Successfully handler the request. 718 */ 719 static int UbusInterfaceNameHandler(struct ubus_context *aContext, 720 struct ubus_object *aObj, 721 struct ubus_request_data *aRequest, 722 const char *aMethod, 723 struct blob_attr *aMsg); 724 725 /** 726 * This method handle initial diagnostic get response. 727 * 728 * @param[in] aError A error of receiving the diagnostic response. 729 * @param[in] aMessage A pointer to the message. 730 * @param[in] aMessageInfo A pointer to the message information. 731 * @param[in] aContext A pointer to the context. 732 */ 733 static void HandleDiagnosticGetResponse(otError aError, 734 otMessage *aMessage, 735 const otMessageInfo *aMessageInfo, 736 void *aContext); 737 738 /** 739 * This method handle diagnosticget response. 740 * 741 * @param[in] aError A error of receiving the diagnostic response. 742 * @param[in] aMessage A pointer to the message. 743 * @param[in] aMessageInfo A pointer to the message information. 744 */ 745 void HandleDiagnosticGetResponse(otError aError, otMessage *aMessage, const otMessageInfo *aMessageInfo); 746 747 private: 748 bool mIfFinishScan; 749 struct ubus_context *mContext; 750 const char *mSockPath; 751 struct blob_buf mBuf; 752 struct blob_buf mNetworkdataBuf; 753 Ncp::RcpHost *mHost; 754 std::mutex *mHostMutex; 755 time_t mSecond; 756 enum 757 { 758 kDefaultJoinerTimeout = 120, 759 }; 760 761 /** 762 * Constructor 763 * 764 * @param[in] aHost The pointer to OpenThread Controller structure. 765 * @param[in] aMutex A pointer to mutex. 766 */ 767 UbusServer(Ncp::RcpHost *aHost, std::mutex *aMutex); 768 769 /** 770 * This method start scan. 771 */ 772 void ProcessScan(void); 773 774 /** 775 * This method detailly start scan. 776 * 777 * @param[in] aContext A pointer to the ubus context. 778 * @param[in] aObj A pointer to the ubus object. 779 * @param[in] aRequest A pointer to the ubus request. 780 * @param[in] aMethod A pointer to the ubus method. 781 * @param[in] aMsg A pointer to the ubus message. 782 * 783 * @retval 0 Successfully handler the request. 784 */ 785 int UbusScanHandlerDetail(struct ubus_context *aContext, 786 struct ubus_object *aObj, 787 struct ubus_request_data *aRequest, 788 const char *aMethod, 789 struct blob_attr *aMsg); 790 791 /** 792 * This method handle scan result (callback function). 793 * 794 * @param[in] aResult A pointer to result. 795 * @param[in] aContext A pointer to context. 796 */ 797 static void HandleActiveScanResult(otActiveScanResult *aResult, void *aContext); 798 799 /** 800 * This method detailly handler the scan result, called by HandleActiveScanResult. 801 * 802 * @param[in] aResult A pointer to result. 803 */ 804 void HandleActiveScanResultDetail(otActiveScanResult *aResult); 805 806 /** 807 * This method detailly handler get neighbor information. 808 * 809 * @param[in] aContext A pointer to the ubus context. 810 * @param[in] aObj A pointer to the ubus object. 811 * @param[in] aRequest A pointer to the ubus request. 812 * @param[in] aMethod A pointer to the ubus method. 813 * @param[in] aMsg A pointer to the ubus message. 814 * 815 * @retval 0 Successfully handler the request. 816 */ 817 int UbusNeighborHandlerDetail(struct ubus_context *aContext, 818 struct ubus_object *aObj, 819 struct ubus_request_data *aRequest, 820 const char *aMethod, 821 struct blob_attr *aMsg); 822 823 /** 824 * This method detailly handler get parent information. 825 * 826 * @param[in] aContext A pointer to the ubus context. 827 * @param[in] aObj A pointer to the ubus object. 828 * @param[in] aRequest A pointer to the ubus request. 829 * @param[in] aMethod A pointer to the ubus method. 830 * @param[in] aMsg A pointer to the ubus message. 831 * 832 * @retval 0 Successfully handler the request. 833 */ 834 int UbusParentHandlerDetail(struct ubus_context *aContext, 835 struct ubus_object *aObj, 836 struct ubus_request_data *aRequest, 837 const char *aMethod, 838 struct blob_attr *aMsg); 839 840 /** 841 * This method handle mgmtset request. 842 * 843 * @param[in] aContext A pointer to the ubus context. 844 * @param[in] aObj A pointer to the ubus object. 845 * @param[in] aRequest A pointer to the ubus request. 846 * @param[in] aMethod A pointer to the ubus method. 847 * @param[in] aMsg A pointer to the ubus message. 848 * 849 * @retval 0 Successfully handler the request. 850 */ 851 int UbusMgmtset(struct ubus_context *aContext, 852 struct ubus_object *aObj, 853 struct ubus_request_data *aRequest, 854 const char *aMethod, 855 struct blob_attr *aMsg); 856 857 /** 858 * This method handle leave request. 859 * 860 * @param[in] aContext A pointer to the ubus context. 861 * @param[in] aObj A pointer to the ubus object. 862 * @param[in] aRequest A pointer to the ubus request. 863 * @param[in] aMethod A pointer to the ubus method. 864 * @param[in] aMsg A pointer to the ubus message. 865 * 866 * @retval 0 Successfully handler the request. 867 */ 868 int UbusLeaveHandlerDetail(struct ubus_context *aContext, 869 struct ubus_object *aObj, 870 struct ubus_request_data *aRequest, 871 const char *aMethod, 872 struct blob_attr *aMsg); 873 874 /** 875 * This method handle thread related request. 876 * 877 * @param[in] aContext A pointer to the ubus context. 878 * @param[in] aObj A pointer to the ubus object. 879 * @param[in] aRequest A pointer to the ubus request. 880 * @param[in] aMethod A pointer to the ubus method. 881 * @param[in] aMsg A pointer to the ubus message. 882 * @param[in] aAction A pointer to the action needed. 883 * 884 * @retval 0 Successfully handler the request. 885 */ 886 int UbusThreadHandler(struct ubus_context *aContext, 887 struct ubus_object *aObj, 888 struct ubus_request_data *aRequest, 889 const char *aMethod, 890 struct blob_attr *aMsg, 891 const char *aAction); 892 893 /** 894 * This method handle get information request. 895 * 896 * @param[in] aContext A pointer to the ubus context. 897 * @param[in] aObj A pointer to the ubus object. 898 * @param[in] aRequest A pointer to the ubus request. 899 * @param[in] aMethod A pointer to the ubus method. 900 * @param[in] aMsg A pointer to the ubus message. 901 * @param[in] aAction A pointer to the action needed. 902 * 903 * @retval 0 Successfully handler the request. 904 */ 905 int UbusGetInformation(struct ubus_context *aContext, 906 struct ubus_object *aObj, 907 struct ubus_request_data *aRequest, 908 const char *aMethod, 909 struct blob_attr *aMsg, 910 const char *action); 911 912 /** 913 * This method handle set information request. 914 * 915 * @param[in] aContext A pointer to the ubus context. 916 * @param[in] aObj A pointer to the ubus object. 917 * @param[in] aRequest A pointer to the ubus request. 918 * @param[in] aMethod A pointer to the ubus method. 919 * @param[in] aMsg A pointer to the ubus message. 920 * @param[in] aAction A pointer to the action needed. 921 * 922 * @retval 0 Successfully handler the request. 923 */ 924 int UbusSetInformation(struct ubus_context *aContext, 925 struct ubus_object *aObj, 926 struct ubus_request_data *aRequest, 927 const char *aMethod, 928 struct blob_attr *aMsg, 929 const char *aAction); 930 931 /** 932 * This method handle commissioner related request. 933 * 934 * @param[in] aContext A pointer to the ubus context. 935 * @param[in] aObj A pointer to the ubus object. 936 * @param[in] aRequest A pointer to the ubus request. 937 * @param[in] aMethod A pointer to the ubus method. 938 * @param[in] aMsg A pointer to the ubus message. 939 * @param[in] aAction A pointer to the action needed. 940 * 941 * @retval 0 Successfully handler the request. 942 */ 943 int UbusCommissioner(struct ubus_context *aContext, 944 struct ubus_object *aObj, 945 struct ubus_request_data *aRequest, 946 const char *aMethod, 947 struct blob_attr *aMsg, 948 const char *aAction); 949 950 /** 951 * This method handle conmmissione state change (callback function). 952 * 953 * @param[in] aState The state of commissioner. 954 * @param[in] aContext A pointer to the ubus context. 955 */ 956 static void HandleStateChanged(otCommissionerState aState, void *aContext); 957 958 /** 959 * This method handle conmmissione state change. 960 * 961 * @param[in] aState The state of commissioner. 962 */ 963 void HandleStateChanged(otCommissionerState aState); 964 965 /** 966 * This method handle joiner event (callback function). 967 * 968 * @param[in] aEvent The joiner event type. 969 * @param[in] aJoinerInfo A pointer to the Joiner Info. 970 * @param[in] aJoinerId A pointer to the Joiner ID (if not known, it will be NULL). 971 * @param[in] aContext A pointer to application-specific context. 972 */ 973 static void HandleJoinerEvent(otCommissionerJoinerEvent aEvent, 974 const otJoinerInfo *aJoinerInfo, 975 const otExtAddress *aJoinerId, 976 void *aContext); 977 978 /** 979 * This method handle joiner event. 980 * 981 * @param[in] aEvent The joiner event type. 982 * @param[in] aJoinerInfo A pointer to the Joiner Info. 983 * @param[in] aJoinerId A pointer to the Joiner ID (if not known, it will be NULL). 984 */ 985 void HandleJoinerEvent(otCommissionerJoinerEvent aEvent, 986 const otJoinerInfo *aJoinerInfo, 987 const otExtAddress *aJoinerId); 988 989 /** 990 * This method convert thread network state to string. 991 * 992 * @param[in] aInstance A pointer to the instance. 993 * @param[out] aState A pointer to the string address. 994 */ 995 void GetState(otInstance *aInstance, char *aState); 996 997 /** 998 * This method add fd of ubus object. 999 */ 1000 void UbusAddFd(void); 1001 1002 /** 1003 * This method set ubus reconnect time. 1004 * 1005 * @param[in] aTimeout A pointer to the timeout. 1006 */ 1007 static void UbusReconnTimer(struct uloop_timeout *aTimeout); 1008 1009 /** 1010 * This method detailly handle ubus reconnect time. 1011 * 1012 * @param[in] aTimeout A pointer to the timeout. 1013 */ 1014 void UbusReconnTimerDetail(struct uloop_timeout *aTimeout); 1015 1016 /** 1017 * This method handle ubus connection lost. 1018 * 1019 * @param[in] aContext A pointer to the context. 1020 */ 1021 static void UbusConnectionLost(struct ubus_context *aContext); 1022 1023 /** 1024 * This method connect and display ubus. 1025 * 1026 * @param[in] aPath A pointer to the ubus server path(default is nullptr). 1027 * 1028 * @retval 0 Successfully handler the request. 1029 */ 1030 int DisplayUbusInit(const char *aPath); 1031 1032 /** 1033 * This method disconnect and display ubus. 1034 */ 1035 void DisplayUbusDone(void); 1036 1037 /** 1038 * This method parses an ASCII string as a long. 1039 * 1040 * @param[in] aString A pointer to the ASCII string. 1041 * @param[out] aLong A reference to where the parsed long is placed. 1042 * 1043 * @retval OT_ERROR_NONE Successfully parsed the ASCII string. 1044 * @retval OT_ERROR_PARSE Could not parse the ASCII string. 1045 */ 1046 otError ParseLong(char *aString, long &aLong); 1047 1048 /** 1049 * This method converts a hex string to binary. 1050 * 1051 * @param[in] aHex A pointer to the hex string. 1052 * @param[out] aBin A pointer to where the binary representation is placed. 1053 * @param[in] aBinLength Maximum length of the binary representation. 1054 * 1055 * @returns The number of bytes in the binary representation. 1056 */ 1057 int Hex2Bin(const char *aHex, uint8_t *aBin, uint16_t aBinLength); 1058 1059 /** 1060 * This method output bytes into char*. 1061 * 1062 * @param[in] aBytes A pointer to the bytes need to be convert. 1063 * @param[in] aLength The length of the bytes. 1064 * @param[out] aOutput A pointer to the char* string. 1065 */ 1066 void OutputBytes(const uint8_t *aBytes, uint8_t aLength, char *aOutput); 1067 1068 /** 1069 * This method append result in message passed to ubus. 1070 * 1071 * @param[in] aError The error type of the message. 1072 * @param[in] aContext A pointer to the context. 1073 * @param[in] aRequest A pointer to the request. 1074 */ 1075 void AppendResult(otError aError, struct ubus_context *aContext, struct ubus_request_data *aRequest); 1076 }; 1077 1078 class UBusAgent : public MainloopProcessor 1079 { 1080 public: 1081 /** 1082 * The constructor to initialize the UBus agent. 1083 * 1084 * @param[in] aHost A reference to the Thread controller. 1085 */ UBusAgent(otbr::Ncp::RcpHost & aHost)1086 UBusAgent(otbr::Ncp::RcpHost &aHost) 1087 : mHost(aHost) 1088 , mThreadMutex() 1089 { 1090 } 1091 1092 /** 1093 * This method initializes the UBus agent. 1094 */ 1095 void Init(void); 1096 1097 void Update(MainloopContext &aMainloop) override; 1098 void Process(const MainloopContext &aMainloop) override; 1099 1100 private: UbusServerRun(void)1101 static void UbusServerRun(void) { otbr::ubus::UbusServer::GetInstance().InstallUbusObject(); } 1102 1103 otbr::Ncp::RcpHost &mHost; 1104 std::mutex mThreadMutex; 1105 }; 1106 } // namespace ubus 1107 } // namespace otbr 1108 1109 #endif // OTBR_AGENT_OTUBUS_HPP_ 1110