瀏覽代碼

first commit

Signed-off-by: wlxuz <myxuan475@126.com>
wlxuz 11 月之前
當前提交
050869a25e

+ 7 - 0
CMakeLists.txt

@@ -0,0 +1,7 @@
+add_subdirectory(apps)
+add_subdirectory(http)
+
+install(DIRECTORY dist
+        DESTINATION webconfig/
+        FILES_MATCHING PATTERN "*"
+)

+ 21 - 0
apps/CMakeLists.txt

@@ -0,0 +1,21 @@
+# 子模块参考模版
+include(${ROOT_DIR}/buildtools/cmake/toolchain_common.cmake)
+
+# 模块名称
+set(MODULE apps)
+
+aux_source_directory(. DIRSRCS)
+include_directories(./)
+
+build_subdirectory(thirdparty/mosquitto)
+build_subdirectory(thirdparty/SQLiteCpp include thirdparty/SQLiteCpp/sqlite3)
+build_subdirectory(thirdparty/json-c define STDC_HEADERS)
+
+message("current dir: ${CMAKE_CURRENT_SOURCE_DIR}")
+
+add_library(${MODULE} ${DIRSRCS})
+target_link_libraries(${MODULE} sqlite3 json-c rt pthread m)
+
+install(TARGETS ${MODULE} DESTINATION webconfig)
+install(FILES app.json DESTINATION webconfig)
+install(FILES device_db.db DESTINATION webconfig)

+ 55 - 0
apps/Makefile

