Welcome to mirror list, hosted at ThFree Co, Russian Federation.

zigbee_core_wb.c « src « core « zigbee « STM32_WPAN « ST « Middlewares - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b446186ecd1eb6394a40d2a7e8fc3106b9d83cd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
/**
 ******************************************************************************
 * @file    zigbee_core_wb.c
 * @author  MCD Application Team
 * @brief   Contains STM32WB specificities requested to control the zigbee stack
 *          from the M4 core.
 *
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2018-2021 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include <stdlib.h>
#include <limits.h> /* ULONG_MAX */
#include <assert.h>

#include "zigbee_types.h"
#include "zigbee_interface.h" /* zigbee.h, etc */
#include "zcl/zcl.h"
#include "zcl/key/zcl.key.h" /* ZbZclKeWithDevice */
#include "zcl/general/zcl.diagnostics.h"
#include "zcl/zcl.touchlink.h"
#include "extras/zigbee.extras.h"
#include "ieee802154_crc.h"

#ifdef ZIGBEE_DIRECT_ACTIVATED
/* SHA-256 */
#include "sha.h"
#endif /* ZIGBEE_DIRECT_ACTIVATED */

#include "zigbee_core.h"
#include "tl_zigbee_hci.h"

#include "dbg_trace.h"
#include "stm_logging.h"

/* Lock isn't required on this platform */
#define ZbEnterCritical(_zb_)
#define ZbExitCritical(_zb_)

#ifndef ZB_HEAP_MAX_ALLOC
#define ZB_HEAP_MAX_ALLOC                   2000U
#endif

/* Protyptes (move to header file? */
void zb_ipc_m4_stack_logging_config(bool enable);
unsigned int ZbHeapMaxAlloc(void);
bool zb_ipc_get_secured_mem_info(uint32_t *unsec_sram2a_sz, uint32_t *unsec_sram2b_sz);
unsigned int zb_malloc_current_sz(void);
bool ZbZclDeviceLogCheckAllow(struct ZigBeeT *zb, struct ZbApsdeDataIndT *dataIndPtr, struct ZbZclHeaderT *zclHdrPtr);

#ifdef ZIGBEE_DIRECT_ACTIVATED
extern void ZbCcmTransform(const void *key, const void *nonce, uint32_t nonce_len, void *m, uint32_t m_len, void *mic);
extern void ZbCcmAuthenticate(const void *key, const void *nonce, uint32_t nonce_len, const void *a, uint32_t a_len,
  const void *m, uint32_t m_len, void *mic, uint32_t mic_len);
int ec_mul_x25519(const void *private, const void *base, void *result);
int ec_mul_p256(const void *priv, const void *pub_x, const void *pub_y, void *result_x, void *result_y);
enum sha_status_t SHA256Reset(SHA256Context *context);
enum sha_status_t SHA256Input(SHA256Context *context, const uint8_t *bytes, unsigned int bytecount);
enum sha_status_t SHA256Result(SHA256Context * context, uint8_t Message_Digest[SHA256HashSize]);
int hmacReset(HMACContext *context, enum SHAversion whichSha, const unsigned char *key, int key_len);
int hmacInput(HMACContext *context, const unsigned char *text, int text_len);
int hmacResult(HMACContext * context, uint8_t digest[USHAMaxHashSize]);
#endif /* ZIGBEE_DIRECT_ACTIVATED */
/* Touchlink callbacks */
static struct ZbTouchlinkCallbacks zigbee_m4_tl_callbacks;

void * zb_heap_alloc(struct ZigBeeT *zb, size_t sz, const char *funcname, unsigned int linenum);
void zb_heap_free(struct ZigBeeT *zb, void *ptr, const char *funcname, unsigned int linenum);
#if defined(CONFIG_ZB_MEMORY_DEBUG) || defined(CONFIG_ZB_MEMPOOL_HISTORY)
# define ZbHeapAlloc(_zb_, _sz_)             zb_heap_alloc(_zb_, _sz_, __func__, __LINE__)
# define ZbHeapFree(_zb_, _ptr_)             zb_heap_free(_zb_, _ptr_, __func__, __LINE__)
#else
# define ZbHeapAlloc(_zb_, _sz_)             zb_heap_alloc(_zb_, _sz_, "", 0)
# define ZbHeapFree(_zb_, _ptr_)             zb_heap_free(_zb_, _ptr_, "", 0)
#endif

/* API Wrapper Helpers -------------------------------------------------------*/
#define IPC_REQ_FUNC(name, cmd_id, req_type) \
    void \
    name(struct ZigBeeT *zb, req_type *r) \
    { \
        Zigbee_Cmd_Request_t *ipcc_req; \
    \
        Pre_ZigbeeCmdProcessing(); \
        ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer(); \
        ipcc_req->ID = cmd_id; \
        ipcc_req->Size = 1; \
        ipcc_req->Data[0] = (uint32_t)r; \
        ZIGBEE_CmdTransfer(); \
    }

#define IPC_REQ_CONF_FUNC(name, cmd_id, req_type, conf_type) \
    void \
    name(struct ZigBeeT *zb, req_type *r, conf_type *c) \
    { \
        Zigbee_Cmd_Request_t *ipcc_req; \
    \
        Pre_ZigbeeCmdProcessing(); \
        ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer(); \
        ipcc_req->ID = cmd_id; \
        ipcc_req->Size = 2; \
        ipcc_req->Data[0] = (uint32_t)r; \
        ipcc_req->Data[1] = (uint32_t)c; \
        ZIGBEE_CmdTransfer(); \
    }

#define IPC_REQ_CALLBACK_FUNC(name, cmd_id, req_type, conf_type) \
    enum ZbStatusCodeT \
    name(struct ZigBeeT *zb, req_type *r, void (*callback)(conf_type *conf, void *cbarg), void *cbarg) \
    { \
        Zigbee_Cmd_Request_t *ipcc_req; \
        struct zb_ipc_m4_cb_info_t *info; \
    \
        info = zb_ipc_m4_cb_info_alloc((void *)callback, cbarg); \
        if (info == NULL) { \
            return ZB_STATUS_ALLOC_FAIL; \
        } \
    \
        Pre_ZigbeeCmdProcessing(); \
        ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer(); \
        ipcc_req->ID = cmd_id; \
        ipcc_req->Size = 2; \
        ipcc_req->Data[0] = (uint32_t)r; \
        ipcc_req->Data[1] = (uint32_t)info; \
        ZIGBEE_CmdTransfer(); \
        return (enum ZbStatusCodeT)zb_ipc_m4_get_retval(); \
    }

#define IPC_CLUSTER_REQ_CALLBACK_FUNC(name, cmd_id, req_type, rsp_type) \
    enum ZclStatusCodeT \
    name(struct ZbZclClusterT *clusterPtr, req_type *r, \
    void (*callback)(const rsp_type *rsp, void *cbarg), void *cbarg) \
    { \
        Zigbee_Cmd_Request_t *ipcc_req; \
        struct zb_ipc_m4_cb_info_t *info; \
    \
        info = zb_ipc_m4_cb_info_alloc((void *)callback, cbarg); \
        if (info == NULL) { \
            return ZCL_STATUS_INSUFFICIENT_SPACE; \
        } \
    \
        Pre_ZigbeeCmdProcessing(); \
        ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer(); \
        ipcc_req->ID = cmd_id; \
        ipcc_req->Size = 3; \
        ipcc_req->Data[0] = (uint32_t)clusterPtr; \
        ipcc_req->Data[1] = (uint32_t)r; \
        ipcc_req->Data[2] = (uint32_t)info; \
        ZIGBEE_CmdTransfer(); \
        return (enum ZclStatusCodeT)zb_ipc_m4_get_retval(); \
    }

/* ZbUptimeT is an "unsigned long" */
#define ZB_UPTIME_MAX                       (ULONG_MAX)

/* If a value is greater than the high mark, and a second value is lower
 * than the low mark, then the second value is deemed to have rolled-over
 * when comparing the two values. */
#define TIMER_ROLL_OVER_HIGH                ((ZB_UPTIME_MAX / 2UL) + (ZB_UPTIME_MAX / 4UL))
#define TIMER_ROLL_OVER_LOW                 ((ZB_UPTIME_MAX / 2UL) - (ZB_UPTIME_MAX / 4UL))

/* Null (all zeroes)
 * 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00 */
const uint8_t sec_key_null[ZB_SEC_KEYSIZE] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

/* "ZigBeeAlliance09"
 * 5a:69:67:42:65:65:41:6c:6c:69:61:6e:63:65:30:39 */
const uint8_t sec_key_ha[ZB_SEC_KEYSIZE] = {
    0x5a, 0x69, 0x67, 0x42, 0x65, 0x65, 0x41, 0x6c, 0x6c, 0x69, 0x61, 0x6e, 0x63, 0x65, 0x30, 0x39
};

/* Uncertified Device's Distributed Link Key
 * d0:d1:d2:d3:d4:d5:d6:d7:d8:d9:da:db:dc:dd:de:df */
const uint8_t sec_key_distrib_uncert[ZB_SEC_KEYSIZE] = {
    0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf
};

/* TOUCHLINK_KEY_INDEX_CERTIFICATION key
 * c0:c1:c2:c3:c4:c5:c6:c7:c8:c9:ca:cb:cc:cd:ce:cf */
const uint8_t sec_key_touchlink_cert[ZB_SEC_KEYSIZE] = {
    0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
};

struct aps_filter_cb_t {
    struct ZbApsFilterT *filter;
    int (*callback)(struct ZbApsdeDataIndT *data_ind, void *cb_arg);
    void *cb_arg;
};

/* Helper struct to send to APS Binding */
static const struct ZbApsAddrT sendToBinding = {
    ZB_APSDE_ADDRMODE_NOTPRESENT,
    0xff,
    0xffff,
    0xffff,
    0,
};
const struct ZbApsAddrT *ZbApsAddrBinding = &sendToBinding;

struct zb_ipc_m4_cb_info_t {
    void *callback;
    void *arg;
    bool zcl_recv_multi_rsp;
};

static const va_list va_null;

/* Single static callback for persistent data notifications */
static void (*zb_persist_cb)(struct ZigBeeT *zb, void *cbarg) = NULL;
static void *zb_persist_arg = NULL;

/* Callback for ZbZdoMatchDescMulti. It is static and never freed. It can be overwritten
 * by another call to ZbZdoMatchDescMulti. */
static void (*zdo_match_multi_cb)(struct ZbZdoMatchDescRspT *ind, void *cb_arg) = NULL;
static void *zdo_match_multi_arg;

/* Some globals */
PACKED_STRUCT zb_ipc_globals_t {
    struct ZigBeeT *zb;
    /* Stack logging callback */
    void (*log_cb)(struct ZigBeeT *zb, uint32_t mask, const char *hdr,
        const char *fmt, va_list argptr);
    uint32_t log_mask;
    bool log_enable;
    unsigned zb_alloc_sz; /* ZbMalloc (MSG_M0TOM4_ZB_MALLOC) */
};
static struct zb_ipc_globals_t zb_ipc_globals;

int zcl_cluster_data_ind(struct ZbApsdeDataIndT *dataIndPtr, void *arg);
int zcl_cluster_alarm_data_ind(struct ZbApsdeDataIndT *data_ind, void *arg);

/* ST: Don't use built-in memcpy. "Unfortunately when full size optimization is enabled on
 * M4 side, IAR maps memcpy to aeaby_memcpy4 instead of aeabi_memcpy which allows
 * unaligned memcpy." */
static void
zb_ipc_m4_memcpy2(void *dst, void *src, unsigned int len)
{
    unsigned int i;

    for (i = 0; i < len; i++) {
        ((uint8_t *)dst)[i] = ((uint8_t *)src)[i];
    }
}

#if 0 /* not used */
struct ZigBeeT *
GetZigbeePtr(void)
{
    return zb_ipc_globals.zb;
}
#endif

void
zb_ipc_m4_stack_logging_config(bool enable)
{
    uint32_t mask = 0U;
    void(*func)(struct ZigBeeT *zb, uint32_t mask, const char *hdr, const char *fmt, va_list argptr) = NULL;

    zb_ipc_globals.log_enable = enable;

    /* Now reconfigure the logging on the M0. If log_func == NULL, then we disable the M0
     * sending log messages over the IPCC. Otherwise, it's enabled. We first need to get
     * the current log mask in-use, before re-asserting it back to the M0. */
    ZbGetLogging(zb_ipc_globals.zb, &mask, &func);
    ZbSetLogging(zb_ipc_globals.zb, mask, func);
}

static struct zb_ipc_m4_cb_info_t *
zb_ipc_m4_cb_info_alloc(void *callback, void *arg)
{
    struct zb_ipc_m4_cb_info_t *info;

    info = malloc(sizeof(struct zb_ipc_m4_cb_info_t));
    if (info != NULL) {
        memset(info, 0, sizeof(struct zb_ipc_m4_cb_info_t));
        info->callback = callback;
        info->arg = arg;
    }
    return info;
}

static void
zb_ipc_m4_cb_info_free(struct zb_ipc_m4_cb_info_t *info)
{
    free(info);
}

static uint32_t
zb_ipc_m4_get_retval(void)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    uint32_t retval;

    ipcc_req = ZIGBEE_Get_OTCmdRspPayloadBuffer();
    assert(ipcc_req->Size == 1);
    zb_ipc_m4_memcpy2(&retval, (void*)&ipcc_req->Data[0], 4);
    return retval;
}

unsigned int
ZbHeapMaxAlloc(void)
{
    return ZB_HEAP_MAX_ALLOC;
}

struct ZigBeeT *
ZbInit(uint64_t extAddr, struct ZbInitTblSizesT *tblSizes, struct ZbInitSetLoggingT *setLogging)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    if (zb_ipc_globals.zb != NULL) {
        return NULL;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZB_INIT;
    ipcc_req->Size = 4;
    zb_ipc_m4_memcpy2((void*)&ipcc_req->Data[0], &extAddr, 8);
    ipcc_req->Data[2] = (uint32_t)tblSizes;
    ipcc_req->Data[3] = (uint32_t)setLogging;
    ZIGBEE_CmdTransfer();
    zb_ipc_globals.zb = (struct ZigBeeT *)zb_ipc_m4_get_retval();
    if (setLogging != NULL) {
        /* Save the log mask */
        zb_ipc_globals.log_cb = setLogging->func;
        zb_ipc_globals.log_mask = setLogging->mask;
    }
    return zb_ipc_globals.zb;
}

void
ZbDestroy(struct ZigBeeT *zb)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    if (zb_ipc_globals.zb == NULL) {
        return;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZB_DESTROY;
    ipcc_req->Size = 0;
    ZIGBEE_CmdTransfer();
    zb_ipc_globals.zb = NULL;
}

enum ZbStatusCodeT
ZbDestroyWithCb(struct ZigBeeT *zb, void (*callback)(void *arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zb_ipc_m4_cb_info_t *info;

    if (zb_ipc_globals.zb == NULL) {
        return ZB_NWK_STATUS_INVALID_REQUEST;
    }
    info = zb_ipc_m4_cb_info_alloc((void *)callback, arg);
    if (info == NULL) {
        return ZB_STATUS_ALLOC_FAIL;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZB_DESTROY_WITH_CB;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)info;
    ZIGBEE_CmdTransfer();
    /* Followed up in MSG_M0TOM4_ZB_DESTROY_CB */
    return ZB_STATUS_SUCCESS;
}


void
ZbSetLogging(struct ZigBeeT *zb, uint32_t mask,
    void (*func)(struct ZigBeeT *zb, uint32_t mask, const char *hdr, const char *fmt, va_list argptr))
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_LOG_CONFIG;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = mask;
    /* Ignore the 'func' argument. Only use zb_ipc_globals.log_enable to determine if we want
     * log messages from the M4. If zb_ipc_globals.log_func != NULL, M0 sends log messages
     * over IPCC. */
    ipcc_req->Data[1] = (zb_ipc_globals.log_enable || (func != NULL)) ? 1U : 0U;
    ZIGBEE_CmdTransfer();
    /* Save the log mask */
    zb_ipc_globals.log_cb = func;
    zb_ipc_globals.log_mask = mask;
}

void
ZbGetLogging(struct ZigBeeT *zb, uint32_t *mask,
    void(**func)(struct ZigBeeT *zb, uint32_t mask, const char *hdr, const char *fmt, va_list argptr))
{
    if (mask != NULL) {
        *mask = zb_ipc_globals.log_mask;
    }
    if (func != NULL) {
        *func = zb_ipc_globals.log_cb;
    }
}

uint64_t
ZbExtendedAddress(struct ZigBeeT *zb)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    uint64_t ext_addr = 0U;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_EXTADDR_GET;
    ipcc_req->Size = 0;
    ZIGBEE_CmdTransfer();
    ipcc_req = ZIGBEE_Get_OTCmdRspPayloadBuffer();
    assert(ipcc_req->Size == 2);
    zb_ipc_m4_memcpy2(&ext_addr, &ipcc_req->Data, 8);
    return ext_addr;
}

