Donatien Garnier / MiniTLS-GPL

Dependents:   MiniTLS-HTTPS-Example

Revision:
0:35aa5be3b78d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/mbed_net.c	Fri Jun 06 10:49:02 2014 +0000
@@ -0,0 +1,183 @@
+/*
+MuTLS - A super trimmed down TLS/SSL Library for embedded devices
+Author: Donatien Garnier
+Copyright (C) 2013-2014 AppNearMe Ltd
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*//**
+ * \file mbed_net.c
+ * \copyright Copyright (c) AppNearMe Ltd 2013
+ * \author Donatien Garnier
+ */
+
+#define __DEBUG__ 4
+#ifndef __MODULE__
+#define __MODULE__ "mbed_net.c"
+#endif
+
+#include "core/fwk.h"
+#include "stdio.h"
+
+#include "LPC17xx.h"
+
+#include "lwip/inet.h"
+#include "lwip/netif.h"
+#include "netif/etharp.h"
+#include "lwip/dhcp.h"
+#include "arch/lpc17_emac.h"
+#include "lpc_phy.h"
+#include "lwip/tcpip.h"
+
+#include "mbed_interface.h"
+
+#include "rtos/cmsis_os.h"
+
+#include "net/socket/bsd_socket.h"
+
+/* TCP/IP and Network Interface Initialisation */
+static struct netif lpcNetif;
+
+static char mac_addr[19];
+static char ip_addr[17] = "\0";
+static bool use_dhcp = false;
+
+static osSemaphoreId tcpip_inited_sem_id;
+static osSemaphoreDef_t tcpip_inited_sem_def;
+static uint32_t tcpip_inited_sem_buffer[2];
+
+static osSemaphoreId netif_linked_sem_id;
+static osSemaphoreDef_t netif_linked_sem_def;
+static uint32_t netif_linked_sem_buffer[2];
+
+static osSemaphoreId netif_up_sem_id;
+static osSemaphoreDef_t netif_up_sem_def;
+static uint32_t netif_up_sem_buffer[2];
+
+static void tcpip_init_done(void *arg) {
+  DBG("TCP/IP started");
+  osSemaphoreRelease(tcpip_inited_sem_id);
+}
+
+static void netif_link_callback(struct netif *netif) {
+  DBG("Link change");
+    if (netif_is_link_up(netif)) {
+      osSemaphoreRelease(netif_linked_sem_id);
+    }
+}
+
+static void netif_status_callback(struct netif *netif) {
+    DBG("netif change");
+    if (netif_is_up(netif)) {
+        strcpy(ip_addr, inet_ntoa(netif->ip_addr));
+        osSemaphoreRelease(netif_up_sem_id);
+    }
+}
+
+static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) {
+    tcpip_init(tcpip_init_done, NULL);
+    osSemaphoreWait(tcpip_inited_sem_id, -1);
+
+    memset((void*) &lpcNetif, 0, sizeof(lpcNetif));
+    netif_add(&lpcNetif, ipaddr, netmask, gw, NULL, lpc_enetif_init, tcpip_input);
+    netif_set_default(&lpcNetif);
+
+    netif_set_link_callback  (&lpcNetif, netif_link_callback);
+    netif_set_status_callback(&lpcNetif, netif_status_callback);
+}
+
+static void set_mac_address(void) {
+#if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
+    snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", MBED_MAC_ADDR_0, MBED_MAC_ADDR_1, MBED_MAC_ADDR_2,
+             MBED_MAC_ADDR_3, MBED_MAC_ADDR_4, MBED_MAC_ADDR_5);
+#else
+    char mac[6];
+    mbed_mac_address(mac);
+    snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
+#endif
+}
+
+void mbed_net_init(const char* ip, const char* mask, const char* gateway)
+{
+ // DBG("Buffers @ %p %p %p", netif_linked_sem_buffer, netif_linked_sem_buffer, netif_up_sem_buffer); return;
+
+  /* Init semaphores */
+  memset(tcpip_inited_sem_buffer, 0, sizeof(tcpip_inited_sem_buffer));
+  tcpip_inited_sem_def.semaphore = tcpip_inited_sem_buffer;
+  tcpip_inited_sem_id = osSemaphoreCreate(&tcpip_inited_sem_def, 1);
+  osSemaphoreWait(tcpip_inited_sem_id, 0);
+
+  memset(netif_linked_sem_buffer, 0, sizeof(netif_linked_sem_buffer));
+  netif_linked_sem_def.semaphore = netif_linked_sem_buffer;
+  netif_linked_sem_id = osSemaphoreCreate(&netif_linked_sem_def, 1);
+  osSemaphoreWait(netif_linked_sem_id, 0);
+
+  memset(netif_up_sem_buffer, 0, sizeof(netif_up_sem_buffer));
+  netif_up_sem_def.semaphore = netif_up_sem_buffer;
+  netif_up_sem_id = osSemaphoreCreate(&netif_up_sem_def, 1);
+  osSemaphoreWait(netif_up_sem_id, 0);
+
+  set_mac_address();
+
+  if(ip == NULL)
+  {
+    use_dhcp = true;
+    init_netif(NULL, NULL, NULL);
+  }
+  else
+  {
+    use_dhcp = false;
+    strcpy(ip_addr, ip);
+    ip_addr_t ip_n, mask_n, gateway_n;
+    inet_aton(ip, &ip_n);
+    inet_aton(mask, &mask_n);
+    inet_aton(gateway, &gateway_n);
+    init_netif(&ip_n, &mask_n, &gateway_n);
+  }
+
+}
+
+int mbed_net_connect(unsigned int timeout_ms) {
+    NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01));
+    NVIC_EnableIRQ(ENET_IRQn);
+
+    int inited;
+    if (use_dhcp) {
+        dhcp_start(&lpcNetif);
+
+        // Wait for an IP Address
+        // -1: error, 0: timeout
+        inited = osSemaphoreWait(netif_up_sem_id, timeout_ms);
+    } else {
+        netif_set_up(&lpcNetif);
+
+        // Wait for the link up
+        inited =  osSemaphoreWait(netif_linked_sem_id, timeout_ms);
+    }
+
+    return (inited > 0) ? (0) : (-1);
+}
+
+int mbed_net_disconnect() {
+    if (use_dhcp) {
+        dhcp_release(&lpcNetif);
+        dhcp_stop(&lpcNetif);
+    } else {
+        netif_set_down(&lpcNetif);
+    }
+
+    NVIC_DisableIRQ(ENET_IRQn);
+
+    return 0;
+}