├── .gitignore ├── .gitmodules ├── LICENSE ├── Makefile ├── README.md ├── msvs ├── .gitignore ├── whsnbg.sln ├── whsnbg.vcxproj └── whsnbg.vcxproj.filters ├── res ├── whsnbg.conf └── whsnbg.pem ├── src ├── cJSON.c ├── cJSON.h ├── config.h ├── cron_trigger.c ├── cron_trigger.h ├── list.c ├── list.h ├── list_mqtt_conn.c ├── list_mqtt_conn.h ├── list_mqtt_user.c ├── list_mqtt_user.h ├── list_mqttsn_conn.c ├── list_mqttsn_conn.h ├── list_tcp_conn.c ├── list_tcp_conn.h ├── lists.c ├── lists.h ├── main.c ├── mqtt.c ├── mqtt.h ├── mqtt_trigger.c ├── mqtt_trigger.h ├── mqttsn.c ├── mqttsn.h ├── msg_mqtt_mysql.c ├── msg_mqtt_mysql.h ├── msg_mqtt_tcp.c ├── msg_mqtt_tcp.h ├── msg_mqtt_udp.c ├── msg_mqtt_udp.h ├── msg_rules_mqtt.c ├── msg_rules_mqtt.h ├── msg_tcp_mqtt.c ├── msg_tcp_mqtt.h ├── msg_trigger_rules.c ├── msg_trigger_rules.h ├── msg_udp_mqtt.c ├── msg_udp_mqtt.h ├── msgs.c ├── msgs.h ├── os_port.h ├── parse_conf.c ├── parse_conf.h ├── parse_json.c ├── parse_json.h ├── rules.c ├── rules.h ├── sensor_data.c ├── sensor_data.h ├── stdint_msc.h ├── thread_cron.c ├── thread_cron.h ├── thread_mqtt.c ├── thread_mqtt.h ├── thread_mysql.c ├── thread_mysql.h ├── thread_rules.c ├── thread_rules.h ├── thread_state.h ├── thread_tcp.c ├── thread_tcp.h ├── thread_udp.c ├── thread_udp.h ├── tls.c ├── tls.h ├── utf8.c └── utf8.h └── utils ├── README.md ├── sensim ├── .gitignore ├── Makefile ├── msvs │ ├── .gitignore │ ├── LICENSE │ ├── sensim.sln │ ├── sensim.vcxproj │ └── sensim.vcxproj.filters └── src │ ├── config.h │ ├── getopt.h │ ├── list.c │ ├── list.h │ ├── list_mqtt_user.c │ ├── list_mqtt_user.h │ ├── lists.c │ ├── lists.h │ ├── main.c │ ├── mqtt.c │ ├── mqtt.h │ ├── mqttsn.c │ ├── mqttsn.h │ ├── msg_mqtt_tcp.c │ ├── msg_mqtt_tcp.h │ ├── msg_mqtt_udp.c │ ├── msg_mqtt_udp.h │ ├── msg_tcp_mqtt.c │ ├── msg_tcp_mqtt.h │ ├── msg_udp_mqtt.c │ ├── msg_udp_mqtt.h │ ├── msgs.c │ ├── msgs.h │ ├── os_port.h │ ├── sensor_data.c │ ├── sensor_data.h │ ├── stdint_msc.h │ ├── test.h │ ├── test_mqtt.c │ ├── test_mqtt.h │ ├── test_mqtt_list1.c │ ├── test_mqtt_list1.h │ ├── test_mqtt_list2.c │ ├── test_mqtt_list2.h │ ├── test_mqtt_list3.c │ ├── test_mqtt_list3.h │ ├── test_mqttsn.c │ ├── test_mqttsn.h │ ├── test_mqttsn_list1.c │ ├── test_mqttsn_list1.h │ ├── test_mqttsn_list2.c │ ├── test_mqttsn_list2.h │ ├── test_mqttsn_list3.c │ ├── test_mqttsn_list3.h │ ├── thread_state.h │ ├── thread_tcp.c │ ├── thread_tcp.h │ ├── thread_test_mqtt.c │ ├── thread_test_mqtt.h │ ├── thread_test_mqttsn.c │ ├── thread_test_mqttsn.h │ ├── thread_tick.c │ ├── thread_tick.h │ ├── thread_udp.c │ ├── thread_udp.h │ ├── tick_counter.c │ ├── tick_counter.h │ ├── utf8.c │ └── utf8.h └── sensimgui ├── .gitignore ├── LICENSE ├── sensimgui.sln ├── sensimgui.vcxproj ├── sensimgui.vcxproj.filters └── src ├── MainDlg.h ├── config.h ├── list.c ├── list.h ├── lists.c ├── lists.h ├── main.aps ├── main.cpp ├── main.rc ├── mqttsn.c ├── mqttsn.h ├── msg_mqtt_udp.c ├── msg_mqtt_udp.h ├── msg_udp_mqtt.c ├── msg_udp_mqtt.h ├── msgs.c ├── msgs.h ├── os_port.h ├── res └── main.ico ├── resource.h ├── sensor_data.c ├── sensor_data.h ├── stdafx.cpp ├── stdafx.h ├── stdint_msc.h ├── thread_udp.cpp ├── thread_udp.h └── wtl ├── atlapp.h ├── atlcrack.h ├── atlctrls.h ├── atlctrlw.h ├── atlctrlx.h ├── atlddx.h ├── atldlgs.h ├── atlfind.h ├── atlframe.h ├── atlgdi.h ├── atlmisc.h ├── atlprint.h ├── atlres.h ├── atlresce.h ├── atlscrl.h ├── atlsplit.h ├── atltheme.h ├── atluser.h ├── atlwince.h └── atlwinx.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Update 2 | update.txt 3 | .vscode 4 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "mysql.settings"] 2 | path = mysql.settings 3 | url = git://github.com/homewsn/homewsn.mysql.settings 4 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2013-2015 Vladimir Alemasov 3 | # All rights reserved 4 | # 5 | # This program and the accompanying materials are distributed under 6 | # the terms of GNU General Public License version 2 7 | # as published by the Free Software Foundation. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | 15 | TARGET = whsnbg 16 | OBJDIR = obj 17 | SRCDIR = src 18 | SOURCES = $(wildcard $(SRCDIR)/*.c) 19 | HEADERS = $(wildcard $(SRCDIR)/*.h) 20 | OBJECTS = $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o) 21 | DEPS = $(HEADERS) 22 | 23 | INCLPATH += -I. 24 | 25 | # Installation directories by convention 26 | # http://www.gnu.org/prep/standards/html_node/Directory-Variables.html 27 | PREFIX = /usr/local 28 | EXEC_PREFIX = $(PREFIX) 29 | BINDIR = $(EXEC_PREFIX)/bin 30 | SYSCONFDIR = $(PREFIX)/etc 31 | LOCALSTATEDIR = $(PREFIX)/var 32 | 33 | CFLAGS += -DSYSCONFDIR=\"$(SYSCONFDIR)\" 34 | CFLAGS += -DLOCALSTATEDIR=\"$(LOCALSTATEDIR)\" 35 | 36 | # Syntax of Conditionals 37 | # https://www.gnu.org/software/make/manual/html_node/Conditional-Syntax.html 38 | ifdef DAEMON_VERSION 39 | CFLAGS += -DLINUX_DAEMON_VERSION 40 | endif 41 | ifndef DETAILED_LOG 42 | CFLAGS += -DNDPRINTF 43 | endif 44 | ifdef RULES_ENGINE 45 | CFLAGS += -DRULES_ENGINE 46 | endif 47 | ifdef TLS_LIB_AXTLS 48 | CFLAGS += -DUSE_TLS_LIBRARY 49 | CFLAGS += -DAXTLS_LIBRARY 50 | LIBS += -laxtls 51 | endif 52 | ifdef TLS_LIB_OPENSSL 53 | CFLAGS += -DUSE_TLS_LIBRARY 54 | CFLAGS += -DOPENSSL_LIBRARY 55 | LIBS += -lssl -lcrypto 56 | ifdef STATIC_LINK 57 | LIBS += -lz 58 | endif 59 | endif 60 | ifdef SENSOR_DATA_DECODING 61 | CFLAGS += -DSENSOR_DATA 62 | endif 63 | ifdef SENSOR_DATA_STORING 64 | CFLAGS += -DSENSOR_DATA_MYSQL 65 | endif 66 | ifdef MQTT_DATA_STORING 67 | CFLAGS += -DMQTT_DATA_MYSQL 68 | endif 69 | ifneq ($(SENSOR_DATA_STORING),$(filter $(MQTT_DATA_STORING),)) 70 | INCLPATH += -I/usr/include/mysql 71 | LIBS += -lmysqlclient 72 | ifdef STATIC_LINK 73 | LIBS += -lz 74 | endif 75 | endif 76 | LIBS += -lpthread -lgcc_eh -lm 77 | ifdef STATIC_LINK 78 | LIBS += -ldl -static 79 | endif 80 | ifdef WITH_DEBUG 81 | CFLAGS += -g 82 | else 83 | CFLAGS += -O2 -DNDEBUG 84 | endif 85 | 86 | # main goal 87 | all: $(TARGET) 88 | 89 | # target executable 90 | $(TARGET): $(OBJECTS) 91 | $(CC) $(LDFLAGS) -o $(TARGET) $(OBJECTS) $(LIBPATH) $(LIBS) 92 | 93 | # object files 94 | $(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c $(DEPS) | $(OBJDIR) 95 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLPATH) 96 | 97 | # create object files directory 98 | $(OBJDIR): 99 | mkdir -p $(OBJDIR) 100 | 101 | # clean 102 | clean: 103 | rm -rf $(OBJDIR) 104 | 105 | # distclean 106 | distclean: clean 107 | rm -f $(TARGET) 108 | 109 | # install 110 | # http://unixhelp.ed.ac.uk/CGI/man-cgi?install 111 | install: all 112 | install -d -m 755 $(BINDIR) 113 | install -m 755 $(TARGET) $(BINDIR) 114 | install -d -m 755 $(SYSCONFDIR) 115 | cp -n ./res/whsnbg.conf $(SYSCONFDIR) || true 116 | chmod 644 $(SYSCONFDIR)/whsnbg.conf 117 | cp -n ./res/whsnbg.pem $(SYSCONFDIR) || true 118 | chmod 444 $(SYSCONFDIR)/whsnbg.pem 119 | 120 | # uninstall 121 | uninstall: 122 | rm -f $(BINDIR)/$(TARGET) 123 | rm -f $(SYSCONFDIR)/whsnbg.conf 124 | rm -f $(SYSCONFDIR)/whsnbg.pem 125 | rm -f $(LOCALSTATEDIR)/whsnbg.rules 126 | rm -f $(LOCALSTATEDIR)/whsnbg.log 127 | 128 | .PHONY: all clean distclean install uninstall 129 | -------------------------------------------------------------------------------- /msvs/.gitignore: -------------------------------------------------------------------------------- 1 | # Visual Studio 2005 files 2 | *.exe 3 | *.dll 4 | *.suo 5 | *.user 6 | *.ncb 7 | *.txt 8 | Debug/ 9 | Release/ 10 | 11 | # Visual Studio 2010 files 12 | *.[Oo]bj 13 | *.user 14 | *.aps 15 | *.pch 16 | *.vspscc 17 | *.vssscc 18 | *_i.c 19 | *_p.c 20 | *.ncb 21 | *.suo 22 | *.tlb 23 | *.tlh 24 | *.bak 25 | *.[Cc]ache 26 | *.ilk 27 | *.log 28 | *.lib 29 | *.sbr 30 | *.sdf 31 | *.opensdf 32 | *.unsuccessfulbuild 33 | ipch/ 34 | obj/ 35 | [Bb]in 36 | [Dd]ebug*/ 37 | [Rr]elease*/ 38 | [Pp]ublish 39 | Ankh.NoLoad 40 | -------------------------------------------------------------------------------- /msvs/whsnbg.sln: -------------------------------------------------------------------------------- 1 | Microsoft Visual Studio Solution File, Format Version 11.00 2 | # Visual Studio 2010 3 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "whsnbg", "whsnbg.vcxproj", "{50BBA366-B17A-4F94-AA0A-B4B9FE79B4A2}" 4 | EndProject 5 | Global 6 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 7 | Debug|Win32 = Debug|Win32 8 | Release|Win32 = Release|Win32 9 | EndGlobalSection 10 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 11 | {50BBA366-B17A-4F94-AA0A-B4B9FE79B4A2}.Debug|Win32.ActiveCfg = Debug|Win32 12 | {50BBA366-B17A-4F94-AA0A-B4B9FE79B4A2}.Debug|Win32.Build.0 = Debug|Win32 13 | {50BBA366-B17A-4F94-AA0A-B4B9FE79B4A2}.Release|Win32.ActiveCfg = Release|Win32 14 | {50BBA366-B17A-4F94-AA0A-B4B9FE79B4A2}.Release|Win32.Build.0 = Release|Win32 15 | EndGlobalSection 16 | GlobalSection(SolutionProperties) = preSolution 17 | HideSolutionNode = FALSE 18 | EndGlobalSection 19 | EndGlobal 20 | -------------------------------------------------------------------------------- /res/whsnbg.conf: -------------------------------------------------------------------------------- 1 | # name = value 2 | # maximum name length is 32 bytes 3 | # maximum value length is 64 bytes 4 | 5 | # MQTT section (tcp ports) 6 | mqtt_iface = eth0 7 | mqtt_port = 1883 8 | mqtt_tls_port = 8883 9 | mqtt_ws_port = 8082 10 | mqtt_ws_tls_port = 8081 11 | 12 | # MQTT-SN section (udp port) 13 | mqttsn_iface = eth0 14 | mqttsn_port = 1883 15 | 16 | # MySQL section (remote MySQL database) 17 | mysql_enable = 0 18 | mysql_server = 192.168.0.213 19 | mysql_user = whsnbg 20 | mysql_password = some_pass 21 | mysql_database = homewsn 22 | mysql_port = 3306 23 | 24 | # MQTT users section 25 | mqtt_auth_enable = 0 26 | user_name = name1 27 | user_password = password1 28 | user_publish_enable = 1 29 | user_name = name2 30 | user_password = password2 31 | user_publish_enable = 0 32 | -------------------------------------------------------------------------------- /res/whsnbg.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEogIBAAKCAQEAszsd4fMcxUQBWMQrJ26S7q3VtHWxt6yYjh6Y+KguC/gNf+7f 3 | CCFBhG/OdtWwWcqclALPYc/gaiStm7Vb7UQN6Yot3D9L35jabGe0P2hQSy6UHnTn 4 | S2WDHRwzScY/r3lTkwvhvTJcV8ksP4q69/Ly2HLZ2RSOMgg5Q0UbolzzqOtwY63q 5 | D6uHjpbPdmCYkcEZxBA0m4fhpoD0azvIbm7rCuFkaV1QecBe0SIq+Lls0WYtJIsr 6 | f5czZhgin3340O0iTzZEO2VjuI9he5oX3FeQ0Ryf950IRL0peLgr8Jdvrbh9BrjZ 7 | u3zPVVAKZsGSjZjE42bPTMt/xfQBAmO1FhQNowIDAQABAoIBAErj0MCwzLzAha+J 8 | iF6ZnVuN11XoN7H/tF7t+g0seNEXfMyQCzFic/XaofKVp3Jdbr+JzQ2ZxrOTT9dY 9 | GGkznnyO+EqnSBTN0baqPm6gL6y6cbmoL10ZtYdcWTHiXIn9sCYpONHNUTh9HrgO 10 | R8GDCmXW16AVLrVJKmQpZDqbjFRwGReiLG7gf/5MTQhJHEhwpAKy5hy5dUwsTxaM 11 | PK2f5eNPrwAUzFnuZNMZp6OrA+SgC0HSqSPnm/MxJKX7Q9Aa2czQj+Jr/ZI8jj3W 12 | 8nMdN15bZhz0J776w5NEv0ImcLS9NRpfpBslvXCZuKG9mg3tMLNW+wcE09sMCoMa 13 | xovxncECgYEA3Kt6J2CpRgJWClKuq2mnbjKFWJiIuIRMrzd3UT7nmbYMPsf4xVhh 14 | ULuti8s/T7W1CjP85UbzdtS1t8kjISJNoU0qC3rHyPJPOI/QlaKmDElfHiUHSLt4 15 | /Ib/HgRmvC8yjxQC9B7PQYRNzdw+Tn69XWqT0B96KwrPJglwodtaJF0CgYEAz+0y 16 | TS56AKSYBErmwknbx1vHp5a1GflHr0zKV/tInMM+IMV63RwvbTNPbStbrdOiVFty 17 | kzEUOCqnEwtOQuWa2WkomObyHYVQiLPMDH1OGdlO7an0KWgvvAhOTtldePrqWlHy 18 | Q+TUz/HZPbSPa7zSSvkDn9c84w3cVWiiNrDj2f8CgYApGHI2VSURGaxGXeGToKiC 19 | uDvnJPkJS/IwC2dYJRvin2mmY31r2yq8a0dFqoNO5+sgTFBDrrP2pGR3Jqk5KXdF 20 | m+ZBnwsH3EBuqzHQPCsqTJu/NGwn2moMtWNbhrxPPYbv6G0oY3h7nhUTSnn8ClRf 21 | ylJexw3saCXt0oI8Ump2LQKBgDiAe2fP1mVwVMLt27E2Qtv0y9d1s7bgL6OqWV35 22 | Me0vfSWIpG+LZjPlepLu8lXVKcqEoyjucFEm/gnFy7mQDTlBQz/xmh99h7nuKefG 23 | 8T9C9xjMU0YhQr90/lqc5LKodKCgnhXnDBGJ5f/HniWnxPmMm3iHE8ubi6FF8rar 24 | 8O7fAoGARiVjSHPkTmDMkgtlFty/pgpvFMTd64cd4c6vJwkwPIyUVCGhB+gTSLXD 25 | bXYWuR9YSwprwZhYzhT70rlpCB8fn2WulfDb1UYY7HwruSuheWF0CM8VHSzXM1a/ 26 | Hou+NQANiAkfMANmUnwlSTJZFa901jfy/t91zockDZc5aP9b5eo= 27 | -----END RSA PRIVATE KEY----- 28 | -----BEGIN CERTIFICATE----- 29 | MIIC+zCCAeOgAwIBAgIJAJ8hjgkvUc2JMA0GCSqGSIb3DQEBBQUAMBQxEjAQBgNV 30 | BAMMCTEyNy4wLjAuMTAeFw0xNTA2MjQxMTI3MDlaFw0yNTA2MjExMTI3MDlaMBQx 31 | EjAQBgNVBAMMCTEyNy4wLjAuMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC 32 | ggEBALM7HeHzHMVEAVjEKyduku6t1bR1sbesmI4emPioLgv4DX/u3wghQYRvznbV 33 | sFnKnJQCz2HP4GokrZu1W+1EDemKLdw/S9+Y2mxntD9oUEsulB5050tlgx0cM0nG 34 | P695U5ML4b0yXFfJLD+Kuvfy8thy2dkUjjIIOUNFG6Jc86jrcGOt6g+rh46Wz3Zg 35 | mJHBGcQQNJuH4aaA9Gs7yG5u6wrhZGldUHnAXtEiKvi5bNFmLSSLK3+XM2YYIp99 36 | +NDtIk82RDtlY7iPYXuaF9xXkNEcn/edCES9KXi4K/CXb624fQa42bt8z1VQCmbB 37 | ko2YxONmz0zLf8X0AQJjtRYUDaMCAwEAAaNQME4wHQYDVR0OBBYEFBGhv6dVe2tb 38 | dWVCTc7m79nib2H4MB8GA1UdIwQYMBaAFBGhv6dVe2tbdWVCTc7m79nib2H4MAwG 39 | A1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBAIpBl7JSbpdKXkuFNyYVLTRM 40 | stUkM48DUiL+pgBVbnAbxBiUd5pxFRdZF2Pg5zUVKpB43oCDu69kKAMrXoK5W+Q0 41 | OaAA6IQWgibxxQV7tzMfbj0ncbOn1UG/aqRLziBx1pJUPGP5Z6kkvP5iDK9fyjSE 42 | hzDNrS38eM0vKw9XXkWjQ4s95psF2Zct5SZJBcQJUNN0mIolvo85SU4T89H99cwC 43 | aSDLzJGmf8afFFZrde09KlzcFudZEOwHgO6qExk6j665HOjGqkrUc9AACD1q2Stp 44 | vbRdOKNZo5FlEVkwiKF2TIPQYYC0NKt9kCqrVCGjAM2C1GbmZgekbO4j7/yXu34= 45 | -----END CERTIFICATE----- 46 | -------------------------------------------------------------------------------- /src/config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef CONFIG_H_ 16 | #define CONFIG_H_ 17 | 18 | #ifdef WIN32 /* Windows */ 19 | // Windows 20 | // ** Manually configure ** >>>>> 21 | 22 | //#define USE_TLS_LIBRARY // use external TLS library 23 | // TLS library 24 | //#define OPENSSL_LIBRARY // OpenSSL 25 | //#define AXTLS_LIBRARY // axTLS 26 | //#define SSL_LIBRARY_HEADERS // TLS library headers 27 | 28 | // sensor and MySQL support 29 | //#define SENSOR_DATA // automatic decoding of the MQTT-SN message payload from the sensors 30 | //#define SENSOR_DATA_MYSQL // storing decoded data from the sensors in the MySQL database (see parse_mqttsn_topic_name_to_mysql_query in sensor_data.c) 31 | //#define MQTT_DATA_MYSQL // storing payload from the specific MQTT topics in the MySQL database (see parse_mqtt_topic_name_to_mysql_query in sensor_data.c) 32 | 33 | // assert() and detailed log messages support 34 | #define NDEBUG // disable assert() 35 | #define NDPRINTF // disable detailed log messages 36 | 37 | // rules engine support 38 | //#define RULES_ENGINE 39 | 40 | #define SYSCONFDIR "../res" 41 | #define LOCALSTATEDIR "../res" 42 | 43 | // <<<<< ** Manually configure ** 44 | 45 | #else /* Linux, OpenWRT */ 46 | // all options in Makefile 47 | #endif 48 | 49 | #ifdef SENSOR_DATA_MYSQL 50 | #ifndef SENSOR_DATA 51 | #define SENSOR_DATA 52 | #endif 53 | #endif 54 | #if defined SENSOR_DATA_MYSQL || defined MQTT_DATA_MYSQL 55 | #define THREAD_MYSQL 56 | #endif 57 | 58 | // MQTT-SN IPv4 network interface MTU size 59 | // IEEE 802.15.4 + Contiki Rime => 127 - 9 - 2 - 6 = 110 bytes 60 | // including IPv4 header(20) + UDP header(8) + MQTT-SN payload(82) 61 | // (packet fragmentation is not supported) 62 | #define MQTTSN_IF_MTU 110 63 | 64 | 65 | #ifdef SYSCONFDIR 66 | #define CONFIG_FILE SYSCONFDIR "/whsnbg.conf" 67 | #define PEM_FILE SYSCONFDIR "/whsnbg.pem" 68 | #define RULES_FILE SYSCONFDIR "/whsnbg.rules" 69 | #else 70 | #define CONFIG_FILE "whsnbg.conf" 71 | #define PEM_FILE "whsnbg.pem" 72 | #define RULES_FILE "whsnbg.rules" 73 | #endif 74 | #ifdef LOCALSTATEDIR 75 | #define LOG_FILE LOCALSTATEDIR "/whsnbg.log" 76 | #else 77 | #define LOG_FILE "whsnbg.log" 78 | #endif 79 | 80 | #endif /* CONFIG_H_ */ 81 | -------------------------------------------------------------------------------- /src/cron_trigger.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef CRON_TRIGGER_H_ 16 | #define CRON_TRIGGER_H_ 17 | 18 | #include "list.h" 19 | #include /* time_t, time() */ 20 | 21 | #ifdef _MSC_VER 22 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 23 | #else 24 | #include /* uint8_t ... uint64_t */ 25 | #include /* size_t */ 26 | #endif 27 | 28 | 29 | /* -------------------------------------------- 30 | * The following structure has been taken from BusyBox crond.c 31 | * Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com) 32 | * (version 2.3.2) 33 | * Vladimir Oleynik (C) 2002 34 | * 35 | * Licensed under GPLv2. 36 | */ 37 | typedef struct cron_line 38 | { 39 | /* ordered by size, not in natural order. makes code smaller: */ 40 | char dow[7]; /* 0-6, beginning sunday */ 41 | char mons[12]; /* 0-11 */ 42 | char hrs[24]; /* 0-23 */ 43 | char days[32]; /* 1-31 */ 44 | char mins[60]; /* 0-59 */ 45 | } cron_line_t; 46 | 47 | //-------------------------------------------- 48 | typedef struct cron_trigger 49 | { 50 | list_t next; 51 | cron_line_t cl; 52 | uint32_t next_id; 53 | } cron_trigger_t; 54 | 55 | unsigned int flag_starting_jobs(cron_line_t *cl); 56 | 57 | cron_trigger_t *cron_trigger_add_new(cron_trigger_t **list, const char *str, uint32_t next_id); 58 | #define cron_trigger_head(a) (cron_trigger_t *)list_head((list_t **)a) 59 | #define cron_trigger_next(a) (cron_trigger_t *)list_next((list_t *)a) 60 | void cron_trigger_remove_all(cron_trigger_t **list); 61 | 62 | #endif /* CRON_TRIGGER_H_ */ 63 | -------------------------------------------------------------------------------- /src/list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "list.h" 16 | #include /* assert */ 17 | 18 | 19 | //-------------------------------------------- 20 | void list_init(list_t **list) 21 | { 22 | assert(list != NULL); 23 | 24 | *list = NULL; 25 | } 26 | 27 | //-------------------------------------------- 28 | list_t *list_head(list_t **list) 29 | { 30 | assert(list != NULL); 31 | 32 | return *list; 33 | } 34 | 35 | //-------------------------------------------- 36 | list_t *list_remove(list_t **list, list_t *item) 37 | { 38 | list_t *find; 39 | list_t *prev; 40 | list_t *next; 41 | 42 | assert(list != NULL); 43 | assert(item != NULL); 44 | 45 | if (*list == NULL) 46 | return NULL; 47 | prev = NULL; 48 | for (find = *list; find != NULL; find = find->next) 49 | { 50 | if (find == item) 51 | { 52 | next = find->next; 53 | if (prev == NULL) 54 | *list = next; 55 | else 56 | prev->next = next; 57 | find->next = NULL; 58 | return next; 59 | } 60 | prev = find; 61 | } 62 | return NULL; 63 | } 64 | 65 | //-------------------------------------------- 66 | list_t *list_add_item(list_t **list, list_t *item) 67 | { 68 | list_t *last; 69 | 70 | assert(list != NULL); 71 | assert(item != NULL); 72 | 73 | item->next = NULL; 74 | 75 | if (*list == NULL) 76 | *list = item; 77 | else 78 | { 79 | for (last = *list; last->next != NULL; last = last->next); 80 | last->next = item; 81 | } 82 | 83 | return item; 84 | } 85 | 86 | //-------------------------------------------- 87 | size_t list_get_length(list_t **list) 88 | { 89 | list_t *item; 90 | size_t cnt = 0; 91 | 92 | assert(list != NULL); 93 | 94 | for (item = *list, cnt = 0; item != NULL; item = item->next, ++cnt); 95 | return cnt; 96 | } 97 | 98 | //-------------------------------------------- 99 | list_t *list_next(list_t *item) 100 | { 101 | return item == NULL ? NULL : item->next; 102 | } 103 | -------------------------------------------------------------------------------- /src/list.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef LIST_H_ 16 | #define LIST_H_ 17 | 18 | #ifdef _MSC_VER 19 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 20 | #include /* NULL */ 21 | #else 22 | #include /* uint8_t ... uint64_t */ 23 | #include /* size_t */ 24 | #endif 25 | 26 | 27 | //-------------------------------------------- 28 | typedef struct list 29 | { 30 | struct list *next; 31 | } list_t; 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | //-------------------------------------------- 37 | void list_init(list_t **list); 38 | list_t *list_head(list_t **list); 39 | list_t *list_remove(list_t **list, list_t *item); 40 | list_t *list_add_item(list_t **list, list_t *item); 41 | size_t list_get_length(list_t **list); 42 | list_t *list_next(list_t *item); 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif /* LIST_H_ */ 48 | -------------------------------------------------------------------------------- /src/list_mqtt_conn.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015, 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "list_mqtt_conn.h" 16 | #include /* assert */ 17 | #include /* malloc */ 18 | #include /* memcpy, memset, memcmp */ 19 | 20 | #ifdef _MSC_VER 21 | #pragma warning (disable:4996) // This function may be unsafe. 22 | #endif 23 | 24 | 25 | //-------------------------------------------- 26 | list_mqtt_conn_t *list_mqtt_conn_add_new(list_mqtt_conn_t **list, struct sockaddr_in *addr) 27 | { 28 | list_mqtt_conn_t *item; 29 | 30 | assert(list != NULL); 31 | 32 | item = (list_mqtt_conn_t *)malloc(sizeof(list_mqtt_conn_t)); 33 | memset(item, 0, sizeof(list_mqtt_conn_t)); 34 | memcpy(&item->addr, addr, sizeof(struct sockaddr_in)); 35 | 36 | list_mqtt_conn_add(list, item); 37 | return item; 38 | } 39 | 40 | //-------------------------------------------- 41 | list_mqtt_conn_t *list_mqtt_conn_find_addr(list_mqtt_conn_t **list, struct sockaddr_in *addr) 42 | { 43 | list_mqtt_conn_t *item; 44 | 45 | assert(list != NULL); 46 | 47 | item = *list; 48 | while (item != NULL) 49 | { 50 | if (memcmp(&item->addr, addr, sizeof(struct sockaddr_in)) == 0) 51 | return item; 52 | item = list_mqtt_conn_next(item); 53 | } 54 | return NULL; 55 | } 56 | 57 | #if 0 58 | //-------------------------------------------- 59 | list_mqtt_conn_t *list_mqtt_conn_find_client_id(list_mqtt_conn_t **list, const char *client_id, size_t client_id_length) 60 | { 61 | list_mqtt_conn_t *item; 62 | 63 | assert(list != NULL); 64 | 65 | item = *list; 66 | while (item != NULL) 67 | { 68 | if (item->client_id_length == client_id_length && 69 | memcmp(item->client_id, client_id, client_id_length) == 0) 70 | return item; 71 | item = list_mqtt_conn_next(item); 72 | } 73 | return NULL; 74 | } 75 | 76 | //-------------------------------------------- 77 | void list_mqtt_conn_set_client_id(list_mqtt_conn_t *item, const char *client_id, size_t client_id_length) 78 | { 79 | assert(item != NULL); 80 | 81 | if (item->client_id != NULL) 82 | free(item->client_id); 83 | item->client_id_length = client_id_length; 84 | item->client_id = (char *)malloc(client_id_length); 85 | memcpy(item->client_id, client_id, client_id_length); 86 | } 87 | #endif 88 | 89 | //-------------------------------------------- 90 | void list_mqtt_conn_reset_remainsec(list_mqtt_conn_t *item) 91 | { 92 | assert(item != NULL); 93 | 94 | item->remainsec = item->keepalivesec; 95 | } 96 | 97 | //-------------------------------------------- 98 | void list_mqtt_conn_topics_clear(list_mqtt_conn_t *item) 99 | { 100 | list_sub_remove_all(&item->sub_list); 101 | will_remove_all(&item->will); 102 | } 103 | 104 | //-------------------------------------------- 105 | list_mqtt_conn_t *list_mqtt_conn_remove(list_mqtt_conn_t **list, list_mqtt_conn_t *item) 106 | { 107 | list_mqtt_conn_t *next; 108 | 109 | assert(list != NULL); 110 | assert(item != NULL); 111 | 112 | #if 0 113 | if (item->client_id != NULL) 114 | free(item->client_id); 115 | #endif 116 | list_mqtt_conn_topics_clear(item); 117 | next = (list_mqtt_conn_t *)list_remove((list_t **)list, (list_t *)item); 118 | free(item); 119 | return next; 120 | } 121 | 122 | //-------------------------------------------- 123 | void list_mqtt_conn_remove_all(list_mqtt_conn_t **list) 124 | { 125 | list_mqtt_conn_t *next; 126 | list_mqtt_conn_t *item; 127 | 128 | item = *list; 129 | while (item != NULL) 130 | { 131 | next = list_mqtt_conn_next(item); 132 | #if 0 133 | if (item->client_id != NULL) 134 | free(item->client_id); 135 | #endif 136 | list_mqtt_conn_topics_clear(item); 137 | free(item); 138 | item = next; 139 | } 140 | *list = NULL; 141 | } 142 | -------------------------------------------------------------------------------- /src/list_mqtt_conn.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef LIST_MQTT_CONN_H_ 16 | #define LIST_MQTT_CONN_H_ 17 | 18 | #include "lists.h" 19 | #include "os_port.h" 20 | #include "mqtt.h" 21 | 22 | #ifdef _MSC_VER 23 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 24 | #else 25 | #include /* uint8_t ... uint64_t */ 26 | #include /* size_t */ 27 | #endif 28 | 29 | //-------------------------------------------- 30 | typedef struct list_mqtt_conn 31 | { 32 | list_t next; 33 | struct sockaddr_in addr; 34 | #if 0 35 | char *client_id; 36 | size_t client_id_length; 37 | #endif 38 | list_sub_t *sub_list; 39 | will_t will; 40 | size_t keepalivesec; 41 | size_t remainsec; 42 | unsigned char publish_enable; 43 | } list_mqtt_conn_t; 44 | 45 | //-------------------------------------------- 46 | #define list_mqtt_conn_add(a, b) (list_mqtt_conn_t *)list_add_item((list_t **)a, (list_t *)b) 47 | #define list_mqtt_conn_next(a) (list_mqtt_conn_t *)list_next((list_t *)a) 48 | list_mqtt_conn_t *list_mqtt_conn_add_new(list_mqtt_conn_t **list, struct sockaddr_in *addr); 49 | list_mqtt_conn_t *list_mqtt_conn_find_addr(list_mqtt_conn_t **list, struct sockaddr_in *addr); 50 | #if 0 51 | list_mqtt_conn_t *list_mqttsn_conn_find_client_id(list_mqtt_conn_t **list, const char *client_id, size_t client_id_length); 52 | void list_mqtt_conn_set_client_id(list_mqtt_conn_t *item, const char *client_id, size_t client_id_length); 53 | #endif 54 | void list_mqtt_conn_reset_remainsec(list_mqtt_conn_t *item); 55 | list_mqtt_conn_t *list_mqtt_conn_remove(list_mqtt_conn_t **list, list_mqtt_conn_t *item); 56 | void list_mqtt_conn_remove_all(list_mqtt_conn_t **list); 57 | 58 | //-------------------------------------------- 59 | #define mqtt_conn_will_remove(a) will_remove_all(&a->will) 60 | #define mqtt_conn_will_set(a, b, c, d, e, f) will_replace(&a->will, b, c, d, e, f) 61 | 62 | #endif /* LIST_MQTT_CONN_H_ */ 63 | -------------------------------------------------------------------------------- /src/list_mqtt_user.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "list_mqtt_user.h" 16 | #include /* assert */ 17 | #include /* malloc */ 18 | #include /* memcpy, memset, memcmp */ 19 | 20 | #ifdef _MSC_VER 21 | #pragma warning (disable:4996) // This function may be unsafe. 22 | #endif 23 | 24 | 25 | //-------------------------------------------- 26 | list_mqtt_user_t *list_mqtt_user_add_new(list_mqtt_user_t **list, const char *user_name, const char *password, unsigned char publish_enable) 27 | { 28 | list_mqtt_user_t *item; 29 | 30 | assert(list != NULL); 31 | assert(user_name != NULL); 32 | assert(password != NULL); 33 | 34 | item = (list_mqtt_user_t *)malloc(sizeof(list_mqtt_user_t)); 35 | memset(item, 0, sizeof(list_mqtt_user_t)); 36 | item->user_name_length = (uint16_t)strlen(user_name); 37 | item->user_name = (uint8_t *)malloc(item->user_name_length); 38 | memcpy(item->user_name, user_name, item->user_name_length); 39 | item->password_length = (uint16_t)strlen(password); 40 | item->password = (uint8_t *)malloc(item->password_length); 41 | memcpy(item->password, password, item->password_length); 42 | item->publish_enable = publish_enable; 43 | 44 | list_mqtt_user_add(list, item); 45 | return item; 46 | } 47 | 48 | //-------------------------------------------- 49 | void list_mqtt_user_remove_all(list_mqtt_user_t **list) 50 | { 51 | list_mqtt_user_t *next; 52 | list_mqtt_user_t *item; 53 | 54 | assert(list != NULL); 55 | 56 | item = *list; 57 | while (item != NULL) 58 | { 59 | next = list_mqtt_user_next(item); 60 | if (item->user_name != NULL) 61 | free(item->user_name); 62 | if (item->password != NULL) 63 | free(item->password); 64 | free(item); 65 | item = next; 66 | } 67 | *list = NULL; 68 | } 69 | 70 | //-------------------------------------------- 71 | list_mqtt_user_t *list_mqtt_user_find_user(list_mqtt_user_t **list, uint8_t *user_name, uint16_t user_name_length, uint8_t *password, uint16_t password_length) 72 | { 73 | list_mqtt_user_t *item; 74 | 75 | assert(list != NULL); 76 | assert(user_name != NULL); 77 | assert(password != NULL); 78 | assert(user_name_length != 0); 79 | assert(password_length != 0); 80 | 81 | item = *list; 82 | while (item != NULL) 83 | { 84 | if ((item->user_name_length == user_name_length && memcmp(item->user_name, user_name, user_name_length) == 0) && 85 | (item->password_length == password_length && memcmp(item->password, password, password_length) == 0)) 86 | return item; 87 | item = list_mqtt_user_next(item); 88 | } 89 | return NULL; 90 | } 91 | -------------------------------------------------------------------------------- /src/list_mqtt_user.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef LIST_USER_H_ 16 | #define LIST_USER_H_ 17 | 18 | #include "list.h" 19 | 20 | #ifdef _MSC_VER 21 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 22 | #else 23 | #include /* uint8_t ... uint64_t */ 24 | #include /* size_t */ 25 | #endif 26 | 27 | typedef struct list_mqtt_user 28 | { 29 | list_t next; 30 | uint16_t user_name_length; 31 | uint8_t *user_name; 32 | uint16_t password_length; 33 | uint8_t *password; 34 | unsigned char publish_enable; 35 | } list_mqtt_user_t; 36 | 37 | list_mqtt_user_t *list_mqtt_user_add_new(list_mqtt_user_t **list, const char *user_name, const char *password, unsigned char publish_enable); 38 | #define list_mqtt_user_add(a, b) (list_mqtt_user_t *)list_add_item((list_t **)a, (list_t *)b) 39 | #define list_mqtt_user_next(a) (list_mqtt_user_t *)list_next((list_t *)a) 40 | void list_mqtt_user_remove_all(list_mqtt_user_t **list); 41 | list_mqtt_user_t *list_mqtt_user_find_user(list_mqtt_user_t **list, uint8_t *user_name, uint16_t user_name_length, uint8_t *password, uint16_t password_length); 42 | 43 | #endif /* LIST_USER_H_ */ 44 | -------------------------------------------------------------------------------- /src/list_mqttsn_conn.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "list_mqttsn_conn.h" 16 | #include /* assert */ 17 | #include /* malloc */ 18 | #include /* memcpy, memset, memcmp */ 19 | 20 | #ifdef _MSC_VER 21 | #pragma warning (disable:4996) // This function may be unsafe. 22 | #endif 23 | 24 | 25 | //-------------------------------------------- 26 | list_mqttsn_conn_t *list_mqttsn_conn_add_new(list_mqttsn_conn_t **list, struct sockaddr_in *addr) 27 | { 28 | list_mqttsn_conn_t *item; 29 | 30 | assert(list != NULL); 31 | 32 | item = (list_mqttsn_conn_t *)malloc(sizeof(list_mqttsn_conn_t)); 33 | memset(item, 0, sizeof(list_mqttsn_conn_t)); 34 | memcpy(&item->addr, addr, sizeof(struct sockaddr_in)); 35 | 36 | list_mqttsn_conn_add(list, item); 37 | return item; 38 | } 39 | 40 | //-------------------------------------------- 41 | list_mqttsn_conn_t *list_mqttsn_conn_find_addr(list_mqttsn_conn_t **list, struct sockaddr_in *addr) 42 | { 43 | list_mqttsn_conn_t *item; 44 | 45 | assert(list != NULL); 46 | 47 | item = *list; 48 | while (item != NULL) 49 | { 50 | if (memcmp(&item->addr, addr, sizeof(struct sockaddr_in)) == 0) 51 | return item; 52 | item = list_mqttsn_conn_next(item); 53 | } 54 | return NULL; 55 | } 56 | 57 | //-------------------------------------------- 58 | list_mqttsn_conn_t *list_mqttsn_conn_find_client_id(list_mqttsn_conn_t **list, const char *client_id, size_t client_id_length) 59 | { 60 | list_mqttsn_conn_t *item; 61 | 62 | assert(list != NULL); 63 | 64 | item = *list; 65 | while (item != NULL) 66 | { 67 | if (item->client_id_length == client_id_length && 68 | memcmp(item->client_id, client_id, client_id_length) == 0) 69 | return item; 70 | item = list_mqttsn_conn_next(item); 71 | } 72 | return NULL; 73 | } 74 | 75 | //-------------------------------------------- 76 | void list_mqttsn_conn_set_client_id(list_mqttsn_conn_t *item, const char *client_id, size_t client_id_length) 77 | { 78 | assert(item != NULL); 79 | 80 | if (item->client_id != NULL) 81 | free(item->client_id); 82 | item->client_id_length = client_id_length; 83 | item->client_id = (char *)malloc(client_id_length); 84 | memcpy(item->client_id, client_id, client_id_length); 85 | } 86 | 87 | //-------------------------------------------- 88 | void list_mqttsn_conn_reset_remainsec(list_mqttsn_conn_t *item) 89 | { 90 | assert(item != NULL); 91 | assert(item->keepalivesec != 0); 92 | 93 | item->remainsec = item->keepalivesec; 94 | } 95 | 96 | //-------------------------------------------- 97 | void list_mqttsn_conn_topics_clear(list_mqttsn_conn_t *item) 98 | { 99 | list_sub_remove_all(&item->sub_list); 100 | will_remove_all(&item->will); 101 | list_reg_remove_all(&item->reg_list); 102 | list_msg_remove_all(&item->reg_msg_list); 103 | list_msg_remove_all(&item->pub_msg_list); 104 | } 105 | 106 | //-------------------------------------------- 107 | list_mqttsn_conn_t *list_mqttsn_conn_remove(list_mqttsn_conn_t **list, list_mqttsn_conn_t *item) 108 | { 109 | list_mqttsn_conn_t *next; 110 | 111 | assert(list != NULL); 112 | assert(item != NULL); 113 | 114 | if (item->client_id != NULL) 115 | free(item->client_id); 116 | list_mqttsn_conn_topics_clear(item); 117 | next = (list_mqttsn_conn_t *)list_remove((list_t **)list, (list_t *)item); 118 | free(item); 119 | return next; 120 | } 121 | 122 | //-------------------------------------------- 123 | void list_mqttsn_conn_remove_all(list_mqttsn_conn_t **list) 124 | { 125 | list_mqttsn_conn_t *next; 126 | list_mqttsn_conn_t *item; 127 | 128 | item = *list; 129 | while (item != NULL) 130 | { 131 | next = list_mqttsn_conn_next(item); 132 | if (item->client_id != NULL) 133 | free(item->client_id); 134 | list_mqttsn_conn_topics_clear(item); 135 | free(item); 136 | item = next; 137 | } 138 | *list = NULL; 139 | } 140 | -------------------------------------------------------------------------------- /src/list_mqttsn_conn.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef LIST_MQTTSN_CONN_H_ 16 | #define LIST_MQTTSN_CONN_H_ 17 | 18 | #include "lists.h" 19 | #include "os_port.h" 20 | #include "mqttsn.h" 21 | 22 | #ifdef _MSC_VER 23 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 24 | #else 25 | #include /* uint8_t ... uint64_t */ 26 | #include /* size_t */ 27 | #endif 28 | 29 | //-------------------------------------------- 30 | typedef struct list_mqttsn_conn 31 | { 32 | list_t next; 33 | struct sockaddr_in addr; 34 | char *client_id; 35 | size_t client_id_length; 36 | list_sub_t *sub_list; 37 | will_t will; 38 | size_t keepalivesec; 39 | size_t remainsec; 40 | mqttsn_client_state_t state; 41 | list_reg_t *reg_list; // list of the registered topics 42 | list_msg_t *reg_msg_list; // list of the REGISTER messages to send 43 | list_msg_t *pub_msg_list; // list of the PUBLISH messages to send 44 | } list_mqttsn_conn_t; 45 | 46 | //-------------------------------------------- 47 | #define list_mqttsn_conn_add(a, b) (list_mqttsn_conn_t *)list_add_item((list_t **)a, (list_t *)b) 48 | #define list_mqttsn_conn_next(a) (list_mqttsn_conn_t *)list_next((list_t *)a) 49 | list_mqttsn_conn_t *list_mqttsn_conn_add_new(list_mqttsn_conn_t **list, struct sockaddr_in *addr); 50 | list_mqttsn_conn_t *list_mqttsn_conn_find_addr(list_mqttsn_conn_t **list, struct sockaddr_in *addr); 51 | list_mqttsn_conn_t *list_mqttsn_conn_find_client_id(list_mqttsn_conn_t **list, const char *client_id, size_t client_id_length); 52 | void list_mqttsn_conn_set_client_id(list_mqttsn_conn_t *item, const char *client_id, size_t client_id_length); 53 | void list_mqttsn_conn_reset_remainsec(list_mqttsn_conn_t *item); 54 | void list_mqttsn_conn_topics_clear(list_mqttsn_conn_t *item); 55 | list_mqttsn_conn_t *list_mqttsn_conn_remove(list_mqttsn_conn_t **list, list_mqttsn_conn_t *item); 56 | void list_mqttsn_conn_remove_all(list_mqttsn_conn_t **list); 57 | 58 | //-------------------------------------------- 59 | #define mqttsn_conn_registered_topic_id_add(a, b) list_reg_add(&a->reg_list, b) 60 | #define mqttsn_conn_registered_topic_id_find(a, b) list_reg_find_topic_id(&a->reg_list, b) 61 | 62 | //-------------------------------------------- 63 | #define mqttsn_conn_register_msg_head(a) list_msg_head(&a->reg_msg_list) 64 | #define mqttsn_conn_register_msg_length(a) list_msg_length(&a->reg_msg_list) 65 | #define mqttsn_conn_register_msg_add(a, b, c) list_msg_add(&a->reg_msg_list, b->topic_id, c) 66 | #define mqttsn_conn_register_msg_find(a, b) list_msg_find_msg_id(&a->reg_msg_list, b) 67 | #define mqttsn_conn_register_msg_remove_msg_id(a, b) list_msg_remove_msg_id(&a->reg_msg_list, b) 68 | 69 | //-------------------------------------------- 70 | #define mqttsn_conn_publish_msg_head(a) list_msg_head(&a->pub_msg_list) 71 | #define mqttsn_conn_publish_msg_length(a) list_msg_length(&a->pub_msg_list) 72 | #define mqttsn_conn_publish_msg_add(a, b, c) list_msg_add(&a->pub_msg_list, b->topic_id, c) 73 | #define mqttsn_conn_publish_msg_remove(a, b) list_msg_remove(&a->pub_msg_list, b) 74 | #define mqttsn_conn_publish_msg_find(a, b) list_msg_find_msg_id(&a->pub_msg_list, b) 75 | #define mqttsn_conn_publish_msg_remove_msg_id(a, b) list_msg_remove_msg_id(&a->pub_msg_list, b) 76 | 77 | //-------------------------------------------- 78 | #define mqttsn_conn_will_remove(a) will_remove_all(&a->will) 79 | #define mqttsn_conn_will_name_set(a, b, c, d) will_name_replace(&a->will, b, c, d) 80 | #define mqttsn_conn_will_data_set(a, b, c) will_data_replace(&a->will, b, c) 81 | 82 | 83 | #endif /* LIST_MQTTSN_CONN_H_ */ 84 | -------------------------------------------------------------------------------- /src/list_tcp_conn.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "config.h" 16 | #include "tls.h" 17 | #include "list_tcp_conn.h" 18 | #include /* assert */ 19 | #include /* malloc */ 20 | #include /* memcpy, memset, memcmp */ 21 | 22 | #ifdef _MSC_VER 23 | #pragma warning (disable:4996) // This function may be unsafe. 24 | #endif 25 | 26 | 27 | //-------------------------------------------- 28 | list_tcp_conn_t *list_tcp_conn_add_new(list_tcp_conn_t **list, struct sockaddr_in *addr) 29 | { 30 | list_tcp_conn_t *item; 31 | 32 | assert(list != NULL); 33 | 34 | item = (list_tcp_conn_t *)malloc(sizeof(list_tcp_conn_t)); 35 | memset(item, 0, sizeof(list_tcp_conn_t)); 36 | memcpy(&item->addr, addr, sizeof(struct sockaddr_in)); 37 | 38 | list_tcp_conn_add(list, item); 39 | return item; 40 | } 41 | 42 | //-------------------------------------------- 43 | list_tcp_conn_t *list_tcp_conn_find_addr(list_tcp_conn_t **list, struct sockaddr_in *addr) 44 | { 45 | list_tcp_conn_t *item; 46 | 47 | assert(list != NULL); 48 | 49 | item = *list; 50 | while (item != NULL) 51 | { 52 | if (memcmp(&item->addr, addr, sizeof(struct sockaddr_in)) == 0) 53 | return item; 54 | item = list_tcp_conn_next(item); 55 | } 56 | return NULL; 57 | } 58 | 59 | //-------------------------------------------- 60 | list_tcp_conn_t *list_tcp_conn_remove(list_tcp_conn_t **list, list_tcp_conn_t *item) 61 | { 62 | list_tcp_conn_t *next; 63 | 64 | assert(list != NULL); 65 | assert(item != NULL); 66 | 67 | next = (list_tcp_conn_t *)list_remove((list_t **)list, (list_t *)item); 68 | free(item); 69 | return next; 70 | } 71 | 72 | //-------------------------------------------- 73 | void list_tcp_conn_remove_all(list_tcp_conn_t **list) 74 | { 75 | list_tcp_conn_t *next; 76 | list_tcp_conn_t *item; 77 | 78 | item = *list; 79 | while (item != NULL) 80 | { 81 | next = list_tcp_conn_next(item); 82 | free(item); 83 | item = next; 84 | } 85 | *list = NULL; 86 | } 87 | -------------------------------------------------------------------------------- /src/list_tcp_conn.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef LIST_TCP_CONN_H_ 16 | #define LIST_TCP_CONN_H_ 17 | 18 | #include "lists.h" 19 | #include "os_port.h" 20 | 21 | #ifdef _MSC_VER 22 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 23 | #else 24 | #include /* uint8_t ... uint64_t */ 25 | #include /* size_t */ 26 | #endif 27 | 28 | //-------------------------------------------- 29 | typedef enum 30 | { 31 | WS_OPCODE_CONTINUATION = 0x00, 32 | WS_OPCODE_TEXT = 0x01, 33 | WS_OPCODE_BINARY = 0x02, 34 | WS_OPCODE_CONNECTION_CLOSE = 0x08, 35 | WS_OPCODE_PING = 0x09, 36 | WS_OPCODE_PONG = 0x0A 37 | } ws_opcode_t; 38 | 39 | //-------------------------------------------- 40 | typedef struct ws_frame 41 | { 42 | ws_opcode_t opcode; 43 | unsigned char mask; 44 | unsigned char fin; 45 | unsigned char key[4]; 46 | size_t hdr_size; 47 | size_t pld_size; 48 | unsigned char hdr_buf[14]; 49 | size_t hdr_cnt; 50 | } ws_frame_t; 51 | 52 | //-------------------------------------------- 53 | typedef enum 54 | { 55 | HANDLE_NOTHING = 0, 56 | HANDLE_HTTP_HEADER, 57 | HANDLE_WS_FRAME, 58 | HANDLE_MQTT_MESSAGE 59 | } conn_state_t; 60 | 61 | //-------------------------------------------- 62 | typedef struct list_tcp_conn 63 | { 64 | list_t next; 65 | SOCKET sock; 66 | struct sockaddr_in addr; 67 | #ifdef USE_TLS_LIBRARY 68 | SSL *ssl; 69 | #endif 70 | conn_state_t state; 71 | unsigned char *recv_buf; 72 | size_t recv_cnt; 73 | ws_frame_t ws_frame; 74 | } list_tcp_conn_t; 75 | 76 | //-------------------------------------------- 77 | #define list_tcp_conn_add(a, b) (list_tcp_conn_t *)list_add_item((list_t **)a, (list_t *)b) 78 | #define list_tcp_conn_next(a) (list_tcp_conn_t *)list_next((list_t *)a) 79 | list_tcp_conn_t *list_tcp_conn_add_new(list_tcp_conn_t **list, struct sockaddr_in *addr); 80 | list_tcp_conn_t *list_tcp_conn_find_addr(list_tcp_conn_t **list, struct sockaddr_in *addr); 81 | list_tcp_conn_t *list_tcp_conn_remove(list_tcp_conn_t **list, list_tcp_conn_t *item); 82 | void list_tcp_conn_remove_all(list_tcp_conn_t **list); 83 | 84 | #endif /* LIST_TCP_CONN_H_ */ 85 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "config.h" 16 | #ifdef LINUX_DAEMON_VERSION 17 | #include 18 | #include 19 | #include 20 | #include /* errno */ 21 | #include /* umask */ 22 | #endif 23 | 24 | #include /* exit */ 25 | #include /* getchar */ 26 | #include "os_port.h" 27 | #include "msg_tcp_mqtt.h" 28 | #include "msg_mqtt_tcp.h" 29 | #include "msg_udp_mqtt.h" 30 | #include "msg_mqtt_udp.h" 31 | #include "msg_mqtt_mysql.h" 32 | #include "msg_trigger_rules.h" 33 | #include "msg_rules_mqtt.h" 34 | #include "thread_mqtt.h" 35 | #include "thread_tcp.h" 36 | #include "thread_udp.h" 37 | #include "thread_mysql.h" 38 | #include "thread_cron.h" 39 | #include "thread_rules.h" 40 | #include "parse_conf.h" 41 | #include "parse_json.h" 42 | 43 | #ifdef _MSC_VER 44 | #pragma warning (disable:4996) // This function may be unsafe. 45 | #endif 46 | 47 | #ifdef LINUX_DAEMON_VERSION 48 | unsigned int signal_exit = 0; 49 | 50 | //-------------------------------------------- 51 | void signal_handler(int sig) 52 | { 53 | switch(sig) 54 | { 55 | case SIGTERM: 56 | syslog(LOG_WARNING, "Received SIGTERM signal."); 57 | signal_exit = 1; 58 | break; 59 | } 60 | } 61 | #endif 62 | 63 | 64 | //-------------------------------------------- 65 | int main(void) 66 | { 67 | #ifdef WIN32 68 | WSADATA data; 69 | WSAStartup(MAKEWORD(2, 2), &data); 70 | #endif 71 | 72 | #ifdef LINUX_DAEMON_VERSION 73 | pid_t pid; 74 | 75 | // setup signal handling 76 | signal(SIGTERM, signal_handler); 77 | 78 | // fork off the parent process 79 | pid = fork(); 80 | if (pid < 0) 81 | { 82 | fprintf(stderr, "fork failed: %s\n", strerror(errno)); 83 | exit(EXIT_FAILURE); 84 | } 85 | if (pid > 0) 86 | { 87 | // kill the parent process 88 | fprintf(stdout, "process id of child process %d \n", pid); 89 | exit(EXIT_SUCCESS); 90 | } 91 | // change the file mode mask 92 | umask(0); 93 | // set new session 94 | if (setsid() < 0) 95 | { 96 | fprintf(stderr, "setsid failed: %s\n", strerror(errno)); 97 | exit(EXIT_FAILURE); 98 | } 99 | // change the current working directory to root. 100 | if ((chdir("/")) < 0) 101 | { 102 | fprintf(stderr, "chdir failed: %s\n", strerror(errno)); 103 | exit(EXIT_FAILURE); 104 | } 105 | // close stdin, stdout and stderr 106 | close(STDIN_FILENO); 107 | close(STDOUT_FILENO); 108 | close(STDERR_FILENO); 109 | 110 | // open a log file 111 | openlog("whsnbg", LOG_PID, LOG_DAEMON); 112 | #endif 113 | 114 | if (parse_conf_file() < 0) 115 | exit(EXIT_FAILURE); 116 | 117 | msg_tcp_mqtt_init(); 118 | msg_mqtt_tcp_init(); 119 | msg_udp_mqtt_init(); 120 | msg_mqtt_udp_init(); 121 | #ifdef THREAD_MYSQL 122 | if (mysql_enable == 1) 123 | msg_mqtt_mysql_init(); 124 | #endif 125 | #ifdef RULES_ENGINE 126 | msg_trigger_rules_init(); 127 | msggap_rules_mqtt_init(); 128 | msg_rules_mqtt_init(); 129 | #endif 130 | 131 | if (thread_tcp_start() < 0) 132 | exit(EXIT_FAILURE); 133 | if (thread_udp_start() < 0) 134 | exit(EXIT_FAILURE); 135 | thread_mqtt_start(); 136 | #ifdef THREAD_MYSQL 137 | if (mysql_enable == 1) 138 | { 139 | if (thread_mysql_start() < 0) 140 | exit(EXIT_FAILURE); 141 | } 142 | #endif 143 | #ifdef RULES_ENGINE 144 | parse_json_file(); 145 | publish_rules_engine_version(); 146 | thread_rules_start(); 147 | thread_cron_start(); 148 | #endif 149 | 150 | #ifdef LINUX_DAEMON_VERSION 151 | while (!signal_exit) 152 | sleep(500); 153 | #else 154 | getchar(); 155 | #endif 156 | 157 | 158 | msg_tcp_mqtt_close(); 159 | msg_mqtt_tcp_close(); 160 | msg_udp_mqtt_close(); 161 | msg_mqtt_udp_close(); 162 | #ifdef THREAD_MYSQL 163 | if (mysql_enable == 1) 164 | msg_mqtt_mysql_close(); 165 | #endif 166 | #ifdef RULES_ENGINE 167 | msg_trigger_rules_close(); 168 | msggap_rules_mqtt_close(); 169 | msg_rules_mqtt_close(); 170 | #endif 171 | 172 | 173 | thread_udp_stop(); 174 | thread_tcp_stop(); 175 | thread_mqtt_stop(); 176 | #ifdef THREAD_MYSQL 177 | if (mysql_enable == 1) 178 | thread_mysql_stop(); 179 | #endif 180 | #ifdef RULES_ENGINE 181 | thread_cron_stop(); 182 | thread_rules_stop(); 183 | #endif 184 | 185 | 186 | msg_tcp_mqtt_destroy(); 187 | msg_udp_mqtt_destroy(); 188 | msg_mqtt_tcp_destroy(); 189 | msg_mqtt_udp_destroy(); 190 | #ifdef THREAD_MYSQL 191 | if (mysql_enable == 1) 192 | msg_mqtt_mysql_destroy(); 193 | #endif 194 | #ifdef RULES_ENGINE 195 | msg_trigger_rules_destroy(); 196 | msggap_rules_mqtt_destroy(); 197 | msg_rules_mqtt_destroy(); 198 | #endif 199 | 200 | 201 | #ifdef LINUX_DAEMON_VERSION 202 | syslog(LOG_NOTICE, "exit"); 203 | closelog(); 204 | #endif 205 | 206 | #ifdef WIN32 207 | WSACleanup(); 208 | #endif 209 | 210 | exit(EXIT_SUCCESS); 211 | } 212 | -------------------------------------------------------------------------------- /src/mqtt_trigger.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include /* sscanf */ 16 | #include /* memset, memcmp, memcpy */ 17 | #include /* malloc, strtol */ 18 | #include /* isdigit */ 19 | #include /* assert */ 20 | #include "mqtt_trigger.h" 21 | #include "os_port.h" 22 | 23 | #ifdef _MSC_VER 24 | #pragma warning (disable:4996) // This function may be unsafe. 25 | #endif 26 | 27 | 28 | 29 | //-------------------------------------------- 30 | mqtt_trigger_t *mqtt_trigger_add_new(mqtt_trigger_t **list, const char *name, uint32_t next_id) 31 | { 32 | mqtt_trigger_t *item; 33 | 34 | assert(list != NULL); 35 | assert(name != NULL); 36 | assert(next_id != 0); 37 | 38 | item = (mqtt_trigger_t *)malloc(sizeof(mqtt_trigger_t)); 39 | memset(item, 0, sizeof(mqtt_trigger_t)); 40 | item->name_len = (uint16_t)strlen(name); 41 | item->name = (uint8_t *)malloc(item->name_len); 42 | memcpy(item->name, name, item->name_len); 43 | item->next_id = next_id; 44 | list_add_item((list_t **)list, (list_t *)item); 45 | return item; 46 | } 47 | 48 | //-------------------------------------------- 49 | void mqtt_trigger_remove_all(mqtt_trigger_t **list) 50 | { 51 | mqtt_trigger_t *next; 52 | mqtt_trigger_t *item; 53 | 54 | assert(list != NULL); 55 | 56 | item = *list; 57 | while (item != NULL) 58 | { 59 | next = mqtt_trigger_next(item); 60 | free(item); 61 | item = next; 62 | } 63 | *list = NULL; 64 | } 65 | 66 | -------------------------------------------------------------------------------- /src/mqtt_trigger.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef MQTT_TRIGGER_H_ 16 | #define MQTT_TRIGGER_H_ 17 | 18 | #include "list.h" 19 | 20 | #ifdef _MSC_VER 21 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 22 | #else 23 | #include /* uint8_t ... uint64_t */ 24 | #include /* size_t */ 25 | #endif 26 | 27 | 28 | //-------------------------------------------- 29 | typedef struct mqtt_trigger 30 | { 31 | list_t next; 32 | uint8_t *name; 33 | uint16_t name_len; 34 | uint32_t next_id; 35 | } mqtt_trigger_t; 36 | 37 | mqtt_trigger_t *mqtt_trigger_add_new(mqtt_trigger_t **list, const char *name, uint32_t next_id); 38 | #define mqtt_trigger_head(a) (mqtt_trigger_t *)list_head((list_t **)a) 39 | #define mqtt_trigger_next(a) (mqtt_trigger_t *)list_next((list_t *)a) 40 | void mqtt_trigger_remove_all(mqtt_trigger_t **list); 41 | 42 | #endif /* MQTT_TRIGGER_H_ */ 43 | -------------------------------------------------------------------------------- /src/msg_mqtt_mysql.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015, 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef MSG_MQTT_MYSQL_H_ 16 | #define MSG_MQTT_MYSQL_H_ 17 | 18 | #include "msgs.h" 19 | #include /* time_t, time() */ 20 | 21 | #ifdef _MSC_VER 22 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 23 | #else 24 | #include /* uint8_t ... uint64_t */ 25 | #include /* size_t */ 26 | #endif 27 | 28 | //-------------------------------------------- 29 | typedef enum msg_mysql_type 30 | { 31 | MYSQL_ADD_LONG_DATA, 32 | MYSQL_ADD_FLOAT_DATA, 33 | MYSQL_ADD_UTF8STR_DATA, 34 | MYSQL_UPDATE_DEVICE_IP, 35 | MYSQL_UPDATE_SENSOR_PARAM_UNIT, 36 | MYSQL_UPDATE_ACTUATOR_PARAM_UNIT, 37 | MYSQL_UPDATE_SENSOR_PARAM_DATA_TYPE, 38 | MYSQL_UPDATE_ACTUATOR_PARAM_DATA_TYPE, 39 | MYSQL_UPDATE_DEVICE_TIMEOUT 40 | } msg_mysql_type_t; 41 | 42 | //-------------------------------------------- 43 | typedef struct msg_mysql_add_long_data 44 | { 45 | uint32_t id; 46 | uint32_t param; 47 | time_t timestamp; 48 | long long_data; 49 | } msg_mysql_add_long_data_t; 50 | 51 | typedef struct msg_mysql_add_float_data 52 | { 53 | uint32_t id; 54 | uint32_t param; 55 | time_t timestamp; 56 | float float_data; 57 | } msg_mysql_add_float_data_t; 58 | 59 | typedef struct msg_mysql_add_utf8str_data 60 | { 61 | uint32_t id; 62 | uint32_t param; 63 | time_t timestamp; 64 | char *utf8str_data; 65 | } msg_mysql_add_utf8str_data_t; 66 | 67 | typedef struct msg_mysql_add_param_utf8str 68 | { 69 | uint32_t id; 70 | uint32_t param; 71 | char *utf8str_data; 72 | } msg_mysql_add_param_utf8str_t; 73 | 74 | typedef struct msg_mysql_add_sensor_utf8str 75 | { 76 | uint32_t id; 77 | char *utf8str_data; 78 | } msg_mysql_add_sensor_utf8str_t; 79 | 80 | #define msg_mysql_add_actuator_utf8str_t msg_mysql_add_sensor_utf8str_t 81 | 82 | typedef struct msg_mysql_add_sensor_long 83 | { 84 | uint32_t id; 85 | long long_data; 86 | } msg_mysql_add_sensor_long_t; 87 | 88 | 89 | 90 | 91 | //-------------------------------------------- 92 | typedef struct msg_mqtt_mysql 93 | { 94 | msg_t msg; 95 | msg_mysql_type_t type; 96 | void *msg_mysql; 97 | } msg_mqtt_mysql_t; 98 | 99 | //-------------------------------------------- 100 | void msg_mqtt_mysql_init(void); 101 | msg_mqtt_mysql_t *msg_mqtt_mysql_new(void); 102 | void msg_mqtt_mysql_add(msg_mqtt_mysql_t *ms); 103 | void msg_mqtt_mysql_remove(msg_mqtt_mysql_t *ms); 104 | msg_mqtt_mysql_t *msg_mqtt_mysql_get_first(void); 105 | #define msg_mqtt_mysql_close() msg_close(a) 106 | void msg_mqtt_mysql_destroy(void); 107 | 108 | void msg_mqtt_mysql_add_long_data(uint32_t id, uint32_t param, long long_data); 109 | void msg_mqtt_mysql_add_float_data(uint32_t id, uint32_t param, float float_data); 110 | void msg_mqtt_mysql_add_utf8str_data(uint32_t id, uint32_t param, char *utf8str_data); 111 | void msg_mqtt_mysql_update_sensor_param_unit(uint32_t id, uint32_t param, char *utf8str_data); 112 | void msg_mqtt_mysql_update_actuator_param_unit(uint32_t id, uint32_t param, char *utf8str_data); 113 | void msg_mqtt_mysql_update_sensor_param_type(uint32_t id, uint32_t param, char *utf8str_data); 114 | void msg_mqtt_mysql_update_actuator_param_type(uint32_t id, uint32_t param, char *utf8str_data); 115 | void msg_mqtt_mysql_update_device_ip(uint32_t id, char *utf8str_data); 116 | void msg_mqtt_mysql_update_device_timeout(uint32_t id, long long_data); 117 | 118 | #endif /* MSG_MQTT_MYSQL_H_ */ 119 | -------------------------------------------------------------------------------- /src/msg_mqtt_tcp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "msg_mqtt_tcp.h" 16 | #include /* memset */ 17 | 18 | static msgqueue_t queue; 19 | 20 | //-------------------------------------------- 21 | void msg_mqtt_tcp_init(void) 22 | { 23 | msg_init(&queue); 24 | } 25 | 26 | //-------------------------------------------- 27 | msg_mqtt_tcp_t *msg_mqtt_tcp_new(void) 28 | { 29 | msg_mqtt_tcp_t *ms; 30 | ms = (msg_mqtt_tcp_t *)malloc(sizeof(msg_mqtt_tcp_t)); 31 | memset(ms, 0, sizeof(msg_mqtt_tcp_t)); 32 | return ms; 33 | } 34 | 35 | //-------------------------------------------- 36 | void msg_mqtt_tcp_add(msg_mqtt_tcp_t *ms) 37 | { 38 | msg_add(&queue, (msg_t *)ms); 39 | } 40 | 41 | //-------------------------------------------- 42 | void msg_mqtt_tcp_remove(msg_mqtt_tcp_t *ms) 43 | { 44 | msg_remove(&queue, (msg_t *)ms); 45 | if (ms->msg_buf != NULL) 46 | free(ms->msg_buf); 47 | free(ms); 48 | } 49 | 50 | //-------------------------------------------- 51 | msg_mqtt_tcp_t* msg_mqtt_tcp_get_first(void) 52 | { 53 | return (msg_mqtt_tcp_t *)msg_get_first(&queue); 54 | } 55 | 56 | //-------------------------------------------- 57 | void msg_mqtt_tcp_destroy(void) 58 | { 59 | msg_destroy(&queue); 60 | } 61 | 62 | //-------------------------------------------- 63 | void msg_mqtt_tcp_add_close_conn(struct sockaddr_in *addr) 64 | { 65 | msg_mqtt_tcp_t *ms = msg_mqtt_tcp_new(); 66 | memcpy(&ms->addr, addr, sizeof(struct sockaddr_in)); 67 | ms->close = 1; 68 | msg_mqtt_tcp_add(ms); 69 | } 70 | 71 | //-------------------------------------------- 72 | void msg_mqtt_tcp_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size) 73 | { 74 | msg_mqtt_tcp_t *ms = msg_mqtt_tcp_new(); 75 | memcpy(&ms->addr, addr, sizeof(struct sockaddr_in)); 76 | ms->msg_buf = buf; // external malloc 77 | ms->msg_cnt = size; 78 | msg_mqtt_tcp_add(ms); 79 | } 80 | -------------------------------------------------------------------------------- /src/msg_mqtt_tcp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef MSG_MQTT_TCP_H_ 16 | #define MSG_MQTT_TCP_H_ 17 | 18 | #include "msgs.h" 19 | 20 | //-------------------------------------------- 21 | typedef struct msg_mqtt_tcp 22 | { 23 | msg_t msg; 24 | unsigned char *msg_buf; 25 | size_t msg_cnt; 26 | struct sockaddr_in addr; 27 | unsigned char close; 28 | } msg_mqtt_tcp_t; 29 | 30 | //-------------------------------------------- 31 | void msg_mqtt_tcp_init(void); 32 | msg_mqtt_tcp_t *msg_mqtt_tcp_new(void); 33 | void msg_mqtt_tcp_add(msg_mqtt_tcp_t *ms); 34 | void msg_mqtt_tcp_remove(msg_mqtt_tcp_t *ms); 35 | msg_mqtt_tcp_t *msg_mqtt_tcp_get_first(void); 36 | #define msg_mqtt_tcp_close() msg_close(a) 37 | void msg_mqtt_tcp_destroy(void); 38 | 39 | void msg_mqtt_tcp_add_close_conn(struct sockaddr_in *addr); 40 | void msg_mqtt_tcp_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size); 41 | 42 | #endif /* MSG_MQTT_TCP_H_ */ 43 | -------------------------------------------------------------------------------- /src/msg_mqtt_udp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "msg_mqtt_udp.h" 16 | #include /* memset */ 17 | 18 | static msgqueue_t queue; 19 | 20 | //-------------------------------------------- 21 | void msg_mqtt_udp_init(void) 22 | { 23 | msg_init(&queue); 24 | } 25 | 26 | //-------------------------------------------- 27 | msg_mqtt_udp_t *msg_mqtt_udp_new(void) 28 | { 29 | msg_mqtt_udp_t *ms; 30 | ms = (msg_mqtt_udp_t *)malloc(sizeof(msg_mqtt_udp_t)); 31 | memset(ms, 0, sizeof(msg_mqtt_udp_t)); 32 | return ms; 33 | } 34 | 35 | //-------------------------------------------- 36 | void msg_mqtt_udp_add(msg_mqtt_udp_t *ms) 37 | { 38 | msg_add(&queue, (msg_t *)ms); 39 | } 40 | 41 | //-------------------------------------------- 42 | void msg_mqtt_udp_remove(msg_mqtt_udp_t *ms) 43 | { 44 | msg_remove(&queue, (msg_t *)ms); 45 | if (ms->msg_buf != NULL) 46 | free(ms->msg_buf); 47 | free(ms); 48 | } 49 | 50 | //-------------------------------------------- 51 | msg_mqtt_udp_t* msg_mqtt_udp_get_first(void) 52 | { 53 | return (msg_mqtt_udp_t *)msg_get_first(&queue); 54 | } 55 | 56 | //-------------------------------------------- 57 | void msg_mqtt_udp_destroy(void) 58 | { 59 | msg_destroy(&queue); 60 | } 61 | 62 | //-------------------------------------------- 63 | void msg_mqtt_udp_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size) 64 | { 65 | msg_mqtt_udp_t *ms = msg_mqtt_udp_new(); 66 | memcpy(&ms->addr, addr, sizeof(struct sockaddr_in)); 67 | ms->msg_buf = buf; // external malloc 68 | ms->msg_cnt = size; 69 | msg_mqtt_udp_add(ms); 70 | } 71 | -------------------------------------------------------------------------------- /src/msg_mqtt_udp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef MSG_MQTT_UDP_H_ 16 | #define MSG_MQTT_UDP_H_ 17 | 18 | #include "msgs.h" 19 | 20 | 21 | //-------------------------------------------- 22 | typedef struct msg_mqtt_udp 23 | { 24 | msg_t msg; 25 | unsigned char *msg_buf; 26 | size_t msg_cnt; 27 | struct sockaddr_in addr; 28 | } msg_mqtt_udp_t; 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | //-------------------------------------------- 34 | void msg_mqtt_udp_init(void); 35 | msg_mqtt_udp_t *msg_mqtt_udp_new(void); 36 | void msg_mqtt_udp_add(msg_mqtt_udp_t *ms); 37 | void msg_mqtt_udp_remove(msg_mqtt_udp_t *ms); 38 | msg_mqtt_udp_t *msg_mqtt_udp_get_first(void); 39 | #define msg_mqtt_udp_close() msg_close(a) 40 | void msg_mqtt_udp_destroy(void); 41 | 42 | void msg_mqtt_udp_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size); 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif /* MSG_MQTT_UDP_H_ */ 48 | -------------------------------------------------------------------------------- /src/msg_rules_mqtt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "msg_rules_mqtt.h" 16 | #include /* memset */ 17 | 18 | static msggap_t gap; 19 | static msgqueue_t queue; 20 | 21 | 22 | //-------------------------------------------- 23 | void msggap_rules_mqtt_init(void) 24 | { 25 | msggap_init(&gap); 26 | } 27 | 28 | //-------------------------------------------- 29 | void msggap_rules_mqtt_request(list_data_t *list) 30 | { 31 | gap.msg = (void *)list; 32 | msggap_request(&gap); 33 | } 34 | 35 | //-------------------------------------------- 36 | void msggap_rules_mqtt_clear_request(void) 37 | { 38 | gap.msg = NULL; 39 | } 40 | 41 | //-------------------------------------------- 42 | list_data_t *msggap_rules_mqtt_get_request(void) 43 | { 44 | if (msggap_get_request(&gap) == 1) 45 | return (list_data_t *)gap.msg; 46 | else 47 | return NULL; 48 | } 49 | 50 | //-------------------------------------------- 51 | void msggap_rules_mqtt_reply_request(void) 52 | { 53 | msggap_reply(&gap); 54 | } 55 | 56 | //-------------------------------------------- 57 | void msggap_rules_mqtt_close(void) 58 | { 59 | msggap_close(&gap); 60 | } 61 | 62 | //-------------------------------------------- 63 | void msggap_rules_mqtt_destroy(void) 64 | { 65 | msggap_destroy(&gap); 66 | } 67 | 68 | 69 | 70 | 71 | //-------------------------------------------- 72 | void msg_rules_mqtt_init(void) 73 | { 74 | msg_init(&queue); 75 | } 76 | 77 | //-------------------------------------------- 78 | msg_rules_mqtt_t *msg_rules_mqtt_new(void) 79 | { 80 | msg_rules_mqtt_t *msg; 81 | 82 | msg = (msg_rules_mqtt_t *)malloc(sizeof(msg_rules_mqtt_t)); 83 | memset(msg, 0, sizeof(msg_rules_mqtt_t)); 84 | return msg; 85 | } 86 | 87 | //-------------------------------------------- 88 | void msg_rules_mqtt_add(msg_rules_mqtt_t *msg) 89 | { 90 | msg_add(&queue, (msg_t *)msg); 91 | } 92 | 93 | //-------------------------------------------- 94 | void msg_rules_mqtt_remove(msg_rules_mqtt_t *msg) 95 | { 96 | msg_remove(&queue, (msg_t *)msg); 97 | list_data_remove_all(&msg->list); 98 | free(msg); 99 | } 100 | 101 | //-------------------------------------------- 102 | msg_rules_mqtt_t* msg_rules_mqtt_get_first(void) 103 | { 104 | return (msg_rules_mqtt_t *)msg_get_first(&queue); 105 | } 106 | 107 | //-------------------------------------------- 108 | void msg_rules_mqtt_destroy(void) 109 | { 110 | msg_destroy(&queue); 111 | } 112 | 113 | //-------------------------------------------- 114 | void msg_rules_mqtt_add_packet(list_data_t *list, uint8_t retain) 115 | { 116 | msg_rules_mqtt_t *msg = msg_rules_mqtt_new(); 117 | msg->list = list; 118 | msg->retain = retain; 119 | msg_rules_mqtt_add(msg); 120 | } 121 | -------------------------------------------------------------------------------- /src/msg_rules_mqtt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef MSG_RULES_MQTT_H_ 16 | #define MSG_RULES_MQTT_H_ 17 | 18 | #include "msgs.h" 19 | #include "lists.h" 20 | 21 | //-------------------------------------------- 22 | void msggap_rules_mqtt_init(void); 23 | void msggap_rules_mqtt_request(list_data_t *list); 24 | void msggap_rules_mqtt_clear_request(void); 25 | list_data_t *msggap_rules_mqtt_get_request(void); 26 | void msggap_rules_mqtt_reply_request(void); 27 | void msggap_rules_mqtt_close(void); 28 | void msggap_rules_mqtt_destroy(void); 29 | 30 | //-------------------------------------------- 31 | typedef struct msg_rules_mqtt 32 | { 33 | msg_t msg; 34 | list_data_t *list; 35 | uint8_t retain; 36 | } msg_rules_mqtt_t; 37 | 38 | //-------------------------------------------- 39 | void msg_rules_mqtt_init(void); 40 | msg_rules_mqtt_t *msg_rules_mqtt_new(void); 41 | void msg_rules_mqtt_add(msg_rules_mqtt_t *msg); 42 | void msg_rules_mqtt_remove(msg_rules_mqtt_t *msg); 43 | msg_rules_mqtt_t* msg_rules_mqtt_get_first(void); 44 | #define msg_rules_mqtt_close() msg_close(a) 45 | void msg_rules_mqtt_destroy(void); 46 | 47 | void msg_rules_mqtt_add_packet(list_data_t *list, uint8_t retain); 48 | 49 | #endif /* MSG_RULES_MQTT_H_ */ 50 | -------------------------------------------------------------------------------- /src/msg_tcp_mqtt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015, 2019 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "msg_tcp_mqtt.h" 16 | #include /* memset, memcpy */ 17 | 18 | static msgqueue_t queue; 19 | 20 | //-------------------------------------------- 21 | void msg_tcp_mqtt_init(void) 22 | { 23 | msg_init(&queue); 24 | } 25 | 26 | //-------------------------------------------- 27 | msg_tcp_mqtt_t *msg_tcp_mqtt_new(void) 28 | { 29 | msg_tcp_mqtt_t *ms; 30 | ms = (msg_tcp_mqtt_t *)malloc(sizeof(msg_tcp_mqtt_t)); 31 | memset(ms, 0, sizeof(msg_tcp_mqtt_t)); 32 | return ms; 33 | } 34 | 35 | //-------------------------------------------- 36 | void msg_tcp_mqtt_add(msg_tcp_mqtt_t *ms) 37 | { 38 | msg_add(&queue, (msg_t *)ms); 39 | } 40 | 41 | //-------------------------------------------- 42 | void msg_tcp_mqtt_remove(msg_tcp_mqtt_t *ms) 43 | { 44 | msg_remove(&queue, (msg_t *)ms); 45 | if (ms->msg_buf != NULL) 46 | free(ms->msg_buf); 47 | free(ms); 48 | } 49 | 50 | //-------------------------------------------- 51 | msg_tcp_mqtt_t *msg_tcp_mqtt_get_first(void) 52 | { 53 | return (msg_tcp_mqtt_t *)msg_get_first(&queue); 54 | } 55 | 56 | //-------------------------------------------- 57 | void msg_tcp_mqtt_destroy(void) 58 | { 59 | msg_destroy(&queue); 60 | } 61 | 62 | //-------------------------------------------- 63 | void msg_tcp_mqtt_add_close_conn(struct sockaddr_in *addr) 64 | { 65 | msg_tcp_mqtt_t *ms = msg_tcp_mqtt_new(); 66 | memcpy(&ms->addr, addr, sizeof(struct sockaddr_in)); 67 | ms->close = 1; 68 | msg_tcp_mqtt_add(ms); 69 | } 70 | 71 | //-------------------------------------------- 72 | void msg_tcp_mqtt_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size) 73 | { 74 | msg_tcp_mqtt_t *ms = msg_tcp_mqtt_new(); 75 | memcpy(&ms->addr, addr, sizeof(struct sockaddr_in)); 76 | ms->msg_buf = (unsigned char *)malloc(size); 77 | ms->msg_cnt = size; 78 | ms->proc_msg_cnt = 0; 79 | memcpy(ms->msg_buf, buf, size); 80 | msg_tcp_mqtt_add(ms); 81 | } 82 | -------------------------------------------------------------------------------- /src/msg_tcp_mqtt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015, 2019 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef MSG_TCP_MQTT_H_ 16 | #define MSG_TCP_MQTT_H_ 17 | 18 | #include "msgs.h" 19 | 20 | //-------------------------------------------- 21 | typedef struct msg_tcp_mqtt 22 | { 23 | msg_t msg; 24 | char *msg_buf; 25 | size_t msg_cnt; 26 | size_t proc_msg_cnt; 27 | struct sockaddr_in addr; 28 | unsigned char close; 29 | } msg_tcp_mqtt_t; 30 | 31 | //-------------------------------------------- 32 | void msg_tcp_mqtt_init(void); 33 | msg_tcp_mqtt_t *msg_tcp_mqtt_new(void); 34 | void msg_tcp_mqtt_add(msg_tcp_mqtt_t *ms); 35 | void msg_tcp_mqtt_remove(msg_tcp_mqtt_t *ms); 36 | msg_tcp_mqtt_t *msg_tcp_mqtt_get_first(void); 37 | #define msg_tcp_mqtt_close() msg_close(a) 38 | void msg_tcp_mqtt_destroy(void); 39 | 40 | void msg_tcp_mqtt_add_close_conn(struct sockaddr_in *addr); 41 | void msg_tcp_mqtt_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size); 42 | 43 | #endif /* MSG_TCP_MQTT_H_ */ 44 | -------------------------------------------------------------------------------- /src/msg_trigger_rules.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "msg_trigger_rules.h" 16 | #include /* memset */ 17 | 18 | static msgqueue_t queue; 19 | 20 | //-------------------------------------------- 21 | void msg_trigger_rules_init(void) 22 | { 23 | msg_init(&queue); 24 | } 25 | 26 | //-------------------------------------------- 27 | msg_trigger_rules_t *msg_trigger_rules_new(void) 28 | { 29 | msg_trigger_rules_t *msg; 30 | 31 | msg = (msg_trigger_rules_t *)malloc(sizeof(msg_trigger_rules_t)); 32 | memset(msg, 0, sizeof(msg_trigger_rules_t)); 33 | return msg; 34 | } 35 | 36 | //-------------------------------------------- 37 | void msg_trigger_rules_add(msg_trigger_rules_t *msg) 38 | { 39 | msg_add(&queue, (msg_t *)msg); 40 | } 41 | 42 | //-------------------------------------------- 43 | void msg_trigger_rules_remove(msg_trigger_rules_t *msg) 44 | { 45 | msg_remove(&queue, (msg_t *)msg); 46 | free(msg); 47 | } 48 | 49 | //-------------------------------------------- 50 | msg_trigger_rules_t* msg_trigger_rules_get_first(void) 51 | { 52 | return (msg_trigger_rules_t *)msg_get_first(&queue); 53 | } 54 | 55 | //-------------------------------------------- 56 | void msg_trigger_rules_destroy(void) 57 | { 58 | msg_destroy(&queue); 59 | } 60 | 61 | //-------------------------------------------- 62 | void msg_cron_rules_add_packet(cron_trigger_t *cron_trigger) 63 | { 64 | msg_trigger_rules_t *msg = msg_trigger_rules_new(); 65 | msg->next_id = cron_trigger->next_id; 66 | msg_trigger_rules_add(msg); 67 | } 68 | 69 | //-------------------------------------------- 70 | void msg_mqtt_rules_add_packet(mqtt_trigger_t *mqtt_trigger) 71 | { 72 | msg_trigger_rules_t *msg = msg_trigger_rules_new(); 73 | msg->next_id = mqtt_trigger->next_id; 74 | msg_trigger_rules_add(msg); 75 | } 76 | -------------------------------------------------------------------------------- /src/msg_trigger_rules.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef MSG_TRIGGER_RULES_H_ 16 | #define MSG_TRIGGER_RULES_H_ 17 | 18 | #include "msgs.h" 19 | #include "cron_trigger.h" 20 | #include "mqtt_trigger.h" 21 | 22 | #ifdef _MSC_VER 23 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 24 | #else 25 | #include /* uint8_t ... uint64_t */ 26 | #include /* size_t */ 27 | #endif 28 | 29 | //-------------------------------------------- 30 | typedef struct msg_trigger_rules 31 | { 32 | msg_t msg; 33 | uint32_t next_id; 34 | } msg_trigger_rules_t; 35 | 36 | //-------------------------------------------- 37 | void msg_trigger_rules_init(void); 38 | msg_trigger_rules_t *msg_trigger_rules_new(void); 39 | void msg_trigger_rules_add(msg_trigger_rules_t *msg); 40 | void msg_trigger_rules_remove(msg_trigger_rules_t *msg); 41 | msg_trigger_rules_t* msg_trigger_rules_get_first(void); 42 | #define msg_trigger_rules_close() msg_close(a) 43 | void msg_trigger_rules_destroy(void); 44 | 45 | void msg_cron_rules_add_packet(cron_trigger_t *cron_trigger); 46 | void msg_mqtt_rules_add_packet(mqtt_trigger_t *mqtt_trigger); 47 | 48 | #endif /* MSG_TRIGGER_RULES_H_ */ 49 | -------------------------------------------------------------------------------- /src/msg_udp_mqtt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "msg_udp_mqtt.h" 16 | #include /* memset, memcpy */ 17 | 18 | static msgqueue_t queue; 19 | 20 | //-------------------------------------------- 21 | void msg_udp_mqtt_init(void) 22 | { 23 | msg_init(&queue); 24 | } 25 | 26 | //-------------------------------------------- 27 | msg_udp_mqtt_t *msg_udp_mqtt_new(void) 28 | { 29 | msg_udp_mqtt_t *msg; 30 | 31 | msg = (msg_udp_mqtt_t *)malloc(sizeof(msg_udp_mqtt_t)); 32 | memset(msg, 0, sizeof(msg_udp_mqtt_t)); 33 | return msg; 34 | } 35 | 36 | //-------------------------------------------- 37 | void msg_udp_mqtt_add(msg_udp_mqtt_t *msg) 38 | { 39 | msg_add(&queue, (msg_t *)msg); 40 | } 41 | 42 | //-------------------------------------------- 43 | void msg_udp_mqtt_remove(msg_udp_mqtt_t *msg) 44 | { 45 | msg_remove(&queue, (msg_t *)msg); 46 | if (msg->msg_buf != NULL) 47 | free(msg->msg_buf); 48 | free(msg); 49 | } 50 | 51 | //-------------------------------------------- 52 | msg_udp_mqtt_t *msg_udp_mqtt_get_first(void) 53 | { 54 | return (msg_udp_mqtt_t *)msg_get_first(&queue); 55 | } 56 | 57 | //-------------------------------------------- 58 | void msg_udp_mqtt_destroy(void) 59 | { 60 | msg_destroy(&queue); 61 | } 62 | 63 | //-------------------------------------------- 64 | void msg_udp_mqtt_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size) 65 | { 66 | msg_udp_mqtt_t *msg; 67 | 68 | msg = msg_udp_mqtt_new(); 69 | memcpy(&msg->addr, addr, sizeof(struct sockaddr_in)); 70 | msg->msg_buf = (unsigned char *)malloc(size); 71 | msg->msg_cnt = size; 72 | memcpy(msg->msg_buf, buf, size); 73 | msg_udp_mqtt_add(msg); 74 | } 75 | -------------------------------------------------------------------------------- /src/msg_udp_mqtt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef MSG_UDP_MQTT_H_ 16 | #define MSG_UDP_MQTT_H_ 17 | 18 | #include "msgs.h" 19 | 20 | 21 | //-------------------------------------------- 22 | typedef struct msg_udp_mqtt 23 | { 24 | msg_t msg; 25 | char *msg_buf; 26 | size_t msg_cnt; 27 | struct sockaddr_in addr; 28 | } msg_udp_mqtt_t; 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | //-------------------------------------------- 34 | void msg_udp_mqtt_init(void); 35 | msg_udp_mqtt_t *msg_udp_mqtt_new(void); 36 | void msg_udp_mqtt_add(msg_udp_mqtt_t *msg); 37 | void msg_udp_mqtt_remove(msg_udp_mqtt_t *msg); 38 | msg_udp_mqtt_t *msg_udp_mqtt_get_first(void); 39 | #define msg_udp_mqtt_close() msg_close(a) 40 | void msg_udp_mqtt_destroy(void); 41 | 42 | void msg_udp_mqtt_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size); 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif /* MSG_UDP_MQTT_H_ */ 48 | -------------------------------------------------------------------------------- /src/msgs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef MSGS_H_ 16 | #define MSGS_H_ 17 | 18 | #include "os_port.h" 19 | 20 | //-------------------------------------------- 21 | typedef struct msg 22 | { 23 | struct msg *next; 24 | } msg_t; 25 | 26 | 27 | //-------------------------------------------- 28 | typedef struct msgqueue 29 | { 30 | msg_t *msglist; 31 | pthread_mutex_t mutex; 32 | } msgqueue_t; 33 | 34 | void msg_init(msgqueue_t *queue); 35 | void msg_add(msgqueue_t *queue, msg_t *msg); 36 | void msg_remove(msgqueue_t *queue, msg_t *msg); 37 | msg_t* msg_get_first(msgqueue_t *queue); 38 | #define msg_close(a) 39 | void msg_destroy(msgqueue_t *queue); 40 | 41 | 42 | //-------------------------------------------- 43 | typedef struct msgqueue_cond 44 | { 45 | msgqueue_t msgqueue; 46 | pthread_cond_t cond; 47 | } msgqueue_cond_t; 48 | 49 | void msg_cond_init(msgqueue_cond_t *queue); 50 | void msg_cond_add(msgqueue_cond_t *queue, msg_t *msg); 51 | void msg_cond_remove(msgqueue_cond_t *queue, msg_t *msg); 52 | msg_t* msg_cond_get_first(msgqueue_cond_t *queue); 53 | void msg_cond_close(msgqueue_cond_t *queue); 54 | void msg_cond_destroy(msgqueue_cond_t *queue); 55 | 56 | 57 | //-------------------------------------------- 58 | typedef struct msggap 59 | { 60 | void *msg; 61 | int request; 62 | pthread_mutex_t mutex; 63 | pthread_cond_t cond; 64 | } msggap_t; 65 | 66 | void msggap_init(msggap_t *gap); 67 | void msggap_request(msggap_t *gap); 68 | int msggap_get_request(msggap_t *gap); 69 | void msggap_reply(msggap_t *gap); 70 | void msggap_close(msggap_t *gap); 71 | void msggap_destroy(msggap_t *gap); 72 | 73 | #endif /* MSGS_H_ */ 74 | -------------------------------------------------------------------------------- /src/parse_conf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef PARSE_CONF_H_ 16 | #define PARSE_CONF_H_ 17 | 18 | #include "config.h" 19 | 20 | // .conf file 21 | #define MAX_OPTIONS 20 22 | #define MAX_CONF_NAME_SIZE 32 23 | #define MAX_CONF_VALUE_SIZE 64 24 | #define MAX_CONF_FILE_LINE_SIZE MAX_CONF_NAME_SIZE + MAX_CONF_VALUE_SIZE + 10 25 | 26 | #ifdef THREAD_MYSQL 27 | //-------------------------------------------- 28 | extern long mysql_enable; 29 | #endif 30 | 31 | //-------------------------------------------- 32 | int parse_conf_file(void); 33 | 34 | #endif /* PARSE_CONF_H_ */ 35 | -------------------------------------------------------------------------------- /src/parse_json.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef PARSE_JSON_H_ 16 | #define PARSE_JSON_H_ 17 | 18 | #include "config.h" 19 | 20 | //-------------------------------------------- 21 | void get_variable_topic(char *var, size_t var_len, char **name, size_t *name_len); 22 | int parse_json_file(void); 23 | void publish_rules_engine_version(void); 24 | 25 | #endif /* PARSE_JSON_H_ */ 26 | -------------------------------------------------------------------------------- /src/rules.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef RULES_H_ 16 | #define RULES_H_ 17 | 18 | #include "list.h" 19 | 20 | 21 | #ifdef _MSC_VER 22 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 23 | #else 24 | #include /* uint8_t ... uint64_t */ 25 | #include /* size_t */ 26 | #endif 27 | 28 | #define RULES_VERSION "1.00" 29 | #define RULES_VERSION_STRING_TOKENS 2 30 | 31 | #define RT_TRIGGER_CRON "trigger-cron" 32 | #define RT_TRIGGER_TOPIC "trigger-topic" 33 | #define RT_CONDITION_TOPIC_VALUE "condition-topic-value" 34 | #define RT_CONDITION_TOPIC_TOPIC "condition-topic-topic" 35 | #define RT_CONDITION_TOPIC_VARIABLE "condition-topic-variable" 36 | #define RT_CONDITION_VARIABLE_VALUE "condition-variable-value" 37 | #define RT_CONDITION_VARIABLE_VARIABLE "condition-variable-variable" 38 | #define RT_ACTION_VALUE "action-value" 39 | #define RT_ACTION_TOPIC "action-topic" 40 | #define RT_ACTION_VARIABLE "action-variable" 41 | #define RT_VARIABLE_INIT "variable-init" 42 | #define RT_VARIABLE_SET "variable-set" 43 | #define RT_VARIABLE_INCREMENT "variable-increment" 44 | #define RT_VARIABLE_DECREMENT "variable-decrement" 45 | 46 | typedef enum rf_type 47 | { 48 | RF_NONE = 0, 49 | RF_CONDITION_TOPIC_VALUE, 50 | RF_CONDITION_TOPIC_TOPIC, 51 | RF_CONDITION_TOPIC_VARIABLE, 52 | RF_CONDITION_VARIABLE_VALUE, 53 | RF_CONDITION_VARIABLE_VARIABLE, 54 | RF_ACTION_VALUE, 55 | RF_ACTION_TOPIC, 56 | RF_ACTION_VARIABLE, 57 | RF_VARIABLE_SET, 58 | RF_VARIABLE_INCREMENT, 59 | RF_VARIABLE_DECREMENT 60 | } rf_type_t; 61 | 62 | typedef struct rf_condition_param 63 | { 64 | uint32_t nextid_true; 65 | uint32_t nextid_false; 66 | uint8_t *name; 67 | uint16_t name_len; 68 | char *condition; 69 | uint8_t *str; 70 | uint16_t str_len; 71 | } rf_condition_param_t; 72 | 73 | typedef struct rf_action_param 74 | { 75 | uint32_t nextid; 76 | uint8_t *name; 77 | uint16_t name_len; 78 | uint8_t *str; 79 | uint16_t str_len; 80 | uint8_t retain; 81 | } rf_action_param_t; 82 | 83 | #define rf_variable_param_t rf_action_param_t 84 | 85 | typedef struct rules_function 86 | { 87 | list_t next; 88 | rf_type_t type; 89 | uint32_t (*func)(void *param); 90 | } rules_function_t; 91 | 92 | typedef struct rules_node 93 | { 94 | list_t next; 95 | uint32_t id; 96 | rf_type_t type; // function type 97 | void *param; // function parameter 98 | } rules_node_t; 99 | 100 | rules_function_t *rules_function_add(rules_function_t **list, rf_type_t type, uint32_t (*func)(void *param)); 101 | #define rules_function_head(a) (rules_function_t *)list_head((list_t **)a) 102 | #define rules_function_next(a) (rules_function_t *)list_next((list_t *)a) 103 | void rules_function_remove_all(rules_function_t **list); 104 | 105 | rules_node_t *rules_node_add(rules_node_t **list, uint32_t id, rf_type_t type, void *param); 106 | #define rules_node_head(a) (rules_node_t *)list_head((list_t **)a) 107 | #define rules_node_next(a) (rules_node_t *)list_next((list_t *)a) 108 | void rules_node_remove_all(rules_node_t **list); 109 | 110 | void rules_functions_init(rules_function_t **rfs); 111 | uint32_t func_execute(rules_function_t **rfs, rf_type_t type, void *param); 112 | void node_execute(rules_function_t **rfs, rules_node_t **rns, uint32_t id); 113 | 114 | #endif /* RULES_H_ */ 115 | -------------------------------------------------------------------------------- /src/sensor_data.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homewsn/whsnbg/7767d3c72b03299286a851e4e8e59e9709a5485c/src/sensor_data.c -------------------------------------------------------------------------------- /src/sensor_data.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef SENSOR_DATA_H_ 16 | #define SENSOR_DATA_H_ 17 | 18 | #ifdef _MSC_VER 19 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 20 | #else 21 | #include /* uint8_t ... uint64_t */ 22 | #include /* size_t */ 23 | #endif 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | //-------------------------------------------- 29 | void encode_mqttsn_sensor_data(uint8_t **buf, size_t *size, const char *data, size_t data_len); 30 | void decode_mqttsn_sensor_data(char **buf, size_t *size, uint8_t *data); 31 | void parse_mqttsn_topic_name_to_mysql_query(char *name, size_t name_len, uint8_t *data); 32 | void parse_mqtt_topic_name_to_mysql_query(char *name, size_t name_len, char *data, size_t data_len); 33 | int check_for_actuators_id_topic(char *name, size_t name_len); 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | 38 | #endif /* SENSOR_DATA_H_ */ 39 | -------------------------------------------------------------------------------- /src/thread_cron.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "cron_trigger.h" 16 | #include "os_port.h" 17 | #include "msg_trigger_rules.h" 18 | #include "thread_state.h" 19 | 20 | //-------------------------------------------- 21 | static cron_trigger_t *cron_triggers = NULL; 22 | static volatile thread_state_t thread_state; 23 | 24 | 25 | //-------------------------------------------- 26 | static unsigned int sleep_500ms(void) 27 | { 28 | size_t cnt; 29 | 30 | for (cnt = 0; cnt < 5; cnt++) 31 | { 32 | sleep(100); 33 | if (thread_state == THREAD_STAYING) 34 | return 1; 35 | } 36 | return 0; 37 | } 38 | 39 | 40 | //-------------------------------------------- 41 | //** main thread 42 | 43 | //-------------------------------------------- 44 | static void thread_run(void *param) 45 | { 46 | cron_trigger_t *item; 47 | 48 | for (;;) 49 | { 50 | if (time(NULL) % 60 == 0) 51 | { 52 | item = cron_trigger_head(&cron_triggers); 53 | while (item != NULL) 54 | { 55 | if (flag_starting_jobs(&item->cl) == 1) 56 | msg_cron_rules_add_packet(item); 57 | item = cron_trigger_next(item); 58 | } 59 | if (sleep_500ms() == 1) 60 | break; 61 | } 62 | if (sleep_500ms() == 1) 63 | break; 64 | } 65 | 66 | cron_trigger_remove_all(&cron_triggers); 67 | thread_state = THREAD_STOPPED; 68 | } 69 | 70 | //-------------------------------------------- 71 | #ifdef WIN32 72 | static unsigned int __stdcall thread_launcher(void *param) 73 | { 74 | thread_run(param); 75 | return 0; 76 | } 77 | #else 78 | static void *thread_launcher(void *param) 79 | { 80 | thread_run(param); 81 | return NULL; 82 | } 83 | #endif 84 | 85 | //-------------------------------------------- 86 | void thread_cron_start(void) 87 | { 88 | pthread_t thread; 89 | void *param = NULL; 90 | 91 | thread_state = THREAD_RUNNING; 92 | thread_begin(thread_launcher, param, &thread); 93 | } 94 | 95 | //-------------------------------------------- 96 | void thread_cron_stop(void) 97 | { 98 | if (thread_state == THREAD_RUNNING) 99 | thread_state = THREAD_STAYING; 100 | while (thread_state != THREAD_STOPPED) 101 | sleep(10); 102 | } 103 | 104 | //-------------------------------------------- 105 | void thread_cron_trigger_add(const char *cronstr, uint32_t next_id) 106 | { 107 | cron_trigger_add_new(&cron_triggers, cronstr, next_id); 108 | } 109 | 110 | //-------------------------------------------- 111 | void thread_cron_remove_all(void) 112 | { 113 | cron_trigger_remove_all(&cron_triggers); 114 | } 115 | -------------------------------------------------------------------------------- /src/thread_cron.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef THREAD_CRON_H_ 16 | #define THREAD_CRON_H_ 17 | 18 | void thread_cron_start(void); 19 | void thread_cron_stop(void); 20 | void thread_cron_trigger_add(const char *cronstr, uint32_t next_id); 21 | void thread_cron_remove_all(void); 22 | 23 | #endif /* THREAD_CRON_H_ */ 24 | -------------------------------------------------------------------------------- /src/thread_mqtt.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homewsn/whsnbg/7767d3c72b03299286a851e4e8e59e9709a5485c/src/thread_mqtt.c -------------------------------------------------------------------------------- /src/thread_mqtt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef THREAD_MQTT_H_ 16 | #define THREAD_MQTT_H_ 17 | 18 | #include "lists.h" 19 | 20 | #ifdef _MSC_VER 21 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 22 | #else 23 | #include /* uint8_t ... uint64_t */ 24 | #include /* size_t */ 25 | #endif 26 | 27 | //-------------------------------------------- 28 | void thread_mqtt_start(void); 29 | void thread_mqtt_stop(void); 30 | void thread_mqtt_user_add(const char *user_name, const char *password, unsigned char publish_enable); 31 | void thread_mqtt_trigger_add(const char *name, uint32_t next_id); 32 | void thread_mqtt_trigger_remove_all(void); 33 | void thread_mqtt_set_rules_topic_data(const char *data, size_t data_len); 34 | 35 | #endif /* THREAD_MQTT_H_ */ 36 | -------------------------------------------------------------------------------- /src/thread_mysql.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef THREAD_MYSQL_H_ 16 | #define THREAD_MYSQL_H_ 17 | 18 | #include "parse_conf.h" 19 | 20 | typedef struct thread_mysql_options 21 | { 22 | char mysql_server[MAX_CONF_VALUE_SIZE + 1]; 23 | char mysql_user[MAX_CONF_VALUE_SIZE + 1]; 24 | char mysql_password[MAX_CONF_VALUE_SIZE + 1]; 25 | char mysql_database[MAX_CONF_VALUE_SIZE + 1]; 26 | long mysql_port; 27 | } thread_mysql_options_t; 28 | 29 | int thread_mysql_start(void); 30 | void thread_mysql_stop(void); 31 | void thread_mysql_setup(thread_mysql_options_t *options); 32 | 33 | #endif /* THREAD_MYSQL_H_ */ 34 | -------------------------------------------------------------------------------- /src/thread_rules.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "rules.h" 16 | #include "os_port.h" 17 | #include "msg_trigger_rules.h" 18 | #include "thread_state.h" 19 | 20 | 21 | //-------------------------------------------- 22 | static rules_function_t *rules_functions = NULL; 23 | static rules_node_t *rules_nodes = NULL; 24 | static volatile thread_state_t thread_state; 25 | 26 | 27 | //-------------------------------------------- 28 | static void msg_trigger_rules_handle(msg_trigger_rules_t *ms) 29 | { 30 | node_execute(&rules_functions, &rules_nodes, ms->next_id); 31 | } 32 | 33 | 34 | //-------------------------------------------- 35 | //** main thread 36 | 37 | //-------------------------------------------- 38 | static void thread_run(void *param) 39 | { 40 | for (;;) 41 | { 42 | msg_trigger_rules_t *msg; 43 | 44 | if ((msg = msg_trigger_rules_get_first()) != NULL) 45 | { 46 | msg_trigger_rules_handle(msg); 47 | msg_trigger_rules_remove(msg); 48 | } 49 | if (thread_state == THREAD_STAYING) 50 | break; 51 | sleep(10); 52 | } 53 | 54 | rules_function_remove_all(&rules_functions); 55 | rules_node_remove_all(&rules_nodes); 56 | thread_state = THREAD_STOPPED; 57 | } 58 | 59 | //-------------------------------------------- 60 | #ifdef WIN32 61 | static unsigned int __stdcall thread_launcher(void *param) 62 | { 63 | thread_run(param); 64 | return 0; 65 | } 66 | #else 67 | static void *thread_launcher(void *param) 68 | { 69 | thread_run(param); 70 | return NULL; 71 | } 72 | #endif 73 | 74 | //-------------------------------------------- 75 | void thread_rules_start(void) 76 | { 77 | pthread_t thread; 78 | void *param = NULL; 79 | 80 | rules_functions_init(&rules_functions); 81 | 82 | thread_state = THREAD_RUNNING; 83 | thread_begin(thread_launcher, param, &thread); 84 | } 85 | 86 | //-------------------------------------------- 87 | void thread_rules_stop(void) 88 | { 89 | if (thread_state == THREAD_RUNNING) 90 | thread_state = THREAD_STAYING; 91 | while (thread_state != THREAD_STOPPED) 92 | sleep(10); 93 | } 94 | 95 | //-------------------------------------------- 96 | void thread_rules_add_node(uint32_t id, rf_type_t type, void *param) 97 | { 98 | rules_node_add(&rules_nodes, id, type, param); 99 | } 100 | 101 | //-------------------------------------------- 102 | void thread_rules_remove_all(void) 103 | { 104 | rules_node_remove_all(&rules_nodes); 105 | } 106 | -------------------------------------------------------------------------------- /src/thread_rules.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef THREAD_RULES_H_ 16 | #define THREAD_RULES_H_ 17 | 18 | #include "rules.h" 19 | 20 | void thread_rules_start(void); 21 | void thread_rules_stop(void); 22 | void thread_rules_add_node(uint32_t id, rf_type_t type, void *param); 23 | void thread_rules_remove_all(void); 24 | 25 | #endif /* THREAD_RULES_H_ */ 26 | -------------------------------------------------------------------------------- /src/thread_state.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef THREAD_STATE_H_ 16 | #define THREAD_STATE_H_ 17 | 18 | //-------------------------------------------- 19 | typedef enum 20 | { 21 | THREAD_RUNNING, 22 | THREAD_STAYING, 23 | THREAD_STOPPED 24 | } thread_state_t; 25 | 26 | 27 | #endif /* THREAD_STATE_H_ */ 28 | -------------------------------------------------------------------------------- /src/thread_tcp.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homewsn/whsnbg/7767d3c72b03299286a851e4e8e59e9709a5485c/src/thread_tcp.c -------------------------------------------------------------------------------- /src/thread_tcp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef THREAD_TCP_H_ 16 | #define THREAD_TCP_H_ 17 | 18 | #include "parse_conf.h" 19 | 20 | typedef struct thread_tcp_options 21 | { 22 | long mqtt_port; 23 | long mqtt_tls_port; 24 | long mqtt_ws_port; 25 | long mqtt_ws_tls_port; 26 | char mqtt_iface[MAX_CONF_VALUE_SIZE + 1]; 27 | } thread_tcp_options_t; 28 | 29 | int thread_tcp_start(void); 30 | void thread_tcp_stop(void); 31 | void thread_tcp_serv_setup(thread_tcp_options_t *options); 32 | 33 | #endif /* THREAD_TCP_H_ */ 34 | -------------------------------------------------------------------------------- /src/thread_udp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef THREAD_UDP_H_ 16 | #define THREAD_UDP_H_ 17 | 18 | #include "parse_conf.h" 19 | 20 | typedef struct thread_udp_options 21 | { 22 | long mqttsn_port; 23 | char mqttsn_iface[MAX_CONF_VALUE_SIZE + 1]; 24 | } thread_udp_options_t; 25 | 26 | int thread_udp_start(void); 27 | void thread_udp_stop(void); 28 | void thread_udp_serv_setup(thread_udp_options_t *options); 29 | 30 | #endif /* THREAD_UDP_H_ */ 31 | -------------------------------------------------------------------------------- /src/utf8.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "utf8.h" 16 | 17 | //-------------------------------------------- 18 | uint32_t utf8_strlen(char *utf8str, uint32_t size) 19 | { 20 | uint32_t byte_cnt, char_cnt; 21 | for (byte_cnt = 0, char_cnt = 0; byte_cnt < size; byte_cnt++) 22 | { 23 | if ((utf8str[byte_cnt] & 0xc0) != 0x80) 24 | char_cnt++; 25 | } 26 | return char_cnt; 27 | } 28 | -------------------------------------------------------------------------------- /src/utf8.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef UTF8_H_ 16 | #define UTF8_H_ 17 | 18 | #ifdef _MSC_VER 19 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 20 | #else 21 | #include /* uint8_t ... uint64_t */ 22 | #include /* size_t */ 23 | #endif 24 | 25 | uint32_t utf8_strlen(char *utf8str, uint32_t size); 26 | 27 | #endif /* UTF8_H_ */ 28 | -------------------------------------------------------------------------------- /utils/README.md: -------------------------------------------------------------------------------- 1 | ### Sensor simulator (sensim) and sensor simulator with GUI (sensimgui) 2 | 3 | Sensor simulator (sensim) and sensor simulator with GUI (sensimgui) are designed fot testing MQTT (tcp) and MQTT-SN (udp) network command interfaces of whsnbg. 4 | 5 | #### Sensim 6 | 7 | Sensim is a console application for automated testing. Allows you to select a test suite that simulates the connection of the sensor or actuator by the mqtt-sn protocol, while commands from the "control panel" are sent via the mqtt protocol. 8 | 9 | ##### Building and testing (Linux): 10 | 11 | Download [the latest release](https://github.com/homewsn/whsnbg/releases) in tarball from github and untar it: 12 | ```sh 13 | $ curl -L https://github.com/homewsn/whsnbg/archive/v1.4.tar.gz | tar zx 14 | $ cd whsnbg-1.4 15 | ``` 16 | Or you can also clone whsnbg repository: 17 | ```sh 18 | $ git clone https://github.com/homewsn/whsnbg.git 19 | $ cd whsnbg 20 | ``` 21 | Then build whsnbg with `SENSOR_DATA_DECODING=1` option: 22 | ```sh 23 | $ make SENSOR_DATA_DECODING=1 24 | $ sudo make install 25 | ``` 26 | Configure whsnbg: 27 | ```sh 28 | $ sudo nano /usr/local/etc/whsnbg.conf 29 | ``` 30 | Change mqtt_iface from `eth0` to `lo`. 31 | Change mqttsn_iface from `eth0` to `lo`. 32 | 33 | Build sensim: 34 | ```sh 35 | $ cd utils/sensim 36 | $ make 37 | ``` 38 | 39 | Open first terminal window and run whsnbg: 40 | ```sh 41 | $ whsnbg 42 | ``` 43 | Please note: 44 | * whsnbg must be built with `SENSOR_DATA_DECODING=1` option. 45 | * whsnbg must be restarted each time before running sensim. 46 | 47 | Then open second terminal window and run sensim: 48 | ```sh 49 | $ ./sensim -a 127.0.0.1 -p 1883 -s 1 50 | ``` 51 | where: 52 | * `127.0.0.1` is an IP address of the network interface where whsnbg is ran. 53 | * `1883` is a number of mqtt(tcp) and mqq-sn(udp) ports (must be the same). 54 | * `1` is a test suite number (must be between 1 to 3). 1 - general connection test suite, 2 - sleep sensor test suite, 3 - active actuator test suite. 55 | 56 | ##### Building and testing (Windows): 57 | 58 | Download [the latest release](https://github.com/homewsn/whsnbg/releases) in zip from github and unzip it. Open the the MSVS 2010 solution and build sensim. 59 | 60 | Open first command prompt window and run whsnbg. 61 | ```sh 62 | > whsnbg 63 | ``` 64 | Please note: 65 | * whsnbg must be built with `#define SENSOR_DATA` in the config.h file. 66 | * whsnbg must be restarted each time before running sensim. 67 | 68 | Then open next command prompt window and run sensim: 69 | ```sh 70 | > sensim -a 127.0.0.1 -p 1883 -s 1 71 | ``` 72 | where: 73 | * `127.0.0.1` is an IP address of the network interface where whsnbg is ran. 74 | * `1883` is a number of mqtt(tcp) and mqq-sn(udp) ports (must be the same). 75 | * `1` is a test suite number (must be between 1 to 3). 1 - general connection test suite, 2 - sleep sensor test suite, 3 - active actuator test suite. 76 | 77 | 78 | #### Sensimgui 79 | 80 | Sensimgui is a Windows desktop application for manual testing of whsnbg. The application allows to send and receive mqtt-sn messages to/from whsnbg individually. 81 | 82 | ##### Building and testing (Windows): 83 | 84 | Download [the latest release](https://github.com/homewsn/whsnbg/releases) in zip from github and unzip it. Open the the MSVS 2010 solution and build sensimgui. 85 | 86 | Open command prompt window and run whsnbg. 87 | ```sh 88 | > whsnbg.exe 89 | ``` 90 | Please note: 91 | * whsnbg must be built with `#define SENSOR_DATA` in the config.h file. 92 | 93 | Then run sensimgui.exe and choose the command you want to send whsnbg. 94 | 95 | Dependences: 96 | * ATL 97 | * WTL (source code is included) 98 | 99 | 100 | #### License 101 | 102 | [GNU GPL v 2.0](http://www.gnu.org/licenses/gpl-2.0.html) 103 | -------------------------------------------------------------------------------- /utils/sensim/.gitignore: -------------------------------------------------------------------------------- 1 | sensim 2 | obj/ 3 | -------------------------------------------------------------------------------- /utils/sensim/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2018 Vladimir Alemasov 3 | # All rights reserved 4 | # 5 | # This program and the accompanying materials are distributed under 6 | # the terms of GNU General Public License version 2 7 | # as published by the Free Software Foundation. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | 15 | TARGET = sensim 16 | OBJDIR = obj 17 | SRCDIR = src 18 | SOURCES = $(wildcard $(SRCDIR)/*.c) 19 | HEADERS = $(wildcard $(SRCDIR)/*.h) 20 | OBJECTS = $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o) 21 | DEPS = $(HEADERS) 22 | 23 | INCLPATH += -I. 24 | 25 | # Installation directories by convention 26 | # http://www.gnu.org/prep/standards/html_node/Directory-Variables.html 27 | PREFIX = /usr/local 28 | EXEC_PREFIX = $(PREFIX) 29 | BINDIR = $(EXEC_PREFIX)/bin 30 | 31 | # Syntax of Conditionals 32 | # https://www.gnu.org/software/make/manual/html_node/Conditional-Syntax.html 33 | ifndef DETAILED_LOG 34 | CFLAGS += -DNDPRINTF 35 | endif 36 | LIBS += -lpthread -lgcc_eh -lm 37 | ifdef STATIC_LINK 38 | LIBS += -ldl -static 39 | endif 40 | ifdef WITH_DEBUG 41 | CFLAGS += -g 42 | else 43 | CFLAGS += -O2 -DNDEBUG 44 | endif 45 | 46 | # main goal 47 | all: $(TARGET) 48 | 49 | # target executable 50 | $(TARGET): $(OBJECTS) 51 | $(CC) $(LDFLAGS) -o $(TARGET) $(OBJECTS) $(LIBPATH) $(LIBS) 52 | 53 | # object files 54 | $(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c $(DEPS) | $(OBJDIR) 55 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLPATH) 56 | 57 | # create object files directory 58 | $(OBJDIR): 59 | mkdir -p $(OBJDIR) 60 | 61 | # clean 62 | clean: 63 | rm -rf $(OBJDIR) 64 | 65 | # distclean 66 | distclean: clean 67 | rm -f $(TARGET) 68 | 69 | # install 70 | # http://unixhelp.ed.ac.uk/CGI/man-cgi?install 71 | install: all 72 | install -d -m 755 "$(BINDIR)" 73 | install -m 755 $(TARGET) "$(BINDIR)/" 74 | 75 | # uninstall 76 | uninstall: 77 | rm -f $(BINDIR)/$(TARGET) 78 | 79 | .PHONY: all clean distclean install uninstall 80 | -------------------------------------------------------------------------------- /utils/sensim/msvs/.gitignore: -------------------------------------------------------------------------------- 1 | # Visual Studio 2005 files 2 | *.exe 3 | *.dll 4 | *.suo 5 | *.user 6 | *.ncb 7 | *.txt 8 | Debug/ 9 | Release/ 10 | 11 | # Visual Studio 2010 files 12 | *.[Oo]bj 13 | *.user 14 | *.aps 15 | *.pch 16 | *.vspscc 17 | *.vssscc 18 | *_i.c 19 | *_p.c 20 | *.ncb 21 | *.suo 22 | *.tlb 23 | *.tlh 24 | *.bak 25 | *.[Cc]ache 26 | *.ilk 27 | *.log 28 | *.lib 29 | *.sbr 30 | *.sdf 31 | *.opensdf 32 | *.unsuccessfulbuild 33 | ipch/ 34 | obj/ 35 | [Bb]in 36 | [Dd]ebug*/ 37 | [Rr]elease*/ 38 | [Pp]ublish 39 | Ankh.NoLoad 40 | -------------------------------------------------------------------------------- /utils/sensim/msvs/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013-2018 Vladimir Alemasov 2 | All rights reserved 3 | 4 | This program and the accompanying materials are distributed under 5 | the terms of GNU General Public License version 2 6 | as published by the Free Software Foundation. 7 | 8 | This program is distributed in the hope that it will be useful, 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | GNU General Public License for more details. -------------------------------------------------------------------------------- /utils/sensim/msvs/sensim.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sensim", "sensim.vcxproj", "{050ADE50-FCDC-4ECD-9B0F-31384AB0189A}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Win32 = Debug|Win32 9 | Release|Win32 = Release|Win32 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {050ADE50-FCDC-4ECD-9B0F-31384AB0189A}.Debug|Win32.ActiveCfg = Debug|Win32 13 | {050ADE50-FCDC-4ECD-9B0F-31384AB0189A}.Debug|Win32.Build.0 = Debug|Win32 14 | {050ADE50-FCDC-4ECD-9B0F-31384AB0189A}.Release|Win32.ActiveCfg = Release|Win32 15 | {050ADE50-FCDC-4ECD-9B0F-31384AB0189A}.Release|Win32.Build.0 = Release|Win32 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /utils/sensim/src/config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef CONFIG_H_ 16 | #define CONFIG_H_ 17 | 18 | #define SENSOR_DATA 19 | 20 | #endif /* CONFIG_H_ */ 21 | -------------------------------------------------------------------------------- /utils/sensim/src/getopt.h: -------------------------------------------------------------------------------- 1 | /* -------------------------------------------- 2 | * The following code has been taken from 3 | * https://stackoverflow.com/a/17195644/4202234 4 | */ 5 | 6 | #ifndef GETOPT_H_ 7 | #define GETOPT_H_ 8 | 9 | int opterr = 1, /* if error message should be printed */ 10 | optind = 1, /* index into parent argv vector */ 11 | optopt, /* character checked for validity */ 12 | optreset; /* reset getopt */ 13 | char *optarg; /* argument associated with option */ 14 | 15 | #define BADCH (int)'?' 16 | #define BADARG (int)':' 17 | #define EMSG "" 18 | 19 | /* 20 | * getopt -- 21 | * Parse argc/argv argument vector. 22 | */ 23 | int 24 | getopt(int nargc, char * const nargv[], const char *ostr) 25 | { 26 | static char *place = EMSG; /* option letter processing */ 27 | const char *oli; /* option letter list index */ 28 | 29 | if (optreset || !*place) { /* update scanning pointer */ 30 | optreset = 0; 31 | if (optind >= nargc || *(place = nargv[optind]) != '-') { 32 | place = EMSG; 33 | return (-1); 34 | } 35 | if (place[1] && *++place == '-') { /* found "--" */ 36 | ++optind; 37 | place = EMSG; 38 | return (-1); 39 | } 40 | } /* option letter okay? */ 41 | if ((optopt = (int)*place++) == (int)':' || 42 | !(oli = strchr(ostr, optopt))) { 43 | /* 44 | * if the user didn't specify '-' as an option, 45 | * assume it means -1. 46 | */ 47 | if (optopt == (int)'-') 48 | return (-1); 49 | if (!*place) 50 | ++optind; 51 | if (opterr && *ostr != ':') 52 | (void)printf("illegal option -- %c\n", optopt); 53 | return (BADCH); 54 | } 55 | if (*++oli != ':') { /* don't need argument */ 56 | optarg = NULL; 57 | if (!*place) 58 | ++optind; 59 | } 60 | else { /* need an argument */ 61 | if (*place) /* no white space */ 62 | optarg = place; 63 | else if (nargc <= ++optind) { /* no arg */ 64 | place = EMSG; 65 | if (*ostr == ':') 66 | return (BADARG); 67 | if (opterr) 68 | (void)printf("option requires an argument -- %c\n", optopt); 69 | return (BADCH); 70 | } 71 | else /* white space */ 72 | optarg = nargv[optind]; 73 | place = EMSG; 74 | ++optind; 75 | } 76 | return (optopt); /* dump back option letter */ 77 | } 78 | 79 | #endif /* GETOPT_H_ */ 80 | -------------------------------------------------------------------------------- /utils/sensim/src/list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "list.h" 16 | #include /* assert */ 17 | 18 | 19 | //-------------------------------------------- 20 | void list_init(list_t **list) 21 | { 22 | assert(list != NULL); 23 | 24 | *list = NULL; 25 | } 26 | 27 | //-------------------------------------------- 28 | list_t *list_head(list_t **list) 29 | { 30 | assert(list != NULL); 31 | 32 | return *list; 33 | } 34 | 35 | //-------------------------------------------- 36 | list_t *list_remove(list_t **list, list_t *item) 37 | { 38 | list_t *find; 39 | list_t *prev; 40 | list_t *next; 41 | 42 | assert(list != NULL); 43 | assert(item != NULL); 44 | 45 | if (*list == NULL) 46 | return NULL; 47 | prev = NULL; 48 | for (find = *list; find != NULL; find = find->next) 49 | { 50 | if (find == item) 51 | { 52 | next = find->next; 53 | if (prev == NULL) 54 | *list = next; 55 | else 56 | prev->next = next; 57 | find->next = NULL; 58 | return next; 59 | } 60 | prev = find; 61 | } 62 | return NULL; 63 | } 64 | 65 | //-------------------------------------------- 66 | list_t *list_add_item(list_t **list, list_t *item) 67 | { 68 | list_t *last; 69 | 70 | assert(list != NULL); 71 | assert(item != NULL); 72 | 73 | item->next = NULL; 74 | 75 | if (*list == NULL) 76 | *list = item; 77 | else 78 | { 79 | for (last = *list; last->next != NULL; last = last->next); 80 | last->next = item; 81 | } 82 | 83 | return item; 84 | } 85 | 86 | //-------------------------------------------- 87 | size_t list_get_length(list_t **list) 88 | { 89 | list_t *item; 90 | size_t cnt = 0; 91 | 92 | assert(list != NULL); 93 | 94 | for (item = *list, cnt = 0; item != NULL; item = item->next, ++cnt); 95 | return cnt; 96 | } 97 | 98 | //-------------------------------------------- 99 | list_t *list_next(list_t *item) 100 | { 101 | return item == NULL ? NULL : item->next; 102 | } 103 | -------------------------------------------------------------------------------- /utils/sensim/src/list.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef LIST_H_ 16 | #define LIST_H_ 17 | 18 | #ifdef _MSC_VER 19 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 20 | #include /* NULL */ 21 | #else 22 | #include /* uint8_t ... uint64_t */ 23 | #include /* size_t */ 24 | #endif 25 | 26 | 27 | //-------------------------------------------- 28 | typedef struct list 29 | { 30 | struct list *next; 31 | } list_t; 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | //-------------------------------------------- 37 | void list_init(list_t **list); 38 | list_t *list_head(list_t **list); 39 | list_t *list_remove(list_t **list, list_t *item); 40 | list_t *list_add_item(list_t **list, list_t *item); 41 | size_t list_get_length(list_t **list); 42 | list_t *list_next(list_t *item); 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif /* LIST_H_ */ 48 | -------------------------------------------------------------------------------- /utils/sensim/src/list_mqtt_user.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "list_mqtt_user.h" 16 | #include /* assert */ 17 | #include /* malloc */ 18 | #include /* memcpy, memset, memcmp */ 19 | 20 | #ifdef _MSC_VER 21 | #pragma warning (disable:4996) // This function may be unsafe. 22 | #endif 23 | 24 | 25 | //-------------------------------------------- 26 | list_mqtt_user_t *list_mqtt_user_add_new(list_mqtt_user_t **list, const char *user_name, const char *password, unsigned char publish_enable) 27 | { 28 | list_mqtt_user_t *item; 29 | 30 | assert(list != NULL); 31 | assert(user_name != NULL); 32 | assert(password != NULL); 33 | 34 | item = (list_mqtt_user_t *)malloc(sizeof(list_mqtt_user_t)); 35 | memset(item, 0, sizeof(list_mqtt_user_t)); 36 | item->user_name_length = (uint16_t)strlen(user_name); 37 | item->user_name = (uint8_t *)malloc(item->user_name_length); 38 | memcpy(item->user_name, user_name, item->user_name_length); 39 | item->password_length = (uint16_t)strlen(password); 40 | item->password = (uint8_t *)malloc(item->password_length); 41 | memcpy(item->password, password, item->password_length); 42 | item->publish_enable = publish_enable; 43 | 44 | list_mqtt_user_add(list, item); 45 | return item; 46 | } 47 | 48 | //-------------------------------------------- 49 | void list_mqtt_user_remove_all(list_mqtt_user_t **list) 50 | { 51 | list_mqtt_user_t *next; 52 | list_mqtt_user_t *item; 53 | 54 | assert(list != NULL); 55 | 56 | item = *list; 57 | while (item != NULL) 58 | { 59 | next = list_mqtt_user_next(item); 60 | if (item->user_name != NULL) 61 | free(item->user_name); 62 | if (item->password != NULL) 63 | free(item->password); 64 | free(item); 65 | item = next; 66 | } 67 | *list = NULL; 68 | } 69 | 70 | //-------------------------------------------- 71 | list_mqtt_user_t *list_mqtt_user_find_user(list_mqtt_user_t **list, uint8_t *user_name, uint16_t user_name_length, uint8_t *password, uint16_t password_length) 72 | { 73 | list_mqtt_user_t *item; 74 | 75 | assert(list != NULL); 76 | assert(user_name != NULL); 77 | assert(password != NULL); 78 | assert(user_name_length != 0); 79 | assert(password_length != 0); 80 | 81 | item = *list; 82 | while (item != NULL) 83 | { 84 | if ((item->user_name_length == user_name_length && memcmp(item->user_name, user_name, user_name_length) == 0) && 85 | (item->password_length == password_length && memcmp(item->password, password, password_length) == 0)) 86 | return item; 87 | item = list_mqtt_user_next(item); 88 | } 89 | return NULL; 90 | } 91 | -------------------------------------------------------------------------------- /utils/sensim/src/list_mqtt_user.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef LIST_USER_H_ 16 | #define LIST_USER_H_ 17 | 18 | #include "list.h" 19 | 20 | #ifdef _MSC_VER 21 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 22 | #else 23 | #include /* uint8_t ... uint64_t */ 24 | #include /* size_t */ 25 | #endif 26 | 27 | typedef struct list_mqtt_user 28 | { 29 | list_t next; 30 | uint16_t user_name_length; 31 | uint8_t *user_name; 32 | uint16_t password_length; 33 | uint8_t *password; 34 | unsigned char publish_enable; 35 | } list_mqtt_user_t; 36 | 37 | list_mqtt_user_t *list_mqtt_user_add_new(list_mqtt_user_t **list, const char *user_name, const char *password, unsigned char publish_enable); 38 | #define list_mqtt_user_add(a, b) (list_mqtt_user_t *)list_add_item((list_t **)a, (list_t *)b) 39 | #define list_mqtt_user_next(a) (list_mqtt_user_t *)list_next((list_t *)a) 40 | void list_mqtt_user_remove_all(list_mqtt_user_t **list); 41 | list_mqtt_user_t *list_mqtt_user_find_user(list_mqtt_user_t **list, uint8_t *user_name, uint16_t user_name_length, uint8_t *password, uint16_t password_length); 42 | 43 | #endif /* LIST_USER_H_ */ 44 | -------------------------------------------------------------------------------- /utils/sensim/src/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifdef _MSC_VER 16 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 17 | #else 18 | #include /* uint8_t ... uint64_t */ 19 | #endif 20 | 21 | #include "config.h" 22 | #include /* exit */ 23 | #include /* getchar, printf */ 24 | #include "os_port.h" 25 | #include "msg_udp_mqtt.h" 26 | #include "msg_mqtt_udp.h" 27 | #include "msg_tcp_mqtt.h" 28 | #include "msg_mqtt_tcp.h" 29 | #include "thread_udp.h" 30 | #include "thread_tcp.h" 31 | #include "thread_test_mqtt.h" 32 | #include "thread_test_mqttsn.h" 33 | #include "thread_tick.h" 34 | #include "tick_counter.h" 35 | #include "test_mqttsn.h" 36 | #include "test_mqtt.h" 37 | 38 | #ifdef _MSC_VER 39 | #pragma warning (disable:4996) // This function may be unsafe. 40 | #endif 41 | 42 | #ifdef WIN32 43 | #include "getopt.h" 44 | #endif 45 | 46 | //-------------------------------------------- 47 | size_t test_suite_get_size() 48 | { 49 | return (test_mqttsn_get_size() < test_mqtt_get_size() ? test_mqttsn_get_size() : test_mqtt_get_size()); 50 | } 51 | 52 | //-------------------------------------------- 53 | void print_usage() 54 | { 55 | printf("Usage: sensim -a whsnbg_ip_addr[127.0.0.1] -p whsnbg_mqttsn_udp_port[1883] -s test_suite_number[1...3]\n"); 56 | printf("test_suite_number:\n"); 57 | printf("\t1 - general connect test\n"); 58 | printf("\t2 - sleep sensor test\n"); 59 | printf("\t3 - actuator test\n"); 60 | } 61 | 62 | //-------------------------------------------- 63 | int main(int argc, char *argv[]) 64 | { 65 | int option; 66 | unsigned short port; 67 | unsigned long addr; 68 | size_t suite; 69 | #ifdef WIN32 70 | WSADATA data; 71 | #endif 72 | 73 | if (argc != 7) 74 | { 75 | print_usage(); 76 | exit(EXIT_FAILURE); 77 | } 78 | 79 | option = 0; 80 | while ((option = getopt(argc, argv, "a:p:s:")) != -1) 81 | { 82 | switch (option) 83 | { 84 | case 'a': 85 | addr = inet_addr(optarg); 86 | break; 87 | case 'p': 88 | port = (unsigned short)atoi(optarg); 89 | break; 90 | case 's': 91 | suite = (size_t)atoi(optarg); 92 | if (suite >= 1 && suite <= test_suite_get_size()) 93 | { 94 | suite -= 1; 95 | break; 96 | } 97 | default: 98 | print_usage(); 99 | exit(EXIT_FAILURE); 100 | } 101 | } 102 | 103 | test_mqtt_set_list(suite); 104 | test_mqttsn_set_list(suite); 105 | test_mqtt_setup(0, 0); 106 | test_mqttsn_setup(0, 0); 107 | 108 | #ifdef WIN32 109 | WSAStartup(MAKEWORD(2, 2), &data); 110 | #endif 111 | 112 | tick_counter_init(); 113 | msg_udp_mqtt_init(); 114 | msg_mqtt_udp_init(); 115 | msg_tcp_mqtt_init(); 116 | msg_mqtt_tcp_init(); 117 | 118 | thread_udp_addr_setup(port, addr); 119 | if (thread_udp_start() < 0) 120 | { 121 | exit(EXIT_FAILURE); 122 | } 123 | thread_tcp_addr_setup(port, addr); 124 | if (thread_tcp_start() < 0) 125 | { 126 | exit(EXIT_FAILURE); 127 | } 128 | thread_tick_start(); 129 | thread_test_mqttsn_start(); 130 | thread_test_mqtt_start(); 131 | 132 | getchar(); 133 | 134 | msg_udp_mqtt_close(); 135 | msg_mqtt_udp_close(); 136 | msg_tcp_mqtt_close(); 137 | msg_mqtt_tcp_close(); 138 | 139 | thread_test_mqttsn_stop(); 140 | thread_test_mqtt_stop(); 141 | thread_tick_stop(); 142 | thread_udp_stop(); 143 | thread_tcp_stop(); 144 | 145 | test_mqttsn_destroy(); 146 | test_mqtt_destroy(); 147 | 148 | tick_counter_destroy(); 149 | msg_udp_mqtt_destroy(); 150 | msg_mqtt_udp_destroy(); 151 | msg_tcp_mqtt_destroy(); 152 | msg_mqtt_tcp_destroy(); 153 | 154 | #ifdef WIN32 155 | WSACleanup(); 156 | #endif 157 | 158 | exit(EXIT_SUCCESS); 159 | } 160 | -------------------------------------------------------------------------------- /utils/sensim/src/msg_mqtt_tcp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "msg_mqtt_tcp.h" 16 | #include /* memset */ 17 | 18 | static msgqueue_t queue; 19 | 20 | //-------------------------------------------- 21 | void msg_mqtt_tcp_init(void) 22 | { 23 | msg_init(&queue); 24 | } 25 | 26 | //-------------------------------------------- 27 | msg_mqtt_tcp_t *msg_mqtt_tcp_new(void) 28 | { 29 | msg_mqtt_tcp_t *ms; 30 | ms = (msg_mqtt_tcp_t *)malloc(sizeof(msg_mqtt_tcp_t)); 31 | memset(ms, 0, sizeof(msg_mqtt_tcp_t)); 32 | return ms; 33 | } 34 | 35 | //-------------------------------------------- 36 | void msg_mqtt_tcp_add(msg_mqtt_tcp_t *ms) 37 | { 38 | msg_add(&queue, (msg_t *)ms); 39 | } 40 | 41 | //-------------------------------------------- 42 | void msg_mqtt_tcp_remove(msg_mqtt_tcp_t *ms) 43 | { 44 | msg_remove(&queue, (msg_t *)ms); 45 | if (ms->msg_buf != NULL) 46 | free(ms->msg_buf); 47 | free(ms); 48 | } 49 | 50 | //-------------------------------------------- 51 | msg_mqtt_tcp_t* msg_mqtt_tcp_get_first(void) 52 | { 53 | return (msg_mqtt_tcp_t *)msg_get_first(&queue); 54 | } 55 | 56 | //-------------------------------------------- 57 | void msg_mqtt_tcp_destroy(void) 58 | { 59 | msg_destroy(&queue); 60 | } 61 | 62 | //-------------------------------------------- 63 | void msg_mqtt_tcp_add_close_conn(struct sockaddr_in *addr) 64 | { 65 | msg_mqtt_tcp_t *ms = msg_mqtt_tcp_new(); 66 | memcpy(&ms->addr, addr, sizeof(struct sockaddr_in)); 67 | ms->close = 1; 68 | msg_mqtt_tcp_add(ms); 69 | } 70 | 71 | //-------------------------------------------- 72 | void msg_mqtt_tcp_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size) 73 | { 74 | msg_mqtt_tcp_t *ms = msg_mqtt_tcp_new(); 75 | memcpy(&ms->addr, addr, sizeof(struct sockaddr_in)); 76 | ms->msg_buf = buf; // external malloc 77 | ms->msg_cnt = size; 78 | msg_mqtt_tcp_add(ms); 79 | } 80 | -------------------------------------------------------------------------------- /utils/sensim/src/msg_mqtt_tcp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef MSG_MQTT_TCP_H_ 16 | #define MSG_MQTT_TCP_H_ 17 | 18 | #include "msgs.h" 19 | 20 | //-------------------------------------------- 21 | typedef struct msg_mqtt_tcp 22 | { 23 | msg_t msg; 24 | unsigned char *msg_buf; 25 | size_t msg_cnt; 26 | struct sockaddr_in addr; 27 | unsigned char close; 28 | } msg_mqtt_tcp_t; 29 | 30 | //-------------------------------------------- 31 | void msg_mqtt_tcp_init(void); 32 | msg_mqtt_tcp_t *msg_mqtt_tcp_new(void); 33 | void msg_mqtt_tcp_add(msg_mqtt_tcp_t *ms); 34 | void msg_mqtt_tcp_remove(msg_mqtt_tcp_t *ms); 35 | msg_mqtt_tcp_t *msg_mqtt_tcp_get_first(void); 36 | #define msg_mqtt_tcp_close() msg_close(a) 37 | void msg_mqtt_tcp_destroy(void); 38 | 39 | void msg_mqtt_tcp_add_close_conn(struct sockaddr_in *addr); 40 | void msg_mqtt_tcp_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size); 41 | 42 | #endif /* MSG_MQTT_TCP_H_ */ 43 | -------------------------------------------------------------------------------- /utils/sensim/src/msg_mqtt_udp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "msg_mqtt_udp.h" 16 | #include /* memset */ 17 | 18 | static msgqueue_t queue; 19 | 20 | //-------------------------------------------- 21 | void msg_mqtt_udp_init(void) 22 | { 23 | msg_init(&queue); 24 | } 25 | 26 | //-------------------------------------------- 27 | msg_mqtt_udp_t *msg_mqtt_udp_new(void) 28 | { 29 | msg_mqtt_udp_t *ms; 30 | ms = (msg_mqtt_udp_t *)malloc(sizeof(msg_mqtt_udp_t)); 31 | memset(ms, 0, sizeof(msg_mqtt_udp_t)); 32 | return ms; 33 | } 34 | 35 | //-------------------------------------------- 36 | void msg_mqtt_udp_add(msg_mqtt_udp_t *ms) 37 | { 38 | msg_add(&queue, (msg_t *)ms); 39 | } 40 | 41 | //-------------------------------------------- 42 | void msg_mqtt_udp_remove(msg_mqtt_udp_t *ms) 43 | { 44 | msg_remove(&queue, (msg_t *)ms); 45 | if (ms->msg_buf != NULL) 46 | free(ms->msg_buf); 47 | free(ms); 48 | } 49 | 50 | //-------------------------------------------- 51 | msg_mqtt_udp_t* msg_mqtt_udp_get_first(void) 52 | { 53 | return (msg_mqtt_udp_t *)msg_get_first(&queue); 54 | } 55 | 56 | //-------------------------------------------- 57 | void msg_mqtt_udp_destroy(void) 58 | { 59 | msg_destroy(&queue); 60 | } 61 | 62 | //-------------------------------------------- 63 | void msg_mqtt_udp_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size) 64 | { 65 | msg_mqtt_udp_t *ms = msg_mqtt_udp_new(); 66 | memcpy(&ms->addr, addr, sizeof(struct sockaddr_in)); 67 | ms->msg_buf = buf; // external malloc 68 | ms->msg_cnt = size; 69 | msg_mqtt_udp_add(ms); 70 | } 71 | -------------------------------------------------------------------------------- /utils/sensim/src/msg_mqtt_udp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef MSG_MQTT_UDP_H_ 16 | #define MSG_MQTT_UDP_H_ 17 | 18 | #include "msgs.h" 19 | 20 | 21 | //-------------------------------------------- 22 | typedef struct msg_mqtt_udp 23 | { 24 | msg_t msg; 25 | unsigned char *msg_buf; 26 | size_t msg_cnt; 27 | struct sockaddr_in addr; 28 | } msg_mqtt_udp_t; 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | //-------------------------------------------- 34 | void msg_mqtt_udp_init(void); 35 | msg_mqtt_udp_t *msg_mqtt_udp_new(void); 36 | void msg_mqtt_udp_add(msg_mqtt_udp_t *ms); 37 | void msg_mqtt_udp_remove(msg_mqtt_udp_t *ms); 38 | msg_mqtt_udp_t *msg_mqtt_udp_get_first(void); 39 | #define msg_mqtt_udp_close() msg_close(a) 40 | void msg_mqtt_udp_destroy(void); 41 | 42 | void msg_mqtt_udp_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size); 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif /* MSG_MQTT_UDP_H_ */ 48 | -------------------------------------------------------------------------------- /utils/sensim/src/msg_tcp_mqtt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015, 2019 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "msg_tcp_mqtt.h" 16 | #include /* memset, memcpy */ 17 | 18 | static msgqueue_t queue; 19 | 20 | //-------------------------------------------- 21 | void msg_tcp_mqtt_init(void) 22 | { 23 | msg_init(&queue); 24 | } 25 | 26 | //-------------------------------------------- 27 | msg_tcp_mqtt_t *msg_tcp_mqtt_new(void) 28 | { 29 | msg_tcp_mqtt_t *ms; 30 | ms = (msg_tcp_mqtt_t *)malloc(sizeof(msg_tcp_mqtt_t)); 31 | memset(ms, 0, sizeof(msg_tcp_mqtt_t)); 32 | return ms; 33 | } 34 | 35 | //-------------------------------------------- 36 | void msg_tcp_mqtt_add(msg_tcp_mqtt_t *ms) 37 | { 38 | msg_add(&queue, (msg_t *)ms); 39 | } 40 | 41 | //-------------------------------------------- 42 | void msg_tcp_mqtt_remove(msg_tcp_mqtt_t *ms) 43 | { 44 | msg_remove(&queue, (msg_t *)ms); 45 | if (ms->msg_buf != NULL) 46 | free(ms->msg_buf); 47 | free(ms); 48 | } 49 | 50 | //-------------------------------------------- 51 | msg_tcp_mqtt_t *msg_tcp_mqtt_get_first(void) 52 | { 53 | return (msg_tcp_mqtt_t *)msg_get_first(&queue); 54 | } 55 | 56 | //-------------------------------------------- 57 | void msg_tcp_mqtt_destroy(void) 58 | { 59 | msg_destroy(&queue); 60 | } 61 | 62 | //-------------------------------------------- 63 | void msg_tcp_mqtt_add_close_conn(struct sockaddr_in *addr) 64 | { 65 | msg_tcp_mqtt_t *ms = msg_tcp_mqtt_new(); 66 | memcpy(&ms->addr, addr, sizeof(struct sockaddr_in)); 67 | ms->close = 1; 68 | msg_tcp_mqtt_add(ms); 69 | } 70 | 71 | //-------------------------------------------- 72 | void msg_tcp_mqtt_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size) 73 | { 74 | msg_tcp_mqtt_t *ms = msg_tcp_mqtt_new(); 75 | memcpy(&ms->addr, addr, sizeof(struct sockaddr_in)); 76 | ms->msg_buf = (unsigned char *)malloc(size); 77 | ms->msg_cnt = size; 78 | ms->proc_msg_cnt = 0; 79 | memcpy(ms->msg_buf, buf, size); 80 | msg_tcp_mqtt_add(ms); 81 | } 82 | -------------------------------------------------------------------------------- /utils/sensim/src/msg_tcp_mqtt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015, 2019 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef MSG_TCP_MQTT_H_ 16 | #define MSG_TCP_MQTT_H_ 17 | 18 | #include "msgs.h" 19 | 20 | //-------------------------------------------- 21 | typedef struct msg_tcp_mqtt 22 | { 23 | msg_t msg; 24 | char *msg_buf; 25 | size_t msg_cnt; 26 | size_t proc_msg_cnt; 27 | struct sockaddr_in addr; 28 | unsigned char close; 29 | } msg_tcp_mqtt_t; 30 | 31 | //-------------------------------------------- 32 | void msg_tcp_mqtt_init(void); 33 | msg_tcp_mqtt_t *msg_tcp_mqtt_new(void); 34 | void msg_tcp_mqtt_add(msg_tcp_mqtt_t *ms); 35 | void msg_tcp_mqtt_remove(msg_tcp_mqtt_t *ms); 36 | msg_tcp_mqtt_t *msg_tcp_mqtt_get_first(void); 37 | #define msg_tcp_mqtt_close() msg_close(a) 38 | void msg_tcp_mqtt_destroy(void); 39 | 40 | void msg_tcp_mqtt_add_close_conn(struct sockaddr_in *addr); 41 | void msg_tcp_mqtt_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size); 42 | 43 | #endif /* MSG_TCP_MQTT_H_ */ 44 | -------------------------------------------------------------------------------- /utils/sensim/src/msg_udp_mqtt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "msg_udp_mqtt.h" 16 | #include /* memset, memcpy */ 17 | 18 | static msgqueue_t queue; 19 | 20 | //-------------------------------------------- 21 | void msg_udp_mqtt_init(void) 22 | { 23 | msg_init(&queue); 24 | } 25 | 26 | //-------------------------------------------- 27 | msg_udp_mqtt_t *msg_udp_mqtt_new(void) 28 | { 29 | msg_udp_mqtt_t *msg; 30 | 31 | msg = (msg_udp_mqtt_t *)malloc(sizeof(msg_udp_mqtt_t)); 32 | memset(msg, 0, sizeof(msg_udp_mqtt_t)); 33 | return msg; 34 | } 35 | 36 | //-------------------------------------------- 37 | void msg_udp_mqtt_add(msg_udp_mqtt_t *msg) 38 | { 39 | msg_add(&queue, (msg_t *)msg); 40 | } 41 | 42 | //-------------------------------------------- 43 | void msg_udp_mqtt_remove(msg_udp_mqtt_t *msg) 44 | { 45 | msg_remove(&queue, (msg_t *)msg); 46 | if (msg->msg_buf != NULL) 47 | free(msg->msg_buf); 48 | free(msg); 49 | } 50 | 51 | //-------------------------------------------- 52 | msg_udp_mqtt_t *msg_udp_mqtt_get_first(void) 53 | { 54 | return (msg_udp_mqtt_t *)msg_get_first(&queue); 55 | } 56 | 57 | //-------------------------------------------- 58 | void msg_udp_mqtt_destroy(void) 59 | { 60 | msg_destroy(&queue); 61 | } 62 | 63 | //-------------------------------------------- 64 | void msg_udp_mqtt_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size) 65 | { 66 | msg_udp_mqtt_t *msg; 67 | 68 | msg = msg_udp_mqtt_new(); 69 | memcpy(&msg->addr, addr, sizeof(struct sockaddr_in)); 70 | msg->msg_buf = (unsigned char *)malloc(size); 71 | msg->msg_cnt = size; 72 | memcpy(msg->msg_buf, buf, size); 73 | msg_udp_mqtt_add(msg); 74 | } 75 | -------------------------------------------------------------------------------- /utils/sensim/src/msg_udp_mqtt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef MSG_UDP_MQTT_H_ 16 | #define MSG_UDP_MQTT_H_ 17 | 18 | #include "msgs.h" 19 | 20 | 21 | //-------------------------------------------- 22 | typedef struct msg_udp_mqtt 23 | { 24 | msg_t msg; 25 | char *msg_buf; 26 | size_t msg_cnt; 27 | struct sockaddr_in addr; 28 | } msg_udp_mqtt_t; 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | //-------------------------------------------- 34 | void msg_udp_mqtt_init(void); 35 | msg_udp_mqtt_t *msg_udp_mqtt_new(void); 36 | void msg_udp_mqtt_add(msg_udp_mqtt_t *msg); 37 | void msg_udp_mqtt_remove(msg_udp_mqtt_t *msg); 38 | msg_udp_mqtt_t *msg_udp_mqtt_get_first(void); 39 | #define msg_udp_mqtt_close() msg_close(a) 40 | void msg_udp_mqtt_destroy(void); 41 | 42 | void msg_udp_mqtt_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size); 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif /* MSG_UDP_MQTT_H_ */ 48 | -------------------------------------------------------------------------------- /utils/sensim/src/msgs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef MSGS_H_ 16 | #define MSGS_H_ 17 | 18 | #include "os_port.h" 19 | 20 | //-------------------------------------------- 21 | typedef struct msg 22 | { 23 | struct msg *next; 24 | } msg_t; 25 | 26 | 27 | //-------------------------------------------- 28 | typedef struct msgqueue 29 | { 30 | msg_t *msglist; 31 | pthread_mutex_t mutex; 32 | } msgqueue_t; 33 | 34 | void msg_init(msgqueue_t *queue); 35 | void msg_add(msgqueue_t *queue, msg_t *msg); 36 | void msg_remove(msgqueue_t *queue, msg_t *msg); 37 | msg_t* msg_get_first(msgqueue_t *queue); 38 | #define msg_close(a) 39 | void msg_destroy(msgqueue_t *queue); 40 | 41 | 42 | //-------------------------------------------- 43 | typedef struct msgqueue_cond 44 | { 45 | msgqueue_t msgqueue; 46 | pthread_cond_t cond; 47 | } msgqueue_cond_t; 48 | 49 | void msg_cond_init(msgqueue_cond_t *queue); 50 | void msg_cond_add(msgqueue_cond_t *queue, msg_t *msg); 51 | void msg_cond_remove(msgqueue_cond_t *queue, msg_t *msg); 52 | msg_t* msg_cond_get_first(msgqueue_cond_t *queue); 53 | void msg_cond_close(msgqueue_cond_t *queue); 54 | void msg_cond_destroy(msgqueue_cond_t *queue); 55 | 56 | 57 | //-------------------------------------------- 58 | typedef struct msggap 59 | { 60 | void *msg; 61 | int request; 62 | pthread_mutex_t mutex; 63 | pthread_cond_t cond; 64 | } msggap_t; 65 | 66 | void msggap_init(msggap_t *gap); 67 | void msggap_request(msggap_t *gap); 68 | int msggap_get_request(msggap_t *gap); 69 | void msggap_reply(msggap_t *gap); 70 | void msggap_close(msggap_t *gap); 71 | void msggap_destroy(msggap_t *gap); 72 | 73 | #endif /* MSGS_H_ */ 74 | -------------------------------------------------------------------------------- /utils/sensim/src/sensor_data.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homewsn/whsnbg/7767d3c72b03299286a851e4e8e59e9709a5485c/utils/sensim/src/sensor_data.c -------------------------------------------------------------------------------- /utils/sensim/src/sensor_data.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef SENSOR_DATA_H_ 16 | #define SENSOR_DATA_H_ 17 | 18 | #ifdef _MSC_VER 19 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 20 | #else 21 | #include /* uint8_t ... uint64_t */ 22 | #include /* size_t */ 23 | #endif 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | //-------------------------------------------- 29 | void encode_mqttsn_sensor_data(uint8_t **buf, size_t *size, const char *data, size_t data_len); 30 | void decode_mqttsn_sensor_data(char **buf, size_t *size, uint8_t *data); 31 | void parse_mqttsn_topic_name_to_mysql_query(char *name, size_t name_len, uint8_t *data); 32 | void parse_mqtt_topic_name_to_mysql_query(char *name, size_t name_len, char *data, size_t data_len); 33 | int check_for_actuators_id_topic(char *name, size_t name_len); 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | 38 | #endif /* SENSOR_DATA_H_ */ 39 | -------------------------------------------------------------------------------- /utils/sensim/src/test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef TEST_H_ 16 | #define TEST_H_ 17 | 18 | //-------------------------------------------- 19 | typedef struct test 20 | { 21 | uint32_t delay; 22 | void (*command)(void *); 23 | void *to_send; 24 | int (*response)(void *, void *); 25 | void *expected; 26 | int (*noresponse)(void); 27 | uint32_t timeout; 28 | } test_t; 29 | 30 | #endif /* TEST_H_ */ 31 | -------------------------------------------------------------------------------- /utils/sensim/src/test_mqtt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef TEST_MQTT_H_ 16 | #define TEST_MQTT_H_ 17 | 18 | #include "test.h" 19 | 20 | //-------------------------------------------- 21 | void test_mqtt_setup(int auto_msgid, int auto_strlen); 22 | void test_mqtt_destroy(void); 23 | size_t test_mqtt_get_size(void); 24 | size_t test_mqtt_list_get_size(void); 25 | test_t *test_mqtt_list_get_test(size_t number); 26 | void test_mqtt_set_list(size_t number); 27 | 28 | 29 | //-------------------------------------------- 30 | // CONNECT 31 | //-------------------------------------------- 32 | void mqtt_command_connect(void *to_send); 33 | int mqtt_noresponse_command_connect_connack(void); 34 | int mqtt_noresponse_command_connect_willtopicreq(void); 35 | int mqtt_noresponse_command_connect_willmsgreq(void); 36 | int mqtt_noresponse_command_connect_passed(void); 37 | 38 | //-------------------------------------------- 39 | // CONNACK 40 | //-------------------------------------------- 41 | int mqtt_response_connack(void *received, void *expected); 42 | 43 | //-------------------------------------------- 44 | // DISCONNECT 45 | //-------------------------------------------- 46 | void mqtt_command_disconnect(void *to_send); 47 | int mqtt_noresponse_command_disconnect_passed(void); 48 | int mqtt_response_disconnect(void *received, void *expected); 49 | 50 | //-------------------------------------------- 51 | // PINGREQ 52 | //-------------------------------------------- 53 | void mqtt_command_pingreq(void *to_send); 54 | int mqtt_noresponse_command_pingreq_pingresp(void); 55 | 56 | //-------------------------------------------- 57 | // PINGRESP 58 | //-------------------------------------------- 59 | int mqtt_response_pingresp(void *received, void *expected); 60 | 61 | //-------------------------------------------- 62 | // PUBLISH 63 | //-------------------------------------------- 64 | void mqtt_command_publish(void *to_send); 65 | int mqtt_noresponse_command_publish_puback(void); 66 | int mqtt_noresponse_command_publish_pubrec(void); 67 | int mqtt_response_publish(void *received, void *expected); 68 | 69 | //-------------------------------------------- 70 | // PUBACK 71 | //-------------------------------------------- 72 | int mqtt_response_puback(void *received, void *expected); 73 | void mqtt_command_puback(void *to_send); 74 | 75 | //-------------------------------------------- 76 | // PUBREC 77 | //-------------------------------------------- 78 | int mqtt_response_pubrec(void *received, void *expected); 79 | 80 | //-------------------------------------------- 81 | // PUBREL 82 | //-------------------------------------------- 83 | void mqtt_command_pubrel(void *to_send); 84 | int mqtt_noresponse_command_pubrel_pubcomp(void); 85 | 86 | //-------------------------------------------- 87 | // PUBCOMP 88 | //-------------------------------------------- 89 | int mqtt_response_pubcomp(void *received, void *expected); 90 | 91 | //-------------------------------------------- 92 | // SUBSCRIBE 93 | //-------------------------------------------- 94 | void mqtt_command_subscribe(void *to_send); 95 | int mqtt_noresponse_command_subscribe_suback(void); 96 | 97 | //-------------------------------------------- 98 | // SUBACK 99 | //-------------------------------------------- 100 | int mqtt_response_suback(void *received, void *expected); 101 | 102 | //-------------------------------------------- 103 | // NOTHING 104 | //-------------------------------------------- 105 | int mqtt_noresponse_nothing(void); 106 | int mqtt_response_nothing(void *received, void *expected); 107 | int mqtt_noresponse_nothing_publish(void); 108 | 109 | #endif /* TEST_MQTT_H_ */ 110 | -------------------------------------------------------------------------------- /utils/sensim/src/test_mqtt_list1.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef TEST_MQTT_LIST1_H_ 16 | #define TEST_MQTT_LIST1_H_ 17 | 18 | #include "test.h" 19 | 20 | //-------------------------------------------- 21 | size_t test_mqtt_list1_get_size(void); 22 | test_t *test_mqtt_list1_get_test(size_t number); 23 | 24 | #endif /* TEST_MQTT_LIST1_H_ */ 25 | -------------------------------------------------------------------------------- /utils/sensim/src/test_mqtt_list2.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef TEST_MQTT_LIST2_H_ 16 | #define TEST_MQTT_LIST2_H_ 17 | 18 | #include "test.h" 19 | 20 | //-------------------------------------------- 21 | size_t test_mqtt_list2_get_size(void); 22 | test_t *test_mqtt_list2_get_test(size_t number); 23 | 24 | #endif /* TEST_MQTT_LIST2_H_ */ 25 | -------------------------------------------------------------------------------- /utils/sensim/src/test_mqtt_list3.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef TEST_MQTT_LIST3_H_ 16 | #define TEST_MQTT_LIST3_H_ 17 | 18 | #include "test.h" 19 | 20 | //-------------------------------------------- 21 | size_t test_mqtt_list3_get_size(void); 22 | test_t *test_mqtt_list3_get_test(size_t number); 23 | 24 | #endif /* TEST_MQTT_LIST3_H_ */ 25 | -------------------------------------------------------------------------------- /utils/sensim/src/test_mqttsn_list1.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef TEST_MQTTSN_LIST1_H_ 16 | #define TEST_MQTTSN_LIST1_H_ 17 | 18 | #include "test.h" 19 | 20 | //-------------------------------------------- 21 | size_t test_mqttsn_list1_get_size(void); 22 | test_t *test_mqttsn_list1_get_test(size_t number); 23 | 24 | #endif /* TEST_MQTTSN_LIST1_H_ */ 25 | -------------------------------------------------------------------------------- /utils/sensim/src/test_mqttsn_list2.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef TEST_MQTTSN_LIST2_H_ 16 | #define TEST_MQTTSN_LIST2_H_ 17 | 18 | #include "test.h" 19 | 20 | //-------------------------------------------- 21 | size_t test_mqttsn_list2_get_size(void); 22 | test_t *test_mqttsn_list2_get_test(size_t number); 23 | 24 | #endif /* TEST_MQTTSN_LIST2_H_ */ 25 | -------------------------------------------------------------------------------- /utils/sensim/src/test_mqttsn_list3.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef TEST_MQTTSN_LIST3_H_ 16 | #define TEST_MQTTSN_LIST3_H_ 17 | 18 | #include "test.h" 19 | 20 | //-------------------------------------------- 21 | size_t test_mqttsn_list3_get_size(void); 22 | test_t *test_mqttsn_list3_get_test(size_t number); 23 | 24 | #endif /* TEST_MQTTSN_LIST3_H_ */ 25 | -------------------------------------------------------------------------------- /utils/sensim/src/thread_state.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef THREAD_STATE_H_ 16 | #define THREAD_STATE_H_ 17 | 18 | //-------------------------------------------- 19 | typedef enum 20 | { 21 | THREAD_RUNNING, 22 | THREAD_STAYING, 23 | THREAD_STOPPED 24 | } thread_state_t; 25 | 26 | 27 | #endif /* THREAD_STATE_H_ */ 28 | -------------------------------------------------------------------------------- /utils/sensim/src/thread_tcp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef THREAD_TCP_H_ 16 | #define THREAD_TCP_H_ 17 | 18 | int thread_tcp_start(void); 19 | void thread_tcp_stop(void); 20 | void thread_tcp_addr_setup(unsigned short port, unsigned long address); 21 | 22 | #endif /* THREAD_TCP_H_ */ 23 | -------------------------------------------------------------------------------- /utils/sensim/src/thread_test_mqtt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef THREAD_TEST_MQTT_H_ 16 | #define THREAD_TEST_MQTT_H_ 17 | 18 | void thread_test_mqtt_start(void); 19 | void thread_test_mqtt_stop(void); 20 | void thread_test_mqtt_cancel(void); 21 | void thread_test_mqtt_exitmsg(void); 22 | 23 | #endif /* THREAD_TEST_MQTT_H_ */ 24 | -------------------------------------------------------------------------------- /utils/sensim/src/thread_test_mqttsn.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifdef _MSC_VER 16 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 17 | #else 18 | #include /* uint8_t ... uint64_t */ 19 | #endif 20 | 21 | #include /* assert */ 22 | #include /* printf */ 23 | #include "os_port.h" 24 | #include "msg_udp_mqtt.h" 25 | #include "thread_state.h" 26 | #include "tick_counter.h" 27 | #include "mqttsn.h" 28 | #include "test_mqttsn.h" 29 | #include "thread_test_mqtt.h" 30 | 31 | #ifdef _MSC_VER 32 | #pragma warning (disable:4996) // This function may be unsafe. 33 | #endif 34 | 35 | #ifndef NDPRINTF 36 | #define dprintf(...) printf(__VA_ARGS__) 37 | #else 38 | #define dprintf(...) 39 | #endif 40 | 41 | //-------------------------------------------- 42 | static int cancel_flag = 0; 43 | static int exitmsg_flag = 0; 44 | static volatile thread_state_t thread_state; 45 | 46 | //-------------------------------------------- 47 | static void thread_run(void *param) 48 | { 49 | alarm_timer_t test_timer = { 0 }; 50 | size_t cnt; 51 | msg_udp_mqtt_t *ms; 52 | mqttsn_fixed_header_t fixhdr; 53 | int test_failed = 0; 54 | test_t *test; 55 | 56 | for (cnt = 0; (cnt < test_mqttsn_list_get_size()) && (!test_failed); cnt++) 57 | { 58 | test = test_mqttsn_list_get_test(cnt); 59 | if (test->delay) 60 | { 61 | printf("MQTTSN: delay %d sec before sending the next command.\n\n", test->delay); 62 | tick_counter_set_mqttsn_mode(1); 63 | while (!alarm_timer_check(&test_timer, test->delay)) 64 | { 65 | if (thread_state == THREAD_STAYING) 66 | { 67 | goto test_stop; 68 | } 69 | if (cancel_flag) 70 | { 71 | goto test_cancel; 72 | } 73 | sched_yield(); 74 | } 75 | tick_counter_set_mqttsn_mode(0); 76 | } 77 | if (test->command) 78 | { 79 | test->command(test->to_send); 80 | } 81 | for (;;) 82 | { 83 | tick_counter_set_mqttsn_mode(1); 84 | if (alarm_timer_check(&test_timer, test->timeout)) 85 | { 86 | test_failed = test->noresponse(); 87 | break; 88 | } 89 | if ((ms = msg_udp_mqtt_get_first()) != NULL) 90 | { 91 | if (mqttsn_fixed_header_decode(&fixhdr, (unsigned char *)ms->msg_buf, ms->msg_cnt) == 0) 92 | { 93 | test_failed = test->response(&fixhdr, test->expected); 94 | } 95 | else 96 | { 97 | printf("The received packet is corrupted.\n"); 98 | test_failed = 1; 99 | } 100 | msg_udp_mqtt_remove(ms); 101 | break; 102 | } 103 | if (test_failed) 104 | { 105 | break; 106 | } 107 | if (thread_state == THREAD_STAYING) 108 | { 109 | goto test_stop; 110 | } 111 | if (cancel_flag) 112 | { 113 | goto test_cancel; 114 | } 115 | sched_yield(); 116 | } 117 | tick_counter_set_mqttsn_mode(0); 118 | } 119 | 120 | printf("\n--------------------------------------------\n"); 121 | if (test_failed) 122 | { 123 | thread_test_mqtt_cancel(); 124 | printf("MQTTSN tests are failed.\n"); 125 | } 126 | else 127 | { 128 | printf("MQTTSN tests passed successfully.\n"); 129 | } 130 | thread_test_mqtt_exitmsg(); 131 | printf("--------------------------------------------\n\n"); 132 | 133 | test_cancel: 134 | if (cancel_flag) 135 | { 136 | tick_counter_set_mqttsn_mode(0); 137 | printf("MQTTSN tests are cancelled.\n"); 138 | } 139 | if (exitmsg_flag) 140 | { 141 | printf("Press any key to exit...\n"); 142 | } 143 | 144 | for (;;) 145 | { 146 | if (thread_state == THREAD_STAYING) 147 | { 148 | break; 149 | } 150 | sched_yield(); 151 | } 152 | 153 | test_stop: 154 | thread_state = THREAD_STOPPED; 155 | } 156 | 157 | //-------------------------------------------- 158 | #ifdef WIN32 159 | static unsigned int __stdcall thread_launcher(void *param) 160 | { 161 | thread_run(param); 162 | return 0; 163 | } 164 | #else 165 | static void *thread_launcher(void *param) 166 | { 167 | thread_run(param); 168 | return NULL; 169 | } 170 | #endif 171 | 172 | //-------------------------------------------- 173 | void thread_test_mqttsn_start(void) 174 | { 175 | pthread_t thread; 176 | void *param = NULL; 177 | 178 | thread_state = THREAD_RUNNING; 179 | thread_begin(thread_launcher, param, &thread); 180 | } 181 | 182 | //-------------------------------------------- 183 | void thread_test_mqttsn_stop(void) 184 | { 185 | if (thread_state == THREAD_RUNNING) 186 | thread_state = THREAD_STAYING; 187 | while (thread_state != THREAD_STOPPED) 188 | sleep(10); 189 | } 190 | 191 | //-------------------------------------------- 192 | void thread_test_mqttsn_cancel(void) 193 | { 194 | cancel_flag = 1; 195 | } 196 | 197 | //-------------------------------------------- 198 | void thread_test_mqttsn_exitmsg(void) 199 | { 200 | exitmsg_flag = 1; 201 | } 202 | -------------------------------------------------------------------------------- /utils/sensim/src/thread_test_mqttsn.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef THREAD_TEST_MQTTSN_H_ 16 | #define THREAD_TEST_MQTTSN_H_ 17 | 18 | void thread_test_mqttsn_start(void); 19 | void thread_test_mqttsn_stop(void); 20 | void thread_test_mqttsn_cancel(void); 21 | void thread_test_mqttsn_exitmsg(void); 22 | 23 | #endif /* THREAD_TEST_MQTTSN_H_ */ 24 | -------------------------------------------------------------------------------- /utils/sensim/src/thread_tick.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifdef _MSC_VER 16 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 17 | #else 18 | #include /* uint8_t ... uint64_t */ 19 | #endif 20 | 21 | #include "time.h" 22 | #include /* assert */ 23 | #include /* printf */ 24 | #include "os_port.h" 25 | #include "thread_state.h" 26 | #include "tick_counter.h" 27 | 28 | #ifdef _MSC_VER 29 | #pragma warning (disable:4996) // This function may be unsafe. 30 | #endif 31 | 32 | #ifndef NDPRINTF 33 | #define dprintf(...) printf(__VA_ARGS__) 34 | #else 35 | #define dprintf(...) 36 | #endif 37 | 38 | //-------------------------------------------- 39 | static volatile thread_state_t thread_state; 40 | 41 | //-------------------------------------------- 42 | static void thread_run(void *param) 43 | { 44 | clock_t ticks_start; 45 | clock_t ticks_now; 46 | uint32_t counter; 47 | 48 | ticks_start = clock(); 49 | for (;;) 50 | { 51 | if ((ticks_now = clock()) - ticks_start > TICK_TIME) 52 | { 53 | ticks_start = ticks_now; 54 | tick_counter_increment(); 55 | if (tick_counter_get_mode()) 56 | { 57 | counter = tick_counter_get(); 58 | if (!(counter % TICKS_IN_SECOND)) 59 | { 60 | printf("secs: %d\n", counter / TICKS_IN_SECOND); 61 | } 62 | } 63 | } 64 | if (thread_state == THREAD_STAYING) 65 | { 66 | break; 67 | } 68 | sched_yield(); 69 | } 70 | 71 | thread_state = THREAD_STOPPED; 72 | } 73 | 74 | //-------------------------------------------- 75 | #ifdef WIN32 76 | static unsigned int __stdcall thread_launcher(void *param) 77 | { 78 | thread_run(param); 79 | return 0; 80 | } 81 | #else 82 | static void *thread_launcher(void *param) 83 | { 84 | thread_run(param); 85 | return NULL; 86 | } 87 | #endif 88 | 89 | //-------------------------------------------- 90 | void thread_tick_start(void) 91 | { 92 | pthread_t thread; 93 | void *param = NULL; 94 | 95 | thread_state = THREAD_RUNNING; 96 | thread_begin(thread_launcher, param, &thread); 97 | } 98 | 99 | //-------------------------------------------- 100 | void thread_tick_stop(void) 101 | { 102 | if (thread_state == THREAD_RUNNING) 103 | thread_state = THREAD_STAYING; 104 | while (thread_state != THREAD_STOPPED) 105 | sleep(10); 106 | } 107 | -------------------------------------------------------------------------------- /utils/sensim/src/thread_tick.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef THREAD_TICK_H_ 16 | #define THREAD_TICK_H_ 17 | 18 | void thread_tick_start(void); 19 | void thread_tick_stop(void); 20 | 21 | #endif /* THREAD_TICK_H_ */ 22 | -------------------------------------------------------------------------------- /utils/sensim/src/thread_udp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifdef _MSC_VER 16 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 17 | #else 18 | #include /* uint8_t ... uint64_t */ 19 | #endif 20 | 21 | #include /* assert */ 22 | #include 23 | #include "os_port.h" 24 | #include "msg_udp_mqtt.h" 25 | #include "msg_mqtt_udp.h" 26 | #include "thread_state.h" 27 | 28 | #ifdef _MSC_VER 29 | #pragma warning (disable:4996) // This function may be unsafe. 30 | #endif 31 | 32 | #ifndef NDPRINTF 33 | #include 34 | #define dprintf(...) printf(__VA_ARGS__) 35 | #ifdef WIN32 36 | #define print_error_socket(line) \ 37 | do { \ 38 | char output[1024]; \ 39 | LPTSTR s = NULL; \ 40 | FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \ 41 | NULL, \ 42 | WSAGetLastError(), \ 43 | 0, \ 44 | (LPSTR)&s, \ 45 | 0, \ 46 | NULL); \ 47 | fprintf(stderr, "Socket Error on line %d: %s\n", line, s); \ 48 | sprintf((char *)&output, "Socket Error on line %d: %s\n", line, s); \ 49 | LocalFree(s); \ 50 | OutputDebugStringA(output); \ 51 | } while (0) 52 | #else 53 | #include /* errno */ 54 | #include /* strerror */ 55 | #define print_error_socket(line) fprintf(stderr, "Socket Error on line %d: %s\n", line, strerror(errno)) 56 | #endif 57 | #else 58 | #define dprintf(...) 59 | #define print_error_socket(...) 60 | #endif 61 | 62 | #define MTU 110 63 | //-------------------------------------------- 64 | static SOCKET sock; 65 | static char sock_buf[MTU]; 66 | static volatile thread_state_t thread_state; 67 | 68 | static struct sockaddr_in addr; 69 | //-------------------------------------------- 70 | void thread_udp_addr_setup(unsigned short port, unsigned long address) 71 | { 72 | addr.sin_family = AF_INET; 73 | addr.sin_port = htons(port); 74 | addr.sin_addr.s_addr = address; 75 | } 76 | 77 | //-------------------------------------------- 78 | static void thread_run(void *param) 79 | { 80 | for (;;) 81 | { 82 | fd_set rd; 83 | int res; 84 | struct timeval tv = { 0, 10000 }; /* 0.01 sec */ 85 | 86 | FD_ZERO(&rd); 87 | FD_SET(sock, &rd); 88 | 89 | res = select((int)sock + 1, &rd, NULL, NULL, &tv); 90 | 91 | if (res < 0) 92 | { 93 | print_error_socket(__LINE__); 94 | break; 95 | } 96 | 97 | if (res == 0) 98 | { 99 | // timeout 100 | msg_mqtt_udp_t *ms; 101 | if ((ms = msg_mqtt_udp_get_first()) != NULL) 102 | { 103 | if (sendto(sock, ms->msg_buf, (int)ms->msg_cnt, 0, (const struct sockaddr *)&addr, sizeof(addr)) < 0) 104 | print_error_socket(__LINE__); 105 | msg_mqtt_udp_remove(ms); 106 | } 107 | if (thread_state == THREAD_STAYING) 108 | break; 109 | continue; 110 | } 111 | 112 | if (FD_ISSET(sock, &rd)) 113 | { 114 | struct sockaddr_in addr_in; 115 | int recv_cnt; 116 | int len = sizeof(addr); 117 | 118 | recv_cnt = recvfrom(sock, sock_buf, sizeof(sock_buf), 0, (struct sockaddr *)&addr_in, &len); 119 | if (recv_cnt > 0) 120 | msg_udp_mqtt_add_packet(&addr_in, sock_buf, recv_cnt); 121 | if (recv_cnt < 0) 122 | print_error_socket(__LINE__); 123 | } 124 | } 125 | 126 | if (sock != INVALID_SOCKET) 127 | closesocket(sock); 128 | 129 | thread_state = THREAD_STOPPED; 130 | } 131 | 132 | //-------------------------------------------- 133 | #ifdef WIN32 134 | static unsigned int __stdcall thread_launcher(void *param) 135 | { 136 | thread_run(param); 137 | return 0; 138 | } 139 | #else 140 | static void *thread_launcher(void *param) 141 | { 142 | thread_run(param); 143 | return NULL; 144 | } 145 | #endif 146 | 147 | //-------------------------------------------- 148 | int thread_udp_start(void) 149 | { 150 | pthread_t thread; 151 | void *param = NULL; 152 | 153 | if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 154 | { 155 | print_error_socket(__LINE__); 156 | return -1; 157 | } 158 | 159 | thread_state = THREAD_RUNNING; 160 | thread_begin(thread_launcher, param, &thread); 161 | return 0; 162 | } 163 | 164 | //-------------------------------------------- 165 | void thread_udp_stop(void) 166 | { 167 | if (thread_state == THREAD_RUNNING) 168 | thread_state = THREAD_STAYING; 169 | while (thread_state != THREAD_STOPPED) 170 | sleep(10); 171 | } 172 | -------------------------------------------------------------------------------- /utils/sensim/src/thread_udp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef THREAD_UDP_H_ 16 | #define THREAD_UDP_H_ 17 | 18 | int thread_udp_start(void); 19 | void thread_udp_stop(void); 20 | void thread_udp_addr_setup(unsigned short port, unsigned long address); 21 | 22 | #endif /* THREAD_UDP_H_ */ 23 | -------------------------------------------------------------------------------- /utils/sensim/src/tick_counter.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifdef _MSC_VER 16 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 17 | #else 18 | #include /* uint8_t ... uint64_t */ 19 | #endif 20 | 21 | #include /* assert */ 22 | #include "time.h" 23 | #include "os_port.h" 24 | #include "tick_counter.h" 25 | 26 | #ifdef _MSC_VER 27 | #pragma warning (disable:4996) // This function may be unsafe. 28 | #endif 29 | 30 | #ifndef NDPRINTF 31 | #include 32 | #define dprintf(...) printf(__VA_ARGS__) 33 | #else 34 | #define dprintf(...) 35 | #endif 36 | 37 | //-------------------------------------------- 38 | static struct tick_counter 39 | { 40 | pthread_mutex_t mutex; 41 | uint32_t counter; 42 | int mqtt_mode; 43 | int mqttsn_mode; 44 | } tick_counter; 45 | 46 | //-------------------------------------------- 47 | void tick_counter_init(void) 48 | { 49 | pthread_mutex_init(&tick_counter.mutex, NULL); 50 | tick_counter.counter = 0; 51 | tick_counter.mqtt_mode = 0; 52 | tick_counter.mqttsn_mode = 0; 53 | } 54 | 55 | //-------------------------------------------- 56 | void tick_counter_increment(void) 57 | { 58 | pthread_mutex_lock(&tick_counter.mutex); 59 | ++tick_counter.counter; 60 | pthread_mutex_unlock(&tick_counter.mutex); 61 | } 62 | 63 | //-------------------------------------------- 64 | uint32_t tick_counter_get(void) 65 | { 66 | uint32_t counter; 67 | pthread_mutex_lock(&tick_counter.mutex); 68 | counter = tick_counter.counter; 69 | pthread_mutex_unlock(&tick_counter.mutex); 70 | return counter; 71 | } 72 | 73 | //-------------------------------------------- 74 | void tick_counter_set_mqtt_mode(int mode) 75 | { 76 | pthread_mutex_lock(&tick_counter.mutex); 77 | tick_counter.mqtt_mode = mode; 78 | // printf("mode=%d, tick_counter.mode=%d\n", mode, tick_counter.mode); 79 | pthread_mutex_unlock(&tick_counter.mutex); 80 | } 81 | 82 | //-------------------------------------------- 83 | void tick_counter_set_mqttsn_mode(int mode) 84 | { 85 | pthread_mutex_lock(&tick_counter.mutex); 86 | tick_counter.mqttsn_mode = mode; 87 | // printf("mode=%d, tick_counter.mode=%d\n", mode, tick_counter.mode); 88 | pthread_mutex_unlock(&tick_counter.mutex); 89 | } 90 | 91 | //-------------------------------------------- 92 | int tick_counter_get_mode(void) 93 | { 94 | int mode; 95 | pthread_mutex_lock(&tick_counter.mutex); 96 | mode = tick_counter.mqtt_mode + tick_counter.mqttsn_mode; 97 | pthread_mutex_unlock(&tick_counter.mutex); 98 | return mode; 99 | } 100 | 101 | //-------------------------------------------- 102 | void tick_counter_destroy(void) 103 | { 104 | pthread_mutex_destroy(&tick_counter.mutex); 105 | } 106 | 107 | //-------------------------------------------- 108 | uint32_t alarm_timer_check(alarm_timer_t *alarm_timer, uint32_t interval) 109 | { 110 | uint32_t current_counter; 111 | 112 | current_counter = tick_counter_get(); 113 | if (alarm_timer->running == 0) 114 | { 115 | alarm_timer->running = 1; 116 | alarm_timer->start_counter = current_counter; 117 | } 118 | else 119 | { 120 | if ((current_counter - alarm_timer->start_counter) >= (interval * TICKS_IN_SECOND)) 121 | { 122 | alarm_timer->running = 0; 123 | return 1; 124 | } 125 | } 126 | return 0; 127 | } 128 | -------------------------------------------------------------------------------- /utils/sensim/src/tick_counter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef TICK_COUNTER_H_ 16 | #define TICK_COUNTER_H_ 17 | 18 | #define TICKS_IN_SECOND (uint32_t)(10) 19 | #define TICK_TIME (uint32_t)(CLOCKS_PER_SEC / TICKS_IN_SECOND) 20 | 21 | //-------------------------------------------- 22 | typedef struct alarm_timer 23 | { 24 | uint32_t start_counter; 25 | uint32_t running; 26 | } alarm_timer_t; 27 | 28 | //-------------------------------------------- 29 | void tick_counter_init(void); 30 | void tick_counter_increment(void); 31 | uint32_t tick_counter_get(void); 32 | void tick_counter_destroy(void); 33 | void tick_counter_set_mqtt_mode(int mode); 34 | void tick_counter_set_mqttsn_mode(int mode); 35 | int tick_counter_get_mode(void); 36 | uint32_t alarm_timer_check(alarm_timer_t *timer_name, uint32_t interval); 37 | 38 | #endif /* TICK_COUNTER_H_ */ 39 | -------------------------------------------------------------------------------- /utils/sensim/src/utf8.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "utf8.h" 16 | 17 | //-------------------------------------------- 18 | uint32_t utf8_strlen(char *utf8str, uint32_t size) 19 | { 20 | uint32_t byte_cnt, char_cnt; 21 | for (byte_cnt = 0, char_cnt = 0; byte_cnt < size; byte_cnt++) 22 | { 23 | if ((utf8str[byte_cnt] & 0xc0) != 0x80) 24 | char_cnt++; 25 | } 26 | return char_cnt; 27 | } 28 | -------------------------------------------------------------------------------- /utils/sensim/src/utf8.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef UTF8_H_ 16 | #define UTF8_H_ 17 | 18 | #ifdef _MSC_VER 19 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 20 | #else 21 | #include /* uint8_t ... uint64_t */ 22 | #include /* size_t */ 23 | #endif 24 | 25 | uint32_t utf8_strlen(char *utf8str, uint32_t size); 26 | 27 | #endif /* UTF8_H_ */ 28 | -------------------------------------------------------------------------------- /utils/sensimgui/.gitignore: -------------------------------------------------------------------------------- 1 | # Visual Studio 2005 files 2 | *.exe 3 | *.dll 4 | *.suo 5 | *.user 6 | *.ncb 7 | *.txt 8 | Debug/ 9 | Release/ 10 | 11 | # Visual Studio 2010 files 12 | *.[Oo]bj 13 | *.user 14 | *.aps 15 | *.pch 16 | *.vspscc 17 | *.vssscc 18 | *_i.c 19 | *_p.c 20 | *.ncb 21 | *.suo 22 | *.tlb 23 | *.tlh 24 | *.bak 25 | *.[Cc]ache 26 | *.ilk 27 | *.log 28 | *.lib 29 | *.sbr 30 | *.sdf 31 | *.opensdf 32 | *.unsuccessfulbuild 33 | ipch/ 34 | obj/ 35 | [Bb]in 36 | [Dd]ebug*/ 37 | [Rr]elease*/ 38 | [Pp]ublish 39 | Ankh.NoLoad 40 | -------------------------------------------------------------------------------- /utils/sensimgui/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013-2018 Vladimir Alemasov 2 | All rights reserved 3 | 4 | This program and the accompanying materials are distributed under 5 | the terms of GNU General Public License version 2 6 | as published by the Free Software Foundation. 7 | 8 | This program is distributed in the hope that it will be useful, 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | GNU General Public License for more details. -------------------------------------------------------------------------------- /utils/sensimgui/sensimgui.sln: -------------------------------------------------------------------------------- 1 | Microsoft Visual Studio Solution File, Format Version 11.00 2 | # Visual Studio 2010 3 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sensimgui", "sensimgui.vcxproj", "{D558BAA2-3D8F-4A20-ADEF-BAF61E17708F}" 4 | EndProject 5 | Global 6 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 7 | Debug|Win32 = Debug|Win32 8 | Release|Win32 = Release|Win32 9 | EndGlobalSection 10 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 11 | {D558BAA2-3D8F-4A20-ADEF-BAF61E17708F}.Debug|Win32.ActiveCfg = Debug|Win32 12 | {D558BAA2-3D8F-4A20-ADEF-BAF61E17708F}.Debug|Win32.Build.0 = Debug|Win32 13 | {D558BAA2-3D8F-4A20-ADEF-BAF61E17708F}.Release|Win32.ActiveCfg = Release|Win32 14 | {D558BAA2-3D8F-4A20-ADEF-BAF61E17708F}.Release|Win32.Build.0 = Release|Win32 15 | EndGlobalSection 16 | GlobalSection(SolutionProperties) = preSolution 17 | HideSolutionNode = FALSE 18 | EndGlobalSection 19 | EndGlobal 20 | -------------------------------------------------------------------------------- /utils/sensimgui/sensimgui.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {bad0b50b-1b1b-4341-8522-ff1571d8ac7a} 6 | cpp;c;cxx;rc;def;r;odl;idl;hpj;bat 7 | 8 | 9 | {c9666d52-fca6-44b5-aa58-a98d38f20dc2} 10 | h;hpp;hxx;hm;inl 11 | 12 | 13 | {66e8826c-fd5e-436f-801f-3882d598899b} 14 | ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | Source Files 29 | 30 | 31 | Source Files 32 | 33 | 34 | Source Files 35 | 36 | 37 | Source Files 38 | 39 | 40 | Source Files 41 | 42 | 43 | Source Files 44 | 45 | 46 | Source Files 47 | 48 | 49 | 50 | 51 | Source Files 52 | 53 | 54 | 55 | 56 | Header Files 57 | 58 | 59 | Header Files 60 | 61 | 62 | Header Files 63 | 64 | 65 | Header Files 66 | 67 | 68 | Header Files 69 | 70 | 71 | Header Files 72 | 73 | 74 | Header Files 75 | 76 | 77 | Header Files 78 | 79 | 80 | Header Files 81 | 82 | 83 | Header Files 84 | 85 | 86 | Header Files 87 | 88 | 89 | Header Files 90 | 91 | 92 | Header Files 93 | 94 | 95 | Header Files 96 | 97 | 98 | 99 | 100 | Resource Files 101 | 102 | 103 | -------------------------------------------------------------------------------- /utils/sensimgui/src/MainDlg.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homewsn/whsnbg/7767d3c72b03299286a851e4e8e59e9709a5485c/utils/sensimgui/src/MainDlg.h -------------------------------------------------------------------------------- /utils/sensimgui/src/config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef CONFIG_H_ 16 | #define CONFIG_H_ 17 | 18 | #define SENSOR_DATA 19 | //** internal message 20 | #define WM_MQTTSN_MSG WM_USER + 500 21 | 22 | #endif /* CONFIG_H_ */ 23 | -------------------------------------------------------------------------------- /utils/sensimgui/src/list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "list.h" 16 | #include /* assert */ 17 | 18 | 19 | //-------------------------------------------- 20 | void list_init(list_t **list) 21 | { 22 | assert(list != NULL); 23 | 24 | *list = NULL; 25 | } 26 | 27 | //-------------------------------------------- 28 | list_t *list_head(list_t **list) 29 | { 30 | assert(list != NULL); 31 | 32 | return *list; 33 | } 34 | 35 | //-------------------------------------------- 36 | list_t *list_remove(list_t **list, list_t *item) 37 | { 38 | list_t *find; 39 | list_t *prev; 40 | list_t *next; 41 | 42 | assert(list != NULL); 43 | assert(item != NULL); 44 | 45 | if (*list == NULL) 46 | return NULL; 47 | prev = NULL; 48 | for (find = *list; find != NULL; find = find->next) 49 | { 50 | if (find == item) 51 | { 52 | next = find->next; 53 | if (prev == NULL) 54 | *list = next; 55 | else 56 | prev->next = next; 57 | find->next = NULL; 58 | return next; 59 | } 60 | prev = find; 61 | } 62 | return NULL; 63 | } 64 | 65 | //-------------------------------------------- 66 | list_t *list_add_item(list_t **list, list_t *item) 67 | { 68 | list_t *last; 69 | 70 | assert(list != NULL); 71 | assert(item != NULL); 72 | 73 | item->next = NULL; 74 | 75 | if (*list == NULL) 76 | *list = item; 77 | else 78 | { 79 | for (last = *list; last->next != NULL; last = last->next); 80 | last->next = item; 81 | } 82 | 83 | return item; 84 | } 85 | 86 | //-------------------------------------------- 87 | size_t list_get_length(list_t **list) 88 | { 89 | list_t *item; 90 | size_t cnt = 0; 91 | 92 | assert(list != NULL); 93 | 94 | for (item = *list, cnt = 0; item != NULL; item = item->next, ++cnt); 95 | return cnt; 96 | } 97 | 98 | //-------------------------------------------- 99 | list_t *list_next(list_t *item) 100 | { 101 | return item == NULL ? NULL : item->next; 102 | } 103 | -------------------------------------------------------------------------------- /utils/sensimgui/src/list.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef LIST_H_ 16 | #define LIST_H_ 17 | 18 | #ifdef _MSC_VER 19 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 20 | #include /* NULL */ 21 | #else 22 | #include /* uint8_t ... uint64_t */ 23 | #include /* size_t */ 24 | #endif 25 | 26 | 27 | //-------------------------------------------- 28 | typedef struct list 29 | { 30 | struct list *next; 31 | } list_t; 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | //-------------------------------------------- 37 | void list_init(list_t **list); 38 | list_t *list_head(list_t **list); 39 | list_t *list_remove(list_t **list, list_t *item); 40 | list_t *list_add_item(list_t **list, list_t *item); 41 | size_t list_get_length(list_t **list); 42 | list_t *list_next(list_t *item); 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif /* LIST_H_ */ 48 | -------------------------------------------------------------------------------- /utils/sensimgui/src/main.aps: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homewsn/whsnbg/7767d3c72b03299286a851e4e8e59e9709a5485c/utils/sensimgui/src/main.aps -------------------------------------------------------------------------------- /utils/sensimgui/src/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "stdafx.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "resource.h" 23 | #include "MainDlg.h" 24 | 25 | CAppModule _Module; 26 | 27 | int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpstrCmdLine, int nCmdShow) 28 | { 29 | WSADATA data; 30 | 31 | ::CoInitialize(NULL); 32 | 33 | AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES); 34 | 35 | WSAStartup(MAKEWORD(2, 2), &data); 36 | 37 | CMessageLoop theLoop; 38 | _Module.Init(NULL, hInstance); 39 | _Module.AddMessageLoop(&theLoop); 40 | 41 | int nRet = 0; 42 | { 43 | CMainDlg dlgMain; 44 | dlgMain.Create(NULL); 45 | dlgMain.ShowWindow(nCmdShow); 46 | int nRet = theLoop.Run(); 47 | } 48 | 49 | _Module.RemoveMessageLoop(); 50 | _Module.Term(); 51 | 52 | WSACleanup(); 53 | 54 | ::CoUninitialize(); 55 | 56 | return nRet; 57 | } 58 | -------------------------------------------------------------------------------- /utils/sensimgui/src/msg_mqtt_udp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "msg_mqtt_udp.h" 16 | #include /* memset */ 17 | 18 | static msgqueue_t queue; 19 | 20 | //-------------------------------------------- 21 | void msg_mqtt_udp_init(void) 22 | { 23 | msg_init(&queue); 24 | } 25 | 26 | //-------------------------------------------- 27 | msg_mqtt_udp_t *msg_mqtt_udp_new(void) 28 | { 29 | msg_mqtt_udp_t *ms; 30 | ms = (msg_mqtt_udp_t *)malloc(sizeof(msg_mqtt_udp_t)); 31 | memset(ms, 0, sizeof(msg_mqtt_udp_t)); 32 | return ms; 33 | } 34 | 35 | //-------------------------------------------- 36 | void msg_mqtt_udp_add(msg_mqtt_udp_t *ms) 37 | { 38 | msg_add(&queue, (msg_t *)ms); 39 | } 40 | 41 | //-------------------------------------------- 42 | void msg_mqtt_udp_remove(msg_mqtt_udp_t *ms) 43 | { 44 | msg_remove(&queue, (msg_t *)ms); 45 | if (ms->msg_buf != NULL) 46 | free(ms->msg_buf); 47 | free(ms); 48 | } 49 | 50 | //-------------------------------------------- 51 | msg_mqtt_udp_t* msg_mqtt_udp_get_first(void) 52 | { 53 | return (msg_mqtt_udp_t *)msg_get_first(&queue); 54 | } 55 | 56 | //-------------------------------------------- 57 | void msg_mqtt_udp_destroy(void) 58 | { 59 | msg_destroy(&queue); 60 | } 61 | 62 | //-------------------------------------------- 63 | void msg_mqtt_udp_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size) 64 | { 65 | msg_mqtt_udp_t *ms = msg_mqtt_udp_new(); 66 | memcpy(&ms->addr, addr, sizeof(struct sockaddr_in)); 67 | ms->msg_buf = buf; // external malloc 68 | ms->msg_cnt = size; 69 | msg_mqtt_udp_add(ms); 70 | } 71 | -------------------------------------------------------------------------------- /utils/sensimgui/src/msg_mqtt_udp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef MSG_MQTT_UDP_H_ 16 | #define MSG_MQTT_UDP_H_ 17 | 18 | #include "msgs.h" 19 | 20 | 21 | //-------------------------------------------- 22 | typedef struct msg_mqtt_udp 23 | { 24 | msg_t msg; 25 | unsigned char *msg_buf; 26 | size_t msg_cnt; 27 | struct sockaddr_in addr; 28 | } msg_mqtt_udp_t; 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | //-------------------------------------------- 34 | void msg_mqtt_udp_init(void); 35 | msg_mqtt_udp_t *msg_mqtt_udp_new(void); 36 | void msg_mqtt_udp_add(msg_mqtt_udp_t *ms); 37 | void msg_mqtt_udp_remove(msg_mqtt_udp_t *ms); 38 | msg_mqtt_udp_t *msg_mqtt_udp_get_first(void); 39 | #define msg_mqtt_udp_close() msg_close(a) 40 | void msg_mqtt_udp_destroy(void); 41 | 42 | void msg_mqtt_udp_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size); 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif /* MSG_MQTT_UDP_H_ */ 48 | -------------------------------------------------------------------------------- /utils/sensimgui/src/msg_udp_mqtt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include "msg_udp_mqtt.h" 16 | #include /* memset, memcpy */ 17 | 18 | static msgqueue_t queue; 19 | 20 | //-------------------------------------------- 21 | void msg_udp_mqtt_init(void) 22 | { 23 | msg_init(&queue); 24 | } 25 | 26 | //-------------------------------------------- 27 | msg_udp_mqtt_t *msg_udp_mqtt_new(void) 28 | { 29 | msg_udp_mqtt_t *msg; 30 | 31 | msg = (msg_udp_mqtt_t *)malloc(sizeof(msg_udp_mqtt_t)); 32 | memset(msg, 0, sizeof(msg_udp_mqtt_t)); 33 | return msg; 34 | } 35 | 36 | //-------------------------------------------- 37 | void msg_udp_mqtt_add(msg_udp_mqtt_t *msg) 38 | { 39 | msg_add(&queue, (msg_t *)msg); 40 | } 41 | 42 | //-------------------------------------------- 43 | void msg_udp_mqtt_remove(msg_udp_mqtt_t *msg) 44 | { 45 | msg_remove(&queue, (msg_t *)msg); 46 | if (msg->msg_buf != NULL) 47 | free(msg->msg_buf); 48 | free(msg); 49 | } 50 | 51 | //-------------------------------------------- 52 | msg_udp_mqtt_t *msg_udp_mqtt_get_first(void) 53 | { 54 | return (msg_udp_mqtt_t *)msg_get_first(&queue); 55 | } 56 | 57 | //-------------------------------------------- 58 | void msg_udp_mqtt_destroy(void) 59 | { 60 | msg_destroy(&queue); 61 | } 62 | 63 | //-------------------------------------------- 64 | void msg_udp_mqtt_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size) 65 | { 66 | msg_udp_mqtt_t *msg; 67 | 68 | msg = msg_udp_mqtt_new(); 69 | memcpy(&msg->addr, addr, sizeof(struct sockaddr_in)); 70 | msg->msg_buf = (unsigned char *)malloc(size); 71 | msg->msg_cnt = size; 72 | memcpy(msg->msg_buf, buf, size); 73 | msg_udp_mqtt_add(msg); 74 | } 75 | -------------------------------------------------------------------------------- /utils/sensimgui/src/msg_udp_mqtt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef MSG_UDP_MQTT_H_ 16 | #define MSG_UDP_MQTT_H_ 17 | 18 | #include "msgs.h" 19 | 20 | 21 | //-------------------------------------------- 22 | typedef struct msg_udp_mqtt 23 | { 24 | msg_t msg; 25 | char *msg_buf; 26 | size_t msg_cnt; 27 | struct sockaddr_in addr; 28 | } msg_udp_mqtt_t; 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | //-------------------------------------------- 34 | void msg_udp_mqtt_init(void); 35 | msg_udp_mqtt_t *msg_udp_mqtt_new(void); 36 | void msg_udp_mqtt_add(msg_udp_mqtt_t *msg); 37 | void msg_udp_mqtt_remove(msg_udp_mqtt_t *msg); 38 | msg_udp_mqtt_t *msg_udp_mqtt_get_first(void); 39 | #define msg_udp_mqtt_close() msg_close(a) 40 | void msg_udp_mqtt_destroy(void); 41 | 42 | void msg_udp_mqtt_add_packet(struct sockaddr_in *addr, unsigned char *buf, size_t size); 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif /* MSG_UDP_MQTT_H_ */ 48 | -------------------------------------------------------------------------------- /utils/sensimgui/src/msgs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef MSGS_H_ 16 | #define MSGS_H_ 17 | 18 | #include "os_port.h" 19 | 20 | //-------------------------------------------- 21 | typedef struct msg 22 | { 23 | struct msg *next; 24 | } msg_t; 25 | 26 | 27 | //-------------------------------------------- 28 | typedef struct msgqueue 29 | { 30 | msg_t *msglist; 31 | pthread_mutex_t mutex; 32 | } msgqueue_t; 33 | 34 | void msg_init(msgqueue_t *queue); 35 | void msg_add(msgqueue_t *queue, msg_t *msg); 36 | void msg_remove(msgqueue_t *queue, msg_t *msg); 37 | msg_t* msg_get_first(msgqueue_t *queue); 38 | #define msg_close(a) 39 | void msg_destroy(msgqueue_t *queue); 40 | 41 | 42 | //-------------------------------------------- 43 | typedef struct msgqueue_cond 44 | { 45 | msgqueue_t msgqueue; 46 | pthread_cond_t cond; 47 | } msgqueue_cond_t; 48 | 49 | void msg_cond_init(msgqueue_cond_t *queue); 50 | void msg_cond_add(msgqueue_cond_t *queue, msg_t *msg); 51 | void msg_cond_remove(msgqueue_cond_t *queue, msg_t *msg); 52 | msg_t* msg_cond_get_first(msgqueue_cond_t *queue); 53 | void msg_cond_close(msgqueue_cond_t *queue); 54 | void msg_cond_destroy(msgqueue_cond_t *queue); 55 | 56 | 57 | //-------------------------------------------- 58 | typedef struct msggap 59 | { 60 | void *msg; 61 | int request; 62 | pthread_mutex_t mutex; 63 | pthread_cond_t cond; 64 | } msggap_t; 65 | 66 | void msggap_init(msggap_t *gap); 67 | void msggap_request(msggap_t *gap); 68 | int msggap_get_request(msggap_t *gap); 69 | void msggap_reply(msggap_t *gap); 70 | void msggap_close(msggap_t *gap); 71 | void msggap_destroy(msggap_t *gap); 72 | 73 | #endif /* MSGS_H_ */ 74 | -------------------------------------------------------------------------------- /utils/sensimgui/src/res/main.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homewsn/whsnbg/7767d3c72b03299286a851e4e8e59e9709a5485c/utils/sensimgui/src/res/main.ico -------------------------------------------------------------------------------- /utils/sensimgui/src/resource.h: -------------------------------------------------------------------------------- 1 | //{{NO_DEPENDENCIES}} 2 | // Microsoft Visual C++ generated include file. 3 | // Used by main.rc 4 | // 5 | #define IDD_ABOUTBOX 100 6 | #define IDR_MAINFRAME 128 7 | #define IDD_MAINDLG 129 8 | #define IDC_DVRLOGIN 1000 9 | #define IDC_DVRLOGOUT 1001 10 | #define IDC_DVRSTARTLIVESTREAM 1002 11 | #define IDC_DVRSTOPLIVESTREAM 1003 12 | #define IDC_CAMERA 1004 13 | #define IDC_SDKINIT 1005 14 | #define IDC_TXPACKET 1005 15 | #define IDC_PORT 1005 16 | #define IDC_SDKUNINIT 1006 17 | #define IDC_SEND 1006 18 | #define IDC_CONNECT 1006 19 | #define IDC_DVRGET 1007 20 | #define IDC_DVRGETCONFIG 1007 21 | #define IDC_RXPACKET 1007 22 | #define IDC_PACKETS 1007 23 | #define IDC_STOP 1008 24 | #define IDC_PUBLISH 1008 25 | #define IDC_HOST 1009 26 | #define IDC_CLIENTID 1010 27 | #define IDC_WILL 1011 28 | #define IDC_WILLRETAIN 1012 29 | #define IDC_DURATION 1013 30 | #define IDC_CLEANSESSION 1014 31 | #define IDC_WILLTOPIC 1015 32 | #define IDC_WILLMESSAGE 1016 33 | #define IDC_WILLQOS 1017 34 | #define IDC_STATICWILLQOS 1018 35 | #define IDC_TOPIC 1019 36 | #define IDC_MESSAGE 1020 37 | #define IDC_RETAIN 1021 38 | #define IDC_STATICQOS 1022 39 | #define IDC_QOS 1023 40 | #define IDC_WILLTOPICUPDATE 1024 41 | #define IDC_WILLMESSAGEUPDATE 1025 42 | #define IDC_REGISTER 1026 43 | #define IDC_REGTOPICS 1027 44 | #define IDC_DUR0 1028 45 | #define IDC_STATICHOST 1029 46 | #define IDC_STATICPORT 1030 47 | #define IDC_STATICCLIENTID 1031 48 | #define IDC_STATICDURATION 1032 49 | #define IDC_STATICWILLTOPIC 1033 50 | #define IDC_STATICWILLMESSAGE 1034 51 | #define IDC_STATICTOPIC 1035 52 | #define IDC_STATICREGTOPICS 1036 53 | #define IDC_STATICMESSAGE 1037 54 | #define IDC_LOST 1040 55 | #define IDC_SUBREG 1042 56 | #define IDC_UNSUBSCRIBE 1043 57 | #define IDC_UNSUBREG 1043 58 | #define IDC_SUB 1044 59 | #define IDC_UNSUB 1045 60 | 61 | // Next default values for new objects 62 | // 63 | #ifdef APSTUDIO_INVOKED 64 | #ifndef APSTUDIO_READONLY_SYMBOLS 65 | #define _APS_NEXT_RESOURCE_VALUE 201 66 | #define _APS_NEXT_COMMAND_VALUE 32772 67 | #define _APS_NEXT_CONTROL_VALUE 1041 68 | #define _APS_NEXT_SYMED_VALUE 101 69 | #endif 70 | #endif 71 | -------------------------------------------------------------------------------- /utils/sensimgui/src/sensor_data.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homewsn/whsnbg/7767d3c72b03299286a851e4e8e59e9709a5485c/utils/sensimgui/src/sensor_data.c -------------------------------------------------------------------------------- /utils/sensimgui/src/sensor_data.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2015 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef SENSOR_DATA_H_ 16 | #define SENSOR_DATA_H_ 17 | 18 | #ifdef _MSC_VER 19 | #include "stdint_msc.h" /* uint8_t ... uint64_t */ 20 | #else 21 | #include /* uint8_t ... uint64_t */ 22 | #include /* size_t */ 23 | #endif 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | //-------------------------------------------- 29 | void encode_mqttsn_sensor_data(uint8_t **buf, size_t *size, const char *data, size_t data_len); 30 | void decode_mqttsn_sensor_data(char **buf, size_t *size, uint8_t *data); 31 | void parse_mqttsn_topic_name_to_mysql_query(char *name, size_t name_len, uint8_t *data); 32 | void parse_mqtt_topic_name_to_mysql_query(char *name, size_t name_len, char *data, size_t data_len); 33 | int check_for_actuators_id_topic(char *name, size_t name_len); 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | 38 | #endif /* SENSOR_DATA_H_ */ 39 | -------------------------------------------------------------------------------- /utils/sensimgui/src/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // udpcheck.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | -------------------------------------------------------------------------------- /utils/sensimgui/src/stdafx.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homewsn/whsnbg/7767d3c72b03299286a851e4e8e59e9709a5485c/utils/sensimgui/src/stdafx.h -------------------------------------------------------------------------------- /utils/sensimgui/src/thread_udp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #include /* assert */ 16 | 17 | #include "config.h" 18 | #include "os_port.h" 19 | #include "msg_udp_mqtt.h" 20 | #include "msg_mqtt_udp.h" 21 | 22 | #ifdef _MSC_VER 23 | #pragma warning (disable:4996) 24 | #endif 25 | 26 | #include 27 | #define print_error_socket(line) \ 28 | do { \ 29 | char output[1024]; \ 30 | LPTSTR s = NULL; \ 31 | FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \ 32 | NULL, \ 33 | WSAGetLastError(), \ 34 | 0, \ 35 | (LPTSTR)&s, \ 36 | 0, \ 37 | NULL); \ 38 | fprintf(stderr, "Socket Error on line %d: %s\n", line, s); \ 39 | sprintf((char *)&output, "Socket Error on line %d: %s\n", line, s); \ 40 | LocalFree(s); \ 41 | OutputDebugStringA(output); \ 42 | } while (0) 43 | 44 | 45 | //-------------------------------------------- 46 | 47 | static SOCKET sock; 48 | 49 | #define MTU 100 50 | static char sock_buf[MTU]; 51 | static volatile int stop_flag = 0; 52 | 53 | 54 | //-------------------------------------------- 55 | //** main thread 56 | 57 | //-------------------------------------------- 58 | static void thread_run(void *param) 59 | { 60 | HWND hWnd = (HWND)param; 61 | 62 | for (;;) 63 | { 64 | fd_set rd; 65 | int res; 66 | struct timeval tv = { 0, 10000 }; /* 0.01 sec */ 67 | 68 | FD_ZERO(&rd); 69 | FD_SET(sock, &rd); 70 | 71 | res = select((int)sock + 1, &rd, NULL, NULL, &tv); 72 | 73 | if (res < 0) 74 | { 75 | print_error_socket(__LINE__); 76 | assert(0); 77 | break; 78 | } 79 | 80 | if (res == 0) 81 | { 82 | // timeout 83 | msg_mqtt_udp_t *ms; 84 | if ((ms = msg_mqtt_udp_get_first()) != NULL) 85 | { 86 | sendto(sock, (const char *)ms->msg_buf, (int)ms->msg_cnt, 0, (const struct sockaddr *)&ms->addr, sizeof(ms->addr)); 87 | msg_mqtt_udp_remove(ms); 88 | } 89 | if (stop_flag == 1) 90 | break; 91 | continue; 92 | } 93 | 94 | if (FD_ISSET(sock, &rd)) 95 | { 96 | struct sockaddr_in addr; 97 | int recv_cnt; 98 | int len = sizeof(addr); 99 | 100 | recv_cnt = recvfrom(sock, sock_buf, sizeof(sock_buf), 0, (struct sockaddr *)&addr, &len); 101 | if (recv_cnt > 0) 102 | { 103 | msg_udp_mqtt_add_packet(&addr, (unsigned char *)sock_buf, recv_cnt); 104 | PostMessage(hWnd, WM_MQTTSN_MSG, (WPARAM)0, (LPARAM)0); 105 | } 106 | } 107 | } 108 | 109 | if (sock != INVALID_SOCKET) 110 | closesocket(sock); 111 | 112 | stop_flag = 2; 113 | } 114 | 115 | //-------------------------------------------- 116 | static unsigned __stdcall thread_launcher(void *param) 117 | { 118 | thread_run(param); 119 | return 0; 120 | } 121 | 122 | //-------------------------------------------- 123 | int thread_udp_start(HWND hWnd) 124 | { 125 | pthread_t thread; 126 | void *param = NULL; 127 | 128 | if ((int)(sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 129 | { 130 | print_error_socket(__LINE__); 131 | return -1; 132 | } 133 | 134 | thread_begin(thread_launcher, (void *)hWnd, &thread); 135 | return 0; 136 | } 137 | 138 | //-------------------------------------------- 139 | void thread_udp_stop(void) 140 | { 141 | if (stop_flag == 0) 142 | stop_flag = 1; 143 | while (stop_flag != 2) 144 | sleep(10); 145 | } 146 | -------------------------------------------------------------------------------- /utils/sensimgui/src/thread_udp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2018 Vladimir Alemasov 3 | * All rights reserved 4 | * 5 | * This program and the accompanying materials are distributed under 6 | * the terms of GNU General Public License version 2 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | */ 14 | 15 | #ifndef THREAD_UDP_H_ 16 | #define THREAD_UDP_H_ 17 | 18 | int thread_udp_start(HWND hWnd); 19 | void thread_udp_stop(void); 20 | 21 | #endif /* THREAD_UDP_H_ */ 22 | -------------------------------------------------------------------------------- /utils/sensimgui/src/wtl/atlresce.h: -------------------------------------------------------------------------------- 1 | // Windows Template Library - WTL version 8.0 2 | // Copyright (C) Microsoft Corporation. All rights reserved. 3 | // 4 | // This file is a part of the Windows Template Library. 5 | // The use and distribution terms for this software are covered by the 6 | // Common Public License 1.0 (http://opensource.org/osi3.0/licenses/cpl1.0.php) 7 | // which can be found in the file CPL.TXT at the root of this distribution. 8 | // By using this software in any fashion, you are agreeing to be bound by 9 | // the terms of this license. You must not remove this notice, or 10 | // any other, from this software. 11 | 12 | #ifndef __ATLRESCE_H__ 13 | #define __ATLRESCE_H__ 14 | 15 | #pragma once 16 | 17 | #ifndef _WIN32_WCE 18 | #error atlresCE.h is only for Windows CE 19 | #endif 20 | 21 | 22 | #ifdef RC_INVOKED 23 | #ifndef _INC_WINDOWS 24 | 25 | #define VS_VERSION_INFO 1 26 | 27 | #ifdef APSTUDIO_INVOKED 28 | #define APSTUDIO_HIDDEN_SYMBOLS // Ignore following symbols 29 | #endif // APSTUDIO_INVOKED 30 | 31 | #ifndef WINVER 32 | #define WINVER 0x0400 // default to Windows Version 4.0 33 | #endif // !WINVER 34 | 35 | #if !defined(WCEOLE_ENABLE_DIALOGEX) 36 | #define DIALOGEX DIALOG DISCARDABLE 37 | #endif 38 | 39 | #include 40 | #define SHMENUBAR RCDATA 41 | 42 | #if defined(SHELLSDK_MODULES_AYGSHELL) 43 | #include 44 | #else 45 | #define NOMENU 0xFFFF 46 | #define IDS_SHNEW 1 47 | #define IDM_SHAREDNEW 10 48 | #define IDM_SHAREDNEWDEFAULT 11 49 | #endif 50 | #ifndef I_IMAGENONE 51 | #define I_IMAGENONE (-2) 52 | #endif 53 | 54 | #include 55 | 56 | #endif // !_INC_WINDOWS 57 | #endif // RC_INVOKED 58 | 59 | #include "atlres.h" 60 | 61 | #ifdef APSTUDIO_INVOKED 62 | #undef APSTUDIO_HIDDEN_SYMBOLS 63 | #endif // APSTUDIO_INVOKED 64 | 65 | // Visual Studio dialog editor bug fix 66 | #ifndef DS_FIXEDSYS 67 | #define DS_FIXEDSYS 0 68 | #endif 69 | 70 | #define IDC_INFOSTATIC 0xFFFE // == IDC_STATIC -1 71 | 72 | /////////////////////////////////////////////////////////////////////////////// 73 | // Smartphone and PPC 2005 Resource IDs 74 | 75 | // Command and associated string resource IDs 76 | #define ID_MENU_OK 0xE790 77 | #define ID_MENU_CANCEL 0xE791 78 | #define ID_MENU 0xE792 79 | #define ID_ACTION 0xE793 80 | #define ID_VIEW_FULLSCREEN 0xE802 81 | 82 | // MenuBar resource IDs 83 | #define ATL_IDM_MENU_DONE 0xE701 84 | #define ATL_IDM_MENU_CANCEL 0xE702 85 | #define ATL_IDM_MENU_DONECANCEL 0xE703 86 | 87 | // Default device MenuBar control ID and MenuBar resource ID 88 | #define ATL_IDW_MENU_BAR 0xE802 89 | 90 | // SmartPhone spinned controls ID offset for CSpinCtrl 91 | #define ATL_IDW_SPIN_ID 9999 92 | 93 | #endif // __ATLRESCE_H__ 94 | --------------------------------------------------------------------------------