Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / easy-setup / sdk / mediator / src / prov_adapter.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <signal.h>
24 #include <unistd.h>
25 #include <stdint.h>
26 #include <sstream>
27
28 #include "prov_adapter.h"
29
30 #define ES_PROV_ADAP_TAG "ES_PROVISIONING_ADAPTER"
31
32 //Use ipv4addr for both InitDiscovery and InitDeviceDiscovery
33 char ipv4addr[IPV4_ADDR_SIZE] = {0};
34
35 static const char * UNICAST_PROVISIONING_QUERY = "coap://%s:%d/oic/res?rt=oic.r.prov";
36
37 volatile static OCProvisioningStatusCB cbData = NULL;
38
39 OCStackResult InitProvProcess() {
40
41
42     OCStackResult result = OC_STACK_ERROR;
43
44     if (InitProvisioningHandler() == OC_STACK_OK) {
45         result = OC_STACK_OK;
46         OIC_LOG(DEBUG, ES_PROV_ADAP_TAG, "InitProvisioningHandler returned Success");
47     } else {
48         result = OC_STACK_ERROR;
49         OIC_LOG_V(ERROR, ES_PROV_ADAP_TAG, "InitProvisioningHandler returned error = %s",
50                   result);
51     }
52
53     return result;
54 }
55
56 OCStackResult ResetProvProcess() {
57     return TerminateProvisioningHandler();
58 }
59
60 OCStackResult RegisterCallback(OCProvisioningStatusCB provisioningStatusCallback) {
61     OCStackResult result = OC_STACK_OK;
62
63     if (provisioningStatusCallback != NULL) {
64         cbData = provisioningStatusCallback;
65     }
66     else {
67         result = OC_STACK_ERROR;
68         OIC_LOG(ERROR, ES_PROV_ADAP_TAG, "provisioningStatusCallback is NULL");
69     }
70
71     return result;
72 }
73
74 void UnRegisterCallback() {
75     if (cbData) {
76         cbData = NULL;
77     }
78 }
79
80 OCStackResult StartProvisioning(const EnrolleeNWProvInfo_t *netInfo) {
81
82     char findQuery[64] = {0};
83     snprintf(findQuery, sizeof(findQuery) - 1, UNICAST_PROVISIONING_QUERY,
84              netInfo->netAddressInfo.WIFI.ipAddress, IP_PORT);
85
86     return StartProvisioningProcess(netInfo, cbData, findQuery);
87 }
88
89 OCStackResult StopProvisioning(OCConnectivityType /*connectivityType*/) {
90     OCStackResult result = OC_STACK_OK;
91
92     StopProvisioningProcess();
93
94     return result;
95 }
96
97