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

app_thread.c « App « STM32_WPAN « Thread_Coap_DataTransfer « Thread « Applications « P-NUCLEO-WB55.Nucleo « Projects - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f13715c70cd39fbd3b25813baa3b19d775cd18e2 (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
/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * File Name          : App/app_thread.c
 * Description        : Thread Application.
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under Ultimate Liberty license
 * SLA0044, the "License"; You may not use this file except in compliance with
 * the License. You may obtain a copy of the License at:
 *                             www.st.com/SLA0044
 *
 ******************************************************************************
 */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "app_common.h"
#include "utilities_common.h"
#include "app_entry.h"
#include "dbg_trace.h"
#include "app_thread.h"
#include "stm32wbxx_core_interface_def.h"
#include "openthread_api_wb.h"
#include "shci.h"
#include "stm_logging.h"
#include "app_conf.h"
#include "stm32_lpm.h"
#include "stm32_seq.h"
#if (CFG_USB_INTERFACE_ENABLE != 0)
#include "vcp.h"
#include "vcp_conf.h"
#endif /* (CFG_USB_INTERFACE_ENABLE != 0) */

/* Private includes -----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "data_transfer.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */
/* Private defines -----------------------------------------------------------*/
#define C_SIZE_CMD_STRING     256U
#define C_PANID               0x2226U
#define C_CHANNEL_NB          19U

/* USER CODE BEGIN PD */
#define C_RESSOURCE_DATA_TRANSFER   "dataTransfer"
#define C_RESSOURCE_Provisioning    "provisioning"
/* USER CODE END PD */

/* Private macros ------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private function prototypes -----------------------------------------------*/
static void APP_THREAD_CheckWirelessFirmwareInfo(void);
static void APP_THREAD_DeviceConfig(void);
static void APP_THREAD_StateNotif(uint32_t NotifFlags, void *pContext);
static void APP_THREAD_TraceError(const char * pMess, uint32_t ErrCode);

static void Send_CLI_To_M0(void);
static void Send_CLI_Ack_For_OT(void);
static void HostTxCb( void );
static void Wait_Getting_Ack_From_M0(void);
static void Receive_Ack_From_M0(void);
static void Receive_Notification_From_M0(void);
#if (CFG_HW_LPUART1_ENABLED == 1)
extern void MX_LPUART1_UART_Init(void);
#endif
#if (CFG_HW_USART1_ENABLED == 1)
extern void MX_USART1_UART_Init(void);
#endif
#if (CFG_USB_INTERFACE_ENABLE != 0)
static uint32_t ProcessCmdString(uint8_t* buf , uint32_t len);
#else
static void RxCpltCallback(void);
#endif

/* USER CODE BEGIN PFP */
static void APP_THREAD_CheckMsgValidity(void);
static void APP_THREAD_SendNextBuffer(void);
static void APP_THREAD_DummyReqHandler(void * p_context,
    otCoapHeader * pHeader,
    otMessage * pMessage,
    const otMessageInfo * pMessageInfo);
static void APP_THREAD_CoapDataReqHandler(otCoapHeader * pHeader,
    otMessage * pMessage,
    const otMessageInfo * pMessageInfo);
static void APP_THREAD_SendDataResponse(otCoapHeader * pRequestHeader,
    const otMessageInfo * pMessageInfo);
static void APP_THREAD_ProvisioningReqHandler(otCoapHeader * pHeader,
    otMessage * pMessage,
    const otMessageInfo * pMessageInfo);
static otError APP_THREAD_ProvisioningRespSend(otCoapHeader* pRequestHeader,
    const otMessageInfo * pMessageInfo);
static void APP_THREAD_ProvisioningReqSend(void);
static void APP_THREAD_ProvisioningRespHandler(otCoapHeader * pHeader,
    otMessage * pMessage,
    const otMessageInfo * pMessageInfo,
    otError Result);
static void APP_THREAD_SendCoapUnicastRequest(void);
static void APP_THREAD_DataRespHandler(otCoapHeader  * pHeader,
    otMessage * pMessage,
    const otMessageInfo * pMessageInfo,
    otError Result);
static void APP_THREAD_DummyRespHandler(void * p_context,
    otCoapHeader * pHeader,
    otMessage * pMessage,
    const otMessageInfo * pMessageInfo,
    otError Result);
static void APP_THREAD_AskProvisioning(void);
/* USER CODE END PFP */

/* Private variables -----------------------------------------------*/
#if (CFG_USB_INTERFACE_ENABLE != 0)
  static uint8_t TmpString[C_SIZE_CMD_STRING];
static uint8_t VcpRxBuffer[sizeof(TL_CmdSerial_t)];        /* Received Data over USB are stored in this buffer */
static uint8_t VcpTxBuffer[sizeof(TL_EvtPacket_t) + 254U]; /* Transmit buffer over USB */
#else
static uint8_t aRxBuffer[C_SIZE_CMD_STRING];
#endif /* (CFG_USB_INTERFACE_ENABLE != 0) */

static uint8_t CommandString[C_SIZE_CMD_STRING];
static __IO uint16_t indexReceiveChar = 0;
static __IO uint16_t CptReceiveCmdFromUser = 0;

static TL_CmdPacket_t *p_thread_otcmdbuffer;
static TL_EvtPacket_t *p_thread_notif_M0_to_M4;
static __IO uint32_t  CptReceiveMsgFromM0 = 0;
PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static TL_TH_Config_t ThreadConfigBuffer;
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t ThreadOtCmdBuffer;
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t ThreadNotifRspEvtBuffer[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255U];
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t ThreadCliCmdBuffer;

/* USER CODE BEGIN PV */
static otCoapResource OT_RessourceDataTransfer = {C_RESSOURCE_DATA_TRANSFER, APP_THREAD_DummyReqHandler, (void*)APP_THREAD_CoapDataReqHandler, NULL};
static otCoapResource OT_RessourceProvisionning = {C_RESSOURCE_Provisioning, APP_THREAD_DummyReqHandler, (void*)APP_THREAD_ProvisioningReqHandler, NULL};
static otMessageInfo OT_MessageInfo = {0};
static otCoapHeader  OT_Header = {0};
static uint8_t OT_Command = 0;
static uint16_t OT_BufferIdRead = 1U;
static uint16_t OT_BufferIdSend = 1U;
static uint8_t OT_BufferSend[COAP_PAYLOAD_MAX_LENGTH] = {0};
static uint8_t OT_BufferReceived[COAP_PAYLOAD_MAX_LENGTH] = {0};
static otMessage   * pOT_Message = NULL;
static otIp6Address   OT_PeerAddress = { .mFields.m8 = { 0 } };
/* USER CODE END PV */

/* Functions Definition ------------------------------------------------------*/

void APP_THREAD_Init( void )
{
  /* USER CODE BEGIN APP_THREAD_INIT_1 */

  /* USER CODE END APP_THREAD_INIT_1 */

  SHCI_CmdStatus_t ThreadInitStatus;

  /* Check the compatibility with the Coprocessor Wireless Firmware loaded */
  APP_THREAD_CheckWirelessFirmwareInfo();

#if (CFG_USB_INTERFACE_ENABLE != 0)
  VCP_Init(&VcpTxBuffer[0], &VcpRxBuffer[0]);
#endif /* (CFG_USB_INTERFACE_ENABLE != 0) */

  /* Register cmdbuffer */
  APP_THREAD_RegisterCmdBuffer(&ThreadOtCmdBuffer);

  /**
   * Do not allow standby in the application
   */
  UTIL_LPM_SetOffMode(1 << CFG_LPM_APP_THREAD, UTIL_LPM_DISABLE);

  /* Init config buffer and call TL_THREAD_Init */
  APP_THREAD_TL_THREAD_INIT();

  /* Configure UART for sending CLI command from M4 */
  APP_THREAD_Init_UART_CLI();

  /* Send Thread start system cmd to M0 */
  ThreadInitStatus = SHCI_C2_THREAD_Init();

  /* Prevent unused argument(s) compilation warning */
  UNUSED(ThreadInitStatus);

  /* Register task */
  /* Create the different tasks */
  UTIL_SEQ_RegTask( 1<<(uint32_t)CFG_TASK_MSG_FROM_M0_TO_M4, UTIL_SEQ_RFU, APP_THREAD_ProcessMsgM0ToM4);
  
  /* USER CODE BEGIN INIT TASKS */
  UTIL_SEQ_RegTask( 1<<(uint32_t)CFG_TASK_SEND_BUFFER, UTIL_SEQ_RFU, APP_THREAD_SendNextBuffer);
  UTIL_SEQ_RegTask( 1<<(uint32_t)CFG_TASK_PROVISIONING, UTIL_SEQ_RFU, APP_THREAD_AskProvisioning);
  /* USER CODE END INIT TASKS */

  /* Initialize and configure the Thread device*/
  APP_THREAD_DeviceConfig();

  /* USER CODE BEGIN APP_THREAD_INIT_2 */
  /* USER CODE END APP_THREAD_INIT_2 */
}

/**
 * @brief  Trace the error or the warning reported.
 * @param  ErrId :
 * @param  ErrCode
 * @retval None
 */
void APP_THREAD_Error(uint32_t ErrId, uint32_t ErrCode)
{
  /* USER CODE BEGIN APP_THREAD_Error_1 */

  /* USER CODE END APP_THREAD_Error_1 */
  switch(ErrId)
  {
  case ERR_REC_MULTI_MSG_FROM_M0 :
    APP_THREAD_TraceError("ERROR : ERR_REC_MULTI_MSG_FROM_M0 ", ErrCode);
    break;
  case ERR_THREAD_SET_STATE_CB :
    APP_THREAD_TraceError("ERROR : ERR_THREAD_SET_STATE_CB ",ErrCode);
    break;
  case ERR_THREAD_SET_CHANNEL :
    APP_THREAD_TraceError("ERROR : ERR_THREAD_SET_CHANNEL ",ErrCode);
    break;
  case ERR_THREAD_SET_PANID :
    APP_THREAD_TraceError("ERROR : ERR_THREAD_SET_PANID ",ErrCode);
    break;
  case ERR_THREAD_IPV6_ENABLE :
    APP_THREAD_TraceError("ERROR : ERR_THREAD_IPV6_ENABLE ",ErrCode);
    break;
  case ERR_THREAD_START :
    APP_THREAD_TraceError("ERROR: ERR_THREAD_START ", ErrCode);
    break;
  case ERR_THREAD_ERASE_PERSISTENT_INFO :
    APP_THREAD_TraceError("ERROR : ERR_THREAD_ERASE_PERSISTENT_INFO ",ErrCode);
    break;
  case ERR_THREAD_CHECK_WIRELESS :
    APP_THREAD_TraceError("ERROR : ERR_THREAD_CHECK_WIRELESS ",ErrCode);
    break;
  /* USER CODE BEGIN APP_THREAD_Error_2 */
  case ERR_THREAD_COAP_START :
    APP_THREAD_TraceError("ERROR : ERR_THREAD_COAP_START ",ErrCode);
    break;
  case ERR_THREAD_COAP_ADD_RESSOURCE :
    APP_THREAD_TraceError("ERROR : ERR_THREAD_COAP_ADD_RESSOURCE ",ErrCode);
    break;
  case ERR_THREAD_MESSAGE_READ :
    APP_THREAD_TraceError("ERROR : ERR_THREAD_MESSAGE_READ ",ErrCode);
    break;
  case ERR_THREAD_COAP_SEND_RESPONSE :
    APP_THREAD_TraceError("ERROR : ERR_THREAD_COAP_SEND_RESPONSE ",ErrCode);
    break;
  case ERR_THREAD_COAP_APPEND :
    APP_THREAD_TraceError("ERROR : ERR_THREAD_COAP_APPEND ",ErrCode);
    break;
  case ERR_THREAD_COAP_SEND_REQUEST :
    APP_THREAD_TraceError("ERROR : ERR_THREAD_COAP_SEND_REQUEST ",ErrCode);
    break;
  case ERR_THREAD_SETUP :
    APP_THREAD_TraceError("ERROR : ERR_THREAD_SETUP ", ErrCode);
    break;
  case ERR_THREAD_LINK_MODE :
    APP_THREAD_TraceError("ERROR : ERR_THREAD_LINK_MODE ", ErrCode);
    break;
  case ERR_ALLOC_MSG :
    APP_THREAD_TraceError("ERROR : ERR_ALLOC_MSG ", ErrCode);
    break;
  case ERR_FILE_RESP_HANDLER :
    APP_THREAD_TraceError("ERROR : ERR_FILE_RESP_HANDLER ", ErrCode);
    break;
  case ERR_MSG_COMPARE_FAILED :
    APP_THREAD_TraceError("ERROR : ERR_MSG_COMPARE_FAILED ", ErrCode);
    break;
  case ERR_NEW_MSG_ALLOC:
    APP_THREAD_TraceError("ERROR : ERR_NEW_MSG_ALLOC ", ErrCode);
    break;
  case ERR_PROVISIONING_RESP:
    APP_THREAD_TraceError("ERROR : ERR_PROVISIONING_RESP ", ErrCode);
    break;
  case ERR_THREAD_DATA_RESPONSE:
    APP_THREAD_TraceError("ERROR : ERR_THREAD_DATA_RESPONSE ", ErrCode);
    break;
  case ERR_APPEND:
    APP_THREAD_TraceError("ERROR : ERR_APPEND ", ErrCode);
    break;
  case ERR_HEADER_INIT:
    APP_THREAD_TraceError("ERROR : ERR_HEADER_INIT ", ErrCode);
    break;
  case ERR_TOKEN:
    APP_THREAD_TraceError("ERROR : ERR_TOKEN ", ErrCode);
    break;
  /* USER CODE END APP_THREAD_Error_2 */
  default :
    APP_THREAD_TraceError("ERROR Unknown ", 0);
    break;
  }
}


/*************************************************************
 *
 * LOCAL FUNCTIONS
 *
 *************************************************************/

/**
 * @brief Thread initialization.
 * @param  None
 * @retval None
 */
static void APP_THREAD_DeviceConfig(void)
{
  otError error;
  error = otInstanceErasePersistentInfo(NULL);
  if (error != OT_ERROR_NONE)
  {
    APP_THREAD_Error(ERR_THREAD_ERASE_PERSISTENT_INFO,error);
  }
  otInstanceFinalize(NULL);
  otInstanceInitSingle();
  error = otSetStateChangedCallback(NULL, APP_THREAD_StateNotif, NULL);
  if (error != OT_ERROR_NONE)
  {
    APP_THREAD_Error(ERR_THREAD_SET_STATE_CB,error);
  }
  error = otLinkSetChannel(NULL, C_CHANNEL_NB);
  if (error != OT_ERROR_NONE)
  {
    APP_THREAD_Error(ERR_THREAD_SET_CHANNEL,error);
  }
  error = otLinkSetPanId(NULL, C_PANID);
  if (error != OT_ERROR_NONE)
  {
    APP_THREAD_Error(ERR_THREAD_SET_PANID,error);
  }
  error = otIp6SetEnabled(NULL, true);
  if (error != OT_ERROR_NONE)
  {
    APP_THREAD_Error(ERR_THREAD_IPV6_ENABLE,error);
  }
  error = otThreadSetEnabled(NULL, true);
  if (error != OT_ERROR_NONE)
  {
    APP_THREAD_Error(ERR_THREAD_START,error);
  }

  /* USER CODE BEGIN DEVICECONFIG */
  /* Start the COAP server */
  error = otCoapStart(NULL, OT_DEFAULT_COAP_PORT);
  if (error != OT_ERROR_NONE)
  {
    APP_THREAD_Error(ERR_THREAD_COAP_START,error);
  }
  /* Add COAP resources */
  error = otCoapAddResource(NULL, &OT_RessourceDataTransfer);
  if (error != OT_ERROR_NONE)
  {
    APP_THREAD_Error(ERR_THREAD_COAP_ADD_RESSOURCE,error);
  }
  /* Add APP_THREAD_AskProvisioning resources */
  error = otCoapAddResource(NULL, &OT_RessourceProvisionning);
  if (error != OT_ERROR_NONE)
  {
    APP_THREAD_Error(ERR_THREAD_COAP_ADD_RESSOURCE,error);
  }
  /* USER CODE END DEVICECONFIG */
}


/**
 * @brief Thread notification when the state changes.
 * @param  aFlags  : Define the item that has been modified
 *     aContext: Context
 *
 * @retval None
 */
static void APP_THREAD_StateNotif(uint32_t NotifFlags, void *pContext)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(pContext);

  /* USER CODE BEGIN APP_THREAD_STATENOTIF */
  static uint32_t provisioning = 0;
  /* USER CODE END APP_THREAD_STATENOTIF */

  if ((NotifFlags & (uint32_t)OT_CHANGED_THREAD_ROLE) == (uint32_t)OT_CHANGED_THREAD_ROLE)
  {
    switch (otThreadGetDeviceRole(NULL))
    {
    case OT_DEVICE_ROLE_DISABLED:
      /* USER CODE BEGIN OT_DEVICE_ROLE_DISABLED */
      BSP_LED_Off(LED2);
      BSP_LED_Off(LED3);
      /* USER CODE END OT_DEVICE_ROLE_DISABLED */
      break;
    case OT_DEVICE_ROLE_DETACHED:
      /* USER CODE BEGIN OT_DEVICE_ROLE_DETACHED */
      BSP_LED_Off(LED2);
      BSP_LED_Off(LED3);
      /* USER CODE END OT_DEVICE_ROLE_DETACHED */
      break;
    case OT_DEVICE_ROLE_CHILD:
      /* USER CODE BEGIN OT_DEVICE_ROLE_CHILD */
      BSP_LED_Off(LED2);
      BSP_LED_On(LED3);
      if (provisioning == 0)
      {
        HAL_Delay(3000U);
        UTIL_SEQ_SetTask(TASK_PROVISIONING, CFG_SCH_PRIO_1);
      }
      provisioning = 1U;
	  /* USER CODE END OT_DEVICE_ROLE_CHILD */
      break;
    case OT_DEVICE_ROLE_ROUTER :
      /* USER CODE BEGIN OT_DEVICE_ROLE_ROUTER */
      BSP_LED_Off(LED2);
      BSP_LED_On(LED3);
      /* USER CODE END OT_DEVICE_ROLE_ROUTER */
      break;
    case OT_DEVICE_ROLE_LEADER :
      /* USER CODE BEGIN OT_DEVICE_ROLE_LEADER */
      BSP_LED_On(LED2);
      BSP_LED_Off(LED3);
      /* USER CODE END OT_DEVICE_ROLE_LEADER */
      break;
    default:
      /* USER CODE BEGIN DEFAULT */
      BSP_LED_Off(LED2);
      BSP_LED_Off(LED3);
      /* USER CODE END DEFAULT */
      break;
    }
  }
}

