resourcehandler.c 12.6 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
#include "resourcehandler.h"
22

23 24 25 26
#include <stdio.h>

#include "ocpayload.h"
#include "oic_string.h"
27

28 29 30 31 32 33 34 35 36 37
/**
 * @var ES_RH_TAG
 * @brief Logging tag for module name.
 */
#define ES_RH_TAG "ES_RH"
//-----------------------------------------------------------------------------
// Private variables
//-----------------------------------------------------------------------------

/**
38
 * @var gProvResource
39 40
 * @brief Structure for holding the Provisioning status and target information required to
 * connect to the target network
41
 */
42
static ProvResource gProvResource;
43 44

/**
45
 * @var gNetResource
46 47
 * @brief Structure forr holding the Provisioning status of network information
 */
48
static NetResource gNetResource;
49 50 51 52

//-----------------------------------------------------------------------------
// Private internal function prototypes
//-----------------------------------------------------------------------------
53 54
OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest *ehRequest,
        void *callback);
55
const char *getResult(OCStackResult result);
56 57 58
OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
59 60
OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest);

61
ESEnrolleeResourceEventCallback gNetworkInfoProvEventCb = NULL;
62

63
void RegisterResourceEventCallBack(ESEnrolleeResourceEventCallback cb)
64
{
65
    gNetworkInfoProvEventCb = cb;
66 67
}

68 69 70 71 72 73 74 75
void UnRegisterResourceEventCallBack()
{
    if (gNetworkInfoProvEventCb)
    {
        gNetworkInfoProvEventCb = NULL;
    }
}

76 77 78 79
void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
{
    if (name != NULL && pass != NULL)
    {
80 81
        OICStrcpy(name, MAXSSIDLEN, gProvResource.tnn);
        OICStrcpy(pass, MAXNETCREDLEN, gProvResource.cd);
82 83 84
    }
}

85
OCStackResult CreateProvisioningResource(bool isSecured)
86
{
87
    gProvResource.ps = ES_PS_NEED_PROVISIONING;
88
    gProvResource.tr = ES_PS_TRIGGER_INIT_VALUE;
89

90
    gProvResource.tnt = CT_ADAPTER_IP;
91
    OICStrcpy(gProvResource.tnn, sizeof(gProvResource.tnn), "Unknown");
92
    OICStrcpy(gProvResource.cd, sizeof(gProvResource.cd), "Unknown");
93

94 95 96 97
    OCStackResult res = OC_STACK_ERROR;
    if (isSecured)
    {
        res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_PROV_RES_TYPE,
98 99 100
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
101 102 103 104
    }
    else
    {
        res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_PROV_RES_TYPE,
105 106 107
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
108 109
    }

110
    OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
111 112
    return res;
}
113

114 115 116 117 118
OCStackResult DeleteProvisioningResource()
{
    OCStackResult res = OCDeleteResource(gProvResource.handle);
    if (res != OC_STACK_OK)
    {
119
        OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
120 121 122 123 124
    }

    return res;
}

125
OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
126 127 128 129
{
    OCEntityHandlerResult ehResult = OC_EH_ERROR;
    if (!ehRequest)
    {
130
        OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
131 132 133 134
        return ehResult;
    }
    if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
    {
135
        OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
136 137 138 139 140 141
        return ehResult;
    }

    OCRepPayload *getResp = constructResponse(ehRequest);
    if (!getResp)
    {
142
        OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
143 144 145 146 147 148 149 150 151
        return OC_EH_ERROR;
    }

    *payload = getResp;
    ehResult = OC_EH_OK;

    return ehResult;
}

152
OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
153
{
154
    OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
155 156 157
    OCEntityHandlerResult ehResult = OC_EH_ERROR;
    if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
    {
158
        OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
159 160 161 162 163 164
        return ehResult;
    }

    OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
    if (!input)
    {
165
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
166 167 168
        return ehResult;
    }

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    char* tnn;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn))
    {
        OICStrcpy(gProvResource.tnn, sizeof(gProvResource.tnn), tnn);
        OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.tnn %s", gProvResource.tnn);

        gProvResource.ps = ES_PS_PROVISIONING_COMPLETED;
    }

    char* cd;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
    {
        OICStrcpy(gProvResource.cd, sizeof(gProvResource.cd), cd);
        OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.cd %s", gProvResource.cd);
    }

    OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.ps %lld", gProvResource.ps);

187 188 189 190 191 192 193 194
    int64_t tr;
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_TR, &tr))
    {
        // Triggering
        gProvResource.tr = tr;
    }

    //ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed.
195
    // A new request for provisioning means overriding existing network provisioning information.
196 197 198 199 200 201 202 203 204 205 206 207
    if (gProvResource.ps == ES_PS_PROVISIONING_COMPLETED && tr == ES_PS_TRIGGER_CONNECTION)
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed."
                "Tiggering the network connection");

        if (gNetworkInfoProvEventCb)
        {
            gNetworkInfoProvEventCb(ES_RECVTRIGGEROFPROVRES);
            ehResult = OC_EH_OK;
        }
        else
        {
208
            gProvResource.tr = ES_PS_TRIGGER_INIT_VALUE;
209 210 211 212 213 214 215 216
            OIC_LOG(ERROR, ES_RH_TAG, "gNetworkInfoProvEventCb is NULL."
                    "Network handler not registered. Failed to connect to the network");
            ehResult = OC_EH_ERROR;
        }

        return ehResult;
    }
    else if (gProvResource.ps == ES_PS_PROVISIONING_COMPLETED)
217
    {
218
        OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed. "
219 220
                "This a request to override the existing the network provisioning information");
    }
221 222 223 224
    else
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning the network information to the Enrollee.");
    }
225

226 227 228
    OCRepPayload *getResp = constructResponse(ehRequest);
    if (!getResp)
    {
229
        OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
230 231 232 233 234 235 236 237 238
        return OC_EH_ERROR;
    }

    *payload = getResp;
    ehResult = OC_EH_OK;

    return ehResult;
}

239 240
OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
        OCRepPayload** payload)
241 242 243 244 245 246 247 248 249 250 251
{
    OCEntityHandlerResult ehResult = OC_EH_ERROR;

    return ehResult;
}

OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest)
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
252
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
253 254 255
        return NULL;
    }

256
    if (ehRequest->resource == gProvResource.handle)
257
    {
258
        OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
259
        OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
260 261
        OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PS, gProvResource.ps);
        OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_TNT, gProvResource.tnt);
262
    }
263
    else if (ehRequest->requestHandle == gNetResource.handle)
264 265 266
    {

        OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_NET);
267
        OCRepPayloadSetPropInt(payload, "ant", gNetResource.ant[0]);
268 269 270 271
    }
    return payload;
}

272 273 274 275
/**
 * This is the entity handler for the registered resource.
 * This is invoked by OCStack whenever it recevies a request for this resource.
 */
276 277 278 279 280
OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
        OCEntityHandlerRequest* entityHandlerRequest, void *callback)
{
    (void) callback;
    OCEntityHandlerResult ehRet = OC_EH_OK;
281 282 283 284
    OCEntityHandlerResponse response =
    { 0, 0, OC_EH_ERROR, 0, 0,
    { },
    { 0 }, false };
285
    OCRepPayload* payload = NULL;
286

287 288 289 290
    if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
    {
        if (OC_REST_GET == entityHandlerRequest->method)
        {
291
            OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
292 293 294 295
            ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
        }
        else if (OC_REST_PUT == entityHandlerRequest->method)
        {
296
            OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
297

298 299 300
            //PUT request will be handled in the internal implementation
            if (gProvResource.handle != NULL
                    && entityHandlerRequest->resource == gProvResource.handle)
301 302 303 304 305
            {
                ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
            }
            else
            {
306
                OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
307 308 309 310 311
                ehRet = OC_EH_ERROR;
            }
        }
        else if (OC_REST_POST == entityHandlerRequest->method)
        {
312
            OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
313 314 315 316 317 318 319 320 321 322
            if (gProvResource.handle != NULL
                    && entityHandlerRequest->resource == gProvResource.handle)
            {
                ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
            }
            else
            {
                OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
                ehRet = OC_EH_ERROR;
            }
323 324 325 326 327 328 329 330 331 332 333 334
        }

        if (ehRet == OC_EH_OK)
        {
            // Format the response.  Note this requires some info about the request
            response.requestHandle = entityHandlerRequest->requestHandle;
            response.resourceHandle = entityHandlerRequest->resource;
            response.ehResult = ehRet;
            //response uses OCPaylod while all get,put methodes use OCRepPayload
            response.payload = (OCPayload*) (payload);
            response.numSendVendorSpecificHeaderOptions = 0;
            memset(response.sendVendorSpecificHeaderOptions, 0,
335 336
                    sizeof(response.sendVendorSpecificHeaderOptions));
            memset(response.resourceUri, 0, sizeof(response.resourceUri));
337 338 339 340 341 342
            // Indicate that response is NOT in a persistent buffer
            response.persistentBufferFlag = 0;

            // Send the response
            if (OCDoResponse(&response) != OC_STACK_OK)
            {
343
                OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
                ehRet = OC_EH_ERROR;
            }
        }
    }

    return ehRet;
}

const char *getResult(OCStackResult result)
{
    switch (result)
    {
        case OC_STACK_OK:
            return "OC_STACK_OK";
        case OC_STACK_INVALID_URI:
            return "OC_STACK_INVALID_URI";
        case OC_STACK_INVALID_QUERY:
            return "OC_STACK_INVALID_QUERY";
        case OC_STACK_INVALID_IP:
            return "OC_STACK_INVALID_IP";
        case OC_STACK_INVALID_PORT:
            return "OC_STACK_INVALID_PORT";
        case OC_STACK_INVALID_CALLBACK:
            return "OC_STACK_INVALID_CALLBACK";
        case OC_STACK_INVALID_METHOD:
            return "OC_STACK_INVALID_METHOD";
        case OC_STACK_NO_MEMORY:
            return "OC_STACK_NO_MEMORY";
        case OC_STACK_COMM_ERROR:
            return "OC_STACK_COMM_ERROR";
        case OC_STACK_INVALID_PARAM:
            return "OC_STACK_INVALID_PARAM";
        case OC_STACK_NOTIMPL:
            return "OC_STACK_NOTIMPL";
        case OC_STACK_NO_RESOURCE:
            return "OC_STACK_NO_RESOURCE";
        case OC_STACK_RESOURCE_ERROR:
            return "OC_STACK_RESOURCE_ERROR";
        case OC_STACK_SLOW_RESOURCE:
            return "OC_STACK_SLOW_RESOURCE";
        case OC_STACK_NO_OBSERVERS:
            return "OC_STACK_NO_OBSERVERS";
        case OC_STACK_ERROR:
            return "OC_STACK_ERROR";
        default:
            return "UNKNOWN";
    }
}