AVSystem / Anjay

C implementation of the client-side OMA LwM2M protocol

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using 'demo' with *only* a bootstrap server

chelliwell opened this issue · comments

I am working on an embedded system (STM32F7 + ThreadX + lwIP + mbedTLS) into which I am integrating Anjay.
I've adapted the 'demo' to become my LWM2M client thread. Because it is an embedded system, I don't use demo_args.c but instead I am just hard-coding values into DEFAULT_CMDLINE_ARGS.
I am using Leshan servers (192.168.10.86), just for simple connection testing. I can set .servers[0] with URI for my server, and it connects and communicates correctly.
Now I want to try bootstrap server functionality (again, using the Leshan bootstrap server).
What I have found is that if I configure with a 'fictional' server then Anjay tries it, fails, and falls back to the bootstrap server. It can then get the real server info from the bootstrap, and can connect as before.
BUT I can't configure DEFAULT_CMDLINE_ARGS with only the bootstrap server.
In other words this succeeds [using info given by bootstrap server]:

    {
        .servers[0] = 
        {
            .security_iid = ANJAY_IID_INVALID,
            .server_iid = ANJAY_IID_INVALID,
            .id = 1,
            .uri = "coap://192.168.10.86:5700",
            .is_bootstrap = true
        },
        .servers[1] = 
        {
            .security_iid = ANJAY_IID_INVALID,
            .server_iid = ANJAY_IID_INVALID,
            .id = 2,
            .uri = "coap://99.99.99.99:5683",
            .is_bootstrap = false
        },
        ....

But this - servers[0] only - doesn't:

    {
        .servers[0] = 
        {
            .security_iid = ANJAY_IID_INVALID,
            .server_iid = ANJAY_IID_INVALID,
            .id = 1,
            .uri = "coap://192.168.10.86:5700",
            .is_bootstrap = true
        },
        ....

The rest of the config is

        .bootstrap_holdoff_s = 5,
        .bootstrap_timeout_s = 10,
        .lifetime = 120,
        .binding_mode = "U",
        .security_mode = ANJAY_UDP_SECURITY_NOSEC,
    },
    .inbuf_size = 4000,
    .outbuf_size = 4000,
    .msg_cache_size = 0,
    .fw_updated_marker_path = "/tmp/anjay-fw-updated",
    .fw_security_info =     { .mode = (avs_net_security_mode_t) -1 },
    .attr_storage_file = NULL,
    .disable_server_initiated_bootstrap = true,
    .tx_params = ANJAY_COAP_DEFAULT_UDP_TX_PARAMS,
    .dtls_hs_tx_params = ANJAY_DTLS_DEFAULT_UDP_HS_TX_PARAMS,
    .fwu_tx_params_modified = false,
    .fwu_tx_params = ANJAY_COAP_DEFAULT_UDP_TX_PARAMS

I'm puzzled why it can't be given just a bootstrap server, and work from there?
Watching in Wireshark, I see nothing being sent to the bootstrap server; and the client logs only report:

2018-11-23 16:49:40 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/anjay_core.c:171]: Initializing Anjay 1.13.1
2018-11-23 16:49:40 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/dm_core.c:129]: successfully registered object /2
2018-11-23 16:49:40 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/dm_core.c:129]: successfully registered object /0
2018-11-23 16:49:40 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/dm_core.c:129]: successfully registered object /1
2018-11-23 16:49:40 DEBUG [client] [C:\Projects\AmPlatform-Development_TCPIP\Transceiver\lwm2m\objects\device.c:407]: manufacturer: Atronic; serial number: 000001
2018-11-23 16:49:40 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/dm_core.c:129]: successfully registered object /3
2018-11-23 16:49:40 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/dm_core.c:129]: successfully registered object /11111
2018-11-23 16:49:40 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/dm_core.c:129]: successfully registered object /1337
2018-11-23 16:49:40 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/dm_core.c:129]: successfully registered object /16
2018-11-23 16:49:40 DEBUG [security] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/modules/security/src/security_utils.c:64]: Invalid SMS Security Mode
2018-11-23 16:49:40 INFO [security] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/modules/security/src/mod_security.c:166]: Added instance 0 (bootstrap, URI: coap://192.168.10.86:5700)
2018-11-23 16:49:40 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/servers/reload.c:165]: servers reloaded
2018-11-23 16:49:40 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/servers/connection_udp.c:164]: server /0/0: UDP security mode = 3
2018-11-23 16:49:40 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/servers/connection_udp.c:350]: bound to port 52720
2018-11-23 16:49:40 DEBUG [avs_net] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/avs_commons/git/net/compat/posix/net_impl.c:1891]: get_opt_net: unknown or unsupported option key: (avs_net_socket_opt_key_t) 5
2018-11-23 16:49:40 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/servers/connections.c:161]: reconnected
2018-11-23 16:49:40 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/interface/bootstrap_core.c:890]: scheduling Client Initiated Bootstrap
2018-11-23 16:49:40 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/servers_utils.c:105]: Registration session changed for SSID = 65535, forcing re-register
2018-11-23 16:49:45 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/servers/servers_internal.c:390]: servers_foreach_ssid: break on 65535
2018-11-23 16:49:45 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/interface/bootstrap_core.c:828]: Bootstrap already started on the same connection

