mirror of
https://github.com/eclipse/paho.mqtt-sn.embedded-c.git
synced 2025-12-15 16:36:52 +01:00
Refacter UDP SensorNetwork
Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
@@ -170,7 +170,7 @@ int SensorNetwork::broadcast(const uint8_t* payload, uint16_t payloadLength)
|
|||||||
|
|
||||||
int SensorNetwork::read(uint8_t* buf, uint16_t bufLen)
|
int SensorNetwork::read(uint8_t* buf, uint16_t bufLen)
|
||||||
{
|
{
|
||||||
return UDPPort::recv(buf, bufLen, &_clientAddr);
|
return UDPPort::recv(buf, bufLen, &_senderAddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -237,7 +237,7 @@ const char* SensorNetwork::getDescription(void)
|
|||||||
|
|
||||||
SensorNetAddress* SensorNetwork::getSenderAddress(void)
|
SensorNetAddress* SensorNetwork::getSenderAddress(void)
|
||||||
{
|
{
|
||||||
return &_clientAddr;
|
return &_senderAddr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*=========================================
|
/*=========================================
|
||||||
@@ -283,8 +283,8 @@ int UDPPort::open(const char* ipAddress, uint16_t multiPortNo, uint16_t uniPortN
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint32_t ip = inet_addr(ipAddress);
|
uint32_t ip = inet_addr(ipAddress);
|
||||||
_grpAddr.setAddress(ip, htons(multiPortNo));
|
_multicastAddr.setAddress(ip, htons(multiPortNo));
|
||||||
_clientAddr.setAddress(ip, htons(uniPortNo));
|
_unicastAddr.setAddress(ip, htons(uniPortNo));
|
||||||
_ttl = ttl;
|
_ttl = ttl;
|
||||||
|
|
||||||
/*------ Create unicast socket --------*/
|
/*------ Create unicast socket --------*/
|
||||||
@@ -327,7 +327,7 @@ int UDPPort::open(const char* ipAddress, uint16_t multiPortNo, uint16_t uniPortN
|
|||||||
|
|
||||||
sockaddr_in addrm;
|
sockaddr_in addrm;
|
||||||
addrm.sin_family = AF_INET;
|
addrm.sin_family = AF_INET;
|
||||||
addrm.sin_port = _grpAddr.getPortNo();
|
addrm.sin_port = _multicastAddr.getPortNo();
|
||||||
addrm.sin_addr.s_addr = INADDR_ANY;
|
addrm.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
if (::bind(_sockfdMulticast, (sockaddr*) &addrm, sizeof(addrm)) < 0)
|
if (::bind(_sockfdMulticast, (sockaddr*) &addrm, sizeof(addrm)) < 0)
|
||||||
@@ -344,7 +344,7 @@ int UDPPort::open(const char* ipAddress, uint16_t multiPortNo, uint16_t uniPortN
|
|||||||
|
|
||||||
ip_mreq mreq;
|
ip_mreq mreq;
|
||||||
mreq.imr_interface.s_addr = INADDR_ANY;
|
mreq.imr_interface.s_addr = INADDR_ANY;
|
||||||
mreq.imr_multiaddr.s_addr = _grpAddr.getIpAddress();
|
mreq.imr_multiaddr.s_addr = _multicastAddr.getIpAddress();
|
||||||
|
|
||||||
if (setsockopt(_sockfdMulticast, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
|
if (setsockopt(_sockfdMulticast, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
|
||||||
{
|
{
|
||||||
@@ -387,7 +387,7 @@ int UDPPort::unicast(const uint8_t* buf, uint32_t length, SensorNetAddress* addr
|
|||||||
|
|
||||||
int UDPPort::broadcast(const uint8_t* buf, uint32_t length)
|
int UDPPort::broadcast(const uint8_t* buf, uint32_t length)
|
||||||
{
|
{
|
||||||
return unicast(buf, length, &_grpAddr);
|
return unicast(buf, length, &_multicastAddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
int UDPPort::recv(uint8_t* buf, uint16_t len, SensorNetAddress* addr)
|
int UDPPort::recv(uint8_t* buf, uint16_t len, SensorNetAddress* addr)
|
||||||
@@ -420,7 +420,7 @@ int UDPPort::recv(uint8_t* buf, uint16_t len, SensorNetAddress* addr)
|
|||||||
}
|
}
|
||||||
else if (FD_ISSET(_sockfdMulticast, &recvfds))
|
else if (FD_ISSET(_sockfdMulticast, &recvfds))
|
||||||
{
|
{
|
||||||
rc = recvfrom(_sockfdMulticast, buf, len, 0, &_grpAddr);
|
rc = recvfrom(_sockfdMulticast, buf, len, 0, &_multicastAddr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return rc;
|
return rc;
|
||||||
|
|||||||
@@ -73,8 +73,8 @@ private:
|
|||||||
int _sockfdUnicast;
|
int _sockfdUnicast;
|
||||||
int _sockfdMulticast;
|
int _sockfdMulticast;
|
||||||
|
|
||||||
SensorNetAddress _grpAddr;
|
SensorNetAddress _multicastAddr;
|
||||||
SensorNetAddress _clientAddr;
|
SensorNetAddress _unicastAddr;
|
||||||
bool _disconReq;
|
bool _disconReq;
|
||||||
unsigned int _ttl;
|
unsigned int _ttl;
|
||||||
};
|
};
|
||||||
@@ -96,7 +96,7 @@ public:
|
|||||||
SensorNetAddress* getSenderAddress(void);
|
SensorNetAddress* getSenderAddress(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SensorNetAddress _clientAddr; // Sender's address. not gateway's one.
|
SensorNetAddress _senderAddr;
|
||||||
string _description;
|
string _description;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user