Index: /branches/rel_apv_10_7_2/usr/click/lib/libca_snmp/snmp_cadmin.c
===================================================================
--- /branches/rel_apv_10_7_2/usr/click/lib/libca_snmp/snmp_cadmin.c	(revision 39136)
+++ /branches/rel_apv_10_7_2/usr/click/lib/libca_snmp/snmp_cadmin.c	(working copy)
@@ -128,6 +128,8 @@
 #include <sys/sysctl.h>
 #include <limits.h>
 #include <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
 
 #include "snmpcli.h"
 #include "fastlog.h"
@@ -169,6 +171,104 @@
 
 extern array_model_t array_models[MAX_MODEL];
 
+/* Base64 encoding table */
+static const char b64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+/* Function to get serial number */
+int get_sr_num(char* serial_num, int slen)
+{
+    size_t len = 0;
+    FILE *fd = NULL;
+    if (slen < SERNUMLEN) {
+        return -1;
+    }
+    memset(serial_num, 0, SERNUMLEN);
+    len = MACLEN;
+    fd = fopen(SRNFN, "r");
+    if (!fd) {
+        if ((u_sysctlbyname("kern.click_id", serial_num, &len, NULL, 0) == -1)) {
+            return -1;
+        } else {
+            serial_num[MACLEN - 1] = '\0';
+        }
+    } else {
+        fscanf(fd, "%s", serial_num);
+        fclose(fd);
+        if ((u_sysctlbyname("kern.click_id", (serial_num + ARRAYIDLEN), &len, NULL, 0) == -1)) {
+            serial_num[ARRAYIDLEN] = '\0';
+        } else {
+            serial_num[SERNUMLEN - 1] = '\0';
+        }
+    }
+    return 0;
+}
+
+/* Simple base64 encoding function */
+static void base64_encode(const unsigned char *in, size_t in_len, char *out, size_t out_len)
+{
+    size_t i, o = 0;
+    unsigned int val = 0;
+    int valb = -6;
+
+    for (i = 0; i < in_len; i++) {
+        val = (val << 8) + in[i];
+        valb += 8;
+        while (valb >= 0) {
+            if (o >= out_len - 1) break; // prevent overflow
+            out[o++] = b64_table[(val >> valb) & 0x3F];
+            valb -= 6;
+        }
+    }
+    if (valb > -6 && o < out_len - 1) {
+        out[o++] = b64_table[((val << 8) >> (valb + 8)) & 0x3F];
+    }
+    while (o % 4 != 0) {
+        if (o >= out_len - 1) break;
+        out[o++] = '=';
+    }
+    out[o] = '\0';
+}
+
+/* Generate SNMP engineID (20 bytes raw, base64 encoded to 27 chars) */
+int generate_snmp_engineID(char *engineID_b64, size_t b64_len)
+{
+    if (b64_len < B64_ENGINE_ID_LEN) { // 27 chars + null terminator
+        return -1;
+    }
+
+    unsigned char engineID_raw[20];
+    char serial_num[SERNUMLEN];
+    int ret = get_sr_num(serial_num, SERNUMLEN);
+    if (ret != 0) {
+        return -2; // failed to get serial number
+    }
+
+    /* Construct RFC3411 EngineID (32 bytes):
+     * Byte 0: 0x80 (enterprise format)
+     * Bytes 1-4: Enterprise ID (7564 in network byte order)
+     * Bytes 5-24: SHA1 hash (20 bytes)
+     * Bytes 25-31: Padding zeros */
+    engineID_raw[0] = 0x80;
+    engineID_raw[1] = 0x00;
+    engineID_raw[2] = 0x00;
+    engineID_raw[3] = 0x1D;
+    engineID_raw[4] = 0x8C;
+
+    /* Fill the remaining 15bytes with serial number or hash there of
+     * use first 15 bytes of serial number (or pad with zeros) */
+    size_t sn_len = strlen(serial_num);
+    if (sn_len > 15) sn_len = 15;
+    memcpy(&engineID_raw[5], serial_num, sn_len);
+    if (sn_len < 15) {
+        memset(&engineID_raw[5 + sn_len], 0, 15 - sn_len);
+    }
+
+    /* Base64 encode rawa engineID (20bytes -> 27bytes) */
+    base64_encode(engineID_raw, sizeof(engineID_raw), engineID_b64, b64_len);
+
+    return 0;
+}
+
 /* 
  * get snmpd's pid
  * if it do not running, return 0
@@ -272,13 +372,25 @@
 	char priv_decrypt[PASSWORD_MAXLEN + 1];
 	int ret = 0;
 	int product_id = -1;
-	
+
+	/* Buffer for base64-encoded engineID */
+	char engineID_b64[B64_ENGINE_ID_LEN]; // 27 chars + null terminator
+
 	conf_fp_tmp = fopen(SNMPD_CONF_FILE_TMP, "w");
 	if (conf_fp_tmp == NULL) {
 		printf("Creat config file error.\n");
 		return -1;
 	}
 
+	/* Generate and store base64-encoded SNMP engine ID */
+	ret = generate_snmp_engineID(engineID_b64, sizeof(engineID_b64));
+    
+	if (ret != 0) {
+	    printf("Error: Failed to generate SNMP engineID\n");
+	}
+
+        /* Write base64-encoded SNMP engineID to configuration */
+	fprintf(conf_fp_tmp, "engineID %s\n", engineID_b64);
 
 	/* Load CA-SNMP-MIB dynamic library */
 #if defined(__linux__)
Index: /branches/rel_apv_10_7_2/usr/click/lib/libca_snmp/snmpcli.h
===================================================================
--- /branches/rel_apv_10_7_2/usr/click/lib/libca_snmp/snmpcli.h	(revision 39136)
+++ /branches/rel_apv_10_7_2/usr/click/lib/libca_snmp/snmpcli.h	(working copy)
@@ -79,7 +79,7 @@
 #define MAX_ENCRPT_PASSWD_LEN    ( ENCRYPTSTR_LEN + PASSWORD_MAXLEN * 2 )
 
 #define ENGINE_ID_MAX_LEN       32
-
+#define B64_ENGINE_ID_LEN	28
 #define SNMP_MAX_IPLIST         16
 #define SNMP_BUFFER_SIZE        4096
 
