X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=service%2Fnotification%2Fexamples%2Flinux%2Fnotificationprovider.c;h=11c1c36733fbde20058082c426cb47a486e564c3;hb=refs%2Ftags%2Ftizen_4.0.m2_release;hp=f4313e1497584f4b7f8a770d20cd38d0186798c4;hpb=83cce9ed0ab42d4c4968e00ad7ad9b795620725e;p=platform%2Fupstream%2Fiotivity.git diff --git a/service/notification/examples/linux/notificationprovider.c b/service/notification/examples/linux/notificationprovider.c index f4313e1..11c1c36 100644 --- a/service/notification/examples/linux/notificationprovider.c +++ b/service/notification/examples/linux/notificationprovider.c @@ -1,233 +1,410 @@ -/****************************************************************** - * - * Copyright 2015 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 -#include -#include -#include -#include "NSCommon.h" -#include "NSProviderInterface.h" -#include "logger.h" -#include "octypes.h" -#include "pthread.h" -#include "oic_string.h" -#include "oic_malloc.h" - -#define TAG "NSProviderExample" - -extern char *strdup(const char *s); - -bool isExit = false; - -int id; - -void* OCProcessThread(void * ptr) -{ - (void) ptr; - while (!isExit) - { - if (OCProcess() != OC_STACK_OK) - { - OIC_LOG(ERROR, TAG, "OCStack process error"); - return NULL; - } - } - - return NULL; -} - -void subscribeRequestCallback(NSConsumer *consumer) -{ - OIC_LOG(INFO, TAG, "consumer requested to subscribe"); - - printf("NS_APP Consumer Device ID: %s\n", consumer->consumerId); - - NSAccept(consumer, true); -} - -void syncCallback(NSSyncInfo *sync) -{ - OIC_LOG(INFO, TAG, "sync requested"); - - printf("NS_APP Sync State: %d\n", sync->state); -} - -OCStackApplicationResult CloudLoginoutCallback(void *ctx, - OCDoHandle handle, OCClientResponse *clientResponse) -{ - int CtxValue = 0x99; - if (ctx != (void *)CtxValue) - { - OIC_LOG(INFO, TAG, "Invalid Cloud Login/out callback received"); - } - - OIC_LOG(INFO, TAG, "Login/out response received"); - - if (clientResponse->payload != NULL && - clientResponse->payload->type == PAYLOAD_TYPE_REPRESENTATION) - { - OIC_LOG(INFO, TAG, "PAYLOAD_TYPE_REPRESENTATION received"); - - OCRepPayloadValue *val = ((OCRepPayload *)clientResponse->payload)->values; - - OIC_LOG(INFO, TAG, "Get payload values"); - while (val) - { - OIC_LOG_V(INFO, TAG, "key: %s / Value: %s", val->name, val->str); - val = val->next; - } - - char *serverAddress = "coap+tcp://52.69.149.85:5683"; - NSProviderEnableRemoteService(serverAddress); - } - - return OC_STACK_KEEP_TRANSACTION; -} - -FILE* server_fopen(const char *path, const char *mode) -{ - (void)path; - return fopen("oic_ns_provider_db.dat", mode); -} - -int main() -{ - int num; - pthread_t processThread; - - // cloud host address - const char *host = "coap+tcp://52.69.149.85:5683"; - // cloud auth session - const char *auth_session = "gZDRuDyYapZXIcrs"; - - - OIC_LOG(INFO, TAG, "NSStartProvider()"); - - // open oic_db - static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink}; - OCRegisterPersistentStorageHandler(&ps); - - if (OCInit(NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK) - { - OIC_LOG(INFO, TAG, "OCStack init error"); - return 0; - } - - pthread_create(&processThread, NULL, OCProcessThread, unlink); - - while (!isExit) - { - char dummy; - - printf("1. NSStartProvider(Accepter: Provider) \n"); - printf("2. NSStartProvider(Accepter: Consumer) \n"); - printf("3. NSSendNotification() \n"); - printf("4. NSRead \n"); - printf("5. NSStopProvider() \n"); - printf("6. NSGetConsumerList \n"); - printf("11. NSCloudLogin \n"); - printf("12. NSCloudLogout \n"); - printf("0. Exit() \n"); - - printf("input : "); - - scanf("%d", &num); - fflush(stdin); - scanf("%c", &dummy); - fflush(stdin); - - switch (num) - { - case 1: - OIC_LOG(INFO, TAG, "NSStartProvider(Accepter: Provider)"); - NSStartProvider(NS_ACCESS_ALLOW, subscribeRequestCallback, syncCallback); - break; - case 2: - OIC_LOG(INFO, TAG, "NSStartProvider(Accepter: Consumer)"); - NSStartProvider(NS_ACCESS_DENY, subscribeRequestCallback, syncCallback); - break; - case 3: - OIC_LOG(INFO, TAG, "NSSendNotification()"); - - char title[100]; - char body[100]; - - printf("id : %d\n", ++id); - printf("title : "); - - gets(title); - - printf("body : "); - gets(body); - - printf("app - mTitle : %s \n", title); - printf("app - mContentText : %s \n", body); - - NSMessage * msg = NSCreateMessage(); - - msg->title = OICStrdup(title); - msg->contentText = OICStrdup(body); - msg->sourceName = OICStrdup("OCF"); - - NSSendMessage(msg); - - break; - - case 4: - OIC_LOG(INFO, TAG, "NSRead"); - NSSyncInfo * sync = (NSSyncInfo*) OICMalloc(sizeof(NSSyncInfo)); - - sync->messageId = OICStrdup("dev_001"); - sync->state = 1; - - break; - - case 5: - NSStopProvider(); - break; - case 6: - OIC_LOG(INFO, TAG, "NSGetConsumerList"); - break; - case 11: - OIC_LOG(INFO, TAG, "NSCloudLogin"); - - NSCloudLogin(host, auth_session, CloudLoginoutCallback); - OIC_LOG(INFO, TAG, "OCCloudLogin requested"); - break; - case 12: - OIC_LOG(INFO, TAG, "NSCloudLogout"); - - NSCloudLogout(host, auth_session, CloudLoginoutCallback); - OIC_LOG(INFO, TAG, "OCCloudLogout requested"); - break; - case 0: - NSStopProvider(); - isExit = true; - break; - default: - OIC_LOG(INFO, TAG, "Under Construction"); - break; - } - - printf("\n"); - } - - return 0; -} +//****************************************************************** +// +// 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 +#include +#include +#include + +#include "NSProviderInterface.h" +#include "NSCommon.h" +#include "logger.h" +#include "octypes.h" +#include "pthread.h" +#include "oic_string.h" +#include "oic_malloc.h" +#include "ocstack.h" + +#define TAG "NSProviderExample" + +#ifdef WITH_CLOUD +#include "cloud_connector.h" + +// Input the following values to publish resource to cloud +char REMOTE_SERVER_ADDRESS[50] = {'\0',}; +char AUTH_PROVIDER[50] = {'\0',}; +char AUTH_CODE[50] = {'\0',}; +char UID[50] = {'\0',}; +char ACCESS_TOKEN[50] = {'\0',}; +#endif + +#ifdef WITH_MQ +char CLOUD_ADDRESS[100] = "10.113.64.52:5686"; +char CLOUD_TOPIC[100] = "/oic/ps/notification"; +#endif + +char mainConsumer[37] = {'\0',}; + +extern char *strdup(const char *s); + +bool isExit = false; + +int id; + +void* OCProcessThread(void * ptr) +{ + (void) ptr; + while (!isExit) + { + if (OCProcess() != OC_STACK_OK) + { + printf("OCStack process error"); + return NULL; + } + } + + return NULL; +} + +void subscribeRequestCallback(NSConsumer *consumer) +{ + printf("consumer requested to subscribe"); + + printf("NS_APP Consumer Device ID: %s\n", consumer->consumerId); + + if(mainConsumer[0] == '\0') + { + OICStrcpy(mainConsumer, 37, consumer->consumerId); + } + + NSAcceptSubscription(consumer->consumerId, true); +} + +void syncCallback(NSSyncInfo *sync) +{ + printf("sync requested"); + + printf("NS_APP Sync State: %d\n", sync->state); +} + +FILE* server_fopen(const char *path, const char *mode) +{ + (void)path; + return fopen("oic_ns_provider_db.dat", mode); +} + +void printTopics(NSTopicLL * topics) +{ + if(!topics) + { + printf("topics is null\n"); + return; + } + + NSTopicLL * iter = topics; + + while(iter) + { + printf("tName = %s, tState = %d\n", iter->topicName, (int)iter->state); + iter = iter->next; + } +} + +void removeTopics(NSTopicLL * topics) +{ + if(!topics) + { + printf("topics is null\n"); + return; + } + + NSTopicLL * iter = topics; + + while(iter) + { + NSTopicLL * del = iter; + if(del->topicName) + { + OICFree(del->topicName); + } + iter = iter->next; + + OICFree(del); + } +} + +void input(char * buffer) +{ + char ch; + int i = 0; + + while( (ch = getchar()) != '\n' && i < 100) + buffer[i++] = ch; + + buffer[i] = '\0'; +} + +int main() +{ + int num; + pthread_t processThread; + + printf("NSStartProvider()\n\n"); + + // open oic_db + static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink}; + OCRegisterPersistentStorageHandler(&ps); + + if (OCInit(NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK) + { + printf("OCStack init error"); + return 0; + } + + pthread_create(&processThread, NULL, OCProcessThread, unlink); + + while (!isExit) + { + char dummy; + + printf("==============================================\n"); + printf("1. NSStartProvider(Accepter: Provider) \n"); + printf("2. NSStartProvider(Accepter: Consumer) \n"); + printf("3. NSSendNotification() \n"); + printf("4. NSRead() \n"); + printf("5. NSProviderAddTopic(); \n"); + printf("6. NSProviderDeleteTopic(); \n"); + printf("7. NSProviderSelectTopic(); \n"); + printf("8. NSProviderUnselectTopic(); \n"); + printf("9. NSProviderGetConsumerTopics(); \n"); + printf("10. NSProviderGetTopics(); \n"); + printf("11. NSProviderSubscribeMQService() \n"); + printf("0. NSStopProvider() \n"); +#ifdef WITH_CLOUD + printf("21. NSProviderEnableRemoteService (after login) \n"); + printf("22. NSProviderDisableRemoteService (after login) \n"); + printf("31. Cloud Signup \n"); + printf("32. Cloud Login \n"); + printf("33. Cloud Logout \n"); +#endif + printf("0. Exit() \n"); + printf("==============================================\n"); + + printf("input : "); + + if(scanf("%d", &num) > 0) + { + if(scanf("%c", &dummy) > 0) + { + fflush(stdin); + printf("\n"); + } + } + else + { + printf("invalid input \n"); + num = 0; + } + + switch (num) + { + case 1: + { + printf("NSStartProvider(Accepter: Provider)"); + NSProviderConfig config; + config.subControllability = true; + config.subRequestCallback = subscribeRequestCallback; + config.syncInfoCallback = syncCallback; + config.userInfo = OICStrdup("OCF_NOTIFICATION"); + NSStartProvider(config); + } + break; + + case 2: + { + printf("NSStartProvider(Accepter: Consumer)"); + NSProviderConfig config; + config.subControllability = false; + config.subRequestCallback = subscribeRequestCallback; + config.syncInfoCallback = syncCallback; + config.userInfo = OICStrdup("OCF_NOTIFICATION"); + NSStartProvider(config); + } + break; + + case 3: + { + printf("NSSendNotification()"); + char title[100] = {'\0',}; + char body[100] = {'\0',}; + char topic[100] = {'\0',}; + + printf("id : %d\n", ++id); + printf("title : "); + input(title); + + printf("body : "); + input(body); + + printf("topic : "); + + input(topic); + + printf("app - mTitle : %s \n", title); + printf("app - mContentText : %s \n", body); + printf("app - topic : %s \n", topic); + + NSMessage * msg = NSCreateMessage(); + if(msg) + { + + msg->title = OICStrdup(title); + msg->contentText = OICStrdup(body); + msg->sourceName = OICStrdup("OCF"); + + if(topic[0] != '\0') + { + msg->topic = OICStrdup(topic); + } + + NSSendMessage(msg); + } + } + break; + + case 4: + printf("NSRead\n"); + break; + + case 5: + printf("NSProviderAddTopic\n"); + NSProviderRegisterTopic("OCF_TOPIC1"); + NSProviderRegisterTopic("OCF_TOPIC2"); + NSProviderRegisterTopic("OCF_TOPIC3"); + NSProviderRegisterTopic("OCF_TOPIC4"); + break; + + case 6: + printf("NSProviderDeleteTopic\n"); + NSProviderUnregisterTopic("OCF_TOPIC2"); + break; + + case 7: + printf("NSProviderSelectTopic\n"); + NSProviderSetConsumerTopic(mainConsumer, "OCF_TOPIC1"); + NSProviderSetConsumerTopic(mainConsumer, "OCF_TOPIC2"); + NSProviderSetConsumerTopic(mainConsumer, "OCF_TOPIC3"); + NSProviderSetConsumerTopic(mainConsumer, "OCF_TOPIC4"); + break; + + case 8: + printf("NSProviderUnSelectTopic\n"); + NSProviderUnsetConsumerTopic(mainConsumer, "OCF_TOPIC1"); + break; + + case 9: + printf("NSProviderGetConsumerTopics\n"); + { + NSTopicLL * topics = NSProviderGetConsumerTopics(mainConsumer); + printTopics(topics); + removeTopics(topics); + } + break; + + case 10: + printf("NSProviderGetTopics\n"); + { + NSTopicLL * topics = NSProviderGetTopics(); + printTopics(topics); + removeTopics(topics); + } + break; + +#if WITH_MQ + case 11: + printf("11. Subscribe MQ Service\n"); + + printf("Remote Server Address: "); + //input(CLOUD_ADDRESS); + + printf("Topic[notificationtest]: "); + //input(CLOUD_TOPIC); + + NSProviderSubscribeMQService(CLOUD_ADDRESS, CLOUD_TOPIC); + break; +#endif + +#ifdef WITH_CLOUD + case 21: + printf("Enable Remote Service\n"); + if(!IsCloudLoggedin()) + { + printf("Login required\n"); + break; + } + NSProviderEnableRemoteService(REMOTE_SERVER_ADDRESS); + break; + + case 22: + printf("Disable Remote Service\n"); + if(!IsCloudLoggedin()) + { + printf("Login required\n"); + break; + } + NSProviderDisableRemoteService(REMOTE_SERVER_ADDRESS); + break; + + case 31: + printf("Remote Server Address: "); + input(REMOTE_SERVER_ADDRESS); + + printf("Auth Provider(eg. github): "); + input(AUTH_PROVIDER); + + printf("Auth Code: "); + input(AUTH_CODE); + + OCCloudSignup(REMOTE_SERVER_ADDRESS, OCGetServerInstanceIDString(), + AUTH_PROVIDER, AUTH_CODE, CloudSignupCallback); + printf("OCCloudSignup requested"); + break; + case 32: + printf("Remote Server Address: "); + input(REMOTE_SERVER_ADDRESS); + + printf("UID: "); + input(UID); + + printf("ACCESS_TOKEN: "); + input(ACCESS_TOKEN); + + OCCloudLogin(REMOTE_SERVER_ADDRESS, UID, OCGetServerInstanceIDString(), + ACCESS_TOKEN, CloudLoginoutCallback); + printf("OCCloudLogin requested\n"); + break; + case 33: + OCCloudLogout(REMOTE_SERVER_ADDRESS, CloudLoginoutCallback); + printf("OCCloudLogin requested\n"); + break; +#endif + case 0: + NSStopProvider(); + isExit = true; + break; + default: + break; + } + + printf("\n"); + } + + return 0; +}