inquire about adding C++ code to exist library

I have a problem like this .

http://forum.arduino.cc/index.php?topic=345134.5

I want to know how to add the code which was written in a comment to library of UDP

The beginPacketMAC and endPacketMAC functions dont exist so lets add them to libraries\ethernet\src\ethernetudp.cpp and the header file

Code: [Select]

int EthernetUDP::beginPacketMAC(IPAddress ip, uint8_t* mac, uint16_t port)
{
_offset = 0;
return startUDPMAC(_sock, rawIPAddress(ip), mac, port);
}

int EthernetUDP::endPacketMAC()
{
return sendUDPMAC(_sock);
}

The startUDPMAC and sendUDPMAC functions also dont exist so lets add them to libraries\ethernet\src\utility\socket.h and the header file ... this is where the real work gets done.

Code: [Select]

int startUDPMAC(SOCKET s, uint8_t* addr, uint8_t* mac, uint16_t port)
{
if
(((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
((mac[0] == 0x00) && (mac[1] == 0x00) && (mac[2] == 0x00) && (mac[3] == 0x00) && (mac[4] == 0x00) && (mac[5] == 0x00)) ||
(port == 0x00)
)
{
return 0;
}
else
{
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.writeSnDIPR(s, addr);
// this is where we tell it the mac address we are sending to
W5100.writeSnDHAR(s, mac);
W5100.writeSnDPORT(s, port);
SPI.endTransaction();
return 1;
}
}

int sendUDPMAC(SOCKET s)
{
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.execCmdSn(s, Sock_SEND_MAC);

/* +2008.01 bj /
while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK )
{
if (W5100.readSnIR(s) & SnIR::TIMEOUT)
{
/
+2008.01 [bj]: clear interrupt */
W5100.writeSnIR(s, (SnIR::SEND_OK|SnIR::TIMEOUT));
SPI.endTransaction();
return 0;
}
SPI.endTransaction();
yield();
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
}

/* +2008.01 bj */
W5100.writeSnIR(s, SnIR::SEND_OK);
SPI.endTransaction();

/* Sent ok */
return 1;
}

and your question ? (btw you've used code-tags but i think the code is not in between them)

How to add theese fuctions
such as int EthernetUDP::beginPacketMAC(IPAddress ip, uint8_t* mac, uint16_t port)

OK, well best practice is to copy the whole ethernet folder and put it somewhere out of the compilers reach
like this you keep the original copies.
now add the new functions to the EthernetUdp.cpp file somewhere in between 2 other functions,
And then add the function declarations

int beginPacketMAC(IPAddress ip, uint8_t* mac, uint16_t port);
int endPacketMAC();

between the other function declarations in EthernetUdp.h
the other 2 go in the utility\socket.cpp

int startUDPMAC(SOCKET s, uint8_t* addr, uint8_t* mac, uint16_t port) { //the code}

&

int sendUDPMAC(SOCKET s) {// the code }

also in between the other functions and within the socket.h you need to add the declarations

and here i have doubts, but i suspect declared as extern just like the other functions in socket.cpp

extern int startUDPMAC(SOCKET s, uint8_t* addr, uint8_t* mac, uint16_t port);
extern int sendUDPMAC(SOCKET s);

and that should do it.

Deva_Rishi:
OK, well best practice is to copy the whole ethernet folder and put it somewhere out of the compilers reach
like this you keep the original copies.

Also, if you are modifying a library I suggest you give the modified version a new name so that there is no confusion between the original and the modified version. The two versions should be capable of co-existing.

Beware, also, if you modify one of the standard libraries your changes may get over-written if you update the Arduino IDE

...R

Robin2:
Also, if you are modifying a library I suggest you give the modified version a new name so that there is no confusion between the original and the modified version. The two versions should be capable of co-existing.

I do agree with you on that matter, though in this case there are multiple files that make up the library and some are cross-referenced, there is a fair chance of not getting it exactly correct (and you do have to) therefore i didn't suggest this option.