5 Commits

Author SHA1 Message Date
nhkefus
885a7a3692 refactor: improve memory safety in web logging and add connection check for MQTT power reporting 2026-04-08 23:16:13 +08:00
nhkefus
8374484d0a 去除web后台没数据时的默认值 2025-04-28 15:57:54 +08:00
Your Name
c3d9db335e 调试用 2025-04-25 11:46:19 +08:00
nhkefus
3470d373eb 尝试增加一个外网mqtt客户端 2025-04-24 13:00:44 +08:00
Your Name
3caf76f4c6 开发基于udp的app控制协议 2025-04-23 23:32:04 +08:00
26 changed files with 2613 additions and 1531 deletions

3
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

679
.idea/caches/deviceStreaming.xml generated Normal file
View File

@@ -0,0 +1,679 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DeviceStreaming">
<option name="deviceSelectionList">
<list>
<PersistentDeviceSelectionData>
<option name="api" value="27" />
<option name="brand" value="DOCOMO" />
<option name="codename" value="F01L" />
<option name="id" value="F01L" />
<option name="labId" value="google" />
<option name="manufacturer" value="FUJITSU" />
<option name="name" value="F-01L" />
<option name="screenDensity" value="360" />
<option name="screenX" value="720" />
<option name="screenY" value="1280" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="OnePlus" />
<option name="codename" value="OP535DL1" />
<option name="id" value="OP535DL1" />
<option name="labId" value="google" />
<option name="manufacturer" value="OnePlus" />
<option name="name" value="CPH2409" />
<option name="screenDensity" value="401" />
<option name="screenX" value="1080" />
<option name="screenY" value="2412" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="OnePlus" />
<option name="codename" value="OP5552L1" />
<option name="id" value="OP5552L1" />
<option name="labId" value="google" />
<option name="manufacturer" value="OnePlus" />
<option name="name" value="CPH2415" />
<option name="screenDensity" value="480" />
<option name="screenX" value="1080" />
<option name="screenY" value="2412" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="OPPO" />
<option name="codename" value="OP573DL1" />
<option name="id" value="OP573DL1" />
<option name="labId" value="google" />
<option name="manufacturer" value="OPPO" />
<option name="name" value="CPH2557" />
<option name="screenDensity" value="480" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="28" />
<option name="brand" value="DOCOMO" />
<option name="codename" value="SH-01L" />
<option name="id" value="SH-01L" />
<option name="labId" value="google" />
<option name="manufacturer" value="SHARP" />
<option name="name" value="AQUOS sense2 SH-01L" />
<option name="screenDensity" value="480" />
<option name="screenX" value="1080" />
<option name="screenY" value="2160" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="a15" />
<option name="id" value="a15" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="A15" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="a15x" />
<option name="id" value="a15x" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="A15 5G" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="a16x" />
<option name="id" value="a16x" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="A16 5G" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="a35x" />
<option name="id" value="a35x" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="A35" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="31" />
<option name="brand" value="samsung" />
<option name="codename" value="a51" />
<option name="id" value="a51" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy A51" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="akita" />
<option name="id" value="akita" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 8a" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="motorola" />
<option name="codename" value="arcfox" />
<option name="id" value="arcfox" />
<option name="labId" value="google" />
<option name="manufacturer" value="Motorola" />
<option name="name" value="razr plus 2024" />
<option name="screenDensity" value="360" />
<option name="screenX" value="1080" />
<option name="screenY" value="1272" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="motorola" />
<option name="codename" value="austin" />
<option name="id" value="austin" />
<option name="labId" value="google" />
<option name="manufacturer" value="Motorola" />
<option name="name" value="moto g 5G (2022)" />
<option name="screenDensity" value="280" />
<option name="screenX" value="720" />
<option name="screenY" value="1600" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="samsung" />
<option name="codename" value="b0q" />
<option name="id" value="b0q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy S22 Ultra" />
<option name="screenDensity" value="600" />
<option name="screenX" value="1440" />
<option name="screenY" value="3088" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="32" />
<option name="brand" value="google" />
<option name="codename" value="bluejay" />
<option name="id" value="bluejay" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 6a" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="caiman" />
<option name="id" value="caiman" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9 Pro" />
<option name="screenDensity" value="360" />
<option name="screenX" value="960" />
<option name="screenY" value="2142" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="comet" />
<option name="default" value="true" />
<option name="id" value="comet" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9 Pro Fold" />
<option name="screenDensity" value="390" />
<option name="screenX" value="2076" />
<option name="screenY" value="2152" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="35" />
<option name="brand" value="google" />
<option name="codename" value="comet" />
<option name="default" value="true" />
<option name="id" value="comet" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9 Pro Fold" />
<option name="screenDensity" value="390" />
<option name="screenX" value="2076" />
<option name="screenY" value="2152" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="29" />
<option name="brand" value="samsung" />
<option name="codename" value="crownqlteue" />
<option name="id" value="crownqlteue" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy Note9" />
<option name="screenDensity" value="420" />
<option name="screenX" value="2220" />
<option name="screenY" value="1080" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="dm2q" />
<option name="id" value="dm2q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="S23 Plus" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="dm3q" />
<option name="id" value="dm3q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy S23 Ultra" />
<option name="screenDensity" value="600" />
<option name="screenX" value="1440" />
<option name="screenY" value="3088" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="e1q" />
<option name="default" value="true" />
<option name="id" value="e1q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy S24" />
<option name="screenDensity" value="480" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="e3q" />
<option name="id" value="e3q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy S24 Ultra" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1440" />
<option name="screenY" value="3120" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="google" />
<option name="codename" value="eos" />
<option name="id" value="eos" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Eos" />
<option name="screenDensity" value="320" />
<option name="screenX" value="384" />
<option name="screenY" value="384" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="google" />
<option name="codename" value="felix" />
<option name="id" value="felix" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel Fold" />
<option name="screenDensity" value="420" />
<option name="screenX" value="2208" />
<option name="screenY" value="1840" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="felix" />
<option name="id" value="felix" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel Fold" />
<option name="screenDensity" value="420" />
<option name="screenX" value="2208" />
<option name="screenY" value="1840" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="google" />
<option name="codename" value="felix_camera" />
<option name="id" value="felix_camera" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel Fold (Camera-enabled)" />
<option name="screenDensity" value="420" />
<option name="screenX" value="2208" />
<option name="screenY" value="1840" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="motorola" />
<option name="codename" value="fogona" />
<option name="id" value="fogona" />
<option name="labId" value="google" />
<option name="manufacturer" value="Motorola" />
<option name="name" value="moto g play - 2024" />
<option name="screenDensity" value="280" />
<option name="screenX" value="720" />
<option name="screenY" value="1600" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="g0q" />
<option name="id" value="g0q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="SM-S906U1" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="gta9pwifi" />
<option name="id" value="gta9pwifi" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="SM-X210" />
<option name="screenDensity" value="240" />
<option name="screenX" value="1200" />
<option name="screenY" value="1920" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="gts7xllite" />
<option name="id" value="gts7xllite" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="SM-T738U" />
<option name="screenDensity" value="340" />
<option name="screenX" value="1600" />
<option name="screenY" value="2560" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="samsung" />
<option name="codename" value="gts8uwifi" />
<option name="formFactor" value="Tablet" />
<option name="id" value="gts8uwifi" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy Tab S8 Ultra" />
<option name="screenDensity" value="320" />
<option name="screenX" value="1848" />
<option name="screenY" value="2960" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="gts8wifi" />
<option name="formFactor" value="Tablet" />
<option name="id" value="gts8wifi" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy Tab S8" />
<option name="screenDensity" value="274" />
<option name="screenX" value="1600" />
<option name="screenY" value="2560" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="gts9fe" />
<option name="id" value="gts9fe" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy Tab S9 FE 5G" />
<option name="screenDensity" value="280" />
<option name="screenX" value="1440" />
<option name="screenY" value="2304" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="husky" />
<option name="id" value="husky" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 8 Pro" />
<option name="screenDensity" value="390" />
<option name="screenX" value="1008" />
<option name="screenY" value="2244" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="30" />
<option name="brand" value="motorola" />
<option name="codename" value="java" />
<option name="id" value="java" />
<option name="labId" value="google" />
<option name="manufacturer" value="Motorola" />
<option name="name" value="G20" />
<option name="screenDensity" value="280" />
<option name="screenX" value="720" />
<option name="screenY" value="1600" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="komodo" />
<option name="id" value="komodo" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9 Pro XL" />
<option name="screenDensity" value="360" />
<option name="screenX" value="1008" />
<option name="screenY" value="2244" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="35" />
<option name="brand" value="google" />
<option name="codename" value="komodo" />
<option name="id" value="komodo" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9 Pro XL" />
<option name="screenDensity" value="360" />
<option name="screenX" value="1008" />
<option name="screenY" value="2244" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="google" />
<option name="codename" value="lynx" />
<option name="id" value="lynx" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 7a" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="motorola" />
<option name="codename" value="maui" />
<option name="id" value="maui" />
<option name="labId" value="google" />
<option name="manufacturer" value="Motorola" />
<option name="name" value="moto g play - 2023" />
<option name="screenDensity" value="280" />
<option name="screenX" value="720" />
<option name="screenY" value="1600" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="o1q" />
<option name="id" value="o1q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy S21" />
<option name="screenDensity" value="421" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="31" />
<option name="brand" value="google" />
<option name="codename" value="oriole" />
<option name="id" value="oriole" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 6" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="google" />
<option name="codename" value="panther" />
<option name="id" value="panther" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 7" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="q5q" />
<option name="id" value="q5q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy Z Fold5" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1812" />
<option name="screenY" value="2176" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="q6q" />
<option name="id" value="q6q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy Z Fold6" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1856" />
<option name="screenY" value="2160" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="30" />
<option name="brand" value="google" />
<option name="codename" value="r11" />
<option name="formFactor" value="Wear OS" />
<option name="id" value="r11" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel Watch" />
<option name="screenDensity" value="320" />
<option name="screenX" value="384" />
<option name="screenY" value="384" />
<option name="type" value="WEAR_OS" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="r11q" />
<option name="id" value="r11q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="SM-S711U" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="30" />
<option name="brand" value="google" />
<option name="codename" value="redfin" />
<option name="id" value="redfin" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 5" />
<option name="screenDensity" value="440" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="shiba" />
<option name="id" value="shiba" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 8" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="t2q" />
<option name="id" value="t2q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy S21 Plus" />
<option name="screenDensity" value="394" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="google" />
<option name="codename" value="tangorpro" />
<option name="formFactor" value="Tablet" />
<option name="id" value="tangorpro" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel Tablet" />
<option name="screenDensity" value="320" />
<option name="screenX" value="1600" />
<option name="screenY" value="2560" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="35" />
<option name="brand" value="google" />
<option name="codename" value="tegu" />
<option name="id" value="tegu" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9a" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2424" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="tokay" />
<option name="default" value="true" />
<option name="id" value="tokay" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2424" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="35" />
<option name="brand" value="google" />
<option name="codename" value="tokay" />
<option name="default" value="true" />
<option name="id" value="tokay" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2424" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="xcover7" />
<option name="id" value="xcover7" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="SM-G556B" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1080" />
<option name="screenY" value="2408" />
</PersistentDeviceSelectionData>
</list>
</option>
</component>
</project>

