1 /* *****************************************************************
3 * Copyright 2017 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * *****************************************************************/
22 #include "oic_malloc.h"
24 #include "securevirtualresourcetypes.h"
25 #include "srmresourcestrings.h"
26 #include "psinterface.h"
28 #include "pstatresource.h"
29 #include "svrdbeditorpstat.h"
34 static OicSecPstat_t *g_pstat = NULL;
36 typedef enum PstatModifyType
43 PSTAT_EDIT_ROWNERID = 6
46 void DeInitPstat(void)
48 DeletePstatBinData(g_pstat);
52 void RefreshPstat(void)
54 OCStackResult ocResult = OC_STACK_ERROR;
55 OicSecPstat_t *tmpPstat = NULL;
56 uint8_t *secPayload = NULL;
57 size_t payloadSize = 0;
59 ocResult = GetSecureVirtualDatabaseFromPS(OIC_JSON_PSTAT_NAME, &secPayload, &payloadSize);
60 if (OC_STACK_OK != ocResult)
62 PRINT_WARN("GetSecureVirtualDatabaseFromPS : %d", ocResult);
66 ocResult = CBORPayloadToPstat(secPayload, payloadSize, &tmpPstat);
67 if (OC_STACK_OK != ocResult)
69 PRINT_ERR("CBORPayloadToPstat : %d", ocResult);
82 static void UpdatePstat(void)
84 OCStackResult pstatResult = OC_STACK_ERROR;
85 uint8_t *pstatPayload = NULL;
86 size_t pstatPayloadSize = 0;
88 pstatResult = PstatToCBORPayload(g_pstat, &pstatPayload, &pstatPayloadSize, false);
89 if (OC_STACK_OK != pstatResult)
91 PRINT_ERR("PstatToCBORPayload error : %d", pstatResult);
94 pstatResult = UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, pstatPayload, pstatPayloadSize);
95 if (OC_STACK_OK != pstatResult)
97 PRINT_ERR("UpdateSecureResourceInPS error : %d", pstatResult);
98 OICFree(pstatPayload);
101 OICFree(pstatPayload);
104 static void PrintDpm(const OicSecDpm_t dpm)
106 PRINT_DATA("%d (", dpm);
110 PRINT_DATA(" NORMAL ");
114 PRINT_DATA(" RESET ");
116 if (dpm & TAKE_OWNER)
118 PRINT_DATA(" TAKE_OWNER ");
120 if (dpm & BOOTSTRAP_SERVICE)
122 PRINT_DATA(" BOOTSTRAP_SERVICE ");
124 if (dpm & SECURITY_MANAGEMENT_SERVICES)
126 PRINT_DATA(" SECURITY_MANAGEMENT_SERVICES ");
128 if (dpm & PROVISION_CREDENTIALS)
130 PRINT_DATA(" PROVISION_CREDENTIALS ");
132 if (dpm & PROVISION_ACLS)
134 PRINT_DATA(" PROVISION_ACLS ");
140 static void PrintDpom(const OicSecDpom_t dpom)
142 PRINT_DATA("%d (", dpom);
144 if (dpom & MULTIPLE_SERVICE_SERVER_DRIVEN)
146 PRINT_DATA(" MULTIPLE_SERVICE_SERVER_DRIVEN ");
148 if (dpom & SINGLE_SERVICE_SERVER_DRIVEN)
150 PRINT_DATA(" SINGLE_SERVICE_SERVER_DRIVEN ");
152 if (dpom & SINGLE_SERVICE_CLIENT_DRIVEN)
154 PRINT_DATA(" SINGLE_SERVICE_CLIENT_DRIVEN ");
159 void PrintPstat(void)
161 PRINT_INFO("\n\n********************* [%-20s] *********************",
165 PRINT_PROG("%15s : ", OIC_JSON_ISOP_NAME);
166 (g_pstat->isOp ? PrintString("True") : PrintString("False"));
169 PRINT_PROG("%15s : ", OIC_JSON_CM_NAME);
170 PrintDpm(g_pstat->cm);
173 PRINT_PROG("%15s : ", OIC_JSON_TM_NAME);
174 PrintDpm(g_pstat->tm);
177 PRINT_PROG("%15s : ", OIC_JSON_OM_NAME);
178 PrintDpom(g_pstat->om);
181 PRINT_PROG("%15s : \n", OIC_JSON_SM_NAME);
182 for (size_t i = 0; i < g_pstat->smLen; i++)
184 PRINT_DATA("supported mode #%zu : ", i + 1);
185 PrintDpom(g_pstat->sm[i]);
188 PRINT_PROG("%15s : ", OIC_JSON_ROWNERID_NAME);
189 PrintUuid(&(g_pstat->rownerID));
193 PRINT_PROG("pstat is empty.\n");
195 PRINT_INFO("********************* [%-20s] *********************",
199 static unsigned int InputDpm(void)
201 unsigned int dpm = 0;
202 unsigned int dpm_msk = 1; //default mask
203 const char *Operation_Mode[DPM_CNT] =
205 "RESET", "TAKE_OWNER", "BOOTSTRAP_SERVICE", "SECURITY_MANAGEMENT_SERVICES",
206 "PROVISION_CREDENTIALS", "PROVISION_ACLS"
209 for (int i = 0; i < DPM_CNT; i++)
214 PRINT_NORMAL("\tEnter %s Mode (y/n): ", Operation_Mode[i]);
215 for (int ret = 0; 1 != ret; )
217 ret = scanf("%c", &ans);
218 for ( ; 0x20 <= getchar(); ); // for removing overflow garbages
219 // '0x20<=code' is character region
221 if ('y' == ans || 'Y' == ans || 'n' == ans || 'N' == ans)
223 ans &= ~0x20; // for masking lower case, 'y/n'
226 PRINT_NORMAL("\tEntered Wrong Answer. Please Enter 'y/n' Again\n");
237 static unsigned int InputDpom()
239 unsigned int dpom = 0;
240 unsigned int dpom_msk = 1; //default mask
241 const char *Operation_Mode[DPOM_CNT] =
243 "MULTIPLE_SERVICE_SERVER_DRIVEN", "SINGLE_SERVICE_SERVER_DRIVEN",
244 "SINGLE_SERVICE_CLIENT_DRIVEN"
247 PRINT_PROG("SINGLE_SERVICE_CLIENT_DRIVEN is the only mode IoTivity supports currently\n");
249 for (int i = 0; i < DPOM_CNT; i++)
254 PRINT_NORMAL("\tEnter %s Mode (y/n): ", Operation_Mode[i]);
255 for (int ret = 0; 1 != ret; )
257 ret = scanf("%c", &ans);
258 for ( ; 0x20 <= getchar(); ); // for removing overflow garbages
259 // '0x20<=code' is character region
261 if ('y' == ans || 'Y' == ans || 'n' == ans || 'N' == ans)
263 ans &= ~0x20; // for masking lower case, 'y/n'
266 PRINT_NORMAL("\tEntered Wrong Answer. Please Enter 'y/n' Again\n");
277 static OicSecPstat_t *InputPstatData(void)
279 OicSecPstat_t *pstat = NULL;
282 pstat = (OicSecPstat_t *)OICCalloc(1, sizeof(OicSecPstat_t));
285 PRINT_ERR("Failed to allocate memory");
289 PRINT_PROG("\n\nPlease input the each entity of new pstat.\n");
291 isop = InputNumber("Input the isop value ( 0.False, 1.True ) : ");
292 if (isop < 0 || 1 < isop)
294 PRINT_ERR("Invalid input");
300 pstat->isOp = (1 == isop ? true : false);
303 PRINT_PROG("Input Current Mode\n");
304 pstat->cm = InputDpm();
305 PRINT_PROG("Input Target Mode\n");
306 pstat->tm = InputDpm();
307 PRINT_PROG("Input Operational Mode\n");
309 pstat->om = InputDpom();
310 pstat->smLen = 1; // supported only one dpom mode in current iotivity
311 pstat->sm = (OicSecDpom_t *)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
312 if (NULL == pstat->sm)
314 PRINT_ERR("InputPstatData : Failed to allocate memory");
318 for (size_t i = 0; i < pstat->smLen; i++)
320 PRINT_PROG("Input dpom #%zu", i);
321 pstat->sm[i] = InputDpom();
325 "\tInput the ROWNER UUID (e.g. 61646D69-6E44-6576-6963-655575696430) : ");
326 if (0 != InputUuid(&(pstat->rownerID)))
328 PRINT_ERR("InputUuid error");
336 static int ModifyPstat(void)
342 PRINT_ERR("pstat is NULL");
346 PRINT_PROG("\n\nPlease input the attribute you want to modify\n");
347 PRINT_DATA("\t%2d. Edit isop\n", PSTAT_EDIT_ISOP);
348 PRINT_DATA("\t%2d. Edit cm\n", PSTAT_EDIT_CM);
349 PRINT_DATA("\t%2d. Edit tm\n", PSTAT_EDIT_TM);
350 PRINT_DATA("\t%2d. Edit om\n", PSTAT_EDIT_OM);
351 PRINT_DATA("\t%2d. Edit sm\n", PSTAT_EDIT_SM);
352 PRINT_DATA("\t%2d. Edit rownerID\n", PSTAT_EDIT_ROWNERID);
353 PRINT_DATA("\t%2d. Back to the previous\n", BACK);
354 modifyMenu = InputNumber("Select the menu : ");
357 case PSTAT_EDIT_ISOP:
359 int isop = InputNumber("Input the isop value ( 0.False, 1.True ) : ");
360 if (isop < 0 || 1 < isop)
362 PRINT_ERR("Invalid input");
367 g_pstat->isOp = (1 == isop ? true : false);
372 PRINT_PROG("Input Current Mode\n");
373 g_pstat->cm = InputDpm();
376 PRINT_PROG("Input Target Mode\n");
377 g_pstat->tm = InputDpm();
380 PRINT_PROG("Input Operational Mode\n");
381 g_pstat->om = InputDpom();
385 OicSecDpom_t *sm = NULL;
388 sm = (OicSecDpom_t *)OICCalloc(smLen, sizeof(OicSecDpom_t));
391 PRINT_ERR("Failed to allocate memory");
394 for (size_t i = 0; i < smLen; i++)
396 PRINT_PROG("Input dpom #%zu", i);
401 OICFree(g_pstat->sm);
403 g_pstat->smLen = smLen;
407 case PSTAT_EDIT_ROWNERID:
409 "\tInput the ROWNER UUID (e.g. 61646D69-6E44-6576-6963-655575696430) : ");
410 ret = InputUuid(&(g_pstat->rownerID));
413 PRINT_ERR("InputUuid error");
418 PRINT_INFO("Back to the previous menu.");
421 PRINT_ERR("Wrong type Number");
428 void HandlePstatOperation(const SubOperationType_t cmd)
430 OCStackResult pstatResult = OC_STACK_ERROR;
432 if (SVR_EDIT_IDX_SIZE <= cmd)
434 PRINT_ERR("Invalid menu for pstat");
445 PRINT_ERR("Already exist");
450 g_pstat = InputPstatData();
453 PRINT_ERR("Failed to InputPstatData");
463 pstatResult = UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, NULL, 0);
464 if (OC_STACK_OK != pstatResult)
466 PRINT_ERR("UpdateSecureResourceInPS error : %d", pstatResult);
472 PRINT_ERR("pstat is NULL");
477 if (0 != ModifyPstat())
479 PRINT_ERR("Failed Modify");
482 PRINT_INFO("\n\nPstat Modified");
487 PRINT_ERR("Invalid menu for pstat");