mirror of
https://github.com/Hizenberg469/Generic_Notification_Chain.git
synced 2026-04-19 18:12:25 +03:00
NFC
This commit is contained in:
1
clearner.sh
Normal file
1
clearner.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
rm *~ .*
|
||||||
1
glthread/cleaner.sh
Normal file
1
glthread/cleaner.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
rm *~ .*
|
||||||
196
glthread/glthread.c
Normal file
196
glthread/glthread.c
Normal file
@@ -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 <stdlib.h>
|
||||||
|
|
||||||
|
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
|
||||||
95
glthread/glthread.h
Normal file
95
glthread/glthread.h
Normal file
@@ -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__ */
|
||||||
96
notif.c
Normal file
96
notif.c
Normal file
@@ -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 <stdlib.h>
|
||||||
|
#include <memory.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#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);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
96
notif.h
Normal file
96
notif.h
Normal file
@@ -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 <stddef.h> /* 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_ */
|
||||||
Reference in New Issue
Block a user