From c48ac773ed64a5e97ea2f2b3c40a067987208548 Mon Sep 17 00:00:00 2001 From: Sachin Agrawal Date: Mon, 8 Sep 2014 09:17:28 -0700 Subject: [PATCH] Patch 1 : Adding Arduino WiFi support. This requires updated Arduino WiFi Shield firmware Patch 2 : Removed ocserver_wifi app and incorporated wifi changes in the single ocserver application. Updated location of Time library in arduinomega and arduinodue properties file. Added some more comments in sample app. Added license info in ocserver which was missing. Change-Id: Ie852a61ff3b0b5707f79899427668698792d1fcc --- csdk/README | 7 +- csdk/arduinodue.properties | 4 +- csdk/arduinomega.properties | 17 +- csdk/libcoap-4.1.1/makefile | 11 +- csdk/makefile | 7 +- csdk/ocsocket/src/ocsocket_arduino_wifi.cpp | 300 +++++++++++++++++++++ csdk/ocsocket/test/arduino/makefile | 120 +++++---- csdk/ocsocket/test/arduino/ocsocket_wifi_test.cpp | 204 ++++++++++++++ .../arduino/SimpleClientServer/ocserver/README | 3 + .../arduino/SimpleClientServer/ocserver/makefile | 11 +- .../SimpleClientServer/ocserver/ocserver.cpp | 128 +++++++-- 11 files changed, 720 insertions(+), 92 deletions(-) create mode 100644 csdk/ocsocket/src/ocsocket_arduino_wifi.cpp create mode 100644 csdk/ocsocket/test/arduino/ocsocket_wifi_test.cpp diff --git a/csdk/README b/csdk/README index e1e92af..bf548d8 100644 --- a/csdk/README +++ b/csdk/README @@ -19,13 +19,14 @@ the following definition(s): ARDUINO_TOOLS_DIR = (location of avr-g++ and other avr tools) To make octbstack.a in release mode: - make PLATFORM=arduinomega (For Arduino Due, use ) To make octbstack.a in debug mode: - make PLATFORM=arduinomega BUILD=debug (For Arduino Due, use ) +To make octbstack.a with Arduino WiFi Shield support: + make PLATFORM=arduinomega ARDUINOWIFI=1 (For Arduino Due, use ) + Dependencies: 1) Patch the Wiznet Ethernet library with the patch available at : oic-utilities/tb/arduino_linux_ethernet_socket_cpp.patch @@ -34,6 +35,8 @@ Dependencies: 3) Arduino Due has been tested with Arduino 1.5.7 framework. 4) For Arduino Due, binary tools are located in: /hardware/tools/gcc-arm-none-eabi-4.8.3-2014q1/bin + 5) For Arduino WiFi, update the WiFi shield with the firmware patches provided + by Intel. ------------------------------------------------------------------------------- **Clean-Up** ------------------------------------------------------------------------------- diff --git a/csdk/arduinodue.properties b/csdk/arduinodue.properties index 907c340..06a40b0 100644 --- a/csdk/arduinodue.properties +++ b/csdk/arduinodue.properties @@ -6,7 +6,7 @@ SDIR_ARD_CORE_4 = $(ARDUINO_DIR)/hardware/arduino/sam/cores/arduino/avr SDIR_ARD_SPI = $(ARDUINO_DIR)/hardware/arduino/sam/libraries/SPI SDIR_ARD_ETH = $(ARDUINO_DIR)/libraries/Ethernet/src SDIR_ARD_ETH_UTIL = $(ARDUINO_DIR)/libraries/Ethernet/src/utility -SDIR_ARD_TIME = $(ARDUINO_DIR)/libraries/Time +SDIR_ARD_TIME = $(ARDUINO_DIR)/libraries/Time/Time SDIR_ARD_PLATFORM = $(SDIR_ARD_CORE_1):$(SDIR_ARD_CORE_2):$(SDIR_ARD_CORE_3):$(SDIR_ARD_CORE_4):$(SDIR_ARD_SPI):\ $(SDIR_ARD_ETH):$(SDIR_ARD_ETH_UTIL):$(SDIR_ARD_TIME) @@ -20,7 +20,7 @@ IDIR_ARD_CORE_5 = $(ARDUINO_DIR)/hardware/arduino/sam/variants/arduino_due_x IDIR_ARD_SPI = $(ARDUINO_DIR)/hardware/arduino/sam/libraries/SPI IDIR_ARD_ETH = $(ARDUINO_DIR)/libraries/Ethernet/src IDIR_ARD_ETH_UTIL = $(ARDUINO_DIR)/libraries/Ethernet/src/utility -INCD_ARD_TIME = $(ARDUINO_DIR)/libraries/Time +INCD_ARD_TIME = $(ARDUINO_DIR)/libraries/Time/Time INC_DIR_PLATFORM = -I$(IDIR_ARD_CORE_1) -I$(IDIR_ARD_CORE_2) -I$(IDIR_ARD_CORE_3) -I$(IDIR_ARD_CORE_4) -I$(IDIR_ARD_CORE_5) \ diff --git a/csdk/arduinomega.properties b/csdk/arduinomega.properties index 845cb50..ec99069 100644 --- a/csdk/arduinomega.properties +++ b/csdk/arduinomega.properties @@ -4,8 +4,11 @@ SDIR_ARD_CORE = $(ARDUINO_DIR)/hardware/arduino/cores/arduino SDIR_ARD_SPI = $(ARDUINO_DIR)/libraries/SPI SDIR_ARD_ETH = $(ARDUINO_DIR)/libraries/Ethernet SDIR_ARD_ETH_UTIL = $(ARDUINO_DIR)/libraries/Ethernet/utility -SDIR_ARD_TIME = $(ARDUINO_DIR)/libraries/Time -SDIR_ARD_PLATFORM = $(SDIR_ARD_CORE):$(SDIR_ARD_SPI):$(SDIR_ARD_ETH):$(SDIR_ARD_ETH_UTIL):$(SDIR_ARD_TIME) +SDIR_ARD_WIFI = $(ARDUINO_DIR)/libraries/WiFi/src +SDIR_ARD_WIFI_UTIL = $(ARDUINO_DIR)/libraries/WiFi/src/utility +SDIR_ARD_TIME = $(ARDUINO_DIR)/libraries/Time/Time +SDIR_ARD_PLATFORM = $(SDIR_ARD_CORE):$(SDIR_ARD_SPI):$(SDIR_ARD_ETH):$(SDIR_ARD_ETH_UTIL):$(SDIR_ARD_TIME) \ + :$(SDIR_ARD_WIFI):$(SDIR_ARD_WIFI_UTIL) #include directories INCD_ARD_CORE = -I$(ARDUINO_DIR)/hardware/arduino/cores/arduino @@ -13,9 +16,12 @@ INCD_ARD_VARIANT = -I$(ARDUINO_DIR)/hardware/arduino/variants/mega INCD_ARD_SPI = -I$(ARDUINO_DIR)/libraries/SPI INCD_ARD_ETH = -I$(ARDUINO_DIR)/libraries/Ethernet INCD_ARD_ETH_UTIL = -I$(ARDUINO_DIR)/libraries/Ethernet/utility -INCD_ARD_TIME = -I$(ARDUINO_DIR)/libraries/Time +INCD_ARD_WIFI = -I$(ARDUINO_DIR)/libraries/WiFi/src +INCD_ARD_WIFI_UTIL = -I$(ARDUINO_DIR)/libraries/WiFi/src/utility +INCD_ARD_TIME = -I$(ARDUINO_DIR)/libraries/Time/Time -INC_DIR_PLATFORM = $(INCD_ARD_CORE) $(INCD_ARD_VARIANT) $(INCD_ARD_SPI) $(INCD_ARD_ETH) $(INCD_ARD_ETH_UTIL) $(INCD_ARD_TIME) +INC_DIR_PLATFORM = $(INCD_ARD_CORE) $(INCD_ARD_VARIANT) $(INCD_ARD_SPI) $(INCD_ARD_ETH) $(INCD_ARD_ETH_UTIL) \ + $(INCD_ARD_TIME) $(INCD_ARD_WIFI) $(INCD_ARD_WIFI_UTIL) #Compiler/Linker flags CFLAGS_PLATFORM = -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=156 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -DWITH_ARDUINO \ @@ -34,7 +40,8 @@ CORE_COBJ = WInterrupts.o wiring.o wiring_digital.o CORE_CPPOBJ = main.o Stream.o WMath.o WString.o HardwareSerial.o Print.o SPI.o IPAddress.o ETH_CPPOBJ = Dhcp.o Dns.o Ethernet.o EthernetUdp.o ETH_UTIL_CPPOBJ = socket.o w5100.o +WIFI_COBJ = WiFi.o WiFiClient.o WiFiServer.o WiFiUdp.o server_drv.o spi_drv.o wifi_drv.o OCDEPENDENT_CPPOBJ = wiring_analog.o -PLATFORM_OBJS = $(CORE_COBJ) $(CORE_CPPOBJ) $(ETH_CPPOBJ) $(ETH_UTIL_CPPOBJ) $(OCDEPENDENT_CPPOBJ) +PLATFORM_OBJS = $(CORE_COBJ) $(CORE_CPPOBJ) $(ETH_CPPOBJ) $(ETH_UTIL_CPPOBJ) $(OCDEPENDENT_CPPOBJ) $(WIFI_COBJ) diff --git a/csdk/libcoap-4.1.1/makefile b/csdk/libcoap-4.1.1/makefile index 8655268..d4b6905 100644 --- a/csdk/libcoap-4.1.1/makefile +++ b/csdk/libcoap-4.1.1/makefile @@ -1,10 +1,13 @@ # override with `make BUILD=debug` -# override with `make PLATFORM=arduino` +# override with `make PLATFORM=arduinomega` or `make PLATFORM=arduinodue` # default to release build # default to build for linux BUILD := release #other options are android, arduino PLATFORM=linux +# override with `make PLATFORM=arduinomega ARDUINOWIFI=1` to enable Arduino WiFi shield +ARDUINOWIFI := 0 + OUT_DIR := ./$(BUILD) OBJ_DIR := $(OUT_DIR)/obj @@ -50,7 +53,11 @@ SOURCES:= pdu.c net.c debug.c encode.c uri.c coap_list.c resource.c hashkey.c \ str.c option.c async.c subscribe.c block.c logger.c ocrandom.c VPATH := $(OCSOCK_DIR)/src:$(LOGGER_DIR)/src:$(RANDOM_DIR)/src ifeq (arduino, $(findstring arduino,$(PLATFORM))) - SOURCES += ocsocket_arduino.c + ifeq ($(ARDUINOWIFI),1) + SOURCES += ocsocket_arduino_wifi.c + else + SOURCES += ocsocket_arduino.c + endif SOURCESCPP:= Time.cpp OBJECTSCPP:= $(patsubst %.cpp, %.o, $(SOURCESCPP)) VPATH += $(SDIR_ARD_TIME) diff --git a/csdk/makefile b/csdk/makefile index ef0dcd7..473ef46 100644 --- a/csdk/makefile +++ b/csdk/makefile @@ -1,10 +1,13 @@ # override with `make BUILD=debug` -# override with `make PLATFORM=arduino` +# override with `make PLATFORM=arduinomega` or `make PLATFORM=arduinodue` # default to release build # default to build for linux BUILD := release PLATFORM := linux +# override with `make PLATFORM=arduinomega ARDUINOWIFI=1` to enable Arduino WiFi shield +ARDUINOWIFI := 0 + ifeq ($(ROOT_DIR), ) ROOT_DIR = $(PWD) endif @@ -86,7 +89,7 @@ SOURCES += $(OCTBSTACK_SOURCES) all: make_lcoap objdirs obj_build liboctbstack.a #print_vars make_lcoap: - $(MAKE) -C $(LCOAP_DIR) "BUILD=$(BUILD)" "PLATFORM=$(PLATFORM)" + $(MAKE) -C $(LCOAP_DIR) "BUILD=$(BUILD)" "PLATFORM=$(PLATFORM)" "ARDUINOWIFI=$(ARDUINOWIFI)" objdirs: $(ROOT_DIR) mkdir -p $(BUILD) diff --git a/csdk/ocsocket/src/ocsocket_arduino_wifi.cpp b/csdk/ocsocket/src/ocsocket_arduino_wifi.cpp new file mode 100644 index 0000000..eeb5c01 --- /dev/null +++ b/csdk/ocsocket/src/ocsocket_arduino_wifi.cpp @@ -0,0 +1,300 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include +#include +#include +#include +#include +#include +#include + +/// Module Name +#define MOD_NAME PCF("ocsocket") + +/// Macro to verify the validity of input argument +#define VERIFY_NON_NULL(arg) { if (!arg) {OC_LOG_V(FATAL, MOD_NAME, "%s is NULL", #arg); \ + return ERR_INVALID_INPUT;} } + +/// Length of the IP address decimal notation string +#define IPNAMESIZE (16) + +/// This is the max buffer size between Arduino and WiFi Shield +#define ARDUINO_WIFI_SPI_RECV_BUFFERSIZE (64) + +// Start offsets based on end of received data buffer +#define WIFI_RECBUF_IPADDR_OFFSET (6) +#define WIFI_RECBUF_PORT_OFFSET (2) + +#define WIFI_RECBUF_IPADDR_SIZE (WIFI_RECBUF_IPADDR_OFFSET - WIFI_RECBUF_PORT_OFFSET) +#define WIFI_RECBUF_PORT_SIZE (WIFI_RECBUF_PORT_OFFSET - 0) +#define WIFI_RECBUF_FOOTER_SIZE (WIFI_RECBUF_IPADDR_SIZE + WIFI_RECBUF_PORT_SIZE) + + +/// IPv4 address representation for Arduino Ethernet Shield +typedef struct { + uint32_t size; /// size of IP address and port bytes + uint8_t a; + uint8_t b; + uint8_t c; + uint8_t d; + uint16_t port; +} ArduinoAddr; + + +/// Builds a socket interface address using IP address and port number +int32_t OCBuildIPv4Address(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t port, OCDevAddr *ipAddr) +{ + ArduinoAddr* ardAddr = (ArduinoAddr*)ipAddr; + + VERIFY_NON_NULL(ardAddr); + + memset(ardAddr, 0, sizeof(ArduinoAddr)); + + ardAddr->size = sizeof(ArduinoAddr) - sizeof(ardAddr->size); + ardAddr-> a = a; + ardAddr-> b = b; + ardAddr-> c = c; + ardAddr-> d = d; + ardAddr-> port = port; + + return ERR_SUCCESS; +} + + +/// Retrieves the IP address assigned to Arduino Ethernet shield +int32_t OCGetInterfaceAddress(uint8_t* ifName, uint32_t ifNameLen, uint16_t addrType, + uint8_t *addr, uint32_t addrLen) +{ + WiFiClass WiFi; + + VERIFY_NON_NULL(addr); + if (addrLen < IPNAMESIZE) { + OC_LOG(FATAL, MOD_NAME, PCF("OCGetInterfaceAddress: addrLen MUST be at least 16")); + return ERR_INVALID_INPUT; + } + + if (addrType != AF_INET) { + return ERR_INVALID_INPUT; + } + + IPAddress ip = WiFi.localIP(); + sprintf((char *)addr,"%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); + + OC_LOG_BUFFER(INFO, MOD_NAME, addr, addrLen); + + return ERR_SUCCESS; +} + +/// Retrieves a empty socket and bind it for UDP with the input port +int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd) +{ + ArduinoAddr* ardAddr = (ArduinoAddr*)ipAddr; + uint8_t sock; + + VERIFY_NON_NULL(ardAddr); + VERIFY_NON_NULL(sockfd); + + OC_LOG(DEBUG, MOD_NAME, PCF("OCInitUDP Begin")); + //Is any socket available to work with ? + *sockfd = -1; + + sock = WiFiClass::getSocket(); + if (sock != NO_SOCKET_AVAIL) + { + ServerDrv::startServer(ardAddr->port, sock, UDP_MODE); + WiFiClass::_server_port[sock] = ardAddr->port; + *sockfd = (int32_t)sock; + } + + if (*sockfd == -1) + { + return ERR_UNKNOWN; + } + + OC_LOG(DEBUG, MOD_NAME, PCF("OCInitUDP End")); + return ERR_SUCCESS; +} + + + +/// Currently WiFi shield does NOT support multicast. +int32_t OCInitUDPMulticast(OCDevAddr* ipMcastMacAddr, int32_t* sockfd) +{ + return OCInitUDP(ipMcastMacAddr, sockfd); +} + + +/// Send data to requested end-point using UDP socket +int32_t OCSendTo(int32_t sockfd, const uint8_t* buf, uint32_t bufLen, uint32_t flags, + OCDevAddr * ipAddr) +{ + int32_t ret = 0; + ArduinoAddr* ardAddr = (ArduinoAddr*)ipAddr; + uint32_t ip; + uint16_t rem, send; + + VERIFY_NON_NULL(buf); + VERIFY_NON_NULL(ardAddr); + OC_LOG(DEBUG, MOD_NAME, PCF("OCSendTo Begin")); + + if (sockfd >= MAX_SOCK_NUM) + { + OC_LOG(ERROR, MOD_NAME, PCF("Invalid sockfd")); + return -1; + } + + memcpy((uint8_t*)&ip, (uint8_t*)&(ardAddr->a), sizeof(ip)); + ServerDrv::startClient(ip, ardAddr->port, (uint8_t)sockfd, UDP_MODE); + + rem = bufLen; + do + { + send = (rem > ARDUINO_WIFI_SPI_RECV_BUFFERSIZE ) ? ARDUINO_WIFI_SPI_RECV_BUFFERSIZE : rem; + if (!ServerDrv::insertDataBuf((uint8_t)sockfd, buf, (uint16_t)send)) + { + OC_LOG(ERROR, MOD_NAME, PCF("insertDataBuf error")); + ret = -1; + break; + } + rem = rem - send; + buf = buf + send; + }while(rem > 0); + + if (ret != -1) + { + if (!ServerDrv::sendUdpData((uint8_t)sockfd)) + { + OC_LOG(ERROR, MOD_NAME, PCF("sendUdpData error")); + ret = -1; + } + else + { + ret = bufLen; + } + } + OC_LOG(DEBUG, MOD_NAME, PCF("OCSendTo End")); + return ret; +} + + +/// Retrieve any available data from UDP socket. This is a non-blocking call. +int32_t OCRecvFrom(int32_t sockfd, uint8_t* buf, uint32_t bufLen, uint32_t flags, + OCDevAddr * ipAddr) +{ + + uint16_t recvLen; + uint16_t size = 0; + ArduinoAddr* ardAddr = (ArduinoAddr*)ipAddr; + + VERIFY_NON_NULL(buf); + VERIFY_NON_NULL(ardAddr); + + OC_LOG(DEBUG, MOD_NAME, PCF("OCRecvFrom Begin")); + if (sockfd >= MAX_SOCK_NUM) + { + OC_LOG(ERROR, MOD_NAME, PCF("Invalid sockfd")); + return -1; + } + + recvLen = (int32_t)ServerDrv::availData((uint8_t)sockfd); + if (recvLen == 0) + { + return recvLen; + } + + // Make sure buf is large enough for received data + if ((uint32_t)recvLen > bufLen) + { + OC_LOG(ERROR, MOD_NAME, PCF("Receive buffer too small")); + return -1; + } + + if (!ServerDrv::getDataBuf((uint8_t)sockfd, buf, &size)) + { + OC_LOG(ERROR, MOD_NAME, PCF("getDataBuf error")); + return -1; + } + + // Read IP Address and Port from end of receive buffer + memcpy(&(ardAddr->a), &buf[size - WIFI_RECBUF_IPADDR_OFFSET], WIFI_RECBUF_IPADDR_SIZE); + // Change the endianness of the port number + *((uint8_t*)&(ardAddr->port)) = buf[size - (WIFI_RECBUF_PORT_OFFSET-1)]; + *((uint8_t*)&(ardAddr->port) + 1) = buf[size - (WIFI_RECBUF_PORT_OFFSET)]; + + size -= WIFI_RECBUF_FOOTER_SIZE; + + ardAddr->size = sizeof(ArduinoAddr) - sizeof(ardAddr->size); + OC_LOG(DEBUG, MOD_NAME, PCF("OCRecvFrom End")); + return (int32_t)size; +} + + +/// Close the socket and release all system resources. +int32_t OCClose(int32_t sockfd) +{ + if (sockfd >= MAX_SOCK_NUM) + { + OC_LOG(ERROR, MOD_NAME, PCF("Invalid sockfd")); + return -1; + } + ServerDrv::stopClient(sockfd); + + WiFiClass::_server_port[sockfd] = 0; + WiFiClass::_state[sockfd] = NA_STATE; + + return ERR_SUCCESS; +} + + +/// Retrieve the IPv4 address embedded inside OCDev address data structure +int32_t OCDevAddrToIPv4Addr(OCDevAddr *ipAddr, uint8_t *a, uint8_t *b, + uint8_t *c, uint8_t *d ) +{ + ArduinoAddr* ardAddr = (ArduinoAddr*)ipAddr; + + if ( !ardAddr || !a || !b || !c || !d ) { + OC_LOG(FATAL, MOD_NAME, PCF("Invalid argument")); + return ERR_INVALID_INPUT; + } + + *a = ardAddr->a; + *b = ardAddr->b; + *c = ardAddr->c; + *d = ardAddr->d; + + return ERR_SUCCESS; +} + + +/// Retrieve the IPv4 address embedded inside OCDev address data structure +int32_t OCDevAddrToPort(OCDevAddr *ipAddr, uint16_t *port) +{ + ArduinoAddr* ardAddr = (ArduinoAddr*)ipAddr; + + if ( !ardAddr || !port ) { + OC_LOG(FATAL, MOD_NAME, PCF("Invalid argument")); + return ERR_INVALID_INPUT; + } + + *port = ardAddr->port; + + return ERR_SUCCESS; +} diff --git a/csdk/ocsocket/test/arduino/makefile b/csdk/ocsocket/test/arduino/makefile index a57388a..9772433 100644 --- a/csdk/ocsocket/test/arduino/makefile +++ b/csdk/ocsocket/test/arduino/makefile @@ -1,82 +1,84 @@ +BUILD := release PLATFORM := arduinomega +ARDUINO_PORT := /dev/ttyACM0 + +# override with `make PLATFORM=arduinomega ARDUINOWIFI=1` to enable Arduino WiFi shield +ARDUINOWIFI := 0 + +ifeq ($(ARDUINOWIFI),0) + APP_NAME := ocsocket_test + OCSOCKET_CPPOBJ = ocsocket_arduino.o +else + APP_NAME := ocsocket_wifi_test + OCSOCKET_CPPOBJ = ocsocket_arduino_wifi.o +endif + +OBJ_DIR := ./bin ROOT_DIR = ../../.. + include $(ROOT_DIR)/local.properties +include $(ROOT_DIR)/$(PLATFORM).properties + + +#include directories OCSOCK_DIR = $(ROOT_DIR)/ocsocket LOGGER_DIR = $(ROOT_DIR)/logger -BIN_DIR = $(OCSOCK_DIR)/bin/arduino/ -OBJ_DIR = $(OCSOCK_DIR)/obj/arduino/ +INC_DIRS = -I$(OCSOCK_DIR)/include/ -I$(LOGGER_DIR)/include -#Source directories -SDIR_ARD_CORE = $(ARDUINO_DIR)/hardware/arduino/cores/arduino -SDIR_ARD_SPI = $(ARDUINO_DIR)/libraries/SPI -SDIR_ARD_ETH = $(ARDUINO_DIR)/libraries/Ethernet -SDIR_ARD_ETH_UTIL = $(ARDUINO_DIR)/libraries/Ethernet/utility +VPATH := $(SDIR_ARD_PLATFORM):$(LOGGER_DIR)/src:$(OCSOCK_DIR)/src -VPATH = $(SDIR_ARD_CORE):$(SDIR_ARD_SPI):$(SDIR_ARD_ETH):$(SDIR_ARD_ETH_UTIL):$(OCSOCK_DIR)/src:$(LOGGER_DIR)/src +CFLAGS := -Os -Wall -c -DTB_LOG -#include directories -IDIR_ARD_CORE = $(ARDUINO_DIR)/hardware/arduino/cores/arduino -IDIR_ARD_VARIANT = $(ARDUINO_DIR)/hardware/arduino/variants/mega -IDIR_ARD_SPI = $(ARDUINO_DIR)/libraries/SPI -IDIR_ARD_ETH = $(ARDUINO_DIR)/libraries/Ethernet -IDIR_ARD_ETH_UTIL = $(ARDUINO_DIR)/libraries/Ethernet/utility - -INC_DIRS = -I$(IDIR_ARD_CORE) -I$(IDIR_ARD_VARIANT) -I$(IDIR_ARD_SPI) -I$(IDIR_ARD_ETH) -I$(IDIR_ARD_ETH_UTIL) -I$(OCSOCK_DIR)/include -I$(LOGGER_DIR)/include -I$(ROOT_DIR)/stack/include - -#Compiler/Linker flags -COPTIONS_ARDUINO = -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=156 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -MMD -CFLAGS += -c -Os -Wall -Wno-write-strings -ffunction-sections -fdata-sections -fno-exceptions -felide-constructors -std=c++0x -DATMEGA2560 -DTB_LOG - -#Compilers -CCPLUS=avr-g++ -CC=avr-gcc -AR=avr-ar -RANLIB=avr-ranlib - -TESTAPP = ocsocket_test - -CORE_COBJ = WInterrupts.o wiring.o wiring_digital.o -CORE_CPPOBJ = main.o Stream.o WMath.o WString.o HardwareSerial.o Print.o SPI.o IPAddress.o -ETH_CPPOBJ = Dhcp.o Dns.o Ethernet.o EthernetUdp.o -ETH_UTIL_CPPOBJ = socket.o w5100.o -OCSOCKET_CPPOBJ = logger.o ocsocket_arduino.o - -all: core.a $(TESTAPP).o $(TESTAPP).elf $(TESTAPP).hex - mkdir -p $(BIN_DIR) - cp $(TESTAPP).hex $(BIN_DIR) - mkdir -p $(OBJ_DIR) - cp *.o $(OBJ_DIR) - cp *.elf $(OBJ_DIR) - -%.o: %.c - $(CC) $(CFLAGS) $(COPTIONS_ARDUINO) $(INC_DIRS) $< -o $@ +all: core.a $(APP_NAME).o $(APP_NAME).elf $(APP_NAME).hex -%.o: %.cpp - $(CCPLUS) $(CFLAGS) $(COPTIONS_ARDUINO) $(INC_DIRS) $< -o $@ +core.a: $(PLATFORM_OBJS) + $(AR) rcs $@ $^ + $(RANLIB) $@ #logger needs to be compiled using C++ compiler logger.o: logger.c - $(CCPLUS) $(CFLAGS) $(COPTIONS_ARDUINO) $(INC_DIRS) $< -o $@ + $(CCPLUS) $(CFLAGS) $(CFLAGS_PLATFORM) $(INC_DIRS) $(INC_DIR_PLATFORM) $< -o $@ -core.a: $(CORE_COBJ) $(CORE_CPPOBJ) $(ETH_CPPOBJ) $(ETH_UTIL_CPPOBJ) - $(AR) rcs $@ $^ - $(RANLIB) $@ +%.o: %.c + $(CC) $(CFLAGS) $(CFLAGS_PLATFORM) $(INC_DIRS) $(INC_DIR_PLATFORM) $< -o $@ + +%.o: %.cpp + $(CCPLUS) $(CFLAGS) $(CFLAGS_PLATFORM) $(INC_DIRS) $(INC_DIR_PLATFORM) $< -o $@ -$(TESTAPP).elf: $(TESTAPP).o core.a $(OCSOCKET_CPPOBJ) - $(CC) -Os -Wl,--gc-sections,--relax $(COPTIONS_ARDUINO) $^ -lm -o $@ +$(APP_NAME).elf: $(APP_NAME).o $(OCSOCKET_CPPOBJ) logger.o core.a + $(CC) -Os -Wl,--gc-sections,--relax $(CFLAGS_PLATFORM) $^ -lm -o $@ -$(TESTAPP).hex: $(TESTAPP).elf - avr-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 $< $(TESTAPP).eep - avr-objcopy -O ihex -R .eeprom $< $@ +$(APP_NAME).hex: $(APP_NAME).elf + $(AVR_OBJCOPY) -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 $< $(APP_NAME).eep + $(AVR_OBJCOPY) -O ihex -R .eeprom $< $@ install: all - avrdude -C$(ARDUINO_DIR)/hardware/tools/avrdude.conf -v -v -v -v -patmega2560 -cstk500v2 -P/dev/ttyACM0 -b115200 -D -Uflash:w:$(TESTAPP).hex:i + $(AVR_PROGRAMMER) -C$(ARDUINO_DIR)/hardware/tools/avrdude.conf -v -v -v -v -patmega2560 -cstk500v2 -P$(ARDUINO_PORT) -b115200 -D -Uflash:w:$(APP_NAME).hex:i .PHONY: clean clean: - rm -f *.o *.d *.elf *.eep *.a *.hex *- - rm -fr $(BIN_DIR) - rm -fr $(OBJ_DIR) + @rm -f *.o *.d *.elf *.eep *.a *.hex *.bin *.map *- + @rm -rf $(OBJ_DIR) + + + + + + + + + + + + + + + + + + + + diff --git a/csdk/ocsocket/test/arduino/ocsocket_wifi_test.cpp b/csdk/ocsocket/test/arduino/ocsocket_wifi_test.cpp new file mode 100644 index 0000000..0138afb --- /dev/null +++ b/csdk/ocsocket/test/arduino/ocsocket_wifi_test.cpp @@ -0,0 +1,204 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include +#include +#include +#include +#include + +/// WiFi Shield firmware with Intel patches +static const char INTEL_WIFI_SHIELD_FW_VER[] = "1.2.0"; + +/// IP address of the peer whom I wish to send/recv some data +static uint8_t PEER_IP_ADDR[] = {192, 168, 1, 132}; + +/// Port number of the peer whom I wish to send/recv some data +#define TEST_PORT_NUM (4097) + +/// Max buffer size +#define MAX_BUF_SIZE (1024) + +/// Some test bytes to send to the peer +unsigned char TEST_BUF[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f, + 0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74, + 0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f, + 0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f, + 0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74, + 0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f, + 0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f, + 0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74, + 0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f, + 0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f, + 0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74, + 0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f, + 0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f, + 0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74, + 0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f, + 0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f, + 0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74, + 0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f, + 0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f, + 0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74, + 0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f, + 0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f, + 0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74, + 0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f, + 0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01, +}; +unsigned int TEST_BUF_LEN = sizeof(TEST_BUF); + +#define MOD_NAME PCF("ocsocket_test") + +#define VERIFY_SUCCESS(op, res) { if (op == res) {OC_LOG(DEBUG, MOD_NAME, PCF(#op " SUCCEEDED"));} \ + else {OC_LOG(FATAL, MOD_NAME, PCF(#op "!!!! FAILED FAILED FAILED !!!!"));} } + +/// WiFi network info and credentials +char ssid[] = "mDNSAP"; +char pass[] = "letmein9"; +int status = WL_IDLE_STATUS; + +//Start sample app 'sendrecv' on different Linux box. +void SendReceiveTest() +{ + Serial.println("entering SendReceiveTest..."); + OCDevAddr ipAddr, peerAddr, recvAddr; + int32_t sfd; + int32_t recvLen; + uint8_t buf[MAX_BUF_SIZE]; + uint16_t recvPort; + + VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS); + VERIFY_SUCCESS(OCBuildIPv4Address(PEER_IP_ADDR[0], PEER_IP_ADDR[1], + PEER_IP_ADDR[2], PEER_IP_ADDR[3], TEST_PORT_NUM, &peerAddr), ERR_SUCCESS); + VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd), ERR_SUCCESS); + OC_LOG(DEBUG, MOD_NAME, PCF("Peer Addr :")); + OC_LOG_BUFFER(INFO, MOD_NAME, peerAddr.addr, peerAddr.size); + for (int i = 0; i < 300; i++) + { + + OC_LOG_V(DEBUG, MOD_NAME, "--------------------- i --------------- %d", i); + VERIFY_SUCCESS(OCSendTo(sfd, TEST_BUF, i + 10, 0, &peerAddr), i+10); + + delay(2000); + recvLen = OCRecvFrom(sfd, buf, MAX_BUF_SIZE, 0, &recvAddr); + if (recvLen > 0) + { + OC_LOG(DEBUG, MOD_NAME, PCF("Rcvd data from :")); + OC_LOG_BUFFER(INFO, MOD_NAME, recvAddr.addr, recvAddr.size); + OCDevAddrToPort(&recvAddr, &recvPort); + OC_LOG_V(DEBUG, MOD_NAME, "Recv Data from Port %hu", recvPort); + OC_LOG(DEBUG, MOD_NAME, PCF("Data Length :")); + OC_LOG_BUFFER(INFO, MOD_NAME, (uint8_t*)&recvLen, sizeof(recvLen)); + OC_LOG(DEBUG, MOD_NAME, PCF("Data :")); + if (recvLen < 255) + { + OC_LOG_BUFFER(INFO, MOD_NAME, buf, recvLen); + } + else + { + int idx = 0; + int rem = recvLen; + do { + if (rem > 255) + { + OC_LOG_BUFFER(INFO, MOD_NAME, buf + idx, 255); + rem = rem - 255; + } + else + { + OC_LOG_BUFFER(INFO, MOD_NAME, buf + idx, rem); + rem = 0; + } + idx = idx + 255; + }while(rem > 0); + } + + } + } + VERIFY_SUCCESS(OCClose( sfd), ERR_SUCCESS); + OC_LOG(DEBUG, MOD_NAME, PCF("WifiTest - Completed")); + +} +void setup() +{ + Serial.begin(115200); + Serial.println("WiFi test starting..."); + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) + { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + + Serial.print("WiFi version: "); + Serial.println(WiFi.firmwareVersion()); + + if (strcmp(WiFi.firmwareVersion(), INTEL_WIFI_SHIELD_FW_VER) != 0) + { + Serial.println("!!!!! Upgrade WiFi Shield Firmware version !!!!!!"); + } + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + status = WiFi.begin(ssid,pass); + + // wait 10 seconds for connection: + delay(10000); + } + Serial.println("Connected to wifi"); + + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); +} + +void loop() { + Serial.println("entering loop..."); + SendReceiveTest(); + delay(5000); + + do { + OC_LOG(DEBUG, MOD_NAME, PCF("All tests Completed")); + delay(10000); + } while (1); +} + + diff --git a/csdk/stack/samples/arduino/SimpleClientServer/ocserver/README b/csdk/stack/samples/arduino/SimpleClientServer/ocserver/README index 84c30d3..9a8f240 100644 --- a/csdk/stack/samples/arduino/SimpleClientServer/ocserver/README +++ b/csdk/stack/samples/arduino/SimpleClientServer/ocserver/README @@ -29,6 +29,9 @@ To make & install ocserver in debug mode, with specified: make install ARDUINO_PORT= BUILD=debug +To make ocserver_wifi with Arduino WiFi Shield support: + + make ARDUINOWIFI=1 (arduinomega) ------------------------------------------------------------------------------- **Clean-Up** ------------------------------------------------------------------------------- diff --git a/csdk/stack/samples/arduino/SimpleClientServer/ocserver/makefile b/csdk/stack/samples/arduino/SimpleClientServer/ocserver/makefile index c587860..7f5179f 100644 --- a/csdk/stack/samples/arduino/SimpleClientServer/ocserver/makefile +++ b/csdk/stack/samples/arduino/SimpleClientServer/ocserver/makefile @@ -1,8 +1,13 @@ -APP_NAME := ocserver + BUILD := release PLATFORM := arduinomega ARDUINO_PORT := /dev/ttyACM0 +# override with `make PLATFORM=arduinomega ARDUINOWIFI=1` to enable Arduino WiFi shield +ARDUINOWIFI := 0 + +APP_NAME := ocserver + OBJ_DIR := ./bin ROOT_DIR = ../../../../.. @@ -23,6 +28,10 @@ INC_DIRS = -I$(OCSOCK_DIR)/include/ -I$(LOGGER_DIR)/include -I$(STACK_DIR)/inclu CFLAGS := -Os -Wall -c -DTB_LOG +ifeq ($(ARDUINOWIFI),1) + CFLAGS += -DARDUINOWIFI +endif + all: prep_dirs core.a $(APP_NAME).o $(APP_NAME).elf $(APP_NAME).hex core.a: $(PLATFORM_OBJS) diff --git a/csdk/stack/samples/arduino/SimpleClientServer/ocserver/ocserver.cpp b/csdk/stack/samples/arduino/SimpleClientServer/ocserver/ocserver.cpp index 844af57..069390a 100644 --- a/csdk/stack/samples/arduino/SimpleClientServer/ocserver/ocserver.cpp +++ b/csdk/stack/samples/arduino/SimpleClientServer/ocserver/ocserver.cpp @@ -1,3 +1,23 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + // Do not remove the include below #include "Arduino.h" @@ -5,6 +25,13 @@ #include "ocstack.h" #include +#ifdef ARDUINOWIFI +// Arduino WiFi Shield +#include +#include +#include +#else +// Arduino Ethernet Shield #include #include #include @@ -12,18 +39,12 @@ #include #include #include - -#ifdef __cplusplus -extern "C" { #endif const char *getResult(OCStackResult result); -#define PCF(str) ((PROGMEM const char *)(F(str))) - PROGMEM const char TAG[] = "ArduinoServer"; -int gQuitFlag = 0; int gLEDUnderObservation = 0; void createLEDResource(); typedef struct LEDRESOURCE{ @@ -36,8 +57,77 @@ static LEDResource LED; static char responsePayloadGet[] = "{\"href\":\"/a/led\",\"rep\":{\"state\":\"on\",\"power\":10}}"; static char responsePayloadPut[] = "{\"href\":\"/a/led\",\"rep\":{\"state\":\"off\",\"power\":0}}"; + +/// This is the port which Arduino Server will use for all unicast communication with it's peers static uint16_t OC_WELL_KNOWN_PORT = 5683; +#ifdef ARDUINOWIFI +// Arduino WiFi Shield +// Note : Arduino WiFi Shield currently does NOT support multicast and therefore +// this server will NOT be listening on 224.0.1.187 multicast address. + +/// WiFi Shield firmware with Intel patches +static const char INTEL_WIFI_SHIELD_FW_VER[] = "1.2.0"; + +/// WiFi network info and credentials +char ssid[] = "mDNSAP"; +char pass[] = "letmein9"; + +int ConnectToNetwork() +{ + char *fwVersion; + int status = WL_IDLE_STATUS; + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) + { + OC_LOG(ERROR, TAG, PCF("WiFi shield not present")); + return -1; + } + + // Verify that WiFi Shield is running the firmware with all UDP fixes + fwVersion = WiFi.firmwareVersion(); + OC_LOG_V(INFO, TAG, "WiFi Shield Firmware version %s", fwVersion); + if ( strncmp(fwVersion, INTEL_WIFI_SHIELD_FW_VER, sizeof(INTEL_WIFI_SHIELD_FW_VER)) !=0 ) + { + OC_LOG(DEBUG, TAG, PCF("!!!!! Upgrade WiFi Shield Firmware version !!!!!!")); + return -1; + } + + // attempt to connect to Wifi network: + while (status != WL_CONNECTED) + { + OC_LOG_V(INFO, TAG, "Attempting to connect to SSID: %s", ssid); + status = WiFi.begin(ssid,pass); + + // wait 10 seconds for connection: + delay(10000); + } + OC_LOG(DEBUG, TAG, PCF("Connected to wifi")); + + IPAddress ip = WiFi.localIP(); + OC_LOG_V(INFO, TAG, "IP Address: %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); + return 0; +} +#else +// Arduino Ethernet Shield +int ConnectToNetwork() +{ + // Note: ****Update the MAC address here with your shield's MAC address**** + uint8_t ETHERNET_MAC[] = {0x90, 0xA2, 0xDA, 0x0E, 0xC4, 0x05}; + uint8_t error = Ethernet.begin(ETHERNET_MAC); + if (error == 0) + { + OC_LOG_V(ERROR, TAG, "error is: %d", error); + return -1; + } + OC_LOG_V(INFO, TAG, "IPAddress : %s", Serial.print(Ethernet.localIP())); + return 0; +} +#endif //ARDUINOWIFI + + +// This is the entity handler for the registered resource. +// This is invoked by OCStack whenever it recevies a request for this resource. OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest ) { OCEntityHandlerResult ehRet = OC_EH_OK; @@ -93,6 +183,8 @@ OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandle return ehRet; } + +// This method is used to display 'Observe' functionality of OC Stack. static uint8_t modCounter = 0; void *ChangeLEDRepresentation (void *param) { @@ -115,6 +207,8 @@ void *ChangeLEDRepresentation (void *param) return NULL; } + + //The setup function is called once at startup of the sketch void setup() { @@ -124,25 +218,21 @@ void setup() OC_LOG(DEBUG, TAG, PCF("OCServer is starting...")); uint16_t port = OC_WELL_KNOWN_PORT; - //Mac address of my ethernet shield - uint8_t ETHERNET_MAC[] = {0x90, 0xA2, 0xDA, 0x0E, 0xC4, 0x05}; - uint8_t error = Ethernet.begin(ETHERNET_MAC); - Serial.print(Ethernet.localIP()); - if (error == 0) + // Connect to Ethernet or WiFi network + if (ConnectToNetwork() != 0) { - OC_LOG_V(ERROR, TAG, "error is: %d", error); + OC_LOG(ERROR, TAG, "Unable to connect to network"); return; } + // Initialize the OC Stack in Server mode if (OCInit(NULL, port, OC_SERVER) != OC_STACK_OK) { OC_LOG(ERROR, TAG, PCF("OCStack init error")); return; } - /* - * Declare and create the example resource: LED - */ + // Declare and create the example resource: LED createLEDResource(); } @@ -150,7 +240,10 @@ void setup() // The loop function is called in an endless loop void loop() { + // This artificial delay is kept here to avoid endless spinning + // of Arduino microcontroller. Modify it as per specfic application needs. delay(2000); + if (OCProcess() != OC_STACK_OK) { OC_LOG(ERROR, TAG, PCF("OCStack process error")); @@ -164,7 +257,7 @@ void createLEDResource() LED.state = false; OCStackResult res = OCCreateResource(&LED.handle, "core.led", - "core.rw", + "oc.mi.def", "/a/led", OCEntityHandlerCb, OC_DISCOVERABLE|OC_OBSERVABLE); @@ -209,6 +302,3 @@ const char *getResult(OCStackResult result) { return "UNKNOWN"; } } -#ifdef __cplusplus -} // extern "C" -#endif -- 2.7.4