support aliyun sdk on TencentOS tiny
sample: examples\aliyun_iotkit_csdk_mqtt project: board\TencentOS_tiny_EVB_MX_Plus\KEIL\aliyun_iotkit_csdk_mqtt
This commit is contained in:
747
components/connectivity/iotkit-embedded-3.0.1/3rdparty/wrappers/atm/at_tcp/mk3060.c
vendored
Normal file
747
components/connectivity/iotkit-embedded-3.0.1/3rdparty/wrappers/atm/at_tcp/mk3060.c
vendored
Normal file
@@ -0,0 +1,747 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "infra_config.h"
|
||||
#include "mqtt_api.h"
|
||||
|
||||
#include "at_wrapper.h"
|
||||
#include "at_parser.h"
|
||||
#include "at_api.h"
|
||||
|
||||
#define TAG "at_mk3060_wifi"
|
||||
|
||||
#define CMD_FAIL_RSP "ERROR"
|
||||
|
||||
#define MAX_DATA_LEN 4096
|
||||
#define MAX_DOMAIN_LEN 256
|
||||
#define DATA_LEN_MAX 10
|
||||
#define LINK_ID_MAX 5
|
||||
#define SEM_WAIT_DURATION 5000
|
||||
|
||||
#define AT_CMD_EHCO_OFF "AT+UARTE=OFF"
|
||||
#define STOP_CMD "AT+CIPSTOP"
|
||||
#define STOP_CMD_LEN (sizeof(STOP_CMD)+1+1+5+1)
|
||||
|
||||
#define STOP_AUTOCONN_CMD "AT+CIPAUTOCONN"
|
||||
#define STOP_AUTOCONN_CMD_LEN (sizeof(STOP_AUTOCONN_CMD)+1+1+5+1)
|
||||
|
||||
#ifdef AT_DEBUG_MODE
|
||||
#define at_conn_hal_err(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#define at_conn_hal_warning(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#define at_conn_hal_info(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#define at_conn_hal_debug(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#else
|
||||
#define at_conn_hal_err(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#define at_conn_hal_warning(...)
|
||||
#define at_conn_hal_info(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#define at_conn_hal_debug(...)
|
||||
#endif
|
||||
|
||||
void *HAL_SemaphoreCreate(void);
|
||||
void HAL_SemaphoreDestroy(void *sem);
|
||||
void HAL_SemaphorePost(void *sem);
|
||||
int HAL_SemaphoreWait(void *sem, uint32_t timeout_ms);
|
||||
typedef int (*at_data_check_cb_t)(char data);
|
||||
|
||||
/* Change to include data slink for each link id respectively. <TODO> */
|
||||
typedef struct link_s {
|
||||
int fd;
|
||||
void* sem_start;
|
||||
void* sem_close;
|
||||
} link_t;
|
||||
|
||||
static link_t g_link[LINK_ID_MAX];
|
||||
static void* g_link_mutex;
|
||||
|
||||
static char localipaddr[16];
|
||||
|
||||
static int socket_data_info_get(char *buf, uint32_t buflen, at_data_check_cb_t valuecheck);
|
||||
static int socket_data_len_check(char data);
|
||||
|
||||
#define WIFI_SSID "Yuemewifi-3766"
|
||||
#define WIFI_PWD "aos12345"
|
||||
#define WIFI_TIMEOUT 20000
|
||||
static uint8_t gotip = 0;
|
||||
|
||||
#define MK3080_MAX_PAYLOAD_SIZE (CONFIG_MQTT_MESSAGE_MAXLEN + CONFIG_MQTT_TOPIC_MAXLEN + 20)
|
||||
|
||||
#ifndef PLATFORM_HAS_DYNMEM
|
||||
static uint8_t payload[MK3080_MAX_PAYLOAD_SIZE] = {0};
|
||||
#endif
|
||||
|
||||
static uint64_t _get_time_ms(void)
|
||||
{
|
||||
return HAL_UptimeMs();
|
||||
}
|
||||
|
||||
static uint64_t _time_left(uint64_t t_end, uint64_t t_now)
|
||||
{
|
||||
uint64_t t_left;
|
||||
|
||||
if (t_end > t_now) {
|
||||
t_left = t_end - t_now;
|
||||
} else {
|
||||
t_left = 0;
|
||||
}
|
||||
|
||||
return t_left;
|
||||
}
|
||||
|
||||
static int at_connect_wifi(char *ssid, char *pwd, uint32_t timeout_ms)
|
||||
{
|
||||
char conn_str[100]= {0};
|
||||
char out[20] = {0};
|
||||
uint64_t t_end, t_left;
|
||||
|
||||
t_end = _get_time_ms() + timeout_ms;
|
||||
|
||||
HAL_Snprintf(conn_str, 100, "AT+WJAP=%s,%s", ssid, pwd);
|
||||
|
||||
if (at_send_wait_reply(conn_str, strlen(conn_str), true, NULL,
|
||||
0, out, sizeof(out), NULL) < 0){
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strstr(out, "ERROR") != NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
while(0 == gotip) {
|
||||
HAL_SleepMs(50);
|
||||
|
||||
t_left = _time_left(t_end, _get_time_ms());
|
||||
if (0 == t_left) {
|
||||
at_conn_hal_err("wifi connect timeout!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifndef PLATFORM_HAS_OS
|
||||
at_yield(NULL, 0, NULL, 100);
|
||||
#endif
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void handle_tcp_udp_client_conn_state(uint8_t link_id)
|
||||
{
|
||||
char s[32] = {0};
|
||||
|
||||
at_read(s, 6);
|
||||
if (strstr(s, "CLOSED") != NULL) {
|
||||
at_conn_hal_info("Server closed event.");
|
||||
if (g_link[link_id].sem_close) {
|
||||
at_conn_hal_debug(TAG, "sem is going to be waked up: 0x%x", &g_link[link_id].sem_close);
|
||||
HAL_SemaphorePost(g_link[link_id].sem_close); /* wakeup send task */
|
||||
}
|
||||
at_conn_hal_info("Server conn (%d) closed.", link_id);
|
||||
} else if (strstr(s, "CONNEC") != NULL) {
|
||||
at_conn_hal_info("Server conn (%d) successful.", link_id);
|
||||
at_read(s, 3);
|
||||
if (g_link[link_id].sem_start) {
|
||||
at_conn_hal_debug("sem is going to be waked up: 0x%x", &g_link[link_id].sem_start);
|
||||
HAL_SemaphorePost(g_link[link_id].sem_start); /* wakeup send task */
|
||||
}
|
||||
} else if (strstr(s, "DISCON") != NULL) {
|
||||
at_conn_hal_info("Server conn (%d) disconnected.", link_id);
|
||||
at_read(s, 6);
|
||||
} else {
|
||||
at_conn_hal_warning("No one handle this unkown event!!!");
|
||||
}
|
||||
}
|
||||
|
||||
static int socket_data_len_check(char data)
|
||||
{
|
||||
if (data > '9' || data < '0') {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int socket_data_info_get(char *buf, uint32_t buflen, at_data_check_cb_t valuecheck)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
|
||||
if (NULL == buf || 0 == buflen) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
do {
|
||||
at_read(&buf[i], 1);
|
||||
if (buf[i] == ',') {
|
||||
buf[i] = 0;
|
||||
break;
|
||||
}
|
||||
if (i >= buflen) {
|
||||
at_conn_hal_err("Too long length of data.reader is %s \r\n", buf);
|
||||
return -1;
|
||||
}
|
||||
if (NULL != valuecheck) {
|
||||
if (valuecheck(buf[i])) {
|
||||
at_conn_hal_err("Invalid string!!!, reader is %s \r\n", buf);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
} while (1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void handle_socket_data()
|
||||
{
|
||||
int link_id = 0;
|
||||
int ret = 0;
|
||||
uint32_t len = 0;
|
||||
char reader[16] = {0};
|
||||
char *recvdata = NULL;
|
||||
struct at_conn_input param;
|
||||
|
||||
/* Eat the "OCKET," */
|
||||
at_read(reader, 6);
|
||||
if (memcmp(reader, "OCKET,", strlen("OCKET,")) != 0) {
|
||||
at_conn_hal_err("0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x invalid event format!!!\r\n",
|
||||
reader[0], reader[1], reader[2], reader[3], reader[4], reader[5]);
|
||||
return;
|
||||
}
|
||||
|
||||
memset(reader, 0, sizeof(reader));
|
||||
ret = socket_data_info_get(reader, 1, &socket_data_len_check);
|
||||
if (ret) {
|
||||
at_conn_hal_err("Invalid link id 0x%02x !!!\r\n", reader[0]);
|
||||
return;
|
||||
}
|
||||
link_id = reader[0] - '0';
|
||||
|
||||
memset(reader, 0, sizeof(reader));
|
||||
/* len */
|
||||
ret = socket_data_info_get(reader, sizeof(reader), &socket_data_len_check);
|
||||
if (ret) {
|
||||
at_conn_hal_err("Invalid datalen %s !!!\r\n", reader);
|
||||
return;
|
||||
}
|
||||
|
||||
len = atoi(reader);
|
||||
if (len > MAX_DATA_LEN) {
|
||||
at_conn_hal_err("invalid input socket data len %d \r\n", len);
|
||||
return;
|
||||
}
|
||||
/* Prepare socket data */
|
||||
#ifdef PLATFORM_HAS_DYNMEM
|
||||
recvdata = (char *)HAL_Malloc(len);
|
||||
#else
|
||||
if (len <= MK3080_MAX_PAYLOAD_SIZE) {
|
||||
recvdata = (char *)payload;
|
||||
}
|
||||
#endif
|
||||
if (!recvdata) {
|
||||
at_conn_hal_err("Error: %s %d out of memory, len is %d. \r\n", __func__, __LINE__, len);
|
||||
return;
|
||||
}
|
||||
|
||||
ret = at_read(recvdata, len);
|
||||
if (ret != len) {
|
||||
at_conn_hal_err("at read error recv %d want %d!\n", ret, len);
|
||||
goto err;
|
||||
}
|
||||
|
||||
memset(reader, 0, sizeof(reader));
|
||||
at_read(reader, 2);
|
||||
if (strncmp(reader, AT_RECV_PREFIX, 2) != 0) {
|
||||
at_conn_hal_err("at fail to read delimiter %s after data %s!\n", AT_RECV_PREFIX, reader);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (g_link[link_id].fd >= 0) {
|
||||
param.fd = g_link[link_id].fd;
|
||||
param.data = recvdata;
|
||||
param.datalen = len;
|
||||
param.remote_ip = NULL;
|
||||
param.remote_port = 0;
|
||||
|
||||
/* TODO get recv data src ip and port*/
|
||||
if (IOT_ATM_Input(¶m) != 0) {
|
||||
at_conn_hal_err(" %s socket %d get data len %d fail to post to at_conn, drop it\n",
|
||||
__func__, g_link[link_id].fd, len);
|
||||
}
|
||||
}
|
||||
|
||||
at_conn_hal_debug("%s socket data on link %d with length %d posted to at_conn\n",
|
||||
__func__, link_id, len);
|
||||
|
||||
err:
|
||||
#ifdef PLATFORM_HAS_DYNMEM
|
||||
HAL_Free(recvdata);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Wifi station event handler. include:
|
||||
* +WEVENT:AP_UP
|
||||
* +WEVENT:AP_DOWN
|
||||
* +WEVENT:STATION_UP
|
||||
* +WEVENT:STATION_DOWN
|
||||
*/
|
||||
static void mk3060wifi_event_handler(void *arg, char *buf, int buflen)
|
||||
{
|
||||
char eventhead[4] = {0};
|
||||
char eventotal[16] = {0};
|
||||
|
||||
at_read(eventhead, 3);
|
||||
if (strcmp(eventhead, "AP_") == 0) {
|
||||
at_read(eventotal, 2);
|
||||
if (strcmp(eventotal, "UP") == 0) {
|
||||
|
||||
} else if (strcmp(eventotal, "DO") == 0) {
|
||||
/*eat WN*/
|
||||
at_read(eventotal, 2);
|
||||
} else {
|
||||
at_conn_hal_err("!!!Error: wrong WEVENT AP string received. %s\r\n", eventotal);
|
||||
return;
|
||||
}
|
||||
} else if (strcmp(eventhead, "STA") == 0) {
|
||||
at_read(eventotal, 7);
|
||||
if (strcmp(eventotal, "TION_UP") == 0) {
|
||||
gotip = 1;
|
||||
} else if (strcmp(eventotal, "TION_DO") == 0) {
|
||||
/*eat WN*/
|
||||
at_read(eventotal, 2);
|
||||
memset(localipaddr, 0, sizeof(localipaddr));
|
||||
gotip = 0;
|
||||
} else {
|
||||
at_conn_hal_err("!!!Error: wrong WEVENT STATION string received. %s\r\n", eventotal);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
at_conn_hal_err("!!!Error: wrong WEVENT string received. %s\r\n", eventhead);
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Network connection state event handler. Events includes:
|
||||
* 1. +CIPEVENT:id,SERVER,CONNECTED
|
||||
* 2. +CIPEVENT:id,SERVER,CLOSED
|
||||
* 3. +CIPEVENT:CLIENT,CONNECTED,ip,port
|
||||
* 4. +CIPEVENT:CLIENT,CLOSED,ip,port
|
||||
* 5. +CIPEVENT:id,UDP,CONNECTED
|
||||
* 6. +CIPEVENT:id,UDP,CLOSED
|
||||
* 7. +CIPEVENT:SOCKET,id,len,data
|
||||
* 8. +CIPEVENT:UDP_BROADCAST,ip,port,id,len,data
|
||||
*/
|
||||
static void net_event_handler(void *arg, char *buf, int buflen)
|
||||
{
|
||||
char c;
|
||||
char s[32] = {0};
|
||||
|
||||
at_read(&c, 1);
|
||||
if (c >= '0' && c < ('0' + LINK_ID_MAX)) {
|
||||
int link_id = c - '0';
|
||||
at_read(&c, 1);
|
||||
if (c != ',') {
|
||||
at_conn_hal_err("!!!Error: wrong CIPEVENT string. 0x%02x\r\n", c);
|
||||
return;
|
||||
}
|
||||
at_read(&c, 1);
|
||||
if (c == 'S') {
|
||||
at_conn_hal_debug("%s server conn state event, linkid: %d.", __func__, link_id);
|
||||
/* Eat the "ERVER," */
|
||||
at_read(s, 6);
|
||||
if (memcmp(s, "ERVER,", strlen("ERVER,")) != 0) {
|
||||
at_conn_hal_err("invalid event format 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x",
|
||||
s[0], s[1], s[2], s[3], s[4], s[5]);
|
||||
return;
|
||||
}
|
||||
handle_tcp_udp_client_conn_state(link_id);
|
||||
} else if (c == 'U') {
|
||||
at_conn_hal_debug("%s UDP conn state event.", __func__);
|
||||
/* Eat the "DP," */
|
||||
at_read(s, 3);
|
||||
if (memcmp(s, "DP,", strlen("DP,")) != 0) {
|
||||
at_conn_hal_err("%s invalid event format 0x%02x 0x%02x 0x%02x \r\n", __FUNCTION__, s[0], s[1], s[2]);
|
||||
return;
|
||||
}
|
||||
handle_tcp_udp_client_conn_state(link_id);
|
||||
} else {
|
||||
at_conn_hal_err( "!!!Error: wrong CIPEVENT string 0x%02x at line %d\r\n", c, __LINE__);
|
||||
return ;
|
||||
}
|
||||
} else if (c == 'S') {
|
||||
at_conn_hal_debug("%s socket data event.", __func__);
|
||||
handle_socket_data();
|
||||
} else {
|
||||
at_conn_hal_err("!!!Error: wrong CIPEVENT string received. 0x%02x\r\n", c);
|
||||
return;
|
||||
}
|
||||
|
||||
at_conn_hal_debug("%s exit.", __func__);
|
||||
}
|
||||
|
||||
static void mk3060_uart_echo_off()
|
||||
{
|
||||
char *at_echo_str = AT_CMD_EHCO_OFF;
|
||||
char out[64] = {0};
|
||||
|
||||
at_send_wait_reply(at_echo_str, strlen(AT_CMD_EHCO_OFF), true,
|
||||
NULL, 0, out, sizeof(out), NULL);
|
||||
at_conn_hal_debug("The AT response is: %s", out);
|
||||
if (strstr(out, CMD_FAIL_RSP) != NULL) {
|
||||
at_conn_hal_err("%s %d failed", __func__, __LINE__);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static uint8_t inited = 0;
|
||||
|
||||
#define NET_OOB_PREFIX "+CIPEVENT:"
|
||||
#define WIFIEVENT_OOB_PREFIX "+WEVENT:"
|
||||
int HAL_AT_CONN_Init(void)
|
||||
{
|
||||
int link;
|
||||
char cmd[STOP_AUTOCONN_CMD_LEN] = {0};
|
||||
char out[64] = {0};
|
||||
|
||||
if (inited) {
|
||||
at_conn_hal_warning("at_conn component is already initialized");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (NULL == (g_link_mutex = HAL_MutexCreate())) {
|
||||
at_conn_hal_err("Creating link mutex failed (%s %d).", __func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
mk3060_uart_echo_off();
|
||||
|
||||
memset(g_link, 0, sizeof(g_link));
|
||||
for (link = 0; link < LINK_ID_MAX; link++) {
|
||||
g_link[link].fd = -1;
|
||||
/*close all link */
|
||||
HAL_Snprintf(cmd, STOP_CMD_LEN - 1, "%s=%d", STOP_CMD, link);
|
||||
at_conn_hal_debug("%s %d - AT cmd to run: %s", __func__, __LINE__, cmd);
|
||||
|
||||
at_send_wait_reply(cmd, strlen(cmd), true, NULL, 0, out,
|
||||
sizeof(out), NULL);
|
||||
at_conn_hal_debug("The AT response is: %s", out);
|
||||
if (strstr(out, CMD_FAIL_RSP) != NULL) {
|
||||
at_conn_hal_debug("%s %d failed", __func__, __LINE__);
|
||||
}
|
||||
|
||||
memset(cmd, 0, sizeof(cmd));
|
||||
|
||||
/*close all link auto reconnect */
|
||||
HAL_Snprintf(cmd, STOP_AUTOCONN_CMD_LEN - 1, "%s=%d,0", STOP_AUTOCONN_CMD, link);
|
||||
at_conn_hal_debug("%s %d - AT cmd to run: %s", __func__, __LINE__, cmd);
|
||||
|
||||
at_send_wait_reply(cmd, strlen(cmd), true, NULL, 0, out,
|
||||
sizeof(out), NULL);
|
||||
at_conn_hal_debug("The AT response is: %s", out);
|
||||
if (strstr(out, CMD_FAIL_RSP) != NULL) {
|
||||
at_conn_hal_err("%s %d failed", __func__, __LINE__);
|
||||
}
|
||||
memset(cmd, 0, sizeof(cmd));
|
||||
}
|
||||
|
||||
at_register_callback(NET_OOB_PREFIX, NULL, NULL, 0, net_event_handler, NULL);
|
||||
at_register_callback(WIFIEVENT_OOB_PREFIX, NULL, NULL, 0, mk3060wifi_event_handler, NULL);
|
||||
|
||||
if (at_connect_wifi(WIFI_SSID, WIFI_PWD, WIFI_TIMEOUT) < 0) {
|
||||
at_conn_hal_err("%s %d failed", __func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
inited = 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int HAL_AT_CONN_Deinit(void)
|
||||
{
|
||||
if (!inited) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
HAL_MutexDestroy(g_link_mutex);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define START_CMD "AT+CIPSTART"
|
||||
#define START_CMD_LEN (sizeof(START_CMD)+1+1+13+1+MAX_DOMAIN_LEN+1+5+1+5+1)
|
||||
static char *start_cmd_type_str[] = {"tcp_server", "tcp_client", \
|
||||
"ssl_client", "udp_broadcast", "udp_unicast"
|
||||
};
|
||||
|
||||
int HAL_AT_CONN_Start(at_conn_t *c)
|
||||
{
|
||||
int link_id;
|
||||
char cmd[START_CMD_LEN] = {0};
|
||||
char out[256] = {0};
|
||||
|
||||
if (!c || !c->addr) {
|
||||
at_conn_hal_err("%s %d - invalid argument", __func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
HAL_MutexLock(g_link_mutex);
|
||||
for (link_id = 0; link_id < LINK_ID_MAX; link_id++) {
|
||||
if (g_link[link_id].fd >= 0) {
|
||||
continue;
|
||||
} else {
|
||||
g_link[link_id].fd = c->fd;
|
||||
if (NULL == (g_link[link_id].sem_start = HAL_SemaphoreCreate())) {
|
||||
at_conn_hal_err("failed to allocate semaphore %s", __func__);
|
||||
g_link[link_id].fd = -1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (NULL == (g_link[link_id].sem_close = HAL_SemaphoreCreate())) {
|
||||
at_conn_hal_err("failed to allocate semaphore %s", __func__);
|
||||
HAL_SemaphoreDestroy(g_link[link_id].sem_start);
|
||||
g_link[link_id].fd = -1;
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
HAL_MutexUnlock(g_link_mutex);
|
||||
|
||||
/* The caller should deal with this failure */
|
||||
if (link_id >= LINK_ID_MAX) {
|
||||
at_conn_hal_info("No link available for now, %s failed.", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
at_conn_hal_debug("Creating %s connection ...", start_cmd_type_str[c->type]);
|
||||
|
||||
switch (c->type) {
|
||||
case TCP_CLIENT:
|
||||
HAL_Snprintf(cmd, START_CMD_LEN - 5 - 1, "%s=%d,%s,%s,%d",
|
||||
START_CMD, link_id, start_cmd_type_str[c->type],
|
||||
c->addr, c->r_port);
|
||||
if (c->l_port >= 0) {
|
||||
HAL_Snprintf(cmd + strlen(cmd), 7, ",%d", c->l_port);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
at_conn_hal_err("Invalid connection type.");
|
||||
goto err;
|
||||
}
|
||||
|
||||
at_conn_hal_debug("\r\n%s %d - AT cmd to run: %s \r\n", __func__, __LINE__, cmd);
|
||||
|
||||
at_send_wait_reply(cmd, strlen(cmd), true, NULL, 0, out,
|
||||
sizeof(out), NULL);
|
||||
at_conn_hal_debug("The AT response is: %s", out);
|
||||
if (strstr(out, CMD_FAIL_RSP) != NULL) {
|
||||
at_conn_hal_err("%s %d failed", __func__, __LINE__);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (HAL_SemaphoreWait(g_link[link_id].sem_start, SEM_WAIT_DURATION) != 0) {
|
||||
at_conn_hal_err("%s sem_wait failed", __func__);
|
||||
goto err;
|
||||
}
|
||||
|
||||
at_conn_hal_debug("%s sem_wait succeed.", __func__);
|
||||
|
||||
return 0;
|
||||
err:
|
||||
HAL_MutexLock(g_link_mutex);
|
||||
if (g_link[link_id].sem_start) {
|
||||
HAL_SemaphoreDestroy(g_link[link_id].sem_start);
|
||||
}
|
||||
|
||||
if (g_link[link_id].sem_close) {
|
||||
HAL_SemaphoreDestroy(g_link[link_id].sem_close);
|
||||
}
|
||||
g_link[link_id].fd = -1;
|
||||
HAL_MutexUnlock(g_link_mutex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int fd_to_linkid(int fd)
|
||||
{
|
||||
int link_id;
|
||||
|
||||
HAL_MutexLock(g_link_mutex);
|
||||
for (link_id = 0; link_id < LINK_ID_MAX; link_id++) {
|
||||
if (g_link[link_id].fd == fd) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
HAL_MutexUnlock(g_link_mutex);
|
||||
|
||||
return link_id;
|
||||
}
|
||||
|
||||
#define SEND_CMD "AT+CIPSEND"
|
||||
#define SEND_CMD_LEN (sizeof(SEND_CMD)+1+1+5+1+DATA_LEN_MAX+1)
|
||||
int HAL_AT_CONN_Send(int fd,
|
||||
uint8_t *data,
|
||||
uint32_t len,
|
||||
char remote_ip[16],
|
||||
int32_t remote_port,
|
||||
int32_t timeout)
|
||||
{
|
||||
int link_id;
|
||||
char cmd[SEND_CMD_LEN] = {0}, out[128] = {0};
|
||||
|
||||
if (!data) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
at_conn_hal_debug("%s on fd %d", __func__, fd);
|
||||
|
||||
link_id = fd_to_linkid(fd);
|
||||
if (link_id < 0 || link_id >= LINK_ID_MAX) {
|
||||
at_conn_hal_err("No connection found for fd (%d) in %s", fd, __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* AT+CIPSEND=id, */
|
||||
HAL_Snprintf(cmd, SEND_CMD_LEN - 1, "%s=%d,", SEND_CMD, link_id);
|
||||
/* [remote_port,] */
|
||||
if (remote_port >= 0) {
|
||||
HAL_Snprintf(cmd + strlen(cmd), 7, "%d,", remote_port);
|
||||
}
|
||||
/* data_length */
|
||||
HAL_Snprintf(cmd + strlen(cmd), DATA_LEN_MAX + 1, "%d", len);
|
||||
at_conn_hal_debug("\r\n%s %d - AT cmd to run: %s\r\n", __func__, __LINE__, cmd);
|
||||
|
||||
at_send_wait_reply((const char *)cmd, strlen(cmd), true, (const char *)data, len,
|
||||
out, sizeof(out), NULL);
|
||||
|
||||
at_conn_hal_debug("\r\nThe AT response is: %s\r\n", out);
|
||||
|
||||
if (strstr(out, CMD_FAIL_RSP) != NULL) {
|
||||
at_conn_hal_debug("%s %d failed", __func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define DOMAIN_RSP "+CIPDOMAIN:"
|
||||
#define DOMAIN_CMD "AT+CIPDOMAIN"
|
||||
#define DOMAIN_CMD_LEN (sizeof(DOMAIN_CMD)+MAX_DOMAIN_LEN+1)
|
||||
/* Return the first IP if multiple found. */
|
||||
int HAL_AT_CONN_DomainToIp(char *domain,
|
||||
char ip[16])
|
||||
{
|
||||
char cmd[DOMAIN_CMD_LEN] = {0}, out[256] = {0}, *head, *end;
|
||||
|
||||
HAL_Snprintf(cmd, DOMAIN_CMD_LEN - 1, "%s=%s", DOMAIN_CMD, domain);
|
||||
at_conn_hal_debug("%s %d - AT cmd to run: %s", __func__, __LINE__, cmd);
|
||||
|
||||
at_send_wait_reply(cmd, strlen(cmd), true, NULL, 0, out,
|
||||
sizeof(out), NULL);
|
||||
at_conn_hal_debug("The AT response is: %s", out);
|
||||
if (strstr(out, AT_RECV_SUCCESS_POSTFIX) == NULL) {
|
||||
at_conn_hal_err("%s %d failed", __func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* +CIPDOMAIN:1\r\n
|
||||
* 180.97.33.108\r\n
|
||||
*
|
||||
* OK\r\n
|
||||
*/
|
||||
if ((head = strstr(out, DOMAIN_RSP)) == NULL) {
|
||||
at_conn_hal_err("No IP info found in result string %s \r\n.", out);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Check the format */
|
||||
head += strlen(DOMAIN_RSP);
|
||||
if (head[0] < '0' && head[0] >= ('0' + LINK_ID_MAX)) {
|
||||
at_conn_hal_err("%s %d failed", __func__, __LINE__);
|
||||
goto err;
|
||||
}
|
||||
|
||||
|
||||
head++;
|
||||
if (memcmp(head, AT_RECV_PREFIX, strlen(AT_RECV_PREFIX)) != 0) {
|
||||
at_conn_hal_err("%s %d failed", __func__, __LINE__);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* We find the IP head */
|
||||
head += strlen(AT_RECV_PREFIX);
|
||||
|
||||
end = head;
|
||||
while (((end - head) < 15) && (*end != '\r')) {
|
||||
end++;
|
||||
}
|
||||
if (((end - head) < 6) || ((end - head) > 15)) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* We find a good IP, save it. */
|
||||
memcpy(ip, head, end - head);
|
||||
ip[end - head] = '\0';
|
||||
at_conn_hal_debug("get domain %s ip %s \r\n", domain, ip);
|
||||
return 0;
|
||||
|
||||
err:
|
||||
at_conn_hal_err("Failed to get IP due to unexpected result string %s \r\n.", out);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int HAL_AT_CONN_Close(int fd,
|
||||
int32_t remote_port)
|
||||
{
|
||||
int link_id;
|
||||
char cmd[STOP_CMD_LEN] = {0}, out[64];
|
||||
|
||||
link_id = fd_to_linkid(fd);
|
||||
if (link_id < 0 || link_id >= LINK_ID_MAX) {
|
||||
at_conn_hal_err("No connection found for fd (%d) in %s", fd, __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
HAL_Snprintf(cmd, STOP_CMD_LEN - 1, "%s=%d", STOP_CMD, link_id);
|
||||
at_conn_hal_debug("%s %d - AT cmd to run: %s", __func__, __LINE__, cmd);
|
||||
|
||||
at_send_wait_reply(cmd, strlen(cmd), true, NULL, 0, out,
|
||||
sizeof(out), NULL);
|
||||
at_conn_hal_debug("The AT response is: %s", out);
|
||||
if (strstr(out, CMD_FAIL_RSP) != NULL) {
|
||||
at_conn_hal_err("%s %d failed", __func__, __LINE__);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (HAL_SemaphoreWait(g_link[link_id].sem_close, SEM_WAIT_DURATION) != 0) {
|
||||
at_conn_hal_err("%s sem_wait failed", __func__);
|
||||
goto err;
|
||||
}
|
||||
|
||||
at_conn_hal_debug("%s sem_wait succeed.", __func__);
|
||||
err:
|
||||
HAL_MutexLock(g_link_mutex);
|
||||
|
||||
if (g_link[link_id].sem_start) {
|
||||
HAL_SemaphoreDestroy(g_link[link_id].sem_start);
|
||||
}
|
||||
|
||||
if (g_link[link_id].sem_close) {
|
||||
HAL_SemaphoreDestroy(g_link[link_id].sem_close);
|
||||
}
|
||||
g_link[link_id].fd = -1;
|
||||
HAL_MutexUnlock(g_link_mutex);
|
||||
return -1;
|
||||
|
||||
}
|
870
components/connectivity/iotkit-embedded-3.0.1/3rdparty/wrappers/atm/at_tcp/sim800.c
vendored
Normal file
870
components/connectivity/iotkit-embedded-3.0.1/3rdparty/wrappers/atm/at_tcp/sim800.c
vendored
Normal file
@@ -0,0 +1,870 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "infra_config.h"
|
||||
#include "mqtt_api.h"
|
||||
|
||||
#include "at_wrapper.h"
|
||||
#include "at_parser.h"
|
||||
#include "at_api.h"
|
||||
|
||||
#define TAG "sim800_gprs_module"
|
||||
|
||||
#define SIM800_AT_CMD_SUCCESS_RSP "OK"
|
||||
#define SIM800_AT_CMD_FAIL_RSP "ERROR"
|
||||
#define AT_CMD_TEST "AT"
|
||||
#define AT_CMD_TEST_RESULT "OK\r\n"
|
||||
|
||||
#define AT_CMD_ECHO_OFF "ATE0"
|
||||
#define AT_CMD_BAUDRATE_SET "AT+IPR"
|
||||
#define AT_CMD_FLOW_CONTROL "AT+IFC"
|
||||
|
||||
#define AT_CMD_SAVE_CONFIG "AT&W"
|
||||
|
||||
#define AT_CMD_SIM_PIN_CHECK "AT+CPIN?"
|
||||
#define AT_CMD_SIGNAL_QUALITY_CHECK "AT+CSQ"
|
||||
#define AT_CMD_NETWORK_REG_CHECK "AT+CREG?"
|
||||
#define AT_CMD_GPRS_ATTACH_CHECK "AT+CGATT?"
|
||||
|
||||
#define AT_CMD_GPRS_PDP_DEACTIVE "AT+CIPSHUT"
|
||||
#define AT_CMD_MULTI_IP_CONNECTION "AT+CIPMUX"
|
||||
#define AT_CMD_SEND_DATA_PROMPT_SET "AT+CIPSPRT"
|
||||
#define AT_CMD_RECV_DATA_FORMAT_SET "AT+CIPSRIP"
|
||||
|
||||
#define AT_CMD_DOMAIN_TO_IP "AT+CDNSGIP"
|
||||
#define AT_CMD_DOMAIN_RSP "\r\n+CDNSGIP: "
|
||||
|
||||
#define AT_CMD_START_TASK "AT+CSTT"
|
||||
#define AT_CMD_BRING_UP_GPRS_CONNECT "AT+CIICR"
|
||||
#define AT_CMD_GOT_LOCAL_IP "AT+CIFSR"
|
||||
|
||||
#define AT_CMD_START_CLIENT_CONN "AT+CIPSTART"
|
||||
|
||||
#define AT_CMD_CLIENT_CONNECT_OK "CONNECT OK\r\n"
|
||||
#define AT_CMD_CLIENT_CONNECT_FAIL "CONNECT FAIL\r\n"
|
||||
|
||||
#define AT_CMD_STOP_CONN "AT+CIPCLOSE"
|
||||
|
||||
#define AT_CMD_SEND_DATA "AT+CIPSEND"
|
||||
|
||||
#define AT_CMD_DATA_RECV "\r\n+RECEIVE,"
|
||||
|
||||
#define SIM800_DEFAULT_CMD_LEN 64
|
||||
#define SIM800_DEFAULT_RSP_LEN 64
|
||||
|
||||
#define SIM800_MAX_LINK_NUM 6
|
||||
|
||||
#define SIM800_DOMAIN_MAX_LEN 64
|
||||
#define SIM800_DOMAIN_RSP_MAX_LEN 128
|
||||
#define SIM800_DOMAIN_CMD_LEN (sizeof(AT_CMD_DOMAIN_TO_IP) + SIM800_DOMAIN_MAX_LEN + 1)
|
||||
|
||||
#define SIM800_CONN_CMD_LEN (SIM800_DOMAIN_MAX_LEN + SIM800_DEFAULT_CMD_LEN)
|
||||
|
||||
#define SIM800_RETRY_MAX 50
|
||||
#define SIM800_WAIT_MAX_MS 10000
|
||||
|
||||
#ifdef AT_DEBUG_MODE
|
||||
#define at_conn_hal_err(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#define at_conn_hal_info(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#define at_conn_hal_debug(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#else
|
||||
#define at_conn_hal_err(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#define at_conn_hal_info(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#define at_conn_hal_debug(...)
|
||||
#endif
|
||||
|
||||
#define SIM800_MAX_PAYLOAD_SIZE (CONFIG_MQTT_MESSAGE_MAXLEN + CONFIG_MQTT_TOPIC_MAXLEN + 20)
|
||||
|
||||
#ifndef PLATFORM_HAS_DYNMEM
|
||||
static uint8_t payload[SIM800_MAX_PAYLOAD_SIZE] = {0};
|
||||
#endif
|
||||
|
||||
/* Change to include data slink for each link id respectively. <TODO> */
|
||||
typedef struct link_s {
|
||||
int fd;
|
||||
void* sem_start;
|
||||
void* sem_close;
|
||||
} link_t;
|
||||
|
||||
static uint8_t inited = 0;
|
||||
static link_t g_link[SIM800_MAX_LINK_NUM];
|
||||
static void *g_link_mutex;
|
||||
static void *g_domain_mutex;
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
static void *g_domain_sem;
|
||||
#else
|
||||
static int g_domain_mark = 0;
|
||||
#endif
|
||||
static char g_pcdomain_buf[SIM800_DOMAIN_RSP_MAX_LEN];
|
||||
static char g_pcdomain_rsp[SIM800_DOMAIN_RSP_MAX_LEN];
|
||||
static char g_pccmd[SIM800_CONN_CMD_LEN];
|
||||
static char g_cmd[SIM800_DEFAULT_CMD_LEN];
|
||||
static char g_rsp[SIM800_DEFAULT_RSP_LEN];
|
||||
|
||||
static int fd_to_linkid(int fd)
|
||||
{
|
||||
int link_id;
|
||||
|
||||
HAL_MutexLock(g_link_mutex);
|
||||
for (link_id = 0; link_id < SIM800_MAX_LINK_NUM; link_id++) {
|
||||
if (g_link[link_id].fd == fd) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
HAL_MutexUnlock(g_link_mutex);
|
||||
|
||||
return link_id;
|
||||
}
|
||||
|
||||
static void sim800_gprs_domain_rsp_callback(void *arg, char *rspinfo, int rsplen)
|
||||
{
|
||||
if (NULL == rspinfo || rsplen == 0) {
|
||||
at_conn_hal_err( "invalid input at %s \r\n", __func__);
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(g_pcdomain_rsp, rspinfo, rsplen);
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
HAL_SemaphorePost(g_domain_sem);
|
||||
#else
|
||||
g_domain_mark = 1;
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
static void sim800_gprs_module_socket_data_handle(void *arg, char *rspinfo, int rsplen)
|
||||
{
|
||||
unsigned char uclinkid = 0;
|
||||
unsigned char unusesymbol = 0;
|
||||
unsigned char datalen[6] = {0};
|
||||
unsigned char ipaddr[16] = {0};
|
||||
unsigned char port[6] = {0};
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int len = 0;
|
||||
int remoteport = 0;
|
||||
int linkid = 0;
|
||||
char *recvdata = NULL;
|
||||
struct at_conn_input param;
|
||||
|
||||
at_read((char *)&uclinkid, 1);
|
||||
linkid = uclinkid - '0';
|
||||
if (linkid < 0 || linkid >= SIM800_MAX_LINK_NUM) {
|
||||
at_conn_hal_err( "Invalid link id 0x%02x !!!\r\n", linkid);
|
||||
return;
|
||||
}
|
||||
|
||||
/*eat , char*/
|
||||
at_read((char *)&unusesymbol, 1);
|
||||
|
||||
/* get data len */
|
||||
i = 0;
|
||||
do {
|
||||
at_read((char *)&datalen[i], 1);
|
||||
if (datalen[i] == ',') {
|
||||
break;
|
||||
}
|
||||
if (i >= sizeof(datalen)) {
|
||||
at_conn_hal_err( "Too long length of data.datalen is %s \r\n", datalen);
|
||||
return;
|
||||
}
|
||||
if (datalen[i] > '9' || datalen[i] < '0') {
|
||||
at_conn_hal_err( "Invalid len string!!!, datalen is %s \r\n", datalen);
|
||||
return;
|
||||
}
|
||||
i++;
|
||||
} while (1);
|
||||
|
||||
/* len: string to number */
|
||||
for (j = 0; j < i; j++) {
|
||||
len = len * 10 + datalen[j] - '0';
|
||||
}
|
||||
|
||||
/*get ip addr and port*/
|
||||
i = 0;
|
||||
do {
|
||||
at_read((char *)&ipaddr[i], 1);
|
||||
if (ipaddr[i] == ':') {
|
||||
break;
|
||||
}
|
||||
if (i >= sizeof(ipaddr)) {
|
||||
at_conn_hal_err( "Too long length of ipaddr.ipaddr is %s \r\n", ipaddr);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!((ipaddr[i] <= '9' && ipaddr[i] >= '0') || ipaddr[i] == '.')) {
|
||||
at_conn_hal_err( "Invalid ipaddr string!!!, ipaddr is %s \r\n", ipaddr);
|
||||
return;
|
||||
}
|
||||
i++;
|
||||
} while (1);
|
||||
|
||||
ipaddr[i] = 0;
|
||||
|
||||
i = 0;
|
||||
do {
|
||||
at_read((char *)&port[i], 1);
|
||||
if (port[i] == '\r') {
|
||||
break;
|
||||
}
|
||||
if (i >= sizeof(port)) {
|
||||
at_conn_hal_err( "Too long length of remote port.port is %s \r\n", port);
|
||||
return;
|
||||
}
|
||||
|
||||
if (port[i] > '9' || port[i] < '0') {
|
||||
at_conn_hal_err( "Invalid ipaddr string!!!, port is %s \r\n", port);
|
||||
return;
|
||||
}
|
||||
i++;
|
||||
} while (1);
|
||||
|
||||
port[i] = 0;
|
||||
|
||||
/*eat \n char*/
|
||||
at_read((char *)&unusesymbol, 1);
|
||||
|
||||
for (j = 0; j < i; j++) {
|
||||
remoteport = remoteport * 10 + port[j] - '0';
|
||||
}
|
||||
|
||||
#ifdef PLATFORM_HAS_DYNMEM
|
||||
/* Prepare socket data */
|
||||
recvdata = (char *)HAL_Malloc(len);
|
||||
#else
|
||||
if (len <= SIM800_MAX_PAYLOAD_SIZE) {
|
||||
recvdata = (char *)payload;
|
||||
}
|
||||
#endif
|
||||
if (!recvdata) {
|
||||
at_conn_hal_err( "Error: %s %d out of memory.", __func__, __LINE__);
|
||||
return;
|
||||
}
|
||||
|
||||
memset(recvdata, 0, len);
|
||||
|
||||
at_read(recvdata, len);
|
||||
|
||||
if (g_link[linkid].fd >= 0) {
|
||||
param.fd = g_link[linkid].fd;
|
||||
param.data = recvdata;
|
||||
param.datalen = len;
|
||||
param.remote_ip = (char *)ipaddr;
|
||||
param.remote_port = remoteport;
|
||||
|
||||
/* TODO get recv data src ip and port*/
|
||||
if (IOT_ATM_Input(¶m) != 0) {
|
||||
at_conn_hal_err( " %s socket %d get data len %d fail to post to at_conn, drop it\n",
|
||||
__func__, g_link[linkid].fd, len);
|
||||
}
|
||||
}
|
||||
at_conn_hal_debug( "%s socket data on link %d with length %d posted to at_conn\n",
|
||||
__func__, linkid, len);
|
||||
#ifdef PLATFORM_HAS_DYNMEM
|
||||
HAL_Free(recvdata);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
static int sim800_send_with_retry(const char *cmd, int cmdlen, bool delimiter, const char *data,
|
||||
int datalen, char *rspbuf, int buflen, const char *expectrsp)
|
||||
{
|
||||
int retry = 0;
|
||||
|
||||
if (NULL == cmd || 0 == cmdlen || NULL == rspbuf ||
|
||||
0 == buflen || NULL == expectrsp) {
|
||||
at_conn_hal_err("Invalid input %s %d\r\n", __func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
while(true) {
|
||||
retry++;
|
||||
memset(rspbuf, 0, buflen);
|
||||
at_send_wait_reply(cmd, cmdlen, delimiter, data, datalen, rspbuf, buflen, NULL);
|
||||
if (strstr(rspbuf, expectrsp) == NULL) {
|
||||
if (retry > SIM800_RETRY_MAX) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
at_conn_hal_err("%s %d cmd %s failed rsp %s retry count %d\r\n", __func__, __LINE__, cmd,rspbuf, retry);
|
||||
HAL_SleepMs(50);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sim800_uart_selfadaption(const char *command, const char *rsp, uint32_t rsplen)
|
||||
{
|
||||
char *buffer = g_rsp;
|
||||
|
||||
if (NULL == command || NULL == rsp || 0 == rsplen) {
|
||||
at_conn_hal_err( "invalid input %s %d\r\n", __FILE__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sim800_send_with_retry(command, strlen(command), true, NULL, 0,
|
||||
buffer, SIM800_DEFAULT_RSP_LEN, rsp) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sim800_uart_init(void)
|
||||
{
|
||||
int ret = 0;
|
||||
char *cmd = g_cmd;
|
||||
char *rsp = g_rsp;
|
||||
|
||||
/* uart baudrate self adaption*/
|
||||
ret = sim800_uart_selfadaption(AT_CMD_TEST, AT_CMD_TEST_RESULT, strlen(AT_CMD_TEST_RESULT));
|
||||
if (ret) {
|
||||
at_conn_hal_err( "sim800_uart_selfadaption fail \r\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
memset(rsp, 0, SIM800_DEFAULT_RSP_LEN);
|
||||
/*turn off echo*/
|
||||
at_send_wait_reply(AT_CMD_ECHO_OFF, strlen(AT_CMD_ECHO_OFF), true, NULL, 0,
|
||||
rsp, SIM800_DEFAULT_RSP_LEN, NULL);
|
||||
if (strstr(rsp, SIM800_AT_CMD_SUCCESS_RSP) == NULL) {
|
||||
at_conn_hal_err( "%s %d failed rsp %s\r\n", __func__, __LINE__, rsp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(cmd, 0, SIM800_DEFAULT_CMD_LEN);
|
||||
memset(rsp, 0, SIM800_DEFAULT_RSP_LEN);
|
||||
/*set baudrate 115200*/
|
||||
HAL_Snprintf(cmd, SIM800_DEFAULT_CMD_LEN - 1, "%s=%d", AT_CMD_BAUDRATE_SET, AT_UART_BAUDRATE);
|
||||
at_send_wait_reply(cmd, strlen(cmd), true, NULL, 0, rsp, SIM800_DEFAULT_RSP_LEN, NULL);
|
||||
if (strstr(rsp, SIM800_AT_CMD_SUCCESS_RSP) == NULL) {
|
||||
at_conn_hal_err( "%s %d failed rsp %s\r\n", __func__, __LINE__, rsp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(cmd, 0, SIM800_DEFAULT_CMD_LEN);
|
||||
memset(rsp, 0, SIM800_DEFAULT_RSP_LEN);
|
||||
/*turn off flow control*/
|
||||
HAL_Snprintf(cmd, SIM800_DEFAULT_CMD_LEN - 1, "%s=%d,%d", AT_CMD_FLOW_CONTROL, 0, 0);
|
||||
at_send_wait_reply(cmd, strlen(cmd), true, NULL, 0, rsp, SIM800_DEFAULT_RSP_LEN, NULL);
|
||||
if (strstr(rsp, SIM800_AT_CMD_SUCCESS_RSP) == NULL) {
|
||||
at_conn_hal_err( "%s %d failed rsp %s\r\n", __func__, __LINE__, rsp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(rsp, 0, SIM800_DEFAULT_RSP_LEN);
|
||||
/*save configuration */
|
||||
at_send_wait_reply(AT_CMD_SAVE_CONFIG, strlen(AT_CMD_SAVE_CONFIG), true, NULL, 0,
|
||||
rsp, SIM800_DEFAULT_RSP_LEN, NULL);
|
||||
if (strstr(rsp, SIM800_AT_CMD_SUCCESS_RSP) == NULL) {
|
||||
at_conn_hal_err( "%s %d failed rsp %s\r\n", __func__, __LINE__, rsp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sim800_gprs_status_check(void)
|
||||
{
|
||||
char *rsp = g_rsp;
|
||||
|
||||
/*sim card status check*/
|
||||
if (sim800_send_with_retry(AT_CMD_SIM_PIN_CHECK, strlen(AT_CMD_SIM_PIN_CHECK), true,
|
||||
NULL, 0, rsp, SIM800_DEFAULT_RSP_LEN, SIM800_AT_CMD_SUCCESS_RSP) < 0) {
|
||||
at_conn_hal_err("sim card status check failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(rsp, 0, SIM800_DEFAULT_RSP_LEN);
|
||||
/*Signal quaility check*/
|
||||
at_send_wait_reply(AT_CMD_SIGNAL_QUALITY_CHECK, strlen(AT_CMD_SIGNAL_QUALITY_CHECK), true,
|
||||
NULL, 0, rsp, SIM800_DEFAULT_RSP_LEN, NULL);
|
||||
if (strstr(rsp, SIM800_AT_CMD_SUCCESS_RSP) == NULL) {
|
||||
at_conn_hal_err( "%s %d failed rsp %s\r\n", __func__, __LINE__, rsp);
|
||||
return -1;
|
||||
}
|
||||
at_conn_hal_info( "signal quality is %s \r\n", rsp);
|
||||
|
||||
memset(rsp, 0, SIM800_DEFAULT_RSP_LEN);
|
||||
/*network registration check*/
|
||||
at_send_wait_reply(AT_CMD_NETWORK_REG_CHECK, strlen(AT_CMD_NETWORK_REG_CHECK), true,
|
||||
NULL, 0, rsp, SIM800_DEFAULT_RSP_LEN, NULL);
|
||||
if (strstr(rsp, SIM800_AT_CMD_SUCCESS_RSP) == NULL) {
|
||||
at_conn_hal_err( "%s %d failed rsp %s\r\n", __func__, __LINE__, rsp);
|
||||
return -1;
|
||||
}
|
||||
at_conn_hal_info( "network registration is %s \r\n", rsp);
|
||||
|
||||
|
||||
memset(rsp, 0, SIM800_DEFAULT_RSP_LEN);
|
||||
/*GPRS attach check*/
|
||||
at_send_wait_reply(AT_CMD_GPRS_ATTACH_CHECK, strlen(AT_CMD_GPRS_ATTACH_CHECK),true,
|
||||
NULL, 0, rsp, SIM800_DEFAULT_RSP_LEN, NULL);
|
||||
if (strstr(rsp, SIM800_AT_CMD_SUCCESS_RSP) == NULL) {
|
||||
at_conn_hal_err( "%s %d failed rsp %s\r\n", __func__, __LINE__, rsp);
|
||||
return -1;
|
||||
}
|
||||
at_conn_hal_info( "gprs attach check %s \r\n", rsp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sim800_gprs_ip_init(void)
|
||||
{
|
||||
char *cmd = g_cmd;
|
||||
char *rsp = g_rsp;
|
||||
|
||||
/*Deactivate GPRS PDP Context*/
|
||||
if (sim800_send_with_retry(AT_CMD_GPRS_PDP_DEACTIVE, strlen(AT_CMD_GPRS_PDP_DEACTIVE), true,
|
||||
NULL, 0, rsp, SIM800_DEFAULT_RSP_LEN, SIM800_AT_CMD_SUCCESS_RSP) < 0) {
|
||||
at_conn_hal_err("Deactivate GPRS PDP Context failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*set multi ip connection mode*/
|
||||
memset(cmd, 0, SIM800_DEFAULT_CMD_LEN);
|
||||
memset(rsp, 0, SIM800_DEFAULT_RSP_LEN);
|
||||
HAL_Snprintf(cmd, SIM800_DEFAULT_CMD_LEN - 1, "%s=%d", AT_CMD_MULTI_IP_CONNECTION, 1);
|
||||
at_send_wait_reply(cmd, strlen(cmd), true, NULL, 0,
|
||||
rsp, SIM800_DEFAULT_RSP_LEN, NULL);
|
||||
if (strstr(rsp, SIM800_AT_CMD_SUCCESS_RSP) == NULL) {
|
||||
at_conn_hal_err( "%s %d failed rsp %s\r\n", __func__, __LINE__, rsp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*not prompt echo > when sending data*/
|
||||
memset(rsp, 0, SIM800_DEFAULT_RSP_LEN);
|
||||
memset(cmd, 0, SIM800_DEFAULT_CMD_LEN);
|
||||
HAL_Snprintf(cmd, SIM800_DEFAULT_CMD_LEN - 1, "%s=%d", AT_CMD_SEND_DATA_PROMPT_SET, 0);
|
||||
at_send_wait_reply(cmd, strlen(cmd), true, NULL, 0,
|
||||
rsp, SIM800_DEFAULT_RSP_LEN, NULL);
|
||||
if (strstr(rsp, SIM800_AT_CMD_SUCCESS_RSP) == NULL) {
|
||||
at_conn_hal_err( "%s %d failed rsp %s\r\n", __func__, __LINE__, rsp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*Show Remote ip and port when receive data*/
|
||||
memset(rsp, 0, SIM800_DEFAULT_RSP_LEN);
|
||||
memset(cmd, 0, SIM800_DEFAULT_CMD_LEN);
|
||||
HAL_Snprintf(cmd, SIM800_DEFAULT_CMD_LEN - 1, "%s=%d", AT_CMD_RECV_DATA_FORMAT_SET, 1);
|
||||
at_send_wait_reply(cmd, strlen(cmd), true, NULL, 0, rsp, SIM800_DEFAULT_RSP_LEN, NULL);
|
||||
if (strstr(rsp, SIM800_AT_CMD_SUCCESS_RSP) == NULL) {
|
||||
at_conn_hal_err( "%s %d failed rsp %s\r\n", __func__, __LINE__, rsp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sim800_gprs_got_ip(void)
|
||||
{
|
||||
char *rsp = g_rsp;
|
||||
atcmd_config_t atcmd_config = {NULL, AT_RECV_PREFIX, NULL};
|
||||
|
||||
/*start gprs stask*/
|
||||
if (sim800_send_with_retry(AT_CMD_START_TASK, strlen(AT_CMD_START_TASK), true,
|
||||
NULL, 0, rsp, SIM800_DEFAULT_RSP_LEN, SIM800_AT_CMD_SUCCESS_RSP) < 0) {
|
||||
at_conn_hal_err("%s %d failed rsp %s\r\n", __func__, __LINE__, rsp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*bring up wireless connectiong with gprs*/
|
||||
memset(rsp, 0, SIM800_DEFAULT_RSP_LEN);
|
||||
at_send_wait_reply(AT_CMD_BRING_UP_GPRS_CONNECT, strlen(AT_CMD_BRING_UP_GPRS_CONNECT), true,
|
||||
NULL, 0, rsp, SIM800_DEFAULT_RSP_LEN, NULL);
|
||||
if (strstr(rsp, SIM800_AT_CMD_SUCCESS_RSP) == NULL) {
|
||||
at_conn_hal_err( "%s %d failed rsp %s\r\n", __func__, __LINE__, rsp);
|
||||
}
|
||||
|
||||
/*try to got ip*/
|
||||
memset(rsp, 0, SIM800_DEFAULT_RSP_LEN);
|
||||
at_send_wait_reply(AT_CMD_GOT_LOCAL_IP, strlen(AT_CMD_GOT_LOCAL_IP), true, NULL, 0,
|
||||
rsp, SIM800_DEFAULT_RSP_LEN, &atcmd_config);
|
||||
if (strstr(rsp, SIM800_AT_CMD_FAIL_RSP) != NULL) {
|
||||
at_conn_hal_err( "%s %d failed rsp %s\r\n", __func__, __LINE__, rsp);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sim800_gprs_get_ip_only()
|
||||
{
|
||||
char *rsp = g_rsp;
|
||||
atcmd_config_t atcmd_config = {NULL, AT_RECV_PREFIX, NULL};
|
||||
|
||||
memset(rsp, 0, SIM800_DEFAULT_RSP_LEN);
|
||||
at_send_wait_reply(AT_CMD_GOT_LOCAL_IP, strlen(AT_CMD_GOT_LOCAL_IP), true,
|
||||
NULL, 0, rsp, SIM800_DEFAULT_RSP_LEN, &atcmd_config);
|
||||
if (strstr(rsp, SIM800_AT_CMD_FAIL_RSP) != NULL) {
|
||||
at_conn_hal_err( "%s %d failed rsp %s\r\n", __func__, __LINE__, rsp);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int HAL_AT_CONN_Init(void)
|
||||
{
|
||||
int ret = 0;
|
||||
uint32_t linknum = 0;
|
||||
|
||||
if (inited) {
|
||||
at_conn_hal_info( "sim800 gprs module have already inited \r\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
memset(g_pcdomain_rsp , 0, SIM800_DOMAIN_RSP_MAX_LEN);
|
||||
|
||||
if (NULL == (g_link_mutex = HAL_MutexCreate())) {
|
||||
at_conn_hal_err( "Creating link mutex failed (%s %d).", __func__, __LINE__);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (NULL == (g_domain_mutex = HAL_MutexCreate())) {
|
||||
at_conn_hal_err( "Creating link mutex failed (%s %d).", __func__, __LINE__);
|
||||
goto err;
|
||||
}
|
||||
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
if (NULL == (g_domain_sem = HAL_SemaphoreCreate())) {
|
||||
at_conn_hal_err( "Creating domain mutex failed (%s %d).", __func__, __LINE__);
|
||||
goto err;
|
||||
}
|
||||
#endif
|
||||
|
||||
memset(g_link, 0, sizeof(g_link));
|
||||
|
||||
for (linknum = 0; linknum < SIM800_MAX_LINK_NUM; linknum++) {
|
||||
g_link[linknum].fd = -1;
|
||||
}
|
||||
|
||||
ret = sim800_uart_init();
|
||||
if (ret) {
|
||||
at_conn_hal_err( "%s %d failed \r\n", __func__, __LINE__);
|
||||
goto err;
|
||||
}
|
||||
|
||||
ret = sim800_gprs_status_check();
|
||||
if (ret) {
|
||||
at_conn_hal_err( "%s %d failed \r\n", __func__, __LINE__);
|
||||
goto err;
|
||||
}
|
||||
|
||||
ret = sim800_gprs_ip_init();
|
||||
if (ret) {
|
||||
at_conn_hal_err( "%s %d failed \r\n", __func__, __LINE__);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* reg oob for domain and packet input*/
|
||||
at_register_callback(AT_CMD_DOMAIN_RSP, AT_RECV_PREFIX, g_pcdomain_buf, SIM800_DOMAIN_RSP_MAX_LEN,
|
||||
sim800_gprs_domain_rsp_callback, NULL);
|
||||
at_register_callback(AT_CMD_DATA_RECV, NULL, NULL, 0, sim800_gprs_module_socket_data_handle, NULL);
|
||||
ret = sim800_gprs_got_ip();
|
||||
if (ret) {
|
||||
at_conn_hal_err( "%s %d failed \r\n", __func__, __LINE__);
|
||||
goto err;
|
||||
}
|
||||
|
||||
inited = 1;
|
||||
|
||||
return 0;
|
||||
err:
|
||||
if (g_link_mutex != NULL) {
|
||||
HAL_MutexDestroy(g_link_mutex);
|
||||
}
|
||||
|
||||
if (g_domain_mutex != NULL) {
|
||||
HAL_MutexDestroy(g_domain_mutex);
|
||||
}
|
||||
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
if (g_domain_sem != NULL) {
|
||||
HAL_SemaphoreDestroy(g_domain_sem);
|
||||
}
|
||||
#endif
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int HAL_AT_CONN_Deinit()
|
||||
{
|
||||
if (!inited) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
HAL_MutexDestroy(g_link_mutex);
|
||||
inited = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef PLATFORM_HAS_OS
|
||||
static uint64_t _get_time_ms(void)
|
||||
{
|
||||
return HAL_UptimeMs();
|
||||
}
|
||||
|
||||
static uint64_t _time_left(uint64_t t_end, uint64_t t_now)
|
||||
{
|
||||
uint64_t t_left;
|
||||
|
||||
if (t_end > t_now) {
|
||||
t_left = t_end - t_now;
|
||||
} else {
|
||||
t_left = 0;
|
||||
}
|
||||
|
||||
return t_left;
|
||||
}
|
||||
#endif
|
||||
|
||||
int HAL_AT_CONN_DomainToIp(char *domain, char ip[16])
|
||||
{
|
||||
char *pccmd = NULL;
|
||||
char *head = NULL;
|
||||
char *end = NULL;
|
||||
char *rsp = g_rsp;
|
||||
int count = 0;
|
||||
#ifndef PLATFORM_HAS_OS
|
||||
uint64_t t_end, t_left;
|
||||
#endif
|
||||
|
||||
if (!inited) {
|
||||
at_conn_hal_err( "%s sim800 gprs module haven't init yet \r\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (NULL == domain || NULL == ip) {
|
||||
at_conn_hal_err( "invalid input at %s \r\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strlen(domain) > SIM800_DOMAIN_MAX_LEN) {
|
||||
at_conn_hal_err( "domain length oversize at %s \r\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
pccmd = g_pccmd;
|
||||
if (NULL == pccmd) {
|
||||
at_conn_hal_err( "fail to malloc memory %d at %s \r\n", SIM800_DOMAIN_CMD_LEN, __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(pccmd, 0, SIM800_DOMAIN_CMD_LEN);
|
||||
HAL_Snprintf(pccmd, SIM800_DOMAIN_CMD_LEN - 1, "%s=%s", AT_CMD_DOMAIN_TO_IP, domain);
|
||||
|
||||
HAL_MutexLock(g_domain_mutex);
|
||||
restart:
|
||||
count++;
|
||||
if (count > SIM800_RETRY_MAX) {
|
||||
at_conn_hal_err( "domain to ip retry failed!\r\n");
|
||||
HAL_MutexUnlock(g_domain_mutex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(rsp, 0, SIM800_DEFAULT_RSP_LEN);
|
||||
at_send_wait_reply(pccmd, strlen(pccmd), true, NULL, 0, rsp,
|
||||
SIM800_DEFAULT_RSP_LEN, NULL);
|
||||
if (strstr(rsp, SIM800_AT_CMD_SUCCESS_RSP) == NULL) {
|
||||
at_conn_hal_err( "%s %d failed rsp %s\r\n", __func__, __LINE__, rsp);
|
||||
goto err;
|
||||
}
|
||||
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
HAL_SemaphoreWait(g_domain_sem, SIM800_WAIT_MAX_MS);
|
||||
#else
|
||||
t_end = _get_time_ms() + SIM800_WAIT_MAX_MS;
|
||||
while(!g_domain_mark) {
|
||||
at_yield(NULL, 0, NULL, 100);
|
||||
|
||||
t_left = _time_left(t_end, _get_time_ms());
|
||||
if (0 == t_left) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
g_domain_mark = 0;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* formate is :
|
||||
+CDNSGIP: 1,"www.baidu.com","183.232.231.173","183.232.231.172"
|
||||
or :
|
||||
+CDNSGIP: 0,8
|
||||
*/
|
||||
if ((head = strstr(g_pcdomain_rsp, domain)) == NULL) {
|
||||
at_conn_hal_err( "invalid domain rsp %s at %d\r\n", g_pcdomain_rsp, __LINE__);
|
||||
goto err;
|
||||
}
|
||||
|
||||
head += (strlen(domain) + 3);
|
||||
if ((end = strstr(head, "\"")) == NULL) {
|
||||
at_conn_hal_err( "invalid domain rsp head is %s at %d\r\n", head, __LINE__);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if ((end - head) > 15 || (end - head) < 7) {
|
||||
at_conn_hal_err( "invalid domain rsp head is %s at %d\r\n", head, __LINE__);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* We find a good IP, save it. */
|
||||
memcpy(ip, head, end - head);
|
||||
ip[end - head] = '\0';
|
||||
memset(g_pcdomain_rsp, 0, SIM800_DOMAIN_RSP_MAX_LEN);
|
||||
HAL_MutexUnlock(g_domain_mutex);
|
||||
|
||||
return 0;
|
||||
err:
|
||||
if (sim800_gprs_get_ip_only() != 0) {
|
||||
sim800_gprs_ip_init();
|
||||
sim800_gprs_got_ip();
|
||||
goto restart;
|
||||
}
|
||||
|
||||
memset(g_pcdomain_rsp, 0, SIM800_DOMAIN_RSP_MAX_LEN);
|
||||
HAL_MutexUnlock(g_domain_mutex);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int HAL_AT_CONN_Start(at_conn_t *conn)
|
||||
{
|
||||
int linkid = 0;
|
||||
char *pccmd = NULL;
|
||||
char *rsp = g_rsp;
|
||||
atcmd_config_t atcmd_config_client = { NULL, AT_CMD_CLIENT_CONNECT_OK, AT_CMD_CLIENT_CONNECT_FAIL};
|
||||
|
||||
if (!inited) {
|
||||
at_conn_hal_err( "%s sim800 gprs module haven't init yet \r\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!conn || !conn->addr) {
|
||||
at_conn_hal_err( "%s %d - invalid input \r\n", __func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
HAL_MutexLock(g_link_mutex);
|
||||
for (linkid = 0; linkid < SIM800_MAX_LINK_NUM; linkid++) {
|
||||
if (g_link[linkid].fd >= 0) {
|
||||
continue;
|
||||
}
|
||||
g_link[linkid].fd = conn->fd;
|
||||
break;
|
||||
}
|
||||
HAL_MutexUnlock(g_link_mutex);
|
||||
|
||||
if (linkid >= SIM800_MAX_LINK_NUM) {
|
||||
at_conn_hal_err( "No link available for now, %s failed. \r\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
pccmd = g_pccmd;
|
||||
if (NULL == pccmd) {
|
||||
at_conn_hal_err( "fail to malloc %d at %s \r\n", SIM800_CONN_CMD_LEN, __func__);
|
||||
goto err;
|
||||
}
|
||||
memset(pccmd, 0, SIM800_CONN_CMD_LEN);
|
||||
memset(rsp, 0, SIM800_DEFAULT_RSP_LEN);
|
||||
|
||||
switch (conn->type) {
|
||||
case TCP_CLIENT:
|
||||
HAL_Snprintf(pccmd, SIM800_CONN_CMD_LEN - 1, "%s=%d,\"TCP\",\"%s\",%d", AT_CMD_START_CLIENT_CONN, linkid, conn->addr,
|
||||
conn->r_port);
|
||||
|
||||
at_send_wait_reply(pccmd, strlen(pccmd), true, NULL, 0, rsp, SIM800_DEFAULT_RSP_LEN,
|
||||
&atcmd_config_client);
|
||||
if (strstr(rsp, AT_CMD_CLIENT_CONNECT_FAIL) != NULL) {
|
||||
at_conn_hal_err( "pccmd %s fail, rsp %s \r\n", pccmd, rsp);
|
||||
goto err;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
at_conn_hal_err( "sim800 gprs module connect type %d not support \r\n", conn->type);
|
||||
goto err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
err:
|
||||
HAL_MutexLock(g_link_mutex);
|
||||
g_link[linkid].fd = -1;
|
||||
HAL_MutexUnlock(g_link_mutex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int HAL_AT_CONN_Close(int fd, int32_t remote_port)
|
||||
{
|
||||
int linkid = 0;
|
||||
int ret = 0;
|
||||
char *cmd = g_cmd;
|
||||
char *rsp = g_rsp;
|
||||
|
||||
if (!inited) {
|
||||
at_conn_hal_err( "%s sim800 gprs module haven't init yet \r\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
linkid = fd_to_linkid(fd);
|
||||
if (linkid < 0 || linkid >= SIM800_MAX_LINK_NUM) {
|
||||
at_conn_hal_err( "No connection found for fd (%d) in %s \r\n", fd, __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(cmd, 0, SIM800_DEFAULT_CMD_LEN);
|
||||
memset(rsp, 0, SIM800_DEFAULT_RSP_LEN);
|
||||
HAL_Snprintf(cmd, SIM800_DEFAULT_CMD_LEN - 1, "%s=%d", AT_CMD_STOP_CONN, linkid);
|
||||
at_send_wait_reply(cmd, strlen(cmd), true, NULL, 0,
|
||||
rsp, SIM800_DEFAULT_RSP_LEN, NULL);
|
||||
if (strstr(rsp, SIM800_AT_CMD_SUCCESS_RSP) == NULL) {
|
||||
at_conn_hal_err( "cmd %s rsp is %s \r\n", cmd, rsp);
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
HAL_MutexLock(g_link_mutex);
|
||||
g_link[linkid].fd = -1;
|
||||
HAL_MutexUnlock(g_link_mutex);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int HAL_AT_CONN_Send(int fd,
|
||||
uint8_t *data,
|
||||
uint32_t len,
|
||||
char remote_ip[16],
|
||||
int32_t remote_port,
|
||||
int32_t timeout)
|
||||
{
|
||||
int linkid;
|
||||
char *cmd = g_cmd;
|
||||
char *rsp = g_rsp;
|
||||
|
||||
if (!inited) {
|
||||
at_conn_hal_err( "%s sim800 gprs module haven't init yet \r\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
linkid = fd_to_linkid(fd);
|
||||
if (linkid < 0 || linkid >= SIM800_MAX_LINK_NUM) {
|
||||
at_conn_hal_err( "No connection found for fd (%d) in %s \r\n", fd, __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(cmd, 0, SIM800_DEFAULT_CMD_LEN);
|
||||
HAL_Snprintf(cmd, SIM800_DEFAULT_CMD_LEN - 1, "%s=%d,%d", AT_CMD_SEND_DATA, linkid, len);
|
||||
|
||||
if (sim800_send_with_retry((const char *)cmd, strlen(cmd), true, (const char *)data, len,
|
||||
rsp, SIM800_DEFAULT_RSP_LEN, SIM800_AT_CMD_SUCCESS_RSP) < 0) {
|
||||
at_conn_hal_err("cmd %s rsp %s at %s %d failed \r\n", cmd, rsp, __func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user