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

appli_test.c « Src « mesh « ble « STM32_WPAN « ST « Middlewares - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9544139b8bf23c10de9391dce468dcf12252e4ab (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
/**
******************************************************************************
* @file   appli_test.c
* @author  BLE Mesh Team
* @brief   Serial Interface file 
******************************************************************************
* @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 <string.h>
#include <stdio.h>
#include <errno.h>
#include "hal_common.h"
#include "mesh_cfg.h"
#include "common.h"
#include "appli_test.h"
#include <string.h>
#if ENABLE_SERIAL_CONTROL
#include "serial_ctrl.h"
#endif
#if ENABLE_UT
#include "serial_ut.h"
#endif

/** @addtogroup BlueNRG_Mesh
*  @{
*/

/** @addtogroup Application
*  @{
*/

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
#define CMD_INDEX_RES_01                  1
#define CMD_INDEX_RES_02                  2
#define CMD_INDEX_RES_03                  3
#define CMD_INDEX_RES_04                  4

#define CMD_SET_OFFSET                    7
#define CMD_RES_OFFSET                    5
#define CMD_RES_COUNT                     4

/* Private variables ---------------------------------------------------------*/
MOBLEUINT8 TestNumber = 0;
MOBLEUINT32 TestCount = 0;
MOBLEUINT32 RecvCount = 0;
MOBLEUINT32 Totaltest = 0;
/* Private function prototypes -----------------------------------------------*/
static MOBLE_RESULT SerialResponse_doubleHexToHex(MOBLEUINT8* hexArray,MOBLEUINT8* outputArray, MOBLEUINT8 length);
static MOBLEUINT16 SerialResponse_GetFunctionIndex(char *text);
MOBLE_RESULT Read_CommandCount(MOBLE_ADDRESS src ,MOBLE_ADDRESS dst);
MOBLEUINT32 timeStampSend = 0;
MOBLE_ADDRESS srcAddress;
MOBLE_ADDRESS destAddress;
MOBLEUINT8 ReadFlag = 0;
extern MOBLEUINT8 successCounter=0;
extern MOBLEUINT8 sendCounter=0;
/* Private functions ---------------------------------------------------------*/

/**
* @brief  SerialResponse_Process: This function extracts the command and variables from
the recieved string and passes it to BluenrgMesh library.
* @param  rcvdStringBuff: Pointer to the recieved ascii character array from the user
* @param  rcvdStringSize: Size of the recieved array
* @retval void
*/ 
void SerialResponse_Process(char *rcvdStringBuff, uint16_t rcvdStringSize)
{
  MOBLEUINT8 testFunctionParm[10]= {'\0'} ;
  MOBLEUINT8 asciiFunctionParameter[16] = {'\0'} ;
  successCounter = 0;
  sendCounter = 0;
  
    MOBLEUINT16 commandIndex = SerialResponse_GetFunctionIndex(rcvdStringBuff+5);
    if(commandIndex != 0x00)
    {
  sscanf(rcvdStringBuff + CMD_RES_OFFSET + CMD_SET_OFFSET, "%8s %4s %4s ", asciiFunctionParameter, asciiFunctionParameter+8,asciiFunctionParameter+12);   
  /*SerialResponse_doubleHexToHex
  Function will convert the asci string into orinal hex format.
  eg- send-01 12 3456
  return 0x12,0x34,0x56       
  */
        if(MOBLE_RESULT_SUCCESS == SerialResponse_doubleHexToHex(asciiFunctionParameter,testFunctionParm,16))
        { 
  TestCount = (MOBLEUINT32)(testFunctionParm[0] << 24); 
  TestCount |= (MOBLEUINT32)(testFunctionParm[1] << 16);
  TestCount |= (MOBLEUINT32)(testFunctionParm[2] << 8);
  TestCount |= (MOBLEUINT32)(testFunctionParm[3] << 0);
  TestNumber = commandIndex;
  Totaltest = TestCount;
  srcAddress = (MOBLEUINT16)((testFunctionParm[4] << 8) | testFunctionParm[5]);  
  destAddress = (MOBLEUINT16)((testFunctionParm[6] << 8) | testFunctionParm[7]);
}
        else
        {
          TRACE_I(TF_SERIAL_CTRL, "Wrong command typed \n\r");
        }
    }
    else
    {
      TRACE_I(TF_SERIAL_CTRL, "Wrong command typed \n\r");
    }
}

/**
* @brief  SerialResponse_GetFunctionIndex: This function returns the calculated index 
of the command recieved by the user
* @param  rcvdStringBuff: Pointer to the recieved ascii character array from the user
* @param  rcvdStringSize: Size of the recieved array
* @retval MOBLEUINT16
*/  
static MOBLEUINT16 SerialResponse_GetFunctionIndex(char *text)
{
  MOBLEINT16 index = 0;
  
  if (!strncmp(text, "SET-",4))
  {   
    sscanf(text, "SET-%hd", &index);
    index = (index<=CMD_RES_COUNT)? index : 0;
    
  }
  else
  {
    return 0x00;
  }
  
  return index;
}

/**
* @brief  SerialResponse_doubleHexToHex: This function converts two 4-bit hex integers 
to one 8-bit hex integer 
* @param  hexArray: Pointer to input hex array 
* @param  outputArray: Pointer to output hex array 
* @param  length: length of the input hex array
* @retval void
*/ 
static MOBLE_RESULT SerialResponse_doubleHexToHex(MOBLEUINT8* hexArray,MOBLEUINT8* outputArray, MOBLEUINT8 length)
{
  MOBLE_RESULT status = MOBLE_RESULT_SUCCESS;
  MOBLEUINT8 counter =0,position = 0;
  MOBLEUINT8 msb = 0,lsb = 0;
  while (counter <length)
  {
    msb = Serial_CharToHexConvert(hexArray[counter]);
    lsb = Serial_CharToHexConvert(hexArray[counter + 1 ]);
    if((msb == 0xFF) && (lsb == 0xFF))
    {
      return MOBLE_RESULT_FALSE;
    }
    outputArray[position] = msb<<4;
    outputArray[position] |= lsb;
    TRACE_I(TF_SERIAL_CTRL, "data scanned is  %02hx \n\r",outputArray[position]);
    counter+=2;
    position++;
  }
  return status;
}

/**
* @brief  Test_ApplicationTest_Set01: This function sends the vendor test 
  command till the testCount become 0.this test is used to calculate the round trip
  time for the packet send and received response.
* @param  testCount: Pointer to the test count value
* @param  src: source address
* @param  dst: destination address
* @retval MOBLE_RESULT
*/
MOBLE_RESULT Test_ApplicationTest_Set01(MOBLEUINT32 testCount ,MOBLE_ADDRESS src ,MOBLE_ADDRESS dst)
{
  MOBLEUINT8 elementIndex = 0;

  /* The function will called untill the testcount will not become zero */     
  if(testCount != 0)
  {
    if(processDelay(TEST_1_WAIT_PERIOD) == 0x01)
    {
      /* Taking the timestamp while sending the command for vendor model */       
      timeStampSend = Clock_Time();      
      Vendor_TestRemoteData(src,dst,elementIndex);  

      TestCount--;                       
    }     
  }

  return MOBLE_RESULT_SUCCESS;
}

/**
* @brief  Test_ApplicationTest_Set02: This function sends the vendor test 
  command till the testCount become 0.This function is used to send the bunch of 
  command to the receiver node and then called a function Read_CommandCount to get 
  the number of command received by receiver. 
* @param  testCount: Pointer to the test count value
* @param  src: source address
* @param  dst: destination address
* @retval MOBLE_RESULT
*/
MOBLE_RESULT Test_ApplicationTest_Set02(MOBLEUINT32 testCount ,MOBLE_ADDRESS src ,MOBLE_ADDRESS dst)                                             
{
  MOBLEUINT8 elementIndex = 0;
   
  if(testCount != 0)
  {
    if(processDelay(TEST_2_WAIT_PERIOD) == 0x01)
    {  
      /* Sending the command one after one in defined interval of time */         
      Vendor_TestCounterInc(src,dst,elementIndex);  

      TestCount--;              
      TRACE_I(TF_SERIAL_CTRL, "Sending No Response command  \r\n");

      if(TestCount == 0)
      {
        /* Flag to call the function Read_CommandCount after some duration of time */          
        ReadFlag = 1;
      }
                
   }     
 }
   
 return MOBLE_RESULT_SUCCESS;
}

/**
* @brief  Read_CommandCount: This function read the number of 
  command received by the reciver successfully.
* @param  src: source address of the node
* @param  dst: destination address of the node
* @retval MOBLE_RESULT
*/
MOBLE_RESULT Read_CommandCount(MOBLE_ADDRESS src ,MOBLE_ADDRESS dst)
{
  MOBLEUINT8 readData[2];
  MODEL_MessageHeader_t msgParam;
   
  msgParam.elementIndex = 0;
  msgParam.peer_addr = src;
  msgParam.dst_peer = dst;
  msgParam.ttl = 0;
  msgParam.rssi = 0;
  msgParam.rcvdAppKeyOffset = 0;
  msgParam.rcvdNetKeyOffset = 0;

  readData[0] = APPLI_TEST_INC_COUNTER;

  if(processDelay(TEST_READ_PERIOD) == 0x01)
  {   
    TRACE_I(TF_SERIAL_CTRL, " NUMBER OF COMMANDS SEND     %d \r\n",Totaltest);
 
    BLEMesh_ReadRemoteData(&msgParam,APPLI_TEST_CMD,readData,sizeof(readData));
      
    ReadFlag = 0;
  }     

  return MOBLE_RESULT_SUCCESS;
}

/**
* @brief  Packet_ResponseTimeStamp: This function called in vendor_response
  it calculates all the packet sending ,receiving, average time.this function called
  to take the response received time stamp.
* @param  rcvTimeStamp: Pointer to the response time stamp received
* @retval MOBLE_RESULT
*/
MOBLE_RESULT Packet_ResponseTimeStamp(MOBLEUINT32 rcvTimeStamp)
{
  static MOBLEUINT32 totalTimeStamp = 0;
  MOBLEUINT32 DiffTimeStamp;
  RecvCount ++;
  DiffTimeStamp = rcvTimeStamp - timeStampSend; 
      
  /* Adding the differnce time stamp for calculation of average time of packet 
    sending and receiving 
  */ 
  totalTimeStamp = totalTimeStamp + DiffTimeStamp;
  TRACE_I(TF_SERIAL_CTRL, "Response Got                        %d out of %d\r\n", RecvCount, Totaltest);
  TRACE_I(TF_SERIAL_CTRL, "Packet send time stamp              %d \r\n",timeStampSend);
  TRACE_I(TF_SERIAL_CTRL, "Response receive time stamp         %d \r\n",rcvTimeStamp);
  TRACE_I(TF_SERIAL_CTRL, "Round trip Time %d \r\n \r\n",DiffTimeStamp); 

  if(TestCount == 0)
  {   
    totalTimeStamp = totalTimeStamp/Totaltest; 
    TRACE_I(TF_SERIAL_CTRL, "Maximum Wait Time             %d ms\r\n", TEST_1_WAIT_PERIOD);
    TRACE_I(TF_SERIAL_CTRL, "AVERAGE ROUND TRIP TIME       %d \r\n",totalTimeStamp);
    TRACE_I(TF_SERIAL_CTRL, "PACKET SINGLE TRIP TIME       %d \r\n",totalTimeStamp/2);
    totalTimeStamp = 0;
  }
  return MOBLE_RESULT_SUCCESS;
}

/**
* @brief  Test_ApplicationTest_Set03: This function sends the number of byte
  and get back the same data in response.
* @param  timeStamp: Pointer to the response received time stamp
* @param  src: source address of the node
* @param  dst: destination address of the node
* @retval MOBLE_RESULT
*/
MOBLE_RESULT Test_ApplicationTest_Set03(MOBLE_ADDRESS src ,MOBLE_ADDRESS dst)
{
  MOBLEUINT8 txDataArray[DATA_BYTE_SEND] = {0};
  txDataArray[0] = APPLI_TEST_ECHO; 
  MOBLE_RESULT result = MOBLE_RESULT_SUCCESS;
  MOBLEUINT16 idx;

  for (idx=1; idx<DATA_BYTE_SEND; idx++)
      {
    txDataArray[idx] = idx;
    TRACE_I(TF_SERIAL_CTRL,"data[%d]= %d \r\n",idx,txDataArray[idx]);             
      }

  result = BLEMesh_SetRemoteData(dst, 0,
                                 APPLI_TEST_CMD, txDataArray, 
                                 sizeof(txDataArray), MOBLE_TRUE, 
                                 MOBLE_TRUE);


      if(result)
      {
        TRACE_I(TF_SERIAL_CTRL,"Publication Error \r\n");
      }

        TestNumber = 0;
  
  return MOBLE_RESULT_SUCCESS;
}

/**
* @brief  Function used to calculate the delay.
* @param  MOBLEUINT16
* @retval MOBLEUINT8
*/
MOBLEUINT8 processDelay(MOBLEUINT16 waitPeriod)
{
  static MOBLEUINT8 Clockflag = 0;
  static MOBLEUINT32 Check_time;


  if(Clockflag == CLOCK_FLAG_DISABLE)
  {
    Check_time = Clock_Time();
    Clockflag = CLOCK_FLAG_ENABLE;
  } 
  /* The function will called untill the testcount will not become zero */     

  if(((Clock_Time()- Check_time) >= waitPeriod))
  {
    Clockflag = CLOCK_FLAG_DISABLE;
    return 0x01;
            
  }
  return 0x00;
}

/**
* @brief  Function used to run the testing function for Packet response time
* @param  void
* @retval void
*/
void Test_Process(void)
{
  if(TestNumber)
  {

    if(TestNumber == CMD_INDEX_RES_01)
    {
      Test_ApplicationTest_Set01(TestCount,srcAddress,destAddress);
    }
    else if(TestNumber == CMD_INDEX_RES_02)
    {
      Test_ApplicationTest_Set02(TestCount,srcAddress,destAddress);
      if(ReadFlag == 1)
      {
        Read_CommandCount(srcAddress , destAddress);     
      }
    }
    else if(TestNumber == CMD_INDEX_RES_03)
    {
      Test_ApplicationTest_Set03(srcAddress,destAddress);
    }
    else
    {
      TRACE_I(TF_SERIAL_CTRL,"Invalid Command\n\r");
    }
  }
}