Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / mediator / linux / csdk_sample / mediator.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
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <signal.h>
25 #include <unistd.h>
26 #include <stdint.h>
27 #include <sstream>
28
29 #include "provisioningapi.h"
30 #include "logger.h"
31
32 #define ES_MEDIATOR_TAG "easysetupsample"
33
34 int quitFlag = 0;
35
36 /* SIGINT handler: set quitFlag to 1 for graceful termination */
37 void handleSigInt(int signum) {
38     if (signum == SIGINT) {
39         quitFlag = 1;
40     }
41 }
42
43 /**
44  * This callback function is used to receive the notifications about the provisioning status
45  * In success or failure, ProvisioningInfo structure holds the current state of provisioning
46  * and also holds the Enrollee information for which provisioning is requested
47  * This function can be used to update the application about the current provisioning status of the Enrollee
48  */
49 void ProvisioningStatusCallback(ProvisioningInfo * provInfo) {
50     OIC_LOG_V(INFO, ES_MEDIATOR_TAG, "Enrollee connectivity: %d", provInfo->provDeviceInfo.connType);
51     if (provInfo->provStatus == DEVICE_PROVISIONED) {
52         OIC_LOG_V(INFO, ES_MEDIATOR_TAG, "Successfully provisioned the Enrollee with IP : %s ",
53                   provInfo->provDeviceInfo.addr->addr);
54     }
55     else {
56         OIC_LOG_V(INFO, ES_MEDIATOR_TAG, "Provisioing Failed for the Enrollee with IP : %s",
57                   provInfo->provDeviceInfo.addr->addr);
58     }
59 }
60
61 static void PrintUsage() {
62     OIC_LOG(INFO, ES_MEDIATOR_TAG, "Usage : occlient -d \"192.168.0.20\"");
63 }
64
65 int main(int argc, char **argv) {
66     int opt;
67
68     ProvConfig provConfig;
69     //WiFiOnboardingConfig onboardConfig;
70     WiFiOnboadingConnection onboardConn;
71
72     PrintUsage();
73     InitProvProcess();
74
75
76     RegisterCallback(&ProvisioningStatusCallback);
77
78     while ((opt = getopt(argc, argv, "d:s:p:")) != -1) {
79         switch (opt) {
80             case 'd':
81                 strncpy(onboardConn.ipAddress, optarg, IPV4_ADDR_SIZE - 1);
82                 break;
83             case 's':
84                 strncpy(provConfig.provData.WIFI.ssid, optarg, NET_WIFI_SSID_SIZE - 1);
85                 break;
86             case 'p':
87                 strncpy(provConfig.provData.WIFI.pwd, optarg, NET_WIFI_PWD_SIZE - 1);
88                 break;
89             default:
90                 PrintUsage();
91                 return -1;
92         }
93     }
94
95     provConfig.connType = CT_ADAPTER_IP;
96     OIC_LOG_V(INFO, ES_MEDIATOR_TAG, "IP Address of the Provisioning device is =%s\n",
97               onboardConn.ipAddress);
98     OIC_LOG_V(INFO, ES_MEDIATOR_TAG, "SSID of the Enroller is =%s\n", provConfig.provData.WIFI.ssid);
99     OIC_LOG_V(INFO, ES_MEDIATOR_TAG, "Password of the Enroller is =%s\n", provConfig.provData.WIFI.pwd);
100
101     StartProvisioning(&provConfig, &onboardConn);
102
103     signal(SIGINT, handleSigInt);
104     while (!quitFlag) {
105         OCStackResult result;
106
107         result = OCProcess();
108
109         if (result != OC_STACK_OK)
110         {
111             OIC_LOG(ERROR, "Mediator_CSDK", "OCStack stop error");
112         }
113
114         // To minimize CPU utilization we may wish to do this with sleep
115         sleep(1);
116     }
117
118     ResetProvProcess();
119     OIC_LOG(INFO, ES_MEDIATOR_TAG, "Exiting occlient main loop...");
120
121     return 0;
122 }
123