Commit f41ebdf1 authored by Jihun Ha's avatar Jihun Ha Committed by Uze Choi

Update the folder structure for Enrollee implmentation.

I have tried to make the relationship of folders be platform independent
and connectivity independent. And I have seperated the folders into Enrollee
and Mediator so that one can understand what the folder contains easily.

Additionally, I have changed the names of some functions to be expressed more clear.

Change-Id: I126d388178d473b56812fb597441ab4c481ec721
Signed-off-by: default avatarJihun Ha <jihun.ha@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1280Reviewed-by: default avatarMadan Lanka <lanka.madan@samsung.com>
Tested-by: default avatarUze Choi <uzchoi@samsung.com>
parent edc74860
......@@ -38,7 +38,7 @@ if target_os == 'android':
######################################################################
# Build flags
######################################################################
easy_setup_env.AppendUnique(CPPPATH = ['sdk/inc', 'sdk/src'])
easy_setup_env.AppendUnique(CPPPATH = ['sdk/inc', 'sdk/src', 'sdk/common'])
if target_os not in ['windows', 'winrt']:
easy_setup_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall'])
......@@ -64,8 +64,9 @@ if target_os == 'arduino':
'../../resource/csdk/logger/include',
'../../resource/csdk/stack/include',
'../../extlibs/cjson',
'sdk/arduino/wifi/inc',
'sdk/arduino/wifi/src' ])
'sdk/enrollee/arduino/wifi',
'sdk/enrollee/common/src',
'sdk/enrollee/common/inc'])
if target_os in ['android','linux']:
easy_setup_env.PrependUnique(CPPPATH = [
......@@ -84,9 +85,9 @@ if target_os in ['android','linux']:
if target_os == 'arduino':
es_sdk_static = easy_setup_env.StaticLibrary('ESSDKLibrary',
['sdk/src/easysetup.cpp',
'sdk/src/resourceHandler.cpp',
'sdk/arduino/wifi/src/networkHandler.cpp'])
['sdk/enrollee/common/src/easysetup.cpp',
'sdk/enrollee/common/src/resourceHandler.cpp',
'sdk/enrollee/arduino/wifi/networkHandler.cpp'])
easy_setup_env.InstallTarget(es_sdk_static, 'libESSDK')
......
......@@ -29,8 +29,10 @@ thinserver_env.PrependUnique(CPPPATH = [
'../../../../../resource/csdk/logger/include',
'../../../../../resource/csdk/stack/include',
'../../../../../extlibs/cjson',
'../../../sdk/inc',
'../../../sdk/arduino/wifi/inc'
'../../../sdk/common',
'../../../sdk/enrollee/arduino/wifi',
'../../../sdk/enrollee/common/inc',
'../../../sdk/enrollee/common/src'
])
thinserver_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
......
......@@ -45,7 +45,10 @@
const char *getResult(OCStackResult result);
PROGMEM const char TAG[] = "ArduinoServer";
PROGMEM const char TAG[] = "ThinServer";
char ssid[] = "hub2.4G";
char passwd[] = "09877890";
void EventCallbackInApp(ES_RESULT eventFlag)
{
......@@ -78,7 +81,7 @@ void setup()
OC_LOG_INIT();
OC_LOG(DEBUG, TAG, PCF("OCServer is starting..."));
WaitingForOnboarding(ES_WIFI, EventCallbackInApp);
FindNetworkForOnboarding(ES_WIFI, ssid, passwd, EventCallbackInApp);
// Initialize the OC Stack in Server mode
if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
......@@ -87,7 +90,7 @@ void setup()
return;
}
PrepareToProvisioning(EventCallbackInApp);
InitializeProvisioning(EventCallbackInApp);
if (OCStartPresence(0) != OC_STACK_OK)
{
......@@ -112,5 +115,4 @@ void loop()
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.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include "networkHandler.h"
PROGMEM const char TAG[] = "networkHandler";
int findNetwork(const char *ssid);
int ConnectToNetwork(const char *ssid, const 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 ConnectToWiFiNetwork(const char *ssid, const 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(1000);
}
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(const 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(const char *ssid, const char *pass)
{
int status = WL_IDLE_STATUS;
// 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((char *) ssid, (char *) 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.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Do not remove the include below
#include "Arduino.h"
#include "logger.h"
#include "ocstack.h"
#include <string.h>
// Arduino WiFi Shield
#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#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 ConnectToWiFiNetwork(const char *ssid, const char *pass, NetworkEventCallback);
int getCurrentNetworkInfo(NetworkType targetType, NetworkInfo *info);
#endif
......@@ -25,22 +25,6 @@
#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"
#include "networkHandler.h"
#include "resourceHandler.h"
......@@ -49,8 +33,11 @@ typedef void (*EventCallback)(ES_RESULT);
OCStackResult Init();
ES_RESULT WaitingForOnboarding(NetworkType networkType, EventCallback);
//OCStackResult WaitingForOnboarding(NetworkType networkType, char *name);
//OCStackResult WaitingForOnboarding(NetworkType networkType, char *name, char *pass);
ES_RESULT FindNetworkForOnboarding(NetworkType networkType, EventCallback);
ES_RESULT FindNetworkForOnboarding(NetworkType networkType, const char *name, const char *pass,
EventCallback);
//OCStackResult FindNetworkForOnboarding(NetworkType networkType, char *name);
//OCStackResult FindNetworkForOnboarding(NetworkType networkType, char *name, char *pass);
ES_RESULT PrepareToProvisioning(EventCallback);
ES_RESULT InitializeProvisioning(EventCallback);
......@@ -25,22 +25,6 @@
#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"
#include "networkHandler.h"
#include "octypes.h"
......@@ -70,9 +54,9 @@ typedef struct NETRESOURCE
char cnn[MAXSSIDLEN]; // current network name
} NetResource;
OCStackResult createProvisioningResource();
OCStackResult createNetworkResource();
void getTargetNetworkInfoFromProvResource(char *, char *);
OCStackResult CreateProvisioningResource();
OCStackResult CreateNetworkResource();
void GetTargetNetworkInfoFromProvResource(char *, char *);
void RegisterResourceEventCallBack(ResourceEventCallback);
#endif
......@@ -20,8 +20,6 @@
#include "easysetup.h"
PROGMEM const char TAG[] = "ArduinoServer";
/// WiFi network info and credentials
char defaultSsid[] = "EasyConnect";
char defaultPass[] = "EasyConnect";
......@@ -40,8 +38,6 @@ void EventCallbackAfterProvisioning(ES_RESULT event);
void EventCallbackInOnboarding(ES_RESULT event)
{
Serial.println("[Callback] in onboarding process");
if (event == ES_NETWORKFOUND || event == ES_NETWORKCONNECTED)
{
if (g_cbForOnboarding != NULL)
......@@ -55,17 +51,14 @@ void EventCallbackInProvisioning(ES_RESULT event)
{
ES_RESULT res = ES_OK;
Serial.println("[Callback] in provisioning process");
if (event == ES_RECVTRIGGEROFPROVRES)
{
targetSsid = (char *) malloc(MAXSSIDLEN);
targetPass = (char *) malloc(MAXNETCREDLEN);
getTargetNetworkInfoFromProvResource(targetSsid, targetPass);
GetTargetNetworkInfoFromProvResource(targetSsid, targetPass);
res = ConnectToWiFiNetworkForOnboarding(targetSsid, targetPass,
EventCallbackAfterProvisioning);
res = ConnectToWiFiNetwork(targetSsid, targetPass, EventCallbackAfterProvisioning);
if (g_cbForProvisioning != NULL)
{
......@@ -76,8 +69,6 @@ void EventCallbackInProvisioning(ES_RESULT event)
void EventCallbackAfterProvisioning(ES_RESULT event)
{
Serial.println("[Callback] after provisioning process");
if (event == ES_NETWORKFOUND || event == ES_NETWORKCONNECTED)
{
if (g_cbForProvisioning != NULL)
......@@ -87,7 +78,7 @@ void EventCallbackAfterProvisioning(ES_RESULT event)
}
}
ES_RESULT WaitingForOnboarding(NetworkType networkType, EventCallback cb)
ES_RESULT FindNetworkForOnboarding(NetworkType networkType, EventCallback cb)
{
if (networkType == ES_WIFI)
{
......@@ -96,12 +87,30 @@ ES_RESULT WaitingForOnboarding(NetworkType networkType, EventCallback cb)
g_cbForOnboarding = cb;
}
return ConnectToWiFiNetworkForOnboarding(defaultSsid, defaultPass,
EventCallbackInOnboarding);
return ConnectToWiFiNetwork(defaultSsid, defaultPass, EventCallbackInOnboarding);
}
}
ES_RESULT FindNetworkForOnboarding(NetworkType networkType, const char *ssid, const char *passwd,
EventCallback cb)
{
if (!ssid || !passwd)
{
return ES_ERROR;
}
if (networkType == ES_WIFI)
{
if (g_cbForOnboarding == NULL)
{
g_cbForOnboarding = cb;
}
return ConnectToWiFiNetwork(ssid, passwd, EventCallbackInOnboarding);
}
}
ES_RESULT PrepareToProvisioning(EventCallback cb)
ES_RESULT InitializeProvisioning(EventCallback cb)
{
if (cb == NULL)
{
......@@ -112,12 +121,12 @@ ES_RESULT PrepareToProvisioning(EventCallback cb)
g_cbForProvisioning = cb;
}
if (createProvisioningResource() != OC_STACK_OK)
if (CreateProvisioningResource() != OC_STACK_OK)
{
return ES_ERROR;
}
if (createNetworkResource() != OC_STACK_OK)
if (CreateNetworkResource() != OC_STACK_OK)
{
return ES_ERROR;
}
......
......@@ -21,7 +21,7 @@
#include "resourceHandler.h"
#include "cJSON.h"
PROGMEM const char TAG[] = "ArduinoServer";
PROGMEM const char TAG[] = "resourceHandler";
ProvResource g_prov;
NetResource g_net;
......@@ -46,7 +46,7 @@ void RegisterResourceEventCallBack(ResourceEventCallback cb)
g_cbForResEvent = cb;
}
void getTargetNetworkInfoFromProvResource(char *name, char *pass)
void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
{
if (name != NULL && pass != NULL)
{
......@@ -55,7 +55,7 @@ void getTargetNetworkInfoFromProvResource(char *name, char *pass)
}
}
OCStackResult createProvisioningResource()
OCStackResult CreateProvisioningResource()
{
g_prov.ps = 1; // need to provisioning
g_prov.tnt = ES_WIFI;
......@@ -70,7 +70,7 @@ OCStackResult createProvisioningResource()
return res;
}
OCStackResult createNetworkResource()
OCStackResult CreateNetworkResource()
{
NetworkInfo netInfo;
......@@ -283,9 +283,6 @@ char* constructJsonResponse(OCEntityHandlerRequest *ehRequest)
OC_LOG_V(INFO, TAG, "Constructed Response: %s", jsonResponse);
Serial.print("Constructed Response: ");
Serial.println(jsonResponse);
return jsonResponse;
}
......
//******************************************************************
//
// Copyright 2015 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 __PROVISIONING_HANDLER_H_
#define __PROVISIONING_HANDLER_H_
#include "logger.h"
#include "ocstack.h"
#include "easysetupcommon.h"
//-----------------------------------------------------------------------------
// Defines
//-----------------------------------------------------------------------------
#define TAG "provisioninghandler"
#define DEFAULT_CONTEXT_VALUE 0x99
#ifndef MAX_LENGTH_IPv4_ADDR
#define MAX_LENGTH_IPv4_ADDR 16
#endif
//-----------------------------------------------------------------------------
// Typedefs
//-----------------------------------------------------------------------------
/**
* List of methods that can be inititated from the client
*/
OCStackResult InitProvisioningHandler();
OCStackResult TerminateProvisioningHandler();
void listeningFunc(void*);
OCStackApplicationResult ProvisionEnrolleeResponse(void* ctx, OCDoHandle handle,
OCClientResponse * clientResponse);
OCStackResult ProvisionEnrollee(OCQualityOfService qos, const char* query);
OCStackApplicationResult GetProvisioningStatusResponse(void* ctx,
OCDoHandle handle, OCClientResponse * clientResponse);
OCStackResult InvokeOCDoResource(const char* query, OCMethod method,
OCQualityOfService qos, OCClientResponseHandler cb, const char* request,
OCHeaderOption * options, uint8_t numOptions);
OCStackResult GetProvisioningStatus(OCQualityOfService qos, const char* query);
OCStackResult StartProvisioningProcess(const EnrolleeNWProvInfo_t *netInfo,
OCProvisioningStatusCB provisioningStatusCallback);
void StopProvisioningProcess();
OCStackApplicationResult SubscribeProvPresenceCallback(void* ctx, OCDoHandle handle,
OCClientResponse* clientResponse);
OCStackResult SubscribeProvPresence(OCQualityOfService qos, const char* requestURI);
OCStackApplicationResult FindProvisioningResourceResponse(void* ctx,
OCDoHandle handle, OCClientResponse * clientResponse);
OCStackResult FindProvisioningResource(OCQualityOfService qos,