/**
 * @brief  Warn the user that an error has occurred.In this case,
 *         the LEDs on the Board will start blinking.
 *
 * @param  pMess  : Message associated to the error.
 * @param  ErrCode: Error code associated to the module (OpenThread or other module if any)
 * @retval None
 */
static void APP_THREAD_TraceError(const char * pMess, uint32_t ErrCode)
{
  /* USER CODE BEGIN TRACE_ERROR */
  APP_DBG("**** Fatal error = %s (Err = %d)", pMess, ErrCode);
  while(1U == 1U)
  {
    BSP_LED_Toggle(LED1);
    HAL_Delay(500U);
    BSP_LED_Toggle(LED2);
    HAL_Delay(500U);
    BSP_LED_Toggle(LED3);
    HAL_Delay(500U);
  }
  /* USER CODE END TRACE_ERROR */
}

/**
 * @brief Check if the Coprocessor Wireless Firmware loaded supports Thread
 *        and display associated informations
 * @param  None
 * @retval None
 */
static void APP_THREAD_CheckWirelessFirmwareInfo(void)
{
  WirelessFwInfo_t wireless_info_instance;
  WirelessFwInfo_t* p_wireless_info = &wireless_info_instance;

  if (SHCI_GetWirelessFwInfo(p_wireless_info) != SHCI_Success)
  {
    APP_THREAD_Error((uint32_t)ERR_THREAD_CHECK_WIRELESS, (uint32_t)ERR_INTERFACE_FATAL);
  }
  else
  {
    APP_DBG("**********************************************************");
    APP_DBG("WIRELESS COPROCESSOR FW:");
    /* Print version */
    APP_DBG("VERSION ID = %d.%d.%d", p_wireless_info->VersionMajor, p_wireless_info->VersionMinor, p_wireless_info->VersionSub);

    switch(p_wireless_info->StackType)
    {
    case INFO_STACK_TYPE_THREAD_FTD :
      APP_DBG("FW Type : Thread FTD");
      break;
    case INFO_STACK_TYPE_THREAD_MTD :
      APP_DBG("FW Type : Thread MTD");
      break;
    case INFO_STACK_TYPE_BLE_THREAD_FTD_STATIC :
      APP_DBG("FW Type : Static Concurrent Mode BLE/Thread");
      break;
    default :
      /* No Thread device supported ! */
      APP_THREAD_Error((uint32_t)ERR_THREAD_CHECK_WIRELESS, (uint32_t)ERR_INTERFACE_FATAL);
      break;
    }
    APP_DBG("**********************************************************");
  }
}

