-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Tizen] Implement the base functions (#9633)
* Add mainloop for Tizen OS * Add app preference for Tizen OS * Implement key/value store * Implement posix config * Implement system time support * Add tizen support for build * Replace duplicate code with function * Using a namespace instead of class
- Loading branch information
Showing
10 changed files
with
587 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Copyright (c) 2021 Project CHIP Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
declare_args() { | ||
# Root directory for tizen. | ||
tizen_root = "//config/tizen/chip-gn/platform" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/* | ||
* | ||
* Copyright (c) 2021 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "AppPreference.h" | ||
#include <app_preference.h> | ||
#include <lib/support/Base64.h> | ||
#include <lib/support/CHIPMem.h> | ||
|
||
namespace chip { | ||
namespace DeviceLayer { | ||
namespace PersistedStorage { | ||
namespace Internal { | ||
namespace AppPreference { | ||
|
||
static CHIP_ERROR __IsKeyExist(const char * key) | ||
{ | ||
CHIP_ERROR err = CHIP_NO_ERROR; | ||
int preErr = PREFERENCE_ERROR_NONE; | ||
bool isExist = false; | ||
|
||
preErr = preference_is_existing(key, &isExist); | ||
if (preErr != PREFERENCE_ERROR_NONE) | ||
{ | ||
err = CHIP_ERROR_INCORRECT_STATE; | ||
} | ||
else if (isExist == false) | ||
{ | ||
err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND; | ||
} | ||
|
||
return err; | ||
} | ||
|
||
CHIP_ERROR CheckData(const char * key) | ||
{ | ||
return __IsKeyExist(key); | ||
} | ||
|
||
CHIP_ERROR GetData(const char * key, void * data, size_t dataSize, size_t * getDataSize, size_t offset) | ||
{ | ||
CHIP_ERROR err = CHIP_NO_ERROR; | ||
int preErr = PREFERENCE_ERROR_NONE; | ||
char * encodedData = NULL; | ||
uint8_t * decodedData = NULL; | ||
size_t encodedDataSize = 0; | ||
size_t encodedDataPaddingSize = 0; | ||
size_t decodedDataSize = 0; | ||
size_t expectedDecodedSize = 0; | ||
size_t copy_size = 0; | ||
|
||
VerifyOrExit(data != NULL, err = CHIP_ERROR_INVALID_ARGUMENT); | ||
|
||
err = __IsKeyExist(key); | ||
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogProgress(DeviceLayer, "Not found data [%s]", key)); | ||
|
||
preErr = preference_get_string(key, &encodedData); | ||
VerifyOrExit(preErr == PREFERENCE_ERROR_NONE, err = CHIP_ERROR_INCORRECT_STATE); | ||
encodedDataSize = strlen(encodedData); | ||
|
||
if ((encodedDataSize > 0) && (encodedData[encodedDataSize - 1] == '=')) | ||
{ | ||
encodedDataPaddingSize++; | ||
if ((encodedDataSize > 1) && (encodedData[encodedDataSize - 2] == '=')) | ||
encodedDataPaddingSize++; | ||
} | ||
expectedDecodedSize = ((encodedDataSize - encodedDataPaddingSize) * 3) / 4; | ||
|
||
decodedData = static_cast<uint8_t *>(chip::Platform::MemoryAlloc(expectedDecodedSize)); | ||
VerifyOrExit(decodedData != NULL, err = CHIP_ERROR_INCORRECT_STATE); | ||
|
||
decodedDataSize = Base64Decode(encodedData, static_cast<uint16_t>(encodedDataSize), decodedData); | ||
|
||
copy_size = min(dataSize, decodedDataSize - offset); | ||
if (getDataSize != NULL) | ||
{ | ||
*getDataSize = copy_size; | ||
} | ||
memset(data, 0, dataSize); | ||
memcpy(data, decodedData + offset, copy_size); | ||
|
||
ChipLogProgress(DeviceLayer, "Get data [%s:%s]", key, (char *) data); | ||
|
||
chip::Platform::MemoryFree(decodedData); | ||
|
||
VerifyOrExit(dataSize >= decodedDataSize - offset, err = CHIP_ERROR_BUFFER_TOO_SMALL); | ||
|
||
exit: | ||
free(encodedData); | ||
return err; | ||
} | ||
|
||
CHIP_ERROR SaveData(const char * key, const uint8_t * data, size_t dataSize) | ||
{ | ||
CHIP_ERROR err = CHIP_NO_ERROR; | ||
int preErr = PREFERENCE_ERROR_NONE; | ||
char * encodedData = NULL; | ||
size_t encodedDataSize = 0; | ||
size_t expectedEncodedSize = ((dataSize + 3) * 4) / 3; | ||
|
||
err = __IsKeyExist(key); | ||
if (err == CHIP_NO_ERROR) | ||
{ | ||
VerifyOrExit(RemoveData(key) == CHIP_NO_ERROR, err = CHIP_ERROR_INCORRECT_STATE); | ||
} | ||
err = CHIP_NO_ERROR; | ||
|
||
encodedData = static_cast<char *>(chip::Platform::MemoryAlloc(expectedEncodedSize)); | ||
VerifyOrExit(encodedData != NULL, err = CHIP_ERROR_INCORRECT_STATE); | ||
|
||
encodedDataSize = Base64Encode(data, static_cast<uint16_t>(dataSize), encodedData); | ||
encodedData[encodedDataSize] = 0; | ||
|
||
preErr = preference_set_string(key, encodedData); | ||
if (preErr == PREFERENCE_ERROR_NONE) | ||
{ | ||
ChipLogProgress(DeviceLayer, "Save data [%s:%s]", key, data); | ||
} | ||
else | ||
{ | ||
ChipLogProgress(DeviceLayer, "FAIL: set string [%s]", get_error_message(preErr)); | ||
err = CHIP_ERROR_INCORRECT_STATE; | ||
} | ||
|
||
chip::Platform::MemoryFree(encodedData); | ||
|
||
exit: | ||
return err; | ||
} | ||
|
||
CHIP_ERROR RemoveData(const char * key) | ||
{ | ||
CHIP_ERROR err = CHIP_NO_ERROR; | ||
int preErr = PREFERENCE_ERROR_NONE; | ||
|
||
preErr = preference_remove(key); | ||
if (preErr == PREFERENCE_ERROR_NONE) | ||
{ | ||
ChipLogProgress(DeviceLayer, "Remove data [%s]", key); | ||
} | ||
else | ||
{ | ||
ChipLogProgress(DeviceLayer, "FAIL: remove preference [%s]", get_error_message(preErr)); | ||
err = CHIP_ERROR_INCORRECT_STATE; | ||
} | ||
|
||
return err; | ||
} | ||
|
||
} // namespace AppPreference | ||
} // namespace Internal | ||
} // namespace PersistedStorage | ||
} // namespace DeviceLayer | ||
} // namespace chip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* | ||
* Copyright (c) 2021 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <app_preference.h> | ||
#include <platform/CHIPDeviceLayer.h> | ||
|
||
namespace chip { | ||
namespace DeviceLayer { | ||
namespace PersistedStorage { | ||
namespace Internal { | ||
namespace AppPreference { | ||
|
||
CHIP_ERROR CheckData(const char * key); | ||
CHIP_ERROR GetData(const char * key, void * data, size_t dataSize, size_t * getDataSize, size_t offset); | ||
CHIP_ERROR SaveData(const char * key, const uint8_t * data, size_t size); | ||
CHIP_ERROR RemoveData(const char * key); | ||
|
||
} // namespace AppPreference | ||
} // namespace Internal | ||
} // namespace PersistedStorage | ||
} // namespace DeviceLayer | ||
} // namespace chip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.