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

serial_ctrl.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: 4914bbc734ee4ef82bab6cac7fbce806a2015160 (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
/**
******************************************************************************
* @file    serial_ctrl.c
* @author  BLE Mesh Team
* @brief   Serial Control file 
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2020 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
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "hal_common.h"
#include "serial_if.h"
#include "serial_ctrl.h"
#include "light.h"
#include "light_lc.h"
#include "vendor.h"
#include "appli_vendor.h"
#include "generic_client.h"
#include "light_client.h"
#include "sensors_client.h"
/** @addtogroup BlueNRG_Mesh
*  @{
*/

/** @addtogroup Middlewares_Serial_Interface
*  @{
*/

/* Private define ------------------------------------------------------------*/
#define SERIAL_MODEL_DATA_OFFSET      15

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
const MODEL_OpcodeTableParam_t *Light_OpcodeTable;
const MODEL_OpcodeTableParam_t *Generic_OpcodeTable;
const MODEL_OpcodeTableParam_t *LightLC_OpcodeTable;
const MODEL_OpcodeTableParam_t *Sensor_OpcodeTable;
MOBLEUINT16 Light_OpcodeTableLength;
MOBLEUINT16 Generic_OpcodeTableLength;
MOBLEUINT16 LightLC_OpcodeTableLength;
MOBLEUINT16 Sensor_OpcodeTableLength;
extern MOBLEUINT16 Vendor_Opcodes_Table[] ;

/* Private function prototypes -----------------------------------------------*/
MOBLEUINT8 SerialCtrl_GetMinParamLength(MOBLEUINT32 opcode, const MODEL_OpcodeTableParam_t list[], MOBLEUINT16 length);
MOBLEUINT8 SerialCtrl_GetData(char *rcvdStringBuff, uint16_t rcvdStringSize, MOBLEUINT8 dataOffset, MOBLEUINT8  *data);
/* Private functions ---------------------------------------------------------*/ 
/**
* @brief  This funcrion is used to parse the string given by the user
* @param  rcvdStringBuff: buffer to store input string 
* @param  rcvdStringSize: length of the input string 
* @retval void
*/ 
void SerialCtrlVendorRead_Process(char *rcvdStringBuff, uint16_t rcvdStringSize)
{
  MOBLEUINT16 command = 0;                          /*Opcode command to be executed by the destination node*/
  MOBLEUINT8 datalength = 0;
  MOBLEUINT8  data [10] = {0};                  /*buffer to output property variables */
  MOBLE_RESULT result = MOBLE_RESULT_FAIL;
  MODEL_MessageHeader_t msgHdr;
  
  /*Initializing the parameters*/
  msgHdr.elementIndex = 0;
  msgHdr.peer_addr = 0;
  msgHdr.dst_peer = 0;
  msgHdr.ttl = 0;
  msgHdr.rssi = 0;
  msgHdr.rcvdAppKeyOffset = 0;
  msgHdr.rcvdNetKeyOffset = 0;
  
  sscanf(rcvdStringBuff+5, "%4hx %hx ", &msgHdr.peer_addr, &command); 
  
    for(int i = 0; i < 6 ; i++)
    {
      if(command == Vendor_Opcodes_Table[i])
      {                 
        result = MOBLE_RESULT_SUCCESS;
        break;
      }
    
    }
  
  datalength = SerialCtrl_GetData(rcvdStringBuff, rcvdStringSize, SERIAL_MODEL_DATA_OFFSET, data);
  
  
  if(result)
  {
    TRACE_I(TF_SERIAL_PRINTS,"Invalid Command\r\n");
    return;
  }
  
 else
  {
      result = BLEMesh_ReadRemoteData(&msgHdr,command, data, datalength);   
      if(result == MOBLE_RESULT_SUCCESS)
      {
        TRACE_I(TF_SERIAL_PRINTS,"Command Executed Successfully\r\n");
      }
      else
      {
        TRACE_I(TF_SERIAL_PRINTS,"Invalid Opcode Parameter\r\n");
      }
  }
   
}


void SerialCtrlVendorWrite_Process(char *rcvdStringBuff, uint16_t rcvdStringSize)
{
  MOBLE_ADDRESS peer = 0;                           /*node adderess of the destination node*/
  MOBLEUINT16 command = 0;                          /*Opcode command to be executed by the destination node*/
  MOBLEUINT8 elementIndex = 0;                  /*default element index*/  
  MOBLE_RESULT result = MOBLE_RESULT_FAIL;
  MOBLEBOOL response = MOBLE_FALSE;
  MOBLEUINT8 data_buff[VENDOR_DATA_BUFFER_SIZE];
  MOBLEUINT16 idx=0;
  MOBLEUINT8 length;
  MOBLEUINT8 j = 1;
  
  sscanf(rcvdStringBuff+5, "%4hx %hx %hx", &peer,&command,&idx); 
  
  if(command == 0x000E)
  {
    /* Check parameter if data to be send continuously */
    if (idx == 0xFF)
    {
        data_buff[0] = 0x01;     /*  data write sub command; */
        length = sizeof(data_buff)-1;
        for(MOBLEUINT8 i=1;i <sizeof(data_buff);i++)
        {
          data_buff[j] = i;
          j++;
        }
        Appli_Vendor_SetBigDataPacket(data_buff, length, 0 , peer);
        Vendor_SendDataFreq(0xFF);
        TRACE_I(TF_SERIAL_PRINTS,"Command Executed Successfully\r\n");
        return;
    }
    /* Check parameter if continuously data send operation need to stop  */
    else if (idx == 0x00)
    {
        BSP_LED_Off(LED_BLUE);
        Vendor_SendDataFreq(0x00);
        TRACE_I(TF_SERIAL_PRINTS,"Command Executed Successfully\r\n");
        return;
    }
    /* Data will be sent only once */
    else
    {
    data_buff[0] = 0x01;     /*  data write sub command; */
    length = sizeof(data_buff)-idx;
        Vendor_SendDataFreq(0x00); /* To stop sending packets periodically */
    for(MOBLEUINT8 i=idx;i <sizeof(data_buff);i++)
    {
      data_buff[j] = i;
      j++;
    }
  }
  }
  else
  {
    length = SerialCtrl_GetData(rcvdStringBuff, rcvdStringSize, SERIAL_MODEL_DATA_OFFSET, data_buff);
  }
  
  for(int i = 0; i < 6 ; i++)
    {
      if(command == Vendor_Opcodes_Table[i])
      {                 
        result = MOBLE_RESULT_SUCCESS;
        break;
      }
    }
  if(result)
  {
    TRACE_I(TF_SERIAL_PRINTS,"Invalid Command\r\n");
    return;
  }
  
 else
  {
      result = BLEMesh_SetRemoteData(peer,elementIndex,command, 
                                         data_buff, length,
                                         response, MOBLE_TRUE);   
      if(result == MOBLE_RESULT_SUCCESS)
      {
        TRACE_I(TF_SERIAL_PRINTS,"Command Executed Successfully\r\n");
      }
      else
      {
        TRACE_I(TF_SERIAL_PRINTS,"Invalid Opcode Parameter\r\n");
      }
  }
}
  

void SerialCtrl_Process(char *rcvdStringBuff, uint16_t rcvdStringSize)
{
  MOBLE_ADDRESS peer = 0;                           /*node adderess of the destination node*/
  MOBLEUINT16 command = 0;                          /*Opcode command to be executed by the destination node*/
  MOBLEUINT8 minParamLength = 0;                /*minimum number of properties required by a specific command*/
  MOBLEUINT8 elementIndex = 0;          /*default element index*/
  MOBLEUINT8  data [10] = {0};        /*buffer to output property variables */
  MOBLE_RESULT result;
  MOBLEBOOL response = MOBLE_TRUE;
  
  sscanf(rcvdStringBuff+5, "%4hx %hx ", &peer,&command); 
  
  /* Callback to store a pointer to Opcode table starting sddress and length of the table*/
#ifdef ENABLE_GENERIC_MODEL_SERVER  
  GenericModelServer_GetOpcodeTableCb(&Generic_OpcodeTable,&Generic_OpcodeTableLength);
#else /* Get Generic Client OpCode Table */
#ifdef ENABLE_GENERIC_MODEL_CLIENT
  GenericModelClient_GetOpcodeTableCb(&Generic_OpcodeTable,&Generic_OpcodeTableLength);
#endif
#endif
#ifdef ENABLE_LIGHT_MODEL_SERVER  
  LightModelServer_GetOpcodeTableCb(&Light_OpcodeTable,&Light_OpcodeTableLength);     
  LightLcServer_GetOpcodeTableCb(&LightLC_OpcodeTable,&LightLC_OpcodeTableLength);
#else /* Get Light Client OpCode Table */
#ifdef ENABLE_LIGHT_MODEL_CLIENT
  LightModelClient_GetOpcodeTableCb(&Light_OpcodeTable,&Light_OpcodeTableLength);
  //Note: Light LC is included in Lighting Client Model
#endif
#endif
#ifdef ENABLE_SENSOR_MODEL_SERVER   
  SensorModelServer_GetOpcodeTableCb(&Sensor_OpcodeTable,&Sensor_OpcodeTableLength);
#else /* Get Sensor Client OpCode Table */
#ifdef ENABLE_SENSOR_MODEL_CLIENT
  SensorsModelClient_GetOpcodeTableCb(&Sensor_OpcodeTable,&Sensor_OpcodeTableLength);
#endif
#endif
  
  /* Minimum parameter length required for a valid opcade in Generic opcode table */
  minParamLength = SerialCtrl_GetMinParamLength(command,
                                                Generic_OpcodeTable,
                                                Generic_OpcodeTableLength);
  
  /* Opcode not found in Generic opcode table 
      Start finding for opcode in Light Table*/
  if (minParamLength == 0xff)
  {
    minParamLength = SerialCtrl_GetMinParamLength(command,
                                                  Light_OpcodeTable,
                                                  Light_OpcodeTableLength);
  }
  /* Opcode not found in Light opcode table
      Start finding for opcode in Light LC Table*/
  if (minParamLength == 0xff)
  {
     minParamLength = SerialCtrl_GetMinParamLength(command,
                                                   LightLC_OpcodeTable,
                                                   LightLC_OpcodeTableLength);
     
  }
  /* Opcode not found in Light LC opcode table 
      Start finding for opcode in Sensor Table*/
  if (minParamLength == 0xff)
  {
    minParamLength = SerialCtrl_GetMinParamLength(command,
                                                  Sensor_OpcodeTable,
                                                  Sensor_OpcodeTableLength);
    
    TRACE_I(TF_SERIAL_PRINTS, "Min Parameter Length after sensor model check %d\r\n", 
                             minParamLength);
  }
  
  if (minParamLength != 0xff) /* Opcode found in one of the models */ 
  {
      minParamLength = SerialCtrl_GetData(rcvdStringBuff, 
                                          rcvdStringSize, 
                                          SERIAL_MODEL_DATA_OFFSET, 
                                          data);

      result = BLEMesh_SetRemoteData(peer,
                                     elementIndex,
                                     command, 
                                     data, 
                                     minParamLength,
                                     response, 
                                     MOBLE_FALSE);   
    if(result == MOBLE_RESULT_SUCCESS)
    {
        TRACE_I(TF_SERIAL_PRINTS, "Command Executed Successfully\r\n");
    }
    else
    {
        TRACE_I(TF_SERIAL_PRINTS, "Invalid Opcode Parameter\r\n");
    }
  }
  else
  {
    TRACE_I(TF_SERIAL_PRINTS, "Unknown Opcode\r\n");
  }
}



/**
* @brief  Returns the minimum number of parameters required by a particular Opcode
* @param  opcode: Opcode of the model whose minimum number of parameters are required
* @param  list: 
* @param  length:  
* @retval MOBLEUINT16
*/ 
MOBLEUINT8 SerialCtrl_GetMinParamLength(MOBLEUINT32 opcode, const MODEL_OpcodeTableParam_t list[], MOBLEUINT16 length)
{
  for (int i = 0; i < length; i++)
  {
    if (list[i].opcode == opcode)
    {
      return list[i].min_payload_size;
    }
  }
  return 0xff;
}


/**
* @brief  This function extract the function parameter from the string
* @param  rcvdStringBuff: array of the string parsed from the serial terminal
* @param  rcvdStringSize: sizeOf rcvdStringBuff
* @param  dataOffset:
* @param  data: Output array comprising of Data
* @param  dataIndex:  
* @retval MOBLEUINT8
*/
MOBLEUINT8 SerialCtrl_GetData(char *rcvdStringBuff, uint16_t rcvdStringSize, MOBLEUINT8 dataOffset, MOBLEUINT8  *data)
{
  MOBLEUINT8 byteBuff[10] = {0};
  MOBLEUINT8 dataIndex = 0;
  int msb, lsb, byteCounter=0;           

    for(int i=dataOffset ; i<=(rcvdStringSize) ; i++)
    {
      /* check if space or NULL found */
      if(rcvdStringBuff[i] == ' '||rcvdStringBuff[i] == '\0' )
      {
        /*if number of bytes is one*/
        while(byteCounter > 0)
        {
          data[dataIndex++] = byteBuff[--byteCounter];
            
        }
      }
      else
      {
        /* take two consecutive ascii characters from the rcvdStringBuff and convert to hex values */  
        msb = Serial_CharToHexConvert(rcvdStringBuff[i]);        
        lsb = Serial_CharToHexConvert(rcvdStringBuff[i + 1 ]);
        /*join two hex values to make one hex value*/
        byteBuff[byteCounter]  = msb << 4;
        byteBuff[byteCounter] |= lsb;
        
        i++;      /*increament for loop counter as two values are used */
        byteCounter++;   /*increament byteCounter counter*/
      }
    }
        
  return dataIndex;

}



/**
* @}
*/

/**
* @}
*/
/******************* (C) COPYRIGHT 2020 STMicroelectronics *****END OF FILE****/