Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of my_eeprom_funcs by
device_configuration.cpp
- Committer:
- olympux
- Date:
- 2016-08-20
- Revision:
- 17:574f90b11417
- Parent:
- 16:700377cd9d29
- Child:
- 18:d88314ae7979
File content as of revision 17:574f90b11417:
#include "mbed.h"
#include "device_configuration.h"
/*
* Debug option
*/
#if 0
//Enable debug
#include <cstdio>
#define INFO(x, ...) std::printf(""x"\r\n", ##__VA_ARGS__);
#define DBG(x, ...) std::printf("[dev_conf: DBG]"x"\r\n", ##__VA_ARGS__);
#define WARN(x, ...) std::printf("[dev_conf: WARN]"x"\r\n", ##__VA_ARGS__);
#define ERR(x, ...) std::printf("[dev_conf: ERR]"x"\r\n", ##__VA_ARGS__);
#else
//Disable debug
#define INFO(x, ...)
#define DBG(x, ...)
#define WARN(x, ...)
#define ERR(x, ...)
#endif
/*
* Variables for network configuration
*/
// Device configuration
uint16_t u16IpAddr[4], u16IpSubnet[4], u16IpGateway[4], u16MacAddr[3]; // 16-bits variables to be compatible with eeprom functions
char strIpAddr[16], strIpSubnet[16], strIpGateway[16], strMacAddr[20]; // RPC variables, converted from 16-bits u16ip_xxx
uint16_t u16DeviceConfiguredFlag = 0; // flag indicates whether device has been configured (0xA5A5) or not
// TCP server/UDP
uint16_t u16LocalTcpServerPort = DEFAULT_LOCAL_TCP_SERVER_PORT;
uint16_t u16LocalUdpPort = DEFAULT_LOCAL_UDP_PORT;
// TCP client mode, set parameters of the remote TCP server this device connects to.
// When enabled, this device will send its status to the server every transmit_time_period.
uint16_t u16RemoteTcpServerIpAddr[4]; // 16-bit variable to be compatible with eeprom functions
char strRemoteTcpServerIpAddr[16]; // RPC variable, converted from 16-bits u16server_ip_addr
uint16_t u16RemoteTcpServerPort; // 16-bit variable to be compatible with eeprom functions
uint16_t u16AutoTransmitFlag = 0, u16TransmitPeriod = DEFAULT_TRANSMIT_PERIOD; // auto transmit status, time period = 1s
// enable modes
uint16_t u16EnableTcpServer = 0, u16EnableTcpClient = 0, u16EnableUdp = DEFAULT_ENABLE_FLAG_VALUE; // flags for enabling TCP server/client and UDP (UDP is always on for configuration)
// extra
uint8_t u8IpAddr[4]; // keep device ip address in 8-bits
uint8_t u8MacAddr[6]; // keep mac in 8-bits
uint8_t u8RemoteTcpServerIpAddr[4]; // remote TCP server ip address in 8-bits
/*!
* Function to write module network configuration
* @param <char *buf> configuration buffer
* @note 4x16-bit IP address
* @note 4x16-bit subnet
* @note 4x16-bit gateway
* @note 3x16-bit MAC
* @note 16-bit TCP local port
* @note 16-bit UDP local port
*/
void write_device_configuration(uint16_t* ip, uint16_t* subnet, uint16_t* gateway,
uint16_t* mac, uint16_t tcp_port, uint16_t udp_port,
uint16_t* remote_ip, uint16_t remote_port, uint16_t auto_transmit, uint16_t transmit_period,
uint16_t enable_tcp_server, uint16_t enable_tcp_client, uint16_t enable_udp) {
// Write network configuration
// 4-byte IP address + 4-byte subnet + 4-byte gateway + 3-byte MAC
// + local TCP server port + local UDP port
INFO("Saving network configuration for this device...");
enableEEPROMWriting();
// erase first_run flag
writeEEPROMHalfWord(DEVICE_CONFIGURED_FLAG_POS, DEFAULT_ENABLE_FLAG_VALUE);
// IP address
writeEEPROMHalfWord(IP_ADDRESS_POS+2*0, ip[0]);
writeEEPROMHalfWord(IP_ADDRESS_POS+2*1, ip[1]);
writeEEPROMHalfWord(IP_ADDRESS_POS+2*2, ip[2]);
writeEEPROMHalfWord(IP_ADDRESS_POS+2*3, ip[3]);
// IP subnet
writeEEPROMHalfWord(IP_SUBNET_POS+2*0, subnet[0]);
writeEEPROMHalfWord(IP_SUBNET_POS+2*1, subnet[1]);
writeEEPROMHalfWord(IP_SUBNET_POS+2*2, subnet[2]);
writeEEPROMHalfWord(IP_SUBNET_POS+2*3, subnet[3]);
// IP gateway
writeEEPROMHalfWord(IP_GATEWAY_POS+2*0, gateway[0]);
writeEEPROMHalfWord(IP_GATEWAY_POS+2*1, gateway[1]);
writeEEPROMHalfWord(IP_GATEWAY_POS+2*2, gateway[2]);
writeEEPROMHalfWord(IP_GATEWAY_POS+2*3, gateway[3]);
// MAC address
writeEEPROMHalfWord(MAC_ADDRESS_POS+2*0, mac[0]);
writeEEPROMHalfWord(MAC_ADDRESS_POS+2*1, mac[1]);
writeEEPROMHalfWord(MAC_ADDRESS_POS+2*2, mac[2]);
// Ports
writeEEPROMHalfWord(LOCAL_TCP_SERVER_PORT_POS, tcp_port);
writeEEPROMHalfWord(LOCAL_UDP_PORT_POS, udp_port);
// Remote TCP server
writeEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*0, remote_ip[0]);
writeEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*1, remote_ip[1]);
writeEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*2, remote_ip[2]);
writeEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*3, remote_ip[3]);
// Remote port
writeEEPROMHalfWord(REMOTE_TCP_SERVER_PORT_POS, remote_port);
// Auto transmit
writeEEPROMHalfWord(AUTO_TRANSMIT_FLAG_POS, auto_transmit);
writeEEPROMHalfWord(AUTO_TRANSMIT_TIME_PERIOD_POS, transmit_period);
// Enable modes
writeEEPROMHalfWord(ENABLE_TCP_SERVER_POS, enable_tcp_server);
writeEEPROMHalfWord(ENABLE_TCP_CLIENT_POS, enable_tcp_client);
writeEEPROMHalfWord(ENABLE_UDP_MODE_POS, enable_udp);
disableEEPROMWriting();
INFO("Successful");
}
/*!
* Function to load module network configuration
*/
void read_device_configuration(void) {
mbed_mac_address((char *)u8MacAddr);
INFO("Loading network configuration...");
// check if configured
u16DeviceConfiguredFlag = readEEPROMHalfWord(DEVICE_CONFIGURED_FLAG_POS);
// if not first run, load network config
if (u16DeviceConfiguredFlag == DEFAULT_ENABLE_FLAG_VALUE) {
INFO("User settings");
// IP address
u16IpAddr[0] = readEEPROMHalfWord(IP_ADDRESS_POS+2*0);
u16IpAddr[1] = readEEPROMHalfWord(IP_ADDRESS_POS+2*1);
u16IpAddr[2] = readEEPROMHalfWord(IP_ADDRESS_POS+2*2);
u16IpAddr[3] = readEEPROMHalfWord(IP_ADDRESS_POS+2*3);
u8IpAddr[0] = (uint8_t)(u16IpAddr[0] & 0x00FF);
u8IpAddr[1] = (uint8_t)(u16IpAddr[1] & 0x00FF);
u8IpAddr[2] = (uint8_t)(u16IpAddr[2] & 0x00FF);
u8IpAddr[3] = (uint8_t)(u16IpAddr[3] & 0x00FF);
// IP subnet
u16IpSubnet[0] = readEEPROMHalfWord(IP_SUBNET_POS+2*0);
u16IpSubnet[1] = readEEPROMHalfWord(IP_SUBNET_POS+2*1);
u16IpSubnet[2] = readEEPROMHalfWord(IP_SUBNET_POS+2*2);
u16IpSubnet[3] = readEEPROMHalfWord(IP_SUBNET_POS+2*3);
// IP gateway
u16IpGateway[0] = readEEPROMHalfWord(IP_GATEWAY_POS+2*0);
u16IpGateway[1] = readEEPROMHalfWord(IP_GATEWAY_POS+2*1);
u16IpGateway[2] = readEEPROMHalfWord(IP_GATEWAY_POS+2*2);
u16IpGateway[3] = readEEPROMHalfWord(IP_GATEWAY_POS+2*3);
// MAC address
u16MacAddr[0] = readEEPROMHalfWord(MAC_ADDRESS_POS+2*0);
u16MacAddr[1] = readEEPROMHalfWord(MAC_ADDRESS_POS+2*1);
u16MacAddr[2] = readEEPROMHalfWord(MAC_ADDRESS_POS+2*2);
u8MacAddr[0] = DEFAULT_MAC0; u8MacAddr[1] = DEFAULT_MAC1; u8MacAddr[2] = DEFAULT_MAC2;
u8MacAddr[3] = (uint8_t)(u16MacAddr[0] & 0x00FF);
u8MacAddr[4] = (uint8_t)(u16MacAddr[1] & 0x00FF);
u8MacAddr[5] = (uint8_t)(u16MacAddr[2] & 0x00FF);
// Ports
u16LocalTcpServerPort = readEEPROMHalfWord(LOCAL_TCP_SERVER_PORT_POS);
u16LocalUdpPort = readEEPROMHalfWord(LOCAL_UDP_PORT_POS);
// Remote TCP server
u16RemoteTcpServerIpAddr[0] = readEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*0);
u16RemoteTcpServerIpAddr[1] = readEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*1);
u16RemoteTcpServerIpAddr[2] = readEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*2);
u16RemoteTcpServerIpAddr[3] = readEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*3);
u8RemoteTcpServerIpAddr[0] = (uint8_t)(u16RemoteTcpServerIpAddr[0] & 0x00FF);
u8RemoteTcpServerIpAddr[1] = (uint8_t)(u16RemoteTcpServerIpAddr[1] & 0x00FF);
u8RemoteTcpServerIpAddr[2] = (uint8_t)(u16RemoteTcpServerIpAddr[2] & 0x00FF);
u8RemoteTcpServerIpAddr[3] = (uint8_t)(u16RemoteTcpServerIpAddr[3] & 0x00FF);
// Remote port
u16RemoteTcpServerPort = readEEPROMHalfWord(REMOTE_TCP_SERVER_PORT_POS);
// Auto transmit
u16AutoTransmitFlag = readEEPROMHalfWord(AUTO_TRANSMIT_FLAG_POS);
u16TransmitPeriod = readEEPROMHalfWord(AUTO_TRANSMIT_TIME_PERIOD_POS);
// Enable modes
u16EnableTcpServer = readEEPROMHalfWord(ENABLE_TCP_SERVER_POS);
u16EnableTcpClient = readEEPROMHalfWord(ENABLE_TCP_CLIENT_POS);
u16EnableUdp = readEEPROMHalfWord(ENABLE_UDP_MODE_POS);
sprintf(strIpAddr, "%d.%d.%d.%d", u8IpAddr[0], u8IpAddr[1], u8IpAddr[2], u8IpAddr[3]);
sprintf(strIpSubnet, "%d.%d.%d.%d", (uint8_t)u16IpSubnet[0], (uint8_t)u16IpSubnet[1], (uint8_t)u16IpSubnet[2], (uint8_t)u16IpSubnet[3]);
sprintf(strIpGateway, "%d.%d.%d.%d", (uint8_t)u16IpGateway[0], (uint8_t)u16IpGateway[1], (uint8_t)u16IpGateway[2], (uint8_t)u16IpGateway[3]);
sprintf(strRemoteTcpServerIpAddr, "%d.%d.%d.%d", u8RemoteTcpServerIpAddr[0], u8RemoteTcpServerIpAddr[1], u8RemoteTcpServerIpAddr[2], u8RemoteTcpServerIpAddr[3]);
sprintf(strMacAddr, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", u8MacAddr[0], u8MacAddr[1], u8MacAddr[2], u8MacAddr[3], u8MacAddr[4], u8MacAddr[5]);
}
// if ip is not configured, use default addresses
else {
INFO("No user settings, load defaults");
u8MacAddr[0] = DEFAULT_MAC0; u8MacAddr[1] = DEFAULT_MAC1; u8MacAddr[2] = DEFAULT_MAC2;
u8MacAddr[3] = DEFAULT_MAC3; u8MacAddr[4] = DEFAULT_MAC4; u8MacAddr[5] = DEFAULT_MAC5;
sprintf(strIpAddr, DEFAULT_IP_ADDRESS);
sprintf(strIpSubnet, DEFAULT_IP_SUBNET);
sprintf(strIpGateway, DEFAULT_IP_GATEWAY);
sprintf(strRemoteTcpServerIpAddr, DEFAULT_REMOTE_TCP_SERVER_IP);
sprintf(strMacAddr, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", u8MacAddr[0], u8MacAddr[1], u8MacAddr[2], u8MacAddr[3], u8MacAddr[4], u8MacAddr[5]);
}
INFO("Successful");
INFO("IP: %s", strIpAddr);
INFO("MASK: %s", strIpSubnet);
INFO("GW: %s", strIpGateway);
INFO("TCP server local port: %d", u16LocalTcpServerPort);
INFO("UDP server local port: %d", u16LocalUdpPort);
INFO("TCP server: %s", (u16EnableTcpServer == DEFAULT_ENABLE_FLAG_VALUE) ? "enabled" : "disable");
INFO("TCP client: %s", (u16EnableTcpClient == DEFAULT_ENABLE_FLAG_VALUE) ? "enabled" : "disable");
INFO("UDP: %s", (u16EnableUdp == DEFAULT_ENABLE_FLAG_VALUE) ? "enabled" : "disable");
INFO("Remote TCP server: %s", strRemoteTcpServerIpAddr);
INFO("Remote TCP server port: %d", u16RemoteTcpServerPort);
INFO("Auto transmit: %s", (u16AutoTransmitFlag == DEFAULT_ENABLE_FLAG_VALUE) ? "enabled" : "disable");
INFO("Period: %d", u16TransmitPeriod);
}
void reset_device_configuration() {
// erase first_run flag
enableEEPROMWriting();
disableEEPROMWriting();
}
