From 60786b94e232de03b30e7fbc97ecd5896abcf305 Mon Sep 17 00:00:00 2001 From: Hizenberg Date: Sun, 10 Mar 2024 12:28:24 +0530 Subject: [PATCH] NFC --- clearner.sh | 1 + glthread/cleaner.sh | 1 + glthread/glthread.c | 196 ++++++++++++++++++++++++++++++++++++++++++++ glthread/glthread.h | 95 +++++++++++++++++++++ notif.c | 96 ++++++++++++++++++++++ notif.h | 96 ++++++++++++++++++++++ 6 files changed, 485 insertions(+) create mode 100644 clearner.sh create mode 100644 glthread/cleaner.sh create mode 100644 glthread/glthread.c create mode 100644 glthread/glthread.h create mode 100644 notif.c create mode 100644 notif.h diff --git a/clearner.sh b/clearner.sh new file mode 100644 index 0000000..5b01d20 --- /dev/null +++ b/clearner.sh @@ -0,0 +1 @@ +rm *~ .* diff --git a/glthread/cleaner.sh b/glthread/cleaner.sh new file mode 100644 index 0000000..5b01d20 --- /dev/null +++ b/glthread/cleaner.sh @@ -0,0 +1 @@ +rm *~ .* diff --git a/glthread/glthread.c b/glthread/glthread.c new file mode 100644 index 0000000..612b251 --- /dev/null +++ b/glthread/glthread.c @@ -0,0 +1,196 @@ +/* + * ===================================================================================== + * + * Filename: glthread.c + * + * Description: + * + * Version: 1.0 + * Created: 09/03/24 07:31:52 PM IST + * Revision: none + * Compiler: gcc + * + * Author: YOUR NAME (), + * Organization: + * + * ===================================================================================== + */ + + + +#include "glthread.h" +#include + +void +init_glthread(glthread_t *glthread){ + + glthread->left = NULL; + glthread->right = NULL; +} + +void +glthread_add_next(glthread_t *curr_glthread, glthread_t *new_glthread){ + + if(!curr_glthread->right){ + curr_glthread->right = new_glthread; + new_glthread->left = curr_glthread; + return; + } + + glthread_t *temp = curr_glthread->right; + curr_glthread->right = new_glthread; + new_glthread->left = curr_glthread; + new_glthread->right = temp; + temp->left = new_glthread; +} + +void +glthread_add_before(glthread_t *curr_glthread, glthread_t *new_glthread){ + + if(!curr_glthread->left){ + new_glthread->left = NULL; + new_glthread->right = curr_glthread; + curr_glthread->left = new_glthread; + return; + } + + glthread_t *temp = curr_glthread->left; + temp->right = new_glthread; + new_glthread->left = temp; + new_glthread->right = curr_glthread; + curr_glthread->left = new_glthread; +} + +void +remove_glthread(glthread_t *curr_glthread){ + + if(!curr_glthread->left){ + if(curr_glthread->right){ + curr_glthread->right->left = NULL; + curr_glthread->right = 0; + return; + } + return; + } + if(!curr_glthread->right){ + curr_glthread->left->right = NULL; + curr_glthread->left = NULL; + return; + } + + curr_glthread->left->right = curr_glthread->right; + curr_glthread->right->left = curr_glthread->left; + curr_glthread->left = 0; + curr_glthread->right = 0; +} + +void +delete_glthread_list(glthread_t *glthread_head){ + + glthread_t *glthreadptr = NULL; + + ITERATE_GLTHREAD_BEGIN(glthread_head, glthreadptr){ + remove_glthread(glthreadptr); + } ITERATE_GLTHREAD_END(glthread_head, glthreadptr); +} + +void +glthread_add_last(glthread_t *glthread_head, glthread_t *new_glthread){ + + glthread_t *glthreadptr = NULL, + *prevglthreadptr = NULL; + + ITERATE_GLTHREAD_BEGIN(glthread_head, glthreadptr){ + prevglthreadptr = glthreadptr; + } ITERATE_GLTHREAD_END(glthread_head, glthreadptr); + + if(prevglthreadptr) + glthread_add_next(prevglthreadptr, new_glthread); + else + glthread_add_next(glthread_head, new_glthread); +} + +unsigned int +get_glthread_list_count(glthread_t *glthread_head){ + + unsigned int count = 0; + glthread_t *glthreadptr = NULL; + + ITERATE_GLTHREAD_BEGIN(glthread_head, glthreadptr){ + count++; + } ITERATE_GLTHREAD_END(glthread_head, glthreadptr); + return count; +} + + +void +glthread_priority_insert(glthread_t *glthread_head, + glthread_t *glthread, + int (*comp_fn)(void *, void *), + int offset){ + + + glthread_t *curr = NULL, + *prev = NULL; + + init_glthread(glthread); + + if(IS_GLTHREAD_LIST_EMPTY(glthread_head)){ + glthread_add_next(glthread_head, glthread); + return; + } + + /* Only one node*/ + if(glthread_head->right && !glthread_head->right->right){ + if(comp_fn(GLTHREAD_GET_USER_DATA_FROM_OFFSET(glthread, offset), + GLTHREAD_GET_USER_DATA_FROM_OFFSET(glthread_head->right, offset)) == -1){ + glthread_add_next(glthread_head, glthread); + } + else{ + glthread_add_next(glthread_head->right, glthread); + } + return; + } + + ITERATE_GLTHREAD_BEGIN(glthread_head, curr){ + + if(comp_fn(GLTHREAD_GET_USER_DATA_FROM_OFFSET(glthread, offset), + GLTHREAD_GET_USER_DATA_FROM_OFFSET(curr, offset)) != -1){ + prev = curr; + continue; + } + + if(!prev) + glthread_add_next(glthread_head, glthread); + else + glthread_add_next(prev, glthread); + + return; + + }ITERATE_GLTHREAD_END(glthread_head, curr); + + /*Add in the end*/ + glthread_add_next(prev, glthread); +} + +glthread_t * +dequeue_glthread_first(glthread_t *base_glthread){ + + glthread_t *temp; + if(!base_glthread->right) + return NULL; + temp = base_glthread->right; + remove_glthread(temp); + return temp; +} + +#if 0 +void * +gl_thread_search(glthread_t *glthread_head, + void *(*thread_to_struct_fn)(glthread_t *), + void *key, + int (*comparison_fn)(void *, void *)){ + + return NULL; +} +#endif diff --git a/glthread/glthread.h b/glthread/glthread.h new file mode 100644 index 0000000..e221b99 --- /dev/null +++ b/glthread/glthread.h @@ -0,0 +1,95 @@ +/* + * ===================================================================================== + * + * Filename: glthread.h + * + * Description: + * + * Version: 1.0 + * Created: 09/03/24 07:30:40 PM IST + * Revision: none + * Compiler: gcc + * + * Author: YOUR NAME (), + * Organization: + * + * ===================================================================================== + */ + + +#ifndef __GLUETHREAD__ +#define __GLUETHREAD__ + +typedef struct _glthread{ + + struct _glthread *left; + struct _glthread *right; +} glthread_t; + +void +glthread_add_next(glthread_t *base_glthread, glthread_t *new_glthread); + +void +glthread_add_before(glthread_t *base_glthread, glthread_t *new_glthread); + +void +remove_glthread(glthread_t *glthread); + +void +init_glthread(glthread_t *glthread); + +void +glthread_add_last(glthread_t *base_glthread, glthread_t *new_glthread); + +#define IS_GLTHREAD_LIST_EMPTY(glthreadptr) \ + ((glthreadptr)->right == 0 && (glthreadptr)->left == 0) + +#define GLTHREAD_TO_STRUCT(fn_name, structure_name, field_name,glthreadptr) \ + static inline structure_name * fn_name(glthread_t *glthreadptr){ \ + if(glthreadptr)\ + return (structure_name *)((char *)(glthreadptr) - (char *)&(((structure_name *)0)->field_name)); \ + else return NULL;\ + } + +/* delete safe loop*/ +/*Normal continue and break can be used with this loop macro*/ + +#define BASE(glthreadptr) ((glthreadptr)->right) + +#define ITERATE_GLTHREAD_BEGIN(glthreadptrstart, glthreadptr) \ +{ \ + glthread_t *_glthread_ptr = NULL; \ + glthreadptr = BASE(glthreadptrstart); \ + for(; glthreadptr!= NULL; glthreadptr = _glthread_ptr){ \ + _glthread_ptr = (glthreadptr)->right; + +#define ITERATE_GLTHREAD_END(glthreadptrstart, glthreadptr) \ + }} + +#define GLTHREAD_GET_USER_DATA_FROM_OFFSET(glthreadptr, offset) \ + (void *)((char *)(glthreadptr) - offset) + +void +delete_glthread_list(glthread_t *base_glthread); + +unsigned int +get_glthread_list_count(glthread_t *base_glthread); + +void +glthread_priority_insert(glthread_t *base_glthread, + glthread_t *glthread, + int (*comp_fn)(void *, void *), + int offset); + +glthread_t * +dequeue_glthread_first(glthread_t *base_glthread); + +#if 0 +void * +gl_thread_search(glthread_t *base_glthread, + void *(*thread_to_struct_fn)(glthread_t *), + void *key, + int (*comparison_fn)(void *, void *)); + +#endif +#endif /* __GLUETHREAD__ */ diff --git a/notif.c b/notif.c new file mode 100644 index 0000000..ce88d23 --- /dev/null +++ b/notif.c @@ -0,0 +1,96 @@ +/* + * ===================================================================================== + * + * Filename: notif.c + * + * Description: + * + * Version: 1.0 + * Created: 10/03/24 12:00:51 PM IST + * Revision: none + * Compiler: gcc + * + * Author: YOUR NAME (), + * Organization: + * + * ===================================================================================== + */ + +#include +#include +#include +#include "notif.h" + +notif_chain_t * +nfc_create_new_notif_chain(char *notif_chain_name) { + + notif_chain_t *nfc = calloc(1, sizeof(notif_chain_t)); + if(notif_chain_name) { + strncpy(nfc->nfc_name, notif_chain_name, + sizeof(nfc->nfc_name)); + } + init_glthread(&nfc->notif_chain_head); + return nfc; +} + +void +nfc_register_notif_chain(notif_chain_t *nfc, + notif_chain_elem_t *nfce){ + + notif_chain_elem_t *new_nfce = calloc(1, sizeof(notif_chain_elem_t)); + memcpy(new_nfce, nfce, sizeof(notif_chain_elem_t)); + init_glthread(&new_nfce->glue); + glthread_add_next(&nfc->notif_chain_head, &new_nfce->glue); +} + +void +nfc_invoke_notif_chain(notif_chain_t *nfc, + void *arg, size_t arg_size, + char *key, size_t key_size, + nfc_op_t nfc_op_code){ + + glthread_t *curr; + notif_chain_elem_t *nfce; + + if(IS_GLTHREAD_LIST_EMPTY(&nfc->notif_chain_head)) { + return; + } + + assert(key_size <= MAX_NOTIF_KEY_SIZE); + + char *nfc_op_s = nfc_get_str_op_code(nfc_op_code); + + ITERATE_GLTHREAD_BEGIN(&nfc->notif_chain_head, curr){ + + nfce = glthread_glue_to_notif_chain_elem(curr); + + if(!(key && key_size && + nfce->is_key_set && (key_size == nfce->key_size))){ + + nfce->app_cb(arg, arg_size, nfc_op_s, nfce->subs_id); + } + else { + + if(memcmp(key, nfce->key, key_size) == 0) { + + nfce->app_cb(arg, arg_size, nfc_op_s, nfce->subs_id); + } + } + }ITERATE_GLTHREAD_END(&nfc->notif_chain_head, curr); +} + +void +nfc_delete_all_nfce(notif_chain_t *nfc){ + + glthread *curr; + notif_chain_elem_t *nfc_e; + + ITERATE_GLTHREAD_BEGIN(&nfc->notif_chain_head,curr){ + + nfc_e = glthread_glue_to_notif_chain_elem(curr); + remove_glthread(&nfc_e->glue); + free(nfc_e); + }ITERATE_GLTHREAD_END(&nfc->notif_chain_head,curr); + + +} diff --git a/notif.h b/notif.h new file mode 100644 index 0000000..0910e32 --- /dev/null +++ b/notif.h @@ -0,0 +1,96 @@ +/* + * ===================================================================================== + * + * Filename: notif.h + * + * Description: + * + * Version: 1.0 + * Created: 10/03/24 12:02:16 PM IST + * Revision: none + * Compiler: gcc + * + * Author: YOUR NAME (), + * Organization: + * + * ===================================================================================== + */ + +#ifndef __NOTIF_CHAIN_ +#define __NOTIF_CHAIN_ + +#include /* for size_t */ +#include "utils.h" +#include "gluethread/glthread.h" + +#define MAX_NOTIF_KEY_SIZE 64 + +typedef enum{ + + NFC_UNKNOWN, + NFC_SUB, + NFC_ADD, + NFC_MOD, + NFC_DEL, +} nfc_op_t; + +static inline char * +nfc_get_str_op_code(nfc_op_t nfc_op_code) { + + static char op_code_str[16]; + + switch(nfc_op_code) { + + case NFC_UNKNOWN: + return "NFC_UNKNOWN"; + case NFC_SUB: + return "NFC_SUB"; + case NFC_ADD: + return "NFC_ADD"; + case NFC_MOD: + return "NFC_MOD"; + case NFC_DEL: + return "NFC_DEL"; + default: + return NULL; + } +} + + +typedef void (*nfc_app_cb)(void *, size_t, char *, uint32_t); + +typedef struct notif_chain_elem_{ + + char key[MAX_NOTIF_KEY_SIZE]; + size_t key_size; + uint32_t subs_id; + bool_t is_key_set; + nfc_app_cb app_cb; + glthread_t glue; +} notif_chain_elem_t; +GLTHREAD_TO_STRUCT(glthread_glue_to_notif_chain_elem, + notif_chain_elem_t, glue); + +typedef struct notif_chain_ { + + char nfc_name[64]; + glthread_t notif_chain_head; +} notif_chain_t; + +notif_chain_t * +nfc_create_new_notif_chain(char *notif_chain_name); + +void +nfc_delete_all_nfce(notif_chain_t *nfc); + +void +nfc_register_notif_chain(notif_chain_t *nfc, + notif_chain_elem_t *nfce); + +void +nfc_invoke_notif_chain(notif_chain_t *nfc, + void *arg, size_t arg_size, + char *key, size_t key_size, + nfc_op_t nfc_op_code); + +#endif /* __NOTIF_CHAIN_ */