35 #include "hostIf_utils.h"
37 #include "libsoup-2.4/libsoup/soup.h"
38 #include <yajl/yajl_parse.h>
39 #include <yajl/yajl_gen.h>
42 typedef enum { STATE_BEFORE = 0, STATE_PARAM, STATE_PROCESSING, STATE_DONE } state_t;
50 SoupServer *server = NULL;
58 static int process_null(
void * ctx) {
61 static int process_boolean(
void * ctx,
int boolVal) {
64 static int process_integer(
void * ctx,
long long integerVal) {
67 static int process_double(
void * ctx,
double doubleVal) {
70 static int process_number(
void * ctx,
const char * numberVal,
size_t numberLen) {
77 static int process_string(
void * ctx,
const unsigned char * stringVal,
size_t stringLen)
81 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"Process string: %d\n", context->state);
82 if ((context->state == STATE_PROCESSING) && (context->grabString) && (stringLen<TR69HOSTIFMGR_MAX_PARAM_LEN-1))
84 char *
string = strndup((
const char *)stringVal, stringLen);
85 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"Adding string %s\n",
string);
86 context->list = g_list_append(context->list,
string);
87 context->grabString = FALSE;
93 static int process_start_map(
void * ctx)
98 static int process_map_key(
void * ctx,
const unsigned char * key,
size_t stringLen)
104 if ((context->state == STATE_BEFORE) &&
105 (stringLen == strlen(
"paramList")) &&
106 (strncmp(
"paramList", (
const char *) key, stringLen) == 0))
108 context->state = STATE_PARAM;
110 else if ((context->state == STATE_PROCESSING) &&
112 (strncmp(
"name", (
const char *) key, stringLen) == 0))
114 context->grabString =
TRUE;
118 context->grabString = FALSE;
123 static int process_end_map(
void * ctx)
129 static int process_start_array(
void * ctx)
132 if (context->state == STATE_PARAM)
134 context->state = STATE_PROCESSING;
139 static int process_end_array(
void * ctx)
142 if ((context->state == STATE_PROCESSING) || (context->state == STATE_PARAM))
144 context->state = STATE_DONE;
149 static yajl_callbacks callbacks = {
172 void *jsonIfHandlerThread(
void *msg)
174 RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,
"[%s:%s] Entering..\n", __FUNCTION__, __FILE__);
179 RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,
"[%s:%s] Exiting..\n", __FUNCTION__, __FILE__);
185 hostIf_HTTPJsonParse(
const unsigned char *message,
int length)
190 RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,
"[%s:%s] Entering..\n", __FUNCTION__, __FILE__);
192 yajl_handle parser = yajl_alloc(&callbacks, NULL, NULL, (
void *) (&context));
194 yajl_handle parser = yajl_alloc(&callbacks, NULL, (
void *) (&context));
197 stat = yajl_parse(parser, message, length);
198 if (stat != yajl_status_ok)
200 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"[%s:%s] Failed to parse in yajl_parse()\n", __FUNCTION__, __FILE__);
203 stat = yajl_parse_complete(parser);
205 stat = yajl_complete_parse(parser);
207 if (stat != yajl_status_ok)
209 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"[%s:%s] Failed to parse in yajl_parse_complete()\n", __FUNCTION__, __FILE__);
215 RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,
"[%s:%s] Exiting..\n", __FUNCTION__, __FILE__);
225 SoupClientContext *client,
229 GList *requestList = NULL;
232 RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,
"[%s:%s] Entering..\n", __FUNCTION__, __FILE__);
234 if (!msg->request_body ||
235 !msg->request_body->data ||
236 !msg->request_body->length)
238 soup_message_set_status_full (msg, SOUP_STATUS_BAD_REQUEST,
"No request data.");
239 RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,
"[%s:%s] Exiting.. Failed due to no message data.\n", __FUNCTION__, __FILE__);
243 params = hostIf_HTTPJsonParse((
const unsigned char *) msg->request_body->data, msg->request_body->length);
246 soup_message_set_status_full (msg, SOUP_STATUS_BAD_REQUEST,
"No request data.");
247 RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,
"[%s:%s] Exiting... Failed due to Parse HTTP Json messages. \n", __FUNCTION__, __FILE__);
253 json = yajl_gen_alloc( NULL, NULL);
255 json = yajl_gen_alloc(NULL);
259 soup_message_set_status_full (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR,
"Cannot create return object");
260 RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,
"[%s:%s] Exiting.. Failed to create json object\n", __FUNCTION__, __FILE__);
264 yajl_gen_map_open(json);
265 yajl_gen_string(json, (
const unsigned char *)
"paramList", 9);
266 yajl_gen_array_open(json);
272 strncpy( param->
paramName,(
char *) l->data,TR69HOSTIFMGR_MAX_PARAM_LEN );
275 if (hostIf_GetMsgHandler(param) == OK)
277 yajl_gen_map_open(json);
278 yajl_gen_string(json, (
const unsigned char *)
"name", 4);
279 yajl_gen_string(json, (
const unsigned char *) param->
paramName, strlen(param->
paramName));
281 yajl_gen_string(json, (
const unsigned char *)
"value", 5);
283 case hostIf_StringType:
284 yajl_gen_string(json, (
const unsigned char*) param->
paramValue, strlen((
char*)param->
paramValue));
286 case hostIf_IntegerType:
287 case hostIf_UnsignedIntType:
288 yajl_gen_integer(json, get_int(param->
paramValue));
290 case hostIf_UnsignedLongType:
291 yajl_gen_integer(json, get_ulong(param->
paramValue));
293 case hostIf_BooleanType:
294 yajl_gen_bool(json, get_boolean(param->
paramValue));
296 case hostIf_DateTimeType:
298 yajl_gen_string(json, (
const unsigned char *)
"Unknown", 7);
301 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"Unknown data type: %d", param->
paramtype);
305 yajl_gen_map_close(json);
307 hostIf_Free_stMsgData(param);
313 g_list_free_full(params, g_free);
317 yajl_gen_array_close(json);
318 yajl_gen_map_close(json);
321 const unsigned char *buf;
323 yajl_gen_get_buf(json, &buf, &len);
326 soup_message_set_response(msg, (
const char *)
"application/json", SOUP_MEMORY_COPY, (
const char *) buf, len);
327 soup_message_set_status (msg, SOUP_STATUS_OK);
334 RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,
"[%s:%s] Exiting..\n", __FUNCTION__, __FILE__);
345 GError *error = NULL;
346 guint port = argList.httpPort;
347 RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,
"[%s:%s] Entering..\n", __FUNCTION__, __FILE__);
353 server = soup_server_new (SOUP_SERVER_SERVER_HEADER,
"hostif", NULL);
357 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"SERVER: Could not create server.\n");
365 if(FALSE == soup_server_listen_local (server, port, SOUP_SERVER_LISTEN_IPV4_ONLY, &error))
367 RDK_LOG(RDK_LOG_ERROR, LOG_TR69HOSTIF,
"SERVER: failed in soup_server_listen_local. (%s).\n", error->message);
371 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"SERVER: Started server successfully.\n");
374 RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,
"[%s:%s] Exiting..\n", __FUNCTION__, __FILE__);
385 soup_server_disconnect(server);
386 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"SERVER: Stopped server successfully.\n");