easysetup.c 6.09 KB
Newer Older
1 2
//******************************************************************
//
3
// Copyright 2015 Samsung Electronics All Rights Reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// 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.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

21 22 23 24 25 26
/**
 * @file
 *
 * This file contains the implementation for EasySetup Enrollee device
 */

27
#include "easysetup.h"
28 29
#include "softap.h"
#include "onboarding.h"
30
#include "logger.h"
31
#include "resourcehandler.h"
32
#include "easysetupcallbacks.h"
33

34 35 36 37 38 39 40 41 42 43 44
/**
 * @var ES_ENROLLEE_TAG
 * @brief Logging tag for module name.
 */
#define ES_ENROLLEE_TAG "ES"

//-----------------------------------------------------------------------------
// Private variables
//-----------------------------------------------------------------------------

/**
45
 * @var gTargetSsid
46 47
 * @brief Target SSID of the Soft Access point to which the device has to connect
 */
48
static char gTargetSsid[MAXSSIDLEN];
49 50

/**
51
 * @var gTargetPass
52 53
 * @brief Password of the target access point to which the device has to connect
 */
54
static char gTargetPass[MAXNETCREDLEN];
55 56

/**
57
 * @var gEnrolleeStatusCb
58 59
 * @brief Fucntion pointer holding the callback for intimation of EasySetup Enrollee status callback
 */
60
static ESEnrolleeEventCallback gEnrolleeStatusCb = NULL;
61

62 63 64 65 66 67
/**
 * @var gIsSecured
 * @brief Variable to check if secure mode is enabled or not.
 */
static bool gIsSecured = false;

68
void ESOnboardingCallback(ESResult esResult)
69
{
70
        OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESOnboardingCallback with  result = %d", esResult);
71 72 73
        if(esResult == ES_OK)
        {
            gEnrolleeStatusCb(esResult, ES_ON_BOARDED_STATE);
74
        }
75 76
        else
        {
77
            OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG,
78 79
                        "Onboarding is failed callback result is = %d", esResult);
            gEnrolleeStatusCb(esResult, ES_INIT_STATE);
80
        }
81 82
}

83
void ESProvisioningCallback(ESResult esResult)
84
{
85
    OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESProvisioningCallback with  result = %d", esResult);
86

87
    if (esResult == ES_RECVTRIGGEROFPROVRES)
88
    {
89
        GetTargetNetworkInfoFromProvResource(gTargetSsid, gTargetPass);
90
        gEnrolleeStatusCb(ES_OK, ES_PROVISIONED_STATE);
91
        OIC_LOG(DEBUG, ES_ENROLLEE_TAG, "Connecting with target network");
92

93
        // Connecting/onboarding to target network
94
        ConnectToWiFiNetwork(gTargetSsid, gTargetPass, ESOnboardingCallbackTargetNet);
95 96 97
    }
    else
    {
98
       OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Provisioning is failed callback result is = %d", esResult);
99 100
       // Resetting Enrollee to ONBOARDED_STATE as Enrollee is alreday onboarded in previous step
       gEnrolleeStatusCb(ES_OK, ES_ON_BOARDED_STATE);
101 102 103
    }
}

104
void ESOnboardingCallbackTargetNet(ESResult esResult)
105
{
106
    OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESOnboardingCallback on target network with result = %d",
107 108 109 110
                                                                                        esResult);
    if(esResult == ES_OK)
    {
        gEnrolleeStatusCb(esResult, ES_ON_BOARDED_TARGET_NETWORK_STATE);
111
    }
112 113
    else
    {
114
        OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG,
115 116 117 118
                    "Onboarding is failed on target network and callback result is = %d", esResult);
        // Resetting Enrollee state to the ES_PROVISIONED_STATE
        // as device is already being provisioned with target network creds.
        gEnrolleeStatusCb(esResult, ES_PROVISIONED_STATE);
119 120 121
    }
}

122
ESResult ESInitEnrollee(OCConnectivityType networkType, const char *ssid, const char *passwd,
123
        bool isSecured,
124
        ESEnrolleeEventCallback cb)
125
{
126 127
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee IN");
    if(!ESEnrolleeValidateParam(networkType,ssid,passwd,cb))
128
    {
129
        OIC_LOG(ERROR, ES_ENROLLEE_TAG,
130
                            "ESInitEnrollee::Stopping Easy setup due to invalid parameters");
131 132 133
        return ES_ERROR;
    }

134 135 136
    //Init callback
    gEnrolleeStatusCb = cb;

137 138
    gIsSecured = isSecured;

139 140 141 142
    // TODO : This onboarding state has to be set by lower layer, as they better
    // knows when actually on-boarding started.
    cb(ES_ERROR,ES_ON_BOARDING_STATE);

143 144
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "received callback");
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "onboarding now..");
145

146
    if(!ESOnboard(ssid, passwd, ESOnboardingCallback))
147
    {
148
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESInitEnrollee::On-boarding failed");
149
        cb(ES_ERROR, ES_INIT_STATE);
150 151
        return ES_ERROR;
    }
152

153
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee OUT");
154
    return ES_OK;
155 156
}

157
ESResult ESTerminateEnrollee()
158
{
159 160 161 162
    UnRegisterResourceEventCallBack();

    //Delete Prov resource
    if (DeleteProvisioningResource() != OC_STACK_OK)
163
    {
164
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
165 166
        return ES_ERROR;
    }
167

168
    OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESTerminateEnrollee success");
169
    return ES_OK;
170 171
}

172
ESResult ESInitProvisioning()
173
{
174
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitProvisioning <<IN>>");
175

176
    if (CreateProvisioningResource(gIsSecured) != OC_STACK_OK)
177
    {
178
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "CreateProvisioningResource error");
179 180 181
        return ES_ERROR;
    }

182
    RegisterResourceEventCallBack(ESProvisioningCallback);
183

184
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitProvisioning <<OUT>>");
185 186 187
    return ES_RESOURCECREATED;
}

188 189
static bool ESEnrolleeValidateParam(OCConnectivityType networkType, const char *ssid,
                                                const char *passwd, ESEnrolleeEventCallback cb)
190 191 192
{
    if (!ssid || !passwd || !cb)
    {
193
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESEnrolleeValidateParam - Invalid parameters");
194 195 196 197 198
        return false;
    }
    return true;
}