Skip to content

Commit

Permalink
[Tizen] Implement the base functions (#9633)
Browse files Browse the repository at this point in the history
* 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
hyunuktak authored and pull[bot] committed Sep 30, 2021
1 parent bd948a2 commit 22cea95
Show file tree
Hide file tree
Showing 10 changed files with 587 additions and 23 deletions.
18 changes: 18 additions & 0 deletions build_overrides/tizen.gni
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"
}
167 changes: 167 additions & 0 deletions src/platform/Tizen/AppPreference.cpp
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
38 changes: 38 additions & 0 deletions src/platform/Tizen/AppPreference.h
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
4 changes: 4 additions & 0 deletions src/platform/Tizen/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ static_library("Tizen") {
sources = [
"../DeviceSafeQueue.cpp",
"../DeviceSafeQueue.h",
"AppPreference.cpp",
"AppPreference.h",
"BLEManagerImpl.cpp",
"BLEManagerImpl.h",
"BlePlatformConfig.h",
Expand All @@ -42,6 +44,8 @@ static_library("Tizen") {
"KeyValueStoreManagerImpl.cpp",
"KeyValueStoreManagerImpl.h",
"Logging.cpp",
"MainLoop.cpp",
"MainLoop.h",
"PlatformManagerImpl.cpp",
"PlatformManagerImpl.h",
"PosixConfig.cpp",
Expand Down
12 changes: 9 additions & 3 deletions src/platform/Tizen/KeyValueStoreManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* Platform-specific key value storage implementation for Tizen.
*/

#include "AppPreference.h"
#include <platform/KeyValueStoreManager.h>

namespace chip {
Expand All @@ -29,20 +30,25 @@ namespace PersistedStorage {

KeyValueStoreManagerImpl KeyValueStoreManagerImpl::sInstance;

CHIP_ERROR KeyValueStoreManagerImpl::_Check(const char * key)
{
return Internal::AppPreference::CheckData(key);
}

CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t value_size, size_t * read_bytes_size,
size_t offset_bytes)
{
return CHIP_ERROR_NOT_IMPLEMENTED;
return Internal::AppPreference::GetData(key, value, value_size, read_bytes_size, offset_bytes);
}

CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, size_t value_size)
{
return CHIP_ERROR_NOT_IMPLEMENTED;
return Internal::AppPreference::SaveData(key, reinterpret_cast<const uint8_t *>(value), value_size);
}

CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key)
{
return CHIP_ERROR_NOT_IMPLEMENTED;
return Internal::AppPreference::RemoveData(key);
}

} // namespace PersistedStorage
Expand Down
1 change: 1 addition & 0 deletions src/platform/Tizen/KeyValueStoreManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class KeyValueStoreManagerImpl final : public KeyValueStoreManager
friend class KeyValueStoreManager;

public:
CHIP_ERROR _Check(const char * key);
CHIP_ERROR _Get(const char * key, void * value, size_t value_size, size_t * read_bytes_size = nullptr, size_t offset = 0);
CHIP_ERROR _Delete(const char * key);
CHIP_ERROR _Put(const char * key, const void * value, size_t value_size);
Expand Down
Loading

0 comments on commit 22cea95

Please sign in to comment.