Commit 82699c98 authored by Jihun Ha's avatar Jihun Ha Committed by Madan Lanka

Add an initial prototype of Multi-Phy Easy Setup service for Arduino device

It provides the service library and an sample application using the library.
Arduino Mega board and WiFi shield are used for functionlity test.

Change-Id: Iadb19361db8a777b84437ad978887dedeb2e6e63
Signed-off-by: default avatarJihun Ha <jihun.ha@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1189Reviewed-by: default avatarMadan Lanka <lanka.madan@samsung.com>
Tested-by: default avatarMadan Lanka <lanka.madan@samsung.com>
parent 29156153
......@@ -59,7 +59,7 @@ else:
env.SConscript('./src/SConscript')
if build_sample == 'ON':
if target_os in ['linux', 'arduino', 'android']:
env.SConscript('./samples/' + target_os + '/SConscript')
#if build_sample == 'ON':
# if target_os in ['linux', 'arduino', 'android']:
# env.SConscript('./samples/' + target_os + '/SConscript')
......@@ -26,7 +26,7 @@ Import('env')
target_os = env.get('TARGET_OS')
if target_os not in ['arduino','darwin','ios']:
if target_os not in ['arduino','darwin']:
# Build things manager project
SConscript('things-manager/SConscript')
......@@ -35,7 +35,7 @@ if target_os not in ['arduino','darwin','ios']:
# Build protocol plugin project
# protocol-plugin use 'inotify', this feature isn't support by MAC OSX
if target_os not in ['darwin', 'ios']:
if target_os not in ['darwin', 'ios', 'android']:
SConscript('protocol-plugin/SConscript')
# Build notification manager project
......@@ -43,3 +43,5 @@ if target_os not in ['arduino','darwin','ios']:
#else:
# SConscript('notification-manager/SampleApp/arduino/SConscript')
if target_os == 'arduino':
SConscript('easy-setup/SConscript')
\ No newline at end of file
#******************************************************************
#
# Copyright 2014 Samsung Electronics 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.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
##
# easy-setup project build script
##
import os
Import('env')
# Add third party libraries
lib_env = env.Clone()
#SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', exports = 'lib_env')
easy_setup_env = lib_env.Clone()
target_os = env.get('TARGET_OS')
######################################################################
# Build flags
######################################################################
easy_setup_env.AppendUnique(CPPPATH = ['sdk/inc', 'sdk/src'])
if target_os not in ['windows', 'winrt']:
easy_setup_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall'])
if target_os not in ['android', 'arduino']:
easy_setup_env.AppendUnique(CXXFLAGS = ['-pthread'])
if target_os == 'android':
easy_setup_env.AppendUnique(CXXFLAGS = ['-frtti', '-fexceptions'])
easy_setup_env.PrependUnique(LIBS = ['oc', 'octbstack', 'gnustl_shared'])
if target_os == 'arduino':
easy_setup_env.AppendUnique(CPPPATH = [
'../../resource/oc_logger/include',
'../../resource/csdk/logger/include',
'../../resource/csdk/stack/include',
'../../extlibs/cjson',
'sdk/arduino/wifi/inc',
'sdk/arduino/wifi/src' ])
######################################################################
# Source files and Targets
######################################################################
#if target_os == 'arduino':
es_src = env.Glob('sdk/src/*.cpp')
es_src += env.Glob('sdk/arduino/wifi/src/*.cpp')
es_sdk_static = easy_setup_env.StaticLibrary('ESSDKLibrary', es_src)
easy_setup_env.InstallTarget(es_sdk_static, 'libESSDK')
if target_os == 'android':
es_src = env.Glob('sdk/src/*.cpp')
es_src += env.Glob('sdk/android/wifi/src/*.cpp')
es_sdk_static = easy_setup_env.StaticLibrary('ESLib', es_src)
es_sdk_shared = easy_setup_env.SharedLibrary('ESLib', es_src)
easy_setup_env.InstallTarget([es_sdk_static, es_sdk_shared], 'libESSDK')
# Build JNI layer
#if target_os == 'android':
# SConscript(os.path.join('sdk', 'java', 'jni', 'SConscript'))
#Go to build sample apps
SConscript('sampleapp/SConscript')
#******************************************************************
#
# Copyright 2014 Samsung Electronics 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.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
##
# sampleapp build script
##
Import('env')
# Add third party libraries
lib_env = env.Clone()
#SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env')
sample_env = lib_env.Clone()
target_os = env.get('TARGET_OS')
######################################################################
# Build flags
######################################################################
######################################################################
# Source files and Targets
######################################################################
if target_os == 'arduino' :
# Build linux sample app
SConscript('arduino/thinserver/SConscript')
#******************************************************************
#
# 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.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Import('env')
thinserver_env = env.Clone()
######################################################################
# Build flags
######################################################################
thinserver_env.PrependUnique(CPPPATH = [
'../../../../../resource/oc_logger/include',
'../../../../../resource/csdk/logger/include',
'../../../../../resource/csdk/stack/include',
'../../../../../extlibs/cjson',
'../../../sdk/inc',
'../../../sdk/arduino/wifi/inc'
])
thinserver_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
thinserver_env.AppendUnique(CPPDEFINES = ['TB_LOG', 'ARDUINOWIFI'])
thinserver_env.PrependUnique(LIBS = ['octbstack', 'connectivity_abstraction','coap', 'ESSDKLibrary'])
thinserver = thinserver_env.Program('thinserver', 'thinserver.cpp')
env.CreateBin('thinserver')
i_thinserver = thinserver_env.Install(env.get('BUILD_DIR'), thinserver)
Alias('thinserver', i_thinserver)
env.AppendTarget('thinserver')
if(thinserver_env['UPLOAD'] == True):
from sys import platform as _platform
if _platform == "linux" or _platform == "linux2":
thinserver_env.Upload(env.get('BUILD_DIR') + '/service/easy-setup/sampleapp/arduino/thinserver/thinserver.hex')
else:
print 'Please use appropriate install method for your developing machine. Linux is the only supported platform right now.'
//******************************************************************
//
// Copyright 2014 Samsung Electronics 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"
#include "logger.h"
#include "ocstack.h"
#include <string.h>
#ifdef ARDUINOWIFI
// Arduino WiFi Shield
#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#else
// Arduino Ethernet Shield
#include <EthernetServer.h>
#include <Ethernet.h>
#include <Dns.h>
#include <EthernetClient.h>
#include <util.h>
#include <EthernetUdp.h>
#include <Dhcp.h>
#endif
#include "easysetup.h"
const char *getResult(OCStackResult result);
PROGMEM const char TAG[] = "ArduinoServer";
void EventCallbackInApp(ES_RESULT eventFlag)
{
Serial.println("callback!!! in app");
}
// On Arduino Atmel boards with Harvard memory architecture, the stack grows
// downwards from the top and the heap grows upwards. This method will print
// the distance(in terms of bytes) between those two.
// See here for more details :
// http://www.atmel.com/webdoc/AVRLibcReferenceManual/malloc_1malloc_intro.html
void PrintArduinoMemoryStats()
{
#ifdef ARDUINO_AVR_MEGA2560
//This var is declared in avr-libc/stdlib/malloc.c
//It keeps the largest address not allocated for heap
extern char *__brkval;
//address of tmp gives us the current stack boundry
int tmp;
OC_LOG_V(INFO, TAG, "Stack: %u Heap: %u", (unsigned int)&tmp, (unsigned int)__brkval);
OC_LOG_V(INFO, TAG, "Unallocated Memory between heap and stack: %u",
((unsigned int)&tmp - (unsigned int)__brkval));
#endif
}
//The setup function is called once at startup of the sketch
void setup()
{
// Add your initialization code here
// Note : This will initialize Serial port on Arduino at 115200 bauds
OC_LOG_INIT();
OC_LOG(DEBUG, TAG, PCF("OCServer is starting..."));
WaitingForOnboarding(ES_WIFI, EventCallbackInApp);
// Initialize the OC Stack in Server mode
if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
{
OC_LOG(ERROR, TAG, PCF("OCStack init error"));
return;
}
PrepareToProvisioning(EventCallbackInApp);
if (OCStartPresence(0) != OC_STACK_OK)
{
OC_LOG(ERROR, TAG, PCF("Start Presence init error"));
return;
}
}
// 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 specific application needs.
delay(2000);
// This call displays the amount of free SRAM available on Arduino
PrintArduinoMemoryStats();
// Give CPU cycles to OCStack to perform send/recv and other OCStack stuff
if (OCProcess() != OC_STACK_OK)
{
OC_LOG(ERROR, TAG, PCF("OCStack process error"));
return;
}
//ChangeLightRepresentation(NULL);
}
//******************************************************************
//
// Copyright 2014 Samsung Electronics 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"
#include "logger.h"
#include "ocstack.h"
#include <string.h>
#ifdef ARDUINOWIFI
// Arduino WiFi Shield
#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#else
// Arduino Ethernet Shield
#include <EthernetServer.h>
#include <Ethernet.h>
#include <Dns.h>
#include <EthernetClient.h>
#include <util.h>
#include <EthernetUdp.h>
#include <Dhcp.h>
#endif
#include "common.h"
#ifndef ES_NETWORK_HANDLER_H_
#define ES_NETWORK_HANDLER_H_
#define MAXSSIDLEN 33
#define MAXNETCREDLEN 20
#define MAXNUMTYPE 5
#define MAXADDRLEN 15
typedef void (*NetworkEventCallback)(ES_RESULT);
enum NetworkType
{
ES_WIFI = 1, ES_BT = 2, ES_BLE = 3, ES_ZIGBEE = 4, ES_ETH = 5
};
typedef struct NETWORKINFO
{
NetworkType type;
// for WiFI
IPAddress ipaddr;
char ssid[MAXSSIDLEN];
// for BT, BLE
byte mac[6];
} NetworkInfo;
ES_RESULT ConnectToWiFiNetworkForOnboarding(char *ssid, char *pass, NetworkEventCallback);
int getCurrentNetworkInfo(NetworkType targetType, NetworkInfo *info);
#endif
//******************************************************************
//
// Copyright 2014 Samsung Electronics 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 "networkHandler.h"
PROGMEM const char TAG[] = "ArduinoServer";
int findNetwork(char *ssid);
int ConnectToNetwork(char *ssid, char *pass);
void printEncryptionType(int thisType);
// 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.
static const char ARDUINO_WIFI_SHIELD_UDP_FW_VER[] = "1.1.0";
IPAddress myIP;
ES_RESULT ConnectToWiFiNetworkForOnboarding(char *ssid, char *pass, NetworkEventCallback cb)
{
char *fwVersion;
int status = WL_IDLE_STATUS;
int res;
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD)
{
OC_LOG(ERROR, TAG, PCF("WiFi shield not present"));
return ES_ERROR;
}
// 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, ARDUINO_WIFI_SHIELD_UDP_FW_VER, sizeof(ARDUINO_WIFI_SHIELD_UDP_FW_VER))
!= 0)
{
OC_LOG(DEBUG, TAG, PCF("!!!!! Upgrade WiFi Shield Firmware version !!!!!!"));
return ES_ERROR;
}
while (findNetwork(ssid) == 0) // found
{
delay(2000);
}
if (cb != NULL)
{
cb(ES_NETWORKFOUND);
}
if (WiFi.status() == WL_CONNECTED)
WiFi.disconnect();
res = ConnectToNetwork(ssid, pass);
if (res == 0)
{
return ES_NETWORKCONNECTED;
}
else
{
return ES_NETWORKNOTCONNECTED;
}
}
int findNetwork(char *ssid)
{
int res = 0;
// scan for nearby networks:
Serial.println("** Scan Networks **");
int numSsid = WiFi.scanNetworks();
if (numSsid == -1)
{
Serial.println("Couldn't get a wifi connection");
return res;
}
// print the list of networks seen:
Serial.print("number of available networks:");
Serial.println(numSsid);
// print the network number and name for each network found:
for (int thisNet = 0; thisNet < numSsid; thisNet++)
{
Serial.print(thisNet);
Serial.print(") ");
Serial.print(WiFi.SSID(thisNet));
Serial.print("\tEncryption: ");
printEncryptionType(WiFi.encryptionType(thisNet));
if (strcmp(WiFi.SSID(thisNet), ssid) == 0)
{
res = 1;
}
}
return res;
}
int ConnectToNetwork(char *ssid, char *pass)
{
int status = WL_IDLE_STATUS;
//IPAddress desiredIP(192, 168, 43, 2);
//IPAddress desiredIP(192, 168, 0, 115);
//WiFi.config(desiredIP);
// 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"));
myIP = WiFi.localIP();
OC_LOG_V(INFO, TAG, "IP Address: %d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]);
char buf[50];
sprintf(buf, "IP Address: %d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]);
Serial.println(buf);
return 0;
}
int getCurrentNetworkInfo(NetworkType targetType, NetworkInfo *info)
{
if (targetType == ES_WIFI && WiFi.status() == WL_CONNECTED)
{
info->type = ES_WIFI;
info->ipaddr = WiFi.localIP();
strcpy(info->ssid, WiFi.SSID());
return 0;
}
return -1;
}
void printEncryptionType(int thisType)
{
// read the encryption type and print out the name:
switch (thisType)
{
case ENC_TYPE_WEP:
Serial.println("WEP");
break;
case ENC_TYPE_TKIP:
Serial.println("WPA");
break;
case ENC_TYPE_CCMP:
Serial.println("WPA2");
break;
case ENC_TYPE_NONE:
Serial.println("None");
break;
case ENC_TYPE_AUTO:
Serial.println("Auto");
break;
}
}
//******************************************************************
//
// Copyright 2014 Samsung Electronics 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.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#ifndef ES_COMMON_H_
#define ES_COMMON_H_
typedef enum
{
ES_ERROR = -1,
ES_OK = 0,
ES_NETWORKFOUND = 1,
ES_NETWORKCONNECTED,
ES_NETWORKNOTCONNECTED,
ES_RESOURCECREATED = 11,
ES_RECVREQOFPROVRES = 21,
ES_RECVREQOFNETRES,
ES_RECVUPDATEOFPROVRES,