From: kyz Date: Wed, 4 Jun 2025 07:37:33 +0000 (+0800) Subject: add ntpd config X-Git-Url: http://kyz.pub/gitweb/?a=commitdiff_plain;h=a228778cc10bcc5e64be1eb570691622ef33fc20;p=yn%2Falarmmonitor.git add ntpd config --- diff --git a/CMakeLists.txt b/CMakeLists.txt index a07c2db..ca7c941 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 ) diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 22a8a6e..80592f6 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -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) diff --git a/app/S99_alarmmonitor b/app/S99_alarmmonitor index 6d40f50..7fbca59 100755 --- a/app/S99_alarmmonitor +++ b/app/S99_alarmmonitor @@ -6,7 +6,6 @@ case "$1" in start) - start /app/alarm_d start & ;; stop) diff --git a/app/inc/alarmmonitor.h b/app/inc/alarmmonitor.h index 1d48268..b93bd9b 100644 --- a/app/inc/alarmmonitor.h +++ b/app/inc/alarmmonitor.h @@ -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 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 + diff --git a/app/src/am_main.c b/app/src/am_main.c index 793efc1..0f0b997 100644 --- a/app/src/am_main.c +++ b/app/src/am_main.c @@ -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; diff --git a/app/src/iot.c b/app/src/iot.c index 33e4904..bdf5322 100644 --- a/app/src/iot.c +++ b/app/src/iot.c @@ -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 ); diff --git a/app/src/iot_report.c b/app/src/iot_report.c index 348ef1a..b5478bd 100644 --- a/app/src/iot_report.c +++ b/app/src/iot_report.c @@ -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);