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