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