uint16_t
ZbShortAddress(struct ZigBeeT *zb)
{
    uint16_t nwkAddr = 0U;

    (void)ZbNwkGet(zb, ZB_NWK_NIB_ID_NetworkAddress, &nwkAddr, sizeof(nwkAddr));
    return nwkAddr;
}

void
ZbChangeExtAddr(struct ZigBeeT *zb, uint64_t extAddr)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_EXTADDR_CHANGE;
    ipcc_req->Size = 2;
    zb_ipc_m4_memcpy2((void*)&ipcc_req->Data[0], &extAddr, 8);
    ZIGBEE_CmdTransfer();
}

void
ZbStartupConfigGetProDefaults(struct ZbStartupT *configPtr)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_STARTUP_GET_CFG;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)configPtr;
    ZIGBEE_CmdTransfer();
}

void
ZbStartupConfigGetProSeDefaults(struct ZbStartupT *configPtr)
{
    ZbStartupConfigGetProDefaults(configPtr);
    /* Remove the default preconfigured link key by clearing it. */
    (void)memset(configPtr->security.preconfiguredLinkKey, 0, ZB_SEC_KEYSIZE);
    /* Remove the distributed link key by clearing it. */
    (void)memset(configPtr->security.distributedGlobalKey, 0, ZB_SEC_KEYSIZE);
}

enum ZbStatusCodeT
ZbStartup(struct ZigBeeT *zb, struct ZbStartupT *configPtr,
    void (*callback)(enum ZbStatusCodeT status, void *cb_arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zb_ipc_m4_cb_info_t *info;

    info = zb_ipc_m4_cb_info_alloc((void *)callback, arg);
    if (info == NULL) {
        return ZB_STATUS_ALLOC_FAIL;
    }

    /* Save the touchlink callbacks */
    /* I.e. MSG_M0TOM4_ZCL_TL_EP_INFO_CB */
    memcpy(&zigbee_m4_tl_callbacks, &configPtr->touchlink.callbacks, sizeof(struct ZbTouchlinkCallbacks));

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_STARTUP_REQ;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)configPtr;
    ipcc_req->Data[1] = (uint32_t)info;
    ZIGBEE_CmdTransfer();
    return (enum ZbStatusCodeT)zb_ipc_m4_get_retval();
    /* Followed up in MSG_M0TOM4_STARTUP_CB handler */
}

enum ZbStatusCodeT
ZbStartupRejoin(struct ZigBeeT *zb, void (*callback)(struct ZbNlmeJoinConfT *conf, void *arg), void *cbarg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zb_ipc_m4_cb_info_t *info;
    enum ZbStatusCodeT status;

    info = zb_ipc_m4_cb_info_alloc((void *)callback, cbarg);
    if (info == NULL) {
        return ZB_STATUS_ALLOC_FAIL;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_STARTUP_REJOIN;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)info;
    ZIGBEE_CmdTransfer();
    status = (enum ZbStatusCodeT)zb_ipc_m4_get_retval();
    if (status != ZB_STATUS_SUCCESS) {
        zb_ipc_m4_cb_info_free(info);
    }
    return status;
    /* If ZB_STATUS_SUCECSS, followed up in MSG_M0TOM4_STARTUP_REJOIN_CB handler */
}

enum ZbStatusCodeT
ZbStartupPersist(struct ZigBeeT *zb, const void *pdata, unsigned int plen,
    struct ZbStartupCbkeT *cbke_config,
    void (*callback)(enum ZbStatusCodeT status, void *arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zb_ipc_m4_cb_info_t *info;
    enum ZbStatusCodeT status;

    info = zb_ipc_m4_cb_info_alloc((void *)callback, arg);
    if (info == NULL) {
        return ZB_STATUS_ALLOC_FAIL;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_STARTUP_PERSIST;
    ipcc_req->Size = 4;
    ipcc_req->Data[0] = (uint32_t)pdata;
    ipcc_req->Data[1] = (uint32_t)plen;
    ipcc_req->Data[2] = (uint32_t)cbke_config;
    ipcc_req->Data[3] = (uint32_t)info;
    ZIGBEE_CmdTransfer();
    status = (enum ZbStatusCodeT)zb_ipc_m4_get_retval();
    if (status != ZB_STATUS_SUCCESS) {
        zb_ipc_m4_cb_info_free(info);
    }
    return status;
    /* If ZB_STATUS_SUCECSS, followed up in MSG_M0TOM4_STARTUP_PERSIST_CB handler */
}

enum ZbStatusCodeT
ZbStartupFindBindStart(struct ZigBeeT *zb, void (*callback)(enum ZbStatusCodeT status, void *arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zb_ipc_m4_cb_info_t *info;
    enum ZbStatusCodeT status;

    info = zb_ipc_m4_cb_info_alloc((void *)callback, arg);
    if (info == NULL) {
        return ZB_STATUS_ALLOC_FAIL;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_STARTUP_FINDBIND;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)info;
    ZIGBEE_CmdTransfer();
    status = (enum ZbStatusCodeT)zb_ipc_m4_get_retval();
    if (status != ZB_STATUS_SUCCESS) {
        zb_ipc_m4_cb_info_free(info);
    }
    return status;
    /* Followed up in MSG_M0TOM4_STARTUP_FINDBIND_CB handler */
}

enum ZbStatusCodeT
ZbStartupTouchlinkTargetStop(struct ZigBeeT *zb)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_STARTUP_TOUCHLINK_TARGET_STOP;
    ipcc_req->Size = 0;
    ZIGBEE_CmdTransfer();
    return (enum ZbStatusCodeT)zb_ipc_m4_get_retval();
}

bool
ZbStartupTcsoStart(struct ZigBeeT *zb, void (*callback)(enum ZbTcsoStatusT status, void *arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zb_ipc_m4_cb_info_t *info;
    bool retval;

    info = zb_ipc_m4_cb_info_alloc((void *)callback, arg);
    if (info == NULL) {
        return false;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_STARTUP_TCSO_START;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)info;
    ZIGBEE_CmdTransfer();
    retval = zb_ipc_m4_get_retval() != 0U ? true : false;
    if (!retval) {
        zb_ipc_m4_cb_info_free(info);
    }
    return retval;
    /* Followed up in MSG_M0TOM4_STARTUP_TCSO_CB */
}

bool
ZbStartupTcsoAbort(struct ZigBeeT *zb)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_STARTUP_TCSO_ABORT;
    ipcc_req->Size = 0;
    ZIGBEE_CmdTransfer();
    return zb_ipc_m4_get_retval() != 0U ? true : false;
}

enum ZbStatusCodeT
ZbTrustCenterRejoin(struct ZigBeeT *zb, void (*callback)(enum ZbStatusCodeT status, void *arg), void *cbarg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zb_ipc_m4_cb_info_t *info;

    info = zb_ipc_m4_cb_info_alloc((void *)callback, cbarg);
    if (info == NULL) {
        return ZB_STATUS_ALLOC_FAIL;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_STARTUP_TC_REJOIN;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)info;
    ZIGBEE_CmdTransfer();
    return (enum ZbStatusCodeT) zb_ipc_m4_get_retval();
    /* Followed up in MSG_M0TOM4_STARTUP_TC_REJOIN_CB handler */
}

bool
ZbPersistNotifyRegister(struct ZigBeeT *zb, void (*callback)(struct ZigBeeT *zb, void *cbarg), void *cbarg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    bool result;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_PERSIST_ENABLE;
    ipcc_req->Size = 0;
    ZIGBEE_CmdTransfer();
    result = zb_ipc_m4_get_retval() != 0U ? true : false;
    if (result) {
        zb_persist_arg = cbarg;
        zb_persist_cb = callback;
    }
    else {
        zb_persist_arg = NULL;
        zb_persist_cb = NULL;
    }
    return result;
    /* Followed up by M0 calls to MSG_M0TOM4_PERSIST_CB */
}

unsigned int
ZbPersistGet(struct ZigBeeT *zb, uint8_t *buf, unsigned int maxlen)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_PERSIST_GET;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)buf;
    ipcc_req->Data[1] = (uint32_t)maxlen;
    ZIGBEE_CmdTransfer();
    return zb_ipc_m4_get_retval();
}

unsigned int
ZbStateGet(struct ZigBeeT *zb, uint8_t *buf, unsigned int maxlen)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_STATE_GET;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)buf;
    ipcc_req->Data[1] = (uint32_t)maxlen;
    ZIGBEE_CmdTransfer();
    return zb_ipc_m4_get_retval();
}

enum ZbStatusCodeT
ZbLeaveReq(struct ZigBeeT *zb, void (*callback)(struct ZbNlmeLeaveConfT *conf, void *arg), void *cbarg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zb_ipc_m4_cb_info_t *info;

    info = zb_ipc_m4_cb_info_alloc((void *)callback, cbarg);
    if (info == NULL) {
        return ZB_STATUS_ALLOC_FAIL;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZB_LEAVE_REQ;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)info;
    ZIGBEE_CmdTransfer();
    return (enum ZbStatusCodeT)zb_ipc_m4_get_retval();
    /* Followed up in MSG_M0TOM4_ZB_LEAVE_CB handler */
}

void
ZbReset(struct ZigBeeT *zb)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZB_RESET_REQ;
    ipcc_req->Size = 0;
    ZIGBEE_CmdTransfer();
}

/******************************************************************************
 * Shutdown, Pause, Resume
 ******************************************************************************
 */

void
ZbShutdown(struct ZigBeeT *zb)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZB_SHUTDOWN;
    ipcc_req->Size = 0;
    ZIGBEE_CmdTransfer();
}

enum ZbStatusCodeT
ZbStatePause(struct ZigBeeT *zb, void (*callback)(void *arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zb_ipc_m4_cb_info_t *info;

    info = zb_ipc_m4_cb_info_alloc((void *)callback, arg);
    if (info == NULL) {
        return ZB_STATUS_ALLOC_FAIL;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZB_STATE_PAUSE;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)info;
    ZIGBEE_CmdTransfer();
    return (enum ZbStatusCodeT)zb_ipc_m4_get_retval();
    /* Followed up in MSG_M0TOM4_ZB_STATE_PAUSE_CB handler */
}

enum ZbStatusCodeT
ZbStateResume(struct ZigBeeT *zb)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZB_STATE_RESUME;
    ipcc_req->Size = 0;
    ZIGBEE_CmdTransfer();
    return (enum ZbStatusCodeT)zb_ipc_m4_get_retval();
}

/******************************************************************************
 * ECDSA Signature
 ******************************************************************************
 */
enum ZbStatusCodeT
ZbSecEcdsaValidate(struct ZigBeeT *zb, enum ZbSecEcdsaSigType sig_type,
    const uint8_t *ca_pub_key_array, unsigned int ca_pub_key_len,
    const uint8_t *certificate, const uint8_t *signature,
    const uint8_t *image_digest, const uint8_t *cert_digest)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZB_SEC_ECDSA_VALIDATE;
    ipcc_req->Size = 7;
    ipcc_req->Data[0] = (uint32_t)sig_type;
    ipcc_req->Data[1] = (uint32_t)ca_pub_key_array;
    ipcc_req->Data[2] = (uint32_t)ca_pub_key_len;
    ipcc_req->Data[3] = (uint32_t)certificate;
    ipcc_req->Data[4] = (uint32_t)signature;
    ipcc_req->Data[5] = (uint32_t)image_digest;
    ipcc_req->Data[6] = (uint32_t)cert_digest;
    ZIGBEE_CmdTransfer();
    return (enum ZbStatusCodeT)zb_ipc_m4_get_retval();
}

/******************************************************************************
 * Touchlink Utility Commands
 ******************************************************************************
 */
enum ZclStatusCodeT
ZbZclTouchlinkInitiatorGetGrpIdReq(struct ZigBeeT *zb, struct ZbTlGetGroupIdsReqCmd *cmd,
    const struct ZbApsAddrT *dst, void (*callback)(struct ZbTlGetGroupIdsRspCmd *rsp, void *arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zb_ipc_m4_cb_info_t *info;

    info = zb_ipc_m4_cb_info_alloc((void *)callback, arg);
    if (info == NULL) {
        return ZCL_STATUS_FAILURE;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_TL_GET_GRP_REQ;
    ipcc_req->Size = 3;
    ipcc_req->Data[0] = (uint32_t)cmd;
    ipcc_req->Data[1] = (uint32_t)dst;
    ipcc_req->Data[2] = (uint32_t)info;
    ZIGBEE_CmdTransfer();
    return (enum ZclStatusCodeT)zb_ipc_m4_get_retval();
    /* Followed up in MSG_M0TOM4_ZCL_TL_GET_GRP_CB handler */
}

enum ZclStatusCodeT
ZbZclTouchlinkInitiatorGetEpListReq(struct ZigBeeT *zb, struct ZbTlGetEpListReqCmd *cmd,
    const struct ZbApsAddrT *dst, void (*callback)(struct ZbTlGetEpListRspCmd *rsp, void *arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zb_ipc_m4_cb_info_t *info;

    info = zb_ipc_m4_cb_info_alloc((void *)callback, arg);
    if (info == NULL) {
        return ZCL_STATUS_FAILURE;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_TL_GET_EPLIST_REQ;
    ipcc_req->Size = 3;
    ipcc_req->Data[0] = (uint32_t)cmd;
    ipcc_req->Data[1] = (uint32_t)dst;
    ipcc_req->Data[2] = (uint32_t)info;
    ZIGBEE_CmdTransfer();
    return (enum ZclStatusCodeT)zb_ipc_m4_get_retval();
    /* Followed up in MSG_M0TOM4_ZCL_TL_GET_EPLIST_CB handler */
}

enum ZclStatusCodeT
ZbZclTouchlinkTargetSendEpInfoCmd(struct ZigBeeT *zb, uint8_t endpoint,
    const struct ZbApsAddrT *dst, void (*callback)(struct ZbZclCommandRspT *zcl_rsp, void *arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zb_ipc_m4_cb_info_t *info;

    info = zb_ipc_m4_cb_info_alloc((void *)callback, arg);
    if (info == NULL) {
        return ZCL_STATUS_FAILURE;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_TL_SEND_EPINFO_REQ;
    ipcc_req->Size = 3;
    ipcc_req->Data[0] = (uint32_t)endpoint;
    ipcc_req->Data[1] = (uint32_t)dst;
    ipcc_req->Data[2] = (uint32_t)info;
    ZIGBEE_CmdTransfer();
    return (enum ZclStatusCodeT)zb_ipc_m4_get_retval();
    /* Followed up in MSG_M0TOM4_ZCL_TL_SEND_EPINFO_CB handler */
}

/******************************************************************************
 * BDB
 ******************************************************************************
 */
enum ZbStatusCodeT
ZbBdbGetIndex(struct ZigBeeT *zb, enum ZbBdbAttrIdT attrId, void *attrPtr,
    unsigned int attrSz, unsigned int attrIndex)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct ZbBdbGetReqT bdbGetReq;
    struct ZbBdbGetConfT bdbGetConf;

    /* Do the BDB-GET.request */
    bdbGetReq.attrId = attrId;
    bdbGetReq.attr = attrPtr;
    bdbGetReq.attrLength = attrSz;
    bdbGetReq.attrIndex = attrIndex;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_BDB_GET_REQ;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)&bdbGetReq;
    ipcc_req->Data[1] = (uint32_t)&bdbGetConf;
    ZIGBEE_CmdTransfer();
    return bdbGetConf.status;
}

enum ZbStatusCodeT
ZbBdbSetIndex(struct ZigBeeT *zb, enum ZbBdbAttrIdT attrId, const void *attrPtr,
    unsigned int attrSz, unsigned int attrIndex)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct ZbBdbSetReqT bdbSetReq;
    struct ZbBdbSetConfT bdbSetConf;

    /* Do the BDB-SET.request */
    bdbSetReq.attrId = attrId;
    bdbSetReq.attr = attrPtr;
    bdbSetReq.attrLength = attrSz;
    bdbSetReq.attrIndex = attrIndex;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_BDB_SET_REQ;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)&bdbSetReq;
    ipcc_req->Data[1] = (uint32_t)&bdbSetConf;
    ZIGBEE_CmdTransfer();
    return bdbSetConf.status;
}

enum ZbBdbCommissioningStatusT
ZbBdbGetEndpointStatus(struct ZigBeeT *zb, uint8_t endpoint)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    uint32_t retval;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_BDB_GET_EP_STATUS;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)endpoint;
    ZIGBEE_CmdTransfer();
    /* Get the status code */
    retval = zb_ipc_m4_get_retval();
    return (enum ZbBdbCommissioningStatusT)retval;
}

/* Required for the Identify Server Cluster */
void
ZbBdbSetEndpointStatus(struct ZigBeeT *zb, enum ZbBdbCommissioningStatusT status, uint8_t endpoint)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_BDB_SET_EP_STATUS;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)status;
    ipcc_req->Data[1] = (uint32_t)endpoint;
    ZIGBEE_CmdTransfer();
}

