add ntpd config
authorkyz <kyz@kyz-vbox.myguest.virtualbox.org>
Wed, 4 Jun 2025 07:37:33 +0000 (15:37 +0800)
committerkyz <kyz@kyz-vbox.myguest.virtualbox.org>
Wed, 4 Jun 2025 07:37:33 +0000 (15:37 +0800)
CMakeLists.txt
app/CMakeLists.txt
app/S99_alarmmonitor
app/inc/alarmmonitor.h
app/ntpd [new file with mode: 0755]
app/src/am_main.c
app/src/iot.c
app/src/iot_report.c

index a07c2dbaa10cbd869b3fd99c48d19605dfe400e8..ca7c9414b3a73e6f429f464612ee7ed524123467 100644 (file)
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.8)
 
 project(alarmmonitor)
 
-set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pie -fPIE -g2 -O2")
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pie -fPIE -g2 -O0")
 
 # Add the posix targets
 add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/app )
index 22a8a6e11b0dc8ec4d5acc05ceaa51b3f810e73d..80592f658e593c5e689407c1277ef2b8788805d7 100644 (file)
@@ -48,5 +48,6 @@ target_link_libraries(
 install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION /app)
 install(PROGRAMS ${PROJECT_SOURCE_DIR}/alarm_d DESTINATION /app)
 install(PROGRAMS ${PROJECT_SOURCE_DIR}/S99_alarmmonitor DESTINATION /etc/init.d)
+install(FILES ${PROJECT_SOURCE_DIR}/ntpd DESTINATION /etc/default/)
 install(FILES ${PROJECT_SOURCE_DIR}/log4crc DESTINATION /app)
 install(FILES ${PROJECT_SOURCE_DIR}/resolv.conf.head DESTINATION /etc)
index 6d40f503b1e146d962638668fd20dd8956202a42..7fbca590c7309e3659b39ddf2fc5c5d0d518e085 100755 (executable)
@@ -6,7 +6,6 @@
 
 case "$1" in
   start)
-       start
        /app/alarm_d start &
        ;;
   stop)
index 1d482688759db1605328a4a5e6e94da0c400fa85..b93bd9b7c5e385827ffa0a928057194a8c8816bd 100644 (file)
@@ -51,6 +51,8 @@
 
 #define PID_FILE              "/tmp/alarmmonitor.pid"
 
+#define CUSTOM_NTP_CONF       "/tmp/custom_ntp.conf"
+
 #else
 
 //ON BOARD
@@ -78,6 +80,8 @@
 
 #define PID_FILE             "/var/run/alarmmonitor/alarmmonitor.pid"
 
+#define CUSTOM_NTP_CONF      "/etc/custom_ntp.conf"
+
 #endif
 
 typedef struct{
@@ -332,6 +336,7 @@ void remote_jbkzq_power( int on );
 extern am_dev_t  g_dev;
 
 
+int ntp_update( char *ntp_srv );
 
 #endif //__ALARMMONITOR_H__
 
diff --git a/app/ntpd b/app/ntpd
new file mode 100755 (executable)
index 0000000..88d4ce1
--- /dev/null
+++ b/app/ntpd
@@ -0,0 +1,9 @@
+#!/bin/sh
+#
+# Network Time Protocol daemon ARGS
+#
+
+if [ -r "/etc/custom_ntp.conf" ]; then
+   NTPD_ARGS="${NTPD_ARGS} -c /etc/custom_ntp.conf"
+fi
+
index 793efc1c68b387ae262a974bfa46d40ca78f2cda..0f0b99747214b751836e819abd6e873da2f17ea1 100644 (file)
@@ -49,7 +49,7 @@ static int alone_runnind(void)
     }
 
     sprintf(buf, "%ld\n", (long)getpid());
-    if( write(fd, buf, strlen(buf) + 1)){
+    if( write(fd, buf, strlen(buf) )){
     }
 
     //这里不关闭文件, 保持锁定状态
@@ -151,7 +151,14 @@ void remote_jbq_power( int on )
 void remote_alarm( int alarm )
 {
     AM_LOGI( "remote alarm -> %d", alarm );
-    alarm_start( &g_dev, alarm );
+    if( alarm >=0 && alarm<=5 ){
+        alarm_start( &g_dev, alarm );
+    }else if( alarm==6 ){
+        //TTS Data, utf8 string
+        printf( "TODO: remote play TTS ....\n" );
+    }else{
+        printf( "invalid remote alarm cmd\n" );
+    }
 }
 
 int32_t device_disp_info_update( am_dev_t *dev )
@@ -208,6 +215,7 @@ void keypad_lock_timer_cb( uv_timer_t *handle )
     AM_LOGI( "KEYPAD timeout -> Locked" );
 }
 
+extern int ntp_update( char * ntp_srv );
 void button_event_cb( button_code_t btn_code, button_event_t btn_event, void* value )
 {
     int32_t alarm = 0;
@@ -322,6 +330,44 @@ int device_alarm_time_check( am_dev_t *dev )
     }
 }
 
+/*
+/etc/ntp.conf
+server ntp.ntsc.ac.cn iburst
+# Allow only time queries, at a limited rate, sending KoD when in excess.
+# Allow all local queries (IPv4, IPv6)
+restrict default nomodify nopeer noquery limited kod
+restrict 127.0.0.1
+restrict [::1]
+*/
+
+int ntp_update( char *ntp_srv )
+{
+    FILE *fd;
+    printf( "-------------------ntp update srv[%s]\n", ntp_srv );
+
+    char cmd[200];
+    sprintf( cmd, "grep %s %s", ntp_srv, CUSTOM_NTP_CONF );
+
+    if( system( cmd ) == 0 ){
+        AM_LOGI( "ntp conf already exist!\n" );
+        return 0;
+    }
+
+    fd = fopen( CUSTOM_NTP_CONF, "w" );
+    if( fd  ){
+        fprintf( fd, "server %s iburst\n" \
+                "restrict default nomodify nopeer noquery limited kod\n" \
+                "restrict 127.0.0.1\n" \
+                "restrict [::1]\n",
+                ntp_srv );
+        fclose(fd );
+        AM_LOGE( "create ntp conf[%s] ... OK!", CUSTOM_NTP_CONF );
+    }else{
+        AM_LOGE( "create ntp conf[%s] ... Fail!", CUSTOM_NTP_CONF );
+    }
+    return 0;
+}
+
 void monitor_timer_cb( uv_timer_t *handle )
 {
     btn_msg_t btn_msg;
index 33e4904819ae2ed50d421229920a21d34a27f512..bdf532215df261001fb7c15e11a493c693e54a90 100644 (file)
@@ -114,6 +114,7 @@ void  on_connect_resp( uint8_t *rx_data, uint32_t rx_sz )
 {
     cJSON *root;
     cJSON *idbm;
+    cJSON *ntp;
     cJSON *sn;
 
     root = cJSON_Parse( rx_data );
@@ -121,6 +122,7 @@ void  on_connect_resp( uint8_t *rx_data, uint32_t rx_sz )
     if( root ){
         sn = cJSON_GetObjectItem( root, "sn" );
         idbm = cJSON_GetObjectItem( root, "idbm" );
+        ntp = cJSON_GetObjectItem( root, "ntp" );
         printf( "jsondata[%s],------get idbm:%s, %s\n", rx_data, g_dev.idbm, idbm?idbm->valuestring:"null" );
         if( idbm ){
             if( g_dev.idbm )
@@ -128,6 +130,11 @@ void  on_connect_resp( uint8_t *rx_data, uint32_t rx_sz )
             g_dev.idbm = strdup( idbm->valuestring );
             printf( "g_dev.idbm->%s\n", g_dev.idbm );
         }
+        if( ntp && cJSON_IsString( ntp ) ){
+            printf( "------get ntp srv name:%s\n", ntp->valuestring );
+            ntp_update( ntp->valuestring );
+        }
+        cJSON_Delete(root);
     }
 }
 
@@ -314,7 +321,7 @@ void iot_tx_data( uint8_t *data, uint32_t sz )
             }
         }
     }else{
-        printf( "tcp write data  -- tx_busy->1\n" );
+        printf( "tcp write data[sz:%d]  -- tx_busy->1\n",sz );
         s_iot.tx_busy = 1;
         //uv_write
         uv_buf_t uvBuf = uv_buf_init( data, sz );
index 348ef1ae6bf664639d6f1f2e922e9f42707edb6c..b5478bd8d3dce0a46212cef1ed5bbcb975dd95cf 100644 (file)
@@ -634,7 +634,8 @@ char* handle_iot_rx_data( char * data, int sz )
     if( item_ccode && item_devices && item_time && item_type
                     && item_devices_device && cJSON_IsString(item_devices_device )
                     && item_devices_tagcode && cJSON_IsString(item_devices_tagcode )
-                    && strcmp( item_type->valuestring, "write")== 0){
+                    && strcmp( item_type->valuestring, "write")== 0
+                    && item_devices_value && cJSON_IsString(item_devices_value )){
         if( strcmp(item_devices_device->valuestring, "gddykz" )==0 ){
             //power ctrl
             if( strcmp(item_devices_tagcode->valuestring, "jbq" )== 0 ){
@@ -663,7 +664,7 @@ char* handle_iot_rx_data( char * data, int sz )
 
         }else if( strcmp(item_devices_device->valuestring, "alarm" )==0 ){
             //remote alarm
-            printf( "\n\n---------alarm:%s\n", item_devices_value->valuestring );
+            printf( "\n\n--------remote -- alarm:%s\n", item_devices_value->valuestring );
             resp = wrap_ctrl_resp_json( item_ccode->valuestring, item_devices_device->valuestring, "true", item_devices_tagcode->valuestring, item_devices_value->valuestring, NULL, "write" );
             remote_alarm(item_devices_value->valuestring[0]-'0');
         }else if( strcmp(item_devices_device->valuestring, "upgrade" )==0 ){
@@ -703,6 +704,11 @@ char* handle_iot_rx_data( char * data, int sz )
 
             }
             resp = wrap_ctrl_resp_json( item_ccode->valuestring, item_devices_device->valuestring, "true", item_devices_tagcode->valuestring, item_devices_value->valuestring, NULL, "write" );
+        }else if( strcmp(item_devices_device->valuestring, "ntp" )==0 ){
+            //remote alarm
+            printf( "\n\n--------remote --set-ntp:[%s]\n", item_devices_value->valuestring );
+            resp = wrap_ctrl_resp_json( item_ccode->valuestring, item_devices_device->valuestring, "true", item_devices_tagcode->valuestring, item_devices_value->valuestring, NULL, "write" );
+            ntp_update( item_devices_value->valuestring );
         }
     }
 
@@ -748,6 +754,8 @@ cJSON* rpt_pkt_encrypted( int secret,  cJSON * item )
         //pkcs7
         int pad_len = 16-(strlen( item_str )%16);
 
+        cJSON_Delete( item );
+
         plaintext_len = strlen(item_str)+pad_len;
         plaintext =  malloc( plaintext_len + 1);