@@ -0,0 +1,55 @@
+### Application-specific constants
+APP_NAME = apps
+### Environment constants 
+#ARCH := arm
+#CROSS_COMPILE :=  ../../buildroot-at91-2018.02-at91/output/host/bin/arm-linux-
+#CROSS_COMPILE := /home/logan/EmbeddedProject/RTU/buildroot/buildroot-at91-2018.02-at91/output/host/bin/arm-linux-
+#CROSS_COMPILE := /home/logan/iot/EdgeGateway/a40_src/out/sun8iw11p1/linux/common/buildroot/host/opt/ext-toolchain/bin/arm-linux-gnueabi-
+
+OBJDIR = obj
+INCLUDES = $(wildcard inc/*.h)
+
+### External constant definitions
+# must get library build option to know if mpsse must be linked or not
+
+### Constant symbols
+
+CC := $(CROSS_COMPILE)gcc
+AR := $(CROSS_COMPILE)ar
+#CC := gcc
+#AR := ar
+
+CFLAGS := -O2 -Wall -Wextra  -Iinc -I.
+##CFLAGS := -O2 -Wall -Wextra -I.
+
+# List the library sub-modules that are used by the application
+
+### Linking options
+
+#LIBS := -lmosquitto -lcrypto -lsqlite3 -ljson-c -lrt -lpthread -lm
+#LIBS := -lmosquitto -lcrypto -lsqlite3 -ljson-c -lrt -lpthread -lm -Wl,-rpath=../../buildroot-at91-2018.02-at91/output/target/usr/lib
+LIBS := -lmosquitto  -lsqlite3 -ljson-c -lrt -lpthread -lm
+
+### General build targets
+
+all: $(APP_NAME)
+
+clean:
+	rm -f *.o
+	rm -f $(APP_NAME)
+
+### Sub-modules compilation
+%.o: %.c
+	$(CC) $(CFLAGS) -c  $< -o $@
+
+$(APP_NAME):apps.o  sql_api.o common.o
+	$(CC)  $^ -o $@ $(LIBS)
+
+#sql_api: command.o sql_api.o common.o
+#	$(CC)   $^ -o $@ $(LIBS)
+
+install:
+	echo "install"
+
+### EOF
+

+ 35 - 0
apps/app.json

@@ -0,0 +1,35 @@
+{
+    "system":{
+        "uuid":"0102030405060708",
+        "boot_time":"2023-12-14 21:41:52",
+        "product_name":"LoRa Gateway",
+        "product_model":"M1001_For_Modbus",
+        "hw_version":"M1000_hardware_V1.0.1",
+        "sw_version":"M1000_firmware_V1.0.1",
+        "total_memory":"1000",
+        "used_memory":"200",
+        "total_flash":"1000",
+        "used_flash":"1000",
+        "ethernet_wan":"connected",
+        "wan_mac":"C2:96:78:2C:CE:D3",
+        "ethernet_lan":"disconnected",
+        "lan_mac":"C2:96:78:2C:CE:D3",
+        "lte_status":"disconnected",
+        "lte_mac":"C2:96:78:2C:CE:D3"
+    },
+
+    "user":{
+        "user":"admin",
+        "passwd":"leo2023"
+    },
+
+    "platform":{
+        "platform_proto":"modbus",
+        "platform_server":"123.60.64.212",
+        "platform_port":"9401",
+        "platform_id":"1"
+    }
+
+}
+
+

+ 906 - 0
apps/apps.c

@@ -0,0 +1,906 @@
+#include "common.h"
+#include <arpa/inet.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <json-c/json.h>
+#include <json-c/json_object.h>
+#include <json-c/json_util.h>
+#include <mosquitto.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <netinet/ip.h>
+#include <netinet/tcp.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <termios.h>
+#include <time.h>
+#include <unistd.h>
+
+#define MAX_VCOM 4
+
+#define ETH_0_NAME "eth0"
+#define ETH_1_NAME "eth1"
+
+#define GET_LAN_IP "ifconfig eth0 | grep 'inet addr' | awk -F'[ :]+' '{print $4}'"
+#define GET_LAN_NETMASK "ifconfig eth0 | grep 'inet addr' | awk -F'[ :]+' '{print $8}'"
+#define GET_LAN_GATEWAY "ifconfig eth0 | grep 'inet addr' | awk -F'[ :]+' '{print $6}'"
+#define GET_LAN_MACADDR "ifconfig eth0 | grep 'HWaddr' | awk -F' ' '{print $5}'"
+#define GET_LAN_STATE "cat /sys/class/net/eth0/operstate"
+
+#define GET_WAN_IP "ifconfig eth1 | grep 'inet addr' | awk -F'[ :]+' '{print $4}'"
+#define GET_WAN_NETMASK "ifconfig eth1 | grep 'inet addr' | awk -F'[ :]+' '{print $8}'"
+#define GET_WAN_GATEWAY "ifconfig eth1 | grep 'inet addr' | awk -F'[ :]+' '{print $6}'"
+#define GET_WAN_MACADDR "ifconfig eth1 | grep 'HWaddr' | awk -F' ' '{print $5}'"
+#define GET_WAN_STATE "cat /sys/class/net/eth1/operstate"
+
+#define GET_LTE_IP "ifconfig usb0 | grep 'inet addr' | awk -F'[ :]+' '{print $4}'"
+#define GET_LTE_NETMASK "ifconfig usb0 | grep 'inet addr' | awk -F'[ :]+' '{print $8}'"
+#define GET_LTE_GATEWAY "ifconfig usb0 | grep 'inet addr' | awk -F'[ :]+' '{print $6}'"
+#define GET_LTE_MACADDR "ifconfig usb0 | grep 'HWaddr' | awk -F' ' '{print $5}'"
+#define GET_LTE_STATE "cat /sys/class/net/usb0/operstate"
+
+#define GET_WLAN_IP "ifconfig wlan0 | grep 'inet addr' | awk -F'[ :]+' '{print $4}'"
+#define GET_WLAN_NETMASK "ifconfig wlan0 | grep 'inet addr' | awk -F'[ :]+' '{print $8}'"
+#define GET_WLAN_GATEWAY "ifconfig wlan0 | grep 'inet addr' | awk -F'[ :]+' '{print $6}'"
+#define GET_WLAN_MACADDR "ifconfig wlan0 | grep 'HWaddr' | awk -F' ' '{print $5}'"
+#define GET_WLAN_STATE "cat /sys/class/net/wlan0/operstate"
+
+#define GET_TOTAL_MEM "free -m | grep 'Mem:' | awk -F' ' '{print $2}'"
+#define GET_USED_MEM "free -m | grep 'Mem:' | awk -F' ' '{print $3}'"
+
+#define GET_TOTAL_FLASH "4096"
+#define GET_USED_FLASH "362"
+
+#define GET_NOWTIME "date +'%Y-%m-%d %H:%M.%S'"
+
+int vcom_fd[MAX_VCOM];
+struct mosquitto* mosq_up_mqtt[MAX_VCOM];
+struct mosquitto* mosq_dw_mqtt[MAX_VCOM];
+
+volatile bool exit_sig = false;
+volatile bool quit_sig = false;
+int net_sock = 0;
+
+struct json_object* root_obj = NULL;
+
+pthread_mutex_t mx_db = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t root_lock;
+
+struct mosquitto* mosq_http;
+
+static uint16_t CRC16(const uint8_t* p_data, uint32_t len, uint16_t* p_crc)
+{
+    uint32_t i;
+    uint16_t crc = 0xffff;
+    if (NULL == p_data || NULL == p_crc || 0 == len) {
+        return -1;
+    }
+    for (i = 0; i < len; i++) {
+        crc = (unsigned char)(crc >> 8) | (crc << 8);
+        crc ^= p_data[i];
+        crc ^= (unsigned char)(crc & 0xff) >> 4;
+        crc ^= (crc << 8) << 4;
+        crc ^= ((crc & 0xff) << 4) << 1;
+    }
+    *p_crc = crc;
+    return 0;
+}
+
+void exec_cmd(char* cmd, char* buf)
+{
+    FILE* fp = NULL;
+
+    if ((fp = popen(cmd, "r")) == NULL) {
+        printf("error: popen failed!\r\n");
+        exit(1);
+    }
+
+    while (fgets(buf, 199, fp) != NULL) {
+        if (buf[strlen(buf) - 1] == '\n')
+            buf[strlen(buf) - 1] = '\0';
+    }
+
+    pclose(fp);
+}
+
+static void sig_handler(int sigio)
+{
+    if (sigio == SIGQUIT) {
+        quit_sig = true;
+    } else if ((sigio == SIGINT) || (sigio == SIGTERM)) {
+        exit_sig = true;
+    }
+
+    return;
+}
+
+/*
+
+modbus  ---> json
+{
+ "reg_0001":23,
+ "reg_0002":24
+}
+
+
+*/
+int dwlink_packet_process(uint8_t* device_id_str,
+                          uint8_t* dw_buffer,
+                          uint8_t* dw_buffer_len,
+                          uint8_t* attr_addr_str,
+                          uint8_t* attr_value_str)
+{
+    struct json_object* dw_obj = NULL;
+
+    dw_obj = json_object_new_object();
+    if (dw_obj == NULL) {
+        printf("dw_obj is null\r\n");
+        return NULL;
+    }
+
+    json_object_object_add(dw_obj, attr_addr_str, json_object_new_string(attr_value_str));
+    if (strlen(json_object_to_json_string(dw_obj)) < 255) {
+        memcpy(dw_buffer, json_object_to_json_string(dw_obj), strlen(json_object_to_json_string(dw_obj)));
+        *dw_buffer_len = strlen(json_object_to_json_string(dw_obj));
+    }
+
+    json_object_put(dw_obj);
+}
+
+void config_init()
+{
+    root_obj = json_object_from_file("./app.json");
+    if (root_obj == NULL) {
+        return 0;
+    }
+}
+
+void mosq_http_connect_callback(struct mosquitto* mosq, void* obj, int rc)
+{
+    printf("http_json_connect_callback set\r\n");
+}
+
+void mosq_http_disconnect_callback(struct mosquitto* mosq, void* obj, int result)
+{
+    printf("http_json_disconnect_callback set\r\n");
+}
+
+struct json_object* get_system_json()
+{
+    struct json_object *rsp_obj = NULL, *system_obj = NULL;
+
+    rsp_obj = json_object_new_object();
+    if (rsp_obj == NULL) {
+        printf("rsp_obj is null\r\n");
+        return NULL;
+    }
+
+    json_object_object_get_ex(root_obj, "system", &system_obj);
+    json_object_object_add(rsp_obj, "system", system_obj);
+    json_object_get(system_obj);
+    json_object_object_add(rsp_obj, "state", json_object_new_string("success"));
+
+    return rsp_obj;
+}
+
+struct json_object* get_platform_json()
+{
+    struct json_object *rsp_obj = NULL, *platform_obj = NULL;
+
+    rsp_obj = json_object_new_object();
+    if (rsp_obj == NULL) {
+        printf("rsp_obj is null\r\n");
+        return NULL;
+    }
+
+    json_object_object_get_ex(root_obj, "platform", &platform_obj);
+    json_object_object_add(rsp_obj, "platform", platform_obj);
+    json_object_get(platform_obj);
+    json_object_object_add(rsp_obj, "state", json_object_new_string("success"));
+
+    return rsp_obj;
+}
+
+struct json_object* get_user_json()
+{
+    struct json_object *rsp_obj = NULL, *user_obj = NULL;
+
+    rsp_obj = json_object_new_object();
+    if (rsp_obj == NULL) {
+        printf("rsp_obj is null\r\n");
+        return NULL;
+    }
+    json_object_object_get_ex(root_obj, "user", &user_obj);
+    json_object_object_add(rsp_obj, "user", user_obj);
+    json_object_get(user_obj);
+    json_object_object_add(rsp_obj, "state", json_object_new_string("success"));
+
+    return rsp_obj;
+}
+
+struct json_object* get_devices_json()
+{
+    struct json_object* rsp_obj = NULL;
+
+    select_all_device(&rsp_obj);
+
+    return rsp_obj;
+}
+
+struct json_object* set_devices_json(char* payload)
+{
+    struct json_object *payload_obj = NULL, *item_obj = NULL, *devices_obj = NULL, *device_id_obj = NULL,
+                       *device_name_obj = NULL, *device_addr_obj = NULL, *application_id_obj = NULL;
+    char *device_id_str = NULL, *device_name_str = NULL, *device_addr_str = NULL, *application_id_str = NULL;
+    int len = 0, index = 0;
+
+    payload_obj = json_tokener_parse(payload);
+    if (payload_obj == NULL) {
+        printf("payload is null\r\n");
+        return NULL;
+    }
+
+    json_object_object_get_ex(payload_obj, "devices", &devices_obj);
+    len = json_object_array_length(devices_obj);
+    for (index = 0; index < len; index++) {
+        item_obj = json_object_array_get_idx(devices_obj, index);
+        json_object_object_get_ex(item_obj, "device_id", &device_id_obj);
+        device_id_str = json_object_get_string(device_id_obj);
+        json_object_object_get_ex(item_obj, "device_name", &device_name_obj);
+        device_name_str = json_object_get_string(device_name_obj);
+        json_object_object_get_ex(item_obj, "device_addr", &device_addr_obj);
+        device_addr_str = json_object_get_string(device_addr_obj);
+        json_object_object_get_ex(item_obj, "application_id", &application_id_obj);
+        application_id_str = json_object_get_string(application_id_obj);
+
+        pthread_mutex_lock(&mx_db);
+        update_device_by_device_id(tolower(device_id_str), tolower(device_name_str), atoi(device_addr_str),
+                                   tolower(application_id_str));
+        pthread_mutex_unlock(&mx_db);
+    }
+
+    json_object_object_add(payload_obj, "state", json_object_new_string("success"));
+
+    return payload_obj;
+}
+
+struct json_object* set_platform_json(char* payload)
+{
+    struct json_object *payload_obj = NULL, *platform_obj = NULL;
+
+    payload_obj = json_tokener_parse(payload);
+    if (payload_obj == NULL) {
+        printf("payload is null\r\n");
+        return NULL;
+    }
+
+    json_object_object_get_ex(payload_obj, "platform", &platform_obj);
+    json_object_object_add(root_obj, "platform", platform_obj);
+    json_object_get(platform_obj);
+    json_object_to_file_ext("./app.json", root_obj, JSON_C_TO_STRING_PRETTY);
+    sync();
+    json_object_object_add(payload_obj, "state", json_object_new_string("success"));
+
+    return payload_obj;
+}
+
+struct json_object* del_devices_json(char* payload)
+{
+    struct json_object *payload_obj = NULL, *item_obj = NULL, *devices_obj = NULL, *device_id_obj = NULL,
+                       *device_name_obj = NULL, *device_addr_obj = NULL, *module_id_obj = NULL;
+    char *device_id_str = NULL, *device_name_str = NULL, *device_addr_str = NULL, *module_id_str = NULL;
+    int len = 0, index = 0;
+
+    payload_obj = json_tokener_parse(payload);
+    if (payload_obj == NULL) {
+        printf("payload is null\r\n");
+        return NULL;
+    }
+
+    json_object_object_get_ex(payload_obj, "devices", &devices_obj);
+    len = json_object_array_length(devices_obj);
+    for (index = 0; index < len; index++) {
+        item_obj = json_object_array_get_idx(devices_obj, index);
+        json_object_object_get_ex(item_obj, "device_id", &device_id_obj);
+        device_id_str = json_object_get_string(device_id_obj);
+
+        pthread_mutex_lock(&mx_db);
+        delete_device_by_device_id(device_id_str);
+        pthread_mutex_unlock(&mx_db);
+    }
+
+    json_object_object_add(payload_obj, "state", json_object_new_string("success"));
+
+    return payload_obj;
+}
+
+struct json_object* add_devices_json(char* payload)
+{
+    struct json_object *payload_obj = NULL, *item_obj = NULL, *devices_obj = NULL, *device_id_obj = NULL,
+                       *device_name_obj = NULL, *device_addr_obj = NULL, *application_id_obj = NULL;
+    char *device_id_str = NULL, *device_name_str = NULL, *device_addr_str = NULL, *application_id_str = NULL;
+    int len = 0, index = 0;
+    int ret = 0;
+
+    payload_obj = json_tokener_parse(payload);
+    if (payload_obj == NULL) {
+        printf("payload is null\r\n");
+        return NULL;
+    }
+
+    json_object_object_get_ex(payload_obj, "devices", &devices_obj);
+    len = json_object_array_length(devices_obj);
+    for (index = 0; index < len; index++) {
+        item_obj = json_object_array_get_idx(devices_obj, index);
+        json_object_object_get_ex(item_obj, "device_id", &device_id_obj);
+        device_id_str = json_object_get_string(device_id_obj);
+        json_object_object_get_ex(item_obj, "device_name", &device_name_obj);
+        device_name_str = json_object_get_string(device_name_obj);
+        json_object_object_get_ex(item_obj, "device_addr", &device_addr_obj);
+        device_addr_str = json_object_get_string(device_addr_obj);
+        json_object_object_get_ex(item_obj, "application_id", &application_id_obj);
+        application_id_str = json_object_get_string(application_id_obj);
+
+        pthread_mutex_lock(&mx_db);
+        ret = insert_device_item(tolower(device_id_str), tolower(device_name_str), atoi(device_addr_str),
+                                 tolower(application_id_str));
+        pthread_mutex_unlock(&mx_db);
+    }
+
+    if (ret == 0)
+        json_object_object_add(payload_obj, "state", json_object_new_string("success"));
+    else
+        json_object_object_add(payload_obj, "state", json_object_new_string("fail"));
+
+    return payload_obj;
+}
+
+// req:get/devices/none/http
+// rsp:ack/get/device/none/http
+void mosq_http_json_message_callback(struct mosquitto* mosq, void* obj, const struct mosquitto_message* msg)
+{
+    struct json_object* rsp_obj = NULL;
+    char** topics;
+    int topic_count;
+    char pub_topic[200];
+
+    mosquitto_sub_topic_tokenise(msg->topic, &topics, &topic_count);
+    if (strncmp(topics[0], "get", STR_MAX_LEN(topics[0], "get")) == 0) {
+        if (strncmp(topics[1], "system", STR_MAX_LEN(topics[1], "system")) == 0) {
+            pthread_mutex_lock(&root_lock);
+            rsp_obj = get_system_json();
+            pthread_mutex_unlock(&root_lock);
+
+            if (rsp_obj != NULL) {
+                printf("get system rsp_obj:%s\r\n", json_object_to_json_string(rsp_obj));
+            }
+
+        } else if (strncmp(topics[1], "platform", STR_MAX_LEN(topics[1], "platform")) == 0) {
+            pthread_mutex_lock(&root_lock);
+            rsp_obj = get_platform_json();
+            pthread_mutex_unlock(&root_lock);
+
+            if (rsp_obj != NULL) {
+                printf("get platform rsp_obj:%s\r\n", json_object_to_json_string(rsp_obj));
+            }
+        } else if (strncmp(topics[1], "user", STR_MAX_LEN(topics[1], "user")) == 0) {
+            pthread_mutex_lock(&root_lock);
+            rsp_obj = get_user_json();
+            pthread_mutex_unlock(&root_lock);
+
+            if (rsp_obj != NULL) {
+                printf("get user rsp_obj:%s\r\n", json_object_to_json_string(rsp_obj));
+            }
+        } else if (strncmp(topics[1], "devices", STR_MAX_LEN(topics[1], "devices")) == 0) {
+            pthread_mutex_lock(&root_lock);
+            rsp_obj = get_devices_json();
+            pthread_mutex_unlock(&root_lock);
+
+            if (rsp_obj != NULL) {
+                printf("get devices rsp_obj:%s\r\n", json_object_to_json_string(rsp_obj));
+            }
+        }
+    } else if (strncmp(topics[0], "set", STR_MAX_LEN(topics[0], "set")) == 0) {
+        if (strncmp(topics[1], "platform", STR_MAX_LEN(topics[1], "platform")) == 0) {
+            pthread_mutex_lock(&root_lock);
+            rsp_obj = set_platform_json(msg->payload);
+            pthread_mutex_unlock(&root_lock);
+
+            if (rsp_obj != NULL) {
+                printf("set platform rsp_obj:%s\r\n", json_object_to_json_string(rsp_obj));
+            }
+        } else if (strncmp(topics[1], "devices", STR_MAX_LEN(topics[1], "devices")) == 0) {
+            pthread_mutex_lock(&root_lock);
+            rsp_obj = set_devices_json(msg->payload);
+            pthread_mutex_unlock(&root_lock);
+
+            if (rsp_obj != NULL) {
+                printf("set devices rsp_obj:%s\r\n", json_object_to_json_string(rsp_obj));
+            }
+        }
+    } else if (strncmp(topics[0], "del", STR_MAX_LEN(topics[0], "del")) == 0) {
+        if (strncmp(topics[1], "devices", STR_MAX_LEN(topics[1], "devices")) == 0) {
+            pthread_mutex_lock(&root_lock);
+            rsp_obj = del_devices_json(msg->payload);
+            pthread_mutex_unlock(&root_lock);
+
+            if (rsp_obj != NULL) {
+                printf("del devices rsp_obj:%s\r\n", json_object_to_json_string(rsp_obj));
+            }
+        }
+    } else if (strncmp(topics[0], "add", STR_MAX_LEN(topics[0], "add")) == 0) {
+        if (strncmp(topics[1], "devices", STR_MAX_LEN(topics[1], "devices")) == 0) {
+            pthread_mutex_lock(&root_lock);
+            rsp_obj = add_devices_json(msg->payload);
+            pthread_mutex_unlock(&root_lock);
+            if (rsp_obj != NULL) {
+                printf("add devices rsp_obj:%s\r\n", json_object_to_json_string(rsp_obj));
+            }
+        }
+    }
+
+    if (rsp_obj != NULL) {
+        sprintf(pub_topic, "ack/%s/%s/%s/%s", topics[0], topics[1], topics[2], topics[3]);
+        mosquitto_publish(mosq, NULL, pub_topic, strlen(json_object_to_json_string(rsp_obj)),
+                          json_object_to_json_string(rsp_obj), 1, false);
+        json_object_put(rsp_obj);
+    }
+
+    mosquitto_sub_topic_tokens_free(&topics, topic_count);
+
+    printf("mosq_http_json_message_callback\r\n");
+}
+
+void mosq_http_json_subscribe_callback(struct mosquitto* mosq,
+                                       void* obj,
+                                       int mid,
+                                       int qos_count,
+                                       const int* granted_qos)
+{
+    printf("http_json_subscribe_callback set\r\n");
+}
+
+// req:get/devices/none/http
+// rsp:ack/get/device/none/http
+void http_json_process_thread(void)
+{
+    char pub_topic[200];
+    char id[50];
+
+    snprintf(id, 50, "http_json_%d", getpid());
+    mosq_http = mosquitto_new(id, true, NULL);
+    mosquitto_connect_callback_set(mosq_http, mosq_http_connect_callback);
+    mosquitto_disconnect_callback_set(mosq_http, mosq_http_disconnect_callback);
+    mosquitto_message_callback_set(mosq_http, mosq_http_json_message_callback);
+    mosquitto_subscribe_callback_set(mosq_http, mosq_http_json_subscribe_callback);
+    mosquitto_connect(mosq_http, "localhost", 1883, 600);
+    // mosquitto_username_pw_set(mosq_http,"leo_mqtt", "leo_mqtt@");
+    mosquitto_subscribe(mosq_http, NULL, "+/+/+/http", 1);
+
+    while (!exit_sig && !quit_sig) {
+        if (mosquitto_loop(mosq_http, 100, 1)) {
+            mosquitto_reconnect(mosq_http);
+            sleep(10);
+        }
+
+        usleep(500000);
+    }
+
+    printf("http_json_process_thread exit\r\n");
+}
+
+int get_system_status()
+{
+    char buf[200];
+
+    struct json_object* system_obj = NULL;
+
+    json_object_object_get_ex(root_obj, "system", &system_obj);
+
+    memset(buf, '\0', 200);
+    exec_cmd(GET_WAN_MACADDR, buf);
+    // printf("WAN-ADDR:%s\r\n",buf);
+
+    json_object_object_add(system_obj, "wan_mac", json_object_new_string(buf));
+
+    memset(buf, '\0', 200);
+    exec_cmd(GET_WAN_STATE, buf);
+    // printf("WAN-STTE:%s\r\n",buf);
+    if (strncmp(buf, "up", STR_MAX_LEN("up", buf)) == 0) {
+        json_object_object_add(system_obj, "ethernet_wan", json_object_new_string("连接"));
+    } else {
+        json_object_object_add(system_obj, "ethernet_wan", json_object_new_string("断开"));
+    }
+
+    memset(buf, '\0', 200);
+    exec_cmd(GET_LAN_MACADDR, buf);
+    // printf("LAN-ADDR:%s\r\n",buf);
+    json_object_object_add(system_obj, "lan_mac", json_object_new_string(buf));
+
+    memset(buf, '\0', 200);
+    exec_cmd(GET_LAN_STATE, buf);
+    // printf("LAN-STATE:%s\r\n",buf);
+    if (strncmp(buf, "up", STR_MAX_LEN("up", buf)) == 0) {
+        json_object_object_add(system_obj, "ethernet_lan", json_object_new_string("连接"));
+    } else {
+        json_object_object_add(system_obj, "ethernet_lan", json_object_new_string("断开"));
+    }
+
+    memset(buf, '\0', 200);
+    exec_cmd(GET_LTE_MACADDR, buf);
+    // printf("LTE-ADDR:%s\r\n",buf);
+    json_object_object_add(system_obj, "lte_mac", json_object_new_string(buf));
+
+    memset(buf, '\0', 200);
+    exec_cmd(GET_LTE_STATE, buf);
+    // printf("LTE-STATE:%s\r\n",buf);
+    if (strncmp(buf, "up", STR_MAX_LEN("up", buf)) == 0) {
+        json_object_object_add(system_obj, "lte_status", json_object_new_string("连接"));
+    } else {
+        json_object_object_add(system_obj, "lte_status", json_object_new_string("断开"));
+    }
+
+    memset(buf, '\0', 200);
+    exec_cmd(GET_TOTAL_MEM, buf);
+    // printf("TOTAL-MEM:%s\r\n",buf);
+    json_object_object_add(system_obj, "total_memory", json_object_new_string(buf));
+
+    memset(buf, '\0', 200);
+    exec_cmd(GET_USED_MEM, buf);
+    // printf("USED-MEM:%s\r\n",buf);
+    json_object_object_add(system_obj, "used_memory", json_object_new_string(buf));
+
+    memset(buf, '\0', 200);
+    // exec_cmd(GET_TOTAL_FLASH, buf);
+    json_object_object_add(system_obj, "total_flash", json_object_new_string("4096"));
+
+    memset(buf, '\0', 200);
+    // exec_cmd(GET_USED_FLASH, buf);
+    json_object_object_add(system_obj, "used_flash", json_object_new_string("361"));
+
+    memset(buf, '\0', 200);
+    exec_cmd(GET_NOWTIME, buf);
+    json_object_object_add(system_obj, "boot_time", json_object_new_string(buf));
+}
+
+void mosq_up_mqtt_connect_callback(struct mosquitto* mosq, void* obj, int rc)
+{
+    printf("mosq_up_mqtt_connect_callback set\r\n");
+}
+
+void mosq_up_mqtt_disconnect_callback(struct mosquitto* mosq, void* obj, int result)
+{
+    printf("mosq_up_mqtt_disconnect_callback set\r\n");
+}
+
+void mosq_up_mqtt_message_callback(struct mosquitto* mosq, void* obj, const struct mosquitto_message* msg)
+{
+    struct json_object *payload_obj = NULL, *devEUI_obj = NULL, *object_obj = NULL;
+    char* devEUI_str = NULL;
+
+    payload_obj = json_tokener_parse(msg->payload);
+    if (payload_obj == NULL)
+        goto _exit_0;
+
+    json_object_object_get_ex(payload_obj, "devEUI", &devEUI_obj);
+    devEUI_str = json_object_get_string(devEUI_obj);
+    json_object_object_get_ex(payload_obj, "object", &object_obj);
+    if (object_obj == NULL || object_obj == "") {
+        goto _exit_0;
+    }
+
+    json_object_object_foreach(object_obj, key, val)
+    {
+        printf("key:%s --- value:%s   \r\n", key, json_object_to_json_string(val));
+        update_attrtab_item_by_device_id(devEUI_str, key, json_object_to_json_string(val));
+    }
+
+    json_object_put(payload_obj);
+
+_exit_0:
+    printf("mqtt_process_message_callback\r\n");
+}
+
+void mosq_up_mqtt_subscribe_callback(struct mosquitto* mosq, void* obj, int mid, int qos_count, const int* granted_qos)
+{
+    printf("mosq_up_mqtt_subscribe_callback set\r\n");
+}
+
+/*
+uplink topic: application/+/device/+/event/up
+
+{"applicationID":"1","applicationName":"leo","deviceName":"1000000000000001","deviceProfileName":"leo_otaa_c","deviceProfileID":"172bf053-caf5-49d1-8bdb-5d989aef9ec1","devEUI":"1000000000000001","rxInfo":[{"gatewayID":"aa555a0000000000","uplinkID":"312238b9-ffc3-4a4d-914d-e8ace8ffbae1","name":"AA555A0000000000","rssi":-93,"loRaSNR":8.2,"location":{"latitude":0,"longitude":0,"altitude":0}}],"txInfo":{"frequency":476100000,"dr":2},"adr":false,"fCnt":3,"fPort":2,"data":"MTIzNDU2","object":{"reg_0001":49,"reg_0002":50}}
+
+dwlink topic: application/+/device/+/command/down
+*/
+
+void mqtt_modbus_uplink_process_thread(void* argc)
+{
+    char sub_topic[200];
+    char id[50];
+    int mqtt_index = *((int*)argc);
+
+    printf("mqtt_index_uplink:%d \r\n", mqtt_index);
+    snprintf(id, 50, "mqtt_modbus_up_%d", mqtt_index);
+    printf("mqtt_uplink_id:%s\r\n", id);
+    mosq_up_mqtt[mqtt_index] = mosquitto_new(id, true, NULL);
+    mosquitto_connect_callback_set(mosq_up_mqtt[mqtt_index], mosq_up_mqtt_connect_callback);
+    mosquitto_disconnect_callback_set(mosq_up_mqtt[mqtt_index], mosq_up_mqtt_disconnect_callback);
+    mosquitto_message_callback_set(mosq_up_mqtt[mqtt_index], mosq_up_mqtt_message_callback);
+    mosquitto_subscribe_callback_set(mosq_up_mqtt[mqtt_index], mosq_up_mqtt_subscribe_callback);
+    mosquitto_connect(mosq_up_mqtt[mqtt_index], "localhost", 1883, 600);
+    sprintf(sub_topic, "application/%d/device/+/event/up", mqtt_index + 1);
+    mosquitto_username_pw_set(mosq_up_mqtt[mqtt_index], "leo_mqtt", "leo_mqtt@");
+    mosquitto_subscribe(mosq_up_mqtt[mqtt_index], NULL, sub_topic, 0);
+
+    while (!exit_sig && !quit_sig) {
+        if (mosquitto_loop(mosq_up_mqtt[mqtt_index], 100, 1)) {
+            mosquitto_reconnect(mosq_up_mqtt[mqtt_index]);
+            sleep(10);
+        }
+        usleep(500000);
+    }
+
+    mosquitto_disconnect(mosq_up_mqtt[mqtt_index]);
+    mosquitto_destroy(mosq_up_mqtt[mqtt_index]);
+
+    printf("mqtt_modbus_uplink_process_thread exit\r\n");
+}
+
+void mosq_dw_mqtt_connect_callback(struct mosquitto* mosq, void* obj, int rc)
+{
+    printf("mosq_dw_mqtt_connect_callback set\r\n");
+}
+void mosq_dw_mqtt_disconnect_callback(struct mosquitto* mosq, void* obj, int result)
+{
+    printf("mosq_dw_mqtt_disconnect_callback set\r\n");
+}
+void mosq_dw_mqtt_message_callback(struct mosquitto* mosq, void* obj, const struct mosquitto_message* msg)
+{
+    printf("mosq_dw_mqtt_message_callback\r\n");
+}
+void mosq_dw_mqtt_subscribe_callback(struct mosquitto* mosq, void* obj, int mid, int qos_count, const int* granted_qos)
+{
+    printf("mosq_dw_mqtt_subscribe_callback set\r\n");
+}
+
+/*
+dwlink topic: application/+/device/+/command/down
+*/
+void mqtt_modbus_dwlink_process_thread(void* argc)
+{
+    uint8_t rx_buffer[255] = {0};
+    uint8_t tx_buffer[255] = {0};
+    int ret = 0, i = 0, j = 0, rx_buffer_len = 0, tx_buffer_len = 0;
+    char attr_addr_str[9], attr_value_str[5];
+    uint16_t attr_value = 0, attr_num = 0, attr_addr = 0, crc = 0;
+    uint8_t device_addr = 0, device_funcode = 0;
+    uint8_t device_id[17];
+    char pub_topic[200];
+    char id[50];
+
+    uint8_t dw_buffer[255];
+    uint8_t dw_buffer_len = 0;
+
+    int mqtt_index = *((int*)argc);
+
+    printf("dwmqtt_index:%d\r\n", mqtt_index);
+    snprintf(id, 50, "mqtt_modbus_dw_%d", mqtt_index);
+    printf("dwid:%s\r\n", id);
+    mosq_dw_mqtt[mqtt_index] = mosquitto_new(id, true, NULL);
+    mosquitto_connect_callback_set(mosq_dw_mqtt[mqtt_index], mosq_dw_mqtt_connect_callback);
+    mosquitto_disconnect_callback_set(mosq_dw_mqtt[mqtt_index], mosq_dw_mqtt_disconnect_callback);
+    mosquitto_message_callback_set(mosq_dw_mqtt[mqtt_index], mosq_dw_mqtt_message_callback);
+    mosquitto_subscribe_callback_set(mosq_dw_mqtt[mqtt_index], mosq_dw_mqtt_subscribe_callback);
+    mosquitto_username_pw_set(mosq_dw_mqtt[mqtt_index], "leo_mqtt", "leo_mqtt@");
+    mosquitto_connect(mosq_dw_mqtt[mqtt_index], "localhost", 1883, 600);
+
+    while (!exit_sig && !quit_sig) {
+        ret = read(vcom_fd[mqtt_index], rx_buffer, 255);
+        if (ret > 0) {
+            rx_buffer_len = ret;
+            printf("info: rx modbus data -->");
+            for (i = 0; i < rx_buffer_len; i++)
+                printf("0x%x ", rx_buffer[i]);
+            printf("\r\n");
+            crc = App_Crc16(rx_buffer, rx_buffer_len - 2);
+            if (crc != (*(rx_buffer + rx_buffer_len - 2) << 8 | *(rx_buffer + rx_buffer_len - 1))) {
+                printf("rx modbus crc failed \r\n");
+                tcflush(vcom_fd[mqtt_index], TCIOFLUSH);
+                continue;
+            }
+
+            device_addr = rx_buffer[0];
+            device_funcode = rx_buffer[1];
+            tx_buffer[0] = device_addr;
+            tx_buffer[1] = device_funcode;
+
+            switch (device_funcode) {
+                case 0x03: {
+                    attr_addr = rx_buffer[2] << 8 | rx_buffer[3];
+                    attr_num = rx_buffer[4] << 8 | rx_buffer[5];
+                    tx_buffer[2] = attr_num * 2;
+                    for (j = 0; j < attr_num; j++) {
+                        sprintf(attr_addr_str, "reg_%04x", (attr_addr + j));
+                        attr_addr_str[8] = '\0';
+                        pthread_mutex_lock(&mx_db);
+                        printf("attr_id:%s --device_addr:%d --appid:%d\r\n", attr_addr_str, device_addr,
+                               mqtt_index + 1);
+                        select_attr_by_device_addr(&attr_value, device_addr, attr_addr_str, mqtt_index + 1);
+                        pthread_mutex_unlock(&mx_db);
+                        tx_buffer[3 + 2 * j] = (attr_value >> 8 & 0xff);
+                        tx_buffer[3 + 2 * j + 1] = (attr_value >> 0 & 0xff);
+                    }
+
+                    crc = App_Crc16(tx_buffer, 3 + attr_num * 2);
+                    tx_buffer[3 + 2 * attr_num] = (crc >> 8 & 0xff);
+                    tx_buffer[3 + 2 * attr_num + 1] = (crc >> 0 & 0xff);
+                    tx_buffer_len = 3 + 2 * attr_num + 2;
+
+                    break;
+                }
+                case 0x06: {
+                    attr_addr = rx_buffer[2] << 8 | rx_buffer[3];
+                    attr_value = rx_buffer[4] << 8 | rx_buffer[5];
+                    sprintf(attr_addr_str, "reg_%04x", attr_addr);
+                    attr_addr_str[8] = '\0';
+                    sprintf(attr_value_str, "%d", attr_value);
+                    attr_value_str[4] = '\0';
+                    tx_buffer[2] = (attr_addr >> 8 & 0xff);
+                    tx_buffer[3] = (attr_addr >> 0 & 0xff);
+                    tx_buffer[4] = (attr_value >> 8 & 0xff);
+                    tx_buffer[5] = (attr_value >> 0 & 0xff);
+                    crc = App_Crc16(tx_buffer, 6);
+                    tx_buffer[6] = (crc >> 8 & 0xff);
+                    tx_buffer[7] = (crc >> 0 & 0xff);
+                    tx_buffer_len = 8;
+
+                    if (select_device_id_by_device_addr(device_addr, device_id, mqtt_index) == 1) {
+                        printf("attr_addr:%s --- attr-value:%s \r\n", attr_addr_str, attr_value_str);
+
+                        // update sqllite
+                        pthread_mutex_lock(&mx_db);
+                        update_tab_item_by_device_addr(device_addr, attr_addr_str, attr_value_str, mqtt_index);
+                        pthread_mutex_unlock(&mx_db);
+
+                        dwlink_packet_process(device_id, dw_buffer, &dw_buffer_len, attr_addr_str, attr_value_str);
+
+                        // publish dwlink/devices/id/mqtt
+                        sprintf(pub_topic, "application/%d/device/%s/command/down", mqtt_index + 1, device_id);
+                        mosquitto_publish(mosq_dw_mqtt[mqtt_index], NULL, pub_topic, dw_buffer_len, dw_buffer, 1,
+                                          false);
+
+                        printf("publish:\r\n");
+                        for (i = 0; i < dw_buffer_len; i++)
+                            printf("%02x ", dw_buffer[i]);
+                        printf("\r\n");
+                    }
+                    break;
+                }
+                default: {
+                    tx_buffer[0] = device_addr;
+                    tx_buffer[1] = 0x83;
+                    tx_buffer[2] = 0x02;
+                    tx_buffer_len = 3;
+                    break;
+                }
+            }
+
+            write(vcom_fd[mqtt_index], tx_buffer, tx_buffer_len);
+            printf("info: tx modbus data -->");
+            for (i = 0; i < tx_buffer_len; i++)
+                printf("0x%x ", tx_buffer[i]);
+            printf("\r\n");
+
+        } else if (ret < 0) {
+            continue;
+        }
+
+        if (mosquitto_loop(mosq_dw_mqtt[mqtt_index], 100, 1)) {
+            mosquitto_reconnect(mosq_dw_mqtt[mqtt_index]);
+            sleep(10);
+        }
+
+        usleep(500000);
+    }
+
+    mosquitto_disconnect(mosq_dw_mqtt[mqtt_index]);
+    mosquitto_destroy(mosq_dw_mqtt[mqtt_index]);
+
+    printf("mqtt_modbus_dwlink_process_thread exit\r\n");
+}
+
+int vcom_init()
+{
+    char* dev[] = {"/dev/tty10", "/dev/tty12", "/dev/tty14", "/dev/tty16"};
+    int i = 0;
+
+    for (i = 0; i < MAX_VCOM; i++) {
+        vcom_fd[i] = open(dev[i], O_RDWR | O_NOCTTY | O_NDELAY);  // open("/dev/tttyS0",O_RDWR | O_NOCTTY);  //O_NDELAY
+        if (vcom_fd[i] == -1) {
+            printf("can't open :%s \r\n", dev[i]);
+            return -1;
+        }
+    }
+
+    printf("vcom fd open success\r\n");
+
+    return 1;
+}
+
+int main(int argc, char* argv[])
+{
+    pthread_t mqtt_modbus_uplink_tid[MAX_VCOM], mqtt_modbus_dwlink_tid[MAX_VCOM], http_json_tid;
+    int err = 0, i = 0;
+    int thread_index[MAX_VCOM] = {0, 1, 2, 3};
+    struct sigaction sigact;
+
+    mosquitto_lib_init();
+    init_device_db();
+    pthread_mutex_init(&root_lock, NULL);
+
+    config_init();
+
+    vcom_init();
+
+    for (i = 0; i < MAX_VCOM; i++) {
+        printf("mqtt modbus vcom_%d uplink thread\r\n", i);
+        err = pthread_create(&mqtt_modbus_uplink_tid[i], NULL, (void* (*)(void*))mqtt_modbus_uplink_process_thread,
+                             (void*)(&thread_index[i]));
+        if (err != 0) {
+            printf("create mqtt modbus vcom_%d uplink thread failed\r\n", i);
+            exit(EXIT_FAILURE);
+        }
+
+        printf("mqtt modbus vcom_%d dwlink thread\r\n", i);
+        err = pthread_create(&mqtt_modbus_dwlink_tid[i], NULL, (void* (*)(void*))mqtt_modbus_dwlink_process_thread,
+                             (void*)(&thread_index[i]));
+        if (err != 0) {
+            printf("create mqtt modbus vcomd_%d dwlink thread failed\r\n", i);
+            exit(EXIT_FAILURE);
+        }
+    }
+
+    // web
+    printf("http json process thread\r\n");
+    err = pthread_create(&http_json_tid, NULL, (void* (*)(void*))http_json_process_thread, NULL);
+    if (err != 0) {
+        printf("create http json thread failed\r\n");
+        exit(EXIT_FAILURE);
+    }
+
+    sigemptyset(&sigact.sa_mask);
+    sigact.sa_flags = 0;
+    sigact.sa_handler = sig_handler;
+    sigaction(SIGQUIT, &sigact, NULL);
+    sigaction(SIGINT, &sigact, NULL);
+    sigaction(SIGTERM, &sigact, NULL);
+
+    while (!exit_sig && !quit_sig) {
+        pthread_mutex_lock(&root_lock);
+        get_system_status();
+        pthread_mutex_unlock(&root_lock);
+        sleep(120);
+    }
+
+    for (i = 0; i < MAX_VCOM; i++) {
+        pthread_join(mqtt_modbus_uplink_tid[i], NULL);
+        pthread_join(mqtt_modbus_dwlink_tid[i], NULL);
+    }
+
+    pthread_join(http_json_tid, NULL);
+    if (exit_sig || quit_sig) {
+        // shutdown(net_sock, SHUT_RDWR);
+    }
+
+    pthread_mutex_destroy(&root_lock);
+    mosquitto_lib_cleanup();
+    exit(EXIT_SUCCESS);
+}

+ 155 - 0
apps/common.c

@@ -0,0 +1,155 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+#include <signal.h>
+#include <unistd.h>
+#include "common.h"
+
+
+static const uint8_t table_crc_hi[] = {
+    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
+    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+    0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
+    0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
+    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
+    0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
+    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
+    0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
+    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
+    0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
+    0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
+    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
+    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
+    0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
+    0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
+    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
+    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+    0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
+    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+    0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
+    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
+    0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
+    0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
+    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
+};
+
+static const uint8_t table_crc_lo[] = {
+    0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
+    0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
+    0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
+    0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
+    0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
+    0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
+    0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
+    0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
+    0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
+    0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
+    0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
+    0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
+    0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
+    0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
+    0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
+    0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
+    0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
+    0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
+    0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
+    0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
+    0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
+    0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
+    0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
+    0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
+    0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
+    0x43, 0x83, 0x41, 0x81, 0x80, 0x40
+};
+
+
+uint16_t App_Crc16(uint8_t *buffer, uint16_t buffer_length)
+{
+    uint8_t crc_hi = 0xFF; 
+    uint8_t crc_lo = 0xFF; 
+    unsigned int i; 
+
+    while (buffer_length--) {
+        i = crc_hi ^ *buffer++;
+        crc_hi = crc_lo ^ table_crc_hi[i];
+        crc_lo = table_crc_lo[i];
+    }
+
+    return (crc_hi << 8 | crc_lo);
+}
+
+
+void timestamp_get(char *timestamp)
+{	
+	time_t t;
+
+	t = time(NULL);  
+	sprintf(timestamp,"%lld",((long long)t)*1000);
+}
+
+
+uint8_t CharToHex(uint8_t ch)
+{
+    uint8_t val = (uint8_t)-1;
+
+    if ((ch >= '0') && (ch <='9')) 
+	{
+        val = ch - '0';
+    }
+    else if ((ch >= 'A') && (ch <='F')) 
+	{
+        val = ch - 'A' + 10;
+    }
+    else if ((ch >= 'a') && (ch <='f')) 
+	{
+        val = ch - 'a' + 10;
+    }
+    return val;
+}
+
+
+
+void HexToString(char *des, uint8_t *src , uint16_t len)
+{
+    uint16_t i;
+    uint8_t *p;
+    char tmp[4];
+
+    p = src;
+    for(i=0; i<len; i++) 
+	{
+        memset((void *)tmp, 0, sizeof(tmp));
+        sprintf(tmp, "%02x", *p++);
+        strcat(des, tmp);
+    }
+}
+
+
+int StringToHex(uint8_t *pbDest, const char *pbSrc, uint16_t srcLen)
+{
+    uint16_t i=0,j=0;
+    uint8_t chl,chh;
+
+    memset(pbDest, 0, srcLen/2);
+    while (i < srcLen) 
+	{
+        if ((i+1) == srcLen) 
+		{  
+            chl = CharToHex(pbSrc[i]);
+            pbDest[j] = chl<<4;
+            j ++;
+        }
+		else
+		{
+            chh = CharToHex(pbSrc[i]);
+            chl = CharToHex(pbSrc[i+1]);
+            pbDest[j] = (chh << 4) | chl;
+            j ++;
+        }
+        i = i+2;
+    }
+    return j;
+}

+ 21 - 0
apps/common.h

@@ -0,0 +1,21 @@
+#ifndef __COMMON_H__
+#define __COMMON_H__
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <signal.h>
+#include <unistd.h>
+#include <sys/types.h>
+
+#define STR_MAX_LEN(str1,str2)	((strlen(str1)>strlen(str2))?strlen(str1):strlen(str2))
+
+void timestamp_get(char *timestamp);
+
+uint16_t App_Crc16(uint8_t *buffer, uint16_t buffer_length);
+
+void HexToString(char *des, uint8_t *src , uint16_t len);
+int StringToHex(uint8_t *pbDest, const char *pbSrc, uint16_t srcLen);
+
+#endif

二進制
apps/device_db.db


+ 426 - 0
apps/sql_api.c

@@ -0,0 +1,426 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "sqlite3.h"
+#include "sql_api.h"
+
+int callback_db(void *ptr, int count)
+{
+    usleep(500000);
+    return -1;
+}
+
+int do_cmd_str(char* sql)
+{
+	int rc;
+	sqlite3* db;
+	char *errmsg = NULL;
+
+	rc = sqlite3_open(SQL_NAME, &db);	
+	if ( rc != SQLITE_OK )	
+	{		
+		printf("ERROR: open sqlite %s \r\n",sqlite3_errmsg(db));		
+		sqlite3_close(db);		
+		return -1;
+	}
+	
+	sqlite3_busy_handler(db, callback_db, (void*)db);
+	rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
+	if (rc != SQLITE_OK )
+	{
+		printf("ERROR: %s-%s \r\n", sql, errmsg);
+		sqlite3_close(db);
+		return -1;
+	}
+	
+	sqlite3_close(db);
+	return 0;
+}
+
+//初始化设备数据库
+int init_device_db(void)
+{
+	char cmd[MAX_TXET_LEN];
+	sprintf(cmd,"%s","create table if not exists device_db(device_id text primary key , device_name text, device_addr integer unique, application_id text);");
+	if(do_cmd_str(cmd) != 0)
+		return -1;
+	return 0;
+}
+
+//插入设备到数据库
+int insert_device_item(char*device_id,char*device_name,uint8_t device_addr, char* application_id)
+{
+	char cmd[MAX_TXET_LEN];
+	sprintf(cmd,"insert into device_db(device_id,device_name,device_addr,application_id) values('%s','%s',%d,'%s');", device_id,device_name,device_addr,application_id);
+	if(do_cmd_str(cmd)!=0)
+		return -1;
+	return 0;
+}
+
+
+//更新设备到数据库
+int update_device_by_device_id(char*device_id,char*device_name,uint8_t device_addr,char *application_id)
+{
+	char cmd[MAX_TXET_LEN];
+	sprintf(cmd,"update device_db set device_id = '%s',device_name = '%s', device_addr = %d, application_id = '%s' where device_id = '%s';", device_id,device_name,device_addr,application_id,device_id);
+	if(do_cmd_str(cmd)!=0)
+		return -1;
+	return 0;
+}
+
+//删除设备通过设备id
+int delete_device_by_device_id( char*device_id )
+{
+	char cmd[MAX_TXET_LEN];
+	sprintf(cmd,"delete from device_db where device_id = '%s';", device_id);
+	if(do_cmd_str(cmd)!=0)
+		return -1;
+	return 0;
+}
+
+
+int update_attrtab_item_by_device_id(char*device_id, char*attr_id, char*attr_value)
+{
+	char cmd[MAX_TXET_LEN];
+	int rc,nRow,nColumn,i=0;
+	sqlite3* db;
+	char **result=NULL, *errmsg = NULL;
+	
+	
+	rc = sqlite3_open(SQL_NAME, &db);	
+	if ( rc != SQLITE_OK )	
+	{		
+		printf("ERROR:open sqlite %s \r\n",sqlite3_errmsg(db));		
+		sqlite3_close(db);		
+		return -1;
+	}
+	
+	sprintf(cmd,"select * from device_db limit 0;");
+	rc = sqlite3_get_table(db,cmd,&result,&nRow,&nColumn,&errmsg);
+	if (rc != SQLITE_OK)
+	{
+		printf("ERROR: get table %s \r\n", errmsg);
+		sqlite3_close(db);
+		return -1;
+	}
+
+	for(i=0; i<nColumn ;i++)
+	{	
+		if(strcmp(attr_id, result[i])==0)
+			break;
+	}
+	
+	if( i == nColumn)
+	{
+		sprintf(cmd, "alter table device_db add column %s text", attr_id);
+		sqlite3_busy_handler(db, callback_db, (void*)db);
+		sqlite3_exec(db,cmd,NULL,NULL,&errmsg);
+		
+		sprintf(cmd, "update device_db set  %s = '%s' where device_id = '%s';", attr_id,attr_value,device_id);
+		sqlite3_busy_handler(db, callback_db, (void*)db);
+		sqlite3_exec(db,cmd,NULL,NULL,&errmsg);
+	}
+	else
+	{
+		sprintf(cmd, "update device_db set  %s = '%s' where device_id = '%s';", attr_id,attr_value,device_id);
+		sqlite3_busy_handler(db, callback_db, (void*)db);
+		sqlite3_exec(db,cmd,NULL,NULL,&errmsg);
+	}
+	
+	sqlite3_free_table(result);
+	sqlite3_close(db);
+	
+	return 1;	
+}
+
+
+
+//更新数据到数据库通过设备id 
+int update_tab_item_by_device_id(char*device_id,char *attr_id, char *attr_value)
+{
+	char cmd[MAX_TXET_LEN];
+	sprintf(cmd,"update device_db set %s = '%s' where device_id = '%s';", attr_id,attr_value,device_id);
+	if(do_cmd_str(cmd)!=0)
+		return -1;
+	return 0;
+}
+//更新数据到数据库通过设备地址
+int update_tab_item_by_device_addr(uint8_t device_addr,char *attr_id, char *attr_value, uint8_t application_id)
+{
+	char cmd[MAX_TXET_LEN];
+	sprintf(cmd,"update device_db set %s = '%s' where device_addr = %d and application_id = %d;", attr_id,attr_value,device_addr,application_id);
+	if(do_cmd_str(cmd)!=0)
+		return -1;
+	return 0;
+}
+
+//查询设备列表
+int select_all_device(struct json_object **rsp_obj)
+{
+	int rc,nRow,nColumn,i=0;
+	sqlite3* db;
+	char **result=NULL, *errmsg = NULL, cmd[MAX_TXET_LEN]={0};
+	struct json_object *device_obj =NULL, *device_array=NULL;
+	 
+	rc = sqlite3_open(SQL_NAME, &db);	
+	if ( rc != SQLITE_OK )	
+	{		
+		printf("ERROR:open sqlite %s \r\n",sqlite3_errmsg(db));		
+		sqlite3_close(db);		
+		return -1;
+	}
+
+	sprintf(cmd,"select device_id, device_name, device_addr, application_id from device_db;");
+	rc = sqlite3_get_table(db,cmd,&result,&nRow,&nColumn,&errmsg);
+	if (rc != SQLITE_OK)
+	{
+		printf("ERROR: get table %s \r\n", errmsg);
+		sqlite3_close(db);
+		return -1;
+	}
+
+	*rsp_obj = json_object_new_object();
+	if(*rsp_obj == NULL)
+	{
+		sqlite3_free_table(result);
+		sqlite3_close(db);
+		return -1;
+	}
+
+	device_array = json_object_new_array();
+	if(device_array == NULL)
+	{
+		sqlite3_free_table(result);
+		sqlite3_close(db);
+		json_object_put(*rsp_obj);
+		return -1;
+	}
+	
+
+	for(i=1; i<=nRow ;i++)
+	{	
+		device_obj = json_object_new_object();
+		json_object_object_add(device_obj, "device_id", json_object_new_string(result[i*nColumn+0]));
+		json_object_object_add(device_obj, "device_name", json_object_new_string(result[i*nColumn+1]));
+		json_object_object_add(device_obj, "device_addr", json_object_new_string(result[i*nColumn+2]));
+		json_object_object_add(device_obj, "application_id", json_object_new_string(result[i*nColumn+3]));
+		json_object_array_add(device_array, device_obj);
+		
+	}
+
+	json_object_object_add(*rsp_obj, "devices", device_array);
+	json_object_object_add(*rsp_obj, "state", json_object_new_string("success"));
+
+
+	sqlite3_free_table(result);
+	sqlite3_close(db);
+
+	return nRow;
+}
+
+int select_device_id_by_device_addr(uint8_t device_addr,uint8_t *device_id, uint8_t application_id)
+{
+	int rc,nRow,nColumn,i=0;
+	sqlite3* db;
+	char **result=NULL, *errmsg = NULL, cmd[MAX_TXET_LEN]={0};
+	 
+	rc = sqlite3_open(SQL_NAME, &db);	
+	if ( rc != SQLITE_OK )	
+	{		
+		printf("ERROR:open sqlite %s \r\n",sqlite3_errmsg(db));		
+		sqlite3_close(db);		
+		return -1;
+	}
+
+	sprintf(cmd,"select device_id from device_db where device_addr = %d and application_id = %d;", device_addr,application_id);
+	rc = sqlite3_get_table(db,cmd,&result,&nRow,&nColumn,&errmsg);
+	if (rc != SQLITE_OK)
+	{
+		printf("ERROR: get table %s \r\n", errmsg);
+		sqlite3_close(db);
+		return -1;
+	}
+
+
+	if(nRow == 0)
+	{
+		memset(device_id,'\0', 17);
+	}
+	else
+	{
+		memset(device_id,'\0', 17);
+		memcpy(device_id,result[1*nColumn+0],16);
+	}
+
+	sqlite3_free_table(result);
+	sqlite3_close(db);
+
+	return nRow;
+}
+
+//通过设备地址查询设备数据
+int select_attr_by_device_addr(uint16_t *buffer, uint8_t device_addr, char*attr_id, uint8_t application_id)
+{
+	int rc,nRow,nColumn,i=0;
+	sqlite3* db;
+	char **result=NULL, *errmsg = NULL, cmd[MAX_TXET_LEN]={0};
+	uint16_t value =0 ;
+	 
+	rc = sqlite3_open(SQL_NAME, &db);	
+	if ( rc != SQLITE_OK )	
+	{		
+		printf("ERROR:open sqlite %s \r\n",sqlite3_errmsg(db));		
+		sqlite3_close(db);		
+		return -1;
+	}
+
+	sprintf(cmd,"select %s from device_db where device_addr = %d and application_id = %d;", attr_id,device_addr,application_id);
+	rc = sqlite3_get_table(db,cmd,&result,&nRow,&nColumn,&errmsg);
+	if (rc != SQLITE_OK)
+	{
+		*buffer =0;
+		sqlite3_close(db);
+		return -1;
+	}
+
+	if(nRow == 0)
+	{
+	  	*buffer =0;
+	}
+	else
+	{
+		*buffer = atoi(result[1*nColumn+0]);
+	}
+	//sscanf(result[1*nColumn+0],"%04d",&value);
+	//*buffer = value;
+	//strtol(result[1*nColumn+0], buffer, 16);
+
+	sqlite3_free_table(result);
+	sqlite3_close(db);
+
+	return nRow;
+}
+
+#if 0
+//读取数据库
+int get_tab_item(int numofday)
+{
+	int rc,nRow,nColumn,i=0;
+	sqlite3* db;
+	char **result;
+	char *errmsg = NULL;
+	char cmd[512];
+
+	rc = sqlite3_open(SQL_NAME, &db);	
+	if ( rc != SQLITE_OK )	
+	{		
+		printf("ERROR:open sqlite %s \r\n",sqlite3_errmsg(db));		
+		sqlite3_close(db);		
+		return -1;
+	}
+
+	sprintf(cmd,"select * from log_db where date(logtime) between date('now','-%d day','localtime') and date('now','localtime');", numofday);
+
+	rc = sqlite3_get_table(db,cmd,&result,&nRow,&nColumn,&errmsg);
+	if (rc != SQLITE_OK)
+	{
+		printf("ERROR: get table %s \r\n", errmsg);
+		sqlite3_close(db);
+		return -1;
+	}
+
+	for(i=0;i<nRow;i++)
+	{
+		printf("%s %s %s %s %s\r\n",result[i*nColumn],result[i*nColumn+1],result[i*nColumn+2],result[i*nColumn+3],result[i*nColumn+4]);
+		
+	}
+
+	sqlite3_free_table(result);
+	sqlite3_close(db);
+
+	return 0;
+}
+
+#endif
+
+#if 0
+//删除30天前的日志
+int del_30d_item()
+{
+	char cmd[512];
+
+	sprintf(cmd,"%s","delete from log_db where date('now', '-30 day') >= date(logtime);");
+	if(do_cmd_str(cmd)!=0)
+		return -1;
+	return 0;
+}
+
+//用户写日志
+int log_write(char*type,char*level,char *log)
+{
+	insert_tab_item(type,level,log);
+}
+//用户读日志
+void log_read(struct json_object *root)
+{
+	get_tab_item(root);
+}
+
+#endif
+
+#if 0
+int main(void)
+{
+	uint16_t buffer16;
+	uint8_t tmp[5];
+	uint16_t attr_value;
+	uint8_t j=0;
+	uint8_t device_id[17];
+
+	init_device_db();
+	//insert_device_item("0102030405060708","test0",1, "lora");
+	//insert_device_item("0102030405060709","test1",2, "lora");
+	//insert_device_item("010203040506070a","test2",3, "lora");
+	
+	update_tab_item_by_device_id("0102030405060709","0000", "0");
+	update_tab_item_by_device_id("0102030405060709","0001", "1");
+	update_tab_item_by_device_id("0102030405060709","0002", "2");
+	update_tab_item_by_device_id("0102030405060709","0003", "3");
+
+	update_tab_item_by_device_addr(2, "0000", "8");
+	update_tab_item_by_device_addr(2, "0001", "8");
+	update_tab_item_by_device_addr(2, "0002", "8");
+	update_tab_item_by_device_addr(2, "0003", "8");
+
+	update_tab_item_by_device_addr(3, "0000", "8");
+
+	update_tab_item_by_device_addr(4, "0000", "8");
+
+	select_device_id_by_device_addr(4,device_id);
+	printf("addr:%d ---id:0x%s\r\n",4,device_id);
+
+	select_device_id_by_device_addr(2,device_id);
+	printf("addr:%d ---id:0x%s\r\n",2,device_id);
+
+
+	for(j=1; j<4; j++)
+	{ 
+		sprintf(tmp, "%04x",j);	
+		tmp[4]='\0';
+		printf("%s----",tmp);
+		printf("nrow=%d\r\n",select_attr_by_device_addr(&attr_value, 2, tmp));
+		printf("maddr:%d, attrid:%s, buffer16:%d \r\n",2, tmp, attr_value);
+	}
+
+	for(j=1; j<4; j++)
+	{ 
+		sprintf(tmp, "%04x",j);	
+		tmp[4]='\0';
+		printf("%s----",tmp);
+		printf("nrow=%d\r\n",select_attr_by_device_addr(&attr_value, 3, tmp));
+		printf("maddr:%d, attrid:%s, buffer16:%d \r\n",3, tmp, attr_value);
+	}
+	
+}
+#endif
+

+ 26 - 0
apps/sql_api.h

@@ -0,0 +1,26 @@
+#ifndef __SQL_API_H__
+#define __SQL_API_H__
+
+#include "json-c/json.h"
+#include "json-c/json_util.h"
+
+
+#define SQL_NAME	"./device_db.db"
+#define MAX_TXET_LEN	10240
+
+int init_device_db(void);
+int insert_device_item(char*device_id,char*device_name,uint8_t device_addr,char *application_id);
+int delete_device_by_device_id( char*device_id );
+int update_device_by_device_id(char*device_id,char*device_name,uint8_t device_addr,char *application_id);
+int update_tab_item_by_device_id(char*device_id,char *attr_id, char *attr_value);
+//int update_tab_item_by_device_addr(uint8_t device_addr,char *attr_id, char *attr_value);
+int update_tab_item_by_device_addr(uint8_t device_addr,char *attr_id, char *attr_value, uint8_t application_id);
+int select_all_device(struct json_object **rsp_obj);
+//int select_device_id_by_device_addr(uint8_t device_addr,uint8_t *device_id);
+int select_device_id_by_device_addr(uint8_t device_addr,uint8_t *device_id, uint8_t application_id);
+//int select_attr_by_device_addr(uint16_t *buffer, uint8_t device_addr, char*attr_id);
+int select_attr_by_device_addr(uint16_t *buffer, uint8_t device_addr, char*attr_id, uint8_t application_id);
+
+
+
+#endif

+ 1 - 0
dist/index.html

@@ -0,0 +1 @@
+<!DOCTYPE html><html><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>rtuweb</title><link href=/static/css/app.3d8d24b268d0726c1919f7c7f329e2d4.css rel=stylesheet></head><body><div id=app></div><script type=text/javascript src=/static/js/manifest.2ae2e69a05c33dfc65f8.js></script><script type=text/javascript src=/static/js/vendor.3ce127c54cf2044ee291.js></script><script type=text/javascript src=/static/js/app.6f0ac4c14391b3e0c8fe.js></script></body></html>

文件差異過大導致無法顯示
+ 0 - 0
dist/static/css/app.3d8d24b268d0726c1919f7c7f329e2d4.css


文件差異過大導致無法顯示
+ 0 - 0
dist/static/css/app.3d8d24b268d0726c1919f7c7f329e2d4.css.map


二進制
dist/static/fonts/element-icons.535877f.woff


二進制
dist/static/fonts/element-icons.732389d.ttf


二進制
dist/static/img/logo_bg.b20eb92.png


文件差異過大導致無法顯示
+ 0 - 0
dist/static/js/app.6f0ac4c14391b3e0c8fe.js


文件差異過大導致無法顯示
+ 0 - 0
dist/static/js/app.6f0ac4c14391b3e0c8fe.js.map


+ 2 - 0
dist/static/js/manifest.2ae2e69a05c33dfc65f8.js

@@ -0,0 +1,2 @@
+!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a<e.length;a++)i=e[a],o[i]&&l.push(o[i][0]),o[i]=0;for(f in u)Object.prototype.hasOwnProperty.call(u,f)&&(r[f]=u[f]);for(n&&n(e,u,c);l.length;)l.shift()();if(c)for(a=0;a<c.length;a++)p=t(t.s=c[a]);return p};var e={},o={2:0};function t(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return r[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}t.m=r,t.c=e,t.d=function(r,n,e){t.o(r,n)||Object.defineProperty(r,n,{configurable:!1,enumerable:!0,get:e})},t.n=function(r){var n=r&&r.__esModule?function(){return r.default}:function(){return r};return t.d(n,"a",n),n},t.o=function(r,n){return Object.prototype.hasOwnProperty.call(r,n)},t.p="/",t.oe=function(r){throw console.error(r),r}}([]);
+//# sourceMappingURL=manifest.2ae2e69a05c33dfc65f8.js.map