View File

@@ -0,0 +1,21 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="HtmlUnknownTag" enabled="true" level="WARNING" enabled_by_default="true">
<option name="myValues">
<value>
<list size="7">
<item index="0" class="java.lang.String" itemvalue="nobr" />
<item index="1" class="java.lang.String" itemvalue="noembed" />
<item index="2" class="java.lang.String" itemvalue="comment" />
<item index="3" class="java.lang.String" itemvalue="noscript" />
<item index="4" class="java.lang.String" itemvalue="embed" />
<item index="5" class="java.lang.String" itemvalue="script" />
<item index="6" class="java.lang.String" itemvalue="h5" />
</list>
</value>
</option>
<option name="myCustomValuesEnabled" value="true" />
</inspection_tool>
</profile>
</component>

View File

@@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="FlashPlus_v1.0.10.msi">
<CLASSES>
<root url="jar://$PROJECT_DIR$/doc/FlashPlus_v1.0.10.msi.zip!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

9
.idea/libraries/mkfatimg.xml generated Normal file
View File

@@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="mkfatimg">
<CLASSES>
<root url="jar://$PROJECT_DIR$/mico-os/libraries/filesystem/FatFs/ver0_11/doc/img/mkfatimg.zip!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

5
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,5 @@
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/zTC1.iml" filepath="$PROJECT_DIR$/.idea/zTC1.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@@ -60,9 +60,7 @@ TC1 排插硬件分 A1 A2 两个版本, 本固件仅支持 **A1 版本**. A1 A2
- [x] 后台可自定义设备名称此名称将会同至ha mqtt作为mqtt中插座名称的前缀 - [x] 后台可自定义设备名称此名称将会同至ha mqtt作为mqtt中插座名称的前缀
- [x] 后台可自定义按键连击,长按等功能 - [x] 后台可自定义按键连击,长按等功能
- [x] 定时开关现在支持更多操作项,与按键功能自定义一样 - [x] 定时开关现在支持更多操作项,与按键功能自定义一样
- [x] 现在可以上传本地文件来更新固件,同时可以在最新发布的固件说明中参考使用我搭建的文件服务来在线更新
# 固件烧录
- 参考原始固件代码提供者[a2633063](https://github.com/a2633063/zTC1/wiki/固件烧录)大佬提供的说明
# 编译固件 # 编译固件
- windows环境先安装python2.7,下载安装的时候添加环境变量到Path默认安装路径需要添加两个环境变量C:\Python27C:\Python27\Scripts - windows环境先安装python2.7,下载安装的时候添加环境变量到Path默认安装路径需要添加两个环境变量C:\Python27C:\Python27\Scripts

BIN
TC1/.DS_Store vendored Normal file

Binary file not shown.

View File

@@ -34,7 +34,8 @@ $(NAME)_SOURCES := main.c\
user_power.c\ user_power.c\
timed_task/timed_task.c\ timed_task/timed_task.c\
http_server/web_log.c\ http_server/web_log.c\
http_server/app_httpd.c http_server/app_httpd.c\
udp_server/udp_server.c
$(NAME)_COMPONENTS := protocols/SNTP\ $(NAME)_COMPONENTS := protocols/SNTP\
protocols/mqtt\ protocols/mqtt\

View File

@@ -20,13 +20,16 @@ void WebLog(const char *M, ...);
extern LogRecord log_record; extern LogRecord log_record;
extern char* LOG_TMP; extern char* LOG_TMP;
extern time_t LOG_NOW; extern time_t LOG_NOW;
#define web_log(N, M, ...) \ #define web_log(N, M, ...) do { \
LOG_TMP = (char*)malloc(sizeof(char)*LOG_LEN); \ char* _wl_tmp = (char*)malloc(sizeof(char)*LOG_LEN); \
LOG_NOW = time(NULL) + 28800; \ if (_wl_tmp) { \
strftime(LOG_TMP, TIME_LEN, "[%Y-%m-%d %H:%M:%S]", localtime(&LOG_NOW)); \ time_t _wl_now = time(NULL) + 28800; \
LOG_TMP[TIME_LEN - 1] = ' '; \ strftime(_wl_tmp, TIME_LEN, "[%Y-%m-%d %H:%M:%S]", localtime(&_wl_now)); \
snprintf(LOG_TMP + TIME_LEN, LOG_LEN - TIME_LEN, "["N" %s:%d] "M, SHORT_FILE, __LINE__, ##__VA_ARGS__); \ _wl_tmp[TIME_LEN - 1] = ' '; \
SetLogRecord(&log_record, LOG_TMP); \ snprintf(_wl_tmp + TIME_LEN, LOG_LEN - TIME_LEN, "[" N " %s:%d] " M, SHORT_FILE, __LINE__, ##__VA_ARGS__); \
SetLogRecord(&log_record, _wl_tmp); \
} \
} while(0)
#define web_log0(N, M, ...) WebLog("["N" %s:%d] "M, SHORT_FILE, __LINE__, ##__VA_ARGS__) #define web_log0(N, M, ...) WebLog("["N" %s:%d] "M, SHORT_FILE, __LINE__, ##__VA_ARGS__)

View File

@@ -10,7 +10,9 @@
#include "user_wifi.h" #include "user_wifi.h"
#include "time_server/user_rtc.h" #include "time_server/user_rtc.h"
#include "user_power.h" #include "user_power.h"
#include "mqtt_server/user_mqtt_client.h"
#include "http_server/app_httpd.h" #include "http_server/app_httpd.h"
#include "udp_server/udp_server.h"
#include "timed_task/timed_task.h" #include "timed_task/timed_task.h"
char rtc_init = 0; //sntp校时成功标志位 char rtc_init = 0; //sntp校时成功标志位
@@ -112,7 +114,9 @@ void schedule_p_count_task(mico_thread_arg_t arg) {
void reportMqttPowerInfoThread() { void reportMqttPowerInfoThread() {
while (1) { while (1) {
if (UserMqttIsConnect()) {
UserMqttHassPower(); UserMqttHassPower();
}
int freq = user_config->mqtt_report_freq; int freq = user_config->mqtt_report_freq;
if (freq == 0) { if (freq == 0) {
@@ -189,7 +193,10 @@ int application_start(void) {
require_noerr(err, exit); require_noerr(err, exit);
PowerInit(); PowerInit();
AppHttpdStart(); // start http server thread AppHttpdStart(); // start http server thread
// udp_server_start();
//if (!(MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1)){
// UserMqttInit();
// }
UserLedSet(user_config->power_led_enabled); UserLedSet(user_config->power_led_enabled);
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "p_count", err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "p_count",

View File

@@ -36,10 +36,18 @@ typedef struct {
uint32_t datalen; uint32_t datalen;
} mqtt_recv_msg_t, *p_mqtt_recv_msg_t, mqtt_send_msg_t, *p_mqtt_send_msg_t; } mqtt_recv_msg_t, *p_mqtt_recv_msg_t, mqtt_send_msg_t, *p_mqtt_send_msg_t;
typedef struct {
Client client;
Network network;
// 其他状态、配置参数……
} mqtt_context_t;
static void MqttClientThread(mico_thread_arg_t arg); static void MqttClientThread(mico_thread_arg_t arg);
static void MessageArrived(MessageData *md); static void MessageArrived(MessageData *md);
static void MqttClientThread2(mico_thread_arg_t arg);
static OSStatus static OSStatus
MqttMsgPublish(Client *c, const char *topic, char qos, char retained, const unsigned char *msg, MqttMsgPublish(Client *c, const char *topic, char qos, char retained, const unsigned char *msg,
uint32_t msg_len); uint32_t msg_len);
@@ -49,13 +57,15 @@ OSStatus UserRecvHandler(void *arg);
void ProcessHaCmd(char *cmd); void ProcessHaCmd(char *cmd);
bool isconnect = false; bool isconnect = false;
bool isconnect2 = false;
mico_queue_t mqtt_msg_send_queue = NULL; mico_queue_t mqtt_msg_send_queue = NULL;
mico_queue_t mqtt_msg_send_queue2 = NULL;
Client c; // mqtt client object
Network n; // socket network for mqtt client
volatile bool mqtt_thread_should_exit = false; volatile bool mqtt_thread_should_exit = false;
static bool mqtt_initialized = false;
static mico_worker_thread_t mqtt_client_worker_thread; /* Worker thread to manage send/recv events */ static mico_worker_thread_t mqtt_client_worker_thread; /* Worker thread to manage send/recv events */
//static mico_timed_event_t mqtt_client_send_event; //static mico_timed_event_t mqtt_client_send_event;
char topic_state[MAX_MQTT_TOPIC_SIZE]; char topic_state[MAX_MQTT_TOPIC_SIZE];
@@ -63,6 +73,8 @@ char topic_set[MAX_MQTT_TOPIC_SIZE];
mico_timer_t timer_handle; mico_timer_t timer_handle;
static char timer_status = 0; static char timer_status = 0;
mico_timer_t timer_handle2;
static char timer_status_2 = 0;
void UserMqttTimerFunc(void *arg) { void UserMqttTimerFunc(void *arg) {
LinkStatusTypeDef LinkStatus; LinkStatusTypeDef LinkStatus;
@@ -99,32 +111,60 @@ void UserMqttTimerFunc(void *arg) {
} }
} }
void UserMqttTimerFunc2(void *arg) {
LinkStatusTypeDef LinkStatus;
micoWlanGetLinkStatus(&LinkStatus);
if (LinkStatus.is_connected != 1) {
mico_stop_timer(&timer_handle2);
return;
}
if (mico_rtos_is_queue_empty(&mqtt_msg_send_queue2)) {
switch (timer_status_2) {
case 0:
UserMqttHassAutoLed();
UserMqttHassAutoTotalSocket();
UserMqttHassAutoChildLock();
UserMqttHassAutoRebootButton();
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
UserMqttHassAuto(timer_status_2);
break;
case 7:
UserMqttHassAutoPower();
break;
default:
mico_stop_timer(&timer_handle2);
break;
}
timer_status_2++;
}
}
OSStatus UserMqttDeInit(void) { OSStatus UserMqttDeInit(void) {
OSStatus err = kNoErr; OSStatus err = kNoErr;
// 1. 请求线程退出 // 1. 请求线程退出
mqtt_thread_should_exit = true; mqtt_thread_should_exit = true;
mqtt_initialized = false;
return err; return err;
} }
void clear_mqtt_msg_send_queue(void) {
if(mqtt_msg_send_queue == NULL){
return;
}
void *msg = NULL;
while (mico_rtos_is_queue_empty(&mqtt_msg_send_queue) == false) {
if (mico_rtos_pop_from_queue(&mqtt_msg_send_queue, &msg, 0) == kNoErr) {
if (msg) free(msg); // 释放消息内存,避免泄漏
}
}
}
/* Application entrance */ /* Application entrance */
OSStatus UserMqttInit(void) { OSStatus UserMqttInit(void) {
OSStatus err = kNoErr; OSStatus err = kNoErr;
if(mqtt_msg_send_queue != NULL)
return err; if (mqtt_initialized) {
mqtt_log("MQTT already initialized, skip re-init.");
return kNoErr;
}
sprintf(topic_set, MQTT_CLIENT_SUB_TOPIC1); sprintf(topic_set, MQTT_CLIENT_SUB_TOPIC1);
sprintf(topic_state, MQTT_CLIENT_PUB_TOPIC, str_mac); sprintf(topic_state, MQTT_CLIENT_PUB_TOPIC, str_mac);
//TODO size:0x800 //TODO size:0x800
@@ -140,17 +180,39 @@ if(mqtt_msg_send_queue != NULL)
MAX_MQTT_SEND_QUEUE_SIZE); MAX_MQTT_SEND_QUEUE_SIZE);
require_noerr_action(err, exit, mqtt_log("ERROR: create mqtt msg send queue err=%d.", err)); require_noerr_action(err, exit, mqtt_log("ERROR: create mqtt msg send queue err=%d.", err));
/* create mqtt msg send queue 2 */
err = mico_rtos_init_queue(&mqtt_msg_send_queue2, "mqtt_msg_send_queue2",
sizeof(p_mqtt_send_msg_t),
MAX_MQTT_SEND_QUEUE_SIZE);
require_noerr_action(err, exit, mqtt_log("ERROR: create mqtt msg send queue2 err=%d.", err));
/* start mqtt client */ /* start mqtt client */
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mqtt_client", mqtt_context_t *ctx1 = malloc(sizeof(mqtt_context_t));
(mico_thread_function_t) MqttClientThread, memset(ctx1, 0, sizeof(mqtt_context_t));
mqtt_thread_stack_size, 0);
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mqtt_client_1",
(mico_thread_function_t)MqttClientThread,
mqtt_thread_stack_size, ctx1);
require_noerr_string(err, exit, "ERROR: Unable to start the mqtt client thread."); require_noerr_string(err, exit, "ERROR: Unable to start the mqtt client thread.");
// 第二个客户端
mqtt_context_t *ctx2 = malloc(sizeof(mqtt_context_t));
memset(ctx2, 0, sizeof(mqtt_context_t));
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mqtt_client_2",
(mico_thread_function_t)MqttClientThread2,
mqtt_thread_stack_size, ctx2);
require_noerr_string(err, exit, "ERROR: Unable to start the mqtt client thread2.");
/* Create a worker thread for user handling MQTT data event */ /* Create a worker thread for user handling MQTT data event */
err = mico_rtos_create_worker_thread(&mqtt_client_worker_thread, MICO_APPLICATION_PRIORITY, err = mico_rtos_create_worker_thread(&mqtt_client_worker_thread, MICO_APPLICATION_PRIORITY,
0x800, 5); 0x1000, 5);
require_noerr_string(err, exit, "ERROR: Unable to start the mqtt client worker thread."); require_noerr_string(err, exit, "ERROR: Unable to start the mqtt client worker thread.");
mqtt_initialized = true;
exit: exit:
if (kNoErr != err)mqtt_log("ERROR2, app thread exit err: %d kNoErr[%d]", err, kNoErr); if (kNoErr != err)mqtt_log("ERROR2, app thread exit err: %d kNoErr[%d]", err, kNoErr);
return err; return err;
@@ -159,32 +221,13 @@ if(mqtt_msg_send_queue != NULL)
static OSStatus UserMqttClientRelease(Client *c, Network *n) { static OSStatus UserMqttClientRelease(Client *c, Network *n) {
OSStatus err = kNoErr; OSStatus err = kNoErr;
if (c == NULL || n == NULL) return kParamErr; if (c->isconnected) MQTTDisconnect(c);
if (c->isconnected) { n->disconnect(n); // close connection
MQTTDisconnect(c);
c->isconnected = 0;
}
if (c->buf) { if (MQTT_SUCCESS != MQTTClientDeinit(c)) { mqtt_log("MQTTClientDeinit failed!");
free(c->buf);
c->buf = NULL;
}
if (c->readbuf) {
free(c->readbuf);
c->readbuf = NULL;
}
if (n->disconnect) {
n->disconnect(n);
}
if (MQTT_SUCCESS != MQTTClientDeinit(c)) {
mqtt_log("MQTTClientDeinit failed!");
err = kDeletedErr; err = kDeletedErr;
} }
return err; return err;
} }
@@ -219,16 +262,18 @@ static OSStatus MqttMsgPublish(Client *c, const char *topic, char qos, char reta
} }
void registerMqttEvents(void) { void registerMqttEvents(void) {
if(timer_status !=0){
mico_stop_timer(&timer_handle);
}
timer_status = 0; timer_status = 0;
mico_start_timer(&timer_handle); mico_start_timer(&timer_handle);
} }
void registerMqttEvents2(void) {
timer_status_2 = 0;
mico_start_timer(&timer_handle2);
}
void MqttClientThread(mico_thread_arg_t arg) { void MqttClientThread(mico_thread_arg_t arg) {
OSStatus err = kUnknownErr; OSStatus err = kUnknownErr;
mqtt_context_t *ctx = (mqtt_context_t *)arg;
// 后续一直用 ctx->client 和 ctx->network
int rc = -1; int rc = -1;
fd_set readfds; fd_set readfds;
struct timeval t = {0, MQTT_YIELD_TMIE * 1000}; struct timeval t = {0, MQTT_YIELD_TMIE * 1000};
@@ -242,13 +287,11 @@ void MqttClientThread(mico_thread_arg_t arg) {
mqtt_log("MQTT client thread started..."); mqtt_log("MQTT client thread started...");
memset(&c, 0, sizeof(c)); memset(&ctx->client, 0, sizeof(ctx->client));
memset(&n, 0, sizeof(n)); memset(&ctx->network, 0, sizeof(ctx->network));
/* create msg send queue event fd */ /* create msg send queue event fd */
msg_send_event_fd = mico_create_event_fd(mqtt_msg_send_queue); msg_send_event_fd = mico_create_event_fd(mqtt_msg_send_queue);
mico_init_timer(&timer_handle, 150, UserMqttTimerFunc, &arg);
require_action(msg_send_event_fd >= 0, exit, require_action(msg_send_event_fd >= 0, exit,
mqtt_log("ERROR: create msg send queue event fd failed!!!")); mqtt_log("ERROR: create msg send queue event fd failed!!!"));
mqtt_thread_should_exit = false; mqtt_thread_should_exit = false;
@@ -271,16 +314,15 @@ void MqttClientThread(mico_thread_arg_t arg) {
continue; continue;
} }
rc = NewNetwork(&n, MQTT_SERVER, MQTT_SERVER_PORT, ssl_settings); rc = NewNetwork(&ctx->network, MQTT_SERVER, MQTT_SERVER_PORT, ssl_settings);
if (rc == MQTT_SUCCESS) break; if (rc == MQTT_SUCCESS) break;
//mqtt_log("ERROR: MQTT network connect err=%d, reconnect after 3s...", rc); //mqtt_log("ERROR: MQTT network connect err=%d, reconnect after 3s...", rc);
} }mqtt_log("MQTT network connect success!");
mqtt_log("MQTT network connect success!");
/* 2. init mqtt client */ /* 2. init mqtt client */
//c.heartbeat_retry_max = 2; //c.heartbeat_retry_max = 2;
rc = MQTTClientInit(&c, &n, MQTT_CMD_TIMEOUT); rc = MQTTClientInit(&ctx->client, &ctx->network, MQTT_CMD_TIMEOUT);
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client init err."); require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client init err.");
mqtt_log("MQTT client init success!"); mqtt_log("MQTT client init success!");
@@ -294,15 +336,15 @@ void MqttClientThread(mico_thread_arg_t arg) {
connectData.keepAliveInterval = MQTT_CLIENT_KEEPALIVE; connectData.keepAliveInterval = MQTT_CLIENT_KEEPALIVE;
connectData.cleansession = 1; connectData.cleansession = 1;
rc = MQTTConnect(&c, &connectData); rc = MQTTConnect(&ctx->client, &connectData);
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client connect err."); require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client connect err.");
mqtt_log("MQTT client connect success, result: %d ", rc); mqtt_log("MQTT client connect success!");
UserLedSet(RelayOut() && user_config->power_led_enabled); UserLedSet(RelayOut() && user_config->power_led_enabled);
/* 4. mqtt client subscribe */ /* 4. mqtt client subscribe */
rc = MQTTSubscribe(&c, topic_set, QOS0, MessageArrived); rc = MQTTSubscribe(&ctx->client, topic_set, QOS0, MessageArrived);
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client subscribe err.");mqtt_log( require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client subscribe err.");mqtt_log(
"MQTT client subscribe success! recv_topic=[%s].", topic_set); "MQTT client subscribe success! recv_topic=[%s].", topic_set);
/*4.1 杩炴帴鎴愬姛鍚庡厛鏇存柊鍙戦<E98D99>佷竴娆℃暟鎹<E69A9F>*/ /*4.1 杩炴帴鎴愬姛鍚庡厛鏇存柊鍙戦<E98D99>佷竴娆℃暟鎹<E69A9F>*/
@@ -317,19 +359,22 @@ void MqttClientThread(mico_thread_arg_t arg) {
UserMqttSendTotalSocketState(); UserMqttSendTotalSocketState();
UserMqttSendChildLockState(); UserMqttSendChildLockState();
mico_stop_timer(&timer_handle);
mico_deinit_timer(&timer_handle);
mico_init_timer(&timer_handle, 150, UserMqttTimerFunc, &arg);
registerMqttEvents(); registerMqttEvents();
/* 5. client loop for recv msg && keepalive */ /* 5. client loop for recv msg && keepalive */
while (!mqtt_thread_should_exit) { while (!mqtt_thread_should_exit) {
isconnect = true; isconnect = true;
no_mqtt_msg_exchange = true; no_mqtt_msg_exchange = true;
FD_ZERO(&readfds); FD_ZERO(&readfds);
FD_SET(c.ipstack->my_socket, &readfds); FD_SET(ctx->client.ipstack->my_socket, &readfds);
FD_SET(msg_send_event_fd, &readfds); FD_SET(msg_send_event_fd, &readfds);
select(msg_send_event_fd + 1, &readfds, NULL, NULL, &t); select(msg_send_event_fd + 1, &readfds, NULL, NULL, &t);
/* recv msg from server */ /* recv msg from server */
if (FD_ISSET(c.ipstack->my_socket, &readfds)) { if (FD_ISSET(ctx->client.ipstack->my_socket, &readfds)) {
rc = MQTTYield(&c, (int) MQTT_YIELD_TMIE); rc = MQTTYield(&ctx->client, (int) MQTT_YIELD_TMIE);
require_noerr(rc, MQTT_reconnect); require_noerr(rc, MQTT_reconnect);
no_mqtt_msg_exchange = false; no_mqtt_msg_exchange = false;
} }
@@ -342,21 +387,22 @@ void MqttClientThread(mico_thread_arg_t arg) {
require_string(p_send_msg, exit, "Wrong data point"); require_string(p_send_msg, exit, "Wrong data point");
// send message to server // send message to server
err = MqttMsgPublish(&c, p_send_msg->topic, p_send_msg->qos, p_send_msg->retained, err = MqttMsgPublish(&ctx->client, p_send_msg->topic, p_send_msg->qos, p_send_msg->retained,
(const unsigned char *) p_send_msg->data, (const unsigned char *) p_send_msg->data,
p_send_msg->datalen); p_send_msg->datalen);
free(p_send_msg);
p_send_msg = NULL;
require_noerr_string(err, MQTT_reconnect, "ERROR: MQTT publish data err"); require_noerr_string(err, MQTT_reconnect, "ERROR: MQTT publish data err");
//mqtt_log("MQTT publish data success! send_topic=[%s], msg=[%ld].", p_send_msg->topic, p_send_msg->datalen); //mqtt_log("MQTT publish data success! send_topic=[%s], msg=[%ld].", p_send_msg->topic, p_send_msg->datalen);
no_mqtt_msg_exchange = false; no_mqtt_msg_exchange = false;
free(p_send_msg);
p_send_msg = NULL;
} }
} }
/* if no msg exchange, we need to check ping msg to keep alive. */ /* if no msg exchange, we need to check ping msg to keep alive. */
if (no_mqtt_msg_exchange) { if (no_mqtt_msg_exchange) {
rc = keepalive(&c); rc = keepalive(&ctx->client);
require_noerr_string(rc, MQTT_reconnect, "ERROR: keepalive err"); require_noerr_string(rc, MQTT_reconnect, "ERROR: keepalive err");
} }
} }
@@ -366,8 +412,8 @@ void MqttClientThread(mico_thread_arg_t arg) {
mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d, err = %d", rc, err); mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d, err = %d", rc, err);
timer_status = 100; timer_status = 100;
clear_mqtt_msg_send_queue();
UserMqttClientRelease(&c, &n); UserMqttClientRelease(&ctx->client, &ctx->network);
isconnect = false; isconnect = false;
UserLedSet(-1); UserLedSet(-1);
mico_rtos_thread_msleep(100); mico_rtos_thread_msleep(100);
@@ -378,7 +424,168 @@ mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d,
exit: exit:
isconnect = false; isconnect = false;
mqtt_log("EXIT: MQTT client exit with err = %d.", err); mqtt_log("EXIT: MQTT client exit with err = %d.", err);
UserMqttClientRelease(&c, &n); UserMqttClientRelease(&ctx->client, &ctx->network);
mico_rtos_delete_thread(NULL); // 自删
return;
}
void MqttClientThread2(mico_thread_arg_t arg) {
OSStatus err = kUnknownErr;
mqtt_context_t *ctx = (mqtt_context_t *)arg;
// 后续一直用 ctx->client 和 ctx->network
int rc = -1;
fd_set readfds;
struct timeval t = {0, MQTT_YIELD_TMIE * 1000};
ssl_opts ssl_settings;
MQTTPacket_connectData connectData = MQTTPacket_connectData_initializer;
p_mqtt_send_msg_t p_send_msg = NULL;
int msg_send_event_fd = -1;
bool no_mqtt_msg_exchange = true;
mqtt_log("MQTT client thread2 started...");
memset(&ctx->client, 0, sizeof(ctx->client));
memset(&ctx->network, 0, sizeof(ctx->network));
/* create msg send queue event fd */
msg_send_event_fd = mico_create_event_fd(mqtt_msg_send_queue2);
require_action(msg_send_event_fd >= 0, exit,
mqtt_log("ERROR: create msg send queue2 event fd failed!!!"));
mqtt_thread_should_exit = false;
MQTT_start:
isconnect2 = false;
/* 1. create network connection */
ssl_settings.ssl_enable = false;
LinkStatusTypeDef LinkStatus;
while (!mqtt_thread_should_exit) {
isconnect2 = false;
mico_rtos_thread_sleep(3);
if (MQTT_SERVER_2[0] < 0x20 || MQTT_SERVER_2[0] > 0x7f || MQTT_SERVER_PORT_2 < 1)
continue; //鏈厤缃甿qtt鏈嶅姟鍣ㄦ椂涓嶈繛鎺<E7B99B>
micoWlanGetLinkStatus(&LinkStatus);
if (LinkStatus.is_connected != 1) { mqtt_log(
"ERROR:WIFI not connect, waiting 3s for connecting and then connecting MQTT2 ");
mico_rtos_thread_sleep(3);
continue;
}
rc = NewNetwork(&ctx->network, MQTT_SERVER_2, MQTT_SERVER_PORT_2, ssl_settings);
if (rc == MQTT_SUCCESS) break;
//mqtt_log("ERROR: MQTT network connect err=%d, reconnect after 3s...", rc);
}mqtt_log("MQTT2 network connect success!");
/* 2. init mqtt client */
//c.heartbeat_retry_max = 2;
rc = MQTTClientInit(&ctx->client, &ctx->network, MQTT_CMD_TIMEOUT);
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client init err.");
mqtt_log("MQTT2 client init success!");
/* 3. create mqtt client connection */
connectData.willFlag = 0;
connectData.MQTTVersion = 4; // 3: 3.1, 4: v3.1.1
connectData.clientID.cstring = str_mac;
connectData.username.cstring = MQTT_SERVER_USR_2;
connectData.password.cstring = MQTT_SERVER_PWD_2;
connectData.keepAliveInterval = MQTT_CLIENT_KEEPALIVE;
connectData.cleansession = 1;
rc = MQTTConnect(&ctx->client, &connectData);
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT2 client connect err.");
mqtt_log("MQTT2 client connect success!");
UserLedSet(RelayOut() && user_config->power_led_enabled);
/* 4. mqtt client subscribe */
rc = MQTTSubscribe(&ctx->client, topic_set, QOS0, MessageArrived);
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT2 client subscribe err.");mqtt_log(
"MQTT2 client subscribe success! recv_topic=[%s].", topic_set);
/*4.1 杩炴帴鎴愬姛鍚庡厛鏇存柊鍙戦<E98D99>佷竴娆℃暟鎹<E69A9F>*/
isconnect2 = true;
int i = 0;
for (; i < SOCKET_NUM; i++) {
UserMqttSendSocketState(i);
}
UserMqttSendLedState();
UserMqttSendTotalSocketState();
UserMqttSendChildLockState();
mico_stop_timer(&timer_handle2);
mico_deinit_timer(&timer_handle2);
mico_init_timer(&timer_handle2, 150, UserMqttTimerFunc2, &arg);
registerMqttEvents2();
/* 5. client loop for recv msg && keepalive */
while (!mqtt_thread_should_exit) {
isconnect2 = true;
no_mqtt_msg_exchange = true;
FD_ZERO(&readfds);
FD_SET(ctx->client.ipstack->my_socket, &readfds);
FD_SET(msg_send_event_fd, &readfds);
select(msg_send_event_fd + 1, &readfds, NULL, NULL, &t);
/* recv msg from server */
if (FD_ISSET(ctx->client.ipstack->my_socket, &readfds)) {
rc = MQTTYield(&ctx->client, (int) MQTT_YIELD_TMIE);
require_noerr(rc, MQTT_reconnect);
no_mqtt_msg_exchange = false;
}
/* recv msg from user worker thread to be sent to server */
if (FD_ISSET(msg_send_event_fd, &readfds)) {
while (mico_rtos_is_queue_empty(&mqtt_msg_send_queue2) == false) {
// get msg from send queue
mico_rtos_pop_from_queue(&mqtt_msg_send_queue2, &p_send_msg, 0);
require_string(p_send_msg, exit, "Wrong data point");
// send message to server
err = MqttMsgPublish(&ctx->client, p_send_msg->topic, p_send_msg->qos, p_send_msg->retained,
(const unsigned char *) p_send_msg->data,
p_send_msg->datalen);
require_noerr_string(err, MQTT_reconnect, "ERROR: MQTT2 publish data err");
//mqtt_log("MQTT publish data success! send_topic=[%s], msg=[%ld].", p_send_msg->topic, p_send_msg->datalen);
no_mqtt_msg_exchange = false;
free(p_send_msg);
p_send_msg = NULL;
}
}
/* if no msg exchange, we need to check ping msg to keep alive. */
if (no_mqtt_msg_exchange) {
rc = keepalive(&ctx->client);
require_noerr_string(rc, MQTT_reconnect, "ERROR: keepalive err");
}
}
MQTT_reconnect:
mqtt_log("Disconnect MQTT2 client, and reconnect after 5s, reason: mqtt_rc = %d, err = %d", rc, err);
timer_status_2 = 100;
UserMqttClientRelease(&ctx->client, &ctx->network);
isconnect2 = false;
UserLedSet(-1);
mico_rtos_thread_msleep(100);
UserLedSet(-1);
mico_rtos_thread_sleep(5);
goto MQTT_start;
exit:
isconnect2 = false;
mqtt_log("EXIT: MQTT2 client exit with err = %d.", err);
UserMqttClientRelease(&ctx->client, &ctx->network);
mico_rtos_delete_thread(NULL); // 自删 mico_rtos_delete_thread(NULL); // 自删
return; return;
} }
@@ -471,46 +678,63 @@ void ProcessHaCmd(char *cmd) {
childLockEnabled = on; childLockEnabled = on;
UserMqttSendChildLockState(); UserMqttSendChildLockState();
mico_system_context_update(sys_config); mico_system_context_update(sys_config);
}else if (strcmp(cmd, "reboot") == ' ') { }else if (strcmp(cmd, "reboot") == 0) {
sscanf(cmd, "reboot %s", mac);
if (strcmp(mac, str_mac)) return;
MicoSystemReboot(); // 立即重启设备 MicoSystemReboot(); // 立即重启设备
} }
} }
OSStatus UserMqttSendTopic(char *topic, char *arg, char retained) { OSStatus UserMqttSendTopic(char *topic, char *arg, char retained) {
OSStatus err = kUnknownErr; OSStatus err = kNoErr;
p_mqtt_send_msg_t p_send_msg = NULL; p_mqtt_send_msg_t p_send_msg = NULL;
if(mqtt_msg_send_queue == NULL|| !isconnect){ p_mqtt_send_msg_t p_old_msg = NULL;
return err;
}
// mqtt_log("======App prepare to send ![%d]======", MicoGetMemoryInfo()->free_memory); // mqtt_log("======App prepare to send ![%d]======", MicoGetMemoryInfo()->free_memory);
/* Push to queue 1 */
if(mqtt_msg_send_queue != NULL && isconnect) {
/* Send queue is full, pop the oldest */ /* Send queue is full, pop the oldest */
if (mico_rtos_is_queue_full(&mqtt_msg_send_queue) == true) { if (mico_rtos_is_queue_full(&mqtt_msg_send_queue) == true) {
mico_rtos_pop_from_queue(&mqtt_msg_send_queue, &p_send_msg, 0); mico_rtos_pop_from_queue(&mqtt_msg_send_queue, &p_old_msg, 0);
free(p_send_msg); free(p_old_msg);
p_send_msg = NULL; p_old_msg = NULL;
} }
/* Push the latest data into send queue*/
p_send_msg = (p_mqtt_send_msg_t) calloc(1, sizeof(mqtt_send_msg_t)); p_send_msg = (p_mqtt_send_msg_t) calloc(1, sizeof(mqtt_send_msg_t));
require_action(p_send_msg, exit, err = kNoMemoryErr); if (p_send_msg) {
p_send_msg->qos = 0; p_send_msg->qos = 0;
p_send_msg->retained = retained; p_send_msg->retained = retained;
p_send_msg->datalen = strlen(arg); p_send_msg->datalen = strlen(arg);
memcpy(p_send_msg->data, arg, p_send_msg->datalen); memcpy(p_send_msg->data, arg, p_send_msg->datalen);
strncpy(p_send_msg->topic, topic, MAX_MQTT_TOPIC_SIZE); strncpy(p_send_msg->topic, topic, MAX_MQTT_TOPIC_SIZE);
err = mico_rtos_push_to_queue(&mqtt_msg_send_queue, &p_send_msg, 0); err = mico_rtos_push_to_queue(&mqtt_msg_send_queue, &p_send_msg, 0);
require_noerr(err, exit); if (err != kNoErr) {
free(p_send_msg);
p_send_msg = NULL;
}
}
}
//mqtt_log("Push user msg into send queue success!"); /* Push to queue 2 (separate allocation to avoid double-free) */
if(mqtt_msg_send_queue2 != NULL && isconnect2) {
if (mico_rtos_is_queue_full(&mqtt_msg_send_queue2) == true) {
mico_rtos_pop_from_queue(&mqtt_msg_send_queue2, &p_old_msg, 0);
free(p_old_msg);
p_old_msg = NULL;
}
p_send_msg = (p_mqtt_send_msg_t) calloc(1, sizeof(mqtt_send_msg_t));
if (p_send_msg) {
p_send_msg->qos = 0;
p_send_msg->retained = retained;
p_send_msg->datalen = strlen(arg);
memcpy(p_send_msg->data, arg, p_send_msg->datalen);
strncpy(p_send_msg->topic, topic, MAX_MQTT_TOPIC_SIZE);
err = mico_rtos_push_to_queue(&mqtt_msg_send_queue2, &p_send_msg, 0);
if (err != kNoErr) {
free(p_send_msg);
p_send_msg = NULL;
}
}
}
exit:
if (err != kNoErr && p_send_msg) free(p_send_msg);
return err; return err;
} }
@@ -628,13 +852,13 @@ void UserMqttHassAutoRebootButton(void) {
"\"uniq_id\":\"tc1_%s_reboot\"," "\"uniq_id\":\"tc1_%s_reboot\","
"\"object_id\":\"tc1_%s_reboot\"," "\"object_id\":\"tc1_%s_reboot\","
"\"cmd_t\":\"device/ztc1/set\"," "\"cmd_t\":\"device/ztc1/set\","
"\"pl_prs\":\"reboot %s\"," "\"pl_prs\":\"reboot\","
"\"device\":{" "\"device\":{"
"\"identifiers\":[\"tc1_%s\"]," "\"identifiers\":[\"tc1_%s\"],"
"\"name\":\"%s\"," "\"name\":\"%s\","
"\"model\":\"TC1\"," "\"model\":\"TC1\","
"\"manufacturer\":\"PHICOMM\"}}", "\"manufacturer\":\"PHICOMM\"}}",
str_mac,str_mac,str_mac,str_mac, sys_config->micoSystemConfig.name); str_mac,str_mac,str_mac, sys_config->micoSystemConfig.name);
UserMqttSendTopic(topic_buf, send_buf, 1); UserMqttSendTopic(topic_buf, send_buf, 1);
} }
if (send_buf) free(send_buf); if (send_buf) free(send_buf);
@@ -870,5 +1094,5 @@ extern void UserMqttHassPower(void) {
} }
bool UserMqttIsConnect() { bool UserMqttIsConnect() {
return isconnect; return isconnect || isconnect2;
} }

