Commit 0229e949 authored by Jozef Kralik's avatar Jozef Kralik

enable feature cloud

parent a4e610c3
......@@ -15,7 +15,6 @@ idf.py -p (PORT) flash monitor
```
## windows
- [install] (https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/windows-setup.html)
- Please select master repository
- Install CMake
......
......@@ -38,7 +38,6 @@ void print_macro_info();
* */
void print_message_info(oc_message_t *message);
/**
* @brief print the data detail information
*
......@@ -52,8 +51,7 @@ void print_message_info(oc_message_t *message);
* @return noreturn
*
*/
void print_debug(const char* data, const unsigned int len, const char* note, int mode);
void print_debug(const char *data, const unsigned int len, const char *note, int mode);
/**
* @brief print the fatal error information and cycle it
......@@ -62,21 +60,23 @@ void print_debug(const char* data, const unsigned int len, const char* note, int
*
* @return noreturn
* */
#define print_error(fmt, args...) \
do { \
printf("[error]:");\
printf(fmt, ##args); \
printf(",heap size:%d%s", esp_get_free_heap_size(), "\r\n"); \
vTaskDelay(2000 / portTICK_RATE_MS); \
} while(1)
#define print_error(fmt, args...) \
do \
{ \
printf("[error]:"); \
printf(fmt, ##args); \
printf(",heap size:%d%s", esp_get_free_heap_size(), "\r\n"); \
vTaskDelay(2000 / portTICK_RATE_MS); \
} while (1)
#if APP_DEBUG
#define APP_LOG(level, ...) \
do { \
APP_PRINT("%s: %s <%s:%d>: ", level, __FILE__, __FUNCTION__, __LINE__); \
APP_PRINT(__VA_ARGS__); \
printf("\n"); \
#define APP_LOG(level, ...) \
do \
{ \
APP_PRINT("%s: %s <%s:%d>: ", level, __FILE__, __FUNCTION__, __LINE__); \
APP_PRINT(__VA_ARGS__); \
printf("\n"); \
} while (0)
#define APP_DBG(...) APP_LOG("DEBUG", __VA_ARGS__)
#define APP_WRN(...) APP_LOG("WARNING", __VA_ARGS__)
......@@ -89,6 +89,6 @@ void print_debug(const char* data, const unsigned int len, const char* note, int
#define APP_WRN(...)
#define APP_ERR(...)
#endif // endif APP_DEBUG
#endif // endif APP_DEBUG
#endif // endif _DEBUG_PRINT_H_
#endif // endif _DEBUG_PRINT_H_
......@@ -7,10 +7,11 @@
#include "sdkconfig.h"
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
typedef uint64_t oc_clock_time_t;
typedef uint64_t oc_clock_time_t;
#define OC_CLOCK_CONF_TICKS_PER_SECOND CLOCKS_PER_SEC
//#define OC_SPEC_VER_OIC
/* Security Layer */
......@@ -21,7 +22,7 @@ typedef uint64_t oc_clock_time_t;
#define SELECT_TIMEOUT_SEC (1)
/* Add support for passing network up/down events to the app */
//#define OC_NETWORK_MONITOR
#define OC_NETWORK_MONITOR
/* Add support for passing TCP/TLS/DTLS session connection events to the app */
#define OC_SESSION_EVENTS
......
/*
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#ifndef OC_ENDPOINT_EXT_H
#define OC_ENDPOINT_EXT_H
#include "oc_helpers.h"
#include "port/oc_connectivity.h"
void oc_init_endpoint_list(void);
int oc_add_endpoint_to_list(oc_endpoint_t *endpoint);
oc_endpoint_t *oc_get_endpoint_list(void);
#if 0
void oc_free_endpoint_list(void);
oc_endpoint_t *oc_new_endpoint(void);
void oc_free_endpoint(oc_endpoint_t *endpoint);
int oc_endpoint_to_string(oc_endpoint_t *endpoint, oc_string_t *endpoint_str);
int oc_string_to_endpoint(oc_string_t *endpoint_str, oc_endpoint_t *endpoint,
oc_string_t *uri);
int oc_ipv6_endpoint_is_link_local(oc_endpoint_t *endpoint);
int oc_endpoint_compare(oc_endpoint_t *ep1, oc_endpoint_t *ep2);
int oc_endpoint_compare_address(oc_endpoint_t *ep1, oc_endpoint_t *ep2);
#endif
#endif /* OC_ENDPOINT_EXT_H */
......@@ -17,14 +17,12 @@
#include "port/oc_assert.h"
#include <stdlib.h>
#include <assert.h>
void
abort_impl(void)
void abort_impl(void)
{
assert(NULL);
}
void
exit_impl(int status)
void exit_impl(int status)
{
(void)status;
assert(NULL);
......
......@@ -20,8 +20,7 @@
#include <time.h>
#include <unistd.h>
void
oc_clock_init(void)
void oc_clock_init(void)
{
}
......@@ -30,10 +29,13 @@ oc_clock_time(void)
{
oc_clock_time_t time = 0;
struct timespec t;
if (clock_gettime(CLOCK_REALTIME, &t) != -1) {
if (clock_gettime(CLOCK_REALTIME, &t) != -1)
{
time = (oc_clock_time_t)t.tv_sec * OC_CLOCK_SECOND +
(oc_clock_time_t)ceil(t.tv_nsec / (1.e09 / OC_CLOCK_SECOND));
}
OC_DBG("oc_clock_time (ts.tv_sec = %ld, ts.tv_nsec = %ld) %llu", t.tv_sec, t.tv_nsec, time);
return time;
}
......@@ -41,14 +43,15 @@ unsigned long
oc_clock_seconds(void)
{
struct timespec t;
if (clock_gettime(CLOCK_REALTIME, &t) != -1) {
if (clock_gettime(CLOCK_REALTIME, &t) != -1)
{
OC_DBG("oc_clock_seconds (ts.tv_sec = %ld, ts.tv_nsec = %ld)", t.tv_sec, t.tv_nsec);
return t.tv_sec;
}
return 0;
}
void
oc_clock_wait(oc_clock_time_t t)
void oc_clock_wait(oc_clock_time_t t)
{
usleep(t * 1.e03);
}
......@@ -80,20 +80,23 @@ void print_message_info(oc_message_t *message)
printf("ipv4 message info:\n------------------\n");
printf("message length:%d ref_count:%d\n", message->length, message->ref_count);
printf("endpoint flags:%d port:%d\naddr:", message->endpoint.flags, message->endpoint.addr.ipv4.port);
for (int i = 0; i < 4; ++i) {
for (int i = 0; i < 4; ++i)
{
printf("%d ", message->endpoint.addr.ipv4.address[i]);
}
#else
printf("ipv6 message info:\n------------------\n");
printf("message length:%d ref_count:%d\n", message->length, message->ref_count);
printf("endpoint flags:%d port:%d scope:%d\naddr:", message->endpoint.flags, message->endpoint.addr.ipv6.port, message->endpoint.addr.ipv6.scope);
for (int i = 0; i < 16; ++i) {
for (int i = 0; i < 16; ++i)
{
printf("%d ", message->endpoint.addr.ipv6.address[i]);
}
#endif
printf("\nmessage content:\n");
for (int i = 0; i < message->length; ++i) {
for (int i = 0; i < message->length; ++i)
{
printf("%x ", message->data[i]);
}
......@@ -114,29 +117,37 @@ void print_message_info(oc_message_t *message)
* @return noreturn
*
*/
void print_debug(const char* data, const unsigned int len, const char* note, int mode)
void print_debug(const char *data, const unsigned int len, const char *note, int mode)
{
#define BINARY_SHOW 0x10
#define BYTES_SHOW 0x01
#define BYTES_SHOW 0x01
printf("\n********** %s [len:%u] start addr:%p **********\n", note, len, data);
int i = 0;
for (i = 0; i < len; ++i) {
if (BINARY_SHOW & mode) {
printf("%02x ",data[i]);
} else {
if(data[i] < 32 || data[i] > 126) { // control || invisible charset
if(i > 0 && (data[i-1] >= 33 && data[i-1] <= 126) )
printf(" ");
printf("%02x ",data[i]);
} else {
for (i = 0; i < len; ++i)
{
if (BINARY_SHOW & mode)
{
printf("%02x ", data[i]);
}
else
{
if (data[i] < 32 || data[i] > 126)
{ // control || invisible charset
if (i > 0 && (data[i - 1] >= 33 && data[i - 1] <= 126))
printf(" ");
printf("%02x ", data[i]);
}
else
{
printf("%c", data[i]);
}
}
}
if ((BYTES_SHOW & mode) && ((i + 1) % 32 == 0)) {
printf(" | %d Bytes\n",i + 1);
if ((BYTES_SHOW & mode) && ((i + 1) % 32 == 0))
{
printf(" | %d Bytes\n", i + 1);
}
} // end for
} // end for
printf("\n---------- %s End ----------\n", note);
}
......@@ -134,26 +134,10 @@ add_ip_interface(int target_index)
static bool
check_new_ip_interfaces(void)
{
struct ifaddrs *ifs = NULL, *interface = NULL;
if (getifaddrs(&ifs) < 0)
for (esp_netif_t *esp_netif = esp_netif_next(NULL); esp_netif; esp_netif = esp_netif_next(esp_netif))
{
OC_ERR("querying interface address");
return false;
}
for (interface = ifs; interface != NULL; interface = interface->ifa_next)
{
/* Ignore interfaces that are down and the loopback interface */
if (!(interface->ifa_flags & IFF_UP) ||
(interface->ifa_flags & IFF_LOOPBACK))
{
continue;
}
/* Obtain interface index for this address */
int if_index = if_nametoindex(interface->ifa_name);
add_ip_interface(if_index);
add_ip_interface(esp_netif_get_netif_impl_index(esp_netif));
}
freeifaddrs(ifs);
return true;
}
......@@ -270,22 +254,14 @@ get_ip_context_for_device(size_t device)
#ifdef OC_IPV4
static int
add_mcast_sock_to_ipv4_mcast_group(int mcast_sock, const struct in_addr *local,
add_mcast_sock_to_ipv4_mcast_group(int mcast_sock, const esp_ip4_addr_t *local,
int interface_index)
{
struct ip_mreq imreq = {0};
int err = 0;
// Configure source interface
memset(&imreq, 0, sizeof(struct ip_mreq));
tcpip_adapter_ip_info_t ip_info = {0};
err = tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info);
if (err != ESP_OK)
{
OC_ERR("get ip4 ret:%d\n", err);
return -1;
}
inet_addr_from_ip4addr(&imreq.imr_interface, &ip_info.ip);
inet_addr_from_ip4addr(&imreq.imr_interface, local);
imreq.imr_multiaddr.s_addr = htonl(ALL_COAP_NODES_V4);
OC_DBG("Configured IPV4 Multicast address %s", inet_ntoa(imreq.imr_multiaddr.s_addr));
......@@ -385,9 +361,13 @@ static int
configure_mcast_socket(int mcast_sock, int sa_family)
{
int ret = 0;
for (esp_netif_t *iface = esp_netif_next(NULL); iface; iface = esp_netif_next(iface))
for (esp_netif_t *esp_netif = esp_netif_next(NULL); esp_netif; esp_netif = esp_netif_next(esp_netif))
{
int netif_index = esp_netif_get_netif_impl_index(iface);
if (!esp_netif_is_netif_up(esp_netif))
{
continue;
}
int netif_index = esp_netif_get_netif_impl_index(esp_netif);
/* Accordingly handle IPv6/IPv4 addresses */
if (sa_family == AF_INET6)
{
......@@ -396,7 +376,17 @@ configure_mcast_socket(int mcast_sock, int sa_family)
#ifdef OC_IPV4
else if (sa_family == AF_INET)
{
ret += add_mcast_sock_to_ipv4_mcast_group(mcast_sock, NULL,
esp_netif_ip_info_t ip_info;
if (esp_netif_get_ip_info(esp_netif, &ip_info) != ESP_OK)
{
OC_ERR("esp_netif_get_ip_info at interface_index(%d) returns error", netif_index);
continue;
}
if (ip4_addr_isloopback(&ip_info.ip))
{
continue;
}
ret += add_mcast_sock_to_ipv4_mcast_group(mcast_sock, &ip_info.ip,
netif_index);
}
#endif /* OC_IPV4 */
......@@ -447,10 +437,10 @@ static void
get_interface_addresses(ip_context_t *dev, unsigned char family, uint16_t port,
bool secure, bool tcp)
{
for (esp_netif_t *iface = esp_netif_next(NULL); iface; iface = esp_netif_next(iface))
for (esp_netif_t *esp_netif = esp_netif_next(NULL); esp_netif; esp_netif = esp_netif_next(esp_netif))
{
oc_endpoint_t ep = { 0 };
ep.interface_index = esp_netif_get_netif_impl_index(iface);
oc_endpoint_t ep = {0};
ep.interface_index = esp_netif_get_netif_impl_index(esp_netif);
if (secure)
{
......@@ -479,10 +469,17 @@ get_interface_addresses(ip_context_t *dev, unsigned char family, uint16_t port,
if (family == AF_INET)
{
esp_netif_ip_info_t ip_info;
if (esp_netif_get_ip_info(iface, &ip_info) != ESP_OK) {
if (esp_netif_get_ip_info(esp_netif, &ip_info) != ESP_OK)
{
OC_ERR("esp_netif_get_ip_info at interface_index(%d) returns error", ep.interface_index);
return;
}
if (ip4_addr_isany(&ip_info.ip) || ip4_addr_isloopback(&ip_info.ip))
{
return;
}
memcpy(ep.addr.ipv4.address, &ip_info.ip, 4);
ep.flags |= IPV4;
oc_endpoint_t *new_ep = oc_memb_alloc(&device_eps);
if (!new_ep)
......@@ -491,7 +488,7 @@ get_interface_addresses(ip_context_t *dev, unsigned char family, uint16_t port,
}
memcpy(new_ep, &ep, sizeof(oc_endpoint_t));
#ifdef OC_DEBUG
PRINT("add ep: %d %d %d %d ", (int) family, (int) port, (int) secure, (int) tcp);
PRINT("add ep: %d %d %d %d ", (int)family, (int)port, (int)secure, (int)tcp);
PRINTipaddr(ep);
PRINT(" index %d", ep.interface_index);
PRINT("\n\n");
......@@ -504,8 +501,13 @@ get_interface_addresses(ip_context_t *dev, unsigned char family, uint16_t port,
{
ep.flags |= IPV6;
esp_ip6_addr_t if_ip6[LWIP_IPV6_NUM_ADDRESSES];
int num = esp_netif_get_all_ip6(iface, if_ip6);
for (int i=0; i<num; ++i) {
int num = esp_netif_get_all_ip6(esp_netif, if_ip6);
for (int i = 0; i < num; ++i)
{
if (ip6_addr_isany(&if_ip6[i]) || ip6_addr_isloopback(&if_ip6[i]))
{
continue;
}
memcpy(ep.addr.ipv6.address, &if_ip6[i].addr, 16);
ep.addr.ipv6.scope = if_ip6[i].zone;
oc_endpoint_t *new_ep = oc_memb_alloc(&device_eps);
......@@ -515,10 +517,10 @@ get_interface_addresses(ip_context_t *dev, unsigned char family, uint16_t port,
}
memcpy(new_ep, &ep, sizeof(oc_endpoint_t));
#ifdef OC_DEBUG
PRINT("add ep: %d %d %d %d ", (int) family, (int) port, (int) secure, (int) tcp);
PRINTipaddr(ep);
PRINT(" index %d", ep.interface_index);
PRINT("\n\n");
PRINT("add ep: %d %d %d %d ", (int)family, (int)port, (int)secure, (int)tcp);
PRINTipaddr(ep);
PRINT(" index %d", ep.interface_index);
PRINT("\n\n");
#endif /* OC_DEBUG */
oc_list_add(dev->eps, new_ep);
}
......@@ -571,7 +573,7 @@ refresh_endpoints_list(ip_context_t *dev)
oc_endpoint_t *
oc_connectivity_get_endpoints(size_t device)
{
OC_DBG("oc_connectivity_get_endpoints %d", (int) device);
OC_DBG("oc_connectivity_get_endpoints %d", (int)device);
ip_context_t *dev = get_ip_context_for_device(device);
if (!dev)
......@@ -595,101 +597,64 @@ oc_connectivity_get_endpoints(size_t device)
* all logical devices.
*/
static int
process_interface_change_event(void)
process_interface_change_event(int ifa_index, int ifa_family, const void *ifa_ip, oc_interface_event_t event)
{
//TODO
#if 0
int ret = 0, i, num_devices = oc_core_get_num_devices();
struct nlmsghdr *response = NULL;
int guess = 512, response_len;
do {
guess <<= 1;
uint8_t dummy[guess];
response_len = recv(ifchange_sock, dummy, guess, MSG_PEEK);
if (response_len < 0) {
OC_ERR("reading payload size from netlink interface");
return -1;
}
} while (response_len == guess);
uint8_t buffer[response_len];
response_len = recv(ifchange_sock, buffer, response_len, 0);
if (response_len < 0) {
OC_ERR("reading payload from netlink interface");
return -1;
}
response = (struct nlmsghdr *)buffer;
if (response->nlmsg_type == NLMSG_ERROR) {
OC_ERR("caught NLMSG_ERROR in payload from netlink interface");
return -1;
}
bool if_state_changed = false;
while (NLMSG_OK(response, response_len)) {
if (response->nlmsg_type == RTM_NEWADDR) {
struct ifaddrmsg *ifa = (struct ifaddrmsg *)NLMSG_DATA(response);
if (ifa) {
int num_devices = oc_core_get_num_devices();
int ret = 0;
if (event == NETWORK_INTERFACE_UP)
{
#ifdef OC_NETWORK_MONITOR
if (add_ip_interface(ifa->ifa_index)) {
oc_network_interface_event(NETWORK_INTERFACE_UP);
}
if (add_ip_interface(ifa_index))
{
oc_network_interface_event(event);
}
#endif /* OC_NETWORK_MONITOR */
struct rtattr *attr = (struct rtattr *)IFA_RTA(ifa);
int att_len = IFA_PAYLOAD(response);
while (RTA_OK(attr, att_len)) {
if (attr->rta_type == IFA_ADDRESS) {
#ifdef OC_IPV4
if (ifa->ifa_family == AF_INET) {
for (i = 0; i < num_devices; i++) {
ip_context_t *dev = get_ip_context_for_device(i);
ret += add_mcast_sock_to_ipv4_mcast_group(
dev->mcast4_sock, RTA_DATA(attr), ifa->ifa_index);
}
} else
if (ifa_family == AF_INET)
{
const esp_ip4_addr_t *ip = (const esp_ip4_addr_t *)ifa_ip;
for (int i = 0; i < num_devices; i++)
{
ip_context_t *dev = get_ip_context_for_device(i);
ret += add_mcast_sock_to_ipv4_mcast_group(
dev->mcast4_sock, ip, ifa_index);
}
}
else
#endif /* OC_IPV4 */
if (ifa->ifa_family == AF_INET6 &&
ifa->ifa_scope == RT_SCOPE_LINK) {
for (i = 0; i < num_devices; i++) {
ip_context_t *dev = get_ip_context_for_device(i);
ret += add_mcast_sock_to_ipv6_mcast_group(dev->mcast_sock,
ifa->ifa_index);
}
}
}
attr = RTA_NEXT(attr, att_len);
if (ifa_family == AF_INET6)
{
const esp_ip6_addr_t *ip = (const esp_ip6_addr_t *)ifa_ip;
if (ip6_addr_islinklocal(ip))
{
for (int i = 0; i < num_devices; i++)
{
ip_context_t *dev = get_ip_context_for_device(i);
ret += add_mcast_sock_to_ipv6_mcast_group(dev->mcast_sock,
ifa_index);
}
}
if_state_changed = true;
} else if (response->nlmsg_type == RTM_DELADDR) {
struct ifaddrmsg *ifa = (struct ifaddrmsg *)NLMSG_DATA(response);
if (ifa) {
}
}
else if (event == NETWORK_INTERFACE_DOWN)
{
#ifdef OC_NETWORK_MONITOR
if (remove_ip_interface(ifa->ifa_index)) {
oc_network_interface_event(NETWORK_INTERFACE_DOWN);
}
#endif /* OC_NETWORK_MONITOR */
}
if_state_changed = true;
if (remove_ip_interface(ifa_index))
{
oc_network_interface_event(event);
}
response = NLMSG_NEXT(response, response_len);
#endif /* OC_NETWORK_MONITOR */
}
if (if_state_changed) {
for (i = 0; i < num_devices; i++) {
ip_context_t *dev = get_ip_context_for_device(i);
oc_network_event_handler_mutex_lock();
refresh_endpoints_list(dev);
oc_network_event_handler_mutex_unlock();
}
for (int i = 0; i < num_devices; i++)
{
ip_context_t *dev = get_ip_context_for_device(i);
oc_network_event_handler_mutex_lock();
refresh_endpoints_list(dev);
oc_network_event_handler_mutex_unlock();
}
return ret;
#endif
OC_ERR("process_interface_change_event: not implmemented");
return -1;
}
static int
......@@ -952,18 +917,6 @@ network_event_thread(void *data)
for (i = 0; i < n; i++)
{
#if 0
if (dev->device == 0) {
if (FD_ISSET(ifchange_sock, &setfds)) {
if (process_interface_change_event() < 0) {
OC_WRN("caught errors while handling a network interface change");
}
FD_CLR(ifchange_sock, &setfds);
continue;
}
}
#endif
oc_message_t *message = oc_allocate_message();
if (!message)
......@@ -1179,59 +1132,54 @@ int oc_send_buffer(oc_message_t *message)
#ifdef OC_CLIENT
void oc_send_discovery_request(oc_message_t *message)
{
struct ifaddrs *ifs = NULL, *interface = NULL;
if (getifaddrs(&ifs) < 0)
{
OC_ERR("querying interfaces: %d", errno);
goto done;
}
memset(&message->endpoint.addr_local, 0,
sizeof(message->endpoint.addr_local));
message->endpoint.interface_index = 0;
ip_context_t *dev = get_ip_context_for_device(message->endpoint.device);
#define IN6_IS_ADDR_MC_REALM_LOCAL(addr) \
IN6_IS_ADDR_MULTICAST(addr) && ((((const uint8_t *)(addr))[1] & 0x0f) == 0x03)
#define IN6_IS_ADDR_MC_REALM_LOCAL(ip6) \
ip6_addr_ismulticast(ip6) && ((((const uint8_t *)(ip6->addr))[1] & 0x0f) == 0x03)
for (interface = ifs; interface != NULL; interface = interface->ifa_next)
for (esp_netif_t *esp_netif = esp_netif_next(NULL); esp_netif; esp_netif = esp_netif_next(esp_netif))
{
if (!(interface->ifa_flags & IFF_UP) ||
(interface->ifa_flags & IFF_LOOPBACK))
if (!esp_netif_is_netif_up(esp_netif))
continue;
if ((message->endpoint.flags & IPV6) && interface->ifa_addr &&
interface->ifa_addr->sa_family == AF_INET6)
unsigned int mif = esp_netif_get_netif_impl_index(esp_netif);
if (message->endpoint.flags & IPV6)
{
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)interface->ifa_addr;