文件差異過大導致無法顯示
+ 0 - 0
dist/static/js/manifest.2ae2e69a05c33dfc65f8.js.map


文件差異過大導致無法顯示
+ 14 - 0
dist/static/js/vendor.3ce127c54cf2044ee291.js


文件差異過大導致無法顯示
+ 0 - 0
dist/static/js/vendor.3ce127c54cf2044ee291.js.map


+ 41 - 0
http/CMakeLists.txt

@@ -0,0 +1,41 @@
+# 子模块参考模版
+include(${ROOT_DIR}/buildtools/cmake/toolchain_common.cmake)
+
+# 模块名称
+set(MODULE httpConfig)
+
+aux_source_directory(. DIRSRCS)
+include_directories(./)
+
+build_subdirectory(thirdparty/mosquitto)
+build_subdirectory(thirdparty/SQLiteCpp include thirdparty/SQLiteCpp/sqlite3)
+build_subdirectory(thirdparty/json-c define STDC_HEADERS)
+
+# link_libraries(${MODULE} mosquitto sqlite3 json-c rt pthread m)
+
+function(gen_cgi src)
+  set(cgi_name "${src}.cgi")
+  add_library(${cgi_name} ${src}.c common.c command.c)
+  target_link_libraries(${cgi_name} sqlite3 json-c rt pthread m)
+  install(TARGETS ${cgi_name}
+          DESTINATION webconfig/cgi-bin
+          PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ)
+endfunction(gen_cgi src)
+
+
+gen_cgi("get_device")
+gen_cgi("set_device")
+gen_cgi("add_device")
+gen_cgi("del_device")
+gen_cgi("get_system")
+gen_cgi("get_user")
+gen_cgi("get_platform")
+gen_cgi("set_platform")
+gen_cgi("login")
+gen_cgi("logout")
+
+
+install(DIRECTORY cookie
+        DESTINATION webconfig/cgi-bin
+        FILES_MATCHING PATTERN "*"
+)

