-
Notifications
You must be signed in to change notification settings - Fork 8.2k
HTTP client API #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
HTTP client API #50
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # | ||
| # Copyright (c) 2017 Intel Corporation | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
|
|
||
| # Common routines used in net samples | ||
|
|
||
| ifeq ($(CONFIG_NET_L2_BLUETOOTH), y) | ||
| CFLAGS +=-I${ZEPHYR_BASE}/samples/bluetooth/ | ||
| obj-y += ../bluetooth/gatt/ipss.o | ||
| endif | ||
|
|
||
| ifeq ($(CONFIG_NET_L2_IEEE802154), y) | ||
| ifeq ($(CONFIG_NET_APP_SETTINGS), y) | ||
| obj-y += ../../common/ieee802154_settings.o | ||
| endif | ||
| endif | ||
|
|
||
| ccflags-y += -I${ZEPHYR_BASE}/samples/net/common/ | ||
| obj-y += ../../common/sample_app_setup.o |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /** @file | ||
| * @brief Common routines needed in various network sample applications. | ||
| */ | ||
|
|
||
| /* | ||
| * Copyright (c) 2017 Intel Corporation | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef __NET_SAMPLE_APP_H | ||
| #define __NET_SAMPLE_APP_H | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /* What kind of functionality is needed by the application. */ | ||
| #define NET_SAMPLE_NEED_ROUTER 0x00000001 | ||
|
|
||
| int net_sample_app_init(const char *app_info, u32_t flags, s32_t timeout); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* __NET_SAMPLE_APP_H */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| /* sample_app_setup.c */ | ||
|
|
||
| /* | ||
| * Copyright (c) 2017 Intel Corporation. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #if 1 | ||
| #define SYS_LOG_DOMAIN "sample/net" | ||
| #define NET_SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG | ||
| #define NET_LOG_ENABLED 1 | ||
| #endif | ||
|
|
||
| #include <zephyr.h> | ||
|
|
||
| #include <net/net_core.h> | ||
| #include <net/net_ip.h> | ||
| #include <net/net_if.h> | ||
| #include <net/dhcpv4.h> | ||
| #include <net/net_mgmt.h> | ||
|
|
||
| #include "net_sample_app.h" | ||
|
|
||
| #if defined(CONFIG_NET_L2_BLUETOOTH) | ||
| #include <bluetooth/bluetooth.h> | ||
| #include <gatt/ipss.h> | ||
| #endif | ||
|
|
||
| #if defined(CONFIG_NET_L2_IEEE802154) | ||
| #include <ieee802154_settings.h> | ||
| #endif | ||
|
|
||
| struct k_sem waiter = K_SEM_INITIALIZER(waiter, 0, 1); | ||
|
|
||
| #if defined(CONFIG_NET_DHCPV4) | ||
| static struct net_mgmt_event_callback mgmt4_cb; | ||
|
|
||
| static void ipv4_addr_add_handler(struct net_mgmt_event_callback *cb, | ||
| u32_t mgmt_event, | ||
| struct net_if *iface) | ||
| { | ||
| char hr_addr[NET_IPV4_ADDR_LEN]; | ||
| int i; | ||
|
|
||
| if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) { | ||
| return; | ||
| } | ||
|
|
||
| for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) { | ||
| struct net_if_addr *if_addr = &iface->ipv4.unicast[i]; | ||
|
|
||
| if (if_addr->addr_type != NET_ADDR_DHCP || !if_addr->is_used) { | ||
| continue; | ||
| } | ||
|
|
||
| NET_INFO("IPv4 address: %s", | ||
| net_addr_ntop(AF_INET, &if_addr->address.in_addr, | ||
| hr_addr, NET_IPV4_ADDR_LEN)); | ||
| NET_INFO("Lease time: %u seconds", iface->dhcpv4.lease_time); | ||
| NET_INFO("Subnet: %s", | ||
| net_addr_ntop(AF_INET, &iface->ipv4.netmask, | ||
| hr_addr, NET_IPV4_ADDR_LEN)); | ||
| NET_INFO("Router: %s", | ||
| net_addr_ntop(AF_INET, &iface->ipv4.gw, | ||
| hr_addr, NET_IPV4_ADDR_LEN)); | ||
| break; | ||
| } | ||
|
|
||
| k_sem_give(&waiter); | ||
| } | ||
|
|
||
| static void setup_dhcpv4(struct net_if *iface) | ||
| { | ||
| NET_INFO("Running dhcpv4 client..."); | ||
|
|
||
| net_mgmt_init_event_callback(&mgmt4_cb, ipv4_addr_add_handler, | ||
| NET_EVENT_IPV4_ADDR_ADD); | ||
| net_mgmt_add_event_callback(&mgmt4_cb); | ||
|
|
||
| net_dhcpv4_start(iface); | ||
| } | ||
|
|
||
| #else | ||
| #define setup_dhcpv4(...) | ||
| #endif /* CONFIG_NET_DHCPV4 */ | ||
|
|
||
| #if defined(CONFIG_NET_IPV4) && !defined(CONFIG_NET_DHCPV4) | ||
|
|
||
| #if !defined(CONFIG_NET_APP_MY_IPV4_ADDR) | ||
| #error "You need to define an IPv4 address or enable DHCPv4!" | ||
| #endif | ||
|
|
||
| static void setup_ipv4(struct net_if *iface) | ||
| { | ||
| struct in_addr addr; | ||
| int ret; | ||
|
|
||
| if (net_addr_pton(AF_INET, CONFIG_NET_APP_MY_IPV4_ADDR, &addr)) { | ||
| NET_ERR("Invalid address: %s", CONFIG_NET_APP_MY_IPV4_ADDR); | ||
| return; | ||
| } | ||
|
|
||
| net_if_ipv4_addr_add(iface, &addr, NET_ADDR_MANUAL, 0); | ||
|
|
||
| NET_INFO("IPv4 address: %s", | ||
| net_addr_ntop(AF_INET, &addr, hr_addr, NET_IPV4_ADDR_LEN)); | ||
|
|
||
| k_sem_give(&waiter); | ||
| } | ||
|
|
||
| #else | ||
| #define setup_ipv4(...) | ||
| #endif /* CONFIG_NET_IPV4 && !CONFIG_NET_DHCPV4 */ | ||
|
|
||
| #if defined(CONFIG_NET_IPV6) | ||
| #if !defined(CONFIG_NET_APP_MY_IPV6_ADDR) | ||
| #error "You need to define an IPv6 address!" | ||
| #endif | ||
|
|
||
| static struct net_mgmt_event_callback mgmt6_cb; | ||
| static struct in6_addr laddr; | ||
|
|
||
| /* DNS query will most probably fail if we do not have a default | ||
| * router configured because typically the DNS server is outside of local | ||
| * network. So wait for that before continuing. | ||
| */ | ||
| static void ipv6_event_handler(struct net_mgmt_event_callback *cb, | ||
| u32_t mgmt_event, struct net_if *iface) | ||
| { | ||
| if (mgmt_event == NET_EVENT_IPV6_DAD_SUCCEED) { | ||
| char hr_addr[NET_IPV6_ADDR_LEN]; | ||
| struct net_if_addr *ifaddr; | ||
|
|
||
| ifaddr = net_if_ipv6_addr_lookup(&laddr, &iface); | ||
| if (!ifaddr || | ||
| !(net_ipv6_addr_cmp(&ifaddr->address.in6_addr, &laddr) && | ||
| ifaddr->addr_state == NET_ADDR_PREFERRED)) { | ||
| /* Address is not yet properly setup */ | ||
| return; | ||
| } | ||
|
|
||
| NET_INFO("IPv6 address: %s", | ||
| net_addr_ntop(AF_INET6, &laddr, hr_addr, | ||
| NET_IPV6_ADDR_LEN)); | ||
|
|
||
| k_sem_give(&waiter); | ||
| } | ||
|
|
||
| if (mgmt_event == NET_EVENT_IPV6_ROUTER_ADD) { | ||
| k_sem_give(&waiter); | ||
| } | ||
| } | ||
|
|
||
| static void setup_ipv6(struct net_if *iface, u32_t flags) | ||
| { | ||
| struct net_if_addr *ifaddr; | ||
| u32_t mask = NET_EVENT_IPV6_DAD_SUCCEED; | ||
|
|
||
| if (net_addr_pton(AF_INET6, CONFIG_NET_APP_MY_IPV6_ADDR, &laddr)) { | ||
| NET_ERR("Invalid address: %s", CONFIG_NET_APP_MY_IPV6_ADDR); | ||
| return; | ||
| } | ||
|
|
||
| if (flags & NET_SAMPLE_NEED_ROUTER) { | ||
| mask |= NET_EVENT_IPV6_ROUTER_ADD; | ||
| } | ||
|
|
||
| net_mgmt_init_event_callback(&mgmt6_cb, ipv6_event_handler, mask); | ||
| net_mgmt_add_event_callback(&mgmt6_cb); | ||
|
|
||
| ifaddr = net_if_ipv6_addr_add(iface, &laddr, NET_ADDR_MANUAL, 0); | ||
| if (!ifaddr) { | ||
| NET_ERR("Cannot add %s to interface", | ||
| CONFIG_NET_APP_MY_IPV6_ADDR); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| #else | ||
| #define setup_ipv6(...) | ||
| #endif /* CONFIG_NET_IPV6 */ | ||
|
|
||
| int net_sample_app_init(const char *app_info, u32_t flags, s32_t timeout) | ||
| { | ||
| struct net_if *iface = net_if_get_default(); | ||
|
|
||
| if (app_info) { | ||
| NET_INFO("%s", app_info); | ||
| } | ||
|
|
||
| #if defined(CONFIG_NET_L2_BLUETOOTH) | ||
| if (bt_enable(NULL)) { | ||
| NET_ERR("Bluetooth init failed"); | ||
| return -EFAULT; | ||
| } else { | ||
| ipss_init(); | ||
| ipss_advertise(); | ||
| } | ||
| #endif | ||
|
|
||
| #if defined(CONFIG_NET_L2_IEEE802154) | ||
| if (ieee802154_sample_setup()) { | ||
| NET_ERR("IEEE 802.15.4 setup failed"); | ||
| return -EFAULT; | ||
| } | ||
| #endif | ||
|
|
||
| setup_ipv4(iface); | ||
|
|
||
| setup_dhcpv4(iface); | ||
|
|
||
| setup_ipv6(iface, flags); | ||
|
|
||
| /* Wait until we are ready to continue */ | ||
| if (k_sem_take(&waiter, timeout)) { | ||
| NET_ERR("Timeout while waiting setup"); | ||
| return -ETIMEDOUT; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This apparently should be removed before merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enabling debug by default for sample apps is currently done on purpose. Because there is no kconfig for the sample apps debug options, it makes sense to activate the debugging by default for them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jukkar : Ack, so do we need "#if 1" line? If you say "yes", I'm only happy, I love these, and will include them in my patches ;-).