GONEC_ARTERY_NetConf_GSM/Src/netconf.c

300 lines
10 KiB
C

/**
**************************************************************************
* @file netconf.c
* @version v2.0.4
* @date 2021-12-31
* @brief network connection configuration
**************************************************************************
* Copyright notice & Disclaimer
*
* The software Board Support Package (BSP) that is made available to
* download from Artery official website is the copyrighted work of Artery.
* Artery authorizes customers to use, copy, and distribute the BSP
* software and its related documentation for the purpose of design and
* development in conjunction with Artery microcontrollers. Use of the
* software is governed by this copyright notice and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
* GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
* TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
* STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
* INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
*
**************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "lwip/memp.h"
#include "lwip/tcp.h"
#include "lwip/priv/tcp_priv.h"
#include "lwip/udp.h"
#include "netif/etharp.h"
#include "lwip/dhcp.h"
#include "ethernetif.h"
#include "../Inc/netconf.h"
#include "stdio.h"
#include "SystemDelayInterface.h"
/** @addtogroup AT32F437_periph_examples
* @{
*/
/** @addtogroup 437_EMAC_tcp_server
* @{
*/
/** @addtogroup network_configuration
* @{
*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
#define MAC_ADDR_LENGTH (6)
#define ADDR_LENGTH (4)
/* Private variables ---------------------------------------------------------*/
struct netif netif;
volatile uint32_t tcp_timer = 0;
volatile uint32_t arp_timer = 0;
#include "MainModesArbiter.h"
extern tMma MAIN_ENV;
static uint8_t mac_address[MAC_ADDR_LENGTH] = {0, 0, 0x45, 0x45, 0x55, 3};
#if LWIP_DHCP
volatile uint32_t dhcp_fine_timer = 0;
volatile uint32_t dhcp_coarse_timer = 0;
#else
static uint8_t local_ip[ADDR_LENGTH] = {192, 168, 1, 99};
static uint8_t local_gw[ADDR_LENGTH] = {192, 168, 1, 1};
static uint8_t local_mask[ADDR_LENGTH] = {255, 255, 255, 0};
#endif
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
int extract_ip_ex_parameters(char *params, char *param_names[], char *param_vals[], int params_max);
/**
* @brief initializes the lwip stack
* @param none
* @retval none
*/
void tcpip_stack_init(uint16_t data) {
ip_addr_t ipaddr;
ip_addr_t netmask;
ip_addr_t gw;
mac_address[4] = data >> 8;
mac_address[5] = data;
/* Initializes the dynamic memory heap defined by MEM_SIZE.*/
mem_init();
/* Initializes the memory pools defined by MEMP_NUM_x.*/
memp_init();
#if LWIP_DHCP //need DHCP server
ipaddr.addr = 0;
netmask.addr = 0;
gw.addr = 0;
#else
char MyIPadres_v[32];
char MyIPmask_v[32];
char MyGateway_v[32];
memcpy(MyIPadres_v, MAIN_ENV.storageIni.nvm.Settings_Network.MyIPadres_v, 32);
memcpy(MyIPmask_v, MAIN_ENV.storageIni.nvm.Settings_Network.MyIPmask_v, 32);
memcpy(MyGateway_v, MAIN_ENV.storageIni.nvm.Settings_Network.MyGateway_v, 32);
struct {
int paramcount;
char *params_names[4];
char *params_vals[4];
} params_grp;
params_grp.paramcount = extract_ip_ex_parameters(MyIPadres_v,
params_grp.params_names,
params_grp.params_vals,
4);
if (params_grp.paramcount == 4) {
uint32_t local_ip0 = atoi(params_grp.params_names[0]);
uint32_t local_ip1 = atoi(params_grp.params_names[1]);
uint32_t local_ip2 = atoi(params_grp.params_names[2]);
uint32_t local_ip3 = atoi(params_grp.params_names[3]);
if (((local_ip0 < 255) && (local_ip0 > 0)) && ((local_ip1 < 255) && (local_ip1 > 0)) &&
((local_ip2 < 255) && (local_ip2 > 0)) && ((local_ip3 < 255) && (local_ip3 > 0))) {
local_ip[0] = atoi(params_grp.params_names[0]);
local_ip[1] = atoi(params_grp.params_names[1]);
local_ip[2] = atoi(params_grp.params_names[2]);
local_ip[3] = atoi(params_grp.params_names[3]);
}
}
params_grp.paramcount = extract_ip_ex_parameters(MyIPmask_v,
params_grp.params_names,
params_grp.params_vals,
4);
if (params_grp.paramcount == 4) {
uint32_t local_mask0 = atoi(params_grp.params_names[0]);
uint32_t local_mask1 = atoi(params_grp.params_names[1]);
uint32_t local_mask2 = atoi(params_grp.params_names[2]);
uint32_t local_mask3 = atoi(params_grp.params_names[3]);
if (((local_mask0 <= 255) && (local_mask0 >= 0)) && ((local_mask1 <= 255) && (local_mask1 >= 0)) &&
((local_mask2 <= 255) && (local_mask2 >= 0)) && ((local_mask3 <= 255) && (local_mask3 >= 0))) {
local_mask[0] = atoi(params_grp.params_names[0]);
local_mask[1] = atoi(params_grp.params_names[1]);
local_mask[2] = atoi(params_grp.params_names[2]);
local_mask[3] = atoi(params_grp.params_names[3]);
}
}
params_grp.paramcount = extract_ip_ex_parameters(MyGateway_v,
params_grp.params_names,
params_grp.params_vals,
4);
if (params_grp.paramcount == 4) {
uint32_t local_gw0 = atoi(params_grp.params_names[0]);
uint32_t local_gw1 = atoi(params_grp.params_names[1]);
uint32_t local_gw2 = atoi(params_grp.params_names[2]);
uint32_t local_gw3 = atoi(params_grp.params_names[3]);
if (((local_gw0 < 255) && (local_gw0 > 0)) && ((local_gw1 < 255) && (local_gw1 > 0)) &&
((local_gw2 < 255) && (local_gw2 > 0)) && ((local_gw3 < 255) && (local_gw3 > 0))) {
local_gw[0] = atoi(params_grp.params_names[0]);
local_gw[1] = atoi(params_grp.params_names[1]);
local_gw[2] = atoi(params_grp.params_names[2]);
local_gw[3] = atoi(params_grp.params_names[3]);
}
}
IP4_ADDR(&ipaddr, local_ip[0], local_ip[1], local_ip[2], local_ip[3]);
IP4_ADDR(&netmask, local_mask[0], local_mask[1], local_mask[2], local_mask[3]);
IP4_ADDR(&gw, local_gw[0], local_gw[1], local_gw[2], local_gw[3]);
#endif
lwip_set_mac_address(mac_address);
/* - netif_add(struct netif *netif, struct ip_addr *ipaddr,
struct ip_addr *netmask, struct ip_addr *gw,
void *state, err_t (* init)(struct netif *netif),
err_t (* input)(struct pbuf *p, struct netif *netif))
Adds your network interface to the netif_list. Allocate a struct
netif and pass a pointer to this structure as the first argument.
Give pointers to cleared ip_addr structures when using DHCP,
or fill them with sane numbers otherwise. The state pointer may be NULL.
The init function pointer must point to a initialization function for
your ethernet netif interface. The following code illustrates it's use.*/
if (netif_add(&netif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &netif_input) == NULL) {
while (1);
}
/* Registers the default network interface.*/
netif_set_default(&netif);
#if LWIP_DHCP
/* Creates a new DHCP client for this interface on the first call.
Note: you must call dhcp_fine_tmr() and dhcp_coarse_tmr() at
the predefined regular intervals after starting the client.
You can peek in the netif->dhcp struct for the actual DHCP status.*/
dhcp_start(&netif);
#endif
/* When the netif is fully configured this function must be called.*/
netif_set_up(&netif);
}
/**
* @brief called when a frame is received
* @param none
* @retval none
*/
void lwip_pkt_handle(void) {
/* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */
if (ethernetif_input(&netif) != ERR_OK) {
while (1);
}
}
void lwip_pkt_set_queue_handle(osMessageQueueId_t EthDataQueue) {
/* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */
if (ethernetif_set_queue_input(&netif, EthDataQueue) != ERR_OK) {
// while (1);
}
}
void lwip_pkt_get_queue_handle(struct pbuf *p) {
/* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */
if (ethernetif_get_queue_input(&netif, p) != ERR_OK) {
// while (1);
}
}
/**
* @brief lwip periodic tasks
* @param localtime the current localtime value
* @retval none
*/
void lwip_periodic_handle(volatile uint32_t localtime) {
/* TCP periodic process every 250 ms */
if (localtime - tcp_timer >= TCP_TMR_INTERVAL) {
tcp_timer = localtime;
tcp_tmr();
}
/* ARP periodic process every 5s */
if (localtime - arp_timer >= ARP_TMR_INTERVAL) {
arp_timer = localtime;
etharp_tmr();
}
#if LWIP_DHCP
/* Fine DHCP periodic process every 500ms */
if (localtime - dhcp_fine_timer >= DHCP_FINE_TIMER_MSECS)
{
dhcp_fine_timer = localtime;
dhcp_fine_tmr();
}
/* DHCP Coarse periodic process every 60s */
if (localtime - dhcp_coarse_timer >= DHCP_COARSE_TIMER_MSECS)
{
dhcp_coarse_timer = localtime;
dhcp_coarse_tmr();
}
#endif
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/