Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding SocketApp sketch

SocketApp sketch is a simple network application which demonstrates the use of protosockets, the uIP equivalent of socket API for network
programming.
  • Loading branch information...
commit 04e40ebb9023d4e612765b7696a2cb2c4865f246 1 parent 8ebe3d7
@asynclabs asynclabs authored
View
5 apps-conf.h
@@ -38,6 +38,7 @@
//Here we include the header file for the application(s) we use in our project.
#define APP_WEBSERVER
//#define APP_WEBCLIENT
+//#define APP_SOCKAPP
#ifdef APP_WEBSERVER
#include "webserver.h"
@@ -47,4 +48,8 @@
#include "webclient.h"
#endif
+#ifdef APP_SOCKAPP
+#include "socketapp.h"
+#endif
+
#endif /*__APPS_CONF_H__*/
View
48 examples/SocketApp/SocketApp.pde
@@ -0,0 +1,48 @@
+/*
+ * Socket App
+ *
+ * A simple socket application example using the WiShield 1.0
+ */
+
+#include <WiShield.h>
+
+#define WIRELESS_MODE_INFRA 1
+#define WIRELESS_MODE_ADHOC 2
+
+// Wireless configuration parameters ----------------------------------------
+unsigned char local_ip[] = {192,168,1,2}; // IP address of WiShield
+unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
+unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
+const prog_char ssid[] PROGMEM = {"ASYNCLABS"}; // max 32 bytes
+
+unsigned char security_type = 0; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
+
+// WPA/WPA2 passphrase
+const prog_char security_passphrase[] PROGMEM = {"12345678"}; // max 64 characters
+
+// WEP 128-bit keys
+// sample HEX keys
+prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
+ };
+
+// setup the wireless mode
+// infrastructure - connect to AP
+// adhoc - connect to another WiFi device
+unsigned char wireless_mode = WIRELESS_MODE_INFRA;
+
+unsigned char ssid_len;
+unsigned char security_passphrase_len;
+//---------------------------------------------------------------------------
+
+void setup()
+{
+ WiFi.init();
+}
+
+void loop()
+{
+ WiFi.run();
+}
View
118 examples/SocketApp/socketapp.c
@@ -0,0 +1,118 @@
+
+/******************************************************************************
+
+ Filename: socketapp.c
+ Description: Simple socket programming example for the WiShield 1.0
+
+ ******************************************************************************
+
+ TCP/IP stack and driver for the WiShield 1.0 wireless devices
+
+ Copyright(c) 2009 Async Labs Inc. All rights reserved.
+
+ This program is free software; you can redistribute it and/or modify it
+ under the terms of version 2 of the GNU General Public License as
+ published by the Free Software Foundation.
+
+ This program is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ more details.
+
+ You should have received a copy of the GNU General Public License along with
+ this program; if not, write to the Free Software Foundation, Inc., 59
+ Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+ Contact Information:
+ <asynclabs@asynclabs.com>
+
+ Author Date Comment
+ ---------------------------------------------------------------
+ AsyncLabs 06/06/2009 Initial version
+
+ *****************************************************************************/
+
+/*
+ * This is a short example of how to write uIP applications using
+ * protosockets.
+ */
+
+/*
+ * We define the application state (struct socket_app_state) in the
+ * socketapp.h file, so we need to include it here. We also include
+ * uip.h (since this cannot be included in socketapp.h) and
+ * <string.h>, since we use the memcpy() function in the code.
+ */
+#include "socketapp.h"
+#include "uip.h"
+#include <string.h>
+
+/*
+ * Declaration of the protosocket function that handles the connection
+ * (defined at the end of the code).
+ */
+static int handle_connection(struct socket_app_state *s);
+/*---------------------------------------------------------------------------*/
+/*
+ * The initialization function. We must explicitly call this function
+ * from the system initialization code, some time after uip_init() is
+ * called.
+ */
+void socket_app_init(void)
+{
+ /* We start to listen for connections on TCP port 1000. */
+ uip_listen(HTONS(1000));
+}
+/*---------------------------------------------------------------------------*/
+/*
+ * In socketapp.h we have defined the UIP_APPCALL macro to
+ * socket_app_appcall so that this function is uIP's application
+ * function. This function is called whenever an uIP event occurs
+ * (e.g. when a new connection is established, new data arrives, sent
+ * data is acknowledged, data needs to be retransmitted, etc.).
+ */
+void socket_app_appcall(void)
+{
+ /*
+ * The uip_conn structure has a field called "appstate" that holds
+ * the application state of the connection. We make a pointer to
+ * this to access it easier.
+ */
+ struct socket_app_state *s = &(uip_conn->appstate);
+
+ /*
+ * If a new connection was just established, we should initialize
+ * the protosocket in our applications' state structure.
+ */
+ if(uip_connected()) {
+ PSOCK_INIT(&s->p, s->inputbuffer, sizeof(s->inputbuffer));
+ }
+
+ /*
+ * Finally, we run the protosocket function that actually handles
+ * the communication. We pass it a pointer to the application state
+ * of the current connection.
+ */
+ handle_connection(s);
+}
+/*---------------------------------------------------------------------------*/
+/*
+ * This is the protosocket function that handles the communication. A
+ * protosocket function must always return an int, but must never
+ * explicitly return - all return statements are hidden in the PSOCK
+ * macros.
+ */
+static int handle_connection(struct socket_app_state *s)
+{
+ PSOCK_BEGIN(&s->p);
+
+ PSOCK_SEND_STR(&s->p, "Hello. What is you name?\n");
+ PSOCK_READTO(&s->p, '\n');
+ PSOCK_SEND_STR(&s->p, "Hello ");
+ PSOCK_SEND_STR(&s->p, s->inputbuffer);
+ memset(s->inputbuffer, 0x00, sizeof(s->inputbuffer));
+ PSOCK_CLOSE(&s->p);
+
+ PSOCK_END(&s->p);
+}
+/*---------------------------------------------------------------------------*/
View
62 socketapp.h
@@ -0,0 +1,62 @@
+
+/******************************************************************************
+
+ Filename: socketapp.h
+ Description: Simple socket programming example for the WiShield 1.0
+
+ ******************************************************************************
+
+ TCP/IP stack and driver for the WiShield 1.0 wireless devices
+
+ Copyright(c) 2009 Async Labs Inc. All rights reserved.
+
+ This program is free software; you can redistribute it and/or modify it
+ under the terms of version 2 of the GNU General Public License as
+ published by the Free Software Foundation.
+
+ This program is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ more details.
+
+ You should have received a copy of the GNU General Public License along with
+ this program; if not, write to the Free Software Foundation, Inc., 59
+ Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+ Contact Information:
+ <asynclabs@asynclabs.com>
+
+ Author Date Comment
+ ---------------------------------------------------------------
+ AsyncLabs 06/06/2009 Initial version
+
+ *****************************************************************************/
+
+#ifndef __SOCKET_APP_H__
+#define __SOCKET_APP_H__
+
+/* Since this file will be included by uip.h, we cannot include uip.h
+ here. But we might need to include uipopt.h if we need the u8_t and
+ u16_t datatypes. */
+#include "uipopt.h"
+#include "psock.h"
+
+/* Next, we define the uip_tcp_appstate_t datatype. This is the state
+ of our application, and the memory required for this state is
+ allocated together with each TCP connection. One application state
+ for each TCP connection. */
+typedef struct socket_app_state {
+ struct psock p;
+ char inputbuffer[20];
+ char name[20];
+} uip_tcp_appstate_t;
+
+/* Finally we define the application function to be called by uIP. */
+void socket_app_appcall(void);
+#ifndef UIP_APPCALL
+#define UIP_APPCALL socket_app_appcall
+#endif /* UIP_APPCALL */
+
+void socket_app_init(void);
+
+#endif /* __SOCKET_APP_H__ */
View
4 stack.c
@@ -81,6 +81,10 @@ void stack_init(void)
webclient_init();
#endif
+#ifdef APP_SOCKAPP
+ socket_app_init();
+#endif
+
uip_ipaddr(ipaddr, local_ip[0], local_ip[1], local_ip[2], local_ip[3]);
uip_sethostaddr(ipaddr);
uip_ipaddr(ipaddr, gateway_ip[0],gateway_ip[1],gateway_ip[2],gateway_ip[3]);
Please sign in to comment.
Something went wrong with that request. Please try again.