iot/zoap: Add port information to network addresses

uIP keeps the port separated from the IP addresses, so if the
application wants to communicate with a remote endpoint we must also
have the port information available.

Change-Id: I8e2b01fe5717166e1f9cebcc74b2056325b8ccc3
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
This commit is contained in:
Vinicius Costa Gomes 2016-09-14 11:10:29 -03:00 committed by Anas Nashif
commit 441d22a371
4 changed files with 37 additions and 17 deletions

View file

@ -530,8 +530,8 @@ static zoap_method_t method_from_code(const struct zoap_resource *resource,
}
int zoap_handle_request(struct zoap_packet *pkt,
struct zoap_resource *resources,
const void *from)
struct zoap_resource *resources,
const uip_ipaddr_t *addr, uint16_t port)
{
struct zoap_resource *resource;
@ -551,7 +551,7 @@ int zoap_handle_request(struct zoap_packet *pkt,
return 0;
}
return method(resource, pkt, from);
return method(resource, pkt, addr, port);
}
return -ENOENT;
@ -587,7 +587,8 @@ static int get_observe_option(const struct zoap_packet *pkt)
}
struct zoap_reply *zoap_response_received(
const struct zoap_packet *response, const void *from,
const struct zoap_packet *response,
const uip_ipaddr_t *addr, uint16_t port,
struct zoap_reply *replies, size_t len)
{
struct zoap_reply *r;
@ -621,7 +622,7 @@ struct zoap_reply *zoap_response_received(
r->age = age;
}
r->reply(response, r, from);
r->reply(response, r, addr, port);
return r;
}

View file

@ -151,7 +151,8 @@ struct zoap_resource;
*/
typedef int (*zoap_method_t)(struct zoap_resource *resource,
struct zoap_packet *request,
const void *from);
const uip_ipaddr_t *addr,
uint16_t port);
/**
* Type of the callback being called when a resource's has observers to be
@ -199,7 +200,9 @@ struct zoap_packet {
* a pending request.
*/
typedef int (*zoap_reply_t)(const struct zoap_packet *response,
struct zoap_reply *reply, const void *from);
struct zoap_reply *reply,
const uip_ipaddr_t *addr,
uint16_t port);
/**
* Represents a request awaiting for an acknowledgment (ACK).
@ -313,7 +316,8 @@ struct zoap_pending *zoap_pending_received(
* that response.
*/
struct zoap_reply *zoap_response_received(
const struct zoap_packet *response, const void *from,
const struct zoap_packet *response,
const uip_ipaddr_t *addr, uint16_t port,
struct zoap_reply *replies, size_t len);
/**
@ -345,8 +349,8 @@ void zoap_reply_clear(struct zoap_reply *reply);
* matching resources.
*/
int zoap_handle_request(struct zoap_packet *pkt,
struct zoap_resource *resources,
const void *from);
struct zoap_resource *resources,
const uip_ipaddr_t *addr, uint16_t port);
/**
* Indicates that this resource was updated and that the @a notify callback

View file

@ -17,6 +17,7 @@
#include <errno.h>
#include <stdio.h>
#include <misc/byteorder.h>
#include <misc/nano_work.h>
#include <net/net_core.h>
#include <net/net_socket.h>
@ -58,7 +59,9 @@ static void msg_dump(const char *s, uint8_t *data, unsigned len)
}
static int resource_reply_cb(const struct zoap_packet *response,
struct zoap_reply *reply, const void *from)
struct zoap_reply *reply,
const uip_ipaddr_t *addr,
uint16_t port)
{
struct net_buf *buf = response->buf;
@ -76,6 +79,8 @@ static void udp_receive(void)
int r;
while (true) {
struct uip_conn *conn;
buf = net_receive(receive_context, TICKS_UNLIMITED);
if (!buf) {
continue;
@ -87,13 +92,17 @@ static void udp_receive(void)
continue;
}
conn = uip_conn(buf);
pending = zoap_pending_received(&response, pendings,
NUM_PENDINGS);
if (pending) {
/* If necessary cancel retransmissions */
}
reply = zoap_response_received(&response, buf,
reply = zoap_response_received(&response,
&conn->ripaddr,
sys_be16_to_cpu(conn->rport),
replies, NUM_REPLIES);
if (!reply) {
printf("No handler for response (%d)\n", r);

View file

@ -19,6 +19,7 @@
#include <zephyr.h>
#include <misc/byteorder.h>
#include <net/net_core.h>
#include <net/net_socket.h>
#include <net/ip_buf.h>
@ -42,10 +43,10 @@ char fiberStack[STACKSIZE];
static struct net_context *context;
static int test_get(struct zoap_resource *resource,
struct zoap_packet *request,
const void *from)
struct zoap_packet *request,
const uip_ipaddr_t *addr,
uint16_t port)
{
struct net_context *ctx = (struct net_context *) from;
struct net_buf *buf;
struct zoap_packet response;
uint8_t *payload, code, type;
@ -93,7 +94,7 @@ static int test_get(struct zoap_resource *resource,
return -EINVAL;
}
return net_reply(ctx, buf);
return net_reply(context, buf);
}
static const char * const test_path[] = { "test", NULL };
@ -111,6 +112,8 @@ static void udp_receive(void)
int r;
while (true) {
struct uip_conn *conn;
buf = net_receive(context, TICKS_UNLIMITED);
if (!buf) {
continue;
@ -122,7 +125,10 @@ static void udp_receive(void)
continue;
}
r = zoap_handle_request(&request, resources, context);
conn = uip_conn(buf);
r = zoap_handle_request(&request, resources, &conn->ripaddr,
sys_be16_to_cpu(conn->rport));
if (r < 0) {
printf("No handler for such request (%d)\n", r);
continue;