+ 86 - 0
http/Makefile

@@ -0,0 +1,86 @@
+### Environment constants 
+ARCH := arm
+#CROSS_COMPILE := ../../buildroot-at91-2018.02-at91/output/host/bin/arm-linux-
+#CROSS_COMPILE := /home/logan/EmbeddedProject/RTU/buildroot/buildroot-at91-2018.02-at91/output/host/bin/arm-linux-
+
+OBJDIR = obj
+INCLUDES = $(wildcard inc/*.h)
+
+### External constant definitions
+# must get library build option to know if mpsse must be linked or not
+
+### Constant symbols
+
+#CC := $(CROSS_COMPILE)gcc
+#AR := $(CROSS_COMPILE)ar
+CC := $(CROSS_COMPILE)gcc
+AR := $(CROSS_COMPILE)ar
+RANLIB := $(CROSS_COMPILE)ranlib
+
+CFLAGS := -O2 -Wall -Wextra -std=c99 -Iinc -I.
+# List the library sub-modules that are used by the application
+
+### Linking options
+
+LIBS := -lmosquitto  -lsqlite3 -ljson-c -lrt -lpthread -lm
+### General build targets
+
+all: get_device.cgi set_device.cgi add_device.cgi del_device.cgi get_system.cgi \
+	get_user.cgi get_platform.cgi set_platform.cgi login.cgi logout.cgi
+
+clean:
+	rm -f *.o
+	rm -f get_device.cgi set_device.cgi add_device.cgi del_device.cgi get_system.cgi \
+	get_user.cgi get_platform.cgi set_platform.cgi login.cgi logout.cgi
+		
+
+### Sub-modules compilation
+
+libcgic.a: cgic.o cgic.h
+	rm -f libcgic.a
+	$(AR) rc libcgic.a cgic.o
+	$(RANLIB) libcgic.a
+
+
+%.o: %.c
+	$(CC) $(CFLAGS) -c  $< -o $@
+
+get_device.cgi:get_device.o common.o command.o
+	$(CC)   $^ -o $@ $(LIBS)
+
+set_device.cgi:set_device.o common.o command.o
+	$(CC)   $^ -o $@ $(LIBS)
+
+add_device.cgi:add_device.o common.o command.o
+	$(CC)   $^ -o $@ $(LIBS)
+
+del_device.cgi:del_device.o common.o command.o
+	$(CC)   $^ -o $@ $(LIBS)
+
+get_system.cgi:get_system.o common.o command.o
+	$(CC)   $^ -o $@ $(LIBS)
+
+get_user.cgi:get_user.o common.o command.o
+	$(CC)   $^ -o $@ $(LIBS)
+
+get_platform.cgi:get_platform.o common.o command.o
+	$(CC)   $^ -o $@ $(LIBS)
+
+set_platform.cgi:set_platform.o common.o command.o
+	$(CC)    $^ -o $@ $(LIBS)
+
+login.cgi:login.o common.o command.o
+	$(CC)    $^ -o $@ $(LIBS)
+
+logout.cgi:logout.o common.o command.o
+	$(CC)    $^ -o $@ $(LIBS)
+
+
+install:
+	echo "install"
+	cp -rf ../dist/index.html /var/www/
+	cp -rf ../dist/static /var/www/
+	cp -rf cookie /var/www/cgi-bin/
+	cp *.cgi /var/www/cgi-bin/
+### EOF
+

+ 11 - 0
http/add_device.c

@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include "common.h"
+#include "command.h"
+
+int main(int argc, char *argv[])
+{
+
+	add_configure_by_type(PUB_ADD_DEVICES_NONE_HTTP, SUB_ADD_DEVICES_NONE_HTTP);
+
+	return 0;
+}

+ 346 - 0
http/command.c

@@ -0,0 +1,346 @@
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <stdbool.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <json-c/json.h>
+#include <json-c/json_util.h>
+#include <json-c/json_object.h>
+#include <mosquitto.h>
+#include "common.h"
+#include "command.h"
+
+static volatile bool run =true;
+
+bool auth_user()
+{
+#if ENABLE_HTTP_COOKIE
+	char*info=NULL;
+	struct json_object *proot=NULL, *cookied_obj =NULL, *resp_obj = NULL;
+	const char *cookied=NULL;
+	const char *resp_string=NULL;
+	char id[100];
+
+	info = getenv("HTTP_COOKIE");
+	if(info == NULL)
+	{
+		printf("%s\n\n","Content-Type:application/json;charset=UTF-8");
+		resp_obj = json_object_new_object();
+		json_object_object_add(resp_obj, "status", json_object_new_string("expired"));
+		resp_string = json_object_to_json_string(resp_obj);
+		printf("%s",resp_string);
+		json_object_put(resp_obj);
+		return false;
+	}
+	else
+	{
+		sscanf(info,"cookied=%s",id);
+		proot = json_object_from_file("./cookie/cookied.json");
+		if(proot == NULL)
+		{
+			printf("%s\n\n","Content-Type:application/json;charset=UTF-8"); 
+			resp_obj = json_object_new_object();
+			json_object_object_add(resp_obj, "status", json_object_new_string("expired"));
+			resp_string = json_object_to_json_string(resp_obj);
+			printf("%s",resp_string);
+			json_object_put(resp_obj);
+			return false;
+		}
+		json_object_object_get_ex(proot, "cookied",&cookied_obj);
+		cookied = json_object_get_string(cookied_obj);
+		if(strncmp(cookied, id, STR_MAX_LEN(cookied, id)) != 0)
+		{
+			json_object_put(proot);
+			printf("%s\n\n","Content-Type:application/json;charset=UTF-8"); 
+			resp_obj = json_object_new_object();
+			json_object_object_add(resp_obj, "status", json_object_new_string("expired"));
+			resp_string = json_object_to_json_string(resp_obj);
+			printf("%s",resp_string);
+			json_object_put(resp_obj);
+			return false;
+		}
+		json_object_put(proot);
+	}
+
+#endif
+	return true;
+
+}
+
+void http_connect_callback(struct mosquitto *mosq, void *obj, int rc)
+{
+	run = true;
+}
+
+void http_disconnect_callback(struct mosquitto *mosq, void *obj, int result)
+{
+	run = false;
+}
+
+void http_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
+{
+
+	printf("%s\n\n","Content-Type:application/json;charset=UTF-8"); 
+
+	
+	printf("%s",msg->payload);
+	
+	run =false;
+
+}
+
+void http_subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos)
+{
+	run = true;
+}
+
+
+void get_configure_by_type(char *pub_topic, char *sub_topic)
+{
+	struct mosquitto *http_mosq;
+	char*info=NULL;
+	int max=0;
+	char id[50];
+
+#if ENABLE_HTTP_COOKIE
+	if (auth_user() ==false)
+		return;
+#endif
+
+	mosquitto_lib_init();
+	snprintf(id, 50, "get_configure_%d", getpid());
+	http_mosq = mosquitto_new(id, true, NULL);
+
+	mosquitto_connect_callback_set(http_mosq, http_connect_callback);
+	mosquitto_disconnect_callback_set(http_mosq, http_disconnect_callback);
+	mosquitto_message_callback_set(http_mosq, http_message_callback);
+	mosquitto_subscribe_callback_set(http_mosq, http_subscribe_callback);
+	mosquitto_connect(http_mosq, "localhost", 1883, 600);
+	mosquitto_subscribe(http_mosq, NULL, sub_topic, 1);
+	mosquitto_publish(http_mosq, NULL, pub_topic, 0, "", 1, false);
+	while(run ==true && max < MAX_TIMEOUT)
+	{
+		mosquitto_loop(http_mosq, 500, 1);
+		max++;
+
+	}
+
+	mosquitto_disconnect(http_mosq);
+	mosquitto_destroy(http_mosq);
+	
+	mosquitto_lib_cleanup();
+
+}
+
+
+void set_configure_by_type(char *pub_topic ,char *sub_topic)
+{
+	char *info=NULL, *p=NULL, *pRequestMethod=NULL, *inputbuffer=NULL;
+	int ContentLength,x,i=0;
+	struct mosquitto *http_mosq;
+	int max=0;
+	char id[50];
+
+#if ENABLE_HTTP_COOKIE
+	if (auth_user() ==false)
+		return;
+#endif
+
+	setvbuf(stdin, NULL , _IONBF, 0);
+	pRequestMethod = getenv("REQUEST_METHOD");
+	if(pRequestMethod == NULL)
+	{
+		printf("<p> request = null </p>");
+		return;
+	}
+	
+	if(strcasecmp(pRequestMethod, "POST") == 0)
+	{
+		p = getenv("CONTENT_LENGTH");
+		if(p!=NULL)
+		{
+			ContentLength =atoi(p);
+		}
+		else
+		{
+			ContentLength = 0;
+		}
+		
+		inputbuffer = (char *)malloc(ContentLength + 1);
+		while(i < ContentLength)
+		{
+			x = fgetc(stdin);
+			if(x == EOF)
+				break;
+			*(inputbuffer+i) =x;
+			i++;
+		}
+		*(inputbuffer+i) = '\0';
+		ContentLength = i;
+
+		mosquitto_lib_init();
+		snprintf(id, 50, "set_configure_%d", getpid());
+		http_mosq = mosquitto_new(id, true, NULL);
+		mosquitto_connect_callback_set(http_mosq, http_connect_callback);
+		mosquitto_disconnect_callback_set(http_mosq, http_disconnect_callback);
+		mosquitto_message_callback_set(http_mosq, http_message_callback);
+		mosquitto_subscribe_callback_set(http_mosq, http_subscribe_callback);
+		mosquitto_connect(http_mosq, "localhost", 1883, 600);
+		mosquitto_subscribe(http_mosq, NULL, sub_topic, 1);
+		mosquitto_publish(http_mosq, NULL, pub_topic, ContentLength, inputbuffer, 1, false);
+		while(run ==true && max < MAX_TIMEOUT)
+		{
+			mosquitto_loop(http_mosq, 500, 1);
+			max++;
+		}
+
+		mosquitto_disconnect(http_mosq);
+		mosquitto_destroy(http_mosq);
+	
+		mosquitto_lib_cleanup();
+		free(inputbuffer);
+	}
+
+}
+
+void del_configure_by_type(char *pub_topic,char *sub_topic)
+{
+	char *info=NULL, *p=NULL, *pRequestMethod=NULL, *inputbuffer=NULL;
+	int ContentLength,x,i=0;
+	struct mosquitto *http_mosq;
+	int max=0;
+	char id[50];
+
+#if ENABLE_HTTP_COOKIE
+	if (auth_user() ==false)
+		return;
+#endif
+
+	setvbuf(stdin, NULL , _IONBF, 0);
+	pRequestMethod = getenv("REQUEST_METHOD");
+	if(pRequestMethod == NULL)
+	{
+		printf("<p> request = null </p>");
+		return;
+	}
+	
+	if(strcasecmp(pRequestMethod, "POST") == 0)
+	{
+		p = getenv("CONTENT_LENGTH");
+		if(p!=NULL)
+		{
+			ContentLength =atoi(p);
+		}
+		else
+		{
+			ContentLength = 0;
+		}
+		
+		inputbuffer = (char *)malloc(ContentLength + 1);
+		while(i < ContentLength)
+		{
+			x = fgetc(stdin);
+			if(x == EOF)
+				break;
+			*(inputbuffer+i) =x;
+			i++;
+		}
+		*(inputbuffer+i) = '\0';
+		ContentLength = i;
+
+		mosquitto_lib_init();
+		snprintf(id, 50, "del_configure_%d", getpid());
+		http_mosq = mosquitto_new(id, true, NULL);
+		mosquitto_connect_callback_set(http_mosq, http_connect_callback);
+		mosquitto_disconnect_callback_set(http_mosq, http_disconnect_callback);
+		mosquitto_message_callback_set(http_mosq, http_message_callback);
+		mosquitto_subscribe_callback_set(http_mosq, http_subscribe_callback);
+		mosquitto_connect(http_mosq, "localhost", 1883, 600);
+		mosquitto_subscribe(http_mosq, NULL, sub_topic, 1);
+		mosquitto_publish(http_mosq, NULL, pub_topic, ContentLength, inputbuffer, 1, false);
+		while(run ==true && max < MAX_TIMEOUT)
+		{
+			mosquitto_loop(http_mosq, 500, 1);
+			max++;
+		}
+
+		mosquitto_disconnect(http_mosq);
+		mosquitto_destroy(http_mosq);
+	
+		mosquitto_lib_cleanup();
+		free(inputbuffer);
+	}
+}
+
+void add_configure_by_type(char *pub_topic , char *sub_topic)
+{
+	char *info=NULL, *p=NULL, *pRequestMethod=NULL, *inputbuffer=NULL;
+	int ContentLength,x,i=0;
+	struct mosquitto *http_mosq;
+	int max =0;
+	char id[50];
+
+#if ENABLE_HTTP_COOKIE
+	if (auth_user() ==false)
+		return;
+#endif
+
+	setvbuf(stdin, NULL , _IONBF, 0);
+	pRequestMethod = getenv("REQUEST_METHOD");
+	if(pRequestMethod == NULL)
+	{
+		printf("<p> request = null </p>");
+		return;
+	}
+	
+	if(strcasecmp(pRequestMethod, "POST") == 0)
+	{
+		p = getenv("CONTENT_LENGTH");
+		if(p!=NULL)
+		{
+			ContentLength =atoi(p);
+		}
+		else
+		{
+			ContentLength = 0;
+		}
+		
+		inputbuffer = (char *)malloc(ContentLength + 1);
+		while(i < ContentLength)
+		{
+			x = fgetc(stdin);
+			if(x == EOF)
+				break;
+			*(inputbuffer+i) =x;
+			i++;
+		}
+		*(inputbuffer+i) = '\0';
+		ContentLength = i;
+
+		mosquitto_lib_init();
+		snprintf(id, 50, "add_configure_%d", getpid());
+		http_mosq = mosquitto_new(id, true, NULL);
+		mosquitto_connect_callback_set(http_mosq, http_connect_callback);
+		mosquitto_disconnect_callback_set(http_mosq, http_disconnect_callback);
+		mosquitto_message_callback_set(http_mosq, http_message_callback);
+		mosquitto_subscribe_callback_set(http_mosq, http_subscribe_callback);
+		mosquitto_connect(http_mosq, "localhost", 1883, 600);
+		mosquitto_subscribe(http_mosq, NULL, sub_topic, 1);
+		mosquitto_publish(http_mosq, NULL, pub_topic, ContentLength, inputbuffer, 1, false);
+		while(run ==true && max < MAX_TIMEOUT)
+		{
+			mosquitto_loop(http_mosq, 500, 1);
+			max++;
+		}
+
+		mosquitto_disconnect(http_mosq);
+		mosquitto_destroy(http_mosq);
+	
+		mosquitto_lib_cleanup();
+		free(inputbuffer);
+	}
+}
+
+
+

+ 73 - 0
http/command.h

@@ -0,0 +1,73 @@
+#ifndef __COMMAND_H__
+#define __COMMAND_H__
+#include<stdio.h>
+#include <time.h>
+#include <stdbool.h>
+
+#define ENABLE_HTTP_COOKIE 1
+
+#define MAX_TIMEOUT 20
+
+//devices.db http 
+#define PUB_GET_DEVICES_NONE_HTTP		"get/devices/none/http"
+#define SUB_GET_DEVICES_NONE_HTTP		"ack/get/devices/none/http"
+
+#define PUB_ADD_DEVICES_NONE_HTTP		"add/devices/none/http"
+#define SUB_ADD_DEVICES_NONE_HTTP		"ack/add/devices/none/http"
+
+#define PUB_SET_DEVICES_NONE_HTTP		"set/devices/none/http"
+#define SUB_SET_DEVICES_NONE_HTTP		"ack/set/devices/none/http"
+
+#define PUB_DEL_DEVICES_NONE_HTTP		"del/devices/none/http"
+#define SUB_DEL_DEVICES_NONE_HTTP		"ack/del/devices/none/http"
+
+//platform.json
+#define PUB_GET_PLATFORM_NONE_HTTP		"get/platform/none/http"
+#define SUB_GET_PLATFORM_NONE_HTTP		"ack/get/platform/none/http"
+
+#define PUB_ADD_PLATFORM_NONE_HTTP		"add/platform/none/http"
+#define SUB_ADD_PLATFORM_NONE_HTTP		"ack/add/platform/none/http"
+
+#define PUB_SET_PLATFORM_NONE_HTTP		"set/platform/none/http"
+#define SUB_SET_PLATFORM_NONE_HTTP		"ack/set/platform/none/http"
+
+#define PUB_DEL_PLATFORM_NONE_HTTP		"del/platform/none/http"
+#define SUB_DEL_PLATFORM_NONE_HTTP		"ack/del/platform/none/http"
+
+//system.json
+#define PUB_GET_SYSTEM_NONE_HTTP		"get/system/none/http"
+#define SUB_GET_SYSTEM_NONE_HTTP		"ack/get/system/none/http"
+
+#define PUB_ADD_SYSTEM_NONE_HTTP		"add/system/none/http"
+#define SUB_ADD_SYSTEM_NONE_HTTP		"ack/add/system/none/http
+
+#define PUB_SET_SYSTEM_NONE_HTTP		"set/system/none/http"
+#define SUB_SET_SYSTEM_NONE_HTTP		"ack/set/system/none/http"
+
+#define PUB_DEL_SYSTEM_NONE_HTTP		"del/system/none/http"
+#define SUB_DEL_SYSTEM_NONE_HTTP		"ack/del/system/none/http"
+
+//user.json
+#define PUB_GET_USER_NONE_HTTP			"get/user/none/http"
+#define SUB_GET_USER_NONE_HTTP			"ack/get/user/none/http"
+
+#define PUB_ADD_SYSTEM_NONE_HTTP		"add/user/none/http"
+#define SUB_ADD_SYSTEM_NONE_HTTP		"ack/add/user/none/http"
+
+#define PUB_SET_SYSTEM_NONE_HTTP		"set/user/none/http"
+#define SUB_SET_SYSTEM_NONE_HTTP		"ack/set/user/none/http"
+
+#define PUB_DEL_SYSTEM_NONE_HTTP		"del/user/none/http"
+#define SUB_DEL_SYSTEM_NONE_HTTP		"ack/del/user/none/http"
+
+
+
+
+bool auth_user();
+
+void get_configure_by_type(char *pub_topic, char *sub_topic);
+void set_configure_by_type(char *pub_topic, char *sub_topic);
+void del_configure_by_type(char *pub_topic, char *sub_topic);
+void add_configure_by_type(char *pub_topic, char *sub_topic);
+
+#endif

+ 14 - 0
http/common.c

@@ -0,0 +1,14 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <time.h>
+#include "common.h"
+
+void timestamp_get(char *timestamp)
+{	
+	time_t t;
+
+	t = time(NULL);  
+	sprintf(timestamp,"%lld",((long long)t)*1000);
+}

+ 13 - 0
http/common.h

@@ -0,0 +1,13 @@
+#ifndef __COMMON_H__
+#define __COMMON_H__
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <string.h>
+
+#define STR_MAX_LEN(str1,str2)	((strlen(str1)>strlen(str2))?strlen(str1):strlen(str2))
+
+void timestamp_get(char *timestamp);
+
+#endif

+ 1 - 0
http/cookie/cookied.json

@@ -0,0 +1 @@
+{"cookied":""}

+ 11 - 0
http/del_device.c

@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include "common.h"
+#include "command.h"
+
+int main(int argc, char *argv[])
+{
+
+	del_configure_by_type(PUB_DEL_DEVICES_NONE_HTTP, SUB_DEL_DEVICES_NONE_HTTP);
+
+	return 0;
+}

+ 11 - 0
http/get_device.c

@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include "common.h"
+#include "command.h"
+
+int main(int argc, char *argv[])
+{
+
+	get_configure_by_type(PUB_GET_DEVICES_NONE_HTTP, SUB_GET_DEVICES_NONE_HTTP);
+
+	return 0;
+}

+ 10 - 0
http/get_platform.c

@@ -0,0 +1,10 @@
+#include <stdio.h>
+#include "common.h"
+#include "command.h"
+
+int main(int argc, char *argv[])
+{
+
+	get_configure_by_type(PUB_GET_PLATFORM_NONE_HTTP,SUB_GET_PLATFORM_NONE_HTTP);
+	return 0;
+}

+ 9 - 0
http/get_system.c

@@ -0,0 +1,9 @@
+#include <stdio.h>
+#include "common.h"
+#include "command.h"
+
+int main(int argc, char *argv[])
+{
+	get_configure_by_type(PUB_GET_SYSTEM_NONE_HTTP,SUB_GET_SYSTEM_NONE_HTTP);
+	return 0;
+}

+ 10 - 0
http/get_user.c

@@ -0,0 +1,10 @@
+#include <stdio.h>
+#include "common.h"
+#include "command.h"
+
+int main(int argc, char *argv[])
+{
+
+	get_configure_by_type(PUB_GET_USER_NONE_HTTP,SUB_GET_USER_NONE_HTTP);
+	return 0;
+}

+ 170 - 0
http/login.c

@@ -0,0 +1,170 @@
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <json-c/json.h>
+#include <json-c/json_util.h>
+#include <json-c/json_object.h>
+#include <mosquitto.h>
+#include "common.h"
+#include "command.h"
+
+
+static volatile bool run =true;
+char *inputbuffer=NULL;
+
+void connect_callback(struct mosquitto *mosq, void *obj, int rc)
+{
+	run = true;
+}
+
+void disconnect_callback(struct mosquitto *mosq, void *obj, int result)
+{
+	run = false;
+}
+
+void message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
+{
+	struct json_object *proot=NULL, *payload_obj=NULL, *item_obj=NULL, *user_obj=NULL, *temp_obj=NULL, *cookied_obj=NULL, *rsp_obj=NULL;
+	const char *proot_string=NULL, *rsp_string=NULL;
+	const char *user=NULL;
+	const char *passwd=NULL;
+	const char *load_user=NULL;
+	const char *load_passwd=NULL;
+	char timestamp[100];
+
+	proot = json_tokener_parse(msg->payload);
+	if(proot == NULL)
+	{
+		printf("ERROR: login proot is NULL!\r\n");
+		run =false;
+		return ;
+	}
+	json_object_object_get_ex(proot, "user", &user_obj);
+	json_object_object_get_ex(user_obj, "user", &temp_obj);
+	user = json_object_get_string(temp_obj);
+	json_object_object_get_ex(user_obj, "passwd", &temp_obj);
+	passwd = json_object_get_string(temp_obj);
+
+
+	payload_obj = json_tokener_parse(inputbuffer);
+	if(payload_obj == NULL)
+	{
+		printf("ERROR: inputbuffer is error!\r\n");
+		json_object_put(proot);
+		run =false;
+		return;
+	}
+	json_object_object_get_ex(payload_obj, "user", &temp_obj);
+	load_user = json_object_get_string(temp_obj);
+	json_object_object_get_ex(payload_obj, "passwd", &temp_obj);
+	load_passwd = json_object_get_string(temp_obj);
+	if(strncmp(user,load_user, STR_MAX_LEN(user, load_user)) ==0 &&
+	   strncmp(passwd, load_passwd,STR_MAX_LEN(passwd,load_passwd)) ==0)
+	{
+		rsp_obj = json_object_new_object();
+		json_object_object_add(rsp_obj,"state", json_object_new_string("success"));
+		json_object_object_add(rsp_obj,"user", json_object_new_string(user));
+		json_object_object_add(rsp_obj,"passwd", json_object_new_string(passwd));
+
+		timestamp_get(timestamp);
+		cookied_obj = json_object_new_object();
+		json_object_object_add(cookied_obj, "cookied", json_object_new_string(timestamp));
+		json_object_to_file_ext("./cookie/cookied.json", cookied_obj, JSON_C_TO_STRING_PRETTY);
+		printf("Set-Cookie:cookied=%s;path=/;",timestamp);
+		json_object_put(cookied_obj);
+	}
+	else{
+		rsp_obj = json_object_new_object();
+		json_object_object_add(rsp_obj,"state", json_object_new_string("fail"));
+		json_object_object_add(rsp_obj,"user", json_object_new_string(load_user));
+		json_object_object_add(rsp_obj,"passwd", json_object_new_string(load_passwd));
+
+	}
+
+	printf("%s\n\n","Content-Type:application/json;charset=UTF-8");
+
+	rsp_string = json_object_to_json_string(rsp_obj);
+	printf("%s",rsp_string);
+	json_object_put(proot);
+	json_object_put(payload_obj);
+	json_object_put(rsp_obj);
+
+	run =false;
+
+}
+
+void subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos)
+{
+	run = true;
+}
+
+
+int main(int argc, char *argv[])
+{
+	char *info=NULL, *p=NULL, *pRequestMethod=NULL;
+	int ContentLength,x,i=0;
+	struct mosquitto *http_mosq=NULL;
+	int max =0;
+	char id[50];
+
+    setvbuf(stdin, NULL , _IONBF, 0);
+	pRequestMethod = getenv("REQUEST_METHOD");
+	if(pRequestMethod == NULL)
+	{
+		printf("<p> request = null </p>");
+		return 0;
+	}
+
+	if(strcasecmp(pRequestMethod, "POST") == 0)
+	{
+		p = getenv("CONTENT_LENGTH");
+		if(p!=NULL)
+		{
+			ContentLength =atoi(p);
+		}
+		else
+		{
+			ContentLength = 0;
+		}
+
+		inputbuffer = (char *)malloc(ContentLength + 1);
+		while(i < ContentLength)
+		{
+			x = fgetc(stdin);
+			if(x == EOF)
+				break;
+			*(inputbuffer+i) =x;
+			i++;
+		}
+		*(inputbuffer+i) = '\0';
+		ContentLength = i;
+
+
+		mosquitto_lib_init();
+		snprintf(id, 50, "login_%d", getpid());
+		http_mosq = mosquitto_new(id, true, NULL);
+		mosquitto_connect_callback_set(http_mosq, connect_callback);
+		mosquitto_disconnect_callback_set(http_mosq, disconnect_callback);
+		mosquitto_message_callback_set(http_mosq, message_callback);
+		mosquitto_subscribe_callback_set(http_mosq, subscribe_callback);
+		mosquitto_connect(http_mosq, "localhost", 1883, 600);
+		mosquitto_subscribe(http_mosq, NULL, SUB_GET_USER_NONE_HTTP, 1);
+		mosquitto_publish(http_mosq, NULL, PUB_GET_USER_NONE_HTTP, 0, "", 1, false);
+		while(run ==true && max < MAX_TIMEOUT)
+		{
+			mosquitto_loop(http_mosq, 500, 1);
+			max++;
+
+		}
+
+		mosquitto_disconnect(http_mosq);
+		mosquitto_destroy(http_mosq);
+		mosquitto_lib_cleanup();
+
+		free(inputbuffer);
+	}
+
+	return 0;
+
+}

+ 38 - 0
http/logout.c

@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include <string.h>
+#include <json-c/json.h>
+#include <json-c/json_util.h>
+#include <json-c/json_object.h>
+#include <mosquitto.h>
+#include "common.h"
+#include "command.h"
+
+int main(int argc, char *argv[])
+{
+	struct json_object *proot=NULL, *resp_obj = NULL;
+	const char *resp_string=NULL;
+	
+#if ENABLE_HTTP_COOKIE
+	if (auth_user() ==false)
+		return 0;
+#endif
+
+	proot = json_object_from_file("./cookie/cookied.json");
+	if(proot == NULL)
+	{
+		printf("Location:/index.html\n\n");
+	}
+	json_object_object_add(proot,"cookied", json_object_new_string(""));
+	json_object_to_file_ext("./cookie/cookied.json", proot,JSON_C_TO_STRING_PRETTY);
+	json_object_put(proot);
+
+
+	printf("%s\n\n","Content-Type:application/json;charset=UTF-8"); 
+	resp_obj = json_object_new_object();
+	json_object_object_add(resp_obj, "state", json_object_new_string("success"));
+	resp_string = json_object_to_json_string(resp_obj);
+	printf("%s",resp_string);
+	json_object_put(resp_obj);
+	
+	return 0;
+}

+ 11 - 0
http/set_device.c

@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include "common.h"
+#include "command.h"
+
+int main(int argc, char *argv[])
+{
+
+	set_configure_by_type(PUB_SET_DEVICES_NONE_HTTP, SUB_SET_DEVICES_NONE_HTTP);
+
+	return 0;
+}

+ 11 - 0
http/set_platform.c

@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include "common.h"
+#include "command.h"
+
+int main(int argc, char *argv[])
+{
+
+	set_configure_by_type(PUB_SET_PLATFORM_NONE_HTTP,SUB_SET_PLATFORM_NONE_HTTP);
+
+	return 0;
+}

部分文件因文件數量過多而無法顯示