#include "gprs.h"
#include "system.h"
#include <stdio.h>
#include <string.h>
#include"dbg.h"
#include "appcfg.h"
#include "stm32l0xx_hal.h"
/////////////////////Use M5311 Module/////////////////////////
/************全局变量***************/
extern ADC_HandleTypeDef hadc;
extern UART_HandleTypeDef hlpuart1;
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
extern DMA_HandleTypeDef hdma_lpuart1_rx;
extern DMA_HandleTypeDef hdma_lpuart1_tx;
extern DMA_HandleTypeDef hdma_usart1_rx;
/***********************************/
//////////
//////////
#define AT_CMD_TIMEOUT 120000 //unit ms
#define REC_BUF_SIZE 258
static volatile bool netregstatus; //网络是否注册
static const unsigned char FIFO_DEPTH=128;
static unsigned char fiforear;
static unsigned char uartfifo[FIFO_DEPTH];
/*
static inline void UARTWriteByte(unsigned char dat)
{
while(HAL_OK!=HAL_UART_Transmit(&huart1,&dat,1,0xffffffff));
}
*/
static bool UARTIsFIFOEmpty(void)
{
if((FIFO_DEPTH-hdma_usart1_rx.Instance->CNDTR)==fiforear)
{
return true;
}
return false;
}
static void UARTClearRxBuffer(void)
{
__disable_irq();
fiforear=(FIFO_DEPTH-hdma_usart1_rx.Instance->CNDTR);
__enable_irq();
}
static unsigned char UARTReadByte(void)
{
unsigned char result;
result=uartfifo[fiforear++];
if(fiforear>=FIFO_DEPTH)
{
fiforear=0;
}
return result;
}
static void GPRS_SendString(const char *str)
{
while(HAL_OK!=HAL_UART_Transmit(&huart1,(unsigned char *)str,strlen(str),0xffffffff));
}
static bool UARTReadBytesEx(unsigned char *buf,int bufsize,int reccount,int time)
{
unsigned long tickcount;
tickcount=System_GetTick();
for(int count=0;count<reccount;count++)
{
while (UARTIsFIFOEmpty())
{
if(System_GetTick()-tickcount>time)
{
return false;
}
}
unsigned char ch=UARTReadByte();
if(count<bufsize)
{
buf[count]=ch;
}
}
return true;
}
#if 0
/*
//GET /release.asp HTTP/1.1
//Host: www.lmcraft.com
//Accept: *
//Accept-Encoding: gzip, deflate, br
//Accept-Language: zh-CN,zh;q=0.9
//User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36
//Connection: Keep-Alive
*/
static const char httpheaderpart[]={"Accept: */*\r\n\
Accept-Encoding: gzip, deflate, br\r\n\
Accept-Language: zh-CN,zh;q=0.9\r\n\
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36\r\n\
Connection: Keep-Alive\r\n\
\r\n\
\r\n"
};
bool GPRS_HttpGet(const char server[],const unsigned long port,const char suburl[],unsigned char recbuf[],int bufsize,const unsigned long timeout)
{
char atstr[256];
if(!GPRS_TCPOpen(server,port,timeout))
{
return false;
}
sprintf(atstr,"GET %s HTTP/1.1\r\n",suburl);
if(!GPRS_TCPSend((const unsigned char *)atstr,strlen(atstr),timeout))
{
return false;
}
sprintf(atstr,"Host: %s\r\n",server);
if(!GPRS_TCPSend((const unsigned char *)atstr,strlen(atstr),timeout))
{
return false;
}
if(!GPRS_TCPSend((const unsigned char *)httpheaderpart,strlen(httpheaderpart),timeout))
{
return false;
}
unsigned long tickcount;
tickcount=System_GetTick();
unsigned char step=0; //0-搜索Content-Length:;1-搜索 [num]\r\n;2-搜索\r\n;3-接收body
char numstr[16]; //用来存Content-Length:后面的数字
int contentlen;
int cmpcount=0;
while(1)
{
const char targetstr[]={"Content-Length:"};
int targetstrlen=strlen(targetstr);
int rt;
rt=GPRS_TCPRecv((unsigned char *)atstr,256);
if(rt<0)
{
//连接已断开
GPRS_TCPClose(timeout);
return false;
}else if(rt>0)
{
for(int n=0;n<rt;n++)
{
switch(step)
{
case 0: //搜索Content-Length:
if(atstr[n]==targetstr[cmpcount])
{
cmpcount++;
if(cmpcount>=targetstrlen)
{
//完成匹配
cmpcount=0;
step++;
}
}else
{
cmpcount=0;
if(atstr[n]==targetstr[cmpcount])
{
cmpcount++;
if(cmpcount>=targetstrlen)
{
//完成匹配
cmpcount=0;
step++;
}
}
}
break;
case 1: //搜索 [num]\r\n
if(atstr[n]!='\r'&&atstr[n]!='\n')
{
numstr[cmpcount++]=atstr[n];
if(cmpcount>=16) //防止数组越界
{
cmpcount=15;
}
}else if(atstr[n]=='\n')
{
numstr[cmpcount]='\0';
sscanf(numstr,"%d",&contentlen);
step++;
}
break;
case 2: //搜索\r\n
if(atstr[n]=='\n')
{
step++;
cmpcount=0;
}
break;
case 3: //接收body
if(cmpcount<bufsize)
{
recbuf[cmpcount++]=atstr[n];
}
contentlen--;
if(contentlen<=0)
{
//接收完毕
if(cmpcount>=bufsize)
{
cmpcount=bufsize-1;
}
recbuf[cmpcount]='\0';
GPRS_TCPClose(timeout);
return true;
}
break;
default:
break;
}
}
tickcount=System_GetTick();
}
if(System_GetTick()-tickcount>timeout)
{
//超时
GPRS_TCPClose(timeout);
return false;
}
}
}
#endif
static char gprsapn[32];
static void _str_trim(char str[])
{
char *p;
p=str;
while((*p)==' ')
{
p++;
}
int n;
for(n=0;;n++)
{
str[n]=p[n];
if(p[n]=='\0')
{
break;
}
}
n--;
while(p[n]==' ')
{
p[n]='\0';
}
}
#define GPRS_INIT_TIME_OUT 20000 //初始化超时时间unit ms
#define GPRS_INIT_IP_TIME_OUT 120000 //初始化时IP驻网超时时间unit ms
static char IMEI[32];
static char CIMI[32];
static char CCID[32];
void GPRS_GetInfo(char **imei,char **cimi,char **ccid)
{
*imei=IMEI;
*cimi=CIMI;
*ccid=CCID;
}
/**********字符串异步匹配函数************/
typedef struct _STR_CMP_DAT{
char *str;
int pos;
}STR_CMP_DAT;
static bool mystrcmp(STR_CMP_DAT *strdat,const char ch)
{
if(strdat->str[strdat->pos]==ch)
{
strdat->pos++;
if(strdat->str[strdat->pos]=='\0')
{
return true;
}
}else if(strdat->pos!=0){
strdat->pos=0;
if(strdat->str[strdat->pos]==ch)
{
strdat->pos++;
if(strdat->str[strdat->pos]=='\0')
{
return true;
}
}
}
return false;
}
/*************GPRS公共消息判断函数*************/
extern void GPRS_TcpDataComingCallBack(int time);
extern void GPRS_TcpCloseCallBack(int time);
#pragma anon_unions
#define GPRS_GLOBAL_EVENT_STR_LEN 16
typedef struct _GPRS_GLOBAL_EVENT{
/*
type:
0-单一的消息
1-字符串参数,例如 +IP:10.162.7.92
*/
unsigned char type;
const char event[20];
union{
void *param;
bool *param_flag;
char *param_str;
void(*callback)(int time);
};
int paramsize;
unsigned long time; //超时时间ms
}GPRS_GLOBAL_EVENT;
static const int local_IP_size=20;
static char local_IP[local_IP_size];
static const int gprsglobaleventcount=2;
static const GPRS_GLOBAL_EVENT gprsglobalevent[gprsglobaleventcount]={
{0,"+IPRD:",(void*)GPRS_TcpDataComingCallBack,NULL,1000},
{0,"+IPCLOSE:",(void*)GPRS_TcpCloseCallBack,NULL,1000},
};
static STR_CMP_DAT gprsglobaleventstrcmpdat[gprsglobaleventcount];
static bool gprsglobalerrorflag;
static inline void GPRS_GlobalEventProcess(const char ch)
{
for(int n=0;n<gprsglobaleventcount;n++)
{
if(mystrcmp(&gprsglobaleventstrcmpdat[n],ch))
{
//有消息匹配
printf("收到事件:%s\r\n",gprsglobalevent[n].event);
switch(gprsglobalevent[n].type)
{
case 0: //+IPNMI:
if(gprsglobalevent[n].callback!=NULL)
{
gprsglobalevent[n].callback(gprsglobalevent[n].time);
}
break;
case 1: //+IP:10.162.7.92
{
unsigned long tickcount;
int strcount=0;
tickcount=System_GetTick();
while(1)
{
unsigned char ch;
while (UARTIsFIFOEmpty())
{
if(System_GetTick()-tickcount>gprsglobalevent[n].time)
M5311Driver_AT命令参考程序_IOTAT命令驱动_m5311程序_M5311_m5311发送数据_m5311驱动_源
版权申诉
5星 · 超过95%的资源 64 浏览量
2021-09-11
15:59:21
上传
评论
收藏 6KB RAR 举报
心梓
- 粉丝: 812
- 资源: 8057
最新资源
- 手机电脑互传文件LocalSend(一款免费、开源、跨平台的局域网文件互传软件)
- 小波分析是一种基于时频分析的数学工具,它的核心概念是小波变换和小波系数 小波变换通过将信号与小波函数进行卷积运算,得到一系列的小
- 五一数学建模联赛的论文通常包含了对特定问题的数学建模、算法设计、编程实现以及结果分析等部分 由于具体的题目和解决方案会因每年的比
- 2024年五一数学建模竞赛是一项重要的数学建模赛事,旨在通过模拟面对实际问题寻求解决方案的活动,训练学生的想象力、洞察力和创造力
- Unity3D游戏源码PLUS教程(2018)3D传送门
- 管理系统源码是一套用于支持和优化组织内部运营的软件程序资源,涵盖了从项目规划、执行到监控的全方位管理功能 以下是关于管理系统源码
- Unity3D游戏源码PLUS教程火炬之光demo第四代
- Python爬虫是一种使用Python编程语言来自动化获取网页数据的技术 这项技术主要涉及到向目标服务器发送请求,获取HTML页
- Unity3D游戏源码PLUS教程大型ARPG手游(降临)客户端服务端全套源码
- 毕业设计是大学生涯中一项重要且富有挑战性的任务,它不仅是对学生专业知识掌握程度的检验,更是对学生创新能力、实践能力和团队协作能力
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