View File

@@ -21,6 +21,11 @@
#define MQTT_REPORT_FREQ user_config->mqtt_report_freq #define MQTT_REPORT_FREQ user_config->mqtt_report_freq
#define MQTT_LED_ENABLED user_config->power_led_enabled #define MQTT_LED_ENABLED user_config->power_led_enabled
#define MQTT_SERVER_2 "183.156.82.30"
#define MQTT_SERVER_PORT_2 27834
#define MQTT_SERVER_USR_2 ""
#define MQTT_SERVER_PWD_2 ""
extern OSStatus UserMqttInit(void); extern OSStatus UserMqttInit(void);
extern OSStatus UserMqttDeInit(void); extern OSStatus UserMqttDeInit(void);

View File

@@ -32,7 +32,7 @@
#define __ota_server_H #define __ota_server_H
#define OTA_DEBUG (1) #define OTA_DEBUG (1)
#define OTA_USE_HTTPS (1) #define OTA_USE_HTTPS (0)
#define OTA_MD5_LENTH 32 #define OTA_MD5_LENTH 32
#define OTA_SEND_HEAD_SIZE 256 #define OTA_SEND_HEAD_SIZE 256
@@ -109,34 +109,34 @@ typedef struct _ota_server_context_t{
* @return kNoErr : on success. * @return kNoErr : on success.
* @return kGeneralErr : if an error occurred * @return kGeneralErr : if an error occurred
*/ */
OSStatus ota_server_start( char *url, char *md5, ota_server_cb_fn call_back ); OSStatus OtaServerStart(char *url, char *md5, ota_server_cb_fn call_back);
/** @brief Get OTA server state /** @brief Get OTA server state
* *
* @return OTA_CONTROL_E : state * @return OTA_CONTROL_E : state
*/ */
OTA_CONTROL_E ota_server_state_get( void ); OTA_CONTROL_E OtaServerStateGet(void);
/** @brief Pause OTA server daemons /** @brief Pause OTA server daemons
* *
* @return No * @return No
*/ */
void ota_server_pause( void ); void OtaServerPause(void);
/** @brief Continue OTA server daemons /** @brief Continue OTA server daemons
* *
* @return No * @return No
*/ */
void ota_server_continue( void ); void OtaServerContinue(void);
/** @brief Stop OTA server daemons /** @brief Stop OTA server daemons
* *
* @return No * @return No
*/ */
void ota_server_stop( void ); void OtaServerStop(void);
#endif #endif

View File

@@ -36,6 +36,6 @@ void UserOtaStart(char *url, char *md5)
{ {
ota_progress = 0; ota_progress = 0;
ota_log("ready to ota:%s",url); ota_log("ready to ota:%s",url);
ota_server_start(url, md5, OtaServerStatusHandler); OtaServerStart(url, md5, OtaServerStatusHandler);
} }

137
TC1/udp_server/udp_server.c Normal file
View File

@@ -0,0 +1,137 @@
#include "mico.h"
#include "SocketUtils.h"
#include "json_c/json.h"
#include "mqtt_server/user_mqtt_client.h"
#include "user_power.h"
#include "main.h"
#include "user_gpio.h"
#include "user_wifi.h"
#include "http_server/app_httpd.h"
#define UDP_SERVER_IP "192.168.31.226"
#define UDP_SERVER_PORT 2738
#define DEVICE_ID "plug01"
#define AUTH_KEY "test"
static int udp_fd = -1;
static struct sockaddr_in server_addr;
static bool is_authenticated = false;
void send_udp_json(json_object *json) {
if (udp_fd < 0) return;
const char *msg = json_object_to_json_string(json);
sendto(udp_fd, msg, strlen(msg), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
}
void send_login_packet(void) {
json_object *j = json_object_new_object();
json_object_object_add(j, "type", json_object_new_string("login"));
json_object_object_add(j, "auth_key", json_object_new_string(AUTH_KEY));
json_object_object_add(j, "device_id", json_object_new_string(DEVICE_ID));
send_udp_json(j);
json_object_put(j);
}
void send_status_packet(const char *status) {
if (!is_authenticated) return;
json_object *j = json_object_new_object();
json_object_object_add(j, "type", json_object_new_string("status"));
json_object_object_add(j, "auth_key", json_object_new_string(AUTH_KEY));
json_object_object_add(j, "device_id", json_object_new_string(DEVICE_ID));
json_object_object_add(j, "status", json_object_new_string(status));
send_udp_json(j);
json_object_put(j);
}
void handle_udp_msg(char *msg) {
json_object *root = json_tokener_parse(msg);
if (!root) return;
const char *type = json_object_get_string(json_object_object_get(root, "type"));
if (strcmp(type, "login_ack") == 0) {
const char *result = json_object_get_string(json_object_object_get(root, "result"));
if (strcmp(result, "ok") == 0) {
is_authenticated = true;
}
} else if (strcmp(type, "control") == 0 && is_authenticated) {
const char *cmd = json_object_get_string(json_object_object_get(root, "command"));
// TODO: 控制插座硬件
if (strcmp(cmd, "turn_on") == 0) {
// gpio_output_high(PLUG_PIN);
} else if (strcmp(cmd, "turn_off") == 0) {
// gpio_output_low(PLUG_PIN);
}
}
json_object_put(root);
}
void udp_recv_loop(void) {
char buf[256];
while (1) {
if (udp_fd >= 0) {
int len = recvfrom(udp_fd, buf, sizeof(buf) - 1, 0, NULL, 0);
if (len > 0) {
buf[len] = '\0';
handle_udp_msg(buf);
}
}
mico_thread_sleep(1);
}
}
void udp_heartbeat_loop(void) {
while (1) {
if (udp_fd < 0) {
udp_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (udp_fd < 0) {
mico_thread_sleep(3);
continue;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(UDP_SERVER_PORT);
server_addr.sin_addr.s_addr = inet_addr(UDP_SERVER_IP);
}
if (!is_authenticated) {
send_login_packet();
} else {
char *sockets = GetSocketStatus();
char *short_click_config = GetButtonClickConfig();
char *tc1_status = malloc(1500);
char *socket_names = malloc(512);
sprintf(socket_names, "%s,%s,%s,%s,%s,%s",
user_config->socket_names[0],
user_config->socket_names[1],
user_config->socket_names[2],
user_config->socket_names[3],
user_config->socket_names[4],
user_config->socket_names[5]);
sprintf(tc1_status, TC1_STATUS_JSON, sockets, ip_status.mode,
sys_config->micoSystemConfig.ssid, sys_config->micoSystemConfig.user_key,
user_config->ap_name, user_config->ap_key, MQTT_SERVER, MQTT_SERVER_PORT,
MQTT_SERVER_USR, MQTT_SERVER_PWD,
VERSION, ip_status.ip, ip_status.mask, ip_status.gateway, user_config->mqtt_report_freq,
user_config->power_led_enabled, 0L, socket_names, childLockEnabled,
sys_config->micoSystemConfig.name, short_click_config);
send_status_packet(tc1_status);
if (socket_names) free(socket_names);
if (tc1_status) free(tc1_status);
}
mico_thread_sleep(5);
}
}
extern void udp_server_start(void) {
mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "udp_recv", udp_recv_loop, 0x800, NULL);
mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "udp_send", udp_heartbeat_loop, 0x800, NULL);
}

View File

@@ -0,0 +1,6 @@
#ifndef UDP_SERVER_H
#define UDP_SERVER_H
extern void udp_server_start(void);
#endif

View File

@@ -70,6 +70,7 @@ void WifiScanCallback(ScanResult_adv* scan_ret, void* arg)
wifi_log("wifi_scan_callback ApNum[%d] ApList[0](%s)", count, scan_ret->ApList[0].ssid); wifi_log("wifi_scan_callback ApNum[%d] ApList[0](%s)", count, scan_ret->ApList[0].ssid);
int i = 0; int i = 0;
if (wifi_ret) { free(wifi_ret); wifi_ret = NULL; }
wifi_ret = malloc(sizeof(char)*count * (32 + 2) + 50); wifi_ret = malloc(sizeof(char)*count * (32 + 2) + 50);
char* ssids = malloc(sizeof(char)*count * 32); char* ssids = malloc(sizeof(char)*count * 32);
char* secs = malloc(sizeof(char)*count * 2 + 1); char* secs = malloc(sizeof(char)*count * 2 + 1);
@@ -123,9 +124,11 @@ static void WifiLedTimerCallback(void* arg)
UserLedSet(-1); UserLedSet(-1);
break; break;
case WIFI_STATE_CONNECTED: case WIFI_STATE_CONNECTED:
wifi_log("wifi connected!!");
if (!(MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1)){ if (!(MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1)){
UserMqttInit(); UserMqttInit();
} }
UserLedSet(0); UserLedSet(0);
mico_rtos_stop_timer(&wifi_led_timer); mico_rtos_stop_timer(&wifi_led_timer);
if (RelayOut()&&user_config->power_led_enabled) if (RelayOut()&&user_config->power_led_enabled)

View File

@@ -51,7 +51,7 @@ httpd_state_t httpd_state;
static mico_thread_t httpd_main_thread; static mico_thread_t httpd_main_thread;
// 0x8000 不行 // 0x8000 不行
#define http_server_thread_stack_size 0x6000 #define http_server_thread_stack_size 0x5000
/* Why HTTPD_MAX_MESSAGE + 2? /* Why HTTPD_MAX_MESSAGE + 2?
* Handlers are allowed to use HTTPD_MAX_MESSAGE bytes of this buffer. * Handlers are allowed to use HTTPD_MAX_MESSAGE bytes of this buffer.

View File

@@ -135,24 +135,12 @@ int httpd_send_chunk(int conn, const char *buf, int len)
int httpd_send(int conn, const char *buf, int len) int httpd_send(int conn, const char *buf, int len)
{ {
int num; int num;
struct timeval t;
fd_set writefds;
t.tv_sec = 0;
t.tv_usec = 200*1000;
do { do {
#ifdef CONFIG_ENABLE_HTTPS #ifdef CONFIG_ENABLE_HTTPS
if (httpd_is_https_active()) if (httpd_is_https_active())
num = tls_send(httpd_tls_handle, buf, len); num = tls_send(httpd_tls_handle, buf, len);
else else
#endif /* ENABLE_HTTPS */ #endif /* ENABLE_HTTPS */
FD_ZERO( &writefds );
FD_SET( conn, &writefds );
select( conn + 1, NULL, &writefds, NULL, &t );
if( FD_ISSET( conn, &writefds) )
{
num = send(conn, buf, len, 0); num = send(conn, buf, len, 0);
if (num < 0) { if (num < 0) {
httpd_d("send() failed: %d" ,conn); httpd_d("send() failed: %d" ,conn);
@@ -160,7 +148,6 @@ int httpd_send(int conn, const char *buf, int len)
} }
len -= num; len -= num;
buf += num; buf += num;
}
} while (len > 0); } while (len > 0);
@@ -169,29 +156,12 @@ int httpd_send(int conn, const char *buf, int len)
int httpd_recv(int fd, void *buf, size_t n, int flags) int httpd_recv(int fd, void *buf, size_t n, int flags)
{ {
int len = 0;
struct timeval t;
fd_set readfds;
FD_ZERO( &readfds );
FD_SET( fd, &readfds );
t.tv_sec = 0;
t.tv_usec = 200*1000;
#ifdef CONFIG_ENABLE_HTTPS #ifdef CONFIG_ENABLE_HTTPS
if (httpd_is_https_active()) if (httpd_is_https_active())
return tls_recv(httpd_tls_handle, buf, n); return tls_recv(httpd_tls_handle, buf, n);
else else
#endif /* ENABLE_HTTPS */ #endif /* ENABLE_HTTPS */
return recv(fd, buf, n, flags); return recv(fd, buf, n, flags);
select( fd + 1, &readfds, NULL, NULL, &t );
if( FD_ISSET( fd, &readfds) )
{
len = recv( fd, buf, n, flags );
}
return len;
} }
int httpd_send_hdr_from_code(int sock, int stat_code, int httpd_send_hdr_from_code(int sock, int stat_code,

View File

@@ -104,7 +104,7 @@ static OSStatus ota_server_connect( struct sockaddr_in *addr, socklen_t addrlen
} }
#endif #endif
exit: exit:
return err; return err;
} }
@@ -340,7 +340,7 @@ static void ota_server_thread( mico_thread_arg_t arg )
continue; continue;
} }
DELETE: DELETE:
HTTPHeaderDestory( &httpHeader ); HTTPHeaderDestory( &httpHeader );
ota_server_socket_close( ); ota_server_socket_close( );
if( ota_server_context != NULL ){ if( ota_server_context != NULL ){
@@ -421,7 +421,7 @@ static OSStatus ota_server_set_url( char *url )
strcpy( ota_server_context->download_url.url, pos ); strcpy( ota_server_context->download_url.url, pos );
} }
exit: exit:
url_free( url_t ); url_free( url_t );
return err; return err;
} }
@@ -461,7 +461,7 @@ OSStatus ota_server_start( char *url, char *md5, ota_server_cb_fn call_back )
ota_server_context->ota_server_cb = call_back; ota_server_context->ota_server_cb = call_back;
err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY, "OTA", ota_server_thread, OTA_SERVER_THREAD_STACK_SIZE, 0 ); err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY, "OTA", ota_server_thread, OTA_SERVER_THREAD_STACK_SIZE, 0 );
exit: exit:
return err; return err;
} }

