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

Commita740b1c

Browse files
Improvements from arduino-esp32 repo
Added printTo(). Added index operators and type converters. See test project here:https://github.com/mrengineer7777/MA_Test. No further changes expected.
1 parentf63a58a commita740b1c

File tree

4 files changed

+184
-53
lines changed

4 files changed

+184
-53
lines changed

‎api/MacAddress.cpp‎

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#include<MacAddress.h>
12
#include<stdio.h>
2-
#include"MacAddress.h"
3+
#include<Print.h>
34

45
//Default constructor, blank mac address.
56
MacAddress::MacAddress() {
@@ -14,6 +15,15 @@ MacAddress::MacAddress(const uint8_t *macbytearray) {
1415
memcpy(_mac.bytes, macbytearray,sizeof(_mac.bytes));
1516
}
1617

18+
MacAddress::MacAddress(uint8_t b1,uint8_t b2,uint8_t b3,uint8_t b4,uint8_t b5,uint8_t b6) {
19+
_mac.bytes[0] = b1;
20+
_mac.bytes[1] = b2;
21+
_mac.bytes[2] = b3;
22+
_mac.bytes[3] = b4;
23+
_mac.bytes[4] = b5;
24+
_mac.bytes[5] = b6;
25+
}
26+
1727
//Parse user entered string into MAC address
1828
boolMacAddress::fromCStr(constchar *buf) {
1929
char cs[18];
@@ -63,33 +73,74 @@ uint64_t MacAddress::Value() {
6373
return _mac.val;
6474
}
6575

66-
//Implicit conversion object to number [same as .Value()]
67-
MacAddress::operatoruint64_t()const
68-
{
69-
return _mac.val;
76+
//Allow getting individual octets of the address. e.g. uint8_t b0 = ma[0];
77+
uint8_tMacAddress::operator[](int index)const {
78+
index =EnforceIndexBounds(index);
79+
return _mac.bytes[index];
7080
}
7181

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));
82+
//Allow setting individual octets of the address. e.g. ma[2] = 255;
83+
uint8_t& MacAddress::operator[](int index) {
84+
index =EnforceIndexBounds(index);
85+
return _mac.bytes[index];
86+
}
87+
88+
//Overloaded copy operator: init MacAddress object from byte array
89+
MacAddress& MacAddress::operator=(constuint8_t *macbytearray) {
90+
memcpy(_mac.bytes, macbytearray,sizeof(_mac.bytes));
7691
return *this;
7792
}
7893

79-
MacAddress& MacAddress::operator=(uint64_t macval)
80-
{
94+
//Overloaded copyoperator: init MacAddress object from uint64_t
95+
MacAddress& MacAddress::operator=(uint64_t macval){
8196
_mac.val = macval;
8297
return *this;
8398
}
8499

85100
//Compare class to byte array
86-
bool MacAddress::operator==(constuint8_t *mac)const
87-
{
88-
return !memcmp(_mac.bytes, mac,sizeof(_mac.bytes));
101+
bool MacAddress::operator==(constuint8_t *macbytearray)const {
102+
return !memcmp(_mac.bytes, macbytearray,sizeof(_mac.bytes));
89103
}
90104

91105
//Allow comparing value of two classes
92-
bool MacAddress::operator==(const MacAddress& mac2)const
93-
{
106+
bool MacAddress::operator==(const MacAddress& mac2)const {
94107
return _mac.val == mac2._mac.val;
95108
}
109+
110+
//Type converter object to uint64_t [same as .Value()]
111+
MacAddress::operatoruint64_t()const {
112+
return _mac.val;
113+
}
114+
115+
//Type converter object to read only pointer to mac bytes. e.g. const uint8_t *ip_8 = ma;
116+
MacAddress::operatorconstuint8_t*()const {
117+
return _mac.bytes;
118+
}
119+
120+
//Type converter object to read only pointer to mac value. e.g. const uint32_t *ip_64 = ma;
121+
MacAddress::operatorconstuint64_t*()const {
122+
return &_mac.val;
123+
}
124+
125+
size_tMacAddress::printTo(Print& p)const
126+
{
127+
size_t n =0;
128+
for(int i =0; i <6; i++) {
129+
if(i){
130+
n += p.print(':');
131+
}
132+
n += p.printf("%02X", _mac.bytes[i]);
133+
}
134+
return n;
135+
}
136+
137+
//Bounds checking
138+
intMacAddress::EnforceIndexBounds(int i)const {
139+
if(i <0) {
140+
return0;
141+
}
142+
if(i >=sizeof(_mac.bytes)) {
143+
returnsizeof(_mac.bytes)-1;
144+
}
145+
return i;
146+
}

‎api/MacAddress.h‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
#defineMacAddress_h
2020

2121
#include<stdint.h>
22-
#include"String.h"
23-
24-
namespacearduino {
22+
#include<WString.h>
23+
#include<Printable.h>
2524

2625
// A class to make it easier to handle and pass around 6-byte BSSID and MAC addresses.
27-
classMacAddress {
26+
classMacAddress:publicPrintable{
2827
private:
2928
union {
3029
struct {
@@ -38,6 +37,7 @@ class MacAddress {
3837
MacAddress();
3938
MacAddress(uint64_t mac);
4039
MacAddress(constuint8_t *macbytearray);
40+
MacAddress(uint8_t b1,uint8_t b2,uint8_t b3,uint8_t b4,uint8_t b5,uint8_t b6);
4141
virtual~MacAddress() {}
4242
boolfromCStr(constchar *buf);
4343
boolfromString(const String &macstr);
@@ -46,14 +46,28 @@ class MacAddress {
4646
StringtoString()const;
4747
uint64_tValue();
4848

49-
operatoruint64_t()const;
50-
MacAddress&operator=(constuint8_t *mac);
49+
uint8_toperator[](int index)const;
50+
uint8_t&operator[](int index);
51+
MacAddress&operator=(constuint8_t *macbytearray);
5152
MacAddress&operator=(uint64_t macval);
52-
booloperator==(constuint8_t *mac)const;
53+
booloperator==(constuint8_t *macbytearray)const;
5354
booloperator==(const MacAddress& mac2)const;
54-
};
55+
operatoruint64_t()const;
56+
operatorconstuint8_t*()const;
57+
operatorconstuint64_t*()const;
5558

56-
}
57-
using arduino::MacAddress;
59+
virtualsize_tprintTo(Print& p)const;
60+
61+
// future use in Arduino Networking
62+
friendclassEthernetClass;
63+
friendclassUDP;
64+
friendclassClient;
65+
friendclassServer;
66+
friendclassDhcpClass;
67+
friendclassDNSClient;
68+
69+
private:
70+
intEnforceIndexBounds(int i)const;
71+
};
5872

5973
#endif

‎api/MacAddress8.cpp‎

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#include<MacAddress8.h>
12
#include<stdio.h>
2-
#include"MacAddress8.h"
3+
#include<Print.h>
34

45
//Default constructor, blank mac address.
56
MacAddress8::MacAddress8() {
@@ -14,6 +15,17 @@ MacAddress8::MacAddress8(const uint8_t *macbytearray) {
1415
memcpy(_mac.bytes, macbytearray,sizeof(_mac.bytes));
1516
}
1617

18+
MacAddress8::MacAddress8(uint8_t b1,uint8_t b2,uint8_t b3,uint8_t b4,uint8_t b5,uint8_t b6,uint8_t b7,uint8_t b8) {
19+
_mac.bytes[0] = b1;
20+
_mac.bytes[1] = b2;
21+
_mac.bytes[2] = b3;
22+
_mac.bytes[3] = b4;
23+
_mac.bytes[4] = b5;
24+
_mac.bytes[5] = b6;
25+
_mac.bytes[6] = b7;
26+
_mac.bytes[7] = b8;
27+
}
28+
1729
//Parse user entered string into MAC address
1830
boolMacAddress8::fromCStr(constchar *buf) {
1931
char cs[24];
@@ -63,33 +75,73 @@ uint64_t MacAddress8::Value() {
6375
return _mac.val;
6476
}
6577

66-
//Implicit conversion object to number [same as .Value()]
67-
MacAddress8::operatoruint64_t()const
68-
{
69-
return _mac.val;
78+
uint8_t MacAddress8::operator[](int index)const {
79+
index =EnforceIndexBounds(index);
80+
return _mac.bytes[index];
7081
}
7182

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));
83+
//Allow setting individual octets of the address. e.g. ma[2] = 255;
84+
uint8_t& MacAddress8::operator[](int index) {
85+
index =EnforceIndexBounds(index);
86+
return _mac.bytes[index];
87+
}
88+
89+
//Overloaded copy operator: init MacAddress object from byte array
90+
MacAddress8& MacAddress8::operator=(constuint8_t *macbytearray) {
91+
memcpy(_mac.bytes, macbytearray,sizeof(_mac.bytes));
7692
return *this;
7793
}
7894

79-
MacAddress8& MacAddress8::operator=(uint64_t macval)
80-
{
95+
//Overloaded copyoperator: init MacAddress object from uint64_t
96+
MacAddress8& MacAddress8::operator=(uint64_t macval){
8197
_mac.val = macval;
8298
return *this;
8399
}
84100

85101
//Compare class to byte array
86-
bool MacAddress8::operator==(constuint8_t *mac)const
87-
{
88-
return !memcmp(_mac.bytes, mac,sizeof(_mac.bytes));
102+
bool MacAddress8::operator==(constuint8_t *macbytearray)const {
103+
return !memcmp(_mac.bytes, macbytearray,sizeof(_mac.bytes));
89104
}
90105

91106
//Allow comparing value of two classes
92-
bool MacAddress8::operator==(const MacAddress8& mac2)const
93-
{
107+
bool MacAddress8::operator==(const MacAddress8& mac2)const {
94108
return _mac.val == mac2._mac.val;
95109
}
110+
111+
//Type converter object to uint64_t [same as .Value()]
112+
MacAddress8::operatoruint64_t()const {
113+
return _mac.val;
114+
}
115+
116+
//Type converter object to read only pointer to mac bytes. e.g. const uint8_t *ip_8 = ma;
117+
MacAddress8::operatorconstuint8_t*()const {
118+
return _mac.bytes;
119+
}
120+
121+
//Type converter object to read only pointer to mac value. e.g. const uint32_t *ip_64 = ma;
122+
MacAddress8::operatorconstuint64_t*()const {
123+
return &_mac.val;
124+
}
125+
126+
size_tMacAddress8::printTo(Print& p)const
127+
{
128+
size_t n =0;
129+
for(int i =0; i <8; i++) {
130+
if(i){
131+
n += p.print(':');
132+
}
133+
n += p.printf("%02X", _mac.bytes[i]);
134+
}
135+
return n;
136+
}
137+
138+
//Bounds checking
139+
intMacAddress8::EnforceIndexBounds(int i)const {
140+
if(i <0) {
141+
return0;
142+
}
143+
if(i >=sizeof(_mac.bytes)) {
144+
returnsizeof(_mac.bytes)-1;
145+
}
146+
return i;
147+
}

‎api/MacAddress8.h‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
#defineMacAddress8_h
2020

2121
#include<stdint.h>
22-
#include"String.h"
23-
24-
namespacearduino {
22+
#include<WString.h>
23+
#include<Printable.h>
2524

2625
// 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 {
26+
classMacAddress8:publicPrintable{
2827
private:
2928
union {
3029
uint8_t bytes[8];
@@ -35,22 +34,37 @@ class MacAddress8 {
3534
MacAddress8();
3635
MacAddress8(uint64_t mac);
3736
MacAddress8(constuint8_t *macbytearray);
37+
MacAddress8(uint8_t b1,uint8_t b2,uint8_t b3,uint8_t b4,uint8_t b5,uint8_t b6,uint8_t b7,uint8_t b8);
3838
virtual~MacAddress8() {}
3939
boolfromCStr(constchar *buf);
4040
boolfromString(const String &macstr);
4141
voidtoBytes(uint8_t *buf);
42-
inttoCStr(char *buf);
42+
inttoCStr(char *buf);
4343
StringtoString()const;
4444
uint64_tValue();
4545

46-
operatoruint64_t()const;
47-
MacAddress8&operator=(constuint8_t *mac);
46+
uint8_toperator[](int index)const;
47+
uint8_t&operator[](int index);
48+
MacAddress8&operator=(constuint8_t *macbytearray);
4849
MacAddress8&operator=(uint64_t macval);
49-
booloperator==(constuint8_t *mac)const;
50+
booloperator==(constuint8_t *macbytearray)const;
5051
booloperator==(const MacAddress8& mac2)const;
51-
};
52+
operatoruint64_t()const;
53+
operatorconstuint8_t*()const;
54+
operatorconstuint64_t*()const;
5255

53-
}
54-
using arduino::MacAddress8;
56+
virtualsize_tprintTo(Print& p)const;
57+
58+
// future use in Arduino Networking
59+
friendclassEthernetClass;
60+
friendclassUDP;
61+
friendclassClient;
62+
friendclassServer;
63+
friendclassDhcpClass;
64+
friendclassDNSClient;
65+
66+
private:
67+
intEnforceIndexBounds(int i)const;
68+
};
5569

5670
#endif

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp