464 lines
14 KiB
C
464 lines
14 KiB
C
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
|
|
#include "main.h"
|
|
#include "usart.h"
|
|
#include "gpio.h"
|
|
|
|
#include "eg91.h"
|
|
#include "lte_comms.h"
|
|
|
|
#define LTE_CONTEXT_1 1
|
|
|
|
EG91Object_t Eg91Obj;
|
|
static uint16_t rnglocalport = 0;
|
|
|
|
int Network_Init(void)
|
|
{
|
|
LTE_Ret_t ret = LTE_RET_ERROR;
|
|
LTE_Registatus_t reg_status = LTE_REGISTATUS_UNKNOWN;
|
|
int32_t quality_level = 0;
|
|
int8_t quality_level_db = 0;
|
|
int try_counter = 0;
|
|
|
|
char moduleInfo[MAX(MAX(MAX(MAX(LTE_FW_REV_SIZE, LTE_MAN_SIZE), LTE_PROD_ID_SIZE), LTE_IMEI_SIZE), LTE_ICCID_SIZE) + 1];
|
|
LTE_HardwarePowerUp();
|
|
|
|
while (ret != LTE_RET_OK)
|
|
{
|
|
if (try_counter >= 3)
|
|
{
|
|
ret = LTE_RET_ERROR;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
try_counter++;
|
|
HAL_Delay(50);
|
|
|
|
reg_status = LTE_Init(120);
|
|
|
|
LTE_GetModuleName(moduleInfo);
|
|
APP_LOG_MSG("Module Name: %s\r\n", moduleInfo);
|
|
memset(moduleInfo, 0, sizeof(moduleInfo));
|
|
|
|
LTE_GetModuleID(moduleInfo);
|
|
APP_LOG_MSG("Product ID: %s\r\n", moduleInfo);
|
|
memset(moduleInfo, 0, sizeof(moduleInfo));
|
|
|
|
LTE_GetModuleFirmwareRevision(moduleInfo);
|
|
APP_LOG_MSG("FW Version: %s\r\n", moduleInfo);
|
|
memset(moduleInfo, 0, sizeof(moduleInfo));
|
|
|
|
LTE_GetIMEI(moduleInfo);
|
|
APP_LOG_MSG("IMEI: %s\r\n", moduleInfo);
|
|
memset(moduleInfo, 0, sizeof(moduleInfo));
|
|
|
|
LTE_GetSimId(moduleInfo);
|
|
APP_LOG_MSG("ICCID: %s\r\n", moduleInfo);
|
|
memset(moduleInfo, 0, sizeof(moduleInfo));
|
|
|
|
|
|
if ((reg_status != LTE_REGISTATUS_ERROR) && (reg_status != LTE_REGISTATUS_SIM_NOT_INSERTED) && (reg_status == LTE_REGISTATUS_NOT_REGISTERED))
|
|
{
|
|
APP_LOG_MSG("Successful Initialization of the Module\r\n\r\n");
|
|
memset(moduleInfo, 0, sizeof(moduleInfo));
|
|
|
|
|
|
/* Retrieve the Quality Level of the Connection */
|
|
LTE_GetSignalQualityStatus(&quality_level);
|
|
if (quality_level != 99)
|
|
{
|
|
quality_level_db = (int8_t)(-113 + 2 * quality_level);
|
|
APP_LOG_MSG("Signal Quality Level %d dBm (GSM Format: %ld)\r\n", quality_level_db, quality_level);
|
|
}
|
|
else
|
|
{
|
|
APP_LOG_MSG("Signal Quality Level not detectable\r\n");
|
|
}
|
|
APP_LOG_MSG("\r\n");
|
|
}
|
|
|
|
|
|
switch (reg_status)
|
|
{
|
|
case LTE_REGISTATUS_HOME_NETWORK:
|
|
case LTE_REGISTATUS_ROAMING:
|
|
case LTE_REGISTATUS_UNKNOWN:
|
|
{
|
|
HAL_Delay(1000);
|
|
ret = LTE_RET_OK;
|
|
APP_LOG_MSG("\r\n >>> Module Registration Successful\r\n\r\n");
|
|
break;
|
|
}
|
|
|
|
case LTE_REGISTATUS_TRYING:
|
|
{
|
|
APP_LOG_MSG("\r\n >>> Module Registration Trying\r\n\r\n");
|
|
break;
|
|
}
|
|
|
|
case LTE_REGISTATUS_REG_DENIED:
|
|
{
|
|
APP_LOG_MSG("\r\n >>> Module Registration Denied\r\n\r\n");
|
|
break;
|
|
}
|
|
|
|
case LTE_REGISTATUS_NOT_REGISTERED:
|
|
{
|
|
APP_LOG_MSG("\r\n >>> Module Registration Failed\r\n\r\n");
|
|
break;
|
|
}
|
|
|
|
case LTE_REGISTATUS_ERROR:
|
|
{
|
|
APP_LOG_MSG("\r\n >>> Module AT Communication Error with the Device\r\n");
|
|
APP_LOG_MSG(" >>> Device might be disconnected or wrongly connected\r\n\r\n");
|
|
break;
|
|
}
|
|
|
|
case LTE_REGISTATUS_SIM_NOT_INSERTED:
|
|
{
|
|
APP_LOG_MSG("\r\n >>> SIM is not INSERTED\r\n\r\n");
|
|
break;
|
|
}
|
|
|
|
default:
|
|
{
|
|
APP_LOG_MSG("\r\n >>> Module SIM Error: %d\r\n", reg_status);
|
|
APP_LOG_MSG(" >>> Please check if the SIM is inserted and valid, if credentials are ok, etc.\r\n\r\n");
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
static char operatorStr[LTE_OPERATORS_LIST + 1];
|
|
static uint8_t sim = -1;
|
|
char apnString[30];
|
|
APP_LOG_MSG("Cellular Operator: ");
|
|
if (LTE_GetCurrentOperator(operatorStr, sizeof(operatorStr)) == LTE_RET_OK)
|
|
{
|
|
APP_LOG_MSG("%s\r\n", operatorStr);
|
|
if ((strstr(operatorStr, "SMART") != NULL) || (strstr(operatorStr, "51503") != NULL))
|
|
{
|
|
sim = SIM_OPERATOR_SMART;
|
|
sprintf(apnString, "internet");
|
|
}
|
|
else if ((strstr(operatorStr, "GLOBE") != NULL) || (strstr(operatorStr, "51502") != NULL))
|
|
{
|
|
sim = SIM_OPERATOR_GLOBE;
|
|
sprintf(apnString, "internet.globe.com.ph");
|
|
}
|
|
else if ((strstr(operatorStr, "DITO") != NULL) || (strstr(operatorStr, "51566") != NULL))
|
|
{
|
|
sim = SIM_OPERATOR_DITO;
|
|
sprintf(apnString, "internet.dito.ph");
|
|
}
|
|
else
|
|
{
|
|
sim = SIM_OPERATOR_UNKNOWN;
|
|
ret = LTE_RET_NOT_SUPPORTED;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
APP_LOG_MSG("\rFailed to get the current cellular operator name\r\n\r\n");
|
|
}
|
|
|
|
if(LTE_Connect(apnString) == LTE_RET_OK)
|
|
{
|
|
LTE_GetActiveIpAddress();
|
|
|
|
if (LTE_isConnected() == LTE_AP_ACTIVATED)
|
|
{
|
|
ret = LTE_Ping();
|
|
HAL_Delay(5000);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return (ret == LTE_RET_OK) ? 0 : -1;
|
|
}
|
|
|
|
|
|
|
|
void LTE_ConvertIpAddrToString(const uint8_t * ipArray, char *returnStr)
|
|
{
|
|
snprintf((char*) returnStr, 16, "%d.%d.%d.%d", ipArray[0], ipArray[1], ipArray[2], ipArray[3]);
|
|
}
|
|
|
|
LTE_Registatus_t LTE_Init(uint16_t registration_timeout_sec)
|
|
{
|
|
EG91_InitRet_t init_status;
|
|
EG91_SIMState_t sim_tmp;
|
|
LTE_Registatus_t ret = LTE_REGISTATUS_ERROR;
|
|
|
|
bool signalMsg = false;
|
|
|
|
/* Channel Signal Quality */
|
|
int32_t quality_level = 0;
|
|
int8_t quality_level_db = 0;
|
|
|
|
/* Counter for PS Attachement */
|
|
int8_t attRetry = 0;
|
|
|
|
/* Init for timeout management */
|
|
uint32_t tickstart;
|
|
uint32_t tickcurrent;
|
|
uint32_t registration_timeout_msec = registration_timeout_sec * 1000;
|
|
|
|
tickstart = HAL_GetTick();
|
|
|
|
EG91_RegisterTickCb(&Eg91Obj, HAL_GetTick);
|
|
|
|
if (EG91_RegisterBusIO(&Eg91Obj, (IO_Init_Func)MX_USART1_UART_Init,
|
|
(IO_DeInit_Func)HAL_UART_DeInit, (IO_Baudrate_Func)LTE_UART_SetBaudrate,
|
|
(IO_Send_Func)LTE_UART_SendData, (IO_ReceiveOne_Func)LTE_UART_ReceiveSingleData,
|
|
(IO_Flush_Func)LTE_UART_FlushBuffer) == EG91_RETURN_OK)
|
|
{
|
|
init_status = EG91_Init(&Eg91Obj);
|
|
if (init_status == EG91_INIT_RET_OK)
|
|
{
|
|
tickcurrent = HAL_GetTick() - tickstart;
|
|
while ((tickcurrent < registration_timeout_msec) || (registration_timeout_sec == LTE_COMMS_MAX_DELAY))
|
|
{
|
|
/* Check Signal Quality*/
|
|
if (EG91_GetSignalQualityStatus(&Eg91Obj, &quality_level) == EG91_RETURN_OK)
|
|
{
|
|
if (quality_level == 99)
|
|
{
|
|
if (signalMsg == false)
|
|
{
|
|
APP_LOG_MSG("Signal is not known or not detectable yet... ");
|
|
signalMsg = true;
|
|
}
|
|
else
|
|
{
|
|
APP_LOG_MSG(".");
|
|
}
|
|
HAL_Delay(1000);
|
|
}
|
|
else
|
|
{
|
|
quality_level_db = (int8_t)(-113 + 2 * quality_level);
|
|
APP_LOG_MSG("\r\nSignal Level: %d dBm\r\n", quality_level_db);
|
|
HAL_Delay(1000);
|
|
break;
|
|
}
|
|
}
|
|
tickcurrent = HAL_GetTick() - tickstart;
|
|
}
|
|
|
|
EG91_ConfigurePDPContext(&Eg91Obj, LTE_CONTEXT_1, "internet");
|
|
|
|
tickcurrent = HAL_GetTick() - tickstart;
|
|
while( (tickcurrent < registration_timeout_msec) || (registration_timeout_sec == LTE_COMMS_MAX_DELAY) )
|
|
{
|
|
if((EG91_GetPsNetworkRegistrationStatus(&Eg91Obj) == EG91_NRS_HOME_NETWORK) &&
|
|
(EG91_GetCsNetworkRegistrationStatus(&Eg91Obj) == EG91_NRS_HOME_NETWORK))
|
|
{
|
|
/* Attach the MT to the backet domain service */
|
|
if (EG91_PSAttach(&Eg91Obj) == EG91_RETURN_OK)
|
|
{
|
|
APP_DEBUG_MSG("Packet Switched attachement succeeded\r\n");
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
/* Start an Automatic PLMN selection */
|
|
attRetry++;
|
|
if (attRetry == 1)
|
|
{
|
|
APP_DEBUG_MSG("Trying an automatic registration. It may take until 3 minutes, please wait ...\r\n");
|
|
if (EG91_RETURN_OK != EG91_AutomaticPlmnSelection(&Eg91Obj))
|
|
{
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
if (attRetry > 4)
|
|
{
|
|
APP_DEBUG_MSG("Unrecoverable Error, PS attachement failed\r\n");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
HAL_Delay(1000);
|
|
}
|
|
}
|
|
tickcurrent = HAL_GetTick() - tickstart;
|
|
}
|
|
/* Check Packet Switched Registration */
|
|
ret = (LTE_Registatus_t) EG91_GetPsNetworkRegistrationStatus(&Eg91Obj);
|
|
APP_DEBUG_MSG("Packet Switch Registration: %d\r\n", ret);
|
|
if ( (ret == LTE_REGISTATUS_HOME_NETWORK) || (ret == LTE_REGISTATUS_ROAMING) )
|
|
{
|
|
tickcurrent = HAL_GetTick() - tickstart;
|
|
APP_DEBUG_MSG("Registation done in %lu ms\r\n", tickcurrent);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (init_status == EG91_INIT_RET_SIM_ERR)
|
|
{
|
|
sim_tmp = Eg91Obj.SimInfo.SimStatus;
|
|
if (sim_tmp == EG91_OPERATION_NOT_ALLOW)
|
|
{
|
|
ret = LTE_REGISTATUS_OP_NOT_ALLOWED;
|
|
}
|
|
else
|
|
{
|
|
ret = (LTE_Registatus_t) Eg91Obj.SimInfo.SimStatus;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ret = LTE_REGISTATUS_ERROR; /* Generic e.g. module does not respond to AT command */
|
|
}
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
LTE_Ret_t LTE_GetSignalQualityStatus(int32_t *qValue)
|
|
{
|
|
LTE_Ret_t ret = LTE_RET_ERROR;
|
|
if (EG91_GetSignalQualityStatus(&Eg91Obj, qValue) == EG91_RETURN_OK)
|
|
{
|
|
ret = LTE_RET_OK;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
LTE_Ret_t LTE_ListOperators(char *operator)
|
|
{
|
|
LTE_Ret_t ret = LTE_RET_ERROR;
|
|
if (EG91_ListOperators(&Eg91Obj, operator) == EG91_RETURN_OK)
|
|
{
|
|
ret = LTE_RET_OK;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
LTE_Ret_t LTE_GetCurrentOperator (char *operator, int32_t bufSize)
|
|
{
|
|
LTE_Ret_t ret = LTE_RET_ERROR;
|
|
if (EG91_GetCurrentOperator(&Eg91Obj, operator, bufSize) == EG91_RETURN_OK)
|
|
{
|
|
ret = LTE_RET_OK;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
LTE_Ret_t LTE_Connect(const char* apn)
|
|
{
|
|
LTE_Ret_t ret = LTE_RET_ERROR;
|
|
|
|
if (EG91_Activate(&Eg91Obj, LTE_CONTEXT_1, apn) == EG91_RETURN_OK)
|
|
{
|
|
ret = LTE_RET_OK;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
LTE_Ret_t LTE_Disconnect(void)
|
|
{
|
|
LTE_Ret_t ret = LTE_RET_ERROR;
|
|
if (EG91_Deactivate(&Eg91Obj, LTE_CONTEXT_1) == EG91_RETURN_OK)
|
|
{
|
|
ret = LTE_RET_OK;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
LTE_APState_t LTE_isConnected(void)
|
|
{
|
|
return (LTE_APState_t) EG91_IsActivated(&Eg91Obj, LTE_CONTEXT_1);
|
|
}
|
|
|
|
LTE_Ret_t LTE_GetActiveIpAddress(void)
|
|
{
|
|
LTE_Ret_t ret = LTE_RET_ERROR;
|
|
if (EG91_GetActiveIpAddresses(&Eg91Obj) == EG91_RETURN_OK)
|
|
{
|
|
ret = LTE_RET_OK;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
LTE_Ret_t LTE_Ping(void)
|
|
{
|
|
LTE_Ret_t ret = LTE_RET_ERROR;
|
|
if (EG91_Ping(&Eg91Obj) == EG91_RETURN_OK)
|
|
{
|
|
ret = LTE_RET_OK;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
LTE_Ret_t LTE_PowerOff(void)
|
|
{
|
|
LTE_Ret_t ret = LTE_RET_ERROR;
|
|
if (EG91_PowerDown(&Eg91Obj) == EG91_RETURN_OK)
|
|
{
|
|
ret = LTE_RET_OK;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
LTE_Ret_t LTE_GetModuleFirmwareRevision(char *rev)
|
|
{
|
|
LTE_Ret_t ret = LTE_RET_ERROR;
|
|
if (Eg91Obj.FW_Rev != NULL)
|
|
{
|
|
strncpy(rev, (char*)Eg91Obj.FW_Rev, MIN(LTE_FW_REV_SIZE, EG91_FW_REV_SIZE));
|
|
ret = LTE_RET_OK;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
LTE_Ret_t LTE_GetModuleID(char *productId)
|
|
{
|
|
LTE_Ret_t ret = LTE_RET_ERROR;
|
|
if (Eg91Obj.ProductID != NULL)
|
|
{
|
|
strncpy(productId, (char*)Eg91Obj.ProductID, MIN(LTE_PROD_ID_SIZE, EG91_PROD_ID_SIZE));
|
|
ret = LTE_RET_OK;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
LTE_Ret_t LTE_GetModuleName(char *moduleName)
|
|
{
|
|
LTE_Ret_t ret = LTE_RET_ERROR;
|
|
if (Eg91Obj.Manufacturer != NULL)
|
|
{
|
|
strncpy(moduleName, (char*)Eg91Obj.Manufacturer, MIN(LTE_MAN_SIZE, EG91_MFC_SIZE));
|
|
ret = LTE_RET_OK;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
LTE_Ret_t LTE_GetSimId(char *simId)
|
|
{
|
|
LTE_Ret_t ret = LTE_RET_ERROR;
|
|
if (Eg91Obj.SimInfo.ICCID != NULL)
|
|
{
|
|
strncpy(simId, (char*)Eg91Obj.SimInfo.ICCID, MIN(LTE_ICCID_SIZE, EG91_ICCID_SIZE));
|
|
ret = LTE_RET_OK;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
LTE_Ret_t LTE_GetIMEI(char *imei)
|
|
{
|
|
LTE_Ret_t ret = LTE_RET_ERROR;
|
|
if (Eg91Obj.Imei != NULL)
|
|
{
|
|
strncpy(imei, (char*)Eg91Obj.Imei, MIN(LTE_IMEI_SIZE, EG91_IMEI_SIZE));
|
|
ret = LTE_RET_OK;
|
|
}
|
|
return ret;
|
|
}
|