liujie
2026-08-11 323764c813a09cd56cdab780155ef659dbe0b364
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.ruoyi.web.integration.dahua;
 
import com.sun.jna.Memory;
import com.sun.jna.Native;
 
import java.nio.charset.StandardCharsets;
 
/**
 * 仅用于联调阶段验证 NetSDK 原生库及设备登录,不执行任何设备写操作。
 */
public final class DahuaLoginProbe {
 
    private DahuaLoginProbe() {
    }
 
    public static void main(String[] args) {
        if (args.length != 4) {
            throw new IllegalArgumentException("参数:原生库路径、设备IP、端口、用户名");
        }
        String password = System.getenv("DAHUA_PROBE_PASSWORD");
        if (password == null || password.isEmpty()) {
            throw new IllegalArgumentException("缺少 DAHUA_PROBE_PASSWORD");
        }
 
        DahuaNetSdk sdk = Native.load(args[0], DahuaNetSdk.class);
        if (!sdk.CLIENT_Init(null, null)) {
            throw new IllegalStateException("CLIENT_Init failed: " + formatError(sdk.CLIENT_GetLastError()));
        }
        try {
            sdk.CLIENT_SetConnectTime(5000, 1);
            Memory input = memory(DahuaSdkLayout.LOGIN_INPUT_SIZE);
            input.setInt(0, DahuaSdkLayout.LOGIN_INPUT_SIZE);
            writeFixed(input, 4, 64, args[1]);
            input.setInt(68, Integer.parseInt(args[2]));
            writeFixed(input, 72, 64, args[3]);
            writeFixed(input, 136, 64, password);
 
            Memory output = memory(DahuaSdkLayout.LOGIN_OUTPUT_SIZE);
            output.setInt(0, DahuaSdkLayout.LOGIN_OUTPUT_SIZE);
            long loginHandle = sdk.CLIENT_LoginWithHighLevelSecurity(input, output);
            int loginError = output.getInt(DahuaSdkLayout.LOGIN_OUTPUT_ERROR_OFFSET);
            System.out.println("loginInputSize=" + DahuaSdkLayout.LOGIN_INPUT_SIZE);
            System.out.println("loginHandleValid=" + (loginHandle != 0));
            System.out.println("loginError=" + loginError);
            System.out.println("sdkError=" + formatError(sdk.CLIENT_GetLastError()));
            if (loginHandle != 0) {
                System.out.println("logoutSuccess=" + sdk.CLIENT_Logout(loginHandle));
            }
        } finally {
            sdk.CLIENT_Cleanup();
        }
    }
 
    private static Memory memory(long size) {
        Memory memory = new Memory(size);
        memory.clear();
        return memory;
    }
 
    private static void writeFixed(Memory memory, long offset, int capacity, String value) {
        byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
        if (bytes.length >= capacity) {
            throw new IllegalArgumentException("字段长度超限");
        }
        memory.write(offset, bytes, 0, bytes.length);
    }
 
    private static String formatError(int error) {
        return String.format("0x%08X", error);
    }
}