3ed1984736e3385ee47d779b5e43872de84097f3
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / mediator / linux / 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 "prov_adapter.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     EnrolleeNWProvInfo netInfo;
68     PrintUsage();
69     InitProvProcess();
70
71
72     RegisterCallback(ProvisioningStatusCallback);
73
74     while ((opt = getopt(argc, argv, "d:s:p:")) != -1) {
75         switch (opt) {
76             case 'd':
77                 strncpy(netInfo.netAddressInfo.WIFI.ipAddress, optarg, IPV4_ADDR_SIZE - 1);
78                 break;
79             case 's':
80                 strncpy(netInfo.netAddressInfo.WIFI.ssid, optarg, NET_WIFI_SSID_SIZE - 1);
81                 break;
82             case 'p':
83                 strncpy(netInfo.netAddressInfo.WIFI.pwd, optarg, NET_WIFI_PWD_SIZE - 1);
84                 break;
85             default:
86                 PrintUsage();
87                 return -1;
88         }
89     }
90
91     netInfo.connType = CT_ADAPTER_IP;
92     OIC_LOG_V(INFO, ES_MEDIATOR_TAG, "IP Address of the Provisioning device is =%s\n",
93               netInfo.netAddressInfo.WIFI.ipAddress);
94     OIC_LOG_V(INFO, ES_MEDIATOR_TAG, "SSID of the Enroller is =%s\n", netInfo.netAddressInfo.WIFI.ssid);
95     OIC_LOG_V(INFO, ES_MEDIATOR_TAG, "Password of the Enroller is =%s\n", netInfo.netAddressInfo.WIFI.pwd);
96
97     StartProvisioning(&netInfo);
98
99     signal(SIGINT, handleSigInt);
100     while (!quitFlag) {
101         sleep(1);
102     }
103
104     ResetProvProcess();
105     OIC_LOG(INFO, ES_MEDIATOR_TAG, "Exiting occlient main loop...");
106
107     return 0;
108 }
109