#include <mdgd_iot.h>
#include <mdgd_group.h>
-#include <mdgd_gdbus_group.h>
+#include <mdgd_gdbus.h>
#include <mdgd_mot_agent.h>
#include <mdgd_db.h>
using namespace OC;
using namespace std;
-#define MAX_FILE_PATH_LEN 1024
-#define SVR_DB_FILE_NAME "oic_svr_db_mdgd_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"
-#define SYSTEM_INFO_PROFILE "http://tizen.org/feature/profile"
-
-int last_get_result;
-OCPersistentStorage ps;
-
-static int ConvertUuidToStr(const OicUuid_t* uuid, char** strUuid)
-{
- if (NULL == uuid || NULL == strUuid || NULL != *strUuid) {
- LOG_ERR("ConvertUuidToStr : Invalid param");
- return OC_STACK_INVALID_PARAM;
- }
-
- size_t uuidIdx = 0;
- size_t urnIdx = 0;
- const size_t urnBufSize = (UUID_LENGTH * 2) + 4 + 1;
- char* convertedUrn = (char*)calloc(urnBufSize, sizeof(char));
- if (NULL == convertedUrn) {
- LOG_ERR("Memory Allocation Failed");
- goto exit;
- }
-
- for (uuidIdx = 0, urnIdx = 0; uuidIdx < UUID_LENGTH && urnIdx < urnBufSize;
- uuidIdx++, urnIdx += 2) {
- // canonical format for UUID has '8-4-4-4-12'
- if (uuidIdx == 4 || uuidIdx == 6 || uuidIdx == 8 || uuidIdx == 10) {
- snprintf(convertedUrn + urnIdx, 2, "%c", '-');
- urnIdx++;
- }
- snprintf(convertedUrn + urnIdx, 3, "%02x", (uint8_t)(uuid->id[uuidIdx]));
- }
- convertedUrn[urnBufSize - 1] = '\0';
-
- *strUuid = convertedUrn;
- return OC_STACK_OK;
-
-exit:
- return OC_STACK_NO_MEMORY;
-}
-
-#define CASE_TO_STR(x) case x: return #x;
-
-static const char *command2string(mdgd_request_type_e command)
-{
- switch (command) {
- CASE_TO_STR(MDGD_REQ_SEND_DATA)
- CASE_TO_STR(MDGD_REQ_CREATE_GROUP)
- CASE_TO_STR(MDGD_REQ_INVITE_DEVICE)
- CASE_TO_STR(MDGD_REQ_EJECT_DEVICE)
- CASE_TO_STR(MDGD_REQ_DELETE_GROUP)
- default:
- return "Unknown Command";
- }
-}
-
-static mdgd_request_type_e string2command(char *command)
-{
- if (g_strcmp0(command, "5") == 0)
- return MDGD_REQ_SEND_DATA;
- else if (g_strcmp0(command, "1") == 0)
- return MDGD_REQ_CREATE_GROUP;
- else if (g_strcmp0(command, "2") == 0)
- return MDGD_REQ_INVITE_DEVICE;
- else if (g_strcmp0(command, "3") == 0)
- return MDGD_REQ_EJECT_DEVICE;
- else if (g_strcmp0(command, "4") == 0)
- return MDGD_REQ_DELETE_GROUP;
- else
- return MDGD_REQ_UNKNOWN_COMMAND;
-}
-
-static FILE* client_open(const char* /*path*/, const char *mode)
+static FILE* __client_open(const char* /*path*/, const char *mode)
{
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 __mdgd_iot_get_platform_info(OCPlatformInfo *platform_info)
+static int __get_platform_info(OCPlatformInfo *platform_info)
{
int ret;
char *tizen_id = NULL;
char *device_name = NULL;
char platform_id[1024];
- struct utsname uts;
device_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
if (device_name == NULL)
free(device_name);
free(tizen_id);
- LOG_DEBUG("platform_id: %s", platform_id);
/* Mandatory (oic.wk.p) */
platform_info->platformID = strdup(platform_id);
LOG_ERR("system_info_get_platform_string(build_string) Fail(%d)", ret);
platform_info->manufacturerUrl = g_strdup("NONE");
- mdgd_check_null_print_error(platform_info->manufacturerUrl,
- "Memory allocation failed");
-
platform_info->dateOfManufacture = g_strdup("NONE");
- mdgd_check_null_print_error(platform_info->dateOfManufacture,
- "Memory allocation failed");
+ struct utsname uts;
if (!uname(&uts))
platform_info->operatingSystemVersion = g_strdup(uts.sysname);
else
platform_info->operatingSystemVersion = g_strdup("NONE");
- mdgd_check_null_print_error(platform_info->operatingSystemVersion,
- "Memory allocation failed");
platform_info->hardwareVersion = g_strdup("0-0");
- mdgd_check_null_print_error(platform_info->hardwareVersion,
- "Memory allocation failed");
-
platform_info->supportUrl = g_strdup("NONE");
- mdgd_check_null_print_error(platform_info->supportUrl,
- "Memory allocation failed");
-
platform_info->systemTime = g_strdup("NONE");
- mdgd_check_null_print_error(platform_info->systemTime,
- "Memroy allocation failed");
return 0;
}
-static void _clean_platform_info(OCPlatformInfo *platform_info)
+static void __clean_platform_info(OCPlatformInfo *platform_info)
{
mdgd_check_null_ret("platform_info", platform_info);
}
}
+OCPersistentStorage ps;
+
int mdgd_iot_initialize()
{
char *device_id = NULL;
int ret;
OCPlatformInfo platform_info = {0};
- ps.open = client_open;
+ mdgd_context_t *mdgd_ctx = mdgd_context_get_context();
+ mdgd_check_null_ret_error("mdgd_ctx", mdgd_ctx, MDGD_ERROR_INVALID_PARAMETER);
+
+ ps.open = __client_open;
ps.read = fread;
ps.write = fwrite;
ps.close = fclose;
OCPlatform::Configure(cfg);
- ret = __mdgd_iot_get_platform_info(&platform_info);
+ ret = __get_platform_info(&platform_info);
if (ret != 0) {
- _clean_platform_info(&platform_info);
LOG_ERR("Failed to get platform info %d", ret);
+ __clean_platform_info(&platform_info);
return MDGD_ERROR_NONE;
}
ret = OCPlatform::registerPlatformInfo(platform_info);
+ __clean_platform_info(&platform_info);
if (ret != OC_STACK_OK)
{
- _clean_platform_info(&platform_info);
LOG_ERR("Platform Registration failed");
return MDGD_ERROR_NONE;
}
- _clean_platform_info(&platform_info);
OCDeviceInfo device_info = {0};
device_info.deviceName = g_strdup("UNKNOWN");
ret = OCPlatform::registerDeviceInfo(device_info);
-
+ g_free(device_info.deviceName);
if (ret != OC_STACK_OK)
{
LOG_ERR("Device Registration failed");
return MDGD_ERROR_NONE;
}
- g_free(device_info.deviceName);
-
OicUuid_t uuid;
ret = GetDoxmDeviceID(&uuid);
if (OC_STACK_OK != ret)
LOG_DEBUG("GetDoxmDevOwnerId failed = [%d][%s]", ret, get_error_message(ret));
- ret = ConvertUuidToStr(&uuid, &device_id);
+ ret = mdgd_uuid_to_str(&uuid, &device_id);
if (OC_STACK_OK != ret)
- LOG_DEBUG("ConvertUuidToStr failed = [%d][%s]", ret, get_error_message(ret));
+ LOG_DEBUG("mdgd_uuid_to_str failed = [%d][%s]", ret, get_error_message(ret));
LOG_DEBUG("device_id : %s", device_id);
- mdgd_context_t *mdgd_ctx = mdgd_context_get_context();
- mdgd_check_null_ret_error("mdgd_ctx", mdgd_ctx, MDGD_ERROR_INVALID_PARAMETER);
-
mdgd_ctx->device_uuid = g_strdup(device_id);
/* Do Self-Ownership Transfer */
LOG_ERR( "OCConfigSelfOwnership() error = [%d][%s]", ret, get_error_message(ret));
}
- /*
- "Device" resource
- For get peer description.
- ex) /comp/device
- */
- mdgd_iot_add_resource(MDGD_RESOURCE_TYPE_DEVICE, "");
-
- /*
- "operation" resource
- The operation resource is a control command channel between daemon and daemon.
- resource type is core.comp.operation
- Within this function we create a default operation resource. (uri is /comp/operation/1)
- If we need more control channels, increase the number.
- ex) /comp/operation/2, /comp/operation/3
- */
- mdgd_iot_add_resource(MDGD_RESOURCE_TYPE_OPERATION, "1");
-
- /*
- "DATA" resource
- To show the send data to remote daemon.
- It can be used when the remote device is mot enable and pairwise.
- ex) /comp/data/1
- */
- mdgd_iot_add_resource(MDGD_RESOURCE_TYPE_DATA, "1");
-
- return MDGD_ERROR_NONE;
-}
-
-static void _print_oc_representation(OCRepresentation rep)
-{
- /* attribute.type() is an enum class AttributeType defined in
- * AttributeValue.h header file of IoTivity */
- for (auto& attribute : rep) {
- LOG_DEBUG("%s of type %d with value %s", attribute.attrname().c_str(),
- attribute.type(),
- attribute.getValueToString().c_str());
- }
-}
-
-OCEntityHandlerResult _request_handler(std::shared_ptr<OCResourceRequest> 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);
-
- if (requestType == "GET") {
- //Send Peer Description
- LOG_DEBUG("requestFlag : GET");
- auto pResponse = std::make_shared<OC::OCResourceResponse>();
- OCRepresentation rep;
-
- /* Set Peer Description */
-
- char *device_name = NULL;
- char *model_name = NULL;
- char *platform_ver = NULL;
- char *profile = NULL;
-
- system_settings_get_value_string(SYSTEM_SETTINGS_KEY_DEVICE_NAME, &device_name);
- system_info_get_platform_string(SYSTEM_INFO_MODEL_NAME, &model_name);
- system_info_get_platform_string(SYSTEM_INFO_PLATFORM_VERSION, &platform_ver);
- system_info_get_platform_string(SYSTEM_INFO_PROFILE, &profile);
- /* How to get the vendor id? */
-
- LOG_DEBUG("device name : %s", device_name);
- LOG_DEBUG("model name : %s", model_name);
- LOG_DEBUG("platform ver name : %s", platform_ver);
- LOG_DEBUG("profile : %s", profile);
-
- rep.setValue("model_name", std::string(model_name));
- rep.setValue("platform_ver", std::string(platform_ver));
- rep.setValue("profile", std::string(profile));
- rep.setValue("device_name", std::string(device_name));
- rep.setValue("vendor_id", std::string("6FFF"));
-
- pResponse->setRequestHandle(request->getRequestHandle());
- pResponse->setResourceHandle(request->getResourceHandle());
- pResponse->setErrorCode(200);
- pResponse->setResponseResult(OC_EH_OK);
- pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE);
- OCPlatform::sendResponse(pResponse);
- } else if (requestType == "POST") {
- if (requestFlag & RequestHandlerFlag::RequestFlag) {
- LOG_DEBUG("requestFlag : Request");
- auto pResponse = std::make_shared<OC::OCResourceResponse>();
- unsigned char *arg = NULL;
- int arg_len = 0;
- int result = MDGD_ERROR_NONE;
- int cmd;
- std::string requester_id, group_name, uuid, data;
-#ifdef SUPPORT_BASE64_ENCODING
- int payload_len;
- size_t outSize;
- uint8_t *out;
- uint32_t len = 0;
-#endif
- mdgd_context_t *mdgd_ctx;
-
- try {
- OCRepresentation rep = request->getResourceRepresentation();
-
- _print_oc_representation(rep);
-
- ObservationInfo observationInfo = request->getObservationInfo();
- LOG_DEBUG("Received request from %s:%d",
- observationInfo.address.c_str(), observationInfo.port);
+ mdgd_iot_add_resource(MDGD_RESOURCE_TYPE_GROUP, ""); //For Group
+ mdgd_iot_add_resource(MDGD_RESOURCE_TYPE_DEVICE, ""); //For Device Description
+ mdgd_iot_add_resource(MDGD_RESOURCE_TYPE_DATA, ""); //For Secure Data Transfer
- if (!rep.getValue("CMD", cmd))
- LOG_ERR("CMD not found in representation");
- else
- LOG_DEBUG("Command received %d", cmd);
-
- if (!rep.getValue("device_id", requester_id))
- LOG_ERR("device_id not found in representation");
- else
- LOG_DEBUG("device_id received %s", requester_id.c_str());
-
- switch(cmd) {
- case MDGD_REQ_SEND_DATA:
- LOG_DEBUG("Receive Data");
-#ifdef SUPPORT_BASE64_ENCODING
- if (!rep.getValue("data", data))
- LOG_ERR("data not found");
-
- /*
- * BASE64 encoding/decoding system use '=' as padding byte
- * But, query parameter does not allow use '=' character.Basically,
- * So, in order to use BASE64 as query parameters, we need additioinal length param
- * such as 'len=xx'
- */
- payload_len = strlen(data.c_str()); /* This data may be cliped the last padding 1~2 bytes ('='/'==') */
-
- LOG_DEBUG("data = %s payload_len = %d", data.c_str(),
- payload_len);
-
- outSize = B64DECODE_OUT_SAFESIZE(payload_len + 1);
- out = (uint8_t *)g_try_malloc0(outSize);
- if (NULL == out) {
- LOG_ERR("Can't allocate memory for base64 str");
- return OC_EH_OK;
- }
-
- if (B64_OK != b64Decode(data.c_str(), payload_len, out,
- outSize, &len)) {
- LOG_ERR("Base64 decoding failed.");
- g_free(out);
- return OC_EH_OK;
- }
-
- arg = (unsigned char *) g_try_malloc0(len);
- if (NULL == arg) {
- LOG_ERR("Memory allocation failed.");
- g_free(out);
- return OC_EH_OK;
- }
-
- memcpy(arg, out, len);
- arg_len = len;
-
- g_free(out);
-
- LOG_DEBUG("Successfully decoded from base64");
-#else
- if (!rep.getValue("data", data))
- LOG_ERR("data not found");
-
- LOG_DEBUG("Receive Data = %s", data.c_str());
- arg = (unsigned char *) g_strdup(data.c_str());
- arg_len = strlen(data.c_str());
-#endif
- break;
- case MDGD_REQ_CREATE_GROUP:
- LOG_DEBUG("Request create group");
-
- if (rep.getValue("name", group_name)) {
- LOG_DEBUG("group_name : %s", group_name.c_str());
- if (mdgd_check_group_exist(group_name.c_str()))
- LOG_DEBUG("Group already created");
- else {
- result = mdgd_group_create(group_name.c_str());
- if (result == 0)
- mdgd_db_group_insert(group_name.c_str());
- }
- arg = (unsigned char *) g_strdup(group_name.c_str());
- arg_len = strlen(group_name.c_str());
- }
- break;
- case MDGD_REQ_INVITE_DEVICE:
- LOG_DEBUG("Request invite device");
-
- if (!rep.getValue("name", group_name))
- LOG_ERR("Group name not found");
- if (!rep.getValue("id", uuid))
- LOG_ERR("UUID not found");
-
- LOG_DEBUG("group name : %s, UUID : %s", group_name.c_str(),
- uuid.c_str());
-
- result = mdgd_group_invite(group_name.c_str(),
- uuid.c_str(), "12341234");
- if (result == 0)
- mdgd_db_device_insert(uuid.c_str(), group_name.c_str());
- arg = (unsigned char *) g_strdup(uuid.c_str());
- arg_len = strlen(uuid.c_str());
- break;
- case MDGD_REQ_EJECT_DEVICE:
- LOG_DEBUG("Request eject device");
-
- mdgd_ctx = mdgd_context_get_context();
-
- if (!rep.getValue("name", group_name))
- LOG_ERR("Group name not found");
- if (!rep.getValue("id", uuid))
- LOG_ERR("UUID not found");
-
- LOG_DEBUG("group name : %s Self UUID : %s Target UUID : %s",
- group_name.c_str(),mdgd_ctx->device_uuid,
- uuid.c_str());
-
- result = mdgd_group_eject(group_name.c_str(), mdgd_ctx->device_uuid,
- uuid.c_str());
- arg = (unsigned char *) g_strdup(uuid.c_str());
- arg_len = strlen(uuid.c_str());
- break;
- case MDGD_REQ_DELETE_GROUP:
- LOG_DEBUG("Request delete group");
- arg = (unsigned char *) g_strdup("DELETED");
- arg_len = strlen("DELETED");
- break;
- case MDGD_REQ_KEEP_ALIVE:
- LOG_DEBUG("Keep Alive message");
-
- if (rep.getValue("group_name", group_name)) {
- LOG_DEBUG("group_name : %s", group_name.c_str());
- }
- break;
- default:
- LOG_ERR("Unknown request command %d", cmd);
- break;
- }
+ char data_dir[MAX_FILE_PATH_LEN] = {0,};
- if (cmd != MDGD_REQ_KEEP_ALIVE) {
- const char *cmd_str;
- cmd_str = command2string((mdgd_request_type_e) cmd);
- notify_request_result(cmd_str, requester_id.c_str(), arg,
- arg_len, result);
- }
+ snprintf(data_dir, MAX_FILE_PATH_LEN, "%s/network/%s",
+ "/opt/usr/data", SVR_DB_FILE_NAME);
- pResponse->setRequestHandle(request->getRequestHandle());
- pResponse->setResourceHandle(request->getResourceHandle());
- pResponse->setErrorCode(200);
- pResponse->setResponseResult(OC_EH_OK);
- OCPlatform::sendResponse(pResponse);
- } catch (std::exception& e) {
- LOG_ERR("Exceptioin occured %s", e.what());
- }
+ ret = OCInitPM(data_dir);
- g_free(arg);
- }
+ OCProvisionDev_t* motdev_list = NULL;
+ OCDiscoverMultipleOwnerEnabledDevices(1, &motdev_list);
+ OCProvisionDev_t *iter = NULL;
+ iter = motdev_list;
+ while (iter != NULL) {
+ char *device_id = NULL;
+ OicUuid_t *uuid = &iter->doxm->deviceID;
+ mdgd_uuid_to_str(uuid, &device_id);
- } else {
- LOG_ERR("Invalid Request Type");
- return OC_EH_OK;
+ if (g_strcmp0(mdgd_ctx->device_uuid, device_id) != 0) {
+ iter = iter->next;
+ continue;
}
- }
- return OC_EH_OK;
-}
-
-int mdgd_iot_add_resource(mdgd_resource_type_e resource_type, const char *uri)
-{
- OCResourceHandle resourceHandle;
- std::string uri_path;
-
- uri_path = std::string(mdgd_resource_get_uri_prefix(resource_type)) +
- std::string(uri);
-
- LOG_DEBUG("[ADD] resource uri is %s", uri_path.c_str());
-
- OCStackResult result = OCPlatform::registerResource(resourceHandle,
- uri_path,
- std::string(mdgd_resource_get_type(resource_type)),
- std::string(DEFAULT_INTERFACE), &_request_handler,
- mdgd_resource_get_policies(resource_type));
- if (result != OC_STACK_OK) {
- LOG_ERR("Failed to create resource");
- return MDGD_ERROR_NONE;
+ mdgd_device_t *device = (mdgd_device_t *)g_try_malloc0(sizeof(mdgd_device_t));
+ char *device_name = NULL;
+ char *model_name = NULL;
+ char *platform_ver = NULL;
+ char *profile = NULL;
+ char *vendor_id = NULL;
+
+ system_settings_get_value_string(SYSTEM_SETTINGS_KEY_DEVICE_NAME, &device_name);
+ system_info_get_platform_string(SYSTEM_INFO_MODEL_NAME, &model_name);
+ system_info_get_platform_string(SYSTEM_INFO_PLATFORM_VERSION, &platform_ver);
+ system_info_get_platform_string(SYSTEM_INFO_PROFILE, &profile);
+
+ device->device_id = device_id;
+ device->port = iter->endpoint.port;
+ device->addr = g_strdup(iter->endpoint.addr);
+ device->conn_type = iter->connType;
+ device->secure_port = iter->securePort;
+ device->device_name = g_strdup(device_name);
+ device->model_name = g_strdup(model_name);
+ device->platform_ver = g_strdup(platform_ver);
+ device->profile = g_strdup(profile);
+ device->vendor_id = g_strdup("6FFF");
+ device->is_invited = true;
+ device->type = MDGD_DEVICE_TYPE_LOCAL;
+
+ mdgd_ctx->device = device;
+ LOG_DEBUG("local device stored");
+ iter = iter->next;
}
- LOG_DEBUG("Successfully created resource");
-
- //Append to resource list
- mdgd_resource_append(resource_type, uri_path.c_str(), resourceHandle);
-
return MDGD_ERROR_NONE;
}
-int found_group_count = 0;
-
-static void _clear_user_data(void *user_data)
-{
- mdgd_command_t *cmd = (mdgd_command_t *)user_data;
-
- if (NULL == cmd)
- return;
-
- if (cmd->tid) {
- g_source_remove(cmd->tid);
- }
-
- if (cmd->uuid) {
- g_free(cmd->uuid);
- cmd->uuid = NULL;
- }
- if (cmd->host) {
- g_free(cmd->host);
- cmd->host = NULL;
- }
- if (cmd->arg1) {
- g_free(cmd->arg1);
- cmd->arg1 = NULL;
- }
- if (cmd->arg2) {
- g_free(cmd->arg2);
- cmd->arg2 = NULL;
- }
- if (cmd->arg3) {
- g_free(cmd->arg3);
- cmd->arg3 = NULL;
- }
- if (cmd->arg4) {
- g_free(cmd->arg4);
- cmd->arg4 = NULL;
- }
-
-#ifdef SUPPORT_BASE64_ENCODING
- if (cmd->data) {
- g_free(cmd->data);
- cmd->data = NULL;
- }
-#endif
-
- g_free(cmd);
- cmd = NULL;
-}
-
-static void _on_post(const HeaderOptions& /*headerOptions*/,
- const OCRepresentation& rep, const int eCode,
- void *user_data)
-{
- mdgd_command_t *cmd = (mdgd_command_t *)user_data;
-
- try {
- if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED ||
- eCode == OC_STACK_RESOURCE_CHANGED) {
- last_get_result = 0;
- LOG_DEBUG("Post request is successful");
-
- if (cmd->command == MDGD_REQ_KEEP_ALIVE) {
- mdgd_group_notify_keepalive_response(cmd);
- return;
- }
- } else {
- last_get_result = eCode;
- LOG_ERR("_on_post Response error %d", eCode);
- }
- } catch(std::exception& e) {
- LOG_ERR("Exception %s in on post", e.what());
- }
-
- notify_send_data_finish("RESP_DATA", last_get_result);
-
- //_clear_user_data(cmd);
-}
-
-static void _on_get(const HeaderOptions& /*headerOptions*/,
- const OCRepresentation& rep, const int eCode,
- void *user_data)
-{
- LOG_DEBUG("On get function.");
-
- try {
- if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED ||
- eCode == OC_STACK_RESOURCE_CHANGED) {
- mdgd_mot_device_t *device = (mdgd_mot_device_t *)user_data;
- mdgd_check_null_ret("device", device);
-
- std::string modelName;
- std::string platformVer;
- std::string Profile;
- std::string deviceName;
- std::string vendorID;
-
- rep.getValue("model_name", modelName);
- rep.getValue("platform_ver", platformVer);
- rep.getValue("profile", Profile);
- rep.getValue("device_name", deviceName);
- rep.getValue("vendor_id", vendorID);
-
- mdgd_check_null_ret("modelName", modelName.c_str());
- mdgd_check_null_ret("platformVer", platformVer.c_str());
- mdgd_check_null_ret("Profile", Profile.c_str());
- mdgd_check_null_ret("deviceName", deviceName.c_str());
- mdgd_check_null_ret("vendorID", vendorID.c_str());
-
- device->model_name = g_strdup(modelName.c_str());
- device->platform_ver= g_strdup(platformVer.c_str());
- device->profile = g_strdup(Profile.c_str());
- device->device_name = g_strdup(deviceName.c_str());
- device->vendor_id = g_strdup(vendorID.c_str());
-
- if (device->mowned == false)
- mdgd_group_add_new_mot_device(device);
- else
- mdgd_group_add_new_mowned_device(device);
- } else {
- LOG_ERR("_on_get Response error %d", eCode);
- }
- } catch(std::exception& e) {
- LOG_ERR("Exception %s in on get", e.what());
- }
-}
-
-static int _perform_mot_me(gpointer data)
-{
- int ret;
- mdgd_context_t *mdgd_ctx = mdgd_context_get_context();
- mdgd_check_null_ret_error("mdgd_ctx", mdgd_ctx, FALSE);
-
- ret = agent_mot(mdgd_ctx->device_uuid, (char *)"12341234");
- if (ret != MDGD_ERROR_NONE)
- LOG_ERR("agent_mot(%s) Failed", mdgd_ctx->device_uuid);
-
- return FALSE;
-}
-
-static bool _found_resource(std::shared_ptr<OCResource> resource,
- void *user_data)
-{
- char *resource_uri_path;
- char *resource_device_id = NULL;
- char *resource_host;
- char *resource_type = NULL;
- mdgd_group_type_e group_type;
- std::string resourceURI;
- std::string hostAddress;
-
- LOG_DEBUG("Found Resource");
- try {
- if (resource) {
- resource_host = g_strdup(resource->host().c_str());
-
- resource_uri_path = g_strdup(resource->uri().c_str());
- resource_device_id = g_strdup(resource->sid().c_str());
-
- LOG_DEBUG("Resource URI : %s", resource_uri_path);
- LOG_DEBUG("Host Address : %s", resource_host);
-
- LOG_DEBUG("Resource Device ID : %s", resource_device_id);
-
- for(auto &resourceTypes : resource->getResourceTypes()) {
- resource_type = g_strdup(resourceTypes.c_str());
- LOG_DEBUG("Resource Type : %s", resource_type);
- }
-
- if (g_strcmp0(resource_type, "core.comp.group") == 0) {
- mdgd_context_t *mdgd_ctx = mdgd_context_get_context();
- if (g_strcmp0(resource_device_id, mdgd_ctx->device_uuid) == 0)
- group_type = MDGD_GROUP_GROUP_LOCAL;
- else
- group_type = MDGD_GROUP_GROUP_REMOTE;
-
- char *temp = g_strdup(resource_uri_path);
- if (NULL == temp) {
- LOG_ERR("Memory allocation failed");
- return TRUE;
- }
- char *ptr = strtok(temp, "/");
- ptr = strtok(NULL, "/");
- ptr = strtok(NULL, "/");
-
- LOG_DEBUG("group name is %s", ptr);
- mdgd_group_add_new(resource_uri_path, resource_device_id, ptr,
- resource_host, resource_type, group_type);
- found_group_count++;
-
- g_free(temp);
-
- } else if (g_strcmp0(resource_type, "core.comp.device") == 0) {
- mdgd_command_t *cmd = (mdgd_command_t *)user_data;
- GVariant *parameters = (GVariant *)cmd->user_data;
- GVariant *device_info;
- bool mowned;
- GVariantIter *iter = NULL;
- mdgd_mot_device_t *device = NULL;
-
- if (NULL == parameters) {
- LOG_ERR("No Multiple Owned devices found");
- return TRUE;
- }
-
- if (!strncmp(cmd->arg1, "disc_mot_enb_devs_done", strlen(cmd->arg1)))
- mowned = false;
- else
- mowned = true;
-
- g_variant_get(parameters, "(aa{sv})", &iter);
- while ((device_info = g_variant_iter_next_value(iter))) {
- GVariant *val = g_variant_lookup_value(device_info, "deviceId", G_VARIANT_TYPE_STRING);
- gsize len = 0;
- const char *device_id = g_variant_get_string(val, &len);
- if (g_strcmp0(resource_device_id, device_id) == 0) {
- device = (mdgd_mot_device_t *)g_try_malloc0(sizeof(mdgd_mot_device_t));
- if (NULL == device) {
- LOG_ERR("Memory allocation failed");
- continue;
- }
-
- LOG_DEBUG("deviceId = %s", device_id);
- device->device_id = g_strdup(device_id);
-
- val = g_variant_lookup_value(device_info, "adapter", G_VARIANT_TYPE_INT32);
- int adapter = g_variant_get_int32(val);
- device->adapter = adapter;
-
- val = g_variant_lookup_value(device_info, "flags", G_VARIANT_TYPE_INT32);
- int flags = g_variant_get_int32(val);
- LOG_DEBUG("flags = %d", flags);
- device->flags = flags;
-
- val = g_variant_lookup_value(device_info, "port", G_VARIANT_TYPE_UINT16);
- int port = g_variant_get_uint16(val);
- LOG_DEBUG("port = %d", port);
- device->port = port;
-
- val = g_variant_lookup_value(device_info, "addr", G_VARIANT_TYPE_STRING);
- const char *addr = g_variant_get_string(val, &len);
- LOG_DEBUG("addr = %s", addr);
- device->addr = g_strdup(addr);
-
- val = g_variant_lookup_value(device_info, "ifindex", G_VARIANT_TYPE_INT32);
- int ifindex = g_variant_get_int32(val);
- LOG_DEBUG("ifindex = %d", ifindex);
- device->ifindex = ifindex;
-
- val = g_variant_lookup_value(device_info, "routeData", G_VARIANT_TYPE_STRING);
- const char *routeData = g_variant_get_string(val, &len);
- LOG_DEBUG("routeData = %s", routeData);
- device->route_data = g_strdup(routeData);
-
- val = g_variant_lookup_value(device_info, "remoteId", G_VARIANT_TYPE_STRING);
- const char *remoteId = g_variant_get_string(val, &len);
- LOG_DEBUG("remoteId = %s", remoteId);
- device->remote_id = g_strdup(remoteId);
-
- val = g_variant_lookup_value(device_info, "connType", G_VARIANT_TYPE_UINT32);
- int connType = g_variant_get_uint32(val);
- LOG_DEBUG("connType = %d", connType);
- device->conn_type = connType;
-
- val = g_variant_lookup_value(device_info, "securePort", G_VARIANT_TYPE_UINT16);
- int securePort = g_variant_get_uint16(val);
- LOG_DEBUG("securePort = %d", securePort);
- device->secure_port = securePort;
-#ifdef WITH_TCP
- val = g_variant_lookup_value(device_info, "tcpPort", G_VARIANT_TYPE_UINT16);
- int tcpPort = g_variant_get_uint16(val);
- LOG_DEBUG("tcpPort = %d", tcpPort);
- device->tcp_port = tcpPort;
-#endif
- val = g_variant_lookup_value(device_info, "secVer", G_VARIANT_TYPE_STRING);
- const char *secVer = g_variant_get_string(val, &len);
- LOG_DEBUG("secVer = %s", secVer);
- device->sec_ver = g_strdup(secVer);
-
- val = g_variant_lookup_value(device_info, "devStatus", G_VARIANT_TYPE_UINT32);
- int devStatus = g_variant_get_uint32(val);
- LOG_DEBUG("devStatus = %d", devStatus);
- device->dev_status = devStatus;
-
- device->mowned = mowned;
- }
- }
-
- mdgd_check_null_ret_error("device", device, TRUE);
-
- if (strncmp(cmd->uuid, resource_device_id, strlen(cmd->uuid))) {
- LOG_DEBUG("Get peer description from another device");
- /* Get Peer Description using "Get" operation */
- device->type = MDGD_GROUP_DEVICE_REMOTE;
- resource->get(QueryParamsMap(), std::bind(&_on_get, std::placeholders::_1,
- std::placeholders::_2, std::placeholders::_3, device));
- } else {
- LOG_DEBUG("It's me. Get peer description myself");
- device->type = MDGD_GROUP_DEVICE_LOCAL;
-
- mdgd_context_t *mdgd_ctx = mdgd_context_get_context();
- if (mdgd_ctx && (FALSE == mdgd_ctx->mot_me)) {
- g_timeout_add(10, _perform_mot_me, NULL);
- mdgd_ctx->mot_me = TRUE;
- }
-
- char *device_name = NULL;
- char *model_name = NULL;
- char *platform_ver = NULL;
- char *profile = NULL;
-
- system_settings_get_value_string(SYSTEM_SETTINGS_KEY_DEVICE_NAME, &device_name);
- system_info_get_platform_string(SYSTEM_INFO_MODEL_NAME, &model_name);
- system_info_get_platform_string(SYSTEM_INFO_PLATFORM_VERSION, &platform_ver);
- system_info_get_platform_string(SYSTEM_INFO_PROFILE, &profile);
- /* How to get the vendor id? */
-
- device->model_name = g_strdup(device_name);
- device->platform_ver= g_strdup(model_name);
- device->profile = g_strdup(platform_ver);
- device->device_name = g_strdup(profile);
- device->vendor_id = g_strdup("6FFF");
-
- mdgd_group_change_mowned_device(device);
- }
- } else if (g_strcmp0(resource_type, "core.comp.data") == 0) {
- mdgd_command_t *cmd = (mdgd_command_t *)user_data;
-
- LOG_DEBUG("Resource Type %s", resource_type);
- LOG_DEBUG("Command Device ID: %s", cmd ? cmd->uuid : "NULL");
-
- if (cmd != NULL && !strncmp(cmd->uuid, resource_device_id, strlen(cmd->uuid))) {
- OCRepresentation rep;
-
- LOG_DEBUG("Request to Remote Device");
-
- LOG_DEBUG("CMD = %s", command2string(cmd->command));
-
- rep.setValue("CMD", cmd->command);
-
- mdgd_context_t *mdgd_ctx = mdgd_context_get_context();
- rep.setValue("device_id", std::string(mdgd_ctx->device_uuid));
-
- if (cmd->command == MDGD_REQ_CREATE_GROUP) { //request create group
- rep.setValue("name", std::string(cmd->arg1));
- } else if (cmd->command == MDGD_REQ_INVITE_DEVICE) { //request invite
- rep.setValue("name", std::string(cmd->arg1));
- rep.setValue("id", std::string(cmd->arg2));
- rep.setValue("PIN", std::string(cmd->arg3));
- } else if (cmd->command == MDGD_REQ_EJECT_DEVICE) { //request eject
- rep.setValue("name", std::string(cmd->arg1));
- rep.setValue("id", std::string(cmd->arg2));
- } else if (cmd->command == MDGD_REQ_DELETE_GROUP) { //request delete group
- rep.setValue("name", std::string(cmd->arg1));
- } else { /* Send Data */
-#ifdef SUPPORT_BASE64_ENCODING
- uint32_t outSize = 0;
- size_t b64BufSize = B64ENCODE_OUT_SAFESIZE(
- (cmd->data_len + 1));
- char *b64Buf = (char *) g_malloc0(b64BufSize+1);
- if (NULL == b64Buf) {
- _clear_user_data(cmd);
- return TRUE;
- }
- b64Encode(cmd->data, cmd->data_len,
- b64Buf, b64BufSize, &outSize);
- b64Buf[b64BufSize] = '\0';
- rep.setValue("data", std::string(b64Buf));
- LOG_DEBUG("b64BufSize =%d outSize = %d b64Buf = %s",
- b64BufSize, outSize, b64Buf);
- g_free(b64Buf);
-#else
- rep.setValue("data", std::string(cmd->arg1));
-#endif
- }
- resource->post(rep, QueryParamsMap(),
- std::bind (&_on_post, std::placeholders::_1,
- std::placeholders::_2, std::placeholders::_3, cmd));
- }
- }
-
- g_free(resource_uri_path);
- g_free(resource_host);
- g_free(resource_type);
- g_free(resource_device_id);
- }
- else {
- LOG_ERR("Resource is invalid");
- }
- }
- catch(std::exception& e)
- {
- LOG_ERR("Exception in foundResource: %s", e.what());
- }
-
- return TRUE;
-}
-
-void __invite_device(gpointer data, gpointer user_data)
-{
- int sql_ret = SQLITE_OK;
- char *sql = NULL;
- sqlite3_stmt *stmt = NULL;
-
- mdgd_context_t *mdgd_ctx = mdgd_context_get_context();
-
- mdgd_grp_t *grp = (mdgd_grp_t *)data;
- LOG_DEBUG("Group name : %s", grp->name);
-
- sql = sqlite3_mprintf("SELECT device_id FROM %s WHERE group_name=%Q;",
- MDGD_DB_DEVICE_TABLE, grp->name);
-
- if (sql != NULL) {
- sql_ret = sqlite3_prepare_v2(mdgd_ctx->db, sql, strlen(sql), &stmt, NULL);
- if (sql_ret != SQLITE_OK) {
- LOG_ERR("Error: %s", sqlite3_errmsg(mdgd_ctx->db));
- sqlite3_free(sql);
- return;
- }
-
- do {
- char *device_id = NULL;
- sql_ret = sqlite3_step(stmt);
- if (sql_ret != SQLITE_ROW) {
- break;
- }
-
- device_id = g_strdup((const char *)sqlite3_column_text(stmt, 0));
- if (NULL == device_id ) {
- LOG_ERR("Memory allocation failed");
- continue;
- }
- mdgd_mot_device_t *device = mdgd_group_check_device_in_mot_enb_dev_list(device_id);
-
- if (device != NULL)
- mdgd_group_add_device_in_group(grp->name, device_id);
- } while (sql_ret != SQLITE_DONE);
-
- sqlite3_finalize(stmt);
- sqlite3_free(sql);
- }
-}
-
-static gboolean _timeout_cb(gpointer data)
-{
- int ret = MDGD_ERROR_NONE;
- mdgd_command_t *cmd = (mdgd_command_t *)data;
-
- if (cmd->resource_type == MDGD_RESOURCE_TYPE_GROUP) {
- notify_group_find_finish(ret);
- } else if (cmd->resource_type == MDGD_RESOURCE_TYPE_DATA) {
- if (cmd->command != MDGD_REQ_KEEP_ALIVE)
- notify_send_data_finish("RESP_DATA", last_get_result);
- } else if (cmd->resource_type == MDGD_RESOURCE_TYPE_DEVICE) {
- if (!strncmp(cmd->arg1, "disc_mot_enb_devs_done", strlen(cmd->arg1))) {
- LOG_DEBUG("Device Find Done. notified");
- if (mdgd_group_get_mot_device_count() > 0)
- ret = 0;
-
- mdgd_context_t *mdgd_ctx = mdgd_context_get_context();
- if (mdgd_ctx->first_search == true) {
- if (ret == 0)
- g_list_foreach(mdgd_ctx->grp_list, __invite_device, NULL);
- mdgd_ctx->first_search = false;
- } else {
- mdgd_group_notify_mot_enable_device_done();
- notify_device_find_finish(ret);
- }
- } else {
- LOG_DEBUG("MOT Device Find Done. notified");
- mdgd_group_notify_mowned_device_find_done();
- notify_mowned_device_find_finish(ret);
- }
- }
-
- _clear_user_data(data);
-
- return false;
-}
-
-int mdgd_iot_discovery_resource(mdgd_resource_type_e resource_type, int timeout,
- void *user_data)
-{
- int ret;
- std::ostringstream requestURI;
- mdgd_command_t *cmd = (mdgd_command_t *) user_data;
- mdgd_check_null_ret_error("cmd", cmd, MDGD_ERROR_INVALID_PARAMETER);
-
- found_group_count = 0;
-
- try {
- requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=" <<
- mdgd_resource_get_type(resource_type);
-
- auto found_cb = std::bind (&_found_resource, std::placeholders::_1,
- cmd);
- if (cmd) {
- LOG_DEBUG("Querying for Resource host address %s",
- cmd->host ? cmd->host : "NULL");
-
- if (cmd->host)
- ret = OCPlatform::findResource(cmd->host, requestURI.str(),
- CT_ADAPTER_IP,
- found_cb);
- else
- ret = OCPlatform::findResource("", requestURI.str(),
- CT_ADAPTER_IP,
- found_cb);
- }
- } catch (OCException &e) {
- LOG_ERR("Failed to find resource %s", e.what());
- ret = e.code();
- }
-
- cmd->resource_type = resource_type;
- cmd->tid = g_timeout_add_seconds(timeout + 1, _timeout_cb, cmd);
-
- LOG_DEBUG("find resource : %s", get_error_message(ret));
-
- return ret;
-}
-
-int mdgd_iot_send_data(mdgd_resource_type_e resource_type, int timeout,
- mdgd_command_t *cmd)
-{
- int ret;
- OCResource::Ptr resource;
- std::string requestURI;
- std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
- std::vector<std::string> resource_types;
- resource_types = {mdgd_resource_get_type(resource_type)};
-
- try {
- requestURI = std::string(mdgd_resource_get_uri_prefix(resource_type)) +
- "1";
-
- LOG_DEBUG("Request URI %s", requestURI.c_str());
-
- resource = OCPlatform::constructResourceObject(std::string(cmd->host),
- requestURI, CT_ADAPTER_IP, false,
- resource_types, ifaces);
- if (!resource) {
- LOG_ERR("Failed to construct resource");
- return -1;
- }
-
- OCRepresentation rep;
- rep.setValue("CMD", cmd->command);
-
- mdgd_context_t *mdgd_ctx = mdgd_context_get_context();
- rep.setValue("device_id", std::string(mdgd_ctx->device_uuid));
-
-#ifdef SUPPORT_BASE64_ENCODING
- uint32_t outSize = 0;
- size_t b64BufSize;
- char *b64Buf;
-#endif
- switch (cmd->command) {
- case MDGD_REQ_SEND_DATA:
-#ifdef SUPPORT_BASE64_ENCODING
- b64BufSize = B64ENCODE_OUT_SAFESIZE((cmd->data_len + 1));
- b64Buf = (char *) g_try_malloc0(b64BufSize + 1);
- if (NULL == b64Buf) {
- _clear_user_data(cmd);
- return TRUE;
- }
- b64Encode(cmd->data, cmd->data_len,
- b64Buf, b64BufSize, &outSize);
- rep.setValue("data", std::string(b64Buf));
- LOG_DEBUG("b64BufSize =%d outSize = %d b64Buf = %s",
- b64BufSize, outSize, b64Buf);
- g_free(b64Buf);
-#else
- rep.setValue("data", std::string(cmd->arg1));
-#endif
- break;
- case MDGD_REQ_CREATE_GROUP:
- case MDGD_REQ_DELETE_GROUP:
- rep.setValue("name", std::string(cmd->arg1));
- break;
- case MDGD_REQ_INVITE_DEVICE:
- rep.setValue("name", std::string(cmd->arg1));
- rep.setValue("id", std::string(cmd->arg2));
- rep.setValue("PIN", std::string(cmd->arg3));
- break;
- case MDGD_REQ_EJECT_DEVICE:
- rep.setValue("name", std::string(cmd->arg1));
- rep.setValue("id", std::string(cmd->arg2));
- break;
- case MDGD_REQ_KEEP_ALIVE:
- rep.setValue("group_name", std::string(cmd->arg1));
- break;
- default:
- LOG_ERR("Invalid command %d", cmd->command);
- return -1;
- }
-
- ret = resource->post(rep, QueryParamsMap(),
- std::bind (&_on_post, std::placeholders::_1,
- std::placeholders::_2, std::placeholders::_3, cmd));
-
- } catch (OCException &e) {
- LOG_ERR("Failed to send data %s", e.what());
- ret = e.code();
- }
-
- cmd->resource_type = resource_type;
- cmd->tid = g_timeout_add_seconds(timeout + 1, _timeout_cb, cmd);
-
- LOG_DEBUG("Send Data : %s", get_error_message(ret));
-
- return ret;
-}
-
-int mdgd_iot_delete_resource(mdgd_resource_type_e resource_type,
- const char *uri)
+int mdgd_iot_deinitialize()
{
- OCResourceHandle resourceHandle;
- std::string uri_path;
-
- //delete resource from resource list using resource_type and index
-
- uri_path = std::string(mdgd_resource_get_uri_prefix(resource_type)) +
- std::string(uri);
+ LOG_BEGIN();
- LOG_DEBUG("[Delete] resource uri is %s", uri_path.c_str());
+ //iot deinitiatlize
- resourceHandle = mdgd_resource_get_handle(resource_type, uri_path.c_str());
+ LOG_END();
- OCStackResult result = OCPlatform::unregisterResource(resourceHandle);
- if (OC_STACK_OK != result) {
- LOG_ERR("Failed to unregister resource");
- return MDGD_ERROR_OPERATION_FAILED;
- }
-
- //delete DB
- return MDGD_ERROR_NONE;
-}
-
-int mdgd_iot_deinitialize()
-{
return MDGD_ERROR_NONE;
}