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

stm32wb_at.c « stm32wb_at « Components « BSP « Drivers - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6f41b12430ea5340d6515f0122c9b5d244c27812 (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
/**
  ******************************************************************************
  * @file    stm32wb_at.c
  * @author  MCD Application Team
  * @brief   Transport layer.
  *
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 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 "stm32wb_at.h"
#include "stm32wb_at_ll.h"
#include "stm32wb_at_client.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macros ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint8_t *buffer_rx;
uint8_t buffer_rx_size;
uint8_t buffer_rx_cursor;
char str_received[64];
/* Private function prototypes -----------------------------------------------*/

/* Exported functions --------------------------------------------------------*/

/**
 * @brief Init the at module
 * @param None
 * @retval 0 in case of success, an error code otherwise
 */
uint8_t stm32wb_at_Init(uint8_t *buff_rx, uint8_t buff_rx_size)
{
  uint8_t status;

  if(buff_rx_size >= sizeof(str_received))
  {
    buffer_rx_size = buff_rx_size;
    buffer_rx = buff_rx;
    (void)memset(&buffer_rx[0], 0, buffer_rx_size);
    buffer_rx_cursor = 0;
    (void)memset(&str_received[0], 0, sizeof(str_received));

    stm32wb_at_ll_Async_receive(0);
    status = 0;
  }
  else
  {
    status = 1;
  }
  return status;
}

/**
 * @brief Accumulate bytes received and detects a new frame received
 * @param byte the last byte received
 * @retval 0 in case of success, an error code otherwise
 */
uint8_t stm32wb_at_Received(uint8_t byte)
{
  uint8_t status, i;

  if(buffer_rx_cursor < buffer_rx_size)
  {
    buffer_rx[buffer_rx_cursor] = byte;
    buffer_rx_cursor++;

    /* check if we have received the command delimiter LF */
    if(byte == (uint8_t)'\n')
    {
      /* remove CR if any */
      if(buffer_rx_cursor > 2U)
      {
        if( buffer_rx[buffer_rx_cursor - 2U] == (uint8_t)'\r')
        {
          buffer_rx[buffer_rx_cursor - 1U] = 0U;
          buffer_rx[buffer_rx_cursor - 2U] = (uint8_t)'\n';
          buffer_rx_cursor--;
        }
      }
      /* remove the last \n */
      buffer_rx[buffer_rx_cursor - 1U] = 0U;
      buffer_rx_cursor--;

      /* remove null leading caractere if any*/
      if( buffer_rx[0] == 0U)
      {
        i = 1U;
        while(buffer_rx[i] != 0U)
        {
          buffer_rx[i - 1U] = buffer_rx[i];
          i++;
        }
        buffer_rx[i - 1U] = 0U;
      }


      /* check if command is long enough */
      if(buffer_rx_cursor > 1U)
      {
        (void)strcpy(&str_received[0], (char*)&buffer_rx[0]);

        stm32wb_at_ll_Async_receive((uint8_t)sizeof(str_received));

        (void)memset(&buffer_rx[0], 0, buffer_rx_size);
        buffer_rx_cursor = 0U;

        status = stm32wb_at_Process_rx_frame(str_received);
        (void)memset(&str_received[0], 0, sizeof(str_received));
      }
      else
      {
        (void)memset(&buffer_rx[0], 0, buffer_rx_size);
        buffer_rx_cursor = 0U;
        
        stm32wb_at_ll_Async_receive(0);
      }
    }
    else
    {
      stm32wb_at_ll_Async_receive(0);
    }
    status = 0U;
  }
  else
  {
    status = 1U;
  }

  return status;
}

/**
 * @brief Process a frame received
 * @param str pointer string to process
 * @retval 0 in case of success, an error code otherwise
 */
__weak uint8_t stm32wb_at_Process_rx_frame(char * str)
{
  (void)stm32wb_at_client_Process_rx_frame(str);

  return 0;
}