uint16_t port, OCDevAddr *ipAddr);
-//-- OCGetInterfaceAddress ------------------------------------------------------
-/** @ingroup ocsocket
- *
- * This method is used to retrieved the IPv4/IPv6 address of the local interface.
- * If no interface name is provided, this API retrieves the IP address of
- * the default interface.
- *
- * @note currently, only IPv4(AF_INET) is supported for addrType argument.
- *
- * @param[in] ifName
- * interface whose address needs to be retrieved.
- * @param[in] ifNameLen
- * length of the interface name
- * @param[in] addrType
- * IPv4 or IPv6
- * @param[out] addrv4
- * IPv4 address in a.b.c.d format
- * @param[in] addrLen
- * size of the buffer at addrv4. Should be at least 16 bytes for an
- * IPv4 address.
- *
- * @retval 0 for Success, otherwise some error value
- */
-//-------------------------------------------------------------------------------
-int32_t OCGetInterfaceAddress(uint8_t* ifName, uint32_t ifNameLen, uint16_t addrType,
- uint8_t *addrv4, uint32_t addrLen);
-
-
//-- OCDevAddrToString ----------------------------------------------------
/** @ingroup ocsocket
*
return ERR_SUCCESS;
}
-#ifdef __ANDROID__
-/// Retrieves the IP address assigned to specified wireless interface
-int32_t OCGetInterfaceAddress(uint8_t* ifName, uint32_t ifNameLen, uint16_t addrType,
- uint8_t *addr, uint32_t addrLen)
-{
- int32_t ret = ERR_UNKNOWN;
- int32_t sfd = 0xFFFFFFFF;
- struct ifreq ifr;
-
- VERIFY_NON_NULL(addr);
- VERIFY_NON_NULL(ifName);
- if (ifNameLen > (IFNAMSIZ - 1) ) {
- return ERR_INVALID_INPUT;
- }
- if (addrType != AF_INET) {
- return ERR_INVALID_INPUT;
- }
-
- sfd = socket(addrType, SOCK_DGRAM, 0);
- if (sfd < 0) {
- OC_LOG_V(FATAL, MOD_NAME, "socket API ret val %d", sfd);
- goto exit;
- }
-
- ifr.ifr_addr.sa_family = addrType;
-
- strncpy(ifr.ifr_name, (const char*)ifName, ifNameLen);
-
- if (ioctl(sfd, SIOCGIFADDR, &ifr) != 0) {
- OC_LOG(FATAL, MOD_NAME, "ioctl call failed");
- goto exit;
- }
-
- strncpy((char *)addr,
- inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr),
- addrLen);
- ret = ERR_SUCCESS;
-
-exit:
- if (sfd >= 0) {
- close(sfd);
- }
- return ret;
-}
-
-#else
-/// Retrieves the IP address assigned to specified wireless interface
-int32_t OCGetInterfaceAddress(uint8_t* ifName, uint32_t ifNameLen, uint16_t addrType,
- uint8_t *addr, uint32_t addrLen)
-{
- struct ifaddrs *myaddrs = NULL, *ifa = NULL;
- int32_t ret = ERR_UNKNOWN;
-
- VERIFY_NON_NULL(addr);
-
- if (addrType != AF_INET) {
- return ERR_INVALID_INPUT;
- }
-
- if(getifaddrs(&myaddrs) != 0) {
- goto exit;
- }
-
- for (ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next) {
- if (ifa->ifa_addr == NULL)
- continue;
- if (!(ifa->ifa_flags & IFF_UP))
- continue;
- if (!(ifa->ifa_flags & IFF_RUNNING))
- continue;
-
- if (ifName && ifa->ifa_name) {
- if(strncmp((const char*)ifName, ifa->ifa_name, ifNameLen) != 0)
- continue;
- }
-
- switch (ifa->ifa_addr->sa_family)
- {
- case AF_INET:
- {
- struct sockaddr_in *s4 = (struct sockaddr_in *)ifa->ifa_addr;
- if(inet_ntop(AF_INET, &(s4->sin_addr), (char *)addr, addrLen))
- ret = ERR_SUCCESS;
- goto exit;
- }
-
- default:
- continue;
- }
- }
-
-exit:
- if (myaddrs) {
- freeifaddrs(myaddrs);
- }
- return ret;
-}
-#endif //__ANDROID__
-
/// Creates a BSD socket and binds it specified port for UDP
int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t *sockfd, OC_SOCKET_OPTION sockoption)
{
return ERR_SUCCESS;
}
-
-/// Retrieves the IP address assigned to Arduino Ethernet shield
-int32_t OCGetInterfaceAddress(uint8_t* ifName, uint32_t ifNameLen, uint16_t addrType,
- uint8_t *addr, uint32_t addrLen)
-{
- //TODO : Fix this for scenarios when this API is invoked when device is not connected
- uint8_t rawIPAddr[4];
- VERIFY_NON_NULL(addr);
- if (addrLen < IPNAMESIZE) {
- OC_LOG(FATAL, MOD_NAME, PCF("OCGetInterfaceAddress: addrLen MUST be atleast 16"));
- return ERR_INVALID_INPUT;
- }
-
- if (addrType != AF_INET) {
- return ERR_INVALID_INPUT;
- }
- W5100.getIPAddress(rawIPAddr);
- snprintf((char *)addr, addrLen, "%d.%d.%d.%d", rawIPAddr[0], rawIPAddr[1], rawIPAddr[2], rawIPAddr[3]);
-
- OC_LOG_BUFFER(INFO, MOD_NAME, addr, addrLen);
-
- return ERR_SUCCESS;
-}
-
/// Retrieves a empty socket and bind it for UDP with the input port
int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd, OC_SOCKET_OPTION sockoption)
{
return ERR_SUCCESS;
}
-
-/// Retrieves the IP address assigned to Arduino Ethernet shield
-int32_t OCGetInterfaceAddress(uint8_t* ifName, uint32_t ifNameLen, uint16_t addrType,
- uint8_t *addr, uint32_t addrLen)
-{
- WiFiClass WiFi;
-
- VERIFY_NON_NULL(addr);
- if (addrLen < IPNAMESIZE) {
- OC_LOG(FATAL, MOD_NAME, PCF("OCGetInterfaceAddress: addrLen MUST be at least 16"));
- return ERR_INVALID_INPUT;
- }
-
- if (addrType != AF_INET) {
- return ERR_INVALID_INPUT;
- }
-
- IPAddress ip = WiFi.localIP();
- snprintf((char *)addr, addrLen, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
-
- OC_LOG_BUFFER(INFO, MOD_NAME, addr, addrLen);
-
- return ERR_SUCCESS;
-}
-
/// Retrieves a empty socket and bind it for UDP with the input port
int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd, OC_SOCKET_OPTION sockoption)
{
EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToPort(NULL, NULL ));
}
-
-TEST(GetInterfaceAddress, Positive) {
- uint8_t addr[20];
- uint8_t ifname[] = "wlan0";
- EXPECT_EQ(ERR_SUCCESS, OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, sizeof(addr)));
- OC_LOG_V(INFO, MOD_NAME, "IPv4 Address: %s\n", addr);
- //On Android, it is MUST to provide interface name
- //EXPECT_EQ(0, get_ipv4addr( NULL, 0, addr, sizeof(addr)));
-}
-
-TEST(GetInterfaceAddress, Negative) {
- uint8_t addr[20];
- uint8_t ifname[] = "ethxx";
- EXPECT_EQ(ERR_INVALID_INPUT, OCGetInterfaceAddress( NULL, 0, AF_INET, addr, sizeof(addr)));
- EXPECT_EQ(ERR_UNKNOWN, OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, sizeof(addr)));
- EXPECT_EQ(ERR_INVALID_INPUT, OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, NULL, sizeof(addr)));
- EXPECT_EQ(ERR_UNKNOWN, OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, 0));
- EXPECT_EQ(ERR_INVALID_INPUT, OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET6, addr, sizeof(addr)));
-}
-
TEST(InitUDP, Positive) {
OCDevAddr ipaddr;
int32_t sockfd;
EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
OCClose(sockfd);
- OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
sscanf((const char*)addr, "%d.%d.%d.%d", (int*)&a, (int*)&b, (int*)&c, (int*)&d);
OCBuildIPv4Address(a,b,c,d, TEST_PORT_NUM, &ipaddr);
EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
uint8_t a,b,c,d;
//uint8_t tmp1[512];
-
- OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
sscanf((const char*)addr, "%d.%d.%d.%d", (int*)&a, (int*)&b, (int*)&c, (int*)&d);
//Create sending socket
#define VERIFY_SUCCESS(op, res) { if (op == res) {OC_LOG(DEBUG, MOD_NAME, PCF(#op " SUCCEEDED"));} \
else {OC_LOG(FATAL, MOD_NAME, PCF(#op "!!!! FAILED FAILED FAILED !!!!"));} }
-
-//OCGetInterfaceAddress tests
-void test10() {
- char strAddr[16] = "";
- VERIFY_SUCCESS(OCGetInterfaceAddress(NULL, 0, AF_INET, (uint8_t*)strAddr, 16), ERR_SUCCESS);
- OC_LOG(DEBUG, MOD_NAME, PCF("My IP address :"));
- OC_LOG_BUFFER(INFO, MOD_NAME, (uint8_t*)strAddr, sizeof(strAddr));
- delay(15000);
- VERIFY_SUCCESS(OCGetInterfaceAddress(NULL, 0, AF_INET, NULL, 16), ERR_INVALID_INPUT);
- VERIFY_SUCCESS(OCGetInterfaceAddress(NULL, 0, AF_INET, (uint8_t*)strAddr, 10), ERR_INVALID_INPUT);
- OC_LOG(DEBUG, MOD_NAME, PCF("test10 - Completed"));
-}
-
//OCBuildIPv4Address tests
void test20() {
OCDevAddr ipAddr;
EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToPort(NULL, NULL ));
}
-
-
-
-TEST(GetInterfaceAddress, Positive) {
- uint8_t addr[20];
- uint8_t ifname[] = "eth0";
- EXPECT_EQ(ERR_SUCCESS, OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, sizeof(addr)));
- printf("IPv4 Address: %s\n", addr);
- EXPECT_EQ(ERR_SUCCESS, OCGetInterfaceAddress( NULL, 0, AF_INET, addr, sizeof(addr)));
- printf("IPv4 Address: %s\n", addr);
-}
-
-TEST(GetInterfaceAddress, Negative) {
- uint8_t addr[20];
- uint8_t ifname[] = "ethxx";
- EXPECT_EQ(ERR_UNKNOWN, OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, sizeof(addr)));
- EXPECT_EQ(ERR_INVALID_INPUT, OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, NULL, sizeof(addr)));
- EXPECT_EQ(ERR_UNKNOWN, OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, 0));
- EXPECT_EQ(ERR_INVALID_INPUT, OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET6, addr, sizeof(addr)));
-}
-
TEST(InitUDP, Positive) {
OCDevAddr ipaddr;
int32_t sockfd;
EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
OCClose(sockfd);
- OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
sscanf((const char*)addr, "%d.%d.%d.%d", (int*)&a, (int*)&b, (int*)&c, (int*)&d);
OCBuildIPv4Address(a,b,c,d, TEST_PORT_NUM, &ipaddr);
EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
OCInitUDP(&ipaddr2, &rsfd);
//Since this is a Unit test, we will attempt to send message to ourself at a specific port
- OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
sscanf((const char*)addr, "%d.%d.%d.%d", (int*)&a, (int*)&b, (int*)&c, (int*)&d);
OCBuildIPv4Address(a,b,c,d, TEST_PORT_NUM, &ipaddr2);
EXPECT_TRUE(port == 5678);
OCClose(sockfd);
- OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
sscanf((const char*)addr, "%d.%d.%d.%d", (int*)&a, (int*)&b, (int*)&c, (int*)&d);
OCBuildIPv4Address(a,b,c,d, TEST_PORT_NUM, &ipaddr);
EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
int main(int argc, char* argv[])
{
- uint8_t addr[20] = {0};
- uint8_t* paddr = NULL;
- uint16_t port = OC_WELL_KNOWN_PORT;
- uint8_t ifname[] = "eth0";
pthread_t threadId;
pthread_t threadId_presence;
int opt;
}
OC_LOG(DEBUG, TAG, "OCServer is starting...");
- /*Get Ip address on defined interface and initialize coap on it with random port number
- * this port number will be used as a source port in all coap communications*/
- if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
- sizeof(addr)) == ERR_SUCCESS)
- {
- OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
- paddr = addr;
- }
- if (OCInit((char *) paddr, port, OC_SERVER) != OC_STACK_OK) {
+ if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK) {
OC_LOG(ERROR, TAG, "OCStack init error");
return 0;
}
int main(int argc, char* argv[])
{
- uint8_t addr[20] = {0};
- uint8_t* paddr = NULL;
- uint16_t port = OC_WELL_KNOWN_PORT;
- uint8_t ifname[] = "eth0";
-
OC_LOG(DEBUG, TAG, "OCServer is starting...");
- /*Get Ip address on defined interface and initialize coap on it with random port number
- * this port number will be used as a source port in all coap communications*/
- if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
- sizeof(addr)) == ERR_SUCCESS)
- {
- OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
- paddr = addr;
- }
-
- if (OCInit((char *) paddr, port, OC_SERVER) != OC_STACK_OK)
+ if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
{
OC_LOG(ERROR, TAG, "OCStack init error");
return 0;
int main(int argc, char* argv[])
{
- uint8_t addr[20] = {0};
- uint8_t* paddr = NULL;
- uint16_t port = 0;
- uint8_t ifname[] = "eth0";
pthread_t threadId;
int opt;
}
OC_LOG(DEBUG, TAG, "OCServer is starting...");
- /*Get Ip address on defined interface and initialize coap on it with random port number
- * this port number will be used as a source port in all coap communications*/
- if (OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
- sizeof(addr)) == ERR_SUCCESS)
- {
- OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
- paddr = addr;
- }
-
- if (OCInit((char *) paddr, port, OC_SERVER) != OC_STACK_OK) {
+ if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK) {
OC_LOG(ERROR, TAG, "OCStack init error");
return 0;
}
int main(int argc, char* argv[])
{
- uint8_t addr[20] = {0};
- uint8_t* paddr = NULL;
- uint16_t port = OC_WELL_KNOWN_PORT;
- uint8_t ifname[] = "eth0";
-
-
OC_LOG(DEBUG, TAG, "OCServer is starting...");
- /*Get Ip address on defined interface and initialize coap on it with random port number
- * this port number will be used as a source port in all coap communications*/
- if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
- sizeof(addr)) == ERR_SUCCESS)
- {
- OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
- paddr = addr;
- }
- if (OCInit((char *) paddr, port, OC_SERVER) != OC_STACK_OK)
+ if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
{
OC_LOG(ERROR, TAG, "OCStack init error");
return 0;
#define TAG PCF("ocserver")
void ocInitialize () {
- char ipAddr[16] = "";
- OCGetInterfaceAddress (NULL, 0, AF_INET, (uint8_t *)ipAddr, 16);
OC_LOG(DEBUG, TAG, PCF("IP addr is:"));
OC_LOG_BUFFER(INFO, TAG, (uint8_t*)ipAddr, sizeof(ipAddr));
delay(2000);
- OCInit (ipAddr, 8001, OC_SERVER);
+ OCInit (NULL, 0, OC_SERVER);
}
void setup() {
}
int main() {
- uint8_t addr[20];
- uint16_t port = USE_RANDOM_PORT;
- uint8_t ifname[] = "eth0";
OCDoHandle handle;
- /*Get Ip address on defined interface and initialize coap on it with random port number
- * this port number will be used as a source port in all coap communications*/
- OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
/* Initialize OCStack*/
- if (OCInit((char *) addr, port, OC_CLIENT) != OC_STACK_OK) {
+ if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK) {
OC_LOG(ERROR, TAG, "OCStack init error");
return 0;
}
}
int main() {
- uint8_t addr[20];
- uint16_t port = USE_RANDOM_PORT;
- uint8_t ifname[] = "eth0";
-
- /*Get Ip address on defined interface and initialize coap on it with random port number
- * this port number will be used as a source port in all coap communications*/
- OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
-
OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
- if (OCInit((char *) addr, port, OC_SERVER) != OC_STACK_OK) {
+ if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK) {
OC_LOG(ERROR, TAG, "OCStack init error");
return 0;
}
void InitStack(OCMode mode)
{
OC_LOG(INFO, TAG, "Entering InitStack");
- uint8_t addr[20];
- uint16_t port = USE_RANDOM_PORT;
-
- OCGetInterfaceAddress(NULL, 0, AF_INET, addr, sizeof(addr));
OC_LOG_V(INFO, TAG, "InitStack on address %s",addr);
- EXPECT_EQ(OC_STACK_OK, OCInit((char *) addr, port, mode));
+ EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, mode));
OC_LOG(INFO, TAG, "Leaving InitStack");
}