}
signal.countDown();
}
+
+ @Override
+ public void onFindResourceFailed(Throwable ex, String uri) {
+ Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
+ }
};
try {
assertTrue(false);
}
}
+
+ @Override
+ public void onFindResourceFailed(Throwable ex, String uri) {
+ Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
+ }
};
try {
}
signal1.countDown();
}
+
+ @Override
+ public void onFindResourceFailed(Throwable ex, String uri) {
+ Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
+ }
};
try {
}
signal1.countDown();
}
+
+ @Override
+ public void onFindResourceFailed(Throwable ex, String uri) {
+ Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
+ }
};
try {
}
signal1.countDown();
}
+
+ @Override
+ public void onFindResourceFailed(Throwable ex, String uri) {
+ Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
+ }
};
try {
}
signal1.countDown();
}
+
+ @Override
+ public void onFindResourceFailed(Throwable ex, String uri) {
+ Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
+ }
};
try {
}
signal1.countDown();
}
+
+ @Override
+ public void onFindResourceFailed(Throwable ex, String uri) {
+ Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
+ }
};
try {
public void onResourceFound(OcResource resource) {
signal1.countDown();
}
+
+ @Override
+ public void onFindResourceFailed(Throwable ex, String uri) {
+ Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
+ }
};
OcPlatform.OnResourceFoundListener resourceFoundListener2 = new OcPlatform.OnResourceFoundListener() {
public void onResourceFound(OcResource resource) {
signal2.countDown();
}
+
+ @Override
+ public void onFindResourceFailed(Throwable ex, String uri) {
+ Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
+ }
};
OcPlatform.OnResourceFoundListener resourceFoundListener3 = new OcPlatform.OnResourceFoundListener() {
public void onResourceFound(OcResource resource) {
signal3.countDown();
}
+
+ @Override
+ public void onFindResourceFailed(Throwable ex, String uri) {
+ Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
+ }
};
OcPlatform.OnResourceFoundListener resourceFoundListener4 = new OcPlatform.OnResourceFoundListener() {
public void onResourceFound(OcResource resource) {
signal4.countDown();
}
+
+ @Override
+ public void onFindResourceFailed(Throwable ex, String uri) {
+ Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
+ }
};
OcPlatform.OnResourceFoundListener resourceFoundListener5 = new OcPlatform.OnResourceFoundListener() {
public void onResourceFound(OcResource resource) {
signal5.countDown();
}
+
+ @Override
+ public void onFindResourceFailed(Throwable ex, String uri) {
+ Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
+ }
};
OcPlatform.OnResourceFoundListener resourceFoundListener6 = new OcPlatform.OnResourceFoundListener() {
public void onResourceFound(OcResource resource) {
signal6.countDown();
}
+
+ @Override
+ public void onFindResourceFailed(Throwable ex, String uri) {
+ Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
+ }
};
try {
signal.countDown();
}
+
+ @Override
+ public void onFindResourceFailed(Throwable ex, String uri) {
+ Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
+ }
};
try {
}
signal.countDown();
}
+
+ @Override
+ public void onFindResourceFailed(Throwable ex, String uri) {
+ Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
+ }
};
try {
}
}
}
+
+ @Override
+ public void onFindResourceFailed(Throwable ex, String uri) {
+ Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
+ }
};
try {
//server
help_vars.Add(BoolVariable('WITH_RA', 'Build with Remote Access module', False))
help_vars.Add(BoolVariable('WITH_TCP', 'Build with TCP adapter', False))
help_vars.Add(ListVariable('WITH_MQ', 'Build with MQ publisher/broker', 'OFF', ['OFF', 'SUB', 'PUB', 'BROKER']))
-help_vars.Add(EnumVariable('WITH_RD', 'Build including Resource Directory', '0', allowed_values=('0', '1')))
help_vars.Add(BoolVariable('WITH_CLOUD', 'Build including Cloud Connector and Cloud Client sample', False))
+help_vars.Add(ListVariable('RD_MODE', 'Resource Directory build mode', 'CLIENT', ['CLIENT', 'SERVER']))
help_vars.Add(BoolVariable('SIMULATOR', 'Build with simulator module', False))
import org.iotivity.cloud.accountserver.resources.account.tokenrefresh.TokenRefreshResource;
import org.iotivity.cloud.base.ServerSystem;
import org.iotivity.cloud.base.server.CoapServer;
-import org.iotivity.cloud.util.ErrorLogger;
-import org.iotivity.cloud.util.FileLogger;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
/**
*
public class AccountServer {
public static void main(String[] args) throws Exception {
- System.setOut(FileLogger.createLoggingProxy(System.out));
-
+ Log.Init();
+
System.out.println("-----Account SERVER-----");
if (args.length != 2) {
- Logger.e("coap server port and TLS mode required\n"
+ Log.e("coap server port and TLS mode required\n"
+ "ex) 5685 0\n");
return;
}
- ErrorLogger.Init();
-
ServerSystem serverSystem = new ServerSystem();
serverSystem.addResource(new AccountResource());
import org.iotivity.cloud.accountserver.oauth.OAuthServerFactory;
import org.iotivity.cloud.accountserver.token.Token;
import org.iotivity.cloud.accountserver.token.TokenManager;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
/**
*
/**
* API for requesting user account
- *
+ *
* @param userId
* user identifier
* @param deviceId
/**
* API for requesting user account and getting session code for registered
* user.
- *
+ *
* @param userId
* user identifier
* @return String - session code for registered user
/**
* API for requesting user identifier corresponding with authorization
* information.
- *
+ *
* @param authCode
* authorization code
* @param authServer
/**
* API for requesting user identifier corresponding with session code.
- *
+ *
* @param sessionCode
* session code
* @return String - user identifier
/**
* API for getting devices corresponding with user identifier.
- *
+ *
* @param userId
* user identifier
* @return ArrayList<String> - list of devices
*/
public ArrayList<String> requestAccountDevices(String userId) {
- Logger.d("userId= " + userId);
+ Log.d("userId= " + userId);
ArrayList<String> deviceList = AccountDBManager.getInstance()
.getDevices(userId);
import org.bson.Document;
import org.iotivity.cloud.accountserver.Constants;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
/**
* API creating MongoClient and initializing MongoDatabase
- *
+ *
* @param dbname
* database name to create MongoDatabase
* @throws Exception
/**
* API creating collection
- *
+ *
* @param tableName
* collection name
*/
/**
* API deleting collection
- *
+ *
* @param tableName
* collection name
*/
/**
* API getting database object
- *
+ *
*/
public MongoDatabase getMongoDatabase() {
/**
* API for inserting device information of user
- *
+ *
* @param UserDevice
* device information of user
*/
/**
* API for getting user identifier corresponding with session code from
* database
- *
+ *
* @param sessionCode
* session code
* @return String - user identifier
/**
* API for getting devices corresponding with user identifier from database
- *
+ *
* @param userId
* user identifier
*/
updateResource = true;
} else {
- Logger.e("UpdateResource failed!");
+ Log.e("UpdateResource failed!");
}
return updateResource;
ArrayList<UserDevice> dlist = readDeviceResources();
int size = dlist.size();
-
- Logger.i("[" + Constants.DEVICE_TABLE + "]Table");
+
+ Log.i("[" + Constants.DEVICE_TABLE + "]Table");
for (int i = 0; i < size; i++) {
UserDevice item = dlist.get(i);
- Logger.i("[" + i + "]" + item.getUserId() + ", "
+ Log.i("[" + i + "]" + item.getUserId() + ", "
+ item.getDeviceId());
}
/*
* ArrayList<UserSession> slist = readSessionResources(); size =
* slist.size();
- *
- * Logger.i("*Table: " + Constants.SESSION_TABLE);
- *
+ *
+ * Log.i("*Table: " + Constants.SESSION_TABLE);
+ *
* for (int i = 0; i < size; i++) {
- *
+ *
* UserSession item = slist.get(i);
- *
- * Logger.i("[" + i + "]" + item.getUserId() + ", " +
+ *
+ * Log.i("[" + i + "]" + item.getUserId() + ", " +
* item.getSessionCode());
- *
+ *
* }
*/
ArrayList<UserToken> tlist = readUserTokenResources();
size = tlist.size();
- Logger.i("[" + Constants.TOKEN_TABLE + "]Table");
+ Log.i("[" + Constants.TOKEN_TABLE + "]Table");
for (int i = 0; i < size; i++) {
UserToken item = tlist.get(i);
- Logger.i("[" + i + "]" + item.getUserId() + "/"
+ Log.i("[" + i + "]" + item.getUserId() + "/"
+ item.getAccessToken() + "/" + item.getRefreshToken() + "/"
+ item.getIssuedTime());
String refreshToken = doc.getString(Constants.KEY_REFRESH_TOKEN);
String issuedTime = doc.getString(Constants.KEY_ISSUED_TIME);
- // Logger.d("issuedTime: " + issuedTime);
+ // Log.d("issuedTime: " + issuedTime);
userToken.setUserToken(userId, accessToken, refreshToken);
userToken.setIssuedTime(issuedTime);
--- /dev/null
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // 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.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+package org.iotivity.cloud.accountserver.oauth;
+
+import java.util.HashMap;
+import org.apache.oltu.oauth2.client.OAuthClient;
+import org.apache.oltu.oauth2.client.URLConnectionClient;
+import org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest;
+import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
+import org.apache.oltu.oauth2.client.response.GitHubTokenResponse;
+import org.apache.oltu.oauth2.client.response.OAuthResourceResponse;
+import org.apache.oltu.oauth2.common.OAuth;
+import org.apache.oltu.oauth2.common.OAuthProviderType;
+import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
+import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
+import org.apache.oltu.oauth2.common.message.types.GrantType;
+import org.iotivity.cloud.accountserver.Constants;
+import org.iotivity.cloud.util.JSONUtil;
+import org.iotivity.cloud.util.Log;
+
+/**
+ *
+ * This class provides a sample to make .jar that communicates with OAuth2
+ * provider.
+ *
+ */
+public class Github implements OAuthServer {
+
+ // do not use 'client_id' and 'secret' variables.
+ // should use values that are obtained from github.
+ final static private String client_id = "ea9c18f540323b0213d0";
+ final static private String secret = "4bc0cd9fe21269507eb8eba3a32664a0f598dbc9";
+ final static private String resource_url = "https://api.github.com/user";
+
+ @Override
+ public HashMap<String, String> requestAccessToken(String authCode,
+ String authServerUrl) {
+
+ HashMap<String, String> authServerInfo = new HashMap<String, String>();
+
+ try {
+
+ OAuthClientRequest request = OAuthClientRequest
+ .tokenProvider(OAuthProviderType.GITHUB)
+ .setGrantType(GrantType.AUTHORIZATION_CODE)
+ .setClientId(client_id).setClientSecret(secret)
+ .setCode(authCode).buildBodyMessage();
+
+ OAuthClient oAuthClient = new OAuthClient(
+ new URLConnectionClient());
+ GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(request,
+ GitHubTokenResponse.class);
+
+ authServerInfo.put(Constants.KEY_ACCESS_TOKEN_GH,
+ oAuthResponse.getAccessToken());
+
+ } catch (OAuthSystemException | OAuthProblemException e) {
+ authServerInfo.put(Constants.ERROR_MESSAGE, e.getMessage());
+ }
+
+ return authServerInfo;
+ }
+
+ @Override
+ public HashMap<String, String> requestGetUserInfo(String accessToken,
+ String apiServerUrl) {
+
+ HashMap<String, String> authServerInfo = new HashMap<String, String>();
+
+ String userInfo = null;
+
+ if (accessToken == null) {
+ Log.w("accessToken is null!");
+ return null;
+ }
+
+ try {
+
+ OAuthClientRequest request = new OAuthBearerClientRequest(
+ resource_url).setAccessToken(accessToken)
+ .buildQueryMessage();
+
+ OAuthClient oAuthClient = new OAuthClient(
+ new URLConnectionClient());
+ OAuthResourceResponse resourceResponse = oAuthClient.resource(
+ request, OAuth.HttpMethod.GET, OAuthResourceResponse.class);
+
+ userInfo = resourceResponse.getBody();
+ Log.d("userInfo: " + userInfo);
+
+ } catch (OAuthSystemException | OAuthProblemException e) {
+ authServerInfo.put(Constants.ERROR_MESSAGE, e.getMessage());
+ }
+
+ String userIdKey = "login";
+ JSONUtil<HashMap<String, String>> util = new JSONUtil<HashMap<String, String>>();
+ HashMap<String, String> parsedData = util.parseJSON(userInfo,
+ HashMap.class);
+ String userId = parsedData.get(userIdKey);
+ authServerInfo.put(Constants.KEY_USER_ID, userId);
+
+ return authServerInfo;
+ }
+}
import java.util.HashMap;
import org.iotivity.cloud.accountserver.Constants;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
/**
*
.invoke(authServerObj, authCode, authServerUrl);
String errorMessage = authServerInfo.get(Constants.ERROR_MESSAGE);
if (errorMessage != null) {
- Logger.d("Exception Error Message from Auth Server : "
+ Log.d("Exception Error Message from Auth Server : "
+ errorMessage);
} else {
accessToken = authServerInfo.get(Constants.KEY_ACCESS_TOKEN_GH);
.invoke(authServerObj, accessToken, apiServerUrl);
String errorMessage = authServerInfo.get(Constants.ERROR_MESSAGE);
if (errorMessage != null) {
- Logger.d("Exception Error Message from Auth Server : "
+ Log.d("Exception Error Message from Auth Server : "
+ errorMessage);
} else {
userId = authServerInfo.get(Constants.KEY_USER_ID);
try {
URL urls = new URL("jar:" + jarFile.toURI() + "!/");
- Logger.d("urls: " + urls.toString());
+ Log.d("urls: " + urls.toString());
classLoader = new URLClassLoader(new URL[] { urls });
Class<?> gitHubClass = classLoader
import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
import org.iotivity.cloud.base.resource.Resource;
import org.iotivity.cloud.util.Cbor;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
/**
*
String authProvider = payloadData.get(Constants.REQ_AUTH_PROVIDER)
.toString();
- Logger.d("authCode: " + authCode);
+ Log.d("authCode: " + authCode);
@SuppressWarnings("unchecked")
HashMap<String, String> options = (HashMap<String, String>) payloadData
import org.apache.oltu.oauth2.as.issuer.OAuthIssuer;
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
public class TokenGenerator {
try {
accessToken = oauthIssuerImpl.accessToken();
- Logger.d("accessToken : " + accessToken);
+ Log.d("accessToken : " + accessToken);
} catch (OAuthSystemException e) {
try {
refreshToken = oauthIssuerImpl.refreshToken();
- Logger.d("refreshToken : " + refreshToken);
+ Log.d("refreshToken : " + refreshToken);
} catch (OAuthSystemException e) {
package org.iotivity.cloud.accountserver.token;
import org.iotivity.cloud.accountserver.db.AccountDBManager;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
public class TokenManager {
verifyRefreshToken = tokenValidator.verifyRefreshToken(token);
if (!verifyRefreshToken) {
- Logger.w("RefreshToken is not existed!");
+ Log.w("RefreshToken is not existed!");
return null;
}
import java.util.Date;
import org.iotivity.cloud.accountserver.db.AccountDBManager;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
public class TokenValidator {
// check token existed
valid = AccountDBManager.getInstance().hasAccessToken(token);
if (!valid) {
- Logger.w("accessToken is not existed..");
+ Log.w("accessToken is not existed..");
return false;
}
// check token expired
if (getElaspedSeconds(token) > TokenPolicy.EXPIRES_IN) {
- Logger.w("accessToken is expired..");
+ Log.w("accessToken is expired..");
return false;
}
long difference = currentTime.getTime() - issuedTime.getTime();
long elaspedSeconds = difference / 1000;
- // Logger.d("currentTime : " + currentTime.getTime());
- // Logger.d("issuedTime : " + issuedTime.getTime());
- // Logger.d("difference : " + difference);
- Logger.d("accessToken elasped time: " + elaspedSeconds + "s");
+ // Log.d("currentTime : " + currentTime.getTime());
+ // Log.d("issuedTime : " + issuedTime.getTime());
+ // Log.d("difference : " + difference);
+ Log.d("accessToken elasped time: " + elaspedSeconds + "s");
return elaspedSeconds;
}
// check token existed
valid = AccountDBManager.getInstance().hasRefreshToken(token);
if (!valid)
- Logger.w("refreshToken is not existed..");
+ Log.w("refreshToken is not existed..");
return valid;
}
import org.iotivity.cloud.ciserver.resources.proxy.ResourceDirectory;
import org.iotivity.cloud.ciserver.resources.proxy.ResourceFind;
import org.iotivity.cloud.ciserver.resources.proxy.ResourcePresence;
-import org.iotivity.cloud.util.ErrorLogger;
-import org.iotivity.cloud.util.FileLogger;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
public class CloudInterfaceServer {
public static void main(String[] args) throws Exception {
-
- System.setOut(FileLogger.createLoggingProxy(System.out));
+ Log.Init();
System.out.println("-----CI SERVER-------");
if (args.length != 8) {
- Logger.e(
+ Log.e(
"coap server port and RDServer_Address port AccountServer_Address Port MQBroker_Address Port and TLS mode required\n"
+ "ex) 5683 127.0.0.1 5684 127.0.0.1 5685 127.0.0.1 5686 0\n");
return;
new InetSocketAddress(args[5], Integer.parseInt(args[6])),
tlsMode);
- ErrorLogger.Init();
-
DeviceServerSystem deviceServer = new DeviceServerSystem();
Account acHandler = new Account();
import org.iotivity.cloud.base.server.HttpServer;
import org.iotivity.cloud.base.server.Server;
import org.iotivity.cloud.util.Cbor;
-import org.iotivity.cloud.util.ErrorLogger;
+import org.iotivity.cloud.util.Log;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler.Sharable;
: ResponseStatus.BAD_REQUEST;
ctx.channel().writeAndFlush(MessageBuilder
.createResponse((CoapRequest) msg, responseStatus));
- ErrorLogger.write(ctx.channel(), t);
+ Log.f(ctx.channel(), t);
}
}
ctx.writeAndFlush(msg);
} catch (Throwable t) {
- ErrorLogger.write(ctx.channel(), t);
+ Log.f(ctx.channel(), t);
ctx.writeAndFlush(msg);
ctx.close();
}
: ResponseStatus.UNAUTHORIZED;
ctx.channel().writeAndFlush(MessageBuilder
.createResponse((CoapRequest) msg, responseStatus));
- ErrorLogger.write(ctx.channel(), t);
+ Log.f(ctx.channel(), t);
}
}
}
import org.iotivity.cloud.base.protocols.IRequest;
import org.iotivity.cloud.base.protocols.IResponse;
import org.iotivity.cloud.base.protocols.MessageBuilder;
+import org.iotivity.cloud.base.protocols.coap.CoapResponse;
import org.iotivity.cloud.base.protocols.enums.ContentFormat;
import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
import org.iotivity.cloud.base.resource.Resource;
return uriPath.toString();
}
+ private IResponse convertReponseUri(IResponse response, String di) {
+
+ String convertedUri = new String();
+
+ CoapResponse coapResponse = (CoapResponse) response;
+
+ if (coapResponse.getUriPath().isEmpty() == false) {
+ convertedUri = "/di/" + di + "/" + coapResponse.getUriPath();
+ }
+
+ return MessageBuilder
+ .modifyResponse(response, convertedUri, null, null);
+ }
+
class LinkInterfaceHandler implements IResponseEventHandler {
private Cbor<List<HashMap<String, Object>>> mCbor = new Cbor<>();
private String mTargetDI = null;
convertHref(linkPayload);
}
- mSrcDevice.sendResponse(MessageBuilder.modifyResponse(response,
- ContentFormat.APPLICATION_CBOR, linkPayload != null
- ? mCbor.encodingPayloadToCbor(linkPayload) : null));
+ mSrcDevice.sendResponse(MessageBuilder.modifyResponse(
+ convertReponseUri(response, mTargetDI),
+ ContentFormat.APPLICATION_CBOR,
+ linkPayload != null ? mCbor
+ .encodingPayloadToCbor(linkPayload) : null));
}
}
String deviceId = request.getUriPathSegments().get(1);
- requestChannel.sendRequest(
- MessageBuilder.modifyRequest(request,
- extractTargetUriPath(request), null, null, null),
+ requestChannel.sendRequest(MessageBuilder.modifyRequest(request,
+ extractTargetUriPath(request), null, null, null),
new LinkInterfaceHandler(deviceId, srcDevice));
}
+ class DefaultResponseHandler implements IResponseEventHandler {
+ private String mTargetDI = null;
+ private Device mSrcDevice = null;
+
+ public DefaultResponseHandler(String targetDI, Device srcDevice) {
+ mTargetDI = targetDI;
+ mSrcDevice = srcDevice;
+ }
+
+ @Override
+ public void onResponseReceived(IResponse response) {
+
+ mSrcDevice.sendResponse(convertReponseUri(response, mTargetDI));
+ }
+ }
+
// This is optional method for packet handling
@Override
public void onDefaultRequestReceived(Device srcDevice, IRequest request)
throw new NotFoundException();
}
- requestChannel.sendRequest(
- MessageBuilder.modifyRequest(request,
- extractTargetUriPath(request), null, null, null),
- srcDevice);
+ String deviceId = request.getUriPathSegments().get(1);
+
+ requestChannel.sendRequest(MessageBuilder.modifyRequest(request,
+ extractTargetUriPath(request), null, null, null),
+ new DefaultResponseHandler(deviceId, srcDevice));
}
-}
+
+}
\ No newline at end of file
IRequestChannel mPSServer = null;
public MessageQueue() {
- super(Arrays.asList(Constants.PREFIX_OIC, Constants.MQ_BROKER_URI));
+ super(Arrays.asList(Constants.PREFIX_WELL_KNOWN, Constants.PREFIX_OCF,
+ Constants.MQ_BROKER_URI));
mPSServer = ConnectorPool.getConnection("mq");
}
public class Constants extends OCFConstants {
- public static final String MQ_TOPIC = "topic";
- public static final String MQ_LOCATION = "location";
public static final String MQ_TOPICLIST = "topiclist";
- public static final long MIN_SEQ_NUM = 5;
- public static final long MAX_SEQ_NUM = 16777215;
-
// For Kafka
public static final int KAFKA_SESSION_TIMEOUT = 10000;
public static final int KAFKA_CONNECT_TIMEOUT = 10000;
import org.iotivity.cloud.base.ServerSystem;
import org.iotivity.cloud.base.server.CoapServer;
import org.iotivity.cloud.mqserver.resources.MQBrokerResource;
-import org.iotivity.cloud.util.ErrorLogger;
-import org.iotivity.cloud.util.FileLogger;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
public class MessageQueueServer {
public static void main(String[] args) throws Exception {
- System.setOut(FileLogger.createLoggingProxy(System.out));
+ Log.Init();
System.out.println("-----MQ SERVER-----");
if (args.length != 6) {
- Logger.e("coap server port, Kafka_zookeeper_Address port"
+ Log.e("coap server port, Kafka_zookeeper_Address port"
+ " and Kafka_broker_Address Port and TLS mode required\n"
+ "ex) 5686 127.0.0.1 2181 127.0.0.1 9092 0\n");
return;
}
- ErrorLogger.Init();
-
ServerSystem serverSystem = new ServerSystem();
MQBrokerResource MQBroker = new MQBrokerResource();
import java.util.HashMap;
import org.iotivity.cloud.util.Cbor;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
public class MessageQueueUtils {
public static <T> T extractDataFromPayload(byte[] payload, String key) {
if (payload == null || key.isEmpty()) {
- Logger.e("payload or key is empty");
+ Log.e("payload or key is empty");
return null;
}
parsedData = cbor.parsePayloadFromCbor(payload, HashMap.class);
if (parsedData == null || parsedData.containsKey(key) == false) {
- Logger.e("payload doesn't contain " + key + " information");
+ Log.e("payload doesn't contain " + key + " information");
return null;
}
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;
import org.iotivity.cloud.mqserver.Constants;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
public class KafkaCommonWrapper {
public boolean createTopic(String topic) {
- Logger.d("kafka createTopic - " + topic);
+ Log.d("kafka createTopic - " + topic);
topic = topic.replace('/', '.');
public boolean deleteTopic(String topic) {
- Logger.d("kafka deleteTopic - " + topic);
+ Log.d("kafka deleteTopic - " + topic);
topic = topic.replace('/', '.');
import org.I0Itec.zkclient.ZkConnection;
import org.iotivity.cloud.mqserver.Constants;
import org.iotivity.cloud.mqserver.topic.Topic;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
public class KafkaConsumerWrapper {
// TODO exception handling
public boolean subscribeTopic() {
- Logger.d("kafka subscribeTopic - " + mTopicName);
+ Log.d("kafka subscribeTopic - " + mTopicName);
if (mConsumerStarted == true) {
return true;
for (final KafkaStream<byte[], byte[]> stream : streams) {
- Logger.d("kafka subscribe complete");
+ Log.d("kafka subscribe complete");
mConsumerExecutor.execute(new Runnable() {
public boolean unsubscribeTopic() {
- Logger.d("kafka unsubscribeTopic - " + mTopicName);
+ Log.d("kafka unsubscribeTopic - " + mTopicName);
// remove consumer group info in zookeeper
List<String> subscribers = mZkClient.getChildren(ZkUtils
return true;
}
+ public void closeConnection() {
+
+ if (mConsumerStarted == true) {
+ unsubscribeTopic();
+ }
+
+ mZkUtils.close();
+ mZkClient.close();
+ }
+
public ArrayList<byte[]> getMessages() {
- Logger.d("kafka get all messages - " + mTopicName);
+ Log.d("kafka get all messages - " + mTopicName);
String brokerHost = mBroker.substring(0, mBroker.indexOf(':'));
int brokerPort = Integer.parseInt(mBroker.substring(mBroker
.indexOf(':') + 1));
- Logger.d("host " + brokerHost + ", port " + brokerPort);
+ Log.d("host " + brokerHost + ", port " + brokerPort);
// TODO check options - Timeout: Int, bufferSize: Int
SimpleConsumer simpleConsumer = new SimpleConsumer(brokerHost,
if (fetchResponse == null || fetchResponse.hasError()) {
- Logger.e("Error fetching data from the Broker");
+ Log.e("Error fetching data from the Broker");
return null;
}
long currentOffset = messageAndOffset.offset();
if (currentOffset < lastOffset) {
- Logger.e("Found an old offset: " + currentOffset
+ Log.e("Found an old offset: " + currentOffset
+ " Expecting: " + lastOffset);
continue;
}
simpleConsumer.close();
- Logger.d("kafka get all messages complete");
+ Log.d("kafka get all messages complete");
return initialData;
}
OffsetResponse response = consumer.getOffsetsBefore(request);
if (response == null || response.hasError()) {
- Logger.e("Error fetching data Offset Data the Broker");
+ Log.e("Error fetching data Offset Data the Broker");
return 0;
}
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
public class KafkaProducerWrapper {
// TODO handle exception
public boolean publishMessage(byte[] message) {
- Logger.d("kafka publishMessage - " + mTopicName);
+ Log.d("kafka publishMessage - " + mTopicName);
ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(
mTopicName, message);
return true;
}
+ public void closeConnection() {
+
+ mProducer.close();
+ }
+
private Properties buildPropertiesForPublish() {
// TODO check property settings
private TopicManager mTopicManager = new TopicManager();
public MQBrokerResource() {
- super(Arrays.asList(Constants.PREFIX_OIC, Constants.MQ_BROKER_URI));
-
- // addQueryHandler(Arrays.asList("if=" + Constants.INTERFACE_DEFAULT),
- // this::onDefaultInterfaceReceived);
+ super(Arrays.asList(Constants.PREFIX_WELL_KNOWN, Constants.PREFIX_OCF,
+ Constants.MQ_BROKER_URI));
}
public void setKafkaInformation(String zookeeper, String broker) {
private IResponse handleGetRequest(Device srcDevice, IRequest request) {
// DISCOVER
- if (request.getUriPathSegments().size() == getUriPathSegments()
- .size()) {
+ if (request.getUriPathSegments().size() == getUriPathSegments().size()) {
return discoverTopic(request);
}
ResponseStatus.BAD_REQUEST);
}
- // PUBLISH
+ // CREATE topic
private IResponse handlePutRequest(IRequest request) {
- return publishMessage(request);
+ if (request.getUriPathSegments().size() == getUriPathSegments().size()) {
+
+ return MessageBuilder.createResponse(request,
+ ResponseStatus.BAD_REQUEST);
+ }
+
+ return createTopic(request);
}
- // CREATE topic
+ // PUBLISH
private IResponse handlePostRequest(IRequest request) {
- return createTopic(request);
+ return publishMessage(request);
}
// REMOVE topic
private IResponse createTopic(IRequest request) {
- String uriPath = request.getUriPath();
-
// main topic creation request
- if (request.getUriPathSegments().size() == getUriPathSegments()
- .size()) {
+ if (request.getUriPathSegments().size() == getUriPathSegments().size() + 1) {
return createMainTopic(request);
}
// subtopic creation request
+ String uriPath = request.getUriPath();
+ uriPath = uriPath.substring(0, uriPath.lastIndexOf('/'));
+
Topic targetTopic = mTopicManager.getTopic(uriPath);
if (targetTopic == null) {
private IResponse removeTopic(IRequest request) {
- String uriPath = request.getUriPath();
-
- String parentName = uriPath.substring(0, uriPath.lastIndexOf('/'));
- String targetName = request.getUriPathSegments()
- .get(request.getUriPathSegments().size() - 1);
-
// main topic removal request
if (request.getUriPathSegments().size() - 1 == getUriPathSegments()
.size()) {
}
// subtopic removal request
+ String uriPath = request.getUriPath();
+
+ String parentName = uriPath.substring(0, uriPath.lastIndexOf('/'));
+ String targetName = request.getUriPathSegments().get(
+ request.getUriPathSegments().size() - 1);
+
Topic parentTopic = mTopicManager.getTopic(parentName);
if (parentTopic == null) {
}
return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
- ContentFormat.APPLICATION_CBOR, MessageQueueUtils
- .buildPayload(Constants.MQ_TOPICLIST, topicList));
+ ContentFormat.APPLICATION_CBOR, MessageQueueUtils.buildPayload(
+ Constants.MQ_TOPICLIST, topicList));
}
private IResponse createMainTopic(IRequest request) {
- String topicName = MessageQueueUtils.extractDataFromPayload(
- request.getPayload(), Constants.MQ_TOPIC);
+ String topicName = request.getUriPathSegments().get(
+ request.getUriPathSegments().size() - 1);
String type = new String();
ResponseStatus.BAD_REQUEST);
}
- topicName = "/" + Constants.PREFIX_OIC + "/" + Constants.MQ_BROKER_URI
- + "/" + topicName;
+ StringBuilder stringBuilder = new StringBuilder();
+ stringBuilder.append("/");
+
+ for (String uri : getUriPathSegments()) {
+ stringBuilder.append(uri);
+ stringBuilder.append("/");
+ }
+
+ stringBuilder.append(topicName);
+
+ topicName = stringBuilder.toString();
if (mTopicManager.getTopic(topicName) != null) {
// Topic already exists
ResponseStatus.INTERNAL_SERVER_ERROR);
}
- return MessageBuilder.createResponse(request, ResponseStatus.CREATED,
- ContentFormat.APPLICATION_CBOR, MessageQueueUtils.buildPayload(
- Constants.MQ_LOCATION, newTopic.getName()));
+ return MessageBuilder.createResponse(request, ResponseStatus.CREATED);
}
private IResponse removeMainTopic(IRequest request) {
Topic targetTopic = mTopicManager.getTopic(topicName);
- // TODO check error
if (targetTopic == null) {
// Topic doesn't exist
return MessageBuilder.createResponse(request,
ResponseStatus.BAD_REQUEST);
}
+ targetTopic.cleanup();
+
if (mTopicManager.removeTopic(targetTopic) == false) {
return MessageBuilder.createResponse(request,
ResponseStatus.INTERNAL_SERVER_ERROR);
import org.iotivity.cloud.base.protocols.MessageBuilder;
import org.iotivity.cloud.base.protocols.enums.ContentFormat;
import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
-import org.iotivity.cloud.mqserver.Constants;
-import org.iotivity.cloud.mqserver.MessageQueueUtils;
import org.iotivity.cloud.mqserver.kafka.KafkaConsumerWrapper;
import org.iotivity.cloud.mqserver.kafka.KafkaProducerWrapper;
public IResponse handleCreateSubtopic(IRequest request) {
- String newTopicName = MessageQueueUtils.extractDataFromPayload(
- request.getPayload(), Constants.MQ_TOPIC);
+ String newTopicName = request.getUriPathSegments().get(
+ request.getUriPathSegments().size() - 1);
String newTopicType = new String();
mSubtopics.put(newTopicName, newTopic);
- return MessageBuilder.createResponse(
- request,
- ResponseStatus.CREATED,
- ContentFormat.APPLICATION_CBOR,
- MessageQueueUtils.buildPayload(Constants.MQ_LOCATION,
- newTopic.getName()));
+ return MessageBuilder.createResponse(request, ResponseStatus.CREATED);
}
public IResponse handleRemoveSubtopic(IRequest request, String topicName) {
Topic targetTopic = getSubtopic(topicName);
- // TODO check error
if (targetTopic == null) {
// topic doesn't exist
return MessageBuilder.createResponse(request,
ResponseStatus.BAD_REQUEST);
}
+ targetTopic.cleanup();
+
if (mTopicManager.removeTopic(targetTopic) == false) {
+
return MessageBuilder.createResponse(request,
ResponseStatus.INTERNAL_SERVER_ERROR);
}
ContentFormat.APPLICATION_CBOR, mLatestData);
}
+ public void cleanup() {
+
+ mKafkaProducerOperator.closeConnection();
+ mKafkaConsumerOperator.closeConnection();
+ }
+
// callback from Kafka Consumer
public void onMessagePublished(byte[] message) {
notifyPublishedMessage();
}
- // TODO check
private Topic getSubtopic(String topicName) {
if (mSubtopics.containsKey(topicName) == false) {
private void notifyPublishedMessage() {
synchronized (mSubscribers) {
for (TopicSubscriber subscriber : mSubscribers.values()) {
+
subscriber.mSubscriber.sendResponse(MessageBuilder
.createResponse(subscriber.mRequest,
ResponseStatus.CONTENT,
import org.iotivity.cloud.rdserver.resources.directory.res.DiscoveryResource;
import org.iotivity.cloud.rdserver.resources.presence.device.DevicePresenceResource;
import org.iotivity.cloud.rdserver.resources.presence.resource.ResPresenceResource;
-import org.iotivity.cloud.util.ErrorLogger;
-import org.iotivity.cloud.util.FileLogger;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
public class ResourceDirectoryServer {
public static void main(String[] args) throws Exception {
- System.setOut(FileLogger.createLoggingProxy(System.out));
+ Log.Init();
System.out.println("-----RD SERVER-----");
if (args.length != 2) {
- Logger.e("coap server port and TLS mode required\n"
+ Log.e("coap server port and TLS mode required\n"
+ "ex) 5684 0\n");
return;
}
- ErrorLogger.Init();
-
ServerSystem serverSystem = new ServerSystem();
serverSystem.addResource(new ResourceDirectoryResource());
import org.iotivity.cloud.rdserver.resources.presence.resource.ResPresencePayload;
import org.iotivity.cloud.rdserver.util.TypeCastingManager;
import org.iotivity.cloud.util.Cbor;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
public class ResourceDirectoryResource extends Resource {
}
responseMap.put(Constants.LINKS, links);
- Logger.i("publish response :" + responseMap.toString());
+ Log.i("publish response :" + responseMap.toString());
byte[] encodedPaylod = mCbor.encodingPayloadToCbor(responseMap);
if (payloadData == null) {
throw new BadRequestException("payload is null");
} else {
- Logger.i("publish payload: " + payloadData.toString());
+ Log.i("publish payload: " + payloadData.toString());
}
PublishTags tags = new PublishTags();
import org.iotivity.cloud.rdserver.db.DBManager;
import org.iotivity.cloud.rdserver.util.TypeCastingManager;
import org.iotivity.cloud.util.Cbor;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
public class DiscoveryResource extends Resource {
private Cbor<HashMap<Object, Object>> mCbor = new Cbor<>();
responseMapList.add(responseSegment);
}
- Logger.i("discover payload :" + responseMapList.toString());
+ Log.i("discover payload :" + responseMapList.toString());
byte[] encodedPaylod = mCbor.encodingPayloadToCbor(responseMapList);
import org.iotivity.cloud.rdserver.db.DBManager;
import org.iotivity.cloud.rdserver.util.TypeCastingManager;
import org.iotivity.cloud.util.Cbor;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
public class DevicePresenceResource extends Resource {
getPayload.add(payloadSegment);
}
- Logger.i("Get observe response" + getPayload.toString());
+ Log.i("Get observe response" + getPayload.toString());
return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
ContentFormat.APPLICATION_CBOR,
DBManager.getInstance().findDeviceState(deviceId));
getPayload.add(payloadSegment);
}
- Logger.i("Get observe response" + getPayload.toString());
+ Log.i("Get observe response" + getPayload.toString());
return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
ContentFormat.APPLICATION_CBOR,
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
- <version>2.7.4</version>
+ <version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-cbor</artifactId>
- <version>2.7.4</version>
+ <version>2.4.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
import org.iotivity.cloud.base.server.CoapServer;
import org.iotivity.cloud.base.server.HttpServer;
import org.iotivity.cloud.base.server.Server;
-import org.iotivity.cloud.util.ErrorLogger;
+import org.iotivity.cloud.util.Log;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler.Sharable;
} catch (ServerException e) {
ctx.channel().writeAndFlush(MessageBuilder.createResponse(msg,
e.getErrorResponse()));
- ErrorLogger.write(ctx.channel(), e);
+ Log.f(ctx.channel(), e);
} catch (ClientException e) {
- ErrorLogger.write(ctx.channel(), e);
+ Log.f(ctx.channel(), e);
} catch (Throwable t) {
- ErrorLogger.write(ctx.channel(), t);
+ Log.f(ctx.channel(), t);
if (msg instanceof CoapRequest) {
ctx.channel().writeAndFlush(MessageBuilder.createResponse(
msg, ResponseStatus.INTERNAL_SERVER_ERROR));
import org.iotivity.cloud.base.protocols.coap.CoapResponse;
import org.iotivity.cloud.base.protocols.enums.Observe;
import org.iotivity.cloud.util.Bytes;
-import org.iotivity.cloud.util.ErrorLogger;
+import org.iotivity.cloud.util.Log;
import io.netty.channel.Channel;
mChannel.writeAndFlush(request);
} catch (Exception e) {
- ErrorLogger.write(mChannel, e);
+ Log.f(mChannel, e);
}
}
import org.iotivity.cloud.base.connector.CoapClient;
import org.iotivity.cloud.base.protocols.IResponse;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
import io.netty.channel.ChannelHandlerContext;
if (remainTime < 0) {
- Logger.w("accessToken is expired..");
+ Log.w("accessToken is expired..");
return true;
}
if (request instanceof CoapRequest) {
CoapRequest coapRequest = (CoapRequest) request;
CoapResponse coapResponse = new CoapResponse(responseStatus);
+ coapResponse.setUriPath(coapRequest.getUriPath());
coapResponse.setToken(coapRequest.getToken());
if (payload != null) {
coapResponse.setContentFormat(format);
public static IResponse modifyResponse(IResponse orgResponse,
ContentFormat contentFormat, byte[] payload) {
+ return modifyResponse(orgResponse, null, contentFormat, payload);
+ }
+
+ public static IResponse modifyResponse(IResponse orgResponse,
+ String uriPath, ContentFormat contentFormat, byte[] payload) {
+
CoapResponse coapResponse = (CoapResponse) orgResponse;
+ if (uriPath != null) {
+ coapResponse.setUriPath(uriPath);
+ }
if (payload != null) {
coapResponse.setContentFormat(contentFormat);
coapResponse.setPayload(payload);
*/
package org.iotivity.cloud.base.protocols.coap;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler.Sharable;
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
- Logger.v(ctx.channel().id().asLongText().substring(26)
+ Log.v(ctx.channel().id().asLongText().substring(26)
+ " Connected, Address: "
+ ctx.channel().remoteAddress().toString());
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
ctx.fireChannelInactive();
- Logger.v(ctx.channel().id().asLongText().substring(26)
+ Log.v(ctx.channel().id().asLongText().substring(26)
+ " Disconnected, Address: "
+ ctx.channel().remoteAddress().toString());
}
(CoapResponse) msg);
}
- Logger.v(log);
+ Log.v(log);
ctx.writeAndFlush(msg);
}
(CoapResponse) msg);
}
- Logger.v(log);
+ Log.v(log);
ctx.fireChannelRead(msg);
}
import javax.net.ssl.SSLException;
import org.iotivity.cloud.base.OCFConstants;
-import org.iotivity.cloud.util.Logger;
+import org.iotivity.cloud.util.Log;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandler;
throws CertificateException, SSLException, InterruptedException {
try {
- Logger.i("[CoapServer]startServer with tlsMode! mode= " + tlsMode);
+ Log.i("[CoapServer]startServer with tlsMode! mode= " + tlsMode);
if (tlsMode == true) {
throw new PreconditionFailedException("deviceId is null");
}
- Logger.i("deviceId : " + deviceId);
+ Log.i("deviceId : " + deviceId);
return deviceId;
}
+++ /dev/null
-package org.iotivity.cloud.util;
-
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.PrintStream;
-import java.text.SimpleDateFormat;
-import java.util.Calendar;
-
-import io.netty.channel.Channel;
-
-public class ErrorLogger extends Logger {
-
- private static FileOutputStream fos = null;
- private static PrintStream ps = null;
-
- static public void Init() throws FileNotFoundException {
- File dir = new File("..//errLog//");
- if (!dir.isDirectory()) {
- System.out.println("Is Not Folder");
- dir.mkdirs();
- }
- fos = new FileOutputStream("../errLog/[" + getDate() + "] error.log",
- true);
- ps = new PrintStream(fos);
- }
-
- static public void write(Channel channel, Throwable t) {
- String log = channel.id().asLongText().substring(26) + " "
- + t.getMessage();
- ps.println(getTime() + " " + log);
- t.printStackTrace(ps);
- Logger.v(log);
- }
-
- protected static String getDate() {
- Calendar calendar = Calendar.getInstance();
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
- return dateFormat.format(calendar.getTime());
- }
-}
+++ /dev/null
-/*
- * //******************************************************************
- * //
- * // Copyright 2016 Samsung Electronics All Rights Reserved.
- * //
- * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
- * //
- * // 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.
- * //
- * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
- */
-package org.iotivity.cloud.util;
-
-import java.io.PrintStream;
-
-import org.apache.log4j.Logger;
-
-public class FileLogger {
- private final static Logger logger = Logger.getLogger(FileLogger.class);
-
- public static PrintStream createLoggingProxy(
- final PrintStream realPrintStream) {
- // TODO Auto-generated method stub
- return new PrintStream(realPrintStream) {
- public void print(final String string) {
- realPrintStream.print(string);
- logger.fatal(string);
- }
- };
- }
-
-}
*/
package org.iotivity.cloud.util;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
-public class Logger {
+import org.apache.log4j.Logger;
+
+import io.netty.channel.Channel;
+
+public class Log {
public static final int VERBOSE = 0;
public static final int DEBUG = 1;
private static int logLevel = VERBOSE;
+ private static FileOutputStream fos = null;
+ private static PrintStream ps = null;
+
+ private final static Logger logger = Logger.getLogger(Log.class);
+
+ public static void Init() throws FileNotFoundException {
+ System.setOut(Log.createLoggingProxy(System.out));
+ createfile();
+ }
+
+ public static PrintStream createLoggingProxy(
+ final PrintStream realPrintStream) {
+ // TODO Auto-generated method stub
+ return new PrintStream(realPrintStream) {
+ public void print(final String string) {
+ realPrintStream.print(string);
+ logger.fatal(string);
+ }
+ };
+ }
+
+ public static void createfile() throws FileNotFoundException{
+ File dir = new File("..//errLog//");
+ if (!dir.isDirectory()) {
+ dir.mkdirs();
+ }
+ fos = new FileOutputStream("../errLog/[" + getDate() + "] error.log",
+ true);
+ ps = new PrintStream(fos);
+ }
+
+ static public void f(Channel channel, Throwable t) {
+ String log = channel.id().asLongText().substring(26) + " "
+ + t.getMessage();
+ ps.println(getTime() + " " + log);
+ t.printStackTrace(ps);
+ Log.v(log);
+ }
+
+
public static void setLogLevel(int level) {
logLevel = level;
}
return dateFormat.format(calendar.getTime());
}
+ protected static String getDate() {
+ Calendar calendar = Calendar.getInstance();
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
+ return dateFormat.format(calendar.getTime());
+ }
+
private static String getLogLevelString(int level) {
String res = "";
cp -R ./extlibs/cjson $sourcedir/tmp/extlibs
cp -R ./extlibs/gtest $sourcedir/tmp/extlibs
cp -R ./extlibs/tinydtls $sourcedir/tmp/extlibs
-cp -R ./extlibs/sqlite3 $sourcedir/tmp/extlibs
+cp -LR ./extlibs/sqlite3 $sourcedir/tmp/extlibs
cp -R ./extlibs/timer $sourcedir/tmp/extlibs
cp -R ./extlibs/rapidxml $sourcedir/tmp/extlibs
cp -R ./resource $sourcedir/tmp
liboctbstack_env = lib_env.Clone()
target_os = env.get('TARGET_OS')
+rd_mode = env.get('RD_MODE')
with_ra = env.get('WITH_RA')
with_ra_ibb = env.get('WITH_RA_IBB')
with_tcp = env.get('WITH_TCP')
if env.get('DTLS_WITH_X509') == '1':
liboctbstack_env.AppendUnique(CPPDEFINES = ['__WITH_X509__'])
-if env.get('WITH_RD') == '1':
- liboctbstack_env.PrependUnique(CPPPATH = ['../../service/resource-directory/include'])
- liboctbstack_env.PrependUnique(LIBPATH = [env.get('BUILD_DIR') + 'service/resource-directory/include'])
- liboctbstack_env.AppendUnique(CPPDEFINES = ['-DWITH_RD'])
- liboctbstack_env.AppendUnique(LIBS = ['resource_directory'])
-
liboctbstack_env.Append(LIBS = ['c_common'])
if liboctbstack_env.get('ROUTING') in ['GW', 'EP']:
OCTBSTACK_SRC + 'ocobserve.c',
OCTBSTACK_SRC + 'ocserverrequest.c',
OCTBSTACK_SRC + 'occollection.c',
- OCTBSTACK_SRC + 'oicgroup.c',
- OCTBSTACK_SRC + "rdpayload.c"
+ OCTBSTACK_SRC + 'oicgroup.c'
]
+if 'CLIENT' in rd_mode or 'SERVER' in rd_mode:
+ liboctbstack_src.append(OCTBSTACK_SRC + 'rdpayload.c')
+ liboctbstack_src.append(OCTBSTACK_SRC + 'oicresourcedirectory.c')
+ if 'CLIENT' in rd_mode:
+ liboctbstack_env.AppendUnique(CPPDEFINES = ['RD_CLIENT'])
+ if 'SERVER' in rd_mode:
+ liboctbstack_env.AppendUnique(CPPDEFINES = ['RD_SERVER'])
+
if with_tcp == True:
liboctbstack_src.append(OCTBSTACK_SRC + 'oickeepalive.c')
coap_list_t *options = NULL;
coap_transport_type transport = coap_udp;
CAResult_t res = CA_SEND_FAILED;
- if (NULL != data->requestInfo)
+
+ if (!data->requestInfo && !data->responseInfo)
+ {
+ OIC_LOG(ERROR, TAG, "request or response info is empty");
+ return res;
+ }
+
+ if (data->requestInfo)
{
OIC_LOG(DEBUG, TAG, "requestInfo is available..");
info = &data->requestInfo->info;
pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint, &options, &transport);
+ }
+ else if (data->responseInfo)
+ {
+ OIC_LOG(DEBUG, TAG, "responseInfo is available..");
- if (NULL != pdu)
- {
-#ifdef WITH_BWT
- if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter))
- {
- // Blockwise transfer
- res = CAAddBlockOption(&pdu, info, data->remoteEndpoint, &options);
- if (CA_STATUS_OK != res)
- {
- OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
- goto exit;
- }
- }
-#endif // WITH_BWT
- }
- else
- {
- OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
- CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
- return res;
- }
+ info = &data->responseInfo->info;
+ pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
+ &options, &transport);
}
- else
+
+ if (!pdu)
{
- OIC_LOG(ERROR, TAG, "not supported message type for multicast.");
+ OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
+ CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
+ coap_delete_list(options);
return res;
}
+#ifdef WITH_BWT
+ if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter))
+ {
+ // Blockwise transfer
+ res = CAAddBlockOption(&pdu, info, data->remoteEndpoint, &options);
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
+ goto exit;
+ }
+ }
+#endif // WITH_BWT
+
CALogPDUInfo(pdu, data->remoteEndpoint);
OIC_LOG(DEBUG, TAG, "pdu to send :");
catest_env.AppendUnique(LIBS = ['tinydtls'])
catest_env.AppendUnique(LIBS = ['timer'])
-if catest_env.get('WITH_RD') == '1':
- catest_env.PrependUnique(LIBS = ['resource_directory'])
-
if catest_env.get('LOGGING'):
catest_env.AppendUnique(CPPDEFINES = ['TB_LOG'])
import os.path
provisioning_env = env.Clone()
+target_os = provisioning_env.get('TARGET_OS')
######################################################################
# Build flags
provisioning_env.AppendUnique(CPPDEFINES = ['TB_LOG'])
+if target_os == 'tizen':
+ provisioning_env.AppendUnique(CPPDEFINES = ['__TIZEN__'])
+
######################################################################
# Source files and Targets
######################################################################
// A device should always have a default acl. Therefore, payload should never be NULL.
ehRet = (payload ? OC_EH_OK : OC_EH_ERROR);
- // Send response payload to request originator
- if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, payload, size))
- {
- ehRet = OC_EH_ERROR;
- OIC_LOG(ERROR, TAG, "SendSRMResponse failed for HandleACLGetRequest");
- }
+ //Send payload to request originator
+ ehRet = ((SendSRMResponse(ehRequest, ehRet, payload, size)) == OC_STACK_OK) ?
+ OC_EH_OK : OC_EH_ERROR;
+
OICFree(payload);
OIC_LOG_V(DEBUG, TAG, "%s RetVal %d", __func__, ehRet);
DeleteACLList(newAcl);
}
- // Send payload to request originator
- if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL, 0))
- {
- ehRet = OC_EH_ERROR;
- OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandleACLPostRequest");
- }
+ //Send response to request originator
+ ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
+ OC_EH_OK : OC_EH_ERROR;
OIC_LOG_V(DEBUG, TAG, "%s RetVal %d", __func__, ehRet);
return ehRet;
}
exit:
- // Send payload to request originator
- if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL, 0))
- {
- ehRet = OC_EH_ERROR;
- OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandleACLDeleteRequest");
- }
+ //Send response to request originator
+ ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
+ OC_EH_OK : OC_EH_ERROR;
return ehRet;
}
break;
default:
- ehRet = OC_EH_ERROR;
- SendSRMResponse(ehRequest, ehRet, NULL, 0);
+ ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
+ OC_EH_OK : OC_EH_ERROR;
}
}
OCEntityHandlerResult ret = OC_EH_ERROR;
OIC_LOG(DEBUG, TAG, "HandleCREDPostRequest IN");
+ static uint16_t previousMsgId = 0;
//Get binary representation of cbor
OicSecCred_t *cred = NULL;
uint8_t *payload = (((OCSecurityPayload*)ehRequest->payload)->securityData);
* If some error is occured while ownership transfer,
* ownership transfer related resource should be revert back to initial status.
*/
- RestoreDoxmToInitState();
- RestorePstatToInitState();
+ const OicSecDoxm_t* doxm = GetDoxmResourceData();
+ if(doxm)
+ {
+ if(!doxm->owned && previousMsgId != ehRequest->messageID)
+ {
+ OIC_LOG(WARNING, TAG, "The operation failed during handle DOXM request,"\
+ "DOXM will be reverted.");
+ RestoreDoxmToInitState();
+ RestorePstatToInitState();
+ }
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "Invalid DOXM resource");
+ }
}
}
else
}
FreeCred(cred);
}
+ else
+ {
+ previousMsgId = ehRequest->messageID;
+ }
+ //Send response to request originator
+ ret = ((SendSRMResponse(ehRequest, ret, NULL, 0)) == OC_STACK_OK) ?
+ OC_EH_OK : OC_EH_ERROR;
+
OIC_LOG(DEBUG, TAG, "HandleCREDPostRequest OUT");
return ret;
}
// A device should always have a default cred. Therefore, payload should never be NULL.
OCEntityHandlerResult ehRet = (res == OC_STACK_OK) ? OC_EH_OK : OC_EH_ERROR;
- // Send response payload to request originator
- if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, payload, size))
- {
- ehRet = OC_EH_ERROR;
- OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandlePstatGetRequest");
- }
+
+ //Send payload to request originator
+ ehRet = ((SendSRMResponse(ehRequest, ehRet, payload, size)) == OC_STACK_OK) ?
+ OC_EH_OK : OC_EH_ERROR;
OICFree(payload);
return ehRet;
}
{
ehRet = OC_EH_RESOURCE_DELETED;
}
-
+ //Send response to request originator
+ ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
+ OC_EH_OK : OC_EH_ERROR;
exit:
return ehRet;
}
ret = HandleDeleteRequest(ehRequest);
break;
default:
- ret = OC_EH_ERROR;
+ ret = ((SendSRMResponse(ehRequest, ret, NULL, 0)) == OC_STACK_OK) ?
+ OC_EH_OK : OC_EH_ERROR;
break;
}
}
-
- //Send payload to request originator
- ret = (SendSRMResponse(ehRequest, ret, NULL, 0) == OC_STACK_OK) ?
- ret : OC_EH_ERROR;
-
return ret;
}
OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
// Send response payload to request originator
- if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, payload, size))
- {
- ehRet = OC_EH_ERROR;
- OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandleDoxmGetRequest");
- }
+ ehRet = ((SendSRMResponse(ehRequest, ehRet, payload, size)) == OC_STACK_OK) ?
+ OC_EH_OK : OC_EH_ERROR;
OICFree(payload);
{
if(!gDoxm->owned && previousMsgId != ehRequest->messageID)
{
- OIC_LOG(WARNING, TAG, "The operation failed during handle DOXM request,"\
- "DOXM will be reverted.");
RestoreDoxmToInitState();
RestorePstatToInitState();
}
}
//Send payload to request originator
- if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL, 0))
- {
- ehRet = OC_EH_ERROR;
- OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandleDoxmPostRequest");
- }
+ ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
+ OC_EH_OK : OC_EH_ERROR;
+
DeleteDoxmBinData(newDoxm);
return ehRet;
break;
default:
- ehRet = OC_EH_ERROR;
- SendSRMResponse(ehRequest, ehRet, NULL, 0);
+ ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
+ OC_EH_OK : OC_EH_ERROR;
break;
}
}
}
// Send response payload to request originator
- if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, payload, size))
- {
- ehRet = OC_EH_ERROR;
- OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandlePstatGetRequest");
- }
+ ehRet = ((SendSRMResponse(ehRequest, ehRet, payload, size)) == OC_STACK_OK) ?
+ OC_EH_OK : OC_EH_ERROR;
OICFree(payload);
return ehRet;
}
prevMsgId = ehRequest->messageID;
}
+ // Send response payload to request originator
+ ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
+ OC_EH_OK : OC_EH_ERROR;
- //Send payload to request originator
- if(OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL, 0))
- {
- ehRet = OC_EH_ERROR;
- OIC_LOG (ERROR, TAG, "SendSRMResponse failed in HandlePstatPostRequest");
- }
DeletePstatBinData(pstat);
return ehRet;
}
ehRet = HandlePstatPostRequest(ehRequest);
break;
default:
- ehRet = OC_EH_ERROR;
- SendSRMResponse(ehRequest, ehRet, NULL, 0);
+ ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
+ OC_EH_OK : OC_EH_ERROR;
break;
}
}
/** Pointer of ActionSet which to support group action.*/
OCActionSet *actionsetHead;
+
+ /** The instance identifier for this web link in an array of web links - used in links. */
+ union
+ {
+ /** An ordinal number that is not repeated - must be unique in the collection context. */
+ uint8_t ins;
+ /** Any unique string including a URI. */
+ char *uniqueStr;
+ /** Use UUID for universal uniqueness - used in /oic/res to identify the device. */
+ OCIdentity uniqueUUID;
+ };
} OCResource;
/** The coap scheme */
#define OC_COAP_SCHEME "coap://"
-/** the first outgoing sequence number will be 2*/
-#define OC_OFFSET_SEQUENCE_NUMBER (1)
+/** the first outgoing sequence number will be 1*/
+#define OC_OFFSET_SEQUENCE_NUMBER (0)
/**
* This structure will be created in occoap and passed up the stack on the server side.
*/
OC_EXPORT OCStackResult OCDoDirectPairing(void *ctx, OCDPDev_t* peer, OCPrm_t pmSel, char *pinNumber,
OCDirectPairingCB resultCallback);
+
+#if defined(RD_CLIENT) || defined(RD_SERVER)
+/**
+ * This function binds an resource unique id to the resource.
+ *
+ * @param handle Handle to the resource that the contained resource is to be bound.
+ * @param ins Unique ID for resource.
+ *
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+OC_EXPORT OCStackResult OCBindResourceInsToResource(OCResourceHandle handle, uint8_t ins);
+
+/**
+ * This function gets the resource unique id for a resource.
+ *
+ * @param handle Handle of resource.
+ * @param ins Unique ID for resource.
+ *
+ * @return Ins if resource found or 0 resource not found.
+ */
+OC_EXPORT OCStackResult OCGetResourceIns(OCResourceHandle handle, uint8_t *ins);
+
+/**
+ * This function gets a resource handle by resource uri.
+ *
+ * @param uri Uri of Resource to get Resource handle.
+ *
+ * @return Found resource handle or NULL if not found.
+ */
+OC_EXPORT OCResourceHandle OCGetResourceHandleAtUri(const char *uri);
+#endif
//#endif // DIRECT_PAIRING
#ifdef __cplusplus
/** Presence URI through which the OIC devices advertise their presence.*/
#define OC_RSRVD_PRESENCE_URI "/oic/ad"
+#ifdef WITH_CLOUD
/** Presence URI through which the OCF devices advertise their device presence.*/
#define OCF_RSRVD_DEVICE_PRESENCE_URI "/.well-known/ocf/prs"
+#endif
/** Sets the default time to live (TTL) for presence.*/
#define OC_DEFAULT_PRESENCE_TTL_SECONDS (60)
/** To represent interface.*/
#define OC_RSRVD_INTERFACE "if"
+/** To indicate how long RD should publish this item.*/
+#define OC_RSRVD_DEVICE_TTL "lt"
+
/** To represent time to live.*/
#define OC_RSRVD_TTL "ttl"
#define OC_RSRVD_HOSTING_PORT "port"
/** TCP Port. */
-#define OC_RSRVD_TCP_PORT "tcp"
+#define OC_RSRVD_TCP_PORT "x.org.iotivity.tcp"
/** For Server instance ID.*/
#define OC_RSRVD_SERVER_INSTANCE_ID "sid"
#define MAC_ADDR_BLOCKS (6)
/** Max identity size. */
-#define MAX_IDENTITY_SIZE (32)
+#define MAX_IDENTITY_SIZE (37)
/** Universal unique identity size. */
#define UUID_IDENTITY_SIZE (128/8)
#define OC_RSRVD_TITLE "title"
/** Defines URI. */
-#define OC_RSRVD_URI "uri"
+#define OC_RSRVD_URI "anchor"
/** Defines media type. */
-#define OC_RSRVD_MEDIA_TYPE "mt"
+#define OC_RSRVD_MEDIA_TYPE "type"
/** To represent resource type with Publish RD.*/
#define OC_RSRVD_RESOURCE_TYPE_RDPUBLISH "oic.wk.rdpub"
{
/** This is the target relative URI. */
char *href;
+ /** The relation of the target URI referenced by the link to the context URI;
+ * The default value is null. */
+ char *rel;
/** Resource Type - A standard OIC specified or vendor defined resource
* type of the resource referenced by the target URI. */
OCStringLL *rt;
/** Interface - The interfaces supported by the resource referenced by the target URI. */
OCStringLL *itf;
- /** The relation of the target URI referenced by the link to the context URI;
- * The default value is null. */
- char *rel;
- /** Specifies if the resource referenced by the target URIis observable or not. */
- bool obs;
+ /** Bitmap - The bitmap holds observable, discoverable, secure option flag. */
+ uint8_t p;
/** A title for the link relation. Can be used by the UI to provide a context. */
char *title;
/** This is used to override the context URI e.g. override the URI of the containing collection. */
- char *uri;
+ char *anchor;
/** The instance identifier for this web link in an array of web links - used in links. */
union
{
/** Use UUID for universal uniqueness - used in /oic/res to identify the device. */
OCIdentity uniqueUUID;
};
+ /** Time to keep holding resource.*/
+ uint64_t ttl;
/** A hint of the media type of the representation of the resource referenced by the target URI. */
- OCStringLL *mt;
+ OCStringLL *type;
/** Holding address of the next resource. */
struct OCLinksPayload *next;
} OCLinksPayload;
OCDeviceInfo n;
/** Device identifier. */
OCIdentity di;
- /** The base URI where the resources are hold. */
- char *baseURI;
- /** Bitmap holds observable, discoverable, secure option flag.*/
- uint8_t bitmap;
- /** Port set in case, the secure flag is set above. */
- uint16_t port;
- /** Id for each set of links i.e. tag. */
- union
- {
- /** An ordinal number that is not repeated - must be unique in the collection context. */
- uint8_t ins;
- /** Any unique string including a URI. */
- char *uniqueStr;
- /** Use UUID for universal uniqueness - used in /oic/res to identify the device. */
- OCIdentity uniqueUUID;
- };
- /** Defines the list of allowable resource types (for Target and anchors) in links included
- * in the collection; new links being created can only be from this list. */
- char *rts;
- /** When specified this is the default relationship to use when an OIC Link does not specify
- * an explicit relationship with *rel* parameter. */
- char *drel;
/** Time to keep holding resource.*/
- uint32_t ttl;
+ uint64_t ttl;
} OCTagsPayload;
/** Resource collection payload. */
OCTagsPayload *tags;
/** Array of links payload. */
OCLinksPayload *setLinks;
- /** Holding address of the next resource. */
- struct OCResourceCollectionPayload *next;
} OCResourceCollectionPayload;
typedef struct OCDiscoveryPayload
--- /dev/null
+//******************************************************************
+//
+// Copyright 2016 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// 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.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef OC_RESOURCE_DIRECTORY_H_
+#define OC_RESOURCE_DIRECTORY_H_
+
+#include "octypes.h"
+#include "logger.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif // __cplusplus
+
+#ifdef RD_CLIENT
+/**
+ * Publish RD resource to Resource Directory.
+ *
+ * @param host The address of the RD.
+ * @param connectivityType Type of connectivity indicating the interface.
+ * @param resourceHandles This is the resource handle which we need to register to RD.
+ * @param nHandles The counts of resource handle.
+ * @param cbData Asynchronous callback function that is invoked by the stack when
+ * response is received. The callback is generated for each response
+ * received.
+ * @param qos Quality of service.
+ *
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+OC_EXPORT OCStackResult OCRDPublish(const char *host, OCConnectivityType connectivityType,
+ OCResourceHandle resourceHandles[], uint8_t nHandles,
+ OCCallbackData *cbData, OCQualityOfService qos);
+
+/**
+ * Delete RD resource from Resource Directory.
+ *
+ * @param host The address of the RD.
+ * @param connectivityType Type of connectivity indicating the interface.
+ * @param resourceHandles This is the resource handle which we need to delete to RD.
+ * @param nHandles The counts of resource handle.
+ * @param cbData Asynchronous callback function that is invoked by the stack when
+ * response is received. The callback is generated for each response
+ * received.
+ * @param qos Quality of service.
+ *
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+OC_EXPORT OCStackResult OCRDDelete(const char *host, OCConnectivityType connectivityType,
+ OCResourceHandle resourceHandles[], uint8_t nHandles,
+ OCCallbackData *cbData, OCQualityOfService qos);
+#endif
+#ifdef __cplusplus
+}
+#endif // __cplusplus
+
+#endif /* OC_RESOURCE_DIRECTORY_H_ */
OC_EXPORT const char *convertTriggerEnumToString(OCPresenceTrigger trigger);
OC_EXPORT OCPresenceTrigger convertTriggerStringToEnum(const char * triggerStr);
+#if defined(RD_CLIENT) || defined(RD_SERVER)
+INLINE_API void OCTagsLog(const LogLevel level, const OCTagsPayload *tags)
+{
+ if (tags)
+ {
+ if (tags->n.deviceName)
+ {
+ OIC_LOG_V(level, PL_TAG, " Device Name : %s ", tags->n.deviceName);
+ }
+ if (tags->di.id)
+ {
+ OIC_LOG_V(level, PL_TAG, " Device ID : %s ", tags->di.id);
+ }
+ OIC_LOG_V(level, PL_TAG, " lt : %lld ",tags->ttl);
+ }
+ else
+ {
+ (void) level;
+ }
+}
+
+INLINE_API void OCLinksLog(const LogLevel level, const OCLinksPayload *links)
+{
+ if (!links)
+ {
+ return;
+ }
+
+ while (links)
+ {
+ if (links->href)
+ {
+ OIC_LOG_V(level, PL_TAG, " href: %s ",links->href);
+ }
+ OIC_LOG(level, PL_TAG, " RT: ");
+ OCStringLL *rt = links->rt;
+ while (rt)
+ {
+ if (rt->value)
+ {
+ OIC_LOG_V(level, PL_TAG, " %s", rt->value);
+ }
+ rt = rt->next;
+ }
+ OIC_LOG(level, PL_TAG, " IF: ");
+ OCStringLL *itf = links->itf;
+ while (itf)
+ {
+ if (itf->value)
+ {
+ OIC_LOG_V(level, PL_TAG, " %s", itf->value);
+ }
+ itf = itf->next;
+ }
+ OIC_LOG(level, PL_TAG, " MT: ");
+ OCStringLL *mt = links->type;
+ while (mt)
+ {
+ if (mt->value)
+ {
+ OIC_LOG_V(level, PL_TAG, " %s", mt->value);
+ }
+ mt = mt->next;
+ }
+ if (links->type)
+ {
+ OIC_LOG_V(level, PL_TAG, " %s", links->type);
+ }
+ OIC_LOG_V(level, PL_TAG, " INS: %d", links->ins);
+ OIC_LOG_V(level, PL_TAG, " TTL: %d", links->ttl);
+ OIC_LOG_V(level, PL_TAG, " P: %d", links->p);
+ if (links->rel)
+ {
+ OIC_LOG_V(level, PL_TAG, " REL: %s", links->rel);
+ }
+ if (links->title)
+ {
+ OIC_LOG_V(level, PL_TAG, " TITLE: %s", links->title);
+ }
+ if (links->anchor)
+ {
+ OIC_LOG_V(level, PL_TAG, " URI: %s", links->anchor);
+ }
+ links = links->next;
+ }
+}
+#endif
INLINE_API void OCPayloadLogRep(LogLevel level, OCRepPayload* payload)
{
OIC_LOG(level, (PL_TAG), "Payload Type: Representation");
OIC_LOG_V(level, PL_TAG, "\tSecurity Data: %s", payload->securityData);
}
+#if defined(RD_CLIENT) || defined(RD_SERVER)
INLINE_API void OCRDPayloadLog(const LogLevel level, const OCRDPayload *payload)
{
if (!payload)
OCLinksLog(level, rdPublish->setLinks);
}
}
-
+#endif
INLINE_API void OCPayloadLog(LogLevel level, OCPayload* payload)
{
if(!payload)
case PAYLOAD_TYPE_SECURITY:
OCPayloadLogSecurity(level, (OCSecurityPayload*)payload);
break;
+#if defined(RD_CLIENT) || defined(RD_SERVER)
case PAYLOAD_TYPE_RD:
OCRDPayloadLog(level, (OCRDPayload*)payload);
break;
+#endif
default:
OIC_LOG_V(level, PL_TAG, "Unknown Payload Type: %d", payload->type);
break;
extern "C" {
#endif // __cplusplus
+#define OIC_RD_PUBLISH_TTL 86400
+
+#define OIC_RD_DEFAULT_RESOURCE 2
+
+#define DEFAULT_MESSAGE_TYPE "application/json"
+
/**
* Converts RD payload from structure to CBOR format. It creates the outPayload
* which is then transmitted over the wire.
*/
OCRDPayload *OCRDPayloadCreate();
+#ifdef RD_CLIENT
+/**
+ * Initializes RD Publish payload structure.
+ *
+ * @param resourceHandles The handle of registered resource.
+ * @param nHandles The number of registered resource handles.
+ * @param ttl Time to live of the published resource.
+ *
+ * @return Allocated memory for the OCRDPayload and NULL in case if
+ * failed to allocate memory.
+ */
+OCRDPayload *OCRDPublishPayloadCreate(OCResourceHandle resourceHandles[], uint8_t nHandles,
+ uint64_t ttl);
+#endif
+
/**
* Initializes RD Discovery payload structure and sets the bias factor.
*
* Copies tag paramter to creates OCTagsPayload.
*
* @param deviceName The device name as set during enrollment.
- * @param id The device UUID
- * @param baseURI baseURI pointing to the resource directory location.
- * @param bitmap The bitmap value include observe, discovery and secure bit set.
- * @param port The secure port in case above bitmap is set to secure.
- * @param ins Unique value per collection.
- * @param rts Defines allowed resource types.
- * @param drel Defines defaultr relationship.
+ * @param id The device UUID.
* @param ttl Time to leave for the . Used only in resource directory.
*
* @retun Allocated memory for OCTagsPayload or else NULL in case of error.
*/
-OCTagsPayload* OCCopyTagsResources(const char *deviceName, const unsigned char *id,
- const char *baseURI, uint8_t bitmap, uint16_t port, uint8_t ins, const char *rts, const char *drel, uint32_t ttl);
+OCTagsPayload* OCCopyTagsResources(const char *deviceName, const unsigned char *id, uint64_t ttl);
/**
* Copies link resource to create LinksPayload.
*
* @param href URI of the resource
+ * @param rel Relation
* @param rt Array of String pointing to resource types.
* @param itf Array of String pointing to interface
- * @param rel Relation
- * @param obs Whether to observe or not.
+ * @param p Whether to observe or not.
* @param title Title
- * @param uri URI
+ * @param anchor URI
* @param ins Unique value per link.
+ * @param ttl Time to live for this link.
* @param mt Media Type
- * @retun Allocated memory for OCLinksPayload or else NULL in case of error.
+ * @retun Allocated memory for OCLinksResource or else NULL in case of error.
*/
-OCLinksPayload* OCCopyLinksResources(const char *href, OCStringLL *rt, OCStringLL *itf,
- const char *rel, bool obs, const char *title, const char *uri, uint8_t ins, OCStringLL *mt);
+OCLinksPayload* OCCopyLinksResources(const char *href, const char *rel, OCStringLL *rt,
+ OCStringLL *itf, uint8_t p, const char *title,
+ const char *anchor, uint8_t ins, uint64_t ttl,
+ OCStringLL *mt);
/**
* Creates a resource collection object.
*/
void OCDiscoveryCollectionPayloadDestroy(OCDiscoveryPayload* payload);
-/**
- * Prints tags payload.
- *
- * @param level LogLevel for the print.
- * @param tags Structure of the tags payload.
- */
-OC_EXPORT void OCTagsLog(const LogLevel level, const OCTagsPayload *tags);
-
-/**
- * Prints links payload.
- *
- * @param level LogLevel for the print.
- * @param tags Structure of the links payload.
- */
-OC_EXPORT void OCLinksLog(const LogLevel level, const OCLinksPayload *links);
-
#ifdef __cplusplus
}
#endif // __cplusplus
case PAYLOAD_TYPE_SECURITY:
OCSecurityPayloadDestroy((OCSecurityPayload*)payload);
break;
+#if defined(RD_CLIENT) || defined(RD_SERVER)
case PAYLOAD_TYPE_RD:
OCRDPayloadDestroy((OCRDPayload*)payload);
break;
+#endif
default:
OIC_LOG_V(ERROR, TAG, "Unsupported payload type in destroy: %d", payload->type);
OICFree(payload);
if (!val)
{
- return false;
+ return true;
}
return val->type == OCREP_PROP_NULL;
#include "ocrandom.h"
#include "ocresourcehandler.h"
#include "cbor.h"
+
+#if defined(RD_CLIENT) || defined(RD_SERVER)
#include "rdpayload.h"
+#endif
#define TAG "OIC_RI_PAYLOADCONVERT"
return OCConvertPresencePayload((OCPresencePayload*)payload, outPayload, size);
case PAYLOAD_TYPE_SECURITY:
return OCConvertSecurityPayload((OCSecurityPayload*)payload, outPayload, size);
+#if defined(RD_CLIENT) || defined(RD_SERVER)
case PAYLOAD_TYPE_RD:
return OCRDPayloadToCbor((OCRDPayload*)payload, outPayload, size);
+#endif
default:
OIC_LOG_V(INFO,TAG, "ConvertPayload default %d", payload->type);
return CborErrorUnknownType;
#include "ocpayloadcbor.h"
#include "ocstackinternal.h"
#include "payload_logging.h"
-#include "rdpayload.h"
#include "platform_features.h"
+#if defined(RD_CLIENT) || defined(RD_SERVER)
+#include "rdpayload.h"
+#endif
+
#define TAG "OIC_RI_PAYLOADPARSE"
static OCStackResult OCParseDiscoveryPayload(OCPayload **outPayload, CborValue *arrayVal);
case PAYLOAD_TYPE_SECURITY:
result = OCParseSecurityPayload(outPayload, payload, payloadSize);
break;
+#if defined(RD_CLIENT) || defined(RD_SERVER)
case PAYLOAD_TYPE_RD:
result = OCRDCborToPayload(&rootValue, outPayload);
break;
+#endif
default:
OIC_LOG_V(ERROR, TAG, "ParsePayload Type default: %d", payloadType);
result = OC_STACK_INVALID_PARAM;
return result;
}
-void OCFreeOCStringLL(OCStringLL* ll);
-
static OCStackResult OCParseSecurityPayload(OCPayload** outPayload, const uint8_t *payload,
size_t size)
{
#ifdef WITH_STRING_H
#include <string.h>
#endif
-#ifdef WITH_STRINGS_H
+#ifdef WITH_STRING_H
#include <strings.h>
#endif
#include "secureresourcemanager.h"
#include "cacommon.h"
#include "cainterface.h"
-#include "rdpayload.h"
#include "ocpayload.h"
-#ifdef WITH_RD
-#include "rd_server.h"
-#endif
-
#ifdef ROUTING_GATEWAY
#include "routingmanager.h"
#endif
return OCDoResponse(&response);
}
-#ifdef WITH_RD
-static OCStackResult checkResourceExistsAtRD(const char *interfaceType, const char *resourceType,
- OCResource **payload, OCDevAddr *devAddr)
-{
- OCResourceCollectionPayload *repPayload;
- if (!payload)
- {
- return OC_STACK_ERROR;
- }
- if (OCRDCheckPublishedResource(interfaceType, resourceType, &repPayload, devAddr) == OC_STACK_OK)
- {
- if (!repPayload)
- {
- return OC_STACK_ERROR;
- }
- OCResource *ptr = ((OCResource *) OICCalloc(1, sizeof(OCResource)));
- if (!ptr)
- {
- return OC_STACK_NO_MEMORY;
- }
-
- ptr->uri = OICStrdup(repPayload->setLinks->href);
- if (!ptr->uri)
- {
- return OC_STACK_NO_MEMORY;
- }
- OCStringLL *rt = repPayload->setLinks->rt;
- while (rt)
- {
- OCResourceType *temp = (OCResourceType *) OICCalloc(1, sizeof(OCResourceType));
- if(!temp)
- {
- OICFree(ptr->uri);
- return OC_STACK_NO_MEMORY;
- }
- temp->next = NULL;
- temp->resourcetypename = OICStrdup(rt->value);
- if (!ptr->rsrcType)
- {
- ptr->rsrcType = temp;
- }
- else
- {
- OCResourceType *type = ptr->rsrcType;
- while (type->next)
- {
- type = type->next;
- }
- type->next = temp;
- }
- rt = rt->next;
- }
-
- OCStringLL *itf = repPayload->setLinks->itf;
- while (itf)
- {
- OCResourceInterface *temp = (OCResourceInterface *) OICCalloc(1, sizeof(OCResourceInterface));
- if (!temp)
- {
- OICFree(ptr->uri);
-
- return OC_STACK_NO_MEMORY;
- }
- temp->next = NULL;
- temp->name = OICStrdup(itf->value);
- if (!ptr->rsrcInterface)
- {
- ptr->rsrcInterface = temp;
- }
- else
- {
- OCResourceInterface *type = ptr->rsrcInterface;
- while (type->next)
- {
- type = type->next;
- }
- type->next = temp;
- }
- itf = itf->next;
- }
-
- ptr->resourceProperties = (OCResourceProperty) repPayload->tags->bitmap;
-
- OCFreeCollectionResource(repPayload);
- *payload = ptr;
- return OC_STACK_OK;
- }
- else
- {
- OIC_LOG_V(ERROR, TAG, "The resource type or interface type doe not exist \
- on the resource directory");
- }
- return OC_STACK_ERROR;
-}
-#endif
-
static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
{
if (!request || !resource)
bool foundResourceAtRD = false;
for (;resource && discoveryResult == OC_STACK_OK; resource = resource->next)
{
-#ifdef WITH_RD
- if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0)
- {
- OCResource *resource1 = NULL;
- OCDevAddr devAddr;
- discoveryResult = checkResourceExistsAtRD(interfaceQuery,
- resourceTypeQuery, &resource1, &devAddr);
- if (discoveryResult != OC_STACK_OK)
- {
- break;
- }
- discoveryResult = BuildVirtualResourceResponse(resource1,
- discPayload, &devAddr, true);
- if (payload)
- {
- discPayload->baseURI = OICStrdup(devAddr.addr);
- }
- OICFree(resource1->uri);
- for (OCResourceType *rsrcRt = resource1->rsrcType, *rsrcRtNext = NULL; rsrcRt; )
- {
- rsrcRtNext = rsrcRt->next;
- OICFree(rsrcRt->resourcetypename);
- OICFree(rsrcRt);
- rsrcRt = rsrcRtNext;
- }
-
- for (OCResourceInterface *rsrcPtr = resource1->rsrcInterface, *rsrcNext = NULL; rsrcPtr; )
- {
- rsrcNext = rsrcPtr->next;
- OICFree(rsrcPtr->name);
- OICFree(rsrcPtr);
- rsrcPtr = rsrcNext;
- }
- foundResourceAtRD = true;
- }
-#endif
if (!foundResourceAtRD && includeThisResourceInResponse(resource, interfaceQuery, resourceTypeQuery))
{
discoveryResult = BuildVirtualResourceResponse(resource,
{
OIC_LOG(INFO, TAG, "Removed observer successfully");
request->observeResult = OC_STACK_OK;
+ // There should be no observe option header for de-registration response.
+ // Set as an invalid value here so we can detect it later and remove the field in response.
+ request->observationOption = MAX_SEQUENCE_NUMBER + 1;
}
else
{
#include "ocstack.h"
#include "ocserverrequest.h"
#include "ocresourcehandler.h"
+#include "ocobserve.h"
#include "oic_malloc.h"
#include "oic_string.h"
#include "ocpayload.h"
memcpy(responseInfo.info.token, serverRequest->requestToken, serverRequest->tokenLength);
responseInfo.info.tokenLength = serverRequest->tokenLength;
- // De-register observe option should not be included in the response header
- if((serverRequest->observeResult == OC_STACK_OK) && (serverRequest->observationOption != OC_OBSERVE_DEREGISTER))
+ if((serverRequest->observeResult == OC_STACK_OK)&&
+ (serverRequest->observationOption != MAX_SEQUENCE_NUMBER + 1))
{
responseInfo.info.numOptions = ehResponse->numSendVendorSpecificHeaderOptions + 1;
}
OCClientResponse response =
{.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
- response.sequenceNumber = -1;
+ response.sequenceNumber = MAX_SEQUENCE_NUMBER + 1;
CopyEndpointToDevAddr(endPoint, &response.devAddr);
FixUpClientResponse(&response);
response.resourceUri = responseInfo->info.resourceUri;
if (cbNode->method == OC_REST_OBSERVE &&
response.sequenceNumber > OC_OFFSET_SEQUENCE_NUMBER &&
+ cbNode->sequenceNumber <= MAX_SEQUENCE_NUMBER &&
response.sequenceNumber <= cbNode->sequenceNumber)
{
OIC_LOG_V(INFO, TAG, "Received stale notification. Number :%d",
return false;
}
}
+
+#if defined(RD_CLIENT) || defined(RD_SERVER)
+OCStackResult OCBindResourceInsToResource(OCResourceHandle handle, uint8_t ins)
+{
+ VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
+
+ OCResource *resource = NULL;
+
+ resource = findResource((OCResource *) handle);
+ if (!resource)
+ {
+ OIC_LOG(ERROR, TAG, "Resource not found");
+ return OC_STACK_ERROR;
+ }
+
+ resource->ins = ins;
+
+ return OC_STACK_OK;
+}
+
+OCResourceHandle OCGetResourceHandleAtUri(const char *uri)
+{
+ if (!uri)
+ {
+ OIC_LOG(ERROR, TAG, "Resource uri is NULL");
+ return NULL;
+ }
+
+ OCResource *pointer = headResource;
+
+ while (pointer)
+ {
+ if (strncmp(uri, pointer->uri, MAX_URI_LENGTH) == 0)
+ {
+ OIC_LOG_V(DEBUG, TAG, "Found Resource %s", uri);
+ return pointer;
+ }
+ pointer = pointer->next;
+ }
+ return NULL;
+}
+
+OCStackResult OCGetResourceIns(OCResourceHandle handle, uint8_t *ins)
+{
+ OCResource *resource = NULL;
+
+ VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
+ VERIFY_NON_NULL(ins, ERROR, OC_STACK_INVALID_PARAM);
+
+ resource = findResource((OCResource *) handle);
+ if (resource)
+ {
+ *ins = resource->ins;
+ return OC_STACK_OK;
+ }
+ return OC_STACK_ERROR;
+}
+#endif
--- /dev/null
+//******************************************************************
+//
+// Copyright 2016 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// 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 "oicresourcedirectory.h"
+
+#include "rdpayload.h"
+#include "oic_malloc.h"
+#include "oic_string.h"
+#include "octypes.h"
+#include "ocstack.h"
+#include "ocpayload.h"
+#include "rdpayload.h"
+#include "ocresource.h"
+#include "payload_logging.h"
+
+#define TAG "OIC_RI_RESOURCE_DIRECTORY"
+
+#ifdef RD_CLIENT
+OCStackResult OCRDPublish(const char *host, OCConnectivityType connectivityType,
+ OCResourceHandle resourceHandles[], uint8_t nHandles,
+ OCCallbackData *cbData, OCQualityOfService qos)
+{
+ // Validate input parameters
+ if (!host || !cbData || !cbData->cb)
+ {
+ return OC_STACK_INVALID_CALLBACK;
+ }
+
+ OCResourceHandle *pubResHandle = resourceHandles;
+ uint8_t nPubResHandles = nHandles;
+
+ // if resource handles is null, "/oic/p" and "/oic/d" resource will be published to RD.
+ if (!pubResHandle && !nPubResHandles)
+ {
+ OCResourceHandle defaultResHandles[OIC_RD_DEFAULT_RESOURCE] = { 0 };
+
+ // get "/oic/d" resource handle from stack.
+ defaultResHandles[0] = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI);
+ // get "/oic/p" resource handle from stack.
+ defaultResHandles[1] = OCGetResourceHandleAtUri(OC_RSRVD_PLATFORM_URI);
+
+ pubResHandle = defaultResHandles;
+ nPubResHandles = OIC_RD_DEFAULT_RESOURCE;
+ }
+
+ char targetUri[MAX_URI_LENGTH] = { 0 };
+ snprintf(targetUri, MAX_URI_LENGTH, "%s%s?rt=%s", host,
+ OC_RSRVD_RD_URI, OC_RSRVD_RESOURCE_TYPE_RDPUBLISH);
+ OIC_LOG_V(DEBUG, TAG, "Target URI: %s", targetUri);
+
+ OCPayload *rdPayload = (OCPayload *) OCRDPublishPayloadCreate(pubResHandle, nPubResHandles,
+ OIC_RD_PUBLISH_TTL);
+ if (!rdPayload)
+ {
+ OIC_LOG_V(ERROR, TAG, "Failed to create RD Payload");
+ return OC_STACK_NO_MEMORY;
+ }
+
+ OIC_LOG(DEBUG, TAG, "Create RD payload successfully");
+
+ return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)rdPayload,
+ connectivityType, qos, cbData, NULL, 0);
+}
+
+OCStackResult OCRDDelete(const char *host, OCConnectivityType connectivityType,
+ OCResourceHandle resourceHandles[], uint8_t nHandles,
+ OCCallbackData *cbData, OCQualityOfService qos)
+{
+ // Validate input parameters
+ if (!host || !cbData || !cbData->cb)
+ {
+ return OC_STACK_INVALID_CALLBACK;
+ }
+
+ const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString();
+
+ char targetUri[MAX_URI_LENGTH] = { 0 };
+ snprintf(targetUri, MAX_URI_LENGTH, "%s%s?di=%s", host, OC_RSRVD_RD_URI, id);
+
+ char queryParam[MAX_URI_LENGTH] = { 0 };
+ for (uint8_t j = 0; j < nHandles; j++)
+ {
+ OCResource *handle = (OCResource *) resourceHandles[j];
+ snprintf(queryParam, MAX_URI_LENGTH, "&ins=%d", handle->ins);
+ }
+
+ OICStrcatPartial(targetUri, sizeof(targetUri), queryParam, strlen(queryParam));
+ OIC_LOG_V(DEBUG, TAG, "Target URI: %s", targetUri);
+
+ return OCDoResource(NULL, OC_REST_DELETE, targetUri, NULL, NULL, connectivityType,
+ qos, cbData, NULL, 0);
+}
+#endif
#define CBOR_ROOT_ARRAY_LENGTH 1
static int64_t OCTagsPayloadToCbor(OCTagsPayload *tags, CborEncoder *setMap);
-static int64_t OCLinksPayloadToCbor(OCLinksPayload *rtPtr, CborEncoder *setMap);
+static int64_t OCLinksPayloadToCbor(OCLinksPayload *links, CborEncoder *setMap);
static int64_t ConditionalAddTextStringToMap(CborEncoder* map, const char* key, const char *value);
static int64_t ConditionalAddIntToMap(CborEncoder *map, const char *tags, const uint64_t *value);
static int64_t AddStringLLToMap(CborEncoder *map, const char *tag, const OCStringLL *value);
static CborError FindStringInMap(const CborValue *map, const char *tags, char **value);
static CborError FindIntInMap(const CborValue *map, const char *tags, uint64_t *value);
static CborError FindStringLLInMap(const CborValue *linksMap, const char *tag, OCStringLL **links);
+static OCStackResult CreateStringLL(uint8_t numElements, OCResourceHandle handle,
+ const char* (*getValue)(OCResourceHandle handle, uint8_t i),
+ OCStringLL **stringLL);
int64_t OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t *outPayload, size_t *size)
{
- int64_t cborEncoderResult = CborErrorIO;
+ int64_t cborEncoderResult = CborNoError;
int flags = 0;
CborEncoder encoder;
VERIFY_PARAM_NON_NULL(TAG, rdPayload, "Invalid input parameter rdPayload");
}
else if (rdPayload->rdPublish)
{
- CborEncoder colArray;
- cborEncoderResult |= cbor_encoder_create_array(&encoder, &colArray, CborIndefiniteLength);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create collection array");
+ CborEncoder collMap;
+ cborEncoderResult |= cbor_encoder_create_map(&encoder, &collMap, CborIndefiniteLength);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create collection map");
OCResourceCollectionPayload *rdPublish = rdPayload->rdPublish;
- while (rdPublish)
- {
- cborEncoderResult |= OCTagsPayloadToCbor(rdPublish->tags, &colArray);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding tags payload");
- cborEncoderResult |= OCLinksPayloadToCbor(rdPublish->setLinks, &colArray);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding setLinks payload");
- rdPublish = rdPublish->next;
- }
- cborEncoderResult |= cbor_encoder_close_container(&encoder, &colArray);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing collection array");
+ cborEncoderResult |= OCTagsPayloadToCbor(rdPublish->tags, &collMap);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding tags payload");
+ cborEncoderResult |= OCLinksPayloadToCbor(rdPublish->setLinks, &collMap);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding setLinks payload");
+
+ cborEncoderResult |= cbor_encoder_close_container(&encoder, &collMap);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing collection map");
}
else
{
static int64_t OCTagsPayloadToCbor(OCTagsPayload *tags, CborEncoder *setMap)
{
- CborEncoder tagsMap;
int64_t cborEncoderResult = CborNoError;
- cborEncoderResult |= cbor_encoder_create_map(setMap, &tagsMap, CborIndefiniteLength);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create tags map");
- cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DEVICE_NAME, tags->n.deviceName);
+ cborEncoderResult |= ConditionalAddTextStringToMap(setMap, OC_RSRVD_DEVICE_NAME,
+ tags->n.deviceName);
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_DEVICE_NAME in tags map");
- cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DEVICE_ID,
- (char *)tags->di.id);
+ cborEncoderResult |= ConditionalAddTextStringToMap(setMap, OC_RSRVD_DEVICE_ID,
+ (char *)tags->di.id);
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_DEVICE_ID in tags map");
- cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_RTS, tags->rts);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_RTS in tags map");
-
- cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DREL, tags->drel);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_DREL in tags map");
-
- cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_BASE_URI, tags->baseURI);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_BASE_URI in tags map");
-
- {
- uint64_t value = tags->bitmap;
- cborEncoderResult |= ConditionalAddIntToMap(&tagsMap, OC_RSRVD_BITMAP, &value);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_BITMAP in tags map");
-
- value = tags->port;
- cborEncoderResult |= ConditionalAddIntToMap(&tagsMap, OC_RSRVD_HOSTING_PORT, &value);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_HOSTING_PORT in tags map");
-
- value = tags->ins;
- cborEncoderResult |= ConditionalAddIntToMap(&tagsMap, OC_RSRVD_INS, &value);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_INS in tags map");
-
- value = tags->ttl;
- cborEncoderResult |= ConditionalAddIntToMap(&tagsMap, OC_RSRVD_TTL, &value);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_TTL in tags map");
- }
-
- cborEncoderResult |= cbor_encoder_close_container(setMap, &tagsMap);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to close container");
-
+ cborEncoderResult |= ConditionalAddIntToMap(setMap, OC_RSRVD_DEVICE_TTL, &tags->ttl);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_TTL in tags map");
exit:
return cborEncoderResult;
}
-static int64_t OCLinksPayloadToCbor(OCLinksPayload *rtPtr, CborEncoder *setMap)
+static int64_t OCLinksPayloadToCbor(OCLinksPayload *links, CborEncoder *setMap)
{
CborEncoder linksArray;
int64_t cborEncoderResult = CborNoError;
+ cborEncoderResult |= cbor_encode_text_string(setMap, OC_RSRVD_LINKS,
+ sizeof(OC_RSRVD_LINKS) - 1);
+
cborEncoderResult |= cbor_encoder_create_array(setMap, &linksArray, CborIndefiniteLength);
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create Links array");
- while (rtPtr)
+ while (links)
{
CborEncoder linksMap;
cborEncoderResult |= cbor_encoder_create_map(&linksArray, &linksMap, CborIndefiniteLength);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create Links map");
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create links map");
- cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_HREF, rtPtr->href);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_HREF in Links map");
+ cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_HREF, links->href);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_HREF in links map");
- cborEncoderResult|= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_REL, rtPtr->rel);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_REL in Links map");
+ cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_REL, links->rel);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_REL in links map");
- cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_TITLE, rtPtr->title);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_TITLE in Links map");
+ cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_RESOURCE_TYPE, links->rt);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_RT in links map");
- cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_URI, rtPtr->uri);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_URI in Links map");
+ cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_INTERFACE, links->itf);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_ITF in links map");
- cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_RESOURCE_TYPE, rtPtr->rt);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_RT in Links map");
+ // Policy
+ CborEncoder policyMap;
+ cborEncoderResult |= cbor_encode_text_string(&linksMap, OC_RSRVD_POLICY,
+ sizeof(OC_RSRVD_POLICY) - 1);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding policy tag to links map");
+ cborEncoderResult |= cbor_encoder_create_map(&linksMap, &policyMap, CborIndefiniteLength);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding policy map to links map");
- cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_INTERFACE, rtPtr->itf);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_ITF in Links map");
+ // Bitmap
+ cborEncoderResult |= cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP,
+ sizeof(OC_RSRVD_BITMAP) - 1);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding bitmap tag to policy map");
+ cborEncoderResult |= cbor_encode_uint(&policyMap, links->p);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding bitmap value to policy map");
- cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_MEDIA_TYPE, rtPtr->mt);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_MT in Links map");
+ cborEncoderResult |= cbor_encoder_close_container(&linksMap, &policyMap);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing policy map");
- {
- uint64_t value = rtPtr->ins;
- cborEncoderResult |= ConditionalAddIntToMap(&linksMap, OC_RSRVD_INS, &value);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_INS in Links map");
- }
+ cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_TITLE, links->title);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_TITLE in links map");
+
+ cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_URI, links->anchor);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_URI in links map");
+ cborEncoderResult |= ConditionalAddIntToMap(&linksMap, OC_RSRVD_INS, (uint64_t *) &links->ins);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_INS in links map");
+
+ cborEncoderResult |= ConditionalAddIntToMap(&linksMap, OC_RSRVD_TTL, &links->ttl);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_TTL in links map");
+
+ cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_MEDIA_TYPE, links->type);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_MT in links map");
+
+ // Finsihed encoding a resource, close the map.
cborEncoderResult |= cbor_encoder_close_container(&linksArray, &linksMap);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing Links map");
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing links map");
- rtPtr = rtPtr->next;
+ links = links->next;
}
cborEncoderResult |= cbor_encoder_close_container(setMap, &linksArray);
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing links array");
ret = OC_STACK_MALFORMED_RESPONSE;
- if (cbor_value_is_array(rdCBORPayload))
+ if (cbor_value_is_map(rdCBORPayload))
{
- OCLinksPayload *linksPayload = NULL;
+ // rdCBORPayload is already inside the main root map.
OCTagsPayload *tagsPayload = NULL;
+ OCLinksPayload *linksPayload = NULL;
+
+ cborFindResult = OCTagsCborToPayload(rdCBORPayload, &tagsPayload);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed parsing tags payload.");
+
+ cborFindResult = OCLinksCborToPayload(rdCBORPayload, &linksPayload);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed parsing links payload.");
+
+ // Move from tags payload to links array.
+ cborFindResult = cbor_value_advance(rdCBORPayload);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed advancing rdCborPayload.");
- while (cbor_value_is_container(rdCBORPayload))
- {
- // enter tags map
- CborValue tags;
- cborFindResult = cbor_value_enter_container(rdCBORPayload, &tags);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed entering tags container.");
-
- cborFindResult= OCTagsCborToPayload(&tags, &tagsPayload);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed parsing tags payload.");
- OCTagsLog(DEBUG, tagsPayload);
-
- cborFindResult = OCLinksCborToPayload(&tags, &linksPayload);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed parsing links payload.");
- OCLinksLog(DEBUG, linksPayload);
-
- // Move from tags payload to links array.
- cborFindResult = cbor_value_advance(rdCBORPayload);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed advancing rdCborPayload.");
- }
rdPayload->rdPublish = OCCopyCollectionResource(tagsPayload, linksPayload);
VERIFY_PARAM_NON_NULL(TAG, rdPayload->rdPublish, "Failed allocating rdPayload->rdPublish");
}
+ // TODO: This logic needs to be modified to check the payload type exactly..
else if (cbor_value_is_map(rdCBORPayload))
{
rdPayload->rdDiscovery = (OCRDDiscoveryPayload *)OICCalloc(1, sizeof(OCRDDiscoveryPayload));
VERIFY_PARAM_NON_NULL(TAG, rdPayload->rdDiscovery, "Failed allocating discoveryPayload");
cborFindResult = FindStringInMap(rdCBORPayload, OC_RSRVD_DEVICE_NAME,
- &rdPayload->rdDiscovery->n.deviceName);
+ &rdPayload->rdDiscovery->n.deviceName);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding OC_RSRVD_DEVICE_NAME.");
char *deviceId = NULL;
cborFindResult = FindStringInMap(rdCBORPayload, OC_RSRVD_DEVICE_ID, &deviceId);
VERIFY_PARAM_NON_NULL(TAG, llPtr, "Failed allocating OCStringLL");
*links = llPtr;
}
- else if(llPtr)
+ else if (llPtr)
{
while (llPtr->next)
{
llPtr = llPtr->next;
}
llPtr->next = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
- VERIFY_PARAM_NON_NULL(TAG, llPtr->next, "Failed allocating OCStringLL->next");
+ llPtr = llPtr->next;
+ VERIFY_PARAM_NON_NULL(TAG, llPtr, "Failed allocating OCStringLL->next");
}
cborFindResult = cbor_value_dup_text_string(&rtVal, &(llPtr->value), &len, NULL);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed duplicating value");
OCTagsPayload *tags = (OCTagsPayload *)OICCalloc(1, sizeof(OCTagsPayload));
VERIFY_PARAM_NON_NULL(TAG, tags, "Failed allocating tags");
- if (cbor_value_is_map(tagsMap))
+ cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_DEVICE_NAME, &tags->n.deviceName);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding deviceName");
+
{
- cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_DEVICE_NAME, &tags->n.deviceName);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding deviceName");
- cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_DREL, &tags->drel);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding drel");
- cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_RTS, &tags->rts);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding rts");
- cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_BASE_URI, &tags->baseURI);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding baseURI");
char *deviceId = NULL;
cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_DEVICE_ID, &deviceId);
if (deviceId)
OICFree(deviceId);
}
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding deviceId");
- {
- uint64_t value = 0;
- cborFindResult = FindIntInMap(tagsMap, OC_RSRVD_HOSTING_PORT, &value);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding port");
- tags->port = value;
- value = 0;
- cborFindResult = FindIntInMap(tagsMap, OC_RSRVD_BITMAP, &value);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding bitmap");
- tags->bitmap = value;
- value = 0;
- cborFindResult = FindIntInMap(tagsMap, OC_RSRVD_INS, &value);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding ins");
- tags->ins = value;
- value = 0;
- cborFindResult = FindIntInMap(tagsMap, OC_RSRVD_TTL, &value);
- tags->ttl = value;
- }
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding ttl");
- cborFindResult = cbor_value_advance(tagsMap);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed advancing bitmap");
}
+
+ cborFindResult = FindIntInMap(tagsMap, OC_RSRVD_DEVICE_TTL, &tags->ttl);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding ttl");
+
*tagsPayload = tags;
return cborFindResult;
return cborFindResult;
}
-static CborError OCLinksCborToPayload(CborValue *linksArray, OCLinksPayload **linksPayload)
+static CborError OCLinksCborToPayload(CborValue *links, OCLinksPayload **linksPayload)
{
- CborValue linksMap;
OCLinksPayload *setLinks = NULL;
- CborError cborFindResult = cbor_value_enter_container(linksArray, &linksMap);
+ CborValue linksMap;
+ CborValue linksArray;
+ CborError cborFindResult = CborErrorOutOfMemory;
+
+ cborFindResult = cbor_value_map_find_value(links, OC_RSRVD_LINKS, &linksArray);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding links array");
+
+ cborFindResult = cbor_value_enter_container(&linksArray, &linksMap);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed entering links map container");
while (cbor_value_is_map(&linksMap))
cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_REL, &setLinks->rel);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding rel value");
+ cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_RESOURCE_TYPE, &setLinks->rt);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding rt value");
+
+ cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_INTERFACE, &setLinks->itf);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding itf value");
+
+ // Policy
+ CborValue policyMap;
+ cborFindResult = cbor_value_map_find_value(&linksMap, OC_RSRVD_POLICY, &policyMap);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "to find policy tag");
+
+ // Bitmap
+ cborFindResult = FindIntInMap(&policyMap, OC_RSRVD_BITMAP, (uint64_t *) &setLinks->p);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding bitmap value");
+
cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_TITLE, &setLinks->title);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding title value");
- cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_URI, &setLinks->uri);
+ cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_URI, &setLinks->anchor);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding uri value");
- cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_RESOURCE_TYPE, &setLinks->rt);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding rt value");
+ cborFindResult = FindIntInMap(&linksMap, OC_RSRVD_INS, (uint64_t *) &setLinks->ins);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding ins value");
- cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_INTERFACE, &setLinks->itf);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding itf value");
+ cborFindResult = FindIntInMap(&linksMap, OC_RSRVD_TTL, &setLinks->ttl);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding ttl");
- cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_MEDIA_TYPE, &setLinks->mt);
+ cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_MEDIA_TYPE, &setLinks->type);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding mt value");
- {
- uint64_t value = 0;
- cborFindResult = FindIntInMap(&linksMap, OC_RSRVD_INS, &value);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding ins value");
- setLinks->ins = value;
- }
-
if (!*linksPayload)
{
*linksPayload = setLinks;
{
CborEncoder array;
int64_t cborEncoderResult = CborNoError;
+
cborEncoderResult |= cbor_encode_text_string(map, tag, strlen(tag));
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed encoding string tag name");
cborEncoderResult |= cbor_encoder_create_array(map, &array, CborIndefiniteLength);
return rdPayload;
}
+#ifdef RD_CLIENT
+OCRDPayload *OCRDPublishPayloadCreate(OCResourceHandle resourceHandles[], uint8_t nHandles,
+ uint64_t ttl)
+{
+ OCTagsPayload *tagsPayload = NULL;
+ OCLinksPayload *linksPayload = NULL;
+ OCStringLL *rt = NULL;
+ OCStringLL *itf = NULL;
+ OCStringLL *mt = NULL;
+ OCResourceProperty p = OC_RES_PROP_NONE;
+ uint8_t ins = 0;
+
+ OCRDPayload *rdPayload = OCRDPayloadCreate();
+ if (!rdPayload)
+ {
+ return NULL;
+ }
+
+ const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString();
+ tagsPayload = OCCopyTagsResources(NULL, id, ttl);
+ if (!tagsPayload)
+ {
+ goto exit;
+ }
+
+ for (uint8_t j = 0; j < nHandles; j++)
+ {
+ OCResourceHandle handle = resourceHandles[j];
+ if (handle)
+ {
+ rt = NULL;
+ itf = NULL;
+ mt = NULL;
+ ins = 0;
+ const char *uri = OCGetResourceUri(handle);
+ uint8_t numElement = 0;
+ if (OC_STACK_OK == OCGetNumberOfResourceTypes(handle, &numElement))
+ {
+ OCStackResult res = CreateStringLL(numElement, handle, OCGetResourceTypeName, &rt);
+ if (OC_STACK_OK != res || !rt)
+ {
+ goto exit;
+ }
+ }
+
+ if (OC_STACK_OK == OCGetNumberOfResourceInterfaces(handle, &numElement))
+ {
+ OCStackResult res = CreateStringLL(numElement, handle, OCGetResourceInterfaceName,
+ &itf);
+ if (OC_STACK_OK != res || !itf)
+ {
+ goto exit;
+ }
+ }
+
+ p = OCGetResourceProperties(handle);
+ p = (OCResourceProperty) ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE));
+
+ OCStackResult res = OCGetResourceIns(handle, &ins);
+ if (OC_STACK_OK != res)
+ {
+ goto exit;
+ }
+
+ mt = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
+ if (!mt)
+ {
+ goto exit;
+ }
+ mt->value = OICStrdup(DEFAULT_MESSAGE_TYPE);
+ if (!mt->value)
+ {
+ goto exit;
+ }
+
+ if (!linksPayload)
+ {
+ linksPayload = OCCopyLinksResources(uri, NULL, rt, itf, p, NULL,
+ NULL, ins, ttl, mt);;
+ if (!linksPayload)
+ {
+ goto exit;
+ }
+ }
+ else
+ {
+ OCLinksPayload *temp = linksPayload;
+ while (temp->next)
+ {
+ temp = temp->next;
+ }
+ temp->next = OCCopyLinksResources(uri, NULL, rt, itf, p, NULL,
+ NULL, ins, ttl, mt);
+ if (!temp->next)
+ {
+ goto exit;
+ }
+ }
+ OCFreeOCStringLL(rt);
+ OCFreeOCStringLL(itf);
+ OCFreeOCStringLL(mt);
+ }
+ }
+
+ rdPayload->rdPublish = OCCopyCollectionResource(tagsPayload, linksPayload);
+ if (!rdPayload->rdPublish)
+ {
+ goto exit;
+ }
+
+ return rdPayload;
+
+exit:
+ if (rt)
+ {
+ OCFreeOCStringLL(rt);
+ }
+ if (itf)
+ {
+ OCFreeOCStringLL(itf);
+ }
+ if (mt)
+ {
+ OCFreeOCStringLL(mt);
+ }
+ if (tagsPayload)
+ {
+ OCFreeTagsResource(tagsPayload);
+ }
+ if (linksPayload)
+ {
+ OCFreeLinksResource(linksPayload);
+ }
+ OCRDPayloadDestroy(rdPayload);
+ return NULL;
+}
+#endif
OCRDDiscoveryPayload *OCRDDiscoveryPayloadCreate(const char *deviceName, const char *id, int biasFactor)
{
OCRDDiscoveryPayload *discoveryPayload = (OCRDDiscoveryPayload *)OICCalloc(1, sizeof(OCRDDiscoveryPayload));
if (payload->rdPublish)
{
- for (OCResourceCollectionPayload *col = payload->rdPublish; col; )
+ OCResourceCollectionPayload *col = payload->rdPublish;
+
+ if (col->setLinks)
{
- if (col->setLinks)
- {
- OCFreeLinksResource(col->setLinks);
- }
+ OCFreeLinksResource(col->setLinks);
+ }
- if (col->tags)
- {
- OCFreeTagsResource(col->tags);
- }
- OCResourceCollectionPayload *temp = col->next;
- OICFree(col);
- col = temp;
+ if (col->tags)
+ {
+ OCFreeTagsResource(col->tags);
}
+ OICFree(col);
}
OICFree(payload);
}
-OCTagsPayload* OCCopyTagsResources(const char *deviceName, const unsigned char *id, const char *baseURI,
- uint8_t bitmap, uint16_t port, uint8_t ins, const char *rts,const char *drel, uint32_t ttl)
+OCTagsPayload* OCCopyTagsResources(const char *deviceName, const unsigned char *id, uint64_t ttl)
{
OCTagsPayload *tags = (OCTagsPayload *)OICCalloc(1, sizeof(OCTagsPayload));
if (!tags)
{
return NULL;
}
- if (deviceName)
+ if (deviceName)
{
tags->n.deviceName = OICStrdup(deviceName);
if (!tags->n.deviceName)
{
OICStrcpy((char*)tags->di.id, MAX_IDENTITY_SIZE, (char *)id);
}
- if (baseURI)
- {
- tags->baseURI = OICStrdup(baseURI);
- if (!tags->baseURI)
- {
- goto memory_allocation_failed;
- }
- }
- tags->bitmap = bitmap;
- tags->port = port;
- tags->ins = ins;
- if (rts)
- {
- tags->rts = OICStrdup(rts);
- if (!tags->rts)
- {
- goto memory_allocation_failed;
- }
- }
- if (drel)
- {
- tags->drel = OICStrdup(drel);
- if (!tags->drel)
- {
- goto memory_allocation_failed;
- }
- }
tags->ttl = ttl;
return tags;
return NULL;
}
-OCLinksPayload* OCCopyLinksResources(const char *href, OCStringLL *rt, OCStringLL *itf,
- const char *rel, bool obs, const char *title, const char *uri, uint8_t ins, OCStringLL *mt)
+OCLinksPayload* OCCopyLinksResources(const char *href, const char *rel, OCStringLL *rt,
+ OCStringLL *itf, uint8_t p, const char *title,
+ const char *anchor, uint8_t ins, uint64_t ttl,
+ OCStringLL *mt)
{
OCLinksPayload *links = (OCLinksPayload *)OICCalloc(1, sizeof(OCLinksPayload));
if (!links)
goto memory_allocation_failed;
}
}
+ if (rel)
+ {
+ links->rel = OICStrdup(rel);
+ if (!links->rel)
+ {
+ goto memory_allocation_failed;
+ }
+ }
if (rt)
{
links->rt = CloneOCStringLL(rt);
goto memory_allocation_failed;
}
}
- if (rel)
- {
- links->rel = OICStrdup(rel);
- if (!links->rel)
- {
- goto memory_allocation_failed;
- }
- }
- links->obs = obs;
+ links->p = p;
if (title)
{
links->title = OICStrdup(title);
goto memory_allocation_failed;
}
}
- if (uri)
+ if (anchor)
{
- links->uri = OICStrdup(uri);
- if (!links->uri)
+ links->anchor = OICStrdup(anchor);
+ if (!links->anchor)
{
goto memory_allocation_failed;
}
}
links->ins = ins;
+ links->ttl = ttl;
if (mt)
{
- links->mt = CloneOCStringLL(mt);
- if (!links->mt)
+ links->type = CloneOCStringLL(mt);
+ if (!links->type)
{
goto memory_allocation_failed;
}
return pl;
}
-void OCFreeLinksResource(OCLinksPayload *payload)
+void OCFreeLinksResource(OCLinksPayload *links)
{
- if (!payload)
+ if (!links)
{
return;
}
- OICFree(payload->href);
- OCFreeOCStringLL(payload->rt);
- OCFreeOCStringLL(payload->itf);
- OICFree(payload->rel);
- OICFree(payload->title);
- OICFree(payload->uri);
- OCFreeOCStringLL(payload->mt);
- OCFreeLinksResource(payload->next);
- OICFree(payload);
+ OICFree(links->href);
+ OICFree(links->rel);
+ OCFreeOCStringLL(links->rt);
+ OCFreeOCStringLL(links->itf);
+ OICFree(links->title);
+ OICFree(links->anchor);
+ OCFreeOCStringLL(links->type);
+ OCFreeLinksResource(links->next);
+ OICFree(links);
}
void OCFreeTagsResource(OCTagsPayload *payload)
{
return;
}
- OICFree(payload->n.deviceName);
- OICFree(payload->baseURI);
- OICFree(payload->rts);
- OICFree(payload->drel);
+ OICFree(payload->n.deviceName);;
OICFree(payload);
}
{
OCFreeLinksResource(payload->setLinks);
}
- OCFreeCollectionResource(payload->next);
OICFree(payload);
}
-void OCTagsLog(const LogLevel level, const OCTagsPayload *tags)
+static OCStackResult CreateStringLL(uint8_t numElements, OCResourceHandle handle,
+ const char* (*getValue)(OCResourceHandle handle, uint8_t i),
+ OCStringLL **stringLL)
{
- if (tags)
+ for (uint8_t i = 0; i < numElements; ++i)
{
- if (tags->n.deviceName)
- {
- OIC_LOG_V(level, TAG, " Device Name : %s ",tags->n.deviceName);
- }
- if (tags->baseURI)
+ const char *value = getValue(handle, i);
+ OIC_LOG_V(ERROR, TAG, "value: %s", value);
+ if (!*stringLL)
{
- OIC_LOG_V(level, TAG, " Base URI : %s ",tags->baseURI);
- }
- OIC_LOG_V(level, TAG, " Device ID : %s ",tags->di.id);
- OIC_LOG_V(level, TAG, " Bitmap : %d ",tags->bitmap);
- OIC_LOG_V(level, TAG, " Port : %d ",tags->port);
- OIC_LOG_V(level, TAG, " Ins : %d ",tags->ins);
- OIC_LOG_V(level, TAG, " Ttl : %d ",tags->ttl);
-
- if (tags->rts)
- {
- OIC_LOG_V(level, TAG, " RTS : %s ",tags->rts);
- }
- if (tags->drel)
- {
- OIC_LOG_V(level, TAG, " DREL : %s ",tags->drel);
- }
- }
- else
- {
- (void) level;
- }
-}
-
-void OCLinksLog(const LogLevel level, const OCLinksPayload *links)
-{
- while (links)
- {
- if (links->href)
- {
- OIC_LOG_V(level, TAG, " href: %s ",links->href);
- }
- OIC_LOG(level, TAG, " RT: ");
- OCStringLL *rt = links->rt;
- while (rt)
- {
- if (rt->value)
+ *stringLL = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
+ if (!*stringLL)
{
- OIC_LOG_V(level, TAG, " %s", rt->value);
+ OIC_LOG(ERROR, TAG, "Failed allocating memory.");
+ return OC_STACK_NO_MEMORY;
}
- rt = rt->next;
- }
- OIC_LOG(level, TAG, " IF: ");
- OCStringLL *itf = links->itf;
- while (itf)
- {
- if (itf->value)
+ (*stringLL)->value = OICStrdup(value);
+ if (!(*stringLL)->value)
{
- OIC_LOG_V(level, TAG, " %s", itf->value);
+ OIC_LOG(ERROR, TAG, "Failed copying to OCStringLL.");
+ return OC_STACK_NO_MEMORY;
}
- itf = itf->next;
}
- OIC_LOG(level, TAG, " MT: ");
- OCStringLL *mt = links->mt;
- while (mt)
+ else
{
- if (mt->value)
+ OCStringLL *cur = *stringLL;
+ while (cur->next)
{
- OIC_LOG_V(level, TAG, " %s", mt->value);
+ cur = cur->next;
+ }
+ cur->next = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
+ if (!cur->next)
+ {
+ OIC_LOG(ERROR, TAG, "Failed allocating memory.");
+ return OC_STACK_NO_MEMORY;
+ }
+ cur->next->value = OICStrdup(value);
+ if (!cur->next->value)
+ {
+ OIC_LOG(ERROR, TAG, "Failed copying to OCStringLL.");
+ return OC_STACK_NO_MEMORY;
}
- mt = mt->next;
- }
- OIC_LOG_V(level, TAG, " INS: %d", links->ins);
- OIC_LOG_V(level, TAG, " OBS: %d", links->obs);
- if (links->rel)
- {
- OIC_LOG_V(level, TAG, " REL: %s", links->rel);
- }
- if (links->title)
- {
- OIC_LOG_V(level, TAG, " TITLE: %s", links->title);
- }
- if (links->uri)
- {
- OIC_LOG_V(level, TAG, " URI: %s", links->uri);
}
- links = links->next;
- }
- if (!links)
- {
- (void) level;
}
+ return OC_STACK_OK;
}
--- /dev/null
+//******************************************************************
+//
+// Copyright 2016 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// 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.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+extern "C"
+{
+ #include "rdpayload.h"
+ #include "oicresourcedirectory.h"
+ #include "ocstack.h"
+ #include "ocstackinternal.h"
+ #include "logger.h"
+ #include "oic_malloc.h"
+}
+
+#include "gtest/gtest.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+//-----------------------------------------------------------------------------
+// Includes
+//-----------------------------------------------------------------------------
+#include <stdio.h>
+#include <string.h>
+
+#include <iostream>
+#include <stdint.h>
+
+#include "gtest_helper.h"
+
+using namespace std;
+
+namespace itst = iotivity::test;
+
+#define DEFAULT_CONTEXT_VALUE 0x99
+
+//-----------------------------------------------------------------------------
+// Private variables
+//-----------------------------------------------------------------------------
+static const char TAG[] = "RDTests";
+
+std::chrono::seconds const SHORT_TEST_TIMEOUT = std::chrono::seconds(5);
+
+//-----------------------------------------------------------------------------
+// Callback functions
+//-----------------------------------------------------------------------------
+static OCStackApplicationResult handlePublishCB(__attribute__((unused))void *ctx,
+ __attribute__((unused)) OCDoHandle handle,
+ __attribute__((unused))
+ OCClientResponse *clientResponse)
+{
+ OIC_LOG(DEBUG, TAG, "Successfully published resources.");
+
+ return OC_STACK_DELETE_TRANSACTION;
+}
+
+static OCStackApplicationResult handleDeleteCB(__attribute__((unused))void *ctx,
+ __attribute__((unused)) OCDoHandle handle,
+ __attribute__((unused))
+ OCClientResponse *clientResponse)
+{
+ OIC_LOG(DEBUG, TAG, "Successfully delete resources.");
+
+ return OC_STACK_DELETE_TRANSACTION;
+}
+
+//-----------------------------------------------------------------------------
+// Entity handler
+//-----------------------------------------------------------------------------
+OCEntityHandlerResult rdEntityHandler(OCEntityHandlerFlag /*flag*/,
+ OCEntityHandlerRequest * /*entityHandlerRequest*/,
+ void* /*callbackParam*/)
+{
+ OIC_LOG(INFO, TAG, "Entering entityHandler");
+
+ return OC_EH_OK;
+}
+
+//-----------------------------------------------------------------------------
+// Tests
+//-----------------------------------------------------------------------------
+class RDTests : public testing::Test {
+ protected:
+ virtual void SetUp()
+ {
+ OCInit("127.0.0.1", 5683, OC_CLIENT_SERVER);
+ }
+
+ virtual void TearDown()
+ {
+ OCStop();
+ }
+};
+
+TEST_F(RDTests, CreateRDPayload)
+{
+ itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
+
+ OCResourceHandle handle;
+ EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle, "core.light",
+ "oic.if.baseline", "/a/light", rdEntityHandler,
+ NULL, OC_DISCOVERABLE | OC_OBSERVABLE));
+
+ // Create RD Publish Payload.
+ OCRDPayload *rdPayload = (OCRDPayload *) OCRDPublishPayloadCreate(&handle, 1, 86400);
+ EXPECT_TRUE(rdPayload != NULL);
+
+ // Cleanup.
+ OCRDPayloadDestroy(rdPayload);
+}
+
+TEST_F(RDTests, ConvertParseRDPayload)
+{
+ itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
+
+ OCResourceHandle handle;
+ EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle, "core.light",
+ "oic.if.baseline", "/a/light", rdEntityHandler,
+ NULL, OC_DISCOVERABLE | OC_OBSERVABLE));
+
+ // Create RD Publish Payload.
+ OCRDPayload *rdPayload = (OCRDPayload *) OCRDPublishPayloadCreate(&handle, 1, 86400);
+ EXPECT_TRUE(rdPayload != NULL);
+
+ if (rdPayload)
+ {
+ // Convert RD Publish Payload to CBOR.
+ size_t curSize = 255;
+ uint8_t *outPayload = (uint8_t *)OICCalloc(1, curSize);
+ OCRDPayloadToCbor(rdPayload, outPayload, &curSize);
+
+ // Parse CBOR back to RD Publish Payload.
+ CborParser parser;
+ CborValue rootValue;
+ cbor_parser_init(outPayload, curSize, 0, &parser, &rootValue);
+
+ OCRDPayload *rdPayload = NULL;
+ EXPECT_EQ(OC_STACK_OK, OCRDCborToPayload(&rootValue, (OCPayload**) &rdPayload));
+
+ // Cleanup.
+ OICFree(outPayload);
+ }
+ OCRDPayloadDestroy(rdPayload);
+}
+
+TEST_F(RDTests, RDPublishResourceNullAddr)
+{
+ itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
+ EXPECT_EQ(OC_STACK_INVALID_IP, OCRDPublish(0, CT_ADAPTER_IP, nullptr, 0, 0, OC_LOW_QOS));
+}
+
+TEST_F(RDTests, RDPublishResourceNullCB)
+{
+ itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
+ EXPECT_EQ(OC_STACK_INVALID_CALLBACK, OCRDPublish("127.0.0.1", CT_ADAPTER_IP, nullptr,
+ 0, 0, OC_LOW_QOS));
+}
+
+TEST_F(RDTests, RDPublishResource)
+{
+ itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
+
+ OCCallbackData cbData;
+ cbData.cb = &handlePublishCB;
+ cbData.cd = NULL;
+ cbData.context = (void*) DEFAULT_CONTEXT_VALUE;
+
+ OCResourceHandle handle;
+
+ EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle, "core.light",
+ "oic.if.baseline", "/a/light", rdEntityHandler,
+ NULL, (OC_DISCOVERABLE | OC_OBSERVABLE)));
+
+ EXPECT_EQ(OC_STACK_OK, OCRDPublish("127.0.0.1", CT_ADAPTER_IP, &handle,
+ 1, &cbData, OC_LOW_QOS));
+}
+
+TEST_F(RDTests, RDPublishMultipleResources)
+{
+ itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
+
+ OCCallbackData cbData;
+ cbData.cb = &handlePublishCB;
+ cbData.cd = NULL;
+ cbData.context = (void*) DEFAULT_CONTEXT_VALUE;
+
+ OCResourceHandle handles[2];
+ EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handles[0], "core.light",
+ "oic.if.baseline", "/a/light", rdEntityHandler,
+ NULL, (OC_DISCOVERABLE | OC_OBSERVABLE)));
+
+ EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handles[1], "core.light",
+ "oic.if.baseline", "/a/light2", rdEntityHandler,
+ NULL, (OC_DISCOVERABLE | OC_OBSERVABLE)));
+
+ EXPECT_EQ(OC_STACK_OK, OCRDPublish("127.0.0.1", CT_ADAPTER_IP, handles,
+ 2, &cbData, OC_LOW_QOS));
+}
+
+TEST_F(RDTests, RDDeleteResourceNullAddr)
+{
+ itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
+ EXPECT_EQ(OC_STACK_INVALID_IP, OCRDDelete(0, CT_ADAPTER_IP, nullptr, 0, 0, OC_LOW_QOS));
+}
+
+TEST_F(RDTests, RDDeleteResourceNullCB)
+{
+ itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
+ EXPECT_EQ(OC_STACK_INVALID_CALLBACK, OCRDDelete("127.0.0.1", CT_ADAPTER_IP, nullptr,
+ 0, 0, OC_LOW_QOS));
+}
+
+TEST_F(RDTests, RDDeleteAllResource)
+{
+ itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
+
+ OCCallbackData cbData;
+ cbData.cb = &handleDeleteCB;
+ cbData.cd = NULL;
+ cbData.context = (void*) DEFAULT_CONTEXT_VALUE;
+
+ EXPECT_EQ(OC_STACK_OK, OCRDDelete("127.0.0.1", CT_ADAPTER_IP, nullptr, 0, &cbData,
+ OC_LOW_QOS));
+}
+
+TEST_F(RDTests, RDDeleteSpecificResource)
+{
+ itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
+
+ OCCallbackData cbData;
+ cbData.cb = &handleDeleteCB;
+ cbData.cd = NULL;
+ cbData.context = (void*) DEFAULT_CONTEXT_VALUE;
+
+ OCResourceHandle handle;
+
+ EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle, "core.light",
+ "oic.if.baseline", "/a/light", rdEntityHandler,
+ NULL, OC_DISCOVERABLE | OC_OBSERVABLE));
+
+ EXPECT_EQ(OC_STACK_OK, OCRDDelete("127.0.0.1", CT_ADAPTER_IP, &handle,
+ 1, &cbData, OC_LOW_QOS));
+}
if target_os in ['msys_nt', 'windows']:
examples_env.AppendUnique(LIBS = ['Comctl32', 'Gdi32', 'User32'])
+if examples_env.get('LOGGING'):
+ examples_env.AppendUnique(CPPDEFINES = ['TB_LOG'])
+
+if 'CLIENT' in examples_env.get('RD_MODE'):
+ examples_env.AppendUnique(CPPDEFINES = ['RD_CLIENT'])
+
def make_single_file_cpp_program(program_name):
return examples_env.Program(program_name, program_name + ".cpp")
'groupserver',
'groupclient',
'lightserver',
- 'threadingsample',
+ 'threadingsample'
+ ]
+
+if 'CLIENT' in examples_env.get('RD_MODE'):
+ example_names += [
+ 'rdclient'
]
examples = map(make_single_file_cpp_program, example_names)
// callback handler on PUT request
void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
{
- if(eCode == SUCCESS_RESPONSE)
+ if(eCode == SUCCESS_RESPONSE || eCode == OC_STACK_RESOURCE_CHANGED)
{
std::cout << "PUT request was successful" << std::endl;
--- /dev/null
+//******************************************************************
+//
+// Copyright 2016 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// 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 "OCPlatform.h"
+#include "OCApi.h"
+
+#include <functional>
+#include <iostream>
+
+using namespace std;
+using namespace OC;
+
+string rdAddress;
+OCConnectivityType connectivityType = CT_ADAPTER_TCP;
+
+ResourceHandles m_publishedResourceHandles;
+
+static void onDelete(const int& eCode);
+static void onPublish(const OCRepresentation& rep, const int& eCode);
+
+class Resource
+{
+
+public:
+ std::string m_resourceUri;
+ OCResourceHandle m_resourceHandle;
+
+public:
+ /// This function internally calls registerResource API.
+ void registerResource()
+ {
+ string resourceURI;
+ string resourceTypeName;
+ string resourceInterface;
+
+ std::cout <<" resourceURI: ";
+ std::cin >> resourceURI;
+ std::cout <<" resourceTypeName: ";
+ std::cin >> resourceTypeName;
+ std::cout <<" resourceInterface: ";
+ std::cin >> resourceInterface;
+
+ m_resourceUri = resourceURI;
+
+ OCPlatform::registerResource(m_resourceHandle, resourceURI, resourceTypeName,
+ resourceInterface,
+ nullptr,
+ OC_DISCOVERABLE);
+
+ m_publishedResourceHandles.push_back(m_resourceHandle);
+ cout << "registerResource is called." << endl;
+ }
+
+ void updateResource()
+ {
+ cout << " 1:: add resource type\n";
+ cout << " 2:: add resource interface\n";
+
+ int selectedMenu;
+ std::cin >> selectedMenu;
+
+ string inputString;
+ switch (selectedMenu)
+ {
+ case 1:
+ std::cout << "Add Resource Type" << std::endl;
+ std::cout <<" resourceTypeName: ";
+ std::cin >> inputString;
+ OCPlatform::bindTypeToResource(m_resourceHandle, inputString);
+ break;
+ case 2:
+ std::cout << "Add Resource Interface" << std::endl;
+ std::cout <<" resourceInterface: ";
+ std::cin >> inputString;
+ OCPlatform::bindInterfaceToResource(m_resourceHandle, inputString);
+ break;
+ default:
+ cout << "Invalid option" << endl;
+ return;
+ }
+ }
+
+ void publishResource()
+ {
+ /*
+ * Publish Resource of Resource-Server to RD.
+ */
+
+ OCStackResult result = OCPlatform::publishResourceToRD(rdAddress, connectivityType,
+ m_publishedResourceHandles,
+ &onPublish);
+ if (OC_STACK_OK != result)
+ {
+ cout << "Resource publish was unsuccessful\n";
+ }
+ }
+
+ void deleteResource()
+ {
+ /*
+ * Delete Resource with Resource Handle.
+ * Don't need to include resource handle,
+ * if resource-server want to delete all resources from RD.
+ * Ex.) OCPlatform::deleteResourceFromRD(rdAddress, connectivityType, &onDelete);
+ */
+ OCStackResult result = OCPlatform::deleteResourceFromRD(rdAddress, connectivityType,
+ m_publishedResourceHandles,
+ &onDelete);
+ if (OC_STACK_OK != result)
+ {
+ cout << "Resource delete was unsuccessful\n";
+ }
+ }
+};
+
+void onDelete(const int& eCode)
+{
+ cout << "Received Delete Resource Response From RD\n";
+ try
+ {
+ if (OC_STACK_RESOURCE_DELETED == eCode)
+ {
+ cout << "Resource delete was successful\n";
+ }
+ else
+ {
+ std::cout << "onDelete Response error: " << eCode << std::endl;
+ std::exit(-1);
+ }
+ }
+ catch(std::exception& e)
+ {
+ std::cout << "Exception: " << e.what() << " in onDelete" << std::endl;
+ }
+}
+
+void onPublish(const OCRepresentation& rep, const int& eCode)
+{
+ cout << endl <<"Received Publish Resource Response From RD\n";
+
+ try
+ {
+ if (OC_STACK_RESOURCE_CREATED == eCode)
+ {
+ cout << "=========== Published Resource ===========" << endl;
+ if (rep.hasAttribute("di"))
+ {
+ std::cout << " di: " << rep.getValue<std::string>("di") << std::endl;
+ }
+
+ // Published Resource is included as the child resource.
+ std::vector<OCRepresentation> children = rep.getChildren();
+
+ for (auto oit = children.begin(); oit != children.end(); ++oit)
+ {
+ std::string m_href;
+ oit->getValue("href", m_href);
+ cout << " href : " << m_href << "\n";
+
+ cout << " resource type : \n";
+ for(const std::string& type : oit->getResourceTypes())
+ {
+ cout << " " << type << "\n";
+ }
+
+ cout << " resource interface : \n";
+ for(const std::string& type : oit->getResourceInterfaces())
+ {
+ cout << " " << type << "\n";
+ }
+
+ int m_ins;
+ oit->getValue("ins", m_ins);
+ cout << " ins : " << m_ins << "\n";
+ }
+ cout << "=========================================" << endl;
+ }
+ else
+ {
+ std::cout << "onPublish Response error: " << eCode << std::endl;
+ std::exit(-1);
+ }
+ }
+ catch(std::exception& e)
+ {
+ std::cout << "Exception: " << e.what() << " in onPublish" << std::endl;
+ }
+}
+
+static void printUsage()
+{
+ std::cout<<"Usage: rdserver <coap+tcp://10.11.12.13:5683>\n";
+}
+
+int main(int argc, char* argv[])
+{
+ ostringstream requestURI;
+
+ if (argc == 2)
+ {
+ rdAddress = argv[1];
+ }
+ else
+ {
+ printUsage();
+ return -1;
+ }
+
+ PlatformConfig config
+ { OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
+
+ try
+ {
+ OCPlatform::Configure(config);
+
+ int selectedMenu;
+ bool isRun = true;
+
+ std::cout << "Register resources" << std::endl;
+ Resource myResource;
+ myResource.registerResource();
+
+ while (isRun)
+ {
+ cout << "================= MENU =================" << endl;
+ cout << "0 :: Quit" << endl;
+ cout << "1 :: UPDATE RESOURCE" << endl;
+ cout << "2 :: PUBLISH RESOURCE TO RD" << endl;
+ cout << "3 :: DELETE RESOURCE FROM RD" << endl;
+ cout << "========================================" << endl;
+ std::cin >> selectedMenu;
+
+ switch (selectedMenu)
+ {
+ case 0:
+ isRun = false;
+ break;
+ case 1:
+ std::cout << "Update resources" << std::endl;
+ myResource.updateResource();
+ break;
+ case 2:
+ std::cout << "Publish resources to RD" << std::endl;
+ myResource.publishResource();
+ break;
+ case 3:
+ std::cout << "Delete resources from RD" << std::endl;
+ myResource.deleteResource();
+ break;
+ default:
+ cout << "Invalid option" << endl;
+ }
+
+ }
+ }
+ catch (OCException& e)
+ {
+ oclog() << "Exception in main: "<< e.what();
+ }
+
+ return 0;
+}
+
#include "OCPlatform.h"
#include "OCApi.h"
+#define maxSequenceNumber 0xFFFFFF
+
using namespace OC;
static const char* SVR_DB_FILE_NAME = "./oic_svr_db_client.dat";
{
try
{
- if(eCode == OC_STACK_OK && sequenceNumber != -1)
+ if(eCode == OC_STACK_OK && sequenceNumber != maxSequenceNumber + 1)
{
if(sequenceNumber == OC_OBSERVE_REGISTER)
{
{
try
{
- if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
+ if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED
+ || eCode == OC_STACK_RESOURCE_CHANGED)
{
std::cout << "POST request was successful" << std::endl;
{
try
{
- if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
+ if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED
+ || eCode == OC_STACK_RESOURCE_CHANGED)
{
std::cout << "POST request was successful" << std::endl;
{
try
{
- if(eCode == OC_STACK_OK)
+ if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CHANGED)
{
std::cout << "PUT request was successful" << std::endl;
void onPost2(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
{
- if(eCode == SUCCESS_RESPONSE)
+ if(eCode == SUCCESS_RESPONSE || eCode == OC_STACK_RESOURCE_CHANGED)
{
std::cout << "POST request was successful" << std::endl;
void onPost(const HeaderOptions& /*headerOptions*/,
const OCRepresentation& rep, const int eCode)
{
- if(eCode == SUCCESS_RESPONSE)
+ if(eCode == SUCCESS_RESPONSE || eCode == OC_STACK_RESOURCE_CHANGED)
{
std::cout << "POST request was successful" << std::endl;
std::cout <<"Clientside Put response to get was: "<<std::endl;
std::cout <<"ErrorCode: "<<eCode <<std::endl;
- if(eCode == 0)
+ if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CHANGED)
{
std::cout<<"Successful Put. Attributes sent were: "<<std::endl;
{\r
try\r
{\r
- if (eCode == OC_STACK_OK)\r
+ if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CHANGED)\r
{\r
std::cout << "PUT request was successful" << std::endl;\r
\r
{\r
try\r
{\r
- if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)\r
+ if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED\r
+ || OC_STACK_RESOURCE_CHANGED)\r
{\r
std::cout << "POST request was successful" << std::endl;\r
\r
virtual OCStackResult UnsubscribePresence(OCDoHandle handle) =0;
+#ifdef WITH_CLOUD
virtual OCStackResult SubscribeDevicePresence(OCDoHandle* handle,
const std::string& host,
const QueryParamsList& queryParams,
OCConnectivityType connectivityType,
ObserveCallback& callback) = 0;
+#endif
virtual OCStackResult GetDefaultQos(QualityOfService& qos) = 0;
virtual OCStackResult setDefaultDeviceEntityHandler(EntityHandler entityHandler) = 0;
virtual OCStackResult sendResponse(const std::shared_ptr<OCResourceResponse> pResponse) = 0;
+#ifdef RD_CLIENT
+ virtual OCStackResult publishResourceToRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ PublishResourceCallback& callback,
+ OCQualityOfService qos) = 0;
+
+ virtual OCStackResult deleteResourceFromRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ DeleteResourceCallback& callback,
+ OCQualityOfService qos) = 0;
+#endif
};
}
virtual OCStackResult UnsubscribePresence(OCDoHandle handle);
+#ifdef WITH_CLOUD
virtual OCStackResult SubscribeDevicePresence(OCDoHandle* handle,
const std::string& host,
const QueryParamsList& queryParams,
OCConnectivityType connectivityType,
ObserveCallback& callback);
+#endif
OCStackResult GetDefaultQos(QualityOfService& QoS);
namespace OC
{
+#ifdef RD_CLIENT
+ namespace ServerCallbackContext
+ {
+ struct PublishContext
+ {
+ PublishResourceCallback callback;
+ PublishContext(PublishResourceCallback cb) : callback(cb){}
+ };
+
+ struct DeleteContext
+ {
+ DeleteResourceCallback callback;
+ DeleteContext(DeleteResourceCallback cb) : callback(cb){}
+ };
+ }
+#endif
class InProcServerWrapper : public IServerWrapper
{
public:
virtual OCStackResult setDefaultDeviceEntityHandler(EntityHandler entityHandler);
virtual OCStackResult sendResponse(const std::shared_ptr<OCResourceResponse> pResponse);
+#ifdef RD_CLIENT
+ virtual OCStackResult publishResourceToRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ PublishResourceCallback& callback,
+ OCQualityOfService qos);
+
+ virtual OCStackResult deleteResourceFromRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ DeleteResourceCallback& callback,
+ OCQualityOfService qos);
+#endif
private:
void processFunc();
std::thread m_processThread;
Observe,
ObserveAll
};
- //
- // Typedef for header option vector
- // OCHeaderOption class is in HeaderOption namespace
+
+ // Typedef for list of resource handles.
+ typedef std::vector<OCResourceHandle> ResourceHandles;
+
+ // Typedef for header option vector.
+ // OCHeaderOption class is in HeaderOption namespace.
typedef std::vector<HeaderOption::OCHeaderOption> HeaderOptions;
- // Typedef for query parameter map
+ // Typedef for query parameter map.
typedef std::map<std::string, std::string> QueryParamsMap;
// Typedef for query parameter map with Vector
typedef std::map< std::string, std::vector<std::string> > QueryParamsList;
- // Typedef for list of observation IDs
+ // Typedef for list of observation IDs.
typedef std::vector<OCObservationId> ObservationIds;
enum class ObserveAction
typedef std::function<void(const HeaderOptions&,
const OCRepresentation&, const int,
std::shared_ptr<OCResource>)> MQCreateTopicCallback;
+#ifdef RD_CLIENT
+ typedef std::function<void(const OCRepresentation&, const int)> PublishResourceCallback;
+ typedef std::function<void(const int)> DeleteResourceCallback;
+#endif
} // namespace OC
#endif
*/
OCStackResult unsubscribePresence(OCPresenceHandle presenceHandle);
+#ifdef WITH_CLOUD
/**
* Subscribes to a server's device presence change events.
*
const QueryParamsList& queryParams,
OCConnectivityType connectivityType,
ObserveCallback callback);
+#endif
/**
* Creates a resource proxy object so that get/put/observe functionality
OCConnectivityType connectivityType,
PostCallback cloudConnectHandler);
#endif // WITH_CLOUD
+#ifdef RD_CLIENT
+ /**
+ * API for Virtual Resource("/oic/d" and "/oic/p") Publish to Resource Directory.
+ * @note This API applies to resource server side only.
+ *
+ * @param host Host IP Address of a service to direct resource publish query.
+ * @param connectivityType ::OCConnectivityType type of connectivity.
+ * @param callback Handles callbacks, success states and failure states.
+ *
+ * @return Returns ::OC_STACK_OK if success.
+ */
+ OCStackResult publishResourceToRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ PublishResourceCallback callback);
+
+ /**
+ * API for Resource Publish to Resource Directory.
+ * @note This API applies to resource server side only.
+ *
+ * @param host Host IP Address of a service to direct resource publish query.
+ * @param connectivityType ::OCConnectivityType type of connectivity.
+ * @param resourceHandle resource handle of the resource.
+ * @param callback Handles callbacks, success states and failure states.
+ *
+ * @return Returns ::OC_STACK_OK if success.
+ */
+ OCStackResult publishResourceToRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ PublishResourceCallback callback);
+
+ /**
+ * @overload
+ *
+ * @param host Host IP Address of a service to direct resource publish query.
+ * @param connectivityType ::OCConnectivityType type of connectivity.
+ * @param resourceHandle resource handle of the resource.
+ * @param callback function to callback with published resources.
+ * @param QoS the quality of communication
+ * @see publishResourceToRD(const std::string&, OCConnectivityType, OCResourceHandle,
+ * uint8_t, PublishResourceCallback)
+ */
+ OCStackResult publishResourceToRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ PublishResourceCallback callback, QualityOfService QoS);
+
+ /**
+ * API for published resource delete from Resource Directory.
+ * @note This API applies to resource server side only.
+ *
+ * @param host Host IP Address of a service to direct resource delete query.
+ * @param connectivityType ::OCConnectivityType type of connectivity.
+ * @param callback Handles callbacks, success states and failure states.
+ *
+ * @return Returns ::OC_STACK_OK if success.
+ */
+ OCStackResult deleteResourceFromRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ DeleteResourceCallback callback);
+
+ /**
+ * @overload
+ *
+ * @param host Host IP Address of a service to direct resource delete query.
+ * @param connectivityType ::OCConnectivityType type of connectivity.
+ * @param resourceHandle resource handle of the resource.
+ * @param callback function to callback with published resources.
+ * @param QoS the quality of communication
+ * @see publishResourceToRD(const std::string&, OCConnectivityType, OCResourceHandle,
+ * uint8_t, PublishResourceCallback)
+ */
+ OCStackResult deleteResourceFromRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ DeleteResourceCallback callback);
+
+ /**
+ * @overload
+ *
+ * @param host Host IP Address of a service to direct resource delete query.
+ * @param connectivityType ::OCConnectivityType type of connectivity.
+ * @param resourceHandle resource handle of the resource.
+ * @param callback function to callback with published resources.
+ * @param QoS the quality of communication
+ * @see publishResourceToRD(const std::string&, OCConnectivityType, OCResourceHandle,
+ * uint8_t, PublishResourceCallback)
+ */
+ OCStackResult deleteResourceFromRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ DeleteResourceCallback callback, QualityOfService QoS);
+#endif
}
}
SubscribeCallback presenceHandler);
OCStackResult unsubscribePresence(OCPresenceHandle presenceHandle);
+#ifdef WITH_CLOUD
OCStackResult subscribeDevicePresence(OCPresenceHandle& presenceHandle,
const std::string& host,
const QueryParamsList& queryParams,
OCConnectivityType connectivityType,
ObserveCallback callback);
+#endif
OCResource::Ptr constructResourceObject(const std::string& host, const std::string& uri,
OCConnectivityType connectivityType, bool isObservable,
const std::vector<std::string>& resourceTypes,
const std::vector<std::string>& interfaces);
OCStackResult sendResponse(const std::shared_ptr<OCResourceResponse> pResponse);
-
+#ifdef RD_CLIENT
+ OCStackResult publishResourceToRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ PublishResourceCallback callback);
+
+ OCStackResult publishResourceToRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ PublishResourceCallback callback, QualityOfService qos);
+
+ OCStackResult deleteResourceFromRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ DeleteResourceCallback callback);
+
+ OCStackResult deleteResourceFromRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ DeleteResourceCallback callback);
+
+ OCStackResult deleteResourceFromRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ DeleteResourceCallback callback, QualityOfService qos);
+#endif
std::weak_ptr<std::recursive_mutex> csdkLock();
OCStackResult findDirectPairingDevices(unsigned short waittime,
void setPayload(const OCPlatformPayload* rep);
void setPayload(const OCRepPayload* rep);
-
+#ifdef RD_CLIENT
+ void setPayload(const OCRDPayload* rep);
+#endif
OCRepPayload* getPayload() const;
const std::vector<OCRepresentation>& representations() const;
template<typename T>
T payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl);
void setPayload(const OCRepPayload* payload);
+#ifdef RD_CLIENT
+ void setPayload(const OCLinksPayload* payload);
+#endif
void setPayloadArray(const OCRepPayloadValue* pl);
void getPayloadArray(OCRepPayload* payload,
const OCRepresentation::AttributeItem& item) const;
virtual OCStackResult UnsubscribePresence(OCDoHandle /*handle*/)
{return OC_STACK_NOTIMPL;}
-
+#ifdef WITH_CLOUD
virtual OCStackResult SubscribeDevicePresence(
OCDoHandle* /*handle*/,
const std::string& /*host*/,
OCConnectivityType /*connectivityType*/,
ObserveCallback& /*callback*/)
{return OC_STACK_NOTIMPL;}
+#endif
virtual OCStackResult GetDefaultQos(QualityOfService& /*QoS*/)
{return OC_STACK_NOTIMPL;}
static const char DUPLICATE_UUID[] = "Duplicate UUID in DB";
static const char INCONSISTENT_DB[] = "Data in provisioning DB is inconsistent";
static const char AUTHENTICATION_FAILURE[] = "Authentication failure";
+ static const char PUBLISH_RESOURCE_FAILED[] = "Publish Resource failure";
}
namespace Error
std::thread exec(context->callback, serverHeaderOptions, attrs,
result, sequenceNumber);
exec.detach();
- if (sequenceNumber == OC_OBSERVE_DEREGISTER)
- {
- return OC_STACK_DELETE_TRANSACTION;
- }
+
return OC_STACK_KEEP_TRANSACTION;
}
return result;
}
+#ifdef WITH_CLOUD
OCStackResult InProcClientWrapper::SubscribeDevicePresence(OCDoHandle* handle,
const std::string& host,
const QueryParamsList& queryParams,
return result;
}
+#endif
OCStackResult InProcClientWrapper::GetDefaultQos(QualityOfService& qos)
{
#include <OCPlatform.h>
#include <OCUtilities.h>
+#ifdef RD_CLIENT
+#include <oicresourcedirectory.h>
+#endif
+
using namespace std;
using namespace OC;
return result;
}
}
+#ifdef RD_CLIENT
+ OCRepresentation parseRDResponseCallback(OCClientResponse* clientResponse)
+ {
+ if (nullptr == clientResponse->payload ||
+ PAYLOAD_TYPE_RD != clientResponse->payload->type)
+ {
+ return OCRepresentation();
+ }
+
+ MessageContainer oc;
+ oc.setPayload(clientResponse->payload);
+
+ std::vector<OCRepresentation>::const_iterator it = oc.representations().begin();
+ if (it == oc.representations().end())
+ {
+ return OCRepresentation();
+ }
+ // first one is considered the root, everything else is considered a child of this one.
+ OCRepresentation root = *it;
+ root.setDevAddr(clientResponse->devAddr);
+ root.setUri(clientResponse->resourceUri);
+ ++it;
+
+ std::for_each(it, oc.representations().end(),
+ [&root](const OCRepresentation& repItr)
+ {root.addChild(repItr);});
+ return root;
+
+ }
+
+ OCStackApplicationResult publishResourceToRDCallback(void* ctx, OCDoHandle /*handle*/,
+ OCClientResponse* clientResponse)
+ {
+ ServerCallbackContext::PublishContext* context =
+ static_cast<ServerCallbackContext::PublishContext*>(ctx);
+
+ try
+ {
+ // Update resource unique id in stack.
+ if (clientResponse)
+ {
+ if (clientResponse->payload)
+ {
+ OCRDPayload *rdPayload = (OCRDPayload *) clientResponse->payload;
+ OCLinksPayload *links = rdPayload->rdPublish->setLinks;
+
+ while (links)
+ {
+ OCResourceHandle handle = OCGetResourceHandleAtUri(links->href);
+ OCBindResourceInsToResource(handle, links->ins);
+ links = links->next;
+ }
+
+ }
+ }
+
+ OCRepresentation rep = parseRDResponseCallback(clientResponse);
+ std::thread exec(context->callback, rep, clientResponse->result);
+ exec.detach();
+ }
+ catch (OC::OCException& e)
+ {
+ oclog() <<"Exception in publishResourceToRDCallback, ignoring response: "
+ <<e.what() <<std::flush;
+ }
+
+ return OC_STACK_KEEP_TRANSACTION;
+ }
+
+ OCStackResult InProcServerWrapper::publishResourceToRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ PublishResourceCallback& callback,
+ OCQualityOfService qos)
+ {
+ ServerCallbackContext::PublishContext* ctx =
+ new ServerCallbackContext::PublishContext(callback);
+ OCCallbackData cbdata(
+ static_cast<void*>(ctx),
+ publishResourceToRDCallback,
+ [](void* c)
+ {delete static_cast<ServerCallbackContext::PublishContext*>(c);}
+ );
+
+ auto cLock = m_csdkLock.lock();
+ OCStackResult result = OC_STACK_ERROR;
+ if (cLock)
+ {
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
+ result = OCRDPublish(host.c_str(), connectivityType, &resourceHandles[0],
+ resourceHandles.size(), &cbdata, qos);
+ }
+
+ if (OC_STACK_OK != result)
+ {
+ throw OCException(OC::Exception::PUBLISH_RESOURCE_FAILED, result);
+ }
+ return result;
+ }
+
+ OCStackApplicationResult deleteResourceFromRDCallback(void* ctx, OCDoHandle /*handle*/,
+ OCClientResponse* clientResponse)
+ {
+ ServerCallbackContext::DeleteContext* context =
+ static_cast<ServerCallbackContext::DeleteContext*>(ctx);
+
+ std::thread exec(context->callback, clientResponse->result);
+ exec.detach();
+ return OC_STACK_DELETE_TRANSACTION;
+ }
+
+ OCStackResult InProcServerWrapper::deleteResourceFromRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ DeleteResourceCallback& callback,
+ OCQualityOfService qos)
+ {
+ ServerCallbackContext::DeleteContext* ctx =
+ new ServerCallbackContext::DeleteContext(callback);
+ OCCallbackData cbdata(
+ static_cast<void*>(ctx),
+ deleteResourceFromRDCallback,
+ [](void* c)
+ {delete static_cast<ServerCallbackContext::DeleteContext*>(c);}
+ );
+
+ auto cLock = m_csdkLock.lock();
+ OCStackResult result = OC_STACK_ERROR;
+ if (cLock)
+ {
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
+ result = OCRDDelete(host.c_str(), connectivityType, &resourceHandles[0],
+ resourceHandles.size(), &cbdata, qos);
+ }
+
+ if (OC_STACK_OK != result)
+ {
+ throw OCException(OC::Exception::PUBLISH_RESOURCE_FAILED, result);
+ }
+ return result;
+ }
+#endif
InProcServerWrapper::~InProcServerWrapper()
{
if(m_processThread.joinable())
return OCPlatform_impl::Instance().unsubscribePresence(presenceHandle);
}
+#ifdef WITH_CLOUD
OCStackResult subscribeDevicePresence(OCPresenceHandle& presenceHandle,
const std::string& host,
const QueryParamsList& queryParams,
connectivityType,
callback);
}
+#endif
OCStackResult sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
{
connectivityType, cloudConnectHandler);
}
#endif // WITH_CLOUD
+#ifdef RD_CLIENT
+ OCStackResult publishResourceToRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ PublishResourceCallback callback)
+ {
+ ResourceHandles resourceHandles;
+ return OCPlatform_impl::Instance().publishResourceToRD(host, connectivityType,
+ resourceHandles,
+ callback);
+ }
+
+ OCStackResult publishResourceToRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ PublishResourceCallback callback)
+ {
+ return OCPlatform_impl::Instance().publishResourceToRD(host, connectivityType,
+ resourceHandles,
+ callback);
+ }
+
+ OCStackResult publishResourceToRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ PublishResourceCallback callback, QualityOfService QoS)
+ {
+ return OCPlatform_impl::Instance().publishResourceToRD(host, connectivityType,
+ resourceHandles,
+ callback, QoS);
+ }
+
+ OCStackResult deleteResourceFromRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ DeleteResourceCallback callback)
+ {
+ ResourceHandles resourceHandles;
+ return OCPlatform_impl::Instance().deleteResourceFromRD(host, connectivityType,
+ resourceHandles, callback);
+ }
+
+ OCStackResult deleteResourceFromRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ DeleteResourceCallback callback)
+ {
+ return OCPlatform_impl::Instance().deleteResourceFromRD(host, connectivityType,
+ resourceHandles, callback);
+ }
+
+ OCStackResult deleteResourceFromRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ DeleteResourceCallback callback, QualityOfService QoS)
+ {
+ return OCPlatform_impl::Instance().deleteResourceFromRD(host, connectivityType,
+ resourceHandles, callback,
+ QoS);
+ }
+#endif
} // namespace OCPlatform
} //namespace OC
std::ref(presenceHandle));
}
+#ifdef WITH_CLOUD
OCStackResult OCPlatform_impl::subscribeDevicePresence(OCPresenceHandle& presenceHandle,
const std::string& host,
const QueryParamsList& queryParams,
return checked_guard(m_client, &IClientWrapper::SubscribeDevicePresence,
&presenceHandle, host, queryParams, connectivityType, callback);
}
+#endif
OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
{
return checked_guard(m_server, &IServerWrapper::sendResponse,
pResponse);
}
+#ifdef RD_CLIENT
+ OCStackResult OCPlatform_impl::publishResourceToRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ PublishResourceCallback callback)
+ {
+ return publishResourceToRD(host, connectivityType, resourceHandles,
+ callback, m_cfg.QoS);
+ }
+
+ OCStackResult OCPlatform_impl::publishResourceToRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ PublishResourceCallback callback,
+ QualityOfService qos)
+ {
+ return checked_guard(m_server, &IServerWrapper::publishResourceToRD,
+ host, connectivityType, resourceHandles, callback,
+ static_cast<OCQualityOfService>(qos));
+ }
+ OCStackResult OCPlatform_impl::deleteResourceFromRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ DeleteResourceCallback callback)
+ {
+ return deleteResourceFromRD(host, connectivityType, resourceHandles, callback,
+ m_cfg.QoS);
+ }
+
+ OCStackResult OCPlatform_impl::deleteResourceFromRD(const std::string& host,
+ OCConnectivityType connectivityType,
+ ResourceHandles& resourceHandles,
+ DeleteResourceCallback callback,
+ QualityOfService qos)
+ {
+ return checked_guard(m_server, &IServerWrapper::deleteResourceFromRD,
+ host, connectivityType, resourceHandles, callback,
+ static_cast<OCQualityOfService>(qos));
+ }
+#endif
std::weak_ptr<std::recursive_mutex> OCPlatform_impl::csdkLock()
{
return m_csdkLock;
case PAYLOAD_TYPE_PLATFORM:
setPayload(reinterpret_cast<const OCPlatformPayload*>(rep));
break;
+#ifdef RD_CLIENT
+ case PAYLOAD_TYPE_RD:
+ setPayload(reinterpret_cast<const OCRDPayload*>(rep));
+ break;
+#endif
default:
throw OC::OCException("Invalid Payload type in setPayload");
break;
void MessageContainer::setPayload(const OCRepPayload* payload)
{
const OCRepPayload* pl = payload;
- while(pl)
+ while (pl)
{
OCRepresentation cur;
cur.setPayload(pl);
this->addRepresentation(cur);
}
}
+#ifdef RD_CLIENT
+ void MessageContainer::setPayload(const OCRDPayload* payload)
+ {
+ OCRepresentation rep;
+ rep[OC_RSRVD_DEVICE_ID] = (payload->rdPublish->tags->di.id) ?
+ std::string(reinterpret_cast<const char*>(payload->rdPublish->tags->di.id)) :
+ std::string();
+ rep[OC_RSRVD_DEVICE_NAME] = (payload->rdPublish->tags->n.deviceName) ?
+ std::string(payload->rdPublish->tags->n.deviceName) :
+ std::string();
+ this->addRepresentation(rep);
+ const OCLinksPayload* pl = payload->rdPublish->setLinks;
+ while (pl)
+ {
+ OCRepresentation cur;
+ cur.setPayload(pl);
+
+ pl = pl->next;
+ this->addRepresentation(cur);
+ }
+ }
+#endif
OCRepPayload* MessageContainer::getPayload() const
{
OCRepPayload* root = nullptr;
val = val->next;
}
}
+#ifdef RD_CLIENT
+ void OCRepresentation::setPayload(const OCLinksPayload* pl)
+ {
+ if (pl->href)
+ {
+ setValue<std::string>(OC_RSRVD_HREF, pl->href);
+ }
+ if (pl->rel)
+ {
+ setValue<std::string>(OC_RSRVD_REL, pl->rel);
+ }
+ OCStringLL* ll = pl->rt;
+ while (ll)
+ {
+ addResourceType(ll->value);
+ ll = ll->next;
+ }
+ ll = pl->itf;
+ while (ll)
+ {
+ addResourceInterface(ll->value);
+ ll = ll->next;
+ }
+ setValue<int>(OC_RSRVD_POLICY, pl->p);
+ setValue<int>(OC_RSRVD_INS, pl->ins);
+ setValue<int>(OC_RSRVD_TTL, pl->ttl);
+ if (pl->title)
+ {
+ setValue<std::string>(OC_RSRVD_TITLE, pl->title);
+ }
+ if (pl->anchor)
+ {
+ setValue<std::string>(OC_RSRVD_URI, pl->anchor);
+ }
+ }
+#endif
void OCRepresentation::addChild(const OCRepresentation& rep)
{
m_children.push_back(rep);
if oclib_env.get('WITH_CLOUD'):
oclib_env.AppendUnique(CPPDEFINES = ['WITH_CLOUD'])
+if 'CLIENT' in oclib_env.get('RD_MODE'):
+ oclib_env.AppendUnique(CPPDEFINES = ['RD_CLIENT'])
+if 'SERVER' in oclib_env.get('RD_MODE'):
+ oclib_env.AppendUnique(CPPDEFINES = ['RD_SERVER'])
+
######################################################################
# Source files and Targets
######################################################################
SConscript('csdk/connectivity/test/SConscript')
# Build Security Resource Manager unit tests
- SConscript('csdk/security/unittest/SConscript')
+ if env.get('SECURED') == '1':
+ SConscript('csdk/security/unittest/SConscript')
# Build C++ unit tests
SConscript('unittests/SConscript')
SConscript('csdk/connectivity/test/SConscript')
# Build Security Resource Manager unit tests
- SConscript('csdk/security/unittest/SConscript')
+ if env.get('SECURED') == '1':
+ SConscript('csdk/security/unittest/SConscript')
# Build Provisioning API unit test
if env.get('SECURED') == '1':
{
}
+#ifdef WITH_CLOUD
void onObserve(const HeaderOptions, const OCRepresentation&, const int&, const int&)
{
}
+#endif
void directPairHandler(std::shared_ptr<OCDirectPairing> /*dev*/, OCStackResult /*res*/)
{
EXPECT_EQ(OC_STACK_OK, OCPlatform::unsubscribePresence(presenceHandle));
}
+#ifdef WITH_CLOUD
//SubscribeDevicePresence Test
TEST(SubscribeDevicePresenceTest, DISABLED_SubscribeDevicePresenceWithValidParameters)
{
hostAddress, queryParams, CT_DEFAULT, &onObserve));
EXPECT_EQ(OC_STACK_OK, OCPlatform::unsubscribePresence(presenceHandle));
}
+#endif
TEST(FindDirectPairingTest, FindDirectPairingNullCallback)
{
if target_os in ['linux'] and env.get('SIMULATOR', False):
SConscript('simulator/SConscript')
-# Build resource directory project
-if env.get('WITH_RD') == '1':
- SConscript('resource-directory/SConscript')
+ # Build resource directory project
+ #if env.get('WITH_RD') == '1':
+ #SConscript('resource-directory/SConscript')
# Build EasySetup module
if target_os in ['arduino', 'android', 'linux','tizen']:
const unsigned char *id = (unsigned char *)OCGetServerInstanceIDString();
NS_LOG_V(DEBUG, "DeviceID: %s", id);
- tagsPayload = OCCopyTagsResources(NULL, id,
- NULL, OC_DISCOVERABLE, 0, 0, NULL, NULL, OC_RD_PUBLISH_TTL);
+ tagsPayload = OCCopyTagsResources(NULL, id, OC_RD_PUBLISH_TTL);
if (!tagsPayload)
{
goto no_memory;
if (!linksPayload)
{
- linksPayload = OCCopyLinksResources(uri, rt, itf, NULL, 0, NULL,
+ /* Need to update with Cloud Interface Implementation.
+ *
+ linksPayload = OCCopyLinksResources(uri, rt, itf, NULL, 0, NULL,
NULL, j, mt);;
if (!linksPayload)
{
goto no_memory;
}
+ */
}
else
{
{
temp = temp->next;
}
+ /* Need to update with Cloud Interface Implementation.
+ *
temp->next = OCCopyLinksResources(uri, rt, itf, NULL, 0, NULL,
NULL, j, mt);
if (!temp->next)
{
goto no_memory;
}
+ */
}
OCFreeOCStringLL(rt);
OCFreeOCStringLL(itf);