PlatformConfig cfg {
OC::ServiceType::InProc,
OC::ModeType::Both,
- (OCTransportAdapter)(OCTransportAdapter::OC_ADAPTER_IP|OCTransportAdapter::OC_ADAPTER_TCP),
+ (OCTransportAdapter)(OCTransportAdapter::OC_ADAPTER_IP),
OC::QualityOfService::LowQos,
&ps
};
return COMP_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");
int requestFlag = request->getRequestHandlerFlag();
LOG_DEBUG("request type %s flag %x", requestType.c_str(), requestFlag);
+
+ if (requestType != "POST") {
+ LOG_ERR("Invalid Request Type");
+ return OC_EH_OK;
+ }
+
+ if(requestFlag & RequestHandlerFlag::RequestFlag) {
+ LOG_DEBUG("requestFlag : Request");
+ auto pResponse = std::make_shared<OC::OCResourceResponse>();
+ char *arg = NULL;
+ int result;
+ int cmd;
+ std::string group_name, uuid, data;
+ comp_context_t *comp_ctx;
+
+ try {
+ OCRepresentation rep = request->getResourceRepresentation();
+
+ _print_oc_representation(rep);
+
+ if (!rep.getValue("CMD", cmd)) {
+ LOG_ERR("CMD not found in representation");
+ } else {
+ LOG_DEBUG("Command received %d", cmd);
+ }
+ switch(cmd) {
+ case COMP_REQ_SEND_DATA:
+#ifdef SUPPORT_BASE64_ENCODING
+ int payload_len;
+
+ LOG_DEBUG("Receive Data");
+
+ 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);
+
+ size_t outSize = B64DECODE_OUT_SAFESIZE(payload_len + 1);
+ uint8_t* out = g_malloc0(outSize);
+ if (NULL == out) {
+ LOG_ERR("Can't allocate memory for base64 str");
+ return;
+ }
+ uint32_t len = 0;
+
+ if(B64_OK == b64Decode(data.c_str(), payload_len, out,
+ outSize, &len)) {
+ LOG_ERR("Base64 decoding failed.");
+ return;
+ }
+ memcpy(arg, out, len);
+
+ LOG_DEBUG("successfully decoded to base64. %s", arg);
+
+ iotcon_query_remove(query, "data");
+#else
+ if (!rep.getValue("data", data))
+ LOG_ERR("data not found");
+
+ LOG_DEBUG("Receive Data = %s", data.c_str());
+ arg = g_strdup(data.c_str());
+#endif
+ break;
+ case COMP_REQ_CREATE_GROUP:
+ LOG_DEBUG("Request create group");
+
+ if (rep.getValue("name", group_name)) {
+ LOG_DEBUG("group_name : %s", group_name.c_str());
+ result = comp_group_create(group_name.c_str());
+ arg = g_strdup(group_name.c_str());
+ }
+ break;
+ case COMP_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 = comp_group_invite(group_name.c_str(),
+ uuid.c_str(), "12341234");
+ arg = g_strdup(uuid.c_str());
+ break;
+ case COMP_REQ_EJECT_DEVICE:
+ LOG_DEBUG("Request eject device");
+
+ comp_ctx = comp_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(),comp_ctx->device_uuid,
+ uuid.c_str());
+
+ result = comp_group_dismiss(comp_ctx->device_uuid,
+ uuid.c_str());
+ arg = g_strdup(uuid.c_str());
+ break;
+ case COMP_REQ_DELETE_GROUP:
+ LOG_DEBUG("Request delete group");
+ arg = g_strdup("DELETED");
+ break;
+ default:
+ LOG_ERR("Unknown request command %d", cmd);
+ break;
+ }
+
+ notify_request_result(command2string((comp_request_type_e) cmd),
+ arg, result);
+
+ g_free(arg);
+
+ pResponse->setErrorCode(200);
+ pResponse->setResponseResult(OC_EH_OK);
+ OCPlatform::sendResponse(pResponse);
+ } catch (std::exception& e) {
+ LOG_ERR("Exceptioin occured %s", e.what());
+ }
+ }
}
return OC_EH_OK;
if(resource) {
resource_host = g_strdup(resource->host().c_str());
- if (g_strstr_len(resource_host, strlen(resource_host), "tcp")) {
- LOG_DEBUG("Ignore Resource found for TCP host");
- g_free(resource_host);
- return IOTCON_FUNC_CONTINUE;
- }
-
resource_uri_path = g_strdup(resource->uri().c_str());
resource_device_id = g_strdup(resource->sid().c_str());
LOG_DEBUG("CMD = %s", command2string(cmd->command));
if (cmd->command == COMP_REQ_CREATE_GROUP) { //request create group
- rep.setValue("CMD", "1");
- rep.setValue("name", cmd->arg1);
+ rep.setValue("CMD", COMP_REQ_CREATE_GROUP);
+ rep.setValue("name", std::string(cmd->arg1));
} else if (cmd->command == COMP_REQ_INVITE_DEVICE) { //request invite
- rep.setValue("CMD", "2");
- rep.setValue("name", cmd->arg1);
- rep.setValue("id", cmd->arg2);
- rep.setValue("PIN", cmd->arg3);
+ rep.setValue("CMD", COMP_REQ_INVITE_DEVICE);
+ 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 == COMP_REQ_EJECT_DEVICE) { //request eject
- rep.setValue("CMD", "3");
- rep.setValue("name", cmd->arg1);
- rep.setValue("id", cmd->arg2);
+ rep.setValue("CMD", COMP_REQ_EJECT_DEVICE);
+ rep.setValue("name", std::string(cmd->arg1));
+ rep.setValue("id", std::string(cmd->arg2));
} else if (cmd->command == COMP_REQ_DELETE_GROUP) { //request delete group
- rep.setValue("CMD", "4");
- rep.setValue("name", cmd->arg1);
+ rep.setValue("CMD", COMP_REQ_DELETE_GROUP);
+ rep.setValue("name", std::string(cmd->arg1));
} else { /* Send Data */
- rep.setValue("CMD", "5");
+ rep.setValue("CMD", COMP_REQ_SEND_DATA);
#ifdef SUPPORT_BASE64_ENCODING
uint32_t outSize = 0;
size_t b64BufSize = B64ENCODE_OUT_SAFESIZE(
(cmd->arg1_len + 1));
char* b64Buf = g_malloc0(b64BufSize);
if (NULL == b64Buf) {
- iotcon_remote_resource_destroy(resource_clone);
_clear_user_data(cmd);
return IOTCON_FUNC_CONTINUE;
}
b64Encode((const char *)cmd->arg1, cmd->arg1_len,
b64Buf, b64BufSize, &outSize);
b64Buf[b64BufSize] = '\0';
- rep.setValue("data", b64Buf);
+ rep.setValue("data", std::string(b64Buf));
LOG_DEBUG("b64BufSize =%d outSize = %d b64Buf = %s",
b64BufSize, outSize, b64Buf);
g_free(b64Buf);
#else
- rep.setValue("data", cmd->arg1);
+ rep.setValue("data", std::string(cmd->arg1));
#endif
}
- resource->post(rep, QueryParamsMap(), _on_post);
+ resource->post(rep, QueryParamsMap(), &_on_post);
}
}
void *user_data)
{
int ret;
- iotcon_query_h query;
std::ostringstream requestURI;
comp_command_t *cmd = (comp_command_t *) user_data;
comp_check_null_ret_error("cmd", cmd, COMP_ERROR_INVALID_PARAMETER);
auto found_cb = std::bind (&_found_resource, std::placeholders::_1,
cmd);
- LOG_DEBUG("Querying for Resource");
- LOG_DEBUG("[Saurav] host address %s", cmd->host ? cmd->host : "NULL");
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,