Esempio n. 1
0
void zmq_socket_init(zmq_socket_t *self, zmq_socket_type_t socket_type) {
    zmtp_channel_init(&self->channel, socket_type, PROCESS_CURRENT(), PROCESS_CURRENT());

    switch(socket_type) {
      case ZMQ_ROUTER:
        zmq_router_init(self);
        break;
      case ZMQ_DEALER:
        zmq_dealer_init(self);
        break;
      case ZMQ_PUB:
        zmq_pub_init(self);
        break;
      case ZMQ_SUB:
        zmq_sub_init(self);
        break;
      case ZMQ_PUSH:
        zmq_push_init(self);
        break;
      case ZMQ_PULL:
        zmq_pull_init(self);
        break;
      default:
        printf("ERROR: Socket type not supported yet\r\n");
    }
}
Esempio n. 2
0
/*---------------------------------------------------------------------------*/
static void
add_timer(struct etimer *timer)
{
  struct etimer *t;

  etimer_request_poll();

  if(timer->p != PROCESS_NONE) {
    for(t = timerlist; t != NULL; t = t->next) {
      if(t == timer) {
	/* Timer already on list, bail out. */
        timer->p = PROCESS_CURRENT();
	update_time();
	return;
      }
    }
  }

  /* Timer not on list. */
  timer->p = PROCESS_CURRENT();
  timer->next = timerlist;
  timerlist = timer;

  update_time();
}
Esempio n. 3
0
/*---------------------------------------------------------------------------*/
static void
add_timer(struct etimer *timer) {
    struct etimer *t;
    struct process *p;

    etimer_request_poll();

    if (timer->p != PROCESS_NONE) {
        for (t = timerlist; t != NULL; t = t->next) {
            if (t == timer) {
                /* Timer already on list, bail out. */
                p = PROCESS_CURRENT();
                memcpy(&(timer->p), &(p), sizeof (struct process*));
                update_time();
                return;
            }
        }
    }

    /* Timer not on list. */

    p = PROCESS_CURRENT();
    memcpy(&(timer->p), &(p), sizeof (struct process*));
    timer->next = timerlist;
    timerlist = timer;

    update_time();
}
layer_state_t contiki_io_layer_data_ready(
      layer_connectivity_t* context
    , const void* data
    , const layer_hint_t hint)
{
	contiki_data_t* s = (contiki_data_t*) context->self->user_data;
	const const_data_descriptor_t* buffer   = ( const const_data_descriptor_t* ) data;

	XI_UNUSED(hint);

	s->process = PROCESS_CURRENT();

	if (s->state == WRITTING) {
		return LAYER_STATE_WANT_WRITE;
	} else if (s->state == WRITE_END) {
		s->state = CONNECTED;
		return LAYER_STATE_OK;
	} else if (s->state == ERROR) {
		return LAYER_STATE_ERROR;
	} else if (s->state == CONNECTED) {
		if (buffer != NULL  && buffer->data_size > 0) {
			s->out_buf = (uint8_t *)buffer->data_ptr;
 			s->out_len = buffer->data_size;
 			//s->out_buf[s->out_len] = 0;
 			//xprintf("%s\n", s->out_buf);
 			s->state = WRITTING;
			handle_output(s);
			return LAYER_STATE_WANT_WRITE;
		}
		return LAYER_STATE_OK;
	}
	return LAYER_STATE_ERROR;
}
Esempio n. 5
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(tcpip_process, ev, data)
{
  PROCESS_BEGIN();
  
#if UIP_TCP
 {
   static unsigned char i;
   
   for(i = 0; i < UIP_LISTENPORTS; ++i) {
     s.listenports[i].port = 0;
   }
   s.p = PROCESS_CURRENT();
 }
#endif

  tcpip_event = process_alloc_event();
  etimer_set(&periodic, CLOCK_SECOND/2);

  uip_init();
  
  while(1) {
    PROCESS_YIELD();
    eventhandler(ev, data);
  }
  
  PROCESS_END();
}
void
tcpip_init()
{

 #if UIP_TCP
 {
   static unsigned char i;
   
   for(i = 0; i < UIP_LISTENPORTS; ++i) {
     s.listenports[i].port = 0;
   }
   s.p = PROCESS_CURRENT();
 }
#endif
  
  //tcpip_event = process_alloc_event();
//  etimer_set(&periodic, tcpip_process, CLOCK_SECOND/2);
  etimer_set(&periodic, CLOCK_SECOND/2);

  uip_init();

/* initialize RPL if configured for using RPL */
#if UIP_CONF_IPV6_RPL
  rpl_init();
#endif /* UIP_CONF_IPV6_RPL */
}
Esempio n. 7
0
/*---------------------------------------------------------------------------*/
char
ctk_filedialog_eventhandler(struct ctk_filedialog_state *s,
			    process_event_t ev, process_data_t data)
{
  static struct cfs_dirent dirent;
  
  if(state == STATE_OPEN) {
    if(ev == ctk_signal_widget_activate &&
       data == (process_data_t)&button) {
      ctk_dialog_close();
      state = STATE_CLOSED;
      process_post(PROCESS_CURRENT(), s->ev, &filename);
      return 1;
    } else if(ev == PROCESS_EVENT_CONTINUE &&
	      (process_data_t)s == data) {
      if(cfs_readdir(&dir, &dirent) == 0 &&
	 dirfileptr < MAX_NUMFILES) {
	strncpy(&files[dirfileptr * FILES_WIDTH],
		dirent.name, FILES_WIDTH);
	CTK_WIDGET_REDRAW(&fileslabel);
	++dirfileptr;
	process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, s);
      } else {
	fileptr = 0;	
	cfs_closedir(&dir);
      }
      return 1; 
    } else if(ev == ctk_signal_keypress) {
      if((char)(size_t)data == CH_CURS_UP) {
	clearptr();
	if(fileptr > 0) {
	  --fileptr;
	}
	showptr();
	return 1;
      } else if((char)(size_t)data == CH_CURS_DOWN) {
	clearptr();
	if(fileptr < FILES_HEIGHT - 1) {
	  ++fileptr;
	}
	showptr();
	return 1;
      }
    }
  }
  return 0;
}
Esempio n. 8
0
/*{ if "tc_condition_wait" in all_syscalls }*/
void tc_condition_wait(ec_tframe_tc_condition_wait_t* frame) {
    thread->ctx.tc_condition_wait.frame = frame;
    thread->syscall = SYSCALL_tc_condition_wait;

    OBS("condition_wait: cond.waiting=%d", frame->cond->waiting);
    frame->cond->waiting_process = PROCESS_CURRENT();
    frame->cond->waiting = true;
}
Esempio n. 9
0
void
tcp_attach(struct uip_conn *conn,
	   void *appstate)
{
  uip_tcp_appstate_t *s;
  s = &conn->appstate;
  s->p = PROCESS_CURRENT();
  s->state = appstate;
}
Esempio n. 10
0
uint8_t
icmp6_new(void *appstate) {
    if(uip_icmp6_conns.appstate.p == PROCESS_NONE) {
        uip_icmp6_conns.appstate.p = PROCESS_CURRENT();
        uip_icmp6_conns.appstate.state = appstate;
        return 0;
    }
    return 1;
}
Esempio n. 11
0
void
udp_attach(struct uip_udp_conn *conn,
	   void *appstate)
{
  register uip_udp_appstate_t *s;

  s = &conn->appstate;
  s->p = PROCESS_CURRENT();
  s->state = appstate;
}
Esempio n. 12
0
/*{ if "tc_sleep" in all_syscalls }*/
void tc_sleep(ec_tframe_tc_sleep_t* frame) {
    thread->ctx.tc_sleep.frame = frame;
    thread->syscall = SYSCALL_tc_sleep;

    if (frame->cond) {
        OBS("sleep: tics=%ld, cond.waiting=%d", frame->tics, frame->cond->waiting);
        frame->cond->waiting = true;
        frame->cond->waiting_process = PROCESS_CURRENT();
    } else {
        OBS("sleep: tics=%ld", frame->tics);
    }

    clock_time_t now = clock_time();
    if (frame->tics > now) {
        etimer_set(&thread->ctx.tc_sleep.et, frame->tics - now);
    } else {
        process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, &tc_sleep);
    }
}
/*---------------------------------------------------------------------------*/
void
interrupt_register(int int_vect)
{
	int i = 0;
	for (; i < INTERRUPT_NUM; i++) {
		if (interrupts[i].int_vect == int_vect) {
			interrupts[i].p = PROCESS_CURRENT();
			break;
		}
	}
}
layer_state_t contiki_io_layer_connect(
	layer_connectivity_t* context
	, const void* data
	, const layer_hint_t hint )
{

 	XI_UNUSED( hint );

	uip_ipaddr_t *ip;
	resolv_status_t dns_status;
    xi_connection_data_t* connection_data;
    layer_t* layer = ( layer_t* ) context->self;
    contiki_data_t* s = (contiki_data_t* ) layer->user_data;
    struct uip_conn *c;

    s->process = PROCESS_CURRENT();
    if (s->state == CONNECTED) {
    	xi_debug_logger( "Connecting to the endpoint [ok]" );
    	return LAYER_STATE_OK;
    } else if (s->state == CONNECTING) {
    	return LAYER_STATE_WANT_WRITE;
    } else if (s->state == CLOSED) {
	    connection_data   = ( xi_connection_data_t* ) data;
	    dns_status = resolv_lookup(connection_data->address, &ip);

	    if (dns_status != RESOLV_STATUS_CACHED) {
	    	if (dns_status ==  RESOLV_STATUS_NOT_FOUND || dns_status == RESOLV_STATUS_ERROR) {
	    		xi_debug_logger( "Getting Host by name [failed]" );
	        	xi_set_err( XI_SOCKET_GETHOSTBYNAME_ERROR );
	        	return LAYER_STATE_ERROR;
	    	}
	    	if (dns_status != RESOLV_STATUS_RESOLVING) {
	    		resolv_query(connection_data->address);
	    	}
	    	return LAYER_STATE_WANT_WRITE;	/* no IP, cannot go further */
	   	}
	   	xi_debug_logger( "Getting Host by name [ok]" );
	    xi_debug_logger( "Connecting to the endpoint..." );

	    c = uip_connect(ip, uip_htons(connection_data->port));
	 	if(c == NULL) {
	        xi_debug_logger( "Connecting to the endpoint [failed]" );
	        xi_set_err( XI_SOCKET_CONNECTION_ERROR );
	        return LAYER_STATE_ERROR;
	    }
	    s->state = CONNECTING;
	    c->appstate.p = &xively_process;
	    c->appstate.state = s;
	    tcpip_poll_tcp(c);
		return LAYER_STATE_WANT_WRITE;
	}

	return LAYER_STATE_ERROR;
}
Esempio n. 15
0
/*---------------------------------------------------------------------------*/
int
process_post(struct process *p, process_event_t ev, process_data_t data)
{
  static process_num_events_t snum;

  WDTE=0xac;
  
  if(PROCESS_CURRENT() == NULL) {
    printf("process_post: NULL process posts event %d to process '%s', nevents %d\n",
	   ev,PROCESS_NAME_STRING(p), nevents);
  } else {
    printf("process_post: Process '%s' posts event %d to process '%s', nevents %d\n",
	   PROCESS_NAME_STRING(PROCESS_CURRENT()), ev,
	   p == PROCESS_BROADCAST? "<broadcast>": PROCESS_NAME_STRING(p), nevents);
  }
  
  if(nevents == PROCESS_CONF_NUMEVENTS) {
#if DEBUG
    if(p == PROCESS_BROADCAST) {
      printf("soft panic: event queue is full when broadcast event %d was posted from %s\n", ev, PROCESS_NAME_STRING(process_current));
    } else {
      printf("soft panic: event queue is full when event %d was posted to %s frpm %s\n", ev, PROCESS_NAME_STRING(p), PROCESS_NAME_STRING(process_current));
    }
#endif /* DEBUG */
    return PROCESS_ERR_FULL;
  }
  
  snum = (process_num_events_t)(fevent + nevents) % PROCESS_CONF_NUMEVENTS;
  events[snum].ev = ev;
  events[snum].data = data;
  events[snum].p = p;
  ++nevents;

#if PROCESS_CONF_STATS
  if(nevents > process_maxevents) {
    process_maxevents = nevents;
  }
#endif /* PROCESS_CONF_STATS */
  
  return PROCESS_ERR_OK;
}
Esempio n. 16
0
/*{ if "tc_receive" in all_syscalls }*/
void tc_receive(ec_tframe_tc_receive_t* frame) {
    thread->ctx.tc_receive.frame = frame;
    thread->syscall = SYSCALL_tc_receive;

    if (frame->cond) {
        OBS("receive: cond.waiting=%d", frame->cond->waiting);
        frame->cond->waiting = true;
        frame->cond->waiting_process = PROCESS_CURRENT();
    } else {
        OBS("receive: %s", "-");
    }
}
Esempio n. 17
0
/*-----------------------------------------------------------------------------------*/
void
dhcpc_configured(const struct dhcpc_state *s)
{
  uip_sethostaddr(&s->ipaddr);
  uip_setnetmask(&s->netmask);
  uip_setdraddr(&s->default_router);
#if WITH_DNS
  resolv_conf(&s->dnsaddr);
#endif /* WITH_DNS */

  set_statustext("Configured.");
  process_post(PROCESS_CURRENT(), PROCESS_EVENT_MSG, NULL);
}
Esempio n. 18
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(serial_line_process, ev, data)
{
  static char buf[BUFSIZE];
  static int ptr;

  PROCESS_BEGIN();

  serial_line_event_message = process_alloc_event();


  ptr = 0;

  while(1) {
    /* Fill application buffer until newline or empty */
    
    printf("Inside serial_line_process : serial_line_event_message = %d\n", serial_line_event_message);
  
    int c = ringbuf_get(&rxbuf);
    
    if(c == -1) {
      printf("rxbuf empty, process yeilding\n") ;
      /* Buffer empty, wait for poll */
      PROCESS_YIELD();
    } else {
      if(c != END) {
        if(ptr < BUFSIZE-1) {
          buf[ptr++] = (uint8_t)c;
        } else {
          /* Ignore character (wait for EOL) */
        }
      } else {
        /* Terminate */
        buf[ptr++] = (uint8_t)'\0';

        /* Broadcast event */
   
       printf("Broadcast event serial_line_event_message\n");
       process_post(PROCESS_BROADCAST, serial_line_event_message, buf);

        /* Wait until all processes have handled the serial line event */
        if(PROCESS_ERR_OK ==
          process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, NULL)) {
          PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_CONTINUE);
        }
        ptr = 0;
      }
    }
  }

  PROCESS_END();
}
Esempio n. 19
0
// Connect to the specified server
int mqtt_connect(uip_ip6addr_t* address, uint16_t port, int auto_reconnect, mqtt_connect_info_t* info)
{
  if(process_is_running(&mqtt_process))
    return -1;

  mqtt_state.address = *address;
  mqtt_state.port = port;
  mqtt_state.auto_reconnect = auto_reconnect;
  mqtt_state.connect_info = info;
  mqtt_state.calling_process = PROCESS_CURRENT();
  process_start(&mqtt_process, (const char*)&mqtt_state);

  return 0;
}
Esempio n. 20
0
struct uip_conn *
tcp_connect(uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
{
  struct uip_conn *c;

  c = uip_connect(ripaddr, port);
  if(c == NULL) {
    return NULL;
  }
  c->appstate.p = PROCESS_CURRENT();
  c->appstate.state = appstate;

  tcpip_poll_tcp(c);
  return c;
}
Esempio n. 21
0
/*-----------------------------------------------------------------------------------*/
void
dhcpc_unconfigured(const struct dhcpc_state *s)
{
  static uip_ipaddr_t nulladdr;

  uip_sethostaddr(&nulladdr);
  uip_setnetmask(&nulladdr);
  uip_setdraddr(&nulladdr);
#if WITH_DNS
  resolv_conf(&nulladdr);
#endif /* WITH_DNS */

  set_statustext("Unconfigured.");
  process_post(PROCESS_CURRENT(), PROCESS_EVENT_MSG, NULL);
}
Esempio n. 22
0
/*---------------------------------------------------------------------------*/
struct uip_udp_conn *
udp_new(const uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
{
  struct uip_udp_conn *c;
  uip_udp_appstate_t *s;

  c = uip_udp_new(ripaddr, port);
  if(c == NULL) {
    return NULL;
  }
  s = &c->appstate;
  s->p = PROCESS_CURRENT();
  s->state = appstate;
  return c;
}
Esempio n. 23
0
/*--------------------------------------------------------------------------*/
PROCESS_THREAD(adc_reading_process, ev, data)
{
  PROCESS_POLLHANDLER(adc_pollhandler());
  PROCESS_BEGIN();

  leds_off(LEDS_ALL);

  while(1) {
    /* read analog in A7, store the result in adcval and poll this process when done */
    adc_get_poll(A7, &adcval, PROCESS_CURRENT());
    etimer_set(&et, CLOCK_SECOND/8);
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
  }
  PROCESS_END();
}
layer_state_t contiki_io_layer_on_close( layer_connectivity_t* context )
{
	contiki_data_t* s = ( contiki_data_t* ) context->self->user_data;

	s->process = PROCESS_CURRENT();
	if (s->state == CLOSED) {
		return LAYER_STATE_OK;
	}
	if (s->state == CLOSING) {
		return LAYER_STATE_WANT_WRITE;
	} else if (s->state == CONNECTED) {
		s->state = CLOSING;
		return LAYER_STATE_WANT_WRITE;
	}
	return LAYER_STATE_ERROR;
}
Esempio n. 25
0
/*---------------------------------------------------------------------------*/
void
tcp_listen(uint16_t port)
{
    static unsigned char i;
    struct listenport *l;

    l = s.listenports;
    for(i = 0; i < UIP_LISTENPORTS; ++i) {
        if(l->port == 0) {
            l->port = port;
            l->p = PROCESS_CURRENT();
            uip_listen(port);
            break;
        }
        ++l;
    }
}
Esempio n. 26
0
/*---------------------------------------------------------------------------*/
coap_context_t *
coap_context_new(uip_ipaddr_t *my_addr, uint16_t port)
{
  coap_context_t *ctx = NULL;
  int i;
  for(i = 0; i < MAX_CONTEXTS; i++) {
    if(!coap_contexts[i].is_used) {
      ctx = &coap_contexts[i];
      break;
    }
  }

  if(ctx == NULL) {
    PRINTF("coap-context: no free contexts\n");
    return NULL;
  }

  memset(ctx, 0, sizeof(coap_context_t));

  /* initialize context */
  ctx->dtls_context = dtls_new_context(ctx);
  if(ctx->dtls_context == NULL) {
    PRINTF("coap-context: failed to get DTLS context\n");
    uip_udp_remove(uip_udp_conn(ctx->buf));
    return NULL;
  }

  ctx->dtls_handler.write = send_to_peer;
  ctx->dtls_handler.read = get_from_peer;
  ctx->dtls_handler.event = event;

  dtls_set_handler(ctx->dtls_context, &ctx->dtls_handler);

#ifdef NETSTACK_CONF_WITH_IPV6
  memcpy(&ctx->my_addr.in6_addr, my_addr, sizeof(ctx->my_addr.in6_addr));
#else
  memcpy(&ctx->my_addr.in_addr, my_addr, sizeof(ctx->my_addr.in_addr));
#endif
  ctx->my_port = port;

  ctx->process = PROCESS_CURRENT();
  ctx->is_used = 1;
  PRINTF("Secure listening on port %u\n", port);

  return ctx;
}
Esempio n. 27
0
/*---------------------------------------------------------------------------*/
void
tcp_unlisten(uint16_t port)
{
  unsigned char i;
  struct listenport *l;

  l = s.listenports;
  for(i = 0; i < UIP_LISTENPORTS; ++i) {
    if(l->port == port &&
       l->p == PROCESS_CURRENT()) {
      l->port = 0;
      uip_unlisten(port);
      break;
    }
    ++l;
  }
}
Esempio n. 28
0
/*---------------------------------------------------------------------------*/
void
ctimer_set(struct ctimer *c, clock_time_t t,
	   void (*f)(void *), void *ptr)
{
  PRINTF("ctimer_set %p %u\n", c, (unsigned)t);
  c->p = PROCESS_CURRENT();
  c->f = f;
  c->ptr = ptr;
  if(initialized) {
    PROCESS_CONTEXT_BEGIN(&ctimer_process);
    etimer_set(&c->etimer, t);
    PROCESS_CONTEXT_END(&ctimer_process);
  } else {
    c->etimer.tmr.interval = t;
  }

  list_remove(ctimer_list, c);
  list_add(ctimer_list, c);
}
Esempio n. 29
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(udtn_demo_process, ev, data)
{
	static struct registration_api reg;
	struct mmem * outgoing_bundle_memory = NULL;
	uint8_t payload[255];
	uint32_t tmp;

	PROCESS_BEGIN();

	/* Wait for the agent to be initialized */
	PROCESS_PAUSE();

	/* Register our endpoint to receive bundles */
	reg.status = APP_ACTIVE;
	reg.application_process = PROCESS_CURRENT();
	reg.app_id = 25;
	process_post(&agent_process, dtn_application_registration_event,&reg);

	/* Allocate memory for the outgoing bundle */
	outgoing_bundle_memory = bundle_create_bundle();
	if( outgoing_bundle_memory == NULL ) {
		printf("No memory to send bundle\n");
		return -1;
	}

	/* Get the bundle flags */
	bundle_get_attr(outgoing_bundle_memory, FLAGS, &tmp);

	/* Set the bundle flags to singleton */
	tmp = BUNDLE_FLAG_SINGLETON;
	bundle_set_attr(outgoing_bundle_memory, FLAGS, &tmp);

	/* Add the payload block */
	bundle_add_block(outgoing_bundle_memory, BUNDLE_BLOCK_TYPE_PAYLOAD, BUNDLE_BLOCK_FLAG_NULL, payload, 255);

	/* To send the bundle, we send an event to the agent */
	process_post(&agent_process, dtn_send_bundle_event, (void *) outgoing_bundle_memory);

	TEST_PASS();

	PROCESS_END();
}
layer_state_t contiki_io_layer_on_data_ready(
      layer_connectivity_t* context
    , const void* data
    , const layer_hint_t hint)
{
	contiki_data_t* s = (contiki_data_t*) context->self->user_data;
	data_descriptor_t* buffer  = 0;
	layer_state_t state;

	XI_UNUSED(hint);

	s->process = PROCESS_CURRENT();

	if (data) {
		buffer  = ( data_descriptor_t* ) data;
	} else {
        buffer = &s->buffer_descriptor;
	}

	if (s->state == READ_END) {
		buffer->data_ptr = (char *)uip_appdata;
		buffer->real_size = uip_datalen();
		buffer->data_size = uip_datalen() + 1;
    	buffer->data_ptr[buffer->real_size] = '\0'; // put guard
    	buffer->curr_pos = 0;
	 	state = CALL_ON_NEXT_ON_DATA_READY( context->self, ( void* ) buffer, LAYER_HINT_MORE_DATA );

	 	if (state == LAYER_STATE_WANT_READ) {
	 		s->state = READING;
	 		return LAYER_STATE_WANT_READ;
	 	}
	 	s->state = CONNECTED;
	 	return LAYER_STATE_OK;
	 } else if (s->state == READING) {
	 	return LAYER_STATE_WANT_READ;
	 } else if (s->state == CONNECTED) {
	 	s->state = READING;
		return LAYER_STATE_WANT_READ;
	 }

	 return LAYER_STATE_ERROR;	/* error, come here */
}