diff --git a/CMakeLists.txt b/CMakeLists.txt index dba321e..9da0dde 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,7 +100,9 @@ install(FILES "${PROJECT_BINARY_DIR}/libwsfs.pc" DESTINATION lib${LIB_SUFFIX}/pk set(WSFS_PROVIDER_SOURCES lib/wsfsp/client.c + lib/wsfsp/client_protocol.c lib/wsfsp/provider.c + lib/wsfsp/provider_default.c ) add_library(wsfs-provider SHARED ${WSFS_PROVIDER_SOURCES}) diff --git a/include/wsfsp/client_protocol.h b/include/wsfsp/client_protocol.h index 119e905..c296801 100644 --- a/include/wsfsp/client_protocol.h +++ b/include/wsfsp/client_protocol.h @@ -1,7 +1,7 @@ #ifndef _WSFSP_CLIENT_PROTOCOL_H #define _WSFSP_CLIENT_PROTOCOL_H -#include "wsfs/api.h" +#include "wsfsp/api.h" struct wsfsp_client_protocol; struct wsfsp_provider; diff --git a/lib/wsfsp/client.c b/lib/wsfsp/client.c index 71aaf7b..0a12f95 100644 --- a/lib/wsfsp/client.c +++ b/lib/wsfsp/client.c @@ -4,13 +4,23 @@ #include #include +#include + #include "wsfsp/provider.h" +#include "wsfsp/client_protocol_intern.h" + +#define WSFSP_DISABLE_LWS_LOG 0 +#define WSFSP_CLIENT_PROTOCOL_COUNT 2 +#define WSFSP_CLIENT_TIMEOUT (1 * 1000) struct wsfsp_client { volatile bool is_running; struct wsfsp_provider provider; - void * user_data; + struct wsfsp_client_protocol protocol; + struct lws_context_creation_info info; + struct lws_protocols protocols[WSFSP_CLIENT_PROTOCOL_COUNT]; + struct lws_context * context; }; @@ -18,12 +28,25 @@ struct wsfsp_client * wsfsp_client_create( struct wsfsp_provider * provider, void * user_data) { + lws_set_log_level(WSFSP_DISABLE_LWS_LOG, NULL); + struct wsfsp_client * client = malloc(sizeof(struct wsfsp_client)); if (NULL != client) { client->is_running = true; - client->user_data = user_data; - memcpy(&client->provider, provider, sizeof(struct wsfsp_provider)); + wsfsp_client_protocol_init(&client->protocol, provider, user_data); + + memset(client->protocols, 0, sizeof(struct lws_protocols) * WSFSP_CLIENT_PROTOCOL_COUNT); + client->protocols[0].name = "fs-provider"; + wsfsp_client_protocol_init_lws(&client->protocol, &client->protocols[0]); + + memset(&client->info, 0, sizeof(struct lws_context_creation_info)); + client->info.port = CONTEXT_PORT_NO_LISTEN; + client->info.protocols = client->protocols; + client->info.uid = -1; + client->info.gid = -1; + + client->context = lws_create_context(&client->info); } return client; @@ -32,6 +55,8 @@ struct wsfsp_client * wsfsp_client_create( void wsfsp_client_dispose( struct wsfsp_client * client) { + lws_context_destroy(client->context); + wsfsp_client_protocol_cleanup(&client->protocol); free(client); } @@ -69,8 +94,7 @@ void wsfsp_client_run( { while (client->is_running) { - // ToDo: implement me - break; + lws_service(client->context, WSFSP_CLIENT_TIMEOUT); } } diff --git a/lib/wsfsp/client_protocol.c b/lib/wsfsp/client_protocol.c new file mode 100644 index 0000000..8265cf4 --- /dev/null +++ b/lib/wsfsp/client_protocol.c @@ -0,0 +1,72 @@ +#include "wsfsp/client_protocol_intern.h" + +#include +#include + +#include + +#include "wsfsp/provider_default.h" +#include "wsfs/util.h" + +static int wsfsp_client_protocol_callback( + struct lws * WSFS_UNUSED_PARAM(wsi), + enum lws_callback_reasons WSFS_UNUSED_PARAM(reason), + void * WSFS_UNUSED_PARAM(user), + void * WSFS_UNUSED_PARAM(in), + size_t WSFS_UNUSED_PARAM(len)) +{ + return 0; +} + + +void wsfsp_client_protocol_init( + struct wsfsp_client_protocol * protocol, + struct wsfsp_provider const * provider, + void * user_data) +{ + protocol->user_data = user_data; + protocol->provider.lookup = (NULL != provider->lookup) ? provider->lookup : &wsfsp_lookup_default; + protocol->provider.getattr = (NULL != provider->getattr) ? provider->getattr : &wsfsp_getattr_default; + protocol->provider.readdir = (NULL != provider->readdir) ? provider->readdir : &wsfsp_readdir_default; + protocol->provider.open = (NULL != provider->open) ? provider->open : &wsfsp_open_default; + protocol->provider.close = (NULL != provider->close) ? provider->close : &wsfsp_close_default; + protocol->provider.read = (NULL != provider->read) ? provider->read : &wsfsp_read_default; + protocol->provider.connected = (NULL != provider->connected) ? provider->connected : &wsfsp_connected_default; + protocol->provider.disconnected = (NULL != provider->disconnected) ? provider->disconnected : &wsfsp_disconnected_default; + protocol->provider.ontimer = (NULL != provider->ontimer) ? provider->ontimer : &wsfsp_ontimer_default; +} + +void wsfsp_client_protocol_cleanup( + struct wsfsp_client_protocol * protocol) +{ + (void) protocol; +} + +struct wsfsp_client_protocol * wsfsp_client_protocol_create( + struct wsfsp_provider const * provider, + void * user_data) +{ + struct wsfsp_client_protocol * protocol = malloc(sizeof(struct wsfsp_client_protocol)); + if (NULL != protocol) + { + wsfsp_client_protocol_init(protocol, provider, user_data); + } + + return protocol; +} + +void wsfsp_client_protocol_dispose( + struct wsfsp_client_protocol * protocol) +{ + wsfsp_client_protocol_cleanup(protocol); + free(protocol); +} + +void wsfsp_client_protocol_init_lws( + struct wsfsp_client_protocol * protocol, + struct lws_protocols * lws_protocol) +{ + lws_protocol->callback = &wsfsp_client_protocol_callback; + lws_protocol->per_session_data_size = 0; + lws_protocol->user = protocol; +} diff --git a/lib/wsfsp/client_protocol_intern.h b/lib/wsfsp/client_protocol_intern.h new file mode 100644 index 0000000..61005b4 --- /dev/null +++ b/lib/wsfsp/client_protocol_intern.h @@ -0,0 +1,30 @@ +#ifndef _WSFSP_CLIENT_PROTOCOL_INTERN_H +#define _WSFSP_CLIENT_PROTOCOL_INTERN_H + +#include "wsfsp/client_protocol.h" +#include "wsfsp/provider.h" + +struct wsfsp_client_protocol +{ + struct wsfsp_provider provider; + void * user_data; +}; + +#ifdef __cplusplus +extern "C" +{ +#endif + +extern void wsfsp_client_protocol_init( + struct wsfsp_client_protocol * protocol, + struct wsfsp_provider const * provider, + void * user_data); + +extern void wsfsp_client_protocol_cleanup( + struct wsfsp_client_protocol * protocol); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/wsfsp/provider_default.c b/lib/wsfsp/provider_default.c new file mode 100644 index 0000000..f38e576 --- /dev/null +++ b/lib/wsfsp/provider_default.c @@ -0,0 +1,106 @@ +#include "wsfsp/provider_default.h" + +void wsfsp_lookup_default( + struct wsfsp_request * request, + ino_t parent, + char const * name, + void * user_data) +{ + (void) parent; + (void) name; + (void) user_data; + + wsfsp_respond_error(request, -1); +} + +void wsfsp_getattr_default( + struct wsfsp_request * request, + ino_t inode, + void * user_data) +{ + (void) inode; + (void) user_data; + + wsfsp_respond_error(request, -1); +} + +void wsfsp_readdir_default( + struct wsfsp_request * request, + ino_t directory, + struct wsfsp_dirbuffer * dirbuffer, + void * user_data) +{ + (void) directory; + (void) dirbuffer; + (void) user_data; + + wsfsp_respond_error(request, -1); +} + +void wsfsp_open_default( + struct wsfsp_request * request, + ino_t inode, + int flags, + void * user_data) +{ + (void) inode; + (void) flags; + (void) user_data; + + wsfsp_respond_error(request, -1); +} + +void wsfsp_close_default( + ino_t inode, + uint32_t handle, + int flags, + void * user_data) +{ + (void) inode; + (void) handle; + (void) flags; + (void) user_data; + + // empty +} + +void wsfsp_read_default( + struct wsfsp_request * request, + ino_t inode, + uint32_t handle, + size_t offset, + size_t length, + void * user_data) +{ + (void) inode; + (void) handle; + (void) offset; + (void) length; + (void) user_data; + + wsfsp_respond_error(request, -1); +} + +void wsfsp_connected_default( + void * user_data) +{ + (void) user_data; + + // empty +} + +void wsfsp_disconnected_default( + void * user_data) +{ + (void) user_data; + + // empty +} + +void wsfsp_ontimer_default( + void * user_data) +{ + (void) user_data; + + // empty +} diff --git a/lib/wsfsp/provider_default.h b/lib/wsfsp/provider_default.h new file mode 100644 index 0000000..3afeee1 --- /dev/null +++ b/lib/wsfsp/provider_default.h @@ -0,0 +1,62 @@ +#ifndef _WSFSP_PROVIDER_DEFAULT_H +#define _WSFSP_PROVIDER_DEFAULT_H + +#include "wsfsp/provider.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +extern void wsfsp_lookup_default( + struct wsfsp_request * request, + ino_t parent, + char const * name, + void * user_data); + +extern void wsfsp_getattr_default( + struct wsfsp_request * request, + ino_t inode, + void * user_data); + +extern void wsfsp_readdir_default( + struct wsfsp_request * request, + ino_t directory, + struct wsfsp_dirbuffer * dirbuffer, + void * user_data); + +extern void wsfsp_open_default( + struct wsfsp_request * request, + ino_t inode, + int flags, + void * user_data); + +extern void wsfsp_close_default( + ino_t inode, + uint32_t handle, + int flags, + void * user_data); + +extern void wsfsp_read_default( + struct wsfsp_request * request, + ino_t inode, + uint32_t handle, + size_t offset, + size_t length, + void * user_data); + +extern void wsfsp_connected_default( + void * user_data); + +extern void wsfsp_disconnected_default( +void * user_data); + +extern void wsfsp_ontimer_default( + void * user_data); + + +#ifdef __cplusplus +} +#endif + +#endif