00001
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <stdlib.h>
00036 #include <string.h>
00037 #include <dlfcn.h>
00038 #include <errno.h>
00039 #include <sys/time.h>
00040
00041 #include "mesh_analysis.h"
00042
00043
00044 int add_analysis(self_t *self, char *name, int id, char *path,
00045 char *ctl_size_fctn, char *analysis_fctn)
00046 {
00047 analysis_t *anly = malloc(sizeof(analysis_t));
00048
00049 anly->sensor = self;
00050 strncpy(anly->name, name, ANLY_NAMELEN);
00051 anly->unique_id = id;
00052
00053 if ((anly->lib_handle = dlopen(path, RTLD_LAZY)) == NULL) {
00054 syslog(LOG_ERR, "add_analysis: %s", dlerror());
00055 free(anly);
00056 return -1;
00057 }
00058
00059 anly->mesh_analysis = dlsym(anly->lib_handle, analysis_fctn);
00060 if ((errstr = dlerror()) != NULL) {
00061 syslog(LOG_ERR, "add_analysis (analysis fctn): %s", errstr);
00062 dlclose(anly->lib_handle);
00063 free(anly);
00064 return -1;
00065 }
00066
00067 if (self->last_a != NULL)
00068 self->last_a->next = anly;
00069
00070 if (self->analyses == NULL) {
00071 self->analyses = anly;
00072 self->last_a = anly;
00073 }
00074 self->last_a = anly;
00075
00076 return 0;
00077 }
00078
00079
00080 int delete_analysis(self_t *self, char *string)
00081 {
00082 analysis_t *prev = NULL, *iter = self->analyses;
00083 while (iter != NULL) {
00084 if (strcasecmp(string, iter->name) == 0) {
00085 if (prev == NULL)
00086 self->analyses = iter->next;
00087 else
00088 prev->next = iter->next;
00089 dlclose(iter->lib_handle);
00090 free(iter);
00091 return 0;
00092 }
00093 prev = iter;
00094 iter = iter->next;
00095 }
00096 return -1;
00097 }
00098
00099
00100 analysis_t *analysis_by_string(self_t *self, char *string)
00101 {
00102 analysis_t *iter = self->analyses;
00103 while (iter != NULL) {
00104 if (strcasecmp(string, iter->name) == 0)
00105 return iter;
00106 iter = iter->next;
00107 }
00108 return NULL;
00109 }
00110
00111
00112 analysis_t *analysis_by_id(self_t *self, int id)
00113 {
00114 analysis_t *iter = self->analyses;
00115 while (iter != NULL) {
00116 if (id == iter->unique_id)
00117 return iter;
00118 iter = iter->next;
00119 }
00120 return NULL;
00121 }
00122
00123
00124 int analyze(self_t *self, struct msghdr *mh)
00125 {
00126 analysis_t *iter = self->analyses;
00127 while (iter != NULL) {
00128 iter->mesh_analysis(mh);
00129 }
00130 }