/******************************************************************************
 * APS
 ******************************************************************************
 */
enum ZbStatusCodeT
ZbApsdeDataReqCallback(struct ZigBeeT *zb, struct ZbApsdeDataReqT *req,
    void (*callback)(struct ZbApsdeDataConfT *conf, void *arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    uint32_t retval;
    struct zb_ipc_m4_cb_info_t *info;

    info = zb_ipc_m4_cb_info_alloc((void *)callback, arg);
    if (info == NULL) {
        return ZB_STATUS_ALLOC_FAIL;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_APSDE_DATA_REQ;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)req;
    ipcc_req->Data[1] = (uint32_t)info;
    ZIGBEE_CmdTransfer();
    /* Get the status code */
    retval = zb_ipc_m4_get_retval();
    if (retval != 0x00) {
        zb_ipc_m4_cb_info_free(info);
    }
    return (enum ZbStatusCodeT)retval;
    /* If success, followed up in MSG_M0TOM4_APSDE_DATA_REQ_CB handler */
}

void
ZbApsmeAddEndpoint(struct ZigBeeT *zb, struct ZbApsmeAddEndpointReqT *r, struct ZbApsmeAddEndpointConfT *c)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_APS_ENDPOINT_ADD;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)r;
    ipcc_req->Data[1] = (uint32_t)c;
    ZIGBEE_CmdTransfer();
}

IPC_REQ_CONF_FUNC(ZbApsmeRemoveEndpoint, MSG_M4TOM0_APS_ENDPOINT_DEL,
    struct ZbApsmeRemoveEndpointReqT, struct ZbApsmeRemoveEndpointConfT)

struct ZbApsFilterT *
ZbApsFilterEndpointAdd(struct ZigBeeT *zb, uint8_t endpoint, uint16_t profileId,
    int (*callback)(struct ZbApsdeDataIndT *dataInd, void *cb_arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct aps_filter_cb_t *aps_filter_cb;

    /* This will be freed by ZbApsFilterEndpointFree */
    aps_filter_cb = malloc(sizeof(struct aps_filter_cb_t));
    if (aps_filter_cb == NULL) {
        return NULL;
    }
    aps_filter_cb->callback = callback;
    aps_filter_cb->cb_arg = arg;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_APS_FILTER_ENDPOINT_ADD;
    ipcc_req->Size = 3;
    ipcc_req->Data[0] = (uint32_t)endpoint;
    ipcc_req->Data[1] = (uint32_t)profileId;
    ipcc_req->Data[2] = (uint32_t)aps_filter_cb;
    ZIGBEE_CmdTransfer();
    aps_filter_cb->filter = (struct ZbApsFilterT *)zb_ipc_m4_get_retval();
    if (aps_filter_cb->filter == NULL) {
        free(aps_filter_cb);
        return NULL;
    }

    /* Callbacks go to MSG_M0TOM4_APS_FILTER_ENDPOINT_CB handler */

    /* Since "struct ZbApsFilterT *" is opaque to the application, we can return
     *  apointer to aps_filter_cb, which we can use to free later in
     * ZbApsFilterEndpointFree. */
    return (struct ZbApsFilterT *)aps_filter_cb;
}

struct ZbApsFilterT *
ZbApsFilterClusterAdd(struct ZigBeeT *zb, uint8_t endpoint, uint16_t clusterId, uint16_t profileId,
    int (*callback)(struct ZbApsdeDataIndT *dataInd, void *cb_arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct aps_filter_cb_t *aps_filter_cb;

    /* This will be freed by ZbApsFilterEndpointFree */
    aps_filter_cb = malloc(sizeof(struct aps_filter_cb_t));
    if (aps_filter_cb == NULL) {
        return NULL;
    }
    aps_filter_cb->callback = callback;
    aps_filter_cb->cb_arg = arg;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_APS_FILTER_CLUSTER_ADD;
    ipcc_req->Size = 4;
    ipcc_req->Data[0] = (uint32_t)endpoint;
    ipcc_req->Data[1] = (uint32_t)clusterId;
    ipcc_req->Data[2] = (uint32_t)profileId;
    ipcc_req->Data[3] = (uint32_t)aps_filter_cb;
    ZIGBEE_CmdTransfer();
    aps_filter_cb->filter = (struct ZbApsFilterT *)zb_ipc_m4_get_retval();
    if (aps_filter_cb->filter == NULL) {
        free(aps_filter_cb);
        return NULL;
    }
    /* Callbacks go to MSG_M0TOM4_APS_FILTER_CLUSTER_CB handler */

    /* Since "struct ZbApsFilterT *" is opaque to the application, we can return
     *  apointer to aps_filter_cb, which we can use to free later in
     * ZbApsFilterEndpointFree. */
    return (struct ZbApsFilterT *)aps_filter_cb;
}

void
ZbApsFilterEndpointFree(struct ZigBeeT *zb, struct ZbApsFilterT *filter)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct aps_filter_cb_t *aps_filter_cb;

    /* filter is actually a pointer to a "struct aps_filter_cb_t" */
    aps_filter_cb = (struct aps_filter_cb_t *)filter;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_APS_FILTER_REMOVE;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)aps_filter_cb->filter;
    ZIGBEE_CmdTransfer();

    memset(aps_filter_cb, 0, sizeof(struct aps_filter_cb_t)); /* for debugging */
    free(aps_filter_cb);
}

bool
ZbApsmeEndpointClusterListAppend(struct ZigBeeT *zb, uint8_t endpoint, uint16_t cluster_id, bool is_input)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_APS_EP_CLUSTER_ID_ADD;
    ipcc_req->Size = 3;
    ipcc_req->Data[0] = (uint32_t)endpoint;
    ipcc_req->Data[1] = (uint32_t)cluster_id;
    ipcc_req->Data[2] = (uint32_t)is_input;
    ZIGBEE_CmdTransfer();
    return zb_ipc_m4_get_retval() != 0U ? true : false;
}

bool
ZbApsEndpointExists(struct ZigBeeT *zb, uint8_t endpoint)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_APS_EP_EXISTS;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)endpoint;
    ZIGBEE_CmdTransfer();
    return zb_ipc_m4_get_retval() != 0U ? true : false;
}

uint16_t
ZbApsEndpointProfile(struct ZigBeeT *zb, uint8_t endpoint)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_APS_EP_GET_PROFILE;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)endpoint;
    ZIGBEE_CmdTransfer();
    return (uint16_t)zb_ipc_m4_get_retval();
}

bool
ZbApsAddrIsBcast(const struct ZbApsAddrT *addr)
{
    /* Check the destination of the original request */
    if (addr->mode == ZB_APSDE_ADDRMODE_GROUP) {
        return true;
    }
    if ((addr->mode == ZB_APSDE_ADDRMODE_SHORT) && (ZbNwkAddrIsBcast(addr->nwkAddr))) {
        return true;
    }
    return false;
}

bool
ZbApsAddrIsLocal(struct ZigBeeT *zb, const struct ZbApsAddrT *addr)
{
    if (addr->mode == ZB_APSDE_ADDRMODE_EXT) {
        if (addr->extAddr == ZbExtendedAddress(zb)) {
            return true;
        }
    }
    if (addr->mode == ZB_APSDE_ADDRMODE_SHORT) {
        if (addr->nwkAddr == ZbShortAddress(zb)) {
            /* Note, if our address is ZB_NWK_ADDR_UNDEFINED, then this message could only
             * have been generated locally. */
            return true;
        }
    }
    return false;
}

enum ZbStatusCodeT
ZbApsGetIndex(struct ZigBeeT *zb, enum ZbApsmeIbAttrIdT attrId, void *attrPtr, unsigned int attrSz, unsigned int attrIndex)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct ZbApsmeGetReqT apsmeGetReq;
    struct ZbApsmeGetConfT apsmeGetConf;

    /* Do the APSME-GET.request */
    apsmeGetReq.attrId = attrId;
    apsmeGetReq.attr = attrPtr;
    apsmeGetReq.attrLength = attrSz;
    apsmeGetReq.attrIndex = attrIndex;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_APS_GET_REQ;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)&apsmeGetReq;
    ipcc_req->Data[1] = (uint32_t)&apsmeGetConf;
    ZIGBEE_CmdTransfer();
    return apsmeGetConf.status;
}

enum ZbStatusCodeT
ZbApsSetIndex(struct ZigBeeT *zb, enum ZbApsmeIbAttrIdT attrId, const void *attrPtr, unsigned int attrSz, unsigned int attrIndex)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct ZbApsmeSetReqT apsmeSetReq;
    struct ZbApsmeSetConfT apsmeSetConf;

    /* Do the APSME-SET.request */
    apsmeSetReq.attrId = attrId;
    apsmeSetReq.attr = attrPtr;
    apsmeSetReq.attrLength = attrSz;
    apsmeSetReq.attrIndex = attrIndex;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_APS_SET_REQ;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)&apsmeSetReq;
    ipcc_req->Data[1] = (uint32_t)&apsmeSetConf;
    ZIGBEE_CmdTransfer();
    return apsmeSetConf.status;
}

IPC_REQ_CONF_FUNC(ZbApsmeAddGroupReq, MSG_M4TOM0_APSME_ADD_GROUP, struct ZbApsmeAddGroupReqT, struct ZbApsmeAddGroupConfT)
IPC_REQ_CONF_FUNC(ZbApsmeRemoveGroupReq, MSG_M4TOM0_APSME_REMOVE_GROUP, struct ZbApsmeRemoveGroupReqT, struct ZbApsmeRemoveGroupConfT)
IPC_REQ_CONF_FUNC(ZbApsmeRemoveAllGroupsReq, MSG_M4TOM0_APSME_REMOVE_ALL_GROUPS, struct ZbApsmeRemoveAllGroupsReqT, struct ZbApsmeRemoveAllGroupsConfT)

bool
ZbApsGroupIsMember(struct ZigBeeT *zb, uint16_t groupAddr, uint8_t endpoint)
{
    struct ZbApsmeGroupT group;
    unsigned int i;
    enum ZbStatusCodeT status;

    ZbEnterCritical(zb);
    for (i = 0;; i++) {
        status = ZbApsGetIndex(zb, ZB_APS_IB_ID_GROUP_TABLE, &group, sizeof(group), i);
        if (status != ZB_APS_STATUS_SUCCESS) {
            break;
        }
        /* Check for valid entries. */
        if (group.endpoint == ZB_ENDPOINT_BCAST) {
            continue;
        }
        /* Check if we're a member of this group. */
        if (group.groupAddr != groupAddr) {
            continue;
        }
        if ((endpoint != ZB_ENDPOINT_BCAST) && (group.endpoint != endpoint)) {
            continue;
        }
        /* We are a member. */
        ZbExitCritical(zb);
        return true;
    }
    ZbExitCritical(zb);
    return false;
}

/* Helper to iterate through the Groups table using the APSME-GET interface */
uint8_t
ZbApsGroupsGetCapacity(struct ZigBeeT *zb)
{
    struct ZbApsmeGroupT group;
    unsigned int i, num_found = 0;
    enum ZbStatusCodeT status;

    ZbEnterCritical(zb);
    for (i = 0;; i++) {
        status = ZbApsGetIndex(zb, ZB_APS_IB_ID_GROUP_TABLE, &group, sizeof(group), i);
        if (status != ZB_APS_STATUS_SUCCESS) {
            break;
        }
        if (group.endpoint == ZB_ENDPOINT_BCAST) {
            continue;
        }
        num_found++;
    }
    ZbExitCritical(zb);
    return (uint8_t)(i - num_found);
}

/* Helper to iterate through the Groups table using the APSME-GET interface */
uint8_t
ZbApsGroupsGetMembership(struct ZigBeeT *zb, uint8_t endpoint, uint16_t *group_list, uint8_t max_len)
{
    struct ZbApsmeGroupT group;
    unsigned int i, len = 0;
    enum ZbStatusCodeT status;

    ZbEnterCritical(zb);
    for (i = 0;; i++) {
        status = ZbApsGetIndex(zb, ZB_APS_IB_ID_GROUP_TABLE, &group, sizeof(group), i);
        if (status != ZB_APS_STATUS_SUCCESS) {
            break;
        }
        if (group.endpoint == ZB_ENDPOINT_BCAST) {
            continue;
        }
        if (group.endpoint != endpoint) {
            continue;
        }
        if (len >= max_len) {
            /* ZCL Groups: "If the total number of groups will cause
             * the maximum payload length of a ZigBee frame to be
             * exceeded, then the Group list field shall contain only
             * as many groups as will fit." */
            break;
        }
        group_list[len++] = group.groupAddr;
    }
    ZbExitCritical(zb);
    return len;
}

IPC_REQ_CONF_FUNC(ZbApsmeBindReq, MSG_M4TOM0_APSME_BIND, struct ZbApsmeBindReqT, struct ZbApsmeBindConfT)
IPC_REQ_CONF_FUNC(ZbApsmeUnbindReq, MSG_M4TOM0_APSME_UNBIND, struct ZbApsmeUnbindReqT, struct ZbApsmeUnbindConfT)

void
ZbApsUnbindAllReq(struct ZigBeeT *zb)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_APS_UNBIND_ALL;
    ipcc_req->Size = 0;
    ZIGBEE_CmdTransfer();
}

bool
ZbApsBindSrcExists(struct ZigBeeT *zb, uint8_t endpoint, uint16_t clusterId)
{
    struct ZbApsmeBindT entry;
    enum ZbStatusCodeT status;
    unsigned int i;
    uint64_t local_ext = ZbExtendedAddress(zb);

    ZbEnterCritical(zb);
    for (i = 0; ; i++) {
        status = ZbApsGetIndex(zb, ZB_APS_IB_ID_BINDING_TABLE, &entry, sizeof(entry), i);
        if (status != ZB_APS_STATUS_SUCCESS) {
            break;
        }
        if (entry.srcExtAddr == 0ULL) {
            continue;
        }
        if (entry.srcExtAddr != local_ext) {
            continue;
        }
        if (entry.srcEndpt != endpoint) {
            continue;
        }
        if (entry.clusterId != clusterId) {
            continue;
        }
        ZbExitCritical(zb);
        return true;
    }
    ZbExitCritical(zb);
    return false;
}

IPC_REQ_FUNC(ZbApsmeTransportKeyReq, MSG_M4TOM0_APSME_TRANSPORT_KEY, struct ZbApsmeTransportKeyReqT);
IPC_REQ_FUNC(ZbApsmeRemoveDeviceReq, MSG_M4TOM0_APSME_REMOVE_DEVICE, struct ZbApsmeRemoveDeviceReqT);

enum ZbStatusCodeT
ZbApsmeRequestKeyReq(struct ZigBeeT *zb, struct ZbApsmeRequestKeyReqT *req)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_APSME_REQUEST_KEY;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)req;
    ZIGBEE_CmdTransfer();
    return (enum ZbStatusCodeT)zb_ipc_m4_get_retval();
}

IPC_REQ_FUNC(ZbApsmeSwitchKeyReq, MSG_M4TOM0_APSME_SWITCH_KEY, struct ZbApsmeSwitchKeyReqT);
IPC_REQ_CONF_FUNC(ZbApsmeAddKeyReq, MSG_M4TOM0_APSME_ADD_KEY, struct ZbApsmeAddKeyReqT, struct ZbApsmeAddKeyConfT);
IPC_REQ_CONF_FUNC(ZbApsmeGetKeyReq, MSG_M4TOM0_APSME_GET_KEY, struct ZbApsmeGetKeyReqT, struct ZbApsmeGetKeyConfT);
IPC_REQ_CONF_FUNC(ZbApsmeRemoveKeyReq, MSG_M4TOM0_APSME_REMOVE_KEY, struct ZbApsmeRemoveKeyReqT, struct ZbApsmeRemoveKeyConfT);

/******************************************************************************
* Zigbee Message Filters
******************************************************************************
*/
PACKED_STRUCT zb_msg_filter_cb_info_t {
    struct ZbMsgFilterT *filter;
    enum zb_msg_filter_rc (*callback)(struct ZigBeeT *zb, uint32_t id, void *msg, void *cbarg);
    void *arg;
};

#define ZB_IPC_MSG_FILTER_CB_LIST_MAX               4
static struct zb_msg_filter_cb_info_t zb_msg_filter_cb_list[ZB_IPC_MSG_FILTER_CB_LIST_MAX];

