Skip to content
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

DO NOT MERGE: LwM2M holding PR for 1.10 #1255

Closed
wants to merge 37 commits into from

Conversation

mike-scott
Copy link
Contributor

Currently, the LwM2M uses net_context structures and the LwM2M sample has code to establish network communications.

This should be changed to use the net_app APIs so that the sample can remove most of the network
setup code and the library itself can benefit from other net_app features such as DTLS, etc.

@mike-scott
Copy link
Contributor Author

@rbtchc I realize this code will conflict with your PR: #1049 We'll need to work out which will be merged first and how. :/ Please take a look.

@mike-scott
Copy link
Contributor Author

@pfalcon @rsalveti Please take a look as well.

@jukkar jukkar added the net label Aug 24, 2017
Copy link
Member

@jukkar jukkar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lot of changes, and I probably missed something. Anyway, this is a good start.

* @details Context structure for the LwM2M high-level API.
*/
struct lwm2m_ctx {
/** Net app context structure */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment talks about "net app context" but the parameter is the net_context one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops.. by the end of the patch set this is a net_app_ctx.. I probably didn't adjust the comment mid-patch series. I'll fix.

@@ -57,10 +57,12 @@ static struct device *led_dev;
static u32_t led_state;

#if defined(CONFIG_NET_IPV6)
static struct net_app_ctx udp6;
static struct net_app_ctx app_udp6;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

net_app_ctx support both AF_INET and AF_INET6 at the same time, one just need to specify AF_UNSPEC when specifying local endpoint. So in this respect we could just have one net_app_ctx instead of two. Or perhaps this was removed in later patches, there was lot of changes so I might have missed it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm. At the end of the patch series, I end up with 2 lwm2m_ctx structures (each with it's own net_app_ctx). They handle IPv4 and IPv6 individually. Do we have any samples which illustrate dual use of a net_app_ctx?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I seem to have talked about server side but the lwm2m is about client. So in client side this is not possible, sorry for confusion.

{
return net_context_sendto(pkt, dst_addr, NET_SOCKADDR_MAX_SIZE,
NULL, K_NO_WAIT, NULL, NULL);
}

void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, struct net_pkt *pkt,
bool handle_separate_response,
int (*udp_request_handler)(struct zoap_packet *,
int (*udp_request_handler)(struct net_context *net_ctx,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create a typedef for the function pointer, they are much more convinient to use and the code is easier to read after that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do. I think early on I was heeding the checkpatch warning about no new typedefs :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops, missed this in the changes.

@mike-scott
Copy link
Contributor Author

Grr.. Github still hiding comments as "outdated".. annoying.

int num_zpendings)
struct zoap_pending *lwm2m_init_message_pending(struct lwm2m_ctx *client_ctx,
struct zoap_packet *zpkt,
struct sockaddr *addr)
Copy link
Collaborator

@rbtchc rbtchc Aug 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose "addr" can be removed. Peer sockaddr can be obtained from client_ctx->net_app_ctx.default_ctx->remote

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eventually the addr here will be resolved from the security objects (see page 121 of the LwM2M spec example URL: coaps://bootstrap.example.com). If the 0/0/0 resource is a bootstrap server it will then point us to another server which would need to be resolved, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rbtchc Your original comment here now applies. Going forward the rd_client will start engine with peer_addr information on a per server basis. So net_app_ctx.default_ctx->remote will be the valid destination address as far as the engine is concerned.

I will add a commit to V3 addressing this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also retract my earlier agreement. As with lwm2m_udp_sendto(), this function is used by the observer notify logic. And there shouldn't be an inherent limitation as to who can subscribe to notifications except where access controls (to be added later) allow.

@@ -2181,17 +2175,17 @@ static int handle_request(struct zoap_packet *request,
return r;
}

int lwm2m_udp_sendto(struct net_pkt *pkt, const struct sockaddr *dst_addr)
int lwm2m_udp_sendto(struct net_app_ctx *app_ctx, struct net_pkt *pkt,
const struct sockaddr *dst_addr)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this one, too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I take it back. lwm2m_udp_sendto() is used to send to any address which may have sent a message to the IoT device (not just the peer registered in the net_app_ctx). For instance, if you make a query via Copper plug-in, that IP address needs to be used in lwm2m_udp_sendto().

That being said, this brings up a security question. And there will be more patches in the future to add the access controls object. This should stop production devices from responding to anyone not allowed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. But I suppose it doesn't work when DTLS is enabled.

@rbtchc
Copy link
Collaborator

rbtchc commented Aug 25, 2017

I check the connections on the shell console by executing command

  1. select net
  2. conn
    And it prompts
 Context   	        Iface                Flags Local           	                Remote
 [ 1] 0x00405cc0	0x00405220    4DU   AF_UNSPEC	
 [ 2] 0x00405d1c	0x00405220    6DU   [2001:db8::1]:34161	[2001:db8::2]:5683
 [ 3] 0x00405d78	0x00405220    4DU   192.0.2.1:37773	        192.0.2.2:5683
 [ 4] 0x00405dd4	0x00405220    6DU   AF_UNSPEC	

I'm not sure why there are four connections existing on the list.
I suppose there should only be two of them, one for IPv4 and one for IPv6 clients.

I, too, alter the interfaces to net_app while I was working on the bootstrap interfaces.
But I don't have the connection w/ Local equals to AF_UNSPEC.
I will try to figure out why.

@mike-scott
Copy link
Contributor Author

@rbtchc Please don't test this too much atm. I have 2 net_app_ctx and I'm going to remove 1. That's the reason we have 2 extra AF_UNSPEC in the conn list.

I'll submit a new PR tomorrow during my daytime.

@nashif nashif added this to the v1.10 milestone Aug 26, 2017
@mike-scott
Copy link
Contributor Author

mike-scott commented Aug 28, 2017

V2 posted. Diff: https://hastebin.com/seyogamofe.cs

Summary of changes:

  • Only 1 lwm2m_ctx in the sample since it will use IPv6/IPv4 based on the PEER IP address.
  • Removed peer-address, port and endpoint name from the LwM2M context. They were single use information items. (A lot of code was removed as a result)
  • The sample client no longer calls lwm2m_engine_start(). That's moved into the registration client. This simplifies the sample code even more.
  • Endpoint name was simplified to CONFIG_BOARD. This change will stop multiple samples on the same HW from running against 1 Leshan, however, for testing DTLS (an upcoming change) it's very hard to guess at random endpoint names when you need to add security information.
  • Addressed comments

@mike-scott
Copy link
Contributor Author

I also have a pending DTLS support patch. I'm not sure if I should tack that on here as well. Or keep it for a separate PR.

@mike-scott
Copy link
Contributor Author

Added a few more commits for the LwM2M cleanup:

  • Remove the needless "registered" field.
  • Implement timeout checks for the registration client
  • Set a default range for the client lifetime setting in Kconfig

@mike-scott
Copy link
Contributor Author

Added the last 2 (hopefully) commits to this PR:

  • Adding DTLS support for LwM2M client (in the engine and in the sample)

NOTE: This currently is generating errors due to corruption in the decrypted IP header. This is being addressed in this PR: #1265

@mike-scott
Copy link
Contributor Author

@rbtchc This is pretty much everything I have in my local tree. Feel free to test everything but the DTLS changes.

@rbtchc
Copy link
Collaborator

rbtchc commented Aug 29, 2017

I've applied firmware patches on top of the modifications.
So far so good.

@mike-scott
Copy link
Contributor Author

Fixed checkpatch error referencing spaces instead of tabs. No functional changes.

@mike-scott
Copy link
Contributor Author

I've totally re-worked the timeout processing patch "net: lwm2m: implement client state timeout checks" and will probably remove that patch and everything that followed it from this PR. Then setup a new PR which implements the timeout handling and then another PR with the rest.

@mike-scott
Copy link
Contributor Author

Hello @jukkar and @rbtchc,

I've posted V5, however it's a fairly large diff starting with "net: lwm2m: remove registration client "registered" field". I decided that I was unhappy with the old implementation of timeout handling. So, I went and refactored a lot of code so that the internal message sending APIs all use an "lwm2m_message" structure which keeps track of things a lot more cleanly than before.

I also rebased @rbtchc firmware support patches on top. And I dropped the DTLS patches which will come back once I migrate to the new COAP APIs.

@mike-scott
Copy link
Contributor Author

I also rebased this all on top of latest master.

}

pkt = net_app_get_net_pkt(&msg->ctx->net_app_ctx, AF_UNSPEC,
BUF_ALLOC_TIMEOUT);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be aligned to &msg

zoap_header_set_token(&msg->zpkt, zoap_next_token(), 8);
}

if (msg->type == ZOAP_TYPE_CON) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about change to following to save an extra indent

if (msg->type != ZOAP_TYPE_CON) {
    return 0;
}

// Init pending

off = parsed_uri.field_data[UF_HOST].off;
len = parsed_uri.field_data[UF_HOST].len;

/* IPv6 is wrapped by brackets */
Copy link
Collaborator

@rbtchc rbtchc Sep 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although these are changes made by me for firmware pull.

Line 274~301 & 307 ~ 310 can be removed.
Instead, we can pass the host string to net_app_init_udp_client() directly.
Check the patch: https://hastebin.com/ejonuvurit.diff

rveerama1 and others added 20 commits October 2, 2017 13:47
ZOAP library has certain limitations in parsing and preparation of
coap messages. It can handle only on single network fragment. If
network packet is split between multiple fragments it fails. This
patch is just copy and rename of 'zoap' to 'coap'.

Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
Current coap library fails to parse or prepare if packet is more
than one fragment. Added support to handle multi fragment packet.
Also well-known/core api used to prepare coap packet and send it
through net context api immediately. This is goind to be problematic
if user doesn't enable net context. Also user can not encrypt coap
packets. Now api will return prepared coap packet to application.
Application will send it to peer.

Jira: ZEP-2210

Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
ZoAP tests are modified to use new CoAP API. Also modified tests
name from 'zoap' to 'coap'.

Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
Added deprecated statements to ZOAP library api, structs and enums.

Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
Instead of generating an EINVAL error when there's no payload let's
check for an offset == 0 and exit gracefully.

Signed-off-by: Michael Scott <michael.scott@linaro.org>
This patch moves from the ZoAP API in subsys/net/lib/zoap to
the CoAP API in subsys/net/lib/coap which handles multiple
fragments for sending / receiving data.

NOTE: This patch moves the LwM2M library over to the CoAP APIs
but there will be a follow-up patch which re-writes the content
formatter reader / writers to use net_pkt APIs for parsing
across multiple net buffers. The current implementation assumes
all of the data will land in 1 buffer.

Samples using the library still need a fairly large NET_BUF_DATA_SIZE
setting. (Example: CONFIG_NET_BUF_DATA_SIZE=384)

Signed-off-by: Michael Scott <michael.scott@linaro.org>
Signed-off-by: Michael Scott <michael.scott@linaro.org>
This config is used with samples/net/lwm2m_client.  It varies slightly
from the config-coap.h file used with the COAP samples in that it
has a larger SSL request size.

Signed-off-by: Michael Scott <michael.scott@linaro.org>
For QEMU testing use CONF_FILE=prj_qemu_x86_dtls.conf
For K64F testing use CONF_FILE=prj_frdm_k64f_dtls.conf

Signed-off-by: Michael Scott <michael.scott@linaro.org>
This calculation reads the length portion of the COAP header to determine
the length of the coap packet.  However, when encrypted via DTLS this
value seems to be getting corrupted.  Let's change this calculation so
that it will work for when DTLS is both enabled and disabled.  Use the
total length of the fragment data and substract back out the headers
to get a correct value.

Signed-off-by: Michael Scott <michael.scott@linaro.org>
The default net_context remote address is scrambled when using a
connection via DTLS.  Instead let's use the dtls context remote.

Signed-off-by: Michael Scott <michael.scott@linaro.org>
Let's use conservative defaults for the LwM2M library to enable
hardware with constrained resources.  Users can increase where
necessary.

Signed-off-by: Michael Scott <michael.scott@linaro.org>
Support multiple HW with only 2 configs:
1) default operation w/o DTLS support
2) enable DTLS support

