Protocol_User.c 7.24 KB
Newer Older
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
#include <stdint.h>
#include "Protocol_User.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_bt.h"

#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_bt_main.h"
#include "gatts_table_creat_demo.h"
#include "esp_gatt_common_api.h"

#include "esp_mac.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "string.h"
#include "driver/gpio.h"
#include "MCU_Core_Protocol.h"
27
#include "Protocol_Lib.h"
28
#include "app_Ble_User.h"
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

#define UART_TX_MAX_DEPTH 1024UL          //(2 * 1024UL)    // 4K
#define UART_RX_MAX_DEPTH (2 * 1024UL)    // 4K
#define UART_DATA_BUF_LEN (2 * 1024UL)    // 4K
//extern void OTA_IC_To_Master( uint8_t len);
typedef struct
{
    Protocol_uint32_t read_pos;
    Protocol_uint32_t write_pos;
    Protocol_uint8_t  Rx_Buffer [ UART_RX_MAX_DEPTH ];
} UARTRxBuf_t;

typedef struct
{
    Protocol_uint32_t read_pos;
    Protocol_uint32_t write_pos;
    Protocol_uint8_t  Tx_Buffer [ UART_TX_MAX_DEPTH ];
} UARTTxBuf_t;

static UARTRxBuf_t      UARTRxBuf;
static UARTTxBuf_t      UARTTxBuf;
static Protocol_uint8_t UsartDataBuf [ 256 ];
static Protocol_uint8_t mDataBufPtr [ UART_DATA_BUF_LEN ];

static Protocol_uint8_t  Protocol_OpenUart(void);
static Protocol_uint32_t Protocol_UartRead(Protocol_uint8_t *pData, Protocol_uint32_t u32Len);
static Protocol_uint32_t Protocol_UartSend(const Protocol_uint8_t *pData, Protocol_uint32_t u32Len);
static void              Protocol_UartHandle(const Protocol_Data_t *pData);

