Sample implementation of ocf_light
[iotivity.git] / resource / examples / ocf_light / Platform.cpp
1 /*
2  * Platform.cpp
3  *
4  *  Created on: Feb 12, 2018
5  *      Author: georgena
6  */
7 #include <iostream>
8
9 #include "OCPlatform.h"
10 #include "Platform.h"
11
12 using namespace OC;
13
14 /**
15  *  server_fopen
16  *  opens file
17  *  implements redirection to open:
18  * - initial security settings
19  * - introspection file
20  * @param path path+filename of the file to open
21  * @param mode mode of the file to open
22  * @return the filehandle of the opened file (or error)
23  */
24 FILE* server_fopen(const char* path, const char* mode)
25 {
26     FILE* fileptr = NULL;
27
28     if (0 == strcmp(path, OC_SECURITY_DB_DAT_FILE_NAME))
29     {
30         // reading the security initial setup file
31         fileptr = fopen("server_security.dat", mode);
32         std::cout << "reading security file 'server_security.dat' ptr: " << fileptr << std::endl;
33         return fileptr;
34     }
35     else if (0 == strcmp(path, OC_INTROSPECTION_FILE_NAME))
36     {
37         // reading the introspection file
38         fileptr = fopen("server_introspection.dat", mode);
39         std::cout << "reading introspection file  'server_introspection.dat' ptr: " << fileptr << std::endl;
40         return fileptr;
41     }
42     else
43     {
44         std::cout << "persistent storage - server_fopen: " << path << std::endl;
45         return fopen(path, mode);
46     }
47 }
48
49 // Create persistent storage handlers
50 OCPersistentStorage ps{server_fopen, fread, fwrite, fclose, unlink};
51
52 Platform::Platform(void)
53 {
54     std::cout << "Running Platform constructor" << std::endl;
55     dataModelVersions.push_back("ocf.res.1.3.0");
56     dataModelVersions.push_back("ocf.sh.1.3.0");
57     // create the platform
58     PlatformConfig cfg
59     {
60         ServiceType::InProc,
61         ModeType::Server,
62         &ps
63     };
64     OCPlatform::Configure(cfg);
65     setPlatformInfo(m_platformId, m_manufacturerName, m_manufacturerLink,
66                     m_modelNumber, m_dateOfManufacture, m_platformVersion, m_operatingSystemVersion,
67                     m_hardwareVersion, m_firmwareVersion, m_supportLink, m_systemTime);
68
69 }
70
71 Platform::~Platform(void)
72 {
73     std::cout << "Running Platform destructor" << std::endl;
74     deletePlatformInfo();
75 }
76
77 OCStackResult Platform::start(void)
78 {
79     return OCPlatform::start();
80 }
81
82 OCStackResult Platform::registerPlatformInfo(void)
83 {
84     OCStackResult result = OC_STACK_ERROR;
85     result = OCPlatform::registerPlatformInfo(platformInfo);
86     return result;
87 }
88
89
90 OCPlatformInfo* Platform::getPlatformInfo(void)
91 {
92     return &platformInfo;
93 }
94
95 OCStackResult Platform::stop(void)
96 {
97     return OCPlatform::stop();
98 }
99
100 /**
101  *  DuplicateString
102  *
103  * @param targetString  destination string, will be allocated
104  * @param sourceString  source string, e.g. will be copied
105
106  *  TODO: don't use strncpy
107  */
108 void DuplicateString(char ** targetString, std::string sourceString)
109 {
110     *targetString = new char[sourceString.length() + 1];
111     strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
112 }
113
114 void Platform::setPlatformInfo(std::string platformID, std::string manufacturerName,
115         std::string manufacturerUrl, std::string modelNumber, std::string dateOfManufacture,
116         std::string platformVersion, std::string operatingSystemVersion,
117         std::string hardwareVersion, std::string firmwareVersion, std::string supportUrl,
118         std::string systemTime)
119 {
120     DuplicateString(&platformInfo.platformID, platformID);
121     DuplicateString(&platformInfo.manufacturerName, manufacturerName);
122     DuplicateString(&platformInfo.manufacturerUrl, manufacturerUrl);
123     DuplicateString(&platformInfo.modelNumber, modelNumber);
124     DuplicateString(&platformInfo.dateOfManufacture, dateOfManufacture);
125     DuplicateString(&platformInfo.platformVersion, platformVersion);
126     DuplicateString(&platformInfo.operatingSystemVersion, operatingSystemVersion);
127     DuplicateString(&platformInfo.hardwareVersion, hardwareVersion);
128     DuplicateString(&platformInfo.firmwareVersion, firmwareVersion);
129     DuplicateString(&platformInfo.supportUrl, supportUrl);
130     DuplicateString(&platformInfo.systemTime, systemTime);
131 }
132
133 void Platform::deletePlatformInfo(void)
134 {
135     delete[] platformInfo.platformID;
136     delete[] platformInfo.manufacturerName;
137     delete[] platformInfo.manufacturerUrl;
138     delete[] platformInfo.modelNumber;
139     delete[] platformInfo.dateOfManufacture;
140     delete[] platformInfo.platformVersion;
141     delete[] platformInfo.operatingSystemVersion;
142     delete[] platformInfo.hardwareVersion;
143     delete[] platformInfo.firmwareVersion;
144     delete[] platformInfo.supportUrl;
145     delete[] platformInfo.systemTime;
146 }
147
148 /**
149  *  SetDeviceInfo
150  *  Sets the device information ("oic/d"), from the globals
151
152  * @return OC_STACK_ERROR or OC_STACK_OK
153  */
154 OCStackResult Platform::setDeviceInfo()
155 {
156     OCStackResult result = OC_STACK_ERROR;
157
158     OCResourceHandle handle = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI);
159     if (handle == NULL)
160     {
161         std::cout << "Failed to find resource " << OC_RSRVD_DEVICE_URI << std::endl;
162         return result;
163     }
164     result = OCBindResourceTypeToResource(handle, deviceType.c_str());
165     if (result != OC_STACK_OK)
166     {
167         std::cout << "Failed to add device type" << std::endl;
168         return result;
169     }
170
171     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION,
172             dataModelVersions);
173     if (result != OC_STACK_OK)
174     {
175         std::cout << "Failed to set data model versions" << std::endl;
176         return result;
177     }
178
179     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion);
180     if (result != OC_STACK_OK)
181     {
182         std::cout << "Failed to set spec version" << std::endl;
183         return result;
184     }
185
186     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID,
187             protocolIndependentID);
188     if (result != OC_STACK_OK)
189     {
190         std::cout << "Failed to set piid" << std::endl;
191         return result;
192     }
193
194     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, deviceName);
195     if (result != OC_STACK_OK)
196     {
197         std::cout << "Failed to set device name" << std::endl;
198         return result;
199     }
200     return OC_STACK_OK;
201 }