e7262a8de7d44916bcc650ac14bfc4ef798e73be
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / linux / secure / ocamsservice.cpp
1
2 #include "iotivity_config.h"
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #ifdef HAVE_UNISTD_H
7 #include <unistd.h>
8 #endif
9 #include <signal.h>
10 #ifdef HAVE_WINSOCK2_H
11 #include <winsock2.h>
12 #endif
13 #ifdef HAVE_PTHREAD_H
14 #include <pthread.h>
15 #endif
16 #include "ocstack.h"
17 #include "logger.h"
18 #include "common.h"
19
20 #define TAG  PCF("SRM-AMSS")
21
22 int gQuitFlag = 0;
23
24 //AMS service database, hold AMS service Identity and
25 //the PSK credentials of trusted devices
26 static char AMSS_DB_FILE[] = "oic_amss_db.dat";
27
28 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
29 void handleSigInt(int signum)
30 {
31     if (signum == SIGINT)
32     {
33         gQuitFlag = 1;
34     }
35 }
36
37 FILE* service_fopen(const char *path, const char *mode)
38 {
39     (void)path;
40     return fopen(AMSS_DB_FILE, mode);
41 }
42
43 int main(int /*argc*/, char* /*argv*/[])
44 {
45     struct timespec timeout;
46
47     OIC_LOG(DEBUG, TAG, "OCAMS service is starting...");
48
49     // Initialize Persistent Storage for SVR database
50     OCPersistentStorage ps = { service_fopen, fread, fwrite, fclose, unlink };
51     OCRegisterPersistentStorageHandler(&ps);
52
53     if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
54     {
55         OIC_LOG(ERROR, TAG, "OCStack init error");
56         return 0;
57     }
58
59     timeout.tv_sec  = 0;
60     timeout.tv_nsec = 100000000L;
61
62     // Break from loop with Ctrl-C
63     OIC_LOG(INFO, TAG, "Entering ocamsservice main loop...");
64     signal(SIGINT, handleSigInt);
65     while (!gQuitFlag)
66     {
67         if (OCProcess() != OC_STACK_OK)
68         {
69             OIC_LOG(ERROR, TAG, "OCStack process error");
70             return 0;
71         }
72         nanosleep(&timeout, NULL);
73     }
74
75     OIC_LOG(INFO, TAG, "Exiting ocamsservice main loop...");
76
77     if (OCStop() != OC_STACK_OK)
78     {
79         OIC_LOG(ERROR, TAG, "OCStack process error");
80     }
81
82     return 0;
83 }