main.c 6.71 KB
/*
 * Copyright (C) 2015 Freie Universität Berlin
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License v2.1. See the file LICENSE in the top level
 * directory for more details.
 */

/**
 * @ingroup     examples
 * @{
 *
 * @file
 * @brief       Example application for demonstrating the RIOT network stack
 *
 * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
 *
 * @}
 */

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <arpa/inet.h>

#include "board.h"
#include "periph_conf.h"
#include "periph/gpio.h"
#include "net/sock/udp.h"
#include "net/gnrc/ipv6.h"
#include "net/af.h"
#include "net/sixlowpan.h"
#include "net/gnrc/pktdump.h"
#include "shell.h"
#include "shell_commands.h"
#include "msg.h"
#include "thread.h"
#include "sched.h"
#include "kernel_types.h"
#include "net/netstats.h"
#include "net/ipv6/addr.h"
#include "periph/timer.h"
#include "net/gnrc/ipv6/netif.h"
#include "net/gnrc/netif.h"
#include "net/gnrc/netapi.h"
#include "net/netopt.h"
#include "net/gnrc/pkt.h"
#include "net/gnrc/pktbuf.h"
#include "net/gnrc/netif/hdr.h"
#include "net/gnrc/sixlowpan/netif.h"
#include "net/fib.h"
#include "net/gnrc/udp.h"
#include "periph/pwm.h"
#include "od.h"
#include "xtimer.h"
#include "robotcar.h"
#include "net/gnrc/rpl.h"
#include "net/gnrc/rpl/structs.h"
#include "net/gnrc/rpl/dodag.h"
#include "timex.h" /* for SEC_IN_USEC */


#define MAIN_QUEUE_SIZE	(8)
#define MAX_ADDR_LEN	(8U)

char sock_server_stack[THREAD_STACKSIZE_MAIN];
char sock_client_stack[THREAD_STACKSIZE_MAIN];
char robot_stack[THREAD_STACKSIZE_MAIN];


extern kernel_pid_t server, client, robot_pid;
extern robot_t motor_1, motor_2;

sock_udp_ep_t local = SOCK_IPV6_EP_ANY;
sock_udp_t sock;

#define SERVER_BUFFER_SIZE (10)        /* max size of the buffer where incoming data is stored  */
#define MAX_MESSAGE_LENGTH (10)

extern void send_to_robot_manu(uint32_t msg);
extern int init_robot(robot_t *dev1, robot_t *dev2);
extern void send_to_robot_auto(void);


/***************** RPL functions *****************/
int crea_rpl_dodag_root(uint8_t instance_id, ipv6_addr_t dodag_id)
{
  gnrc_rpl_instance_t *inst = NULL;
  inst = gnrc_rpl_root_init(instance_id, &dodag_id, false, false);
  if (inst == NULL) {
    char addr_str[IPV6_ADDR_MAX_STR_LEN];
    printf("error: could not add DODAG (%s) to instance (%d)\n",
	   ipv6_addr_to_str(addr_str, &dodag_id, sizeof(addr_str)), instance_id);
    return 1;
  }

  printf("successfully added a new RPL DODAG\n");
  return 0;
}


void *robot_thread(void *arg)
{
  (void) arg;
  msg_t msg;
  uint32_t cmd[1];
  init_robot(&motor_1,&motor_2);
  for (;;)
    {
      msg_receive(&msg);
      cmd[0] = msg.content.value;
      if((cmd[0]&0xff) == 4) 
      {
	  send_to_robot_auto(); 
       } //mode auto
      else 
      {
	send_to_robot_manu(cmd[0]);
	xtimer_sleep(1); 
      }  //mode manu
    }
  return NULL;
}

void *sock_client_thread(void *arg)
{
  (void) arg;
  ssize_t res;
  msg_t msg;
  uint32_t sbuf[1];
  char message[MAX_MESSAGE_LENGTH];
  sock_udp_ep_t remote = { .family = AF_INET6 };
  
  remote.port = 1234;
  remote.addr.ipv6[0] = 0xba;
  remote.addr.ipv6[1] = 0xad;
  remote.addr.ipv6[2] = 0xa5;
  remote.addr.ipv6[3] = 0x55;
  remote.addr.ipv6[14] = 0x17;
  remote.addr.ipv6[15] = 0x36;
  
  for (;;)
    {
      msg_receive(&msg);
      //send command from shell
      sbuf[0] = msg.content.value;
      if (sock_udp_send(&sock,sbuf,sizeof(sbuf), &remote) < 0)
	{
	  puts("Error sending message");
	}
      if ((res = sock_udp_recv(&sock,message,MAX_MESSAGE_LENGTH, 20 * SEC_PER_MIN,&remote)) < 0)
	{
	  if (res == -ETIMEDOUT)
	    {
	      puts("Mode auto");
	    }
	  else
	    {
	      puts("Error receiving message");
	    }
	}
      else{
	puts("Mode manu ok!");     
      }
    }
  return NULL;
}

void *sock_server_thread(void *arg)
{
  (void) arg;
  local.port = 1234;
  int count = 0;
  uint32_t server_msg[1];
  uint32_t auto_r;
  msg_t msg;

  if (sock_udp_create(&sock, &local, NULL, 0) < 0)
    {
      puts("Error creating UDP sock");
      return NULL;
    }

  /* Endless loop waiting for incoming packets */
  while (1)
    {
      sock_udp_ep_t remote;    
      
      /* wait for incoming packets */
      if ((sock_udp_recv(&sock,server_msg, sizeof(server_msg), 5 * SEC_PER_MIN,&remote) >= 0)){
	 
         //send  ack  
     	 if (sock_udp_send(&sock,"ok",sizeof("ok"), &remote) < 0)
		{
	   		puts("Error sending reply");
		}
         msg.content.value = server_msg[0];
         count = 0;
         msg_send(&msg, robot_pid); 
      }
   else{
       auto_r = 4;
       msg.content.value = auto_r;
       	if(count < 3) {
      		msg_send(&msg, robot_pid);
		count++;
	}//auto
       
      } 
  
    }
  return NULL;
}

static void _init_interface(void)
{
    kernel_pid_t ifs[GNRC_NETIF_NUMOF];
    ipv6_addr_t addr = IPV6_ADDR_UNSPECIFIED;
    uint8_t hwaddr[MAX_ADDR_LEN];
    int res;
    
    gnrc_netif_get(ifs);

    //addresses gobales
    addr.u8[0] = 0xba;
    addr.u8[1] = 0xad;
    addr.u8[2] = 0xa5;
    addr.u8[3] = 0x55;

    res = gnrc_netapi_get(ifs[0], NETOPT_ADDRESS, 0, hwaddr, sizeof(hwaddr));

    if (res >= 0) {
        addr.u8[14] = *hwaddr;
        addr.u8[15] = *(hwaddr+1);
    }
    
    gnrc_ipv6_netif_add_addr(ifs[0], &addr, 64, GNRC_IPV6_NETIF_ADDR_FLAGS_UNICAST);
    
 
    if((addr.u8[14]==0x17)&&(addr.u8[15]==0x02))
      {  
        crea_rpl_dodag_root(1, addr);
        client=thread_create(sock_client_stack,sizeof(sock_client_stack),8,THREAD_CREATE_STACKTEST,sock_client_thread,NULL,"sock_client_thread");
      } 
     else if((addr.u8[14]==0x17)&&(addr.u8[15]==0x36)) 
      {
	
	server=thread_create(sock_server_stack,sizeof(sock_server_stack),6,THREAD_CREATE_STACKTEST,sock_server_thread,NULL,"sock_server_thread");
        robot_pid=thread_create(robot_stack,sizeof(robot_stack),7,THREAD_CREATE_STACKTEST,robot_thread,NULL,"robot_thread");
      }
    else
      {
        puts("new node?");
      }
}

static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];

/*shell command for driving the robot */
extern int robot_cmd(int argc, char **argv);

static const shell_command_t shell_commands[] = {
    { "robot", "send robot cmd over UDP to slave", robot_cmd },
    { NULL, NULL, NULL }
};

int main(void)
{
    /* we need a message queue for the thread running the shell in order to
     * receive potentially fast incoming networking packets */
  msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE);
  puts("RIOT network stack example application");
  
  /* init network interfaces */
  _init_interface();
  
  /* start shell */
  puts("All up, running the shell now");
  char line_buf[SHELL_DEFAULT_BUFSIZE];
  shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
  
  /* should be never reached */
  return 0;
}