struct ZbMsgFilterT *
ZbMsgFilterRegister(struct ZigBeeT *zb, uint32_t mask, uint8_t prio,
    enum zb_msg_filter_rc (*callback)(struct ZigBeeT *zb, uint32_t id, void *msg, void *cbarg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct ZbMsgFilterT *filter;
    struct zb_msg_filter_cb_info_t *cb_info;
    unsigned int i;

    for (i = 0; i < ZB_IPC_MSG_FILTER_CB_LIST_MAX; i++) {
        cb_info = &zb_msg_filter_cb_list[i];
        if (cb_info->filter == NULL) {
            break;
        }
    }
    if (i == ZB_IPC_MSG_FILTER_CB_LIST_MAX) {
        return NULL;
    }

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_FILTER_ADD;
    ipcc_req->Size = 3;
    ipcc_req->Data[0] = (uint32_t)mask;
    ipcc_req->Data[1] = (uint32_t)prio;
    ipcc_req->Data[2] = (uint32_t)cb_info;
    ZIGBEE_CmdTransfer();
    filter = (struct ZbMsgFilterT *)zb_ipc_m4_get_retval();
    if (filter != NULL) {
        cb_info->filter = filter;
        cb_info->callback = callback;
        cb_info->arg = arg;
    }
    return filter;
    /* Followed up by MSG_M0TOM4_FILTER_MSG_CB */
}

void
ZbMsgFilterRemove(struct ZigBeeT *zb, struct ZbMsgFilterT *filter)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zb_msg_filter_cb_info_t *cb_info;
    unsigned int i;

    if (filter == NULL) {
        return;
    }
    for (i = 0; i < ZB_IPC_MSG_FILTER_CB_LIST_MAX; i++) {
        cb_info = &zb_msg_filter_cb_list[i];
        if (cb_info->filter == filter) {
            break;
        }
    }
    if (i == ZB_IPC_MSG_FILTER_CB_LIST_MAX) {
        return;
    }

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_FILTER_DEL;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)filter;
    ZIGBEE_CmdTransfer();
    cb_info->filter = NULL;
}

#ifndef CONFIG_ZB_M4_ZBTIMER_OVERRIDE
/******************************************************************************
 * Zigbee Timer API
 ******************************************************************************
 */

/* M4 version of 'struct ZbTimerT'. It allows us to wrap up callback and arg into
 * a single arg pointer when communicating with the M0. */
struct ZbTimerT {
    void (*callback)(struct ZigBeeT *zb, void *data);
    void *arg;
    void *m0_timer; /* Handle */
};

struct ZbTimerT *
ZbTimerAlloc(struct ZigBeeT *zb, void (*callback)(struct ZigBeeT *zb, void *cn_arg), void *arg)
{
    struct ZbTimerT *timer;

    timer = ZbHeapAlloc(NULL, sizeof(struct ZbTimerT));
    if (timer != NULL) {
        Zigbee_Cmd_Request_t *ipcc_req;

        /* Configure the callback struct */
        timer->callback = callback;
        timer->arg = arg;

        /* Pass this to the M0 (stack) */
        Pre_ZigbeeCmdProcessing();
        ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
        ipcc_req->ID = MSG_M4TOM0_TIMER_ALLOC;
        ipcc_req->Size = 1;
        ipcc_req->Data[0] = (uint32_t)timer;
        ZIGBEE_CmdTransfer();
        timer->m0_timer = (void *)zb_ipc_m4_get_retval();
        if (timer->m0_timer == NULL) {
            ZbHeapFree(NULL, timer);
            timer = NULL;
        }
    }
    return timer;
}

void
ZbTimerChangeCallback(struct ZbTimerT *timer, void (*callback)(struct ZigBeeT *zb, void *cb_arg), void *arg)
{
    ZbTimerStop(timer);
    timer->callback = callback;
    timer->arg = arg;
}

void
ZbTimerFree(struct ZbTimerT *timer)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    /* Stop and free the timer on the M0 */
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_TIMER_FREE;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)timer->m0_timer;
    ZIGBEE_CmdTransfer();

    /* Free the timer struct on the M4 */
    ZbHeapFree(NULL, timer);
}

bool
ZbTimerRunning(struct ZbTimerT *timer)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    uint32_t retval;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_TIMER_RUNNING;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)timer->m0_timer;
    ZIGBEE_CmdTransfer();
    retval = zb_ipc_m4_get_retval();
    return retval != 0 ? true : false;
}

unsigned int
ZbTimerRemaining(struct ZbTimerT *timer)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_TIMER_REMAINING;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)timer->m0_timer;
    ZIGBEE_CmdTransfer();
    return (unsigned int)zb_ipc_m4_get_retval();
}

void
ZbTimerStop(struct ZbTimerT *timer)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_TIMER_STOP;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)timer->m0_timer;
    ZIGBEE_CmdTransfer();
}

void
ZbTimerReset(struct ZbTimerT *timer, unsigned int timeout)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_TIMER_RESET;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)timer->m0_timer;
    ipcc_req->Data[1] = (uint32_t)timeout;
    ZIGBEE_CmdTransfer();
}

unsigned int
ZbTimeoutRemaining(ZbUptimeT now, ZbUptimeT expire_time)
{
    ZbUptimeT u_delta;

    /* Check for 'timeout' rollover condition */
    if ((now >= TIMER_ROLL_OVER_HIGH) && (expire_time <= TIMER_ROLL_OVER_LOW)) {
        /* Timeout has rolled over, we haven't expired.
         * Compute timeout remaining */
        u_delta = (ZB_UPTIME_MAX - now) + expire_time + 1U;

        return (unsigned int)u_delta;
    }

    /* Check for 'now' rollover condition */
    if ((expire_time >= TIMER_ROLL_OVER_HIGH) && (now <= TIMER_ROLL_OVER_LOW)) {
        /* 'now' has rolled over, so now is > timeout, meaning we expired. */
        return 0;
    }

    /* No rollover, check if timer has expired */
    if (now >= expire_time) {
        /* Timer has expired */
        return 0;
    }

    /* Compute time remaining */
    u_delta = expire_time - now;

    return (unsigned int)u_delta;
}
#endif

/******************************************************************************
 * NWK
 ******************************************************************************
 */
enum ZbStatusCodeT
ZbNwkGetIndex(struct ZigBeeT *zb, enum ZbNwkNibAttrIdT attrId, void *attrPtr,
    unsigned int attrSz, unsigned int attrIndex)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct ZbNlmeGetReqT nlmeGetReq;
    struct ZbNlmeGetConfT nlmeGetConf;

    /* Form the NLME-GET.request */
    nlmeGetReq.attrId = attrId;
    nlmeGetReq.attr = attrPtr;
    nlmeGetReq.attrLength = attrSz;
    nlmeGetReq.attrIndex = attrIndex;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_NWK_GET_INDEX;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)&nlmeGetReq;
    ipcc_req->Data[1] = (uint32_t)&nlmeGetConf;
    ZIGBEE_CmdTransfer();
    return nlmeGetConf.status;
}

enum ZbStatusCodeT
ZbNwkSetIndex(struct ZigBeeT *zb, enum ZbNwkNibAttrIdT attrId, void *attrPtr,
    unsigned int attrSz, unsigned int attrIndex)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct ZbNlmeSetReqT nlmeSetReq;
    struct ZbNlmeSetConfT nlmeSetConf;

    /* Form the NLME-SET.request */
    nlmeSetReq.attrId = attrId;
    nlmeSetReq.attr = attrPtr;
    nlmeSetReq.attrLength = attrSz;
    nlmeSetReq.attrIndex = attrIndex;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_NWK_SET_INDEX;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)&nlmeSetReq;
    ipcc_req->Data[1] = (uint32_t)&nlmeSetConf;
    ZIGBEE_CmdTransfer();
    return nlmeSetConf.status;
}

enum ZbStatusCodeT
ZbNwkGet(struct ZigBeeT *zb, enum ZbNwkNibAttrIdT attrId, void *attrPtr, unsigned int attrSz)
{
    return ZbNwkGetIndex(zb, attrId, attrPtr, attrSz, 0);
}

enum ZbStatusCodeT
ZbNwkSet(struct ZigBeeT *zb, enum ZbNwkNibAttrIdT attrId, void *attrPtr, unsigned int attrSz)
{
    return ZbNwkSetIndex(zb, attrId, attrPtr, attrSz, 0);
}

uint64_t
ZbNwkAddrLookupExt(struct ZigBeeT *zb, uint16_t nwkAddr)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    uint64_t ext_addr;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_NWK_ADDR_LOOKUP_EXT;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)nwkAddr;
    ZIGBEE_CmdTransfer();
    /* Parse return value */
    ipcc_req = ZIGBEE_Get_OTCmdRspPayloadBuffer();
    assert(ipcc_req->Size == 2);
    zb_ipc_m4_memcpy2(&ext_addr, (void*)&ipcc_req->Data[0], 8);
    return ext_addr;
}

uint16_t
ZbNwkAddrLookupNwk(struct ZigBeeT *zb, uint64_t extAddr)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_NWK_ADDR_LOOKUP_NWK;
    /* Extended address is split across two args */
    ipcc_req->Size = 2;
    zb_ipc_m4_memcpy2((void*)ipcc_req->Data, &extAddr, 8);
    ZIGBEE_CmdTransfer();
    return (uint16_t)zb_ipc_m4_get_retval();
}

bool
ZbNwkGetSecMaterial(struct ZigBeeT *zb, uint8_t keySeqno, struct ZbNwkSecMaterialT *material)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_NWK_GET_SEC_MATERIAL;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)keySeqno;
    ipcc_req->Data[1] = (uint32_t)material;
    ZIGBEE_CmdTransfer();
    return zb_ipc_m4_get_retval() != 0U ? true : false;
}

/* Helper function to get the currently active NWK Key. */
bool
ZbNwkGetActiveKey(struct ZigBeeT *zb, struct ZbNwkSecMaterialT *active_key)
{
    uint8_t seqno;
    struct ZbNwkSecMaterialT material;

    if (ZbNwkGet(zb, ZB_NWK_NIB_ID_ActiveKeySeqNumber, &seqno, sizeof(seqno)) != ZB_STATUS_SUCCESS) {
        return false;
    }
    if (!ZbNwkGetSecMaterial(zb, seqno, &material)) {
        return false;
    }
    if (active_key != NULL) {
        *active_key = material;
    }
    return true;
}

IPC_REQ_CALLBACK_FUNC(ZbNlmeNetDiscReq, MSG_M4TOM0_NLME_NET_DISC, struct ZbNlmeNetDiscReqT, struct ZbNlmeNetDiscConfT);
/* Followed up in MSG_M0TOM4_NLME_NET_DISC_CB handler */

IPC_REQ_CALLBACK_FUNC(ZbNlmeLeaveReq, MSG_M4TOM0_NLME_LEAVE, struct ZbNlmeLeaveReqT, struct ZbNlmeLeaveConfT);
/* Followed up in MSG_M0TOM4_NLME_LEAVE_CB handler */

IPC_REQ_CONF_FUNC(ZbNlmePermitJoinReq, MSG_M4TOM0_NLME_PERMIT_JOIN, struct ZbNlmePermitJoinReqT, struct ZbNlmePermitJoinConfT);
IPC_REQ_CONF_FUNC(ZbNlmeResetReq, MSG_M4TOM0_NLME_RESET, struct ZbNlmeResetReqT, struct ZbNlmeResetConfT);

IPC_REQ_CALLBACK_FUNC(ZbNlmeSyncReq, MSG_M4TOM0_NLME_SYNC, struct ZbNlmeSyncReqT, struct ZbNlmeSyncConfT);
/* Followed up in MSG_M0TOM4_NLME_SYNC_CB handler */

IPC_REQ_CALLBACK_FUNC(ZbNlmeRouteDiscReq, MSG_M4TOM0_NLME_ROUTE_DISC, struct ZbNlmeRouteDiscReqT, struct ZbNlmeRouteDiscConfT);
/* Followed up in MSG_M0TOM4_NLME_ROUTE_DISC_CB handler */

enum WpanJoinPolicyT
ZbNlmeJoiningPolicyGet(struct ZigBeeT *zb)
{
    /* This is only for zbcli. We don't need this for 2.4 GHz operation. */
    return WPAN_JOIN_POLICY_ALL;
}

void
ZbNwkNeighborClearAll(struct ZigBeeT *zb, bool keep_parent, bool keep_children)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_NWK_NNT_CLEAR_ALL;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)keep_parent;
    ipcc_req->Data[1] = (uint32_t)keep_children;
    ZIGBEE_CmdTransfer();
}

IPC_REQ_CONF_FUNC(ZbNlmeDirectJoinReq, MSG_M4TOM0_NLME_DIRECT_JOIN, struct ZbNlmeDirectJoinReqT, struct ZbNlmeDirectJoinConfT);
IPC_REQ_CONF_FUNC(ZbNlmeSetInterface, MSG_M4TOM0_NLME_SET_INTERFACE, struct ZbNlmeSetInterfaceReqT, struct ZbNlmeSetInterfaceConfT);
IPC_REQ_CONF_FUNC(ZbNlmeGetInterface, MSG_M4TOM0_NLME_GET_INTERFACE, struct ZbNlmeGetInterfaceReqT, struct ZbNlmeGetInterfaceConfT);

bool
ZbNwkIfSetTxPower(struct ZigBeeT *zb, const char *name, int8_t tx_power)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_NWK_IFC_SET_TX_POWER;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)name;
    ipcc_req->Data[1] = (uint32_t)tx_power;
    ZIGBEE_CmdTransfer();
    return zb_ipc_m4_get_retval() != 0U ? true : false;
}

bool
ZbNwkIfGetTxPower(struct ZigBeeT *zb, const char *name, int8_t *tx_power)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_NWK_IFC_GET_TX_POWER;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)name;
    ipcc_req->Data[1] = (uint32_t)tx_power;
    ZIGBEE_CmdTransfer();
    return zb_ipc_m4_get_retval() != 0U ? true : false;
}

bool
ZbNwkSetFrameCounter(struct ZigBeeT *zb, uint8_t keySeqno, uint64_t srcAddr, uint32_t newFrameCount)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_NWK_SET_FRAME_COUNTER;
    ipcc_req->Size = 4;
    ipcc_req->Data[0] = (uint32_t)keySeqno;
    zb_ipc_m4_memcpy2((void*)&ipcc_req->Data[1], &srcAddr, 8);
    ipcc_req->Data[3] = (uint32_t)newFrameCount;
    ZIGBEE_CmdTransfer();
    return zb_ipc_m4_get_retval() != 0U ? true : false;
}

struct nwk_fastpoll_entry_t *
ZbNwkFastPollRequest(struct ZigBeeT *zb, unsigned int delay, unsigned int timeout)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_NWK_FAST_POLL_REQUEST;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)delay;
    ipcc_req->Data[1] = (uint32_t)timeout;
    ZIGBEE_CmdTransfer();
    return (struct nwk_fastpoll_entry_t *)zb_ipc_m4_get_retval();
}

bool
ZbNwkFastPollRelease(struct ZigBeeT *zb, struct nwk_fastpoll_entry_t *handle)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_NWK_FAST_POLL_RELEASE;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)handle;
    ZIGBEE_CmdTransfer();
    return zb_ipc_m4_get_retval() != 0U ? true : false;
}

unsigned int
ZbNwkFastPollResourceCount(struct ZigBeeT *zb)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_NWK_FAST_POLL_RESOURCE;
    ipcc_req->Size = 0;
    ZIGBEE_CmdTransfer();
    return zb_ipc_m4_get_retval();
}

bool
ZbNwkSendEdkaReq(struct ZigBeeT *zb)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_NWK_EDKA_SEND;
    ipcc_req->Size = 0;
    ZIGBEE_CmdTransfer();
    return zb_ipc_m4_get_retval() != 0U ? true : false;
}

/* Only required by zbcli. And only helps with automated verification of test cases.
 * Do not use for certification. */
void
ZbNwkAddrSetNextChildAddr(struct ZigBeeT *zb, uint16_t nextChildAddr)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_NWK_SET_NEXT_CHILD_ADDR;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)nextChildAddr;
    ZIGBEE_CmdTransfer();
}

bool
ZbNwkCommissioningConfig(struct ZigBeeT *zb, struct ZbNwkCommissioningInfo *commission_info)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_NWK_SET_COMM_INFO;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)commission_info;
    ZIGBEE_CmdTransfer();
    return zb_ipc_m4_get_retval();
}

/******************************************************************************
 * ZDO Utility
 ******************************************************************************
 */
struct zdo_filter_cb_info_t {
    struct ZbZdoFilterT *filter;
    /* void (*callback)(struct ZigBeeT *zb, struct ZbZdoDeviceAnnceT *annce, uint8_t seqno, void *arg); */
    void *callback;
    void *arg;
};

#define ZB_IPC_ZDO_FILTER_CB_LIST_MAX         8U
static struct zdo_filter_cb_info_t zdo_filter_cb_list[ZB_IPC_ZDO_FILTER_CB_LIST_MAX];

static struct zdo_filter_cb_info_t *
zdo_filter_cbinfo_get(void)
{
    struct zdo_filter_cb_info_t *cb_info;
    unsigned int i;

    for (i = 0; i < ZB_IPC_ZDO_FILTER_CB_LIST_MAX; i++) {
        cb_info = &zdo_filter_cb_list[i];
        if (cb_info->filter == NULL) {
            return cb_info;
        }
    }
    return NULL;
}

