From d3d38a603811cd74634d9202936b98f7a412479f Mon Sep 17 00:00:00 2001 From: "ch79.cho" Date: Mon, 8 Aug 2016 16:12:52 +0900 Subject: [PATCH] Create topic resource Topic resource is created in resource module with defined resource attribute. Change-Id: I1b4818f01349669f66503100c7edf28966e7eada Signed-off-by: ch79.cho Reviewed-on: https://gerrit.iotivity.org/gerrit/9779 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- service/notification/include/NSCommon.h | 41 ++++++++++++--- service/notification/src/common/NSConstants.h | 23 ++++++++- service/notification/src/common/NSStructs.h | 16 ++++++ .../notification/src/provider/NSProviderListener.c | 58 +++++++++++++++++++++- .../notification/src/provider/NSProviderListener.h | 3 ++ .../notification/src/provider/NSProviderResource.c | 42 ++++++++++++++++ .../notification/src/provider/NSProviderResource.h | 2 + 7 files changed, 175 insertions(+), 10 deletions(-) diff --git a/service/notification/include/NSCommon.h b/service/notification/include/NSCommon.h index fa3d1a0..6233554 100644 --- a/service/notification/include/NSCommon.h +++ b/service/notification/include/NSCommon.h @@ -29,12 +29,18 @@ #include +#define NS_UUID_STRING_SIZE 37 + #define NS_ATTRIBUTE_POLICY "ACCEPTER" #define NS_ATTRIBUTE_MESSAGE "MESSAGE_URI" #define NS_ATTRIBUTE_SYNC "SYNC_URI" #define NS_ATTRIBUTE_ACCPETANCE "ACCEPTANCE" #define NS_ATTRIBUTE_MESSAGE_ID "MESSAGE_ID" #define NS_ATTRIBUTE_PROVIDER_ID "PROVIDER_ID" +#define NS_ATTRIBUTE_CONSUMER_ID "CONSUMER_ID" +#define NS_ATTRIBUTE_TOPIC_LIST "TOPIC_LIST" +#define NS_ATTRIBUTE_TOPIC_NAME "TOPIC_NAME" +#define NS_ATTRIBUTE_TOPIC_SELECTION "TOPIC_STATE" #define NS_ATTRIBUTE_TITLE "TITLE" #define NS_ATTRIBUTE_TEXT "CONTENTTEXT" #define NS_ATTRIBUTE_SOURCE "SOURCE" @@ -53,8 +59,6 @@ typedef enum eResult NS_ERROR = 200, NS_SUCCESS = 300, NS_FAIL = 400, - NS_ALLOW = 500, - NS_DENY = 600, } NSResult; @@ -98,7 +102,7 @@ typedef enum */ typedef struct { - char consumerId[37]; + char consumerId[NS_UUID_STRING_SIZE]; } NSConsumer; @@ -107,7 +111,7 @@ typedef struct */ typedef struct { - char providerId[37]; + char providerId[NS_UUID_STRING_SIZE]; } NSProvider; @@ -127,7 +131,7 @@ typedef struct { //Mandatory uint64_t messageId; - char providerId[37]; + char providerId[NS_UUID_STRING_SIZE]; //optional NSMessageType type; @@ -146,10 +150,35 @@ typedef struct typedef struct { uint64_t messageId; - char providerId[37]; + char providerId[NS_UUID_STRING_SIZE]; NSSyncType state; } NSSyncInfo; +/** + * Notification topic + */ +typedef enum +{ + NS_TOPIC_CREATED = 0, + NS_TOPIC_SUBSCRIBED = 1, + NS_TOPIC_UNSUBSCRIBED = 2, + +} NSTopicState; + +typedef struct +{ + char * topicName; + NSTopicState state; + +} NSTopic; + +typedef struct +{ + char consumerId[NS_UUID_STRING_SIZE]; + NSTopic ** topics; + +} NSTopicList; + #endif /* _NS_COMMON_H_ */ diff --git a/service/notification/src/common/NSConstants.h b/service/notification/src/common/NSConstants.h index c263fba..56320ee 100644 --- a/service/notification/src/common/NSConstants.h +++ b/service/notification/src/common/NSConstants.h @@ -51,9 +51,9 @@ #define NS_TAG "IOT_NOTI" // SCHEDULE // -#define THREAD_COUNT 4 +#define THREAD_COUNT 5 -// NOTIOBJ // +// NOTIOBJ // #define NOTIOBJ_TITLE_KEY "title" #define NOTIOBJ_ID_KEY "id" #define NOTOOBJ_CONTENT_KEY "contentText" @@ -65,16 +65,19 @@ #define SCHEDULER_TAG "NS_PROVIDER_SCHEDULER" #define LISTENER_TAG "NS_PROVIDER_LISTENER" #define RESOURCE_TAG "NS_PROVIDER_RESOURCE" +#define TOPIC_TAG "NS_PROVIDER_TOPIC" #define NS_ROOT_TYPE "oic.r.notification" #define NS_COLLECTION_MESSAGE_TYPE "oic.r.notification.message" #define NS_COLLECTION_SYNC_TYPE "oic.r.notification.sync" +#define NS_COLLECTION_TOPIC_TYPE "oic.r.notification.topic" #define NS_DEFAULT_INTERFACE "oic.if.baseline" #define NS_ROOT_URI "/notification" #define NS_COLLECTION_MESSAGE_URI "/notification/message" #define NS_COLLECTION_SYNC_URI "/notification/sync" +#define NS_COLLECTION_TOPIC_URI "/notification/topic" #define NS_QUERY_SEPARATOR "&;" #define NS_KEY_VALUE_DELIMITER "=" @@ -182,6 +185,7 @@ typedef enum eSchedulerType DISCOVERY_SCHEDULER = 1, SUBSCRIPTION_SCHEDULER = 2, NOTIFICATION_SCHEDULER = 3, + TOPIC_SCHEDULER = 4, } NSSchedulerType; typedef enum eTaskType @@ -227,6 +231,10 @@ typedef enum eTaskType TASK_CB_SUBSCRIPTION = 10000, TASK_CB_SYNC = 10001, + TASK_SEND_TOPICS = 11000, + TASK_SELECT_TOPICS = 11001, + TASK_SET_TOPICS = 11002, + } NSTaskType; typedef enum eCache @@ -240,6 +248,7 @@ typedef enum eCacheType { NS_PROVIDER_CACHE_SUBSCRIBER = 1000, NS_PROVIDER_CACHE_MESSAGE = 1001, + NS_PROVIDER_CACHE_TOPIC = 1002, NS_CONSUMER_CACHE_PROVIDER = 2000, NS_CONSUMER_CACHE_MESSAGE = 2001, @@ -249,6 +258,16 @@ typedef enum eResourceType { NS_RESOURCE_MESSAGE = 1000, NS_RESOURCE_SYNC = 1001, + NS_RESOURCE_TOPIC = 1002, } NSResourceType; +typedef enum eResponse +{ + NS_ALLOW = 1, + NS_DENY = 2, + NS_TOPIC = 3, + +} NSResponse; + + #endif /* _NS_CONSTANTS_H_ */ diff --git a/service/notification/src/common/NSStructs.h b/service/notification/src/common/NSStructs.h index 752f173..fb207c6 100644 --- a/service/notification/src/common/NSStructs.h +++ b/service/notification/src/common/NSStructs.h @@ -71,10 +71,19 @@ typedef struct typedef struct { + char consumerId[37]; + NSTopicList ** topicList; +} NSCacheTopicData; + +typedef struct +{ OCResourceHandle handle; int accepter; char * message_uri; char * sync_uri; + + //optional + char * topic_uri; } NSNotificationResource; typedef struct @@ -103,6 +112,13 @@ typedef struct typedef struct { + OCResourceHandle handle; + char consumerId[37]; + NSTopicList ** TopicList; +} NSTopicResource; + +typedef struct +{ char providerId[37]; char * providerName; char * userInfo; diff --git a/service/notification/src/provider/NSProviderListener.c b/service/notification/src/provider/NSProviderListener.c index e6fb4c3..3b0e6bc 100644 --- a/service/notification/src/provider/NSProviderListener.c +++ b/service/notification/src/provider/NSProviderListener.c @@ -127,8 +127,8 @@ OCEntityHandlerResult NSEntityHandlerSyncCb(OCEntityHandlerFlag flag, if (OC_REST_POST == entityHandlerRequest->method) { /** Receive sync data from consumer which read or dismiss notification message. - And broadcast the sync data to all subscribers including provider app - to synchronize the notification message status. */ + And broadcast the sync data to all subscribers including provider app + to synchronize the notification message status. */ NS_LOG(DEBUG, "NSEntityHandlerSyncCb - OC_REST_POST"); @@ -171,6 +171,60 @@ OCEntityHandlerResult NSEntityHandlerSyncCb(OCEntityHandlerFlag flag, return ehResult; } +OCEntityHandlerResult NSEntityHandlerTopicCb(OCEntityHandlerFlag flag, + OCEntityHandlerRequest *entityHandlerRequest, void* callback) +{ + NS_LOG(DEBUG, "NSEntityHandlerTopicCb - IN"); + OCEntityHandlerResult ehResult = OC_EH_OK; + + (void)callback; + + // Validate pointer + if (!entityHandlerRequest) + { + NS_LOG(ERROR, "Invalid request pointer"); + return OC_EH_ERROR; + } + + if (flag & OC_REQUEST_FLAG) + { + NS_LOG(DEBUG, "Flag includes OC_REQUEST_FLAG"); + + if (OC_REST_GET == entityHandlerRequest->method) + { + NS_LOG(DEBUG, "NSEntityHandlerTopicCb - OC_REST_GET"); + + // send consumer's interesting topic list if consumer id exists + // otherwise send created / updated topic list + + NSPushQueue(TOPIC_SCHEDULER, TASK_SEND_TOPICS, + NSCopyOCEntityHandlerRequest(entityHandlerRequest)); + + ehResult = OC_EH_OK; + } + else if (OC_REST_POST == entityHandlerRequest->method) + { + // Receive interesting topic list from consumers + NS_LOG(DEBUG, "NSEntityHandlerTopicCb - OC_REST_POST"); + + // Send topic updated message(id=TOPIC) to the consumer who request to post. + NSPushQueue(TOPIC_SCHEDULER, TASK_SELECT_TOPICS, + NSCopyOCEntityHandlerRequest(entityHandlerRequest)); + + ehResult = OC_EH_OK; + } + else + { + NS_LOG_V(DEBUG, "Received unsupported method %d from client", + entityHandlerRequest->method); + ehResult = OC_EH_OK; + } + } + + NS_LOG(DEBUG, "NSEntityHandlerTopicCb - OUT"); + return ehResult; +} + void NSProviderConnectionStateListener(const CAEndpoint_t * info, bool connected) { diff --git a/service/notification/src/provider/NSProviderListener.h b/service/notification/src/provider/NSProviderListener.h index e07c8e1..ddf5671 100644 --- a/service/notification/src/provider/NSProviderListener.h +++ b/service/notification/src/provider/NSProviderListener.h @@ -45,6 +45,9 @@ OCEntityHandlerResult NSEntityHandlerMessageCb(OCEntityHandlerFlag flag, OCEntityHandlerResult NSEntityHandlerSyncCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest *entityHandlerRequest, void* callback); +OCEntityHandlerResult NSEntityHandlerTopicCb(OCEntityHandlerFlag flag, + OCEntityHandlerRequest *entityHandlerRequest, void* callback); + void NSProviderConnectionStateListener(const CAEndpoint_t * info, bool isConnected); void NSProviderAdapterStateListener(CATransportAdapter_t adapter, bool enabled); diff --git a/service/notification/src/provider/NSProviderResource.c b/service/notification/src/provider/NSProviderResource.c index 78e593e..0e4030f 100644 --- a/service/notification/src/provider/NSProviderResource.c +++ b/service/notification/src/provider/NSProviderResource.c @@ -24,6 +24,7 @@ NSNotificationResource NotificationResource; NSMessageResource NotificationMessageResource; NSSyncResource NotificationSyncResource; +NSTopicResource NotificationTopicResource; #ifdef WITH_CLOUD OCStackApplicationResult NSHandlePublishCb(void *ctx, OCDoHandle handle, @@ -74,6 +75,7 @@ NSResult NSCreateResource(char *uri) NotificationResource.accepter = 0; NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI; NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI; + NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI; NotificationResource.handle = NULL; if (OCCreateResource(&NotificationResource.handle, NS_ROOT_TYPE, NS_DEFAULT_INTERFACE, @@ -119,6 +121,20 @@ NSResult NSCreateResource(char *uri) return NS_ERROR; } } + else if (strcmp(uri, NS_COLLECTION_TOPIC_URI) == 0) + { + (NotificationTopicResource.consumerId)[0] = '\0'; + NotificationTopicResource.TopicList = NULL; + NotificationTopicResource.handle = NULL; + + if (OCCreateResource(&(NotificationTopicResource.handle), NS_COLLECTION_TOPIC_TYPE, + NS_DEFAULT_INTERFACE, NS_COLLECTION_TOPIC_URI, NSEntityHandlerTopicCb, NULL, + OC_DISCOVERABLE) != OC_STACK_OK) + { + NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource"); + return NS_ERROR; + } + } else { NS_LOG(ERROR, "Fail to create resource with invalid URI"); @@ -133,6 +149,12 @@ NSResult NSRegisterResource() { NS_LOG(DEBUG, "NSRegisterResource - IN"); + if (NSCreateResource(NS_COLLECTION_TOPIC_URI) != NS_OK) + { + NS_LOG(ERROR, "Fail to register Topic Resource"); + return NS_ERROR; + } + if (NSCreateResource(NS_COLLECTION_SYNC_URI) != NS_OK) { NS_LOG(ERROR, "Fail to register Sync Resource"); @@ -177,9 +199,16 @@ NSResult NSUnRegisterResource() return NS_ERROR; } + if (OCDeleteResource(NotificationTopicResource.handle) != OC_STACK_OK) + { + NS_LOG(ERROR, "Fail to Delete Notification Topic Resource"); + return NS_ERROR; + } + NotificationResource.handle = NULL; NotificationMessageResource.handle = NULL; NotificationSyncResource.handle = NULL; + NotificationTopicResource.handle = NULL; NS_LOG(DEBUG, "NSUnRegisterResource - OUT"); return NS_OK; @@ -192,6 +221,7 @@ NSResult NSPutNotificationResource(int accepter, OCResourceHandle * handle) NotificationResource.accepter = accepter; NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI; NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI; + NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI; *handle = NotificationResource.handle; @@ -239,3 +269,15 @@ NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle) NS_LOG(DEBUG, "NSPutSyncResource - OUT"); return NS_OK; } + +NSResult NSPutTopicResource(NSTopicList **topicList, OCResourceHandle * handle) +{ + NS_LOG(DEBUG, "NSPutTopicResource - IN"); + + (void) topicList; + + *handle = NotificationTopicResource.handle; + + NS_LOG(DEBUG, "NSPutTopicResource - OUT"); + return NS_OK; +} diff --git a/service/notification/src/provider/NSProviderResource.h b/service/notification/src/provider/NSProviderResource.h index 4ee6621..7c868c2 100644 --- a/service/notification/src/provider/NSProviderResource.h +++ b/service/notification/src/provider/NSProviderResource.h @@ -42,4 +42,6 @@ NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle); NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle); +NSResult NSPutTopicResource(NSTopicList **topicList, OCResourceHandle * handle); + #endif /* _NS_PROVIDER_RESOURCE_H_ */ -- 2.7.4