With the fictional server included:

2018-11-23 17:03:10 DEBUG [client] [C:\Projects\AmPlatform-Development_TCPIP\Transceiver\lwm2m\objects\device.c:407]: manufacturer: Atronic; serial number: 000001
2018-11-23 17:03:10 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/dm_core.c:129]: successfully registered object /3
2018-11-23 17:03:10 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/dm_core.c:129]: successfully registered object /11111
2018-11-23 17:03:10 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/dm_core.c:129]: successfully registered object /1337
2018-11-23 17:03:10 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/dm_core.c:129]: successfully registered object /16
2018-11-23 17:03:10 DEBUG [security] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/modules/security/src/security_utils.c:64]: Invalid SMS Security Mode
2018-11-23 17:03:10 INFO [security] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/modules/security/src/mod_security.c:166]: Added instance 0 (bootstrap, URI: coap://192.168.10.86:5700)
2018-11-23 17:03:10 DEBUG [security] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/modules/security/src/security_utils.c:64]: Invalid SMS Security Mode
2018-11-23 17:03:10 INFO [security] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/modules/security/src/mod_security.c:169]: Added instance 1 (SSID: 2, 2018-11-23 17:03:12 ERROR [avs_coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/avs_commons/git/coap/src/ctx.c:125]: receive failed: errno = 110
2018-11-23 17:03:12 ERROR [coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/coap/stream/in.c:43]: recv returned -1505 (unknown error)
2018-11-23 17:03:12 DEBUG [coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/coap/stream/client_internal.c:296]: timeout reached, next: 8589934592.090 s
2018-11-23 17:03:12 DEBUG [avs_coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/avs_commons/git/coap/src/ctx.c:191]: send: 0.02 Post, CONFIRMABLE, id 23429, token e8e732392cfb56ad (8B)
2018-11-23 17:03:18 ERROR [avs_coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/avs_commons/git/coap/src/ctx.c:125]: receive failed: errno = 110
2018-11-23 17:03:18 ERROR [coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/coap/stream/in.c:43]: recv returned -1505 (unknown error)
2018-11-23 17:03:18 DEBUG [coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/coap/stream/client_internal.c:296]: timeout reached, next: 21474836480.090 s
2018-11-23 17:03:18 DEBUG [avs_coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/avs_commons/git/coap/src/ctx.c:191]: send: 0.02 Post, CONFIRMABLE, id 23429, token e8e732392cfb56ad (8B)
2018-11-23 17:03:28 ERROR [avs_coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/avs_commons/git/coap/src/ctx.c:125]: receive failed: errno = 110
2018-11-23 17:03:28 ERROR [coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/coap/stream/in.c:43]: recv returned -1505 (unknown error)
2018-11-23 17:03:28 DEBUG [coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/coap/stream/client_internal.c:296]: timeout reached, next: 42949672960.090 s
2018-11-23 17:03:28 DEBUG [avs_coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/avs_commons/git/coap/src/ctx.c:191]: send: 0.02 Post, CONFIRMABLE, id 23429, token e8e732392cfb56ad (8B)
2018-11-23 17:03:48 ERROR [avs_coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/avs_commons/git/coap/src/ctx.c:125]: receive failed: errno = 110
2018-11-23 17:03:48 ERROR [coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/coap/stream/in.c:43]: recv returned -1505 (unknown error)
2018-11-23 17:03:48 DEBUG [coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/coap/stream/client_internal.c:296]: timeout reached, next: 85899345920.090 s
2018-11-23 17:03:48 DEBUG [avs_coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/avs_commons/git/coap/src/ctx.c:191]: send: 0.02 Post, CONFIRMABLE, id 23429, token e8e732392cfb56ad (8B)
2018-11-23 17:04:28 ERROR [avs_coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/avs_commons/git/coap/src/ctx.c:125]: receive failed: errno = 110
2018-11-23 17:04:28 ERROR [coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/coap/stream/in.c:43]: recv returned -1505 (unknown error)
2018-11-23 17:04:28 DEBUG [coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/coap/stream/client_internal.c:296]: timeout reached, next: 171798691840.090 s
2018-11-23 17:04:28 ERROR [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/interface/register.c:189]: could not send Register message
2018-11-23 17:04:28 ERROR [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/interface/register.c:387]: could not register to server 2
2018-11-23 17:04:28 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/servers/register_internal.c:260]: re-registration timed out
2018-11-23 17:04:28 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/servers_utils.c:105]: Registration session changed for SSID = 2, forcing re-register
2018-11-23 17:04:28 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/servers/connection_udp.c:164]: server /0/0: UDP security mode = 3
2018-11-23 17:04:28 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/servers/connection_udp.c:350]: bound to port 52721
2018-11-23 17:04:28 DEBUG [avs_net] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/avs_commons/git/net/compat/posix/net_impl.c:1891]: get_opt_net: unknown or unsupported option key: (avs_net_socket_opt_key_t) 5
2018-11-23 17:04:28 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/servers/connections.c:161]: reconnected
2018-11-23 17:04:28 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/interface/bootstrap_core.c:890]: scheduling Client Initiated Bootstrap
2018-11-23 17:04:28 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/servers_utils.c:105]: Registration session changed for SSID = 65535, forcing re-register
2018-11-23 17:04:33 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/servers/servers_internal.c:390]: servers_foreach_ssid: break on 65535
2018-11-23 17:04:33 DEBUG [coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/coap/stream/out.c:57]: Buffer size: 4004; socket MTU: 1464
2018-11-23 17:04:33 DEBUG [avs_coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/avs_commons/git/coap/src/ctx.c:191]: send: 0.02 Post, CONFIRMABLE, id 23430, token 30cf3ae1f463ded5 (8B)
2018-11-23 17:04:33 DEBUG [avs_coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/avs_commons/git/coap/src/ctx.c:281]: recv: 2.04 Changed, ACKNOWLEDGEMENT, id 23430, token 30cf3ae1f463ded5 (8B)
2018-11-23 17:04:33 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/interface/bootstrap_core.c:709]: Request Bootstrap sent
2018-11-23 17:04:33 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/servers/servers_internal.c:390]: servers_foreach_ssid: break on 65535
2018-11-23 17:04:33 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/anjay_core.c:725]: bootstrap server
2018-11-23 17:04:33 DEBUG [avs_coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/avs_commons/git/coap/src/ctx.c:281]: recv: 0.04 Delete, CONFIRMABLE, id 47203, token 92c8d88e568ae039 (8B)
2018-11-23 17:04:33 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/anjay_core.c:476]: LwM2M action: Delete
2018-11-23 17:04:33 DEBUG [coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/coap/stream/out.c:57]: Buffer size: 4004; socket MTU: 1464
2018-11-23 17:04:33 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/interface/bootstrap_core.c:415]: Bootstrap Delete /
2018-11-23 17:04:33 ERROR [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/dm/dm_handlers.c:188]: instance_remove handler not set for object /3
2018-11-23 17:04:33 ERROR [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/interface/bootstrap_core.c:368]: delete_instance: cannot delete /3/0: -133
2018-11-23 17:04:33 ERROR [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/dm/dm_handlers.c:367]: transaction_begin handler not set for object /11111
2018-11-23 17:04:33 ERROR [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/interface/bootstrap_core.c:368]: delete_instance: cannot delete /11111/0: -133
2018-11-23 17:04:33 DEBUG [avs_coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/avs_commons/git/coap/src/ctx.c:191]: send: 2.02 Delers_internal.c:390]: servers_foreach_ssid: break on 65535
2018-11-23 17:04:33 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anja2018-11-23 17:04:43 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/servers/servers_internal.c:390]: servers_foreach_ssid: break on 123
2018-11-23 17:04:43 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/servers/connections.c:141]: socket already connected
2018-11-23 17:04:43 DEBUG [coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/coap/stream/out.c:57]: Buffer size: 4004; socket MTU: 1464
2018-11-23 17:04:43 DEBUG [avs_coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/avs_commons/git/coap/src/ctx.c:191]: send: 0.02 Post, CONFIRMABLE, id 23432, token c09f4a318433ee25 (8B)
2018-11-23 17:04:43 DEBUG [avs_coap] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/avs_commons/git/coap/src/ctx.c:281]: recv: 2.04 Changed, ACKNOWLEDGEMENT, id 23432, token c09f4a318433ee25 (8B)
2018-11-23 17:04:43 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/interface/register.c:465]: Update sent
2018-11-23 17:04:43 INFO [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/interface/register.c:482]: registration successfully updated
2018-11-23 17:04:43 DEBUG [anjay] [C:/Projects/AmPlatform-Development_TCPIP/Anjay/source/src/servers/register_internal.c:86]: scheduling update for SSID 123 after 0.537333492

Unfortunately, I haven't been able to reproduce your problem locally - your configuration works fine when pasted into the demo client and ran normally on Linux. Even the logs look the same as what you pasted - but the client connects. There might be some problem with your integration code with the embedded platform, or with some modifications you might have done in seemingly unrelated parts of the code.

I'd advise you to try running the demo client with such configuration on a proper OS (e.g. Linux), and compare the execution between that and your embedded version using a debugger. If you find some place in our code to be the cause of the problems, we'll be happy to fix it.

Also you may want to note that embedded platforms sometimes fail to keep the necessary invariants. We had an experience working with a build of ThreadX that did not align thread stacks to 8-byte boundaries, which in turn caused some standard library functions to behave erratically. You may want to double-check that nothing of the sort is happening in your code.

Thanks for the feedback and suggestions. I have it built and running on Linux now, so I can hopefully start trying to trace the problem. It may be the way I have implemented my 'client thread'.

The area where the log from my platform begins to differ is when it reports:
"src/interface/bootstrap_core.c:828]: Bootstrap already started on the same connection" <- EDIT: I'm looking into this now

My cmake options have to be different; at the moment I am using
-DWITH_AVS_COMPAT_THREADING=ON
-DWITH_CUSTOM_AVS_THREADING=OFF
-DWITH_AVS_COMPAT_THREADING_ATOMIC_SPINLOCK=ON
-DWITH_AVS_COMPAT_THREADING_PTHREAD=OFF
-DWITH_STANDARD_ALLOCATOR=OFF
-DWITH_AVS_HTTP=OFF \

Do any of these jump out as a possible cause?

Ok, I think I see the problem.
I have implemented my own clock_gettime() function. For CLOCK_REALTIME, I fetch tv_sec from my RTC but set tv_nsec=0 because the RTC [I think] only has second resolution.
If I change this to the RTOS tick count with msec resolution - which is what I return for CLOCK_MONOTONIC - then the "Bootstrap already started on the same connection" does not occur and it appears that bootstrapping succeeds.
Any thoughts on how I can resolve this please? Is there anywhere that CLOCK_REALTIME needs to be a real time, or would it all be ok if instead it is just a 'time since reset'?
(Should Anjay use a clock_getres() to determine how it should make it's decisions.....?)

Thank you for your investigation! Frankly speaking, we did not check what happens when precision of either of the clocks is as low as a second. We will look into it and investigate how can it be improved.

Any thoughts on how I can resolve this please? Is there anywhere that CLOCK_REALTIME needs to be a real time, or would it all be ok if instead it is just a 'time since reset'?

CLOCK_REALTIME is used for handling registration lifetime, and it not being actual real time can lead to problems if the machine can go into some kind of sleep mode, i.e. if the clock would not account for the time spent in sleep. That would cause the registration lifetime logic to behave erratically.

Also you definitely want to make sure that such "hacked" not-really-realtime clock is not used by the DTLS library, because otherwise you won't be able to properly check certificate validity.

Thanks. Cert validity was certainly a possible problem area that I had thought of.
(Does Anjay use it's clock_gettime for this and then pass it to the DTLS lib. i.e. Or, as long as the lib has its own method to get Real Datetime, then Anjay will be ok?

Or, as long as the lib has its own method to get Real Datetime, then Anjay will be ok?

Anjay will be OK in this case. AFAIK absolute times are never passed from Anjay to the DTLS library.

I have found a problem with using 'time since reset' - avs_time_real_now is used to seed the PRNG. This means that if I run and then reset/re-run, the same Token and Message ID are used in the CoAP message and are ignored by the server for being a 'duplicate'.
Probably I will have to modify the library code to use my RNG. It might be nice if avs_commons had the option to hook in a user-supplied PRNG function?

Since Anjay 2.3, cryptographically-safe PRNG from the TLS library, with an option to specify user-provided entropy source, is used. I hope that this solves this issue, especialy now that it has not been updated for over a year. Please open another one if you still have questions.