/* USER CODE BEGIN FD_LOCAL_FUNCTIONS */
/**
 * @brief  This function is used to compare the message received versus
 *     the original message.
 * @param  None
 * @retval None
 */
static void APP_THREAD_CheckMsgValidity(void)
{
  uint16_t mMsgId=OT_BufferIdRead - 1U; /* Corrected MsgId (MsgId starts at 1, not at 0) */
  uint32_t mOffset = COAP_PAYLOAD_MAX_LENGTH*mMsgId; /* the starting offset */
  uint32_t i;
  bool valid =true;

  for(i = mOffset; i < mOffset+COAP_PAYLOAD_MAX_LENGTH; i++)
  {
    if(OT_BufferReceived[i-mOffset] != aDataBuffer[i])
    {
      valid =false;
    }
  }

  if(valid == false)
    APP_THREAD_Error(ERR_MSG_COMPARE_FAILED, 0);
}

/**
 * @brief  This function compute the next message to be send
 * @param  None
 * @retval None
 */
static void APP_THREAD_SendNextBuffer(void)
{
  uint16_t j;
  uint16_t mOffset;

  if (OT_BufferIdSend < 5U)
  {
    /* Prepare next buffers to be send */
    OT_BufferIdSend++;
    mOffset=(OT_BufferIdSend - 1U) * COAP_PAYLOAD_MAX_LENGTH;

    memset(OT_BufferSend, 0, COAP_PAYLOAD_MAX_LENGTH);
    for(j = mOffset; j < mOffset + COAP_PAYLOAD_MAX_LENGTH; j++)
    {
      OT_BufferSend[j - mOffset] = aDataBuffer[j];
    }

    /* Send the data in unicast mode */
    APP_THREAD_SendCoapUnicastRequest();
  }
  else
  {
    /* Buffer transfer has been successfully  transfered */
    BSP_LED_On(LED1);
    APP_DBG(" ********* BUFFER HAS BEEN TRANFERED \r\n");
  }
}

/**
 * @brief Dummy request handler
 *
 * @param None
 * @retval None
 */
static void APP_THREAD_DummyReqHandler(void        * p_context,
    otCoapHeader    * pHeader,
    otMessage       * pMessage,
    const otMessageInfo * pMessageInfo)
{
}

/**
 * @brief Data request handler triggered at the reception of the COAP message
 * @param pHeader header pointer
 * @param pMessage message pointer
 * @param pMessageInfo message info pointer
 * @retval None
 */
static void APP_THREAD_CoapDataReqHandler(otCoapHeader    * pHeader,
    otMessage       * pMessage,
    const otMessageInfo * pMessageInfo)
{
  do
  {
    APP_DBG(" ********* APP_THREAD_CoapDataReqHandler \r\n");
    if (otCoapHeaderGetType(pHeader) != OT_COAP_TYPE_CONFIRMABLE &&
        otCoapHeaderGetType(pHeader) != OT_COAP_TYPE_NON_CONFIRMABLE)
    {
      APP_THREAD_Error(ERR_THREAD_COAP_ADD_RESSOURCE, 0);
      break;
    }

    if (otCoapHeaderGetCode(pHeader) != OT_COAP_CODE_PUT)
    {
      APP_THREAD_Error(ERR_THREAD_COAP_ADD_RESSOURCE, 0);
      break;
    }

    if (otMessageRead(pMessage, otMessageGetOffset(pMessage), &OT_BufferReceived, sizeof(OT_BufferReceived)) != sizeof(OT_BufferReceived))
    {
      APP_THREAD_Error(ERR_THREAD_MESSAGE_READ, 0);
    }
    OT_BufferIdRead=otCoapHeaderGetMessageId(pHeader);

    APP_THREAD_CheckMsgValidity();

    if (otCoapHeaderGetType(pHeader) == OT_COAP_TYPE_CONFIRMABLE)
    {
      APP_THREAD_SendDataResponse(pHeader, pMessageInfo);
    }
  } while (false);
}

/**
 * @brief This function acknowledge the data reception by sending an ACK
 *    back to the sender.
 * @param  pRequestHeader coap header
 * @param  pMessageInfo message info pointer
 * @retval None
 */
static void APP_THREAD_SendDataResponse(otCoapHeader    * pRequestHeader,
    const otMessageInfo * pMessageInfo)
{
  otError  error = OT_ERROR_NONE;

  APP_DBG(" ********* APP_THREAD_SendDataResponse \r\n");
  otCoapHeaderInit(&OT_Header, OT_COAP_TYPE_ACKNOWLEDGMENT, OT_COAP_CODE_CHANGED);
  otCoapHeaderSetMessageId(&OT_Header, otCoapHeaderGetMessageId(pRequestHeader));
  otCoapHeaderSetToken(&OT_Header,
      otCoapHeaderGetToken(pRequestHeader),
      otCoapHeaderGetTokenLength(pRequestHeader));

  pOT_Message = otCoapNewMessage(NULL, &OT_Header);
  if (pOT_Message == NULL)
  {
    APP_THREAD_Error(ERR_NEW_MSG_ALLOC,error);
  }
  error = otCoapSendResponse(NULL, pOT_Message, pMessageInfo);
  if (error != OT_ERROR_NONE && pOT_Message != NULL)
  {
    otMessageFree(pOT_Message);
    APP_THREAD_Error(ERR_THREAD_DATA_RESPONSE,error);
  }
}



/**
 * @brief This function is used to handle the APP_THREAD_AskProvisioning handler
 *
 * @param pHeader header pointer
 * @param pMessage message pointer
 * @param pMessageInfo message info pointer
 * @retval None
 */
static void APP_THREAD_ProvisioningReqHandler(otCoapHeader    * pHeader,
    otMessage       * pMessage,
    const otMessageInfo * pMessageInfo)
{
  (void)pMessage;

  APP_DBG("**** Leader receives APP_THREAD_AskProvisioning_request *****\n\r");
  if (otCoapHeaderGetType(pHeader) == OT_COAP_TYPE_NON_CONFIRMABLE &&
      otCoapHeaderGetCode(pHeader) == OT_COAP_CODE_GET)
  {
    OT_MessageInfo = *pMessageInfo;
    memset(&OT_MessageInfo.mSockAddr, 0, sizeof(OT_MessageInfo.mSockAddr));
    if (APP_THREAD_ProvisioningRespSend(pHeader, &OT_MessageInfo) != OT_ERROR_NONE)
    {
      APP_THREAD_Error(ERR_PROVISIONING_RESP, 0);
    }
  }
}

/**
 * @brief This function is used to handle the APP_THREAD_AskProvisioning response
 *
 * @param pRequestHeader
 * @param pMessageInfo message info pointer
 * @retval error code
 */
static otError APP_THREAD_ProvisioningRespSend(otCoapHeader    * pRequestHeader,
    const otMessageInfo * pMessageInfo)
{
  otError  error = OT_ERROR_NO_BUFS;

  do
  {
    otCoapHeaderInit(&OT_Header, OT_COAP_TYPE_NON_CONFIRMABLE, OT_COAP_CODE_CONTENT);
    otCoapHeaderSetToken(&OT_Header,
        otCoapHeaderGetToken(pRequestHeader),
        otCoapHeaderGetTokenLength(pRequestHeader));
    otCoapHeaderSetPayloadMarker(&OT_Header);

    pOT_Message = otCoapNewMessage(NULL, &OT_Header);
    if (pOT_Message == NULL)
    {
      APP_THREAD_Error(ERR_ALLOC_MSG, error);
    }

    error = otMessageAppend(pOT_Message, &OT_Command, sizeof(OT_Command));
    if (error != OT_ERROR_NONE)
    {
      APP_THREAD_Error(ERR_APPEND, error);
    }

    error = otMessageAppend(pOT_Message, otThreadGetMeshLocalEid(NULL), sizeof(otIp6Address));
    if (error != OT_ERROR_NONE)
    {
      break;
    }
    APP_DBG("**** 2) APP_THREAD_ProvisioningRespSend *****\n\r");
    error = otCoapSendResponse(NULL, pOT_Message, pMessageInfo);

    if (error != OT_ERROR_NONE && pOT_Message != NULL)
    {
      otMessageFree(pOT_Message);
      APP_THREAD_Error(ERR_THREAD_COAP_SEND_RESP, error);
    }
  } while (false);

  return error;
}

/**
 * @brief This function is used to manage the APP_THREAD_AskProvisioning request
 *
 * @param None
 * @retval None
 */