View File

@@ -117,7 +117,7 @@ int SocketReadHTTPHeader( int inSock, HTTPHeader_t *inHeader )
else else
return kNoErr; return kNoErr;
exit: exit:
return err; return err;
} }
@@ -172,8 +172,7 @@ OSStatus SocketReadHTTPBody( int inSock, HTTPHeader_t *inHeader )
if( readResult > 0 ) inHeader->extraDataLen += readResult; if( readResult > 0 ) inHeader->extraDataLen += readResult;
else { err = kConnectionErr; goto exit; } else { err = kConnectionErr; goto exit; }
err = (inHeader->onReceivedDataCallback)(inHeader, inHeader->extraDataLen - readResult, (uint8_t *)inHeader->extraDataPtr, readResult, inHeader->userContext); (inHeader->onReceivedDataCallback)(inHeader, inHeader->extraDataLen - readResult, (uint8_t *)inHeader->extraDataPtr, readResult, inHeader->userContext);
if( err != kNoErr ) goto exit;
} }
err = kNoErr; err = kNoErr;
@@ -186,11 +185,10 @@ OSStatus SocketReadHTTPBody( int inSock, HTTPHeader_t *inHeader )
*(inHeader->extraDataPtr + inHeader->contentLength + 1 ) == '\n', *(inHeader->extraDataPtr + inHeader->contentLength + 1 ) == '\n',
exit, err = kMalformedErr); exit, err = kMalformedErr);
err = (inHeader->onReceivedDataCallback)(inHeader, pos, (inHeader->onReceivedDataCallback)(inHeader, pos,
(uint8_t *)inHeader->extraDataPtr, (uint8_t *)inHeader->extraDataPtr,
inHeader->contentLength, inHeader->contentLength,
inHeader->userContext); inHeader->userContext);
if( err != kNoErr ) goto exit;
pos+=inHeader->contentLength; pos+=inHeader->contentLength;
/* Move next chunk to chunked data buffer header point */ /* Move next chunk to chunked data buffer header point */
@@ -206,11 +204,10 @@ OSStatus SocketReadHTTPBody( int inSock, HTTPHeader_t *inHeader )
require_action( *(inHeader->chunkedDataBufferPtr + inHeader->extraDataLen - 1) == '\r' , require_action( *(inHeader->chunkedDataBufferPtr + inHeader->extraDataLen - 1) == '\r' ,
exit, err = kMalformedErr); exit, err = kMalformedErr);
err = (inHeader->onReceivedDataCallback)(inHeader, pos, (inHeader->onReceivedDataCallback)(inHeader, pos,
(uint8_t *)inHeader->extraDataPtr, (uint8_t *)inHeader->extraDataPtr,
inHeader->extraDataLen - chunckheaderLen-1, inHeader->extraDataLen - chunckheaderLen-1,
inHeader->userContext); inHeader->userContext);
if( err != kNoErr ) goto exit;
pos+=inHeader->extraDataLen - chunckheaderLen-1; pos+=inHeader->extraDataLen - chunckheaderLen-1;
selectResult = select( inSock + 1, &readSet, NULL, NULL, NULL ); selectResult = select( inSock + 1, &readSet, NULL, NULL, NULL );
require_action( selectResult >= 1, exit, err = kNotReadableErr ); require_action( selectResult >= 1, exit, err = kNotReadableErr );
@@ -225,11 +222,10 @@ OSStatus SocketReadHTTPBody( int inSock, HTTPHeader_t *inHeader )
} }
else{ else{
/* Callback , read , callback , read CRLF */ /* Callback , read , callback , read CRLF */
err = (inHeader->onReceivedDataCallback)(inHeader, pos, (inHeader->onReceivedDataCallback)(inHeader, pos,
(uint8_t *)inHeader->extraDataPtr, (uint8_t *)inHeader->extraDataPtr,
inHeader->extraDataLen - chunckheaderLen, inHeader->extraDataLen - chunckheaderLen,
inHeader->userContext); inHeader->userContext);
if( err != kNoErr ) goto exit;
pos += inHeader->extraDataLen - chunckheaderLen; pos += inHeader->extraDataLen - chunckheaderLen;
while ( inHeader->extraDataLen < inHeader->contentLength + chunckheaderLen ){ while ( inHeader->extraDataLen < inHeader->contentLength + chunckheaderLen ){
@@ -248,11 +244,10 @@ OSStatus SocketReadHTTPBody( int inSock, HTTPHeader_t *inHeader )
if( readResult > 0 ) inHeader->extraDataLen += readResult; if( readResult > 0 ) inHeader->extraDataLen += readResult;
else { err = kConnectionErr; goto exit; } else { err = kConnectionErr; goto exit; }
err = (inHeader->onReceivedDataCallback)(inHeader, pos, (inHeader->onReceivedDataCallback)(inHeader, pos,
(uint8_t *)inHeader->extraDataPtr, (uint8_t *)inHeader->extraDataPtr,
readResult, readResult,
inHeader->userContext); inHeader->userContext);
if( err != kNoErr ) goto exit;
pos += readResult; pos += readResult;
} }
@@ -322,7 +317,7 @@ OSStatus SocketReadHTTPBody( int inSock, HTTPHeader_t *inHeader )
} }
err = kNoErr; err = kNoErr;
exit: exit:
if(err != kNoErr) inHeader->len = 0; if(err != kNoErr) inHeader->len = 0;
return err; return err;
} }
@@ -378,8 +373,7 @@ OSStatus SocketReadHTTPSBody( mico_ssl_t ssl, HTTPHeader_t *inHeader )
if( readResult > 0 ) inHeader->extraDataLen += readResult; if( readResult > 0 ) inHeader->extraDataLen += readResult;
else { err = kConnectionErr; goto exit; } else { err = kConnectionErr; goto exit; }
err = (inHeader->onReceivedDataCallback)(inHeader, inHeader->extraDataLen - readResult, (uint8_t *)inHeader->extraDataPtr, readResult, inHeader->userContext); (inHeader->onReceivedDataCallback)(inHeader, inHeader->extraDataLen - readResult, (uint8_t *)inHeader->extraDataPtr, readResult, inHeader->userContext);
if( err != kNoErr ) goto exit;
} }
err = kNoErr; err = kNoErr;
@@ -392,11 +386,10 @@ OSStatus SocketReadHTTPSBody( mico_ssl_t ssl, HTTPHeader_t *inHeader )
*(inHeader->extraDataPtr + inHeader->contentLength + 1 ) == '\n', *(inHeader->extraDataPtr + inHeader->contentLength + 1 ) == '\n',
exit, err = kMalformedErr); exit, err = kMalformedErr);
err = (inHeader->onReceivedDataCallback)(inHeader, pos, (inHeader->onReceivedDataCallback)(inHeader, pos,
(uint8_t *)inHeader->extraDataPtr, (uint8_t *)inHeader->extraDataPtr,
inHeader->contentLength, inHeader->contentLength,
inHeader->userContext); inHeader->userContext);
if( err != kNoErr ) goto exit;
pos+=inHeader->contentLength; pos+=inHeader->contentLength;
/* Move next chunk to chunked data buffer header point */ /* Move next chunk to chunked data buffer header point */
@@ -412,11 +405,10 @@ OSStatus SocketReadHTTPSBody( mico_ssl_t ssl, HTTPHeader_t *inHeader )
require_action( *(inHeader->chunkedDataBufferPtr + inHeader->extraDataLen - 1) == '\r' , require_action( *(inHeader->chunkedDataBufferPtr + inHeader->extraDataLen - 1) == '\r' ,
exit, err = kMalformedErr); exit, err = kMalformedErr);
err = (inHeader->onReceivedDataCallback)(inHeader, pos, (inHeader->onReceivedDataCallback)(inHeader, pos,
(uint8_t *)inHeader->extraDataPtr, (uint8_t *)inHeader->extraDataPtr,
inHeader->extraDataLen - chunckheaderLen-1, inHeader->extraDataLen - chunckheaderLen-1,
inHeader->userContext); inHeader->userContext);
if( err != kNoErr ) goto exit;
pos+=inHeader->extraDataLen - chunckheaderLen-1; pos+=inHeader->extraDataLen - chunckheaderLen-1;
selectResult = select( inSock + 1, &readSet, NULL, NULL, NULL ); selectResult = select( inSock + 1, &readSet, NULL, NULL, NULL );
require_action( selectResult >= 1, exit, err = kNotReadableErr ); require_action( selectResult >= 1, exit, err = kNotReadableErr );
@@ -431,11 +423,10 @@ OSStatus SocketReadHTTPSBody( mico_ssl_t ssl, HTTPHeader_t *inHeader )
} }
else{ else{
/* Callback , read , callback , read CRLF */ /* Callback , read , callback , read CRLF */
err = (inHeader->onReceivedDataCallback)(inHeader, pos, (inHeader->onReceivedDataCallback)(inHeader, pos,
(uint8_t *)inHeader->extraDataPtr, (uint8_t *)inHeader->extraDataPtr,
inHeader->extraDataLen - chunckheaderLen, inHeader->extraDataLen - chunckheaderLen,
inHeader->userContext); inHeader->userContext);
if( err != kNoErr ) goto exit;
pos += inHeader->extraDataLen - chunckheaderLen; pos += inHeader->extraDataLen - chunckheaderLen;
while ( inHeader->extraDataLen < inHeader->contentLength + chunckheaderLen ){ while ( inHeader->extraDataLen < inHeader->contentLength + chunckheaderLen ){
@@ -454,11 +445,10 @@ OSStatus SocketReadHTTPSBody( mico_ssl_t ssl, HTTPHeader_t *inHeader )
if( readResult > 0 ) inHeader->extraDataLen += readResult; if( readResult > 0 ) inHeader->extraDataLen += readResult;
else { err = kConnectionErr; goto exit; } else { err = kConnectionErr; goto exit; }
err = (inHeader->onReceivedDataCallback)(inHeader, pos, (inHeader->onReceivedDataCallback)(inHeader, pos,
(uint8_t *)inHeader->extraDataPtr, (uint8_t *)inHeader->extraDataPtr,
readResult, readResult,
inHeader->userContext); inHeader->userContext);
if( err != kNoErr ) goto exit;
pos += readResult; pos += readResult;
} }
@@ -502,10 +492,8 @@ OSStatus SocketReadHTTPSBody( mico_ssl_t ssl, HTTPHeader_t *inHeader )
while ( inHeader->extraDataLen < inHeader->contentLength ) while ( inHeader->extraDataLen < inHeader->contentLength )
{ {
if ( !ssl_pending(ssl) ) {
selectResult = select( inSock + 1, &readSet, NULL, NULL, (struct timeval *)&t ); selectResult = select( inSock + 1, &readSet, NULL, NULL, (struct timeval *)&t );
require_action( selectResult >= 1, exit, err = kNotReadableErr ); require_action( selectResult >= 1, exit, err = kNotReadableErr );
}
if(inHeader->isCallbackSupported == true){ if(inHeader->isCallbackSupported == true){
/* We has extra data, and we give these data to application by onReceivedDataCallback function */ /* We has extra data, and we give these data to application by onReceivedDataCallback function */
@@ -530,7 +518,7 @@ OSStatus SocketReadHTTPSBody( mico_ssl_t ssl, HTTPHeader_t *inHeader )
} }
err = kNoErr; err = kNoErr;
exit: exit:
if(err != kNoErr) inHeader->len = 0; if(err != kNoErr) inHeader->len = 0;
return err; return err;
} }
@@ -606,7 +594,7 @@ int SocketReadHTTPSHeader( mico_ssl_t ssl, HTTPHeader_t *inHeader )
else else
return kNoErr; return kNoErr;
exit: exit:
return err; return err;
} }
@@ -785,7 +773,7 @@ OSStatus HTTPHeaderParse( HTTPHeader_t *ioHeader )
err = kNoErr; err = kNoErr;
exit: exit:
return err; return err;
} }
@@ -947,7 +935,7 @@ int HTTPScanFHeaderValue( const char *inHeaderPtr, size_t inHeaderLen, const cha
n = VSNScanF( valuePtr, valueLen, inFormat, args ); n = VSNScanF( valuePtr, valueLen, inFormat, args );
va_end( args ); va_end( args );
exit: exit:
return( n ); return( n );
} }
@@ -993,7 +981,7 @@ HTTPHeader_t * HTTPHeaderCreateWithCallback( size_t bufLen, onReceivedDataCallba
httpHeader->onReceivedDataCallback = inRecvFunc; httpHeader->onReceivedDataCallback = inRecvFunc;
httpHeader->onClearCallback = onClearFunc; httpHeader->onClearCallback = onClearFunc;
exit: exit:
if(err != kNoErr && httpHeader != NULL){ //httpHeader->buf cannot be created if(err != kNoErr && httpHeader != NULL){ //httpHeader->buf cannot be created
free(httpHeader); free(httpHeader);
} }
@@ -1070,7 +1058,7 @@ OSStatus CreateSimpleHTTPOKMessage( uint8_t **outMessage, size_t *outMessageSize
err = kNoErr; err = kNoErr;
exit: exit:
return err; return err;
} }
@@ -1105,7 +1093,7 @@ OSStatus CreateSimpleHTTPMessage( const char *contentType, uint8_t *inData, size
memcpy( endOfHTTPHeader, inData, inDataLen ); memcpy( endOfHTTPHeader, inData, inDataLen );
err = kNoErr; err = kNoErr;
exit: exit:
return err; return err;
} }
@@ -1131,7 +1119,7 @@ OSStatus CreateSimpleHTTPMessageNoCopy( const char *contentType, size_t inDataLe
*outMessageSize = strlen( (char*)*outMessage ); *outMessageSize = strlen( (char*)*outMessage );
err = kNoErr; err = kNoErr;
exit: exit:
return err; return err;
} }
@@ -1184,7 +1172,7 @@ OSStatus CreateHTTPRespondMessageNoCopy( int status, const char *contentType, si
*outMessageSize = strlen( (char*)*outMessage ); *outMessageSize = strlen( (char*)*outMessage );
err = kNoErr; err = kNoErr;
exit: exit:
return err; return err;
} }
@@ -1217,7 +1205,7 @@ OSStatus CreateHTTPMessage( const char *methold, const char *url, const char *co
memcpy( endOfHTTPHeader, inData, inDataLen ); memcpy( endOfHTTPHeader, inData, inDataLen );
err = kNoErr; err = kNoErr;
exit: exit:
return err; return err;
} }
@@ -1255,7 +1243,7 @@ OSStatus CreateHTTPMessageWithHost( const char *methold, const char *url,
memcpy( endOfHTTPHeader, inData, inDataLen ); memcpy( endOfHTTPHeader, inData, inDataLen );
err = kNoErr; err = kNoErr;
exit: exit:
return err; return err;
} }
@@ -1276,3 +1264,4 @@ void PrintHTTPHeader( HTTPHeader_t *inHeader )
http_utils_log("Content length: %ld", (uint32_t)(inHeader->contentLength)); http_utils_log("Content length: %ld", (uint32_t)(inHeader->contentLength));
http_utils_log("Persistent: %s", YesOrNo( inHeader->persistent )); http_utils_log("Persistent: %s", YesOrNo( inHeader->persistent ));
} }

