Add getStatus API in RemoteEnrollee class for Android platform
[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 #include <vector>
29 #endif
30
31 #include "OCPlatform.h"
32 #include "ocstack.h"
33 #include "octypes.h"
34
35 #include "escommon.h"
36
37 using namespace OC;
38 using namespace std;
39
40 #ifndef WITH_ARDUINO
41 namespace OIC
42 {
43     namespace Service
44     {
45         /**
46          * @brief Properties of provisioning resource. It includes a provisioning status and last
47          *        error code.
48          */
49         class EnrolleeStatus
50         {
51         public:
52
53             /**
54              * Constructor
55              */
56             EnrolleeStatus(const OCRepresentation& rep) { m_rep = rep; }
57
58             /**
59              * Get a provisioning status property of Enrollee.
60              *
61              * @return a provisioning status property of Enrollee
62              */
63             ProvStatus getProvStatus()
64             {
65                 if(m_rep.hasAttribute(OC_RSRVD_ES_PROVSTATUS))
66                     return static_cast<ProvStatus>(
67                                         m_rep.getValue<int>(OC_RSRVD_ES_PROVSTATUS));
68                 return ES_STATE_INIT;
69             }
70
71             /**
72              * Get a last error code property of Enrollee.
73              *
74              * @return a last error code property of Enrollee.
75              */
76             ESErrorCode getLastErrCode()
77             {
78                 if(m_rep.hasAttribute(OC_RSRVD_ES_LAST_ERRORCODE))
79                     return static_cast<ESErrorCode>(
80                                         m_rep.getValue<int>(OC_RSRVD_ES_LAST_ERRORCODE));
81                 return ES_ERRCODE_NO_ERROR;
82             }
83
84             /**
85              * Get OCRepresentation object
86              *
87              * @return OCRepresentation object
88              */
89             const OCRepresentation& getRepresentation()
90             {
91                 return m_rep;
92             }
93         protected:
94             OCRepresentation m_rep;
95         };
96
97         /**
98          * @brief Data class stored for Cloud server property provisioning
99          */
100         class CloudProp
101         {
102         public:
103
104             /**
105              * Constructor
106              */
107             CloudProp() {};
108
109             /**
110              * Constructor with OCRepresentation object. This is used for JNI communication.
111              */
112             CloudProp(const OCRepresentation &rep)
113             {
114                 m_rep = rep;
115                 m_cloudID = "";
116             }
117
118             /**
119              * Set CloudServer resource properties to be delivered to Enrollee
120              *
121              * @param authCode  Auth code issued by OAuth2.0-compatible account server
122              * @param authProvider Auth provider ID
123              * @param ciServer Cloud interface server URL which an Enrollee is going to registered
124              */
125             void setCloudProp(string authCode, string authProvider, string ciServer)
126             {
127                 m_rep.setValue(OC_RSRVD_ES_AUTHCODE, authCode);
128                 m_rep.setValue(OC_RSRVD_ES_AUTHPROVIDER, authProvider);
129                 m_rep.setValue(OC_RSRVD_ES_CISERVER, ciServer);
130             }
131
132             /**
133              * Set CloudServer's UUID
134              *
135              * @param cloudID Cloud Interface server's UUID
136              */
137             void setCloudID(string cloudID)
138             {
139                 m_cloudID = cloudID;
140             }
141
142             /**
143              * Get an auth code to be delivered.
144              *
145              * @return an auth code to be delivered.
146              */
147             std::string getAuthCode()
148             {
149                 if(m_rep.hasAttribute(OC_RSRVD_ES_AUTHCODE))
150                     return m_rep.getValue<std::string>(OC_RSRVD_ES_AUTHCODE);
151                 return std::string("");
152             }
153
154             /**
155              * Get an auth provider which issued an auth code
156              *
157              * @return an auth provider which issued an auth code
158              */
159             std::string getAuthProvider()
160             {
161                 if(m_rep.hasAttribute(OC_RSRVD_ES_AUTHPROVIDER))
162                     return m_rep.getValue<std::string>(OC_RSRVD_ES_AUTHPROVIDER);
163                 return std::string("");
164             }
165
166             /**
167              * Get a CI server to be delivered
168              *
169              * @return a CI server to be delivered
170              */
171             std::string getCiServer()
172             {
173                 if(m_rep.hasAttribute(OC_RSRVD_ES_CISERVER))
174                     return m_rep.getValue<std::string>(OC_RSRVD_ES_CISERVER);
175                 return std::string("");
176             }
177
178             /**
179              * Get a CI server's Uuid to be delivered
180              *
181              * @return a CI server's Uuid to be delivered
182              */
183             std::string getCloudID() const
184             {
185                 return m_cloudID;
186             }
187
188             /**
189              * Get OCRepresentation object
190              *
191              * @return OCRepresentation object
192              */
193             const OCRepresentation &toOCRepresentation() const
194             {
195                 return m_rep;
196             }
197         protected:
198             OCRepresentation m_rep;
199             std::string m_cloudID;
200         };
201
202         /**
203          * @brief Data class stored for Device property provisioning which includes a WiFi
204          *        and device configuration provisioning
205          */
206         class DeviceProp
207         {
208         public:
209
210             /**
211              * Constructor
212              */
213             DeviceProp() {}
214
215             /**
216              * Constructor with OCRepresentation object. This is used for JNI communication.
217              */
218             DeviceProp(const OCRepresentation &rep) { m_rep = rep; }
219
220             /**
221              * Set WiFi resource properties to be delivered to Enrollee
222              *
223              * @param ssid Ssid of the Enroller
224              * @param pwd Pwd of the Enrolle
225              * @param authtype Auth type of the Enroller
226              * @param enctype Encryption type of the Enroller
227              *
228              * @see WIFI_AUTHTYPE
229              * @see WIFI_ENCTYPE
230              */
231             void setWiFiProp(string ssid, string pwd, WIFI_AUTHTYPE authtype, WIFI_ENCTYPE enctype)
232             {
233                 m_rep.setValue(OC_RSRVD_ES_SSID, ssid);
234                 m_rep.setValue(OC_RSRVD_ES_CRED, pwd);
235                 m_rep.setValue(OC_RSRVD_ES_AUTHTYPE, authtype);
236                 m_rep.setValue(OC_RSRVD_ES_ENCTYPE, enctype);
237             }
238
239             /**
240              * Set DevConf resource properties to be delivered to Enrollee
241              *
242              * @param language IETF language tag using ISO 639X
243              * @param country ISO Country Code (ISO 3166-1 Alpha-2)
244              */
245             void setDevConfProp(string language, string country)
246             {
247                 m_rep.setValue(OC_RSRVD_ES_LANGUAGE, language);
248                 m_rep.setValue(OC_RSRVD_ES_COUNTRY, country);
249             }
250
251             /**
252              * Get a SSID of Enroller
253              *
254              * @return a SSID of enroller
255              */
256             std::string getSsid()
257             {
258                 if(m_rep.hasAttribute(OC_RSRVD_ES_SSID))
259                     return m_rep.getValue<std::string>(OC_RSRVD_ES_SSID);
260                 return std::string("");
261             }
262
263             /**
264              * Get a password of Enroller
265              *
266              * @return a password of enroller
267              */
268             std::string getPassword()
269             {
270                 if(m_rep.hasAttribute(OC_RSRVD_ES_CRED))
271                     return m_rep.getValue<std::string>(OC_RSRVD_ES_CRED);
272                 return std::string("");
273             }
274
275             /**
276              * Get an auth type of Enroller
277              *
278              * @return an auth type of enroller
279              *
280              * @see WIFI_AUTHTYPE
281              */
282             WIFI_AUTHTYPE getAuthType()
283             {
284                 if(m_rep.hasAttribute(OC_RSRVD_ES_AUTHTYPE))
285                     return static_cast<WIFI_AUTHTYPE>(m_rep.getValue<int>(OC_RSRVD_ES_AUTHTYPE));
286                 return NONE_AUTH;
287             }
288
289             /**
290              * Get an encryption type of Enroller
291              *
292              * @return an encryption type of enroller
293              *
294              * @see WIFI_ENCTYPE
295              */
296             WIFI_ENCTYPE getEncType()
297             {
298                 if(m_rep.hasAttribute(OC_RSRVD_ES_ENCTYPE))
299                     return static_cast<WIFI_ENCTYPE>(m_rep.getValue<int>(OC_RSRVD_ES_ENCTYPE));
300                 return NONE_ENC;
301             }
302
303             /**
304              * Get a language to be set. A language is expressed in IETF language tag
305              * using ISO 639X.
306              *
307              * @return a language to be set
308              */
309             std::string getLanguage()
310             {
311                 if(m_rep.hasAttribute(OC_RSRVD_ES_LANGUAGE))
312                     return m_rep.getValue<std::string>(OC_RSRVD_ES_LANGUAGE);
313                 return std::string("");
314             }
315
316             /**
317              * Get a country to be set. A country is expressed in ISO Country Code
318              * (ISO 3166-1 Alpha-2)
319              *
320              * @return a country to be set
321              */
322             std::string getCountry()
323             {
324                 if(m_rep.hasAttribute(OC_RSRVD_ES_COUNTRY))
325                     return m_rep.getValue<std::string>(OC_RSRVD_ES_COUNTRY);
326                 return std::string("");
327             }
328
329             /**
330              * Get OCRepresentation object
331              *
332              * @return OCRepresentation object
333              */
334             const OCRepresentation &toOCRepresentation() const
335             {
336                 return m_rep;
337             }
338
339         protected:
340             OCRepresentation m_rep;
341         };
342
343         /**
344          * @brief Provisioning state in cloud server property provisioning.
345          */
346         typedef enum
347         {
348             ES_CLOUD_PROVISIONING_ERROR = -1,   /**< An error in cloud provisioning happens **/
349             ES_CLOUD_PROVISIONING_SUCCESS,      /**< Cloud provisioning is successfully done **/
350             ES_CLOUD_ENROLLEE_FOUND,            /**< An enrollee is found in a given network **/
351             ES_CLOUD_ENROLLEE_NOT_FOUND         /**< NO enrollee is found in a given network **/
352         }ESCloudProvState;
353
354         /**
355          * Security Provisioning Status
356          */
357         class SecProvisioningStatus
358         {
359         public:
360             SecProvisioningStatus(string deviceUUID, ESResult result) :
361                 m_devUUID(deviceUUID), m_result(result)
362             {
363             }
364
365             const string getDeviceUUID()
366             {
367                 return m_devUUID;
368             }
369
370             ESResult getESResult()
371             {
372                 return m_result;
373             }
374         private:
375             string m_devUUID;
376             ESResult m_result;
377         };
378
379         /**
380          * @breif This provide a set of getter APIs from received response for getConfiguration().
381          *        Received information includes a device name, WiFi supported mode, and frequency.
382          *        Additionally, you can know if Enrollee can be access to cloud server with this
383          *        object.
384          */
385         class EnrolleeConf
386         {
387         public:
388             /**
389              * Constructor
390              * The expected OCRepresentation is one for collection resource and has several child
391              * OCRepresentation object corresponding to WiFi, DevConf, and CloudServer resource's
392              * representation.
393              */
394             EnrolleeConf(const OCRepresentation& rep)
395             {
396                 m_ProvRep = rep;
397
398                 std::vector<OCRepresentation> children = rep.getChildren();
399
400                 for(auto child = children.begin(); child != children.end(); ++child)
401                 {
402                     if(child->getUri().find(OC_RSRVD_ES_URI_WIFI) != std::string::npos)
403                     {
404                         m_WiFiRep = *child;
405                     }
406                     else if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos)
407                     {
408                         m_DevConfRep = *child;
409                     }
410                     else if(child->getUri().find(OC_RSRVD_ES_URI_CLOUDSERVER) != std::string::npos)
411                     {
412                         m_CloudRep = *child;
413                     }
414                 }
415             }
416
417             /**
418              * Get a device name of Enrollee. It is Device's human-friendly name like device model
419              * name.
420              *
421              * @return a device name of Enrollee
422              */
423             std::string getDeviceName()
424             {
425                 if(m_DevConfRep.hasAttribute(OC_RSRVD_ES_DEVNAME))
426                     return m_DevConfRep.getValue<std::string>(OC_RSRVD_ES_DEVNAME);
427                 return std::string("");
428             }
429
430             /**
431              * Get a set of WiFi supported modes of Enrollee
432              *
433              * @return a set of WiFi supported modes of Enrollee
434              *
435              * @see WIFI_MODE
436              */
437             vector<WIFI_MODE> getWiFiModes()
438             {
439                 vector<WIFI_MODE> modes;
440                 modes.clear();
441
442                 if(m_WiFiRep.hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIMODE))
443                 {
444                     for(auto it : m_WiFiRep.getValue
445                                         <std::vector<int>>(OC_RSRVD_ES_SUPPORTEDWIFIMODE))
446                     {
447                         modes.push_back(static_cast<WIFI_MODE>(it));
448                     }
449                 }
450                 return modes;
451             }
452
453             /**
454              * Get a WiFi supported frequency of Enrollee
455              *
456              * @return a WiFi supported frequency of Enrollee
457              *
458              * @see WIFI_FREQ
459              */
460             WIFI_FREQ getWiFiFreq()
461             {
462                 if(m_WiFiRep.hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIFREQ))
463                     return static_cast<WIFI_FREQ>(
464                                         m_WiFiRep.getValue<int>(OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
465                 return WIFI_FREQ_NONE;
466             }
467
468             /**
469              * Get an accessibility to cloud server of an Enrollee
470              *
471              * @return an accessibility to cloud server of an Enrollee
472              */
473             bool isCloudAccessible()
474             {
475                 if(m_CloudRep.getUri().find(OC_RSRVD_ES_URI_CLOUDSERVER) != std::string::npos)
476                 {
477                     return true;
478                 }
479                 return false;
480             }
481
482             /**
483              * Get OCRepresentation object
484              *
485              * @return OCRepresentation object
486              */
487             const OCRepresentation& getProvResRep()
488             {
489                 return m_ProvRep;
490             }
491
492         protected:
493             OCRepresentation m_ProvRep, m_WiFiRep, m_DevConfRep, m_CloudRep;
494         };
495
496         /**
497          * Status object for getStatus API. This object is given to application
498          * when a response for GET request to provisioning resource at Enrollee is arrived.
499          * It returns a result of the API and requested data delivered in the response which includes
500          * a provisioning status and last error code stored in Enrollee.
501          *
502          * @see EnrolleeStatus
503          */
504         class GetEnrolleeStatus
505         {
506         public:
507             GetEnrolleeStatus(ESResult result, const EnrolleeStatus& status) :
508                 m_result(result), m_enrolleeStatus(status)
509             {
510             }
511
512             ESResult getESResult()
513             {
514                 return m_result;
515             }
516
517             const EnrolleeStatus& getEnrolleeStatus()
518             {
519                 return m_enrolleeStatus;
520             }
521
522         private:
523             ESResult m_result;
524             EnrolleeStatus m_enrolleeStatus;
525         };
526
527         /**
528          * Status object for getConfiguration API. This object is given to application
529          * when a response for GET request to provisioning resource at Enrollee is arrived.
530          * It returns a result of the API and requested data delivered in the response which includes
531          * WiFi configuration and device configuration stored in Enrollee.
532          *
533          * @see EnrolleeConf
534          */
535         class GetConfigurationStatus
536         {
537         public:
538             GetConfigurationStatus(ESResult result, const EnrolleeConf& conf) :
539                     m_result(result), m_enrolleeConf(conf)
540             {
541             }
542
543             ESResult getESResult()
544             {
545                 return m_result;
546             }
547
548             const EnrolleeConf& getEnrolleeConf()
549             {
550                 return m_enrolleeConf;
551             }
552
553         private:
554             ESResult m_result;
555             EnrolleeConf m_enrolleeConf;
556         };
557
558         /**
559          * Status object for provisionDeviceProperties API. This object is given to application
560          * when a response for GET request to provisioning resource at Enrollee is arrived.
561          * It returns a result of the request.
562          */
563         class DevicePropProvisioningStatus
564         {
565         public:
566             DevicePropProvisioningStatus(ESResult result) :
567                     m_result(result)
568             {
569             }
570
571             ESResult getESResult()
572             {
573                 return m_result;
574             }
575
576
577         private:
578             ESResult m_result;
579         };
580
581         /**
582          * Status object for provisionCloudProperties API. This object is given to application
583          * when a response for GET request to provisioning resource at Enrollee is arrived.
584          * It returns a result of the request and status of this provisioning. The status provides
585          * an information if the enrollee is found in a given network and the provisioning is
586          * successfully done.
587          */
588         class CloudPropProvisioningStatus
589         {
590         public:
591             CloudPropProvisioningStatus(ESResult result, ESCloudProvState state) :
592                     m_result(result), m_esCloudState(state)
593             {
594             }
595
596             ESResult getESResult()
597             {
598                 return m_result;
599             }
600
601             ESCloudProvState getESCloudState()
602             {
603                 return m_esCloudState;
604             }
605
606         private:
607             ESResult m_result;
608             ESCloudProvState m_esCloudState;
609         };
610
611         /**
612          * Callback function definition for providing Enrollee status
613          */
614         typedef function< void(shared_ptr< GetEnrolleeStatus >) > GetStatusCb;
615
616         /**
617          * Callback function definition for providing Enrollee configuration status
618          */
619         typedef function< void(shared_ptr< GetConfigurationStatus >) > GetConfigurationStatusCb;
620
621         /**
622          * Callback function definition for providing Enrollee device property provisioning status
623          */
624         typedef function< void(shared_ptr< DevicePropProvisioningStatus >) > DevicePropProvStatusCb;
625
626         /**
627          * Callback function definition for providing Enrollee cloud property provisioning status
628          */
629         typedef function< void(shared_ptr< CloudPropProvisioningStatus >) > CloudPropProvStatusCb;
630
631         /**
632          * Callback function definition for providing Enrollee security provisioning status
633          */
634         typedef function< void(shared_ptr<SecProvisioningStatus>) > SecurityProvStatusCb;
635
636         /**
637          * Callback definition to be invoked when the security stack expects a pin from application
638          */
639         typedef function< void(string&) > SecurityPinCb;
640
641         /**
642          * Callback definition to be invoked when the stack expects a db path
643          */
644         typedef function< void(string&) > SecProvisioningDbPathCb;
645
646     }
647 }
648 #endif //WITH_ARDUINO
649
650 #endif //ES_COMMON_RICH_H_