Skip to content

Commit

Permalink
Add API mac_addr_from_str
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSomeMan committed May 4, 2023
1 parent 1f861ff commit 825c9b7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/mac_addr.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define RUUVI_MAC_ADDR_H

#include <stdint.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -42,6 +43,15 @@ mac_address_bin_init(mac_address_bin_t* p_mac, const uint8_t mac[MAC_ADDRESS_NUM
mac_address_str_t
mac_address_to_str(const mac_address_bin_t* p_mac);

/**
* @brief Convert a string in format "AA:BB:CC:DD:EE:FF" to binary format
* @param[IN] p_mac_addr_str - input string with MAC address
* @param[OUT] p_mac_addr_bin - pointer to output @def mac_address_bin_t where the converted value will be saved
* @return true if input string contains valid MAC address
*/
bool
mac_addr_from_str(const char* const p_mac_addr_str, mac_address_bin_t* p_mac_addr_bin);

#ifdef __cplusplus
}
#endif
Expand Down
44 changes: 44 additions & 0 deletions src/mac_addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "mac_addr.h"
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "str_buf.h"

void
Expand Down Expand Up @@ -37,3 +39,45 @@ mac_address_to_str(const mac_address_bin_t* p_mac)
}
return mac_str;
}

bool
mac_addr_from_str(const char* const p_mac_addr_str, mac_address_bin_t* p_mac_addr_bin)
{
if ((NULL == p_mac_addr_str) || (NULL == p_mac_addr_bin))
{
return false;
}

mac_address_str_t mac_addr_str;
if (strlen(p_mac_addr_str) >= sizeof(mac_addr_str))
{
return false;
}
strcpy(mac_addr_str.str_buf, p_mac_addr_str);

const char* const p_delim = ":";
char* p_save_ptr = NULL;
char* p_token = strtok_r(mac_addr_str.str_buf, p_delim, &p_save_ptr);

if (NULL == p_token)
{
return false;
}

for (size_t i = 0; i < MAC_ADDRESS_NUM_BYTES && p_token != NULL; ++i)
{
if (strlen(p_token) != 2 || !isxdigit(p_token[0]) || !isxdigit(p_token[1]))
{
return false;
}
p_mac_addr_bin->mac[i] = (uint8_t)strtol(p_token, NULL, 16);
p_token = strtok_r(NULL, p_delim, &p_save_ptr);
}

if (p_token != NULL)
{
return false;
}

return true;
}
17 changes: 17 additions & 0 deletions tests/test_mac_addr/test_mac_addr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,20 @@ TEST_F(TestMacAddr, test_mac_address_to_str) // NOLINT
const mac_address_str_t mac_str = mac_address_to_str(&mac_bin);
ASSERT_EQ(string("11:22:33:AA:BB:CC"), string(mac_str.str_buf));
}

TEST_F(TestMacAddr, test_mac_addr_from_str) // NOLINT
{
mac_address_bin_t mac_addr_bin = { 0 };
ASSERT_TRUE(mac_addr_from_str("11:22:33:AA:BB:CC", &mac_addr_bin));
ASSERT_EQ(0x11U, mac_addr_bin.mac[0]);
ASSERT_EQ(0x22U, mac_addr_bin.mac[1]);
ASSERT_EQ(0x33U, mac_addr_bin.mac[2]);
ASSERT_EQ(0xAAU, mac_addr_bin.mac[3]);
ASSERT_EQ(0xBBU, mac_addr_bin.mac[4]);
ASSERT_EQ(0xCCU, mac_addr_bin.mac[5]);

ASSERT_FALSE(mac_addr_from_str("11:22:33:AA:BB:CC", nullptr));
ASSERT_FALSE(mac_addr_from_str(nullptr, &mac_addr_bin));
ASSERT_FALSE(mac_addr_from_str("11:22:33:AA:BB:C", &mac_addr_bin));
ASSERT_FALSE(mac_addr_from_str("", &mac_addr_bin));
}

0 comments on commit 825c9b7

Please sign in to comment.