static void APP_THREAD_ProvisioningReqSend()
{
  otError   error = OT_ERROR_NONE;

  do
  {
    otCoapHeaderInit(&OT_Header, OT_COAP_TYPE_NON_CONFIRMABLE, OT_COAP_CODE_GET);

    otCoapHeaderGenerateToken(&OT_Header, 2U);

    error = otCoapHeaderAppendUriPathOptions(&OT_Header, C_RESSOURCE_Provisioning);
    if (error != OT_ERROR_NONE)
    {
      APP_THREAD_Error(ERR_APEND_URI,error);
    }

    pOT_Message = otCoapNewMessage(NULL, &OT_Header);
    if (pOT_Message == NULL)
    {
      APP_THREAD_Error(ERR_NEW_MSG_ALLOC,error);
    }

    memset(&OT_MessageInfo, 0, sizeof(OT_MessageInfo));
    OT_MessageInfo.mInterfaceId = OT_NETIF_INTERFACE_ID_THREAD;
    OT_MessageInfo.mPeerPort = OT_DEFAULT_COAP_PORT;
    otIp6AddressFromString("FF03::1", &OT_MessageInfo.mPeerAddr);

    APP_DBG("**** 1) APP_THREAD_ProvisioningReqSend *****\n\r");
    error = otCoapSendRequest(NULL,
        pOT_Message,
        &OT_MessageInfo,
        &APP_THREAD_DummyRespHandler,
        (void*)&APP_THREAD_ProvisioningRespHandler);
  } while (false);

  if (error != OT_ERROR_NONE && pOT_Message != NULL)
  {
    otMessageFree(pOT_Message);
  }
}

/**
 * @brief This function is used to manage the APP_THREAD_AskProvisioning response
 *    handler.
 *
 * @param pHeader  header
 * @param pMessage message pointer
 * @param pMessageInfo message info pointer
 * @param Result error code if any
 * @retval None
 */
static void APP_THREAD_ProvisioningRespHandler(otCoapHeader        * pHeader,
    otMessage           * pMessage,
    const otMessageInfo * pMessageInfo,
    otError             Result)
{
  (void)pHeader;
  if (Result == OT_ERROR_NONE)
  {
    if ((otMessageRead(pMessage, otMessageGetOffset(pMessage), &OT_Command, sizeof(OT_Command)) == sizeof(OT_Command)))
    {
      /* Retrieve the */
      if (otMessageRead(pMessage,
          otMessageGetOffset(pMessage) + sizeof(OT_Command),
          &OT_PeerAddress,
          sizeof(OT_PeerAddress)) != sizeof(OT_PeerAddress))
      {
        APP_THREAD_Error(ERR_READ, 0);
      }
      APP_DBG("**** 3) APP_THREAD_ProvisioningRespHandler *****");
      /* Ask to start the first transfer */
      UTIL_SEQ_SetTask(TASK_SEND_BUFFER, CFG_SCH_PRIO_1);
    }
  }
  else
  {
    APP_DBG("**** 3)APP_THREAD_AskProvisioning failed ***** with ERROR code = %d\n\r",Result);
  }
}


/**
 * @brief This function initiates the APP_THREAD_AskProvisioning phase
 *
 * @param None
 * @retval None
 */
static void APP_THREAD_AskProvisioning(void)
{
  HAL_Delay(1000U);
  APP_THREAD_ProvisioningReqSend();
}
/**
 * @brief This function is used to send the data via a Coap message in
 *    confirmable mode.
 *
 * @param None
 * @retval None
 */
static void APP_THREAD_SendCoapUnicastRequest()
{
  otError   error = OT_ERROR_NONE;

  otCoapHeaderInit(&OT_Header, OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_PUT);
  otCoapHeaderSetMessageId(&OT_Header,OT_BufferIdSend);
  otCoapHeaderGenerateToken(&OT_Header, 2U);

  error = otCoapHeaderAppendUriPathOptions(&OT_Header,C_RESSOURCE_DATA_TRANSFER);
  if (error != OT_ERROR_NONE)
  {
    APP_THREAD_Error(ERR_APEND_URI,error);
  }
  otCoapHeaderSetPayloadMarker(&OT_Header);
  pOT_Message = otCoapNewMessage(NULL, &OT_Header);
  if (pOT_Message == NULL)
  {
    APP_THREAD_Error(ERR_ALLOC_MSG,error);
  }
  error = otMessageAppend(pOT_Message, &OT_BufferSend, sizeof(OT_BufferSend));
  if (error != OT_ERROR_NONE)
  {
    APP_THREAD_Error(ERR_THREAD_COAP_APPEND,error);
  }
  memset(&OT_MessageInfo, 0, sizeof(OT_MessageInfo));
  OT_MessageInfo.mInterfaceId = OT_NETIF_INTERFACE_ID_THREAD;
  OT_MessageInfo.mPeerPort = OT_DEFAULT_COAP_PORT;
  memcpy(&OT_MessageInfo.mPeerAddr, &OT_PeerAddress, sizeof(OT_MessageInfo.mPeerAddr));
  error = otCoapSendRequest(NULL,
      pOT_Message,
      &OT_MessageInfo,
      &APP_THREAD_DummyRespHandler,
      (void*)&APP_THREAD_DataRespHandler);

  if (error != OT_ERROR_NONE && pOT_Message != NULL)
  {
    otMessageFree(pOT_Message);
  }
}

/**
 * @brief This function manages the data response handler
 *    and reschedules the sending of data.
 *
 * @param pHeader  header
 * @param pMessage message pointer
 * @param pMessageInfo message info pointer
 * @param Result error code
 * @retval None
 */
static void APP_THREAD_DataRespHandler(otCoapHeader        * pHeader,
    otMessage           * pMessage,
    const otMessageInfo * pMessageInfo,
    otError             Result)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(pHeader);
  UNUSED(pMessage);
  UNUSED(pMessageInfo);

  if (Result == OT_ERROR_NONE)
  {
    /* Ask to perform a new transfer */
    HAL_Delay(1000U);
    UTIL_SEQ_SetTask(TASK_SEND_BUFFER, CFG_SCH_PRIO_1);
  }
  else
  {
    APP_THREAD_Error(ERR_FILE_RESP_HANDLER,Result);
  }
}

/**
 * @brief This function is used to handle a dummy response handler
 *
 * @param p_context  context
 * @param pHeader  coap header
 * @param pMessage message
 * @paramp pMessageInfo otMessage information
 * @param Result error status
 * @retval None
 */
static void APP_THREAD_DummyRespHandler(void                * p_context,
    otCoapHeader        * pHeader,
    otMessage           * pMessage,
    const otMessageInfo * pMessageInfo,
    otError             Result)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(p_context);
  UNUSED(pHeader);
  UNUSED(pMessage);
  UNUSED(pMessageInfo);
  UNUSED(Result);
}
/* USER CODE END FD_LOCAL_FUNCTIONS */

/*************************************************************
 *
 * WRAP FUNCTIONS
 *
 *************************************************************/

void APP_THREAD_RegisterCmdBuffer(TL_CmdPacket_t* p_buffer)
{
  p_thread_otcmdbuffer = p_buffer;
}


Thread_OT_Cmd_Request_t* THREAD_Get_OTCmdPayloadBuffer(void)
{
  return (Thread_OT_Cmd_Request_t*)p_thread_otcmdbuffer->cmdserial.cmd.payload;
}

Thread_OT_Cmd_Request_t* THREAD_Get_OTCmdRspPayloadBuffer(void)
{
  return (Thread_OT_Cmd_Request_t*)((TL_EvtPacket_t *)p_thread_otcmdbuffer)->evtserial.evt.payload;
}

Thread_OT_Cmd_Request_t* THREAD_Get_NotificationPayloadBuffer(void)
{
  return (Thread_OT_Cmd_Request_t*)(p_thread_notif_M0_to_M4)->evtserial.evt.payload;
}

/**
 * @brief  This function is used to transfer the Ot commands from the
 *         M4 to the M0.
 *
 * @param   None
 * @return  None
 */
void Ot_Cmd_Transfer(void)
{
  /* OpenThread OT command cmdcode range 0x280 .. 0x3DF = 352 */
  p_thread_otcmdbuffer->cmdserial.cmd.cmdcode = 0x280U;
  /* Size = otCmdBuffer->Size (Number of OT cmd arguments : 1 arg = 32bits so multiply by 4 to get size in bytes)
   * + ID (4 bytes) + Size (4 bytes) */
  uint32_t l_size = ((Thread_OT_Cmd_Request_t*)(p_thread_otcmdbuffer->cmdserial.cmd.payload))->Size * 4U + 8U;
  p_thread_otcmdbuffer->cmdserial.cmd.plen = l_size;

  TL_OT_SendCmd();

  /* Wait completion of cmd */
  Wait_Getting_Ack_From_M0();
}

/**
 * @brief  This function is called when acknowledge from OT command is received from the M0+.
 *
 * @param   Otbuffer : a pointer to TL_EvtPacket_t
 * @return  None
 */
void TL_OT_CmdEvtReceived( TL_EvtPacket_t * Otbuffer )
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(Otbuffer);

  Receive_Ack_From_M0();
}

/**
 * @brief  This function is called when notification from M0+ is received.
 *
 * @param   Notbuffer : a pointer to TL_EvtPacket_t
 * @return  None
 */
void TL_THREAD_NotReceived( TL_EvtPacket_t * Notbuffer )
{
  p_thread_notif_M0_to_M4 = Notbuffer;

  Receive_Notification_From_M0();
}

/**
 * @brief  This function is called before sending any ot command to the M0
 *         core. The purpose of this function is to be able to check if
 *         there are no notifications coming from the M0 core which are
 *         pending before sending a new ot command.
 * @param  None
 * @retval None
 */
void Pre_OtCmdProcessing(void)
{
  UTIL_SEQ_WaitEvt( EVENT_SYNCHRO_BYPASS_IDLE);
}

/**
 * @brief  This function waits for getting an acknowledgment from the M0.
 *
 * @param  None
 * @retval None
 */
static void Wait_Getting_Ack_From_M0(void)
{
  UTIL_SEQ_WaitEvt(EVENT_ACK_FROM_M0_EVT);
}

/**
 * @brief  Receive an acknowledgment from the M0+ core.
 *         Each command send by the M4 to the M0 are acknowledged.
 *         This function is called under interrupt.
 * @param  None
 * @retval None
 */
static void Receive_Ack_From_M0(void)
{
  UTIL_SEQ_SetEvt(EVENT_ACK_FROM_M0_EVT);
}

/**
 * @brief  Receive a notification from the M0+ through the IPCC.
 *         This function is called under interrupt.
 * @param  None
 * @retval None
 */
static void Receive_Notification_From_M0(void)
{
  CptReceiveMsgFromM0++;
  UTIL_SEQ_SetTask(TASK_MSG_FROM_M0_TO_M4,CFG_SCH_PRIO_0);
}

#if (CFG_USB_INTERFACE_ENABLE != 0)
#else
static void RxCpltCallback(void)
{
  /* Filling buffer and wait for '\r' char */
  if (indexReceiveChar < C_SIZE_CMD_STRING)
  {
    CommandString[indexReceiveChar++] = aRxBuffer[0];
    if (aRxBuffer[0] == '\r')
    {
      CptReceiveCmdFromUser = 1U;

      /* UART task scheduling*/
      UTIL_SEQ_SetTask(1U << CFG_TASK_SEND_CLI_TO_M0, CFG_SCH_PRIO_0);
    }
  }

  /* Once a character has been sent, put back the device in reception mode */
  HW_UART_Receive_IT(CFG_CLI_UART, aRxBuffer, 1U, RxCpltCallback);
}
#endif /* (CFG_USB_INTERFACE_ENABLE != 0) */

#if (CFG_USB_INTERFACE_ENABLE != 0)
/**
 * @brief Process the command strings.
 *        As soon as a complete command string has been received, the task
 *        in charge of sending the command to the M0 is scheduled
 * @param  None
 * @retval None
 */
static uint32_t  ProcessCmdString( uint8_t* buf , uint32_t len )
{
  uint32_t i,j,tmp_start;
  tmp_start = 0;
  uint32_t res = 0;

  i= 0;
  while ((buf[i] != '\r') && (i < len))
  {
    i++;
  }

  if (i != len)
  {
    memcpy(CommandString, buf,(i+1));
    indexReceiveChar = i + 1U; /* Length of the buffer containing the command string */
    UTIL_SEQ_SetTask(1U << CFG_TASK_SEND_CLI_TO_M0, CFG_SCH_PRIO_0);
    tmp_start = i;
    for (j = 0; j < (len - tmp_start - 1U) ; j++)
    {
      buf[j] = buf[tmp_start + j + 1U];
    }
    res = len - tmp_start - 1U;
  }
  else
  {
    res = len;
  }
  return res; /* Remaining characters in the temporary buffer */
}
#endif/* (CFG_USB_INTERFACE_ENABLE != 0) */

