Refactor oc_core_get_resource_by_uri()
@@ -292,11 +292,11 @@ oc_core_add_new_device(const char *uri, const char *rt, const char *name,
strlen(data_model_version));
oc_device_info[device_count].add_device_cb = add_device_cb;
- if (oc_get_con_res_announced) {
+ if (oc_get_con_res_announced()) {
/* Construct oic.wk.con resource for this device. */
oc_core_populate_resource(
- OCF_CON, device_count, "/" OC_NAME_CON_RES, OC_IF_RW | OC_IF_BASELINE, OC_IF_RW,
- OC_DISCOVERABLE | OC_OBSERVABLE, oc_core_con_handler_get,
+ OCF_CON, device_count, "/" OC_NAME_CON_RES, OC_IF_RW | OC_IF_BASELINE,
+ OC_IF_RW, OC_DISCOVERABLE | OC_OBSERVABLE, oc_core_con_handler_get,
oc_core_con_handler_post, oc_core_con_handler_post, 0, 1, "oic.wk.con");
}
@@ -437,22 +437,19 @@ oc_core_get_resource_by_index(int type, int device)
oc_resource_t *
oc_core_get_resource_by_uri(const char *uri, int device)
{
- int skip = 0;
+ int skip = 0, type = 0;
if (uri[0] != '/')
skip = 1;
if ((strlen(uri) - skip) == 5 && memcmp(uri + skip, "oic/p", 5) == 0) {
return &core_resources[0];
- } else {
- int type = 0;
- if ((strlen(uri) - skip) == 7 && memcmp(uri + skip, "oic/res", 7) == 0) {
+ } else if ((strlen(uri) - skip) == 7 &&
+ memcmp(uri + skip, "oic/res", 7) == 0) {
type = OCF_RES;
- } else if ((strlen(uri) - skip) == OC_NAMELEN_CON_RES &&
+ } else if (oc_get_con_res_announced() &&
+ (strlen(uri) - skip) == OC_NAMELEN_CON_RES &&
memcmp(uri + skip, OC_NAME_CON_RES, OC_NAMELEN_CON_RES) == 0) {
- if (oc_get_con_res_announced()) {
type = OCF_CON;
- }
- } else if ((strlen(uri) - skip) == 5 &&
- memcmp(uri + skip, "oic/d", 5) == 0) {
+ } else if ((strlen(uri) - skip) == 5 && memcmp(uri + skip, "oic/d", 5) == 0) {
type = OCF_D;
}
#ifdef OC_SECURITY
@@ -470,8 +467,9 @@ oc_core_get_resource_by_uri(const char *uri, int device)
type = OCF_SEC_CRED;
}
#endif /* OC_SECURITY */
+ else {
+ return NULL;
+ }
int res = OCF_D * device + type;
return &core_resources[res];
}
- return NULL;
-}