static bool
zdo_filter_cbinfo_del(struct ZbZdoFilterT *filter)
{
    struct zdo_filter_cb_info_t *cb_info;
    unsigned int i;

    if (filter == NULL) {
        return false;
    }
    for (i = 0; i < ZB_IPC_ZDO_FILTER_CB_LIST_MAX; i++) {
        cb_info = &zdo_filter_cb_list[i];
        if (cb_info->filter == filter) {
            cb_info->filter = NULL;
            return true;
        }
    }
    return false;
}

struct ZbZdoFilterT *
ZbZdoDeviceAnnceFilterRegister(struct ZigBeeT *zb,
    int (*callback)(struct ZigBeeT *zb, struct ZbZdoDeviceAnnceT *annce, uint8_t seqno, void *arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zdo_filter_cb_info_t *cb_info;
    struct ZbZdoFilterT *filter;

    if (callback == NULL) {
        return NULL;
    }
    cb_info = zdo_filter_cbinfo_get();
    if (cb_info == NULL) {
        return NULL;
    }

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZDO_DEVICE_ANNCE_FILTER_REG;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)cb_info;
    ZIGBEE_CmdTransfer();
    filter = (struct ZbZdoFilterT *)zb_ipc_m4_get_retval();
    if (filter != NULL) {
        cb_info->filter = filter;
        cb_info->callback = (void *)callback;
        cb_info->arg = arg;
    }
    return filter;
    /* Followed up in MSG_M0TOM4_ZDO_DEVICE_ANNCE_FILTER_CB handler */
}

/******************************************************************************
 * ZDO
 ******************************************************************************
 */
IPC_REQ_CALLBACK_FUNC(ZbZdoNwkAddrReq, MSG_M4TOM0_ZDO_NWK_ADDR, struct ZbZdoNwkAddrReqT, struct ZbZdoNwkAddrRspT);
/* Followed up in MSG_M0TOM4_ZDO_NWK_ADDR_CB handler */

IPC_REQ_CALLBACK_FUNC(ZbZdoIeeeAddrReq, MSG_M4TOM0_ZDO_IEEE_ADDR, struct ZbZdoIeeeAddrReqT, struct ZbZdoIeeeAddrRspT);
/* Followed up in MSG_M0TOM4_ZDO_IEEE_ADDR_CB handler */

IPC_REQ_CALLBACK_FUNC(ZbZdoNodeDescReq, MSG_M4TOM0_ZDO_NODE_DESC, struct ZbZdoNodeDescReqT, struct ZbZdoNodeDescRspT);
/* Followed up in MSG_M0TOM4_ZDO_NODE_DESC_CB handler */

IPC_REQ_CALLBACK_FUNC(ZbZdoPowerDescReq, MSG_M4TOM0_ZDO_POWER_DESC, struct ZbZdoPowerDescReqT, struct ZbZdoPowerDescRspT);
/* Followed up in MSG_M0TOM4_ZDO_POWER_DESC_CB handler */

IPC_REQ_CALLBACK_FUNC(ZbZdoSimpleDescReq, MSG_M4TOM0_ZDO_SIMPLE_DESC, struct ZbZdoSimpleDescReqT, struct ZbZdoSimpleDescRspT);
/* Followed up in MSG_M0TOM4_ZDO_SIMPLE_DESC_CB handler */

IPC_REQ_CALLBACK_FUNC(ZbZdoActiveEpReq, MSG_M4TOM0_ZDO_ACTIVE_EP, struct ZbZdoActiveEpReqT, struct ZbZdoActiveEpRspT);
/* Followed up in MSG_M0TOM4_ZDO_ACTIVE_EP_CB handler */

IPC_REQ_CALLBACK_FUNC(ZbZdoMatchDescReq, MSG_M4TOM0_ZDO_MATCH_DESC, struct ZbZdoMatchDescReqT, struct ZbZdoMatchDescRspT);
/* Followed up in MSG_M0TOM4_ZDO_MATCH_DESC_CB handler */

enum ZbStatusCodeT
ZbZdoMatchDescMulti(struct ZigBeeT *zb, struct ZbZdoMatchDescReqT *req,
    void (*callback)(struct ZbZdoMatchDescRspT *ind, void *cb_arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    enum ZbStatusCodeT status;

    /* The callback must be static so we can receive multiple responses. We don't check here if
     * we're overwriting an existing callback, so application cannot effecticely perform more
     * than one of these at a time. */

    /* Check if a request is already active */
    if (zdo_match_multi_cb != NULL) {
        return ZB_ZDP_STATUS_TABLE_FULL;
    }
    zdo_match_multi_cb = callback;
    zdo_match_multi_arg = arg;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZDO_MATCH_DESC_MULTI;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)req;
    ZIGBEE_CmdTransfer();
    status = (enum ZbStatusCodeT)zb_ipc_m4_get_retval();
    if (status != ZB_STATUS_SUCCESS) {
        zdo_match_multi_cb = NULL;
    }
    return status;
    /* Followed up in MSG_M0TOM4_ZDO_MATCH_DESC_MULTI_CB handler */
}

IPC_REQ_FUNC(ZbZdoDeviceAnnce, MSG_M4TOM0_ZDO_DEVICE_ANNCE, struct ZbZdoDeviceAnnceT);
IPC_REQ_FUNC(ZbZdoDeviceAnnceAlias, MSG_M4TOM0_ZDO_DEVICE_ANNCE_ALIAS, struct ZbZdoDeviceAnnceT);

IPC_REQ_CALLBACK_FUNC(ZbZdoBindReq, MSG_M4TOM0_ZDO_BIND, struct ZbZdoBindReqT, struct ZbZdoBindRspT);
/* Followed up in MSG_M0TOM4_ZDO_BIND_CB handler */

IPC_REQ_CALLBACK_FUNC(ZbZdoUnbindReq, MSG_M4TOM0_ZDO_UNBIND, struct ZbZdoBindReqT, struct ZbZdoBindRspT);
/* Followed up in MSG_M0TOM4_ZDO_UNBIND_CB handler */

IPC_REQ_CALLBACK_FUNC(ZbZdoLqiReq, MSG_M4TOM0_ZDO_MGMT_LQI, struct ZbZdoLqiReqT, struct ZbZdoLqiRspT);
/* Followed up in MSG_M0TOM4_ZDO_MGMT_LQI_CB handler */

IPC_REQ_CALLBACK_FUNC(ZbZdoRtgReq, MSG_M4TOM0_ZDO_MGMT_RTG, struct ZbZdoRtgReqT, struct ZbZdoRtgRspT);
/* Followed up in MSG_M0TOM4_ZDO_MGMT_RTG_CB handler */

IPC_REQ_CALLBACK_FUNC(ZbZdoMgmtBindReq, MSG_M4TOM0_ZDO_MGMT_BIND, struct ZbZdoMgmtBindReqT, struct ZbZdoMgmtBindRspT);
/* Followed up in MSG_M0TOM4_ZDO_MGMT_BIND_CB handler */

IPC_REQ_CALLBACK_FUNC(ZbZdoLeaveReq, MSG_M4TOM0_ZDO_MGMT_LEAVE, struct ZbZdoLeaveReqT, struct ZbZdoLeaveRspT);
/* Followed up in MSG_M0TOM4_ZDO_MGMT_LEAVE_CB handler */

IPC_REQ_CALLBACK_FUNC(ZbZdoPermitJoinReq, MSG_M4TOM0_ZDO_MGMT_PERMIT_JOIN, struct ZbZdoPermitJoinReqT, struct ZbZdoPermitJoinRspT);
/* Followed up in MSG_M0TOM4_ZDO_MGMT_PERMIT_JOIN_CB handler */

IPC_REQ_CALLBACK_FUNC(ZbZdoNwkUpdateReq, MSG_M4TOM0_ZDO_MGMT_NWK_UPDATE, struct ZbZdoNwkUpdateReqT, struct ZbZdoNwkUpdateNotifyT);
/* Followed up in MSG_M0TOM4_ZDO_MGMT_NWK_UPDATE_CB handler */

struct ZbZdoFilterT *
ZbZdoNwkUpdateNotifyFilterRegister(struct ZigBeeT *zb,
    int (*callback)(struct ZigBeeT *zb, struct ZbZdoNwkUpdateNotifyT *msg, uint8_t seqno, void *arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zdo_filter_cb_info_t *cb_info;
    struct ZbZdoFilterT *filter;

    if (callback == NULL) {
        return NULL;
    }
    cb_info = zdo_filter_cbinfo_get();
    if (cb_info == NULL) {
        return NULL;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZDO_MGMT_NWK_UPDATE_FILTER_REGISTER;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)cb_info;
    ZIGBEE_CmdTransfer();
    filter = (struct ZbZdoFilterT *)zb_ipc_m4_get_retval();
    if (filter != NULL) {
        cb_info->filter = filter;
        cb_info->callback = (void *)callback;
        cb_info->arg = arg;
    }
    return filter;
    /* Callbacks go through MSG_M0TOM4_ZDO_MGMT_NWK_UPDATE_FILTER_CB */
}

void
ZbZdoFilterRemove(struct ZigBeeT *zb, struct ZbZdoFilterT *filter)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    if (!zdo_filter_cbinfo_del(filter)) {
        return;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZDO_FILTER_DEL;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)filter;
    ZIGBEE_CmdTransfer();
}

/******************************************************************************
 * ZCL
 ******************************************************************************
 */

ZbUptimeT
ZbZclUptime(struct ZigBeeT *zb)
{
    uint32_t uptime;

    ZbBdbGet(zb, ZB_BDB_Uptime, &uptime, sizeof(uptime));
    return (ZbUptimeT)uptime;
}

bool
ZbZclDeviceLogCheckAllow(struct ZigBeeT *zb, struct ZbApsdeDataIndT *dataIndPtr, struct ZbZclHeaderT *zclHdrPtr)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_DEVICE_LOG_CHECK;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)dataIndPtr;
    ipcc_req->Data[1] = (uint32_t)zclHdrPtr;
    ZIGBEE_CmdTransfer();
    return zb_ipc_m4_get_retval() != 0U ? true : false;
}

bool
ZbZclBasicPostAlarm(struct ZigBeeT *zb, uint8_t endpoint, uint8_t alarm_code)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_BASIC_SERVER_ALARM_POST;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)endpoint;
    ipcc_req->Data[1] = (uint32_t)alarm_code;
    ZIGBEE_CmdTransfer();
    return zb_ipc_m4_get_retval() != 0U ? true : false;
}

void
ZbZclBasicServerResetCmdConfig(struct ZigBeeT *zb, bool allow_reset)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_BASIC_SERVER_CONFIG_RESET;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)allow_reset;
    ZIGBEE_CmdTransfer();
}

enum ZclStatusCodeT
ZbZclBasicWriteDirect(struct ZigBeeT *zb, uint8_t endpoint, uint16_t attributeId, const uint8_t *ptr, unsigned int len)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_BASIC_SERVER_LOCAL_WRITE;
    ipcc_req->Size = 4;
    ipcc_req->Data[0] = (uint32_t)endpoint;
    ipcc_req->Data[1] = (uint32_t)attributeId;
    ipcc_req->Data[2] = (uint32_t)ptr;
    ipcc_req->Data[3] = (uint32_t)len;
    ZIGBEE_CmdTransfer();
    return (enum ZclStatusCodeT)zb_ipc_m4_get_retval();
}

void
ZbZclBasicServerConfigDefaults(struct ZigBeeT *zb, const struct ZbZclBasicServerDefaults *defaults)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_BASIC_SERVER_CONFIG_DEFAULTS;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)defaults;
    ZIGBEE_CmdTransfer();
}

bool
ZbZclDiagServerAlloc(struct ZigBeeT *zb, uint8_t endpoint, uint16_t profileId, enum ZbStatusCodeT minSecurity)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_DIAG_SERVER_ALLOC;
    ipcc_req->Size = 3;
    ipcc_req->Data[0] = (uint32_t)endpoint;
    ipcc_req->Data[1] = (uint32_t)profileId;
    ipcc_req->Data[2] = (uint32_t)minSecurity;
    ZIGBEE_CmdTransfer();
    return (bool)zb_ipc_m4_get_retval();
}

void
ZbZclAddEndpoint(struct ZigBeeT *zb, struct ZbApsmeAddEndpointReqT *req, struct ZbApsmeAddEndpointConfT *conf)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_ENDPOINT_ADD;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)req;
    ipcc_req->Data[1] = (uint32_t)conf;
    ZIGBEE_CmdTransfer();
}

void
ZbZclRemoveEndpoint(struct ZigBeeT *zb, struct ZbApsmeRemoveEndpointReqT *req, struct ZbApsmeRemoveEndpointConfT *conf)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_ENDPOINT_DEL;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)req;
    ipcc_req->Data[1] = (uint32_t)conf;
    ZIGBEE_CmdTransfer();
}

IPC_CLUSTER_REQ_CALLBACK_FUNC(ZbZclReadReq, MSG_M4TOM0_ZCL_READ_REQ, ZbZclReadReqT, ZbZclReadRspT);
/* Followed up in MSG_M0TOM4_ZCL_READ_CB handler */

enum ZclStatusCodeT
ZbZclWriteReq(struct ZbZclClusterT *clusterPtr, ZbZclWriteReqT *req,
    void (*callback)(const struct ZbZclWriteRspT *rsp, void *cbarg), void *cbarg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zb_ipc_m4_cb_info_t *info;

    info = zb_ipc_m4_cb_info_alloc((void *)callback, cbarg);
    if (info == NULL) {
        return ZCL_STATUS_INSUFFICIENT_SPACE;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_WRITE_REQ;
    ipcc_req->Size = 3;
    ipcc_req->Data[0] = (uint32_t)clusterPtr;
    ipcc_req->Data[1] = (uint32_t)req;
    ipcc_req->Data[2] = (uint32_t)info;
    ZIGBEE_CmdTransfer();
    return (enum ZclStatusCodeT)zb_ipc_m4_get_retval();
    /* Followed up in MSG_M0TOM4_ZCL_WRITE_CB handler */
}

IPC_CLUSTER_REQ_CALLBACK_FUNC(ZbZclDiscoverAttrReq, MSG_M4TOM0_ZCL_DISCOVER_ATTR_REQ,
    ZbZclDiscoverAttrReqT, ZbZclDiscoverAttrRspT);
/* Followed up in MSG_M0TOM4_ZCL_DISCOVER_ATTR_CB handler */

uint8_t
ZbZclGetNextSeqnum(void)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_GET_SEQNUM;
    ipcc_req->Size = 0;
    ZIGBEE_CmdTransfer();
    return (uint8_t)zb_ipc_m4_get_retval();
}

enum ZclStatusCodeT
ZbZclCommandReq(struct ZigBeeT *zb, struct ZbZclCommandReqT *zclReq,
    void (*callback)(struct ZbZclCommandRspT *rsp, void *arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zb_ipc_m4_cb_info_t *info = NULL;

    if (callback != NULL) {
        info = zb_ipc_m4_cb_info_alloc((void *)callback, arg);
        if (info == NULL) {
            return (enum ZclStatusCodeT) ZB_STATUS_ALLOC_FAIL;
        }
        if (ZbApsAddrIsBcast(&zclReq->dst)) {
            info->zcl_recv_multi_rsp = true; /* callback only freed on ZCL_STATUS_TIMEOUT */
        }
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_COMMAND_REQ;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)zclReq;
    ipcc_req->Data[1] = (uint32_t)info;
    ZIGBEE_CmdTransfer();
    return (enum ZclStatusCodeT)zb_ipc_m4_get_retval();
    /* Followed up in MSG_M0TOM4_ZCL_COMMAND_REQ_CB handler if callback != NULL */
}

void
ZbZclSendDefaultResponse(struct ZbZclClusterT *clusterPtr, struct ZbApsdeDataIndT *dataIndPtr,
    struct ZbZclHeaderT *zclHdrPtr, enum ZclStatusCodeT status)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_SEND_DEFAULT_RSP;
    ipcc_req->Size = 4;
    ipcc_req->Data[0] = (uint32_t)clusterPtr;
    ipcc_req->Data[1] = (uint32_t)dataIndPtr;
    ipcc_req->Data[2] = (uint32_t)zclHdrPtr;
    ipcc_req->Data[3] = (uint32_t)status;
    ZIGBEE_CmdTransfer();
}

enum ZclStatusCodeT
ZbZclClusterCommandReq(struct ZbZclClusterT *clusterPtr, struct ZbZclClusterCommandReqT *req,
    void (*callback)(struct ZbZclCommandRspT *zcl_rsp, void *arg), void *arg)
{
    struct ZbZclCommandReqT zcl_req;

    /* Configure the request */
    ZbZclClusterInitCommandReq(clusterPtr, &zcl_req);
    zcl_req.dst = req->dst;
    /* ZCL Header */
    zcl_req.hdr.cmdId = req->cmdId;
    zcl_req.hdr.frameCtrl.frameType = ZCL_FRAMETYPE_CLUSTER;
    zcl_req.hdr.frameCtrl.manufacturer = (clusterPtr->mfrCode != 0U) ? 1U : 0U;
    zcl_req.hdr.frameCtrl.direction = (clusterPtr->direction == ZCL_DIRECTION_TO_SERVER) ? ZCL_DIRECTION_TO_CLIENT : ZCL_DIRECTION_TO_SERVER;
    zcl_req.hdr.frameCtrl.noDefaultResp = req->noDefaultResp;
    zcl_req.hdr.manufacturerCode = clusterPtr->mfrCode;
    zcl_req.hdr.seqNum = ZbZclGetNextSeqnum();

    /* Payload */
    zcl_req.payload = req->payload;
    zcl_req.length = req->length;

    return ZbZclCommandReq(clusterPtr->zb, &zcl_req, callback, arg);
}

