old test code: uIP with IPv4, uIP unicast
[wsn-p.git] / exercise_remotecontrol / uip_udp / led_remote_control.c
diff --git a/exercise_remotecontrol/uip_udp/led_remote_control.c b/exercise_remotecontrol/uip_udp/led_remote_control.c
new file mode 100644 (file)
index 0000000..6ecc06e
--- /dev/null
@@ -0,0 +1,155 @@
+#include <contiki.h>
+#include <contiki-lib.h>
+#include <contiki-net.h>
+#include <stdio.h>
+#include <dev/leds.h>
+#include <dev/button-sensor.h>
+#include <settings.h>
+//#include <net/uip.h>
+//#include <net/psock.h>
+#include <string.h>
+
+////////////////// DEBUGGING STUFF
+#define DEBUG DEBUG_PRINT
+#include "net/uip-debug.h"
+//////////////////////////////////
+
+//extern struct uip_eth_addr uip_ethaddr;
+
+PROCESS(led_remote_control, "binary LED counter via button over UDP");
+AUTOSTART_PROCESSES(&led_remote_control);
+
+/** node address */
+static uint16_t node_addr;
+static uip_ipaddr_t ipaddr;
+/** UDP connection object */
+static struct uip_udp_conn * udp_conn;
+/** Port used for the connection */
+#define UDP_PORT 1337
+#define MAX_PAYLOAD_LEN   40
+#define UIP_IP_BUF   ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
+
+/** somehow these need to be defined... */
+
+/*---------------------------------------------------------------------------*/
+static void
+print_local_addresses(void)
+{
+  int i;
+  uint8_t state;
+
+  PRINTF("Server IPv6 addresses: ");
+  for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
+    state = uip_ds6_if.addr_list[i].state;
+    if(uip_ds6_if.addr_list[i].isused &&
+       (state == ADDR_TENTATIVE || state == ADDR_PREFERRED)) {
+      PRINT6ADDR(&uip_ds6_if.addr_list[i].ipaddr);
+      PRINTF("\n");
+    }
+  }
+}
+//
+//PT_THREAD(recv(struct psock * ps)) {
+//  PSOCK_BEGIN(ps);
+//
+//  PSOCK_READBUF(ps);
+//  printf("node %d: recv from %d: %d\n", /* FIXME */ 0, 0, buffer);
+//  // FIXME
+//  if(buffer % 2) {
+//    leds_toggle(LEDS_GREEN);
+//  }
+//  leds_toggle(LEDS_YELLOW);
+//  printf("toggled LEDs\n");
+//  PSOCK_END(ps);
+//}
+
+PROCESS_THREAD(led_remote_control, ev, data) {
+  PROCESS_BEGIN();
+
+  leds_init();
+  leds_off(LEDS_ALL);
+
+  SENSORS_ACTIVATE(button_sensor);
+
+  // get node address
+  node_addr = settings_get_uint16(SETTINGS_KEY_PAN_ADDR, 0);
+  printf("node address is %d\n", node_addr);
+
+
+  // set MAC address
+//  uip_lladdr.addr[4] = 0xfe;
+//  uip_lladdr.addr[5] = 0x33;
+//  uip_lladdr.addr[6] = (node_addr >> 8) & 0xff;
+//  uip_lladdr.addr[7] = node_addr & 0xff;
+//  settings_set(SETTINGS_KEY_EUI64, &uip_lladdr.addr, sizeof(uip_lladdr));
+
+
+
+  // set up IP stack
+//  uip_ds6_init();
+  print_local_addresses();
+//  static struct uip_eth_addr eth_addr = {{0x00, 0x06, 0x98, 0x01, 0x00, 0x00}};
+//  uip_setethaddr(eth_addr);
+
+//  uip_ip6addr(&ipaddr, 0xfe80, 0x1234, 0x5678, 0x90ab, 0xcdef, 0x1357, 0x9ace,
+//    node_addr);
+//  uip_sethostaddr(&ipaddr);
+//  uip_ipaddr(&ipaddr, 255, 255, 0, 0);
+//  uip_setnetmask(&ipaddr);
+
+//  uip_gethostaddr(&ipaddr);
+//  PRINTF("IP address is ");
+//  PRINT6ADDR(&ipaddr);
+//  PRINTF("\n");
+
+  //  udp_conn = udp_broadcast_new(uip_htons(UDP_PORT), 0);
+  uip_ip6addr(&ipaddr, 0xfe80, 0, 0, 0, 0x1d00, 0x22ff, 0xfe33, 0x001f);
+  udp_conn = udp_new(&ipaddr, uip_htons(UDP_PORT), 0);
+
+  udp_bind(udp_conn, UIP_HTONS(UDP_PORT));
+
+  static struct etimer timer;
+  while(1) {
+    PROCESS_WAIT_EVENT_UNTIL(
+      (ev == sensors_event && data == &button_sensor) ||
+      ev == tcpip_event);
+
+    printf("event detected\n");
+    if(ev == sensors_event) {
+      printf("surprise, it's a button event!\n");
+      // debounce for 250 ms
+      etimer_set(&timer, CLOCK_SECOND * 0.25);
+      PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
+
+      static int seq_id;
+      char buf[MAX_PAYLOAD_LEN];
+
+      //uip_create_linklocal_allnodes_mcast(&udp_conn->ripaddr);
+      //uip_ipaddr_copy(&udp_conn->ripaddr, &ipaddr);
+      PRINTF("Sending message from ");
+      PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
+      PRINTF(" to ");
+      PRINT6ADDR(&udp_conn->ripaddr);
+      sprintf(buf, "Hello! (%d)", ++seq_id);
+      PRINTF(": %s\n", buf);
+
+      uip_udp_packet_send(udp_conn, buf, strlen(buf));
+
+
+
+    } else if(ev == tcpip_event) {
+      PRINTF("tcpip_event detected\n");
+      if(uip_newdata()) {
+        ((char *)uip_appdata)[uip_datalen()] = 0;
+        PRINTF("Received: '%s' from ", (char *)uip_appdata);
+        PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
+        PRINTF("\n");
+
+        /* Restore server connection to allow data from any node */
+        //memset(&udp_conn->ripaddr, 0, sizeof(udp_conn->ripaddr));
+      }
+    }
+  }
+
+  PROCESS_END();
+}
This page took 0.023863 seconds and 4 git commands to generate.