Protocol_User.c 9.88 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 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
#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"
#include "Protocol_Lib.h"
#include "app_Ble_User.h"
#include "app_BT_User.h"

#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>



#define TAG "Prot_User"
Protocol_User_Ctrl_Struct Prot_User;



#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);
}


void Protocol_Send_Service(void)
{
    Protocol_uint32_t i       = 0u;
    Protocol_uint32_t DataLen = 0u;
    Protocol_uint32_t SendLen = 0u;


    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;
    }


    bsp_Uart_Send_Data(UsartDataBuf, SendLen);

}

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;
}

时昊's avatar
时昊 committed
212
static void Protocol_UartHandle(const Protocol_Data_t *pData)//esp接收底板数据
213
{
214
    if ( pData->CmdID == 0x20 )//收到底板发送的数据后,才会发送0x12,0x01,0x02的id
215 216 217 218 219 220 221 222
    {
        //printf("0x20 is get\r\n");
        if(Prot_User.State < Prot_Start)
        {
            Prot_User.State = Prot_Start;
            printf("Prot_Start \r\n");
        }
    }
时昊's avatar
时昊 committed
223
    else if(pData->CmdID == 0x03 )//收到底板发送的电话状态,来控制esp32蓝牙电话
224 225 226 227 228 229 230 231 232 233 234 235 236
    {
        //printf("0x03 is get\r\n");
        if(Prot_User.State < Prot_Normal)
        {
            Prot_User.State = Prot_Normal;

            printf("Prot_Normal \r\n");
        }

        if(Prot_User.State >= Prot_Start)
        {
            // if(pData->DataLen == 33)
            {
时昊's avatar
时昊 committed
237
                if(BT_User.HF_Ctrl != pData->Data[0])//和上次数据不同
238
                {
时昊's avatar
时昊 committed
239
                    BT_User.HF_Ctrl = pData->Data[0];//esp32接收到的数据 
240 241
                }

242 243 244 245
                // if(strncmp((const char *) ble_uerid, (const char *)&(pData->Data[1]), 32) != 0)
                // {
                //     memcpy(( uint8_t * )ble_uerid, (uint8_t *)&(pData->Data[1]), 32);
                // }
246

247 248
                // // esp_log_buffer_hex(TAG, ble_uerid, 32);
                // // esp_log_buffer_hex(TAG, &(pData->Data[1]), 32);
249

250 251 252
                // if(Ble_User.UseridUpdate == 0)
                // {
                //     bsp_Ble_Init();
253

254 255
                //     printf("get uuid and init ble!!! \r\n");
                // }
256

257
                // Ble_User.UseridUpdate = 0xAA;
258 259 260
            }
        }
    }
时昊's avatar
时昊 committed
261
    else if(pData->CmdID == 0x21 )//esp32收到底板发送的是1,则esp32需要升级
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
    {
        //printf("Wifi_OTA_Request is %d\r\n",Wifi_OTA_Request);
        if(pData->Data[0] == 1)
        {
            // if((pData->CmdID & 0xC0) == 0X40)
            // {
                if(Wifi_OTA_Request == 0)
                {
                    Wifi_OTA_Request = 1;
                    printf("get ota req! \r\n");
                }
            // }
        }
    }
    else
    {
    }
}

void UART_Put(Protocol_uint16_t Data)
{
   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");
    return;
}



/** 
 *   @brief    Protocol协议应用层数据赋值和发送
 */

void Protocol_User_Ctrl_Init(void )
{
    Prot_User.State = Prot_Idle;
    Prot_User.TimeDelay = 0;
}

时昊's avatar
时昊 committed
312
void Uart_Send_Id10_Pro(void )//esp32发送启动完成给底板
313 314 315 316 317 318
{
    uint8_t wtemp[1] = {0};
    wtemp[0] = 1;
    Protocol_Send(0x10, wtemp, 1);
}

时昊's avatar
时昊 committed
319
void Uart_Send_Id12_Pro(void )//收到底板回复的0x20后,发送蓝牙类型  ID12[0]是判断蓝牙是否连接 不等于0是连接
320
{
321
    uint8_t ID12[3] = {0};
时昊's avatar
时昊 committed
322 323
    ID12[0] |= (uint8_t)(BT_User.BT_Sts << 4);//经典
    ID12[0] |=  Ble_User.Ble_Sts;//ble
324 325 326
    ID12[1] = 22; //小时
    ID12[2] = 22; //分钟
    Protocol_Send(0x12, ID12, 3);
327 328 329 330
}

void Uart_Send_Id01_Pro(void)
{
331 332
    uint8_t ID01[7] = {0x00,0x00,0x00,0x00,0x00,0x00};
    //if(Ble_User.Tpms_Updat)
333
    {   
334 335 336 337 338 339 340 341 342 343 344 345 346
        // ID01[0] = Ble_Tpms_Data.Tpms_Sts;
        // ID01[1] = Ble_Tpms_Data.Tpms_Turn;
        // ID01[2] = Ble_Tpms_Data.Tpms_Dte[0];
        // ID01[3] = Ble_Tpms_Data.Tpms_Dte[1];
        // ID01[4] = Ble_Tpms_Data.Tpms_Dte[2];
        // ID01[5] = Ble_Tpms_Data.Tpms_Dte[3];
        ID01[0] = 0xFF;//胎压状态 
        ID01[1] = 1;//前轮高字节气压值
        ID01[2] = 200;//前轮低字节气压值
        ID01[3] = 0x28;//前轮胎压温度
        ID01[4] = 1;//后轮高字节气压值
        ID01[5] = 45;//后轮低字节气压值
        ID01[6] = 0xAB;//后轮胎压温度
时昊's avatar
时昊 committed
347 348

        //printf("转向编码:%d\r\n",Ble_Tpms_Data.Tpms_Turn);
349
    }
350
    Protocol_Send(0x01,ID01,7);
351 352 353 354 355 356 357
}


void Uart_Send_Id02_Pro(void)
{
    uint8_t ID02[1] = {0x00};
    ID02[0] =  BT_User.Call_Sts;
时昊's avatar
时昊 committed
358
    Protocol_Send(0x02, ID02, 1);//给底板发送电话状态,用这个标志位显示电话号
359 360 361 362 363 364 365 366 367 368 369 370 371 372
}




void Prot_Send_Msg_Process(void )
{
    if(Prot_User.State < Prot_Start)
    {
        Uart_Send_Id10_Pro();

        // printf("send 0x10 \r\n");
    }

373 374
    //printf("Prot_User.State = %d\r\n",Prot_User.State);
    if(Prot_User.State >=  Prot_Start)//收到底板发送的0x20后,才执行
375 376 377 378 379
    {
        if(Prot_User.TimeDelay == 0)
        {
            Uart_Send_Id12_Pro();
            Prot_User.TimeDelay++;
380
            //printf("send 0x12 \r\n");
381 382 383 384 385 386
        }
        else
        {
            Uart_Send_Id01_Pro();
            Uart_Send_Id02_Pro();
            Prot_User.TimeDelay = 0;
387
            //printf("send 0x01 0x02 \r\n");
388 389 390
        }
    }
}