Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf63a58a

Browse files
Adding MacAddress, MacAddress8
Adding New Classes MacAddress, MacAddress8 in the style of IPAddress. Note I didn't include Printable, as I don't understand how that works. If someone else wants to add it later, they are welcome to do so. This is my first time contributing to this repository, so please feel free to point out any mistakes.This library originated on arduino-esp32 (espressif/arduino-esp32#6667). It was suggested that I contribute to the Arduino core as well :)
1 parente03b653 commitf63a58a

File tree

4 files changed

+305
-0
lines changed

4 files changed

+305
-0
lines changed

‎api/MacAddress.cpp‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include<stdio.h>
2+
#include"MacAddress.h"
3+
4+
//Default constructor, blank mac address.
5+
MacAddress::MacAddress() {
6+
_mac.val =0;
7+
}
8+
9+
MacAddress::MacAddress(uint64_t mac) {
10+
_mac.val = mac;
11+
}
12+
13+
MacAddress::MacAddress(constuint8_t *macbytearray) {
14+
memcpy(_mac.bytes, macbytearray,sizeof(_mac.bytes));
15+
}
16+
17+
//Parse user entered string into MAC address
18+
boolMacAddress::fromCStr(constchar *buf) {
19+
char cs[18];
20+
char *token;
21+
char *next;//Unused but required
22+
int i;
23+
24+
strncpy(cs, buf,sizeof(cs));//strtok modifies the buffer: copy to working buffer.
25+
26+
for(i=0; i<sizeof(_mac.bytes); i++) {
27+
token =strtok((i==0) ? cs :NULL,":");//Find first or next token
28+
if(!token) {//No more tokens found
29+
returnfalse;
30+
}
31+
_mac.bytes[i] =strtol(token, &next,16);
32+
}
33+
returntrue;
34+
}
35+
36+
//Parse user entered string into MAC address
37+
boolMacAddress::fromString(const String &macstr) {
38+
returnfromCStr(macstr.c_str());
39+
}
40+
41+
//Copy MAC into 6 byte array
42+
voidMacAddress::toBytes(uint8_t *buf) {
43+
memcpy(buf, _mac.bytes,sizeof(_mac.bytes));
44+
}
45+
46+
//Print MAC address into a C string.
47+
//MAC: Buffer must be at least 18 chars
48+
intMacAddress::toCStr(char *buf) {
49+
returnsprintf(buf,"%02X:%02X:%02X:%02X:%02X:%02X",
50+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2],
51+
_mac.bytes[3], _mac.bytes[4], _mac.bytes[5]);
52+
}
53+
54+
StringMacAddress::toString()const {
55+
char buf[18];
56+
sprintf(buf,"%02X:%02X:%02X:%02X:%02X:%02X",
57+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2],
58+
_mac.bytes[3], _mac.bytes[4], _mac.bytes[5]);
59+
returnString(buf);
60+
}
61+
62+
uint64_tMacAddress::Value() {
63+
return _mac.val;
64+
}
65+
66+
//Implicit conversion object to number [same as .Value()]
67+
MacAddress::operatoruint64_t()const
68+
{
69+
return _mac.val;
70+
}
71+
72+
//Overloaded copy operators to allow initialisation of MacAddress objects from other types
73+
MacAddress& MacAddress::operator=(constuint8_t *mac)
74+
{
75+
memcpy(_mac.bytes, mac,sizeof(_mac.bytes));
76+
return *this;
77+
}
78+
79+
MacAddress& MacAddress::operator=(uint64_t macval)
80+
{
81+
_mac.val = macval;
82+
return *this;
83+
}
84+
85+
//Compare class to byte array
86+
bool MacAddress::operator==(constuint8_t *mac)const
87+
{
88+
return !memcmp(_mac.bytes, mac,sizeof(_mac.bytes));
89+
}
90+
91+
//Allow comparing value of two classes
92+
bool MacAddress::operator==(const MacAddress& mac2)const
93+
{
94+
return _mac.val == mac2._mac.val;
95+
}

‎api/MacAddress.h‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//-----------------------------------------------------------------------------
2+
// MacAddress.h - class to make it easier to handle BSSID and MAC addresses.
3+
//
4+
// Copyright 2022 David McCurley
5+
// Licensed under the Apache License, Version 2.0 (the "License").
6+
// You may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//-----------------------------------------------------------------------------
17+
18+
#ifndef MacAddress_h
19+
#defineMacAddress_h
20+
21+
#include<stdint.h>
22+
#include"String.h"
23+
24+
namespacearduino {
25+
26+
// A class to make it easier to handle and pass around 6-byte BSSID and MAC addresses.
27+
classMacAddress {
28+
private:
29+
union {
30+
struct {
31+
uint8_t align[2];
32+
uint8_t bytes[6];
33+
};
34+
uint64_t val;
35+
} _mac;
36+
37+
public:
38+
MacAddress();
39+
MacAddress(uint64_t mac);
40+
MacAddress(constuint8_t *macbytearray);
41+
virtual~MacAddress() {}
42+
boolfromCStr(constchar *buf);
43+
boolfromString(const String &macstr);
44+
voidtoBytes(uint8_t *buf);
45+
inttoCStr(char *buf);
46+
StringtoString()const;
47+
uint64_tValue();
48+
49+
operatoruint64_t()const;
50+
MacAddress&operator=(constuint8_t *mac);
51+
MacAddress&operator=(uint64_t macval);
52+
booloperator==(constuint8_t *mac)const;
53+
booloperator==(const MacAddress& mac2)const;
54+
};
55+
56+
}
57+
using arduino::MacAddress;
58+
59+
#endif

‎api/MacAddress8.cpp‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include<stdio.h>
2+
#include"MacAddress8.h"
3+
4+
//Default constructor, blank mac address.
5+
MacAddress8::MacAddress8() {
6+
_mac.val =0;
7+
}
8+
9+
MacAddress8::MacAddress8(uint64_t mac) {
10+
_mac.val = mac;
11+
}
12+
13+
MacAddress8::MacAddress8(constuint8_t *macbytearray) {
14+
memcpy(_mac.bytes, macbytearray,sizeof(_mac.bytes));
15+
}
16+
17+
//Parse user entered string into MAC address
18+
boolMacAddress8::fromCStr(constchar *buf) {
19+
char cs[24];
20+
char *token;
21+
char *next;//Unused but required
22+
int i;
23+
24+
strncpy(cs, buf,sizeof(cs));//strtok modifies the buffer: copy to working buffer.
25+
26+
for(i=0; i<sizeof(_mac.bytes); i++) {
27+
token =strtok((i==0) ? cs :NULL,":");//Find first or next token
28+
if(!token) {//No more tokens found
29+
returnfalse;
30+
}
31+
_mac.bytes[i] =strtol(token, &next,16);
32+
}
33+
returntrue;
34+
}
35+
36+
//Parse user entered string into MAC address
37+
boolMacAddress8::fromString(const String &macstr) {
38+
returnfromCStr(macstr.c_str());
39+
}
40+
41+
//Copy MAC into 8 byte array
42+
voidMacAddress8::toBytes(uint8_t *buf) {
43+
memcpy(buf, _mac.bytes,sizeof(_mac.bytes));
44+
}
45+
46+
//Print MAC address into a C string.
47+
//EUI-64: Buffer must be at least 24 chars
48+
intMacAddress8::toCStr(char *buf) {
49+
returnsprintf(buf,"%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
50+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2], _mac.bytes[3],
51+
_mac.bytes[4], _mac.bytes[5], _mac.bytes[6], _mac.bytes[7]);
52+
}
53+
54+
StringMacAddress8::toString()const {
55+
char buf[24];
56+
sprintf(buf,"%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
57+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2], _mac.bytes[3],
58+
_mac.bytes[4], _mac.bytes[5], _mac.bytes[6], _mac.bytes[7]);
59+
returnString(buf);
60+
}
61+
62+
uint64_tMacAddress8::Value() {
63+
return _mac.val;
64+
}
65+
66+
//Implicit conversion object to number [same as .Value()]
67+
MacAddress8::operatoruint64_t()const
68+
{
69+
return _mac.val;
70+
}
71+
72+
//Overloaded copy operators to allow initialisation of MacAddress objects from other types
73+
MacAddress8& MacAddress8::operator=(constuint8_t *mac)
74+
{
75+
memcpy(_mac.bytes, mac,sizeof(_mac.bytes));
76+
return *this;
77+
}
78+
79+
MacAddress8& MacAddress8::operator=(uint64_t macval)
80+
{
81+
_mac.val = macval;
82+
return *this;
83+
}
84+
85+
//Compare class to byte array
86+
bool MacAddress8::operator==(constuint8_t *mac)const
87+
{
88+
return !memcmp(_mac.bytes, mac,sizeof(_mac.bytes));
89+
}
90+
91+
//Allow comparing value of two classes
92+
bool MacAddress8::operator==(const MacAddress8& mac2)const
93+
{
94+
return _mac.val == mac2._mac.val;
95+
}

‎api/MacAddress8.h‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//-----------------------------------------------------------------------------
2+
// MacAddress8.h - class to make it easier to handle 8-byte EUI-64 addresses.
3+
//
4+
// Copyright 2022 David McCurley
5+
// Licensed under the Apache License, Version 2.0 (the "License").
6+
// You may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//-----------------------------------------------------------------------------
17+
18+
#ifndef MacAddress8_h
19+
#defineMacAddress8_h
20+
21+
#include<stdint.h>
22+
#include"String.h"
23+
24+
namespacearduino {
25+
26+
// A class to make it easier to handle and pass around 8-byte EUI-64(used for IEEE 802.15.4) addresses. See <esp_mac.h>.
27+
classMacAddress8 {
28+
private:
29+
union {
30+
uint8_t bytes[8];
31+
uint64_t val;
32+
} _mac;
33+
34+
public:
35+
MacAddress8();
36+
MacAddress8(uint64_t mac);
37+
MacAddress8(constuint8_t *macbytearray);
38+
virtual~MacAddress8() {}
39+
boolfromCStr(constchar *buf);
40+
boolfromString(const String &macstr);
41+
voidtoBytes(uint8_t *buf);
42+
inttoCStr(char *buf);
43+
StringtoString()const;
44+
uint64_tValue();
45+
46+
operatoruint64_t()const;
47+
MacAddress8&operator=(constuint8_t *mac);
48+
MacAddress8&operator=(uint64_t macval);
49+
booloperator==(constuint8_t *mac)const;
50+
booloperator==(const MacAddress8& mac2)const;
51+
};
52+
53+
}
54+
using arduino::MacAddress8;
55+
56+
#endif

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp