• Re: LWM2M C Language Implementation for the Small Embedded IoT Devices

    From Hanah anees@21:1/5 to kalvi...@gmail.com on Thu Apr 21 18:09:42 2022
    On Thursday, July 5, 2018 at 10:33:59 AM UTC-4, kalvi...@gmail.com wrote:
    keskiviikko 30. toukokuuta 2018 22.48.12 UTC+3 sys...@gmail.com kirjoitti:
    hi Kalvin

    any updates, sample code, im trying to do the same on an m4 g55 microchip mcu.

    thanks

    rp


    On Thursday, April 27, 2017 at 2:34:38 AM UTC+10, kalvi...@gmail.com wrote:
    Hi group!

    As a n00b in the IoT domain I have had trouble finding a suitable open source implementation in C for the LWM2M protocol with DTLS support.

    What I have found so far are following implementations:

    - Contiki lwm2m: https://github.com/sics-iot/lwm2m-contiki
    - Wakaama LWM2M: https://github.com/eclipse/wakaama
    - Awa LWM2M: https://github.com/FlowM2M/AwaLWM2M
    - Anjay: https://github.com/AVSystem/Anjay

    However I am not able to compare pros and cons of the different implementations in terms of code quality, features, maturity, code size etc.

    The target device is built on an ARM Cortex M4 with 256 KB Flash memory, 48 KB of RAM and an Ethernet connection. The network stack might be LwIP supporting IPv4 and IPv6, but if I decide to go for Contiki I probably will use the built-in uIP stack.
    The device is a OS-less bare-metal system or Contiki.

    I would appreciate very much if someone has experience in those implementations and could give me some feedback or point me to the right direction.

    Best regards,
    Kalvin
    Hi rp,
    Sorry this late reply, but I haven't been reading the newsgroups lately. Anyway, here is how I proceeded with the porting effort:

    1. First, I took the Wakaama client source code and the LwIP 2.0.2 source code, and made the Wakaama client running on a PC using the LwIP socket interface. This required tweaking the Wakaama build scripts so that the build process would also build the
    LwIP and link it with Wakaama.

    2. Modified Wakaama client to use the LwIP raw API running on a PC. The LwIP porting guide is a good place to start:
    http://lwip.wikia.com/wiki/Porting_for_an_OS

    This all was pretty straight forward to do as Wakaama has quite simple and thin networking interface using only UDP protocol.

    Wireshark proved to be a very valuable too in this process when debugging the DTSL-related stuff for example.

    You may need to use the TAP-interface adapter so that the LwIP is able to use the Ethernet interface of your development machine:
    https://backreference.org/2010/03/26/tuntap-interface-tutorial/

    Now that I had the Wakaama client running on a PC using LwIP raw interface I had to make it running on my target hardware.

    3. I needed setup a LwIP PPP connection.

    4. Tweaked the LwIP configuration to fit the target hardware.

    5. Tweaked the TinyDTLS build scripts so that it compiles cleanly on the target hardware and ARM C compiler.

    6. My target ARM-system did not provide the malloc and free, so I had to figure out what to do with that. Luckily the LwIP provided also malloc and free, and modified the source code to use these library functions. There were some other minor functions
    that I needed to implement, like the time functions.

    6. After a clean build and some printf-debugging, I was able to connect my target hardware running Wakaama client to a server over PPP.

    That's basically the whole process. Although I hadn't been working with the LwIP nor Wakaama before, it took me less than two weeks (with very little sleep) to get the things up and running and to get the Wakaama LWM2M client DTLS connection working
    with the Leshan server.

    During more comprehensive testing it was found out that DTLS/Wakaama was bleeding some memory. I added some printf's to LwIP malloc/free adapter stubs which were created to mimic the std malloc and free functions. Created a simple Python tool to track
    the malloc/free memory blocks using the printf-output, and I was able to spot the problematic places and patched the code to release the allocated memory so that the malloc/free was working without any further issues.

    Hope this helps you to get started.

    Br,
    Kalvin

    Hello All,
    We are integrating Anjay LWM2M client on Murata Type 1LD (cypress psoc). if mbedTLS is enabled there is an error trying to register. Where as the code works in non -secure mode. Also the same code works fine in mbedTLS mode from linux machine. So this
    has to be an issue with Anjay LWM2M and WICED integration. Trying to debug and not sure what the following error means, any help is much appreciated

    Setting IPv6 link-local address
    Obtaining IPv4 address via DHCP
    IPv6 Network ready IP: FE80::29D:6BFF:FE62:78AA
    IPv4 Network ready IP: 192.168.1.195
    Wifi network up success
    Wifi network up success

    TEST Version 1.0.4

    *** mbedtls_ctr_drbg_seed ***

    entropy_callback

    ######################## entropy_callback
    Seed_callback out buff : 536930016
    Initializing Anjay RunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunning$$$$$ AVS_NET_DTLS_SOCKET $$$$$$
    avs_net_dtls_socket_create
    WITH SSL
    $$$$$create_ssl_socket
    configure_ssl socket 0x2000e3cc, configuration= 0x2000e4e8

    *initialize_ssl_socket
    AVS_NET_SECURITY_PSK

    ******** initialize_psk_security ******

    ******************* mbedtls_ssl_conf_psk ###################
    Psk key P4s$w0rd length 8
    Identity testboard length 9
    $$$$$$$ AVS_OK
    ************_anjay_server_connection_internal_bring_online

    Start SSL
    **Could not restore session; performing full handshake
    MBEDTLS_SSL_CLI_C end point 0
    inside mbedtls_ssl_flush_output
    wrap_handshake_result Result 0, security mode 1, dane_ta_certs (null) verify_result 0
    handshake success: new session started
    **Return AVS_OK

    ANJAY_REGISTRATION_ERROR_REJECTED
    update rejected for SSID 1 needs re-registration
    Attempting to register with LwM2M version 1.0
    sending Register
    lwm2m version 0
    coap_base->last_exchange_id 0
    (*exchange_ptr)->id 0
    avs_time_monotonic_now() second 0 nanosec 9
    Register send
    Runningget_dtls_overhead
    avs_net_socket_send

    send_ssl start

    Output Bytes:48 2 B4 66 D 88 34 D0 23 5B 8B 31 B2 72 64 11 28 39 6C 77 6D 32 6D 3D 31 2E 30 C 65 70 3D 74 65 73 74 62 6F 61 72 64 5 6C 74 3D 36 30 FF 3C 2F 31 2F 30 3E
    send_ssl socket= 0x20015b18 , buffer= 0x20017014 , buffer_length= 53
    Going for mbedtls_ssl_write
    Handshake not NULL
    Client id (null) client id length 0 Host name try-anjay.avsystem.com psk (null) psk_leng 0
    Psk key P4s$w0rd ident testboard
    Hand shake not over
    MBEDTLS_SSL_CLI_C end point 0
    inside mbedtls_ssl_flush_output
    inside mbedtls_ssl_flush_output
    avs_bio_send
    avs_net_socket_send
    inside mbedtls_ssl_flush_output
    avs_bio_send
    avs_net_socket_send
    wrap_handshake_result Result 53, security mode 1, dane_ta_certs (null) verify_result 0
    Hand shake result : 53
    RunningReceive_ssl socket = 0x20015b18, buffer= 0x200141e8 , buffer_length= 2048inside mbedtls_ssl_flush_output
    MBEDTLS_SSL_CLI_C end point 0
    inside mbedtls_ssl_flush_output
    wrap_handshake_result Result -76, security mode 1, dane_ta_certs (null) verify_result 0
    receive failed: -76
    RunningRunningRunningavs_net_socket_send

    send_ssl start

    Output Bytes:48 2 B4 66 D 88 34 D0 23 5B 8B 31 B2 72 64 11 28 39 6C 77 6D 32 6D 3D 31 2E 30 C 65 70 3D 74 65 73 74 62 6F 61 72 64 5 6C 74 3D 36 30 FF 3C 2F 31 2F 30 3E
    send_ssl socket= 0x20015b18 , buffer= 0x20017014 , buffer_length= 53
    Going for mbedtls_ssl_write
    Handshake not NULL
    Client id (null) client id length 0 Host name try-anjay.avsystem.com psk (null) psk_leng 0
    Psk key P4s$w0rd ident testboard
    Hand shake not over
    MBEDTLS_SSL_CLI_C end point 0
    inside mbedtls_ssl_flush_output
    inside mbedtls_ssl_flush_output
    avs_bio_send
    avs_net_socket_send
    Handshake return -26880
    Going for mbedtls_ssl_write
    Handshake not NULL
    Client id (null) client id length 0 Host name try-anjay.avsystem.com psk (null) psk_leng 0
    Psk key P4s$w0rd ident testboard
    Hand shake not over
    MBEDTLS_SSL_CLI_C end point 0
    inside mbedtls_ssl_flush_output
    Handshake return -76
    wrap_handshake_result Result -76, security mode 1, dane_ta_certs (null) verify_result 0
    Hand shake result : -76
    socket bio_error
    send failed 0 53 : -76
    AVS_COAP_CLIENT_REQUEST_FAIL
    failure while receiving Register response: Message too long
    mapping CoAP error Message too long to network error ANJAY_REGISTRATION_ERROR_NETWORK
    could not register to server 1

    Thanks
    Anees

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Vikram Kerkar@21:1/5 to Hanah anees on Thu Jun 16 12:07:47 2022
    On Friday, 22 April 2022 at 06:39:45 UTC+5:30, Hanah anees wrote:
    On Thursday, July 5, 2018 at 10:33:59 AM UTC-4, kalvi...@gmail.com wrote:
    keskiviikko 30. toukokuuta 2018 22.48.12 UTC+3 sys...@gmail.com kirjoitti:
    hi Kalvin

    any updates, sample code, im trying to do the same on an m4 g55 microchip mcu.

    thanks

    rp


    On Thursday, April 27, 2017 at 2:34:38 AM UTC+10, kalvi...@gmail.com wrote:
    Hi group!

    As a n00b in the IoT domain I have had trouble finding a suitable open source implementation in C for the LWM2M protocol with DTLS support.

    What I have found so far are following implementations:

    - Contiki lwm2m: https://github.com/sics-iot/lwm2m-contiki
    - Wakaama LWM2M: https://github.com/eclipse/wakaama
    - Awa LWM2M: https://github.com/FlowM2M/AwaLWM2M
    - Anjay: https://github.com/AVSystem/Anjay

    However I am not able to compare pros and cons of the different implementations in terms of code quality, features, maturity, code size etc.

    The target device is built on an ARM Cortex M4 with 256 KB Flash memory, 48 KB of RAM and an Ethernet connection. The network stack might be LwIP supporting IPv4 and IPv6, but if I decide to go for Contiki I probably will use the built-in uIP
    stack. The device is a OS-less bare-metal system or Contiki.

    I would appreciate very much if someone has experience in those implementations and could give me some feedback or point me to the right direction.

    Best regards,
    Kalvin
    Hi rp,
    Sorry this late reply, but I haven't been reading the newsgroups lately. Anyway, here is how I proceeded with the porting effort:

    1. First, I took the Wakaama client source code and the LwIP 2.0.2 source code, and made the Wakaama client running on a PC using the LwIP socket interface. This required tweaking the Wakaama build scripts so that the build process would also build
    the LwIP and link it with Wakaama.

    2. Modified Wakaama client to use the LwIP raw API running on a PC. The LwIP porting guide is a good place to start:
    http://lwip.wikia.com/wiki/Porting_for_an_OS

    This all was pretty straight forward to do as Wakaama has quite simple and thin networking interface using only UDP protocol.

    Wireshark proved to be a very valuable too in this process when debugging the DTSL-related stuff for example.

    You may need to use the TAP-interface adapter so that the LwIP is able to use the Ethernet interface of your development machine:
    https://backreference.org/2010/03/26/tuntap-interface-tutorial/

    Now that I had the Wakaama client running on a PC using LwIP raw interface I had to make it running on my target hardware.

    3. I needed setup a LwIP PPP connection.

    4. Tweaked the LwIP configuration to fit the target hardware.

    5. Tweaked the TinyDTLS build scripts so that it compiles cleanly on the target hardware and ARM C compiler.

    6. My target ARM-system did not provide the malloc and free, so I had to figure out what to do with that. Luckily the LwIP provided also malloc and free, and modified the source code to use these library functions. There were some other minor
    functions that I needed to implement, like the time functions.

    6. After a clean build and some printf-debugging, I was able to connect my target hardware running Wakaama client to a server over PPP.

    That's basically the whole process. Although I hadn't been working with the LwIP nor Wakaama before, it took me less than two weeks (with very little sleep) to get the things up and running and to get the Wakaama LWM2M client DTLS connection working
    with the Leshan server.

    During more comprehensive testing it was found out that DTLS/Wakaama was bleeding some memory. I added some printf's to LwIP malloc/free adapter stubs which were created to mimic the std malloc and free functions. Created a simple Python tool to
    track the malloc/free memory blocks using the printf-output, and I was able to spot the problematic places and patched the code to release the allocated memory so that the malloc/free was working without any further issues.

    Hope this helps you to get started.

    Br,
    Kalvin
    Hello All,
    We are integrating Anjay LWM2M client on Murata Type 1LD (cypress psoc). if mbedTLS is enabled there is an error trying to register. Where as the code works in non -secure mode. Also the same code works fine in mbedTLS mode from linux machine. So this
    has to be an issue with Anjay LWM2M and WICED integration. Trying to debug and not sure what the following error means, any help is much appreciated

    Setting IPv6 link-local address
    Obtaining IPv4 address via DHCP
    IPv6 Network ready IP: FE80::29D:6BFF:FE62:78AA
    IPv4 Network ready IP: 192.168.1.195
    Wifi network up success
    Wifi network up success

    TEST Version 1.0.4

    *** mbedtls_ctr_drbg_seed ***

    entropy_callback

    ######################## entropy_callback
    Seed_callback out buff : 536930016
    Initializing Anjay RunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunningRunning$$$$$ AVS_NET_DTLS_SOCKET $$$$$$
    avs_net_dtls_socket_create
    WITH SSL
    $$$$$create_ssl_socket
    configure_ssl socket 0x2000e3cc, configuration= 0x2000e4e8

    *initialize_ssl_socket
    AVS_NET_SECURITY_PSK

    ******** initialize_psk_security ******

    ******************* mbedtls_ssl_conf_psk ###################
    Psk key P4s$w0rd length 8
    Identity testboard length 9
    $$$$$$$ AVS_OK
    ************_anjay_server_connection_internal_bring_online

    Start SSL
    **Could not restore session; performing full handshake
    MBEDTLS_SSL_CLI_C end point 0
    inside mbedtls_ssl_flush_output
    wrap_handshake_result Result 0, security mode 1, dane_ta_certs (null) verify_result 0
    handshake success: new session started
    **Return AVS_OK

    ANJAY_REGISTRATION_ERROR_REJECTED
    update rejected for SSID 1 needs re-registration
    Attempting to register with LwM2M version 1.0
    sending Register
    lwm2m version 0
    coap_base->last_exchange_id 0
    (*exchange_ptr)->id 0
    avs_time_monotonic_now() second 0 nanosec 9
    Register send
    Runningget_dtls_overhead
    avs_net_socket_send

    send_ssl start

    Output Bytes:48 2 B4 66 D 88 34 D0 23 5B 8B 31 B2 72 64 11 28 39 6C 77 6D 32 6D 3D 31 2E 30 C 65 70 3D 74 65 73 74 62 6F 61 72 64 5 6C 74 3D 36 30 FF 3C 2F 31 2F 30 3E
    send_ssl socket= 0x20015b18 , buffer= 0x20017014 , buffer_length= 53
    Going for mbedtls_ssl_write
    Handshake not NULL
    Client id (null) client id length 0 Host name try-anjay.avsystem.com psk (null) psk_leng 0
    Psk key P4s$w0rd ident testboard
    Hand shake not over
    MBEDTLS_SSL_CLI_C end point 0
    inside mbedtls_ssl_flush_output
    inside mbedtls_ssl_flush_output
    avs_bio_send
    avs_net_socket_send
    inside mbedtls_ssl_flush_output
    avs_bio_send
    avs_net_socket_send
    wrap_handshake_result Result 53, security mode 1, dane_ta_certs (null) verify_result 0
    Hand shake result : 53
    RunningReceive_ssl socket = 0x20015b18, buffer= 0x200141e8 , buffer_length= 2048inside mbedtls_ssl_flush_output
    MBEDTLS_SSL_CLI_C end point 0
    inside mbedtls_ssl_flush_output
    wrap_handshake_result Result -76, security mode 1, dane_ta_certs (null) verify_result 0
    receive failed: -76
    RunningRunningRunningavs_net_socket_send

    send_ssl start

    Output Bytes:48 2 B4 66 D 88 34 D0 23 5B 8B 31 B2 72 64 11 28 39 6C 77 6D 32 6D 3D 31 2E 30 C 65 70 3D 74 65 73 74 62 6F 61 72 64 5 6C 74 3D 36 30 FF 3C 2F 31 2F 30 3E
    send_ssl socket= 0x20015b18 , buffer= 0x20017014 , buffer_length= 53
    Going for mbedtls_ssl_write
    Handshake not NULL
    Client id (null) client id length 0 Host name try-anjay.avsystem.com psk (null) psk_leng 0
    Psk key P4s$w0rd ident testboard
    Hand shake not over
    MBEDTLS_SSL_CLI_C end point 0
    inside mbedtls_ssl_flush_output
    inside mbedtls_ssl_flush_output
    avs_bio_send
    avs_net_socket_send
    Handshake return -26880
    Going for mbedtls_ssl_write
    Handshake not NULL
    Client id (null) client id length 0 Host name try-anjay.avsystem.com psk (null) psk_leng 0
    Psk key P4s$w0rd ident testboard
    Hand shake not over
    MBEDTLS_SSL_CLI_C end point 0
    inside mbedtls_ssl_flush_output
    Handshake return -76
    wrap_handshake_result Result -76, security mode 1, dane_ta_certs (null) verify_result 0
    Hand shake result : -76
    socket bio_error
    send failed 0 53 : -76
    AVS_COAP_CLIENT_REQUEST_FAIL
    failure while receiving Register response: Message too long
    mapping CoAP error Message too long to network error ANJAY_REGISTRATION_ERROR_NETWORK
    could not register to server 1

    Thanks
    Anees

    Hi Anees,

    I'm also working with Wakaama LwM2M library.
    The customer server is secured (COAPS) so need to use DTLS. For this, I'm using mbedTLS.
    But somehow I'm not able to make a handshake with the server.
    Could you help to share the code snippet (mbedTLS) part? That will great help for me.

    Thanks,
    Vikram

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)