1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include "samsung/sc_easysetup.h"
28 #include <oic_string.h>
29 #include "oic_malloc.h"
31 #define TAG "ENROLLEE_SAMPLE"
33 void *listeningFunc(void *);
36 * Secure Virtual Resource database for Iotivity Server
37 * It contains Server's Identity and the PSK credentials
38 * of other devices which the server trusts
40 static char CRED_FILE[] = "oic_svr_db_server.dat";
42 OCPersistentStorage ps;
46 * @brief Variable to check if secure mode is enabled or not.
48 static bool gIsSecured = false;
50 static SCProperties g_SCProperties = {0, 0, "", "", 0, {""}, ""};
54 printf("============\n");
55 printf("S: Enabled Security\n");
56 printf("I: Init & Start EasySetup\n");
57 printf("D: Set DeviceInfo\n");
58 printf("T: Terminate\n");
60 printf("============\n");
63 void WiFiProvCbInApp(ESWiFiConfData *eventData)
65 printf("WiFiProvCbInApp IN\n");
69 printf("ESWiFiProvData is NULL\n");
73 printf("SSID : %s\n", eventData->ssid);
74 printf("Password : %s\n", eventData->pwd);
75 printf("AuthType : %d\n", eventData->authtype);
76 printf("EncType : %d\n", eventData->enctype);
78 if(eventData->userdata != NULL)
80 SCWiFiConfProperties *data = eventData->userdata;
81 printf("[SC] DiscoveryChannel : %d\n", data->discoveryChannel);
84 printf("WiFiProvCbInApp OUT\n");
87 void DevConfProvCbInApp(ESDevConfData *eventData)
89 printf("DevConfProvCbInApp IN\n");
93 printf("ESDevConfProvData is NULL\n");
97 if(eventData->userdata != NULL)
99 SCDevConfProperties *data = eventData->userdata;
100 for(int i = 0 ; i < data->numLocation ; ++i)
102 printf("[SC] Location : %s\n", data->location[i]);
104 printf("[SC] Register Mobile Device : %s\n", data->regMobileDev);
105 printf("[SC] Country : %s\n", data->country);
106 printf("[SC] Language : %s\n", data->language);
107 printf("[SC] GPS Location : %s\n", data->gpsLocation);
108 printf("[SC] UTC Date time : %s\n", data->utcDateTime);
109 printf("[SC] Regional time : %s\n", data->regionalDateTime);
110 printf("[SC] SSO List : %s\n", data->ssoList);
113 printf("DevConfProvCbInApp OUT\n");
116 void CloudDataProvCbInApp(ESCoapCloudConfData *eventData)
118 printf("CloudDataProvCbInApp IN\n");
120 if(eventData == NULL)
122 printf("ESCloudProvData is NULL\n");
126 printf("AuthCode : %s\n", eventData->authCode);
127 printf("AuthProvider : %s\n", eventData->authProvider);
128 printf("CI Server : %s\n", eventData->ciServer);
130 if(eventData->userdata != NULL)
132 SCCoapCloudServerConfProperties *data = eventData->userdata;
133 printf("[SC] ClientID : %s\n", data->clientID);
136 printf("CloudDataProvCbInApp OUT\n");
139 ESProvisioningCallbacks gCallbacks = {
140 .WiFiConfProvCb = &WiFiProvCbInApp,
141 .DevConfProvCb = &DevConfProvCbInApp,
142 .CoapCloudConfProvCb = &CloudDataProvCbInApp
145 FILE* server_fopen(const char *path, const char *mode)
148 return fopen(CRED_FILE, mode);
151 void EnableSecurity()
153 printf("Inside EnableSecurity API..\n");
157 // Initialize Persistent Storage for SVR database
158 ps = (OCPersistentStorage){ server_fopen, fread, fwrite, fclose, unlink };
159 OCRegisterPersistentStorageHandler(&ps);
162 void StartEasySetup()
164 printf("StartEasySetup IN\n");
166 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
168 printf("OCStack init error!!\n");
172 ESResourceMask resourcemMask = ES_WIFICONF_RESOURCE | ES_COAPCLOUDCONF_RESOURCE | ES_DEVCONF_RESOURCE;
173 if(ESInitEnrollee(gIsSecured, resourcemMask, gCallbacks) != ES_OK)
175 printf("OCStack init error!!\n");
178 printf("ESInitEnrollee Success\n");
180 // Set callbacks for Samsung
181 ESSetCallbackForUserdata(&ReadUserdataCb, &WriteUserdataCb);
183 pthread_t thread_handle;
184 if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
186 printf("Thread creation failed\n");
189 printf("StartEasySetup OUT\n");
194 printf("SetDeviceInfo IN\n");
196 ESDeviceProperty deviceProperty = {
197 {{WIFI_11G, WIFI_11N, WIFI_11AC, WiFi_EOF}, WIFI_5G},
201 if(ESSetDeviceProperty(&deviceProperty) == ES_ERROR)
202 printf("ESSetDeviceProperty Error\n");
204 // Set user properties if needed
205 strncpy(g_SCProperties.deviceType, "deviceType", MAXLEN_STRING);
206 strncpy(g_SCProperties.deviceSubType, "deviceSubType", MAXLEN_STRING);
207 g_SCProperties.netConnectionState = NET_STATE_INIT;
208 g_SCProperties.discoveryChannel = WIFI_DISCOVERY_CHANNEL_INIT;
209 strncpy(g_SCProperties.regSetDev, "{\"wm\":\"00:11:22:33:44:55\",\"pm\":\"00:11:22:33:44:55\",\"bm\":\"00:11:22:33:44:55\",\"rk\":[\"VOICE\",\"EXTRA\",\"BTHIDPOWERON\"],\"sl\":[\"TV2MOBILE\",\"MOBILE2TV\",\"BTWAKEUP\",\"WOWLAN\",\"BTREMOTECON\",\"DLNADMR\"]}", MAXLEN_STRING);
210 strncpy(g_SCProperties.nwProvInfo, "{\"IMEI\":\"123456789012345 / 01\",\"IMSI\":\"123401234567890\",\"MCC_MNC\":\"100_10\",\"SN\":\"XY0123456XYZ\"}", MAXLEN_STRING);
211 strncpy(g_SCProperties.pnpPin, "pinNumber", MAXLEN_STRING);
212 strncpy(g_SCProperties.modelNumber, "Model Number", MAXLEN_STRING);
213 strncpy(g_SCProperties.esProtocolVersion, "2.0", MAXLEN_STRING);
215 if(SetSCProperties(&g_SCProperties) == ES_ERROR)
216 printf("SetSCProperties Error\n");
218 printf("SetDeviceInfo OUT\n");
223 printf("StopEasySetup IN\n");
225 if (ESTerminateEnrollee() == ES_ERROR)
227 printf("ESTerminateEnrollee Failed!!\n");
232 if (OCStop() != OC_STACK_OK)
234 printf("OCStack stop failed!!\n");
238 printf("StopEasySetup OUT\n");
243 printf("#########################\n");
244 printf("EasySetup Enrollee SAMPLE\n");
245 printf("#########################\n");
251 if(scanf("%c", &option) != 1)
253 printf("Failed to read input data\n");
271 case 'S': // Enable Security
277 case 'I': // Init EasySetup
283 case 'D': // Set Device Info
288 case 'T': // stop easy setup
294 printf("wrong option\n");
298 if (option == 'Q' || option == 'q') break;
304 void *listeningFunc(void * data)
307 OCStackResult result;
311 result = OCProcess();
312 if (result != OC_STACK_OK)
314 printf("OCStack stop error");