#define OPTION_INFO_LENGTH 1024
#define NETWORK_INFO_LENGTH 1024
+#define COAP_PREFIX "coap://"
+#define COAP_PREFIX_LEN 7
+#define COAPS_PREFIX "coaps://"
+#define COAPS_PREFIX_LEN 8
+
uint16_t g_localSecurePort = SECURE_DEFAULT_PORT;
void request_handler(const CAEndpoint_t* object, const CARequestInfo_t* requestInfo);
static uint16_t g_clientMsgId;
static char *g_remoteAddress = NULL;
+typedef struct
+{
+ char ipAddress[CA_IPADDR_SIZE];
+ uint16_t port;
+} addressSet_t;
+
// init
JNIEXPORT void JNICALL
Java_org_iotivity_ca_service_RMInterface_setNativeResponseListener(JNIEnv *env, jobject obj,
- jobject listener)
+ jobject listener)
{
LOGI("setNativeResponseListener");
g_responseListenerObject = (*env)->NewGlobalRef(env, obj);
JNIEXPORT void JNICALL
Java_org_iotivity_ca_service_RMInterface_RMSendRequest(JNIEnv *env, jobject obj, jstring uri,
- jstring payload, jint selectedNetwork,
- jint isSecured, jint msgType)
+ jstring payload, jint selectedNetwork,
+ jint isSecured, jint msgType)
{
LOGI("selectedNetwork - %d", selectedNetwork);
CAResult_t res = get_network_type(selectedNetwork);
const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
LOGI("RMSendRequest - %s", strUri);
+ addressSet_t address = {};
+ parsing_coap_uri(strUri, &address);
+
//create remote endpoint
CAEndpoint_t* endpoint = NULL;
- res = CACreateEndpoint(CA_DEFAULT_FLAGS, g_selectedNwType, strUri, 0, &endpoint);
- //ReleaseStringUTFChars for strUri
- (*env)->ReleaseStringUTFChars(env, uri, strUri);
-
+ res = CACreateEndpoint(CA_DEFAULT_FLAGS, g_selectedNwType, (const char*)address.ipAddress,
+ address.port, &endpoint);
if (CA_STATUS_OK != res)
{
LOGE("Could not create remote end point");
+ (*env)->ReleaseStringUTFChars(env, uri, strUri);
return;
}
LOGE("token generate error!!");
// destroy remote endpoint
CADestroyEndpoint(endpoint);
+ (*env)->ReleaseStringUTFChars(env, uri, strUri);
return;
}
char resourceURI[RESOURCE_URI_LENGTH + 1] = { 0 };
get_resource_uri((const CAURI_t) strUri, resourceURI, RESOURCE_URI_LENGTH);
+ (*env)->ReleaseStringUTFChars(env, uri, strUri);
CAInfo_t requestData = { 0 };
requestData.token = token;
}
requestData.type = messageType;
+ requestData.resourceUri = (CAURI_t) malloc(sizeof(resourceURI));
+ if (NULL == requestData.resourceUri)
+ {
+ LOGE("Memory allocation failed!");
+ // destroy token
+ CADestroyToken(token);
+ // destroy remote endpoint
+ CADestroyEndpoint(endpoint);
+ free(requestData.payload);
+ return;
+ }
+ memcpy(requestData.resourceUri, resourceURI, sizeof(resourceURI));
CARequestInfo_t requestInfo = { 0 };
requestInfo.method = CA_GET;
CADestroyEndpoint(endpoint);
free(requestData.payload);
+ free(requestData.resourceUri);
}
JNIEXPORT void JNICALL
Java_org_iotivity_ca_service_RMInterface_RMSendReqestToAll(JNIEnv *env, jobject obj, jstring uri,
- jint selectedNetwork)
+ jint selectedNetwork)
{
LOGI("selectedNetwork - %d", selectedNetwork);
CAResult_t res = get_network_type(selectedNetwork);
return;
}
- const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
- LOGI("RMSendReqestToAll - %s", strUri);
-
// create remote endpoint
CAEndpoint_t *endpoint = NULL;
- res = CACreateEndpoint(CA_DEFAULT_FLAGS, g_selectedNwType, strUri, 0, &endpoint);
-
- //ReleaseStringUTFChars for strUri
- (*env)->ReleaseStringUTFChars(env, uri, strUri);
+ res = CACreateEndpoint(CA_DEFAULT_FLAGS, g_selectedNwType, NULL, 0, &endpoint);
if (CA_STATUS_OK != res)
{
requestData.payload = "Temp Json Payload";
requestData.type = CA_MSG_NONCONFIRM;
+ const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
+ LOGI("resourceUri - %s", strUri);
+ requestData.resourceUri = (CAURI_t)strUri;
+
CARequestInfo_t requestInfo = { 0 };
requestInfo.method = CA_GET;
requestInfo.isMulticast = true;
g_lastRequestTokenLength = tokenLength;
}
+ //ReleaseStringUTFChars for strUri
+ (*env)->ReleaseStringUTFChars(env, uri, strUri);
+
// destroy remote endpoint
CADestroyEndpoint(endpoint);
}
JNIEXPORT void JNICALL
Java_org_iotivity_ca_service_RMInterface_RMSendResponse(JNIEnv *env, jobject obj,
- jint selectedNetwork,
- jint isSecured, jint msgType,
- jint responseValue)
+ jint selectedNetwork,
+ jint isSecured, jint msgType,
+ jint responseValue)
{
LOGI("RMSendResponse");
CAInfo_t responseData = { 0 };
responseData.type = messageType;
responseData.messageId = g_clientMsgId;
+ responseData.resourceUri = (CAURI_t)g_resourceUri;
CAResponseInfo_t responseInfo = { 0 };
}
JNIEXPORT void JNICALL
+Java_org_iotivity_ca_service_RMInterface_RMAdvertiseResource(JNIEnv *env, jobject obj, jstring uri)
+{
+ LOGE("Java_org_iotivity_ca_service_RMInterface_RMAdvertiseResource not implemented");
+}
+
+JNIEXPORT void JNICALL
Java_org_iotivity_ca_service_RMInterface_RMSendNotification(JNIEnv *env, jobject obj, jstring uri,
- jstring payload, jint selectedNetwork,
- jint isSecured, jint msgType,
- jint responseValue)
+ jstring payload, jint selectedNetwork,
+ jint isSecured, jint msgType,
+ jint responseValue)
{
LOGI("selectedNetwork - %d", selectedNetwork);
const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
LOGI("RMSendNotification - %s", strUri);
+ addressSet_t address = {};
+ parsing_coap_uri(strUri, &address);
+
//create remote endpoint
CAEndpoint_t* endpoint = NULL;
- if (CA_STATUS_OK != CACreateEndpoint(CA_DEFAULT_FLAGS, g_selectedNwType, strUri, 0, &endpoint))
+ if (CA_STATUS_OK != CACreateEndpoint(CA_DEFAULT_FLAGS, g_selectedNwType,
+ (const char*)address.ipAddress,
+ address.port, &endpoint))
{
//ReleaseStringUTFChars for strUri
(*env)->ReleaseStringUTFChars(env, uri, strUri);
CAInfo_t responseData = { 0 };
responseData.token = token;
responseData.tokenLength = tokenLength;
+ responseData.resourceUri = (CAURI_t) malloc(sizeof(resourceURI));
+ if (NULL == responseData.resourceUri)
+ {
+ LOGE("Memory allocation failed!");
+ // destroy token
+ CADestroyToken(token);
+ // destroy remote endpoint
+ CADestroyEndpoint(endpoint);
+ return;
+ }
+ memcpy(responseData.resourceUri, resourceURI, sizeof(resourceURI));
if (1 == isSecured)
{
CADestroyToken(token);
// destroy remote endpoint
CADestroyEndpoint(endpoint);
+
+ free(responseData.resourceUri);
return;
}
snprintf(responseData.payload, length, SECURE_INFO_DATA, resourceURI, g_localSecurePort);
CADestroyToken(token);
// destroy remote endpoint
CADestroyEndpoint(endpoint);
+
+ free(responseData.resourceUri);
return;
}
snprintf(responseData.payload, length, NORMAL_INFO_DATA, resourceURI);
CADestroyEndpoint(endpoint);
free(responseData.payload);
+ free(responseData.resourceUri);
}
JNIEXPORT void JNICALL
-Java_org_iotivity_ca_service_RMInterface_RMSelectNetwork(JNIEnv *env, jobject obj, jint networkType)
+Java_org_iotivity_ca_service_RMInterface_RMSelectNetwork(JNIEnv *env, jobject obj,
+ jint networkType)
{
LOGI("RMSelectNetwork Type : %d", networkType);
}
JNIEXPORT void JNICALL
-Java_org_iotivity_ca_service_RMInterface_RMUnSelectNetwork(JNIEnv *env, jobject obj, jint networkType)
+Java_org_iotivity_ca_service_RMInterface_RMUnSelectNetwork(JNIEnv *env, jobject obj,
+ jint networkType)
{
LOGI("RMUnSelectNetwork Type : %d", networkType);
CAEndpoint_t *endpoint = NULL;
if (CA_STATUS_OK != CACreateEndpoint(CA_SECURE,
- object->adapter, uri, securePort, &endpoint))
+ object->adapter, object->addr, securePort, &endpoint))
{
LOGE("Failed to create duplicate of remote endpoint!");
free(uri);
return CA_STATUS_OK;
}
+
+
+void parsing_coap_uri(const char* uri, addressSet_t* address)
+{
+ if (NULL == uri || NULL == address)
+ {
+ LOGE("parameter is null");
+ return;
+ }
+
+ // parse uri
+ // #1. check prefix
+ uint8_t startIndex = 0;
+ if (strncmp(COAPS_PREFIX, uri, COAPS_PREFIX_LEN) == 0)
+ {
+ LOGI("uri has '%s' prefix", COAPS_PREFIX);
+ startIndex = COAPS_PREFIX_LEN;
+ }
+ else if (strncmp(COAP_PREFIX, uri, COAP_PREFIX_LEN) == 0)
+ {
+ LOGI("uri has '%s' prefix", COAP_PREFIX);
+ startIndex = COAP_PREFIX_LEN;
+ }
+
+ // #2. copy uri for parse
+ int32_t len = strlen(uri) - startIndex;
+
+ if (len <= 0)
+ {
+ LOGE("uri length is 0!");
+ return;
+ }
+
+ char *cloneUri = (char *) calloc(len + 1, sizeof(char));
+ if (NULL == cloneUri)
+ {
+ LOGE("Out of memory");
+ return;
+ }
+
+ memcpy(cloneUri, &uri[startIndex], sizeof(char) * len);
+ cloneUri[len] = '\0';
+
+ char *pAddress = cloneUri;
+ char *pResourceUri = NULL;
+
+ int32_t i = 0;
+ for (i = 0; i < len; i++)
+ {
+ if (cloneUri[i] == '/')
+ {
+ // separate
+ cloneUri[i] = 0;
+ pResourceUri = &cloneUri[i + 1];
+ break;
+ }
+ }
+ LOGI("pAddress : %s", pAddress);
+
+ int res = get_address_set(pAddress, address);
+ if (res == -1)
+ {
+ LOGE("address parse error");
+
+ free(cloneUri);
+ return;
+ }
+ return;
+}
+
+int get_address_set(const char *pAddress, addressSet_t* outAddress)
+{
+ if (NULL == pAddress || NULL == outAddress)
+ {
+ LOGE("parameter is null");
+ return -1;
+ }
+
+ int32_t len = strlen(pAddress);
+ int32_t isIp = 0;
+ int32_t ipLen = 0;
+
+ int32_t i = 0;
+ for (i = 0; i < len; i++)
+ {
+ if (pAddress[i] == '.')
+ {
+ isIp = 1;
+ }
+
+ // found port number start index
+ if (isIp && pAddress[i] == ':')
+ {
+ ipLen = i;
+ break;
+ }
+ }
+
+ if (isIp)
+ {
+ if(ipLen && ipLen < sizeof(outAddress->ipAddress))
+ {
+ strncpy(outAddress->ipAddress, pAddress, ipLen);
+ outAddress->ipAddress[ipLen] = '\0';
+ }
+ else if (!ipLen && len < sizeof(outAddress->ipAddress))
+ {
+ strncpy(outAddress->ipAddress, pAddress, len);
+ outAddress->ipAddress[len] = '\0';
+ }
+ else
+ {
+ LOGE("IP Address too long: %d", ipLen==0 ? len : ipLen);
+ return -1;
+ }
+
+ if (ipLen > 0)
+ {
+ outAddress->port = atoi(pAddress + ipLen + 1);
+ }
+ }
+ else
+ {
+ strncpy(outAddress->ipAddress, pAddress, len);
+ outAddress->ipAddress[len] = '\0';
+ }
+
+ return isIp;
+}
private final static String TAG = "MainActivity";
- private final CharSequence[] mNetworkCheckBoxItems = { Network.IPV4.name(),
- Network.IPV6.name(), Network.EDR.name(), Network.LE.name()};
+ private final CharSequence[] mNetworkCheckBoxItems = { Network.IP.name(),
+ Network.LE.name(), Network.EDR.name()};
private final CharSequence[] mDTLSCheckBoxItems = { DTLS.UNSECURED.name(),
DTLS.SECURED.name() };
};
private enum Network {
- IPV4, IPV6, EDR, LE
+ IP, LE, EDR
};
private enum DTLS {
false, false, false, false
};
- private int mSelectedItems[] = { 0, 0, 0, 0 };
+ private int mSelectedItems[] = { 0, 0, 0 };
- private int mUnSelectedItems[] = { 0, 0, 0, 0 };
+ private int mUnSelectedItems[] = { 0, 0, 0 };
private Mode mCurrentMode = Mode.UNKNOWN;
/**
* Defined ConnectivityType in cacommon.c
*
- * CA_IPV4 = (1 << 0) CA_IPV6 = (1 << 1) CA_EDR = (1 << 2) CA_LE = (1 << 3)
+ * CA_IP = (1 << 0) CA_LE = (1 << 2) CA_EDR = (1 << 3)
*/
- private int CA_IPV4 = (1 << 0);
- private int CA_IPV6 = (1 << 1);
+ private int CA_IP = (1 << 0);
+ private int CA_LE = (1 << 1);
private int CA_EDR = (1 << 2);
- private int CA_LE = (1 << 3);
private int isSecured = 0;
private int msgType = 1;
private int responseValue = 0;
public void onClick(View v) {
DLog.v(TAG, "FindResource click");
- RM.RMFindResource(mUri_ed.getText().toString());
-
+ RM.RMSendReqestToAll(mReqToAllData_ed.getText().toString(), selectedNetwork);
}
};
for (int i = 0; i < mSelectedItems.length; i++) {
if (mSelectedItems[i] == 1) {
- if(i != 1)
- interestedNetwork |= (1 << i);
- else
- checkNotSupportedTransport("Not Supported Transport");
+ interestedNetwork |= (1 << i);
}
}
if(0 != interestedNetwork)
for (int i = 0; i < mUnSelectedItems.length; i++) {
if (mUnSelectedItems[i] == 1) {
- if (i != 1)
- uninterestedNetwork |= (1 << i);
- else
- checkNotSupportedTransport("Not Supported Transport");
- mUnSelectedItems[i] = 0;
+ uninterestedNetwork |= (1 << i);
}
}
if(0 != uninterestedNetwork)
}).show();
}
- private void checkNotSupportedTransport(String title) {
-
- AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
- builder.setTitle(title).
- setMessage("Selected Transport Not Supported")
- .setPositiveButton("OK", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
- }
- }).show();
- }
-
private void checkMsgSecured(String title) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
@Override
public void onClick(DialogInterface dialog, int which) {
- if (selectedNetworkType == Network.IPV4.ordinal()) {
- selectedNetwork = CA_IPV4;
- DLog.v(TAG, "Selected Network is CA_IPV4");
- } else if (selectedNetworkType == Network.EDR.ordinal()) {
- selectedNetwork = CA_EDR;
- DLog.v(TAG, "Selected Network is EDR");
+ if (selectedNetworkType == Network.IP.ordinal()) {
+ selectedNetwork = CA_IP;
+ DLog.v(TAG, "Selected Network is CA_IP");
} else if (selectedNetworkType == Network.LE.ordinal()) {
selectedNetwork = CA_LE;
DLog.v(TAG, "Selected Network is LE");
- }
- else {
+ } else if (selectedNetworkType == Network.EDR.ordinal()) {
+ selectedNetwork = CA_EDR;
+ DLog.v(TAG, "Selected Network is EDR");
+ } else {
DLog.v(TAG, "Selected Network is NULL");
selectedNetwork = -1;
}