[C] 纯文本查看 复制代码
#include "lte.h"
#include <stdio.h>
#include "dwt_delay.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#define LTE_TYPE_EC600N
// LTE UART
// recieve buff
volatile uint8_t LTE_RX_Buffer[LTE_RX_Buffer_SIZE];
// 0X4000:0x0D;0x8000:0x0A;lower:rx data size
// 0x0D:CR (carriage return),0x0A:LF (NL line feed, new line)
volatile uint16_t LTE_RX_STA = 0;
volatile uint8_t LTE_RX_State = 0;
// http request end
uint8_t end[2] = {0x1A, 0x00};
bool flag_debug_log = false;
void LTE_PowerOn(void)
{
HAL_UART_Receive_IT(&HUART_LTE, (uint8_t*)LTE_RX_Buffer, 1);
__LTE_POWER_ON;
Delay_ms(10);
Refresh_IWDG();
LOG_Info("LTE Power On.\n");
}
void LTE_PowerOff(void)
{
__LTE_POWER_OFF;
Delay_ms(50);
HAL_UART_AbortReceive_IT(&HUART_LTE);
Refresh_IWDG();
LOG_Info("LTE Power Off.\n");
}
void LTE_Clear_Buffer(void)
{
memset((char*)LTE_RX_Buffer, 0, sizeof(LTE_RX_Buffer));
LTE_RX_STA = 0;
HAL_UART_AbortReceive_IT(&HUART_LTE);
HAL_UART_Receive_IT(&HUART_LTE, (uint8_t*)LTE_RX_Buffer + (LTE_RX_STA & 0x3FFF), 1);
Refresh_IWDG();
}
void LTE_TurnOn(void)
{
HAL_GPIO_WritePin(LTE_ON_GPIO_Port, LTE_ON_Pin, GPIO_PIN_SET);
Delay_second(2);
Delay_ms(100);
HAL_GPIO_WritePin(LTE_ON_GPIO_Port, LTE_ON_Pin, GPIO_PIN_RESET);
Delay_ms(1000);
Refresh_IWDG();
LOG_Info("LTE Turn On.\n");
}
void LTE_Command(char* SendBuf)
{
__LTE_LED_NET_ON;
uint16_t times = 0;
while (LTE_RX_State == 1 && times < 5000)
{
Refresh_IWDG();
times += 10;
Delay_ms(10);
}
Refresh_IWDG();
LTE_Clear_Buffer();
if (times <= 100)
{
Delay_ms(100);
}
Refresh_IWDG();
UART_SendStr(&HUART_LTE, (uint8_t*)SendBuf);
Refresh_IWDG();
UART_SendStr(&HUART_LTE, (uint8_t*)"\r\n");
Refresh_IWDG();
Delay_ms(300);
if (flag_debug_log)
{
if (strlen((const char*)LTE_RX_Buffer) > 200)
{
LOG_Debug("LTE_TX>>%s->RX:%.200s\n", SendBuf, LTE_RX_Buffer);
}
else
{
LOG_Debug("LTE_TX>>%s->RX:%s\n", SendBuf, LTE_RX_Buffer);
}
}
Refresh_IWDG();
__LTE_LED_NET_OFF;
}
/**
* @brief send command and judge rx
*/
bool LTE_CommandJudge(const char* judge, const uint32_t times, char* SendBuf)
{
flag_debug_log = false;
char* strx = NULL;
uint32_t LTE_AT_Count = 0;
while (strx == NULL)
{
Refresh_IWDG();
LTE_Command(SendBuf);
strx = strstr((const char*)LTE_RX_Buffer, judge);
if (LTE_AT_Count++ >= times) break;
}
flag_debug_log = true;
LOG_Debug("LTE_TX>>%s->RX:%s\n", SendBuf, LTE_RX_Buffer);
return (strx != NULL);
}
bool LTE_Init()
{
LOG_Info("LTE Init Begin...\n");
int tryTimes = 3;
int totalTimes = 0;
bool flag = false;
while (totalTimes < tryTimes)
{
totalTimes++;
Refresh_IWDG();
if (totalTimes > 1)
{
LTE_PowerOff();
Refresh_IWDG();
LTE_PowerOn();
Refresh_IWDG();
LTE_TurnOn();
}
if (!LTE_CommandJudge("OK", 50, "AT"))
{
continue;
}
Refresh_IWDG();
LTE_Command("ATI");
Refresh_IWDG();
// query sim state, nomal will return "READY"
if (!LTE_CommandJudge("READY", 20, "AT+CPIN?"))
{
continue;
}
Refresh_IWDG();
LTE_Command("AT+CSQ");
Refresh_IWDG();
// // query CS, nomal will return "0,1"或"0,5" 2G
// {
// char *strx = NULL;
// uint32_t LTE_AT_Count = 0;
// while(strx == NULL)
// {
// LTE_Command("AT+CREG?");
// strx = strstr((const char*)LTE_RX_Buffer, "0,1");
// if (strx == NULL){
// strx = strstr((const char*)LTE_RX_Buffer, "0,5");
// }
// if (LTE_AT_Count++ >= 100) break;
// }
// if (strx == NULL){
// continue;
// }
// }
// query CS, nomal will return "0,1"或"0,5" 4G
{
char* strx = NULL;
uint32_t LTE_AT_Count = 0;
while (strx == NULL)
{
Refresh_IWDG();
LTE_Command("AT+CEREG?");
Refresh_IWDG();
strx = strstr((const char*)LTE_RX_Buffer, ",1");
if (strx == NULL)
{
strx = strstr((const char*)LTE_RX_Buffer, ",5");
}
if (LTE_AT_Count++ >= 100) break;
}
if (strx == NULL)
{
continue;
}
}
// // query PS, nomal will return "0,1"或"0,5"
// {
// char *strx = NULL;
// uint32_t LTE_AT_Count = 0;
// while(strx == NULL)
// {
// LTE_Command("AT+CGREG?");
// strx = strstr((const char*)LTE_RX_Buffer, "0,1");
// if (strx == NULL){
// strx = strstr((const char*)LTE_RX_Buffer, "0,5");
// }
// if (LTE_AT_Count++ >= 100) break;
// }
// }
Refresh_IWDG();
LTE_CommandJudge("OK", 10, "AT+QHTTPCFG=\"contextid\",1");
Refresh_IWDG();
LTE_Command("AT+QIACT?");
Refresh_IWDG();
// set APN, nomal will return "OK"
// CMNET
// LTE_CommandJudge("OK", 5, "AT+QICSGP=1,1,\"CMNET\",\"\",\"\",1");
// UNINET
// LTE_CommandJudge("OK", 5, "AT+QICSGP=1,1,\"UNINET\",\"\",\"\",1");
// activate PDP,nomal will return "OK"
flag = LTE_CommandJudge("OK", 10, "AT+QIACT=1");
Refresh_IWDG();
LTE_Command("AT+QIACT?");
Refresh_IWDG();
LTE_Command("AT+CSQ");
if (flag) break;
}
return flag;
}
int8_t LTE_GetCSQ(void)
{
LTE_Command("AT+CSQ");
char* strx = NULL;
strx = strstr((const char*)LTE_RX_Buffer, "+CSQ:");
if (strx != NULL)
{
int x = atoi(strx + 5);
return x;
}
return -1;
}
/**
* @brief httpGet
* @note SendBuf is url, return true if httpstatus is 200
*/
bool LTE_HttpGet(char* SendBuf)
{
int len = strlen(SendBuf);
char temp[256] = {'\0'};
sprintf(temp, "AT+QHTTPURL=%d,%d", len, LTE_WAITTIME_SECONDS);
LTE_Command(&temp[0]);
int times = 0;
{
char* strx = NULL;
while (strx == NULL && times < 5000)
{
Refresh_IWDG();
strx = strstr((const char*)LTE_RX_Buffer, "CONNECT");
Delay_ms(50);
times += 50;
}
}
LTE_Command(SendBuf);
{
times = 0;
char* strx = NULL;
while (strx == NULL && times < 5000)
{
Refresh_IWDG();
strx = strstr((const char*)LTE_RX_Buffer, "OK");
Delay_ms(50);
times += 50;
}
}
memset(temp, 0x00, sizeof(temp));
sprintf(temp, "AT+QHTTPGET=%d", LTE_WAITTIME_SECONDS);
LTE_Command(&temp[0]);
char* strx = NULL;
{
times = 0;
while (strx == NULL && times < LTE_WAITTIME_MS)
{
Refresh_IWDG();
strx = strstr((const char*)LTE_RX_Buffer, "+QHTTPGET:");
Delay_ms(50);
times += 50;
}
}
return (strx != NULL);
}
bool LTE_HttpRead(void)
{
BKP_TEST_FLAG_REG = 201;
char temp[256] = {'\0'};
sprintf(temp, "AT+QHTTPREAD=%d", LTE_WAITTIME_SECONDS);
BKP_TEST_FLAG_REG = 202;
LTE_Command(&temp[0]);
BKP_TEST_FLAG_REG = 203;
uint16_t times = 0;
while (LTE_RX_State == 1 && times < LTE_WAITTIME_MS)
{
BKP_TEST_FLAG_REG = 9000 + times / 10;
Refresh_IWDG();
times += 10;
Delay_ms(10);
}
BKP_TEST_FLAG_REG = 204;
char* strx = NULL;
strx = strstr((const char*)LTE_RX_Buffer, "OK");
BKP_TEST_FLAG_REG = 205;
return (strx != NULL);
}
/**
* @brief httpPost
* @note SendBuf is url, return true if httpstatus is 200
*/
bool LTE_HttpPost(char* SendBuf)
{
// set content_type "application/json"
// LTE_Command("AT+QHTTPCFG=\"contenttype\",4");
BKP_TEST_FLAG_REG = 101;
int len = strlen(SendBuf);
char temp[256] = {'\0'};
sprintf(temp, "AT+QHTTPURL=%d,%d", len, LTE_WAITTIME_SECONDS);
BKP_TEST_FLAG_REG = 102;
LTE_Command(&temp[0]);
BKP_TEST_FLAG_REG = 103;
int times = 0;
{
BKP_TEST_FLAG_REG = 104;
char* strx = NULL;
while (strx == NULL && times < LTE_WAITTIME_MS)
{
BKP_TEST_FLAG_REG = 1000 + times / 50;
Refresh_IWDG();
strx = strstr((const char*)LTE_RX_Buffer, "CONNECT");
Delay_ms(50);
times += 50;
}
}
BKP_TEST_FLAG_REG = 105;
LTE_Command(SendBuf);
BKP_TEST_FLAG_REG = 106;
times = 0;
{
char* strx = NULL;
BKP_TEST_FLAG_REG = 107;
while (strx == NULL && times < LTE_WAITTIME_MS)
{
BKP_TEST_FLAG_REG = 3000 + times / 50;
Refresh_IWDG();
strx = strstr((const char*)LTE_RX_Buffer, "OK");
Delay_ms(50);
times += 50;
}
}
BKP_TEST_FLAG_REG = 108;
memset(temp, 0x00, sizeof(temp));
BKP_TEST_FLAG_REG = 109;
sprintf(temp, "AT+QHTTPPOST=3,%d,%d", LTE_WAITTIME_SECONDS, LTE_WAITTIME_SECONDS);
BKP_TEST_FLAG_REG = 110;
LTE_Command(&temp[0]);
BKP_TEST_FLAG_REG = 111;
times = 0;
{
char* strx = NULL;
while (strx == NULL && times < LTE_WAITTIME_MS)
{
BKP_TEST_FLAG_REG = 5000 + times / 50;
Refresh_IWDG();
strx = strstr((const char*)LTE_RX_Buffer, "CONNECT");
Delay_ms(50);
times += 50;
}
}
BKP_TEST_FLAG_REG = 112;
LTE_Command("1=1");
BKP_TEST_FLAG_REG = 113;
char* strx = NULL;
times = 0;
{
while (strx == NULL && times < LTE_WAITTIME_MS)
{
BKP_TEST_FLAG_REG = 7000 + times / 50;
Refresh_IWDG();
strx = strstr((const char*)LTE_RX_Buffer, "QHTTPPOST");
Delay_ms(50);
times += 50;
}
}
BKP_TEST_FLAG_REG = 114;
return (strx != NULL);
}
bool LTE_TCPClientConnect(char* TcpServerIp, int TcpServerPort)
{
char temp[100] = {'\0'};
sprintf(temp, "AT+QIOPEN=1,0,\"TCP\",\"%s\",%d,0,0", TcpServerIp, TcpServerPort);
LTE_Command(&temp[0]);
int times = 0;
{
char* strx = NULL;
while (strx == NULL && times < LTE_WAITTIME_MS)
{
Refresh_IWDG();
strx = strstr((const char*)LTE_RX_Buffer, "OK");
Delay_ms(50);
times += 50;
}
}
LTE_Command("AT+QISTATE=1,0");
char* strx = NULL;
times = 0;
{
while (strx == NULL && times < LTE_WAITTIME_MS)
{
Refresh_IWDG();
strx = strstr((const char*)LTE_RX_Buffer, "OK");
Delay_ms(50);
times += 50;
}
}
return (strx != NULL);
}
bool LTE_TCPClientSend(char* SendBuf)
{
int len = strlen(SendBuf);
char temp[100] = {'\0'};
sprintf(temp, "AT+QISEND=0,%d", len);
LTE_Command(&temp[0]);
LTE_Command(SendBuf);
char* strx = NULL;
int times = 0;
{
while (strx == NULL && times < LTE_WAITTIME_MS)
{
Refresh_IWDG();
strx = strstr((const char*)LTE_RX_Buffer, "OK");
Delay_ms(50);
times += 50;
}
}
return (strx != NULL);
}
bool LTE_TCPClientRecv(void)
{
LTE_Command("AT+QIRD=0,1500");
char* strx = NULL;
int times = 0;
{
while (strx == NULL && times < LTE_WAITTIME_MS)
{
Refresh_IWDG();
strx = strstr((const char*)LTE_RX_Buffer, "OK");
Delay_ms(50);
times += 50;
}
}
return (strx != NULL);
}
bool LTE_TCPClientClose(void)
{
LTE_Command("AT+QICLOSE=0");
char* strx = NULL;
int times = 0;
{
while (strx == NULL && times < LTE_WAITTIME_MS)
{
Refresh_IWDG();
strx = strstr((const char*)LTE_RX_Buffer, "OK");
Delay_ms(50);
times += 50;
}
}
return (strx != NULL);
}
#define DEFAULT_UART_TRANSMIT_TIMEOUT 5000
void UART_SendStr(UART_HandleTypeDef *huart, uint8_t *SendBuf) {
int len = strlen((const char *)SendBuf);
UART_Send(huart, SendBuf, len);
}
void UART_Send(UART_HandleTypeDef *huart, uint8_t *SendBuf, uint32_t Length) {
HAL_UART_Transmit(huart, SendBuf, Length, DEFAULT_UART_TRANSMIT_TIMEOUT);
}