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