Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / easy-setup / mediator / csdk / src / provisioningapi.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 "provisioningapi.h"
29 #include "oic_string.h"
30
31 #define ES_PROV_ADAP_TAG "ES_PROVISIONING_ADAPTER"
32
33 //Use ipv4addr for both InitDiscovery and InitDeviceDiscovery
34 char ipv4addr[IPV4_ADDR_SIZE] =
35 { 0 };
36
37 #ifdef REMOTE_ARDUINO_ENROLEE
38 //Arduino Enrollee needs mediator application provide IP and port55555 which is specific
39 // to Arduino WiFi enrollee
40 static const char * UNICAST_PROVISIONING_QUERY = "coap://%s:%d/oic/res?rt=oic.r.prov";
41 #else
42 static const char * UNICAST_PROVISIONING_QUERY = "/oic/res?rt=oic.r.prov";
43 #endif
44
45 volatile static OCProvisioningStatusCB cbData = NULL;
46
47 OCStackResult InitProvProcess()
48 {
49
50     OCStackResult result = OC_STACK_ERROR;
51
52     if (InitProvisioningHandler() == OC_STACK_OK)
53     {
54         result = OC_STACK_OK;
55         OIC_LOG(DEBUG, ES_PROV_ADAP_TAG, "InitProvisioningHandler returned Success");
56     }
57     else
58     {
59         result = OC_STACK_ERROR;
60         OIC_LOG_V(ERROR, ES_PROV_ADAP_TAG, "InitProvisioningHandler returned error = %d",
61                 result);
62     }
63
64     return result;
65 }
66
67 OCStackResult ResetProvProcess()
68 {
69     return TerminateProvisioningHandler();
70 }
71
72 OCStackResult RegisterCallback(OCProvisioningStatusCB provisioningStatusCallback)
73 {
74     OCStackResult result = OC_STACK_OK;
75
76     if (provisioningStatusCallback != NULL)
77     {
78         cbData = provisioningStatusCallback;
79     }
80     else
81     {
82         result = OC_STACK_ERROR;
83         OIC_LOG(ERROR, ES_PROV_ADAP_TAG, "provisioningStatusCallback is NULL");
84     }
85
86     return result;
87 }
88
89 void UnRegisterCallback()
90 {
91     if (cbData)
92     {
93         cbData = NULL;
94     }
95 }
96
97 OCStackResult StartProvisioning(const ProvConfig *provConfig, WiFiOnboadingConnection *onboardConn)
98 {
99
100     char findQuery[64] =
101     { 0 };
102
103     if (provConfig == NULL || onboardConn == NULL)
104     {
105         return OC_STACK_ERROR;
106     }
107
108 #ifdef REMOTE_ARDUINO_ENROLEE
109     //Arduino Enrollee needs mediator application provide IP and port55555 which is specific
110     // to Arduino WiFi enrollee
111     snprintf(findQuery, sizeof(findQuery) - 1, UNICAST_PROVISIONING_QUERY,
112             onboardConn->ipAddress, IP_PORT);
113 #else
114     OICStrcpy(findQuery, sizeof(findQuery) - 1, UNICAST_PROVISIONING_QUERY);
115 #endif
116
117     return StartProvisioningProcess(provConfig, onboardConn, cbData, findQuery);
118 }
119
120 OCStackResult StopProvisioning(OCConnectivityType /*connectivityType*/)
121 {
122     OCStackResult result = OC_STACK_OK;
123
124     StopProvisioningProcess();
125
126     return result;
127 }