#define CONFIG_FILE "/etc/setup-adaptor/config.json"
#define CONFIG_FOLDER "/etc/setup-adaptor"
#define CONFIG_NAME "config.json"
+#define EVENT_NAME_MAX 256
#define EVENT_SIZE (sizeof(struct inotify_event))
-#define BUF_LEN (1024 * (EVENT_SIZE + 16))
+#define EVENT_BUF_LEN (512 * (EVENT_SIZE + EVENT_NAME_MAX))
static void *__config_main_loop(void *arg)
{
- int fd;
- int wd;
+ int fd = 0;
+ int wd = 0;
+ int i = 0;
file_state_cb callback;
- char buffer[BUF_LEN];
+ char *buffer = NULL;
_D("__config_main_loop start\n");
fd = inotify_init();
if (fd < 0) {
- _D("inotify_init error");
+ _E("inotify_init error");
return NULL;
}
wd = inotify_add_watch(fd, CONFIG_FOLDER, IN_MODIFY | IN_CREATE | IN_DELETE);
- // Start callack
- _D("Registerd Callback Triggered");
- callback(SA_FILE_STATE_REGISTERED, NULL, NULL);
- while (1) {
- int length, i = 0;
- length = read(fd, buffer, BUF_LEN);
-
- if (length < 0)
- perror("read");
-
- while (i < length) {
- struct inotify_event *event = (struct inotify_event *)&buffer[i];
- _D("[debug] wd=%d mask=%d cookie=%d len=%d dir=%s", event->wd, event->mask, event->cookie, event->len, (event->mask & IN_ISDIR) ? "yes" : "no");
- if (event->len) {
- if (event->mask & IN_CREATE) {
- if (event->mask & IN_ISDIR) {
- _D("The directory %s was created", event->name);
- } else {
- _D("The file %s was create.", event->name);
- if (!strcmp(event->name, CONFIG_NAME))
- _D("config.json is created!!");
- }
- } else if (event->mask & IN_DELETE) {
- if (event->mask & IN_ISDIR) {
- _D("The directory %s was deleted.", event->name);
- } else {
- _D("The file %s was deleted", event->name);
- if (!strcmp(event->name, CONFIG_NAME))
- _D("config.json is deleted!!");
- }
- } else if (event->mask & IN_MODIFY) {
- if (event->mask & IN_ISDIR) {
- _D("The directory %s was modified", event->name);
- } else {
- if (!strcmp(event->name, CONFIG_NAME)) {
- _D("config.json is modified!!");
- callback(SA_FILE_STATE_CHANGED, NULL, NULL);
+
+ if (wd < 0) {
+ _E("inotify_add_watch fail");
+ close(fd);
+ return NULL;
+ }
+
+ buffer = (char *) malloc(EVENT_BUF_LEN);
+
+ if (buffer != NULL) {
+ // Start callack
+ _D("Registerd Callback Triggered");
+ callback(SA_FILE_STATE_REGISTERED, NULL, NULL);
+ while (1) {
+ int length = 0;
+ length = read(fd, buffer, EVENT_BUF_LEN);
+
+ if (length <= 0) {
+ _E("read error");
+ break;
+ }
+
+ i = 0;
+ while (i < length && i < (EVENT_BUF_LEN-EVENT_SIZE)) {
+ struct inotify_event *event = (struct inotify_event *)&buffer[i];
+ if (event->len > 0 && event->len < EVENT_NAME_MAX) {
+ if (event->mask & IN_CREATE) {
+ if (event->mask & IN_ISDIR) {
+ _D("The directory was created");
+ } else {
+ if (!strcmp(event->name, CONFIG_NAME))
+ _D("config.json is created!!");
+ }
+ } else if (event->mask & IN_DELETE) {
+ if (event->mask & IN_ISDIR) {
+ _D("The directory was deleted.");
+ } else {
+ if (!strcmp(event->name, CONFIG_NAME))
+ _D("config.json is deleted!!");
+ }
+ } else if (event->mask & IN_MODIFY) {
+ if (event->mask & IN_ISDIR) {
+ _D("The directory was modified");
+ } else {
+ if (!strcmp(event->name, CONFIG_NAME))
+ callback(SA_FILE_STATE_CHANGED, NULL, NULL);
}
}
+ i += EVENT_SIZE + event->len;
+ } else {
+ _E("event->len is wrong");
+ break;
}
}
-
- i += EVENT_SIZE + event->len;
}
+ } else {
+ _E("buffer is NULL");
}
inotify_rm_watch(fd, wd);
close(fd);
+ if (buffer != NULL)
+ free(buffer);
+
return NULL;
}
int ret = 0;
fd = open(file, O_RDONLY);
- if (fd) {
- len = lseek(fd, 0L, SEEK_END);
+ if (fd < 0) {
+ _E("config file can't be opened");
+ return -1;
+ }
- if (len > 0) {
- lseek(fd, 0L, SEEK_SET);
- jsonData = (char *)malloc(len + 1);
- if (jsonData != NULL) {
- memset(jsonData, 0x00, len + 1);
- readLen = read(fd, jsonData, len);
- _D("JSON full data[%s]", jsonData);
+ len = lseek(fd, 0L, SEEK_END);
- configObj = json_tokener_parse(jsonData);
+ if (len > 0) {
+ lseek(fd, 0L, SEEK_SET);
+ jsonData = (char *)malloc(len + 1);
+ if (jsonData != NULL) {
+ memset(jsonData, 0x00, len + 1);
+ readLen = read(fd, jsonData, len);
+ _D("JSON full data[%s]", jsonData);
- if (configObj != NULL && readLen > 0) {
- // parse version
- ret = __parse_version(configObj, setupConfig);
- if (ret == 0)
- ret = __parse_system_data(configObj, setupConfig);
+ configObj = json_tokener_parse(jsonData);
- if (ret == 0)
- ret = __parse_network_data(configObj, setupConfig);
+ if (configObj != NULL && readLen > 0) {
+ // parse version
+ ret = __parse_version(configObj, setupConfig);
+ if (ret == 0)
+ ret = __parse_system_data(configObj, setupConfig);
- } else {
- ret = -1;
- _D("ConfigObj is not existed");
- }
+ if (ret == 0)
+ ret = __parse_network_data(configObj, setupConfig);
- json_object_put(configObj);
- free(jsonData);
} else {
ret = -1;
- _D("memory allocation fail for jsonData");
+ _D("ConfigObj is not existed");
}
- }
- close(fd);
- } else {
- ret = -1;
- _D("config file can't be opened");
+ json_object_put(configObj);
+ free(jsonData);
+ } else {
+ ret = -1;
+ _D("memory allocation fail for jsonData");
+ }
}
+ close(fd);
+
return ret;
}
void sa_inputfile_set_completion_flag(sa_file_config_e config_type)
{
- int fd;
+ int fd = 0;
char buff[256];
switch (config_type) {