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:
945
components/connectivity/iotkit-embedded-3.0.1/3rdparty/wrappers/atm/at_mqtt/mqtt_ica.c
vendored
Normal file
945
components/connectivity/iotkit-embedded-3.0.1/3rdparty/wrappers/atm/at_mqtt/mqtt_ica.c
vendored
Normal file
@@ -0,0 +1,945 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "infra_config.h"
|
||||
#include "mqtt_api.h"
|
||||
|
||||
#include "at_wrapper.h"
|
||||
#include "at_parser.h"
|
||||
#include "at_api.h"
|
||||
#include "at_mqtt.h"
|
||||
|
||||
#define AT_ICA_MQTT_MQTTMODE "AT+IMQTTMODE"
|
||||
#define AT_ICA_MQTT_MQTTOPEN "AT+IMQTTOPEN"
|
||||
#define AT_ICA_MQTT_MQTTAUTH "AT+IMQTTAUTH"
|
||||
#define AT_ICA_MQTT_MQTTPARA "AT+IMQTTPARA"
|
||||
#define AT_ICA_MQTT_MQTTCONN "AT+IMQTTCONN"
|
||||
#define AT_ICA_MQTT_MQTTPUB "AT+IMQTTPUB"
|
||||
#define AT_ICA_MQTT_MQTTPUBIN "AT+IMQTTPUBIN"
|
||||
#define AT_ICA_MQTT_MQTTSUB "AT+IMQTTSUB"
|
||||
#define AT_ICA_MQTT_MQTTUNSUB "AT+IMQTTUNSUB"
|
||||
#define AT_ICA_MQTT_MQTTSTATE "AT+IMQTTSTATE"
|
||||
#define AT_ICA_MQTT_MQTTDISCONN "AT+IMQTTDISCONN"
|
||||
#define AT_ICA_MQTT_MQTTDBG "AT+IMQTTDBG"
|
||||
|
||||
#define AT_ICA_MQTT_MQTTRCV "+IMQTT"
|
||||
#define AT_ICA_MQTT_MQTTERROR "+CME"
|
||||
#define AT_ICA_MQTT_MQTTOK "OK"
|
||||
#define AT_ICA_MQTT_MQTTRCVPUB "+IMQTTRCVPUB"
|
||||
#define AT_ICA_MQTT_MQTTRCVPUBIN "+IMQTTRCVPUBIN"
|
||||
#define AT_ICA_MQTT_MQTTPINGRSP "+IMQTTPINGRSP"
|
||||
#define AT_ICA_MQTT_MQTTAUTHRSP "+IMQTTAUTH"
|
||||
#define AT_ICA_MQTT_MQTTPUBRSP "+IMQTTPUB"
|
||||
#define AT_ICA_MQTT_MQTTSUBRSP "+IMQTTSUB"
|
||||
#define AT_ICA_MQTT_MQTTUNSUBRSP "+IMQTTUNSUB"
|
||||
#define AT_ICA_MQTT_MQTTSTATERSP "+IMQTTSTATE"
|
||||
|
||||
#define AT_ICA_MQTT_POSTFIX "\r\n"
|
||||
|
||||
#define AT_MQTT_WAIT_FOREVER 0xffffffffu
|
||||
|
||||
#define AT_MQTT_CMD_MAX_LEN 400
|
||||
#define AT_MQTT_CMD_SUCCESS_RSP "OK"
|
||||
#define AT_MQTT_CMD_FAIL_RSP "FAIL"
|
||||
#define AT_MQTT_CMD_ERROR_RSP "ERROR"
|
||||
#define AT_MQTT_SUBSCRIBE_FAIL 128
|
||||
#define AT_MQTT_RSP_MAX_LEN (CONFIG_MQTT_MESSAGE_MAXLEN + CONFIG_MQTT_TOPIC_MAXLEN + 20)
|
||||
|
||||
#define AT_MQTT_WAIT_TIMEOUT 10*1000
|
||||
|
||||
#define mal_err(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
|
||||
#ifdef INFRA_MEM_STATS
|
||||
#include "infra_mem_stats.h"
|
||||
#define AT_MQTT_ICA_MALLOC(size) LITE_malloc(size, MEM_MAGIC, "mal.ica")
|
||||
#define AT_MQTT_ICA_FREE(ptr) LITE_free(ptr)
|
||||
#else
|
||||
#define AT_MQTT_ICA_MALLOC(size) HAL_Malloc(size)
|
||||
#define AT_MQTT_ICA_FREE(ptr) {HAL_Free((void *)ptr);ptr = NULL;}
|
||||
#endif
|
||||
|
||||
char at_recv_rsp_buf[AT_MQTT_CMD_MAX_LEN];
|
||||
|
||||
typedef enum {
|
||||
AT_MQTT_IDLE = 0,
|
||||
AT_MQTT_SEND_TYPE_SIMPLE,
|
||||
AT_MQTT_AUTH,
|
||||
AT_MQTT_SUB,
|
||||
AT_MQTT_UNSUB,
|
||||
AT_MQTT_PUB,
|
||||
} at_mqtt_send_type_t;
|
||||
|
||||
int at_ica_mqtt_atsend(char *at_cmd, int timeout_ms);
|
||||
int at_ica_mqtt_client_deinit(void);
|
||||
int at_ica_mqtt_client_init(void);
|
||||
int at_ica_mqtt_client_state(void);
|
||||
int at_ica_mqtt_client_publish(const char *topic, int qos, const char *message);
|
||||
int at_ica_mqtt_client_unsubscribe(const char *topic,
|
||||
unsigned int *mqtt_packet_id,
|
||||
int *mqtt_status);
|
||||
int at_ica_mqtt_client_subscribe(const char *topic,
|
||||
int qos,
|
||||
unsigned int *mqtt_packet_id,
|
||||
int *mqtt_status,
|
||||
int timeout_ms);
|
||||
int at_ica_mqtt_client_conn(char *proKey, char *devName, char *devSecret, int tlsEnable);
|
||||
int at_ica_mqtt_client_auth(char *proKey, char *devName, char *devSecret, int tlsEnable);
|
||||
int at_ica_mqtt_client_disconn(void);
|
||||
|
||||
int HAL_AT_MQTT_Init(iotx_mqtt_param_t *pInitParams)
|
||||
{
|
||||
return at_ica_mqtt_client_init();
|
||||
}
|
||||
|
||||
int HAL_AT_MQTT_Deinit()
|
||||
{
|
||||
return at_ica_mqtt_client_deinit();
|
||||
}
|
||||
|
||||
int HAL_AT_MQTT_Connect(char *proKey, char *devName, char *devSecret)
|
||||
{
|
||||
return at_ica_mqtt_client_conn(proKey, devName, devSecret, 0);
|
||||
}
|
||||
|
||||
int HAL_AT_MQTT_Disconnect(void)
|
||||
{
|
||||
return at_ica_mqtt_client_disconn();
|
||||
}
|
||||
|
||||
int HAL_AT_MQTT_Subscribe(const char *topic, int qos, unsigned int *mqtt_packet_id, int *mqtt_status, int timeout_ms)
|
||||
{
|
||||
return at_ica_mqtt_client_subscribe(topic, qos, mqtt_packet_id, mqtt_status, timeout_ms);
|
||||
}
|
||||
|
||||
int HAL_AT_MQTT_Unsubscribe(const char *topic, unsigned int *mqtt_packet_id, int *mqtt_status)
|
||||
{
|
||||
return at_ica_mqtt_client_unsubscribe(topic, mqtt_packet_id, mqtt_status);
|
||||
}
|
||||
|
||||
int HAL_AT_MQTT_Publish(const char *topic, int qos, const char *message, unsigned int msg_len)
|
||||
{
|
||||
return at_ica_mqtt_client_publish(topic, qos, message);
|
||||
}
|
||||
|
||||
int HAL_AT_MQTT_State(void)
|
||||
{
|
||||
return at_ica_mqtt_client_state();
|
||||
}
|
||||
|
||||
int HAL_AT_MQTT_Connectwifi(char *at_conn_wifi)
|
||||
{
|
||||
#ifdef MAL_ICA_ENABLED
|
||||
char at_cmd[64];
|
||||
/* disconnect before connect to the network */
|
||||
if (at_ica_mqtt_client_disconn() != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(at_cmd, at_conn_wifi, 64);
|
||||
/* connect to the network */
|
||||
if (at_ica_mqtt_atsend(at_cmd, AT_MQTT_WAIT_FOREVER) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
#ifndef PLATFORM_HAS_OS
|
||||
char g_ica_rsp_buff[AT_MQTT_RSP_MAX_LEN];
|
||||
#else
|
||||
static char *g_ica_rsp_buff = NULL;
|
||||
#endif
|
||||
static volatile int g_mqtt_connect_state = IOTX_MC_STATE_INVALID;
|
||||
static volatile at_mqtt_send_type_t g_ica_at_response = AT_MQTT_IDLE;
|
||||
static volatile int g_at_response_result = 0;
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
static void *g_sem_response;
|
||||
#else
|
||||
int g_at_response = 0;
|
||||
#endif
|
||||
static volatile int g_response_msg_number = 0;
|
||||
static int g_response_packetid = 0;
|
||||
static int g_response_status = 0;
|
||||
static int g_public_qos = 0;
|
||||
|
||||
int at_ica_mqtt_atsend(char *at_cmd, int timeout_ms);
|
||||
|
||||
static void at_err_callback(char *at_rsp)
|
||||
{
|
||||
char *temp;
|
||||
int data;
|
||||
|
||||
temp = strtok(at_rsp, ":");
|
||||
temp = strtok(NULL, ":");
|
||||
if ((data = strtol(temp, NULL, 0)) == 3) {
|
||||
g_at_response_result = 0;
|
||||
} else {
|
||||
g_at_response_result = -1;
|
||||
}
|
||||
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
/* notify the sender error; */
|
||||
HAL_SemaphorePost(g_sem_response);
|
||||
#else
|
||||
g_at_response ++;
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
static void at_succ_callback(void)
|
||||
{
|
||||
g_at_response_result = 0;
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
/* notify the sender ok; */
|
||||
HAL_SemaphorePost(g_sem_response);
|
||||
#else
|
||||
g_at_response ++;
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
static void sub_callback(char *at_rsp)
|
||||
{
|
||||
char *temp;
|
||||
|
||||
if (strstr(at_rsp, AT_MQTT_CMD_ERROR_RSP)) {
|
||||
g_at_response_result = -1;
|
||||
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
/* notify the sender fail; */
|
||||
HAL_SemaphorePost(g_sem_response);
|
||||
#else
|
||||
g_at_response ++;
|
||||
#endif
|
||||
return;
|
||||
} else if (NULL != strstr(at_rsp, AT_ICA_MQTT_MQTTSUBRSP)) {
|
||||
/* get status/packet_id */
|
||||
if (NULL != strstr(at_rsp, ",")) {
|
||||
g_at_response_result = 0;
|
||||
|
||||
temp = strtok(at_rsp, ":");
|
||||
|
||||
if (temp != NULL) {
|
||||
temp = strtok(NULL, ",");
|
||||
} else {
|
||||
mal_err("subscribe rsp param invalid 1");
|
||||
g_at_response_result = -1;
|
||||
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
HAL_SemaphorePost(g_sem_response);
|
||||
#else
|
||||
g_at_response ++;
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
if (temp != NULL) {
|
||||
g_response_packetid = strtol(temp, NULL, 0);
|
||||
|
||||
temp = strtok(NULL, "\r\n");
|
||||
} else {
|
||||
mal_err("subscribe rsp param invalid 2");
|
||||
g_at_response_result = -1;
|
||||
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
HAL_SemaphorePost(g_sem_response);
|
||||
#else
|
||||
g_at_response ++;
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
if (temp != NULL) {
|
||||
g_response_status = strtol(temp, NULL, 0);
|
||||
} else {
|
||||
mal_err("subscribe rsp param invalid 3");
|
||||
g_at_response_result = -1;
|
||||
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
HAL_SemaphorePost(g_sem_response);
|
||||
#else
|
||||
g_at_response ++;
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
/* notify the sender ok; */
|
||||
HAL_SemaphorePost(g_sem_response);
|
||||
#else
|
||||
g_at_response ++;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static void unsub_callback(char *at_rsp)
|
||||
{
|
||||
char *temp;
|
||||
if (strstr(at_rsp, AT_MQTT_CMD_ERROR_RSP)) {
|
||||
g_at_response_result = -1;
|
||||
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
/* notify the sender fail; */
|
||||
HAL_SemaphorePost(g_sem_response);
|
||||
#else
|
||||
g_at_response ++;
|
||||
#endif
|
||||
|
||||
return;
|
||||
} else if (NULL != strstr(at_rsp, AT_ICA_MQTT_MQTTUNSUBRSP)) {
|
||||
/* get status/packet_id */
|
||||
if (NULL != strstr(at_rsp, ",")) {
|
||||
g_at_response_result = 0;
|
||||
|
||||
temp = strtok(at_rsp, ":");
|
||||
|
||||
if (temp != NULL) {
|
||||
temp = strtok(NULL, ",");
|
||||
} else {
|
||||
mal_err("subscribe rsp param invalid 1");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (temp != NULL) {
|
||||
g_response_packetid = strtol(temp, NULL, 0);
|
||||
|
||||
temp = strtok(NULL, "\r\n");
|
||||
} else {
|
||||
mal_err("subscribe rsp param invalid 2");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (temp != NULL) {
|
||||
g_response_status = strtol(temp, NULL, 0);
|
||||
} else {
|
||||
mal_err("subscribe rsp param invalid 3");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
mal_err("unsub: %s", g_ica_rsp_buff);
|
||||
mal_err("unsub packetid: %d, sta: %d", g_response_packetid, g_response_status);
|
||||
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
/* notify the sender ok; */
|
||||
HAL_SemaphorePost(g_sem_response);
|
||||
#else
|
||||
g_at_response ++;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static void pub_callback(char *at_rsp)
|
||||
{
|
||||
char *temp;
|
||||
if (strstr(at_rsp, AT_MQTT_CMD_ERROR_RSP)) {
|
||||
g_at_response_result = -1;
|
||||
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
/* notify the sender fail; */
|
||||
HAL_SemaphorePost(g_sem_response);
|
||||
#else
|
||||
g_at_response ++;
|
||||
#endif
|
||||
|
||||
return;
|
||||
} else if (NULL != strstr(at_rsp, AT_ICA_MQTT_MQTTPUBRSP)) {
|
||||
/* get status/packet_id */
|
||||
if ((NULL != strstr(at_rsp, ","))
|
||||
|| (0 == g_public_qos)) {
|
||||
|
||||
temp = strtok(at_rsp, ":");
|
||||
|
||||
if (temp != NULL) {
|
||||
if (0 == g_public_qos) {
|
||||
temp = strtok(NULL, "\r\n");
|
||||
} else {
|
||||
temp = strtok(NULL, ",");
|
||||
}
|
||||
} else {
|
||||
mal_err("public get packetid error");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (temp != NULL) {
|
||||
g_response_packetid = strtol(temp, NULL, 0);
|
||||
} else {
|
||||
mal_err("public parse packetid error");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (0 != g_public_qos) {
|
||||
temp = strtok(NULL, "\r\n");
|
||||
|
||||
if (temp != NULL) {
|
||||
g_response_status = strtol(temp, NULL, 0);
|
||||
} else {
|
||||
mal_err("public parse status error");
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
g_at_response_result = 0;
|
||||
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
/* notify the sender ok; */
|
||||
HAL_SemaphorePost(g_sem_response);
|
||||
#else
|
||||
g_at_response ++;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static void state_change_callback(char *at_rsp)
|
||||
{
|
||||
char *temp;
|
||||
|
||||
if (NULL == at_rsp) {
|
||||
return;
|
||||
}
|
||||
|
||||
temp = strtok(at_rsp, ":");
|
||||
|
||||
if (temp != NULL) {
|
||||
temp = strtok(NULL, "\r\n");
|
||||
|
||||
if (temp != NULL) {
|
||||
g_mqtt_connect_state = strtol(temp, NULL, 0);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
static void recv_data_callback(char *at_rsp)
|
||||
{
|
||||
char *temp = NULL;
|
||||
char *topic_ptr = NULL;
|
||||
char *msg_ptr = NULL;
|
||||
unsigned int msg_len = 0;
|
||||
struct at_mqtt_input param;
|
||||
|
||||
if (NULL == at_rsp) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* try to get msg id */
|
||||
temp = strtok(g_ica_rsp_buff, ":");
|
||||
|
||||
if (temp != NULL) {
|
||||
temp = strtok(NULL, ",");
|
||||
|
||||
if (temp != NULL) {
|
||||
/* packet_id = strtol(temp, NULL, 0); */
|
||||
} else {
|
||||
mal_err("packet id error");
|
||||
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
mal_err("packet id not found");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* try to get topic string */
|
||||
temp = strtok(NULL, "\"");
|
||||
|
||||
if (temp != NULL) {
|
||||
temp[strlen(temp)] = '\0';
|
||||
|
||||
topic_ptr = temp;
|
||||
} else {
|
||||
mal_err("publish topic not found");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* try to get payload string */
|
||||
temp = strtok(NULL, ",");
|
||||
|
||||
if (temp != NULL) {
|
||||
msg_len = strtol(temp, NULL, 0);
|
||||
|
||||
while (*temp++ != '\"');
|
||||
|
||||
msg_ptr = temp;
|
||||
|
||||
msg_ptr[msg_len] = '\0';
|
||||
|
||||
param.topic = topic_ptr;
|
||||
param.topic_len = strlen(topic_ptr);
|
||||
param.message = msg_ptr;
|
||||
param.msg_len = strlen(msg_ptr);
|
||||
|
||||
if (IOT_ATM_Input(¶m) != 0) {
|
||||
mal_err("hand data to uplayer fail!\n");
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
mal_err("publish data not found");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void at_ica_mqtt_client_rsp_callback(void *arg, char *rspinfo, int rsplen)
|
||||
{
|
||||
if (NULL == rspinfo || rsplen == 0) {
|
||||
mal_err("invalid input of rsp callback");
|
||||
return;
|
||||
}
|
||||
if (NULL == g_ica_rsp_buff) {
|
||||
mal_err("g_ica_rsp_buff rsp is NULL");
|
||||
return;
|
||||
}
|
||||
|
||||
if (rsplen > AT_MQTT_RSP_MAX_LEN) {
|
||||
mal_err("rsp len(%d) exceed max len", rsplen);
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(g_ica_rsp_buff, rspinfo, rsplen);
|
||||
g_ica_rsp_buff[rsplen] = '\0';
|
||||
|
||||
if (0 == memcmp(g_ica_rsp_buff,
|
||||
AT_ICA_MQTT_MQTTERROR,
|
||||
strlen(AT_ICA_MQTT_MQTTERROR))) {
|
||||
|
||||
at_err_callback(g_ica_rsp_buff);
|
||||
} else if (0 == memcmp(g_ica_rsp_buff,
|
||||
AT_ICA_MQTT_MQTTRCVPUB,
|
||||
strlen(AT_ICA_MQTT_MQTTRCVPUB))) { /* Receive Publish Data */
|
||||
|
||||
recv_data_callback(g_ica_rsp_buff);
|
||||
} else if (0 == memcmp(g_ica_rsp_buff,
|
||||
AT_ICA_MQTT_MQTTSTATERSP,
|
||||
strlen(AT_ICA_MQTT_MQTTSTATERSP))) { /* Receive Mqtt Status Change */
|
||||
|
||||
state_change_callback(g_ica_rsp_buff);
|
||||
} else {
|
||||
switch (g_ica_at_response) { /* nothing to process */
|
||||
|
||||
case AT_MQTT_IDLE:
|
||||
|
||||
break;
|
||||
|
||||
case AT_MQTT_SEND_TYPE_SIMPLE:
|
||||
|
||||
if (0 == memcmp(g_ica_rsp_buff,
|
||||
AT_MQTT_CMD_SUCCESS_RSP,
|
||||
strlen(AT_MQTT_CMD_SUCCESS_RSP))) {
|
||||
|
||||
at_succ_callback();
|
||||
} else {
|
||||
|
||||
mal_err("invalid success type %s", g_ica_rsp_buff);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case AT_MQTT_AUTH:
|
||||
|
||||
if (0 == memcmp(g_ica_rsp_buff,
|
||||
AT_ICA_MQTT_MQTTAUTHRSP,
|
||||
strlen(AT_ICA_MQTT_MQTTAUTHRSP))) {
|
||||
|
||||
if (NULL != strstr(g_ica_rsp_buff, AT_MQTT_CMD_SUCCESS_RSP)) {
|
||||
at_succ_callback();
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case AT_MQTT_SUB:
|
||||
sub_callback(g_ica_rsp_buff);
|
||||
break;
|
||||
|
||||
case AT_MQTT_UNSUB:
|
||||
unsub_callback(g_ica_rsp_buff);
|
||||
break;
|
||||
|
||||
case AT_MQTT_PUB:
|
||||
pub_callback(g_ica_rsp_buff);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int at_ica_mqtt_client_disconn(void)
|
||||
{
|
||||
char at_cmd[64];
|
||||
|
||||
memset(at_cmd, 0, 64);
|
||||
|
||||
/* connect to the network */
|
||||
HAL_Snprintf(at_cmd,
|
||||
64,
|
||||
"%s\r\n",
|
||||
AT_ICA_MQTT_MQTTDISCONN);
|
||||
|
||||
/* disconnect from server */
|
||||
if (0 != at_ica_mqtt_atsend(at_cmd, AT_MQTT_WAIT_TIMEOUT)) {
|
||||
mal_err("disconnect at command fail");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int at_ica_mqtt_client_auth(char *proKey, char *devName, char *devSecret, int tlsEnable)
|
||||
{
|
||||
char at_cmd[AT_MQTT_CMD_MAX_LEN];
|
||||
|
||||
if ((proKey == NULL) || (devName == NULL) || (devSecret == NULL)) {
|
||||
|
||||
mal_err("auth param should not be NULL");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* set tls mode before auth */
|
||||
if (tlsEnable) {
|
||||
memset(at_cmd, 0, AT_MQTT_CMD_MAX_LEN);
|
||||
|
||||
HAL_Snprintf(at_cmd,
|
||||
AT_MQTT_CMD_MAX_LEN - 1,
|
||||
"%s=%d\r\n",
|
||||
AT_ICA_MQTT_MQTTMODE,
|
||||
1);
|
||||
|
||||
if (0 != at_ica_mqtt_atsend(at_cmd, AT_MQTT_WAIT_TIMEOUT)) {
|
||||
|
||||
mal_err("tls at command fail");
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* submit auth */
|
||||
memset(at_cmd, 0, AT_MQTT_CMD_MAX_LEN);
|
||||
|
||||
HAL_Snprintf(at_cmd,
|
||||
AT_MQTT_CMD_MAX_LEN - 1,
|
||||
"%s=\"%s\",\"%s\",\"%s\"\r\n",
|
||||
AT_ICA_MQTT_MQTTAUTH,
|
||||
proKey, devName, devSecret);
|
||||
|
||||
if (0 != at_ica_mqtt_atsend(at_cmd, AT_MQTT_WAIT_TIMEOUT)) {
|
||||
|
||||
mal_err("auth at command fail");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int at_ica_mqtt_client_conn(char *proKey, char *devName, char *devSecret, int tlsEnable)
|
||||
{
|
||||
char at_cmd[64];
|
||||
|
||||
if ((proKey == NULL) || (devName == NULL) || (devSecret == NULL)) {
|
||||
|
||||
mal_err("conn param should not be NULL");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (0 != at_ica_mqtt_client_auth(proKey, devName, devSecret, tlsEnable)) {
|
||||
|
||||
mal_err("authen fail");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* connect to mqtt server */
|
||||
memset(at_cmd, 0, 64);
|
||||
|
||||
HAL_Snprintf(at_cmd,
|
||||
64,
|
||||
"%s\r\n",
|
||||
AT_ICA_MQTT_MQTTCONN);
|
||||
|
||||
if (0 != at_ica_mqtt_atsend(at_cmd, AT_MQTT_WAIT_TIMEOUT)) {
|
||||
|
||||
mal_err("conn at command fail");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int at_ica_mqtt_client_subscribe(const char *topic,
|
||||
int qos,
|
||||
unsigned int *mqtt_packet_id,
|
||||
int *mqtt_status,
|
||||
int timeout_ms)
|
||||
{
|
||||
char at_cmd[AT_MQTT_CMD_MAX_LEN];
|
||||
|
||||
if ((topic == NULL) || (mqtt_packet_id == NULL) || (mqtt_status == NULL)) {
|
||||
|
||||
mal_err("subscribe param should not be NULL");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(at_cmd, 0, AT_MQTT_CMD_MAX_LEN);
|
||||
|
||||
HAL_Snprintf(at_cmd,
|
||||
AT_MQTT_CMD_MAX_LEN - 1,
|
||||
"%s=\"%s\",%d\r\n",
|
||||
AT_ICA_MQTT_MQTTSUB,
|
||||
topic,
|
||||
qos);
|
||||
|
||||
if (0 != at_ica_mqtt_atsend(at_cmd, timeout_ms)) {
|
||||
mal_err("sub at command fail");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int at_ica_mqtt_client_unsubscribe(const char *topic,
|
||||
unsigned int *mqtt_packet_id,
|
||||
int *mqtt_status)
|
||||
{
|
||||
char at_cmd[AT_MQTT_CMD_MAX_LEN];
|
||||
if ((topic == NULL) || (mqtt_packet_id == NULL) || (mqtt_status == NULL)) {
|
||||
|
||||
mal_err("unsubscribe param should not be NULL");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(at_cmd, 0, AT_MQTT_CMD_MAX_LEN);
|
||||
|
||||
HAL_Snprintf(at_cmd,
|
||||
AT_MQTT_CMD_MAX_LEN - 1,
|
||||
"%s=\"%s\"\r\n",
|
||||
AT_ICA_MQTT_MQTTUNSUB,
|
||||
topic);
|
||||
|
||||
if (0 != at_ica_mqtt_atsend(at_cmd, AT_MQTT_WAIT_TIMEOUT)) {
|
||||
|
||||
mal_err("unsub at command fail");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int at_ica_mqtt_client_publish(const char *topic, int qos, const char *message)
|
||||
{
|
||||
char at_cmd[AT_MQTT_CMD_MAX_LEN] = {0};
|
||||
char msg_convert[AT_MQTT_CMD_MAX_LEN] = {0};
|
||||
char *temp;
|
||||
if ((topic == NULL) || (message == NULL)) {
|
||||
|
||||
mal_err("publish param should not be NULL");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
temp = msg_convert;
|
||||
|
||||
/* for the case of " appeared in the string */
|
||||
while (*message) {
|
||||
if (*message == '\"') {
|
||||
*temp++ = '\\';
|
||||
}
|
||||
|
||||
*temp++ = *message++;
|
||||
}
|
||||
|
||||
HAL_Snprintf(at_cmd,
|
||||
AT_MQTT_CMD_MAX_LEN - 1,
|
||||
"%s=\"%s\",%d,\"%s\"\r\n",
|
||||
AT_ICA_MQTT_MQTTPUB,
|
||||
topic,
|
||||
qos,
|
||||
msg_convert);
|
||||
|
||||
g_public_qos = qos;
|
||||
if (0 != at_ica_mqtt_atsend(at_cmd, AT_MQTT_WAIT_TIMEOUT)) {
|
||||
|
||||
mal_err("pub at command fail");
|
||||
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int at_ica_mqtt_client_state(void)
|
||||
{
|
||||
int state;
|
||||
|
||||
switch(g_mqtt_connect_state){
|
||||
case 0:
|
||||
state = IOTX_MC_STATE_DISCONNECTED;
|
||||
break;
|
||||
case 1:
|
||||
state = IOTX_MC_STATE_CONNECTED;
|
||||
break;
|
||||
default:
|
||||
state = IOTX_MC_STATE_INVALID;
|
||||
break;
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
int at_ica_mqtt_client_init(void)
|
||||
{
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
g_ica_rsp_buff = AT_MQTT_ICA_MALLOC(AT_MQTT_RSP_MAX_LEN);
|
||||
if (NULL == g_ica_rsp_buff) {
|
||||
mal_err("at ica mqtt client malloc buff failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (NULL == (g_sem_response = HAL_SemaphoreCreate())) {
|
||||
if (NULL != g_ica_rsp_buff) {
|
||||
AT_MQTT_ICA_FREE(g_ica_rsp_buff);
|
||||
|
||||
g_ica_rsp_buff = NULL;
|
||||
}
|
||||
mal_err("at ica mqtt client create sem failed");
|
||||
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
memset(g_ica_rsp_buff, 0, AT_MQTT_RSP_MAX_LEN);
|
||||
g_at_response = 0;
|
||||
#endif
|
||||
|
||||
g_mqtt_connect_state = IOTX_MC_STATE_INVALID;
|
||||
|
||||
at_register_callback(AT_ICA_MQTT_MQTTRCV,
|
||||
AT_ICA_MQTT_POSTFIX,
|
||||
at_recv_rsp_buf,
|
||||
AT_MQTT_CMD_MAX_LEN,
|
||||
at_ica_mqtt_client_rsp_callback,
|
||||
NULL);
|
||||
|
||||
at_register_callback(AT_ICA_MQTT_MQTTERROR,
|
||||
AT_ICA_MQTT_POSTFIX,
|
||||
at_recv_rsp_buf,
|
||||
AT_MQTT_CMD_MAX_LEN,
|
||||
at_ica_mqtt_client_rsp_callback,
|
||||
NULL);
|
||||
|
||||
at_register_callback(AT_ICA_MQTT_MQTTOK,
|
||||
AT_ICA_MQTT_POSTFIX,
|
||||
at_recv_rsp_buf,
|
||||
AT_MQTT_CMD_MAX_LEN,
|
||||
at_ica_mqtt_client_rsp_callback,
|
||||
NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int at_ica_mqtt_client_deinit(void)
|
||||
{
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
if (NULL != g_ica_rsp_buff) {
|
||||
AT_MQTT_ICA_FREE(g_ica_rsp_buff);
|
||||
g_ica_rsp_buff = NULL;
|
||||
}
|
||||
HAL_SemaphoreDestroy(g_sem_response);
|
||||
#else
|
||||
memset(g_ica_rsp_buff, 0, AT_MQTT_RSP_MAX_LEN);
|
||||
g_at_response = 0;
|
||||
#endif
|
||||
|
||||
g_mqtt_connect_state = IOTX_MC_STATE_INVALID;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int at_ica_mqtt_atsend(char *at_cmd, int timeout_ms)
|
||||
{
|
||||
if (at_cmd == NULL) {
|
||||
return -1;
|
||||
}
|
||||
/* state error */
|
||||
if (g_ica_at_response != AT_MQTT_IDLE) {
|
||||
|
||||
mal_err("at send state is not idle (%d)", g_ica_at_response);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
mal_err("send: %s", at_cmd);
|
||||
|
||||
if (NULL != strstr(at_cmd, AT_ICA_MQTT_MQTTAUTH)) {
|
||||
g_ica_at_response = AT_MQTT_AUTH;
|
||||
} else if (NULL != strstr(at_cmd, AT_ICA_MQTT_MQTTSUB)) {
|
||||
g_ica_at_response = AT_MQTT_SUB;
|
||||
} else if (NULL != strstr(at_cmd, AT_ICA_MQTT_MQTTUNSUB)) {
|
||||
g_ica_at_response = AT_MQTT_UNSUB;
|
||||
} else if (NULL != strstr(at_cmd, AT_ICA_MQTT_MQTTPUB)) {
|
||||
g_ica_at_response = AT_MQTT_PUB;
|
||||
} else {
|
||||
g_ica_at_response = AT_MQTT_SEND_TYPE_SIMPLE;
|
||||
}
|
||||
|
||||
if (0 != at_send_no_reply(at_cmd, strlen(at_cmd), false)) {
|
||||
|
||||
mal_err("at send raw api fail");
|
||||
|
||||
g_ica_at_response = AT_MQTT_IDLE;
|
||||
|
||||
return -1;
|
||||
}
|
||||
#ifdef PLATFORM_HAS_OS
|
||||
HAL_SemaphoreWait(g_sem_response, timeout_ms);
|
||||
#else
|
||||
if(!g_at_response)
|
||||
{
|
||||
at_yield(NULL, 0, NULL, 500);
|
||||
}
|
||||
else
|
||||
{
|
||||
g_at_response --;
|
||||
}
|
||||
#endif
|
||||
|
||||
g_ica_at_response = AT_MQTT_IDLE;
|
||||
|
||||
return g_at_response_result;
|
||||
}
|
1348
components/connectivity/iotkit-embedded-3.0.1/3rdparty/wrappers/atm/at_mqtt/mqtt_sim800.c
vendored
Normal file
1348
components/connectivity/iotkit-embedded-3.0.1/3rdparty/wrappers/atm/at_mqtt/mqtt_sim800.c
vendored
Normal file
File diff suppressed because it is too large
Load Diff
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