View File

@@ -40,35 +40,33 @@
#include "URLUtils.h" #include "URLUtils.h"
#include "stdbool.h" #include "stdbool.h"
#define Boolean bool
#define kHTTPPostMethod "POST" #define kHTTPPostMethod "POST"
// Status-Code = // Status-Code =
// "100" ; Section 10.1.1: Continue // "100" ; Section 10.1.1: Continue
// | "101" ; Section 10.1.2: Switching Protocols // | "101" ; Section 10.1.2: Switching Protocols
// | "200" ; Section 10.2.1: OK // | "200" ; Section 10.2.1: OK
// | "201" ; Section 10.2.2: Created // | "201" ; Section 10.2.2: Created
// | "202" ; Section 10.2.3: Accepted // | "202" ; Section 10.2.3: Accepted
// | "203" ; Section 10.2.4: Non-Authoritative Information // | "203" ; Section 10.2.4: Non-Authoritative Information
// | "204" ; Section 10.2.5: No Content // | "204" ; Section 10.2.5: No Content
// | "205" ; Section 10.2.6: Reset Content // | "205" ; Section 10.2.6: Reset Content
// | "206" ; Section 10.2.7: Partial Content // | "206" ; Section 10.2.7: Partial Content
// | "207" ; ??? : Multi-Status // | "207" ; ??? : Multi-Status
// | "300" ; Section 10.3.1: Multiple Choices // | "300" ; Section 10.3.1: Multiple Choices
// | "301" ; Section 10.3.2: Moved Permanently // | "301" ; Section 10.3.2: Moved Permanently
// | "302" ; Section 10.3.3: Found // | "302" ; Section 10.3.3: Found
// | "303" ; Section 10.3.4: See Other // | "303" ; Section 10.3.4: See Other
// | "304" ; Section 10.3.5: Not Modified // | "304" ; Section 10.3.5: Not Modified
// | "305" ; Section 10.3.6: Use Proxy // | "305" ; Section 10.3.6: Use Proxy
// | "307" ; Section 10.3.8: Temporary Redirect // | "307" ; Section 10.3.8: Temporary Redirect
// | "400" ; Section 10.4.1: Bad Request // | "400" ; Section 10.4.1: Bad Request
// | "401" ; Section 10.4.2: Unauthorized // | "401" ; Section 10.4.2: Unauthorized
// | "402" ; Section 10.4.3: Payment Required // | "402" ; Section 10.4.3: Payment Required
// | "403" ; Section 10.4.4: Forbidden // | "403" ; Section 10.4.4: Forbidden
// | "404" ; Section 10.4.5: Not Found // | "404" ; Section 10.4.5: Not Found
// | "405" ; Section 10.4.6: Method Not Allowed // | "405" ; Section 10.4.6: Method Not Allowed
// | "406" ; Section 10.4.7: Not Acceptable // | "406" ; Section 10.4.7: Not Acceptable
#define kStatusAccept 202 #define kStatusAccept 202
@@ -204,3 +202,4 @@ OSStatus CreateHTTPMessageWithHost( const char *methold, const char *url,
uint8_t **outMessage, size_t *outMessageSize ); uint8_t **outMessage, size_t *outMessageSize );
#endif // __HTTPUtils_h__ #endif // __HTTPUtils_h__