Copyright ©

Mongoose OS Forum

ATTENTION! This forum has moved to:

Do not post any new messages.

Mongoose on PIC32 Harmony

pfriedpfried Germany Stuttgart

Mongoose on the PIC32 platform

I plan to bring the mongoose library to the PIC32 platform and specifically to the Harmony TCP/IP stack. I want to summarize what i plan to do and where i need some support.

Harmony stack

Microchip provides a software framework called Harmony where they do have a TCP/IP stack which contains a BSD like socket API. Additionally they do have a filesystem where they provide some level of POSIX compatibility.

For the sake of simplicity i will do the filesystem support at a later stage and concentrate on the TCP/IP stack first.

How to integrate?

My first approach would be to define the MG_LOCALS which includes a mg_locals.h file where i can include libraries and define enabling/disabling flags and provide entrypoints to the custom code parts. Also i do set #define CS_PLATFORM 0. In a later stage this would likely go into the mongoose.c/h files by defining a MG_PIC32 symbol and using preprocessor commands.

The follwing contents are placed in the mg_locals.h file.

TCP/IP Stack

Header files

#include <sys/time.h>
#include <sys/fcntl.h>
#include <sys/errno.h>

#include <time.h>
#include <stdio.h>

The docs on the Harmony TCP/IP BSD API state that all calls to the API are non-blocking. There are a few undefined symbols and functions which i think have a close relation and are missing because of the non-blocking behaviour:

#define SO_ERROR
// Select is used to effectively poll the sockets, but as we have a low number maybe no using it is not too bad
// fcntl is only used once to make a socket non-blocking: fcntl(sock, F_SETFL, flags | O_NONBLOCK);

How would one approach this? I think the task would be to exclude all calls to these functions and just assume that all calls are non blocking. Any thoughts on this?

Other missing functions
I need to see what to do with this function, i do not know yet if it is only a convenience function or if it is necessary to work:


The following functions should not be to hard:



  • rojerrojer Dublin, Ireland

    hi Frederick

    i had a brief look at the Harmony documentation.
    as mentioned on the call, this seems sufficiently different to deserve its own net_if implementation of the net_if interface.
    it will share a lot with the net_if_socket implementation, but you'll be able to focus on the idiosyncrasies of the platform at hand (of which there are many, it seems).
    in particular, the polling function will need to be rewritten entirely.
    not a trivial task, but should be doable.

  • pfriedpfried Germany Stuttgart

    Maybe this code example will give some more insight into the behaviour of the BSD API

  • pfriedpfried Germany Stuttgart
    edited August 2016

    At this point i got mongoose working for basic stuff, so i the whole TCP flow works and i can accept and respond to HTTP requests. The net_if stayed as it is, i added only a little bit in the mg_mgr_poll function:

    #ifdef MG_PIC32
         * Translate from BSD socket identifiers to native tcp sockets
         * The 'parent' socket does not have a native tcp handle, so we will get
         * a NET_PRES_INVALID SOCKET identifier, whereas child sockets do have a
         * valid handle and can be checked for data. We need to set the read flag
         * for 'parent' sockets manually (they will not be read anyways but 
         * called accept() upon).
        NET_PRES_SKT_HANDLE_T native_socket = TCPIP_BSD_Socket(nc->sock);
        if(native_socket != NET_PRES_INVALID_SOCKET) {
            if(NET_PRES_SocketReadIsReady(native_socket) > 0) {
                fd_flags |= _MG_F_FD_CAN_READ;
            if(NET_PRES_SocketWriteIsReady(native_socket, 0, 1) > 0) {
                fd_flags |= _MG_F_FD_CAN_WRITE;
        } else {
            // Setting the read flag will enable accept() on the parent
            fd_flags |= _MG_F_FD_CAN_READ;

    I simply disabled the function to enable non-blocking sockets.

    There was some other stuff to do (like the parsing did not work properly, a bug in the tcp/ip stack, creating definitions and so on).

    But this part is basically done.

Sign In or Register to comment.