1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
| #include <glib.h>
#include <string>
#include <luna-service2/lunaservice.h>
#include <PmLog.h>
#include <pbnjson.hpp>
static PmLogContext getPmLogContext()
{
static PmLogContext s_context = 0;
if (0 == s_context)
{
PmLogGetContext("NativeService", &s_context);
}
return s_context;
}
static pbnjson::JValue convertStringToJson(const char *rawData)
{
pbnjson::JInput input(rawData);
pbnjson::JSchema schema = pbnjson::JSchemaFragment("{}");
pbnjson::JDomParser parser;
if (!parser.parse(input, schema))
{
return pbnjson::JValue();
}
return parser.getDom();
}
static std::string convertJsonToString(const pbnjson::JValue json)
{
return pbnjson::JGenerator::serialize(json, pbnjson::JSchemaFragment("{}"));
}
static bool onHello(LSHandle *sh, LSMessage* message, void* ctx)
{
PmLogInfo(getPmLogContext(), "HANDLE_HELLO", 0, "hello method called");
pbnjson::JValue reply = pbnjson::Object();
if (reply.isNull())
return false;
reply.put("returnValue", true);
reply.put("answer", "Hello, Native Service!!");
LSError lserror;
LSErrorInit(&lserror);
if (!LSMessageReply(sh, message, reply.stringify().c_str(), &lserror))
{
PmLogError(getPmLogContext(), "HANDLE_HELLO", 0, "Message reply error!!");
LSErrorPrint(&lserror, stdout);
return false;
}
return true;
}
static bool cbGetTime(LSHandle *sh, LSMessage *msg, void *user_data)
{
LSError lserror;
LSErrorInit(&lserror);
PmLogInfo(getPmLogContext(), "GETTIME_CALLBACK", 1, PMLOGJSON("payload", LSMessageGetPayload(msg)), " ");
pbnjson::JValue response = convertStringToJson(LSMessageGetPayload(msg));
bool successCallback = response["returnValue"].asBool();
if (successCallback)
{
int64_t utc= response["utc"].asNumber<int64_t>();
PmLogInfo(getPmLogContext(), "GETTIME_CALLBACK", 1, PMLOGKFV("UTC", "%lld", utc), " ");
}
return true;
}
static LSMethod serviceMethods[] = {
{ "hello", onHello }
};
int main(int argc, char* argv[])
{
PmLogInfo(getPmLogContext(), "SERVICE_MAIN", 0, "start com.example.service.native");
LSError lserror;
LSErrorInit(&lserror);
GMainLoop* mainLoop = g_main_loop_new(nullptr, false);
LSHandle *m_handle = nullptr;
if(!LSRegister("com.example.service.native", &m_handle, &lserror))
{
PmLogError(getPmLogContext(), "LS_REGISTER", 0, "Unable to register to luna-bus");
LSErrorPrint(&lserror, stdout);
return false;
}
if (!LSRegisterCategory(m_handle, "/", serviceMethods, NULL, NULL, &lserror))
{
PmLogError(getPmLogContext(), "LS_REGISTER", 0, "Unable to register category and method");
LSErrorPrint(&lserror, stdout);
return false;
}
if(!LSGmainAttach(m_handle, mainLoop, &lserror))
{
PmLogError(getPmLogContext(), "LS_REGISTER", 0, "Unable to attach service");
LSErrorPrint(&lserror, stdout);
return false;
}
if (!LSCall(m_handle,
"luna://com.webos.service.systemservice/clock/getTime",
"{}",
cbGetTime,
NULL,
NULL,
&lserror))
{
PmLogError(getPmLogContext(), "LSCALL_GETTIME", 0, "Cannot call getTime");
LSErrorPrint(&lserror, stderr);
}
g_main_loop_run(mainLoop);
if(!LSUnregister(m_handle, &lserror))
{
PmLogError(getPmLogContext(), "LS_REGISTER", 0, "Unable to unregister service");
LSErrorPrint(&lserror, stdout);
return false;
}
g_main_loop_unref(mainLoop);
mainLoop = nullptr;
return 0;
}
|