#include <bluetooth_internal.h>
#include <vconf.h>
#include "Log.h"
+#include <net/if.h>
+#include <net/if_arp.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
#include <glib.h>
#include <pthread.h>
if (error != 0) {
_D("vconf_get_int failed %d", error);
-// return false;
-// temporary code for binary without vconf
- return true;
+ return false;
}
if ((serviceValue & currentState) > 0) {
return false;
}
}
+
+bool conv::util::getPeerMac(const string &remoteIP, const int remotePort, char *buf)
+{
+#ifdef _WIN32
+ return false;
+#else
+ sockaddr_in ClientAddress;
+ memset(&ClientAddress, 0, sizeof(ClientAddress));
+ ClientAddress.sin_family = AF_INET;
+ if(remotePort != -1) {
+ ClientAddress.sin_port = htons(remotePort);
+ }
+ ClientAddress.sin_addr.s_addr = inet_addr(remoteIP.c_str());
+
+ // create socket
+ int sockfd = socket(AF_INET, SOCK_STREAM, 0);
+
+ struct if_nameindex* interface = if_nameindex();
+ struct if_nameindex* head = interface;
+ if(interface != NULL) {
+ while(interface->if_index != 0 && interface->if_name != NULL) {
+ struct arpreq myarp;
+ memset(&myarp, 0, sizeof(myarp));
+ memcpy(&myarp.arp_pa, &ClientAddress, sizeof(myarp.arp_pa));
+ strncpy(myarp.arp_dev, interface->if_name, sizeof(myarp.arp_dev) - 1);
+ myarp.arp_dev[sizeof(myarp.arp_dev) - 1] = 0;
+ if(ioctl(sockfd, SIOCGARP, &myarp) != -1) {
+ unsigned char *ptr = (unsigned char*)(&myarp.arp_ha.sa_data[0]);
+ snprintf(buf, 18,"%02X-%02X-%02X-%02X-%02X-%02X", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5));
+ _D("%s MAC = %s\n", myarp.arp_dev, buf);
+ if_freenameindex(head);
+ if(sockfd >= 0)
+ close(sockfd);
+ return true;
+ } else {
+ _D( "Device:%s error ioctl %d", myarp.arp_dev, errno);
+ }
+ interface++;
+ }
+ if_freenameindex(head);
+ }
+
+ if(sockfd >= 0)
+ close(sockfd);
+
+ return false;
+#endif
+}
+
+std::string conv::util::getIpAddress(std::string hostAddress)
+{
+ if (hostAddress.empty())
+ return "";
+
+ std::string::size_type pos = hostAddress.find(":", 0);
+
+ std::string sub = hostAddress.substr(0, pos);
+
+ return sub;
+}
return CONV_ERROR_NONE;
}
+static int sendResponse(conv::Json payload, const char* request_type, conv_error_e error, conv::Request* requestObj)
+{
+ _D(RED("publishing_response"));
+ IF_FAIL_RETURN_TAG(requestObj != NULL, CONV_ERROR_INVALID_OPERATION, _E, "listener_cb is not registered");
+
+ conv::Json result;
+ conv::Json description = requestObj->getDescription();
+
+ payload.set(NULL, CONV_JSON_RESULT_TYPE, request_type);
+
+ result.set(NULL, CONV_JSON_DESCRIPTION, description);
+ result.set(NULL, CONV_JSON_PAYLOAD, payload);
+ requestObj->publish(error, result);
+
+ return CONV_ERROR_NONE;
+}
+
+//callback function handles app control reply callback from callee application
static void _app_control_cb(app_control_h request, app_control_h reply, app_control_result_e result, void *user_data)
{
_D("app control reply received");
app_control_cb_map.erase(find_iter);
}
-static int handle_request(iotcon_representation_h rep, iotcon_request_h request)
+static int __access_control(iotcon_request_h request, iotcon_attributes_h attributes)
+{
+ string policy;
+ char *deviceName;
+
+ _D("Access Request Handling");
+ int ret = iotcon_attributes_get_str(attributes, CONV_JSON_DEVICE_NAME, &deviceName);
+ IF_FAIL_RETURN_TAG(ret == IOTCON_ERROR_NONE, CONV_ERROR_INVALID_PARAMETER, _E, "iotcon_representation_get_attributes() Fail(%d)", ret);
+
+#if defined(_TV_)
+ _D("TV:Always returns PERMITTED");
+ policy = CONV_ACCESS_CONTROL_PERMITTED;
+#else
+ _D("Always returns PERMITTED");
+ policy = CONV_ACCESS_CONTROL_PERMITTED;
+#endif
+ iotcon_response_result_e result;
+ result = IOTCON_RESPONSE_OK;
+
+ iotcon_representation_h representation;
+ iotcon_representation_create(&representation);
+ iotcon_attributes_h attributesReply;
+ iotcon_attributes_create(&attributesReply);
+
+ iotcon_attributes_add_str(attributesReply, CONV_JSON_ACCESS_REQUEST_RESULT, (char*)policy.c_str());
+
+ iotcon_representation_set_attributes(representation, attributesReply);
+
+ _send_response(request, representation, result);
+
+ iotcon_attributes_destroy(attributesReply);
+ iotcon_representation_destroy(representation);
+
+ return CONV_ERROR_NONE;
+}
+
+static int __app_control_launch(iotcon_request_h request, iotcon_attributes_h attributes)
{
- iotcon_attributes_h attributes;
- char* appctl_char;
app_control_h app_control;
int reply = 0;
+ char *appctl_char;
- int ret = iotcon_representation_get_attributes(rep, &attributes);
- if (IOTCON_ERROR_NONE != ret) {
- _E("iotcon_representation_get_attributes() Fail(%d)", ret);
- return CONV_ERROR_INVALID_PARAMETER;
- }
+// Check if it's permitted device
+#if defined(_TV_)
+ _D("TV:Always PERMITTED");
+#else
+ _D("Always PERMITTED");
+#endif
- ret = iotcon_attributes_get_str(attributes, CONV_JSON_APP_CONTROL, &appctl_char);
- if (IOTCON_ERROR_NONE != ret) {
- _E("iotcon_attributes_get_str() Fail(%d)", ret);
- return CONV_ERROR_INVALID_PARAMETER;
- }
+ int ret = iotcon_attributes_get_str(attributes, CONV_JSON_APP_CONTROL, &appctl_char);
+ IF_FAIL_RETURN_TAG(ret == IOTCON_ERROR_NONE, CONV_ERROR_INVALID_PARAMETER, _E, "iotcon_attributes_get_str() Fail(%d)", ret);
bundle_raw* encoded = reinterpret_cast<unsigned char*>(appctl_char);
bundle* appctl_bundle = bundle_decode(encoded, strlen(appctl_char));
return CONV_ERROR_NONE;
}
+//callback function handles put command for remote-app-control resource
+static int __handle_request(iotcon_representation_h rep, iotcon_request_h request)
+{
+ iotcon_attributes_h attributes;
+ char *requestType;
+
+ int ret = iotcon_representation_get_attributes(rep, &attributes);
+ IF_FAIL_RETURN_TAG(ret == IOTCON_ERROR_NONE, CONV_ERROR_INVALID_PARAMETER, _E, "iotcon_representation_get_attributes() Fail(%d)", ret);
+
+ // Handles Access Request
+ ret = iotcon_attributes_get_str(attributes, CONV_JSON_REQUEST_TYPE, &requestType);
+ IF_FAIL_RETURN_TAG(ret == IOTCON_ERROR_NONE, CONV_ERROR_INVALID_PARAMETER, _E, "iotcon_attributes_get_str() Fail(%d)", ret);
+
+ if (!strcmp(CONV_ACCESS_CONTROL_REQUEST, requestType)) {
+ return __access_control(request, attributes);
+ } else if (!strcmp(CONV_LAUNCH_REQUEST, requestType)) {
+ return __app_control_launch(request, attributes);
+ }
+
+ return CONV_ERROR_INVALID_PARAMETER;
+}
+
+//callback function handles requests for remote-app-control resource
void conv::RemoteAppControlServiceProvider::__iotcon_request_cb(iotcon_resource_h resource, iotcon_request_h request, void *user_data)
{
_D("request cb called");
return;
}
- if (handle_request(req_repr, request) != CONV_ERROR_NONE) {
- _E("handle_request() Fail");
+ if (__handle_request(req_repr, request) != CONV_ERROR_NONE) {
+ _E("__handle_request() Fail");
_send_response(request, NULL, IOTCON_RESPONSE_ERROR);
}
return CONV_ERROR_NONE;
}
+//callback function handles reply from access control request
+static void __on_access_response(iotcon_remote_resource_h resource, iotcon_error_e err,
+ iotcon_request_type_e request_type, iotcon_response_h response, void *user_data)
+{
+ _D("__on_access_response called");
+ int ret;
+ iotcon_response_result_e response_result;
+ iotcon_representation_h repr;
+
+ conv::RemoteAppControlServiceInfo *svcInfo = reinterpret_cast<conv::RemoteAppControlServiceInfo*>(user_data);
+
+ ret = iotcon_response_get_result(response, &response_result);
+ if (IOTCON_ERROR_NONE != ret) {
+ _E("iotcon_response_get_result() Fail(%d)", ret);
+ conv::Json result;
+ sendResponse(result, CONV_JSON_ON_START, CONV_ERROR_INVALID_OPERATION, svcInfo->registeredRequest);
+ return;
+ }
+
+ if (IOTCON_RESPONSE_RESOURCE_CHANGED != response_result) {
+ _E("_on_response_observe Response error(%d)", response_result);
+ conv::Json result;
+ sendResponse(result, CONV_JSON_ON_START, CONV_ERROR_INVALID_OPERATION, svcInfo->registeredRequest);
+ return;
+ }
+
+ ret = iotcon_response_get_representation(response, &repr);
+
+ char* accessControlResult;
+ iotcon_attributes_h attributes;
+ iotcon_representation_get_attributes(repr, &attributes);
+ ret = iotcon_attributes_get_str(attributes, CONV_JSON_ACCESS_REQUEST_RESULT, &accessControlResult);
+ if (IOTCON_ERROR_NONE != ret) {
+ _E("iotcon_attributes_get_str() Fail(%d)", ret);
+ conv::Json result;
+ sendResponse(result, CONV_JSON_ON_START, CONV_ERROR_INVALID_OPERATION, svcInfo->registeredRequest);
+ return;
+ }
+
+ if (!strcmp(CONV_ACCESS_CONTROL_PERMITTED, accessControlResult)) {
+ conv::Json result;
+ svcInfo->accessControlState = ACCESS_CONTROL_STATE_PERMITTED;
+ _D("__on_access_response PERMITTED");
+ sendResponse(result, CONV_JSON_ON_START, CONV_ERROR_NONE, svcInfo->registeredRequest);
+ return;
+ } else {
+ conv::Json result;
+ svcInfo->accessControlState = ACCESS_CONTROL_STATE_DENIED;
+ _D("__on_access_response DENIED");
+ sendResponse(result, CONV_JSON_ON_START, CONV_ERROR_INVALID_OPERATION, svcInfo->registeredRequest);
+ return;
+ }
+}
+
int conv::RemoteAppControlServiceProvider::startRequest(Request* requestObj)
{
IF_FAIL_RETURN_TAG(__activationState == 1, CONV_ERROR_INVALID_OPERATION, _E, "service provider is not activated");
iotcon_resource_types_destroy(resource_types);
iotcon_resource_interfaces_destroy(resource_ifaces);
- sendResponse(result, CONV_JSON_ON_START, CONV_ERROR_NONE, svcInfo->registeredRequest);
-
- return CONV_ERROR_NONE;
-}
+ //send message for access request
+ iotcon_representation_h representation;
+ iotcon_representation_create(&representation);
+ iotcon_attributes_h attributes;
+ iotcon_attributes_create(&attributes);
-int conv::RemoteAppControlServiceProvider::sendResponse(Json payload, const char* request_type, conv_error_e error, Request* requestObj)
-{
- _D(RED("publishing_response"));
- IF_FAIL_RETURN_TAG(requestObj != NULL, CONV_ERROR_INVALID_OPERATION, _E, "listener_cb is not registered");
+ iotcon_attributes_add_str(attributes, CONV_JSON_REQUEST_TYPE, CONV_ACCESS_CONTROL_REQUEST);
+ iotcon_attributes_add_str(attributes, CONV_JSON_DEVICE_NAME, (char*)svcInfo->deviceName.c_str());
- Json result;
- Json description = requestObj->getDescription();
+ iotcon_representation_set_attributes(representation, attributes);
+ svcInfo->iotconInfoObj.iotconRepresentationHandle = representation;
- payload.set(NULL, CONV_JSON_RESULT_TYPE, request_type);
+ iotcon_attributes_destroy(attributes);
- result.set(NULL, CONV_JSON_DESCRIPTION, description);
- result.set(NULL, CONV_JSON_PAYLOAD, payload);
- requestObj->publish(error, result);
+ error = iotcon_remote_resource_put(svcInfo->iotconInfoObj.iotconResourceHandle, representation, NULL, __on_access_response, svcInfo);
return CONV_ERROR_NONE;
}
return CONV_ERROR_NONE;
}
+//callback function handles reply from publish request
static void __on_response(iotcon_remote_resource_h resource, iotcon_error_e err,
iotcon_request_type_e request_type, iotcon_response_h response, void *user_data)
{
RemoteAppControlServiceInfo *svcInfo = reinterpret_cast<RemoteAppControlServiceInfo*>(requestObj->getServiceInfo());
int error;
+ IF_FAIL_RETURN_TAG(svcInfo->accessControlState == ACCESS_CONTROL_STATE_PERMITTED, CONV_ERROR_INVALID_OPERATION, _E, "Access to device is not permitted");
+
requestObj->setCommunicationInfo(&(svcInfo->iotconInfoObj));
iotcon_representation_h representation;
iotcon_attributes_add_str(attributes, CONV_JSON_APP_CONTROL, appControl);
iotcon_attributes_add_int(attributes, CONV_JSON_REPLY, reply);
+ iotcon_attributes_add_str(attributes, CONV_JSON_REQUEST_TYPE, CONV_LAUNCH_REQUEST);
delete[] appControl;