Created
December 9, 2022 13:11
-
-
Save nickfox-taterli/571d80b1232fe4321552bdffb7d0f19b to your computer and use it in GitHub Desktop.
EC616S
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Includes ------------------------------------------------------------------*/ | |
#include "ec616s.h" | |
#include "ec616s_io.h" | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
/* Private typedef -----------------------------------------------------------*/ | |
/* Private define ------------------------------------------------------------*/ | |
/* Private macro -------------------------------------------------------------*/ | |
/* Private variables ---------------------------------------------------------*/ | |
static uint8_t AtCmd[MAX_AT_CMD_SIZE]; | |
static uint8_t RxBuffer[MAX_BUFFER_SIZE]; | |
/* Private function prototypes -----------------------------------------------*/ | |
static EC616S_StatusTypeDef runAtCmd(uint8_t *cmd, uint32_t Length, const uint8_t *Token); | |
/** | |
* @brief Initialize the EC616S module. | |
* IT intitalize the IO to communicate between the MCU and the module, then | |
* test that the modules is working using some basic AT commands. | |
* in case of success the string "OK" is returned inside otherwise | |
* it is an error. | |
* @param None | |
* @retval EC616S_OK on sucess, EC616S_ERROR otherwise. | |
*/ | |
EC616S_StatusTypeDef EC616S_Init(void) | |
{ | |
EC616S_StatusTypeDef Ret; | |
/* Configuration the IO low layer */ | |
EC616S_IO_Init(); | |
/* Disable the Echo mode */ | |
/* Construct the command */ | |
memset(AtCmd, '\0', MAX_AT_CMD_SIZE); | |
sprintf((char *)AtCmd, "ATE0\r\n"); | |
/* Send the command */ | |
Ret = runAtCmd(AtCmd, strlen((char *)AtCmd), (uint8_t *)AT_OK_STRING); | |
/* Exit in case of error */ | |
if (Ret != EC616S_OK) | |
{ | |
return EC616S_ERROR; | |
} | |
/* Construct the command */ | |
memset(AtCmd, '\0', MAX_AT_CMD_SIZE); | |
sprintf((char *)AtCmd, "AT+CFUN=1\r\n"); | |
/* Send the command */ | |
Ret = runAtCmd(AtCmd, strlen((char *)AtCmd), (uint8_t *)AT_OK_STRING); | |
/* Exit in case of error */ | |
if (Ret != EC616S_OK) | |
{ | |
return EC616S_ERROR; | |
} | |
return Ret; | |
} | |
/** | |
* @brief Restarts the EC616S module. | |
* @param None | |
* @retval EC616S_OK on sucess, EC616S_ERROR otherwise. | |
*/ | |
EC616S_StatusTypeDef EC616S_Restart(void) | |
{ | |
EC616S_StatusTypeDef Ret; | |
/* Construct the command */ | |
memset(AtCmd, '\0', MAX_AT_CMD_SIZE); | |
sprintf((char *)AtCmd, "AT+ECRST%c%c", '\r', '\n'); | |
/* Send the command */ | |
Ret = runAtCmd(AtCmd, strlen((char *)AtCmd), (uint8_t *)AT_OK_STRING); | |
return Ret; | |
} | |
/** | |
* @brief Run the AT command | |
* @param cmd the buffer to fill will the received data. | |
* @param Length the maximum data size to receive. | |
* @param Token the expected output if command runs successfully | |
* @retval Returns EC616S_OK on success and EC616S_ERROR otherwise. | |
*/ | |
static EC616S_StatusTypeDef runAtCmd(uint8_t *cmd, uint32_t Length, const uint8_t *Token) | |
{ | |
uint32_t idx = 0; | |
uint8_t RxChar; | |
/* Reset the Rx buffer to make sure no previous data exist */ | |
memset(RxBuffer, '\0', MAX_BUFFER_SIZE); | |
/* Send the command */ | |
EC616S_IO_Send(cmd, Length); | |
/* Wait for reception */ | |
while (1) | |
{ | |
/* Wait to recieve data */ | |
if (EC616S_IO_Receive(&RxChar, 1) != 0) | |
{ | |
RxBuffer[idx++] = RxChar; | |
} | |
else | |
{ | |
break; | |
} | |
/* Check that max buffer size has not been reached */ | |
if (idx == MAX_BUFFER_SIZE) | |
{ | |
break; | |
} | |
/* Extract the Token */ | |
if (strstr((char *)RxBuffer, (char *)Token) != NULL) | |
{ | |
return EC616S_OK; | |
} | |
/* Check if the message contains error code */ | |
if (strstr((char *)RxBuffer, AT_ERROR_STRING) != NULL) | |
{ | |
return EC616S_ERROR; | |
} | |
} | |
return EC616S_ERROR; | |
} | |
/** | |
* @brief Set Keep Alive Packet timer | |
* @param keep_alive: 0 - 3600,default 120,unit:sec | |
* @retval Returns EC616S_OK on success and EC616S_ERROR otherwise. | |
*/ | |
EC616S_StatusTypeDef EC616S_MQTT_SetKeepAlive(uint16_t keep_alive) | |
{ | |
EC616S_StatusTypeDef Ret; | |
memset(AtCmd, '\0', MAX_AT_CMD_SIZE); | |
sprintf((char *)AtCmd, "AT+ECMTCFG=\"keepalive\",0,%d\r\n", keep_alive); | |
/* Send the command */ | |
Ret = runAtCmd(AtCmd, strlen((char *)AtCmd), (uint8_t *)AT_OK_STRING); | |
return Ret; | |
} | |
/** | |
* @brief Set Session Save or not-Save | |
* @param clean_session: 0:not save 1:save by module | |
* @retval Returns EC616S_OK on success and EC616S_ERROR otherwise. | |
*/ | |
EC616S_StatusTypeDef EC616S_MQTT_SetSession(uint8_t clean_session) | |
{ | |
EC616S_StatusTypeDef Ret; | |
memset(AtCmd, '\0', MAX_AT_CMD_SIZE); | |
sprintf((char *)AtCmd, "AT+ECMTCFG=\"session\",0,%d\r\n", clean_session); | |
/* Send the command */ | |
Ret = runAtCmd(AtCmd, strlen((char *)AtCmd), (uint8_t *)AT_OK_STRING); | |
return Ret; | |
} | |
/** | |
* @brief Set timeout timer | |
* @param pkt_timeout: Packet timeout 1 - 60 (default:10s) | |
* @param retry_times: Packet retry times 0 - 10 (default:3) - unsupport | |
* @param timeout_notice: Packet retry times 0 - 1 (default:0) - unsupport | |
* @retval Returns EC616S_OK on success and EC616S_ERROR otherwise. | |
*/ | |
EC616S_StatusTypeDef EC616S_MQTT_SetTimeOut(uint8_t pkt_timeout, uint8_t retry_times, uint8_t timeout_notice) | |
{ | |
EC616S_StatusTypeDef Ret; | |
memset(AtCmd, '\0', MAX_AT_CMD_SIZE); | |
sprintf((char *)AtCmd, "AT+ECMTCFG=\"timeout\",0,%d,%d,%d\r\n", pkt_timeout, retry_times, timeout_notice); | |
/* Send the command */ | |
Ret = runAtCmd(AtCmd, strlen((char *)AtCmd), (uint8_t *)AT_OK_STRING); | |
return Ret; | |
} | |
/** | |
* @brief Set will flag | |
* @param will_fg: Will flag | |
* @param will_qos: QOS flag (0,1,2) | |
* @param will_retain: Retain or not (0,1) | |
* @param will_topic: Topic | |
* @param will_msg: Message when disconnect | |
* @retval Returns EC616S_OK on success and EC616S_ERROR otherwise. | |
*/ | |
EC616S_StatusTypeDef EC616S_MQTT_SetWill(uint8_t will_fg, uint8_t will_qos, uint8_t will_retain, uint8_t *will_topic, uint8_t *will_msg) | |
{ | |
EC616S_StatusTypeDef Ret; | |
memset(AtCmd, '\0', MAX_AT_CMD_SIZE); | |
sprintf((char *)AtCmd, "AT+ECMTCFG=\"will\",0,%d,%d,%d,\"%s\",\"%s\"\r\n", will_fg, will_qos, will_retain, will_topic, will_msg); | |
/* Send the command */ | |
Ret = runAtCmd(AtCmd, strlen((char *)AtCmd), (uint8_t *)AT_OK_STRING); | |
return Ret; | |
} | |
/** | |
* @brief Set MQTT Version | |
* @param version: MQTT(3,4) | |
* @retval Returns EC616S_OK on success and EC616S_ERROR otherwise. | |
*/ | |
EC616S_StatusTypeDef EC616S_MQTT_SetVersion(uint8_t version) | |
{ | |
EC616S_StatusTypeDef Ret; | |
memset(AtCmd, '\0', MAX_AT_CMD_SIZE); | |
sprintf((char *)AtCmd, "AT+ECMTCFG=\"version\",0,%d\r\n", version); | |
/* Send the command */ | |
Ret = runAtCmd(AtCmd, strlen((char *)AtCmd), (uint8_t *)AT_OK_STRING); | |
return Ret; | |
} | |
/** | |
* @brief Set MQTT Version | |
* @param data_type: (1 = > json,2 => string) | |
* @retval Returns EC616S_OK on success and EC616S_ERROR otherwise. | |
*/ | |
EC616S_StatusTypeDef EC616S_MQTT_SetCloud(uint8_t data_type) | |
{ | |
EC616S_StatusTypeDef Ret; | |
memset(AtCmd, '\0', MAX_AT_CMD_SIZE); | |
sprintf((char *)AtCmd, "AT+ECMTCFG=\"cloud\",0,0,%d\r\n", data_type); | |
/* Send the command */ | |
Ret = runAtCmd(AtCmd, strlen((char *)AtCmd), (uint8_t *)AT_OK_STRING); | |
return Ret; | |
} | |
/** | |
* @brief Open MQTT Connect | |
* @param hostname: ip addr or domain | |
* @param port: remote port | |
* @retval Returns EC616S_OK on success and EC616S_ERROR otherwise. | |
*/ | |
EC616S_StatusTypeDef EC616S_MQTT_Open(uint8_t *hostname, uint16_t port) | |
{ | |
EC616S_StatusTypeDef Ret; | |
memset(AtCmd, '\0', MAX_AT_CMD_SIZE); | |
sprintf((char *)AtCmd, "AT+ECMTOPEN=0,\"%s\",%d\r\n", hostname, port); | |
/* Send the command */ | |
Ret = runAtCmd(AtCmd, strlen((char *)AtCmd), (uint8_t *)AT_OK_STRING); | |
return Ret; | |
} | |
/** | |
* @brief Close MQTT Connect | |
* @retval Returns EC616S_OK on success and EC616S_ERROR otherwise. | |
*/ | |
EC616S_StatusTypeDef EC616S_MQTT_Close(void) | |
{ | |
EC616S_StatusTypeDef Ret; | |
memset(AtCmd, '\0', MAX_AT_CMD_SIZE); | |
sprintf((char *)AtCmd, "AT+ECMTCLOSE=0\r\n"); | |
/* Send the command */ | |
Ret = runAtCmd(AtCmd, strlen((char *)AtCmd), (uint8_t *)AT_OK_STRING); | |
return Ret; | |
} | |
/** | |
* @brief MQTT Auth with-out password | |
* @param clientID: device descr | |
* @retval Returns EC616S_OK on success and EC616S_ERROR otherwise. | |
*/ | |
EC616S_StatusTypeDef EC616S_MQTT_ConnectWithOutPassword(uint8_t *clientID) | |
{ | |
EC616S_StatusTypeDef Ret; | |
memset(AtCmd, '\0', MAX_AT_CMD_SIZE); | |
sprintf((char *)AtCmd, "AT+ECMTCONN=0,\"%s\"\r\n", clientID); | |
/* Send the command */ | |
Ret = runAtCmd(AtCmd, strlen((char *)AtCmd), (uint8_t *)AT_OK_STRING); | |
return Ret; | |
} | |
/** | |
* @brief MQTT Auth with Password | |
* @param clientID: device descr | |
* @param username: device username | |
* @param password: device password | |
* @retval Returns EC616S_OK on success and EC616S_ERROR otherwise. | |
*/ | |
EC616S_StatusTypeDef EC616S_MQTT_ConnectWithPassword(uint8_t *clientID, uint8_t *username, uint8_t *password) | |
{ | |
EC616S_StatusTypeDef Ret; | |
memset(AtCmd, '\0', MAX_AT_CMD_SIZE); | |
sprintf((char *)AtCmd, "AT+ECMTCONN=0,\"%s\",\"%s\",\"%s\"\r\n", clientID, username, password); | |
/* Send the command */ | |
Ret = runAtCmd(AtCmd, strlen((char *)AtCmd), (uint8_t *)AT_OK_STRING); | |
return Ret; | |
} | |
/** | |
* @brief Close MQTT Disconnect | |
* @retval Returns EC616S_OK on success and EC616S_ERROR otherwise. | |
*/ | |
EC616S_StatusTypeDef EC616S_MQTT_DisConnect(void) | |
{ | |
EC616S_StatusTypeDef Ret; | |
memset(AtCmd, '\0', MAX_AT_CMD_SIZE); | |
sprintf((char *)AtCmd, "AT+ECMTDISC=0\r\n"); | |
/* Send the command */ | |
Ret = runAtCmd(AtCmd, strlen((char *)AtCmd), (uint8_t *)AT_OK_STRING); | |
return Ret; | |
} | |
/** | |
* @brief Subscribe MQTT Topic | |
* @param msgID: msgId | |
* @param topicID: msg topic | |
* @param qos: (0,1,2) | |
* @retval Returns EC616S_OK on success and EC616S_ERROR otherwise. | |
*/ | |
EC616S_StatusTypeDef EC616S_MQTT_Subscribe(uint16_t msgID, uint8_t *topicID, uint8_t qos) | |
{ | |
EC616S_StatusTypeDef Ret; | |
memset(AtCmd, '\0', MAX_AT_CMD_SIZE); | |
sprintf((char *)AtCmd, "AT+ECMTSUB=0,\"%s\",\"%s\",%d\r\n", msgID, topicID, qos); | |
/* Send the command */ | |
Ret = runAtCmd(AtCmd, strlen((char *)AtCmd), (uint8_t *)AT_OK_STRING); | |
return Ret; | |
} | |
/** | |
* @brief Unsubscribe MQTT Topic | |
* @param msgID: msgId | |
* @param topicID: msg topic | |
* @retval Returns EC616S_OK on success and EC616S_ERROR otherwise. | |
*/ | |
EC616S_StatusTypeDef EC616S_MQTT_Unsubscribe(uint16_t msgID, uint8_t *topicID) | |
{ | |
EC616S_StatusTypeDef Ret; | |
memset(AtCmd, '\0', MAX_AT_CMD_SIZE); | |
sprintf((char *)AtCmd, "AT+ECMTUNS=0,\"%s\",\"%s\"\r\n", msgID, topicID); | |
/* Send the command */ | |
Ret = runAtCmd(AtCmd, strlen((char *)AtCmd), (uint8_t *)AT_OK_STRING); | |
return Ret; | |
} | |
/** | |
* @brief Publish MQTT Message | |
* @param msgID: msgId | |
* @param qos: (0,1,2) | |
* @param retain: (0,1) | |
* @param topicID: msg topic | |
* @param payload: msg payload | |
* @retval Returns EC616S_OK on success and EC616S_ERROR otherwise. | |
*/ | |
EC616S_StatusTypeDef EC616S_MQTT_Publish(uint16_t msgID, uint8_t qos, uint8_t retain, uint8_t *topicID, uint8_t *payload) | |
{ | |
EC616S_StatusTypeDef Ret; | |
memset(AtCmd, '\0', MAX_AT_CMD_SIZE); | |
sprintf((char *)AtCmd, "AT+ECMTPUB=0,%d,%d,%d,\"%s\",\"%s\"\r\n", msgID, qos, retain, topicID, payload); | |
/* Send the command */ | |
Ret = runAtCmd(AtCmd, strlen((char *)AtCmd), (uint8_t *)AT_OK_STRING); | |
return Ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment