error handling of consumer was modified to macro functions.
TASK_CONSUMER_RECV_NOTIFICATION is renamed to MESSAGE.
POST callback for sendSyncInfo request is added.
it is bug for fail of sending POST msg since listener module is removed.
Change-Id: Ibad81bc7ee8e51a63feb640f55711a98fa022eda
Signed-off-by: KIM JungYong <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/8553
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Tested-by: Uze Choi <uzchoi@samsung.com>
TASK_CONSUMER_REQ_DISCOVER = 8001,
TASK_CONSUMER_REQ_SUBSCRIBE = 8002,
TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL = 8003,
- TASK_CONSUMER_RECV_NOTIFICATION = 8101,
+ TASK_CONSUMER_RECV_MESSAGE = 8101,
TASK_EVENT_CONNECTED = 9000,
TASK_EVENT_DISCONNECTED = 9001,
void NSDiscoveredProvider(NSProvider * provider)
{
- if (!NSThreadInit(NSDiscoveredProviderFunc, (void *) provider))
- {
- NS_LOG(ERROR, "execute discovered provider callback fail");
- return;
- }
+ NSConsumerThread * thread = NSThreadInit(NSDiscoveredProviderFunc, (void *) provider);
+ NS_VERTIFY_NOT_NULL_V(thread);
}
NSSyncCallback * NSGetBoneNotificationSyncCb()
void NSNotificationSync(NSProvider * provider, NSSync * sync)
{
NSSyncData * obj = (NSSyncData *)OICMalloc(sizeof(NSSyncData));
+ NS_VERTIFY_NOT_NULL_V(obj);
obj->provider = provider;
obj->sync = sync;
- if (!NSThreadInit(NSNotificationSyncFunc, (void *) obj))
- {
- NS_LOG(ERROR, "execute noti post callback fail");
- return;
- }
+ NSConsumerThread * thread = NSThreadInit(NSNotificationSyncFunc, (void *) obj);
+ NS_VERTIFY_NOT_NULL_V(thread);
}
NSNotificationReceivedCallback * NSGetBoneNotificationPostedCb()
void NSNotificationPost(NSProvider * provider, NSMessage * msg)
{
NSMessageData * obj = (NSMessageData *)OICMalloc(sizeof(NSMessageData));
- if (!obj)
- {
- NS_LOG(ERROR, "NSMessageData allocation is failed");
- return;
- }
+ NS_VERTIFY_NOT_NULL_V(obj);
obj->provider = provider;
obj->msg = msg;
- if (!NSThreadInit(NSNotificationPostFunc, (void *) obj))
- {
- NS_LOG(ERROR, "execute noti post callback fail");
- return;
- }
+ NSConsumerThread * thread = NSThreadInit(NSNotificationPostFunc, (void *) obj);
+ NS_VERTIFY_NOT_NULL_V(thread);
}
NSTask * NSMakeTask(NSTaskType type, void * data)
{
NSTask * retTask = OICMalloc(sizeof(NSTask));
- if (!retTask)
- {
- return NULL;
- }
+ NS_VERTIFY_NOT_NULL(retTask, NULL);
retTask->taskType = type;
retTask->taskData = data;
NSMessage_consumer * NSCopyMessage(NSMessage_consumer * msg)
{
- if (!msg)
- {
- return NULL;
- }
+ NS_VERTIFY_NOT_NULL(msg, NULL);
NSMessage_consumer * newMsg = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer));
- if (!newMsg)
- {
- return NULL;
- }
+ NS_VERTIFY_NOT_NULL(newMsg, NULL);
+
+ newMsg->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
+ NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(newMsg, NULL, OICFree(newMsg));
+ memcpy(newMsg->addr, msg->addr, sizeof(OCDevAddr));
newMsg->mId = OICStrdup(msg->mId);
newMsg->mTitle = OICStrdup(msg->mTitle);
newMsg->mContentText = OICStrdup(msg->mContentText);
newMsg->mSource = OICStrdup(msg->mSource);
- newMsg->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
- if (!newMsg->addr)
- {
- NS_LOG(ERROR, "OCDevAddr allocation is failed");
- }
- memcpy(newMsg->addr, msg->addr, sizeof(OCDevAddr));
return newMsg;
}
#define NS_DISCOVER_QUERY "/oic/res?rt=oic.r.notification"
+#define NS_VERTIFY_NOT_NULL_V(obj) { if ((obj) == NULL) \
+ { \
+ NS_LOG_V(ERROR, "%s : obj is null", __func__); \
+ return; \
+ }}
+
#define NS_VERTIFY_NOT_NULL(obj, retVal) { if ((obj) == NULL) \
{ \
NS_LOG_V(ERROR, "%s : obj is null", __func__); \
return (retVal); \
}}
-#define NS_VERTIFY_STACK_OK(obj, retVal) { if ((obj) != OC_STACK_OK) \
+#define NS_VERTIFY_STACK_OK_V(obj) { OCStackResult _ret = (obj); \
+ if ( _ret != OC_STACK_OK) \
+ { \
+ NS_LOG_V(ERROR, "%s : obj is not OC_STACK_OK : %d", __func__, _ret); \
+ return; \
+ }}
+
+#define NS_VERTIFY_STACK_OK(obj, retVal) { OCStackResult _ret = (obj); \
+ if ( _ret != OC_STACK_OK) \
+ { \
+ NS_LOG_V(ERROR, "%s : obj is not OC_STACK_OK : %d", __func__, _ret); \
+ return (retVal); \
+ }}
+
+#define NS_VERTIFY_STACK_OK_WITH_POST_CLEANING(obj, retVal, func) { \
+ OCStackResult _ret = (obj); \
+ if ( _ret != OC_STACK_OK) \
{ \
- NS_LOG_V(ERROR, "%s : obj is not OC_STACK_OK", __func__); \
+ NS_LOG_V(ERROR, "%s : obj is not OC_STACK_OK : %d", __func__, _ret); \
+ (func); \
return (retVal); \
}}
NSMessage_consumer * NSGetNSMessage(OCClientResponse * clientResponse);
NSSync * NSGetNSSyncInfo(OCClientResponse * clientResponse);
-
NSProvider * NSGetNSProvider(OCClientResponse * clientResponse);
+OCRepPayload * NSGetPayloadofSyncInfo(NSMessage_consumer * message, int type);
+
+
NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type);
+OCStackResult NSSendSyncInfo(NSMessage_consumer * message, int type);
+
NSResult NSConsumerSubscribeProvider(NSProvider * provider)
{
- if (OC_STACK_OK != NSInvokeRequest(&(provider->messageHandle),
- OC_REST_OBSERVE, (OCDevAddr *) provider->mUserData,
- provider->messageUri, NULL, NSConsumerMessageListener, NULL))
- {
- return NS_ERROR;
- }
+ OCStackResult ret = NSInvokeRequest(&(provider->messageHandle),
+ OC_REST_OBSERVE, (OCDevAddr *) provider->mUserData,
+ provider->messageUri, NULL, NSConsumerMessageListener, NULL);
+ NS_VERTIFY_STACK_OK(ret, NS_ERROR);
- if (OC_STACK_OK != NSInvokeRequest(&(provider->syncHandle),
- OC_REST_OBSERVE, (OCDevAddr *) provider->mUserData,
- provider->syncUri, NULL, NSConsumerSyncInfoListener, NULL))
- {
- return NS_ERROR;
- }
+ ret = NSInvokeRequest(&(provider->syncHandle),
+ OC_REST_OBSERVE, (OCDevAddr *) provider->mUserData,
+ provider->syncUri, NULL, NSConsumerSyncInfoListener, NULL);
+ NS_VERTIFY_STACK_OK(ret, NS_ERROR);
return NS_OK;
}
+OCStackApplicationResult NSConsumerPostResultCheck(
+ void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
+{
+ (void) ctx;
+ (void) handle;
+
+ NS_VERTIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
+ NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
+
+ return OC_STACK_KEEP_TRANSACTION;
+}
+
NSResult NSConsumerPostProvider(OCDevAddr * addr, OCPayload * payload, const char * uri)
{
- if (OC_STACK_OK != NSInvokeRequest(NULL, OC_REST_POST, addr, uri, payload, NULL, NULL))
- {
- return NS_ERROR;
- }
+ OCStackResult ret = NSInvokeRequest(NULL, OC_REST_POST, addr, uri, payload,
+ NSConsumerPostResultCheck, NULL);
+ NS_VERTIFY_STACK_OK(ret, NS_ERROR);
return NS_OK;
}
NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
NSSync * newNoti = NULL;
+
+ NS_LOG(ERROR, "get provider");
NSProvider * provider = NSGetNSProvider(clientResponse);
- if (!provider)
- {
- NS_LOG(ERROR, "getting provider is failed");
- return OC_STACK_KEEP_TRANSACTION;
- }
+ NS_VERTIFY_NOT_NULL(provider, OC_STACK_KEEP_TRANSACTION);
newNoti = NSGetNSSyncInfo(clientResponse);
- if (!newNoti)
- {
- return OC_STACK_KEEP_TRANSACTION;
- }
+ NS_VERTIFY_NOT_NULL(newNoti, OC_STACK_KEEP_TRANSACTION);
NSTaskType taskType = TASK_RECV_READ;
NSNotificationSync(provider, newNoti);
- if (NS_OK != NSPushToCache(clientResponse, taskType))
- {
- return OC_STACK_KEEP_TRANSACTION;
- }
+ NSResult ret = NSPushToCache(clientResponse, taskType);
+ NS_VERTIFY_NOT_NULL(ret == NS_OK ? (void *)1 : NULL, OC_STACK_KEEP_TRANSACTION);
return OC_STACK_KEEP_TRANSACTION;
}
NS_VERTIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
+ NS_LOG(DEBUG, "build NSProvider");
NSProvider * provider = NSGetNSProvider(clientResponse);
- if (!provider)
- {
- NS_LOG(ERROR, "getting provider is failed");
- return OC_STACK_KEEP_TRANSACTION;
- }
+ NS_VERTIFY_NOT_NULL(provider, OC_STACK_KEEP_TRANSACTION);
+ NS_LOG(DEBUG, "build NSMessage");
NSMessage_consumer * newNoti = NSGetNSMessage(clientResponse);
- if (!newNoti)
- {
- return OC_STACK_KEEP_TRANSACTION;
- }
+ NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(
+ newNoti, OC_STACK_KEEP_TRANSACTION, OICFree(provider));
if (!strcmp(newNoti->mId, NS_MESSAGE_ACCEPTANCE))
{
+ // TODO update provider list.
NS_LOG(DEBUG, "Receive Subscribe confirm");
OICFree(provider->mUserData);
OICFree(provider);
return OC_STACK_KEEP_TRANSACTION;
}
- NSTaskType taskType = TASK_CONSUMER_RECV_NOTIFICATION;
-
NS_LOG(DEBUG, "newNoti->type == Notification");
NSNotificationPost(provider, (NSMessage *) newNoti);
- if (NS_OK != NSPushToCache(clientResponse, taskType))
- {
- NSRemoveMessage(newNoti);
- return OC_STACK_KEEP_TRANSACTION;
- }
+ NSResult ret = NSPushToCache(clientResponse, TASK_CONSUMER_RECV_MESSAGE);
+ NS_VERTIFY_NOT_NULL(ret == NS_OK ? (void *)1 : NULL, OC_STACK_KEEP_TRANSACTION);
return OC_STACK_KEEP_TRANSACTION;
}
NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type)
{
NSMessage_consumer * cachedNoti = NSGetNSMessage(clientResponse);
- if (!cachedNoti)
- {
- return NS_ERROR;
- }
+ NS_LOG(DEBUG, "build NSMessage");
+ NS_VERTIFY_NOT_NULL(cachedNoti, NS_ERROR);
+
+ NS_LOG(DEBUG, "build NSTask");
NSTask * task = NSMakeTask(type, (void *) cachedNoti);
- if (!task)
- {
- NS_LOG(ERROR, "NSTask allocation fail");
- NSRemoveMessage(cachedNoti);
- return NS_ERROR;
- }
+ NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveMessage(cachedNoti));
+
NSConsumerPushEvent(task);
return NS_OK;
NSMessage_consumer * NSGetNSMessage(OCClientResponse * clientResponse)
{
- if(!clientResponse->payload)
- {
- return NULL;
- }
+ NS_VERTIFY_NOT_NULL(clientResponse->payload, NULL);
+
+ NS_LOG(DEBUG, "get id");
+ OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
+ char * id = NULL;
+ bool getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_ID, &id);
+ NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
+ NS_LOG(DEBUG, "create NSMessage");
NSMessage_consumer * retNoti = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer));
- if (!retNoti)
- {
- return NULL;
- }
+ NS_VERTIFY_NOT_NULL(retNoti, NULL);
- retNoti->mId = NULL;
+ retNoti->mId = id;
retNoti->mTitle = NULL;
retNoti->mContentText = NULL;
retNoti->mSource = NULL;
- OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
- if (!OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_ID, &retNoti->mId))
- {
- NS_LOG(ERROR, "id of received notification is null");
- OICFree(retNoti);
- return NULL;
- }
-
OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TITLE, &retNoti->mTitle);
OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TEXT, &retNoti->mContentText);
OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_SOURCE, &retNoti->mSource);
NS_LOG_V(DEBUG, "Msg Content : %s", retNoti->mContentText);
NS_LOG_V(DEBUG, "Msg Source : %s", retNoti->mSource);
+ NS_LOG(DEBUG, "copy target address");
retNoti->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
+ NS_VERTIFY_NOT_NULL(retNoti->addr, NULL);
memcpy(retNoti->addr, clientResponse->addr, sizeof(OCDevAddr));
retNoti->type = Notification;
NSSync * NSGetNSSyncInfo(OCClientResponse * clientResponse)
{
- if(!clientResponse->payload)
- {
- return NULL;
- }
- NSSync * retSync = (NSSync *)OICMalloc(sizeof(NSSync));
- if (!retSync)
- {
- return NULL;
- }
-
- retSync->mMessageId = NULL;
- retSync->mState = Notification_Read;
+ NS_VERTIFY_NOT_NULL(clientResponse->payload, NULL);
OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
- if (!OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_ID, &retSync->mMessageId))
- {
- NS_LOG(ERROR, "id of received sync is null");
- OICFree(retSync);
- return NULL;
- }
- int64_t state;
- if (!OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_STATE, & state))
- {
- NS_LOG(ERROR, "state of received sync is null");
- OICFree(retSync->mMessageId);
- OICFree(retSync);
- return NULL;
- }
+ NS_LOG(DEBUG, "get state");
+ int64_t state = 0;
+ bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_STATE, & state);
+ NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
+
+ NS_LOG(DEBUG, "get id");
+ char * id = NULL;
+ getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_ID, &id);
+ NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
+
+ NS_LOG(DEBUG, "create NSSyncInfo");
+ NSSync * retSync = (NSSync *)OICMalloc(sizeof(NSSync));
+ NS_VERTIFY_NOT_NULL(retSync, NULL);
+
+ retSync->mMessageId = id;
retSync->mState = (NSSyncTypes) state;
NS_LOG_V(DEBUG, "Sync ID : %s", retSync->mMessageId);
NSProvider * NSGetNSProvider(OCClientResponse * clientResponse)
{
+ NS_LOG(DEBUG, "create NSProvider");
NSProvider * newProvider = (NSProvider *)OICMalloc(sizeof(NSProvider));
- if (!newProvider)
- {
- NS_LOG(DEBUG, "NSProvider allocation fail");
- return NULL;
- }
+ NS_VERTIFY_NOT_NULL(newProvider, NULL);
// TODO set id
newProvider->mId = NULL;
newProvider->mUserData = (void *)OICMalloc(sizeof(OCDevAddr));
- if (!newProvider->mUserData)
- {
- NS_LOG(DEBUG, "OCDevAddr allocation fail");
- OICFree(newProvider);
- return NULL;
- }
+ NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(newProvider, NULL, OICFree(newProvider));
+
memcpy(newProvider->mUserData, clientResponse->addr, sizeof(OCDevAddr));
return newProvider;
void NSConsumerNotificationTaskProcessing(NSTask * task)
{
- if (!task)
- {
- NS_LOG(ERROR, "task is null");
- return;
- }
+ NS_VERTIFY_NOT_NULL_V(task);
NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE)
{
- if (NS_OK != NSConsumerSubscribeProvider((NSProvider *)task->taskData))
- {
- NS_LOG(ERROR, "Subscribe fail");
- return;
- }
+ NS_LOG(DEBUG, "Request Subscribe");
+ NSResult ret = NSConsumerSubscribeProvider((NSProvider *)task->taskData);
+ NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *)1 : NULL);
}
else if (task->taskType == TASK_SEND_READ || task->taskType == TASK_SEND_DISMISS)
{
- NSMessage_consumer * nsConsumer = (NSMessage_consumer *) task->taskData;
- if (!nsConsumer)
- {
- NS_LOG(ERROR, "taskData is NULL");
- return;
- }
-
- OCRepPayload * payload = OCRepPayloadCreate ();
- if (!payload)
- {
- NS_LOG(ERROR, "Failed to create POST payload object");
- return;
- }
-
- int type = (task->taskType == TASK_SEND_READ) ? 0 : 1;
- OCRepPayloadSetPropString(payload, "ID", (char *) nsConsumer->mId);
- OCRepPayloadSetPropString(payload, "SOURCE", (char *) nsConsumer->mSource);
- OCRepPayloadSetPropInt(payload, "STATE", type);
-
- // TODO fix param for uri
- if (NS_OK != NSConsumerPostProvider(
- (OCDevAddr *) nsConsumer->addr, (OCPayload *) payload, "/notification/sync"))
- {
- NS_LOG(ERROR, "Subscribe fail");
- return;
- }
+ NSMessage_consumer * message = (NSMessage_consumer *) task->taskData;
+ NS_VERTIFY_NOT_NULL_V(message);
+
+ OCStackResult ret = NSSendSyncInfo(message, (task->taskType == TASK_SEND_READ) ? 0 : 1);
+ NS_VERTIFY_STACK_OK_V(ret);
+
}
else if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL)
{
NS_LOG(ERROR, "Unknown type message");
}
}
+
+OCRepPayload * NSGetPayloadofSyncInfo(NSMessage_consumer * message, int type)
+{
+ OCRepPayload * payload = OCRepPayloadCreate();
+ NS_VERTIFY_NOT_NULL(payload, NULL);
+
+ OCRepPayloadSetPropString(payload, "ID", (char *) message->mId);
+ OCRepPayloadSetPropInt(payload, "STATE", type);
+ if (message->mSource)
+ {
+ OCRepPayloadSetPropString(payload, "SOURCE", (char *) message->mSource);
+ }
+
+ return payload;
+}
+
+OCStackResult NSSendSyncInfo(NSMessage_consumer * message, int type)
+{
+ OCRepPayload * payload = NSGetPayloadofSyncInfo(message, type);
+ NS_VERTIFY_NOT_NULL(payload, OC_STACK_ERROR);
+
+ return NSInvokeRequest(NULL, OC_REST_POST, message->addr,
+ "/notification/sync", (OCPayload*)payload,
+ NSConsumerPostResultCheck, NULL);
+}
#define NS_PRESENCE_SUBSCRIBE_QUERY "coap://224.0.1.187:5683/oic/ad?rt=oic.r.notification"
#define NS_GET_INFORMATION_QUERY "/notification?if=oic.if.notification"
-OCDoHandle * getPresenceHandle()
-{
- static OCDoHandle * g_PresenceHandle = NULL;
-
- return g_PresenceHandle;
-}
-
OCStackApplicationResult NSConsumerPresenceListener(
void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
{
NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
int64_t accepter = 0;
+ char * messageUri = NULL;
+ char * syncUri = NULL;
NS_LOG_V(DEBUG, "GET response income : %s:%d",
clientResponse->devAddr.addr, clientResponse->devAddr.port);
return OC_STACK_DELETE_TRANSACTION;
}
- if (!clientResponse->payload)
- {
- NS_LOG(ERROR, "payload is null");
- return OC_STACK_KEEP_TRANSACTION;
- }
+ NS_VERTIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
- if (!OCRepPayloadGetPropInt((OCRepPayload *)clientResponse->payload,
- NS_PAYLOAD_KEY_ACCEPTER, & accepter))
+ OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
+ while (payload)
{
- NS_LOG(ERROR, "can not seach for accepter");
- return OC_STACK_KEEP_TRANSACTION;
+ NS_LOG_V(DEBUG, "Payload Key : %s", payload->values->name);
+ payload = payload->next;
}
+ NS_LOG(DEBUG, "getting information of accepter");
+ bool getResult = OCRepPayloadGetPropInt((OCRepPayload *)clientResponse->payload,
+ NS_PAYLOAD_KEY_ACCEPTER, & accepter);
+ NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, OC_STACK_KEEP_TRANSACTION);
+
+ NS_LOG(DEBUG, "getting message URI");
+ getResult = OCRepPayloadGetPropString(
+ (OCRepPayload *)clientResponse->payload,
+ "MESSAGE_URI", & messageUri);
+ NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, OC_STACK_KEEP_TRANSACTION);
+
+ NS_LOG(DEBUG, "getting sync URI");
+ getResult = OCRepPayloadGetPropString((OCRepPayload *)clientResponse->payload,
+ NS_ATTRIBUTE_SYNC, & syncUri);
+ NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL,
+ OC_STACK_KEEP_TRANSACTION, OICFree(messageUri));
+
NSProvider * newProvider = (NSProvider *)OICMalloc(sizeof(NSProvider));
- if (!newProvider)
- {
- NS_LOG(DEBUG, "NSProvider allocation fail");
- return OC_STACK_KEEP_TRANSACTION;
- }
+ NS_VERTIFY_NOT_NULL(newProvider, OC_STACK_KEEP_TRANSACTION);
// TODO set id
newProvider->mId = NULL;
+ newProvider->messageUri = messageUri;
+ newProvider->syncUri = syncUri;
+
newProvider->mUserData = (void *)OICMalloc(sizeof(OCDevAddr));
- if (!newProvider)
- {
- NS_LOG(DEBUG, "OCDevAddr allocation fail");
- OICFree(newProvider);
- return OC_STACK_KEEP_TRANSACTION;
- }
+ NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(newProvider->mUserData,
+ OC_STACK_KEEP_TRANSACTION, OICFree(newProvider));
memcpy(newProvider->mUserData, clientResponse->addr, sizeof(OCDevAddr));
- {
- OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
- while (payload)
- {
- NS_LOG_V(DEBUG, "Payload Key : %s", payload->values->name);
- payload = payload->next;
- }
- }
-
- if (!OCRepPayloadGetPropString((OCRepPayload *)clientResponse->payload,
- "MESSAGE_URI", & newProvider->messageUri))
- {
- OICFree(newProvider->mUserData);
- OICFree(newProvider);
- NS_LOG(ERROR, "can not seach for message uri");
- return OC_STACK_KEEP_TRANSACTION;
- }
-
- if (!OCRepPayloadGetPropString((OCRepPayload *)clientResponse->payload,
- NS_ATTRIBUTE_SYNC, & newProvider->syncUri))
- {
- OICFree(newProvider->messageUri);
- OICFree(newProvider->mUserData);
- OICFree(newProvider);
- NS_LOG(ERROR, "can not seach for sync uri");
- return OC_STACK_KEEP_TRANSACTION;
- }
-
if (accepter == NS_ACCEPTER_CONSUMER)
{
NS_LOG(DEBUG, "accepter is NS_ACCEPTER_CONSUMER, Callback to user");
NS_LOG(DEBUG, "accepter is NS_ACCEPTER_PROVIDER, request subscribe");
NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) newProvider);
- if (!task)
- {
- NS_LOG(DEBUG, "NSTask allocation fail");
- return OC_STACK_KEEP_TRANSACTION;
- }
+ NS_VERTIFY_NOT_NULL(task, OC_STACK_KEEP_TRANSACTION);
NSConsumerPushEvent(task);
}
void NSConsumerDiscoveryTaskProcessing(NSTask * task)
{
- if (!task)
- {
- NS_LOG(ERROR, "task is null");
- return;
- }
+ NS_VERTIFY_NOT_NULL_V(task);
NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_CONSUMER_REQ_DISCOVER)
NSNotificationReceivedCallback postCb,
NSSyncCallback syncCb)
{
- if (NSIsStartedConsumer())
- {
- NS_LOG(DEBUG, "is already started consumer service?");
- return NS_OK;
- }
-
- if (!discoverCb)
- {
- NS_LOG(ERROR, "discoverCb is NULL");
- return NS_ERROR;
- }
-
- if (!postCb)
- {
- NS_LOG(ERROR, "postCb is NULL");
- return NS_ERROR;
- }
+ bool isStartedConsumer = NSIsStartedConsumer();
+ NS_VERTIFY_NOT_NULL(isStartedConsumer == false ? (void *) 1 : NULL, NS_OK);
- if (!syncCb)
- {
- NS_LOG(ERROR, "syncCb is NULL");
- return NS_ERROR;
- }
+ NS_VERTIFY_NOT_NULL(discoverCb, NS_ERROR);
+ NS_VERTIFY_NOT_NULL(postCb, NS_ERROR);
+ NS_VERTIFY_NOT_NULL(syncCb, NS_ERROR);
NSSetDiscoverProviderCb(discoverCb);
NSSetNotificationPostedCb(postCb);
NSSetNotificationSyncCb(syncCb);
NSSetIsStartedConsumer(true);
- if (NS_OK != NSConsumerMessageHandlerInit())
- {
- NSStopConsumer();
- return NS_ERROR;
- }
+ NSResult ret = NSConsumerMessageHandlerInit();
+ NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL,
+ NS_ERROR, NSStopConsumer());
return NS_OK;
}
NSResult NSSubscribe(NSProvider * provider)
{
NSTask * subscribeTask = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) provider);
- if (!subscribeTask)
- {
- NS_LOG(ERROR, "NSTask allocation fail");
- return NS_ERROR;
- }
+ NS_VERTIFY_NOT_NULL(subscribeTask, NS_ERROR);
return NSConsumerPushEvent(subscribeTask);
}
NSResult NSUnsubscribe(NSProvider * provider)
{
NSTask * unsubscribeTask = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL, (void *) provider);
- if (!unsubscribeTask)
- {
- NS_LOG(ERROR, "NSTask allocation fail");
- return NS_ERROR;
- }
+ NS_VERTIFY_NOT_NULL(unsubscribeTask, NS_ERROR);
return NSConsumerPushEvent(unsubscribeTask);
}
NSResult NSRescanProvider()
{
NSTask * discoverTask = NSMakeTask(TASK_CONSUMER_REQ_DISCOVER, NULL);
- if (!discoverTask)
- {
- NS_LOG(ERROR, "NSTask allocation fail");
- return NS_ERROR;
- }
+ NS_VERTIFY_NOT_NULL(discoverTask, NS_ERROR);
return NSConsumerPushEvent(discoverTask);
}
NSResult NSConsumerReadCheck(NSMessage * obj)
{
NSTask * readTask = NSMakeTask(TASK_SEND_READ, (void *) obj);
- if (!readTask)
- {
- NS_LOG(ERROR, "NSTask allocation fail");
- return NS_ERROR;
- }
+ NS_VERTIFY_NOT_NULL(readTask, NS_ERROR);
return NSConsumerPushEvent(readTask);
}
NSResult NSConsumerDismissCheck(NSMessage * obj)
{
NSTask * dismissTask = NSMakeTask(TASK_SEND_DISMISS, (void *) obj);
- if (!dismissTask)
- {
- NS_LOG(ERROR, "NSTask allocation fail");
- return NS_ERROR;
- }
+ NS_VERTIFY_NOT_NULL(dismissTask, NS_ERROR);
return NSConsumerPushEvent(dismissTask);
}
NSResult NSDropNSObject(NSMessage * obj)
{
- if (!obj)
- {
- return NS_ERROR;
- }
+ NS_VERTIFY_NOT_NULL(obj, NS_ERROR);
if (obj->mId)
{
{
NSCacheList * cache;
cache = *(NSGetCacheList());
- if (!cache)
+ if (cache)
{
NSStorageDestroy(cache);
}
noti->type = type;
NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement));
- if (!obj)
- {
- NS_LOG(ERROR, "Cache allocation is failed");
- }
+ NS_VERTIFY_NOT_NULL(obj, NS_ERROR);
+
obj->data = (NSCacheData *) noti;
obj->next = NULL;
- if (NS_OK != NSStorageWrite(cache, obj))
- {
- NS_LOG(ERROR, "Cache write fail");
- return NS_ERROR;
- }
+ NS_LOG(DEBUG, "try to write to storage");
+ NSResult ret = NSStorageWrite(cache, obj);
+ NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL,
+ NS_ERROR, NSRemoveMessage(noti));
NSRemoveMessage(noti);
OICFree(obj);
void NSConsumerSubscriptionTaskProcessing(NSTask * task)
{
- if (!task)
- {
- NS_LOG(ERROR, "task is null");
- return;
- }
+ NS_VERTIFY_NOT_NULL_V(task);
NSCacheList * cache;
if (!*(NSGetCacheList()))
{
NS_LOG(DEBUG, "Cache Init");
cache = NSStorageCreate();
+ NS_VERTIFY_NOT_NULL_V(cache);
+
cache->cacheType = NS_CONSUMER_CACHE_MESSAGE;
- if (!cache)
- {
- NS_LOG(ERROR, "Cache create fail");
- return;
- }
NSSetCacheList(cache);
}
cache = *(NSGetCacheList());
+ NSResult ret = NS_ERROR;
NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
-
switch (task->taskType)
{
- case TASK_CONSUMER_RECV_NOTIFICATION:
+ case TASK_CONSUMER_RECV_MESSAGE:
{
NS_LOG(DEBUG, "Receive New Notification");
- if (NS_OK != NSCacheUpdate(cache, task, Notification))
- {
- NS_LOG(ERROR, "Cache Update fail");
- return;
- }
+ ret = NSCacheUpdate(cache, task, Notification);
+ NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
break;
}
case TASK_RECV_READ:
{
NS_LOG(DEBUG, "Receive Read Notification");
- if (NS_OK != NSCacheUpdate(cache, task, Read))
- {
- NS_LOG(ERROR, "Cache Update fail");
- return;
- }
+ ret = NSCacheUpdate(cache, task, Read);
+ NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
break;
}
case TASK_RECV_DISMISS:
{
NS_LOG(DEBUG, "Receive Dismiss Notification");
- if (NS_OK != NSCacheUpdate(cache, task, Dismiss))
- {
- NS_LOG(ERROR, "Cache Update fail");
- return;
- }
+
+ ret = NSCacheUpdate(cache, task, Dismiss);
+ NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
break;
}
default :
void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled);
+OCDoHandle * getPresenceHandle()
+{
+ static OCDoHandle g_PresenceHandle = NULL;
+
+ return & g_PresenceHandle;
+}
+
+
NSResult NSConsumerListenerInit()
{
// TODO replace with comment lines when enable network monitor of IP Adapter
// return NS_ERROR;
// }
- if (OC_STACK_OK != NSInvokeRequest(NULL, OC_REST_PRESENCE, NULL,
- NS_PRESENCE_SUBSCRIBE_QUERY, NULL, NSConsumerPresenceListener, NULL))
- {
- NS_LOG(ERROR, "Presence request fail");
- return NS_ERROR;
- }
+ NS_LOG(DEBUG, "Request to subscribe presence");
+ OCStackResult stackResult = NSInvokeRequest(getPresenceHandle(), OC_REST_PRESENCE, NULL,
+ NS_PRESENCE_SUBSCRIBE_QUERY, NULL, NSConsumerPresenceListener, NULL);
+ NS_VERTIFY_STACK_OK(stackResult, NS_ERROR);
- if (OC_STACK_OK != NSInvokeRequest(NULL, OC_REST_DISCOVER, NULL,
- NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL))
- {
- NS_LOG(ERROR, "Discover request fail");
- return NS_ERROR;
- }
+ NS_LOG(DEBUG, "Request to discover provider");
+ stackResult = NSInvokeRequest(NULL, OC_REST_DISCOVER, NULL,
+ NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL);
+ NS_VERTIFY_STACK_OK(stackResult, NS_ERROR);
return NS_OK;
}
+void NSConsumerListenerTermiate()
+{
+ CARegisterNetworkMonitorHandler(NULL, NULL);
+ OCCancel(*getPresenceHandle(), NS_QOS, NULL, 0);
+}
+
void NSConnectionStateListener(CATransportAdapter_t adapter,
const char *remote_address, bool connected)
{
NS_LOG(DEBUG, "try to discover notification provider.");
NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL);
- if (!task)
- {
- NS_LOG(ERROR, "NSTask allocation fail.");
- return;
- }
+ NS_VERTIFY_NOT_NULL_V(task);
+
NSConsumerPushEvent(task);
}
}
NS_LOG(DEBUG, "try to discover notification provider.");
NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL);
- if (!task)
- {
- NS_LOG(ERROR, "NSTask allocation fail.");
- return;
- }
+ NS_VERTIFY_NOT_NULL_V(task);
+
NSConsumerPushEvent(task);
}
}
NSResult NSConsumerListenerInit();
+void NSConsumerListenerTermiate();
+
#ifdef __cplusplus
}
#endif // __cplusplus
NSConsumerQueue * NSCreateQueue()
{
NSConsumerQueue * newQueue = (NSConsumerQueue *)OICMalloc(sizeof(NSConsumerQueue));
- if (!newQueue)
- {
- return NULL;
- }
+ NS_VERTIFY_NOT_NULL(newQueue, NULL);
newQueue->size = 0;
newQueue->head = NULL;
bool NSPushQueue(NSConsumerQueue * queue, NSConsumerQueueObject * object)
{
- if (!queue)
- {
- return false;
- }
-
- if (!object)
- {
- NS_LOG(ERROR, "object is null. can not insert to queue");
- }
+ NS_VERTIFY_NOT_NULL(queue, false);
+ NS_VERTIFY_NOT_NULL(object, false);
if (!(queue->head))
{
{
NSConsumerQueueObject * retObject = NULL;
- if (!queue)
- {
- return NULL;
- }
+ NS_VERTIFY_NOT_NULL(queue, NULL);
+ NS_VERTIFY_NOT_NULL(queue->head, NULL);
if (queue->size <= 0)
{
return NULL;
}
- if (!(queue->head))
- {
- return NULL;
- }
-
retObject = queue->head;
queue->head = (NSConsumerQueueObject *)(retObject->next);
NSConsumerThread * handle = NULL;
NSConsumerQueue * queue = NULL;
- if (NS_OK != NSConsumerListenerInit())
- {
- NS_LOG(ERROR, "listener init fail");
- return NS_ERROR;
- }
+ NS_LOG(DEBUG, "listener init");
+ NSResult ret = NSConsumerListenerInit();
+ NS_VERTIFY_NOT_NULL(ret == NS_OK ? (void *) 1 : NULL, NS_ERROR);
- if (NS_OK != NSConsumerSystemInit())
- {
- NS_LOG(ERROR, "system init fail");
- return NS_ERROR;
- }
+ NS_LOG(DEBUG, "system init");
+ ret = NSConsumerSystemInit();
+ NS_VERTIFY_NOT_NULL(ret == NS_OK ? (void *) 1 : NULL, NS_ERROR);
+ NS_LOG(DEBUG, "queue thread init");
handle = NSThreadInit(NSConsumerMsgHandleThreadFunc, NULL);
- if (!handle)
- {
- NS_LOG(ERROR, "msg handle thread init fail");
- return NS_ERROR;
- }
+ NS_VERTIFY_NOT_NULL(handle, NS_ERROR);
NSSetMsgHandleThreadHandle(handle);
+ NS_LOG(DEBUG, "create queue");
queue = NSCreateQueue();
- if (!queue)
- {
- NS_LOG(ERROR, "msg handle queue create fail");
- return NS_ERROR;
- }
+ NS_VERTIFY_NOT_NULL(queue, NS_ERROR);
NSSetMsgHandleQueue(queue);
return NS_OK;
NSResult NSConsumerPushEvent(NSTask * task)
{
- if (!NSThreadInit(NSConsumerMsgPushThreadFunc, (void *) task))
- {
- NS_LOG(ERROR, "insert to queue thread init fail");
- return NS_ERROR;
- }
+ NSConsumerThread * thread = NSThreadInit(NSConsumerMsgPushThreadFunc, (void *) task);
+ NS_VERTIFY_NOT_NULL(thread, NS_ERROR);
return NS_OK;
}
void NSConsumerMessageHandlerExit()
{
NSDestroyCacheList();
+ NSConsumerListenerTermiate();
NSThreadStop(*(NSGetMsgHandleThreadHandle()));
+ NSDestroyQueue(*(NSGetMsgHandleQueue()));
}
void * NSConsumerMsgHandleThreadFunc(void * threadHandle)
{
NSConsumerQueue * queue = NULL;
NSConsumerQueueObject * obj = NULL;
+
+ NS_LOG(DEBUG, "create thread for consumer message handle");
NSConsumerThread * queueHandleThread = (NSConsumerThread *) threadHandle;
- NS_LOG(DEBUG, "created thread for consumer message handle");
- if (!queueHandleThread)
- {
- NS_LOG(ERROR, "thread handle is null");
- return NULL;
- }
+ NS_VERTIFY_NOT_NULL(queueHandleThread, NULL);
while (true)
{
{
NSConsumerQueueObject * obj = NULL;
NSConsumerQueue * queue = NULL;
+
+ NS_LOG(DEBUG, "get queueThread handle");
NSConsumerThread * msgHandleThread = *(NSGetMsgHandleThreadHandle());
- if (!msgHandleThread)
- {
- NS_LOG(ERROR, "NSThreadHandle is null. can not insert to queue");
- return NULL;
- }
+ NS_VERTIFY_NOT_NULL(msgHandleThread, NULL);
+ NS_LOG(DEBUG, "create queue object");
obj = (NSConsumerQueueObject *)OICMalloc(sizeof(NSConsumerQueueObject));
- if (!obj)
- {
- NS_LOG(ERROR, "NSConsumerQueueObject allocation fail");
- return NULL;
- }
+ NS_VERTIFY_NOT_NULL(obj, NULL);
obj->data = data;
obj->next = NULL;
}
case TASK_RECV_READ:
case TASK_RECV_DISMISS:
- case TASK_CONSUMER_RECV_NOTIFICATION:
+ case TASK_CONSUMER_RECV_MESSAGE:
{
NSConsumerSubscriptionTaskProcessing(task);
break;
NSConsumerThread * NSThreadInit(NSThreadFunc func, void * data)
{
- if (!func)
- {
- NS_LOG(ERROR, "thread function is null");
- return NULL;
- }
+ NS_VERTIFY_NOT_NULL(func, NULL);
pthread_mutex_init(&g_create_mutex, NULL);
NSConsumerThread * handle = (NSConsumerThread *)OICMalloc(sizeof(NSConsumerThread));
- if (!handle)
- {
- NS_LOG(ERROR, "thread allocation fail");
- return NULL;
- }
+ NS_VERTIFY_NOT_NULL(handle, NULL);
memset(handle, 0, sizeof(NSConsumerThread));
pthread_mutexattr_init(&(handle->mutex_attr));
- if (pthread_mutexattr_settype(&(handle->mutex_attr), PTHREAD_MUTEX_RECURSIVE))
- {
- NS_LOG(ERROR, "thread mutex_attr init fail");
- NSDestroyThreadHandle(handle);
- return NULL;
- }
-
- if (pthread_mutex_init(&(handle->mutex), &(handle->mutex_attr)))
- {
- NS_LOG(ERROR, "thread mutex init fail");
- NSDestroyThreadHandle(handle);
- return NULL;
- }
+
+ int pthreadResult = pthread_mutexattr_settype(&(handle->mutex_attr), PTHREAD_MUTEX_RECURSIVE);
+ NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
+ NULL, NSDestroyThreadHandle(handle));
+
+ pthreadResult = pthread_mutex_init(&(handle->mutex), &(handle->mutex_attr));
+ NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
+ NULL, NSDestroyThreadHandle(handle));
pthread_mutex_lock(&g_create_mutex);
handle->isStarted = true;
- if (pthread_create(&(handle->thread_id), NULL, func,
- (data == NULL) ? (void *) handle : (void *)data))
- {
- NS_LOG(ERROR, "thread create fail");
- NSDestroyThreadHandle(handle);
- return NULL;
- }
+ pthreadResult = pthread_create(&(handle->thread_id), NULL, func,
+ (data == NULL) ? (void *) handle : (void *)data);
+ NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
+ NULL, NSDestroyThreadHandle(handle));
+
pthread_mutex_unlock(&g_create_mutex);
return handle;