How to execute a different binary on run-time in Contiki OS?
Asked Answered
M

1

10

I am trying to make a sensor that runs on Contiki OS to execute a new binary and replace the current one, if an event occurs.

I am using Cooja simulator and sky note and I uploaded the binary into the coffee file system of the node(using cooja script) and I want to execute the hello-world.ce

To compile my current program(reboot.c) that i will dynamicaly load a module from I used the following commands:

  1. make TARGET=sky clean CLEAN=symbols.?
  2. make reboot.sky TARGET=sky
  3. make CORE=reboot.sky TARGET=sky reboot.sky

And for the hello-world that will be loaded I used:

  1. make TARGET=sky hello-world.ce

Here is part of my code (reboot.c) from where i am trying to execute hello-world

#include "contiki.h"
#include "core/loader/elfloader.h"
#include "cfs/cfs.h"

PROCESS(hello_world_process, "Reboot process");
AUTOSTART_PROCESSES(&hello_world_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(hello_world_process, ev, data)
{
    PROCESS_BEGIN();

    int i;
    int binFile,ret;

    elfloader_init();

    binFile=cfs_open("hello-world.ce",CFS_READ);
    printf("cfs_open:%d\n",binFile); //returns 0 so the file is opened

    ret=elfloader_load(binFile);
    cfs_close(binFile);
    printf("loader returned: %d\n",ret); //returns 0 ->meaning everything is ok

    if(ret == ELFLOADER_OK){
        printf("elf OK\n");
        for(i=0; elfloader_autostart_processes[i] != NULL; i++) {
            printf("exec: starting process %s. \n", elfloader_autostart_processes[i]->name);
        }
        autostart_start(elfloader_autostart_processes);
    }

    printf("end of rebooting program\n”);
    PROCESS_END();
}

It seems that the elfloader_autostart_processes is set to null since the print statement is not executed in the for loop. The program continues and prints "end of rebooting program" and I was expecting it to print hello-world as an indicator that the binary has been loaded and started.

Can you provide any help?

Melodramatize answered 15/9, 2019 at 12:12 Comment(5)
Could you share more of your code? This as it is doesn't have enough surrounding context to convey the problem clearly.Cullie
Yeah I added the whole code that I am currently using. This code is just a minor independent project that I am trying to implement.Melodramatize
Where and how is elfloader_autostart_processes data structure definied and initialized?Calamine
Furthermore, I assume that ret == ELFLOADER_OK is true so you get to the for loop at all?Calamine
@Calamine Yes it is true The elfloader_autostart_processes is defined at the header of core/loader/elfloader.h and it is initialised from the library as far as I knowMelodramatize
M
0

I manage to achieve it using another build in library of Contiki. Specifically I used shell-exec.c that is responsible to execute files and I gave it as an argument the file that I wanted to be executed on run-time. I used a Cooja script to upload the file on the sensor in Cooja simulator.

#include "contiki.h"
#include "shell-exec.c"
#include "serial-shell.h"
#include "cfs/cfs.h"

#include <stdio.h> /* For printf() */
/*---------------------------------------------------------------------------*/
PROCESS(rebooting, "Reboot process");
AUTOSTART_PROCESSES(&rebooting);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(rebooting, ev, data)
{
  PROCESS_BEGIN();
    
    static struct etimer et;
    /* Allow some time for the file to upload using cooja. */
    etimer_set(&et, 10 * CLOCK_SECOND);
    PROCESS_WAIT_UNTIL(etimer_expired(&et));

    process_start(&shell_exec_process,"hello-world.ce\0");
    

  PROCESS_END();
}

Cooja Script

WAIT_UNTIL(msg.startsWith('Starting'));
log.log("Mote started\n");
mymote = mote; /* store mote reference */

fs = mote.getFilesystem();

DIR="~/hello-world"; /* Directory that the binary is stored in the host machine running cooja */

mote_file=DIR+ "hello-world.ce";

ret=fs.insertFile(mote_file);

log.log("inserted");
Melodramatize answered 13/8, 2020 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.