/**
 * @brief Process sends receive CLI command to M0.
 * @param  None
 * @retval None
 */
static void Send_CLI_To_M0(void)
{
  memset(ThreadCliCmdBuffer.cmdserial.cmd.payload, 0x0U, 255U);
  memcpy(ThreadCliCmdBuffer.cmdserial.cmd.payload, CommandString, indexReceiveChar);
  ThreadCliCmdBuffer.cmdserial.cmd.plen = indexReceiveChar;
  ThreadCliCmdBuffer.cmdserial.cmd.cmdcode = 0x0;

  /* Clear receive buffer, character counter and command complete */
  CptReceiveCmdFromUser = 0;
  indexReceiveChar = 0;
  memset(CommandString, 0, C_SIZE_CMD_STRING);

  TL_CLI_SendCmd();
}

/**
 * @brief Send notification for CLI TL Channel.
 * @param  None
 * @retval None
 */
static void Send_CLI_Ack_For_OT(void)
{

  /* Notify M0 that characters have been sent to UART */
  TL_THREAD_CliSendAck();
}

/**
 * @brief Perform initialization of CLI UART interface.
 * @param  None
 * @retval None
 */
void APP_THREAD_Init_UART_CLI(void)
{
  UTIL_SEQ_RegTask( 1<<CFG_TASK_SEND_CLI_TO_M0, UTIL_SEQ_RFU,Send_CLI_To_M0);
#if (CFG_USB_INTERFACE_ENABLE != 0)
#else
  MX_USART1_UART_Init();
  HW_UART_Receive_IT(CFG_CLI_UART, aRxBuffer, 1, RxCpltCallback);
#endif /* (CFG_USB_INTERFACE_ENABLE != 0) */
}

/**
 * @brief Perform initialization of TL for THREAD.
 * @param  None
 * @retval None
 */
void APP_THREAD_TL_THREAD_INIT(void)
{
  ThreadConfigBuffer.p_ThreadOtCmdRspBuffer = (uint8_t*)&ThreadOtCmdBuffer;
  ThreadConfigBuffer.p_ThreadNotAckBuffer = (uint8_t*)ThreadNotifRspEvtBuffer;
  ThreadConfigBuffer.p_ThreadCliRspBuffer = (uint8_t*)&ThreadCliCmdBuffer;

  TL_THREAD_Init( &ThreadConfigBuffer );
}

/**
 * @brief  This function is called when notification on CLI TL Channel from M0+ is received.
 *
 * @param   Notbuffer : a pointer to TL_EvtPacket_t
 * @return  None
 */
void TL_THREAD_CliNotReceived( TL_EvtPacket_t * Notbuffer )
{
  TL_CmdPacket_t* l_CliBuffer = (TL_CmdPacket_t*)Notbuffer;
  uint8_t l_size = l_CliBuffer->cmdserial.cmd.plen;

  /* WORKAROUND: if string to output is "> " then respond directly to M0 and do not output it */
  if (strcmp((const char *)l_CliBuffer->cmdserial.cmd.payload, "> ") != 0)
  {
    /* Write to CLI UART */
#if (CFG_USB_INTERFACE_ENABLE != 0)
    VCP_SendData( l_CliBuffer->cmdserial.cmd.payload, l_size, HostTxCb);
#else
    HW_UART_Transmit_IT(CFG_CLI_UART, l_CliBuffer->cmdserial.cmd.payload, l_size, HostTxCb);
#endif /*USAGE_OF_VCP */
  }
  else
  {
    Send_CLI_Ack_For_OT();
  }
}

/**
 * @brief  End of transfer callback for CLI UART sending.
 *
 * @param   Notbuffer : a pointer to TL_EvtPacket_t
 * @return  None
 */
void HostTxCb(void)
{
  Send_CLI_Ack_For_OT();
}

/**
 * @brief Process the messages coming from the M0.
 * @param  None
 * @retval None
 */
void APP_THREAD_ProcessMsgM0ToM4(void)
{
  if (CptReceiveMsgFromM0 != 0)
  {
    /* If CptReceiveMsgFromM0 is > 1. it means that we did not serve all the events from the radio */
    if (CptReceiveMsgFromM0 > 1U)
    {
      APP_THREAD_Error(ERR_REC_MULTI_MSG_FROM_M0, 0);
    }
    else
    {
      OpenThread_CallBack_Processing();
    }
    /* Reset counter */
    CptReceiveMsgFromM0 = 0;
  }
}

#if (CFG_USB_INTERFACE_ENABLE != 0)
/**
 * @brief  This function is called when thereare some data coming
 *         from the Hyperterminal via the USB port
 *         Data received over USB OUT endpoint are sent over CDC interface
 *         through this function.
 * @param  Buf: Buffer of data received
 * @param  Len: Number of data received (in bytes)
 * @retval Number of characters remaining in the buffer and not yet processed
 */
void VCP_DataReceived(uint8_t* Buf , uint32_t *Len)
{
  uint32_t i,flag_continue_checking = TRUE;
  uint32_t char_remaining = 0;
  static uint32_t len_total = 0;

  /* Copy the characteres in the temporary buffer */
  for (i = 0; i < *Len; i++)
  {
    TmpString[len_total++] = Buf[i];
  }

  /* Process the buffer commands one by one     */
  /* A command is limited by a \r caracaters    */
  while (flag_continue_checking == TRUE)
  {
    char_remaining = ProcessCmdString(TmpString,len_total);
    /* If char_remaining is equal to len_total, it means that the command string is not yet
     * completed.
     * If char_remaining is equal to 0, it means that the command string has
     * been entirely processed.
     */
    if ((char_remaining == 0) || (char_remaining == len_total))
    {
      flag_continue_checking = FALSE;
    }
    len_total = char_remaining;
  }
}
#endif /* (CFG_USB_INTERFACE_ENABLE != 0) */

/* USER CODE BEGIN FD_WRAP_FUNCTIONS */

/* USER CODE END FD_WRAP_FUNCTIONS */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/