Skip to content

Commit

Permalink
Feature: Added wifi config support for static IP (#959)
Browse files Browse the repository at this point in the history
In addition to DHCP, static IP is now configurable via
uWifiCfgConfigure(devHandle, pCfg)
  • Loading branch information
mekm authored Aug 29, 2023
1 parent 554d008 commit dc8420c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 9 deletions.
1 change: 0 additions & 1 deletion wifi/api/u_wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ typedef struct {
uint8_t grpCipherBitmask; /**< group cipher bitmask, see U_WIFI_CIPHER_MASK_xx defines for values. */
} uWifiScanResult_t;


/** Scan result callback type.
*
* This callback will be called once for each entry found.
Expand Down
13 changes: 12 additions & 1 deletion wifi/api/u_wifi_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,23 @@ extern "C" {
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */

#define U_WIFI_IP_ADDR_STR_MAX_LEN 16 /**< Max length of IP string, including null terminator. */

/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */

typedef struct {
bool notUsed; //DHCP/static TODO
uint8_t IPv4Addr[U_WIFI_IP_ADDR_STR_MAX_LEN];
uint8_t subnetMask[U_WIFI_IP_ADDR_STR_MAX_LEN];
uint8_t defaultGW[U_WIFI_IP_ADDR_STR_MAX_LEN];
uint8_t DNS1[U_WIFI_IP_ADDR_STR_MAX_LEN];
uint8_t DNS2[U_WIFI_IP_ADDR_STR_MAX_LEN];
} uWifiIpCfg_t;

typedef struct {
bool dhcp;
uWifiIpCfg_t wifiIpCfg;
} uWifiCfg_t;

/* ----------------------------------------------------------------
Expand Down
5 changes: 1 addition & 4 deletions wifi/src/u_wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

#include "u_wifi_module_type.h"
#include "u_wifi.h"
#include "u_wifi_cfg.h"

#include "u_hex_bin_convert.h"

Expand Down Expand Up @@ -767,10 +768,6 @@ int32_t uWifiStationConnect(uDeviceHandle_t devHandle, const char *pSsid,
// Set PSK/passphrase
errorCode = writeWifiStaCfgStr(atHandle, 0, 8, pPassPhrase);
}
if (errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) {
// Set IP mode to static IP
errorCode = writeWifiStaCfgInt(atHandle, 0, 100, 2);
}
}
if (errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) {
// Activate wifi
Expand Down
66 changes: 64 additions & 2 deletions wifi/src/u_wifi_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,64 @@ static int32_t restart(const uAtClientHandle_t atHandle, bool store)
return error;
}

static int32_t uWifiStationSetStaticIP(const uAtClientHandle_t atHandle,
const uWifiIpCfg_t *wifiIpCfg)
{
int32_t error = (int32_t) U_ERROR_COMMON_SUCCESS;

uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UWSC=");
uAtClientWriteInt(atHandle, 0);
uAtClientWriteInt(atHandle, 100);
uAtClientWriteInt(atHandle, 1);
uAtClientWriteInt(atHandle, 101);
uAtClientWriteString(atHandle, (const char *)wifiIpCfg->IPv4Addr, false);
uAtClientCommandStopReadResponse(atHandle);
error = uAtClientUnlock(atHandle);

if (error == (int32_t) U_ERROR_COMMON_SUCCESS) {
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UWSC=");
uAtClientWriteInt(atHandle, 0);
uAtClientWriteInt(atHandle, 102);
uAtClientWriteString(atHandle, (const char *)wifiIpCfg->subnetMask, false);
uAtClientWriteInt(atHandle, 103);
uAtClientWriteString(atHandle, (const char *)wifiIpCfg->defaultGW, false);
uAtClientCommandStopReadResponse(atHandle);
error = uAtClientUnlock(atHandle);
}

if (error == (int32_t) U_ERROR_COMMON_SUCCESS) {
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UWSC=");
uAtClientWriteInt(atHandle, 0);
uAtClientWriteInt(atHandle, 104);
uAtClientWriteString(atHandle, (const char *)wifiIpCfg->DNS1, false);
uAtClientWriteInt(atHandle, 105);
uAtClientWriteString(atHandle, (const char *)wifiIpCfg->DNS2, false);

uAtClientCommandStopReadResponse(atHandle);
error = uAtClientUnlock(atHandle);
}

return error;
}

static int32_t uWifiStationSetDHCP(const uAtClientHandle_t atHandle)
{
int32_t error = (int32_t) U_ERROR_COMMON_SUCCESS;

uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UWSC=");
uAtClientWriteInt(atHandle, 0);
uAtClientWriteInt(atHandle, 100);
uAtClientWriteInt(atHandle, 2);
uAtClientCommandStopReadResponse(atHandle);
error = uAtClientUnlock(atHandle);

return error;
}

/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS
* -------------------------------------------------------------- */
Expand All @@ -134,10 +192,14 @@ int32_t uWifiCfgConfigure(uDeviceHandle_t devHandle,

pInstance = pUShortRangePrivateGetInstance(devHandle);
if (pInstance != NULL) {
atHandle = pInstance->atHandle;
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
if (pCfg->dhcp) {
uWifiStationSetDHCP(atHandle);
} else {
uWifiStationSetStaticIP(atHandle, &pCfg->wifiIpCfg);
}
bool restartNeeded = false;
atHandle = pInstance->atHandle;


int32_t mode = getStartupMode(atHandle);
if (mode != U_WIFI_CFG_STARTUP_MODE_EDM) {
Expand Down
8 changes: 7 additions & 1 deletion wifi/test/u_wifi_cfg_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ U_PORT_TEST_FUNCTION("[wifiCfg]", "wifiCfgConfigureModule")
{
int32_t heapUsed;
uWifiCfg_t cfg;
uWifiIpCfg_t wifiIpCfg = {"172.0.1.100", "255.255.255.0", "172.0.1.1", "172.0.1.2", "172.0.1.3" };
uShortRangeUartConfig_t uart = { .uartPort = U_CFG_APP_SHORT_RANGE_UART,
.baudRate = U_SHORT_RANGE_UART_BAUD_RATE,
.pinTx = U_CFG_APP_PIN_SHORT_RANGE_TXD,
Expand All @@ -117,7 +118,12 @@ U_PORT_TEST_FUNCTION("[wifiCfg]", "wifiCfgConfigureModule")
U_PORT_TEST_ASSERT(uWifiTestPrivatePreamble((uWifiModuleType_t) U_CFG_TEST_SHORT_RANGE_MODULE_TYPE,
&uart,
&gHandles) == 0);
cfg.notUsed = false;

cfg.dhcp = false; // set static IP
cfg.wifiIpCfg = wifiIpCfg;
U_PORT_TEST_ASSERT(uWifiCfgConfigure(gHandles.devHandle, &cfg) == 0);

cfg.dhcp = true; // set DHCP
U_PORT_TEST_ASSERT(uWifiCfgConfigure(gHandles.devHandle, &cfg) == 0);


Expand Down

0 comments on commit dc8420c

Please sign in to comment.