2023-08-05 00:51:54 -03:00
|
|
|
/*
|
2023-08-09 23:09:00 -03:00
|
|
|
class for holding IPv4 address parameters
|
2023-08-05 00:51:54 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AP_Networking_Config.h"
|
|
|
|
|
|
|
|
#if AP_NETWORKING_ENABLED
|
|
|
|
|
2023-11-25 20:46:07 -04:00
|
|
|
#include <arpa/inet.h>
|
2023-08-05 00:51:54 -03:00
|
|
|
#include "AP_Networking.h"
|
2023-12-25 15:27:13 -04:00
|
|
|
#include <AP_HAL/utility/Socket.h>
|
2023-08-05 00:51:54 -03:00
|
|
|
|
|
|
|
const AP_Param::GroupInfo AP_Networking_IPV4::var_info[] = {
|
|
|
|
// @Param: 0
|
|
|
|
// @DisplayName: IPv4 Address 1st byte
|
|
|
|
// @Description: IPv4 address. Example: 192.xxx.xxx.xxx
|
|
|
|
// @Range: 0 255
|
|
|
|
// @RebootRequired: True
|
|
|
|
AP_GROUPINFO("0", 1, AP_Networking_IPV4, addr[0], 0),
|
|
|
|
|
|
|
|
// @Param: 1
|
2023-08-09 23:09:00 -03:00
|
|
|
// @DisplayName: IPv4 Address 2nd byte
|
2023-08-05 00:51:54 -03:00
|
|
|
// @Description: IPv4 address. Example: xxx.168.xxx.xxx
|
|
|
|
// @Range: 0 255
|
|
|
|
// @RebootRequired: True
|
|
|
|
AP_GROUPINFO("1", 2, AP_Networking_IPV4, addr[1], 0),
|
|
|
|
|
|
|
|
// @Param: 2
|
2023-08-09 23:09:00 -03:00
|
|
|
// @DisplayName: IPv4 Address 3rd byte
|
2023-08-05 00:51:54 -03:00
|
|
|
// @Description: IPv4 address. Example: xxx.xxx.13.xxx
|
|
|
|
// @Range: 0 255
|
|
|
|
// @RebootRequired: True
|
|
|
|
AP_GROUPINFO("2", 3, AP_Networking_IPV4, addr[2], 0),
|
|
|
|
|
|
|
|
// @Param: 3
|
2023-08-09 23:09:00 -03:00
|
|
|
// @DisplayName: IPv4 Address 4th byte
|
2023-08-05 00:51:54 -03:00
|
|
|
// @Description: IPv4 address. Example: xxx.xxx.xxx.14
|
|
|
|
// @Range: 0 255
|
|
|
|
// @RebootRequired: True
|
|
|
|
AP_GROUPINFO("3", 4, AP_Networking_IPV4, addr[3], 0),
|
|
|
|
|
|
|
|
AP_GROUPEND
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
IPV4 address parameter class
|
|
|
|
*/
|
|
|
|
AP_Networking_IPV4::AP_Networking_IPV4(const char *default_addr)
|
|
|
|
{
|
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
|
|
|
set_default_uint32(AP_Networking::convert_str_to_ip(default_addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t AP_Networking_IPV4::get_uint32(void) const
|
|
|
|
{
|
|
|
|
uint32_t v = 0;
|
|
|
|
uint8_t *b = (uint8_t*)&v;
|
2023-08-05 04:08:52 -03:00
|
|
|
for (uint8_t i=0; i<4; i++) {
|
|
|
|
b[3-i] = uint8_t(addr[i].get());
|
2023-08-05 00:51:54 -03:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Networking_IPV4::set_default_uint32(uint32_t v)
|
|
|
|
{
|
|
|
|
uint8_t *b = (uint8_t*)&v;
|
|
|
|
for (uint8_t i=0; i<ARRAY_SIZE(addr); i++) {
|
2023-08-05 04:08:52 -03:00
|
|
|
addr[3-i].set_default(b[i]);
|
2023-08-05 00:51:54 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-25 20:46:07 -04:00
|
|
|
const char* AP_Networking_IPV4::get_str()
|
2023-11-12 21:41:10 -04:00
|
|
|
{
|
2023-11-25 20:46:07 -04:00
|
|
|
const auto ip = ntohl(get_uint32());
|
2023-12-25 15:27:13 -04:00
|
|
|
return SocketAPM::inet_addr_to_str(&ip, strbuf, sizeof(strbuf));
|
2023-11-12 21:41:10 -04:00
|
|
|
}
|
|
|
|
|
2023-08-05 00:51:54 -03:00
|
|
|
#endif // AP_NETWORKING_ENABLED
|