struct cluster_command_delay_t {
    struct ZbZclClusterT *cluster;
    struct ZbTimerT *timer;
    struct ZbZclClusterCommandReqT req;
    void (*callback)(struct ZbZclCommandRspT *rsp, void *arg);
    void *arg;
};

static void
cluster_command_timer(struct ZigBeeT *zb, void *arg)
{
    struct cluster_command_delay_t *info = arg;
    enum ZclStatusCodeT status;

    status = ZbZclClusterCommandReq(info->cluster, &info->req, info->callback, info->arg);
    if (status != ZCL_STATUS_SUCCESS) {
        if (info->callback != NULL) {
            struct ZbZclCommandRspT rsp;

            (void)memset(&rsp, 0, sizeof(rsp));
            rsp.status = status;
            rsp.profileId = info->cluster->profileId;
            rsp.clusterId = info->cluster->clusterId;
            info->callback(&rsp, info->arg);
        }
    }

    ZbTimerFree(info->timer);
    ZbHeapFree(info->cluster->zb, info);
}

enum ZclStatusCodeT
ZbZclClusterCommandReqDelayed(struct ZbZclClusterT *cluster, struct ZbZclClusterCommandReqT *req,
    unsigned int delay, void (*callback)(struct ZbZclCommandRspT *zcl_rsp, void *arg), void *arg)
{
    struct cluster_command_delay_t *info;

    info = ZbHeapAlloc(cluster->zb, sizeof(struct cluster_command_delay_t) + req->length);
    if (info == NULL) {
        return ZCL_STATUS_FAILURE;
    }
    (void)memset(info, 0, sizeof(struct cluster_command_delay_t));
    info->timer = ZbTimerAlloc(cluster->zb, cluster_command_timer, info);
    if (info->timer == NULL) {
        ZbHeapFree(cluster->zb, info);
        return ZCL_STATUS_FAILURE;
    }
    info->cluster = cluster;
    info->callback = callback;
    info->arg = arg;
    (void)memcpy(&info->req, req, sizeof(struct ZbZclClusterCommandReqT));
    info->req.payload = (uint8_t *)(info + 1);
    (void)memcpy(info->req.payload, req->payload, req->length);

    ZbTimerReset(info->timer, delay);
    return ZCL_STATUS_SUCCESS;
}

enum ZclStatusCodeT
ZbZclClusterCommandRsp(struct ZbZclClusterT *clusterPtr, struct ZbZclAddrInfoT *dstInfo,
    uint8_t cmdId, struct ZbApsBufT *payloads, uint8_t numPayloads)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_CLUSTER_CMD_RSP;
    ipcc_req->Size = 5;
    ipcc_req->Data[0] = (uint32_t)clusterPtr;
    ipcc_req->Data[1] = (uint32_t)dstInfo;
    ipcc_req->Data[2] = (uint32_t)cmdId;
    ipcc_req->Data[3] = (uint32_t)payloads;
    ipcc_req->Data[4] = (uint32_t)numPayloads;
    ZIGBEE_CmdTransfer();
    return (enum ZclStatusCodeT)zb_ipc_m4_get_retval();
}

enum ZclStatusCodeT
ZbZclClusterCommandRspWithCb(struct ZbZclClusterT *clusterPtr, struct ZbZclAddrInfoT *dstInfo, uint8_t cmdId,
    struct ZbApsBufT *payloads, uint8_t numPayloads, void (*callback)(struct ZbApsdeDataConfT *conf, void *arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zb_ipc_m4_cb_info_t *info = NULL;
    enum ZclStatusCodeT status;

    if (callback == NULL) {
        return ZbZclClusterCommandRsp(clusterPtr, dstInfo, cmdId, payloads, numPayloads);
    }
    info = zb_ipc_m4_cb_info_alloc((void *)callback, arg);
    if (info == NULL) {
        return ZCL_STATUS_ALLOC_FAIL;
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_CLUSTER_CMD_RSP_WITH_CB;
    ipcc_req->Size = 6;
    ipcc_req->Data[0] = (uint32_t)clusterPtr;
    ipcc_req->Data[1] = (uint32_t)dstInfo;
    ipcc_req->Data[2] = (uint32_t)cmdId;
    ipcc_req->Data[3] = (uint32_t)payloads;
    ipcc_req->Data[4] = (uint32_t)numPayloads;
    ipcc_req->Data[5] = (uint32_t)info;
    ZIGBEE_CmdTransfer();
    status = (enum ZclStatusCodeT)zb_ipc_m4_get_retval();
    if (status != ZCL_STATUS_SUCCESS) {
        zb_ipc_m4_cb_info_free(info);
    }
    return status;
    /* Followed up in MSG_M0TOM4_ZCL_CLUSTER_CMD_RSP_CONF_CB handler */
}

enum ZclStatusCodeT
ZbZclSendClusterStatusResponse(struct ZbZclClusterT *clusterPtr, struct ZbApsdeDataIndT *dataIndPtr,
    struct ZbZclHeaderT *zclHdrPtr, uint8_t cmdId, uint8_t *zclPayload, uint8_t zclPaylen)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_CLUSTER_CMD_RSP_WITH_STATUS;
    ipcc_req->Size = 6;
    ipcc_req->Data[0] = (uint32_t)clusterPtr;
    ipcc_req->Data[1] = (uint32_t)dataIndPtr;
    ipcc_req->Data[2] = (uint32_t)zclHdrPtr;
    ipcc_req->Data[3] = (uint32_t)cmdId;
    ipcc_req->Data[4] = (uint32_t)zclPayload;
    ipcc_req->Data[5] = (uint32_t)zclPaylen;
    ZIGBEE_CmdTransfer();
    return (enum ZclStatusCodeT)zb_ipc_m4_get_retval();
}

bool
ZbZclClusterEndpointRegister(struct ZbZclClusterT *clusterPtr)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    uint32_t retval;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_CLUSTER_EP_REGISTER;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)clusterPtr;
    ZIGBEE_CmdTransfer();
    retval = zb_ipc_m4_get_retval();
    return retval != 0 ? true : false;
}

bool
ZbZclClusterEndpointRemove(struct ZbZclClusterT *clusterPtr)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    uint32_t retval;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_CLUSTER_EP_REMOVE;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)clusterPtr;
    ZIGBEE_CmdTransfer();
    retval = zb_ipc_m4_get_retval();
    return retval != 0 ? true : false;
}

enum ZclStatusCodeT
ZbZclClusterBind(struct ZbZclClusterT *clusterPtr, uint8_t endpoint, uint16_t profileId, enum ZbZclDirectionT direction)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_CLUSTER_BIND;
    ipcc_req->Size = 4;
    ipcc_req->Data[0] = (uint32_t)clusterPtr;
    ipcc_req->Data[1] = (uint32_t)endpoint;
    ipcc_req->Data[2] = (uint32_t)profileId;
    ipcc_req->Data[3] = (uint32_t)direction;
    ZIGBEE_CmdTransfer();
    return (enum ZclStatusCodeT)zb_ipc_m4_get_retval();
    /* Data indication callbacks go to MSG_M0TOM4_ZCL_CLUSTER_DATA_IND */
}

void
ZbZclClusterUnbind(struct ZbZclClusterT *clusterPtr)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_CLUSTER_UNBIND;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)clusterPtr;
    ZIGBEE_CmdTransfer();
}

struct ZbApsFilterT *
ZbZclClusterReverseBind(struct ZbZclClusterT *clusterPtr)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_CLUSTER_REVERSE_BIND;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)clusterPtr;
    ZIGBEE_CmdTransfer();
    return (struct ZbApsFilterT *)zb_ipc_m4_get_retval();
    /* Data indication callbacks go to MSG_M0TOM4_ZCL_CLUSTER_DATA_IND */
}

void
ZbZclClusterReverseUnbind(struct ZbZclClusterT *clusterPtr, struct ZbApsFilterT *filter)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_CLUSTER_REVERSE_UNBIND;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)clusterPtr;
    ipcc_req->Data[1] = (uint32_t)filter;
    ZIGBEE_CmdTransfer();
}

enum ZclStatusCodeT
ZbZclClusterRegisterAlarmResetHandler(struct ZbZclClusterT *clusterPtr,
    enum ZclStatusCodeT (*callback)(struct ZbZclClusterT *clusterPtr, uint8_t alarm_code,
        uint16_t cluster_id, struct ZbApsdeDataIndT *data_ind, struct ZbZclHeaderT *hdr))
{
    Zigbee_Cmd_Request_t *ipcc_req;
    enum ZclStatusCodeT status;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_CLUSTER_ALARM_ADD_FILTER;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)clusterPtr;
    ipcc_req->Data[1] = (uint32_t)callback;
    ZIGBEE_CmdTransfer();
    status = (enum ZclStatusCodeT)zb_ipc_m4_get_retval();
    return status;
    /* Callbacks followed up in MSG_M0TOM4_ZCL_CLUSTER_ALARM_CB handler. */
}

void
ZbZclClusterRemoveAlarmResetHandler(struct ZbZclClusterT *clusterPtr)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_CLUSTER_ALARM_REMOVE_FILTER;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)clusterPtr;
    ZIGBEE_CmdTransfer();
}

void
ZbZclClusterSendAlarm(struct ZbZclClusterT *clusterPtr, uint8_t src_endpoint, uint8_t alarm_code)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_CLUSTER_ALARM_SEND;
    ipcc_req->Size = 3;
    ipcc_req->Data[0] = (uint32_t)clusterPtr;
    ipcc_req->Data[1] = (uint32_t)src_endpoint;
    ipcc_req->Data[2] = (uint32_t)alarm_code;
    ZIGBEE_CmdTransfer();
}

/******************************************************************************
 * SE Key Exchange
 ******************************************************************************
 */
enum ZclStatusCodeT
ZbZclKeWithDevice(struct ZigBeeT *zb, uint64_t partnerAddr, bool aps_req_key,
    void (*callback)(uint64_t partnerAddr, uint16_t keSuite, enum ZbZclKeyStatusT key_status, void *arg), void *arg)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    struct zb_ipc_m4_cb_info_t *info = NULL;
    enum ZclStatusCodeT status;

    if (callback != NULL) {
        info = zb_ipc_m4_cb_info_alloc((void *)callback, arg);
        if (info == NULL) {
            return ZCL_STATUS_ALLOC_FAIL;
        }
    }
    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZCL_KE_WITH_DEVICE;
    ipcc_req->Size = 4;
    zb_ipc_m4_memcpy2((void*)&ipcc_req->Data[0], &partnerAddr, 8);
    ipcc_req->Data[2] = (uint32_t)aps_req_key;
    ipcc_req->Data[3] = (uint32_t)info;
    ZIGBEE_CmdTransfer();
    status = (enum ZclStatusCodeT)zb_ipc_m4_get_retval();
    if (status != ZCL_STATUS_SUCCESS) {
        zb_ipc_m4_cb_info_free(info);
    }
    return status;
    /* Followed up in MSG_M0TOM4_ZCL_KE_WITH_DEVICE_CB handler */
}

/******************************************************************************
 * Certification Test Hooks
 ******************************************************************************
 */
void
ZbTestCaseClear(struct ZigBeeT *zb)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_TEST_CASE_CLEAR;
    ipcc_req->Size = 0;
    ZIGBEE_CmdTransfer();
}

uint32_t
ZbTestCaseCurrent(struct ZigBeeT *zb)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_TEST_CASE_CURRENT;
    ipcc_req->Size = 0;
    ZIGBEE_CmdTransfer();
    return zb_ipc_m4_get_retval();
}

void
ZbTestCaseDisable(struct ZigBeeT *zb, enum ZbTestcaseT testcase)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_TEST_CASE_DISABLE;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)testcase;
    ZIGBEE_CmdTransfer();
}

void
ZbTestCaseEnable(struct ZigBeeT *zb, enum ZbTestcaseT testcase)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_TEST_CASE_ENABLE;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)testcase;
    ZIGBEE_CmdTransfer();
}

bool
ZbApsFragDropTxAdd(struct ZigBeeT *zb, uint8_t blockNum)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    uint32_t retval;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_APS_FRAG_DROP_ADD;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)blockNum;
    ZIGBEE_CmdTransfer();
    retval = zb_ipc_m4_get_retval();
    return retval != 0 ? true : false;
}

void
ZbApsFragDropTxClear(struct ZigBeeT *zb)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_APS_FRAG_DROP_CLEAR;
    ipcc_req->Size = 0;
    ZIGBEE_CmdTransfer();
}

/******************************************************************************
 * AES & Hashing
 ******************************************************************************
 */
bool
ZbAesMmoHash(uint8_t const *data, const unsigned int length, uint8_t *digest)
{
    struct ZbHash hash;

    memset(&hash, 0, sizeof(struct ZbHash));
    ZbHashInit(&hash);
    ZbHashAdd(&hash, data, length);
    ZbHashDigest(&hash, digest);
    return true;
}

void
ZbHashInit(struct ZbHash *h)
{
    (void)memset(h->m, 0, sizeof(h->m));
    (void)memset(h->hash, 0, sizeof(h->hash));
    /* Don't touch h->key */
    h->length = 0;
}

void
ZbHashAdd(struct ZbHash *h, const void *data, uint32_t len)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZB_HASH_ADD;
    ipcc_req->Size = 3;
    ipcc_req->Data[0] = (uint32_t)h;
    ipcc_req->Data[1] = (uint32_t)data;
    ipcc_req->Data[2] = (uint32_t)len;
    ZIGBEE_CmdTransfer();
}
#ifdef ZIGBEE_DIRECT_ACTIVATED
void
ZbHashByte(struct ZbHash *h, uint8_t data)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZB_HASH_BYTE;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)h;
    ipcc_req->Data[1] = (uint32_t)data;
    ZIGBEE_CmdTransfer();
}
#endif /*ZIGBEE_DIRECT_ACTIVATED */

void
ZbHashDigest(struct ZbHash *h, void *digest)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_ZB_HASH_DIGEST;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)h;
    ipcc_req->Data[1] = (uint32_t)digest;
    ZIGBEE_CmdTransfer();
}

void
ZbHmacInit(struct ZbHash *h, const void *key, uint32_t len)
{
    const unsigned char ipad = 0x36;
    const unsigned char opad = 0x5c;
    unsigned int i;

    /* Initialize the hash data. */
    ZbHashInit(h);
    /* If the key is longer than the digest size, hash it into something smaller. */
    if (len > AES_BLOCK_SIZE) {
        ZbHashAdd(h, key, len);
        ZbHashDigest(h, h->key);
        ZbHashInit(h);
    }
    else {
        (void)memcpy(h->key, key, AES_BLOCK_SIZE);
    }

    /* Apply the inner padding and input the key to the hash function. */
    for (i = 0; i < AES_BLOCK_SIZE; i++) {
        h->key[i] ^= ipad;
    }
    ZbHashAdd(h, h->key, AES_BLOCK_SIZE);

    /* Undo the inner padding, and save the outer-padded key for later. */
    for (i = 0; i < AES_BLOCK_SIZE; i++) {
        h->key[i] ^= (ipad ^ opad);
    }
}

void
ZbHmacDigest(struct ZbHash *h, void *digest)
{
    /* Compute the inner digest of the inner-padded key and the message. */
    ZbHashDigest(h, digest);

    /* Reset the hash, and compute the digest of the outer-padded key and the inner digest. */
    ZbHashInit(h);
    ZbHashAdd(h, h->key, AES_BLOCK_SIZE);
    ZbHashAdd(h, digest, AES_BLOCK_SIZE);
    ZbHashDigest(h, digest);
}

/******************************************************************************
 * Security Helpers
 ******************************************************************************
 */
enum ZbStatusCodeT
ZbSecAddDeviceLinkKeyByKey(struct ZigBeeT *zb, uint64_t extAddr, uint8_t *key)
{
    struct ZbApsmeAddKeyReqT addKeyReq;
    struct ZbApsmeAddKeyConfT addKeyConf;

    (void)memset(&addKeyReq, 0, sizeof(addKeyReq));
    addKeyReq.keyType = ZB_SEC_KEYTYPE_TC_LINK;
    (void)memcpy(addKeyReq.key, key, sizeof(addKeyReq.key));
    addKeyReq.keySeqNumber = 0;
    addKeyReq.partnerAddr = extAddr;
    addKeyReq.keyAttribute = ZB_APSME_KEY_ATTR_VERIFIED; /* 0x00 */
    ZbApsmeAddKeyReq(zb, &addKeyReq, &addKeyConf);
    return addKeyConf.status;
}

enum ZbStatusCodeT
ZbSecAddDeviceLinkKeyByInstallCode(struct ZigBeeT *zb, uint64_t extAddr, uint8_t *ic, unsigned int len)
{
    uint8_t key[ZB_SEC_KEYSIZE];

    if (!ZbAesMmoHash(ic, len, key)) {
        return ZB_APS_STATUS_INVALID_PARAMETER;
    }
    return ZbSecAddDeviceLinkKeyByKey(zb, extAddr, key);
}

void
ZbSecKeyTransform(uint8_t *key, uint8_t input, uint8_t *keyOut)
{
    struct ZbHash hash;

    ZbHmacInit(&hash, key, ZB_SEC_KEYSIZE);
    ZbHmacAdd(&hash, &input, sizeof(input));
    ZbHmacDigest(&hash, keyOut);
}

#ifdef ZIGBEE_DIRECT_ACTIVATED
void
ZbCcmTransform(const void *key, const void *nonce, uint32_t nonce_len, void *m, uint32_t m_len, void *mic)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_AES_CCM_TRANSFORM;
    ipcc_req->Size = 6;
    ipcc_req->Data[0] = (uint32_t)key;
    ipcc_req->Data[1] = (uint32_t)nonce;
    ipcc_req->Data[2] = (uint32_t)nonce_len;
    ipcc_req->Data[3] = (uint32_t)m;
    ipcc_req->Data[4] = (uint32_t)m_len;
    ipcc_req->Data[5] = (uint32_t)mic;
    ZIGBEE_CmdTransfer();
}

void
ZbCcmAuthenticate(const void *key, const void *nonce, uint32_t nonce_len, const void *a, uint32_t a_len,
    const void *m, uint32_t m_len, void *mic, uint32_t mic_len)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_AES_CCM_AUTHENTICATE;
    ipcc_req->Size = 9;
    ipcc_req->Data[0] = (uint32_t)key;
    ipcc_req->Data[1] = (uint32_t)nonce;
    ipcc_req->Data[2] = (uint32_t)nonce_len;
    ipcc_req->Data[3] = (uint32_t)a;
    ipcc_req->Data[4] = (uint32_t)a_len;
    ipcc_req->Data[5] = (uint32_t)m;
    ipcc_req->Data[6] = (uint32_t)m_len;
    ipcc_req->Data[7] = (uint32_t)mic;
    ipcc_req->Data[8] = (uint32_t)mic_len;
    ZIGBEE_CmdTransfer();
}

int
ec_mul_x25519(const void *private, const void *base, void *result)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_EC_MUL_X25519;
    ipcc_req->Size = 3;
    ipcc_req->Data[0] = (uint32_t)private;
    ipcc_req->Data[1] = (uint32_t)base;
    ipcc_req->Data[2] = (uint32_t)result;
    ZIGBEE_CmdTransfer();
    return (int)zb_ipc_m4_get_retval();
}

int
ec_mul_p256(const void *priv, const void *pub_x, const void *pub_y, void *result_x, void *result_y)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_EC_MUL_P256;
    ipcc_req->Size = 5;
    ipcc_req->Data[0] = (uint32_t)priv;
    ipcc_req->Data[1] = (uint32_t)pub_x;
    ipcc_req->Data[2] = (uint32_t)pub_y;
    ipcc_req->Data[3] = (uint32_t)result_x;
    ipcc_req->Data[4] = (uint32_t)result_y;
    ZIGBEE_CmdTransfer();
    return (int)zb_ipc_m4_get_retval();
}

enum sha_status_t
SHA256Reset(SHA256Context *context)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_SHA256_RESET;
    ipcc_req->Size = 1;
    ipcc_req->Data[0] = (uint32_t)context;
    ZIGBEE_CmdTransfer();
    return (enum sha_status_t)zb_ipc_m4_get_retval();
}

enum sha_status_t
SHA256Input(SHA256Context *context, const uint8_t *bytes, unsigned int bytecount)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_SHA256_INPUT;
    ipcc_req->Size = 3;
    ipcc_req->Data[0] = (uint32_t)context;
    ipcc_req->Data[1] = (uint32_t)bytes;
    ipcc_req->Data[2] = (uint32_t)bytecount;
    ZIGBEE_CmdTransfer();
    return (enum sha_status_t)zb_ipc_m4_get_retval();
}

enum sha_status_t
SHA256Result(SHA256Context * context, uint8_t Message_Digest[SHA256HashSize])
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_SHA256_RESULT;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)context;
    ipcc_req->Data[1] = (uint32_t)Message_Digest;
    ZIGBEE_CmdTransfer();
    return (enum sha_status_t)zb_ipc_m4_get_retval();
}

int
hmacReset(HMACContext *context, enum SHAversion whichSha,
    const unsigned char *key, int key_len)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_SHA256_HMAC_RESET;
    ipcc_req->Size = 4;
    ipcc_req->Data[0] = (uint32_t)context;
    ipcc_req->Data[1] = (uint32_t)whichSha;
    ipcc_req->Data[2] = (uint32_t)key;
    ipcc_req->Data[3] = (uint32_t)key_len;
    ZIGBEE_CmdTransfer();
    return (int)zb_ipc_m4_get_retval();
}

int
hmacInput(HMACContext *context, const unsigned char *text, int text_len)
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_SHA256_HMAC_INPUT;
    ipcc_req->Size = 3;
    ipcc_req->Data[0] = (uint32_t)context;
    ipcc_req->Data[1] = (uint32_t)text;
    ipcc_req->Data[2] = (uint32_t)text_len;
    ZIGBEE_CmdTransfer();
    return (int)zb_ipc_m4_get_retval();
}

int
hmacResult(HMACContext * context, uint8_t digest[USHAMaxHashSize])
{
    Zigbee_Cmd_Request_t *ipcc_req;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_SHA256_HMAC_RESULT;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)context;
    ipcc_req->Data[1] = (uint32_t)digest;
    ZIGBEE_CmdTransfer();
    return (int)zb_ipc_m4_get_retval();
}
#endif /* ZIGBEE_DIRECT_ACTIVATED */

/******************************************************************************
 * Memory Helpers
 ******************************************************************************
 */
void *
zb_heap_alloc(struct ZigBeeT *zb, size_t sz, const char *filename, unsigned int line)
{
    /* The M4 has access to malloc */
    return malloc(sz);
}

void
zb_heap_free(struct ZigBeeT *zb, void *ptr, const char *filename, unsigned int line)
{
    free(ptr);
}

bool
zb_ipc_get_secured_mem_info(uint32_t *unsec_sram2a_sz, uint32_t *unsec_sram2b_sz)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    uint32_t retval;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_GET_SECURED_MEM_INFO;
    ipcc_req->Size = 2;
    ipcc_req->Data[0] = (uint32_t)unsec_sram2a_sz;
    ipcc_req->Data[1] = (uint32_t)unsec_sram2b_sz;
    ZIGBEE_CmdTransfer();
    retval = zb_ipc_m4_get_retval();
    return retval != 0 ? true : false;
}

unsigned long
ZbHeapAvailable(struct ZigBeeT *zb)
{
    Zigbee_Cmd_Request_t *ipcc_req;
    uint32_t retval;

    Pre_ZigbeeCmdProcessing();
    ipcc_req = ZIGBEE_Get_OTCmdPayloadBuffer();
    ipcc_req->ID = MSG_M4TOM0_GET_ZB_HEAP_AVAILABLE;
    ipcc_req->Size = 0;
    ZIGBEE_CmdTransfer();
    retval = zb_ipc_m4_get_retval();
    return (unsigned long)retval;
}

/******************************************************************************
 * CRC (required for ZCL reporting (hash)
 ******************************************************************************
 */
/*
 * Precomputed CRC table for IEEE 802.15.4 FCS fields. Intended to be
 * used in the reflected CRC algorithm. Generated using the Rocksoft
 * CRC table generator with the following parameters:
 *
 * Width = 2 bytes
 * Polynomial = x^16 + x^12 + x^5 + 1 (0x1021)
 * Reflected = true
 */
const uint16_t wpanCrcTable[256] = {
    0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF,
    0x8C48, 0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7,
    0x1081, 0x0108, 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E,
    0x9CC9, 0x8D40, 0xBFDB, 0xAE52, 0xDAED, 0xCB64, 0xF9FF, 0xE876,
    0x2102, 0x308B, 0x0210, 0x1399, 0x6726, 0x76AF, 0x4434, 0x55BD,
    0xAD4A, 0xBCC3, 0x8E58, 0x9FD1, 0xEB6E, 0xFAE7, 0xC87C, 0xD9F5,
    0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E, 0x54B5, 0x453C,
    0xBDCB, 0xAC42, 0x9ED9, 0x8F50, 0xFBEF, 0xEA66, 0xD8FD, 0xC974,
    0x4204, 0x538D, 0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB,
    0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1, 0xAB7A, 0xBAF3,
    0x5285, 0x430C, 0x7197, 0x601E, 0x14A1, 0x0528, 0x37B3, 0x263A,
    0xDECD, 0xCF44, 0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72,
    0x6306, 0x728F, 0x4014, 0x519D, 0x2522, 0x34AB, 0x0630, 0x17B9,
    0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5, 0xA96A, 0xB8E3, 0x8A78, 0x9BF1,
    0x7387, 0x620E, 0x5095, 0x411C, 0x35A3, 0x242A, 0x16B1, 0x0738,
    0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862, 0x9AF9, 0x8B70,
    0x8408, 0x9581, 0xA71A, 0xB693, 0xC22C, 0xD3A5, 0xE13E, 0xF0B7,
    0x0840, 0x19C9, 0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF,
    0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324, 0xF1BF, 0xE036,
    0x18C1, 0x0948, 0x3BD3, 0x2A5A, 0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E,
    0xA50A, 0xB483, 0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5,
    0x2942, 0x38CB, 0x0A50, 0x1BD9, 0x6F66, 0x7EEF, 0x4C74, 0x5DFD,
    0xB58B, 0xA402, 0x9699, 0x8710, 0xF3AF, 0xE226, 0xD0BD, 0xC134,
    0x39C3, 0x284A, 0x1AD1, 0x0B58, 0x7FE7, 0x6E6E, 0x5CF5, 0x4D7C,
    0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1, 0xA33A, 0xB2B3,
    0x4A44, 0x5BCD, 0x6956, 0x78DF, 0x0C60, 0x1DE9, 0x2F72, 0x3EFB,
    0xD68D, 0xC704, 0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232,
    0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68, 0x3FF3, 0x2E7A,
    0xE70E, 0xF687, 0xC41C, 0xD595, 0xA12A, 0xB0A3, 0x8238, 0x93B1,
    0x6B46, 0x7ACF, 0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9,
    0xF78F, 0xE606, 0xD49D, 0xC514, 0xB1AB, 0xA022, 0x92B9, 0x8330,
    0x7BC7, 0x6A4E, 0x58D5, 0x495C, 0x3DE3, 0x2C6A, 0x1EF1, 0x0F78
};

uint16_t
WpanCrc(uint16_t crc, const void *dataPtr, unsigned int dataLen)
{
    const uint8_t *p = dataPtr;
    /*
     * Reflected CRC for dummies:
     *
     * Step 1: Right-Shift the crc register by one byte, save the byte that
     *          pops off as an index to the CRC table.
     * Step 2: XOR the index with the next byte of data.
     * Step 3: XOR the crc register with the table entry given by the index.
     * Step 4: Repeat until out of data.
     *
     * Non-Reflected CRCs use the same algorithm, except that the crc register
     * is shifted left, and the table needs to be regenerated.
     */
    while (dataLen--) {
        crc = (crc >> 8) ^ wpanCrcTable[(crc & 0xff) ^ *p++];
    }
    return crc;
}

/**
  * @brief  This function is used to manage all the callbacks used by the
  *         OpenThread interface. These callbacks are used for example to
  *         notify the application as soon as the state of a device has been
  *         modified.
  *
  *         Important Note: This function must be called each time a message
  *         is sent from the M0 to the M4.
  *
  * @param  None
  * @retval None
  */
HAL_StatusTypeDef
Zigbee_CallBackProcessing(void)
{
    HAL_StatusTypeDef status = HAL_OK;
    struct zb_ipc_m4_cb_info_t *info = NULL;
    Zigbee_Cmd_Request_t *p_notification;
    uint32_t retval = 0;

    /* Get pointer on received event buffer from M0 */
    p_notification = ZIGBEE_Get_NotificationPayloadBuffer();

    switch (p_notification->ID) {
        case MSG_M0TOM4_ZB_DESTROY_CB:
            zb_ipc_globals.zb = NULL;
            assert(p_notification->Size == 1);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[0];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(void *arg);

                callback = (void (*)(void *arg))info->callback;
                callback(info->arg);
            }
            break;

        case MSG_M0TOM4_FILTER_MSG_CB:
        {
            struct zb_msg_filter_cb_info_t *cb_info;
            enum zb_msg_filter_rc filter_rc;

            assert(p_notification->Size == 3);
            cb_info = (struct zb_msg_filter_cb_info_t *)p_notification->Data[2];
            filter_rc = cb_info->callback(zb_ipc_globals.zb, (uint32_t)p_notification->Data[0],
                    (void *)p_notification->Data[1], cb_info->arg);
            retval = (uint32_t)filter_rc;
            break;
        }

#ifndef CONFIG_ZB_M4_ZBTIMER_OVERRIDE
        case MSG_M0TOM4_TIMER_CB:
        {
            struct ZbTimerT *timer;

            assert(p_notification->Size == 1);
            timer = (struct ZbTimerT *)p_notification->Data[0];
            if (timer->callback != NULL) {
                timer->callback(NULL, timer->arg);
            }
            break;
        }
#endif

        case MSG_M0TOM4_STARTUP_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(enum ZbStatusCodeT status, void *arg);

                callback = (void (*)(enum ZbStatusCodeT status, void *arg))info->callback;
                callback((enum ZbStatusCodeT)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_STARTUP_REJOIN_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbNlmeJoinConfT *conf, void *arg);

                callback = (void (*)(struct ZbNlmeJoinConfT *conf, void *arg))info->callback;
                callback((struct ZbNlmeJoinConfT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_STARTUP_PERSIST_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(enum ZbStatusCodeT status, void *arg);

                callback = (void (*)(enum ZbStatusCodeT status, void *arg))info->callback;
                callback((enum ZbStatusCodeT)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_STARTUP_FINDBIND_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(enum ZbStatusCodeT status, void *arg);

                callback = (void (*)(enum ZbStatusCodeT status, void *arg))info->callback;
                callback((enum ZbStatusCodeT)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_STARTUP_TCSO_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(enum ZbTcsoStatusT status, void *arg);

                callback = (void (*)(enum ZbTcsoStatusT status, void *arg))info->callback;
                callback((enum ZbTcsoStatusT)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_STARTUP_TC_REJOIN_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(enum ZbStatusCodeT status, void *arg);

                callback = (void (*)(enum ZbStatusCodeT status, void *arg))info->callback;
                callback((enum ZbStatusCodeT)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_PERSIST_CB:
            if (zb_persist_cb != NULL) {
                zb_persist_cb(zb_ipc_globals.zb, zb_persist_arg);
            }
            break;

        case MSG_M0TOM4_ZB_LEAVE_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbNlmeLeaveConfT *conf, void *arg);

                callback = (void (*)(struct ZbNlmeLeaveConfT *conf, void *arg))info->callback;
                callback((struct ZbNlmeLeaveConfT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZB_STATE_PAUSE_CB:
            assert(p_notification->Size == 1);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[0];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(void *arg);

                callback = (void (*)(void *arg))info->callback;
                callback(info->arg);
            }
            break;

        /* void (*callback)(struct ZbTlGetGroupIdsRspCmd *rsp, void *arg) */
        case MSG_M0TOM4_ZCL_TL_GET_GRP_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbTlGetGroupIdsRspCmd *rsp, void *arg);

                callback = (void (*)(struct ZbTlGetGroupIdsRspCmd *rsp, void *arg))info->callback;
                callback((struct ZbTlGetGroupIdsRspCmd *)p_notification->Data[0], info->arg);
            }
            break;

        /* void (*callback)(struct ZbTlGetEpListRspCmd *rsp, void *arg) */
        case MSG_M0TOM4_ZCL_TL_GET_EPLIST_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbTlGetEpListRspCmd *rsp, void *arg);

                callback = (void (*)(struct ZbTlGetEpListRspCmd *rsp, void *arg))info->callback;
                callback((struct ZbTlGetEpListRspCmd *)p_notification->Data[0], info->arg);
            }
            break;

        /* void (*callback)(struct ZbZclCommandRspT *zcl_rsp, void *arg) */
        case MSG_M0TOM4_ZCL_TL_SEND_EPINFO_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZclCommandRspT *rsp, void *arg);

                callback = (void (*)(struct ZbZclCommandRspT *rsp, void *arg))info->callback;
                callback((struct ZbZclCommandRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_APSDE_DATA_REQ_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbApsdeDataConfT *conf, void *arg);

                callback = (void (*)(struct ZbApsdeDataConfT *conf, void *arg))info->callback;
                callback((struct ZbApsdeDataConfT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_APS_FILTER_ENDPOINT_CB:
        {
            struct ZbApsdeDataIndT *data_ind;
            struct aps_filter_cb_t *aps_filter_cb;
            int err = ZB_APS_FILTER_CONTINUE;

            assert(p_notification->Size == 2);
            data_ind = (struct ZbApsdeDataIndT *)p_notification->Data[0];
            aps_filter_cb = (struct aps_filter_cb_t *)p_notification->Data[1];
            if (aps_filter_cb->callback != NULL) {
                err = aps_filter_cb->callback(data_ind, aps_filter_cb->cb_arg);
            }
            /* Return err in second argument */
            p_notification->Data[1] = (uint32_t)err;
            break;
        }

        case MSG_M0TOM4_APS_FILTER_CLUSTER_CB:
        {
            struct ZbApsdeDataIndT *data_ind;
            struct aps_filter_cb_t *aps_filter_cb;
            int err = ZB_APS_FILTER_CONTINUE;

            assert(p_notification->Size == 2);
            data_ind = (struct ZbApsdeDataIndT *)p_notification->Data[0];
            aps_filter_cb = (struct aps_filter_cb_t *)p_notification->Data[1];
            if (aps_filter_cb->callback != NULL) {
                err = aps_filter_cb->callback(data_ind, aps_filter_cb->cb_arg);
            }
            /* Return err in second argument */
            p_notification->Data[1] = (uint32_t)err;
            break;
        }

        case MSG_M0TOM4_NLME_NET_DISC_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbNlmeNetDiscConfT *conf, void *arg);

                callback = (void (*)(struct ZbNlmeNetDiscConfT *conf, void *arg))info->callback;
                callback((struct ZbNlmeNetDiscConfT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_NLME_LEAVE_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbNlmeLeaveConfT *conf, void *arg);

                callback = (void (*)(struct ZbNlmeLeaveConfT *conf, void *arg))info->callback;
                callback((struct ZbNlmeLeaveConfT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_NLME_SYNC_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbNlmeSyncConfT *conf, void *arg);

                callback = (void (*)(struct ZbNlmeSyncConfT *discConf, void *arg))info->callback;
                callback((struct ZbNlmeSyncConfT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_NLME_ROUTE_DISC_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbNlmeRouteDiscConfT *discConf, void *cbarg);

                callback = (void (*)(struct ZbNlmeRouteDiscConfT *discConf, void *cbarg))info->callback;
                callback((struct ZbNlmeRouteDiscConfT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZDO_DEVICE_ANNCE_FILTER_CB:
        {
            struct zdo_filter_cb_info_t *cb_info;

            assert(p_notification->Size == 3);
            cb_info = (void *)p_notification->Data[2];
            if ((cb_info != NULL) && (cb_info->callback != NULL)) {
                struct ZbZdoDeviceAnnceT *msg;
                uint8_t seqno;
                unsigned int i;
                int (*callback)(struct ZigBeeT *zb, struct ZbZdoDeviceAnnceT *annce, uint8_t seqno, void *arg);

                for (i = 0; i < ZB_IPC_ZDO_FILTER_CB_LIST_MAX; i++) {
                    /* Find the matching filter callback */
                    if (cb_info != &zdo_filter_cb_list[i]) {
                        continue;
                    }
                    if (cb_info->filter == NULL) {
                        /* Shouldn't get here */
                        break;
                    }
                    /* Call the Device Annce callback */
                    msg = (struct ZbZdoDeviceAnnceT *)p_notification->Data[0];
                    seqno = (uint8_t)p_notification->Data[1];
                    callback = (int (*)(struct ZigBeeT *zb, struct ZbZdoDeviceAnnceT *annce, uint8_t seqno, void *arg))cb_info->callback;
                    callback(zb_ipc_globals.zb, msg, seqno, cb_info->arg);
                    break;
                }
            }
            break;
        }

        case MSG_M0TOM4_ZDO_NWK_ADDR_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZdoNwkAddrRspT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZdoNwkAddrRspT *rsp, void *cbarg))info->callback;
                callback((struct ZbZdoNwkAddrRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZDO_IEEE_ADDR_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZdoIeeeAddrRspT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZdoIeeeAddrRspT *rsp, void *cbarg))info->callback;
                callback((struct ZbZdoIeeeAddrRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZDO_NODE_DESC_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZdoNodeDescRspT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZdoNodeDescRspT *rsp, void *cbarg))info->callback;
                callback((struct ZbZdoNodeDescRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZDO_POWER_DESC_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZdoPowerDescRspT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZdoPowerDescRspT *rsp, void *cbarg))info->callback;
                callback((struct ZbZdoPowerDescRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZDO_SIMPLE_DESC_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZdoSimpleDescRspT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZdoSimpleDescRspT *rsp, void *cbarg))info->callback;
                callback((struct ZbZdoSimpleDescRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZDO_ACTIVE_EP_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZdoActiveEpRspT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZdoActiveEpRspT *rsp, void *cbarg))info->callback;
                callback((struct ZbZdoActiveEpRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZDO_MATCH_DESC_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZdoMatchDescRspT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZdoMatchDescRspT *rsp, void *cbarg))info->callback;
                callback((struct ZbZdoMatchDescRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZDO_MATCH_DESC_MULTI_CB:
            /* Note, we're not using zb_ipc_m4_cb_info for this API, so we don't need
             * the callback argument. */
            assert(p_notification->Size == 1);
            if (zdo_match_multi_cb != NULL) {
                struct ZbZdoMatchDescRspT *rsp;

                rsp = (struct ZbZdoMatchDescRspT *)p_notification->Data[0];
                zdo_match_multi_cb(rsp, zdo_match_multi_arg);
                if (rsp->status == ZB_ZDP_STATUS_TIMEOUT) {
                    /* Release the callback */
                    zdo_match_multi_cb = NULL;
                }
            }
            break;

        case MSG_M0TOM4_ZDO_BIND_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZdoBindRspT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZdoBindRspT *rsp, void *cbarg))info->callback;
                callback((struct ZbZdoBindRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZDO_UNBIND_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZdoBindRspT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZdoBindRspT *rsp, void *cbarg))info->callback;
                callback((struct ZbZdoBindRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZDO_MGMT_LQI_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZdoLqiRspT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZdoLqiRspT *rsp, void *cbarg))info->callback;
                callback((struct ZbZdoLqiRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZDO_MGMT_RTG_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZdoRtgRspT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZdoRtgRspT *rsp, void *cbarg))info->callback;
                callback((struct ZbZdoRtgRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZDO_MGMT_BIND_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZdoMgmtBindRspT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZdoMgmtBindRspT *rsp, void *cbarg))info->callback;
                callback((struct ZbZdoMgmtBindRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZDO_MGMT_LEAVE_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZdoLeaveRspT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZdoLeaveRspT *rsp, void *cbarg))info->callback;
                callback((struct ZbZdoLeaveRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZDO_MGMT_PERMIT_JOIN_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZdoPermitJoinRspT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZdoPermitJoinRspT *rsp, void *cbarg))info->callback;
                callback((struct ZbZdoPermitJoinRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZDO_MGMT_NWK_UPDATE_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZdoNwkUpdateNotifyT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZdoNwkUpdateNotifyT *rsp, void *cbarg))info->callback;
                callback((struct ZbZdoNwkUpdateNotifyT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZDO_MGMT_NWK_UPDATE_FILTER_CB:
        {
            struct zdo_filter_cb_info_t *cb_info;

            assert(p_notification->Size == 3);
            cb_info = (void *)p_notification->Data[2];
            if ((cb_info != NULL) && (cb_info->callback != NULL)) {
                struct ZbZdoNwkUpdateNotifyT *msg;
                uint8_t seqno;
                unsigned int i;
                int (*callback)(struct ZigBeeT *zb, struct ZbZdoNwkUpdateNotifyT *msg, uint8_t seqno, void *arg);

                for (i = 0; i < ZB_IPC_ZDO_FILTER_CB_LIST_MAX; i++) {
                    /* Find the matching filter callback */
                    if (cb_info != &zdo_filter_cb_list[i]) {
                        continue;
                    }
                    if (cb_info->filter == NULL) {
                        /* Shouldn't get here */
                        break;
                    }
                    msg = (struct ZbZdoNwkUpdateNotifyT *)p_notification->Data[0];
                    seqno = (uint8_t)p_notification->Data[1];
                    callback = (int (*)(struct ZigBeeT *zb, struct ZbZdoNwkUpdateNotifyT *msg, uint8_t seqno, void *arg))cb_info->callback;
                    callback(zb_ipc_globals.zb, msg, seqno, cb_info->arg);
                    break;
                }
            }
            break;
        }

        case MSG_M0TOM4_ZCL_CLUSTER_DATA_IND:
        {
            struct ZbApsdeDataIndT *dataIndPtr;
            void *cb_arg;
            int err;

            assert(p_notification->Size == 2);
            dataIndPtr = (struct ZbApsdeDataIndT *)p_notification->Data[0];
            cb_arg = (void *)p_notification->Data[1];
            err = zcl_cluster_data_ind(dataIndPtr, cb_arg);
            /* Return err in second argument */
            p_notification->Data[1] = (uint32_t)err;
            break;
        }

        case MSG_M0TOM4_ZCL_CLUSTER_ALARM_CB:
        {
            struct ZbApsdeDataIndT *dataIndPtr;
            void *cb_arg;
            int err;

            assert(p_notification->Size == 2);
            dataIndPtr = (struct ZbApsdeDataIndT *)p_notification->Data[0];
            cb_arg = (void *)p_notification->Data[1];
            err = zcl_cluster_alarm_data_ind(dataIndPtr, cb_arg);
            /* Return err in second argument */
            p_notification->Data[1] = (uint32_t)err;
            break;
        }

        case MSG_M0TOM4_ZCL_CLUSTER_CMD_RSP_CONF_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbApsdeDataConfT *conf, void *arg);

                callback = (void (*)(struct ZbApsdeDataConfT *conf, void *arg))info->callback;
                callback((struct ZbApsdeDataConfT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZCL_COMMAND_REQ_CB:
        {
            int err = ZB_APS_FILTER_CONTINUE;

            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            /* Note: shouldn't get here if callback was NULL in request, so info should
             * always be non-NULL. */
            if (info != NULL) {
                struct ZbZclCommandRspT *zcl_rsp = (struct ZbZclCommandRspT *)p_notification->Data[0];

                if (info->callback != NULL) {
                    int (*callback)(struct ZbZclCommandRspT *conf, void *arg);

                    callback = (int (*)(struct ZbZclCommandRspT *rsp, void *arg))info->callback;
                    err = callback(zcl_rsp, info->arg);
                }
                if (info->zcl_recv_multi_rsp && (zcl_rsp->status != ZCL_STATUS_TIMEOUT)) {
                    /* Don't free the callback yet */
                    info = NULL;
                }
            }
            /* Return err in second argument */
            p_notification->Data[1] = (uint32_t)err;
            break;
        }

        case MSG_M0TOM4_ZCL_READ_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZclReadRspT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZclReadRspT *rsp, void *cbarg))info->callback;
                callback((struct ZbZclReadRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZCL_WRITE_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZclWriteRspT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZclWriteRspT *rsp, void *cbarg))info->callback;
                callback((struct ZbZclWriteRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZCL_DISCOVER_ATTR_CB:
            assert(p_notification->Size == 2);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[1];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(struct ZbZclDiscoverAttrRspT *rsp, void *cbarg);

                callback = (void (*)(struct ZbZclDiscoverAttrRspT *rsp, void *cbarg))info->callback;
                callback((struct ZbZclDiscoverAttrRspT *)p_notification->Data[0], info->arg);
            }
            break;

        case MSG_M0TOM4_ZCL_KE_WITH_DEVICE_CB:
            assert(p_notification->Size == 5);
            info = (struct zb_ipc_m4_cb_info_t *)p_notification->Data[4];
            if ((info != NULL) && (info->callback != NULL)) {
                void (*callback)(uint64_t partnerAddr, uint16_t keSuite, enum ZbZclKeyStatusT key_status, void *arg);
                uint64_t partnerAddr;

                zb_ipc_m4_memcpy2(&partnerAddr, (void*)&p_notification->Data[0], 8);
                callback = (void (*)(uint64_t partnerAddr, uint16_t keSuite, enum ZbZclKeyStatusT key_status, void *arg))info->callback;
                callback(partnerAddr, (uint16_t)p_notification->Data[2], (enum ZbZclKeyStatusT)p_notification->Data[3], info->arg);
            }
            break;

        case MSG_M0TOM4_ZCL_TL_EP_INFO_CB:
        {
            struct ZbTlEpInfoCmd *cmd;
            struct ZbZclAddrInfoT *srcInfo;

            assert(p_notification->Size == 3);
            if (zigbee_m4_tl_callbacks.ep_info_cb == NULL) {
                retval = (uint32_t)ZCL_STATUS_UNSUPP_CLUSTER_COMMAND;
                break;
            }
            cmd = (struct ZbTlEpInfoCmd *)p_notification->Data[0];
            srcInfo = (struct ZbZclAddrInfoT *)p_notification->Data[1];
            retval = (uint32_t)zigbee_m4_tl_callbacks.ep_info_cb(zb_ipc_globals.zb, cmd,
                srcInfo, (void *)p_notification->Data[2]);
            break;
        }

        default:
            status = HAL_ERROR;
            break;
    }

    if (info != NULL) {
        zb_ipc_m4_cb_info_free(info);
    }

    /* Return the retval, if any. */
    p_notification->Data[0] = retval;

    TL_ZIGBEE_SendM4AckToM0Notify();
    return status;
}

HAL_StatusTypeDef
Zigbee_M0RequestProcessing(void)
{
    HAL_StatusTypeDef status = HAL_OK;
    Zigbee_Cmd_Request_t *p_logging = ZIGBEE_Get_M0RequestPayloadBuffer();

    switch (p_logging->ID) {
        case MSG_M0TOM4_ZB_LOGGING:
        {
            const char *log_str;

            assert(p_logging->Size == 1);
            log_str = (const char *)p_logging->Data[0];
            if (zb_ipc_globals.log_cb != NULL) {
                /* We just need to print the raw string. The formatting has already been done. */
                zb_ipc_globals.log_cb(zb_ipc_globals.zb, 0 /* mask is unknown */, NULL,
                    log_str /* fmt */, va_null);
            }
            break;
        }

        case MSG_M0TOM4_ZB_MALLOC:
        {
            void *ptr;
            uint32_t alloc_sz;

            assert(p_logging->Size == 1);
            alloc_sz = (uint32_t)p_logging->Data[0];
            ptr = malloc(alloc_sz);
            if (ptr != NULL) {
                *(uint32_t *)ptr = alloc_sz;
                ptr = ((uint8_t *)ptr) + 4U;
                zb_ipc_globals.zb_alloc_sz += alloc_sz;
            }
            /* Return ptr in second argument */
            p_logging->Data[1] = (uint32_t)ptr;
            break;
        }

        case MSG_M0TOM4_ZB_FREE:
        {
            void *ptr;
            uint32_t alloc_sz;

            assert(p_logging->Size == 1);
            ptr = (void *)p_logging->Data[0];
            assert(ptr != NULL);
            alloc_sz = *(uint32_t *)ptr;
            /* Sanity check */
            if (alloc_sz <= zb_ipc_globals.zb_alloc_sz) {
                zb_ipc_globals.zb_alloc_sz -= alloc_sz;
            }
            else {
                zb_ipc_globals.zb_alloc_sz = 0;
            }
            ptr = ((uint8_t *)ptr) - 4U;
            free(ptr);
            break;
        }

        default:
            status = HAL_ERROR;
            break;
    }

    TL_ZIGBEE_SendM4AckToM0Request();
    return status;
}

/* ZbMalloc (MSG_M0TOM4_ZB_MALLOC) Debugging */
unsigned int
zb_malloc_current_sz(void)
{
    return zb_ipc_globals.zb_alloc_sz;
}

/* This is only for ZB_LOG_MASK_ZCL log messages from M4 */
void
ZbLogPrintf(struct ZigBeeT *zb, uint32_t mask, const char *hdr, const char *fmt, ...)
{
    /* ZB_LOG_MASK_ZCL */
    if ((zb_ipc_globals.log_cb != NULL) && ((mask & zb_ipc_globals.log_mask) != 0U)) {
        va_list argptr;

        va_start(argptr, fmt);
        zb_ipc_globals.log_cb(zb, mask, hdr, fmt, argptr);
        va_end(argptr);
    }
}