cc386944afb2b3bb740167bcadea93b46cd529e5
[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 #include "oic_string.h"
30
31
32 #define ES_PROV_ADAP_TAG "ES_PROVISIONING_ADAPTER"
33
34 //Use ipv4addr for both InitDiscovery and InitDeviceDiscovery
35 char ipv4addr[IPV4_ADDR_SIZE] = {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         result = OC_STACK_OK;
54         OC_LOG(DEBUG, ES_PROV_ADAP_TAG, "InitProvisioningHandler returned Success");
55     } else {
56         result = OC_STACK_ERROR;
57         OC_LOG_V(ERROR, ES_PROV_ADAP_TAG, "InitProvisioningHandler returned error = %d",
58                   result);
59     }
60
61     return result;
62 }
63
64 OCStackResult ResetProvProcess() {
65     return TerminateProvisioningHandler();
66 }
67
68 OCStackResult RegisterCallback(OCProvisioningStatusCB provisioningStatusCallback) {
69     OCStackResult result = OC_STACK_OK;
70
71     if (provisioningStatusCallback != NULL) {
72         cbData = provisioningStatusCallback;
73     }
74     else {
75         result = OC_STACK_ERROR;
76         OC_LOG(ERROR, ES_PROV_ADAP_TAG, "provisioningStatusCallback is NULL");
77     }
78
79     return result;
80 }
81
82 void UnRegisterCallback() {
83     if (cbData) {
84         cbData = NULL;
85     }
86 }
87
88 OCStackResult StartProvisioning(const EnrolleeNWProvInfo *netInfo) {
89
90     char findQuery[64] = {0};
91
92 #ifdef REMOTE_ARDUINO_ENROLEE
93     //Arduino Enrollee needs mediator application provide IP and port55555 which is specific
94     // to Arduino WiFi enrollee
95     snprintf(findQuery, sizeof(findQuery) - 1, UNICAST_PROVISIONING_QUERY,
96              netInfo->netAddressInfo.WIFI.ipAddress, IP_PORT);
97 #else
98     OICStrcpy(findQuery, sizeof(findQuery)-1, UNICAST_PROVISIONING_QUERY);
99 #endif
100
101     return StartProvisioningProcess(netInfo, cbData, findQuery);
102 }
103
104 OCStackResult StopProvisioning(OCConnectivityType /*connectivityType*/) {
105     OCStackResult result = OC_STACK_OK;
106
107     StopProvisioningProcess();
108
109     return result;
110 }