2010-03-15 11:02:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* COOJA Contiki mote main file.
|
|
|
|
* \author
|
|
|
|
* Fredrik Osterlind <fros@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "contiki.h"
|
2014-12-01 12:58:34 +00:00
|
|
|
#include "sys/cc.h"
|
2010-03-15 11:02:57 +00:00
|
|
|
|
|
|
|
#include "sys/clock.h"
|
|
|
|
#include "sys/etimer.h"
|
|
|
|
#include "sys/cooja_mt.h"
|
|
|
|
#include "sys/autostart.h"
|
|
|
|
|
|
|
|
#include "lib/random.h"
|
|
|
|
#include "lib/simEnvChange.h"
|
|
|
|
|
|
|
|
#include "net/netstack.h"
|
2017-05-13 11:09:58 +00:00
|
|
|
#include "net/queuebuf.h"
|
2010-03-15 11:02:57 +00:00
|
|
|
|
2014-11-03 21:48:13 +00:00
|
|
|
#include "dev/eeprom.h"
|
2010-03-15 11:02:57 +00:00
|
|
|
#include "dev/serial-line.h"
|
|
|
|
#include "dev/cooja-radio.h"
|
|
|
|
#include "dev/button-sensor.h"
|
|
|
|
#include "dev/pir-sensor.h"
|
|
|
|
#include "dev/vib-sensor.h"
|
|
|
|
|
2012-11-20 18:57:20 +00:00
|
|
|
#include "sys/node-id.h"
|
2010-03-15 11:02:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* JNI-defined functions, depends on the environment variable CLASSNAME */
|
|
|
|
#ifndef CLASSNAME
|
|
|
|
#error CLASSNAME is undefined, required by contiki-cooja-main.c
|
|
|
|
#endif /* CLASSNAME */
|
|
|
|
#define COOJA__QUOTEME(a,b,c) COOJA_QUOTEME(a,b,c)
|
|
|
|
#define COOJA_QUOTEME(a,b,c) a##b##c
|
2013-11-19 11:17:37 +00:00
|
|
|
#define COOJA_JNI_PATH Java_org_contikios_cooja_corecomm_
|
|
|
|
#define Java_org_contikios_cooja_corecomm_CLASSNAME_init COOJA__QUOTEME(COOJA_JNI_PATH,CLASSNAME,_init)
|
|
|
|
#define Java_org_contikios_cooja_corecomm_CLASSNAME_getMemory COOJA__QUOTEME(COOJA_JNI_PATH,CLASSNAME,_getMemory)
|
|
|
|
#define Java_org_contikios_cooja_corecomm_CLASSNAME_setMemory COOJA__QUOTEME(COOJA_JNI_PATH,CLASSNAME,_setMemory)
|
|
|
|
#define Java_org_contikios_cooja_corecomm_CLASSNAME_tick COOJA__QUOTEME(COOJA_JNI_PATH,CLASSNAME,_tick)
|
|
|
|
#define Java_org_contikios_cooja_corecomm_CLASSNAME_setReferenceAddress COOJA__QUOTEME(COOJA_JNI_PATH,CLASSNAME,_setReferenceAddress)
|
2010-03-15 11:02:57 +00:00
|
|
|
|
2014-12-01 20:02:57 +00:00
|
|
|
#ifndef NETSTACK_CONF_WITH_IPV4
|
|
|
|
#define NETSTACK_CONF_WITH_IPV4 0
|
2010-03-15 11:02:57 +00:00
|
|
|
#endif
|
2014-12-01 20:02:57 +00:00
|
|
|
#if NETSTACK_CONF_WITH_IPV4
|
2010-03-31 09:39:46 +00:00
|
|
|
#include "dev/rs232.h"
|
|
|
|
#include "dev/slip.h"
|
2013-11-22 08:17:54 +00:00
|
|
|
#include "net/ip/uip.h"
|
|
|
|
#include "net/ipv4/uip-fw.h"
|
2010-03-15 11:02:57 +00:00
|
|
|
#include "net/uip-fw-drv.h"
|
2013-11-22 08:17:54 +00:00
|
|
|
#include "net/ipv4/uip-over-mesh.h"
|
2010-03-31 09:39:46 +00:00
|
|
|
static struct uip_fw_netif slipif =
|
|
|
|
{UIP_FW_NETIF(0,0,0,0, 255,255,255,255, slip_send)};
|
|
|
|
static struct uip_fw_netif meshif =
|
|
|
|
{UIP_FW_NETIF(172,16,0,0, 255,255,0,0, uip_over_mesh_send)};
|
|
|
|
|
|
|
|
#define UIP_OVER_MESH_CHANNEL 8
|
|
|
|
static uint8_t is_gateway;
|
2014-12-01 20:02:57 +00:00
|
|
|
#endif /* NETSTACK_CONF_WITH_IPV4 */
|
2010-03-15 11:02:57 +00:00
|
|
|
|
2014-12-01 20:02:57 +00:00
|
|
|
#ifndef NETSTACK_CONF_WITH_IPV6
|
|
|
|
#define NETSTACK_CONF_WITH_IPV6 0
|
2010-03-15 11:02:57 +00:00
|
|
|
#endif
|
2014-12-01 20:02:57 +00:00
|
|
|
#if NETSTACK_CONF_WITH_IPV6
|
2013-11-22 08:17:54 +00:00
|
|
|
#include "net/ip/uip.h"
|
|
|
|
#include "net/ipv6/uip-ds6.h"
|
2012-02-20 19:32:05 +00:00
|
|
|
#define PRINT6ADDR(addr) printf("%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
|
2014-12-01 20:02:57 +00:00
|
|
|
#endif /* NETSTACK_CONF_WITH_IPV6 */
|
2010-03-15 11:02:57 +00:00
|
|
|
|
|
|
|
/* Simulation mote interfaces */
|
|
|
|
SIM_INTERFACE_NAME(moteid_interface);
|
|
|
|
SIM_INTERFACE_NAME(vib_interface);
|
|
|
|
SIM_INTERFACE_NAME(rs232_interface);
|
|
|
|
SIM_INTERFACE_NAME(simlog_interface);
|
|
|
|
SIM_INTERFACE_NAME(beep_interface);
|
|
|
|
SIM_INTERFACE_NAME(radio_interface);
|
|
|
|
SIM_INTERFACE_NAME(button_interface);
|
|
|
|
SIM_INTERFACE_NAME(pir_interface);
|
|
|
|
SIM_INTERFACE_NAME(clock_interface);
|
|
|
|
SIM_INTERFACE_NAME(leds_interface);
|
|
|
|
SIM_INTERFACE_NAME(cfs_interface);
|
2014-05-08 20:29:10 +00:00
|
|
|
SIM_INTERFACE_NAME(eeprom_interface);
|
|
|
|
SIM_INTERFACES(&vib_interface, &moteid_interface, &rs232_interface, &simlog_interface, &beep_interface, &radio_interface, &button_interface, &pir_interface, &clock_interface, &leds_interface, &cfs_interface, &eeprom_interface);
|
2011-02-11 14:23:59 +00:00
|
|
|
/* Example: manually add mote interfaces */
|
|
|
|
//SIM_INTERFACE_NAME(dummy_interface);
|
|
|
|
//SIM_INTERFACES(..., &dummy_interface);
|
2010-03-15 11:02:57 +00:00
|
|
|
|
|
|
|
/* Sensors */
|
|
|
|
SENSORS(&button_sensor, &pir_sensor, &vib_sensor);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* referenceVar is used for comparing absolute and process relative memory.
|
|
|
|
* (this must not be static due to memory locations)
|
|
|
|
*/
|
|
|
|
long referenceVar;
|
|
|
|
|
|
|
|
/*
|
2010-03-23 13:12:41 +00:00
|
|
|
* Contiki and rtimer threads.
|
2010-03-15 11:02:57 +00:00
|
|
|
*/
|
2010-03-23 13:12:41 +00:00
|
|
|
static struct cooja_mt_thread rtimer_thread;
|
2010-03-15 11:02:57 +00:00
|
|
|
static struct cooja_mt_thread process_run_thread;
|
|
|
|
|
2010-03-31 09:39:46 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2014-12-01 20:02:57 +00:00
|
|
|
#if NETSTACK_CONF_WITH_IPV4
|
2010-03-31 09:39:46 +00:00
|
|
|
static void
|
|
|
|
set_gateway(void)
|
|
|
|
{
|
|
|
|
if(!is_gateway) {
|
|
|
|
printf("%d.%d: making myself the IP network gateway.\n\n",
|
2013-12-12 22:58:52 +00:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
|
2010-03-31 09:39:46 +00:00
|
|
|
printf("IPv4 address of the gateway: %d.%d.%d.%d\n\n",
|
|
|
|
uip_ipaddr_to_quad(&uip_hostaddr));
|
2013-12-12 22:58:52 +00:00
|
|
|
uip_over_mesh_set_gateway(&linkaddr_node_addr);
|
2010-03-31 09:39:46 +00:00
|
|
|
uip_over_mesh_make_announced_gateway();
|
|
|
|
is_gateway = 1;
|
|
|
|
}
|
|
|
|
}
|
2014-12-01 20:02:57 +00:00
|
|
|
#endif /* NETSTACK_CONF_WITH_IPV4 */
|
2010-03-15 11:02:57 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
print_processes(struct process * const processes[])
|
|
|
|
{
|
|
|
|
/* const struct process * const * p = processes;*/
|
|
|
|
printf("Starting");
|
|
|
|
while(*processes != NULL) {
|
|
|
|
printf(" '%s'", (*processes)->name);
|
|
|
|
processes++;
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2010-03-23 13:12:41 +00:00
|
|
|
rtimer_thread_loop(void *data)
|
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
rtimer_arch_check();
|
|
|
|
|
|
|
|
/* Return to COOJA */
|
|
|
|
cooja_mt_yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-12-09 20:24:03 +00:00
|
|
|
static void
|
|
|
|
set_rime_addr(void)
|
|
|
|
{
|
2013-12-12 22:58:52 +00:00
|
|
|
linkaddr_t addr;
|
2012-12-09 20:24:03 +00:00
|
|
|
int i;
|
|
|
|
|
2013-12-12 22:58:52 +00:00
|
|
|
memset(&addr, 0, sizeof(linkaddr_t));
|
2014-12-01 20:02:57 +00:00
|
|
|
#if NETSTACK_CONF_WITH_IPV6
|
2012-12-09 20:24:03 +00:00
|
|
|
for(i = 0; i < sizeof(uip_lladdr.addr); i += 2) {
|
|
|
|
addr.u8[i + 1] = node_id & 0xff;
|
|
|
|
addr.u8[i + 0] = node_id >> 8;
|
|
|
|
}
|
2014-12-01 20:02:57 +00:00
|
|
|
#else /* NETSTACK_CONF_WITH_IPV6 */
|
2012-12-09 20:24:03 +00:00
|
|
|
addr.u8[0] = node_id & 0xff;
|
|
|
|
addr.u8[1] = node_id >> 8;
|
2014-12-01 20:02:57 +00:00
|
|
|
#endif /* NETSTACK_CONF_WITH_IPV6 */
|
2013-12-12 22:58:52 +00:00
|
|
|
linkaddr_set_node_addr(&addr);
|
2012-12-09 20:24:03 +00:00
|
|
|
printf("Rime started with address ");
|
|
|
|
for(i = 0; i < sizeof(addr.u8) - 1; i++) {
|
|
|
|
printf("%d.", addr.u8[i]);
|
|
|
|
}
|
|
|
|
printf("%d\n", addr.u8[i]);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-09-24 12:59:06 +00:00
|
|
|
void
|
|
|
|
contiki_init()
|
2010-03-15 11:02:57 +00:00
|
|
|
{
|
2010-09-24 12:59:06 +00:00
|
|
|
/* Initialize random generator (moved to moteid.c) */
|
2010-03-15 11:02:57 +00:00
|
|
|
|
2010-09-24 12:59:06 +00:00
|
|
|
/* Start process handler */
|
|
|
|
process_init();
|
2010-03-15 11:02:57 +00:00
|
|
|
|
2012-12-09 20:24:03 +00:00
|
|
|
|
2010-09-24 12:59:06 +00:00
|
|
|
/* Start Contiki processes */
|
2012-12-09 20:24:03 +00:00
|
|
|
|
|
|
|
process_start(&etimer_process, NULL);
|
|
|
|
process_start(&sensors_process, NULL);
|
|
|
|
ctimer_init();
|
2010-03-15 11:02:57 +00:00
|
|
|
|
2010-09-24 12:59:06 +00:00
|
|
|
/* Print startup information */
|
|
|
|
printf(CONTIKI_VERSION_STRING " started. ");
|
|
|
|
if(node_id > 0) {
|
|
|
|
printf("Node id is set to %u.\n", node_id);
|
|
|
|
} else {
|
|
|
|
printf("Node id is not set.\n");
|
|
|
|
}
|
2010-03-15 11:02:57 +00:00
|
|
|
|
2012-12-09 20:24:03 +00:00
|
|
|
set_rime_addr();
|
2010-09-24 12:59:06 +00:00
|
|
|
{
|
2012-12-09 20:24:03 +00:00
|
|
|
uint8_t longaddr[8];
|
2015-10-30 10:41:59 +00:00
|
|
|
|
2012-12-09 20:24:03 +00:00
|
|
|
memset(longaddr, 0, sizeof(longaddr));
|
2013-12-12 22:58:52 +00:00
|
|
|
linkaddr_copy((linkaddr_t *)&longaddr, &linkaddr_node_addr);
|
2012-12-09 20:24:03 +00:00
|
|
|
printf("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ",
|
|
|
|
longaddr[0], longaddr[1], longaddr[2], longaddr[3],
|
|
|
|
longaddr[4], longaddr[5], longaddr[6], longaddr[7]);
|
2010-09-24 12:59:06 +00:00
|
|
|
}
|
2010-03-15 11:02:57 +00:00
|
|
|
|
2010-09-24 12:59:06 +00:00
|
|
|
queuebuf_init();
|
2010-03-15 11:02:57 +00:00
|
|
|
|
2010-09-24 12:59:06 +00:00
|
|
|
/* Initialize communication stack */
|
|
|
|
netstack_init();
|
2012-12-09 20:24:03 +00:00
|
|
|
printf("%s/%s/%s, channel check rate %lu Hz\n",
|
2015-10-30 10:41:59 +00:00
|
|
|
NETSTACK_NETWORK.name, NETSTACK_MAC.name, NETSTACK_RDC.name,
|
2012-12-09 20:24:03 +00:00
|
|
|
CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1:
|
|
|
|
NETSTACK_RDC.channel_check_interval()));
|
2010-03-31 09:39:46 +00:00
|
|
|
|
2014-12-01 20:02:57 +00:00
|
|
|
#if NETSTACK_CONF_WITH_IPV4
|
2010-09-24 12:59:06 +00:00
|
|
|
/* IPv4 CONFIGURATION */
|
|
|
|
{
|
|
|
|
uip_ipaddr_t hostaddr, netmask;
|
2010-03-31 09:39:46 +00:00
|
|
|
|
2010-09-24 12:59:06 +00:00
|
|
|
process_start(&tcpip_process, NULL);
|
|
|
|
process_start(&uip_fw_process, NULL);
|
|
|
|
process_start(&slip_process, NULL);
|
2010-03-31 09:39:46 +00:00
|
|
|
|
2010-09-24 12:59:06 +00:00
|
|
|
slip_set_input_callback(set_gateway);
|
2010-03-31 09:39:46 +00:00
|
|
|
|
2010-09-24 12:59:06 +00:00
|
|
|
uip_init();
|
|
|
|
uip_fw_init();
|
2013-12-12 22:58:52 +00:00
|
|
|
uip_ipaddr(&hostaddr, 172,16,linkaddr_node_addr.u8[0],linkaddr_node_addr.u8[1]);
|
2010-09-24 12:59:06 +00:00
|
|
|
uip_ipaddr(&netmask, 255,255,0,0);
|
|
|
|
uip_ipaddr_copy(&meshif.ipaddr, &hostaddr);
|
2010-03-31 09:39:46 +00:00
|
|
|
|
2010-09-24 12:59:06 +00:00
|
|
|
uip_sethostaddr(&hostaddr);
|
|
|
|
uip_setnetmask(&netmask);
|
|
|
|
uip_over_mesh_set_net(&hostaddr, &netmask);
|
|
|
|
uip_over_mesh_set_gateway_netif(&slipif);
|
|
|
|
uip_fw_default(&meshif);
|
|
|
|
uip_over_mesh_init(UIP_OVER_MESH_CHANNEL);
|
2010-03-31 09:39:46 +00:00
|
|
|
|
2010-09-24 12:59:06 +00:00
|
|
|
rs232_set_input(slip_input_byte);
|
|
|
|
printf("IPv4 address: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&hostaddr));
|
|
|
|
}
|
2014-12-01 20:02:57 +00:00
|
|
|
#endif /* NETSTACK_CONF_WITH_IPV4 */
|
2010-03-15 11:02:57 +00:00
|
|
|
|
2014-12-01 20:02:57 +00:00
|
|
|
#if NETSTACK_CONF_WITH_IPV6
|
2010-09-24 12:59:06 +00:00
|
|
|
/* IPv6 CONFIGURATION */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint8_t addr[sizeof(uip_lladdr.addr)];
|
2012-12-09 20:24:03 +00:00
|
|
|
for(i = 0; i < sizeof(uip_lladdr.addr); i += 2) {
|
|
|
|
addr[i + 1] = node_id & 0xff;
|
|
|
|
addr[i + 0] = node_id >> 8;
|
2010-09-24 12:59:06 +00:00
|
|
|
}
|
2014-11-03 21:56:20 +00:00
|
|
|
linkaddr_copy((linkaddr_t *)addr, &linkaddr_node_addr);
|
2010-09-24 12:59:06 +00:00
|
|
|
memcpy(&uip_lladdr.addr, addr, sizeof(uip_lladdr.addr));
|
2012-12-09 20:24:03 +00:00
|
|
|
|
2010-09-24 12:59:06 +00:00
|
|
|
process_start(&tcpip_process, NULL);
|
|
|
|
|
|
|
|
printf("Tentative link-local IPv6 address ");
|
2010-03-15 11:02:57 +00:00
|
|
|
{
|
2012-12-09 20:24:03 +00:00
|
|
|
uip_ds6_addr_t *lladdr;
|
|
|
|
int i;
|
|
|
|
lladdr = uip_ds6_get_link_local(-1);
|
2010-09-24 12:59:06 +00:00
|
|
|
for(i = 0; i < 7; ++i) {
|
2015-10-30 10:41:59 +00:00
|
|
|
printf("%02x%02x:", lladdr->ipaddr.u8[i * 2],
|
|
|
|
lladdr->ipaddr.u8[i * 2 + 1]);
|
2010-03-29 11:52:08 +00:00
|
|
|
}
|
2012-12-09 20:24:03 +00:00
|
|
|
printf("%02x%02x\n", lladdr->ipaddr.u8[14],
|
2015-10-30 10:41:59 +00:00
|
|
|
lladdr->ipaddr.u8[15]);
|
2010-09-24 12:59:06 +00:00
|
|
|
}
|
2010-03-29 11:52:08 +00:00
|
|
|
|
2010-09-24 12:59:06 +00:00
|
|
|
if(1) {
|
|
|
|
uip_ipaddr_t ipaddr;
|
|
|
|
int i;
|
2015-09-30 14:06:58 +00:00
|
|
|
uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0);
|
2010-09-24 12:59:06 +00:00
|
|
|
uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
|
|
|
|
uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);
|
|
|
|
printf("Tentative global IPv6 address ");
|
|
|
|
for(i = 0; i < 7; ++i) {
|
|
|
|
printf("%02x%02x:",
|
|
|
|
ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]);
|
2010-03-29 11:52:08 +00:00
|
|
|
}
|
2010-09-24 12:59:06 +00:00
|
|
|
printf("%02x%02x\n",
|
|
|
|
ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]);
|
2010-03-15 11:02:57 +00:00
|
|
|
}
|
2010-09-24 12:59:06 +00:00
|
|
|
}
|
2014-12-01 20:02:57 +00:00
|
|
|
#endif /* NETSTACK_CONF_WITH_IPV6 */
|
2010-03-15 11:02:57 +00:00
|
|
|
|
2014-05-08 20:29:10 +00:00
|
|
|
/* Initialize eeprom */
|
|
|
|
eeprom_init();
|
2015-10-30 10:41:59 +00:00
|
|
|
|
2010-09-24 12:59:06 +00:00
|
|
|
/* Start serial process */
|
|
|
|
serial_line_init();
|
|
|
|
|
|
|
|
/* Start autostart processes (defined in Contiki application) */
|
|
|
|
print_processes(autostart_processes);
|
|
|
|
autostart_start(autostart_processes);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
process_run_thread_loop(void *data)
|
|
|
|
{
|
|
|
|
/* Yield once during bootup */
|
|
|
|
simProcessRunValue = 1;
|
|
|
|
cooja_mt_yield();
|
2010-03-15 11:02:57 +00:00
|
|
|
|
2010-09-24 12:59:06 +00:00
|
|
|
contiki_init();
|
2010-03-15 11:02:57 +00:00
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
simProcessRunValue = process_run();
|
|
|
|
while(simProcessRunValue-- > 0) {
|
|
|
|
process_run();
|
|
|
|
}
|
|
|
|
simProcessRunValue = process_nevents();
|
|
|
|
|
|
|
|
/* Check if we must stay awake */
|
|
|
|
if(simDontFallAsleep) {
|
|
|
|
simDontFallAsleep=0;
|
|
|
|
simProcessRunValue = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return to COOJA */
|
|
|
|
cooja_mt_yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \brief Initialize a mote by starting processes etc.
|
2015-10-30 07:55:11 +00:00
|
|
|
* \param env JNI Environment interface pointer
|
|
|
|
* \param obj unused
|
2010-03-15 11:02:57 +00:00
|
|
|
*
|
|
|
|
* This function initializes a mote by starting certain
|
|
|
|
* processes and setting up the environment.
|
|
|
|
*
|
|
|
|
* This is a JNI function and should only be called via the
|
|
|
|
* responsible Java part (MoteType.java).
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL
|
2013-11-19 11:17:37 +00:00
|
|
|
Java_org_contikios_cooja_corecomm_CLASSNAME_init(JNIEnv *env, jobject obj)
|
2010-03-15 11:02:57 +00:00
|
|
|
{
|
2010-03-23 13:12:41 +00:00
|
|
|
/* Create rtimers and Contiki threads */
|
|
|
|
cooja_mt_start(&rtimer_thread, &rtimer_thread_loop, NULL);
|
|
|
|
cooja_mt_start(&process_run_thread, &process_run_thread_loop, NULL);
|
2010-03-15 11:02:57 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \brief Get a segment from the process memory.
|
2015-10-30 07:55:11 +00:00
|
|
|
* \param env JNI Environment interface pointer
|
|
|
|
* \param obj unused
|
|
|
|
* \param rel_addr Start address of segment
|
|
|
|
* \param length Size of memory segment
|
|
|
|
* \param mem_arr Byte array destination for the fetched memory segment
|
2010-03-15 11:02:57 +00:00
|
|
|
* \return Java byte array containing a copy of memory segment.
|
|
|
|
*
|
|
|
|
* Fetches a memory segment from the process memory starting at
|
2015-10-30 07:55:11 +00:00
|
|
|
* (rel_addr), with size (length). This function does not perform
|
2010-03-15 11:02:57 +00:00
|
|
|
* ANY error checking, and the process may crash if addresses are
|
|
|
|
* not available/readable.
|
|
|
|
*
|
|
|
|
* This is a JNI function and should only be called via the
|
|
|
|
* responsible Java part (MoteType.java).
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL
|
2013-11-19 11:17:37 +00:00
|
|
|
Java_org_contikios_cooja_corecomm_CLASSNAME_getMemory(JNIEnv *env, jobject obj, jint rel_addr, jint length, jbyteArray mem_arr)
|
2010-03-15 11:02:57 +00:00
|
|
|
{
|
|
|
|
(*env)->SetByteArrayRegion(
|
|
|
|
env,
|
|
|
|
mem_arr,
|
|
|
|
0,
|
|
|
|
(size_t) length,
|
|
|
|
(jbyte *) (((long)rel_addr) + referenceVar)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \brief Replace a segment of the process memory with given byte array.
|
2015-10-30 07:55:11 +00:00
|
|
|
* \param env JNI Environment interface pointer
|
|
|
|
* \param obj unused
|
|
|
|
* \param rel_addr Start address of segment
|
|
|
|
* \param length Size of memory segment
|
|
|
|
* \param mem_arr Byte array contaning new memory
|
2010-03-15 11:02:57 +00:00
|
|
|
*
|
|
|
|
* Replaces a process memory segment with given byte array.
|
|
|
|
* This function does not perform ANY error checking, and the
|
|
|
|
* process may crash if addresses are not available/writable.
|
|
|
|
*
|
|
|
|
* This is a JNI function and should only be called via the
|
|
|
|
* responsible Java part (MoteType.java).
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL
|
2013-11-19 11:17:37 +00:00
|
|
|
Java_org_contikios_cooja_corecomm_CLASSNAME_setMemory(JNIEnv *env, jobject obj, jint rel_addr, jint length, jbyteArray mem_arr)
|
2010-03-15 11:02:57 +00:00
|
|
|
{
|
|
|
|
jbyte *mem = (*env)->GetByteArrayElements(env, mem_arr, 0);
|
2015-10-30 10:41:59 +00:00
|
|
|
memcpy((char*) (((long)rel_addr) + referenceVar),
|
|
|
|
mem,
|
|
|
|
length);
|
2010-03-15 11:02:57 +00:00
|
|
|
(*env)->ReleaseByteArrayElements(env, mem_arr, mem, 0);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \brief Let mote execute one "block" of code (tick mote).
|
2015-10-30 07:55:11 +00:00
|
|
|
* \param env JNI Environment interface pointer
|
|
|
|
* \param obj unused
|
2010-03-15 11:02:57 +00:00
|
|
|
*
|
|
|
|
* Let mote defined by the active contiki processes and current
|
|
|
|
* process memory execute some program code. This code must not block
|
|
|
|
* or else this function will never return. A typical contiki
|
|
|
|
* process will return when it executes PROCESS_WAIT..() statements.
|
|
|
|
*
|
|
|
|
* Before the control is left to contiki processes, any messages
|
|
|
|
* from the Java part are handled. These may for example be
|
|
|
|
* incoming network data. After the contiki processes return control,
|
|
|
|
* messages to the Java part are also handled (those which may need
|
|
|
|
* special attention).
|
|
|
|
*
|
|
|
|
* This is a JNI function and should only be called via the
|
|
|
|
* responsible Java part (MoteType.java).
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL
|
2013-11-19 11:17:37 +00:00
|
|
|
Java_org_contikios_cooja_corecomm_CLASSNAME_tick(JNIEnv *env, jobject obj)
|
2010-03-15 11:02:57 +00:00
|
|
|
{
|
2010-03-23 13:12:41 +00:00
|
|
|
simProcessRunValue = 0;
|
|
|
|
|
2010-03-15 11:02:57 +00:00
|
|
|
/* Let all simulation interfaces act first */
|
|
|
|
doActionsBeforeTick();
|
|
|
|
|
|
|
|
/* Poll etimer process */
|
2015-10-30 10:41:59 +00:00
|
|
|
if(etimer_pending()) {
|
2010-03-15 11:02:57 +00:00
|
|
|
etimer_request_poll();
|
|
|
|
}
|
|
|
|
|
2010-03-23 13:12:41 +00:00
|
|
|
/* Let rtimers run.
|
|
|
|
* Sets simProcessRunValue */
|
|
|
|
cooja_mt_exec(&rtimer_thread);
|
|
|
|
|
|
|
|
if(simProcessRunValue == 0) {
|
|
|
|
/* Rtimers done: Let Contiki handle a few events.
|
|
|
|
* Sets simProcessRunValue */
|
|
|
|
cooja_mt_exec(&process_run_thread);
|
|
|
|
}
|
2010-03-15 11:02:57 +00:00
|
|
|
|
|
|
|
/* Let all simulation interfaces act before returning to java */
|
|
|
|
doActionsAfterTick();
|
|
|
|
|
2010-03-23 13:12:41 +00:00
|
|
|
/* Do we have any pending timers */
|
2016-09-06 13:14:10 +00:00
|
|
|
simEtimerPending = etimer_pending();
|
2010-03-15 11:02:57 +00:00
|
|
|
|
2010-03-25 09:12:03 +00:00
|
|
|
/* Save nearest expiration time */
|
2016-09-06 13:14:10 +00:00
|
|
|
simEtimerNextExpirationTime = etimer_next_expiration_time();
|
|
|
|
|
2010-03-15 11:02:57 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \brief Set the relative memory address of the reference variable.
|
2015-10-30 07:55:11 +00:00
|
|
|
* \param env JNI Environment interface pointer
|
|
|
|
* \param obj unused
|
|
|
|
* \param addr Relative memory address
|
2010-03-15 11:02:57 +00:00
|
|
|
*
|
|
|
|
* This is a JNI function and should only be called via the
|
|
|
|
* responsible Java part (MoteType.java).
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL
|
2013-11-19 11:17:37 +00:00
|
|
|
Java_org_contikios_cooja_corecomm_CLASSNAME_setReferenceAddress(JNIEnv *env, jobject obj, jint addr)
|
2010-03-15 11:02:57 +00:00
|
|
|
{
|
|
|
|
referenceVar = (((long)&referenceVar) - ((long)addr));
|
|
|
|
}
|