From: Manuel Bachmann Date: Wed, 16 Dec 2015 10:25:54 +0000 (+0100) Subject: New tree organization, update CMake req. to 2.8.8. X-Git-Tag: blowfish_2.0.1~352 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=src%2Fapp-framework-binder.git;a=commitdiff_plain;h=e2d857c5f05f84de8e2642ff9272a80ea9e5fed6 New tree organization, update CMake req. to 2.8.8. Use CMake-2.8.8-specific feature to allow building the daemon from various source directories. Signed-off-by: Manuel Bachmann --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e9314d1..af98b389 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ PROJECT(afb-daemon C) -CMAKE_MINIMUM_REQUIRED(VERSION 2.8) +CMAKE_MINIMUM_REQUIRED(VERSION 2.8.8) SET(CMAKE_BUILD_TYPE, Debug) SET(CMAKE_POSITION_INDEPENDENT_CODE ON) @@ -35,10 +35,9 @@ ENDIF(CMAKE_BUILD_TYPE MATCHES Debug) INCLUDE(FindPkgConfig) PKG_CHECK_MODULES(json-c REQUIRED json-c) PKG_CHECK_MODULES(libmicrohttpd REQUIRED libmicrohttpd) +PKG_CHECK_MODULES(uuid REQUIRED uuid) # Optional plugin dependencies PKG_CHECK_MODULES(librtlsdr librtlsdr>=0.5.0) -PKG_CHECK_MODULES(uuid REQUIRED uuid) - IF(librtlsdr_FOUND) MESSAGE(STATUS "librtlsdr found ; will compile Radio plugin... (PLUGIN)") @@ -51,3 +50,10 @@ SET(include_dirs ${INCLUDE_DIRS} ${CMAKE_SOURCE_DIR}/include ${json-c_INCLUDE_DI SET(link_libraries ${json-c_LIBRARIES} ${libmicrohttpd_LIBRARIES} ${uuid_LIBRARIES} ${librtlsdr_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${libefence_LIBRARIES} -lmagic -lm) ADD_SUBDIRECTORY(src) +ADD_SUBDIRECTORY(plugins) + +ADD_EXECUTABLE(afb-daemon $ $) +TARGET_LINK_LIBRARIES(afb-daemon ${link_libraries}) + +INSTALL(TARGETS afb-daemon + RUNTIME DESTINATION bin) diff --git a/README.md b/README.md index 7eaed62a..cb23e45c 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ industry requirementsas the primary target for this code is AGL. and the following tools: * pkg-config; - * cmake >= 2.8. + * cmake >= 2.8.8. To install all dependencies under OpenSUSE (except rtl-sdr), please type: diff --git a/nbproject/private/configurations.xml b/nbproject/private/configurations.xml index 706acf31..0b5e7921 100644 --- a/nbproject/private/configurations.xml +++ b/nbproject/private/configurations.xml @@ -2,30 +2,18 @@ - - - - - CMakeCCompilerId.c - - - - - feature_tests.c - - - - - + + + - - local-def.h proto-def.h + + SamplePost.c afbs-api.c @@ -40,26 +28,16 @@ - - - - - - - - - - - - - + + + - - + + @@ -89,8 +67,6 @@ - - diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml index 795635d6..10916bd0 100644 --- a/nbproject/private/private.xml +++ b/nbproject/private/private.xml @@ -6,6 +6,11 @@ - + + file:/home/mbc/git/afb-daemon-newapi/include/local-def.h + file:/home/mbc/git/afb-daemon-newapi/src/radio-api.c + file:/home/mbc/git/afb-daemon-newapi/src/afbs-api.c + file:/home/mbc/git/afb-daemon-newapi/src/dbus-api.c + diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt new file mode 100644 index 00000000..269517ad --- /dev/null +++ b/plugins/CMakeLists.txt @@ -0,0 +1,8 @@ +SET(PLUGINS_SOURCES audio/audio-api.c session/token-api.c) + +IF(librtlsdr_FOUND) + SET(PLUGINS_SOURCES ${PLUGINS_SOURCES} radio/radio-api.c) +ENDIF(librtlsdr_FOUND) + +ADD_LIBRARY(plugins OBJECT ${PLUGINS_SOURCES}) +INCLUDE_DIRECTORIES(${include_dirs}) diff --git a/src/alsa-api.c b/plugins/audio/audio-api.c similarity index 100% rename from src/alsa-api.c rename to plugins/audio/audio-api.c diff --git a/src/radio-api.c b/plugins/radio/radio-api.c similarity index 93% rename from src/radio-api.c rename to plugins/radio/radio-api.c index ef345044..a3e230b1 100644 --- a/src/radio-api.c +++ b/plugins/radio/radio-api.c @@ -461,38 +461,6 @@ STATIC void* _output_thread_fn (void *ctx) { } - -STATIC json_object* start (AFB_session *session, AFB_request *request, void* handle) { - json_object *response; - char query [512]; - - // request all query key/value - getQueryAll (request, query, sizeof(query)); - - // check if we have some post data - if (request->post == NULL) request->post="NoData"; - - // return response to caller - response = jsonNewMessage(AFB_SUCCESS, "Start Radio plugin query={%s} PostData: \'%s\' ", query, request->post); - - //if (verbose) fprintf(stderr, "%d: \n", pingcount); - return (response); -} - -STATIC json_object* stop (AFB_session *session, AFB_request *request, void* handle) { - json_object *response; - char query [512]; - - getQueryAll (request, query, sizeof(query)); - - if (request->post == NULL) request->post="NoData"; - - response = jsonNewMessage(AFB_SUCCESS, "Stop Radio plugin query={%s} PostData: \'%s\' ", query, request->post); - - return (response); -} - - // ******************************************************** // FULUP integration proposal with client session context @@ -641,11 +609,24 @@ STATIC json_object* powerOnOff (AFB_request *request) { // At this point we should have something to retreive radio status before last poweroff [but this is only a demonstrator] } +STATIC json_object* start (AFB_request *request) { + return NULL; +} + +STATIC json_object* stop (AFB_request *request) { + return NULL; +} + +STATIC json_object* status (AFB_request *request) { + return NULL; +} + STATIC AFB_restapi pluginApis[]= { - {"power" , (AFB_apiCB)powerOnOff , "Ping Application Framework"}, - {"start" , (AFB_apiCB)start , "Ping Application Framework"}, - {"stop" , (AFB_apiCB)stop , "Ping Application Framework"}, + {"power" , AFB_SESSION_CREATE, (AFB_apiCB)powerOnOff , "Ping Application Framework"}, + {"start" , AFB_SESSION_CHECK, (AFB_apiCB)start , "Ping Application Framework"}, + {"stop" , AFB_SESSION_CHECK, (AFB_apiCB)stop , "Ping Application Framework"}, + {"status" , AFB_SESSION_RENEW, (AFB_apiCB)status , "Ping Application Framework"}, {NULL} }; diff --git a/src/dbus-api.c b/plugins/samples/HelloWorld.c similarity index 100% rename from src/dbus-api.c rename to plugins/samples/HelloWorld.c diff --git a/src/afbs-api.c b/plugins/samples/SamplePost.c similarity index 100% rename from src/afbs-api.c rename to plugins/samples/SamplePost.c diff --git a/plugins/session/token-api.c b/plugins/session/token-api.c new file mode 100644 index 00000000..9e1c7660 --- /dev/null +++ b/plugins/session/token-api.c @@ -0,0 +1,194 @@ +/* + * Copyright (C) 2015 "IoT.bzh" + * Author "Fulup Ar Foll" + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "local-def.h" + +// Dummy sample of Client Application Context +typedef struct { + int something; + void *whateveryouwant; +} MyClientApplicationHandle; + + +// Request Creation of new context if it does not exist +STATIC json_object* clientContextCreate (AFB_request *request) { + json_object *jresp; + + // add an application specific client context to session + request->client->ctx = malloc (sizeof (MyClientApplicationHandle)); + + // Send response to UI + jresp = json_object_new_object(); + json_object_object_add(jresp, "token", json_object_new_string ("A New Token and Session Context Was Created")); + + return (jresp); +} + +// Before entering here token will be check and renew +STATIC json_object* clientContextRefresh (AFB_request *request) { + json_object *jresp; + + + jresp = json_object_new_object(); + json_object_object_add(jresp, "token", json_object_new_string ("Token was refreshed")); + + return (jresp); +} + + +// Session token will we verified before entering here +STATIC json_object* clientContextCheck (AFB_request *request) { + + json_object *jresp = json_object_new_object(); + json_object_object_add(jresp, "isvalid", json_object_new_boolean (TRUE)); + + return (jresp); +} + + +// Close and Free context +STATIC json_object* clientContextReset (AFB_request *request) { + json_object *jresp; + + jresp = json_object_new_object(); + json_object_object_add(jresp, "uuid", json_object_new_string (request->client->uuid)); + + return (jresp); +} + +// In this case or handle is quite basic +typedef struct { + int fd; +} appPostCtx; + +// This function is call when PostForm processing is completed +STATIC void DonePostForm (AFB_request *request) { + AFB_PostHandle *postHandle = (AFB_PostHandle*)request->post->data; + appPostCtx *appCtx= postHandle->ctx; + + // Close upload file ID + close (appCtx->fd); + + // Free application specific handle + free (postHandle->ctx); + + if (verbose) fprintf (stderr, "DonePostForm upload done\n"); +} + + +// WARNING: PostForm callback are call multiple time (one or each key within form) +// When processing POST_JSON request->data hold a PostHandle and not data directly as for POST_JSON +STATIC json_object* ProcessPostForm (AFB_request *request, AFB_PostItem *item) { + + AFB_PostHandle *postHandle; + appPostCtx *appCtx; + char filepath[512]; + + // When Post is fully processed the same callback is call with a item==NULL + if (item == NULL) { + // Close file, Free handle + + request->errcode = MHD_HTTP_OK; + return(jsonNewMessage(AFB_SUCCESS,"File [%s] uploaded at [%s] error=\n", item->filename, request->config->sessiondir)); + } + + // Let's make sure this is a valid PostForm request + if (!request->post && request->post->type != AFB_POST_FORM) { + request->errcode = MHD_HTTP_FORBIDDEN; + return(jsonNewMessage(AFB_FAIL,"This is not a valid PostForm request\n")); + } else { + // In AFB_POST_FORM case post->data is a PostForm handle + postHandle = (AFB_PostHandle*) request->post->data; + appCtx = (appPostCtx*) postHandle->ctx; + } + + // Check this is a file element + if (0 != strcmp (item->key, "file")) { + request->errcode = MHD_HTTP_FORBIDDEN; + return (jsonNewMessage(AFB_FAIL,"No File within element key=%s\n", item->key)); + } + + // This is the 1st Item iteration let's open output file and allocate necessary resources + if (postHandle->ctx == NULL) { + int fd; + + strncpy (filepath, request->config->sessiondir, sizeof(filepath)); + strncat (filepath, "/", sizeof(filepath)); + strncat (filepath, item->filename, sizeof(filepath)); + + if((fd = open(request->config->sessiondir, O_RDONLY)) < 0) { + request->errcode = MHD_HTTP_FORBIDDEN; + return (jsonNewMessage(AFB_FAIL,"Fail to Upload file [%s] at [%s] error=\n", item->filename, request->config->sessiondir, strerror(errno))); + }; + + // Create an application specific context + appCtx = malloc (sizeof(appPostCtx)); // May place anything here until post->completeCB handle resources liberation + appCtx->fd = fd; + + // attach application to postHandle + postHandle->ctx = (void*) appCtx; // May place anything here until post->completeCB handle resources liberation + postHandle->completeCB = (AFB_apiCB)DonePostForm; // CallBack when Form Processing is finished + + } else { + // this is not the call, FD is already open + appCtx = (appPostCtx*) postHandle->ctx; + } + + // We have something to write + if (item->len > 0) { + + if (!write (appCtx->fd, item->data, item->len)) { + request->errcode = MHD_HTTP_FORBIDDEN; + return (jsonNewMessage(AFB_FAIL,"Fail to write file [%s] at [%s] error=\n", item->filename, strerror(errno))); + } + } + + // every event should return Sucess or Form processing stop + request->errcode = MHD_HTTP_OK; + return NULL; +} + +// This function is call when Client Session Context is removed +// Note: when freeCtxCB==NULL standard free/malloc is called +STATIC void clientContextFree(AFB_clientCtx *client) { + fprintf (stderr,"Plugin[%s] Closing Session uuid=[%s]\n", client->plugin->prefix, client->uuid); + free (client->ctx); +} + +STATIC AFB_restapi pluginApis[]= { + {"ping" , AFB_SESSION_NONE , (AFB_apiCB)apiPingTest ,"Ping Rest Test Service"}, + {"token-create" , AFB_SESSION_CREATE, (AFB_apiCB)clientContextCreate ,"Request Client Context Creation"}, + {"token-refresh" , AFB_SESSION_RENEW , (AFB_apiCB)clientContextRefresh,"Refresh Client Context Token"}, + {"token-check" , AFB_SESSION_CHECK , (AFB_apiCB)clientContextCheck ,"Check Client Context Token"}, + {"token-reset" , AFB_SESSION_CLOSE , (AFB_apiCB)clientContextReset ,"Close Client Context and Free resources"}, + {"file-upload" , AFB_SESSION_NONE , (AFB_apiCB)ProcessPostForm ,"Demo for file upload"}, + {NULL} +}; + +PUBLIC AFB_plugin *afsvRegister () { + AFB_plugin *plugin = malloc (sizeof (AFB_plugin)); + plugin->type = AFB_PLUGIN_JSON; + plugin->info = "Application Framework Binder Service"; + plugin->prefix= "afbs"; // url base + plugin->apis = pluginApis; + plugin->handle= (void*) "What ever you want"; + plugin->freeCtxCB= (void*) clientContextFree; + + return (plugin); +}; \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 663a7917..ed6947e9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,15 +1,6 @@ -SET(AFB-DAEMON_HEADERS local-def.h proto-def.h) -SET(AFB-DAEMON_SOURCES main.c config.c session.c http-svc.c afbs-api.c dbus-api.c rest-api.c alsa-api.c) - IF(librtlsdr_FOUND) ADD_DEFINITIONS(-DWITH_RADIO_PLUGIN=1) - SET(AFB-DAEMON_SOURCES ${AFB-DAEMON_SOURCES} radio-api.c) ENDIF(librtlsdr_FOUND) -ADD_EXECUTABLE(afb-daemon ${AFB-DAEMON_SOURCES}) - -include_directories(${include_dirs}) -target_link_libraries(afb-daemon ${link_libraries}) - -INSTALL(TARGETS afb-daemon - RUNTIME DESTINATION bin) +ADD_LIBRARY(src OBJECT main.c config.c session.c http-svc.c rest-api.c) +INCLUDE_DIRECTORIES(${include_dirs}) diff --git a/src/SamplePost.c b/src/SamplePost.c deleted file mode 100644 index 6d5c44be..00000000 --- a/src/SamplePost.c +++ /dev/null @@ -1,244 +0,0 @@ -/* - * Copyright (C) 2015 "IoT.bzh" - * Author "Fulup Ar Foll" - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include -#include -#include -#include - -#define PORT 8888 -#define POSTBUFFERSIZE 512 -#define MAXCLIENTS 2 - -#define GET 0 -#define POST 1 - -static unsigned int nr_of_uploading_clients = 0; - -struct connection_info_struct -{ - int connectiontype; - struct MHD_PostProcessor *postprocessor; - FILE *fp; - const char *answerstring; - int answercode; -}; - -const char *askpage = "\n\ - Upload a file, please!
\n\ - There are %u clients uploading at the moment.
\n\ -
\n\ - \n\ -
\n\ - "; - -const char *busypage = - "This server is busy, please try again later."; - -const char *completepage = - "The upload has been completed."; - -const char *errorpage = - "This doesn't seem to be right."; -const char *servererrorpage = - "An internal server error has occured."; -const char *fileexistspage = - "This file already exists."; - - -static int -send_page (struct MHD_Connection *connection, const char *page, - int status_code) -{ - int ret; - struct MHD_Response *response; - - response = - MHD_create_response_from_buffer (strlen (page), (void *) page, - MHD_RESPMEM_PERSISTENT); - if (!response) - return MHD_NO; - - ret = MHD_queue_response (connection, status_code, response); - MHD_destroy_response (response); - - return ret; -} - - -static int -iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, - const char *filename, const char *content_type, - const char *transfer_encoding, const char *data, uint64_t off, - size_t size) -{ - struct connection_info_struct *con_info = coninfo_cls; - FILE *fp; - - con_info->answerstring = servererrorpage; - con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR; - - if (0 != strcmp (key, "file")) - return MHD_NO; - - if (!con_info->fp) - { - if (NULL != (fp = fopen (filename, "rb"))) - { - fclose (fp); - con_info->answerstring = fileexistspage; - con_info->answercode = MHD_HTTP_FORBIDDEN; - return MHD_NO; - } - - con_info->fp = fopen (filename, "ab"); - if (!con_info->fp) - return MHD_NO; - } - - if (size > 0) - { - if (!fwrite (data, size, sizeof (char), con_info->fp)) - return MHD_NO; - } - - con_info->answerstring = completepage; - con_info->answercode = MHD_HTTP_OK; - - return MHD_YES; -} - -static void -request_completed (void *cls, struct MHD_Connection *connection, - void **con_cls, enum MHD_RequestTerminationCode toe) -{ - struct connection_info_struct *con_info = *con_cls; - - if (NULL == con_info) - return; - - if (con_info->connectiontype == POST) - { - if (NULL != con_info->postprocessor) - { - MHD_destroy_post_processor (con_info->postprocessor); - nr_of_uploading_clients--; - } - - if (con_info->fp) - fclose (con_info->fp); - } - - free (con_info); - *con_cls = NULL; -} - - -static int -answer_to_connection (void *cls, struct MHD_Connection *connection, - const char *url, const char *method, - const char *version, const char *upload_data, - size_t *upload_data_size, void **con_cls) -{ - if (NULL == *con_cls) - { - struct connection_info_struct *con_info; - - if (nr_of_uploading_clients >= MAXCLIENTS) - return send_page (connection, busypage, MHD_HTTP_SERVICE_UNAVAILABLE); - - con_info = malloc (sizeof (struct connection_info_struct)); - if (NULL == con_info) - return MHD_NO; - - con_info->fp = NULL; - - if (0 == strcmp (method, "POST")) - { - con_info->postprocessor = - MHD_create_post_processor (connection, POSTBUFFERSIZE, - iterate_post, (void *) con_info); - - if (NULL == con_info->postprocessor) - { - free (con_info); - return MHD_NO; - } - - nr_of_uploading_clients++; - - con_info->connectiontype = POST; - con_info->answercode = MHD_HTTP_OK; - con_info->answerstring = completepage; - } - else - con_info->connectiontype = GET; - - *con_cls = (void *) con_info; - - return MHD_YES; - } - - if (0 == strcmp (method, "GET")) - { - int ret; - char buffer[1024]; - - sprintf (buffer, askpage, nr_of_uploading_clients); - return send_page (connection, buffer, MHD_HTTP_OK); - } - - if (0 == strcmp (method, "POST")) - { - struct connection_info_struct *con_info = *con_cls; - - if (0 != *upload_data_size) - { - MHD_post_process (con_info->postprocessor, upload_data, - *upload_data_size); - *upload_data_size = 0; - - return MHD_YES; - } - else - return send_page (connection, con_info->answerstring, - con_info->answercode); - } - - return send_page (connection, errorpage, MHD_HTTP_BAD_REQUEST); -} - -int -main () -{ - struct MHD_Daemon *daemon; - - - daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, - &answer_to_connection, NULL, - MHD_OPTION_NOTIFY_COMPLETED, request_completed, - NULL, MHD_OPTION_END); - if (NULL == daemon) - return 1; - - getchar (); - - MHD_stop_daemon (daemon); - - return 0; -} \ No newline at end of file diff --git a/src/rest-api.c b/src/rest-api.c index ad3a4f22..c1110c51 100644 --- a/src/rest-api.c +++ b/src/rest-api.c @@ -613,7 +613,6 @@ void initPlugins(AFB_session *session) { int i = 0; plugins[i++] = afsvRegister(session), - plugins[i++] = dbusRegister(session), plugins[i++] = alsaRegister(session), #ifdef HAVE_RADIO_PLUGIN plugins[i++] = radioRegister(session),