void Protocol_KL30_Wakeup_Init(void)
{
    Protocol_Func_t pFunc;

    pFunc.UARTOpen_Cbk        = Protocol_OpenUart;
    pFunc.UARTSend_Cbk        = Protocol_UartSend;
    pFunc.UARTRead_Cbk        = Protocol_UartRead;
    pFunc.UARTClose_Cbk       = Protocol_NULL;
    pFunc.ProcParseCbk        = Protocol_NULL;
    pFunc.ProtocolSetData_Cbk = Protocol_UartHandle;
    UARTTxBuf.read_pos        = 0;
    UARTTxBuf.write_pos       = 0;
    UARTRxBuf.read_pos        = 0;
    UARTRxBuf.write_pos       = 0;

    Protocol_Init(mDataBufPtr, UART_DATA_BUF_LEN, &pFunc);
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
void Send_UartID(Protocol_uint8_t ID)
{
    switch (ID)
    {
    case BLE_START_STA:
        Uart_ID10_process();
        break;
    case BLE_NAVIGATION:
        Uart_ID01_process();
        break;
    case BLE_STA:
        Uart_ID12_process();
        break;
    
    default:
        break;
    }

}

96 97
void Protocol_Send_Service(void)
{
98 99
    Protocol_uint8_t ID = BLE_START_STA;
    Send_UartID(ID);
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
    Protocol_uint32_t i       = 0u;
    Protocol_uint32_t DataLen = 0u;
    Protocol_uint32_t SendLen = 0u;
    static const char *TX_TASK_TAG = "TX_TASK";
    esp_log_level_set(TX_TASK_TAG, ESP_LOG_INFO);

    if (1/*判断上一个是否发送成功*/)
    {
        if ( UARTTxBuf.write_pos == UARTTxBuf.read_pos )
        {
            return;
        }

        if ( UARTTxBuf.write_pos > UARTTxBuf.read_pos )
        {
            DataLen = UARTTxBuf.write_pos - UARTTxBuf.read_pos;
        }
        else
        {
            DataLen = UART_TX_MAX_DEPTH - (UARTTxBuf.read_pos - UARTTxBuf.write_pos);
        }

        if ( DataLen > 255 )
        {
            SendLen = 255;
        }
        else
        {
            SendLen = DataLen;
        }

        for ( i = 0u; i < SendLen; i++ )
        {
            UsartDataBuf [ i ] = UARTTxBuf.Tx_Buffer [ UARTTxBuf.read_pos ];
            UARTTxBuf.read_pos = (UARTTxBuf.read_pos + 1) % UART_TX_MAX_DEPTH;
        }


        //memcpy(Uart_Data.data, UsartDataBuf, SendLen);
        int i = 0;
        for(i = 0;i < SendLen;i++)
141
        // printf("data is  %x !!!\r\n",UsartDataBuf[i]);
142
        sendData(TX_TASK_TAG, (const char*)UsartDataBuf,SendLen);
143
        //UART_Ch1_Send_Multiple_Byte(UsartDataBuf, SendLen);/*ESP32底层*/
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
    }
}

static Protocol_uint8_t Protocol_OpenUart(void)
{
#if 0
    UART_Channel_Config_st_t loc_config;

    loc_config.u32UARTChEn            = 1;
    loc_config.u32UARTbps             = 115200;
    loc_config.pfnUARTConfirmCallBack = Protocol_NULL;
    loc_config.pfnUARTReadMsgCallBack = UART_Put;

    UART_Init(UART_RLIN31, &loc_config);
#endif
    return 1;
}

static Protocol_uint32_t Protocol_UartRead(Protocol_uint8_t *pData, Protocol_uint32_t len)
{
    Protocol_uint32_t i       = 0;
    Protocol_uint32_t DataLen = 0u;
    Protocol_uint32_t ReadLen = 0u;

    if ( UARTRxBuf.write_pos == UARTRxBuf.read_pos )
    {
        return 0;    //队列空
    }

    if ( UARTRxBuf.write_pos > UARTRxBuf.read_pos )
    {
        DataLen = UARTRxBuf.write_pos - UARTRxBuf.read_pos;
    }
    else
    {
        DataLen = UART_RX_MAX_DEPTH - (UARTRxBuf.read_pos - UARTRxBuf.write_pos);
    }

    if ( len > DataLen )
    {
        ReadLen = DataLen;
    }
    else
    {
        ReadLen = len;
    }

    for ( i = 0u; i < ReadLen; i++ )
    {
        pData [ i ]        = UARTRxBuf.Rx_Buffer [ UARTRxBuf.read_pos ];
        UARTRxBuf.read_pos = (UARTRxBuf.read_pos + 1) % UART_RX_MAX_DEPTH;
    }

    return ReadLen;
}

static Protocol_uint32_t Protocol_UartSend(const Protocol_uint8_t *pData, Protocol_uint32_t u32Len)
{
    Protocol_uint32_t i         = 0;
    Protocol_uint32_t RemainLen = 0u;

    if ( UARTTxBuf.write_pos >= UARTTxBuf.read_pos )
    {
        RemainLen = UART_TX_MAX_DEPTH - (UARTTxBuf.write_pos - UARTTxBuf.read_pos);
    }
    else
    {
        RemainLen = UARTTxBuf.read_pos - UARTTxBuf.write_pos;
    }

    if ( u32Len > RemainLen )
    {
        return 1;    //队列已满,无法插入队列
    }

    for ( i = 0; i < u32Len; i++ )
    {
        UARTTxBuf.Tx_Buffer [ UARTTxBuf.write_pos ] = pData [ i ];
        UARTTxBuf.write_pos                         = (UARTTxBuf.write_pos + 1) % UART_TX_MAX_DEPTH;
    }

    return 0;
}

229
uint8_t Ble_connect_flag = 0;
230 231
static void Protocol_UartHandle(const Protocol_Data_t *pData)
{
232 233 234
   printf("ProcParse success22222222222!!\r\n");
    Ble_connect_flag = 0;
    if ( pData->CmdID == BLE_START_STA )
235
    {
236
        Uart_ID10_process();//TX
237
    }
238
    else if ( pData->CmdID == MCU_REPLY )
239
    {
240 241 242
        Uart_ID20_process();
        Ble_connect_flag = 1;
        
243
    }
244
    else if(pData->CmdID == BLE_NAVIGATION )
245
    {
246
        Uart_ID01_process();//TX
247
    }
248
    else if(pData->CmdID == BLE_WEATHER )
249
    {
250 251 252 253 254 255 256 257 258 259
       
    }
    else if(pData->CmdID == BLE_LOCATION )
    {
    
    }
    else if(pData->CmdID == BLE_STA )
    {
        Uart_ID12_process();//TX
    }
260
    else if((pData->CmdID & 0x3f) == MCU_TO_BLE_INFO )
261
    {
262 263 264 265 266 267 268 269

        if((pData->CmdID & 0xC0) == 0X40)
        {
            if(Wifi_OTA_Request == 0)
            {
                Wifi_OTA_Request = 1;
            }
        }
270
        printf("ID21 IS in!!!\r\n");
271
        // Uart_ID21_process();//RX
272 273 274 275 276 277 278 279 280
    }
    else
    {
        //非本协议数据,不处理
    }
}

void UART_Put(Protocol_uint16_t Data)
{
281 282 283 284 285 286 287 288 289 290 291 292 293 294
   Protocol_uint32_t nextPos = 0u;

   nextPos = (UARTRxBuf.write_pos + 1) % UART_RX_MAX_DEPTH;

   if ( nextPos == UARTRxBuf.read_pos )
   {
       //队列已满,无法插入队列
   }
   else
   {
       UARTRxBuf.Rx_Buffer [ UARTRxBuf.write_pos ] = Data;
       UARTRxBuf.write_pos                         = (UARTRxBuf.write_pos + 1) % UART_RX_MAX_DEPTH;
   }
    printf("into UARTRxBuf!!!!\r\n");
295 296
    return;
}