NOTE: This also adjusts README.rst

Signed-off-by: Michael Scott <michael.scott@linaro.org>
LwM2M is intended for constrained devices.  The default samples
settings are quite large by that standard and can be reduced to
reflect actual usage.

Signed-off-by: Michael Scott <michael.scott@linaro.org>
@mike-scott
Copy link
Contributor Author

Rebased on master branch and added WIP DTLS support using new CoAP APIs.

This PR is mostly for @rbtchc to keep track of what's in my tree for upstream patches. But for others who are curious you're welcome to chime in.

NOTE! Still missing from this:

  • Some work to clean up lwm2m_msg in the case of an error response back to server in f6fcbc8.
  • Update the content formatter reader/writer APIs to use net_pkt parsing to drop the contiguous buffer requirement.

@mike-scott
Copy link
Contributor Author

@dbkinder no need to review this for documentation changes as these patches will flow into normal PRs.

@nashif nashif modified the milestones: v1.10, v1.10.0 Oct 3, 2017
@mike-scott
Copy link
Contributor Author

Closing. These were all merged via different PRs.

@mike-scott mike-scott closed this Oct 20, 2017
@mike-scott
Copy link
Contributor Author

I should say.. the rest will be merged in different PRs :)

@mike-scott mike-scott deleted the lwm2m-net-app branch October 20, 2017 18:02
nagineni pushed a commit to nagineni/zephyr that referenced this pull request Nov 20, 2017
The OCF was not able to send packets over BLE 6lowpan
consistently and segfaulting, this patch increase
the data size to help improve stability.

Fixes zephyrproject-rtos#1255, zephyrproject-rtos#1408, zephyrproject-rtos#1415

Signed-off-by: Jimmy Huang <jimmy.huang@intel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants