You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
208 lines
5.5 KiB
208 lines
5.5 KiB
#include "VK_Client.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "VK_Com.h"
|
|
#include "drv_ec800k.h"
|
|
#include "rtdef.h"
|
|
#include "rtthread.h"
|
|
#include "rttypes.h"
|
|
|
|
#define LINK_4G
|
|
|
|
char cmd_cmd[100] = {0};
|
|
char cmd_str[100] = {0};
|
|
|
|
/*关闭TCP连接*/
|
|
void client_tcp_close(Cclient_t *client) {
|
|
sprintf(cmd_str, AT_QICLOSE, client->connect_ID);
|
|
BSP_LTE_SendCmd(cmd_str, DEFAULT_EXPECT, DEFAULT_ERROR, LTE_CMD_WAIT_NORMAL);
|
|
client->connect_sta = false;
|
|
client->error_cnt = 0;
|
|
}
|
|
|
|
/*将EC800K软件复位*/
|
|
int16_t EC800K_Reset(void) {
|
|
int16_t rst = -1;
|
|
|
|
lte_dev.config_sta = false;
|
|
lte_dev.client1->connect_sta = false;
|
|
//lte_dev.client2->connect_sta = false;
|
|
|
|
rt_thread_mdelay(3000);
|
|
BSP_LTE_SendData((uint8_t *)"+++", 3);
|
|
rt_thread_mdelay(2000);
|
|
BSP_LTE_SendData((uint8_t *)"+++", 3);
|
|
rt_thread_mdelay(2000);
|
|
|
|
uint8_t try_count = 5;
|
|
|
|
do {
|
|
rt_thread_mdelay(1000);
|
|
|
|
rst = BSP_LTE_SendCmd(SOFT_RESET, DEFAULT_EXPECT, DEFAULT_ERROR,
|
|
LTE_CMD_WAIT_LONG);
|
|
if (0 == rst) return 0;
|
|
|
|
try_count--;
|
|
} while (try_count > 0);
|
|
|
|
return rst;
|
|
}
|
|
|
|
/*将EC800K为透传模式*/
|
|
// int16_t Set_Tansparent_Mode(Cclient_t *pClient)
|
|
//{
|
|
// int AT_QISWTMD_error_count = 0;
|
|
// /*如果socket成功打开,则将它设置为透传模式*/
|
|
// if(pClient->connect_sta == true)
|
|
// {
|
|
// /*打开透传模式*/
|
|
// memset(cmd_cmd, 0, sizeof(cmd_cmd));
|
|
// sprintf(cmd_cmd, AT_QISWTMD, pClient->connect_ID, 2);
|
|
// while(0 != BSP_LTE_SendCmd( cmd_cmd, CONNECTE_OK, DEFAULT_ERROR,
|
|
// LTE_CMD_WAIT_LONG ))
|
|
// {
|
|
// //rt_thread_mdelay(1000);
|
|
//
|
|
// if(AT_QISWTMD_error_count++ > 5)
|
|
// {
|
|
// EC800K_Reset();
|
|
//
|
|
// rt_thread_mdelay(3000);
|
|
//
|
|
// break;
|
|
// }
|
|
// }
|
|
// }
|
|
//
|
|
// return 0;
|
|
// }
|
|
|
|
int16_t client_tcp_connect(Cclient_t *client) {
|
|
int16_t rtn = 0;
|
|
|
|
if (lte_dev.config_sta == false) return rtn;
|
|
if (client->connect_sta == true) return rtn;
|
|
|
|
/*查询PDP场景*/
|
|
if (0 != BSP_LTE_SendCmd(AT_QIACT, "+QIACT: 1,1,1,", DEFAULT_ERROR,
|
|
LTE_CMD_WAIT_LLONG)) {
|
|
/*如果查询PDP场景未设置成功,则激活PDP场景模式*/
|
|
if (0 != BSP_LTE_SendCmd(AT_QIACT1, DEFAULT_EXPECT, DEFAULT_ERROR,
|
|
LTE_CMD_WAIT_LLONG)) {
|
|
BSP_LTE_SendCmd(AT_QIDEACT, NETCLOSE_SUCCESS, NETCLOSE_FAILURE,
|
|
LTE_CMD_WAIT_LLONG);
|
|
client->error_cnt++;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
//
|
|
BSP_LTE_SendCmd(AT_CFG_TRANS_SIZE, DEFAULT_EXPECT, DEFAULT_ERROR,
|
|
LTE_CMD_WAIT_LLONG);
|
|
BSP_LTE_SendCmd(AT_CFG_TRANS_WAITTM, DEFAULT_EXPECT, DEFAULT_ERROR,
|
|
LTE_CMD_WAIT_LLONG);
|
|
//
|
|
|
|
// 2, 打开链接
|
|
memset(cmd_cmd, 0, sizeof(cmd_cmd));
|
|
memset(cmd_str, 0, sizeof(cmd_str));
|
|
|
|
sprintf(cmd_cmd, AT_QIOPEN_TRANS /*AT_QIOPEN_FORW*/, client->connect_ID,
|
|
client->mode, client->ip, client->port);
|
|
sprintf(cmd_str, "+QIOPEN: %d,0", client->connect_ID);
|
|
|
|
/*打开socket, 这个模块他不返回+QIOPEN:字符串*/
|
|
if (0 != BSP_LTE_SendCmd(cmd_cmd, CONNECTE_OK, DEFAULT_ERROR,
|
|
LTE_CMD_WAIT_LLONG) &&
|
|
0 != BSP_LTE_SendCmd(cmd_cmd, cmd_str, DEFAULT_ERROR,
|
|
LTE_CMD_WAIT_LLONG)) {
|
|
if (strstr((char *)lte_dev.at_buf,
|
|
"563")) // Socket标识被占用 **应该是已经建立连接了
|
|
client_tcp_close(client);
|
|
client->connect_sta = false;
|
|
client->error_cnt++;
|
|
|
|
EC800K_Reset();
|
|
|
|
return 0;
|
|
} else {
|
|
client->connect_sta = true;
|
|
client->error_cnt = 0;
|
|
}
|
|
rt_kprintf("%s\r", lte_dev.at_buf);
|
|
|
|
#ifdef TRANSPARENT_MODE
|
|
// Set_Tansparent_Mode(client);
|
|
#endif
|
|
|
|
#ifdef LINK_4G
|
|
char buf[128] = {0};
|
|
sprintf(buf, "{\"sn\":\"%s\",\"client_id\":\"%s\"}\r\n", lte_dev.iccid,
|
|
lte_dev.imei);
|
|
|
|
rt_device_write(lte_dev.com_dev, 0, buf, sizeof(buf));
|
|
|
|
rt_device_write(lte_dev.com_dev, 0, buf, sizeof(buf));
|
|
|
|
rt_device_write(lte_dev.com_dev, 0, buf, sizeof(buf));
|
|
#endif
|
|
|
|
return rtn;
|
|
}
|
|
|
|
int16_t client_yield(CLte_dev_t *pLte_dev) {
|
|
int16_t rtn = 0;
|
|
|
|
while (1) {
|
|
if (lte_dev.client1->connect_sta == true) {
|
|
rt_err_t ret = rt_sem_take(lte_dev.com_dev_rx_sem, 1000);
|
|
if (RT_EOK == ret) {
|
|
rt_ssize_t len = 0;
|
|
while (1) {
|
|
len = rt_device_read(lte_dev.com_dev, 0, lte_dev.at_buf,
|
|
sizeof(lte_dev.at_buf));
|
|
rt_device_t fmu_com = fmu_com_get();
|
|
if (fmu_com) {
|
|
rt_device_write(fmu_com, 0, pLte_dev->at_buf, len);
|
|
}
|
|
|
|
if (len > 0) {
|
|
/*收到TCP关闭的消息*/
|
|
if (strstr((const char *)lte_dev.at_buf, CLOSED)) {
|
|
memset(cmd_str, 0, sizeof(cmd_str));
|
|
sprintf(cmd_str, "\"closed\",%d", pLte_dev->client1->connect_ID);
|
|
if (NULL != strstr((char *)lte_dev.at_buf, cmd_str)) {
|
|
EC800K_Reset();
|
|
pLte_dev->client1->connect_sta = false;
|
|
}
|
|
return 1;
|
|
} else if (strstr((const char *)lte_dev.at_buf, "NO CARRIER")) {
|
|
rt_kprintf("disconnect!!!");
|
|
pLte_dev->client1->connect_sta = false;
|
|
}
|
|
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
} else {
|
|
BSP_LTE_Config();
|
|
client_tcp_connect(lte_dev.client1);
|
|
}
|
|
}
|
|
|
|
return rtn;
|
|
}
|
|
|
|
void client_task(void) {
|
|
if (lte_dev.config_sta == false) {
|
|
}
|
|
|
|
client_yield(<e_dev);
|
|
}
|
|
|
|
//------------------End of File----------------------------
|
|
|