e9e344b9c0a5ee718c7f78e177dc2898fc635087
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / inc / ESRichCommon.h
1 //******************************************************************
2 //
3 // Copyright 2016 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 #ifndef ES_COMMON_RICH_H_
22 #define ES_COMMON_RICH_H_
23
24 #include <iostream>
25 #include <string>
26 #ifndef WITH_ARDUINO
27 #include <memory>
28 #endif
29
30 #include "ocstack.h"
31 #include "octypes.h"
32
33 using namespace std;
34
35 // Defines
36 #define OIC_STRING_MAX_VALUE    100
37 #define IPV4_ADDR_SIZE          16
38 #define IP_PORT                 55555
39 #define NET_WIFI_SSID_SIZE      100
40 #define NET_WIFI_PWD_SIZE       100
41
42 /**
43  * @brief Mac address length for BT port
44  */
45 #define NET_MACADDR_SIZE 18
46
47 /**
48  * Attributes used to form a proper easysetup conforming JSON message.
49  */
50 #define OC_RSRVD_ES_PS                     "ps"
51 #define OC_RSRVD_ES_TNN                    "tnn"
52 #define OC_RSRVD_ES_CD                     "cd"
53 #define OC_RSRVD_ES_TR                     "tr"
54 #define OC_RSRVD_ES_TNT                    "tnt"
55 #define OC_RSRVD_ES_ANT                    "ant"
56
57 /**
58  * Easysetup defined resoruce types and uris.
59  */
60 #define OC_RSRVD_ES_PROV_RES_TYPE           "oic.r.prov"
61 #define OC_RSRVD_ES_URI_PROV               "/oic/prov"
62 #define OC_RSRVD_ES_URI_NET                "/oic/net"
63
64 /**
65  * @brief Defines for Provisioning status accepted values
66  */
67 #define ES_PS_NEED_PROVISIONING         1
68 #define ES_PS_PROVISIONING_COMPLETED    2
69 #define ES_PS_TRIGGER_INIT_VALUE        0
70 #define ES_PS_TRIGGER_CONNECTION        1
71
72 #ifndef WITH_ARDUINO
73 namespace OIC
74 {
75     namespace Service
76     {
77
78         /**
79         * Device Roles defined for each device type used in easy setup
80         */
81         typedef enum
82         {
83             ENROLLEE,
84             MEDIATOR,
85             ENROLLER,
86         } DeviceRole;
87
88         /**
89         * On-boarding connection to create Adhoc network.
90         */
91         typedef enum
92         {
93             SOFTAP,
94             BLE,
95         } OBConnection;
96
97         typedef enum
98         {
99             ES_ERROR = -1,
100             ES_OK = 0,
101             ES_NETWORKFOUND = 1,
102             ES_NETWORKCONNECTED,
103             ES_NETWORKNOTCONNECTED,
104             ES_RESOURCECREATED = 11,
105             ES_RECVREQOFPROVRES = 21,
106             ES_RECVREQOFNETRES,
107             ES_RECVUPDATEOFPROVRES,
108             ES_RECVTRIGGEROFPROVRES,
109         } ESResult;
110
111         typedef enum
112         {
113             /**
114              * Default state of the device
115              */
116             ES_INIT_STATE,
117
118             /**
119              * Device will move to this state once the on boarding begins
120              */
121             ES_ON_BOARDING_STATE,
122
123             /**
124              * Device will move to this state after successful on-boarding of the device
125              */
126             ES_ON_BOARDED_STATE,
127
128             /**
129              * Device will move to this state once the on boarding is done
130              */
131             ES_PROVISIONING_STATE,
132
133             /**
134              * Easy setup process is successful.
135              */
136             ES_PROVISIONED_STATE,
137
138             /**
139              * This state is arbitrary one, any time device can come into this state
140              * Device will move to this state if the ownership transfer initiated  by the Application
141              */
142             ES_OWNERSHIP_TRANSFERRING_STATE,
143
144             /**
145              * This state is arbitrary one, any time device can come into this state
146              * Device will move to this state if the ownership transfer is completed
147              */
148             ES_OWNERSHIP_TRANSFERRED_STATE,
149
150             /**
151              * This state is arbitrary one, any time device can come into this state
152              * Device will move to this state once the Application factory reset the device
153              */
154             ES_FACTORY_RESET_STATE,
155
156             /**
157              * Enrollee moves to this state after connecting to target network
158              */
159             ES_ON_BOARDED_TARGET_NETWORK_STATE,
160         }EnrolleeState;
161
162         /**
163          * Provisioning Device Status
164          */
165         typedef struct
166         {
167             // Address of remote server
168             OCDevAddr * addr;
169             // Indicates adaptor type on which the response was received
170             OCConnectivityType connType;
171         } EasySetupDeviceInfo;
172
173         /**
174          * Provosioning Status
175          */
176         typedef enum
177         {
178             DEVICE_PROVISIONED = 0,
179             DEVICE_NOT_PROVISIONED,
180             DEVICE_OWNED,
181             DEVICE_NOT_OWNED
182         } EasySetupState, ProvStatus;
183
184         /**
185          * Response from queries to remote servers.
186          */
187         typedef struct
188         {
189             // EasySetup Status
190             EasySetupState provStatus;
191             // EasySetup Device Info
192             EasySetupDeviceInfo provDeviceInfo;
193         } EasySetupInfo, ProvisioningInfo;
194
195         /**
196          * @brief  Network information of the Enroller
197          */
198         typedef union
199         {
200             /**
201              * @brief BT Mac Information
202              */
203             struct
204             {
205                 char btMacAddress[NET_MACADDR_SIZE];   /**< BT mac address **/
206             } BT;
207
208             /**
209              * @brief LE MAC Information
210              */
211             struct
212             {
213                 char leMacAddress[NET_MACADDR_SIZE];   /**< BLE mac address **/
214             } LE;
215
216             /**
217              * @brief IP Information
218              */
219             struct
220             {
221                 char ssid[NET_WIFI_SSID_SIZE]; /**< ssid of the Enroller**/
222                 char pwd[NET_WIFI_PWD_SIZE]; /**< pwd of the Enroller**/
223             } WIFI;
224         } ProvData;
225
226         /**
227          * @brief Network Information
228          */
229         typedef struct
230         {
231             ProvData provData;    /**< Enroller Network Info**/
232             OCConnectivityType connType;    /**< Connectivity Type**/
233         } ProvConfig;
234
235         /**
236          * Client applications implement this callback to consume responses received from Servers.
237          */
238         typedef void (*OCProvisioningStatusCB)(EasySetupInfo *easySetupInfo);
239
240         /**
241          * @brief This structure represent configuration information to create wifi onboarding SoftAP or connection.
242         */
243
244         // Note : Below structure is not currently used but added for future purpose.
245         typedef struct
246         {
247             char ssid[NET_WIFI_SSID_SIZE]; /**< ssid of the onboarding Adhoc Wifi network**/
248             char pwd[NET_WIFI_PWD_SIZE]; /**< pwd of the onboarding Adhoc wifi network**/
249             bool isSecured;                 /**< Secure connection**/
250         }WiFiOnboardingConfig;
251
252         /**
253          * @brief This structure represent onboarding connection instance.
254         */
255         typedef struct
256         {
257          /*Actual use of ipAddress is for unicast discovery, but also used to identify the Enrollee device as of now,
258             device identification should be based on DeviceID in next release.*/
259            char ipAddress[IPV4_ADDR_SIZE]; /**< IP Address of the Enrollee **/
260            bool isSecured;                 /**< Secure connection**/
261         }WiFiOnboadingConnection;
262
263         typedef enum
264         {
265             ES_PROVISIONING_ERROR = -1,
266             ES_NEED_PROVISIONING,
267             ES_PROVISIONED_ALREADY,
268             ES_PROVISIONING_SUCCESS
269         } ESState;
270
271         typedef enum
272         {
273             ES_UNKNOWN = 0,
274             ES_ONBOARDED,
275             ES_OWNED,
276             ES_PROVISIONED
277         } CurrentESState;
278
279         typedef enum
280         {
281             ES_SEC_UNKNOWN = 0,
282             ES_SEC_OWNED,
283             ES_SEC_ACL_PROVISIONED,
284             ES_SEC_CREDS_PROVISIONED
285         } EnrolleeSecState;
286
287         /**
288          * Security Provisioning Status
289          */
290         class SecProvisioningResult
291         {
292         public:
293             std::shared_ptr< SecProvisioningResult > shared_ptr;
294             SecProvisioningResult(std::string deviceUUID, ESResult result) :
295                 m_devUUID(deviceUUID), m_result(result)
296             {
297
298             }
299
300             std::string getDeviceUUID()
301             {
302                 return m_devUUID;
303             }
304
305             ESResult getResult()
306             {
307                 return m_result;
308             }
309         private:
310             std::string m_devUUID;
311             ESResult m_result;
312         };
313
314         /**
315          * Callback function definition for providing Enrollee security status .
316          */
317         typedef std::function< void(std::shared_ptr<SecProvisioningResult>) > EnrolleeSecStatusCb;
318
319         /**
320          * Callback definition to be invoked when the security stack expects a pin from application.
321          */
322         typedef std::function< void(std::string&) > SecurityPinCb;
323
324         /**
325          * Callback definition to be invoked when the stack expects a db path.
326          */
327         typedef std::function< void(std::string&) > SecProvisioningDbPathCb;
328
329         class ProvisioningStatus
330         {
331         public:
332             std::shared_ptr< ProvisioningStatus > shared_ptr;
333             ProvisioningStatus(ESResult result, ESState esState) :
334                     m_result(result), m_esState(esState)
335             {
336
337             }
338
339             ESResult getESResult()
340             {
341                 return m_result;
342             }
343
344             ESState getESState()
345             {
346                 return m_esState;
347             }
348         private:
349             ESResult m_result;
350             ESState m_esState;
351         };
352
353         class EasySetupStatus
354         {
355         public:
356             std::shared_ptr< EasySetupStatus > shared_ptr;
357             EasySetupStatus(const EasySetupState& easySetupState,
358                     const ProvConfig& provConfig) :
359                     m_easySetupState(easySetupState), m_ProvConfig(provConfig)
360             {
361
362             }
363
364             ProvConfig getProvConfig()
365             {
366                 return m_ProvConfig;
367             }
368
369             EasySetupState getEasySetupState()
370             {
371                 return m_easySetupState;
372             }
373         private:
374             EasySetupState m_easySetupState;
375             ProvConfig m_ProvConfig;
376         };
377     }
378 }
379 #endif //WITH_ARDUINO
380
381 #endif //ES_COMMON_RICH_H_