c - 如何在 Contiki OS 的运行时执行不同的二进制文件?

标签 c elf contiki

如果发生事件,我正在尝试制作一个在 Contiki OS 上运行的传感器来执行新的二进制文件并替换当前的二进制文件。

我正在使用 Cooja 模拟器和天空笔记,我将二进制文件上传到节点的咖啡文件系统(使用 cooja 脚本),我想执行 hello-world.ce

要编译我将动态加载模块的当前程序(reboot.c),我使用了以下命令:

  • 使 TARGET=sky clean CLEAN=symbols.?
  • make reboot.sky TARGET=sky
  • make CORE=reboot.sky 目标=sky reboot.sky

  • 对于将要加载的 hello-world,我使用了:
  • make TARGET=sky hello-world.ce

  • 这是我尝试执行 hello-world 的代码(reboot.c)的一部分
    #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();
    }
    

    似乎 elfloader_autostart_processes 设置为 null 因为 print 语句未在 for 循环中执行。该程序继续并打印“重启程序结束”,我期待它打印 hello-world 作为二进制文件已加载和启动的指示。

    你能提供任何帮助吗?

    最佳答案

    我设法使用 Contiki 库中的另一个构建来实现它。具体来说,我使用了负责执行文件的 shell-exec.c,并将它作为参数提供了我想在运行时执行的文件。我使用 Cooja 脚本将文件上传到 Cooja 模拟器中的传感器上。

    #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 脚本
    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");
    

    关于c - 如何在 Contiki OS 的运行时执行不同的二进制文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57943992/

    相关文章:

    c - 导入地址表和全局偏移表有什么区别?

    C、Contiki rime,传输结构

    Contiki os MQTT

    从Contiki中的进程调用进程

    c - 如何在不同文件中使用struct C编程

    c++ - OpenGL Nvidia 驱动程序 259.12 纹理不工作

    assembly - 为什么编译这个简单的汇编代码并链接到 ELF 会导致比平面二进制文件更大的大小?

    c - 编译器 C 的全局变量、静态变量和字符串文字的位置地址在哪里?

    c - 网络不好的 C 中的 TCP 套接字

    c - gdb 目标远程 :1234 connection timeout linux