Update a policy of successful cloud provisioning
[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)
57             {
58                 m_rep = rep;
59             }
60
61             EnrolleeStatus(const EnrolleeStatus& enrolleeStatus) :
62                 m_rep(enrolleeStatus.getRepresentation())
63             {
64             }
65
66             EnrolleeStatus(const EnrolleeStatus&& enrolleeStatus) :
67                 m_rep(std::move(enrolleeStatus.getRepresentation()))
68             {
69             }
70
71             /**
72              * Get a provisioning status property of Enrollee.
73              *
74              * @return a provisioning status property of Enrollee
75              */
76             ProvStatus getProvStatus()
77             {
78                 if(m_rep.hasAttribute(OC_RSRVD_ES_PROVSTATUS))
79                 {
80                     return static_cast<ProvStatus>(
81                                         m_rep.getValue<int>(OC_RSRVD_ES_PROVSTATUS));
82                 }
83                 return ES_STATE_INIT;
84             }
85
86             /**
87              * Get a last error code property of Enrollee.
88              *
89              * @return a last error code property of Enrollee.
90              */
91             ESErrorCode getLastErrCode()
92             {
93                 if(m_rep.hasAttribute(OC_RSRVD_ES_LAST_ERRORCODE))
94                 {
95                     return static_cast<ESErrorCode>(
96                                         m_rep.getValue<int>(OC_RSRVD_ES_LAST_ERRORCODE));
97                 }
98                 return ES_ERRCODE_NO_ERROR;
99             }
100
101             /**
102              * Get OCRepresentation object
103              *
104              * @return OCRepresentation object
105              */
106             const OCRepresentation& getRepresentation() const
107             {
108                 return m_rep;
109             }
110         protected:
111             OCRepresentation m_rep;
112         };
113
114         /**
115          * @brief Data class stored for Cloud server property provisioning
116          */
117         class CloudProp
118         {
119         public:
120
121             /**
122              * Constructor
123              */
124             CloudProp()
125             {
126                 m_cloudID = "";
127                 m_credID = -1;
128             }
129
130             CloudProp(const CloudProp& cloudProp) :
131                                             m_rep(cloudProp.toOCRepresentation()),
132                                             m_cloudID(cloudProp.getCloudID()),
133                                             m_credID(cloudProp.getCredID())
134             {
135             }
136
137             CloudProp(const CloudProp&& cloudProp) :
138                                             m_rep(std::move(cloudProp.toOCRepresentation())),
139                                             m_cloudID(cloudProp.getCloudID()),
140                                             m_credID(cloudProp.getCredID())
141             {
142             }
143
144             /**
145              * Constructor with OCRepresentation object. This is used for JNI communication.
146              */
147             CloudProp(const OCRepresentation &rep)
148             {
149                 m_rep = rep;
150                 m_cloudID = "";
151             }
152
153             /**
154              * Set CloudServer resource properties to be delivered to Enrollee
155              *
156              * @param authCode  Auth code issued by OAuth2.0-compatible account server
157              * @param authProvider Auth provider ID
158              * @param ciServer Cloud interface server URL which an Enrollee is going to registered
159              */
160             void setCloudProp(string authCode, string authProvider, string ciServer)
161             {
162                 m_rep.setValue(OC_RSRVD_ES_AUTHCODE, authCode);
163                 m_rep.setValue(OC_RSRVD_ES_AUTHPROVIDER, authProvider);
164                 m_rep.setValue(OC_RSRVD_ES_CISERVER, ciServer);
165             }
166
167             /**
168              * Set CloudServer's UUID
169              *
170              * @param cloudID Cloud Interface server's UUID
171              */
172             void setCloudID(string cloudID)
173             {
174                 m_cloudID = cloudID;
175             }
176
177             /**
178              * Set CloudServer's credential ID of certificate
179              *
180              * @param credID Cloud Interface server's credential ID of certificate
181              */
182             void setCredID(int credID)
183             {
184                 m_credID = credID;
185             }
186
187             /**
188              * Get an auth code to be delivered.
189              *
190              * @return an auth code to be delivered.
191              */
192             std::string getAuthCode() const
193             {
194                 if(m_rep.hasAttribute(OC_RSRVD_ES_AUTHCODE))
195                 {
196                     return m_rep.getValue<std::string>(OC_RSRVD_ES_AUTHCODE);
197                 }
198                 return std::string("");
199             }
200
201             /**
202              * Get an auth provider which issued an auth code
203              *
204              * @return an auth provider which issued an auth code
205              */
206             std::string getAuthProvider() const
207             {
208                 if(m_rep.hasAttribute(OC_RSRVD_ES_AUTHPROVIDER))
209                 {
210                     return m_rep.getValue<std::string>(OC_RSRVD_ES_AUTHPROVIDER);
211                 }
212                 return std::string("");
213             }
214
215             /**
216              * Get a CI server to be delivered
217              *
218              * @return a CI server to be delivered
219              */
220             std::string getCiServer() const
221             {
222                 if(m_rep.hasAttribute(OC_RSRVD_ES_CISERVER))
223                 {
224                     return m_rep.getValue<std::string>(OC_RSRVD_ES_CISERVER);
225                 }
226                 return std::string("");
227             }
228
229             /**
230              * Get a CI server's Uuid to be delivered
231              *
232              * @return a CI server's Uuid to be delivered
233              */
234             std::string getCloudID() const
235             {
236                 return m_cloudID;
237             }
238
239             /**
240              * Get a CI server's credential ID of certificate
241              *
242              * @return a CI server's credential ID of certificated
243              */
244             int getCredID() const
245             {
246                 return m_credID;
247             }
248
249             /**
250              * Get OCRepresentation object
251              *
252              * @return OCRepresentation object
253              */
254             const OCRepresentation &toOCRepresentation() const
255             {
256                 return m_rep;
257             }
258         protected:
259             OCRepresentation m_rep;
260             std::string m_cloudID;
261             int m_credID;
262         };
263
264         /**
265          * @brief Data class stored for Device property provisioning which includes a WiFi
266          *        and device configuration provisioning
267          */
268         class DeviceProp
269         {
270         public:
271
272             /**
273              * Constructor
274              */
275             DeviceProp()
276             {
277             }
278
279             DeviceProp(const DeviceProp& deviceProp) :
280                 m_rep(deviceProp.toOCRepresentation())
281             {
282             }
283
284             DeviceProp(const DeviceProp&& deviceProp) :
285                 m_rep(std::move(deviceProp.toOCRepresentation()))
286             {
287             }
288
289             /**
290              * Constructor with OCRepresentation object. This is used for JNI communication.
291              */
292             DeviceProp(const OCRepresentation &rep)
293             {
294                 m_rep = rep;
295             }
296
297             /**
298              * Set WiFi resource properties to be delivered to Enrollee
299              *
300              * @param ssid Ssid of the Enroller
301              * @param pwd Pwd of the Enrolle
302              * @param authtype Auth type of the Enroller
303              * @param enctype Encryption type of the Enroller
304              *
305              * @see WIFI_AUTHTYPE
306              * @see WIFI_ENCTYPE
307              */
308             void setWiFiProp(string ssid, string pwd, WIFI_AUTHTYPE authtype, WIFI_ENCTYPE enctype)
309             {
310                 m_rep.setValue(OC_RSRVD_ES_SSID, ssid);
311                 m_rep.setValue(OC_RSRVD_ES_CRED, pwd);
312                 m_rep.setValue(OC_RSRVD_ES_AUTHTYPE, authtype);
313                 m_rep.setValue(OC_RSRVD_ES_ENCTYPE, enctype);
314             }
315
316             /**
317              * Set DevConf resource properties to be delivered to Enrollee
318              *
319              * @param language IETF language tag using ISO 639X
320              * @param country ISO Country Code (ISO 3166-1 Alpha-2)
321              */
322             void setDevConfProp(string language, string country, string location)
323             {
324                 m_rep.setValue(OC_RSRVD_ES_LANGUAGE, language);
325                 m_rep.setValue(OC_RSRVD_ES_COUNTRY, country);
326                 m_rep.setValue(OC_RSRVD_ES_LOCATION, location);
327             }
328
329             /**
330              * Get a SSID of Enroller
331              *
332              * @return a SSID of enroller
333              */
334             std::string getSsid() const
335             {
336                 if(m_rep.hasAttribute(OC_RSRVD_ES_SSID))
337                 {
338                     return m_rep.getValue<std::string>(OC_RSRVD_ES_SSID);
339                 }
340                 return std::string("");
341             }
342
343             /**
344              * Get a password of Enroller
345              *
346              * @return a password of enroller
347              */
348             std::string getPassword() const
349             {
350                 if(m_rep.hasAttribute(OC_RSRVD_ES_CRED))
351                 {
352                     return m_rep.getValue<std::string>(OC_RSRVD_ES_CRED);
353                 }
354                 return std::string("");
355             }
356
357             /**
358              * Get an auth type of Enroller
359              *
360              * @return an auth type of enroller
361              *
362              * @see WIFI_AUTHTYPE
363              */
364             WIFI_AUTHTYPE getAuthType() const
365             {
366                 if(m_rep.hasAttribute(OC_RSRVD_ES_AUTHTYPE))
367                 {
368                     return static_cast<WIFI_AUTHTYPE>(m_rep.getValue<int>(OC_RSRVD_ES_AUTHTYPE));
369                 }
370                 return NONE_AUTH;
371             }
372
373             /**
374              * Get an encryption type of Enroller
375              *
376              * @return an encryption type of enroller
377              *
378              * @see WIFI_ENCTYPE
379              */
380             WIFI_ENCTYPE getEncType() const
381             {
382                 if(m_rep.hasAttribute(OC_RSRVD_ES_ENCTYPE))
383                 {
384                     return static_cast<WIFI_ENCTYPE>(m_rep.getValue<int>(OC_RSRVD_ES_ENCTYPE));
385                 }
386                 return NONE_ENC;
387             }
388
389             /**
390              * Get a language to be set. A language is expressed in IETF language tag
391              * using ISO 639X.
392              *
393              * @return a language to be set
394              */
395             std::string getLanguage() const
396             {
397                 if(m_rep.hasAttribute(OC_RSRVD_ES_LANGUAGE))
398                 {
399                     return m_rep.getValue<std::string>(OC_RSRVD_ES_LANGUAGE);
400                 }
401                 return std::string("");
402             }
403
404             /**
405              * Get a country to be set. A country is expressed in ISO Country Code
406              * (ISO 3166-1 Alpha-2)
407              *
408              * @return a country to be set
409              */
410             std::string getCountry() const
411             {
412                 if(m_rep.hasAttribute(OC_RSRVD_ES_COUNTRY))
413                 {
414                     return m_rep.getValue<std::string>(OC_RSRVD_ES_COUNTRY);
415                 }
416                 return std::string("");
417             }
418
419             /**
420              * Get a location to be set. A location is GPS information
421              *
422              * @return a country to be set
423              */
424             std::string getLocation() const
425             {
426                 if(m_rep.hasAttribute(OC_RSRVD_ES_LOCATION))
427                 {
428                     return m_rep.getValue<std::string>(OC_RSRVD_ES_LOCATION);
429                 }
430                 return std::string("");
431             }
432
433             /**
434              * Get OCRepresentation object
435              *
436              * @return OCRepresentation object
437              */
438             const OCRepresentation &toOCRepresentation() const
439             {
440                 return m_rep;
441             }
442
443         protected:
444             OCRepresentation m_rep;
445         };
446
447         /**
448          * Security Provisioning Status
449          */
450         class SecProvisioningStatus
451         {
452         public:
453             SecProvisioningStatus(string deviceUUID, ESResult result) :
454                 m_devUUID(deviceUUID), m_result(result)
455             {
456             }
457
458             const string getDeviceUUID()
459             {
460                 return m_devUUID;
461             }
462
463             ESResult getESResult()
464             {
465                 return m_result;
466             }
467         private:
468             string m_devUUID;
469             ESResult m_result;
470         };
471
472         /**
473          * @breif This provide a set of getter APIs from received response for getConfiguration().
474          *        Received information includes a device name, WiFi supported mode, and frequency.
475          *        Additionally, you can know if Enrollee can be access to cloud server with this
476          *        object.
477          */
478         class EnrolleeConf
479         {
480         public:
481             /**
482              * Constructor
483              * The expected OCRepresentation is one for collection resource and has several child
484              * OCRepresentation object corresponding to WiFi, DevConf, and CloudServer resource's
485              * representation.
486              */
487             EnrolleeConf(const OCRepresentation& rep) :
488                 m_ProvRep(rep)
489             {
490             }
491
492             EnrolleeConf(const EnrolleeConf& enrolleeConf) :
493                 m_ProvRep(enrolleeConf.getProvResRep())
494             {
495             }
496
497             EnrolleeConf(const EnrolleeConf&& enrolleeConf) :
498                 m_ProvRep(std::move(enrolleeConf.getProvResRep()))
499             {
500             }
501
502             /**
503              * Get a device name of Enrollee. It is Device's human-friendly name like device model
504              * name.
505              *
506              * @return a device name of Enrollee
507              */
508             std::string getDeviceName() const
509             {
510                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
511                 for(auto child = children.begin(); child != children.end(); ++child)
512                 {
513                     if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos)
514                     {
515                         if(child->hasAttribute(OC_RSRVD_ES_DEVNAME))
516                         {
517                             return child->getValue<std::string>(OC_RSRVD_ES_DEVNAME);
518                         }
519                     }
520                 }
521                 return std::string("");
522             }
523
524             /**
525              * Get a model number of Enrollee.
526              *
527              * @return a model number of Enrollee
528              */
529             std::string getModelNumber() const
530             {
531                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
532                 for(auto child = children.begin(); child != children.end(); ++child)
533                 {
534                     if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos)
535                     {
536                         if(child->hasAttribute(OC_RSRVD_ES_MODELNUMBER))
537                         {
538                             return child->getValue<std::string>(OC_RSRVD_ES_MODELNUMBER);
539                         }
540                     }
541                 }
542                 return std::string("");
543             }
544
545             /**
546              * Get a set of WiFi supported modes of Enrollee
547              *
548              * @return a set of WiFi supported modes of Enrollee
549              *
550              * @see WIFI_MODE
551              */
552             vector<WIFI_MODE> getWiFiModes() const
553             {
554                 vector<WIFI_MODE> modes;
555                 modes.clear();
556
557                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
558                 for(auto child = children.begin(); child != children.end(); ++child)
559                 {
560                     if(child->getUri().find(OC_RSRVD_ES_URI_WIFI) != std::string::npos)
561                     {
562                         if(child->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIMODE))
563                         {
564                             for(auto it : child->getValue
565                                         <std::vector<int>>(OC_RSRVD_ES_SUPPORTEDWIFIMODE))
566                             {
567                                 modes.push_back(static_cast<WIFI_MODE>(it));
568                             }
569                         }
570                     }
571                 }
572                 return modes;
573             }
574
575             /**
576              * Get a WiFi supported frequency of Enrollee
577              *
578              * @return a WiFi supported frequency of Enrollee
579              *
580              * @see WIFI_FREQ
581              */
582             WIFI_FREQ getWiFiFreq() const
583             {
584                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
585                 for(auto child = children.begin(); child != children.end(); ++child)
586                 {
587                     if(child->getUri().find(OC_RSRVD_ES_URI_WIFI) != std::string::npos)
588                     {
589                         if(child->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIFREQ))
590                         {
591                             return static_cast<WIFI_FREQ>(
592                                         child->getValue<int>(OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
593                         }
594                     }
595                 }
596                 return WIFI_FREQ_NONE;
597             }
598
599             /**
600              * Get an accessibility to cloud server of an Enrollee
601              *
602              * @return an accessibility to cloud server of an Enrollee
603              */
604             bool isCloudAccessible() const
605             {
606                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
607                 for(auto child = children.begin(); child != children.end(); ++child)
608                 {
609                     if(child->getUri().find(OC_RSRVD_ES_URI_CLOUDSERVER) != std::string::npos)
610                     {
611                         return true;
612                     }
613                 }
614                 return false;
615             }
616
617             /**
618              * Get OCRepresentation object
619              *
620              * @return OCRepresentation object
621              */
622             const OCRepresentation& getProvResRep() const
623             {
624                 return m_ProvRep;
625             }
626
627         protected:
628             OCRepresentation m_ProvRep;
629         };
630
631         /**
632          * Status object for getStatus API. This object is given to application
633          * when a response for GET request to provisioning resource at Enrollee is arrived.
634          * It returns a result of the API and requested data delivered in the response which includes
635          * a provisioning status and last error code stored in Enrollee.
636          *
637          * @see EnrolleeStatus
638          */
639         class GetEnrolleeStatus
640         {
641         public:
642             /**
643              * Constructor
644              */
645             GetEnrolleeStatus(ESResult result, const EnrolleeStatus& status) :
646                 m_result(result), m_enrolleeStatus(status)
647             {
648             }
649
650             /**
651              * Get a result of getting provisioning status and last error code of Enrollee
652              *
653              * @return a result of getting provisioning status and last error code of Enrollee
654              *
655              * @see ESResult
656              */
657             ESResult getESResult()
658             {
659                 return m_result;
660             }
661
662             /**
663              * Get Enrollee's status and last error code properties
664              *
665              * @return Enrollee's status and last error code properties
666              *
667              * @see EnrolleeStatus
668              */
669             const EnrolleeStatus& getEnrolleeStatus()
670             {
671                 return m_enrolleeStatus;
672             }
673
674         private:
675             ESResult m_result;
676             EnrolleeStatus m_enrolleeStatus;
677         };
678
679         /**
680          * Status object for getConfiguration API. This object is given to application
681          * when a response for GET request to provisioning resource at Enrollee is arrived.
682          * It returns a result of the API and requested data delivered in the response which includes
683          * WiFi configuration and device configuration stored in Enrollee.
684          *
685          * @see EnrolleeConf
686          */
687         class GetConfigurationStatus
688         {
689         public:
690             /**
691              * Constructor
692              */
693             GetConfigurationStatus(ESResult result, const EnrolleeConf& conf) :
694                     m_result(result), m_enrolleeConf(conf)
695             {
696             }
697
698             /**
699              * Get a result of getting preconfiguration of Enrollee
700              *
701              * @return a result of preconfiguration of Enrollee
702              *
703              * @see ESResult
704              */
705             ESResult getESResult()
706             {
707                 return m_result;
708             }
709
710             /**
711              * Get Enrollee's pre-configuration properties
712              *
713              * @return Enrollee's pre-configuration properties
714              *
715              * @see EnrolleeConf
716              */
717             EnrolleeConf& getEnrolleeConf()
718             {
719                 return m_enrolleeConf;
720             }
721
722         private:
723             ESResult m_result;
724             EnrolleeConf m_enrolleeConf;
725         };
726
727         /**
728          * Status object for provisionDeviceProperties API. This object is given to application
729          * when a response for GET request to provisioning resource at Enrollee is arrived.
730          * It returns a result of the request.
731          */
732         class DevicePropProvisioningStatus
733         {
734         public:
735             /**
736              * Constructor
737              */
738             DevicePropProvisioningStatus(ESResult result) :
739                     m_result(result)
740             {
741             }
742
743             /**
744              * Get a result of Device property provisioning
745              *
746              * @return a result of Device property provisioning
747              *
748              * @see ESResult
749              */
750             ESResult getESResult()
751             {
752                 return m_result;
753             }
754
755         private:
756             ESResult m_result;
757         };
758
759         /**
760          * Status object for provisionCloudProperties API. This object is given to application
761          * when a response for GET request to provisioning resource at Enrollee is arrived.
762          * It returns a result of the request and status of this provisioning. The status provides
763          * an information if the enrollee is found in a given network and the provisioning is
764          * successfully done.
765          */
766         class CloudPropProvisioningStatus
767         {
768         public:
769             /**
770              * Constructor
771              */
772             CloudPropProvisioningStatus(ESResult result) :
773                     m_result(result)
774             {
775             }
776
777             /**
778              * Get a result of Cloud property provisioning
779              *
780              * @return a result of Cloud property provisioning
781              *
782              * @see ESResult
783              */
784             ESResult getESResult()
785             {
786                 return m_result;
787             }
788
789         private:
790             ESResult m_result;
791         };
792
793         /**
794          * Callback function definition for providing Enrollee status
795          */
796         typedef function< void(shared_ptr< GetEnrolleeStatus >) > GetStatusCb;
797
798         /**
799          * Callback function definition for providing Enrollee configuration status
800          */
801         typedef function< void(shared_ptr< GetConfigurationStatus >) > GetConfigurationStatusCb;
802
803         /**
804          * Callback function definition for providing Enrollee device property provisioning status
805          */
806         typedef function< void(shared_ptr< DevicePropProvisioningStatus >) > DevicePropProvStatusCb;
807
808         /**
809          * Callback function definition for providing Enrollee cloud property provisioning status
810          */
811         typedef function< void(shared_ptr< CloudPropProvisioningStatus >) > CloudPropProvStatusCb;
812
813         /**
814          * Callback function definition for providing Enrollee security provisioning status
815          */
816         typedef function< void(shared_ptr<SecProvisioningStatus>) > SecurityProvStatusCb;
817
818         /**
819          * Callback definition to be invoked when the security stack expects a pin from application
820          */
821         typedef function< void(string&) > SecurityPinCb;
822
823         /**
824          * Callback definition to be invoked when the stack expects a db path
825          */
826         typedef function< void(string&) > SecProvisioningDbPathCb;
827
828     }
829 }
830 #endif //WITH_ARDUINO
831
832 #endif //ES_COMMON_RICH_H_