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