From f3b7204fb29aa0230d8f29ab13b15f459b0eb439 Mon Sep 17 00:00:00 2001 From: Saurav Babu Date: Mon, 19 Mar 2018 19:36:45 +0530 Subject: [PATCH] comp-manager: Add initial cpp code This patch initializes IoTivity C++ Stack and creates group using IoTivity C++ functionaility Signed-off-by: Saurav Babu --- src/companion-manager/include/comp_iot.h | 2 +- .../src/{comp_iot.c => comp_iot.cpp} | 194 ++++++++++++++++++++- 2 files changed, 186 insertions(+), 10 deletions(-) rename src/companion-manager/src/{comp_iot.c => comp_iot.cpp} (78%) diff --git a/src/companion-manager/include/comp_iot.h b/src/companion-manager/include/comp_iot.h index d53eb8a..4868afd 100644 --- a/src/companion-manager/include/comp_iot.h +++ b/src/companion-manager/include/comp_iot.h @@ -32,7 +32,7 @@ typedef struct _comp_command_t { } comp_command_t; int comp_iot_initialize(); -int comp_iot_add_resource(comp_resource_type_e resource_type, char *uri); +int comp_iot_add_resource(comp_resource_type_e resource_type, const char *uri); int comp_iot_delete_resource(comp_resource_type_e resource_type); int comp_iot_discovery_resource(comp_resource_type_e resource_type, int timeout, void *user_data); int comp_iot_deinitialize(); diff --git a/src/companion-manager/src/comp_iot.c b/src/companion-manager/src/comp_iot.cpp similarity index 78% rename from src/companion-manager/src/comp_iot.c rename to src/companion-manager/src/comp_iot.cpp index f2717ca..8316365 100644 --- a/src/companion-manager/src/comp_iot.c +++ b/src/companion-manager/src/comp_iot.cpp @@ -7,15 +7,30 @@ #include #include #include +#include +#include #include #include #include +#include "OCProvisioningManager.hpp" +#include "OCPlatform.h" +#include "OCApi.h" + +using namespace OC; +using namespace std; + #define MAX_FILE_PATH_LEN 1024 #define SVR_DB_FILE_NAME "oic_svr_db_comp_manager.dat" +#define SYSTEM_INFO_PLATFORM_VERSION "http://tizen.org/feature/platform.version" +#define SYSTEM_INFO_MANUF_NAME "http://tizen.org/system/manufacturer" +#define SYSTEM_INFO_MODEL_NAME "http://tizen.org/system/model_name" +#define SYSTEM_INFO_BUILD_STRING "http://tizen.org/system/build.string" +#define SYSTEM_INFO_TIZEN_ID "http://tizen.org/system/tizenid" int last_get_result; +OCPersistentStorage ps; #define CASE_TO_STR(x) case x: return #x; @@ -48,22 +63,143 @@ static comp_request_type_e string2command(char *command) return COMP_REQ_UNKNOWN_COMMAND; } -int comp_iot_initialize() +static FILE* client_open(const char* /*path*/, const char *mode) { - char *device_id = NULL; char data_dir[MAX_FILE_PATH_LEN] = {0,}; snprintf(data_dir, MAX_FILE_PATH_LEN, "%s/network/%s", "/opt/usr/data", SVR_DB_FILE_NAME); + LOG_DEBUG("Open file %s", data_dir); + + return fopen(data_dir, mode); +} + +int __comp_iot_get_platform_info(OCPlatformInfo *platform_info) +{ + int ret; + char *tizen_id = NULL; + char *device_name = NULL; + char platform_id[1024]; + + device_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR); + if (device_name == NULL) + LOG_ERR("vconf_get_str() Fail"); + + ret = system_info_get_platform_string(SYSTEM_INFO_TIZEN_ID, &tizen_id); + if (SYSTEM_INFO_ERROR_NONE != ret) + LOG_ERR("system_info_get_platform_string() Fail(%d)", ret); + + snprintf(platform_id, sizeof(platform_id), "%s(%s)", + device_name ? device_name : "", tizen_id ? tizen_id : ""); + + free(device_name); + free(tizen_id); + LOG_DEBUG("platform_id: %s", platform_id); + + /* Mandatory (oic.wk.p) */ + platform_info->platformID = strdup(platform_id); + + /* Mandatory (oic.wk.p) */ + ret = system_info_get_platform_string(SYSTEM_INFO_MANUF_NAME, + &platform_info->manufacturerName); + if (SYSTEM_INFO_ERROR_NONE != ret) { + LOG_ERR("system_info_get_platform_string(manufacturer) Fail(%d)", ret); + free(platform_info->platformID); + return -1; + } + + ret = system_info_get_platform_string(SYSTEM_INFO_MODEL_NAME, + &platform_info->modelNumber); + if (SYSTEM_INFO_ERROR_NONE != ret) + LOG_ERR("system_info_get_platform_string(model_name) Fail(%d)", ret); + + ret = system_info_get_platform_string(SYSTEM_INFO_PLATFORM_VERSION, + &platform_info->platformVersion); + if (SYSTEM_INFO_ERROR_NONE != ret) + LOG_ERR("system_info_get_platform_string(platform_version) Fail(%d)", ret); + + ret = system_info_get_platform_string(SYSTEM_INFO_BUILD_STRING, + &platform_info->firmwareVersion); + if (SYSTEM_INFO_ERROR_NONE != ret) + LOG_ERR("system_info_get_platform_string(build_string) Fail(%d)", ret); + + /* platform_info.manufacturerUrl */ + /* platform_info.dateOfManufacture */ + /* platform_info.operatingSystemVersion */ + /* platform_info.hardwareVersion */ + /* platform_info.supportUrl */ + /* platform_info.systemTime */ + + return 0; +} + +int comp_iot_initialize() +{ + char *device_id = NULL; + int ret; + OCPlatformInfo platform_info = {0}; + + ps.open = client_open; + ps.read = fread; + ps.write = fwrite; + ps.close = fclose; + ps.unlink = unlink; + + PlatformConfig cfg { + OC::ServiceType::InProc, + OC::ModeType::Server, + (OCTransportAdapter)(OCTransportAdapter::OC_ADAPTER_IP|OCTransportAdapter::OC_ADAPTER_TCP), + OC::QualityOfService::LowQos, + &ps + }; + + OCPlatform::Configure(cfg); + + ret = __comp_iot_get_platform_info(&platform_info); + if (ret != 0) { + LOG_ERR("Failed to get platform info %d", ret); + return COMP_ERROR_NONE; + } + + free(platform_info.platformID); + free(platform_info.manufacturerName); + free(platform_info.modelNumber); + free(platform_info.platformVersion); + free(platform_info.firmwareVersion); + + ret = OCPlatform::registerPlatformInfo(platform_info); + + if (ret != OC_STACK_OK) + { + LOG_ERR("Platform Registration failed"); + return COMP_ERROR_NONE; + } + + OCDeviceInfo device_info = {0}; + device_info.deviceName = g_strdup("UNKNOWN"); + + ret = OCPlatform::registerDeviceInfo(device_info); + + if (ret != OC_STACK_OK) + { + LOG_ERR("Device Registration failed"); + return COMP_ERROR_NONE; + } + + g_free(device_info.deviceName); + +#if 0 int ret = iotcon_initialize(data_dir); if (IOTCON_ERROR_NONE != ret) { LOG_ERR("iotcon_initialize: Failed %s", get_error_message(ret)); return false; } LOG_DEBUG("iotcon_initialize : %s", get_error_message(ret)); +#endif OicUuid_t uuid; + ret = GetDoxmDeviceID(&uuid); if (OC_STACK_OK != ret) LOG_DEBUG("GetDoxmDevOwnerId failed = [%d][%s]", ret, get_error_message(ret)); @@ -78,7 +214,10 @@ int comp_iot_initialize() comp_ctx->device_uuid = g_strdup(device_id); /* Do Self-Ownership Transfer */ + ret = OCSecure::configSelfOwnership(); +#if 0 ret = OCConfigSelfOwnership(); +#endif if (OC_STACK_OK != ret ) { LOG_ERR( "OCConfigSelfOwnership() error = [%d][%s]", ret, get_error_message(ret)); } @@ -104,10 +243,18 @@ int comp_iot_initialize() return COMP_ERROR_NONE; } -static void _request_handler(iotcon_resource_h resource, iotcon_request_h request, - void *user_data) +OCEntityHandlerResult _request_handler(std::shared_ptr request) { LOG_DEBUG("_request_handler is called"); + + if (request) { + std::string requestType = request->getRequestType(); + int requestFlag = request->getRequestHandlerFlag(); + + LOG_DEBUG("request type %s flag %x", requestType.c_str(), requestFlag); + } + + return OC_EH_OK; //get resource element from comp resource list or, parsing resource uri //if request type is "get" and resource type is "group", @@ -116,6 +263,7 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques //then It is join request. CLIENT send device information to OWNER(Device info Exchange) //resource type "operation" don't have "get" request type. +#if 0 int ret; iotcon_request_type_e type; @@ -264,10 +412,37 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques iotcon_response_destroy(response); } +#endif } -int comp_iot_add_resource(comp_resource_type_e resource_type, char *uri) +int comp_iot_add_resource(comp_resource_type_e resource_type, const char *uri) { + //char uri_path[PATH_MAX] = {0,}; + OCResourceHandle resourceHandle; + std::string uri_path; + + uri_path = std::string(comp_resource_get_uri_prefix(resource_type)) + std::string(uri); + //strncpy(uri_path, comp_resource_get_uri_prefix(resource_type), PATH_MAX); + //strncat(uri_path, uri, PATH_MAX); + + LOG_DEBUG("[ADD] resource uri is %s", uri_path.c_str()); + + EntityHandler cb = std::bind(&_request_handler, std::placeholders::_1); + + OCStackResult result = OCPlatform::registerResource(resourceHandle, + uri_path, + std::string(comp_resource_get_type(resource_type)), + std::string(DEFAULT_INTERFACE), cb, + comp_resource_get_policies(resource_type)); + if (result != OC_STACK_OK) { + LOG_ERR("Failed to create resource"); + return COMP_ERROR_NONE; + } + + LOG_DEBUG("Successfully created resource"); + return COMP_ERROR_NONE; + +#if 0 iotcon_resource_h resource = NULL; iotcon_resource_types_h resource_types = NULL; iotcon_resource_interfaces_h resource_ifaces = NULL; @@ -325,11 +500,12 @@ EXIT: iotcon_resource_interfaces_destroy(resource_ifaces); return COMP_ERROR_NONE; +#endif } static bool _get_res_type_cb(const char *string, void *user_data) { - char **resource_type = user_data; + char **resource_type = (char **)user_data; *resource_type = g_strdup(string); @@ -342,7 +518,7 @@ int found_group_count = 0; static void _clear_user_data(void *user_data) { - comp_command_t *cmd = user_data; + comp_command_t *cmd = (comp_command_t *)user_data; if (NULL == cmd) return; @@ -509,7 +685,7 @@ static bool _found_resource(iotcon_remote_resource_h resource, if (IOTCON_ERROR_NONE != ret) { LOG_ERR("iotcon_attributes_create() Fail(%d)", ret); iotcon_representation_destroy(repr); - return; + return IOTCON_FUNC_CONTINUE; } LOG_DEBUG("CMD = %s", command2string(cmd->command)); @@ -603,7 +779,7 @@ int comp_iot_discovery_resource(comp_resource_type_e resource_type, int timeout, { int ret; iotcon_query_h query; - comp_command_t *cmd = user_data; + comp_command_t *cmd = (comp_command_t *) user_data; found_group_count = 0; -- 2.7.4